1
0

0000-raspberry-pi.patch 3.3 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421534225342353424534255342653427534285342953430534315343253433534345343553436534375343853439534405344153442534435344453445534465344753448534495345053451534525345353454534555345653457534585345953460534615346253463534645346553466534675346853469534705347153472534735347453475534765347753478534795348053481534825348353484534855348653487534885348953490534915349253493534945349553496534975349853499535005350153502535035350453505535065350753508535095351053511535125351353514535155351653517535185351953520535215352253523535245352553526535275352853529535305353153532535335353453535535365353753538535395354053541535425354353544535455354653547535485354953550535515355253553535545355553556535575355853559535605356153562535635356453565535665356753568535695357053571535725357353574535755357653577535785357953580535815358253583535845358553586535875358853589535905359153592535935359453595535965359753598535995360053601536025360353604536055360653607536085360953610536115361253613536145361553616536175361853619536205362153622536235362453625536265362753628536295363053631536325363353634536355363653637536385363953640536415364253643536445364553646536475364853649536505365153652536535365453655536565365753658536595366053661536625366353664536655366653667536685366953670536715367253673536745367553676536775367853679536805368153682536835368453685536865368753688536895369053691536925369353694536955369653697536985369953700537015370253703537045370553706537075370853709537105371153712537135371453715537165371753718537195372053721537225372353724537255372653727537285372953730537315373253733537345373553736537375373853739537405374153742537435374453745537465374753748537495375053751537525375353754537555375653757537585375953760537615376253763537645376553766537675376853769537705377153772537735377453775537765377753778537795378053781537825378353784537855378653787537885378953790537915379253793537945379553796537975379853799538005380153802538035380453805538065380753808538095381053811538125381353814538155381653817538185381953820538215382253823538245382553826538275382853829538305383153832538335383453835538365383753838538395384053841538425384353844538455384653847538485384953850538515385253853538545385553856538575385853859538605386153862538635386453865538665386753868538695387053871538725387353874538755387653877538785387953880538815388253883538845388553886538875388853889538905389153892538935389453895538965389753898538995390053901539025390353904539055390653907539085390953910539115391253913539145391553916539175391853919539205392153922539235392453925539265392753928539295393053931539325393353934539355393653937539385393953940539415394253943539445394553946539475394853949539505395153952539535395453955539565395753958539595396053961539625396353964539655396653967539685396953970539715397253973539745397553976539775397853979539805398153982539835398453985539865398753988539895399053991539925399353994539955399653997539985399954000540015400254003540045400554006540075400854009540105401154012540135401454015540165401754018540195402054021540225402354024540255402654027540285402954030540315403254033540345403554036540375403854039540405404154042540435404454045540465404754048540495405054051540525405354054540555405654057540585405954060540615406254063540645406554066540675406854069540705407154072540735407454075540765407754078540795408054081540825408354084540855408654087540885408954090540915409254093540945409554096540975409854099541005410154102541035410454105541065410754108541095411054111541125411354114541155411654117541185411954120541215412254123541245412554126541275412854129541305413154132541335413454135541365413754138541395414054141541425414354144541455414654147541485414954150541515415254153541545415554156541575415854159541605416154162541635416454165541665416754168541695417054171541725417354174541755417654177541785417954180541815418254183541845418554186541875418854189541905419154192541935419454195541965419754198541995420054201542025420354204542055420654207542085420954210542115421254213542145421554216542175421854219542205422154222542235422454225542265422754228542295423054231542325423354234542355423654237542385423954240542415424254243542445424554246542475424854249542505425154252542535425454255542565425754258542595426054261542625426354264542655426654267542685426954270542715427254273542745427554276542775427854279542805428154282542835428454285542865428754288542895429054291542925429354294542955429654297542985429954300543015430254303543045430554306543075430854309543105431154312543135431454315543165431754318543195432054321543225432354324543255432654327543285432954330543315433254333543345433554336543375433854339543405434154342543435434454345543465434754348543495435054351543525435354354543555435654357543585435954360543615436254363543645436554366543675436854369543705437154372543735437454375543765437754378543795438054381543825438354384543855438654387543885438954390543915439254393543945439554396543975439854399544005440154402544035440454405544065440754408544095441054411544125441354414544155441654417544185441954420544215442254423544245442554426544275442854429544305443154432544335443454435544365443754438544395444054441544425444354444544455444654447544485444954450544515445254453544545445554456544575445854459544605446154462544635446454465544665446754468544695447054471544725447354474544755447654477544785447954480544815448254483544845448554486544875448854489544905449154492544935449454495544965449754498544995450054501545025450354504545055450654507545085450954510545115451254513545145451554516545175451854519545205452154522545235452454525545265452754528545295453054531545325453354534545355453654537545385453954540545415454254543545445454554546545475454854549545505455154552545535455454555545565455754558545595456054561545625456354564545655456654567545685456954570545715457254573545745457554576545775457854579545805458154582545835458454585545865458754588545895459054591545925459354594545955459654597545985459954600546015460254603546045460554606546075460854609546105461154612546135461454615546165461754618546195462054621546225462354624546255462654627546285462954630546315463254633546345463554636546375463854639546405464154642546435464454645546465464754648546495465054651546525465354654546555465654657546585465954660546615466254663546645466554666546675466854669546705467154672546735467454675546765467754678546795468054681546825468354684546855468654687546885468954690546915469254693546945469554696546975469854699547005470154702547035470454705547065470754708547095471054711547125471354714547155471654717547185471954720547215472254723547245472554726547275472854729547305473154732547335473454735547365473754738547395474054741547425474354744547455474654747547485474954750547515475254753547545475554756547575475854759547605476154762547635476454765547665476754768547695477054771547725477354774547755477654777547785477954780547815478254783547845478554786547875478854789547905479154792547935479454795547965479754798547995480054801548025480354804548055480654807548085480954810548115481254813548145481554816548175481854819548205482154822548235482454825548265482754828548295483054831548325483354834548355483654837548385483954840548415484254843548445484554846548475484854849548505485154852548535485454855548565485754858548595486054861548625486354864548655486654867548685486954870548715487254873548745487554876548775487854879548805488154882548835488454885548865488754888548895489054891548925489354894548955489654897548985489954900549015490254903549045490554906549075490854909549105491154912549135491454915549165491754918549195492054921549225492354924549255492654927549285492954930549315493254933549345493554936549375493854939549405494154942549435494454945549465494754948549495495054951549525495354954549555495654957549585495954960549615496254963549645496554966549675496854969549705497154972549735497454975549765497754978549795498054981549825498354984549855498654987549885498954990549915499254993549945499554996549975499854999550005500155002550035500455005550065500755008550095501055011550125501355014550155501655017550185501955020550215502255023550245502555026550275502855029550305503155032550335503455035550365503755038550395504055041550425504355044550455504655047550485504955050550515505255053550545505555056550575505855059550605506155062550635506455065550665506755068550695507055071550725507355074550755507655077550785507955080550815508255083550845508555086550875508855089550905509155092550935509455095550965509755098550995510055101551025510355104551055510655107551085510955110551115511255113551145511555116551175511855119551205512155122551235512455125551265512755128551295513055131551325513355134551355513655137551385513955140551415514255143551445514555146551475514855149551505515155152551535515455155551565515755158551595516055161551625516355164551655516655167551685516955170551715517255173551745517555176551775517855179551805518155182551835518455185551865518755188551895519055191551925519355194551955519655197551985519955200552015520255203552045520555206552075520855209552105521155212552135521455215552165521755218552195522055221552225522355224552255522655227552285522955230552315523255233552345523555236552375523855239552405524155242552435524455245552465524755248552495525055251552525525355254552555525655257552585525955260552615526255263552645526555266552675526855269552705527155272552735527455275552765527755278552795528055281552825528355284552855528655287552885528955290552915529255293552945529555296552975529855299553005530155302553035530455305553065530755308553095531055311553125531355314553155531655317553185531955320553215532255323553245532555326553275532855329553305533155332553335533455335553365533755338553395534055341553425534355344553455534655347553485534955350553515535255353553545535555356553575535855359553605536155362553635536455365553665536755368553695537055371553725537355374553755537655377553785537955380553815538255383553845538555386553875538855389553905539155392553935539455395553965539755398553995540055401554025540355404554055540655407554085540955410554115541255413554145541555416554175541855419554205542155422554235542455425554265542755428554295543055431554325543355434554355543655437554385543955440554415544255443554445544555446554475544855449554505545155452554535545455455554565545755458554595546055461554625546355464554655546655467554685546955470554715547255473554745547555476554775547855479554805548155482554835548455485554865548755488554895549055491554925549355494554955549655497554985549955500555015550255503555045550555506555075550855509555105551155512555135551455515555165551755518555195552055521555225552355524555255552655527555285552955530555315553255533555345553555536555375553855539555405554155542555435554455545555465554755548555495555055551555525555355554555555555655557555585555955560555615556255563555645556555566555675556855569555705557155572555735557455575555765557755578555795558055581555825558355584555855558655587555885558955590555915559255593555945559555596555975559855599556005560155602556035560455605556065560755608556095561055611556125561355614556155561655617556185561955620556215562255623556245562555626556275562855629556305563155632556335563455635556365563755638556395564055641556425564355644556455564655647556485564955650556515565255653556545565555656556575565855659556605566155662556635566455665556665566755668556695567055671556725567355674556755567655677556785567955680556815568255683556845568555686556875568855689556905569155692556935569455695556965569755698556995570055701557025570355704557055570655707557085570955710557115571255713557145571555716557175571855719557205572155722557235572455725557265572755728557295573055731557325573355734557355573655737557385573955740557415574255743557445574555746557475574855749557505575155752557535575455755557565575755758557595576055761557625576355764557655576655767557685576955770557715577255773557745577555776557775577855779557805578155782557835578455785557865578755788557895579055791557925579355794557955579655797557985579955800558015580255803558045580555806558075580855809558105581155812558135581455815558165581755818558195582055821558225582355824558255582655827558285582955830558315583255833558345583555836558375583855839558405584155842558435584455845558465584755848558495585055851558525585355854558555585655857558585585955860558615586255863558645586555866558675586855869558705587155872558735587455875558765587755878558795588055881558825588355884558855588655887558885588955890558915589255893558945589555896558975589855899559005590155902559035590455905559065590755908559095591055911559125591355914559155591655917559185591955920559215592255923559245592555926559275592855929559305593155932559335593455935559365593755938559395594055941559425594355944559455594655947559485594955950559515595255953559545595555956559575595855959559605596155962559635596455965559665596755968559695597055971559725597355974559755597655977559785597955980559815598255983559845598555986559875598855989559905599155992559935599455995559965599755998559995600056001560025600356004560055600656007560085600956010560115601256013560145601556016560175601856019560205602156022560235602456025560265602756028560295603056031560325603356034560355603656037560385603956040560415604256043560445604556046560475604856049560505605156052560535605456055560565605756058560595606056061560625606356064560655606656067560685606956070560715607256073560745607556076560775607856079560805608156082560835608456085560865608756088560895609056091560925609356094560955609656097560985609956100561015610256103561045610556106561075610856109561105611156112561135611456115561165611756118561195612056121561225612356124561255612656127561285612956130561315613256133561345613556136561375613856139561405614156142561435614456145561465614756148561495615056151561525615356154561555615656157561585615956160561615616256163561645616556166561675616856169561705617156172561735617456175561765617756178561795618056181561825618356184561855618656187561885618956190561915619256193561945619556196561975619856199562005620156202562035620456205562065620756208562095621056211562125621356214562155621656217562185621956220562215622256223562245622556226562275622856229562305623156232562335623456235562365623756238562395624056241562425624356244562455624656247562485624956250562515625256253562545625556256562575625856259562605626156262562635626456265562665626756268562695627056271562725627356274562755627656277562785627956280562815628256283562845628556286562875628856289562905629156292562935629456295562965629756298562995630056301563025630356304563055630656307563085630956310563115631256313563145631556316563175631856319563205632156322563235632456325563265632756328563295633056331563325633356334563355633656337563385633956340563415634256343563445634556346563475634856349563505635156352563535635456355563565635756358563595636056361563625636356364563655636656367563685636956370563715637256373563745637556376563775637856379563805638156382563835638456385563865638756388563895639056391563925639356394563955639656397563985639956400564015640256403564045640556406564075640856409564105641156412564135641456415564165641756418564195642056421564225642356424564255642656427564285642956430564315643256433564345643556436564375643856439564405644156442564435644456445564465644756448564495645056451564525645356454564555645656457564585645956460564615646256463564645646556466564675646856469564705647156472564735647456475564765647756478564795648056481564825648356484564855648656487564885648956490564915649256493564945649556496564975649856499565005650156502565035650456505565065650756508565095651056511565125651356514565155651656517565185651956520565215652256523565245652556526565275652856529565305653156532565335653456535565365653756538565395654056541565425654356544565455654656547565485654956550565515655256553565545655556556565575655856559565605656156562565635656456565565665656756568565695657056571565725657356574565755657656577565785657956580565815658256583565845658556586565875658856589565905659156592565935659456595565965659756598565995660056601566025660356604566055660656607566085660956610566115661256613566145661556616566175661856619566205662156622566235662456625566265662756628566295663056631566325663356634566355663656637566385663956640566415664256643566445664556646566475664856649566505665156652566535665456655566565665756658566595666056661566625666356664566655666656667566685666956670566715667256673566745667556676566775667856679566805668156682566835668456685566865668756688566895669056691566925669356694566955669656697566985669956700567015670256703567045670556706567075670856709567105671156712567135671456715567165671756718567195672056721567225672356724567255672656727567285672956730567315673256733567345673556736567375673856739567405674156742567435674456745567465674756748567495675056751567525675356754567555675656757567585675956760567615676256763567645676556766567675676856769567705677156772567735677456775567765677756778567795678056781567825678356784567855678656787567885678956790567915679256793567945679556796567975679856799568005680156802568035680456805568065680756808568095681056811568125681356814568155681656817568185681956820568215682256823568245682556826568275682856829568305683156832568335683456835568365683756838568395684056841568425684356844568455684656847568485684956850568515685256853568545685556856568575685856859568605686156862568635686456865568665686756868568695687056871568725687356874568755687656877568785687956880568815688256883568845688556886568875688856889568905689156892568935689456895568965689756898568995690056901569025690356904569055690656907569085690956910569115691256913569145691556916569175691856919569205692156922569235692456925569265692756928569295693056931569325693356934569355693656937569385693956940569415694256943569445694556946569475694856949569505695156952569535695456955569565695756958569595696056961569625696356964569655696656967569685696956970569715697256973569745697556976569775697856979569805698156982569835698456985569865698756988569895699056991569925699356994569955699656997569985699957000570015700257003570045700557006570075700857009570105701157012570135701457015570165701757018570195702057021570225702357024570255702657027570285702957030570315703257033570345703557036570375703857039570405704157042570435704457045570465704757048570495705057051570525705357054570555705657057570585705957060570615706257063570645706557066570675706857069570705707157072570735707457075570765707757078570795708057081570825708357084570855708657087570885708957090570915709257093570945709557096570975709857099571005710157102571035710457105571065710757108571095711057111571125711357114571155711657117571185711957120571215712257123571245712557126571275712857129571305713157132571335713457135571365713757138571395714057141571425714357144571455714657147571485714957150571515715257153571545715557156571575715857159571605716157162571635716457165571665716757168571695717057171571725717357174571755717657177571785717957180571815718257183571845718557186571875718857189571905719157192571935719457195571965719757198571995720057201572025720357204572055720657207572085720957210572115721257213572145721557216572175721857219572205722157222572235722457225572265722757228572295723057231572325723357234572355723657237572385723957240572415724257243572445724557246572475724857249572505725157252572535725457255572565725757258572595726057261572625726357264572655726657267572685726957270572715727257273572745727557276572775727857279572805728157282572835728457285572865728757288572895729057291572925729357294572955729657297572985729957300573015730257303573045730557306573075730857309573105731157312573135731457315573165731757318573195732057321573225732357324573255732657327573285732957330573315733257333573345733557336573375733857339573405734157342573435734457345573465734757348573495735057351573525735357354573555735657357573585735957360573615736257363573645736557366573675736857369573705737157372573735737457375573765737757378573795738057381573825738357384573855738657387573885738957390573915739257393573945739557396573975739857399574005740157402574035740457405574065740757408574095741057411574125741357414574155741657417574185741957420574215742257423574245742557426574275742857429574305743157432574335743457435574365743757438574395744057441574425744357444574455744657447574485744957450574515745257453574545745557456574575745857459574605746157462574635746457465574665746757468574695747057471574725747357474574755747657477574785747957480574815748257483574845748557486574875748857489574905749157492574935749457495574965749757498574995750057501575025750357504575055750657507575085750957510575115751257513575145751557516575175751857519575205752157522575235752457525575265752757528575295753057531575325753357534575355753657537575385753957540575415754257543575445754557546575475754857549575505755157552575535755457555575565755757558575595756057561575625756357564575655756657567575685756957570575715757257573575745757557576575775757857579575805758157582575835758457585575865758757588575895759057591575925759357594575955759657597575985759957600576015760257603576045760557606576075760857609576105761157612576135761457615576165761757618576195762057621576225762357624576255762657627576285762957630576315763257633576345763557636576375763857639576405764157642576435764457645576465764757648576495765057651576525765357654576555765657657576585765957660576615766257663576645766557666576675766857669576705767157672576735767457675576765767757678576795768057681576825768357684576855768657687576885768957690576915769257693576945769557696576975769857699577005770157702577035770457705577065770757708577095771057711577125771357714577155771657717577185771957720577215772257723577245772557726577275772857729577305773157732577335773457735577365773757738577395774057741577425774357744577455774657747577485774957750577515775257753577545775557756577575775857759577605776157762577635776457765577665776757768577695777057771577725777357774577755777657777577785777957780577815778257783577845778557786577875778857789577905779157792577935779457795577965779757798577995780057801578025780357804578055780657807578085780957810578115781257813578145781557816578175781857819578205782157822578235782457825578265782757828578295783057831578325783357834578355783657837578385783957840578415784257843578445784557846578475784857849578505785157852578535785457855578565785757858578595786057861578625786357864578655786657867578685786957870578715787257873578745787557876578775787857879578805788157882578835788457885578865788757888578895789057891578925789357894578955789657897578985789957900579015790257903579045790557906579075790857909579105791157912579135791457915579165791757918579195792057921579225792357924579255792657927579285792957930579315793257933579345793557936579375793857939579405794157942579435794457945579465794757948579495795057951579525795357954579555795657957579585795957960579615796257963579645796557966579675796857969579705797157972579735797457975579765797757978579795798057981579825798357984579855798657987579885798957990579915799257993579945799557996579975799857999580005800158002580035800458005580065800758008580095801058011580125801358014580155801658017580185801958020580215802258023580245802558026580275802858029580305803158032580335803458035580365803758038580395804058041580425804358044580455804658047580485804958050580515805258053580545805558056580575805858059580605806158062580635806458065580665806758068580695807058071580725807358074580755807658077580785807958080580815808258083580845808558086580875808858089580905809158092580935809458095580965809758098580995810058101581025810358104581055810658107581085810958110581115811258113581145811558116581175811858119581205812158122581235812458125581265812758128581295813058131581325813358134581355813658137581385813958140581415814258143581445814558146581475814858149581505815158152581535815458155581565815758158581595816058161581625816358164581655816658167581685816958170581715817258173581745817558176581775817858179581805818158182581835818458185581865818758188581895819058191581925819358194581955819658197581985819958200582015820258203582045820558206582075820858209582105821158212582135821458215582165821758218582195822058221582225822358224582255822658227582285822958230582315823258233582345823558236582375823858239582405824158242582435824458245582465824758248582495825058251582525825358254582555825658257582585825958260582615826258263582645826558266582675826858269582705827158272582735827458275582765827758278582795828058281582825828358284582855828658287582885828958290582915829258293582945829558296582975829858299583005830158302583035830458305583065830758308583095831058311583125831358314583155831658317583185831958320583215832258323583245832558326583275832858329583305833158332583335833458335583365833758338583395834058341583425834358344583455834658347583485834958350583515835258353583545835558356583575835858359583605836158362583635836458365583665836758368583695837058371583725837358374583755837658377583785837958380583815838258383583845838558386583875838858389583905839158392583935839458395583965839758398583995840058401584025840358404584055840658407584085840958410584115841258413584145841558416584175841858419584205842158422584235842458425584265842758428584295843058431584325843358434584355843658437584385843958440584415844258443584445844558446584475844858449584505845158452584535845458455584565845758458584595846058461584625846358464584655846658467584685846958470584715847258473584745847558476584775847858479584805848158482584835848458485584865848758488584895849058491584925849358494584955849658497584985849958500585015850258503585045850558506585075850858509585105851158512585135851458515585165851758518585195852058521585225852358524585255852658527585285852958530585315853258533585345853558536585375853858539585405854158542585435854458545585465854758548585495855058551585525855358554585555855658557585585855958560585615856258563585645856558566585675856858569585705857158572585735857458575585765857758578585795858058581585825858358584585855858658587585885858958590585915859258593585945859558596585975859858599586005860158602586035860458605586065860758608586095861058611586125861358614586155861658617586185861958620586215862258623586245862558626586275862858629586305863158632586335863458635586365863758638586395864058641586425864358644586455864658647586485864958650586515865258653586545865558656586575865858659586605866158662586635866458665586665866758668586695867058671586725867358674586755867658677586785867958680586815868258683586845868558686586875868858689586905869158692586935869458695586965869758698586995870058701587025870358704587055870658707587085870958710587115871258713587145871558716587175871858719587205872158722587235872458725587265872758728587295873058731587325873358734587355873658737587385873958740587415874258743587445874558746587475874858749587505875158752587535875458755587565875758758587595876058761587625876358764587655876658767587685876958770587715877258773587745877558776587775877858779587805878158782587835878458785587865878758788587895879058791587925879358794587955879658797587985879958800588015880258803588045880558806588075880858809588105881158812588135881458815588165881758818588195882058821588225882358824588255882658827588285882958830588315883258833588345883558836588375883858839588405884158842588435884458845588465884758848588495885058851588525885358854588555885658857588585885958860588615886258863588645886558866588675886858869588705887158872588735887458875588765887758878588795888058881588825888358884588855888658887588885888958890588915889258893588945889558896588975889858899589005890158902589035890458905589065890758908589095891058911589125891358914589155891658917589185891958920589215892258923589245892558926589275892858929589305893158932589335893458935589365893758938589395894058941589425894358944589455894658947589485894958950589515895258953589545895558956589575895858959589605896158962589635896458965589665896758968589695897058971589725897358974589755897658977589785897958980589815898258983589845898558986589875898858989589905899158992589935899458995589965899758998589995900059001590025900359004590055900659007590085900959010590115901259013590145901559016590175901859019590205902159022590235902459025590265902759028590295903059031590325903359034590355903659037590385903959040590415904259043590445904559046590475904859049590505905159052590535905459055590565905759058590595906059061590625906359064590655906659067590685906959070590715907259073590745907559076590775907859079590805908159082590835908459085590865908759088590895909059091590925909359094590955909659097590985909959100591015910259103591045910559106591075910859109591105911159112591135911459115591165911759118591195912059121591225912359124591255912659127591285912959130591315913259133591345913559136591375913859139591405914159142591435914459145591465914759148591495915059151591525915359154591555915659157591585915959160591615916259163591645916559166591675916859169591705917159172591735917459175591765917759178591795918059181591825918359184591855918659187591885918959190591915919259193591945919559196591975919859199592005920159202592035920459205592065920759208592095921059211592125921359214592155921659217592185921959220592215922259223592245922559226592275922859229592305923159232592335923459235592365923759238592395924059241592425924359244592455924659247592485924959250592515925259253592545925559256592575925859259592605926159262592635926459265592665926759268592695927059271592725927359274592755927659277592785927959280592815928259283592845928559286592875928859289592905929159292592935929459295592965929759298592995930059301593025930359304593055930659307593085930959310593115931259313593145931559316593175931859319593205932159322593235932459325593265932759328593295933059331593325933359334593355933659337593385933959340593415934259343593445934559346593475934859349593505935159352593535935459355593565935759358593595936059361593625936359364593655936659367593685936959370593715937259373593745937559376593775937859379593805938159382593835938459385593865938759388593895939059391593925939359394593955939659397593985939959400594015940259403594045940559406594075940859409594105941159412594135941459415594165941759418594195942059421594225942359424594255942659427594285942959430594315943259433594345943559436594375943859439594405944159442594435944459445594465944759448594495945059451594525945359454594555945659457594585945959460594615946259463594645946559466594675946859469594705947159472594735947459475594765947759478594795948059481594825948359484594855948659487594885948959490594915949259493594945949559496594975949859499595005950159502595035950459505595065950759508595095951059511595125951359514595155951659517595185951959520595215952259523595245952559526595275952859529595305953159532595335953459535595365953759538595395954059541595425954359544595455954659547595485954959550595515955259553595545955559556595575955859559595605956159562595635956459565595665956759568595695957059571595725957359574595755957659577595785957959580595815958259583595845958559586595875958859589595905959159592595935959459595595965959759598595995960059601596025960359604596055960659607596085960959610596115961259613596145961559616596175961859619596205962159622596235962459625596265962759628596295963059631596325963359634596355963659637596385963959640596415964259643596445964559646596475964859649596505965159652596535965459655596565965759658596595966059661596625966359664596655966659667596685966959670596715967259673596745967559676596775967859679596805968159682596835968459685596865968759688596895969059691596925969359694596955969659697596985969959700597015970259703597045970559706597075970859709597105971159712597135971459715597165971759718597195972059721597225972359724597255972659727597285972959730597315973259733597345973559736597375973859739597405974159742597435974459745597465974759748597495975059751597525975359754597555975659757597585975959760597615976259763597645976559766597675976859769597705977159772597735977459775597765977759778597795978059781597825978359784597855978659787597885978959790597915979259793597945979559796597975979859799598005980159802598035980459805598065980759808598095981059811598125981359814598155981659817598185981959820598215982259823598245982559826598275982859829598305983159832598335983459835598365983759838598395984059841598425984359844598455984659847598485984959850598515985259853598545985559856598575985859859598605986159862598635986459865598665986759868598695987059871598725987359874598755987659877598785987959880598815988259883598845988559886598875988859889598905989159892598935989459895598965989759898598995990059901599025990359904599055990659907599085990959910599115991259913599145991559916599175991859919599205992159922599235992459925599265992759928599295993059931599325993359934599355993659937599385993959940599415994259943599445994559946599475994859949599505995159952599535995459955599565995759958599595996059961599625996359964599655996659967599685996959970599715997259973599745997559976599775997859979599805998159982599835998459985599865998759988599895999059991599925999359994599955999659997599985999960000600016000260003600046000560006600076000860009600106001160012600136001460015600166001760018600196002060021600226002360024600256002660027600286002960030600316003260033600346003560036600376003860039600406004160042600436004460045600466004760048600496005060051600526005360054600556005660057600586005960060600616006260063600646006560066600676006860069600706007160072600736007460075600766007760078600796008060081600826008360084600856008660087600886008960090600916009260093600946009560096600976009860099601006010160102601036010460105601066010760108601096011060111601126011360114601156011660117601186011960120601216012260123601246012560126601276012860129601306013160132601336013460135601366013760138601396014060141601426014360144601456014660147601486014960150601516015260153601546015560156601576015860159601606016160162601636016460165601666016760168601696017060171601726017360174601756017660177601786017960180601816018260183601846018560186601876018860189601906019160192601936019460195601966019760198601996020060201602026020360204602056020660207602086020960210602116021260213602146021560216602176021860219602206022160222602236022460225602266022760228602296023060231602326023360234602356023660237602386023960240602416024260243602446024560246602476024860249602506025160252602536025460255602566025760258602596026060261602626026360264602656026660267602686026960270602716027260273602746027560276602776027860279602806028160282602836028460285602866028760288602896029060291602926029360294602956029660297602986029960300603016030260303603046030560306603076030860309603106031160312603136031460315603166031760318603196032060321603226032360324603256032660327603286032960330603316033260333603346033560336603376033860339603406034160342603436034460345603466034760348603496035060351603526035360354603556035660357603586035960360603616036260363603646036560366603676036860369603706037160372603736037460375603766037760378603796038060381603826038360384603856038660387603886038960390603916039260393603946039560396603976039860399604006040160402604036040460405604066040760408604096041060411604126041360414604156041660417604186041960420604216042260423604246042560426604276042860429604306043160432604336043460435604366043760438604396044060441604426044360444604456044660447604486044960450604516045260453604546045560456604576045860459604606046160462604636046460465604666046760468604696047060471604726047360474604756047660477604786047960480604816048260483604846048560486604876048860489604906049160492604936049460495604966049760498604996050060501605026050360504605056050660507605086050960510605116051260513605146051560516605176051860519605206052160522605236052460525605266052760528605296053060531605326053360534605356053660537605386053960540605416054260543605446054560546605476054860549605506055160552605536055460555605566055760558605596056060561605626056360564605656056660567605686056960570605716057260573605746057560576605776057860579605806058160582605836058460585605866058760588605896059060591605926059360594605956059660597605986059960600606016060260603606046060560606606076060860609606106061160612606136061460615606166061760618606196062060621606226062360624606256062660627606286062960630606316063260633606346063560636606376063860639606406064160642606436064460645606466064760648606496065060651606526065360654606556065660657606586065960660606616066260663606646066560666606676066860669606706067160672606736067460675606766067760678606796068060681606826068360684606856068660687606886068960690606916069260693606946069560696606976069860699607006070160702607036070460705607066070760708607096071060711607126071360714607156071660717607186071960720607216072260723607246072560726607276072860729607306073160732607336073460735607366073760738607396074060741607426074360744607456074660747607486074960750607516075260753607546075560756607576075860759607606076160762607636076460765607666076760768607696077060771607726077360774607756077660777607786077960780607816078260783607846078560786607876078860789607906079160792607936079460795607966079760798607996080060801608026080360804608056080660807608086080960810608116081260813608146081560816608176081860819608206082160822608236082460825608266082760828608296083060831608326083360834608356083660837608386083960840608416084260843608446084560846608476084860849608506085160852608536085460855608566085760858608596086060861608626086360864608656086660867608686086960870608716087260873608746087560876608776087860879608806088160882608836088460885608866088760888608896089060891608926089360894608956089660897608986089960900609016090260903609046090560906609076090860909609106091160912609136091460915609166091760918609196092060921609226092360924609256092660927609286092960930609316093260933609346093560936609376093860939609406094160942609436094460945609466094760948609496095060951609526095360954609556095660957609586095960960609616096260963609646096560966609676096860969609706097160972609736097460975609766097760978609796098060981609826098360984609856098660987609886098960990609916099260993609946099560996609976099860999610006100161002610036100461005610066100761008610096101061011610126101361014610156101661017610186101961020610216102261023610246102561026610276102861029610306103161032610336103461035610366103761038610396104061041610426104361044610456104661047610486104961050610516105261053610546105561056610576105861059610606106161062610636106461065610666106761068610696107061071610726107361074610756107661077610786107961080610816108261083610846108561086610876108861089610906109161092610936109461095610966109761098610996110061101611026110361104611056110661107611086110961110611116111261113611146111561116611176111861119611206112161122611236112461125611266112761128611296113061131611326113361134611356113661137611386113961140611416114261143611446114561146611476114861149611506115161152611536115461155611566115761158611596116061161611626116361164611656116661167611686116961170611716117261173611746117561176611776117861179611806118161182611836118461185611866118761188611896119061191611926119361194611956119661197611986119961200612016120261203612046120561206612076120861209612106121161212612136121461215612166121761218612196122061221612226122361224612256122661227612286122961230612316123261233612346123561236612376123861239612406124161242612436124461245612466124761248612496125061251612526125361254612556125661257612586125961260612616126261263612646126561266612676126861269612706127161272612736127461275612766127761278612796128061281612826128361284612856128661287612886128961290612916129261293612946129561296612976129861299613006130161302613036130461305613066130761308613096131061311613126131361314613156131661317613186131961320613216132261323613246132561326613276132861329613306133161332613336133461335613366133761338613396134061341613426134361344613456134661347613486134961350613516135261353613546135561356613576135861359613606136161362613636136461365613666136761368613696137061371613726137361374613756137661377613786137961380613816138261383613846138561386613876138861389613906139161392613936139461395613966139761398613996140061401614026140361404614056140661407614086140961410614116141261413614146141561416614176141861419614206142161422614236142461425614266142761428614296143061431614326143361434614356143661437614386143961440614416144261443614446144561446614476144861449614506145161452614536145461455614566145761458614596146061461614626146361464614656146661467614686146961470614716147261473614746147561476614776147861479614806148161482614836148461485614866148761488614896149061491614926149361494614956149661497614986149961500615016150261503615046150561506615076150861509615106151161512615136151461515615166151761518615196152061521615226152361524615256152661527615286152961530615316153261533615346153561536615376153861539615406154161542615436154461545615466154761548615496155061551615526155361554615556155661557615586155961560615616156261563615646156561566615676156861569615706157161572615736157461575615766157761578615796158061581615826158361584615856158661587615886158961590615916159261593615946159561596615976159861599616006160161602616036160461605616066160761608616096161061611616126161361614616156161661617616186161961620616216162261623616246162561626616276162861629616306163161632616336163461635616366163761638616396164061641616426164361644616456164661647616486164961650616516165261653616546165561656616576165861659616606166161662616636166461665616666166761668616696167061671616726167361674616756167661677616786167961680616816168261683616846168561686616876168861689616906169161692616936169461695616966169761698616996170061701617026170361704617056170661707617086170961710617116171261713617146171561716617176171861719617206172161722617236172461725617266172761728617296173061731617326173361734617356173661737617386173961740617416174261743617446174561746617476174861749617506175161752617536175461755617566175761758617596176061761617626176361764617656176661767617686176961770617716177261773617746177561776617776177861779617806178161782617836178461785617866178761788617896179061791617926179361794617956179661797617986179961800618016180261803618046180561806618076180861809618106181161812618136181461815618166181761818618196182061821618226182361824618256182661827618286182961830618316183261833618346183561836618376183861839618406184161842618436184461845618466184761848618496185061851618526185361854618556185661857618586185961860618616186261863618646186561866618676186861869618706187161872618736187461875618766187761878618796188061881618826188361884618856188661887618886188961890618916189261893618946189561896618976189861899619006190161902619036190461905619066190761908619096191061911619126191361914619156191661917619186191961920619216192261923619246192561926619276192861929619306193161932619336193461935619366193761938619396194061941619426194361944619456194661947619486194961950619516195261953619546195561956619576195861959619606196161962619636196461965619666196761968619696197061971619726197361974619756197661977619786197961980619816198261983619846198561986619876198861989619906199161992619936199461995619966199761998619996200062001620026200362004620056200662007620086200962010620116201262013620146201562016620176201862019620206202162022620236202462025620266202762028620296203062031620326203362034620356203662037620386203962040620416204262043620446204562046620476204862049620506205162052620536205462055620566205762058620596206062061620626206362064620656206662067620686206962070620716207262073620746207562076620776207862079620806208162082620836208462085620866208762088620896209062091620926209362094620956209662097620986209962100621016210262103621046210562106621076210862109621106211162112621136211462115621166211762118621196212062121621226212362124621256212662127621286212962130621316213262133621346213562136621376213862139621406214162142621436214462145621466214762148621496215062151621526215362154621556215662157621586215962160621616216262163621646216562166621676216862169621706217162172621736217462175621766217762178621796218062181621826218362184621856218662187621886218962190621916219262193621946219562196621976219862199622006220162202622036220462205622066220762208622096221062211622126221362214622156221662217622186221962220622216222262223622246222562226622276222862229622306223162232622336223462235622366223762238622396224062241622426224362244622456224662247622486224962250622516225262253622546225562256622576225862259622606226162262622636226462265622666226762268622696227062271622726227362274622756227662277622786227962280622816228262283622846228562286622876228862289622906229162292622936229462295622966229762298622996230062301623026230362304623056230662307623086230962310623116231262313623146231562316623176231862319623206232162322623236232462325623266232762328623296233062331623326233362334623356233662337623386233962340623416234262343623446234562346623476234862349623506235162352623536235462355623566235762358623596236062361623626236362364623656236662367623686236962370623716237262373623746237562376623776237862379623806238162382623836238462385623866238762388623896239062391623926239362394623956239662397623986239962400624016240262403624046240562406624076240862409624106241162412624136241462415624166241762418624196242062421624226242362424624256242662427624286242962430624316243262433624346243562436624376243862439624406244162442624436244462445624466244762448624496245062451624526245362454624556245662457624586245962460624616246262463624646246562466624676246862469624706247162472624736247462475624766247762478624796248062481624826248362484624856248662487624886248962490624916249262493624946249562496624976249862499625006250162502625036250462505625066250762508625096251062511625126251362514625156251662517625186251962520625216252262523625246252562526625276252862529625306253162532625336253462535625366253762538625396254062541625426254362544625456254662547625486254962550625516255262553625546255562556625576255862559625606256162562625636256462565625666256762568625696257062571625726257362574625756257662577625786257962580625816258262583625846258562586625876258862589625906259162592625936259462595625966259762598625996260062601626026260362604626056260662607626086260962610626116261262613626146261562616626176261862619626206262162622626236262462625626266262762628626296263062631626326263362634626356263662637626386263962640626416264262643626446264562646626476264862649626506265162652626536265462655626566265762658626596266062661626626266362664626656266662667626686266962670626716267262673626746267562676626776267862679626806268162682626836268462685626866268762688626896269062691626926269362694626956269662697626986269962700627016270262703627046270562706627076270862709627106271162712627136271462715627166271762718627196272062721627226272362724627256272662727627286272962730627316273262733627346273562736627376273862739627406274162742627436274462745627466274762748627496275062751627526275362754627556275662757627586275962760627616276262763627646276562766627676276862769627706277162772627736277462775627766277762778627796278062781627826278362784627856278662787627886278962790627916279262793627946279562796627976279862799628006280162802628036280462805628066280762808628096281062811628126281362814628156281662817628186281962820628216282262823628246282562826628276282862829628306283162832628336283462835628366283762838628396284062841628426284362844628456284662847628486284962850628516285262853628546285562856628576285862859628606286162862628636286462865628666286762868628696287062871628726287362874628756287662877628786287962880628816288262883628846288562886628876288862889628906289162892628936289462895628966289762898628996290062901629026290362904629056290662907629086290962910629116291262913629146291562916629176291862919629206292162922629236292462925629266292762928629296293062931629326293362934629356293662937629386293962940629416294262943629446294562946629476294862949629506295162952629536295462955629566295762958629596296062961629626296362964629656296662967629686296962970629716297262973629746297562976629776297862979629806298162982629836298462985629866298762988629896299062991629926299362994629956299662997629986299963000630016300263003630046300563006630076300863009630106301163012630136301463015630166301763018630196302063021630226302363024630256302663027630286302963030630316303263033630346303563036630376303863039630406304163042630436304463045630466304763048630496305063051630526305363054630556305663057630586305963060630616306263063630646306563066630676306863069630706307163072630736307463075630766307763078630796308063081630826308363084630856308663087630886308963090630916309263093630946309563096630976309863099631006310163102631036310463105631066310763108631096311063111631126311363114631156311663117631186311963120631216312263123631246312563126631276312863129631306313163132631336313463135631366313763138631396314063141631426314363144631456314663147631486314963150631516315263153631546315563156631576315863159631606316163162631636316463165631666316763168631696317063171631726317363174631756317663177631786317963180631816318263183631846318563186631876318863189631906319163192631936319463195631966319763198631996320063201632026320363204632056320663207632086320963210632116321263213632146321563216632176321863219632206322163222632236322463225632266322763228632296323063231632326323363234632356323663237632386323963240632416324263243632446324563246632476324863249632506325163252632536325463255632566325763258632596326063261632626326363264632656326663267632686326963270632716327263273632746327563276632776327863279632806328163282632836328463285632866328763288632896329063291632926329363294632956329663297632986329963300633016330263303633046330563306633076330863309633106331163312633136331463315633166331763318633196332063321633226332363324633256332663327633286332963330633316333263333633346333563336633376333863339633406334163342633436334463345633466334763348633496335063351633526335363354633556335663357633586335963360633616336263363633646336563366633676336863369633706337163372633736337463375633766337763378633796338063381633826338363384633856338663387633886338963390633916339263393633946339563396633976339863399634006340163402634036340463405634066340763408634096341063411634126341363414634156341663417634186341963420634216342263423634246342563426634276342863429634306343163432634336343463435634366343763438634396344063441634426344363444634456344663447634486344963450634516345263453634546345563456634576345863459634606346163462634636346463465634666346763468634696347063471634726347363474634756347663477634786347963480634816348263483634846348563486634876348863489634906349163492634936349463495634966349763498634996350063501635026350363504635056350663507635086350963510635116351263513635146351563516635176351863519635206352163522635236352463525635266352763528635296353063531635326353363534635356353663537635386353963540635416354263543635446354563546635476354863549635506355163552635536355463555635566355763558635596356063561635626356363564635656356663567635686356963570635716357263573635746357563576635776357863579635806358163582635836358463585635866358763588635896359063591635926359363594635956359663597635986359963600636016360263603636046360563606636076360863609636106361163612636136361463615636166361763618636196362063621636226362363624636256362663627636286362963630636316363263633636346363563636636376363863639636406364163642636436364463645636466364763648636496365063651636526365363654636556365663657636586365963660636616366263663636646366563666636676366863669636706367163672636736367463675636766367763678636796368063681636826368363684636856368663687636886368963690636916369263693636946369563696636976369863699637006370163702637036370463705637066370763708637096371063711637126371363714637156371663717637186371963720637216372263723637246372563726637276372863729637306373163732637336373463735637366373763738637396374063741637426374363744637456374663747637486374963750637516375263753637546375563756637576375863759637606376163762637636376463765637666376763768637696377063771637726377363774637756377663777637786377963780637816378263783637846378563786637876378863789637906379163792637936379463795637966379763798637996380063801638026380363804638056380663807638086380963810638116381263813638146381563816638176381863819638206382163822638236382463825638266382763828638296383063831638326383363834638356383663837638386383963840638416384263843638446384563846638476384863849638506385163852638536385463855638566385763858638596386063861638626386363864638656386663867638686386963870638716387263873638746387563876638776387863879638806388163882638836388463885638866388763888638896389063891638926389363894638956389663897638986389963900639016390263903639046390563906639076390863909639106391163912639136391463915639166391763918639196392063921639226392363924639256392663927639286392963930639316393263933639346393563936639376393863939639406394163942639436394463945639466394763948639496395063951639526395363954639556395663957639586395963960639616396263963639646396563966639676396863969639706397163972639736397463975639766397763978639796398063981639826398363984639856398663987639886398963990639916399263993639946399563996639976399863999640006400164002640036400464005640066400764008640096401064011640126401364014640156401664017640186401964020640216402264023640246402564026640276402864029640306403164032640336403464035640366403764038640396404064041640426404364044640456404664047640486404964050640516405264053640546405564056640576405864059640606406164062640636406464065640666406764068640696407064071640726407364074640756407664077640786407964080640816408264083640846408564086640876408864089640906409164092640936409464095640966409764098640996410064101641026410364104641056410664107641086410964110641116411264113641146411564116641176411864119641206412164122641236412464125641266412764128641296413064131641326413364134641356413664137641386413964140641416414264143641446414564146641476414864149641506415164152641536415464155641566415764158641596416064161641626416364164641656416664167641686416964170641716417264173641746417564176641776417864179641806418164182641836418464185641866418764188641896419064191641926419364194641956419664197641986419964200642016420264203642046420564206642076420864209642106421164212642136421464215642166421764218642196422064221642226422364224642256422664227642286422964230642316423264233642346423564236642376423864239642406424164242642436424464245642466424764248642496425064251642526425364254642556425664257642586425964260642616426264263642646426564266642676426864269642706427164272642736427464275642766427764278642796428064281642826428364284642856428664287642886428964290642916429264293642946429564296642976429864299643006430164302643036430464305643066430764308643096431064311643126431364314643156431664317643186431964320643216432264323643246432564326643276432864329643306433164332643336433464335643366433764338643396434064341643426434364344643456434664347643486434964350643516435264353643546435564356643576435864359643606436164362643636436464365643666436764368643696437064371643726437364374643756437664377643786437964380643816438264383643846438564386643876438864389643906439164392643936439464395643966439764398643996440064401644026440364404644056440664407644086440964410644116441264413644146441564416644176441864419644206442164422644236442464425644266442764428644296443064431644326443364434644356443664437644386443964440644416444264443644446444564446644476444864449644506445164452644536445464455644566445764458644596446064461644626446364464644656446664467644686446964470644716447264473644746447564476644776447864479644806448164482644836448464485644866448764488644896449064491644926449364494644956449664497644986449964500645016450264503645046450564506645076450864509645106451164512645136451464515645166451764518645196452064521645226452364524645256452664527645286452964530645316453264533645346453564536645376453864539645406454164542645436454464545645466454764548645496455064551645526455364554645556455664557645586455964560645616456264563645646456564566645676456864569645706457164572645736457464575645766457764578645796458064581645826458364584645856458664587645886458964590645916459264593645946459564596645976459864599646006460164602646036460464605646066460764608646096461064611646126461364614646156461664617646186461964620646216462264623646246462564626646276462864629646306463164632646336463464635646366463764638646396464064641646426464364644646456464664647646486464964650646516465264653646546465564656646576465864659646606466164662646636466464665646666466764668646696467064671646726467364674646756467664677646786467964680646816468264683646846468564686646876468864689646906469164692646936469464695646966469764698646996470064701647026470364704647056470664707647086470964710647116471264713647146471564716647176471864719647206472164722647236472464725647266472764728647296473064731647326473364734647356473664737647386473964740647416474264743647446474564746647476474864749647506475164752647536475464755647566475764758647596476064761647626476364764647656476664767647686476964770647716477264773647746477564776647776477864779647806478164782647836478464785647866478764788647896479064791647926479364794647956479664797647986479964800648016480264803648046480564806648076480864809648106481164812648136481464815648166481764818648196482064821648226482364824648256482664827648286482964830648316483264833648346483564836648376483864839648406484164842648436484464845648466484764848648496485064851648526485364854648556485664857648586485964860648616486264863648646486564866648676486864869648706487164872648736487464875648766487764878648796488064881648826488364884648856488664887648886488964890648916489264893648946489564896648976489864899649006490164902649036490464905649066490764908649096491064911649126491364914649156491664917649186491964920649216492264923649246492564926649276492864929649306493164932649336493464935649366493764938649396494064941649426494364944649456494664947649486494964950649516495264953649546495564956649576495864959649606496164962649636496464965649666496764968649696497064971649726497364974649756497664977649786497964980649816498264983649846498564986649876498864989649906499164992649936499464995649966499764998649996500065001650026500365004650056500665007650086500965010650116501265013650146501565016650176501865019650206502165022650236502465025650266502765028650296503065031650326503365034650356503665037650386503965040650416504265043650446504565046650476504865049650506505165052650536505465055650566505765058650596506065061650626506365064650656506665067650686506965070650716507265073650746507565076650776507865079650806508165082650836508465085650866508765088650896509065091650926509365094650956509665097650986509965100651016510265103651046510565106651076510865109651106511165112651136511465115651166511765118651196512065121651226512365124651256512665127651286512965130651316513265133651346513565136651376513865139651406514165142651436514465145651466514765148651496515065151651526515365154651556515665157651586515965160651616516265163651646516565166651676516865169651706517165172651736517465175651766517765178651796518065181651826518365184651856518665187651886518965190651916519265193651946519565196651976519865199652006520165202652036520465205652066520765208652096521065211652126521365214652156521665217652186521965220652216522265223652246522565226652276522865229652306523165232652336523465235652366523765238652396524065241652426524365244652456524665247652486524965250652516525265253652546525565256652576525865259652606526165262652636526465265652666526765268652696527065271652726527365274652756527665277652786527965280652816528265283652846528565286652876528865289652906529165292652936529465295652966529765298652996530065301653026530365304653056530665307653086530965310653116531265313653146531565316653176531865319653206532165322653236532465325653266532765328653296533065331653326533365334653356533665337653386533965340653416534265343653446534565346653476534865349653506535165352653536535465355653566535765358653596536065361653626536365364653656536665367653686536965370653716537265373653746537565376653776537865379653806538165382653836538465385653866538765388653896539065391653926539365394653956539665397653986539965400654016540265403654046540565406654076540865409654106541165412654136541465415654166541765418654196542065421654226542365424654256542665427654286542965430654316543265433654346543565436654376543865439654406544165442654436544465445654466544765448654496545065451654526545365454654556545665457654586545965460654616546265463654646546565466654676546865469654706547165472654736547465475654766547765478654796548065481654826548365484654856548665487654886548965490654916549265493654946549565496654976549865499655006550165502655036550465505655066550765508655096551065511655126551365514655156551665517655186551965520655216552265523655246552565526655276552865529655306553165532655336553465535655366553765538655396554065541655426554365544655456554665547655486554965550655516555265553655546555565556655576555865559655606556165562655636556465565655666556765568655696557065571655726557365574655756557665577655786557965580655816558265583655846558565586655876558865589655906559165592655936559465595655966559765598655996560065601656026560365604656056560665607656086560965610656116561265613656146561565616656176561865619656206562165622656236562465625656266562765628656296563065631656326563365634656356563665637656386563965640656416564265643656446564565646656476564865649656506565165652656536565465655656566565765658656596566065661656626566365664656656566665667656686566965670656716567265673656746567565676656776567865679656806568165682656836568465685656866568765688656896569065691656926569365694656956569665697656986569965700657016570265703657046570565706657076570865709657106571165712657136571465715657166571765718657196572065721657226572365724657256572665727657286572965730657316573265733657346573565736657376573865739657406574165742657436574465745657466574765748657496575065751657526575365754657556575665757657586575965760657616576265763657646576565766657676576865769657706577165772657736577465775657766577765778657796578065781657826578365784657856578665787657886578965790657916579265793657946579565796657976579865799658006580165802658036580465805658066580765808658096581065811658126581365814658156581665817658186581965820658216582265823658246582565826658276582865829658306583165832658336583465835658366583765838658396584065841658426584365844658456584665847658486584965850658516585265853658546585565856658576585865859658606586165862658636586465865658666586765868658696587065871658726587365874658756587665877658786587965880658816588265883658846588565886658876588865889658906589165892658936589465895658966589765898658996590065901659026590365904659056590665907659086590965910659116591265913659146591565916659176591865919659206592165922659236592465925659266592765928659296593065931659326593365934659356593665937659386593965940659416594265943659446594565946659476594865949659506595165952659536595465955659566595765958659596596065961659626596365964659656596665967659686596965970659716597265973659746597565976659776597865979659806598165982659836598465985659866598765988659896599065991659926599365994659956599665997659986599966000660016600266003660046600566006660076600866009660106601166012660136601466015660166601766018660196602066021660226602366024660256602666027660286602966030660316603266033660346603566036660376603866039660406604166042660436604466045660466604766048660496605066051660526605366054660556605666057660586605966060660616606266063660646606566066660676606866069660706607166072660736607466075660766607766078660796608066081660826608366084660856608666087660886608966090660916609266093660946609566096660976609866099661006610166102661036610466105661066610766108661096611066111661126611366114661156611666117661186611966120661216612266123661246612566126661276612866129661306613166132661336613466135661366613766138661396614066141661426614366144661456614666147661486614966150661516615266153661546615566156661576615866159661606616166162661636616466165661666616766168661696617066171661726617366174661756617666177661786617966180661816618266183661846618566186661876618866189661906619166192661936619466195661966619766198661996620066201662026620366204662056620666207662086620966210662116621266213662146621566216662176621866219662206622166222662236622466225662266622766228662296623066231662326623366234662356623666237662386623966240662416624266243662446624566246662476624866249662506625166252662536625466255662566625766258662596626066261662626626366264662656626666267662686626966270662716627266273662746627566276662776627866279662806628166282662836628466285662866628766288662896629066291662926629366294662956629666297662986629966300663016630266303663046630566306663076630866309663106631166312663136631466315663166631766318663196632066321663226632366324663256632666327663286632966330663316633266333663346633566336663376633866339663406634166342663436634466345663466634766348663496635066351663526635366354663556635666357663586635966360663616636266363663646636566366663676636866369663706637166372663736637466375663766637766378663796638066381663826638366384663856638666387663886638966390663916639266393663946639566396663976639866399664006640166402664036640466405664066640766408664096641066411664126641366414664156641666417664186641966420664216642266423664246642566426664276642866429664306643166432664336643466435664366643766438664396644066441664426644366444664456644666447664486644966450664516645266453664546645566456664576645866459664606646166462664636646466465664666646766468664696647066471664726647366474664756647666477664786647966480664816648266483664846648566486664876648866489664906649166492664936649466495664966649766498664996650066501665026650366504665056650666507665086650966510665116651266513665146651566516665176651866519665206652166522665236652466525665266652766528665296653066531665326653366534665356653666537665386653966540665416654266543665446654566546665476654866549665506655166552665536655466555665566655766558665596656066561665626656366564665656656666567665686656966570665716657266573665746657566576665776657866579665806658166582665836658466585665866658766588665896659066591665926659366594665956659666597665986659966600666016660266603666046660566606666076660866609666106661166612666136661466615666166661766618666196662066621666226662366624666256662666627666286662966630666316663266633666346663566636666376663866639666406664166642666436664466645666466664766648666496665066651666526665366654666556665666657666586665966660666616666266663666646666566666666676666866669666706667166672666736667466675666766667766678666796668066681666826668366684666856668666687666886668966690666916669266693666946669566696666976669866699667006670166702667036670466705667066670766708667096671066711667126671366714667156671666717667186671966720667216672266723667246672566726667276672866729667306673166732667336673466735667366673766738667396674066741667426674366744667456674666747667486674966750667516675266753667546675566756667576675866759667606676166762667636676466765667666676766768667696677066771667726677366774667756677666777667786677966780667816678266783667846678566786667876678866789667906679166792667936679466795667966679766798667996680066801668026680366804668056680666807668086680966810668116681266813668146681566816668176681866819668206682166822668236682466825668266682766828668296683066831668326683366834668356683666837668386683966840668416684266843668446684566846668476684866849668506685166852668536685466855668566685766858668596686066861668626686366864668656686666867668686686966870668716687266873668746687566876668776687866879668806688166882668836688466885668866688766888668896689066891668926689366894668956689666897668986689966900669016690266903669046690566906669076690866909669106691166912669136691466915669166691766918669196692066921669226692366924669256692666927669286692966930669316693266933669346693566936669376693866939669406694166942669436694466945669466694766948669496695066951669526695366954669556695666957669586695966960669616696266963669646696566966669676696866969669706697166972669736697466975669766697766978669796698066981669826698366984669856698666987669886698966990669916699266993669946699566996669976699866999670006700167002670036700467005670066700767008670096701067011670126701367014670156701667017670186701967020670216702267023670246702567026670276702867029670306703167032670336703467035670366703767038670396704067041670426704367044670456704667047670486704967050670516705267053670546705567056670576705867059670606706167062670636706467065670666706767068670696707067071670726707367074670756707667077670786707967080670816708267083670846708567086670876708867089670906709167092670936709467095670966709767098670996710067101671026710367104671056710667107671086710967110671116711267113671146711567116671176711867119671206712167122671236712467125671266712767128671296713067131671326713367134671356713667137671386713967140671416714267143671446714567146671476714867149671506715167152671536715467155671566715767158671596716067161671626716367164671656716667167671686716967170671716717267173671746717567176671776717867179671806718167182671836718467185671866718767188671896719067191671926719367194671956719667197671986719967200672016720267203672046720567206672076720867209672106721167212672136721467215672166721767218672196722067221672226722367224672256722667227672286722967230672316723267233672346723567236672376723867239672406724167242672436724467245672466724767248672496725067251672526725367254672556725667257672586725967260672616726267263672646726567266672676726867269672706727167272672736727467275672766727767278672796728067281672826728367284672856728667287672886728967290672916729267293672946729567296672976729867299673006730167302673036730467305673066730767308673096731067311673126731367314673156731667317673186731967320673216732267323673246732567326673276732867329673306733167332673336733467335673366733767338673396734067341673426734367344673456734667347673486734967350673516735267353673546735567356673576735867359673606736167362673636736467365673666736767368673696737067371673726737367374673756737667377673786737967380673816738267383673846738567386673876738867389673906739167392673936739467395673966739767398673996740067401674026740367404674056740667407674086740967410674116741267413674146741567416674176741867419674206742167422674236742467425674266742767428674296743067431674326743367434674356743667437674386743967440674416744267443674446744567446674476744867449674506745167452674536745467455674566745767458674596746067461674626746367464674656746667467674686746967470674716747267473674746747567476674776747867479674806748167482674836748467485674866748767488674896749067491674926749367494674956749667497674986749967500675016750267503675046750567506675076750867509675106751167512675136751467515675166751767518675196752067521675226752367524675256752667527675286752967530675316753267533675346753567536675376753867539675406754167542675436754467545675466754767548675496755067551675526755367554675556755667557675586755967560675616756267563675646756567566675676756867569675706757167572675736757467575675766757767578675796758067581675826758367584675856758667587675886758967590675916759267593675946759567596675976759867599676006760167602676036760467605676066760767608676096761067611676126761367614676156761667617676186761967620676216762267623676246762567626676276762867629676306763167632676336763467635676366763767638676396764067641676426764367644676456764667647676486764967650676516765267653676546765567656676576765867659676606766167662676636766467665676666766767668676696767067671676726767367674676756767667677676786767967680676816768267683676846768567686676876768867689676906769167692676936769467695676966769767698676996770067701677026770367704677056770667707677086770967710677116771267713677146771567716677176771867719677206772167722677236772467725677266772767728677296773067731677326773367734677356773667737677386773967740677416774267743677446774567746677476774867749677506775167752677536775467755677566775767758677596776067761677626776367764677656776667767677686776967770677716777267773677746777567776677776777867779677806778167782677836778467785677866778767788677896779067791677926779367794677956779667797677986779967800678016780267803678046780567806678076780867809678106781167812678136781467815678166781767818678196782067821678226782367824678256782667827678286782967830678316783267833678346783567836678376783867839678406784167842678436784467845678466784767848678496785067851678526785367854678556785667857678586785967860678616786267863678646786567866678676786867869678706787167872678736787467875678766787767878678796788067881678826788367884678856788667887678886788967890678916789267893678946789567896678976789867899679006790167902679036790467905679066790767908679096791067911679126791367914679156791667917679186791967920679216792267923679246792567926679276792867929679306793167932679336793467935679366793767938679396794067941679426794367944679456794667947679486794967950679516795267953679546795567956679576795867959679606796167962679636796467965679666796767968679696797067971679726797367974679756797667977679786797967980679816798267983679846798567986679876798867989679906799167992679936799467995679966799767998679996800068001680026800368004680056800668007680086800968010680116801268013680146801568016680176801868019680206802168022680236802468025680266802768028680296803068031680326803368034680356803668037680386803968040680416804268043680446804568046680476804868049680506805168052680536805468055680566805768058680596806068061680626806368064680656806668067680686806968070680716807268073680746807568076680776807868079680806808168082680836808468085680866808768088680896809068091680926809368094680956809668097680986809968100681016810268103681046810568106681076810868109681106811168112681136811468115681166811768118681196812068121681226812368124681256812668127681286812968130681316813268133681346813568136681376813868139681406814168142681436814468145681466814768148681496815068151681526815368154681556815668157681586815968160681616816268163681646816568166681676816868169681706817168172681736817468175681766817768178681796818068181681826818368184681856818668187681886818968190681916819268193681946819568196681976819868199682006820168202682036820468205682066820768208682096821068211682126821368214682156821668217682186821968220682216822268223682246822568226682276822868229682306823168232682336823468235682366823768238682396824068241682426824368244682456824668247682486824968250682516825268253682546825568256682576825868259682606826168262682636826468265682666826768268682696827068271682726827368274682756827668277682786827968280682816828268283682846828568286682876828868289682906829168292682936829468295682966829768298682996830068301683026830368304683056830668307683086830968310683116831268313683146831568316683176831868319683206832168322683236832468325683266832768328683296833068331683326833368334683356833668337683386833968340683416834268343683446834568346683476834868349683506835168352683536835468355683566835768358683596836068361683626836368364683656836668367683686836968370683716837268373683746837568376683776837868379683806838168382683836838468385683866838768388683896839068391683926839368394683956839668397683986839968400684016840268403684046840568406684076840868409684106841168412684136841468415684166841768418684196842068421684226842368424684256842668427684286842968430684316843268433684346843568436684376843868439684406844168442684436844468445684466844768448684496845068451684526845368454684556845668457684586845968460684616846268463684646846568466684676846868469684706847168472684736847468475684766847768478684796848068481684826848368484684856848668487684886848968490684916849268493684946849568496684976849868499685006850168502685036850468505685066850768508685096851068511685126851368514685156851668517685186851968520685216852268523685246852568526685276852868529685306853168532685336853468535685366853768538685396854068541685426854368544685456854668547685486854968550685516855268553685546855568556685576855868559685606856168562685636856468565685666856768568685696857068571685726857368574685756857668577685786857968580685816858268583685846858568586685876858868589685906859168592685936859468595685966859768598685996860068601686026860368604686056860668607686086860968610686116861268613686146861568616686176861868619686206862168622686236862468625686266862768628686296863068631686326863368634686356863668637686386863968640686416864268643686446864568646686476864868649686506865168652686536865468655686566865768658686596866068661686626866368664686656866668667686686866968670686716867268673686746867568676686776867868679686806868168682686836868468685686866868768688686896869068691686926869368694686956869668697686986869968700687016870268703687046870568706687076870868709687106871168712687136871468715687166871768718687196872068721687226872368724687256872668727687286872968730687316873268733687346873568736687376873868739687406874168742687436874468745687466874768748687496875068751687526875368754687556875668757687586875968760687616876268763687646876568766687676876868769687706877168772687736877468775687766877768778687796878068781687826878368784687856878668787687886878968790687916879268793687946879568796687976879868799688006880168802688036880468805688066880768808688096881068811688126881368814688156881668817688186881968820688216882268823688246882568826688276882868829688306883168832688336883468835688366883768838688396884068841688426884368844688456884668847688486884968850688516885268853688546885568856688576885868859688606886168862688636886468865688666886768868688696887068871688726887368874688756887668877688786887968880688816888268883688846888568886688876888868889688906889168892688936889468895688966889768898688996890068901689026890368904689056890668907689086890968910689116891268913689146891568916689176891868919689206892168922689236892468925689266892768928689296893068931689326893368934689356893668937689386893968940689416894268943689446894568946689476894868949689506895168952689536895468955689566895768958689596896068961689626896368964689656896668967689686896968970689716897268973689746897568976689776897868979689806898168982689836898468985689866898768988689896899068991689926899368994689956899668997689986899969000690016900269003690046900569006690076900869009690106901169012690136901469015690166901769018690196902069021690226902369024690256902669027690286902969030690316903269033690346903569036690376903869039690406904169042690436904469045690466904769048690496905069051690526905369054690556905669057690586905969060690616906269063690646906569066690676906869069690706907169072690736907469075690766907769078690796908069081690826908369084690856908669087690886908969090690916909269093690946909569096690976909869099691006910169102691036910469105691066910769108691096911069111691126911369114691156911669117691186911969120691216912269123691246912569126691276912869129691306913169132691336913469135691366913769138691396914069141691426914369144691456914669147691486914969150691516915269153691546915569156691576915869159691606916169162691636916469165691666916769168691696917069171691726917369174691756917669177691786917969180691816918269183691846918569186691876918869189691906919169192691936919469195691966919769198691996920069201692026920369204692056920669207692086920969210692116921269213692146921569216692176921869219692206922169222692236922469225692266922769228692296923069231692326923369234692356923669237692386923969240692416924269243692446924569246692476924869249692506925169252692536925469255692566925769258692596926069261692626926369264692656926669267692686926969270692716927269273692746927569276692776927869279692806928169282692836928469285692866928769288692896929069291692926929369294692956929669297692986929969300693016930269303693046930569306693076930869309693106931169312693136931469315693166931769318693196932069321693226932369324693256932669327693286932969330693316933269333693346933569336693376933869339693406934169342693436934469345693466934769348693496935069351693526935369354693556935669357693586935969360693616936269363693646936569366693676936869369693706937169372693736937469375693766937769378693796938069381693826938369384693856938669387693886938969390693916939269393693946939569396693976939869399694006940169402694036940469405694066940769408694096941069411694126941369414694156941669417694186941969420694216942269423694246942569426694276942869429694306943169432694336943469435694366943769438694396944069441694426944369444694456944669447694486944969450694516945269453694546945569456694576945869459694606946169462694636946469465694666946769468694696947069471694726947369474694756947669477694786947969480694816948269483694846948569486694876948869489694906949169492694936949469495694966949769498694996950069501695026950369504695056950669507695086950969510695116951269513695146951569516695176951869519695206952169522695236952469525695266952769528695296953069531695326953369534695356953669537695386953969540695416954269543695446954569546695476954869549695506955169552695536955469555695566955769558695596956069561695626956369564695656956669567695686956969570695716957269573695746957569576695776957869579695806958169582695836958469585695866958769588695896959069591695926959369594695956959669597695986959969600696016960269603696046960569606696076960869609696106961169612696136961469615696166961769618696196962069621696226962369624696256962669627696286962969630696316963269633696346963569636696376963869639696406964169642696436964469645696466964769648696496965069651696526965369654696556965669657696586965969660696616966269663696646966569666696676966869669696706967169672696736967469675696766967769678696796968069681696826968369684696856968669687696886968969690696916969269693696946969569696696976969869699697006970169702697036970469705697066970769708697096971069711697126971369714697156971669717697186971969720697216972269723697246972569726697276972869729697306973169732697336973469735697366973769738697396974069741697426974369744697456974669747697486974969750697516975269753697546975569756697576975869759697606976169762697636976469765697666976769768697696977069771697726977369774697756977669777697786977969780697816978269783697846978569786697876978869789697906979169792697936979469795697966979769798697996980069801698026980369804698056980669807698086980969810698116981269813698146981569816698176981869819698206982169822698236982469825698266982769828698296983069831698326983369834698356983669837698386983969840698416984269843698446984569846698476984869849698506985169852698536985469855698566985769858698596986069861698626986369864698656986669867698686986969870698716987269873698746987569876698776987869879698806988169882698836988469885698866988769888698896989069891698926989369894698956989669897698986989969900699016990269903699046990569906699076990869909699106991169912699136991469915699166991769918699196992069921699226992369924699256992669927699286992969930699316993269933699346993569936699376993869939699406994169942699436994469945699466994769948699496995069951699526995369954699556995669957699586995969960699616996269963699646996569966699676996869969699706997169972699736997469975699766997769978699796998069981699826998369984699856998669987699886998969990699916999269993699946999569996699976999869999700007000170002700037000470005700067000770008700097001070011700127001370014700157001670017700187001970020700217002270023700247002570026700277002870029700307003170032700337003470035700367003770038700397004070041700427004370044700457004670047700487004970050700517005270053700547005570056700577005870059700607006170062700637006470065700667006770068700697007070071700727007370074700757007670077700787007970080700817008270083700847008570086700877008870089700907009170092700937009470095700967009770098700997010070101701027010370104701057010670107701087010970110701117011270113701147011570116701177011870119701207012170122701237012470125701267012770128701297013070131701327013370134701357013670137701387013970140701417014270143701447014570146701477014870149701507015170152701537015470155701567015770158701597016070161701627016370164701657016670167701687016970170701717017270173701747017570176701777017870179701807018170182701837018470185701867018770188701897019070191701927019370194701957019670197701987019970200702017020270203702047020570206702077020870209702107021170212702137021470215702167021770218702197022070221702227022370224702257022670227702287022970230702317023270233702347023570236702377023870239702407024170242702437024470245702467024770248702497025070251702527025370254702557025670257702587025970260702617026270263702647026570266702677026870269702707027170272702737027470275702767027770278702797028070281702827028370284702857028670287702887028970290702917029270293702947029570296702977029870299703007030170302703037030470305703067030770308703097031070311703127031370314703157031670317703187031970320703217032270323703247032570326703277032870329703307033170332703337033470335703367033770338703397034070341703427034370344703457034670347703487034970350703517035270353703547035570356703577035870359703607036170362703637036470365703667036770368703697037070371703727037370374703757037670377703787037970380703817038270383703847038570386703877038870389703907039170392703937039470395703967039770398703997040070401704027040370404704057040670407704087040970410704117041270413704147041570416704177041870419704207042170422704237042470425704267042770428704297043070431704327043370434704357043670437704387043970440704417044270443704447044570446704477044870449704507045170452704537045470455704567045770458704597046070461704627046370464704657046670467704687046970470704717047270473704747047570476704777047870479704807048170482704837048470485704867048770488704897049070491704927049370494704957049670497704987049970500705017050270503705047050570506705077050870509705107051170512705137051470515705167051770518705197052070521705227052370524705257052670527705287052970530705317053270533705347053570536705377053870539705407054170542705437054470545705467054770548705497055070551705527055370554705557055670557705587055970560705617056270563705647056570566705677056870569705707057170572705737057470575705767057770578705797058070581705827058370584705857058670587705887058970590705917059270593705947059570596705977059870599706007060170602706037060470605706067060770608706097061070611706127061370614706157061670617706187061970620706217062270623706247062570626706277062870629706307063170632706337063470635706367063770638706397064070641706427064370644706457064670647706487064970650706517065270653706547065570656706577065870659706607066170662706637066470665706667066770668706697067070671706727067370674706757067670677706787067970680706817068270683706847068570686706877068870689706907069170692706937069470695706967069770698706997070070701707027070370704707057070670707707087070970710707117071270713707147071570716707177071870719707207072170722707237072470725707267072770728707297073070731707327073370734707357073670737707387073970740707417074270743707447074570746707477074870749707507075170752707537075470755707567075770758707597076070761707627076370764707657076670767707687076970770707717077270773707747077570776707777077870779707807078170782707837078470785707867078770788707897079070791707927079370794707957079670797707987079970800708017080270803708047080570806708077080870809708107081170812708137081470815708167081770818708197082070821708227082370824708257082670827708287082970830708317083270833708347083570836708377083870839708407084170842708437084470845708467084770848708497085070851708527085370854708557085670857708587085970860708617086270863708647086570866708677086870869708707087170872708737087470875708767087770878708797088070881708827088370884708857088670887708887088970890708917089270893708947089570896708977089870899709007090170902709037090470905709067090770908709097091070911709127091370914709157091670917709187091970920709217092270923709247092570926709277092870929709307093170932709337093470935709367093770938709397094070941709427094370944709457094670947709487094970950709517095270953709547095570956709577095870959709607096170962709637096470965709667096770968709697097070971709727097370974709757097670977709787097970980709817098270983709847098570986709877098870989709907099170992709937099470995709967099770998709997100071001710027100371004710057100671007710087100971010710117101271013710147101571016710177101871019710207102171022710237102471025710267102771028710297103071031710327103371034710357103671037710387103971040710417104271043710447104571046710477104871049710507105171052710537105471055710567105771058710597106071061710627106371064710657106671067710687106971070710717107271073710747107571076710777107871079710807108171082710837108471085710867108771088710897109071091710927109371094710957109671097710987109971100711017110271103711047110571106711077110871109711107111171112711137111471115711167111771118711197112071121711227112371124711257112671127711287112971130711317113271133711347113571136711377113871139711407114171142711437114471145711467114771148711497115071151711527115371154711557115671157711587115971160711617116271163711647116571166711677116871169711707117171172711737117471175711767117771178711797118071181711827118371184711857118671187711887118971190711917119271193711947119571196711977119871199712007120171202712037120471205712067120771208712097121071211712127121371214712157121671217712187121971220712217122271223712247122571226712277122871229712307123171232712337123471235712367123771238712397124071241712427124371244712457124671247712487124971250712517125271253712547125571256712577125871259712607126171262712637126471265712667126771268712697127071271712727127371274712757127671277712787127971280712817128271283712847128571286712877128871289712907129171292712937129471295712967129771298712997130071301713027130371304713057130671307713087130971310713117131271313713147131571316713177131871319713207132171322713237132471325713267132771328713297133071331713327133371334713357133671337713387133971340713417134271343713447134571346713477134871349713507135171352713537135471355713567135771358713597136071361713627136371364713657136671367713687136971370713717137271373713747137571376713777137871379713807138171382713837138471385713867138771388713897139071391713927139371394713957139671397713987139971400714017140271403714047140571406714077140871409714107141171412714137141471415714167141771418714197142071421714227142371424714257142671427714287142971430714317143271433714347143571436714377143871439714407144171442714437144471445714467144771448714497145071451714527145371454714557145671457714587145971460714617146271463714647146571466714677146871469714707147171472714737147471475714767147771478714797148071481714827148371484714857148671487714887148971490714917149271493714947149571496714977149871499715007150171502715037150471505715067150771508715097151071511715127151371514715157151671517715187151971520715217152271523715247152571526715277152871529715307153171532715337153471535715367153771538715397154071541715427154371544715457154671547715487154971550715517155271553715547155571556715577155871559715607156171562715637156471565715667156771568715697157071571715727157371574715757157671577715787157971580715817158271583715847158571586715877158871589715907159171592715937159471595715967159771598715997160071601716027160371604716057160671607716087160971610716117161271613716147161571616716177161871619716207162171622716237162471625716267162771628716297163071631716327163371634716357163671637716387163971640716417164271643716447164571646716477164871649716507165171652716537165471655716567165771658716597166071661716627166371664716657166671667716687166971670716717167271673716747167571676716777167871679716807168171682716837168471685716867168771688716897169071691716927169371694716957169671697716987169971700717017170271703717047170571706717077170871709717107171171712717137171471715717167171771718717197172071721717227172371724717257172671727717287172971730717317173271733717347173571736717377173871739717407174171742717437174471745717467174771748717497175071751717527175371754717557175671757717587175971760717617176271763717647176571766717677176871769717707177171772717737177471775717767177771778717797178071781717827178371784717857178671787717887178971790717917179271793717947179571796717977179871799718007180171802718037180471805718067180771808718097181071811718127181371814718157181671817718187181971820718217182271823718247182571826718277182871829718307183171832718337183471835718367183771838718397184071841718427184371844718457184671847718487184971850718517185271853718547185571856718577185871859718607186171862718637186471865718667186771868718697187071871718727187371874718757187671877718787187971880718817188271883718847188571886718877188871889718907189171892718937189471895718967189771898718997190071901719027190371904719057190671907719087190971910719117191271913719147191571916719177191871919719207192171922719237192471925719267192771928719297193071931719327193371934719357193671937719387193971940719417194271943719447194571946719477194871949719507195171952719537195471955719567195771958719597196071961719627196371964719657196671967719687196971970719717197271973719747197571976719777197871979719807198171982719837198471985719867198771988719897199071991719927199371994719957199671997719987199972000720017200272003720047200572006720077200872009720107201172012720137201472015720167201772018720197202072021720227202372024720257202672027720287202972030720317203272033720347203572036720377203872039720407204172042720437204472045720467204772048720497205072051720527205372054720557205672057720587205972060720617206272063720647206572066720677206872069720707207172072720737207472075720767207772078720797208072081720827208372084720857208672087720887208972090720917209272093720947209572096720977209872099721007210172102721037210472105721067210772108721097211072111721127211372114721157211672117721187211972120721217212272123721247212572126721277212872129721307213172132721337213472135721367213772138721397214072141721427214372144721457214672147721487214972150721517215272153721547215572156721577215872159721607216172162721637216472165721667216772168721697217072171721727217372174721757217672177721787217972180721817218272183721847218572186721877218872189721907219172192721937219472195721967219772198721997220072201722027220372204722057220672207722087220972210722117221272213722147221572216722177221872219722207222172222722237222472225722267222772228722297223072231722327223372234722357223672237722387223972240722417224272243722447224572246722477224872249722507225172252722537225472255722567225772258722597226072261722627226372264722657226672267722687226972270722717227272273722747227572276722777227872279722807228172282722837228472285722867228772288722897229072291722927229372294722957229672297722987229972300723017230272303723047230572306723077230872309723107231172312723137231472315723167231772318723197232072321723227232372324723257232672327723287232972330723317233272333723347233572336723377233872339723407234172342723437234472345723467234772348723497235072351723527235372354723557235672357723587235972360723617236272363723647236572366723677236872369723707237172372723737237472375723767237772378723797238072381723827238372384723857238672387723887238972390723917239272393723947239572396723977239872399724007240172402724037240472405724067240772408724097241072411724127241372414724157241672417724187241972420724217242272423724247242572426724277242872429724307243172432724337243472435724367243772438724397244072441724427244372444724457244672447724487244972450724517245272453724547245572456724577245872459724607246172462724637246472465724667246772468724697247072471724727247372474724757247672477724787247972480724817248272483724847248572486724877248872489724907249172492724937249472495724967249772498724997250072501725027250372504725057250672507725087250972510725117251272513725147251572516725177251872519725207252172522725237252472525725267252772528725297253072531725327253372534725357253672537725387253972540725417254272543725447254572546725477254872549725507255172552725537255472555725567255772558725597256072561725627256372564725657256672567725687256972570725717257272573725747257572576725777257872579725807258172582725837258472585725867258772588725897259072591725927259372594725957259672597725987259972600726017260272603726047260572606726077260872609726107261172612726137261472615726167261772618726197262072621726227262372624726257262672627726287262972630726317263272633726347263572636726377263872639726407264172642726437264472645726467264772648726497265072651726527265372654726557265672657726587265972660726617266272663726647266572666726677266872669726707267172672726737267472675726767267772678726797268072681726827268372684726857268672687726887268972690726917269272693726947269572696726977269872699727007270172702727037270472705727067270772708727097271072711727127271372714727157271672717727187271972720727217272272723727247272572726727277272872729727307273172732727337273472735727367273772738727397274072741727427274372744727457274672747727487274972750727517275272753727547275572756727577275872759727607276172762727637276472765727667276772768727697277072771727727277372774727757277672777727787277972780727817278272783727847278572786727877278872789727907279172792727937279472795727967279772798727997280072801728027280372804728057280672807728087280972810728117281272813728147281572816728177281872819728207282172822728237282472825728267282772828728297283072831728327283372834728357283672837728387283972840728417284272843728447284572846728477284872849728507285172852728537285472855728567285772858728597286072861728627286372864728657286672867728687286972870728717287272873728747287572876728777287872879728807288172882728837288472885728867288772888728897289072891728927289372894728957289672897728987289972900729017290272903729047290572906729077290872909729107291172912729137291472915729167291772918729197292072921729227292372924729257292672927729287292972930729317293272933729347293572936729377293872939729407294172942729437294472945729467294772948729497295072951729527295372954729557295672957729587295972960729617296272963729647296572966729677296872969729707297172972729737297472975729767297772978729797298072981729827298372984729857298672987729887298972990729917299272993729947299572996729977299872999730007300173002730037300473005730067300773008730097301073011730127301373014730157301673017730187301973020730217302273023730247302573026730277302873029730307303173032730337303473035730367303773038730397304073041730427304373044730457304673047730487304973050730517305273053730547305573056730577305873059730607306173062730637306473065730667306773068730697307073071730727307373074730757307673077730787307973080730817308273083730847308573086730877308873089730907309173092730937309473095730967309773098730997310073101731027310373104731057310673107731087310973110731117311273113731147311573116731177311873119731207312173122731237312473125731267312773128731297313073131731327313373134731357313673137731387313973140731417314273143731447314573146731477314873149731507315173152731537315473155731567315773158731597316073161731627316373164731657316673167731687316973170731717317273173731747317573176731777317873179731807318173182731837318473185731867318773188731897319073191731927319373194731957319673197731987319973200732017320273203732047320573206732077320873209732107321173212732137321473215732167321773218732197322073221732227322373224732257322673227732287322973230732317323273233732347323573236732377323873239732407324173242732437324473245732467324773248732497325073251732527325373254732557325673257732587325973260732617326273263732647326573266732677326873269732707327173272732737327473275732767327773278732797328073281732827328373284732857328673287732887328973290732917329273293732947329573296732977329873299733007330173302733037330473305733067330773308733097331073311733127331373314733157331673317733187331973320733217332273323733247332573326733277332873329733307333173332733337333473335733367333773338733397334073341733427334373344733457334673347733487334973350733517335273353733547335573356733577335873359733607336173362733637336473365733667336773368733697337073371733727337373374733757337673377733787337973380733817338273383733847338573386733877338873389733907339173392733937339473395733967339773398733997340073401734027340373404734057340673407734087340973410734117341273413734147341573416734177341873419734207342173422734237342473425734267342773428734297343073431734327343373434734357343673437734387343973440734417344273443734447344573446734477344873449734507345173452734537345473455734567345773458734597346073461734627346373464734657346673467734687346973470734717347273473734747347573476734777347873479734807348173482734837348473485734867348773488734897349073491734927349373494734957349673497734987349973500735017350273503735047350573506735077350873509735107351173512735137351473515735167351773518735197352073521735227352373524735257352673527735287352973530735317353273533735347353573536735377353873539735407354173542735437354473545735467354773548735497355073551735527355373554735557355673557735587355973560735617356273563735647356573566735677356873569735707357173572735737357473575735767357773578735797358073581735827358373584735857358673587735887358973590735917359273593735947359573596735977359873599736007360173602736037360473605736067360773608736097361073611736127361373614736157361673617736187361973620736217362273623736247362573626736277362873629736307363173632736337363473635736367363773638736397364073641736427364373644736457364673647736487364973650736517365273653736547365573656736577365873659736607366173662736637366473665736667366773668736697367073671736727367373674736757367673677736787367973680736817368273683736847368573686736877368873689736907369173692736937369473695736967369773698736997370073701737027370373704737057370673707737087370973710737117371273713737147371573716737177371873719737207372173722737237372473725737267372773728737297373073731737327373373734737357373673737737387373973740737417374273743737447374573746737477374873749737507375173752737537375473755737567375773758737597376073761737627376373764737657376673767737687376973770737717377273773737747377573776737777377873779737807378173782737837378473785737867378773788737897379073791737927379373794737957379673797737987379973800738017380273803738047380573806738077380873809738107381173812738137381473815738167381773818738197382073821738227382373824738257382673827738287382973830738317383273833738347383573836738377383873839738407384173842738437384473845738467384773848738497385073851738527385373854738557385673857738587385973860738617386273863738647386573866738677386873869738707387173872738737387473875738767387773878738797388073881738827388373884738857388673887738887388973890738917389273893738947389573896738977389873899739007390173902739037390473905739067390773908739097391073911739127391373914739157391673917739187391973920739217392273923739247392573926739277392873929739307393173932739337393473935739367393773938739397394073941739427394373944739457394673947739487394973950739517395273953739547395573956739577395873959739607396173962739637396473965739667396773968739697397073971739727397373974739757397673977739787397973980739817398273983739847398573986739877398873989739907399173992739937399473995739967399773998739997400074001740027400374004740057400674007740087400974010740117401274013740147401574016740177401874019740207402174022740237402474025740267402774028740297403074031740327403374034740357403674037740387403974040740417404274043740447404574046740477404874049740507405174052740537405474055740567405774058740597406074061740627406374064740657406674067740687406974070740717407274073740747407574076740777407874079740807408174082740837408474085740867408774088740897409074091740927409374094740957409674097740987409974100741017410274103741047410574106741077410874109741107411174112741137411474115741167411774118741197412074121741227412374124741257412674127741287412974130741317413274133741347413574136741377413874139741407414174142741437414474145741467414774148741497415074151741527415374154741557415674157741587415974160741617416274163741647416574166741677416874169741707417174172741737417474175741767417774178741797418074181741827418374184741857418674187741887418974190741917419274193741947419574196741977419874199742007420174202742037420474205742067420774208742097421074211742127421374214742157421674217742187421974220742217422274223742247422574226742277422874229742307423174232742337423474235742367423774238742397424074241742427424374244742457424674247742487424974250742517425274253742547425574256742577425874259742607426174262742637426474265742667426774268742697427074271742727427374274742757427674277742787427974280742817428274283742847428574286742877428874289742907429174292742937429474295742967429774298742997430074301743027430374304743057430674307743087430974310743117431274313743147431574316743177431874319743207432174322743237432474325743267432774328743297433074331743327433374334743357433674337743387433974340743417434274343743447434574346743477434874349743507435174352743537435474355743567435774358743597436074361743627436374364743657436674367743687436974370743717437274373743747437574376743777437874379743807438174382743837438474385743867438774388743897439074391743927439374394743957439674397743987439974400744017440274403744047440574406744077440874409744107441174412744137441474415744167441774418744197442074421744227442374424744257442674427744287442974430744317443274433744347443574436744377443874439744407444174442744437444474445744467444774448744497445074451744527445374454744557445674457744587445974460744617446274463744647446574466744677446874469744707447174472744737447474475744767447774478744797448074481744827448374484744857448674487744887448974490744917449274493744947449574496744977449874499745007450174502745037450474505745067450774508745097451074511745127451374514745157451674517745187451974520745217452274523745247452574526745277452874529745307453174532745337453474535745367453774538745397454074541745427454374544745457454674547745487454974550745517455274553745547455574556745577455874559745607456174562745637456474565745667456774568745697457074571745727457374574745757457674577745787457974580745817458274583745847458574586745877458874589745907459174592745937459474595745967459774598745997460074601746027460374604746057460674607746087460974610746117461274613746147461574616746177461874619746207462174622746237462474625746267462774628746297463074631746327463374634746357463674637746387463974640746417464274643746447464574646746477464874649746507465174652746537465474655746567465774658746597466074661746627466374664746657466674667746687466974670746717467274673746747467574676746777467874679746807468174682746837468474685746867468774688746897469074691746927469374694746957469674697746987469974700747017470274703747047470574706747077470874709747107471174712747137471474715747167471774718747197472074721747227472374724747257472674727747287472974730747317473274733747347473574736747377473874739747407474174742747437474474745747467474774748747497475074751747527475374754747557475674757747587475974760747617476274763747647476574766747677476874769747707477174772747737477474775747767477774778747797478074781747827478374784747857478674787747887478974790747917479274793747947479574796747977479874799748007480174802748037480474805748067480774808748097481074811748127481374814748157481674817748187481974820748217482274823748247482574826748277482874829748307483174832748337483474835748367483774838748397484074841748427484374844748457484674847748487484974850748517485274853748547485574856748577485874859748607486174862748637486474865748667486774868748697487074871748727487374874748757487674877748787487974880748817488274883748847488574886748877488874889748907489174892748937489474895748967489774898748997490074901749027490374904749057490674907749087490974910749117491274913749147491574916749177491874919749207492174922749237492474925749267492774928749297493074931749327493374934749357493674937749387493974940749417494274943749447494574946749477494874949749507495174952749537495474955749567495774958749597496074961749627496374964749657496674967749687496974970749717497274973749747497574976749777497874979749807498174982749837498474985749867498774988749897499074991749927499374994749957499674997749987499975000750017500275003750047500575006750077500875009750107501175012750137501475015750167501775018750197502075021750227502375024750257502675027750287502975030750317503275033750347503575036750377503875039750407504175042750437504475045750467504775048750497505075051750527505375054750557505675057750587505975060750617506275063750647506575066750677506875069750707507175072750737507475075750767507775078750797508075081750827508375084750857508675087750887508975090750917509275093750947509575096750977509875099751007510175102751037510475105751067510775108751097511075111751127511375114751157511675117751187511975120751217512275123751247512575126751277512875129751307513175132751337513475135751367513775138751397514075141751427514375144751457514675147751487514975150751517515275153751547515575156751577515875159751607516175162751637516475165751667516775168751697517075171751727517375174751757517675177751787517975180751817518275183751847518575186751877518875189751907519175192751937519475195751967519775198751997520075201752027520375204752057520675207752087520975210752117521275213752147521575216752177521875219752207522175222752237522475225752267522775228752297523075231752327523375234752357523675237752387523975240752417524275243752447524575246752477524875249752507525175252752537525475255752567525775258752597526075261752627526375264752657526675267752687526975270752717527275273752747527575276752777527875279752807528175282752837528475285752867528775288752897529075291752927529375294752957529675297752987529975300753017530275303753047530575306753077530875309753107531175312753137531475315753167531775318753197532075321753227532375324753257532675327753287532975330753317533275333753347533575336753377533875339753407534175342753437534475345753467534775348753497535075351753527535375354753557535675357753587535975360753617536275363753647536575366753677536875369753707537175372753737537475375753767537775378753797538075381753827538375384753857538675387753887538975390753917539275393753947539575396753977539875399754007540175402754037540475405754067540775408754097541075411754127541375414754157541675417754187541975420754217542275423754247542575426754277542875429754307543175432754337543475435754367543775438754397544075441754427544375444754457544675447754487544975450754517545275453754547545575456754577545875459754607546175462754637546475465754667546775468754697547075471754727547375474754757547675477754787547975480754817548275483754847548575486754877548875489754907549175492754937549475495754967549775498754997550075501755027550375504755057550675507755087550975510755117551275513755147551575516755177551875519755207552175522755237552475525755267552775528755297553075531755327553375534755357553675537755387553975540755417554275543755447554575546755477554875549755507555175552755537555475555755567555775558755597556075561755627556375564755657556675567755687556975570755717557275573755747557575576755777557875579755807558175582755837558475585755867558775588755897559075591755927559375594755957559675597755987559975600756017560275603756047560575606756077560875609756107561175612756137561475615756167561775618756197562075621756227562375624756257562675627756287562975630756317563275633756347563575636756377563875639756407564175642756437564475645756467564775648756497565075651756527565375654756557565675657756587565975660756617566275663756647566575666756677566875669756707567175672756737567475675756767567775678756797568075681756827568375684756857568675687756887568975690756917569275693756947569575696756977569875699757007570175702757037570475705757067570775708757097571075711757127571375714757157571675717757187571975720757217572275723757247572575726757277572875729757307573175732757337573475735757367573775738757397574075741757427574375744757457574675747757487574975750757517575275753757547575575756757577575875759757607576175762757637576475765757667576775768757697577075771757727577375774757757577675777757787577975780757817578275783757847578575786757877578875789757907579175792757937579475795757967579775798757997580075801758027580375804758057580675807758087580975810758117581275813758147581575816758177581875819758207582175822758237582475825758267582775828758297583075831758327583375834758357583675837758387583975840758417584275843758447584575846758477584875849758507585175852758537585475855758567585775858758597586075861758627586375864758657586675867758687586975870758717587275873758747587575876758777587875879758807588175882758837588475885758867588775888758897589075891758927589375894758957589675897758987589975900759017590275903759047590575906759077590875909759107591175912759137591475915759167591775918759197592075921759227592375924759257592675927759287592975930759317593275933759347593575936759377593875939759407594175942759437594475945759467594775948759497595075951759527595375954759557595675957759587595975960759617596275963759647596575966759677596875969759707597175972759737597475975759767597775978759797598075981759827598375984759857598675987759887598975990759917599275993759947599575996759977599875999760007600176002760037600476005760067600776008760097601076011760127601376014760157601676017760187601976020760217602276023760247602576026760277602876029760307603176032760337603476035760367603776038760397604076041760427604376044760457604676047760487604976050760517605276053760547605576056760577605876059760607606176062760637606476065760667606776068760697607076071760727607376074760757607676077760787607976080760817608276083760847608576086760877608876089760907609176092760937609476095760967609776098760997610076101761027610376104761057610676107761087610976110761117611276113761147611576116761177611876119761207612176122761237612476125761267612776128761297613076131761327613376134761357613676137761387613976140761417614276143761447614576146761477614876149761507615176152761537615476155761567615776158761597616076161761627616376164761657616676167761687616976170761717617276173761747617576176761777617876179761807618176182761837618476185761867618776188761897619076191761927619376194761957619676197761987619976200762017620276203762047620576206762077620876209762107621176212762137621476215762167621776218762197622076221762227622376224762257622676227762287622976230762317623276233762347623576236762377623876239762407624176242762437624476245762467624776248762497625076251762527625376254762557625676257762587625976260762617626276263762647626576266762677626876269762707627176272762737627476275762767627776278762797628076281762827628376284762857628676287762887628976290762917629276293762947629576296762977629876299763007630176302763037630476305763067630776308763097631076311763127631376314763157631676317763187631976320763217632276323763247632576326763277632876329763307633176332763337633476335763367633776338763397634076341763427634376344763457634676347763487634976350763517635276353763547635576356763577635876359763607636176362763637636476365763667636776368763697637076371763727637376374763757637676377763787637976380763817638276383763847638576386763877638876389763907639176392763937639476395763967639776398763997640076401764027640376404764057640676407764087640976410764117641276413764147641576416764177641876419764207642176422764237642476425764267642776428764297643076431764327643376434764357643676437764387643976440764417644276443764447644576446764477644876449764507645176452764537645476455764567645776458764597646076461764627646376464764657646676467764687646976470764717647276473764747647576476764777647876479764807648176482764837648476485764867648776488764897649076491764927649376494764957649676497764987649976500765017650276503765047650576506765077650876509765107651176512765137651476515765167651776518765197652076521765227652376524765257652676527765287652976530765317653276533765347653576536765377653876539765407654176542765437654476545765467654776548765497655076551765527655376554765557655676557765587655976560765617656276563765647656576566765677656876569765707657176572765737657476575765767657776578765797658076581765827658376584765857658676587765887658976590765917659276593765947659576596765977659876599766007660176602766037660476605766067660776608766097661076611766127661376614766157661676617766187661976620766217662276623766247662576626766277662876629766307663176632766337663476635766367663776638766397664076641766427664376644766457664676647766487664976650766517665276653766547665576656766577665876659766607666176662766637666476665766667666776668766697667076671766727667376674766757667676677766787667976680766817668276683766847668576686766877668876689766907669176692766937669476695766967669776698766997670076701767027670376704767057670676707767087670976710767117671276713767147671576716767177671876719767207672176722767237672476725767267672776728767297673076731767327673376734767357673676737767387673976740767417674276743767447674576746767477674876749767507675176752767537675476755767567675776758767597676076761767627676376764767657676676767767687676976770767717677276773767747677576776767777677876779767807678176782767837678476785767867678776788767897679076791767927679376794767957679676797767987679976800768017680276803768047680576806768077680876809768107681176812768137681476815768167681776818768197682076821768227682376824768257682676827768287682976830768317683276833768347683576836768377683876839768407684176842768437684476845768467684776848768497685076851768527685376854768557685676857768587685976860768617686276863768647686576866768677686876869768707687176872768737687476875768767687776878768797688076881768827688376884768857688676887768887688976890768917689276893768947689576896768977689876899769007690176902769037690476905769067690776908769097691076911769127691376914769157691676917769187691976920769217692276923769247692576926769277692876929769307693176932769337693476935769367693776938769397694076941769427694376944769457694676947769487694976950769517695276953769547695576956769577695876959769607696176962769637696476965769667696776968769697697076971769727697376974769757697676977769787697976980769817698276983769847698576986769877698876989769907699176992769937699476995769967699776998769997700077001770027700377004770057700677007770087700977010770117701277013770147701577016770177701877019770207702177022770237702477025770267702777028770297703077031770327703377034770357703677037770387703977040770417704277043770447704577046770477704877049770507705177052770537705477055770567705777058770597706077061770627706377064770657706677067770687706977070770717707277073770747707577076770777707877079770807708177082770837708477085770867708777088770897709077091770927709377094770957709677097770987709977100771017710277103771047710577106771077710877109771107711177112771137711477115771167711777118771197712077121771227712377124771257712677127771287712977130771317713277133771347713577136771377713877139771407714177142771437714477145771467714777148771497715077151771527715377154771557715677157771587715977160771617716277163771647716577166771677716877169771707717177172771737717477175771767717777178771797718077181771827718377184771857718677187771887718977190771917719277193771947719577196771977719877199772007720177202772037720477205772067720777208772097721077211772127721377214772157721677217772187721977220772217722277223772247722577226772277722877229772307723177232772337723477235772367723777238772397724077241772427724377244772457724677247772487724977250772517725277253772547725577256772577725877259772607726177262772637726477265772667726777268772697727077271772727727377274772757727677277772787727977280772817728277283772847728577286772877728877289772907729177292772937729477295772967729777298772997730077301773027730377304773057730677307773087730977310773117731277313773147731577316773177731877319773207732177322773237732477325773267732777328773297733077331773327733377334773357733677337773387733977340773417734277343773447734577346773477734877349773507735177352773537735477355773567735777358773597736077361773627736377364773657736677367773687736977370773717737277373773747737577376773777737877379773807738177382773837738477385773867738777388773897739077391773927739377394773957739677397773987739977400774017740277403774047740577406774077740877409774107741177412774137741477415774167741777418774197742077421774227742377424774257742677427774287742977430774317743277433774347743577436774377743877439774407744177442774437744477445774467744777448774497745077451774527745377454774557745677457774587745977460774617746277463774647746577466774677746877469774707747177472774737747477475774767747777478774797748077481774827748377484774857748677487774887748977490774917749277493774947749577496774977749877499775007750177502775037750477505775067750777508775097751077511775127751377514775157751677517775187751977520775217752277523775247752577526775277752877529775307753177532775337753477535775367753777538775397754077541775427754377544775457754677547775487754977550775517755277553775547755577556775577755877559775607756177562775637756477565775667756777568775697757077571775727757377574775757757677577775787757977580775817758277583775847758577586775877758877589775907759177592775937759477595775967759777598775997760077601776027760377604776057760677607776087760977610776117761277613776147761577616776177761877619776207762177622776237762477625776267762777628776297763077631776327763377634776357763677637776387763977640776417764277643776447764577646776477764877649776507765177652776537765477655776567765777658776597766077661776627766377664776657766677667776687766977670776717767277673776747767577676776777767877679776807768177682776837768477685776867768777688776897769077691776927769377694776957769677697776987769977700777017770277703777047770577706777077770877709777107771177712777137771477715777167771777718777197772077721777227772377724777257772677727777287772977730777317773277733777347773577736777377773877739777407774177742777437774477745777467774777748777497775077751777527775377754777557775677757777587775977760777617776277763777647776577766777677776877769777707777177772777737777477775777767777777778777797778077781777827778377784777857778677787777887778977790777917779277793777947779577796777977779877799778007780177802778037780477805778067780777808778097781077811778127781377814778157781677817778187781977820778217782277823778247782577826778277782877829778307783177832778337783477835778367783777838778397784077841778427784377844778457784677847778487784977850778517785277853778547785577856778577785877859778607786177862778637786477865778667786777868778697787077871778727787377874778757787677877778787787977880778817788277883778847788577886778877788877889778907789177892778937789477895778967789777898778997790077901779027790377904779057790677907779087790977910779117791277913779147791577916779177791877919779207792177922779237792477925779267792777928779297793077931779327793377934779357793677937779387793977940779417794277943779447794577946779477794877949779507795177952779537795477955779567795777958779597796077961779627796377964779657796677967779687796977970779717797277973779747797577976779777797877979779807798177982779837798477985779867798777988779897799077991779927799377994779957799677997779987799978000780017800278003780047800578006780077800878009780107801178012780137801478015780167801778018780197802078021780227802378024780257802678027780287802978030780317803278033780347803578036780377803878039780407804178042780437804478045780467804778048780497805078051780527805378054780557805678057780587805978060780617806278063780647806578066780677806878069780707807178072780737807478075780767807778078780797808078081780827808378084780857808678087780887808978090780917809278093780947809578096780977809878099781007810178102781037810478105781067810778108781097811078111781127811378114781157811678117781187811978120781217812278123781247812578126781277812878129781307813178132781337813478135781367813778138781397814078141781427814378144781457814678147781487814978150781517815278153781547815578156781577815878159781607816178162781637816478165781667816778168781697817078171781727817378174781757817678177781787817978180781817818278183781847818578186781877818878189781907819178192781937819478195781967819778198781997820078201782027820378204782057820678207782087820978210782117821278213782147821578216782177821878219782207822178222782237822478225782267822778228782297823078231782327823378234782357823678237782387823978240782417824278243782447824578246782477824878249782507825178252782537825478255782567825778258782597826078261782627826378264782657826678267782687826978270782717827278273782747827578276782777827878279782807828178282782837828478285782867828778288782897829078291782927829378294782957829678297782987829978300783017830278303783047830578306783077830878309783107831178312783137831478315783167831778318783197832078321783227832378324783257832678327783287832978330783317833278333783347833578336783377833878339783407834178342783437834478345783467834778348783497835078351783527835378354783557835678357783587835978360783617836278363783647836578366783677836878369783707837178372783737837478375783767837778378783797838078381783827838378384783857838678387783887838978390783917839278393783947839578396783977839878399784007840178402784037840478405784067840778408784097841078411784127841378414784157841678417784187841978420784217842278423784247842578426784277842878429784307843178432784337843478435784367843778438784397844078441784427844378444784457844678447784487844978450784517845278453784547845578456784577845878459784607846178462784637846478465784667846778468784697847078471784727847378474784757847678477784787847978480784817848278483784847848578486784877848878489784907849178492784937849478495784967849778498784997850078501785027850378504785057850678507785087850978510785117851278513785147851578516785177851878519785207852178522785237852478525785267852778528785297853078531785327853378534785357853678537785387853978540785417854278543785447854578546785477854878549785507855178552785537855478555785567855778558785597856078561785627856378564785657856678567785687856978570785717857278573785747857578576785777857878579785807858178582785837858478585785867858778588785897859078591785927859378594785957859678597785987859978600786017860278603786047860578606786077860878609786107861178612786137861478615786167861778618786197862078621786227862378624786257862678627786287862978630786317863278633786347863578636786377863878639786407864178642786437864478645786467864778648786497865078651786527865378654786557865678657786587865978660786617866278663786647866578666786677866878669786707867178672786737867478675786767867778678786797868078681786827868378684786857868678687786887868978690786917869278693786947869578696786977869878699787007870178702787037870478705787067870778708787097871078711787127871378714787157871678717787187871978720787217872278723787247872578726787277872878729787307873178732787337873478735787367873778738787397874078741787427874378744787457874678747787487874978750787517875278753787547875578756787577875878759787607876178762787637876478765787667876778768787697877078771787727877378774787757877678777787787877978780787817878278783787847878578786787877878878789787907879178792787937879478795787967879778798787997880078801788027880378804788057880678807788087880978810788117881278813788147881578816788177881878819788207882178822788237882478825788267882778828788297883078831788327883378834788357883678837788387883978840788417884278843788447884578846788477884878849788507885178852788537885478855788567885778858788597886078861788627886378864788657886678867788687886978870788717887278873788747887578876788777887878879788807888178882788837888478885788867888778888788897889078891788927889378894788957889678897788987889978900789017890278903789047890578906789077890878909789107891178912789137891478915789167891778918789197892078921789227892378924789257892678927789287892978930789317893278933789347893578936789377893878939789407894178942789437894478945789467894778948789497895078951789527895378954789557895678957789587895978960789617896278963789647896578966789677896878969789707897178972789737897478975789767897778978789797898078981789827898378984789857898678987789887898978990789917899278993789947899578996789977899878999790007900179002790037900479005790067900779008790097901079011790127901379014790157901679017790187901979020790217902279023790247902579026790277902879029790307903179032790337903479035790367903779038790397904079041790427904379044790457904679047790487904979050790517905279053790547905579056790577905879059790607906179062790637906479065790667906779068790697907079071790727907379074790757907679077790787907979080790817908279083790847908579086790877908879089790907909179092790937909479095790967909779098790997910079101791027910379104791057910679107791087910979110791117911279113791147911579116791177911879119791207912179122791237912479125791267912779128791297913079131791327913379134791357913679137791387913979140791417914279143791447914579146791477914879149791507915179152791537915479155791567915779158791597916079161791627916379164791657916679167791687916979170791717917279173791747917579176791777917879179791807918179182791837918479185791867918779188791897919079191791927919379194791957919679197791987919979200792017920279203792047920579206792077920879209792107921179212792137921479215792167921779218792197922079221792227922379224792257922679227792287922979230792317923279233792347923579236792377923879239792407924179242792437924479245792467924779248792497925079251792527925379254792557925679257792587925979260792617926279263792647926579266792677926879269792707927179272792737927479275792767927779278792797928079281792827928379284792857928679287792887928979290792917929279293792947929579296792977929879299793007930179302793037930479305793067930779308793097931079311793127931379314793157931679317793187931979320793217932279323793247932579326793277932879329793307933179332793337933479335793367933779338793397934079341793427934379344793457934679347793487934979350793517935279353793547935579356793577935879359793607936179362793637936479365793667936779368793697937079371793727937379374793757937679377793787937979380793817938279383793847938579386793877938879389793907939179392793937939479395793967939779398793997940079401794027940379404794057940679407794087940979410794117941279413794147941579416794177941879419794207942179422794237942479425794267942779428794297943079431794327943379434794357943679437794387943979440794417944279443794447944579446794477944879449794507945179452794537945479455794567945779458794597946079461794627946379464794657946679467794687946979470794717947279473794747947579476794777947879479794807948179482794837948479485794867948779488794897949079491794927949379494794957949679497794987949979500795017950279503795047950579506795077950879509795107951179512795137951479515795167951779518795197952079521795227952379524795257952679527795287952979530795317953279533795347953579536795377953879539795407954179542795437954479545795467954779548795497955079551795527955379554795557955679557795587955979560795617956279563795647956579566795677956879569795707957179572795737957479575795767957779578795797958079581795827958379584795857958679587795887958979590795917959279593795947959579596795977959879599796007960179602796037960479605796067960779608796097961079611796127961379614796157961679617796187961979620796217962279623796247962579626796277962879629796307963179632796337963479635796367963779638796397964079641796427964379644796457964679647796487964979650796517965279653796547965579656796577965879659796607966179662796637966479665796667966779668796697967079671796727967379674796757967679677796787967979680796817968279683796847968579686796877968879689796907969179692796937969479695796967969779698796997970079701797027970379704797057970679707797087970979710797117971279713797147971579716797177971879719797207972179722797237972479725797267972779728797297973079731797327973379734797357973679737797387973979740797417974279743797447974579746797477974879749797507975179752797537975479755797567975779758797597976079761797627976379764797657976679767797687976979770797717977279773797747977579776797777977879779797807978179782797837978479785797867978779788797897979079791797927979379794797957979679797797987979979800798017980279803798047980579806798077980879809798107981179812798137981479815798167981779818798197982079821798227982379824798257982679827798287982979830798317983279833798347983579836798377983879839798407984179842798437984479845798467984779848798497985079851798527985379854798557985679857798587985979860798617986279863798647986579866798677986879869798707987179872798737987479875798767987779878798797988079881798827988379884798857988679887798887988979890798917989279893798947989579896798977989879899799007990179902799037990479905799067990779908799097991079911799127991379914799157991679917799187991979920799217992279923799247992579926799277992879929799307993179932799337993479935799367993779938799397994079941799427994379944799457994679947799487994979950799517995279953799547995579956799577995879959799607996179962799637996479965799667996779968799697997079971799727997379974799757997679977799787997979980799817998279983799847998579986799877998879989799907999179992799937999479995799967999779998799998000080001800028000380004800058000680007800088000980010800118001280013800148001580016800178001880019800208002180022800238002480025800268002780028800298003080031800328003380034800358003680037800388003980040800418004280043800448004580046800478004880049800508005180052800538005480055800568005780058800598006080061800628006380064800658006680067800688006980070800718007280073800748007580076800778007880079800808008180082800838008480085800868008780088800898009080091800928009380094800958009680097800988009980100801018010280103801048010580106801078010880109801108011180112801138011480115801168011780118801198012080121801228012380124801258012680127801288012980130801318013280133801348013580136801378013880139801408014180142801438014480145801468014780148801498015080151801528015380154801558015680157801588015980160801618016280163801648016580166801678016880169801708017180172801738017480175801768017780178801798018080181801828018380184801858018680187801888018980190801918019280193801948019580196801978019880199802008020180202802038020480205802068020780208802098021080211802128021380214802158021680217802188021980220802218022280223802248022580226802278022880229802308023180232802338023480235802368023780238802398024080241802428024380244802458024680247802488024980250802518025280253802548025580256802578025880259802608026180262802638026480265802668026780268802698027080271802728027380274802758027680277802788027980280802818028280283802848028580286802878028880289802908029180292802938029480295802968029780298802998030080301803028030380304803058030680307803088030980310803118031280313803148031580316803178031880319803208032180322803238032480325803268032780328803298033080331803328033380334803358033680337803388033980340803418034280343803448034580346803478034880349803508035180352803538035480355803568035780358803598036080361803628036380364803658036680367803688036980370803718037280373803748037580376803778037880379803808038180382803838038480385803868038780388803898039080391803928039380394803958039680397803988039980400804018040280403804048040580406804078040880409804108041180412804138041480415804168041780418804198042080421804228042380424804258042680427804288042980430804318043280433804348043580436804378043880439804408044180442804438044480445804468044780448804498045080451804528045380454804558045680457804588045980460804618046280463804648046580466804678046880469804708047180472804738047480475804768047780478804798048080481804828048380484804858048680487804888048980490804918049280493804948049580496804978049880499805008050180502805038050480505805068050780508805098051080511805128051380514805158051680517805188051980520805218052280523805248052580526805278052880529805308053180532805338053480535805368053780538805398054080541805428054380544805458054680547805488054980550805518055280553805548055580556805578055880559805608056180562805638056480565805668056780568805698057080571805728057380574805758057680577805788057980580805818058280583805848058580586805878058880589805908059180592805938059480595805968059780598805998060080601806028060380604806058060680607806088060980610806118061280613806148061580616806178061880619806208062180622806238062480625806268062780628806298063080631806328063380634806358063680637806388063980640806418064280643806448064580646806478064880649806508065180652806538065480655806568065780658806598066080661806628066380664806658066680667806688066980670806718067280673806748067580676806778067880679806808068180682806838068480685806868068780688806898069080691806928069380694806958069680697806988069980700807018070280703807048070580706807078070880709807108071180712807138071480715807168071780718807198072080721807228072380724807258072680727807288072980730807318073280733807348073580736807378073880739807408074180742807438074480745807468074780748807498075080751807528075380754807558075680757807588075980760807618076280763807648076580766807678076880769807708077180772807738077480775807768077780778807798078080781807828078380784807858078680787807888078980790807918079280793807948079580796807978079880799808008080180802808038080480805808068080780808808098081080811808128081380814808158081680817808188081980820808218082280823808248082580826808278082880829808308083180832808338083480835808368083780838808398084080841808428084380844808458084680847808488084980850808518085280853808548085580856808578085880859808608086180862808638086480865808668086780868808698087080871808728087380874808758087680877808788087980880808818088280883808848088580886808878088880889808908089180892808938089480895808968089780898808998090080901809028090380904809058090680907809088090980910809118091280913809148091580916809178091880919809208092180922809238092480925809268092780928809298093080931809328093380934809358093680937809388093980940809418094280943809448094580946809478094880949809508095180952809538095480955809568095780958809598096080961809628096380964809658096680967809688096980970809718097280973809748097580976809778097880979809808098180982809838098480985809868098780988809898099080991809928099380994809958099680997809988099981000810018100281003810048100581006810078100881009810108101181012810138101481015810168101781018810198102081021810228102381024810258102681027810288102981030810318103281033810348103581036810378103881039810408104181042810438104481045810468104781048810498105081051810528105381054810558105681057810588105981060810618106281063810648106581066810678106881069810708107181072810738107481075810768107781078810798108081081810828108381084810858108681087810888108981090810918109281093810948109581096810978109881099811008110181102811038110481105811068110781108811098111081111811128111381114811158111681117811188111981120811218112281123811248112581126811278112881129811308113181132811338113481135811368113781138811398114081141811428114381144811458114681147811488114981150811518115281153811548115581156811578115881159811608116181162811638116481165811668116781168811698117081171811728117381174811758117681177811788117981180811818118281183811848118581186811878118881189811908119181192811938119481195811968119781198811998120081201812028120381204812058120681207812088120981210812118121281213812148121581216812178121881219812208122181222812238122481225812268122781228812298123081231812328123381234812358123681237812388123981240812418124281243812448124581246812478124881249812508125181252812538125481255812568125781258812598126081261812628126381264812658126681267812688126981270812718127281273812748127581276812778127881279812808128181282812838128481285812868128781288812898129081291812928129381294812958129681297812988129981300813018130281303813048130581306813078130881309813108131181312813138131481315813168131781318813198132081321813228132381324813258132681327813288132981330813318133281333813348133581336813378133881339813408134181342813438134481345813468134781348813498135081351813528135381354813558135681357813588135981360813618136281363813648136581366813678136881369813708137181372813738137481375813768137781378813798138081381813828138381384813858138681387813888138981390813918139281393813948139581396813978139881399814008140181402814038140481405814068140781408814098141081411814128141381414814158141681417814188141981420814218142281423814248142581426814278142881429814308143181432814338143481435814368143781438814398144081441814428144381444814458144681447814488144981450814518145281453814548145581456814578145881459814608146181462814638146481465814668146781468814698147081471814728147381474814758147681477814788147981480814818148281483814848148581486814878148881489814908149181492814938149481495814968149781498814998150081501815028150381504815058150681507815088150981510815118151281513815148151581516815178151881519815208152181522815238152481525815268152781528815298153081531815328153381534815358153681537815388153981540815418154281543815448154581546815478154881549815508155181552815538155481555815568155781558815598156081561815628156381564815658156681567815688156981570815718157281573815748157581576815778157881579815808158181582815838158481585815868158781588815898159081591815928159381594815958159681597815988159981600816018160281603816048160581606816078160881609816108161181612816138161481615816168161781618816198162081621816228162381624816258162681627816288162981630816318163281633816348163581636816378163881639816408164181642816438164481645816468164781648816498165081651816528165381654816558165681657816588165981660816618166281663816648166581666816678166881669816708167181672816738167481675816768167781678816798168081681816828168381684816858168681687816888168981690816918169281693816948169581696816978169881699817008170181702817038170481705817068170781708817098171081711817128171381714817158171681717817188171981720817218172281723817248172581726817278172881729817308173181732817338173481735817368173781738817398174081741817428174381744817458174681747817488174981750817518175281753817548175581756817578175881759817608176181762817638176481765817668176781768817698177081771817728177381774817758177681777817788177981780817818178281783817848178581786817878178881789817908179181792817938179481795817968179781798817998180081801818028180381804818058180681807818088180981810818118181281813818148181581816818178181881819818208182181822818238182481825818268182781828818298183081831818328183381834818358183681837818388183981840818418184281843818448184581846818478184881849818508185181852818538185481855818568185781858818598186081861818628186381864818658186681867818688186981870818718187281873818748187581876818778187881879818808188181882818838188481885818868188781888818898189081891818928189381894818958189681897818988189981900819018190281903819048190581906819078190881909819108191181912819138191481915819168191781918819198192081921819228192381924819258192681927819288192981930819318193281933819348193581936819378193881939819408194181942819438194481945819468194781948819498195081951819528195381954819558195681957819588195981960819618196281963819648196581966819678196881969819708197181972819738197481975819768197781978819798198081981819828198381984819858198681987819888198981990819918199281993819948199581996819978199881999820008200182002820038200482005820068200782008820098201082011820128201382014820158201682017820188201982020820218202282023820248202582026820278202882029820308203182032820338203482035820368203782038820398204082041820428204382044820458204682047820488204982050820518205282053820548205582056820578205882059820608206182062820638206482065820668206782068820698207082071820728207382074820758207682077820788207982080820818208282083820848208582086820878208882089820908209182092820938209482095820968209782098820998210082101821028210382104821058210682107821088210982110821118211282113821148211582116821178211882119821208212182122821238212482125821268212782128821298213082131821328213382134821358213682137821388213982140821418214282143821448214582146821478214882149821508215182152821538215482155821568215782158821598216082161821628216382164821658216682167821688216982170821718217282173821748217582176821778217882179821808218182182821838218482185821868218782188821898219082191821928219382194821958219682197821988219982200822018220282203822048220582206822078220882209822108221182212822138221482215822168221782218822198222082221822228222382224822258222682227822288222982230822318223282233822348223582236822378223882239822408224182242822438224482245822468224782248822498225082251822528225382254822558225682257822588225982260822618226282263822648226582266822678226882269822708227182272822738227482275822768227782278822798228082281822828228382284822858228682287822888228982290822918229282293822948229582296822978229882299823008230182302823038230482305823068230782308823098231082311823128231382314823158231682317823188231982320823218232282323823248232582326823278232882329823308233182332823338233482335823368233782338823398234082341823428234382344823458234682347823488234982350823518235282353823548235582356823578235882359823608236182362823638236482365823668236782368823698237082371823728237382374823758237682377823788237982380823818238282383823848238582386823878238882389823908239182392823938239482395823968239782398823998240082401824028240382404824058240682407824088240982410824118241282413824148241582416824178241882419824208242182422824238242482425824268242782428824298243082431824328243382434824358243682437824388243982440824418244282443824448244582446824478244882449824508245182452824538245482455824568245782458824598246082461824628246382464824658246682467824688246982470824718247282473824748247582476824778247882479824808248182482824838248482485824868248782488824898249082491824928249382494824958249682497824988249982500825018250282503825048250582506825078250882509825108251182512825138251482515825168251782518825198252082521825228252382524825258252682527825288252982530825318253282533825348253582536825378253882539825408254182542825438254482545825468254782548825498255082551825528255382554825558255682557825588255982560825618256282563825648256582566825678256882569825708257182572825738257482575825768257782578825798258082581825828258382584825858258682587825888258982590825918259282593825948259582596825978259882599826008260182602826038260482605826068260782608826098261082611826128261382614826158261682617826188261982620826218262282623826248262582626826278262882629826308263182632826338263482635826368263782638826398264082641826428264382644826458264682647826488264982650826518265282653826548265582656826578265882659826608266182662826638266482665826668266782668826698267082671826728267382674826758267682677826788267982680826818268282683826848268582686826878268882689826908269182692826938269482695826968269782698826998270082701827028270382704827058270682707827088270982710827118271282713827148271582716827178271882719827208272182722827238272482725827268272782728827298273082731827328273382734827358273682737827388273982740827418274282743827448274582746827478274882749827508275182752827538275482755827568275782758827598276082761827628276382764827658276682767827688276982770827718277282773827748277582776827778277882779827808278182782827838278482785827868278782788827898279082791827928279382794827958279682797827988279982800828018280282803828048280582806828078280882809828108281182812828138281482815828168281782818828198282082821828228282382824828258282682827828288282982830828318283282833828348283582836828378283882839828408284182842828438284482845828468284782848828498285082851828528285382854828558285682857828588285982860828618286282863828648286582866828678286882869828708287182872828738287482875828768287782878828798288082881828828288382884828858288682887828888288982890828918289282893828948289582896828978289882899829008290182902829038290482905829068290782908829098291082911829128291382914829158291682917829188291982920829218292282923829248292582926829278292882929829308293182932829338293482935829368293782938829398294082941829428294382944829458294682947829488294982950829518295282953829548295582956829578295882959829608296182962829638296482965829668296782968829698297082971829728297382974829758297682977829788297982980829818298282983829848298582986829878298882989829908299182992829938299482995829968299782998829998300083001830028300383004830058300683007830088300983010830118301283013830148301583016830178301883019830208302183022830238302483025830268302783028830298303083031830328303383034830358303683037830388303983040830418304283043830448304583046830478304883049830508305183052830538305483055830568305783058830598306083061830628306383064830658306683067830688306983070830718307283073830748307583076830778307883079830808308183082830838308483085830868308783088830898309083091830928309383094830958309683097830988309983100831018310283103831048310583106831078310883109831108311183112831138311483115831168311783118831198312083121831228312383124831258312683127831288312983130831318313283133831348313583136831378313883139831408314183142831438314483145831468314783148831498315083151831528315383154831558315683157831588315983160831618316283163831648316583166831678316883169831708317183172831738317483175831768317783178831798318083181831828318383184831858318683187831888318983190831918319283193831948319583196831978319883199832008320183202832038320483205832068320783208832098321083211832128321383214832158321683217832188321983220832218322283223832248322583226832278322883229832308323183232832338323483235832368323783238832398324083241832428324383244832458324683247832488324983250832518325283253832548325583256832578325883259832608326183262832638326483265832668326783268832698327083271832728327383274832758327683277832788327983280832818328283283832848328583286832878328883289832908329183292832938329483295832968329783298832998330083301833028330383304833058330683307833088330983310833118331283313833148331583316833178331883319833208332183322833238332483325833268332783328833298333083331833328333383334833358333683337833388333983340833418334283343833448334583346833478334883349833508335183352833538335483355833568335783358833598336083361833628336383364833658336683367833688336983370833718337283373833748337583376833778337883379833808338183382833838338483385833868338783388833898339083391833928339383394833958339683397833988339983400834018340283403834048340583406834078340883409834108341183412834138341483415834168341783418834198342083421834228342383424834258342683427834288342983430834318343283433834348343583436834378343883439834408344183442834438344483445834468344783448834498345083451834528345383454834558345683457834588345983460834618346283463834648346583466834678346883469834708347183472834738347483475834768347783478834798348083481834828348383484834858348683487834888348983490834918349283493834948349583496834978349883499835008350183502835038350483505835068350783508835098351083511835128351383514835158351683517835188351983520835218352283523835248352583526835278352883529835308353183532835338353483535835368353783538835398354083541835428354383544835458354683547835488354983550835518355283553835548355583556835578355883559835608356183562835638356483565835668356783568835698357083571835728357383574835758357683577835788357983580835818358283583835848358583586835878358883589835908359183592835938359483595835968359783598835998360083601836028360383604836058360683607836088360983610836118361283613836148361583616836178361883619836208362183622836238362483625836268362783628836298363083631836328363383634836358363683637836388363983640836418364283643836448364583646836478364883649836508365183652836538365483655836568365783658836598366083661836628366383664836658366683667836688366983670836718367283673836748367583676836778367883679836808368183682836838368483685836868368783688836898369083691836928369383694836958369683697836988369983700837018370283703837048370583706837078370883709837108371183712837138371483715837168371783718837198372083721837228372383724837258372683727837288372983730837318373283733837348373583736837378373883739837408374183742837438374483745837468374783748837498375083751837528375383754837558375683757837588375983760837618376283763837648376583766837678376883769837708377183772837738377483775837768377783778837798378083781837828378383784837858378683787837888378983790837918379283793837948379583796837978379883799838008380183802838038380483805838068380783808838098381083811838128381383814838158381683817838188381983820838218382283823838248382583826838278382883829838308383183832838338383483835838368383783838838398384083841838428384383844838458384683847838488384983850838518385283853838548385583856838578385883859838608386183862838638386483865838668386783868838698387083871838728387383874838758387683877838788387983880838818388283883838848388583886838878388883889838908389183892838938389483895838968389783898838998390083901839028390383904839058390683907839088390983910839118391283913839148391583916839178391883919839208392183922839238392483925839268392783928839298393083931839328393383934839358393683937839388393983940839418394283943839448394583946839478394883949839508395183952839538395483955839568395783958839598396083961839628396383964839658396683967839688396983970839718397283973839748397583976839778397883979839808398183982839838398483985839868398783988839898399083991839928399383994839958399683997839988399984000840018400284003840048400584006840078400884009840108401184012840138401484015840168401784018840198402084021840228402384024840258402684027840288402984030840318403284033840348403584036840378403884039840408404184042840438404484045840468404784048840498405084051840528405384054840558405684057840588405984060840618406284063840648406584066840678406884069840708407184072840738407484075840768407784078840798408084081840828408384084840858408684087840888408984090840918409284093840948409584096840978409884099841008410184102841038410484105841068410784108841098411084111841128411384114841158411684117841188411984120841218412284123841248412584126841278412884129841308413184132841338413484135841368413784138841398414084141841428414384144841458414684147841488414984150841518415284153841548415584156841578415884159841608416184162841638416484165841668416784168841698417084171841728417384174841758417684177841788417984180841818418284183841848418584186841878418884189841908419184192841938419484195841968419784198841998420084201842028420384204842058420684207842088420984210842118421284213842148421584216842178421884219842208422184222842238422484225842268422784228842298423084231842328423384234842358423684237842388423984240842418424284243842448424584246842478424884249842508425184252842538425484255842568425784258842598426084261842628426384264842658426684267842688426984270842718427284273842748427584276842778427884279842808428184282842838428484285842868428784288842898429084291842928429384294842958429684297842988429984300843018430284303843048430584306843078430884309843108431184312843138431484315843168431784318843198432084321843228432384324843258432684327843288432984330843318433284333843348433584336843378433884339843408434184342843438434484345843468434784348843498435084351843528435384354843558435684357843588435984360843618436284363843648436584366843678436884369843708437184372843738437484375843768437784378843798438084381843828438384384843858438684387843888438984390843918439284393843948439584396843978439884399844008440184402844038440484405844068440784408844098441084411844128441384414844158441684417844188441984420844218442284423844248442584426844278442884429844308443184432844338443484435844368443784438844398444084441844428444384444844458444684447844488444984450844518445284453844548445584456844578445884459844608446184462844638446484465844668446784468844698447084471844728447384474844758447684477844788447984480844818448284483844848448584486844878448884489844908449184492844938449484495844968449784498844998450084501845028450384504845058450684507845088450984510845118451284513845148451584516845178451884519845208452184522845238452484525845268452784528845298453084531845328453384534845358453684537845388453984540845418454284543845448454584546845478454884549845508455184552845538455484555845568455784558845598456084561845628456384564845658456684567845688456984570845718457284573845748457584576845778457884579845808458184582845838458484585845868458784588845898459084591845928459384594845958459684597845988459984600846018460284603846048460584606846078460884609846108461184612846138461484615846168461784618846198462084621846228462384624846258462684627846288462984630846318463284633846348463584636846378463884639846408464184642846438464484645846468464784648846498465084651846528465384654846558465684657846588465984660846618466284663846648466584666846678466884669846708467184672846738467484675846768467784678846798468084681846828468384684846858468684687846888468984690846918469284693846948469584696846978469884699847008470184702847038470484705847068470784708847098471084711847128471384714847158471684717847188471984720847218472284723847248472584726847278472884729847308473184732847338473484735847368473784738847398474084741847428474384744847458474684747847488474984750847518475284753847548475584756847578475884759847608476184762847638476484765847668476784768847698477084771847728477384774847758477684777847788477984780847818478284783847848478584786847878478884789847908479184792847938479484795847968479784798847998480084801848028480384804848058480684807848088480984810848118481284813848148481584816848178481884819848208482184822848238482484825848268482784828848298483084831848328483384834848358483684837848388483984840848418484284843848448484584846848478484884849848508485184852848538485484855848568485784858848598486084861848628486384864848658486684867848688486984870848718487284873848748487584876848778487884879848808488184882848838488484885848868488784888848898489084891848928489384894848958489684897848988489984900849018490284903849048490584906849078490884909849108491184912849138491484915849168491784918849198492084921849228492384924849258492684927849288492984930849318493284933849348493584936849378493884939849408494184942849438494484945849468494784948849498495084951849528495384954849558495684957849588495984960849618496284963849648496584966849678496884969849708497184972849738497484975849768497784978849798498084981849828498384984849858498684987849888498984990849918499284993849948499584996849978499884999850008500185002850038500485005850068500785008850098501085011850128501385014850158501685017850188501985020850218502285023850248502585026850278502885029850308503185032850338503485035850368503785038850398504085041850428504385044850458504685047850488504985050850518505285053850548505585056850578505885059850608506185062850638506485065850668506785068850698507085071850728507385074850758507685077850788507985080850818508285083850848508585086850878508885089850908509185092850938509485095850968509785098850998510085101851028510385104851058510685107851088510985110851118511285113851148511585116851178511885119851208512185122851238512485125851268512785128851298513085131851328513385134851358513685137851388513985140851418514285143851448514585146851478514885149851508515185152851538515485155851568515785158851598516085161851628516385164851658516685167851688516985170851718517285173851748517585176851778517885179851808518185182851838518485185851868518785188851898519085191851928519385194851958519685197851988519985200852018520285203852048520585206852078520885209852108521185212852138521485215852168521785218852198522085221852228522385224852258522685227852288522985230852318523285233852348523585236852378523885239852408524185242852438524485245852468524785248852498525085251852528525385254852558525685257852588525985260852618526285263852648526585266852678526885269852708527185272852738527485275852768527785278852798528085281852828528385284852858528685287852888528985290852918529285293852948529585296852978529885299853008530185302853038530485305853068530785308853098531085311853128531385314853158531685317853188531985320853218532285323853248532585326853278532885329853308533185332853338533485335853368533785338853398534085341853428534385344853458534685347853488534985350853518535285353853548535585356853578535885359853608536185362853638536485365853668536785368853698537085371853728537385374853758537685377853788537985380853818538285383853848538585386853878538885389853908539185392853938539485395853968539785398853998540085401854028540385404854058540685407854088540985410854118541285413854148541585416854178541885419854208542185422854238542485425854268542785428854298543085431854328543385434854358543685437854388543985440854418544285443854448544585446854478544885449854508545185452854538545485455854568545785458854598546085461854628546385464854658546685467854688546985470854718547285473854748547585476854778547885479854808548185482854838548485485854868548785488854898549085491854928549385494854958549685497854988549985500855018550285503855048550585506855078550885509855108551185512855138551485515855168551785518855198552085521855228552385524855258552685527855288552985530855318553285533855348553585536855378553885539855408554185542855438554485545855468554785548855498555085551855528555385554855558555685557855588555985560855618556285563855648556585566855678556885569855708557185572855738557485575855768557785578855798558085581855828558385584855858558685587855888558985590855918559285593855948559585596855978559885599856008560185602856038560485605856068560785608856098561085611856128561385614856158561685617856188561985620856218562285623856248562585626856278562885629856308563185632856338563485635856368563785638856398564085641856428564385644856458564685647856488564985650856518565285653856548565585656856578565885659856608566185662856638566485665856668566785668856698567085671856728567385674856758567685677856788567985680856818568285683856848568585686856878568885689856908569185692856938569485695856968569785698856998570085701857028570385704857058570685707857088570985710857118571285713857148571585716857178571885719857208572185722857238572485725857268572785728857298573085731857328573385734857358573685737857388573985740857418574285743857448574585746857478574885749857508575185752857538575485755857568575785758857598576085761857628576385764857658576685767857688576985770857718577285773857748577585776857778577885779857808578185782857838578485785857868578785788857898579085791857928579385794857958579685797857988579985800858018580285803858048580585806858078580885809858108581185812858138581485815858168581785818858198582085821858228582385824858258582685827858288582985830858318583285833858348583585836858378583885839858408584185842858438584485845858468584785848858498585085851858528585385854858558585685857858588585985860858618586285863858648586585866858678586885869858708587185872858738587485875858768587785878858798588085881858828588385884858858588685887858888588985890858918589285893858948589585896858978589885899859008590185902859038590485905859068590785908859098591085911859128591385914859158591685917859188591985920859218592285923859248592585926859278592885929859308593185932859338593485935859368593785938859398594085941859428594385944859458594685947859488594985950859518595285953859548595585956859578595885959859608596185962859638596485965859668596785968859698597085971859728597385974859758597685977859788597985980859818598285983859848598585986859878598885989859908599185992859938599485995859968599785998859998600086001860028600386004860058600686007860088600986010860118601286013860148601586016860178601886019860208602186022860238602486025860268602786028860298603086031860328603386034860358603686037860388603986040860418604286043860448604586046860478604886049860508605186052860538605486055860568605786058860598606086061860628606386064860658606686067860688606986070860718607286073860748607586076860778607886079860808608186082860838608486085860868608786088860898609086091860928609386094860958609686097860988609986100861018610286103861048610586106861078610886109861108611186112861138611486115861168611786118861198612086121861228612386124861258612686127861288612986130861318613286133861348613586136861378613886139861408614186142861438614486145861468614786148861498615086151861528615386154861558615686157861588615986160861618616286163861648616586166861678616886169861708617186172861738617486175861768617786178861798618086181861828618386184861858618686187861888618986190861918619286193861948619586196861978619886199862008620186202862038620486205862068620786208862098621086211862128621386214862158621686217862188621986220862218622286223862248622586226862278622886229862308623186232862338623486235862368623786238862398624086241862428624386244862458624686247862488624986250862518625286253862548625586256862578625886259862608626186262862638626486265862668626786268862698627086271862728627386274862758627686277862788627986280862818628286283862848628586286862878628886289862908629186292862938629486295862968629786298862998630086301863028630386304863058630686307863088630986310863118631286313863148631586316863178631886319863208632186322863238632486325863268632786328863298633086331863328633386334863358633686337863388633986340863418634286343863448634586346863478634886349863508635186352863538635486355863568635786358863598636086361863628636386364863658636686367863688636986370863718637286373863748637586376863778637886379863808638186382863838638486385863868638786388863898639086391863928639386394863958639686397863988639986400864018640286403864048640586406864078640886409864108641186412864138641486415864168641786418864198642086421864228642386424864258642686427864288642986430864318643286433864348643586436864378643886439864408644186442864438644486445864468644786448864498645086451864528645386454864558645686457864588645986460864618646286463864648646586466864678646886469864708647186472864738647486475864768647786478864798648086481864828648386484864858648686487864888648986490864918649286493864948649586496864978649886499865008650186502865038650486505865068650786508865098651086511865128651386514865158651686517865188651986520865218652286523865248652586526865278652886529865308653186532865338653486535865368653786538865398654086541865428654386544865458654686547865488654986550865518655286553865548655586556865578655886559865608656186562865638656486565865668656786568865698657086571865728657386574865758657686577865788657986580865818658286583865848658586586865878658886589865908659186592865938659486595865968659786598865998660086601866028660386604866058660686607866088660986610866118661286613866148661586616866178661886619866208662186622866238662486625866268662786628866298663086631866328663386634866358663686637866388663986640866418664286643866448664586646866478664886649866508665186652866538665486655866568665786658866598666086661866628666386664866658666686667866688666986670866718667286673866748667586676866778667886679866808668186682866838668486685866868668786688866898669086691866928669386694866958669686697866988669986700867018670286703867048670586706867078670886709867108671186712867138671486715867168671786718867198672086721867228672386724867258672686727867288672986730867318673286733867348673586736867378673886739867408674186742867438674486745867468674786748867498675086751867528675386754867558675686757867588675986760867618676286763867648676586766867678676886769867708677186772867738677486775867768677786778867798678086781867828678386784867858678686787867888678986790867918679286793867948679586796867978679886799868008680186802868038680486805868068680786808868098681086811868128681386814868158681686817868188681986820868218682286823868248682586826868278682886829868308683186832868338683486835868368683786838868398684086841868428684386844868458684686847868488684986850868518685286853868548685586856868578685886859868608686186862868638686486865868668686786868868698687086871868728687386874868758687686877868788687986880868818688286883868848688586886868878688886889868908689186892868938689486895868968689786898868998690086901869028690386904869058690686907869088690986910869118691286913869148691586916869178691886919869208692186922869238692486925869268692786928869298693086931869328693386934869358693686937869388693986940869418694286943869448694586946869478694886949869508695186952869538695486955869568695786958869598696086961869628696386964869658696686967869688696986970869718697286973869748697586976869778697886979869808698186982869838698486985869868698786988869898699086991869928699386994869958699686997869988699987000870018700287003870048700587006870078700887009870108701187012870138701487015870168701787018870198702087021870228702387024870258702687027870288702987030870318703287033870348703587036870378703887039870408704187042870438704487045870468704787048870498705087051870528705387054870558705687057870588705987060870618706287063870648706587066870678706887069870708707187072870738707487075870768707787078870798708087081870828708387084870858708687087870888708987090870918709287093870948709587096870978709887099871008710187102871038710487105871068710787108871098711087111871128711387114871158711687117871188711987120871218712287123871248712587126871278712887129871308713187132871338713487135871368713787138871398714087141871428714387144871458714687147871488714987150871518715287153871548715587156871578715887159871608716187162871638716487165871668716787168871698717087171871728717387174871758717687177871788717987180871818718287183871848718587186871878718887189871908719187192871938719487195871968719787198871998720087201872028720387204872058720687207872088720987210872118721287213872148721587216872178721887219872208722187222872238722487225872268722787228872298723087231872328723387234872358723687237872388723987240872418724287243872448724587246872478724887249872508725187252872538725487255872568725787258872598726087261872628726387264872658726687267872688726987270872718727287273872748727587276872778727887279872808728187282872838728487285872868728787288872898729087291872928729387294872958729687297872988729987300873018730287303873048730587306873078730887309873108731187312873138731487315873168731787318873198732087321873228732387324873258732687327873288732987330873318733287333873348733587336873378733887339873408734187342873438734487345873468734787348873498735087351873528735387354873558735687357873588735987360873618736287363873648736587366873678736887369873708737187372873738737487375873768737787378873798738087381873828738387384873858738687387873888738987390873918739287393873948739587396873978739887399874008740187402874038740487405874068740787408874098741087411874128741387414874158741687417874188741987420874218742287423874248742587426874278742887429874308743187432874338743487435874368743787438874398744087441874428744387444874458744687447874488744987450874518745287453874548745587456874578745887459874608746187462874638746487465874668746787468874698747087471874728747387474874758747687477874788747987480874818748287483874848748587486874878748887489874908749187492874938749487495874968749787498874998750087501875028750387504875058750687507875088750987510875118751287513875148751587516875178751887519875208752187522875238752487525875268752787528875298753087531875328753387534875358753687537875388753987540875418754287543875448754587546875478754887549875508755187552875538755487555875568755787558875598756087561875628756387564875658756687567875688756987570875718757287573875748757587576875778757887579875808758187582875838758487585875868758787588875898759087591875928759387594875958759687597875988759987600876018760287603876048760587606876078760887609876108761187612876138761487615876168761787618876198762087621876228762387624876258762687627876288762987630876318763287633876348763587636876378763887639876408764187642876438764487645876468764787648876498765087651876528765387654876558765687657876588765987660876618766287663876648766587666876678766887669876708767187672876738767487675876768767787678876798768087681876828768387684876858768687687876888768987690876918769287693876948769587696876978769887699877008770187702877038770487705877068770787708877098771087711877128771387714877158771687717877188771987720877218772287723877248772587726877278772887729877308773187732877338773487735877368773787738877398774087741877428774387744877458774687747877488774987750877518775287753877548775587756877578775887759877608776187762877638776487765877668776787768877698777087771877728777387774877758777687777877788777987780877818778287783877848778587786877878778887789877908779187792877938779487795877968779787798877998780087801878028780387804878058780687807878088780987810878118781287813878148781587816878178781887819878208782187822878238782487825878268782787828878298783087831878328783387834878358783687837878388783987840878418784287843878448784587846878478784887849878508785187852878538785487855878568785787858878598786087861878628786387864878658786687867878688786987870878718787287873878748787587876878778787887879878808788187882878838788487885878868788787888878898789087891878928789387894878958789687897878988789987900879018790287903879048790587906879078790887909879108791187912879138791487915879168791787918879198792087921879228792387924879258792687927879288792987930879318793287933879348793587936879378793887939879408794187942879438794487945879468794787948879498795087951879528795387954879558795687957879588795987960879618796287963879648796587966879678796887969879708797187972879738797487975879768797787978879798798087981879828798387984879858798687987879888798987990879918799287993879948799587996879978799887999880008800188002880038800488005880068800788008880098801088011880128801388014880158801688017880188801988020880218802288023880248802588026880278802888029880308803188032880338803488035880368803788038880398804088041880428804388044880458804688047880488804988050880518805288053880548805588056880578805888059880608806188062880638806488065880668806788068880698807088071880728807388074880758807688077880788807988080880818808288083880848808588086880878808888089880908809188092880938809488095880968809788098880998810088101881028810388104881058810688107881088810988110881118811288113881148811588116881178811888119881208812188122881238812488125881268812788128881298813088131881328813388134881358813688137881388813988140881418814288143881448814588146881478814888149881508815188152881538815488155881568815788158881598816088161881628816388164881658816688167881688816988170881718817288173881748817588176881778817888179881808818188182881838818488185881868818788188881898819088191881928819388194881958819688197881988819988200882018820288203882048820588206882078820888209882108821188212882138821488215882168821788218882198822088221882228822388224882258822688227882288822988230882318823288233882348823588236882378823888239882408824188242882438824488245882468824788248882498825088251882528825388254882558825688257882588825988260882618826288263882648826588266882678826888269882708827188272882738827488275882768827788278882798828088281882828828388284882858828688287882888828988290882918829288293882948829588296882978829888299883008830188302883038830488305883068830788308883098831088311883128831388314883158831688317883188831988320883218832288323883248832588326883278832888329883308833188332883338833488335883368833788338883398834088341883428834388344883458834688347883488834988350883518835288353883548835588356883578835888359883608836188362883638836488365883668836788368883698837088371883728837388374883758837688377883788837988380883818838288383883848838588386883878838888389883908839188392883938839488395883968839788398883998840088401884028840388404884058840688407884088840988410884118841288413884148841588416884178841888419884208842188422884238842488425884268842788428884298843088431884328843388434884358843688437884388843988440884418844288443884448844588446884478844888449884508845188452884538845488455884568845788458884598846088461884628846388464884658846688467884688846988470884718847288473884748847588476884778847888479884808848188482884838848488485884868848788488884898849088491884928849388494884958849688497884988849988500885018850288503885048850588506885078850888509885108851188512885138851488515885168851788518885198852088521885228852388524885258852688527885288852988530885318853288533885348853588536885378853888539885408854188542885438854488545885468854788548885498855088551885528855388554885558855688557885588855988560885618856288563885648856588566885678856888569885708857188572885738857488575885768857788578885798858088581885828858388584885858858688587885888858988590885918859288593885948859588596885978859888599886008860188602886038860488605886068860788608886098861088611886128861388614886158861688617886188861988620886218862288623886248862588626886278862888629886308863188632886338863488635886368863788638886398864088641886428864388644886458864688647886488864988650886518865288653886548865588656886578865888659886608866188662886638866488665886668866788668886698867088671886728867388674886758867688677886788867988680886818868288683886848868588686886878868888689886908869188692886938869488695886968869788698886998870088701887028870388704887058870688707887088870988710887118871288713887148871588716887178871888719887208872188722887238872488725887268872788728887298873088731887328873388734887358873688737887388873988740887418874288743887448874588746887478874888749887508875188752887538875488755887568875788758887598876088761887628876388764887658876688767887688876988770887718877288773887748877588776887778877888779887808878188782887838878488785887868878788788887898879088791887928879388794887958879688797887988879988800888018880288803888048880588806888078880888809888108881188812888138881488815888168881788818888198882088821888228882388824888258882688827888288882988830888318883288833888348883588836888378883888839888408884188842888438884488845888468884788848888498885088851888528885388854888558885688857888588885988860888618886288863888648886588866888678886888869888708887188872888738887488875888768887788878888798888088881888828888388884888858888688887888888888988890888918889288893888948889588896888978889888899889008890188902889038890488905889068890788908889098891088911889128891388914889158891688917889188891988920889218892288923889248892588926889278892888929889308893188932889338893488935889368893788938889398894088941889428894388944889458894688947889488894988950889518895288953889548895588956889578895888959889608896188962889638896488965889668896788968889698897088971889728897388974889758897688977889788897988980889818898288983889848898588986889878898888989889908899188992889938899488995889968899788998889998900089001890028900389004890058900689007890088900989010890118901289013890148901589016890178901889019890208902189022890238902489025890268902789028890298903089031890328903389034890358903689037890388903989040890418904289043890448904589046890478904889049890508905189052890538905489055890568905789058890598906089061890628906389064890658906689067890688906989070890718907289073890748907589076890778907889079890808908189082890838908489085890868908789088890898909089091890928909389094890958909689097890988909989100891018910289103891048910589106891078910889109891108911189112891138911489115891168911789118891198912089121891228912389124891258912689127891288912989130891318913289133891348913589136891378913889139891408914189142891438914489145891468914789148891498915089151891528915389154891558915689157891588915989160891618916289163891648916589166891678916889169891708917189172891738917489175891768917789178891798918089181891828918389184891858918689187891888918989190891918919289193891948919589196891978919889199892008920189202892038920489205892068920789208892098921089211892128921389214892158921689217892188921989220892218922289223892248922589226892278922889229892308923189232892338923489235892368923789238892398924089241892428924389244892458924689247892488924989250892518925289253892548925589256892578925889259892608926189262892638926489265892668926789268892698927089271892728927389274892758927689277892788927989280892818928289283892848928589286892878928889289892908929189292892938929489295892968929789298892998930089301893028930389304893058930689307893088930989310893118931289313893148931589316893178931889319893208932189322893238932489325893268932789328893298933089331893328933389334893358933689337893388933989340893418934289343893448934589346893478934889349893508935189352893538935489355893568935789358893598936089361893628936389364893658936689367893688936989370893718937289373893748937589376893778937889379893808938189382893838938489385893868938789388893898939089391893928939389394893958939689397893988939989400894018940289403894048940589406894078940889409894108941189412894138941489415894168941789418894198942089421894228942389424894258942689427894288942989430894318943289433894348943589436894378943889439894408944189442894438944489445894468944789448894498945089451894528945389454894558945689457894588945989460894618946289463894648946589466894678946889469894708947189472894738947489475894768947789478894798948089481894828948389484894858948689487894888948989490894918949289493894948949589496894978949889499895008950189502895038950489505895068950789508895098951089511895128951389514895158951689517895188951989520895218952289523895248952589526895278952889529895308953189532895338953489535895368953789538895398954089541895428954389544895458954689547895488954989550895518955289553895548955589556895578955889559895608956189562895638956489565895668956789568895698957089571895728957389574895758957689577895788957989580895818958289583895848958589586895878958889589895908959189592895938959489595895968959789598895998960089601896028960389604896058960689607896088960989610896118961289613896148961589616896178961889619896208962189622896238962489625896268962789628896298963089631896328963389634896358963689637896388963989640896418964289643896448964589646896478964889649896508965189652896538965489655896568965789658896598966089661896628966389664896658966689667896688966989670896718967289673896748967589676896778967889679896808968189682896838968489685896868968789688896898969089691896928969389694896958969689697896988969989700897018970289703897048970589706897078970889709897108971189712897138971489715897168971789718897198972089721897228972389724897258972689727897288972989730897318973289733897348973589736897378973889739897408974189742897438974489745897468974789748897498975089751897528975389754897558975689757897588975989760897618976289763897648976589766897678976889769897708977189772897738977489775897768977789778897798978089781897828978389784897858978689787897888978989790897918979289793897948979589796897978979889799898008980189802898038980489805898068980789808898098981089811898128981389814898158981689817898188981989820898218982289823898248982589826898278982889829898308983189832898338983489835898368983789838898398984089841898428984389844898458984689847898488984989850898518985289853898548985589856898578985889859898608986189862898638986489865898668986789868898698987089871898728987389874898758987689877898788987989880898818988289883898848988589886898878988889889898908989189892898938989489895898968989789898898998990089901899028990389904899058990689907899088990989910899118991289913899148991589916899178991889919899208992189922899238992489925899268992789928899298993089931899328993389934899358993689937899388993989940899418994289943899448994589946899478994889949899508995189952899538995489955899568995789958899598996089961899628996389964899658996689967899688996989970899718997289973899748997589976899778997889979899808998189982899838998489985899868998789988899898999089991899928999389994899958999689997899988999990000900019000290003900049000590006900079000890009900109001190012900139001490015900169001790018900199002090021900229002390024900259002690027900289002990030900319003290033900349003590036900379003890039900409004190042900439004490045900469004790048900499005090051900529005390054900559005690057900589005990060900619006290063900649006590066900679006890069900709007190072900739007490075900769007790078900799008090081900829008390084900859008690087900889008990090900919009290093900949009590096900979009890099901009010190102901039010490105901069010790108901099011090111901129011390114901159011690117901189011990120901219012290123901249012590126901279012890129901309013190132901339013490135901369013790138901399014090141901429014390144901459014690147901489014990150901519015290153901549015590156901579015890159901609016190162901639016490165901669016790168901699017090171901729017390174901759017690177901789017990180901819018290183901849018590186901879018890189901909019190192901939019490195901969019790198901999020090201902029020390204902059020690207902089020990210902119021290213902149021590216902179021890219902209022190222902239022490225902269022790228902299023090231902329023390234902359023690237902389023990240902419024290243902449024590246902479024890249902509025190252902539025490255902569025790258902599026090261902629026390264902659026690267902689026990270902719027290273902749027590276902779027890279902809028190282902839028490285902869028790288902899029090291902929029390294902959029690297902989029990300903019030290303903049030590306903079030890309903109031190312903139031490315903169031790318903199032090321903229032390324903259032690327903289032990330903319033290333903349033590336903379033890339903409034190342903439034490345903469034790348903499035090351903529035390354903559035690357903589035990360903619036290363903649036590366903679036890369903709037190372903739037490375903769037790378903799038090381903829038390384903859038690387903889038990390903919039290393903949039590396903979039890399904009040190402904039040490405904069040790408904099041090411904129041390414904159041690417904189041990420904219042290423904249042590426904279042890429904309043190432904339043490435904369043790438904399044090441904429044390444904459044690447904489044990450904519045290453904549045590456904579045890459904609046190462904639046490465904669046790468904699047090471904729047390474904759047690477904789047990480904819048290483904849048590486904879048890489904909049190492904939049490495904969049790498904999050090501905029050390504905059050690507905089050990510905119051290513905149051590516905179051890519905209052190522905239052490525905269052790528905299053090531905329053390534905359053690537905389053990540905419054290543905449054590546905479054890549905509055190552905539055490555905569055790558905599056090561905629056390564905659056690567905689056990570905719057290573905749057590576905779057890579905809058190582905839058490585905869058790588905899059090591905929059390594905959059690597905989059990600906019060290603906049060590606906079060890609906109061190612906139061490615906169061790618906199062090621906229062390624906259062690627906289062990630906319063290633906349063590636906379063890639906409064190642906439064490645906469064790648906499065090651906529065390654906559065690657906589065990660906619066290663906649066590666906679066890669906709067190672906739067490675906769067790678906799068090681906829068390684906859068690687906889068990690906919069290693906949069590696906979069890699907009070190702907039070490705907069070790708907099071090711907129071390714907159071690717907189071990720907219072290723907249072590726907279072890729907309073190732907339073490735907369073790738907399074090741907429074390744907459074690747907489074990750907519075290753907549075590756907579075890759907609076190762907639076490765907669076790768907699077090771907729077390774907759077690777907789077990780907819078290783907849078590786907879078890789907909079190792907939079490795907969079790798907999080090801908029080390804908059080690807908089080990810908119081290813908149081590816908179081890819908209082190822908239082490825908269082790828908299083090831908329083390834908359083690837908389083990840908419084290843908449084590846908479084890849908509085190852908539085490855908569085790858908599086090861908629086390864908659086690867908689086990870908719087290873908749087590876908779087890879908809088190882908839088490885908869088790888908899089090891908929089390894908959089690897908989089990900909019090290903909049090590906909079090890909909109091190912909139091490915909169091790918909199092090921909229092390924909259092690927909289092990930909319093290933909349093590936909379093890939909409094190942909439094490945909469094790948909499095090951909529095390954909559095690957909589095990960909619096290963909649096590966909679096890969909709097190972909739097490975909769097790978909799098090981909829098390984909859098690987909889098990990909919099290993909949099590996909979099890999910009100191002910039100491005910069100791008910099101091011910129101391014910159101691017910189101991020910219102291023910249102591026910279102891029910309103191032910339103491035910369103791038910399104091041910429104391044910459104691047910489104991050910519105291053910549105591056910579105891059910609106191062910639106491065910669106791068910699107091071910729107391074910759107691077910789107991080910819108291083910849108591086910879108891089910909109191092910939109491095910969109791098910999110091101911029110391104911059110691107911089110991110911119111291113911149111591116911179111891119911209112191122911239112491125911269112791128911299113091131911329113391134911359113691137911389113991140911419114291143911449114591146911479114891149911509115191152911539115491155911569115791158911599116091161911629116391164911659116691167911689116991170911719117291173911749117591176911779117891179911809118191182911839118491185911869118791188911899119091191911929119391194911959119691197911989119991200912019120291203912049120591206912079120891209912109121191212912139121491215912169121791218912199122091221912229122391224912259122691227912289122991230912319123291233912349123591236912379123891239912409124191242912439124491245912469124791248912499125091251912529125391254912559125691257912589125991260912619126291263912649126591266912679126891269912709127191272912739127491275912769127791278912799128091281912829128391284912859128691287912889128991290912919129291293912949129591296912979129891299913009130191302913039130491305913069130791308913099131091311913129131391314913159131691317913189131991320913219132291323913249132591326913279132891329913309133191332913339133491335913369133791338913399134091341913429134391344913459134691347913489134991350913519135291353913549135591356913579135891359913609136191362913639136491365913669136791368913699137091371913729137391374913759137691377913789137991380913819138291383913849138591386913879138891389913909139191392913939139491395913969139791398913999140091401914029140391404914059140691407914089140991410914119141291413914149141591416914179141891419914209142191422914239142491425914269142791428914299143091431914329143391434914359143691437914389143991440914419144291443914449144591446914479144891449914509145191452914539145491455914569145791458914599146091461914629146391464914659146691467914689146991470914719147291473914749147591476914779147891479914809148191482914839148491485914869148791488914899149091491914929149391494914959149691497914989149991500915019150291503915049150591506915079150891509915109151191512915139151491515915169151791518915199152091521915229152391524915259152691527915289152991530915319153291533915349153591536915379153891539915409154191542915439154491545915469154791548915499155091551915529155391554915559155691557915589155991560915619156291563915649156591566915679156891569915709157191572915739157491575915769157791578915799158091581915829158391584915859158691587915889158991590915919159291593915949159591596915979159891599916009160191602916039160491605916069160791608916099161091611916129161391614916159161691617916189161991620916219162291623916249162591626916279162891629916309163191632916339163491635916369163791638916399164091641916429164391644916459164691647916489164991650916519165291653916549165591656916579165891659916609166191662916639166491665916669166791668916699167091671916729167391674916759167691677916789167991680916819168291683916849168591686916879168891689916909169191692916939169491695916969169791698916999170091701917029170391704917059170691707917089170991710917119171291713917149171591716917179171891719917209172191722917239172491725917269172791728917299173091731917329173391734917359173691737917389173991740917419174291743917449174591746917479174891749917509175191752917539175491755917569175791758917599176091761917629176391764917659176691767917689176991770917719177291773917749177591776917779177891779917809178191782917839178491785917869178791788917899179091791917929179391794917959179691797917989179991800918019180291803918049180591806918079180891809918109181191812918139181491815918169181791818918199182091821918229182391824918259182691827918289182991830918319183291833918349183591836918379183891839918409184191842918439184491845918469184791848918499185091851918529185391854918559185691857918589185991860918619186291863918649186591866918679186891869918709187191872918739187491875918769187791878918799188091881918829188391884918859188691887918889188991890918919189291893918949189591896918979189891899919009190191902919039190491905919069190791908919099191091911919129191391914919159191691917919189191991920919219192291923919249192591926919279192891929919309193191932919339193491935919369193791938919399194091941919429194391944919459194691947919489194991950919519195291953919549195591956919579195891959919609196191962919639196491965919669196791968919699197091971919729197391974919759197691977919789197991980919819198291983919849198591986919879198891989919909199191992919939199491995919969199791998919999200092001920029200392004920059200692007920089200992010920119201292013920149201592016920179201892019920209202192022920239202492025920269202792028920299203092031920329203392034920359203692037920389203992040920419204292043920449204592046920479204892049920509205192052920539205492055920569205792058920599206092061920629206392064920659206692067920689206992070920719207292073920749207592076920779207892079920809208192082920839208492085920869208792088920899209092091920929209392094920959209692097920989209992100921019210292103921049210592106921079210892109921109211192112921139211492115921169211792118921199212092121921229212392124921259212692127921289212992130921319213292133921349213592136921379213892139921409214192142921439214492145921469214792148921499215092151921529215392154921559215692157921589215992160921619216292163921649216592166921679216892169921709217192172921739217492175921769217792178921799218092181921829218392184921859218692187921889218992190921919219292193921949219592196921979219892199922009220192202922039220492205922069220792208922099221092211922129221392214922159221692217922189221992220922219222292223922249222592226922279222892229922309223192232922339223492235922369223792238922399224092241922429224392244922459224692247922489224992250922519225292253922549225592256922579225892259922609226192262922639226492265922669226792268922699227092271922729227392274922759227692277922789227992280922819228292283922849228592286922879228892289922909229192292922939229492295922969229792298922999230092301923029230392304923059230692307923089230992310923119231292313923149231592316923179231892319923209232192322923239232492325923269232792328923299233092331923329233392334923359233692337923389233992340923419234292343923449234592346923479234892349923509235192352923539235492355923569235792358923599236092361923629236392364923659236692367923689236992370923719237292373923749237592376923779237892379923809238192382923839238492385923869238792388923899239092391923929239392394923959239692397923989239992400924019240292403924049240592406924079240892409924109241192412924139241492415924169241792418924199242092421924229242392424924259242692427924289242992430924319243292433924349243592436924379243892439924409244192442924439244492445924469244792448924499245092451924529245392454924559245692457924589245992460924619246292463924649246592466924679246892469924709247192472924739247492475924769247792478924799248092481924829248392484924859248692487924889248992490924919249292493924949249592496924979249892499925009250192502925039250492505925069250792508925099251092511925129251392514925159251692517925189251992520925219252292523925249252592526925279252892529925309253192532925339253492535925369253792538925399254092541925429254392544925459254692547925489254992550925519255292553925549255592556925579255892559925609256192562925639256492565925669256792568925699257092571925729257392574925759257692577925789257992580925819258292583925849258592586925879258892589925909259192592925939259492595925969259792598925999260092601926029260392604926059260692607926089260992610926119261292613926149261592616926179261892619926209262192622926239262492625926269262792628926299263092631926329263392634926359263692637926389263992640926419264292643926449264592646926479264892649926509265192652926539265492655926569265792658926599266092661926629266392664926659266692667926689266992670926719267292673926749267592676926779267892679926809268192682926839268492685926869268792688926899269092691926929269392694926959269692697926989269992700927019270292703927049270592706927079270892709927109271192712927139271492715927169271792718927199272092721927229272392724927259272692727927289272992730927319273292733927349273592736927379273892739927409274192742927439274492745927469274792748927499275092751927529275392754927559275692757927589275992760927619276292763927649276592766927679276892769927709277192772927739277492775927769277792778927799278092781927829278392784927859278692787927889278992790927919279292793927949279592796927979279892799928009280192802928039280492805928069280792808928099281092811928129281392814928159281692817928189281992820928219282292823928249282592826928279282892829928309283192832928339283492835928369283792838928399284092841928429284392844928459284692847928489284992850928519285292853928549285592856928579285892859928609286192862928639286492865928669286792868928699287092871928729287392874928759287692877928789287992880928819288292883928849288592886928879288892889928909289192892928939289492895928969289792898928999290092901929029290392904929059290692907929089290992910929119291292913929149291592916929179291892919929209292192922929239292492925929269292792928929299293092931929329293392934929359293692937929389293992940929419294292943929449294592946929479294892949929509295192952929539295492955929569295792958929599296092961929629296392964929659296692967929689296992970929719297292973929749297592976929779297892979929809298192982929839298492985929869298792988929899299092991929929299392994929959299692997929989299993000930019300293003930049300593006930079300893009930109301193012930139301493015930169301793018930199302093021930229302393024930259302693027930289302993030930319303293033930349303593036930379303893039930409304193042930439304493045930469304793048930499305093051930529305393054930559305693057930589305993060930619306293063930649306593066930679306893069930709307193072930739307493075930769307793078930799308093081930829308393084930859308693087930889308993090930919309293093930949309593096930979309893099931009310193102931039310493105931069310793108931099311093111931129311393114931159311693117931189311993120931219312293123931249312593126931279312893129931309313193132931339313493135931369313793138931399314093141931429314393144931459314693147931489314993150931519315293153931549315593156931579315893159931609316193162931639316493165931669316793168931699317093171931729317393174931759317693177931789317993180931819318293183931849318593186931879318893189931909319193192931939319493195931969319793198931999320093201932029320393204932059320693207932089320993210932119321293213932149321593216932179321893219932209322193222932239322493225932269322793228932299323093231932329323393234932359323693237932389323993240932419324293243932449324593246932479324893249932509325193252932539325493255932569325793258932599326093261932629326393264932659326693267932689326993270932719327293273932749327593276932779327893279932809328193282932839328493285932869328793288932899329093291932929329393294932959329693297932989329993300933019330293303933049330593306933079330893309933109331193312933139331493315933169331793318933199332093321933229332393324933259332693327933289332993330933319333293333933349333593336933379333893339933409334193342933439334493345933469334793348933499335093351933529335393354933559335693357933589335993360933619336293363933649336593366933679336893369933709337193372933739337493375933769337793378933799338093381933829338393384933859338693387933889338993390933919339293393933949339593396933979339893399934009340193402934039340493405934069340793408934099341093411934129341393414934159341693417934189341993420934219342293423934249342593426934279342893429934309343193432934339343493435934369343793438934399344093441934429344393444934459344693447934489344993450934519345293453934549345593456934579345893459934609346193462934639346493465934669346793468934699347093471934729347393474934759347693477934789347993480934819348293483934849348593486934879348893489934909349193492934939349493495934969349793498934999350093501935029350393504935059350693507935089350993510935119351293513935149351593516935179351893519935209352193522935239352493525935269352793528935299353093531935329353393534935359353693537935389353993540935419354293543935449354593546935479354893549935509355193552935539355493555935569355793558935599356093561935629356393564935659356693567935689356993570935719357293573935749357593576935779357893579935809358193582935839358493585935869358793588935899359093591935929359393594935959359693597935989359993600936019360293603936049360593606936079360893609936109361193612936139361493615936169361793618936199362093621936229362393624936259362693627936289362993630936319363293633936349363593636936379363893639936409364193642936439364493645936469364793648936499365093651936529365393654936559365693657936589365993660936619366293663936649366593666936679366893669936709367193672936739367493675936769367793678936799368093681936829368393684936859368693687936889368993690936919369293693936949369593696936979369893699937009370193702937039370493705937069370793708937099371093711937129371393714937159371693717937189371993720937219372293723937249372593726937279372893729937309373193732937339373493735937369373793738937399374093741937429374393744937459374693747937489374993750937519375293753937549375593756937579375893759937609376193762937639376493765937669376793768937699377093771937729377393774937759377693777937789377993780937819378293783937849378593786937879378893789937909379193792937939379493795937969379793798937999380093801938029380393804938059380693807938089380993810938119381293813938149381593816938179381893819938209382193822938239382493825938269382793828938299383093831938329383393834938359383693837938389383993840938419384293843938449384593846938479384893849938509385193852938539385493855938569385793858938599386093861938629386393864938659386693867938689386993870938719387293873938749387593876938779387893879938809388193882938839388493885938869388793888938899389093891938929389393894938959389693897938989389993900939019390293903939049390593906939079390893909939109391193912939139391493915939169391793918939199392093921939229392393924939259392693927939289392993930939319393293933939349393593936939379393893939939409394193942939439394493945939469394793948939499395093951939529395393954939559395693957939589395993960939619396293963939649396593966939679396893969939709397193972939739397493975939769397793978939799398093981939829398393984939859398693987939889398993990939919399293993939949399593996939979399893999940009400194002940039400494005940069400794008940099401094011940129401394014940159401694017940189401994020940219402294023940249402594026940279402894029940309403194032940339403494035940369403794038940399404094041940429404394044940459404694047940489404994050940519405294053940549405594056940579405894059940609406194062940639406494065940669406794068940699407094071940729407394074940759407694077940789407994080940819408294083940849408594086940879408894089940909409194092940939409494095940969409794098940999410094101941029410394104941059410694107941089410994110941119411294113941149411594116941179411894119941209412194122941239412494125941269412794128941299413094131941329413394134941359413694137941389413994140941419414294143941449414594146941479414894149941509415194152941539415494155941569415794158941599416094161941629416394164941659416694167941689416994170941719417294173941749417594176941779417894179941809418194182941839418494185941869418794188941899419094191941929419394194941959419694197941989419994200942019420294203942049420594206942079420894209942109421194212942139421494215942169421794218942199422094221942229422394224942259422694227942289422994230942319423294233942349423594236942379423894239942409424194242942439424494245942469424794248942499425094251942529425394254942559425694257942589425994260942619426294263942649426594266942679426894269942709427194272942739427494275942769427794278942799428094281942829428394284942859428694287942889428994290942919429294293942949429594296942979429894299943009430194302943039430494305943069430794308943099431094311943129431394314943159431694317943189431994320943219432294323943249432594326943279432894329943309433194332943339433494335943369433794338943399434094341943429434394344943459434694347943489434994350943519435294353943549435594356943579435894359943609436194362943639436494365943669436794368943699437094371943729437394374943759437694377943789437994380943819438294383943849438594386943879438894389943909439194392943939439494395943969439794398943999440094401944029440394404944059440694407944089440994410944119441294413944149441594416944179441894419944209442194422944239442494425944269442794428944299443094431944329443394434944359443694437944389443994440944419444294443944449444594446944479444894449944509445194452944539445494455944569445794458944599446094461944629446394464944659446694467944689446994470944719447294473944749447594476944779447894479944809448194482944839448494485944869448794488944899449094491944929449394494944959449694497944989449994500945019450294503945049450594506945079450894509945109451194512945139451494515945169451794518945199452094521945229452394524945259452694527945289452994530945319453294533945349453594536945379453894539945409454194542945439454494545945469454794548945499455094551945529455394554945559455694557945589455994560945619456294563945649456594566945679456894569945709457194572945739457494575945769457794578945799458094581945829458394584945859458694587945889458994590945919459294593945949459594596945979459894599946009460194602946039460494605946069460794608946099461094611946129461394614946159461694617946189461994620946219462294623946249462594626946279462894629946309463194632946339463494635946369463794638946399464094641946429464394644946459464694647946489464994650946519465294653946549465594656946579465894659946609466194662946639466494665946669466794668946699467094671946729467394674946759467694677946789467994680946819468294683946849468594686946879468894689946909469194692946939469494695946969469794698946999470094701947029470394704947059470694707947089470994710947119471294713947149471594716947179471894719947209472194722947239472494725947269472794728947299473094731947329473394734947359473694737947389473994740947419474294743947449474594746947479474894749947509475194752947539475494755947569475794758947599476094761947629476394764947659476694767947689476994770947719477294773947749477594776947779477894779947809478194782947839478494785947869478794788947899479094791947929479394794947959479694797947989479994800948019480294803948049480594806948079480894809948109481194812948139481494815948169481794818948199482094821948229482394824948259482694827948289482994830948319483294833948349483594836948379483894839948409484194842948439484494845948469484794848948499485094851948529485394854948559485694857948589485994860948619486294863948649486594866948679486894869948709487194872948739487494875948769487794878948799488094881948829488394884948859488694887948889488994890948919489294893948949489594896948979489894899949009490194902949039490494905949069490794908949099491094911949129491394914949159491694917949189491994920949219492294923949249492594926949279492894929949309493194932949339493494935949369493794938949399494094941949429494394944949459494694947949489494994950949519495294953949549495594956949579495894959949609496194962949639496494965949669496794968949699497094971949729497394974949759497694977949789497994980949819498294983949849498594986949879498894989949909499194992949939499494995949969499794998949999500095001950029500395004950059500695007950089500995010950119501295013950149501595016950179501895019950209502195022950239502495025950269502795028950299503095031950329503395034950359503695037950389503995040950419504295043950449504595046950479504895049950509505195052950539505495055950569505795058950599506095061950629506395064950659506695067950689506995070950719507295073950749507595076950779507895079950809508195082950839508495085950869508795088950899509095091950929509395094950959509695097950989509995100951019510295103951049510595106951079510895109951109511195112951139511495115951169511795118951199512095121951229512395124951259512695127951289512995130951319513295133951349513595136951379513895139951409514195142951439514495145951469514795148951499515095151951529515395154951559515695157951589515995160951619516295163951649516595166951679516895169951709517195172951739517495175951769517795178951799518095181951829518395184951859518695187951889518995190951919519295193951949519595196951979519895199952009520195202952039520495205952069520795208952099521095211952129521395214952159521695217952189521995220952219522295223952249522595226952279522895229952309523195232952339523495235952369523795238952399524095241952429524395244952459524695247952489524995250952519525295253952549525595256952579525895259952609526195262952639526495265952669526795268952699527095271952729527395274952759527695277952789527995280952819528295283952849528595286952879528895289952909529195292952939529495295952969529795298952999530095301953029530395304953059530695307953089530995310953119531295313953149531595316953179531895319953209532195322953239532495325953269532795328953299533095331953329533395334953359533695337953389533995340953419534295343953449534595346953479534895349953509535195352953539535495355953569535795358953599536095361953629536395364953659536695367953689536995370953719537295373953749537595376953779537895379953809538195382953839538495385953869538795388953899539095391953929539395394953959539695397953989539995400954019540295403954049540595406954079540895409954109541195412954139541495415954169541795418954199542095421954229542395424954259542695427954289542995430954319543295433954349543595436954379543895439954409544195442954439544495445954469544795448954499545095451954529545395454954559545695457954589545995460954619546295463954649546595466954679546895469954709547195472954739547495475954769547795478954799548095481954829548395484954859548695487954889548995490954919549295493954949549595496954979549895499955009550195502955039550495505955069550795508955099551095511955129551395514955159551695517955189551995520955219552295523955249552595526955279552895529955309553195532955339553495535955369553795538955399554095541955429554395544955459554695547955489554995550955519555295553955549555595556955579555895559955609556195562955639556495565955669556795568955699557095571955729557395574955759557695577955789557995580955819558295583955849558595586955879558895589955909559195592955939559495595955969559795598955999560095601956029560395604956059560695607956089560995610956119561295613956149561595616956179561895619956209562195622956239562495625956269562795628956299563095631956329563395634956359563695637956389563995640956419564295643956449564595646956479564895649956509565195652956539565495655956569565795658956599566095661956629566395664956659566695667956689566995670956719567295673956749567595676956779567895679956809568195682956839568495685956869568795688956899569095691956929569395694956959569695697956989569995700957019570295703957049570595706957079570895709957109571195712957139571495715957169571795718957199572095721957229572395724957259572695727957289572995730957319573295733957349573595736957379573895739957409574195742957439574495745957469574795748957499575095751957529575395754957559575695757957589575995760957619576295763957649576595766957679576895769957709577195772957739577495775957769577795778957799578095781957829578395784957859578695787957889578995790957919579295793957949579595796957979579895799958009580195802958039580495805958069580795808958099581095811958129581395814958159581695817958189581995820958219582295823958249582595826958279582895829958309583195832958339583495835958369583795838958399584095841958429584395844958459584695847958489584995850958519585295853958549585595856958579585895859958609586195862958639586495865958669586795868958699587095871958729587395874958759587695877958789587995880958819588295883958849588595886958879588895889958909589195892958939589495895958969589795898958999590095901959029590395904959059590695907959089590995910959119591295913959149591595916959179591895919959209592195922959239592495925959269592795928959299593095931959329593395934959359593695937959389593995940959419594295943959449594595946959479594895949959509595195952959539595495955959569595795958959599596095961959629596395964959659596695967959689596995970959719597295973959749597595976959779597895979959809598195982959839598495985959869598795988959899599095991959929599395994959959599695997959989599996000960019600296003960049600596006960079600896009960109601196012960139601496015960169601796018960199602096021960229602396024960259602696027960289602996030960319603296033960349603596036960379603896039960409604196042960439604496045960469604796048960499605096051960529605396054960559605696057960589605996060960619606296063960649606596066960679606896069960709607196072960739607496075960769607796078960799608096081960829608396084960859608696087960889608996090960919609296093960949609596096960979609896099961009610196102961039610496105961069610796108961099611096111961129611396114961159611696117961189611996120961219612296123961249612596126961279612896129961309613196132961339613496135961369613796138961399614096141961429614396144961459614696147961489614996150961519615296153961549615596156961579615896159961609616196162961639616496165961669616796168961699617096171961729617396174961759617696177961789617996180961819618296183961849618596186961879618896189961909619196192961939619496195961969619796198961999620096201962029620396204962059620696207962089620996210962119621296213962149621596216962179621896219962209622196222962239622496225962269622796228962299623096231962329623396234962359623696237962389623996240962419624296243962449624596246962479624896249962509625196252962539625496255962569625796258962599626096261962629626396264962659626696267962689626996270962719627296273962749627596276962779627896279962809628196282962839628496285962869628796288962899629096291962929629396294962959629696297962989629996300963019630296303963049630596306963079630896309963109631196312963139631496315963169631796318963199632096321963229632396324963259632696327963289632996330963319633296333963349633596336963379633896339963409634196342963439634496345963469634796348963499635096351963529635396354963559635696357963589635996360963619636296363963649636596366963679636896369963709637196372963739637496375963769637796378963799638096381963829638396384963859638696387963889638996390963919639296393963949639596396963979639896399964009640196402964039640496405964069640796408964099641096411964129641396414964159641696417964189641996420964219642296423964249642596426964279642896429964309643196432964339643496435964369643796438964399644096441964429644396444964459644696447964489644996450964519645296453964549645596456964579645896459964609646196462964639646496465964669646796468964699647096471964729647396474964759647696477964789647996480964819648296483964849648596486964879648896489964909649196492964939649496495964969649796498964999650096501965029650396504965059650696507965089650996510965119651296513965149651596516965179651896519965209652196522965239652496525965269652796528965299653096531965329653396534965359653696537965389653996540965419654296543965449654596546965479654896549965509655196552965539655496555965569655796558965599656096561965629656396564965659656696567965689656996570965719657296573965749657596576965779657896579965809658196582965839658496585965869658796588965899659096591965929659396594965959659696597965989659996600966019660296603966049660596606966079660896609966109661196612966139661496615966169661796618966199662096621966229662396624966259662696627966289662996630966319663296633966349663596636966379663896639966409664196642966439664496645966469664796648966499665096651966529665396654966559665696657966589665996660966619666296663966649666596666966679666896669966709667196672966739667496675966769667796678966799668096681966829668396684966859668696687966889668996690966919669296693966949669596696966979669896699967009670196702967039670496705967069670796708967099671096711967129671396714967159671696717967189671996720967219672296723967249672596726967279672896729967309673196732967339673496735967369673796738967399674096741967429674396744967459674696747967489674996750967519675296753967549675596756967579675896759967609676196762967639676496765967669676796768967699677096771967729677396774967759677696777967789677996780967819678296783967849678596786967879678896789967909679196792967939679496795967969679796798967999680096801968029680396804968059680696807968089680996810968119681296813968149681596816968179681896819968209682196822968239682496825968269682796828968299683096831968329683396834968359683696837968389683996840968419684296843968449684596846968479684896849968509685196852968539685496855968569685796858968599686096861968629686396864968659686696867968689686996870968719687296873968749687596876968779687896879968809688196882968839688496885968869688796888968899689096891968929689396894968959689696897968989689996900969019690296903969049690596906969079690896909969109691196912969139691496915969169691796918969199692096921969229692396924969259692696927969289692996930969319693296933969349693596936969379693896939969409694196942969439694496945969469694796948969499695096951969529695396954969559695696957969589695996960969619696296963969649696596966969679696896969969709697196972969739697496975969769697796978969799698096981969829698396984969859698696987969889698996990969919699296993969949699596996969979699896999970009700197002970039700497005970069700797008970099701097011970129701397014970159701697017970189701997020970219702297023970249702597026970279702897029970309703197032970339703497035970369703797038970399704097041970429704397044970459704697047970489704997050970519705297053970549705597056970579705897059970609706197062970639706497065970669706797068970699707097071970729707397074970759707697077970789707997080970819708297083970849708597086970879708897089970909709197092970939709497095970969709797098970999710097101971029710397104971059710697107971089710997110971119711297113971149711597116971179711897119971209712197122971239712497125971269712797128971299713097131971329713397134971359713697137971389713997140971419714297143971449714597146971479714897149971509715197152971539715497155971569715797158971599716097161971629716397164971659716697167971689716997170971719717297173971749717597176971779717897179971809718197182971839718497185971869718797188971899719097191971929719397194971959719697197971989719997200972019720297203972049720597206972079720897209972109721197212972139721497215972169721797218972199722097221972229722397224972259722697227972289722997230972319723297233972349723597236972379723897239972409724197242972439724497245972469724797248972499725097251972529725397254972559725697257972589725997260972619726297263972649726597266972679726897269972709727197272972739727497275972769727797278972799728097281972829728397284972859728697287972889728997290972919729297293972949729597296972979729897299973009730197302973039730497305973069730797308973099731097311973129731397314973159731697317973189731997320973219732297323973249732597326973279732897329973309733197332973339733497335973369733797338973399734097341973429734397344973459734697347973489734997350973519735297353973549735597356973579735897359973609736197362973639736497365973669736797368973699737097371973729737397374973759737697377973789737997380973819738297383973849738597386973879738897389973909739197392973939739497395973969739797398973999740097401974029740397404974059740697407974089740997410974119741297413974149741597416974179741897419974209742197422974239742497425974269742797428974299743097431974329743397434974359743697437974389743997440974419744297443974449744597446974479744897449974509745197452974539745497455974569745797458974599746097461974629746397464974659746697467974689746997470974719747297473974749747597476974779747897479974809748197482974839748497485974869748797488974899749097491974929749397494974959749697497974989749997500975019750297503975049750597506975079750897509975109751197512975139751497515975169751797518975199752097521975229752397524975259752697527975289752997530975319753297533975349753597536975379753897539975409754197542975439754497545975469754797548975499755097551975529755397554975559755697557975589755997560975619756297563975649756597566975679756897569975709757197572975739757497575975769757797578975799758097581975829758397584975859758697587975889758997590975919759297593975949759597596975979759897599976009760197602976039760497605976069760797608976099761097611976129761397614976159761697617976189761997620976219762297623976249762597626976279762897629976309763197632976339763497635976369763797638976399764097641976429764397644976459764697647976489764997650976519765297653976549765597656976579765897659976609766197662976639766497665976669766797668976699767097671976729767397674976759767697677976789767997680976819768297683976849768597686976879768897689976909769197692976939769497695976969769797698976999770097701977029770397704977059770697707977089770997710977119771297713977149771597716977179771897719977209772197722977239772497725977269772797728977299773097731977329773397734977359773697737977389773997740977419774297743977449774597746977479774897749977509775197752977539775497755977569775797758977599776097761977629776397764977659776697767977689776997770977719777297773977749777597776977779777897779977809778197782977839778497785977869778797788977899779097791977929779397794977959779697797977989779997800978019780297803978049780597806978079780897809978109781197812978139781497815978169781797818978199782097821978229782397824978259782697827978289782997830978319783297833978349783597836978379783897839978409784197842978439784497845978469784797848978499785097851978529785397854978559785697857978589785997860978619786297863978649786597866978679786897869978709787197872978739787497875978769787797878978799788097881978829788397884978859788697887978889788997890978919789297893978949789597896978979789897899979009790197902979039790497905979069790797908979099791097911979129791397914979159791697917979189791997920979219792297923979249792597926979279792897929979309793197932979339793497935979369793797938979399794097941979429794397944979459794697947979489794997950979519795297953979549795597956979579795897959979609796197962979639796497965979669796797968979699797097971979729797397974979759797697977979789797997980979819798297983979849798597986979879798897989979909799197992979939799497995979969799797998979999800098001980029800398004980059800698007980089800998010980119801298013980149801598016980179801898019980209802198022980239802498025980269802798028980299803098031980329803398034980359803698037980389803998040980419804298043980449804598046980479804898049980509805198052980539805498055980569805798058980599806098061980629806398064980659806698067980689806998070980719807298073980749807598076980779807898079980809808198082980839808498085980869808798088980899809098091980929809398094980959809698097980989809998100981019810298103981049810598106981079810898109981109811198112981139811498115981169811798118981199812098121981229812398124981259812698127981289812998130981319813298133981349813598136981379813898139981409814198142981439814498145981469814798148981499815098151981529815398154981559815698157981589815998160981619816298163981649816598166981679816898169981709817198172981739817498175981769817798178981799818098181981829818398184981859818698187981889818998190981919819298193981949819598196981979819898199982009820198202982039820498205982069820798208982099821098211982129821398214982159821698217982189821998220982219822298223982249822598226982279822898229982309823198232982339823498235982369823798238982399824098241982429824398244982459824698247982489824998250982519825298253982549825598256982579825898259982609826198262982639826498265982669826798268982699827098271982729827398274982759827698277982789827998280982819828298283982849828598286982879828898289982909829198292982939829498295982969829798298982999830098301983029830398304983059830698307983089830998310983119831298313983149831598316983179831898319983209832198322983239832498325983269832798328983299833098331983329833398334983359833698337983389833998340983419834298343983449834598346983479834898349983509835198352983539835498355983569835798358983599836098361983629836398364983659836698367983689836998370983719837298373983749837598376983779837898379983809838198382983839838498385983869838798388983899839098391983929839398394983959839698397983989839998400984019840298403984049840598406984079840898409984109841198412984139841498415984169841798418984199842098421984229842398424984259842698427984289842998430984319843298433984349843598436984379843898439984409844198442984439844498445984469844798448984499845098451984529845398454984559845698457984589845998460984619846298463984649846598466984679846898469984709847198472984739847498475984769847798478984799848098481984829848398484984859848698487984889848998490984919849298493984949849598496984979849898499985009850198502985039850498505985069850798508985099851098511985129851398514985159851698517985189851998520985219852298523985249852598526985279852898529985309853198532985339853498535985369853798538985399854098541985429854398544985459854698547985489854998550985519855298553985549855598556985579855898559985609856198562985639856498565985669856798568985699857098571985729857398574985759857698577985789857998580985819858298583985849858598586985879858898589985909859198592985939859498595985969859798598985999860098601986029860398604986059860698607986089860998610986119861298613986149861598616986179861898619986209862198622986239862498625986269862798628986299863098631986329863398634986359863698637986389863998640986419864298643986449864598646986479864898649986509865198652986539865498655986569865798658986599866098661986629866398664986659866698667986689866998670986719867298673986749867598676986779867898679986809868198682986839868498685986869868798688986899869098691986929869398694986959869698697986989869998700987019870298703987049870598706987079870898709987109871198712987139871498715987169871798718987199872098721987229872398724987259872698727987289872998730987319873298733987349873598736987379873898739987409874198742987439874498745987469874798748987499875098751987529875398754987559875698757987589875998760987619876298763987649876598766987679876898769987709877198772987739877498775987769877798778987799878098781987829878398784987859878698787987889878998790987919879298793987949879598796987979879898799988009880198802988039880498805988069880798808988099881098811988129881398814988159881698817988189881998820988219882298823988249882598826988279882898829988309883198832988339883498835988369883798838988399884098841988429884398844988459884698847988489884998850988519885298853988549885598856988579885898859988609886198862988639886498865988669886798868988699887098871988729887398874988759887698877988789887998880988819888298883988849888598886988879888898889988909889198892988939889498895988969889798898988999890098901989029890398904989059890698907989089890998910989119891298913989149891598916989179891898919989209892198922989239892498925989269892798928989299893098931989329893398934989359893698937989389893998940989419894298943989449894598946989479894898949989509895198952989539895498955989569895798958989599896098961989629896398964989659896698967989689896998970989719897298973989749897598976989779897898979989809898198982989839898498985989869898798988989899899098991989929899398994989959899698997989989899999000990019900299003990049900599006990079900899009990109901199012990139901499015990169901799018990199902099021990229902399024990259902699027990289902999030990319903299033990349903599036990379903899039990409904199042990439904499045990469904799048990499905099051990529905399054990559905699057990589905999060990619906299063990649906599066990679906899069990709907199072990739907499075990769907799078990799908099081990829908399084990859908699087990889908999090990919909299093990949909599096990979909899099991009910199102991039910499105991069910799108991099911099111991129911399114991159911699117991189911999120991219912299123991249912599126991279912899129991309913199132991339913499135991369913799138991399914099141991429914399144991459914699147991489914999150991519915299153991549915599156991579915899159991609916199162991639916499165991669916799168991699917099171991729917399174991759917699177991789917999180991819918299183991849918599186991879918899189991909919199192991939919499195991969919799198991999920099201992029920399204992059920699207992089920999210992119921299213992149921599216992179921899219992209922199222992239922499225992269922799228992299923099231992329923399234992359923699237992389923999240992419924299243992449924599246992479924899249992509925199252992539925499255992569925799258992599926099261992629926399264992659926699267992689926999270992719927299273992749927599276992779927899279992809928199282992839928499285992869928799288992899929099291992929929399294992959929699297992989929999300993019930299303993049930599306993079930899309993109931199312993139931499315993169931799318993199932099321993229932399324993259932699327993289932999330993319933299333993349933599336993379933899339993409934199342993439934499345993469934799348993499935099351993529935399354993559935699357993589935999360993619936299363993649936599366993679936899369993709937199372993739937499375993769937799378993799938099381993829938399384993859938699387993889938999390993919939299393993949939599396993979939899399994009940199402994039940499405994069940799408994099941099411994129941399414994159941699417994189941999420994219942299423994249942599426994279942899429994309943199432994339943499435994369943799438994399944099441994429944399444994459944699447994489944999450994519945299453994549945599456994579945899459994609946199462994639946499465994669946799468994699947099471994729947399474994759947699477994789947999480994819948299483994849948599486994879948899489994909949199492994939949499495994969949799498994999950099501995029950399504995059950699507995089950999510995119951299513995149951599516995179951899519995209952199522995239952499525995269952799528995299953099531995329953399534995359953699537995389953999540995419954299543995449954599546995479954899549995509955199552995539955499555995569955799558995599956099561995629956399564995659956699567995689956999570995719957299573995749957599576995779957899579995809958199582995839958499585995869958799588995899959099591995929959399594995959959699597995989959999600996019960299603996049960599606996079960899609996109961199612996139961499615996169961799618996199962099621996229962399624996259962699627996289962999630996319963299633996349963599636996379963899639996409964199642996439964499645996469964799648996499965099651996529965399654996559965699657996589965999660996619966299663996649966599666996679966899669996709967199672996739967499675996769967799678996799968099681996829968399684996859968699687996889968999690996919969299693996949969599696996979969899699997009970199702997039970499705997069970799708997099971099711997129971399714997159971699717997189971999720997219972299723997249972599726997279972899729997309973199732997339973499735997369973799738997399974099741997429974399744997459974699747997489974999750997519975299753997549975599756997579975899759997609976199762997639976499765997669976799768997699977099771997729977399774997759977699777997789977999780997819978299783997849978599786997879978899789997909979199792997939979499795997969979799798997999980099801998029980399804998059980699807998089980999810998119981299813998149981599816998179981899819998209982199822998239982499825998269982799828998299983099831998329983399834998359983699837998389983999840998419984299843998449984599846998479984899849998509985199852998539985499855998569985799858998599986099861998629986399864998659986699867998689986999870998719987299873998749987599876998779987899879998809988199882998839988499885998869988799888998899989099891998929989399894998959989699897998989989999900999019990299903999049990599906999079990899909999109991199912999139991499915999169991799918999199992099921999229992399924999259992699927999289992999930999319993299933999349993599936999379993899939999409994199942999439994499945999469994799948999499995099951999529995399954999559995699957999589995999960999619996299963999649996599966999679996899969999709997199972999739997499975999769997799978999799998099981999829998399984999859998699987999889998999990999919999299993999949999599996999979999899999100000100001100002100003100004100005100006100007100008100009100010100011100012100013100014100015100016100017100018100019100020100021100022100023100024100025100026100027100028100029100030100031100032100033100034100035100036100037100038100039100040100041100042100043100044100045100046100047100048100049100050100051100052100053100054100055100056100057100058100059100060100061100062100063100064100065100066100067100068100069100070100071100072100073100074100075100076100077100078100079100080100081100082100083100084100085100086100087100088100089100090100091100092100093100094100095100096100097100098100099100100100101100102100103100104100105100106100107100108100109100110100111100112100113100114100115100116100117100118100119100120100121100122100123100124100125100126100127100128100129100130100131100132100133100134100135100136100137100138100139100140100141100142100143100144100145100146100147100148100149100150100151100152100153100154100155100156100157100158100159100160100161100162100163100164100165100166100167100168100169100170100171100172100173100174100175100176100177100178100179100180100181100182100183100184100185100186100187100188100189100190100191100192100193100194100195100196100197100198100199100200100201100202100203100204100205100206100207100208100209100210100211100212100213100214100215100216100217100218100219100220100221100222100223100224100225100226100227100228100229100230100231100232100233100234100235100236100237100238100239100240100241100242100243100244100245100246100247100248100249100250100251100252100253100254100255100256100257100258100259100260100261100262100263100264100265100266100267100268100269100270100271100272100273100274100275100276100277100278100279100280100281100282100283100284100285100286100287100288100289100290100291100292100293100294100295100296100297100298100299100300100301100302100303100304100305100306100307100308100309100310100311100312100313100314100315100316100317100318100319100320100321100322100323100324100325100326100327100328100329100330100331100332100333100334100335100336100337100338100339100340100341100342100343100344100345100346100347100348100349100350100351100352100353100354100355100356100357100358100359100360100361100362100363100364100365100366100367100368100369100370100371100372100373100374100375100376100377100378100379100380100381100382100383100384100385100386100387100388100389100390100391100392100393100394100395100396100397100398100399100400100401100402100403100404100405100406100407100408100409100410100411100412100413100414100415100416100417100418100419100420100421100422100423100424100425100426100427100428100429100430100431100432100433100434100435100436100437100438100439100440100441100442100443100444100445100446100447100448100449100450100451100452100453100454100455100456100457100458100459100460100461100462100463100464100465100466100467100468100469100470100471100472100473100474100475100476100477100478100479100480100481100482100483100484100485100486100487100488100489100490100491100492100493100494100495100496100497100498100499100500100501100502100503100504100505100506100507100508100509100510100511100512100513100514100515100516100517100518100519100520100521100522100523100524100525100526100527100528100529100530100531100532100533100534100535100536100537100538100539100540100541100542100543100544100545100546100547100548100549100550100551100552100553100554100555100556100557100558100559100560100561100562100563100564100565100566100567100568100569100570100571100572100573100574100575100576100577100578100579100580100581100582100583100584100585100586100587100588100589100590100591100592100593100594100595100596100597100598100599100600100601100602100603100604100605100606100607100608100609100610100611100612100613100614100615100616100617100618100619100620100621100622100623100624100625100626100627100628100629100630100631100632100633100634100635100636100637100638100639100640100641100642100643100644100645100646100647100648100649100650100651100652100653100654100655100656100657100658100659100660100661100662100663100664100665100666100667100668100669100670100671100672100673100674100675100676100677100678100679100680100681100682100683100684100685100686100687100688100689100690100691100692100693100694100695100696100697100698100699100700100701100702100703100704100705100706100707100708100709100710100711100712100713100714100715100716100717100718100719100720100721100722100723100724100725100726100727100728100729100730100731100732100733100734100735100736100737100738100739100740100741100742100743100744100745100746100747100748100749100750100751100752100753100754100755100756100757100758100759100760100761100762100763100764100765100766100767100768100769100770100771100772100773100774100775100776100777100778100779100780100781100782100783100784100785100786100787100788100789100790100791100792100793100794100795100796100797100798100799100800100801100802100803100804100805100806100807100808100809100810100811100812100813100814100815100816100817100818100819100820100821100822100823100824100825100826100827100828100829100830100831100832100833100834100835100836100837100838100839100840100841100842100843100844100845100846100847100848100849100850100851100852100853100854100855100856100857100858100859100860100861100862100863100864100865100866100867100868100869100870100871100872100873100874100875100876100877100878100879100880100881100882100883100884100885100886100887100888100889100890100891100892100893100894100895100896100897100898100899100900100901100902100903100904100905100906100907100908100909100910100911100912100913100914100915100916100917100918100919100920100921100922100923100924100925100926100927100928100929100930100931100932100933100934100935100936100937100938100939100940100941100942100943100944100945100946100947100948100949100950100951100952100953100954100955100956100957100958100959100960100961100962100963100964100965100966100967100968100969100970100971100972100973100974100975100976100977100978100979100980100981100982100983100984100985100986100987100988100989100990100991100992100993100994100995100996100997100998100999101000101001101002101003101004101005101006101007101008101009101010101011101012101013101014101015101016101017101018101019101020101021101022101023101024101025101026101027101028101029101030101031101032101033101034101035101036101037101038101039101040101041101042101043101044101045101046101047101048101049101050101051101052101053101054101055101056101057101058101059101060101061101062101063101064101065101066101067101068101069101070101071101072101073101074101075101076101077101078101079101080101081101082101083101084101085101086101087101088101089101090101091101092101093101094101095101096101097101098101099101100101101101102101103101104101105101106101107101108101109101110101111101112101113101114101115101116101117101118101119101120101121101122101123101124101125101126101127101128101129101130101131101132101133101134101135101136101137101138101139101140101141101142101143101144101145101146101147101148101149101150101151101152101153101154101155101156101157101158101159101160101161101162101163101164101165101166101167101168101169101170101171101172101173101174101175101176101177101178101179101180101181101182101183101184101185101186101187101188101189101190101191101192101193101194101195101196101197101198101199101200101201101202101203101204101205101206101207101208101209101210101211101212101213101214101215101216101217101218101219101220101221101222101223101224101225101226101227101228101229101230101231101232101233101234101235101236101237101238101239101240101241101242101243101244101245101246101247101248101249101250101251101252101253101254101255101256101257101258101259101260101261101262101263101264101265101266101267101268101269101270101271101272101273101274101275101276101277101278101279101280101281101282101283101284101285101286101287101288101289101290101291101292101293101294101295101296101297101298101299101300101301101302101303101304101305101306101307101308101309101310101311101312101313101314101315101316101317101318101319101320101321101322101323101324101325101326101327101328101329101330101331101332101333101334101335101336101337101338101339101340101341101342101343101344101345101346101347101348101349101350101351101352101353101354101355101356101357101358101359101360101361101362101363101364101365101366101367101368101369101370101371101372101373101374101375101376101377101378101379101380101381101382101383101384101385101386101387101388101389101390101391101392101393101394101395101396101397101398101399101400101401101402101403101404101405101406101407101408101409101410101411101412101413101414101415101416101417101418101419101420101421101422101423101424101425101426101427101428101429101430101431101432101433101434101435101436101437101438101439101440101441101442101443101444101445101446101447101448101449101450101451101452101453101454101455101456101457101458101459101460101461101462101463101464101465101466101467101468101469101470101471101472101473101474101475101476101477101478101479101480101481101482101483101484101485101486101487101488101489101490101491101492101493101494101495101496101497101498101499101500101501101502101503101504101505101506101507101508101509101510101511101512101513101514101515101516101517101518101519101520101521101522101523101524101525101526101527101528101529101530101531101532101533101534101535101536101537101538101539101540101541101542101543101544101545101546101547101548101549101550101551101552101553101554101555101556101557101558101559101560101561101562101563101564101565101566101567101568101569101570101571101572101573101574101575101576101577101578101579101580101581101582101583101584101585101586101587101588101589101590101591101592101593101594101595101596101597101598101599101600101601101602101603101604101605101606101607101608101609101610101611101612101613101614101615101616101617101618101619101620101621101622101623101624101625101626101627101628101629101630101631101632101633101634101635101636101637101638101639101640101641101642101643101644101645101646101647101648101649101650101651101652101653101654101655101656101657101658101659101660101661101662101663101664101665101666101667101668101669101670101671101672101673101674101675101676101677101678101679101680101681101682101683101684101685101686101687101688101689101690101691101692101693101694101695101696101697101698101699101700101701101702101703101704101705101706101707101708101709101710101711101712101713101714101715101716101717101718101719101720101721101722101723101724101725101726101727101728101729101730101731101732101733101734101735101736101737101738101739101740101741101742101743101744101745101746101747101748101749101750101751101752101753101754101755101756101757101758101759101760101761101762101763101764101765101766101767101768101769101770101771101772101773101774101775101776101777101778101779101780101781101782101783101784101785101786101787101788101789101790101791101792101793101794101795101796101797101798101799101800101801101802101803101804101805101806101807101808101809101810101811101812101813101814101815101816101817101818101819101820101821101822101823101824101825101826101827101828101829101830101831101832101833101834101835101836101837101838101839101840101841101842101843101844101845101846101847101848101849101850101851101852101853101854101855101856101857101858101859101860101861101862101863101864101865101866101867101868101869101870101871101872101873101874101875101876101877101878101879101880101881101882101883101884101885101886101887101888101889101890101891101892101893101894101895101896101897101898101899101900101901101902101903101904101905101906101907101908101909101910101911101912101913101914101915101916101917101918101919101920101921101922101923101924101925101926101927101928101929101930101931101932101933101934101935101936101937101938101939101940101941101942101943101944101945101946101947101948101949101950101951101952101953101954101955101956101957101958101959101960101961101962101963101964101965101966101967101968101969101970101971101972101973101974101975101976101977101978101979101980101981101982101983101984101985101986101987101988101989101990101991101992101993101994101995101996101997101998101999102000102001102002102003102004102005102006102007102008102009102010102011102012102013102014102015102016102017102018102019102020102021102022102023102024102025102026102027102028102029102030102031102032102033102034102035102036102037102038102039102040102041102042102043102044102045102046102047102048102049102050102051102052102053102054102055102056102057102058102059102060102061102062102063102064102065102066102067102068102069102070102071102072102073102074102075102076102077102078102079102080102081102082102083102084102085102086102087102088102089102090102091102092102093102094102095102096102097102098102099102100102101102102102103102104102105102106102107102108102109102110102111102112102113102114102115102116102117102118102119102120102121102122102123102124102125102126102127102128102129102130102131102132102133102134102135102136102137102138102139102140102141102142102143102144102145102146102147102148102149102150102151102152102153102154102155102156102157102158102159102160102161102162102163102164102165102166102167102168102169102170102171102172102173102174102175102176102177102178102179102180102181102182102183102184102185102186102187102188102189102190102191102192102193102194102195102196102197102198102199102200102201102202102203102204102205102206102207102208102209102210102211102212102213102214102215102216102217102218102219102220102221102222102223102224102225102226102227102228102229102230102231102232102233102234102235102236102237102238102239102240102241102242102243102244102245102246102247102248102249102250102251102252102253102254102255102256102257102258102259102260102261102262102263102264102265102266102267102268102269102270102271102272102273102274102275102276102277102278102279102280102281102282102283102284102285102286102287102288102289102290102291102292102293102294102295102296102297102298102299102300102301102302102303102304102305102306102307102308102309102310102311102312102313102314102315102316102317102318102319102320102321102322102323102324102325102326102327102328102329102330102331102332102333102334102335102336102337102338102339102340102341102342102343102344102345102346102347102348102349102350102351102352102353102354102355102356102357102358102359102360102361102362102363102364102365102366102367102368102369102370102371102372102373102374102375102376102377102378102379102380102381102382102383102384102385102386102387102388102389102390102391102392102393102394102395102396102397102398102399102400102401102402102403102404102405102406102407102408102409102410102411102412102413102414102415102416102417102418102419102420102421102422102423102424102425102426102427102428102429102430102431102432102433102434102435102436102437102438102439102440102441102442102443102444102445102446102447102448102449102450102451102452102453102454102455102456102457102458102459102460102461102462102463102464102465102466102467102468102469102470102471102472102473102474102475102476102477102478102479102480102481102482102483102484102485102486102487102488102489102490102491102492102493102494102495102496102497102498102499102500102501102502102503102504102505102506102507102508102509102510102511102512102513102514102515102516102517102518102519102520102521102522102523102524102525102526102527102528102529102530102531102532102533102534102535102536102537102538102539102540102541102542102543102544102545102546102547102548102549102550102551102552102553102554102555102556102557102558102559102560102561102562102563102564102565102566102567102568102569102570102571102572102573102574102575102576102577102578102579102580102581102582102583102584102585102586102587102588102589102590102591102592102593102594102595102596102597102598102599102600102601102602102603102604102605102606102607102608102609102610102611102612102613102614102615102616102617102618102619102620102621102622102623102624102625102626102627102628102629102630102631102632102633102634102635102636102637102638102639102640102641102642102643102644102645102646102647102648102649102650102651102652102653102654102655102656102657102658102659102660102661102662102663102664102665102666102667102668102669102670102671102672102673102674102675102676102677102678102679102680102681102682102683102684102685102686102687102688102689102690102691102692102693102694102695102696102697102698102699102700102701102702102703102704102705102706102707102708102709102710102711102712102713102714102715102716102717102718102719102720102721102722102723102724102725102726102727102728102729102730102731102732102733102734102735102736102737102738102739102740102741102742102743102744102745102746102747102748102749102750102751102752102753102754102755102756102757102758102759102760102761102762102763102764102765102766102767102768102769102770102771102772102773102774102775102776102777102778102779102780102781102782102783102784102785102786102787102788102789102790102791102792102793102794102795102796102797102798102799102800102801102802102803102804102805102806102807102808102809102810102811102812102813102814102815102816102817102818102819102820102821102822102823102824102825102826102827102828102829102830102831102832102833102834102835102836102837102838102839102840102841102842102843102844102845102846102847102848102849102850102851102852102853102854102855102856102857102858102859102860102861102862102863102864102865102866102867102868102869102870102871102872102873102874102875102876102877102878102879102880102881102882102883102884102885102886102887102888102889102890102891102892102893102894102895102896102897102898102899102900102901102902102903102904102905102906102907102908102909102910102911102912102913102914102915102916102917102918102919102920102921102922102923102924102925102926102927102928102929102930102931102932102933102934102935102936102937102938102939102940102941102942102943102944102945102946102947102948102949102950102951102952102953102954102955102956102957102958102959102960102961102962102963102964102965102966102967102968102969102970102971102972102973102974102975102976102977102978102979102980102981102982102983102984102985102986102987102988102989102990102991102992102993102994102995102996102997102998102999103000103001103002103003103004103005103006103007103008103009103010103011103012103013103014103015103016103017103018103019103020103021103022103023103024103025103026103027103028103029103030103031103032103033103034103035103036103037103038103039103040103041103042103043103044103045103046103047103048103049103050103051103052103053103054103055103056103057103058103059103060103061103062103063103064103065103066103067103068103069103070103071103072103073103074103075103076103077103078103079103080103081103082103083103084103085103086103087103088103089103090103091103092103093103094103095103096103097103098103099103100103101103102103103103104103105103106103107103108103109103110103111103112103113103114103115103116103117103118103119103120103121103122103123103124103125103126103127103128103129103130103131103132103133103134103135103136103137103138103139103140103141103142103143103144103145103146103147103148103149103150103151103152103153103154103155103156103157103158103159103160103161103162103163103164103165103166103167103168103169103170103171103172103173103174103175103176103177103178103179103180103181103182103183103184103185103186103187103188103189103190103191103192103193103194103195103196103197103198103199103200103201103202103203103204103205103206103207103208103209103210103211103212103213103214103215103216103217103218103219103220103221103222103223103224103225103226103227103228103229103230103231103232103233103234103235103236103237103238103239103240103241103242103243103244103245103246103247103248103249103250103251103252103253103254103255103256103257103258103259103260103261103262103263103264103265103266103267103268103269103270103271103272103273103274103275103276103277103278103279103280103281103282103283103284103285103286103287103288103289103290103291103292103293103294103295103296103297103298103299103300103301103302103303103304103305103306103307103308103309103310103311103312103313103314103315103316103317103318103319103320103321103322103323103324103325103326103327103328103329103330103331103332103333103334103335103336103337103338103339103340103341103342103343103344103345103346103347103348103349103350103351103352103353103354103355103356103357103358103359103360103361103362103363103364103365103366103367103368103369103370103371103372103373103374103375103376103377103378103379103380103381103382103383103384103385103386103387103388103389103390103391103392103393103394103395103396103397103398103399103400103401103402103403103404103405103406103407103408103409103410103411103412103413103414103415103416103417103418103419103420103421103422103423103424103425103426103427103428103429103430103431103432103433103434103435103436103437103438103439103440103441103442103443103444103445103446103447103448103449103450103451103452103453103454103455103456103457103458103459103460103461103462103463103464103465103466103467103468103469103470103471103472103473103474103475103476103477103478103479103480103481103482103483103484103485103486103487103488103489103490103491103492103493103494103495103496103497103498103499103500103501103502103503103504103505103506103507103508103509103510103511103512103513103514103515103516103517103518103519103520103521103522103523103524103525103526103527103528103529103530103531103532103533103534103535103536103537103538103539103540103541103542103543103544103545103546103547103548103549103550103551103552103553103554103555103556103557103558103559103560103561103562103563103564103565103566103567103568103569103570103571103572103573103574103575103576103577103578103579103580103581103582103583103584103585103586103587103588103589103590103591103592103593103594103595103596103597103598103599103600103601103602103603103604103605103606103607103608103609103610103611103612103613103614103615103616103617103618103619103620103621103622103623103624103625103626103627103628103629103630103631103632103633103634103635103636103637103638103639103640103641103642103643103644103645103646103647103648103649103650103651103652103653103654103655103656103657103658103659103660103661103662103663103664103665103666103667103668103669103670103671103672103673103674103675103676103677103678103679103680103681103682103683103684103685103686103687103688103689103690103691103692103693103694103695103696103697103698103699103700103701103702103703103704103705103706103707103708103709103710103711103712103713103714103715103716103717103718103719103720103721103722103723103724103725103726103727103728103729103730103731103732103733103734103735103736103737103738103739103740103741103742103743103744103745103746103747103748103749103750103751103752103753103754103755103756103757103758103759103760103761103762103763103764103765103766103767103768103769103770103771103772103773103774103775103776103777103778103779103780103781103782103783103784103785103786103787103788103789103790103791103792103793103794103795103796103797103798103799103800103801103802103803103804103805103806103807103808103809103810103811103812103813103814103815103816103817103818103819103820103821103822103823103824103825103826103827103828103829103830103831103832103833103834103835103836103837103838103839103840103841103842103843103844103845103846103847103848103849103850103851103852103853103854103855103856103857103858103859103860103861103862103863103864103865103866103867103868103869103870103871103872103873103874103875103876103877103878103879103880103881103882103883103884103885103886103887103888103889103890103891103892103893103894103895103896103897103898103899103900103901103902103903103904103905103906103907103908103909103910103911103912103913103914103915103916103917103918103919103920103921103922103923103924103925103926103927103928103929103930103931103932103933103934103935103936103937103938103939103940103941103942103943103944103945103946103947103948103949103950103951103952103953103954103955103956103957103958103959103960103961103962103963103964103965103966103967103968103969103970103971103972103973103974103975103976103977103978103979103980103981103982103983103984103985103986103987103988103989103990103991103992103993103994103995103996103997103998103999104000104001104002104003104004104005104006104007104008104009104010104011104012104013104014104015104016104017104018104019104020104021104022104023104024104025104026104027104028104029104030104031104032104033104034104035104036104037104038104039104040104041104042104043104044104045104046104047104048104049104050104051104052104053104054104055104056104057104058104059104060104061104062104063104064104065104066104067104068104069104070104071104072104073104074104075104076104077104078104079104080104081104082104083104084104085104086104087104088104089104090104091104092104093104094104095104096104097104098104099104100104101104102104103104104104105104106104107104108104109104110104111104112104113104114104115104116104117104118104119104120104121104122104123104124104125104126104127104128104129104130104131104132104133104134104135104136104137104138104139104140104141104142104143104144104145104146104147104148104149104150104151104152104153104154104155104156104157104158104159104160104161104162104163104164104165104166104167104168104169104170104171104172104173104174104175104176104177104178104179104180104181104182104183104184104185104186104187104188104189104190104191104192104193104194104195104196104197104198104199104200104201104202104203104204104205104206104207104208104209104210104211104212104213104214104215104216104217104218104219104220104221104222104223104224104225104226104227104228104229104230104231104232104233104234104235104236104237104238104239104240104241104242104243104244104245104246104247104248104249104250104251104252104253104254104255104256104257104258104259104260104261104262104263104264104265104266104267104268104269104270104271104272104273104274104275104276104277104278104279104280104281104282104283104284104285104286104287104288104289104290104291104292104293104294104295104296104297104298104299104300104301104302104303104304104305104306104307104308104309104310104311104312104313104314104315104316104317104318104319104320104321104322104323104324104325104326104327104328104329104330104331104332104333104334104335104336104337104338104339104340104341104342104343104344104345104346104347104348104349104350104351104352104353104354104355104356104357104358104359104360104361104362104363104364104365104366104367104368104369104370104371104372104373104374104375104376104377104378104379104380104381104382104383104384104385104386104387104388104389104390104391104392104393104394104395104396104397104398104399104400104401104402104403104404104405104406104407104408104409104410104411104412104413104414104415104416104417104418104419104420104421104422104423104424104425104426104427104428104429104430104431104432104433104434104435104436104437104438104439104440104441104442104443104444104445104446104447104448104449104450104451104452104453104454104455104456104457104458104459104460104461104462104463104464104465104466104467104468104469104470104471104472104473104474104475104476104477104478104479104480104481104482104483104484104485104486104487104488104489104490104491104492104493104494104495104496104497104498104499104500104501104502104503104504104505104506104507104508104509104510104511104512104513104514104515104516104517104518104519104520104521104522104523104524104525104526104527104528104529104530104531104532104533104534104535104536104537104538104539104540104541104542104543104544104545104546104547104548104549104550104551104552104553104554104555104556104557104558104559104560104561104562104563104564104565104566104567104568104569104570104571104572104573104574104575104576104577104578104579104580104581104582104583104584104585104586104587104588104589104590104591104592104593104594104595104596104597104598104599104600104601104602104603104604104605104606104607104608104609104610104611104612104613104614104615104616104617104618104619104620104621104622104623104624104625104626104627104628104629104630104631104632104633104634104635104636104637104638104639104640104641104642104643104644104645104646104647104648104649104650104651104652104653104654104655104656104657104658104659104660104661104662104663104664104665104666104667104668104669104670104671104672104673104674104675104676104677104678104679104680104681104682104683104684104685104686104687104688104689104690104691104692104693104694104695104696104697104698104699104700104701104702104703104704104705104706104707104708104709104710104711104712104713104714104715104716104717104718104719104720104721104722104723104724104725104726104727104728104729104730104731104732104733104734104735104736104737104738104739104740104741104742104743104744104745104746104747104748104749104750104751104752104753104754104755104756104757104758104759104760104761104762104763104764104765104766104767104768104769104770104771104772104773104774104775104776104777104778104779104780104781104782104783104784104785104786104787104788104789104790104791104792104793104794104795104796104797104798104799104800104801104802104803104804104805104806104807104808104809104810104811104812104813104814104815104816104817104818104819104820104821104822104823104824104825104826104827104828104829104830104831104832104833104834104835104836104837104838104839104840104841104842104843104844104845104846104847104848104849104850104851104852104853104854104855104856104857104858104859104860104861104862104863104864104865104866104867104868104869104870104871104872104873104874104875104876104877104878104879104880104881104882104883104884104885104886104887104888104889104890104891104892104893104894104895104896104897104898104899104900104901104902104903104904104905104906104907104908104909104910104911104912104913104914104915104916104917104918104919104920104921104922104923104924104925104926104927104928104929104930104931104932104933104934104935104936104937104938104939104940104941104942104943104944104945104946104947104948104949104950104951104952104953104954104955104956104957104958104959104960104961104962104963104964104965104966104967104968104969104970104971104972104973104974104975104976104977104978104979104980104981104982104983104984104985104986104987104988104989104990104991104992104993104994104995104996104997104998104999105000105001105002105003105004105005105006105007105008105009105010105011105012105013105014105015105016105017105018105019105020105021105022105023105024105025105026105027105028105029105030105031105032105033105034105035105036105037105038105039105040105041105042105043105044105045105046105047105048105049105050105051105052105053105054105055105056105057105058105059105060105061105062105063105064105065105066105067105068105069105070105071105072105073105074105075105076105077105078105079105080105081105082105083105084105085105086105087105088105089105090105091105092105093105094105095105096105097105098105099105100105101105102105103105104105105105106105107105108105109105110105111105112105113105114105115105116105117105118105119105120105121105122105123105124105125105126105127105128105129105130105131105132105133105134105135105136105137105138105139105140105141105142105143105144105145105146105147105148105149105150105151105152105153105154105155105156105157105158105159105160105161105162105163105164105165105166105167105168105169105170105171105172105173105174105175105176105177105178105179105180105181105182105183105184105185105186105187105188105189105190105191105192105193105194105195105196105197105198105199105200105201105202105203105204105205105206105207105208105209105210105211105212105213105214105215105216105217105218105219105220105221105222105223105224105225105226105227105228105229105230105231105232105233105234105235105236105237105238105239105240105241105242105243105244105245105246105247105248105249105250105251105252105253105254105255105256105257105258105259105260105261105262105263105264105265105266105267105268105269105270105271105272105273105274105275105276105277105278105279105280105281105282105283105284105285105286105287105288105289105290105291105292105293105294105295105296105297105298105299105300105301105302105303105304105305105306105307105308105309105310105311105312105313105314105315105316105317105318105319105320105321105322105323105324105325105326105327105328105329105330105331105332105333105334105335105336105337105338105339105340105341105342105343105344105345105346105347105348105349105350105351105352105353105354105355105356105357105358105359105360105361105362105363105364105365105366105367105368105369105370105371105372105373105374105375105376105377105378105379105380105381105382105383105384105385105386105387105388105389105390105391105392105393105394105395105396105397105398105399105400105401105402105403105404105405105406105407105408105409105410105411105412105413105414105415105416105417105418105419105420105421105422105423105424105425105426105427105428105429105430105431105432105433105434105435105436105437105438105439105440105441105442105443105444105445105446105447105448105449105450105451105452105453105454105455105456105457105458105459105460105461105462105463105464105465105466105467105468105469105470105471105472105473105474105475105476105477105478105479105480105481105482105483105484105485105486105487105488105489105490105491105492105493105494105495105496105497105498105499105500105501105502105503105504105505105506105507105508105509105510105511105512105513105514105515105516105517105518105519105520105521105522105523105524105525105526105527105528105529105530105531105532105533105534105535105536105537105538105539105540105541105542105543105544105545105546105547105548105549105550105551105552105553105554105555105556105557105558105559105560105561105562105563105564105565105566105567105568105569105570105571105572105573105574105575105576105577105578105579105580105581105582105583105584105585105586105587105588105589105590105591105592105593105594105595105596105597105598105599105600105601105602105603105604105605105606105607105608105609105610105611105612105613105614105615105616105617105618105619105620105621105622105623105624105625105626105627105628105629105630105631105632105633105634105635105636105637105638105639105640105641105642105643105644105645105646105647105648105649105650105651105652105653105654105655105656105657105658105659105660105661105662105663105664105665105666105667105668105669105670105671105672105673105674105675105676105677105678105679105680105681105682105683105684105685105686105687105688105689105690105691105692105693105694105695105696105697105698105699105700105701105702105703105704105705105706105707105708105709105710105711105712105713105714105715105716105717105718105719105720105721105722105723105724105725105726105727105728105729105730105731105732105733105734105735105736105737105738105739105740105741105742105743105744105745105746105747105748105749105750105751105752105753105754105755105756105757105758105759105760105761105762105763105764105765105766105767105768105769105770105771105772105773105774105775105776105777105778105779105780105781105782105783105784105785105786105787105788105789105790105791105792105793105794105795105796105797105798105799105800105801105802105803105804105805105806105807105808105809105810105811105812105813105814105815105816105817105818105819105820105821105822105823105824105825105826105827105828105829105830105831105832105833105834105835105836105837105838105839105840105841105842105843105844105845105846105847105848105849105850105851105852105853105854105855105856105857105858105859105860105861105862105863105864105865105866105867105868105869105870105871105872105873105874105875105876105877105878105879105880105881105882105883105884105885105886105887105888105889105890105891105892105893105894105895105896105897105898105899105900105901105902105903105904105905105906105907105908105909105910105911105912105913105914105915105916105917105918105919105920105921105922105923105924105925105926105927105928105929105930105931105932105933105934105935105936105937105938105939105940105941105942105943105944105945105946105947105948105949105950105951105952105953105954105955105956105957105958105959105960105961105962105963105964105965105966105967105968105969105970105971105972105973105974105975105976105977105978105979105980105981105982105983105984105985105986105987105988105989105990105991105992105993105994105995105996105997105998105999106000106001106002106003106004106005106006106007106008106009106010106011106012106013106014106015106016106017106018106019106020106021106022106023106024106025106026106027106028106029106030106031106032106033106034106035106036106037106038106039106040106041106042106043106044106045106046106047106048106049106050106051106052106053106054106055106056106057106058106059106060106061106062106063106064106065106066106067106068106069106070106071106072106073106074106075106076106077106078106079106080106081106082106083106084106085106086106087106088106089106090106091106092106093106094106095106096106097106098106099106100106101106102106103106104106105106106106107106108106109106110106111106112106113106114106115106116106117106118106119106120106121106122106123106124106125106126106127106128106129106130106131106132106133106134106135106136106137106138106139106140106141106142106143106144106145106146106147106148106149106150106151106152106153106154106155106156106157106158106159106160106161106162106163106164106165106166106167106168106169106170106171106172106173106174106175106176106177106178106179106180106181106182106183106184106185106186106187106188106189106190106191106192106193106194106195106196106197106198106199106200106201106202106203106204106205106206106207106208106209106210106211106212106213106214106215106216106217106218106219106220106221106222106223106224106225106226106227106228106229106230106231106232106233106234106235106236106237106238106239106240106241106242106243106244106245106246106247106248106249106250106251106252106253106254106255106256106257106258106259106260106261106262106263106264106265106266106267106268106269106270106271106272106273106274106275106276106277106278106279106280106281106282106283106284106285106286106287106288106289106290106291106292106293106294106295106296106297106298106299106300106301106302106303106304106305106306106307106308106309106310106311106312106313106314106315106316106317106318106319106320106321106322106323106324106325106326106327106328106329106330106331106332106333106334106335106336106337106338106339106340106341106342106343106344106345106346106347106348106349106350106351106352106353106354106355106356106357106358106359106360106361106362106363106364106365106366106367106368106369106370106371106372106373106374106375106376106377106378106379106380106381106382106383106384106385106386106387106388106389106390106391106392106393106394106395106396106397106398106399106400106401106402106403106404106405106406106407106408106409106410106411106412106413106414106415106416106417106418106419106420106421106422106423106424106425106426106427106428106429106430106431106432106433106434106435106436106437106438106439106440106441106442106443106444106445106446106447106448106449106450106451106452106453106454106455106456106457106458106459106460106461106462106463106464106465106466106467106468106469106470106471106472106473106474106475106476106477106478106479106480106481106482106483106484106485106486106487106488106489106490106491106492106493106494106495106496106497106498106499106500106501106502106503106504106505106506106507106508106509106510106511106512106513106514106515106516106517106518106519106520106521106522106523106524106525106526106527106528106529106530106531106532106533106534106535106536106537106538106539106540106541106542106543106544106545106546106547106548106549106550106551106552106553106554106555106556106557106558106559106560106561106562106563106564106565106566106567106568106569106570106571106572106573106574106575106576106577106578106579106580106581106582106583106584106585106586106587106588106589106590106591106592106593106594106595106596106597106598106599106600106601106602106603106604106605106606106607106608106609106610106611106612106613106614106615106616106617106618106619106620106621106622106623106624106625106626106627106628106629106630106631106632106633106634106635106636106637106638106639106640106641106642106643106644106645106646106647106648106649106650106651106652106653106654106655106656106657106658106659106660106661106662106663106664106665106666106667106668106669106670106671106672106673106674106675106676106677106678106679106680106681106682106683106684106685106686106687106688106689106690106691106692106693106694106695106696106697106698106699106700106701106702106703106704106705106706106707106708106709106710106711106712106713106714106715106716106717106718106719106720106721106722106723106724106725106726106727106728106729106730106731106732106733106734106735106736106737106738106739106740106741106742106743106744106745106746106747106748106749106750106751106752106753106754106755106756106757106758106759106760106761106762106763106764106765106766106767106768106769106770106771106772106773106774106775106776106777106778106779106780106781106782106783106784106785106786106787106788106789106790106791106792106793106794106795106796106797106798106799106800106801106802106803106804106805106806106807106808106809106810106811106812106813106814106815106816106817106818106819106820106821106822106823106824106825106826106827106828106829106830106831106832106833106834106835106836106837106838106839106840106841106842106843106844106845106846106847106848106849106850106851106852106853106854106855106856106857106858106859106860106861106862106863106864106865106866106867106868106869106870106871106872106873106874106875106876106877106878106879106880106881106882106883106884106885106886106887106888106889106890106891106892106893106894106895106896106897106898106899106900106901106902106903106904106905106906106907106908106909106910106911106912106913106914106915106916106917106918106919106920106921106922106923106924106925106926106927106928106929106930106931106932106933106934106935106936106937106938106939106940106941106942106943106944106945106946106947106948106949106950106951106952106953106954106955106956106957106958106959106960106961106962106963106964106965106966106967106968106969106970106971106972106973106974106975106976106977106978106979106980106981106982106983106984106985106986106987106988106989106990106991106992106993106994106995106996106997106998106999107000107001107002107003107004107005107006107007107008107009107010107011107012107013107014107015107016107017107018107019107020107021107022107023107024107025107026107027107028107029107030107031107032107033107034107035107036107037107038107039107040107041107042107043107044107045107046107047107048107049107050107051107052107053107054107055107056107057107058107059107060107061107062107063107064107065107066107067107068107069107070107071107072107073107074107075107076107077107078107079107080107081107082107083107084107085107086107087107088107089107090107091107092107093107094107095107096107097107098107099107100107101107102107103107104107105107106107107107108107109107110107111107112107113107114107115107116107117107118107119107120107121107122107123107124107125107126107127107128107129107130107131107132107133107134107135107136107137107138107139107140107141107142107143107144107145107146107147107148107149107150107151107152107153107154107155107156107157107158107159107160107161107162107163107164107165107166107167107168107169107170107171107172107173107174107175107176107177107178107179107180107181107182107183107184107185107186107187107188107189107190107191107192107193107194107195107196107197107198107199107200107201107202107203107204107205107206107207107208107209107210107211107212107213107214107215107216107217107218107219107220107221107222107223107224107225107226107227107228107229107230107231107232107233107234107235107236107237107238107239107240107241107242107243107244107245107246107247107248107249107250107251107252107253107254107255107256107257107258107259107260107261107262107263107264107265107266107267107268107269107270107271107272107273107274107275107276107277107278107279107280107281107282107283107284107285107286107287107288107289107290107291107292107293107294107295107296107297107298107299107300107301107302107303107304107305107306107307107308107309107310107311107312107313107314107315107316107317107318107319107320107321107322107323107324107325107326107327107328107329107330107331107332107333107334107335107336107337107338107339107340107341107342107343107344107345107346107347107348107349107350107351107352107353107354107355107356107357107358107359107360107361107362107363107364107365107366107367107368107369107370107371107372107373107374107375107376107377107378107379107380107381107382107383107384107385107386107387107388107389107390107391107392107393107394107395107396107397107398107399107400107401107402107403107404107405107406107407107408107409107410107411107412107413107414107415107416107417107418107419107420107421107422107423107424107425107426107427107428107429107430107431107432107433107434107435107436107437107438107439107440107441107442107443107444107445107446107447107448107449107450107451107452107453107454107455107456107457107458107459107460107461107462107463107464107465107466107467107468107469107470107471107472107473107474107475107476107477107478107479107480107481107482107483107484107485107486107487107488107489107490107491107492107493107494107495107496107497107498107499107500107501107502107503107504107505107506107507107508107509107510107511107512107513107514107515107516107517107518107519107520107521107522107523107524107525107526107527107528107529107530107531107532107533107534107535107536107537107538107539107540107541107542107543107544107545107546107547107548107549107550107551107552107553107554107555107556107557107558107559107560107561107562107563107564107565107566107567107568107569107570107571107572107573107574107575107576107577107578107579107580107581107582107583107584107585107586107587107588107589107590107591107592107593107594107595107596107597107598107599107600107601107602107603107604107605107606107607107608107609107610107611107612107613107614107615107616107617107618107619107620107621107622107623107624107625107626107627107628107629107630107631107632107633107634107635107636107637107638107639107640107641107642107643107644107645107646107647107648107649107650107651107652107653107654107655107656107657107658107659107660107661107662107663107664107665107666107667107668107669107670107671107672107673107674107675107676107677107678107679107680107681107682107683107684107685107686107687107688107689107690107691107692107693107694107695107696107697107698107699107700107701107702107703107704107705107706107707107708107709107710107711107712107713107714107715107716107717107718107719107720107721107722107723107724107725107726107727107728107729107730107731107732107733107734107735107736107737107738107739107740107741107742107743107744107745107746107747107748107749107750107751107752107753107754107755107756107757107758107759107760107761107762107763107764107765107766107767107768107769107770107771107772107773107774107775107776107777107778107779107780107781107782107783107784107785107786107787107788107789107790107791107792107793107794107795107796107797107798107799107800107801107802107803107804107805107806107807107808107809107810107811107812107813107814107815107816107817107818107819107820107821107822107823107824107825107826107827107828107829107830107831107832107833107834107835107836107837107838107839107840107841107842107843107844107845107846107847107848107849107850107851107852107853107854107855107856107857107858107859107860107861107862107863107864107865107866107867107868107869107870107871107872107873107874107875107876107877107878107879107880107881107882107883107884107885107886107887107888107889107890107891107892107893107894107895107896107897107898107899107900107901107902107903107904107905107906107907107908107909107910107911107912107913107914107915107916107917107918107919107920107921107922107923107924107925107926107927107928107929107930107931107932107933107934107935107936107937107938107939107940107941107942107943107944107945107946107947107948107949107950107951107952107953107954107955107956107957107958107959107960107961107962107963107964107965107966107967107968107969107970107971107972107973107974107975107976107977107978107979107980107981107982107983107984107985107986107987107988107989107990107991107992107993107994107995107996107997107998107999108000108001108002108003108004108005108006108007108008108009108010108011108012108013108014108015108016108017108018108019108020108021108022108023108024108025108026108027108028108029108030108031108032108033108034108035108036108037108038108039108040108041108042108043108044108045108046108047108048108049108050108051108052108053108054108055108056108057108058108059108060108061108062108063108064108065108066108067108068108069108070108071108072108073108074108075108076108077108078108079108080108081108082108083108084108085108086108087108088108089108090108091108092108093108094108095108096108097108098108099108100108101108102108103108104108105108106108107108108108109108110108111108112108113108114108115108116108117108118108119108120108121108122108123108124108125108126108127108128108129108130108131108132108133108134108135108136108137108138108139108140108141108142108143108144108145108146108147108148108149108150108151108152108153108154108155108156108157108158108159108160108161108162108163108164108165108166108167108168108169108170108171108172108173108174108175108176108177108178108179108180108181108182108183108184108185108186108187108188108189108190108191108192108193108194108195108196108197108198108199108200108201108202108203108204108205108206108207108208108209108210108211108212108213108214108215108216108217108218108219108220108221108222108223108224108225108226108227108228108229108230108231108232108233108234108235108236108237108238108239108240108241108242108243108244108245108246108247108248108249108250108251108252108253108254108255108256108257108258108259108260108261108262108263108264108265108266108267108268108269108270108271108272108273108274108275108276108277108278108279108280108281108282108283108284108285108286108287108288108289108290108291108292108293108294108295108296108297108298108299108300108301108302108303108304108305108306108307108308108309108310108311108312108313108314108315108316108317108318108319108320108321108322108323108324108325108326108327108328108329108330108331108332108333108334108335108336108337108338108339108340108341108342108343108344108345108346108347108348108349108350108351108352108353108354108355108356108357108358108359108360108361108362108363108364108365108366108367108368108369108370108371108372108373108374108375108376108377108378108379108380108381108382108383108384108385108386108387108388108389108390108391108392108393108394108395108396108397108398108399108400108401108402108403108404108405108406108407108408108409108410108411108412108413108414108415108416108417108418108419108420108421108422108423108424108425108426108427108428108429108430108431108432108433108434108435108436108437108438108439108440108441108442108443108444108445108446108447108448108449108450108451108452108453108454108455108456108457108458108459108460108461108462108463108464108465108466108467108468108469108470108471108472108473108474108475108476108477108478108479108480108481108482108483108484108485108486108487108488108489108490108491108492108493108494108495108496108497108498108499108500108501108502108503108504108505108506108507108508108509108510108511108512108513108514108515108516108517108518108519108520108521108522108523108524108525108526108527108528108529108530108531108532108533108534108535108536108537108538108539108540108541108542108543108544108545108546108547108548108549108550108551108552108553108554108555108556108557108558108559108560108561108562108563108564108565108566108567108568108569108570108571108572108573108574108575108576108577108578108579108580108581108582108583108584108585108586108587108588108589108590108591108592108593108594108595108596108597108598108599108600108601108602108603108604108605108606108607108608108609108610108611108612108613108614108615108616108617108618108619108620108621108622108623108624108625108626108627108628108629108630108631108632108633108634108635108636108637108638108639108640108641108642108643108644108645108646108647108648108649108650108651108652108653108654108655108656108657108658108659108660108661108662108663108664108665108666108667108668108669108670108671108672108673108674108675108676108677108678108679108680108681108682108683108684108685108686108687108688108689108690108691108692108693108694108695108696108697108698108699108700108701108702108703108704108705108706108707108708108709108710108711108712108713108714108715108716108717108718108719108720108721108722108723108724108725108726108727108728108729108730108731108732108733108734108735108736108737108738108739108740108741108742108743108744108745108746108747108748108749108750108751108752108753108754108755108756108757108758108759108760108761108762108763108764108765108766108767108768108769108770108771108772108773108774108775108776108777108778108779108780108781108782108783108784108785108786108787108788108789108790108791108792108793108794108795108796108797108798108799108800108801108802108803108804108805108806108807108808108809108810108811108812108813108814108815108816108817108818108819108820108821108822108823108824108825108826108827108828108829108830108831108832108833108834108835108836108837108838108839108840108841108842108843108844108845108846108847108848108849108850108851108852108853108854108855108856108857108858108859108860108861108862108863108864108865108866108867108868108869108870108871108872108873108874108875108876108877108878108879108880108881108882108883108884108885108886108887108888108889108890108891108892108893108894108895108896108897108898108899108900108901108902108903108904108905108906108907108908108909108910108911108912108913108914108915108916108917108918108919108920108921108922108923108924108925108926108927108928108929108930108931108932108933108934108935108936108937108938108939108940108941108942108943108944108945108946108947108948108949108950108951108952108953108954108955108956108957108958108959108960108961108962108963108964108965108966108967108968108969108970108971108972108973108974108975108976108977108978108979108980108981108982108983108984108985108986108987108988108989108990108991108992108993108994108995108996108997108998108999109000109001109002109003109004109005109006109007109008109009109010109011109012109013109014109015109016109017109018109019109020109021109022109023109024109025109026109027109028109029109030109031109032109033109034109035109036109037109038109039109040109041109042109043109044109045109046109047109048109049109050109051109052109053109054109055109056109057109058109059109060109061109062109063109064109065109066109067109068109069109070109071109072109073109074109075109076109077109078109079109080109081109082109083109084109085109086109087109088109089109090109091109092109093109094109095109096109097109098109099109100109101109102109103109104109105109106109107109108109109109110109111109112109113109114109115109116109117109118109119109120109121109122109123109124109125109126109127109128109129109130109131109132109133109134109135109136109137109138109139109140109141109142109143109144109145109146109147109148109149109150109151109152109153109154109155109156109157109158109159109160109161109162109163109164109165109166109167109168109169109170109171109172109173109174109175109176109177109178109179109180109181109182109183109184109185109186109187109188109189109190109191109192109193109194109195109196109197109198109199109200109201109202109203109204109205109206109207109208109209109210109211109212109213109214109215109216109217109218109219109220109221109222109223109224109225109226109227109228109229109230109231109232109233109234109235109236109237109238109239109240109241109242109243109244109245109246109247109248109249109250109251109252109253109254109255109256109257109258109259109260109261109262109263109264109265109266109267109268109269109270109271109272109273109274109275109276109277109278109279109280109281109282109283109284109285109286109287109288109289109290109291109292109293109294109295109296109297109298109299109300109301109302109303109304109305109306109307109308109309109310109311109312109313109314109315109316109317109318109319109320109321109322109323109324109325109326109327109328109329109330109331109332109333109334109335109336109337109338109339109340109341109342109343109344109345109346109347109348109349109350109351109352109353109354109355109356109357109358109359109360109361109362109363109364109365109366109367109368109369109370109371109372109373109374109375109376109377109378109379109380109381109382109383109384109385109386109387109388109389109390109391109392109393109394109395109396109397109398109399109400109401109402109403109404109405109406109407109408109409109410109411109412109413109414109415109416109417109418109419109420109421109422109423109424109425109426109427109428109429109430109431109432109433109434109435109436109437109438109439109440109441109442109443109444109445109446109447109448109449109450109451109452109453109454109455109456109457109458109459109460109461109462109463109464109465109466109467109468109469109470109471109472109473109474109475109476109477109478109479109480109481109482109483109484109485109486109487109488109489109490109491109492109493109494109495109496109497109498109499109500109501109502109503109504109505109506109507109508109509109510109511109512109513109514109515109516109517109518109519109520109521109522109523109524109525109526109527109528109529109530109531109532109533109534109535109536109537109538109539109540109541109542109543109544109545109546109547109548109549109550109551109552109553109554109555109556109557109558109559109560109561109562109563109564109565109566109567109568109569109570109571109572109573109574109575109576109577109578109579109580109581109582109583109584109585109586109587109588109589109590109591109592109593109594109595109596109597109598109599109600109601109602109603109604109605109606109607109608109609109610109611109612109613109614109615109616109617109618109619109620109621109622109623109624109625109626109627109628109629109630109631109632109633109634109635109636109637109638109639109640109641109642109643109644109645109646109647109648109649109650109651109652109653109654109655109656109657109658109659109660109661109662109663109664109665109666109667109668109669109670109671109672109673109674109675109676109677109678109679109680109681109682109683109684109685109686109687109688109689109690109691109692109693109694109695109696109697109698109699109700109701109702109703109704109705109706109707109708109709109710109711109712109713109714109715109716109717109718109719109720109721109722109723109724109725109726109727109728109729109730109731109732109733109734109735109736109737109738109739109740109741109742109743109744109745109746109747109748109749109750109751109752109753109754109755109756109757109758109759109760109761109762109763109764109765109766109767109768109769109770109771109772109773109774109775109776109777109778109779109780109781109782109783109784109785109786109787109788109789109790109791109792109793109794109795109796109797109798109799109800109801109802109803109804109805109806109807109808109809109810109811109812109813109814109815109816109817109818109819109820109821109822109823109824109825109826109827109828109829109830109831109832109833109834109835109836109837109838109839109840109841109842109843109844109845109846109847109848109849109850109851109852109853109854109855109856109857109858109859109860109861109862109863109864109865109866109867109868109869109870109871109872109873109874109875109876109877109878109879109880109881109882109883109884109885109886109887109888109889109890109891109892109893109894109895109896109897109898109899109900109901109902109903109904109905109906109907109908109909109910109911109912109913109914109915109916109917109918109919109920109921109922109923109924109925109926109927109928109929109930109931109932109933109934109935109936109937109938109939109940109941109942109943109944109945109946109947109948109949109950109951109952109953109954109955109956109957109958109959109960109961109962109963109964109965109966109967109968109969109970109971109972109973109974109975109976109977109978109979109980109981109982109983109984109985109986109987109988109989109990109991109992109993109994109995109996109997109998109999110000110001110002110003110004110005110006110007110008110009110010110011110012110013110014110015110016110017110018110019110020110021110022110023110024110025110026110027110028110029110030110031110032110033110034110035110036110037110038110039110040110041110042110043110044110045110046110047110048110049110050110051110052110053110054110055110056110057110058110059110060110061110062110063110064110065110066110067110068110069110070110071110072110073110074110075110076110077110078110079110080110081110082110083110084110085110086110087110088110089110090110091110092110093110094110095110096110097110098110099110100110101110102110103110104110105110106110107110108110109110110110111110112110113110114110115110116110117110118110119110120110121110122110123110124110125110126110127110128110129110130110131110132110133110134110135110136110137110138110139110140110141110142110143110144110145110146110147110148110149110150110151110152110153110154110155110156110157110158110159110160110161110162110163110164110165110166110167110168110169110170110171110172110173110174110175110176110177110178110179110180110181110182110183110184110185110186110187110188110189110190110191110192110193110194110195110196110197110198110199110200110201110202110203110204110205110206110207110208110209110210110211110212110213110214110215110216110217110218110219110220110221110222110223110224110225110226110227110228110229110230110231110232110233110234110235110236110237110238110239110240110241110242110243110244110245110246110247110248110249110250110251110252110253110254110255110256110257110258110259110260110261110262110263110264110265110266110267110268110269110270110271110272110273110274110275110276110277110278110279110280110281110282110283110284110285110286110287110288110289110290110291110292110293110294110295110296110297110298110299110300110301110302110303110304110305110306110307110308110309110310110311110312110313110314110315110316110317110318110319110320110321110322110323110324110325110326110327110328110329110330110331110332110333110334110335110336110337110338110339110340110341110342110343110344110345110346110347110348110349110350110351110352110353110354110355110356110357110358110359110360110361110362110363110364110365110366110367110368110369110370110371110372110373110374110375110376110377110378110379110380110381110382110383110384110385110386110387110388110389110390110391110392110393110394110395110396110397110398110399110400110401110402110403110404110405110406110407110408110409110410110411110412110413110414110415110416110417110418110419110420110421110422110423110424110425110426110427110428110429110430110431110432110433110434110435110436110437110438110439110440110441110442110443110444110445110446110447110448110449110450110451110452110453110454110455110456110457110458110459110460110461110462110463110464110465110466110467110468110469110470110471110472110473110474110475110476110477110478110479110480110481110482110483110484110485110486110487110488110489110490110491110492110493110494110495110496110497110498110499110500110501110502110503110504110505110506110507110508110509110510110511110512110513110514110515110516110517110518110519110520110521110522110523110524110525110526110527110528110529110530110531110532110533110534110535110536110537110538110539110540110541110542110543110544110545110546110547110548110549110550110551110552110553110554110555110556110557110558110559110560110561110562110563110564110565110566110567110568110569110570110571110572110573110574110575110576110577110578110579110580110581110582110583110584110585110586110587110588110589110590110591110592110593110594110595110596110597110598110599110600110601110602110603110604110605110606110607110608110609110610110611110612110613110614110615110616110617110618110619110620110621110622110623110624110625110626110627110628110629110630110631110632110633110634110635110636110637110638110639110640110641110642110643110644110645110646110647110648110649110650110651110652110653110654110655110656110657110658110659110660110661110662110663110664110665110666110667110668110669110670110671110672110673110674110675110676110677110678110679110680110681110682110683110684110685110686110687110688110689110690110691110692110693110694110695110696110697110698110699110700110701110702110703110704110705110706110707110708110709110710110711110712110713110714110715110716110717110718110719110720110721110722110723110724110725110726110727110728110729110730110731110732110733110734110735110736110737110738110739110740110741110742110743110744110745110746110747110748110749110750110751110752110753110754110755110756110757110758110759110760110761110762110763110764110765110766110767110768110769110770110771110772110773110774110775110776110777110778110779110780110781110782110783110784110785110786110787110788110789110790110791110792110793110794110795110796110797110798110799110800110801110802110803110804110805110806110807110808110809110810110811110812110813110814110815110816110817110818110819110820110821110822110823110824110825110826110827110828110829110830110831110832110833110834110835110836110837110838110839110840110841110842110843110844110845110846110847110848110849110850110851110852110853110854110855110856110857110858110859110860110861110862110863110864110865110866110867110868110869110870110871110872110873110874110875110876110877110878110879110880110881110882110883110884110885110886110887110888110889110890110891110892110893110894110895110896110897110898110899110900110901110902110903110904110905110906110907110908110909110910110911110912110913110914110915110916110917110918110919110920110921110922110923110924110925110926110927110928110929110930110931110932110933110934110935110936110937110938110939110940110941110942110943110944110945110946110947110948110949110950110951110952110953110954110955110956110957110958110959110960110961110962110963110964110965110966110967110968110969110970110971110972110973110974110975110976110977110978110979110980110981110982110983110984110985110986110987110988110989110990110991110992110993110994110995110996110997110998110999111000111001111002111003111004111005111006111007111008111009111010111011111012111013111014111015111016111017111018111019111020111021111022111023111024111025111026111027111028111029111030111031111032111033111034111035111036111037111038111039111040111041111042111043111044111045111046111047111048111049111050111051111052111053111054111055111056111057111058111059111060111061111062111063111064111065111066111067111068111069111070111071111072111073111074111075111076111077111078111079111080111081111082111083111084111085111086111087111088111089111090111091111092111093111094111095111096111097111098111099111100111101111102111103111104111105111106111107111108111109111110111111111112111113111114111115111116111117111118111119111120111121111122111123111124111125111126111127111128111129111130111131111132111133111134111135111136111137111138111139111140111141111142111143111144111145111146111147111148111149111150111151111152111153111154111155111156111157111158111159111160111161111162111163111164111165111166111167111168111169111170111171111172111173111174111175111176111177111178111179111180111181111182111183111184111185111186111187111188111189111190111191111192111193111194111195111196111197111198111199111200111201111202111203111204111205111206111207111208111209111210111211111212111213111214111215111216111217111218111219111220111221111222111223111224111225111226111227111228111229111230111231111232111233111234111235111236111237111238111239111240111241111242111243111244111245111246111247111248111249111250111251111252111253111254111255111256111257111258111259111260111261111262111263111264111265111266111267111268111269111270111271111272111273111274111275111276111277111278111279111280111281111282111283111284111285111286111287111288111289111290111291111292111293111294111295111296111297111298111299111300111301111302111303111304111305111306111307111308111309111310111311111312111313111314111315111316111317111318111319111320111321111322111323111324111325111326111327111328111329111330111331111332111333111334111335111336111337111338111339111340111341111342111343111344111345111346111347111348111349111350111351111352111353111354111355111356111357111358111359111360111361111362111363111364111365111366111367111368111369111370111371111372111373111374111375111376111377111378111379111380111381111382111383111384111385111386111387111388111389111390111391111392111393111394111395111396111397111398111399111400111401111402111403111404111405111406111407111408111409111410111411111412111413111414111415111416111417111418111419111420111421111422111423111424111425111426111427111428111429111430111431111432111433111434111435111436111437111438111439111440111441111442111443111444111445111446111447111448111449111450111451111452111453111454111455111456111457111458111459111460111461111462111463111464111465111466111467111468111469111470111471111472111473111474111475111476111477111478111479111480111481111482111483111484111485111486111487111488111489111490111491111492111493111494111495111496111497111498111499111500111501111502111503111504111505111506111507111508111509111510111511111512111513111514111515111516111517111518111519111520111521111522111523111524111525111526111527111528111529111530111531111532111533111534111535111536111537111538111539111540111541111542111543111544111545111546111547111548111549111550111551111552111553111554111555111556111557111558111559111560111561111562111563111564111565111566111567111568111569111570111571111572111573111574111575111576111577111578111579111580111581111582111583111584111585111586111587111588111589111590111591111592111593111594111595111596111597111598111599111600111601111602111603111604111605111606111607111608111609111610111611111612111613111614111615111616111617111618111619111620111621111622111623111624111625111626111627111628111629111630111631111632111633111634111635111636111637111638111639111640111641111642111643111644111645111646111647111648111649111650111651111652111653111654111655111656111657111658111659111660111661111662111663111664111665111666111667111668111669111670111671111672111673111674111675111676111677111678111679111680111681111682111683111684111685111686111687111688111689111690111691111692111693111694111695111696111697111698111699111700111701111702111703111704111705111706111707111708111709111710111711111712111713111714111715111716111717111718111719111720111721111722111723111724111725111726111727111728111729111730111731111732111733111734111735111736111737111738111739111740111741111742111743111744111745111746111747111748111749111750111751111752111753111754111755111756111757111758111759111760111761111762111763111764111765111766111767111768111769111770111771111772111773111774111775111776111777111778111779111780111781111782111783111784111785111786111787111788111789111790111791111792111793111794111795111796111797111798111799111800111801111802111803111804111805111806111807111808111809111810111811111812111813111814111815111816111817111818111819111820111821111822111823111824111825111826111827111828111829111830111831111832111833111834111835111836111837111838111839111840111841111842111843111844111845111846111847111848111849111850111851111852111853111854111855111856111857111858111859111860111861111862111863111864111865111866111867111868111869111870111871111872111873111874111875111876111877111878111879111880111881111882111883111884111885111886111887111888111889111890111891111892111893111894111895111896111897111898111899111900111901111902111903111904111905111906111907111908111909111910111911111912111913111914111915111916111917111918111919111920111921111922111923111924111925111926111927111928111929111930111931111932111933111934111935111936111937111938111939111940111941111942111943111944111945111946111947111948111949111950111951111952111953111954111955111956111957111958111959111960111961111962111963111964111965111966111967111968111969111970111971111972111973111974111975111976111977111978111979111980111981111982111983111984111985111986111987111988111989111990111991111992111993111994111995111996111997111998111999112000112001112002112003112004112005112006112007112008112009112010112011112012112013112014112015112016112017112018112019112020112021112022112023112024112025112026112027112028112029112030112031112032112033112034112035112036112037112038112039112040112041112042112043112044112045112046112047112048112049112050112051112052112053112054112055112056112057112058112059112060112061112062112063112064112065112066112067112068112069112070112071112072112073112074112075112076112077112078112079112080112081112082112083112084112085112086112087112088112089112090112091112092112093112094112095112096112097112098112099112100112101112102112103112104112105112106112107112108112109112110112111112112112113112114112115112116112117112118112119112120112121112122112123112124112125112126112127112128112129112130112131112132112133112134112135112136112137112138112139112140112141112142112143112144112145112146112147112148112149112150112151112152112153112154112155112156112157112158112159112160112161112162112163112164112165112166112167112168112169112170112171112172112173112174112175112176112177112178112179112180112181112182112183112184112185112186112187112188112189112190112191112192112193112194112195112196112197112198112199112200112201112202112203112204112205112206112207112208112209112210112211112212112213112214112215112216112217112218112219112220112221112222112223112224112225112226112227112228112229112230112231112232112233112234112235112236112237112238112239112240112241112242112243112244112245112246112247112248112249112250112251112252112253112254112255112256112257112258112259112260112261112262112263112264112265112266112267112268112269112270112271112272112273112274112275112276112277112278112279112280112281112282112283112284112285112286112287112288112289112290112291112292112293112294112295112296112297112298112299112300112301112302112303112304112305112306112307112308112309112310112311112312112313112314112315112316112317112318112319112320112321112322112323112324112325112326112327112328112329112330112331112332112333112334112335112336112337112338112339112340112341112342112343112344112345112346112347112348112349112350112351112352112353112354112355112356112357112358112359112360112361112362112363112364112365112366112367112368112369112370112371112372112373112374112375112376112377112378112379112380112381112382112383112384112385112386112387112388112389112390112391112392112393112394112395112396112397112398112399112400112401112402112403112404112405112406112407112408112409112410112411112412112413112414112415112416112417112418112419112420112421112422112423112424112425112426112427112428112429112430112431112432112433112434112435112436112437112438112439112440112441112442112443112444112445112446112447112448112449112450112451112452112453112454112455112456112457112458112459112460112461112462112463112464112465112466112467112468112469112470112471112472112473112474112475112476112477112478112479112480112481112482112483112484112485112486112487112488112489112490112491112492112493112494112495112496112497112498112499112500112501112502112503112504112505112506112507112508112509112510112511112512112513112514112515112516112517112518112519112520112521112522112523112524112525112526112527112528112529112530112531112532112533112534112535112536112537112538112539112540112541112542112543112544112545112546112547112548112549112550112551112552112553112554112555112556112557112558112559112560112561112562112563112564112565112566112567112568112569112570112571112572112573112574112575112576112577112578112579112580112581112582112583112584112585112586112587112588112589112590112591112592112593112594112595112596112597112598112599112600112601112602112603112604112605112606112607112608112609112610112611112612112613112614112615112616112617112618112619112620112621112622112623112624112625112626112627112628112629112630112631112632112633112634112635112636112637112638112639112640112641112642112643112644112645112646112647112648112649112650112651112652112653112654112655112656112657112658112659112660112661112662112663112664112665112666112667112668112669112670112671112672112673112674112675112676112677112678112679112680112681112682112683112684112685112686112687112688112689112690112691112692112693112694112695112696112697112698112699112700112701112702112703112704112705112706112707112708112709112710112711112712112713112714112715112716112717112718112719112720112721112722112723112724112725112726112727112728112729112730112731112732112733112734112735112736112737112738112739112740112741112742112743112744112745112746112747112748112749112750112751112752112753112754112755112756112757112758112759112760112761112762112763112764112765112766112767112768112769112770112771112772112773112774112775112776112777112778112779112780112781112782112783112784112785112786112787112788112789112790112791112792112793112794112795112796112797112798112799112800112801112802112803112804112805112806112807112808112809112810112811112812112813112814112815112816112817112818112819112820112821112822112823112824112825112826112827112828112829112830112831112832112833112834112835112836112837112838112839112840112841112842112843112844112845112846112847112848112849112850112851112852112853112854112855112856112857112858112859112860112861112862112863112864112865112866112867112868112869112870112871112872112873112874112875112876112877112878112879112880112881112882112883112884112885112886112887112888112889112890112891112892112893112894112895112896112897112898112899112900112901112902112903112904112905112906112907112908112909112910112911112912112913112914112915112916112917112918112919112920112921112922112923112924112925112926112927112928112929112930112931112932112933112934112935112936112937112938112939112940112941112942112943112944112945112946112947112948112949112950112951112952112953112954112955112956112957112958112959112960112961112962112963112964112965112966112967112968112969112970112971112972112973112974112975112976112977112978112979112980112981112982112983112984112985112986112987112988112989112990112991112992112993112994112995112996112997112998112999113000113001113002113003113004113005113006113007113008113009113010113011113012113013113014113015113016113017113018113019113020113021113022113023113024113025113026113027113028113029113030113031113032113033113034113035113036113037113038113039113040113041113042113043113044113045113046113047113048113049113050113051113052113053113054113055113056113057113058113059113060113061113062113063113064113065113066113067113068113069113070113071113072113073113074113075113076113077113078113079113080113081113082113083113084113085113086113087113088113089113090113091113092113093113094113095113096113097113098113099113100113101113102113103113104113105113106113107113108113109113110113111113112113113113114113115113116113117113118113119113120113121113122113123113124113125113126113127113128113129113130113131113132113133113134113135113136113137113138113139113140113141113142113143113144113145113146113147113148113149113150113151113152113153113154113155113156113157113158113159113160113161113162113163113164113165113166113167113168113169113170113171113172113173113174113175113176113177113178113179113180113181113182113183113184113185113186113187113188113189113190113191113192113193113194113195113196113197113198113199113200113201113202113203113204113205113206113207113208113209113210113211113212113213113214113215113216113217113218113219113220113221113222113223113224113225113226113227113228113229113230113231113232113233113234113235113236113237113238113239113240113241113242113243113244113245113246113247113248113249113250113251113252113253113254113255113256113257113258113259113260113261113262113263113264113265113266113267113268113269113270113271113272113273113274113275113276113277113278113279113280113281113282113283113284113285113286113287113288113289113290113291113292113293113294113295113296113297113298113299113300113301113302113303113304113305113306113307113308113309113310113311113312113313113314113315113316113317113318113319113320113321113322113323113324113325113326113327113328113329113330113331113332113333113334113335113336113337113338113339113340113341113342113343113344113345113346113347113348113349113350113351113352113353113354113355113356113357113358113359113360113361113362113363113364113365113366113367113368113369113370113371113372113373113374113375113376113377113378113379113380113381113382113383113384113385113386113387113388113389113390113391113392113393113394113395113396113397113398113399113400113401113402113403113404113405113406113407113408113409113410113411113412113413113414113415113416113417113418113419113420113421113422113423113424113425113426113427113428113429113430113431113432113433113434113435113436113437113438113439113440113441113442113443113444113445113446113447113448113449113450113451113452113453113454113455113456113457113458113459113460113461113462113463113464113465113466113467113468113469113470113471113472113473113474113475113476113477113478113479113480113481113482113483113484113485113486113487113488113489113490113491113492113493113494113495113496113497113498113499113500113501113502113503113504113505113506113507113508113509113510113511113512113513113514113515113516113517113518113519113520113521113522113523113524113525113526113527113528113529113530113531113532113533113534113535113536113537113538113539113540113541113542113543113544113545113546113547113548113549113550113551113552113553113554113555113556113557113558113559113560113561113562113563113564113565113566113567113568113569113570113571113572113573113574113575113576113577113578113579113580113581113582113583113584113585113586113587113588113589113590113591113592113593113594113595113596113597113598113599113600113601113602113603113604113605113606113607113608113609113610113611113612113613113614113615113616113617113618113619113620113621113622113623113624113625113626113627113628113629113630113631113632113633113634113635113636113637113638113639113640113641113642113643113644113645113646113647113648113649113650113651113652113653113654113655113656113657113658113659113660113661113662113663113664113665113666113667113668113669113670113671113672113673113674113675113676113677113678113679113680113681113682113683113684113685113686113687113688113689113690113691113692113693113694113695113696113697113698113699113700113701113702113703
  1. diff -Nur linux-3.12.33/arch/arm/configs/bcmrpi_cutdown_defconfig linux-3.12.33-rpi/arch/arm/configs/bcmrpi_cutdown_defconfig
  2. --- linux-3.12.33/arch/arm/configs/bcmrpi_cutdown_defconfig 1969-12-31 18:00:00.000000000 -0600
  3. +++ linux-3.12.33-rpi/arch/arm/configs/bcmrpi_cutdown_defconfig 2014-12-03 19:13:32.260418001 -0600
  4. @@ -0,0 +1,463 @@
  5. +# CONFIG_LOCALVERSION_AUTO is not set
  6. +CONFIG_SYSVIPC=y
  7. +CONFIG_POSIX_MQUEUE=y
  8. +CONFIG_NO_HZ=y
  9. +CONFIG_HIGH_RES_TIMERS=y
  10. +CONFIG_IKCONFIG=y
  11. +CONFIG_IKCONFIG_PROC=y
  12. +# CONFIG_UID16 is not set
  13. +# CONFIG_KALLSYMS is not set
  14. +CONFIG_EMBEDDED=y
  15. +# CONFIG_VM_EVENT_COUNTERS is not set
  16. +# CONFIG_COMPAT_BRK is not set
  17. +CONFIG_SLAB=y
  18. +CONFIG_MODULES=y
  19. +CONFIG_MODULE_UNLOAD=y
  20. +CONFIG_MODVERSIONS=y
  21. +CONFIG_MODULE_SRCVERSION_ALL=y
  22. +# CONFIG_BLK_DEV_BSG is not set
  23. +CONFIG_PARTITION_ADVANCED=y
  24. +CONFIG_MAC_PARTITION=y
  25. +CONFIG_ARCH_BCM2708=y
  26. +CONFIG_AEABI=y
  27. +CONFIG_ZBOOT_ROM_TEXT=0x0
  28. +CONFIG_ZBOOT_ROM_BSS=0x0
  29. +CONFIG_CMDLINE="dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext3 rootwait"
  30. +CONFIG_CPU_IDLE=y
  31. +CONFIG_VFP=y
  32. +CONFIG_BINFMT_MISC=m
  33. +CONFIG_NET=y
  34. +CONFIG_PACKET=y
  35. +CONFIG_UNIX=y
  36. +CONFIG_XFRM_USER=y
  37. +CONFIG_NET_KEY=m
  38. +CONFIG_INET=y
  39. +CONFIG_IP_MULTICAST=y
  40. +CONFIG_IP_PNP=y
  41. +CONFIG_IP_PNP_DHCP=y
  42. +CONFIG_IP_PNP_RARP=y
  43. +CONFIG_SYN_COOKIES=y
  44. +# CONFIG_INET_XFRM_MODE_TRANSPORT is not set
  45. +# CONFIG_INET_XFRM_MODE_TUNNEL is not set
  46. +# CONFIG_INET_XFRM_MODE_BEET is not set
  47. +# CONFIG_INET_LRO is not set
  48. +# CONFIG_INET_DIAG is not set
  49. +# CONFIG_IPV6 is not set
  50. +CONFIG_NET_PKTGEN=m
  51. +CONFIG_IRDA=m
  52. +CONFIG_IRLAN=m
  53. +CONFIG_IRCOMM=m
  54. +CONFIG_IRDA_ULTRA=y
  55. +CONFIG_IRDA_CACHE_LAST_LSAP=y
  56. +CONFIG_IRDA_FAST_RR=y
  57. +CONFIG_IRTTY_SIR=m
  58. +CONFIG_KINGSUN_DONGLE=m
  59. +CONFIG_KSDAZZLE_DONGLE=m
  60. +CONFIG_KS959_DONGLE=m
  61. +CONFIG_USB_IRDA=m
  62. +CONFIG_SIGMATEL_FIR=m
  63. +CONFIG_MCS_FIR=m
  64. +CONFIG_BT=m
  65. +CONFIG_BT_RFCOMM=m
  66. +CONFIG_BT_RFCOMM_TTY=y
  67. +CONFIG_BT_BNEP=m
  68. +CONFIG_BT_BNEP_MC_FILTER=y
  69. +CONFIG_BT_BNEP_PROTO_FILTER=y
  70. +CONFIG_BT_HIDP=m
  71. +CONFIG_BT_HCIBTUSB=m
  72. +CONFIG_BT_HCIBCM203X=m
  73. +CONFIG_BT_HCIBPA10X=m
  74. +CONFIG_BT_HCIBFUSB=m
  75. +CONFIG_BT_HCIVHCI=m
  76. +CONFIG_BT_MRVL=m
  77. +CONFIG_BT_MRVL_SDIO=m
  78. +CONFIG_BT_ATH3K=m
  79. +CONFIG_CFG80211=m
  80. +CONFIG_MAC80211=m
  81. +CONFIG_MAC80211_RC_PID=y
  82. +CONFIG_MAC80211_MESH=y
  83. +CONFIG_WIMAX=m
  84. +CONFIG_NET_9P=m
  85. +CONFIG_NFC=m
  86. +CONFIG_NFC_PN533=m
  87. +CONFIG_DEVTMPFS=y
  88. +CONFIG_BLK_DEV_LOOP=y
  89. +CONFIG_BLK_DEV_CRYPTOLOOP=m
  90. +CONFIG_BLK_DEV_NBD=m
  91. +CONFIG_BLK_DEV_RAM=y
  92. +CONFIG_CDROM_PKTCDVD=m
  93. +CONFIG_SCSI=y
  94. +# CONFIG_SCSI_PROC_FS is not set
  95. +CONFIG_BLK_DEV_SD=m
  96. +CONFIG_BLK_DEV_SR=m
  97. +CONFIG_SCSI_MULTI_LUN=y
  98. +# CONFIG_SCSI_LOWLEVEL is not set
  99. +CONFIG_NETDEVICES=y
  100. +CONFIG_NETCONSOLE=m
  101. +CONFIG_TUN=m
  102. +CONFIG_MDIO_BITBANG=m
  103. +CONFIG_PPP=m
  104. +CONFIG_PPP_BSDCOMP=m
  105. +CONFIG_PPP_DEFLATE=m
  106. +CONFIG_PPP_ASYNC=m
  107. +CONFIG_PPP_SYNC_TTY=m
  108. +CONFIG_SLIP=m
  109. +CONFIG_SLIP_COMPRESSED=y
  110. +CONFIG_USB_CATC=m
  111. +CONFIG_USB_KAWETH=m
  112. +CONFIG_USB_PEGASUS=m
  113. +CONFIG_USB_RTL8150=m
  114. +CONFIG_USB_USBNET=y
  115. +CONFIG_USB_NET_AX8817X=m
  116. +CONFIG_USB_NET_CDCETHER=m
  117. +CONFIG_USB_NET_CDC_EEM=m
  118. +CONFIG_USB_NET_DM9601=m
  119. +CONFIG_USB_NET_SMSC75XX=m
  120. +CONFIG_USB_NET_SMSC95XX=y
  121. +CONFIG_USB_NET_GL620A=m
  122. +CONFIG_USB_NET_NET1080=m
  123. +CONFIG_USB_NET_PLUSB=m
  124. +CONFIG_USB_NET_MCS7830=m
  125. +CONFIG_USB_NET_CDC_SUBSET=m
  126. +CONFIG_USB_ALI_M5632=y
  127. +CONFIG_USB_AN2720=y
  128. +CONFIG_USB_KC2190=y
  129. +# CONFIG_USB_NET_ZAURUS is not set
  130. +CONFIG_USB_NET_CX82310_ETH=m
  131. +CONFIG_USB_NET_KALMIA=m
  132. +CONFIG_USB_NET_INT51X1=m
  133. +CONFIG_USB_IPHETH=m
  134. +CONFIG_USB_SIERRA_NET=m
  135. +CONFIG_USB_VL600=m
  136. +CONFIG_LIBERTAS_THINFIRM=m
  137. +CONFIG_LIBERTAS_THINFIRM_USB=m
  138. +CONFIG_AT76C50X_USB=m
  139. +CONFIG_USB_ZD1201=m
  140. +CONFIG_USB_NET_RNDIS_WLAN=m
  141. +CONFIG_RTL8187=m
  142. +CONFIG_MAC80211_HWSIM=m
  143. +CONFIG_B43=m
  144. +CONFIG_B43LEGACY=m
  145. +CONFIG_HOSTAP=m
  146. +CONFIG_LIBERTAS=m
  147. +CONFIG_LIBERTAS_USB=m
  148. +CONFIG_LIBERTAS_SDIO=m
  149. +CONFIG_P54_COMMON=m
  150. +CONFIG_P54_USB=m
  151. +CONFIG_RT2X00=m
  152. +CONFIG_RT2500USB=m
  153. +CONFIG_RT73USB=m
  154. +CONFIG_RT2800USB=m
  155. +CONFIG_RT2800USB_RT53XX=y
  156. +CONFIG_RTL8192CU=m
  157. +CONFIG_ZD1211RW=m
  158. +CONFIG_MWIFIEX=m
  159. +CONFIG_MWIFIEX_SDIO=m
  160. +CONFIG_WIMAX_I2400M_USB=m
  161. +CONFIG_INPUT_POLLDEV=m
  162. +# CONFIG_INPUT_MOUSEDEV_PSAUX is not set
  163. +CONFIG_INPUT_JOYDEV=m
  164. +CONFIG_INPUT_EVDEV=m
  165. +# CONFIG_INPUT_KEYBOARD is not set
  166. +# CONFIG_INPUT_MOUSE is not set
  167. +CONFIG_INPUT_MISC=y
  168. +CONFIG_INPUT_AD714X=m
  169. +CONFIG_INPUT_ATI_REMOTE2=m
  170. +CONFIG_INPUT_KEYSPAN_REMOTE=m
  171. +CONFIG_INPUT_POWERMATE=m
  172. +CONFIG_INPUT_YEALINK=m
  173. +CONFIG_INPUT_CM109=m
  174. +CONFIG_INPUT_UINPUT=m
  175. +CONFIG_INPUT_GPIO_ROTARY_ENCODER=m
  176. +CONFIG_INPUT_ADXL34X=m
  177. +CONFIG_INPUT_CMA3000=m
  178. +CONFIG_SERIO=m
  179. +CONFIG_SERIO_RAW=m
  180. +CONFIG_GAMEPORT=m
  181. +CONFIG_GAMEPORT_NS558=m
  182. +CONFIG_GAMEPORT_L4=m
  183. +# CONFIG_LEGACY_PTYS is not set
  184. +# CONFIG_DEVKMEM is not set
  185. +CONFIG_SERIAL_AMBA_PL011=y
  186. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  187. +# CONFIG_HW_RANDOM is not set
  188. +CONFIG_RAW_DRIVER=y
  189. +CONFIG_I2C=y
  190. +CONFIG_I2C_CHARDEV=m
  191. +CONFIG_I2C_BCM2708=m
  192. +CONFIG_SPI=y
  193. +CONFIG_SPI_BCM2708=m
  194. +CONFIG_GPIO_SYSFS=y
  195. +# CONFIG_HWMON is not set
  196. +CONFIG_WATCHDOG=y
  197. +CONFIG_BCM2708_WDT=m
  198. +CONFIG_FB=y
  199. +CONFIG_FB_BCM2708=y
  200. +CONFIG_FRAMEBUFFER_CONSOLE=y
  201. +CONFIG_LOGO=y
  202. +# CONFIG_LOGO_LINUX_MONO is not set
  203. +# CONFIG_LOGO_LINUX_VGA16 is not set
  204. +CONFIG_SOUND=y
  205. +CONFIG_SND=m
  206. +CONFIG_SND_SEQUENCER=m
  207. +CONFIG_SND_SEQ_DUMMY=m
  208. +CONFIG_SND_MIXER_OSS=m
  209. +CONFIG_SND_PCM_OSS=m
  210. +CONFIG_SND_SEQUENCER_OSS=y
  211. +CONFIG_SND_HRTIMER=m
  212. +CONFIG_SND_DUMMY=m
  213. +CONFIG_SND_ALOOP=m
  214. +CONFIG_SND_VIRMIDI=m
  215. +CONFIG_SND_MTPAV=m
  216. +CONFIG_SND_SERIAL_U16550=m
  217. +CONFIG_SND_MPU401=m
  218. +CONFIG_SND_BCM2835=m
  219. +CONFIG_SND_USB_AUDIO=m
  220. +CONFIG_SND_USB_UA101=m
  221. +CONFIG_SND_USB_CAIAQ=m
  222. +CONFIG_SND_USB_6FIRE=m
  223. +CONFIG_SOUND_PRIME=m
  224. +CONFIG_HID_A4TECH=m
  225. +CONFIG_HID_ACRUX=m
  226. +CONFIG_HID_APPLE=m
  227. +CONFIG_HID_BELKIN=m
  228. +CONFIG_HID_CHERRY=m
  229. +CONFIG_HID_CHICONY=m
  230. +CONFIG_HID_CYPRESS=m
  231. +CONFIG_HID_DRAGONRISE=m
  232. +CONFIG_HID_EMS_FF=m
  233. +CONFIG_HID_ELECOM=m
  234. +CONFIG_HID_EZKEY=m
  235. +CONFIG_HID_HOLTEK=m
  236. +CONFIG_HID_KEYTOUCH=m
  237. +CONFIG_HID_KYE=m
  238. +CONFIG_HID_UCLOGIC=m
  239. +CONFIG_HID_WALTOP=m
  240. +CONFIG_HID_GYRATION=m
  241. +CONFIG_HID_TWINHAN=m
  242. +CONFIG_HID_KENSINGTON=m
  243. +CONFIG_HID_LCPOWER=m
  244. +CONFIG_HID_LOGITECH=m
  245. +CONFIG_HID_MAGICMOUSE=m
  246. +CONFIG_HID_MICROSOFT=m
  247. +CONFIG_HID_MONTEREY=m
  248. +CONFIG_HID_MULTITOUCH=m
  249. +CONFIG_HID_NTRIG=m
  250. +CONFIG_HID_ORTEK=m
  251. +CONFIG_HID_PANTHERLORD=m
  252. +CONFIG_HID_PETALYNX=m
  253. +CONFIG_HID_PICOLCD=m
  254. +CONFIG_HID_ROCCAT=m
  255. +CONFIG_HID_SAMSUNG=m
  256. +CONFIG_HID_SPEEDLINK=m
  257. +CONFIG_HID_SUNPLUS=m
  258. +CONFIG_HID_GREENASIA=m
  259. +CONFIG_HID_SMARTJOYPLUS=m
  260. +CONFIG_HID_TOPSEED=m
  261. +CONFIG_HID_THRUSTMASTER=m
  262. +CONFIG_HID_ZEROPLUS=m
  263. +CONFIG_HID_ZYDACRON=m
  264. +CONFIG_HID_PID=y
  265. +CONFIG_USB_HIDDEV=y
  266. +CONFIG_USB=y
  267. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  268. +CONFIG_USB_MON=m
  269. +CONFIG_USB_DWCOTG=y
  270. +CONFIG_USB_STORAGE=y
  271. +CONFIG_USB_STORAGE_REALTEK=m
  272. +CONFIG_USB_STORAGE_DATAFAB=m
  273. +CONFIG_USB_STORAGE_FREECOM=m
  274. +CONFIG_USB_STORAGE_ISD200=m
  275. +CONFIG_USB_STORAGE_USBAT=m
  276. +CONFIG_USB_STORAGE_SDDR09=m
  277. +CONFIG_USB_STORAGE_SDDR55=m
  278. +CONFIG_USB_STORAGE_JUMPSHOT=m
  279. +CONFIG_USB_STORAGE_ALAUDA=m
  280. +CONFIG_USB_STORAGE_ONETOUCH=m
  281. +CONFIG_USB_STORAGE_KARMA=m
  282. +CONFIG_USB_STORAGE_CYPRESS_ATACB=m
  283. +CONFIG_USB_STORAGE_ENE_UB6250=m
  284. +CONFIG_USB_MDC800=m
  285. +CONFIG_USB_MICROTEK=m
  286. +CONFIG_USB_SERIAL=m
  287. +CONFIG_USB_SERIAL_GENERIC=y
  288. +CONFIG_USB_SERIAL_AIRCABLE=m
  289. +CONFIG_USB_SERIAL_ARK3116=m
  290. +CONFIG_USB_SERIAL_BELKIN=m
  291. +CONFIG_USB_SERIAL_CH341=m
  292. +CONFIG_USB_SERIAL_WHITEHEAT=m
  293. +CONFIG_USB_SERIAL_DIGI_ACCELEPORT=m
  294. +CONFIG_USB_SERIAL_CP210X=m
  295. +CONFIG_USB_SERIAL_CYPRESS_M8=m
  296. +CONFIG_USB_SERIAL_EMPEG=m
  297. +CONFIG_USB_SERIAL_FTDI_SIO=m
  298. +CONFIG_USB_SERIAL_VISOR=m
  299. +CONFIG_USB_SERIAL_IPAQ=m
  300. +CONFIG_USB_SERIAL_IR=m
  301. +CONFIG_USB_SERIAL_EDGEPORT=m
  302. +CONFIG_USB_SERIAL_EDGEPORT_TI=m
  303. +CONFIG_USB_SERIAL_GARMIN=m
  304. +CONFIG_USB_SERIAL_IPW=m
  305. +CONFIG_USB_SERIAL_IUU=m
  306. +CONFIG_USB_SERIAL_KEYSPAN_PDA=m
  307. +CONFIG_USB_SERIAL_KEYSPAN=m
  308. +CONFIG_USB_SERIAL_KLSI=m
  309. +CONFIG_USB_SERIAL_KOBIL_SCT=m
  310. +CONFIG_USB_SERIAL_MCT_U232=m
  311. +CONFIG_USB_SERIAL_MOS7720=m
  312. +CONFIG_USB_SERIAL_MOS7840=m
  313. +CONFIG_USB_SERIAL_NAVMAN=m
  314. +CONFIG_USB_SERIAL_PL2303=m
  315. +CONFIG_USB_SERIAL_OTI6858=m
  316. +CONFIG_USB_SERIAL_QCAUX=m
  317. +CONFIG_USB_SERIAL_QUALCOMM=m
  318. +CONFIG_USB_SERIAL_SPCP8X5=m
  319. +CONFIG_USB_SERIAL_SAFE=m
  320. +CONFIG_USB_SERIAL_SIERRAWIRELESS=m
  321. +CONFIG_USB_SERIAL_SYMBOL=m
  322. +CONFIG_USB_SERIAL_TI=m
  323. +CONFIG_USB_SERIAL_CYBERJACK=m
  324. +CONFIG_USB_SERIAL_XIRCOM=m
  325. +CONFIG_USB_SERIAL_OPTION=m
  326. +CONFIG_USB_SERIAL_OMNINET=m
  327. +CONFIG_USB_SERIAL_OPTICON=m
  328. +CONFIG_USB_SERIAL_SSU100=m
  329. +CONFIG_USB_SERIAL_DEBUG=m
  330. +CONFIG_USB_EMI62=m
  331. +CONFIG_USB_EMI26=m
  332. +CONFIG_USB_ADUTUX=m
  333. +CONFIG_USB_SEVSEG=m
  334. +CONFIG_USB_RIO500=m
  335. +CONFIG_USB_LEGOTOWER=m
  336. +CONFIG_USB_LCD=m
  337. +CONFIG_USB_LED=m
  338. +CONFIG_USB_CYPRESS_CY7C63=m
  339. +CONFIG_USB_CYTHERM=m
  340. +CONFIG_USB_IDMOUSE=m
  341. +CONFIG_USB_FTDI_ELAN=m
  342. +CONFIG_USB_APPLEDISPLAY=m
  343. +CONFIG_USB_LD=m
  344. +CONFIG_USB_TRANCEVIBRATOR=m
  345. +CONFIG_USB_IOWARRIOR=m
  346. +CONFIG_USB_TEST=m
  347. +CONFIG_USB_ISIGHTFW=m
  348. +CONFIG_USB_YUREX=m
  349. +CONFIG_MMC=y
  350. +CONFIG_MMC_SDHCI=y
  351. +CONFIG_MMC_SDHCI_PLTFM=y
  352. +CONFIG_MMC_SDHCI_BCM2708=y
  353. +CONFIG_MMC_SDHCI_BCM2708_DMA=y
  354. +CONFIG_MMC_BCM2835=y
  355. +CONFIG_MMC_BCM2835_DMA=y
  356. +CONFIG_UIO=m
  357. +CONFIG_UIO_PDRV_GENIRQ=m
  358. +# CONFIG_IOMMU_SUPPORT is not set
  359. +CONFIG_EXT4_FS=y
  360. +CONFIG_EXT4_FS_POSIX_ACL=y
  361. +CONFIG_EXT4_FS_SECURITY=y
  362. +CONFIG_REISERFS_FS=m
  363. +CONFIG_REISERFS_FS_XATTR=y
  364. +CONFIG_REISERFS_FS_POSIX_ACL=y
  365. +CONFIG_REISERFS_FS_SECURITY=y
  366. +CONFIG_JFS_FS=m
  367. +CONFIG_JFS_POSIX_ACL=y
  368. +CONFIG_JFS_SECURITY=y
  369. +CONFIG_XFS_FS=m
  370. +CONFIG_XFS_QUOTA=y
  371. +CONFIG_XFS_POSIX_ACL=y
  372. +CONFIG_XFS_RT=y
  373. +CONFIG_GFS2_FS=m
  374. +CONFIG_OCFS2_FS=m
  375. +CONFIG_BTRFS_FS=m
  376. +CONFIG_BTRFS_FS_POSIX_ACL=y
  377. +CONFIG_NILFS2_FS=m
  378. +CONFIG_AUTOFS4_FS=y
  379. +CONFIG_FUSE_FS=m
  380. +CONFIG_CUSE=m
  381. +CONFIG_FSCACHE=y
  382. +CONFIG_CACHEFILES=y
  383. +CONFIG_ISO9660_FS=m
  384. +CONFIG_JOLIET=y
  385. +CONFIG_ZISOFS=y
  386. +CONFIG_UDF_FS=m
  387. +CONFIG_MSDOS_FS=y
  388. +CONFIG_VFAT_FS=y
  389. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  390. +CONFIG_NTFS_FS=m
  391. +CONFIG_TMPFS=y
  392. +CONFIG_TMPFS_POSIX_ACL=y
  393. +CONFIG_CONFIGFS_FS=y
  394. +CONFIG_SQUASHFS=m
  395. +CONFIG_SQUASHFS_XATTR=y
  396. +CONFIG_SQUASHFS_LZO=y
  397. +CONFIG_SQUASHFS_XZ=y
  398. +CONFIG_NFS_FS=y
  399. +CONFIG_NFS_V3_ACL=y
  400. +CONFIG_NFS_V4=y
  401. +CONFIG_ROOT_NFS=y
  402. +CONFIG_NFS_FSCACHE=y
  403. +CONFIG_CIFS=m
  404. +CONFIG_CIFS_WEAK_PW_HASH=y
  405. +CONFIG_CIFS_XATTR=y
  406. +CONFIG_CIFS_POSIX=y
  407. +CONFIG_9P_FS=m
  408. +CONFIG_NLS_DEFAULT="utf8"
  409. +CONFIG_NLS_CODEPAGE_437=y
  410. +CONFIG_NLS_CODEPAGE_737=m
  411. +CONFIG_NLS_CODEPAGE_775=m
  412. +CONFIG_NLS_CODEPAGE_850=m
  413. +CONFIG_NLS_CODEPAGE_852=m
  414. +CONFIG_NLS_CODEPAGE_855=m
  415. +CONFIG_NLS_CODEPAGE_857=m
  416. +CONFIG_NLS_CODEPAGE_860=m
  417. +CONFIG_NLS_CODEPAGE_861=m
  418. +CONFIG_NLS_CODEPAGE_862=m
  419. +CONFIG_NLS_CODEPAGE_863=m
  420. +CONFIG_NLS_CODEPAGE_864=m
  421. +CONFIG_NLS_CODEPAGE_865=m
  422. +CONFIG_NLS_CODEPAGE_866=m
  423. +CONFIG_NLS_CODEPAGE_869=m
  424. +CONFIG_NLS_CODEPAGE_936=m
  425. +CONFIG_NLS_CODEPAGE_950=m
  426. +CONFIG_NLS_CODEPAGE_932=m
  427. +CONFIG_NLS_CODEPAGE_949=m
  428. +CONFIG_NLS_CODEPAGE_874=m
  429. +CONFIG_NLS_ISO8859_8=m
  430. +CONFIG_NLS_CODEPAGE_1250=m
  431. +CONFIG_NLS_CODEPAGE_1251=m
  432. +CONFIG_NLS_ASCII=y
  433. +CONFIG_NLS_ISO8859_1=m
  434. +CONFIG_NLS_ISO8859_2=m
  435. +CONFIG_NLS_ISO8859_3=m
  436. +CONFIG_NLS_ISO8859_4=m
  437. +CONFIG_NLS_ISO8859_5=m
  438. +CONFIG_NLS_ISO8859_6=m
  439. +CONFIG_NLS_ISO8859_7=m
  440. +CONFIG_NLS_ISO8859_9=m
  441. +CONFIG_NLS_ISO8859_13=m
  442. +CONFIG_NLS_ISO8859_14=m
  443. +CONFIG_NLS_ISO8859_15=m
  444. +CONFIG_NLS_KOI8_R=m
  445. +CONFIG_NLS_KOI8_U=m
  446. +CONFIG_NLS_UTF8=m
  447. +# CONFIG_SCHED_DEBUG is not set
  448. +# CONFIG_DEBUG_BUGVERBOSE is not set
  449. +# CONFIG_FTRACE is not set
  450. +# CONFIG_ARM_UNWIND is not set
  451. +CONFIG_CRYPTO_AUTHENC=m
  452. +CONFIG_CRYPTO_SEQIV=m
  453. +CONFIG_CRYPTO_CBC=y
  454. +CONFIG_CRYPTO_HMAC=y
  455. +CONFIG_CRYPTO_XCBC=m
  456. +CONFIG_CRYPTO_MD5=y
  457. +CONFIG_CRYPTO_SHA1=y
  458. +CONFIG_CRYPTO_SHA512=m
  459. +CONFIG_CRYPTO_TGR192=m
  460. +CONFIG_CRYPTO_WP512=m
  461. +CONFIG_CRYPTO_CAST5=m
  462. +CONFIG_CRYPTO_DES=y
  463. +CONFIG_CRYPTO_DEFLATE=m
  464. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  465. +# CONFIG_CRYPTO_HW is not set
  466. +CONFIG_CRC_ITU_T=y
  467. +CONFIG_LIBCRC32C=y
  468. diff -Nur linux-3.12.33/arch/arm/configs/bcmrpi_defconfig linux-3.12.33-rpi/arch/arm/configs/bcmrpi_defconfig
  469. --- linux-3.12.33/arch/arm/configs/bcmrpi_defconfig 1969-12-31 18:00:00.000000000 -0600
  470. +++ linux-3.12.33-rpi/arch/arm/configs/bcmrpi_defconfig 2014-12-03 19:13:32.288418001 -0600
  471. @@ -0,0 +1,1119 @@
  472. +# CONFIG_ARM_PATCH_PHYS_VIRT is not set
  473. +# CONFIG_LOCALVERSION_AUTO is not set
  474. +CONFIG_SYSVIPC=y
  475. +CONFIG_POSIX_MQUEUE=y
  476. +CONFIG_FHANDLE=y
  477. +CONFIG_AUDIT=y
  478. +CONFIG_NO_HZ=y
  479. +CONFIG_HIGH_RES_TIMERS=y
  480. +CONFIG_BSD_PROCESS_ACCT=y
  481. +CONFIG_BSD_PROCESS_ACCT_V3=y
  482. +CONFIG_TASKSTATS=y
  483. +CONFIG_TASK_DELAY_ACCT=y
  484. +CONFIG_TASK_XACCT=y
  485. +CONFIG_TASK_IO_ACCOUNTING=y
  486. +CONFIG_IKCONFIG=y
  487. +CONFIG_IKCONFIG_PROC=y
  488. +CONFIG_CGROUP_FREEZER=y
  489. +CONFIG_CGROUP_DEVICE=y
  490. +CONFIG_CGROUP_CPUACCT=y
  491. +CONFIG_RESOURCE_COUNTERS=y
  492. +CONFIG_MEMCG=y
  493. +CONFIG_BLK_CGROUP=y
  494. +CONFIG_NAMESPACES=y
  495. +CONFIG_SCHED_AUTOGROUP=y
  496. +CONFIG_RELAY=y
  497. +CONFIG_BLK_DEV_INITRD=y
  498. +CONFIG_EMBEDDED=y
  499. +# CONFIG_COMPAT_BRK is not set
  500. +CONFIG_PROFILING=y
  501. +CONFIG_OPROFILE=m
  502. +CONFIG_KPROBES=y
  503. +CONFIG_JUMP_LABEL=y
  504. +CONFIG_MODULES=y
  505. +CONFIG_MODULE_UNLOAD=y
  506. +CONFIG_MODVERSIONS=y
  507. +CONFIG_MODULE_SRCVERSION_ALL=y
  508. +CONFIG_BLK_DEV_THROTTLING=y
  509. +CONFIG_PARTITION_ADVANCED=y
  510. +CONFIG_MAC_PARTITION=y
  511. +CONFIG_CFQ_GROUP_IOSCHED=y
  512. +CONFIG_ARCH_BCM2708=y
  513. +CONFIG_PREEMPT=y
  514. +CONFIG_AEABI=y
  515. +CONFIG_CLEANCACHE=y
  516. +CONFIG_FRONTSWAP=y
  517. +CONFIG_CMA=y
  518. +CONFIG_UACCESS_WITH_MEMCPY=y
  519. +CONFIG_SECCOMP=y
  520. +CONFIG_CC_STACKPROTECTOR=y
  521. +CONFIG_ZBOOT_ROM_TEXT=0x0
  522. +CONFIG_ZBOOT_ROM_BSS=0x0
  523. +CONFIG_CMDLINE="console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait"
  524. +CONFIG_KEXEC=y
  525. +CONFIG_CPU_FREQ=y
  526. +CONFIG_CPU_FREQ_STAT=m
  527. +CONFIG_CPU_FREQ_STAT_DETAILS=y
  528. +CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE=y
  529. +CONFIG_CPU_FREQ_GOV_PERFORMANCE=y
  530. +CONFIG_CPU_FREQ_GOV_USERSPACE=y
  531. +CONFIG_CPU_FREQ_GOV_ONDEMAND=y
  532. +CONFIG_CPU_FREQ_GOV_CONSERVATIVE=y
  533. +CONFIG_CPU_IDLE=y
  534. +CONFIG_VFP=y
  535. +CONFIG_BINFMT_MISC=m
  536. +CONFIG_NET=y
  537. +CONFIG_PACKET=y
  538. +CONFIG_UNIX=y
  539. +CONFIG_XFRM_USER=y
  540. +CONFIG_NET_KEY=m
  541. +CONFIG_INET=y
  542. +CONFIG_IP_MULTICAST=y
  543. +CONFIG_IP_ADVANCED_ROUTER=y
  544. +CONFIG_IP_MULTIPLE_TABLES=y
  545. +CONFIG_IP_ROUTE_MULTIPATH=y
  546. +CONFIG_IP_ROUTE_VERBOSE=y
  547. +CONFIG_IP_PNP=y
  548. +CONFIG_IP_PNP_DHCP=y
  549. +CONFIG_IP_PNP_RARP=y
  550. +CONFIG_NET_IPIP=m
  551. +CONFIG_NET_IPGRE_DEMUX=m
  552. +CONFIG_NET_IPGRE=m
  553. +CONFIG_IP_MROUTE=y
  554. +CONFIG_IP_MROUTE_MULTIPLE_TABLES=y
  555. +CONFIG_IP_PIMSM_V1=y
  556. +CONFIG_IP_PIMSM_V2=y
  557. +CONFIG_SYN_COOKIES=y
  558. +CONFIG_INET_AH=m
  559. +CONFIG_INET_ESP=m
  560. +CONFIG_INET_IPCOMP=m
  561. +CONFIG_INET_XFRM_MODE_TRANSPORT=m
  562. +CONFIG_INET_XFRM_MODE_TUNNEL=m
  563. +CONFIG_INET_XFRM_MODE_BEET=m
  564. +CONFIG_INET_LRO=m
  565. +CONFIG_INET_DIAG=m
  566. +CONFIG_IPV6_PRIVACY=y
  567. +CONFIG_INET6_AH=m
  568. +CONFIG_INET6_ESP=m
  569. +CONFIG_INET6_IPCOMP=m
  570. +CONFIG_IPV6_TUNNEL=m
  571. +CONFIG_IPV6_MULTIPLE_TABLES=y
  572. +CONFIG_IPV6_MROUTE=y
  573. +CONFIG_IPV6_MROUTE_MULTIPLE_TABLES=y
  574. +CONFIG_IPV6_PIMSM_V2=y
  575. +CONFIG_NETFILTER=y
  576. +CONFIG_NF_CONNTRACK=m
  577. +CONFIG_NF_CONNTRACK_ZONES=y
  578. +CONFIG_NF_CONNTRACK_EVENTS=y
  579. +CONFIG_NF_CONNTRACK_TIMESTAMP=y
  580. +CONFIG_NF_CT_PROTO_DCCP=m
  581. +CONFIG_NF_CT_PROTO_UDPLITE=m
  582. +CONFIG_NF_CONNTRACK_AMANDA=m
  583. +CONFIG_NF_CONNTRACK_FTP=m
  584. +CONFIG_NF_CONNTRACK_H323=m
  585. +CONFIG_NF_CONNTRACK_IRC=m
  586. +CONFIG_NF_CONNTRACK_NETBIOS_NS=m
  587. +CONFIG_NF_CONNTRACK_SNMP=m
  588. +CONFIG_NF_CONNTRACK_PPTP=m
  589. +CONFIG_NF_CONNTRACK_SANE=m
  590. +CONFIG_NF_CONNTRACK_SIP=m
  591. +CONFIG_NF_CONNTRACK_TFTP=m
  592. +CONFIG_NF_CT_NETLINK=m
  593. +CONFIG_NETFILTER_XT_SET=m
  594. +CONFIG_NETFILTER_XT_TARGET_AUDIT=m
  595. +CONFIG_NETFILTER_XT_TARGET_CHECKSUM=m
  596. +CONFIG_NETFILTER_XT_TARGET_CLASSIFY=m
  597. +CONFIG_NETFILTER_XT_TARGET_CONNMARK=m
  598. +CONFIG_NETFILTER_XT_TARGET_DSCP=m
  599. +CONFIG_NETFILTER_XT_TARGET_HMARK=m
  600. +CONFIG_NETFILTER_XT_TARGET_IDLETIMER=m
  601. +CONFIG_NETFILTER_XT_TARGET_LED=m
  602. +CONFIG_NETFILTER_XT_TARGET_LOG=m
  603. +CONFIG_NETFILTER_XT_TARGET_MARK=m
  604. +CONFIG_NETFILTER_XT_TARGET_NFLOG=m
  605. +CONFIG_NETFILTER_XT_TARGET_NFQUEUE=m
  606. +CONFIG_NETFILTER_XT_TARGET_NOTRACK=m
  607. +CONFIG_NETFILTER_XT_TARGET_TEE=m
  608. +CONFIG_NETFILTER_XT_TARGET_TPROXY=m
  609. +CONFIG_NETFILTER_XT_TARGET_TRACE=m
  610. +CONFIG_NETFILTER_XT_TARGET_TCPMSS=m
  611. +CONFIG_NETFILTER_XT_TARGET_TCPOPTSTRIP=m
  612. +CONFIG_NETFILTER_XT_MATCH_ADDRTYPE=m
  613. +CONFIG_NETFILTER_XT_MATCH_BPF=m
  614. +CONFIG_NETFILTER_XT_MATCH_CLUSTER=m
  615. +CONFIG_NETFILTER_XT_MATCH_COMMENT=m
  616. +CONFIG_NETFILTER_XT_MATCH_CONNBYTES=m
  617. +CONFIG_NETFILTER_XT_MATCH_CONNLABEL=m
  618. +CONFIG_NETFILTER_XT_MATCH_CONNLIMIT=m
  619. +CONFIG_NETFILTER_XT_MATCH_CONNMARK=m
  620. +CONFIG_NETFILTER_XT_MATCH_CONNTRACK=m
  621. +CONFIG_NETFILTER_XT_MATCH_CPU=m
  622. +CONFIG_NETFILTER_XT_MATCH_DCCP=m
  623. +CONFIG_NETFILTER_XT_MATCH_DEVGROUP=m
  624. +CONFIG_NETFILTER_XT_MATCH_DSCP=m
  625. +CONFIG_NETFILTER_XT_MATCH_ESP=m
  626. +CONFIG_NETFILTER_XT_MATCH_HASHLIMIT=m
  627. +CONFIG_NETFILTER_XT_MATCH_HELPER=m
  628. +CONFIG_NETFILTER_XT_MATCH_IPRANGE=m
  629. +CONFIG_NETFILTER_XT_MATCH_IPVS=m
  630. +CONFIG_NETFILTER_XT_MATCH_LENGTH=m
  631. +CONFIG_NETFILTER_XT_MATCH_LIMIT=m
  632. +CONFIG_NETFILTER_XT_MATCH_MAC=m
  633. +CONFIG_NETFILTER_XT_MATCH_MARK=m
  634. +CONFIG_NETFILTER_XT_MATCH_MULTIPORT=m
  635. +CONFIG_NETFILTER_XT_MATCH_NFACCT=m
  636. +CONFIG_NETFILTER_XT_MATCH_OSF=m
  637. +CONFIG_NETFILTER_XT_MATCH_OWNER=m
  638. +CONFIG_NETFILTER_XT_MATCH_POLICY=m
  639. +CONFIG_NETFILTER_XT_MATCH_PHYSDEV=m
  640. +CONFIG_NETFILTER_XT_MATCH_PKTTYPE=m
  641. +CONFIG_NETFILTER_XT_MATCH_QUOTA=m
  642. +CONFIG_NETFILTER_XT_MATCH_RATEEST=m
  643. +CONFIG_NETFILTER_XT_MATCH_REALM=m
  644. +CONFIG_NETFILTER_XT_MATCH_RECENT=m
  645. +CONFIG_NETFILTER_XT_MATCH_SOCKET=m
  646. +CONFIG_NETFILTER_XT_MATCH_STATE=m
  647. +CONFIG_NETFILTER_XT_MATCH_STATISTIC=m
  648. +CONFIG_NETFILTER_XT_MATCH_STRING=m
  649. +CONFIG_NETFILTER_XT_MATCH_TCPMSS=m
  650. +CONFIG_NETFILTER_XT_MATCH_TIME=m
  651. +CONFIG_NETFILTER_XT_MATCH_U32=m
  652. +CONFIG_IP_SET=m
  653. +CONFIG_IP_SET_BITMAP_IP=m
  654. +CONFIG_IP_SET_BITMAP_IPMAC=m
  655. +CONFIG_IP_SET_BITMAP_PORT=m
  656. +CONFIG_IP_SET_HASH_IP=m
  657. +CONFIG_IP_SET_HASH_IPPORT=m
  658. +CONFIG_IP_SET_HASH_IPPORTIP=m
  659. +CONFIG_IP_SET_HASH_IPPORTNET=m
  660. +CONFIG_IP_SET_HASH_NET=m
  661. +CONFIG_IP_SET_HASH_NETPORT=m
  662. +CONFIG_IP_SET_HASH_NETIFACE=m
  663. +CONFIG_IP_SET_LIST_SET=m
  664. +CONFIG_IP_VS=m
  665. +CONFIG_IP_VS_PROTO_TCP=y
  666. +CONFIG_IP_VS_PROTO_UDP=y
  667. +CONFIG_IP_VS_PROTO_ESP=y
  668. +CONFIG_IP_VS_PROTO_AH=y
  669. +CONFIG_IP_VS_PROTO_SCTP=y
  670. +CONFIG_IP_VS_RR=m
  671. +CONFIG_IP_VS_WRR=m
  672. +CONFIG_IP_VS_LC=m
  673. +CONFIG_IP_VS_WLC=m
  674. +CONFIG_IP_VS_LBLC=m
  675. +CONFIG_IP_VS_LBLCR=m
  676. +CONFIG_IP_VS_DH=m
  677. +CONFIG_IP_VS_SH=m
  678. +CONFIG_IP_VS_SED=m
  679. +CONFIG_IP_VS_NQ=m
  680. +CONFIG_IP_VS_FTP=m
  681. +CONFIG_IP_VS_PE_SIP=m
  682. +CONFIG_NF_CONNTRACK_IPV4=m
  683. +CONFIG_IP_NF_IPTABLES=m
  684. +CONFIG_IP_NF_MATCH_AH=m
  685. +CONFIG_IP_NF_MATCH_ECN=m
  686. +CONFIG_IP_NF_MATCH_TTL=m
  687. +CONFIG_IP_NF_FILTER=m
  688. +CONFIG_IP_NF_TARGET_REJECT=m
  689. +CONFIG_IP_NF_TARGET_ULOG=m
  690. +CONFIG_NF_NAT_IPV4=m
  691. +CONFIG_IP_NF_TARGET_MASQUERADE=m
  692. +CONFIG_IP_NF_TARGET_NETMAP=m
  693. +CONFIG_IP_NF_TARGET_REDIRECT=m
  694. +CONFIG_IP_NF_MANGLE=m
  695. +CONFIG_IP_NF_TARGET_ECN=m
  696. +CONFIG_IP_NF_TARGET_TTL=m
  697. +CONFIG_IP_NF_RAW=m
  698. +CONFIG_IP_NF_ARPTABLES=m
  699. +CONFIG_IP_NF_ARPFILTER=m
  700. +CONFIG_IP_NF_ARP_MANGLE=m
  701. +CONFIG_NF_CONNTRACK_IPV6=m
  702. +CONFIG_IP6_NF_IPTABLES=m
  703. +CONFIG_IP6_NF_MATCH_AH=m
  704. +CONFIG_IP6_NF_MATCH_EUI64=m
  705. +CONFIG_IP6_NF_MATCH_FRAG=m
  706. +CONFIG_IP6_NF_MATCH_OPTS=m
  707. +CONFIG_IP6_NF_MATCH_HL=m
  708. +CONFIG_IP6_NF_MATCH_IPV6HEADER=m
  709. +CONFIG_IP6_NF_MATCH_MH=m
  710. +CONFIG_IP6_NF_MATCH_RT=m
  711. +CONFIG_IP6_NF_TARGET_HL=m
  712. +CONFIG_IP6_NF_FILTER=m
  713. +CONFIG_IP6_NF_TARGET_REJECT=m
  714. +CONFIG_IP6_NF_MANGLE=m
  715. +CONFIG_IP6_NF_RAW=m
  716. +CONFIG_NF_NAT_IPV6=m
  717. +CONFIG_IP6_NF_TARGET_MASQUERADE=m
  718. +CONFIG_IP6_NF_TARGET_NPT=m
  719. +CONFIG_BRIDGE_NF_EBTABLES=m
  720. +CONFIG_BRIDGE_EBT_BROUTE=m
  721. +CONFIG_BRIDGE_EBT_T_FILTER=m
  722. +CONFIG_BRIDGE_EBT_T_NAT=m
  723. +CONFIG_BRIDGE_EBT_802_3=m
  724. +CONFIG_BRIDGE_EBT_AMONG=m
  725. +CONFIG_BRIDGE_EBT_ARP=m
  726. +CONFIG_BRIDGE_EBT_IP=m
  727. +CONFIG_BRIDGE_EBT_IP6=m
  728. +CONFIG_BRIDGE_EBT_LIMIT=m
  729. +CONFIG_BRIDGE_EBT_MARK=m
  730. +CONFIG_BRIDGE_EBT_PKTTYPE=m
  731. +CONFIG_BRIDGE_EBT_STP=m
  732. +CONFIG_BRIDGE_EBT_VLAN=m
  733. +CONFIG_BRIDGE_EBT_ARPREPLY=m
  734. +CONFIG_BRIDGE_EBT_DNAT=m
  735. +CONFIG_BRIDGE_EBT_MARK_T=m
  736. +CONFIG_BRIDGE_EBT_REDIRECT=m
  737. +CONFIG_BRIDGE_EBT_SNAT=m
  738. +CONFIG_BRIDGE_EBT_LOG=m
  739. +CONFIG_BRIDGE_EBT_ULOG=m
  740. +CONFIG_BRIDGE_EBT_NFLOG=m
  741. +CONFIG_SCTP_COOKIE_HMAC_SHA1=y
  742. +CONFIG_ATM=m
  743. +CONFIG_L2TP=m
  744. +CONFIG_L2TP_V3=y
  745. +CONFIG_L2TP_IP=m
  746. +CONFIG_L2TP_ETH=m
  747. +CONFIG_BRIDGE=m
  748. +CONFIG_VLAN_8021Q=m
  749. +CONFIG_VLAN_8021Q_GVRP=y
  750. +CONFIG_ATALK=m
  751. +CONFIG_NET_SCHED=y
  752. +CONFIG_NET_SCH_CBQ=m
  753. +CONFIG_NET_SCH_HTB=m
  754. +CONFIG_NET_SCH_HFSC=m
  755. +CONFIG_NET_SCH_PRIO=m
  756. +CONFIG_NET_SCH_MULTIQ=m
  757. +CONFIG_NET_SCH_RED=m
  758. +CONFIG_NET_SCH_SFB=m
  759. +CONFIG_NET_SCH_SFQ=m
  760. +CONFIG_NET_SCH_TEQL=m
  761. +CONFIG_NET_SCH_TBF=m
  762. +CONFIG_NET_SCH_GRED=m
  763. +CONFIG_NET_SCH_DSMARK=m
  764. +CONFIG_NET_SCH_NETEM=m
  765. +CONFIG_NET_SCH_DRR=m
  766. +CONFIG_NET_SCH_MQPRIO=m
  767. +CONFIG_NET_SCH_CHOKE=m
  768. +CONFIG_NET_SCH_QFQ=m
  769. +CONFIG_NET_SCH_CODEL=m
  770. +CONFIG_NET_SCH_FQ_CODEL=m
  771. +CONFIG_NET_SCH_INGRESS=m
  772. +CONFIG_NET_SCH_PLUG=m
  773. +CONFIG_NET_CLS_BASIC=m
  774. +CONFIG_NET_CLS_TCINDEX=m
  775. +CONFIG_NET_CLS_ROUTE4=m
  776. +CONFIG_NET_CLS_FW=m
  777. +CONFIG_NET_CLS_U32=m
  778. +CONFIG_CLS_U32_MARK=y
  779. +CONFIG_NET_CLS_RSVP=m
  780. +CONFIG_NET_CLS_RSVP6=m
  781. +CONFIG_NET_CLS_FLOW=m
  782. +CONFIG_NET_CLS_CGROUP=m
  783. +CONFIG_NET_EMATCH=y
  784. +CONFIG_NET_EMATCH_CMP=m
  785. +CONFIG_NET_EMATCH_NBYTE=m
  786. +CONFIG_NET_EMATCH_U32=m
  787. +CONFIG_NET_EMATCH_META=m
  788. +CONFIG_NET_EMATCH_TEXT=m
  789. +CONFIG_NET_EMATCH_IPSET=m
  790. +CONFIG_NET_CLS_ACT=y
  791. +CONFIG_NET_ACT_POLICE=m
  792. +CONFIG_NET_ACT_GACT=m
  793. +CONFIG_GACT_PROB=y
  794. +CONFIG_NET_ACT_MIRRED=m
  795. +CONFIG_NET_ACT_IPT=m
  796. +CONFIG_NET_ACT_NAT=m
  797. +CONFIG_NET_ACT_PEDIT=m
  798. +CONFIG_NET_ACT_SIMP=m
  799. +CONFIG_NET_ACT_SKBEDIT=m
  800. +CONFIG_NET_ACT_CSUM=m
  801. +CONFIG_BATMAN_ADV=m
  802. +CONFIG_OPENVSWITCH=m
  803. +CONFIG_NET_PKTGEN=m
  804. +CONFIG_HAMRADIO=y
  805. +CONFIG_AX25=m
  806. +CONFIG_NETROM=m
  807. +CONFIG_ROSE=m
  808. +CONFIG_MKISS=m
  809. +CONFIG_6PACK=m
  810. +CONFIG_BPQETHER=m
  811. +CONFIG_BAYCOM_SER_FDX=m
  812. +CONFIG_BAYCOM_SER_HDX=m
  813. +CONFIG_YAM=m
  814. +CONFIG_IRDA=m
  815. +CONFIG_IRLAN=m
  816. +CONFIG_IRNET=m
  817. +CONFIG_IRCOMM=m
  818. +CONFIG_IRDA_ULTRA=y
  819. +CONFIG_IRDA_CACHE_LAST_LSAP=y
  820. +CONFIG_IRDA_FAST_RR=y
  821. +CONFIG_IRTTY_SIR=m
  822. +CONFIG_KINGSUN_DONGLE=m
  823. +CONFIG_KSDAZZLE_DONGLE=m
  824. +CONFIG_KS959_DONGLE=m
  825. +CONFIG_USB_IRDA=m
  826. +CONFIG_SIGMATEL_FIR=m
  827. +CONFIG_MCS_FIR=m
  828. +CONFIG_BT=m
  829. +CONFIG_BT_RFCOMM=m
  830. +CONFIG_BT_RFCOMM_TTY=y
  831. +CONFIG_BT_BNEP=m
  832. +CONFIG_BT_BNEP_MC_FILTER=y
  833. +CONFIG_BT_BNEP_PROTO_FILTER=y
  834. +CONFIG_BT_HIDP=m
  835. +CONFIG_BT_HCIBTUSB=m
  836. +CONFIG_BT_HCIBCM203X=m
  837. +CONFIG_BT_HCIBPA10X=m
  838. +CONFIG_BT_HCIBFUSB=m
  839. +CONFIG_BT_HCIVHCI=m
  840. +CONFIG_BT_MRVL=m
  841. +CONFIG_BT_MRVL_SDIO=m
  842. +CONFIG_BT_ATH3K=m
  843. +CONFIG_BT_WILINK=m
  844. +CONFIG_CFG80211=m
  845. +CONFIG_CFG80211_WEXT=y
  846. +CONFIG_MAC80211=m
  847. +CONFIG_MAC80211_RC_PID=y
  848. +CONFIG_MAC80211_MESH=y
  849. +CONFIG_WIMAX=m
  850. +CONFIG_RFKILL=m
  851. +CONFIG_RFKILL_INPUT=y
  852. +CONFIG_NET_9P=m
  853. +CONFIG_NFC=m
  854. +CONFIG_NFC_PN533=m
  855. +CONFIG_DEVTMPFS=y
  856. +CONFIG_DEVTMPFS_MOUNT=y
  857. +CONFIG_DMA_CMA=y
  858. +CONFIG_CMA_SIZE_MBYTES=5
  859. +CONFIG_BLK_DEV_LOOP=y
  860. +CONFIG_BLK_DEV_CRYPTOLOOP=m
  861. +CONFIG_BLK_DEV_DRBD=m
  862. +CONFIG_BLK_DEV_NBD=m
  863. +CONFIG_BLK_DEV_RAM=y
  864. +CONFIG_CDROM_PKTCDVD=m
  865. +CONFIG_EEPROM_AT24=m
  866. +CONFIG_SCSI=y
  867. +# CONFIG_SCSI_PROC_FS is not set
  868. +CONFIG_BLK_DEV_SD=y
  869. +CONFIG_CHR_DEV_ST=m
  870. +CONFIG_CHR_DEV_OSST=m
  871. +CONFIG_BLK_DEV_SR=m
  872. +CONFIG_CHR_DEV_SG=m
  873. +CONFIG_SCSI_MULTI_LUN=y
  874. +CONFIG_SCSI_ISCSI_ATTRS=y
  875. +CONFIG_ISCSI_TCP=m
  876. +CONFIG_ISCSI_BOOT_SYSFS=m
  877. +CONFIG_MD=y
  878. +CONFIG_MD_LINEAR=m
  879. +CONFIG_MD_RAID0=m
  880. +CONFIG_BLK_DEV_DM=m
  881. +CONFIG_DM_CRYPT=m
  882. +CONFIG_DM_SNAPSHOT=m
  883. +CONFIG_DM_MIRROR=m
  884. +CONFIG_DM_RAID=m
  885. +CONFIG_DM_LOG_USERSPACE=m
  886. +CONFIG_DM_ZERO=m
  887. +CONFIG_DM_DELAY=m
  888. +CONFIG_NETDEVICES=y
  889. +CONFIG_BONDING=m
  890. +CONFIG_DUMMY=m
  891. +CONFIG_IFB=m
  892. +CONFIG_MACVLAN=m
  893. +CONFIG_NETCONSOLE=m
  894. +CONFIG_TUN=m
  895. +CONFIG_VETH=m
  896. +CONFIG_MDIO_BITBANG=m
  897. +CONFIG_PPP=m
  898. +CONFIG_PPP_BSDCOMP=m
  899. +CONFIG_PPP_DEFLATE=m
  900. +CONFIG_PPP_FILTER=y
  901. +CONFIG_PPP_MPPE=m
  902. +CONFIG_PPP_MULTILINK=y
  903. +CONFIG_PPPOATM=m
  904. +CONFIG_PPPOE=m
  905. +CONFIG_PPPOL2TP=m
  906. +CONFIG_PPP_ASYNC=m
  907. +CONFIG_PPP_SYNC_TTY=m
  908. +CONFIG_SLIP=m
  909. +CONFIG_SLIP_COMPRESSED=y
  910. +CONFIG_SLIP_SMART=y
  911. +CONFIG_USB_CATC=m
  912. +CONFIG_USB_KAWETH=m
  913. +CONFIG_USB_PEGASUS=m
  914. +CONFIG_USB_RTL8150=m
  915. +CONFIG_USB_RTL8152=m
  916. +CONFIG_USB_USBNET=y
  917. +CONFIG_USB_NET_AX8817X=m
  918. +CONFIG_USB_NET_AX88179_178A=m
  919. +CONFIG_USB_NET_CDCETHER=m
  920. +CONFIG_USB_NET_CDC_EEM=m
  921. +CONFIG_USB_NET_CDC_NCM=m
  922. +CONFIG_USB_NET_CDC_MBIM=m
  923. +CONFIG_USB_NET_DM9601=m
  924. +CONFIG_USB_NET_SMSC75XX=m
  925. +CONFIG_USB_NET_SMSC95XX=y
  926. +CONFIG_USB_NET_GL620A=m
  927. +CONFIG_USB_NET_NET1080=m
  928. +CONFIG_USB_NET_PLUSB=m
  929. +CONFIG_USB_NET_MCS7830=m
  930. +CONFIG_USB_NET_CDC_SUBSET=m
  931. +CONFIG_USB_ALI_M5632=y
  932. +CONFIG_USB_AN2720=y
  933. +CONFIG_USB_EPSON2888=y
  934. +CONFIG_USB_KC2190=y
  935. +CONFIG_USB_NET_ZAURUS=m
  936. +CONFIG_USB_NET_CX82310_ETH=m
  937. +CONFIG_USB_NET_KALMIA=m
  938. +CONFIG_USB_NET_QMI_WWAN=m
  939. +CONFIG_USB_HSO=m
  940. +CONFIG_USB_NET_INT51X1=m
  941. +CONFIG_USB_IPHETH=m
  942. +CONFIG_USB_SIERRA_NET=m
  943. +CONFIG_USB_VL600=m
  944. +CONFIG_LIBERTAS_THINFIRM=m
  945. +CONFIG_LIBERTAS_THINFIRM_USB=m
  946. +CONFIG_AT76C50X_USB=m
  947. +CONFIG_USB_ZD1201=m
  948. +CONFIG_USB_NET_RNDIS_WLAN=m
  949. +CONFIG_RTL8187=m
  950. +CONFIG_MAC80211_HWSIM=m
  951. +CONFIG_ATH_CARDS=m
  952. +CONFIG_ATH9K=m
  953. +CONFIG_ATH9K_HTC=m
  954. +CONFIG_CARL9170=m
  955. +CONFIG_ATH6KL=m
  956. +CONFIG_ATH6KL_USB=m
  957. +CONFIG_AR5523=m
  958. +CONFIG_B43=m
  959. +# CONFIG_B43_PHY_N is not set
  960. +CONFIG_B43LEGACY=m
  961. +CONFIG_BRCMFMAC=m
  962. +# CONFIG_BRCMFMAC_SDIO is not set
  963. +CONFIG_BRCMFMAC_USB=y
  964. +CONFIG_HOSTAP=m
  965. +CONFIG_LIBERTAS=m
  966. +CONFIG_LIBERTAS_USB=m
  967. +CONFIG_LIBERTAS_SDIO=m
  968. +CONFIG_P54_COMMON=m
  969. +CONFIG_P54_USB=m
  970. +CONFIG_RT2X00=m
  971. +CONFIG_RT2500USB=m
  972. +CONFIG_RT73USB=m
  973. +CONFIG_RT2800USB=m
  974. +CONFIG_RT2800USB_RT3573=y
  975. +CONFIG_RT2800USB_RT53XX=y
  976. +CONFIG_RT2800USB_RT55XX=y
  977. +CONFIG_RT2800USB_UNKNOWN=y
  978. +CONFIG_RTL8192CU=m
  979. +CONFIG_ZD1211RW=m
  980. +CONFIG_MWIFIEX=m
  981. +CONFIG_MWIFIEX_SDIO=m
  982. +CONFIG_WIMAX_I2400M_USB=m
  983. +CONFIG_INPUT_POLLDEV=m
  984. +# CONFIG_INPUT_MOUSEDEV_PSAUX is not set
  985. +CONFIG_INPUT_JOYDEV=m
  986. +CONFIG_INPUT_EVDEV=m
  987. +# CONFIG_INPUT_KEYBOARD is not set
  988. +# CONFIG_INPUT_MOUSE is not set
  989. +CONFIG_INPUT_JOYSTICK=y
  990. +CONFIG_JOYSTICK_IFORCE=m
  991. +CONFIG_JOYSTICK_IFORCE_USB=y
  992. +CONFIG_JOYSTICK_XPAD=m
  993. +CONFIG_JOYSTICK_XPAD_FF=y
  994. +CONFIG_INPUT_MISC=y
  995. +CONFIG_INPUT_AD714X=m
  996. +CONFIG_INPUT_ATI_REMOTE2=m
  997. +CONFIG_INPUT_KEYSPAN_REMOTE=m
  998. +CONFIG_INPUT_POWERMATE=m
  999. +CONFIG_INPUT_YEALINK=m
  1000. +CONFIG_INPUT_CM109=m
  1001. +CONFIG_INPUT_UINPUT=m
  1002. +CONFIG_INPUT_GPIO_ROTARY_ENCODER=m
  1003. +CONFIG_INPUT_ADXL34X=m
  1004. +CONFIG_INPUT_CMA3000=m
  1005. +CONFIG_SERIO=m
  1006. +CONFIG_SERIO_RAW=m
  1007. +CONFIG_GAMEPORT=m
  1008. +CONFIG_GAMEPORT_NS558=m
  1009. +CONFIG_GAMEPORT_L4=m
  1010. +CONFIG_DEVPTS_MULTIPLE_INSTANCES=y
  1011. +# CONFIG_LEGACY_PTYS is not set
  1012. +# CONFIG_DEVKMEM is not set
  1013. +CONFIG_SERIAL_AMBA_PL011=y
  1014. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  1015. +CONFIG_TTY_PRINTK=y
  1016. +CONFIG_HW_RANDOM=y
  1017. +CONFIG_HW_RANDOM_BCM2708=m
  1018. +CONFIG_RAW_DRIVER=y
  1019. +CONFIG_BRCM_CHAR_DRIVERS=y
  1020. +CONFIG_BCM_VC_CMA=y
  1021. +CONFIG_BCM_VC_SM=y
  1022. +CONFIG_I2C=y
  1023. +CONFIG_I2C_CHARDEV=m
  1024. +CONFIG_I2C_BCM2708=m
  1025. +CONFIG_SPI=y
  1026. +CONFIG_SPI_BCM2708=m
  1027. +CONFIG_SPI_SPIDEV=y
  1028. +CONFIG_PPS=m
  1029. +CONFIG_PPS_CLIENT_LDISC=m
  1030. +CONFIG_PPS_CLIENT_GPIO=m
  1031. +CONFIG_GPIO_SYSFS=y
  1032. +CONFIG_W1=m
  1033. +CONFIG_W1_MASTER_DS2490=m
  1034. +CONFIG_W1_MASTER_DS2482=m
  1035. +CONFIG_W1_MASTER_DS1WM=m
  1036. +CONFIG_W1_MASTER_GPIO=m
  1037. +CONFIG_W1_SLAVE_THERM=m
  1038. +CONFIG_W1_SLAVE_SMEM=m
  1039. +CONFIG_W1_SLAVE_DS2408=m
  1040. +CONFIG_W1_SLAVE_DS2413=m
  1041. +CONFIG_W1_SLAVE_DS2423=m
  1042. +CONFIG_W1_SLAVE_DS2431=m
  1043. +CONFIG_W1_SLAVE_DS2433=m
  1044. +CONFIG_W1_SLAVE_DS2760=m
  1045. +CONFIG_W1_SLAVE_DS2780=m
  1046. +CONFIG_W1_SLAVE_DS2781=m
  1047. +CONFIG_W1_SLAVE_DS28E04=m
  1048. +CONFIG_W1_SLAVE_BQ27000=m
  1049. +CONFIG_BATTERY_DS2760=m
  1050. +# CONFIG_HWMON is not set
  1051. +CONFIG_THERMAL=y
  1052. +CONFIG_THERMAL_BCM2835=y
  1053. +CONFIG_WATCHDOG=y
  1054. +CONFIG_BCM2708_WDT=m
  1055. +CONFIG_MEDIA_SUPPORT=m
  1056. +CONFIG_MEDIA_CAMERA_SUPPORT=y
  1057. +CONFIG_MEDIA_ANALOG_TV_SUPPORT=y
  1058. +CONFIG_MEDIA_DIGITAL_TV_SUPPORT=y
  1059. +CONFIG_MEDIA_RADIO_SUPPORT=y
  1060. +CONFIG_MEDIA_RC_SUPPORT=y
  1061. +CONFIG_MEDIA_CONTROLLER=y
  1062. +CONFIG_LIRC=m
  1063. +CONFIG_RC_DEVICES=y
  1064. +CONFIG_RC_ATI_REMOTE=m
  1065. +CONFIG_IR_IMON=m
  1066. +CONFIG_IR_MCEUSB=m
  1067. +CONFIG_IR_REDRAT3=m
  1068. +CONFIG_IR_STREAMZAP=m
  1069. +CONFIG_IR_IGUANA=m
  1070. +CONFIG_IR_TTUSBIR=m
  1071. +CONFIG_RC_LOOPBACK=m
  1072. +CONFIG_IR_GPIO_CIR=m
  1073. +CONFIG_MEDIA_USB_SUPPORT=y
  1074. +CONFIG_USB_VIDEO_CLASS=m
  1075. +CONFIG_USB_M5602=m
  1076. +CONFIG_USB_STV06XX=m
  1077. +CONFIG_USB_GL860=m
  1078. +CONFIG_USB_GSPCA_BENQ=m
  1079. +CONFIG_USB_GSPCA_CONEX=m
  1080. +CONFIG_USB_GSPCA_CPIA1=m
  1081. +CONFIG_USB_GSPCA_ETOMS=m
  1082. +CONFIG_USB_GSPCA_FINEPIX=m
  1083. +CONFIG_USB_GSPCA_JEILINJ=m
  1084. +CONFIG_USB_GSPCA_JL2005BCD=m
  1085. +CONFIG_USB_GSPCA_KINECT=m
  1086. +CONFIG_USB_GSPCA_KONICA=m
  1087. +CONFIG_USB_GSPCA_MARS=m
  1088. +CONFIG_USB_GSPCA_MR97310A=m
  1089. +CONFIG_USB_GSPCA_NW80X=m
  1090. +CONFIG_USB_GSPCA_OV519=m
  1091. +CONFIG_USB_GSPCA_OV534=m
  1092. +CONFIG_USB_GSPCA_OV534_9=m
  1093. +CONFIG_USB_GSPCA_PAC207=m
  1094. +CONFIG_USB_GSPCA_PAC7302=m
  1095. +CONFIG_USB_GSPCA_PAC7311=m
  1096. +CONFIG_USB_GSPCA_SE401=m
  1097. +CONFIG_USB_GSPCA_SN9C2028=m
  1098. +CONFIG_USB_GSPCA_SN9C20X=m
  1099. +CONFIG_USB_GSPCA_SONIXB=m
  1100. +CONFIG_USB_GSPCA_SONIXJ=m
  1101. +CONFIG_USB_GSPCA_SPCA500=m
  1102. +CONFIG_USB_GSPCA_SPCA501=m
  1103. +CONFIG_USB_GSPCA_SPCA505=m
  1104. +CONFIG_USB_GSPCA_SPCA506=m
  1105. +CONFIG_USB_GSPCA_SPCA508=m
  1106. +CONFIG_USB_GSPCA_SPCA561=m
  1107. +CONFIG_USB_GSPCA_SPCA1528=m
  1108. +CONFIG_USB_GSPCA_SQ905=m
  1109. +CONFIG_USB_GSPCA_SQ905C=m
  1110. +CONFIG_USB_GSPCA_SQ930X=m
  1111. +CONFIG_USB_GSPCA_STK014=m
  1112. +CONFIG_USB_GSPCA_STK1135=m
  1113. +CONFIG_USB_GSPCA_STV0680=m
  1114. +CONFIG_USB_GSPCA_SUNPLUS=m
  1115. +CONFIG_USB_GSPCA_T613=m
  1116. +CONFIG_USB_GSPCA_TOPRO=m
  1117. +CONFIG_USB_GSPCA_TV8532=m
  1118. +CONFIG_USB_GSPCA_VC032X=m
  1119. +CONFIG_USB_GSPCA_VICAM=m
  1120. +CONFIG_USB_GSPCA_XIRLINK_CIT=m
  1121. +CONFIG_USB_GSPCA_ZC3XX=m
  1122. +CONFIG_USB_PWC=m
  1123. +CONFIG_VIDEO_CPIA2=m
  1124. +CONFIG_USB_ZR364XX=m
  1125. +CONFIG_USB_STKWEBCAM=m
  1126. +CONFIG_USB_S2255=m
  1127. +CONFIG_USB_SN9C102=m
  1128. +CONFIG_VIDEO_USBTV=m
  1129. +CONFIG_VIDEO_PVRUSB2=m
  1130. +CONFIG_VIDEO_HDPVR=m
  1131. +CONFIG_VIDEO_TLG2300=m
  1132. +CONFIG_VIDEO_USBVISION=m
  1133. +CONFIG_VIDEO_STK1160_COMMON=m
  1134. +CONFIG_VIDEO_AU0828=m
  1135. +CONFIG_VIDEO_CX231XX=m
  1136. +CONFIG_VIDEO_CX231XX_ALSA=m
  1137. +CONFIG_VIDEO_CX231XX_DVB=m
  1138. +CONFIG_VIDEO_TM6000=m
  1139. +CONFIG_VIDEO_TM6000_ALSA=m
  1140. +CONFIG_VIDEO_TM6000_DVB=m
  1141. +CONFIG_DVB_USB=m
  1142. +CONFIG_DVB_USB_A800=m
  1143. +CONFIG_DVB_USB_DIBUSB_MB=m
  1144. +CONFIG_DVB_USB_DIBUSB_MB_FAULTY=y
  1145. +CONFIG_DVB_USB_DIBUSB_MC=m
  1146. +CONFIG_DVB_USB_DIB0700=m
  1147. +CONFIG_DVB_USB_UMT_010=m
  1148. +CONFIG_DVB_USB_CXUSB=m
  1149. +CONFIG_DVB_USB_M920X=m
  1150. +CONFIG_DVB_USB_DIGITV=m
  1151. +CONFIG_DVB_USB_VP7045=m
  1152. +CONFIG_DVB_USB_VP702X=m
  1153. +CONFIG_DVB_USB_GP8PSK=m
  1154. +CONFIG_DVB_USB_NOVA_T_USB2=m
  1155. +CONFIG_DVB_USB_TTUSB2=m
  1156. +CONFIG_DVB_USB_DTT200U=m
  1157. +CONFIG_DVB_USB_OPERA1=m
  1158. +CONFIG_DVB_USB_AF9005=m
  1159. +CONFIG_DVB_USB_AF9005_REMOTE=m
  1160. +CONFIG_DVB_USB_PCTV452E=m
  1161. +CONFIG_DVB_USB_DW2102=m
  1162. +CONFIG_DVB_USB_CINERGY_T2=m
  1163. +CONFIG_DVB_USB_DTV5100=m
  1164. +CONFIG_DVB_USB_FRIIO=m
  1165. +CONFIG_DVB_USB_AZ6027=m
  1166. +CONFIG_DVB_USB_TECHNISAT_USB2=m
  1167. +CONFIG_DVB_USB_V2=m
  1168. +CONFIG_DVB_USB_AF9015=m
  1169. +CONFIG_DVB_USB_AF9035=m
  1170. +CONFIG_DVB_USB_ANYSEE=m
  1171. +CONFIG_DVB_USB_AU6610=m
  1172. +CONFIG_DVB_USB_AZ6007=m
  1173. +CONFIG_DVB_USB_CE6230=m
  1174. +CONFIG_DVB_USB_EC168=m
  1175. +CONFIG_DVB_USB_GL861=m
  1176. +CONFIG_DVB_USB_IT913X=m
  1177. +CONFIG_DVB_USB_LME2510=m
  1178. +CONFIG_DVB_USB_MXL111SF=m
  1179. +CONFIG_DVB_USB_RTL28XXU=m
  1180. +CONFIG_SMS_USB_DRV=m
  1181. +CONFIG_DVB_B2C2_FLEXCOP_USB=m
  1182. +CONFIG_VIDEO_EM28XX=m
  1183. +CONFIG_VIDEO_EM28XX_ALSA=m
  1184. +CONFIG_VIDEO_EM28XX_DVB=m
  1185. +CONFIG_V4L_PLATFORM_DRIVERS=y
  1186. +CONFIG_VIDEO_BCM2835=y
  1187. +CONFIG_VIDEO_BCM2835_MMAL=m
  1188. +CONFIG_RADIO_SI470X=y
  1189. +CONFIG_USB_SI470X=m
  1190. +CONFIG_I2C_SI470X=m
  1191. +CONFIG_USB_MR800=m
  1192. +CONFIG_USB_DSBR=m
  1193. +CONFIG_RADIO_SHARK=m
  1194. +CONFIG_RADIO_SHARK2=m
  1195. +CONFIG_RADIO_SI4713=m
  1196. +CONFIG_USB_KEENE=m
  1197. +CONFIG_USB_MA901=m
  1198. +CONFIG_RADIO_TEA5764=m
  1199. +CONFIG_RADIO_SAA7706H=m
  1200. +CONFIG_RADIO_TEF6862=m
  1201. +CONFIG_RADIO_WL1273=m
  1202. +CONFIG_RADIO_WL128X=m
  1203. +CONFIG_FB=y
  1204. +CONFIG_FB_BCM2708=y
  1205. +# CONFIG_BACKLIGHT_GENERIC is not set
  1206. +CONFIG_FRAMEBUFFER_CONSOLE=y
  1207. +CONFIG_LOGO=y
  1208. +# CONFIG_LOGO_LINUX_MONO is not set
  1209. +# CONFIG_LOGO_LINUX_VGA16 is not set
  1210. +CONFIG_SOUND=y
  1211. +CONFIG_SND=m
  1212. +CONFIG_SND_SEQUENCER=m
  1213. +CONFIG_SND_SEQ_DUMMY=m
  1214. +CONFIG_SND_MIXER_OSS=m
  1215. +CONFIG_SND_PCM_OSS=m
  1216. +CONFIG_SND_SEQUENCER_OSS=y
  1217. +CONFIG_SND_HRTIMER=m
  1218. +CONFIG_SND_DUMMY=m
  1219. +CONFIG_SND_ALOOP=m
  1220. +CONFIG_SND_VIRMIDI=m
  1221. +CONFIG_SND_MTPAV=m
  1222. +CONFIG_SND_SERIAL_U16550=m
  1223. +CONFIG_SND_MPU401=m
  1224. +CONFIG_SND_BCM2835=m
  1225. +CONFIG_SND_USB_AUDIO=m
  1226. +CONFIG_SND_USB_UA101=m
  1227. +CONFIG_SND_USB_CAIAQ=m
  1228. +CONFIG_SND_USB_CAIAQ_INPUT=y
  1229. +CONFIG_SND_USB_6FIRE=m
  1230. +CONFIG_SND_SOC=m
  1231. +CONFIG_SND_BCM2708_SOC_I2S=m
  1232. +CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC=m
  1233. +CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS=m
  1234. +CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI=m
  1235. +CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP=m
  1236. +CONFIG_SND_BCM2708_SOC_RPI_DAC=m
  1237. +CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC=m
  1238. +CONFIG_SND_SIMPLE_CARD=m
  1239. +CONFIG_SOUND_PRIME=m
  1240. +CONFIG_HIDRAW=y
  1241. +CONFIG_HID_A4TECH=m
  1242. +CONFIG_HID_ACRUX=m
  1243. +CONFIG_HID_APPLE=m
  1244. +CONFIG_HID_BELKIN=m
  1245. +CONFIG_HID_CHERRY=m
  1246. +CONFIG_HID_CHICONY=m
  1247. +CONFIG_HID_CYPRESS=m
  1248. +CONFIG_HID_DRAGONRISE=m
  1249. +CONFIG_HID_EMS_FF=m
  1250. +CONFIG_HID_ELECOM=m
  1251. +CONFIG_HID_ELO=m
  1252. +CONFIG_HID_EZKEY=m
  1253. +CONFIG_HID_HOLTEK=m
  1254. +CONFIG_HID_KEYTOUCH=m
  1255. +CONFIG_HID_KYE=m
  1256. +CONFIG_HID_UCLOGIC=m
  1257. +CONFIG_HID_WALTOP=m
  1258. +CONFIG_HID_GYRATION=m
  1259. +CONFIG_HID_TWINHAN=m
  1260. +CONFIG_HID_KENSINGTON=m
  1261. +CONFIG_HID_LCPOWER=m
  1262. +CONFIG_HID_LOGITECH=m
  1263. +CONFIG_HID_MAGICMOUSE=m
  1264. +CONFIG_HID_MICROSOFT=m
  1265. +CONFIG_HID_MONTEREY=m
  1266. +CONFIG_HID_MULTITOUCH=m
  1267. +CONFIG_HID_NTRIG=m
  1268. +CONFIG_HID_ORTEK=m
  1269. +CONFIG_HID_PANTHERLORD=m
  1270. +CONFIG_HID_PETALYNX=m
  1271. +CONFIG_HID_PICOLCD=m
  1272. +CONFIG_HID_ROCCAT=m
  1273. +CONFIG_HID_SAMSUNG=m
  1274. +CONFIG_HID_SONY=m
  1275. +CONFIG_HID_SPEEDLINK=m
  1276. +CONFIG_HID_SUNPLUS=m
  1277. +CONFIG_HID_GREENASIA=m
  1278. +CONFIG_HID_SMARTJOYPLUS=m
  1279. +CONFIG_HID_TOPSEED=m
  1280. +CONFIG_HID_THINGM=m
  1281. +CONFIG_HID_THRUSTMASTER=m
  1282. +CONFIG_HID_WACOM=m
  1283. +CONFIG_HID_WIIMOTE=m
  1284. +CONFIG_HID_XINMO=m
  1285. +CONFIG_HID_ZEROPLUS=m
  1286. +CONFIG_HID_ZYDACRON=m
  1287. +CONFIG_HID_PID=y
  1288. +CONFIG_USB_HIDDEV=y
  1289. +CONFIG_USB=y
  1290. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  1291. +CONFIG_USB_MON=m
  1292. +CONFIG_USB_DWCOTG=y
  1293. +CONFIG_USB_PRINTER=m
  1294. +CONFIG_USB_STORAGE=y
  1295. +CONFIG_USB_STORAGE_REALTEK=m
  1296. +CONFIG_USB_STORAGE_DATAFAB=m
  1297. +CONFIG_USB_STORAGE_FREECOM=m
  1298. +CONFIG_USB_STORAGE_ISD200=m
  1299. +CONFIG_USB_STORAGE_USBAT=m
  1300. +CONFIG_USB_STORAGE_SDDR09=m
  1301. +CONFIG_USB_STORAGE_SDDR55=m
  1302. +CONFIG_USB_STORAGE_JUMPSHOT=m
  1303. +CONFIG_USB_STORAGE_ALAUDA=m
  1304. +CONFIG_USB_STORAGE_ONETOUCH=m
  1305. +CONFIG_USB_STORAGE_KARMA=m
  1306. +CONFIG_USB_STORAGE_CYPRESS_ATACB=m
  1307. +CONFIG_USB_STORAGE_ENE_UB6250=m
  1308. +CONFIG_USB_MDC800=m
  1309. +CONFIG_USB_MICROTEK=m
  1310. +CONFIG_USB_SERIAL=m
  1311. +CONFIG_USB_SERIAL_GENERIC=y
  1312. +CONFIG_USB_SERIAL_AIRCABLE=m
  1313. +CONFIG_USB_SERIAL_ARK3116=m
  1314. +CONFIG_USB_SERIAL_BELKIN=m
  1315. +CONFIG_USB_SERIAL_CH341=m
  1316. +CONFIG_USB_SERIAL_WHITEHEAT=m
  1317. +CONFIG_USB_SERIAL_DIGI_ACCELEPORT=m
  1318. +CONFIG_USB_SERIAL_CP210X=m
  1319. +CONFIG_USB_SERIAL_CYPRESS_M8=m
  1320. +CONFIG_USB_SERIAL_EMPEG=m
  1321. +CONFIG_USB_SERIAL_FTDI_SIO=m
  1322. +CONFIG_USB_SERIAL_VISOR=m
  1323. +CONFIG_USB_SERIAL_IPAQ=m
  1324. +CONFIG_USB_SERIAL_IR=m
  1325. +CONFIG_USB_SERIAL_EDGEPORT=m
  1326. +CONFIG_USB_SERIAL_EDGEPORT_TI=m
  1327. +CONFIG_USB_SERIAL_F81232=m
  1328. +CONFIG_USB_SERIAL_GARMIN=m
  1329. +CONFIG_USB_SERIAL_IPW=m
  1330. +CONFIG_USB_SERIAL_IUU=m
  1331. +CONFIG_USB_SERIAL_KEYSPAN_PDA=m
  1332. +CONFIG_USB_SERIAL_KEYSPAN=m
  1333. +CONFIG_USB_SERIAL_KLSI=m
  1334. +CONFIG_USB_SERIAL_KOBIL_SCT=m
  1335. +CONFIG_USB_SERIAL_MCT_U232=m
  1336. +CONFIG_USB_SERIAL_METRO=m
  1337. +CONFIG_USB_SERIAL_MOS7720=m
  1338. +CONFIG_USB_SERIAL_MOS7840=m
  1339. +CONFIG_USB_SERIAL_NAVMAN=m
  1340. +CONFIG_USB_SERIAL_PL2303=m
  1341. +CONFIG_USB_SERIAL_OTI6858=m
  1342. +CONFIG_USB_SERIAL_QCAUX=m
  1343. +CONFIG_USB_SERIAL_QUALCOMM=m
  1344. +CONFIG_USB_SERIAL_SPCP8X5=m
  1345. +CONFIG_USB_SERIAL_SAFE=m
  1346. +CONFIG_USB_SERIAL_SIERRAWIRELESS=m
  1347. +CONFIG_USB_SERIAL_SYMBOL=m
  1348. +CONFIG_USB_SERIAL_TI=m
  1349. +CONFIG_USB_SERIAL_CYBERJACK=m
  1350. +CONFIG_USB_SERIAL_XIRCOM=m
  1351. +CONFIG_USB_SERIAL_OPTION=m
  1352. +CONFIG_USB_SERIAL_OMNINET=m
  1353. +CONFIG_USB_SERIAL_OPTICON=m
  1354. +CONFIG_USB_SERIAL_XSENS_MT=m
  1355. +CONFIG_USB_SERIAL_WISHBONE=m
  1356. +CONFIG_USB_SERIAL_ZTE=m
  1357. +CONFIG_USB_SERIAL_SSU100=m
  1358. +CONFIG_USB_SERIAL_QT2=m
  1359. +CONFIG_USB_SERIAL_DEBUG=m
  1360. +CONFIG_USB_EMI62=m
  1361. +CONFIG_USB_EMI26=m
  1362. +CONFIG_USB_ADUTUX=m
  1363. +CONFIG_USB_SEVSEG=m
  1364. +CONFIG_USB_RIO500=m
  1365. +CONFIG_USB_LEGOTOWER=m
  1366. +CONFIG_USB_LCD=m
  1367. +CONFIG_USB_LED=m
  1368. +CONFIG_USB_CYPRESS_CY7C63=m
  1369. +CONFIG_USB_CYTHERM=m
  1370. +CONFIG_USB_IDMOUSE=m
  1371. +CONFIG_USB_FTDI_ELAN=m
  1372. +CONFIG_USB_APPLEDISPLAY=m
  1373. +CONFIG_USB_LD=m
  1374. +CONFIG_USB_TRANCEVIBRATOR=m
  1375. +CONFIG_USB_IOWARRIOR=m
  1376. +CONFIG_USB_TEST=m
  1377. +CONFIG_USB_ISIGHTFW=m
  1378. +CONFIG_USB_YUREX=m
  1379. +CONFIG_USB_ATM=m
  1380. +CONFIG_USB_SPEEDTOUCH=m
  1381. +CONFIG_USB_CXACRU=m
  1382. +CONFIG_USB_UEAGLEATM=m
  1383. +CONFIG_USB_XUSBATM=m
  1384. +CONFIG_MMC=y
  1385. +CONFIG_MMC_BLOCK_MINORS=32
  1386. +CONFIG_MMC_SDHCI=y
  1387. +CONFIG_MMC_SDHCI_PLTFM=y
  1388. +CONFIG_MMC_SDHCI_BCM2708=y
  1389. +CONFIG_MMC_SDHCI_BCM2708_DMA=y
  1390. +CONFIG_MMC_BCM2835=y
  1391. +CONFIG_MMC_BCM2835_DMA=y
  1392. +CONFIG_MMC_SPI=m
  1393. +CONFIG_LEDS_GPIO=m
  1394. +CONFIG_LEDS_TRIGGER_TIMER=y
  1395. +CONFIG_LEDS_TRIGGER_ONESHOT=y
  1396. +CONFIG_LEDS_TRIGGER_HEARTBEAT=y
  1397. +CONFIG_LEDS_TRIGGER_BACKLIGHT=y
  1398. +CONFIG_LEDS_TRIGGER_CPU=y
  1399. +CONFIG_LEDS_TRIGGER_GPIO=y
  1400. +CONFIG_LEDS_TRIGGER_DEFAULT_ON=y
  1401. +CONFIG_LEDS_TRIGGER_TRANSIENT=m
  1402. +CONFIG_LEDS_TRIGGER_CAMERA=m
  1403. +CONFIG_RTC_CLASS=y
  1404. +# CONFIG_RTC_HCTOSYS is not set
  1405. +CONFIG_RTC_DRV_DS1307=m
  1406. +CONFIG_RTC_DRV_DS1374=m
  1407. +CONFIG_RTC_DRV_DS1672=m
  1408. +CONFIG_RTC_DRV_DS3232=m
  1409. +CONFIG_RTC_DRV_MAX6900=m
  1410. +CONFIG_RTC_DRV_RS5C372=m
  1411. +CONFIG_RTC_DRV_ISL1208=m
  1412. +CONFIG_RTC_DRV_ISL12022=m
  1413. +CONFIG_RTC_DRV_X1205=m
  1414. +CONFIG_RTC_DRV_PCF2127=m
  1415. +CONFIG_RTC_DRV_PCF8523=m
  1416. +CONFIG_RTC_DRV_PCF8563=m
  1417. +CONFIG_RTC_DRV_PCF8583=m
  1418. +CONFIG_RTC_DRV_M41T80=m
  1419. +CONFIG_RTC_DRV_BQ32K=m
  1420. +CONFIG_RTC_DRV_S35390A=m
  1421. +CONFIG_RTC_DRV_FM3130=m
  1422. +CONFIG_RTC_DRV_RX8581=m
  1423. +CONFIG_RTC_DRV_RX8025=m
  1424. +CONFIG_RTC_DRV_EM3027=m
  1425. +CONFIG_RTC_DRV_RV3029C2=m
  1426. +CONFIG_RTC_DRV_M41T93=m
  1427. +CONFIG_RTC_DRV_M41T94=m
  1428. +CONFIG_RTC_DRV_DS1305=m
  1429. +CONFIG_RTC_DRV_DS1390=m
  1430. +CONFIG_RTC_DRV_MAX6902=m
  1431. +CONFIG_RTC_DRV_R9701=m
  1432. +CONFIG_RTC_DRV_RS5C348=m
  1433. +CONFIG_RTC_DRV_DS3234=m
  1434. +CONFIG_RTC_DRV_PCF2123=m
  1435. +CONFIG_RTC_DRV_RX4581=m
  1436. +CONFIG_DMADEVICES=y
  1437. +CONFIG_DMA_BCM2708=y
  1438. +CONFIG_UIO=m
  1439. +CONFIG_UIO_PDRV_GENIRQ=m
  1440. +CONFIG_STAGING=y
  1441. +CONFIG_W35UND=m
  1442. +CONFIG_PRISM2_USB=m
  1443. +CONFIG_R8712U=m
  1444. +CONFIG_VT6656=m
  1445. +CONFIG_SPEAKUP=m
  1446. +CONFIG_SPEAKUP_SYNTH_SOFT=m
  1447. +CONFIG_STAGING_MEDIA=y
  1448. +CONFIG_DVB_AS102=m
  1449. +CONFIG_LIRC_STAGING=y
  1450. +CONFIG_LIRC_IGORPLUGUSB=m
  1451. +CONFIG_LIRC_IMON=m
  1452. +CONFIG_LIRC_RPI=m
  1453. +CONFIG_LIRC_SASEM=m
  1454. +CONFIG_LIRC_SERIAL=m
  1455. +# CONFIG_IOMMU_SUPPORT is not set
  1456. +CONFIG_EXT4_FS=y
  1457. +CONFIG_EXT4_FS_POSIX_ACL=y
  1458. +CONFIG_EXT4_FS_SECURITY=y
  1459. +CONFIG_REISERFS_FS=m
  1460. +CONFIG_REISERFS_FS_XATTR=y
  1461. +CONFIG_REISERFS_FS_POSIX_ACL=y
  1462. +CONFIG_REISERFS_FS_SECURITY=y
  1463. +CONFIG_JFS_FS=m
  1464. +CONFIG_JFS_POSIX_ACL=y
  1465. +CONFIG_JFS_SECURITY=y
  1466. +CONFIG_JFS_STATISTICS=y
  1467. +CONFIG_XFS_FS=m
  1468. +CONFIG_XFS_QUOTA=y
  1469. +CONFIG_XFS_POSIX_ACL=y
  1470. +CONFIG_XFS_RT=y
  1471. +CONFIG_GFS2_FS=m
  1472. +CONFIG_OCFS2_FS=m
  1473. +CONFIG_BTRFS_FS=m
  1474. +CONFIG_BTRFS_FS_POSIX_ACL=y
  1475. +CONFIG_NILFS2_FS=m
  1476. +CONFIG_FANOTIFY=y
  1477. +CONFIG_QFMT_V1=m
  1478. +CONFIG_QFMT_V2=m
  1479. +CONFIG_AUTOFS4_FS=y
  1480. +CONFIG_FUSE_FS=m
  1481. +CONFIG_CUSE=m
  1482. +CONFIG_FSCACHE=y
  1483. +CONFIG_FSCACHE_STATS=y
  1484. +CONFIG_FSCACHE_HISTOGRAM=y
  1485. +CONFIG_CACHEFILES=y
  1486. +CONFIG_ISO9660_FS=m
  1487. +CONFIG_JOLIET=y
  1488. +CONFIG_ZISOFS=y
  1489. +CONFIG_UDF_FS=m
  1490. +CONFIG_MSDOS_FS=y
  1491. +CONFIG_VFAT_FS=y
  1492. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  1493. +CONFIG_NTFS_FS=m
  1494. +CONFIG_NTFS_RW=y
  1495. +CONFIG_TMPFS=y
  1496. +CONFIG_TMPFS_POSIX_ACL=y
  1497. +CONFIG_CONFIGFS_FS=y
  1498. +CONFIG_ECRYPT_FS=m
  1499. +CONFIG_HFS_FS=m
  1500. +CONFIG_HFSPLUS_FS=m
  1501. +CONFIG_SQUASHFS=m
  1502. +CONFIG_SQUASHFS_XATTR=y
  1503. +CONFIG_SQUASHFS_LZO=y
  1504. +CONFIG_SQUASHFS_XZ=y
  1505. +CONFIG_F2FS_FS=y
  1506. +CONFIG_NFS_FS=y
  1507. +CONFIG_NFS_V3_ACL=y
  1508. +CONFIG_NFS_V4=y
  1509. +CONFIG_NFS_SWAP=y
  1510. +CONFIG_ROOT_NFS=y
  1511. +CONFIG_NFS_FSCACHE=y
  1512. +CONFIG_NFSD=m
  1513. +CONFIG_NFSD_V3_ACL=y
  1514. +CONFIG_NFSD_V4=y
  1515. +CONFIG_CIFS=m
  1516. +CONFIG_CIFS_WEAK_PW_HASH=y
  1517. +CONFIG_CIFS_XATTR=y
  1518. +CONFIG_CIFS_POSIX=y
  1519. +CONFIG_9P_FS=m
  1520. +CONFIG_9P_FS_POSIX_ACL=y
  1521. +CONFIG_NLS_DEFAULT="utf8"
  1522. +CONFIG_NLS_CODEPAGE_437=y
  1523. +CONFIG_NLS_CODEPAGE_737=m
  1524. +CONFIG_NLS_CODEPAGE_775=m
  1525. +CONFIG_NLS_CODEPAGE_850=m
  1526. +CONFIG_NLS_CODEPAGE_852=m
  1527. +CONFIG_NLS_CODEPAGE_855=m
  1528. +CONFIG_NLS_CODEPAGE_857=m
  1529. +CONFIG_NLS_CODEPAGE_860=m
  1530. +CONFIG_NLS_CODEPAGE_861=m
  1531. +CONFIG_NLS_CODEPAGE_862=m
  1532. +CONFIG_NLS_CODEPAGE_863=m
  1533. +CONFIG_NLS_CODEPAGE_864=m
  1534. +CONFIG_NLS_CODEPAGE_865=m
  1535. +CONFIG_NLS_CODEPAGE_866=m
  1536. +CONFIG_NLS_CODEPAGE_869=m
  1537. +CONFIG_NLS_CODEPAGE_936=m
  1538. +CONFIG_NLS_CODEPAGE_950=m
  1539. +CONFIG_NLS_CODEPAGE_932=m
  1540. +CONFIG_NLS_CODEPAGE_949=m
  1541. +CONFIG_NLS_CODEPAGE_874=m
  1542. +CONFIG_NLS_ISO8859_8=m
  1543. +CONFIG_NLS_CODEPAGE_1250=m
  1544. +CONFIG_NLS_CODEPAGE_1251=m
  1545. +CONFIG_NLS_ASCII=y
  1546. +CONFIG_NLS_ISO8859_1=m
  1547. +CONFIG_NLS_ISO8859_2=m
  1548. +CONFIG_NLS_ISO8859_3=m
  1549. +CONFIG_NLS_ISO8859_4=m
  1550. +CONFIG_NLS_ISO8859_5=m
  1551. +CONFIG_NLS_ISO8859_6=m
  1552. +CONFIG_NLS_ISO8859_7=m
  1553. +CONFIG_NLS_ISO8859_9=m
  1554. +CONFIG_NLS_ISO8859_13=m
  1555. +CONFIG_NLS_ISO8859_14=m
  1556. +CONFIG_NLS_ISO8859_15=m
  1557. +CONFIG_NLS_KOI8_R=m
  1558. +CONFIG_NLS_KOI8_U=m
  1559. +CONFIG_DLM=m
  1560. +CONFIG_PRINTK_TIME=y
  1561. +CONFIG_BOOT_PRINTK_DELAY=y
  1562. +CONFIG_DEBUG_FS=y
  1563. +CONFIG_DEBUG_MEMORY_INIT=y
  1564. +CONFIG_DETECT_HUNG_TASK=y
  1565. +CONFIG_TIMER_STATS=y
  1566. +# CONFIG_DEBUG_PREEMPT is not set
  1567. +CONFIG_LATENCYTOP=y
  1568. +# CONFIG_KPROBE_EVENT is not set
  1569. +CONFIG_KGDB=y
  1570. +CONFIG_KGDB_KDB=y
  1571. +CONFIG_KDB_KEYBOARD=y
  1572. +CONFIG_STRICT_DEVMEM=y
  1573. +CONFIG_CRYPTO_USER=m
  1574. +CONFIG_CRYPTO_NULL=m
  1575. +CONFIG_CRYPTO_CRYPTD=m
  1576. +CONFIG_CRYPTO_CBC=y
  1577. +CONFIG_CRYPTO_CTS=m
  1578. +CONFIG_CRYPTO_XTS=m
  1579. +CONFIG_CRYPTO_XCBC=m
  1580. +CONFIG_CRYPTO_SHA1_ARM=m
  1581. +CONFIG_CRYPTO_SHA512=m
  1582. +CONFIG_CRYPTO_TGR192=m
  1583. +CONFIG_CRYPTO_WP512=m
  1584. +CONFIG_CRYPTO_AES_ARM=m
  1585. +CONFIG_CRYPTO_CAST5=m
  1586. +CONFIG_CRYPTO_DES=y
  1587. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  1588. +# CONFIG_CRYPTO_HW is not set
  1589. +CONFIG_CRC_ITU_T=y
  1590. +CONFIG_LIBCRC32C=y
  1591. diff -Nur linux-3.12.33/arch/arm/configs/bcmrpi_emergency_defconfig linux-3.12.33-rpi/arch/arm/configs/bcmrpi_emergency_defconfig
  1592. --- linux-3.12.33/arch/arm/configs/bcmrpi_emergency_defconfig 1969-12-31 18:00:00.000000000 -0600
  1593. +++ linux-3.12.33-rpi/arch/arm/configs/bcmrpi_emergency_defconfig 2014-12-03 19:13:32.288418001 -0600
  1594. @@ -0,0 +1,495 @@
  1595. +# CONFIG_LOCALVERSION_AUTO is not set
  1596. +CONFIG_SYSVIPC=y
  1597. +CONFIG_POSIX_MQUEUE=y
  1598. +CONFIG_FHANDLE=y
  1599. +CONFIG_AUDIT=y
  1600. +CONFIG_NO_HZ=y
  1601. +CONFIG_HIGH_RES_TIMERS=y
  1602. +CONFIG_BSD_PROCESS_ACCT=y
  1603. +CONFIG_BSD_PROCESS_ACCT_V3=y
  1604. +CONFIG_IKCONFIG=y
  1605. +CONFIG_IKCONFIG_PROC=y
  1606. +CONFIG_CGROUP_FREEZER=y
  1607. +CONFIG_CGROUP_DEVICE=y
  1608. +CONFIG_CGROUP_CPUACCT=y
  1609. +CONFIG_RESOURCE_COUNTERS=y
  1610. +CONFIG_BLK_CGROUP=y
  1611. +CONFIG_NAMESPACES=y
  1612. +CONFIG_SCHED_AUTOGROUP=y
  1613. +CONFIG_BLK_DEV_INITRD=y
  1614. +CONFIG_INITRAMFS_SOURCE="../target_fs"
  1615. +CONFIG_EMBEDDED=y
  1616. +# CONFIG_COMPAT_BRK is not set
  1617. +CONFIG_SLAB=y
  1618. +CONFIG_PROFILING=y
  1619. +CONFIG_OPROFILE=m
  1620. +CONFIG_KPROBES=y
  1621. +CONFIG_MODULES=y
  1622. +CONFIG_MODULE_UNLOAD=y
  1623. +CONFIG_MODVERSIONS=y
  1624. +CONFIG_MODULE_SRCVERSION_ALL=y
  1625. +# CONFIG_BLK_DEV_BSG is not set
  1626. +CONFIG_BLK_DEV_THROTTLING=y
  1627. +CONFIG_PARTITION_ADVANCED=y
  1628. +CONFIG_MAC_PARTITION=y
  1629. +CONFIG_CFQ_GROUP_IOSCHED=y
  1630. +CONFIG_ARCH_BCM2708=y
  1631. +CONFIG_AEABI=y
  1632. +CONFIG_SECCOMP=y
  1633. +CONFIG_CC_STACKPROTECTOR=y
  1634. +CONFIG_ZBOOT_ROM_TEXT=0x0
  1635. +CONFIG_ZBOOT_ROM_BSS=0x0
  1636. +CONFIG_CMDLINE="dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext3 rootwait"
  1637. +CONFIG_KEXEC=y
  1638. +CONFIG_CPU_IDLE=y
  1639. +CONFIG_VFP=y
  1640. +CONFIG_BINFMT_MISC=m
  1641. +CONFIG_NET=y
  1642. +CONFIG_PACKET=y
  1643. +CONFIG_UNIX=y
  1644. +CONFIG_XFRM_USER=y
  1645. +CONFIG_NET_KEY=m
  1646. +CONFIG_INET=y
  1647. +CONFIG_IP_MULTICAST=y
  1648. +CONFIG_IP_PNP=y
  1649. +CONFIG_IP_PNP_DHCP=y
  1650. +CONFIG_IP_PNP_RARP=y
  1651. +CONFIG_SYN_COOKIES=y
  1652. +# CONFIG_INET_XFRM_MODE_TRANSPORT is not set
  1653. +# CONFIG_INET_XFRM_MODE_TUNNEL is not set
  1654. +# CONFIG_INET_XFRM_MODE_BEET is not set
  1655. +# CONFIG_INET_LRO is not set
  1656. +# CONFIG_INET_DIAG is not set
  1657. +# CONFIG_IPV6 is not set
  1658. +CONFIG_NET_PKTGEN=m
  1659. +CONFIG_IRDA=m
  1660. +CONFIG_IRLAN=m
  1661. +CONFIG_IRCOMM=m
  1662. +CONFIG_IRDA_ULTRA=y
  1663. +CONFIG_IRDA_CACHE_LAST_LSAP=y
  1664. +CONFIG_IRDA_FAST_RR=y
  1665. +CONFIG_IRTTY_SIR=m
  1666. +CONFIG_KINGSUN_DONGLE=m
  1667. +CONFIG_KSDAZZLE_DONGLE=m
  1668. +CONFIG_KS959_DONGLE=m
  1669. +CONFIG_USB_IRDA=m
  1670. +CONFIG_SIGMATEL_FIR=m
  1671. +CONFIG_MCS_FIR=m
  1672. +CONFIG_BT=m
  1673. +CONFIG_BT_RFCOMM=m
  1674. +CONFIG_BT_RFCOMM_TTY=y
  1675. +CONFIG_BT_BNEP=m
  1676. +CONFIG_BT_BNEP_MC_FILTER=y
  1677. +CONFIG_BT_BNEP_PROTO_FILTER=y
  1678. +CONFIG_BT_HIDP=m
  1679. +CONFIG_BT_HCIBTUSB=m
  1680. +CONFIG_BT_HCIBCM203X=m
  1681. +CONFIG_BT_HCIBPA10X=m
  1682. +CONFIG_BT_HCIBFUSB=m
  1683. +CONFIG_BT_HCIVHCI=m
  1684. +CONFIG_BT_MRVL=m
  1685. +CONFIG_BT_MRVL_SDIO=m
  1686. +CONFIG_BT_ATH3K=m
  1687. +CONFIG_CFG80211=m
  1688. +CONFIG_MAC80211=m
  1689. +CONFIG_MAC80211_RC_PID=y
  1690. +CONFIG_MAC80211_MESH=y
  1691. +CONFIG_WIMAX=m
  1692. +CONFIG_NET_9P=m
  1693. +CONFIG_NFC=m
  1694. +CONFIG_NFC_PN533=m
  1695. +CONFIG_UEVENT_HELPER_PATH="/sbin/hotplug"
  1696. +CONFIG_BLK_DEV_LOOP=y
  1697. +CONFIG_BLK_DEV_CRYPTOLOOP=m
  1698. +CONFIG_BLK_DEV_NBD=m
  1699. +CONFIG_BLK_DEV_RAM=y
  1700. +CONFIG_CDROM_PKTCDVD=m
  1701. +CONFIG_SCSI=y
  1702. +# CONFIG_SCSI_PROC_FS is not set
  1703. +CONFIG_BLK_DEV_SD=y
  1704. +CONFIG_BLK_DEV_SR=m
  1705. +CONFIG_SCSI_MULTI_LUN=y
  1706. +# CONFIG_SCSI_LOWLEVEL is not set
  1707. +CONFIG_MD=y
  1708. +CONFIG_NETDEVICES=y
  1709. +CONFIG_NETCONSOLE=m
  1710. +CONFIG_TUN=m
  1711. +CONFIG_MDIO_BITBANG=m
  1712. +CONFIG_PPP=m
  1713. +CONFIG_PPP_BSDCOMP=m
  1714. +CONFIG_PPP_DEFLATE=m
  1715. +CONFIG_PPP_ASYNC=m
  1716. +CONFIG_PPP_SYNC_TTY=m
  1717. +CONFIG_SLIP=m
  1718. +CONFIG_SLIP_COMPRESSED=y
  1719. +CONFIG_USB_CATC=m
  1720. +CONFIG_USB_KAWETH=m
  1721. +CONFIG_USB_PEGASUS=m
  1722. +CONFIG_USB_RTL8150=m
  1723. +CONFIG_USB_USBNET=y
  1724. +CONFIG_USB_NET_AX8817X=m
  1725. +CONFIG_USB_NET_CDCETHER=m
  1726. +CONFIG_USB_NET_CDC_EEM=m
  1727. +CONFIG_USB_NET_DM9601=m
  1728. +CONFIG_USB_NET_SMSC75XX=m
  1729. +CONFIG_USB_NET_SMSC95XX=y
  1730. +CONFIG_USB_NET_GL620A=m
  1731. +CONFIG_USB_NET_NET1080=m
  1732. +CONFIG_USB_NET_PLUSB=m
  1733. +CONFIG_USB_NET_MCS7830=m
  1734. +CONFIG_USB_NET_CDC_SUBSET=m
  1735. +CONFIG_USB_ALI_M5632=y
  1736. +CONFIG_USB_AN2720=y
  1737. +CONFIG_USB_KC2190=y
  1738. +# CONFIG_USB_NET_ZAURUS is not set
  1739. +CONFIG_USB_NET_CX82310_ETH=m
  1740. +CONFIG_USB_NET_KALMIA=m
  1741. +CONFIG_USB_NET_INT51X1=m
  1742. +CONFIG_USB_IPHETH=m
  1743. +CONFIG_USB_SIERRA_NET=m
  1744. +CONFIG_USB_VL600=m
  1745. +CONFIG_LIBERTAS_THINFIRM=m
  1746. +CONFIG_LIBERTAS_THINFIRM_USB=m
  1747. +CONFIG_AT76C50X_USB=m
  1748. +CONFIG_USB_ZD1201=m
  1749. +CONFIG_USB_NET_RNDIS_WLAN=m
  1750. +CONFIG_RTL8187=m
  1751. +CONFIG_MAC80211_HWSIM=m
  1752. +CONFIG_B43=m
  1753. +CONFIG_B43LEGACY=m
  1754. +CONFIG_HOSTAP=m
  1755. +CONFIG_LIBERTAS=m
  1756. +CONFIG_LIBERTAS_USB=m
  1757. +CONFIG_LIBERTAS_SDIO=m
  1758. +CONFIG_P54_COMMON=m
  1759. +CONFIG_P54_USB=m
  1760. +CONFIG_RT2X00=m
  1761. +CONFIG_RT2500USB=m
  1762. +CONFIG_RT73USB=m
  1763. +CONFIG_RT2800USB=m
  1764. +CONFIG_RT2800USB_RT53XX=y
  1765. +CONFIG_RTL8192CU=m
  1766. +CONFIG_ZD1211RW=m
  1767. +CONFIG_MWIFIEX=m
  1768. +CONFIG_MWIFIEX_SDIO=m
  1769. +CONFIG_WIMAX_I2400M_USB=m
  1770. +CONFIG_INPUT_POLLDEV=m
  1771. +# CONFIG_INPUT_MOUSEDEV_PSAUX is not set
  1772. +CONFIG_INPUT_JOYDEV=m
  1773. +CONFIG_INPUT_EVDEV=m
  1774. +# CONFIG_INPUT_KEYBOARD is not set
  1775. +# CONFIG_INPUT_MOUSE is not set
  1776. +CONFIG_INPUT_MISC=y
  1777. +CONFIG_INPUT_AD714X=m
  1778. +CONFIG_INPUT_ATI_REMOTE2=m
  1779. +CONFIG_INPUT_KEYSPAN_REMOTE=m
  1780. +CONFIG_INPUT_POWERMATE=m
  1781. +CONFIG_INPUT_YEALINK=m
  1782. +CONFIG_INPUT_CM109=m
  1783. +CONFIG_INPUT_UINPUT=m
  1784. +CONFIG_INPUT_GPIO_ROTARY_ENCODER=m
  1785. +CONFIG_INPUT_ADXL34X=m
  1786. +CONFIG_INPUT_CMA3000=m
  1787. +CONFIG_SERIO=m
  1788. +CONFIG_SERIO_RAW=m
  1789. +CONFIG_GAMEPORT=m
  1790. +CONFIG_GAMEPORT_NS558=m
  1791. +CONFIG_GAMEPORT_L4=m
  1792. +# CONFIG_LEGACY_PTYS is not set
  1793. +# CONFIG_DEVKMEM is not set
  1794. +CONFIG_SERIAL_AMBA_PL011=y
  1795. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  1796. +# CONFIG_HW_RANDOM is not set
  1797. +CONFIG_RAW_DRIVER=y
  1798. +CONFIG_GPIO_SYSFS=y
  1799. +# CONFIG_HWMON is not set
  1800. +CONFIG_WATCHDOG=y
  1801. +CONFIG_BCM2708_WDT=m
  1802. +CONFIG_FB=y
  1803. +CONFIG_FB_BCM2708=y
  1804. +CONFIG_FRAMEBUFFER_CONSOLE=y
  1805. +CONFIG_LOGO=y
  1806. +# CONFIG_LOGO_LINUX_MONO is not set
  1807. +# CONFIG_LOGO_LINUX_VGA16 is not set
  1808. +CONFIG_SOUND=y
  1809. +CONFIG_SND=m
  1810. +CONFIG_SND_SEQUENCER=m
  1811. +CONFIG_SND_SEQ_DUMMY=m
  1812. +CONFIG_SND_MIXER_OSS=m
  1813. +CONFIG_SND_PCM_OSS=m
  1814. +CONFIG_SND_SEQUENCER_OSS=y
  1815. +CONFIG_SND_HRTIMER=m
  1816. +CONFIG_SND_DUMMY=m
  1817. +CONFIG_SND_ALOOP=m
  1818. +CONFIG_SND_VIRMIDI=m
  1819. +CONFIG_SND_MTPAV=m
  1820. +CONFIG_SND_SERIAL_U16550=m
  1821. +CONFIG_SND_MPU401=m
  1822. +CONFIG_SND_BCM2835=m
  1823. +CONFIG_SND_USB_AUDIO=m
  1824. +CONFIG_SND_USB_UA101=m
  1825. +CONFIG_SND_USB_CAIAQ=m
  1826. +CONFIG_SND_USB_6FIRE=m
  1827. +CONFIG_SOUND_PRIME=m
  1828. +CONFIG_HID_A4TECH=m
  1829. +CONFIG_HID_ACRUX=m
  1830. +CONFIG_HID_APPLE=m
  1831. +CONFIG_HID_BELKIN=m
  1832. +CONFIG_HID_CHERRY=m
  1833. +CONFIG_HID_CHICONY=m
  1834. +CONFIG_HID_CYPRESS=m
  1835. +CONFIG_HID_DRAGONRISE=m
  1836. +CONFIG_HID_EMS_FF=m
  1837. +CONFIG_HID_ELECOM=m
  1838. +CONFIG_HID_EZKEY=m
  1839. +CONFIG_HID_HOLTEK=m
  1840. +CONFIG_HID_KEYTOUCH=m
  1841. +CONFIG_HID_KYE=m
  1842. +CONFIG_HID_UCLOGIC=m
  1843. +CONFIG_HID_WALTOP=m
  1844. +CONFIG_HID_GYRATION=m
  1845. +CONFIG_HID_TWINHAN=m
  1846. +CONFIG_HID_KENSINGTON=m
  1847. +CONFIG_HID_LCPOWER=m
  1848. +CONFIG_HID_LOGITECH=m
  1849. +CONFIG_HID_MAGICMOUSE=m
  1850. +CONFIG_HID_MICROSOFT=m
  1851. +CONFIG_HID_MONTEREY=m
  1852. +CONFIG_HID_MULTITOUCH=m
  1853. +CONFIG_HID_NTRIG=m
  1854. +CONFIG_HID_ORTEK=m
  1855. +CONFIG_HID_PANTHERLORD=m
  1856. +CONFIG_HID_PETALYNX=m
  1857. +CONFIG_HID_PICOLCD=m
  1858. +CONFIG_HID_ROCCAT=m
  1859. +CONFIG_HID_SAMSUNG=m
  1860. +CONFIG_HID_SPEEDLINK=m
  1861. +CONFIG_HID_SUNPLUS=m
  1862. +CONFIG_HID_GREENASIA=m
  1863. +CONFIG_HID_SMARTJOYPLUS=m
  1864. +CONFIG_HID_TOPSEED=m
  1865. +CONFIG_HID_THRUSTMASTER=m
  1866. +CONFIG_HID_ZEROPLUS=m
  1867. +CONFIG_HID_ZYDACRON=m
  1868. +CONFIG_HID_PID=y
  1869. +CONFIG_USB_HIDDEV=y
  1870. +CONFIG_USB=y
  1871. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  1872. +CONFIG_USB_MON=m
  1873. +CONFIG_USB_DWCOTG=y
  1874. +CONFIG_USB_STORAGE=y
  1875. +CONFIG_USB_STORAGE_REALTEK=m
  1876. +CONFIG_USB_STORAGE_DATAFAB=m
  1877. +CONFIG_USB_STORAGE_FREECOM=m
  1878. +CONFIG_USB_STORAGE_ISD200=m
  1879. +CONFIG_USB_STORAGE_USBAT=m
  1880. +CONFIG_USB_STORAGE_SDDR09=m
  1881. +CONFIG_USB_STORAGE_SDDR55=m
  1882. +CONFIG_USB_STORAGE_JUMPSHOT=m
  1883. +CONFIG_USB_STORAGE_ALAUDA=m
  1884. +CONFIG_USB_STORAGE_ONETOUCH=m
  1885. +CONFIG_USB_STORAGE_KARMA=m
  1886. +CONFIG_USB_STORAGE_CYPRESS_ATACB=m
  1887. +CONFIG_USB_STORAGE_ENE_UB6250=m
  1888. +CONFIG_USB_MDC800=m
  1889. +CONFIG_USB_MICROTEK=m
  1890. +CONFIG_USB_SERIAL=m
  1891. +CONFIG_USB_SERIAL_GENERIC=y
  1892. +CONFIG_USB_SERIAL_AIRCABLE=m
  1893. +CONFIG_USB_SERIAL_ARK3116=m
  1894. +CONFIG_USB_SERIAL_BELKIN=m
  1895. +CONFIG_USB_SERIAL_CH341=m
  1896. +CONFIG_USB_SERIAL_WHITEHEAT=m
  1897. +CONFIG_USB_SERIAL_DIGI_ACCELEPORT=m
  1898. +CONFIG_USB_SERIAL_CP210X=m
  1899. +CONFIG_USB_SERIAL_CYPRESS_M8=m
  1900. +CONFIG_USB_SERIAL_EMPEG=m
  1901. +CONFIG_USB_SERIAL_FTDI_SIO=m
  1902. +CONFIG_USB_SERIAL_VISOR=m
  1903. +CONFIG_USB_SERIAL_IPAQ=m
  1904. +CONFIG_USB_SERIAL_IR=m
  1905. +CONFIG_USB_SERIAL_EDGEPORT=m
  1906. +CONFIG_USB_SERIAL_EDGEPORT_TI=m
  1907. +CONFIG_USB_SERIAL_GARMIN=m
  1908. +CONFIG_USB_SERIAL_IPW=m
  1909. +CONFIG_USB_SERIAL_IUU=m
  1910. +CONFIG_USB_SERIAL_KEYSPAN_PDA=m
  1911. +CONFIG_USB_SERIAL_KEYSPAN=m
  1912. +CONFIG_USB_SERIAL_KLSI=m
  1913. +CONFIG_USB_SERIAL_KOBIL_SCT=m
  1914. +CONFIG_USB_SERIAL_MCT_U232=m
  1915. +CONFIG_USB_SERIAL_MOS7720=m
  1916. +CONFIG_USB_SERIAL_MOS7840=m
  1917. +CONFIG_USB_SERIAL_NAVMAN=m
  1918. +CONFIG_USB_SERIAL_PL2303=m
  1919. +CONFIG_USB_SERIAL_OTI6858=m
  1920. +CONFIG_USB_SERIAL_QCAUX=m
  1921. +CONFIG_USB_SERIAL_QUALCOMM=m
  1922. +CONFIG_USB_SERIAL_SPCP8X5=m
  1923. +CONFIG_USB_SERIAL_SAFE=m
  1924. +CONFIG_USB_SERIAL_SIERRAWIRELESS=m
  1925. +CONFIG_USB_SERIAL_SYMBOL=m
  1926. +CONFIG_USB_SERIAL_TI=m
  1927. +CONFIG_USB_SERIAL_CYBERJACK=m
  1928. +CONFIG_USB_SERIAL_XIRCOM=m
  1929. +CONFIG_USB_SERIAL_OPTION=m
  1930. +CONFIG_USB_SERIAL_OMNINET=m
  1931. +CONFIG_USB_SERIAL_OPTICON=m
  1932. +CONFIG_USB_SERIAL_SSU100=m
  1933. +CONFIG_USB_SERIAL_DEBUG=m
  1934. +CONFIG_USB_EMI62=m
  1935. +CONFIG_USB_EMI26=m
  1936. +CONFIG_USB_ADUTUX=m
  1937. +CONFIG_USB_SEVSEG=m
  1938. +CONFIG_USB_RIO500=m
  1939. +CONFIG_USB_LEGOTOWER=m
  1940. +CONFIG_USB_LCD=m
  1941. +CONFIG_USB_LED=m
  1942. +CONFIG_USB_CYPRESS_CY7C63=m
  1943. +CONFIG_USB_CYTHERM=m
  1944. +CONFIG_USB_IDMOUSE=m
  1945. +CONFIG_USB_FTDI_ELAN=m
  1946. +CONFIG_USB_APPLEDISPLAY=m
  1947. +CONFIG_USB_LD=m
  1948. +CONFIG_USB_TRANCEVIBRATOR=m
  1949. +CONFIG_USB_IOWARRIOR=m
  1950. +CONFIG_USB_TEST=m
  1951. +CONFIG_USB_ISIGHTFW=m
  1952. +CONFIG_USB_YUREX=m
  1953. +CONFIG_MMC=y
  1954. +CONFIG_MMC_SDHCI=y
  1955. +CONFIG_MMC_SDHCI_PLTFM=y
  1956. +CONFIG_MMC_SDHCI_BCM2708=y
  1957. +CONFIG_MMC_SDHCI_BCM2708_DMA=y
  1958. +CONFIG_MMC_BCM2835=y
  1959. +CONFIG_MMC_BCM2835_DMA=y
  1960. +CONFIG_UIO=m
  1961. +CONFIG_UIO_PDRV_GENIRQ=m
  1962. +# CONFIG_IOMMU_SUPPORT is not set
  1963. +CONFIG_EXT4_FS=y
  1964. +CONFIG_EXT4_FS_POSIX_ACL=y
  1965. +CONFIG_EXT4_FS_SECURITY=y
  1966. +CONFIG_REISERFS_FS=m
  1967. +CONFIG_REISERFS_FS_XATTR=y
  1968. +CONFIG_REISERFS_FS_POSIX_ACL=y
  1969. +CONFIG_REISERFS_FS_SECURITY=y
  1970. +CONFIG_JFS_FS=m
  1971. +CONFIG_JFS_POSIX_ACL=y
  1972. +CONFIG_JFS_SECURITY=y
  1973. +CONFIG_JFS_STATISTICS=y
  1974. +CONFIG_XFS_FS=m
  1975. +CONFIG_XFS_QUOTA=y
  1976. +CONFIG_XFS_POSIX_ACL=y
  1977. +CONFIG_XFS_RT=y
  1978. +CONFIG_GFS2_FS=m
  1979. +CONFIG_OCFS2_FS=m
  1980. +CONFIG_BTRFS_FS=m
  1981. +CONFIG_BTRFS_FS_POSIX_ACL=y
  1982. +CONFIG_NILFS2_FS=m
  1983. +CONFIG_FANOTIFY=y
  1984. +CONFIG_AUTOFS4_FS=y
  1985. +CONFIG_FUSE_FS=m
  1986. +CONFIG_CUSE=m
  1987. +CONFIG_FSCACHE=y
  1988. +CONFIG_FSCACHE_STATS=y
  1989. +CONFIG_FSCACHE_HISTOGRAM=y
  1990. +CONFIG_CACHEFILES=y
  1991. +CONFIG_ISO9660_FS=m
  1992. +CONFIG_JOLIET=y
  1993. +CONFIG_ZISOFS=y
  1994. +CONFIG_UDF_FS=m
  1995. +CONFIG_MSDOS_FS=y
  1996. +CONFIG_VFAT_FS=y
  1997. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  1998. +CONFIG_NTFS_FS=m
  1999. +CONFIG_TMPFS=y
  2000. +CONFIG_TMPFS_POSIX_ACL=y
  2001. +CONFIG_CONFIGFS_FS=y
  2002. +CONFIG_SQUASHFS=m
  2003. +CONFIG_SQUASHFS_XATTR=y
  2004. +CONFIG_SQUASHFS_LZO=y
  2005. +CONFIG_SQUASHFS_XZ=y
  2006. +CONFIG_NFS_FS=y
  2007. +CONFIG_NFS_V3_ACL=y
  2008. +CONFIG_NFS_V4=y
  2009. +CONFIG_ROOT_NFS=y
  2010. +CONFIG_NFS_FSCACHE=y
  2011. +CONFIG_CIFS=m
  2012. +CONFIG_CIFS_WEAK_PW_HASH=y
  2013. +CONFIG_CIFS_XATTR=y
  2014. +CONFIG_CIFS_POSIX=y
  2015. +CONFIG_9P_FS=m
  2016. +CONFIG_9P_FS_POSIX_ACL=y
  2017. +CONFIG_NLS_DEFAULT="utf8"
  2018. +CONFIG_NLS_CODEPAGE_437=y
  2019. +CONFIG_NLS_CODEPAGE_737=m
  2020. +CONFIG_NLS_CODEPAGE_775=m
  2021. +CONFIG_NLS_CODEPAGE_850=m
  2022. +CONFIG_NLS_CODEPAGE_852=m
  2023. +CONFIG_NLS_CODEPAGE_855=m
  2024. +CONFIG_NLS_CODEPAGE_857=m
  2025. +CONFIG_NLS_CODEPAGE_860=m
  2026. +CONFIG_NLS_CODEPAGE_861=m
  2027. +CONFIG_NLS_CODEPAGE_862=m
  2028. +CONFIG_NLS_CODEPAGE_863=m
  2029. +CONFIG_NLS_CODEPAGE_864=m
  2030. +CONFIG_NLS_CODEPAGE_865=m
  2031. +CONFIG_NLS_CODEPAGE_866=m
  2032. +CONFIG_NLS_CODEPAGE_869=m
  2033. +CONFIG_NLS_CODEPAGE_936=m
  2034. +CONFIG_NLS_CODEPAGE_950=m
  2035. +CONFIG_NLS_CODEPAGE_932=m
  2036. +CONFIG_NLS_CODEPAGE_949=m
  2037. +CONFIG_NLS_CODEPAGE_874=m
  2038. +CONFIG_NLS_ISO8859_8=m
  2039. +CONFIG_NLS_CODEPAGE_1250=m
  2040. +CONFIG_NLS_CODEPAGE_1251=m
  2041. +CONFIG_NLS_ASCII=y
  2042. +CONFIG_NLS_ISO8859_1=m
  2043. +CONFIG_NLS_ISO8859_2=m
  2044. +CONFIG_NLS_ISO8859_3=m
  2045. +CONFIG_NLS_ISO8859_4=m
  2046. +CONFIG_NLS_ISO8859_5=m
  2047. +CONFIG_NLS_ISO8859_6=m
  2048. +CONFIG_NLS_ISO8859_7=m
  2049. +CONFIG_NLS_ISO8859_9=m
  2050. +CONFIG_NLS_ISO8859_13=m
  2051. +CONFIG_NLS_ISO8859_14=m
  2052. +CONFIG_NLS_ISO8859_15=m
  2053. +CONFIG_NLS_KOI8_R=m
  2054. +CONFIG_NLS_KOI8_U=m
  2055. +CONFIG_NLS_UTF8=m
  2056. +CONFIG_PRINTK_TIME=y
  2057. +CONFIG_BOOT_PRINTK_DELAY=y
  2058. +CONFIG_DEBUG_INFO=y
  2059. +CONFIG_DEBUG_STACK_USAGE=y
  2060. +CONFIG_DEBUG_MEMORY_INIT=y
  2061. +CONFIG_DETECT_HUNG_TASK=y
  2062. +CONFIG_TIMER_STATS=y
  2063. +CONFIG_LATENCYTOP=y
  2064. +CONFIG_IRQSOFF_TRACER=y
  2065. +CONFIG_SCHED_TRACER=y
  2066. +CONFIG_STACK_TRACER=y
  2067. +CONFIG_BLK_DEV_IO_TRACE=y
  2068. +CONFIG_FUNCTION_PROFILER=y
  2069. +CONFIG_KGDB=y
  2070. +CONFIG_KGDB_KDB=y
  2071. +CONFIG_KDB_KEYBOARD=y
  2072. +CONFIG_STRICT_DEVMEM=y
  2073. +CONFIG_CRYPTO_AUTHENC=m
  2074. +CONFIG_CRYPTO_SEQIV=m
  2075. +CONFIG_CRYPTO_CBC=y
  2076. +CONFIG_CRYPTO_HMAC=y
  2077. +CONFIG_CRYPTO_XCBC=m
  2078. +CONFIG_CRYPTO_MD5=y
  2079. +CONFIG_CRYPTO_SHA1=y
  2080. +CONFIG_CRYPTO_SHA512=m
  2081. +CONFIG_CRYPTO_TGR192=m
  2082. +CONFIG_CRYPTO_WP512=m
  2083. +CONFIG_CRYPTO_CAST5=m
  2084. +CONFIG_CRYPTO_DES=y
  2085. +CONFIG_CRYPTO_DEFLATE=m
  2086. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  2087. +# CONFIG_CRYPTO_HW is not set
  2088. +CONFIG_CRC_ITU_T=y
  2089. +CONFIG_LIBCRC32C=y
  2090. diff -Nur linux-3.12.33/arch/arm/configs/bcmrpi_quick_defconfig linux-3.12.33-rpi/arch/arm/configs/bcmrpi_quick_defconfig
  2091. --- linux-3.12.33/arch/arm/configs/bcmrpi_quick_defconfig 1969-12-31 18:00:00.000000000 -0600
  2092. +++ linux-3.12.33-rpi/arch/arm/configs/bcmrpi_quick_defconfig 2014-12-03 19:13:32.288418001 -0600
  2093. @@ -0,0 +1,198 @@
  2094. +# CONFIG_ARM_PATCH_PHYS_VIRT is not set
  2095. +CONFIG_LOCALVERSION="-quick"
  2096. +# CONFIG_LOCALVERSION_AUTO is not set
  2097. +# CONFIG_SWAP is not set
  2098. +CONFIG_SYSVIPC=y
  2099. +CONFIG_POSIX_MQUEUE=y
  2100. +CONFIG_NO_HZ=y
  2101. +CONFIG_HIGH_RES_TIMERS=y
  2102. +CONFIG_IKCONFIG=y
  2103. +CONFIG_IKCONFIG_PROC=y
  2104. +CONFIG_KALLSYMS_ALL=y
  2105. +CONFIG_EMBEDDED=y
  2106. +CONFIG_PERF_EVENTS=y
  2107. +# CONFIG_COMPAT_BRK is not set
  2108. +CONFIG_SLAB=y
  2109. +CONFIG_MODULES=y
  2110. +CONFIG_MODULE_UNLOAD=y
  2111. +CONFIG_MODVERSIONS=y
  2112. +CONFIG_MODULE_SRCVERSION_ALL=y
  2113. +# CONFIG_BLK_DEV_BSG is not set
  2114. +CONFIG_ARCH_BCM2708=y
  2115. +CONFIG_PREEMPT=y
  2116. +CONFIG_AEABI=y
  2117. +CONFIG_UACCESS_WITH_MEMCPY=y
  2118. +CONFIG_ZBOOT_ROM_TEXT=0x0
  2119. +CONFIG_ZBOOT_ROM_BSS=0x0
  2120. +CONFIG_CMDLINE="dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait"
  2121. +CONFIG_CPU_FREQ=y
  2122. +CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE=y
  2123. +CONFIG_CPU_FREQ_GOV_PERFORMANCE=y
  2124. +CONFIG_CPU_FREQ_GOV_USERSPACE=y
  2125. +CONFIG_CPU_FREQ_GOV_ONDEMAND=y
  2126. +CONFIG_CPU_FREQ_GOV_CONSERVATIVE=y
  2127. +CONFIG_CPU_IDLE=y
  2128. +CONFIG_VFP=y
  2129. +CONFIG_BINFMT_MISC=y
  2130. +CONFIG_NET=y
  2131. +CONFIG_PACKET=y
  2132. +CONFIG_UNIX=y
  2133. +CONFIG_INET=y
  2134. +CONFIG_IP_MULTICAST=y
  2135. +CONFIG_IP_PNP=y
  2136. +CONFIG_IP_PNP_DHCP=y
  2137. +CONFIG_IP_PNP_RARP=y
  2138. +CONFIG_SYN_COOKIES=y
  2139. +# CONFIG_INET_XFRM_MODE_TRANSPORT is not set
  2140. +# CONFIG_INET_XFRM_MODE_TUNNEL is not set
  2141. +# CONFIG_INET_XFRM_MODE_BEET is not set
  2142. +# CONFIG_INET_LRO is not set
  2143. +# CONFIG_INET_DIAG is not set
  2144. +# CONFIG_IPV6 is not set
  2145. +# CONFIG_WIRELESS is not set
  2146. +CONFIG_DEVTMPFS=y
  2147. +CONFIG_DEVTMPFS_MOUNT=y
  2148. +CONFIG_BLK_DEV_LOOP=y
  2149. +CONFIG_BLK_DEV_RAM=y
  2150. +CONFIG_SCSI=y
  2151. +# CONFIG_SCSI_PROC_FS is not set
  2152. +# CONFIG_SCSI_LOWLEVEL is not set
  2153. +CONFIG_NETDEVICES=y
  2154. +# CONFIG_NET_VENDOR_BROADCOM is not set
  2155. +# CONFIG_NET_VENDOR_CIRRUS is not set
  2156. +# CONFIG_NET_VENDOR_FARADAY is not set
  2157. +# CONFIG_NET_VENDOR_INTEL is not set
  2158. +# CONFIG_NET_VENDOR_MARVELL is not set
  2159. +# CONFIG_NET_VENDOR_MICREL is not set
  2160. +# CONFIG_NET_VENDOR_NATSEMI is not set
  2161. +# CONFIG_NET_VENDOR_SEEQ is not set
  2162. +# CONFIG_NET_VENDOR_STMICRO is not set
  2163. +# CONFIG_NET_VENDOR_WIZNET is not set
  2164. +CONFIG_USB_USBNET=y
  2165. +# CONFIG_USB_NET_AX8817X is not set
  2166. +# CONFIG_USB_NET_CDCETHER is not set
  2167. +# CONFIG_USB_NET_CDC_NCM is not set
  2168. +CONFIG_USB_NET_SMSC95XX=y
  2169. +# CONFIG_USB_NET_NET1080 is not set
  2170. +# CONFIG_USB_NET_CDC_SUBSET is not set
  2171. +# CONFIG_USB_NET_ZAURUS is not set
  2172. +# CONFIG_WLAN is not set
  2173. +# CONFIG_INPUT_MOUSEDEV is not set
  2174. +CONFIG_INPUT_EVDEV=y
  2175. +# CONFIG_INPUT_KEYBOARD is not set
  2176. +# CONFIG_INPUT_MOUSE is not set
  2177. +# CONFIG_SERIO is not set
  2178. +# CONFIG_LEGACY_PTYS is not set
  2179. +# CONFIG_DEVKMEM is not set
  2180. +CONFIG_SERIAL_AMBA_PL011=y
  2181. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  2182. +CONFIG_TTY_PRINTK=y
  2183. +CONFIG_HW_RANDOM=y
  2184. +CONFIG_HW_RANDOM_BCM2708=y
  2185. +CONFIG_RAW_DRIVER=y
  2186. +CONFIG_THERMAL=y
  2187. +CONFIG_THERMAL_BCM2835=y
  2188. +CONFIG_WATCHDOG=y
  2189. +CONFIG_BCM2708_WDT=y
  2190. +CONFIG_REGULATOR=y
  2191. +CONFIG_REGULATOR_DEBUG=y
  2192. +CONFIG_REGULATOR_FIXED_VOLTAGE=y
  2193. +CONFIG_REGULATOR_VIRTUAL_CONSUMER=y
  2194. +CONFIG_REGULATOR_USERSPACE_CONSUMER=y
  2195. +CONFIG_FB=y
  2196. +CONFIG_FB_BCM2708=y
  2197. +CONFIG_FRAMEBUFFER_CONSOLE=y
  2198. +CONFIG_LOGO=y
  2199. +# CONFIG_LOGO_LINUX_MONO is not set
  2200. +# CONFIG_LOGO_LINUX_VGA16 is not set
  2201. +CONFIG_SOUND=y
  2202. +CONFIG_SND=y
  2203. +CONFIG_SND_BCM2835=y
  2204. +# CONFIG_SND_USB is not set
  2205. +CONFIG_USB=y
  2206. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  2207. +CONFIG_USB_DWCOTG=y
  2208. +CONFIG_MMC=y
  2209. +CONFIG_MMC_SDHCI=y
  2210. +CONFIG_MMC_SDHCI_PLTFM=y
  2211. +CONFIG_MMC_SDHCI_BCM2708=y
  2212. +CONFIG_MMC_SDHCI_BCM2708_DMA=y
  2213. +CONFIG_MMC_BCM2835=y
  2214. +CONFIG_MMC_BCM2835_DMA=y
  2215. +CONFIG_NEW_LEDS=y
  2216. +CONFIG_LEDS_CLASS=y
  2217. +CONFIG_LEDS_TRIGGERS=y
  2218. +# CONFIG_IOMMU_SUPPORT is not set
  2219. +CONFIG_EXT4_FS=y
  2220. +CONFIG_EXT4_FS_POSIX_ACL=y
  2221. +CONFIG_EXT4_FS_SECURITY=y
  2222. +CONFIG_AUTOFS4_FS=y
  2223. +CONFIG_FSCACHE=y
  2224. +CONFIG_CACHEFILES=y
  2225. +CONFIG_MSDOS_FS=y
  2226. +CONFIG_VFAT_FS=y
  2227. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  2228. +CONFIG_TMPFS=y
  2229. +CONFIG_TMPFS_POSIX_ACL=y
  2230. +CONFIG_CONFIGFS_FS=y
  2231. +# CONFIG_MISC_FILESYSTEMS is not set
  2232. +CONFIG_NFS_FS=y
  2233. +CONFIG_NFS_V3_ACL=y
  2234. +CONFIG_NFS_V4=y
  2235. +CONFIG_ROOT_NFS=y
  2236. +CONFIG_NFS_FSCACHE=y
  2237. +CONFIG_NLS_DEFAULT="utf8"
  2238. +CONFIG_NLS_CODEPAGE_437=y
  2239. +CONFIG_NLS_CODEPAGE_737=y
  2240. +CONFIG_NLS_CODEPAGE_775=y
  2241. +CONFIG_NLS_CODEPAGE_850=y
  2242. +CONFIG_NLS_CODEPAGE_852=y
  2243. +CONFIG_NLS_CODEPAGE_855=y
  2244. +CONFIG_NLS_CODEPAGE_857=y
  2245. +CONFIG_NLS_CODEPAGE_860=y
  2246. +CONFIG_NLS_CODEPAGE_861=y
  2247. +CONFIG_NLS_CODEPAGE_862=y
  2248. +CONFIG_NLS_CODEPAGE_863=y
  2249. +CONFIG_NLS_CODEPAGE_864=y
  2250. +CONFIG_NLS_CODEPAGE_865=y
  2251. +CONFIG_NLS_CODEPAGE_866=y
  2252. +CONFIG_NLS_CODEPAGE_869=y
  2253. +CONFIG_NLS_CODEPAGE_936=y
  2254. +CONFIG_NLS_CODEPAGE_950=y
  2255. +CONFIG_NLS_CODEPAGE_932=y
  2256. +CONFIG_NLS_CODEPAGE_949=y
  2257. +CONFIG_NLS_CODEPAGE_874=y
  2258. +CONFIG_NLS_ISO8859_8=y
  2259. +CONFIG_NLS_CODEPAGE_1250=y
  2260. +CONFIG_NLS_CODEPAGE_1251=y
  2261. +CONFIG_NLS_ASCII=y
  2262. +CONFIG_NLS_ISO8859_1=y
  2263. +CONFIG_NLS_ISO8859_2=y
  2264. +CONFIG_NLS_ISO8859_3=y
  2265. +CONFIG_NLS_ISO8859_4=y
  2266. +CONFIG_NLS_ISO8859_5=y
  2267. +CONFIG_NLS_ISO8859_6=y
  2268. +CONFIG_NLS_ISO8859_7=y
  2269. +CONFIG_NLS_ISO8859_9=y
  2270. +CONFIG_NLS_ISO8859_13=y
  2271. +CONFIG_NLS_ISO8859_14=y
  2272. +CONFIG_NLS_ISO8859_15=y
  2273. +CONFIG_NLS_UTF8=y
  2274. +CONFIG_PRINTK_TIME=y
  2275. +CONFIG_DEBUG_FS=y
  2276. +CONFIG_DETECT_HUNG_TASK=y
  2277. +# CONFIG_DEBUG_PREEMPT is not set
  2278. +# CONFIG_DEBUG_BUGVERBOSE is not set
  2279. +# CONFIG_FTRACE is not set
  2280. +CONFIG_KGDB=y
  2281. +CONFIG_KGDB_KDB=y
  2282. +# CONFIG_ARM_UNWIND is not set
  2283. +CONFIG_CRYPTO_CBC=y
  2284. +CONFIG_CRYPTO_HMAC=y
  2285. +CONFIG_CRYPTO_MD5=y
  2286. +CONFIG_CRYPTO_SHA1=y
  2287. +CONFIG_CRYPTO_DES=y
  2288. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  2289. +# CONFIG_CRYPTO_HW is not set
  2290. +CONFIG_CRC_ITU_T=y
  2291. +CONFIG_LIBCRC32C=y
  2292. diff -Nur linux-3.12.33/arch/arm/include/asm/irqflags.h linux-3.12.33-rpi/arch/arm/include/asm/irqflags.h
  2293. --- linux-3.12.33/arch/arm/include/asm/irqflags.h 2014-11-15 06:28:07.000000000 -0600
  2294. +++ linux-3.12.33-rpi/arch/arm/include/asm/irqflags.h 2014-12-03 19:13:32.320418001 -0600
  2295. @@ -145,12 +145,22 @@
  2296. }
  2297. /*
  2298. - * restore saved IRQ & FIQ state
  2299. + * restore saved IRQ state
  2300. */
  2301. static inline void arch_local_irq_restore(unsigned long flags)
  2302. {
  2303. - asm volatile(
  2304. - " msr " IRQMASK_REG_NAME_W ", %0 @ local_irq_restore"
  2305. + unsigned long temp = 0;
  2306. + flags &= ~(1 << 6);
  2307. + asm volatile (
  2308. + " mrs %0, cpsr"
  2309. + : "=r" (temp)
  2310. + :
  2311. + : "memory", "cc");
  2312. + /* Preserve FIQ bit */
  2313. + temp &= (1 << 6);
  2314. + flags = flags | temp;
  2315. + asm volatile (
  2316. + " msr cpsr_c, %0 @ local_irq_restore"
  2317. :
  2318. : "r" (flags)
  2319. : "memory", "cc");
  2320. diff -Nur linux-3.12.33/arch/arm/include/asm/string.h linux-3.12.33-rpi/arch/arm/include/asm/string.h
  2321. --- linux-3.12.33/arch/arm/include/asm/string.h 2014-11-15 06:28:07.000000000 -0600
  2322. +++ linux-3.12.33-rpi/arch/arm/include/asm/string.h 2014-12-03 19:13:32.324418001 -0600
  2323. @@ -24,6 +24,11 @@
  2324. #define __HAVE_ARCH_MEMSET
  2325. extern void * memset(void *, int, __kernel_size_t);
  2326. +#ifdef CONFIG_MACH_BCM2708
  2327. +#define __HAVE_ARCH_MEMCMP
  2328. +extern int memcmp(const void *, const void *, size_t);
  2329. +#endif
  2330. +
  2331. extern void __memzero(void *ptr, __kernel_size_t n);
  2332. #define memset(p,v,n) \
  2333. diff -Nur linux-3.12.33/arch/arm/include/asm/uaccess.h linux-3.12.33-rpi/arch/arm/include/asm/uaccess.h
  2334. --- linux-3.12.33/arch/arm/include/asm/uaccess.h 2014-11-15 06:28:07.000000000 -0600
  2335. +++ linux-3.12.33-rpi/arch/arm/include/asm/uaccess.h 2014-12-03 19:13:32.324418001 -0600
  2336. @@ -427,6 +427,7 @@
  2337. #ifdef CONFIG_MMU
  2338. extern unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n);
  2339. +extern unsigned long __must_check __copy_from_user_std(void *to, const void __user *from, unsigned long n);
  2340. extern unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n);
  2341. extern unsigned long __must_check __copy_to_user_std(void __user *to, const void *from, unsigned long n);
  2342. extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
  2343. diff -Nur linux-3.12.33/arch/arm/Kconfig linux-3.12.33-rpi/arch/arm/Kconfig
  2344. --- linux-3.12.33/arch/arm/Kconfig 2014-11-15 06:28:07.000000000 -0600
  2345. +++ linux-3.12.33-rpi/arch/arm/Kconfig 2014-12-03 19:13:32.212418001 -0600
  2346. @@ -369,6 +369,24 @@
  2347. This enables support for systems based on Atmel
  2348. AT91RM9200 and AT91SAM9* processors.
  2349. +config ARCH_BCM2708
  2350. + bool "Broadcom BCM2708 family"
  2351. + select CPU_V6
  2352. + select ARM_AMBA
  2353. + select HAVE_CLK
  2354. + select HAVE_SCHED_CLOCK
  2355. + select NEED_MACH_GPIO_H
  2356. + select NEED_MACH_MEMORY_H
  2357. + select CLKDEV_LOOKUP
  2358. + select ARCH_HAS_CPUFREQ
  2359. + select GENERIC_CLOCKEVENTS
  2360. + select ARM_ERRATA_411920
  2361. + select MACH_BCM2708
  2362. + select VC4
  2363. + select FIQ
  2364. + help
  2365. + This enables support for Broadcom BCM2708 boards.
  2366. +
  2367. config ARCH_CLPS711X
  2368. bool "Cirrus Logic CLPS711x/EP721x/EP731x-based"
  2369. select ARCH_REQUIRE_GPIOLIB
  2370. @@ -1044,6 +1062,7 @@
  2371. source "arch/arm/mach-vt8500/Kconfig"
  2372. source "arch/arm/mach-w90x900/Kconfig"
  2373. +source "arch/arm/mach-bcm2708/Kconfig"
  2374. source "arch/arm/mach-zynq/Kconfig"
  2375. diff -Nur linux-3.12.33/arch/arm/Kconfig.debug linux-3.12.33-rpi/arch/arm/Kconfig.debug
  2376. --- linux-3.12.33/arch/arm/Kconfig.debug 2014-11-15 06:28:07.000000000 -0600
  2377. +++ linux-3.12.33-rpi/arch/arm/Kconfig.debug 2014-12-03 19:13:32.212418001 -0600
  2378. @@ -847,6 +847,14 @@
  2379. options; the platform specific options are deprecated
  2380. and will be soon removed.
  2381. + config DEBUG_BCM2708_UART0
  2382. + bool "Broadcom BCM2708 UART0 (PL011)"
  2383. + depends on MACH_BCM2708
  2384. + help
  2385. + Say Y here if you want the debug print routines to direct
  2386. + their output to UART 0. The port must have been initialised
  2387. + by the boot-loader before use.
  2388. +
  2389. endchoice
  2390. config DEBUG_EXYNOS_UART
  2391. diff -Nur linux-3.12.33/arch/arm/kernel/fiqasm.S linux-3.12.33-rpi/arch/arm/kernel/fiqasm.S
  2392. --- linux-3.12.33/arch/arm/kernel/fiqasm.S 2014-11-15 06:28:07.000000000 -0600
  2393. +++ linux-3.12.33-rpi/arch/arm/kernel/fiqasm.S 2014-12-03 19:13:32.328418001 -0600
  2394. @@ -47,3 +47,7 @@
  2395. mov r0, r0 @ avoid hazard prior to ARMv4
  2396. mov pc, lr
  2397. ENDPROC(__get_fiq_regs)
  2398. +
  2399. +ENTRY(__FIQ_Branch)
  2400. + mov pc, r8
  2401. +ENDPROC(__FIQ_Branch)
  2402. diff -Nur linux-3.12.33/arch/arm/kernel/process.c linux-3.12.33-rpi/arch/arm/kernel/process.c
  2403. --- linux-3.12.33/arch/arm/kernel/process.c 2014-11-15 06:28:07.000000000 -0600
  2404. +++ linux-3.12.33-rpi/arch/arm/kernel/process.c 2014-12-03 19:13:32.332418001 -0600
  2405. @@ -176,6 +176,16 @@
  2406. default_idle();
  2407. }
  2408. +char bcm2708_reboot_mode = 'h';
  2409. +
  2410. +int __init reboot_setup(char *str)
  2411. +{
  2412. + bcm2708_reboot_mode = str[0];
  2413. + return 1;
  2414. +}
  2415. +
  2416. +__setup("reboot=", reboot_setup);
  2417. +
  2418. /*
  2419. * Called by kexec, immediately prior to machine_kexec().
  2420. *
  2421. diff -Nur linux-3.12.33/arch/arm/lib/arm-mem.h linux-3.12.33-rpi/arch/arm/lib/arm-mem.h
  2422. --- linux-3.12.33/arch/arm/lib/arm-mem.h 1969-12-31 18:00:00.000000000 -0600
  2423. +++ linux-3.12.33-rpi/arch/arm/lib/arm-mem.h 2014-12-03 19:13:32.424418001 -0600
  2424. @@ -0,0 +1,159 @@
  2425. +/*
  2426. +Copyright (c) 2013, Raspberry Pi Foundation
  2427. +Copyright (c) 2013, RISC OS Open Ltd
  2428. +All rights reserved.
  2429. +
  2430. +Redistribution and use in source and binary forms, with or without
  2431. +modification, are permitted provided that the following conditions are met:
  2432. + * Redistributions of source code must retain the above copyright
  2433. + notice, this list of conditions and the following disclaimer.
  2434. + * Redistributions in binary form must reproduce the above copyright
  2435. + notice, this list of conditions and the following disclaimer in the
  2436. + documentation and/or other materials provided with the distribution.
  2437. + * Neither the name of the copyright holder nor the
  2438. + names of its contributors may be used to endorse or promote products
  2439. + derived from this software without specific prior written permission.
  2440. +
  2441. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  2442. +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  2443. +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  2444. +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  2445. +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  2446. +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  2447. +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  2448. +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2449. +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  2450. +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2451. +*/
  2452. +
  2453. +.macro myfunc fname
  2454. + .func fname
  2455. + .global fname
  2456. +fname:
  2457. +.endm
  2458. +
  2459. +.macro preload_leading_step1 backwards, ptr, base
  2460. +/* If the destination is already 16-byte aligned, then we need to preload
  2461. + * between 0 and prefetch_distance (inclusive) cache lines ahead so there
  2462. + * are no gaps when the inner loop starts.
  2463. + */
  2464. + .if backwards
  2465. + sub ptr, base, #1
  2466. + bic ptr, ptr, #31
  2467. + .else
  2468. + bic ptr, base, #31
  2469. + .endif
  2470. + .set OFFSET, 0
  2471. + .rept prefetch_distance+1
  2472. + pld [ptr, #OFFSET]
  2473. + .if backwards
  2474. + .set OFFSET, OFFSET-32
  2475. + .else
  2476. + .set OFFSET, OFFSET+32
  2477. + .endif
  2478. + .endr
  2479. +.endm
  2480. +
  2481. +.macro preload_leading_step2 backwards, ptr, base, leading_bytes, tmp
  2482. +/* However, if the destination is not 16-byte aligned, we may need to
  2483. + * preload one more cache line than that. The question we need to ask is:
  2484. + * are the leading bytes more than the amount by which the source
  2485. + * pointer will be rounded down for preloading, and if so, by how many
  2486. + * cache lines?
  2487. + */
  2488. + .if backwards
  2489. +/* Here we compare against how many bytes we are into the
  2490. + * cache line, counting down from the highest such address.
  2491. + * Effectively, we want to calculate
  2492. + * leading_bytes = dst&15
  2493. + * cacheline_offset = 31-((src-leading_bytes-1)&31)
  2494. + * extra_needed = leading_bytes - cacheline_offset
  2495. + * and test if extra_needed is <= 0, or rearranging:
  2496. + * leading_bytes + (src-leading_bytes-1)&31 <= 31
  2497. + */
  2498. + mov tmp, base, lsl #32-5
  2499. + sbc tmp, tmp, leading_bytes, lsl #32-5
  2500. + adds tmp, tmp, leading_bytes, lsl #32-5
  2501. + bcc 61f
  2502. + pld [ptr, #-32*(prefetch_distance+1)]
  2503. + .else
  2504. +/* Effectively, we want to calculate
  2505. + * leading_bytes = (-dst)&15
  2506. + * cacheline_offset = (src+leading_bytes)&31
  2507. + * extra_needed = leading_bytes - cacheline_offset
  2508. + * and test if extra_needed is <= 0.
  2509. + */
  2510. + mov tmp, base, lsl #32-5
  2511. + add tmp, tmp, leading_bytes, lsl #32-5
  2512. + rsbs tmp, tmp, leading_bytes, lsl #32-5
  2513. + bls 61f
  2514. + pld [ptr, #32*(prefetch_distance+1)]
  2515. + .endif
  2516. +61:
  2517. +.endm
  2518. +
  2519. +.macro preload_trailing backwards, base, remain, tmp
  2520. + /* We need either 0, 1 or 2 extra preloads */
  2521. + .if backwards
  2522. + rsb tmp, base, #0
  2523. + mov tmp, tmp, lsl #32-5
  2524. + .else
  2525. + mov tmp, base, lsl #32-5
  2526. + .endif
  2527. + adds tmp, tmp, remain, lsl #32-5
  2528. + adceqs tmp, tmp, #0
  2529. + /* The instruction above has two effects: ensures Z is only
  2530. + * set if C was clear (so Z indicates that both shifted quantities
  2531. + * were 0), and clears C if Z was set (so C indicates that the sum
  2532. + * of the shifted quantities was greater and not equal to 32) */
  2533. + beq 82f
  2534. + .if backwards
  2535. + sub tmp, base, #1
  2536. + bic tmp, tmp, #31
  2537. + .else
  2538. + bic tmp, base, #31
  2539. + .endif
  2540. + bcc 81f
  2541. + .if backwards
  2542. + pld [tmp, #-32*(prefetch_distance+1)]
  2543. +81:
  2544. + pld [tmp, #-32*prefetch_distance]
  2545. + .else
  2546. + pld [tmp, #32*(prefetch_distance+2)]
  2547. +81:
  2548. + pld [tmp, #32*(prefetch_distance+1)]
  2549. + .endif
  2550. +82:
  2551. +.endm
  2552. +
  2553. +.macro preload_all backwards, narrow_case, shift, base, remain, tmp0, tmp1
  2554. + .if backwards
  2555. + sub tmp0, base, #1
  2556. + bic tmp0, tmp0, #31
  2557. + pld [tmp0]
  2558. + sub tmp1, base, remain, lsl #shift
  2559. + .else
  2560. + bic tmp0, base, #31
  2561. + pld [tmp0]
  2562. + add tmp1, base, remain, lsl #shift
  2563. + sub tmp1, tmp1, #1
  2564. + .endif
  2565. + bic tmp1, tmp1, #31
  2566. + cmp tmp1, tmp0
  2567. + beq 92f
  2568. + .if narrow_case
  2569. + /* In this case, all the data fits in either 1 or 2 cache lines */
  2570. + pld [tmp1]
  2571. + .else
  2572. +91:
  2573. + .if backwards
  2574. + sub tmp0, tmp0, #32
  2575. + .else
  2576. + add tmp0, tmp0, #32
  2577. + .endif
  2578. + cmp tmp0, tmp1
  2579. + pld [tmp0]
  2580. + bne 91b
  2581. + .endif
  2582. +92:
  2583. +.endm
  2584. diff -Nur linux-3.12.33/arch/arm/lib/copy_from_user.S linux-3.12.33-rpi/arch/arm/lib/copy_from_user.S
  2585. --- linux-3.12.33/arch/arm/lib/copy_from_user.S 2014-11-15 06:28:07.000000000 -0600
  2586. +++ linux-3.12.33-rpi/arch/arm/lib/copy_from_user.S 2014-12-03 19:13:32.424418001 -0600
  2587. @@ -84,11 +84,13 @@
  2588. .text
  2589. -ENTRY(__copy_from_user)
  2590. +ENTRY(__copy_from_user_std)
  2591. +WEAK(__copy_from_user)
  2592. #include "copy_template.S"
  2593. ENDPROC(__copy_from_user)
  2594. +ENDPROC(__copy_from_user_std)
  2595. .pushsection .fixup,"ax"
  2596. .align 0
  2597. diff -Nur linux-3.12.33/arch/arm/lib/exports_rpi.c linux-3.12.33-rpi/arch/arm/lib/exports_rpi.c
  2598. --- linux-3.12.33/arch/arm/lib/exports_rpi.c 1969-12-31 18:00:00.000000000 -0600
  2599. +++ linux-3.12.33-rpi/arch/arm/lib/exports_rpi.c 2014-12-03 19:13:32.428418001 -0600
  2600. @@ -0,0 +1,37 @@
  2601. +/**
  2602. + * Copyright (c) 2014, Raspberry Pi (Trading) Ltd.
  2603. + *
  2604. + * Redistribution and use in source and binary forms, with or without
  2605. + * modification, are permitted provided that the following conditions
  2606. + * are met:
  2607. + * 1. Redistributions of source code must retain the above copyright
  2608. + * notice, this list of conditions, and the following disclaimer,
  2609. + * without modification.
  2610. + * 2. Redistributions in binary form must reproduce the above copyright
  2611. + * notice, this list of conditions and the following disclaimer in the
  2612. + * documentation and/or other materials provided with the distribution.
  2613. + * 3. The names of the above-listed copyright holders may not be used
  2614. + * to endorse or promote products derived from this software without
  2615. + * specific prior written permission.
  2616. + *
  2617. + * ALTERNATIVELY, this software may be distributed under the terms of the
  2618. + * GNU General Public License ("GPL") version 2, as published by the Free
  2619. + * Software Foundation.
  2620. + *
  2621. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  2622. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  2623. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  2624. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  2625. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  2626. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  2627. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  2628. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  2629. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  2630. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  2631. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2632. + */
  2633. +
  2634. +#include <linux/kernel.h>
  2635. +#include <linux/module.h>
  2636. +
  2637. +EXPORT_SYMBOL(memcmp);
  2638. diff -Nur linux-3.12.33/arch/arm/lib/Makefile linux-3.12.33-rpi/arch/arm/lib/Makefile
  2639. --- linux-3.12.33/arch/arm/lib/Makefile 2014-11-15 06:28:07.000000000 -0600
  2640. +++ linux-3.12.33-rpi/arch/arm/lib/Makefile 2014-12-03 19:13:32.424418001 -0600
  2641. @@ -6,15 +6,24 @@
  2642. lib-y := backtrace.o changebit.o csumipv6.o csumpartial.o \
  2643. csumpartialcopy.o csumpartialcopyuser.o clearbit.o \
  2644. - delay.o delay-loop.o findbit.o memchr.o memcpy.o \
  2645. - memmove.o memset.o memzero.o setbit.o \
  2646. - strchr.o strrchr.o \
  2647. + delay.o delay-loop.o findbit.o memchr.o memzero.o \
  2648. + setbit.o strchr.o strrchr.o \
  2649. testchangebit.o testclearbit.o testsetbit.o \
  2650. ashldi3.o ashrdi3.o lshrdi3.o muldi3.o \
  2651. ucmpdi2.o lib1funcs.o div64.o \
  2652. io-readsb.o io-writesb.o io-readsl.o io-writesl.o \
  2653. call_with_stack.o
  2654. +# Choose optimised implementations for Raspberry Pi
  2655. +ifeq ($(CONFIG_MACH_BCM2708),y)
  2656. + CFLAGS_uaccess_with_memcpy.o += -DCOPY_FROM_USER_THRESHOLD=1600
  2657. + CFLAGS_uaccess_with_memcpy.o += -DCOPY_TO_USER_THRESHOLD=672
  2658. + obj-$(CONFIG_MODULES) += exports_rpi.o
  2659. + lib-y += memcpy_rpi.o memmove_rpi.o memset_rpi.o memcmp_rpi.o
  2660. +else
  2661. + lib-y += memcpy.o memmove.o memset.o
  2662. +endif
  2663. +
  2664. mmu-y := clear_user.o copy_page.o getuser.o putuser.o
  2665. # the code in uaccess.S is not preemption safe and
  2666. diff -Nur linux-3.12.33/arch/arm/lib/memcmp_rpi.S linux-3.12.33-rpi/arch/arm/lib/memcmp_rpi.S
  2667. --- linux-3.12.33/arch/arm/lib/memcmp_rpi.S 1969-12-31 18:00:00.000000000 -0600
  2668. +++ linux-3.12.33-rpi/arch/arm/lib/memcmp_rpi.S 2014-12-03 19:13:32.432418001 -0600
  2669. @@ -0,0 +1,285 @@
  2670. +/*
  2671. +Copyright (c) 2013, Raspberry Pi Foundation
  2672. +Copyright (c) 2013, RISC OS Open Ltd
  2673. +All rights reserved.
  2674. +
  2675. +Redistribution and use in source and binary forms, with or without
  2676. +modification, are permitted provided that the following conditions are met:
  2677. + * Redistributions of source code must retain the above copyright
  2678. + notice, this list of conditions and the following disclaimer.
  2679. + * Redistributions in binary form must reproduce the above copyright
  2680. + notice, this list of conditions and the following disclaimer in the
  2681. + documentation and/or other materials provided with the distribution.
  2682. + * Neither the name of the copyright holder nor the
  2683. + names of its contributors may be used to endorse or promote products
  2684. + derived from this software without specific prior written permission.
  2685. +
  2686. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  2687. +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  2688. +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  2689. +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  2690. +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  2691. +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  2692. +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  2693. +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2694. +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  2695. +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2696. +*/
  2697. +
  2698. +#include <linux/linkage.h>
  2699. +#include "arm-mem.h"
  2700. +
  2701. +/* Prevent the stack from becoming executable */
  2702. +#if defined(__linux__) && defined(__ELF__)
  2703. +.section .note.GNU-stack,"",%progbits
  2704. +#endif
  2705. +
  2706. + .text
  2707. + .arch armv6
  2708. + .object_arch armv4
  2709. + .arm
  2710. + .altmacro
  2711. + .p2align 2
  2712. +
  2713. +.macro memcmp_process_head unaligned
  2714. + .if unaligned
  2715. + ldr DAT0, [S_1], #4
  2716. + ldr DAT1, [S_1], #4
  2717. + ldr DAT2, [S_1], #4
  2718. + ldr DAT3, [S_1], #4
  2719. + .else
  2720. + ldmia S_1!, {DAT0, DAT1, DAT2, DAT3}
  2721. + .endif
  2722. + ldmia S_2!, {DAT4, DAT5, DAT6, DAT7}
  2723. +.endm
  2724. +
  2725. +.macro memcmp_process_tail
  2726. + cmp DAT0, DAT4
  2727. + cmpeq DAT1, DAT5
  2728. + cmpeq DAT2, DAT6
  2729. + cmpeq DAT3, DAT7
  2730. + bne 200f
  2731. +.endm
  2732. +
  2733. +.macro memcmp_leading_31bytes
  2734. + movs DAT0, OFF, lsl #31
  2735. + ldrmib DAT0, [S_1], #1
  2736. + ldrcsh DAT1, [S_1], #2
  2737. + ldrmib DAT4, [S_2], #1
  2738. + ldrcsh DAT5, [S_2], #2
  2739. + movpl DAT0, #0
  2740. + movcc DAT1, #0
  2741. + movpl DAT4, #0
  2742. + movcc DAT5, #0
  2743. + submi N, N, #1
  2744. + subcs N, N, #2
  2745. + cmp DAT0, DAT4
  2746. + cmpeq DAT1, DAT5
  2747. + bne 200f
  2748. + movs DAT0, OFF, lsl #29
  2749. + ldrmi DAT0, [S_1], #4
  2750. + ldrcs DAT1, [S_1], #4
  2751. + ldrcs DAT2, [S_1], #4
  2752. + ldrmi DAT4, [S_2], #4
  2753. + ldmcsia S_2!, {DAT5, DAT6}
  2754. + movpl DAT0, #0
  2755. + movcc DAT1, #0
  2756. + movcc DAT2, #0
  2757. + movpl DAT4, #0
  2758. + movcc DAT5, #0
  2759. + movcc DAT6, #0
  2760. + submi N, N, #4
  2761. + subcs N, N, #8
  2762. + cmp DAT0, DAT4
  2763. + cmpeq DAT1, DAT5
  2764. + cmpeq DAT2, DAT6
  2765. + bne 200f
  2766. + tst OFF, #16
  2767. + beq 105f
  2768. + memcmp_process_head 1
  2769. + sub N, N, #16
  2770. + memcmp_process_tail
  2771. +105:
  2772. +.endm
  2773. +
  2774. +.macro memcmp_trailing_15bytes unaligned
  2775. + movs N, N, lsl #29
  2776. + .if unaligned
  2777. + ldrcs DAT0, [S_1], #4
  2778. + ldrcs DAT1, [S_1], #4
  2779. + .else
  2780. + ldmcsia S_1!, {DAT0, DAT1}
  2781. + .endif
  2782. + ldrmi DAT2, [S_1], #4
  2783. + ldmcsia S_2!, {DAT4, DAT5}
  2784. + ldrmi DAT6, [S_2], #4
  2785. + movcc DAT0, #0
  2786. + movcc DAT1, #0
  2787. + movpl DAT2, #0
  2788. + movcc DAT4, #0
  2789. + movcc DAT5, #0
  2790. + movpl DAT6, #0
  2791. + cmp DAT0, DAT4
  2792. + cmpeq DAT1, DAT5
  2793. + cmpeq DAT2, DAT6
  2794. + bne 200f
  2795. + movs N, N, lsl #2
  2796. + ldrcsh DAT0, [S_1], #2
  2797. + ldrmib DAT1, [S_1]
  2798. + ldrcsh DAT4, [S_2], #2
  2799. + ldrmib DAT5, [S_2]
  2800. + movcc DAT0, #0
  2801. + movpl DAT1, #0
  2802. + movcc DAT4, #0
  2803. + movpl DAT5, #0
  2804. + cmp DAT0, DAT4
  2805. + cmpeq DAT1, DAT5
  2806. + bne 200f
  2807. +.endm
  2808. +
  2809. +.macro memcmp_long_inner_loop unaligned
  2810. +110:
  2811. + memcmp_process_head unaligned
  2812. + pld [S_2, #prefetch_distance*32 + 16]
  2813. + memcmp_process_tail
  2814. + memcmp_process_head unaligned
  2815. + pld [S_1, OFF]
  2816. + memcmp_process_tail
  2817. + subs N, N, #32
  2818. + bhs 110b
  2819. + /* Just before the final (prefetch_distance+1) 32-byte blocks,
  2820. + * deal with final preloads */
  2821. + preload_trailing 0, S_1, N, DAT0
  2822. + preload_trailing 0, S_2, N, DAT0
  2823. + add N, N, #(prefetch_distance+2)*32 - 16
  2824. +120:
  2825. + memcmp_process_head unaligned
  2826. + memcmp_process_tail
  2827. + subs N, N, #16
  2828. + bhs 120b
  2829. + /* Trailing words and bytes */
  2830. + tst N, #15
  2831. + beq 199f
  2832. + memcmp_trailing_15bytes unaligned
  2833. +199: /* Reached end without detecting a difference */
  2834. + mov a1, #0
  2835. + setend le
  2836. + pop {DAT1-DAT6, pc}
  2837. +.endm
  2838. +
  2839. +.macro memcmp_short_inner_loop unaligned
  2840. + subs N, N, #16 /* simplifies inner loop termination */
  2841. + blo 122f
  2842. +120:
  2843. + memcmp_process_head unaligned
  2844. + memcmp_process_tail
  2845. + subs N, N, #16
  2846. + bhs 120b
  2847. +122: /* Trailing words and bytes */
  2848. + tst N, #15
  2849. + beq 199f
  2850. + memcmp_trailing_15bytes unaligned
  2851. +199: /* Reached end without detecting a difference */
  2852. + mov a1, #0
  2853. + setend le
  2854. + pop {DAT1-DAT6, pc}
  2855. +.endm
  2856. +
  2857. +/*
  2858. + * int memcmp(const void *s1, const void *s2, size_t n);
  2859. + * On entry:
  2860. + * a1 = pointer to buffer 1
  2861. + * a2 = pointer to buffer 2
  2862. + * a3 = number of bytes to compare (as unsigned chars)
  2863. + * On exit:
  2864. + * a1 = >0/=0/<0 if s1 >/=/< s2
  2865. + */
  2866. +
  2867. +.set prefetch_distance, 2
  2868. +
  2869. +ENTRY(memcmp)
  2870. + S_1 .req a1
  2871. + S_2 .req a2
  2872. + N .req a3
  2873. + DAT0 .req a4
  2874. + DAT1 .req v1
  2875. + DAT2 .req v2
  2876. + DAT3 .req v3
  2877. + DAT4 .req v4
  2878. + DAT5 .req v5
  2879. + DAT6 .req v6
  2880. + DAT7 .req ip
  2881. + OFF .req lr
  2882. +
  2883. + push {DAT1-DAT6, lr}
  2884. + setend be /* lowest-addressed bytes are most significant */
  2885. +
  2886. + /* To preload ahead as we go, we need at least (prefetch_distance+2) 32-byte blocks */
  2887. + cmp N, #(prefetch_distance+3)*32 - 1
  2888. + blo 170f
  2889. +
  2890. + /* Long case */
  2891. + /* Adjust N so that the decrement instruction can also test for
  2892. + * inner loop termination. We want it to stop when there are
  2893. + * (prefetch_distance+1) complete blocks to go. */
  2894. + sub N, N, #(prefetch_distance+2)*32
  2895. + preload_leading_step1 0, DAT0, S_1
  2896. + preload_leading_step1 0, DAT1, S_2
  2897. + tst S_2, #31
  2898. + beq 154f
  2899. + rsb OFF, S_2, #0 /* no need to AND with 15 here */
  2900. + preload_leading_step2 0, DAT0, S_1, OFF, DAT2
  2901. + preload_leading_step2 0, DAT1, S_2, OFF, DAT2
  2902. + memcmp_leading_31bytes
  2903. +154: /* Second source now cacheline (32-byte) aligned; we have at
  2904. + * least one prefetch to go. */
  2905. + /* Prefetch offset is best selected such that it lies in the
  2906. + * first 8 of each 32 bytes - but it's just as easy to aim for
  2907. + * the first one */
  2908. + and OFF, S_1, #31
  2909. + rsb OFF, OFF, #32*prefetch_distance
  2910. + tst S_1, #3
  2911. + bne 140f
  2912. + memcmp_long_inner_loop 0
  2913. +140: memcmp_long_inner_loop 1
  2914. +
  2915. +170: /* Short case */
  2916. + teq N, #0
  2917. + beq 199f
  2918. + preload_all 0, 0, 0, S_1, N, DAT0, DAT1
  2919. + preload_all 0, 0, 0, S_2, N, DAT0, DAT1
  2920. + tst S_2, #3
  2921. + beq 174f
  2922. +172: subs N, N, #1
  2923. + blo 199f
  2924. + ldrb DAT0, [S_1], #1
  2925. + ldrb DAT4, [S_2], #1
  2926. + cmp DAT0, DAT4
  2927. + bne 200f
  2928. + tst S_2, #3
  2929. + bne 172b
  2930. +174: /* Second source now 4-byte aligned; we have 0 or more bytes to go */
  2931. + tst S_1, #3
  2932. + bne 140f
  2933. + memcmp_short_inner_loop 0
  2934. +140: memcmp_short_inner_loop 1
  2935. +
  2936. +200: /* Difference found: determine sign. */
  2937. + movhi a1, #1
  2938. + movlo a1, #-1
  2939. + setend le
  2940. + pop {DAT1-DAT6, pc}
  2941. +
  2942. + .unreq S_1
  2943. + .unreq S_2
  2944. + .unreq N
  2945. + .unreq DAT0
  2946. + .unreq DAT1
  2947. + .unreq DAT2
  2948. + .unreq DAT3
  2949. + .unreq DAT4
  2950. + .unreq DAT5
  2951. + .unreq DAT6
  2952. + .unreq DAT7
  2953. + .unreq OFF
  2954. +ENDPROC(memcmp)
  2955. diff -Nur linux-3.12.33/arch/arm/lib/memcpymove.h linux-3.12.33-rpi/arch/arm/lib/memcpymove.h
  2956. --- linux-3.12.33/arch/arm/lib/memcpymove.h 1969-12-31 18:00:00.000000000 -0600
  2957. +++ linux-3.12.33-rpi/arch/arm/lib/memcpymove.h 2014-12-03 19:13:32.432418001 -0600
  2958. @@ -0,0 +1,506 @@
  2959. +/*
  2960. +Copyright (c) 2013, Raspberry Pi Foundation
  2961. +Copyright (c) 2013, RISC OS Open Ltd
  2962. +All rights reserved.
  2963. +
  2964. +Redistribution and use in source and binary forms, with or without
  2965. +modification, are permitted provided that the following conditions are met:
  2966. + * Redistributions of source code must retain the above copyright
  2967. + notice, this list of conditions and the following disclaimer.
  2968. + * Redistributions in binary form must reproduce the above copyright
  2969. + notice, this list of conditions and the following disclaimer in the
  2970. + documentation and/or other materials provided with the distribution.
  2971. + * Neither the name of the copyright holder nor the
  2972. + names of its contributors may be used to endorse or promote products
  2973. + derived from this software without specific prior written permission.
  2974. +
  2975. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  2976. +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  2977. +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  2978. +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  2979. +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  2980. +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  2981. +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  2982. +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2983. +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  2984. +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2985. +*/
  2986. +
  2987. +.macro unaligned_words backwards, align, use_pld, words, r0, r1, r2, r3, r4, r5, r6, r7, r8
  2988. + .if words == 1
  2989. + .if backwards
  2990. + mov r1, r0, lsl #32-align*8
  2991. + ldr r0, [S, #-4]!
  2992. + orr r1, r1, r0, lsr #align*8
  2993. + str r1, [D, #-4]!
  2994. + .else
  2995. + mov r0, r1, lsr #align*8
  2996. + ldr r1, [S, #4]!
  2997. + orr r0, r0, r1, lsl #32-align*8
  2998. + str r0, [D], #4
  2999. + .endif
  3000. + .elseif words == 2
  3001. + .if backwards
  3002. + ldr r1, [S, #-4]!
  3003. + mov r2, r0, lsl #32-align*8
  3004. + ldr r0, [S, #-4]!
  3005. + orr r2, r2, r1, lsr #align*8
  3006. + mov r1, r1, lsl #32-align*8
  3007. + orr r1, r1, r0, lsr #align*8
  3008. + stmdb D!, {r1, r2}
  3009. + .else
  3010. + ldr r1, [S, #4]!
  3011. + mov r0, r2, lsr #align*8
  3012. + ldr r2, [S, #4]!
  3013. + orr r0, r0, r1, lsl #32-align*8
  3014. + mov r1, r1, lsr #align*8
  3015. + orr r1, r1, r2, lsl #32-align*8
  3016. + stmia D!, {r0, r1}
  3017. + .endif
  3018. + .elseif words == 4
  3019. + .if backwards
  3020. + ldmdb S!, {r2, r3}
  3021. + mov r4, r0, lsl #32-align*8
  3022. + ldmdb S!, {r0, r1}
  3023. + orr r4, r4, r3, lsr #align*8
  3024. + mov r3, r3, lsl #32-align*8
  3025. + orr r3, r3, r2, lsr #align*8
  3026. + mov r2, r2, lsl #32-align*8
  3027. + orr r2, r2, r1, lsr #align*8
  3028. + mov r1, r1, lsl #32-align*8
  3029. + orr r1, r1, r0, lsr #align*8
  3030. + stmdb D!, {r1, r2, r3, r4}
  3031. + .else
  3032. + ldmib S!, {r1, r2}
  3033. + mov r0, r4, lsr #align*8
  3034. + ldmib S!, {r3, r4}
  3035. + orr r0, r0, r1, lsl #32-align*8
  3036. + mov r1, r1, lsr #align*8
  3037. + orr r1, r1, r2, lsl #32-align*8
  3038. + mov r2, r2, lsr #align*8
  3039. + orr r2, r2, r3, lsl #32-align*8
  3040. + mov r3, r3, lsr #align*8
  3041. + orr r3, r3, r4, lsl #32-align*8
  3042. + stmia D!, {r0, r1, r2, r3}
  3043. + .endif
  3044. + .elseif words == 8
  3045. + .if backwards
  3046. + ldmdb S!, {r4, r5, r6, r7}
  3047. + mov r8, r0, lsl #32-align*8
  3048. + ldmdb S!, {r0, r1, r2, r3}
  3049. + .if use_pld
  3050. + pld [S, OFF]
  3051. + .endif
  3052. + orr r8, r8, r7, lsr #align*8
  3053. + mov r7, r7, lsl #32-align*8
  3054. + orr r7, r7, r6, lsr #align*8
  3055. + mov r6, r6, lsl #32-align*8
  3056. + orr r6, r6, r5, lsr #align*8
  3057. + mov r5, r5, lsl #32-align*8
  3058. + orr r5, r5, r4, lsr #align*8
  3059. + mov r4, r4, lsl #32-align*8
  3060. + orr r4, r4, r3, lsr #align*8
  3061. + mov r3, r3, lsl #32-align*8
  3062. + orr r3, r3, r2, lsr #align*8
  3063. + mov r2, r2, lsl #32-align*8
  3064. + orr r2, r2, r1, lsr #align*8
  3065. + mov r1, r1, lsl #32-align*8
  3066. + orr r1, r1, r0, lsr #align*8
  3067. + stmdb D!, {r5, r6, r7, r8}
  3068. + stmdb D!, {r1, r2, r3, r4}
  3069. + .else
  3070. + ldmib S!, {r1, r2, r3, r4}
  3071. + mov r0, r8, lsr #align*8
  3072. + ldmib S!, {r5, r6, r7, r8}
  3073. + .if use_pld
  3074. + pld [S, OFF]
  3075. + .endif
  3076. + orr r0, r0, r1, lsl #32-align*8
  3077. + mov r1, r1, lsr #align*8
  3078. + orr r1, r1, r2, lsl #32-align*8
  3079. + mov r2, r2, lsr #align*8
  3080. + orr r2, r2, r3, lsl #32-align*8
  3081. + mov r3, r3, lsr #align*8
  3082. + orr r3, r3, r4, lsl #32-align*8
  3083. + mov r4, r4, lsr #align*8
  3084. + orr r4, r4, r5, lsl #32-align*8
  3085. + mov r5, r5, lsr #align*8
  3086. + orr r5, r5, r6, lsl #32-align*8
  3087. + mov r6, r6, lsr #align*8
  3088. + orr r6, r6, r7, lsl #32-align*8
  3089. + mov r7, r7, lsr #align*8
  3090. + orr r7, r7, r8, lsl #32-align*8
  3091. + stmia D!, {r0, r1, r2, r3}
  3092. + stmia D!, {r4, r5, r6, r7}
  3093. + .endif
  3094. + .endif
  3095. +.endm
  3096. +
  3097. +.macro memcpy_leading_15bytes backwards, align
  3098. + movs DAT1, DAT2, lsl #31
  3099. + sub N, N, DAT2
  3100. + .if backwards
  3101. + ldrmib DAT0, [S, #-1]!
  3102. + ldrcsh DAT1, [S, #-2]!
  3103. + strmib DAT0, [D, #-1]!
  3104. + strcsh DAT1, [D, #-2]!
  3105. + .else
  3106. + ldrmib DAT0, [S], #1
  3107. + ldrcsh DAT1, [S], #2
  3108. + strmib DAT0, [D], #1
  3109. + strcsh DAT1, [D], #2
  3110. + .endif
  3111. + movs DAT1, DAT2, lsl #29
  3112. + .if backwards
  3113. + ldrmi DAT0, [S, #-4]!
  3114. + .if align == 0
  3115. + ldmcsdb S!, {DAT1, DAT2}
  3116. + .else
  3117. + ldrcs DAT2, [S, #-4]!
  3118. + ldrcs DAT1, [S, #-4]!
  3119. + .endif
  3120. + strmi DAT0, [D, #-4]!
  3121. + stmcsdb D!, {DAT1, DAT2}
  3122. + .else
  3123. + ldrmi DAT0, [S], #4
  3124. + .if align == 0
  3125. + ldmcsia S!, {DAT1, DAT2}
  3126. + .else
  3127. + ldrcs DAT1, [S], #4
  3128. + ldrcs DAT2, [S], #4
  3129. + .endif
  3130. + strmi DAT0, [D], #4
  3131. + stmcsia D!, {DAT1, DAT2}
  3132. + .endif
  3133. +.endm
  3134. +
  3135. +.macro memcpy_trailing_15bytes backwards, align
  3136. + movs N, N, lsl #29
  3137. + .if backwards
  3138. + .if align == 0
  3139. + ldmcsdb S!, {DAT0, DAT1}
  3140. + .else
  3141. + ldrcs DAT1, [S, #-4]!
  3142. + ldrcs DAT0, [S, #-4]!
  3143. + .endif
  3144. + ldrmi DAT2, [S, #-4]!
  3145. + stmcsdb D!, {DAT0, DAT1}
  3146. + strmi DAT2, [D, #-4]!
  3147. + .else
  3148. + .if align == 0
  3149. + ldmcsia S!, {DAT0, DAT1}
  3150. + .else
  3151. + ldrcs DAT0, [S], #4
  3152. + ldrcs DAT1, [S], #4
  3153. + .endif
  3154. + ldrmi DAT2, [S], #4
  3155. + stmcsia D!, {DAT0, DAT1}
  3156. + strmi DAT2, [D], #4
  3157. + .endif
  3158. + movs N, N, lsl #2
  3159. + .if backwards
  3160. + ldrcsh DAT0, [S, #-2]!
  3161. + ldrmib DAT1, [S, #-1]
  3162. + strcsh DAT0, [D, #-2]!
  3163. + strmib DAT1, [D, #-1]
  3164. + .else
  3165. + ldrcsh DAT0, [S], #2
  3166. + ldrmib DAT1, [S]
  3167. + strcsh DAT0, [D], #2
  3168. + strmib DAT1, [D]
  3169. + .endif
  3170. +.endm
  3171. +
  3172. +.macro memcpy_long_inner_loop backwards, align
  3173. + .if align != 0
  3174. + .if backwards
  3175. + ldr DAT0, [S, #-align]!
  3176. + .else
  3177. + ldr LAST, [S, #-align]!
  3178. + .endif
  3179. + .endif
  3180. +110:
  3181. + .if align == 0
  3182. + .if backwards
  3183. + ldmdb S!, {DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, LAST}
  3184. + pld [S, OFF]
  3185. + stmdb D!, {DAT4, DAT5, DAT6, LAST}
  3186. + stmdb D!, {DAT0, DAT1, DAT2, DAT3}
  3187. + .else
  3188. + ldmia S!, {DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, LAST}
  3189. + pld [S, OFF]
  3190. + stmia D!, {DAT0, DAT1, DAT2, DAT3}
  3191. + stmia D!, {DAT4, DAT5, DAT6, LAST}
  3192. + .endif
  3193. + .else
  3194. + unaligned_words backwards, align, 1, 8, DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, DAT7, LAST
  3195. + .endif
  3196. + subs N, N, #32
  3197. + bhs 110b
  3198. + /* Just before the final (prefetch_distance+1) 32-byte blocks, deal with final preloads */
  3199. + preload_trailing backwards, S, N, OFF
  3200. + add N, N, #(prefetch_distance+2)*32 - 32
  3201. +120:
  3202. + .if align == 0
  3203. + .if backwards
  3204. + ldmdb S!, {DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, LAST}
  3205. + stmdb D!, {DAT4, DAT5, DAT6, LAST}
  3206. + stmdb D!, {DAT0, DAT1, DAT2, DAT3}
  3207. + .else
  3208. + ldmia S!, {DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, LAST}
  3209. + stmia D!, {DAT0, DAT1, DAT2, DAT3}
  3210. + stmia D!, {DAT4, DAT5, DAT6, LAST}
  3211. + .endif
  3212. + .else
  3213. + unaligned_words backwards, align, 0, 8, DAT0, DAT1, DAT2, DAT3, DAT4, DAT5, DAT6, DAT7, LAST
  3214. + .endif
  3215. + subs N, N, #32
  3216. + bhs 120b
  3217. + tst N, #16
  3218. + .if align == 0
  3219. + .if backwards
  3220. + ldmnedb S!, {DAT0, DAT1, DAT2, LAST}
  3221. + stmnedb D!, {DAT0, DAT1, DAT2, LAST}
  3222. + .else
  3223. + ldmneia S!, {DAT0, DAT1, DAT2, LAST}
  3224. + stmneia D!, {DAT0, DAT1, DAT2, LAST}
  3225. + .endif
  3226. + .else
  3227. + beq 130f
  3228. + unaligned_words backwards, align, 0, 4, DAT0, DAT1, DAT2, DAT3, LAST
  3229. +130:
  3230. + .endif
  3231. + /* Trailing words and bytes */
  3232. + tst N, #15
  3233. + beq 199f
  3234. + .if align != 0
  3235. + add S, S, #align
  3236. + .endif
  3237. + memcpy_trailing_15bytes backwards, align
  3238. +199:
  3239. + pop {DAT3, DAT4, DAT5, DAT6, DAT7}
  3240. + pop {D, DAT1, DAT2, pc}
  3241. +.endm
  3242. +
  3243. +.macro memcpy_medium_inner_loop backwards, align
  3244. +120:
  3245. + .if backwards
  3246. + .if align == 0
  3247. + ldmdb S!, {DAT0, DAT1, DAT2, LAST}
  3248. + .else
  3249. + ldr LAST, [S, #-4]!
  3250. + ldr DAT2, [S, #-4]!
  3251. + ldr DAT1, [S, #-4]!
  3252. + ldr DAT0, [S, #-4]!
  3253. + .endif
  3254. + stmdb D!, {DAT0, DAT1, DAT2, LAST}
  3255. + .else
  3256. + .if align == 0
  3257. + ldmia S!, {DAT0, DAT1, DAT2, LAST}
  3258. + .else
  3259. + ldr DAT0, [S], #4
  3260. + ldr DAT1, [S], #4
  3261. + ldr DAT2, [S], #4
  3262. + ldr LAST, [S], #4
  3263. + .endif
  3264. + stmia D!, {DAT0, DAT1, DAT2, LAST}
  3265. + .endif
  3266. + subs N, N, #16
  3267. + bhs 120b
  3268. + /* Trailing words and bytes */
  3269. + tst N, #15
  3270. + beq 199f
  3271. + memcpy_trailing_15bytes backwards, align
  3272. +199:
  3273. + pop {D, DAT1, DAT2, pc}
  3274. +.endm
  3275. +
  3276. +.macro memcpy_short_inner_loop backwards, align
  3277. + tst N, #16
  3278. + .if backwards
  3279. + .if align == 0
  3280. + ldmnedb S!, {DAT0, DAT1, DAT2, LAST}
  3281. + .else
  3282. + ldrne LAST, [S, #-4]!
  3283. + ldrne DAT2, [S, #-4]!
  3284. + ldrne DAT1, [S, #-4]!
  3285. + ldrne DAT0, [S, #-4]!
  3286. + .endif
  3287. + stmnedb D!, {DAT0, DAT1, DAT2, LAST}
  3288. + .else
  3289. + .if align == 0
  3290. + ldmneia S!, {DAT0, DAT1, DAT2, LAST}
  3291. + .else
  3292. + ldrne DAT0, [S], #4
  3293. + ldrne DAT1, [S], #4
  3294. + ldrne DAT2, [S], #4
  3295. + ldrne LAST, [S], #4
  3296. + .endif
  3297. + stmneia D!, {DAT0, DAT1, DAT2, LAST}
  3298. + .endif
  3299. + memcpy_trailing_15bytes backwards, align
  3300. +199:
  3301. + pop {D, DAT1, DAT2, pc}
  3302. +.endm
  3303. +
  3304. +.macro memcpy backwards
  3305. + D .req a1
  3306. + S .req a2
  3307. + N .req a3
  3308. + DAT0 .req a4
  3309. + DAT1 .req v1
  3310. + DAT2 .req v2
  3311. + DAT3 .req v3
  3312. + DAT4 .req v4
  3313. + DAT5 .req v5
  3314. + DAT6 .req v6
  3315. + DAT7 .req sl
  3316. + LAST .req ip
  3317. + OFF .req lr
  3318. +
  3319. + .cfi_startproc
  3320. +
  3321. + push {D, DAT1, DAT2, lr}
  3322. +
  3323. + .cfi_def_cfa_offset 16
  3324. + .cfi_rel_offset D, 0
  3325. + .cfi_undefined S
  3326. + .cfi_undefined N
  3327. + .cfi_undefined DAT0
  3328. + .cfi_rel_offset DAT1, 4
  3329. + .cfi_rel_offset DAT2, 8
  3330. + .cfi_undefined LAST
  3331. + .cfi_rel_offset lr, 12
  3332. +
  3333. + .if backwards
  3334. + add D, D, N
  3335. + add S, S, N
  3336. + .endif
  3337. +
  3338. + /* See if we're guaranteed to have at least one 16-byte aligned 16-byte write */
  3339. + cmp N, #31
  3340. + blo 170f
  3341. + /* To preload ahead as we go, we need at least (prefetch_distance+2) 32-byte blocks */
  3342. + cmp N, #(prefetch_distance+3)*32 - 1
  3343. + blo 160f
  3344. +
  3345. + /* Long case */
  3346. + push {DAT3, DAT4, DAT5, DAT6, DAT7}
  3347. +
  3348. + .cfi_def_cfa_offset 36
  3349. + .cfi_rel_offset D, 20
  3350. + .cfi_rel_offset DAT1, 24
  3351. + .cfi_rel_offset DAT2, 28
  3352. + .cfi_rel_offset DAT3, 0
  3353. + .cfi_rel_offset DAT4, 4
  3354. + .cfi_rel_offset DAT5, 8
  3355. + .cfi_rel_offset DAT6, 12
  3356. + .cfi_rel_offset DAT7, 16
  3357. + .cfi_rel_offset lr, 32
  3358. +
  3359. + /* Adjust N so that the decrement instruction can also test for
  3360. + * inner loop termination. We want it to stop when there are
  3361. + * (prefetch_distance+1) complete blocks to go. */
  3362. + sub N, N, #(prefetch_distance+2)*32
  3363. + preload_leading_step1 backwards, DAT0, S
  3364. + .if backwards
  3365. + /* Bug in GAS: it accepts, but mis-assembles the instruction
  3366. + * ands DAT2, D, #60, 2
  3367. + * which sets DAT2 to the number of leading bytes until destination is aligned and also clears C (sets borrow)
  3368. + */
  3369. + .word 0xE210513C
  3370. + beq 154f
  3371. + .else
  3372. + ands DAT2, D, #15
  3373. + beq 154f
  3374. + rsb DAT2, DAT2, #16 /* number of leading bytes until destination aligned */
  3375. + .endif
  3376. + preload_leading_step2 backwards, DAT0, S, DAT2, OFF
  3377. + memcpy_leading_15bytes backwards, 1
  3378. +154: /* Destination now 16-byte aligned; we have at least one prefetch as well as at least one 16-byte output block */
  3379. + /* Prefetch offset is best selected such that it lies in the first 8 of each 32 bytes - but it's just as easy to aim for the first one */
  3380. + .if backwards
  3381. + rsb OFF, S, #3
  3382. + and OFF, OFF, #28
  3383. + sub OFF, OFF, #32*(prefetch_distance+1)
  3384. + .else
  3385. + and OFF, S, #28
  3386. + rsb OFF, OFF, #32*prefetch_distance
  3387. + .endif
  3388. + movs DAT0, S, lsl #31
  3389. + bhi 157f
  3390. + bcs 156f
  3391. + bmi 155f
  3392. + memcpy_long_inner_loop backwards, 0
  3393. +155: memcpy_long_inner_loop backwards, 1
  3394. +156: memcpy_long_inner_loop backwards, 2
  3395. +157: memcpy_long_inner_loop backwards, 3
  3396. +
  3397. + .cfi_def_cfa_offset 16
  3398. + .cfi_rel_offset D, 0
  3399. + .cfi_rel_offset DAT1, 4
  3400. + .cfi_rel_offset DAT2, 8
  3401. + .cfi_same_value DAT3
  3402. + .cfi_same_value DAT4
  3403. + .cfi_same_value DAT5
  3404. + .cfi_same_value DAT6
  3405. + .cfi_same_value DAT7
  3406. + .cfi_rel_offset lr, 12
  3407. +
  3408. +160: /* Medium case */
  3409. + preload_all backwards, 0, 0, S, N, DAT2, OFF
  3410. + sub N, N, #16 /* simplifies inner loop termination */
  3411. + .if backwards
  3412. + ands DAT2, D, #15
  3413. + beq 164f
  3414. + .else
  3415. + ands DAT2, D, #15
  3416. + beq 164f
  3417. + rsb DAT2, DAT2, #16
  3418. + .endif
  3419. + memcpy_leading_15bytes backwards, align
  3420. +164: /* Destination now 16-byte aligned; we have at least one 16-byte output block */
  3421. + tst S, #3
  3422. + bne 140f
  3423. + memcpy_medium_inner_loop backwards, 0
  3424. +140: memcpy_medium_inner_loop backwards, 1
  3425. +
  3426. +170: /* Short case, less than 31 bytes, so no guarantee of at least one 16-byte block */
  3427. + teq N, #0
  3428. + beq 199f
  3429. + preload_all backwards, 1, 0, S, N, DAT2, LAST
  3430. + tst D, #3
  3431. + beq 174f
  3432. +172: subs N, N, #1
  3433. + blo 199f
  3434. + .if backwards
  3435. + ldrb DAT0, [S, #-1]!
  3436. + strb DAT0, [D, #-1]!
  3437. + .else
  3438. + ldrb DAT0, [S], #1
  3439. + strb DAT0, [D], #1
  3440. + .endif
  3441. + tst D, #3
  3442. + bne 172b
  3443. +174: /* Destination now 4-byte aligned; we have 0 or more output bytes to go */
  3444. + tst S, #3
  3445. + bne 140f
  3446. + memcpy_short_inner_loop backwards, 0
  3447. +140: memcpy_short_inner_loop backwards, 1
  3448. +
  3449. + .cfi_endproc
  3450. +
  3451. + .unreq D
  3452. + .unreq S
  3453. + .unreq N
  3454. + .unreq DAT0
  3455. + .unreq DAT1
  3456. + .unreq DAT2
  3457. + .unreq DAT3
  3458. + .unreq DAT4
  3459. + .unreq DAT5
  3460. + .unreq DAT6
  3461. + .unreq DAT7
  3462. + .unreq LAST
  3463. + .unreq OFF
  3464. +.endm
  3465. diff -Nur linux-3.12.33/arch/arm/lib/memcpy_rpi.S linux-3.12.33-rpi/arch/arm/lib/memcpy_rpi.S
  3466. --- linux-3.12.33/arch/arm/lib/memcpy_rpi.S 1969-12-31 18:00:00.000000000 -0600
  3467. +++ linux-3.12.33-rpi/arch/arm/lib/memcpy_rpi.S 2014-12-03 19:13:32.432418001 -0600
  3468. @@ -0,0 +1,59 @@
  3469. +/*
  3470. +Copyright (c) 2013, Raspberry Pi Foundation
  3471. +Copyright (c) 2013, RISC OS Open Ltd
  3472. +All rights reserved.
  3473. +
  3474. +Redistribution and use in source and binary forms, with or without
  3475. +modification, are permitted provided that the following conditions are met:
  3476. + * Redistributions of source code must retain the above copyright
  3477. + notice, this list of conditions and the following disclaimer.
  3478. + * Redistributions in binary form must reproduce the above copyright
  3479. + notice, this list of conditions and the following disclaimer in the
  3480. + documentation and/or other materials provided with the distribution.
  3481. + * Neither the name of the copyright holder nor the
  3482. + names of its contributors may be used to endorse or promote products
  3483. + derived from this software without specific prior written permission.
  3484. +
  3485. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  3486. +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  3487. +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  3488. +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  3489. +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  3490. +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  3491. +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  3492. +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3493. +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  3494. +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3495. +*/
  3496. +
  3497. +#include <linux/linkage.h>
  3498. +#include "arm-mem.h"
  3499. +#include "memcpymove.h"
  3500. +
  3501. +/* Prevent the stack from becoming executable */
  3502. +#if defined(__linux__) && defined(__ELF__)
  3503. +.section .note.GNU-stack,"",%progbits
  3504. +#endif
  3505. +
  3506. + .text
  3507. + .arch armv6
  3508. + .object_arch armv4
  3509. + .arm
  3510. + .altmacro
  3511. + .p2align 2
  3512. +
  3513. +/*
  3514. + * void *memcpy(void * restrict s1, const void * restrict s2, size_t n);
  3515. + * On entry:
  3516. + * a1 = pointer to destination
  3517. + * a2 = pointer to source
  3518. + * a3 = number of bytes to copy
  3519. + * On exit:
  3520. + * a1 preserved
  3521. + */
  3522. +
  3523. +.set prefetch_distance, 3
  3524. +
  3525. +ENTRY(memcpy)
  3526. + memcpy 0
  3527. +ENDPROC(memcpy)
  3528. diff -Nur linux-3.12.33/arch/arm/lib/memmove_rpi.S linux-3.12.33-rpi/arch/arm/lib/memmove_rpi.S
  3529. --- linux-3.12.33/arch/arm/lib/memmove_rpi.S 1969-12-31 18:00:00.000000000 -0600
  3530. +++ linux-3.12.33-rpi/arch/arm/lib/memmove_rpi.S 2014-12-03 19:13:32.432418001 -0600
  3531. @@ -0,0 +1,61 @@
  3532. +/*
  3533. +Copyright (c) 2013, Raspberry Pi Foundation
  3534. +Copyright (c) 2013, RISC OS Open Ltd
  3535. +All rights reserved.
  3536. +
  3537. +Redistribution and use in source and binary forms, with or without
  3538. +modification, are permitted provided that the following conditions are met:
  3539. + * Redistributions of source code must retain the above copyright
  3540. + notice, this list of conditions and the following disclaimer.
  3541. + * Redistributions in binary form must reproduce the above copyright
  3542. + notice, this list of conditions and the following disclaimer in the
  3543. + documentation and/or other materials provided with the distribution.
  3544. + * Neither the name of the copyright holder nor the
  3545. + names of its contributors may be used to endorse or promote products
  3546. + derived from this software without specific prior written permission.
  3547. +
  3548. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  3549. +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  3550. +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  3551. +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  3552. +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  3553. +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  3554. +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  3555. +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3556. +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  3557. +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3558. +*/
  3559. +
  3560. +#include <linux/linkage.h>
  3561. +#include "arm-mem.h"
  3562. +#include "memcpymove.h"
  3563. +
  3564. +/* Prevent the stack from becoming executable */
  3565. +#if defined(__linux__) && defined(__ELF__)
  3566. +.section .note.GNU-stack,"",%progbits
  3567. +#endif
  3568. +
  3569. + .text
  3570. + .arch armv6
  3571. + .object_arch armv4
  3572. + .arm
  3573. + .altmacro
  3574. + .p2align 2
  3575. +
  3576. +/*
  3577. + * void *memmove(void *s1, const void *s2, size_t n);
  3578. + * On entry:
  3579. + * a1 = pointer to destination
  3580. + * a2 = pointer to source
  3581. + * a3 = number of bytes to copy
  3582. + * On exit:
  3583. + * a1 preserved
  3584. + */
  3585. +
  3586. +.set prefetch_distance, 3
  3587. +
  3588. +ENTRY(memmove)
  3589. + cmp a2, a1
  3590. + bpl memcpy /* pl works even over -1 - 0 and 0x7fffffff - 0x80000000 boundaries */
  3591. + memcpy 1
  3592. +ENDPROC(memmove)
  3593. diff -Nur linux-3.12.33/arch/arm/lib/memset_rpi.S linux-3.12.33-rpi/arch/arm/lib/memset_rpi.S
  3594. --- linux-3.12.33/arch/arm/lib/memset_rpi.S 1969-12-31 18:00:00.000000000 -0600
  3595. +++ linux-3.12.33-rpi/arch/arm/lib/memset_rpi.S 2014-12-03 19:13:32.432418001 -0600
  3596. @@ -0,0 +1,121 @@
  3597. +/*
  3598. +Copyright (c) 2013, Raspberry Pi Foundation
  3599. +Copyright (c) 2013, RISC OS Open Ltd
  3600. +All rights reserved.
  3601. +
  3602. +Redistribution and use in source and binary forms, with or without
  3603. +modification, are permitted provided that the following conditions are met:
  3604. + * Redistributions of source code must retain the above copyright
  3605. + notice, this list of conditions and the following disclaimer.
  3606. + * Redistributions in binary form must reproduce the above copyright
  3607. + notice, this list of conditions and the following disclaimer in the
  3608. + documentation and/or other materials provided with the distribution.
  3609. + * Neither the name of the copyright holder nor the
  3610. + names of its contributors may be used to endorse or promote products
  3611. + derived from this software without specific prior written permission.
  3612. +
  3613. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  3614. +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  3615. +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  3616. +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  3617. +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  3618. +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  3619. +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  3620. +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3621. +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  3622. +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3623. +*/
  3624. +
  3625. +#include <linux/linkage.h>
  3626. +#include "arm-mem.h"
  3627. +
  3628. +/* Prevent the stack from becoming executable */
  3629. +#if defined(__linux__) && defined(__ELF__)
  3630. +.section .note.GNU-stack,"",%progbits
  3631. +#endif
  3632. +
  3633. + .text
  3634. + .arch armv6
  3635. + .object_arch armv4
  3636. + .arm
  3637. + .altmacro
  3638. + .p2align 2
  3639. +
  3640. +/*
  3641. + * void *memset(void *s, int c, size_t n);
  3642. + * On entry:
  3643. + * a1 = pointer to buffer to fill
  3644. + * a2 = byte pattern to fill with (caller-narrowed)
  3645. + * a3 = number of bytes to fill
  3646. + * On exit:
  3647. + * a1 preserved
  3648. + */
  3649. +ENTRY(memset)
  3650. + S .req a1
  3651. + DAT0 .req a2
  3652. + N .req a3
  3653. + DAT1 .req a4
  3654. + DAT2 .req ip
  3655. + DAT3 .req lr
  3656. +
  3657. + orr DAT0, DAT0, lsl #8
  3658. + push {S, lr}
  3659. + orr DAT0, DAT0, lsl #16
  3660. + mov DAT1, DAT0
  3661. +
  3662. + /* See if we're guaranteed to have at least one 16-byte aligned 16-byte write */
  3663. + cmp N, #31
  3664. + blo 170f
  3665. +
  3666. +161: sub N, N, #16 /* simplifies inner loop termination */
  3667. + /* Leading words and bytes */
  3668. + tst S, #15
  3669. + beq 164f
  3670. + rsb DAT3, S, #0 /* bits 0-3 = number of leading bytes until aligned */
  3671. + movs DAT2, DAT3, lsl #31
  3672. + submi N, N, #1
  3673. + strmib DAT0, [S], #1
  3674. + subcs N, N, #2
  3675. + strcsh DAT0, [S], #2
  3676. + movs DAT2, DAT3, lsl #29
  3677. + submi N, N, #4
  3678. + strmi DAT0, [S], #4
  3679. + subcs N, N, #8
  3680. + stmcsia S!, {DAT0, DAT1}
  3681. +164: /* Delayed set up of DAT2 and DAT3 so we could use them as scratch registers above */
  3682. + mov DAT2, DAT0
  3683. + mov DAT3, DAT0
  3684. + /* Now the inner loop of 16-byte stores */
  3685. +165: stmia S!, {DAT0, DAT1, DAT2, DAT3}
  3686. + subs N, N, #16
  3687. + bhs 165b
  3688. +166: /* Trailing words and bytes */
  3689. + movs N, N, lsl #29
  3690. + stmcsia S!, {DAT0, DAT1}
  3691. + strmi DAT0, [S], #4
  3692. + movs N, N, lsl #2
  3693. + strcsh DAT0, [S], #2
  3694. + strmib DAT0, [S]
  3695. +199: pop {S, pc}
  3696. +
  3697. +170: /* Short case */
  3698. + mov DAT2, DAT0
  3699. + mov DAT3, DAT0
  3700. + tst S, #3
  3701. + beq 174f
  3702. +172: subs N, N, #1
  3703. + blo 199b
  3704. + strb DAT0, [S], #1
  3705. + tst S, #3
  3706. + bne 172b
  3707. +174: tst N, #16
  3708. + stmneia S!, {DAT0, DAT1, DAT2, DAT3}
  3709. + b 166b
  3710. +
  3711. + .unreq S
  3712. + .unreq DAT0
  3713. + .unreq N
  3714. + .unreq DAT1
  3715. + .unreq DAT2
  3716. + .unreq DAT3
  3717. +ENDPROC(memset)
  3718. diff -Nur linux-3.12.33/arch/arm/lib/uaccess_with_memcpy.c linux-3.12.33-rpi/arch/arm/lib/uaccess_with_memcpy.c
  3719. --- linux-3.12.33/arch/arm/lib/uaccess_with_memcpy.c 2014-11-15 06:28:07.000000000 -0600
  3720. +++ linux-3.12.33-rpi/arch/arm/lib/uaccess_with_memcpy.c 2014-12-03 19:13:32.432418001 -0600
  3721. @@ -21,6 +21,14 @@
  3722. #include <asm/current.h>
  3723. #include <asm/page.h>
  3724. +#ifndef COPY_FROM_USER_THRESHOLD
  3725. +#define COPY_FROM_USER_THRESHOLD 64
  3726. +#endif
  3727. +
  3728. +#ifndef COPY_TO_USER_THRESHOLD
  3729. +#define COPY_TO_USER_THRESHOLD 64
  3730. +#endif
  3731. +
  3732. static int
  3733. pin_page_for_write(const void __user *_addr, pte_t **ptep, spinlock_t **ptlp)
  3734. {
  3735. @@ -56,7 +64,44 @@
  3736. return 1;
  3737. }
  3738. -static unsigned long noinline
  3739. +static int
  3740. +pin_page_for_read(const void __user *_addr, pte_t **ptep, spinlock_t **ptlp)
  3741. +{
  3742. + unsigned long addr = (unsigned long)_addr;
  3743. + pgd_t *pgd;
  3744. + pmd_t *pmd;
  3745. + pte_t *pte;
  3746. + pud_t *pud;
  3747. + spinlock_t *ptl;
  3748. +
  3749. + pgd = pgd_offset(current->mm, addr);
  3750. + if (unlikely(pgd_none(*pgd) || pgd_bad(*pgd)))
  3751. + {
  3752. + return 0;
  3753. + }
  3754. + pud = pud_offset(pgd, addr);
  3755. + if (unlikely(pud_none(*pud) || pud_bad(*pud)))
  3756. + {
  3757. + return 0;
  3758. + }
  3759. +
  3760. + pmd = pmd_offset(pud, addr);
  3761. + if (unlikely(pmd_none(*pmd) || pmd_bad(*pmd)))
  3762. + return 0;
  3763. +
  3764. + pte = pte_offset_map_lock(current->mm, pmd, addr, &ptl);
  3765. + if (unlikely(!pte_present(*pte) || !pte_young(*pte))) {
  3766. + pte_unmap_unlock(pte, ptl);
  3767. + return 0;
  3768. + }
  3769. +
  3770. + *ptep = pte;
  3771. + *ptlp = ptl;
  3772. +
  3773. + return 1;
  3774. +}
  3775. +
  3776. +unsigned long noinline
  3777. __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n)
  3778. {
  3779. int atomic;
  3780. @@ -103,6 +148,54 @@
  3781. return n;
  3782. }
  3783. +unsigned long noinline
  3784. +__copy_from_user_memcpy(void *to, const void __user *from, unsigned long n)
  3785. +{
  3786. + int atomic;
  3787. +
  3788. + if (unlikely(segment_eq(get_fs(), KERNEL_DS))) {
  3789. + memcpy(to, (const void *)from, n);
  3790. + return 0;
  3791. + }
  3792. +
  3793. + /* the mmap semaphore is taken only if not in an atomic context */
  3794. + atomic = in_atomic();
  3795. +
  3796. + if (!atomic)
  3797. + down_read(&current->mm->mmap_sem);
  3798. + while (n) {
  3799. + pte_t *pte;
  3800. + spinlock_t *ptl;
  3801. + int tocopy;
  3802. +
  3803. + while (!pin_page_for_read(from, &pte, &ptl)) {
  3804. + char temp;
  3805. + if (!atomic)
  3806. + up_read(&current->mm->mmap_sem);
  3807. + if (__get_user(temp, (char __user *)from))
  3808. + goto out;
  3809. + if (!atomic)
  3810. + down_read(&current->mm->mmap_sem);
  3811. + }
  3812. +
  3813. + tocopy = (~(unsigned long)from & ~PAGE_MASK) + 1;
  3814. + if (tocopy > n)
  3815. + tocopy = n;
  3816. +
  3817. + memcpy(to, (const void *)from, tocopy);
  3818. + to += tocopy;
  3819. + from += tocopy;
  3820. + n -= tocopy;
  3821. +
  3822. + pte_unmap_unlock(pte, ptl);
  3823. + }
  3824. + if (!atomic)
  3825. + up_read(&current->mm->mmap_sem);
  3826. +
  3827. +out:
  3828. + return n;
  3829. +}
  3830. +
  3831. unsigned long
  3832. __copy_to_user(void __user *to, const void *from, unsigned long n)
  3833. {
  3834. @@ -113,10 +206,25 @@
  3835. * With frame pointer disabled, tail call optimization kicks in
  3836. * as well making this test almost invisible.
  3837. */
  3838. - if (n < 64)
  3839. + if (n < COPY_TO_USER_THRESHOLD)
  3840. return __copy_to_user_std(to, from, n);
  3841. return __copy_to_user_memcpy(to, from, n);
  3842. }
  3843. +
  3844. +unsigned long
  3845. +__copy_from_user(void *to, const void __user *from, unsigned long n)
  3846. +{
  3847. + /*
  3848. + * This test is stubbed out of the main function above to keep
  3849. + * the overhead for small copies low by avoiding a large
  3850. + * register dump on the stack just to reload them right away.
  3851. + * With frame pointer disabled, tail call optimization kicks in
  3852. + * as well making this test almost invisible.
  3853. + */
  3854. + if (n < COPY_FROM_USER_THRESHOLD)
  3855. + return __copy_from_user_std(to, from, n);
  3856. + return __copy_from_user_memcpy(to, from, n);
  3857. +}
  3858. static unsigned long noinline
  3859. __clear_user_memset(void __user *addr, unsigned long n)
  3860. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/armctrl.c linux-3.12.33-rpi/arch/arm/mach-bcm2708/armctrl.c
  3861. --- linux-3.12.33/arch/arm/mach-bcm2708/armctrl.c 1969-12-31 18:00:00.000000000 -0600
  3862. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/armctrl.c 2014-12-03 19:13:32.448418001 -0600
  3863. @@ -0,0 +1,219 @@
  3864. +/*
  3865. + * linux/arch/arm/mach-bcm2708/armctrl.c
  3866. + *
  3867. + * Copyright (C) 2010 Broadcom
  3868. + *
  3869. + * This program is free software; you can redistribute it and/or modify
  3870. + * it under the terms of the GNU General Public License as published by
  3871. + * the Free Software Foundation; either version 2 of the License, or
  3872. + * (at your option) any later version.
  3873. + *
  3874. + * This program is distributed in the hope that it will be useful,
  3875. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3876. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3877. + * GNU General Public License for more details.
  3878. + *
  3879. + * You should have received a copy of the GNU General Public License
  3880. + * along with this program; if not, write to the Free Software
  3881. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  3882. + */
  3883. +#include <linux/init.h>
  3884. +#include <linux/list.h>
  3885. +#include <linux/io.h>
  3886. +#include <linux/version.h>
  3887. +#include <linux/syscore_ops.h>
  3888. +#include <linux/interrupt.h>
  3889. +
  3890. +#include <asm/mach/irq.h>
  3891. +#include <mach/hardware.h>
  3892. +#include "armctrl.h"
  3893. +
  3894. +/* For support of kernels >= 3.0 assume only one VIC for now*/
  3895. +static unsigned int remap_irqs[(INTERRUPT_ARASANSDIO + 1) - INTERRUPT_JPEG] = {
  3896. + INTERRUPT_VC_JPEG,
  3897. + INTERRUPT_VC_USB,
  3898. + INTERRUPT_VC_3D,
  3899. + INTERRUPT_VC_DMA2,
  3900. + INTERRUPT_VC_DMA3,
  3901. + INTERRUPT_VC_I2C,
  3902. + INTERRUPT_VC_SPI,
  3903. + INTERRUPT_VC_I2SPCM,
  3904. + INTERRUPT_VC_SDIO,
  3905. + INTERRUPT_VC_UART,
  3906. + INTERRUPT_VC_ARASANSDIO
  3907. +};
  3908. +
  3909. +static void armctrl_mask_irq(struct irq_data *d)
  3910. +{
  3911. + static const unsigned int disables[4] = {
  3912. + ARM_IRQ_DIBL1,
  3913. + ARM_IRQ_DIBL2,
  3914. + ARM_IRQ_DIBL3,
  3915. + 0
  3916. + };
  3917. +
  3918. + if (d->irq >= FIQ_START) {
  3919. + writel(0, __io_address(ARM_IRQ_FAST));
  3920. + } else {
  3921. + unsigned int data = (unsigned int)irq_get_chip_data(d->irq);
  3922. + writel(1 << (data & 0x1f), __io_address(disables[(data >> 5) & 0x3]));
  3923. + }
  3924. +}
  3925. +
  3926. +static void armctrl_unmask_irq(struct irq_data *d)
  3927. +{
  3928. + static const unsigned int enables[4] = {
  3929. + ARM_IRQ_ENBL1,
  3930. + ARM_IRQ_ENBL2,
  3931. + ARM_IRQ_ENBL3,
  3932. + 0
  3933. + };
  3934. +
  3935. + if (d->irq >= FIQ_START) {
  3936. + unsigned int data =
  3937. + (unsigned int)irq_get_chip_data(d->irq) - FIQ_START;
  3938. + writel(0x80 | data, __io_address(ARM_IRQ_FAST));
  3939. + } else {
  3940. + unsigned int data = (unsigned int)irq_get_chip_data(d->irq);
  3941. + writel(1 << (data & 0x1f), __io_address(enables[(data >> 5) & 0x3]));
  3942. + }
  3943. +}
  3944. +
  3945. +#if defined(CONFIG_PM)
  3946. +
  3947. +/* for kernels 3.xx use the new syscore_ops apis but for older kernels use the sys dev class */
  3948. +
  3949. +/* Static defines
  3950. + * struct armctrl_device - VIC PM device (< 3.xx)
  3951. + * @sysdev: The system device which is registered. (< 3.xx)
  3952. + * @irq: The IRQ number for the base of the VIC.
  3953. + * @base: The register base for the VIC.
  3954. + * @resume_sources: A bitmask of interrupts for resume.
  3955. + * @resume_irqs: The IRQs enabled for resume.
  3956. + * @int_select: Save for VIC_INT_SELECT.
  3957. + * @int_enable: Save for VIC_INT_ENABLE.
  3958. + * @soft_int: Save for VIC_INT_SOFT.
  3959. + * @protect: Save for VIC_PROTECT.
  3960. + */
  3961. +struct armctrl_info {
  3962. + void __iomem *base;
  3963. + int irq;
  3964. + u32 resume_sources;
  3965. + u32 resume_irqs;
  3966. + u32 int_select;
  3967. + u32 int_enable;
  3968. + u32 soft_int;
  3969. + u32 protect;
  3970. +} armctrl;
  3971. +
  3972. +static int armctrl_suspend(void)
  3973. +{
  3974. + return 0;
  3975. +}
  3976. +
  3977. +static void armctrl_resume(void)
  3978. +{
  3979. + return;
  3980. +}
  3981. +
  3982. +/**
  3983. + * armctrl_pm_register - Register a VIC for later power management control
  3984. + * @base: The base address of the VIC.
  3985. + * @irq: The base IRQ for the VIC.
  3986. + * @resume_sources: bitmask of interrupts allowed for resume sources.
  3987. + *
  3988. + * For older kernels (< 3.xx) do -
  3989. + * Register the VIC with the system device tree so that it can be notified
  3990. + * of suspend and resume requests and ensure that the correct actions are
  3991. + * taken to re-instate the settings on resume.
  3992. + */
  3993. +static void __init armctrl_pm_register(void __iomem * base, unsigned int irq,
  3994. + u32 resume_sources)
  3995. +{
  3996. + armctrl.base = base;
  3997. + armctrl.resume_sources = resume_sources;
  3998. + armctrl.irq = irq;
  3999. +}
  4000. +
  4001. +static int armctrl_set_wake(struct irq_data *d, unsigned int on)
  4002. +{
  4003. + unsigned int off = d->irq & 31;
  4004. + u32 bit = 1 << off;
  4005. +
  4006. + if (!(bit & armctrl.resume_sources))
  4007. + return -EINVAL;
  4008. +
  4009. + if (on)
  4010. + armctrl.resume_irqs |= bit;
  4011. + else
  4012. + armctrl.resume_irqs &= ~bit;
  4013. +
  4014. + return 0;
  4015. +}
  4016. +
  4017. +#else
  4018. +static inline void armctrl_pm_register(void __iomem * base, unsigned int irq,
  4019. + u32 arg1)
  4020. +{
  4021. +}
  4022. +
  4023. +#define armctrl_suspend NULL
  4024. +#define armctrl_resume NULL
  4025. +#define armctrl_set_wake NULL
  4026. +#endif /* CONFIG_PM */
  4027. +
  4028. +static struct syscore_ops armctrl_syscore_ops = {
  4029. + .suspend = armctrl_suspend,
  4030. + .resume = armctrl_resume,
  4031. +};
  4032. +
  4033. +/**
  4034. + * armctrl_syscore_init - initicall to register VIC pm functions
  4035. + *
  4036. + * This is called via late_initcall() to register
  4037. + * the resources for the VICs due to the early
  4038. + * nature of the VIC's registration.
  4039. +*/
  4040. +static int __init armctrl_syscore_init(void)
  4041. +{
  4042. + register_syscore_ops(&armctrl_syscore_ops);
  4043. + return 0;
  4044. +}
  4045. +
  4046. +late_initcall(armctrl_syscore_init);
  4047. +
  4048. +static struct irq_chip armctrl_chip = {
  4049. + .name = "ARMCTRL",
  4050. + .irq_ack = NULL,
  4051. + .irq_mask = armctrl_mask_irq,
  4052. + .irq_unmask = armctrl_unmask_irq,
  4053. + .irq_set_wake = armctrl_set_wake,
  4054. +};
  4055. +
  4056. +/**
  4057. + * armctrl_init - initialise a vectored interrupt controller
  4058. + * @base: iomem base address
  4059. + * @irq_start: starting interrupt number, must be muliple of 32
  4060. + * @armctrl_sources: bitmask of interrupt sources to allow
  4061. + * @resume_sources: bitmask of interrupt sources to allow for resume
  4062. + */
  4063. +int __init armctrl_init(void __iomem * base, unsigned int irq_start,
  4064. + u32 armctrl_sources, u32 resume_sources)
  4065. +{
  4066. + unsigned int irq;
  4067. +
  4068. + for (irq = 0; irq < NR_IRQS; irq++) {
  4069. + unsigned int data = irq;
  4070. + if (irq >= INTERRUPT_JPEG && irq <= INTERRUPT_ARASANSDIO)
  4071. + data = remap_irqs[irq - INTERRUPT_JPEG];
  4072. +
  4073. + irq_set_chip(irq, &armctrl_chip);
  4074. + irq_set_chip_data(irq, (void *)data);
  4075. + irq_set_handler(irq, handle_level_irq);
  4076. + set_irq_flags(irq, IRQF_VALID | IRQF_PROBE | IRQF_DISABLED);
  4077. + }
  4078. +
  4079. + armctrl_pm_register(base, irq_start, resume_sources);
  4080. + init_FIQ(FIQ_START);
  4081. + return 0;
  4082. +}
  4083. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/armctrl.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/armctrl.h
  4084. --- linux-3.12.33/arch/arm/mach-bcm2708/armctrl.h 1969-12-31 18:00:00.000000000 -0600
  4085. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/armctrl.h 2014-12-03 19:13:32.448418001 -0600
  4086. @@ -0,0 +1,27 @@
  4087. +/*
  4088. + * linux/arch/arm/mach-bcm2708/armctrl.h
  4089. + *
  4090. + * Copyright (C) 2010 Broadcom
  4091. + *
  4092. + * This program is free software; you can redistribute it and/or modify
  4093. + * it under the terms of the GNU General Public License as published by
  4094. + * the Free Software Foundation; either version 2 of the License, or
  4095. + * (at your option) any later version.
  4096. + *
  4097. + * This program is distributed in the hope that it will be useful,
  4098. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4099. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4100. + * GNU General Public License for more details.
  4101. + *
  4102. + * You should have received a copy of the GNU General Public License
  4103. + * along with this program; if not, write to the Free Software
  4104. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  4105. + */
  4106. +
  4107. +#ifndef __BCM2708_ARMCTRL_H
  4108. +#define __BCM2708_ARMCTRL_H
  4109. +
  4110. +extern int __init armctrl_init(void __iomem * base, unsigned int irq_start,
  4111. + u32 armctrl_sources, u32 resume_sources);
  4112. +
  4113. +#endif
  4114. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/bcm2708.c linux-3.12.33-rpi/arch/arm/mach-bcm2708/bcm2708.c
  4115. --- linux-3.12.33/arch/arm/mach-bcm2708/bcm2708.c 1969-12-31 18:00:00.000000000 -0600
  4116. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/bcm2708.c 2014-12-03 19:13:32.448418001 -0600
  4117. @@ -0,0 +1,1139 @@
  4118. +/*
  4119. + * linux/arch/arm/mach-bcm2708/bcm2708.c
  4120. + *
  4121. + * Copyright (C) 2010 Broadcom
  4122. + *
  4123. + * This program is free software; you can redistribute it and/or modify
  4124. + * it under the terms of the GNU General Public License as published by
  4125. + * the Free Software Foundation; either version 2 of the License, or
  4126. + * (at your option) any later version.
  4127. + *
  4128. + * This program is distributed in the hope that it will be useful,
  4129. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4130. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4131. + * GNU General Public License for more details.
  4132. + *
  4133. + * You should have received a copy of the GNU General Public License
  4134. + * along with this program; if not, write to the Free Software
  4135. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  4136. + */
  4137. +
  4138. +#include <linux/init.h>
  4139. +#include <linux/device.h>
  4140. +#include <linux/dma-mapping.h>
  4141. +#include <linux/serial_8250.h>
  4142. +#include <linux/platform_device.h>
  4143. +#include <linux/syscore_ops.h>
  4144. +#include <linux/interrupt.h>
  4145. +#include <linux/amba/bus.h>
  4146. +#include <linux/amba/clcd.h>
  4147. +#include <linux/clockchips.h>
  4148. +#include <linux/cnt32_to_63.h>
  4149. +#include <linux/io.h>
  4150. +#include <linux/module.h>
  4151. +#include <linux/spi/spi.h>
  4152. +#include <linux/w1-gpio.h>
  4153. +#include <linux/pps-gpio.h>
  4154. +
  4155. +#include <linux/version.h>
  4156. +#include <linux/clkdev.h>
  4157. +#include <asm/system.h>
  4158. +#include <mach/hardware.h>
  4159. +#include <asm/irq.h>
  4160. +#include <linux/leds.h>
  4161. +#include <asm/mach-types.h>
  4162. +#include <linux/sched_clock.h>
  4163. +
  4164. +#include <asm/mach/arch.h>
  4165. +#include <asm/mach/flash.h>
  4166. +#include <asm/mach/irq.h>
  4167. +#include <asm/mach/time.h>
  4168. +#include <asm/mach/map.h>
  4169. +
  4170. +#include <mach/timex.h>
  4171. +#include <mach/dma.h>
  4172. +#include <mach/vcio.h>
  4173. +#include <mach/system.h>
  4174. +
  4175. +#include <linux/delay.h>
  4176. +
  4177. +#include "bcm2708.h"
  4178. +#include "armctrl.h"
  4179. +#include "clock.h"
  4180. +
  4181. +#ifdef CONFIG_BCM_VC_CMA
  4182. +#include <linux/broadcom/vc_cma.h>
  4183. +#endif
  4184. +
  4185. +
  4186. +/* Effectively we have an IOMMU (ARM<->VideoCore map) that is set up to
  4187. + * give us IO access only to 64Mbytes of physical memory (26 bits). We could
  4188. + * represent this window by setting our dmamasks to 26 bits but, in fact
  4189. + * we're not going to use addresses outside this range (they're not in real
  4190. + * memory) so we don't bother.
  4191. + *
  4192. + * In the future we might include code to use this IOMMU to remap other
  4193. + * physical addresses onto VideoCore memory then the use of 32-bits would be
  4194. + * more legitimate.
  4195. + */
  4196. +#define DMA_MASK_BITS_COMMON 32
  4197. +
  4198. +// use GPIO 4 for the one-wire GPIO pin, if enabled
  4199. +#define W1_GPIO 4
  4200. +// ensure one-wire GPIO pullup is disabled by default
  4201. +#define W1_PULLUP -1
  4202. +
  4203. +/* command line parameters */
  4204. +static unsigned boardrev, serial;
  4205. +static unsigned uart_clock;
  4206. +static unsigned disk_led_gpio = 16;
  4207. +static unsigned disk_led_active_low = 1;
  4208. +static unsigned reboot_part = 0;
  4209. +static unsigned w1_gpio_pin = W1_GPIO;
  4210. +static unsigned w1_gpio_pullup = W1_PULLUP;
  4211. +static int pps_gpio_pin = -1;
  4212. +static unsigned bcm2835_mmc = 1;
  4213. +static bool vc_i2c_override = false;
  4214. +
  4215. +static void __init bcm2708_init_led(void);
  4216. +
  4217. +void __init bcm2708_init_irq(void)
  4218. +{
  4219. + armctrl_init(__io_address(ARMCTRL_IC_BASE), 0, 0, 0);
  4220. +}
  4221. +
  4222. +static struct map_desc bcm2708_io_desc[] __initdata = {
  4223. + {
  4224. + .virtual = IO_ADDRESS(ARMCTRL_BASE),
  4225. + .pfn = __phys_to_pfn(ARMCTRL_BASE),
  4226. + .length = SZ_4K,
  4227. + .type = MT_DEVICE},
  4228. + {
  4229. + .virtual = IO_ADDRESS(UART0_BASE),
  4230. + .pfn = __phys_to_pfn(UART0_BASE),
  4231. + .length = SZ_4K,
  4232. + .type = MT_DEVICE},
  4233. + {
  4234. + .virtual = IO_ADDRESS(UART1_BASE),
  4235. + .pfn = __phys_to_pfn(UART1_BASE),
  4236. + .length = SZ_4K,
  4237. + .type = MT_DEVICE},
  4238. + {
  4239. + .virtual = IO_ADDRESS(DMA_BASE),
  4240. + .pfn = __phys_to_pfn(DMA_BASE),
  4241. + .length = SZ_4K,
  4242. + .type = MT_DEVICE},
  4243. + {
  4244. + .virtual = IO_ADDRESS(MCORE_BASE),
  4245. + .pfn = __phys_to_pfn(MCORE_BASE),
  4246. + .length = SZ_4K,
  4247. + .type = MT_DEVICE},
  4248. + {
  4249. + .virtual = IO_ADDRESS(ST_BASE),
  4250. + .pfn = __phys_to_pfn(ST_BASE),
  4251. + .length = SZ_4K,
  4252. + .type = MT_DEVICE},
  4253. + {
  4254. + .virtual = IO_ADDRESS(USB_BASE),
  4255. + .pfn = __phys_to_pfn(USB_BASE),
  4256. + .length = SZ_128K,
  4257. + .type = MT_DEVICE},
  4258. + {
  4259. + .virtual = IO_ADDRESS(PM_BASE),
  4260. + .pfn = __phys_to_pfn(PM_BASE),
  4261. + .length = SZ_4K,
  4262. + .type = MT_DEVICE},
  4263. + {
  4264. + .virtual = IO_ADDRESS(GPIO_BASE),
  4265. + .pfn = __phys_to_pfn(GPIO_BASE),
  4266. + .length = SZ_4K,
  4267. + .type = MT_DEVICE}
  4268. +};
  4269. +
  4270. +void __init bcm2708_map_io(void)
  4271. +{
  4272. + iotable_init(bcm2708_io_desc, ARRAY_SIZE(bcm2708_io_desc));
  4273. +}
  4274. +
  4275. +/* The STC is a free running counter that increments at the rate of 1MHz */
  4276. +#define STC_FREQ_HZ 1000000
  4277. +
  4278. +static inline uint32_t timer_read(void)
  4279. +{
  4280. + /* STC: a free running counter that increments at the rate of 1MHz */
  4281. + return readl(__io_address(ST_BASE + 0x04));
  4282. +}
  4283. +
  4284. +static unsigned long bcm2708_read_current_timer(void)
  4285. +{
  4286. + return timer_read();
  4287. +}
  4288. +
  4289. +static u32 notrace bcm2708_read_sched_clock(void)
  4290. +{
  4291. + return timer_read();
  4292. +}
  4293. +
  4294. +static cycle_t clksrc_read(struct clocksource *cs)
  4295. +{
  4296. + return timer_read();
  4297. +}
  4298. +
  4299. +static struct clocksource clocksource_stc = {
  4300. + .name = "stc",
  4301. + .rating = 300,
  4302. + .read = clksrc_read,
  4303. + .mask = CLOCKSOURCE_MASK(32),
  4304. + .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  4305. +};
  4306. +
  4307. +unsigned long frc_clock_ticks32(void)
  4308. +{
  4309. + return timer_read();
  4310. +}
  4311. +
  4312. +static void __init bcm2708_clocksource_init(void)
  4313. +{
  4314. + if (clocksource_register_hz(&clocksource_stc, STC_FREQ_HZ)) {
  4315. + printk(KERN_ERR "timer: failed to initialize clock "
  4316. + "source %s\n", clocksource_stc.name);
  4317. + }
  4318. +}
  4319. +
  4320. +
  4321. +/*
  4322. + * These are fixed clocks.
  4323. + */
  4324. +static struct clk ref24_clk = {
  4325. + .rate = UART0_CLOCK, /* The UART is clocked at 3MHz via APB_CLK */
  4326. +};
  4327. +
  4328. +static struct clk osc_clk = {
  4329. +#ifdef CONFIG_ARCH_BCM2708_CHIPIT
  4330. + .rate = 27000000,
  4331. +#else
  4332. + .rate = 500000000, /* ARM clock is set from the VideoCore booter */
  4333. +#endif
  4334. +};
  4335. +
  4336. +/* warning - the USB needs a clock > 34MHz */
  4337. +
  4338. +static struct clk sdhost_clk = {
  4339. +#ifdef CONFIG_ARCH_BCM2708_CHIPIT
  4340. + .rate = 4000000, /* 4MHz */
  4341. +#else
  4342. + .rate = 250000000, /* 250MHz */
  4343. +#endif
  4344. +};
  4345. +
  4346. +static struct clk_lookup lookups[] = {
  4347. + { /* UART0 */
  4348. + .dev_id = "dev:f1",
  4349. + .clk = &ref24_clk,
  4350. + },
  4351. + { /* USB */
  4352. + .dev_id = "bcm2708_usb",
  4353. + .clk = &osc_clk,
  4354. + }, { /* SPI */
  4355. + .dev_id = "bcm2708_spi.0",
  4356. + .clk = &sdhost_clk,
  4357. + }, { /* BSC0 */
  4358. + .dev_id = "bcm2708_i2c.0",
  4359. + .clk = &sdhost_clk,
  4360. + }, { /* BSC1 */
  4361. + .dev_id = "bcm2708_i2c.1",
  4362. + .clk = &sdhost_clk,
  4363. + }
  4364. +};
  4365. +
  4366. +#define UART0_IRQ { IRQ_UART, 0 /*NO_IRQ*/ }
  4367. +#define UART0_DMA { 15, 14 }
  4368. +
  4369. +AMBA_DEVICE(uart0, "dev:f1", UART0, NULL);
  4370. +
  4371. +static struct amba_device *amba_devs[] __initdata = {
  4372. + &uart0_device,
  4373. +};
  4374. +
  4375. +static struct resource bcm2708_dmaman_resources[] = {
  4376. + {
  4377. + .start = DMA_BASE,
  4378. + .end = DMA_BASE + SZ_4K - 1,
  4379. + .flags = IORESOURCE_MEM,
  4380. + }
  4381. +};
  4382. +
  4383. +static struct platform_device bcm2708_dmaman_device = {
  4384. + .name = BCM_DMAMAN_DRIVER_NAME,
  4385. + .id = 0, /* first bcm2708_dma */
  4386. + .resource = bcm2708_dmaman_resources,
  4387. + .num_resources = ARRAY_SIZE(bcm2708_dmaman_resources),
  4388. +};
  4389. +
  4390. +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
  4391. +static struct w1_gpio_platform_data w1_gpio_pdata = {
  4392. + .pin = W1_GPIO,
  4393. + .ext_pullup_enable_pin = W1_PULLUP,
  4394. + .is_open_drain = 0,
  4395. +};
  4396. +
  4397. +static struct platform_device w1_device = {
  4398. + .name = "w1-gpio",
  4399. + .id = -1,
  4400. + .dev.platform_data = &w1_gpio_pdata,
  4401. +};
  4402. +#endif
  4403. +
  4404. +static struct pps_gpio_platform_data pps_gpio_info = {
  4405. + .assert_falling_edge = false,
  4406. + .capture_clear = false,
  4407. + .gpio_pin = -1,
  4408. + .gpio_label = "PPS",
  4409. +};
  4410. +
  4411. +static struct platform_device pps_gpio_device = {
  4412. + .name = "pps-gpio",
  4413. + .id = PLATFORM_DEVID_NONE,
  4414. + .dev.platform_data = &pps_gpio_info,
  4415. +};
  4416. +
  4417. +static u64 fb_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  4418. +
  4419. +static struct platform_device bcm2708_fb_device = {
  4420. + .name = "bcm2708_fb",
  4421. + .id = -1, /* only one bcm2708_fb */
  4422. + .resource = NULL,
  4423. + .num_resources = 0,
  4424. + .dev = {
  4425. + .dma_mask = &fb_dmamask,
  4426. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  4427. + },
  4428. +};
  4429. +
  4430. +static struct plat_serial8250_port bcm2708_uart1_platform_data[] = {
  4431. + {
  4432. + .mapbase = UART1_BASE + 0x40,
  4433. + .irq = IRQ_AUX,
  4434. + .uartclk = 125000000,
  4435. + .regshift = 2,
  4436. + .iotype = UPIO_MEM,
  4437. + .flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_SKIP_TEST,
  4438. + .type = PORT_8250,
  4439. + },
  4440. + {},
  4441. +};
  4442. +
  4443. +static struct platform_device bcm2708_uart1_device = {
  4444. + .name = "serial8250",
  4445. + .id = PLAT8250_DEV_PLATFORM,
  4446. + .dev = {
  4447. + .platform_data = bcm2708_uart1_platform_data,
  4448. + },
  4449. +};
  4450. +
  4451. +static struct resource bcm2708_usb_resources[] = {
  4452. + [0] = {
  4453. + .start = USB_BASE,
  4454. + .end = USB_BASE + SZ_128K - 1,
  4455. + .flags = IORESOURCE_MEM,
  4456. + },
  4457. + [1] = {
  4458. + .start = MPHI_BASE,
  4459. + .end = MPHI_BASE + SZ_4K - 1,
  4460. + .flags = IORESOURCE_MEM,
  4461. + },
  4462. + [2] = {
  4463. + .start = IRQ_HOSTPORT,
  4464. + .end = IRQ_HOSTPORT,
  4465. + .flags = IORESOURCE_IRQ,
  4466. + },
  4467. + [3] = {
  4468. + .start = IRQ_USB,
  4469. + .end = IRQ_USB,
  4470. + .flags = IORESOURCE_IRQ,
  4471. + },
  4472. +};
  4473. +
  4474. +
  4475. +static u64 usb_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  4476. +
  4477. +static struct platform_device bcm2708_usb_device = {
  4478. + .name = "bcm2708_usb",
  4479. + .id = -1, /* only one bcm2708_usb */
  4480. + .resource = bcm2708_usb_resources,
  4481. + .num_resources = ARRAY_SIZE(bcm2708_usb_resources),
  4482. + .dev = {
  4483. + .dma_mask = &usb_dmamask,
  4484. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  4485. + },
  4486. +};
  4487. +
  4488. +static struct resource bcm2708_vcio_resources[] = {
  4489. + [0] = { /* mailbox/semaphore/doorbell access */
  4490. + .start = MCORE_BASE,
  4491. + .end = MCORE_BASE + SZ_4K - 1,
  4492. + .flags = IORESOURCE_MEM,
  4493. + },
  4494. +};
  4495. +
  4496. +static u64 vcio_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  4497. +
  4498. +static struct platform_device bcm2708_vcio_device = {
  4499. + .name = BCM_VCIO_DRIVER_NAME,
  4500. + .id = -1, /* only one VideoCore I/O area */
  4501. + .resource = bcm2708_vcio_resources,
  4502. + .num_resources = ARRAY_SIZE(bcm2708_vcio_resources),
  4503. + .dev = {
  4504. + .dma_mask = &vcio_dmamask,
  4505. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  4506. + },
  4507. +};
  4508. +
  4509. +#ifdef CONFIG_BCM2708_GPIO
  4510. +#define BCM_GPIO_DRIVER_NAME "bcm2708_gpio"
  4511. +
  4512. +static struct resource bcm2708_gpio_resources[] = {
  4513. + [0] = { /* general purpose I/O */
  4514. + .start = GPIO_BASE,
  4515. + .end = GPIO_BASE + SZ_4K - 1,
  4516. + .flags = IORESOURCE_MEM,
  4517. + },
  4518. +};
  4519. +
  4520. +static u64 gpio_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  4521. +
  4522. +static struct platform_device bcm2708_gpio_device = {
  4523. + .name = BCM_GPIO_DRIVER_NAME,
  4524. + .id = -1, /* only one VideoCore I/O area */
  4525. + .resource = bcm2708_gpio_resources,
  4526. + .num_resources = ARRAY_SIZE(bcm2708_gpio_resources),
  4527. + .dev = {
  4528. + .dma_mask = &gpio_dmamask,
  4529. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  4530. + },
  4531. +};
  4532. +#endif
  4533. +
  4534. +static struct resource bcm2708_systemtimer_resources[] = {
  4535. + [0] = { /* system timer access */
  4536. + .start = ST_BASE,
  4537. + .end = ST_BASE + SZ_4K - 1,
  4538. + .flags = IORESOURCE_MEM,
  4539. + },
  4540. + {
  4541. + .start = IRQ_TIMER3,
  4542. + .end = IRQ_TIMER3,
  4543. + .flags = IORESOURCE_IRQ,
  4544. + }
  4545. +
  4546. +};
  4547. +
  4548. +static u64 systemtimer_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  4549. +
  4550. +static struct platform_device bcm2708_systemtimer_device = {
  4551. + .name = "bcm2708_systemtimer",
  4552. + .id = -1, /* only one VideoCore I/O area */
  4553. + .resource = bcm2708_systemtimer_resources,
  4554. + .num_resources = ARRAY_SIZE(bcm2708_systemtimer_resources),
  4555. + .dev = {
  4556. + .dma_mask = &systemtimer_dmamask,
  4557. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  4558. + },
  4559. +};
  4560. +
  4561. +#ifdef CONFIG_MMC_SDHCI_BCM2708 /* Arasan emmc SD */
  4562. +static struct resource bcm2708_emmc_resources[] = {
  4563. + [0] = {
  4564. + .start = EMMC_BASE,
  4565. + .end = EMMC_BASE + SZ_256 - 1, /* we only need this area */
  4566. + /* the memory map actually makes SZ_4K available */
  4567. + .flags = IORESOURCE_MEM,
  4568. + },
  4569. + [1] = {
  4570. + .start = IRQ_ARASANSDIO,
  4571. + .end = IRQ_ARASANSDIO,
  4572. + .flags = IORESOURCE_IRQ,
  4573. + },
  4574. +};
  4575. +
  4576. +static u64 bcm2708_emmc_dmamask = 0xffffffffUL;
  4577. +
  4578. +struct platform_device bcm2708_emmc_device = {
  4579. + .name = "bcm2708_sdhci",
  4580. + .id = 0,
  4581. + .num_resources = ARRAY_SIZE(bcm2708_emmc_resources),
  4582. + .resource = bcm2708_emmc_resources,
  4583. + .dev = {
  4584. + .dma_mask = &bcm2708_emmc_dmamask,
  4585. + .coherent_dma_mask = 0xffffffffUL},
  4586. +};
  4587. +#endif /* CONFIG_MMC_SDHCI_BCM2708 */
  4588. +
  4589. +#ifdef CONFIG_MMC_BCM2835 /* Arasan emmc SD (new) */
  4590. +static struct resource bcm2835_emmc_resources[] = {
  4591. + [0] = {
  4592. + .start = EMMC_BASE,
  4593. + .end = EMMC_BASE + SZ_256 - 1, /* we only need this area */
  4594. + /* the memory map actually makes SZ_4K available */
  4595. + .flags = IORESOURCE_MEM,
  4596. + },
  4597. + [1] = {
  4598. + .start = IRQ_ARASANSDIO,
  4599. + .end = IRQ_ARASANSDIO,
  4600. + .flags = IORESOURCE_IRQ,
  4601. + },
  4602. +};
  4603. +
  4604. +static u64 bcm2835_emmc_dmamask = 0xffffffffUL;
  4605. +
  4606. +struct platform_device bcm2835_emmc_device = {
  4607. + .name = "mmc-bcm2835",
  4608. + .id = 0,
  4609. + .num_resources = ARRAY_SIZE(bcm2835_emmc_resources),
  4610. + .resource = bcm2835_emmc_resources,
  4611. + .dev = {
  4612. + .dma_mask = &bcm2835_emmc_dmamask,
  4613. + .coherent_dma_mask = 0xffffffffUL},
  4614. +};
  4615. +#endif /* CONFIG_MMC_BCM2835 */
  4616. +
  4617. +static struct resource bcm2708_powerman_resources[] = {
  4618. + [0] = {
  4619. + .start = PM_BASE,
  4620. + .end = PM_BASE + SZ_256 - 1,
  4621. + .flags = IORESOURCE_MEM,
  4622. + },
  4623. +};
  4624. +
  4625. +static u64 powerman_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  4626. +
  4627. +struct platform_device bcm2708_powerman_device = {
  4628. + .name = "bcm2708_powerman",
  4629. + .id = 0,
  4630. + .num_resources = ARRAY_SIZE(bcm2708_powerman_resources),
  4631. + .resource = bcm2708_powerman_resources,
  4632. + .dev = {
  4633. + .dma_mask = &powerman_dmamask,
  4634. + .coherent_dma_mask = 0xffffffffUL},
  4635. +};
  4636. +
  4637. +
  4638. +static struct platform_device bcm2708_alsa_devices[] = {
  4639. + [0] = {
  4640. + .name = "bcm2835_AUD0",
  4641. + .id = 0, /* first audio device */
  4642. + .resource = 0,
  4643. + .num_resources = 0,
  4644. + },
  4645. + [1] = {
  4646. + .name = "bcm2835_AUD1",
  4647. + .id = 1, /* second audio device */
  4648. + .resource = 0,
  4649. + .num_resources = 0,
  4650. + },
  4651. + [2] = {
  4652. + .name = "bcm2835_AUD2",
  4653. + .id = 2, /* third audio device */
  4654. + .resource = 0,
  4655. + .num_resources = 0,
  4656. + },
  4657. + [3] = {
  4658. + .name = "bcm2835_AUD3",
  4659. + .id = 3, /* forth audio device */
  4660. + .resource = 0,
  4661. + .num_resources = 0,
  4662. + },
  4663. + [4] = {
  4664. + .name = "bcm2835_AUD4",
  4665. + .id = 4, /* fifth audio device */
  4666. + .resource = 0,
  4667. + .num_resources = 0,
  4668. + },
  4669. + [5] = {
  4670. + .name = "bcm2835_AUD5",
  4671. + .id = 5, /* sixth audio device */
  4672. + .resource = 0,
  4673. + .num_resources = 0,
  4674. + },
  4675. + [6] = {
  4676. + .name = "bcm2835_AUD6",
  4677. + .id = 6, /* seventh audio device */
  4678. + .resource = 0,
  4679. + .num_resources = 0,
  4680. + },
  4681. + [7] = {
  4682. + .name = "bcm2835_AUD7",
  4683. + .id = 7, /* eighth audio device */
  4684. + .resource = 0,
  4685. + .num_resources = 0,
  4686. + },
  4687. +};
  4688. +
  4689. +static struct resource bcm2708_spi_resources[] = {
  4690. + {
  4691. + .start = SPI0_BASE,
  4692. + .end = SPI0_BASE + SZ_256 - 1,
  4693. + .flags = IORESOURCE_MEM,
  4694. + }, {
  4695. + .start = IRQ_SPI,
  4696. + .end = IRQ_SPI,
  4697. + .flags = IORESOURCE_IRQ,
  4698. + }
  4699. +};
  4700. +
  4701. +
  4702. +static u64 bcm2708_spi_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  4703. +static struct platform_device bcm2708_spi_device = {
  4704. + .name = "bcm2708_spi",
  4705. + .id = 0,
  4706. + .num_resources = ARRAY_SIZE(bcm2708_spi_resources),
  4707. + .resource = bcm2708_spi_resources,
  4708. + .dev = {
  4709. + .dma_mask = &bcm2708_spi_dmamask,
  4710. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON)},
  4711. +};
  4712. +
  4713. +#ifdef CONFIG_BCM2708_SPIDEV
  4714. +static struct spi_board_info bcm2708_spi_devices[] = {
  4715. +#ifdef CONFIG_SPI_SPIDEV
  4716. + {
  4717. + .modalias = "spidev",
  4718. + .max_speed_hz = 500000,
  4719. + .bus_num = 0,
  4720. + .chip_select = 0,
  4721. + .mode = SPI_MODE_0,
  4722. + }, {
  4723. + .modalias = "spidev",
  4724. + .max_speed_hz = 500000,
  4725. + .bus_num = 0,
  4726. + .chip_select = 1,
  4727. + .mode = SPI_MODE_0,
  4728. + }
  4729. +#endif
  4730. +};
  4731. +#endif
  4732. +
  4733. +static struct resource bcm2708_bsc0_resources[] = {
  4734. + {
  4735. + .start = BSC0_BASE,
  4736. + .end = BSC0_BASE + SZ_256 - 1,
  4737. + .flags = IORESOURCE_MEM,
  4738. + }, {
  4739. + .start = INTERRUPT_I2C,
  4740. + .end = INTERRUPT_I2C,
  4741. + .flags = IORESOURCE_IRQ,
  4742. + }
  4743. +};
  4744. +
  4745. +static struct platform_device bcm2708_bsc0_device = {
  4746. + .name = "bcm2708_i2c",
  4747. + .id = 0,
  4748. + .num_resources = ARRAY_SIZE(bcm2708_bsc0_resources),
  4749. + .resource = bcm2708_bsc0_resources,
  4750. +};
  4751. +
  4752. +
  4753. +static struct resource bcm2708_bsc1_resources[] = {
  4754. + {
  4755. + .start = BSC1_BASE,
  4756. + .end = BSC1_BASE + SZ_256 - 1,
  4757. + .flags = IORESOURCE_MEM,
  4758. + }, {
  4759. + .start = INTERRUPT_I2C,
  4760. + .end = INTERRUPT_I2C,
  4761. + .flags = IORESOURCE_IRQ,
  4762. + }
  4763. +};
  4764. +
  4765. +static struct platform_device bcm2708_bsc1_device = {
  4766. + .name = "bcm2708_i2c",
  4767. + .id = 1,
  4768. + .num_resources = ARRAY_SIZE(bcm2708_bsc1_resources),
  4769. + .resource = bcm2708_bsc1_resources,
  4770. +};
  4771. +
  4772. +static struct platform_device bcm2835_hwmon_device = {
  4773. + .name = "bcm2835_hwmon",
  4774. +};
  4775. +
  4776. +static struct platform_device bcm2835_thermal_device = {
  4777. + .name = "bcm2835_thermal",
  4778. +};
  4779. +
  4780. +#ifdef CONFIG_SND_BCM2708_SOC_I2S_MODULE
  4781. +static struct resource bcm2708_i2s_resources[] = {
  4782. + {
  4783. + .start = I2S_BASE,
  4784. + .end = I2S_BASE + 0x20,
  4785. + .flags = IORESOURCE_MEM,
  4786. + },
  4787. + {
  4788. + .start = PCM_CLOCK_BASE,
  4789. + .end = PCM_CLOCK_BASE + 0x02,
  4790. + .flags = IORESOURCE_MEM,
  4791. + }
  4792. +};
  4793. +
  4794. +static struct platform_device bcm2708_i2s_device = {
  4795. + .name = "bcm2708-i2s",
  4796. + .id = 0,
  4797. + .num_resources = ARRAY_SIZE(bcm2708_i2s_resources),
  4798. + .resource = bcm2708_i2s_resources,
  4799. +};
  4800. +#endif
  4801. +
  4802. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC_MODULE)
  4803. +static struct platform_device snd_hifiberry_dac_device = {
  4804. + .name = "snd-hifiberry-dac",
  4805. + .id = 0,
  4806. + .num_resources = 0,
  4807. +};
  4808. +
  4809. +static struct platform_device snd_pcm5102a_codec_device = {
  4810. + .name = "pcm5102a-codec",
  4811. + .id = -1,
  4812. + .num_resources = 0,
  4813. +};
  4814. +#endif
  4815. +
  4816. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS_MODULE)
  4817. +static struct platform_device snd_rpi_hifiberry_dacplus_device = {
  4818. + .name = "snd-rpi-hifiberry-dacplus",
  4819. + .id = 0,
  4820. + .num_resources = 0,
  4821. +};
  4822. +
  4823. +static struct i2c_board_info __initdata snd_pcm512x_hbdacplus_i2c_devices[] = {
  4824. + {
  4825. + I2C_BOARD_INFO("pcm5122", 0x4d)
  4826. + },
  4827. +};
  4828. +#endif
  4829. +
  4830. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI_MODULE)
  4831. +static struct platform_device snd_hifiberry_digi_device = {
  4832. + .name = "snd-hifiberry-digi",
  4833. + .id = 0,
  4834. + .num_resources = 0,
  4835. +};
  4836. +
  4837. +static struct i2c_board_info __initdata snd_wm8804_i2c_devices[] = {
  4838. + {
  4839. + I2C_BOARD_INFO("wm8804", 0x3b)
  4840. + },
  4841. +};
  4842. +
  4843. +#endif
  4844. +
  4845. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP_MODULE)
  4846. +static struct platform_device snd_hifiberry_amp_device = {
  4847. + .name = "snd-hifiberry-amp",
  4848. + .id = 0,
  4849. + .num_resources = 0,
  4850. +};
  4851. +
  4852. +static struct i2c_board_info __initdata snd_tas5713_i2c_devices[] = {
  4853. + {
  4854. + I2C_BOARD_INFO("tas5713", 0x1b)
  4855. + },
  4856. +};
  4857. +#endif
  4858. +
  4859. +#if defined(CONFIG_SND_BCM2708_SOC_RPI_DAC) || defined(CONFIG_SND_BCM2708_SOC_RPI_DAC_MODULE)
  4860. +static struct platform_device snd_rpi_dac_device = {
  4861. + .name = "snd-rpi-dac",
  4862. + .id = 0,
  4863. + .num_resources = 0,
  4864. +};
  4865. +
  4866. +static struct platform_device snd_pcm1794a_codec_device = {
  4867. + .name = "pcm1794a-codec",
  4868. + .id = -1,
  4869. + .num_resources = 0,
  4870. +};
  4871. +#endif
  4872. +
  4873. +
  4874. +#if defined(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) || defined(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC_MODULE)
  4875. +static struct platform_device snd_rpi_iqaudio_dac_device = {
  4876. + .name = "snd-rpi-iqaudio-dac",
  4877. + .id = 0,
  4878. + .num_resources = 0,
  4879. +};
  4880. +
  4881. +// Use the actual device name rather than generic driver name
  4882. +static struct i2c_board_info __initdata snd_pcm512x_i2c_devices[] = {
  4883. + {
  4884. + I2C_BOARD_INFO("pcm5122", 0x4c)
  4885. + },
  4886. +};
  4887. +#endif
  4888. +
  4889. +int __init bcm_register_device(struct platform_device *pdev)
  4890. +{
  4891. + int ret;
  4892. +
  4893. + ret = platform_device_register(pdev);
  4894. + if (ret)
  4895. + pr_debug("Unable to register platform device '%s': %d\n",
  4896. + pdev->name, ret);
  4897. +
  4898. + return ret;
  4899. +}
  4900. +
  4901. +int calc_rsts(int partition)
  4902. +{
  4903. + return PM_PASSWORD |
  4904. + ((partition & (1 << 0)) << 0) |
  4905. + ((partition & (1 << 1)) << 1) |
  4906. + ((partition & (1 << 2)) << 2) |
  4907. + ((partition & (1 << 3)) << 3) |
  4908. + ((partition & (1 << 4)) << 4) |
  4909. + ((partition & (1 << 5)) << 5);
  4910. +}
  4911. +
  4912. +static void bcm2708_restart(enum reboot_mode mode, const char *cmd)
  4913. +{
  4914. + extern char bcm2708_reboot_mode;
  4915. + uint32_t pm_rstc, pm_wdog;
  4916. + uint32_t timeout = 10;
  4917. + uint32_t pm_rsts = 0;
  4918. +
  4919. + if(bcm2708_reboot_mode == 'q')
  4920. + {
  4921. + // NOOBS < 1.3 booting with reboot=q
  4922. + pm_rsts = readl(__io_address(PM_RSTS));
  4923. + pm_rsts = PM_PASSWORD | pm_rsts | PM_RSTS_HADWRQ_SET;
  4924. + }
  4925. + else if(bcm2708_reboot_mode == 'p')
  4926. + {
  4927. + // NOOBS < 1.3 halting
  4928. + pm_rsts = readl(__io_address(PM_RSTS));
  4929. + pm_rsts = PM_PASSWORD | pm_rsts | PM_RSTS_HADWRH_SET;
  4930. + }
  4931. + else
  4932. + {
  4933. + pm_rsts = calc_rsts(reboot_part);
  4934. + }
  4935. +
  4936. + writel(pm_rsts, __io_address(PM_RSTS));
  4937. +
  4938. + /* Setup watchdog for reset */
  4939. + pm_rstc = readl(__io_address(PM_RSTC));
  4940. +
  4941. + pm_wdog = PM_PASSWORD | (timeout & PM_WDOG_TIME_SET); // watchdog timer = timer clock / 16; need password (31:16) + value (11:0)
  4942. + pm_rstc = PM_PASSWORD | (pm_rstc & PM_RSTC_WRCFG_CLR) | PM_RSTC_WRCFG_FULL_RESET;
  4943. +
  4944. + writel(pm_wdog, __io_address(PM_WDOG));
  4945. + writel(pm_rstc, __io_address(PM_RSTC));
  4946. +}
  4947. +
  4948. +/* We can't really power off, but if we do the normal reset scheme, and indicate to bootcode.bin not to reboot, then most of the chip will be powered off */
  4949. +static void bcm2708_power_off(void)
  4950. +{
  4951. + extern char bcm2708_reboot_mode;
  4952. + if(bcm2708_reboot_mode == 'q')
  4953. + {
  4954. + // NOOBS < v1.3
  4955. + bcm2708_restart('p', "");
  4956. + }
  4957. + else
  4958. + {
  4959. + /* partition 63 is special code for HALT the bootloader knows not to boot*/
  4960. + reboot_part = 63;
  4961. + /* continue with normal reset mechanism */
  4962. + bcm2708_restart(0, "");
  4963. + }
  4964. +}
  4965. +
  4966. +void __init bcm2708_init(void)
  4967. +{
  4968. + int i;
  4969. +
  4970. +#if defined(CONFIG_BCM_VC_CMA)
  4971. + vc_cma_early_init();
  4972. +#endif
  4973. + printk("bcm2708.uart_clock = %d\n", uart_clock);
  4974. + pm_power_off = bcm2708_power_off;
  4975. +
  4976. + if (uart_clock)
  4977. + lookups[0].clk->rate = uart_clock;
  4978. +
  4979. + for (i = 0; i < ARRAY_SIZE(lookups); i++)
  4980. + clkdev_add(&lookups[i]);
  4981. +
  4982. + bcm_register_device(&bcm2708_dmaman_device);
  4983. + bcm_register_device(&bcm2708_vcio_device);
  4984. +#ifdef CONFIG_BCM2708_GPIO
  4985. + bcm_register_device(&bcm2708_gpio_device);
  4986. + if (pps_gpio_pin >= 0) {
  4987. + pr_info("bcm2708: GPIO %d setup as pps-gpio device\n", pps_gpio_pin);
  4988. + pps_gpio_info.gpio_pin = pps_gpio_pin;
  4989. + pps_gpio_device.id = pps_gpio_pin;
  4990. + bcm_register_device(&pps_gpio_device);
  4991. + }
  4992. +#endif
  4993. +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
  4994. + w1_gpio_pdata.pin = w1_gpio_pin;
  4995. + w1_gpio_pdata.ext_pullup_enable_pin = w1_gpio_pullup;
  4996. + platform_device_register(&w1_device);
  4997. +#endif
  4998. + bcm_register_device(&bcm2708_systemtimer_device);
  4999. + bcm_register_device(&bcm2708_fb_device);
  5000. + bcm_register_device(&bcm2708_usb_device);
  5001. + bcm_register_device(&bcm2708_uart1_device);
  5002. + bcm_register_device(&bcm2708_powerman_device);
  5003. +
  5004. +#ifdef CONFIG_MMC_SDHCI_BCM2708
  5005. + if (!bcm2835_mmc)
  5006. + bcm_register_device(&bcm2708_emmc_device);
  5007. +#endif
  5008. +#ifdef CONFIG_MMC_BCM2835
  5009. + if (bcm2835_mmc)
  5010. + bcm_register_device(&bcm2835_emmc_device);
  5011. +#endif
  5012. + bcm2708_init_led();
  5013. + for (i = 0; i < ARRAY_SIZE(bcm2708_alsa_devices); i++)
  5014. + bcm_register_device(&bcm2708_alsa_devices[i]);
  5015. +
  5016. + bcm_register_device(&bcm2708_spi_device);
  5017. +
  5018. + if (vc_i2c_override) {
  5019. + bcm_register_device(&bcm2708_bsc0_device);
  5020. + bcm_register_device(&bcm2708_bsc1_device);
  5021. + } else if ((boardrev & 0xffffff) == 0x2 || (boardrev & 0xffffff) == 0x3) {
  5022. + bcm_register_device(&bcm2708_bsc0_device);
  5023. + } else {
  5024. + bcm_register_device(&bcm2708_bsc1_device);
  5025. + }
  5026. +
  5027. + bcm_register_device(&bcm2835_hwmon_device);
  5028. + bcm_register_device(&bcm2835_thermal_device);
  5029. +
  5030. +#ifdef CONFIG_SND_BCM2708_SOC_I2S_MODULE
  5031. + bcm_register_device(&bcm2708_i2s_device);
  5032. +#endif
  5033. +
  5034. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC_MODULE)
  5035. + bcm_register_device(&snd_hifiberry_dac_device);
  5036. + bcm_register_device(&snd_pcm5102a_codec_device);
  5037. +#endif
  5038. +
  5039. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS_MODULE)
  5040. + bcm_register_device(&snd_rpi_hifiberry_dacplus_device);
  5041. + i2c_register_board_info(1, snd_pcm512x_hbdacplus_i2c_devices, ARRAY_SIZE(snd_pcm512x_hbdacplus_i2c_devices));
  5042. +#endif
  5043. +
  5044. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI_MODULE)
  5045. + bcm_register_device(&snd_hifiberry_digi_device);
  5046. + i2c_register_board_info(1, snd_wm8804_i2c_devices, ARRAY_SIZE(snd_wm8804_i2c_devices));
  5047. +#endif
  5048. +
  5049. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP_MODULE)
  5050. + bcm_register_device(&snd_hifiberry_amp_device);
  5051. + i2c_register_board_info(1, snd_tas5713_i2c_devices, ARRAY_SIZE(snd_tas5713_i2c_devices));
  5052. +#endif
  5053. +
  5054. +
  5055. +#if defined(CONFIG_SND_BCM2708_SOC_RPI_DAC) || defined(CONFIG_SND_BCM2708_SOC_RPI_DAC_MODULE)
  5056. + bcm_register_device(&snd_rpi_dac_device);
  5057. + bcm_register_device(&snd_pcm1794a_codec_device);
  5058. +#endif
  5059. +
  5060. +#if defined(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) || defined(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC_MODULE)
  5061. + bcm_register_device(&snd_rpi_iqaudio_dac_device);
  5062. + i2c_register_board_info(1, snd_pcm512x_i2c_devices, ARRAY_SIZE(snd_pcm512x_i2c_devices));
  5063. +#endif
  5064. +
  5065. +
  5066. + for (i = 0; i < ARRAY_SIZE(amba_devs); i++) {
  5067. + struct amba_device *d = amba_devs[i];
  5068. + amba_device_register(d, &iomem_resource);
  5069. + }
  5070. + system_rev = boardrev;
  5071. + system_serial_low = serial;
  5072. +
  5073. +#ifdef CONFIG_BCM2708_SPIDEV
  5074. + spi_register_board_info(bcm2708_spi_devices,
  5075. + ARRAY_SIZE(bcm2708_spi_devices));
  5076. +#endif
  5077. +}
  5078. +
  5079. +static void timer_set_mode(enum clock_event_mode mode,
  5080. + struct clock_event_device *clk)
  5081. +{
  5082. + switch (mode) {
  5083. + case CLOCK_EVT_MODE_ONESHOT: /* Leave the timer disabled, .set_next_event will enable it */
  5084. + case CLOCK_EVT_MODE_SHUTDOWN:
  5085. + break;
  5086. + case CLOCK_EVT_MODE_PERIODIC:
  5087. +
  5088. + case CLOCK_EVT_MODE_UNUSED:
  5089. + case CLOCK_EVT_MODE_RESUME:
  5090. +
  5091. + default:
  5092. + printk(KERN_ERR "timer_set_mode: unhandled mode:%d\n",
  5093. + (int)mode);
  5094. + break;
  5095. + }
  5096. +
  5097. +}
  5098. +
  5099. +static int timer_set_next_event(unsigned long cycles,
  5100. + struct clock_event_device *unused)
  5101. +{
  5102. + unsigned long stc;
  5103. + do {
  5104. + stc = readl(__io_address(ST_BASE + 0x04));
  5105. + /* We could take a FIQ here, which may push ST above STC3 */
  5106. + writel(stc + cycles, __io_address(ST_BASE + 0x18));
  5107. + } while ((signed long) cycles >= 0 &&
  5108. + (signed long) (readl(__io_address(ST_BASE + 0x04)) - stc)
  5109. + >= (signed long) cycles);
  5110. + return 0;
  5111. +}
  5112. +
  5113. +static struct clock_event_device timer0_clockevent = {
  5114. + .name = "timer0",
  5115. + .shift = 32,
  5116. + .features = CLOCK_EVT_FEAT_ONESHOT,
  5117. + .set_mode = timer_set_mode,
  5118. + .set_next_event = timer_set_next_event,
  5119. +};
  5120. +
  5121. +/*
  5122. + * IRQ handler for the timer
  5123. + */
  5124. +static irqreturn_t bcm2708_timer_interrupt(int irq, void *dev_id)
  5125. +{
  5126. + struct clock_event_device *evt = &timer0_clockevent;
  5127. +
  5128. + writel(1 << 3, __io_address(ST_BASE + 0x00)); /* stcs clear timer int */
  5129. +
  5130. + evt->event_handler(evt);
  5131. +
  5132. + return IRQ_HANDLED;
  5133. +}
  5134. +
  5135. +static struct irqaction bcm2708_timer_irq = {
  5136. + .name = "BCM2708 Timer Tick",
  5137. + .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  5138. + .handler = bcm2708_timer_interrupt,
  5139. +};
  5140. +
  5141. +/*
  5142. + * Set up timer interrupt, and return the current time in seconds.
  5143. + */
  5144. +
  5145. +static struct delay_timer bcm2708_delay_timer = {
  5146. + .read_current_timer = bcm2708_read_current_timer,
  5147. + .freq = STC_FREQ_HZ,
  5148. +};
  5149. +
  5150. +static void __init bcm2708_timer_init(void)
  5151. +{
  5152. + /* init high res timer */
  5153. + bcm2708_clocksource_init();
  5154. +
  5155. + /*
  5156. + * Initialise to a known state (all timers off)
  5157. + */
  5158. + writel(0, __io_address(ARM_T_CONTROL));
  5159. + /*
  5160. + * Make irqs happen for the system timer
  5161. + */
  5162. + setup_irq(IRQ_TIMER3, &bcm2708_timer_irq);
  5163. +
  5164. + setup_sched_clock(bcm2708_read_sched_clock, 32, STC_FREQ_HZ);
  5165. +
  5166. + timer0_clockevent.mult =
  5167. + div_sc(STC_FREQ_HZ, NSEC_PER_SEC, timer0_clockevent.shift);
  5168. + timer0_clockevent.max_delta_ns =
  5169. + clockevent_delta2ns(0xffffffff, &timer0_clockevent);
  5170. + timer0_clockevent.min_delta_ns =
  5171. + clockevent_delta2ns(0xf, &timer0_clockevent);
  5172. +
  5173. + timer0_clockevent.cpumask = cpumask_of(0);
  5174. + clockevents_register_device(&timer0_clockevent);
  5175. +
  5176. + register_current_timer_delay(&bcm2708_delay_timer);
  5177. +}
  5178. +
  5179. +#if defined(CONFIG_LEDS_GPIO) || defined(CONFIG_LEDS_GPIO_MODULE)
  5180. +#include <linux/leds.h>
  5181. +
  5182. +static struct gpio_led bcm2708_leds[] = {
  5183. + [0] = {
  5184. + .gpio = 16,
  5185. + .name = "led0",
  5186. + .default_trigger = "mmc0",
  5187. + .active_low = 1,
  5188. + },
  5189. +};
  5190. +
  5191. +static struct gpio_led_platform_data bcm2708_led_pdata = {
  5192. + .num_leds = ARRAY_SIZE(bcm2708_leds),
  5193. + .leds = bcm2708_leds,
  5194. +};
  5195. +
  5196. +static struct platform_device bcm2708_led_device = {
  5197. + .name = "leds-gpio",
  5198. + .id = -1,
  5199. + .dev = {
  5200. + .platform_data = &bcm2708_led_pdata,
  5201. + },
  5202. +};
  5203. +
  5204. +static void __init bcm2708_init_led(void)
  5205. +{
  5206. + bcm2708_leds[0].gpio = disk_led_gpio;
  5207. + bcm2708_leds[0].active_low = disk_led_active_low;
  5208. + platform_device_register(&bcm2708_led_device);
  5209. +}
  5210. +#else
  5211. +static inline void bcm2708_init_led(void)
  5212. +{
  5213. +}
  5214. +#endif
  5215. +
  5216. +void __init bcm2708_init_early(void)
  5217. +{
  5218. + /*
  5219. + * Some devices allocate their coherent buffers from atomic
  5220. + * context. Increase size of atomic coherent pool to make sure such
  5221. + * the allocations won't fail.
  5222. + */
  5223. + init_dma_coherent_pool_size(SZ_4M);
  5224. +}
  5225. +
  5226. +static void __init board_reserve(void)
  5227. +{
  5228. +#if defined(CONFIG_BCM_VC_CMA)
  5229. + vc_cma_reserve();
  5230. +#endif
  5231. +}
  5232. +
  5233. +MACHINE_START(BCM2708, "BCM2708")
  5234. + /* Maintainer: Broadcom Europe Ltd. */
  5235. + .map_io = bcm2708_map_io,
  5236. + .init_irq = bcm2708_init_irq,
  5237. + .init_time = bcm2708_timer_init,
  5238. + .init_machine = bcm2708_init,
  5239. + .init_early = bcm2708_init_early,
  5240. + .reserve = board_reserve,
  5241. + .restart = bcm2708_restart,
  5242. +MACHINE_END
  5243. +
  5244. +module_param(boardrev, uint, 0644);
  5245. +module_param(serial, uint, 0644);
  5246. +module_param(uart_clock, uint, 0644);
  5247. +module_param(disk_led_gpio, uint, 0644);
  5248. +module_param(disk_led_active_low, uint, 0644);
  5249. +module_param(reboot_part, uint, 0644);
  5250. +module_param(w1_gpio_pin, uint, 0644);
  5251. +module_param(w1_gpio_pullup, uint, 0644);
  5252. +module_param(pps_gpio_pin, int, 0644);
  5253. +MODULE_PARM_DESC(pps_gpio_pin, "Set GPIO pin to reserve for PPS");
  5254. +module_param(bcm2835_mmc, uint, 0644);
  5255. +module_param(vc_i2c_override, bool, 0644);
  5256. +MODULE_PARM_DESC(vc_i2c_override, "Allow the use of VC's I2C peripheral.");
  5257. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/bcm2708_gpio.c linux-3.12.33-rpi/arch/arm/mach-bcm2708/bcm2708_gpio.c
  5258. --- linux-3.12.33/arch/arm/mach-bcm2708/bcm2708_gpio.c 1969-12-31 18:00:00.000000000 -0600
  5259. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/bcm2708_gpio.c 2014-12-03 19:13:32.448418001 -0600
  5260. @@ -0,0 +1,401 @@
  5261. +/*
  5262. + * linux/arch/arm/mach-bcm2708/bcm2708_gpio.c
  5263. + *
  5264. + * Copyright (C) 2010 Broadcom
  5265. + *
  5266. + * This program is free software; you can redistribute it and/or modify
  5267. + * it under the terms of the GNU General Public License version 2 as
  5268. + * published by the Free Software Foundation.
  5269. + *
  5270. + */
  5271. +
  5272. +#include <linux/spinlock.h>
  5273. +#include <linux/module.h>
  5274. +#include <linux/delay.h>
  5275. +#include <linux/list.h>
  5276. +#include <linux/io.h>
  5277. +#include <linux/irq.h>
  5278. +#include <linux/interrupt.h>
  5279. +#include <linux/slab.h>
  5280. +#include <mach/gpio.h>
  5281. +#include <linux/gpio.h>
  5282. +#include <linux/platform_device.h>
  5283. +#include <mach/platform.h>
  5284. +
  5285. +#include <linux/platform_data/bcm2708.h>
  5286. +
  5287. +#define BCM_GPIO_DRIVER_NAME "bcm2708_gpio"
  5288. +#define DRIVER_NAME BCM_GPIO_DRIVER_NAME
  5289. +#define BCM_GPIO_USE_IRQ 1
  5290. +
  5291. +#define GPIOFSEL(x) (0x00+(x)*4)
  5292. +#define GPIOSET(x) (0x1c+(x)*4)
  5293. +#define GPIOCLR(x) (0x28+(x)*4)
  5294. +#define GPIOLEV(x) (0x34+(x)*4)
  5295. +#define GPIOEDS(x) (0x40+(x)*4)
  5296. +#define GPIOREN(x) (0x4c+(x)*4)
  5297. +#define GPIOFEN(x) (0x58+(x)*4)
  5298. +#define GPIOHEN(x) (0x64+(x)*4)
  5299. +#define GPIOLEN(x) (0x70+(x)*4)
  5300. +#define GPIOAREN(x) (0x7c+(x)*4)
  5301. +#define GPIOAFEN(x) (0x88+(x)*4)
  5302. +#define GPIOUD(x) (0x94+(x)*4)
  5303. +#define GPIOUDCLK(x) (0x98+(x)*4)
  5304. +
  5305. +enum { GPIO_FSEL_INPUT, GPIO_FSEL_OUTPUT,
  5306. + GPIO_FSEL_ALT5, GPIO_FSEL_ALT_4,
  5307. + GPIO_FSEL_ALT0, GPIO_FSEL_ALT1,
  5308. + GPIO_FSEL_ALT2, GPIO_FSEL_ALT3,
  5309. +};
  5310. +
  5311. + /* Each of the two spinlocks protects a different set of hardware
  5312. + * regiters and data structurs. This decouples the code of the IRQ from
  5313. + * the GPIO code. This also makes the case of a GPIO routine call from
  5314. + * the IRQ code simpler.
  5315. + */
  5316. +static DEFINE_SPINLOCK(lock); /* GPIO registers */
  5317. +
  5318. +struct bcm2708_gpio {
  5319. + struct list_head list;
  5320. + void __iomem *base;
  5321. + struct gpio_chip gc;
  5322. + unsigned long rising[(BCM2708_NR_GPIOS + 31) / 32];
  5323. + unsigned long falling[(BCM2708_NR_GPIOS + 31) / 32];
  5324. + unsigned long high[(BCM2708_NR_GPIOS + 31) / 32];
  5325. + unsigned long low[(BCM2708_NR_GPIOS + 31) / 32];
  5326. +};
  5327. +
  5328. +static int bcm2708_set_function(struct gpio_chip *gc, unsigned offset,
  5329. + int function)
  5330. +{
  5331. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  5332. + unsigned long flags;
  5333. + unsigned gpiodir;
  5334. + unsigned gpio_bank = offset / 10;
  5335. + unsigned gpio_field_offset = (offset - 10 * gpio_bank) * 3;
  5336. +
  5337. +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_set_function %p (%d,%d)\n", gc, offset, function);
  5338. + if (offset >= BCM2708_NR_GPIOS)
  5339. + return -EINVAL;
  5340. +
  5341. + spin_lock_irqsave(&lock, flags);
  5342. +
  5343. + gpiodir = readl(gpio->base + GPIOFSEL(gpio_bank));
  5344. + gpiodir &= ~(7 << gpio_field_offset);
  5345. + gpiodir |= function << gpio_field_offset;
  5346. + writel(gpiodir, gpio->base + GPIOFSEL(gpio_bank));
  5347. + spin_unlock_irqrestore(&lock, flags);
  5348. + gpiodir = readl(gpio->base + GPIOFSEL(gpio_bank));
  5349. +
  5350. + return 0;
  5351. +}
  5352. +
  5353. +static int bcm2708_gpio_dir_in(struct gpio_chip *gc, unsigned offset)
  5354. +{
  5355. + return bcm2708_set_function(gc, offset, GPIO_FSEL_INPUT);
  5356. +}
  5357. +
  5358. +static void bcm2708_gpio_set(struct gpio_chip *gc, unsigned offset, int value);
  5359. +static int bcm2708_gpio_dir_out(struct gpio_chip *gc, unsigned offset,
  5360. + int value)
  5361. +{
  5362. + int ret;
  5363. + ret = bcm2708_set_function(gc, offset, GPIO_FSEL_OUTPUT);
  5364. + if (ret >= 0)
  5365. + bcm2708_gpio_set(gc, offset, value);
  5366. + return ret;
  5367. +}
  5368. +
  5369. +static int bcm2708_gpio_get(struct gpio_chip *gc, unsigned offset)
  5370. +{
  5371. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  5372. + unsigned gpio_bank = offset / 32;
  5373. + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
  5374. + unsigned lev;
  5375. +
  5376. + if (offset >= BCM2708_NR_GPIOS)
  5377. + return 0;
  5378. + lev = readl(gpio->base + GPIOLEV(gpio_bank));
  5379. +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_get %p (%d)=%d\n", gc, offset, 0x1 & (lev>>gpio_field_offset));
  5380. + return 0x1 & (lev >> gpio_field_offset);
  5381. +}
  5382. +
  5383. +static void bcm2708_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  5384. +{
  5385. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  5386. + unsigned gpio_bank = offset / 32;
  5387. + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
  5388. +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_set %p (%d=%d)\n", gc, offset, value);
  5389. + if (offset >= BCM2708_NR_GPIOS)
  5390. + return;
  5391. + if (value)
  5392. + writel(1 << gpio_field_offset, gpio->base + GPIOSET(gpio_bank));
  5393. + else
  5394. + writel(1 << gpio_field_offset, gpio->base + GPIOCLR(gpio_bank));
  5395. +}
  5396. +
  5397. +/**********************
  5398. + * extension to configure pullups
  5399. + */
  5400. +int bcm2708_gpio_setpull(struct gpio_chip *gc, unsigned offset,
  5401. + bcm2708_gpio_pull_t value)
  5402. +{
  5403. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  5404. + unsigned gpio_bank = offset / 32;
  5405. + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
  5406. +
  5407. + if (offset >= BCM2708_NR_GPIOS)
  5408. + return -EINVAL;
  5409. +
  5410. + switch (value) {
  5411. + case BCM2708_PULL_UP:
  5412. + writel(2, gpio->base + GPIOUD(0));
  5413. + break;
  5414. + case BCM2708_PULL_DOWN:
  5415. + writel(1, gpio->base + GPIOUD(0));
  5416. + break;
  5417. + case BCM2708_PULL_OFF:
  5418. + writel(0, gpio->base + GPIOUD(0));
  5419. + break;
  5420. + }
  5421. +
  5422. + udelay(5);
  5423. + writel(1 << gpio_field_offset, gpio->base + GPIOUDCLK(gpio_bank));
  5424. + udelay(5);
  5425. + writel(0, gpio->base + GPIOUD(0));
  5426. + writel(0 << gpio_field_offset, gpio->base + GPIOUDCLK(gpio_bank));
  5427. +
  5428. + return 0;
  5429. +}
  5430. +EXPORT_SYMBOL(bcm2708_gpio_setpull);
  5431. +
  5432. +/*************************************************************************************************************************
  5433. + * bcm2708 GPIO IRQ
  5434. + */
  5435. +
  5436. +#if BCM_GPIO_USE_IRQ
  5437. +
  5438. +static int bcm2708_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
  5439. +{
  5440. + return gpio_to_irq(gpio);
  5441. +}
  5442. +
  5443. +static int bcm2708_gpio_irq_set_type(struct irq_data *d, unsigned type)
  5444. +{
  5445. + unsigned irq = d->irq;
  5446. + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
  5447. + unsigned gn = irq_to_gpio(irq);
  5448. + unsigned gb = gn / 32;
  5449. + unsigned go = gn % 32;
  5450. +
  5451. + gpio->rising[gb] &= ~(1 << go);
  5452. + gpio->falling[gb] &= ~(1 << go);
  5453. + gpio->high[gb] &= ~(1 << go);
  5454. + gpio->low[gb] &= ~(1 << go);
  5455. +
  5456. + if (type & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  5457. + return -EINVAL;
  5458. +
  5459. + if (type & IRQ_TYPE_EDGE_RISING)
  5460. + gpio->rising[gb] |= (1 << go);
  5461. + if (type & IRQ_TYPE_EDGE_FALLING)
  5462. + gpio->falling[gb] |= (1 << go);
  5463. + if (type & IRQ_TYPE_LEVEL_HIGH)
  5464. + gpio->high[gb] |= (1 << go);
  5465. + if (type & IRQ_TYPE_LEVEL_LOW)
  5466. + gpio->low[gb] |= (1 << go);
  5467. + return 0;
  5468. +}
  5469. +
  5470. +static void bcm2708_gpio_irq_mask(struct irq_data *d)
  5471. +{
  5472. + unsigned irq = d->irq;
  5473. + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
  5474. + unsigned gn = irq_to_gpio(irq);
  5475. + unsigned gb = gn / 32;
  5476. + unsigned long rising = readl(gpio->base + GPIOREN(gb));
  5477. + unsigned long falling = readl(gpio->base + GPIOFEN(gb));
  5478. + unsigned long high = readl(gpio->base + GPIOHEN(gb));
  5479. + unsigned long low = readl(gpio->base + GPIOLEN(gb));
  5480. +
  5481. + gn = gn % 32;
  5482. +
  5483. + writel(rising & ~(1 << gn), gpio->base + GPIOREN(gb));
  5484. + writel(falling & ~(1 << gn), gpio->base + GPIOFEN(gb));
  5485. + writel(high & ~(1 << gn), gpio->base + GPIOHEN(gb));
  5486. + writel(low & ~(1 << gn), gpio->base + GPIOLEN(gb));
  5487. +}
  5488. +
  5489. +static void bcm2708_gpio_irq_unmask(struct irq_data *d)
  5490. +{
  5491. + unsigned irq = d->irq;
  5492. + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
  5493. + unsigned gn = irq_to_gpio(irq);
  5494. + unsigned gb = gn / 32;
  5495. + unsigned go = gn % 32;
  5496. + unsigned long rising = readl(gpio->base + GPIOREN(gb));
  5497. + unsigned long falling = readl(gpio->base + GPIOFEN(gb));
  5498. + unsigned long high = readl(gpio->base + GPIOHEN(gb));
  5499. + unsigned long low = readl(gpio->base + GPIOLEN(gb));
  5500. +
  5501. + writel(1 << go, gpio->base + GPIOEDS(gb));
  5502. +
  5503. + if (gpio->rising[gb] & (1 << go)) {
  5504. + writel(rising | (1 << go), gpio->base + GPIOREN(gb));
  5505. + } else {
  5506. + writel(rising & ~(1 << go), gpio->base + GPIOREN(gb));
  5507. + }
  5508. +
  5509. + if (gpio->falling[gb] & (1 << go)) {
  5510. + writel(falling | (1 << go), gpio->base + GPIOFEN(gb));
  5511. + } else {
  5512. + writel(falling & ~(1 << go), gpio->base + GPIOFEN(gb));
  5513. + }
  5514. +
  5515. + if (gpio->high[gb] & (1 << go)) {
  5516. + writel(high | (1 << go), gpio->base + GPIOHEN(gb));
  5517. + } else {
  5518. + writel(high & ~(1 << go), gpio->base + GPIOHEN(gb));
  5519. + }
  5520. +
  5521. + if (gpio->low[gb] & (1 << go)) {
  5522. + writel(low | (1 << go), gpio->base + GPIOLEN(gb));
  5523. + } else {
  5524. + writel(low & ~(1 << go), gpio->base + GPIOLEN(gb));
  5525. + }
  5526. +}
  5527. +
  5528. +static struct irq_chip bcm2708_irqchip = {
  5529. + .name = "GPIO",
  5530. + .irq_enable = bcm2708_gpio_irq_unmask,
  5531. + .irq_disable = bcm2708_gpio_irq_mask,
  5532. + .irq_unmask = bcm2708_gpio_irq_unmask,
  5533. + .irq_mask = bcm2708_gpio_irq_mask,
  5534. + .irq_set_type = bcm2708_gpio_irq_set_type,
  5535. +};
  5536. +
  5537. +static irqreturn_t bcm2708_gpio_interrupt(int irq, void *dev_id)
  5538. +{
  5539. + unsigned long edsr;
  5540. + unsigned bank;
  5541. + int i;
  5542. + unsigned gpio;
  5543. + for (bank = 0; bank <= 1; bank++) {
  5544. + edsr = readl(__io_address(GPIO_BASE) + GPIOEDS(bank));
  5545. + for_each_set_bit(i, &edsr, 32) {
  5546. + gpio = i + bank * 32;
  5547. + generic_handle_irq(gpio_to_irq(gpio));
  5548. + }
  5549. + writel(0xffffffff, __io_address(GPIO_BASE) + GPIOEDS(bank));
  5550. + }
  5551. + return IRQ_HANDLED;
  5552. +}
  5553. +
  5554. +static struct irqaction bcm2708_gpio_irq = {
  5555. + .name = "BCM2708 GPIO catchall handler",
  5556. + .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  5557. + .handler = bcm2708_gpio_interrupt,
  5558. +};
  5559. +
  5560. +static void bcm2708_gpio_irq_init(struct bcm2708_gpio *ucb)
  5561. +{
  5562. + unsigned irq;
  5563. +
  5564. + ucb->gc.to_irq = bcm2708_gpio_to_irq;
  5565. +
  5566. + for (irq = GPIO_IRQ_START; irq < (GPIO_IRQ_START + GPIO_IRQS); irq++) {
  5567. + irq_set_chip_data(irq, ucb);
  5568. + irq_set_chip(irq, &bcm2708_irqchip);
  5569. + set_irq_flags(irq, IRQF_VALID);
  5570. + }
  5571. + setup_irq(IRQ_GPIO3, &bcm2708_gpio_irq);
  5572. +}
  5573. +
  5574. +#else
  5575. +
  5576. +static void bcm2708_gpio_irq_init(struct bcm2708_gpio *ucb)
  5577. +{
  5578. +}
  5579. +
  5580. +#endif /* #if BCM_GPIO_USE_IRQ ***************************************************************************************************************** */
  5581. +
  5582. +static int bcm2708_gpio_probe(struct platform_device *dev)
  5583. +{
  5584. + struct bcm2708_gpio *ucb;
  5585. + struct resource *res;
  5586. + int err = 0;
  5587. +
  5588. + printk(KERN_INFO DRIVER_NAME ": bcm2708_gpio_probe %p\n", dev);
  5589. +
  5590. + ucb = kzalloc(sizeof(*ucb), GFP_KERNEL);
  5591. + if (NULL == ucb) {
  5592. + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
  5593. + "mailbox memory\n");
  5594. + err = -ENOMEM;
  5595. + goto err;
  5596. + }
  5597. +
  5598. + res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  5599. +
  5600. + platform_set_drvdata(dev, ucb);
  5601. + ucb->base = __io_address(GPIO_BASE);
  5602. +
  5603. + ucb->gc.label = "bcm2708_gpio";
  5604. + ucb->gc.base = 0;
  5605. + ucb->gc.ngpio = BCM2708_NR_GPIOS;
  5606. + ucb->gc.owner = THIS_MODULE;
  5607. +
  5608. + ucb->gc.direction_input = bcm2708_gpio_dir_in;
  5609. + ucb->gc.direction_output = bcm2708_gpio_dir_out;
  5610. + ucb->gc.get = bcm2708_gpio_get;
  5611. + ucb->gc.set = bcm2708_gpio_set;
  5612. + ucb->gc.can_sleep = 0;
  5613. +
  5614. + bcm2708_gpio_irq_init(ucb);
  5615. +
  5616. + err = gpiochip_add(&ucb->gc);
  5617. + if (err)
  5618. + goto err;
  5619. +
  5620. +err:
  5621. + return err;
  5622. +
  5623. +}
  5624. +
  5625. +static int bcm2708_gpio_remove(struct platform_device *dev)
  5626. +{
  5627. + int err = 0;
  5628. + struct bcm2708_gpio *ucb = platform_get_drvdata(dev);
  5629. +
  5630. + printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_remove %p\n", dev);
  5631. +
  5632. + err = gpiochip_remove(&ucb->gc);
  5633. +
  5634. + platform_set_drvdata(dev, NULL);
  5635. + kfree(ucb);
  5636. +
  5637. + return err;
  5638. +}
  5639. +
  5640. +static struct platform_driver bcm2708_gpio_driver = {
  5641. + .probe = bcm2708_gpio_probe,
  5642. + .remove = bcm2708_gpio_remove,
  5643. + .driver = {
  5644. + .name = "bcm2708_gpio"},
  5645. +};
  5646. +
  5647. +static int __init bcm2708_gpio_init(void)
  5648. +{
  5649. + return platform_driver_register(&bcm2708_gpio_driver);
  5650. +}
  5651. +
  5652. +static void __exit bcm2708_gpio_exit(void)
  5653. +{
  5654. + platform_driver_unregister(&bcm2708_gpio_driver);
  5655. +}
  5656. +
  5657. +module_init(bcm2708_gpio_init);
  5658. +module_exit(bcm2708_gpio_exit);
  5659. +
  5660. +MODULE_DESCRIPTION("Broadcom BCM2708 GPIO driver");
  5661. +MODULE_LICENSE("GPL");
  5662. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/bcm2708.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/bcm2708.h
  5663. --- linux-3.12.33/arch/arm/mach-bcm2708/bcm2708.h 1969-12-31 18:00:00.000000000 -0600
  5664. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/bcm2708.h 2014-12-03 19:13:32.448418001 -0600
  5665. @@ -0,0 +1,49 @@
  5666. +/*
  5667. + * linux/arch/arm/mach-bcm2708/bcm2708.h
  5668. + *
  5669. + * BCM2708 machine support header
  5670. + *
  5671. + * Copyright (C) 2010 Broadcom
  5672. + *
  5673. + * This program is free software; you can redistribute it and/or modify
  5674. + * it under the terms of the GNU General Public License as published by
  5675. + * the Free Software Foundation; either version 2 of the License, or
  5676. + * (at your option) any later version.
  5677. + *
  5678. + * This program is distributed in the hope that it will be useful,
  5679. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5680. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5681. + * GNU General Public License for more details.
  5682. + *
  5683. + * You should have received a copy of the GNU General Public License
  5684. + * along with this program; if not, write to the Free Software
  5685. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5686. + */
  5687. +
  5688. +#ifndef __BCM2708_BCM2708_H
  5689. +#define __BCM2708_BCM2708_H
  5690. +
  5691. +#include <linux/amba/bus.h>
  5692. +
  5693. +extern void __init bcm2708_init(void);
  5694. +extern void __init bcm2708_init_irq(void);
  5695. +extern void __init bcm2708_map_io(void);
  5696. +extern struct sys_timer bcm2708_timer;
  5697. +extern unsigned int mmc_status(struct device *dev);
  5698. +
  5699. +#define AMBA_DEVICE(name, busid, base, plat) \
  5700. +static struct amba_device name##_device = { \
  5701. + .dev = { \
  5702. + .coherent_dma_mask = ~0, \
  5703. + .init_name = busid, \
  5704. + .platform_data = plat, \
  5705. + }, \
  5706. + .res = { \
  5707. + .start = base##_BASE, \
  5708. + .end = (base##_BASE) + SZ_4K - 1,\
  5709. + .flags = IORESOURCE_MEM, \
  5710. + }, \
  5711. + .irq = base##_IRQ, \
  5712. +}
  5713. +
  5714. +#endif
  5715. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/clock.c linux-3.12.33-rpi/arch/arm/mach-bcm2708/clock.c
  5716. --- linux-3.12.33/arch/arm/mach-bcm2708/clock.c 1969-12-31 18:00:00.000000000 -0600
  5717. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/clock.c 2014-12-03 19:13:32.448418001 -0600
  5718. @@ -0,0 +1,61 @@
  5719. +/*
  5720. + * linux/arch/arm/mach-bcm2708/clock.c
  5721. + *
  5722. + * Copyright (C) 2010 Broadcom
  5723. + *
  5724. + * This program is free software; you can redistribute it and/or modify
  5725. + * it under the terms of the GNU General Public License as published by
  5726. + * the Free Software Foundation; either version 2 of the License, or
  5727. + * (at your option) any later version.
  5728. + *
  5729. + * This program is distributed in the hope that it will be useful,
  5730. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5731. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5732. + * GNU General Public License for more details.
  5733. + *
  5734. + * You should have received a copy of the GNU General Public License
  5735. + * along with this program; if not, write to the Free Software
  5736. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5737. + */
  5738. +#include <linux/module.h>
  5739. +#include <linux/kernel.h>
  5740. +#include <linux/device.h>
  5741. +#include <linux/list.h>
  5742. +#include <linux/errno.h>
  5743. +#include <linux/err.h>
  5744. +#include <linux/string.h>
  5745. +#include <linux/clk.h>
  5746. +#include <linux/mutex.h>
  5747. +
  5748. +#include <asm/clkdev.h>
  5749. +
  5750. +#include "clock.h"
  5751. +
  5752. +int clk_enable(struct clk *clk)
  5753. +{
  5754. + return 0;
  5755. +}
  5756. +EXPORT_SYMBOL(clk_enable);
  5757. +
  5758. +void clk_disable(struct clk *clk)
  5759. +{
  5760. +}
  5761. +EXPORT_SYMBOL(clk_disable);
  5762. +
  5763. +unsigned long clk_get_rate(struct clk *clk)
  5764. +{
  5765. + return clk->rate;
  5766. +}
  5767. +EXPORT_SYMBOL(clk_get_rate);
  5768. +
  5769. +long clk_round_rate(struct clk *clk, unsigned long rate)
  5770. +{
  5771. + return clk->rate;
  5772. +}
  5773. +EXPORT_SYMBOL(clk_round_rate);
  5774. +
  5775. +int clk_set_rate(struct clk *clk, unsigned long rate)
  5776. +{
  5777. + return -EIO;
  5778. +}
  5779. +EXPORT_SYMBOL(clk_set_rate);
  5780. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/clock.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/clock.h
  5781. --- linux-3.12.33/arch/arm/mach-bcm2708/clock.h 1969-12-31 18:00:00.000000000 -0600
  5782. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/clock.h 2014-12-03 19:13:32.448418001 -0600
  5783. @@ -0,0 +1,24 @@
  5784. +/*
  5785. + * linux/arch/arm/mach-bcm2708/clock.h
  5786. + *
  5787. + * Copyright (C) 2010 Broadcom
  5788. + *
  5789. + * This program is free software; you can redistribute it and/or modify
  5790. + * it under the terms of the GNU General Public License as published by
  5791. + * the Free Software Foundation; either version 2 of the License, or
  5792. + * (at your option) any later version.
  5793. + *
  5794. + * This program is distributed in the hope that it will be useful,
  5795. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5796. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5797. + * GNU General Public License for more details.
  5798. + *
  5799. + * You should have received a copy of the GNU General Public License
  5800. + * along with this program; if not, write to the Free Software
  5801. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5802. + */
  5803. +struct module;
  5804. +
  5805. +struct clk {
  5806. + unsigned long rate;
  5807. +};
  5808. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/dma.c linux-3.12.33-rpi/arch/arm/mach-bcm2708/dma.c
  5809. --- linux-3.12.33/arch/arm/mach-bcm2708/dma.c 1969-12-31 18:00:00.000000000 -0600
  5810. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/dma.c 2014-12-03 19:13:32.448418001 -0600
  5811. @@ -0,0 +1,409 @@
  5812. +/*
  5813. + * linux/arch/arm/mach-bcm2708/dma.c
  5814. + *
  5815. + * Copyright (C) 2010 Broadcom
  5816. + *
  5817. + * This program is free software; you can redistribute it and/or modify
  5818. + * it under the terms of the GNU General Public License version 2 as
  5819. + * published by the Free Software Foundation.
  5820. + */
  5821. +
  5822. +#include <linux/slab.h>
  5823. +#include <linux/device.h>
  5824. +#include <linux/platform_device.h>
  5825. +#include <linux/module.h>
  5826. +#include <linux/scatterlist.h>
  5827. +
  5828. +#include <mach/dma.h>
  5829. +#include <mach/irqs.h>
  5830. +
  5831. +/*****************************************************************************\
  5832. + * *
  5833. + * Configuration *
  5834. + * *
  5835. +\*****************************************************************************/
  5836. +
  5837. +#define CACHE_LINE_MASK 31
  5838. +#define DRIVER_NAME BCM_DMAMAN_DRIVER_NAME
  5839. +#define DEFAULT_DMACHAN_BITMAP 0x10 /* channel 4 only */
  5840. +
  5841. +/* valid only for channels 0 - 14, 15 has its own base address */
  5842. +#define BCM2708_DMA_CHAN(n) ((n)<<8) /* base address */
  5843. +#define BCM2708_DMA_CHANIO(dma_base, n) \
  5844. + ((void __iomem *)((char *)(dma_base)+BCM2708_DMA_CHAN(n)))
  5845. +
  5846. +
  5847. +/*****************************************************************************\
  5848. + * *
  5849. + * DMA Auxilliary Functions *
  5850. + * *
  5851. +\*****************************************************************************/
  5852. +
  5853. +/* A DMA buffer on an arbitrary boundary may separate a cache line into a
  5854. + section inside the DMA buffer and another section outside it.
  5855. + Even if we flush DMA buffers from the cache there is always the chance that
  5856. + during a DMA someone will access the part of a cache line that is outside
  5857. + the DMA buffer - which will then bring in unwelcome data.
  5858. + Without being able to dictate our own buffer pools we must insist that
  5859. + DMA buffers consist of a whole number of cache lines.
  5860. +*/
  5861. +
  5862. +extern int
  5863. +bcm_sg_suitable_for_dma(struct scatterlist *sg_ptr, int sg_len)
  5864. +{
  5865. + int i;
  5866. +
  5867. + for (i = 0; i < sg_len; i++) {
  5868. + if (sg_ptr[i].offset & CACHE_LINE_MASK ||
  5869. + sg_ptr[i].length & CACHE_LINE_MASK)
  5870. + return 0;
  5871. + }
  5872. +
  5873. + return 1;
  5874. +}
  5875. +EXPORT_SYMBOL_GPL(bcm_sg_suitable_for_dma);
  5876. +
  5877. +extern void
  5878. +bcm_dma_start(void __iomem *dma_chan_base, dma_addr_t control_block)
  5879. +{
  5880. + dsb(); /* ARM data synchronization (push) operation */
  5881. +
  5882. + writel(control_block, dma_chan_base + BCM2708_DMA_ADDR);
  5883. + writel(BCM2708_DMA_ACTIVE, dma_chan_base + BCM2708_DMA_CS);
  5884. +}
  5885. +
  5886. +extern void bcm_dma_wait_idle(void __iomem *dma_chan_base)
  5887. +{
  5888. + dsb();
  5889. +
  5890. + /* ugly busy wait only option for now */
  5891. + while (readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_ACTIVE)
  5892. + cpu_relax();
  5893. +}
  5894. +
  5895. +EXPORT_SYMBOL_GPL(bcm_dma_start);
  5896. +
  5897. +extern bool bcm_dma_is_busy(void __iomem *dma_chan_base)
  5898. +{
  5899. + dsb();
  5900. +
  5901. + return readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_ACTIVE;
  5902. +}
  5903. +EXPORT_SYMBOL_GPL(bcm_dma_is_busy);
  5904. +
  5905. +/* Complete an ongoing DMA (assuming its results are to be ignored)
  5906. + Does nothing if there is no DMA in progress.
  5907. + This routine waits for the current AXI transfer to complete before
  5908. + terminating the current DMA. If the current transfer is hung on a DREQ used
  5909. + by an uncooperative peripheral the AXI transfer may never complete. In this
  5910. + case the routine times out and return a non-zero error code.
  5911. + Use of this routine doesn't guarantee that the ongoing or aborted DMA
  5912. + does not produce an interrupt.
  5913. +*/
  5914. +extern int
  5915. +bcm_dma_abort(void __iomem *dma_chan_base)
  5916. +{
  5917. + unsigned long int cs;
  5918. + int rc = 0;
  5919. +
  5920. + cs = readl(dma_chan_base + BCM2708_DMA_CS);
  5921. +
  5922. + if (BCM2708_DMA_ACTIVE & cs) {
  5923. + long int timeout = 10000;
  5924. +
  5925. + /* write 0 to the active bit - pause the DMA */
  5926. + writel(0, dma_chan_base + BCM2708_DMA_CS);
  5927. +
  5928. + /* wait for any current AXI transfer to complete */
  5929. + while (0 != (cs & BCM2708_DMA_ISPAUSED) && --timeout >= 0)
  5930. + cs = readl(dma_chan_base + BCM2708_DMA_CS);
  5931. +
  5932. + if (0 != (cs & BCM2708_DMA_ISPAUSED)) {
  5933. + /* we'll un-pause when we set of our next DMA */
  5934. + rc = -ETIMEDOUT;
  5935. +
  5936. + } else if (BCM2708_DMA_ACTIVE & cs) {
  5937. + /* terminate the control block chain */
  5938. + writel(0, dma_chan_base + BCM2708_DMA_NEXTCB);
  5939. +
  5940. + /* abort the whole DMA */
  5941. + writel(BCM2708_DMA_ABORT | BCM2708_DMA_ACTIVE,
  5942. + dma_chan_base + BCM2708_DMA_CS);
  5943. + }
  5944. + }
  5945. +
  5946. + return rc;
  5947. +}
  5948. +EXPORT_SYMBOL_GPL(bcm_dma_abort);
  5949. +
  5950. +
  5951. +/***************************************************************************** \
  5952. + * *
  5953. + * DMA Manager Device Methods *
  5954. + * *
  5955. +\*****************************************************************************/
  5956. +
  5957. +struct vc_dmaman {
  5958. + void __iomem *dma_base;
  5959. + u32 chan_available; /* bitmap of available channels */
  5960. + u32 has_feature[BCM_DMA_FEATURE_COUNT]; /* bitmap of feature presence */
  5961. +};
  5962. +
  5963. +static void vc_dmaman_init(struct vc_dmaman *dmaman, void __iomem *dma_base,
  5964. + u32 chans_available)
  5965. +{
  5966. + dmaman->dma_base = dma_base;
  5967. + dmaman->chan_available = chans_available;
  5968. + dmaman->has_feature[BCM_DMA_FEATURE_FAST_ORD] = 0x0c; /* chans 2 & 3 */
  5969. + dmaman->has_feature[BCM_DMA_FEATURE_BULK_ORD] = 0x01; /* chan 0 */
  5970. + dmaman->has_feature[BCM_DMA_FEATURE_NORMAL_ORD] = 0xfe; /* chans 1 to 7 */
  5971. + dmaman->has_feature[BCM_DMA_FEATURE_LITE_ORD] = 0x7f00; /* chans 8 to 14 */
  5972. +}
  5973. +
  5974. +static int vc_dmaman_chan_alloc(struct vc_dmaman *dmaman,
  5975. + unsigned preferred_feature_set)
  5976. +{
  5977. + u32 chans;
  5978. + int feature;
  5979. +
  5980. + chans = dmaman->chan_available;
  5981. + for (feature = 0; feature < BCM_DMA_FEATURE_COUNT; feature++)
  5982. + /* select the subset of available channels with the desired
  5983. + feature so long as some of the candidate channels have that
  5984. + feature */
  5985. + if ((preferred_feature_set & (1 << feature)) &&
  5986. + (chans & dmaman->has_feature[feature]))
  5987. + chans &= dmaman->has_feature[feature];
  5988. +
  5989. + if (chans) {
  5990. + int chan = 0;
  5991. + /* return the ordinal of the first channel in the bitmap */
  5992. + while (chans != 0 && (chans & 1) == 0) {
  5993. + chans >>= 1;
  5994. + chan++;
  5995. + }
  5996. + /* claim the channel */
  5997. + dmaman->chan_available &= ~(1 << chan);
  5998. + return chan;
  5999. + } else
  6000. + return -ENOMEM;
  6001. +}
  6002. +
  6003. +static int vc_dmaman_chan_free(struct vc_dmaman *dmaman, int chan)
  6004. +{
  6005. + if (chan < 0)
  6006. + return -EINVAL;
  6007. + else if ((1 << chan) & dmaman->chan_available)
  6008. + return -EIDRM;
  6009. + else {
  6010. + dmaman->chan_available |= (1 << chan);
  6011. + return 0;
  6012. + }
  6013. +}
  6014. +
  6015. +/*****************************************************************************\
  6016. + * *
  6017. + * DMA IRQs *
  6018. + * *
  6019. +\*****************************************************************************/
  6020. +
  6021. +static unsigned char bcm_dma_irqs[] = {
  6022. + IRQ_DMA0,
  6023. + IRQ_DMA1,
  6024. + IRQ_DMA2,
  6025. + IRQ_DMA3,
  6026. + IRQ_DMA4,
  6027. + IRQ_DMA5,
  6028. + IRQ_DMA6,
  6029. + IRQ_DMA7,
  6030. + IRQ_DMA8,
  6031. + IRQ_DMA9,
  6032. + IRQ_DMA10,
  6033. + IRQ_DMA11,
  6034. + IRQ_DMA12
  6035. +};
  6036. +
  6037. +
  6038. +/***************************************************************************** \
  6039. + * *
  6040. + * DMA Manager Monitor *
  6041. + * *
  6042. +\*****************************************************************************/
  6043. +
  6044. +static struct device *dmaman_dev; /* we assume there's only one! */
  6045. +
  6046. +extern int bcm_dma_chan_alloc(unsigned preferred_feature_set,
  6047. + void __iomem **out_dma_base, int *out_dma_irq)
  6048. +{
  6049. + if (!dmaman_dev)
  6050. + return -ENODEV;
  6051. + else {
  6052. + struct vc_dmaman *dmaman = dev_get_drvdata(dmaman_dev);
  6053. + int rc;
  6054. +
  6055. + device_lock(dmaman_dev);
  6056. + rc = vc_dmaman_chan_alloc(dmaman, preferred_feature_set);
  6057. + if (rc >= 0) {
  6058. + *out_dma_base = BCM2708_DMA_CHANIO(dmaman->dma_base,
  6059. + rc);
  6060. + *out_dma_irq = bcm_dma_irqs[rc];
  6061. + }
  6062. + device_unlock(dmaman_dev);
  6063. +
  6064. + return rc;
  6065. + }
  6066. +}
  6067. +EXPORT_SYMBOL_GPL(bcm_dma_chan_alloc);
  6068. +
  6069. +extern int bcm_dma_chan_free(int channel)
  6070. +{
  6071. + if (dmaman_dev) {
  6072. + struct vc_dmaman *dmaman = dev_get_drvdata(dmaman_dev);
  6073. + int rc;
  6074. +
  6075. + device_lock(dmaman_dev);
  6076. + rc = vc_dmaman_chan_free(dmaman, channel);
  6077. + device_unlock(dmaman_dev);
  6078. +
  6079. + return rc;
  6080. + } else
  6081. + return -ENODEV;
  6082. +}
  6083. +EXPORT_SYMBOL_GPL(bcm_dma_chan_free);
  6084. +
  6085. +static int dev_dmaman_register(const char *dev_name, struct device *dev)
  6086. +{
  6087. + int rc = dmaman_dev ? -EINVAL : 0;
  6088. + dmaman_dev = dev;
  6089. + return rc;
  6090. +}
  6091. +
  6092. +static void dev_dmaman_deregister(const char *dev_name, struct device *dev)
  6093. +{
  6094. + dmaman_dev = NULL;
  6095. +}
  6096. +
  6097. +/*****************************************************************************\
  6098. + * *
  6099. + * DMA Device *
  6100. + * *
  6101. +\*****************************************************************************/
  6102. +
  6103. +static int dmachans = -1; /* module parameter */
  6104. +
  6105. +static int bcm_dmaman_probe(struct platform_device *pdev)
  6106. +{
  6107. + int ret = 0;
  6108. + struct vc_dmaman *dmaman;
  6109. + struct resource *dma_res = NULL;
  6110. + void __iomem *dma_base = NULL;
  6111. + int have_dma_region = 0;
  6112. +
  6113. + dmaman = kzalloc(sizeof(*dmaman), GFP_KERNEL);
  6114. + if (NULL == dmaman) {
  6115. + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
  6116. + "DMA management memory\n");
  6117. + ret = -ENOMEM;
  6118. + } else {
  6119. +
  6120. + dma_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  6121. + if (dma_res == NULL) {
  6122. + printk(KERN_ERR DRIVER_NAME ": failed to obtain memory "
  6123. + "resource\n");
  6124. + ret = -ENODEV;
  6125. + } else if (!request_mem_region(dma_res->start,
  6126. + resource_size(dma_res),
  6127. + DRIVER_NAME)) {
  6128. + dev_err(&pdev->dev, "cannot obtain DMA region\n");
  6129. + ret = -EBUSY;
  6130. + } else {
  6131. + have_dma_region = 1;
  6132. + dma_base = ioremap(dma_res->start,
  6133. + resource_size(dma_res));
  6134. + if (!dma_base) {
  6135. + dev_err(&pdev->dev, "cannot map DMA region\n");
  6136. + ret = -ENOMEM;
  6137. + } else {
  6138. + /* use module parameter if one was provided */
  6139. + if (dmachans > 0)
  6140. + vc_dmaman_init(dmaman, dma_base,
  6141. + dmachans);
  6142. + else
  6143. + vc_dmaman_init(dmaman, dma_base,
  6144. + DEFAULT_DMACHAN_BITMAP);
  6145. +
  6146. + platform_set_drvdata(pdev, dmaman);
  6147. + dev_dmaman_register(DRIVER_NAME, &pdev->dev);
  6148. +
  6149. + printk(KERN_INFO DRIVER_NAME ": DMA manager "
  6150. + "at %p\n", dma_base);
  6151. + }
  6152. + }
  6153. + }
  6154. + if (ret != 0) {
  6155. + if (dma_base)
  6156. + iounmap(dma_base);
  6157. + if (dma_res && have_dma_region)
  6158. + release_mem_region(dma_res->start,
  6159. + resource_size(dma_res));
  6160. + if (dmaman)
  6161. + kfree(dmaman);
  6162. + }
  6163. + return ret;
  6164. +}
  6165. +
  6166. +static int bcm_dmaman_remove(struct platform_device *pdev)
  6167. +{
  6168. + struct vc_dmaman *dmaman = platform_get_drvdata(pdev);
  6169. +
  6170. + platform_set_drvdata(pdev, NULL);
  6171. + dev_dmaman_deregister(DRIVER_NAME, &pdev->dev);
  6172. + kfree(dmaman);
  6173. +
  6174. + return 0;
  6175. +}
  6176. +
  6177. +static struct platform_driver bcm_dmaman_driver = {
  6178. + .probe = bcm_dmaman_probe,
  6179. + .remove = bcm_dmaman_remove,
  6180. +
  6181. + .driver = {
  6182. + .name = DRIVER_NAME,
  6183. + .owner = THIS_MODULE,
  6184. + },
  6185. +};
  6186. +
  6187. +/*****************************************************************************\
  6188. + * *
  6189. + * Driver init/exit *
  6190. + * *
  6191. +\*****************************************************************************/
  6192. +
  6193. +static int __init bcm_dmaman_drv_init(void)
  6194. +{
  6195. + int ret;
  6196. +
  6197. + ret = platform_driver_register(&bcm_dmaman_driver);
  6198. + if (ret != 0) {
  6199. + printk(KERN_ERR DRIVER_NAME ": failed to register "
  6200. + "on platform\n");
  6201. + }
  6202. +
  6203. + return ret;
  6204. +}
  6205. +
  6206. +static void __exit bcm_dmaman_drv_exit(void)
  6207. +{
  6208. + platform_driver_unregister(&bcm_dmaman_driver);
  6209. +}
  6210. +
  6211. +module_init(bcm_dmaman_drv_init);
  6212. +module_exit(bcm_dmaman_drv_exit);
  6213. +
  6214. +module_param(dmachans, int, 0644);
  6215. +
  6216. +MODULE_AUTHOR("Gray Girling <grayg@broadcom.com>");
  6217. +MODULE_DESCRIPTION("DMA channel manager driver");
  6218. +MODULE_LICENSE("GPL");
  6219. +
  6220. +MODULE_PARM_DESC(dmachans, "Bitmap of DMA channels available to the ARM");
  6221. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/arm_control.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/arm_control.h
  6222. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/arm_control.h 1969-12-31 18:00:00.000000000 -0600
  6223. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/arm_control.h 2014-12-03 19:13:32.448418001 -0600
  6224. @@ -0,0 +1,419 @@
  6225. +/*
  6226. + * linux/arch/arm/mach-bcm2708/arm_control.h
  6227. + *
  6228. + * Copyright (C) 2010 Broadcom
  6229. + *
  6230. + * This program is free software; you can redistribute it and/or modify
  6231. + * it under the terms of the GNU General Public License as published by
  6232. + * the Free Software Foundation; either version 2 of the License, or
  6233. + * (at your option) any later version.
  6234. + *
  6235. + * This program is distributed in the hope that it will be useful,
  6236. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6237. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6238. + * GNU General Public License for more details.
  6239. + *
  6240. + * You should have received a copy of the GNU General Public License
  6241. + * along with this program; if not, write to the Free Software
  6242. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6243. + */
  6244. +
  6245. +#ifndef __BCM2708_ARM_CONTROL_H
  6246. +#define __BCM2708_ARM_CONTROL_H
  6247. +
  6248. +/*
  6249. + * Definitions and addresses for the ARM CONTROL logic
  6250. + * This file is manually generated.
  6251. + */
  6252. +
  6253. +#define ARM_BASE 0x7E00B000
  6254. +
  6255. +/* Basic configuration */
  6256. +#define ARM_CONTROL0 HW_REGISTER_RW(ARM_BASE+0x000)
  6257. +#define ARM_C0_SIZ128M 0x00000000
  6258. +#define ARM_C0_SIZ256M 0x00000001
  6259. +#define ARM_C0_SIZ512M 0x00000002
  6260. +#define ARM_C0_SIZ1G 0x00000003
  6261. +#define ARM_C0_BRESP0 0x00000000
  6262. +#define ARM_C0_BRESP1 0x00000004
  6263. +#define ARM_C0_BRESP2 0x00000008
  6264. +#define ARM_C0_BOOTHI 0x00000010
  6265. +#define ARM_C0_UNUSED05 0x00000020 /* free */
  6266. +#define ARM_C0_FULLPERI 0x00000040
  6267. +#define ARM_C0_UNUSED78 0x00000180 /* free */
  6268. +#define ARM_C0_JTAGMASK 0x00000E00
  6269. +#define ARM_C0_JTAGOFF 0x00000000
  6270. +#define ARM_C0_JTAGBASH 0x00000800 /* Debug on GPIO off */
  6271. +#define ARM_C0_JTAGGPIO 0x00000C00 /* Debug on GPIO on */
  6272. +#define ARM_C0_APROTMSK 0x0000F000
  6273. +#define ARM_C0_DBG0SYNC 0x00010000 /* VPU0 halt sync */
  6274. +#define ARM_C0_DBG1SYNC 0x00020000 /* VPU1 halt sync */
  6275. +#define ARM_C0_SWDBGREQ 0x00040000 /* HW debug request */
  6276. +#define ARM_C0_PASSHALT 0x00080000 /* ARM halt passed to debugger */
  6277. +#define ARM_C0_PRIO_PER 0x00F00000 /* per priority mask */
  6278. +#define ARM_C0_PRIO_L2 0x0F000000
  6279. +#define ARM_C0_PRIO_UC 0xF0000000
  6280. +
  6281. +#define ARM_C0_APROTPASS 0x0000A000 /* Translate 1:1 */
  6282. +#define ARM_C0_APROTUSER 0x00000000 /* Only user mode */
  6283. +#define ARM_C0_APROTSYST 0x0000F000 /* Only system mode */
  6284. +
  6285. +
  6286. +#define ARM_CONTROL1 HW_REGISTER_RW(ARM_BASE+0x440)
  6287. +#define ARM_C1_TIMER 0x00000001 /* re-route timer IRQ to VC */
  6288. +#define ARM_C1_MAIL 0x00000002 /* re-route Mail IRQ to VC */
  6289. +#define ARM_C1_BELL0 0x00000004 /* re-route Doorbell 0 to VC */
  6290. +#define ARM_C1_BELL1 0x00000008 /* re-route Doorbell 1 to VC */
  6291. +#define ARM_C1_PERSON 0x00000100 /* peripherals on */
  6292. +#define ARM_C1_REQSTOP 0x00000200 /* ASYNC bridge request stop */
  6293. +
  6294. +#define ARM_STATUS HW_REGISTER_RW(ARM_BASE+0x444)
  6295. +#define ARM_S_ACKSTOP 0x80000000 /* Bridge stopped */
  6296. +#define ARM_S_READPEND 0x000003FF /* pending reads counter */
  6297. +#define ARM_S_WRITPEND 0x000FFC00 /* pending writes counter */
  6298. +
  6299. +#define ARM_ERRHALT HW_REGISTER_RW(ARM_BASE+0x448)
  6300. +#define ARM_EH_PERIBURST 0x00000001 /* Burst write seen on peri bus */
  6301. +#define ARM_EH_ILLADDRS1 0x00000002 /* Address bits 25-27 error */
  6302. +#define ARM_EH_ILLADDRS2 0x00000004 /* Address bits 31-28 error */
  6303. +#define ARM_EH_VPU0HALT 0x00000008 /* VPU0 halted & in debug mode */
  6304. +#define ARM_EH_VPU1HALT 0x00000010 /* VPU1 halted & in debug mode */
  6305. +#define ARM_EH_ARMHALT 0x00000020 /* ARM in halted debug mode */
  6306. +
  6307. +#define ARM_ID_SECURE HW_REGISTER_RW(ARM_BASE+0x00C)
  6308. +#define ARM_ID HW_REGISTER_RW(ARM_BASE+0x44C)
  6309. +#define ARM_IDVAL 0x364D5241
  6310. +
  6311. +/* Translation memory */
  6312. +#define ARM_TRANSLATE HW_REGISTER_RW(ARM_BASE+0x100)
  6313. +/* 32 locations: 0x100.. 0x17F */
  6314. +/* 32 spare means we CAN go to 64 pages.... */
  6315. +
  6316. +
  6317. +/* Interrupts */
  6318. +#define ARM_IRQ_PEND0 HW_REGISTER_RW(ARM_BASE+0x200) /* Top IRQ bits */
  6319. +#define ARM_I0_TIMER 0x00000001 /* timer IRQ */
  6320. +#define ARM_I0_MAIL 0x00000002 /* Mail IRQ */
  6321. +#define ARM_I0_BELL0 0x00000004 /* Doorbell 0 */
  6322. +#define ARM_I0_BELL1 0x00000008 /* Doorbell 1 */
  6323. +#define ARM_I0_BANK1 0x00000100 /* Bank1 IRQ */
  6324. +#define ARM_I0_BANK2 0x00000200 /* Bank2 IRQ */
  6325. +
  6326. +#define ARM_IRQ_PEND1 HW_REGISTER_RW(ARM_BASE+0x204) /* All bank1 IRQ bits */
  6327. +/* todo: all I1_interrupt sources */
  6328. +#define ARM_IRQ_PEND2 HW_REGISTER_RW(ARM_BASE+0x208) /* All bank2 IRQ bits */
  6329. +/* todo: all I2_interrupt sources */
  6330. +
  6331. +#define ARM_IRQ_FAST HW_REGISTER_RW(ARM_BASE+0x20C) /* FIQ control */
  6332. +#define ARM_IF_INDEX 0x0000007F /* FIQ select */
  6333. +#define ARM_IF_ENABLE 0x00000080 /* FIQ enable */
  6334. +#define ARM_IF_VCMASK 0x0000003F /* FIQ = (index from VC source) */
  6335. +#define ARM_IF_TIMER 0x00000040 /* FIQ = ARM timer */
  6336. +#define ARM_IF_MAIL 0x00000041 /* FIQ = ARM Mail */
  6337. +#define ARM_IF_BELL0 0x00000042 /* FIQ = ARM Doorbell 0 */
  6338. +#define ARM_IF_BELL1 0x00000043 /* FIQ = ARM Doorbell 1 */
  6339. +#define ARM_IF_VP0HALT 0x00000044 /* FIQ = VPU0 Halt seen */
  6340. +#define ARM_IF_VP1HALT 0x00000045 /* FIQ = VPU1 Halt seen */
  6341. +#define ARM_IF_ILLEGAL 0x00000046 /* FIQ = Illegal access seen */
  6342. +
  6343. +#define ARM_IRQ_ENBL1 HW_REGISTER_RW(ARM_BASE+0x210) /* Bank1 enable bits */
  6344. +#define ARM_IRQ_ENBL2 HW_REGISTER_RW(ARM_BASE+0x214) /* Bank2 enable bits */
  6345. +#define ARM_IRQ_ENBL3 HW_REGISTER_RW(ARM_BASE+0x218) /* ARM irqs enable bits */
  6346. +#define ARM_IRQ_DIBL1 HW_REGISTER_RW(ARM_BASE+0x21C) /* Bank1 disable bits */
  6347. +#define ARM_IRQ_DIBL2 HW_REGISTER_RW(ARM_BASE+0x220) /* Bank2 disable bits */
  6348. +#define ARM_IRQ_DIBL3 HW_REGISTER_RW(ARM_BASE+0x224) /* ARM irqs disable bits */
  6349. +#define ARM_IE_TIMER 0x00000001 /* Timer IRQ */
  6350. +#define ARM_IE_MAIL 0x00000002 /* Mail IRQ */
  6351. +#define ARM_IE_BELL0 0x00000004 /* Doorbell 0 */
  6352. +#define ARM_IE_BELL1 0x00000008 /* Doorbell 1 */
  6353. +#define ARM_IE_VP0HALT 0x00000010 /* VPU0 Halt */
  6354. +#define ARM_IE_VP1HALT 0x00000020 /* VPU1 Halt */
  6355. +#define ARM_IE_ILLEGAL 0x00000040 /* Illegal access seen */
  6356. +
  6357. +/* Timer */
  6358. +/* For reg. fields see sp804 spec. */
  6359. +#define ARM_T_LOAD HW_REGISTER_RW(ARM_BASE+0x400)
  6360. +#define ARM_T_VALUE HW_REGISTER_RW(ARM_BASE+0x404)
  6361. +#define ARM_T_CONTROL HW_REGISTER_RW(ARM_BASE+0x408)
  6362. +#define ARM_T_IRQCNTL HW_REGISTER_RW(ARM_BASE+0x40C)
  6363. +#define ARM_T_RAWIRQ HW_REGISTER_RW(ARM_BASE+0x410)
  6364. +#define ARM_T_MSKIRQ HW_REGISTER_RW(ARM_BASE+0x414)
  6365. +#define ARM_T_RELOAD HW_REGISTER_RW(ARM_BASE+0x418)
  6366. +#define ARM_T_PREDIV HW_REGISTER_RW(ARM_BASE+0x41c)
  6367. +#define ARM_T_FREECNT HW_REGISTER_RW(ARM_BASE+0x420)
  6368. +
  6369. +#define TIMER_CTRL_ONESHOT (1 << 0)
  6370. +#define TIMER_CTRL_32BIT (1 << 1)
  6371. +#define TIMER_CTRL_DIV1 (0 << 2)
  6372. +#define TIMER_CTRL_DIV16 (1 << 2)
  6373. +#define TIMER_CTRL_DIV256 (2 << 2)
  6374. +#define TIMER_CTRL_IE (1 << 5)
  6375. +#define TIMER_CTRL_PERIODIC (1 << 6)
  6376. +#define TIMER_CTRL_ENABLE (1 << 7)
  6377. +#define TIMER_CTRL_DBGHALT (1 << 8)
  6378. +#define TIMER_CTRL_ENAFREE (1 << 9)
  6379. +#define TIMER_CTRL_FREEDIV_SHIFT 16)
  6380. +#define TIMER_CTRL_FREEDIV_MASK 0xff
  6381. +
  6382. +/* Semaphores, Doorbells, Mailboxes */
  6383. +#define ARM_SBM_OWN0 (ARM_BASE+0x800)
  6384. +#define ARM_SBM_OWN1 (ARM_BASE+0x900)
  6385. +#define ARM_SBM_OWN2 (ARM_BASE+0xA00)
  6386. +#define ARM_SBM_OWN3 (ARM_BASE+0xB00)
  6387. +
  6388. +/* MAILBOXES
  6389. + * Register flags are common across all
  6390. + * owner registers. See end of this section
  6391. + *
  6392. + * Semaphores, Doorbells, Mailboxes Owner 0
  6393. + *
  6394. + */
  6395. +
  6396. +#define ARM_0_SEMS HW_REGISTER_RW(ARM_SBM_OWN0+0x00)
  6397. +#define ARM_0_SEM0 HW_REGISTER_RW(ARM_SBM_OWN0+0x00)
  6398. +#define ARM_0_SEM1 HW_REGISTER_RW(ARM_SBM_OWN0+0x04)
  6399. +#define ARM_0_SEM2 HW_REGISTER_RW(ARM_SBM_OWN0+0x08)
  6400. +#define ARM_0_SEM3 HW_REGISTER_RW(ARM_SBM_OWN0+0x0C)
  6401. +#define ARM_0_SEM4 HW_REGISTER_RW(ARM_SBM_OWN0+0x10)
  6402. +#define ARM_0_SEM5 HW_REGISTER_RW(ARM_SBM_OWN0+0x14)
  6403. +#define ARM_0_SEM6 HW_REGISTER_RW(ARM_SBM_OWN0+0x18)
  6404. +#define ARM_0_SEM7 HW_REGISTER_RW(ARM_SBM_OWN0+0x1C)
  6405. +#define ARM_0_BELL0 HW_REGISTER_RW(ARM_SBM_OWN0+0x40)
  6406. +#define ARM_0_BELL1 HW_REGISTER_RW(ARM_SBM_OWN0+0x44)
  6407. +#define ARM_0_BELL2 HW_REGISTER_RW(ARM_SBM_OWN0+0x48)
  6408. +#define ARM_0_BELL3 HW_REGISTER_RW(ARM_SBM_OWN0+0x4C)
  6409. +/* MAILBOX 0 access in Owner 0 area */
  6410. +/* Some addresses should ONLY be used by owner 0 */
  6411. +#define ARM_0_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN0+0x80) /* .. 0x8C (4 locations) */
  6412. +#define ARM_0_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN0+0x80) /* .. 0x8C (4 locations) Normal read */
  6413. +#define ARM_0_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN0+0x90) /* none-pop read */
  6414. +#define ARM_0_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN0+0x94) /* Sender read (only LS 2 bits) */
  6415. +#define ARM_0_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN0+0x98) /* Status read */
  6416. +#define ARM_0_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN0+0x9C) /* Config read/write */
  6417. +/* MAILBOX 1 access in Owner 0 area */
  6418. +/* Owner 0 should only WRITE to this mailbox */
  6419. +#define ARM_0_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN0+0xA0) /* .. 0xAC (4 locations) */
  6420. +/*#define ARM_0_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN0+0xA0) */ /* DO NOT USE THIS !!!!! */
  6421. +/*#define ARM_0_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN0+0xB0) */ /* DO NOT USE THIS !!!!! */
  6422. +/*#define ARM_0_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN0+0xB4) */ /* DO NOT USE THIS !!!!! */
  6423. +#define ARM_0_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN0+0xB8) /* Status read */
  6424. +/*#define ARM_0_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN0+0xBC) */ /* DO NOT USE THIS !!!!! */
  6425. +/* General SEM, BELL, MAIL config/status */
  6426. +#define ARM_0_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN0+0xE0) /* semaphore clear/debug register */
  6427. +#define ARM_0_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN0+0xE4) /* Doorbells clear/debug register */
  6428. +#define ARM_0_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN0+0xF8) /* ALL interrupts */
  6429. +#define ARM_0_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN0+0xFC) /* IRQS pending for owner 0 */
  6430. +
  6431. +/* Semaphores, Doorbells, Mailboxes Owner 1 */
  6432. +#define ARM_1_SEMS HW_REGISTER_RW(ARM_SBM_OWN1+0x00)
  6433. +#define ARM_1_SEM0 HW_REGISTER_RW(ARM_SBM_OWN1+0x00)
  6434. +#define ARM_1_SEM1 HW_REGISTER_RW(ARM_SBM_OWN1+0x04)
  6435. +#define ARM_1_SEM2 HW_REGISTER_RW(ARM_SBM_OWN1+0x08)
  6436. +#define ARM_1_SEM3 HW_REGISTER_RW(ARM_SBM_OWN1+0x0C)
  6437. +#define ARM_1_SEM4 HW_REGISTER_RW(ARM_SBM_OWN1+0x10)
  6438. +#define ARM_1_SEM5 HW_REGISTER_RW(ARM_SBM_OWN1+0x14)
  6439. +#define ARM_1_SEM6 HW_REGISTER_RW(ARM_SBM_OWN1+0x18)
  6440. +#define ARM_1_SEM7 HW_REGISTER_RW(ARM_SBM_OWN1+0x1C)
  6441. +#define ARM_1_BELL0 HW_REGISTER_RW(ARM_SBM_OWN1+0x40)
  6442. +#define ARM_1_BELL1 HW_REGISTER_RW(ARM_SBM_OWN1+0x44)
  6443. +#define ARM_1_BELL2 HW_REGISTER_RW(ARM_SBM_OWN1+0x48)
  6444. +#define ARM_1_BELL3 HW_REGISTER_RW(ARM_SBM_OWN1+0x4C)
  6445. +/* MAILBOX 0 access in Owner 0 area */
  6446. +/* Owner 1 should only WRITE to this mailbox */
  6447. +#define ARM_1_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN1+0x80) /* .. 0x8C (4 locations) */
  6448. +/*#define ARM_1_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN1+0x80) */ /* DO NOT USE THIS !!!!! */
  6449. +/*#define ARM_1_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN1+0x90) */ /* DO NOT USE THIS !!!!! */
  6450. +/*#define ARM_1_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN1+0x94) */ /* DO NOT USE THIS !!!!! */
  6451. +#define ARM_1_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN1+0x98) /* Status read */
  6452. +/*#define ARM_1_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN1+0x9C) */ /* DO NOT USE THIS !!!!! */
  6453. +/* MAILBOX 1 access in Owner 0 area */
  6454. +#define ARM_1_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN1+0xA0) /* .. 0xAC (4 locations) */
  6455. +#define ARM_1_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN1+0xA0) /* .. 0xAC (4 locations) Normal read */
  6456. +#define ARM_1_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN1+0xB0) /* none-pop read */
  6457. +#define ARM_1_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN1+0xB4) /* Sender read (only LS 2 bits) */
  6458. +#define ARM_1_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN1+0xB8) /* Status read */
  6459. +#define ARM_1_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN1+0xBC)
  6460. +/* General SEM, BELL, MAIL config/status */
  6461. +#define ARM_1_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN1+0xE0) /* semaphore clear/debug register */
  6462. +#define ARM_1_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN1+0xE4) /* Doorbells clear/debug register */
  6463. +#define ARM_1_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN1+0xFC) /* IRQS pending for owner 1 */
  6464. +#define ARM_1_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN1+0xF8) /* ALL interrupts */
  6465. +
  6466. +/* Semaphores, Doorbells, Mailboxes Owner 2 */
  6467. +#define ARM_2_SEMS HW_REGISTER_RW(ARM_SBM_OWN2+0x00)
  6468. +#define ARM_2_SEM0 HW_REGISTER_RW(ARM_SBM_OWN2+0x00)
  6469. +#define ARM_2_SEM1 HW_REGISTER_RW(ARM_SBM_OWN2+0x04)
  6470. +#define ARM_2_SEM2 HW_REGISTER_RW(ARM_SBM_OWN2+0x08)
  6471. +#define ARM_2_SEM3 HW_REGISTER_RW(ARM_SBM_OWN2+0x0C)
  6472. +#define ARM_2_SEM4 HW_REGISTER_RW(ARM_SBM_OWN2+0x10)
  6473. +#define ARM_2_SEM5 HW_REGISTER_RW(ARM_SBM_OWN2+0x14)
  6474. +#define ARM_2_SEM6 HW_REGISTER_RW(ARM_SBM_OWN2+0x18)
  6475. +#define ARM_2_SEM7 HW_REGISTER_RW(ARM_SBM_OWN2+0x1C)
  6476. +#define ARM_2_BELL0 HW_REGISTER_RW(ARM_SBM_OWN2+0x40)
  6477. +#define ARM_2_BELL1 HW_REGISTER_RW(ARM_SBM_OWN2+0x44)
  6478. +#define ARM_2_BELL2 HW_REGISTER_RW(ARM_SBM_OWN2+0x48)
  6479. +#define ARM_2_BELL3 HW_REGISTER_RW(ARM_SBM_OWN2+0x4C)
  6480. +/* MAILBOX 0 access in Owner 2 area */
  6481. +/* Owner 2 should only WRITE to this mailbox */
  6482. +#define ARM_2_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN2+0x80) /* .. 0x8C (4 locations) */
  6483. +/*#define ARM_2_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN2+0x80) */ /* DO NOT USE THIS !!!!! */
  6484. +/*#define ARM_2_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN2+0x90) */ /* DO NOT USE THIS !!!!! */
  6485. +/*#define ARM_2_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN2+0x94) */ /* DO NOT USE THIS !!!!! */
  6486. +#define ARM_2_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN2+0x98) /* Status read */
  6487. +/*#define ARM_2_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN2+0x9C) */ /* DO NOT USE THIS !!!!! */
  6488. +/* MAILBOX 1 access in Owner 2 area */
  6489. +/* Owner 2 should only WRITE to this mailbox */
  6490. +#define ARM_2_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN2+0xA0) /* .. 0xAC (4 locations) */
  6491. +/*#define ARM_2_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN2+0xA0) */ /* DO NOT USE THIS !!!!! */
  6492. +/*#define ARM_2_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN2+0xB0) */ /* DO NOT USE THIS !!!!! */
  6493. +/*#define ARM_2_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN2+0xB4) */ /* DO NOT USE THIS !!!!! */
  6494. +#define ARM_2_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN2+0xB8) /* Status read */
  6495. +/*#define ARM_2_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN2+0xBC) */ /* DO NOT USE THIS !!!!! */
  6496. +/* General SEM, BELL, MAIL config/status */
  6497. +#define ARM_2_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN2+0xE0) /* semaphore clear/debug register */
  6498. +#define ARM_2_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN2+0xE4) /* Doorbells clear/debug register */
  6499. +#define ARM_2_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN2+0xFC) /* IRQS pending for owner 2 */
  6500. +#define ARM_2_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN2+0xF8) /* ALL interrupts */
  6501. +
  6502. +/* Semaphores, Doorbells, Mailboxes Owner 3 */
  6503. +#define ARM_3_SEMS HW_REGISTER_RW(ARM_SBM_OWN3+0x00)
  6504. +#define ARM_3_SEM0 HW_REGISTER_RW(ARM_SBM_OWN3+0x00)
  6505. +#define ARM_3_SEM1 HW_REGISTER_RW(ARM_SBM_OWN3+0x04)
  6506. +#define ARM_3_SEM2 HW_REGISTER_RW(ARM_SBM_OWN3+0x08)
  6507. +#define ARM_3_SEM3 HW_REGISTER_RW(ARM_SBM_OWN3+0x0C)
  6508. +#define ARM_3_SEM4 HW_REGISTER_RW(ARM_SBM_OWN3+0x10)
  6509. +#define ARM_3_SEM5 HW_REGISTER_RW(ARM_SBM_OWN3+0x14)
  6510. +#define ARM_3_SEM6 HW_REGISTER_RW(ARM_SBM_OWN3+0x18)
  6511. +#define ARM_3_SEM7 HW_REGISTER_RW(ARM_SBM_OWN3+0x1C)
  6512. +#define ARM_3_BELL0 HW_REGISTER_RW(ARM_SBM_OWN3+0x40)
  6513. +#define ARM_3_BELL1 HW_REGISTER_RW(ARM_SBM_OWN3+0x44)
  6514. +#define ARM_3_BELL2 HW_REGISTER_RW(ARM_SBM_OWN3+0x48)
  6515. +#define ARM_3_BELL3 HW_REGISTER_RW(ARM_SBM_OWN3+0x4C)
  6516. +/* MAILBOX 0 access in Owner 3 area */
  6517. +/* Owner 3 should only WRITE to this mailbox */
  6518. +#define ARM_3_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN3+0x80) /* .. 0x8C (4 locations) */
  6519. +/*#define ARM_3_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN3+0x80) */ /* DO NOT USE THIS !!!!! */
  6520. +/*#define ARM_3_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN3+0x90) */ /* DO NOT USE THIS !!!!! */
  6521. +/*#define ARM_3_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN3+0x94) */ /* DO NOT USE THIS !!!!! */
  6522. +#define ARM_3_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN3+0x98) /* Status read */
  6523. +/*#define ARM_3_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN3+0x9C) */ /* DO NOT USE THIS !!!!! */
  6524. +/* MAILBOX 1 access in Owner 3 area */
  6525. +/* Owner 3 should only WRITE to this mailbox */
  6526. +#define ARM_3_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN3+0xA0) /* .. 0xAC (4 locations) */
  6527. +/*#define ARM_3_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN3+0xA0) */ /* DO NOT USE THIS !!!!! */
  6528. +/*#define ARM_3_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN3+0xB0) */ /* DO NOT USE THIS !!!!! */
  6529. +/*#define ARM_3_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN3+0xB4) */ /* DO NOT USE THIS !!!!! */
  6530. +#define ARM_3_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN3+0xB8) /* Status read */
  6531. +/*#define ARM_3_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN3+0xBC) */ /* DO NOT USE THIS !!!!! */
  6532. +/* General SEM, BELL, MAIL config/status */
  6533. +#define ARM_3_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN3+0xE0) /* semaphore clear/debug register */
  6534. +#define ARM_3_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN3+0xE4) /* Doorbells clear/debug register */
  6535. +#define ARM_3_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN3+0xFC) /* IRQS pending for owner 3 */
  6536. +#define ARM_3_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN3+0xF8) /* ALL interrupts */
  6537. +
  6538. +
  6539. +
  6540. +/* Mailbox flags. Valid for all owners */
  6541. +
  6542. +/* Mailbox status register (...0x98) */
  6543. +#define ARM_MS_FULL 0x80000000
  6544. +#define ARM_MS_EMPTY 0x40000000
  6545. +#define ARM_MS_LEVEL 0x400000FF /* Max. value depdnds on mailbox depth parameter */
  6546. +
  6547. +/* MAILBOX config/status register (...0x9C) */
  6548. +/* ANY write to this register clears the error bits! */
  6549. +#define ARM_MC_IHAVEDATAIRQEN 0x00000001 /* mailbox irq enable: has data */
  6550. +#define ARM_MC_IHAVESPACEIRQEN 0x00000002 /* mailbox irq enable: has space */
  6551. +#define ARM_MC_OPPISEMPTYIRQEN 0x00000004 /* mailbox irq enable: Opp. is empty */
  6552. +#define ARM_MC_MAIL_CLEAR 0x00000008 /* mailbox clear write 1, then 0 */
  6553. +#define ARM_MC_IHAVEDATAIRQPEND 0x00000010 /* mailbox irq pending: has space */
  6554. +#define ARM_MC_IHAVESPACEIRQPEND 0x00000020 /* mailbox irq pending: Opp. is empty */
  6555. +#define ARM_MC_OPPISEMPTYIRQPEND 0x00000040 /* mailbox irq pending */
  6556. +/* Bit 7 is unused */
  6557. +#define ARM_MC_ERRNOOWN 0x00000100 /* error : none owner read from mailbox */
  6558. +#define ARM_MC_ERROVERFLW 0x00000200 /* error : write to fill mailbox */
  6559. +#define ARM_MC_ERRUNDRFLW 0x00000400 /* error : read from empty mailbox */
  6560. +
  6561. +/* Semaphore clear/debug register (...0xE0) */
  6562. +#define ARM_SD_OWN0 0x00000003 /* Owner of sem 0 */
  6563. +#define ARM_SD_OWN1 0x0000000C /* Owner of sem 1 */
  6564. +#define ARM_SD_OWN2 0x00000030 /* Owner of sem 2 */
  6565. +#define ARM_SD_OWN3 0x000000C0 /* Owner of sem 3 */
  6566. +#define ARM_SD_OWN4 0x00000300 /* Owner of sem 4 */
  6567. +#define ARM_SD_OWN5 0x00000C00 /* Owner of sem 5 */
  6568. +#define ARM_SD_OWN6 0x00003000 /* Owner of sem 6 */
  6569. +#define ARM_SD_OWN7 0x0000C000 /* Owner of sem 7 */
  6570. +#define ARM_SD_SEM0 0x00010000 /* Status of sem 0 */
  6571. +#define ARM_SD_SEM1 0x00020000 /* Status of sem 1 */
  6572. +#define ARM_SD_SEM2 0x00040000 /* Status of sem 2 */
  6573. +#define ARM_SD_SEM3 0x00080000 /* Status of sem 3 */
  6574. +#define ARM_SD_SEM4 0x00100000 /* Status of sem 4 */
  6575. +#define ARM_SD_SEM5 0x00200000 /* Status of sem 5 */
  6576. +#define ARM_SD_SEM6 0x00400000 /* Status of sem 6 */
  6577. +#define ARM_SD_SEM7 0x00800000 /* Status of sem 7 */
  6578. +
  6579. +/* Doorbells clear/debug register (...0xE4) */
  6580. +#define ARM_BD_OWN0 0x00000003 /* Owner of doorbell 0 */
  6581. +#define ARM_BD_OWN1 0x0000000C /* Owner of doorbell 1 */
  6582. +#define ARM_BD_OWN2 0x00000030 /* Owner of doorbell 2 */
  6583. +#define ARM_BD_OWN3 0x000000C0 /* Owner of doorbell 3 */
  6584. +#define ARM_BD_BELL0 0x00000100 /* Status of doorbell 0 */
  6585. +#define ARM_BD_BELL1 0x00000200 /* Status of doorbell 1 */
  6586. +#define ARM_BD_BELL2 0x00000400 /* Status of doorbell 2 */
  6587. +#define ARM_BD_BELL3 0x00000800 /* Status of doorbell 3 */
  6588. +
  6589. +/* MY IRQS register (...0xF8) */
  6590. +#define ARM_MYIRQ_BELL 0x00000001 /* This owner has a doorbell IRQ */
  6591. +#define ARM_MYIRQ_MAIL 0x00000002 /* This owner has a mailbox IRQ */
  6592. +
  6593. +/* ALL IRQS register (...0xF8) */
  6594. +#define ARM_AIS_BELL0 0x00000001 /* Doorbell 0 IRQ pending */
  6595. +#define ARM_AIS_BELL1 0x00000002 /* Doorbell 1 IRQ pending */
  6596. +#define ARM_AIS_BELL2 0x00000004 /* Doorbell 2 IRQ pending */
  6597. +#define ARM_AIS_BELL3 0x00000008 /* Doorbell 3 IRQ pending */
  6598. +#define ARM_AIS0_HAVEDATA 0x00000010 /* MAIL 0 has data IRQ pending */
  6599. +#define ARM_AIS0_HAVESPAC 0x00000020 /* MAIL 0 has space IRQ pending */
  6600. +#define ARM_AIS0_OPPEMPTY 0x00000040 /* MAIL 0 opposite is empty IRQ */
  6601. +#define ARM_AIS1_HAVEDATA 0x00000080 /* MAIL 1 has data IRQ pending */
  6602. +#define ARM_AIS1_HAVESPAC 0x00000100 /* MAIL 1 has space IRQ pending */
  6603. +#define ARM_AIS1_OPPEMPTY 0x00000200 /* MAIL 1 opposite is empty IRQ */
  6604. +/* Note that bell-0, bell-1 and MAIL0 IRQ go only to the ARM */
  6605. +/* Whilst that bell-2, bell-3 and MAIL1 IRQ go only to the VC */
  6606. +/* */
  6607. +/* ARM JTAG BASH */
  6608. +/* */
  6609. +#define AJB_BASE 0x7e2000c0
  6610. +
  6611. +#define AJBCONF HW_REGISTER_RW(AJB_BASE+0x00)
  6612. +#define AJB_BITS0 0x000000
  6613. +#define AJB_BITS4 0x000004
  6614. +#define AJB_BITS8 0x000008
  6615. +#define AJB_BITS12 0x00000C
  6616. +#define AJB_BITS16 0x000010
  6617. +#define AJB_BITS20 0x000014
  6618. +#define AJB_BITS24 0x000018
  6619. +#define AJB_BITS28 0x00001C
  6620. +#define AJB_BITS32 0x000020
  6621. +#define AJB_BITS34 0x000022
  6622. +#define AJB_OUT_MS 0x000040
  6623. +#define AJB_OUT_LS 0x000000
  6624. +#define AJB_INV_CLK 0x000080
  6625. +#define AJB_D0_RISE 0x000100
  6626. +#define AJB_D0_FALL 0x000000
  6627. +#define AJB_D1_RISE 0x000200
  6628. +#define AJB_D1_FALL 0x000000
  6629. +#define AJB_IN_RISE 0x000400
  6630. +#define AJB_IN_FALL 0x000000
  6631. +#define AJB_ENABLE 0x000800
  6632. +#define AJB_HOLD0 0x000000
  6633. +#define AJB_HOLD1 0x001000
  6634. +#define AJB_HOLD2 0x002000
  6635. +#define AJB_HOLD3 0x003000
  6636. +#define AJB_RESETN 0x004000
  6637. +#define AJB_CLKSHFT 16
  6638. +#define AJB_BUSY 0x80000000
  6639. +#define AJBTMS HW_REGISTER_RW(AJB_BASE+0x04)
  6640. +#define AJBTDI HW_REGISTER_RW(AJB_BASE+0x08)
  6641. +#define AJBTDO HW_REGISTER_RW(AJB_BASE+0x0c)
  6642. +
  6643. +#endif
  6644. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/arm_power.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/arm_power.h
  6645. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/arm_power.h 1969-12-31 18:00:00.000000000 -0600
  6646. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/arm_power.h 2014-12-03 19:13:32.448418001 -0600
  6647. @@ -0,0 +1,62 @@
  6648. +/*
  6649. + * linux/arch/arm/mach-bcm2708/include/mach/arm_power.h
  6650. + *
  6651. + * Copyright (C) 2010 Broadcom
  6652. + *
  6653. + * This program is free software; you can redistribute it and/or modify
  6654. + * it under the terms of the GNU General Public License as published by
  6655. + * the Free Software Foundation; either version 2 of the License, or
  6656. + * (at your option) any later version.
  6657. + *
  6658. + * This program is distributed in the hope that it will be useful,
  6659. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6660. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6661. + * GNU General Public License for more details.
  6662. + *
  6663. + * You should have received a copy of the GNU General Public License
  6664. + * along with this program; if not, write to the Free Software
  6665. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6666. + */
  6667. +
  6668. +#ifndef _ARM_POWER_H
  6669. +#define _ARM_POWER_H
  6670. +
  6671. +/* Use meaningful names on each side */
  6672. +#ifdef __VIDEOCORE__
  6673. +#define PREFIX(x) ARM_##x
  6674. +#else
  6675. +#define PREFIX(x) BCM_##x
  6676. +#endif
  6677. +
  6678. +enum {
  6679. + PREFIX(POWER_SDCARD_BIT),
  6680. + PREFIX(POWER_UART_BIT),
  6681. + PREFIX(POWER_MINIUART_BIT),
  6682. + PREFIX(POWER_USB_BIT),
  6683. + PREFIX(POWER_I2C0_BIT),
  6684. + PREFIX(POWER_I2C1_BIT),
  6685. + PREFIX(POWER_I2C2_BIT),
  6686. + PREFIX(POWER_SPI_BIT),
  6687. + PREFIX(POWER_CCP2TX_BIT),
  6688. + PREFIX(POWER_DSI_BIT),
  6689. +
  6690. + PREFIX(POWER_MAX)
  6691. +};
  6692. +
  6693. +enum {
  6694. + PREFIX(POWER_SDCARD) = (1 << PREFIX(POWER_SDCARD_BIT)),
  6695. + PREFIX(POWER_UART) = (1 << PREFIX(POWER_UART_BIT)),
  6696. + PREFIX(POWER_MINIUART) = (1 << PREFIX(POWER_MINIUART_BIT)),
  6697. + PREFIX(POWER_USB) = (1 << PREFIX(POWER_USB_BIT)),
  6698. + PREFIX(POWER_I2C0) = (1 << PREFIX(POWER_I2C0_BIT)),
  6699. + PREFIX(POWER_I2C1_MASK) = (1 << PREFIX(POWER_I2C1_BIT)),
  6700. + PREFIX(POWER_I2C2_MASK) = (1 << PREFIX(POWER_I2C2_BIT)),
  6701. + PREFIX(POWER_SPI_MASK) = (1 << PREFIX(POWER_SPI_BIT)),
  6702. + PREFIX(POWER_CCP2TX_MASK) = (1 << PREFIX(POWER_CCP2TX_BIT)),
  6703. + PREFIX(POWER_DSI) = (1 << PREFIX(POWER_DSI_BIT)),
  6704. +
  6705. + PREFIX(POWER_MASK) = (1 << PREFIX(POWER_MAX)) - 1,
  6706. + PREFIX(POWER_NONE) = 0
  6707. +};
  6708. +
  6709. +#endif
  6710. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/clkdev.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/clkdev.h
  6711. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/clkdev.h 1969-12-31 18:00:00.000000000 -0600
  6712. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/clkdev.h 2014-12-03 19:13:32.448418001 -0600
  6713. @@ -0,0 +1,7 @@
  6714. +#ifndef __ASM_MACH_CLKDEV_H
  6715. +#define __ASM_MACH_CLKDEV_H
  6716. +
  6717. +#define __clk_get(clk) ({ 1; })
  6718. +#define __clk_put(clk) do { } while (0)
  6719. +
  6720. +#endif
  6721. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/debug-macro.S linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/debug-macro.S
  6722. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/debug-macro.S 1969-12-31 18:00:00.000000000 -0600
  6723. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/debug-macro.S 2014-12-03 19:13:32.448418001 -0600
  6724. @@ -0,0 +1,22 @@
  6725. +/* arch/arm/mach-bcm2708/include/mach/debug-macro.S
  6726. + *
  6727. + * Debugging macro include header
  6728. + *
  6729. + * Copyright (C) 2010 Broadcom
  6730. + * Copyright (C) 1994-1999 Russell King
  6731. + * Moved from linux/arch/arm/kernel/debug.S by Ben Dooks
  6732. + *
  6733. + * This program is free software; you can redistribute it and/or modify
  6734. + * it under the terms of the GNU General Public License version 2 as
  6735. + * published by the Free Software Foundation.
  6736. + *
  6737. +*/
  6738. +
  6739. +#include <mach/platform.h>
  6740. +
  6741. + .macro addruart, rp, rv, tmp
  6742. + ldr \rp, =UART0_BASE
  6743. + ldr \rv, =IO_ADDRESS(UART0_BASE)
  6744. + .endm
  6745. +
  6746. +#include <debug/pl01x.S>
  6747. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/dma.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/dma.h
  6748. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/dma.h 1969-12-31 18:00:00.000000000 -0600
  6749. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/dma.h 2014-12-03 19:13:32.448418001 -0600
  6750. @@ -0,0 +1,94 @@
  6751. +/*
  6752. + * linux/arch/arm/mach-bcm2708/include/mach/dma.h
  6753. + *
  6754. + * Copyright (C) 2010 Broadcom
  6755. + *
  6756. + * This program is free software; you can redistribute it and/or modify
  6757. + * it under the terms of the GNU General Public License version 2 as
  6758. + * published by the Free Software Foundation.
  6759. + */
  6760. +
  6761. +
  6762. +#ifndef _MACH_BCM2708_DMA_H
  6763. +#define _MACH_BCM2708_DMA_H
  6764. +
  6765. +#define BCM_DMAMAN_DRIVER_NAME "bcm2708_dma"
  6766. +
  6767. +/* DMA CS Control and Status bits */
  6768. +#define BCM2708_DMA_ACTIVE (1 << 0)
  6769. +#define BCM2708_DMA_INT (1 << 2)
  6770. +#define BCM2708_DMA_ISPAUSED (1 << 4) /* Pause requested or not active */
  6771. +#define BCM2708_DMA_ISHELD (1 << 5) /* Is held by DREQ flow control */
  6772. +#define BCM2708_DMA_ERR (1 << 8)
  6773. +#define BCM2708_DMA_ABORT (1 << 30) /* stop current CB, go to next, WO */
  6774. +#define BCM2708_DMA_RESET (1 << 31) /* WO, self clearing */
  6775. +
  6776. +/* DMA control block "info" field bits */
  6777. +#define BCM2708_DMA_INT_EN (1 << 0)
  6778. +#define BCM2708_DMA_TDMODE (1 << 1)
  6779. +#define BCM2708_DMA_WAIT_RESP (1 << 3)
  6780. +#define BCM2708_DMA_D_INC (1 << 4)
  6781. +#define BCM2708_DMA_D_WIDTH (1 << 5)
  6782. +#define BCM2708_DMA_D_DREQ (1 << 6)
  6783. +#define BCM2708_DMA_S_INC (1 << 8)
  6784. +#define BCM2708_DMA_S_WIDTH (1 << 9)
  6785. +#define BCM2708_DMA_S_DREQ (1 << 10)
  6786. +
  6787. +#define BCM2708_DMA_BURST(x) (((x)&0xf) << 12)
  6788. +#define BCM2708_DMA_PER_MAP(x) ((x) << 16)
  6789. +#define BCM2708_DMA_WAITS(x) (((x)&0x1f) << 21)
  6790. +
  6791. +#define BCM2708_DMA_DREQ_EMMC 11
  6792. +#define BCM2708_DMA_DREQ_SDHOST 13
  6793. +
  6794. +#define BCM2708_DMA_CS 0x00 /* Control and Status */
  6795. +#define BCM2708_DMA_ADDR 0x04
  6796. +/* the current control block appears in the following registers - read only */
  6797. +#define BCM2708_DMA_INFO 0x08
  6798. +#define BCM2708_DMA_SOURCE_AD 0x0c
  6799. +#define BCM2708_DMA_DEST_AD 0x10
  6800. +#define BCM2708_DMA_NEXTCB 0x1C
  6801. +#define BCM2708_DMA_DEBUG 0x20
  6802. +
  6803. +#define BCM2708_DMA4_CS (BCM2708_DMA_CHAN(4)+BCM2708_DMA_CS)
  6804. +#define BCM2708_DMA4_ADDR (BCM2708_DMA_CHAN(4)+BCM2708_DMA_ADDR)
  6805. +
  6806. +#define BCM2708_DMA_TDMODE_LEN(w, h) ((h) << 16 | (w))
  6807. +
  6808. +struct bcm2708_dma_cb {
  6809. + unsigned long info;
  6810. + unsigned long src;
  6811. + unsigned long dst;
  6812. + unsigned long length;
  6813. + unsigned long stride;
  6814. + unsigned long next;
  6815. + unsigned long pad[2];
  6816. +};
  6817. +struct scatterlist;
  6818. +
  6819. +extern int bcm_sg_suitable_for_dma(struct scatterlist *sg_ptr, int sg_len);
  6820. +extern void bcm_dma_start(void __iomem *dma_chan_base,
  6821. + dma_addr_t control_block);
  6822. +extern void bcm_dma_wait_idle(void __iomem *dma_chan_base);
  6823. +extern bool bcm_dma_is_busy(void __iomem *dma_chan_base);
  6824. +extern int /*rc*/ bcm_dma_abort(void __iomem *dma_chan_base);
  6825. +
  6826. +/* When listing features we can ask for when allocating DMA channels give
  6827. + those with higher priority smaller ordinal numbers */
  6828. +#define BCM_DMA_FEATURE_FAST_ORD 0
  6829. +#define BCM_DMA_FEATURE_BULK_ORD 1
  6830. +#define BCM_DMA_FEATURE_NORMAL_ORD 2
  6831. +#define BCM_DMA_FEATURE_LITE_ORD 3
  6832. +#define BCM_DMA_FEATURE_FAST (1<<BCM_DMA_FEATURE_FAST_ORD)
  6833. +#define BCM_DMA_FEATURE_BULK (1<<BCM_DMA_FEATURE_BULK_ORD)
  6834. +#define BCM_DMA_FEATURE_NORMAL (1<<BCM_DMA_FEATURE_NORMAL_ORD)
  6835. +#define BCM_DMA_FEATURE_LITE (1<<BCM_DMA_FEATURE_LITE_ORD)
  6836. +#define BCM_DMA_FEATURE_COUNT 4
  6837. +
  6838. +/* return channel no or -ve error */
  6839. +extern int bcm_dma_chan_alloc(unsigned preferred_feature_set,
  6840. + void __iomem **out_dma_base, int *out_dma_irq);
  6841. +extern int bcm_dma_chan_free(int channel);
  6842. +
  6843. +
  6844. +#endif /* _MACH_BCM2708_DMA_H */
  6845. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/entry-macro.S linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/entry-macro.S
  6846. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/entry-macro.S 1969-12-31 18:00:00.000000000 -0600
  6847. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/entry-macro.S 2014-12-03 19:13:32.448418001 -0600
  6848. @@ -0,0 +1,69 @@
  6849. +/*
  6850. + * arch/arm/mach-bcm2708/include/mach/entry-macro.S
  6851. + *
  6852. + * Low-level IRQ helper macros for BCM2708 platforms
  6853. + *
  6854. + * Copyright (C) 2010 Broadcom
  6855. + *
  6856. + * This program is free software; you can redistribute it and/or modify
  6857. + * it under the terms of the GNU General Public License as published by
  6858. + * the Free Software Foundation; either version 2 of the License, or
  6859. + * (at your option) any later version.
  6860. + *
  6861. + * This program is distributed in the hope that it will be useful,
  6862. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6863. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6864. + * GNU General Public License for more details.
  6865. + *
  6866. + * You should have received a copy of the GNU General Public License
  6867. + * along with this program; if not, write to the Free Software
  6868. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6869. + */
  6870. +#include <mach/hardware.h>
  6871. +
  6872. + .macro disable_fiq
  6873. + .endm
  6874. +
  6875. + .macro get_irqnr_preamble, base, tmp
  6876. + ldr \base, =IO_ADDRESS(ARMCTRL_IC_BASE)
  6877. + .endm
  6878. +
  6879. + .macro arch_ret_to_user, tmp1, tmp2
  6880. + .endm
  6881. +
  6882. + .macro get_irqnr_and_base, irqnr, irqstat, base, tmp
  6883. + /* get masked status */
  6884. + ldr \irqstat, [\base, #(ARM_IRQ_PEND0 - ARMCTRL_IC_BASE)]
  6885. + mov \irqnr, #(ARM_IRQ0_BASE + 31)
  6886. + and \tmp, \irqstat, #0x300 @ save bits 8 and 9
  6887. + /* clear bits 8 and 9, and test */
  6888. + bics \irqstat, \irqstat, #0x300
  6889. + bne 1010f
  6890. +
  6891. + tst \tmp, #0x100
  6892. + ldrne \irqstat, [\base, #(ARM_IRQ_PEND1 - ARMCTRL_IC_BASE)]
  6893. + movne \irqnr, #(ARM_IRQ1_BASE + 31)
  6894. + @ Mask out the interrupts also present in PEND0 - see SW-5809
  6895. + bicne \irqstat, #((1<<7) | (1<<9) | (1<<10))
  6896. + bicne \irqstat, #((1<<18) | (1<<19))
  6897. + bne 1010f
  6898. +
  6899. + tst \tmp, #0x200
  6900. + ldrne \irqstat, [\base, #(ARM_IRQ_PEND2 - ARMCTRL_IC_BASE)]
  6901. + movne \irqnr, #(ARM_IRQ2_BASE + 31)
  6902. + @ Mask out the interrupts also present in PEND0 - see SW-5809
  6903. + bicne \irqstat, #((1<<21) | (1<<22) | (1<<23) | (1<<24) | (1<<25))
  6904. + bicne \irqstat, #((1<<30))
  6905. + beq 1020f
  6906. +
  6907. +1010:
  6908. + @ For non-zero x, LSB(x) = 31 - CLZ(x^(x-1))
  6909. + @ N.B. CLZ is an ARM5 instruction.
  6910. + sub \tmp, \irqstat, #1
  6911. + eor \irqstat, \irqstat, \tmp
  6912. + clz \tmp, \irqstat
  6913. + sub \irqnr, \tmp
  6914. +
  6915. +1020: @ EQ will be set if no irqs pending
  6916. +
  6917. + .endm
  6918. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/frc.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/frc.h
  6919. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/frc.h 1969-12-31 18:00:00.000000000 -0600
  6920. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/frc.h 2014-12-03 19:13:32.448418001 -0600
  6921. @@ -0,0 +1,38 @@
  6922. +/*
  6923. + * arch/arm/mach-bcm2708/include/mach/timex.h
  6924. + *
  6925. + * BCM2708 free running counter (timer)
  6926. + *
  6927. + * Copyright (C) 2010 Broadcom
  6928. + *
  6929. + * This program is free software; you can redistribute it and/or modify
  6930. + * it under the terms of the GNU General Public License as published by
  6931. + * the Free Software Foundation; either version 2 of the License, or
  6932. + * (at your option) any later version.
  6933. + *
  6934. + * This program is distributed in the hope that it will be useful,
  6935. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6936. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6937. + * GNU General Public License for more details.
  6938. + *
  6939. + * You should have received a copy of the GNU General Public License
  6940. + * along with this program; if not, write to the Free Software
  6941. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6942. + */
  6943. +
  6944. +#ifndef _MACH_FRC_H
  6945. +#define _MACH_FRC_H
  6946. +
  6947. +#define FRC_TICK_RATE (1000000)
  6948. +
  6949. +/*! Free running counter incrementing at the CLOCK_TICK_RATE
  6950. + (slightly faster than frc_clock_ticks63()
  6951. + */
  6952. +extern unsigned long frc_clock_ticks32(void);
  6953. +
  6954. +/*! Free running counter incrementing at the CLOCK_TICK_RATE
  6955. + * Note - top bit should be ignored (see cnt32_to_63)
  6956. + */
  6957. +extern unsigned long long frc_clock_ticks63(void);
  6958. +
  6959. +#endif
  6960. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/gpio.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/gpio.h
  6961. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/gpio.h 1969-12-31 18:00:00.000000000 -0600
  6962. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/gpio.h 2014-12-03 19:13:32.448418001 -0600
  6963. @@ -0,0 +1,17 @@
  6964. +/*
  6965. + * arch/arm/mach-bcm2708/include/mach/gpio.h
  6966. + *
  6967. + * This file is licensed under the terms of the GNU General Public
  6968. + * License version 2. This program is licensed "as is" without any
  6969. + * warranty of any kind, whether express or implied.
  6970. + */
  6971. +
  6972. +#ifndef __ASM_ARCH_GPIO_H
  6973. +#define __ASM_ARCH_GPIO_H
  6974. +
  6975. +#define BCM2708_NR_GPIOS 54 // number of gpio lines
  6976. +
  6977. +#define gpio_to_irq(x) ((x) + GPIO_IRQ_START)
  6978. +#define irq_to_gpio(x) ((x) - GPIO_IRQ_START)
  6979. +
  6980. +#endif
  6981. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/hardware.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/hardware.h
  6982. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/hardware.h 1969-12-31 18:00:00.000000000 -0600
  6983. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/hardware.h 2014-12-03 19:13:32.448418001 -0600
  6984. @@ -0,0 +1,28 @@
  6985. +/*
  6986. + * arch/arm/mach-bcm2708/include/mach/hardware.h
  6987. + *
  6988. + * This file contains the hardware definitions of the BCM2708 devices.
  6989. + *
  6990. + * Copyright (C) 2010 Broadcom
  6991. + *
  6992. + * This program is free software; you can redistribute it and/or modify
  6993. + * it under the terms of the GNU General Public License as published by
  6994. + * the Free Software Foundation; either version 2 of the License, or
  6995. + * (at your option) any later version.
  6996. + *
  6997. + * This program is distributed in the hope that it will be useful,
  6998. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6999. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7000. + * GNU General Public License for more details.
  7001. + *
  7002. + * You should have received a copy of the GNU General Public License
  7003. + * along with this program; if not, write to the Free Software
  7004. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7005. + */
  7006. +#ifndef __ASM_ARCH_HARDWARE_H
  7007. +#define __ASM_ARCH_HARDWARE_H
  7008. +
  7009. +#include <asm/sizes.h>
  7010. +#include <mach/platform.h>
  7011. +
  7012. +#endif
  7013. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/io.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/io.h
  7014. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/io.h 1969-12-31 18:00:00.000000000 -0600
  7015. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/io.h 2014-12-03 19:13:32.448418001 -0600
  7016. @@ -0,0 +1,27 @@
  7017. +/*
  7018. + * arch/arm/mach-bcm2708/include/mach/io.h
  7019. + *
  7020. + * Copyright (C) 2003 ARM Limited
  7021. + *
  7022. + * This program is free software; you can redistribute it and/or modify
  7023. + * it under the terms of the GNU General Public License as published by
  7024. + * the Free Software Foundation; either version 2 of the License, or
  7025. + * (at your option) any later version.
  7026. + *
  7027. + * This program is distributed in the hope that it will be useful,
  7028. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7029. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7030. + * GNU General Public License for more details.
  7031. + *
  7032. + * You should have received a copy of the GNU General Public License
  7033. + * along with this program; if not, write to the Free Software
  7034. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7035. + */
  7036. +#ifndef __ASM_ARM_ARCH_IO_H
  7037. +#define __ASM_ARM_ARCH_IO_H
  7038. +
  7039. +#define IO_SPACE_LIMIT 0xffffffff
  7040. +
  7041. +#define __io(a) __typesafe_io(a)
  7042. +
  7043. +#endif
  7044. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/irqs.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/irqs.h
  7045. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/irqs.h 1969-12-31 18:00:00.000000000 -0600
  7046. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/irqs.h 2014-12-03 19:13:32.448418001 -0600
  7047. @@ -0,0 +1,197 @@
  7048. +/*
  7049. + * arch/arm/mach-bcm2708/include/mach/irqs.h
  7050. + *
  7051. + * Copyright (C) 2010 Broadcom
  7052. + * Copyright (C) 2003 ARM Limited
  7053. + * Copyright (C) 2000 Deep Blue Solutions Ltd.
  7054. + *
  7055. + * This program is free software; you can redistribute it and/or modify
  7056. + * it under the terms of the GNU General Public License as published by
  7057. + * the Free Software Foundation; either version 2 of the License, or
  7058. + * (at your option) any later version.
  7059. + *
  7060. + * This program is distributed in the hope that it will be useful,
  7061. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7062. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7063. + * GNU General Public License for more details.
  7064. + *
  7065. + * You should have received a copy of the GNU General Public License
  7066. + * along with this program; if not, write to the Free Software
  7067. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7068. + */
  7069. +
  7070. +#ifndef _BCM2708_IRQS_H_
  7071. +#define _BCM2708_IRQS_H_
  7072. +
  7073. +#include <mach/platform.h>
  7074. +
  7075. +/*
  7076. + * IRQ interrupts definitions are the same as the INT definitions
  7077. + * held within platform.h
  7078. + */
  7079. +#define IRQ_ARMCTRL_START 0
  7080. +#define IRQ_TIMER0 (IRQ_ARMCTRL_START + INTERRUPT_TIMER0)
  7081. +#define IRQ_TIMER1 (IRQ_ARMCTRL_START + INTERRUPT_TIMER1)
  7082. +#define IRQ_TIMER2 (IRQ_ARMCTRL_START + INTERRUPT_TIMER2)
  7083. +#define IRQ_TIMER3 (IRQ_ARMCTRL_START + INTERRUPT_TIMER3)
  7084. +#define IRQ_CODEC0 (IRQ_ARMCTRL_START + INTERRUPT_CODEC0)
  7085. +#define IRQ_CODEC1 (IRQ_ARMCTRL_START + INTERRUPT_CODEC1)
  7086. +#define IRQ_CODEC2 (IRQ_ARMCTRL_START + INTERRUPT_CODEC2)
  7087. +#define IRQ_JPEG (IRQ_ARMCTRL_START + INTERRUPT_JPEG)
  7088. +#define IRQ_ISP (IRQ_ARMCTRL_START + INTERRUPT_ISP)
  7089. +#define IRQ_USB (IRQ_ARMCTRL_START + INTERRUPT_USB)
  7090. +#define IRQ_3D (IRQ_ARMCTRL_START + INTERRUPT_3D)
  7091. +#define IRQ_TRANSPOSER (IRQ_ARMCTRL_START + INTERRUPT_TRANSPOSER)
  7092. +#define IRQ_MULTICORESYNC0 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC0)
  7093. +#define IRQ_MULTICORESYNC1 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC1)
  7094. +#define IRQ_MULTICORESYNC2 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC2)
  7095. +#define IRQ_MULTICORESYNC3 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC3)
  7096. +#define IRQ_DMA0 (IRQ_ARMCTRL_START + INTERRUPT_DMA0)
  7097. +#define IRQ_DMA1 (IRQ_ARMCTRL_START + INTERRUPT_DMA1)
  7098. +#define IRQ_DMA2 (IRQ_ARMCTRL_START + INTERRUPT_DMA2)
  7099. +#define IRQ_DMA3 (IRQ_ARMCTRL_START + INTERRUPT_DMA3)
  7100. +#define IRQ_DMA4 (IRQ_ARMCTRL_START + INTERRUPT_DMA4)
  7101. +#define IRQ_DMA5 (IRQ_ARMCTRL_START + INTERRUPT_DMA5)
  7102. +#define IRQ_DMA6 (IRQ_ARMCTRL_START + INTERRUPT_DMA6)
  7103. +#define IRQ_DMA7 (IRQ_ARMCTRL_START + INTERRUPT_DMA7)
  7104. +#define IRQ_DMA8 (IRQ_ARMCTRL_START + INTERRUPT_DMA8)
  7105. +#define IRQ_DMA9 (IRQ_ARMCTRL_START + INTERRUPT_DMA9)
  7106. +#define IRQ_DMA10 (IRQ_ARMCTRL_START + INTERRUPT_DMA10)
  7107. +#define IRQ_DMA11 (IRQ_ARMCTRL_START + INTERRUPT_DMA11)
  7108. +#define IRQ_DMA12 (IRQ_ARMCTRL_START + INTERRUPT_DMA12)
  7109. +#define IRQ_AUX (IRQ_ARMCTRL_START + INTERRUPT_AUX)
  7110. +#define IRQ_ARM (IRQ_ARMCTRL_START + INTERRUPT_ARM)
  7111. +#define IRQ_VPUDMA (IRQ_ARMCTRL_START + INTERRUPT_VPUDMA)
  7112. +#define IRQ_HOSTPORT (IRQ_ARMCTRL_START + INTERRUPT_HOSTPORT)
  7113. +#define IRQ_VIDEOSCALER (IRQ_ARMCTRL_START + INTERRUPT_VIDEOSCALER)
  7114. +#define IRQ_CCP2TX (IRQ_ARMCTRL_START + INTERRUPT_CCP2TX)
  7115. +#define IRQ_SDC (IRQ_ARMCTRL_START + INTERRUPT_SDC)
  7116. +#define IRQ_DSI0 (IRQ_ARMCTRL_START + INTERRUPT_DSI0)
  7117. +#define IRQ_AVE (IRQ_ARMCTRL_START + INTERRUPT_AVE)
  7118. +#define IRQ_CAM0 (IRQ_ARMCTRL_START + INTERRUPT_CAM0)
  7119. +#define IRQ_CAM1 (IRQ_ARMCTRL_START + INTERRUPT_CAM1)
  7120. +#define IRQ_HDMI0 (IRQ_ARMCTRL_START + INTERRUPT_HDMI0)
  7121. +#define IRQ_HDMI1 (IRQ_ARMCTRL_START + INTERRUPT_HDMI1)
  7122. +#define IRQ_PIXELVALVE1 (IRQ_ARMCTRL_START + INTERRUPT_PIXELVALVE1)
  7123. +#define IRQ_I2CSPISLV (IRQ_ARMCTRL_START + INTERRUPT_I2CSPISLV)
  7124. +#define IRQ_DSI1 (IRQ_ARMCTRL_START + INTERRUPT_DSI1)
  7125. +#define IRQ_PWA0 (IRQ_ARMCTRL_START + INTERRUPT_PWA0)
  7126. +#define IRQ_PWA1 (IRQ_ARMCTRL_START + INTERRUPT_PWA1)
  7127. +#define IRQ_CPR (IRQ_ARMCTRL_START + INTERRUPT_CPR)
  7128. +#define IRQ_SMI (IRQ_ARMCTRL_START + INTERRUPT_SMI)
  7129. +#define IRQ_GPIO0 (IRQ_ARMCTRL_START + INTERRUPT_GPIO0)
  7130. +#define IRQ_GPIO1 (IRQ_ARMCTRL_START + INTERRUPT_GPIO1)
  7131. +#define IRQ_GPIO2 (IRQ_ARMCTRL_START + INTERRUPT_GPIO2)
  7132. +#define IRQ_GPIO3 (IRQ_ARMCTRL_START + INTERRUPT_GPIO3)
  7133. +#define IRQ_I2C (IRQ_ARMCTRL_START + INTERRUPT_I2C)
  7134. +#define IRQ_SPI (IRQ_ARMCTRL_START + INTERRUPT_SPI)
  7135. +#define IRQ_I2SPCM (IRQ_ARMCTRL_START + INTERRUPT_I2SPCM)
  7136. +#define IRQ_SDIO (IRQ_ARMCTRL_START + INTERRUPT_SDIO)
  7137. +#define IRQ_UART (IRQ_ARMCTRL_START + INTERRUPT_UART)
  7138. +#define IRQ_SLIMBUS (IRQ_ARMCTRL_START + INTERRUPT_SLIMBUS)
  7139. +#define IRQ_VEC (IRQ_ARMCTRL_START + INTERRUPT_VEC)
  7140. +#define IRQ_CPG (IRQ_ARMCTRL_START + INTERRUPT_CPG)
  7141. +#define IRQ_RNG (IRQ_ARMCTRL_START + INTERRUPT_RNG)
  7142. +#define IRQ_ARASANSDIO (IRQ_ARMCTRL_START + INTERRUPT_ARASANSDIO)
  7143. +#define IRQ_AVSPMON (IRQ_ARMCTRL_START + INTERRUPT_AVSPMON)
  7144. +
  7145. +#define IRQ_ARM_TIMER (IRQ_ARMCTRL_START + INTERRUPT_ARM_TIMER)
  7146. +#define IRQ_ARM_MAILBOX (IRQ_ARMCTRL_START + INTERRUPT_ARM_MAILBOX)
  7147. +#define IRQ_ARM_DOORBELL_0 (IRQ_ARMCTRL_START + INTERRUPT_ARM_DOORBELL_0)
  7148. +#define IRQ_ARM_DOORBELL_1 (IRQ_ARMCTRL_START + INTERRUPT_ARM_DOORBELL_1)
  7149. +#define IRQ_VPU0_HALTED (IRQ_ARMCTRL_START + INTERRUPT_VPU0_HALTED)
  7150. +#define IRQ_VPU1_HALTED (IRQ_ARMCTRL_START + INTERRUPT_VPU1_HALTED)
  7151. +#define IRQ_ILLEGAL_TYPE0 (IRQ_ARMCTRL_START + INTERRUPT_ILLEGAL_TYPE0)
  7152. +#define IRQ_ILLEGAL_TYPE1 (IRQ_ARMCTRL_START + INTERRUPT_ILLEGAL_TYPE1)
  7153. +#define IRQ_PENDING1 (IRQ_ARMCTRL_START + INTERRUPT_PENDING1)
  7154. +#define IRQ_PENDING2 (IRQ_ARMCTRL_START + INTERRUPT_PENDING2)
  7155. +
  7156. +#define FIQ_START HARD_IRQS
  7157. +
  7158. +/*
  7159. + * FIQ interrupts definitions are the same as the INT definitions.
  7160. + */
  7161. +#define FIQ_TIMER0 (FIQ_START+INTERRUPT_TIMER0)
  7162. +#define FIQ_TIMER1 (FIQ_START+INTERRUPT_TIMER1)
  7163. +#define FIQ_TIMER2 (FIQ_START+INTERRUPT_TIMER2)
  7164. +#define FIQ_TIMER3 (FIQ_START+INTERRUPT_TIMER3)
  7165. +#define FIQ_CODEC0 (FIQ_START+INTERRUPT_CODEC0)
  7166. +#define FIQ_CODEC1 (FIQ_START+INTERRUPT_CODEC1)
  7167. +#define FIQ_CODEC2 (FIQ_START+INTERRUPT_CODEC2)
  7168. +#define FIQ_JPEG (FIQ_START+INTERRUPT_JPEG)
  7169. +#define FIQ_ISP (FIQ_START+INTERRUPT_ISP)
  7170. +#define FIQ_USB (FIQ_START+INTERRUPT_USB)
  7171. +#define FIQ_3D (FIQ_START+INTERRUPT_3D)
  7172. +#define FIQ_TRANSPOSER (FIQ_START+INTERRUPT_TRANSPOSER)
  7173. +#define FIQ_MULTICORESYNC0 (FIQ_START+INTERRUPT_MULTICORESYNC0)
  7174. +#define FIQ_MULTICORESYNC1 (FIQ_START+INTERRUPT_MULTICORESYNC1)
  7175. +#define FIQ_MULTICORESYNC2 (FIQ_START+INTERRUPT_MULTICORESYNC2)
  7176. +#define FIQ_MULTICORESYNC3 (FIQ_START+INTERRUPT_MULTICORESYNC3)
  7177. +#define FIQ_DMA0 (FIQ_START+INTERRUPT_DMA0)
  7178. +#define FIQ_DMA1 (FIQ_START+INTERRUPT_DMA1)
  7179. +#define FIQ_DMA2 (FIQ_START+INTERRUPT_DMA2)
  7180. +#define FIQ_DMA3 (FIQ_START+INTERRUPT_DMA3)
  7181. +#define FIQ_DMA4 (FIQ_START+INTERRUPT_DMA4)
  7182. +#define FIQ_DMA5 (FIQ_START+INTERRUPT_DMA5)
  7183. +#define FIQ_DMA6 (FIQ_START+INTERRUPT_DMA6)
  7184. +#define FIQ_DMA7 (FIQ_START+INTERRUPT_DMA7)
  7185. +#define FIQ_DMA8 (FIQ_START+INTERRUPT_DMA8)
  7186. +#define FIQ_DMA9 (FIQ_START+INTERRUPT_DMA9)
  7187. +#define FIQ_DMA10 (FIQ_START+INTERRUPT_DMA10)
  7188. +#define FIQ_DMA11 (FIQ_START+INTERRUPT_DMA11)
  7189. +#define FIQ_DMA12 (FIQ_START+INTERRUPT_DMA12)
  7190. +#define FIQ_AUX (FIQ_START+INTERRUPT_AUX)
  7191. +#define FIQ_ARM (FIQ_START+INTERRUPT_ARM)
  7192. +#define FIQ_VPUDMA (FIQ_START+INTERRUPT_VPUDMA)
  7193. +#define FIQ_HOSTPORT (FIQ_START+INTERRUPT_HOSTPORT)
  7194. +#define FIQ_VIDEOSCALER (FIQ_START+INTERRUPT_VIDEOSCALER)
  7195. +#define FIQ_CCP2TX (FIQ_START+INTERRUPT_CCP2TX)
  7196. +#define FIQ_SDC (FIQ_START+INTERRUPT_SDC)
  7197. +#define FIQ_DSI0 (FIQ_START+INTERRUPT_DSI0)
  7198. +#define FIQ_AVE (FIQ_START+INTERRUPT_AVE)
  7199. +#define FIQ_CAM0 (FIQ_START+INTERRUPT_CAM0)
  7200. +#define FIQ_CAM1 (FIQ_START+INTERRUPT_CAM1)
  7201. +#define FIQ_HDMI0 (FIQ_START+INTERRUPT_HDMI0)
  7202. +#define FIQ_HDMI1 (FIQ_START+INTERRUPT_HDMI1)
  7203. +#define FIQ_PIXELVALVE1 (FIQ_START+INTERRUPT_PIXELVALVE1)
  7204. +#define FIQ_I2CSPISLV (FIQ_START+INTERRUPT_I2CSPISLV)
  7205. +#define FIQ_DSI1 (FIQ_START+INTERRUPT_DSI1)
  7206. +#define FIQ_PWA0 (FIQ_START+INTERRUPT_PWA0)
  7207. +#define FIQ_PWA1 (FIQ_START+INTERRUPT_PWA1)
  7208. +#define FIQ_CPR (FIQ_START+INTERRUPT_CPR)
  7209. +#define FIQ_SMI (FIQ_START+INTERRUPT_SMI)
  7210. +#define FIQ_GPIO0 (FIQ_START+INTERRUPT_GPIO0)
  7211. +#define FIQ_GPIO1 (FIQ_START+INTERRUPT_GPIO1)
  7212. +#define FIQ_GPIO2 (FIQ_START+INTERRUPT_GPIO2)
  7213. +#define FIQ_GPIO3 (FIQ_START+INTERRUPT_GPIO3)
  7214. +#define FIQ_I2C (FIQ_START+INTERRUPT_I2C)
  7215. +#define FIQ_SPI (FIQ_START+INTERRUPT_SPI)
  7216. +#define FIQ_I2SPCM (FIQ_START+INTERRUPT_I2SPCM)
  7217. +#define FIQ_SDIO (FIQ_START+INTERRUPT_SDIO)
  7218. +#define FIQ_UART (FIQ_START+INTERRUPT_UART)
  7219. +#define FIQ_SLIMBUS (FIQ_START+INTERRUPT_SLIMBUS)
  7220. +#define FIQ_VEC (FIQ_START+INTERRUPT_VEC)
  7221. +#define FIQ_CPG (FIQ_START+INTERRUPT_CPG)
  7222. +#define FIQ_RNG (FIQ_START+INTERRUPT_RNG)
  7223. +#define FIQ_ARASANSDIO (FIQ_START+INTERRUPT_ARASANSDIO)
  7224. +#define FIQ_AVSPMON (FIQ_START+INTERRUPT_AVSPMON)
  7225. +
  7226. +#define FIQ_ARM_TIMER (FIQ_START+INTERRUPT_ARM_TIMER)
  7227. +#define FIQ_ARM_MAILBOX (FIQ_START+INTERRUPT_ARM_MAILBOX)
  7228. +#define FIQ_ARM_DOORBELL_0 (FIQ_START+INTERRUPT_ARM_DOORBELL_0)
  7229. +#define FIQ_ARM_DOORBELL_1 (FIQ_START+INTERRUPT_ARM_DOORBELL_1)
  7230. +#define FIQ_VPU0_HALTED (FIQ_START+INTERRUPT_VPU0_HALTED)
  7231. +#define FIQ_VPU1_HALTED (FIQ_START+INTERRUPT_VPU1_HALTED)
  7232. +#define FIQ_ILLEGAL_TYPE0 (FIQ_START+INTERRUPT_ILLEGAL_TYPE0)
  7233. +#define FIQ_ILLEGAL_TYPE1 (FIQ_START+INTERRUPT_ILLEGAL_TYPE1)
  7234. +#define FIQ_PENDING1 (FIQ_START+INTERRUPT_PENDING1)
  7235. +#define FIQ_PENDING2 (FIQ_START+INTERRUPT_PENDING2)
  7236. +
  7237. +#define HARD_IRQS (64 + 21)
  7238. +#define FIQ_IRQS (64 + 21)
  7239. +#define GPIO_IRQ_START (HARD_IRQS + FIQ_IRQS)
  7240. +#define GPIO_IRQS (32*5)
  7241. +#define SPARE_IRQS (64)
  7242. +#define NR_IRQS (HARD_IRQS+FIQ_IRQS+GPIO_IRQS+SPARE_IRQS)
  7243. +
  7244. +#endif /* _BCM2708_IRQS_H_ */
  7245. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/memory.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/memory.h
  7246. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/memory.h 1969-12-31 18:00:00.000000000 -0600
  7247. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/memory.h 2014-12-03 19:13:32.448418001 -0600
  7248. @@ -0,0 +1,57 @@
  7249. +/*
  7250. + * arch/arm/mach-bcm2708/include/mach/memory.h
  7251. + *
  7252. + * Copyright (C) 2010 Broadcom
  7253. + *
  7254. + * This program is free software; you can redistribute it and/or modify
  7255. + * it under the terms of the GNU General Public License as published by
  7256. + * the Free Software Foundation; either version 2 of the License, or
  7257. + * (at your option) any later version.
  7258. + *
  7259. + * This program is distributed in the hope that it will be useful,
  7260. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7261. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7262. + * GNU General Public License for more details.
  7263. + *
  7264. + * You should have received a copy of the GNU General Public License
  7265. + * along with this program; if not, write to the Free Software
  7266. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7267. + */
  7268. +#ifndef __ASM_ARCH_MEMORY_H
  7269. +#define __ASM_ARCH_MEMORY_H
  7270. +
  7271. +/* Memory overview:
  7272. +
  7273. + [ARMcore] <--virtual addr-->
  7274. + [ARMmmu] <--physical addr-->
  7275. + [GERTmap] <--bus add-->
  7276. + [VCperiph]
  7277. +
  7278. +*/
  7279. +
  7280. +/*
  7281. + * Physical DRAM offset.
  7282. + */
  7283. +#define PLAT_PHYS_OFFSET UL(0x00000000)
  7284. +#define VC_ARMMEM_OFFSET UL(0x00000000) /* offset in VC of ARM memory */
  7285. +
  7286. +#ifdef CONFIG_BCM2708_NOL2CACHE
  7287. + #define _REAL_BUS_OFFSET UL(0xC0000000) /* don't use L1 or L2 caches */
  7288. +#else
  7289. + #define _REAL_BUS_OFFSET UL(0x40000000) /* use L2 cache */
  7290. +#endif
  7291. +
  7292. +/* We're using the memory at 64M in the VideoCore for Linux - this adjustment
  7293. + * will provide the offset into this area as well as setting the bits that
  7294. + * stop the L1 and L2 cache from being used
  7295. + *
  7296. + * WARNING: this only works because the ARM is given memory at a fixed location
  7297. + * (ARMMEM_OFFSET)
  7298. + */
  7299. +#define BUS_OFFSET (VC_ARMMEM_OFFSET + _REAL_BUS_OFFSET)
  7300. +#define __virt_to_bus(x) ((x) + (BUS_OFFSET - PAGE_OFFSET))
  7301. +#define __bus_to_virt(x) ((x) - (BUS_OFFSET - PAGE_OFFSET))
  7302. +#define __pfn_to_bus(x) (__pfn_to_phys(x) + (BUS_OFFSET - PLAT_PHYS_OFFSET))
  7303. +#define __bus_to_pfn(x) __phys_to_pfn((x) - (BUS_OFFSET - PLAT_PHYS_OFFSET))
  7304. +
  7305. +#endif
  7306. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/platform.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/platform.h
  7307. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/platform.h 1969-12-31 18:00:00.000000000 -0600
  7308. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/platform.h 2014-12-03 19:13:32.448418001 -0600
  7309. @@ -0,0 +1,228 @@
  7310. +/*
  7311. + * arch/arm/mach-bcm2708/include/mach/platform.h
  7312. + *
  7313. + * Copyright (C) 2010 Broadcom
  7314. + *
  7315. + * This program is free software; you can redistribute it and/or modify
  7316. + * it under the terms of the GNU General Public License as published by
  7317. + * the Free Software Foundation; either version 2 of the License, or
  7318. + * (at your option) any later version.
  7319. + *
  7320. + * This program is distributed in the hope that it will be useful,
  7321. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7322. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7323. + * GNU General Public License for more details.
  7324. + *
  7325. + * You should have received a copy of the GNU General Public License
  7326. + * along with this program; if not, write to the Free Software
  7327. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7328. + */
  7329. +
  7330. +#ifndef _BCM2708_PLATFORM_H
  7331. +#define _BCM2708_PLATFORM_H
  7332. +
  7333. +
  7334. +/* macros to get at IO space when running virtually */
  7335. +#define IO_ADDRESS(x) (((x) & 0x0fffffff) + (((x) >> 4) & 0x0f000000) + 0xf0000000)
  7336. +
  7337. +#define __io_address(n) IOMEM(IO_ADDRESS(n))
  7338. +
  7339. +
  7340. +/*
  7341. + * SDRAM
  7342. + */
  7343. +#define BCM2708_SDRAM_BASE 0x00000000
  7344. +
  7345. +/*
  7346. + * Logic expansion modules
  7347. + *
  7348. + */
  7349. +
  7350. +
  7351. +/* ------------------------------------------------------------------------
  7352. + * BCM2708 ARMCTRL Registers
  7353. + * ------------------------------------------------------------------------
  7354. + */
  7355. +
  7356. +#define HW_REGISTER_RW(addr) (addr)
  7357. +#define HW_REGISTER_RO(addr) (addr)
  7358. +
  7359. +#include "arm_control.h"
  7360. +#undef ARM_BASE
  7361. +
  7362. +/*
  7363. + * Definitions and addresses for the ARM CONTROL logic
  7364. + * This file is manually generated.
  7365. + */
  7366. +
  7367. +#define BCM2708_PERI_BASE 0x20000000
  7368. +#define IC0_BASE (BCM2708_PERI_BASE + 0x2000)
  7369. +#define ST_BASE (BCM2708_PERI_BASE + 0x3000) /* System Timer */
  7370. +#define MPHI_BASE (BCM2708_PERI_BASE + 0x6000) /* Message -based Parallel Host Interface */
  7371. +#define DMA_BASE (BCM2708_PERI_BASE + 0x7000) /* DMA controller */
  7372. +#define ARM_BASE (BCM2708_PERI_BASE + 0xB000) /* BCM2708 ARM control block */
  7373. +#define PM_BASE (BCM2708_PERI_BASE + 0x100000) /* Power Management, Reset controller and Watchdog registers */
  7374. +#define PCM_CLOCK_BASE (BCM2708_PERI_BASE + 0x101098) /* PCM Clock */
  7375. +#define RNG_BASE (BCM2708_PERI_BASE + 0x104000) /* Hardware RNG */
  7376. +#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO */
  7377. +#define UART0_BASE (BCM2708_PERI_BASE + 0x201000) /* Uart 0 */
  7378. +#define MMCI0_BASE (BCM2708_PERI_BASE + 0x202000) /* MMC interface */
  7379. +#define I2S_BASE (BCM2708_PERI_BASE + 0x203000) /* I2S */
  7380. +#define SPI0_BASE (BCM2708_PERI_BASE + 0x204000) /* SPI0 */
  7381. +#define BSC0_BASE (BCM2708_PERI_BASE + 0x205000) /* BSC0 I2C/TWI */
  7382. +#define UART1_BASE (BCM2708_PERI_BASE + 0x215000) /* Uart 1 */
  7383. +#define EMMC_BASE (BCM2708_PERI_BASE + 0x300000) /* eMMC interface */
  7384. +#define SMI_BASE (BCM2708_PERI_BASE + 0x600000) /* SMI */
  7385. +#define BSC1_BASE (BCM2708_PERI_BASE + 0x804000) /* BSC1 I2C/TWI */
  7386. +#define USB_BASE (BCM2708_PERI_BASE + 0x980000) /* DTC_OTG USB controller */
  7387. +#define MCORE_BASE (BCM2708_PERI_BASE + 0x0000) /* Fake frame buffer device (actually the multicore sync block*/
  7388. +
  7389. +#define ARMCTRL_BASE (ARM_BASE + 0x000)
  7390. +#define ARMCTRL_IC_BASE (ARM_BASE + 0x200) /* ARM interrupt controller */
  7391. +#define ARMCTRL_TIMER0_1_BASE (ARM_BASE + 0x400) /* Timer 0 and 1 */
  7392. +#define ARMCTRL_0_SBM_BASE (ARM_BASE + 0x800) /* User 0 (ARM)'s Semaphores Doorbells and Mailboxes */
  7393. +
  7394. +
  7395. +/*
  7396. + * Interrupt assignments
  7397. + */
  7398. +
  7399. +#define ARM_IRQ1_BASE 0
  7400. +#define INTERRUPT_TIMER0 (ARM_IRQ1_BASE + 0)
  7401. +#define INTERRUPT_TIMER1 (ARM_IRQ1_BASE + 1)
  7402. +#define INTERRUPT_TIMER2 (ARM_IRQ1_BASE + 2)
  7403. +#define INTERRUPT_TIMER3 (ARM_IRQ1_BASE + 3)
  7404. +#define INTERRUPT_CODEC0 (ARM_IRQ1_BASE + 4)
  7405. +#define INTERRUPT_CODEC1 (ARM_IRQ1_BASE + 5)
  7406. +#define INTERRUPT_CODEC2 (ARM_IRQ1_BASE + 6)
  7407. +#define INTERRUPT_VC_JPEG (ARM_IRQ1_BASE + 7)
  7408. +#define INTERRUPT_ISP (ARM_IRQ1_BASE + 8)
  7409. +#define INTERRUPT_VC_USB (ARM_IRQ1_BASE + 9)
  7410. +#define INTERRUPT_VC_3D (ARM_IRQ1_BASE + 10)
  7411. +#define INTERRUPT_TRANSPOSER (ARM_IRQ1_BASE + 11)
  7412. +#define INTERRUPT_MULTICORESYNC0 (ARM_IRQ1_BASE + 12)
  7413. +#define INTERRUPT_MULTICORESYNC1 (ARM_IRQ1_BASE + 13)
  7414. +#define INTERRUPT_MULTICORESYNC2 (ARM_IRQ1_BASE + 14)
  7415. +#define INTERRUPT_MULTICORESYNC3 (ARM_IRQ1_BASE + 15)
  7416. +#define INTERRUPT_DMA0 (ARM_IRQ1_BASE + 16)
  7417. +#define INTERRUPT_DMA1 (ARM_IRQ1_BASE + 17)
  7418. +#define INTERRUPT_VC_DMA2 (ARM_IRQ1_BASE + 18)
  7419. +#define INTERRUPT_VC_DMA3 (ARM_IRQ1_BASE + 19)
  7420. +#define INTERRUPT_DMA4 (ARM_IRQ1_BASE + 20)
  7421. +#define INTERRUPT_DMA5 (ARM_IRQ1_BASE + 21)
  7422. +#define INTERRUPT_DMA6 (ARM_IRQ1_BASE + 22)
  7423. +#define INTERRUPT_DMA7 (ARM_IRQ1_BASE + 23)
  7424. +#define INTERRUPT_DMA8 (ARM_IRQ1_BASE + 24)
  7425. +#define INTERRUPT_DMA9 (ARM_IRQ1_BASE + 25)
  7426. +#define INTERRUPT_DMA10 (ARM_IRQ1_BASE + 26)
  7427. +#define INTERRUPT_DMA11 (ARM_IRQ1_BASE + 27)
  7428. +#define INTERRUPT_DMA12 (ARM_IRQ1_BASE + 28)
  7429. +#define INTERRUPT_AUX (ARM_IRQ1_BASE + 29)
  7430. +#define INTERRUPT_ARM (ARM_IRQ1_BASE + 30)
  7431. +#define INTERRUPT_VPUDMA (ARM_IRQ1_BASE + 31)
  7432. +
  7433. +#define ARM_IRQ2_BASE 32
  7434. +#define INTERRUPT_HOSTPORT (ARM_IRQ2_BASE + 0)
  7435. +#define INTERRUPT_VIDEOSCALER (ARM_IRQ2_BASE + 1)
  7436. +#define INTERRUPT_CCP2TX (ARM_IRQ2_BASE + 2)
  7437. +#define INTERRUPT_SDC (ARM_IRQ2_BASE + 3)
  7438. +#define INTERRUPT_DSI0 (ARM_IRQ2_BASE + 4)
  7439. +#define INTERRUPT_AVE (ARM_IRQ2_BASE + 5)
  7440. +#define INTERRUPT_CAM0 (ARM_IRQ2_BASE + 6)
  7441. +#define INTERRUPT_CAM1 (ARM_IRQ2_BASE + 7)
  7442. +#define INTERRUPT_HDMI0 (ARM_IRQ2_BASE + 8)
  7443. +#define INTERRUPT_HDMI1 (ARM_IRQ2_BASE + 9)
  7444. +#define INTERRUPT_PIXELVALVE1 (ARM_IRQ2_BASE + 10)
  7445. +#define INTERRUPT_I2CSPISLV (ARM_IRQ2_BASE + 11)
  7446. +#define INTERRUPT_DSI1 (ARM_IRQ2_BASE + 12)
  7447. +#define INTERRUPT_PWA0 (ARM_IRQ2_BASE + 13)
  7448. +#define INTERRUPT_PWA1 (ARM_IRQ2_BASE + 14)
  7449. +#define INTERRUPT_CPR (ARM_IRQ2_BASE + 15)
  7450. +#define INTERRUPT_SMI (ARM_IRQ2_BASE + 16)
  7451. +#define INTERRUPT_GPIO0 (ARM_IRQ2_BASE + 17)
  7452. +#define INTERRUPT_GPIO1 (ARM_IRQ2_BASE + 18)
  7453. +#define INTERRUPT_GPIO2 (ARM_IRQ2_BASE + 19)
  7454. +#define INTERRUPT_GPIO3 (ARM_IRQ2_BASE + 20)
  7455. +#define INTERRUPT_VC_I2C (ARM_IRQ2_BASE + 21)
  7456. +#define INTERRUPT_VC_SPI (ARM_IRQ2_BASE + 22)
  7457. +#define INTERRUPT_VC_I2SPCM (ARM_IRQ2_BASE + 23)
  7458. +#define INTERRUPT_VC_SDIO (ARM_IRQ2_BASE + 24)
  7459. +#define INTERRUPT_VC_UART (ARM_IRQ2_BASE + 25)
  7460. +#define INTERRUPT_SLIMBUS (ARM_IRQ2_BASE + 26)
  7461. +#define INTERRUPT_VEC (ARM_IRQ2_BASE + 27)
  7462. +#define INTERRUPT_CPG (ARM_IRQ2_BASE + 28)
  7463. +#define INTERRUPT_RNG (ARM_IRQ2_BASE + 29)
  7464. +#define INTERRUPT_VC_ARASANSDIO (ARM_IRQ2_BASE + 30)
  7465. +#define INTERRUPT_AVSPMON (ARM_IRQ2_BASE + 31)
  7466. +
  7467. +#define ARM_IRQ0_BASE 64
  7468. +#define INTERRUPT_ARM_TIMER (ARM_IRQ0_BASE + 0)
  7469. +#define INTERRUPT_ARM_MAILBOX (ARM_IRQ0_BASE + 1)
  7470. +#define INTERRUPT_ARM_DOORBELL_0 (ARM_IRQ0_BASE + 2)
  7471. +#define INTERRUPT_ARM_DOORBELL_1 (ARM_IRQ0_BASE + 3)
  7472. +#define INTERRUPT_VPU0_HALTED (ARM_IRQ0_BASE + 4)
  7473. +#define INTERRUPT_VPU1_HALTED (ARM_IRQ0_BASE + 5)
  7474. +#define INTERRUPT_ILLEGAL_TYPE0 (ARM_IRQ0_BASE + 6)
  7475. +#define INTERRUPT_ILLEGAL_TYPE1 (ARM_IRQ0_BASE + 7)
  7476. +#define INTERRUPT_PENDING1 (ARM_IRQ0_BASE + 8)
  7477. +#define INTERRUPT_PENDING2 (ARM_IRQ0_BASE + 9)
  7478. +#define INTERRUPT_JPEG (ARM_IRQ0_BASE + 10)
  7479. +#define INTERRUPT_USB (ARM_IRQ0_BASE + 11)
  7480. +#define INTERRUPT_3D (ARM_IRQ0_BASE + 12)
  7481. +#define INTERRUPT_DMA2 (ARM_IRQ0_BASE + 13)
  7482. +#define INTERRUPT_DMA3 (ARM_IRQ0_BASE + 14)
  7483. +#define INTERRUPT_I2C (ARM_IRQ0_BASE + 15)
  7484. +#define INTERRUPT_SPI (ARM_IRQ0_BASE + 16)
  7485. +#define INTERRUPT_I2SPCM (ARM_IRQ0_BASE + 17)
  7486. +#define INTERRUPT_SDIO (ARM_IRQ0_BASE + 18)
  7487. +#define INTERRUPT_UART (ARM_IRQ0_BASE + 19)
  7488. +#define INTERRUPT_ARASANSDIO (ARM_IRQ0_BASE + 20)
  7489. +
  7490. +#define MAXIRQNUM (32 + 32 + 20)
  7491. +#define MAXFIQNUM (32 + 32 + 20)
  7492. +
  7493. +#define MAX_TIMER 2
  7494. +#define MAX_PERIOD 699050
  7495. +#define TICKS_PER_uSEC 1
  7496. +
  7497. +/*
  7498. + * These are useconds NOT ticks.
  7499. + *
  7500. + */
  7501. +#define mSEC_1 1000
  7502. +#define mSEC_5 (mSEC_1 * 5)
  7503. +#define mSEC_10 (mSEC_1 * 10)
  7504. +#define mSEC_25 (mSEC_1 * 25)
  7505. +#define SEC_1 (mSEC_1 * 1000)
  7506. +
  7507. +/*
  7508. + * Watchdog
  7509. + */
  7510. +#define PM_RSTC (PM_BASE+0x1c)
  7511. +#define PM_RSTS (PM_BASE+0x20)
  7512. +#define PM_WDOG (PM_BASE+0x24)
  7513. +
  7514. +#define PM_WDOG_RESET 0000000000
  7515. +#define PM_PASSWORD 0x5a000000
  7516. +#define PM_WDOG_TIME_SET 0x000fffff
  7517. +#define PM_RSTC_WRCFG_CLR 0xffffffcf
  7518. +#define PM_RSTC_WRCFG_SET 0x00000030
  7519. +#define PM_RSTC_WRCFG_FULL_RESET 0x00000020
  7520. +#define PM_RSTC_RESET 0x00000102
  7521. +
  7522. +#define PM_RSTS_HADPOR_SET 0x00001000
  7523. +#define PM_RSTS_HADSRH_SET 0x00000400
  7524. +#define PM_RSTS_HADSRF_SET 0x00000200
  7525. +#define PM_RSTS_HADSRQ_SET 0x00000100
  7526. +#define PM_RSTS_HADWRH_SET 0x00000040
  7527. +#define PM_RSTS_HADWRF_SET 0x00000020
  7528. +#define PM_RSTS_HADWRQ_SET 0x00000010
  7529. +#define PM_RSTS_HADDRH_SET 0x00000004
  7530. +#define PM_RSTS_HADDRF_SET 0x00000002
  7531. +#define PM_RSTS_HADDRQ_SET 0x00000001
  7532. +
  7533. +#define UART0_CLOCK 3000000
  7534. +
  7535. +#endif
  7536. +
  7537. +/* END */
  7538. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/power.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/power.h
  7539. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/power.h 1969-12-31 18:00:00.000000000 -0600
  7540. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/power.h 2014-12-03 19:13:32.448418001 -0600
  7541. @@ -0,0 +1,26 @@
  7542. +/*
  7543. + * linux/arch/arm/mach-bcm2708/power.h
  7544. + *
  7545. + * Copyright (C) 2010 Broadcom
  7546. + *
  7547. + * This program is free software; you can redistribute it and/or modify
  7548. + * it under the terms of the GNU General Public License version 2 as
  7549. + * published by the Free Software Foundation.
  7550. + *
  7551. + * This device provides a shared mechanism for controlling the power to
  7552. + * VideoCore subsystems.
  7553. + */
  7554. +
  7555. +#ifndef _MACH_BCM2708_POWER_H
  7556. +#define _MACH_BCM2708_POWER_H
  7557. +
  7558. +#include <linux/types.h>
  7559. +#include <mach/arm_power.h>
  7560. +
  7561. +typedef unsigned int BCM_POWER_HANDLE_T;
  7562. +
  7563. +extern int bcm_power_open(BCM_POWER_HANDLE_T *handle);
  7564. +extern int bcm_power_request(BCM_POWER_HANDLE_T handle, uint32_t request);
  7565. +extern int bcm_power_close(BCM_POWER_HANDLE_T handle);
  7566. +
  7567. +#endif
  7568. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/system.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/system.h
  7569. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/system.h 1969-12-31 18:00:00.000000000 -0600
  7570. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/system.h 2014-12-03 19:13:32.448418001 -0600
  7571. @@ -0,0 +1,38 @@
  7572. +/*
  7573. + * arch/arm/mach-bcm2708/include/mach/system.h
  7574. + *
  7575. + * Copyright (C) 2010 Broadcom
  7576. + * Copyright (C) 2003 ARM Limited
  7577. + * Copyright (C) 2000 Deep Blue Solutions Ltd
  7578. + *
  7579. + * This program is free software; you can redistribute it and/or modify
  7580. + * it under the terms of the GNU General Public License as published by
  7581. + * the Free Software Foundation; either version 2 of the License, or
  7582. + * (at your option) any later version.
  7583. + *
  7584. + * This program is distributed in the hope that it will be useful,
  7585. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7586. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7587. + * GNU General Public License for more details.
  7588. + *
  7589. + * You should have received a copy of the GNU General Public License
  7590. + * along with this program; if not, write to the Free Software
  7591. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7592. + */
  7593. +#ifndef __ASM_ARCH_SYSTEM_H
  7594. +#define __ASM_ARCH_SYSTEM_H
  7595. +
  7596. +#include <linux/io.h>
  7597. +#include <mach/hardware.h>
  7598. +#include <mach/platform.h>
  7599. +
  7600. +static inline void arch_idle(void)
  7601. +{
  7602. + /*
  7603. + * This should do all the clock switching
  7604. + * and wait for interrupt tricks
  7605. + */
  7606. + cpu_do_idle();
  7607. +}
  7608. +
  7609. +#endif
  7610. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/timex.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/timex.h
  7611. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/timex.h 1969-12-31 18:00:00.000000000 -0600
  7612. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/timex.h 2014-12-03 19:13:32.448418001 -0600
  7613. @@ -0,0 +1,23 @@
  7614. +/*
  7615. + * arch/arm/mach-bcm2708/include/mach/timex.h
  7616. + *
  7617. + * BCM2708 sysem clock frequency
  7618. + *
  7619. + * Copyright (C) 2010 Broadcom
  7620. + *
  7621. + * This program is free software; you can redistribute it and/or modify
  7622. + * it under the terms of the GNU General Public License as published by
  7623. + * the Free Software Foundation; either version 2 of the License, or
  7624. + * (at your option) any later version.
  7625. + *
  7626. + * This program is distributed in the hope that it will be useful,
  7627. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7628. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7629. + * GNU General Public License for more details.
  7630. + *
  7631. + * You should have received a copy of the GNU General Public License
  7632. + * along with this program; if not, write to the Free Software
  7633. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7634. + */
  7635. +
  7636. +#define CLOCK_TICK_RATE (1000000)
  7637. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/uncompress.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/uncompress.h
  7638. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/uncompress.h 1969-12-31 18:00:00.000000000 -0600
  7639. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/uncompress.h 2014-12-03 19:13:32.448418001 -0600
  7640. @@ -0,0 +1,84 @@
  7641. +/*
  7642. + * arch/arm/mach-bcn2708/include/mach/uncompress.h
  7643. + *
  7644. + * Copyright (C) 2010 Broadcom
  7645. + * Copyright (C) 2003 ARM Limited
  7646. + *
  7647. + * This program is free software; you can redistribute it and/or modify
  7648. + * it under the terms of the GNU General Public License as published by
  7649. + * the Free Software Foundation; either version 2 of the License, or
  7650. + * (at your option) any later version.
  7651. + *
  7652. + * This program is distributed in the hope that it will be useful,
  7653. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7654. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7655. + * GNU General Public License for more details.
  7656. + *
  7657. + * You should have received a copy of the GNU General Public License
  7658. + * along with this program; if not, write to the Free Software
  7659. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7660. + */
  7661. +
  7662. +#include <linux/io.h>
  7663. +#include <linux/amba/serial.h>
  7664. +#include <mach/hardware.h>
  7665. +
  7666. +#define UART_BAUD 115200
  7667. +
  7668. +#define BCM2708_UART_DR __io(UART0_BASE + UART01x_DR)
  7669. +#define BCM2708_UART_FR __io(UART0_BASE + UART01x_FR)
  7670. +#define BCM2708_UART_IBRD __io(UART0_BASE + UART011_IBRD)
  7671. +#define BCM2708_UART_FBRD __io(UART0_BASE + UART011_FBRD)
  7672. +#define BCM2708_UART_LCRH __io(UART0_BASE + UART011_LCRH)
  7673. +#define BCM2708_UART_CR __io(UART0_BASE + UART011_CR)
  7674. +
  7675. +/*
  7676. + * This does not append a newline
  7677. + */
  7678. +static inline void putc(int c)
  7679. +{
  7680. + while (__raw_readl(BCM2708_UART_FR) & UART01x_FR_TXFF)
  7681. + barrier();
  7682. +
  7683. + __raw_writel(c, BCM2708_UART_DR);
  7684. +}
  7685. +
  7686. +static inline void flush(void)
  7687. +{
  7688. + int fr;
  7689. +
  7690. + do {
  7691. + fr = __raw_readl(BCM2708_UART_FR);
  7692. + barrier();
  7693. + } while ((fr & (UART011_FR_TXFE | UART01x_FR_BUSY)) != UART011_FR_TXFE);
  7694. +}
  7695. +
  7696. +static inline void arch_decomp_setup(void)
  7697. +{
  7698. + int temp, div, rem, frac;
  7699. +
  7700. + temp = 16 * UART_BAUD;
  7701. + div = UART0_CLOCK / temp;
  7702. + rem = UART0_CLOCK % temp;
  7703. + temp = (8 * rem) / UART_BAUD;
  7704. + frac = (temp >> 1) + (temp & 1);
  7705. +
  7706. + /* Make sure the UART is disabled before we start */
  7707. + __raw_writel(0, BCM2708_UART_CR);
  7708. +
  7709. + /* Set the baud rate */
  7710. + __raw_writel(div, BCM2708_UART_IBRD);
  7711. + __raw_writel(frac, BCM2708_UART_FBRD);
  7712. +
  7713. + /* Set the UART to 8n1, FIFO enabled */
  7714. + __raw_writel(UART01x_LCRH_WLEN_8 | UART01x_LCRH_FEN, BCM2708_UART_LCRH);
  7715. +
  7716. + /* Enable the UART */
  7717. + __raw_writel(UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_RXE,
  7718. + BCM2708_UART_CR);
  7719. +}
  7720. +
  7721. +/*
  7722. + * nothing to do
  7723. + */
  7724. +#define arch_decomp_wdog()
  7725. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/vcio.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/vcio.h
  7726. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/vcio.h 1969-12-31 18:00:00.000000000 -0600
  7727. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/vcio.h 2014-12-03 19:13:32.448418001 -0600
  7728. @@ -0,0 +1,165 @@
  7729. +/*
  7730. + * arch/arm/mach-bcm2708/include/mach/vcio.h
  7731. + *
  7732. + * Copyright (C) 2010 Broadcom
  7733. + *
  7734. + * This program is free software; you can redistribute it and/or modify
  7735. + * it under the terms of the GNU General Public License as published by
  7736. + * the Free Software Foundation; either version 2 of the License, or
  7737. + * (at your option) any later version.
  7738. + *
  7739. + * This program is distributed in the hope that it will be useful,
  7740. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7741. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7742. + * GNU General Public License for more details.
  7743. + *
  7744. + * You should have received a copy of the GNU General Public License
  7745. + * along with this program; if not, write to the Free Software
  7746. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7747. + */
  7748. +#ifndef _MACH_BCM2708_VCIO_H
  7749. +#define _MACH_BCM2708_VCIO_H
  7750. +
  7751. +/* Routines to handle I/O via the VideoCore "ARM control" registers
  7752. + * (semaphores, doorbells, mailboxes)
  7753. + */
  7754. +
  7755. +#define BCM_VCIO_DRIVER_NAME "bcm2708_vcio"
  7756. +
  7757. +/* Constants shared with the ARM identifying separate mailbox channels */
  7758. +#define MBOX_CHAN_POWER 0 /* for use by the power management interface */
  7759. +#define MBOX_CHAN_FB 1 /* for use by the frame buffer */
  7760. +#define MBOX_CHAN_VCHIQ 3 /* for use by the VCHIQ interface */
  7761. +#define MBOX_CHAN_PROPERTY 8 /* for use by the property channel */
  7762. +#define MBOX_CHAN_COUNT 9
  7763. +
  7764. +enum {
  7765. + VCMSG_PROCESS_REQUEST = 0x00000000
  7766. +};
  7767. +enum {
  7768. + VCMSG_REQUEST_SUCCESSFUL = 0x80000000,
  7769. + VCMSG_REQUEST_FAILED = 0x80000001
  7770. +};
  7771. +/* Mailbox property tags */
  7772. +enum {
  7773. + VCMSG_PROPERTY_END = 0x00000000,
  7774. + VCMSG_GET_FIRMWARE_REVISION = 0x00000001,
  7775. + VCMSG_GET_BOARD_MODEL = 0x00010001,
  7776. + VCMSG_GET_BOARD_REVISION = 0x00010002,
  7777. + VCMSG_GET_BOARD_MAC_ADDRESS = 0x00010003,
  7778. + VCMSG_GET_BOARD_SERIAL = 0x00010004,
  7779. + VCMSG_GET_ARM_MEMORY = 0x00010005,
  7780. + VCMSG_GET_VC_MEMORY = 0x00010006,
  7781. + VCMSG_GET_CLOCKS = 0x00010007,
  7782. + VCMSG_GET_COMMAND_LINE = 0x00050001,
  7783. + VCMSG_GET_DMA_CHANNELS = 0x00060001,
  7784. + VCMSG_GET_POWER_STATE = 0x00020001,
  7785. + VCMSG_GET_TIMING = 0x00020002,
  7786. + VCMSG_SET_POWER_STATE = 0x00028001,
  7787. + VCMSG_GET_CLOCK_STATE = 0x00030001,
  7788. + VCMSG_SET_CLOCK_STATE = 0x00038001,
  7789. + VCMSG_GET_CLOCK_RATE = 0x00030002,
  7790. + VCMSG_SET_CLOCK_RATE = 0x00038002,
  7791. + VCMSG_GET_VOLTAGE = 0x00030003,
  7792. + VCMSG_SET_VOLTAGE = 0x00038003,
  7793. + VCMSG_GET_MAX_CLOCK = 0x00030004,
  7794. + VCMSG_GET_MAX_VOLTAGE = 0x00030005,
  7795. + VCMSG_GET_TEMPERATURE = 0x00030006,
  7796. + VCMSG_GET_MIN_CLOCK = 0x00030007,
  7797. + VCMSG_GET_MIN_VOLTAGE = 0x00030008,
  7798. + VCMSG_GET_TURBO = 0x00030009,
  7799. + VCMSG_GET_MAX_TEMPERATURE = 0x0003000a,
  7800. + VCMSG_GET_STC = 0x0003000b,
  7801. + VCMSG_SET_TURBO = 0x00038009,
  7802. + VCMSG_SET_ALLOCATE_MEM = 0x0003000c,
  7803. + VCMSG_SET_LOCK_MEM = 0x0003000d,
  7804. + VCMSG_SET_UNLOCK_MEM = 0x0003000e,
  7805. + VCMSG_SET_RELEASE_MEM = 0x0003000f,
  7806. + VCMSG_SET_EXECUTE_CODE = 0x00030010,
  7807. + VCMSG_SET_EXECUTE_QPU = 0x00030011,
  7808. + VCMSG_SET_ENABLE_QPU = 0x00030012,
  7809. + VCMSG_GET_RESOURCE_HANDLE = 0x00030014,
  7810. + VCMSG_GET_EDID_BLOCK = 0x00030020,
  7811. + VCMSG_GET_CUSTOMER_OTP = 0x00030021,
  7812. + VCMSG_SET_CUSTOMER_OTP = 0x00038021,
  7813. + VCMSG_SET_ALLOCATE_BUFFER = 0x00040001,
  7814. + VCMSG_SET_RELEASE_BUFFER = 0x00048001,
  7815. + VCMSG_SET_BLANK_SCREEN = 0x00040002,
  7816. + VCMSG_TST_BLANK_SCREEN = 0x00044002,
  7817. + VCMSG_GET_PHYSICAL_WIDTH_HEIGHT = 0x00040003,
  7818. + VCMSG_TST_PHYSICAL_WIDTH_HEIGHT = 0x00044003,
  7819. + VCMSG_SET_PHYSICAL_WIDTH_HEIGHT = 0x00048003,
  7820. + VCMSG_GET_VIRTUAL_WIDTH_HEIGHT = 0x00040004,
  7821. + VCMSG_TST_VIRTUAL_WIDTH_HEIGHT = 0x00044004,
  7822. + VCMSG_SET_VIRTUAL_WIDTH_HEIGHT = 0x00048004,
  7823. + VCMSG_GET_DEPTH = 0x00040005,
  7824. + VCMSG_TST_DEPTH = 0x00044005,
  7825. + VCMSG_SET_DEPTH = 0x00048005,
  7826. + VCMSG_GET_PIXEL_ORDER = 0x00040006,
  7827. + VCMSG_TST_PIXEL_ORDER = 0x00044006,
  7828. + VCMSG_SET_PIXEL_ORDER = 0x00048006,
  7829. + VCMSG_GET_ALPHA_MODE = 0x00040007,
  7830. + VCMSG_TST_ALPHA_MODE = 0x00044007,
  7831. + VCMSG_SET_ALPHA_MODE = 0x00048007,
  7832. + VCMSG_GET_PITCH = 0x00040008,
  7833. + VCMSG_TST_PITCH = 0x00044008,
  7834. + VCMSG_SET_PITCH = 0x00048008,
  7835. + VCMSG_GET_VIRTUAL_OFFSET = 0x00040009,
  7836. + VCMSG_TST_VIRTUAL_OFFSET = 0x00044009,
  7837. + VCMSG_SET_VIRTUAL_OFFSET = 0x00048009,
  7838. + VCMSG_GET_OVERSCAN = 0x0004000a,
  7839. + VCMSG_TST_OVERSCAN = 0x0004400a,
  7840. + VCMSG_SET_OVERSCAN = 0x0004800a,
  7841. + VCMSG_GET_PALETTE = 0x0004000b,
  7842. + VCMSG_TST_PALETTE = 0x0004400b,
  7843. + VCMSG_SET_PALETTE = 0x0004800b,
  7844. + VCMSG_GET_LAYER = 0x0004000c,
  7845. + VCMSG_TST_LAYER = 0x0004400c,
  7846. + VCMSG_SET_LAYER = 0x0004800c,
  7847. + VCMSG_GET_TRANSFORM = 0x0004000d,
  7848. + VCMSG_TST_TRANSFORM = 0x0004400d,
  7849. + VCMSG_SET_TRANSFORM = 0x0004800d,
  7850. + VCMSG_TST_VSYNC = 0x0004400e,
  7851. + VCMSG_SET_VSYNC = 0x0004800e,
  7852. + VCMSG_SET_CURSOR_INFO = 0x00008010,
  7853. + VCMSG_SET_CURSOR_STATE = 0x00008011,
  7854. +};
  7855. +
  7856. +extern int /*rc*/ bcm_mailbox_read(unsigned chan, uint32_t *data28);
  7857. +extern int /*rc*/ bcm_mailbox_write(unsigned chan, uint32_t data28);
  7858. +extern int /*rc*/ bcm_mailbox_property(void *data, int size);
  7859. +
  7860. +#include <linux/ioctl.h>
  7861. +
  7862. +/*
  7863. + * The major device number. We can't rely on dynamic
  7864. + * registration any more, because ioctls need to know
  7865. + * it.
  7866. + */
  7867. +#define MAJOR_NUM 100
  7868. +
  7869. +/*
  7870. + * Set the message of the device driver
  7871. + */
  7872. +#define IOCTL_MBOX_PROPERTY _IOWR(MAJOR_NUM, 0, char *)
  7873. +/*
  7874. + * _IOWR means that we're creating an ioctl command
  7875. + * number for passing information from a user process
  7876. + * to the kernel module and from the kernel module to user process
  7877. + *
  7878. + * The first arguments, MAJOR_NUM, is the major device
  7879. + * number we're using.
  7880. + *
  7881. + * The second argument is the number of the command
  7882. + * (there could be several with different meanings).
  7883. + *
  7884. + * The third argument is the type we want to get from
  7885. + * the process to the kernel.
  7886. + */
  7887. +
  7888. +/*
  7889. + * The name of the device file
  7890. + */
  7891. +#define DEVICE_FILE_NAME "vcio"
  7892. +
  7893. +#endif
  7894. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/vc_mem.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/vc_mem.h
  7895. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/vc_mem.h 1969-12-31 18:00:00.000000000 -0600
  7896. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/vc_mem.h 2014-12-03 19:13:32.448418001 -0600
  7897. @@ -0,0 +1,35 @@
  7898. +/*****************************************************************************
  7899. +* Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
  7900. +*
  7901. +* Unless you and Broadcom execute a separate written software license
  7902. +* agreement governing use of this software, this software is licensed to you
  7903. +* under the terms of the GNU General Public License version 2, available at
  7904. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  7905. +*
  7906. +* Notwithstanding the above, under no circumstances may you combine this
  7907. +* software in any way with any other Broadcom software provided under a
  7908. +* license other than the GPL, without Broadcom's express prior written
  7909. +* consent.
  7910. +*****************************************************************************/
  7911. +
  7912. +#if !defined( VC_MEM_H )
  7913. +#define VC_MEM_H
  7914. +
  7915. +#include <linux/ioctl.h>
  7916. +
  7917. +#define VC_MEM_IOC_MAGIC 'v'
  7918. +
  7919. +#define VC_MEM_IOC_MEM_PHYS_ADDR _IOR( VC_MEM_IOC_MAGIC, 0, unsigned long )
  7920. +#define VC_MEM_IOC_MEM_SIZE _IOR( VC_MEM_IOC_MAGIC, 1, unsigned int )
  7921. +#define VC_MEM_IOC_MEM_BASE _IOR( VC_MEM_IOC_MAGIC, 2, unsigned int )
  7922. +#define VC_MEM_IOC_MEM_LOAD _IOR( VC_MEM_IOC_MAGIC, 3, unsigned int )
  7923. +
  7924. +#if defined( __KERNEL__ )
  7925. +#define VC_MEM_TO_ARM_ADDR_MASK 0x3FFFFFFF
  7926. +
  7927. +extern unsigned long mm_vc_mem_phys_addr;
  7928. +extern unsigned int mm_vc_mem_size;
  7929. +extern int vc_mem_get_current_size( void );
  7930. +#endif
  7931. +
  7932. +#endif /* VC_MEM_H */
  7933. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/vc_sm_defs.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/vc_sm_defs.h
  7934. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/vc_sm_defs.h 1969-12-31 18:00:00.000000000 -0600
  7935. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/vc_sm_defs.h 2014-12-03 19:13:32.448418001 -0600
  7936. @@ -0,0 +1,181 @@
  7937. +/*****************************************************************************
  7938. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  7939. +*
  7940. +* Unless you and Broadcom execute a separate written software license
  7941. +* agreement governing use of this software, this software is licensed to you
  7942. +* under the terms of the GNU General Public License version 2, available at
  7943. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  7944. +*
  7945. +* Notwithstanding the above, under no circumstances may you combine this
  7946. +* software in any way with any other Broadcom software provided under a
  7947. +* license other than the GPL, without Broadcom's express prior written
  7948. +* consent.
  7949. +*****************************************************************************/
  7950. +
  7951. +#ifndef __VC_SM_DEFS_H__INCLUDED__
  7952. +#define __VC_SM_DEFS_H__INCLUDED__
  7953. +
  7954. +/* FourCC code used for VCHI connection */
  7955. +#define VC_SM_SERVER_NAME MAKE_FOURCC("SMEM")
  7956. +
  7957. +/* Maximum message length */
  7958. +#define VC_SM_MAX_MSG_LEN (sizeof(VC_SM_MSG_UNION_T) + \
  7959. + sizeof(VC_SM_MSG_HDR_T))
  7960. +#define VC_SM_MAX_RSP_LEN (sizeof(VC_SM_MSG_UNION_T))
  7961. +
  7962. +/* Resource name maximum size */
  7963. +#define VC_SM_RESOURCE_NAME 32
  7964. +
  7965. +/* All message types supported for HOST->VC direction */
  7966. +typedef enum {
  7967. + /* Allocate shared memory block */
  7968. + VC_SM_MSG_TYPE_ALLOC,
  7969. + /* Lock allocated shared memory block */
  7970. + VC_SM_MSG_TYPE_LOCK,
  7971. + /* Unlock allocated shared memory block */
  7972. + VC_SM_MSG_TYPE_UNLOCK,
  7973. + /* Unlock allocated shared memory block, do not answer command */
  7974. + VC_SM_MSG_TYPE_UNLOCK_NOANS,
  7975. + /* Free shared memory block */
  7976. + VC_SM_MSG_TYPE_FREE,
  7977. + /* Resize a shared memory block */
  7978. + VC_SM_MSG_TYPE_RESIZE,
  7979. + /* Walk the allocated shared memory block(s) */
  7980. + VC_SM_MSG_TYPE_WALK_ALLOC,
  7981. +
  7982. + /* A previously applied action will need to be reverted */
  7983. + VC_SM_MSG_TYPE_ACTION_CLEAN,
  7984. + VC_SM_MSG_TYPE_MAX
  7985. +} VC_SM_MSG_TYPE;
  7986. +
  7987. +/* Type of memory to be allocated */
  7988. +typedef enum {
  7989. + VC_SM_ALLOC_CACHED,
  7990. + VC_SM_ALLOC_NON_CACHED,
  7991. +
  7992. +} VC_SM_ALLOC_TYPE_T;
  7993. +
  7994. +/* Message header for all messages in HOST->VC direction */
  7995. +typedef struct {
  7996. + int32_t type;
  7997. + uint32_t trans_id;
  7998. + uint8_t body[0];
  7999. +
  8000. +} VC_SM_MSG_HDR_T;
  8001. +
  8002. +/* Request to allocate memory (HOST->VC) */
  8003. +typedef struct {
  8004. + /* type of memory to allocate */
  8005. + VC_SM_ALLOC_TYPE_T type;
  8006. + /* byte amount of data to allocate per unit */
  8007. + uint32_t base_unit;
  8008. + /* number of unit to allocate */
  8009. + uint32_t num_unit;
  8010. + /* alignement to be applied on allocation */
  8011. + uint32_t alignement;
  8012. + /* identity of who allocated this block */
  8013. + uint32_t allocator;
  8014. + /* resource name (for easier tracking on vc side) */
  8015. + char name[VC_SM_RESOURCE_NAME];
  8016. +
  8017. +} VC_SM_ALLOC_T;
  8018. +
  8019. +/* Result of a requested memory allocation (VC->HOST) */
  8020. +typedef struct {
  8021. + /* Transaction identifier */
  8022. + uint32_t trans_id;
  8023. +
  8024. + /* Resource handle */
  8025. + uint32_t res_handle;
  8026. + /* Pointer to resource buffer */
  8027. + void *res_mem;
  8028. + /* Resource base size (bytes) */
  8029. + uint32_t res_base_size;
  8030. + /* Resource number */
  8031. + uint32_t res_num;
  8032. +
  8033. +} VC_SM_ALLOC_RESULT_T;
  8034. +
  8035. +/* Request to free a previously allocated memory (HOST->VC) */
  8036. +typedef struct {
  8037. + /* Resource handle (returned from alloc) */
  8038. + uint32_t res_handle;
  8039. + /* Resource buffer (returned from alloc) */
  8040. + void *res_mem;
  8041. +
  8042. +} VC_SM_FREE_T;
  8043. +
  8044. +/* Request to lock a previously allocated memory (HOST->VC) */
  8045. +typedef struct {
  8046. + /* Resource handle (returned from alloc) */
  8047. + uint32_t res_handle;
  8048. + /* Resource buffer (returned from alloc) */
  8049. + void *res_mem;
  8050. +
  8051. +} VC_SM_LOCK_UNLOCK_T;
  8052. +
  8053. +/* Request to resize a previously allocated memory (HOST->VC) */
  8054. +typedef struct {
  8055. + /* Resource handle (returned from alloc) */
  8056. + uint32_t res_handle;
  8057. + /* Resource buffer (returned from alloc) */
  8058. + void *res_mem;
  8059. + /* Resource *new* size requested (bytes) */
  8060. + uint32_t res_new_size;
  8061. +
  8062. +} VC_SM_RESIZE_T;
  8063. +
  8064. +/* Result of a requested memory lock (VC->HOST) */
  8065. +typedef struct {
  8066. + /* Transaction identifier */
  8067. + uint32_t trans_id;
  8068. +
  8069. + /* Resource handle */
  8070. + uint32_t res_handle;
  8071. + /* Pointer to resource buffer */
  8072. + void *res_mem;
  8073. + /* Pointer to former resource buffer if the memory
  8074. + * was reallocated */
  8075. + void *res_old_mem;
  8076. +
  8077. +} VC_SM_LOCK_RESULT_T;
  8078. +
  8079. +/* Generic result for a request (VC->HOST) */
  8080. +typedef struct {
  8081. + /* Transaction identifier */
  8082. + uint32_t trans_id;
  8083. +
  8084. + int32_t success;
  8085. +
  8086. +} VC_SM_RESULT_T;
  8087. +
  8088. +/* Request to revert a previously applied action (HOST->VC) */
  8089. +typedef struct {
  8090. + /* Action of interest */
  8091. + VC_SM_MSG_TYPE res_action;
  8092. + /* Transaction identifier for the action of interest */
  8093. + uint32_t action_trans_id;
  8094. +
  8095. +} VC_SM_ACTION_CLEAN_T;
  8096. +
  8097. +/* Request to remove all data associated with a given allocator (HOST->VC) */
  8098. +typedef struct {
  8099. + /* Allocator identifier */
  8100. + uint32_t allocator;
  8101. +
  8102. +} VC_SM_FREE_ALL_T;
  8103. +
  8104. +/* Union of ALL messages */
  8105. +typedef union {
  8106. + VC_SM_ALLOC_T alloc;
  8107. + VC_SM_ALLOC_RESULT_T alloc_result;
  8108. + VC_SM_FREE_T free;
  8109. + VC_SM_ACTION_CLEAN_T action_clean;
  8110. + VC_SM_RESIZE_T resize;
  8111. + VC_SM_LOCK_RESULT_T lock_result;
  8112. + VC_SM_RESULT_T result;
  8113. + VC_SM_FREE_ALL_T free_all;
  8114. +
  8115. +} VC_SM_MSG_UNION_T;
  8116. +
  8117. +#endif /* __VC_SM_DEFS_H__INCLUDED__ */
  8118. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/vc_sm_knl.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/vc_sm_knl.h
  8119. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/vc_sm_knl.h 1969-12-31 18:00:00.000000000 -0600
  8120. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/vc_sm_knl.h 2014-12-03 19:13:32.448418001 -0600
  8121. @@ -0,0 +1,55 @@
  8122. +/*****************************************************************************
  8123. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  8124. +*
  8125. +* Unless you and Broadcom execute a separate written software license
  8126. +* agreement governing use of this software, this software is licensed to you
  8127. +* under the terms of the GNU General Public License version 2, available at
  8128. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  8129. +*
  8130. +* Notwithstanding the above, under no circumstances may you combine this
  8131. +* software in any way with any other Broadcom software provided under a
  8132. +* license other than the GPL, without Broadcom's express prior written
  8133. +* consent.
  8134. +*****************************************************************************/
  8135. +
  8136. +#ifndef __VC_SM_KNL_H__INCLUDED__
  8137. +#define __VC_SM_KNL_H__INCLUDED__
  8138. +
  8139. +#if !defined(__KERNEL__)
  8140. +#error "This interface is for kernel use only..."
  8141. +#endif
  8142. +
  8143. +/* Type of memory to be locked (ie mapped) */
  8144. +typedef enum {
  8145. + VC_SM_LOCK_CACHED,
  8146. + VC_SM_LOCK_NON_CACHED,
  8147. +
  8148. +} VC_SM_LOCK_CACHE_MODE_T;
  8149. +
  8150. +/* Allocate a shared memory handle and block.
  8151. +*/
  8152. +int vc_sm_alloc(VC_SM_ALLOC_T *alloc, int *handle);
  8153. +
  8154. +/* Free a previously allocated shared memory handle and block.
  8155. +*/
  8156. +int vc_sm_free(int handle);
  8157. +
  8158. +/* Lock a memory handle for use by kernel.
  8159. +*/
  8160. +int vc_sm_lock(int handle, VC_SM_LOCK_CACHE_MODE_T mode,
  8161. + long unsigned int *data);
  8162. +
  8163. +/* Unlock a memory handle in use by kernel.
  8164. +*/
  8165. +int vc_sm_unlock(int handle, int flush, int no_vc_unlock);
  8166. +
  8167. +/* Get an internal resource handle mapped from the external one.
  8168. +*/
  8169. +int vc_sm_int_handle(int handle);
  8170. +
  8171. +/* Map a shared memory region for use by kernel.
  8172. +*/
  8173. +int vc_sm_map(int handle, unsigned int sm_addr, VC_SM_LOCK_CACHE_MODE_T mode,
  8174. + long unsigned int *data);
  8175. +
  8176. +#endif /* __VC_SM_KNL_H__INCLUDED__ */
  8177. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/vc_vchi_sm.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/vc_vchi_sm.h
  8178. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/vc_vchi_sm.h 1969-12-31 18:00:00.000000000 -0600
  8179. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/vc_vchi_sm.h 2014-12-03 19:13:32.448418001 -0600
  8180. @@ -0,0 +1,82 @@
  8181. +/*****************************************************************************
  8182. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  8183. +*
  8184. +* Unless you and Broadcom execute a separate written software license
  8185. +* agreement governing use of this software, this software is licensed to you
  8186. +* under the terms of the GNU General Public License version 2, available at
  8187. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  8188. +*
  8189. +* Notwithstanding the above, under no circumstances may you combine this
  8190. +* software in any way with any other Broadcom software provided under a
  8191. +* license other than the GPL, without Broadcom's express prior written
  8192. +* consent.
  8193. +*****************************************************************************/
  8194. +
  8195. +#ifndef __VC_VCHI_SM_H__INCLUDED__
  8196. +#define __VC_VCHI_SM_H__INCLUDED__
  8197. +
  8198. +#include "interface/vchi/vchi.h"
  8199. +
  8200. +#include "vc_sm_defs.h"
  8201. +
  8202. +/* Forward declare.
  8203. +*/
  8204. +typedef struct sm_instance *VC_VCHI_SM_HANDLE_T;
  8205. +
  8206. +/* Initialize the shared memory service, opens up vchi connection to talk to it.
  8207. +*/
  8208. +VC_VCHI_SM_HANDLE_T vc_vchi_sm_init(VCHI_INSTANCE_T vchi_instance,
  8209. + VCHI_CONNECTION_T **vchi_connections,
  8210. + uint32_t num_connections);
  8211. +
  8212. +/* Terminates the shared memory service.
  8213. +*/
  8214. +int vc_vchi_sm_stop(VC_VCHI_SM_HANDLE_T *handle);
  8215. +
  8216. +/* Ask the shared memory service to allocate some memory on videocre and
  8217. +** return the result of this allocation (which upon success will be a pointer
  8218. +** to some memory in videocore space).
  8219. +*/
  8220. +int vc_vchi_sm_alloc(VC_VCHI_SM_HANDLE_T handle,
  8221. + VC_SM_ALLOC_T *alloc,
  8222. + VC_SM_ALLOC_RESULT_T *alloc_result, uint32_t *trans_id);
  8223. +
  8224. +/* Ask the shared memory service to free up some memory that was previously
  8225. +** allocated by the vc_vchi_sm_alloc function call.
  8226. +*/
  8227. +int vc_vchi_sm_free(VC_VCHI_SM_HANDLE_T handle,
  8228. + VC_SM_FREE_T *free, uint32_t *trans_id);
  8229. +
  8230. +/* Ask the shared memory service to lock up some memory that was previously
  8231. +** allocated by the vc_vchi_sm_alloc function call.
  8232. +*/
  8233. +int vc_vchi_sm_lock(VC_VCHI_SM_HANDLE_T handle,
  8234. + VC_SM_LOCK_UNLOCK_T *lock_unlock,
  8235. + VC_SM_LOCK_RESULT_T *lock_result, uint32_t *trans_id);
  8236. +
  8237. +/* Ask the shared memory service to unlock some memory that was previously
  8238. +** allocated by the vc_vchi_sm_alloc function call.
  8239. +*/
  8240. +int vc_vchi_sm_unlock(VC_VCHI_SM_HANDLE_T handle,
  8241. + VC_SM_LOCK_UNLOCK_T *lock_unlock,
  8242. + uint32_t *trans_id, uint8_t wait_reply);
  8243. +
  8244. +/* Ask the shared memory service to resize some memory that was previously
  8245. +** allocated by the vc_vchi_sm_alloc function call.
  8246. +*/
  8247. +int vc_vchi_sm_resize(VC_VCHI_SM_HANDLE_T handle,
  8248. + VC_SM_RESIZE_T *resize, uint32_t *trans_id);
  8249. +
  8250. +/* Walk the allocated resources on the videocore side, the allocation will
  8251. +** show up in the log. This is purely for debug/information and takes no
  8252. +** specific actions.
  8253. +*/
  8254. +int vc_vchi_sm_walk_alloc(VC_VCHI_SM_HANDLE_T handle);
  8255. +
  8256. +/* Clean up following a previously interrupted action which left the system
  8257. +** in a bad state of some sort.
  8258. +*/
  8259. +int vc_vchi_sm_clean_up(VC_VCHI_SM_HANDLE_T handle,
  8260. + VC_SM_ACTION_CLEAN_T *action_clean);
  8261. +
  8262. +#endif /* __VC_VCHI_SM_H__INCLUDED__ */
  8263. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/vmalloc.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/vmalloc.h
  8264. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/vmalloc.h 1969-12-31 18:00:00.000000000 -0600
  8265. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/vmalloc.h 2014-12-03 19:13:32.448418001 -0600
  8266. @@ -0,0 +1,20 @@
  8267. +/*
  8268. + * arch/arm/mach-bcm2708/include/mach/vmalloc.h
  8269. + *
  8270. + * Copyright (C) 2010 Broadcom
  8271. + *
  8272. + * This program is free software; you can redistribute it and/or modify
  8273. + * it under the terms of the GNU General Public License as published by
  8274. + * the Free Software Foundation; either version 2 of the License, or
  8275. + * (at your option) any later version.
  8276. + *
  8277. + * This program is distributed in the hope that it will be useful,
  8278. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8279. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8280. + * GNU General Public License for more details.
  8281. + *
  8282. + * You should have received a copy of the GNU General Public License
  8283. + * along with this program; if not, write to the Free Software
  8284. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  8285. + */
  8286. +#define VMALLOC_END (0xe8000000)
  8287. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/include/mach/vmcs_sm_ioctl.h linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/vmcs_sm_ioctl.h
  8288. --- linux-3.12.33/arch/arm/mach-bcm2708/include/mach/vmcs_sm_ioctl.h 1969-12-31 18:00:00.000000000 -0600
  8289. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/include/mach/vmcs_sm_ioctl.h 2014-12-03 19:13:32.448418001 -0600
  8290. @@ -0,0 +1,233 @@
  8291. +/*****************************************************************************
  8292. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  8293. +*
  8294. +* Unless you and Broadcom execute a separate written software license
  8295. +* agreement governing use of this software, this software is licensed to you
  8296. +* under the terms of the GNU General Public License version 2, available at
  8297. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  8298. +*
  8299. +* Notwithstanding the above, under no circumstances may you combine this
  8300. +* software in any way with any other Broadcom software provided under a
  8301. +* license other than the GPL, without Broadcom's express prior written
  8302. +* consent.
  8303. +*
  8304. +*****************************************************************************/
  8305. +
  8306. +#if !defined(__VMCS_SM_IOCTL_H__INCLUDED__)
  8307. +#define __VMCS_SM_IOCTL_H__INCLUDED__
  8308. +
  8309. +/* ---- Include Files ---------------------------------------------------- */
  8310. +
  8311. +#if defined(__KERNEL__)
  8312. +#include <linux/types.h> /* Needed for standard types */
  8313. +#else
  8314. +#include <stdint.h>
  8315. +#endif
  8316. +
  8317. +#include <linux/ioctl.h>
  8318. +
  8319. +/* ---- Constants and Types ---------------------------------------------- */
  8320. +
  8321. +#define VMCS_SM_RESOURCE_NAME 32
  8322. +#define VMCS_SM_RESOURCE_NAME_DEFAULT "sm-host-resource"
  8323. +
  8324. +/* Type define used to create unique IOCTL number */
  8325. +#define VMCS_SM_MAGIC_TYPE 'I'
  8326. +
  8327. +/* IOCTL commands */
  8328. +enum vmcs_sm_cmd_e {
  8329. + VMCS_SM_CMD_ALLOC = 0x5A, /* Start at 0x5A arbitrarily */
  8330. + VMCS_SM_CMD_ALLOC_SHARE,
  8331. + VMCS_SM_CMD_LOCK,
  8332. + VMCS_SM_CMD_LOCK_CACHE,
  8333. + VMCS_SM_CMD_UNLOCK,
  8334. + VMCS_SM_CMD_RESIZE,
  8335. + VMCS_SM_CMD_UNMAP,
  8336. + VMCS_SM_CMD_FREE,
  8337. + VMCS_SM_CMD_FLUSH,
  8338. + VMCS_SM_CMD_INVALID,
  8339. +
  8340. + VMCS_SM_CMD_SIZE_USR_HANDLE,
  8341. + VMCS_SM_CMD_CHK_USR_HANDLE,
  8342. +
  8343. + VMCS_SM_CMD_MAPPED_USR_HANDLE,
  8344. + VMCS_SM_CMD_MAPPED_USR_ADDRESS,
  8345. + VMCS_SM_CMD_MAPPED_VC_HDL_FROM_ADDR,
  8346. + VMCS_SM_CMD_MAPPED_VC_HDL_FROM_HDL,
  8347. + VMCS_SM_CMD_MAPPED_VC_ADDR_FROM_HDL,
  8348. +
  8349. + VMCS_SM_CMD_VC_WALK_ALLOC,
  8350. + VMCS_SM_CMD_HOST_WALK_MAP,
  8351. + VMCS_SM_CMD_HOST_WALK_PID_ALLOC,
  8352. + VMCS_SM_CMD_HOST_WALK_PID_MAP,
  8353. +
  8354. + VMCS_SM_CMD_LAST /* Do no delete */
  8355. +};
  8356. +
  8357. +/* Cache type supported, conveniently matches the user space definition in
  8358. +** user-vcsm.h.
  8359. +*/
  8360. +enum vmcs_sm_cache_e {
  8361. + VMCS_SM_CACHE_NONE,
  8362. + VMCS_SM_CACHE_HOST,
  8363. + VMCS_SM_CACHE_VC,
  8364. + VMCS_SM_CACHE_BOTH,
  8365. +};
  8366. +
  8367. +/* IOCTL Data structures */
  8368. +struct vmcs_sm_ioctl_alloc {
  8369. + /* user -> kernel */
  8370. + unsigned int size;
  8371. + unsigned int num;
  8372. + enum vmcs_sm_cache_e cached;
  8373. + char name[VMCS_SM_RESOURCE_NAME];
  8374. +
  8375. + /* kernel -> user */
  8376. + unsigned int handle;
  8377. + /* unsigned int base_addr; */
  8378. +};
  8379. +
  8380. +struct vmcs_sm_ioctl_alloc_share {
  8381. + /* user -> kernel */
  8382. + unsigned int handle;
  8383. + unsigned int size;
  8384. +};
  8385. +
  8386. +struct vmcs_sm_ioctl_free {
  8387. + /* user -> kernel */
  8388. + unsigned int handle;
  8389. + /* unsigned int base_addr; */
  8390. +};
  8391. +
  8392. +struct vmcs_sm_ioctl_lock_unlock {
  8393. + /* user -> kernel */
  8394. + unsigned int handle;
  8395. +
  8396. + /* kernel -> user */
  8397. + unsigned int addr;
  8398. +};
  8399. +
  8400. +struct vmcs_sm_ioctl_lock_cache {
  8401. + /* user -> kernel */
  8402. + unsigned int handle;
  8403. + enum vmcs_sm_cache_e cached;
  8404. +};
  8405. +
  8406. +struct vmcs_sm_ioctl_resize {
  8407. + /* user -> kernel */
  8408. + unsigned int handle;
  8409. + unsigned int new_size;
  8410. +
  8411. + /* kernel -> user */
  8412. + unsigned int old_size;
  8413. +};
  8414. +
  8415. +struct vmcs_sm_ioctl_map {
  8416. + /* user -> kernel */
  8417. + /* and kernel -> user */
  8418. + unsigned int pid;
  8419. + unsigned int handle;
  8420. + unsigned int addr;
  8421. +
  8422. + /* kernel -> user */
  8423. + unsigned int size;
  8424. +};
  8425. +
  8426. +struct vmcs_sm_ioctl_walk {
  8427. + /* user -> kernel */
  8428. + unsigned int pid;
  8429. +};
  8430. +
  8431. +struct vmcs_sm_ioctl_chk {
  8432. + /* user -> kernel */
  8433. + unsigned int handle;
  8434. +
  8435. + /* kernel -> user */
  8436. + unsigned int addr;
  8437. + unsigned int size;
  8438. + enum vmcs_sm_cache_e cache;
  8439. +};
  8440. +
  8441. +struct vmcs_sm_ioctl_size {
  8442. + /* user -> kernel */
  8443. + unsigned int handle;
  8444. +
  8445. + /* kernel -> user */
  8446. + unsigned int size;
  8447. +};
  8448. +
  8449. +struct vmcs_sm_ioctl_cache {
  8450. + /* user -> kernel */
  8451. + unsigned int handle;
  8452. + unsigned int addr;
  8453. + unsigned int size;
  8454. +};
  8455. +
  8456. +/* IOCTL numbers */
  8457. +#define VMCS_SM_IOCTL_MEM_ALLOC\
  8458. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_ALLOC,\
  8459. + struct vmcs_sm_ioctl_alloc)
  8460. +#define VMCS_SM_IOCTL_MEM_ALLOC_SHARE\
  8461. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_ALLOC_SHARE,\
  8462. + struct vmcs_sm_ioctl_alloc_share)
  8463. +#define VMCS_SM_IOCTL_MEM_LOCK\
  8464. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_LOCK,\
  8465. + struct vmcs_sm_ioctl_lock_unlock)
  8466. +#define VMCS_SM_IOCTL_MEM_LOCK_CACHE\
  8467. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_LOCK_CACHE,\
  8468. + struct vmcs_sm_ioctl_lock_cache)
  8469. +#define VMCS_SM_IOCTL_MEM_UNLOCK\
  8470. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_UNLOCK,\
  8471. + struct vmcs_sm_ioctl_lock_unlock)
  8472. +#define VMCS_SM_IOCTL_MEM_RESIZE\
  8473. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_RESIZE,\
  8474. + struct vmcs_sm_ioctl_resize)
  8475. +#define VMCS_SM_IOCTL_MEM_FREE\
  8476. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_FREE,\
  8477. + struct vmcs_sm_ioctl_free)
  8478. +#define VMCS_SM_IOCTL_MEM_FLUSH\
  8479. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_FLUSH,\
  8480. + struct vmcs_sm_ioctl_cache)
  8481. +#define VMCS_SM_IOCTL_MEM_INVALID\
  8482. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_INVALID,\
  8483. + struct vmcs_sm_ioctl_cache)
  8484. +
  8485. +#define VMCS_SM_IOCTL_SIZE_USR_HDL\
  8486. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_SIZE_USR_HANDLE,\
  8487. + struct vmcs_sm_ioctl_size)
  8488. +#define VMCS_SM_IOCTL_CHK_USR_HDL\
  8489. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_CHK_USR_HANDLE,\
  8490. + struct vmcs_sm_ioctl_chk)
  8491. +
  8492. +#define VMCS_SM_IOCTL_MAP_USR_HDL\
  8493. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_USR_HANDLE,\
  8494. + struct vmcs_sm_ioctl_map)
  8495. +#define VMCS_SM_IOCTL_MAP_USR_ADDRESS\
  8496. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_USR_ADDRESS,\
  8497. + struct vmcs_sm_ioctl_map)
  8498. +#define VMCS_SM_IOCTL_MAP_VC_HDL_FR_ADDR\
  8499. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_VC_HDL_FROM_ADDR,\
  8500. + struct vmcs_sm_ioctl_map)
  8501. +#define VMCS_SM_IOCTL_MAP_VC_HDL_FR_HDL\
  8502. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_VC_HDL_FROM_HDL,\
  8503. + struct vmcs_sm_ioctl_map)
  8504. +#define VMCS_SM_IOCTL_MAP_VC_ADDR_FR_HDL\
  8505. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_VC_ADDR_FROM_HDL,\
  8506. + struct vmcs_sm_ioctl_map)
  8507. +
  8508. +#define VMCS_SM_IOCTL_VC_WALK_ALLOC\
  8509. + _IO(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_VC_WALK_ALLOC)
  8510. +#define VMCS_SM_IOCTL_HOST_WALK_MAP\
  8511. + _IO(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_HOST_WALK_MAP)
  8512. +#define VMCS_SM_IOCTL_HOST_WALK_PID_ALLOC\
  8513. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_HOST_WALK_PID_ALLOC,\
  8514. + struct vmcs_sm_ioctl_walk)
  8515. +#define VMCS_SM_IOCTL_HOST_WALK_PID_MAP\
  8516. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_HOST_WALK_PID_MAP,\
  8517. + struct vmcs_sm_ioctl_walk)
  8518. +
  8519. +/* ---- Variable Externs ------------------------------------------------- */
  8520. +
  8521. +/* ---- Function Prototypes ---------------------------------------------- */
  8522. +
  8523. +#endif /* __VMCS_SM_IOCTL_H__INCLUDED__ */
  8524. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/Kconfig linux-3.12.33-rpi/arch/arm/mach-bcm2708/Kconfig
  8525. --- linux-3.12.33/arch/arm/mach-bcm2708/Kconfig 1969-12-31 18:00:00.000000000 -0600
  8526. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/Kconfig 2014-12-03 19:13:32.448418001 -0600
  8527. @@ -0,0 +1,41 @@
  8528. +menu "Broadcom BCM2708 Implementations"
  8529. + depends on ARCH_BCM2708
  8530. +
  8531. +config MACH_BCM2708
  8532. + bool "Broadcom BCM2708 Development Platform"
  8533. + select NEED_MACH_MEMORY_H
  8534. + select NEED_MACH_IO_H
  8535. + select CPU_V6
  8536. + help
  8537. + Include support for the Broadcom(R) BCM2708 platform.
  8538. +
  8539. +config BCM2708_GPIO
  8540. + bool "BCM2708 gpio support"
  8541. + depends on MACH_BCM2708
  8542. + select ARCH_REQUIRE_GPIOLIB
  8543. + default y
  8544. + help
  8545. + Include support for the Broadcom(R) BCM2708 gpio.
  8546. +
  8547. +config BCM2708_VCMEM
  8548. + bool "Videocore Memory"
  8549. + depends on MACH_BCM2708
  8550. + default y
  8551. + help
  8552. + Helper for videocore memory access and total size allocation.
  8553. +
  8554. +config BCM2708_NOL2CACHE
  8555. + bool "Videocore L2 cache disable"
  8556. + depends on MACH_BCM2708
  8557. + default n
  8558. + help
  8559. + Do not allow ARM to use GPU's L2 cache. Requires disable_l2cache in config.txt.
  8560. +
  8561. +config BCM2708_SPIDEV
  8562. + bool "Bind spidev to SPI0 master"
  8563. + depends on MACH_BCM2708
  8564. + depends on SPI
  8565. + default y
  8566. + help
  8567. + Binds spidev driver to the SPI0 master
  8568. +endmenu
  8569. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/Makefile linux-3.12.33-rpi/arch/arm/mach-bcm2708/Makefile
  8570. --- linux-3.12.33/arch/arm/mach-bcm2708/Makefile 1969-12-31 18:00:00.000000000 -0600
  8571. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/Makefile 2014-12-03 19:13:32.448418001 -0600
  8572. @@ -0,0 +1,7 @@
  8573. +#
  8574. +# Makefile for the linux kernel.
  8575. +#
  8576. +
  8577. +obj-$(CONFIG_MACH_BCM2708) += clock.o bcm2708.o armctrl.o vcio.o power.o dma.o
  8578. +obj-$(CONFIG_BCM2708_GPIO) += bcm2708_gpio.o
  8579. +obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
  8580. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/Makefile.boot linux-3.12.33-rpi/arch/arm/mach-bcm2708/Makefile.boot
  8581. --- linux-3.12.33/arch/arm/mach-bcm2708/Makefile.boot 1969-12-31 18:00:00.000000000 -0600
  8582. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/Makefile.boot 2014-12-03 19:13:32.448418001 -0600
  8583. @@ -0,0 +1,3 @@
  8584. + zreladdr-y := 0x00008000
  8585. +params_phys-y := 0x00000100
  8586. +initrd_phys-y := 0x00800000
  8587. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/power.c linux-3.12.33-rpi/arch/arm/mach-bcm2708/power.c
  8588. --- linux-3.12.33/arch/arm/mach-bcm2708/power.c 1969-12-31 18:00:00.000000000 -0600
  8589. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/power.c 2014-12-03 19:13:32.468418001 -0600
  8590. @@ -0,0 +1,194 @@
  8591. +/*
  8592. + * linux/arch/arm/mach-bcm2708/power.c
  8593. + *
  8594. + * Copyright (C) 2010 Broadcom
  8595. + *
  8596. + * This program is free software; you can redistribute it and/or modify
  8597. + * it under the terms of the GNU General Public License version 2 as
  8598. + * published by the Free Software Foundation.
  8599. + *
  8600. + * This device provides a shared mechanism for controlling the power to
  8601. + * VideoCore subsystems.
  8602. + */
  8603. +
  8604. +#include <linux/module.h>
  8605. +#include <linux/semaphore.h>
  8606. +#include <linux/bug.h>
  8607. +#include <mach/power.h>
  8608. +#include <mach/vcio.h>
  8609. +#include <mach/arm_power.h>
  8610. +
  8611. +#define DRIVER_NAME "bcm2708_power"
  8612. +
  8613. +#define BCM_POWER_MAXCLIENTS 4
  8614. +#define BCM_POWER_NOCLIENT (1<<31)
  8615. +
  8616. +/* Some drivers expect there devices to be permanently powered */
  8617. +#define BCM_POWER_ALWAYS_ON (BCM_POWER_USB)
  8618. +
  8619. +#if 1
  8620. +#define DPRINTK printk
  8621. +#else
  8622. +#define DPRINTK if (0) printk
  8623. +#endif
  8624. +
  8625. +struct state_struct {
  8626. + uint32_t global_request;
  8627. + uint32_t client_request[BCM_POWER_MAXCLIENTS];
  8628. + struct semaphore client_mutex;
  8629. + struct semaphore mutex;
  8630. +} g_state;
  8631. +
  8632. +int bcm_power_open(BCM_POWER_HANDLE_T *handle)
  8633. +{
  8634. + BCM_POWER_HANDLE_T i;
  8635. + int ret = -EBUSY;
  8636. +
  8637. + down(&g_state.client_mutex);
  8638. +
  8639. + for (i = 0; i < BCM_POWER_MAXCLIENTS; i++) {
  8640. + if (g_state.client_request[i] == BCM_POWER_NOCLIENT) {
  8641. + g_state.client_request[i] = BCM_POWER_NONE;
  8642. + *handle = i;
  8643. + ret = 0;
  8644. + break;
  8645. + }
  8646. + }
  8647. +
  8648. + up(&g_state.client_mutex);
  8649. +
  8650. + DPRINTK("bcm_power_open() -> %d\n", *handle);
  8651. +
  8652. + return ret;
  8653. +}
  8654. +EXPORT_SYMBOL_GPL(bcm_power_open);
  8655. +
  8656. +int bcm_power_request(BCM_POWER_HANDLE_T handle, uint32_t request)
  8657. +{
  8658. + int rc = 0;
  8659. +
  8660. + DPRINTK("bcm_power_request(%d, %x)\n", handle, request);
  8661. +
  8662. + if ((handle < BCM_POWER_MAXCLIENTS) &&
  8663. + (g_state.client_request[handle] != BCM_POWER_NOCLIENT)) {
  8664. + if (down_interruptible(&g_state.mutex) != 0) {
  8665. + DPRINTK("bcm_power_request -> interrupted\n");
  8666. + return -EINTR;
  8667. + }
  8668. +
  8669. + if (request != g_state.client_request[handle]) {
  8670. + uint32_t others_request = 0;
  8671. + uint32_t global_request;
  8672. + BCM_POWER_HANDLE_T i;
  8673. +
  8674. + for (i = 0; i < BCM_POWER_MAXCLIENTS; i++) {
  8675. + if (i != handle)
  8676. + others_request |=
  8677. + g_state.client_request[i];
  8678. + }
  8679. + others_request &= ~BCM_POWER_NOCLIENT;
  8680. +
  8681. + global_request = request | others_request;
  8682. + if (global_request != g_state.global_request) {
  8683. + uint32_t actual;
  8684. +
  8685. + /* Send a request to VideoCore */
  8686. + bcm_mailbox_write(MBOX_CHAN_POWER,
  8687. + global_request << 4);
  8688. +
  8689. + /* Wait for a response during power-up */
  8690. + if (global_request & ~g_state.global_request) {
  8691. + rc = bcm_mailbox_read(MBOX_CHAN_POWER,
  8692. + &actual);
  8693. + DPRINTK
  8694. + ("bcm_mailbox_read -> %08x, %d\n",
  8695. + actual, rc);
  8696. + actual >>= 4;
  8697. + } else {
  8698. + rc = 0;
  8699. + actual = global_request;
  8700. + }
  8701. +
  8702. + if (rc == 0) {
  8703. + if (actual != global_request) {
  8704. + printk(KERN_ERR
  8705. + "%s: prev global %x, new global %x, actual %x, request %x, others_request %x\n",
  8706. + __func__,
  8707. + g_state.global_request,
  8708. + global_request, actual, request, others_request);
  8709. + /* A failure */
  8710. + BUG_ON((others_request & actual)
  8711. + != others_request);
  8712. + request &= actual;
  8713. + rc = -EIO;
  8714. + }
  8715. +
  8716. + g_state.global_request = actual;
  8717. + g_state.client_request[handle] =
  8718. + request;
  8719. + }
  8720. + }
  8721. + }
  8722. + up(&g_state.mutex);
  8723. + } else {
  8724. + rc = -EINVAL;
  8725. + }
  8726. + DPRINTK("bcm_power_request -> %d\n", rc);
  8727. + return rc;
  8728. +}
  8729. +EXPORT_SYMBOL_GPL(bcm_power_request);
  8730. +
  8731. +int bcm_power_close(BCM_POWER_HANDLE_T handle)
  8732. +{
  8733. + int rc;
  8734. +
  8735. + DPRINTK("bcm_power_close(%d)\n", handle);
  8736. +
  8737. + rc = bcm_power_request(handle, BCM_POWER_NONE);
  8738. + if (rc == 0)
  8739. + g_state.client_request[handle] = BCM_POWER_NOCLIENT;
  8740. +
  8741. + return rc;
  8742. +}
  8743. +EXPORT_SYMBOL_GPL(bcm_power_close);
  8744. +
  8745. +static int __init bcm_power_init(void)
  8746. +{
  8747. +#if defined(BCM_POWER_ALWAYS_ON)
  8748. + BCM_POWER_HANDLE_T always_on_handle;
  8749. +#endif
  8750. + int rc = 0;
  8751. + int i;
  8752. +
  8753. + printk(KERN_INFO "bcm_power: Broadcom power driver\n");
  8754. + bcm_mailbox_write(MBOX_CHAN_POWER, 0);
  8755. +
  8756. + for (i = 0; i < BCM_POWER_MAXCLIENTS; i++)
  8757. + g_state.client_request[i] = BCM_POWER_NOCLIENT;
  8758. +
  8759. + sema_init(&g_state.client_mutex, 1);
  8760. + sema_init(&g_state.mutex, 1);
  8761. +
  8762. + g_state.global_request = 0;
  8763. +
  8764. +#if defined(BCM_POWER_ALWAYS_ON)
  8765. + if (BCM_POWER_ALWAYS_ON) {
  8766. + bcm_power_open(&always_on_handle);
  8767. + bcm_power_request(always_on_handle, BCM_POWER_ALWAYS_ON);
  8768. + }
  8769. +#endif
  8770. +
  8771. + return rc;
  8772. +}
  8773. +
  8774. +static void __exit bcm_power_exit(void)
  8775. +{
  8776. + bcm_mailbox_write(MBOX_CHAN_POWER, 0);
  8777. +}
  8778. +
  8779. +arch_initcall(bcm_power_init); /* Initialize early */
  8780. +module_exit(bcm_power_exit);
  8781. +
  8782. +MODULE_AUTHOR("Phil Elwell");
  8783. +MODULE_DESCRIPTION("Interface to BCM2708 power management");
  8784. +MODULE_LICENSE("GPL");
  8785. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/vcio.c linux-3.12.33-rpi/arch/arm/mach-bcm2708/vcio.c
  8786. --- linux-3.12.33/arch/arm/mach-bcm2708/vcio.c 1969-12-31 18:00:00.000000000 -0600
  8787. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/vcio.c 2014-12-03 19:13:32.472418001 -0600
  8788. @@ -0,0 +1,474 @@
  8789. +/*
  8790. + * linux/arch/arm/mach-bcm2708/vcio.c
  8791. + *
  8792. + * Copyright (C) 2010 Broadcom
  8793. + *
  8794. + * This program is free software; you can redistribute it and/or modify
  8795. + * it under the terms of the GNU General Public License version 2 as
  8796. + * published by the Free Software Foundation.
  8797. + *
  8798. + * This device provides a shared mechanism for writing to the mailboxes,
  8799. + * semaphores, doorbells etc. that are shared between the ARM and the
  8800. + * VideoCore processor
  8801. + */
  8802. +
  8803. +#if defined(CONFIG_SERIAL_BCM_MBOX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  8804. +#define SUPPORT_SYSRQ
  8805. +#endif
  8806. +
  8807. +#include <linux/module.h>
  8808. +#include <linux/console.h>
  8809. +#include <linux/serial_core.h>
  8810. +#include <linux/serial.h>
  8811. +#include <linux/errno.h>
  8812. +#include <linux/device.h>
  8813. +#include <linux/init.h>
  8814. +#include <linux/mm.h>
  8815. +#include <linux/dma-mapping.h>
  8816. +#include <linux/platform_device.h>
  8817. +#include <linux/sysrq.h>
  8818. +#include <linux/delay.h>
  8819. +#include <linux/slab.h>
  8820. +#include <linux/interrupt.h>
  8821. +#include <linux/irq.h>
  8822. +
  8823. +#include <linux/io.h>
  8824. +
  8825. +#include <mach/vcio.h>
  8826. +#include <mach/platform.h>
  8827. +
  8828. +#include <asm/uaccess.h>
  8829. +
  8830. +
  8831. +#define DRIVER_NAME BCM_VCIO_DRIVER_NAME
  8832. +
  8833. +/* ----------------------------------------------------------------------
  8834. + * Mailbox
  8835. + * -------------------------------------------------------------------- */
  8836. +
  8837. +/* offsets from a mail box base address */
  8838. +#define MAIL_WRT 0x00 /* write - and next 4 words */
  8839. +#define MAIL_RD 0x00 /* read - and next 4 words */
  8840. +#define MAIL_POL 0x10 /* read without popping the fifo */
  8841. +#define MAIL_SND 0x14 /* sender ID (bottom two bits) */
  8842. +#define MAIL_STA 0x18 /* status */
  8843. +#define MAIL_CNF 0x1C /* configuration */
  8844. +
  8845. +#define MBOX_MSG(chan, data28) (((data28) & ~0xf) | ((chan) & 0xf))
  8846. +#define MBOX_MSG_LSB(chan, data28) (((data28) << 4) | ((chan) & 0xf))
  8847. +#define MBOX_CHAN(msg) ((msg) & 0xf)
  8848. +#define MBOX_DATA28(msg) ((msg) & ~0xf)
  8849. +#define MBOX_DATA28_LSB(msg) (((uint32_t)msg) >> 4)
  8850. +
  8851. +#define MBOX_MAGIC 0xd0d0c0de
  8852. +
  8853. +struct vc_mailbox {
  8854. + struct device *dev; /* parent device */
  8855. + void __iomem *status;
  8856. + void __iomem *config;
  8857. + void __iomem *read;
  8858. + void __iomem *write;
  8859. + uint32_t msg[MBOX_CHAN_COUNT];
  8860. + struct semaphore sema[MBOX_CHAN_COUNT];
  8861. + uint32_t magic;
  8862. +};
  8863. +
  8864. +static void mbox_init(struct vc_mailbox *mbox_out, struct device *dev,
  8865. + uint32_t addr_mbox)
  8866. +{
  8867. + int i;
  8868. +
  8869. + mbox_out->dev = dev;
  8870. + mbox_out->status = __io_address(addr_mbox + MAIL_STA);
  8871. + mbox_out->config = __io_address(addr_mbox + MAIL_CNF);
  8872. + mbox_out->read = __io_address(addr_mbox + MAIL_RD);
  8873. + /* Write to the other mailbox */
  8874. + mbox_out->write =
  8875. + __io_address((addr_mbox ^ ARM_0_MAIL0_WRT ^ ARM_0_MAIL1_WRT) +
  8876. + MAIL_WRT);
  8877. +
  8878. + for (i = 0; i < MBOX_CHAN_COUNT; i++) {
  8879. + mbox_out->msg[i] = 0;
  8880. + sema_init(&mbox_out->sema[i], 0);
  8881. + }
  8882. +
  8883. + /* Enable the interrupt on data reception */
  8884. + writel(ARM_MC_IHAVEDATAIRQEN, mbox_out->config);
  8885. +
  8886. + mbox_out->magic = MBOX_MAGIC;
  8887. +}
  8888. +
  8889. +static int mbox_write(struct vc_mailbox *mbox, unsigned chan, uint32_t data28)
  8890. +{
  8891. + int rc;
  8892. +
  8893. + if (mbox->magic != MBOX_MAGIC)
  8894. + rc = -EINVAL;
  8895. + else {
  8896. + /* wait for the mailbox FIFO to have some space in it */
  8897. + while (0 != (readl(mbox->status) & ARM_MS_FULL))
  8898. + cpu_relax();
  8899. +
  8900. + writel(MBOX_MSG(chan, data28), mbox->write);
  8901. + rc = 0;
  8902. + }
  8903. + return rc;
  8904. +}
  8905. +
  8906. +static int mbox_read(struct vc_mailbox *mbox, unsigned chan, uint32_t *data28)
  8907. +{
  8908. + int rc;
  8909. +
  8910. + if (mbox->magic != MBOX_MAGIC)
  8911. + rc = -EINVAL;
  8912. + else {
  8913. + down(&mbox->sema[chan]);
  8914. + *data28 = MBOX_DATA28(mbox->msg[chan]);
  8915. + mbox->msg[chan] = 0;
  8916. + rc = 0;
  8917. + }
  8918. + return rc;
  8919. +}
  8920. +
  8921. +static irqreturn_t mbox_irq(int irq, void *dev_id)
  8922. +{
  8923. + /* wait for the mailbox FIFO to have some data in it */
  8924. + struct vc_mailbox *mbox = (struct vc_mailbox *) dev_id;
  8925. + int status = readl(mbox->status);
  8926. + int ret = IRQ_NONE;
  8927. +
  8928. + while (!(status & ARM_MS_EMPTY)) {
  8929. + uint32_t msg = readl(mbox->read);
  8930. + int chan = MBOX_CHAN(msg);
  8931. + if (chan < MBOX_CHAN_COUNT) {
  8932. + if (mbox->msg[chan]) {
  8933. + /* Overflow */
  8934. + printk(KERN_ERR DRIVER_NAME
  8935. + ": mbox chan %d overflow - drop %08x\n",
  8936. + chan, msg);
  8937. + } else {
  8938. + mbox->msg[chan] = (msg | 0xf);
  8939. + up(&mbox->sema[chan]);
  8940. + }
  8941. + } else {
  8942. + printk(KERN_ERR DRIVER_NAME
  8943. + ": invalid channel selector (msg %08x)\n", msg);
  8944. + }
  8945. + ret = IRQ_HANDLED;
  8946. + status = readl(mbox->status);
  8947. + }
  8948. + return ret;
  8949. +}
  8950. +
  8951. +static struct irqaction mbox_irqaction = {
  8952. + .name = "ARM Mailbox IRQ",
  8953. + .flags = IRQF_DISABLED | IRQF_IRQPOLL,
  8954. + .handler = mbox_irq,
  8955. +};
  8956. +
  8957. +/* ----------------------------------------------------------------------
  8958. + * Mailbox Methods
  8959. + * -------------------------------------------------------------------- */
  8960. +
  8961. +static struct device *mbox_dev; /* we assume there's only one! */
  8962. +
  8963. +static int dev_mbox_write(struct device *dev, unsigned chan, uint32_t data28)
  8964. +{
  8965. + int rc;
  8966. +
  8967. + struct vc_mailbox *mailbox = dev_get_drvdata(dev);
  8968. + device_lock(dev);
  8969. + rc = mbox_write(mailbox, chan, data28);
  8970. + device_unlock(dev);
  8971. +
  8972. + return rc;
  8973. +}
  8974. +
  8975. +static int dev_mbox_read(struct device *dev, unsigned chan, uint32_t *data28)
  8976. +{
  8977. + int rc;
  8978. +
  8979. + struct vc_mailbox *mailbox = dev_get_drvdata(dev);
  8980. + device_lock(dev);
  8981. + rc = mbox_read(mailbox, chan, data28);
  8982. + device_unlock(dev);
  8983. +
  8984. + return rc;
  8985. +}
  8986. +
  8987. +extern int bcm_mailbox_write(unsigned chan, uint32_t data28)
  8988. +{
  8989. + if (mbox_dev)
  8990. + return dev_mbox_write(mbox_dev, chan, data28);
  8991. + else
  8992. + return -ENODEV;
  8993. +}
  8994. +EXPORT_SYMBOL_GPL(bcm_mailbox_write);
  8995. +
  8996. +extern int bcm_mailbox_read(unsigned chan, uint32_t *data28)
  8997. +{
  8998. + if (mbox_dev)
  8999. + return dev_mbox_read(mbox_dev, chan, data28);
  9000. + else
  9001. + return -ENODEV;
  9002. +}
  9003. +EXPORT_SYMBOL_GPL(bcm_mailbox_read);
  9004. +
  9005. +static void dev_mbox_register(const char *dev_name, struct device *dev)
  9006. +{
  9007. + mbox_dev = dev;
  9008. +}
  9009. +
  9010. +static int mbox_copy_from_user(void *dst, const void *src, int size)
  9011. +{
  9012. + if ( (uint32_t)src < TASK_SIZE)
  9013. + {
  9014. + return copy_from_user(dst, src, size);
  9015. + }
  9016. + else
  9017. + {
  9018. + memcpy( dst, src, size );
  9019. + return 0;
  9020. + }
  9021. +}
  9022. +
  9023. +static int mbox_copy_to_user(void *dst, const void *src, int size)
  9024. +{
  9025. + if ( (uint32_t)dst < TASK_SIZE)
  9026. + {
  9027. + return copy_to_user(dst, src, size);
  9028. + }
  9029. + else
  9030. + {
  9031. + memcpy( dst, src, size );
  9032. + return 0;
  9033. + }
  9034. +}
  9035. +
  9036. +static DEFINE_MUTEX(mailbox_lock);
  9037. +extern int bcm_mailbox_property(void *data, int size)
  9038. +{
  9039. + uint32_t success;
  9040. + dma_addr_t mem_bus; /* the memory address accessed from videocore */
  9041. + void *mem_kern; /* the memory address accessed from driver */
  9042. + int s = 0;
  9043. +
  9044. + mutex_lock(&mailbox_lock);
  9045. + /* allocate some memory for the messages communicating with GPU */
  9046. + mem_kern = dma_alloc_coherent(NULL, PAGE_ALIGN(size), &mem_bus, GFP_ATOMIC);
  9047. + if (mem_kern) {
  9048. + /* create the message */
  9049. + mbox_copy_from_user(mem_kern, data, size);
  9050. +
  9051. + /* send the message */
  9052. + wmb();
  9053. + s = bcm_mailbox_write(MBOX_CHAN_PROPERTY, (uint32_t)mem_bus);
  9054. + if (s == 0) {
  9055. + s = bcm_mailbox_read(MBOX_CHAN_PROPERTY, &success);
  9056. + }
  9057. + if (s == 0) {
  9058. + /* copy the response */
  9059. + rmb();
  9060. + mbox_copy_to_user(data, mem_kern, size);
  9061. + }
  9062. + dma_free_coherent(NULL, PAGE_ALIGN(size), mem_kern, mem_bus);
  9063. + } else {
  9064. + s = -ENOMEM;
  9065. + }
  9066. + if (s != 0)
  9067. + printk(KERN_ERR DRIVER_NAME ": %s failed (%d)\n", __func__, s);
  9068. +
  9069. + mutex_unlock(&mailbox_lock);
  9070. + return s;
  9071. +}
  9072. +EXPORT_SYMBOL_GPL(bcm_mailbox_property);
  9073. +
  9074. +/* ----------------------------------------------------------------------
  9075. + * Platform Device for Mailbox
  9076. + * -------------------------------------------------------------------- */
  9077. +
  9078. +/*
  9079. + * Is the device open right now? Used to prevent
  9080. + * concurent access into the same device
  9081. + */
  9082. +static int Device_Open = 0;
  9083. +
  9084. +/*
  9085. + * This is called whenever a process attempts to open the device file
  9086. + */
  9087. +static int device_open(struct inode *inode, struct file *file)
  9088. +{
  9089. + /*
  9090. + * We don't want to talk to two processes at the same time
  9091. + */
  9092. + if (Device_Open)
  9093. + return -EBUSY;
  9094. +
  9095. + Device_Open++;
  9096. + /*
  9097. + * Initialize the message
  9098. + */
  9099. + try_module_get(THIS_MODULE);
  9100. + return 0;
  9101. +}
  9102. +
  9103. +static int device_release(struct inode *inode, struct file *file)
  9104. +{
  9105. + /*
  9106. + * We're now ready for our next caller
  9107. + */
  9108. + Device_Open--;
  9109. +
  9110. + module_put(THIS_MODULE);
  9111. + return 0;
  9112. +}
  9113. +
  9114. +/*
  9115. + * This function is called whenever a process tries to do an ioctl on our
  9116. + * device file. We get two extra parameters (additional to the inode and file
  9117. + * structures, which all device functions get): the number of the ioctl called
  9118. + * and the parameter given to the ioctl function.
  9119. + *
  9120. + * If the ioctl is write or read/write (meaning output is returned to the
  9121. + * calling process), the ioctl call returns the output of this function.
  9122. + *
  9123. + */
  9124. +static long device_ioctl(struct file *file, /* see include/linux/fs.h */
  9125. + unsigned int ioctl_num, /* number and param for ioctl */
  9126. + unsigned long ioctl_param)
  9127. +{
  9128. + unsigned size;
  9129. + /*
  9130. + * Switch according to the ioctl called
  9131. + */
  9132. + switch (ioctl_num) {
  9133. + case IOCTL_MBOX_PROPERTY:
  9134. + /*
  9135. + * Receive a pointer to a message (in user space) and set that
  9136. + * to be the device's message. Get the parameter given to
  9137. + * ioctl by the process.
  9138. + */
  9139. + mbox_copy_from_user(&size, (void *)ioctl_param, sizeof size);
  9140. + return bcm_mailbox_property((void *)ioctl_param, size);
  9141. + break;
  9142. + default:
  9143. + printk(KERN_ERR DRIVER_NAME "unknown ioctl: %d\n", ioctl_num);
  9144. + return -EINVAL;
  9145. + }
  9146. +
  9147. + return 0;
  9148. +}
  9149. +
  9150. +/* Module Declarations */
  9151. +
  9152. +/*
  9153. + * This structure will hold the functions to be called
  9154. + * when a process does something to the device we
  9155. + * created. Since a pointer to this structure is kept in
  9156. + * the devices table, it can't be local to
  9157. + * init_module. NULL is for unimplemented functios.
  9158. + */
  9159. +struct file_operations fops = {
  9160. + .unlocked_ioctl = device_ioctl,
  9161. + .open = device_open,
  9162. + .release = device_release, /* a.k.a. close */
  9163. +};
  9164. +
  9165. +static int bcm_vcio_probe(struct platform_device *pdev)
  9166. +{
  9167. + int ret = 0;
  9168. + struct vc_mailbox *mailbox;
  9169. +
  9170. + mailbox = kzalloc(sizeof(*mailbox), GFP_KERNEL);
  9171. + if (NULL == mailbox) {
  9172. + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
  9173. + "mailbox memory\n");
  9174. + ret = -ENOMEM;
  9175. + } else {
  9176. + struct resource *res;
  9177. +
  9178. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  9179. + if (res == NULL) {
  9180. + printk(KERN_ERR DRIVER_NAME ": failed to obtain memory "
  9181. + "resource\n");
  9182. + ret = -ENODEV;
  9183. + kfree(mailbox);
  9184. + } else {
  9185. + /* should be based on the registers from res really */
  9186. + mbox_init(mailbox, &pdev->dev, ARM_0_MAIL0_RD);
  9187. +
  9188. + platform_set_drvdata(pdev, mailbox);
  9189. + dev_mbox_register(DRIVER_NAME, &pdev->dev);
  9190. +
  9191. + mbox_irqaction.dev_id = mailbox;
  9192. + setup_irq(IRQ_ARM_MAILBOX, &mbox_irqaction);
  9193. + printk(KERN_INFO DRIVER_NAME ": mailbox at %p\n",
  9194. + __io_address(ARM_0_MAIL0_RD));
  9195. + }
  9196. + }
  9197. +
  9198. + if (ret == 0) {
  9199. + /*
  9200. + * Register the character device
  9201. + */
  9202. + ret = register_chrdev(MAJOR_NUM, DEVICE_FILE_NAME, &fops);
  9203. +
  9204. + /*
  9205. + * Negative values signify an error
  9206. + */
  9207. + if (ret < 0) {
  9208. + printk(KERN_ERR DRIVER_NAME
  9209. + "Failed registering the character device %d\n", ret);
  9210. + return ret;
  9211. + }
  9212. + }
  9213. + return ret;
  9214. +}
  9215. +
  9216. +static int bcm_vcio_remove(struct platform_device *pdev)
  9217. +{
  9218. + struct vc_mailbox *mailbox = platform_get_drvdata(pdev);
  9219. +
  9220. + platform_set_drvdata(pdev, NULL);
  9221. + kfree(mailbox);
  9222. +
  9223. + return 0;
  9224. +}
  9225. +
  9226. +static struct platform_driver bcm_mbox_driver = {
  9227. + .probe = bcm_vcio_probe,
  9228. + .remove = bcm_vcio_remove,
  9229. +
  9230. + .driver = {
  9231. + .name = DRIVER_NAME,
  9232. + .owner = THIS_MODULE,
  9233. + },
  9234. +};
  9235. +
  9236. +static int __init bcm_mbox_init(void)
  9237. +{
  9238. + int ret;
  9239. +
  9240. + printk(KERN_INFO "mailbox: Broadcom VideoCore Mailbox driver\n");
  9241. +
  9242. + ret = platform_driver_register(&bcm_mbox_driver);
  9243. + if (ret != 0) {
  9244. + printk(KERN_ERR DRIVER_NAME ": failed to register "
  9245. + "on platform\n");
  9246. + }
  9247. +
  9248. + return ret;
  9249. +}
  9250. +
  9251. +static void __exit bcm_mbox_exit(void)
  9252. +{
  9253. + platform_driver_unregister(&bcm_mbox_driver);
  9254. +}
  9255. +
  9256. +arch_initcall(bcm_mbox_init); /* Initialize early */
  9257. +module_exit(bcm_mbox_exit);
  9258. +
  9259. +MODULE_AUTHOR("Gray Girling");
  9260. +MODULE_DESCRIPTION("ARM I/O to VideoCore processor");
  9261. +MODULE_LICENSE("GPL");
  9262. +MODULE_ALIAS("platform:bcm-mbox");
  9263. diff -Nur linux-3.12.33/arch/arm/mach-bcm2708/vc_mem.c linux-3.12.33-rpi/arch/arm/mach-bcm2708/vc_mem.c
  9264. --- linux-3.12.33/arch/arm/mach-bcm2708/vc_mem.c 1969-12-31 18:00:00.000000000 -0600
  9265. +++ linux-3.12.33-rpi/arch/arm/mach-bcm2708/vc_mem.c 2014-12-03 19:13:32.468418001 -0600
  9266. @@ -0,0 +1,432 @@
  9267. +/*****************************************************************************
  9268. +* Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
  9269. +*
  9270. +* Unless you and Broadcom execute a separate written software license
  9271. +* agreement governing use of this software, this software is licensed to you
  9272. +* under the terms of the GNU General Public License version 2, available at
  9273. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  9274. +*
  9275. +* Notwithstanding the above, under no circumstances may you combine this
  9276. +* software in any way with any other Broadcom software provided under a
  9277. +* license other than the GPL, without Broadcom's express prior written
  9278. +* consent.
  9279. +*****************************************************************************/
  9280. +
  9281. +#include <linux/kernel.h>
  9282. +#include <linux/module.h>
  9283. +#include <linux/fs.h>
  9284. +#include <linux/device.h>
  9285. +#include <linux/cdev.h>
  9286. +#include <linux/mm.h>
  9287. +#include <linux/slab.h>
  9288. +#include <linux/debugfs.h>
  9289. +#include <asm/uaccess.h>
  9290. +#include <linux/dma-mapping.h>
  9291. +
  9292. +#ifdef CONFIG_ARCH_KONA
  9293. +#include <chal/chal_ipc.h>
  9294. +#elif CONFIG_ARCH_BCM2708
  9295. +#else
  9296. +#include <csp/chal_ipc.h>
  9297. +#endif
  9298. +
  9299. +#include "mach/vc_mem.h"
  9300. +#include <mach/vcio.h>
  9301. +
  9302. +#define DRIVER_NAME "vc-mem"
  9303. +
  9304. +// Device (/dev) related variables
  9305. +static dev_t vc_mem_devnum = 0;
  9306. +static struct class *vc_mem_class = NULL;
  9307. +static struct cdev vc_mem_cdev;
  9308. +static int vc_mem_inited = 0;
  9309. +
  9310. +#ifdef CONFIG_DEBUG_FS
  9311. +static struct dentry *vc_mem_debugfs_entry;
  9312. +#endif
  9313. +
  9314. +/*
  9315. + * Videocore memory addresses and size
  9316. + *
  9317. + * Drivers that wish to know the videocore memory addresses and sizes should
  9318. + * use these variables instead of the MM_IO_BASE and MM_ADDR_IO defines in
  9319. + * headers. This allows the other drivers to not be tied down to a a certain
  9320. + * address/size at compile time.
  9321. + *
  9322. + * In the future, the goal is to have the videocore memory virtual address and
  9323. + * size be calculated at boot time rather than at compile time. The decision of
  9324. + * where the videocore memory resides and its size would be in the hands of the
  9325. + * bootloader (and/or kernel). When that happens, the values of these variables
  9326. + * would be calculated and assigned in the init function.
  9327. + */
  9328. +// in the 2835 VC in mapped above ARM, but ARM has full access to VC space
  9329. +unsigned long mm_vc_mem_phys_addr = 0x00000000;
  9330. +unsigned int mm_vc_mem_size = 0;
  9331. +unsigned int mm_vc_mem_base = 0;
  9332. +
  9333. +EXPORT_SYMBOL(mm_vc_mem_phys_addr);
  9334. +EXPORT_SYMBOL(mm_vc_mem_size);
  9335. +EXPORT_SYMBOL(mm_vc_mem_base);
  9336. +
  9337. +static uint phys_addr = 0;
  9338. +static uint mem_size = 0;
  9339. +static uint mem_base = 0;
  9340. +
  9341. +
  9342. +/****************************************************************************
  9343. +*
  9344. +* vc_mem_open
  9345. +*
  9346. +***************************************************************************/
  9347. +
  9348. +static int
  9349. +vc_mem_open(struct inode *inode, struct file *file)
  9350. +{
  9351. + (void) inode;
  9352. + (void) file;
  9353. +
  9354. + pr_debug("%s: called file = 0x%p\n", __func__, file);
  9355. +
  9356. + return 0;
  9357. +}
  9358. +
  9359. +/****************************************************************************
  9360. +*
  9361. +* vc_mem_release
  9362. +*
  9363. +***************************************************************************/
  9364. +
  9365. +static int
  9366. +vc_mem_release(struct inode *inode, struct file *file)
  9367. +{
  9368. + (void) inode;
  9369. + (void) file;
  9370. +
  9371. + pr_debug("%s: called file = 0x%p\n", __func__, file);
  9372. +
  9373. + return 0;
  9374. +}
  9375. +
  9376. +/****************************************************************************
  9377. +*
  9378. +* vc_mem_get_size
  9379. +*
  9380. +***************************************************************************/
  9381. +
  9382. +static void
  9383. +vc_mem_get_size(void)
  9384. +{
  9385. +}
  9386. +
  9387. +/****************************************************************************
  9388. +*
  9389. +* vc_mem_get_base
  9390. +*
  9391. +***************************************************************************/
  9392. +
  9393. +static void
  9394. +vc_mem_get_base(void)
  9395. +{
  9396. +}
  9397. +
  9398. +/****************************************************************************
  9399. +*
  9400. +* vc_mem_get_current_size
  9401. +*
  9402. +***************************************************************************/
  9403. +
  9404. +int
  9405. +vc_mem_get_current_size(void)
  9406. +{
  9407. + return mm_vc_mem_size;
  9408. +}
  9409. +
  9410. +EXPORT_SYMBOL_GPL(vc_mem_get_current_size);
  9411. +
  9412. +/****************************************************************************
  9413. +*
  9414. +* vc_mem_ioctl
  9415. +*
  9416. +***************************************************************************/
  9417. +
  9418. +static long
  9419. +vc_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  9420. +{
  9421. + int rc = 0;
  9422. +
  9423. + (void) cmd;
  9424. + (void) arg;
  9425. +
  9426. + pr_debug("%s: called file = 0x%p\n", __func__, file);
  9427. +
  9428. + switch (cmd) {
  9429. + case VC_MEM_IOC_MEM_PHYS_ADDR:
  9430. + {
  9431. + pr_debug("%s: VC_MEM_IOC_MEM_PHYS_ADDR=0x%p\n",
  9432. + __func__, (void *) mm_vc_mem_phys_addr);
  9433. +
  9434. + if (copy_to_user((void *) arg, &mm_vc_mem_phys_addr,
  9435. + sizeof (mm_vc_mem_phys_addr)) != 0) {
  9436. + rc = -EFAULT;
  9437. + }
  9438. + break;
  9439. + }
  9440. + case VC_MEM_IOC_MEM_SIZE:
  9441. + {
  9442. + // Get the videocore memory size first
  9443. + vc_mem_get_size();
  9444. +
  9445. + pr_debug("%s: VC_MEM_IOC_MEM_SIZE=%u\n", __func__,
  9446. + mm_vc_mem_size);
  9447. +
  9448. + if (copy_to_user((void *) arg, &mm_vc_mem_size,
  9449. + sizeof (mm_vc_mem_size)) != 0) {
  9450. + rc = -EFAULT;
  9451. + }
  9452. + break;
  9453. + }
  9454. + case VC_MEM_IOC_MEM_BASE:
  9455. + {
  9456. + // Get the videocore memory base
  9457. + vc_mem_get_base();
  9458. +
  9459. + pr_debug("%s: VC_MEM_IOC_MEM_BASE=%u\n", __func__,
  9460. + mm_vc_mem_base);
  9461. +
  9462. + if (copy_to_user((void *) arg, &mm_vc_mem_base,
  9463. + sizeof (mm_vc_mem_base)) != 0) {
  9464. + rc = -EFAULT;
  9465. + }
  9466. + break;
  9467. + }
  9468. + case VC_MEM_IOC_MEM_LOAD:
  9469. + {
  9470. + // Get the videocore memory base
  9471. + vc_mem_get_base();
  9472. +
  9473. + pr_debug("%s: VC_MEM_IOC_MEM_LOAD=%u\n", __func__,
  9474. + mm_vc_mem_base);
  9475. +
  9476. + if (copy_to_user((void *) arg, &mm_vc_mem_base,
  9477. + sizeof (mm_vc_mem_base)) != 0) {
  9478. + rc = -EFAULT;
  9479. + }
  9480. + break;
  9481. + }
  9482. + default:
  9483. + {
  9484. + return -ENOTTY;
  9485. + }
  9486. + }
  9487. + pr_debug("%s: file = 0x%p returning %d\n", __func__, file, rc);
  9488. +
  9489. + return rc;
  9490. +}
  9491. +
  9492. +/****************************************************************************
  9493. +*
  9494. +* vc_mem_mmap
  9495. +*
  9496. +***************************************************************************/
  9497. +
  9498. +static int
  9499. +vc_mem_mmap(struct file *filp, struct vm_area_struct *vma)
  9500. +{
  9501. + int rc = 0;
  9502. + unsigned long length = vma->vm_end - vma->vm_start;
  9503. + unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  9504. +
  9505. + pr_debug("%s: vm_start = 0x%08lx vm_end = 0x%08lx vm_pgoff = 0x%08lx\n",
  9506. + __func__, (long) vma->vm_start, (long) vma->vm_end,
  9507. + (long) vma->vm_pgoff);
  9508. +
  9509. + if (offset + length > mm_vc_mem_size) {
  9510. + pr_err("%s: length %ld is too big\n", __func__, length);
  9511. + return -EINVAL;
  9512. + }
  9513. + // Do not cache the memory map
  9514. + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  9515. +
  9516. + rc = remap_pfn_range(vma, vma->vm_start,
  9517. + (mm_vc_mem_phys_addr >> PAGE_SHIFT) +
  9518. + vma->vm_pgoff, length, vma->vm_page_prot);
  9519. + if (rc != 0) {
  9520. + pr_err("%s: remap_pfn_range failed (rc=%d)\n", __func__, rc);
  9521. + }
  9522. +
  9523. + return rc;
  9524. +}
  9525. +
  9526. +/****************************************************************************
  9527. +*
  9528. +* File Operations for the driver.
  9529. +*
  9530. +***************************************************************************/
  9531. +
  9532. +static const struct file_operations vc_mem_fops = {
  9533. + .owner = THIS_MODULE,
  9534. + .open = vc_mem_open,
  9535. + .release = vc_mem_release,
  9536. + .unlocked_ioctl = vc_mem_ioctl,
  9537. + .mmap = vc_mem_mmap,
  9538. +};
  9539. +
  9540. +#ifdef CONFIG_DEBUG_FS
  9541. +static void vc_mem_debugfs_deinit(void)
  9542. +{
  9543. + debugfs_remove_recursive(vc_mem_debugfs_entry);
  9544. + vc_mem_debugfs_entry = NULL;
  9545. +}
  9546. +
  9547. +
  9548. +static int vc_mem_debugfs_init(
  9549. + struct device *dev)
  9550. +{
  9551. + vc_mem_debugfs_entry = debugfs_create_dir(DRIVER_NAME, NULL);
  9552. + if (!vc_mem_debugfs_entry) {
  9553. + dev_warn(dev, "could not create debugfs entry\n");
  9554. + return -EFAULT;
  9555. + }
  9556. +
  9557. + if (!debugfs_create_x32("vc_mem_phys_addr",
  9558. + 0444,
  9559. + vc_mem_debugfs_entry,
  9560. + (u32 *)&mm_vc_mem_phys_addr)) {
  9561. + dev_warn(dev, "%s:could not create vc_mem_phys entry\n",
  9562. + __func__);
  9563. + goto fail;
  9564. + }
  9565. +
  9566. + if (!debugfs_create_x32("vc_mem_size",
  9567. + 0444,
  9568. + vc_mem_debugfs_entry,
  9569. + (u32 *)&mm_vc_mem_size)) {
  9570. + dev_warn(dev, "%s:could not create vc_mem_size entry\n",
  9571. + __func__);
  9572. + goto fail;
  9573. + }
  9574. +
  9575. + if (!debugfs_create_x32("vc_mem_base",
  9576. + 0444,
  9577. + vc_mem_debugfs_entry,
  9578. + (u32 *)&mm_vc_mem_base)) {
  9579. + dev_warn(dev, "%s:could not create vc_mem_base entry\n",
  9580. + __func__);
  9581. + goto fail;
  9582. + }
  9583. +
  9584. + return 0;
  9585. +
  9586. +fail:
  9587. + vc_mem_debugfs_deinit();
  9588. + return -EFAULT;
  9589. +}
  9590. +
  9591. +#endif /* CONFIG_DEBUG_FS */
  9592. +
  9593. +
  9594. +/****************************************************************************
  9595. +*
  9596. +* vc_mem_init
  9597. +*
  9598. +***************************************************************************/
  9599. +
  9600. +static int __init
  9601. +vc_mem_init(void)
  9602. +{
  9603. + int rc = -EFAULT;
  9604. + struct device *dev;
  9605. +
  9606. + pr_debug("%s: called\n", __func__);
  9607. +
  9608. + mm_vc_mem_phys_addr = phys_addr;
  9609. + mm_vc_mem_size = mem_size;
  9610. + mm_vc_mem_base = mem_base;
  9611. +
  9612. + vc_mem_get_size();
  9613. +
  9614. + pr_info("vc-mem: phys_addr:0x%08lx mem_base=0x%08x mem_size:0x%08x(%u MiB)\n",
  9615. + mm_vc_mem_phys_addr, mm_vc_mem_base, mm_vc_mem_size, mm_vc_mem_size / (1024 * 1024));
  9616. +
  9617. + if ((rc = alloc_chrdev_region(&vc_mem_devnum, 0, 1, DRIVER_NAME)) < 0) {
  9618. + pr_err("%s: alloc_chrdev_region failed (rc=%d)\n",
  9619. + __func__, rc);
  9620. + goto out_err;
  9621. + }
  9622. +
  9623. + cdev_init(&vc_mem_cdev, &vc_mem_fops);
  9624. + if ((rc = cdev_add(&vc_mem_cdev, vc_mem_devnum, 1)) != 0) {
  9625. + pr_err("%s: cdev_add failed (rc=%d)\n", __func__, rc);
  9626. + goto out_unregister;
  9627. + }
  9628. +
  9629. + vc_mem_class = class_create(THIS_MODULE, DRIVER_NAME);
  9630. + if (IS_ERR(vc_mem_class)) {
  9631. + rc = PTR_ERR(vc_mem_class);
  9632. + pr_err("%s: class_create failed (rc=%d)\n", __func__, rc);
  9633. + goto out_cdev_del;
  9634. + }
  9635. +
  9636. + dev = device_create(vc_mem_class, NULL, vc_mem_devnum, NULL,
  9637. + DRIVER_NAME);
  9638. + if (IS_ERR(dev)) {
  9639. + rc = PTR_ERR(dev);
  9640. + pr_err("%s: device_create failed (rc=%d)\n", __func__, rc);
  9641. + goto out_class_destroy;
  9642. + }
  9643. +
  9644. +#ifdef CONFIG_DEBUG_FS
  9645. + /* don't fail if the debug entries cannot be created */
  9646. + vc_mem_debugfs_init(dev);
  9647. +#endif
  9648. +
  9649. + vc_mem_inited = 1;
  9650. + return 0;
  9651. +
  9652. + device_destroy(vc_mem_class, vc_mem_devnum);
  9653. +
  9654. + out_class_destroy:
  9655. + class_destroy(vc_mem_class);
  9656. + vc_mem_class = NULL;
  9657. +
  9658. + out_cdev_del:
  9659. + cdev_del(&vc_mem_cdev);
  9660. +
  9661. + out_unregister:
  9662. + unregister_chrdev_region(vc_mem_devnum, 1);
  9663. +
  9664. + out_err:
  9665. + return -1;
  9666. +}
  9667. +
  9668. +/****************************************************************************
  9669. +*
  9670. +* vc_mem_exit
  9671. +*
  9672. +***************************************************************************/
  9673. +
  9674. +static void __exit
  9675. +vc_mem_exit(void)
  9676. +{
  9677. + pr_debug("%s: called\n", __func__);
  9678. +
  9679. + if (vc_mem_inited) {
  9680. +#if CONFIG_DEBUG_FS
  9681. + vc_mem_debugfs_deinit();
  9682. +#endif
  9683. + device_destroy(vc_mem_class, vc_mem_devnum);
  9684. + class_destroy(vc_mem_class);
  9685. + cdev_del(&vc_mem_cdev);
  9686. + unregister_chrdev_region(vc_mem_devnum, 1);
  9687. + }
  9688. +}
  9689. +
  9690. +module_init(vc_mem_init);
  9691. +module_exit(vc_mem_exit);
  9692. +MODULE_LICENSE("GPL");
  9693. +MODULE_AUTHOR("Broadcom Corporation");
  9694. +
  9695. +module_param(phys_addr, uint, 0644);
  9696. +module_param(mem_size, uint, 0644);
  9697. +module_param(mem_base, uint, 0644);
  9698. +
  9699. diff -Nur linux-3.12.33/arch/arm/Makefile linux-3.12.33-rpi/arch/arm/Makefile
  9700. --- linux-3.12.33/arch/arm/Makefile 2014-11-15 06:28:07.000000000 -0600
  9701. +++ linux-3.12.33-rpi/arch/arm/Makefile 2014-12-03 19:13:32.212418001 -0600
  9702. @@ -146,6 +146,7 @@
  9703. # by CONFIG_* macro name.
  9704. machine-$(CONFIG_ARCH_AT91) += at91
  9705. machine-$(CONFIG_ARCH_BCM) += bcm
  9706. +machine-$(CONFIG_ARCH_BCM2708) += bcm2708
  9707. machine-$(CONFIG_ARCH_BCM2835) += bcm2835
  9708. machine-$(CONFIG_ARCH_CLPS711X) += clps711x
  9709. machine-$(CONFIG_ARCH_CNS3XXX) += cns3xxx
  9710. diff -Nur linux-3.12.33/arch/arm/mm/Kconfig linux-3.12.33-rpi/arch/arm/mm/Kconfig
  9711. --- linux-3.12.33/arch/arm/mm/Kconfig 2014-11-15 06:28:07.000000000 -0600
  9712. +++ linux-3.12.33-rpi/arch/arm/mm/Kconfig 2014-12-03 19:13:32.640418001 -0600
  9713. @@ -358,7 +358,7 @@
  9714. # ARMv6
  9715. config CPU_V6
  9716. - bool "Support ARM V6 processor" if ARCH_INTEGRATOR || MACH_REALVIEW_EB || MACH_REALVIEW_PBX
  9717. + bool "Support ARM V6 processor" if ARCH_INTEGRATOR || MACH_REALVIEW_EB || MACH_REALVIEW_PBX || MACH_BCM2708
  9718. select CPU_32v6
  9719. select CPU_ABRT_EV6
  9720. select CPU_CACHE_V6
  9721. diff -Nur linux-3.12.33/arch/arm/mm/proc-v6.S linux-3.12.33-rpi/arch/arm/mm/proc-v6.S
  9722. --- linux-3.12.33/arch/arm/mm/proc-v6.S 2014-11-15 06:28:07.000000000 -0600
  9723. +++ linux-3.12.33-rpi/arch/arm/mm/proc-v6.S 2014-12-03 19:13:32.644418001 -0600
  9724. @@ -73,10 +73,19 @@
  9725. *
  9726. * IRQs are already disabled.
  9727. */
  9728. +
  9729. +/* See jira SW-5991 for details of this workaround */
  9730. ENTRY(cpu_v6_do_idle)
  9731. - mov r1, #0
  9732. - mcr p15, 0, r1, c7, c10, 4 @ DWB - WFI may enter a low-power mode
  9733. - mcr p15, 0, r1, c7, c0, 4 @ wait for interrupt
  9734. + .align 5
  9735. + mov r1, #2
  9736. +1: subs r1, #1
  9737. + nop
  9738. + mcreq p15, 0, r1, c7, c10, 4 @ DWB - WFI may enter a low-power mode
  9739. + mcreq p15, 0, r1, c7, c0, 4 @ wait for interrupt
  9740. + nop
  9741. + nop
  9742. + nop
  9743. + bne 1b
  9744. mov pc, lr
  9745. ENTRY(cpu_v6_dcache_clean_area)
  9746. diff -Nur linux-3.12.33/arch/arm/tools/mach-types linux-3.12.33-rpi/arch/arm/tools/mach-types
  9747. --- linux-3.12.33/arch/arm/tools/mach-types 2014-11-15 06:28:07.000000000 -0600
  9748. +++ linux-3.12.33-rpi/arch/arm/tools/mach-types 2014-12-03 19:13:32.656418001 -0600
  9749. @@ -522,6 +522,7 @@
  9750. prima2_evb MACH_PRIMA2_EVB PRIMA2_EVB 3103
  9751. paz00 MACH_PAZ00 PAZ00 3128
  9752. acmenetusfoxg20 MACH_ACMENETUSFOXG20 ACMENETUSFOXG20 3129
  9753. +bcm2708 MACH_BCM2708 BCM2708 3138
  9754. ag5evm MACH_AG5EVM AG5EVM 3189
  9755. ics_if_voip MACH_ICS_IF_VOIP ICS_IF_VOIP 3206
  9756. wlf_cragg_6410 MACH_WLF_CRAGG_6410 WLF_CRAGG_6410 3207
  9757. diff -Nur linux-3.12.33/Documentation/video4linux/bcm2835-v4l2.txt linux-3.12.33-rpi/Documentation/video4linux/bcm2835-v4l2.txt
  9758. --- linux-3.12.33/Documentation/video4linux/bcm2835-v4l2.txt 1969-12-31 18:00:00.000000000 -0600
  9759. +++ linux-3.12.33-rpi/Documentation/video4linux/bcm2835-v4l2.txt 2014-12-03 19:13:32.164418001 -0600
  9760. @@ -0,0 +1,60 @@
  9761. +
  9762. +BCM2835 (aka Raspberry Pi) V4L2 driver
  9763. +======================================
  9764. +
  9765. +1. Copyright
  9766. +============
  9767. +
  9768. +Copyright © 2013 Raspberry Pi (Trading) Ltd.
  9769. +
  9770. +2. License
  9771. +==========
  9772. +
  9773. +This program is free software; you can redistribute it and/or modify
  9774. +it under the terms of the GNU General Public License as published by
  9775. +the Free Software Foundation; either version 2 of the License, or
  9776. +(at your option) any later version.
  9777. +
  9778. +This program is distributed in the hope that it will be useful,
  9779. +but WITHOUT ANY WARRANTY; without even the implied warranty of
  9780. +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9781. +GNU General Public License for more details.
  9782. +
  9783. +You should have received a copy of the GNU General Public License
  9784. +along with this program; if not, write to the Free Software
  9785. +Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  9786. +
  9787. +3. Quick Start
  9788. +==============
  9789. +
  9790. +You need a version 1.0 or later of v4l2-ctl, available from:
  9791. + git://git.linuxtv.org/v4l-utils.git
  9792. +
  9793. +$ sudo modprobe bcm2835-v4l2
  9794. +
  9795. +Turn on the overlay:
  9796. +
  9797. +$ v4l2-ctl --overlay=1
  9798. +
  9799. +Turn off the overlay:
  9800. +
  9801. +$ v4l2-ctl --overlay=0
  9802. +
  9803. +Set the capture format for video:
  9804. +
  9805. +$ v4l2-ctl --set-fmt-video=width=1920,height=1088,pixelformat=4
  9806. +
  9807. +(Note: 1088 not 1080).
  9808. +
  9809. +Capture:
  9810. +
  9811. +$ v4l2-ctl --stream-mmap=3 --stream-count=100 --stream-to=somefile.h264
  9812. +
  9813. +Stills capture:
  9814. +
  9815. +$ v4l2-ctl --set-fmt-video=width=2592,height=1944,pixelformat=3
  9816. +$ v4l2-ctl --stream-mmap=3 --stream-count=1 --stream-to=somefile.jpg
  9817. +
  9818. +List of available formats:
  9819. +
  9820. +$ v4l2-ctl --list-formats
  9821. diff -Nur linux-3.12.33/drivers/char/broadcom/Kconfig linux-3.12.33-rpi/drivers/char/broadcom/Kconfig
  9822. --- linux-3.12.33/drivers/char/broadcom/Kconfig 1969-12-31 18:00:00.000000000 -0600
  9823. +++ linux-3.12.33-rpi/drivers/char/broadcom/Kconfig 2014-12-03 19:13:34.236418001 -0600
  9824. @@ -0,0 +1,22 @@
  9825. +#
  9826. +# Broadcom char driver config
  9827. +#
  9828. +
  9829. +menuconfig BRCM_CHAR_DRIVERS
  9830. + bool "Broadcom Char Drivers"
  9831. + help
  9832. + Broadcom's char drivers
  9833. +
  9834. +config BCM_VC_CMA
  9835. + bool "Videocore CMA"
  9836. + depends on CMA && BRCM_CHAR_DRIVERS && BCM2708_VCHIQ
  9837. + default n
  9838. + help
  9839. + Helper for videocore CMA access.
  9840. +
  9841. +config BCM_VC_SM
  9842. + tristate "VMCS Shared Memory"
  9843. + default n
  9844. + help
  9845. + Support for the VC shared memory on the Broadcom reference
  9846. + design. Uses the VCHIQ stack.
  9847. diff -Nur linux-3.12.33/drivers/char/broadcom/Makefile linux-3.12.33-rpi/drivers/char/broadcom/Makefile
  9848. --- linux-3.12.33/drivers/char/broadcom/Makefile 1969-12-31 18:00:00.000000000 -0600
  9849. +++ linux-3.12.33-rpi/drivers/char/broadcom/Makefile 2014-12-03 19:13:34.236418001 -0600
  9850. @@ -0,0 +1,2 @@
  9851. +obj-$(CONFIG_BCM_VC_CMA) += vc_cma/
  9852. +obj-$(CONFIG_BCM_VC_SM) += vc_sm/
  9853. diff -Nur linux-3.12.33/drivers/char/broadcom/vc_cma/Makefile linux-3.12.33-rpi/drivers/char/broadcom/vc_cma/Makefile
  9854. --- linux-3.12.33/drivers/char/broadcom/vc_cma/Makefile 1969-12-31 18:00:00.000000000 -0600
  9855. +++ linux-3.12.33-rpi/drivers/char/broadcom/vc_cma/Makefile 2014-12-03 19:13:34.236418001 -0600
  9856. @@ -0,0 +1,14 @@
  9857. +ccflags-y += -Wall -Wstrict-prototypes -Wno-trigraphs
  9858. +ccflags-y += -Werror
  9859. +ccflags-y += -Iinclude/linux/broadcom
  9860. +ccflags-y += -Idrivers/misc/vc04_services
  9861. +ccflags-y += -Idrivers/misc/vc04_services/interface/vchi
  9862. +ccflags-y += -Idrivers/misc/vc04_services/interface/vchiq_arm
  9863. +
  9864. +ccflags-y += -D__KERNEL__
  9865. +ccflags-y += -D__linux__
  9866. +ccflags-y += -Werror
  9867. +
  9868. +obj-$(CONFIG_BCM_VC_CMA) += vc-cma.o
  9869. +
  9870. +vc-cma-objs := vc_cma.o
  9871. diff -Nur linux-3.12.33/drivers/char/broadcom/vc_cma/vc_cma.c linux-3.12.33-rpi/drivers/char/broadcom/vc_cma/vc_cma.c
  9872. --- linux-3.12.33/drivers/char/broadcom/vc_cma/vc_cma.c 1969-12-31 18:00:00.000000000 -0600
  9873. +++ linux-3.12.33-rpi/drivers/char/broadcom/vc_cma/vc_cma.c 2014-12-03 19:13:34.236418001 -0600
  9874. @@ -0,0 +1,1143 @@
  9875. +/**
  9876. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  9877. + *
  9878. + * Redistribution and use in source and binary forms, with or without
  9879. + * modification, are permitted provided that the following conditions
  9880. + * are met:
  9881. + * 1. Redistributions of source code must retain the above copyright
  9882. + * notice, this list of conditions, and the following disclaimer,
  9883. + * without modification.
  9884. + * 2. Redistributions in binary form must reproduce the above copyright
  9885. + * notice, this list of conditions and the following disclaimer in the
  9886. + * documentation and/or other materials provided with the distribution.
  9887. + * 3. The names of the above-listed copyright holders may not be used
  9888. + * to endorse or promote products derived from this software without
  9889. + * specific prior written permission.
  9890. + *
  9891. + * ALTERNATIVELY, this software may be distributed under the terms of the
  9892. + * GNU General Public License ("GPL") version 2, as published by the Free
  9893. + * Software Foundation.
  9894. + *
  9895. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  9896. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  9897. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  9898. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  9899. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  9900. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  9901. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  9902. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  9903. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  9904. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  9905. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  9906. + */
  9907. +
  9908. +#include <linux/kernel.h>
  9909. +#include <linux/module.h>
  9910. +#include <linux/kthread.h>
  9911. +#include <linux/fs.h>
  9912. +#include <linux/device.h>
  9913. +#include <linux/cdev.h>
  9914. +#include <linux/mm.h>
  9915. +#include <linux/proc_fs.h>
  9916. +#include <linux/seq_file.h>
  9917. +#include <linux/dma-mapping.h>
  9918. +#include <linux/dma-contiguous.h>
  9919. +#include <linux/platform_device.h>
  9920. +#include <linux/uaccess.h>
  9921. +#include <asm/cacheflush.h>
  9922. +
  9923. +#include "vc_cma.h"
  9924. +
  9925. +#include "vchiq_util.h"
  9926. +#include "vchiq_connected.h"
  9927. +//#include "debug_sym.h"
  9928. +//#include "vc_mem.h"
  9929. +
  9930. +#define DRIVER_NAME "vc-cma"
  9931. +
  9932. +#define LOG_DBG(fmt, ...) \
  9933. + if (vc_cma_debug) \
  9934. + printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
  9935. +#define LOG_ERR(fmt, ...) \
  9936. + printk(KERN_ERR fmt "\n", ##__VA_ARGS__)
  9937. +
  9938. +#define VC_CMA_FOURCC VCHIQ_MAKE_FOURCC('C', 'M', 'A', ' ')
  9939. +#define VC_CMA_VERSION 2
  9940. +
  9941. +#define VC_CMA_CHUNK_ORDER 6 /* 256K */
  9942. +#define VC_CMA_CHUNK_SIZE (4096 << VC_CMA_CHUNK_ORDER)
  9943. +#define VC_CMA_MAX_PARAMS_PER_MSG \
  9944. + ((VCHIQ_MAX_MSG_SIZE - sizeof(unsigned short))/sizeof(unsigned short))
  9945. +#define VC_CMA_RESERVE_COUNT_MAX 16
  9946. +
  9947. +#define PAGES_PER_CHUNK (VC_CMA_CHUNK_SIZE / PAGE_SIZE)
  9948. +
  9949. +#define VCADDR_TO_PHYSADDR(vcaddr) (mm_vc_mem_phys_addr + vcaddr)
  9950. +
  9951. +#define loud_error(...) \
  9952. + LOG_ERR("===== " __VA_ARGS__)
  9953. +
  9954. +enum {
  9955. + VC_CMA_MSG_QUIT,
  9956. + VC_CMA_MSG_OPEN,
  9957. + VC_CMA_MSG_TICK,
  9958. + VC_CMA_MSG_ALLOC, /* chunk count */
  9959. + VC_CMA_MSG_FREE, /* chunk, chunk, ... */
  9960. + VC_CMA_MSG_ALLOCATED, /* chunk, chunk, ... */
  9961. + VC_CMA_MSG_REQUEST_ALLOC, /* chunk count */
  9962. + VC_CMA_MSG_REQUEST_FREE, /* chunk count */
  9963. + VC_CMA_MSG_RESERVE, /* bytes lo, bytes hi */
  9964. + VC_CMA_MSG_UPDATE_RESERVE,
  9965. + VC_CMA_MSG_MAX
  9966. +};
  9967. +
  9968. +struct cma_msg {
  9969. + unsigned short type;
  9970. + unsigned short params[VC_CMA_MAX_PARAMS_PER_MSG];
  9971. +};
  9972. +
  9973. +struct vc_cma_reserve_user {
  9974. + unsigned int pid;
  9975. + unsigned int reserve;
  9976. +};
  9977. +
  9978. +/* Device (/dev) related variables */
  9979. +static dev_t vc_cma_devnum;
  9980. +static struct class *vc_cma_class;
  9981. +static struct cdev vc_cma_cdev;
  9982. +static int vc_cma_inited;
  9983. +static int vc_cma_debug;
  9984. +
  9985. +/* Proc entry */
  9986. +static struct proc_dir_entry *vc_cma_proc_entry;
  9987. +
  9988. +phys_addr_t vc_cma_base;
  9989. +struct page *vc_cma_base_page;
  9990. +unsigned int vc_cma_size;
  9991. +EXPORT_SYMBOL(vc_cma_size);
  9992. +unsigned int vc_cma_initial;
  9993. +unsigned int vc_cma_chunks;
  9994. +unsigned int vc_cma_chunks_used;
  9995. +unsigned int vc_cma_chunks_reserved;
  9996. +
  9997. +static int in_loud_error;
  9998. +
  9999. +unsigned int vc_cma_reserve_total;
  10000. +unsigned int vc_cma_reserve_count;
  10001. +struct vc_cma_reserve_user vc_cma_reserve_users[VC_CMA_RESERVE_COUNT_MAX];
  10002. +static DEFINE_SEMAPHORE(vc_cma_reserve_mutex);
  10003. +static DEFINE_SEMAPHORE(vc_cma_worker_queue_push_mutex);
  10004. +
  10005. +static u64 vc_cma_dma_mask = DMA_BIT_MASK(32);
  10006. +static struct platform_device vc_cma_device = {
  10007. + .name = "vc-cma",
  10008. + .id = 0,
  10009. + .dev = {
  10010. + .dma_mask = &vc_cma_dma_mask,
  10011. + .coherent_dma_mask = DMA_BIT_MASK(32),
  10012. + },
  10013. +};
  10014. +
  10015. +static VCHIQ_INSTANCE_T cma_instance;
  10016. +static VCHIQ_SERVICE_HANDLE_T cma_service;
  10017. +static VCHIU_QUEUE_T cma_msg_queue;
  10018. +static struct task_struct *cma_worker;
  10019. +
  10020. +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid);
  10021. +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply);
  10022. +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
  10023. + VCHIQ_HEADER_T * header,
  10024. + VCHIQ_SERVICE_HANDLE_T service,
  10025. + void *bulk_userdata);
  10026. +static void send_vc_msg(unsigned short type,
  10027. + unsigned short param1, unsigned short param2);
  10028. +static bool send_worker_msg(VCHIQ_HEADER_T * msg);
  10029. +
  10030. +static int early_vc_cma_mem(char *p)
  10031. +{
  10032. + unsigned int new_size;
  10033. + printk(KERN_NOTICE "early_vc_cma_mem(%s)", p);
  10034. + vc_cma_size = memparse(p, &p);
  10035. + vc_cma_initial = vc_cma_size;
  10036. + if (*p == '/')
  10037. + vc_cma_size = memparse(p + 1, &p);
  10038. + if (*p == '@')
  10039. + vc_cma_base = memparse(p + 1, &p);
  10040. +
  10041. + new_size = (vc_cma_size - ((-vc_cma_base) & (VC_CMA_CHUNK_SIZE - 1)))
  10042. + & ~(VC_CMA_CHUNK_SIZE - 1);
  10043. + if (new_size > vc_cma_size)
  10044. + vc_cma_size = 0;
  10045. + vc_cma_initial = (vc_cma_initial + VC_CMA_CHUNK_SIZE - 1)
  10046. + & ~(VC_CMA_CHUNK_SIZE - 1);
  10047. + if (vc_cma_initial > vc_cma_size)
  10048. + vc_cma_initial = vc_cma_size;
  10049. + vc_cma_base = (vc_cma_base + VC_CMA_CHUNK_SIZE - 1)
  10050. + & ~(VC_CMA_CHUNK_SIZE - 1);
  10051. +
  10052. + printk(KERN_NOTICE " -> initial %x, size %x, base %x", vc_cma_initial,
  10053. + vc_cma_size, (unsigned int)vc_cma_base);
  10054. +
  10055. + return 0;
  10056. +}
  10057. +
  10058. +early_param("vc-cma-mem", early_vc_cma_mem);
  10059. +
  10060. +void vc_cma_early_init(void)
  10061. +{
  10062. + LOG_DBG("vc_cma_early_init - vc_cma_chunks = %d", vc_cma_chunks);
  10063. + if (vc_cma_size) {
  10064. + int rc = platform_device_register(&vc_cma_device);
  10065. + LOG_DBG("platform_device_register -> %d", rc);
  10066. + }
  10067. +}
  10068. +
  10069. +void vc_cma_reserve(void)
  10070. +{
  10071. + /* if vc_cma_size is set, then declare vc CMA area of the same
  10072. + * size from the end of memory
  10073. + */
  10074. + if (vc_cma_size) {
  10075. + if (dma_declare_contiguous(NULL /*&vc_cma_device.dev*/, vc_cma_size,
  10076. + vc_cma_base, 0) == 0) {
  10077. + } else {
  10078. + LOG_ERR("vc_cma: dma_declare_contiguous(%x,%x) failed",
  10079. + vc_cma_size, (unsigned int)vc_cma_base);
  10080. + vc_cma_size = 0;
  10081. + }
  10082. + }
  10083. + vc_cma_chunks = vc_cma_size / VC_CMA_CHUNK_SIZE;
  10084. +}
  10085. +
  10086. +/****************************************************************************
  10087. +*
  10088. +* vc_cma_open
  10089. +*
  10090. +***************************************************************************/
  10091. +
  10092. +static int vc_cma_open(struct inode *inode, struct file *file)
  10093. +{
  10094. + (void)inode;
  10095. + (void)file;
  10096. +
  10097. + return 0;
  10098. +}
  10099. +
  10100. +/****************************************************************************
  10101. +*
  10102. +* vc_cma_release
  10103. +*
  10104. +***************************************************************************/
  10105. +
  10106. +static int vc_cma_release(struct inode *inode, struct file *file)
  10107. +{
  10108. + (void)inode;
  10109. + (void)file;
  10110. +
  10111. + vc_cma_set_reserve(0, current->tgid);
  10112. +
  10113. + return 0;
  10114. +}
  10115. +
  10116. +/****************************************************************************
  10117. +*
  10118. +* vc_cma_ioctl
  10119. +*
  10120. +***************************************************************************/
  10121. +
  10122. +static long vc_cma_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  10123. +{
  10124. + int rc = 0;
  10125. +
  10126. + (void)cmd;
  10127. + (void)arg;
  10128. +
  10129. + switch (cmd) {
  10130. + case VC_CMA_IOC_RESERVE:
  10131. + rc = vc_cma_set_reserve((unsigned int)arg, current->tgid);
  10132. + if (rc >= 0)
  10133. + rc = 0;
  10134. + break;
  10135. + default:
  10136. + LOG_ERR("vc-cma: Unknown ioctl %x", cmd);
  10137. + return -ENOTTY;
  10138. + }
  10139. +
  10140. + return rc;
  10141. +}
  10142. +
  10143. +/****************************************************************************
  10144. +*
  10145. +* File Operations for the driver.
  10146. +*
  10147. +***************************************************************************/
  10148. +
  10149. +static const struct file_operations vc_cma_fops = {
  10150. + .owner = THIS_MODULE,
  10151. + .open = vc_cma_open,
  10152. + .release = vc_cma_release,
  10153. + .unlocked_ioctl = vc_cma_ioctl,
  10154. +};
  10155. +
  10156. +/****************************************************************************
  10157. +*
  10158. +* vc_cma_proc_open
  10159. +*
  10160. +***************************************************************************/
  10161. +
  10162. +static int vc_cma_show_info(struct seq_file *m, void *v)
  10163. +{
  10164. + int i;
  10165. +
  10166. + seq_printf(m, "Videocore CMA:\n");
  10167. + seq_printf(m, " Base : %08x\n", (unsigned int)vc_cma_base);
  10168. + seq_printf(m, " Length : %08x\n", vc_cma_size);
  10169. + seq_printf(m, " Initial : %08x\n", vc_cma_initial);
  10170. + seq_printf(m, " Chunk size : %08x\n", VC_CMA_CHUNK_SIZE);
  10171. + seq_printf(m, " Chunks : %4d (%d bytes)\n",
  10172. + (int)vc_cma_chunks,
  10173. + (int)(vc_cma_chunks * VC_CMA_CHUNK_SIZE));
  10174. + seq_printf(m, " Used : %4d (%d bytes)\n",
  10175. + (int)vc_cma_chunks_used,
  10176. + (int)(vc_cma_chunks_used * VC_CMA_CHUNK_SIZE));
  10177. + seq_printf(m, " Reserved : %4d (%d bytes)\n",
  10178. + (unsigned int)vc_cma_chunks_reserved,
  10179. + (int)(vc_cma_chunks_reserved * VC_CMA_CHUNK_SIZE));
  10180. +
  10181. + for (i = 0; i < vc_cma_reserve_count; i++) {
  10182. + struct vc_cma_reserve_user *user = &vc_cma_reserve_users[i];
  10183. + seq_printf(m, " PID %5d: %d bytes\n", user->pid,
  10184. + user->reserve);
  10185. + }
  10186. +
  10187. + seq_printf(m, "\n");
  10188. +
  10189. + return 0;
  10190. +}
  10191. +
  10192. +static int vc_cma_proc_open(struct inode *inode, struct file *file)
  10193. +{
  10194. + return single_open(file, vc_cma_show_info, NULL);
  10195. +}
  10196. +
  10197. +/****************************************************************************
  10198. +*
  10199. +* vc_cma_proc_write
  10200. +*
  10201. +***************************************************************************/
  10202. +
  10203. +static int vc_cma_proc_write(struct file *file,
  10204. + const char __user *buffer,
  10205. + size_t size, loff_t *ppos)
  10206. +{
  10207. + int rc = -EFAULT;
  10208. + char input_str[20];
  10209. +
  10210. + memset(input_str, 0, sizeof(input_str));
  10211. +
  10212. + if (size > sizeof(input_str)) {
  10213. + LOG_ERR("%s: input string length too long", __func__);
  10214. + goto out;
  10215. + }
  10216. +
  10217. + if (copy_from_user(input_str, buffer, size - 1)) {
  10218. + LOG_ERR("%s: failed to get input string", __func__);
  10219. + goto out;
  10220. + }
  10221. +#define ALLOC_STR "alloc"
  10222. +#define FREE_STR "free"
  10223. +#define DEBUG_STR "debug"
  10224. +#define RESERVE_STR "reserve"
  10225. + if (strncmp(input_str, ALLOC_STR, strlen(ALLOC_STR)) == 0) {
  10226. + int size;
  10227. + char *p = input_str + strlen(ALLOC_STR);
  10228. +
  10229. + while (*p == ' ')
  10230. + p++;
  10231. + size = memparse(p, NULL);
  10232. + LOG_ERR("/proc/vc-cma: alloc %d", size);
  10233. + if (size)
  10234. + send_vc_msg(VC_CMA_MSG_REQUEST_FREE,
  10235. + size / VC_CMA_CHUNK_SIZE, 0);
  10236. + else
  10237. + LOG_ERR("invalid size '%s'", p);
  10238. + rc = size;
  10239. + } else if (strncmp(input_str, FREE_STR, strlen(FREE_STR)) == 0) {
  10240. + int size;
  10241. + char *p = input_str + strlen(FREE_STR);
  10242. +
  10243. + while (*p == ' ')
  10244. + p++;
  10245. + size = memparse(p, NULL);
  10246. + LOG_ERR("/proc/vc-cma: free %d", size);
  10247. + if (size)
  10248. + send_vc_msg(VC_CMA_MSG_REQUEST_ALLOC,
  10249. + size / VC_CMA_CHUNK_SIZE, 0);
  10250. + else
  10251. + LOG_ERR("invalid size '%s'", p);
  10252. + rc = size;
  10253. + } else if (strncmp(input_str, DEBUG_STR, strlen(DEBUG_STR)) == 0) {
  10254. + char *p = input_str + strlen(DEBUG_STR);
  10255. + while (*p == ' ')
  10256. + p++;
  10257. + if ((strcmp(p, "on") == 0) || (strcmp(p, "1") == 0))
  10258. + vc_cma_debug = 1;
  10259. + else if ((strcmp(p, "off") == 0) || (strcmp(p, "0") == 0))
  10260. + vc_cma_debug = 0;
  10261. + LOG_ERR("/proc/vc-cma: debug %s", vc_cma_debug ? "on" : "off");
  10262. + rc = size;
  10263. + } else if (strncmp(input_str, RESERVE_STR, strlen(RESERVE_STR)) == 0) {
  10264. + int size;
  10265. + int reserved;
  10266. + char *p = input_str + strlen(RESERVE_STR);
  10267. + while (*p == ' ')
  10268. + p++;
  10269. + size = memparse(p, NULL);
  10270. +
  10271. + reserved = vc_cma_set_reserve(size, current->tgid);
  10272. + rc = (reserved >= 0) ? size : reserved;
  10273. + }
  10274. +
  10275. +out:
  10276. + return rc;
  10277. +}
  10278. +
  10279. +/****************************************************************************
  10280. +*
  10281. +* File Operations for /proc interface.
  10282. +*
  10283. +***************************************************************************/
  10284. +
  10285. +static const struct file_operations vc_cma_proc_fops = {
  10286. + .open = vc_cma_proc_open,
  10287. + .read = seq_read,
  10288. + .write = vc_cma_proc_write,
  10289. + .llseek = seq_lseek,
  10290. + .release = single_release
  10291. +};
  10292. +
  10293. +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid)
  10294. +{
  10295. + struct vc_cma_reserve_user *user = NULL;
  10296. + int delta = 0;
  10297. + int i;
  10298. +
  10299. + if (down_interruptible(&vc_cma_reserve_mutex))
  10300. + return -ERESTARTSYS;
  10301. +
  10302. + for (i = 0; i < vc_cma_reserve_count; i++) {
  10303. + if (pid == vc_cma_reserve_users[i].pid) {
  10304. + user = &vc_cma_reserve_users[i];
  10305. + delta = reserve - user->reserve;
  10306. + if (reserve)
  10307. + user->reserve = reserve;
  10308. + else {
  10309. + /* Remove this entry by copying downwards */
  10310. + while ((i + 1) < vc_cma_reserve_count) {
  10311. + user[0].pid = user[1].pid;
  10312. + user[0].reserve = user[1].reserve;
  10313. + user++;
  10314. + i++;
  10315. + }
  10316. + vc_cma_reserve_count--;
  10317. + user = NULL;
  10318. + }
  10319. + break;
  10320. + }
  10321. + }
  10322. +
  10323. + if (reserve && !user) {
  10324. + if (vc_cma_reserve_count == VC_CMA_RESERVE_COUNT_MAX) {
  10325. + LOG_ERR("vc-cma: Too many reservations - "
  10326. + "increase CMA_RESERVE_COUNT_MAX");
  10327. + up(&vc_cma_reserve_mutex);
  10328. + return -EBUSY;
  10329. + }
  10330. + user = &vc_cma_reserve_users[vc_cma_reserve_count];
  10331. + user->pid = pid;
  10332. + user->reserve = reserve;
  10333. + delta = reserve;
  10334. + vc_cma_reserve_count++;
  10335. + }
  10336. +
  10337. + vc_cma_reserve_total += delta;
  10338. +
  10339. + send_vc_msg(VC_CMA_MSG_RESERVE,
  10340. + vc_cma_reserve_total & 0xffff, vc_cma_reserve_total >> 16);
  10341. +
  10342. + send_worker_msg((VCHIQ_HEADER_T *) VC_CMA_MSG_UPDATE_RESERVE);
  10343. +
  10344. + LOG_DBG("/proc/vc-cma: reserve %d (PID %d) - total %u",
  10345. + reserve, pid, vc_cma_reserve_total);
  10346. +
  10347. + up(&vc_cma_reserve_mutex);
  10348. +
  10349. + return vc_cma_reserve_total;
  10350. +}
  10351. +
  10352. +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
  10353. + VCHIQ_HEADER_T * header,
  10354. + VCHIQ_SERVICE_HANDLE_T service,
  10355. + void *bulk_userdata)
  10356. +{
  10357. + switch (reason) {
  10358. + case VCHIQ_MESSAGE_AVAILABLE:
  10359. + if (!send_worker_msg(header))
  10360. + return VCHIQ_RETRY;
  10361. + break;
  10362. + case VCHIQ_SERVICE_CLOSED:
  10363. + LOG_DBG("CMA service closed");
  10364. + break;
  10365. + default:
  10366. + LOG_ERR("Unexpected CMA callback reason %d", reason);
  10367. + break;
  10368. + }
  10369. + return VCHIQ_SUCCESS;
  10370. +}
  10371. +
  10372. +static void send_vc_msg(unsigned short type,
  10373. + unsigned short param1, unsigned short param2)
  10374. +{
  10375. + unsigned short msg[] = { type, param1, param2 };
  10376. + VCHIQ_ELEMENT_T elem = { &msg, sizeof(msg) };
  10377. + VCHIQ_STATUS_T ret;
  10378. + vchiq_use_service(cma_service);
  10379. + ret = vchiq_queue_message(cma_service, &elem, 1);
  10380. + vchiq_release_service(cma_service);
  10381. + if (ret != VCHIQ_SUCCESS)
  10382. + LOG_ERR("vchiq_queue_message returned %x", ret);
  10383. +}
  10384. +
  10385. +static bool send_worker_msg(VCHIQ_HEADER_T * msg)
  10386. +{
  10387. + if (down_interruptible(&vc_cma_worker_queue_push_mutex))
  10388. + return false;
  10389. + vchiu_queue_push(&cma_msg_queue, msg);
  10390. + up(&vc_cma_worker_queue_push_mutex);
  10391. + return true;
  10392. +}
  10393. +
  10394. +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply)
  10395. +{
  10396. + int i;
  10397. + for (i = 0; i < num_chunks; i++) {
  10398. + struct page *chunk;
  10399. + unsigned int chunk_num;
  10400. + uint8_t *chunk_addr;
  10401. + size_t chunk_size = PAGES_PER_CHUNK << PAGE_SHIFT;
  10402. +
  10403. + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
  10404. + PAGES_PER_CHUNK,
  10405. + VC_CMA_CHUNK_ORDER);
  10406. + if (!chunk)
  10407. + break;
  10408. +
  10409. + chunk_addr = page_address(chunk);
  10410. + dmac_flush_range(chunk_addr, chunk_addr + chunk_size);
  10411. + outer_inv_range(__pa(chunk_addr), __pa(chunk_addr) +
  10412. + chunk_size);
  10413. +
  10414. + chunk_num =
  10415. + (page_to_phys(chunk) - vc_cma_base) / VC_CMA_CHUNK_SIZE;
  10416. + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
  10417. + VC_CMA_CHUNK_SIZE) != 0);
  10418. + if (chunk_num >= vc_cma_chunks) {
  10419. + LOG_ERR("%s: ===============================",
  10420. + __func__);
  10421. + LOG_ERR("%s: chunk phys %x, vc_cma %x-%x - "
  10422. + "bad SPARSEMEM configuration?",
  10423. + __func__, (unsigned int)page_to_phys(chunk),
  10424. + vc_cma_base, vc_cma_base + vc_cma_size - 1);
  10425. + LOG_ERR("%s: dev->cma_area = %p\n", __func__,
  10426. + (void*)0/*vc_cma_device.dev.cma_area*/);
  10427. + LOG_ERR("%s: ===============================",
  10428. + __func__);
  10429. + break;
  10430. + }
  10431. + reply->params[i] = chunk_num;
  10432. + vc_cma_chunks_used++;
  10433. + }
  10434. +
  10435. + if (i < num_chunks) {
  10436. + LOG_ERR("%s: dma_alloc_from_contiguous failed "
  10437. + "for %x bytes (alloc %d of %d, %d free)",
  10438. + __func__, VC_CMA_CHUNK_SIZE, i,
  10439. + num_chunks, vc_cma_chunks - vc_cma_chunks_used);
  10440. + num_chunks = i;
  10441. + }
  10442. +
  10443. + LOG_DBG("CMA allocated %d chunks -> %d used",
  10444. + num_chunks, vc_cma_chunks_used);
  10445. + reply->type = VC_CMA_MSG_ALLOCATED;
  10446. +
  10447. + {
  10448. + VCHIQ_ELEMENT_T elem = {
  10449. + reply,
  10450. + offsetof(struct cma_msg, params[0]) +
  10451. + num_chunks * sizeof(reply->params[0])
  10452. + };
  10453. + VCHIQ_STATUS_T ret;
  10454. + vchiq_use_service(cma_service);
  10455. + ret = vchiq_queue_message(cma_service, &elem, 1);
  10456. + vchiq_release_service(cma_service);
  10457. + if (ret != VCHIQ_SUCCESS)
  10458. + LOG_ERR("vchiq_queue_message return " "%x", ret);
  10459. + }
  10460. +
  10461. + return num_chunks;
  10462. +}
  10463. +
  10464. +static int cma_worker_proc(void *param)
  10465. +{
  10466. + static struct cma_msg reply;
  10467. + (void)param;
  10468. +
  10469. + while (1) {
  10470. + VCHIQ_HEADER_T *msg;
  10471. + static struct cma_msg msg_copy;
  10472. + struct cma_msg *cma_msg = &msg_copy;
  10473. + int type, msg_size;
  10474. +
  10475. + msg = vchiu_queue_pop(&cma_msg_queue);
  10476. + if ((unsigned int)msg >= VC_CMA_MSG_MAX) {
  10477. + msg_size = msg->size;
  10478. + memcpy(&msg_copy, msg->data, msg_size);
  10479. + type = cma_msg->type;
  10480. + vchiq_release_message(cma_service, msg);
  10481. + } else {
  10482. + msg_size = 0;
  10483. + type = (int)msg;
  10484. + if (type == VC_CMA_MSG_QUIT)
  10485. + break;
  10486. + else if (type == VC_CMA_MSG_UPDATE_RESERVE) {
  10487. + msg = NULL;
  10488. + cma_msg = NULL;
  10489. + } else {
  10490. + BUG();
  10491. + continue;
  10492. + }
  10493. + }
  10494. +
  10495. + switch (type) {
  10496. + case VC_CMA_MSG_ALLOC:{
  10497. + int num_chunks, free_chunks;
  10498. + num_chunks = cma_msg->params[0];
  10499. + free_chunks =
  10500. + vc_cma_chunks - vc_cma_chunks_used;
  10501. + LOG_DBG("CMA_MSG_ALLOC(%d chunks)", num_chunks);
  10502. + if (num_chunks > VC_CMA_MAX_PARAMS_PER_MSG) {
  10503. + LOG_ERR
  10504. + ("CMA_MSG_ALLOC - chunk count (%d) "
  10505. + "exceeds VC_CMA_MAX_PARAMS_PER_MSG (%d)",
  10506. + num_chunks,
  10507. + VC_CMA_MAX_PARAMS_PER_MSG);
  10508. + num_chunks = VC_CMA_MAX_PARAMS_PER_MSG;
  10509. + }
  10510. +
  10511. + if (num_chunks > free_chunks) {
  10512. + LOG_ERR
  10513. + ("CMA_MSG_ALLOC - chunk count (%d) "
  10514. + "exceeds free chunks (%d)",
  10515. + num_chunks, free_chunks);
  10516. + num_chunks = free_chunks;
  10517. + }
  10518. +
  10519. + vc_cma_alloc_chunks(num_chunks, &reply);
  10520. + }
  10521. + break;
  10522. +
  10523. + case VC_CMA_MSG_FREE:{
  10524. + int chunk_count =
  10525. + (msg_size -
  10526. + offsetof(struct cma_msg,
  10527. + params)) /
  10528. + sizeof(cma_msg->params[0]);
  10529. + int i;
  10530. + BUG_ON(chunk_count <= 0);
  10531. +
  10532. + LOG_DBG("CMA_MSG_FREE(%d chunks - %x, ...)",
  10533. + chunk_count, cma_msg->params[0]);
  10534. + for (i = 0; i < chunk_count; i++) {
  10535. + int chunk_num = cma_msg->params[i];
  10536. + struct page *page = vc_cma_base_page +
  10537. + chunk_num * PAGES_PER_CHUNK;
  10538. + if (chunk_num >= vc_cma_chunks) {
  10539. + LOG_ERR
  10540. + ("CMA_MSG_FREE - chunk %d of %d"
  10541. + " (value %x) exceeds maximum "
  10542. + "(%x)", i, chunk_count,
  10543. + chunk_num,
  10544. + vc_cma_chunks - 1);
  10545. + break;
  10546. + }
  10547. +
  10548. + if (!dma_release_from_contiguous
  10549. + (NULL /*&vc_cma_device.dev*/, page,
  10550. + PAGES_PER_CHUNK)) {
  10551. + LOG_ERR
  10552. + ("CMA_MSG_FREE - failed to "
  10553. + "release chunk %d (phys %x, "
  10554. + "page %x)", chunk_num,
  10555. + page_to_phys(page),
  10556. + (unsigned int)page);
  10557. + }
  10558. + vc_cma_chunks_used--;
  10559. + }
  10560. + LOG_DBG("CMA released %d chunks -> %d used",
  10561. + i, vc_cma_chunks_used);
  10562. + }
  10563. + break;
  10564. +
  10565. + case VC_CMA_MSG_UPDATE_RESERVE:{
  10566. + int chunks_needed =
  10567. + ((vc_cma_reserve_total + VC_CMA_CHUNK_SIZE -
  10568. + 1)
  10569. + / VC_CMA_CHUNK_SIZE) -
  10570. + vc_cma_chunks_reserved;
  10571. +
  10572. + LOG_DBG
  10573. + ("CMA_MSG_UPDATE_RESERVE(%d chunks needed)",
  10574. + chunks_needed);
  10575. +
  10576. + /* Cap the reservations to what is available */
  10577. + if (chunks_needed > 0) {
  10578. + if (chunks_needed >
  10579. + (vc_cma_chunks -
  10580. + vc_cma_chunks_used))
  10581. + chunks_needed =
  10582. + (vc_cma_chunks -
  10583. + vc_cma_chunks_used);
  10584. +
  10585. + chunks_needed =
  10586. + vc_cma_alloc_chunks(chunks_needed,
  10587. + &reply);
  10588. + }
  10589. +
  10590. + LOG_DBG
  10591. + ("CMA_MSG_UPDATE_RESERVE(%d chunks allocated)",
  10592. + chunks_needed);
  10593. + vc_cma_chunks_reserved += chunks_needed;
  10594. + }
  10595. + break;
  10596. +
  10597. + default:
  10598. + LOG_ERR("unexpected msg type %d", type);
  10599. + break;
  10600. + }
  10601. + }
  10602. +
  10603. + LOG_DBG("quitting...");
  10604. + return 0;
  10605. +}
  10606. +
  10607. +/****************************************************************************
  10608. +*
  10609. +* vc_cma_connected_init
  10610. +*
  10611. +* This function is called once the videocore has been connected.
  10612. +*
  10613. +***************************************************************************/
  10614. +
  10615. +static void vc_cma_connected_init(void)
  10616. +{
  10617. + VCHIQ_SERVICE_PARAMS_T service_params;
  10618. +
  10619. + LOG_DBG("vc_cma_connected_init");
  10620. +
  10621. + if (!vchiu_queue_init(&cma_msg_queue, 16)) {
  10622. + LOG_ERR("could not create CMA msg queue");
  10623. + goto fail_queue;
  10624. + }
  10625. +
  10626. + if (vchiq_initialise(&cma_instance) != VCHIQ_SUCCESS)
  10627. + goto fail_vchiq_init;
  10628. +
  10629. + vchiq_connect(cma_instance);
  10630. +
  10631. + service_params.fourcc = VC_CMA_FOURCC;
  10632. + service_params.callback = cma_service_callback;
  10633. + service_params.userdata = NULL;
  10634. + service_params.version = VC_CMA_VERSION;
  10635. + service_params.version_min = VC_CMA_VERSION;
  10636. +
  10637. + if (vchiq_open_service(cma_instance, &service_params,
  10638. + &cma_service) != VCHIQ_SUCCESS) {
  10639. + LOG_ERR("failed to open service - already in use?");
  10640. + goto fail_vchiq_open;
  10641. + }
  10642. +
  10643. + vchiq_release_service(cma_service);
  10644. +
  10645. + cma_worker = kthread_create(cma_worker_proc, NULL, "cma_worker");
  10646. + if (!cma_worker) {
  10647. + LOG_ERR("could not create CMA worker thread");
  10648. + goto fail_worker;
  10649. + }
  10650. + set_user_nice(cma_worker, -20);
  10651. + wake_up_process(cma_worker);
  10652. +
  10653. + return;
  10654. +
  10655. +fail_worker:
  10656. + vchiq_close_service(cma_service);
  10657. +fail_vchiq_open:
  10658. + vchiq_shutdown(cma_instance);
  10659. +fail_vchiq_init:
  10660. + vchiu_queue_delete(&cma_msg_queue);
  10661. +fail_queue:
  10662. + return;
  10663. +}
  10664. +
  10665. +void
  10666. +loud_error_header(void)
  10667. +{
  10668. + if (in_loud_error)
  10669. + return;
  10670. +
  10671. + LOG_ERR("============================================================"
  10672. + "================");
  10673. + LOG_ERR("============================================================"
  10674. + "================");
  10675. + LOG_ERR("=====");
  10676. +
  10677. + in_loud_error = 1;
  10678. +}
  10679. +
  10680. +void
  10681. +loud_error_footer(void)
  10682. +{
  10683. + if (!in_loud_error)
  10684. + return;
  10685. +
  10686. + LOG_ERR("=====");
  10687. + LOG_ERR("============================================================"
  10688. + "================");
  10689. + LOG_ERR("============================================================"
  10690. + "================");
  10691. +
  10692. + in_loud_error = 0;
  10693. +}
  10694. +
  10695. +#if 1
  10696. +static int check_cma_config(void) { return 1; }
  10697. +#else
  10698. +static int
  10699. +read_vc_debug_var(VC_MEM_ACCESS_HANDLE_T handle,
  10700. + const char *symbol,
  10701. + void *buf, size_t bufsize)
  10702. +{
  10703. + VC_MEM_ADDR_T vcMemAddr;
  10704. + size_t vcMemSize;
  10705. + uint8_t *mapAddr;
  10706. + off_t vcMapAddr;
  10707. +
  10708. + if (!LookupVideoCoreSymbol(handle, symbol,
  10709. + &vcMemAddr,
  10710. + &vcMemSize)) {
  10711. + loud_error_header();
  10712. + loud_error(
  10713. + "failed to find VC symbol \"%s\".",
  10714. + symbol);
  10715. + loud_error_footer();
  10716. + return 0;
  10717. + }
  10718. +
  10719. + if (vcMemSize != bufsize) {
  10720. + loud_error_header();
  10721. + loud_error(
  10722. + "VC symbol \"%s\" is the wrong size.",
  10723. + symbol);
  10724. + loud_error_footer();
  10725. + return 0;
  10726. + }
  10727. +
  10728. + vcMapAddr = (off_t)vcMemAddr & VC_MEM_TO_ARM_ADDR_MASK;
  10729. + vcMapAddr += mm_vc_mem_phys_addr;
  10730. + mapAddr = ioremap_nocache(vcMapAddr, vcMemSize);
  10731. + if (mapAddr == 0) {
  10732. + loud_error_header();
  10733. + loud_error(
  10734. + "failed to ioremap \"%s\" @ 0x%x "
  10735. + "(phys: 0x%x, size: %u).",
  10736. + symbol,
  10737. + (unsigned int)vcMapAddr,
  10738. + (unsigned int)vcMemAddr,
  10739. + (unsigned int)vcMemSize);
  10740. + loud_error_footer();
  10741. + return 0;
  10742. + }
  10743. +
  10744. + memcpy(buf, mapAddr, bufsize);
  10745. + iounmap(mapAddr);
  10746. +
  10747. + return 1;
  10748. +}
  10749. +
  10750. +
  10751. +static int
  10752. +check_cma_config(void)
  10753. +{
  10754. + VC_MEM_ACCESS_HANDLE_T mem_hndl;
  10755. + VC_MEM_ADDR_T mempool_start;
  10756. + VC_MEM_ADDR_T mempool_end;
  10757. + VC_MEM_ADDR_T mempool_offline_start;
  10758. + VC_MEM_ADDR_T mempool_offline_end;
  10759. + VC_MEM_ADDR_T cam_alloc_base;
  10760. + VC_MEM_ADDR_T cam_alloc_size;
  10761. + VC_MEM_ADDR_T cam_alloc_end;
  10762. + int success = 0;
  10763. +
  10764. + if (OpenVideoCoreMemory(&mem_hndl) != 0)
  10765. + goto out;
  10766. +
  10767. + /* Read the relevant VideoCore variables */
  10768. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_START",
  10769. + &mempool_start,
  10770. + sizeof(mempool_start)))
  10771. + goto close;
  10772. +
  10773. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_END",
  10774. + &mempool_end,
  10775. + sizeof(mempool_end)))
  10776. + goto close;
  10777. +
  10778. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_START",
  10779. + &mempool_offline_start,
  10780. + sizeof(mempool_offline_start)))
  10781. + goto close;
  10782. +
  10783. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_END",
  10784. + &mempool_offline_end,
  10785. + sizeof(mempool_offline_end)))
  10786. + goto close;
  10787. +
  10788. + if (!read_vc_debug_var(mem_hndl, "cam_alloc_base",
  10789. + &cam_alloc_base,
  10790. + sizeof(cam_alloc_base)))
  10791. + goto close;
  10792. +
  10793. + if (!read_vc_debug_var(mem_hndl, "cam_alloc_size",
  10794. + &cam_alloc_size,
  10795. + sizeof(cam_alloc_size)))
  10796. + goto close;
  10797. +
  10798. + cam_alloc_end = cam_alloc_base + cam_alloc_size;
  10799. +
  10800. + success = 1;
  10801. +
  10802. + /* Now the sanity checks */
  10803. + if (!mempool_offline_start)
  10804. + mempool_offline_start = mempool_start;
  10805. + if (!mempool_offline_end)
  10806. + mempool_offline_end = mempool_end;
  10807. +
  10808. + if (VCADDR_TO_PHYSADDR(mempool_offline_start) != vc_cma_base) {
  10809. + loud_error_header();
  10810. + loud_error(
  10811. + "__MEMPOOL_OFFLINE_START(%x -> %lx) doesn't match "
  10812. + "vc_cma_base(%x)",
  10813. + mempool_offline_start,
  10814. + VCADDR_TO_PHYSADDR(mempool_offline_start),
  10815. + vc_cma_base);
  10816. + success = 0;
  10817. + }
  10818. +
  10819. + if (VCADDR_TO_PHYSADDR(mempool_offline_end) !=
  10820. + (vc_cma_base + vc_cma_size)) {
  10821. + loud_error_header();
  10822. + loud_error(
  10823. + "__MEMPOOL_OFFLINE_END(%x -> %lx) doesn't match "
  10824. + "vc_cma_base(%x) + vc_cma_size(%x) = %x",
  10825. + mempool_offline_start,
  10826. + VCADDR_TO_PHYSADDR(mempool_offline_end),
  10827. + vc_cma_base, vc_cma_size, vc_cma_base + vc_cma_size);
  10828. + success = 0;
  10829. + }
  10830. +
  10831. + if (mempool_end < mempool_start) {
  10832. + loud_error_header();
  10833. + loud_error(
  10834. + "__MEMPOOL_END(%x) must not be before "
  10835. + "__MEMPOOL_START(%x)",
  10836. + mempool_end,
  10837. + mempool_start);
  10838. + success = 0;
  10839. + }
  10840. +
  10841. + if (mempool_offline_end < mempool_offline_start) {
  10842. + loud_error_header();
  10843. + loud_error(
  10844. + "__MEMPOOL_OFFLINE_END(%x) must not be before "
  10845. + "__MEMPOOL_OFFLINE_START(%x)",
  10846. + mempool_offline_end,
  10847. + mempool_offline_start);
  10848. + success = 0;
  10849. + }
  10850. +
  10851. + if (mempool_offline_start < mempool_start) {
  10852. + loud_error_header();
  10853. + loud_error(
  10854. + "__MEMPOOL_OFFLINE_START(%x) must not be before "
  10855. + "__MEMPOOL_START(%x)",
  10856. + mempool_offline_start,
  10857. + mempool_start);
  10858. + success = 0;
  10859. + }
  10860. +
  10861. + if (mempool_offline_end > mempool_end) {
  10862. + loud_error_header();
  10863. + loud_error(
  10864. + "__MEMPOOL_OFFLINE_END(%x) must not be after "
  10865. + "__MEMPOOL_END(%x)",
  10866. + mempool_offline_end,
  10867. + mempool_end);
  10868. + success = 0;
  10869. + }
  10870. +
  10871. + if ((cam_alloc_base < mempool_end) &&
  10872. + (cam_alloc_end > mempool_start)) {
  10873. + loud_error_header();
  10874. + loud_error(
  10875. + "cam_alloc pool(%x-%x) overlaps "
  10876. + "mempool(%x-%x)",
  10877. + cam_alloc_base, cam_alloc_end,
  10878. + mempool_start, mempool_end);
  10879. + success = 0;
  10880. + }
  10881. +
  10882. + loud_error_footer();
  10883. +
  10884. +close:
  10885. + CloseVideoCoreMemory(mem_hndl);
  10886. +
  10887. +out:
  10888. + return success;
  10889. +}
  10890. +#endif
  10891. +
  10892. +static int vc_cma_init(void)
  10893. +{
  10894. + int rc = -EFAULT;
  10895. + struct device *dev;
  10896. +
  10897. + if (!check_cma_config())
  10898. + goto out_release;
  10899. +
  10900. + printk(KERN_INFO "vc-cma: Videocore CMA driver\n");
  10901. + printk(KERN_INFO "vc-cma: vc_cma_base = 0x%08x\n", vc_cma_base);
  10902. + printk(KERN_INFO "vc-cma: vc_cma_size = 0x%08x (%u MiB)\n",
  10903. + vc_cma_size, vc_cma_size / (1024 * 1024));
  10904. + printk(KERN_INFO "vc-cma: vc_cma_initial = 0x%08x (%u MiB)\n",
  10905. + vc_cma_initial, vc_cma_initial / (1024 * 1024));
  10906. +
  10907. + vc_cma_base_page = phys_to_page(vc_cma_base);
  10908. +
  10909. + if (vc_cma_chunks) {
  10910. + int chunks_needed = vc_cma_initial / VC_CMA_CHUNK_SIZE;
  10911. +
  10912. + for (vc_cma_chunks_used = 0;
  10913. + vc_cma_chunks_used < chunks_needed; vc_cma_chunks_used++) {
  10914. + struct page *chunk;
  10915. + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
  10916. + PAGES_PER_CHUNK,
  10917. + VC_CMA_CHUNK_ORDER);
  10918. + if (!chunk)
  10919. + break;
  10920. + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
  10921. + VC_CMA_CHUNK_SIZE) != 0);
  10922. + }
  10923. + if (vc_cma_chunks_used != chunks_needed) {
  10924. + LOG_ERR("%s: dma_alloc_from_contiguous failed (%d "
  10925. + "bytes, allocation %d of %d)",
  10926. + __func__, VC_CMA_CHUNK_SIZE,
  10927. + vc_cma_chunks_used, chunks_needed);
  10928. + goto out_release;
  10929. + }
  10930. +
  10931. + vchiq_add_connected_callback(vc_cma_connected_init);
  10932. + }
  10933. +
  10934. + rc = alloc_chrdev_region(&vc_cma_devnum, 0, 1, DRIVER_NAME);
  10935. + if (rc < 0) {
  10936. + LOG_ERR("%s: alloc_chrdev_region failed (rc=%d)", __func__, rc);
  10937. + goto out_release;
  10938. + }
  10939. +
  10940. + cdev_init(&vc_cma_cdev, &vc_cma_fops);
  10941. + rc = cdev_add(&vc_cma_cdev, vc_cma_devnum, 1);
  10942. + if (rc != 0) {
  10943. + LOG_ERR("%s: cdev_add failed (rc=%d)", __func__, rc);
  10944. + goto out_unregister;
  10945. + }
  10946. +
  10947. + vc_cma_class = class_create(THIS_MODULE, DRIVER_NAME);
  10948. + if (IS_ERR(vc_cma_class)) {
  10949. + rc = PTR_ERR(vc_cma_class);
  10950. + LOG_ERR("%s: class_create failed (rc=%d)", __func__, rc);
  10951. + goto out_cdev_del;
  10952. + }
  10953. +
  10954. + dev = device_create(vc_cma_class, NULL, vc_cma_devnum, NULL,
  10955. + DRIVER_NAME);
  10956. + if (IS_ERR(dev)) {
  10957. + rc = PTR_ERR(dev);
  10958. + LOG_ERR("%s: device_create failed (rc=%d)", __func__, rc);
  10959. + goto out_class_destroy;
  10960. + }
  10961. +
  10962. + vc_cma_proc_entry = proc_create(DRIVER_NAME, 0444, NULL, &vc_cma_proc_fops);
  10963. + if (vc_cma_proc_entry == NULL) {
  10964. + rc = -EFAULT;
  10965. + LOG_ERR("%s: proc_create failed", __func__);
  10966. + goto out_device_destroy;
  10967. + }
  10968. +
  10969. + vc_cma_inited = 1;
  10970. + return 0;
  10971. +
  10972. +out_device_destroy:
  10973. + device_destroy(vc_cma_class, vc_cma_devnum);
  10974. +
  10975. +out_class_destroy:
  10976. + class_destroy(vc_cma_class);
  10977. + vc_cma_class = NULL;
  10978. +
  10979. +out_cdev_del:
  10980. + cdev_del(&vc_cma_cdev);
  10981. +
  10982. +out_unregister:
  10983. + unregister_chrdev_region(vc_cma_devnum, 1);
  10984. +
  10985. +out_release:
  10986. + /* It is tempting to try to clean up by calling
  10987. + dma_release_from_contiguous for all allocated chunks, but it isn't
  10988. + a very safe thing to do. If vc_cma_initial is non-zero it is because
  10989. + VideoCore is already using that memory, so giving it back to Linux
  10990. + is likely to be fatal.
  10991. + */
  10992. + return -1;
  10993. +}
  10994. +
  10995. +/****************************************************************************
  10996. +*
  10997. +* vc_cma_exit
  10998. +*
  10999. +***************************************************************************/
  11000. +
  11001. +static void __exit vc_cma_exit(void)
  11002. +{
  11003. + LOG_DBG("%s: called", __func__);
  11004. +
  11005. + if (vc_cma_inited) {
  11006. + remove_proc_entry(DRIVER_NAME, NULL);
  11007. + device_destroy(vc_cma_class, vc_cma_devnum);
  11008. + class_destroy(vc_cma_class);
  11009. + cdev_del(&vc_cma_cdev);
  11010. + unregister_chrdev_region(vc_cma_devnum, 1);
  11011. + }
  11012. +}
  11013. +
  11014. +module_init(vc_cma_init);
  11015. +module_exit(vc_cma_exit);
  11016. +MODULE_LICENSE("GPL");
  11017. +MODULE_AUTHOR("Broadcom Corporation");
  11018. diff -Nur linux-3.12.33/drivers/char/broadcom/vc_sm/Makefile linux-3.12.33-rpi/drivers/char/broadcom/vc_sm/Makefile
  11019. --- linux-3.12.33/drivers/char/broadcom/vc_sm/Makefile 1969-12-31 18:00:00.000000000 -0600
  11020. +++ linux-3.12.33-rpi/drivers/char/broadcom/vc_sm/Makefile 2014-12-03 19:13:34.236418001 -0600
  11021. @@ -0,0 +1,21 @@
  11022. +EXTRA_CFLAGS += -Wall -Wstrict-prototypes -Wno-trigraphs -O2
  11023. +
  11024. +EXTRA_CFLAGS += -I"./arch/arm/mach-bcm2708/include/mach"
  11025. +EXTRA_CFLAGS += -I"drivers/misc/vc04_services"
  11026. +EXTRA_CFLAGS += -I"drivers/misc/vc04_services/interface/vchi"
  11027. +EXTRA_CFLAGS += -I"drivers/misc/vc04_services/interface/vchiq_arm"
  11028. +EXTRA_CFLAGS += -I"$(srctree)/fs/"
  11029. +
  11030. +EXTRA_CFLAGS += -DOS_ASSERT_FAILURE
  11031. +EXTRA_CFLAGS += -D__STDC_VERSION=199901L
  11032. +EXTRA_CFLAGS += -D__STDC_VERSION__=199901L
  11033. +EXTRA_CFLAGS += -D__VCCOREVER__=0
  11034. +EXTRA_CFLAGS += -D__KERNEL__
  11035. +EXTRA_CFLAGS += -D__linux__
  11036. +EXTRA_CFLAGS += -Werror
  11037. +
  11038. +obj-$(CONFIG_BCM_VC_SM) := vc-sm.o
  11039. +
  11040. +vc-sm-objs := \
  11041. + vmcs_sm.o \
  11042. + vc_vchi_sm.o
  11043. diff -Nur linux-3.12.33/drivers/char/broadcom/vc_sm/vc_vchi_sm.c linux-3.12.33-rpi/drivers/char/broadcom/vc_sm/vc_vchi_sm.c
  11044. --- linux-3.12.33/drivers/char/broadcom/vc_sm/vc_vchi_sm.c 1969-12-31 18:00:00.000000000 -0600
  11045. +++ linux-3.12.33-rpi/drivers/char/broadcom/vc_sm/vc_vchi_sm.c 2014-12-03 19:13:34.236418001 -0600
  11046. @@ -0,0 +1,492 @@
  11047. +/*****************************************************************************
  11048. +* Copyright 2011-2012 Broadcom Corporation. All rights reserved.
  11049. +*
  11050. +* Unless you and Broadcom execute a separate written software license
  11051. +* agreement governing use of this software, this software is licensed to you
  11052. +* under the terms of the GNU General Public License version 2, available at
  11053. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  11054. +*
  11055. +* Notwithstanding the above, under no circumstances may you combine this
  11056. +* software in any way with any other Broadcom software provided under a
  11057. +* license other than the GPL, without Broadcom's express prior written
  11058. +* consent.
  11059. +*****************************************************************************/
  11060. +
  11061. +/* ---- Include Files ----------------------------------------------------- */
  11062. +#include <linux/types.h>
  11063. +#include <linux/kernel.h>
  11064. +#include <linux/list.h>
  11065. +#include <linux/semaphore.h>
  11066. +#include <linux/mutex.h>
  11067. +#include <linux/slab.h>
  11068. +#include <linux/kthread.h>
  11069. +
  11070. +#include "vc_vchi_sm.h"
  11071. +
  11072. +#define VC_SM_VER 1
  11073. +#define VC_SM_MIN_VER 0
  11074. +
  11075. +/* ---- Private Constants and Types -------------------------------------- */
  11076. +
  11077. +/* Command blocks come from a pool */
  11078. +#define SM_MAX_NUM_CMD_RSP_BLKS 32
  11079. +
  11080. +struct sm_cmd_rsp_blk {
  11081. + struct list_head head; /* To create lists */
  11082. + struct semaphore sema; /* To be signaled when the response is there */
  11083. +
  11084. + uint16_t id;
  11085. + uint16_t length;
  11086. +
  11087. + uint8_t msg[VC_SM_MAX_MSG_LEN];
  11088. +
  11089. + uint32_t wait:1;
  11090. + uint32_t sent:1;
  11091. + uint32_t alloc:1;
  11092. +
  11093. +};
  11094. +
  11095. +struct sm_instance {
  11096. + uint32_t num_connections;
  11097. + VCHI_SERVICE_HANDLE_T vchi_handle[VCHI_MAX_NUM_CONNECTIONS];
  11098. + struct task_struct *io_thread;
  11099. + struct semaphore io_sema;
  11100. +
  11101. + uint32_t trans_id;
  11102. +
  11103. + struct mutex lock;
  11104. + struct list_head cmd_list;
  11105. + struct list_head rsp_list;
  11106. + struct list_head dead_list;
  11107. +
  11108. + struct sm_cmd_rsp_blk free_blk[SM_MAX_NUM_CMD_RSP_BLKS];
  11109. + struct list_head free_list;
  11110. + struct mutex free_lock;
  11111. + struct semaphore free_sema;
  11112. +
  11113. +};
  11114. +
  11115. +/* ---- Private Variables ------------------------------------------------ */
  11116. +
  11117. +/* ---- Private Function Prototypes -------------------------------------- */
  11118. +
  11119. +/* ---- Private Functions ------------------------------------------------ */
  11120. +static struct
  11121. +sm_cmd_rsp_blk *vc_vchi_cmd_create(struct sm_instance *instance,
  11122. + VC_SM_MSG_TYPE id, void *msg,
  11123. + uint32_t size, int wait)
  11124. +{
  11125. + struct sm_cmd_rsp_blk *blk;
  11126. + VC_SM_MSG_HDR_T *hdr;
  11127. +
  11128. + if (down_interruptible(&instance->free_sema)) {
  11129. + blk = kmalloc(sizeof(*blk), GFP_KERNEL);
  11130. + if (!blk)
  11131. + return NULL;
  11132. +
  11133. + blk->alloc = 1;
  11134. + sema_init(&blk->sema, 0);
  11135. + } else {
  11136. + mutex_lock(&instance->free_lock);
  11137. + blk =
  11138. + list_first_entry(&instance->free_list,
  11139. + struct sm_cmd_rsp_blk, head);
  11140. + list_del(&blk->head);
  11141. + mutex_unlock(&instance->free_lock);
  11142. + }
  11143. +
  11144. + blk->sent = 0;
  11145. + blk->wait = wait;
  11146. + blk->length = sizeof(*hdr) + size;
  11147. +
  11148. + hdr = (VC_SM_MSG_HDR_T *) blk->msg;
  11149. + hdr->type = id;
  11150. + mutex_lock(&instance->lock);
  11151. + hdr->trans_id = blk->id = ++instance->trans_id;
  11152. + mutex_unlock(&instance->lock);
  11153. +
  11154. + if (size)
  11155. + memcpy(hdr->body, msg, size);
  11156. +
  11157. + return blk;
  11158. +}
  11159. +
  11160. +static void
  11161. +vc_vchi_cmd_delete(struct sm_instance *instance, struct sm_cmd_rsp_blk *blk)
  11162. +{
  11163. + if (blk->alloc) {
  11164. + kfree(blk);
  11165. + return;
  11166. + }
  11167. +
  11168. + mutex_lock(&instance->free_lock);
  11169. + list_add(&blk->head, &instance->free_list);
  11170. + mutex_unlock(&instance->free_lock);
  11171. + up(&instance->free_sema);
  11172. +}
  11173. +
  11174. +static int vc_vchi_sm_videocore_io(void *arg)
  11175. +{
  11176. + struct sm_instance *instance = arg;
  11177. + struct sm_cmd_rsp_blk *cmd = NULL, *cmd_tmp;
  11178. + VC_SM_RESULT_T *reply;
  11179. + uint32_t reply_len;
  11180. + int32_t status;
  11181. + int svc_use = 1;
  11182. +
  11183. + while (1) {
  11184. + if (svc_use)
  11185. + vchi_service_release(instance->vchi_handle[0]);
  11186. + svc_use = 0;
  11187. + if (!down_interruptible(&instance->io_sema)) {
  11188. + vchi_service_use(instance->vchi_handle[0]);
  11189. + svc_use = 1;
  11190. +
  11191. + do {
  11192. + unsigned int flags;
  11193. + /*
  11194. + * Get new command and move it to response list
  11195. + */
  11196. + mutex_lock(&instance->lock);
  11197. + if (list_empty(&instance->cmd_list)) {
  11198. + /* no more commands to process */
  11199. + mutex_unlock(&instance->lock);
  11200. + break;
  11201. + }
  11202. + cmd =
  11203. + list_first_entry(&instance->cmd_list,
  11204. + struct sm_cmd_rsp_blk,
  11205. + head);
  11206. + list_move(&cmd->head, &instance->rsp_list);
  11207. + cmd->sent = 1;
  11208. + mutex_unlock(&instance->lock);
  11209. +
  11210. + /* Send the command */
  11211. + flags = VCHI_FLAGS_BLOCK_UNTIL_QUEUED;
  11212. + status = vchi_msg_queue(
  11213. + instance->vchi_handle[0],
  11214. + cmd->msg, cmd->length,
  11215. + flags, NULL);
  11216. + if (status) {
  11217. + pr_err("%s: failed to queue message (%d)",
  11218. + __func__, status);
  11219. + }
  11220. +
  11221. + /* If no reply is needed then we're done */
  11222. + if (!cmd->wait) {
  11223. + mutex_lock(&instance->lock);
  11224. + list_del(&cmd->head);
  11225. + mutex_unlock(&instance->lock);
  11226. + vc_vchi_cmd_delete(instance, cmd);
  11227. + continue;
  11228. + }
  11229. +
  11230. + if (status) {
  11231. + up(&cmd->sema);
  11232. + continue;
  11233. + }
  11234. +
  11235. + } while (1);
  11236. +
  11237. + while (!vchi_msg_peek
  11238. + (instance->vchi_handle[0], (void **)&reply,
  11239. + &reply_len, VCHI_FLAGS_NONE)) {
  11240. + mutex_lock(&instance->lock);
  11241. + list_for_each_entry(cmd, &instance->rsp_list,
  11242. + head) {
  11243. + if (cmd->id == reply->trans_id)
  11244. + break;
  11245. + }
  11246. + mutex_unlock(&instance->lock);
  11247. +
  11248. + if (&cmd->head == &instance->rsp_list) {
  11249. + pr_debug("%s: received response %u, throw away...",
  11250. + __func__, reply->trans_id);
  11251. + } else if (reply_len > sizeof(cmd->msg)) {
  11252. + pr_err("%s: reply too big (%u) %u, throw away...",
  11253. + __func__, reply_len,
  11254. + reply->trans_id);
  11255. + } else {
  11256. + memcpy(cmd->msg, reply, reply_len);
  11257. + up(&cmd->sema);
  11258. + }
  11259. +
  11260. + vchi_msg_remove(instance->vchi_handle[0]);
  11261. + }
  11262. +
  11263. + /* Go through the dead list and free them */
  11264. + mutex_lock(&instance->lock);
  11265. + list_for_each_entry_safe(cmd, cmd_tmp,
  11266. + &instance->dead_list, head) {
  11267. + list_del(&cmd->head);
  11268. + vc_vchi_cmd_delete(instance, cmd);
  11269. + }
  11270. + mutex_unlock(&instance->lock);
  11271. + }
  11272. + }
  11273. +
  11274. + return 0;
  11275. +}
  11276. +
  11277. +static void vc_sm_vchi_callback(void *param,
  11278. + const VCHI_CALLBACK_REASON_T reason,
  11279. + void *msg_handle)
  11280. +{
  11281. + struct sm_instance *instance = param;
  11282. +
  11283. + (void)msg_handle;
  11284. +
  11285. + switch (reason) {
  11286. + case VCHI_CALLBACK_MSG_AVAILABLE:
  11287. + up(&instance->io_sema);
  11288. + break;
  11289. +
  11290. + case VCHI_CALLBACK_SERVICE_CLOSED:
  11291. + pr_info("%s: service CLOSED!!", __func__);
  11292. + default:
  11293. + break;
  11294. + }
  11295. +}
  11296. +
  11297. +VC_VCHI_SM_HANDLE_T vc_vchi_sm_init(VCHI_INSTANCE_T vchi_instance,
  11298. + VCHI_CONNECTION_T **vchi_connections,
  11299. + uint32_t num_connections)
  11300. +{
  11301. + uint32_t i;
  11302. + struct sm_instance *instance;
  11303. + int status;
  11304. +
  11305. + pr_debug("%s: start", __func__);
  11306. +
  11307. + if (num_connections > VCHI_MAX_NUM_CONNECTIONS) {
  11308. + pr_err("%s: unsupported number of connections %u (max=%u)",
  11309. + __func__, num_connections, VCHI_MAX_NUM_CONNECTIONS);
  11310. +
  11311. + goto err_null;
  11312. + }
  11313. + /* Allocate memory for this instance */
  11314. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  11315. +
  11316. + /* Misc initialisations */
  11317. + mutex_init(&instance->lock);
  11318. + sema_init(&instance->io_sema, 0);
  11319. + INIT_LIST_HEAD(&instance->cmd_list);
  11320. + INIT_LIST_HEAD(&instance->rsp_list);
  11321. + INIT_LIST_HEAD(&instance->dead_list);
  11322. + INIT_LIST_HEAD(&instance->free_list);
  11323. + sema_init(&instance->free_sema, SM_MAX_NUM_CMD_RSP_BLKS);
  11324. + mutex_init(&instance->free_lock);
  11325. + for (i = 0; i < SM_MAX_NUM_CMD_RSP_BLKS; i++) {
  11326. + sema_init(&instance->free_blk[i].sema, 0);
  11327. + list_add(&instance->free_blk[i].head, &instance->free_list);
  11328. + }
  11329. +
  11330. + /* Open the VCHI service connections */
  11331. + instance->num_connections = num_connections;
  11332. + for (i = 0; i < num_connections; i++) {
  11333. + SERVICE_CREATION_T params = {
  11334. + VCHI_VERSION_EX(VC_SM_VER, VC_SM_MIN_VER),
  11335. + VC_SM_SERVER_NAME,
  11336. + vchi_connections[i],
  11337. + 0,
  11338. + 0,
  11339. + vc_sm_vchi_callback,
  11340. + instance,
  11341. + 0,
  11342. + 0,
  11343. + 0,
  11344. + };
  11345. +
  11346. + status = vchi_service_open(vchi_instance,
  11347. + &params, &instance->vchi_handle[i]);
  11348. + if (status) {
  11349. + pr_err("%s: failed to open VCHI service (%d)",
  11350. + __func__, status);
  11351. +
  11352. + goto err_close_services;
  11353. + }
  11354. + }
  11355. +
  11356. + /* Create the thread which takes care of all io to/from videoocore. */
  11357. + instance->io_thread = kthread_create(&vc_vchi_sm_videocore_io,
  11358. + (void *)instance, "SMIO");
  11359. + if (instance->io_thread == NULL) {
  11360. + pr_err("%s: failed to create SMIO thread", __func__);
  11361. +
  11362. + goto err_close_services;
  11363. + }
  11364. + set_user_nice(instance->io_thread, -10);
  11365. + wake_up_process(instance->io_thread);
  11366. +
  11367. + pr_debug("%s: success - instance 0x%x", __func__, (unsigned)instance);
  11368. + return instance;
  11369. +
  11370. +err_close_services:
  11371. + for (i = 0; i < instance->num_connections; i++) {
  11372. + if (instance->vchi_handle[i] != NULL)
  11373. + vchi_service_close(instance->vchi_handle[i]);
  11374. + }
  11375. + kfree(instance);
  11376. +err_null:
  11377. + pr_debug("%s: FAILED", __func__);
  11378. + return NULL;
  11379. +}
  11380. +
  11381. +int vc_vchi_sm_stop(VC_VCHI_SM_HANDLE_T *handle)
  11382. +{
  11383. + struct sm_instance *instance;
  11384. + uint32_t i;
  11385. +
  11386. + if (handle == NULL) {
  11387. + pr_err("%s: invalid pointer to handle %p", __func__, handle);
  11388. + goto lock;
  11389. + }
  11390. +
  11391. + if (*handle == NULL) {
  11392. + pr_err("%s: invalid handle %p", __func__, *handle);
  11393. + goto lock;
  11394. + }
  11395. +
  11396. + instance = *handle;
  11397. +
  11398. + /* Close all VCHI service connections */
  11399. + for (i = 0; i < instance->num_connections; i++) {
  11400. + int32_t success;
  11401. + vchi_service_use(instance->vchi_handle[i]);
  11402. +
  11403. + success = vchi_service_close(instance->vchi_handle[i]);
  11404. + }
  11405. +
  11406. + kfree(instance);
  11407. +
  11408. + *handle = NULL;
  11409. + return 0;
  11410. +
  11411. +lock:
  11412. + return -EINVAL;
  11413. +}
  11414. +
  11415. +int vc_vchi_sm_send_msg(VC_VCHI_SM_HANDLE_T handle,
  11416. + VC_SM_MSG_TYPE msg_id,
  11417. + void *msg, uint32_t msg_size,
  11418. + void *result, uint32_t result_size,
  11419. + uint32_t *cur_trans_id, uint8_t wait_reply)
  11420. +{
  11421. + int status = 0;
  11422. + struct sm_instance *instance = handle;
  11423. + struct sm_cmd_rsp_blk *cmd_blk;
  11424. +
  11425. + if (handle == NULL) {
  11426. + pr_err("%s: invalid handle", __func__);
  11427. + return -EINVAL;
  11428. + }
  11429. + if (msg == NULL) {
  11430. + pr_err("%s: invalid msg pointer", __func__);
  11431. + return -EINVAL;
  11432. + }
  11433. +
  11434. + cmd_blk =
  11435. + vc_vchi_cmd_create(instance, msg_id, msg, msg_size, wait_reply);
  11436. + if (cmd_blk == NULL) {
  11437. + pr_err("[%s]: failed to allocate global tracking resource",
  11438. + __func__);
  11439. + return -ENOMEM;
  11440. + }
  11441. +
  11442. + if (cur_trans_id != NULL)
  11443. + *cur_trans_id = cmd_blk->id;
  11444. +
  11445. + mutex_lock(&instance->lock);
  11446. + list_add_tail(&cmd_blk->head, &instance->cmd_list);
  11447. + mutex_unlock(&instance->lock);
  11448. + up(&instance->io_sema);
  11449. +
  11450. + if (!wait_reply)
  11451. + /* We're done */
  11452. + return 0;
  11453. +
  11454. + /* Wait for the response */
  11455. + if (down_interruptible(&cmd_blk->sema)) {
  11456. + mutex_lock(&instance->lock);
  11457. + if (!cmd_blk->sent) {
  11458. + list_del(&cmd_blk->head);
  11459. + mutex_unlock(&instance->lock);
  11460. + vc_vchi_cmd_delete(instance, cmd_blk);
  11461. + return -ENXIO;
  11462. + }
  11463. + mutex_unlock(&instance->lock);
  11464. +
  11465. + mutex_lock(&instance->lock);
  11466. + list_move(&cmd_blk->head, &instance->dead_list);
  11467. + mutex_unlock(&instance->lock);
  11468. + up(&instance->io_sema);
  11469. + return -EINTR; /* We're done */
  11470. + }
  11471. +
  11472. + if (result && result_size) {
  11473. + memcpy(result, cmd_blk->msg, result_size);
  11474. + } else {
  11475. + VC_SM_RESULT_T *res = (VC_SM_RESULT_T *) cmd_blk->msg;
  11476. + status = (res->success == 0) ? 0 : -ENXIO;
  11477. + }
  11478. +
  11479. + mutex_lock(&instance->lock);
  11480. + list_del(&cmd_blk->head);
  11481. + mutex_unlock(&instance->lock);
  11482. + vc_vchi_cmd_delete(instance, cmd_blk);
  11483. + return status;
  11484. +}
  11485. +
  11486. +int vc_vchi_sm_alloc(VC_VCHI_SM_HANDLE_T handle, VC_SM_ALLOC_T *msg,
  11487. + VC_SM_ALLOC_RESULT_T *result, uint32_t *cur_trans_id)
  11488. +{
  11489. + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_ALLOC,
  11490. + msg, sizeof(*msg), result, sizeof(*result),
  11491. + cur_trans_id, 1);
  11492. +}
  11493. +
  11494. +int vc_vchi_sm_free(VC_VCHI_SM_HANDLE_T handle,
  11495. + VC_SM_FREE_T *msg, uint32_t *cur_trans_id)
  11496. +{
  11497. + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_FREE,
  11498. + msg, sizeof(*msg), 0, 0, cur_trans_id, 0);
  11499. +}
  11500. +
  11501. +int vc_vchi_sm_lock(VC_VCHI_SM_HANDLE_T handle,
  11502. + VC_SM_LOCK_UNLOCK_T *msg,
  11503. + VC_SM_LOCK_RESULT_T *result, uint32_t *cur_trans_id)
  11504. +{
  11505. + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_LOCK,
  11506. + msg, sizeof(*msg), result, sizeof(*result),
  11507. + cur_trans_id, 1);
  11508. +}
  11509. +
  11510. +int vc_vchi_sm_unlock(VC_VCHI_SM_HANDLE_T handle,
  11511. + VC_SM_LOCK_UNLOCK_T *msg,
  11512. + uint32_t *cur_trans_id, uint8_t wait_reply)
  11513. +{
  11514. + return vc_vchi_sm_send_msg(handle, wait_reply ?
  11515. + VC_SM_MSG_TYPE_UNLOCK :
  11516. + VC_SM_MSG_TYPE_UNLOCK_NOANS, msg,
  11517. + sizeof(*msg), 0, 0, cur_trans_id,
  11518. + wait_reply);
  11519. +}
  11520. +
  11521. +int vc_vchi_sm_resize(VC_VCHI_SM_HANDLE_T handle, VC_SM_RESIZE_T *msg,
  11522. + uint32_t *cur_trans_id)
  11523. +{
  11524. + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_RESIZE,
  11525. + msg, sizeof(*msg), 0, 0, cur_trans_id, 1);
  11526. +}
  11527. +
  11528. +int vc_vchi_sm_walk_alloc(VC_VCHI_SM_HANDLE_T handle)
  11529. +{
  11530. + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_WALK_ALLOC,
  11531. + 0, 0, 0, 0, 0, 0);
  11532. +}
  11533. +
  11534. +int vc_vchi_sm_clean_up(VC_VCHI_SM_HANDLE_T handle, VC_SM_ACTION_CLEAN_T *msg)
  11535. +{
  11536. + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_ACTION_CLEAN,
  11537. + msg, sizeof(*msg), 0, 0, 0, 0);
  11538. +}
  11539. diff -Nur linux-3.12.33/drivers/char/broadcom/vc_sm/vmcs_sm.c linux-3.12.33-rpi/drivers/char/broadcom/vc_sm/vmcs_sm.c
  11540. --- linux-3.12.33/drivers/char/broadcom/vc_sm/vmcs_sm.c 1969-12-31 18:00:00.000000000 -0600
  11541. +++ linux-3.12.33-rpi/drivers/char/broadcom/vc_sm/vmcs_sm.c 2014-12-03 19:13:34.236418001 -0600
  11542. @@ -0,0 +1,3163 @@
  11543. +/*****************************************************************************
  11544. +* Copyright 2011-2012 Broadcom Corporation. All rights reserved.
  11545. +*
  11546. +* Unless you and Broadcom execute a separate written software license
  11547. +* agreement governing use of this software, this software is licensed to you
  11548. +* under the terms of the GNU General Public License version 2, available at
  11549. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  11550. +*
  11551. +* Notwithstanding the above, under no circumstances may you combine this
  11552. +* software in any way with any other Broadcom software provided under a
  11553. +* license other than the GPL, without Broadcom's express prior written
  11554. +* consent.
  11555. +*****************************************************************************/
  11556. +
  11557. +/* ---- Include Files ----------------------------------------------------- */
  11558. +
  11559. +#include <linux/cdev.h>
  11560. +#include <linux/device.h>
  11561. +#include <linux/debugfs.h>
  11562. +#include <linux/dma-mapping.h>
  11563. +#include <linux/errno.h>
  11564. +#include <linux/fs.h>
  11565. +#include <linux/hugetlb.h>
  11566. +#include <linux/ioctl.h>
  11567. +#include <linux/kernel.h>
  11568. +#include <linux/list.h>
  11569. +#include <linux/module.h>
  11570. +#include <linux/mm.h>
  11571. +#include <linux/pfn.h>
  11572. +#include <linux/proc_fs.h>
  11573. +#include <linux/pagemap.h>
  11574. +#include <linux/semaphore.h>
  11575. +#include <linux/slab.h>
  11576. +#include <linux/seq_file.h>
  11577. +#include <linux/types.h>
  11578. +#include <asm/cacheflush.h>
  11579. +
  11580. +#include <vc_mem.h>
  11581. +
  11582. +#include "vchiq_connected.h"
  11583. +#include "vc_vchi_sm.h"
  11584. +
  11585. +#include <vmcs_sm_ioctl.h>
  11586. +#include "vc_sm_knl.h"
  11587. +
  11588. +/* ---- Private Constants and Types --------------------------------------- */
  11589. +
  11590. +#define DEVICE_NAME "vcsm"
  11591. +#define DEVICE_MINOR 0
  11592. +
  11593. +#define VC_SM_DIR_ROOT_NAME "vc-smem"
  11594. +#define VC_SM_DIR_ALLOC_NAME "alloc"
  11595. +#define VC_SM_STATE "state"
  11596. +#define VC_SM_STATS "statistics"
  11597. +#define VC_SM_RESOURCES "resources"
  11598. +#define VC_SM_DEBUG "debug"
  11599. +#define VC_SM_WRITE_BUF_SIZE 128
  11600. +
  11601. +/* Statistics tracked per resource and globally.
  11602. +*/
  11603. +enum SM_STATS_T {
  11604. + /* Attempt. */
  11605. + ALLOC,
  11606. + FREE,
  11607. + LOCK,
  11608. + UNLOCK,
  11609. + MAP,
  11610. + FLUSH,
  11611. + INVALID,
  11612. +
  11613. + END_ATTEMPT,
  11614. +
  11615. + /* Failure. */
  11616. + ALLOC_FAIL,
  11617. + FREE_FAIL,
  11618. + LOCK_FAIL,
  11619. + UNLOCK_FAIL,
  11620. + MAP_FAIL,
  11621. + FLUSH_FAIL,
  11622. + INVALID_FAIL,
  11623. +
  11624. + END_ALL,
  11625. +
  11626. +};
  11627. +
  11628. +static const char *const sm_stats_human_read[] = {
  11629. + "Alloc",
  11630. + "Free",
  11631. + "Lock",
  11632. + "Unlock",
  11633. + "Map",
  11634. + "Cache Flush",
  11635. + "Cache Invalidate",
  11636. +};
  11637. +
  11638. +typedef int (*VC_SM_SHOW) (struct seq_file *s, void *v);
  11639. +struct SM_PDE_T {
  11640. + VC_SM_SHOW show; /* Debug fs function hookup. */
  11641. + struct dentry *dir_entry; /* Debug fs directory entry. */
  11642. + void *priv_data; /* Private data */
  11643. +
  11644. +};
  11645. +
  11646. +/* Single resource allocation tracked for all devices.
  11647. +*/
  11648. +struct sm_mmap {
  11649. + struct list_head map_list; /* Linked list of maps. */
  11650. +
  11651. + struct SM_RESOURCE_T *resource; /* Pointer to the resource. */
  11652. +
  11653. + pid_t res_pid; /* PID owning that resource. */
  11654. + unsigned int res_vc_hdl; /* Resource handle (videocore). */
  11655. + unsigned int res_usr_hdl; /* Resource handle (user). */
  11656. +
  11657. + long unsigned int res_addr; /* Mapped virtual address. */
  11658. + struct vm_area_struct *vma; /* VM area for this mapping. */
  11659. + unsigned int ref_count; /* Reference count to this vma. */
  11660. +
  11661. + /* Used to link maps associated with a resource. */
  11662. + struct list_head resource_map_list;
  11663. +};
  11664. +
  11665. +/* Single resource allocation tracked for each opened device.
  11666. +*/
  11667. +struct SM_RESOURCE_T {
  11668. + struct list_head resource_list; /* List of resources. */
  11669. + struct list_head global_resource_list; /* Global list of resources. */
  11670. +
  11671. + pid_t pid; /* PID owning that resource. */
  11672. + uint32_t res_guid; /* Unique identifier. */
  11673. + uint32_t lock_count; /* Lock count for this resource. */
  11674. + uint32_t ref_count; /* Ref count for this resource. */
  11675. +
  11676. + uint32_t res_handle; /* Resource allocation handle. */
  11677. + void *res_base_mem; /* Resource base memory address. */
  11678. + uint32_t res_size; /* Resource size allocated. */
  11679. + enum vmcs_sm_cache_e res_cached; /* Resource cache type. */
  11680. + struct SM_RESOURCE_T *res_shared; /* Shared resource */
  11681. +
  11682. + enum SM_STATS_T res_stats[END_ALL]; /* Resource statistics. */
  11683. +
  11684. + uint8_t map_count; /* Counter of mappings for this resource. */
  11685. + struct list_head map_list; /* Maps associated with a resource. */
  11686. +
  11687. + struct SM_PRIV_DATA_T *private;
  11688. +};
  11689. +
  11690. +/* Private file data associated with each opened device.
  11691. +*/
  11692. +struct SM_PRIV_DATA_T {
  11693. + struct list_head resource_list; /* List of resources. */
  11694. +
  11695. + pid_t pid; /* PID of creator. */
  11696. +
  11697. + struct dentry *dir_pid; /* Debug fs entries root. */
  11698. + struct SM_PDE_T dir_stats; /* Debug fs entries statistics sub-tree. */
  11699. + struct SM_PDE_T dir_res; /* Debug fs resource sub-tree. */
  11700. +
  11701. + int restart_sys; /* Tracks restart on interrupt. */
  11702. + VC_SM_MSG_TYPE int_action; /* Interrupted action. */
  11703. + uint32_t int_trans_id; /* Interrupted transaction. */
  11704. +
  11705. +};
  11706. +
  11707. +/* Global state information.
  11708. +*/
  11709. +struct SM_STATE_T {
  11710. + VC_VCHI_SM_HANDLE_T sm_handle; /* Handle for videocore service. */
  11711. + struct dentry *dir_root; /* Debug fs entries root. */
  11712. + struct dentry *dir_alloc; /* Debug fs entries allocations. */
  11713. + struct SM_PDE_T dir_stats; /* Debug fs entries statistics sub-tree. */
  11714. + struct SM_PDE_T dir_state; /* Debug fs entries state sub-tree. */
  11715. + struct dentry *debug; /* Debug fs entries debug. */
  11716. +
  11717. + struct mutex map_lock; /* Global map lock. */
  11718. + struct list_head map_list; /* List of maps. */
  11719. + struct list_head resource_list; /* List of resources. */
  11720. +
  11721. + enum SM_STATS_T deceased[END_ALL]; /* Natural termination stats. */
  11722. + enum SM_STATS_T terminated[END_ALL]; /* Forced termination stats. */
  11723. + uint32_t res_deceased_cnt; /* Natural termination counter. */
  11724. + uint32_t res_terminated_cnt; /* Forced termination counter. */
  11725. +
  11726. + struct cdev sm_cdev; /* Device. */
  11727. + dev_t sm_devid; /* Device identifier. */
  11728. + struct class *sm_class; /* Class. */
  11729. + struct device *sm_dev; /* Device. */
  11730. +
  11731. + struct SM_PRIV_DATA_T *data_knl; /* Kernel internal data tracking. */
  11732. +
  11733. + struct mutex lock; /* Global lock. */
  11734. + uint32_t guid; /* GUID (next) tracker. */
  11735. +
  11736. +};
  11737. +
  11738. +/* ---- Private Variables ----------------------------------------------- */
  11739. +
  11740. +static struct SM_STATE_T *sm_state;
  11741. +static int sm_inited;
  11742. +
  11743. +static const char *const sm_cache_map_vector[] = {
  11744. + "(null)",
  11745. + "host",
  11746. + "videocore",
  11747. + "host+videocore",
  11748. +};
  11749. +
  11750. +/* ---- Private Function Prototypes -------------------------------------- */
  11751. +
  11752. +/* ---- Private Functions ------------------------------------------------ */
  11753. +
  11754. +static inline unsigned vcaddr_to_pfn(unsigned long vc_addr)
  11755. +{
  11756. + unsigned long pfn = vc_addr & 0x3FFFFFFF;
  11757. + pfn += mm_vc_mem_phys_addr;
  11758. + pfn >>= PAGE_SHIFT;
  11759. + return pfn;
  11760. +}
  11761. +
  11762. +/* Carries over to the state statistics the statistics once owned by a deceased
  11763. +** resource.
  11764. +*/
  11765. +static void vc_sm_resource_deceased(struct SM_RESOURCE_T *p_res, int terminated)
  11766. +{
  11767. + if (sm_state != NULL) {
  11768. + if (p_res != NULL) {
  11769. + int ix;
  11770. +
  11771. + if (terminated)
  11772. + sm_state->res_terminated_cnt++;
  11773. + else
  11774. + sm_state->res_deceased_cnt++;
  11775. +
  11776. + for (ix = 0; ix < END_ALL; ix++) {
  11777. + if (terminated)
  11778. + sm_state->terminated[ix] +=
  11779. + p_res->res_stats[ix];
  11780. + else
  11781. + sm_state->deceased[ix] +=
  11782. + p_res->res_stats[ix];
  11783. + }
  11784. + }
  11785. + }
  11786. +}
  11787. +
  11788. +/* Fetch a videocore handle corresponding to a mapping of the pid+address
  11789. +** returns 0 (ie NULL) if no such handle exists in the global map.
  11790. +*/
  11791. +static unsigned int vmcs_sm_vc_handle_from_pid_and_address(unsigned int pid,
  11792. + unsigned int addr)
  11793. +{
  11794. + struct sm_mmap *map = NULL;
  11795. + unsigned int handle = 0;
  11796. +
  11797. + if (!sm_state || addr == 0)
  11798. + goto out;
  11799. +
  11800. + mutex_lock(&(sm_state->map_lock));
  11801. +
  11802. + /* Lookup the resource.
  11803. + */
  11804. + if (!list_empty(&sm_state->map_list)) {
  11805. + list_for_each_entry(map, &sm_state->map_list, map_list) {
  11806. + if (map->res_pid != pid || map->res_addr != addr)
  11807. + continue;
  11808. +
  11809. + pr_debug("[%s]: global map %p (pid %u, addr %lx) -> vc-hdl %x (usr-hdl %x)\n",
  11810. + __func__, map, map->res_pid, map->res_addr,
  11811. + map->res_vc_hdl, map->res_usr_hdl);
  11812. +
  11813. + handle = map->res_vc_hdl;
  11814. + break;
  11815. + }
  11816. + }
  11817. +
  11818. + mutex_unlock(&(sm_state->map_lock));
  11819. +
  11820. +out:
  11821. + /* Use a debug log here as it may be a valid situation that we query
  11822. + ** for something that is not mapped, we do not want a kernel log each
  11823. + ** time around.
  11824. + **
  11825. + ** There are other error log that would pop up accordingly if someone
  11826. + ** subsequently tries to use something invalid after being told not to
  11827. + ** use it...
  11828. + */
  11829. + if (handle == 0) {
  11830. + pr_debug("[%s]: not a valid map (pid %u, addr %x)\n",
  11831. + __func__, pid, addr);
  11832. + }
  11833. +
  11834. + return handle;
  11835. +}
  11836. +
  11837. +/* Fetch a user handle corresponding to a mapping of the pid+address
  11838. +** returns 0 (ie NULL) if no such handle exists in the global map.
  11839. +*/
  11840. +static unsigned int vmcs_sm_usr_handle_from_pid_and_address(unsigned int pid,
  11841. + unsigned int addr)
  11842. +{
  11843. + struct sm_mmap *map = NULL;
  11844. + unsigned int handle = 0;
  11845. +
  11846. + if (!sm_state || addr == 0)
  11847. + goto out;
  11848. +
  11849. + mutex_lock(&(sm_state->map_lock));
  11850. +
  11851. + /* Lookup the resource.
  11852. + */
  11853. + if (!list_empty(&sm_state->map_list)) {
  11854. + list_for_each_entry(map, &sm_state->map_list, map_list) {
  11855. + if (map->res_pid != pid || map->res_addr != addr)
  11856. + continue;
  11857. +
  11858. + pr_debug("[%s]: global map %p (pid %u, addr %lx) -> usr-hdl %x (vc-hdl %x)\n",
  11859. + __func__, map, map->res_pid, map->res_addr,
  11860. + map->res_usr_hdl, map->res_vc_hdl);
  11861. +
  11862. + handle = map->res_usr_hdl;
  11863. + break;
  11864. + }
  11865. + }
  11866. +
  11867. + mutex_unlock(&(sm_state->map_lock));
  11868. +
  11869. +out:
  11870. + /* Use a debug log here as it may be a valid situation that we query
  11871. + * for something that is not mapped yet.
  11872. + *
  11873. + * There are other error log that would pop up accordingly if someone
  11874. + * subsequently tries to use something invalid after being told not to
  11875. + * use it...
  11876. + */
  11877. + if (handle == 0)
  11878. + pr_debug("[%s]: not a valid map (pid %u, addr %x)\n",
  11879. + __func__, pid, addr);
  11880. +
  11881. + return handle;
  11882. +}
  11883. +
  11884. +#if defined(DO_NOT_USE)
  11885. +/* Fetch an address corresponding to a mapping of the pid+handle
  11886. +** returns 0 (ie NULL) if no such address exists in the global map.
  11887. +*/
  11888. +static unsigned int vmcs_sm_usr_address_from_pid_and_vc_handle(unsigned int pid,
  11889. + unsigned int hdl)
  11890. +{
  11891. + struct sm_mmap *map = NULL;
  11892. + unsigned int addr = 0;
  11893. +
  11894. + if (sm_state == NULL || hdl == 0)
  11895. + goto out;
  11896. +
  11897. + mutex_lock(&(sm_state->map_lock));
  11898. +
  11899. + /* Lookup the resource.
  11900. + */
  11901. + if (!list_empty(&sm_state->map_list)) {
  11902. + list_for_each_entry(map, &sm_state->map_list, map_list) {
  11903. + if (map->res_pid != pid || map->res_vc_hdl != hdl)
  11904. + continue;
  11905. +
  11906. + pr_debug("[%s]: global map %p (pid %u, vc-hdl %x, usr-hdl %x) -> addr %lx\n",
  11907. + __func__, map, map->res_pid, map->res_vc_hdl,
  11908. + map->res_usr_hdl, map->res_addr);
  11909. +
  11910. + addr = map->res_addr;
  11911. + break;
  11912. + }
  11913. + }
  11914. +
  11915. + mutex_unlock(&(sm_state->map_lock));
  11916. +
  11917. +out:
  11918. + /* Use a debug log here as it may be a valid situation that we query
  11919. + ** for something that is not mapped, we do not want a kernel log each
  11920. + ** time around.
  11921. + **
  11922. + ** There are other error log that would pop up accordingly if someone
  11923. + ** subsequently tries to use something invalid after being told not to
  11924. + ** use it...
  11925. + */
  11926. + if (addr == 0)
  11927. + pr_debug("[%s]: not a valid map (pid %u, hdl %x)\n",
  11928. + __func__, pid, hdl);
  11929. +
  11930. + return addr;
  11931. +}
  11932. +#endif
  11933. +
  11934. +/* Fetch an address corresponding to a mapping of the pid+handle
  11935. +** returns 0 (ie NULL) if no such address exists in the global map.
  11936. +*/
  11937. +static unsigned int vmcs_sm_usr_address_from_pid_and_usr_handle(unsigned int
  11938. + pid,
  11939. + unsigned int
  11940. + hdl)
  11941. +{
  11942. + struct sm_mmap *map = NULL;
  11943. + unsigned int addr = 0;
  11944. +
  11945. + if (sm_state == NULL || hdl == 0)
  11946. + goto out;
  11947. +
  11948. + mutex_lock(&(sm_state->map_lock));
  11949. +
  11950. + /* Lookup the resource.
  11951. + */
  11952. + if (!list_empty(&sm_state->map_list)) {
  11953. + list_for_each_entry(map, &sm_state->map_list, map_list) {
  11954. + if (map->res_pid != pid || map->res_usr_hdl != hdl)
  11955. + continue;
  11956. +
  11957. + pr_debug("[%s]: global map %p (pid %u, vc-hdl %x, usr-hdl %x) -> addr %lx\n",
  11958. + __func__, map, map->res_pid, map->res_vc_hdl,
  11959. + map->res_usr_hdl, map->res_addr);
  11960. +
  11961. + addr = map->res_addr;
  11962. + break;
  11963. + }
  11964. + }
  11965. +
  11966. + mutex_unlock(&(sm_state->map_lock));
  11967. +
  11968. +out:
  11969. + /* Use a debug log here as it may be a valid situation that we query
  11970. + * for something that is not mapped, we do not want a kernel log each
  11971. + * time around.
  11972. + *
  11973. + * There are other error log that would pop up accordingly if someone
  11974. + * subsequently tries to use something invalid after being told not to
  11975. + * use it...
  11976. + */
  11977. + if (addr == 0)
  11978. + pr_debug("[%s]: not a valid map (pid %u, hdl %x)\n", __func__,
  11979. + pid, hdl);
  11980. +
  11981. + return addr;
  11982. +}
  11983. +
  11984. +/* Adds a resource mapping to the global data list.
  11985. +*/
  11986. +static void vmcs_sm_add_map(struct SM_STATE_T *state,
  11987. + struct SM_RESOURCE_T *resource, struct sm_mmap *map)
  11988. +{
  11989. + mutex_lock(&(state->map_lock));
  11990. +
  11991. + /* Add to the global list of mappings
  11992. + */
  11993. + list_add(&map->map_list, &state->map_list);
  11994. +
  11995. + /* Add to the list of mappings for this resource
  11996. + */
  11997. + list_add(&map->resource_map_list, &resource->map_list);
  11998. + resource->map_count++;
  11999. +
  12000. + mutex_unlock(&(state->map_lock));
  12001. +
  12002. + pr_debug("[%s]: added map %p (pid %u, vc-hdl %x, usr-hdl %x, addr %lx)\n",
  12003. + __func__, map, map->res_pid, map->res_vc_hdl,
  12004. + map->res_usr_hdl, map->res_addr);
  12005. +}
  12006. +
  12007. +/* Removes a resource mapping from the global data list.
  12008. +*/
  12009. +static void vmcs_sm_remove_map(struct SM_STATE_T *state,
  12010. + struct SM_RESOURCE_T *resource,
  12011. + struct sm_mmap *map)
  12012. +{
  12013. + mutex_lock(&(state->map_lock));
  12014. +
  12015. + /* Remove from the global list of mappings
  12016. + */
  12017. + list_del(&map->map_list);
  12018. +
  12019. + /* Remove from the list of mapping for this resource
  12020. + */
  12021. + list_del(&map->resource_map_list);
  12022. + if (resource->map_count > 0)
  12023. + resource->map_count--;
  12024. +
  12025. + mutex_unlock(&(state->map_lock));
  12026. +
  12027. + pr_debug("[%s]: removed map %p (pid %d, vc-hdl %x, usr-hdl %x, addr %lx)\n",
  12028. + __func__, map, map->res_pid, map->res_vc_hdl, map->res_usr_hdl,
  12029. + map->res_addr);
  12030. +
  12031. + kfree(map);
  12032. +}
  12033. +
  12034. +/* Read callback for the global state proc entry.
  12035. +*/
  12036. +static int vc_sm_global_state_show(struct seq_file *s, void *v)
  12037. +{
  12038. + struct sm_mmap *map = NULL;
  12039. + int map_count = 0;
  12040. +
  12041. + if (sm_state == NULL)
  12042. + return 0;
  12043. +
  12044. + seq_printf(s, "\nVC-ServiceHandle 0x%x\n",
  12045. + (unsigned int)sm_state->sm_handle);
  12046. +
  12047. + /* Log all applicable mapping(s).
  12048. + */
  12049. +
  12050. + mutex_lock(&(sm_state->map_lock));
  12051. +
  12052. + if (!list_empty(&sm_state->map_list)) {
  12053. + list_for_each_entry(map, &sm_state->map_list, map_list) {
  12054. + map_count++;
  12055. +
  12056. + seq_printf(s, "\nMapping 0x%x\n",
  12057. + (unsigned int)map);
  12058. + seq_printf(s, " TGID %u\n",
  12059. + map->res_pid);
  12060. + seq_printf(s, " VC-HDL 0x%x\n",
  12061. + map->res_vc_hdl);
  12062. + seq_printf(s, " USR-HDL 0x%x\n",
  12063. + map->res_usr_hdl);
  12064. + seq_printf(s, " USR-ADDR 0x%lx\n",
  12065. + map->res_addr);
  12066. + }
  12067. + }
  12068. +
  12069. + mutex_unlock(&(sm_state->map_lock));
  12070. + seq_printf(s, "\n\nTotal map count: %d\n\n", map_count);
  12071. +
  12072. + return 0;
  12073. +}
  12074. +
  12075. +static int vc_sm_global_statistics_show(struct seq_file *s, void *v)
  12076. +{
  12077. + int ix;
  12078. +
  12079. + /* Global state tracked statistics.
  12080. + */
  12081. + if (sm_state != NULL) {
  12082. + seq_puts(s, "\nDeceased Resources Statistics\n");
  12083. +
  12084. + seq_printf(s, "\nNatural Cause (%u occurences)\n",
  12085. + sm_state->res_deceased_cnt);
  12086. + for (ix = 0; ix < END_ATTEMPT; ix++) {
  12087. + if (sm_state->deceased[ix] > 0) {
  12088. + seq_printf(s, " %u\t%s\n",
  12089. + sm_state->deceased[ix],
  12090. + sm_stats_human_read[ix]);
  12091. + }
  12092. + }
  12093. + seq_puts(s, "\n");
  12094. + for (ix = 0; ix < END_ATTEMPT; ix++) {
  12095. + if (sm_state->deceased[ix + END_ATTEMPT] > 0) {
  12096. + seq_printf(s, " %u\tFAILED %s\n",
  12097. + sm_state->deceased[ix + END_ATTEMPT],
  12098. + sm_stats_human_read[ix]);
  12099. + }
  12100. + }
  12101. +
  12102. + seq_printf(s, "\nForcefull (%u occurences)\n",
  12103. + sm_state->res_terminated_cnt);
  12104. + for (ix = 0; ix < END_ATTEMPT; ix++) {
  12105. + if (sm_state->terminated[ix] > 0) {
  12106. + seq_printf(s, " %u\t%s\n",
  12107. + sm_state->terminated[ix],
  12108. + sm_stats_human_read[ix]);
  12109. + }
  12110. + }
  12111. + seq_puts(s, "\n");
  12112. + for (ix = 0; ix < END_ATTEMPT; ix++) {
  12113. + if (sm_state->terminated[ix + END_ATTEMPT] > 0) {
  12114. + seq_printf(s, " %u\tFAILED %s\n",
  12115. + sm_state->terminated[ix +
  12116. + END_ATTEMPT],
  12117. + sm_stats_human_read[ix]);
  12118. + }
  12119. + }
  12120. + }
  12121. +
  12122. + return 0;
  12123. +}
  12124. +
  12125. +#if 0
  12126. +/* Read callback for the statistics proc entry.
  12127. +*/
  12128. +static int vc_sm_statistics_show(struct seq_file *s, void *v)
  12129. +{
  12130. + int ix;
  12131. + struct SM_PRIV_DATA_T *file_data;
  12132. + struct SM_RESOURCE_T *resource;
  12133. + int res_count = 0;
  12134. + struct SM_PDE_T *p_pde;
  12135. +
  12136. + p_pde = (struct SM_PDE_T *)(s->private);
  12137. + file_data = (struct SM_PRIV_DATA_T *)(p_pde->priv_data);
  12138. +
  12139. + if (file_data == NULL)
  12140. + return 0;
  12141. +
  12142. + /* Per process statistics.
  12143. + */
  12144. +
  12145. + seq_printf(s, "\nStatistics for TGID %d\n", file_data->pid);
  12146. +
  12147. + mutex_lock(&(sm_state->map_lock));
  12148. +
  12149. + if (!list_empty(&file_data->resource_list)) {
  12150. + list_for_each_entry(resource, &file_data->resource_list,
  12151. + resource_list) {
  12152. + res_count++;
  12153. +
  12154. + seq_printf(s, "\nGUID: 0x%x\n\n",
  12155. + resource->res_guid);
  12156. + for (ix = 0; ix < END_ATTEMPT; ix++) {
  12157. + if (resource->res_stats[ix] > 0) {
  12158. + seq_printf(s,
  12159. + " %u\t%s\n",
  12160. + resource->res_stats[ix],
  12161. + sm_stats_human_read[ix]);
  12162. + }
  12163. + }
  12164. + seq_puts(s, "\n");
  12165. + for (ix = 0; ix < END_ATTEMPT; ix++) {
  12166. + if (resource->res_stats[ix + END_ATTEMPT] > 0) {
  12167. + seq_printf(s,
  12168. + " %u\tFAILED %s\n",
  12169. + resource->res_stats[
  12170. + ix + END_ATTEMPT],
  12171. + sm_stats_human_read[ix]);
  12172. + }
  12173. + }
  12174. + }
  12175. + }
  12176. +
  12177. + mutex_unlock(&(sm_state->map_lock));
  12178. +
  12179. + seq_printf(s, "\nResources Count %d\n", res_count);
  12180. +
  12181. + return 0;
  12182. +}
  12183. +#endif
  12184. +
  12185. +#if 0
  12186. +/* Read callback for the allocation proc entry. */
  12187. +static int vc_sm_alloc_show(struct seq_file *s, void *v)
  12188. +{
  12189. + struct SM_PRIV_DATA_T *file_data;
  12190. + struct SM_RESOURCE_T *resource;
  12191. + int alloc_count = 0;
  12192. + struct SM_PDE_T *p_pde;
  12193. +
  12194. + p_pde = (struct SM_PDE_T *)(s->private);
  12195. + file_data = (struct SM_PRIV_DATA_T *)(p_pde->priv_data);
  12196. +
  12197. + if (!file_data)
  12198. + return 0;
  12199. +
  12200. + /* Per process statistics. */
  12201. + seq_printf(s, "\nAllocation for TGID %d\n", file_data->pid);
  12202. +
  12203. + mutex_lock(&(sm_state->map_lock));
  12204. +
  12205. + if (!list_empty(&file_data->resource_list)) {
  12206. + list_for_each_entry(resource, &file_data->resource_list,
  12207. + resource_list) {
  12208. + alloc_count++;
  12209. +
  12210. + seq_printf(s, "\nGUID: 0x%x\n",
  12211. + resource->res_guid);
  12212. + seq_printf(s, "Lock Count: %u\n",
  12213. + resource->lock_count);
  12214. + seq_printf(s, "Mapped: %s\n",
  12215. + (resource->map_count ? "yes" : "no"));
  12216. + seq_printf(s, "VC-handle: 0x%x\n",
  12217. + resource->res_handle);
  12218. + seq_printf(s, "VC-address: 0x%p\n",
  12219. + resource->res_base_mem);
  12220. + seq_printf(s, "VC-size (bytes): %u\n",
  12221. + resource->res_size);
  12222. + seq_printf(s, "Cache: %s\n",
  12223. + sm_cache_map_vector[resource->res_cached]);
  12224. + }
  12225. + }
  12226. +
  12227. + mutex_unlock(&(sm_state->map_lock));
  12228. +
  12229. + seq_printf(s, "\n\nTotal allocation count: %d\n\n", alloc_count);
  12230. +
  12231. + return 0;
  12232. +}
  12233. +#endif
  12234. +
  12235. +static int vc_sm_seq_file_show(struct seq_file *s, void *v)
  12236. +{
  12237. + struct SM_PDE_T *sm_pde;
  12238. +
  12239. + sm_pde = (struct SM_PDE_T *)(s->private);
  12240. +
  12241. + if (sm_pde && sm_pde->show)
  12242. + sm_pde->show(s, v);
  12243. +
  12244. + return 0;
  12245. +}
  12246. +
  12247. +static int vc_sm_single_open(struct inode *inode, struct file *file)
  12248. +{
  12249. + return single_open(file, vc_sm_seq_file_show, inode->i_private);
  12250. +}
  12251. +
  12252. +static const struct file_operations vc_sm_debug_fs_fops = {
  12253. + .open = vc_sm_single_open,
  12254. + .read = seq_read,
  12255. + .llseek = seq_lseek,
  12256. + .release = single_release,
  12257. +};
  12258. +
  12259. +/* Adds a resource to the private data list which tracks all the allocated
  12260. +** data.
  12261. +*/
  12262. +static void vmcs_sm_add_resource(struct SM_PRIV_DATA_T *privdata,
  12263. + struct SM_RESOURCE_T *resource)
  12264. +{
  12265. + mutex_lock(&(sm_state->map_lock));
  12266. + list_add(&resource->resource_list, &privdata->resource_list);
  12267. + list_add(&resource->global_resource_list, &sm_state->resource_list);
  12268. + mutex_unlock(&(sm_state->map_lock));
  12269. +
  12270. + pr_debug("[%s]: added resource %p (base addr %p, hdl %x, size %u, cache %u)\n",
  12271. + __func__, resource, resource->res_base_mem,
  12272. + resource->res_handle, resource->res_size, resource->res_cached);
  12273. +}
  12274. +
  12275. +/* Locates a resource and acquire a reference on it.
  12276. +** The resource won't be deleted while there is a reference on it.
  12277. +*/
  12278. +static struct SM_RESOURCE_T *vmcs_sm_acquire_resource(struct SM_PRIV_DATA_T
  12279. + *private,
  12280. + unsigned int res_guid)
  12281. +{
  12282. + struct SM_RESOURCE_T *resource, *ret = NULL;
  12283. +
  12284. + mutex_lock(&(sm_state->map_lock));
  12285. +
  12286. + list_for_each_entry(resource, &private->resource_list, resource_list) {
  12287. + if (resource->res_guid != res_guid)
  12288. + continue;
  12289. +
  12290. + pr_debug("[%s]: located resource %p (guid: %x, base addr %p, hdl %x, size %u, cache %u)\n",
  12291. + __func__, resource, resource->res_guid,
  12292. + resource->res_base_mem, resource->res_handle,
  12293. + resource->res_size, resource->res_cached);
  12294. + resource->ref_count++;
  12295. + ret = resource;
  12296. + break;
  12297. + }
  12298. +
  12299. + mutex_unlock(&(sm_state->map_lock));
  12300. +
  12301. + return ret;
  12302. +}
  12303. +
  12304. +/* Locates a resource and acquire a reference on it.
  12305. +** The resource won't be deleted while there is a reference on it.
  12306. +*/
  12307. +static struct SM_RESOURCE_T *vmcs_sm_acquire_first_resource(
  12308. + struct SM_PRIV_DATA_T *private)
  12309. +{
  12310. + struct SM_RESOURCE_T *resource, *ret = NULL;
  12311. +
  12312. + mutex_lock(&(sm_state->map_lock));
  12313. +
  12314. + list_for_each_entry(resource, &private->resource_list, resource_list) {
  12315. + pr_debug("[%s]: located resource %p (guid: %x, base addr %p, hdl %x, size %u, cache %u)\n",
  12316. + __func__, resource, resource->res_guid,
  12317. + resource->res_base_mem, resource->res_handle,
  12318. + resource->res_size, resource->res_cached);
  12319. + resource->ref_count++;
  12320. + ret = resource;
  12321. + break;
  12322. + }
  12323. +
  12324. + mutex_unlock(&(sm_state->map_lock));
  12325. +
  12326. + return ret;
  12327. +}
  12328. +
  12329. +/* Locates a resource and acquire a reference on it.
  12330. +** The resource won't be deleted while there is a reference on it.
  12331. +*/
  12332. +static struct SM_RESOURCE_T *vmcs_sm_acquire_global_resource(unsigned int
  12333. + res_guid)
  12334. +{
  12335. + struct SM_RESOURCE_T *resource, *ret = NULL;
  12336. +
  12337. + mutex_lock(&(sm_state->map_lock));
  12338. +
  12339. + list_for_each_entry(resource, &sm_state->resource_list,
  12340. + global_resource_list) {
  12341. + if (resource->res_guid != res_guid)
  12342. + continue;
  12343. +
  12344. + pr_debug("[%s]: located resource %p (guid: %x, base addr %p, hdl %x, size %u, cache %u)\n",
  12345. + __func__, resource, resource->res_guid,
  12346. + resource->res_base_mem, resource->res_handle,
  12347. + resource->res_size, resource->res_cached);
  12348. + resource->ref_count++;
  12349. + ret = resource;
  12350. + break;
  12351. + }
  12352. +
  12353. + mutex_unlock(&(sm_state->map_lock));
  12354. +
  12355. + return ret;
  12356. +}
  12357. +
  12358. +/* Release a previously acquired resource.
  12359. +** The resource will be deleted when its refcount reaches 0.
  12360. +*/
  12361. +static void vmcs_sm_release_resource(struct SM_RESOURCE_T *resource, int force)
  12362. +{
  12363. + struct SM_PRIV_DATA_T *private = resource->private;
  12364. + struct sm_mmap *map, *map_tmp;
  12365. + struct SM_RESOURCE_T *res_tmp;
  12366. + int ret;
  12367. +
  12368. + mutex_lock(&(sm_state->map_lock));
  12369. +
  12370. + if (--resource->ref_count) {
  12371. + if (force)
  12372. + pr_err("[%s]: resource %p in use\n", __func__, resource);
  12373. +
  12374. + mutex_unlock(&(sm_state->map_lock));
  12375. + return;
  12376. + }
  12377. +
  12378. + /* Time to free the resource. Start by removing it from the list */
  12379. + list_del(&resource->resource_list);
  12380. + list_del(&resource->global_resource_list);
  12381. +
  12382. + /* Walk the global resource list, find out if the resource is used
  12383. + * somewhere else. In which case we don't want to delete it.
  12384. + */
  12385. + list_for_each_entry(res_tmp, &sm_state->resource_list,
  12386. + global_resource_list) {
  12387. + if (res_tmp->res_handle == resource->res_handle) {
  12388. + resource->res_handle = 0;
  12389. + break;
  12390. + }
  12391. + }
  12392. +
  12393. + mutex_unlock(&(sm_state->map_lock));
  12394. +
  12395. + pr_debug("[%s]: freeing data - guid %x, hdl %x, base address %p\n",
  12396. + __func__, resource->res_guid, resource->res_handle,
  12397. + resource->res_base_mem);
  12398. + resource->res_stats[FREE]++;
  12399. +
  12400. + /* Make sure the resource we're removing is unmapped first */
  12401. + if (resource->map_count && !list_empty(&resource->map_list)) {
  12402. + down_write(&current->mm->mmap_sem);
  12403. + list_for_each_entry_safe(map, map_tmp, &resource->map_list,
  12404. + resource_map_list) {
  12405. + ret =
  12406. + do_munmap(current->mm, map->res_addr,
  12407. + resource->res_size);
  12408. + if (ret) {
  12409. + pr_err("[%s]: could not unmap resource %p\n",
  12410. + __func__, resource);
  12411. + }
  12412. + }
  12413. + up_write(&current->mm->mmap_sem);
  12414. + }
  12415. +
  12416. + /* Free up the videocore allocated resource.
  12417. + */
  12418. + if (resource->res_handle) {
  12419. + VC_SM_FREE_T free = {
  12420. + resource->res_handle, resource->res_base_mem
  12421. + };
  12422. + int status = vc_vchi_sm_free(sm_state->sm_handle, &free,
  12423. + &private->int_trans_id);
  12424. + if (status != 0 && status != -EINTR) {
  12425. + pr_err("[%s]: failed to free memory on videocore (status: %u, trans_id: %u)\n",
  12426. + __func__, status, private->int_trans_id);
  12427. + resource->res_stats[FREE_FAIL]++;
  12428. + ret = -EPERM;
  12429. + }
  12430. + }
  12431. +
  12432. + /* Free up the shared resource.
  12433. + */
  12434. + if (resource->res_shared)
  12435. + vmcs_sm_release_resource(resource->res_shared, 0);
  12436. +
  12437. + /* Free up the local resource tracking this allocation.
  12438. + */
  12439. + vc_sm_resource_deceased(resource, force);
  12440. + kfree(resource);
  12441. +}
  12442. +
  12443. +/* Dump the map table for the driver. If process is -1, dumps the whole table,
  12444. +** if process is a valid pid (non -1) dump only the entries associated with the
  12445. +** pid of interest.
  12446. +*/
  12447. +static void vmcs_sm_host_walk_map_per_pid(int pid)
  12448. +{
  12449. + struct sm_mmap *map = NULL;
  12450. +
  12451. + /* Make sure the device was started properly.
  12452. + */
  12453. + if (sm_state == NULL) {
  12454. + pr_err("[%s]: invalid device\n", __func__);
  12455. + return;
  12456. + }
  12457. +
  12458. + mutex_lock(&(sm_state->map_lock));
  12459. +
  12460. + /* Log all applicable mapping(s).
  12461. + */
  12462. + if (!list_empty(&sm_state->map_list)) {
  12463. + list_for_each_entry(map, &sm_state->map_list, map_list) {
  12464. + if (pid == -1 || map->res_pid == pid) {
  12465. + pr_info("[%s]: tgid: %u - vc-hdl: %x, usr-hdl: %x, usr-addr: %lx\n",
  12466. + __func__, map->res_pid, map->res_vc_hdl,
  12467. + map->res_usr_hdl, map->res_addr);
  12468. + }
  12469. + }
  12470. + }
  12471. +
  12472. + mutex_unlock(&(sm_state->map_lock));
  12473. +
  12474. + return;
  12475. +}
  12476. +
  12477. +/* Dump the allocation table from host side point of view. This only dumps the
  12478. +** data allocated for this process/device referenced by the file_data.
  12479. +*/
  12480. +static void vmcs_sm_host_walk_alloc(struct SM_PRIV_DATA_T *file_data)
  12481. +{
  12482. + struct SM_RESOURCE_T *resource = NULL;
  12483. +
  12484. + /* Make sure the device was started properly.
  12485. + */
  12486. + if ((sm_state == NULL) || (file_data == NULL)) {
  12487. + pr_err("[%s]: invalid device\n", __func__);
  12488. + return;
  12489. + }
  12490. +
  12491. + mutex_lock(&(sm_state->map_lock));
  12492. +
  12493. + if (!list_empty(&file_data->resource_list)) {
  12494. + list_for_each_entry(resource, &file_data->resource_list,
  12495. + resource_list) {
  12496. + pr_info("[%s]: guid: %x - hdl: %x, vc-mem: %p, size: %u, cache: %u\n",
  12497. + __func__, resource->res_guid, resource->res_handle,
  12498. + resource->res_base_mem, resource->res_size,
  12499. + resource->res_cached);
  12500. + }
  12501. + }
  12502. +
  12503. + mutex_unlock(&(sm_state->map_lock));
  12504. +
  12505. + return;
  12506. +}
  12507. +
  12508. +/* Create support for private data tracking.
  12509. +*/
  12510. +static struct SM_PRIV_DATA_T *vc_sm_create_priv_data(pid_t id)
  12511. +{
  12512. + char alloc_name[32];
  12513. + struct SM_PRIV_DATA_T *file_data = NULL;
  12514. +
  12515. + /* Allocate private structure. */
  12516. + file_data = kzalloc(sizeof(*file_data), GFP_KERNEL);
  12517. +
  12518. + if (!file_data) {
  12519. + pr_err("[%s]: cannot allocate file data\n", __func__);
  12520. + goto out;
  12521. + }
  12522. +
  12523. + snprintf(alloc_name, sizeof(alloc_name), "%d", id);
  12524. +
  12525. + INIT_LIST_HEAD(&file_data->resource_list);
  12526. + file_data->pid = id;
  12527. + file_data->dir_pid = debugfs_create_dir(alloc_name,
  12528. + sm_state->dir_alloc);
  12529. +#if 0
  12530. + /* TODO: fix this to support querying statistics per pid */
  12531. +
  12532. + if (IS_ERR_OR_NULL(file_data->dir_pid)) {
  12533. + file_data->dir_pid = NULL;
  12534. + } else {
  12535. + struct dentry *dir_entry;
  12536. +
  12537. + dir_entry = debugfs_create_file(VC_SM_RESOURCES, S_IRUGO,
  12538. + file_data->dir_pid, file_data,
  12539. + vc_sm_debug_fs_fops);
  12540. +
  12541. + file_data->dir_res.dir_entry = dir_entry;
  12542. + file_data->dir_res.priv_data = file_data;
  12543. + file_data->dir_res.show = &vc_sm_alloc_show;
  12544. +
  12545. + dir_entry = debugfs_create_file(VC_SM_STATS, S_IRUGO,
  12546. + file_data->dir_pid, file_data,
  12547. + vc_sm_debug_fs_fops);
  12548. +
  12549. + file_data->dir_res.dir_entry = dir_entry;
  12550. + file_data->dir_res.priv_data = file_data;
  12551. + file_data->dir_res.show = &vc_sm_statistics_show;
  12552. + }
  12553. + pr_debug("[%s]: private data allocated %p\n", __func__, file_data);
  12554. +
  12555. +#endif
  12556. +out:
  12557. + return file_data;
  12558. +}
  12559. +
  12560. +/* Open the device. Creates a private state to help track all allocation
  12561. +** associated with this device.
  12562. +*/
  12563. +static int vc_sm_open(struct inode *inode, struct file *file)
  12564. +{
  12565. + int ret = 0;
  12566. +
  12567. + /* Make sure the device was started properly.
  12568. + */
  12569. + if (!sm_state) {
  12570. + pr_err("[%s]: invalid device\n", __func__);
  12571. + ret = -EPERM;
  12572. + goto out;
  12573. + }
  12574. +
  12575. + file->private_data = vc_sm_create_priv_data(current->tgid);
  12576. + if (file->private_data == NULL) {
  12577. + pr_err("[%s]: failed to create data tracker\n", __func__);
  12578. +
  12579. + ret = -ENOMEM;
  12580. + goto out;
  12581. + }
  12582. +
  12583. +out:
  12584. + return ret;
  12585. +}
  12586. +
  12587. +/* Close the device. Free up all resources still associated with this device
  12588. +** at the time.
  12589. +*/
  12590. +static int vc_sm_release(struct inode *inode, struct file *file)
  12591. +{
  12592. + struct SM_PRIV_DATA_T *file_data =
  12593. + (struct SM_PRIV_DATA_T *)file->private_data;
  12594. + struct SM_RESOURCE_T *resource;
  12595. + int ret = 0;
  12596. +
  12597. + /* Make sure the device was started properly.
  12598. + */
  12599. + if (sm_state == NULL || file_data == NULL) {
  12600. + pr_err("[%s]: invalid device\n", __func__);
  12601. + ret = -EPERM;
  12602. + goto out;
  12603. + }
  12604. +
  12605. + pr_debug("[%s]: using private data %p\n", __func__, file_data);
  12606. +
  12607. + if (file_data->restart_sys == -EINTR) {
  12608. + VC_SM_ACTION_CLEAN_T action_clean;
  12609. +
  12610. + pr_debug("[%s]: releasing following EINTR on %u (trans_id: %u) (likely due to signal)...\n",
  12611. + __func__, file_data->int_action,
  12612. + file_data->int_trans_id);
  12613. +
  12614. + action_clean.res_action = file_data->int_action;
  12615. + action_clean.action_trans_id = file_data->int_trans_id;
  12616. +
  12617. + vc_vchi_sm_clean_up(sm_state->sm_handle, &action_clean);
  12618. + }
  12619. +
  12620. + while ((resource = vmcs_sm_acquire_first_resource(file_data)) != NULL) {
  12621. + vmcs_sm_release_resource(resource, 0);
  12622. + vmcs_sm_release_resource(resource, 1);
  12623. + }
  12624. +
  12625. + /* Remove the corresponding proc entry. */
  12626. + debugfs_remove_recursive(file_data->dir_pid);
  12627. +
  12628. + /* Terminate the private data.
  12629. + */
  12630. + kfree(file_data);
  12631. +
  12632. +out:
  12633. + return ret;
  12634. +}
  12635. +
  12636. +static void vcsm_vma_open(struct vm_area_struct *vma)
  12637. +{
  12638. + struct sm_mmap *map = (struct sm_mmap *)vma->vm_private_data;
  12639. +
  12640. + pr_debug("[%s]: virt %lx-%lx, pid %i, pfn %i\n",
  12641. + __func__, vma->vm_start, vma->vm_end, (int)current->tgid,
  12642. + (int)vma->vm_pgoff);
  12643. +
  12644. + map->ref_count++;
  12645. +}
  12646. +
  12647. +static void vcsm_vma_close(struct vm_area_struct *vma)
  12648. +{
  12649. + struct sm_mmap *map = (struct sm_mmap *)vma->vm_private_data;
  12650. +
  12651. + pr_debug("[%s]: virt %lx-%lx, pid %i, pfn %i\n",
  12652. + __func__, vma->vm_start, vma->vm_end, (int)current->tgid,
  12653. + (int)vma->vm_pgoff);
  12654. +
  12655. + map->ref_count--;
  12656. +
  12657. + /* Remove from the map table.
  12658. + */
  12659. + if (map->ref_count == 0)
  12660. + vmcs_sm_remove_map(sm_state, map->resource, map);
  12661. +}
  12662. +
  12663. +static int vcsm_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  12664. +{
  12665. + struct sm_mmap *map = (struct sm_mmap *)vma->vm_private_data;
  12666. + struct SM_RESOURCE_T *resource = map->resource;
  12667. + pgoff_t page_offset;
  12668. + unsigned long pfn;
  12669. + int ret = 0;
  12670. +
  12671. + /* Lock the resource if necessary.
  12672. + */
  12673. + if (!resource->lock_count) {
  12674. + VC_SM_LOCK_UNLOCK_T lock_unlock;
  12675. + VC_SM_LOCK_RESULT_T lock_result;
  12676. + int status;
  12677. +
  12678. + lock_unlock.res_handle = resource->res_handle;
  12679. + lock_unlock.res_mem = resource->res_base_mem;
  12680. +
  12681. + pr_debug("[%s]: attempt to lock data - hdl %x, base address %p\n",
  12682. + __func__, lock_unlock.res_handle, lock_unlock.res_mem);
  12683. +
  12684. + /* Lock the videocore allocated resource.
  12685. + */
  12686. + status = vc_vchi_sm_lock(sm_state->sm_handle,
  12687. + &lock_unlock, &lock_result, 0);
  12688. + if ((status != 0) ||
  12689. + ((status == 0) && (lock_result.res_mem == NULL))) {
  12690. + pr_err("[%s]: failed to lock memory on videocore (status: %u)\n",
  12691. + __func__, status);
  12692. + resource->res_stats[LOCK_FAIL]++;
  12693. + return VM_FAULT_SIGBUS;
  12694. + }
  12695. +
  12696. + pfn = vcaddr_to_pfn((unsigned long)resource->res_base_mem);
  12697. + outer_inv_range(__pfn_to_phys(pfn),
  12698. + __pfn_to_phys(pfn) + resource->res_size);
  12699. +
  12700. + resource->res_stats[LOCK]++;
  12701. + resource->lock_count++;
  12702. +
  12703. + /* Keep track of the new base memory.
  12704. + */
  12705. + if ((lock_result.res_mem != NULL) &&
  12706. + (lock_result.res_old_mem != NULL) &&
  12707. + (lock_result.res_mem != lock_result.res_old_mem)) {
  12708. + resource->res_base_mem = lock_result.res_mem;
  12709. + }
  12710. + }
  12711. +
  12712. + /* We don't use vmf->pgoff since that has the fake offset */
  12713. + page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start);
  12714. + pfn = (uint32_t)resource->res_base_mem & 0x3FFFFFFF;
  12715. + pfn += mm_vc_mem_phys_addr;
  12716. + pfn += page_offset;
  12717. + pfn >>= PAGE_SHIFT;
  12718. +
  12719. + /* Finally, remap it */
  12720. + ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  12721. +
  12722. + switch (ret) {
  12723. + case 0:
  12724. + case -ERESTARTSYS:
  12725. + return VM_FAULT_NOPAGE;
  12726. + case -ENOMEM:
  12727. + case -EAGAIN:
  12728. + return VM_FAULT_OOM;
  12729. + default:
  12730. + return VM_FAULT_SIGBUS;
  12731. + }
  12732. +}
  12733. +
  12734. +static struct vm_operations_struct vcsm_vm_ops = {
  12735. + .open = vcsm_vma_open,
  12736. + .close = vcsm_vma_close,
  12737. + .fault = vcsm_vma_fault,
  12738. +};
  12739. +
  12740. +/* Walks a VMA and clean each valid page from the cache */
  12741. +static void vcsm_vma_cache_clean_page_range(unsigned long addr,
  12742. + unsigned long end)
  12743. +{
  12744. + pgd_t *pgd;
  12745. + pud_t *pud;
  12746. + pmd_t *pmd;
  12747. + pte_t *pte;
  12748. + unsigned long pgd_next, pud_next, pmd_next;
  12749. +
  12750. + if (addr >= end)
  12751. + return;
  12752. +
  12753. + /* Walk PGD */
  12754. + pgd = pgd_offset(current->mm, addr);
  12755. + do {
  12756. + pgd_next = pgd_addr_end(addr, end);
  12757. +
  12758. + if (pgd_none(*pgd) || pgd_bad(*pgd))
  12759. + continue;
  12760. +
  12761. + /* Walk PUD */
  12762. + pud = pud_offset(pgd, addr);
  12763. + do {
  12764. + pud_next = pud_addr_end(addr, pgd_next);
  12765. + if (pud_none(*pud) || pud_bad(*pud))
  12766. + continue;
  12767. +
  12768. + /* Walk PMD */
  12769. + pmd = pmd_offset(pud, addr);
  12770. + do {
  12771. + pmd_next = pmd_addr_end(addr, pud_next);
  12772. + if (pmd_none(*pmd) || pmd_bad(*pmd))
  12773. + continue;
  12774. +
  12775. + /* Walk PTE */
  12776. + pte = pte_offset_map(pmd, addr);
  12777. + do {
  12778. + if (pte_none(*pte)
  12779. + || !pte_present(*pte))
  12780. + continue;
  12781. +
  12782. + /* Clean + invalidate */
  12783. + dmac_flush_range((const void *) addr,
  12784. + (const void *)
  12785. + (addr + PAGE_SIZE));
  12786. +
  12787. + } while (pte++, addr +=
  12788. + PAGE_SIZE, addr != pmd_next);
  12789. + pte_unmap(pte);
  12790. +
  12791. + } while (pmd++, addr = pmd_next, addr != pud_next);
  12792. +
  12793. + } while (pud++, addr = pud_next, addr != pgd_next);
  12794. + } while (pgd++, addr = pgd_next, addr != end);
  12795. +}
  12796. +
  12797. +/* Map an allocated data into something that the user space.
  12798. +*/
  12799. +static int vc_sm_mmap(struct file *file, struct vm_area_struct *vma)
  12800. +{
  12801. + int ret = 0;
  12802. + struct SM_PRIV_DATA_T *file_data =
  12803. + (struct SM_PRIV_DATA_T *)file->private_data;
  12804. + struct SM_RESOURCE_T *resource = NULL;
  12805. + struct sm_mmap *map = NULL;
  12806. +
  12807. + /* Make sure the device was started properly.
  12808. + */
  12809. + if ((sm_state == NULL) || (file_data == NULL)) {
  12810. + pr_err("[%s]: invalid device\n", __func__);
  12811. + return -EPERM;
  12812. + }
  12813. +
  12814. + pr_debug("[%s]: private data %p, guid %x\n", __func__, file_data,
  12815. + ((unsigned int)vma->vm_pgoff << PAGE_SHIFT));
  12816. +
  12817. + /* We lookup to make sure that the data we are being asked to mmap is
  12818. + ** something that we allocated.
  12819. + **
  12820. + ** We use the offset information as the key to tell us which resource
  12821. + ** we are mapping.
  12822. + */
  12823. + resource = vmcs_sm_acquire_resource(file_data,
  12824. + ((unsigned int)vma->vm_pgoff <<
  12825. + PAGE_SHIFT));
  12826. + if (resource == NULL) {
  12827. + pr_err("[%s]: failed to locate resource for guid %x\n", __func__,
  12828. + ((unsigned int)vma->vm_pgoff << PAGE_SHIFT));
  12829. + return -ENOMEM;
  12830. + }
  12831. +
  12832. + pr_debug("[%s]: guid %x, tgid %u, %u, %u\n",
  12833. + __func__, resource->res_guid, current->tgid, resource->pid,
  12834. + file_data->pid);
  12835. +
  12836. + /* Check permissions.
  12837. + */
  12838. + if (resource->pid && (resource->pid != current->tgid)) {
  12839. + pr_err("[%s]: current tgid %u != %u owner\n",
  12840. + __func__, current->tgid, resource->pid);
  12841. + ret = -EPERM;
  12842. + goto error;
  12843. + }
  12844. +
  12845. + /* Verify that what we are asked to mmap is proper.
  12846. + */
  12847. + if (resource->res_size != (unsigned int)(vma->vm_end - vma->vm_start)) {
  12848. + pr_err("[%s]: size inconsistency (resource: %u - mmap: %u)\n",
  12849. + __func__,
  12850. + resource->res_size,
  12851. + (unsigned int)(vma->vm_end - vma->vm_start));
  12852. +
  12853. + ret = -EINVAL;
  12854. + goto error;
  12855. + }
  12856. +
  12857. + /* Keep track of the tuple in the global resource list such that one
  12858. + * can do a mapping lookup for address/memory handle.
  12859. + */
  12860. + map = kzalloc(sizeof(*map), GFP_KERNEL);
  12861. + if (map == NULL) {
  12862. + pr_err("[%s]: failed to allocate global tracking resource\n",
  12863. + __func__);
  12864. + ret = -ENOMEM;
  12865. + goto error;
  12866. + }
  12867. +
  12868. + map->res_pid = current->tgid;
  12869. + map->res_vc_hdl = resource->res_handle;
  12870. + map->res_usr_hdl = resource->res_guid;
  12871. + map->res_addr = (long unsigned int)vma->vm_start;
  12872. + map->resource = resource;
  12873. + map->vma = vma;
  12874. + vmcs_sm_add_map(sm_state, resource, map);
  12875. +
  12876. + /* We are not actually mapping the pages, we just provide a fault
  12877. + ** handler to allow pages to be mapped when accessed
  12878. + */
  12879. + vma->vm_flags |=
  12880. + VM_IO | VM_PFNMAP | VM_DONTCOPY | VM_DONTEXPAND;
  12881. + vma->vm_ops = &vcsm_vm_ops;
  12882. + vma->vm_private_data = map;
  12883. +
  12884. + /* vm_pgoff is the first PFN of the mapped memory */
  12885. + vma->vm_pgoff = (unsigned long)resource->res_base_mem & 0x3FFFFFFF;
  12886. + vma->vm_pgoff += mm_vc_mem_phys_addr;
  12887. + vma->vm_pgoff >>= PAGE_SHIFT;
  12888. +
  12889. + if ((resource->res_cached == VMCS_SM_CACHE_NONE) ||
  12890. + (resource->res_cached == VMCS_SM_CACHE_VC)) {
  12891. + /* Allocated non host cached memory, honour it.
  12892. + */
  12893. + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  12894. + }
  12895. +
  12896. + pr_debug("[%s]: resource %p (guid %x) - cnt %u, base address %p, handle %x, size %u (%u), cache %u\n",
  12897. + __func__,
  12898. + resource, resource->res_guid, resource->lock_count,
  12899. + resource->res_base_mem, resource->res_handle,
  12900. + resource->res_size, (unsigned int)(vma->vm_end - vma->vm_start),
  12901. + resource->res_cached);
  12902. +
  12903. + pr_debug("[%s]: resource %p (base address %p, handle %x) - map-count %d, usr-addr %x\n",
  12904. + __func__, resource, resource->res_base_mem,
  12905. + resource->res_handle, resource->map_count,
  12906. + (unsigned int)vma->vm_start);
  12907. +
  12908. + vcsm_vma_open(vma);
  12909. + resource->res_stats[MAP]++;
  12910. + vmcs_sm_release_resource(resource, 0);
  12911. + return 0;
  12912. +
  12913. +error:
  12914. + vmcs_sm_release_resource(resource, 0);
  12915. + resource->res_stats[MAP_FAIL]++;
  12916. + return ret;
  12917. +}
  12918. +
  12919. +/* Allocate a shared memory handle and block.
  12920. +*/
  12921. +int vc_sm_ioctl_alloc(struct SM_PRIV_DATA_T *private,
  12922. + struct vmcs_sm_ioctl_alloc *ioparam)
  12923. +{
  12924. + int ret = 0;
  12925. + int status;
  12926. + struct SM_RESOURCE_T *resource;
  12927. + VC_SM_ALLOC_T alloc = { 0 };
  12928. + VC_SM_ALLOC_RESULT_T result = { 0 };
  12929. +
  12930. + /* Setup our allocation parameters */
  12931. + alloc.type = ((ioparam->cached == VMCS_SM_CACHE_VC)
  12932. + || (ioparam->cached ==
  12933. + VMCS_SM_CACHE_BOTH)) ? VC_SM_ALLOC_CACHED :
  12934. + VC_SM_ALLOC_NON_CACHED;
  12935. + alloc.base_unit = ioparam->size;
  12936. + alloc.num_unit = ioparam->num;
  12937. + alloc.allocator = current->tgid;
  12938. + /* Align to kernel page size */
  12939. + alloc.alignement = 4096;
  12940. + /* Align the size to the kernel page size */
  12941. + alloc.base_unit =
  12942. + (alloc.base_unit + alloc.alignement - 1) & ~(alloc.alignement - 1);
  12943. + if (*ioparam->name) {
  12944. + memcpy(alloc.name, ioparam->name, sizeof(alloc.name) - 1);
  12945. + } else {
  12946. + memcpy(alloc.name, VMCS_SM_RESOURCE_NAME_DEFAULT,
  12947. + sizeof(VMCS_SM_RESOURCE_NAME_DEFAULT));
  12948. + }
  12949. +
  12950. + pr_debug("[%s]: attempt to allocate \"%s\" data - type %u, base %u (%u), num %u, alignement %u\n",
  12951. + __func__, alloc.name, alloc.type, ioparam->size,
  12952. + alloc.base_unit, alloc.num_unit, alloc.alignement);
  12953. +
  12954. + /* Allocate local resource to track this allocation.
  12955. + */
  12956. + resource = kzalloc(sizeof(*resource), GFP_KERNEL);
  12957. + if (!resource) {
  12958. + ret = -ENOMEM;
  12959. + goto error;
  12960. + }
  12961. + INIT_LIST_HEAD(&resource->map_list);
  12962. + resource->ref_count++;
  12963. + resource->pid = current->tgid;
  12964. +
  12965. + /* Allocate the videocore resource.
  12966. + */
  12967. + status = vc_vchi_sm_alloc(sm_state->sm_handle, &alloc, &result,
  12968. + &private->int_trans_id);
  12969. + if (status == -EINTR) {
  12970. + pr_debug("[%s]: requesting allocate memory action restart (trans_id: %u)\n",
  12971. + __func__, private->int_trans_id);
  12972. + ret = -ERESTARTSYS;
  12973. + private->restart_sys = -EINTR;
  12974. + private->int_action = VC_SM_MSG_TYPE_ALLOC;
  12975. + goto error;
  12976. + } else if (status != 0 || (status == 0 && result.res_mem == NULL)) {
  12977. + pr_err("[%s]: failed to allocate memory on videocore (status: %u, trans_id: %u)\n",
  12978. + __func__, status, private->int_trans_id);
  12979. + ret = -ENOMEM;
  12980. + resource->res_stats[ALLOC_FAIL]++;
  12981. + goto error;
  12982. + }
  12983. +
  12984. + /* Keep track of the resource we created.
  12985. + */
  12986. + resource->private = private;
  12987. + resource->res_handle = result.res_handle;
  12988. + resource->res_base_mem = result.res_mem;
  12989. + resource->res_size = alloc.base_unit * alloc.num_unit;
  12990. + resource->res_cached = ioparam->cached;
  12991. +
  12992. + /* Kernel/user GUID. This global identifier is used for mmap'ing the
  12993. + * allocated region from user space, it is passed as the mmap'ing
  12994. + * offset, we use it to 'hide' the videocore handle/address.
  12995. + */
  12996. + mutex_lock(&sm_state->lock);
  12997. + resource->res_guid = ++sm_state->guid;
  12998. + mutex_unlock(&sm_state->lock);
  12999. + resource->res_guid <<= PAGE_SHIFT;
  13000. +
  13001. + vmcs_sm_add_resource(private, resource);
  13002. +
  13003. + pr_debug("[%s]: allocated data - guid %x, hdl %x, base address %p, size %d, cache %d\n",
  13004. + __func__, resource->res_guid, resource->res_handle,
  13005. + resource->res_base_mem, resource->res_size,
  13006. + resource->res_cached);
  13007. +
  13008. + /* We're done */
  13009. + resource->res_stats[ALLOC]++;
  13010. + ioparam->handle = resource->res_guid;
  13011. + return 0;
  13012. +
  13013. +error:
  13014. + pr_err("[%s]: failed to allocate \"%s\" data (%i) - type %u, base %u (%u), num %u, alignment %u\n",
  13015. + __func__, alloc.name, ret, alloc.type, ioparam->size,
  13016. + alloc.base_unit, alloc.num_unit, alloc.alignement);
  13017. + if (resource != NULL) {
  13018. + vc_sm_resource_deceased(resource, 1);
  13019. + kfree(resource);
  13020. + }
  13021. + return ret;
  13022. +}
  13023. +
  13024. +/* Share an allocate memory handle and block.
  13025. +*/
  13026. +int vc_sm_ioctl_alloc_share(struct SM_PRIV_DATA_T *private,
  13027. + struct vmcs_sm_ioctl_alloc_share *ioparam)
  13028. +{
  13029. + struct SM_RESOURCE_T *resource, *shared_resource;
  13030. + int ret = 0;
  13031. +
  13032. + pr_debug("[%s]: attempt to share resource %u\n", __func__,
  13033. + ioparam->handle);
  13034. +
  13035. + shared_resource = vmcs_sm_acquire_global_resource(ioparam->handle);
  13036. + if (shared_resource == NULL) {
  13037. + ret = -ENOMEM;
  13038. + goto error;
  13039. + }
  13040. +
  13041. + /* Allocate local resource to track this allocation.
  13042. + */
  13043. + resource = kzalloc(sizeof(*resource), GFP_KERNEL);
  13044. + if (resource == NULL) {
  13045. + pr_err("[%s]: failed to allocate local tracking resource\n",
  13046. + __func__);
  13047. + ret = -ENOMEM;
  13048. + goto error;
  13049. + }
  13050. + INIT_LIST_HEAD(&resource->map_list);
  13051. + resource->ref_count++;
  13052. + resource->pid = current->tgid;
  13053. +
  13054. + /* Keep track of the resource we created.
  13055. + */
  13056. + resource->private = private;
  13057. + resource->res_handle = shared_resource->res_handle;
  13058. + resource->res_base_mem = shared_resource->res_base_mem;
  13059. + resource->res_size = shared_resource->res_size;
  13060. + resource->res_cached = shared_resource->res_cached;
  13061. + resource->res_shared = shared_resource;
  13062. +
  13063. + mutex_lock(&sm_state->lock);
  13064. + resource->res_guid = ++sm_state->guid;
  13065. + mutex_unlock(&sm_state->lock);
  13066. + resource->res_guid <<= PAGE_SHIFT;
  13067. +
  13068. + vmcs_sm_add_resource(private, resource);
  13069. +
  13070. + pr_debug("[%s]: allocated data - guid %x, hdl %x, base address %p, size %d, cache %d\n",
  13071. + __func__, resource->res_guid, resource->res_handle,
  13072. + resource->res_base_mem, resource->res_size,
  13073. + resource->res_cached);
  13074. +
  13075. + /* We're done */
  13076. + resource->res_stats[ALLOC]++;
  13077. + ioparam->handle = resource->res_guid;
  13078. + ioparam->size = resource->res_size;
  13079. + return 0;
  13080. +
  13081. +error:
  13082. + pr_err("[%s]: failed to share %u\n", __func__, ioparam->handle);
  13083. + if (shared_resource != NULL)
  13084. + vmcs_sm_release_resource(shared_resource, 0);
  13085. +
  13086. + return ret;
  13087. +}
  13088. +
  13089. +/* Free a previously allocated shared memory handle and block.
  13090. +*/
  13091. +static int vc_sm_ioctl_free(struct SM_PRIV_DATA_T *private,
  13092. + struct vmcs_sm_ioctl_free *ioparam)
  13093. +{
  13094. + struct SM_RESOURCE_T *resource =
  13095. + vmcs_sm_acquire_resource(private, ioparam->handle);
  13096. +
  13097. + if (resource == NULL) {
  13098. + pr_err("[%s]: resource for guid %u does not exist\n", __func__,
  13099. + ioparam->handle);
  13100. + return -EINVAL;
  13101. + }
  13102. +
  13103. + /* Check permissions.
  13104. + */
  13105. + if (resource->pid && (resource->pid != current->tgid)) {
  13106. + pr_err("[%s]: current tgid %u != %u owner\n",
  13107. + __func__, current->tgid, resource->pid);
  13108. + vmcs_sm_release_resource(resource, 0);
  13109. + return -EPERM;
  13110. + }
  13111. +
  13112. + vmcs_sm_release_resource(resource, 0);
  13113. + vmcs_sm_release_resource(resource, 0);
  13114. + return 0;
  13115. +}
  13116. +
  13117. +/* Resize a previously allocated shared memory handle and block.
  13118. +*/
  13119. +static int vc_sm_ioctl_resize(struct SM_PRIV_DATA_T *private,
  13120. + struct vmcs_sm_ioctl_resize *ioparam)
  13121. +{
  13122. + int ret = 0;
  13123. + int status;
  13124. + VC_SM_RESIZE_T resize;
  13125. + struct SM_RESOURCE_T *resource;
  13126. +
  13127. + /* Locate resource from GUID.
  13128. + */
  13129. + resource = vmcs_sm_acquire_resource(private, ioparam->handle);
  13130. + if (!resource) {
  13131. + pr_err("[%s]: failed resource - guid %x\n",
  13132. + __func__, ioparam->handle);
  13133. + ret = -EFAULT;
  13134. + goto error;
  13135. + }
  13136. +
  13137. + /* If the resource is locked, its reference count will be not NULL,
  13138. + ** in which case we will not be allowed to resize it anyways, so
  13139. + ** reject the attempt here.
  13140. + */
  13141. + if (resource->lock_count != 0) {
  13142. + pr_err("[%s]: cannot resize - guid %x, ref-cnt %d\n",
  13143. + __func__, ioparam->handle, resource->lock_count);
  13144. + ret = -EFAULT;
  13145. + goto error;
  13146. + }
  13147. +
  13148. + /* Check permissions.
  13149. + */
  13150. + if (resource->pid && (resource->pid != current->tgid)) {
  13151. + pr_err("[%s]: current tgid %u != %u owner\n", __func__,
  13152. + current->tgid, resource->pid);
  13153. + ret = -EPERM;
  13154. + goto error;
  13155. + }
  13156. +
  13157. + if (resource->map_count != 0) {
  13158. + pr_err("[%s]: cannot resize - guid %x, ref-cnt %d\n",
  13159. + __func__, ioparam->handle, resource->map_count);
  13160. + ret = -EFAULT;
  13161. + goto error;
  13162. + }
  13163. +
  13164. + resize.res_handle = resource->res_handle;
  13165. + resize.res_mem = resource->res_base_mem;
  13166. + resize.res_new_size = ioparam->new_size;
  13167. +
  13168. + pr_debug("[%s]: attempt to resize data - guid %x, hdl %x, base address %p\n",
  13169. + __func__, ioparam->handle, resize.res_handle, resize.res_mem);
  13170. +
  13171. + /* Resize the videocore allocated resource.
  13172. + */
  13173. + status = vc_vchi_sm_resize(sm_state->sm_handle, &resize,
  13174. + &private->int_trans_id);
  13175. + if (status == -EINTR) {
  13176. + pr_debug("[%s]: requesting resize memory action restart (trans_id: %u)\n",
  13177. + __func__, private->int_trans_id);
  13178. + ret = -ERESTARTSYS;
  13179. + private->restart_sys = -EINTR;
  13180. + private->int_action = VC_SM_MSG_TYPE_RESIZE;
  13181. + goto error;
  13182. + } else if (status != 0) {
  13183. + pr_err("[%s]: failed to resize memory on videocore (status: %u, trans_id: %u)\n",
  13184. + __func__, status, private->int_trans_id);
  13185. + ret = -EPERM;
  13186. + goto error;
  13187. + }
  13188. +
  13189. + pr_debug("[%s]: success to resize data - hdl %x, size %d -> %d\n",
  13190. + __func__, resize.res_handle, resource->res_size,
  13191. + resize.res_new_size);
  13192. +
  13193. + /* Successfully resized, save the information and inform the user.
  13194. + */
  13195. + ioparam->old_size = resource->res_size;
  13196. + resource->res_size = resize.res_new_size;
  13197. +
  13198. +error:
  13199. + if (resource)
  13200. + vmcs_sm_release_resource(resource, 0);
  13201. +
  13202. + return ret;
  13203. +}
  13204. +
  13205. +/* Lock a previously allocated shared memory handle and block.
  13206. +*/
  13207. +static int vc_sm_ioctl_lock(struct SM_PRIV_DATA_T *private,
  13208. + struct vmcs_sm_ioctl_lock_unlock *ioparam,
  13209. + int change_cache, enum vmcs_sm_cache_e cache_type,
  13210. + unsigned int vc_addr)
  13211. +{
  13212. + int status;
  13213. + VC_SM_LOCK_UNLOCK_T lock;
  13214. + VC_SM_LOCK_RESULT_T result;
  13215. + struct SM_RESOURCE_T *resource;
  13216. + int ret = 0;
  13217. + struct sm_mmap *map, *map_tmp;
  13218. + long unsigned int phys_addr;
  13219. +
  13220. + map = NULL;
  13221. +
  13222. + /* Locate resource from GUID.
  13223. + */
  13224. + resource = vmcs_sm_acquire_resource(private, ioparam->handle);
  13225. + if (resource == NULL) {
  13226. + ret = -EINVAL;
  13227. + goto error;
  13228. + }
  13229. +
  13230. + /* Check permissions.
  13231. + */
  13232. + if (resource->pid && (resource->pid != current->tgid)) {
  13233. + pr_err("[%s]: current tgid %u != %u owner\n", __func__,
  13234. + current->tgid, resource->pid);
  13235. + ret = -EPERM;
  13236. + goto error;
  13237. + }
  13238. +
  13239. + lock.res_handle = resource->res_handle;
  13240. + lock.res_mem = resource->res_base_mem;
  13241. +
  13242. + /* Take the lock and get the address to be mapped.
  13243. + */
  13244. + if (vc_addr == 0) {
  13245. + pr_debug("[%s]: attempt to lock data - guid %x, hdl %x, base address %p\n",
  13246. + __func__, ioparam->handle, lock.res_handle,
  13247. + lock.res_mem);
  13248. +
  13249. + /* Lock the videocore allocated resource.
  13250. + */
  13251. + status = vc_vchi_sm_lock(sm_state->sm_handle, &lock, &result,
  13252. + &private->int_trans_id);
  13253. + if (status == -EINTR) {
  13254. + pr_debug("[%s]: requesting lock memory action restart (trans_id: %u)\n",
  13255. + __func__, private->int_trans_id);
  13256. + ret = -ERESTARTSYS;
  13257. + private->restart_sys = -EINTR;
  13258. + private->int_action = VC_SM_MSG_TYPE_LOCK;
  13259. + goto error;
  13260. + } else if (status != 0 ||
  13261. + (status == 0 && result.res_mem == NULL)) {
  13262. + pr_err("[%s]: failed to lock memory on videocore (status: %u, trans_id: %u)\n",
  13263. + __func__, status, private->int_trans_id);
  13264. + ret = -EPERM;
  13265. + resource->res_stats[LOCK_FAIL]++;
  13266. + goto error;
  13267. + }
  13268. +
  13269. + pr_debug("[%s]: succeed to lock data - hdl %x, base address %p (%p), ref-cnt %d\n",
  13270. + __func__, lock.res_handle, result.res_mem,
  13271. + lock.res_mem, resource->lock_count);
  13272. + }
  13273. + /* Lock assumed taken already, address to be mapped is known.
  13274. + */
  13275. + else
  13276. + resource->res_base_mem = (void *)vc_addr;
  13277. +
  13278. + resource->res_stats[LOCK]++;
  13279. + resource->lock_count++;
  13280. +
  13281. + /* Keep track of the new base memory allocation if it has changed.
  13282. + */
  13283. + if ((vc_addr == 0) &&
  13284. + (result.res_mem != NULL) &&
  13285. + (result.res_old_mem != NULL) &&
  13286. + (result.res_mem != result.res_old_mem)) {
  13287. + resource->res_base_mem = result.res_mem;
  13288. +
  13289. + /* Kernel allocated resources.
  13290. + */
  13291. + if (resource->pid == 0) {
  13292. + if (!list_empty(&resource->map_list)) {
  13293. + list_for_each_entry_safe(map, map_tmp,
  13294. + &resource->map_list,
  13295. + resource_map_list) {
  13296. + if (map->res_addr) {
  13297. + iounmap((void *)map->res_addr);
  13298. + map->res_addr = 0;
  13299. +
  13300. + vmcs_sm_remove_map(sm_state,
  13301. + map->resource,
  13302. + map);
  13303. + break;
  13304. + }
  13305. + }
  13306. + }
  13307. + }
  13308. + }
  13309. +
  13310. + if (change_cache)
  13311. + resource->res_cached = cache_type;
  13312. +
  13313. + if (resource->map_count) {
  13314. + ioparam->addr =
  13315. + vmcs_sm_usr_address_from_pid_and_usr_handle(
  13316. + current->tgid, ioparam->handle);
  13317. +
  13318. + pr_debug("[%s] map_count %d private->pid %d current->tgid %d hnd %x addr %u\n",
  13319. + __func__, resource->map_count, private->pid,
  13320. + current->tgid, ioparam->handle, ioparam->addr);
  13321. + } else {
  13322. + /* Kernel allocated resources.
  13323. + */
  13324. + if (resource->pid == 0) {
  13325. + pr_debug("[%s]: attempt mapping kernel resource - guid %x, hdl %x\n",
  13326. + __func__, ioparam->handle, lock.res_handle);
  13327. +
  13328. + ioparam->addr = 0;
  13329. +
  13330. + map = kzalloc(sizeof(*map), GFP_KERNEL);
  13331. + if (map == NULL) {
  13332. + pr_err("[%s]: failed allocating tracker\n",
  13333. + __func__);
  13334. + ret = -ENOMEM;
  13335. + goto error;
  13336. + } else {
  13337. + phys_addr = (uint32_t)resource->res_base_mem &
  13338. + 0x3FFFFFFF;
  13339. + phys_addr += mm_vc_mem_phys_addr;
  13340. + if (resource->res_cached
  13341. + == VMCS_SM_CACHE_HOST) {
  13342. + ioparam->addr = (long unsigned int)
  13343. + /* TODO - make cached work */
  13344. + ioremap_nocache(phys_addr,
  13345. + resource->res_size);
  13346. +
  13347. + pr_debug("[%s]: mapping kernel - guid %x, hdl %x - cached mapping %u\n",
  13348. + __func__, ioparam->handle,
  13349. + lock.res_handle, ioparam->addr);
  13350. + } else {
  13351. + ioparam->addr = (long unsigned int)
  13352. + ioremap_nocache(phys_addr,
  13353. + resource->res_size);
  13354. +
  13355. + pr_debug("[%s]: mapping kernel- guid %x, hdl %x - non cached mapping %u\n",
  13356. + __func__, ioparam->handle,
  13357. + lock.res_handle, ioparam->addr);
  13358. + }
  13359. +
  13360. + map->res_pid = 0;
  13361. + map->res_vc_hdl = resource->res_handle;
  13362. + map->res_usr_hdl = resource->res_guid;
  13363. + map->res_addr = ioparam->addr;
  13364. + map->resource = resource;
  13365. + map->vma = NULL;
  13366. +
  13367. + vmcs_sm_add_map(sm_state, resource, map);
  13368. + }
  13369. + } else
  13370. + ioparam->addr = 0;
  13371. + }
  13372. +
  13373. +error:
  13374. + if (resource)
  13375. + vmcs_sm_release_resource(resource, 0);
  13376. +
  13377. + return ret;
  13378. +}
  13379. +
  13380. +/* Unlock a previously allocated shared memory handle and block.
  13381. +*/
  13382. +static int vc_sm_ioctl_unlock(struct SM_PRIV_DATA_T *private,
  13383. + struct vmcs_sm_ioctl_lock_unlock *ioparam,
  13384. + int flush, int wait_reply, int no_vc_unlock)
  13385. +{
  13386. + int status;
  13387. + VC_SM_LOCK_UNLOCK_T unlock;
  13388. + struct sm_mmap *map, *map_tmp;
  13389. + struct SM_RESOURCE_T *resource;
  13390. + int ret = 0;
  13391. +
  13392. + map = NULL;
  13393. +
  13394. + /* Locate resource from GUID.
  13395. + */
  13396. + resource = vmcs_sm_acquire_resource(private, ioparam->handle);
  13397. + if (resource == NULL) {
  13398. + ret = -EINVAL;
  13399. + goto error;
  13400. + }
  13401. +
  13402. + /* Check permissions.
  13403. + */
  13404. + if (resource->pid && (resource->pid != current->tgid)) {
  13405. + pr_err("[%s]: current tgid %u != %u owner\n",
  13406. + __func__, current->tgid, resource->pid);
  13407. + ret = -EPERM;
  13408. + goto error;
  13409. + }
  13410. +
  13411. + unlock.res_handle = resource->res_handle;
  13412. + unlock.res_mem = resource->res_base_mem;
  13413. +
  13414. + pr_debug("[%s]: attempt to unlock data - guid %x, hdl %x, base address %p\n",
  13415. + __func__, ioparam->handle, unlock.res_handle, unlock.res_mem);
  13416. +
  13417. + /* User space allocated resources.
  13418. + */
  13419. + if (resource->pid) {
  13420. + /* Flush if requested */
  13421. + if (resource->res_cached && flush) {
  13422. + dma_addr_t phys_addr = 0;
  13423. + resource->res_stats[FLUSH]++;
  13424. +
  13425. + phys_addr =
  13426. + (dma_addr_t)((uint32_t)resource->res_base_mem &
  13427. + 0x3FFFFFFF);
  13428. + phys_addr += (dma_addr_t)mm_vc_mem_phys_addr;
  13429. +
  13430. + /* L1 cache flush */
  13431. + down_read(&current->mm->mmap_sem);
  13432. + list_for_each_entry(map, &resource->map_list,
  13433. + resource_map_list) {
  13434. + if (map->vma) {
  13435. + unsigned long start;
  13436. + unsigned long end;
  13437. + start = map->vma->vm_start;
  13438. + end = map->vma->vm_end;
  13439. +
  13440. + vcsm_vma_cache_clean_page_range(
  13441. + start, end);
  13442. + }
  13443. + }
  13444. + up_read(&current->mm->mmap_sem);
  13445. +
  13446. + /* L2 cache flush */
  13447. + outer_clean_range(phys_addr,
  13448. + phys_addr +
  13449. + (size_t) resource->res_size);
  13450. + }
  13451. +
  13452. + /* We need to zap all the vmas associated with this resource */
  13453. + if (resource->lock_count == 1) {
  13454. + down_read(&current->mm->mmap_sem);
  13455. + list_for_each_entry(map, &resource->map_list,
  13456. + resource_map_list) {
  13457. + if (map->vma) {
  13458. + zap_vma_ptes(map->vma,
  13459. + map->vma->vm_start,
  13460. + map->vma->vm_end -
  13461. + map->vma->vm_start);
  13462. + }
  13463. + }
  13464. + up_read(&current->mm->mmap_sem);
  13465. + }
  13466. + }
  13467. + /* Kernel allocated resources. */
  13468. + else {
  13469. + /* Global + Taken in this context */
  13470. + if (resource->ref_count == 2) {
  13471. + if (!list_empty(&resource->map_list)) {
  13472. + list_for_each_entry_safe(map, map_tmp,
  13473. + &resource->map_list,
  13474. + resource_map_list) {
  13475. + if (map->res_addr) {
  13476. + if (flush &&
  13477. + (resource->res_cached ==
  13478. + VMCS_SM_CACHE_HOST)) {
  13479. + long unsigned int
  13480. + phys_addr;
  13481. + phys_addr = (uint32_t)
  13482. + resource->res_base_mem & 0x3FFFFFFF;
  13483. + phys_addr +=
  13484. + mm_vc_mem_phys_addr;
  13485. +
  13486. + /* L1 cache flush */
  13487. + dmac_flush_range((const
  13488. + void
  13489. + *)
  13490. + map->res_addr, (const void *)
  13491. + (map->res_addr + resource->res_size));
  13492. +
  13493. + /* L2 cache flush */
  13494. + outer_clean_range
  13495. + (phys_addr,
  13496. + phys_addr +
  13497. + (size_t)
  13498. + resource->res_size);
  13499. + }
  13500. +
  13501. + iounmap((void *)map->res_addr);
  13502. + map->res_addr = 0;
  13503. +
  13504. + vmcs_sm_remove_map(sm_state,
  13505. + map->resource,
  13506. + map);
  13507. + break;
  13508. + }
  13509. + }
  13510. + }
  13511. + }
  13512. + }
  13513. +
  13514. + if (resource->lock_count) {
  13515. + /* Bypass the videocore unlock.
  13516. + */
  13517. + if (no_vc_unlock)
  13518. + status = 0;
  13519. + /* Unlock the videocore allocated resource.
  13520. + */
  13521. + else {
  13522. + status =
  13523. + vc_vchi_sm_unlock(sm_state->sm_handle, &unlock,
  13524. + &private->int_trans_id,
  13525. + wait_reply);
  13526. + if (status == -EINTR) {
  13527. + pr_debug("[%s]: requesting unlock memory action restart (trans_id: %u)\n",
  13528. + __func__, private->int_trans_id);
  13529. +
  13530. + ret = -ERESTARTSYS;
  13531. + resource->res_stats[UNLOCK]--;
  13532. + private->restart_sys = -EINTR;
  13533. + private->int_action = VC_SM_MSG_TYPE_UNLOCK;
  13534. + goto error;
  13535. + } else if (status != 0) {
  13536. + pr_err("[%s]: failed to unlock vc mem (status: %u, trans_id: %u)\n",
  13537. + __func__, status, private->int_trans_id);
  13538. +
  13539. + ret = -EPERM;
  13540. + resource->res_stats[UNLOCK_FAIL]++;
  13541. + goto error;
  13542. + }
  13543. + }
  13544. +
  13545. + resource->res_stats[UNLOCK]++;
  13546. + resource->lock_count--;
  13547. + }
  13548. +
  13549. + pr_debug("[%s]: success to unlock data - hdl %x, base address %p, ref-cnt %d\n",
  13550. + __func__, unlock.res_handle, unlock.res_mem,
  13551. + resource->lock_count);
  13552. +
  13553. +error:
  13554. + if (resource)
  13555. + vmcs_sm_release_resource(resource, 0);
  13556. +
  13557. + return ret;
  13558. +}
  13559. +
  13560. +/* Handle control from host. */
  13561. +static long vc_sm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  13562. +{
  13563. + int ret = 0;
  13564. + unsigned int cmdnr = _IOC_NR(cmd);
  13565. + struct SM_PRIV_DATA_T *file_data =
  13566. + (struct SM_PRIV_DATA_T *)file->private_data;
  13567. + struct SM_RESOURCE_T *resource = NULL;
  13568. +
  13569. + /* Validate we can work with this device. */
  13570. + if ((sm_state == NULL) || (file_data == NULL)) {
  13571. + pr_err("[%s]: invalid device\n", __func__);
  13572. + ret = -EPERM;
  13573. + goto out;
  13574. + }
  13575. +
  13576. + pr_debug("[%s]: cmd %x tgid %u, owner %u\n", __func__, cmdnr,
  13577. + current->tgid, file_data->pid);
  13578. +
  13579. + /* Action is a re-post of a previously interrupted action? */
  13580. + if (file_data->restart_sys == -EINTR) {
  13581. + VC_SM_ACTION_CLEAN_T action_clean;
  13582. +
  13583. + pr_debug("[%s]: clean up of action %u (trans_id: %u) following EINTR\n",
  13584. + __func__, file_data->int_action,
  13585. + file_data->int_trans_id);
  13586. +
  13587. + action_clean.res_action = file_data->int_action;
  13588. + action_clean.action_trans_id = file_data->int_trans_id;
  13589. +
  13590. + vc_vchi_sm_clean_up(sm_state->sm_handle, &action_clean);
  13591. +
  13592. + file_data->restart_sys = 0;
  13593. + }
  13594. +
  13595. + /* Now process the command.
  13596. + */
  13597. + switch (cmdnr) {
  13598. + /* New memory allocation.
  13599. + */
  13600. + case VMCS_SM_CMD_ALLOC:
  13601. + {
  13602. + struct vmcs_sm_ioctl_alloc ioparam;
  13603. +
  13604. + /* Get the parameter data.
  13605. + */
  13606. + if (copy_from_user
  13607. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  13608. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13609. + __func__, cmdnr);
  13610. + ret = -EFAULT;
  13611. + goto out;
  13612. + }
  13613. +
  13614. + ret = vc_sm_ioctl_alloc(file_data, &ioparam);
  13615. + if (!ret &&
  13616. + (copy_to_user((void *)arg,
  13617. + &ioparam, sizeof(ioparam)) != 0)) {
  13618. + struct vmcs_sm_ioctl_free freeparam = {
  13619. + ioparam.handle
  13620. + };
  13621. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  13622. + __func__, cmdnr);
  13623. + vc_sm_ioctl_free(file_data, &freeparam);
  13624. + ret = -EFAULT;
  13625. + }
  13626. +
  13627. + /* Done.
  13628. + */
  13629. + goto out;
  13630. + }
  13631. + break;
  13632. +
  13633. + /* Share existing memory allocation.
  13634. + */
  13635. + case VMCS_SM_CMD_ALLOC_SHARE:
  13636. + {
  13637. + struct vmcs_sm_ioctl_alloc_share ioparam;
  13638. +
  13639. + /* Get the parameter data.
  13640. + */
  13641. + if (copy_from_user
  13642. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  13643. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13644. + __func__, cmdnr);
  13645. + ret = -EFAULT;
  13646. + goto out;
  13647. + }
  13648. +
  13649. + ret = vc_sm_ioctl_alloc_share(file_data, &ioparam);
  13650. +
  13651. + /* Copy result back to user.
  13652. + */
  13653. + if (!ret
  13654. + && copy_to_user((void *)arg, &ioparam,
  13655. + sizeof(ioparam)) != 0) {
  13656. + struct vmcs_sm_ioctl_free freeparam = {
  13657. + ioparam.handle
  13658. + };
  13659. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  13660. + __func__, cmdnr);
  13661. + vc_sm_ioctl_free(file_data, &freeparam);
  13662. + ret = -EFAULT;
  13663. + }
  13664. +
  13665. + /* Done.
  13666. + */
  13667. + goto out;
  13668. + }
  13669. + break;
  13670. +
  13671. + /* Lock (attempt to) *and* register a cache behavior change.
  13672. + */
  13673. + case VMCS_SM_CMD_LOCK_CACHE:
  13674. + {
  13675. + struct vmcs_sm_ioctl_lock_cache ioparam;
  13676. + struct vmcs_sm_ioctl_lock_unlock lock;
  13677. +
  13678. + /* Get parameter data.
  13679. + */
  13680. + if (copy_from_user
  13681. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  13682. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13683. + __func__, cmdnr);
  13684. + ret = -EFAULT;
  13685. + goto out;
  13686. + }
  13687. +
  13688. + lock.handle = ioparam.handle;
  13689. + ret =
  13690. + vc_sm_ioctl_lock(file_data, &lock, 1,
  13691. + ioparam.cached, 0);
  13692. +
  13693. + /* Done.
  13694. + */
  13695. + goto out;
  13696. + }
  13697. + break;
  13698. +
  13699. + /* Lock (attempt to) existing memory allocation.
  13700. + */
  13701. + case VMCS_SM_CMD_LOCK:
  13702. + {
  13703. + struct vmcs_sm_ioctl_lock_unlock ioparam;
  13704. +
  13705. + /* Get parameter data.
  13706. + */
  13707. + if (copy_from_user
  13708. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  13709. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13710. + __func__, cmdnr);
  13711. + ret = -EFAULT;
  13712. + goto out;
  13713. + }
  13714. +
  13715. + ret = vc_sm_ioctl_lock(file_data, &ioparam, 0, 0, 0);
  13716. +
  13717. + /* Copy result back to user.
  13718. + */
  13719. + if (copy_to_user((void *)arg, &ioparam, sizeof(ioparam))
  13720. + != 0) {
  13721. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  13722. + __func__, cmdnr);
  13723. + ret = -EFAULT;
  13724. + }
  13725. +
  13726. + /* Done.
  13727. + */
  13728. + goto out;
  13729. + }
  13730. + break;
  13731. +
  13732. + /* Unlock (attempt to) existing memory allocation.
  13733. + */
  13734. + case VMCS_SM_CMD_UNLOCK:
  13735. + {
  13736. + struct vmcs_sm_ioctl_lock_unlock ioparam;
  13737. +
  13738. + /* Get parameter data.
  13739. + */
  13740. + if (copy_from_user
  13741. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  13742. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13743. + __func__, cmdnr);
  13744. + ret = -EFAULT;
  13745. + goto out;
  13746. + }
  13747. +
  13748. + ret = vc_sm_ioctl_unlock(file_data, &ioparam, 0, 1, 0);
  13749. +
  13750. + /* Done.
  13751. + */
  13752. + goto out;
  13753. + }
  13754. + break;
  13755. +
  13756. + /* Resize (attempt to) existing memory allocation.
  13757. + */
  13758. + case VMCS_SM_CMD_RESIZE:
  13759. + {
  13760. + struct vmcs_sm_ioctl_resize ioparam;
  13761. +
  13762. + /* Get parameter data.
  13763. + */
  13764. + if (copy_from_user
  13765. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  13766. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13767. + __func__, cmdnr);
  13768. + ret = -EFAULT;
  13769. + goto out;
  13770. + }
  13771. +
  13772. + ret = vc_sm_ioctl_resize(file_data, &ioparam);
  13773. +
  13774. + /* Copy result back to user.
  13775. + */
  13776. + if (copy_to_user((void *)arg, &ioparam, sizeof(ioparam))
  13777. + != 0) {
  13778. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  13779. + __func__, cmdnr);
  13780. + ret = -EFAULT;
  13781. + }
  13782. +
  13783. + /* Done.
  13784. + */
  13785. + goto out;
  13786. + }
  13787. + break;
  13788. +
  13789. + /* Terminate existing memory allocation.
  13790. + */
  13791. + case VMCS_SM_CMD_FREE:
  13792. + {
  13793. + struct vmcs_sm_ioctl_free ioparam;
  13794. +
  13795. + /* Get parameter data.
  13796. + */
  13797. + if (copy_from_user
  13798. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  13799. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13800. + __func__, cmdnr);
  13801. + ret = -EFAULT;
  13802. + goto out;
  13803. + }
  13804. +
  13805. + ret = vc_sm_ioctl_free(file_data, &ioparam);
  13806. +
  13807. + /* Done.
  13808. + */
  13809. + goto out;
  13810. + }
  13811. + break;
  13812. +
  13813. + /* Walk allocation on videocore, information shows up in the
  13814. + ** videocore log.
  13815. + */
  13816. + case VMCS_SM_CMD_VC_WALK_ALLOC:
  13817. + {
  13818. + pr_debug("[%s]: invoking walk alloc\n", __func__);
  13819. +
  13820. + if (vc_vchi_sm_walk_alloc(sm_state->sm_handle) != 0)
  13821. + pr_err("[%s]: failed to walk-alloc on videocore\n",
  13822. + __func__);
  13823. +
  13824. + /* Done.
  13825. + */
  13826. + goto out;
  13827. + }
  13828. + break;
  13829. +/* Walk mapping table on host, information shows up in the
  13830. + ** kernel log.
  13831. + */
  13832. + case VMCS_SM_CMD_HOST_WALK_MAP:
  13833. + {
  13834. + /* Use pid of -1 to tell to walk the whole map. */
  13835. + vmcs_sm_host_walk_map_per_pid(-1);
  13836. +
  13837. + /* Done. */
  13838. + goto out;
  13839. + }
  13840. + break;
  13841. +
  13842. + /* Walk mapping table per process on host. */
  13843. + case VMCS_SM_CMD_HOST_WALK_PID_ALLOC:
  13844. + {
  13845. + struct vmcs_sm_ioctl_walk ioparam;
  13846. +
  13847. + /* Get parameter data. */
  13848. + if (copy_from_user(&ioparam,
  13849. + (void *)arg, sizeof(ioparam)) != 0) {
  13850. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13851. + __func__, cmdnr);
  13852. + ret = -EFAULT;
  13853. + goto out;
  13854. + }
  13855. +
  13856. + vmcs_sm_host_walk_alloc(file_data);
  13857. +
  13858. + /* Done. */
  13859. + goto out;
  13860. + }
  13861. + break;
  13862. +
  13863. + /* Walk allocation per process on host. */
  13864. + case VMCS_SM_CMD_HOST_WALK_PID_MAP:
  13865. + {
  13866. + struct vmcs_sm_ioctl_walk ioparam;
  13867. +
  13868. + /* Get parameter data. */
  13869. + if (copy_from_user(&ioparam,
  13870. + (void *)arg, sizeof(ioparam)) != 0) {
  13871. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13872. + __func__, cmdnr);
  13873. + ret = -EFAULT;
  13874. + goto out;
  13875. + }
  13876. +
  13877. + vmcs_sm_host_walk_map_per_pid(ioparam.pid);
  13878. +
  13879. + /* Done. */
  13880. + goto out;
  13881. + }
  13882. + break;
  13883. +
  13884. + /* Gets the size of the memory associated with a user handle. */
  13885. + case VMCS_SM_CMD_SIZE_USR_HANDLE:
  13886. + {
  13887. + struct vmcs_sm_ioctl_size ioparam;
  13888. +
  13889. + /* Get parameter data. */
  13890. + if (copy_from_user(&ioparam,
  13891. + (void *)arg, sizeof(ioparam)) != 0) {
  13892. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13893. + __func__, cmdnr);
  13894. + ret = -EFAULT;
  13895. + goto out;
  13896. + }
  13897. +
  13898. + /* Locate resource from GUID. */
  13899. + resource =
  13900. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  13901. + if (resource != NULL) {
  13902. + ioparam.size = resource->res_size;
  13903. + vmcs_sm_release_resource(resource, 0);
  13904. + } else {
  13905. + ioparam.size = 0;
  13906. + }
  13907. +
  13908. + if (copy_to_user((void *)arg,
  13909. + &ioparam, sizeof(ioparam)) != 0) {
  13910. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  13911. + __func__, cmdnr);
  13912. + ret = -EFAULT;
  13913. + }
  13914. +
  13915. + /* Done. */
  13916. + goto out;
  13917. + }
  13918. + break;
  13919. +
  13920. + /* Verify we are dealing with a valid resource. */
  13921. + case VMCS_SM_CMD_CHK_USR_HANDLE:
  13922. + {
  13923. + struct vmcs_sm_ioctl_chk ioparam;
  13924. +
  13925. + /* Get parameter data.
  13926. + */
  13927. + if (copy_from_user(&ioparam,
  13928. + (void *)arg, sizeof(ioparam)) != 0) {
  13929. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13930. + __func__, cmdnr);
  13931. +
  13932. + ret = -EFAULT;
  13933. + goto out;
  13934. + }
  13935. +
  13936. + /* Locate resource from GUID. */
  13937. + resource =
  13938. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  13939. + if (resource == NULL)
  13940. + ret = -EINVAL;
  13941. + /* If the resource is cacheable, return additional
  13942. + * information that may be needed to flush the cache.
  13943. + */
  13944. + else if ((resource->res_cached == VMCS_SM_CACHE_HOST) ||
  13945. + (resource->res_cached == VMCS_SM_CACHE_BOTH)) {
  13946. + ioparam.addr =
  13947. + vmcs_sm_usr_address_from_pid_and_usr_handle
  13948. + (current->tgid, ioparam.handle);
  13949. + ioparam.size = resource->res_size;
  13950. + ioparam.cache = resource->res_cached;
  13951. + } else {
  13952. + ioparam.addr = 0;
  13953. + ioparam.size = 0;
  13954. + ioparam.cache = resource->res_cached;
  13955. + }
  13956. +
  13957. + if (resource)
  13958. + vmcs_sm_release_resource(resource, 0);
  13959. +
  13960. + if (copy_to_user((void *)arg,
  13961. + &ioparam, sizeof(ioparam)) != 0) {
  13962. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  13963. + __func__, cmdnr);
  13964. + ret = -EFAULT;
  13965. + }
  13966. +
  13967. + /* Done.
  13968. + */
  13969. + goto out;
  13970. + }
  13971. + break;
  13972. +
  13973. + /*
  13974. + * Maps a user handle given the process and the virtual address.
  13975. + */
  13976. + case VMCS_SM_CMD_MAPPED_USR_HANDLE:
  13977. + {
  13978. + struct vmcs_sm_ioctl_map ioparam;
  13979. +
  13980. + /* Get parameter data. */
  13981. + if (copy_from_user(&ioparam,
  13982. + (void *)arg, sizeof(ioparam)) != 0) {
  13983. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  13984. + __func__, cmdnr);
  13985. +
  13986. + ret = -EFAULT;
  13987. + goto out;
  13988. + }
  13989. +
  13990. + ioparam.handle =
  13991. + vmcs_sm_usr_handle_from_pid_and_address(
  13992. + ioparam.pid, ioparam.addr);
  13993. +
  13994. + resource =
  13995. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  13996. + if ((resource != NULL)
  13997. + && ((resource->res_cached == VMCS_SM_CACHE_HOST)
  13998. + || (resource->res_cached ==
  13999. + VMCS_SM_CACHE_BOTH))) {
  14000. + ioparam.size = resource->res_size;
  14001. + } else {
  14002. + ioparam.size = 0;
  14003. + }
  14004. +
  14005. + if (resource)
  14006. + vmcs_sm_release_resource(resource, 0);
  14007. +
  14008. + if (copy_to_user((void *)arg,
  14009. + &ioparam, sizeof(ioparam)) != 0) {
  14010. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  14011. + __func__, cmdnr);
  14012. + ret = -EFAULT;
  14013. + }
  14014. +
  14015. + /* Done. */
  14016. + goto out;
  14017. + }
  14018. + break;
  14019. +
  14020. + /*
  14021. + * Maps a videocore handle given process and virtual address.
  14022. + */
  14023. + case VMCS_SM_CMD_MAPPED_VC_HDL_FROM_ADDR:
  14024. + {
  14025. + struct vmcs_sm_ioctl_map ioparam;
  14026. +
  14027. + /* Get parameter data. */
  14028. + if (copy_from_user(&ioparam,
  14029. + (void *)arg, sizeof(ioparam)) != 0) {
  14030. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  14031. + __func__, cmdnr);
  14032. + ret = -EFAULT;
  14033. + goto out;
  14034. + }
  14035. +
  14036. + ioparam.handle = vmcs_sm_vc_handle_from_pid_and_address(
  14037. + ioparam.pid, ioparam.addr);
  14038. +
  14039. + if (copy_to_user((void *)arg,
  14040. + &ioparam, sizeof(ioparam)) != 0) {
  14041. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  14042. + __func__, cmdnr);
  14043. +
  14044. + ret = -EFAULT;
  14045. + }
  14046. +
  14047. + /* Done.
  14048. + */
  14049. + goto out;
  14050. + }
  14051. + break;
  14052. +
  14053. + /* Maps a videocore handle given process and user handle. */
  14054. + case VMCS_SM_CMD_MAPPED_VC_HDL_FROM_HDL:
  14055. + {
  14056. + struct vmcs_sm_ioctl_map ioparam;
  14057. +
  14058. + /* Get parameter data. */
  14059. + if (copy_from_user(&ioparam,
  14060. + (void *)arg, sizeof(ioparam)) != 0) {
  14061. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  14062. + __func__, cmdnr);
  14063. + ret = -EFAULT;
  14064. + goto out;
  14065. + }
  14066. +
  14067. + /* Locate resource from GUID. */
  14068. + resource =
  14069. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  14070. + if (resource != NULL) {
  14071. + ioparam.handle = resource->res_handle;
  14072. + vmcs_sm_release_resource(resource, 0);
  14073. + } else {
  14074. + ioparam.handle = 0;
  14075. + }
  14076. +
  14077. + if (copy_to_user((void *)arg,
  14078. + &ioparam, sizeof(ioparam)) != 0) {
  14079. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  14080. + __func__, cmdnr);
  14081. +
  14082. + ret = -EFAULT;
  14083. + }
  14084. +
  14085. + /* Done. */
  14086. + goto out;
  14087. + }
  14088. + break;
  14089. +
  14090. + /*
  14091. + * Maps a videocore address given process and videocore handle.
  14092. + */
  14093. + case VMCS_SM_CMD_MAPPED_VC_ADDR_FROM_HDL:
  14094. + {
  14095. + struct vmcs_sm_ioctl_map ioparam;
  14096. +
  14097. + /* Get parameter data. */
  14098. + if (copy_from_user(&ioparam,
  14099. + (void *)arg, sizeof(ioparam)) != 0) {
  14100. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  14101. + __func__, cmdnr);
  14102. +
  14103. + ret = -EFAULT;
  14104. + goto out;
  14105. + }
  14106. +
  14107. + /* Locate resource from GUID. */
  14108. + resource =
  14109. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  14110. + if (resource != NULL) {
  14111. + ioparam.addr =
  14112. + (unsigned int)resource->res_base_mem;
  14113. + vmcs_sm_release_resource(resource, 0);
  14114. + } else {
  14115. + ioparam.addr = 0;
  14116. + }
  14117. +
  14118. + if (copy_to_user((void *)arg,
  14119. + &ioparam, sizeof(ioparam)) != 0) {
  14120. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  14121. + __func__, cmdnr);
  14122. + ret = -EFAULT;
  14123. + }
  14124. +
  14125. + /* Done. */
  14126. + goto out;
  14127. + }
  14128. + break;
  14129. +
  14130. + /* Maps a user address given process and vc handle.
  14131. + */
  14132. + case VMCS_SM_CMD_MAPPED_USR_ADDRESS:
  14133. + {
  14134. + struct vmcs_sm_ioctl_map ioparam;
  14135. +
  14136. + /* Get parameter data. */
  14137. + if (copy_from_user(&ioparam,
  14138. + (void *)arg, sizeof(ioparam)) != 0) {
  14139. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  14140. + __func__, cmdnr);
  14141. + ret = -EFAULT;
  14142. + goto out;
  14143. + }
  14144. +
  14145. + /*
  14146. + * Return the address information from the mapping,
  14147. + * 0 (ie NULL) if it cannot locate the actual mapping.
  14148. + */
  14149. + ioparam.addr =
  14150. + vmcs_sm_usr_address_from_pid_and_usr_handle
  14151. + (ioparam.pid, ioparam.handle);
  14152. +
  14153. + if (copy_to_user((void *)arg,
  14154. + &ioparam, sizeof(ioparam)) != 0) {
  14155. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  14156. + __func__, cmdnr);
  14157. + ret = -EFAULT;
  14158. + }
  14159. +
  14160. + /* Done. */
  14161. + goto out;
  14162. + }
  14163. + break;
  14164. +
  14165. + /* Flush the cache for a given mapping. */
  14166. + case VMCS_SM_CMD_FLUSH:
  14167. + {
  14168. + struct vmcs_sm_ioctl_cache ioparam;
  14169. +
  14170. + /* Get parameter data. */
  14171. + if (copy_from_user(&ioparam,
  14172. + (void *)arg, sizeof(ioparam)) != 0) {
  14173. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  14174. + __func__, cmdnr);
  14175. + ret = -EFAULT;
  14176. + goto out;
  14177. + }
  14178. +
  14179. + /* Locate resource from GUID. */
  14180. + resource =
  14181. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  14182. +
  14183. + if ((resource != NULL) && resource->res_cached) {
  14184. + dma_addr_t phys_addr = 0;
  14185. +
  14186. + resource->res_stats[FLUSH]++;
  14187. +
  14188. + phys_addr =
  14189. + (dma_addr_t)((uint32_t)
  14190. + resource->res_base_mem &
  14191. + 0x3FFFFFFF);
  14192. + phys_addr += (dma_addr_t)mm_vc_mem_phys_addr;
  14193. +
  14194. + /* L1 cache flush */
  14195. + down_read(&current->mm->mmap_sem);
  14196. + vcsm_vma_cache_clean_page_range((unsigned long)
  14197. + ioparam.addr,
  14198. + (unsigned long)
  14199. + ioparam.addr +
  14200. + ioparam.size);
  14201. + up_read(&current->mm->mmap_sem);
  14202. +
  14203. + /* L2 cache flush */
  14204. + outer_clean_range(phys_addr,
  14205. + phys_addr +
  14206. + (size_t) ioparam.size);
  14207. + } else if (resource == NULL) {
  14208. + ret = -EINVAL;
  14209. + goto out;
  14210. + }
  14211. +
  14212. + if (resource)
  14213. + vmcs_sm_release_resource(resource, 0);
  14214. +
  14215. + /* Done. */
  14216. + goto out;
  14217. + }
  14218. + break;
  14219. +
  14220. + /* Invalidate the cache for a given mapping. */
  14221. + case VMCS_SM_CMD_INVALID:
  14222. + {
  14223. + struct vmcs_sm_ioctl_cache ioparam;
  14224. +
  14225. + /* Get parameter data. */
  14226. + if (copy_from_user(&ioparam,
  14227. + (void *)arg, sizeof(ioparam)) != 0) {
  14228. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  14229. + __func__, cmdnr);
  14230. + ret = -EFAULT;
  14231. + goto out;
  14232. + }
  14233. +
  14234. + /* Locate resource from GUID.
  14235. + */
  14236. + resource =
  14237. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  14238. +
  14239. + if ((resource != NULL) && resource->res_cached) {
  14240. + dma_addr_t phys_addr = 0;
  14241. +
  14242. + resource->res_stats[INVALID]++;
  14243. +
  14244. + phys_addr =
  14245. + (dma_addr_t)((uint32_t)
  14246. + resource->res_base_mem &
  14247. + 0x3FFFFFFF);
  14248. + phys_addr += (dma_addr_t)mm_vc_mem_phys_addr;
  14249. +
  14250. + /* L2 cache invalidate */
  14251. + outer_inv_range(phys_addr,
  14252. + phys_addr +
  14253. + (size_t) ioparam.size);
  14254. +
  14255. + /* L1 cache invalidate */
  14256. + down_read(&current->mm->mmap_sem);
  14257. + vcsm_vma_cache_clean_page_range((unsigned long)
  14258. + ioparam.addr,
  14259. + (unsigned long)
  14260. + ioparam.addr +
  14261. + ioparam.size);
  14262. + up_read(&current->mm->mmap_sem);
  14263. + } else if (resource == NULL) {
  14264. + ret = -EINVAL;
  14265. + goto out;
  14266. + }
  14267. +
  14268. + if (resource)
  14269. + vmcs_sm_release_resource(resource, 0);
  14270. +
  14271. + /* Done.
  14272. + */
  14273. + goto out;
  14274. + }
  14275. + break;
  14276. +
  14277. + default:
  14278. + {
  14279. + ret = -EINVAL;
  14280. + goto out;
  14281. + }
  14282. + break;
  14283. + }
  14284. +
  14285. +out:
  14286. + return ret;
  14287. +}
  14288. +
  14289. +/* Device operations that we managed in this driver.
  14290. +*/
  14291. +static const struct file_operations vmcs_sm_ops = {
  14292. + .owner = THIS_MODULE,
  14293. + .unlocked_ioctl = vc_sm_ioctl,
  14294. + .open = vc_sm_open,
  14295. + .release = vc_sm_release,
  14296. + .mmap = vc_sm_mmap,
  14297. +};
  14298. +
  14299. +/* Creation of device.
  14300. +*/
  14301. +static int vc_sm_create_sharedmemory(void)
  14302. +{
  14303. + int ret;
  14304. +
  14305. + if (sm_state == NULL) {
  14306. + ret = -ENOMEM;
  14307. + goto out;
  14308. + }
  14309. +
  14310. + /* Create a device class for creating dev nodes.
  14311. + */
  14312. + sm_state->sm_class = class_create(THIS_MODULE, "vc-sm");
  14313. + if (IS_ERR(sm_state->sm_class)) {
  14314. + pr_err("[%s]: unable to create device class\n", __func__);
  14315. + ret = PTR_ERR(sm_state->sm_class);
  14316. + goto out;
  14317. + }
  14318. +
  14319. + /* Create a character driver.
  14320. + */
  14321. + ret = alloc_chrdev_region(&sm_state->sm_devid,
  14322. + DEVICE_MINOR, 1, DEVICE_NAME);
  14323. + if (ret != 0) {
  14324. + pr_err("[%s]: unable to allocate device number\n", __func__);
  14325. + goto out_dev_class_destroy;
  14326. + }
  14327. +
  14328. + cdev_init(&sm_state->sm_cdev, &vmcs_sm_ops);
  14329. + ret = cdev_add(&sm_state->sm_cdev, sm_state->sm_devid, 1);
  14330. + if (ret != 0) {
  14331. + pr_err("[%s]: unable to register device\n", __func__);
  14332. + goto out_chrdev_unreg;
  14333. + }
  14334. +
  14335. + /* Create a device node.
  14336. + */
  14337. + sm_state->sm_dev = device_create(sm_state->sm_class,
  14338. + NULL,
  14339. + MKDEV(MAJOR(sm_state->sm_devid),
  14340. + DEVICE_MINOR), NULL,
  14341. + DEVICE_NAME);
  14342. + if (IS_ERR(sm_state->sm_dev)) {
  14343. + pr_err("[%s]: unable to create device node\n", __func__);
  14344. + ret = PTR_ERR(sm_state->sm_dev);
  14345. + goto out_chrdev_del;
  14346. + }
  14347. +
  14348. + goto out;
  14349. +
  14350. +out_chrdev_del:
  14351. + cdev_del(&sm_state->sm_cdev);
  14352. +out_chrdev_unreg:
  14353. + unregister_chrdev_region(sm_state->sm_devid, 1);
  14354. +out_dev_class_destroy:
  14355. + class_destroy(sm_state->sm_class);
  14356. + sm_state->sm_class = NULL;
  14357. +out:
  14358. + return ret;
  14359. +}
  14360. +
  14361. +/* Termination of the device.
  14362. +*/
  14363. +static int vc_sm_remove_sharedmemory(void)
  14364. +{
  14365. + int ret;
  14366. +
  14367. + if (sm_state == NULL) {
  14368. + /* Nothing to do.
  14369. + */
  14370. + ret = 0;
  14371. + goto out;
  14372. + }
  14373. +
  14374. + /* Remove the sharedmemory character driver.
  14375. + */
  14376. + cdev_del(&sm_state->sm_cdev);
  14377. +
  14378. + /* Unregister region.
  14379. + */
  14380. + unregister_chrdev_region(sm_state->sm_devid, 1);
  14381. +
  14382. + ret = 0;
  14383. + goto out;
  14384. +
  14385. +out:
  14386. + return ret;
  14387. +}
  14388. +
  14389. +/* Videocore connected. */
  14390. +static void vc_sm_connected_init(void)
  14391. +{
  14392. + int ret;
  14393. + VCHI_INSTANCE_T vchi_instance;
  14394. + VCHI_CONNECTION_T *vchi_connection = NULL;
  14395. +
  14396. + pr_info("[%s]: start\n", __func__);
  14397. +
  14398. + /* Allocate memory for the state structure.
  14399. + */
  14400. + sm_state = kzalloc(sizeof(struct SM_STATE_T), GFP_KERNEL);
  14401. + if (sm_state == NULL) {
  14402. + pr_err("[%s]: failed to allocate memory\n", __func__);
  14403. + ret = -ENOMEM;
  14404. + goto out;
  14405. + }
  14406. +
  14407. + mutex_init(&sm_state->lock);
  14408. + mutex_init(&sm_state->map_lock);
  14409. +
  14410. + /* Initialize and create a VCHI connection for the shared memory service
  14411. + ** running on videocore.
  14412. + */
  14413. + ret = vchi_initialise(&vchi_instance);
  14414. + if (ret != 0) {
  14415. + pr_err("[%s]: failed to initialise VCHI instance (ret=%d)\n",
  14416. + __func__, ret);
  14417. +
  14418. + ret = -EIO;
  14419. + goto err_free_mem;
  14420. + }
  14421. +
  14422. + ret = vchi_connect(NULL, 0, vchi_instance);
  14423. + if (ret != 0) {
  14424. + pr_err("[%s]: failed to connect VCHI instance (ret=%d)\n",
  14425. + __func__, ret);
  14426. +
  14427. + ret = -EIO;
  14428. + goto err_free_mem;
  14429. + }
  14430. +
  14431. + /* Initialize an instance of the shared memory service. */
  14432. + sm_state->sm_handle =
  14433. + vc_vchi_sm_init(vchi_instance, &vchi_connection, 1);
  14434. + if (sm_state->sm_handle == NULL) {
  14435. + pr_err("[%s]: failed to initialize shared memory service\n",
  14436. + __func__);
  14437. +
  14438. + ret = -EPERM;
  14439. + goto err_free_mem;
  14440. + }
  14441. +
  14442. + /* Create a debug fs directory entry (root). */
  14443. + sm_state->dir_root = debugfs_create_dir(VC_SM_DIR_ROOT_NAME, NULL);
  14444. + if (!sm_state->dir_root) {
  14445. + pr_err("[%s]: failed to create \'%s\' directory entry\n",
  14446. + __func__, VC_SM_DIR_ROOT_NAME);
  14447. +
  14448. + ret = -EPERM;
  14449. + goto err_stop_sm_service;
  14450. + }
  14451. +
  14452. + sm_state->dir_state.show = &vc_sm_global_state_show;
  14453. + sm_state->dir_state.dir_entry = debugfs_create_file(VC_SM_STATE,
  14454. + S_IRUGO, sm_state->dir_root, &sm_state->dir_state,
  14455. + &vc_sm_debug_fs_fops);
  14456. +
  14457. + sm_state->dir_stats.show = &vc_sm_global_statistics_show;
  14458. + sm_state->dir_stats.dir_entry = debugfs_create_file(VC_SM_STATS,
  14459. + S_IRUGO, sm_state->dir_root, &sm_state->dir_stats,
  14460. + &vc_sm_debug_fs_fops);
  14461. +
  14462. + /* Create the proc entry children. */
  14463. + sm_state->dir_alloc = debugfs_create_dir(VC_SM_DIR_ALLOC_NAME,
  14464. + sm_state->dir_root);
  14465. +
  14466. + /* Create a shared memory device. */
  14467. + ret = vc_sm_create_sharedmemory();
  14468. + if (ret != 0) {
  14469. + pr_err("[%s]: failed to create shared memory device\n",
  14470. + __func__);
  14471. + goto err_remove_debugfs;
  14472. + }
  14473. +
  14474. + INIT_LIST_HEAD(&sm_state->map_list);
  14475. + INIT_LIST_HEAD(&sm_state->resource_list);
  14476. +
  14477. + sm_state->data_knl = vc_sm_create_priv_data(0);
  14478. + if (sm_state->data_knl == NULL) {
  14479. + pr_err("[%s]: failed to create kernel private data tracker\n",
  14480. + __func__);
  14481. + goto err_remove_shared_memory;
  14482. + }
  14483. +
  14484. + /* Done!
  14485. + */
  14486. + sm_inited = 1;
  14487. + goto out;
  14488. +
  14489. +err_remove_shared_memory:
  14490. + vc_sm_remove_sharedmemory();
  14491. +err_remove_debugfs:
  14492. + debugfs_remove_recursive(sm_state->dir_root);
  14493. +err_stop_sm_service:
  14494. + vc_vchi_sm_stop(&sm_state->sm_handle);
  14495. +err_free_mem:
  14496. + kfree(sm_state);
  14497. +out:
  14498. + pr_info("[%s]: end - returning %d\n", __func__, ret);
  14499. +}
  14500. +
  14501. +/* Driver loading. */
  14502. +static int __init vc_sm_init(void)
  14503. +{
  14504. + pr_info("vc-sm: Videocore shared memory driver\n");
  14505. + vchiq_add_connected_callback(vc_sm_connected_init);
  14506. + return 0;
  14507. +}
  14508. +
  14509. +/* Driver unloading. */
  14510. +static void __exit vc_sm_exit(void)
  14511. +{
  14512. + pr_debug("[%s]: start\n", __func__);
  14513. + if (sm_inited) {
  14514. + /* Remove shared memory device.
  14515. + */
  14516. + vc_sm_remove_sharedmemory();
  14517. +
  14518. + /* Remove all proc entries.
  14519. + */
  14520. + debugfs_remove_recursive(sm_state->dir_root);
  14521. +
  14522. + /* Stop the videocore shared memory service.
  14523. + */
  14524. + vc_vchi_sm_stop(&sm_state->sm_handle);
  14525. +
  14526. + /* Free the memory for the state structure.
  14527. + */
  14528. + mutex_destroy(&(sm_state->map_lock));
  14529. + kfree(sm_state);
  14530. + }
  14531. +
  14532. + pr_debug("[%s]: end\n", __func__);
  14533. +}
  14534. +
  14535. +#if defined(__KERNEL__)
  14536. +/* Allocate a shared memory handle and block. */
  14537. +int vc_sm_alloc(VC_SM_ALLOC_T *alloc, int *handle)
  14538. +{
  14539. + struct vmcs_sm_ioctl_alloc ioparam = { 0 };
  14540. + int ret;
  14541. + struct SM_RESOURCE_T *resource;
  14542. +
  14543. + /* Validate we can work with this device.
  14544. + */
  14545. + if (sm_state == NULL || alloc == NULL || handle == NULL) {
  14546. + pr_err("[%s]: invalid input\n", __func__);
  14547. + return -EPERM;
  14548. + }
  14549. +
  14550. + ioparam.size = alloc->base_unit;
  14551. + ioparam.num = alloc->num_unit;
  14552. + ioparam.cached =
  14553. + alloc->type == VC_SM_ALLOC_CACHED ? VMCS_SM_CACHE_VC : 0;
  14554. +
  14555. + ret = vc_sm_ioctl_alloc(sm_state->data_knl, &ioparam);
  14556. +
  14557. + if (ret == 0) {
  14558. + resource =
  14559. + vmcs_sm_acquire_resource(sm_state->data_knl,
  14560. + ioparam.handle);
  14561. + if (resource) {
  14562. + resource->pid = 0;
  14563. + vmcs_sm_release_resource(resource, 0);
  14564. +
  14565. + /* Assign valid handle at this time.
  14566. + */
  14567. + *handle = ioparam.handle;
  14568. + } else {
  14569. + ret = -ENOMEM;
  14570. + }
  14571. + }
  14572. +
  14573. + return ret;
  14574. +}
  14575. +EXPORT_SYMBOL_GPL(vc_sm_alloc);
  14576. +
  14577. +/* Get an internal resource handle mapped from the external one.
  14578. +*/
  14579. +int vc_sm_int_handle(int handle)
  14580. +{
  14581. + struct SM_RESOURCE_T *resource;
  14582. + int ret = 0;
  14583. +
  14584. + /* Validate we can work with this device.
  14585. + */
  14586. + if (sm_state == NULL || handle == 0) {
  14587. + pr_err("[%s]: invalid input\n", __func__);
  14588. + return 0;
  14589. + }
  14590. +
  14591. + /* Locate resource from GUID.
  14592. + */
  14593. + resource = vmcs_sm_acquire_resource(sm_state->data_knl, handle);
  14594. + if (resource) {
  14595. + ret = resource->res_handle;
  14596. + vmcs_sm_release_resource(resource, 0);
  14597. + }
  14598. +
  14599. + return ret;
  14600. +}
  14601. +EXPORT_SYMBOL_GPL(vc_sm_int_handle);
  14602. +
  14603. +/* Free a previously allocated shared memory handle and block.
  14604. +*/
  14605. +int vc_sm_free(int handle)
  14606. +{
  14607. + struct vmcs_sm_ioctl_free ioparam = { handle };
  14608. +
  14609. + /* Validate we can work with this device.
  14610. + */
  14611. + if (sm_state == NULL || handle == 0) {
  14612. + pr_err("[%s]: invalid input\n", __func__);
  14613. + return -EPERM;
  14614. + }
  14615. +
  14616. + return vc_sm_ioctl_free(sm_state->data_knl, &ioparam);
  14617. +}
  14618. +EXPORT_SYMBOL_GPL(vc_sm_free);
  14619. +
  14620. +/* Lock a memory handle for use by kernel.
  14621. +*/
  14622. +int vc_sm_lock(int handle, VC_SM_LOCK_CACHE_MODE_T mode,
  14623. + long unsigned int *data)
  14624. +{
  14625. + struct vmcs_sm_ioctl_lock_unlock ioparam;
  14626. + int ret;
  14627. +
  14628. + /* Validate we can work with this device.
  14629. + */
  14630. + if (sm_state == NULL || handle == 0 || data == NULL) {
  14631. + pr_err("[%s]: invalid input\n", __func__);
  14632. + return -EPERM;
  14633. + }
  14634. +
  14635. + *data = 0;
  14636. +
  14637. + ioparam.handle = handle;
  14638. + ret = vc_sm_ioctl_lock(sm_state->data_knl,
  14639. + &ioparam,
  14640. + 1,
  14641. + ((mode ==
  14642. + VC_SM_LOCK_CACHED) ? VMCS_SM_CACHE_HOST :
  14643. + VMCS_SM_CACHE_NONE), 0);
  14644. +
  14645. + *data = ioparam.addr;
  14646. + return ret;
  14647. +}
  14648. +EXPORT_SYMBOL_GPL(vc_sm_lock);
  14649. +
  14650. +/* Unlock a memory handle in use by kernel.
  14651. +*/
  14652. +int vc_sm_unlock(int handle, int flush, int no_vc_unlock)
  14653. +{
  14654. + struct vmcs_sm_ioctl_lock_unlock ioparam;
  14655. +
  14656. + /* Validate we can work with this device.
  14657. + */
  14658. + if (sm_state == NULL || handle == 0) {
  14659. + pr_err("[%s]: invalid input\n", __func__);
  14660. + return -EPERM;
  14661. + }
  14662. +
  14663. + ioparam.handle = handle;
  14664. + return vc_sm_ioctl_unlock(sm_state->data_knl,
  14665. + &ioparam, flush, 0, no_vc_unlock);
  14666. +}
  14667. +EXPORT_SYMBOL_GPL(vc_sm_unlock);
  14668. +
  14669. +/* Map a shared memory region for use by kernel.
  14670. +*/
  14671. +int vc_sm_map(int handle, unsigned int sm_addr, VC_SM_LOCK_CACHE_MODE_T mode,
  14672. + long unsigned int *data)
  14673. +{
  14674. + struct vmcs_sm_ioctl_lock_unlock ioparam;
  14675. + int ret;
  14676. +
  14677. + /* Validate we can work with this device.
  14678. + */
  14679. + if (sm_state == NULL || handle == 0 || data == NULL || sm_addr == 0) {
  14680. + pr_err("[%s]: invalid input\n", __func__);
  14681. + return -EPERM;
  14682. + }
  14683. +
  14684. + *data = 0;
  14685. +
  14686. + ioparam.handle = handle;
  14687. + ret = vc_sm_ioctl_lock(sm_state->data_knl,
  14688. + &ioparam,
  14689. + 1,
  14690. + ((mode ==
  14691. + VC_SM_LOCK_CACHED) ? VMCS_SM_CACHE_HOST :
  14692. + VMCS_SM_CACHE_NONE), sm_addr);
  14693. +
  14694. + *data = ioparam.addr;
  14695. + return ret;
  14696. +}
  14697. +EXPORT_SYMBOL_GPL(vc_sm_map);
  14698. +#endif
  14699. +
  14700. +late_initcall(vc_sm_init);
  14701. +module_exit(vc_sm_exit);
  14702. +
  14703. +MODULE_AUTHOR("Broadcom");
  14704. +MODULE_DESCRIPTION("VideoCore SharedMemory Driver");
  14705. +MODULE_LICENSE("GPL v2");
  14706. diff -Nur linux-3.12.33/drivers/char/hw_random/bcm2708-rng.c linux-3.12.33-rpi/drivers/char/hw_random/bcm2708-rng.c
  14707. --- linux-3.12.33/drivers/char/hw_random/bcm2708-rng.c 1969-12-31 18:00:00.000000000 -0600
  14708. +++ linux-3.12.33-rpi/drivers/char/hw_random/bcm2708-rng.c 2014-12-03 19:13:34.236418001 -0600
  14709. @@ -0,0 +1,117 @@
  14710. +/**
  14711. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  14712. + *
  14713. + * Redistribution and use in source and binary forms, with or without
  14714. + * modification, are permitted provided that the following conditions
  14715. + * are met:
  14716. + * 1. Redistributions of source code must retain the above copyright
  14717. + * notice, this list of conditions, and the following disclaimer,
  14718. + * without modification.
  14719. + * 2. Redistributions in binary form must reproduce the above copyright
  14720. + * notice, this list of conditions and the following disclaimer in the
  14721. + * documentation and/or other materials provided with the distribution.
  14722. + * 3. The names of the above-listed copyright holders may not be used
  14723. + * to endorse or promote products derived from this software without
  14724. + * specific prior written permission.
  14725. + *
  14726. + * ALTERNATIVELY, this software may be distributed under the terms of the
  14727. + * GNU General Public License ("GPL") version 2, as published by the Free
  14728. + * Software Foundation.
  14729. + *
  14730. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  14731. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  14732. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  14733. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  14734. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  14735. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  14736. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  14737. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  14738. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  14739. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  14740. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14741. + */
  14742. +
  14743. +#include <linux/kernel.h>
  14744. +#include <linux/module.h>
  14745. +#include <linux/init.h>
  14746. +#include <linux/hw_random.h>
  14747. +#include <linux/printk.h>
  14748. +
  14749. +#include <asm/io.h>
  14750. +#include <mach/hardware.h>
  14751. +#include <mach/platform.h>
  14752. +
  14753. +#define RNG_CTRL (0x0)
  14754. +#define RNG_STATUS (0x4)
  14755. +#define RNG_DATA (0x8)
  14756. +#define RNG_FF_THRESHOLD (0xc)
  14757. +
  14758. +/* enable rng */
  14759. +#define RNG_RBGEN 0x1
  14760. +/* double speed, less random mode */
  14761. +#define RNG_RBG2X 0x2
  14762. +
  14763. +/* the initial numbers generated are "less random" so will be discarded */
  14764. +#define RNG_WARMUP_COUNT 0x40000
  14765. +
  14766. +static int bcm2708_rng_data_read(struct hwrng *rng, u32 *buffer)
  14767. +{
  14768. + void __iomem *rng_base = (void __iomem *)rng->priv;
  14769. + unsigned words;
  14770. + /* wait for a random number to be in fifo */
  14771. + do {
  14772. + words = __raw_readl(rng_base + RNG_STATUS)>>24;
  14773. + }
  14774. + while (words == 0);
  14775. + /* read the random number */
  14776. + *buffer = __raw_readl(rng_base + RNG_DATA);
  14777. + return 4;
  14778. +}
  14779. +
  14780. +static struct hwrng bcm2708_rng_ops = {
  14781. + .name = "bcm2708",
  14782. + .data_read = bcm2708_rng_data_read,
  14783. +};
  14784. +
  14785. +static int __init bcm2708_rng_init(void)
  14786. +{
  14787. + void __iomem *rng_base;
  14788. + int err;
  14789. +
  14790. + /* map peripheral */
  14791. + rng_base = ioremap(RNG_BASE, 0x10);
  14792. + pr_info("bcm2708_rng_init=%p\n", rng_base);
  14793. + if (!rng_base) {
  14794. + pr_err("bcm2708_rng_init failed to ioremap\n");
  14795. + return -ENOMEM;
  14796. + }
  14797. + bcm2708_rng_ops.priv = (unsigned long)rng_base;
  14798. + /* register driver */
  14799. + err = hwrng_register(&bcm2708_rng_ops);
  14800. + if (err) {
  14801. + pr_err("bcm2708_rng_init hwrng_register()=%d\n", err);
  14802. + iounmap(rng_base);
  14803. + } else {
  14804. + /* set warm-up count & enable */
  14805. + __raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS);
  14806. + __raw_writel(RNG_RBGEN, rng_base + RNG_CTRL);
  14807. + }
  14808. + return err;
  14809. +}
  14810. +
  14811. +static void __exit bcm2708_rng_exit(void)
  14812. +{
  14813. + void __iomem *rng_base = (void __iomem *)bcm2708_rng_ops.priv;
  14814. + pr_info("bcm2708_rng_exit\n");
  14815. + /* disable rng hardware */
  14816. + __raw_writel(0, rng_base + RNG_CTRL);
  14817. + /* unregister driver */
  14818. + hwrng_unregister(&bcm2708_rng_ops);
  14819. + iounmap(rng_base);
  14820. +}
  14821. +
  14822. +module_init(bcm2708_rng_init);
  14823. +module_exit(bcm2708_rng_exit);
  14824. +
  14825. +MODULE_DESCRIPTION("BCM2708 H/W Random Number Generator (RNG) driver");
  14826. +MODULE_LICENSE("GPL and additional rights");
  14827. diff -Nur linux-3.12.33/drivers/char/hw_random/Kconfig linux-3.12.33-rpi/drivers/char/hw_random/Kconfig
  14828. --- linux-3.12.33/drivers/char/hw_random/Kconfig 2014-11-15 06:28:07.000000000 -0600
  14829. +++ linux-3.12.33-rpi/drivers/char/hw_random/Kconfig 2014-12-03 19:13:34.236418001 -0600
  14830. @@ -314,3 +314,14 @@
  14831. module will be called tpm-rng.
  14832. If unsure, say Y.
  14833. +
  14834. +config HW_RANDOM_BCM2708
  14835. + tristate "BCM2708 generic true random number generator support"
  14836. + depends on HW_RANDOM && ARCH_BCM2708
  14837. + ---help---
  14838. + This driver provides the kernel-side support for the BCM2708 hardware.
  14839. +
  14840. + To compile this driver as a module, choose M here: the
  14841. + module will be called bcm2708-rng.
  14842. +
  14843. + If unsure, say N.
  14844. diff -Nur linux-3.12.33/drivers/char/hw_random/Makefile linux-3.12.33-rpi/drivers/char/hw_random/Makefile
  14845. --- linux-3.12.33/drivers/char/hw_random/Makefile 2014-11-15 06:28:07.000000000 -0600
  14846. +++ linux-3.12.33-rpi/drivers/char/hw_random/Makefile 2014-12-03 19:13:34.236418001 -0600
  14847. @@ -27,3 +27,4 @@
  14848. obj-$(CONFIG_HW_RANDOM_EXYNOS) += exynos-rng.o
  14849. obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
  14850. obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
  14851. +obj-$(CONFIG_HW_RANDOM_BCM2708) += bcm2708-rng.o
  14852. diff -Nur linux-3.12.33/drivers/char/Kconfig linux-3.12.33-rpi/drivers/char/Kconfig
  14853. --- linux-3.12.33/drivers/char/Kconfig 2014-11-15 06:28:07.000000000 -0600
  14854. +++ linux-3.12.33-rpi/drivers/char/Kconfig 2014-12-03 19:13:34.232418001 -0600
  14855. @@ -574,6 +574,8 @@
  14856. source "drivers/s390/char/Kconfig"
  14857. +source "drivers/char/broadcom/Kconfig"
  14858. +
  14859. config MSM_SMD_PKT
  14860. bool "Enable device interface for some SMD packet ports"
  14861. default n
  14862. diff -Nur linux-3.12.33/drivers/char/Makefile linux-3.12.33-rpi/drivers/char/Makefile
  14863. --- linux-3.12.33/drivers/char/Makefile 2014-11-15 06:28:07.000000000 -0600
  14864. +++ linux-3.12.33-rpi/drivers/char/Makefile 2014-12-03 19:13:34.232418001 -0600
  14865. @@ -62,3 +62,5 @@
  14866. js-rtc-y = rtc.o
  14867. obj-$(CONFIG_TILE_SROM) += tile-srom.o
  14868. +
  14869. +obj-$(CONFIG_BRCM_CHAR_DRIVERS) += broadcom/
  14870. diff -Nur linux-3.12.33/drivers/cpufreq/bcm2835-cpufreq.c linux-3.12.33-rpi/drivers/cpufreq/bcm2835-cpufreq.c
  14871. --- linux-3.12.33/drivers/cpufreq/bcm2835-cpufreq.c 1969-12-31 18:00:00.000000000 -0600
  14872. +++ linux-3.12.33-rpi/drivers/cpufreq/bcm2835-cpufreq.c 2014-12-03 19:13:34.264418001 -0600
  14873. @@ -0,0 +1,239 @@
  14874. +/*****************************************************************************
  14875. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  14876. +*
  14877. +* Unless you and Broadcom execute a separate written software license
  14878. +* agreement governing use of this software, this software is licensed to you
  14879. +* under the terms of the GNU General Public License version 2, available at
  14880. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  14881. +*
  14882. +* Notwithstanding the above, under no circumstances may you combine this
  14883. +* software in any way with any other Broadcom software provided under a
  14884. +* license other than the GPL, without Broadcom's express prior written
  14885. +* consent.
  14886. +*****************************************************************************/
  14887. +
  14888. +/*****************************************************************************
  14889. +* FILENAME: bcm2835-cpufreq.h
  14890. +* DESCRIPTION: This driver dynamically manages the CPU Frequency of the ARM
  14891. +* processor. Messages are sent to Videocore either setting or requesting the
  14892. +* frequency of the ARM in order to match an appropiate frequency to the current
  14893. +* usage of the processor. The policy which selects the frequency to use is
  14894. +* defined in the kernel .config file, but can be changed during runtime.
  14895. +*****************************************************************************/
  14896. +
  14897. +/* ---------- INCLUDES ---------- */
  14898. +#include <linux/kernel.h>
  14899. +#include <linux/init.h>
  14900. +#include <linux/module.h>
  14901. +#include <linux/cpufreq.h>
  14902. +#include <mach/vcio.h>
  14903. +
  14904. +/* ---------- DEFINES ---------- */
  14905. +/*#define CPUFREQ_DEBUG_ENABLE*/ /* enable debugging */
  14906. +#define MODULE_NAME "bcm2835-cpufreq"
  14907. +
  14908. +#define VCMSG_ID_ARM_CLOCK 0x000000003 /* Clock/Voltage ID's */
  14909. +
  14910. +/* debug printk macros */
  14911. +#ifdef CPUFREQ_DEBUG_ENABLE
  14912. +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  14913. +#else
  14914. +#define print_debug(fmt,...)
  14915. +#endif
  14916. +#define print_err(fmt,...) pr_err("%s:%s:%d: "fmt, MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  14917. +#define print_info(fmt,...) pr_info("%s: "fmt, MODULE_NAME, ##__VA_ARGS__)
  14918. +
  14919. +/* tag part of the message */
  14920. +struct vc_msg_tag {
  14921. + uint32_t tag_id; /* the message id */
  14922. + uint32_t buffer_size; /* size of the buffer (which in this case is always 8 bytes) */
  14923. + uint32_t data_size; /* amount of data being sent or received */
  14924. + uint32_t dev_id; /* the ID of the clock/voltage to get or set */
  14925. + uint32_t val; /* the value (e.g. rate (in Hz)) to set */
  14926. +};
  14927. +
  14928. +/* message structure to be sent to videocore */
  14929. +struct vc_msg {
  14930. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  14931. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  14932. + struct vc_msg_tag tag; /* the tag structure above to make */
  14933. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  14934. +};
  14935. +
  14936. +/* ---------- GLOBALS ---------- */
  14937. +static struct cpufreq_driver bcm2835_cpufreq_driver; /* the cpufreq driver global */
  14938. +
  14939. +/*
  14940. + ===============================================
  14941. + clk_rate either gets or sets the clock rates.
  14942. + ===============================================
  14943. +*/
  14944. +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
  14945. +{
  14946. + int s, actual_rate=0;
  14947. + struct vc_msg msg;
  14948. +
  14949. + /* wipe all previous message data */
  14950. + memset(&msg, 0, sizeof msg);
  14951. +
  14952. + msg.msg_size = sizeof msg;
  14953. +
  14954. + msg.tag.tag_id = VCMSG_SET_CLOCK_RATE;
  14955. + msg.tag.buffer_size = 8;
  14956. + msg.tag.data_size = 8; /* we're sending the clock ID and the new rates which is a total of 2 words */
  14957. + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
  14958. + msg.tag.val = arm_rate * 1000;
  14959. +
  14960. + /* send the message */
  14961. + s = bcm_mailbox_property(&msg, sizeof msg);
  14962. +
  14963. + /* check if it was all ok and return the rate in KHz */
  14964. + if (s == 0 && (msg.request_code & 0x80000000))
  14965. + actual_rate = msg.tag.val/1000;
  14966. +
  14967. + print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, actual_rate);
  14968. + return actual_rate;
  14969. +}
  14970. +
  14971. +static uint32_t bcm2835_cpufreq_get_clock(int tag)
  14972. +{
  14973. + int s;
  14974. + int arm_rate = 0;
  14975. + struct vc_msg msg;
  14976. +
  14977. + /* wipe all previous message data */
  14978. + memset(&msg, 0, sizeof msg);
  14979. +
  14980. + msg.msg_size = sizeof msg;
  14981. + msg.tag.tag_id = tag;
  14982. + msg.tag.buffer_size = 8;
  14983. + msg.tag.data_size = 4; /* we're just sending the clock ID which is one word long */
  14984. + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
  14985. +
  14986. + /* send the message */
  14987. + s = bcm_mailbox_property(&msg, sizeof msg);
  14988. +
  14989. + /* check if it was all ok and return the rate in KHz */
  14990. + if (s == 0 && (msg.request_code & 0x80000000))
  14991. + arm_rate = msg.tag.val/1000;
  14992. +
  14993. + print_debug("%s frequency = %d\n",
  14994. + tag == VCMSG_GET_CLOCK_RATE ? "Current":
  14995. + tag == VCMSG_GET_MIN_CLOCK ? "Min":
  14996. + tag == VCMSG_GET_MAX_CLOCK ? "Max":
  14997. + "Unexpected", arm_rate);
  14998. +
  14999. + return arm_rate;
  15000. +}
  15001. +
  15002. +/*
  15003. + ====================================================
  15004. + Module Initialisation registers the cpufreq driver
  15005. + ====================================================
  15006. +*/
  15007. +static int __init bcm2835_cpufreq_module_init(void)
  15008. +{
  15009. + print_debug("IN\n");
  15010. + return cpufreq_register_driver(&bcm2835_cpufreq_driver);
  15011. +}
  15012. +
  15013. +/*
  15014. + =============
  15015. + Module exit
  15016. + =============
  15017. +*/
  15018. +static void __exit bcm2835_cpufreq_module_exit(void)
  15019. +{
  15020. + print_debug("IN\n");
  15021. + cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
  15022. + return;
  15023. +}
  15024. +
  15025. +/*
  15026. + ==============================================================
  15027. + Initialisation function sets up the CPU policy for first use
  15028. + ==============================================================
  15029. +*/
  15030. +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
  15031. +{
  15032. + /* measured value of how long it takes to change frequency */
  15033. + policy->cpuinfo.transition_latency = 355000; /* ns */
  15034. +
  15035. + /* now find out what the maximum and minimum frequencies are */
  15036. + policy->min = policy->cpuinfo.min_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MIN_CLOCK);
  15037. + policy->max = policy->cpuinfo.max_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MAX_CLOCK);
  15038. + policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
  15039. +
  15040. + print_info("min=%d max=%d cur=%d\n", policy->min, policy->max, policy->cur);
  15041. + return 0;
  15042. +}
  15043. +
  15044. +/*
  15045. + =================================================================================
  15046. + Target function chooses the most appropriate frequency from the table to enable
  15047. + =================================================================================
  15048. +*/
  15049. +
  15050. +static int bcm2835_cpufreq_driver_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation)
  15051. +{
  15052. + unsigned int target = target_freq;
  15053. +#ifdef CPUFREQ_DEBUG_ENABLE
  15054. + unsigned int cur = policy->cur;
  15055. +#endif
  15056. + print_debug("%s: min=%d max=%d cur=%d target=%d\n",policy->governor->name,policy->min,policy->max,policy->cur,target_freq);
  15057. +
  15058. + /* if we are above min and using ondemand, then just use max */
  15059. + if (strcmp("ondemand", policy->governor->name)==0 && target > policy->min)
  15060. + target = policy->max;
  15061. + /* if the frequency is the same, just quit */
  15062. + if (target == policy->cur)
  15063. + return 0;
  15064. +
  15065. + /* otherwise were good to set the clock frequency */
  15066. + policy->cur = bcm2835_cpufreq_set_clock(policy->cur, target);
  15067. +
  15068. + if (!policy->cur)
  15069. + {
  15070. + print_err("Error occurred setting a new frequency (%d)!\n", target);
  15071. + policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
  15072. + return -EINVAL;
  15073. + }
  15074. + print_debug("Freq %d->%d (min=%d max=%d target=%d request=%d)\n", cur, policy->cur, policy->min, policy->max, target_freq, target);
  15075. + return 0;
  15076. +}
  15077. +
  15078. +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
  15079. +{
  15080. + unsigned int actual_rate = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
  15081. + print_debug("cpu=%d\n", actual_rate);
  15082. + return actual_rate;
  15083. +}
  15084. +
  15085. +/*
  15086. + =================================================================================
  15087. + Verify ensures that when a policy is changed, it is suitable for the CPU to use
  15088. + =================================================================================
  15089. +*/
  15090. +
  15091. +static int bcm2835_cpufreq_driver_verify(struct cpufreq_policy *policy)
  15092. +{
  15093. + print_info("switching to governor %s\n", policy->governor->name);
  15094. + return 0;
  15095. +}
  15096. +
  15097. +
  15098. +/* the CPUFreq driver */
  15099. +static struct cpufreq_driver bcm2835_cpufreq_driver = {
  15100. + .name = "BCM2835 CPUFreq",
  15101. + .init = bcm2835_cpufreq_driver_init,
  15102. + .verify = bcm2835_cpufreq_driver_verify,
  15103. + .target = bcm2835_cpufreq_driver_target,
  15104. + .get = bcm2835_cpufreq_driver_get
  15105. +};
  15106. +
  15107. +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
  15108. +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
  15109. +MODULE_LICENSE("GPL");
  15110. +
  15111. +module_init(bcm2835_cpufreq_module_init);
  15112. +module_exit(bcm2835_cpufreq_module_exit);
  15113. diff -Nur linux-3.12.33/drivers/cpufreq/Kconfig.arm linux-3.12.33-rpi/drivers/cpufreq/Kconfig.arm
  15114. --- linux-3.12.33/drivers/cpufreq/Kconfig.arm 2014-11-15 06:28:07.000000000 -0600
  15115. +++ linux-3.12.33-rpi/drivers/cpufreq/Kconfig.arm 2014-12-03 19:13:34.264418001 -0600
  15116. @@ -228,6 +228,14 @@
  15117. help
  15118. This adds the CPUFreq driver support for SPEAr SOCs.
  15119. +config ARM_BCM2835_CPUFREQ
  15120. + bool "BCM2835 Driver"
  15121. + default y
  15122. + help
  15123. + This adds the CPUFreq driver for BCM2835
  15124. +
  15125. + If in doubt, say N.
  15126. +
  15127. config ARM_TEGRA_CPUFREQ
  15128. bool "TEGRA CPUFreq support"
  15129. depends on ARCH_TEGRA
  15130. diff -Nur linux-3.12.33/drivers/cpufreq/Makefile linux-3.12.33-rpi/drivers/cpufreq/Makefile
  15131. --- linux-3.12.33/drivers/cpufreq/Makefile 2014-11-15 06:28:07.000000000 -0600
  15132. +++ linux-3.12.33-rpi/drivers/cpufreq/Makefile 2014-12-03 19:13:34.264418001 -0600
  15133. @@ -76,6 +76,7 @@
  15134. obj-$(CONFIG_ARM_SA1100_CPUFREQ) += sa1100-cpufreq.o
  15135. obj-$(CONFIG_ARM_SA1110_CPUFREQ) += sa1110-cpufreq.o
  15136. obj-$(CONFIG_ARM_SPEAR_CPUFREQ) += spear-cpufreq.o
  15137. +obj-$(CONFIG_ARM_BCM2835_CPUFREQ) += bcm2835-cpufreq.o
  15138. obj-$(CONFIG_ARM_TEGRA_CPUFREQ) += tegra-cpufreq.o
  15139. ##################################################################################
  15140. diff -Nur linux-3.12.33/drivers/dma/bcm2708-dmaengine.c linux-3.12.33-rpi/drivers/dma/bcm2708-dmaengine.c
  15141. --- linux-3.12.33/drivers/dma/bcm2708-dmaengine.c 1969-12-31 18:00:00.000000000 -0600
  15142. +++ linux-3.12.33-rpi/drivers/dma/bcm2708-dmaengine.c 2014-12-03 19:13:34.288418001 -0600
  15143. @@ -0,0 +1,1041 @@
  15144. +/*
  15145. + * BCM2835 DMA engine support
  15146. + *
  15147. + * This driver supports cyclic and scatter/gather DMA transfers.
  15148. + *
  15149. + * Author: Florian Meier <florian.meier@koalo.de>
  15150. + * Gellert Weisz <gellert@raspberrypi.org>
  15151. + * Copyright 2013-2014
  15152. + *
  15153. + * Based on
  15154. + * OMAP DMAengine support by Russell King
  15155. + *
  15156. + * BCM2708 DMA Driver
  15157. + * Copyright (C) 2010 Broadcom
  15158. + *
  15159. + * Raspberry Pi PCM I2S ALSA Driver
  15160. + * Copyright (c) by Phil Poole 2013
  15161. + *
  15162. + * MARVELL MMP Peripheral DMA Driver
  15163. + * Copyright 2012 Marvell International Ltd.
  15164. + *
  15165. + * This program is free software; you can redistribute it and/or modify
  15166. + * it under the terms of the GNU General Public License as published by
  15167. + * the Free Software Foundation; either version 2 of the License, or
  15168. + * (at your option) any later version.
  15169. + *
  15170. + * This program is distributed in the hope that it will be useful,
  15171. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15172. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15173. + * GNU General Public License for more details.
  15174. + */
  15175. +
  15176. +#include <linux/dmaengine.h>
  15177. +#include <linux/dma-mapping.h>
  15178. +#include <linux/err.h>
  15179. +#include <linux/init.h>
  15180. +#include <linux/interrupt.h>
  15181. +#include <linux/list.h>
  15182. +#include <linux/module.h>
  15183. +#include <linux/platform_device.h>
  15184. +#include <linux/slab.h>
  15185. +#include <linux/io.h>
  15186. +#include <linux/spinlock.h>
  15187. +
  15188. +#ifndef CONFIG_OF
  15189. +
  15190. +/* dma manager */
  15191. +#include <mach/dma.h>
  15192. +
  15193. +#define DMA_COMPLETE DMA_SUCCESS
  15194. +
  15195. +#endif
  15196. +
  15197. +#include <linux/of.h>
  15198. +#include <linux/of_dma.h>
  15199. +
  15200. +#include "virt-dma.h"
  15201. +
  15202. +
  15203. +struct bcm2835_dmadev {
  15204. + struct dma_device ddev;
  15205. + spinlock_t lock;
  15206. + void __iomem *base;
  15207. + struct device_dma_parameters dma_parms;
  15208. +};
  15209. +
  15210. +struct bcm2835_dma_cb {
  15211. + uint32_t info;
  15212. + uint32_t src;
  15213. + uint32_t dst;
  15214. + uint32_t length;
  15215. + uint32_t stride;
  15216. + uint32_t next;
  15217. + uint32_t pad[2];
  15218. +};
  15219. +
  15220. +struct bcm2835_chan {
  15221. + struct virt_dma_chan vc;
  15222. + struct list_head node;
  15223. +
  15224. + struct dma_slave_config cfg;
  15225. + bool cyclic;
  15226. +
  15227. + int ch;
  15228. + struct bcm2835_desc *desc;
  15229. +
  15230. + void __iomem *chan_base;
  15231. + int irq_number;
  15232. +
  15233. + unsigned int dreq;
  15234. +};
  15235. +
  15236. +struct bcm2835_desc {
  15237. + struct virt_dma_desc vd;
  15238. + enum dma_transfer_direction dir;
  15239. +
  15240. + unsigned int control_block_size;
  15241. + struct bcm2835_dma_cb *control_block_base;
  15242. + dma_addr_t control_block_base_phys;
  15243. +
  15244. + unsigned int frames;
  15245. + size_t size;
  15246. +};
  15247. +
  15248. +#define BCM2835_DMA_CS 0x00
  15249. +#define BCM2835_DMA_ADDR 0x04
  15250. +#define BCM2835_DMA_SOURCE_AD 0x0c
  15251. +#define BCM2835_DMA_DEST_AD 0x10
  15252. +#define BCM2835_DMA_NEXTCB 0x1C
  15253. +
  15254. +/* DMA CS Control and Status bits */
  15255. +#define BCM2835_DMA_ACTIVE BIT(0)
  15256. +#define BCM2835_DMA_INT BIT(2)
  15257. +#define BCM2835_DMA_ISPAUSED BIT(4) /* Pause requested or not active */
  15258. +#define BCM2835_DMA_ISHELD BIT(5) /* Is held by DREQ flow control */
  15259. +#define BCM2835_DMA_ERR BIT(8)
  15260. +#define BCM2835_DMA_ABORT BIT(30) /* Stop current CB, go to next, WO */
  15261. +#define BCM2835_DMA_RESET BIT(31) /* WO, self clearing */
  15262. +
  15263. +#define BCM2835_DMA_INT_EN BIT(0)
  15264. +#define BCM2835_DMA_WAIT_RESP BIT(3)
  15265. +#define BCM2835_DMA_D_INC BIT(4)
  15266. +#define BCM2835_DMA_D_WIDTH BIT(5)
  15267. +#define BCM2835_DMA_D_DREQ BIT(6)
  15268. +#define BCM2835_DMA_S_INC BIT(8)
  15269. +#define BCM2835_DMA_S_WIDTH BIT(9)
  15270. +#define BCM2835_DMA_S_DREQ BIT(10)
  15271. +
  15272. +#define BCM2835_DMA_PER_MAP(x) ((x) << 16)
  15273. +#define BCM2835_DMA_WAITS(x) (((x)&0x1f) << 21)
  15274. +
  15275. +#define SDHCI_BCM_DMA_WAITS 20 /* delays slowing DMA transfers: 0-31 */
  15276. +
  15277. +#define BCM2835_DMA_DATA_TYPE_S8 1
  15278. +#define BCM2835_DMA_DATA_TYPE_S16 2
  15279. +#define BCM2835_DMA_DATA_TYPE_S32 4
  15280. +#define BCM2835_DMA_DATA_TYPE_S128 16
  15281. +
  15282. +#define BCM2835_DMA_BULK_MASK BIT(0)
  15283. +#define BCM2835_DMA_FIQ_MASK (BIT(2) | BIT(3))
  15284. +
  15285. +
  15286. +/* Valid only for channels 0 - 14, 15 has its own base address */
  15287. +#define BCM2835_DMA_CHAN(n) ((n) << 8) /* Base address */
  15288. +#define BCM2835_DMA_CHANIO(base, n) ((base) + BCM2835_DMA_CHAN(n))
  15289. +
  15290. +#define MAX_LITE_TRANSFER 32768
  15291. +#define MAX_NORMAL_TRANSFER 1073741824
  15292. +
  15293. +static inline struct bcm2835_dmadev *to_bcm2835_dma_dev(struct dma_device *d)
  15294. +{
  15295. + return container_of(d, struct bcm2835_dmadev, ddev);
  15296. +}
  15297. +
  15298. +static inline struct bcm2835_chan *to_bcm2835_dma_chan(struct dma_chan *c)
  15299. +{
  15300. + return container_of(c, struct bcm2835_chan, vc.chan);
  15301. +}
  15302. +
  15303. +static inline struct bcm2835_desc *to_bcm2835_dma_desc(
  15304. + struct dma_async_tx_descriptor *t)
  15305. +{
  15306. + return container_of(t, struct bcm2835_desc, vd.tx);
  15307. +}
  15308. +
  15309. +static void dma_dumpregs(struct bcm2835_chan *c)
  15310. +{
  15311. + pr_debug("-------------DMA DUMPREGS-------------\n");
  15312. + pr_debug("CS= %u\n",
  15313. + readl(c->chan_base + BCM2835_DMA_CS));
  15314. + pr_debug("ADDR= %u\n",
  15315. + readl(c->chan_base + BCM2835_DMA_ADDR));
  15316. + pr_debug("SOURCE_ADDR= %u\n",
  15317. + readl(c->chan_base + BCM2835_DMA_SOURCE_AD));
  15318. + pr_debug("DEST_AD= %u\n",
  15319. + readl(c->chan_base + BCM2835_DMA_DEST_AD));
  15320. + pr_debug("NEXTCB= %u\n",
  15321. + readl(c->chan_base + BCM2835_DMA_NEXTCB));
  15322. + pr_debug("--------------------------------------\n");
  15323. +}
  15324. +
  15325. +static void bcm2835_dma_desc_free(struct virt_dma_desc *vd)
  15326. +{
  15327. + struct bcm2835_desc *desc = container_of(vd, struct bcm2835_desc, vd);
  15328. + dma_free_coherent(desc->vd.tx.chan->device->dev,
  15329. + desc->control_block_size,
  15330. + desc->control_block_base,
  15331. + desc->control_block_base_phys);
  15332. + kfree(desc);
  15333. +}
  15334. +
  15335. +static int bcm2835_dma_abort(void __iomem *chan_base)
  15336. +{
  15337. + unsigned long cs;
  15338. + long int timeout = 10000;
  15339. +
  15340. + cs = readl(chan_base + BCM2835_DMA_CS);
  15341. + if (!(cs & BCM2835_DMA_ACTIVE))
  15342. + return 0;
  15343. +
  15344. + /* Write 0 to the active bit - Pause the DMA */
  15345. + writel(0, chan_base + BCM2835_DMA_CS);
  15346. +
  15347. + /* Wait for any current AXI transfer to complete */
  15348. + while ((cs & BCM2835_DMA_ISPAUSED) && --timeout) {
  15349. + cpu_relax();
  15350. + cs = readl(chan_base + BCM2835_DMA_CS);
  15351. + }
  15352. +
  15353. + /* We'll un-pause when we set of our next DMA */
  15354. + if (!timeout)
  15355. + return -ETIMEDOUT;
  15356. +
  15357. + if (!(cs & BCM2835_DMA_ACTIVE))
  15358. + return 0;
  15359. +
  15360. + /* Terminate the control block chain */
  15361. + writel(0, chan_base + BCM2835_DMA_NEXTCB);
  15362. +
  15363. + /* Abort the whole DMA */
  15364. + writel(BCM2835_DMA_ABORT | BCM2835_DMA_ACTIVE,
  15365. + chan_base + BCM2835_DMA_CS);
  15366. +
  15367. + return 0;
  15368. +}
  15369. +
  15370. +
  15371. +static void bcm2835_dma_start_desc(struct bcm2835_chan *c)
  15372. +{
  15373. + struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
  15374. + struct bcm2835_desc *d;
  15375. +
  15376. + if (!vd) {
  15377. + c->desc = NULL;
  15378. + return;
  15379. + }
  15380. +
  15381. + list_del(&vd->node);
  15382. +
  15383. + c->desc = d = to_bcm2835_dma_desc(&vd->tx);
  15384. +
  15385. + writel(d->control_block_base_phys, c->chan_base + BCM2835_DMA_ADDR);
  15386. + writel(BCM2835_DMA_ACTIVE, c->chan_base + BCM2835_DMA_CS);
  15387. +
  15388. +}
  15389. +
  15390. +static irqreturn_t bcm2835_dma_callback(int irq, void *data)
  15391. +{
  15392. + struct bcm2835_chan *c = data;
  15393. + struct bcm2835_desc *d;
  15394. + unsigned long flags;
  15395. +
  15396. + spin_lock_irqsave(&c->vc.lock, flags);
  15397. +
  15398. + /* Acknowledge interrupt */
  15399. + writel(BCM2835_DMA_INT, c->chan_base + BCM2835_DMA_CS);
  15400. +
  15401. + d = c->desc;
  15402. +
  15403. + if (d) {
  15404. + if (c->cyclic) {
  15405. + vchan_cyclic_callback(&d->vd);
  15406. +
  15407. + /* Keep the DMA engine running */
  15408. + writel(BCM2835_DMA_ACTIVE,
  15409. + c->chan_base + BCM2835_DMA_CS);
  15410. +
  15411. + } else {
  15412. + vchan_cookie_complete(&c->desc->vd);
  15413. + bcm2835_dma_start_desc(c);
  15414. + }
  15415. + }
  15416. +
  15417. + spin_unlock_irqrestore(&c->vc.lock, flags);
  15418. +
  15419. + return IRQ_HANDLED;
  15420. +}
  15421. +
  15422. +static int bcm2835_dma_alloc_chan_resources(struct dma_chan *chan)
  15423. +{
  15424. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  15425. + int ret;
  15426. +
  15427. + dev_dbg(c->vc.chan.device->dev,
  15428. + "Allocating DMA channel %d\n", c->ch);
  15429. +
  15430. + ret = request_irq(c->irq_number,
  15431. + bcm2835_dma_callback, 0, "DMA IRQ", c);
  15432. +
  15433. + return ret;
  15434. +}
  15435. +
  15436. +static void bcm2835_dma_free_chan_resources(struct dma_chan *chan)
  15437. +{
  15438. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  15439. +
  15440. + vchan_free_chan_resources(&c->vc);
  15441. + free_irq(c->irq_number, c);
  15442. +
  15443. + dev_dbg(c->vc.chan.device->dev, "Freeing DMA channel %u\n", c->ch);
  15444. +}
  15445. +
  15446. +static size_t bcm2835_dma_desc_size(struct bcm2835_desc *d)
  15447. +{
  15448. + return d->size;
  15449. +}
  15450. +
  15451. +static size_t bcm2835_dma_desc_size_pos(struct bcm2835_desc *d, dma_addr_t addr)
  15452. +{
  15453. + unsigned int i;
  15454. + size_t size;
  15455. +
  15456. + for (size = i = 0; i < d->frames; i++) {
  15457. + struct bcm2835_dma_cb *control_block =
  15458. + &d->control_block_base[i];
  15459. + size_t this_size = control_block->length;
  15460. + dma_addr_t dma;
  15461. +
  15462. + if (d->dir == DMA_DEV_TO_MEM)
  15463. + dma = control_block->dst;
  15464. + else
  15465. + dma = control_block->src;
  15466. +
  15467. + if (size)
  15468. + size += this_size;
  15469. + else if (addr >= dma && addr < dma + this_size)
  15470. + size += dma + this_size - addr;
  15471. + }
  15472. +
  15473. + return size;
  15474. +}
  15475. +
  15476. +static enum dma_status bcm2835_dma_tx_status(struct dma_chan *chan,
  15477. + dma_cookie_t cookie, struct dma_tx_state *txstate)
  15478. +{
  15479. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  15480. + struct bcm2835_desc *d;
  15481. + struct virt_dma_desc *vd;
  15482. + enum dma_status ret;
  15483. + unsigned long flags;
  15484. + dma_addr_t pos;
  15485. +
  15486. + ret = dma_cookie_status(chan, cookie, txstate);
  15487. + if (ret == DMA_COMPLETE || !txstate)
  15488. + return ret;
  15489. +
  15490. + spin_lock_irqsave(&c->vc.lock, flags);
  15491. + vd = vchan_find_desc(&c->vc, cookie);
  15492. + if (vd) {
  15493. + txstate->residue =
  15494. + bcm2835_dma_desc_size(to_bcm2835_dma_desc(&vd->tx));
  15495. + } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
  15496. + d = c->desc;
  15497. +
  15498. + if (d->dir == DMA_MEM_TO_DEV)
  15499. + pos = readl(c->chan_base + BCM2835_DMA_SOURCE_AD);
  15500. + else if (d->dir == DMA_DEV_TO_MEM)
  15501. + pos = readl(c->chan_base + BCM2835_DMA_DEST_AD);
  15502. + else
  15503. + pos = 0;
  15504. +
  15505. + txstate->residue = bcm2835_dma_desc_size_pos(d, pos);
  15506. + } else {
  15507. + txstate->residue = 0;
  15508. + }
  15509. +
  15510. + spin_unlock_irqrestore(&c->vc.lock, flags);
  15511. +
  15512. + return ret;
  15513. +}
  15514. +
  15515. +static void bcm2835_dma_issue_pending(struct dma_chan *chan)
  15516. +{
  15517. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  15518. + unsigned long flags;
  15519. +
  15520. + spin_lock_irqsave(&c->vc.lock, flags);
  15521. + if (vchan_issue_pending(&c->vc) && !c->desc)
  15522. + bcm2835_dma_start_desc(c);
  15523. +
  15524. + spin_unlock_irqrestore(&c->vc.lock, flags);
  15525. +}
  15526. +
  15527. +static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
  15528. + struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  15529. + size_t period_len, enum dma_transfer_direction direction,
  15530. + unsigned long flags, void *context)
  15531. +{
  15532. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  15533. + enum dma_slave_buswidth dev_width;
  15534. + struct bcm2835_desc *d;
  15535. + dma_addr_t dev_addr;
  15536. + unsigned int es, sync_type;
  15537. + unsigned int frame;
  15538. +
  15539. + /* Grab configuration */
  15540. + if (!is_slave_direction(direction)) {
  15541. + dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
  15542. + return NULL;
  15543. + }
  15544. +
  15545. + if (direction == DMA_DEV_TO_MEM) {
  15546. + dev_addr = c->cfg.src_addr;
  15547. + dev_width = c->cfg.src_addr_width;
  15548. + sync_type = BCM2835_DMA_S_DREQ;
  15549. + } else {
  15550. + dev_addr = c->cfg.dst_addr;
  15551. + dev_width = c->cfg.dst_addr_width;
  15552. + sync_type = BCM2835_DMA_D_DREQ;
  15553. + }
  15554. +
  15555. + /* Bus width translates to the element size (ES) */
  15556. + switch (dev_width) {
  15557. + case DMA_SLAVE_BUSWIDTH_4_BYTES:
  15558. + es = BCM2835_DMA_DATA_TYPE_S32;
  15559. + break;
  15560. + default:
  15561. + return NULL;
  15562. + }
  15563. +
  15564. + /* Now allocate and setup the descriptor. */
  15565. + d = kzalloc(sizeof(*d), GFP_NOWAIT);
  15566. + if (!d)
  15567. + return NULL;
  15568. +
  15569. + d->dir = direction;
  15570. + d->frames = buf_len / period_len;
  15571. +
  15572. + /* Allocate memory for control blocks */
  15573. + d->control_block_size = d->frames * sizeof(struct bcm2835_dma_cb);
  15574. + d->control_block_base = dma_zalloc_coherent(chan->device->dev,
  15575. + d->control_block_size, &d->control_block_base_phys,
  15576. + GFP_NOWAIT);
  15577. +
  15578. + if (!d->control_block_base) {
  15579. + kfree(d);
  15580. + return NULL;
  15581. + }
  15582. +
  15583. + /*
  15584. + * Iterate over all frames, create a control block
  15585. + * for each frame and link them together.
  15586. + */
  15587. + for (frame = 0; frame < d->frames; frame++) {
  15588. + struct bcm2835_dma_cb *control_block =
  15589. + &d->control_block_base[frame];
  15590. +
  15591. + /* Setup adresses */
  15592. + if (d->dir == DMA_DEV_TO_MEM) {
  15593. + control_block->info = BCM2835_DMA_D_INC;
  15594. + control_block->src = dev_addr;
  15595. + control_block->dst = buf_addr + frame * period_len;
  15596. + } else {
  15597. + control_block->info = BCM2835_DMA_S_INC;
  15598. + control_block->src = buf_addr + frame * period_len;
  15599. + control_block->dst = dev_addr;
  15600. + }
  15601. +
  15602. + /* Enable interrupt */
  15603. + control_block->info |= BCM2835_DMA_INT_EN;
  15604. +
  15605. + /* Setup synchronization */
  15606. + if (sync_type != 0)
  15607. + control_block->info |= sync_type;
  15608. +
  15609. + /* Setup DREQ channel */
  15610. + if (c->cfg.slave_id != 0)
  15611. + control_block->info |=
  15612. + BCM2835_DMA_PER_MAP(c->cfg.slave_id);
  15613. +
  15614. + /* Length of a frame */
  15615. + control_block->length = period_len;
  15616. + d->size += control_block->length;
  15617. +
  15618. + /*
  15619. + * Next block is the next frame.
  15620. + * This function is called on cyclic DMA transfers.
  15621. + * Therefore, wrap around at number of frames.
  15622. + */
  15623. + control_block->next = d->control_block_base_phys +
  15624. + sizeof(struct bcm2835_dma_cb)
  15625. + * ((frame + 1) % d->frames);
  15626. + }
  15627. +
  15628. + c->cyclic = true;
  15629. +
  15630. + return vchan_tx_prep(&c->vc, &d->vd, flags);
  15631. +}
  15632. +
  15633. +
  15634. +static struct dma_async_tx_descriptor *bcm2835_dma_prep_slave_sg(
  15635. + struct dma_chan *chan, struct scatterlist *sgl,
  15636. + unsigned int sg_len, enum dma_transfer_direction direction,
  15637. + unsigned long flags, void *context)
  15638. +{
  15639. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  15640. + enum dma_slave_buswidth dev_width;
  15641. + struct bcm2835_desc *d;
  15642. + dma_addr_t dev_addr;
  15643. + struct scatterlist *sgent;
  15644. + unsigned int es, sync_type;
  15645. + unsigned int i, j, splitct, max_size;
  15646. +
  15647. + if (!is_slave_direction(direction)) {
  15648. + dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
  15649. + return NULL;
  15650. + }
  15651. +
  15652. + if (direction == DMA_DEV_TO_MEM) {
  15653. + dev_addr = c->cfg.src_addr;
  15654. + dev_width = c->cfg.src_addr_width;
  15655. + sync_type = BCM2835_DMA_S_DREQ;
  15656. + } else {
  15657. + dev_addr = c->cfg.dst_addr;
  15658. + dev_width = c->cfg.dst_addr_width;
  15659. + sync_type = BCM2835_DMA_D_DREQ;
  15660. + }
  15661. +
  15662. + /* Bus width translates to the element size (ES) */
  15663. + switch (dev_width) {
  15664. + case DMA_SLAVE_BUSWIDTH_4_BYTES:
  15665. + es = BCM2835_DMA_DATA_TYPE_S32;
  15666. + break;
  15667. + default:
  15668. + return NULL;
  15669. + }
  15670. +
  15671. + /* Now allocate and setup the descriptor. */
  15672. + d = kzalloc(sizeof(*d), GFP_NOWAIT);
  15673. + if (!d)
  15674. + return NULL;
  15675. +
  15676. + d->dir = direction;
  15677. +
  15678. + if (c->ch >= 8) /* we have a LITE channel */
  15679. + max_size = MAX_LITE_TRANSFER;
  15680. + else
  15681. + max_size = MAX_NORMAL_TRANSFER;
  15682. +
  15683. + /* We store the length of the SG list in d->frames
  15684. + taking care to account for splitting up transfers
  15685. + too large for a LITE channel */
  15686. +
  15687. + d->frames = 0;
  15688. + for_each_sg(sgl, sgent, sg_len, i) {
  15689. + uint32_t len = sg_dma_len(sgent);
  15690. + d->frames += 1 + len / max_size;
  15691. + }
  15692. +
  15693. + /* Allocate memory for control blocks */
  15694. + d->control_block_size = d->frames * sizeof(struct bcm2835_dma_cb);
  15695. + d->control_block_base = dma_zalloc_coherent(chan->device->dev,
  15696. + d->control_block_size, &d->control_block_base_phys,
  15697. + GFP_NOWAIT);
  15698. +
  15699. + if (!d->control_block_base) {
  15700. + kfree(d);
  15701. + return NULL;
  15702. + }
  15703. +
  15704. + /*
  15705. + * Iterate over all SG entries, create a control block
  15706. + * for each frame and link them together.
  15707. + */
  15708. +
  15709. + /* we count the number of times an SG entry had to be splitct
  15710. + as a result of using a LITE channel */
  15711. + splitct = 0;
  15712. +
  15713. + for_each_sg(sgl, sgent, sg_len, i) {
  15714. + dma_addr_t addr = sg_dma_address(sgent);
  15715. + uint32_t len = sg_dma_len(sgent);
  15716. +
  15717. + for (j = 0; j < len; j += max_size) {
  15718. + struct bcm2835_dma_cb *control_block =
  15719. + &d->control_block_base[i+splitct];
  15720. +
  15721. + /* Setup adresses */
  15722. + if (d->dir == DMA_DEV_TO_MEM) {
  15723. + control_block->info = BCM2835_DMA_D_INC |
  15724. + BCM2835_DMA_D_WIDTH | BCM2835_DMA_S_DREQ;
  15725. + control_block->src = dev_addr;
  15726. + control_block->dst = addr + (dma_addr_t)j;
  15727. + } else {
  15728. + control_block->info = BCM2835_DMA_S_INC |
  15729. + BCM2835_DMA_S_WIDTH | BCM2835_DMA_D_DREQ;
  15730. + control_block->src = addr + (dma_addr_t)j;
  15731. + control_block->dst = dev_addr;
  15732. + }
  15733. +
  15734. + /* Common part */
  15735. + control_block->info |= BCM2835_DMA_WAITS(SDHCI_BCM_DMA_WAITS);
  15736. + control_block->info |= BCM2835_DMA_WAIT_RESP;
  15737. +
  15738. + /* Enable */
  15739. + if (i == sg_len-1 && len-j <= max_size)
  15740. + control_block->info |= BCM2835_DMA_INT_EN;
  15741. +
  15742. + /* Setup synchronization */
  15743. + if (sync_type != 0)
  15744. + control_block->info |= sync_type;
  15745. +
  15746. + /* Setup DREQ channel */
  15747. + c->dreq = c->cfg.slave_id; /* DREQ loaded from config */
  15748. +
  15749. + if (c->dreq != 0)
  15750. + control_block->info |=
  15751. + BCM2835_DMA_PER_MAP(c->dreq);
  15752. +
  15753. + /* Length of a frame */
  15754. + control_block->length = min(len-j, max_size);
  15755. + d->size += control_block->length;
  15756. +
  15757. + /*
  15758. + * Next block is the next frame.
  15759. + */
  15760. + if (i < sg_len-1 || len-j > max_size) {
  15761. + /* next block is the next frame. */
  15762. + control_block->next = d->control_block_base_phys +
  15763. + sizeof(struct bcm2835_dma_cb) * (i + splitct + 1);
  15764. + } else {
  15765. + /* next block is empty. */
  15766. + control_block->next = 0;
  15767. + }
  15768. +
  15769. + if (len-j > max_size)
  15770. + splitct++;
  15771. + }
  15772. + }
  15773. +
  15774. + c->cyclic = false;
  15775. +
  15776. + return vchan_tx_prep(&c->vc, &d->vd, flags);
  15777. +}
  15778. +
  15779. +static int bcm2835_dma_slave_config(struct bcm2835_chan *c,
  15780. + struct dma_slave_config *cfg)
  15781. +{
  15782. + if ((cfg->direction == DMA_DEV_TO_MEM &&
  15783. + cfg->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  15784. + (cfg->direction == DMA_MEM_TO_DEV &&
  15785. + cfg->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  15786. + !is_slave_direction(cfg->direction)) {
  15787. + return -EINVAL;
  15788. + }
  15789. +
  15790. + c->cfg = *cfg;
  15791. +
  15792. + return 0;
  15793. +}
  15794. +
  15795. +static int bcm2835_dma_terminate_all(struct bcm2835_chan *c)
  15796. +{
  15797. + struct bcm2835_dmadev *d = to_bcm2835_dma_dev(c->vc.chan.device);
  15798. + unsigned long flags;
  15799. + int timeout = 10000;
  15800. + LIST_HEAD(head);
  15801. +
  15802. + spin_lock_irqsave(&c->vc.lock, flags);
  15803. +
  15804. + /* Prevent this channel being scheduled */
  15805. + spin_lock(&d->lock);
  15806. + list_del_init(&c->node);
  15807. + spin_unlock(&d->lock);
  15808. +
  15809. + /*
  15810. + * Stop DMA activity: we assume the callback will not be called
  15811. + * after bcm_dma_abort() returns (even if it does, it will see
  15812. + * c->desc is NULL and exit.)
  15813. + */
  15814. + if (c->desc) {
  15815. + c->desc = NULL;
  15816. + bcm2835_dma_abort(c->chan_base);
  15817. +
  15818. + /* Wait for stopping */
  15819. + while (--timeout) {
  15820. + if (!(readl(c->chan_base + BCM2835_DMA_CS) &
  15821. + BCM2835_DMA_ACTIVE))
  15822. + break;
  15823. +
  15824. + cpu_relax();
  15825. + }
  15826. +
  15827. + if (!timeout)
  15828. + dev_err(d->ddev.dev, "DMA transfer could not be terminated\n");
  15829. + }
  15830. +
  15831. + vchan_get_all_descriptors(&c->vc, &head);
  15832. + spin_unlock_irqrestore(&c->vc.lock, flags);
  15833. + vchan_dma_desc_free_list(&c->vc, &head);
  15834. +
  15835. + return 0;
  15836. +}
  15837. +
  15838. +static int bcm2835_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  15839. + unsigned long arg)
  15840. +{
  15841. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  15842. +
  15843. + switch (cmd) {
  15844. + case DMA_SLAVE_CONFIG:
  15845. + return bcm2835_dma_slave_config(c,
  15846. + (struct dma_slave_config *)arg);
  15847. +
  15848. + case DMA_TERMINATE_ALL:
  15849. + return bcm2835_dma_terminate_all(c);
  15850. +
  15851. + default:
  15852. + return -ENXIO;
  15853. + }
  15854. +}
  15855. +
  15856. +#ifdef CONFIG_OF
  15857. +static int bcm2835_dma_chan_init(struct bcm2835_dmadev *d, int chan_id, int irq)
  15858. +{
  15859. + struct bcm2835_chan *c;
  15860. +
  15861. + c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
  15862. + if (!c)
  15863. + return -ENOMEM;
  15864. +
  15865. + c->vc.desc_free = bcm2835_dma_desc_free;
  15866. + vchan_init(&c->vc, &d->ddev);
  15867. + INIT_LIST_HEAD(&c->node);
  15868. +
  15869. + d->ddev.chancnt++;
  15870. +
  15871. + c->chan_base = BCM2835_DMA_CHANIO(d->base, chan_id);
  15872. + c->ch = chan_id;
  15873. + c->irq_number = irq;
  15874. +
  15875. + return 0;
  15876. +}
  15877. +#endif
  15878. +
  15879. +static int bcm2708_dma_chan_init(struct bcm2835_dmadev *d,
  15880. + void __iomem *chan_base, int chan_id, int irq)
  15881. +{
  15882. + struct bcm2835_chan *c;
  15883. +
  15884. + c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
  15885. + if (!c)
  15886. + return -ENOMEM;
  15887. +
  15888. + c->vc.desc_free = bcm2835_dma_desc_free;
  15889. + vchan_init(&c->vc, &d->ddev);
  15890. + INIT_LIST_HEAD(&c->node);
  15891. +
  15892. + d->ddev.chancnt++;
  15893. +
  15894. + c->chan_base = chan_base;
  15895. + c->ch = chan_id;
  15896. + c->irq_number = irq;
  15897. +
  15898. + return 0;
  15899. +}
  15900. +
  15901. +
  15902. +static void bcm2835_dma_free(struct bcm2835_dmadev *od)
  15903. +{
  15904. + struct bcm2835_chan *c, *next;
  15905. +
  15906. + list_for_each_entry_safe(c, next, &od->ddev.channels,
  15907. + vc.chan.device_node) {
  15908. + list_del(&c->vc.chan.device_node);
  15909. + tasklet_kill(&c->vc.task);
  15910. + }
  15911. +}
  15912. +
  15913. +static const struct of_device_id bcm2835_dma_of_match[] = {
  15914. + { .compatible = "brcm,bcm2835-dma", },
  15915. + {},
  15916. +};
  15917. +MODULE_DEVICE_TABLE(of, bcm2835_dma_of_match);
  15918. +
  15919. +#ifdef CONFIG_OF
  15920. +static struct dma_chan *bcm2835_dma_xlate(struct of_phandle_args *spec,
  15921. + struct of_dma *ofdma)
  15922. +{
  15923. + struct bcm2835_dmadev *d = ofdma->of_dma_data;
  15924. + struct dma_chan *chan;
  15925. +
  15926. + chan = dma_get_any_slave_channel(&d->ddev);
  15927. + if (!chan)
  15928. + return NULL;
  15929. +
  15930. + /* Set DREQ from param */
  15931. + to_bcm2835_dma_chan(chan)->dreq = spec->args[0];
  15932. +
  15933. + return chan;
  15934. +}
  15935. +#endif
  15936. +
  15937. +static int bcm2835_dma_device_slave_caps(struct dma_chan *dchan,
  15938. + struct dma_slave_caps *caps)
  15939. +{
  15940. + caps->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  15941. + caps->dstn_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  15942. + caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  15943. + caps->cmd_pause = false;
  15944. + caps->cmd_terminate = true;
  15945. +
  15946. + return 0;
  15947. +}
  15948. +
  15949. +static int bcm2835_dma_probe(struct platform_device *pdev)
  15950. +{
  15951. + struct bcm2835_dmadev *od;
  15952. +#ifdef CONFIG_OF
  15953. + struct resource *res;
  15954. + void __iomem *base;
  15955. + uint32_t chans_available;
  15956. +#endif
  15957. + int rc;
  15958. + int i;
  15959. + int irq;
  15960. +
  15961. +
  15962. + if (!pdev->dev.dma_mask)
  15963. + pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  15964. +
  15965. + /* If CONFIG_OF is selected, device tree is used */
  15966. + /* hence the difference between probing */
  15967. +
  15968. +#ifndef CONFIG_OF
  15969. +
  15970. + rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  15971. + if (rc)
  15972. + return rc;
  15973. + dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  15974. +
  15975. +
  15976. + od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
  15977. + if (!od)
  15978. + return -ENOMEM;
  15979. +
  15980. + pdev->dev.dma_parms = &od->dma_parms;
  15981. + dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
  15982. +
  15983. +
  15984. + dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  15985. + dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask);
  15986. + dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
  15987. + od->ddev.device_alloc_chan_resources = bcm2835_dma_alloc_chan_resources;
  15988. + od->ddev.device_free_chan_resources = bcm2835_dma_free_chan_resources;
  15989. + od->ddev.device_tx_status = bcm2835_dma_tx_status;
  15990. + od->ddev.device_issue_pending = bcm2835_dma_issue_pending;
  15991. + od->ddev.device_slave_caps = bcm2835_dma_device_slave_caps;
  15992. + od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic;
  15993. + od->ddev.device_prep_slave_sg = bcm2835_dma_prep_slave_sg;
  15994. + od->ddev.device_control = bcm2835_dma_control;
  15995. + od->ddev.dev = &pdev->dev;
  15996. + INIT_LIST_HEAD(&od->ddev.channels);
  15997. + spin_lock_init(&od->lock);
  15998. +
  15999. + platform_set_drvdata(pdev, od);
  16000. +
  16001. + for (i = 0; i < 5; i++) {
  16002. + void __iomem *chan_base;
  16003. + int chan_id;
  16004. +
  16005. + chan_id = bcm_dma_chan_alloc(BCM_DMA_FEATURE_LITE,
  16006. + &chan_base,
  16007. + &irq);
  16008. +
  16009. + if (chan_id < 0)
  16010. + break;
  16011. +
  16012. + rc = bcm2708_dma_chan_init(od, chan_base, chan_id, irq);
  16013. + if (rc)
  16014. + goto err_no_dma;
  16015. + }
  16016. +#else
  16017. + rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  16018. + if (rc)
  16019. + return rc;
  16020. +
  16021. +
  16022. + od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
  16023. + if (!od)
  16024. + return -ENOMEM;
  16025. +
  16026. + pdev->dev.dma_parms = &od->dma_parms;
  16027. + dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
  16028. +
  16029. +
  16030. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  16031. + base = devm_ioremap_resource(&pdev->dev, res);
  16032. + if (IS_ERR(base))
  16033. + return PTR_ERR(base);
  16034. +
  16035. + od->base = base;
  16036. +
  16037. +
  16038. + dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  16039. + dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask);
  16040. + dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
  16041. + od->ddev.device_alloc_chan_resources = bcm2835_dma_alloc_chan_resources;
  16042. + od->ddev.device_free_chan_resources = bcm2835_dma_free_chan_resources;
  16043. + od->ddev.device_tx_status = bcm2835_dma_tx_status;
  16044. + od->ddev.device_issue_pending = bcm2835_dma_issue_pending;
  16045. + od->ddev.device_slave_caps = bcm2835_dma_device_slave_caps;
  16046. + od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic;
  16047. + od->ddev.device_prep_slave_sg = bcm2835_dma_prep_slave_sg;
  16048. + od->ddev.device_control = bcm2835_dma_control;
  16049. + od->ddev.dev = &pdev->dev;
  16050. + INIT_LIST_HEAD(&od->ddev.channels);
  16051. + spin_lock_init(&od->lock);
  16052. +
  16053. + platform_set_drvdata(pdev, od);
  16054. +
  16055. +
  16056. + /* Request DMA channel mask from device tree */
  16057. + if (of_property_read_u32(pdev->dev.of_node,
  16058. + "brcm,dma-channel-mask",
  16059. + &chans_available)) {
  16060. + dev_err(&pdev->dev, "Failed to get channel mask\n");
  16061. + rc = -EINVAL;
  16062. + goto err_no_dma;
  16063. + }
  16064. +
  16065. +
  16066. + /*
  16067. + * Do not use the FIQ and BULK channels,
  16068. + * because they are used by the GPU.
  16069. + */
  16070. + chans_available &= ~(BCM2835_DMA_FIQ_MASK | BCM2835_DMA_BULK_MASK);
  16071. +
  16072. +
  16073. + for (i = 0; i < pdev->num_resources; i++) {
  16074. + irq = platform_get_irq(pdev, i);
  16075. + if (irq < 0)
  16076. + break;
  16077. +
  16078. + if (chans_available & (1 << i)) {
  16079. + rc = bcm2835_dma_chan_init(od, i, irq);
  16080. + if (rc)
  16081. + goto err_no_dma;
  16082. + }
  16083. + }
  16084. +
  16085. + dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", i);
  16086. +
  16087. + /* Device-tree DMA controller registration */
  16088. + rc = of_dma_controller_register(pdev->dev.of_node,
  16089. + bcm2835_dma_xlate, od);
  16090. + if (rc) {
  16091. + dev_err(&pdev->dev, "Failed to register DMA controller\n");
  16092. + goto err_no_dma;
  16093. + }
  16094. +#endif
  16095. +
  16096. + rc = dma_async_device_register(&od->ddev);
  16097. + if (rc) {
  16098. + dev_err(&pdev->dev,
  16099. + "Failed to register slave DMA engine device: %d\n", rc);
  16100. + goto err_no_dma;
  16101. + }
  16102. +
  16103. + dev_info(&pdev->dev, "Load BCM2835 DMA engine driver\n");
  16104. +
  16105. + return 0;
  16106. +
  16107. +err_no_dma:
  16108. + bcm2835_dma_free(od);
  16109. + return rc;
  16110. +}
  16111. +
  16112. +static int bcm2835_dma_remove(struct platform_device *pdev)
  16113. +{
  16114. + struct bcm2835_dmadev *od = platform_get_drvdata(pdev);
  16115. +
  16116. + dma_async_device_unregister(&od->ddev);
  16117. + bcm2835_dma_free(od);
  16118. +
  16119. + return 0;
  16120. +}
  16121. +
  16122. +#ifndef CONFIG_OF
  16123. +
  16124. +
  16125. +static struct platform_driver bcm2835_dma_driver = {
  16126. + .probe = bcm2835_dma_probe,
  16127. + .remove = bcm2835_dma_remove,
  16128. + .driver = {
  16129. + .name = "bcm2708-dmaengine",
  16130. + .owner = THIS_MODULE,
  16131. + },
  16132. +};
  16133. +
  16134. +static struct platform_device *pdev;
  16135. +
  16136. +static const struct platform_device_info bcm2835_dma_dev_info = {
  16137. + .name = "bcm2708-dmaengine",
  16138. + .id = -1,
  16139. +};
  16140. +
  16141. +static int bcm2835_dma_init(void)
  16142. +{
  16143. + int rc = platform_driver_register(&bcm2835_dma_driver);
  16144. +
  16145. + if (rc == 0) {
  16146. + pdev = platform_device_register_full(&bcm2835_dma_dev_info);
  16147. + if (IS_ERR(pdev)) {
  16148. + platform_driver_unregister(&bcm2835_dma_driver);
  16149. + rc = PTR_ERR(pdev);
  16150. + }
  16151. + }
  16152. +
  16153. + return rc;
  16154. +}
  16155. +module_init(bcm2835_dma_init); /* preferable to subsys_initcall */
  16156. +
  16157. +static void __exit bcm2835_dma_exit(void)
  16158. +{
  16159. + platform_device_unregister(pdev);
  16160. + platform_driver_unregister(&bcm2835_dma_driver);
  16161. +}
  16162. +module_exit(bcm2835_dma_exit);
  16163. +
  16164. +#else
  16165. +
  16166. +static struct platform_driver bcm2835_dma_driver = {
  16167. + .probe = bcm2835_dma_probe,
  16168. + .remove = bcm2835_dma_remove,
  16169. + .driver = {
  16170. + .name = "bcm2835-dma",
  16171. + .owner = THIS_MODULE,
  16172. + .of_match_table = of_match_ptr(bcm2835_dma_of_match),
  16173. + },
  16174. +};
  16175. +
  16176. +module_platform_driver(bcm2835_dma_driver);
  16177. +
  16178. +#endif
  16179. +
  16180. +MODULE_ALIAS("platform:bcm2835-dma");
  16181. +MODULE_DESCRIPTION("BCM2835 DMA engine driver");
  16182. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  16183. +MODULE_AUTHOR("Gellert Weisz <gellert@raspberrypi.org>");
  16184. +MODULE_LICENSE("GPL v2");
  16185. diff -Nur linux-3.12.33/drivers/dma/Kconfig linux-3.12.33-rpi/drivers/dma/Kconfig
  16186. --- linux-3.12.33/drivers/dma/Kconfig 2014-11-15 06:28:07.000000000 -0600
  16187. +++ linux-3.12.33-rpi/drivers/dma/Kconfig 2014-12-03 19:13:34.284418001 -0600
  16188. @@ -288,6 +288,12 @@
  16189. select DMA_ENGINE
  16190. select DMA_VIRTUAL_CHANNELS
  16191. +config DMA_BCM2708
  16192. + tristate "BCM2708 DMA engine support"
  16193. + depends on MACH_BCM2708
  16194. + select DMA_ENGINE
  16195. + select DMA_VIRTUAL_CHANNELS
  16196. +
  16197. config TI_CPPI41
  16198. tristate "AM33xx CPPI41 DMA support"
  16199. depends on ARCH_OMAP
  16200. diff -Nur linux-3.12.33/drivers/dma/Makefile linux-3.12.33-rpi/drivers/dma/Makefile
  16201. --- linux-3.12.33/drivers/dma/Makefile 2014-11-15 06:28:07.000000000 -0600
  16202. +++ linux-3.12.33-rpi/drivers/dma/Makefile 2014-12-03 19:13:34.284418001 -0600
  16203. @@ -37,6 +37,7 @@
  16204. obj-$(CONFIG_DMA_SA11X0) += sa11x0-dma.o
  16205. obj-$(CONFIG_MMP_TDMA) += mmp_tdma.o
  16206. obj-$(CONFIG_DMA_OMAP) += omap-dma.o
  16207. +obj-$(CONFIG_DMA_BCM2708) += bcm2708-dmaengine.o
  16208. obj-$(CONFIG_MMP_PDMA) += mmp_pdma.o
  16209. obj-$(CONFIG_DMA_JZ4740) += dma-jz4740.o
  16210. obj-$(CONFIG_TI_CPPI41) += cppi41.o
  16211. diff -Nur linux-3.12.33/drivers/hid/usbhid/hid-core.c linux-3.12.33-rpi/drivers/hid/usbhid/hid-core.c
  16212. --- linux-3.12.33/drivers/hid/usbhid/hid-core.c 2014-11-15 06:28:07.000000000 -0600
  16213. +++ linux-3.12.33-rpi/drivers/hid/usbhid/hid-core.c 2014-12-03 19:13:37.380418001 -0600
  16214. @@ -49,7 +49,7 @@
  16215. * Module parameters.
  16216. */
  16217. -static unsigned int hid_mousepoll_interval;
  16218. +static unsigned int hid_mousepoll_interval = ~0;
  16219. module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
  16220. MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
  16221. @@ -1081,8 +1081,12 @@
  16222. }
  16223. /* Change the polling interval of mice. */
  16224. - if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
  16225. - interval = hid_mousepoll_interval;
  16226. + if (hid->collection->usage == HID_GD_MOUSE) {
  16227. + if (hid_mousepoll_interval == ~0 && interval < 16)
  16228. + interval = 16;
  16229. + else if (hid_mousepoll_interval != ~0 && hid_mousepoll_interval != 0)
  16230. + interval = hid_mousepoll_interval;
  16231. + }
  16232. ret = -ENOMEM;
  16233. if (usb_endpoint_dir_in(endpoint)) {
  16234. diff -Nur linux-3.12.33/drivers/hwmon/bcm2835-hwmon.c linux-3.12.33-rpi/drivers/hwmon/bcm2835-hwmon.c
  16235. --- linux-3.12.33/drivers/hwmon/bcm2835-hwmon.c 1969-12-31 18:00:00.000000000 -0600
  16236. +++ linux-3.12.33-rpi/drivers/hwmon/bcm2835-hwmon.c 2014-12-03 19:13:37.388418001 -0600
  16237. @@ -0,0 +1,219 @@
  16238. +/*****************************************************************************
  16239. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  16240. +*
  16241. +* Unless you and Broadcom execute a separate written software license
  16242. +* agreement governing use of this software, this software is licensed to you
  16243. +* under the terms of the GNU General Public License version 2, available at
  16244. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  16245. +*
  16246. +* Notwithstanding the above, under no circumstances may you combine this
  16247. +* software in any way with any other Broadcom software provided under a
  16248. +* license other than the GPL, without Broadcom's express prior written
  16249. +* consent.
  16250. +*****************************************************************************/
  16251. +
  16252. +#include <linux/kernel.h>
  16253. +#include <linux/module.h>
  16254. +#include <linux/init.h>
  16255. +#include <linux/hwmon.h>
  16256. +#include <linux/hwmon-sysfs.h>
  16257. +#include <linux/platform_device.h>
  16258. +#include <linux/sysfs.h>
  16259. +#include <mach/vcio.h>
  16260. +#include <linux/slab.h>
  16261. +#include <linux/err.h>
  16262. +
  16263. +#define MODULE_NAME "bcm2835_hwmon"
  16264. +
  16265. +/*#define HWMON_DEBUG_ENABLE*/
  16266. +
  16267. +#ifdef HWMON_DEBUG_ENABLE
  16268. +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  16269. +#else
  16270. +#define print_debug(fmt,...)
  16271. +#endif
  16272. +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  16273. +#define print_info(fmt,...) printk(KERN_INFO "%s: "fmt"\n", MODULE_NAME, ##__VA_ARGS__)
  16274. +
  16275. +#define VC_TAG_GET_TEMP 0x00030006
  16276. +#define VC_TAG_GET_MAX_TEMP 0x0003000A
  16277. +
  16278. +/* --- STRUCTS --- */
  16279. +struct bcm2835_hwmon_data {
  16280. + struct device *hwmon_dev;
  16281. +};
  16282. +
  16283. +/* tag part of the message */
  16284. +struct vc_msg_tag {
  16285. + uint32_t tag_id; /* the tag ID for the temperature */
  16286. + uint32_t buffer_size; /* size of the buffer (should be 8) */
  16287. + uint32_t request_code; /* identifies message as a request (should be 0) */
  16288. + uint32_t id; /* extra ID field (should be 0) */
  16289. + uint32_t val; /* returned value of the temperature */
  16290. +};
  16291. +
  16292. +/* message structure to be sent to videocore */
  16293. +struct vc_msg {
  16294. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  16295. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  16296. + struct vc_msg_tag tag; /* the tag structure above to make */
  16297. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  16298. +};
  16299. +
  16300. +typedef enum {
  16301. + TEMP,
  16302. + MAX_TEMP,
  16303. +} temp_type;
  16304. +
  16305. +/* --- PROTOTYPES --- */
  16306. +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf);
  16307. +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf);
  16308. +
  16309. +/* --- GLOBALS --- */
  16310. +
  16311. +static struct bcm2835_hwmon_data *bcm2835_data;
  16312. +static struct platform_driver bcm2835_hwmon_driver;
  16313. +
  16314. +static SENSOR_DEVICE_ATTR(name, S_IRUGO,bcm2835_get_name,NULL,0);
  16315. +static SENSOR_DEVICE_ATTR(temp1_input,S_IRUGO,bcm2835_get_temp,NULL,TEMP);
  16316. +static SENSOR_DEVICE_ATTR(temp1_max,S_IRUGO,bcm2835_get_temp,NULL,MAX_TEMP);
  16317. +
  16318. +static struct attribute* bcm2835_attributes[] = {
  16319. + &sensor_dev_attr_name.dev_attr.attr,
  16320. + &sensor_dev_attr_temp1_input.dev_attr.attr,
  16321. + &sensor_dev_attr_temp1_max.dev_attr.attr,
  16322. + NULL,
  16323. +};
  16324. +
  16325. +static struct attribute_group bcm2835_attr_group = {
  16326. + .attrs = bcm2835_attributes,
  16327. +};
  16328. +
  16329. +/* --- FUNCTIONS --- */
  16330. +
  16331. +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf)
  16332. +{
  16333. + return sprintf(buf,"bcm2835_hwmon\n");
  16334. +}
  16335. +
  16336. +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf)
  16337. +{
  16338. + struct vc_msg msg;
  16339. + int result;
  16340. + uint temp = 0;
  16341. + int index = ((struct sensor_device_attribute*)to_sensor_dev_attr(attr))->index;
  16342. +
  16343. + print_debug("IN");
  16344. +
  16345. + /* wipe all previous message data */
  16346. + memset(&msg, 0, sizeof msg);
  16347. +
  16348. + /* determine the message type */
  16349. + if(index == TEMP)
  16350. + msg.tag.tag_id = VC_TAG_GET_TEMP;
  16351. + else if (index == MAX_TEMP)
  16352. + msg.tag.tag_id = VC_TAG_GET_MAX_TEMP;
  16353. + else
  16354. + {
  16355. + print_debug("Unknown temperature message!");
  16356. + return -EINVAL;
  16357. + }
  16358. +
  16359. + msg.msg_size = sizeof msg;
  16360. + msg.tag.buffer_size = 8;
  16361. +
  16362. + /* send the message */
  16363. + result = bcm_mailbox_property(&msg, sizeof msg);
  16364. +
  16365. + /* check if it was all ok and return the rate in milli degrees C */
  16366. + if (result == 0 && (msg.request_code & 0x80000000))
  16367. + temp = (uint)msg.tag.val;
  16368. + #ifdef HWMON_DEBUG_ENABLE
  16369. + else
  16370. + print_debug("Failed to get temperature!");
  16371. + #endif
  16372. + print_debug("Got temperature as %u",temp);
  16373. + print_debug("OUT");
  16374. + return sprintf(buf, "%u\n", temp);
  16375. +}
  16376. +
  16377. +
  16378. +static int bcm2835_hwmon_probe(struct platform_device *pdev)
  16379. +{
  16380. + int err;
  16381. +
  16382. + print_debug("IN");
  16383. + print_debug("HWMON Driver has been probed!");
  16384. +
  16385. + /* check that the device isn't null!*/
  16386. + if(pdev == NULL)
  16387. + {
  16388. + print_debug("Platform device is empty!");
  16389. + return -ENODEV;
  16390. + }
  16391. +
  16392. + /* allocate memory for neccessary data */
  16393. + bcm2835_data = kzalloc(sizeof(struct bcm2835_hwmon_data),GFP_KERNEL);
  16394. + if(!bcm2835_data)
  16395. + {
  16396. + print_debug("Unable to allocate memory for hwmon data!");
  16397. + err = -ENOMEM;
  16398. + goto kzalloc_error;
  16399. + }
  16400. +
  16401. + /* create the sysfs files */
  16402. + if(sysfs_create_group(&pdev->dev.kobj, &bcm2835_attr_group))
  16403. + {
  16404. + print_debug("Unable to create sysfs files!");
  16405. + err = -EFAULT;
  16406. + goto sysfs_error;
  16407. + }
  16408. +
  16409. + /* register the hwmon device */
  16410. + bcm2835_data->hwmon_dev = hwmon_device_register(&pdev->dev);
  16411. + if (IS_ERR(bcm2835_data->hwmon_dev))
  16412. + {
  16413. + err = PTR_ERR(bcm2835_data->hwmon_dev);
  16414. + goto hwmon_error;
  16415. + }
  16416. + print_debug("OUT");
  16417. + return 0;
  16418. +
  16419. + /* error goto's */
  16420. + hwmon_error:
  16421. + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
  16422. +
  16423. + sysfs_error:
  16424. + kfree(bcm2835_data);
  16425. +
  16426. + kzalloc_error:
  16427. +
  16428. + return err;
  16429. +
  16430. +}
  16431. +
  16432. +static int bcm2835_hwmon_remove(struct platform_device *pdev)
  16433. +{
  16434. + print_debug("IN");
  16435. + hwmon_device_unregister(bcm2835_data->hwmon_dev);
  16436. +
  16437. + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
  16438. + print_debug("OUT");
  16439. + return 0;
  16440. +}
  16441. +
  16442. +/* Hwmon Driver */
  16443. +static struct platform_driver bcm2835_hwmon_driver = {
  16444. + .probe = bcm2835_hwmon_probe,
  16445. + .remove = bcm2835_hwmon_remove,
  16446. + .driver = {
  16447. + .name = "bcm2835_hwmon",
  16448. + .owner = THIS_MODULE,
  16449. + },
  16450. +};
  16451. +
  16452. +MODULE_LICENSE("GPL");
  16453. +MODULE_AUTHOR("Dorian Peake");
  16454. +MODULE_DESCRIPTION("HW Monitor driver for bcm2835 chip");
  16455. +
  16456. +module_platform_driver(bcm2835_hwmon_driver);
  16457. diff -Nur linux-3.12.33/drivers/hwmon/Kconfig linux-3.12.33-rpi/drivers/hwmon/Kconfig
  16458. --- linux-3.12.33/drivers/hwmon/Kconfig 2014-11-15 06:28:07.000000000 -0600
  16459. +++ linux-3.12.33-rpi/drivers/hwmon/Kconfig 2014-12-03 19:13:37.384418001 -0600
  16460. @@ -1553,6 +1553,16 @@
  16461. help
  16462. Support for the A/D converter on MC13783 and MC13892 PMIC.
  16463. +config SENSORS_BCM2835
  16464. + depends on THERMAL_BCM2835=n
  16465. + tristate "Broadcom BCM2835 HWMON Driver"
  16466. + help
  16467. + If you say yes here you get support for the hardware
  16468. + monitoring features of the BCM2835 Chip
  16469. +
  16470. + This driver can also be built as a module. If so, the module
  16471. + will be called bcm2835-hwmon.
  16472. +
  16473. if ACPI
  16474. comment "ACPI drivers"
  16475. diff -Nur linux-3.12.33/drivers/hwmon/Makefile linux-3.12.33-rpi/drivers/hwmon/Makefile
  16476. --- linux-3.12.33/drivers/hwmon/Makefile 2014-11-15 06:28:07.000000000 -0600
  16477. +++ linux-3.12.33-rpi/drivers/hwmon/Makefile 2014-12-03 19:13:37.384418001 -0600
  16478. @@ -142,6 +142,7 @@
  16479. obj-$(CONFIG_SENSORS_W83L786NG) += w83l786ng.o
  16480. obj-$(CONFIG_SENSORS_WM831X) += wm831x-hwmon.o
  16481. obj-$(CONFIG_SENSORS_WM8350) += wm8350-hwmon.o
  16482. +obj-$(CONFIG_SENSORS_BCM2835) += bcm2835-hwmon.o
  16483. obj-$(CONFIG_PMBUS) += pmbus/
  16484. diff -Nur linux-3.12.33/drivers/i2c/busses/i2c-bcm2708.c linux-3.12.33-rpi/drivers/i2c/busses/i2c-bcm2708.c
  16485. --- linux-3.12.33/drivers/i2c/busses/i2c-bcm2708.c 1969-12-31 18:00:00.000000000 -0600
  16486. +++ linux-3.12.33-rpi/drivers/i2c/busses/i2c-bcm2708.c 2014-12-03 19:13:37.412418001 -0600
  16487. @@ -0,0 +1,448 @@
  16488. +/*
  16489. + * Driver for Broadcom BCM2708 BSC Controllers
  16490. + *
  16491. + * Copyright (C) 2012 Chris Boot & Frank Buss
  16492. + *
  16493. + * This driver is inspired by:
  16494. + * i2c-ocores.c, by Peter Korsgaard <jacmet@sunsite.dk>
  16495. + *
  16496. + * This program is free software; you can redistribute it and/or modify
  16497. + * it under the terms of the GNU General Public License as published by
  16498. + * the Free Software Foundation; either version 2 of the License, or
  16499. + * (at your option) any later version.
  16500. + *
  16501. + * This program is distributed in the hope that it will be useful,
  16502. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16503. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16504. + * GNU General Public License for more details.
  16505. + *
  16506. + * You should have received a copy of the GNU General Public License
  16507. + * along with this program; if not, write to the Free Software
  16508. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16509. + */
  16510. +
  16511. +#include <linux/kernel.h>
  16512. +#include <linux/module.h>
  16513. +#include <linux/spinlock.h>
  16514. +#include <linux/clk.h>
  16515. +#include <linux/err.h>
  16516. +#include <linux/platform_device.h>
  16517. +#include <linux/io.h>
  16518. +#include <linux/slab.h>
  16519. +#include <linux/i2c.h>
  16520. +#include <linux/interrupt.h>
  16521. +#include <linux/sched.h>
  16522. +#include <linux/wait.h>
  16523. +
  16524. +/* BSC register offsets */
  16525. +#define BSC_C 0x00
  16526. +#define BSC_S 0x04
  16527. +#define BSC_DLEN 0x08
  16528. +#define BSC_A 0x0c
  16529. +#define BSC_FIFO 0x10
  16530. +#define BSC_DIV 0x14
  16531. +#define BSC_DEL 0x18
  16532. +#define BSC_CLKT 0x1c
  16533. +
  16534. +/* Bitfields in BSC_C */
  16535. +#define BSC_C_I2CEN 0x00008000
  16536. +#define BSC_C_INTR 0x00000400
  16537. +#define BSC_C_INTT 0x00000200
  16538. +#define BSC_C_INTD 0x00000100
  16539. +#define BSC_C_ST 0x00000080
  16540. +#define BSC_C_CLEAR_1 0x00000020
  16541. +#define BSC_C_CLEAR_2 0x00000010
  16542. +#define BSC_C_READ 0x00000001
  16543. +
  16544. +/* Bitfields in BSC_S */
  16545. +#define BSC_S_CLKT 0x00000200
  16546. +#define BSC_S_ERR 0x00000100
  16547. +#define BSC_S_RXF 0x00000080
  16548. +#define BSC_S_TXE 0x00000040
  16549. +#define BSC_S_RXD 0x00000020
  16550. +#define BSC_S_TXD 0x00000010
  16551. +#define BSC_S_RXR 0x00000008
  16552. +#define BSC_S_TXW 0x00000004
  16553. +#define BSC_S_DONE 0x00000002
  16554. +#define BSC_S_TA 0x00000001
  16555. +
  16556. +#define I2C_TIMEOUT_MS 150
  16557. +
  16558. +#define DRV_NAME "bcm2708_i2c"
  16559. +
  16560. +static unsigned int baudrate = CONFIG_I2C_BCM2708_BAUDRATE;
  16561. +module_param(baudrate, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  16562. +MODULE_PARM_DESC(baudrate, "The I2C baudrate");
  16563. +
  16564. +static bool combined = false;
  16565. +module_param(combined, bool, 0644);
  16566. +MODULE_PARM_DESC(combined, "Use combined transactions");
  16567. +
  16568. +struct bcm2708_i2c {
  16569. + struct i2c_adapter adapter;
  16570. +
  16571. + spinlock_t lock;
  16572. + void __iomem *base;
  16573. + int irq;
  16574. + struct clk *clk;
  16575. +
  16576. + struct completion done;
  16577. +
  16578. + struct i2c_msg *msg;
  16579. + int pos;
  16580. + int nmsgs;
  16581. + bool error;
  16582. +};
  16583. +
  16584. +/*
  16585. + * This function sets the ALT mode on the I2C pins so that we can use them with
  16586. + * the BSC hardware.
  16587. + *
  16588. + * FIXME: This is a hack. Use pinmux / pinctrl.
  16589. + */
  16590. +static void bcm2708_i2c_init_pinmode(int id)
  16591. +{
  16592. +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  16593. +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  16594. +
  16595. + int pin;
  16596. + u32 *gpio = ioremap(GPIO_BASE, SZ_16K);
  16597. +
  16598. + BUG_ON(id != 0 && id != 1);
  16599. + /* BSC0 is on GPIO 0 & 1, BSC1 is on GPIO 2 & 3 */
  16600. + for (pin = id*2+0; pin <= id*2+1; pin++) {
  16601. +printk("bcm2708_i2c_init_pinmode(%d,%d)\n", id, pin);
  16602. + INP_GPIO(pin); /* set mode to GPIO input first */
  16603. + SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
  16604. + }
  16605. +
  16606. + iounmap(gpio);
  16607. +
  16608. +#undef INP_GPIO
  16609. +#undef SET_GPIO_ALT
  16610. +}
  16611. +
  16612. +static inline u32 bcm2708_rd(struct bcm2708_i2c *bi, unsigned reg)
  16613. +{
  16614. + return readl(bi->base + reg);
  16615. +}
  16616. +
  16617. +static inline void bcm2708_wr(struct bcm2708_i2c *bi, unsigned reg, u32 val)
  16618. +{
  16619. + writel(val, bi->base + reg);
  16620. +}
  16621. +
  16622. +static inline void bcm2708_bsc_reset(struct bcm2708_i2c *bi)
  16623. +{
  16624. + bcm2708_wr(bi, BSC_C, 0);
  16625. + bcm2708_wr(bi, BSC_S, BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);
  16626. +}
  16627. +
  16628. +static inline void bcm2708_bsc_fifo_drain(struct bcm2708_i2c *bi)
  16629. +{
  16630. + while ((bcm2708_rd(bi, BSC_S) & BSC_S_RXD) && (bi->pos < bi->msg->len))
  16631. + bi->msg->buf[bi->pos++] = bcm2708_rd(bi, BSC_FIFO);
  16632. +}
  16633. +
  16634. +static inline void bcm2708_bsc_fifo_fill(struct bcm2708_i2c *bi)
  16635. +{
  16636. + while ((bcm2708_rd(bi, BSC_S) & BSC_S_TXD) && (bi->pos < bi->msg->len))
  16637. + bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
  16638. +}
  16639. +
  16640. +static inline void bcm2708_bsc_setup(struct bcm2708_i2c *bi)
  16641. +{
  16642. + unsigned long bus_hz;
  16643. + u32 cdiv, s;
  16644. + u32 c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_ST | BSC_C_CLEAR_1;
  16645. +
  16646. + bus_hz = clk_get_rate(bi->clk);
  16647. + cdiv = bus_hz / baudrate;
  16648. + if (cdiv > 0xffff)
  16649. + cdiv = 0xffff;
  16650. +
  16651. + if (bi->msg->flags & I2C_M_RD)
  16652. + c |= BSC_C_INTR | BSC_C_READ;
  16653. + else
  16654. + c |= BSC_C_INTT;
  16655. +
  16656. + bcm2708_wr(bi, BSC_DIV, cdiv);
  16657. + bcm2708_wr(bi, BSC_A, bi->msg->addr);
  16658. + bcm2708_wr(bi, BSC_DLEN, bi->msg->len);
  16659. + if (combined)
  16660. + {
  16661. + /* Do the next two messages meet combined transaction criteria?
  16662. + - Current message is a write, next message is a read
  16663. + - Both messages to same slave address
  16664. + - Write message can fit inside FIFO (16 bytes or less) */
  16665. + if ( (bi->nmsgs > 1) &&
  16666. + !(bi->msg[0].flags & I2C_M_RD) && (bi->msg[1].flags & I2C_M_RD) &&
  16667. + (bi->msg[0].addr == bi->msg[1].addr) && (bi->msg[0].len <= 16)) {
  16668. + /* Fill FIFO with entire write message (16 byte FIFO) */
  16669. + while (bi->pos < bi->msg->len)
  16670. + bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
  16671. + /* Start write transfer (no interrupts, don't clear FIFO) */
  16672. + bcm2708_wr(bi, BSC_C, BSC_C_I2CEN | BSC_C_ST);
  16673. + /* poll for transfer start bit (should only take 1-20 polls) */
  16674. + do {
  16675. + s = bcm2708_rd(bi, BSC_S);
  16676. + } while (!(s & (BSC_S_TA | BSC_S_ERR | BSC_S_CLKT | BSC_S_DONE)));
  16677. + /* Send next read message before the write transfer finishes. */
  16678. + bi->nmsgs--;
  16679. + bi->msg++;
  16680. + bi->pos = 0;
  16681. + bcm2708_wr(bi, BSC_DLEN, bi->msg->len);
  16682. + c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_INTR | BSC_C_ST | BSC_C_READ;
  16683. + }
  16684. + }
  16685. + bcm2708_wr(bi, BSC_C, c);
  16686. +}
  16687. +
  16688. +static irqreturn_t bcm2708_i2c_interrupt(int irq, void *dev_id)
  16689. +{
  16690. + struct bcm2708_i2c *bi = dev_id;
  16691. + bool handled = true;
  16692. + u32 s;
  16693. +
  16694. + spin_lock(&bi->lock);
  16695. +
  16696. + /* we may see camera interrupts on the "other" I2C channel
  16697. + Just return if we've not sent anything */
  16698. + if (!bi->nmsgs || !bi->msg )
  16699. + goto early_exit;
  16700. +
  16701. + s = bcm2708_rd(bi, BSC_S);
  16702. +
  16703. + if (s & (BSC_S_CLKT | BSC_S_ERR)) {
  16704. + bcm2708_bsc_reset(bi);
  16705. + bi->error = true;
  16706. +
  16707. + /* wake up our bh */
  16708. + complete(&bi->done);
  16709. + } else if (s & BSC_S_DONE) {
  16710. + bi->nmsgs--;
  16711. +
  16712. + if (bi->msg->flags & I2C_M_RD)
  16713. + bcm2708_bsc_fifo_drain(bi);
  16714. +
  16715. + bcm2708_bsc_reset(bi);
  16716. +
  16717. + if (bi->nmsgs) {
  16718. + /* advance to next message */
  16719. + bi->msg++;
  16720. + bi->pos = 0;
  16721. + bcm2708_bsc_setup(bi);
  16722. + } else {
  16723. + /* wake up our bh */
  16724. + complete(&bi->done);
  16725. + }
  16726. + } else if (s & BSC_S_TXW) {
  16727. + bcm2708_bsc_fifo_fill(bi);
  16728. + } else if (s & BSC_S_RXR) {
  16729. + bcm2708_bsc_fifo_drain(bi);
  16730. + } else {
  16731. + handled = false;
  16732. + }
  16733. +
  16734. +early_exit:
  16735. + spin_unlock(&bi->lock);
  16736. +
  16737. + return handled ? IRQ_HANDLED : IRQ_NONE;
  16738. +}
  16739. +
  16740. +static int bcm2708_i2c_master_xfer(struct i2c_adapter *adap,
  16741. + struct i2c_msg *msgs, int num)
  16742. +{
  16743. + struct bcm2708_i2c *bi = adap->algo_data;
  16744. + unsigned long flags;
  16745. + int ret;
  16746. +
  16747. + spin_lock_irqsave(&bi->lock, flags);
  16748. +
  16749. + INIT_COMPLETION(bi->done);
  16750. + bi->msg = msgs;
  16751. + bi->pos = 0;
  16752. + bi->nmsgs = num;
  16753. + bi->error = false;
  16754. +
  16755. + spin_unlock_irqrestore(&bi->lock, flags);
  16756. +
  16757. + bcm2708_bsc_setup(bi);
  16758. +
  16759. + ret = wait_for_completion_timeout(&bi->done,
  16760. + msecs_to_jiffies(I2C_TIMEOUT_MS));
  16761. + if (ret == 0) {
  16762. + dev_err(&adap->dev, "transfer timed out\n");
  16763. + spin_lock_irqsave(&bi->lock, flags);
  16764. + bcm2708_bsc_reset(bi);
  16765. + spin_unlock_irqrestore(&bi->lock, flags);
  16766. + return -ETIMEDOUT;
  16767. + }
  16768. +
  16769. + return bi->error ? -EIO : num;
  16770. +}
  16771. +
  16772. +static u32 bcm2708_i2c_functionality(struct i2c_adapter *adap)
  16773. +{
  16774. + return I2C_FUNC_I2C | /*I2C_FUNC_10BIT_ADDR |*/ I2C_FUNC_SMBUS_EMUL;
  16775. +}
  16776. +
  16777. +static struct i2c_algorithm bcm2708_i2c_algorithm = {
  16778. + .master_xfer = bcm2708_i2c_master_xfer,
  16779. + .functionality = bcm2708_i2c_functionality,
  16780. +};
  16781. +
  16782. +static int bcm2708_i2c_probe(struct platform_device *pdev)
  16783. +{
  16784. + struct resource *regs;
  16785. + int irq, err = -ENOMEM;
  16786. + struct clk *clk;
  16787. + struct bcm2708_i2c *bi;
  16788. + struct i2c_adapter *adap;
  16789. + unsigned long bus_hz;
  16790. + u32 cdiv;
  16791. +
  16792. + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  16793. + if (!regs) {
  16794. + dev_err(&pdev->dev, "could not get IO memory\n");
  16795. + return -ENXIO;
  16796. + }
  16797. +
  16798. + irq = platform_get_irq(pdev, 0);
  16799. + if (irq < 0) {
  16800. + dev_err(&pdev->dev, "could not get IRQ\n");
  16801. + return irq;
  16802. + }
  16803. +
  16804. + clk = clk_get(&pdev->dev, NULL);
  16805. + if (IS_ERR(clk)) {
  16806. + dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
  16807. + return PTR_ERR(clk);
  16808. + }
  16809. +
  16810. + bcm2708_i2c_init_pinmode(pdev->id);
  16811. +
  16812. + bi = kzalloc(sizeof(*bi), GFP_KERNEL);
  16813. + if (!bi)
  16814. + goto out_clk_put;
  16815. +
  16816. + platform_set_drvdata(pdev, bi);
  16817. +
  16818. + adap = &bi->adapter;
  16819. + adap->class = I2C_CLASS_HWMON | I2C_CLASS_DDC;
  16820. + adap->algo = &bcm2708_i2c_algorithm;
  16821. + adap->algo_data = bi;
  16822. + adap->dev.parent = &pdev->dev;
  16823. + adap->nr = pdev->id;
  16824. + strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
  16825. +
  16826. + switch (pdev->id) {
  16827. + case 0:
  16828. + adap->class = I2C_CLASS_HWMON;
  16829. + break;
  16830. + case 1:
  16831. + adap->class = I2C_CLASS_DDC;
  16832. + break;
  16833. + default:
  16834. + dev_err(&pdev->dev, "can only bind to BSC 0 or 1\n");
  16835. + err = -ENXIO;
  16836. + goto out_free_bi;
  16837. + }
  16838. +
  16839. + spin_lock_init(&bi->lock);
  16840. + init_completion(&bi->done);
  16841. +
  16842. + bi->base = ioremap(regs->start, resource_size(regs));
  16843. + if (!bi->base) {
  16844. + dev_err(&pdev->dev, "could not remap memory\n");
  16845. + goto out_free_bi;
  16846. + }
  16847. +
  16848. + bi->irq = irq;
  16849. + bi->clk = clk;
  16850. +
  16851. + err = request_irq(irq, bcm2708_i2c_interrupt, IRQF_SHARED,
  16852. + dev_name(&pdev->dev), bi);
  16853. + if (err) {
  16854. + dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  16855. + goto out_iounmap;
  16856. + }
  16857. +
  16858. + bcm2708_bsc_reset(bi);
  16859. +
  16860. + err = i2c_add_numbered_adapter(adap);
  16861. + if (err < 0) {
  16862. + dev_err(&pdev->dev, "could not add I2C adapter: %d\n", err);
  16863. + goto out_free_irq;
  16864. + }
  16865. +
  16866. + bus_hz = clk_get_rate(bi->clk);
  16867. + cdiv = bus_hz / baudrate;
  16868. + if (cdiv > 0xffff) {
  16869. + cdiv = 0xffff;
  16870. + baudrate = bus_hz / cdiv;
  16871. + }
  16872. +
  16873. + dev_info(&pdev->dev, "BSC%d Controller at 0x%08lx (irq %d) (baudrate %d)\n",
  16874. + pdev->id, (unsigned long)regs->start, irq, baudrate);
  16875. +
  16876. + return 0;
  16877. +
  16878. +out_free_irq:
  16879. + free_irq(bi->irq, bi);
  16880. +out_iounmap:
  16881. + iounmap(bi->base);
  16882. +out_free_bi:
  16883. + kfree(bi);
  16884. +out_clk_put:
  16885. + clk_put(clk);
  16886. + return err;
  16887. +}
  16888. +
  16889. +static int bcm2708_i2c_remove(struct platform_device *pdev)
  16890. +{
  16891. + struct bcm2708_i2c *bi = platform_get_drvdata(pdev);
  16892. +
  16893. + platform_set_drvdata(pdev, NULL);
  16894. +
  16895. + i2c_del_adapter(&bi->adapter);
  16896. + free_irq(bi->irq, bi);
  16897. + iounmap(bi->base);
  16898. + clk_disable(bi->clk);
  16899. + clk_put(bi->clk);
  16900. + kfree(bi);
  16901. +
  16902. + return 0;
  16903. +}
  16904. +
  16905. +static struct platform_driver bcm2708_i2c_driver = {
  16906. + .driver = {
  16907. + .name = DRV_NAME,
  16908. + .owner = THIS_MODULE,
  16909. + },
  16910. + .probe = bcm2708_i2c_probe,
  16911. + .remove = bcm2708_i2c_remove,
  16912. +};
  16913. +
  16914. +// module_platform_driver(bcm2708_i2c_driver);
  16915. +
  16916. +
  16917. +static int __init bcm2708_i2c_init(void)
  16918. +{
  16919. + return platform_driver_register(&bcm2708_i2c_driver);
  16920. +}
  16921. +
  16922. +static void __exit bcm2708_i2c_exit(void)
  16923. +{
  16924. + platform_driver_unregister(&bcm2708_i2c_driver);
  16925. +}
  16926. +
  16927. +module_init(bcm2708_i2c_init);
  16928. +module_exit(bcm2708_i2c_exit);
  16929. +
  16930. +
  16931. +
  16932. +MODULE_DESCRIPTION("BSC controller driver for Broadcom BCM2708");
  16933. +MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  16934. +MODULE_LICENSE("GPL v2");
  16935. +MODULE_ALIAS("platform:" DRV_NAME);
  16936. diff -Nur linux-3.12.33/drivers/i2c/busses/Kconfig linux-3.12.33-rpi/drivers/i2c/busses/Kconfig
  16937. --- linux-3.12.33/drivers/i2c/busses/Kconfig 2014-11-15 06:28:07.000000000 -0600
  16938. +++ linux-3.12.33-rpi/drivers/i2c/busses/Kconfig 2014-12-03 19:13:37.408418001 -0600
  16939. @@ -348,6 +348,25 @@
  16940. This support is also available as a module. If so, the module
  16941. will be called i2c-bcm2835.
  16942. +config I2C_BCM2708
  16943. + tristate "BCM2708 BSC"
  16944. + depends on MACH_BCM2708
  16945. + help
  16946. + Enabling this option will add BSC (Broadcom Serial Controller)
  16947. + support for the BCM2708. BSC is a Broadcom proprietary bus compatible
  16948. + with I2C/TWI/SMBus.
  16949. +
  16950. +config I2C_BCM2708_BAUDRATE
  16951. + prompt "BCM2708 I2C baudrate"
  16952. + depends on I2C_BCM2708
  16953. + int
  16954. + default 100000
  16955. + help
  16956. + Set the I2C baudrate. This will alter the default value. A
  16957. + different baudrate can be set by using a module parameter as well. If
  16958. + no parameter is provided when loading, this is the value that will be
  16959. + used.
  16960. +
  16961. config I2C_BLACKFIN_TWI
  16962. tristate "Blackfin TWI I2C support"
  16963. depends on BLACKFIN
  16964. diff -Nur linux-3.12.33/drivers/i2c/busses/Makefile linux-3.12.33-rpi/drivers/i2c/busses/Makefile
  16965. --- linux-3.12.33/drivers/i2c/busses/Makefile 2014-11-15 06:28:07.000000000 -0600
  16966. +++ linux-3.12.33-rpi/drivers/i2c/busses/Makefile 2014-12-03 19:13:37.408418001 -0600
  16967. @@ -32,6 +32,7 @@
  16968. obj-$(CONFIG_I2C_AT91) += i2c-at91.o
  16969. obj-$(CONFIG_I2C_AU1550) += i2c-au1550.o
  16970. obj-$(CONFIG_I2C_BCM2835) += i2c-bcm2835.o
  16971. +obj-$(CONFIG_I2C_BCM2708) += i2c-bcm2708.o
  16972. obj-$(CONFIG_I2C_BLACKFIN_TWI) += i2c-bfin-twi.o
  16973. obj-$(CONFIG_I2C_CBUS_GPIO) += i2c-cbus-gpio.o
  16974. obj-$(CONFIG_I2C_CPM) += i2c-cpm.o
  16975. diff -Nur linux-3.12.33/drivers/media/platform/bcm2835/bcm2835-camera.c linux-3.12.33-rpi/drivers/media/platform/bcm2835/bcm2835-camera.c
  16976. --- linux-3.12.33/drivers/media/platform/bcm2835/bcm2835-camera.c 1969-12-31 18:00:00.000000000 -0600
  16977. +++ linux-3.12.33-rpi/drivers/media/platform/bcm2835/bcm2835-camera.c 2014-12-03 19:13:38.012418001 -0600
  16978. @@ -0,0 +1,1827 @@
  16979. +/*
  16980. + * Broadcom BM2835 V4L2 driver
  16981. + *
  16982. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  16983. + *
  16984. + * This file is subject to the terms and conditions of the GNU General Public
  16985. + * License. See the file COPYING in the main directory of this archive
  16986. + * for more details.
  16987. + *
  16988. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  16989. + * Dave Stevenson <dsteve@broadcom.com>
  16990. + * Simon Mellor <simellor@broadcom.com>
  16991. + * Luke Diamand <luked@broadcom.com>
  16992. + */
  16993. +
  16994. +#include <linux/errno.h>
  16995. +#include <linux/kernel.h>
  16996. +#include <linux/module.h>
  16997. +#include <linux/slab.h>
  16998. +#include <media/videobuf2-vmalloc.h>
  16999. +#include <media/videobuf2-dma-contig.h>
  17000. +#include <media/v4l2-device.h>
  17001. +#include <media/v4l2-ioctl.h>
  17002. +#include <media/v4l2-ctrls.h>
  17003. +#include <media/v4l2-fh.h>
  17004. +#include <media/v4l2-event.h>
  17005. +#include <media/v4l2-common.h>
  17006. +#include <linux/delay.h>
  17007. +
  17008. +#include "mmal-common.h"
  17009. +#include "mmal-encodings.h"
  17010. +#include "mmal-vchiq.h"
  17011. +#include "mmal-msg.h"
  17012. +#include "mmal-parameters.h"
  17013. +#include "bcm2835-camera.h"
  17014. +
  17015. +#define BM2835_MMAL_VERSION "0.0.2"
  17016. +#define BM2835_MMAL_MODULE_NAME "bcm2835-v4l2"
  17017. +#define MIN_WIDTH 16
  17018. +#define MIN_HEIGHT 16
  17019. +#define MAX_WIDTH 2592
  17020. +#define MAX_HEIGHT 1944
  17021. +#define MIN_BUFFER_SIZE (80*1024)
  17022. +
  17023. +#define MAX_VIDEO_MODE_WIDTH 1280
  17024. +#define MAX_VIDEO_MODE_HEIGHT 720
  17025. +
  17026. +MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
  17027. +MODULE_AUTHOR("Vincent Sanders");
  17028. +MODULE_LICENSE("GPL");
  17029. +MODULE_VERSION(BM2835_MMAL_VERSION);
  17030. +
  17031. +int bcm2835_v4l2_debug;
  17032. +module_param_named(debug, bcm2835_v4l2_debug, int, 0644);
  17033. +MODULE_PARM_DESC(bcm2835_v4l2_debug, "Debug level 0-2");
  17034. +
  17035. +int max_video_width = MAX_VIDEO_MODE_WIDTH;
  17036. +int max_video_height = MAX_VIDEO_MODE_HEIGHT;
  17037. +module_param(max_video_width, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  17038. +MODULE_PARM_DESC(max_video_width, "Threshold for video mode");
  17039. +module_param(max_video_height, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  17040. +MODULE_PARM_DESC(max_video_height, "Threshold for video mode");
  17041. +
  17042. +/* Gstreamer bug https://bugzilla.gnome.org/show_bug.cgi?id=726521
  17043. + * v4l2src does bad (and actually wrong) things when the vidioc_enum_framesizes
  17044. + * function says type V4L2_FRMSIZE_TYPE_STEPWISE, which we do by default.
  17045. + * It's happier if we just don't say anything at all, when it then
  17046. + * sets up a load of defaults that it thinks might work.
  17047. + * If gst_v4l2src_is_broken is non-zero, then we remove the function from
  17048. + * our function table list (actually switch to an alternate set, but same
  17049. + * result).
  17050. + */
  17051. +int gst_v4l2src_is_broken = 0;
  17052. +module_param(gst_v4l2src_is_broken, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  17053. +MODULE_PARM_DESC(gst_v4l2src_is_broken, "If non-zero, enable workaround for Gstreamer");
  17054. +
  17055. +static struct bm2835_mmal_dev *gdev; /* global device data */
  17056. +
  17057. +#define FPS_MIN 1
  17058. +#define FPS_MAX 90
  17059. +
  17060. +/* timeperframe: min/max and default */
  17061. +static const struct v4l2_fract
  17062. + tpf_min = {.numerator = 1, .denominator = FPS_MAX},
  17063. + tpf_max = {.numerator = 1, .denominator = FPS_MIN},
  17064. + tpf_default = {.numerator = 1000, .denominator = 30000};
  17065. +
  17066. +/* video formats */
  17067. +static struct mmal_fmt formats[] = {
  17068. + {
  17069. + .name = "4:2:0, packed YUV",
  17070. + .fourcc = V4L2_PIX_FMT_YUV420,
  17071. + .flags = 0,
  17072. + .mmal = MMAL_ENCODING_I420,
  17073. + .depth = 12,
  17074. + .mmal_component = MMAL_COMPONENT_CAMERA,
  17075. + },
  17076. + {
  17077. + .name = "4:2:2, packed, YUYV",
  17078. + .fourcc = V4L2_PIX_FMT_YUYV,
  17079. + .flags = 0,
  17080. + .mmal = MMAL_ENCODING_YUYV,
  17081. + .depth = 16,
  17082. + .mmal_component = MMAL_COMPONENT_CAMERA,
  17083. + },
  17084. + {
  17085. + .name = "RGB24 (LE)",
  17086. + .fourcc = V4L2_PIX_FMT_RGB24,
  17087. + .flags = 0,
  17088. + .mmal = MMAL_ENCODING_BGR24,
  17089. + .depth = 24,
  17090. + .mmal_component = MMAL_COMPONENT_CAMERA,
  17091. + },
  17092. + {
  17093. + .name = "JPEG",
  17094. + .fourcc = V4L2_PIX_FMT_JPEG,
  17095. + .flags = V4L2_FMT_FLAG_COMPRESSED,
  17096. + .mmal = MMAL_ENCODING_JPEG,
  17097. + .depth = 8,
  17098. + .mmal_component = MMAL_COMPONENT_IMAGE_ENCODE,
  17099. + },
  17100. + {
  17101. + .name = "H264",
  17102. + .fourcc = V4L2_PIX_FMT_H264,
  17103. + .flags = V4L2_FMT_FLAG_COMPRESSED,
  17104. + .mmal = MMAL_ENCODING_H264,
  17105. + .depth = 8,
  17106. + .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
  17107. + },
  17108. + {
  17109. + .name = "MJPEG",
  17110. + .fourcc = V4L2_PIX_FMT_MJPEG,
  17111. + .flags = V4L2_FMT_FLAG_COMPRESSED,
  17112. + .mmal = MMAL_ENCODING_MJPEG,
  17113. + .depth = 8,
  17114. + .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
  17115. + },
  17116. + {
  17117. + .name = "4:2:2, packed, YVYU",
  17118. + .fourcc = V4L2_PIX_FMT_YVYU,
  17119. + .flags = 0,
  17120. + .mmal = MMAL_ENCODING_YVYU,
  17121. + .depth = 16,
  17122. + .mmal_component = MMAL_COMPONENT_CAMERA,
  17123. + },
  17124. + {
  17125. + .name = "4:2:2, packed, VYUY",
  17126. + .fourcc = V4L2_PIX_FMT_VYUY,
  17127. + .flags = 0,
  17128. + .mmal = MMAL_ENCODING_VYUY,
  17129. + .depth = 16,
  17130. + .mmal_component = MMAL_COMPONENT_CAMERA,
  17131. + },
  17132. + {
  17133. + .name = "4:2:2, packed, UYVY",
  17134. + .fourcc = V4L2_PIX_FMT_UYVY,
  17135. + .flags = 0,
  17136. + .mmal = MMAL_ENCODING_UYVY,
  17137. + .depth = 16,
  17138. + .mmal_component = MMAL_COMPONENT_CAMERA,
  17139. + },
  17140. + {
  17141. + .name = "4:2:0, packed, NV12",
  17142. + .fourcc = V4L2_PIX_FMT_NV12,
  17143. + .flags = 0,
  17144. + .mmal = MMAL_ENCODING_NV12,
  17145. + .depth = 12,
  17146. + .mmal_component = MMAL_COMPONENT_CAMERA,
  17147. + },
  17148. + {
  17149. + .name = "RGB24 (BE)",
  17150. + .fourcc = V4L2_PIX_FMT_BGR24,
  17151. + .flags = 0,
  17152. + .mmal = MMAL_ENCODING_RGB24,
  17153. + .depth = 24,
  17154. + .mmal_component = MMAL_COMPONENT_CAMERA,
  17155. + },
  17156. + {
  17157. + .name = "4:2:0, packed YVU",
  17158. + .fourcc = V4L2_PIX_FMT_YVU420,
  17159. + .flags = 0,
  17160. + .mmal = MMAL_ENCODING_YV12,
  17161. + .depth = 12,
  17162. + .mmal_component = MMAL_COMPONENT_CAMERA,
  17163. + },
  17164. + {
  17165. + .name = "4:2:0, packed, NV21",
  17166. + .fourcc = V4L2_PIX_FMT_NV21,
  17167. + .flags = 0,
  17168. + .mmal = MMAL_ENCODING_NV21,
  17169. + .depth = 12,
  17170. + .mmal_component = MMAL_COMPONENT_CAMERA,
  17171. + },
  17172. + {
  17173. + .name = "RGB32 (BE)",
  17174. + .fourcc = V4L2_PIX_FMT_BGR32,
  17175. + .flags = 0,
  17176. + .mmal = MMAL_ENCODING_BGRA,
  17177. + .depth = 32,
  17178. + .mmal_component = MMAL_COMPONENT_CAMERA,
  17179. + },
  17180. +};
  17181. +
  17182. +static struct mmal_fmt *get_format(struct v4l2_format *f)
  17183. +{
  17184. + struct mmal_fmt *fmt;
  17185. + unsigned int k;
  17186. +
  17187. + for (k = 0; k < ARRAY_SIZE(formats); k++) {
  17188. + fmt = &formats[k];
  17189. + if (fmt->fourcc == f->fmt.pix.pixelformat)
  17190. + break;
  17191. + }
  17192. +
  17193. + if (k == ARRAY_SIZE(formats))
  17194. + return NULL;
  17195. +
  17196. + return &formats[k];
  17197. +}
  17198. +
  17199. +/* ------------------------------------------------------------------
  17200. + Videobuf queue operations
  17201. + ------------------------------------------------------------------*/
  17202. +
  17203. +static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  17204. + unsigned int *nbuffers, unsigned int *nplanes,
  17205. + unsigned int sizes[], void *alloc_ctxs[])
  17206. +{
  17207. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  17208. + unsigned long size;
  17209. +
  17210. + /* refuse queue setup if port is not configured */
  17211. + if (dev->capture.port == NULL) {
  17212. + v4l2_err(&dev->v4l2_dev,
  17213. + "%s: capture port not configured\n", __func__);
  17214. + return -EINVAL;
  17215. + }
  17216. +
  17217. + size = dev->capture.port->current_buffer.size;
  17218. + if (size == 0) {
  17219. + v4l2_err(&dev->v4l2_dev,
  17220. + "%s: capture port buffer size is zero\n", __func__);
  17221. + return -EINVAL;
  17222. + }
  17223. +
  17224. + if (*nbuffers < (dev->capture.port->current_buffer.num + 2))
  17225. + *nbuffers = (dev->capture.port->current_buffer.num + 2);
  17226. +
  17227. + *nplanes = 1;
  17228. +
  17229. + sizes[0] = size;
  17230. +
  17231. + /*
  17232. + * videobuf2-vmalloc allocator is context-less so no need to set
  17233. + * alloc_ctxs array.
  17234. + */
  17235. +
  17236. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  17237. + __func__, dev);
  17238. +
  17239. + return 0;
  17240. +}
  17241. +
  17242. +static int buffer_prepare(struct vb2_buffer *vb)
  17243. +{
  17244. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  17245. + unsigned long size;
  17246. +
  17247. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  17248. + __func__, dev);
  17249. +
  17250. + BUG_ON(dev->capture.port == NULL);
  17251. + BUG_ON(dev->capture.fmt == NULL);
  17252. +
  17253. + size = dev->capture.stride * dev->capture.height;
  17254. + if (vb2_plane_size(vb, 0) < size) {
  17255. + v4l2_err(&dev->v4l2_dev,
  17256. + "%s data will not fit into plane (%lu < %lu)\n",
  17257. + __func__, vb2_plane_size(vb, 0), size);
  17258. + return -EINVAL;
  17259. + }
  17260. +
  17261. + return 0;
  17262. +}
  17263. +
  17264. +static inline bool is_capturing(struct bm2835_mmal_dev *dev)
  17265. +{
  17266. + return dev->capture.camera_port ==
  17267. + &dev->
  17268. + component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_CAPTURE];
  17269. +}
  17270. +
  17271. +static void buffer_cb(struct vchiq_mmal_instance *instance,
  17272. + struct vchiq_mmal_port *port,
  17273. + int status,
  17274. + struct mmal_buffer *buf,
  17275. + unsigned long length, u32 mmal_flags, s64 dts, s64 pts)
  17276. +{
  17277. + struct bm2835_mmal_dev *dev = port->cb_ctx;
  17278. +
  17279. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17280. + "%s: status:%d, buf:%p, length:%lu, flags %u, pts %lld\n",
  17281. + __func__, status, buf, length, mmal_flags, pts);
  17282. +
  17283. + if (status != 0) {
  17284. + /* error in transfer */
  17285. + if (buf != NULL) {
  17286. + /* there was a buffer with the error so return it */
  17287. + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  17288. + }
  17289. + return;
  17290. + } else if (length == 0) {
  17291. + /* stream ended */
  17292. + if (buf != NULL) {
  17293. + /* this should only ever happen if the port is
  17294. + * disabled and there are buffers still queued
  17295. + */
  17296. + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  17297. + pr_debug("Empty buffer");
  17298. + } else if (dev->capture.frame_count) {
  17299. + /* grab another frame */
  17300. + if (is_capturing(dev)) {
  17301. + pr_debug("Grab another frame");
  17302. + vchiq_mmal_port_parameter_set(
  17303. + instance,
  17304. + dev->capture.
  17305. + camera_port,
  17306. + MMAL_PARAMETER_CAPTURE,
  17307. + &dev->capture.
  17308. + frame_count,
  17309. + sizeof(dev->capture.frame_count));
  17310. + }
  17311. + } else {
  17312. + /* signal frame completion */
  17313. + complete(&dev->capture.frame_cmplt);
  17314. + }
  17315. + } else {
  17316. + if (dev->capture.frame_count) {
  17317. + if (dev->capture.vc_start_timestamp != -1 &&
  17318. + pts != 0) {
  17319. + s64 runtime_us = pts -
  17320. + dev->capture.vc_start_timestamp;
  17321. + u32 div = 0;
  17322. + u32 rem = 0;
  17323. +
  17324. + div =
  17325. + div_u64_rem(runtime_us, USEC_PER_SEC, &rem);
  17326. + buf->vb.v4l2_buf.timestamp.tv_sec =
  17327. + dev->capture.kernel_start_ts.tv_sec - 1 +
  17328. + div;
  17329. + buf->vb.v4l2_buf.timestamp.tv_usec =
  17330. + dev->capture.kernel_start_ts.tv_usec + rem;
  17331. +
  17332. + if (buf->vb.v4l2_buf.timestamp.tv_usec >=
  17333. + USEC_PER_SEC) {
  17334. + buf->vb.v4l2_buf.timestamp.tv_sec++;
  17335. + buf->vb.v4l2_buf.timestamp.tv_usec -=
  17336. + USEC_PER_SEC;
  17337. + }
  17338. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17339. + "Convert start time %d.%06d and %llu "
  17340. + "with offset %llu to %d.%06d\n",
  17341. + (int)dev->capture.kernel_start_ts.
  17342. + tv_sec,
  17343. + (int)dev->capture.kernel_start_ts.
  17344. + tv_usec,
  17345. + dev->capture.vc_start_timestamp, pts,
  17346. + (int)buf->vb.v4l2_buf.timestamp.tv_sec,
  17347. + (int)buf->vb.v4l2_buf.timestamp.
  17348. + tv_usec);
  17349. + } else {
  17350. + v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
  17351. + }
  17352. +
  17353. + vb2_set_plane_payload(&buf->vb, 0, length);
  17354. + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
  17355. +
  17356. + if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
  17357. + is_capturing(dev)) {
  17358. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17359. + "Grab another frame as buffer has EOS");
  17360. + vchiq_mmal_port_parameter_set(
  17361. + instance,
  17362. + dev->capture.
  17363. + camera_port,
  17364. + MMAL_PARAMETER_CAPTURE,
  17365. + &dev->capture.
  17366. + frame_count,
  17367. + sizeof(dev->capture.frame_count));
  17368. + }
  17369. + } else {
  17370. + /* signal frame completion */
  17371. + complete(&dev->capture.frame_cmplt);
  17372. + }
  17373. + }
  17374. +}
  17375. +
  17376. +static int enable_camera(struct bm2835_mmal_dev *dev)
  17377. +{
  17378. + int ret;
  17379. + if (!dev->camera_use_count) {
  17380. + ret = vchiq_mmal_component_enable(
  17381. + dev->instance,
  17382. + dev->component[MMAL_COMPONENT_CAMERA]);
  17383. + if (ret < 0) {
  17384. + v4l2_err(&dev->v4l2_dev,
  17385. + "Failed enabling camera, ret %d\n", ret);
  17386. + return -EINVAL;
  17387. + }
  17388. + }
  17389. + dev->camera_use_count++;
  17390. + v4l2_dbg(1, bcm2835_v4l2_debug,
  17391. + &dev->v4l2_dev, "enabled camera (refcount %d)\n",
  17392. + dev->camera_use_count);
  17393. + return 0;
  17394. +}
  17395. +
  17396. +static int disable_camera(struct bm2835_mmal_dev *dev)
  17397. +{
  17398. + int ret;
  17399. + if (!dev->camera_use_count) {
  17400. + v4l2_err(&dev->v4l2_dev,
  17401. + "Disabled the camera when already disabled\n");
  17402. + return -EINVAL;
  17403. + }
  17404. + dev->camera_use_count--;
  17405. + if (!dev->camera_use_count) {
  17406. + unsigned int i = 0xFFFFFFFF;
  17407. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17408. + "Disabling camera\n");
  17409. + ret =
  17410. + vchiq_mmal_component_disable(
  17411. + dev->instance,
  17412. + dev->component[MMAL_COMPONENT_CAMERA]);
  17413. + if (ret < 0) {
  17414. + v4l2_err(&dev->v4l2_dev,
  17415. + "Failed disabling camera, ret %d\n", ret);
  17416. + return -EINVAL;
  17417. + }
  17418. + vchiq_mmal_port_parameter_set(
  17419. + dev->instance,
  17420. + &dev->component[MMAL_COMPONENT_CAMERA]->control,
  17421. + MMAL_PARAMETER_CAMERA_NUM, &i,
  17422. + sizeof(i));
  17423. + }
  17424. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17425. + "Camera refcount now %d\n", dev->camera_use_count);
  17426. + return 0;
  17427. +}
  17428. +
  17429. +static void buffer_queue(struct vb2_buffer *vb)
  17430. +{
  17431. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  17432. + struct mmal_buffer *buf = container_of(vb, struct mmal_buffer, vb);
  17433. + int ret;
  17434. +
  17435. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17436. + "%s: dev:%p buf:%p\n", __func__, dev, buf);
  17437. +
  17438. + buf->buffer = vb2_plane_vaddr(&buf->vb, 0);
  17439. + buf->buffer_size = vb2_plane_size(&buf->vb, 0);
  17440. +
  17441. + ret = vchiq_mmal_submit_buffer(dev->instance, dev->capture.port, buf);
  17442. + if (ret < 0)
  17443. + v4l2_err(&dev->v4l2_dev, "%s: error submitting buffer\n",
  17444. + __func__);
  17445. +}
  17446. +
  17447. +static int start_streaming(struct vb2_queue *vq, unsigned int count)
  17448. +{
  17449. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  17450. + int ret;
  17451. + int parameter_size;
  17452. +
  17453. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  17454. + __func__, dev);
  17455. +
  17456. + /* ensure a format has actually been set */
  17457. + if (dev->capture.port == NULL)
  17458. + return -EINVAL;
  17459. +
  17460. + if (enable_camera(dev) < 0) {
  17461. + v4l2_err(&dev->v4l2_dev, "Failed to enable camera\n");
  17462. + return -EINVAL;
  17463. + }
  17464. +
  17465. + /*init_completion(&dev->capture.frame_cmplt); */
  17466. +
  17467. + /* enable frame capture */
  17468. + dev->capture.frame_count = 1;
  17469. +
  17470. + /* if the preview is not already running, wait for a few frames for AGC
  17471. + * to settle down.
  17472. + */
  17473. + if (!dev->component[MMAL_COMPONENT_PREVIEW]->enabled)
  17474. + msleep(300);
  17475. +
  17476. + /* enable the connection from camera to encoder (if applicable) */
  17477. + if (dev->capture.camera_port != dev->capture.port
  17478. + && dev->capture.camera_port) {
  17479. + ret = vchiq_mmal_port_enable(dev->instance,
  17480. + dev->capture.camera_port, NULL);
  17481. + if (ret) {
  17482. + v4l2_err(&dev->v4l2_dev,
  17483. + "Failed to enable encode tunnel - error %d\n",
  17484. + ret);
  17485. + return -1;
  17486. + }
  17487. + }
  17488. +
  17489. + /* Get VC timestamp at this point in time */
  17490. + parameter_size = sizeof(dev->capture.vc_start_timestamp);
  17491. + if (vchiq_mmal_port_parameter_get(dev->instance,
  17492. + dev->capture.camera_port,
  17493. + MMAL_PARAMETER_SYSTEM_TIME,
  17494. + &dev->capture.vc_start_timestamp,
  17495. + &parameter_size)) {
  17496. + v4l2_err(&dev->v4l2_dev,
  17497. + "Failed to get VC start time - update your VC f/w\n");
  17498. +
  17499. + /* Flag to indicate just to rely on kernel timestamps */
  17500. + dev->capture.vc_start_timestamp = -1;
  17501. + } else
  17502. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17503. + "Start time %lld size %d\n",
  17504. + dev->capture.vc_start_timestamp, parameter_size);
  17505. +
  17506. + v4l2_get_timestamp(&dev->capture.kernel_start_ts);
  17507. +
  17508. + /* enable the camera port */
  17509. + dev->capture.port->cb_ctx = dev;
  17510. + ret =
  17511. + vchiq_mmal_port_enable(dev->instance, dev->capture.port, buffer_cb);
  17512. + if (ret) {
  17513. + v4l2_err(&dev->v4l2_dev,
  17514. + "Failed to enable capture port - error %d. "
  17515. + "Disabling camera port again\n", ret);
  17516. +
  17517. + vchiq_mmal_port_disable(dev->instance,
  17518. + dev->capture.camera_port);
  17519. + if (disable_camera(dev) < 0) {
  17520. + v4l2_err(&dev->v4l2_dev, "Failed to disable camera");
  17521. + return -EINVAL;
  17522. + }
  17523. + return -1;
  17524. + }
  17525. +
  17526. + /* capture the first frame */
  17527. + vchiq_mmal_port_parameter_set(dev->instance,
  17528. + dev->capture.camera_port,
  17529. + MMAL_PARAMETER_CAPTURE,
  17530. + &dev->capture.frame_count,
  17531. + sizeof(dev->capture.frame_count));
  17532. + return 0;
  17533. +}
  17534. +
  17535. +/* abort streaming and wait for last buffer */
  17536. +static int stop_streaming(struct vb2_queue *vq)
  17537. +{
  17538. + int ret;
  17539. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  17540. +
  17541. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  17542. + __func__, dev);
  17543. +
  17544. + init_completion(&dev->capture.frame_cmplt);
  17545. + dev->capture.frame_count = 0;
  17546. +
  17547. + /* ensure a format has actually been set */
  17548. + if (dev->capture.port == NULL)
  17549. + return -EINVAL;
  17550. +
  17551. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "stopping capturing\n");
  17552. +
  17553. + /* stop capturing frames */
  17554. + vchiq_mmal_port_parameter_set(dev->instance,
  17555. + dev->capture.camera_port,
  17556. + MMAL_PARAMETER_CAPTURE,
  17557. + &dev->capture.frame_count,
  17558. + sizeof(dev->capture.frame_count));
  17559. +
  17560. + /* wait for last frame to complete */
  17561. + ret = wait_for_completion_timeout(&dev->capture.frame_cmplt, HZ);
  17562. + if (ret <= 0)
  17563. + v4l2_err(&dev->v4l2_dev,
  17564. + "error %d waiting for frame completion\n", ret);
  17565. +
  17566. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17567. + "disabling connection\n");
  17568. +
  17569. + /* disable the connection from camera to encoder */
  17570. + ret = vchiq_mmal_port_disable(dev->instance, dev->capture.camera_port);
  17571. + if (!ret && dev->capture.camera_port != dev->capture.port) {
  17572. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17573. + "disabling port\n");
  17574. + ret = vchiq_mmal_port_disable(dev->instance, dev->capture.port);
  17575. + } else if (dev->capture.camera_port != dev->capture.port) {
  17576. + v4l2_err(&dev->v4l2_dev, "port_disable failed, error %d\n",
  17577. + ret);
  17578. + }
  17579. +
  17580. + if (disable_camera(dev) < 0) {
  17581. + v4l2_err(&dev->v4l2_dev, "Failed to disable camera");
  17582. + return -EINVAL;
  17583. + }
  17584. +
  17585. + return ret;
  17586. +}
  17587. +
  17588. +static void bm2835_mmal_lock(struct vb2_queue *vq)
  17589. +{
  17590. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  17591. + mutex_lock(&dev->mutex);
  17592. +}
  17593. +
  17594. +static void bm2835_mmal_unlock(struct vb2_queue *vq)
  17595. +{
  17596. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  17597. + mutex_unlock(&dev->mutex);
  17598. +}
  17599. +
  17600. +static struct vb2_ops bm2835_mmal_video_qops = {
  17601. + .queue_setup = queue_setup,
  17602. + .buf_prepare = buffer_prepare,
  17603. + .buf_queue = buffer_queue,
  17604. + .start_streaming = start_streaming,
  17605. + .stop_streaming = stop_streaming,
  17606. + .wait_prepare = bm2835_mmal_unlock,
  17607. + .wait_finish = bm2835_mmal_lock,
  17608. +};
  17609. +
  17610. +/* ------------------------------------------------------------------
  17611. + IOCTL operations
  17612. + ------------------------------------------------------------------*/
  17613. +
  17614. +/* overlay ioctl */
  17615. +static int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
  17616. + struct v4l2_fmtdesc *f)
  17617. +{
  17618. + struct mmal_fmt *fmt;
  17619. +
  17620. + if (f->index >= ARRAY_SIZE(formats))
  17621. + return -EINVAL;
  17622. +
  17623. + fmt = &formats[f->index];
  17624. +
  17625. + strlcpy(f->description, fmt->name, sizeof(f->description));
  17626. + f->pixelformat = fmt->fourcc;
  17627. + f->flags = fmt->flags;
  17628. +
  17629. + return 0;
  17630. +}
  17631. +
  17632. +static int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
  17633. + struct v4l2_format *f)
  17634. +{
  17635. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17636. +
  17637. + f->fmt.win = dev->overlay;
  17638. +
  17639. + return 0;
  17640. +}
  17641. +
  17642. +static int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
  17643. + struct v4l2_format *f)
  17644. +{
  17645. + /* Only support one format so get the current one. */
  17646. + vidioc_g_fmt_vid_overlay(file, priv, f);
  17647. +
  17648. + /* todo: allow the size and/or offset to be changed. */
  17649. + return 0;
  17650. +}
  17651. +
  17652. +static int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
  17653. + struct v4l2_format *f)
  17654. +{
  17655. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17656. +
  17657. + vidioc_try_fmt_vid_overlay(file, priv, f);
  17658. +
  17659. + dev->overlay = f->fmt.win;
  17660. +
  17661. + /* todo: program the preview port parameters */
  17662. + return 0;
  17663. +}
  17664. +
  17665. +static int vidioc_overlay(struct file *file, void *f, unsigned int on)
  17666. +{
  17667. + int ret;
  17668. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17669. + struct vchiq_mmal_port *src;
  17670. + struct vchiq_mmal_port *dst;
  17671. + struct mmal_parameter_displayregion prev_config = {
  17672. + .set = MMAL_DISPLAY_SET_LAYER | MMAL_DISPLAY_SET_ALPHA |
  17673. + MMAL_DISPLAY_SET_DEST_RECT | MMAL_DISPLAY_SET_FULLSCREEN,
  17674. + .layer = PREVIEW_LAYER,
  17675. + .alpha = 255,
  17676. + .fullscreen = 0,
  17677. + .dest_rect = {
  17678. + .x = dev->overlay.w.left,
  17679. + .y = dev->overlay.w.top,
  17680. + .width = dev->overlay.w.width,
  17681. + .height = dev->overlay.w.height,
  17682. + },
  17683. + };
  17684. +
  17685. + if ((on && dev->component[MMAL_COMPONENT_PREVIEW]->enabled) ||
  17686. + (!on && !dev->component[MMAL_COMPONENT_PREVIEW]->enabled))
  17687. + return 0; /* already in requested state */
  17688. +
  17689. + src =
  17690. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17691. + output[MMAL_CAMERA_PORT_PREVIEW];
  17692. +
  17693. + if (!on) {
  17694. + /* disconnect preview ports and disable component */
  17695. + ret = vchiq_mmal_port_disable(dev->instance, src);
  17696. + if (!ret)
  17697. + ret =
  17698. + vchiq_mmal_port_connect_tunnel(dev->instance, src,
  17699. + NULL);
  17700. + if (ret >= 0)
  17701. + ret = vchiq_mmal_component_disable(
  17702. + dev->instance,
  17703. + dev->component[MMAL_COMPONENT_PREVIEW]);
  17704. +
  17705. + disable_camera(dev);
  17706. + return ret;
  17707. + }
  17708. +
  17709. + /* set preview port format and connect it to output */
  17710. + dst = &dev->component[MMAL_COMPONENT_PREVIEW]->input[0];
  17711. +
  17712. + ret = vchiq_mmal_port_set_format(dev->instance, src);
  17713. + if (ret < 0)
  17714. + goto error;
  17715. +
  17716. + ret = vchiq_mmal_port_parameter_set(dev->instance, dst,
  17717. + MMAL_PARAMETER_DISPLAYREGION,
  17718. + &prev_config, sizeof(prev_config));
  17719. + if (ret < 0)
  17720. + goto error;
  17721. +
  17722. + if (enable_camera(dev) < 0)
  17723. + goto error;
  17724. +
  17725. + ret = vchiq_mmal_component_enable(
  17726. + dev->instance,
  17727. + dev->component[MMAL_COMPONENT_PREVIEW]);
  17728. + if (ret < 0)
  17729. + goto error;
  17730. +
  17731. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
  17732. + src, dst);
  17733. + ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
  17734. + if (!ret)
  17735. + ret = vchiq_mmal_port_enable(dev->instance, src, NULL);
  17736. +error:
  17737. + return ret;
  17738. +}
  17739. +
  17740. +static int vidioc_g_fbuf(struct file *file, void *fh,
  17741. + struct v4l2_framebuffer *a)
  17742. +{
  17743. + /* The video overlay must stay within the framebuffer and can't be
  17744. + positioned independently. */
  17745. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17746. + struct vchiq_mmal_port *preview_port =
  17747. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17748. + output[MMAL_CAMERA_PORT_PREVIEW];
  17749. + a->flags = V4L2_FBUF_FLAG_OVERLAY;
  17750. + a->fmt.width = preview_port->es.video.width;
  17751. + a->fmt.height = preview_port->es.video.height;
  17752. + a->fmt.pixelformat = V4L2_PIX_FMT_YUV420;
  17753. + a->fmt.bytesperline = (preview_port->es.video.width * 3)>>1;
  17754. + a->fmt.sizeimage = (preview_port->es.video.width *
  17755. + preview_port->es.video.height * 3)>>1;
  17756. + a->fmt.colorspace = V4L2_COLORSPACE_JPEG;
  17757. +
  17758. + return 0;
  17759. +}
  17760. +
  17761. +/* input ioctls */
  17762. +static int vidioc_enum_input(struct file *file, void *priv,
  17763. + struct v4l2_input *inp)
  17764. +{
  17765. + /* only a single camera input */
  17766. + if (inp->index != 0)
  17767. + return -EINVAL;
  17768. +
  17769. + inp->type = V4L2_INPUT_TYPE_CAMERA;
  17770. + sprintf(inp->name, "Camera %u", inp->index);
  17771. + return 0;
  17772. +}
  17773. +
  17774. +static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  17775. +{
  17776. + *i = 0;
  17777. + return 0;
  17778. +}
  17779. +
  17780. +static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  17781. +{
  17782. + if (i != 0)
  17783. + return -EINVAL;
  17784. +
  17785. + return 0;
  17786. +}
  17787. +
  17788. +/* capture ioctls */
  17789. +static int vidioc_querycap(struct file *file, void *priv,
  17790. + struct v4l2_capability *cap)
  17791. +{
  17792. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17793. + u32 major;
  17794. + u32 minor;
  17795. +
  17796. + vchiq_mmal_version(dev->instance, &major, &minor);
  17797. +
  17798. + strcpy(cap->driver, "bm2835 mmal");
  17799. + snprintf(cap->card, sizeof(cap->card), "mmal service %d.%d",
  17800. + major, minor);
  17801. +
  17802. + snprintf(cap->bus_info, sizeof(cap->bus_info),
  17803. + "platform:%s", dev->v4l2_dev.name);
  17804. + cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
  17805. + V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
  17806. + cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  17807. +
  17808. + return 0;
  17809. +}
  17810. +
  17811. +static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  17812. + struct v4l2_fmtdesc *f)
  17813. +{
  17814. + struct mmal_fmt *fmt;
  17815. +
  17816. + if (f->index >= ARRAY_SIZE(formats))
  17817. + return -EINVAL;
  17818. +
  17819. + fmt = &formats[f->index];
  17820. +
  17821. + strlcpy(f->description, fmt->name, sizeof(f->description));
  17822. + f->pixelformat = fmt->fourcc;
  17823. + f->flags = fmt->flags;
  17824. +
  17825. + return 0;
  17826. +}
  17827. +
  17828. +static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  17829. + struct v4l2_format *f)
  17830. +{
  17831. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17832. +
  17833. + f->fmt.pix.width = dev->capture.width;
  17834. + f->fmt.pix.height = dev->capture.height;
  17835. + f->fmt.pix.field = V4L2_FIELD_NONE;
  17836. + f->fmt.pix.pixelformat = dev->capture.fmt->fourcc;
  17837. + f->fmt.pix.bytesperline = dev->capture.stride;
  17838. + f->fmt.pix.sizeimage = dev->capture.buffersize;
  17839. +
  17840. + if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_RGB24)
  17841. + f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  17842. + else
  17843. + f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  17844. + f->fmt.pix.priv = 0;
  17845. +
  17846. + v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
  17847. + __func__);
  17848. + return 0;
  17849. +}
  17850. +
  17851. +static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  17852. + struct v4l2_format *f)
  17853. +{
  17854. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17855. + struct mmal_fmt *mfmt;
  17856. +
  17857. + mfmt = get_format(f);
  17858. + if (!mfmt) {
  17859. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17860. + "Fourcc format (0x%08x) unknown.\n",
  17861. + f->fmt.pix.pixelformat);
  17862. + f->fmt.pix.pixelformat = formats[0].fourcc;
  17863. + mfmt = get_format(f);
  17864. + }
  17865. +
  17866. + f->fmt.pix.field = V4L2_FIELD_NONE;
  17867. +
  17868. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17869. + "Clipping/aligning %dx%d format %08X\n",
  17870. + f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
  17871. +
  17872. + v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, MAX_WIDTH, 1,
  17873. + &f->fmt.pix.height, MIN_HEIGHT, MAX_HEIGHT, 1, 0);
  17874. + f->fmt.pix.bytesperline = (f->fmt.pix.width * mfmt->depth)>>3;
  17875. +
  17876. + /* Image buffer has to be padded to allow for alignment, even though
  17877. + * we then remove that padding before delivering the buffer.
  17878. + */
  17879. + f->fmt.pix.sizeimage = ((f->fmt.pix.height+15)&~15) *
  17880. + (((f->fmt.pix.width+31)&~31) * mfmt->depth) >> 3;
  17881. +
  17882. + if ((mfmt->flags & V4L2_FMT_FLAG_COMPRESSED) &&
  17883. + f->fmt.pix.sizeimage < MIN_BUFFER_SIZE)
  17884. + f->fmt.pix.sizeimage = MIN_BUFFER_SIZE;
  17885. +
  17886. + if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_RGB24)
  17887. + f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  17888. + else
  17889. + f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  17890. + f->fmt.pix.priv = 0;
  17891. +
  17892. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17893. + "Now %dx%d format %08X\n",
  17894. + f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
  17895. +
  17896. + v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
  17897. + __func__);
  17898. + return 0;
  17899. +}
  17900. +
  17901. +static int mmal_setup_components(struct bm2835_mmal_dev *dev,
  17902. + struct v4l2_format *f)
  17903. +{
  17904. + int ret;
  17905. + struct vchiq_mmal_port *port = NULL, *camera_port = NULL;
  17906. + struct vchiq_mmal_component *encode_component = NULL;
  17907. + struct mmal_fmt *mfmt = get_format(f);
  17908. +
  17909. + BUG_ON(!mfmt);
  17910. +
  17911. + if (dev->capture.encode_component) {
  17912. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  17913. + "vid_cap - disconnect previous tunnel\n");
  17914. +
  17915. + /* Disconnect any previous connection */
  17916. + vchiq_mmal_port_connect_tunnel(dev->instance,
  17917. + dev->capture.camera_port, NULL);
  17918. + dev->capture.camera_port = NULL;
  17919. + ret = vchiq_mmal_component_disable(dev->instance,
  17920. + dev->capture.
  17921. + encode_component);
  17922. + if (ret)
  17923. + v4l2_err(&dev->v4l2_dev,
  17924. + "Failed to disable encode component %d\n",
  17925. + ret);
  17926. +
  17927. + dev->capture.encode_component = NULL;
  17928. + }
  17929. + /* format dependant port setup */
  17930. + switch (mfmt->mmal_component) {
  17931. + case MMAL_COMPONENT_CAMERA:
  17932. + /* Make a further decision on port based on resolution */
  17933. + if (f->fmt.pix.width <= max_video_width
  17934. + && f->fmt.pix.height <= max_video_height)
  17935. + camera_port = port =
  17936. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17937. + output[MMAL_CAMERA_PORT_VIDEO];
  17938. + else
  17939. + camera_port = port =
  17940. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17941. + output[MMAL_CAMERA_PORT_CAPTURE];
  17942. + break;
  17943. + case MMAL_COMPONENT_IMAGE_ENCODE:
  17944. + encode_component = dev->component[MMAL_COMPONENT_IMAGE_ENCODE];
  17945. + port = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
  17946. + camera_port =
  17947. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17948. + output[MMAL_CAMERA_PORT_CAPTURE];
  17949. + break;
  17950. + case MMAL_COMPONENT_VIDEO_ENCODE:
  17951. + encode_component = dev->component[MMAL_COMPONENT_VIDEO_ENCODE];
  17952. + port = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  17953. + camera_port =
  17954. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17955. + output[MMAL_CAMERA_PORT_VIDEO];
  17956. + break;
  17957. + default:
  17958. + break;
  17959. + }
  17960. +
  17961. + if (!port)
  17962. + return -EINVAL;
  17963. +
  17964. + if (encode_component)
  17965. + camera_port->format.encoding = MMAL_ENCODING_OPAQUE;
  17966. + else
  17967. + camera_port->format.encoding = mfmt->mmal;
  17968. +
  17969. + camera_port->format.encoding_variant = 0;
  17970. + camera_port->es.video.width = f->fmt.pix.width;
  17971. + camera_port->es.video.height = f->fmt.pix.height;
  17972. + camera_port->es.video.crop.x = 0;
  17973. + camera_port->es.video.crop.y = 0;
  17974. + camera_port->es.video.crop.width = f->fmt.pix.width;
  17975. + camera_port->es.video.crop.height = f->fmt.pix.height;
  17976. + camera_port->es.video.frame_rate.num = 0;
  17977. + camera_port->es.video.frame_rate.den = 1;
  17978. + camera_port->es.video.color_space = MMAL_COLOR_SPACE_JPEG_JFIF;
  17979. +
  17980. + ret = vchiq_mmal_port_set_format(dev->instance, camera_port);
  17981. +
  17982. + if (!ret
  17983. + && camera_port ==
  17984. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17985. + output[MMAL_CAMERA_PORT_VIDEO]) {
  17986. + bool overlay_enabled =
  17987. + !!dev->component[MMAL_COMPONENT_PREVIEW]->enabled;
  17988. + struct vchiq_mmal_port *preview_port =
  17989. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17990. + output[MMAL_CAMERA_PORT_PREVIEW];
  17991. + /* Preview and encode ports need to match on resolution */
  17992. + if (overlay_enabled) {
  17993. + /* Need to disable the overlay before we can update
  17994. + * the resolution
  17995. + */
  17996. + ret =
  17997. + vchiq_mmal_port_disable(dev->instance,
  17998. + preview_port);
  17999. + if (!ret)
  18000. + ret =
  18001. + vchiq_mmal_port_connect_tunnel(
  18002. + dev->instance,
  18003. + preview_port,
  18004. + NULL);
  18005. + }
  18006. + preview_port->es.video.width = f->fmt.pix.width;
  18007. + preview_port->es.video.height = f->fmt.pix.height;
  18008. + preview_port->es.video.crop.x = 0;
  18009. + preview_port->es.video.crop.y = 0;
  18010. + preview_port->es.video.crop.width = f->fmt.pix.width;
  18011. + preview_port->es.video.crop.height = f->fmt.pix.height;
  18012. + preview_port->es.video.frame_rate.num =
  18013. + dev->capture.timeperframe.denominator;
  18014. + preview_port->es.video.frame_rate.den =
  18015. + dev->capture.timeperframe.numerator;
  18016. + ret = vchiq_mmal_port_set_format(dev->instance, preview_port);
  18017. + if (overlay_enabled) {
  18018. + ret = vchiq_mmal_port_connect_tunnel(
  18019. + dev->instance,
  18020. + preview_port,
  18021. + &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
  18022. + if (!ret)
  18023. + ret = vchiq_mmal_port_enable(dev->instance,
  18024. + preview_port,
  18025. + NULL);
  18026. + }
  18027. + }
  18028. +
  18029. + if (ret) {
  18030. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  18031. + "%s failed to set format %dx%d %08X\n", __func__,
  18032. + f->fmt.pix.width, f->fmt.pix.height,
  18033. + f->fmt.pix.pixelformat);
  18034. + /* ensure capture is not going to be tried */
  18035. + dev->capture.port = NULL;
  18036. + } else {
  18037. + if (encode_component) {
  18038. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  18039. + "vid_cap - set up encode comp\n");
  18040. +
  18041. + /* configure buffering */
  18042. + camera_port->current_buffer.size =
  18043. + camera_port->recommended_buffer.size;
  18044. + camera_port->current_buffer.num =
  18045. + camera_port->recommended_buffer.num;
  18046. +
  18047. + ret =
  18048. + vchiq_mmal_port_connect_tunnel(
  18049. + dev->instance,
  18050. + camera_port,
  18051. + &encode_component->input[0]);
  18052. + if (ret) {
  18053. + v4l2_dbg(1, bcm2835_v4l2_debug,
  18054. + &dev->v4l2_dev,
  18055. + "%s failed to create connection\n",
  18056. + __func__);
  18057. + /* ensure capture is not going to be tried */
  18058. + dev->capture.port = NULL;
  18059. + } else {
  18060. + port->es.video.width = f->fmt.pix.width;
  18061. + port->es.video.height = f->fmt.pix.height;
  18062. + port->es.video.crop.x = 0;
  18063. + port->es.video.crop.y = 0;
  18064. + port->es.video.crop.width = f->fmt.pix.width;
  18065. + port->es.video.crop.height = f->fmt.pix.height;
  18066. + port->es.video.frame_rate.num =
  18067. + dev->capture.timeperframe.denominator;
  18068. + port->es.video.frame_rate.den =
  18069. + dev->capture.timeperframe.numerator;
  18070. +
  18071. + port->format.encoding = mfmt->mmal;
  18072. + port->format.encoding_variant = 0;
  18073. + /* Set any encoding specific parameters */
  18074. + switch (mfmt->mmal_component) {
  18075. + case MMAL_COMPONENT_VIDEO_ENCODE:
  18076. + port->format.bitrate =
  18077. + dev->capture.encode_bitrate;
  18078. + break;
  18079. + case MMAL_COMPONENT_IMAGE_ENCODE:
  18080. + /* Could set EXIF parameters here */
  18081. + break;
  18082. + default:
  18083. + break;
  18084. + }
  18085. + ret = vchiq_mmal_port_set_format(dev->instance,
  18086. + port);
  18087. + if (ret)
  18088. + v4l2_dbg(1, bcm2835_v4l2_debug,
  18089. + &dev->v4l2_dev,
  18090. + "%s failed to set format %dx%d fmt %08X\n",
  18091. + __func__,
  18092. + f->fmt.pix.width,
  18093. + f->fmt.pix.height,
  18094. + f->fmt.pix.pixelformat
  18095. + );
  18096. + }
  18097. +
  18098. + if (!ret) {
  18099. + ret = vchiq_mmal_component_enable(
  18100. + dev->instance,
  18101. + encode_component);
  18102. + if (ret) {
  18103. + v4l2_dbg(1, bcm2835_v4l2_debug,
  18104. + &dev->v4l2_dev,
  18105. + "%s Failed to enable encode components\n",
  18106. + __func__);
  18107. + }
  18108. + }
  18109. + if (!ret) {
  18110. + /* configure buffering */
  18111. + port->current_buffer.num = 1;
  18112. + port->current_buffer.size =
  18113. + f->fmt.pix.sizeimage;
  18114. + if (port->format.encoding ==
  18115. + MMAL_ENCODING_JPEG) {
  18116. + v4l2_dbg(1, bcm2835_v4l2_debug,
  18117. + &dev->v4l2_dev,
  18118. + "JPG - buf size now %d was %d\n",
  18119. + f->fmt.pix.sizeimage,
  18120. + port->current_buffer.size);
  18121. + port->current_buffer.size =
  18122. + (f->fmt.pix.sizeimage <
  18123. + (100 << 10))
  18124. + ? (100 << 10) : f->fmt.pix.
  18125. + sizeimage;
  18126. + }
  18127. + v4l2_dbg(1, bcm2835_v4l2_debug,
  18128. + &dev->v4l2_dev,
  18129. + "vid_cap - cur_buf.size set to %d\n",
  18130. + f->fmt.pix.sizeimage);
  18131. + port->current_buffer.alignment = 0;
  18132. + }
  18133. + } else {
  18134. + /* configure buffering */
  18135. + camera_port->current_buffer.num = 1;
  18136. + camera_port->current_buffer.size = f->fmt.pix.sizeimage;
  18137. + camera_port->current_buffer.alignment = 0;
  18138. + }
  18139. +
  18140. + if (!ret) {
  18141. + dev->capture.fmt = mfmt;
  18142. + dev->capture.stride = f->fmt.pix.bytesperline;
  18143. + dev->capture.width = camera_port->es.video.crop.width;
  18144. + dev->capture.height = camera_port->es.video.crop.height;
  18145. + dev->capture.buffersize = port->current_buffer.size;
  18146. +
  18147. + /* select port for capture */
  18148. + dev->capture.port = port;
  18149. + dev->capture.camera_port = camera_port;
  18150. + dev->capture.encode_component = encode_component;
  18151. + v4l2_dbg(1, bcm2835_v4l2_debug,
  18152. + &dev->v4l2_dev,
  18153. + "Set dev->capture.fmt %08X, %dx%d, stride %d, size %d",
  18154. + port->format.encoding,
  18155. + dev->capture.width, dev->capture.height,
  18156. + dev->capture.stride, dev->capture.buffersize);
  18157. + }
  18158. + }
  18159. +
  18160. + /* todo: Need to convert the vchiq/mmal error into a v4l2 error. */
  18161. + return ret;
  18162. +}
  18163. +
  18164. +static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  18165. + struct v4l2_format *f)
  18166. +{
  18167. + int ret;
  18168. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  18169. + struct mmal_fmt *mfmt;
  18170. +
  18171. + /* try the format to set valid parameters */
  18172. + ret = vidioc_try_fmt_vid_cap(file, priv, f);
  18173. + if (ret) {
  18174. + v4l2_err(&dev->v4l2_dev,
  18175. + "vid_cap - vidioc_try_fmt_vid_cap failed\n");
  18176. + return ret;
  18177. + }
  18178. +
  18179. + /* if a capture is running refuse to set format */
  18180. + if (vb2_is_busy(&dev->capture.vb_vidq)) {
  18181. + v4l2_info(&dev->v4l2_dev, "%s device busy\n", __func__);
  18182. + return -EBUSY;
  18183. + }
  18184. +
  18185. + /* If the format is unsupported v4l2 says we should switch to
  18186. + * a supported one and not return an error. */
  18187. + mfmt = get_format(f);
  18188. + if (!mfmt) {
  18189. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  18190. + "Fourcc format (0x%08x) unknown.\n",
  18191. + f->fmt.pix.pixelformat);
  18192. + f->fmt.pix.pixelformat = formats[0].fourcc;
  18193. + mfmt = get_format(f);
  18194. + }
  18195. +
  18196. + ret = mmal_setup_components(dev, f);
  18197. + if (ret != 0) {
  18198. + v4l2_err(&dev->v4l2_dev,
  18199. + "%s: failed to setup mmal components: %d\n",
  18200. + __func__, ret);
  18201. + ret = -EINVAL;
  18202. + }
  18203. +
  18204. + return ret;
  18205. +}
  18206. +
  18207. +int vidioc_enum_framesizes(struct file *file, void *fh,
  18208. + struct v4l2_frmsizeenum *fsize)
  18209. +{
  18210. + static const struct v4l2_frmsize_stepwise sizes = {
  18211. + MIN_WIDTH, MAX_WIDTH, 2,
  18212. + MIN_HEIGHT, MAX_HEIGHT, 2
  18213. + };
  18214. + int i;
  18215. +
  18216. + if (fsize->index)
  18217. + return -EINVAL;
  18218. + for (i = 0; i < ARRAY_SIZE(formats); i++)
  18219. + if (formats[i].fourcc == fsize->pixel_format)
  18220. + break;
  18221. + if (i == ARRAY_SIZE(formats))
  18222. + return -EINVAL;
  18223. + fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  18224. + fsize->stepwise = sizes;
  18225. + return 0;
  18226. +}
  18227. +
  18228. +/* timeperframe is arbitrary and continous */
  18229. +static int vidioc_enum_frameintervals(struct file *file, void *priv,
  18230. + struct v4l2_frmivalenum *fival)
  18231. +{
  18232. + int i;
  18233. +
  18234. + if (fival->index)
  18235. + return -EINVAL;
  18236. +
  18237. + for (i = 0; i < ARRAY_SIZE(formats); i++)
  18238. + if (formats[i].fourcc == fival->pixel_format)
  18239. + break;
  18240. + if (i == ARRAY_SIZE(formats))
  18241. + return -EINVAL;
  18242. +
  18243. + /* regarding width & height - we support any within range */
  18244. + if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH ||
  18245. + fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT)
  18246. + return -EINVAL;
  18247. +
  18248. + fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
  18249. +
  18250. + /* fill in stepwise (step=1.0 is requred by V4L2 spec) */
  18251. + fival->stepwise.min = tpf_min;
  18252. + fival->stepwise.max = tpf_max;
  18253. + fival->stepwise.step = (struct v4l2_fract) {1, 1};
  18254. +
  18255. + return 0;
  18256. +}
  18257. +
  18258. +static int vidioc_g_parm(struct file *file, void *priv,
  18259. + struct v4l2_streamparm *parm)
  18260. +{
  18261. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  18262. +
  18263. + if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  18264. + return -EINVAL;
  18265. +
  18266. + parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  18267. + parm->parm.capture.timeperframe = dev->capture.timeperframe;
  18268. + parm->parm.capture.readbuffers = 1;
  18269. + return 0;
  18270. +}
  18271. +
  18272. +#define FRACT_CMP(a, OP, b) \
  18273. + ((u64)(a).numerator * (b).denominator OP \
  18274. + (u64)(b).numerator * (a).denominator)
  18275. +
  18276. +static int vidioc_s_parm(struct file *file, void *priv,
  18277. + struct v4l2_streamparm *parm)
  18278. +{
  18279. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  18280. + struct v4l2_fract tpf;
  18281. + struct mmal_parameter_rational fps_param;
  18282. +
  18283. + if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  18284. + return -EINVAL;
  18285. +
  18286. + tpf = parm->parm.capture.timeperframe;
  18287. +
  18288. + /* tpf: {*, 0} resets timing; clip to [min, max]*/
  18289. + tpf = tpf.denominator ? tpf : tpf_default;
  18290. + tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
  18291. + tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
  18292. +
  18293. + dev->capture.timeperframe = tpf;
  18294. + parm->parm.capture.timeperframe = tpf;
  18295. + parm->parm.capture.readbuffers = 1;
  18296. +
  18297. + fps_param.num = 0; /* Select variable fps, and then use
  18298. + * FPS_RANGE to select the actual limits.
  18299. + */
  18300. + fps_param.den = 1;
  18301. + set_framerate_params(dev);
  18302. +
  18303. + return 0;
  18304. +}
  18305. +
  18306. +static const struct v4l2_ioctl_ops camera0_ioctl_ops = {
  18307. + /* overlay */
  18308. + .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
  18309. + .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
  18310. + .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
  18311. + .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
  18312. + .vidioc_overlay = vidioc_overlay,
  18313. + .vidioc_g_fbuf = vidioc_g_fbuf,
  18314. +
  18315. + /* inputs */
  18316. + .vidioc_enum_input = vidioc_enum_input,
  18317. + .vidioc_g_input = vidioc_g_input,
  18318. + .vidioc_s_input = vidioc_s_input,
  18319. +
  18320. + /* capture */
  18321. + .vidioc_querycap = vidioc_querycap,
  18322. + .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  18323. + .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  18324. + .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  18325. + .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  18326. +
  18327. + /* buffer management */
  18328. + .vidioc_reqbufs = vb2_ioctl_reqbufs,
  18329. + .vidioc_create_bufs = vb2_ioctl_create_bufs,
  18330. + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  18331. + .vidioc_querybuf = vb2_ioctl_querybuf,
  18332. + .vidioc_qbuf = vb2_ioctl_qbuf,
  18333. + .vidioc_dqbuf = vb2_ioctl_dqbuf,
  18334. + .vidioc_enum_framesizes = vidioc_enum_framesizes,
  18335. + .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  18336. + .vidioc_g_parm = vidioc_g_parm,
  18337. + .vidioc_s_parm = vidioc_s_parm,
  18338. + .vidioc_streamon = vb2_ioctl_streamon,
  18339. + .vidioc_streamoff = vb2_ioctl_streamoff,
  18340. +
  18341. + .vidioc_log_status = v4l2_ctrl_log_status,
  18342. + .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  18343. + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  18344. +};
  18345. +
  18346. +static const struct v4l2_ioctl_ops camera0_ioctl_ops_gstreamer = {
  18347. + /* overlay */
  18348. + .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
  18349. + .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
  18350. + .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
  18351. + .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
  18352. + .vidioc_overlay = vidioc_overlay,
  18353. + .vidioc_g_fbuf = vidioc_g_fbuf,
  18354. +
  18355. + /* inputs */
  18356. + .vidioc_enum_input = vidioc_enum_input,
  18357. + .vidioc_g_input = vidioc_g_input,
  18358. + .vidioc_s_input = vidioc_s_input,
  18359. +
  18360. + /* capture */
  18361. + .vidioc_querycap = vidioc_querycap,
  18362. + .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  18363. + .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  18364. + .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  18365. + .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  18366. +
  18367. + /* buffer management */
  18368. + .vidioc_reqbufs = vb2_ioctl_reqbufs,
  18369. + .vidioc_create_bufs = vb2_ioctl_create_bufs,
  18370. + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  18371. + .vidioc_querybuf = vb2_ioctl_querybuf,
  18372. + .vidioc_qbuf = vb2_ioctl_qbuf,
  18373. + .vidioc_dqbuf = vb2_ioctl_dqbuf,
  18374. + /* Remove this function ptr to fix gstreamer bug
  18375. + .vidioc_enum_framesizes = vidioc_enum_framesizes, */
  18376. + .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  18377. + .vidioc_g_parm = vidioc_g_parm,
  18378. + .vidioc_s_parm = vidioc_s_parm,
  18379. + .vidioc_streamon = vb2_ioctl_streamon,
  18380. + .vidioc_streamoff = vb2_ioctl_streamoff,
  18381. +
  18382. + .vidioc_log_status = v4l2_ctrl_log_status,
  18383. + .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  18384. + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  18385. +};
  18386. +
  18387. +/* ------------------------------------------------------------------
  18388. + Driver init/finalise
  18389. + ------------------------------------------------------------------*/
  18390. +
  18391. +static const struct v4l2_file_operations camera0_fops = {
  18392. + .owner = THIS_MODULE,
  18393. + .open = v4l2_fh_open,
  18394. + .release = vb2_fop_release,
  18395. + .read = vb2_fop_read,
  18396. + .poll = vb2_fop_poll,
  18397. + .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
  18398. + .mmap = vb2_fop_mmap,
  18399. +};
  18400. +
  18401. +static struct video_device vdev_template = {
  18402. + .name = "camera0",
  18403. + .fops = &camera0_fops,
  18404. + .ioctl_ops = &camera0_ioctl_ops,
  18405. + .release = video_device_release_empty,
  18406. +};
  18407. +
  18408. +static int set_camera_parameters(struct vchiq_mmal_instance *instance,
  18409. + struct vchiq_mmal_component *camera)
  18410. +{
  18411. + int ret;
  18412. + struct mmal_parameter_camera_config cam_config = {
  18413. + .max_stills_w = MAX_WIDTH,
  18414. + .max_stills_h = MAX_HEIGHT,
  18415. + .stills_yuv422 = 1,
  18416. + .one_shot_stills = 1,
  18417. + .max_preview_video_w = (max_video_width > 1920) ?
  18418. + max_video_width : 1920,
  18419. + .max_preview_video_h = (max_video_height > 1088) ?
  18420. + max_video_height : 1088,
  18421. + .num_preview_video_frames = 3,
  18422. + .stills_capture_circular_buffer_height = 0,
  18423. + .fast_preview_resume = 0,
  18424. + .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
  18425. + };
  18426. +
  18427. + ret = vchiq_mmal_port_parameter_set(instance, &camera->control,
  18428. + MMAL_PARAMETER_CAMERA_CONFIG,
  18429. + &cam_config, sizeof(cam_config));
  18430. + return ret;
  18431. +}
  18432. +
  18433. +/* MMAL instance and component init */
  18434. +static int __init mmal_init(struct bm2835_mmal_dev *dev)
  18435. +{
  18436. + int ret;
  18437. + struct mmal_es_format *format;
  18438. + u32 bool_true = 1;
  18439. +
  18440. + ret = vchiq_mmal_init(&dev->instance);
  18441. + if (ret < 0)
  18442. + return ret;
  18443. +
  18444. + /* get the camera component ready */
  18445. + ret = vchiq_mmal_component_init(dev->instance, "ril.camera",
  18446. + &dev->component[MMAL_COMPONENT_CAMERA]);
  18447. + if (ret < 0)
  18448. + goto unreg_mmal;
  18449. +
  18450. + if (dev->component[MMAL_COMPONENT_CAMERA]->outputs <
  18451. + MMAL_CAMERA_PORT_COUNT) {
  18452. + ret = -EINVAL;
  18453. + goto unreg_camera;
  18454. + }
  18455. +
  18456. + ret = set_camera_parameters(dev->instance,
  18457. + dev->component[MMAL_COMPONENT_CAMERA]);
  18458. + if (ret < 0)
  18459. + goto unreg_camera;
  18460. +
  18461. + format =
  18462. + &dev->component[MMAL_COMPONENT_CAMERA]->
  18463. + output[MMAL_CAMERA_PORT_PREVIEW].format;
  18464. +
  18465. + format->encoding = MMAL_ENCODING_OPAQUE;
  18466. + format->encoding_variant = MMAL_ENCODING_I420;
  18467. +
  18468. + format->es->video.width = 1024;
  18469. + format->es->video.height = 768;
  18470. + format->es->video.crop.x = 0;
  18471. + format->es->video.crop.y = 0;
  18472. + format->es->video.crop.width = 1024;
  18473. + format->es->video.crop.height = 768;
  18474. + format->es->video.frame_rate.num = 0; /* Rely on fps_range */
  18475. + format->es->video.frame_rate.den = 1;
  18476. +
  18477. + format =
  18478. + &dev->component[MMAL_COMPONENT_CAMERA]->
  18479. + output[MMAL_CAMERA_PORT_VIDEO].format;
  18480. +
  18481. + format->encoding = MMAL_ENCODING_OPAQUE;
  18482. + format->encoding_variant = MMAL_ENCODING_I420;
  18483. +
  18484. + format->es->video.width = 1024;
  18485. + format->es->video.height = 768;
  18486. + format->es->video.crop.x = 0;
  18487. + format->es->video.crop.y = 0;
  18488. + format->es->video.crop.width = 1024;
  18489. + format->es->video.crop.height = 768;
  18490. + format->es->video.frame_rate.num = 0; /* Rely on fps_range */
  18491. + format->es->video.frame_rate.den = 1;
  18492. +
  18493. + vchiq_mmal_port_parameter_set(dev->instance,
  18494. + &dev->component[MMAL_COMPONENT_CAMERA]->
  18495. + output[MMAL_CAMERA_PORT_VIDEO],
  18496. + MMAL_PARAMETER_NO_IMAGE_PADDING,
  18497. + &bool_true, sizeof(bool_true));
  18498. +
  18499. + format =
  18500. + &dev->component[MMAL_COMPONENT_CAMERA]->
  18501. + output[MMAL_CAMERA_PORT_CAPTURE].format;
  18502. +
  18503. + format->encoding = MMAL_ENCODING_OPAQUE;
  18504. +
  18505. + format->es->video.width = 2592;
  18506. + format->es->video.height = 1944;
  18507. + format->es->video.crop.x = 0;
  18508. + format->es->video.crop.y = 0;
  18509. + format->es->video.crop.width = 2592;
  18510. + format->es->video.crop.height = 1944;
  18511. + format->es->video.frame_rate.num = 0; /* Rely on fps_range */
  18512. + format->es->video.frame_rate.den = 1;
  18513. +
  18514. + dev->capture.width = format->es->video.width;
  18515. + dev->capture.height = format->es->video.height;
  18516. + dev->capture.fmt = &formats[0];
  18517. + dev->capture.encode_component = NULL;
  18518. + dev->capture.timeperframe = tpf_default;
  18519. + dev->capture.enc_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
  18520. + dev->capture.enc_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
  18521. +
  18522. + vchiq_mmal_port_parameter_set(dev->instance,
  18523. + &dev->component[MMAL_COMPONENT_CAMERA]->
  18524. + output[MMAL_CAMERA_PORT_CAPTURE],
  18525. + MMAL_PARAMETER_NO_IMAGE_PADDING,
  18526. + &bool_true, sizeof(bool_true));
  18527. +
  18528. + /* get the preview component ready */
  18529. + ret = vchiq_mmal_component_init(
  18530. + dev->instance, "ril.video_render",
  18531. + &dev->component[MMAL_COMPONENT_PREVIEW]);
  18532. + if (ret < 0)
  18533. + goto unreg_camera;
  18534. +
  18535. + if (dev->component[MMAL_COMPONENT_PREVIEW]->inputs < 1) {
  18536. + ret = -EINVAL;
  18537. + pr_debug("too few input ports %d needed %d\n",
  18538. + dev->component[MMAL_COMPONENT_PREVIEW]->inputs, 1);
  18539. + goto unreg_preview;
  18540. + }
  18541. +
  18542. + /* get the image encoder component ready */
  18543. + ret = vchiq_mmal_component_init(
  18544. + dev->instance, "ril.image_encode",
  18545. + &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
  18546. + if (ret < 0)
  18547. + goto unreg_preview;
  18548. +
  18549. + if (dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs < 1) {
  18550. + ret = -EINVAL;
  18551. + v4l2_err(&dev->v4l2_dev, "too few input ports %d needed %d\n",
  18552. + dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs,
  18553. + 1);
  18554. + goto unreg_image_encoder;
  18555. + }
  18556. +
  18557. + /* get the video encoder component ready */
  18558. + ret = vchiq_mmal_component_init(dev->instance, "ril.video_encode",
  18559. + &dev->
  18560. + component[MMAL_COMPONENT_VIDEO_ENCODE]);
  18561. + if (ret < 0)
  18562. + goto unreg_image_encoder;
  18563. +
  18564. + if (dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs < 1) {
  18565. + ret = -EINVAL;
  18566. + v4l2_err(&dev->v4l2_dev, "too few input ports %d needed %d\n",
  18567. + dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs,
  18568. + 1);
  18569. + goto unreg_vid_encoder;
  18570. + }
  18571. +
  18572. + {
  18573. + struct vchiq_mmal_port *encoder_port =
  18574. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  18575. + encoder_port->format.encoding = MMAL_ENCODING_H264;
  18576. + ret = vchiq_mmal_port_set_format(dev->instance,
  18577. + encoder_port);
  18578. + }
  18579. +
  18580. + {
  18581. + unsigned int enable = 1;
  18582. + vchiq_mmal_port_parameter_set(
  18583. + dev->instance,
  18584. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
  18585. + MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
  18586. + &enable, sizeof(enable));
  18587. +
  18588. + vchiq_mmal_port_parameter_set(dev->instance,
  18589. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
  18590. + MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
  18591. + &enable,
  18592. + sizeof(enable));
  18593. + }
  18594. + ret = bm2835_mmal_set_all_camera_controls(dev);
  18595. + if (ret < 0)
  18596. + goto unreg_vid_encoder;
  18597. +
  18598. + return 0;
  18599. +
  18600. +unreg_vid_encoder:
  18601. + pr_err("Cleanup: Destroy video encoder\n");
  18602. + vchiq_mmal_component_finalise(
  18603. + dev->instance,
  18604. + dev->component[MMAL_COMPONENT_VIDEO_ENCODE]);
  18605. +
  18606. +unreg_image_encoder:
  18607. + pr_err("Cleanup: Destroy image encoder\n");
  18608. + vchiq_mmal_component_finalise(
  18609. + dev->instance,
  18610. + dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
  18611. +
  18612. +unreg_preview:
  18613. + pr_err("Cleanup: Destroy video render\n");
  18614. + vchiq_mmal_component_finalise(dev->instance,
  18615. + dev->component[MMAL_COMPONENT_PREVIEW]);
  18616. +
  18617. +unreg_camera:
  18618. + pr_err("Cleanup: Destroy camera\n");
  18619. + vchiq_mmal_component_finalise(dev->instance,
  18620. + dev->component[MMAL_COMPONENT_CAMERA]);
  18621. +
  18622. +unreg_mmal:
  18623. + vchiq_mmal_finalise(dev->instance);
  18624. + return ret;
  18625. +}
  18626. +
  18627. +static int __init bm2835_mmal_init_device(struct bm2835_mmal_dev *dev,
  18628. + struct video_device *vfd)
  18629. +{
  18630. + int ret;
  18631. +
  18632. + *vfd = vdev_template;
  18633. + if (gst_v4l2src_is_broken) {
  18634. + v4l2_info(&dev->v4l2_dev,
  18635. + "Work-around for gstreamer issue is active.\n");
  18636. + vfd->ioctl_ops = &camera0_ioctl_ops_gstreamer;
  18637. + }
  18638. +
  18639. + vfd->v4l2_dev = &dev->v4l2_dev;
  18640. +
  18641. + vfd->lock = &dev->mutex;
  18642. +
  18643. + vfd->queue = &dev->capture.vb_vidq;
  18644. +
  18645. + set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  18646. +
  18647. + /* video device needs to be able to access instance data */
  18648. + video_set_drvdata(vfd, dev);
  18649. +
  18650. + ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
  18651. + if (ret < 0)
  18652. + return ret;
  18653. +
  18654. + v4l2_info(vfd->v4l2_dev,
  18655. + "V4L2 device registered as %s - stills mode > %dx%d\n",
  18656. + video_device_node_name(vfd), max_video_width, max_video_height);
  18657. +
  18658. + return 0;
  18659. +}
  18660. +
  18661. +static struct v4l2_format default_v4l2_format = {
  18662. + .fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG,
  18663. + .fmt.pix.width = 1024,
  18664. + .fmt.pix.bytesperline = 1024,
  18665. + .fmt.pix.height = 768,
  18666. + .fmt.pix.sizeimage = 1024*768,
  18667. +};
  18668. +
  18669. +static int __init bm2835_mmal_init(void)
  18670. +{
  18671. + int ret;
  18672. + struct bm2835_mmal_dev *dev;
  18673. + struct vb2_queue *q;
  18674. +
  18675. + dev = kzalloc(sizeof(*gdev), GFP_KERNEL);
  18676. + if (!dev)
  18677. + return -ENOMEM;
  18678. +
  18679. + /* setup device defaults */
  18680. + dev->overlay.w.left = 150;
  18681. + dev->overlay.w.top = 50;
  18682. + dev->overlay.w.width = 1024;
  18683. + dev->overlay.w.height = 768;
  18684. + dev->overlay.clipcount = 0;
  18685. + dev->overlay.field = V4L2_FIELD_NONE;
  18686. +
  18687. + dev->capture.fmt = &formats[3]; /* JPEG */
  18688. +
  18689. + /* v4l device registration */
  18690. + snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
  18691. + "%s", BM2835_MMAL_MODULE_NAME);
  18692. + ret = v4l2_device_register(NULL, &dev->v4l2_dev);
  18693. + if (ret)
  18694. + goto free_dev;
  18695. +
  18696. + /* setup v4l controls */
  18697. + ret = bm2835_mmal_init_controls(dev, &dev->ctrl_handler);
  18698. + if (ret < 0)
  18699. + goto unreg_dev;
  18700. + dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
  18701. +
  18702. + /* mmal init */
  18703. + ret = mmal_init(dev);
  18704. + if (ret < 0)
  18705. + goto unreg_dev;
  18706. +
  18707. + /* initialize queue */
  18708. + q = &dev->capture.vb_vidq;
  18709. + memset(q, 0, sizeof(*q));
  18710. + q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  18711. + q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  18712. + q->drv_priv = dev;
  18713. + q->buf_struct_size = sizeof(struct mmal_buffer);
  18714. + q->ops = &bm2835_mmal_video_qops;
  18715. + q->mem_ops = &vb2_vmalloc_memops;
  18716. + q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  18717. + ret = vb2_queue_init(q);
  18718. + if (ret < 0)
  18719. + goto unreg_dev;
  18720. +
  18721. + /* v4l2 core mutex used to protect all fops and v4l2 ioctls. */
  18722. + mutex_init(&dev->mutex);
  18723. +
  18724. + /* initialise video devices */
  18725. + ret = bm2835_mmal_init_device(dev, &dev->vdev);
  18726. + if (ret < 0)
  18727. + goto unreg_dev;
  18728. +
  18729. + /* Really want to call vidioc_s_fmt_vid_cap with the default
  18730. + * format, but currently the APIs don't join up.
  18731. + */
  18732. + ret = mmal_setup_components(dev, &default_v4l2_format);
  18733. + if (ret < 0) {
  18734. + v4l2_err(&dev->v4l2_dev,
  18735. + "%s: could not setup components\n", __func__);
  18736. + goto unreg_dev;
  18737. + }
  18738. +
  18739. + v4l2_info(&dev->v4l2_dev,
  18740. + "Broadcom 2835 MMAL video capture ver %s loaded.\n",
  18741. + BM2835_MMAL_VERSION);
  18742. +
  18743. + gdev = dev;
  18744. + return 0;
  18745. +
  18746. +unreg_dev:
  18747. + v4l2_ctrl_handler_free(&dev->ctrl_handler);
  18748. + v4l2_device_unregister(&dev->v4l2_dev);
  18749. +
  18750. +free_dev:
  18751. + kfree(dev);
  18752. +
  18753. + v4l2_err(&dev->v4l2_dev,
  18754. + "%s: error %d while loading driver\n",
  18755. + BM2835_MMAL_MODULE_NAME, ret);
  18756. +
  18757. + return ret;
  18758. +}
  18759. +
  18760. +static void __exit bm2835_mmal_exit(void)
  18761. +{
  18762. + if (!gdev)
  18763. + return;
  18764. +
  18765. + v4l2_info(&gdev->v4l2_dev, "unregistering %s\n",
  18766. + video_device_node_name(&gdev->vdev));
  18767. +
  18768. + video_unregister_device(&gdev->vdev);
  18769. +
  18770. + if (gdev->capture.encode_component) {
  18771. + v4l2_dbg(1, bcm2835_v4l2_debug, &gdev->v4l2_dev,
  18772. + "mmal_exit - disconnect tunnel\n");
  18773. + vchiq_mmal_port_connect_tunnel(gdev->instance,
  18774. + gdev->capture.camera_port, NULL);
  18775. + vchiq_mmal_component_disable(gdev->instance,
  18776. + gdev->capture.encode_component);
  18777. + }
  18778. + vchiq_mmal_component_disable(gdev->instance,
  18779. + gdev->component[MMAL_COMPONENT_CAMERA]);
  18780. +
  18781. + vchiq_mmal_component_finalise(gdev->instance,
  18782. + gdev->
  18783. + component[MMAL_COMPONENT_VIDEO_ENCODE]);
  18784. +
  18785. + vchiq_mmal_component_finalise(gdev->instance,
  18786. + gdev->
  18787. + component[MMAL_COMPONENT_IMAGE_ENCODE]);
  18788. +
  18789. + vchiq_mmal_component_finalise(gdev->instance,
  18790. + gdev->component[MMAL_COMPONENT_PREVIEW]);
  18791. +
  18792. + vchiq_mmal_component_finalise(gdev->instance,
  18793. + gdev->component[MMAL_COMPONENT_CAMERA]);
  18794. +
  18795. + vchiq_mmal_finalise(gdev->instance);
  18796. +
  18797. + v4l2_ctrl_handler_free(&gdev->ctrl_handler);
  18798. +
  18799. + v4l2_device_unregister(&gdev->v4l2_dev);
  18800. +
  18801. + kfree(gdev);
  18802. +}
  18803. +
  18804. +module_init(bm2835_mmal_init);
  18805. +module_exit(bm2835_mmal_exit);
  18806. diff -Nur linux-3.12.33/drivers/media/platform/bcm2835/bcm2835-camera.h linux-3.12.33-rpi/drivers/media/platform/bcm2835/bcm2835-camera.h
  18807. --- linux-3.12.33/drivers/media/platform/bcm2835/bcm2835-camera.h 1969-12-31 18:00:00.000000000 -0600
  18808. +++ linux-3.12.33-rpi/drivers/media/platform/bcm2835/bcm2835-camera.h 2014-12-03 19:13:38.012418001 -0600
  18809. @@ -0,0 +1,126 @@
  18810. +/*
  18811. + * Broadcom BM2835 V4L2 driver
  18812. + *
  18813. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  18814. + *
  18815. + * This file is subject to the terms and conditions of the GNU General Public
  18816. + * License. See the file COPYING in the main directory of this archive
  18817. + * for more details.
  18818. + *
  18819. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  18820. + * Dave Stevenson <dsteve@broadcom.com>
  18821. + * Simon Mellor <simellor@broadcom.com>
  18822. + * Luke Diamand <luked@broadcom.com>
  18823. + *
  18824. + * core driver device
  18825. + */
  18826. +
  18827. +#define V4L2_CTRL_COUNT 28 /* number of v4l controls */
  18828. +
  18829. +enum {
  18830. + MMAL_COMPONENT_CAMERA = 0,
  18831. + MMAL_COMPONENT_PREVIEW,
  18832. + MMAL_COMPONENT_IMAGE_ENCODE,
  18833. + MMAL_COMPONENT_VIDEO_ENCODE,
  18834. + MMAL_COMPONENT_COUNT
  18835. +};
  18836. +
  18837. +enum {
  18838. + MMAL_CAMERA_PORT_PREVIEW = 0,
  18839. + MMAL_CAMERA_PORT_VIDEO,
  18840. + MMAL_CAMERA_PORT_CAPTURE,
  18841. + MMAL_CAMERA_PORT_COUNT
  18842. +};
  18843. +
  18844. +#define PREVIEW_LAYER 2
  18845. +
  18846. +extern int bcm2835_v4l2_debug;
  18847. +
  18848. +struct bm2835_mmal_dev {
  18849. + /* v4l2 devices */
  18850. + struct v4l2_device v4l2_dev;
  18851. + struct video_device vdev;
  18852. + struct mutex mutex;
  18853. +
  18854. + /* controls */
  18855. + struct v4l2_ctrl_handler ctrl_handler;
  18856. + struct v4l2_ctrl *ctrls[V4L2_CTRL_COUNT];
  18857. + enum v4l2_scene_mode scene_mode;
  18858. + struct mmal_colourfx colourfx;
  18859. + int hflip;
  18860. + int vflip;
  18861. + int red_gain;
  18862. + int blue_gain;
  18863. + enum mmal_parameter_exposuremode exposure_mode_user;
  18864. + enum v4l2_exposure_auto_type exposure_mode_v4l2_user;
  18865. + /* active exposure mode may differ if selected via a scene mode */
  18866. + enum mmal_parameter_exposuremode exposure_mode_active;
  18867. + enum mmal_parameter_exposuremeteringmode metering_mode;
  18868. + unsigned int manual_shutter_speed;
  18869. + bool exp_auto_priority;
  18870. +
  18871. + /* allocated mmal instance and components */
  18872. + struct vchiq_mmal_instance *instance;
  18873. + struct vchiq_mmal_component *component[MMAL_COMPONENT_COUNT];
  18874. + int camera_use_count;
  18875. +
  18876. + struct v4l2_window overlay;
  18877. +
  18878. + struct {
  18879. + unsigned int width; /* width */
  18880. + unsigned int height; /* height */
  18881. + unsigned int stride; /* stride */
  18882. + unsigned int buffersize; /* buffer size with padding */
  18883. + struct mmal_fmt *fmt;
  18884. + struct v4l2_fract timeperframe;
  18885. +
  18886. + /* H264 encode bitrate */
  18887. + int encode_bitrate;
  18888. + /* H264 bitrate mode. CBR/VBR */
  18889. + int encode_bitrate_mode;
  18890. + /* H264 profile */
  18891. + enum v4l2_mpeg_video_h264_profile enc_profile;
  18892. + /* H264 level */
  18893. + enum v4l2_mpeg_video_h264_level enc_level;
  18894. + /* JPEG Q-factor */
  18895. + int q_factor;
  18896. +
  18897. + struct vb2_queue vb_vidq;
  18898. +
  18899. + /* VC start timestamp for streaming */
  18900. + s64 vc_start_timestamp;
  18901. + /* Kernel start timestamp for streaming */
  18902. + struct timeval kernel_start_ts;
  18903. +
  18904. + struct vchiq_mmal_port *port; /* port being used for capture */
  18905. + /* camera port being used for capture */
  18906. + struct vchiq_mmal_port *camera_port;
  18907. + /* component being used for encode */
  18908. + struct vchiq_mmal_component *encode_component;
  18909. + /* number of frames remaining which driver should capture */
  18910. + unsigned int frame_count;
  18911. + /* last frame completion */
  18912. + struct completion frame_cmplt;
  18913. +
  18914. + } capture;
  18915. +
  18916. +};
  18917. +
  18918. +int bm2835_mmal_init_controls(
  18919. + struct bm2835_mmal_dev *dev,
  18920. + struct v4l2_ctrl_handler *hdl);
  18921. +
  18922. +int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev);
  18923. +int set_framerate_params(struct bm2835_mmal_dev *dev);
  18924. +
  18925. +/* Debug helpers */
  18926. +
  18927. +#define v4l2_dump_pix_format(level, debug, dev, pix_fmt, desc) \
  18928. +{ \
  18929. + v4l2_dbg(level, debug, dev, \
  18930. +"%s: w %u h %u field %u pfmt 0x%x bpl %u sz_img %u colorspace 0x%x priv %u\n", \
  18931. + desc == NULL ? "" : desc, \
  18932. + (pix_fmt)->width, (pix_fmt)->height, (pix_fmt)->field, \
  18933. + (pix_fmt)->pixelformat, (pix_fmt)->bytesperline, \
  18934. + (pix_fmt)->sizeimage, (pix_fmt)->colorspace, (pix_fmt)->priv); \
  18935. +}
  18936. diff -Nur linux-3.12.33/drivers/media/platform/bcm2835/controls.c linux-3.12.33-rpi/drivers/media/platform/bcm2835/controls.c
  18937. --- linux-3.12.33/drivers/media/platform/bcm2835/controls.c 1969-12-31 18:00:00.000000000 -0600
  18938. +++ linux-3.12.33-rpi/drivers/media/platform/bcm2835/controls.c 2014-12-03 19:13:38.012418001 -0600
  18939. @@ -0,0 +1,1322 @@
  18940. +/*
  18941. + * Broadcom BM2835 V4L2 driver
  18942. + *
  18943. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  18944. + *
  18945. + * This file is subject to the terms and conditions of the GNU General Public
  18946. + * License. See the file COPYING in the main directory of this archive
  18947. + * for more details.
  18948. + *
  18949. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  18950. + * Dave Stevenson <dsteve@broadcom.com>
  18951. + * Simon Mellor <simellor@broadcom.com>
  18952. + * Luke Diamand <luked@broadcom.com>
  18953. + */
  18954. +
  18955. +#include <linux/errno.h>
  18956. +#include <linux/kernel.h>
  18957. +#include <linux/module.h>
  18958. +#include <linux/slab.h>
  18959. +#include <media/videobuf2-vmalloc.h>
  18960. +#include <media/v4l2-device.h>
  18961. +#include <media/v4l2-ioctl.h>
  18962. +#include <media/v4l2-ctrls.h>
  18963. +#include <media/v4l2-fh.h>
  18964. +#include <media/v4l2-event.h>
  18965. +#include <media/v4l2-common.h>
  18966. +
  18967. +#include "mmal-common.h"
  18968. +#include "mmal-vchiq.h"
  18969. +#include "mmal-parameters.h"
  18970. +#include "bcm2835-camera.h"
  18971. +
  18972. +/* The supported V4L2_CID_AUTO_EXPOSURE_BIAS values are from -4.0 to +4.0.
  18973. + * MMAL values are in 1/6th increments so the MMAL range is -24 to +24.
  18974. + * V4L2 docs say value "is expressed in terms of EV, drivers should interpret
  18975. + * the values as 0.001 EV units, where the value 1000 stands for +1 EV."
  18976. + * V4L2 is limited to a max of 32 values in a menu, so count in 1/3rds from
  18977. + * -4 to +4
  18978. + */
  18979. +static const s64 ev_bias_qmenu[] = {
  18980. + -4000, -3667, -3333,
  18981. + -3000, -2667, -2333,
  18982. + -2000, -1667, -1333,
  18983. + -1000, -667, -333,
  18984. + 0, 333, 667,
  18985. + 1000, 1333, 1667,
  18986. + 2000, 2333, 2667,
  18987. + 3000, 3333, 3667,
  18988. + 4000
  18989. +};
  18990. +
  18991. +/* Supported ISO values
  18992. + * ISOO = auto ISO
  18993. + */
  18994. +static const s64 iso_qmenu[] = {
  18995. + 0, 100, 200, 400, 800,
  18996. +};
  18997. +
  18998. +static const s64 mains_freq_qmenu[] = {
  18999. + V4L2_CID_POWER_LINE_FREQUENCY_DISABLED,
  19000. + V4L2_CID_POWER_LINE_FREQUENCY_50HZ,
  19001. + V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
  19002. + V4L2_CID_POWER_LINE_FREQUENCY_AUTO
  19003. +};
  19004. +
  19005. +/* Supported video encode modes */
  19006. +static const s64 bitrate_mode_qmenu[] = {
  19007. + (s64)V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
  19008. + (s64)V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
  19009. +};
  19010. +
  19011. +enum bm2835_mmal_ctrl_type {
  19012. + MMAL_CONTROL_TYPE_STD,
  19013. + MMAL_CONTROL_TYPE_STD_MENU,
  19014. + MMAL_CONTROL_TYPE_INT_MENU,
  19015. + MMAL_CONTROL_TYPE_CLUSTER, /* special cluster entry */
  19016. +};
  19017. +
  19018. +struct bm2835_mmal_v4l2_ctrl;
  19019. +
  19020. +typedef int(bm2835_mmal_v4l2_ctrl_cb)(
  19021. + struct bm2835_mmal_dev *dev,
  19022. + struct v4l2_ctrl *ctrl,
  19023. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl);
  19024. +
  19025. +struct bm2835_mmal_v4l2_ctrl {
  19026. + u32 id; /* v4l2 control identifier */
  19027. + enum bm2835_mmal_ctrl_type type;
  19028. + /* control minimum value or
  19029. + * mask for MMAL_CONTROL_TYPE_STD_MENU */
  19030. + s32 min;
  19031. + s32 max; /* maximum value of control */
  19032. + s32 def; /* default value of control */
  19033. + s32 step; /* step size of the control */
  19034. + const s64 *imenu; /* integer menu array */
  19035. + u32 mmal_id; /* mmal parameter id */
  19036. + bm2835_mmal_v4l2_ctrl_cb *setter;
  19037. + bool ignore_errors;
  19038. +};
  19039. +
  19040. +struct v4l2_to_mmal_effects_setting {
  19041. + u32 v4l2_effect;
  19042. + u32 mmal_effect;
  19043. + s32 col_fx_enable;
  19044. + s32 col_fx_fixed_cbcr;
  19045. + u32 u;
  19046. + u32 v;
  19047. + u32 num_effect_params;
  19048. + u32 effect_params[MMAL_MAX_IMAGEFX_PARAMETERS];
  19049. +};
  19050. +
  19051. +static const struct v4l2_to_mmal_effects_setting
  19052. + v4l2_to_mmal_effects_values[] = {
  19053. + { V4L2_COLORFX_NONE, MMAL_PARAM_IMAGEFX_NONE,
  19054. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  19055. + { V4L2_COLORFX_BW, MMAL_PARAM_IMAGEFX_NONE,
  19056. + 1, 0, 128, 128, 0, {0, 0, 0, 0, 0} },
  19057. + { V4L2_COLORFX_SEPIA, MMAL_PARAM_IMAGEFX_NONE,
  19058. + 1, 0, 87, 151, 0, {0, 0, 0, 0, 0} },
  19059. + { V4L2_COLORFX_NEGATIVE, MMAL_PARAM_IMAGEFX_NEGATIVE,
  19060. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  19061. + { V4L2_COLORFX_EMBOSS, MMAL_PARAM_IMAGEFX_EMBOSS,
  19062. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  19063. + { V4L2_COLORFX_SKETCH, MMAL_PARAM_IMAGEFX_SKETCH,
  19064. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  19065. + { V4L2_COLORFX_SKY_BLUE, MMAL_PARAM_IMAGEFX_PASTEL,
  19066. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  19067. + { V4L2_COLORFX_GRASS_GREEN, MMAL_PARAM_IMAGEFX_WATERCOLOUR,
  19068. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  19069. + { V4L2_COLORFX_SKIN_WHITEN, MMAL_PARAM_IMAGEFX_WASHEDOUT,
  19070. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  19071. + { V4L2_COLORFX_VIVID, MMAL_PARAM_IMAGEFX_SATURATION,
  19072. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  19073. + { V4L2_COLORFX_AQUA, MMAL_PARAM_IMAGEFX_NONE,
  19074. + 1, 0, 171, 121, 0, {0, 0, 0, 0, 0} },
  19075. + { V4L2_COLORFX_ART_FREEZE, MMAL_PARAM_IMAGEFX_HATCH,
  19076. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  19077. + { V4L2_COLORFX_SILHOUETTE, MMAL_PARAM_IMAGEFX_FILM,
  19078. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  19079. + { V4L2_COLORFX_SOLARIZATION, MMAL_PARAM_IMAGEFX_SOLARIZE,
  19080. + 0, 0, 0, 0, 5, {1, 128, 160, 160, 48} },
  19081. + { V4L2_COLORFX_ANTIQUE, MMAL_PARAM_IMAGEFX_COLOURBALANCE,
  19082. + 0, 0, 0, 0, 3, {108, 274, 238, 0, 0} },
  19083. + { V4L2_COLORFX_SET_CBCR, MMAL_PARAM_IMAGEFX_NONE,
  19084. + 1, 1, 0, 0, 0, {0, 0, 0, 0, 0} }
  19085. +};
  19086. +
  19087. +struct v4l2_mmal_scene_config {
  19088. + enum v4l2_scene_mode v4l2_scene;
  19089. + enum mmal_parameter_exposuremode exposure_mode;
  19090. + enum mmal_parameter_exposuremeteringmode metering_mode;
  19091. +};
  19092. +
  19093. +static const struct v4l2_mmal_scene_config scene_configs[] = {
  19094. + /* V4L2_SCENE_MODE_NONE automatically added */
  19095. + {
  19096. + V4L2_SCENE_MODE_NIGHT,
  19097. + MMAL_PARAM_EXPOSUREMODE_NIGHT,
  19098. + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE
  19099. + },
  19100. + {
  19101. + V4L2_SCENE_MODE_SPORTS,
  19102. + MMAL_PARAM_EXPOSUREMODE_SPORTS,
  19103. + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE
  19104. + },
  19105. +};
  19106. +
  19107. +/* control handlers*/
  19108. +
  19109. +static int ctrl_set_rational(struct bm2835_mmal_dev *dev,
  19110. + struct v4l2_ctrl *ctrl,
  19111. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19112. +{
  19113. + struct mmal_parameter_rational rational_value;
  19114. + struct vchiq_mmal_port *control;
  19115. +
  19116. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  19117. +
  19118. + rational_value.num = ctrl->val;
  19119. + rational_value.den = 100;
  19120. +
  19121. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  19122. + mmal_ctrl->mmal_id,
  19123. + &rational_value,
  19124. + sizeof(rational_value));
  19125. +}
  19126. +
  19127. +static int ctrl_set_value(struct bm2835_mmal_dev *dev,
  19128. + struct v4l2_ctrl *ctrl,
  19129. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19130. +{
  19131. + u32 u32_value;
  19132. + struct vchiq_mmal_port *control;
  19133. +
  19134. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  19135. +
  19136. + u32_value = ctrl->val;
  19137. +
  19138. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  19139. + mmal_ctrl->mmal_id,
  19140. + &u32_value, sizeof(u32_value));
  19141. +}
  19142. +
  19143. +static int ctrl_set_value_menu(struct bm2835_mmal_dev *dev,
  19144. + struct v4l2_ctrl *ctrl,
  19145. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19146. +{
  19147. + u32 u32_value;
  19148. + struct vchiq_mmal_port *control;
  19149. +
  19150. + if (ctrl->val > mmal_ctrl->max || ctrl->val < mmal_ctrl->min)
  19151. + return 1;
  19152. +
  19153. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  19154. +
  19155. + u32_value = mmal_ctrl->imenu[ctrl->val];
  19156. +
  19157. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  19158. + mmal_ctrl->mmal_id,
  19159. + &u32_value, sizeof(u32_value));
  19160. +}
  19161. +
  19162. +static int ctrl_set_value_ev(struct bm2835_mmal_dev *dev,
  19163. + struct v4l2_ctrl *ctrl,
  19164. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19165. +{
  19166. + s32 s32_value;
  19167. + struct vchiq_mmal_port *control;
  19168. +
  19169. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  19170. +
  19171. + s32_value = (ctrl->val-12)*2; /* Convert from index to 1/6ths */
  19172. +
  19173. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  19174. + mmal_ctrl->mmal_id,
  19175. + &s32_value, sizeof(s32_value));
  19176. +}
  19177. +
  19178. +static int ctrl_set_rotate(struct bm2835_mmal_dev *dev,
  19179. + struct v4l2_ctrl *ctrl,
  19180. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19181. +{
  19182. + int ret;
  19183. + u32 u32_value;
  19184. + struct vchiq_mmal_component *camera;
  19185. +
  19186. + camera = dev->component[MMAL_COMPONENT_CAMERA];
  19187. +
  19188. + u32_value = ((ctrl->val % 360) / 90) * 90;
  19189. +
  19190. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[0],
  19191. + mmal_ctrl->mmal_id,
  19192. + &u32_value, sizeof(u32_value));
  19193. + if (ret < 0)
  19194. + return ret;
  19195. +
  19196. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[1],
  19197. + mmal_ctrl->mmal_id,
  19198. + &u32_value, sizeof(u32_value));
  19199. + if (ret < 0)
  19200. + return ret;
  19201. +
  19202. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[2],
  19203. + mmal_ctrl->mmal_id,
  19204. + &u32_value, sizeof(u32_value));
  19205. +
  19206. + return ret;
  19207. +}
  19208. +
  19209. +static int ctrl_set_flip(struct bm2835_mmal_dev *dev,
  19210. + struct v4l2_ctrl *ctrl,
  19211. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19212. +{
  19213. + int ret;
  19214. + u32 u32_value;
  19215. + struct vchiq_mmal_component *camera;
  19216. +
  19217. + if (ctrl->id == V4L2_CID_HFLIP)
  19218. + dev->hflip = ctrl->val;
  19219. + else
  19220. + dev->vflip = ctrl->val;
  19221. +
  19222. + camera = dev->component[MMAL_COMPONENT_CAMERA];
  19223. +
  19224. + if (dev->hflip && dev->vflip)
  19225. + u32_value = MMAL_PARAM_MIRROR_BOTH;
  19226. + else if (dev->hflip)
  19227. + u32_value = MMAL_PARAM_MIRROR_HORIZONTAL;
  19228. + else if (dev->vflip)
  19229. + u32_value = MMAL_PARAM_MIRROR_VERTICAL;
  19230. + else
  19231. + u32_value = MMAL_PARAM_MIRROR_NONE;
  19232. +
  19233. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[0],
  19234. + mmal_ctrl->mmal_id,
  19235. + &u32_value, sizeof(u32_value));
  19236. + if (ret < 0)
  19237. + return ret;
  19238. +
  19239. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[1],
  19240. + mmal_ctrl->mmal_id,
  19241. + &u32_value, sizeof(u32_value));
  19242. + if (ret < 0)
  19243. + return ret;
  19244. +
  19245. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[2],
  19246. + mmal_ctrl->mmal_id,
  19247. + &u32_value, sizeof(u32_value));
  19248. +
  19249. + return ret;
  19250. +
  19251. +}
  19252. +
  19253. +static int ctrl_set_exposure(struct bm2835_mmal_dev *dev,
  19254. + struct v4l2_ctrl *ctrl,
  19255. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19256. +{
  19257. + enum mmal_parameter_exposuremode exp_mode = dev->exposure_mode_user;
  19258. + u32 shutter_speed = 0;
  19259. + struct vchiq_mmal_port *control;
  19260. + int ret = 0;
  19261. +
  19262. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  19263. +
  19264. + if (mmal_ctrl->mmal_id == MMAL_PARAMETER_SHUTTER_SPEED) {
  19265. + /* V4L2 is in 100usec increments.
  19266. + * MMAL is 1usec.
  19267. + */
  19268. + dev->manual_shutter_speed = ctrl->val * 100;
  19269. + } else if (mmal_ctrl->mmal_id == MMAL_PARAMETER_EXPOSURE_MODE) {
  19270. + switch (ctrl->val) {
  19271. + case V4L2_EXPOSURE_AUTO:
  19272. + exp_mode = MMAL_PARAM_EXPOSUREMODE_AUTO;
  19273. + break;
  19274. +
  19275. + case V4L2_EXPOSURE_MANUAL:
  19276. + exp_mode = MMAL_PARAM_EXPOSUREMODE_OFF;
  19277. + break;
  19278. + }
  19279. + dev->exposure_mode_user = exp_mode;
  19280. + dev->exposure_mode_v4l2_user = ctrl->val;
  19281. + } else if (mmal_ctrl->id == V4L2_CID_EXPOSURE_AUTO_PRIORITY) {
  19282. + dev->exp_auto_priority = ctrl->val;
  19283. + }
  19284. +
  19285. + if (dev->scene_mode == V4L2_SCENE_MODE_NONE) {
  19286. + if (exp_mode == MMAL_PARAM_EXPOSUREMODE_OFF)
  19287. + shutter_speed = dev->manual_shutter_speed;
  19288. +
  19289. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  19290. + control,
  19291. + MMAL_PARAMETER_SHUTTER_SPEED,
  19292. + &shutter_speed,
  19293. + sizeof(shutter_speed));
  19294. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  19295. + control,
  19296. + MMAL_PARAMETER_EXPOSURE_MODE,
  19297. + &exp_mode,
  19298. + sizeof(u32));
  19299. + dev->exposure_mode_active = exp_mode;
  19300. + }
  19301. + /* exposure_dynamic_framerate (V4L2_CID_EXPOSURE_AUTO_PRIORITY) should
  19302. + * always apply irrespective of scene mode.
  19303. + */
  19304. + ret += set_framerate_params(dev);
  19305. +
  19306. + return ret;
  19307. +}
  19308. +
  19309. +static int ctrl_set_metering_mode(struct bm2835_mmal_dev *dev,
  19310. + struct v4l2_ctrl *ctrl,
  19311. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19312. +{
  19313. + switch (ctrl->val) {
  19314. + case V4L2_EXPOSURE_METERING_AVERAGE:
  19315. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE;
  19316. + break;
  19317. +
  19318. + case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED:
  19319. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT;
  19320. + break;
  19321. +
  19322. + case V4L2_EXPOSURE_METERING_SPOT:
  19323. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT;
  19324. + break;
  19325. +
  19326. + /* todo matrix weighting not added to Linux API till 3.9
  19327. + case V4L2_EXPOSURE_METERING_MATRIX:
  19328. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX;
  19329. + break;
  19330. + */
  19331. +
  19332. + }
  19333. +
  19334. + if (dev->scene_mode == V4L2_SCENE_MODE_NONE) {
  19335. + struct vchiq_mmal_port *control;
  19336. + u32 u32_value = dev->metering_mode;
  19337. +
  19338. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  19339. +
  19340. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  19341. + mmal_ctrl->mmal_id,
  19342. + &u32_value, sizeof(u32_value));
  19343. + } else
  19344. + return 0;
  19345. +}
  19346. +
  19347. +static int ctrl_set_flicker_avoidance(struct bm2835_mmal_dev *dev,
  19348. + struct v4l2_ctrl *ctrl,
  19349. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19350. +{
  19351. + u32 u32_value;
  19352. + struct vchiq_mmal_port *control;
  19353. +
  19354. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  19355. +
  19356. + switch (ctrl->val) {
  19357. + case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
  19358. + u32_value = MMAL_PARAM_FLICKERAVOID_OFF;
  19359. + break;
  19360. + case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
  19361. + u32_value = MMAL_PARAM_FLICKERAVOID_50HZ;
  19362. + break;
  19363. + case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
  19364. + u32_value = MMAL_PARAM_FLICKERAVOID_60HZ;
  19365. + break;
  19366. + case V4L2_CID_POWER_LINE_FREQUENCY_AUTO:
  19367. + u32_value = MMAL_PARAM_FLICKERAVOID_AUTO;
  19368. + break;
  19369. + }
  19370. +
  19371. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  19372. + mmal_ctrl->mmal_id,
  19373. + &u32_value, sizeof(u32_value));
  19374. +}
  19375. +
  19376. +static int ctrl_set_awb_mode(struct bm2835_mmal_dev *dev,
  19377. + struct v4l2_ctrl *ctrl,
  19378. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19379. +{
  19380. + u32 u32_value;
  19381. + struct vchiq_mmal_port *control;
  19382. +
  19383. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  19384. +
  19385. + switch (ctrl->val) {
  19386. + case V4L2_WHITE_BALANCE_MANUAL:
  19387. + u32_value = MMAL_PARAM_AWBMODE_OFF;
  19388. + break;
  19389. +
  19390. + case V4L2_WHITE_BALANCE_AUTO:
  19391. + u32_value = MMAL_PARAM_AWBMODE_AUTO;
  19392. + break;
  19393. +
  19394. + case V4L2_WHITE_BALANCE_INCANDESCENT:
  19395. + u32_value = MMAL_PARAM_AWBMODE_INCANDESCENT;
  19396. + break;
  19397. +
  19398. + case V4L2_WHITE_BALANCE_FLUORESCENT:
  19399. + u32_value = MMAL_PARAM_AWBMODE_FLUORESCENT;
  19400. + break;
  19401. +
  19402. + case V4L2_WHITE_BALANCE_FLUORESCENT_H:
  19403. + u32_value = MMAL_PARAM_AWBMODE_TUNGSTEN;
  19404. + break;
  19405. +
  19406. + case V4L2_WHITE_BALANCE_HORIZON:
  19407. + u32_value = MMAL_PARAM_AWBMODE_HORIZON;
  19408. + break;
  19409. +
  19410. + case V4L2_WHITE_BALANCE_DAYLIGHT:
  19411. + u32_value = MMAL_PARAM_AWBMODE_SUNLIGHT;
  19412. + break;
  19413. +
  19414. + case V4L2_WHITE_BALANCE_FLASH:
  19415. + u32_value = MMAL_PARAM_AWBMODE_FLASH;
  19416. + break;
  19417. +
  19418. + case V4L2_WHITE_BALANCE_CLOUDY:
  19419. + u32_value = MMAL_PARAM_AWBMODE_CLOUDY;
  19420. + break;
  19421. +
  19422. + case V4L2_WHITE_BALANCE_SHADE:
  19423. + u32_value = MMAL_PARAM_AWBMODE_SHADE;
  19424. + break;
  19425. +
  19426. + }
  19427. +
  19428. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  19429. + mmal_ctrl->mmal_id,
  19430. + &u32_value, sizeof(u32_value));
  19431. +}
  19432. +
  19433. +static int ctrl_set_awb_gains(struct bm2835_mmal_dev *dev,
  19434. + struct v4l2_ctrl *ctrl,
  19435. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19436. +{
  19437. + struct vchiq_mmal_port *control;
  19438. + struct mmal_parameter_awbgains gains;
  19439. +
  19440. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  19441. +
  19442. + if (ctrl->id == V4L2_CID_RED_BALANCE)
  19443. + dev->red_gain = ctrl->val;
  19444. + else if (ctrl->id == V4L2_CID_BLUE_BALANCE)
  19445. + dev->blue_gain = ctrl->val;
  19446. +
  19447. + gains.r_gain.num = dev->red_gain;
  19448. + gains.b_gain.num = dev->blue_gain;
  19449. + gains.r_gain.den = gains.b_gain.den = 1000;
  19450. +
  19451. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  19452. + mmal_ctrl->mmal_id,
  19453. + &gains, sizeof(gains));
  19454. +}
  19455. +
  19456. +static int ctrl_set_image_effect(struct bm2835_mmal_dev *dev,
  19457. + struct v4l2_ctrl *ctrl,
  19458. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19459. +{
  19460. + int ret = -EINVAL;
  19461. + int i, j;
  19462. + struct vchiq_mmal_port *control;
  19463. + struct mmal_parameter_imagefx_parameters imagefx;
  19464. +
  19465. + for (i = 0; i < ARRAY_SIZE(v4l2_to_mmal_effects_values); i++) {
  19466. + if (ctrl->val == v4l2_to_mmal_effects_values[i].v4l2_effect) {
  19467. +
  19468. + imagefx.effect =
  19469. + v4l2_to_mmal_effects_values[i].mmal_effect;
  19470. + imagefx.num_effect_params =
  19471. + v4l2_to_mmal_effects_values[i].num_effect_params;
  19472. +
  19473. + if (imagefx.num_effect_params > MMAL_MAX_IMAGEFX_PARAMETERS)
  19474. + imagefx.num_effect_params = MMAL_MAX_IMAGEFX_PARAMETERS;
  19475. +
  19476. + for (j = 0; j < imagefx.num_effect_params; j++)
  19477. + imagefx.effect_parameter[j] =
  19478. + v4l2_to_mmal_effects_values[i].effect_params[j];
  19479. +
  19480. + dev->colourfx.enable =
  19481. + v4l2_to_mmal_effects_values[i].col_fx_enable;
  19482. + if (!v4l2_to_mmal_effects_values[i].col_fx_fixed_cbcr) {
  19483. + dev->colourfx.u =
  19484. + v4l2_to_mmal_effects_values[i].u;
  19485. + dev->colourfx.v =
  19486. + v4l2_to_mmal_effects_values[i].v;
  19487. + }
  19488. +
  19489. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  19490. +
  19491. + ret = vchiq_mmal_port_parameter_set(
  19492. + dev->instance, control,
  19493. + MMAL_PARAMETER_IMAGE_EFFECT_PARAMETERS,
  19494. + &imagefx, sizeof(imagefx));
  19495. + if (ret)
  19496. + goto exit;
  19497. +
  19498. + ret = vchiq_mmal_port_parameter_set(
  19499. + dev->instance, control,
  19500. + MMAL_PARAMETER_COLOUR_EFFECT,
  19501. + &dev->colourfx, sizeof(dev->colourfx));
  19502. + }
  19503. + }
  19504. +
  19505. +exit:
  19506. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  19507. + "mmal_ctrl:%p ctrl id:0x%x ctrl val:%d imagefx:0x%x color_effect:%s u:%d v:%d ret %d(%d)\n",
  19508. + mmal_ctrl, ctrl->id, ctrl->val, imagefx.effect,
  19509. + dev->colourfx.enable ? "true" : "false",
  19510. + dev->colourfx.u, dev->colourfx.v,
  19511. + ret, (ret == 0 ? 0 : -EINVAL));
  19512. + return (ret == 0 ? 0 : EINVAL);
  19513. +}
  19514. +
  19515. +static int ctrl_set_colfx(struct bm2835_mmal_dev *dev,
  19516. + struct v4l2_ctrl *ctrl,
  19517. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19518. +{
  19519. + int ret = -EINVAL;
  19520. + struct vchiq_mmal_port *control;
  19521. +
  19522. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  19523. +
  19524. + dev->colourfx.enable = (ctrl->val & 0xff00) >> 8;
  19525. + dev->colourfx.enable = ctrl->val & 0xff;
  19526. +
  19527. + ret = vchiq_mmal_port_parameter_set(dev->instance, control,
  19528. + MMAL_PARAMETER_COLOUR_EFFECT,
  19529. + &dev->colourfx, sizeof(dev->colourfx));
  19530. +
  19531. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  19532. + "%s: After: mmal_ctrl:%p ctrl id:0x%x ctrl val:%d ret %d(%d)\n",
  19533. + __func__, mmal_ctrl, ctrl->id, ctrl->val, ret,
  19534. + (ret == 0 ? 0 : -EINVAL));
  19535. + return (ret == 0 ? 0 : EINVAL);
  19536. +}
  19537. +
  19538. +static int ctrl_set_bitrate(struct bm2835_mmal_dev *dev,
  19539. + struct v4l2_ctrl *ctrl,
  19540. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19541. +{
  19542. + int ret;
  19543. + struct vchiq_mmal_port *encoder_out;
  19544. +
  19545. + dev->capture.encode_bitrate = ctrl->val;
  19546. +
  19547. + encoder_out = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  19548. +
  19549. + ret = vchiq_mmal_port_parameter_set(dev->instance, encoder_out,
  19550. + mmal_ctrl->mmal_id,
  19551. + &ctrl->val, sizeof(ctrl->val));
  19552. + ret = 0;
  19553. + return ret;
  19554. +}
  19555. +
  19556. +static int ctrl_set_bitrate_mode(struct bm2835_mmal_dev *dev,
  19557. + struct v4l2_ctrl *ctrl,
  19558. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19559. +{
  19560. + u32 bitrate_mode;
  19561. + struct vchiq_mmal_port *encoder_out;
  19562. +
  19563. + encoder_out = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  19564. +
  19565. + dev->capture.encode_bitrate_mode = ctrl->val;
  19566. + switch (ctrl->val) {
  19567. + default:
  19568. + case V4L2_MPEG_VIDEO_BITRATE_MODE_VBR:
  19569. + bitrate_mode = MMAL_VIDEO_RATECONTROL_VARIABLE;
  19570. + break;
  19571. + case V4L2_MPEG_VIDEO_BITRATE_MODE_CBR:
  19572. + bitrate_mode = MMAL_VIDEO_RATECONTROL_CONSTANT;
  19573. + break;
  19574. + }
  19575. +
  19576. + vchiq_mmal_port_parameter_set(dev->instance, encoder_out,
  19577. + mmal_ctrl->mmal_id,
  19578. + &bitrate_mode,
  19579. + sizeof(bitrate_mode));
  19580. + return 0;
  19581. +}
  19582. +
  19583. +static int ctrl_set_image_encode_output(struct bm2835_mmal_dev *dev,
  19584. + struct v4l2_ctrl *ctrl,
  19585. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19586. +{
  19587. + u32 u32_value;
  19588. + struct vchiq_mmal_port *jpeg_out;
  19589. +
  19590. + jpeg_out = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
  19591. +
  19592. + u32_value = ctrl->val;
  19593. +
  19594. + return vchiq_mmal_port_parameter_set(dev->instance, jpeg_out,
  19595. + mmal_ctrl->mmal_id,
  19596. + &u32_value, sizeof(u32_value));
  19597. +}
  19598. +
  19599. +static int ctrl_set_video_encode_param_output(struct bm2835_mmal_dev *dev,
  19600. + struct v4l2_ctrl *ctrl,
  19601. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19602. +{
  19603. + u32 u32_value;
  19604. + struct vchiq_mmal_port *vid_enc_ctl;
  19605. +
  19606. + vid_enc_ctl = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  19607. +
  19608. + u32_value = ctrl->val;
  19609. +
  19610. + return vchiq_mmal_port_parameter_set(dev->instance, vid_enc_ctl,
  19611. + mmal_ctrl->mmal_id,
  19612. + &u32_value, sizeof(u32_value));
  19613. +}
  19614. +
  19615. +static int ctrl_set_video_encode_profile_level(struct bm2835_mmal_dev *dev,
  19616. + struct v4l2_ctrl *ctrl,
  19617. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19618. +{
  19619. + struct mmal_parameter_video_profile param;
  19620. + int ret = 0;
  19621. +
  19622. + if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_PROFILE) {
  19623. + switch (ctrl->val) {
  19624. + case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
  19625. + case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
  19626. + case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
  19627. + case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
  19628. + dev->capture.enc_profile = ctrl->val;
  19629. + break;
  19630. + default:
  19631. + ret = -EINVAL;
  19632. + break;
  19633. + }
  19634. + } else if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_LEVEL) {
  19635. + switch (ctrl->val) {
  19636. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
  19637. + case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
  19638. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
  19639. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
  19640. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
  19641. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
  19642. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
  19643. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
  19644. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
  19645. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
  19646. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
  19647. + case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
  19648. + dev->capture.enc_level = ctrl->val;
  19649. + break;
  19650. + default:
  19651. + ret = -EINVAL;
  19652. + break;
  19653. + }
  19654. + }
  19655. +
  19656. + if (!ret) {
  19657. + switch (dev->capture.enc_profile) {
  19658. + case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
  19659. + param.profile = MMAL_VIDEO_PROFILE_H264_BASELINE;
  19660. + break;
  19661. + case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
  19662. + param.profile =
  19663. + MMAL_VIDEO_PROFILE_H264_CONSTRAINED_BASELINE;
  19664. + break;
  19665. + case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
  19666. + param.profile = MMAL_VIDEO_PROFILE_H264_MAIN;
  19667. + break;
  19668. + case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
  19669. + param.profile = MMAL_VIDEO_PROFILE_H264_HIGH;
  19670. + break;
  19671. + default:
  19672. + /* Should never get here */
  19673. + break;
  19674. + }
  19675. +
  19676. + switch (dev->capture.enc_level) {
  19677. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
  19678. + param.level = MMAL_VIDEO_LEVEL_H264_1;
  19679. + break;
  19680. + case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
  19681. + param.level = MMAL_VIDEO_LEVEL_H264_1b;
  19682. + break;
  19683. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
  19684. + param.level = MMAL_VIDEO_LEVEL_H264_11;
  19685. + break;
  19686. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
  19687. + param.level = MMAL_VIDEO_LEVEL_H264_12;
  19688. + break;
  19689. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
  19690. + param.level = MMAL_VIDEO_LEVEL_H264_13;
  19691. + break;
  19692. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
  19693. + param.level = MMAL_VIDEO_LEVEL_H264_2;
  19694. + break;
  19695. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
  19696. + param.level = MMAL_VIDEO_LEVEL_H264_21;
  19697. + break;
  19698. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
  19699. + param.level = MMAL_VIDEO_LEVEL_H264_22;
  19700. + break;
  19701. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
  19702. + param.level = MMAL_VIDEO_LEVEL_H264_3;
  19703. + break;
  19704. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
  19705. + param.level = MMAL_VIDEO_LEVEL_H264_31;
  19706. + break;
  19707. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
  19708. + param.level = MMAL_VIDEO_LEVEL_H264_32;
  19709. + break;
  19710. + case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
  19711. + param.level = MMAL_VIDEO_LEVEL_H264_4;
  19712. + break;
  19713. + default:
  19714. + /* Should never get here */
  19715. + break;
  19716. + }
  19717. +
  19718. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  19719. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0],
  19720. + mmal_ctrl->mmal_id,
  19721. + &param, sizeof(param));
  19722. + }
  19723. + return ret;
  19724. +}
  19725. +
  19726. +static int ctrl_set_scene_mode(struct bm2835_mmal_dev *dev,
  19727. + struct v4l2_ctrl *ctrl,
  19728. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  19729. +{
  19730. + int ret = 0;
  19731. + int shutter_speed;
  19732. + struct vchiq_mmal_port *control;
  19733. +
  19734. + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
  19735. + "scene mode selected %d, was %d\n", ctrl->val,
  19736. + dev->scene_mode);
  19737. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  19738. +
  19739. + if (ctrl->val == dev->scene_mode)
  19740. + return 0;
  19741. +
  19742. + if (ctrl->val == V4L2_SCENE_MODE_NONE) {
  19743. + /* Restore all user selections */
  19744. + dev->scene_mode = V4L2_SCENE_MODE_NONE;
  19745. +
  19746. + if (dev->exposure_mode_user == MMAL_PARAM_EXPOSUREMODE_OFF)
  19747. + shutter_speed = dev->manual_shutter_speed;
  19748. + else
  19749. + shutter_speed = 0;
  19750. +
  19751. + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
  19752. + "%s: scene mode none: shut_speed %d, exp_mode %d, metering %d\n",
  19753. + __func__, shutter_speed, dev->exposure_mode_user,
  19754. + dev->metering_mode);
  19755. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  19756. + control,
  19757. + MMAL_PARAMETER_SHUTTER_SPEED,
  19758. + &shutter_speed,
  19759. + sizeof(shutter_speed));
  19760. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  19761. + control,
  19762. + MMAL_PARAMETER_EXPOSURE_MODE,
  19763. + &dev->exposure_mode_user,
  19764. + sizeof(u32));
  19765. + dev->exposure_mode_active = dev->exposure_mode_user;
  19766. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  19767. + control,
  19768. + MMAL_PARAMETER_EXP_METERING_MODE,
  19769. + &dev->metering_mode,
  19770. + sizeof(u32));
  19771. + ret += set_framerate_params(dev);
  19772. + } else {
  19773. + /* Set up scene mode */
  19774. + int i;
  19775. + const struct v4l2_mmal_scene_config *scene = NULL;
  19776. + int shutter_speed;
  19777. + enum mmal_parameter_exposuremode exposure_mode;
  19778. + enum mmal_parameter_exposuremeteringmode metering_mode;
  19779. +
  19780. + for (i = 0; i < ARRAY_SIZE(scene_configs); i++) {
  19781. + if (scene_configs[i].v4l2_scene ==
  19782. + ctrl->val) {
  19783. + scene = &scene_configs[i];
  19784. + break;
  19785. + }
  19786. + }
  19787. + if (i >= ARRAY_SIZE(scene_configs))
  19788. + return -EINVAL;
  19789. +
  19790. + /* Set all the values */
  19791. + dev->scene_mode = ctrl->val;
  19792. +
  19793. + if (scene->exposure_mode == MMAL_PARAM_EXPOSUREMODE_OFF)
  19794. + shutter_speed = dev->manual_shutter_speed;
  19795. + else
  19796. + shutter_speed = 0;
  19797. + exposure_mode = scene->exposure_mode;
  19798. + metering_mode = scene->metering_mode;
  19799. +
  19800. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  19801. + "%s: scene mode none: shut_speed %d, exp_mode %d, metering %d\n",
  19802. + __func__, shutter_speed, exposure_mode, metering_mode);
  19803. +
  19804. + ret = vchiq_mmal_port_parameter_set(dev->instance, control,
  19805. + MMAL_PARAMETER_SHUTTER_SPEED,
  19806. + &shutter_speed,
  19807. + sizeof(shutter_speed));
  19808. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  19809. + control,
  19810. + MMAL_PARAMETER_EXPOSURE_MODE,
  19811. + &exposure_mode,
  19812. + sizeof(u32));
  19813. + dev->exposure_mode_active = exposure_mode;
  19814. + ret += vchiq_mmal_port_parameter_set(dev->instance, control,
  19815. + MMAL_PARAMETER_EXPOSURE_MODE,
  19816. + &exposure_mode,
  19817. + sizeof(u32));
  19818. + ret += vchiq_mmal_port_parameter_set(dev->instance, control,
  19819. + MMAL_PARAMETER_EXP_METERING_MODE,
  19820. + &metering_mode,
  19821. + sizeof(u32));
  19822. + ret += set_framerate_params(dev);
  19823. + }
  19824. + if (ret) {
  19825. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  19826. + "%s: Setting scene to %d, ret=%d\n",
  19827. + __func__, ctrl->val, ret);
  19828. + ret = -EINVAL;
  19829. + }
  19830. + return 0;
  19831. +}
  19832. +
  19833. +static int bm2835_mmal_s_ctrl(struct v4l2_ctrl *ctrl)
  19834. +{
  19835. + struct bm2835_mmal_dev *dev =
  19836. + container_of(ctrl->handler, struct bm2835_mmal_dev,
  19837. + ctrl_handler);
  19838. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl = ctrl->priv;
  19839. + int ret;
  19840. +
  19841. + if ((mmal_ctrl == NULL) ||
  19842. + (mmal_ctrl->id != ctrl->id) ||
  19843. + (mmal_ctrl->setter == NULL)) {
  19844. + pr_warn("mmal_ctrl:%p ctrl id:%d\n", mmal_ctrl, ctrl->id);
  19845. + return -EINVAL;
  19846. + }
  19847. +
  19848. + ret = mmal_ctrl->setter(dev, ctrl, mmal_ctrl);
  19849. + if (ret)
  19850. + pr_warn("ctrl id:%d/MMAL param %08X- returned ret %d\n",
  19851. + ctrl->id, mmal_ctrl->mmal_id, ret);
  19852. + if (mmal_ctrl->ignore_errors)
  19853. + ret = 0;
  19854. + return ret;
  19855. +}
  19856. +
  19857. +static const struct v4l2_ctrl_ops bm2835_mmal_ctrl_ops = {
  19858. + .s_ctrl = bm2835_mmal_s_ctrl,
  19859. +};
  19860. +
  19861. +
  19862. +
  19863. +static const struct bm2835_mmal_v4l2_ctrl v4l2_ctrls[V4L2_CTRL_COUNT] = {
  19864. + {
  19865. + V4L2_CID_SATURATION, MMAL_CONTROL_TYPE_STD,
  19866. + -100, 100, 0, 1, NULL,
  19867. + MMAL_PARAMETER_SATURATION,
  19868. + &ctrl_set_rational,
  19869. + false
  19870. + },
  19871. + {
  19872. + V4L2_CID_SHARPNESS, MMAL_CONTROL_TYPE_STD,
  19873. + -100, 100, 0, 1, NULL,
  19874. + MMAL_PARAMETER_SHARPNESS,
  19875. + &ctrl_set_rational,
  19876. + false
  19877. + },
  19878. + {
  19879. + V4L2_CID_CONTRAST, MMAL_CONTROL_TYPE_STD,
  19880. + -100, 100, 0, 1, NULL,
  19881. + MMAL_PARAMETER_CONTRAST,
  19882. + &ctrl_set_rational,
  19883. + false
  19884. + },
  19885. + {
  19886. + V4L2_CID_BRIGHTNESS, MMAL_CONTROL_TYPE_STD,
  19887. + 0, 100, 50, 1, NULL,
  19888. + MMAL_PARAMETER_BRIGHTNESS,
  19889. + &ctrl_set_rational,
  19890. + false
  19891. + },
  19892. + {
  19893. + V4L2_CID_ISO_SENSITIVITY, MMAL_CONTROL_TYPE_INT_MENU,
  19894. + 0, ARRAY_SIZE(iso_qmenu) - 1, 0, 1, iso_qmenu,
  19895. + MMAL_PARAMETER_ISO,
  19896. + &ctrl_set_value_menu,
  19897. + false
  19898. + },
  19899. + {
  19900. + V4L2_CID_IMAGE_STABILIZATION, MMAL_CONTROL_TYPE_STD,
  19901. + 0, 1, 0, 1, NULL,
  19902. + MMAL_PARAMETER_VIDEO_STABILISATION,
  19903. + &ctrl_set_value,
  19904. + false
  19905. + },
  19906. +/* {
  19907. + 0, MMAL_CONTROL_TYPE_CLUSTER, 3, 1, 0, NULL, 0, NULL
  19908. + }, */
  19909. + {
  19910. + V4L2_CID_EXPOSURE_AUTO, MMAL_CONTROL_TYPE_STD_MENU,
  19911. + ~0x03, 3, V4L2_EXPOSURE_AUTO, 0, NULL,
  19912. + MMAL_PARAMETER_EXPOSURE_MODE,
  19913. + &ctrl_set_exposure,
  19914. + false
  19915. + },
  19916. +/* todo this needs mixing in with set exposure
  19917. + {
  19918. + V4L2_CID_SCENE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
  19919. + },
  19920. + */
  19921. + {
  19922. + V4L2_CID_EXPOSURE_ABSOLUTE, MMAL_CONTROL_TYPE_STD,
  19923. + /* Units of 100usecs */
  19924. + 1, 1*1000*10, 100*10, 1, NULL,
  19925. + MMAL_PARAMETER_SHUTTER_SPEED,
  19926. + &ctrl_set_exposure,
  19927. + false
  19928. + },
  19929. + {
  19930. + V4L2_CID_AUTO_EXPOSURE_BIAS, MMAL_CONTROL_TYPE_INT_MENU,
  19931. + 0, ARRAY_SIZE(ev_bias_qmenu) - 1,
  19932. + (ARRAY_SIZE(ev_bias_qmenu)+1)/2 - 1, 0, ev_bias_qmenu,
  19933. + MMAL_PARAMETER_EXPOSURE_COMP,
  19934. + &ctrl_set_value_ev,
  19935. + false
  19936. + },
  19937. + {
  19938. + V4L2_CID_EXPOSURE_AUTO_PRIORITY, MMAL_CONTROL_TYPE_STD,
  19939. + 0, 1,
  19940. + 0, 1, NULL,
  19941. + 0, /* Dummy MMAL ID as it gets mapped into FPS range*/
  19942. + &ctrl_set_exposure,
  19943. + false
  19944. + },
  19945. + {
  19946. + V4L2_CID_EXPOSURE_METERING,
  19947. + MMAL_CONTROL_TYPE_STD_MENU,
  19948. + ~0x7, 2, V4L2_EXPOSURE_METERING_AVERAGE, 0, NULL,
  19949. + MMAL_PARAMETER_EXP_METERING_MODE,
  19950. + &ctrl_set_metering_mode,
  19951. + false
  19952. + },
  19953. + {
  19954. + V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE,
  19955. + MMAL_CONTROL_TYPE_STD_MENU,
  19956. + ~0x3ff, 9, V4L2_WHITE_BALANCE_AUTO, 0, NULL,
  19957. + MMAL_PARAMETER_AWB_MODE,
  19958. + &ctrl_set_awb_mode,
  19959. + false
  19960. + },
  19961. + {
  19962. + V4L2_CID_RED_BALANCE, MMAL_CONTROL_TYPE_STD,
  19963. + 1, 7999, 1000, 1, NULL,
  19964. + MMAL_PARAMETER_CUSTOM_AWB_GAINS,
  19965. + &ctrl_set_awb_gains,
  19966. + false
  19967. + },
  19968. + {
  19969. + V4L2_CID_BLUE_BALANCE, MMAL_CONTROL_TYPE_STD,
  19970. + 1, 7999, 1000, 1, NULL,
  19971. + MMAL_PARAMETER_CUSTOM_AWB_GAINS,
  19972. + &ctrl_set_awb_gains,
  19973. + false
  19974. + },
  19975. + {
  19976. + V4L2_CID_COLORFX, MMAL_CONTROL_TYPE_STD_MENU,
  19977. + 0, 15, V4L2_COLORFX_NONE, 0, NULL,
  19978. + MMAL_PARAMETER_IMAGE_EFFECT,
  19979. + &ctrl_set_image_effect,
  19980. + false
  19981. + },
  19982. + {
  19983. + V4L2_CID_COLORFX_CBCR, MMAL_CONTROL_TYPE_STD,
  19984. + 0, 0xffff, 0x8080, 1, NULL,
  19985. + MMAL_PARAMETER_COLOUR_EFFECT,
  19986. + &ctrl_set_colfx,
  19987. + false
  19988. + },
  19989. + {
  19990. + V4L2_CID_ROTATE, MMAL_CONTROL_TYPE_STD,
  19991. + 0, 360, 0, 90, NULL,
  19992. + MMAL_PARAMETER_ROTATION,
  19993. + &ctrl_set_rotate,
  19994. + false
  19995. + },
  19996. + {
  19997. + V4L2_CID_HFLIP, MMAL_CONTROL_TYPE_STD,
  19998. + 0, 1, 0, 1, NULL,
  19999. + MMAL_PARAMETER_MIRROR,
  20000. + &ctrl_set_flip,
  20001. + false
  20002. + },
  20003. + {
  20004. + V4L2_CID_VFLIP, MMAL_CONTROL_TYPE_STD,
  20005. + 0, 1, 0, 1, NULL,
  20006. + MMAL_PARAMETER_MIRROR,
  20007. + &ctrl_set_flip,
  20008. + false
  20009. + },
  20010. + {
  20011. + V4L2_CID_MPEG_VIDEO_BITRATE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
  20012. + 0, ARRAY_SIZE(bitrate_mode_qmenu) - 1,
  20013. + 0, 0, bitrate_mode_qmenu,
  20014. + MMAL_PARAMETER_RATECONTROL,
  20015. + &ctrl_set_bitrate_mode,
  20016. + false
  20017. + },
  20018. + {
  20019. + V4L2_CID_MPEG_VIDEO_BITRATE, MMAL_CONTROL_TYPE_STD,
  20020. + 25*1000, 25*1000*1000, 10*1000*1000, 25*1000, NULL,
  20021. + MMAL_PARAMETER_VIDEO_BIT_RATE,
  20022. + &ctrl_set_bitrate,
  20023. + false
  20024. + },
  20025. + {
  20026. + V4L2_CID_JPEG_COMPRESSION_QUALITY, MMAL_CONTROL_TYPE_STD,
  20027. + 1, 100,
  20028. + 30, 1, NULL,
  20029. + MMAL_PARAMETER_JPEG_Q_FACTOR,
  20030. + &ctrl_set_image_encode_output,
  20031. + false
  20032. + },
  20033. + {
  20034. + V4L2_CID_POWER_LINE_FREQUENCY, MMAL_CONTROL_TYPE_STD_MENU,
  20035. + 0, ARRAY_SIZE(mains_freq_qmenu) - 1,
  20036. + 1, 1, NULL,
  20037. + MMAL_PARAMETER_FLICKER_AVOID,
  20038. + &ctrl_set_flicker_avoidance,
  20039. + false
  20040. + },
  20041. + {
  20042. + V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER, MMAL_CONTROL_TYPE_STD,
  20043. + 0, 1,
  20044. + 0, 1, NULL,
  20045. + MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER,
  20046. + &ctrl_set_video_encode_param_output,
  20047. + true /* Errors ignored as requires latest firmware to work */
  20048. + },
  20049. + {
  20050. + V4L2_CID_MPEG_VIDEO_H264_PROFILE,
  20051. + MMAL_CONTROL_TYPE_STD_MENU,
  20052. + ~((1<<V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
  20053. + (1<<V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE) |
  20054. + (1<<V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
  20055. + (1<<V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
  20056. + V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
  20057. + V4L2_MPEG_VIDEO_H264_PROFILE_HIGH, 1, NULL,
  20058. + MMAL_PARAMETER_PROFILE,
  20059. + &ctrl_set_video_encode_profile_level,
  20060. + false
  20061. + },
  20062. + {
  20063. + V4L2_CID_MPEG_VIDEO_H264_LEVEL, MMAL_CONTROL_TYPE_STD_MENU,
  20064. + ~((1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_0) |
  20065. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1B) |
  20066. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_1) |
  20067. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_2) |
  20068. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_3) |
  20069. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
  20070. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_2_1) |
  20071. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_2_2) |
  20072. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
  20073. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
  20074. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
  20075. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_4_0)),
  20076. + V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
  20077. + V4L2_MPEG_VIDEO_H264_LEVEL_4_0, 1, NULL,
  20078. + MMAL_PARAMETER_PROFILE,
  20079. + &ctrl_set_video_encode_profile_level,
  20080. + false
  20081. + },
  20082. + {
  20083. + V4L2_CID_SCENE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
  20084. + -1, /* Min is computed at runtime */
  20085. + V4L2_SCENE_MODE_TEXT,
  20086. + V4L2_SCENE_MODE_NONE, 1, NULL,
  20087. + MMAL_PARAMETER_PROFILE,
  20088. + &ctrl_set_scene_mode,
  20089. + false
  20090. + },
  20091. + {
  20092. + V4L2_CID_MPEG_VIDEO_H264_I_PERIOD, MMAL_CONTROL_TYPE_STD,
  20093. + 0, 0x7FFFFFFF, 60, 1, NULL,
  20094. + MMAL_PARAMETER_INTRAPERIOD,
  20095. + &ctrl_set_video_encode_param_output,
  20096. + false
  20097. + },
  20098. +};
  20099. +
  20100. +int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev)
  20101. +{
  20102. + int c;
  20103. + int ret = 0;
  20104. +
  20105. + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
  20106. + if ((dev->ctrls[c]) && (v4l2_ctrls[c].setter)) {
  20107. + ret = v4l2_ctrls[c].setter(dev, dev->ctrls[c],
  20108. + &v4l2_ctrls[c]);
  20109. + if (!v4l2_ctrls[c].ignore_errors && ret) {
  20110. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  20111. + "Failed when setting default values for ctrl %d\n",
  20112. + c);
  20113. + break;
  20114. + }
  20115. + }
  20116. + }
  20117. + return ret;
  20118. +}
  20119. +
  20120. +int set_framerate_params(struct bm2835_mmal_dev *dev)
  20121. +{
  20122. + struct mmal_parameter_fps_range fps_range;
  20123. + int ret;
  20124. +
  20125. + if ((dev->exposure_mode_active != MMAL_PARAM_EXPOSUREMODE_OFF) &&
  20126. + (dev->exp_auto_priority)) {
  20127. + /* Variable FPS. Define min FPS as 1fps.
  20128. + * Max as max defined FPS.
  20129. + */
  20130. + fps_range.fps_low.num = 1;
  20131. + fps_range.fps_low.den = 1;
  20132. + fps_range.fps_high.num = dev->capture.timeperframe.denominator;
  20133. + fps_range.fps_high.den = dev->capture.timeperframe.numerator;
  20134. + } else {
  20135. + /* Fixed FPS - set min and max to be the same */
  20136. + fps_range.fps_low.num = fps_range.fps_high.num =
  20137. + dev->capture.timeperframe.denominator;
  20138. + fps_range.fps_low.den = fps_range.fps_high.den =
  20139. + dev->capture.timeperframe.numerator;
  20140. + }
  20141. +
  20142. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  20143. + "Set fps range to %d/%d to %d/%d\n",
  20144. + fps_range.fps_low.num,
  20145. + fps_range.fps_low.den,
  20146. + fps_range.fps_high.num,
  20147. + fps_range.fps_high.den
  20148. + );
  20149. +
  20150. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  20151. + &dev->component[MMAL_COMPONENT_CAMERA]->
  20152. + output[MMAL_CAMERA_PORT_PREVIEW],
  20153. + MMAL_PARAMETER_FPS_RANGE,
  20154. + &fps_range, sizeof(fps_range));
  20155. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  20156. + &dev->component[MMAL_COMPONENT_CAMERA]->
  20157. + output[MMAL_CAMERA_PORT_VIDEO],
  20158. + MMAL_PARAMETER_FPS_RANGE,
  20159. + &fps_range, sizeof(fps_range));
  20160. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  20161. + &dev->component[MMAL_COMPONENT_CAMERA]->
  20162. + output[MMAL_CAMERA_PORT_CAPTURE],
  20163. + MMAL_PARAMETER_FPS_RANGE,
  20164. + &fps_range, sizeof(fps_range));
  20165. + if (ret)
  20166. + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
  20167. + "Failed to set fps ret %d\n",
  20168. + ret);
  20169. +
  20170. + return ret;
  20171. +
  20172. +}
  20173. +
  20174. +int bm2835_mmal_init_controls(struct bm2835_mmal_dev *dev,
  20175. + struct v4l2_ctrl_handler *hdl)
  20176. +{
  20177. + int c;
  20178. + const struct bm2835_mmal_v4l2_ctrl *ctrl;
  20179. +
  20180. + v4l2_ctrl_handler_init(hdl, V4L2_CTRL_COUNT);
  20181. +
  20182. + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
  20183. + ctrl = &v4l2_ctrls[c];
  20184. +
  20185. + switch (ctrl->type) {
  20186. + case MMAL_CONTROL_TYPE_STD:
  20187. + dev->ctrls[c] = v4l2_ctrl_new_std(hdl,
  20188. + &bm2835_mmal_ctrl_ops, ctrl->id,
  20189. + ctrl->min, ctrl->max, ctrl->step, ctrl->def);
  20190. + break;
  20191. +
  20192. + case MMAL_CONTROL_TYPE_STD_MENU:
  20193. + {
  20194. + int mask = ctrl->min;
  20195. +
  20196. + if (ctrl->id == V4L2_CID_SCENE_MODE) {
  20197. + /* Special handling to work out the mask
  20198. + * value based on the scene_configs array
  20199. + * at runtime. Reduces the chance of
  20200. + * mismatches.
  20201. + */
  20202. + int i;
  20203. + mask = 1<<V4L2_SCENE_MODE_NONE;
  20204. + for (i = 0;
  20205. + i < ARRAY_SIZE(scene_configs);
  20206. + i++) {
  20207. + mask |= 1<<scene_configs[i].v4l2_scene;
  20208. + }
  20209. + mask = ~mask;
  20210. + }
  20211. +
  20212. + dev->ctrls[c] = v4l2_ctrl_new_std_menu(hdl,
  20213. + &bm2835_mmal_ctrl_ops, ctrl->id,
  20214. + ctrl->max, mask, ctrl->def);
  20215. + break;
  20216. + }
  20217. +
  20218. + case MMAL_CONTROL_TYPE_INT_MENU:
  20219. + dev->ctrls[c] = v4l2_ctrl_new_int_menu(hdl,
  20220. + &bm2835_mmal_ctrl_ops, ctrl->id,
  20221. + ctrl->max, ctrl->def, ctrl->imenu);
  20222. + break;
  20223. +
  20224. + case MMAL_CONTROL_TYPE_CLUSTER:
  20225. + /* skip this entry when constructing controls */
  20226. + continue;
  20227. + }
  20228. +
  20229. + if (hdl->error)
  20230. + break;
  20231. +
  20232. + dev->ctrls[c]->priv = (void *)ctrl;
  20233. + }
  20234. +
  20235. + if (hdl->error) {
  20236. + pr_err("error adding control %d/%d id 0x%x\n", c,
  20237. + V4L2_CTRL_COUNT, ctrl->id);
  20238. + return hdl->error;
  20239. + }
  20240. +
  20241. + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
  20242. + ctrl = &v4l2_ctrls[c];
  20243. +
  20244. + switch (ctrl->type) {
  20245. + case MMAL_CONTROL_TYPE_CLUSTER:
  20246. + v4l2_ctrl_auto_cluster(ctrl->min,
  20247. + &dev->ctrls[c+1],
  20248. + ctrl->max,
  20249. + ctrl->def);
  20250. + break;
  20251. +
  20252. + case MMAL_CONTROL_TYPE_STD:
  20253. + case MMAL_CONTROL_TYPE_STD_MENU:
  20254. + case MMAL_CONTROL_TYPE_INT_MENU:
  20255. + break;
  20256. + }
  20257. +
  20258. + }
  20259. +
  20260. + return 0;
  20261. +}
  20262. diff -Nur linux-3.12.33/drivers/media/platform/bcm2835/Kconfig linux-3.12.33-rpi/drivers/media/platform/bcm2835/Kconfig
  20263. --- linux-3.12.33/drivers/media/platform/bcm2835/Kconfig 1969-12-31 18:00:00.000000000 -0600
  20264. +++ linux-3.12.33-rpi/drivers/media/platform/bcm2835/Kconfig 2014-12-03 19:13:38.012418001 -0600
  20265. @@ -0,0 +1,25 @@
  20266. +# Broadcom VideoCore IV v4l2 camera support
  20267. +
  20268. +config VIDEO_BCM2835
  20269. + bool "Broadcom BCM2835 camera interface driver"
  20270. + depends on VIDEO_V4L2 && ARCH_BCM2708
  20271. + ---help---
  20272. + Say Y here to enable camera host interface devices for
  20273. + Broadcom BCM2835 SoC. This operates over the VCHIQ interface
  20274. + to a service running on VideoCore.
  20275. +
  20276. +
  20277. +if VIDEO_BCM2835
  20278. +
  20279. +config VIDEO_BCM2835_MMAL
  20280. + tristate "Broadcom BM2835 MMAL camera interface driver"
  20281. + depends on BCM2708_VCHIQ
  20282. + select VIDEOBUF2_VMALLOC
  20283. + ---help---
  20284. + This is a V4L2 driver for the Broadcom BCM2835 MMAL camera host interface
  20285. +
  20286. + To compile this driver as a module, choose M here: the
  20287. + module will be called bcm2835-v4l2.o
  20288. +
  20289. +
  20290. +endif # VIDEO_BM2835
  20291. diff -Nur linux-3.12.33/drivers/media/platform/bcm2835/Makefile linux-3.12.33-rpi/drivers/media/platform/bcm2835/Makefile
  20292. --- linux-3.12.33/drivers/media/platform/bcm2835/Makefile 1969-12-31 18:00:00.000000000 -0600
  20293. +++ linux-3.12.33-rpi/drivers/media/platform/bcm2835/Makefile 2014-12-03 19:13:38.012418001 -0600
  20294. @@ -0,0 +1,5 @@
  20295. +bcm2835-v4l2-objs := bcm2835-camera.o controls.o mmal-vchiq.o
  20296. +
  20297. +obj-$(CONFIG_VIDEO_BCM2835_MMAL) += bcm2835-v4l2.o
  20298. +
  20299. +ccflags-$(CONFIG_VIDEO_BCM2835) += -Idrivers/misc/vc04_services -Idrivers/misc/vc04_services/interface/vcos/linuxkernel -D__VCCOREVER__=0x04000000
  20300. diff -Nur linux-3.12.33/drivers/media/platform/bcm2835/mmal-common.h linux-3.12.33-rpi/drivers/media/platform/bcm2835/mmal-common.h
  20301. --- linux-3.12.33/drivers/media/platform/bcm2835/mmal-common.h 1969-12-31 18:00:00.000000000 -0600
  20302. +++ linux-3.12.33-rpi/drivers/media/platform/bcm2835/mmal-common.h 2014-12-03 19:13:38.012418001 -0600
  20303. @@ -0,0 +1,53 @@
  20304. +/*
  20305. + * Broadcom BM2835 V4L2 driver
  20306. + *
  20307. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  20308. + *
  20309. + * This file is subject to the terms and conditions of the GNU General Public
  20310. + * License. See the file COPYING in the main directory of this archive
  20311. + * for more details.
  20312. + *
  20313. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  20314. + * Dave Stevenson <dsteve@broadcom.com>
  20315. + * Simon Mellor <simellor@broadcom.com>
  20316. + * Luke Diamand <luked@broadcom.com>
  20317. + *
  20318. + * MMAL structures
  20319. + *
  20320. + */
  20321. +
  20322. +#define MMAL_FOURCC(a, b, c, d) ((a) | (b << 8) | (c << 16) | (d << 24))
  20323. +#define MMAL_MAGIC MMAL_FOURCC('m', 'm', 'a', 'l')
  20324. +
  20325. +/** Special value signalling that time is not known */
  20326. +#define MMAL_TIME_UNKNOWN (1LL<<63)
  20327. +
  20328. +/* mapping between v4l and mmal video modes */
  20329. +struct mmal_fmt {
  20330. + char *name;
  20331. + u32 fourcc; /* v4l2 format id */
  20332. + int flags; /* v4l2 flags field */
  20333. + u32 mmal;
  20334. + int depth;
  20335. + u32 mmal_component; /* MMAL component index to be used to encode */
  20336. +};
  20337. +
  20338. +/* buffer for one video frame */
  20339. +struct mmal_buffer {
  20340. + /* v4l buffer data -- must be first */
  20341. + struct vb2_buffer vb;
  20342. +
  20343. + /* list of buffers available */
  20344. + struct list_head list;
  20345. +
  20346. + void *buffer; /* buffer pointer */
  20347. + unsigned long buffer_size; /* size of allocated buffer */
  20348. +};
  20349. +
  20350. +/* */
  20351. +struct mmal_colourfx {
  20352. + s32 enable;
  20353. + u32 u;
  20354. + u32 v;
  20355. +};
  20356. +
  20357. diff -Nur linux-3.12.33/drivers/media/platform/bcm2835/mmal-encodings.h linux-3.12.33-rpi/drivers/media/platform/bcm2835/mmal-encodings.h
  20358. --- linux-3.12.33/drivers/media/platform/bcm2835/mmal-encodings.h 1969-12-31 18:00:00.000000000 -0600
  20359. +++ linux-3.12.33-rpi/drivers/media/platform/bcm2835/mmal-encodings.h 2014-12-03 19:13:38.012418001 -0600
  20360. @@ -0,0 +1,127 @@
  20361. +/*
  20362. + * Broadcom BM2835 V4L2 driver
  20363. + *
  20364. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  20365. + *
  20366. + * This file is subject to the terms and conditions of the GNU General Public
  20367. + * License. See the file COPYING in the main directory of this archive
  20368. + * for more details.
  20369. + *
  20370. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  20371. + * Dave Stevenson <dsteve@broadcom.com>
  20372. + * Simon Mellor <simellor@broadcom.com>
  20373. + * Luke Diamand <luked@broadcom.com>
  20374. + */
  20375. +#ifndef MMAL_ENCODINGS_H
  20376. +#define MMAL_ENCODINGS_H
  20377. +
  20378. +#define MMAL_ENCODING_H264 MMAL_FOURCC('H', '2', '6', '4')
  20379. +#define MMAL_ENCODING_H263 MMAL_FOURCC('H', '2', '6', '3')
  20380. +#define MMAL_ENCODING_MP4V MMAL_FOURCC('M', 'P', '4', 'V')
  20381. +#define MMAL_ENCODING_MP2V MMAL_FOURCC('M', 'P', '2', 'V')
  20382. +#define MMAL_ENCODING_MP1V MMAL_FOURCC('M', 'P', '1', 'V')
  20383. +#define MMAL_ENCODING_WMV3 MMAL_FOURCC('W', 'M', 'V', '3')
  20384. +#define MMAL_ENCODING_WMV2 MMAL_FOURCC('W', 'M', 'V', '2')
  20385. +#define MMAL_ENCODING_WMV1 MMAL_FOURCC('W', 'M', 'V', '1')
  20386. +#define MMAL_ENCODING_WVC1 MMAL_FOURCC('W', 'V', 'C', '1')
  20387. +#define MMAL_ENCODING_VP8 MMAL_FOURCC('V', 'P', '8', ' ')
  20388. +#define MMAL_ENCODING_VP7 MMAL_FOURCC('V', 'P', '7', ' ')
  20389. +#define MMAL_ENCODING_VP6 MMAL_FOURCC('V', 'P', '6', ' ')
  20390. +#define MMAL_ENCODING_THEORA MMAL_FOURCC('T', 'H', 'E', 'O')
  20391. +#define MMAL_ENCODING_SPARK MMAL_FOURCC('S', 'P', 'R', 'K')
  20392. +#define MMAL_ENCODING_MJPEG MMAL_FOURCC('M', 'J', 'P', 'G')
  20393. +
  20394. +#define MMAL_ENCODING_JPEG MMAL_FOURCC('J', 'P', 'E', 'G')
  20395. +#define MMAL_ENCODING_GIF MMAL_FOURCC('G', 'I', 'F', ' ')
  20396. +#define MMAL_ENCODING_PNG MMAL_FOURCC('P', 'N', 'G', ' ')
  20397. +#define MMAL_ENCODING_PPM MMAL_FOURCC('P', 'P', 'M', ' ')
  20398. +#define MMAL_ENCODING_TGA MMAL_FOURCC('T', 'G', 'A', ' ')
  20399. +#define MMAL_ENCODING_BMP MMAL_FOURCC('B', 'M', 'P', ' ')
  20400. +
  20401. +#define MMAL_ENCODING_I420 MMAL_FOURCC('I', '4', '2', '0')
  20402. +#define MMAL_ENCODING_I420_SLICE MMAL_FOURCC('S', '4', '2', '0')
  20403. +#define MMAL_ENCODING_YV12 MMAL_FOURCC('Y', 'V', '1', '2')
  20404. +#define MMAL_ENCODING_I422 MMAL_FOURCC('I', '4', '2', '2')
  20405. +#define MMAL_ENCODING_I422_SLICE MMAL_FOURCC('S', '4', '2', '2')
  20406. +#define MMAL_ENCODING_YUYV MMAL_FOURCC('Y', 'U', 'Y', 'V')
  20407. +#define MMAL_ENCODING_YVYU MMAL_FOURCC('Y', 'V', 'Y', 'U')
  20408. +#define MMAL_ENCODING_UYVY MMAL_FOURCC('U', 'Y', 'V', 'Y')
  20409. +#define MMAL_ENCODING_VYUY MMAL_FOURCC('V', 'Y', 'U', 'Y')
  20410. +#define MMAL_ENCODING_NV12 MMAL_FOURCC('N', 'V', '1', '2')
  20411. +#define MMAL_ENCODING_NV21 MMAL_FOURCC('N', 'V', '2', '1')
  20412. +#define MMAL_ENCODING_ARGB MMAL_FOURCC('A', 'R', 'G', 'B')
  20413. +#define MMAL_ENCODING_RGBA MMAL_FOURCC('R', 'G', 'B', 'A')
  20414. +#define MMAL_ENCODING_ABGR MMAL_FOURCC('A', 'B', 'G', 'R')
  20415. +#define MMAL_ENCODING_BGRA MMAL_FOURCC('B', 'G', 'R', 'A')
  20416. +#define MMAL_ENCODING_RGB16 MMAL_FOURCC('R', 'G', 'B', '2')
  20417. +#define MMAL_ENCODING_RGB24 MMAL_FOURCC('R', 'G', 'B', '3')
  20418. +#define MMAL_ENCODING_RGB32 MMAL_FOURCC('R', 'G', 'B', '4')
  20419. +#define MMAL_ENCODING_BGR16 MMAL_FOURCC('B', 'G', 'R', '2')
  20420. +#define MMAL_ENCODING_BGR24 MMAL_FOURCC('B', 'G', 'R', '3')
  20421. +#define MMAL_ENCODING_BGR32 MMAL_FOURCC('B', 'G', 'R', '4')
  20422. +
  20423. +/** SAND Video (YUVUV128) format, native format understood by VideoCore.
  20424. + * This format is *not* opaque - if requested you will receive full frames
  20425. + * of YUV_UV video.
  20426. + */
  20427. +#define MMAL_ENCODING_YUVUV128 MMAL_FOURCC('S', 'A', 'N', 'D')
  20428. +
  20429. +/** VideoCore opaque image format, image handles are returned to
  20430. + * the host but not the actual image data.
  20431. + */
  20432. +#define MMAL_ENCODING_OPAQUE MMAL_FOURCC('O', 'P', 'Q', 'V')
  20433. +
  20434. +/** An EGL image handle
  20435. + */
  20436. +#define MMAL_ENCODING_EGL_IMAGE MMAL_FOURCC('E', 'G', 'L', 'I')
  20437. +
  20438. +/* }@ */
  20439. +
  20440. +/** \name Pre-defined audio encodings */
  20441. +/* @{ */
  20442. +#define MMAL_ENCODING_PCM_UNSIGNED_BE MMAL_FOURCC('P', 'C', 'M', 'U')
  20443. +#define MMAL_ENCODING_PCM_UNSIGNED_LE MMAL_FOURCC('p', 'c', 'm', 'u')
  20444. +#define MMAL_ENCODING_PCM_SIGNED_BE MMAL_FOURCC('P', 'C', 'M', 'S')
  20445. +#define MMAL_ENCODING_PCM_SIGNED_LE MMAL_FOURCC('p', 'c', 'm', 's')
  20446. +#define MMAL_ENCODING_PCM_FLOAT_BE MMAL_FOURCC('P', 'C', 'M', 'F')
  20447. +#define MMAL_ENCODING_PCM_FLOAT_LE MMAL_FOURCC('p', 'c', 'm', 'f')
  20448. +
  20449. +/* Pre-defined H264 encoding variants */
  20450. +
  20451. +/** ISO 14496-10 Annex B byte stream format */
  20452. +#define MMAL_ENCODING_VARIANT_H264_DEFAULT 0
  20453. +/** ISO 14496-15 AVC stream format */
  20454. +#define MMAL_ENCODING_VARIANT_H264_AVC1 MMAL_FOURCC('A', 'V', 'C', '1')
  20455. +/** Implicitly delineated NAL units without emulation prevention */
  20456. +#define MMAL_ENCODING_VARIANT_H264_RAW MMAL_FOURCC('R', 'A', 'W', ' ')
  20457. +
  20458. +
  20459. +/** \defgroup MmalColorSpace List of pre-defined video color spaces
  20460. + * This defines a list of common color spaces. This list isn't exhaustive and
  20461. + * is only provided as a convenience to avoid clients having to use FourCC
  20462. + * codes directly. However components are allowed to define and use their own
  20463. + * FourCC codes.
  20464. + */
  20465. +/* @{ */
  20466. +
  20467. +/** Unknown color space */
  20468. +#define MMAL_COLOR_SPACE_UNKNOWN 0
  20469. +/** ITU-R BT.601-5 [SDTV] */
  20470. +#define MMAL_COLOR_SPACE_ITUR_BT601 MMAL_FOURCC('Y', '6', '0', '1')
  20471. +/** ITU-R BT.709-3 [HDTV] */
  20472. +#define MMAL_COLOR_SPACE_ITUR_BT709 MMAL_FOURCC('Y', '7', '0', '9')
  20473. +/** JPEG JFIF */
  20474. +#define MMAL_COLOR_SPACE_JPEG_JFIF MMAL_FOURCC('Y', 'J', 'F', 'I')
  20475. +/** Title 47 Code of Federal Regulations (2003) 73.682 (a) (20) */
  20476. +#define MMAL_COLOR_SPACE_FCC MMAL_FOURCC('Y', 'F', 'C', 'C')
  20477. +/** Society of Motion Picture and Television Engineers 240M (1999) */
  20478. +#define MMAL_COLOR_SPACE_SMPTE240M MMAL_FOURCC('Y', '2', '4', '0')
  20479. +/** ITU-R BT.470-2 System M */
  20480. +#define MMAL_COLOR_SPACE_BT470_2_M MMAL_FOURCC('Y', '_', '_', 'M')
  20481. +/** ITU-R BT.470-2 System BG */
  20482. +#define MMAL_COLOR_SPACE_BT470_2_BG MMAL_FOURCC('Y', '_', 'B', 'G')
  20483. +/** JPEG JFIF, but with 16..255 luma */
  20484. +#define MMAL_COLOR_SPACE_JFIF_Y16_255 MMAL_FOURCC('Y', 'Y', '1', '6')
  20485. +/* @} MmalColorSpace List */
  20486. +
  20487. +#endif /* MMAL_ENCODINGS_H */
  20488. diff -Nur linux-3.12.33/drivers/media/platform/bcm2835/mmal-msg-common.h linux-3.12.33-rpi/drivers/media/platform/bcm2835/mmal-msg-common.h
  20489. --- linux-3.12.33/drivers/media/platform/bcm2835/mmal-msg-common.h 1969-12-31 18:00:00.000000000 -0600
  20490. +++ linux-3.12.33-rpi/drivers/media/platform/bcm2835/mmal-msg-common.h 2014-12-03 19:13:38.012418001 -0600
  20491. @@ -0,0 +1,50 @@
  20492. +/*
  20493. + * Broadcom BM2835 V4L2 driver
  20494. + *
  20495. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  20496. + *
  20497. + * This file is subject to the terms and conditions of the GNU General Public
  20498. + * License. See the file COPYING in the main directory of this archive
  20499. + * for more details.
  20500. + *
  20501. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  20502. + * Dave Stevenson <dsteve@broadcom.com>
  20503. + * Simon Mellor <simellor@broadcom.com>
  20504. + * Luke Diamand <luked@broadcom.com>
  20505. + */
  20506. +
  20507. +#ifndef MMAL_MSG_COMMON_H
  20508. +#define MMAL_MSG_COMMON_H
  20509. +
  20510. +enum mmal_msg_status {
  20511. + MMAL_MSG_STATUS_SUCCESS = 0, /**< Success */
  20512. + MMAL_MSG_STATUS_ENOMEM, /**< Out of memory */
  20513. + MMAL_MSG_STATUS_ENOSPC, /**< Out of resources other than memory */
  20514. + MMAL_MSG_STATUS_EINVAL, /**< Argument is invalid */
  20515. + MMAL_MSG_STATUS_ENOSYS, /**< Function not implemented */
  20516. + MMAL_MSG_STATUS_ENOENT, /**< No such file or directory */
  20517. + MMAL_MSG_STATUS_ENXIO, /**< No such device or address */
  20518. + MMAL_MSG_STATUS_EIO, /**< I/O error */
  20519. + MMAL_MSG_STATUS_ESPIPE, /**< Illegal seek */
  20520. + MMAL_MSG_STATUS_ECORRUPT, /**< Data is corrupt \attention */
  20521. + MMAL_MSG_STATUS_ENOTREADY, /**< Component is not ready */
  20522. + MMAL_MSG_STATUS_ECONFIG, /**< Component is not configured */
  20523. + MMAL_MSG_STATUS_EISCONN, /**< Port is already connected */
  20524. + MMAL_MSG_STATUS_ENOTCONN, /**< Port is disconnected */
  20525. + MMAL_MSG_STATUS_EAGAIN, /**< Resource temporarily unavailable. */
  20526. + MMAL_MSG_STATUS_EFAULT, /**< Bad address */
  20527. +};
  20528. +
  20529. +struct mmal_rect {
  20530. + s32 x; /**< x coordinate (from left) */
  20531. + s32 y; /**< y coordinate (from top) */
  20532. + s32 width; /**< width */
  20533. + s32 height; /**< height */
  20534. +};
  20535. +
  20536. +struct mmal_rational {
  20537. + s32 num; /**< Numerator */
  20538. + s32 den; /**< Denominator */
  20539. +};
  20540. +
  20541. +#endif /* MMAL_MSG_COMMON_H */
  20542. diff -Nur linux-3.12.33/drivers/media/platform/bcm2835/mmal-msg-format.h linux-3.12.33-rpi/drivers/media/platform/bcm2835/mmal-msg-format.h
  20543. --- linux-3.12.33/drivers/media/platform/bcm2835/mmal-msg-format.h 1969-12-31 18:00:00.000000000 -0600
  20544. +++ linux-3.12.33-rpi/drivers/media/platform/bcm2835/mmal-msg-format.h 2014-12-03 19:13:38.012418001 -0600
  20545. @@ -0,0 +1,81 @@
  20546. +/*
  20547. + * Broadcom BM2835 V4L2 driver
  20548. + *
  20549. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  20550. + *
  20551. + * This file is subject to the terms and conditions of the GNU General Public
  20552. + * License. See the file COPYING in the main directory of this archive
  20553. + * for more details.
  20554. + *
  20555. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  20556. + * Dave Stevenson <dsteve@broadcom.com>
  20557. + * Simon Mellor <simellor@broadcom.com>
  20558. + * Luke Diamand <luked@broadcom.com>
  20559. + */
  20560. +
  20561. +#ifndef MMAL_MSG_FORMAT_H
  20562. +#define MMAL_MSG_FORMAT_H
  20563. +
  20564. +#include "mmal-msg-common.h"
  20565. +
  20566. +/* MMAL_ES_FORMAT_T */
  20567. +
  20568. +
  20569. +struct mmal_audio_format {
  20570. + u32 channels; /**< Number of audio channels */
  20571. + u32 sample_rate; /**< Sample rate */
  20572. +
  20573. + u32 bits_per_sample; /**< Bits per sample */
  20574. + u32 block_align; /**< Size of a block of data */
  20575. +};
  20576. +
  20577. +struct mmal_video_format {
  20578. + u32 width; /**< Width of frame in pixels */
  20579. + u32 height; /**< Height of frame in rows of pixels */
  20580. + struct mmal_rect crop; /**< Visible region of the frame */
  20581. + struct mmal_rational frame_rate; /**< Frame rate */
  20582. + struct mmal_rational par; /**< Pixel aspect ratio */
  20583. +
  20584. + /* FourCC specifying the color space of the video stream. See the
  20585. + * \ref MmalColorSpace "pre-defined color spaces" for some examples.
  20586. + */
  20587. + u32 color_space;
  20588. +};
  20589. +
  20590. +struct mmal_subpicture_format {
  20591. + u32 x_offset;
  20592. + u32 y_offset;
  20593. +};
  20594. +
  20595. +union mmal_es_specific_format {
  20596. + struct mmal_audio_format audio;
  20597. + struct mmal_video_format video;
  20598. + struct mmal_subpicture_format subpicture;
  20599. +};
  20600. +
  20601. +/** Definition of an elementary stream format (MMAL_ES_FORMAT_T) */
  20602. +struct mmal_es_format {
  20603. + u32 type; /* enum mmal_es_type */
  20604. +
  20605. + u32 encoding; /* FourCC specifying encoding of the elementary stream.*/
  20606. + u32 encoding_variant; /* FourCC specifying the specific
  20607. + * encoding variant of the elementary
  20608. + * stream.
  20609. + */
  20610. +
  20611. + union mmal_es_specific_format *es; /* TODO: pointers in
  20612. + * message serialisation?!?
  20613. + */
  20614. + /* Type specific
  20615. + * information for the
  20616. + * elementary stream
  20617. + */
  20618. +
  20619. + u32 bitrate; /**< Bitrate in bits per second */
  20620. + u32 flags; /**< Flags describing properties of the elementary stream. */
  20621. +
  20622. + u32 extradata_size; /**< Size of the codec specific data */
  20623. + u8 *extradata; /**< Codec specific data */
  20624. +};
  20625. +
  20626. +#endif /* MMAL_MSG_FORMAT_H */
  20627. diff -Nur linux-3.12.33/drivers/media/platform/bcm2835/mmal-msg.h linux-3.12.33-rpi/drivers/media/platform/bcm2835/mmal-msg.h
  20628. --- linux-3.12.33/drivers/media/platform/bcm2835/mmal-msg.h 1969-12-31 18:00:00.000000000 -0600
  20629. +++ linux-3.12.33-rpi/drivers/media/platform/bcm2835/mmal-msg.h 2014-12-03 19:13:38.012418001 -0600
  20630. @@ -0,0 +1,404 @@
  20631. +/*
  20632. + * Broadcom BM2835 V4L2 driver
  20633. + *
  20634. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  20635. + *
  20636. + * This file is subject to the terms and conditions of the GNU General Public
  20637. + * License. See the file COPYING in the main directory of this archive
  20638. + * for more details.
  20639. + *
  20640. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  20641. + * Dave Stevenson <dsteve@broadcom.com>
  20642. + * Simon Mellor <simellor@broadcom.com>
  20643. + * Luke Diamand <luked@broadcom.com>
  20644. + */
  20645. +
  20646. +/* all the data structures which serialise the MMAL protocol. note
  20647. + * these are directly mapped onto the recived message data.
  20648. + *
  20649. + * BEWARE: They seem to *assume* pointers are u32 and that there is no
  20650. + * structure padding!
  20651. + *
  20652. + * NOTE: this implementation uses kernel types to ensure sizes. Rather
  20653. + * than assigning values to enums to force their size the
  20654. + * implementation uses fixed size types and not the enums (though the
  20655. + * comments have the actual enum type
  20656. + */
  20657. +
  20658. +#define VC_MMAL_VER 15
  20659. +#define VC_MMAL_MIN_VER 10
  20660. +#define VC_MMAL_SERVER_NAME MAKE_FOURCC("mmal")
  20661. +
  20662. +/* max total message size is 512 bytes */
  20663. +#define MMAL_MSG_MAX_SIZE 512
  20664. +/* with six 32bit header elements max payload is therefore 488 bytes */
  20665. +#define MMAL_MSG_MAX_PAYLOAD 488
  20666. +
  20667. +#include "mmal-msg-common.h"
  20668. +#include "mmal-msg-format.h"
  20669. +#include "mmal-msg-port.h"
  20670. +
  20671. +enum mmal_msg_type {
  20672. + MMAL_MSG_TYPE_QUIT = 1,
  20673. + MMAL_MSG_TYPE_SERVICE_CLOSED,
  20674. + MMAL_MSG_TYPE_GET_VERSION,
  20675. + MMAL_MSG_TYPE_COMPONENT_CREATE,
  20676. + MMAL_MSG_TYPE_COMPONENT_DESTROY, /* 5 */
  20677. + MMAL_MSG_TYPE_COMPONENT_ENABLE,
  20678. + MMAL_MSG_TYPE_COMPONENT_DISABLE,
  20679. + MMAL_MSG_TYPE_PORT_INFO_GET,
  20680. + MMAL_MSG_TYPE_PORT_INFO_SET,
  20681. + MMAL_MSG_TYPE_PORT_ACTION, /* 10 */
  20682. + MMAL_MSG_TYPE_BUFFER_FROM_HOST,
  20683. + MMAL_MSG_TYPE_BUFFER_TO_HOST,
  20684. + MMAL_MSG_TYPE_GET_STATS,
  20685. + MMAL_MSG_TYPE_PORT_PARAMETER_SET,
  20686. + MMAL_MSG_TYPE_PORT_PARAMETER_GET, /* 15 */
  20687. + MMAL_MSG_TYPE_EVENT_TO_HOST,
  20688. + MMAL_MSG_TYPE_GET_CORE_STATS_FOR_PORT,
  20689. + MMAL_MSG_TYPE_OPAQUE_ALLOCATOR,
  20690. + MMAL_MSG_TYPE_CONSUME_MEM,
  20691. + MMAL_MSG_TYPE_LMK, /* 20 */
  20692. + MMAL_MSG_TYPE_OPAQUE_ALLOCATOR_DESC,
  20693. + MMAL_MSG_TYPE_DRM_GET_LHS32,
  20694. + MMAL_MSG_TYPE_DRM_GET_TIME,
  20695. + MMAL_MSG_TYPE_BUFFER_FROM_HOST_ZEROLEN,
  20696. + MMAL_MSG_TYPE_PORT_FLUSH, /* 25 */
  20697. + MMAL_MSG_TYPE_HOST_LOG,
  20698. + MMAL_MSG_TYPE_MSG_LAST
  20699. +};
  20700. +
  20701. +/* port action request messages differ depending on the action type */
  20702. +enum mmal_msg_port_action_type {
  20703. + MMAL_MSG_PORT_ACTION_TYPE_UNKNOWN = 0, /* Unkown action */
  20704. + MMAL_MSG_PORT_ACTION_TYPE_ENABLE, /* Enable a port */
  20705. + MMAL_MSG_PORT_ACTION_TYPE_DISABLE, /* Disable a port */
  20706. + MMAL_MSG_PORT_ACTION_TYPE_FLUSH, /* Flush a port */
  20707. + MMAL_MSG_PORT_ACTION_TYPE_CONNECT, /* Connect ports */
  20708. + MMAL_MSG_PORT_ACTION_TYPE_DISCONNECT, /* Disconnect ports */
  20709. + MMAL_MSG_PORT_ACTION_TYPE_SET_REQUIREMENTS, /* Set buffer requirements*/
  20710. +};
  20711. +
  20712. +struct mmal_msg_header {
  20713. + u32 magic;
  20714. + u32 type; /** enum mmal_msg_type */
  20715. +
  20716. + /* Opaque handle to the control service */
  20717. + struct mmal_control_service *control_service;
  20718. +
  20719. + struct mmal_msg_context *context; /** a u32 per message context */
  20720. + u32 status; /** The status of the vchiq operation */
  20721. + u32 padding;
  20722. +};
  20723. +
  20724. +/* Send from VC to host to report version */
  20725. +struct mmal_msg_version {
  20726. + u32 flags;
  20727. + u32 major;
  20728. + u32 minor;
  20729. + u32 minimum;
  20730. +};
  20731. +
  20732. +/* request to VC to create component */
  20733. +struct mmal_msg_component_create {
  20734. + void *client_component; /* component context */
  20735. + char name[128];
  20736. + u32 pid; /* For debug */
  20737. +};
  20738. +
  20739. +/* reply from VC to component creation request */
  20740. +struct mmal_msg_component_create_reply {
  20741. + u32 status; /** enum mmal_msg_status - how does this differ to
  20742. + * the one in the header?
  20743. + */
  20744. + u32 component_handle; /* VideoCore handle for component */
  20745. + u32 input_num; /* Number of input ports */
  20746. + u32 output_num; /* Number of output ports */
  20747. + u32 clock_num; /* Number of clock ports */
  20748. +};
  20749. +
  20750. +/* request to VC to destroy a component */
  20751. +struct mmal_msg_component_destroy {
  20752. + u32 component_handle;
  20753. +};
  20754. +
  20755. +struct mmal_msg_component_destroy_reply {
  20756. + u32 status; /** The component destruction status */
  20757. +};
  20758. +
  20759. +
  20760. +/* request and reply to VC to enable a component */
  20761. +struct mmal_msg_component_enable {
  20762. + u32 component_handle;
  20763. +};
  20764. +
  20765. +struct mmal_msg_component_enable_reply {
  20766. + u32 status; /** The component enable status */
  20767. +};
  20768. +
  20769. +
  20770. +/* request and reply to VC to disable a component */
  20771. +struct mmal_msg_component_disable {
  20772. + u32 component_handle;
  20773. +};
  20774. +
  20775. +struct mmal_msg_component_disable_reply {
  20776. + u32 status; /** The component disable status */
  20777. +};
  20778. +
  20779. +/* request to VC to get port information */
  20780. +struct mmal_msg_port_info_get {
  20781. + u32 component_handle; /* component handle port is associated with */
  20782. + u32 port_type; /* enum mmal_msg_port_type */
  20783. + u32 index; /* port index to query */
  20784. +};
  20785. +
  20786. +/* reply from VC to get port info request */
  20787. +struct mmal_msg_port_info_get_reply {
  20788. + u32 status; /** enum mmal_msg_status */
  20789. + u32 component_handle; /* component handle port is associated with */
  20790. + u32 port_type; /* enum mmal_msg_port_type */
  20791. + u32 port_index; /* port indexed in query */
  20792. + s32 found; /* unused */
  20793. + u32 port_handle; /**< Handle to use for this port */
  20794. + struct mmal_port port;
  20795. + struct mmal_es_format format; /* elementry stream format */
  20796. + union mmal_es_specific_format es; /* es type specific data */
  20797. + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE]; /* es extra data */
  20798. +};
  20799. +
  20800. +/* request to VC to set port information */
  20801. +struct mmal_msg_port_info_set {
  20802. + u32 component_handle;
  20803. + u32 port_type; /* enum mmal_msg_port_type */
  20804. + u32 port_index; /* port indexed in query */
  20805. + struct mmal_port port;
  20806. + struct mmal_es_format format;
  20807. + union mmal_es_specific_format es;
  20808. + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE];
  20809. +};
  20810. +
  20811. +/* reply from VC to port info set request */
  20812. +struct mmal_msg_port_info_set_reply {
  20813. + u32 status;
  20814. + u32 component_handle; /* component handle port is associated with */
  20815. + u32 port_type; /* enum mmal_msg_port_type */
  20816. + u32 index; /* port indexed in query */
  20817. + s32 found; /* unused */
  20818. + u32 port_handle; /**< Handle to use for this port */
  20819. + struct mmal_port port;
  20820. + struct mmal_es_format format;
  20821. + union mmal_es_specific_format es;
  20822. + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE];
  20823. +};
  20824. +
  20825. +
  20826. +/* port action requests that take a mmal_port as a parameter */
  20827. +struct mmal_msg_port_action_port {
  20828. + u32 component_handle;
  20829. + u32 port_handle;
  20830. + u32 action; /* enum mmal_msg_port_action_type */
  20831. + struct mmal_port port;
  20832. +};
  20833. +
  20834. +/* port action requests that take handles as a parameter */
  20835. +struct mmal_msg_port_action_handle {
  20836. + u32 component_handle;
  20837. + u32 port_handle;
  20838. + u32 action; /* enum mmal_msg_port_action_type */
  20839. + u32 connect_component_handle;
  20840. + u32 connect_port_handle;
  20841. +};
  20842. +
  20843. +struct mmal_msg_port_action_reply {
  20844. + u32 status; /** The port action operation status */
  20845. +};
  20846. +
  20847. +
  20848. +
  20849. +
  20850. +/* MMAL buffer transfer */
  20851. +
  20852. +/** Size of space reserved in a buffer message for short messages. */
  20853. +#define MMAL_VC_SHORT_DATA 128
  20854. +
  20855. +/** Signals that the current payload is the end of the stream of data */
  20856. +#define MMAL_BUFFER_HEADER_FLAG_EOS (1<<0)
  20857. +/** Signals that the start of the current payload starts a frame */
  20858. +#define MMAL_BUFFER_HEADER_FLAG_FRAME_START (1<<1)
  20859. +/** Signals that the end of the current payload ends a frame */
  20860. +#define MMAL_BUFFER_HEADER_FLAG_FRAME_END (1<<2)
  20861. +/** Signals that the current payload contains only complete frames (>1) */
  20862. +#define MMAL_BUFFER_HEADER_FLAG_FRAME \
  20863. + (MMAL_BUFFER_HEADER_FLAG_FRAME_START|MMAL_BUFFER_HEADER_FLAG_FRAME_END)
  20864. +/** Signals that the current payload is a keyframe (i.e. self decodable) */
  20865. +#define MMAL_BUFFER_HEADER_FLAG_KEYFRAME (1<<3)
  20866. +/** Signals a discontinuity in the stream of data (e.g. after a seek).
  20867. + * Can be used for instance by a decoder to reset its state */
  20868. +#define MMAL_BUFFER_HEADER_FLAG_DISCONTINUITY (1<<4)
  20869. +/** Signals a buffer containing some kind of config data for the component
  20870. + * (e.g. codec config data) */
  20871. +#define MMAL_BUFFER_HEADER_FLAG_CONFIG (1<<5)
  20872. +/** Signals an encrypted payload */
  20873. +#define MMAL_BUFFER_HEADER_FLAG_ENCRYPTED (1<<6)
  20874. +/** Signals a buffer containing side information */
  20875. +#define MMAL_BUFFER_HEADER_FLAG_CODECSIDEINFO (1<<7)
  20876. +/** Signals a buffer which is the snapshot/postview image from a stills
  20877. + * capture
  20878. + */
  20879. +#define MMAL_BUFFER_HEADER_FLAGS_SNAPSHOT (1<<8)
  20880. +/** Signals a buffer which contains data known to be corrupted */
  20881. +#define MMAL_BUFFER_HEADER_FLAG_CORRUPTED (1<<9)
  20882. +/** Signals that a buffer failed to be transmitted */
  20883. +#define MMAL_BUFFER_HEADER_FLAG_TRANSMISSION_FAILED (1<<10)
  20884. +
  20885. +struct mmal_driver_buffer {
  20886. + u32 magic;
  20887. + u32 component_handle;
  20888. + u32 port_handle;
  20889. + void *client_context;
  20890. +};
  20891. +
  20892. +/* buffer header */
  20893. +struct mmal_buffer_header {
  20894. + struct mmal_buffer_header *next; /* next header */
  20895. + void *priv; /* framework private data */
  20896. + u32 cmd;
  20897. + void *data;
  20898. + u32 alloc_size;
  20899. + u32 length;
  20900. + u32 offset;
  20901. + u32 flags;
  20902. + s64 pts;
  20903. + s64 dts;
  20904. + void *type;
  20905. + void *user_data;
  20906. +};
  20907. +
  20908. +struct mmal_buffer_header_type_specific {
  20909. + union {
  20910. + struct {
  20911. + u32 planes;
  20912. + u32 offset[4];
  20913. + u32 pitch[4];
  20914. + u32 flags;
  20915. + } video;
  20916. + } u;
  20917. +};
  20918. +
  20919. +struct mmal_msg_buffer_from_host {
  20920. + /* The front 32 bytes of the buffer header are copied
  20921. + * back to us in the reply to allow for context. This
  20922. + * area is used to store two mmal_driver_buffer structures to
  20923. + * allow for multiple concurrent service users.
  20924. + */
  20925. + /* control data */
  20926. + struct mmal_driver_buffer drvbuf;
  20927. +
  20928. + /* referenced control data for passthrough buffer management */
  20929. + struct mmal_driver_buffer drvbuf_ref;
  20930. + struct mmal_buffer_header buffer_header; /* buffer header itself */
  20931. + struct mmal_buffer_header_type_specific buffer_header_type_specific;
  20932. + s32 is_zero_copy;
  20933. + s32 has_reference;
  20934. +
  20935. + /** allows short data to be xfered in control message */
  20936. + u32 payload_in_message;
  20937. + u8 short_data[MMAL_VC_SHORT_DATA];
  20938. +};
  20939. +
  20940. +
  20941. +/* port parameter setting */
  20942. +
  20943. +#define MMAL_WORKER_PORT_PARAMETER_SPACE 96
  20944. +
  20945. +struct mmal_msg_port_parameter_set {
  20946. + u32 component_handle; /* component */
  20947. + u32 port_handle; /* port */
  20948. + u32 id; /* Parameter ID */
  20949. + u32 size; /* Parameter size */
  20950. + uint32_t value[MMAL_WORKER_PORT_PARAMETER_SPACE];
  20951. +};
  20952. +
  20953. +struct mmal_msg_port_parameter_set_reply {
  20954. + u32 status; /** enum mmal_msg_status todo: how does this
  20955. + * differ to the one in the header?
  20956. + */
  20957. +};
  20958. +
  20959. +/* port parameter getting */
  20960. +
  20961. +struct mmal_msg_port_parameter_get {
  20962. + u32 component_handle; /* component */
  20963. + u32 port_handle; /* port */
  20964. + u32 id; /* Parameter ID */
  20965. + u32 size; /* Parameter size */
  20966. +};
  20967. +
  20968. +struct mmal_msg_port_parameter_get_reply {
  20969. + u32 status; /* Status of mmal_port_parameter_get call */
  20970. + u32 id; /* Parameter ID */
  20971. + u32 size; /* Parameter size */
  20972. + uint32_t value[MMAL_WORKER_PORT_PARAMETER_SPACE];
  20973. +};
  20974. +
  20975. +/* event messages */
  20976. +#define MMAL_WORKER_EVENT_SPACE 256
  20977. +
  20978. +struct mmal_msg_event_to_host {
  20979. + void *client_component; /* component context */
  20980. +
  20981. + u32 port_type;
  20982. + u32 port_num;
  20983. +
  20984. + u32 cmd;
  20985. + u32 length;
  20986. + u8 data[MMAL_WORKER_EVENT_SPACE];
  20987. + struct mmal_buffer_header *delayed_buffer;
  20988. +};
  20989. +
  20990. +/* all mmal messages are serialised through this structure */
  20991. +struct mmal_msg {
  20992. + /* header */
  20993. + struct mmal_msg_header h;
  20994. + /* payload */
  20995. + union {
  20996. + struct mmal_msg_version version;
  20997. +
  20998. + struct mmal_msg_component_create component_create;
  20999. + struct mmal_msg_component_create_reply component_create_reply;
  21000. +
  21001. + struct mmal_msg_component_destroy component_destroy;
  21002. + struct mmal_msg_component_destroy_reply component_destroy_reply;
  21003. +
  21004. + struct mmal_msg_component_enable component_enable;
  21005. + struct mmal_msg_component_enable_reply component_enable_reply;
  21006. +
  21007. + struct mmal_msg_component_disable component_disable;
  21008. + struct mmal_msg_component_disable_reply component_disable_reply;
  21009. +
  21010. + struct mmal_msg_port_info_get port_info_get;
  21011. + struct mmal_msg_port_info_get_reply port_info_get_reply;
  21012. +
  21013. + struct mmal_msg_port_info_set port_info_set;
  21014. + struct mmal_msg_port_info_set_reply port_info_set_reply;
  21015. +
  21016. + struct mmal_msg_port_action_port port_action_port;
  21017. + struct mmal_msg_port_action_handle port_action_handle;
  21018. + struct mmal_msg_port_action_reply port_action_reply;
  21019. +
  21020. + struct mmal_msg_buffer_from_host buffer_from_host;
  21021. +
  21022. + struct mmal_msg_port_parameter_set port_parameter_set;
  21023. + struct mmal_msg_port_parameter_set_reply
  21024. + port_parameter_set_reply;
  21025. + struct mmal_msg_port_parameter_get
  21026. + port_parameter_get;
  21027. + struct mmal_msg_port_parameter_get_reply
  21028. + port_parameter_get_reply;
  21029. +
  21030. + struct mmal_msg_event_to_host event_to_host;
  21031. +
  21032. + u8 payload[MMAL_MSG_MAX_PAYLOAD];
  21033. + } u;
  21034. +};
  21035. diff -Nur linux-3.12.33/drivers/media/platform/bcm2835/mmal-msg-port.h linux-3.12.33-rpi/drivers/media/platform/bcm2835/mmal-msg-port.h
  21036. --- linux-3.12.33/drivers/media/platform/bcm2835/mmal-msg-port.h 1969-12-31 18:00:00.000000000 -0600
  21037. +++ linux-3.12.33-rpi/drivers/media/platform/bcm2835/mmal-msg-port.h 2014-12-03 19:13:38.012418001 -0600
  21038. @@ -0,0 +1,107 @@
  21039. +/*
  21040. + * Broadcom BM2835 V4L2 driver
  21041. + *
  21042. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  21043. + *
  21044. + * This file is subject to the terms and conditions of the GNU General Public
  21045. + * License. See the file COPYING in the main directory of this archive
  21046. + * for more details.
  21047. + *
  21048. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  21049. + * Dave Stevenson <dsteve@broadcom.com>
  21050. + * Simon Mellor <simellor@broadcom.com>
  21051. + * Luke Diamand <luked@broadcom.com>
  21052. + */
  21053. +
  21054. +/* MMAL_PORT_TYPE_T */
  21055. +enum mmal_port_type {
  21056. + MMAL_PORT_TYPE_UNKNOWN = 0, /**< Unknown port type */
  21057. + MMAL_PORT_TYPE_CONTROL, /**< Control port */
  21058. + MMAL_PORT_TYPE_INPUT, /**< Input port */
  21059. + MMAL_PORT_TYPE_OUTPUT, /**< Output port */
  21060. + MMAL_PORT_TYPE_CLOCK, /**< Clock port */
  21061. +};
  21062. +
  21063. +/** The port is pass-through and doesn't need buffer headers allocated */
  21064. +#define MMAL_PORT_CAPABILITY_PASSTHROUGH 0x01
  21065. +/** The port wants to allocate the buffer payloads.
  21066. + * This signals a preference that payload allocation should be done
  21067. + * on this port for efficiency reasons. */
  21068. +#define MMAL_PORT_CAPABILITY_ALLOCATION 0x02
  21069. +/** The port supports format change events.
  21070. + * This applies to input ports and is used to let the client know
  21071. + * whether the port supports being reconfigured via a format
  21072. + * change event (i.e. without having to disable the port). */
  21073. +#define MMAL_PORT_CAPABILITY_SUPPORTS_EVENT_FORMAT_CHANGE 0x04
  21074. +
  21075. +/* mmal port structure (MMAL_PORT_T)
  21076. + *
  21077. + * most elements are informational only, the pointer values for
  21078. + * interogation messages are generally provided as additional
  21079. + * strucures within the message. When used to set values only teh
  21080. + * buffer_num, buffer_size and userdata parameters are writable.
  21081. + */
  21082. +struct mmal_port {
  21083. + void *priv; /* Private member used by the framework */
  21084. + const char *name; /* Port name. Used for debugging purposes (RO) */
  21085. +
  21086. + u32 type; /* Type of the port (RO) enum mmal_port_type */
  21087. + u16 index; /* Index of the port in its type list (RO) */
  21088. + u16 index_all; /* Index of the port in the list of all ports (RO) */
  21089. +
  21090. + u32 is_enabled; /* Indicates whether the port is enabled or not (RO) */
  21091. + struct mmal_es_format *format; /* Format of the elementary stream */
  21092. +
  21093. + u32 buffer_num_min; /* Minimum number of buffers the port
  21094. + * requires (RO). This is set by the
  21095. + * component.
  21096. + */
  21097. +
  21098. + u32 buffer_size_min; /* Minimum size of buffers the port
  21099. + * requires (RO). This is set by the
  21100. + * component.
  21101. + */
  21102. +
  21103. + u32 buffer_alignment_min; /* Minimum alignment requirement for
  21104. + * the buffers (RO). A value of
  21105. + * zero means no special alignment
  21106. + * requirements. This is set by the
  21107. + * component.
  21108. + */
  21109. +
  21110. + u32 buffer_num_recommended; /* Number of buffers the port
  21111. + * recommends for optimal
  21112. + * performance (RO). A value of
  21113. + * zero means no special
  21114. + * recommendation. This is set
  21115. + * by the component.
  21116. + */
  21117. +
  21118. + u32 buffer_size_recommended; /* Size of buffers the port
  21119. + * recommends for optimal
  21120. + * performance (RO). A value of
  21121. + * zero means no special
  21122. + * recommendation. This is set
  21123. + * by the component.
  21124. + */
  21125. +
  21126. + u32 buffer_num; /* Actual number of buffers the port will use.
  21127. + * This is set by the client.
  21128. + */
  21129. +
  21130. + u32 buffer_size; /* Actual maximum size of the buffers that
  21131. + * will be sent to the port. This is set by
  21132. + * the client.
  21133. + */
  21134. +
  21135. + void *component; /* Component this port belongs to (Read Only) */
  21136. +
  21137. + void *userdata; /* Field reserved for use by the client */
  21138. +
  21139. + u32 capabilities; /* Flags describing the capabilities of a
  21140. + * port (RO). Bitwise combination of \ref
  21141. + * portcapabilities "Port capabilities"
  21142. + * values.
  21143. + */
  21144. +
  21145. +};
  21146. diff -Nur linux-3.12.33/drivers/media/platform/bcm2835/mmal-parameters.h linux-3.12.33-rpi/drivers/media/platform/bcm2835/mmal-parameters.h
  21147. --- linux-3.12.33/drivers/media/platform/bcm2835/mmal-parameters.h 1969-12-31 18:00:00.000000000 -0600
  21148. +++ linux-3.12.33-rpi/drivers/media/platform/bcm2835/mmal-parameters.h 2014-12-03 19:13:38.012418001 -0600
  21149. @@ -0,0 +1,656 @@
  21150. +/*
  21151. + * Broadcom BM2835 V4L2 driver
  21152. + *
  21153. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  21154. + *
  21155. + * This file is subject to the terms and conditions of the GNU General Public
  21156. + * License. See the file COPYING in the main directory of this archive
  21157. + * for more details.
  21158. + *
  21159. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  21160. + * Dave Stevenson <dsteve@broadcom.com>
  21161. + * Simon Mellor <simellor@broadcom.com>
  21162. + * Luke Diamand <luked@broadcom.com>
  21163. + */
  21164. +
  21165. +/* common parameters */
  21166. +
  21167. +/** @name Parameter groups
  21168. + * Parameters are divided into groups, and then allocated sequentially within
  21169. + * a group using an enum.
  21170. + * @{
  21171. + */
  21172. +
  21173. +/** Common parameter ID group, used with many types of component. */
  21174. +#define MMAL_PARAMETER_GROUP_COMMON (0<<16)
  21175. +/** Camera-specific parameter ID group. */
  21176. +#define MMAL_PARAMETER_GROUP_CAMERA (1<<16)
  21177. +/** Video-specific parameter ID group. */
  21178. +#define MMAL_PARAMETER_GROUP_VIDEO (2<<16)
  21179. +/** Audio-specific parameter ID group. */
  21180. +#define MMAL_PARAMETER_GROUP_AUDIO (3<<16)
  21181. +/** Clock-specific parameter ID group. */
  21182. +#define MMAL_PARAMETER_GROUP_CLOCK (4<<16)
  21183. +/** Miracast-specific parameter ID group. */
  21184. +#define MMAL_PARAMETER_GROUP_MIRACAST (5<<16)
  21185. +
  21186. +/* Common parameters */
  21187. +enum mmal_parameter_common_type {
  21188. + MMAL_PARAMETER_UNUSED /**< Never a valid parameter ID */
  21189. + = MMAL_PARAMETER_GROUP_COMMON,
  21190. + MMAL_PARAMETER_SUPPORTED_ENCODINGS, /**< MMAL_PARAMETER_ENCODING_T */
  21191. + MMAL_PARAMETER_URI, /**< MMAL_PARAMETER_URI_T */
  21192. +
  21193. + /** MMAL_PARAMETER_CHANGE_EVENT_REQUEST_T */
  21194. + MMAL_PARAMETER_CHANGE_EVENT_REQUEST,
  21195. +
  21196. + /** MMAL_PARAMETER_BOOLEAN_T */
  21197. + MMAL_PARAMETER_ZERO_COPY,
  21198. +
  21199. + /**< MMAL_PARAMETER_BUFFER_REQUIREMENTS_T */
  21200. + MMAL_PARAMETER_BUFFER_REQUIREMENTS,
  21201. +
  21202. + MMAL_PARAMETER_STATISTICS, /**< MMAL_PARAMETER_STATISTICS_T */
  21203. + MMAL_PARAMETER_CORE_STATISTICS, /**< MMAL_PARAMETER_CORE_STATISTICS_T */
  21204. + MMAL_PARAMETER_MEM_USAGE, /**< MMAL_PARAMETER_MEM_USAGE_T */
  21205. + MMAL_PARAMETER_BUFFER_FLAG_FILTER, /**< MMAL_PARAMETER_UINT32_T */
  21206. + MMAL_PARAMETER_SEEK, /**< MMAL_PARAMETER_SEEK_T */
  21207. + MMAL_PARAMETER_POWERMON_ENABLE, /**< MMAL_PARAMETER_BOOLEAN_T */
  21208. + MMAL_PARAMETER_LOGGING, /**< MMAL_PARAMETER_LOGGING_T */
  21209. + MMAL_PARAMETER_SYSTEM_TIME, /**< MMAL_PARAMETER_UINT64_T */
  21210. + MMAL_PARAMETER_NO_IMAGE_PADDING /**< MMAL_PARAMETER_BOOLEAN_T */
  21211. +};
  21212. +
  21213. +/* camera parameters */
  21214. +
  21215. +enum mmal_parameter_camera_type {
  21216. + /* 0 */
  21217. + /** @ref MMAL_PARAMETER_THUMBNAIL_CONFIG_T */
  21218. + MMAL_PARAMETER_THUMBNAIL_CONFIGURATION
  21219. + = MMAL_PARAMETER_GROUP_CAMERA,
  21220. + MMAL_PARAMETER_CAPTURE_QUALITY, /**< Unused? */
  21221. + MMAL_PARAMETER_ROTATION, /**< @ref MMAL_PARAMETER_INT32_T */
  21222. + MMAL_PARAMETER_EXIF_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21223. + MMAL_PARAMETER_EXIF, /**< @ref MMAL_PARAMETER_EXIF_T */
  21224. + MMAL_PARAMETER_AWB_MODE, /**< @ref MMAL_PARAM_AWBMODE_T */
  21225. + MMAL_PARAMETER_IMAGE_EFFECT, /**< @ref MMAL_PARAMETER_IMAGEFX_T */
  21226. + MMAL_PARAMETER_COLOUR_EFFECT, /**< @ref MMAL_PARAMETER_COLOURFX_T */
  21227. + MMAL_PARAMETER_FLICKER_AVOID, /**< @ref MMAL_PARAMETER_FLICKERAVOID_T */
  21228. + MMAL_PARAMETER_FLASH, /**< @ref MMAL_PARAMETER_FLASH_T */
  21229. + MMAL_PARAMETER_REDEYE, /**< @ref MMAL_PARAMETER_REDEYE_T */
  21230. + MMAL_PARAMETER_FOCUS, /**< @ref MMAL_PARAMETER_FOCUS_T */
  21231. + MMAL_PARAMETER_FOCAL_LENGTHS, /**< Unused? */
  21232. + MMAL_PARAMETER_EXPOSURE_COMP, /**< @ref MMAL_PARAMETER_INT32_T */
  21233. + MMAL_PARAMETER_ZOOM, /**< @ref MMAL_PARAMETER_SCALEFACTOR_T */
  21234. + MMAL_PARAMETER_MIRROR, /**< @ref MMAL_PARAMETER_MIRROR_T */
  21235. +
  21236. + /* 0x10 */
  21237. + MMAL_PARAMETER_CAMERA_NUM, /**< @ref MMAL_PARAMETER_UINT32_T */
  21238. + MMAL_PARAMETER_CAPTURE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21239. + MMAL_PARAMETER_EXPOSURE_MODE, /**< @ref MMAL_PARAMETER_EXPOSUREMODE_T */
  21240. + MMAL_PARAMETER_EXP_METERING_MODE, /**< @ref MMAL_PARAMETER_EXPOSUREMETERINGMODE_T */
  21241. + MMAL_PARAMETER_FOCUS_STATUS, /**< @ref MMAL_PARAMETER_FOCUS_STATUS_T */
  21242. + MMAL_PARAMETER_CAMERA_CONFIG, /**< @ref MMAL_PARAMETER_CAMERA_CONFIG_T */
  21243. + MMAL_PARAMETER_CAPTURE_STATUS, /**< @ref MMAL_PARAMETER_CAPTURE_STATUS_T */
  21244. + MMAL_PARAMETER_FACE_TRACK, /**< @ref MMAL_PARAMETER_FACE_TRACK_T */
  21245. + MMAL_PARAMETER_DRAW_BOX_FACES_AND_FOCUS, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21246. + MMAL_PARAMETER_JPEG_Q_FACTOR, /**< @ref MMAL_PARAMETER_UINT32_T */
  21247. + MMAL_PARAMETER_FRAME_RATE, /**< @ref MMAL_PARAMETER_FRAME_RATE_T */
  21248. + MMAL_PARAMETER_USE_STC, /**< @ref MMAL_PARAMETER_CAMERA_STC_MODE_T */
  21249. + MMAL_PARAMETER_CAMERA_INFO, /**< @ref MMAL_PARAMETER_CAMERA_INFO_T */
  21250. + MMAL_PARAMETER_VIDEO_STABILISATION, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21251. + MMAL_PARAMETER_FACE_TRACK_RESULTS, /**< @ref MMAL_PARAMETER_FACE_TRACK_RESULTS_T */
  21252. + MMAL_PARAMETER_ENABLE_RAW_CAPTURE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21253. +
  21254. + /* 0x20 */
  21255. + MMAL_PARAMETER_DPF_FILE, /**< @ref MMAL_PARAMETER_URI_T */
  21256. + MMAL_PARAMETER_ENABLE_DPF_FILE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21257. + MMAL_PARAMETER_DPF_FAIL_IS_FATAL, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21258. + MMAL_PARAMETER_CAPTURE_MODE, /**< @ref MMAL_PARAMETER_CAPTUREMODE_T */
  21259. + MMAL_PARAMETER_FOCUS_REGIONS, /**< @ref MMAL_PARAMETER_FOCUS_REGIONS_T */
  21260. + MMAL_PARAMETER_INPUT_CROP, /**< @ref MMAL_PARAMETER_INPUT_CROP_T */
  21261. + MMAL_PARAMETER_SENSOR_INFORMATION, /**< @ref MMAL_PARAMETER_SENSOR_INFORMATION_T */
  21262. + MMAL_PARAMETER_FLASH_SELECT, /**< @ref MMAL_PARAMETER_FLASH_SELECT_T */
  21263. + MMAL_PARAMETER_FIELD_OF_VIEW, /**< @ref MMAL_PARAMETER_FIELD_OF_VIEW_T */
  21264. + MMAL_PARAMETER_HIGH_DYNAMIC_RANGE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21265. + MMAL_PARAMETER_DYNAMIC_RANGE_COMPRESSION, /**< @ref MMAL_PARAMETER_DRC_T */
  21266. + MMAL_PARAMETER_ALGORITHM_CONTROL, /**< @ref MMAL_PARAMETER_ALGORITHM_CONTROL_T */
  21267. + MMAL_PARAMETER_SHARPNESS, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  21268. + MMAL_PARAMETER_CONTRAST, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  21269. + MMAL_PARAMETER_BRIGHTNESS, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  21270. + MMAL_PARAMETER_SATURATION, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  21271. +
  21272. + /* 0x30 */
  21273. + MMAL_PARAMETER_ISO, /**< @ref MMAL_PARAMETER_UINT32_T */
  21274. + MMAL_PARAMETER_ANTISHAKE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21275. +
  21276. + /** @ref MMAL_PARAMETER_IMAGEFX_PARAMETERS_T */
  21277. + MMAL_PARAMETER_IMAGE_EFFECT_PARAMETERS,
  21278. +
  21279. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  21280. + MMAL_PARAMETER_CAMERA_BURST_CAPTURE,
  21281. +
  21282. + /** @ref MMAL_PARAMETER_UINT32_T */
  21283. + MMAL_PARAMETER_CAMERA_MIN_ISO,
  21284. +
  21285. + /** @ref MMAL_PARAMETER_CAMERA_USE_CASE_T */
  21286. + MMAL_PARAMETER_CAMERA_USE_CASE,
  21287. +
  21288. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21289. + MMAL_PARAMETER_CAPTURE_STATS_PASS,
  21290. +
  21291. + /** @ref MMAL_PARAMETER_UINT32_T */
  21292. + MMAL_PARAMETER_CAMERA_CUSTOM_SENSOR_CONFIG,
  21293. +
  21294. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  21295. + MMAL_PARAMETER_ENABLE_REGISTER_FILE,
  21296. +
  21297. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  21298. + MMAL_PARAMETER_REGISTER_FAIL_IS_FATAL,
  21299. +
  21300. + /** @ref MMAL_PARAMETER_CONFIGFILE_T */
  21301. + MMAL_PARAMETER_CONFIGFILE_REGISTERS,
  21302. +
  21303. + /** @ref MMAL_PARAMETER_CONFIGFILE_CHUNK_T */
  21304. + MMAL_PARAMETER_CONFIGFILE_CHUNK_REGISTERS,
  21305. + MMAL_PARAMETER_JPEG_ATTACH_LOG, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21306. + MMAL_PARAMETER_ZERO_SHUTTER_LAG, /**< @ref MMAL_PARAMETER_ZEROSHUTTERLAG_T */
  21307. + MMAL_PARAMETER_FPS_RANGE, /**< @ref MMAL_PARAMETER_FPS_RANGE_T */
  21308. + MMAL_PARAMETER_CAPTURE_EXPOSURE_COMP, /**< @ref MMAL_PARAMETER_INT32_T */
  21309. +
  21310. + /* 0x40 */
  21311. + MMAL_PARAMETER_SW_SHARPEN_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21312. + MMAL_PARAMETER_FLASH_REQUIRED, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21313. + MMAL_PARAMETER_SW_SATURATION_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21314. + MMAL_PARAMETER_SHUTTER_SPEED, /**< Takes a @ref MMAL_PARAMETER_UINT32_T */
  21315. + MMAL_PARAMETER_CUSTOM_AWB_GAINS, /**< Takes a @ref MMAL_PARAMETER_AWB_GAINS_T */
  21316. +};
  21317. +
  21318. +struct mmal_parameter_rational {
  21319. + s32 num; /**< Numerator */
  21320. + s32 den; /**< Denominator */
  21321. +};
  21322. +
  21323. +enum mmal_parameter_camera_config_timestamp_mode {
  21324. + MMAL_PARAM_TIMESTAMP_MODE_ZERO = 0, /* Always timestamp frames as 0 */
  21325. + MMAL_PARAM_TIMESTAMP_MODE_RAW_STC, /* Use the raw STC value
  21326. + * for the frame timestamp
  21327. + */
  21328. + MMAL_PARAM_TIMESTAMP_MODE_RESET_STC, /* Use the STC timestamp
  21329. + * but subtract the
  21330. + * timestamp of the first
  21331. + * frame sent to give a
  21332. + * zero based timestamp.
  21333. + */
  21334. +};
  21335. +
  21336. +struct mmal_parameter_fps_range {
  21337. + /**< Low end of the permitted framerate range */
  21338. + struct mmal_parameter_rational fps_low;
  21339. + /**< High end of the permitted framerate range */
  21340. + struct mmal_parameter_rational fps_high;
  21341. +};
  21342. +
  21343. +
  21344. +/* camera configuration parameter */
  21345. +struct mmal_parameter_camera_config {
  21346. + /* Parameters for setting up the image pools */
  21347. + u32 max_stills_w; /* Max size of stills capture */
  21348. + u32 max_stills_h;
  21349. + u32 stills_yuv422; /* Allow YUV422 stills capture */
  21350. + u32 one_shot_stills; /* Continuous or one shot stills captures. */
  21351. +
  21352. + u32 max_preview_video_w; /* Max size of the preview or video
  21353. + * capture frames
  21354. + */
  21355. + u32 max_preview_video_h;
  21356. + u32 num_preview_video_frames;
  21357. +
  21358. + /** Sets the height of the circular buffer for stills capture. */
  21359. + u32 stills_capture_circular_buffer_height;
  21360. +
  21361. + /** Allows preview/encode to resume as fast as possible after the stills
  21362. + * input frame has been received, and then processes the still frame in
  21363. + * the background whilst preview/encode has resumed.
  21364. + * Actual mode is controlled by MMAL_PARAMETER_CAPTURE_MODE.
  21365. + */
  21366. + u32 fast_preview_resume;
  21367. +
  21368. + /** Selects algorithm for timestamping frames if
  21369. + * there is no clock component connected.
  21370. + * enum mmal_parameter_camera_config_timestamp_mode
  21371. + */
  21372. + s32 use_stc_timestamp;
  21373. +};
  21374. +
  21375. +
  21376. +enum mmal_parameter_exposuremode {
  21377. + MMAL_PARAM_EXPOSUREMODE_OFF,
  21378. + MMAL_PARAM_EXPOSUREMODE_AUTO,
  21379. + MMAL_PARAM_EXPOSUREMODE_NIGHT,
  21380. + MMAL_PARAM_EXPOSUREMODE_NIGHTPREVIEW,
  21381. + MMAL_PARAM_EXPOSUREMODE_BACKLIGHT,
  21382. + MMAL_PARAM_EXPOSUREMODE_SPOTLIGHT,
  21383. + MMAL_PARAM_EXPOSUREMODE_SPORTS,
  21384. + MMAL_PARAM_EXPOSUREMODE_SNOW,
  21385. + MMAL_PARAM_EXPOSUREMODE_BEACH,
  21386. + MMAL_PARAM_EXPOSUREMODE_VERYLONG,
  21387. + MMAL_PARAM_EXPOSUREMODE_FIXEDFPS,
  21388. + MMAL_PARAM_EXPOSUREMODE_ANTISHAKE,
  21389. + MMAL_PARAM_EXPOSUREMODE_FIREWORKS,
  21390. +};
  21391. +
  21392. +enum mmal_parameter_exposuremeteringmode {
  21393. + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE,
  21394. + MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT,
  21395. + MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT,
  21396. + MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX,
  21397. +};
  21398. +
  21399. +enum mmal_parameter_awbmode {
  21400. + MMAL_PARAM_AWBMODE_OFF,
  21401. + MMAL_PARAM_AWBMODE_AUTO,
  21402. + MMAL_PARAM_AWBMODE_SUNLIGHT,
  21403. + MMAL_PARAM_AWBMODE_CLOUDY,
  21404. + MMAL_PARAM_AWBMODE_SHADE,
  21405. + MMAL_PARAM_AWBMODE_TUNGSTEN,
  21406. + MMAL_PARAM_AWBMODE_FLUORESCENT,
  21407. + MMAL_PARAM_AWBMODE_INCANDESCENT,
  21408. + MMAL_PARAM_AWBMODE_FLASH,
  21409. + MMAL_PARAM_AWBMODE_HORIZON,
  21410. +};
  21411. +
  21412. +enum mmal_parameter_imagefx {
  21413. + MMAL_PARAM_IMAGEFX_NONE,
  21414. + MMAL_PARAM_IMAGEFX_NEGATIVE,
  21415. + MMAL_PARAM_IMAGEFX_SOLARIZE,
  21416. + MMAL_PARAM_IMAGEFX_POSTERIZE,
  21417. + MMAL_PARAM_IMAGEFX_WHITEBOARD,
  21418. + MMAL_PARAM_IMAGEFX_BLACKBOARD,
  21419. + MMAL_PARAM_IMAGEFX_SKETCH,
  21420. + MMAL_PARAM_IMAGEFX_DENOISE,
  21421. + MMAL_PARAM_IMAGEFX_EMBOSS,
  21422. + MMAL_PARAM_IMAGEFX_OILPAINT,
  21423. + MMAL_PARAM_IMAGEFX_HATCH,
  21424. + MMAL_PARAM_IMAGEFX_GPEN,
  21425. + MMAL_PARAM_IMAGEFX_PASTEL,
  21426. + MMAL_PARAM_IMAGEFX_WATERCOLOUR,
  21427. + MMAL_PARAM_IMAGEFX_FILM,
  21428. + MMAL_PARAM_IMAGEFX_BLUR,
  21429. + MMAL_PARAM_IMAGEFX_SATURATION,
  21430. + MMAL_PARAM_IMAGEFX_COLOURSWAP,
  21431. + MMAL_PARAM_IMAGEFX_WASHEDOUT,
  21432. + MMAL_PARAM_IMAGEFX_POSTERISE,
  21433. + MMAL_PARAM_IMAGEFX_COLOURPOINT,
  21434. + MMAL_PARAM_IMAGEFX_COLOURBALANCE,
  21435. + MMAL_PARAM_IMAGEFX_CARTOON,
  21436. +};
  21437. +
  21438. +enum MMAL_PARAM_FLICKERAVOID_T {
  21439. + MMAL_PARAM_FLICKERAVOID_OFF,
  21440. + MMAL_PARAM_FLICKERAVOID_AUTO,
  21441. + MMAL_PARAM_FLICKERAVOID_50HZ,
  21442. + MMAL_PARAM_FLICKERAVOID_60HZ,
  21443. + MMAL_PARAM_FLICKERAVOID_MAX = 0x7FFFFFFF
  21444. +};
  21445. +
  21446. +struct mmal_parameter_awbgains {
  21447. + struct mmal_parameter_rational r_gain; /**< Red gain */
  21448. + struct mmal_parameter_rational b_gain; /**< Blue gain */
  21449. +};
  21450. +
  21451. +/** Manner of video rate control */
  21452. +enum mmal_parameter_rate_control_mode {
  21453. + MMAL_VIDEO_RATECONTROL_DEFAULT,
  21454. + MMAL_VIDEO_RATECONTROL_VARIABLE,
  21455. + MMAL_VIDEO_RATECONTROL_CONSTANT,
  21456. + MMAL_VIDEO_RATECONTROL_VARIABLE_SKIP_FRAMES,
  21457. + MMAL_VIDEO_RATECONTROL_CONSTANT_SKIP_FRAMES
  21458. +};
  21459. +
  21460. +enum mmal_video_profile {
  21461. + MMAL_VIDEO_PROFILE_H263_BASELINE,
  21462. + MMAL_VIDEO_PROFILE_H263_H320CODING,
  21463. + MMAL_VIDEO_PROFILE_H263_BACKWARDCOMPATIBLE,
  21464. + MMAL_VIDEO_PROFILE_H263_ISWV2,
  21465. + MMAL_VIDEO_PROFILE_H263_ISWV3,
  21466. + MMAL_VIDEO_PROFILE_H263_HIGHCOMPRESSION,
  21467. + MMAL_VIDEO_PROFILE_H263_INTERNET,
  21468. + MMAL_VIDEO_PROFILE_H263_INTERLACE,
  21469. + MMAL_VIDEO_PROFILE_H263_HIGHLATENCY,
  21470. + MMAL_VIDEO_PROFILE_MP4V_SIMPLE,
  21471. + MMAL_VIDEO_PROFILE_MP4V_SIMPLESCALABLE,
  21472. + MMAL_VIDEO_PROFILE_MP4V_CORE,
  21473. + MMAL_VIDEO_PROFILE_MP4V_MAIN,
  21474. + MMAL_VIDEO_PROFILE_MP4V_NBIT,
  21475. + MMAL_VIDEO_PROFILE_MP4V_SCALABLETEXTURE,
  21476. + MMAL_VIDEO_PROFILE_MP4V_SIMPLEFACE,
  21477. + MMAL_VIDEO_PROFILE_MP4V_SIMPLEFBA,
  21478. + MMAL_VIDEO_PROFILE_MP4V_BASICANIMATED,
  21479. + MMAL_VIDEO_PROFILE_MP4V_HYBRID,
  21480. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDREALTIME,
  21481. + MMAL_VIDEO_PROFILE_MP4V_CORESCALABLE,
  21482. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDCODING,
  21483. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDCORE,
  21484. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDSCALABLE,
  21485. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDSIMPLE,
  21486. + MMAL_VIDEO_PROFILE_H264_BASELINE,
  21487. + MMAL_VIDEO_PROFILE_H264_MAIN,
  21488. + MMAL_VIDEO_PROFILE_H264_EXTENDED,
  21489. + MMAL_VIDEO_PROFILE_H264_HIGH,
  21490. + MMAL_VIDEO_PROFILE_H264_HIGH10,
  21491. + MMAL_VIDEO_PROFILE_H264_HIGH422,
  21492. + MMAL_VIDEO_PROFILE_H264_HIGH444,
  21493. + MMAL_VIDEO_PROFILE_H264_CONSTRAINED_BASELINE,
  21494. + MMAL_VIDEO_PROFILE_DUMMY = 0x7FFFFFFF
  21495. +};
  21496. +
  21497. +enum mmal_video_level {
  21498. + MMAL_VIDEO_LEVEL_H263_10,
  21499. + MMAL_VIDEO_LEVEL_H263_20,
  21500. + MMAL_VIDEO_LEVEL_H263_30,
  21501. + MMAL_VIDEO_LEVEL_H263_40,
  21502. + MMAL_VIDEO_LEVEL_H263_45,
  21503. + MMAL_VIDEO_LEVEL_H263_50,
  21504. + MMAL_VIDEO_LEVEL_H263_60,
  21505. + MMAL_VIDEO_LEVEL_H263_70,
  21506. + MMAL_VIDEO_LEVEL_MP4V_0,
  21507. + MMAL_VIDEO_LEVEL_MP4V_0b,
  21508. + MMAL_VIDEO_LEVEL_MP4V_1,
  21509. + MMAL_VIDEO_LEVEL_MP4V_2,
  21510. + MMAL_VIDEO_LEVEL_MP4V_3,
  21511. + MMAL_VIDEO_LEVEL_MP4V_4,
  21512. + MMAL_VIDEO_LEVEL_MP4V_4a,
  21513. + MMAL_VIDEO_LEVEL_MP4V_5,
  21514. + MMAL_VIDEO_LEVEL_MP4V_6,
  21515. + MMAL_VIDEO_LEVEL_H264_1,
  21516. + MMAL_VIDEO_LEVEL_H264_1b,
  21517. + MMAL_VIDEO_LEVEL_H264_11,
  21518. + MMAL_VIDEO_LEVEL_H264_12,
  21519. + MMAL_VIDEO_LEVEL_H264_13,
  21520. + MMAL_VIDEO_LEVEL_H264_2,
  21521. + MMAL_VIDEO_LEVEL_H264_21,
  21522. + MMAL_VIDEO_LEVEL_H264_22,
  21523. + MMAL_VIDEO_LEVEL_H264_3,
  21524. + MMAL_VIDEO_LEVEL_H264_31,
  21525. + MMAL_VIDEO_LEVEL_H264_32,
  21526. + MMAL_VIDEO_LEVEL_H264_4,
  21527. + MMAL_VIDEO_LEVEL_H264_41,
  21528. + MMAL_VIDEO_LEVEL_H264_42,
  21529. + MMAL_VIDEO_LEVEL_H264_5,
  21530. + MMAL_VIDEO_LEVEL_H264_51,
  21531. + MMAL_VIDEO_LEVEL_DUMMY = 0x7FFFFFFF
  21532. +};
  21533. +
  21534. +struct mmal_parameter_video_profile {
  21535. + enum mmal_video_profile profile;
  21536. + enum mmal_video_level level;
  21537. +};
  21538. +
  21539. +/* video parameters */
  21540. +
  21541. +enum mmal_parameter_video_type {
  21542. + /** @ref MMAL_DISPLAYREGION_T */
  21543. + MMAL_PARAMETER_DISPLAYREGION = MMAL_PARAMETER_GROUP_VIDEO,
  21544. +
  21545. + /** @ref MMAL_PARAMETER_VIDEO_PROFILE_T */
  21546. + MMAL_PARAMETER_SUPPORTED_PROFILES,
  21547. +
  21548. + /** @ref MMAL_PARAMETER_VIDEO_PROFILE_T */
  21549. + MMAL_PARAMETER_PROFILE,
  21550. +
  21551. + /** @ref MMAL_PARAMETER_UINT32_T */
  21552. + MMAL_PARAMETER_INTRAPERIOD,
  21553. +
  21554. + /** @ref MMAL_PARAMETER_VIDEO_RATECONTROL_T */
  21555. + MMAL_PARAMETER_RATECONTROL,
  21556. +
  21557. + /** @ref MMAL_PARAMETER_VIDEO_NALUNITFORMAT_T */
  21558. + MMAL_PARAMETER_NALUNITFORMAT,
  21559. +
  21560. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  21561. + MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
  21562. +
  21563. + /** @ref MMAL_PARAMETER_UINT32_T.
  21564. + * Setting the value to zero resets to the default (one slice per frame).
  21565. + */
  21566. + MMAL_PARAMETER_MB_ROWS_PER_SLICE,
  21567. +
  21568. + /** @ref MMAL_PARAMETER_VIDEO_LEVEL_EXTENSION_T */
  21569. + MMAL_PARAMETER_VIDEO_LEVEL_EXTENSION,
  21570. +
  21571. + /** @ref MMAL_PARAMETER_VIDEO_EEDE_ENABLE_T */
  21572. + MMAL_PARAMETER_VIDEO_EEDE_ENABLE,
  21573. +
  21574. + /** @ref MMAL_PARAMETER_VIDEO_EEDE_LOSSRATE_T */
  21575. + MMAL_PARAMETER_VIDEO_EEDE_LOSSRATE,
  21576. +
  21577. + /** @ref MMAL_PARAMETER_BOOLEAN_T. Request an I-frame. */
  21578. + MMAL_PARAMETER_VIDEO_REQUEST_I_FRAME,
  21579. + /** @ref MMAL_PARAMETER_VIDEO_INTRA_REFRESH_T */
  21580. + MMAL_PARAMETER_VIDEO_INTRA_REFRESH,
  21581. +
  21582. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  21583. + MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
  21584. +
  21585. + /** @ref MMAL_PARAMETER_UINT32_T. Run-time bit rate control */
  21586. + MMAL_PARAMETER_VIDEO_BIT_RATE,
  21587. +
  21588. + /** @ref MMAL_PARAMETER_FRAME_RATE_T */
  21589. + MMAL_PARAMETER_VIDEO_FRAME_RATE,
  21590. +
  21591. + /** @ref MMAL_PARAMETER_UINT32_T. */
  21592. + MMAL_PARAMETER_VIDEO_ENCODE_MIN_QUANT,
  21593. +
  21594. + /** @ref MMAL_PARAMETER_UINT32_T. */
  21595. + MMAL_PARAMETER_VIDEO_ENCODE_MAX_QUANT,
  21596. +
  21597. + /** @ref MMAL_PARAMETER_VIDEO_ENCODE_RC_MODEL_T. */
  21598. + MMAL_PARAMETER_VIDEO_ENCODE_RC_MODEL,
  21599. +
  21600. + MMAL_PARAMETER_EXTRA_BUFFERS, /**< @ref MMAL_PARAMETER_UINT32_T. */
  21601. + /** @ref MMAL_PARAMETER_UINT32_T.
  21602. + * Changing this parameter from the default can reduce frame rate
  21603. + * because image buffers need to be re-pitched.
  21604. + */
  21605. + MMAL_PARAMETER_VIDEO_ALIGN_HORIZ,
  21606. +
  21607. + /** @ref MMAL_PARAMETER_UINT32_T.
  21608. + * Changing this parameter from the default can reduce frame rate
  21609. + * because image buffers need to be re-pitched.
  21610. + */
  21611. + MMAL_PARAMETER_VIDEO_ALIGN_VERT,
  21612. +
  21613. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  21614. + MMAL_PARAMETER_VIDEO_DROPPABLE_PFRAMES,
  21615. +
  21616. + /** @ref MMAL_PARAMETER_UINT32_T. */
  21617. + MMAL_PARAMETER_VIDEO_ENCODE_INITIAL_QUANT,
  21618. +
  21619. + /**< @ref MMAL_PARAMETER_UINT32_T. */
  21620. + MMAL_PARAMETER_VIDEO_ENCODE_QP_P,
  21621. +
  21622. + /**< @ref MMAL_PARAMETER_UINT32_T. */
  21623. + MMAL_PARAMETER_VIDEO_ENCODE_RC_SLICE_DQUANT,
  21624. +
  21625. + /** @ref MMAL_PARAMETER_UINT32_T */
  21626. + MMAL_PARAMETER_VIDEO_ENCODE_FRAME_LIMIT_BITS,
  21627. +
  21628. + /** @ref MMAL_PARAMETER_UINT32_T. */
  21629. + MMAL_PARAMETER_VIDEO_ENCODE_PEAK_RATE,
  21630. +
  21631. + /* H264 specific parameters */
  21632. +
  21633. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  21634. + MMAL_PARAMETER_VIDEO_ENCODE_H264_DISABLE_CABAC,
  21635. +
  21636. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  21637. + MMAL_PARAMETER_VIDEO_ENCODE_H264_LOW_LATENCY,
  21638. +
  21639. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  21640. + MMAL_PARAMETER_VIDEO_ENCODE_H264_AU_DELIMITERS,
  21641. +
  21642. + /** @ref MMAL_PARAMETER_UINT32_T. */
  21643. + MMAL_PARAMETER_VIDEO_ENCODE_H264_DEBLOCK_IDC,
  21644. +
  21645. + /** @ref MMAL_PARAMETER_VIDEO_ENCODER_H264_MB_INTRA_MODES_T. */
  21646. + MMAL_PARAMETER_VIDEO_ENCODE_H264_MB_INTRA_MODE,
  21647. +
  21648. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  21649. + MMAL_PARAMETER_VIDEO_ENCODE_HEADER_ON_OPEN,
  21650. +
  21651. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  21652. + MMAL_PARAMETER_VIDEO_ENCODE_PRECODE_FOR_QP,
  21653. +
  21654. + /** @ref MMAL_PARAMETER_VIDEO_DRM_INIT_INFO_T. */
  21655. + MMAL_PARAMETER_VIDEO_DRM_INIT_INFO,
  21656. +
  21657. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  21658. + MMAL_PARAMETER_VIDEO_TIMESTAMP_FIFO,
  21659. +
  21660. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  21661. + MMAL_PARAMETER_VIDEO_DECODE_ERROR_CONCEALMENT,
  21662. +
  21663. + /** @ref MMAL_PARAMETER_VIDEO_DRM_PROTECT_BUFFER_T. */
  21664. + MMAL_PARAMETER_VIDEO_DRM_PROTECT_BUFFER,
  21665. +
  21666. + /** @ref MMAL_PARAMETER_BYTES_T */
  21667. + MMAL_PARAMETER_VIDEO_DECODE_CONFIG_VD3,
  21668. +
  21669. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21670. + MMAL_PARAMETER_VIDEO_ENCODE_H264_VCL_HRD_PARAMETERS,
  21671. +
  21672. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21673. + MMAL_PARAMETER_VIDEO_ENCODE_H264_LOW_DELAY_HRD_FLAG,
  21674. +
  21675. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  21676. + MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER
  21677. +};
  21678. +
  21679. +/** Valid mirror modes */
  21680. +enum mmal_parameter_mirror {
  21681. + MMAL_PARAM_MIRROR_NONE,
  21682. + MMAL_PARAM_MIRROR_VERTICAL,
  21683. + MMAL_PARAM_MIRROR_HORIZONTAL,
  21684. + MMAL_PARAM_MIRROR_BOTH,
  21685. +};
  21686. +
  21687. +enum mmal_parameter_displaytransform {
  21688. + MMAL_DISPLAY_ROT0 = 0,
  21689. + MMAL_DISPLAY_MIRROR_ROT0 = 1,
  21690. + MMAL_DISPLAY_MIRROR_ROT180 = 2,
  21691. + MMAL_DISPLAY_ROT180 = 3,
  21692. + MMAL_DISPLAY_MIRROR_ROT90 = 4,
  21693. + MMAL_DISPLAY_ROT270 = 5,
  21694. + MMAL_DISPLAY_ROT90 = 6,
  21695. + MMAL_DISPLAY_MIRROR_ROT270 = 7,
  21696. +};
  21697. +
  21698. +enum mmal_parameter_displaymode {
  21699. + MMAL_DISPLAY_MODE_FILL = 0,
  21700. + MMAL_DISPLAY_MODE_LETTERBOX = 1,
  21701. +};
  21702. +
  21703. +enum mmal_parameter_displayset {
  21704. + MMAL_DISPLAY_SET_NONE = 0,
  21705. + MMAL_DISPLAY_SET_NUM = 1,
  21706. + MMAL_DISPLAY_SET_FULLSCREEN = 2,
  21707. + MMAL_DISPLAY_SET_TRANSFORM = 4,
  21708. + MMAL_DISPLAY_SET_DEST_RECT = 8,
  21709. + MMAL_DISPLAY_SET_SRC_RECT = 0x10,
  21710. + MMAL_DISPLAY_SET_MODE = 0x20,
  21711. + MMAL_DISPLAY_SET_PIXEL = 0x40,
  21712. + MMAL_DISPLAY_SET_NOASPECT = 0x80,
  21713. + MMAL_DISPLAY_SET_LAYER = 0x100,
  21714. + MMAL_DISPLAY_SET_COPYPROTECT = 0x200,
  21715. + MMAL_DISPLAY_SET_ALPHA = 0x400,
  21716. +};
  21717. +
  21718. +struct mmal_parameter_displayregion {
  21719. + /** Bitfield that indicates which fields are set and should be
  21720. + * used. All other fields will maintain their current value.
  21721. + * \ref MMAL_DISPLAYSET_T defines the bits that can be
  21722. + * combined.
  21723. + */
  21724. + u32 set;
  21725. +
  21726. + /** Describes the display output device, with 0 typically
  21727. + * being a directly connected LCD display. The actual values
  21728. + * will depend on the hardware. Code using hard-wired numbers
  21729. + * (e.g. 2) is certain to fail.
  21730. + */
  21731. +
  21732. + u32 display_num;
  21733. + /** Indicates that we are using the full device screen area,
  21734. + * rather than a window of the display. If zero, then
  21735. + * dest_rect is used to specify a region of the display to
  21736. + * use.
  21737. + */
  21738. +
  21739. + s32 fullscreen;
  21740. + /** Indicates any rotation or flipping used to map frames onto
  21741. + * the natural display orientation.
  21742. + */
  21743. + u32 transform; /* enum mmal_parameter_displaytransform */
  21744. +
  21745. + /** Where to display the frame within the screen, if
  21746. + * fullscreen is zero.
  21747. + */
  21748. + struct vchiq_mmal_rect dest_rect;
  21749. +
  21750. + /** Indicates which area of the frame to display. If all
  21751. + * values are zero, the whole frame will be used.
  21752. + */
  21753. + struct vchiq_mmal_rect src_rect;
  21754. +
  21755. + /** If set to non-zero, indicates that any display scaling
  21756. + * should disregard the aspect ratio of the frame region being
  21757. + * displayed.
  21758. + */
  21759. + s32 noaspect;
  21760. +
  21761. + /** Indicates how the image should be scaled to fit the
  21762. + * display. \code MMAL_DISPLAY_MODE_FILL \endcode indicates
  21763. + * that the image should fill the screen by potentially
  21764. + * cropping the frames. Setting \code mode \endcode to \code
  21765. + * MMAL_DISPLAY_MODE_LETTERBOX \endcode indicates that all the
  21766. + * source region should be displayed and black bars added if
  21767. + * necessary.
  21768. + */
  21769. + u32 mode; /* enum mmal_parameter_displaymode */
  21770. +
  21771. + /** If non-zero, defines the width of a source pixel relative
  21772. + * to \code pixel_y \endcode. If zero, then pixels default to
  21773. + * being square.
  21774. + */
  21775. + u32 pixel_x;
  21776. +
  21777. + /** If non-zero, defines the height of a source pixel relative
  21778. + * to \code pixel_x \endcode. If zero, then pixels default to
  21779. + * being square.
  21780. + */
  21781. + u32 pixel_y;
  21782. +
  21783. + /** Sets the relative depth of the images, with greater values
  21784. + * being in front of smaller values.
  21785. + */
  21786. + u32 layer;
  21787. +
  21788. + /** Set to non-zero to ensure copy protection is used on
  21789. + * output.
  21790. + */
  21791. + s32 copyprotect_required;
  21792. +
  21793. + /** Level of opacity of the layer, where zero is fully
  21794. + * transparent and 255 is fully opaque.
  21795. + */
  21796. + u32 alpha;
  21797. +};
  21798. +
  21799. +#define MMAL_MAX_IMAGEFX_PARAMETERS 5
  21800. +
  21801. +struct mmal_parameter_imagefx_parameters {
  21802. + enum mmal_parameter_imagefx effect;
  21803. + u32 num_effect_params;
  21804. + u32 effect_parameter[MMAL_MAX_IMAGEFX_PARAMETERS];
  21805. +};
  21806. diff -Nur linux-3.12.33/drivers/media/platform/bcm2835/mmal-vchiq.c linux-3.12.33-rpi/drivers/media/platform/bcm2835/mmal-vchiq.c
  21807. --- linux-3.12.33/drivers/media/platform/bcm2835/mmal-vchiq.c 1969-12-31 18:00:00.000000000 -0600
  21808. +++ linux-3.12.33-rpi/drivers/media/platform/bcm2835/mmal-vchiq.c 2014-12-03 19:13:38.012418001 -0600
  21809. @@ -0,0 +1,1916 @@
  21810. +/*
  21811. + * Broadcom BM2835 V4L2 driver
  21812. + *
  21813. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  21814. + *
  21815. + * This file is subject to the terms and conditions of the GNU General Public
  21816. + * License. See the file COPYING in the main directory of this archive
  21817. + * for more details.
  21818. + *
  21819. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  21820. + * Dave Stevenson <dsteve@broadcom.com>
  21821. + * Simon Mellor <simellor@broadcom.com>
  21822. + * Luke Diamand <luked@broadcom.com>
  21823. + *
  21824. + * V4L2 driver MMAL vchiq interface code
  21825. + */
  21826. +
  21827. +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21828. +
  21829. +#include <linux/errno.h>
  21830. +#include <linux/kernel.h>
  21831. +#include <linux/mutex.h>
  21832. +#include <linux/mm.h>
  21833. +#include <linux/slab.h>
  21834. +#include <linux/completion.h>
  21835. +#include <linux/vmalloc.h>
  21836. +#include <asm/cacheflush.h>
  21837. +#include <media/videobuf2-vmalloc.h>
  21838. +
  21839. +#include "mmal-common.h"
  21840. +#include "mmal-vchiq.h"
  21841. +#include "mmal-msg.h"
  21842. +
  21843. +#define USE_VCHIQ_ARM
  21844. +#include "interface/vchi/vchi.h"
  21845. +
  21846. +/* maximum number of components supported */
  21847. +#define VCHIQ_MMAL_MAX_COMPONENTS 4
  21848. +
  21849. +/*#define FULL_MSG_DUMP 1*/
  21850. +
  21851. +#ifdef DEBUG
  21852. +static const char *const msg_type_names[] = {
  21853. + "UNKNOWN",
  21854. + "QUIT",
  21855. + "SERVICE_CLOSED",
  21856. + "GET_VERSION",
  21857. + "COMPONENT_CREATE",
  21858. + "COMPONENT_DESTROY",
  21859. + "COMPONENT_ENABLE",
  21860. + "COMPONENT_DISABLE",
  21861. + "PORT_INFO_GET",
  21862. + "PORT_INFO_SET",
  21863. + "PORT_ACTION",
  21864. + "BUFFER_FROM_HOST",
  21865. + "BUFFER_TO_HOST",
  21866. + "GET_STATS",
  21867. + "PORT_PARAMETER_SET",
  21868. + "PORT_PARAMETER_GET",
  21869. + "EVENT_TO_HOST",
  21870. + "GET_CORE_STATS_FOR_PORT",
  21871. + "OPAQUE_ALLOCATOR",
  21872. + "CONSUME_MEM",
  21873. + "LMK",
  21874. + "OPAQUE_ALLOCATOR_DESC",
  21875. + "DRM_GET_LHS32",
  21876. + "DRM_GET_TIME",
  21877. + "BUFFER_FROM_HOST_ZEROLEN",
  21878. + "PORT_FLUSH",
  21879. + "HOST_LOG",
  21880. +};
  21881. +#endif
  21882. +
  21883. +static const char *const port_action_type_names[] = {
  21884. + "UNKNOWN",
  21885. + "ENABLE",
  21886. + "DISABLE",
  21887. + "FLUSH",
  21888. + "CONNECT",
  21889. + "DISCONNECT",
  21890. + "SET_REQUIREMENTS",
  21891. +};
  21892. +
  21893. +#if defined(DEBUG)
  21894. +#if defined(FULL_MSG_DUMP)
  21895. +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE) \
  21896. + do { \
  21897. + pr_debug(TITLE" type:%s(%d) length:%d\n", \
  21898. + msg_type_names[(MSG)->h.type], \
  21899. + (MSG)->h.type, (MSG_LEN)); \
  21900. + print_hex_dump(KERN_DEBUG, "<<h: ", DUMP_PREFIX_OFFSET, \
  21901. + 16, 4, (MSG), \
  21902. + sizeof(struct mmal_msg_header), 1); \
  21903. + print_hex_dump(KERN_DEBUG, "<<p: ", DUMP_PREFIX_OFFSET, \
  21904. + 16, 4, \
  21905. + ((u8 *)(MSG)) + sizeof(struct mmal_msg_header),\
  21906. + (MSG_LEN) - sizeof(struct mmal_msg_header), 1); \
  21907. + } while (0)
  21908. +#else
  21909. +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE) \
  21910. + { \
  21911. + pr_debug(TITLE" type:%s(%d) length:%d\n", \
  21912. + msg_type_names[(MSG)->h.type], \
  21913. + (MSG)->h.type, (MSG_LEN)); \
  21914. + }
  21915. +#endif
  21916. +#else
  21917. +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE)
  21918. +#endif
  21919. +
  21920. +/* normal message context */
  21921. +struct mmal_msg_context {
  21922. + union {
  21923. + struct {
  21924. + /* work struct for defered callback - must come first */
  21925. + struct work_struct work;
  21926. + /* mmal instance */
  21927. + struct vchiq_mmal_instance *instance;
  21928. + /* mmal port */
  21929. + struct vchiq_mmal_port *port;
  21930. + /* actual buffer used to store bulk reply */
  21931. + struct mmal_buffer *buffer;
  21932. + /* amount of buffer used */
  21933. + unsigned long buffer_used;
  21934. + /* MMAL buffer flags */
  21935. + u32 mmal_flags;
  21936. + /* Presentation and Decode timestamps */
  21937. + s64 pts;
  21938. + s64 dts;
  21939. +
  21940. + int status; /* context status */
  21941. +
  21942. + } bulk; /* bulk data */
  21943. +
  21944. + struct {
  21945. + /* message handle to release */
  21946. + VCHI_HELD_MSG_T msg_handle;
  21947. + /* pointer to received message */
  21948. + struct mmal_msg *msg;
  21949. + /* received message length */
  21950. + u32 msg_len;
  21951. + /* completion upon reply */
  21952. + struct completion cmplt;
  21953. + } sync; /* synchronous response */
  21954. + } u;
  21955. +
  21956. +};
  21957. +
  21958. +struct vchiq_mmal_instance {
  21959. + VCHI_SERVICE_HANDLE_T handle;
  21960. +
  21961. + /* ensure serialised access to service */
  21962. + struct mutex vchiq_mutex;
  21963. +
  21964. + /* ensure serialised access to bulk operations */
  21965. + struct mutex bulk_mutex;
  21966. +
  21967. + /* vmalloc page to receive scratch bulk xfers into */
  21968. + void *bulk_scratch;
  21969. +
  21970. + /* component to use next */
  21971. + int component_idx;
  21972. + struct vchiq_mmal_component component[VCHIQ_MMAL_MAX_COMPONENTS];
  21973. +};
  21974. +
  21975. +static struct mmal_msg_context *get_msg_context(struct vchiq_mmal_instance
  21976. + *instance)
  21977. +{
  21978. + struct mmal_msg_context *msg_context;
  21979. +
  21980. + /* todo: should this be allocated from a pool to avoid kmalloc */
  21981. + msg_context = kmalloc(sizeof(*msg_context), GFP_KERNEL);
  21982. + memset(msg_context, 0, sizeof(*msg_context));
  21983. +
  21984. + return msg_context;
  21985. +}
  21986. +
  21987. +static void release_msg_context(struct mmal_msg_context *msg_context)
  21988. +{
  21989. + kfree(msg_context);
  21990. +}
  21991. +
  21992. +/* deals with receipt of event to host message */
  21993. +static void event_to_host_cb(struct vchiq_mmal_instance *instance,
  21994. + struct mmal_msg *msg, u32 msg_len)
  21995. +{
  21996. + pr_debug("unhandled event\n");
  21997. + pr_debug("component:%p port type:%d num:%d cmd:0x%x length:%d\n",
  21998. + msg->u.event_to_host.client_component,
  21999. + msg->u.event_to_host.port_type,
  22000. + msg->u.event_to_host.port_num,
  22001. + msg->u.event_to_host.cmd, msg->u.event_to_host.length);
  22002. +}
  22003. +
  22004. +/* workqueue scheduled callback
  22005. + *
  22006. + * we do this because it is important we do not call any other vchiq
  22007. + * sync calls from witin the message delivery thread
  22008. + */
  22009. +static void buffer_work_cb(struct work_struct *work)
  22010. +{
  22011. + struct mmal_msg_context *msg_context = (struct mmal_msg_context *)work;
  22012. +
  22013. + msg_context->u.bulk.port->buffer_cb(msg_context->u.bulk.instance,
  22014. + msg_context->u.bulk.port,
  22015. + msg_context->u.bulk.status,
  22016. + msg_context->u.bulk.buffer,
  22017. + msg_context->u.bulk.buffer_used,
  22018. + msg_context->u.bulk.mmal_flags,
  22019. + msg_context->u.bulk.dts,
  22020. + msg_context->u.bulk.pts);
  22021. +
  22022. + /* release message context */
  22023. + release_msg_context(msg_context);
  22024. +}
  22025. +
  22026. +/* enqueue a bulk receive for a given message context */
  22027. +static int bulk_receive(struct vchiq_mmal_instance *instance,
  22028. + struct mmal_msg *msg,
  22029. + struct mmal_msg_context *msg_context)
  22030. +{
  22031. + unsigned long rd_len;
  22032. + unsigned long flags = 0;
  22033. + int ret;
  22034. +
  22035. + /* bulk mutex stops other bulk operations while we have a
  22036. + * receive in progress - released in callback
  22037. + */
  22038. + ret = mutex_lock_interruptible(&instance->bulk_mutex);
  22039. + if (ret != 0)
  22040. + return ret;
  22041. +
  22042. + rd_len = msg->u.buffer_from_host.buffer_header.length;
  22043. +
  22044. + /* take buffer from queue */
  22045. + spin_lock_irqsave(&msg_context->u.bulk.port->slock, flags);
  22046. + if (list_empty(&msg_context->u.bulk.port->buffers)) {
  22047. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  22048. + pr_err("buffer list empty trying to submit bulk receive\n");
  22049. +
  22050. + /* todo: this is a serious error, we should never have
  22051. + * commited a buffer_to_host operation to the mmal
  22052. + * port without the buffer to back it up (underflow
  22053. + * handling) and there is no obvious way to deal with
  22054. + * this - how is the mmal servie going to react when
  22055. + * we fail to do the xfer and reschedule a buffer when
  22056. + * it arrives? perhaps a starved flag to indicate a
  22057. + * waiting bulk receive?
  22058. + */
  22059. +
  22060. + mutex_unlock(&instance->bulk_mutex);
  22061. +
  22062. + return -EINVAL;
  22063. + }
  22064. +
  22065. + msg_context->u.bulk.buffer =
  22066. + list_entry(msg_context->u.bulk.port->buffers.next,
  22067. + struct mmal_buffer, list);
  22068. + list_del(&msg_context->u.bulk.buffer->list);
  22069. +
  22070. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  22071. +
  22072. + /* ensure we do not overrun the available buffer */
  22073. + if (rd_len > msg_context->u.bulk.buffer->buffer_size) {
  22074. + rd_len = msg_context->u.bulk.buffer->buffer_size;
  22075. + pr_warn("short read as not enough receive buffer space\n");
  22076. + /* todo: is this the correct response, what happens to
  22077. + * the rest of the message data?
  22078. + */
  22079. + }
  22080. +
  22081. + /* store length */
  22082. + msg_context->u.bulk.buffer_used = rd_len;
  22083. + msg_context->u.bulk.mmal_flags =
  22084. + msg->u.buffer_from_host.buffer_header.flags;
  22085. + msg_context->u.bulk.dts = msg->u.buffer_from_host.buffer_header.dts;
  22086. + msg_context->u.bulk.pts = msg->u.buffer_from_host.buffer_header.pts;
  22087. +
  22088. + // only need to flush L1 cache here, as VCHIQ takes care of the L2
  22089. + // cache.
  22090. + __cpuc_flush_dcache_area(msg_context->u.bulk.buffer->buffer, rd_len);
  22091. +
  22092. + /* queue the bulk submission */
  22093. + vchi_service_use(instance->handle);
  22094. + ret = vchi_bulk_queue_receive(instance->handle,
  22095. + msg_context->u.bulk.buffer->buffer,
  22096. + /* Actual receive needs to be a multiple
  22097. + * of 4 bytes
  22098. + */
  22099. + (rd_len + 3) & ~3,
  22100. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE |
  22101. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
  22102. + msg_context);
  22103. +
  22104. + vchi_service_release(instance->handle);
  22105. +
  22106. + if (ret != 0) {
  22107. + /* callback will not be clearing the mutex */
  22108. + mutex_unlock(&instance->bulk_mutex);
  22109. + }
  22110. +
  22111. + return ret;
  22112. +}
  22113. +
  22114. +/* enque a dummy bulk receive for a given message context */
  22115. +static int dummy_bulk_receive(struct vchiq_mmal_instance *instance,
  22116. + struct mmal_msg_context *msg_context)
  22117. +{
  22118. + int ret;
  22119. +
  22120. + /* bulk mutex stops other bulk operations while we have a
  22121. + * receive in progress - released in callback
  22122. + */
  22123. + ret = mutex_lock_interruptible(&instance->bulk_mutex);
  22124. + if (ret != 0)
  22125. + return ret;
  22126. +
  22127. + /* zero length indicates this was a dummy transfer */
  22128. + msg_context->u.bulk.buffer_used = 0;
  22129. +
  22130. + /* queue the bulk submission */
  22131. + vchi_service_use(instance->handle);
  22132. +
  22133. + ret = vchi_bulk_queue_receive(instance->handle,
  22134. + instance->bulk_scratch,
  22135. + 8,
  22136. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE |
  22137. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
  22138. + msg_context);
  22139. +
  22140. + vchi_service_release(instance->handle);
  22141. +
  22142. + if (ret != 0) {
  22143. + /* callback will not be clearing the mutex */
  22144. + mutex_unlock(&instance->bulk_mutex);
  22145. + }
  22146. +
  22147. + return ret;
  22148. +}
  22149. +
  22150. +/* data in message, memcpy from packet into output buffer */
  22151. +static int inline_receive(struct vchiq_mmal_instance *instance,
  22152. + struct mmal_msg *msg,
  22153. + struct mmal_msg_context *msg_context)
  22154. +{
  22155. + unsigned long flags = 0;
  22156. +
  22157. + /* take buffer from queue */
  22158. + spin_lock_irqsave(&msg_context->u.bulk.port->slock, flags);
  22159. + if (list_empty(&msg_context->u.bulk.port->buffers)) {
  22160. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  22161. + pr_err("buffer list empty trying to receive inline\n");
  22162. +
  22163. + /* todo: this is a serious error, we should never have
  22164. + * commited a buffer_to_host operation to the mmal
  22165. + * port without the buffer to back it up (with
  22166. + * underflow handling) and there is no obvious way to
  22167. + * deal with this. Less bad than the bulk case as we
  22168. + * can just drop this on the floor but...unhelpful
  22169. + */
  22170. + return -EINVAL;
  22171. + }
  22172. +
  22173. + msg_context->u.bulk.buffer =
  22174. + list_entry(msg_context->u.bulk.port->buffers.next,
  22175. + struct mmal_buffer, list);
  22176. + list_del(&msg_context->u.bulk.buffer->list);
  22177. +
  22178. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  22179. +
  22180. + memcpy(msg_context->u.bulk.buffer->buffer,
  22181. + msg->u.buffer_from_host.short_data,
  22182. + msg->u.buffer_from_host.payload_in_message);
  22183. +
  22184. + msg_context->u.bulk.buffer_used =
  22185. + msg->u.buffer_from_host.payload_in_message;
  22186. +
  22187. + return 0;
  22188. +}
  22189. +
  22190. +/* queue the buffer availability with MMAL_MSG_TYPE_BUFFER_FROM_HOST */
  22191. +static int
  22192. +buffer_from_host(struct vchiq_mmal_instance *instance,
  22193. + struct vchiq_mmal_port *port, struct mmal_buffer *buf)
  22194. +{
  22195. + struct mmal_msg_context *msg_context;
  22196. + struct mmal_msg m;
  22197. + int ret;
  22198. +
  22199. + pr_debug("instance:%p buffer:%p\n", instance->handle, buf);
  22200. +
  22201. + /* bulk mutex stops other bulk operations while we
  22202. + * have a receive in progress
  22203. + */
  22204. + if (mutex_lock_interruptible(&instance->bulk_mutex))
  22205. + return -EINTR;
  22206. +
  22207. + /* get context */
  22208. + msg_context = get_msg_context(instance);
  22209. + if (msg_context == NULL)
  22210. + return -ENOMEM;
  22211. +
  22212. + /* store bulk message context for when data arrives */
  22213. + msg_context->u.bulk.instance = instance;
  22214. + msg_context->u.bulk.port = port;
  22215. + msg_context->u.bulk.buffer = NULL; /* not valid until bulk xfer */
  22216. + msg_context->u.bulk.buffer_used = 0;
  22217. +
  22218. + /* initialise work structure ready to schedule callback */
  22219. + INIT_WORK(&msg_context->u.bulk.work, buffer_work_cb);
  22220. +
  22221. + /* prep the buffer from host message */
  22222. + memset(&m, 0xbc, sizeof(m)); /* just to make debug clearer */
  22223. +
  22224. + m.h.type = MMAL_MSG_TYPE_BUFFER_FROM_HOST;
  22225. + m.h.magic = MMAL_MAGIC;
  22226. + m.h.context = msg_context;
  22227. + m.h.status = 0;
  22228. +
  22229. + /* drvbuf is our private data passed back */
  22230. + m.u.buffer_from_host.drvbuf.magic = MMAL_MAGIC;
  22231. + m.u.buffer_from_host.drvbuf.component_handle = port->component->handle;
  22232. + m.u.buffer_from_host.drvbuf.port_handle = port->handle;
  22233. + m.u.buffer_from_host.drvbuf.client_context = msg_context;
  22234. +
  22235. + /* buffer header */
  22236. + m.u.buffer_from_host.buffer_header.cmd = 0;
  22237. + m.u.buffer_from_host.buffer_header.data = buf->buffer;
  22238. + m.u.buffer_from_host.buffer_header.alloc_size = buf->buffer_size;
  22239. + m.u.buffer_from_host.buffer_header.length = 0; /* nothing used yet */
  22240. + m.u.buffer_from_host.buffer_header.offset = 0; /* no offset */
  22241. + m.u.buffer_from_host.buffer_header.flags = 0; /* no flags */
  22242. + m.u.buffer_from_host.buffer_header.pts = MMAL_TIME_UNKNOWN;
  22243. + m.u.buffer_from_host.buffer_header.dts = MMAL_TIME_UNKNOWN;
  22244. +
  22245. + /* clear buffer type sepecific data */
  22246. + memset(&m.u.buffer_from_host.buffer_header_type_specific, 0,
  22247. + sizeof(m.u.buffer_from_host.buffer_header_type_specific));
  22248. +
  22249. + /* no payload in message */
  22250. + m.u.buffer_from_host.payload_in_message = 0;
  22251. +
  22252. + vchi_service_use(instance->handle);
  22253. +
  22254. + ret = vchi_msg_queue(instance->handle, &m,
  22255. + sizeof(struct mmal_msg_header) +
  22256. + sizeof(m.u.buffer_from_host),
  22257. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  22258. +
  22259. + if (ret != 0) {
  22260. + release_msg_context(msg_context);
  22261. + /* todo: is this correct error value? */
  22262. + }
  22263. +
  22264. + vchi_service_release(instance->handle);
  22265. +
  22266. + mutex_unlock(&instance->bulk_mutex);
  22267. +
  22268. + return ret;
  22269. +}
  22270. +
  22271. +/* submit a buffer to the mmal sevice
  22272. + *
  22273. + * the buffer_from_host uses size data from the ports next available
  22274. + * mmal_buffer and deals with there being no buffer available by
  22275. + * incrementing the underflow for later
  22276. + */
  22277. +static int port_buffer_from_host(struct vchiq_mmal_instance *instance,
  22278. + struct vchiq_mmal_port *port)
  22279. +{
  22280. + int ret;
  22281. + struct mmal_buffer *buf;
  22282. + unsigned long flags = 0;
  22283. +
  22284. + if (!port->enabled)
  22285. + return -EINVAL;
  22286. +
  22287. + /* peek buffer from queue */
  22288. + spin_lock_irqsave(&port->slock, flags);
  22289. + if (list_empty(&port->buffers)) {
  22290. + port->buffer_underflow++;
  22291. + spin_unlock_irqrestore(&port->slock, flags);
  22292. + return -ENOSPC;
  22293. + }
  22294. +
  22295. + buf = list_entry(port->buffers.next, struct mmal_buffer, list);
  22296. +
  22297. + spin_unlock_irqrestore(&port->slock, flags);
  22298. +
  22299. + /* issue buffer to mmal service */
  22300. + ret = buffer_from_host(instance, port, buf);
  22301. + if (ret) {
  22302. + pr_err("adding buffer header failed\n");
  22303. + /* todo: how should this be dealt with */
  22304. + }
  22305. +
  22306. + return ret;
  22307. +}
  22308. +
  22309. +/* deals with receipt of buffer to host message */
  22310. +static void buffer_to_host_cb(struct vchiq_mmal_instance *instance,
  22311. + struct mmal_msg *msg, u32 msg_len)
  22312. +{
  22313. + struct mmal_msg_context *msg_context;
  22314. +
  22315. + pr_debug("buffer_to_host_cb: instance:%p msg:%p msg_len:%d\n",
  22316. + instance, msg, msg_len);
  22317. +
  22318. + if (msg->u.buffer_from_host.drvbuf.magic == MMAL_MAGIC) {
  22319. + msg_context = msg->u.buffer_from_host.drvbuf.client_context;
  22320. + } else {
  22321. + pr_err("MMAL_MSG_TYPE_BUFFER_TO_HOST with bad magic\n");
  22322. + return;
  22323. + }
  22324. +
  22325. + if (msg->h.status != MMAL_MSG_STATUS_SUCCESS) {
  22326. + /* message reception had an error */
  22327. + pr_warn("error %d in reply\n", msg->h.status);
  22328. +
  22329. + msg_context->u.bulk.status = msg->h.status;
  22330. +
  22331. + } else if (msg->u.buffer_from_host.buffer_header.length == 0) {
  22332. + /* empty buffer */
  22333. + if (msg->u.buffer_from_host.buffer_header.flags &
  22334. + MMAL_BUFFER_HEADER_FLAG_EOS) {
  22335. + msg_context->u.bulk.status =
  22336. + dummy_bulk_receive(instance, msg_context);
  22337. + if (msg_context->u.bulk.status == 0)
  22338. + return; /* successful bulk submission, bulk
  22339. + * completion will trigger callback
  22340. + */
  22341. + } else {
  22342. + /* do callback with empty buffer - not EOS though */
  22343. + msg_context->u.bulk.status = 0;
  22344. + msg_context->u.bulk.buffer_used = 0;
  22345. + }
  22346. + } else if (msg->u.buffer_from_host.payload_in_message == 0) {
  22347. + /* data is not in message, queue a bulk receive */
  22348. + msg_context->u.bulk.status =
  22349. + bulk_receive(instance, msg, msg_context);
  22350. + if (msg_context->u.bulk.status == 0)
  22351. + return; /* successful bulk submission, bulk
  22352. + * completion will trigger callback
  22353. + */
  22354. +
  22355. + /* failed to submit buffer, this will end badly */
  22356. + pr_err("error %d on bulk submission\n",
  22357. + msg_context->u.bulk.status);
  22358. +
  22359. + } else if (msg->u.buffer_from_host.payload_in_message <=
  22360. + MMAL_VC_SHORT_DATA) {
  22361. + /* data payload within message */
  22362. + msg_context->u.bulk.status = inline_receive(instance, msg,
  22363. + msg_context);
  22364. + } else {
  22365. + pr_err("message with invalid short payload\n");
  22366. +
  22367. + /* signal error */
  22368. + msg_context->u.bulk.status = -EINVAL;
  22369. + msg_context->u.bulk.buffer_used =
  22370. + msg->u.buffer_from_host.payload_in_message;
  22371. + }
  22372. +
  22373. + /* replace the buffer header */
  22374. + port_buffer_from_host(instance, msg_context->u.bulk.port);
  22375. +
  22376. + /* schedule the port callback */
  22377. + schedule_work(&msg_context->u.bulk.work);
  22378. +}
  22379. +
  22380. +static void bulk_receive_cb(struct vchiq_mmal_instance *instance,
  22381. + struct mmal_msg_context *msg_context)
  22382. +{
  22383. + /* bulk receive operation complete */
  22384. + mutex_unlock(&msg_context->u.bulk.instance->bulk_mutex);
  22385. +
  22386. + /* replace the buffer header */
  22387. + port_buffer_from_host(msg_context->u.bulk.instance,
  22388. + msg_context->u.bulk.port);
  22389. +
  22390. + msg_context->u.bulk.status = 0;
  22391. +
  22392. + /* schedule the port callback */
  22393. + schedule_work(&msg_context->u.bulk.work);
  22394. +}
  22395. +
  22396. +static void bulk_abort_cb(struct vchiq_mmal_instance *instance,
  22397. + struct mmal_msg_context *msg_context)
  22398. +{
  22399. + pr_err("%s: bulk ABORTED msg_context:%p\n", __func__, msg_context);
  22400. +
  22401. + /* bulk receive operation complete */
  22402. + mutex_unlock(&msg_context->u.bulk.instance->bulk_mutex);
  22403. +
  22404. + /* replace the buffer header */
  22405. + port_buffer_from_host(msg_context->u.bulk.instance,
  22406. + msg_context->u.bulk.port);
  22407. +
  22408. + msg_context->u.bulk.status = -EINTR;
  22409. +
  22410. + schedule_work(&msg_context->u.bulk.work);
  22411. +}
  22412. +
  22413. +/* incoming event service callback */
  22414. +static void service_callback(void *param,
  22415. + const VCHI_CALLBACK_REASON_T reason,
  22416. + void *bulk_ctx)
  22417. +{
  22418. + struct vchiq_mmal_instance *instance = param;
  22419. + int status;
  22420. + u32 msg_len;
  22421. + struct mmal_msg *msg;
  22422. + VCHI_HELD_MSG_T msg_handle;
  22423. +
  22424. + if (!instance) {
  22425. + pr_err("Message callback passed NULL instance\n");
  22426. + return;
  22427. + }
  22428. +
  22429. + switch (reason) {
  22430. + case VCHI_CALLBACK_MSG_AVAILABLE:
  22431. + status = vchi_msg_hold(instance->handle, (void **)&msg,
  22432. + &msg_len, VCHI_FLAGS_NONE, &msg_handle);
  22433. + if (status) {
  22434. + pr_err("Unable to dequeue a message (%d)\n", status);
  22435. + break;
  22436. + }
  22437. +
  22438. + DBG_DUMP_MSG(msg, msg_len, "<<< reply message");
  22439. +
  22440. + /* handling is different for buffer messages */
  22441. + switch (msg->h.type) {
  22442. +
  22443. + case MMAL_MSG_TYPE_BUFFER_FROM_HOST:
  22444. + vchi_held_msg_release(&msg_handle);
  22445. + break;
  22446. +
  22447. + case MMAL_MSG_TYPE_EVENT_TO_HOST:
  22448. + event_to_host_cb(instance, msg, msg_len);
  22449. + vchi_held_msg_release(&msg_handle);
  22450. +
  22451. + break;
  22452. +
  22453. + case MMAL_MSG_TYPE_BUFFER_TO_HOST:
  22454. + buffer_to_host_cb(instance, msg, msg_len);
  22455. + vchi_held_msg_release(&msg_handle);
  22456. + break;
  22457. +
  22458. + default:
  22459. + /* messages dependant on header context to complete */
  22460. +
  22461. + /* todo: the msg.context really ought to be sanity
  22462. + * checked before we just use it, afaict it comes back
  22463. + * and is used raw from the videocore. Perhaps it
  22464. + * should be verified the address lies in the kernel
  22465. + * address space.
  22466. + */
  22467. + if (msg->h.context == NULL) {
  22468. + pr_err("received message context was null!\n");
  22469. + vchi_held_msg_release(&msg_handle);
  22470. + break;
  22471. + }
  22472. +
  22473. + /* fill in context values */
  22474. + msg->h.context->u.sync.msg_handle = msg_handle;
  22475. + msg->h.context->u.sync.msg = msg;
  22476. + msg->h.context->u.sync.msg_len = msg_len;
  22477. +
  22478. + /* todo: should this check (completion_done()
  22479. + * == 1) for no one waiting? or do we need a
  22480. + * flag to tell us the completion has been
  22481. + * interrupted so we can free the message and
  22482. + * its context. This probably also solves the
  22483. + * message arriving after interruption todo
  22484. + * below
  22485. + */
  22486. +
  22487. + /* complete message so caller knows it happened */
  22488. + complete(&msg->h.context->u.sync.cmplt);
  22489. + break;
  22490. + }
  22491. +
  22492. + break;
  22493. +
  22494. + case VCHI_CALLBACK_BULK_RECEIVED:
  22495. + bulk_receive_cb(instance, bulk_ctx);
  22496. + break;
  22497. +
  22498. + case VCHI_CALLBACK_BULK_RECEIVE_ABORTED:
  22499. + bulk_abort_cb(instance, bulk_ctx);
  22500. + break;
  22501. +
  22502. + case VCHI_CALLBACK_SERVICE_CLOSED:
  22503. + /* TODO: consider if this requires action if received when
  22504. + * driver is not explicitly closing the service
  22505. + */
  22506. + break;
  22507. +
  22508. + default:
  22509. + pr_err("Received unhandled message reason %d\n", reason);
  22510. + break;
  22511. + }
  22512. +}
  22513. +
  22514. +static int send_synchronous_mmal_msg(struct vchiq_mmal_instance *instance,
  22515. + struct mmal_msg *msg,
  22516. + unsigned int payload_len,
  22517. + struct mmal_msg **msg_out,
  22518. + VCHI_HELD_MSG_T *msg_handle_out)
  22519. +{
  22520. + struct mmal_msg_context msg_context;
  22521. + int ret;
  22522. +
  22523. + /* payload size must not cause message to exceed max size */
  22524. + if (payload_len >
  22525. + (MMAL_MSG_MAX_SIZE - sizeof(struct mmal_msg_header))) {
  22526. + pr_err("payload length %d exceeds max:%d\n", payload_len,
  22527. + (MMAL_MSG_MAX_SIZE - sizeof(struct mmal_msg_header)));
  22528. + return -EINVAL;
  22529. + }
  22530. +
  22531. + init_completion(&msg_context.u.sync.cmplt);
  22532. +
  22533. + msg->h.magic = MMAL_MAGIC;
  22534. + msg->h.context = &msg_context;
  22535. + msg->h.status = 0;
  22536. +
  22537. + DBG_DUMP_MSG(msg, (sizeof(struct mmal_msg_header) + payload_len),
  22538. + ">>> sync message");
  22539. +
  22540. + vchi_service_use(instance->handle);
  22541. +
  22542. + ret = vchi_msg_queue(instance->handle,
  22543. + msg,
  22544. + sizeof(struct mmal_msg_header) + payload_len,
  22545. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  22546. +
  22547. + vchi_service_release(instance->handle);
  22548. +
  22549. + if (ret) {
  22550. + pr_err("error %d queuing message\n", ret);
  22551. + return ret;
  22552. + }
  22553. +
  22554. + ret = wait_for_completion_timeout(&msg_context.u.sync.cmplt, 3*HZ);
  22555. + if (ret <= 0) {
  22556. + pr_err("error %d waiting for sync completion\n", ret);
  22557. + if (ret == 0)
  22558. + ret = -ETIME;
  22559. + /* todo: what happens if the message arrives after aborting */
  22560. + return ret;
  22561. + }
  22562. +
  22563. + *msg_out = msg_context.u.sync.msg;
  22564. + *msg_handle_out = msg_context.u.sync.msg_handle;
  22565. +
  22566. + return 0;
  22567. +}
  22568. +
  22569. +static void dump_port_info(struct vchiq_mmal_port *port)
  22570. +{
  22571. + pr_debug("port handle:0x%x enabled:%d\n", port->handle, port->enabled);
  22572. +
  22573. + pr_debug("buffer minimum num:%d size:%d align:%d\n",
  22574. + port->minimum_buffer.num,
  22575. + port->minimum_buffer.size, port->minimum_buffer.alignment);
  22576. +
  22577. + pr_debug("buffer recommended num:%d size:%d align:%d\n",
  22578. + port->recommended_buffer.num,
  22579. + port->recommended_buffer.size,
  22580. + port->recommended_buffer.alignment);
  22581. +
  22582. + pr_debug("buffer current values num:%d size:%d align:%d\n",
  22583. + port->current_buffer.num,
  22584. + port->current_buffer.size, port->current_buffer.alignment);
  22585. +
  22586. + pr_debug("elementry stream: type:%d encoding:0x%x varient:0x%x\n",
  22587. + port->format.type,
  22588. + port->format.encoding, port->format.encoding_variant);
  22589. +
  22590. + pr_debug(" bitrate:%d flags:0x%x\n",
  22591. + port->format.bitrate, port->format.flags);
  22592. +
  22593. + if (port->format.type == MMAL_ES_TYPE_VIDEO) {
  22594. + pr_debug
  22595. + ("es video format: width:%d height:%d colourspace:0x%x\n",
  22596. + port->es.video.width, port->es.video.height,
  22597. + port->es.video.color_space);
  22598. +
  22599. + pr_debug(" : crop xywh %d,%d,%d,%d\n",
  22600. + port->es.video.crop.x,
  22601. + port->es.video.crop.y,
  22602. + port->es.video.crop.width, port->es.video.crop.height);
  22603. + pr_debug(" : framerate %d/%d aspect %d/%d\n",
  22604. + port->es.video.frame_rate.num,
  22605. + port->es.video.frame_rate.den,
  22606. + port->es.video.par.num, port->es.video.par.den);
  22607. + }
  22608. +}
  22609. +
  22610. +static void port_to_mmal_msg(struct vchiq_mmal_port *port, struct mmal_port *p)
  22611. +{
  22612. +
  22613. + /* todo do readonly fields need setting at all? */
  22614. + p->type = port->type;
  22615. + p->index = port->index;
  22616. + p->index_all = 0;
  22617. + p->is_enabled = port->enabled;
  22618. + p->buffer_num_min = port->minimum_buffer.num;
  22619. + p->buffer_size_min = port->minimum_buffer.size;
  22620. + p->buffer_alignment_min = port->minimum_buffer.alignment;
  22621. + p->buffer_num_recommended = port->recommended_buffer.num;
  22622. + p->buffer_size_recommended = port->recommended_buffer.size;
  22623. +
  22624. + /* only three writable fields in a port */
  22625. + p->buffer_num = port->current_buffer.num;
  22626. + p->buffer_size = port->current_buffer.size;
  22627. + p->userdata = port;
  22628. +}
  22629. +
  22630. +static int port_info_set(struct vchiq_mmal_instance *instance,
  22631. + struct vchiq_mmal_port *port)
  22632. +{
  22633. + int ret;
  22634. + struct mmal_msg m;
  22635. + struct mmal_msg *rmsg;
  22636. + VCHI_HELD_MSG_T rmsg_handle;
  22637. +
  22638. + pr_debug("setting port info port %p\n", port);
  22639. + if (!port)
  22640. + return -1;
  22641. + dump_port_info(port);
  22642. +
  22643. + m.h.type = MMAL_MSG_TYPE_PORT_INFO_SET;
  22644. +
  22645. + m.u.port_info_set.component_handle = port->component->handle;
  22646. + m.u.port_info_set.port_type = port->type;
  22647. + m.u.port_info_set.port_index = port->index;
  22648. +
  22649. + port_to_mmal_msg(port, &m.u.port_info_set.port);
  22650. +
  22651. + /* elementry stream format setup */
  22652. + m.u.port_info_set.format.type = port->format.type;
  22653. + m.u.port_info_set.format.encoding = port->format.encoding;
  22654. + m.u.port_info_set.format.encoding_variant =
  22655. + port->format.encoding_variant;
  22656. + m.u.port_info_set.format.bitrate = port->format.bitrate;
  22657. + m.u.port_info_set.format.flags = port->format.flags;
  22658. +
  22659. + memcpy(&m.u.port_info_set.es, &port->es,
  22660. + sizeof(union mmal_es_specific_format));
  22661. +
  22662. + m.u.port_info_set.format.extradata_size = port->format.extradata_size;
  22663. + memcpy(rmsg->u.port_info_set.extradata, port->format.extradata,
  22664. + port->format.extradata_size);
  22665. +
  22666. + ret = send_synchronous_mmal_msg(instance, &m,
  22667. + sizeof(m.u.port_info_set),
  22668. + &rmsg, &rmsg_handle);
  22669. + if (ret)
  22670. + return ret;
  22671. +
  22672. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_INFO_SET) {
  22673. + /* got an unexpected message type in reply */
  22674. + ret = -EINVAL;
  22675. + goto release_msg;
  22676. + }
  22677. +
  22678. + /* return operation status */
  22679. + ret = -rmsg->u.port_info_get_reply.status;
  22680. +
  22681. + pr_debug("%s:result:%d component:0x%x port:%d\n", __func__, ret,
  22682. + port->component->handle, port->handle);
  22683. +
  22684. +release_msg:
  22685. + vchi_held_msg_release(&rmsg_handle);
  22686. +
  22687. + return ret;
  22688. +
  22689. +}
  22690. +
  22691. +/* use port info get message to retrive port information */
  22692. +static int port_info_get(struct vchiq_mmal_instance *instance,
  22693. + struct vchiq_mmal_port *port)
  22694. +{
  22695. + int ret;
  22696. + struct mmal_msg m;
  22697. + struct mmal_msg *rmsg;
  22698. + VCHI_HELD_MSG_T rmsg_handle;
  22699. +
  22700. + /* port info time */
  22701. + m.h.type = MMAL_MSG_TYPE_PORT_INFO_GET;
  22702. + m.u.port_info_get.component_handle = port->component->handle;
  22703. + m.u.port_info_get.port_type = port->type;
  22704. + m.u.port_info_get.index = port->index;
  22705. +
  22706. + ret = send_synchronous_mmal_msg(instance, &m,
  22707. + sizeof(m.u.port_info_get),
  22708. + &rmsg, &rmsg_handle);
  22709. + if (ret)
  22710. + return ret;
  22711. +
  22712. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_INFO_GET) {
  22713. + /* got an unexpected message type in reply */
  22714. + ret = -EINVAL;
  22715. + goto release_msg;
  22716. + }
  22717. +
  22718. + /* return operation status */
  22719. + ret = -rmsg->u.port_info_get_reply.status;
  22720. + if (ret != MMAL_MSG_STATUS_SUCCESS)
  22721. + goto release_msg;
  22722. +
  22723. + if (rmsg->u.port_info_get_reply.port.is_enabled == 0)
  22724. + port->enabled = false;
  22725. + else
  22726. + port->enabled = true;
  22727. +
  22728. + /* copy the values out of the message */
  22729. + port->handle = rmsg->u.port_info_get_reply.port_handle;
  22730. +
  22731. + /* port type and index cached to use on port info set becuase
  22732. + * it does not use a port handle
  22733. + */
  22734. + port->type = rmsg->u.port_info_get_reply.port_type;
  22735. + port->index = rmsg->u.port_info_get_reply.port_index;
  22736. +
  22737. + port->minimum_buffer.num =
  22738. + rmsg->u.port_info_get_reply.port.buffer_num_min;
  22739. + port->minimum_buffer.size =
  22740. + rmsg->u.port_info_get_reply.port.buffer_size_min;
  22741. + port->minimum_buffer.alignment =
  22742. + rmsg->u.port_info_get_reply.port.buffer_alignment_min;
  22743. +
  22744. + port->recommended_buffer.alignment =
  22745. + rmsg->u.port_info_get_reply.port.buffer_alignment_min;
  22746. + port->recommended_buffer.num =
  22747. + rmsg->u.port_info_get_reply.port.buffer_num_recommended;
  22748. +
  22749. + port->current_buffer.num = rmsg->u.port_info_get_reply.port.buffer_num;
  22750. + port->current_buffer.size =
  22751. + rmsg->u.port_info_get_reply.port.buffer_size;
  22752. +
  22753. + /* stream format */
  22754. + port->format.type = rmsg->u.port_info_get_reply.format.type;
  22755. + port->format.encoding = rmsg->u.port_info_get_reply.format.encoding;
  22756. + port->format.encoding_variant =
  22757. + rmsg->u.port_info_get_reply.format.encoding_variant;
  22758. + port->format.bitrate = rmsg->u.port_info_get_reply.format.bitrate;
  22759. + port->format.flags = rmsg->u.port_info_get_reply.format.flags;
  22760. +
  22761. + /* elementry stream format */
  22762. + memcpy(&port->es,
  22763. + &rmsg->u.port_info_get_reply.es,
  22764. + sizeof(union mmal_es_specific_format));
  22765. + port->format.es = &port->es;
  22766. +
  22767. + port->format.extradata_size =
  22768. + rmsg->u.port_info_get_reply.format.extradata_size;
  22769. + memcpy(port->format.extradata,
  22770. + rmsg->u.port_info_get_reply.extradata,
  22771. + port->format.extradata_size);
  22772. +
  22773. + pr_debug("received port info\n");
  22774. + dump_port_info(port);
  22775. +
  22776. +release_msg:
  22777. +
  22778. + pr_debug("%s:result:%d component:0x%x port:%d\n",
  22779. + __func__, ret, port->component->handle, port->handle);
  22780. +
  22781. + vchi_held_msg_release(&rmsg_handle);
  22782. +
  22783. + return ret;
  22784. +}
  22785. +
  22786. +/* create comonent on vc */
  22787. +static int create_component(struct vchiq_mmal_instance *instance,
  22788. + struct vchiq_mmal_component *component,
  22789. + const char *name)
  22790. +{
  22791. + int ret;
  22792. + struct mmal_msg m;
  22793. + struct mmal_msg *rmsg;
  22794. + VCHI_HELD_MSG_T rmsg_handle;
  22795. +
  22796. + /* build component create message */
  22797. + m.h.type = MMAL_MSG_TYPE_COMPONENT_CREATE;
  22798. + m.u.component_create.client_component = component;
  22799. + strncpy(m.u.component_create.name, name,
  22800. + sizeof(m.u.component_create.name));
  22801. +
  22802. + ret = send_synchronous_mmal_msg(instance, &m,
  22803. + sizeof(m.u.component_create),
  22804. + &rmsg, &rmsg_handle);
  22805. + if (ret)
  22806. + return ret;
  22807. +
  22808. + if (rmsg->h.type != m.h.type) {
  22809. + /* got an unexpected message type in reply */
  22810. + ret = -EINVAL;
  22811. + goto release_msg;
  22812. + }
  22813. +
  22814. + ret = -rmsg->u.component_create_reply.status;
  22815. + if (ret != MMAL_MSG_STATUS_SUCCESS)
  22816. + goto release_msg;
  22817. +
  22818. + /* a valid component response received */
  22819. + component->handle = rmsg->u.component_create_reply.component_handle;
  22820. + component->inputs = rmsg->u.component_create_reply.input_num;
  22821. + component->outputs = rmsg->u.component_create_reply.output_num;
  22822. + component->clocks = rmsg->u.component_create_reply.clock_num;
  22823. +
  22824. + pr_debug("Component handle:0x%x in:%d out:%d clock:%d\n",
  22825. + component->handle,
  22826. + component->inputs, component->outputs, component->clocks);
  22827. +
  22828. +release_msg:
  22829. + vchi_held_msg_release(&rmsg_handle);
  22830. +
  22831. + return ret;
  22832. +}
  22833. +
  22834. +/* destroys a component on vc */
  22835. +static int destroy_component(struct vchiq_mmal_instance *instance,
  22836. + struct vchiq_mmal_component *component)
  22837. +{
  22838. + int ret;
  22839. + struct mmal_msg m;
  22840. + struct mmal_msg *rmsg;
  22841. + VCHI_HELD_MSG_T rmsg_handle;
  22842. +
  22843. + m.h.type = MMAL_MSG_TYPE_COMPONENT_DESTROY;
  22844. + m.u.component_destroy.component_handle = component->handle;
  22845. +
  22846. + ret = send_synchronous_mmal_msg(instance, &m,
  22847. + sizeof(m.u.component_destroy),
  22848. + &rmsg, &rmsg_handle);
  22849. + if (ret)
  22850. + return ret;
  22851. +
  22852. + if (rmsg->h.type != m.h.type) {
  22853. + /* got an unexpected message type in reply */
  22854. + ret = -EINVAL;
  22855. + goto release_msg;
  22856. + }
  22857. +
  22858. + ret = -rmsg->u.component_destroy_reply.status;
  22859. +
  22860. +release_msg:
  22861. +
  22862. + vchi_held_msg_release(&rmsg_handle);
  22863. +
  22864. + return ret;
  22865. +}
  22866. +
  22867. +/* enable a component on vc */
  22868. +static int enable_component(struct vchiq_mmal_instance *instance,
  22869. + struct vchiq_mmal_component *component)
  22870. +{
  22871. + int ret;
  22872. + struct mmal_msg m;
  22873. + struct mmal_msg *rmsg;
  22874. + VCHI_HELD_MSG_T rmsg_handle;
  22875. +
  22876. + m.h.type = MMAL_MSG_TYPE_COMPONENT_ENABLE;
  22877. + m.u.component_enable.component_handle = component->handle;
  22878. +
  22879. + ret = send_synchronous_mmal_msg(instance, &m,
  22880. + sizeof(m.u.component_enable),
  22881. + &rmsg, &rmsg_handle);
  22882. + if (ret)
  22883. + return ret;
  22884. +
  22885. + if (rmsg->h.type != m.h.type) {
  22886. + /* got an unexpected message type in reply */
  22887. + ret = -EINVAL;
  22888. + goto release_msg;
  22889. + }
  22890. +
  22891. + ret = -rmsg->u.component_enable_reply.status;
  22892. +
  22893. +release_msg:
  22894. + vchi_held_msg_release(&rmsg_handle);
  22895. +
  22896. + return ret;
  22897. +}
  22898. +
  22899. +/* disable a component on vc */
  22900. +static int disable_component(struct vchiq_mmal_instance *instance,
  22901. + struct vchiq_mmal_component *component)
  22902. +{
  22903. + int ret;
  22904. + struct mmal_msg m;
  22905. + struct mmal_msg *rmsg;
  22906. + VCHI_HELD_MSG_T rmsg_handle;
  22907. +
  22908. + m.h.type = MMAL_MSG_TYPE_COMPONENT_DISABLE;
  22909. + m.u.component_disable.component_handle = component->handle;
  22910. +
  22911. + ret = send_synchronous_mmal_msg(instance, &m,
  22912. + sizeof(m.u.component_disable),
  22913. + &rmsg, &rmsg_handle);
  22914. + if (ret)
  22915. + return ret;
  22916. +
  22917. + if (rmsg->h.type != m.h.type) {
  22918. + /* got an unexpected message type in reply */
  22919. + ret = -EINVAL;
  22920. + goto release_msg;
  22921. + }
  22922. +
  22923. + ret = -rmsg->u.component_disable_reply.status;
  22924. +
  22925. +release_msg:
  22926. +
  22927. + vchi_held_msg_release(&rmsg_handle);
  22928. +
  22929. + return ret;
  22930. +}
  22931. +
  22932. +/* get version of mmal implementation */
  22933. +static int get_version(struct vchiq_mmal_instance *instance,
  22934. + u32 *major_out, u32 *minor_out)
  22935. +{
  22936. + int ret;
  22937. + struct mmal_msg m;
  22938. + struct mmal_msg *rmsg;
  22939. + VCHI_HELD_MSG_T rmsg_handle;
  22940. +
  22941. + m.h.type = MMAL_MSG_TYPE_GET_VERSION;
  22942. +
  22943. + ret = send_synchronous_mmal_msg(instance, &m,
  22944. + sizeof(m.u.version),
  22945. + &rmsg, &rmsg_handle);
  22946. + if (ret)
  22947. + return ret;
  22948. +
  22949. + if (rmsg->h.type != m.h.type) {
  22950. + /* got an unexpected message type in reply */
  22951. + ret = -EINVAL;
  22952. + goto release_msg;
  22953. + }
  22954. +
  22955. + *major_out = rmsg->u.version.major;
  22956. + *minor_out = rmsg->u.version.minor;
  22957. +
  22958. +release_msg:
  22959. + vchi_held_msg_release(&rmsg_handle);
  22960. +
  22961. + return ret;
  22962. +}
  22963. +
  22964. +/* do a port action with a port as a parameter */
  22965. +static int port_action_port(struct vchiq_mmal_instance *instance,
  22966. + struct vchiq_mmal_port *port,
  22967. + enum mmal_msg_port_action_type action_type)
  22968. +{
  22969. + int ret;
  22970. + struct mmal_msg m;
  22971. + struct mmal_msg *rmsg;
  22972. + VCHI_HELD_MSG_T rmsg_handle;
  22973. +
  22974. + m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
  22975. + m.u.port_action_port.component_handle = port->component->handle;
  22976. + m.u.port_action_port.port_handle = port->handle;
  22977. + m.u.port_action_port.action = action_type;
  22978. +
  22979. + port_to_mmal_msg(port, &m.u.port_action_port.port);
  22980. +
  22981. + ret = send_synchronous_mmal_msg(instance, &m,
  22982. + sizeof(m.u.port_action_port),
  22983. + &rmsg, &rmsg_handle);
  22984. + if (ret)
  22985. + return ret;
  22986. +
  22987. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
  22988. + /* got an unexpected message type in reply */
  22989. + ret = -EINVAL;
  22990. + goto release_msg;
  22991. + }
  22992. +
  22993. + ret = -rmsg->u.port_action_reply.status;
  22994. +
  22995. + pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d)\n",
  22996. + __func__,
  22997. + ret, port->component->handle, port->handle,
  22998. + port_action_type_names[action_type], action_type);
  22999. +
  23000. +release_msg:
  23001. + vchi_held_msg_release(&rmsg_handle);
  23002. +
  23003. + return ret;
  23004. +}
  23005. +
  23006. +/* do a port action with handles as parameters */
  23007. +static int port_action_handle(struct vchiq_mmal_instance *instance,
  23008. + struct vchiq_mmal_port *port,
  23009. + enum mmal_msg_port_action_type action_type,
  23010. + u32 connect_component_handle,
  23011. + u32 connect_port_handle)
  23012. +{
  23013. + int ret;
  23014. + struct mmal_msg m;
  23015. + struct mmal_msg *rmsg;
  23016. + VCHI_HELD_MSG_T rmsg_handle;
  23017. +
  23018. + m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
  23019. +
  23020. + m.u.port_action_handle.component_handle = port->component->handle;
  23021. + m.u.port_action_handle.port_handle = port->handle;
  23022. + m.u.port_action_handle.action = action_type;
  23023. +
  23024. + m.u.port_action_handle.connect_component_handle =
  23025. + connect_component_handle;
  23026. + m.u.port_action_handle.connect_port_handle = connect_port_handle;
  23027. +
  23028. + ret = send_synchronous_mmal_msg(instance, &m,
  23029. + sizeof(m.u.port_action_handle),
  23030. + &rmsg, &rmsg_handle);
  23031. + if (ret)
  23032. + return ret;
  23033. +
  23034. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
  23035. + /* got an unexpected message type in reply */
  23036. + ret = -EINVAL;
  23037. + goto release_msg;
  23038. + }
  23039. +
  23040. + ret = -rmsg->u.port_action_reply.status;
  23041. +
  23042. + pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d)" \
  23043. + " connect component:0x%x connect port:%d\n",
  23044. + __func__,
  23045. + ret, port->component->handle, port->handle,
  23046. + port_action_type_names[action_type],
  23047. + action_type, connect_component_handle, connect_port_handle);
  23048. +
  23049. +release_msg:
  23050. + vchi_held_msg_release(&rmsg_handle);
  23051. +
  23052. + return ret;
  23053. +}
  23054. +
  23055. +static int port_parameter_set(struct vchiq_mmal_instance *instance,
  23056. + struct vchiq_mmal_port *port,
  23057. + u32 parameter_id, void *value, u32 value_size)
  23058. +{
  23059. + int ret;
  23060. + struct mmal_msg m;
  23061. + struct mmal_msg *rmsg;
  23062. + VCHI_HELD_MSG_T rmsg_handle;
  23063. +
  23064. + m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_SET;
  23065. +
  23066. + m.u.port_parameter_set.component_handle = port->component->handle;
  23067. + m.u.port_parameter_set.port_handle = port->handle;
  23068. + m.u.port_parameter_set.id = parameter_id;
  23069. + m.u.port_parameter_set.size = (2 * sizeof(u32)) + value_size;
  23070. + memcpy(&m.u.port_parameter_set.value, value, value_size);
  23071. +
  23072. + ret = send_synchronous_mmal_msg(instance, &m,
  23073. + (4 * sizeof(u32)) + value_size,
  23074. + &rmsg, &rmsg_handle);
  23075. + if (ret)
  23076. + return ret;
  23077. +
  23078. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_SET) {
  23079. + /* got an unexpected message type in reply */
  23080. + ret = -EINVAL;
  23081. + goto release_msg;
  23082. + }
  23083. +
  23084. + ret = -rmsg->u.port_parameter_set_reply.status;
  23085. +
  23086. + pr_debug("%s:result:%d component:0x%x port:%d parameter:%d\n",
  23087. + __func__,
  23088. + ret, port->component->handle, port->handle, parameter_id);
  23089. +
  23090. +release_msg:
  23091. + vchi_held_msg_release(&rmsg_handle);
  23092. +
  23093. + return ret;
  23094. +}
  23095. +
  23096. +static int port_parameter_get(struct vchiq_mmal_instance *instance,
  23097. + struct vchiq_mmal_port *port,
  23098. + u32 parameter_id, void *value, u32 *value_size)
  23099. +{
  23100. + int ret;
  23101. + struct mmal_msg m;
  23102. + struct mmal_msg *rmsg;
  23103. + VCHI_HELD_MSG_T rmsg_handle;
  23104. +
  23105. + m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_GET;
  23106. +
  23107. + m.u.port_parameter_get.component_handle = port->component->handle;
  23108. + m.u.port_parameter_get.port_handle = port->handle;
  23109. + m.u.port_parameter_get.id = parameter_id;
  23110. + m.u.port_parameter_get.size = (2 * sizeof(u32)) + *value_size;
  23111. +
  23112. + ret = send_synchronous_mmal_msg(instance, &m,
  23113. + sizeof(struct
  23114. + mmal_msg_port_parameter_get),
  23115. + &rmsg, &rmsg_handle);
  23116. + if (ret)
  23117. + return ret;
  23118. +
  23119. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_GET) {
  23120. + /* got an unexpected message type in reply */
  23121. + pr_err("Incorrect reply type %d\n", rmsg->h.type);
  23122. + ret = -EINVAL;
  23123. + goto release_msg;
  23124. + }
  23125. +
  23126. + ret = -rmsg->u.port_parameter_get_reply.status;
  23127. + if (ret) {
  23128. + /* Copy only as much as we have space for
  23129. + * but report true size of parameter
  23130. + */
  23131. + memcpy(value, &rmsg->u.port_parameter_get_reply.value,
  23132. + *value_size);
  23133. + *value_size = rmsg->u.port_parameter_get_reply.size;
  23134. + } else
  23135. + memcpy(value, &rmsg->u.port_parameter_get_reply.value,
  23136. + rmsg->u.port_parameter_get_reply.size);
  23137. +
  23138. + pr_debug("%s:result:%d component:0x%x port:%d parameter:%d\n", __func__,
  23139. + ret, port->component->handle, port->handle, parameter_id);
  23140. +
  23141. +release_msg:
  23142. + vchi_held_msg_release(&rmsg_handle);
  23143. +
  23144. + return ret;
  23145. +}
  23146. +
  23147. +/* disables a port and drains buffers from it */
  23148. +static int port_disable(struct vchiq_mmal_instance *instance,
  23149. + struct vchiq_mmal_port *port)
  23150. +{
  23151. + int ret;
  23152. + struct list_head *q, *buf_head;
  23153. + unsigned long flags = 0;
  23154. +
  23155. + if (!port->enabled)
  23156. + return 0;
  23157. +
  23158. + port->enabled = false;
  23159. +
  23160. + ret = port_action_port(instance, port,
  23161. + MMAL_MSG_PORT_ACTION_TYPE_DISABLE);
  23162. + if (ret == 0) {
  23163. +
  23164. + /* drain all queued buffers on port */
  23165. + spin_lock_irqsave(&port->slock, flags);
  23166. +
  23167. + list_for_each_safe(buf_head, q, &port->buffers) {
  23168. + struct mmal_buffer *mmalbuf;
  23169. + mmalbuf = list_entry(buf_head, struct mmal_buffer,
  23170. + list);
  23171. + list_del(buf_head);
  23172. + if (port->buffer_cb)
  23173. + port->buffer_cb(instance,
  23174. + port, 0, mmalbuf, 0, 0,
  23175. + MMAL_TIME_UNKNOWN,
  23176. + MMAL_TIME_UNKNOWN);
  23177. + }
  23178. +
  23179. + spin_unlock_irqrestore(&port->slock, flags);
  23180. +
  23181. + ret = port_info_get(instance, port);
  23182. + }
  23183. +
  23184. + return ret;
  23185. +}
  23186. +
  23187. +/* enable a port */
  23188. +static int port_enable(struct vchiq_mmal_instance *instance,
  23189. + struct vchiq_mmal_port *port)
  23190. +{
  23191. + unsigned int hdr_count;
  23192. + struct list_head *buf_head;
  23193. + int ret;
  23194. +
  23195. + if (port->enabled)
  23196. + return 0;
  23197. +
  23198. + /* ensure there are enough buffers queued to cover the buffer headers */
  23199. + if (port->buffer_cb != NULL) {
  23200. + hdr_count = 0;
  23201. + list_for_each(buf_head, &port->buffers) {
  23202. + hdr_count++;
  23203. + }
  23204. + if (hdr_count < port->current_buffer.num)
  23205. + return -ENOSPC;
  23206. + }
  23207. +
  23208. + ret = port_action_port(instance, port,
  23209. + MMAL_MSG_PORT_ACTION_TYPE_ENABLE);
  23210. + if (ret)
  23211. + goto done;
  23212. +
  23213. + port->enabled = true;
  23214. +
  23215. + if (port->buffer_cb) {
  23216. + /* send buffer headers to videocore */
  23217. + hdr_count = 1;
  23218. + list_for_each(buf_head, &port->buffers) {
  23219. + struct mmal_buffer *mmalbuf;
  23220. + mmalbuf = list_entry(buf_head, struct mmal_buffer,
  23221. + list);
  23222. + ret = buffer_from_host(instance, port, mmalbuf);
  23223. + if (ret)
  23224. + goto done;
  23225. +
  23226. + hdr_count++;
  23227. + if (hdr_count > port->current_buffer.num)
  23228. + break;
  23229. + }
  23230. + }
  23231. +
  23232. + ret = port_info_get(instance, port);
  23233. +
  23234. +done:
  23235. + return ret;
  23236. +}
  23237. +
  23238. +/* ------------------------------------------------------------------
  23239. + * Exported API
  23240. + *------------------------------------------------------------------*/
  23241. +
  23242. +int vchiq_mmal_port_set_format(struct vchiq_mmal_instance *instance,
  23243. + struct vchiq_mmal_port *port)
  23244. +{
  23245. + int ret;
  23246. +
  23247. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  23248. + return -EINTR;
  23249. +
  23250. + ret = port_info_set(instance, port);
  23251. + if (ret)
  23252. + goto release_unlock;
  23253. +
  23254. + /* read what has actually been set */
  23255. + ret = port_info_get(instance, port);
  23256. +
  23257. +release_unlock:
  23258. + mutex_unlock(&instance->vchiq_mutex);
  23259. +
  23260. + return ret;
  23261. +
  23262. +}
  23263. +
  23264. +int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
  23265. + struct vchiq_mmal_port *port,
  23266. + u32 parameter, void *value, u32 value_size)
  23267. +{
  23268. + int ret;
  23269. +
  23270. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  23271. + return -EINTR;
  23272. +
  23273. + ret = port_parameter_set(instance, port, parameter, value, value_size);
  23274. +
  23275. + mutex_unlock(&instance->vchiq_mutex);
  23276. +
  23277. + return ret;
  23278. +}
  23279. +
  23280. +int vchiq_mmal_port_parameter_get(struct vchiq_mmal_instance *instance,
  23281. + struct vchiq_mmal_port *port,
  23282. + u32 parameter, void *value, u32 *value_size)
  23283. +{
  23284. + int ret;
  23285. +
  23286. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  23287. + return -EINTR;
  23288. +
  23289. + ret = port_parameter_get(instance, port, parameter, value, value_size);
  23290. +
  23291. + mutex_unlock(&instance->vchiq_mutex);
  23292. +
  23293. + return ret;
  23294. +}
  23295. +
  23296. +/* enable a port
  23297. + *
  23298. + * enables a port and queues buffers for satisfying callbacks if we
  23299. + * provide a callback handler
  23300. + */
  23301. +int vchiq_mmal_port_enable(struct vchiq_mmal_instance *instance,
  23302. + struct vchiq_mmal_port *port,
  23303. + vchiq_mmal_buffer_cb buffer_cb)
  23304. +{
  23305. + int ret;
  23306. +
  23307. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  23308. + return -EINTR;
  23309. +
  23310. + /* already enabled - noop */
  23311. + if (port->enabled) {
  23312. + ret = 0;
  23313. + goto unlock;
  23314. + }
  23315. +
  23316. + port->buffer_cb = buffer_cb;
  23317. +
  23318. + ret = port_enable(instance, port);
  23319. +
  23320. +unlock:
  23321. + mutex_unlock(&instance->vchiq_mutex);
  23322. +
  23323. + return ret;
  23324. +}
  23325. +
  23326. +int vchiq_mmal_port_disable(struct vchiq_mmal_instance *instance,
  23327. + struct vchiq_mmal_port *port)
  23328. +{
  23329. + int ret;
  23330. +
  23331. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  23332. + return -EINTR;
  23333. +
  23334. + if (!port->enabled) {
  23335. + mutex_unlock(&instance->vchiq_mutex);
  23336. + return 0;
  23337. + }
  23338. +
  23339. + ret = port_disable(instance, port);
  23340. +
  23341. + mutex_unlock(&instance->vchiq_mutex);
  23342. +
  23343. + return ret;
  23344. +}
  23345. +
  23346. +/* ports will be connected in a tunneled manner so data buffers
  23347. + * are not handled by client.
  23348. + */
  23349. +int vchiq_mmal_port_connect_tunnel(struct vchiq_mmal_instance *instance,
  23350. + struct vchiq_mmal_port *src,
  23351. + struct vchiq_mmal_port *dst)
  23352. +{
  23353. + int ret;
  23354. +
  23355. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  23356. + return -EINTR;
  23357. +
  23358. + /* disconnect ports if connected */
  23359. + if (src->connected != NULL) {
  23360. + ret = port_disable(instance, src);
  23361. + if (ret) {
  23362. + pr_err("failed disabling src port(%d)\n", ret);
  23363. + goto release_unlock;
  23364. + }
  23365. +
  23366. + /* do not need to disable the destination port as they
  23367. + * are connected and it is done automatically
  23368. + */
  23369. +
  23370. + ret = port_action_handle(instance, src,
  23371. + MMAL_MSG_PORT_ACTION_TYPE_DISCONNECT,
  23372. + src->connected->component->handle,
  23373. + src->connected->handle);
  23374. + if (ret < 0) {
  23375. + pr_err("failed disconnecting src port\n");
  23376. + goto release_unlock;
  23377. + }
  23378. + src->connected->enabled = false;
  23379. + src->connected = NULL;
  23380. + }
  23381. +
  23382. + if (dst == NULL) {
  23383. + /* do not make new connection */
  23384. + ret = 0;
  23385. + pr_debug("not making new connection\n");
  23386. + goto release_unlock;
  23387. + }
  23388. +
  23389. + /* copy src port format to dst */
  23390. + dst->format.encoding = src->format.encoding;
  23391. + dst->es.video.width = src->es.video.width;
  23392. + dst->es.video.height = src->es.video.height;
  23393. + dst->es.video.crop.x = src->es.video.crop.x;
  23394. + dst->es.video.crop.y = src->es.video.crop.y;
  23395. + dst->es.video.crop.width = src->es.video.crop.width;
  23396. + dst->es.video.crop.height = src->es.video.crop.height;
  23397. + dst->es.video.frame_rate.num = src->es.video.frame_rate.num;
  23398. + dst->es.video.frame_rate.den = src->es.video.frame_rate.den;
  23399. +
  23400. + /* set new format */
  23401. + ret = port_info_set(instance, dst);
  23402. + if (ret) {
  23403. + pr_debug("setting port info failed\n");
  23404. + goto release_unlock;
  23405. + }
  23406. +
  23407. + /* read what has actually been set */
  23408. + ret = port_info_get(instance, dst);
  23409. + if (ret) {
  23410. + pr_debug("read back port info failed\n");
  23411. + goto release_unlock;
  23412. + }
  23413. +
  23414. + /* connect two ports together */
  23415. + ret = port_action_handle(instance, src,
  23416. + MMAL_MSG_PORT_ACTION_TYPE_CONNECT,
  23417. + dst->component->handle, dst->handle);
  23418. + if (ret < 0) {
  23419. + pr_debug("connecting port %d:%d to %d:%d failed\n",
  23420. + src->component->handle, src->handle,
  23421. + dst->component->handle, dst->handle);
  23422. + goto release_unlock;
  23423. + }
  23424. + src->connected = dst;
  23425. +
  23426. +release_unlock:
  23427. +
  23428. + mutex_unlock(&instance->vchiq_mutex);
  23429. +
  23430. + return ret;
  23431. +}
  23432. +
  23433. +int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
  23434. + struct vchiq_mmal_port *port,
  23435. + struct mmal_buffer *buffer)
  23436. +{
  23437. + unsigned long flags = 0;
  23438. +
  23439. + spin_lock_irqsave(&port->slock, flags);
  23440. + list_add_tail(&buffer->list, &port->buffers);
  23441. + spin_unlock_irqrestore(&port->slock, flags);
  23442. +
  23443. + /* the port previously underflowed because it was missing a
  23444. + * mmal_buffer which has just been added, submit that buffer
  23445. + * to the mmal service.
  23446. + */
  23447. + if (port->buffer_underflow) {
  23448. + port_buffer_from_host(instance, port);
  23449. + port->buffer_underflow--;
  23450. + }
  23451. +
  23452. + return 0;
  23453. +}
  23454. +
  23455. +/* Initialise a mmal component and its ports
  23456. + *
  23457. + */
  23458. +int vchiq_mmal_component_init(struct vchiq_mmal_instance *instance,
  23459. + const char *name,
  23460. + struct vchiq_mmal_component **component_out)
  23461. +{
  23462. + int ret;
  23463. + int idx; /* port index */
  23464. + struct vchiq_mmal_component *component;
  23465. +
  23466. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  23467. + return -EINTR;
  23468. +
  23469. + if (instance->component_idx == VCHIQ_MMAL_MAX_COMPONENTS) {
  23470. + ret = -EINVAL; /* todo is this correct error? */
  23471. + goto unlock;
  23472. + }
  23473. +
  23474. + component = &instance->component[instance->component_idx];
  23475. +
  23476. + ret = create_component(instance, component, name);
  23477. + if (ret < 0)
  23478. + goto unlock;
  23479. +
  23480. + /* ports info needs gathering */
  23481. + component->control.type = MMAL_PORT_TYPE_CONTROL;
  23482. + component->control.index = 0;
  23483. + component->control.component = component;
  23484. + spin_lock_init(&component->control.slock);
  23485. + INIT_LIST_HEAD(&component->control.buffers);
  23486. + ret = port_info_get(instance, &component->control);
  23487. + if (ret < 0)
  23488. + goto release_component;
  23489. +
  23490. + for (idx = 0; idx < component->inputs; idx++) {
  23491. + component->input[idx].type = MMAL_PORT_TYPE_INPUT;
  23492. + component->input[idx].index = idx;
  23493. + component->input[idx].component = component;
  23494. + spin_lock_init(&component->input[idx].slock);
  23495. + INIT_LIST_HEAD(&component->input[idx].buffers);
  23496. + ret = port_info_get(instance, &component->input[idx]);
  23497. + if (ret < 0)
  23498. + goto release_component;
  23499. + }
  23500. +
  23501. + for (idx = 0; idx < component->outputs; idx++) {
  23502. + component->output[idx].type = MMAL_PORT_TYPE_OUTPUT;
  23503. + component->output[idx].index = idx;
  23504. + component->output[idx].component = component;
  23505. + spin_lock_init(&component->output[idx].slock);
  23506. + INIT_LIST_HEAD(&component->output[idx].buffers);
  23507. + ret = port_info_get(instance, &component->output[idx]);
  23508. + if (ret < 0)
  23509. + goto release_component;
  23510. + }
  23511. +
  23512. + for (idx = 0; idx < component->clocks; idx++) {
  23513. + component->clock[idx].type = MMAL_PORT_TYPE_CLOCK;
  23514. + component->clock[idx].index = idx;
  23515. + component->clock[idx].component = component;
  23516. + spin_lock_init(&component->clock[idx].slock);
  23517. + INIT_LIST_HEAD(&component->clock[idx].buffers);
  23518. + ret = port_info_get(instance, &component->clock[idx]);
  23519. + if (ret < 0)
  23520. + goto release_component;
  23521. + }
  23522. +
  23523. + instance->component_idx++;
  23524. +
  23525. + *component_out = component;
  23526. +
  23527. + mutex_unlock(&instance->vchiq_mutex);
  23528. +
  23529. + return 0;
  23530. +
  23531. +release_component:
  23532. + destroy_component(instance, component);
  23533. +unlock:
  23534. + mutex_unlock(&instance->vchiq_mutex);
  23535. +
  23536. + return ret;
  23537. +}
  23538. +
  23539. +/*
  23540. + * cause a mmal component to be destroyed
  23541. + */
  23542. +int vchiq_mmal_component_finalise(struct vchiq_mmal_instance *instance,
  23543. + struct vchiq_mmal_component *component)
  23544. +{
  23545. + int ret;
  23546. +
  23547. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  23548. + return -EINTR;
  23549. +
  23550. + if (component->enabled)
  23551. + ret = disable_component(instance, component);
  23552. +
  23553. + ret = destroy_component(instance, component);
  23554. +
  23555. + mutex_unlock(&instance->vchiq_mutex);
  23556. +
  23557. + return ret;
  23558. +}
  23559. +
  23560. +/*
  23561. + * cause a mmal component to be enabled
  23562. + */
  23563. +int vchiq_mmal_component_enable(struct vchiq_mmal_instance *instance,
  23564. + struct vchiq_mmal_component *component)
  23565. +{
  23566. + int ret;
  23567. +
  23568. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  23569. + return -EINTR;
  23570. +
  23571. + if (component->enabled) {
  23572. + mutex_unlock(&instance->vchiq_mutex);
  23573. + return 0;
  23574. + }
  23575. +
  23576. + ret = enable_component(instance, component);
  23577. + if (ret == 0)
  23578. + component->enabled = true;
  23579. +
  23580. + mutex_unlock(&instance->vchiq_mutex);
  23581. +
  23582. + return ret;
  23583. +}
  23584. +
  23585. +/*
  23586. + * cause a mmal component to be enabled
  23587. + */
  23588. +int vchiq_mmal_component_disable(struct vchiq_mmal_instance *instance,
  23589. + struct vchiq_mmal_component *component)
  23590. +{
  23591. + int ret;
  23592. +
  23593. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  23594. + return -EINTR;
  23595. +
  23596. + if (!component->enabled) {
  23597. + mutex_unlock(&instance->vchiq_mutex);
  23598. + return 0;
  23599. + }
  23600. +
  23601. + ret = disable_component(instance, component);
  23602. + if (ret == 0)
  23603. + component->enabled = false;
  23604. +
  23605. + mutex_unlock(&instance->vchiq_mutex);
  23606. +
  23607. + return ret;
  23608. +}
  23609. +
  23610. +int vchiq_mmal_version(struct vchiq_mmal_instance *instance,
  23611. + u32 *major_out, u32 *minor_out)
  23612. +{
  23613. + int ret;
  23614. +
  23615. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  23616. + return -EINTR;
  23617. +
  23618. + ret = get_version(instance, major_out, minor_out);
  23619. +
  23620. + mutex_unlock(&instance->vchiq_mutex);
  23621. +
  23622. + return ret;
  23623. +}
  23624. +
  23625. +int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance)
  23626. +{
  23627. + int status = 0;
  23628. +
  23629. + if (instance == NULL)
  23630. + return -EINVAL;
  23631. +
  23632. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  23633. + return -EINTR;
  23634. +
  23635. + vchi_service_use(instance->handle);
  23636. +
  23637. + status = vchi_service_close(instance->handle);
  23638. + if (status != 0)
  23639. + pr_err("mmal-vchiq: VCHIQ close failed");
  23640. +
  23641. + mutex_unlock(&instance->vchiq_mutex);
  23642. +
  23643. + vfree(instance->bulk_scratch);
  23644. +
  23645. + kfree(instance);
  23646. +
  23647. + return status;
  23648. +}
  23649. +
  23650. +int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance)
  23651. +{
  23652. + int status;
  23653. + struct vchiq_mmal_instance *instance;
  23654. + static VCHI_CONNECTION_T *vchi_connection;
  23655. + static VCHI_INSTANCE_T vchi_instance;
  23656. + SERVICE_CREATION_T params = {
  23657. + VCHI_VERSION_EX(VC_MMAL_VER, VC_MMAL_MIN_VER),
  23658. + VC_MMAL_SERVER_NAME,
  23659. + vchi_connection,
  23660. + 0, /* rx fifo size (unused) */
  23661. + 0, /* tx fifo size (unused) */
  23662. + service_callback,
  23663. + NULL, /* service callback parameter */
  23664. + 1, /* unaligned bulk receives */
  23665. + 1, /* unaligned bulk transmits */
  23666. + 0 /* want crc check on bulk transfers */
  23667. + };
  23668. +
  23669. + /* compile time checks to ensure structure size as they are
  23670. + * directly (de)serialised from memory.
  23671. + */
  23672. +
  23673. + /* ensure the header structure has packed to the correct size */
  23674. + BUILD_BUG_ON(sizeof(struct mmal_msg_header) != 24);
  23675. +
  23676. + /* ensure message structure does not exceed maximum length */
  23677. + BUILD_BUG_ON(sizeof(struct mmal_msg) > MMAL_MSG_MAX_SIZE);
  23678. +
  23679. + /* mmal port struct is correct size */
  23680. + BUILD_BUG_ON(sizeof(struct mmal_port) != 64);
  23681. +
  23682. + /* create a vchi instance */
  23683. + status = vchi_initialise(&vchi_instance);
  23684. + if (status) {
  23685. + pr_err("Failed to initialise VCHI instance (status=%d)\n",
  23686. + status);
  23687. + return -EIO;
  23688. + }
  23689. +
  23690. + status = vchi_connect(NULL, 0, vchi_instance);
  23691. + if (status) {
  23692. + pr_err("Failed to connect VCHI instance (status=%d)\n", status);
  23693. + return -EIO;
  23694. + }
  23695. +
  23696. + instance = kmalloc(sizeof(*instance), GFP_KERNEL);
  23697. + memset(instance, 0, sizeof(*instance));
  23698. +
  23699. + mutex_init(&instance->vchiq_mutex);
  23700. + mutex_init(&instance->bulk_mutex);
  23701. +
  23702. + instance->bulk_scratch = vmalloc(PAGE_SIZE);
  23703. +
  23704. + params.callback_param = instance;
  23705. +
  23706. + status = vchi_service_open(vchi_instance, &params, &instance->handle);
  23707. + if (status) {
  23708. + pr_err("Failed to open VCHI service connection (status=%d)\n",
  23709. + status);
  23710. + goto err_close_services;
  23711. + }
  23712. +
  23713. + vchi_service_release(instance->handle);
  23714. +
  23715. + *out_instance = instance;
  23716. +
  23717. + return 0;
  23718. +
  23719. +err_close_services:
  23720. +
  23721. + vchi_service_close(instance->handle);
  23722. + vfree(instance->bulk_scratch);
  23723. + kfree(instance);
  23724. + return -ENODEV;
  23725. +}
  23726. diff -Nur linux-3.12.33/drivers/media/platform/bcm2835/mmal-vchiq.h linux-3.12.33-rpi/drivers/media/platform/bcm2835/mmal-vchiq.h
  23727. --- linux-3.12.33/drivers/media/platform/bcm2835/mmal-vchiq.h 1969-12-31 18:00:00.000000000 -0600
  23728. +++ linux-3.12.33-rpi/drivers/media/platform/bcm2835/mmal-vchiq.h 2014-12-03 19:13:38.012418001 -0600
  23729. @@ -0,0 +1,178 @@
  23730. +/*
  23731. + * Broadcom BM2835 V4L2 driver
  23732. + *
  23733. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  23734. + *
  23735. + * This file is subject to the terms and conditions of the GNU General Public
  23736. + * License. See the file COPYING in the main directory of this archive
  23737. + * for more details.
  23738. + *
  23739. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  23740. + * Dave Stevenson <dsteve@broadcom.com>
  23741. + * Simon Mellor <simellor@broadcom.com>
  23742. + * Luke Diamand <luked@broadcom.com>
  23743. + *
  23744. + * MMAL interface to VCHIQ message passing
  23745. + */
  23746. +
  23747. +#ifndef MMAL_VCHIQ_H
  23748. +#define MMAL_VCHIQ_H
  23749. +
  23750. +#include "mmal-msg-format.h"
  23751. +
  23752. +#define MAX_PORT_COUNT 4
  23753. +
  23754. +/* Maximum size of the format extradata. */
  23755. +#define MMAL_FORMAT_EXTRADATA_MAX_SIZE 128
  23756. +
  23757. +struct vchiq_mmal_instance;
  23758. +
  23759. +enum vchiq_mmal_es_type {
  23760. + MMAL_ES_TYPE_UNKNOWN, /**< Unknown elementary stream type */
  23761. + MMAL_ES_TYPE_CONTROL, /**< Elementary stream of control commands */
  23762. + MMAL_ES_TYPE_AUDIO, /**< Audio elementary stream */
  23763. + MMAL_ES_TYPE_VIDEO, /**< Video elementary stream */
  23764. + MMAL_ES_TYPE_SUBPICTURE /**< Sub-picture elementary stream */
  23765. +};
  23766. +
  23767. +/* rectangle, used lots so it gets its own struct */
  23768. +struct vchiq_mmal_rect {
  23769. + s32 x;
  23770. + s32 y;
  23771. + s32 width;
  23772. + s32 height;
  23773. +};
  23774. +
  23775. +struct vchiq_mmal_port_buffer {
  23776. + unsigned int num; /* number of buffers */
  23777. + u32 size; /* size of buffers */
  23778. + u32 alignment; /* alignment of buffers */
  23779. +};
  23780. +
  23781. +struct vchiq_mmal_port;
  23782. +
  23783. +typedef void (*vchiq_mmal_buffer_cb)(
  23784. + struct vchiq_mmal_instance *instance,
  23785. + struct vchiq_mmal_port *port,
  23786. + int status, struct mmal_buffer *buffer,
  23787. + unsigned long length, u32 mmal_flags, s64 dts, s64 pts);
  23788. +
  23789. +struct vchiq_mmal_port {
  23790. + bool enabled;
  23791. + u32 handle;
  23792. + u32 type; /* port type, cached to use on port info set */
  23793. + u32 index; /* port index, cached to use on port info set */
  23794. +
  23795. + /* component port belongs to, allows simple deref */
  23796. + struct vchiq_mmal_component *component;
  23797. +
  23798. + struct vchiq_mmal_port *connected; /* port conencted to */
  23799. +
  23800. + /* buffer info */
  23801. + struct vchiq_mmal_port_buffer minimum_buffer;
  23802. + struct vchiq_mmal_port_buffer recommended_buffer;
  23803. + struct vchiq_mmal_port_buffer current_buffer;
  23804. +
  23805. + /* stream format */
  23806. + struct mmal_es_format format;
  23807. + /* elementry stream format */
  23808. + union mmal_es_specific_format es;
  23809. +
  23810. + /* data buffers to fill */
  23811. + struct list_head buffers;
  23812. + /* lock to serialise adding and removing buffers from list */
  23813. + spinlock_t slock;
  23814. + /* count of how many buffer header refils have failed because
  23815. + * there was no buffer to satisfy them
  23816. + */
  23817. + int buffer_underflow;
  23818. + /* callback on buffer completion */
  23819. + vchiq_mmal_buffer_cb buffer_cb;
  23820. + /* callback context */
  23821. + void *cb_ctx;
  23822. +};
  23823. +
  23824. +struct vchiq_mmal_component {
  23825. + bool enabled;
  23826. + u32 handle; /* VideoCore handle for component */
  23827. + u32 inputs; /* Number of input ports */
  23828. + u32 outputs; /* Number of output ports */
  23829. + u32 clocks; /* Number of clock ports */
  23830. + struct vchiq_mmal_port control; /* control port */
  23831. + struct vchiq_mmal_port input[MAX_PORT_COUNT]; /* input ports */
  23832. + struct vchiq_mmal_port output[MAX_PORT_COUNT]; /* output ports */
  23833. + struct vchiq_mmal_port clock[MAX_PORT_COUNT]; /* clock ports */
  23834. +};
  23835. +
  23836. +
  23837. +int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance);
  23838. +int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance);
  23839. +
  23840. +/* Initialise a mmal component and its ports
  23841. +*
  23842. +*/
  23843. +int vchiq_mmal_component_init(
  23844. + struct vchiq_mmal_instance *instance,
  23845. + const char *name,
  23846. + struct vchiq_mmal_component **component_out);
  23847. +
  23848. +int vchiq_mmal_component_finalise(
  23849. + struct vchiq_mmal_instance *instance,
  23850. + struct vchiq_mmal_component *component);
  23851. +
  23852. +int vchiq_mmal_component_enable(
  23853. + struct vchiq_mmal_instance *instance,
  23854. + struct vchiq_mmal_component *component);
  23855. +
  23856. +int vchiq_mmal_component_disable(
  23857. + struct vchiq_mmal_instance *instance,
  23858. + struct vchiq_mmal_component *component);
  23859. +
  23860. +
  23861. +
  23862. +/* enable a mmal port
  23863. + *
  23864. + * enables a port and if a buffer callback provided enque buffer
  23865. + * headers as apropriate for the port.
  23866. + */
  23867. +int vchiq_mmal_port_enable(
  23868. + struct vchiq_mmal_instance *instance,
  23869. + struct vchiq_mmal_port *port,
  23870. + vchiq_mmal_buffer_cb buffer_cb);
  23871. +
  23872. +/* disable a port
  23873. + *
  23874. + * disable a port will dequeue any pending buffers
  23875. + */
  23876. +int vchiq_mmal_port_disable(struct vchiq_mmal_instance *instance,
  23877. + struct vchiq_mmal_port *port);
  23878. +
  23879. +
  23880. +int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
  23881. + struct vchiq_mmal_port *port,
  23882. + u32 parameter,
  23883. + void *value,
  23884. + u32 value_size);
  23885. +
  23886. +int vchiq_mmal_port_parameter_get(struct vchiq_mmal_instance *instance,
  23887. + struct vchiq_mmal_port *port,
  23888. + u32 parameter,
  23889. + void *value,
  23890. + u32 *value_size);
  23891. +
  23892. +int vchiq_mmal_port_set_format(struct vchiq_mmal_instance *instance,
  23893. + struct vchiq_mmal_port *port);
  23894. +
  23895. +int vchiq_mmal_port_connect_tunnel(struct vchiq_mmal_instance *instance,
  23896. + struct vchiq_mmal_port *src,
  23897. + struct vchiq_mmal_port *dst);
  23898. +
  23899. +int vchiq_mmal_version(struct vchiq_mmal_instance *instance,
  23900. + u32 *major_out,
  23901. + u32 *minor_out);
  23902. +
  23903. +int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
  23904. + struct vchiq_mmal_port *port,
  23905. + struct mmal_buffer *buf);
  23906. +
  23907. +#endif /* MMAL_VCHIQ_H */
  23908. diff -Nur linux-3.12.33/drivers/media/platform/Kconfig linux-3.12.33-rpi/drivers/media/platform/Kconfig
  23909. --- linux-3.12.33/drivers/media/platform/Kconfig 2014-11-15 06:28:07.000000000 -0600
  23910. +++ linux-3.12.33-rpi/drivers/media/platform/Kconfig 2014-12-03 19:13:38.012418001 -0600
  23911. @@ -124,6 +124,7 @@
  23912. source "drivers/media/platform/soc_camera/Kconfig"
  23913. source "drivers/media/platform/exynos4-is/Kconfig"
  23914. source "drivers/media/platform/s5p-tv/Kconfig"
  23915. +source "drivers/media/platform/bcm2835/Kconfig"
  23916. endif # V4L_PLATFORM_DRIVERS
  23917. diff -Nur linux-3.12.33/drivers/media/platform/Makefile linux-3.12.33-rpi/drivers/media/platform/Makefile
  23918. --- linux-3.12.33/drivers/media/platform/Makefile 2014-11-15 06:28:07.000000000 -0600
  23919. +++ linux-3.12.33-rpi/drivers/media/platform/Makefile 2014-12-03 19:13:38.012418001 -0600
  23920. @@ -52,4 +52,6 @@
  23921. obj-$(CONFIG_ARCH_OMAP) += omap/
  23922. +obj-$(CONFIG_VIDEO_BCM2835) += bcm2835/
  23923. +
  23924. ccflags-y += -I$(srctree)/drivers/media/i2c
  23925. diff -Nur linux-3.12.33/drivers/media/usb/dvb-usb-v2/rtl28xxu.c linux-3.12.33-rpi/drivers/media/usb/dvb-usb-v2/rtl28xxu.c
  23926. --- linux-3.12.33/drivers/media/usb/dvb-usb-v2/rtl28xxu.c 2014-11-15 06:28:07.000000000 -0600
  23927. +++ linux-3.12.33-rpi/drivers/media/usb/dvb-usb-v2/rtl28xxu.c 2014-12-03 19:13:38.092418001 -0600
  23928. @@ -1390,6 +1390,10 @@
  23929. &rtl2832u_props, "Compro VideoMate U620F", NULL) },
  23930. { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394,
  23931. &rtl2832u_props, "MaxMedia HU394-T", NULL) },
  23932. + { DVB_USB_DEVICE(USB_VID_GTEK, 0xb803 /*USB_PID_AUGUST_DVBT205*/,
  23933. + &rtl2832u_props, "August DVB-T 205", NULL) },
  23934. + { DVB_USB_DEVICE(USB_VID_GTEK, 0xa803 /*USB_PID_AUGUST_DVBT205*/,
  23935. + &rtl2832u_props, "August DVB-T 205", NULL) },
  23936. { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a03,
  23937. &rtl2832u_props, "Leadtek WinFast DTV Dongle mini", NULL) },
  23938. { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_CPYTO_REDI_PC50A,
  23939. diff -Nur linux-3.12.33/drivers/media/usb/usbtv/usbtv.c linux-3.12.33-rpi/drivers/media/usb/usbtv/usbtv.c
  23940. --- linux-3.12.33/drivers/media/usb/usbtv/usbtv.c 2014-11-15 06:28:07.000000000 -0600
  23941. +++ linux-3.12.33-rpi/drivers/media/usb/usbtv/usbtv.c 2014-12-03 19:13:38.136418001 -0600
  23942. @@ -50,13 +50,8 @@
  23943. #define USBTV_ISOC_TRANSFERS 16
  23944. #define USBTV_ISOC_PACKETS 8
  23945. -#define USBTV_WIDTH 720
  23946. -#define USBTV_HEIGHT 480
  23947. -
  23948. #define USBTV_CHUNK_SIZE 256
  23949. #define USBTV_CHUNK 240
  23950. -#define USBTV_CHUNKS (USBTV_WIDTH * USBTV_HEIGHT \
  23951. - / 4 / USBTV_CHUNK)
  23952. /* Chunk header. */
  23953. #define USBTV_MAGIC_OK(chunk) ((be32_to_cpu(chunk[0]) & 0xff000000) \
  23954. @@ -65,6 +60,27 @@
  23955. #define USBTV_ODD(chunk) ((be32_to_cpu(chunk[0]) & 0x0000f000) >> 15)
  23956. #define USBTV_CHUNK_NO(chunk) (be32_to_cpu(chunk[0]) & 0x00000fff)
  23957. +#define USBTV_TV_STD (V4L2_STD_525_60 | V4L2_STD_PAL)
  23958. +
  23959. +/* parameters for supported TV norms */
  23960. +struct usbtv_norm_params {
  23961. + v4l2_std_id norm;
  23962. + int cap_width, cap_height;
  23963. +};
  23964. +
  23965. +static struct usbtv_norm_params norm_params[] = {
  23966. + {
  23967. + .norm = V4L2_STD_525_60,
  23968. + .cap_width = 720,
  23969. + .cap_height = 480,
  23970. + },
  23971. + {
  23972. + .norm = V4L2_STD_PAL,
  23973. + .cap_width = 720,
  23974. + .cap_height = 576,
  23975. + }
  23976. +};
  23977. +
  23978. /* A single videobuf2 frame buffer. */
  23979. struct usbtv_buf {
  23980. struct vb2_buffer vb;
  23981. @@ -94,11 +110,38 @@
  23982. USBTV_COMPOSITE_INPUT,
  23983. USBTV_SVIDEO_INPUT,
  23984. } input;
  23985. + v4l2_std_id norm;
  23986. + int width, height;
  23987. + int n_chunks;
  23988. int iso_size;
  23989. unsigned int sequence;
  23990. struct urb *isoc_urbs[USBTV_ISOC_TRANSFERS];
  23991. };
  23992. +static int usbtv_configure_for_norm(struct usbtv *usbtv, v4l2_std_id norm)
  23993. +{
  23994. + int i, ret = 0;
  23995. + struct usbtv_norm_params *params = NULL;
  23996. +
  23997. + for (i = 0; i < ARRAY_SIZE(norm_params); i++) {
  23998. + if (norm_params[i].norm & norm) {
  23999. + params = &norm_params[i];
  24000. + break;
  24001. + }
  24002. + }
  24003. +
  24004. + if (params) {
  24005. + usbtv->width = params->cap_width;
  24006. + usbtv->height = params->cap_height;
  24007. + usbtv->n_chunks = usbtv->width * usbtv->height
  24008. + / 4 / USBTV_CHUNK;
  24009. + usbtv->norm = params->norm;
  24010. + } else
  24011. + ret = -EINVAL;
  24012. +
  24013. + return ret;
  24014. +}
  24015. +
  24016. static int usbtv_set_regs(struct usbtv *usbtv, const u16 regs[][2], int size)
  24017. {
  24018. int ret;
  24019. @@ -158,6 +201,57 @@
  24020. return ret;
  24021. }
  24022. +static int usbtv_select_norm(struct usbtv *usbtv, v4l2_std_id norm)
  24023. +{
  24024. + int ret;
  24025. + static const u16 pal[][2] = {
  24026. + { USBTV_BASE + 0x001a, 0x0068 },
  24027. + { USBTV_BASE + 0x010e, 0x0072 },
  24028. + { USBTV_BASE + 0x010f, 0x00a2 },
  24029. + { USBTV_BASE + 0x0112, 0x00b0 },
  24030. + { USBTV_BASE + 0x0117, 0x0001 },
  24031. + { USBTV_BASE + 0x0118, 0x002c },
  24032. + { USBTV_BASE + 0x012d, 0x0010 },
  24033. + { USBTV_BASE + 0x012f, 0x0020 },
  24034. + { USBTV_BASE + 0x024f, 0x0002 },
  24035. + { USBTV_BASE + 0x0254, 0x0059 },
  24036. + { USBTV_BASE + 0x025a, 0x0016 },
  24037. + { USBTV_BASE + 0x025b, 0x0035 },
  24038. + { USBTV_BASE + 0x0263, 0x0017 },
  24039. + { USBTV_BASE + 0x0266, 0x0016 },
  24040. + { USBTV_BASE + 0x0267, 0x0036 }
  24041. + };
  24042. +
  24043. + static const u16 ntsc[][2] = {
  24044. + { USBTV_BASE + 0x001a, 0x0079 },
  24045. + { USBTV_BASE + 0x010e, 0x0068 },
  24046. + { USBTV_BASE + 0x010f, 0x009c },
  24047. + { USBTV_BASE + 0x0112, 0x00f0 },
  24048. + { USBTV_BASE + 0x0117, 0x0000 },
  24049. + { USBTV_BASE + 0x0118, 0x00fc },
  24050. + { USBTV_BASE + 0x012d, 0x0004 },
  24051. + { USBTV_BASE + 0x012f, 0x0008 },
  24052. + { USBTV_BASE + 0x024f, 0x0001 },
  24053. + { USBTV_BASE + 0x0254, 0x005f },
  24054. + { USBTV_BASE + 0x025a, 0x0012 },
  24055. + { USBTV_BASE + 0x025b, 0x0001 },
  24056. + { USBTV_BASE + 0x0263, 0x001c },
  24057. + { USBTV_BASE + 0x0266, 0x0011 },
  24058. + { USBTV_BASE + 0x0267, 0x0005 }
  24059. + };
  24060. +
  24061. + ret = usbtv_configure_for_norm(usbtv, norm);
  24062. +
  24063. + if (!ret) {
  24064. + if (norm & V4L2_STD_525_60)
  24065. + ret = usbtv_set_regs(usbtv, ntsc, ARRAY_SIZE(ntsc));
  24066. + else if (norm & V4L2_STD_PAL)
  24067. + ret = usbtv_set_regs(usbtv, pal, ARRAY_SIZE(pal));
  24068. + }
  24069. +
  24070. + return ret;
  24071. +}
  24072. +
  24073. static int usbtv_setup_capture(struct usbtv *usbtv)
  24074. {
  24075. int ret;
  24076. @@ -225,26 +319,11 @@
  24077. { USBTV_BASE + 0x0284, 0x0088 },
  24078. { USBTV_BASE + 0x0003, 0x0004 },
  24079. - { USBTV_BASE + 0x001a, 0x0079 },
  24080. { USBTV_BASE + 0x0100, 0x00d3 },
  24081. - { USBTV_BASE + 0x010e, 0x0068 },
  24082. - { USBTV_BASE + 0x010f, 0x009c },
  24083. - { USBTV_BASE + 0x0112, 0x00f0 },
  24084. { USBTV_BASE + 0x0115, 0x0015 },
  24085. - { USBTV_BASE + 0x0117, 0x0000 },
  24086. - { USBTV_BASE + 0x0118, 0x00fc },
  24087. - { USBTV_BASE + 0x012d, 0x0004 },
  24088. - { USBTV_BASE + 0x012f, 0x0008 },
  24089. { USBTV_BASE + 0x0220, 0x002e },
  24090. { USBTV_BASE + 0x0225, 0x0008 },
  24091. { USBTV_BASE + 0x024e, 0x0002 },
  24092. - { USBTV_BASE + 0x024f, 0x0001 },
  24093. - { USBTV_BASE + 0x0254, 0x005f },
  24094. - { USBTV_BASE + 0x025a, 0x0012 },
  24095. - { USBTV_BASE + 0x025b, 0x0001 },
  24096. - { USBTV_BASE + 0x0263, 0x001c },
  24097. - { USBTV_BASE + 0x0266, 0x0011 },
  24098. - { USBTV_BASE + 0x0267, 0x0005 },
  24099. { USBTV_BASE + 0x024e, 0x0002 },
  24100. { USBTV_BASE + 0x024f, 0x0002 },
  24101. };
  24102. @@ -253,6 +332,10 @@
  24103. if (ret)
  24104. return ret;
  24105. + ret = usbtv_select_norm(usbtv, usbtv->norm);
  24106. + if (ret)
  24107. + return ret;
  24108. +
  24109. ret = usbtv_select_input(usbtv, usbtv->input);
  24110. if (ret)
  24111. return ret;
  24112. @@ -296,7 +379,7 @@
  24113. frame_id = USBTV_FRAME_ID(chunk);
  24114. odd = USBTV_ODD(chunk);
  24115. chunk_no = USBTV_CHUNK_NO(chunk);
  24116. - if (chunk_no >= USBTV_CHUNKS)
  24117. + if (chunk_no >= usbtv->n_chunks)
  24118. return;
  24119. /* Beginning of a frame. */
  24120. @@ -324,10 +407,10 @@
  24121. usbtv->chunks_done++;
  24122. /* Last chunk in a frame, signalling an end */
  24123. - if (odd && chunk_no == USBTV_CHUNKS-1) {
  24124. + if (odd && chunk_no == usbtv->n_chunks-1) {
  24125. int size = vb2_plane_size(&buf->vb, 0);
  24126. enum vb2_buffer_state state = usbtv->chunks_done ==
  24127. - USBTV_CHUNKS ?
  24128. + usbtv->n_chunks ?
  24129. VB2_BUF_STATE_DONE :
  24130. VB2_BUF_STATE_ERROR;
  24131. @@ -500,6 +583,8 @@
  24132. static int usbtv_enum_input(struct file *file, void *priv,
  24133. struct v4l2_input *i)
  24134. {
  24135. + struct usbtv *dev = video_drvdata(file);
  24136. +
  24137. switch (i->index) {
  24138. case USBTV_COMPOSITE_INPUT:
  24139. strlcpy(i->name, "Composite", sizeof(i->name));
  24140. @@ -512,7 +597,7 @@
  24141. }
  24142. i->type = V4L2_INPUT_TYPE_CAMERA;
  24143. - i->std = V4L2_STD_525_60;
  24144. + i->std = dev->vdev.tvnorms;
  24145. return 0;
  24146. }
  24147. @@ -531,23 +616,37 @@
  24148. static int usbtv_fmt_vid_cap(struct file *file, void *priv,
  24149. struct v4l2_format *f)
  24150. {
  24151. - f->fmt.pix.width = USBTV_WIDTH;
  24152. - f->fmt.pix.height = USBTV_HEIGHT;
  24153. + struct usbtv *usbtv = video_drvdata(file);
  24154. +
  24155. + f->fmt.pix.width = usbtv->width;
  24156. + f->fmt.pix.height = usbtv->height;
  24157. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  24158. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  24159. - f->fmt.pix.bytesperline = USBTV_WIDTH * 2;
  24160. + f->fmt.pix.bytesperline = usbtv->width * 2;
  24161. f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height);
  24162. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  24163. - f->fmt.pix.priv = 0;
  24164. +
  24165. return 0;
  24166. }
  24167. static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  24168. {
  24169. - *norm = V4L2_STD_525_60;
  24170. + struct usbtv *usbtv = video_drvdata(file);
  24171. + *norm = usbtv->norm;
  24172. return 0;
  24173. }
  24174. +static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm)
  24175. +{
  24176. + int ret = -EINVAL;
  24177. + struct usbtv *usbtv = video_drvdata(file);
  24178. +
  24179. + if ((norm & V4L2_STD_525_60) || (norm & V4L2_STD_PAL))
  24180. + ret = usbtv_select_norm(usbtv, norm);
  24181. +
  24182. + return ret;
  24183. +}
  24184. +
  24185. static int usbtv_g_input(struct file *file, void *priv, unsigned int *i)
  24186. {
  24187. struct usbtv *usbtv = video_drvdata(file);
  24188. @@ -561,13 +660,6 @@
  24189. return usbtv_select_input(usbtv, i);
  24190. }
  24191. -static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm)
  24192. -{
  24193. - if (norm & V4L2_STD_525_60)
  24194. - return 0;
  24195. - return -EINVAL;
  24196. -}
  24197. -
  24198. struct v4l2_ioctl_ops usbtv_ioctl_ops = {
  24199. .vidioc_querycap = usbtv_querycap,
  24200. .vidioc_enum_input = usbtv_enum_input,
  24201. @@ -604,10 +696,12 @@
  24202. const struct v4l2_format *v4l_fmt, unsigned int *nbuffers,
  24203. unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
  24204. {
  24205. + struct usbtv *usbtv = vb2_get_drv_priv(vq);
  24206. +
  24207. if (*nbuffers < 2)
  24208. *nbuffers = 2;
  24209. *nplanes = 1;
  24210. - sizes[0] = USBTV_WIDTH * USBTV_HEIGHT / 2 * sizeof(u32);
  24211. + sizes[0] = USBTV_CHUNK * usbtv->n_chunks * 2 * sizeof(u32);
  24212. return 0;
  24213. }
  24214. @@ -690,7 +784,11 @@
  24215. return -ENOMEM;
  24216. usbtv->dev = dev;
  24217. usbtv->udev = usb_get_dev(interface_to_usbdev(intf));
  24218. +
  24219. usbtv->iso_size = size;
  24220. +
  24221. + (void)usbtv_configure_for_norm(usbtv, V4L2_STD_525_60);
  24222. +
  24223. spin_lock_init(&usbtv->buflock);
  24224. mutex_init(&usbtv->v4l2_lock);
  24225. mutex_init(&usbtv->vb2q_lock);
  24226. @@ -727,7 +825,7 @@
  24227. usbtv->vdev.release = video_device_release_empty;
  24228. usbtv->vdev.fops = &usbtv_fops;
  24229. usbtv->vdev.ioctl_ops = &usbtv_ioctl_ops;
  24230. - usbtv->vdev.tvnorms = V4L2_STD_525_60;
  24231. + usbtv->vdev.tvnorms = USBTV_TV_STD;
  24232. usbtv->vdev.queue = &usbtv->vb2q;
  24233. usbtv->vdev.lock = &usbtv->v4l2_lock;
  24234. set_bit(V4L2_FL_USE_FH_PRIO, &usbtv->vdev.flags);
  24235. diff -Nur linux-3.12.33/drivers/misc/Kconfig linux-3.12.33-rpi/drivers/misc/Kconfig
  24236. --- linux-3.12.33/drivers/misc/Kconfig 2014-11-15 06:28:07.000000000 -0600
  24237. +++ linux-3.12.33-rpi/drivers/misc/Kconfig 2014-12-03 19:13:38.208418001 -0600
  24238. @@ -537,4 +537,5 @@
  24239. source "drivers/misc/altera-stapl/Kconfig"
  24240. source "drivers/misc/mei/Kconfig"
  24241. source "drivers/misc/vmw_vmci/Kconfig"
  24242. +source "drivers/misc/vc04_services/Kconfig"
  24243. endmenu
  24244. diff -Nur linux-3.12.33/drivers/misc/Makefile linux-3.12.33-rpi/drivers/misc/Makefile
  24245. --- linux-3.12.33/drivers/misc/Makefile 2014-11-15 06:28:07.000000000 -0600
  24246. +++ linux-3.12.33-rpi/drivers/misc/Makefile 2014-12-03 19:13:38.208418001 -0600
  24247. @@ -53,3 +53,4 @@
  24248. obj-$(CONFIG_VMWARE_VMCI) += vmw_vmci/
  24249. obj-$(CONFIG_LATTICE_ECP3_CONFIG) += lattice-ecp3-config.o
  24250. obj-$(CONFIG_SRAM) += sram.o
  24251. +obj-$(CONFIG_BCM2708_VCHIQ) += vc04_services/
  24252. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchi/connections/connection.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchi/connections/connection.h
  24253. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchi/connections/connection.h 1969-12-31 18:00:00.000000000 -0600
  24254. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchi/connections/connection.h 2014-12-03 19:13:38.224418001 -0600
  24255. @@ -0,0 +1,328 @@
  24256. +/**
  24257. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  24258. + *
  24259. + * Redistribution and use in source and binary forms, with or without
  24260. + * modification, are permitted provided that the following conditions
  24261. + * are met:
  24262. + * 1. Redistributions of source code must retain the above copyright
  24263. + * notice, this list of conditions, and the following disclaimer,
  24264. + * without modification.
  24265. + * 2. Redistributions in binary form must reproduce the above copyright
  24266. + * notice, this list of conditions and the following disclaimer in the
  24267. + * documentation and/or other materials provided with the distribution.
  24268. + * 3. The names of the above-listed copyright holders may not be used
  24269. + * to endorse or promote products derived from this software without
  24270. + * specific prior written permission.
  24271. + *
  24272. + * ALTERNATIVELY, this software may be distributed under the terms of the
  24273. + * GNU General Public License ("GPL") version 2, as published by the Free
  24274. + * Software Foundation.
  24275. + *
  24276. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24277. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24278. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24279. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24280. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24281. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24282. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24283. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24284. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24285. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24286. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24287. + */
  24288. +
  24289. +#ifndef CONNECTION_H_
  24290. +#define CONNECTION_H_
  24291. +
  24292. +#include <linux/kernel.h>
  24293. +#include <linux/types.h>
  24294. +#include <linux/semaphore.h>
  24295. +
  24296. +#include "interface/vchi/vchi_cfg_internal.h"
  24297. +#include "interface/vchi/vchi_common.h"
  24298. +#include "interface/vchi/message_drivers/message.h"
  24299. +
  24300. +/******************************************************************************
  24301. + Global defs
  24302. + *****************************************************************************/
  24303. +
  24304. +// Opaque handle for a connection / service pair
  24305. +typedef struct opaque_vchi_connection_connected_service_handle_t *VCHI_CONNECTION_SERVICE_HANDLE_T;
  24306. +
  24307. +// opaque handle to the connection state information
  24308. +typedef struct opaque_vchi_connection_info_t VCHI_CONNECTION_STATE_T;
  24309. +
  24310. +typedef struct vchi_connection_t VCHI_CONNECTION_T;
  24311. +
  24312. +
  24313. +/******************************************************************************
  24314. + API
  24315. + *****************************************************************************/
  24316. +
  24317. +// Routine to init a connection with a particular low level driver
  24318. +typedef VCHI_CONNECTION_STATE_T * (*VCHI_CONNECTION_INIT_T)( struct vchi_connection_t * connection,
  24319. + const VCHI_MESSAGE_DRIVER_T * driver );
  24320. +
  24321. +// Routine to control CRC enabling at a connection level
  24322. +typedef int32_t (*VCHI_CONNECTION_CRC_CONTROL_T)( VCHI_CONNECTION_STATE_T *state_handle,
  24323. + VCHI_CRC_CONTROL_T control );
  24324. +
  24325. +// Routine to create a service
  24326. +typedef int32_t (*VCHI_CONNECTION_SERVICE_CONNECT_T)( VCHI_CONNECTION_STATE_T *state_handle,
  24327. + int32_t service_id,
  24328. + uint32_t rx_fifo_size,
  24329. + uint32_t tx_fifo_size,
  24330. + int server,
  24331. + VCHI_CALLBACK_T callback,
  24332. + void *callback_param,
  24333. + int32_t want_crc,
  24334. + int32_t want_unaligned_bulk_rx,
  24335. + int32_t want_unaligned_bulk_tx,
  24336. + VCHI_CONNECTION_SERVICE_HANDLE_T *service_handle );
  24337. +
  24338. +// Routine to close a service
  24339. +typedef int32_t (*VCHI_CONNECTION_SERVICE_DISCONNECT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle );
  24340. +
  24341. +// Routine to queue a message
  24342. +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  24343. + const void *data,
  24344. + uint32_t data_size,
  24345. + VCHI_FLAGS_T flags,
  24346. + void *msg_handle );
  24347. +
  24348. +// scatter-gather (vector) message queueing
  24349. +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  24350. + VCHI_MSG_VECTOR_T *vector,
  24351. + uint32_t count,
  24352. + VCHI_FLAGS_T flags,
  24353. + void *msg_handle );
  24354. +
  24355. +// Routine to dequeue a message
  24356. +typedef int32_t (*VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  24357. + void *data,
  24358. + uint32_t max_data_size_to_read,
  24359. + uint32_t *actual_msg_size,
  24360. + VCHI_FLAGS_T flags );
  24361. +
  24362. +// Routine to peek at a message
  24363. +typedef int32_t (*VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  24364. + void **data,
  24365. + uint32_t *msg_size,
  24366. + VCHI_FLAGS_T flags );
  24367. +
  24368. +// Routine to hold a message
  24369. +typedef int32_t (*VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  24370. + void **data,
  24371. + uint32_t *msg_size,
  24372. + VCHI_FLAGS_T flags,
  24373. + void **message_handle );
  24374. +
  24375. +// Routine to initialise a received message iterator
  24376. +typedef int32_t (*VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  24377. + VCHI_MSG_ITER_T *iter,
  24378. + VCHI_FLAGS_T flags );
  24379. +
  24380. +// Routine to release a held message
  24381. +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_RELEASE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  24382. + void *message_handle );
  24383. +
  24384. +// Routine to get info on a held message
  24385. +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_INFO_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  24386. + void *message_handle,
  24387. + void **data,
  24388. + int32_t *msg_size,
  24389. + uint32_t *tx_timestamp,
  24390. + uint32_t *rx_timestamp );
  24391. +
  24392. +// Routine to check whether the iterator has a next message
  24393. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  24394. + const VCHI_MSG_ITER_T *iter );
  24395. +
  24396. +// Routine to advance the iterator
  24397. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  24398. + VCHI_MSG_ITER_T *iter,
  24399. + void **data,
  24400. + uint32_t *msg_size );
  24401. +
  24402. +// Routine to remove the last message returned by the iterator
  24403. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_REMOVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  24404. + VCHI_MSG_ITER_T *iter );
  24405. +
  24406. +// Routine to hold the last message returned by the iterator
  24407. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HOLD_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  24408. + VCHI_MSG_ITER_T *iter,
  24409. + void **msg_handle );
  24410. +
  24411. +// Routine to transmit bulk data
  24412. +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  24413. + const void *data_src,
  24414. + uint32_t data_size,
  24415. + VCHI_FLAGS_T flags,
  24416. + void *bulk_handle );
  24417. +
  24418. +// Routine to receive data
  24419. +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  24420. + void *data_dst,
  24421. + uint32_t data_size,
  24422. + VCHI_FLAGS_T flags,
  24423. + void *bulk_handle );
  24424. +
  24425. +// Routine to report if a server is available
  24426. +typedef int32_t (*VCHI_CONNECTION_SERVER_PRESENT)( VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t peer_flags );
  24427. +
  24428. +// Routine to report the number of RX slots available
  24429. +typedef int (*VCHI_CONNECTION_RX_SLOTS_AVAILABLE)( const VCHI_CONNECTION_STATE_T *state );
  24430. +
  24431. +// Routine to report the RX slot size
  24432. +typedef uint32_t (*VCHI_CONNECTION_RX_SLOT_SIZE)( const VCHI_CONNECTION_STATE_T *state );
  24433. +
  24434. +// Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
  24435. +typedef void (*VCHI_CONNECTION_RX_BULK_BUFFER_ADDED)(VCHI_CONNECTION_STATE_T *state,
  24436. + int32_t service,
  24437. + uint32_t length,
  24438. + MESSAGE_TX_CHANNEL_T channel,
  24439. + uint32_t channel_params,
  24440. + uint32_t data_length,
  24441. + uint32_t data_offset);
  24442. +
  24443. +// Callback to inform a service that a Xon or Xoff message has been received
  24444. +typedef void (*VCHI_CONNECTION_FLOW_CONTROL)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t xoff);
  24445. +
  24446. +// Callback to inform a service that a server available reply message has been received
  24447. +typedef void (*VCHI_CONNECTION_SERVER_AVAILABLE_REPLY)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, uint32_t flags);
  24448. +
  24449. +// Callback to indicate that bulk auxiliary messages have arrived
  24450. +typedef void (*VCHI_CONNECTION_BULK_AUX_RECEIVED)(VCHI_CONNECTION_STATE_T *state);
  24451. +
  24452. +// Callback to indicate that bulk auxiliary messages have arrived
  24453. +typedef void (*VCHI_CONNECTION_BULK_AUX_TRANSMITTED)(VCHI_CONNECTION_STATE_T *state, void *handle);
  24454. +
  24455. +// Callback with all the connection info you require
  24456. +typedef void (*VCHI_CONNECTION_INFO)(VCHI_CONNECTION_STATE_T *state, uint32_t protocol_version, uint32_t slot_size, uint32_t num_slots, uint32_t min_bulk_size);
  24457. +
  24458. +// Callback to inform of a disconnect
  24459. +typedef void (*VCHI_CONNECTION_DISCONNECT)(VCHI_CONNECTION_STATE_T *state, uint32_t flags);
  24460. +
  24461. +// Callback to inform of a power control request
  24462. +typedef void (*VCHI_CONNECTION_POWER_CONTROL)(VCHI_CONNECTION_STATE_T *state, MESSAGE_TX_CHANNEL_T channel, int32_t enable);
  24463. +
  24464. +// allocate memory suitably aligned for this connection
  24465. +typedef void * (*VCHI_BUFFER_ALLOCATE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, uint32_t * length);
  24466. +
  24467. +// free memory allocated by buffer_allocate
  24468. +typedef void (*VCHI_BUFFER_FREE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, void * address);
  24469. +
  24470. +
  24471. +/******************************************************************************
  24472. + System driver struct
  24473. + *****************************************************************************/
  24474. +
  24475. +struct opaque_vchi_connection_api_t
  24476. +{
  24477. + // Routine to init the connection
  24478. + VCHI_CONNECTION_INIT_T init;
  24479. +
  24480. + // Connection-level CRC control
  24481. + VCHI_CONNECTION_CRC_CONTROL_T crc_control;
  24482. +
  24483. + // Routine to connect to or create service
  24484. + VCHI_CONNECTION_SERVICE_CONNECT_T service_connect;
  24485. +
  24486. + // Routine to disconnect from a service
  24487. + VCHI_CONNECTION_SERVICE_DISCONNECT_T service_disconnect;
  24488. +
  24489. + // Routine to queue a message
  24490. + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T service_queue_msg;
  24491. +
  24492. + // scatter-gather (vector) message queue
  24493. + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T service_queue_msgv;
  24494. +
  24495. + // Routine to dequeue a message
  24496. + VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T service_dequeue_msg;
  24497. +
  24498. + // Routine to peek at a message
  24499. + VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T service_peek_msg;
  24500. +
  24501. + // Routine to hold a message
  24502. + VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T service_hold_msg;
  24503. +
  24504. + // Routine to initialise a received message iterator
  24505. + VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T service_look_ahead_msg;
  24506. +
  24507. + // Routine to release a message
  24508. + VCHI_CONNECTION_HELD_MSG_RELEASE_T held_msg_release;
  24509. +
  24510. + // Routine to get information on a held message
  24511. + VCHI_CONNECTION_HELD_MSG_INFO_T held_msg_info;
  24512. +
  24513. + // Routine to check for next message on iterator
  24514. + VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T msg_iter_has_next;
  24515. +
  24516. + // Routine to get next message on iterator
  24517. + VCHI_CONNECTION_MSG_ITER_NEXT_T msg_iter_next;
  24518. +
  24519. + // Routine to remove the last message returned by iterator
  24520. + VCHI_CONNECTION_MSG_ITER_REMOVE_T msg_iter_remove;
  24521. +
  24522. + // Routine to hold the last message returned by iterator
  24523. + VCHI_CONNECTION_MSG_ITER_HOLD_T msg_iter_hold;
  24524. +
  24525. + // Routine to transmit bulk data
  24526. + VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T bulk_queue_transmit;
  24527. +
  24528. + // Routine to receive data
  24529. + VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T bulk_queue_receive;
  24530. +
  24531. + // Routine to report the available servers
  24532. + VCHI_CONNECTION_SERVER_PRESENT server_present;
  24533. +
  24534. + // Routine to report the number of RX slots available
  24535. + VCHI_CONNECTION_RX_SLOTS_AVAILABLE connection_rx_slots_available;
  24536. +
  24537. + // Routine to report the RX slot size
  24538. + VCHI_CONNECTION_RX_SLOT_SIZE connection_rx_slot_size;
  24539. +
  24540. + // Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
  24541. + VCHI_CONNECTION_RX_BULK_BUFFER_ADDED rx_bulk_buffer_added;
  24542. +
  24543. + // Callback to inform a service that a Xon or Xoff message has been received
  24544. + VCHI_CONNECTION_FLOW_CONTROL flow_control;
  24545. +
  24546. + // Callback to inform a service that a server available reply message has been received
  24547. + VCHI_CONNECTION_SERVER_AVAILABLE_REPLY server_available_reply;
  24548. +
  24549. + // Callback to indicate that bulk auxiliary messages have arrived
  24550. + VCHI_CONNECTION_BULK_AUX_RECEIVED bulk_aux_received;
  24551. +
  24552. + // Callback to indicate that a bulk auxiliary message has been transmitted
  24553. + VCHI_CONNECTION_BULK_AUX_TRANSMITTED bulk_aux_transmitted;
  24554. +
  24555. + // Callback to provide information about the connection
  24556. + VCHI_CONNECTION_INFO connection_info;
  24557. +
  24558. + // Callback to notify that peer has requested disconnect
  24559. + VCHI_CONNECTION_DISCONNECT disconnect;
  24560. +
  24561. + // Callback to notify that peer has requested power change
  24562. + VCHI_CONNECTION_POWER_CONTROL power_control;
  24563. +
  24564. + // allocate memory suitably aligned for this connection
  24565. + VCHI_BUFFER_ALLOCATE buffer_allocate;
  24566. +
  24567. + // free memory allocated by buffer_allocate
  24568. + VCHI_BUFFER_FREE buffer_free;
  24569. +
  24570. +};
  24571. +
  24572. +struct vchi_connection_t {
  24573. + const VCHI_CONNECTION_API_T *api;
  24574. + VCHI_CONNECTION_STATE_T *state;
  24575. +#ifdef VCHI_COARSE_LOCKING
  24576. + struct semaphore sem;
  24577. +#endif
  24578. +};
  24579. +
  24580. +
  24581. +#endif /* CONNECTION_H_ */
  24582. +
  24583. +/****************************** End of file **********************************/
  24584. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h
  24585. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h 1969-12-31 18:00:00.000000000 -0600
  24586. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h 2014-12-03 19:13:38.224418001 -0600
  24587. @@ -0,0 +1,204 @@
  24588. +/**
  24589. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  24590. + *
  24591. + * Redistribution and use in source and binary forms, with or without
  24592. + * modification, are permitted provided that the following conditions
  24593. + * are met:
  24594. + * 1. Redistributions of source code must retain the above copyright
  24595. + * notice, this list of conditions, and the following disclaimer,
  24596. + * without modification.
  24597. + * 2. Redistributions in binary form must reproduce the above copyright
  24598. + * notice, this list of conditions and the following disclaimer in the
  24599. + * documentation and/or other materials provided with the distribution.
  24600. + * 3. The names of the above-listed copyright holders may not be used
  24601. + * to endorse or promote products derived from this software without
  24602. + * specific prior written permission.
  24603. + *
  24604. + * ALTERNATIVELY, this software may be distributed under the terms of the
  24605. + * GNU General Public License ("GPL") version 2, as published by the Free
  24606. + * Software Foundation.
  24607. + *
  24608. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24609. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24610. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24611. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24612. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24613. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24614. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24615. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24616. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24617. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24618. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24619. + */
  24620. +
  24621. +#ifndef _VCHI_MESSAGE_H_
  24622. +#define _VCHI_MESSAGE_H_
  24623. +
  24624. +#include <linux/kernel.h>
  24625. +#include <linux/types.h>
  24626. +#include <linux/semaphore.h>
  24627. +
  24628. +#include "interface/vchi/vchi_cfg_internal.h"
  24629. +#include "interface/vchi/vchi_common.h"
  24630. +
  24631. +
  24632. +typedef enum message_event_type {
  24633. + MESSAGE_EVENT_NONE,
  24634. + MESSAGE_EVENT_NOP,
  24635. + MESSAGE_EVENT_MESSAGE,
  24636. + MESSAGE_EVENT_SLOT_COMPLETE,
  24637. + MESSAGE_EVENT_RX_BULK_PAUSED,
  24638. + MESSAGE_EVENT_RX_BULK_COMPLETE,
  24639. + MESSAGE_EVENT_TX_COMPLETE,
  24640. + MESSAGE_EVENT_MSG_DISCARDED
  24641. +} MESSAGE_EVENT_TYPE_T;
  24642. +
  24643. +typedef enum vchi_msg_flags
  24644. +{
  24645. + VCHI_MSG_FLAGS_NONE = 0x0,
  24646. + VCHI_MSG_FLAGS_TERMINATE_DMA = 0x1
  24647. +} VCHI_MSG_FLAGS_T;
  24648. +
  24649. +typedef enum message_tx_channel
  24650. +{
  24651. + MESSAGE_TX_CHANNEL_MESSAGE = 0,
  24652. + MESSAGE_TX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
  24653. +} MESSAGE_TX_CHANNEL_T;
  24654. +
  24655. +// Macros used for cycling through bulk channels
  24656. +#define MESSAGE_TX_CHANNEL_BULK_PREV(c) (MESSAGE_TX_CHANNEL_BULK+((c)-MESSAGE_TX_CHANNEL_BULK+VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION-1)%VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION)
  24657. +#define MESSAGE_TX_CHANNEL_BULK_NEXT(c) (MESSAGE_TX_CHANNEL_BULK+((c)-MESSAGE_TX_CHANNEL_BULK+1)%VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION)
  24658. +
  24659. +typedef enum message_rx_channel
  24660. +{
  24661. + MESSAGE_RX_CHANNEL_MESSAGE = 0,
  24662. + MESSAGE_RX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
  24663. +} MESSAGE_RX_CHANNEL_T;
  24664. +
  24665. +// Message receive slot information
  24666. +typedef struct rx_msg_slot_info {
  24667. +
  24668. + struct rx_msg_slot_info *next;
  24669. + //struct slot_info *prev;
  24670. +#if !defined VCHI_COARSE_LOCKING
  24671. + struct semaphore sem;
  24672. +#endif
  24673. +
  24674. + uint8_t *addr; // base address of slot
  24675. + uint32_t len; // length of slot in bytes
  24676. +
  24677. + uint32_t write_ptr; // hardware causes this to advance
  24678. + uint32_t read_ptr; // this module does the reading
  24679. + int active; // is this slot in the hardware dma fifo?
  24680. + uint32_t msgs_parsed; // count how many messages are in this slot
  24681. + uint32_t msgs_released; // how many messages have been released
  24682. + void *state; // connection state information
  24683. + uint8_t ref_count[VCHI_MAX_SERVICES_PER_CONNECTION]; // reference count for slots held by services
  24684. +} RX_MSG_SLOTINFO_T;
  24685. +
  24686. +// The message driver no longer needs to know about the fields of RX_BULK_SLOTINFO_T - sort this out.
  24687. +// In particular, it mustn't use addr and len - they're the client buffer, but the message
  24688. +// driver will be tasked with sending the aligned core section.
  24689. +typedef struct rx_bulk_slotinfo_t {
  24690. + struct rx_bulk_slotinfo_t *next;
  24691. +
  24692. + struct semaphore *blocking;
  24693. +
  24694. + // needed by DMA
  24695. + void *addr;
  24696. + uint32_t len;
  24697. +
  24698. + // needed for the callback
  24699. + void *service;
  24700. + void *handle;
  24701. + VCHI_FLAGS_T flags;
  24702. +} RX_BULK_SLOTINFO_T;
  24703. +
  24704. +
  24705. +/* ----------------------------------------------------------------------
  24706. + * each connection driver will have a pool of the following struct.
  24707. + *
  24708. + * the pool will be managed by vchi_qman_*
  24709. + * this means there will be multiple queues (single linked lists)
  24710. + * a given struct message_info will be on exactly one of these queues
  24711. + * at any one time
  24712. + * -------------------------------------------------------------------- */
  24713. +typedef struct rx_message_info {
  24714. +
  24715. + struct message_info *next;
  24716. + //struct message_info *prev;
  24717. +
  24718. + uint8_t *addr;
  24719. + uint32_t len;
  24720. + RX_MSG_SLOTINFO_T *slot; // points to whichever slot contains this message
  24721. + uint32_t tx_timestamp;
  24722. + uint32_t rx_timestamp;
  24723. +
  24724. +} RX_MESSAGE_INFO_T;
  24725. +
  24726. +typedef struct {
  24727. + MESSAGE_EVENT_TYPE_T type;
  24728. +
  24729. + struct {
  24730. + // for messages
  24731. + void *addr; // address of message
  24732. + uint16_t slot_delta; // whether this message indicated slot delta
  24733. + uint32_t len; // length of message
  24734. + RX_MSG_SLOTINFO_T *slot; // slot this message is in
  24735. + int32_t service; // service id this message is destined for
  24736. + uint32_t tx_timestamp; // timestamp from the header
  24737. + uint32_t rx_timestamp; // timestamp when we parsed it
  24738. + } message;
  24739. +
  24740. + // FIXME: cleanup slot reporting...
  24741. + RX_MSG_SLOTINFO_T *rx_msg;
  24742. + RX_BULK_SLOTINFO_T *rx_bulk;
  24743. + void *tx_handle;
  24744. + MESSAGE_TX_CHANNEL_T tx_channel;
  24745. +
  24746. +} MESSAGE_EVENT_T;
  24747. +
  24748. +
  24749. +// callbacks
  24750. +typedef void VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T( void *state );
  24751. +
  24752. +typedef struct {
  24753. + VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T *event_callback;
  24754. +} VCHI_MESSAGE_DRIVER_OPEN_T;
  24755. +
  24756. +
  24757. +// handle to this instance of message driver (as returned by ->open)
  24758. +typedef struct opaque_mhandle_t *VCHI_MDRIVER_HANDLE_T;
  24759. +
  24760. +struct opaque_vchi_message_driver_t {
  24761. + VCHI_MDRIVER_HANDLE_T *(*open)( VCHI_MESSAGE_DRIVER_OPEN_T *params, void *state );
  24762. + int32_t (*suspending)( VCHI_MDRIVER_HANDLE_T *handle );
  24763. + int32_t (*resumed)( VCHI_MDRIVER_HANDLE_T *handle );
  24764. + int32_t (*power_control)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T, int32_t enable );
  24765. + int32_t (*add_msg_rx_slot)( VCHI_MDRIVER_HANDLE_T *handle, RX_MSG_SLOTINFO_T *slot ); // rx message
  24766. + int32_t (*add_bulk_rx)( VCHI_MDRIVER_HANDLE_T *handle, void *data, uint32_t len, RX_BULK_SLOTINFO_T *slot ); // rx data (bulk)
  24767. + int32_t (*send)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel, const void *data, uint32_t len, VCHI_MSG_FLAGS_T flags, void *send_handle ); // tx (message & bulk)
  24768. + void (*next_event)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_EVENT_T *event ); // get the next event from message_driver
  24769. + int32_t (*enable)( VCHI_MDRIVER_HANDLE_T *handle );
  24770. + int32_t (*form_message)( VCHI_MDRIVER_HANDLE_T *handle, int32_t service_id, VCHI_MSG_VECTOR_T *vector, uint32_t count, void
  24771. + *address, uint32_t length_avail, uint32_t max_total_length, int32_t pad_to_fill, int32_t allow_partial );
  24772. +
  24773. + int32_t (*update_message)( VCHI_MDRIVER_HANDLE_T *handle, void *dest, int16_t *slot_count );
  24774. + int32_t (*buffer_aligned)( VCHI_MDRIVER_HANDLE_T *handle, int tx, int uncached, const void *address, const uint32_t length );
  24775. + void * (*allocate_buffer)( VCHI_MDRIVER_HANDLE_T *handle, uint32_t *length );
  24776. + void (*free_buffer)( VCHI_MDRIVER_HANDLE_T *handle, void *address );
  24777. + int (*rx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
  24778. + int (*tx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
  24779. +
  24780. + int32_t (*tx_supports_terminate)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  24781. + uint32_t (*tx_bulk_chunk_size)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  24782. + int (*tx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  24783. + int (*rx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_RX_CHANNEL_T channel );
  24784. + void (*form_bulk_aux)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel, const void *data, uint32_t len, uint32_t chunk_size, const void **aux_data, int32_t *aux_len );
  24785. + void (*debug)( VCHI_MDRIVER_HANDLE_T *handle );
  24786. +};
  24787. +
  24788. +
  24789. +#endif // _VCHI_MESSAGE_H_
  24790. +
  24791. +/****************************** End of file ***********************************/
  24792. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h
  24793. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h 1969-12-31 18:00:00.000000000 -0600
  24794. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h 2014-12-03 19:13:38.224418001 -0600
  24795. @@ -0,0 +1,224 @@
  24796. +/**
  24797. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  24798. + *
  24799. + * Redistribution and use in source and binary forms, with or without
  24800. + * modification, are permitted provided that the following conditions
  24801. + * are met:
  24802. + * 1. Redistributions of source code must retain the above copyright
  24803. + * notice, this list of conditions, and the following disclaimer,
  24804. + * without modification.
  24805. + * 2. Redistributions in binary form must reproduce the above copyright
  24806. + * notice, this list of conditions and the following disclaimer in the
  24807. + * documentation and/or other materials provided with the distribution.
  24808. + * 3. The names of the above-listed copyright holders may not be used
  24809. + * to endorse or promote products derived from this software without
  24810. + * specific prior written permission.
  24811. + *
  24812. + * ALTERNATIVELY, this software may be distributed under the terms of the
  24813. + * GNU General Public License ("GPL") version 2, as published by the Free
  24814. + * Software Foundation.
  24815. + *
  24816. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24817. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24818. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24819. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24820. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24821. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24822. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24823. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24824. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24825. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24826. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24827. + */
  24828. +
  24829. +#ifndef VCHI_CFG_H_
  24830. +#define VCHI_CFG_H_
  24831. +
  24832. +/****************************************************************************************
  24833. + * Defines in this first section are part of the VCHI API and may be examined by VCHI
  24834. + * services.
  24835. + ***************************************************************************************/
  24836. +
  24837. +/* Required alignment of base addresses for bulk transfer, if unaligned transfers are not enabled */
  24838. +/* Really determined by the message driver, and should be available from a run-time call. */
  24839. +#ifndef VCHI_BULK_ALIGN
  24840. +# if __VCCOREVER__ >= 0x04000000
  24841. +# define VCHI_BULK_ALIGN 32 // Allows for the need to do cache cleans
  24842. +# else
  24843. +# define VCHI_BULK_ALIGN 16
  24844. +# endif
  24845. +#endif
  24846. +
  24847. +/* Required length multiple for bulk transfers, if unaligned transfers are not enabled */
  24848. +/* May be less than or greater than VCHI_BULK_ALIGN */
  24849. +/* Really determined by the message driver, and should be available from a run-time call. */
  24850. +#ifndef VCHI_BULK_GRANULARITY
  24851. +# if __VCCOREVER__ >= 0x04000000
  24852. +# define VCHI_BULK_GRANULARITY 32 // Allows for the need to do cache cleans
  24853. +# else
  24854. +# define VCHI_BULK_GRANULARITY 16
  24855. +# endif
  24856. +#endif
  24857. +
  24858. +/* The largest possible message to be queued with vchi_msg_queue. */
  24859. +#ifndef VCHI_MAX_MSG_SIZE
  24860. +# if defined VCHI_LOCAL_HOST_PORT
  24861. +# define VCHI_MAX_MSG_SIZE 16384 // makes file transfers fast, but should they be using bulk?
  24862. +# else
  24863. +# define VCHI_MAX_MSG_SIZE 4096 // NOTE: THIS MUST BE LARGER THAN OR EQUAL TO THE SIZE OF THE KHRONOS MERGE BUFFER!!
  24864. +# endif
  24865. +#endif
  24866. +
  24867. +/******************************************************************************************
  24868. + * Defines below are system configuration options, and should not be used by VCHI services.
  24869. + *****************************************************************************************/
  24870. +
  24871. +/* How many connections can we support? A localhost implementation uses 2 connections,
  24872. + * 1 for host-app, 1 for VMCS, and these are hooked together by a loopback MPHI VCFW
  24873. + * driver. */
  24874. +#ifndef VCHI_MAX_NUM_CONNECTIONS
  24875. +# define VCHI_MAX_NUM_CONNECTIONS 3
  24876. +#endif
  24877. +
  24878. +/* How many services can we open per connection? Extending this doesn't cost processing time, just a small
  24879. + * amount of static memory. */
  24880. +#ifndef VCHI_MAX_SERVICES_PER_CONNECTION
  24881. +# define VCHI_MAX_SERVICES_PER_CONNECTION 36
  24882. +#endif
  24883. +
  24884. +/* Adjust if using a message driver that supports more logical TX channels */
  24885. +#ifndef VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION
  24886. +# define VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION 9 // 1 MPHI + 8 CCP2 logical channels
  24887. +#endif
  24888. +
  24889. +/* Adjust if using a message driver that supports more logical RX channels */
  24890. +#ifndef VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION
  24891. +# define VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION 1 // 1 MPHI
  24892. +#endif
  24893. +
  24894. +/* How many receive slots do we use. This times VCHI_MAX_MSG_SIZE gives the effective
  24895. + * receive queue space, less message headers. */
  24896. +#ifndef VCHI_NUM_READ_SLOTS
  24897. +# if defined(VCHI_LOCAL_HOST_PORT)
  24898. +# define VCHI_NUM_READ_SLOTS 4
  24899. +# else
  24900. +# define VCHI_NUM_READ_SLOTS 48
  24901. +# endif
  24902. +#endif
  24903. +
  24904. +/* Do we utilise overrun facility for receive message slots? Can aid peer transmit
  24905. + * performance. Only define on VideoCore end, talking to host.
  24906. + */
  24907. +//#define VCHI_MSG_RX_OVERRUN
  24908. +
  24909. +/* How many transmit slots do we use. Generally don't need many, as the hardware driver
  24910. + * underneath VCHI will usually have its own buffering. */
  24911. +#ifndef VCHI_NUM_WRITE_SLOTS
  24912. +# define VCHI_NUM_WRITE_SLOTS 4
  24913. +#endif
  24914. +
  24915. +/* If a service has held or queued received messages in VCHI_XOFF_THRESHOLD or more slots,
  24916. + * then it's taking up too much buffer space, and the peer service will be told to stop
  24917. + * transmitting with an XOFF message. For this to be effective, the VCHI_NUM_READ_SLOTS
  24918. + * needs to be considerably bigger than VCHI_NUM_WRITE_SLOTS, or the transmit latency
  24919. + * is too high. */
  24920. +#ifndef VCHI_XOFF_THRESHOLD
  24921. +# define VCHI_XOFF_THRESHOLD (VCHI_NUM_READ_SLOTS / 2)
  24922. +#endif
  24923. +
  24924. +/* After we've sent an XOFF, the peer will be told to resume transmission once the local
  24925. + * service has dequeued/released enough messages that it's now occupying
  24926. + * VCHI_XON_THRESHOLD slots or fewer. */
  24927. +#ifndef VCHI_XON_THRESHOLD
  24928. +# define VCHI_XON_THRESHOLD (VCHI_NUM_READ_SLOTS / 4)
  24929. +#endif
  24930. +
  24931. +/* A size below which a bulk transfer omits the handshake completely and always goes
  24932. + * via the message channel, if bulk auxiliary is being sent on that service. (The user
  24933. + * can guarantee this by enabling unaligned transmits).
  24934. + * Not API. */
  24935. +#ifndef VCHI_MIN_BULK_SIZE
  24936. +# define VCHI_MIN_BULK_SIZE ( VCHI_MAX_MSG_SIZE / 2 < 4096 ? VCHI_MAX_MSG_SIZE / 2 : 4096 )
  24937. +#endif
  24938. +
  24939. +/* Maximum size of bulk transmission chunks, for each interface type. A trade-off between
  24940. + * speed and latency; the smaller the chunk size the better change of messages and other
  24941. + * bulk transmissions getting in when big bulk transfers are happening. Set to 0 to not
  24942. + * break transmissions into chunks.
  24943. + */
  24944. +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_MPHI
  24945. +# define VCHI_MAX_BULK_CHUNK_SIZE_MPHI (16 * 1024)
  24946. +#endif
  24947. +
  24948. +/* NB Chunked CCP2 transmissions violate the letter of the CCP2 spec by using "JPEG8" mode
  24949. + * with multiple-line frames. Only use if the receiver can cope. */
  24950. +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_CCP2
  24951. +# define VCHI_MAX_BULK_CHUNK_SIZE_CCP2 0
  24952. +#endif
  24953. +
  24954. +/* How many TX messages can we have pending in our transmit slots. Once exhausted,
  24955. + * vchi_msg_queue will be blocked. */
  24956. +#ifndef VCHI_TX_MSG_QUEUE_SIZE
  24957. +# define VCHI_TX_MSG_QUEUE_SIZE 256
  24958. +#endif
  24959. +
  24960. +/* How many RX messages can we have parsed in the receive slots. Once exhausted, parsing
  24961. + * will be suspended until older messages are dequeued/released. */
  24962. +#ifndef VCHI_RX_MSG_QUEUE_SIZE
  24963. +# define VCHI_RX_MSG_QUEUE_SIZE 256
  24964. +#endif
  24965. +
  24966. +/* Really should be able to cope if we run out of received message descriptors, by
  24967. + * suspending parsing as the comment above says, but we don't. This sweeps the issue
  24968. + * under the carpet. */
  24969. +#if VCHI_RX_MSG_QUEUE_SIZE < (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
  24970. +# undef VCHI_RX_MSG_QUEUE_SIZE
  24971. +# define VCHI_RX_MSG_QUEUE_SIZE (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
  24972. +#endif
  24973. +
  24974. +/* How many bulk transmits can we have pending. Once exhausted, vchi_bulk_queue_transmit
  24975. + * will be blocked. */
  24976. +#ifndef VCHI_TX_BULK_QUEUE_SIZE
  24977. +# define VCHI_TX_BULK_QUEUE_SIZE 64
  24978. +#endif
  24979. +
  24980. +/* How many bulk receives can we have pending. Once exhausted, vchi_bulk_queue_receive
  24981. + * will be blocked. */
  24982. +#ifndef VCHI_RX_BULK_QUEUE_SIZE
  24983. +# define VCHI_RX_BULK_QUEUE_SIZE 64
  24984. +#endif
  24985. +
  24986. +/* A limit on how many outstanding bulk requests we expect the peer to give us. If
  24987. + * the peer asks for more than this, VCHI will fail and assert. The number is determined
  24988. + * by the peer's hardware - it's the number of outstanding requests that can be queued
  24989. + * on all bulk channels. VC3's MPHI peripheral allows 16. */
  24990. +#ifndef VCHI_MAX_PEER_BULK_REQUESTS
  24991. +# define VCHI_MAX_PEER_BULK_REQUESTS 32
  24992. +#endif
  24993. +
  24994. +/* Define VCHI_CCP2TX_MANUAL_POWER if the host tells us when to turn the CCP2
  24995. + * transmitter on and off.
  24996. + */
  24997. +/*#define VCHI_CCP2TX_MANUAL_POWER*/
  24998. +
  24999. +#ifndef VCHI_CCP2TX_MANUAL_POWER
  25000. +
  25001. +/* Timeout (in milliseconds) for putting the CCP2TX interface into IDLE state. Set
  25002. + * negative for no IDLE.
  25003. + */
  25004. +# ifndef VCHI_CCP2TX_IDLE_TIMEOUT
  25005. +# define VCHI_CCP2TX_IDLE_TIMEOUT 5
  25006. +# endif
  25007. +
  25008. +/* Timeout (in milliseconds) for putting the CCP2TX interface into OFF state. Set
  25009. + * negative for no OFF.
  25010. + */
  25011. +# ifndef VCHI_CCP2TX_OFF_TIMEOUT
  25012. +# define VCHI_CCP2TX_OFF_TIMEOUT 1000
  25013. +# endif
  25014. +
  25015. +#endif /* VCHI_CCP2TX_MANUAL_POWER */
  25016. +
  25017. +#endif /* VCHI_CFG_H_ */
  25018. +
  25019. +/****************************** End of file **********************************/
  25020. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h
  25021. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h 1969-12-31 18:00:00.000000000 -0600
  25022. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h 2014-12-03 19:13:38.224418001 -0600
  25023. @@ -0,0 +1,71 @@
  25024. +/**
  25025. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  25026. + *
  25027. + * Redistribution and use in source and binary forms, with or without
  25028. + * modification, are permitted provided that the following conditions
  25029. + * are met:
  25030. + * 1. Redistributions of source code must retain the above copyright
  25031. + * notice, this list of conditions, and the following disclaimer,
  25032. + * without modification.
  25033. + * 2. Redistributions in binary form must reproduce the above copyright
  25034. + * notice, this list of conditions and the following disclaimer in the
  25035. + * documentation and/or other materials provided with the distribution.
  25036. + * 3. The names of the above-listed copyright holders may not be used
  25037. + * to endorse or promote products derived from this software without
  25038. + * specific prior written permission.
  25039. + *
  25040. + * ALTERNATIVELY, this software may be distributed under the terms of the
  25041. + * GNU General Public License ("GPL") version 2, as published by the Free
  25042. + * Software Foundation.
  25043. + *
  25044. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  25045. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  25046. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  25047. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  25048. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25049. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25050. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25051. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25052. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25053. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25054. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25055. + */
  25056. +
  25057. +#ifndef VCHI_CFG_INTERNAL_H_
  25058. +#define VCHI_CFG_INTERNAL_H_
  25059. +
  25060. +/****************************************************************************************
  25061. + * Control optimisation attempts.
  25062. + ***************************************************************************************/
  25063. +
  25064. +// Don't use lots of short-term locks - use great long ones, reducing the overall locks-per-second
  25065. +#define VCHI_COARSE_LOCKING
  25066. +
  25067. +// Avoid lock then unlock on exit from blocking queue operations (msg tx, bulk rx/tx)
  25068. +// (only relevant if VCHI_COARSE_LOCKING)
  25069. +#define VCHI_ELIDE_BLOCK_EXIT_LOCK
  25070. +
  25071. +// Avoid lock on non-blocking peek
  25072. +// (only relevant if VCHI_COARSE_LOCKING)
  25073. +#define VCHI_AVOID_PEEK_LOCK
  25074. +
  25075. +// Use one slot-handler thread per connection, rather than 1 thread dealing with all connections in rotation.
  25076. +#define VCHI_MULTIPLE_HANDLER_THREADS
  25077. +
  25078. +// Put free descriptors onto the head of the free queue, rather than the tail, so that we don't thrash
  25079. +// our way through the pool of descriptors.
  25080. +#define VCHI_PUSH_FREE_DESCRIPTORS_ONTO_HEAD
  25081. +
  25082. +// Don't issue a MSG_AVAILABLE callback for every single message. Possibly only safe if VCHI_COARSE_LOCKING.
  25083. +#define VCHI_FEWER_MSG_AVAILABLE_CALLBACKS
  25084. +
  25085. +// Don't use message descriptors for TX messages that don't need them
  25086. +#define VCHI_MINIMISE_TX_MSG_DESCRIPTORS
  25087. +
  25088. +// Nano-locks for multiqueue
  25089. +//#define VCHI_MQUEUE_NANOLOCKS
  25090. +
  25091. +// Lock-free(er) dequeuing
  25092. +//#define VCHI_RX_NANOLOCKS
  25093. +
  25094. +#endif /*VCHI_CFG_INTERNAL_H_*/
  25095. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchi/vchi_common.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchi/vchi_common.h
  25096. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchi/vchi_common.h 1969-12-31 18:00:00.000000000 -0600
  25097. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchi/vchi_common.h 2014-12-03 19:13:38.224418001 -0600
  25098. @@ -0,0 +1,174 @@
  25099. +/**
  25100. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  25101. + *
  25102. + * Redistribution and use in source and binary forms, with or without
  25103. + * modification, are permitted provided that the following conditions
  25104. + * are met:
  25105. + * 1. Redistributions of source code must retain the above copyright
  25106. + * notice, this list of conditions, and the following disclaimer,
  25107. + * without modification.
  25108. + * 2. Redistributions in binary form must reproduce the above copyright
  25109. + * notice, this list of conditions and the following disclaimer in the
  25110. + * documentation and/or other materials provided with the distribution.
  25111. + * 3. The names of the above-listed copyright holders may not be used
  25112. + * to endorse or promote products derived from this software without
  25113. + * specific prior written permission.
  25114. + *
  25115. + * ALTERNATIVELY, this software may be distributed under the terms of the
  25116. + * GNU General Public License ("GPL") version 2, as published by the Free
  25117. + * Software Foundation.
  25118. + *
  25119. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  25120. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  25121. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  25122. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  25123. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25124. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25125. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25126. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25127. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25128. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25129. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25130. + */
  25131. +
  25132. +#ifndef VCHI_COMMON_H_
  25133. +#define VCHI_COMMON_H_
  25134. +
  25135. +
  25136. +//flags used when sending messages (must be bitmapped)
  25137. +typedef enum
  25138. +{
  25139. + VCHI_FLAGS_NONE = 0x0,
  25140. + VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE = 0x1, // waits for message to be received, or sent (NB. not the same as being seen on other side)
  25141. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE = 0x2, // run a callback when message sent
  25142. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED = 0x4, // return once the transfer is in a queue ready to go
  25143. + VCHI_FLAGS_ALLOW_PARTIAL = 0x8,
  25144. + VCHI_FLAGS_BLOCK_UNTIL_DATA_READ = 0x10,
  25145. + VCHI_FLAGS_CALLBACK_WHEN_DATA_READ = 0x20,
  25146. +
  25147. + VCHI_FLAGS_ALIGN_SLOT = 0x000080, // internal use only
  25148. + VCHI_FLAGS_BULK_AUX_QUEUED = 0x010000, // internal use only
  25149. + VCHI_FLAGS_BULK_AUX_COMPLETE = 0x020000, // internal use only
  25150. + VCHI_FLAGS_BULK_DATA_QUEUED = 0x040000, // internal use only
  25151. + VCHI_FLAGS_BULK_DATA_COMPLETE = 0x080000, // internal use only
  25152. + VCHI_FLAGS_INTERNAL = 0xFF0000
  25153. +} VCHI_FLAGS_T;
  25154. +
  25155. +// constants for vchi_crc_control()
  25156. +typedef enum {
  25157. + VCHI_CRC_NOTHING = -1,
  25158. + VCHI_CRC_PER_SERVICE = 0,
  25159. + VCHI_CRC_EVERYTHING = 1,
  25160. +} VCHI_CRC_CONTROL_T;
  25161. +
  25162. +//callback reasons when an event occurs on a service
  25163. +typedef enum
  25164. +{
  25165. + VCHI_CALLBACK_REASON_MIN,
  25166. +
  25167. + //This indicates that there is data available
  25168. + //handle is the msg id that was transmitted with the data
  25169. + // When a message is received and there was no FULL message available previously, send callback
  25170. + // Tasks get kicked by the callback, reset their event and try and read from the fifo until it fails
  25171. + VCHI_CALLBACK_MSG_AVAILABLE,
  25172. + VCHI_CALLBACK_MSG_SENT,
  25173. + VCHI_CALLBACK_MSG_SPACE_AVAILABLE, // XXX not yet implemented
  25174. +
  25175. + // This indicates that a transfer from the other side has completed
  25176. + VCHI_CALLBACK_BULK_RECEIVED,
  25177. + //This indicates that data queued up to be sent has now gone
  25178. + //handle is the msg id that was used when sending the data
  25179. + VCHI_CALLBACK_BULK_SENT,
  25180. + VCHI_CALLBACK_BULK_RX_SPACE_AVAILABLE, // XXX not yet implemented
  25181. + VCHI_CALLBACK_BULK_TX_SPACE_AVAILABLE, // XXX not yet implemented
  25182. +
  25183. + VCHI_CALLBACK_SERVICE_CLOSED,
  25184. +
  25185. + // this side has sent XOFF to peer due to lack of data consumption by service
  25186. + // (suggests the service may need to take some recovery action if it has
  25187. + // been deliberately holding off consuming data)
  25188. + VCHI_CALLBACK_SENT_XOFF,
  25189. + VCHI_CALLBACK_SENT_XON,
  25190. +
  25191. + // indicates that a bulk transfer has finished reading the source buffer
  25192. + VCHI_CALLBACK_BULK_DATA_READ,
  25193. +
  25194. + // power notification events (currently host side only)
  25195. + VCHI_CALLBACK_PEER_OFF,
  25196. + VCHI_CALLBACK_PEER_SUSPENDED,
  25197. + VCHI_CALLBACK_PEER_ON,
  25198. + VCHI_CALLBACK_PEER_RESUMED,
  25199. + VCHI_CALLBACK_FORCED_POWER_OFF,
  25200. +
  25201. +#ifdef USE_VCHIQ_ARM
  25202. + // some extra notifications provided by vchiq_arm
  25203. + VCHI_CALLBACK_SERVICE_OPENED,
  25204. + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
  25205. + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
  25206. +#endif
  25207. +
  25208. + VCHI_CALLBACK_REASON_MAX
  25209. +} VCHI_CALLBACK_REASON_T;
  25210. +
  25211. +// service control options
  25212. +typedef enum
  25213. +{
  25214. + VCHI_SERVICE_OPTION_MIN,
  25215. +
  25216. + VCHI_SERVICE_OPTION_TRACE,
  25217. +
  25218. + VCHI_SERVICE_OPTION_MAX
  25219. +} VCHI_SERVICE_OPTION_T;
  25220. +
  25221. +
  25222. +//Callback used by all services / bulk transfers
  25223. +typedef void (*VCHI_CALLBACK_T)( void *callback_param, //my service local param
  25224. + VCHI_CALLBACK_REASON_T reason,
  25225. + void *handle ); //for transmitting msg's only
  25226. +
  25227. +
  25228. +
  25229. +/*
  25230. + * Define vector struct for scatter-gather (vector) operations
  25231. + * Vectors can be nested - if a vector element has negative length, then
  25232. + * the data pointer is treated as pointing to another vector array, with
  25233. + * '-vec_len' elements. Thus to append a header onto an existing vector,
  25234. + * you can do this:
  25235. + *
  25236. + * void foo(const VCHI_MSG_VECTOR_T *v, int n)
  25237. + * {
  25238. + * VCHI_MSG_VECTOR_T nv[2];
  25239. + * nv[0].vec_base = my_header;
  25240. + * nv[0].vec_len = sizeof my_header;
  25241. + * nv[1].vec_base = v;
  25242. + * nv[1].vec_len = -n;
  25243. + * ...
  25244. + *
  25245. + */
  25246. +typedef struct vchi_msg_vector {
  25247. + const void *vec_base;
  25248. + int32_t vec_len;
  25249. +} VCHI_MSG_VECTOR_T;
  25250. +
  25251. +// Opaque type for a connection API
  25252. +typedef struct opaque_vchi_connection_api_t VCHI_CONNECTION_API_T;
  25253. +
  25254. +// Opaque type for a message driver
  25255. +typedef struct opaque_vchi_message_driver_t VCHI_MESSAGE_DRIVER_T;
  25256. +
  25257. +
  25258. +// Iterator structure for reading ahead through received message queue. Allocated by client,
  25259. +// initialised by vchi_msg_look_ahead. Fields are for internal VCHI use only.
  25260. +// Iterates over messages in queue at the instant of the call to vchi_msg_lookahead -
  25261. +// will not proceed to messages received since. Behaviour is undefined if an iterator
  25262. +// is used again after messages for that service are removed/dequeued by any
  25263. +// means other than vchi_msg_iter_... calls on the iterator itself.
  25264. +typedef struct {
  25265. + struct opaque_vchi_service_t *service;
  25266. + void *last;
  25267. + void *next;
  25268. + void *remove;
  25269. +} VCHI_MSG_ITER_T;
  25270. +
  25271. +
  25272. +#endif // VCHI_COMMON_H_
  25273. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchi/vchi.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchi/vchi.h
  25274. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchi/vchi.h 1969-12-31 18:00:00.000000000 -0600
  25275. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchi/vchi.h 2014-12-03 19:13:38.224418001 -0600
  25276. @@ -0,0 +1,378 @@
  25277. +/**
  25278. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  25279. + *
  25280. + * Redistribution and use in source and binary forms, with or without
  25281. + * modification, are permitted provided that the following conditions
  25282. + * are met:
  25283. + * 1. Redistributions of source code must retain the above copyright
  25284. + * notice, this list of conditions, and the following disclaimer,
  25285. + * without modification.
  25286. + * 2. Redistributions in binary form must reproduce the above copyright
  25287. + * notice, this list of conditions and the following disclaimer in the
  25288. + * documentation and/or other materials provided with the distribution.
  25289. + * 3. The names of the above-listed copyright holders may not be used
  25290. + * to endorse or promote products derived from this software without
  25291. + * specific prior written permission.
  25292. + *
  25293. + * ALTERNATIVELY, this software may be distributed under the terms of the
  25294. + * GNU General Public License ("GPL") version 2, as published by the Free
  25295. + * Software Foundation.
  25296. + *
  25297. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  25298. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  25299. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  25300. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  25301. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25302. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25303. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25304. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25305. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25306. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25307. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25308. + */
  25309. +
  25310. +#ifndef VCHI_H_
  25311. +#define VCHI_H_
  25312. +
  25313. +#include "interface/vchi/vchi_cfg.h"
  25314. +#include "interface/vchi/vchi_common.h"
  25315. +#include "interface/vchi/connections/connection.h"
  25316. +#include "vchi_mh.h"
  25317. +
  25318. +
  25319. +/******************************************************************************
  25320. + Global defs
  25321. + *****************************************************************************/
  25322. +
  25323. +#define VCHI_BULK_ROUND_UP(x) ((((unsigned long)(x))+VCHI_BULK_ALIGN-1) & ~(VCHI_BULK_ALIGN-1))
  25324. +#define VCHI_BULK_ROUND_DOWN(x) (((unsigned long)(x)) & ~(VCHI_BULK_ALIGN-1))
  25325. +#define VCHI_BULK_ALIGN_NBYTES(x) (VCHI_BULK_ALIGNED(x) ? 0 : (VCHI_BULK_ALIGN - ((unsigned long)(x) & (VCHI_BULK_ALIGN-1))))
  25326. +
  25327. +#ifdef USE_VCHIQ_ARM
  25328. +#define VCHI_BULK_ALIGNED(x) 1
  25329. +#else
  25330. +#define VCHI_BULK_ALIGNED(x) (((unsigned long)(x) & (VCHI_BULK_ALIGN-1)) == 0)
  25331. +#endif
  25332. +
  25333. +struct vchi_version {
  25334. + uint32_t version;
  25335. + uint32_t version_min;
  25336. +};
  25337. +#define VCHI_VERSION(v_) { v_, v_ }
  25338. +#define VCHI_VERSION_EX(v_, m_) { v_, m_ }
  25339. +
  25340. +typedef enum
  25341. +{
  25342. + VCHI_VEC_POINTER,
  25343. + VCHI_VEC_HANDLE,
  25344. + VCHI_VEC_LIST
  25345. +} VCHI_MSG_VECTOR_TYPE_T;
  25346. +
  25347. +typedef struct vchi_msg_vector_ex {
  25348. +
  25349. + VCHI_MSG_VECTOR_TYPE_T type;
  25350. + union
  25351. + {
  25352. + // a memory handle
  25353. + struct
  25354. + {
  25355. + VCHI_MEM_HANDLE_T handle;
  25356. + uint32_t offset;
  25357. + int32_t vec_len;
  25358. + } handle;
  25359. +
  25360. + // an ordinary data pointer
  25361. + struct
  25362. + {
  25363. + const void *vec_base;
  25364. + int32_t vec_len;
  25365. + } ptr;
  25366. +
  25367. + // a nested vector list
  25368. + struct
  25369. + {
  25370. + struct vchi_msg_vector_ex *vec;
  25371. + uint32_t vec_len;
  25372. + } list;
  25373. + } u;
  25374. +} VCHI_MSG_VECTOR_EX_T;
  25375. +
  25376. +
  25377. +// Construct an entry in a msg vector for a pointer (p) of length (l)
  25378. +#define VCHI_VEC_POINTER(p,l) VCHI_VEC_POINTER, { { (VCHI_MEM_HANDLE_T)(p), (l) } }
  25379. +
  25380. +// Construct an entry in a msg vector for a message handle (h), starting at offset (o) of length (l)
  25381. +#define VCHI_VEC_HANDLE(h,o,l) VCHI_VEC_HANDLE, { { (h), (o), (l) } }
  25382. +
  25383. +// Macros to manipulate 'FOURCC' values
  25384. +#define MAKE_FOURCC(x) ((int32_t)( (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3] ))
  25385. +#define FOURCC_TO_CHAR(x) (x >> 24) & 0xFF,(x >> 16) & 0xFF,(x >> 8) & 0xFF, x & 0xFF
  25386. +
  25387. +
  25388. +// Opaque service information
  25389. +struct opaque_vchi_service_t;
  25390. +
  25391. +// Descriptor for a held message. Allocated by client, initialised by vchi_msg_hold,
  25392. +// vchi_msg_iter_hold or vchi_msg_iter_hold_next. Fields are for internal VCHI use only.
  25393. +typedef struct
  25394. +{
  25395. + struct opaque_vchi_service_t *service;
  25396. + void *message;
  25397. +} VCHI_HELD_MSG_T;
  25398. +
  25399. +
  25400. +
  25401. +// structure used to provide the information needed to open a server or a client
  25402. +typedef struct {
  25403. + struct vchi_version version;
  25404. + int32_t service_id;
  25405. + VCHI_CONNECTION_T *connection;
  25406. + uint32_t rx_fifo_size;
  25407. + uint32_t tx_fifo_size;
  25408. + VCHI_CALLBACK_T callback;
  25409. + void *callback_param;
  25410. + /* client intends to receive bulk transfers of
  25411. + odd lengths or into unaligned buffers */
  25412. + int32_t want_unaligned_bulk_rx;
  25413. + /* client intends to transmit bulk transfers of
  25414. + odd lengths or out of unaligned buffers */
  25415. + int32_t want_unaligned_bulk_tx;
  25416. + /* client wants to check CRCs on (bulk) xfers.
  25417. + Only needs to be set at 1 end - will do both directions. */
  25418. + int32_t want_crc;
  25419. +} SERVICE_CREATION_T;
  25420. +
  25421. +// Opaque handle for a VCHI instance
  25422. +typedef struct opaque_vchi_instance_handle_t *VCHI_INSTANCE_T;
  25423. +
  25424. +// Opaque handle for a server or client
  25425. +typedef struct opaque_vchi_service_handle_t *VCHI_SERVICE_HANDLE_T;
  25426. +
  25427. +// Service registration & startup
  25428. +typedef void (*VCHI_SERVICE_INIT)(VCHI_INSTANCE_T initialise_instance, VCHI_CONNECTION_T **connections, uint32_t num_connections);
  25429. +
  25430. +typedef struct service_info_tag {
  25431. + const char * const vll_filename; /* VLL to load to start this service. This is an empty string if VLL is "static" */
  25432. + VCHI_SERVICE_INIT init; /* Service initialisation function */
  25433. + void *vll_handle; /* VLL handle; NULL when unloaded or a "static VLL" in build */
  25434. +} SERVICE_INFO_T;
  25435. +
  25436. +/******************************************************************************
  25437. + Global funcs - implementation is specific to which side you are on (local / remote)
  25438. + *****************************************************************************/
  25439. +
  25440. +#ifdef __cplusplus
  25441. +extern "C" {
  25442. +#endif
  25443. +
  25444. +extern /*@observer@*/ VCHI_CONNECTION_T * vchi_create_connection( const VCHI_CONNECTION_API_T * function_table,
  25445. + const VCHI_MESSAGE_DRIVER_T * low_level);
  25446. +
  25447. +
  25448. +// Routine used to initialise the vchi on both local + remote connections
  25449. +extern int32_t vchi_initialise( VCHI_INSTANCE_T *instance_handle );
  25450. +
  25451. +extern int32_t vchi_exit( void );
  25452. +
  25453. +extern int32_t vchi_connect( VCHI_CONNECTION_T **connections,
  25454. + const uint32_t num_connections,
  25455. + VCHI_INSTANCE_T instance_handle );
  25456. +
  25457. +//When this is called, ensure that all services have no data pending.
  25458. +//Bulk transfers can remain 'queued'
  25459. +extern int32_t vchi_disconnect( VCHI_INSTANCE_T instance_handle );
  25460. +
  25461. +// Global control over bulk CRC checking
  25462. +extern int32_t vchi_crc_control( VCHI_CONNECTION_T *connection,
  25463. + VCHI_CRC_CONTROL_T control );
  25464. +
  25465. +// helper functions
  25466. +extern void * vchi_allocate_buffer(VCHI_SERVICE_HANDLE_T handle, uint32_t *length);
  25467. +extern void vchi_free_buffer(VCHI_SERVICE_HANDLE_T handle, void *address);
  25468. +extern uint32_t vchi_current_time(VCHI_INSTANCE_T instance_handle);
  25469. +
  25470. +
  25471. +/******************************************************************************
  25472. + Global service API
  25473. + *****************************************************************************/
  25474. +// Routine to create a named service
  25475. +extern int32_t vchi_service_create( VCHI_INSTANCE_T instance_handle,
  25476. + SERVICE_CREATION_T *setup,
  25477. + VCHI_SERVICE_HANDLE_T *handle );
  25478. +
  25479. +// Routine to destory a service
  25480. +extern int32_t vchi_service_destroy( const VCHI_SERVICE_HANDLE_T handle );
  25481. +
  25482. +// Routine to open a named service
  25483. +extern int32_t vchi_service_open( VCHI_INSTANCE_T instance_handle,
  25484. + SERVICE_CREATION_T *setup,
  25485. + VCHI_SERVICE_HANDLE_T *handle);
  25486. +
  25487. +extern int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle,
  25488. + short *peer_version );
  25489. +
  25490. +// Routine to close a named service
  25491. +extern int32_t vchi_service_close( const VCHI_SERVICE_HANDLE_T handle );
  25492. +
  25493. +// Routine to increment ref count on a named service
  25494. +extern int32_t vchi_service_use( const VCHI_SERVICE_HANDLE_T handle );
  25495. +
  25496. +// Routine to decrement ref count on a named service
  25497. +extern int32_t vchi_service_release( const VCHI_SERVICE_HANDLE_T handle );
  25498. +
  25499. +// Routine to set a control option for a named service
  25500. +extern int32_t vchi_service_set_option( const VCHI_SERVICE_HANDLE_T handle,
  25501. + VCHI_SERVICE_OPTION_T option,
  25502. + int value);
  25503. +
  25504. +// Routine to send a message across a service
  25505. +extern int32_t vchi_msg_queue( VCHI_SERVICE_HANDLE_T handle,
  25506. + const void *data,
  25507. + uint32_t data_size,
  25508. + VCHI_FLAGS_T flags,
  25509. + void *msg_handle );
  25510. +
  25511. +// scatter-gather (vector) and send message
  25512. +int32_t vchi_msg_queuev_ex( VCHI_SERVICE_HANDLE_T handle,
  25513. + VCHI_MSG_VECTOR_EX_T *vector,
  25514. + uint32_t count,
  25515. + VCHI_FLAGS_T flags,
  25516. + void *msg_handle );
  25517. +
  25518. +// legacy scatter-gather (vector) and send message, only handles pointers
  25519. +int32_t vchi_msg_queuev( VCHI_SERVICE_HANDLE_T handle,
  25520. + VCHI_MSG_VECTOR_T *vector,
  25521. + uint32_t count,
  25522. + VCHI_FLAGS_T flags,
  25523. + void *msg_handle );
  25524. +
  25525. +// Routine to receive a msg from a service
  25526. +// Dequeue is equivalent to hold, copy into client buffer, release
  25527. +extern int32_t vchi_msg_dequeue( VCHI_SERVICE_HANDLE_T handle,
  25528. + void *data,
  25529. + uint32_t max_data_size_to_read,
  25530. + uint32_t *actual_msg_size,
  25531. + VCHI_FLAGS_T flags );
  25532. +
  25533. +// Routine to look at a message in place.
  25534. +// The message is not dequeued, so a subsequent call to peek or dequeue
  25535. +// will return the same message.
  25536. +extern int32_t vchi_msg_peek( VCHI_SERVICE_HANDLE_T handle,
  25537. + void **data,
  25538. + uint32_t *msg_size,
  25539. + VCHI_FLAGS_T flags );
  25540. +
  25541. +// Routine to remove a message after it has been read in place with peek
  25542. +// The first message on the queue is dequeued.
  25543. +extern int32_t vchi_msg_remove( VCHI_SERVICE_HANDLE_T handle );
  25544. +
  25545. +// Routine to look at a message in place.
  25546. +// The message is dequeued, so the caller is left holding it; the descriptor is
  25547. +// filled in and must be released when the user has finished with the message.
  25548. +extern int32_t vchi_msg_hold( VCHI_SERVICE_HANDLE_T handle,
  25549. + void **data, // } may be NULL, as info can be
  25550. + uint32_t *msg_size, // } obtained from HELD_MSG_T
  25551. + VCHI_FLAGS_T flags,
  25552. + VCHI_HELD_MSG_T *message_descriptor );
  25553. +
  25554. +// Initialise an iterator to look through messages in place
  25555. +extern int32_t vchi_msg_look_ahead( VCHI_SERVICE_HANDLE_T handle,
  25556. + VCHI_MSG_ITER_T *iter,
  25557. + VCHI_FLAGS_T flags );
  25558. +
  25559. +/******************************************************************************
  25560. + Global service support API - operations on held messages and message iterators
  25561. + *****************************************************************************/
  25562. +
  25563. +// Routine to get the address of a held message
  25564. +extern void *vchi_held_msg_ptr( const VCHI_HELD_MSG_T *message );
  25565. +
  25566. +// Routine to get the size of a held message
  25567. +extern int32_t vchi_held_msg_size( const VCHI_HELD_MSG_T *message );
  25568. +
  25569. +// Routine to get the transmit timestamp as written into the header by the peer
  25570. +extern uint32_t vchi_held_msg_tx_timestamp( const VCHI_HELD_MSG_T *message );
  25571. +
  25572. +// Routine to get the reception timestamp, written as we parsed the header
  25573. +extern uint32_t vchi_held_msg_rx_timestamp( const VCHI_HELD_MSG_T *message );
  25574. +
  25575. +// Routine to release a held message after it has been processed
  25576. +extern int32_t vchi_held_msg_release( VCHI_HELD_MSG_T *message );
  25577. +
  25578. +// Indicates whether the iterator has a next message.
  25579. +extern int32_t vchi_msg_iter_has_next( const VCHI_MSG_ITER_T *iter );
  25580. +
  25581. +// Return the pointer and length for the next message and advance the iterator.
  25582. +extern int32_t vchi_msg_iter_next( VCHI_MSG_ITER_T *iter,
  25583. + void **data,
  25584. + uint32_t *msg_size );
  25585. +
  25586. +// Remove the last message returned by vchi_msg_iter_next.
  25587. +// Can only be called once after each call to vchi_msg_iter_next.
  25588. +extern int32_t vchi_msg_iter_remove( VCHI_MSG_ITER_T *iter );
  25589. +
  25590. +// Hold the last message returned by vchi_msg_iter_next.
  25591. +// Can only be called once after each call to vchi_msg_iter_next.
  25592. +extern int32_t vchi_msg_iter_hold( VCHI_MSG_ITER_T *iter,
  25593. + VCHI_HELD_MSG_T *message );
  25594. +
  25595. +// Return information for the next message, and hold it, advancing the iterator.
  25596. +extern int32_t vchi_msg_iter_hold_next( VCHI_MSG_ITER_T *iter,
  25597. + void **data, // } may be NULL
  25598. + uint32_t *msg_size, // }
  25599. + VCHI_HELD_MSG_T *message );
  25600. +
  25601. +
  25602. +/******************************************************************************
  25603. + Global bulk API
  25604. + *****************************************************************************/
  25605. +
  25606. +// Routine to prepare interface for a transfer from the other side
  25607. +extern int32_t vchi_bulk_queue_receive( VCHI_SERVICE_HANDLE_T handle,
  25608. + void *data_dst,
  25609. + uint32_t data_size,
  25610. + VCHI_FLAGS_T flags,
  25611. + void *transfer_handle );
  25612. +
  25613. +
  25614. +// Prepare interface for a transfer from the other side into relocatable memory.
  25615. +int32_t vchi_bulk_queue_receive_reloc( const VCHI_SERVICE_HANDLE_T handle,
  25616. + VCHI_MEM_HANDLE_T h_dst,
  25617. + uint32_t offset,
  25618. + uint32_t data_size,
  25619. + const VCHI_FLAGS_T flags,
  25620. + void * const bulk_handle );
  25621. +
  25622. +// Routine to queue up data ready for transfer to the other (once they have signalled they are ready)
  25623. +extern int32_t vchi_bulk_queue_transmit( VCHI_SERVICE_HANDLE_T handle,
  25624. + const void *data_src,
  25625. + uint32_t data_size,
  25626. + VCHI_FLAGS_T flags,
  25627. + void *transfer_handle );
  25628. +
  25629. +
  25630. +/******************************************************************************
  25631. + Configuration plumbing
  25632. + *****************************************************************************/
  25633. +
  25634. +// function prototypes for the different mid layers (the state info gives the different physical connections)
  25635. +extern const VCHI_CONNECTION_API_T *single_get_func_table( void );
  25636. +//extern const VCHI_CONNECTION_API_T *local_server_get_func_table( void );
  25637. +//extern const VCHI_CONNECTION_API_T *local_client_get_func_table( void );
  25638. +
  25639. +// declare all message drivers here
  25640. +const VCHI_MESSAGE_DRIVER_T *vchi_mphi_message_driver_func_table( void );
  25641. +
  25642. +#ifdef __cplusplus
  25643. +}
  25644. +#endif
  25645. +
  25646. +extern int32_t vchi_bulk_queue_transmit_reloc( VCHI_SERVICE_HANDLE_T handle,
  25647. + VCHI_MEM_HANDLE_T h_src,
  25648. + uint32_t offset,
  25649. + uint32_t data_size,
  25650. + VCHI_FLAGS_T flags,
  25651. + void *transfer_handle );
  25652. +#endif /* VCHI_H_ */
  25653. +
  25654. +/****************************** End of file **********************************/
  25655. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchi/vchi_mh.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchi/vchi_mh.h
  25656. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchi/vchi_mh.h 1969-12-31 18:00:00.000000000 -0600
  25657. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchi/vchi_mh.h 2014-12-03 19:13:38.224418001 -0600
  25658. @@ -0,0 +1,42 @@
  25659. +/**
  25660. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  25661. + *
  25662. + * Redistribution and use in source and binary forms, with or without
  25663. + * modification, are permitted provided that the following conditions
  25664. + * are met:
  25665. + * 1. Redistributions of source code must retain the above copyright
  25666. + * notice, this list of conditions, and the following disclaimer,
  25667. + * without modification.
  25668. + * 2. Redistributions in binary form must reproduce the above copyright
  25669. + * notice, this list of conditions and the following disclaimer in the
  25670. + * documentation and/or other materials provided with the distribution.
  25671. + * 3. The names of the above-listed copyright holders may not be used
  25672. + * to endorse or promote products derived from this software without
  25673. + * specific prior written permission.
  25674. + *
  25675. + * ALTERNATIVELY, this software may be distributed under the terms of the
  25676. + * GNU General Public License ("GPL") version 2, as published by the Free
  25677. + * Software Foundation.
  25678. + *
  25679. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  25680. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  25681. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  25682. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  25683. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25684. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25685. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25686. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25687. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25688. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25689. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25690. + */
  25691. +
  25692. +#ifndef VCHI_MH_H_
  25693. +#define VCHI_MH_H_
  25694. +
  25695. +#include <linux/types.h>
  25696. +
  25697. +typedef int32_t VCHI_MEM_HANDLE_T;
  25698. +#define VCHI_MEM_HANDLE_INVALID 0
  25699. +
  25700. +#endif
  25701. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
  25702. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c 1969-12-31 18:00:00.000000000 -0600
  25703. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c 2014-12-03 19:13:38.224418001 -0600
  25704. @@ -0,0 +1,562 @@
  25705. +/**
  25706. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  25707. + *
  25708. + * Redistribution and use in source and binary forms, with or without
  25709. + * modification, are permitted provided that the following conditions
  25710. + * are met:
  25711. + * 1. Redistributions of source code must retain the above copyright
  25712. + * notice, this list of conditions, and the following disclaimer,
  25713. + * without modification.
  25714. + * 2. Redistributions in binary form must reproduce the above copyright
  25715. + * notice, this list of conditions and the following disclaimer in the
  25716. + * documentation and/or other materials provided with the distribution.
  25717. + * 3. The names of the above-listed copyright holders may not be used
  25718. + * to endorse or promote products derived from this software without
  25719. + * specific prior written permission.
  25720. + *
  25721. + * ALTERNATIVELY, this software may be distributed under the terms of the
  25722. + * GNU General Public License ("GPL") version 2, as published by the Free
  25723. + * Software Foundation.
  25724. + *
  25725. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  25726. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  25727. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  25728. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  25729. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25730. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25731. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25732. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25733. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25734. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25735. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25736. + */
  25737. +
  25738. +#include <linux/kernel.h>
  25739. +#include <linux/types.h>
  25740. +#include <linux/errno.h>
  25741. +#include <linux/interrupt.h>
  25742. +#include <linux/irq.h>
  25743. +#include <linux/pagemap.h>
  25744. +#include <linux/dma-mapping.h>
  25745. +#include <linux/version.h>
  25746. +#include <linux/io.h>
  25747. +#include <linux/uaccess.h>
  25748. +#include <asm/pgtable.h>
  25749. +
  25750. +#include <mach/irqs.h>
  25751. +
  25752. +#include <mach/platform.h>
  25753. +#include <mach/vcio.h>
  25754. +
  25755. +#define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
  25756. +
  25757. +#define VCHIQ_DOORBELL_IRQ IRQ_ARM_DOORBELL_0
  25758. +#define VCHIQ_ARM_ADDRESS(x) ((void *)__virt_to_bus((unsigned)x))
  25759. +
  25760. +#include "vchiq_arm.h"
  25761. +#include "vchiq_2835.h"
  25762. +#include "vchiq_connected.h"
  25763. +#include "vchiq_killable.h"
  25764. +
  25765. +#define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
  25766. +
  25767. +typedef struct vchiq_2835_state_struct {
  25768. + int inited;
  25769. + VCHIQ_ARM_STATE_T arm_state;
  25770. +} VCHIQ_2835_ARM_STATE_T;
  25771. +
  25772. +static char *g_slot_mem;
  25773. +static int g_slot_mem_size;
  25774. +dma_addr_t g_slot_phys;
  25775. +static FRAGMENTS_T *g_fragments_base;
  25776. +static FRAGMENTS_T *g_free_fragments;
  25777. +struct semaphore g_free_fragments_sema;
  25778. +
  25779. +extern int vchiq_arm_log_level;
  25780. +
  25781. +static DEFINE_SEMAPHORE(g_free_fragments_mutex);
  25782. +
  25783. +static irqreturn_t
  25784. +vchiq_doorbell_irq(int irq, void *dev_id);
  25785. +
  25786. +static int
  25787. +create_pagelist(char __user *buf, size_t count, unsigned short type,
  25788. + struct task_struct *task, PAGELIST_T ** ppagelist);
  25789. +
  25790. +static void
  25791. +free_pagelist(PAGELIST_T *pagelist, int actual);
  25792. +
  25793. +int __init
  25794. +vchiq_platform_init(VCHIQ_STATE_T *state)
  25795. +{
  25796. + VCHIQ_SLOT_ZERO_T *vchiq_slot_zero;
  25797. + int frag_mem_size;
  25798. + int err;
  25799. + int i;
  25800. +
  25801. + /* Allocate space for the channels in coherent memory */
  25802. + g_slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
  25803. + frag_mem_size = PAGE_ALIGN(sizeof(FRAGMENTS_T) * MAX_FRAGMENTS);
  25804. +
  25805. + g_slot_mem = dma_alloc_coherent(NULL, g_slot_mem_size + frag_mem_size,
  25806. + &g_slot_phys, GFP_ATOMIC);
  25807. +
  25808. + if (!g_slot_mem) {
  25809. + vchiq_log_error(vchiq_arm_log_level,
  25810. + "Unable to allocate channel memory");
  25811. + err = -ENOMEM;
  25812. + goto failed_alloc;
  25813. + }
  25814. +
  25815. + WARN_ON(((int)g_slot_mem & (PAGE_SIZE - 1)) != 0);
  25816. +
  25817. + vchiq_slot_zero = vchiq_init_slots(g_slot_mem, g_slot_mem_size);
  25818. + if (!vchiq_slot_zero) {
  25819. + err = -EINVAL;
  25820. + goto failed_init_slots;
  25821. + }
  25822. +
  25823. + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
  25824. + (int)g_slot_phys + g_slot_mem_size;
  25825. + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
  25826. + MAX_FRAGMENTS;
  25827. +
  25828. + g_fragments_base = (FRAGMENTS_T *)(g_slot_mem + g_slot_mem_size);
  25829. + g_slot_mem_size += frag_mem_size;
  25830. +
  25831. + g_free_fragments = g_fragments_base;
  25832. + for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
  25833. + *(FRAGMENTS_T **)&g_fragments_base[i] =
  25834. + &g_fragments_base[i + 1];
  25835. + }
  25836. + *(FRAGMENTS_T **)&g_fragments_base[i] = NULL;
  25837. + sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
  25838. +
  25839. + if (vchiq_init_state(state, vchiq_slot_zero, 0/*slave*/) !=
  25840. + VCHIQ_SUCCESS) {
  25841. + err = -EINVAL;
  25842. + goto failed_vchiq_init;
  25843. + }
  25844. +
  25845. + err = request_irq(VCHIQ_DOORBELL_IRQ, vchiq_doorbell_irq,
  25846. + IRQF_IRQPOLL, "VCHIQ doorbell",
  25847. + state);
  25848. + if (err < 0) {
  25849. + vchiq_log_error(vchiq_arm_log_level, "%s: failed to register "
  25850. + "irq=%d err=%d", __func__,
  25851. + VCHIQ_DOORBELL_IRQ, err);
  25852. + goto failed_request_irq;
  25853. + }
  25854. +
  25855. + /* Send the base address of the slots to VideoCore */
  25856. +
  25857. + dsb(); /* Ensure all writes have completed */
  25858. +
  25859. + bcm_mailbox_write(MBOX_CHAN_VCHIQ, (unsigned int)g_slot_phys);
  25860. +
  25861. + vchiq_log_info(vchiq_arm_log_level,
  25862. + "vchiq_init - done (slots %x, phys %x)",
  25863. + (unsigned int)vchiq_slot_zero, g_slot_phys);
  25864. +
  25865. + vchiq_call_connected_callbacks();
  25866. +
  25867. + return 0;
  25868. +
  25869. +failed_request_irq:
  25870. +failed_vchiq_init:
  25871. +failed_init_slots:
  25872. + dma_free_coherent(NULL, g_slot_mem_size, g_slot_mem, g_slot_phys);
  25873. +
  25874. +failed_alloc:
  25875. + return err;
  25876. +}
  25877. +
  25878. +void __exit
  25879. +vchiq_platform_exit(VCHIQ_STATE_T *state)
  25880. +{
  25881. + free_irq(VCHIQ_DOORBELL_IRQ, state);
  25882. + dma_free_coherent(NULL, g_slot_mem_size,
  25883. + g_slot_mem, g_slot_phys);
  25884. +}
  25885. +
  25886. +
  25887. +VCHIQ_STATUS_T
  25888. +vchiq_platform_init_state(VCHIQ_STATE_T *state)
  25889. +{
  25890. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  25891. + state->platform_state = kzalloc(sizeof(VCHIQ_2835_ARM_STATE_T), GFP_KERNEL);
  25892. + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 1;
  25893. + status = vchiq_arm_init_state(state, &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state);
  25894. + if(status != VCHIQ_SUCCESS)
  25895. + {
  25896. + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 0;
  25897. + }
  25898. + return status;
  25899. +}
  25900. +
  25901. +VCHIQ_ARM_STATE_T*
  25902. +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state)
  25903. +{
  25904. + if(!((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited)
  25905. + {
  25906. + BUG();
  25907. + }
  25908. + return &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state;
  25909. +}
  25910. +
  25911. +void
  25912. +remote_event_signal(REMOTE_EVENT_T *event)
  25913. +{
  25914. + wmb();
  25915. +
  25916. + event->fired = 1;
  25917. +
  25918. + dsb(); /* data barrier operation */
  25919. +
  25920. + if (event->armed) {
  25921. + /* trigger vc interrupt */
  25922. +
  25923. + writel(0, __io_address(ARM_0_BELL2));
  25924. + }
  25925. +}
  25926. +
  25927. +int
  25928. +vchiq_copy_from_user(void *dst, const void *src, int size)
  25929. +{
  25930. + if ((uint32_t)src < TASK_SIZE) {
  25931. + return copy_from_user(dst, src, size);
  25932. + } else {
  25933. + memcpy(dst, src, size);
  25934. + return 0;
  25935. + }
  25936. +}
  25937. +
  25938. +VCHIQ_STATUS_T
  25939. +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk, VCHI_MEM_HANDLE_T memhandle,
  25940. + void *offset, int size, int dir)
  25941. +{
  25942. + PAGELIST_T *pagelist;
  25943. + int ret;
  25944. +
  25945. + WARN_ON(memhandle != VCHI_MEM_HANDLE_INVALID);
  25946. +
  25947. + ret = create_pagelist((char __user *)offset, size,
  25948. + (dir == VCHIQ_BULK_RECEIVE)
  25949. + ? PAGELIST_READ
  25950. + : PAGELIST_WRITE,
  25951. + current,
  25952. + &pagelist);
  25953. + if (ret != 0)
  25954. + return VCHIQ_ERROR;
  25955. +
  25956. + bulk->handle = memhandle;
  25957. + bulk->data = VCHIQ_ARM_ADDRESS(pagelist);
  25958. +
  25959. + /* Store the pagelist address in remote_data, which isn't used by the
  25960. + slave. */
  25961. + bulk->remote_data = pagelist;
  25962. +
  25963. + return VCHIQ_SUCCESS;
  25964. +}
  25965. +
  25966. +void
  25967. +vchiq_complete_bulk(VCHIQ_BULK_T *bulk)
  25968. +{
  25969. + if (bulk && bulk->remote_data && bulk->actual)
  25970. + free_pagelist((PAGELIST_T *)bulk->remote_data, bulk->actual);
  25971. +}
  25972. +
  25973. +void
  25974. +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk)
  25975. +{
  25976. + /*
  25977. + * This should only be called on the master (VideoCore) side, but
  25978. + * provide an implementation to avoid the need for ifdefery.
  25979. + */
  25980. + BUG();
  25981. +}
  25982. +
  25983. +void
  25984. +vchiq_dump_platform_state(void *dump_context)
  25985. +{
  25986. + char buf[80];
  25987. + int len;
  25988. + len = snprintf(buf, sizeof(buf),
  25989. + " Platform: 2835 (VC master)");
  25990. + vchiq_dump(dump_context, buf, len + 1);
  25991. +}
  25992. +
  25993. +VCHIQ_STATUS_T
  25994. +vchiq_platform_suspend(VCHIQ_STATE_T *state)
  25995. +{
  25996. + return VCHIQ_ERROR;
  25997. +}
  25998. +
  25999. +VCHIQ_STATUS_T
  26000. +vchiq_platform_resume(VCHIQ_STATE_T *state)
  26001. +{
  26002. + return VCHIQ_SUCCESS;
  26003. +}
  26004. +
  26005. +void
  26006. +vchiq_platform_paused(VCHIQ_STATE_T *state)
  26007. +{
  26008. +}
  26009. +
  26010. +void
  26011. +vchiq_platform_resumed(VCHIQ_STATE_T *state)
  26012. +{
  26013. +}
  26014. +
  26015. +int
  26016. +vchiq_platform_videocore_wanted(VCHIQ_STATE_T* state)
  26017. +{
  26018. + return 1; // autosuspend not supported - videocore always wanted
  26019. +}
  26020. +
  26021. +int
  26022. +vchiq_platform_use_suspend_timer(void)
  26023. +{
  26024. + return 0;
  26025. +}
  26026. +void
  26027. +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state)
  26028. +{
  26029. + vchiq_log_info((vchiq_arm_log_level>=VCHIQ_LOG_INFO),"Suspend timer not in use");
  26030. +}
  26031. +void
  26032. +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state)
  26033. +{
  26034. + (void)state;
  26035. +}
  26036. +/*
  26037. + * Local functions
  26038. + */
  26039. +
  26040. +static irqreturn_t
  26041. +vchiq_doorbell_irq(int irq, void *dev_id)
  26042. +{
  26043. + VCHIQ_STATE_T *state = dev_id;
  26044. + irqreturn_t ret = IRQ_NONE;
  26045. + unsigned int status;
  26046. +
  26047. + /* Read (and clear) the doorbell */
  26048. + status = readl(__io_address(ARM_0_BELL0));
  26049. +
  26050. + if (status & 0x4) { /* Was the doorbell rung? */
  26051. + remote_event_pollall(state);
  26052. + ret = IRQ_HANDLED;
  26053. + }
  26054. +
  26055. + return ret;
  26056. +}
  26057. +
  26058. +/* There is a potential problem with partial cache lines (pages?)
  26059. +** at the ends of the block when reading. If the CPU accessed anything in
  26060. +** the same line (page?) then it may have pulled old data into the cache,
  26061. +** obscuring the new data underneath. We can solve this by transferring the
  26062. +** partial cache lines separately, and allowing the ARM to copy into the
  26063. +** cached area.
  26064. +
  26065. +** N.B. This implementation plays slightly fast and loose with the Linux
  26066. +** driver programming rules, e.g. its use of __virt_to_bus instead of
  26067. +** dma_map_single, but it isn't a multi-platform driver and it benefits
  26068. +** from increased speed as a result.
  26069. +*/
  26070. +
  26071. +static int
  26072. +create_pagelist(char __user *buf, size_t count, unsigned short type,
  26073. + struct task_struct *task, PAGELIST_T ** ppagelist)
  26074. +{
  26075. + PAGELIST_T *pagelist;
  26076. + struct page **pages;
  26077. + struct page *page;
  26078. + unsigned long *addrs;
  26079. + unsigned int num_pages, offset, i;
  26080. + char *addr, *base_addr, *next_addr;
  26081. + int run, addridx, actual_pages;
  26082. + unsigned long *need_release;
  26083. +
  26084. + offset = (unsigned int)buf & (PAGE_SIZE - 1);
  26085. + num_pages = (count + offset + PAGE_SIZE - 1) / PAGE_SIZE;
  26086. +
  26087. + *ppagelist = NULL;
  26088. +
  26089. + /* Allocate enough storage to hold the page pointers and the page
  26090. + ** list
  26091. + */
  26092. + pagelist = kmalloc(sizeof(PAGELIST_T) +
  26093. + (num_pages * sizeof(unsigned long)) +
  26094. + sizeof(unsigned long) +
  26095. + (num_pages * sizeof(pages[0])),
  26096. + GFP_KERNEL);
  26097. +
  26098. + vchiq_log_trace(vchiq_arm_log_level,
  26099. + "create_pagelist - %x", (unsigned int)pagelist);
  26100. + if (!pagelist)
  26101. + return -ENOMEM;
  26102. +
  26103. + addrs = pagelist->addrs;
  26104. + need_release = (unsigned long *)(addrs + num_pages);
  26105. + pages = (struct page **)(addrs + num_pages + 1);
  26106. +
  26107. + if (is_vmalloc_addr(buf)) {
  26108. + for (actual_pages = 0; actual_pages < num_pages; actual_pages++) {
  26109. + pages[actual_pages] = vmalloc_to_page(buf + (actual_pages * PAGE_SIZE));
  26110. + }
  26111. + *need_release = 0; /* do not try and release vmalloc pages */
  26112. + } else {
  26113. + down_read(&task->mm->mmap_sem);
  26114. + actual_pages = get_user_pages(task, task->mm,
  26115. + (unsigned long)buf & ~(PAGE_SIZE - 1),
  26116. + num_pages,
  26117. + (type == PAGELIST_READ) /*Write */ ,
  26118. + 0 /*Force */ ,
  26119. + pages,
  26120. + NULL /*vmas */);
  26121. + up_read(&task->mm->mmap_sem);
  26122. +
  26123. + if (actual_pages != num_pages) {
  26124. + vchiq_log_info(vchiq_arm_log_level,
  26125. + "create_pagelist - only %d/%d pages locked",
  26126. + actual_pages,
  26127. + num_pages);
  26128. +
  26129. + /* This is probably due to the process being killed */
  26130. + while (actual_pages > 0)
  26131. + {
  26132. + actual_pages--;
  26133. + page_cache_release(pages[actual_pages]);
  26134. + }
  26135. + kfree(pagelist);
  26136. + if (actual_pages == 0)
  26137. + actual_pages = -ENOMEM;
  26138. + return actual_pages;
  26139. + }
  26140. + *need_release = 1; /* release user pages */
  26141. + }
  26142. +
  26143. + pagelist->length = count;
  26144. + pagelist->type = type;
  26145. + pagelist->offset = offset;
  26146. +
  26147. + /* Group the pages into runs of contiguous pages */
  26148. +
  26149. + base_addr = VCHIQ_ARM_ADDRESS(page_address(pages[0]));
  26150. + next_addr = base_addr + PAGE_SIZE;
  26151. + addridx = 0;
  26152. + run = 0;
  26153. +
  26154. + for (i = 1; i < num_pages; i++) {
  26155. + addr = VCHIQ_ARM_ADDRESS(page_address(pages[i]));
  26156. + if ((addr == next_addr) && (run < (PAGE_SIZE - 1))) {
  26157. + next_addr += PAGE_SIZE;
  26158. + run++;
  26159. + } else {
  26160. + addrs[addridx] = (unsigned long)base_addr + run;
  26161. + addridx++;
  26162. + base_addr = addr;
  26163. + next_addr = addr + PAGE_SIZE;
  26164. + run = 0;
  26165. + }
  26166. + }
  26167. +
  26168. + addrs[addridx] = (unsigned long)base_addr + run;
  26169. + addridx++;
  26170. +
  26171. + /* Partial cache lines (fragments) require special measures */
  26172. + if ((type == PAGELIST_READ) &&
  26173. + ((pagelist->offset & (CACHE_LINE_SIZE - 1)) ||
  26174. + ((pagelist->offset + pagelist->length) &
  26175. + (CACHE_LINE_SIZE - 1)))) {
  26176. + FRAGMENTS_T *fragments;
  26177. +
  26178. + if (down_interruptible(&g_free_fragments_sema) != 0) {
  26179. + kfree(pagelist);
  26180. + return -EINTR;
  26181. + }
  26182. +
  26183. + WARN_ON(g_free_fragments == NULL);
  26184. +
  26185. + down(&g_free_fragments_mutex);
  26186. + fragments = (FRAGMENTS_T *) g_free_fragments;
  26187. + WARN_ON(fragments == NULL);
  26188. + g_free_fragments = *(FRAGMENTS_T **) g_free_fragments;
  26189. + up(&g_free_fragments_mutex);
  26190. + pagelist->type =
  26191. + PAGELIST_READ_WITH_FRAGMENTS + (fragments -
  26192. + g_fragments_base);
  26193. + }
  26194. +
  26195. + for (page = virt_to_page(pagelist);
  26196. + page <= virt_to_page(addrs + num_pages - 1); page++) {
  26197. + flush_dcache_page(page);
  26198. + }
  26199. +
  26200. + *ppagelist = pagelist;
  26201. +
  26202. + return 0;
  26203. +}
  26204. +
  26205. +static void
  26206. +free_pagelist(PAGELIST_T *pagelist, int actual)
  26207. +{
  26208. + unsigned long *need_release;
  26209. + struct page **pages;
  26210. + unsigned int num_pages, i;
  26211. +
  26212. + vchiq_log_trace(vchiq_arm_log_level,
  26213. + "free_pagelist - %x, %d", (unsigned int)pagelist, actual);
  26214. +
  26215. + num_pages =
  26216. + (pagelist->length + pagelist->offset + PAGE_SIZE - 1) /
  26217. + PAGE_SIZE;
  26218. +
  26219. + need_release = (unsigned long *)(pagelist->addrs + num_pages);
  26220. + pages = (struct page **)(pagelist->addrs + num_pages + 1);
  26221. +
  26222. + /* Deal with any partial cache lines (fragments) */
  26223. + if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
  26224. + FRAGMENTS_T *fragments = g_fragments_base +
  26225. + (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS);
  26226. + int head_bytes, tail_bytes;
  26227. + head_bytes = (CACHE_LINE_SIZE - pagelist->offset) &
  26228. + (CACHE_LINE_SIZE - 1);
  26229. + tail_bytes = (pagelist->offset + actual) &
  26230. + (CACHE_LINE_SIZE - 1);
  26231. +
  26232. + if ((actual >= 0) && (head_bytes != 0)) {
  26233. + if (head_bytes > actual)
  26234. + head_bytes = actual;
  26235. +
  26236. + memcpy((char *)page_address(pages[0]) +
  26237. + pagelist->offset,
  26238. + fragments->headbuf,
  26239. + head_bytes);
  26240. + }
  26241. + if ((actual >= 0) && (head_bytes < actual) &&
  26242. + (tail_bytes != 0)) {
  26243. + memcpy((char *)page_address(pages[num_pages - 1]) +
  26244. + ((pagelist->offset + actual) &
  26245. + (PAGE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1)),
  26246. + fragments->tailbuf, tail_bytes);
  26247. + }
  26248. +
  26249. + down(&g_free_fragments_mutex);
  26250. + *(FRAGMENTS_T **) fragments = g_free_fragments;
  26251. + g_free_fragments = fragments;
  26252. + up(&g_free_fragments_mutex);
  26253. + up(&g_free_fragments_sema);
  26254. + }
  26255. +
  26256. + if (*need_release) {
  26257. + for (i = 0; i < num_pages; i++) {
  26258. + if (pagelist->type != PAGELIST_WRITE)
  26259. + set_page_dirty(pages[i]);
  26260. +
  26261. + page_cache_release(pages[i]);
  26262. + }
  26263. + }
  26264. +
  26265. + kfree(pagelist);
  26266. +}
  26267. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h
  26268. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h 1969-12-31 18:00:00.000000000 -0600
  26269. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h 2014-12-03 19:13:38.224418001 -0600
  26270. @@ -0,0 +1,42 @@
  26271. +/**
  26272. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  26273. + *
  26274. + * Redistribution and use in source and binary forms, with or without
  26275. + * modification, are permitted provided that the following conditions
  26276. + * are met:
  26277. + * 1. Redistributions of source code must retain the above copyright
  26278. + * notice, this list of conditions, and the following disclaimer,
  26279. + * without modification.
  26280. + * 2. Redistributions in binary form must reproduce the above copyright
  26281. + * notice, this list of conditions and the following disclaimer in the
  26282. + * documentation and/or other materials provided with the distribution.
  26283. + * 3. The names of the above-listed copyright holders may not be used
  26284. + * to endorse or promote products derived from this software without
  26285. + * specific prior written permission.
  26286. + *
  26287. + * ALTERNATIVELY, this software may be distributed under the terms of the
  26288. + * GNU General Public License ("GPL") version 2, as published by the Free
  26289. + * Software Foundation.
  26290. + *
  26291. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26292. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  26293. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  26294. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  26295. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26296. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26297. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26298. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26299. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26300. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26301. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26302. + */
  26303. +
  26304. +#ifndef VCHIQ_2835_H
  26305. +#define VCHIQ_2835_H
  26306. +
  26307. +#include "vchiq_pagelist.h"
  26308. +
  26309. +#define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
  26310. +#define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX 1
  26311. +
  26312. +#endif /* VCHIQ_2835_H */
  26313. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c
  26314. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c 1969-12-31 18:00:00.000000000 -0600
  26315. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c 2014-12-03 19:13:38.224418001 -0600
  26316. @@ -0,0 +1,2884 @@
  26317. +/**
  26318. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  26319. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  26320. + *
  26321. + * Redistribution and use in source and binary forms, with or without
  26322. + * modification, are permitted provided that the following conditions
  26323. + * are met:
  26324. + * 1. Redistributions of source code must retain the above copyright
  26325. + * notice, this list of conditions, and the following disclaimer,
  26326. + * without modification.
  26327. + * 2. Redistributions in binary form must reproduce the above copyright
  26328. + * notice, this list of conditions and the following disclaimer in the
  26329. + * documentation and/or other materials provided with the distribution.
  26330. + * 3. The names of the above-listed copyright holders may not be used
  26331. + * to endorse or promote products derived from this software without
  26332. + * specific prior written permission.
  26333. + *
  26334. + * ALTERNATIVELY, this software may be distributed under the terms of the
  26335. + * GNU General Public License ("GPL") version 2, as published by the Free
  26336. + * Software Foundation.
  26337. + *
  26338. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26339. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  26340. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  26341. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  26342. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26343. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26344. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26345. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26346. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26347. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26348. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26349. + */
  26350. +
  26351. +#include <linux/kernel.h>
  26352. +#include <linux/module.h>
  26353. +#include <linux/types.h>
  26354. +#include <linux/errno.h>
  26355. +#include <linux/cdev.h>
  26356. +#include <linux/fs.h>
  26357. +#include <linux/device.h>
  26358. +#include <linux/mm.h>
  26359. +#include <linux/highmem.h>
  26360. +#include <linux/pagemap.h>
  26361. +#include <linux/bug.h>
  26362. +#include <linux/semaphore.h>
  26363. +#include <linux/list.h>
  26364. +
  26365. +#include "vchiq_core.h"
  26366. +#include "vchiq_ioctl.h"
  26367. +#include "vchiq_arm.h"
  26368. +#include "vchiq_killable.h"
  26369. +#include "vchiq_debugfs.h"
  26370. +
  26371. +#define DEVICE_NAME "vchiq"
  26372. +
  26373. +/* Override the default prefix, which would be vchiq_arm (from the filename) */
  26374. +#undef MODULE_PARAM_PREFIX
  26375. +#define MODULE_PARAM_PREFIX DEVICE_NAME "."
  26376. +
  26377. +#define VCHIQ_MINOR 0
  26378. +
  26379. +/* Some per-instance constants */
  26380. +#define MAX_COMPLETIONS 16
  26381. +#define MAX_SERVICES 64
  26382. +#define MAX_ELEMENTS 8
  26383. +#define MSG_QUEUE_SIZE 64
  26384. +
  26385. +#define KEEPALIVE_VER 1
  26386. +#define KEEPALIVE_VER_MIN KEEPALIVE_VER
  26387. +
  26388. +/* Run time control of log level, based on KERN_XXX level. */
  26389. +int vchiq_arm_log_level = VCHIQ_LOG_DEFAULT;
  26390. +int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
  26391. +
  26392. +#define SUSPEND_TIMER_TIMEOUT_MS 100
  26393. +#define SUSPEND_RETRY_TIMER_TIMEOUT_MS 1000
  26394. +
  26395. +#define VC_SUSPEND_NUM_OFFSET 3 /* number of values before idle which are -ve */
  26396. +static const char *const suspend_state_names[] = {
  26397. + "VC_SUSPEND_FORCE_CANCELED",
  26398. + "VC_SUSPEND_REJECTED",
  26399. + "VC_SUSPEND_FAILED",
  26400. + "VC_SUSPEND_IDLE",
  26401. + "VC_SUSPEND_REQUESTED",
  26402. + "VC_SUSPEND_IN_PROGRESS",
  26403. + "VC_SUSPEND_SUSPENDED"
  26404. +};
  26405. +#define VC_RESUME_NUM_OFFSET 1 /* number of values before idle which are -ve */
  26406. +static const char *const resume_state_names[] = {
  26407. + "VC_RESUME_FAILED",
  26408. + "VC_RESUME_IDLE",
  26409. + "VC_RESUME_REQUESTED",
  26410. + "VC_RESUME_IN_PROGRESS",
  26411. + "VC_RESUME_RESUMED"
  26412. +};
  26413. +/* The number of times we allow force suspend to timeout before actually
  26414. +** _forcing_ suspend. This is to cater for SW which fails to release vchiq
  26415. +** correctly - we don't want to prevent ARM suspend indefinitely in this case.
  26416. +*/
  26417. +#define FORCE_SUSPEND_FAIL_MAX 8
  26418. +
  26419. +/* The time in ms allowed for videocore to go idle when force suspend has been
  26420. + * requested */
  26421. +#define FORCE_SUSPEND_TIMEOUT_MS 200
  26422. +
  26423. +
  26424. +static void suspend_timer_callback(unsigned long context);
  26425. +
  26426. +
  26427. +typedef struct user_service_struct {
  26428. + VCHIQ_SERVICE_T *service;
  26429. + void *userdata;
  26430. + VCHIQ_INSTANCE_T instance;
  26431. + char is_vchi;
  26432. + char dequeue_pending;
  26433. + char close_pending;
  26434. + int message_available_pos;
  26435. + int msg_insert;
  26436. + int msg_remove;
  26437. + struct semaphore insert_event;
  26438. + struct semaphore remove_event;
  26439. + struct semaphore close_event;
  26440. + VCHIQ_HEADER_T * msg_queue[MSG_QUEUE_SIZE];
  26441. +} USER_SERVICE_T;
  26442. +
  26443. +struct bulk_waiter_node {
  26444. + struct bulk_waiter bulk_waiter;
  26445. + int pid;
  26446. + struct list_head list;
  26447. +};
  26448. +
  26449. +struct vchiq_instance_struct {
  26450. + VCHIQ_STATE_T *state;
  26451. + VCHIQ_COMPLETION_DATA_T completions[MAX_COMPLETIONS];
  26452. + int completion_insert;
  26453. + int completion_remove;
  26454. + struct semaphore insert_event;
  26455. + struct semaphore remove_event;
  26456. + struct mutex completion_mutex;
  26457. +
  26458. + int connected;
  26459. + int closing;
  26460. + int pid;
  26461. + int mark;
  26462. + int use_close_delivered;
  26463. + int trace;
  26464. +
  26465. + struct list_head bulk_waiter_list;
  26466. + struct mutex bulk_waiter_list_mutex;
  26467. +
  26468. + VCHIQ_DEBUGFS_NODE_T debugfs_node;
  26469. +};
  26470. +
  26471. +typedef struct dump_context_struct {
  26472. + char __user *buf;
  26473. + size_t actual;
  26474. + size_t space;
  26475. + loff_t offset;
  26476. +} DUMP_CONTEXT_T;
  26477. +
  26478. +static struct cdev vchiq_cdev;
  26479. +static dev_t vchiq_devid;
  26480. +static VCHIQ_STATE_T g_state;
  26481. +static struct class *vchiq_class;
  26482. +static struct device *vchiq_dev;
  26483. +static DEFINE_SPINLOCK(msg_queue_spinlock);
  26484. +
  26485. +static const char *const ioctl_names[] = {
  26486. + "CONNECT",
  26487. + "SHUTDOWN",
  26488. + "CREATE_SERVICE",
  26489. + "REMOVE_SERVICE",
  26490. + "QUEUE_MESSAGE",
  26491. + "QUEUE_BULK_TRANSMIT",
  26492. + "QUEUE_BULK_RECEIVE",
  26493. + "AWAIT_COMPLETION",
  26494. + "DEQUEUE_MESSAGE",
  26495. + "GET_CLIENT_ID",
  26496. + "GET_CONFIG",
  26497. + "CLOSE_SERVICE",
  26498. + "USE_SERVICE",
  26499. + "RELEASE_SERVICE",
  26500. + "SET_SERVICE_OPTION",
  26501. + "DUMP_PHYS_MEM",
  26502. + "LIB_VERSION",
  26503. + "CLOSE_DELIVERED"
  26504. +};
  26505. +
  26506. +vchiq_static_assert((sizeof(ioctl_names)/sizeof(ioctl_names[0])) ==
  26507. + (VCHIQ_IOC_MAX + 1));
  26508. +
  26509. +static void
  26510. +dump_phys_mem(void *virt_addr, uint32_t num_bytes);
  26511. +
  26512. +/****************************************************************************
  26513. +*
  26514. +* add_completion
  26515. +*
  26516. +***************************************************************************/
  26517. +
  26518. +static VCHIQ_STATUS_T
  26519. +add_completion(VCHIQ_INSTANCE_T instance, VCHIQ_REASON_T reason,
  26520. + VCHIQ_HEADER_T *header, USER_SERVICE_T *user_service,
  26521. + void *bulk_userdata)
  26522. +{
  26523. + VCHIQ_COMPLETION_DATA_T *completion;
  26524. + DEBUG_INITIALISE(g_state.local)
  26525. +
  26526. + while (instance->completion_insert ==
  26527. + (instance->completion_remove + MAX_COMPLETIONS)) {
  26528. + /* Out of space - wait for the client */
  26529. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  26530. + vchiq_log_trace(vchiq_arm_log_level,
  26531. + "add_completion - completion queue full");
  26532. + DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT);
  26533. + if (down_interruptible(&instance->remove_event) != 0) {
  26534. + vchiq_log_info(vchiq_arm_log_level,
  26535. + "service_callback interrupted");
  26536. + return VCHIQ_RETRY;
  26537. + } else if (instance->closing) {
  26538. + vchiq_log_info(vchiq_arm_log_level,
  26539. + "service_callback closing");
  26540. + return VCHIQ_ERROR;
  26541. + }
  26542. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  26543. + }
  26544. +
  26545. + completion =
  26546. + &instance->completions[instance->completion_insert &
  26547. + (MAX_COMPLETIONS - 1)];
  26548. +
  26549. + completion->header = header;
  26550. + completion->reason = reason;
  26551. + /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */
  26552. + completion->service_userdata = user_service->service;
  26553. + completion->bulk_userdata = bulk_userdata;
  26554. +
  26555. + if (reason == VCHIQ_SERVICE_CLOSED) {
  26556. + /* Take an extra reference, to be held until
  26557. + this CLOSED notification is delivered. */
  26558. + lock_service(user_service->service);
  26559. + if (instance->use_close_delivered)
  26560. + user_service->close_pending = 1;
  26561. + }
  26562. +
  26563. + /* A write barrier is needed here to ensure that the entire completion
  26564. + record is written out before the insert point. */
  26565. + wmb();
  26566. +
  26567. + if (reason == VCHIQ_MESSAGE_AVAILABLE)
  26568. + user_service->message_available_pos =
  26569. + instance->completion_insert;
  26570. + instance->completion_insert++;
  26571. +
  26572. + up(&instance->insert_event);
  26573. +
  26574. + return VCHIQ_SUCCESS;
  26575. +}
  26576. +
  26577. +/****************************************************************************
  26578. +*
  26579. +* service_callback
  26580. +*
  26581. +***************************************************************************/
  26582. +
  26583. +static VCHIQ_STATUS_T
  26584. +service_callback(VCHIQ_REASON_T reason, VCHIQ_HEADER_T *header,
  26585. + VCHIQ_SERVICE_HANDLE_T handle, void *bulk_userdata)
  26586. +{
  26587. + /* How do we ensure the callback goes to the right client?
  26588. + ** The service_user data points to a USER_SERVICE_T record containing
  26589. + ** the original callback and the user state structure, which contains a
  26590. + ** circular buffer for completion records.
  26591. + */
  26592. + USER_SERVICE_T *user_service;
  26593. + VCHIQ_SERVICE_T *service;
  26594. + VCHIQ_INSTANCE_T instance;
  26595. + DEBUG_INITIALISE(g_state.local)
  26596. +
  26597. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  26598. +
  26599. + service = handle_to_service(handle);
  26600. + BUG_ON(!service);
  26601. + user_service = (USER_SERVICE_T *)service->base.userdata;
  26602. + instance = user_service->instance;
  26603. +
  26604. + if (!instance || instance->closing)
  26605. + return VCHIQ_SUCCESS;
  26606. +
  26607. + vchiq_log_trace(vchiq_arm_log_level,
  26608. + "service_callback - service %lx(%d,%p), reason %d, header %lx, "
  26609. + "instance %lx, bulk_userdata %lx",
  26610. + (unsigned long)user_service,
  26611. + service->localport, user_service->userdata,
  26612. + reason, (unsigned long)header,
  26613. + (unsigned long)instance, (unsigned long)bulk_userdata);
  26614. +
  26615. + if (header && user_service->is_vchi) {
  26616. + spin_lock(&msg_queue_spinlock);
  26617. + while (user_service->msg_insert ==
  26618. + (user_service->msg_remove + MSG_QUEUE_SIZE)) {
  26619. + spin_unlock(&msg_queue_spinlock);
  26620. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  26621. + DEBUG_COUNT(MSG_QUEUE_FULL_COUNT);
  26622. + vchiq_log_trace(vchiq_arm_log_level,
  26623. + "service_callback - msg queue full");
  26624. + /* If there is no MESSAGE_AVAILABLE in the completion
  26625. + ** queue, add one
  26626. + */
  26627. + if ((user_service->message_available_pos -
  26628. + instance->completion_remove) < 0) {
  26629. + VCHIQ_STATUS_T status;
  26630. + vchiq_log_info(vchiq_arm_log_level,
  26631. + "Inserting extra MESSAGE_AVAILABLE");
  26632. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  26633. + status = add_completion(instance, reason,
  26634. + NULL, user_service, bulk_userdata);
  26635. + if (status != VCHIQ_SUCCESS) {
  26636. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  26637. + return status;
  26638. + }
  26639. + }
  26640. +
  26641. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  26642. + if (down_interruptible(&user_service->remove_event)
  26643. + != 0) {
  26644. + vchiq_log_info(vchiq_arm_log_level,
  26645. + "service_callback interrupted");
  26646. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  26647. + return VCHIQ_RETRY;
  26648. + } else if (instance->closing) {
  26649. + vchiq_log_info(vchiq_arm_log_level,
  26650. + "service_callback closing");
  26651. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  26652. + return VCHIQ_ERROR;
  26653. + }
  26654. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  26655. + spin_lock(&msg_queue_spinlock);
  26656. + }
  26657. +
  26658. + user_service->msg_queue[user_service->msg_insert &
  26659. + (MSG_QUEUE_SIZE - 1)] = header;
  26660. + user_service->msg_insert++;
  26661. + spin_unlock(&msg_queue_spinlock);
  26662. +
  26663. + up(&user_service->insert_event);
  26664. +
  26665. + /* If there is a thread waiting in DEQUEUE_MESSAGE, or if
  26666. + ** there is a MESSAGE_AVAILABLE in the completion queue then
  26667. + ** bypass the completion queue.
  26668. + */
  26669. + if (((user_service->message_available_pos -
  26670. + instance->completion_remove) >= 0) ||
  26671. + user_service->dequeue_pending) {
  26672. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  26673. + user_service->dequeue_pending = 0;
  26674. + return VCHIQ_SUCCESS;
  26675. + }
  26676. +
  26677. + header = NULL;
  26678. + }
  26679. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  26680. +
  26681. + return add_completion(instance, reason, header, user_service,
  26682. + bulk_userdata);
  26683. +}
  26684. +
  26685. +/****************************************************************************
  26686. +*
  26687. +* user_service_free
  26688. +*
  26689. +***************************************************************************/
  26690. +static void
  26691. +user_service_free(void *userdata)
  26692. +{
  26693. + kfree(userdata);
  26694. +}
  26695. +
  26696. +/****************************************************************************
  26697. +*
  26698. +* close_delivered
  26699. +*
  26700. +***************************************************************************/
  26701. +static void close_delivered(USER_SERVICE_T *user_service)
  26702. +{
  26703. + vchiq_log_info(vchiq_arm_log_level,
  26704. + "close_delivered(handle=%x)",
  26705. + user_service->service->handle);
  26706. +
  26707. + if (user_service->close_pending) {
  26708. + /* Allow the underlying service to be culled */
  26709. + unlock_service(user_service->service);
  26710. +
  26711. + /* Wake the user-thread blocked in close_ or remove_service */
  26712. + up(&user_service->close_event);
  26713. +
  26714. + user_service->close_pending = 0;
  26715. + }
  26716. +}
  26717. +
  26718. +/****************************************************************************
  26719. +*
  26720. +* vchiq_ioctl
  26721. +*
  26722. +***************************************************************************/
  26723. +static long
  26724. +vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  26725. +{
  26726. + VCHIQ_INSTANCE_T instance = file->private_data;
  26727. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  26728. + VCHIQ_SERVICE_T *service = NULL;
  26729. + long ret = 0;
  26730. + int i, rc;
  26731. + DEBUG_INITIALISE(g_state.local)
  26732. +
  26733. + vchiq_log_trace(vchiq_arm_log_level,
  26734. + "vchiq_ioctl - instance %x, cmd %s, arg %lx",
  26735. + (unsigned int)instance,
  26736. + ((_IOC_TYPE(cmd) == VCHIQ_IOC_MAGIC) &&
  26737. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX)) ?
  26738. + ioctl_names[_IOC_NR(cmd)] : "<invalid>", arg);
  26739. +
  26740. + switch (cmd) {
  26741. + case VCHIQ_IOC_SHUTDOWN:
  26742. + if (!instance->connected)
  26743. + break;
  26744. +
  26745. + /* Remove all services */
  26746. + i = 0;
  26747. + while ((service = next_service_by_instance(instance->state,
  26748. + instance, &i)) != NULL) {
  26749. + status = vchiq_remove_service(service->handle);
  26750. + unlock_service(service);
  26751. + if (status != VCHIQ_SUCCESS)
  26752. + break;
  26753. + }
  26754. + service = NULL;
  26755. +
  26756. + if (status == VCHIQ_SUCCESS) {
  26757. + /* Wake the completion thread and ask it to exit */
  26758. + instance->closing = 1;
  26759. + up(&instance->insert_event);
  26760. + }
  26761. +
  26762. + break;
  26763. +
  26764. + case VCHIQ_IOC_CONNECT:
  26765. + if (instance->connected) {
  26766. + ret = -EINVAL;
  26767. + break;
  26768. + }
  26769. + rc = mutex_lock_interruptible(&instance->state->mutex);
  26770. + if (rc != 0) {
  26771. + vchiq_log_error(vchiq_arm_log_level,
  26772. + "vchiq: connect: could not lock mutex for "
  26773. + "state %d: %d",
  26774. + instance->state->id, rc);
  26775. + ret = -EINTR;
  26776. + break;
  26777. + }
  26778. + status = vchiq_connect_internal(instance->state, instance);
  26779. + mutex_unlock(&instance->state->mutex);
  26780. +
  26781. + if (status == VCHIQ_SUCCESS)
  26782. + instance->connected = 1;
  26783. + else
  26784. + vchiq_log_error(vchiq_arm_log_level,
  26785. + "vchiq: could not connect: %d", status);
  26786. + break;
  26787. +
  26788. + case VCHIQ_IOC_CREATE_SERVICE: {
  26789. + VCHIQ_CREATE_SERVICE_T args;
  26790. + USER_SERVICE_T *user_service = NULL;
  26791. + void *userdata;
  26792. + int srvstate;
  26793. +
  26794. + if (copy_from_user
  26795. + (&args, (const void __user *)arg,
  26796. + sizeof(args)) != 0) {
  26797. + ret = -EFAULT;
  26798. + break;
  26799. + }
  26800. +
  26801. + user_service = kmalloc(sizeof(USER_SERVICE_T), GFP_KERNEL);
  26802. + if (!user_service) {
  26803. + ret = -ENOMEM;
  26804. + break;
  26805. + }
  26806. +
  26807. + if (args.is_open) {
  26808. + if (!instance->connected) {
  26809. + ret = -ENOTCONN;
  26810. + kfree(user_service);
  26811. + break;
  26812. + }
  26813. + srvstate = VCHIQ_SRVSTATE_OPENING;
  26814. + } else {
  26815. + srvstate =
  26816. + instance->connected ?
  26817. + VCHIQ_SRVSTATE_LISTENING :
  26818. + VCHIQ_SRVSTATE_HIDDEN;
  26819. + }
  26820. +
  26821. + userdata = args.params.userdata;
  26822. + args.params.callback = service_callback;
  26823. + args.params.userdata = user_service;
  26824. + service = vchiq_add_service_internal(
  26825. + instance->state,
  26826. + &args.params, srvstate,
  26827. + instance, user_service_free);
  26828. +
  26829. + if (service != NULL) {
  26830. + user_service->service = service;
  26831. + user_service->userdata = userdata;
  26832. + user_service->instance = instance;
  26833. + user_service->is_vchi = (args.is_vchi != 0);
  26834. + user_service->dequeue_pending = 0;
  26835. + user_service->close_pending = 0;
  26836. + user_service->message_available_pos =
  26837. + instance->completion_remove - 1;
  26838. + user_service->msg_insert = 0;
  26839. + user_service->msg_remove = 0;
  26840. + sema_init(&user_service->insert_event, 0);
  26841. + sema_init(&user_service->remove_event, 0);
  26842. + sema_init(&user_service->close_event, 0);
  26843. +
  26844. + if (args.is_open) {
  26845. + status = vchiq_open_service_internal
  26846. + (service, instance->pid);
  26847. + if (status != VCHIQ_SUCCESS) {
  26848. + vchiq_remove_service(service->handle);
  26849. + service = NULL;
  26850. + ret = (status == VCHIQ_RETRY) ?
  26851. + -EINTR : -EIO;
  26852. + break;
  26853. + }
  26854. + }
  26855. +
  26856. + if (copy_to_user((void __user *)
  26857. + &(((VCHIQ_CREATE_SERVICE_T __user *)
  26858. + arg)->handle),
  26859. + (const void *)&service->handle,
  26860. + sizeof(service->handle)) != 0) {
  26861. + ret = -EFAULT;
  26862. + vchiq_remove_service(service->handle);
  26863. + }
  26864. +
  26865. + service = NULL;
  26866. + } else {
  26867. + ret = -EEXIST;
  26868. + kfree(user_service);
  26869. + }
  26870. + } break;
  26871. +
  26872. + case VCHIQ_IOC_CLOSE_SERVICE: {
  26873. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  26874. +
  26875. + service = find_service_for_instance(instance, handle);
  26876. + if (service != NULL) {
  26877. + USER_SERVICE_T *user_service =
  26878. + (USER_SERVICE_T *)service->base.userdata;
  26879. + /* close_pending is false on first entry, and when the
  26880. + wait in vchiq_close_service has been interrupted. */
  26881. + if (!user_service->close_pending) {
  26882. + status = vchiq_close_service(service->handle);
  26883. + if (status != VCHIQ_SUCCESS)
  26884. + break;
  26885. + }
  26886. +
  26887. + /* close_pending is true once the underlying service
  26888. + has been closed until the client library calls the
  26889. + CLOSE_DELIVERED ioctl, signalling close_event. */
  26890. + if (user_service->close_pending &&
  26891. + down_interruptible(&user_service->close_event))
  26892. + status = VCHIQ_RETRY;
  26893. + }
  26894. + else
  26895. + ret = -EINVAL;
  26896. + } break;
  26897. +
  26898. + case VCHIQ_IOC_REMOVE_SERVICE: {
  26899. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  26900. +
  26901. + service = find_service_for_instance(instance, handle);
  26902. + if (service != NULL) {
  26903. + USER_SERVICE_T *user_service =
  26904. + (USER_SERVICE_T *)service->base.userdata;
  26905. + /* close_pending is false on first entry, and when the
  26906. + wait in vchiq_close_service has been interrupted. */
  26907. + if (!user_service->close_pending) {
  26908. + status = vchiq_remove_service(service->handle);
  26909. + if (status != VCHIQ_SUCCESS)
  26910. + break;
  26911. + }
  26912. +
  26913. + /* close_pending is true once the underlying service
  26914. + has been closed until the client library calls the
  26915. + CLOSE_DELIVERED ioctl, signalling close_event. */
  26916. + if (user_service->close_pending &&
  26917. + down_interruptible(&user_service->close_event))
  26918. + status = VCHIQ_RETRY;
  26919. + }
  26920. + else
  26921. + ret = -EINVAL;
  26922. + } break;
  26923. +
  26924. + case VCHIQ_IOC_USE_SERVICE:
  26925. + case VCHIQ_IOC_RELEASE_SERVICE: {
  26926. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  26927. +
  26928. + service = find_service_for_instance(instance, handle);
  26929. + if (service != NULL) {
  26930. + status = (cmd == VCHIQ_IOC_USE_SERVICE) ?
  26931. + vchiq_use_service_internal(service) :
  26932. + vchiq_release_service_internal(service);
  26933. + if (status != VCHIQ_SUCCESS) {
  26934. + vchiq_log_error(vchiq_susp_log_level,
  26935. + "%s: cmd %s returned error %d for "
  26936. + "service %c%c%c%c:%03d",
  26937. + __func__,
  26938. + (cmd == VCHIQ_IOC_USE_SERVICE) ?
  26939. + "VCHIQ_IOC_USE_SERVICE" :
  26940. + "VCHIQ_IOC_RELEASE_SERVICE",
  26941. + status,
  26942. + VCHIQ_FOURCC_AS_4CHARS(
  26943. + service->base.fourcc),
  26944. + service->client_id);
  26945. + ret = -EINVAL;
  26946. + }
  26947. + } else
  26948. + ret = -EINVAL;
  26949. + } break;
  26950. +
  26951. + case VCHIQ_IOC_QUEUE_MESSAGE: {
  26952. + VCHIQ_QUEUE_MESSAGE_T args;
  26953. + if (copy_from_user
  26954. + (&args, (const void __user *)arg,
  26955. + sizeof(args)) != 0) {
  26956. + ret = -EFAULT;
  26957. + break;
  26958. + }
  26959. +
  26960. + service = find_service_for_instance(instance, args.handle);
  26961. +
  26962. + if ((service != NULL) && (args.count <= MAX_ELEMENTS)) {
  26963. + /* Copy elements into kernel space */
  26964. + VCHIQ_ELEMENT_T elements[MAX_ELEMENTS];
  26965. + if (copy_from_user(elements, args.elements,
  26966. + args.count * sizeof(VCHIQ_ELEMENT_T)) == 0)
  26967. + status = vchiq_queue_message
  26968. + (args.handle,
  26969. + elements, args.count);
  26970. + else
  26971. + ret = -EFAULT;
  26972. + } else {
  26973. + ret = -EINVAL;
  26974. + }
  26975. + } break;
  26976. +
  26977. + case VCHIQ_IOC_QUEUE_BULK_TRANSMIT:
  26978. + case VCHIQ_IOC_QUEUE_BULK_RECEIVE: {
  26979. + VCHIQ_QUEUE_BULK_TRANSFER_T args;
  26980. + struct bulk_waiter_node *waiter = NULL;
  26981. + VCHIQ_BULK_DIR_T dir =
  26982. + (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT) ?
  26983. + VCHIQ_BULK_TRANSMIT : VCHIQ_BULK_RECEIVE;
  26984. +
  26985. + if (copy_from_user
  26986. + (&args, (const void __user *)arg,
  26987. + sizeof(args)) != 0) {
  26988. + ret = -EFAULT;
  26989. + break;
  26990. + }
  26991. +
  26992. + service = find_service_for_instance(instance, args.handle);
  26993. + if (!service) {
  26994. + ret = -EINVAL;
  26995. + break;
  26996. + }
  26997. +
  26998. + if (args.mode == VCHIQ_BULK_MODE_BLOCKING) {
  26999. + waiter = kzalloc(sizeof(struct bulk_waiter_node),
  27000. + GFP_KERNEL);
  27001. + if (!waiter) {
  27002. + ret = -ENOMEM;
  27003. + break;
  27004. + }
  27005. + args.userdata = &waiter->bulk_waiter;
  27006. + } else if (args.mode == VCHIQ_BULK_MODE_WAITING) {
  27007. + struct list_head *pos;
  27008. + mutex_lock(&instance->bulk_waiter_list_mutex);
  27009. + list_for_each(pos, &instance->bulk_waiter_list) {
  27010. + if (list_entry(pos, struct bulk_waiter_node,
  27011. + list)->pid == current->pid) {
  27012. + waiter = list_entry(pos,
  27013. + struct bulk_waiter_node,
  27014. + list);
  27015. + list_del(pos);
  27016. + break;
  27017. + }
  27018. +
  27019. + }
  27020. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  27021. + if (!waiter) {
  27022. + vchiq_log_error(vchiq_arm_log_level,
  27023. + "no bulk_waiter found for pid %d",
  27024. + current->pid);
  27025. + ret = -ESRCH;
  27026. + break;
  27027. + }
  27028. + vchiq_log_info(vchiq_arm_log_level,
  27029. + "found bulk_waiter %x for pid %d",
  27030. + (unsigned int)waiter, current->pid);
  27031. + args.userdata = &waiter->bulk_waiter;
  27032. + }
  27033. + status = vchiq_bulk_transfer
  27034. + (args.handle,
  27035. + VCHI_MEM_HANDLE_INVALID,
  27036. + args.data, args.size,
  27037. + args.userdata, args.mode,
  27038. + dir);
  27039. + if (!waiter)
  27040. + break;
  27041. + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
  27042. + !waiter->bulk_waiter.bulk) {
  27043. + if (waiter->bulk_waiter.bulk) {
  27044. + /* Cancel the signal when the transfer
  27045. + ** completes. */
  27046. + spin_lock(&bulk_waiter_spinlock);
  27047. + waiter->bulk_waiter.bulk->userdata = NULL;
  27048. + spin_unlock(&bulk_waiter_spinlock);
  27049. + }
  27050. + kfree(waiter);
  27051. + } else {
  27052. + const VCHIQ_BULK_MODE_T mode_waiting =
  27053. + VCHIQ_BULK_MODE_WAITING;
  27054. + waiter->pid = current->pid;
  27055. + mutex_lock(&instance->bulk_waiter_list_mutex);
  27056. + list_add(&waiter->list, &instance->bulk_waiter_list);
  27057. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  27058. + vchiq_log_info(vchiq_arm_log_level,
  27059. + "saved bulk_waiter %x for pid %d",
  27060. + (unsigned int)waiter, current->pid);
  27061. +
  27062. + if (copy_to_user((void __user *)
  27063. + &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user *)
  27064. + arg)->mode),
  27065. + (const void *)&mode_waiting,
  27066. + sizeof(mode_waiting)) != 0)
  27067. + ret = -EFAULT;
  27068. + }
  27069. + } break;
  27070. +
  27071. + case VCHIQ_IOC_AWAIT_COMPLETION: {
  27072. + VCHIQ_AWAIT_COMPLETION_T args;
  27073. +
  27074. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  27075. + if (!instance->connected) {
  27076. + ret = -ENOTCONN;
  27077. + break;
  27078. + }
  27079. +
  27080. + if (copy_from_user(&args, (const void __user *)arg,
  27081. + sizeof(args)) != 0) {
  27082. + ret = -EFAULT;
  27083. + break;
  27084. + }
  27085. +
  27086. + mutex_lock(&instance->completion_mutex);
  27087. +
  27088. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  27089. + while ((instance->completion_remove ==
  27090. + instance->completion_insert)
  27091. + && !instance->closing) {
  27092. + int rc;
  27093. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  27094. + mutex_unlock(&instance->completion_mutex);
  27095. + rc = down_interruptible(&instance->insert_event);
  27096. + mutex_lock(&instance->completion_mutex);
  27097. + if (rc != 0) {
  27098. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  27099. + vchiq_log_info(vchiq_arm_log_level,
  27100. + "AWAIT_COMPLETION interrupted");
  27101. + ret = -EINTR;
  27102. + break;
  27103. + }
  27104. + }
  27105. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  27106. +
  27107. + /* A read memory barrier is needed to stop prefetch of a stale
  27108. + ** completion record
  27109. + */
  27110. + rmb();
  27111. +
  27112. + if (ret == 0) {
  27113. + int msgbufcount = args.msgbufcount;
  27114. + for (ret = 0; ret < args.count; ret++) {
  27115. + VCHIQ_COMPLETION_DATA_T *completion;
  27116. + VCHIQ_SERVICE_T *service;
  27117. + USER_SERVICE_T *user_service;
  27118. + VCHIQ_HEADER_T *header;
  27119. + if (instance->completion_remove ==
  27120. + instance->completion_insert)
  27121. + break;
  27122. + completion = &instance->completions[
  27123. + instance->completion_remove &
  27124. + (MAX_COMPLETIONS - 1)];
  27125. +
  27126. + service = completion->service_userdata;
  27127. + user_service = service->base.userdata;
  27128. + completion->service_userdata =
  27129. + user_service->userdata;
  27130. +
  27131. + header = completion->header;
  27132. + if (header) {
  27133. + void __user *msgbuf;
  27134. + int msglen;
  27135. +
  27136. + msglen = header->size +
  27137. + sizeof(VCHIQ_HEADER_T);
  27138. + /* This must be a VCHIQ-style service */
  27139. + if (args.msgbufsize < msglen) {
  27140. + vchiq_log_error(
  27141. + vchiq_arm_log_level,
  27142. + "header %x: msgbufsize"
  27143. + " %x < msglen %x",
  27144. + (unsigned int)header,
  27145. + args.msgbufsize,
  27146. + msglen);
  27147. + WARN(1, "invalid message "
  27148. + "size\n");
  27149. + if (ret == 0)
  27150. + ret = -EMSGSIZE;
  27151. + break;
  27152. + }
  27153. + if (msgbufcount <= 0)
  27154. + /* Stall here for lack of a
  27155. + ** buffer for the message. */
  27156. + break;
  27157. + /* Get the pointer from user space */
  27158. + msgbufcount--;
  27159. + if (copy_from_user(&msgbuf,
  27160. + (const void __user *)
  27161. + &args.msgbufs[msgbufcount],
  27162. + sizeof(msgbuf)) != 0) {
  27163. + if (ret == 0)
  27164. + ret = -EFAULT;
  27165. + break;
  27166. + }
  27167. +
  27168. + /* Copy the message to user space */
  27169. + if (copy_to_user(msgbuf, header,
  27170. + msglen) != 0) {
  27171. + if (ret == 0)
  27172. + ret = -EFAULT;
  27173. + break;
  27174. + }
  27175. +
  27176. + /* Now it has been copied, the message
  27177. + ** can be released. */
  27178. + vchiq_release_message(service->handle,
  27179. + header);
  27180. +
  27181. + /* The completion must point to the
  27182. + ** msgbuf. */
  27183. + completion->header = msgbuf;
  27184. + }
  27185. +
  27186. + if ((completion->reason ==
  27187. + VCHIQ_SERVICE_CLOSED) &&
  27188. + !instance->use_close_delivered)
  27189. + unlock_service(service);
  27190. +
  27191. + if (copy_to_user((void __user *)(
  27192. + (size_t)args.buf +
  27193. + ret * sizeof(VCHIQ_COMPLETION_DATA_T)),
  27194. + completion,
  27195. + sizeof(VCHIQ_COMPLETION_DATA_T)) != 0) {
  27196. + if (ret == 0)
  27197. + ret = -EFAULT;
  27198. + break;
  27199. + }
  27200. +
  27201. + instance->completion_remove++;
  27202. + }
  27203. +
  27204. + if (msgbufcount != args.msgbufcount) {
  27205. + if (copy_to_user((void __user *)
  27206. + &((VCHIQ_AWAIT_COMPLETION_T *)arg)->
  27207. + msgbufcount,
  27208. + &msgbufcount,
  27209. + sizeof(msgbufcount)) != 0) {
  27210. + ret = -EFAULT;
  27211. + }
  27212. + }
  27213. + }
  27214. +
  27215. + if (ret != 0)
  27216. + up(&instance->remove_event);
  27217. + mutex_unlock(&instance->completion_mutex);
  27218. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  27219. + } break;
  27220. +
  27221. + case VCHIQ_IOC_DEQUEUE_MESSAGE: {
  27222. + VCHIQ_DEQUEUE_MESSAGE_T args;
  27223. + USER_SERVICE_T *user_service;
  27224. + VCHIQ_HEADER_T *header;
  27225. +
  27226. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  27227. + if (copy_from_user
  27228. + (&args, (const void __user *)arg,
  27229. + sizeof(args)) != 0) {
  27230. + ret = -EFAULT;
  27231. + break;
  27232. + }
  27233. + service = find_service_for_instance(instance, args.handle);
  27234. + if (!service) {
  27235. + ret = -EINVAL;
  27236. + break;
  27237. + }
  27238. + user_service = (USER_SERVICE_T *)service->base.userdata;
  27239. + if (user_service->is_vchi == 0) {
  27240. + ret = -EINVAL;
  27241. + break;
  27242. + }
  27243. +
  27244. + spin_lock(&msg_queue_spinlock);
  27245. + if (user_service->msg_remove == user_service->msg_insert) {
  27246. + if (!args.blocking) {
  27247. + spin_unlock(&msg_queue_spinlock);
  27248. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  27249. + ret = -EWOULDBLOCK;
  27250. + break;
  27251. + }
  27252. + user_service->dequeue_pending = 1;
  27253. + do {
  27254. + spin_unlock(&msg_queue_spinlock);
  27255. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  27256. + if (down_interruptible(
  27257. + &user_service->insert_event) != 0) {
  27258. + vchiq_log_info(vchiq_arm_log_level,
  27259. + "DEQUEUE_MESSAGE interrupted");
  27260. + ret = -EINTR;
  27261. + break;
  27262. + }
  27263. + spin_lock(&msg_queue_spinlock);
  27264. + } while (user_service->msg_remove ==
  27265. + user_service->msg_insert);
  27266. +
  27267. + if (ret)
  27268. + break;
  27269. + }
  27270. +
  27271. + BUG_ON((int)(user_service->msg_insert -
  27272. + user_service->msg_remove) < 0);
  27273. +
  27274. + header = user_service->msg_queue[user_service->msg_remove &
  27275. + (MSG_QUEUE_SIZE - 1)];
  27276. + user_service->msg_remove++;
  27277. + spin_unlock(&msg_queue_spinlock);
  27278. +
  27279. + up(&user_service->remove_event);
  27280. + if (header == NULL)
  27281. + ret = -ENOTCONN;
  27282. + else if (header->size <= args.bufsize) {
  27283. + /* Copy to user space if msgbuf is not NULL */
  27284. + if ((args.buf == NULL) ||
  27285. + (copy_to_user((void __user *)args.buf,
  27286. + header->data,
  27287. + header->size) == 0)) {
  27288. + ret = header->size;
  27289. + vchiq_release_message(
  27290. + service->handle,
  27291. + header);
  27292. + } else
  27293. + ret = -EFAULT;
  27294. + } else {
  27295. + vchiq_log_error(vchiq_arm_log_level,
  27296. + "header %x: bufsize %x < size %x",
  27297. + (unsigned int)header, args.bufsize,
  27298. + header->size);
  27299. + WARN(1, "invalid size\n");
  27300. + ret = -EMSGSIZE;
  27301. + }
  27302. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  27303. + } break;
  27304. +
  27305. + case VCHIQ_IOC_GET_CLIENT_ID: {
  27306. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  27307. +
  27308. + ret = vchiq_get_client_id(handle);
  27309. + } break;
  27310. +
  27311. + case VCHIQ_IOC_GET_CONFIG: {
  27312. + VCHIQ_GET_CONFIG_T args;
  27313. + VCHIQ_CONFIG_T config;
  27314. +
  27315. + if (copy_from_user(&args, (const void __user *)arg,
  27316. + sizeof(args)) != 0) {
  27317. + ret = -EFAULT;
  27318. + break;
  27319. + }
  27320. + if (args.config_size > sizeof(config)) {
  27321. + ret = -EINVAL;
  27322. + break;
  27323. + }
  27324. + status = vchiq_get_config(instance, args.config_size, &config);
  27325. + if (status == VCHIQ_SUCCESS) {
  27326. + if (copy_to_user((void __user *)args.pconfig,
  27327. + &config, args.config_size) != 0) {
  27328. + ret = -EFAULT;
  27329. + break;
  27330. + }
  27331. + }
  27332. + } break;
  27333. +
  27334. + case VCHIQ_IOC_SET_SERVICE_OPTION: {
  27335. + VCHIQ_SET_SERVICE_OPTION_T args;
  27336. +
  27337. + if (copy_from_user(
  27338. + &args, (const void __user *)arg,
  27339. + sizeof(args)) != 0) {
  27340. + ret = -EFAULT;
  27341. + break;
  27342. + }
  27343. +
  27344. + service = find_service_for_instance(instance, args.handle);
  27345. + if (!service) {
  27346. + ret = -EINVAL;
  27347. + break;
  27348. + }
  27349. +
  27350. + status = vchiq_set_service_option(
  27351. + args.handle, args.option, args.value);
  27352. + } break;
  27353. +
  27354. + case VCHIQ_IOC_DUMP_PHYS_MEM: {
  27355. + VCHIQ_DUMP_MEM_T args;
  27356. +
  27357. + if (copy_from_user
  27358. + (&args, (const void __user *)arg,
  27359. + sizeof(args)) != 0) {
  27360. + ret = -EFAULT;
  27361. + break;
  27362. + }
  27363. + dump_phys_mem(args.virt_addr, args.num_bytes);
  27364. + } break;
  27365. +
  27366. + case VCHIQ_IOC_LIB_VERSION: {
  27367. + unsigned int lib_version = (unsigned int)arg;
  27368. +
  27369. + if (lib_version < VCHIQ_VERSION_MIN)
  27370. + ret = -EINVAL;
  27371. + else if (lib_version >= VCHIQ_VERSION_CLOSE_DELIVERED)
  27372. + instance->use_close_delivered = 1;
  27373. + } break;
  27374. +
  27375. + case VCHIQ_IOC_CLOSE_DELIVERED: {
  27376. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  27377. +
  27378. + service = find_closed_service_for_instance(instance, handle);
  27379. + if (service != NULL) {
  27380. + USER_SERVICE_T *user_service =
  27381. + (USER_SERVICE_T *)service->base.userdata;
  27382. + close_delivered(user_service);
  27383. + }
  27384. + else
  27385. + ret = -EINVAL;
  27386. + } break;
  27387. +
  27388. + default:
  27389. + ret = -ENOTTY;
  27390. + break;
  27391. + }
  27392. +
  27393. + if (service)
  27394. + unlock_service(service);
  27395. +
  27396. + if (ret == 0) {
  27397. + if (status == VCHIQ_ERROR)
  27398. + ret = -EIO;
  27399. + else if (status == VCHIQ_RETRY)
  27400. + ret = -EINTR;
  27401. + }
  27402. +
  27403. + if ((status == VCHIQ_SUCCESS) && (ret < 0) && (ret != -EINTR) &&
  27404. + (ret != -EWOULDBLOCK))
  27405. + vchiq_log_info(vchiq_arm_log_level,
  27406. + " ioctl instance %lx, cmd %s -> status %d, %ld",
  27407. + (unsigned long)instance,
  27408. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
  27409. + ioctl_names[_IOC_NR(cmd)] :
  27410. + "<invalid>",
  27411. + status, ret);
  27412. + else
  27413. + vchiq_log_trace(vchiq_arm_log_level,
  27414. + " ioctl instance %lx, cmd %s -> status %d, %ld",
  27415. + (unsigned long)instance,
  27416. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
  27417. + ioctl_names[_IOC_NR(cmd)] :
  27418. + "<invalid>",
  27419. + status, ret);
  27420. +
  27421. + return ret;
  27422. +}
  27423. +
  27424. +/****************************************************************************
  27425. +*
  27426. +* vchiq_open
  27427. +*
  27428. +***************************************************************************/
  27429. +
  27430. +static int
  27431. +vchiq_open(struct inode *inode, struct file *file)
  27432. +{
  27433. + int dev = iminor(inode) & 0x0f;
  27434. + vchiq_log_info(vchiq_arm_log_level, "vchiq_open");
  27435. + switch (dev) {
  27436. + case VCHIQ_MINOR: {
  27437. + int ret;
  27438. + VCHIQ_STATE_T *state = vchiq_get_state();
  27439. + VCHIQ_INSTANCE_T instance;
  27440. +
  27441. + if (!state) {
  27442. + vchiq_log_error(vchiq_arm_log_level,
  27443. + "vchiq has no connection to VideoCore");
  27444. + return -ENOTCONN;
  27445. + }
  27446. +
  27447. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  27448. + if (!instance)
  27449. + return -ENOMEM;
  27450. +
  27451. + instance->state = state;
  27452. + instance->pid = current->tgid;
  27453. +
  27454. + ret = vchiq_debugfs_add_instance(instance);
  27455. + if (ret != 0) {
  27456. + kfree(instance);
  27457. + return ret;
  27458. + }
  27459. +
  27460. + sema_init(&instance->insert_event, 0);
  27461. + sema_init(&instance->remove_event, 0);
  27462. + mutex_init(&instance->completion_mutex);
  27463. + mutex_init(&instance->bulk_waiter_list_mutex);
  27464. + INIT_LIST_HEAD(&instance->bulk_waiter_list);
  27465. +
  27466. + file->private_data = instance;
  27467. + } break;
  27468. +
  27469. + default:
  27470. + vchiq_log_error(vchiq_arm_log_level,
  27471. + "Unknown minor device: %d", dev);
  27472. + return -ENXIO;
  27473. + }
  27474. +
  27475. + return 0;
  27476. +}
  27477. +
  27478. +/****************************************************************************
  27479. +*
  27480. +* vchiq_release
  27481. +*
  27482. +***************************************************************************/
  27483. +
  27484. +static int
  27485. +vchiq_release(struct inode *inode, struct file *file)
  27486. +{
  27487. + int dev = iminor(inode) & 0x0f;
  27488. + int ret = 0;
  27489. + switch (dev) {
  27490. + case VCHIQ_MINOR: {
  27491. + VCHIQ_INSTANCE_T instance = file->private_data;
  27492. + VCHIQ_STATE_T *state = vchiq_get_state();
  27493. + VCHIQ_SERVICE_T *service;
  27494. + int i;
  27495. +
  27496. + vchiq_log_info(vchiq_arm_log_level,
  27497. + "vchiq_release: instance=%lx",
  27498. + (unsigned long)instance);
  27499. +
  27500. + if (!state) {
  27501. + ret = -EPERM;
  27502. + goto out;
  27503. + }
  27504. +
  27505. + /* Ensure videocore is awake to allow termination. */
  27506. + vchiq_use_internal(instance->state, NULL,
  27507. + USE_TYPE_VCHIQ);
  27508. +
  27509. + mutex_lock(&instance->completion_mutex);
  27510. +
  27511. + /* Wake the completion thread and ask it to exit */
  27512. + instance->closing = 1;
  27513. + up(&instance->insert_event);
  27514. +
  27515. + mutex_unlock(&instance->completion_mutex);
  27516. +
  27517. + /* Wake the slot handler if the completion queue is full. */
  27518. + up(&instance->remove_event);
  27519. +
  27520. + /* Mark all services for termination... */
  27521. + i = 0;
  27522. + while ((service = next_service_by_instance(state, instance,
  27523. + &i)) != NULL) {
  27524. + USER_SERVICE_T *user_service = service->base.userdata;
  27525. +
  27526. + /* Wake the slot handler if the msg queue is full. */
  27527. + up(&user_service->remove_event);
  27528. +
  27529. + vchiq_terminate_service_internal(service);
  27530. + unlock_service(service);
  27531. + }
  27532. +
  27533. + /* ...and wait for them to die */
  27534. + i = 0;
  27535. + while ((service = next_service_by_instance(state, instance, &i))
  27536. + != NULL) {
  27537. + USER_SERVICE_T *user_service = service->base.userdata;
  27538. +
  27539. + down(&service->remove_event);
  27540. +
  27541. + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
  27542. +
  27543. + spin_lock(&msg_queue_spinlock);
  27544. +
  27545. + while (user_service->msg_remove !=
  27546. + user_service->msg_insert) {
  27547. + VCHIQ_HEADER_T *header = user_service->
  27548. + msg_queue[user_service->msg_remove &
  27549. + (MSG_QUEUE_SIZE - 1)];
  27550. + user_service->msg_remove++;
  27551. + spin_unlock(&msg_queue_spinlock);
  27552. +
  27553. + if (header)
  27554. + vchiq_release_message(
  27555. + service->handle,
  27556. + header);
  27557. + spin_lock(&msg_queue_spinlock);
  27558. + }
  27559. +
  27560. + spin_unlock(&msg_queue_spinlock);
  27561. +
  27562. + unlock_service(service);
  27563. + }
  27564. +
  27565. + /* Release any closed services */
  27566. + while (instance->completion_remove !=
  27567. + instance->completion_insert) {
  27568. + VCHIQ_COMPLETION_DATA_T *completion;
  27569. + VCHIQ_SERVICE_T *service;
  27570. + completion = &instance->completions[
  27571. + instance->completion_remove &
  27572. + (MAX_COMPLETIONS - 1)];
  27573. + service = completion->service_userdata;
  27574. + if (completion->reason == VCHIQ_SERVICE_CLOSED)
  27575. + {
  27576. + USER_SERVICE_T *user_service =
  27577. + service->base.userdata;
  27578. +
  27579. + /* Wake any blocked user-thread */
  27580. + if (instance->use_close_delivered)
  27581. + up(&user_service->close_event);
  27582. + unlock_service(service);
  27583. + }
  27584. + instance->completion_remove++;
  27585. + }
  27586. +
  27587. + /* Release the PEER service count. */
  27588. + vchiq_release_internal(instance->state, NULL);
  27589. +
  27590. + {
  27591. + struct list_head *pos, *next;
  27592. + list_for_each_safe(pos, next,
  27593. + &instance->bulk_waiter_list) {
  27594. + struct bulk_waiter_node *waiter;
  27595. + waiter = list_entry(pos,
  27596. + struct bulk_waiter_node,
  27597. + list);
  27598. + list_del(pos);
  27599. + vchiq_log_info(vchiq_arm_log_level,
  27600. + "bulk_waiter - cleaned up %x "
  27601. + "for pid %d",
  27602. + (unsigned int)waiter, waiter->pid);
  27603. + kfree(waiter);
  27604. + }
  27605. + }
  27606. +
  27607. + vchiq_debugfs_remove_instance(instance);
  27608. +
  27609. + kfree(instance);
  27610. + file->private_data = NULL;
  27611. + } break;
  27612. +
  27613. + default:
  27614. + vchiq_log_error(vchiq_arm_log_level,
  27615. + "Unknown minor device: %d", dev);
  27616. + ret = -ENXIO;
  27617. + }
  27618. +
  27619. +out:
  27620. + return ret;
  27621. +}
  27622. +
  27623. +/****************************************************************************
  27624. +*
  27625. +* vchiq_dump
  27626. +*
  27627. +***************************************************************************/
  27628. +
  27629. +void
  27630. +vchiq_dump(void *dump_context, const char *str, int len)
  27631. +{
  27632. + DUMP_CONTEXT_T *context = (DUMP_CONTEXT_T *)dump_context;
  27633. +
  27634. + if (context->actual < context->space) {
  27635. + int copy_bytes;
  27636. + if (context->offset > 0) {
  27637. + int skip_bytes = min(len, (int)context->offset);
  27638. + str += skip_bytes;
  27639. + len -= skip_bytes;
  27640. + context->offset -= skip_bytes;
  27641. + if (context->offset > 0)
  27642. + return;
  27643. + }
  27644. + copy_bytes = min(len, (int)(context->space - context->actual));
  27645. + if (copy_bytes == 0)
  27646. + return;
  27647. + if (copy_to_user(context->buf + context->actual, str,
  27648. + copy_bytes))
  27649. + context->actual = -EFAULT;
  27650. + context->actual += copy_bytes;
  27651. + len -= copy_bytes;
  27652. +
  27653. + /* If tne terminating NUL is included in the length, then it
  27654. + ** marks the end of a line and should be replaced with a
  27655. + ** carriage return. */
  27656. + if ((len == 0) && (str[copy_bytes - 1] == '\0')) {
  27657. + char cr = '\n';
  27658. + if (copy_to_user(context->buf + context->actual - 1,
  27659. + &cr, 1))
  27660. + context->actual = -EFAULT;
  27661. + }
  27662. + }
  27663. +}
  27664. +
  27665. +/****************************************************************************
  27666. +*
  27667. +* vchiq_dump_platform_instance_state
  27668. +*
  27669. +***************************************************************************/
  27670. +
  27671. +void
  27672. +vchiq_dump_platform_instances(void *dump_context)
  27673. +{
  27674. + VCHIQ_STATE_T *state = vchiq_get_state();
  27675. + char buf[80];
  27676. + int len;
  27677. + int i;
  27678. +
  27679. + /* There is no list of instances, so instead scan all services,
  27680. + marking those that have been dumped. */
  27681. +
  27682. + for (i = 0; i < state->unused_service; i++) {
  27683. + VCHIQ_SERVICE_T *service = state->services[i];
  27684. + VCHIQ_INSTANCE_T instance;
  27685. +
  27686. + if (service && (service->base.callback == service_callback)) {
  27687. + instance = service->instance;
  27688. + if (instance)
  27689. + instance->mark = 0;
  27690. + }
  27691. + }
  27692. +
  27693. + for (i = 0; i < state->unused_service; i++) {
  27694. + VCHIQ_SERVICE_T *service = state->services[i];
  27695. + VCHIQ_INSTANCE_T instance;
  27696. +
  27697. + if (service && (service->base.callback == service_callback)) {
  27698. + instance = service->instance;
  27699. + if (instance && !instance->mark) {
  27700. + len = snprintf(buf, sizeof(buf),
  27701. + "Instance %x: pid %d,%s completions "
  27702. + "%d/%d",
  27703. + (unsigned int)instance, instance->pid,
  27704. + instance->connected ? " connected, " :
  27705. + "",
  27706. + instance->completion_insert -
  27707. + instance->completion_remove,
  27708. + MAX_COMPLETIONS);
  27709. +
  27710. + vchiq_dump(dump_context, buf, len + 1);
  27711. +
  27712. + instance->mark = 1;
  27713. + }
  27714. + }
  27715. + }
  27716. +}
  27717. +
  27718. +/****************************************************************************
  27719. +*
  27720. +* vchiq_dump_platform_service_state
  27721. +*
  27722. +***************************************************************************/
  27723. +
  27724. +void
  27725. +vchiq_dump_platform_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
  27726. +{
  27727. + USER_SERVICE_T *user_service = (USER_SERVICE_T *)service->base.userdata;
  27728. + char buf[80];
  27729. + int len;
  27730. +
  27731. + len = snprintf(buf, sizeof(buf), " instance %x",
  27732. + (unsigned int)service->instance);
  27733. +
  27734. + if ((service->base.callback == service_callback) &&
  27735. + user_service->is_vchi) {
  27736. + len += snprintf(buf + len, sizeof(buf) - len,
  27737. + ", %d/%d messages",
  27738. + user_service->msg_insert - user_service->msg_remove,
  27739. + MSG_QUEUE_SIZE);
  27740. +
  27741. + if (user_service->dequeue_pending)
  27742. + len += snprintf(buf + len, sizeof(buf) - len,
  27743. + " (dequeue pending)");
  27744. + }
  27745. +
  27746. + vchiq_dump(dump_context, buf, len + 1);
  27747. +}
  27748. +
  27749. +/****************************************************************************
  27750. +*
  27751. +* dump_user_mem
  27752. +*
  27753. +***************************************************************************/
  27754. +
  27755. +static void
  27756. +dump_phys_mem(void *virt_addr, uint32_t num_bytes)
  27757. +{
  27758. + int rc;
  27759. + uint8_t *end_virt_addr = virt_addr + num_bytes;
  27760. + int num_pages;
  27761. + int offset;
  27762. + int end_offset;
  27763. + int page_idx;
  27764. + int prev_idx;
  27765. + struct page *page;
  27766. + struct page **pages;
  27767. + uint8_t *kmapped_virt_ptr;
  27768. +
  27769. + /* Align virtAddr and endVirtAddr to 16 byte boundaries. */
  27770. +
  27771. + virt_addr = (void *)((unsigned long)virt_addr & ~0x0fuL);
  27772. + end_virt_addr = (void *)(((unsigned long)end_virt_addr + 15uL) &
  27773. + ~0x0fuL);
  27774. +
  27775. + offset = (int)(long)virt_addr & (PAGE_SIZE - 1);
  27776. + end_offset = (int)(long)end_virt_addr & (PAGE_SIZE - 1);
  27777. +
  27778. + num_pages = (offset + num_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
  27779. +
  27780. + pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  27781. + if (pages == NULL) {
  27782. + vchiq_log_error(vchiq_arm_log_level,
  27783. + "Unable to allocation memory for %d pages\n",
  27784. + num_pages);
  27785. + return;
  27786. + }
  27787. +
  27788. + down_read(&current->mm->mmap_sem);
  27789. + rc = get_user_pages(current, /* task */
  27790. + current->mm, /* mm */
  27791. + (unsigned long)virt_addr, /* start */
  27792. + num_pages, /* len */
  27793. + 0, /* write */
  27794. + 0, /* force */
  27795. + pages, /* pages (array of page pointers) */
  27796. + NULL); /* vmas */
  27797. + up_read(&current->mm->mmap_sem);
  27798. +
  27799. + prev_idx = -1;
  27800. + page = NULL;
  27801. +
  27802. + while (offset < end_offset) {
  27803. +
  27804. + int page_offset = offset % PAGE_SIZE;
  27805. + page_idx = offset / PAGE_SIZE;
  27806. +
  27807. + if (page_idx != prev_idx) {
  27808. +
  27809. + if (page != NULL)
  27810. + kunmap(page);
  27811. + page = pages[page_idx];
  27812. + kmapped_virt_ptr = kmap(page);
  27813. +
  27814. + prev_idx = page_idx;
  27815. + }
  27816. +
  27817. + if (vchiq_arm_log_level >= VCHIQ_LOG_TRACE)
  27818. + vchiq_log_dump_mem("ph",
  27819. + (uint32_t)(unsigned long)&kmapped_virt_ptr[
  27820. + page_offset],
  27821. + &kmapped_virt_ptr[page_offset], 16);
  27822. +
  27823. + offset += 16;
  27824. + }
  27825. + if (page != NULL)
  27826. + kunmap(page);
  27827. +
  27828. + for (page_idx = 0; page_idx < num_pages; page_idx++)
  27829. + page_cache_release(pages[page_idx]);
  27830. +
  27831. + kfree(pages);
  27832. +}
  27833. +
  27834. +/****************************************************************************
  27835. +*
  27836. +* vchiq_read
  27837. +*
  27838. +***************************************************************************/
  27839. +
  27840. +static ssize_t
  27841. +vchiq_read(struct file *file, char __user *buf,
  27842. + size_t count, loff_t *ppos)
  27843. +{
  27844. + DUMP_CONTEXT_T context;
  27845. + context.buf = buf;
  27846. + context.actual = 0;
  27847. + context.space = count;
  27848. + context.offset = *ppos;
  27849. +
  27850. + vchiq_dump_state(&context, &g_state);
  27851. +
  27852. + *ppos += context.actual;
  27853. +
  27854. + return context.actual;
  27855. +}
  27856. +
  27857. +VCHIQ_STATE_T *
  27858. +vchiq_get_state(void)
  27859. +{
  27860. +
  27861. + if (g_state.remote == NULL)
  27862. + printk(KERN_ERR "%s: g_state.remote == NULL\n", __func__);
  27863. + else if (g_state.remote->initialised != 1)
  27864. + printk(KERN_NOTICE "%s: g_state.remote->initialised != 1 (%d)\n",
  27865. + __func__, g_state.remote->initialised);
  27866. +
  27867. + return ((g_state.remote != NULL) &&
  27868. + (g_state.remote->initialised == 1)) ? &g_state : NULL;
  27869. +}
  27870. +
  27871. +static const struct file_operations
  27872. +vchiq_fops = {
  27873. + .owner = THIS_MODULE,
  27874. + .unlocked_ioctl = vchiq_ioctl,
  27875. + .open = vchiq_open,
  27876. + .release = vchiq_release,
  27877. + .read = vchiq_read
  27878. +};
  27879. +
  27880. +/*
  27881. + * Autosuspend related functionality
  27882. + */
  27883. +
  27884. +int
  27885. +vchiq_videocore_wanted(VCHIQ_STATE_T *state)
  27886. +{
  27887. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27888. + if (!arm_state)
  27889. + /* autosuspend not supported - always return wanted */
  27890. + return 1;
  27891. + else if (arm_state->blocked_count)
  27892. + return 1;
  27893. + else if (!arm_state->videocore_use_count)
  27894. + /* usage count zero - check for override unless we're forcing */
  27895. + if (arm_state->resume_blocked)
  27896. + return 0;
  27897. + else
  27898. + return vchiq_platform_videocore_wanted(state);
  27899. + else
  27900. + /* non-zero usage count - videocore still required */
  27901. + return 1;
  27902. +}
  27903. +
  27904. +static VCHIQ_STATUS_T
  27905. +vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason,
  27906. + VCHIQ_HEADER_T *header,
  27907. + VCHIQ_SERVICE_HANDLE_T service_user,
  27908. + void *bulk_user)
  27909. +{
  27910. + vchiq_log_error(vchiq_susp_log_level,
  27911. + "%s callback reason %d", __func__, reason);
  27912. + return 0;
  27913. +}
  27914. +
  27915. +static int
  27916. +vchiq_keepalive_thread_func(void *v)
  27917. +{
  27918. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  27919. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27920. +
  27921. + VCHIQ_STATUS_T status;
  27922. + VCHIQ_INSTANCE_T instance;
  27923. + VCHIQ_SERVICE_HANDLE_T ka_handle;
  27924. +
  27925. + VCHIQ_SERVICE_PARAMS_T params = {
  27926. + .fourcc = VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
  27927. + .callback = vchiq_keepalive_vchiq_callback,
  27928. + .version = KEEPALIVE_VER,
  27929. + .version_min = KEEPALIVE_VER_MIN
  27930. + };
  27931. +
  27932. + status = vchiq_initialise(&instance);
  27933. + if (status != VCHIQ_SUCCESS) {
  27934. + vchiq_log_error(vchiq_susp_log_level,
  27935. + "%s vchiq_initialise failed %d", __func__, status);
  27936. + goto exit;
  27937. + }
  27938. +
  27939. + status = vchiq_connect(instance);
  27940. + if (status != VCHIQ_SUCCESS) {
  27941. + vchiq_log_error(vchiq_susp_log_level,
  27942. + "%s vchiq_connect failed %d", __func__, status);
  27943. + goto shutdown;
  27944. + }
  27945. +
  27946. + status = vchiq_add_service(instance, &params, &ka_handle);
  27947. + if (status != VCHIQ_SUCCESS) {
  27948. + vchiq_log_error(vchiq_susp_log_level,
  27949. + "%s vchiq_open_service failed %d", __func__, status);
  27950. + goto shutdown;
  27951. + }
  27952. +
  27953. + while (1) {
  27954. + long rc = 0, uc = 0;
  27955. + if (wait_for_completion_interruptible(&arm_state->ka_evt)
  27956. + != 0) {
  27957. + vchiq_log_error(vchiq_susp_log_level,
  27958. + "%s interrupted", __func__);
  27959. + flush_signals(current);
  27960. + continue;
  27961. + }
  27962. +
  27963. + /* read and clear counters. Do release_count then use_count to
  27964. + * prevent getting more releases than uses */
  27965. + rc = atomic_xchg(&arm_state->ka_release_count, 0);
  27966. + uc = atomic_xchg(&arm_state->ka_use_count, 0);
  27967. +
  27968. + /* Call use/release service the requisite number of times.
  27969. + * Process use before release so use counts don't go negative */
  27970. + while (uc--) {
  27971. + atomic_inc(&arm_state->ka_use_ack_count);
  27972. + status = vchiq_use_service(ka_handle);
  27973. + if (status != VCHIQ_SUCCESS) {
  27974. + vchiq_log_error(vchiq_susp_log_level,
  27975. + "%s vchiq_use_service error %d",
  27976. + __func__, status);
  27977. + }
  27978. + }
  27979. + while (rc--) {
  27980. + status = vchiq_release_service(ka_handle);
  27981. + if (status != VCHIQ_SUCCESS) {
  27982. + vchiq_log_error(vchiq_susp_log_level,
  27983. + "%s vchiq_release_service error %d",
  27984. + __func__, status);
  27985. + }
  27986. + }
  27987. + }
  27988. +
  27989. +shutdown:
  27990. + vchiq_shutdown(instance);
  27991. +exit:
  27992. + return 0;
  27993. +}
  27994. +
  27995. +
  27996. +
  27997. +VCHIQ_STATUS_T
  27998. +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state)
  27999. +{
  28000. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  28001. +
  28002. + if (arm_state) {
  28003. + rwlock_init(&arm_state->susp_res_lock);
  28004. +
  28005. + init_completion(&arm_state->ka_evt);
  28006. + atomic_set(&arm_state->ka_use_count, 0);
  28007. + atomic_set(&arm_state->ka_use_ack_count, 0);
  28008. + atomic_set(&arm_state->ka_release_count, 0);
  28009. +
  28010. + init_completion(&arm_state->vc_suspend_complete);
  28011. +
  28012. + init_completion(&arm_state->vc_resume_complete);
  28013. + /* Initialise to 'done' state. We only want to block on resume
  28014. + * completion while videocore is suspended. */
  28015. + set_resume_state(arm_state, VC_RESUME_RESUMED);
  28016. +
  28017. + init_completion(&arm_state->resume_blocker);
  28018. + /* Initialise to 'done' state. We only want to block on this
  28019. + * completion while resume is blocked */
  28020. + complete_all(&arm_state->resume_blocker);
  28021. +
  28022. + init_completion(&arm_state->blocked_blocker);
  28023. + /* Initialise to 'done' state. We only want to block on this
  28024. + * completion while things are waiting on the resume blocker */
  28025. + complete_all(&arm_state->blocked_blocker);
  28026. +
  28027. + arm_state->suspend_timer_timeout = SUSPEND_TIMER_TIMEOUT_MS;
  28028. + arm_state->suspend_timer_running = 0;
  28029. + init_timer(&arm_state->suspend_timer);
  28030. + arm_state->suspend_timer.data = (unsigned long)(state);
  28031. + arm_state->suspend_timer.function = suspend_timer_callback;
  28032. +
  28033. + arm_state->first_connect = 0;
  28034. +
  28035. + }
  28036. + return status;
  28037. +}
  28038. +
  28039. +/*
  28040. +** Functions to modify the state variables;
  28041. +** set_suspend_state
  28042. +** set_resume_state
  28043. +**
  28044. +** There are more state variables than we might like, so ensure they remain in
  28045. +** step. Suspend and resume state are maintained separately, since most of
  28046. +** these state machines can operate independently. However, there are a few
  28047. +** states where state transitions in one state machine cause a reset to the
  28048. +** other state machine. In addition, there are some completion events which
  28049. +** need to occur on state machine reset and end-state(s), so these are also
  28050. +** dealt with in these functions.
  28051. +**
  28052. +** In all states we set the state variable according to the input, but in some
  28053. +** cases we perform additional steps outlined below;
  28054. +**
  28055. +** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time.
  28056. +** The suspend completion is completed after any suspend
  28057. +** attempt. When we reset the state machine we also reset
  28058. +** the completion. This reset occurs when videocore is
  28059. +** resumed, and also if we initiate suspend after a suspend
  28060. +** failure.
  28061. +**
  28062. +** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for
  28063. +** suspend - ie from this point on we must try to suspend
  28064. +** before resuming can occur. We therefore also reset the
  28065. +** resume state machine to VC_RESUME_IDLE in this state.
  28066. +**
  28067. +** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call
  28068. +** complete_all on the suspend completion to notify
  28069. +** anything waiting for suspend to happen.
  28070. +**
  28071. +** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also
  28072. +** initiate resume, so no need to alter resume state.
  28073. +** We call complete_all on the suspend completion to notify
  28074. +** of suspend rejection.
  28075. +**
  28076. +** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the
  28077. +** suspend completion and reset the resume state machine.
  28078. +**
  28079. +** VC_RESUME_IDLE - Initialise the resume completion at the same time. The
  28080. +** resume completion is in it's 'done' state whenever
  28081. +** videcore is running. Therfore, the VC_RESUME_IDLE state
  28082. +** implies that videocore is suspended.
  28083. +** Hence, any thread which needs to wait until videocore is
  28084. +** running can wait on this completion - it will only block
  28085. +** if videocore is suspended.
  28086. +**
  28087. +** VC_RESUME_RESUMED - Resume has completed successfully. Videocore is running.
  28088. +** Call complete_all on the resume completion to unblock
  28089. +** any threads waiting for resume. Also reset the suspend
  28090. +** state machine to it's idle state.
  28091. +**
  28092. +** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
  28093. +*/
  28094. +
  28095. +inline void
  28096. +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
  28097. + enum vc_suspend_status new_state)
  28098. +{
  28099. + /* set the state in all cases */
  28100. + arm_state->vc_suspend_state = new_state;
  28101. +
  28102. + /* state specific additional actions */
  28103. + switch (new_state) {
  28104. + case VC_SUSPEND_FORCE_CANCELED:
  28105. + complete_all(&arm_state->vc_suspend_complete);
  28106. + break;
  28107. + case VC_SUSPEND_REJECTED:
  28108. + complete_all(&arm_state->vc_suspend_complete);
  28109. + break;
  28110. + case VC_SUSPEND_FAILED:
  28111. + complete_all(&arm_state->vc_suspend_complete);
  28112. + arm_state->vc_resume_state = VC_RESUME_RESUMED;
  28113. + complete_all(&arm_state->vc_resume_complete);
  28114. + break;
  28115. + case VC_SUSPEND_IDLE:
  28116. + INIT_COMPLETION(arm_state->vc_suspend_complete);
  28117. + break;
  28118. + case VC_SUSPEND_REQUESTED:
  28119. + break;
  28120. + case VC_SUSPEND_IN_PROGRESS:
  28121. + set_resume_state(arm_state, VC_RESUME_IDLE);
  28122. + break;
  28123. + case VC_SUSPEND_SUSPENDED:
  28124. + complete_all(&arm_state->vc_suspend_complete);
  28125. + break;
  28126. + default:
  28127. + BUG();
  28128. + break;
  28129. + }
  28130. +}
  28131. +
  28132. +inline void
  28133. +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
  28134. + enum vc_resume_status new_state)
  28135. +{
  28136. + /* set the state in all cases */
  28137. + arm_state->vc_resume_state = new_state;
  28138. +
  28139. + /* state specific additional actions */
  28140. + switch (new_state) {
  28141. + case VC_RESUME_FAILED:
  28142. + break;
  28143. + case VC_RESUME_IDLE:
  28144. + INIT_COMPLETION(arm_state->vc_resume_complete);
  28145. + break;
  28146. + case VC_RESUME_REQUESTED:
  28147. + break;
  28148. + case VC_RESUME_IN_PROGRESS:
  28149. + break;
  28150. + case VC_RESUME_RESUMED:
  28151. + complete_all(&arm_state->vc_resume_complete);
  28152. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  28153. + break;
  28154. + default:
  28155. + BUG();
  28156. + break;
  28157. + }
  28158. +}
  28159. +
  28160. +
  28161. +/* should be called with the write lock held */
  28162. +inline void
  28163. +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
  28164. +{
  28165. + del_timer(&arm_state->suspend_timer);
  28166. + arm_state->suspend_timer.expires = jiffies +
  28167. + msecs_to_jiffies(arm_state->
  28168. + suspend_timer_timeout);
  28169. + add_timer(&arm_state->suspend_timer);
  28170. + arm_state->suspend_timer_running = 1;
  28171. +}
  28172. +
  28173. +/* should be called with the write lock held */
  28174. +static inline void
  28175. +stop_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
  28176. +{
  28177. + if (arm_state->suspend_timer_running) {
  28178. + del_timer(&arm_state->suspend_timer);
  28179. + arm_state->suspend_timer_running = 0;
  28180. + }
  28181. +}
  28182. +
  28183. +static inline int
  28184. +need_resume(VCHIQ_STATE_T *state)
  28185. +{
  28186. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28187. + return (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) &&
  28188. + (arm_state->vc_resume_state < VC_RESUME_REQUESTED) &&
  28189. + vchiq_videocore_wanted(state);
  28190. +}
  28191. +
  28192. +static int
  28193. +block_resume(VCHIQ_ARM_STATE_T *arm_state)
  28194. +{
  28195. + int status = VCHIQ_SUCCESS;
  28196. + const unsigned long timeout_val =
  28197. + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS);
  28198. + int resume_count = 0;
  28199. +
  28200. + /* Allow any threads which were blocked by the last force suspend to
  28201. + * complete if they haven't already. Only give this one shot; if
  28202. + * blocked_count is incremented after blocked_blocker is completed
  28203. + * (which only happens when blocked_count hits 0) then those threads
  28204. + * will have to wait until next time around */
  28205. + if (arm_state->blocked_count) {
  28206. + INIT_COMPLETION(arm_state->blocked_blocker);
  28207. + write_unlock_bh(&arm_state->susp_res_lock);
  28208. + vchiq_log_info(vchiq_susp_log_level, "%s wait for previously "
  28209. + "blocked clients", __func__);
  28210. + if (wait_for_completion_interruptible_timeout(
  28211. + &arm_state->blocked_blocker, timeout_val)
  28212. + <= 0) {
  28213. + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
  28214. + "previously blocked clients failed" , __func__);
  28215. + status = VCHIQ_ERROR;
  28216. + write_lock_bh(&arm_state->susp_res_lock);
  28217. + goto out;
  28218. + }
  28219. + vchiq_log_info(vchiq_susp_log_level, "%s previously blocked "
  28220. + "clients resumed", __func__);
  28221. + write_lock_bh(&arm_state->susp_res_lock);
  28222. + }
  28223. +
  28224. + /* We need to wait for resume to complete if it's in process */
  28225. + while (arm_state->vc_resume_state != VC_RESUME_RESUMED &&
  28226. + arm_state->vc_resume_state > VC_RESUME_IDLE) {
  28227. + if (resume_count > 1) {
  28228. + status = VCHIQ_ERROR;
  28229. + vchiq_log_error(vchiq_susp_log_level, "%s waited too "
  28230. + "many times for resume" , __func__);
  28231. + goto out;
  28232. + }
  28233. + write_unlock_bh(&arm_state->susp_res_lock);
  28234. + vchiq_log_info(vchiq_susp_log_level, "%s wait for resume",
  28235. + __func__);
  28236. + if (wait_for_completion_interruptible_timeout(
  28237. + &arm_state->vc_resume_complete, timeout_val)
  28238. + <= 0) {
  28239. + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
  28240. + "resume failed (%s)", __func__,
  28241. + resume_state_names[arm_state->vc_resume_state +
  28242. + VC_RESUME_NUM_OFFSET]);
  28243. + status = VCHIQ_ERROR;
  28244. + write_lock_bh(&arm_state->susp_res_lock);
  28245. + goto out;
  28246. + }
  28247. + vchiq_log_info(vchiq_susp_log_level, "%s resumed", __func__);
  28248. + write_lock_bh(&arm_state->susp_res_lock);
  28249. + resume_count++;
  28250. + }
  28251. + INIT_COMPLETION(arm_state->resume_blocker);
  28252. + arm_state->resume_blocked = 1;
  28253. +
  28254. +out:
  28255. + return status;
  28256. +}
  28257. +
  28258. +static inline void
  28259. +unblock_resume(VCHIQ_ARM_STATE_T *arm_state)
  28260. +{
  28261. + complete_all(&arm_state->resume_blocker);
  28262. + arm_state->resume_blocked = 0;
  28263. +}
  28264. +
  28265. +/* Initiate suspend via slot handler. Should be called with the write lock
  28266. + * held */
  28267. +VCHIQ_STATUS_T
  28268. +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state)
  28269. +{
  28270. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  28271. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28272. +
  28273. + if (!arm_state)
  28274. + goto out;
  28275. +
  28276. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  28277. + status = VCHIQ_SUCCESS;
  28278. +
  28279. +
  28280. + switch (arm_state->vc_suspend_state) {
  28281. + case VC_SUSPEND_REQUESTED:
  28282. + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already "
  28283. + "requested", __func__);
  28284. + break;
  28285. + case VC_SUSPEND_IN_PROGRESS:
  28286. + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already in "
  28287. + "progress", __func__);
  28288. + break;
  28289. +
  28290. + default:
  28291. + /* We don't expect to be in other states, so log but continue
  28292. + * anyway */
  28293. + vchiq_log_error(vchiq_susp_log_level,
  28294. + "%s unexpected suspend state %s", __func__,
  28295. + suspend_state_names[arm_state->vc_suspend_state +
  28296. + VC_SUSPEND_NUM_OFFSET]);
  28297. + /* fall through */
  28298. + case VC_SUSPEND_REJECTED:
  28299. + case VC_SUSPEND_FAILED:
  28300. + /* Ensure any idle state actions have been run */
  28301. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  28302. + /* fall through */
  28303. + case VC_SUSPEND_IDLE:
  28304. + vchiq_log_info(vchiq_susp_log_level,
  28305. + "%s: suspending", __func__);
  28306. + set_suspend_state(arm_state, VC_SUSPEND_REQUESTED);
  28307. + /* kick the slot handler thread to initiate suspend */
  28308. + request_poll(state, NULL, 0);
  28309. + break;
  28310. + }
  28311. +
  28312. +out:
  28313. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
  28314. + return status;
  28315. +}
  28316. +
  28317. +void
  28318. +vchiq_platform_check_suspend(VCHIQ_STATE_T *state)
  28319. +{
  28320. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28321. + int susp = 0;
  28322. +
  28323. + if (!arm_state)
  28324. + goto out;
  28325. +
  28326. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  28327. +
  28328. + write_lock_bh(&arm_state->susp_res_lock);
  28329. + if (arm_state->vc_suspend_state == VC_SUSPEND_REQUESTED &&
  28330. + arm_state->vc_resume_state == VC_RESUME_RESUMED) {
  28331. + set_suspend_state(arm_state, VC_SUSPEND_IN_PROGRESS);
  28332. + susp = 1;
  28333. + }
  28334. + write_unlock_bh(&arm_state->susp_res_lock);
  28335. +
  28336. + if (susp)
  28337. + vchiq_platform_suspend(state);
  28338. +
  28339. +out:
  28340. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  28341. + return;
  28342. +}
  28343. +
  28344. +
  28345. +static void
  28346. +output_timeout_error(VCHIQ_STATE_T *state)
  28347. +{
  28348. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28349. + char service_err[50] = "";
  28350. + int vc_use_count = arm_state->videocore_use_count;
  28351. + int active_services = state->unused_service;
  28352. + int i;
  28353. +
  28354. + if (!arm_state->videocore_use_count) {
  28355. + snprintf(service_err, 50, " Videocore usecount is 0");
  28356. + goto output_msg;
  28357. + }
  28358. + for (i = 0; i < active_services; i++) {
  28359. + VCHIQ_SERVICE_T *service_ptr = state->services[i];
  28360. + if (service_ptr && service_ptr->service_use_count &&
  28361. + (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE)) {
  28362. + snprintf(service_err, 50, " %c%c%c%c(%d) service has "
  28363. + "use count %d%s", VCHIQ_FOURCC_AS_4CHARS(
  28364. + service_ptr->base.fourcc),
  28365. + service_ptr->client_id,
  28366. + service_ptr->service_use_count,
  28367. + service_ptr->service_use_count ==
  28368. + vc_use_count ? "" : " (+ more)");
  28369. + break;
  28370. + }
  28371. + }
  28372. +
  28373. +output_msg:
  28374. + vchiq_log_error(vchiq_susp_log_level,
  28375. + "timed out waiting for vc suspend (%d).%s",
  28376. + arm_state->autosuspend_override, service_err);
  28377. +
  28378. +}
  28379. +
  28380. +/* Try to get videocore into suspended state, regardless of autosuspend state.
  28381. +** We don't actually force suspend, since videocore may get into a bad state
  28382. +** if we force suspend at a bad time. Instead, we wait for autosuspend to
  28383. +** determine a good point to suspend. If this doesn't happen within 100ms we
  28384. +** report failure.
  28385. +**
  28386. +** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
  28387. +** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
  28388. +*/
  28389. +VCHIQ_STATUS_T
  28390. +vchiq_arm_force_suspend(VCHIQ_STATE_T *state)
  28391. +{
  28392. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28393. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  28394. + long rc = 0;
  28395. + int repeat = -1;
  28396. +
  28397. + if (!arm_state)
  28398. + goto out;
  28399. +
  28400. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  28401. +
  28402. + write_lock_bh(&arm_state->susp_res_lock);
  28403. +
  28404. + status = block_resume(arm_state);
  28405. + if (status != VCHIQ_SUCCESS)
  28406. + goto unlock;
  28407. + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
  28408. + /* Already suspended - just block resume and exit */
  28409. + vchiq_log_info(vchiq_susp_log_level, "%s already suspended",
  28410. + __func__);
  28411. + status = VCHIQ_SUCCESS;
  28412. + goto unlock;
  28413. + } else if (arm_state->vc_suspend_state <= VC_SUSPEND_IDLE) {
  28414. + /* initiate suspend immediately in the case that we're waiting
  28415. + * for the timeout */
  28416. + stop_suspend_timer(arm_state);
  28417. + if (!vchiq_videocore_wanted(state)) {
  28418. + vchiq_log_info(vchiq_susp_log_level, "%s videocore "
  28419. + "idle, initiating suspend", __func__);
  28420. + status = vchiq_arm_vcsuspend(state);
  28421. + } else if (arm_state->autosuspend_override <
  28422. + FORCE_SUSPEND_FAIL_MAX) {
  28423. + vchiq_log_info(vchiq_susp_log_level, "%s letting "
  28424. + "videocore go idle", __func__);
  28425. + status = VCHIQ_SUCCESS;
  28426. + } else {
  28427. + vchiq_log_warning(vchiq_susp_log_level, "%s failed too "
  28428. + "many times - attempting suspend", __func__);
  28429. + status = vchiq_arm_vcsuspend(state);
  28430. + }
  28431. + } else {
  28432. + vchiq_log_info(vchiq_susp_log_level, "%s videocore suspend "
  28433. + "in progress - wait for completion", __func__);
  28434. + status = VCHIQ_SUCCESS;
  28435. + }
  28436. +
  28437. + /* Wait for suspend to happen due to system idle (not forced..) */
  28438. + if (status != VCHIQ_SUCCESS)
  28439. + goto unblock_resume;
  28440. +
  28441. + do {
  28442. + write_unlock_bh(&arm_state->susp_res_lock);
  28443. +
  28444. + rc = wait_for_completion_interruptible_timeout(
  28445. + &arm_state->vc_suspend_complete,
  28446. + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS));
  28447. +
  28448. + write_lock_bh(&arm_state->susp_res_lock);
  28449. + if (rc < 0) {
  28450. + vchiq_log_warning(vchiq_susp_log_level, "%s "
  28451. + "interrupted waiting for suspend", __func__);
  28452. + status = VCHIQ_ERROR;
  28453. + goto unblock_resume;
  28454. + } else if (rc == 0) {
  28455. + if (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) {
  28456. + /* Repeat timeout once if in progress */
  28457. + if (repeat < 0) {
  28458. + repeat = 1;
  28459. + continue;
  28460. + }
  28461. + }
  28462. + arm_state->autosuspend_override++;
  28463. + output_timeout_error(state);
  28464. +
  28465. + status = VCHIQ_RETRY;
  28466. + goto unblock_resume;
  28467. + }
  28468. + } while (0 < (repeat--));
  28469. +
  28470. + /* Check and report state in case we need to abort ARM suspend */
  28471. + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED) {
  28472. + status = VCHIQ_RETRY;
  28473. + vchiq_log_error(vchiq_susp_log_level,
  28474. + "%s videocore suspend failed (state %s)", __func__,
  28475. + suspend_state_names[arm_state->vc_suspend_state +
  28476. + VC_SUSPEND_NUM_OFFSET]);
  28477. + /* Reset the state only if it's still in an error state.
  28478. + * Something could have already initiated another suspend. */
  28479. + if (arm_state->vc_suspend_state < VC_SUSPEND_IDLE)
  28480. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  28481. +
  28482. + goto unblock_resume;
  28483. + }
  28484. +
  28485. + /* successfully suspended - unlock and exit */
  28486. + goto unlock;
  28487. +
  28488. +unblock_resume:
  28489. + /* all error states need to unblock resume before exit */
  28490. + unblock_resume(arm_state);
  28491. +
  28492. +unlock:
  28493. + write_unlock_bh(&arm_state->susp_res_lock);
  28494. +
  28495. +out:
  28496. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
  28497. + return status;
  28498. +}
  28499. +
  28500. +void
  28501. +vchiq_check_suspend(VCHIQ_STATE_T *state)
  28502. +{
  28503. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28504. +
  28505. + if (!arm_state)
  28506. + goto out;
  28507. +
  28508. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  28509. +
  28510. + write_lock_bh(&arm_state->susp_res_lock);
  28511. + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED &&
  28512. + arm_state->first_connect &&
  28513. + !vchiq_videocore_wanted(state)) {
  28514. + vchiq_arm_vcsuspend(state);
  28515. + }
  28516. + write_unlock_bh(&arm_state->susp_res_lock);
  28517. +
  28518. +out:
  28519. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  28520. + return;
  28521. +}
  28522. +
  28523. +
  28524. +int
  28525. +vchiq_arm_allow_resume(VCHIQ_STATE_T *state)
  28526. +{
  28527. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28528. + int resume = 0;
  28529. + int ret = -1;
  28530. +
  28531. + if (!arm_state)
  28532. + goto out;
  28533. +
  28534. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  28535. +
  28536. + write_lock_bh(&arm_state->susp_res_lock);
  28537. + unblock_resume(arm_state);
  28538. + resume = vchiq_check_resume(state);
  28539. + write_unlock_bh(&arm_state->susp_res_lock);
  28540. +
  28541. + if (resume) {
  28542. + if (wait_for_completion_interruptible(
  28543. + &arm_state->vc_resume_complete) < 0) {
  28544. + vchiq_log_error(vchiq_susp_log_level,
  28545. + "%s interrupted", __func__);
  28546. + /* failed, cannot accurately derive suspend
  28547. + * state, so exit early. */
  28548. + goto out;
  28549. + }
  28550. + }
  28551. +
  28552. + read_lock_bh(&arm_state->susp_res_lock);
  28553. + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
  28554. + vchiq_log_info(vchiq_susp_log_level,
  28555. + "%s: Videocore remains suspended", __func__);
  28556. + } else {
  28557. + vchiq_log_info(vchiq_susp_log_level,
  28558. + "%s: Videocore resumed", __func__);
  28559. + ret = 0;
  28560. + }
  28561. + read_unlock_bh(&arm_state->susp_res_lock);
  28562. +out:
  28563. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  28564. + return ret;
  28565. +}
  28566. +
  28567. +/* This function should be called with the write lock held */
  28568. +int
  28569. +vchiq_check_resume(VCHIQ_STATE_T *state)
  28570. +{
  28571. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28572. + int resume = 0;
  28573. +
  28574. + if (!arm_state)
  28575. + goto out;
  28576. +
  28577. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  28578. +
  28579. + if (need_resume(state)) {
  28580. + set_resume_state(arm_state, VC_RESUME_REQUESTED);
  28581. + request_poll(state, NULL, 0);
  28582. + resume = 1;
  28583. + }
  28584. +
  28585. +out:
  28586. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  28587. + return resume;
  28588. +}
  28589. +
  28590. +void
  28591. +vchiq_platform_check_resume(VCHIQ_STATE_T *state)
  28592. +{
  28593. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28594. + int res = 0;
  28595. +
  28596. + if (!arm_state)
  28597. + goto out;
  28598. +
  28599. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  28600. +
  28601. + write_lock_bh(&arm_state->susp_res_lock);
  28602. + if (arm_state->wake_address == 0) {
  28603. + vchiq_log_info(vchiq_susp_log_level,
  28604. + "%s: already awake", __func__);
  28605. + goto unlock;
  28606. + }
  28607. + if (arm_state->vc_resume_state == VC_RESUME_IN_PROGRESS) {
  28608. + vchiq_log_info(vchiq_susp_log_level,
  28609. + "%s: already resuming", __func__);
  28610. + goto unlock;
  28611. + }
  28612. +
  28613. + if (arm_state->vc_resume_state == VC_RESUME_REQUESTED) {
  28614. + set_resume_state(arm_state, VC_RESUME_IN_PROGRESS);
  28615. + res = 1;
  28616. + } else
  28617. + vchiq_log_trace(vchiq_susp_log_level,
  28618. + "%s: not resuming (resume state %s)", __func__,
  28619. + resume_state_names[arm_state->vc_resume_state +
  28620. + VC_RESUME_NUM_OFFSET]);
  28621. +
  28622. +unlock:
  28623. + write_unlock_bh(&arm_state->susp_res_lock);
  28624. +
  28625. + if (res)
  28626. + vchiq_platform_resume(state);
  28627. +
  28628. +out:
  28629. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  28630. + return;
  28631. +
  28632. +}
  28633. +
  28634. +
  28635. +
  28636. +VCHIQ_STATUS_T
  28637. +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  28638. + enum USE_TYPE_E use_type)
  28639. +{
  28640. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28641. + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
  28642. + char entity[16];
  28643. + int *entity_uc;
  28644. + int local_uc, local_entity_uc;
  28645. +
  28646. + if (!arm_state)
  28647. + goto out;
  28648. +
  28649. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  28650. +
  28651. + if (use_type == USE_TYPE_VCHIQ) {
  28652. + sprintf(entity, "VCHIQ: ");
  28653. + entity_uc = &arm_state->peer_use_count;
  28654. + } else if (service) {
  28655. + sprintf(entity, "%c%c%c%c:%03d",
  28656. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  28657. + service->client_id);
  28658. + entity_uc = &service->service_use_count;
  28659. + } else {
  28660. + vchiq_log_error(vchiq_susp_log_level, "%s null service "
  28661. + "ptr", __func__);
  28662. + ret = VCHIQ_ERROR;
  28663. + goto out;
  28664. + }
  28665. +
  28666. + write_lock_bh(&arm_state->susp_res_lock);
  28667. + while (arm_state->resume_blocked) {
  28668. + /* If we call 'use' while force suspend is waiting for suspend,
  28669. + * then we're about to block the thread which the force is
  28670. + * waiting to complete, so we're bound to just time out. In this
  28671. + * case, set the suspend state such that the wait will be
  28672. + * canceled, so we can complete as quickly as possible. */
  28673. + if (arm_state->resume_blocked && arm_state->vc_suspend_state ==
  28674. + VC_SUSPEND_IDLE) {
  28675. + set_suspend_state(arm_state, VC_SUSPEND_FORCE_CANCELED);
  28676. + break;
  28677. + }
  28678. + /* If suspend is already in progress then we need to block */
  28679. + if (!try_wait_for_completion(&arm_state->resume_blocker)) {
  28680. + /* Indicate that there are threads waiting on the resume
  28681. + * blocker. These need to be allowed to complete before
  28682. + * a _second_ call to force suspend can complete,
  28683. + * otherwise low priority threads might never actually
  28684. + * continue */
  28685. + arm_state->blocked_count++;
  28686. + write_unlock_bh(&arm_state->susp_res_lock);
  28687. + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
  28688. + "blocked - waiting...", __func__, entity);
  28689. + if (wait_for_completion_killable(
  28690. + &arm_state->resume_blocker) != 0) {
  28691. + vchiq_log_error(vchiq_susp_log_level, "%s %s "
  28692. + "wait for resume blocker interrupted",
  28693. + __func__, entity);
  28694. + ret = VCHIQ_ERROR;
  28695. + write_lock_bh(&arm_state->susp_res_lock);
  28696. + arm_state->blocked_count--;
  28697. + write_unlock_bh(&arm_state->susp_res_lock);
  28698. + goto out;
  28699. + }
  28700. + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
  28701. + "unblocked", __func__, entity);
  28702. + write_lock_bh(&arm_state->susp_res_lock);
  28703. + if (--arm_state->blocked_count == 0)
  28704. + complete_all(&arm_state->blocked_blocker);
  28705. + }
  28706. + }
  28707. +
  28708. + stop_suspend_timer(arm_state);
  28709. +
  28710. + local_uc = ++arm_state->videocore_use_count;
  28711. + local_entity_uc = ++(*entity_uc);
  28712. +
  28713. + /* If there's a pending request which hasn't yet been serviced then
  28714. + * just clear it. If we're past VC_SUSPEND_REQUESTED state then
  28715. + * vc_resume_complete will block until we either resume or fail to
  28716. + * suspend */
  28717. + if (arm_state->vc_suspend_state <= VC_SUSPEND_REQUESTED)
  28718. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  28719. +
  28720. + if ((use_type != USE_TYPE_SERVICE_NO_RESUME) && need_resume(state)) {
  28721. + set_resume_state(arm_state, VC_RESUME_REQUESTED);
  28722. + vchiq_log_info(vchiq_susp_log_level,
  28723. + "%s %s count %d, state count %d",
  28724. + __func__, entity, local_entity_uc, local_uc);
  28725. + request_poll(state, NULL, 0);
  28726. + } else
  28727. + vchiq_log_trace(vchiq_susp_log_level,
  28728. + "%s %s count %d, state count %d",
  28729. + __func__, entity, *entity_uc, local_uc);
  28730. +
  28731. +
  28732. + write_unlock_bh(&arm_state->susp_res_lock);
  28733. +
  28734. + /* Completion is in a done state when we're not suspended, so this won't
  28735. + * block for the non-suspended case. */
  28736. + if (!try_wait_for_completion(&arm_state->vc_resume_complete)) {
  28737. + vchiq_log_info(vchiq_susp_log_level, "%s %s wait for resume",
  28738. + __func__, entity);
  28739. + if (wait_for_completion_killable(
  28740. + &arm_state->vc_resume_complete) != 0) {
  28741. + vchiq_log_error(vchiq_susp_log_level, "%s %s wait for "
  28742. + "resume interrupted", __func__, entity);
  28743. + ret = VCHIQ_ERROR;
  28744. + goto out;
  28745. + }
  28746. + vchiq_log_info(vchiq_susp_log_level, "%s %s resumed", __func__,
  28747. + entity);
  28748. + }
  28749. +
  28750. + if (ret == VCHIQ_SUCCESS) {
  28751. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  28752. + long ack_cnt = atomic_xchg(&arm_state->ka_use_ack_count, 0);
  28753. + while (ack_cnt && (status == VCHIQ_SUCCESS)) {
  28754. + /* Send the use notify to videocore */
  28755. + status = vchiq_send_remote_use_active(state);
  28756. + if (status == VCHIQ_SUCCESS)
  28757. + ack_cnt--;
  28758. + else
  28759. + atomic_add(ack_cnt,
  28760. + &arm_state->ka_use_ack_count);
  28761. + }
  28762. + }
  28763. +
  28764. +out:
  28765. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  28766. + return ret;
  28767. +}
  28768. +
  28769. +VCHIQ_STATUS_T
  28770. +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service)
  28771. +{
  28772. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28773. + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
  28774. + char entity[16];
  28775. + int *entity_uc;
  28776. + int local_uc, local_entity_uc;
  28777. +
  28778. + if (!arm_state)
  28779. + goto out;
  28780. +
  28781. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  28782. +
  28783. + if (service) {
  28784. + sprintf(entity, "%c%c%c%c:%03d",
  28785. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  28786. + service->client_id);
  28787. + entity_uc = &service->service_use_count;
  28788. + } else {
  28789. + sprintf(entity, "PEER: ");
  28790. + entity_uc = &arm_state->peer_use_count;
  28791. + }
  28792. +
  28793. + write_lock_bh(&arm_state->susp_res_lock);
  28794. + if (!arm_state->videocore_use_count || !(*entity_uc)) {
  28795. + /* Don't use BUG_ON - don't allow user thread to crash kernel */
  28796. + WARN_ON(!arm_state->videocore_use_count);
  28797. + WARN_ON(!(*entity_uc));
  28798. + ret = VCHIQ_ERROR;
  28799. + goto unlock;
  28800. + }
  28801. + local_uc = --arm_state->videocore_use_count;
  28802. + local_entity_uc = --(*entity_uc);
  28803. +
  28804. + if (!vchiq_videocore_wanted(state)) {
  28805. + if (vchiq_platform_use_suspend_timer() &&
  28806. + !arm_state->resume_blocked) {
  28807. + /* Only use the timer if we're not trying to force
  28808. + * suspend (=> resume_blocked) */
  28809. + start_suspend_timer(arm_state);
  28810. + } else {
  28811. + vchiq_log_info(vchiq_susp_log_level,
  28812. + "%s %s count %d, state count %d - suspending",
  28813. + __func__, entity, *entity_uc,
  28814. + arm_state->videocore_use_count);
  28815. + vchiq_arm_vcsuspend(state);
  28816. + }
  28817. + } else
  28818. + vchiq_log_trace(vchiq_susp_log_level,
  28819. + "%s %s count %d, state count %d",
  28820. + __func__, entity, *entity_uc,
  28821. + arm_state->videocore_use_count);
  28822. +
  28823. +unlock:
  28824. + write_unlock_bh(&arm_state->susp_res_lock);
  28825. +
  28826. +out:
  28827. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  28828. + return ret;
  28829. +}
  28830. +
  28831. +void
  28832. +vchiq_on_remote_use(VCHIQ_STATE_T *state)
  28833. +{
  28834. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28835. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  28836. + atomic_inc(&arm_state->ka_use_count);
  28837. + complete(&arm_state->ka_evt);
  28838. +}
  28839. +
  28840. +void
  28841. +vchiq_on_remote_release(VCHIQ_STATE_T *state)
  28842. +{
  28843. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28844. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  28845. + atomic_inc(&arm_state->ka_release_count);
  28846. + complete(&arm_state->ka_evt);
  28847. +}
  28848. +
  28849. +VCHIQ_STATUS_T
  28850. +vchiq_use_service_internal(VCHIQ_SERVICE_T *service)
  28851. +{
  28852. + return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
  28853. +}
  28854. +
  28855. +VCHIQ_STATUS_T
  28856. +vchiq_release_service_internal(VCHIQ_SERVICE_T *service)
  28857. +{
  28858. + return vchiq_release_internal(service->state, service);
  28859. +}
  28860. +
  28861. +VCHIQ_DEBUGFS_NODE_T *
  28862. +vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance)
  28863. +{
  28864. + return &instance->debugfs_node;
  28865. +}
  28866. +
  28867. +int
  28868. +vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance)
  28869. +{
  28870. + VCHIQ_SERVICE_T *service;
  28871. + int use_count = 0, i;
  28872. + i = 0;
  28873. + while ((service = next_service_by_instance(instance->state,
  28874. + instance, &i)) != NULL) {
  28875. + use_count += service->service_use_count;
  28876. + unlock_service(service);
  28877. + }
  28878. + return use_count;
  28879. +}
  28880. +
  28881. +int
  28882. +vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance)
  28883. +{
  28884. + return instance->pid;
  28885. +}
  28886. +
  28887. +int
  28888. +vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance)
  28889. +{
  28890. + return instance->trace;
  28891. +}
  28892. +
  28893. +void
  28894. +vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace)
  28895. +{
  28896. + VCHIQ_SERVICE_T *service;
  28897. + int i;
  28898. + i = 0;
  28899. + while ((service = next_service_by_instance(instance->state,
  28900. + instance, &i)) != NULL) {
  28901. + service->trace = trace;
  28902. + unlock_service(service);
  28903. + }
  28904. + instance->trace = (trace != 0);
  28905. +}
  28906. +
  28907. +static void suspend_timer_callback(unsigned long context)
  28908. +{
  28909. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *)context;
  28910. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28911. + if (!arm_state)
  28912. + goto out;
  28913. + vchiq_log_info(vchiq_susp_log_level,
  28914. + "%s - suspend timer expired - check suspend", __func__);
  28915. + vchiq_check_suspend(state);
  28916. +out:
  28917. + return;
  28918. +}
  28919. +
  28920. +VCHIQ_STATUS_T
  28921. +vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle)
  28922. +{
  28923. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  28924. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  28925. + if (service) {
  28926. + ret = vchiq_use_internal(service->state, service,
  28927. + USE_TYPE_SERVICE_NO_RESUME);
  28928. + unlock_service(service);
  28929. + }
  28930. + return ret;
  28931. +}
  28932. +
  28933. +VCHIQ_STATUS_T
  28934. +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle)
  28935. +{
  28936. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  28937. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  28938. + if (service) {
  28939. + ret = vchiq_use_internal(service->state, service,
  28940. + USE_TYPE_SERVICE);
  28941. + unlock_service(service);
  28942. + }
  28943. + return ret;
  28944. +}
  28945. +
  28946. +VCHIQ_STATUS_T
  28947. +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle)
  28948. +{
  28949. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  28950. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  28951. + if (service) {
  28952. + ret = vchiq_release_internal(service->state, service);
  28953. + unlock_service(service);
  28954. + }
  28955. + return ret;
  28956. +}
  28957. +
  28958. +void
  28959. +vchiq_dump_service_use_state(VCHIQ_STATE_T *state)
  28960. +{
  28961. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  28962. + int i, j = 0;
  28963. + /* Only dump 64 services */
  28964. + static const int local_max_services = 64;
  28965. + /* If there's more than 64 services, only dump ones with
  28966. + * non-zero counts */
  28967. + int only_nonzero = 0;
  28968. + static const char *nz = "<-- preventing suspend";
  28969. +
  28970. + enum vc_suspend_status vc_suspend_state;
  28971. + enum vc_resume_status vc_resume_state;
  28972. + int peer_count;
  28973. + int vc_use_count;
  28974. + int active_services;
  28975. + struct service_data_struct {
  28976. + int fourcc;
  28977. + int clientid;
  28978. + int use_count;
  28979. + } service_data[local_max_services];
  28980. +
  28981. + if (!arm_state)
  28982. + return;
  28983. +
  28984. + read_lock_bh(&arm_state->susp_res_lock);
  28985. + vc_suspend_state = arm_state->vc_suspend_state;
  28986. + vc_resume_state = arm_state->vc_resume_state;
  28987. + peer_count = arm_state->peer_use_count;
  28988. + vc_use_count = arm_state->videocore_use_count;
  28989. + active_services = state->unused_service;
  28990. + if (active_services > local_max_services)
  28991. + only_nonzero = 1;
  28992. +
  28993. + for (i = 0; (i < active_services) && (j < local_max_services); i++) {
  28994. + VCHIQ_SERVICE_T *service_ptr = state->services[i];
  28995. + if (!service_ptr)
  28996. + continue;
  28997. +
  28998. + if (only_nonzero && !service_ptr->service_use_count)
  28999. + continue;
  29000. +
  29001. + if (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE) {
  29002. + service_data[j].fourcc = service_ptr->base.fourcc;
  29003. + service_data[j].clientid = service_ptr->client_id;
  29004. + service_data[j++].use_count = service_ptr->
  29005. + service_use_count;
  29006. + }
  29007. + }
  29008. +
  29009. + read_unlock_bh(&arm_state->susp_res_lock);
  29010. +
  29011. + vchiq_log_warning(vchiq_susp_log_level,
  29012. + "-- Videcore suspend state: %s --",
  29013. + suspend_state_names[vc_suspend_state + VC_SUSPEND_NUM_OFFSET]);
  29014. + vchiq_log_warning(vchiq_susp_log_level,
  29015. + "-- Videcore resume state: %s --",
  29016. + resume_state_names[vc_resume_state + VC_RESUME_NUM_OFFSET]);
  29017. +
  29018. + if (only_nonzero)
  29019. + vchiq_log_warning(vchiq_susp_log_level, "Too many active "
  29020. + "services (%d). Only dumping up to first %d services "
  29021. + "with non-zero use-count", active_services,
  29022. + local_max_services);
  29023. +
  29024. + for (i = 0; i < j; i++) {
  29025. + vchiq_log_warning(vchiq_susp_log_level,
  29026. + "----- %c%c%c%c:%d service count %d %s",
  29027. + VCHIQ_FOURCC_AS_4CHARS(service_data[i].fourcc),
  29028. + service_data[i].clientid,
  29029. + service_data[i].use_count,
  29030. + service_data[i].use_count ? nz : "");
  29031. + }
  29032. + vchiq_log_warning(vchiq_susp_log_level,
  29033. + "----- VCHIQ use count count %d", peer_count);
  29034. + vchiq_log_warning(vchiq_susp_log_level,
  29035. + "--- Overall vchiq instance use count %d", vc_use_count);
  29036. +
  29037. + vchiq_dump_platform_use_state(state);
  29038. +}
  29039. +
  29040. +VCHIQ_STATUS_T
  29041. +vchiq_check_service(VCHIQ_SERVICE_T *service)
  29042. +{
  29043. + VCHIQ_ARM_STATE_T *arm_state;
  29044. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  29045. +
  29046. + if (!service || !service->state)
  29047. + goto out;
  29048. +
  29049. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  29050. +
  29051. + arm_state = vchiq_platform_get_arm_state(service->state);
  29052. +
  29053. + read_lock_bh(&arm_state->susp_res_lock);
  29054. + if (service->service_use_count)
  29055. + ret = VCHIQ_SUCCESS;
  29056. + read_unlock_bh(&arm_state->susp_res_lock);
  29057. +
  29058. + if (ret == VCHIQ_ERROR) {
  29059. + vchiq_log_error(vchiq_susp_log_level,
  29060. + "%s ERROR - %c%c%c%c:%d service count %d, "
  29061. + "state count %d, videocore suspend state %s", __func__,
  29062. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  29063. + service->client_id, service->service_use_count,
  29064. + arm_state->videocore_use_count,
  29065. + suspend_state_names[arm_state->vc_suspend_state +
  29066. + VC_SUSPEND_NUM_OFFSET]);
  29067. + vchiq_dump_service_use_state(service->state);
  29068. + }
  29069. +out:
  29070. + return ret;
  29071. +}
  29072. +
  29073. +/* stub functions */
  29074. +void vchiq_on_remote_use_active(VCHIQ_STATE_T *state)
  29075. +{
  29076. + (void)state;
  29077. +}
  29078. +
  29079. +void vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
  29080. + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate)
  29081. +{
  29082. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  29083. + vchiq_log_info(vchiq_susp_log_level, "%d: %s->%s", state->id,
  29084. + get_conn_state_name(oldstate), get_conn_state_name(newstate));
  29085. + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTED) {
  29086. + write_lock_bh(&arm_state->susp_res_lock);
  29087. + if (!arm_state->first_connect) {
  29088. + char threadname[10];
  29089. + arm_state->first_connect = 1;
  29090. + write_unlock_bh(&arm_state->susp_res_lock);
  29091. + snprintf(threadname, sizeof(threadname), "VCHIQka-%d",
  29092. + state->id);
  29093. + arm_state->ka_thread = kthread_create(
  29094. + &vchiq_keepalive_thread_func,
  29095. + (void *)state,
  29096. + threadname);
  29097. + if (arm_state->ka_thread == NULL) {
  29098. + vchiq_log_error(vchiq_susp_log_level,
  29099. + "vchiq: FATAL: couldn't create thread %s",
  29100. + threadname);
  29101. + } else {
  29102. + wake_up_process(arm_state->ka_thread);
  29103. + }
  29104. + } else
  29105. + write_unlock_bh(&arm_state->susp_res_lock);
  29106. + }
  29107. +}
  29108. +
  29109. +
  29110. +/****************************************************************************
  29111. +*
  29112. +* vchiq_init - called when the module is loaded.
  29113. +*
  29114. +***************************************************************************/
  29115. +
  29116. +static int __init
  29117. +vchiq_init(void)
  29118. +{
  29119. + int err;
  29120. + void *ptr_err;
  29121. +
  29122. + /* create debugfs entries */
  29123. + err = vchiq_debugfs_init();
  29124. + if (err != 0)
  29125. + goto failed_debugfs_init;
  29126. +
  29127. + err = alloc_chrdev_region(&vchiq_devid, VCHIQ_MINOR, 1, DEVICE_NAME);
  29128. + if (err != 0) {
  29129. + vchiq_log_error(vchiq_arm_log_level,
  29130. + "Unable to allocate device number");
  29131. + goto failed_alloc_chrdev;
  29132. + }
  29133. + cdev_init(&vchiq_cdev, &vchiq_fops);
  29134. + vchiq_cdev.owner = THIS_MODULE;
  29135. + err = cdev_add(&vchiq_cdev, vchiq_devid, 1);
  29136. + if (err != 0) {
  29137. + vchiq_log_error(vchiq_arm_log_level,
  29138. + "Unable to register device");
  29139. + goto failed_cdev_add;
  29140. + }
  29141. +
  29142. + /* create sysfs entries */
  29143. + vchiq_class = class_create(THIS_MODULE, DEVICE_NAME);
  29144. + ptr_err = vchiq_class;
  29145. + if (IS_ERR(ptr_err))
  29146. + goto failed_class_create;
  29147. +
  29148. + vchiq_dev = device_create(vchiq_class, NULL,
  29149. + vchiq_devid, NULL, "vchiq");
  29150. + ptr_err = vchiq_dev;
  29151. + if (IS_ERR(ptr_err))
  29152. + goto failed_device_create;
  29153. +
  29154. + err = vchiq_platform_init(&g_state);
  29155. + if (err != 0)
  29156. + goto failed_platform_init;
  29157. +
  29158. + vchiq_log_info(vchiq_arm_log_level,
  29159. + "vchiq: initialised - version %d (min %d), device %d.%d",
  29160. + VCHIQ_VERSION, VCHIQ_VERSION_MIN,
  29161. + MAJOR(vchiq_devid), MINOR(vchiq_devid));
  29162. +
  29163. + return 0;
  29164. +
  29165. +failed_platform_init:
  29166. + device_destroy(vchiq_class, vchiq_devid);
  29167. +failed_device_create:
  29168. + class_destroy(vchiq_class);
  29169. +failed_class_create:
  29170. + cdev_del(&vchiq_cdev);
  29171. + err = PTR_ERR(ptr_err);
  29172. +failed_cdev_add:
  29173. + unregister_chrdev_region(vchiq_devid, 1);
  29174. +failed_alloc_chrdev:
  29175. + vchiq_debugfs_deinit();
  29176. +failed_debugfs_init:
  29177. + vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq");
  29178. + return err;
  29179. +}
  29180. +
  29181. +/****************************************************************************
  29182. +*
  29183. +* vchiq_exit - called when the module is unloaded.
  29184. +*
  29185. +***************************************************************************/
  29186. +
  29187. +static void __exit
  29188. +vchiq_exit(void)
  29189. +{
  29190. + vchiq_platform_exit(&g_state);
  29191. + device_destroy(vchiq_class, vchiq_devid);
  29192. + class_destroy(vchiq_class);
  29193. + cdev_del(&vchiq_cdev);
  29194. + unregister_chrdev_region(vchiq_devid, 1);
  29195. +}
  29196. +
  29197. +module_init(vchiq_init);
  29198. +module_exit(vchiq_exit);
  29199. +MODULE_LICENSE("GPL");
  29200. +MODULE_AUTHOR("Broadcom Corporation");
  29201. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h
  29202. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h 1969-12-31 18:00:00.000000000 -0600
  29203. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h 2014-12-03 19:13:38.224418001 -0600
  29204. @@ -0,0 +1,223 @@
  29205. +/**
  29206. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  29207. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  29208. + *
  29209. + * Redistribution and use in source and binary forms, with or without
  29210. + * modification, are permitted provided that the following conditions
  29211. + * are met:
  29212. + * 1. Redistributions of source code must retain the above copyright
  29213. + * notice, this list of conditions, and the following disclaimer,
  29214. + * without modification.
  29215. + * 2. Redistributions in binary form must reproduce the above copyright
  29216. + * notice, this list of conditions and the following disclaimer in the
  29217. + * documentation and/or other materials provided with the distribution.
  29218. + * 3. The names of the above-listed copyright holders may not be used
  29219. + * to endorse or promote products derived from this software without
  29220. + * specific prior written permission.
  29221. + *
  29222. + * ALTERNATIVELY, this software may be distributed under the terms of the
  29223. + * GNU General Public License ("GPL") version 2, as published by the Free
  29224. + * Software Foundation.
  29225. + *
  29226. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29227. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29228. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29229. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29230. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29231. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29232. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29233. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29234. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29235. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29236. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29237. + */
  29238. +
  29239. +#ifndef VCHIQ_ARM_H
  29240. +#define VCHIQ_ARM_H
  29241. +
  29242. +#include <linux/mutex.h>
  29243. +#include <linux/semaphore.h>
  29244. +#include <linux/atomic.h>
  29245. +#include "vchiq_core.h"
  29246. +#include "vchiq_debugfs.h"
  29247. +
  29248. +
  29249. +enum vc_suspend_status {
  29250. + VC_SUSPEND_FORCE_CANCELED = -3, /* Force suspend canceled, too busy */
  29251. + VC_SUSPEND_REJECTED = -2, /* Videocore rejected suspend request */
  29252. + VC_SUSPEND_FAILED = -1, /* Videocore suspend failed */
  29253. + VC_SUSPEND_IDLE = 0, /* VC active, no suspend actions */
  29254. + VC_SUSPEND_REQUESTED, /* User has requested suspend */
  29255. + VC_SUSPEND_IN_PROGRESS, /* Slot handler has recvd suspend request */
  29256. + VC_SUSPEND_SUSPENDED /* Videocore suspend succeeded */
  29257. +};
  29258. +
  29259. +enum vc_resume_status {
  29260. + VC_RESUME_FAILED = -1, /* Videocore resume failed */
  29261. + VC_RESUME_IDLE = 0, /* VC suspended, no resume actions */
  29262. + VC_RESUME_REQUESTED, /* User has requested resume */
  29263. + VC_RESUME_IN_PROGRESS, /* Slot handler has received resume request */
  29264. + VC_RESUME_RESUMED /* Videocore resumed successfully (active) */
  29265. +};
  29266. +
  29267. +
  29268. +enum USE_TYPE_E {
  29269. + USE_TYPE_SERVICE,
  29270. + USE_TYPE_SERVICE_NO_RESUME,
  29271. + USE_TYPE_VCHIQ
  29272. +};
  29273. +
  29274. +
  29275. +
  29276. +typedef struct vchiq_arm_state_struct {
  29277. + /* Keepalive-related data */
  29278. + struct task_struct *ka_thread;
  29279. + struct completion ka_evt;
  29280. + atomic_t ka_use_count;
  29281. + atomic_t ka_use_ack_count;
  29282. + atomic_t ka_release_count;
  29283. +
  29284. + struct completion vc_suspend_complete;
  29285. + struct completion vc_resume_complete;
  29286. +
  29287. + rwlock_t susp_res_lock;
  29288. + enum vc_suspend_status vc_suspend_state;
  29289. + enum vc_resume_status vc_resume_state;
  29290. +
  29291. + unsigned int wake_address;
  29292. +
  29293. + struct timer_list suspend_timer;
  29294. + int suspend_timer_timeout;
  29295. + int suspend_timer_running;
  29296. +
  29297. + /* Global use count for videocore.
  29298. + ** This is equal to the sum of the use counts for all services. When
  29299. + ** this hits zero the videocore suspend procedure will be initiated.
  29300. + */
  29301. + int videocore_use_count;
  29302. +
  29303. + /* Use count to track requests from videocore peer.
  29304. + ** This use count is not associated with a service, so needs to be
  29305. + ** tracked separately with the state.
  29306. + */
  29307. + int peer_use_count;
  29308. +
  29309. + /* Flag to indicate whether resume is blocked. This happens when the
  29310. + ** ARM is suspending
  29311. + */
  29312. + struct completion resume_blocker;
  29313. + int resume_blocked;
  29314. + struct completion blocked_blocker;
  29315. + int blocked_count;
  29316. +
  29317. + int autosuspend_override;
  29318. +
  29319. + /* Flag to indicate that the first vchiq connect has made it through.
  29320. + ** This means that both sides should be fully ready, and we should
  29321. + ** be able to suspend after this point.
  29322. + */
  29323. + int first_connect;
  29324. +
  29325. + unsigned long long suspend_start_time;
  29326. + unsigned long long sleep_start_time;
  29327. + unsigned long long resume_start_time;
  29328. + unsigned long long last_wake_time;
  29329. +
  29330. +} VCHIQ_ARM_STATE_T;
  29331. +
  29332. +extern int vchiq_arm_log_level;
  29333. +extern int vchiq_susp_log_level;
  29334. +
  29335. +extern int __init
  29336. +vchiq_platform_init(VCHIQ_STATE_T *state);
  29337. +
  29338. +extern void __exit
  29339. +vchiq_platform_exit(VCHIQ_STATE_T *state);
  29340. +
  29341. +extern VCHIQ_STATE_T *
  29342. +vchiq_get_state(void);
  29343. +
  29344. +extern VCHIQ_STATUS_T
  29345. +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state);
  29346. +
  29347. +extern VCHIQ_STATUS_T
  29348. +vchiq_arm_force_suspend(VCHIQ_STATE_T *state);
  29349. +
  29350. +extern int
  29351. +vchiq_arm_allow_resume(VCHIQ_STATE_T *state);
  29352. +
  29353. +extern VCHIQ_STATUS_T
  29354. +vchiq_arm_vcresume(VCHIQ_STATE_T *state);
  29355. +
  29356. +extern VCHIQ_STATUS_T
  29357. +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state);
  29358. +
  29359. +extern int
  29360. +vchiq_check_resume(VCHIQ_STATE_T *state);
  29361. +
  29362. +extern void
  29363. +vchiq_check_suspend(VCHIQ_STATE_T *state);
  29364. + VCHIQ_STATUS_T
  29365. +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle);
  29366. +
  29367. +extern VCHIQ_STATUS_T
  29368. +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle);
  29369. +
  29370. +extern VCHIQ_STATUS_T
  29371. +vchiq_check_service(VCHIQ_SERVICE_T *service);
  29372. +
  29373. +extern VCHIQ_STATUS_T
  29374. +vchiq_platform_suspend(VCHIQ_STATE_T *state);
  29375. +
  29376. +extern int
  29377. +vchiq_platform_videocore_wanted(VCHIQ_STATE_T *state);
  29378. +
  29379. +extern int
  29380. +vchiq_platform_use_suspend_timer(void);
  29381. +
  29382. +extern void
  29383. +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state);
  29384. +
  29385. +extern void
  29386. +vchiq_dump_service_use_state(VCHIQ_STATE_T *state);
  29387. +
  29388. +extern VCHIQ_ARM_STATE_T*
  29389. +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state);
  29390. +
  29391. +extern int
  29392. +vchiq_videocore_wanted(VCHIQ_STATE_T *state);
  29393. +
  29394. +extern VCHIQ_STATUS_T
  29395. +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  29396. + enum USE_TYPE_E use_type);
  29397. +extern VCHIQ_STATUS_T
  29398. +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service);
  29399. +
  29400. +extern VCHIQ_DEBUGFS_NODE_T *
  29401. +vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance);
  29402. +
  29403. +extern int
  29404. +vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance);
  29405. +
  29406. +extern int
  29407. +vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance);
  29408. +
  29409. +extern int
  29410. +vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance);
  29411. +
  29412. +extern void
  29413. +vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace);
  29414. +
  29415. +extern void
  29416. +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
  29417. + enum vc_suspend_status new_state);
  29418. +
  29419. +extern void
  29420. +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
  29421. + enum vc_resume_status new_state);
  29422. +
  29423. +extern void
  29424. +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state);
  29425. +
  29426. +
  29427. +#endif /* VCHIQ_ARM_H */
  29428. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h
  29429. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h 1969-12-31 18:00:00.000000000 -0600
  29430. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h 2014-12-03 19:13:38.224418001 -0600
  29431. @@ -0,0 +1,37 @@
  29432. +/**
  29433. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  29434. + *
  29435. + * Redistribution and use in source and binary forms, with or without
  29436. + * modification, are permitted provided that the following conditions
  29437. + * are met:
  29438. + * 1. Redistributions of source code must retain the above copyright
  29439. + * notice, this list of conditions, and the following disclaimer,
  29440. + * without modification.
  29441. + * 2. Redistributions in binary form must reproduce the above copyright
  29442. + * notice, this list of conditions and the following disclaimer in the
  29443. + * documentation and/or other materials provided with the distribution.
  29444. + * 3. The names of the above-listed copyright holders may not be used
  29445. + * to endorse or promote products derived from this software without
  29446. + * specific prior written permission.
  29447. + *
  29448. + * ALTERNATIVELY, this software may be distributed under the terms of the
  29449. + * GNU General Public License ("GPL") version 2, as published by the Free
  29450. + * Software Foundation.
  29451. + *
  29452. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29453. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29454. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29455. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29456. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29457. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29458. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29459. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29460. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29461. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29462. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29463. + */
  29464. +
  29465. +const char *vchiq_get_build_hostname(void);
  29466. +const char *vchiq_get_build_version(void);
  29467. +const char *vchiq_get_build_time(void);
  29468. +const char *vchiq_get_build_date(void);
  29469. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h
  29470. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h 1969-12-31 18:00:00.000000000 -0600
  29471. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h 2014-12-03 19:13:38.224418001 -0600
  29472. @@ -0,0 +1,66 @@
  29473. +/**
  29474. + * Copyright (c) 2010-2014 Broadcom. All rights reserved.
  29475. + *
  29476. + * Redistribution and use in source and binary forms, with or without
  29477. + * modification, are permitted provided that the following conditions
  29478. + * are met:
  29479. + * 1. Redistributions of source code must retain the above copyright
  29480. + * notice, this list of conditions, and the following disclaimer,
  29481. + * without modification.
  29482. + * 2. Redistributions in binary form must reproduce the above copyright
  29483. + * notice, this list of conditions and the following disclaimer in the
  29484. + * documentation and/or other materials provided with the distribution.
  29485. + * 3. The names of the above-listed copyright holders may not be used
  29486. + * to endorse or promote products derived from this software without
  29487. + * specific prior written permission.
  29488. + *
  29489. + * ALTERNATIVELY, this software may be distributed under the terms of the
  29490. + * GNU General Public License ("GPL") version 2, as published by the Free
  29491. + * Software Foundation.
  29492. + *
  29493. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29494. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29495. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29496. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29497. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29498. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29499. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29500. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29501. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29502. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29503. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29504. + */
  29505. +
  29506. +#ifndef VCHIQ_CFG_H
  29507. +#define VCHIQ_CFG_H
  29508. +
  29509. +#define VCHIQ_MAGIC VCHIQ_MAKE_FOURCC('V', 'C', 'H', 'I')
  29510. +/* The version of VCHIQ - change with any non-trivial change */
  29511. +#define VCHIQ_VERSION 7
  29512. +/* The minimum compatible version - update to match VCHIQ_VERSION with any
  29513. +** incompatible change */
  29514. +#define VCHIQ_VERSION_MIN 3
  29515. +
  29516. +/* The version that introduced the VCHIQ_IOC_LIB_VERSION ioctl */
  29517. +#define VCHIQ_VERSION_LIB_VERSION 7
  29518. +
  29519. +/* The version that introduced the VCHIQ_IOC_CLOSE_DELIVERED ioctl */
  29520. +#define VCHIQ_VERSION_CLOSE_DELIVERED 7
  29521. +
  29522. +#define VCHIQ_MAX_STATES 1
  29523. +#define VCHIQ_MAX_SERVICES 4096
  29524. +#define VCHIQ_MAX_SLOTS 128
  29525. +#define VCHIQ_MAX_SLOTS_PER_SIDE 64
  29526. +
  29527. +#define VCHIQ_NUM_CURRENT_BULKS 32
  29528. +#define VCHIQ_NUM_SERVICE_BULKS 4
  29529. +
  29530. +#ifndef VCHIQ_ENABLE_DEBUG
  29531. +#define VCHIQ_ENABLE_DEBUG 1
  29532. +#endif
  29533. +
  29534. +#ifndef VCHIQ_ENABLE_STATS
  29535. +#define VCHIQ_ENABLE_STATS 1
  29536. +#endif
  29537. +
  29538. +#endif /* VCHIQ_CFG_H */
  29539. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c
  29540. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c 1969-12-31 18:00:00.000000000 -0600
  29541. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c 2014-12-03 19:13:38.224418001 -0600
  29542. @@ -0,0 +1,120 @@
  29543. +/**
  29544. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  29545. + *
  29546. + * Redistribution and use in source and binary forms, with or without
  29547. + * modification, are permitted provided that the following conditions
  29548. + * are met:
  29549. + * 1. Redistributions of source code must retain the above copyright
  29550. + * notice, this list of conditions, and the following disclaimer,
  29551. + * without modification.
  29552. + * 2. Redistributions in binary form must reproduce the above copyright
  29553. + * notice, this list of conditions and the following disclaimer in the
  29554. + * documentation and/or other materials provided with the distribution.
  29555. + * 3. The names of the above-listed copyright holders may not be used
  29556. + * to endorse or promote products derived from this software without
  29557. + * specific prior written permission.
  29558. + *
  29559. + * ALTERNATIVELY, this software may be distributed under the terms of the
  29560. + * GNU General Public License ("GPL") version 2, as published by the Free
  29561. + * Software Foundation.
  29562. + *
  29563. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29564. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29565. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29566. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29567. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29568. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29569. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29570. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29571. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29572. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29573. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29574. + */
  29575. +
  29576. +#include "vchiq_connected.h"
  29577. +#include "vchiq_core.h"
  29578. +#include "vchiq_killable.h"
  29579. +#include <linux/module.h>
  29580. +#include <linux/mutex.h>
  29581. +
  29582. +#define MAX_CALLBACKS 10
  29583. +
  29584. +static int g_connected;
  29585. +static int g_num_deferred_callbacks;
  29586. +static VCHIQ_CONNECTED_CALLBACK_T g_deferred_callback[MAX_CALLBACKS];
  29587. +static int g_once_init;
  29588. +static struct mutex g_connected_mutex;
  29589. +
  29590. +/****************************************************************************
  29591. +*
  29592. +* Function to initialize our lock.
  29593. +*
  29594. +***************************************************************************/
  29595. +
  29596. +static void connected_init(void)
  29597. +{
  29598. + if (!g_once_init) {
  29599. + mutex_init(&g_connected_mutex);
  29600. + g_once_init = 1;
  29601. + }
  29602. +}
  29603. +
  29604. +/****************************************************************************
  29605. +*
  29606. +* This function is used to defer initialization until the vchiq stack is
  29607. +* initialized. If the stack is already initialized, then the callback will
  29608. +* be made immediately, otherwise it will be deferred until
  29609. +* vchiq_call_connected_callbacks is called.
  29610. +*
  29611. +***************************************************************************/
  29612. +
  29613. +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback)
  29614. +{
  29615. + connected_init();
  29616. +
  29617. + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
  29618. + return;
  29619. +
  29620. + if (g_connected)
  29621. + /* We're already connected. Call the callback immediately. */
  29622. +
  29623. + callback();
  29624. + else {
  29625. + if (g_num_deferred_callbacks >= MAX_CALLBACKS)
  29626. + vchiq_log_error(vchiq_core_log_level,
  29627. + "There already %d callback registered - "
  29628. + "please increase MAX_CALLBACKS",
  29629. + g_num_deferred_callbacks);
  29630. + else {
  29631. + g_deferred_callback[g_num_deferred_callbacks] =
  29632. + callback;
  29633. + g_num_deferred_callbacks++;
  29634. + }
  29635. + }
  29636. + mutex_unlock(&g_connected_mutex);
  29637. +}
  29638. +
  29639. +/****************************************************************************
  29640. +*
  29641. +* This function is called by the vchiq stack once it has been connected to
  29642. +* the videocore and clients can start to use the stack.
  29643. +*
  29644. +***************************************************************************/
  29645. +
  29646. +void vchiq_call_connected_callbacks(void)
  29647. +{
  29648. + int i;
  29649. +
  29650. + connected_init();
  29651. +
  29652. + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
  29653. + return;
  29654. +
  29655. + for (i = 0; i < g_num_deferred_callbacks; i++)
  29656. + g_deferred_callback[i]();
  29657. +
  29658. + g_num_deferred_callbacks = 0;
  29659. + g_connected = 1;
  29660. + mutex_unlock(&g_connected_mutex);
  29661. +}
  29662. +EXPORT_SYMBOL(vchiq_add_connected_callback);
  29663. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h
  29664. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h 1969-12-31 18:00:00.000000000 -0600
  29665. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h 2014-12-03 19:13:38.224418001 -0600
  29666. @@ -0,0 +1,50 @@
  29667. +/**
  29668. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  29669. + *
  29670. + * Redistribution and use in source and binary forms, with or without
  29671. + * modification, are permitted provided that the following conditions
  29672. + * are met:
  29673. + * 1. Redistributions of source code must retain the above copyright
  29674. + * notice, this list of conditions, and the following disclaimer,
  29675. + * without modification.
  29676. + * 2. Redistributions in binary form must reproduce the above copyright
  29677. + * notice, this list of conditions and the following disclaimer in the
  29678. + * documentation and/or other materials provided with the distribution.
  29679. + * 3. The names of the above-listed copyright holders may not be used
  29680. + * to endorse or promote products derived from this software without
  29681. + * specific prior written permission.
  29682. + *
  29683. + * ALTERNATIVELY, this software may be distributed under the terms of the
  29684. + * GNU General Public License ("GPL") version 2, as published by the Free
  29685. + * Software Foundation.
  29686. + *
  29687. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29688. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29689. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29690. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29691. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29692. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29693. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29694. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29695. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29696. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29697. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29698. + */
  29699. +
  29700. +#ifndef VCHIQ_CONNECTED_H
  29701. +#define VCHIQ_CONNECTED_H
  29702. +
  29703. +/* ---- Include Files ----------------------------------------------------- */
  29704. +
  29705. +/* ---- Constants and Types ---------------------------------------------- */
  29706. +
  29707. +typedef void (*VCHIQ_CONNECTED_CALLBACK_T)(void);
  29708. +
  29709. +/* ---- Variable Externs ------------------------------------------------- */
  29710. +
  29711. +/* ---- Function Prototypes ---------------------------------------------- */
  29712. +
  29713. +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback);
  29714. +void vchiq_call_connected_callbacks(void);
  29715. +
  29716. +#endif /* VCHIQ_CONNECTED_H */
  29717. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c
  29718. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c 1969-12-31 18:00:00.000000000 -0600
  29719. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c 2014-12-03 19:13:38.228418001 -0600
  29720. @@ -0,0 +1,3862 @@
  29721. +/**
  29722. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  29723. + *
  29724. + * Redistribution and use in source and binary forms, with or without
  29725. + * modification, are permitted provided that the following conditions
  29726. + * are met:
  29727. + * 1. Redistributions of source code must retain the above copyright
  29728. + * notice, this list of conditions, and the following disclaimer,
  29729. + * without modification.
  29730. + * 2. Redistributions in binary form must reproduce the above copyright
  29731. + * notice, this list of conditions and the following disclaimer in the
  29732. + * documentation and/or other materials provided with the distribution.
  29733. + * 3. The names of the above-listed copyright holders may not be used
  29734. + * to endorse or promote products derived from this software without
  29735. + * specific prior written permission.
  29736. + *
  29737. + * ALTERNATIVELY, this software may be distributed under the terms of the
  29738. + * GNU General Public License ("GPL") version 2, as published by the Free
  29739. + * Software Foundation.
  29740. + *
  29741. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29742. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29743. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29744. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29745. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29746. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29747. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29748. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29749. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29750. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29751. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29752. + */
  29753. +
  29754. +#include "vchiq_core.h"
  29755. +#include "vchiq_killable.h"
  29756. +
  29757. +#define VCHIQ_SLOT_HANDLER_STACK 8192
  29758. +
  29759. +#define HANDLE_STATE_SHIFT 12
  29760. +
  29761. +#define SLOT_INFO_FROM_INDEX(state, index) (state->slot_info + (index))
  29762. +#define SLOT_DATA_FROM_INDEX(state, index) (state->slot_data + (index))
  29763. +#define SLOT_INDEX_FROM_DATA(state, data) \
  29764. + (((unsigned int)((char *)data - (char *)state->slot_data)) / \
  29765. + VCHIQ_SLOT_SIZE)
  29766. +#define SLOT_INDEX_FROM_INFO(state, info) \
  29767. + ((unsigned int)(info - state->slot_info))
  29768. +#define SLOT_QUEUE_INDEX_FROM_POS(pos) \
  29769. + ((int)((unsigned int)(pos) / VCHIQ_SLOT_SIZE))
  29770. +
  29771. +#define BULK_INDEX(x) (x & (VCHIQ_NUM_SERVICE_BULKS - 1))
  29772. +
  29773. +#define SRVTRACE_LEVEL(srv) \
  29774. + (((srv) && (srv)->trace) ? VCHIQ_LOG_TRACE : vchiq_core_msg_log_level)
  29775. +#define SRVTRACE_ENABLED(srv, lev) \
  29776. + (((srv) && (srv)->trace) || (vchiq_core_msg_log_level >= (lev)))
  29777. +
  29778. +struct vchiq_open_payload {
  29779. + int fourcc;
  29780. + int client_id;
  29781. + short version;
  29782. + short version_min;
  29783. +};
  29784. +
  29785. +struct vchiq_openack_payload {
  29786. + short version;
  29787. +};
  29788. +
  29789. +/* we require this for consistency between endpoints */
  29790. +vchiq_static_assert(sizeof(VCHIQ_HEADER_T) == 8);
  29791. +vchiq_static_assert(IS_POW2(sizeof(VCHIQ_HEADER_T)));
  29792. +vchiq_static_assert(IS_POW2(VCHIQ_NUM_CURRENT_BULKS));
  29793. +vchiq_static_assert(IS_POW2(VCHIQ_NUM_SERVICE_BULKS));
  29794. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SERVICES));
  29795. +vchiq_static_assert(VCHIQ_VERSION >= VCHIQ_VERSION_MIN);
  29796. +
  29797. +/* Run time control of log level, based on KERN_XXX level. */
  29798. +int vchiq_core_log_level = VCHIQ_LOG_DEFAULT;
  29799. +int vchiq_core_msg_log_level = VCHIQ_LOG_DEFAULT;
  29800. +int vchiq_sync_log_level = VCHIQ_LOG_DEFAULT;
  29801. +
  29802. +static atomic_t pause_bulks_count = ATOMIC_INIT(0);
  29803. +
  29804. +static DEFINE_SPINLOCK(service_spinlock);
  29805. +DEFINE_SPINLOCK(bulk_waiter_spinlock);
  29806. +DEFINE_SPINLOCK(quota_spinlock);
  29807. +
  29808. +VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
  29809. +static unsigned int handle_seq;
  29810. +
  29811. +static const char *const srvstate_names[] = {
  29812. + "FREE",
  29813. + "HIDDEN",
  29814. + "LISTENING",
  29815. + "OPENING",
  29816. + "OPEN",
  29817. + "OPENSYNC",
  29818. + "CLOSESENT",
  29819. + "CLOSERECVD",
  29820. + "CLOSEWAIT",
  29821. + "CLOSED"
  29822. +};
  29823. +
  29824. +static const char *const reason_names[] = {
  29825. + "SERVICE_OPENED",
  29826. + "SERVICE_CLOSED",
  29827. + "MESSAGE_AVAILABLE",
  29828. + "BULK_TRANSMIT_DONE",
  29829. + "BULK_RECEIVE_DONE",
  29830. + "BULK_TRANSMIT_ABORTED",
  29831. + "BULK_RECEIVE_ABORTED"
  29832. +};
  29833. +
  29834. +static const char *const conn_state_names[] = {
  29835. + "DISCONNECTED",
  29836. + "CONNECTING",
  29837. + "CONNECTED",
  29838. + "PAUSING",
  29839. + "PAUSE_SENT",
  29840. + "PAUSED",
  29841. + "RESUMING",
  29842. + "PAUSE_TIMEOUT",
  29843. + "RESUME_TIMEOUT"
  29844. +};
  29845. +
  29846. +
  29847. +static void
  29848. +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header);
  29849. +
  29850. +static const char *msg_type_str(unsigned int msg_type)
  29851. +{
  29852. + switch (msg_type) {
  29853. + case VCHIQ_MSG_PADDING: return "PADDING";
  29854. + case VCHIQ_MSG_CONNECT: return "CONNECT";
  29855. + case VCHIQ_MSG_OPEN: return "OPEN";
  29856. + case VCHIQ_MSG_OPENACK: return "OPENACK";
  29857. + case VCHIQ_MSG_CLOSE: return "CLOSE";
  29858. + case VCHIQ_MSG_DATA: return "DATA";
  29859. + case VCHIQ_MSG_BULK_RX: return "BULK_RX";
  29860. + case VCHIQ_MSG_BULK_TX: return "BULK_TX";
  29861. + case VCHIQ_MSG_BULK_RX_DONE: return "BULK_RX_DONE";
  29862. + case VCHIQ_MSG_BULK_TX_DONE: return "BULK_TX_DONE";
  29863. + case VCHIQ_MSG_PAUSE: return "PAUSE";
  29864. + case VCHIQ_MSG_RESUME: return "RESUME";
  29865. + case VCHIQ_MSG_REMOTE_USE: return "REMOTE_USE";
  29866. + case VCHIQ_MSG_REMOTE_RELEASE: return "REMOTE_RELEASE";
  29867. + case VCHIQ_MSG_REMOTE_USE_ACTIVE: return "REMOTE_USE_ACTIVE";
  29868. + }
  29869. + return "???";
  29870. +}
  29871. +
  29872. +static inline void
  29873. +vchiq_set_service_state(VCHIQ_SERVICE_T *service, int newstate)
  29874. +{
  29875. + vchiq_log_info(vchiq_core_log_level, "%d: srv:%d %s->%s",
  29876. + service->state->id, service->localport,
  29877. + srvstate_names[service->srvstate],
  29878. + srvstate_names[newstate]);
  29879. + service->srvstate = newstate;
  29880. +}
  29881. +
  29882. +VCHIQ_SERVICE_T *
  29883. +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle)
  29884. +{
  29885. + VCHIQ_SERVICE_T *service;
  29886. +
  29887. + spin_lock(&service_spinlock);
  29888. + service = handle_to_service(handle);
  29889. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  29890. + (service->handle == handle)) {
  29891. + BUG_ON(service->ref_count == 0);
  29892. + service->ref_count++;
  29893. + } else
  29894. + service = NULL;
  29895. + spin_unlock(&service_spinlock);
  29896. +
  29897. + if (!service)
  29898. + vchiq_log_info(vchiq_core_log_level,
  29899. + "Invalid service handle 0x%x", handle);
  29900. +
  29901. + return service;
  29902. +}
  29903. +
  29904. +VCHIQ_SERVICE_T *
  29905. +find_service_by_port(VCHIQ_STATE_T *state, int localport)
  29906. +{
  29907. + VCHIQ_SERVICE_T *service = NULL;
  29908. + if ((unsigned int)localport <= VCHIQ_PORT_MAX) {
  29909. + spin_lock(&service_spinlock);
  29910. + service = state->services[localport];
  29911. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE)) {
  29912. + BUG_ON(service->ref_count == 0);
  29913. + service->ref_count++;
  29914. + } else
  29915. + service = NULL;
  29916. + spin_unlock(&service_spinlock);
  29917. + }
  29918. +
  29919. + if (!service)
  29920. + vchiq_log_info(vchiq_core_log_level,
  29921. + "Invalid port %d", localport);
  29922. +
  29923. + return service;
  29924. +}
  29925. +
  29926. +VCHIQ_SERVICE_T *
  29927. +find_service_for_instance(VCHIQ_INSTANCE_T instance,
  29928. + VCHIQ_SERVICE_HANDLE_T handle) {
  29929. + VCHIQ_SERVICE_T *service;
  29930. +
  29931. + spin_lock(&service_spinlock);
  29932. + service = handle_to_service(handle);
  29933. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  29934. + (service->handle == handle) &&
  29935. + (service->instance == instance)) {
  29936. + BUG_ON(service->ref_count == 0);
  29937. + service->ref_count++;
  29938. + } else
  29939. + service = NULL;
  29940. + spin_unlock(&service_spinlock);
  29941. +
  29942. + if (!service)
  29943. + vchiq_log_info(vchiq_core_log_level,
  29944. + "Invalid service handle 0x%x", handle);
  29945. +
  29946. + return service;
  29947. +}
  29948. +
  29949. +VCHIQ_SERVICE_T *
  29950. +find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
  29951. + VCHIQ_SERVICE_HANDLE_T handle) {
  29952. + VCHIQ_SERVICE_T *service;
  29953. +
  29954. + spin_lock(&service_spinlock);
  29955. + service = handle_to_service(handle);
  29956. + if (service &&
  29957. + ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  29958. + (service->srvstate == VCHIQ_SRVSTATE_CLOSED)) &&
  29959. + (service->handle == handle) &&
  29960. + (service->instance == instance)) {
  29961. + BUG_ON(service->ref_count == 0);
  29962. + service->ref_count++;
  29963. + } else
  29964. + service = NULL;
  29965. + spin_unlock(&service_spinlock);
  29966. +
  29967. + if (!service)
  29968. + vchiq_log_info(vchiq_core_log_level,
  29969. + "Invalid service handle 0x%x", handle);
  29970. +
  29971. + return service;
  29972. +}
  29973. +
  29974. +VCHIQ_SERVICE_T *
  29975. +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
  29976. + int *pidx)
  29977. +{
  29978. + VCHIQ_SERVICE_T *service = NULL;
  29979. + int idx = *pidx;
  29980. +
  29981. + spin_lock(&service_spinlock);
  29982. + while (idx < state->unused_service) {
  29983. + VCHIQ_SERVICE_T *srv = state->services[idx++];
  29984. + if (srv && (srv->srvstate != VCHIQ_SRVSTATE_FREE) &&
  29985. + (srv->instance == instance)) {
  29986. + service = srv;
  29987. + BUG_ON(service->ref_count == 0);
  29988. + service->ref_count++;
  29989. + break;
  29990. + }
  29991. + }
  29992. + spin_unlock(&service_spinlock);
  29993. +
  29994. + *pidx = idx;
  29995. +
  29996. + return service;
  29997. +}
  29998. +
  29999. +void
  30000. +lock_service(VCHIQ_SERVICE_T *service)
  30001. +{
  30002. + spin_lock(&service_spinlock);
  30003. + BUG_ON(!service || (service->ref_count == 0));
  30004. + if (service)
  30005. + service->ref_count++;
  30006. + spin_unlock(&service_spinlock);
  30007. +}
  30008. +
  30009. +void
  30010. +unlock_service(VCHIQ_SERVICE_T *service)
  30011. +{
  30012. + VCHIQ_STATE_T *state = service->state;
  30013. + spin_lock(&service_spinlock);
  30014. + BUG_ON(!service || (service->ref_count == 0));
  30015. + if (service && service->ref_count) {
  30016. + service->ref_count--;
  30017. + if (!service->ref_count) {
  30018. + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
  30019. + state->services[service->localport] = NULL;
  30020. + } else
  30021. + service = NULL;
  30022. + }
  30023. + spin_unlock(&service_spinlock);
  30024. +
  30025. + if (service && service->userdata_term)
  30026. + service->userdata_term(service->base.userdata);
  30027. +
  30028. + kfree(service);
  30029. +}
  30030. +
  30031. +int
  30032. +vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T handle)
  30033. +{
  30034. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  30035. + int id;
  30036. +
  30037. + id = service ? service->client_id : 0;
  30038. + if (service)
  30039. + unlock_service(service);
  30040. +
  30041. + return id;
  30042. +}
  30043. +
  30044. +void *
  30045. +vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T handle)
  30046. +{
  30047. + VCHIQ_SERVICE_T *service = handle_to_service(handle);
  30048. +
  30049. + return service ? service->base.userdata : NULL;
  30050. +}
  30051. +
  30052. +int
  30053. +vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T handle)
  30054. +{
  30055. + VCHIQ_SERVICE_T *service = handle_to_service(handle);
  30056. +
  30057. + return service ? service->base.fourcc : 0;
  30058. +}
  30059. +
  30060. +static void
  30061. +mark_service_closing_internal(VCHIQ_SERVICE_T *service, int sh_thread)
  30062. +{
  30063. + VCHIQ_STATE_T *state = service->state;
  30064. + VCHIQ_SERVICE_QUOTA_T *service_quota;
  30065. +
  30066. + service->closing = 1;
  30067. +
  30068. + /* Synchronise with other threads. */
  30069. + mutex_lock(&state->recycle_mutex);
  30070. + mutex_unlock(&state->recycle_mutex);
  30071. + if (!sh_thread || (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT)) {
  30072. + /* If we're pausing then the slot_mutex is held until resume
  30073. + * by the slot handler. Therefore don't try to acquire this
  30074. + * mutex if we're the slot handler and in the pause sent state.
  30075. + * We don't need to in this case anyway. */
  30076. + mutex_lock(&state->slot_mutex);
  30077. + mutex_unlock(&state->slot_mutex);
  30078. + }
  30079. +
  30080. + /* Unblock any sending thread. */
  30081. + service_quota = &state->service_quotas[service->localport];
  30082. + up(&service_quota->quota_event);
  30083. +}
  30084. +
  30085. +static void
  30086. +mark_service_closing(VCHIQ_SERVICE_T *service)
  30087. +{
  30088. + mark_service_closing_internal(service, 0);
  30089. +}
  30090. +
  30091. +static inline VCHIQ_STATUS_T
  30092. +make_service_callback(VCHIQ_SERVICE_T *service, VCHIQ_REASON_T reason,
  30093. + VCHIQ_HEADER_T *header, void *bulk_userdata)
  30094. +{
  30095. + VCHIQ_STATUS_T status;
  30096. + vchiq_log_trace(vchiq_core_log_level, "%d: callback:%d (%s, %x, %x)",
  30097. + service->state->id, service->localport, reason_names[reason],
  30098. + (unsigned int)header, (unsigned int)bulk_userdata);
  30099. + status = service->base.callback(reason, header, service->handle,
  30100. + bulk_userdata);
  30101. + if (status == VCHIQ_ERROR) {
  30102. + vchiq_log_warning(vchiq_core_log_level,
  30103. + "%d: ignoring ERROR from callback to service %x",
  30104. + service->state->id, service->handle);
  30105. + status = VCHIQ_SUCCESS;
  30106. + }
  30107. + return status;
  30108. +}
  30109. +
  30110. +inline void
  30111. +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate)
  30112. +{
  30113. + VCHIQ_CONNSTATE_T oldstate = state->conn_state;
  30114. + vchiq_log_info(vchiq_core_log_level, "%d: %s->%s", state->id,
  30115. + conn_state_names[oldstate],
  30116. + conn_state_names[newstate]);
  30117. + state->conn_state = newstate;
  30118. + vchiq_platform_conn_state_changed(state, oldstate, newstate);
  30119. +}
  30120. +
  30121. +static inline void
  30122. +remote_event_create(REMOTE_EVENT_T *event)
  30123. +{
  30124. + event->armed = 0;
  30125. + /* Don't clear the 'fired' flag because it may already have been set
  30126. + ** by the other side. */
  30127. + sema_init(event->event, 0);
  30128. +}
  30129. +
  30130. +static inline void
  30131. +remote_event_destroy(REMOTE_EVENT_T *event)
  30132. +{
  30133. + (void)event;
  30134. +}
  30135. +
  30136. +static inline int
  30137. +remote_event_wait(REMOTE_EVENT_T *event)
  30138. +{
  30139. + if (!event->fired) {
  30140. + event->armed = 1;
  30141. + dsb();
  30142. + if (!event->fired) {
  30143. + if (down_interruptible(event->event) != 0) {
  30144. + event->armed = 0;
  30145. + return 0;
  30146. + }
  30147. + }
  30148. + event->armed = 0;
  30149. + wmb();
  30150. + }
  30151. +
  30152. + event->fired = 0;
  30153. + return 1;
  30154. +}
  30155. +
  30156. +static inline void
  30157. +remote_event_signal_local(REMOTE_EVENT_T *event)
  30158. +{
  30159. + event->armed = 0;
  30160. + up(event->event);
  30161. +}
  30162. +
  30163. +static inline void
  30164. +remote_event_poll(REMOTE_EVENT_T *event)
  30165. +{
  30166. + if (event->fired && event->armed)
  30167. + remote_event_signal_local(event);
  30168. +}
  30169. +
  30170. +void
  30171. +remote_event_pollall(VCHIQ_STATE_T *state)
  30172. +{
  30173. + remote_event_poll(&state->local->sync_trigger);
  30174. + remote_event_poll(&state->local->sync_release);
  30175. + remote_event_poll(&state->local->trigger);
  30176. + remote_event_poll(&state->local->recycle);
  30177. +}
  30178. +
  30179. +/* Round up message sizes so that any space at the end of a slot is always big
  30180. +** enough for a header. This relies on header size being a power of two, which
  30181. +** has been verified earlier by a static assertion. */
  30182. +
  30183. +static inline unsigned int
  30184. +calc_stride(unsigned int size)
  30185. +{
  30186. + /* Allow room for the header */
  30187. + size += sizeof(VCHIQ_HEADER_T);
  30188. +
  30189. + /* Round up */
  30190. + return (size + sizeof(VCHIQ_HEADER_T) - 1) & ~(sizeof(VCHIQ_HEADER_T)
  30191. + - 1);
  30192. +}
  30193. +
  30194. +/* Called by the slot handler thread */
  30195. +static VCHIQ_SERVICE_T *
  30196. +get_listening_service(VCHIQ_STATE_T *state, int fourcc)
  30197. +{
  30198. + int i;
  30199. +
  30200. + WARN_ON(fourcc == VCHIQ_FOURCC_INVALID);
  30201. +
  30202. + for (i = 0; i < state->unused_service; i++) {
  30203. + VCHIQ_SERVICE_T *service = state->services[i];
  30204. + if (service &&
  30205. + (service->public_fourcc == fourcc) &&
  30206. + ((service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  30207. + ((service->srvstate == VCHIQ_SRVSTATE_OPEN) &&
  30208. + (service->remoteport == VCHIQ_PORT_FREE)))) {
  30209. + lock_service(service);
  30210. + return service;
  30211. + }
  30212. + }
  30213. +
  30214. + return NULL;
  30215. +}
  30216. +
  30217. +/* Called by the slot handler thread */
  30218. +static VCHIQ_SERVICE_T *
  30219. +get_connected_service(VCHIQ_STATE_T *state, unsigned int port)
  30220. +{
  30221. + int i;
  30222. + for (i = 0; i < state->unused_service; i++) {
  30223. + VCHIQ_SERVICE_T *service = state->services[i];
  30224. + if (service && (service->srvstate == VCHIQ_SRVSTATE_OPEN)
  30225. + && (service->remoteport == port)) {
  30226. + lock_service(service);
  30227. + return service;
  30228. + }
  30229. + }
  30230. + return NULL;
  30231. +}
  30232. +
  30233. +inline void
  30234. +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type)
  30235. +{
  30236. + uint32_t value;
  30237. +
  30238. + if (service) {
  30239. + do {
  30240. + value = atomic_read(&service->poll_flags);
  30241. + } while (atomic_cmpxchg(&service->poll_flags, value,
  30242. + value | (1 << poll_type)) != value);
  30243. +
  30244. + do {
  30245. + value = atomic_read(&state->poll_services[
  30246. + service->localport>>5]);
  30247. + } while (atomic_cmpxchg(
  30248. + &state->poll_services[service->localport>>5],
  30249. + value, value | (1 << (service->localport & 0x1f)))
  30250. + != value);
  30251. + }
  30252. +
  30253. + state->poll_needed = 1;
  30254. + wmb();
  30255. +
  30256. + /* ... and ensure the slot handler runs. */
  30257. + remote_event_signal_local(&state->local->trigger);
  30258. +}
  30259. +
  30260. +/* Called from queue_message, by the slot handler and application threads,
  30261. +** with slot_mutex held */
  30262. +static VCHIQ_HEADER_T *
  30263. +reserve_space(VCHIQ_STATE_T *state, int space, int is_blocking)
  30264. +{
  30265. + VCHIQ_SHARED_STATE_T *local = state->local;
  30266. + int tx_pos = state->local_tx_pos;
  30267. + int slot_space = VCHIQ_SLOT_SIZE - (tx_pos & VCHIQ_SLOT_MASK);
  30268. +
  30269. + if (space > slot_space) {
  30270. + VCHIQ_HEADER_T *header;
  30271. + /* Fill the remaining space with padding */
  30272. + WARN_ON(state->tx_data == NULL);
  30273. + header = (VCHIQ_HEADER_T *)
  30274. + (state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
  30275. + header->msgid = VCHIQ_MSGID_PADDING;
  30276. + header->size = slot_space - sizeof(VCHIQ_HEADER_T);
  30277. +
  30278. + tx_pos += slot_space;
  30279. + }
  30280. +
  30281. + /* If necessary, get the next slot. */
  30282. + if ((tx_pos & VCHIQ_SLOT_MASK) == 0) {
  30283. + int slot_index;
  30284. +
  30285. + /* If there is no free slot... */
  30286. +
  30287. + if (down_trylock(&state->slot_available_event) != 0) {
  30288. + /* ...wait for one. */
  30289. +
  30290. + VCHIQ_STATS_INC(state, slot_stalls);
  30291. +
  30292. + /* But first, flush through the last slot. */
  30293. + state->local_tx_pos = tx_pos;
  30294. + local->tx_pos = tx_pos;
  30295. + remote_event_signal(&state->remote->trigger);
  30296. +
  30297. + if (!is_blocking ||
  30298. + (down_interruptible(
  30299. + &state->slot_available_event) != 0))
  30300. + return NULL; /* No space available */
  30301. + }
  30302. +
  30303. + BUG_ON(tx_pos ==
  30304. + (state->slot_queue_available * VCHIQ_SLOT_SIZE));
  30305. +
  30306. + slot_index = local->slot_queue[
  30307. + SLOT_QUEUE_INDEX_FROM_POS(tx_pos) &
  30308. + VCHIQ_SLOT_QUEUE_MASK];
  30309. + state->tx_data =
  30310. + (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
  30311. + }
  30312. +
  30313. + state->local_tx_pos = tx_pos + space;
  30314. +
  30315. + return (VCHIQ_HEADER_T *)(state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
  30316. +}
  30317. +
  30318. +/* Called by the recycle thread. */
  30319. +static void
  30320. +process_free_queue(VCHIQ_STATE_T *state)
  30321. +{
  30322. + VCHIQ_SHARED_STATE_T *local = state->local;
  30323. + BITSET_T service_found[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
  30324. + int slot_queue_available;
  30325. +
  30326. + /* Use a read memory barrier to ensure that any state that may have
  30327. + ** been modified by another thread is not masked by stale prefetched
  30328. + ** values. */
  30329. + rmb();
  30330. +
  30331. + /* Find slots which have been freed by the other side, and return them
  30332. + ** to the available queue. */
  30333. + slot_queue_available = state->slot_queue_available;
  30334. +
  30335. + while (slot_queue_available != local->slot_queue_recycle) {
  30336. + unsigned int pos;
  30337. + int slot_index = local->slot_queue[slot_queue_available++ &
  30338. + VCHIQ_SLOT_QUEUE_MASK];
  30339. + char *data = (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
  30340. + int data_found = 0;
  30341. +
  30342. + vchiq_log_trace(vchiq_core_log_level, "%d: pfq %d=%x %x %x",
  30343. + state->id, slot_index, (unsigned int)data,
  30344. + local->slot_queue_recycle, slot_queue_available);
  30345. +
  30346. + /* Initialise the bitmask for services which have used this
  30347. + ** slot */
  30348. + BITSET_ZERO(service_found);
  30349. +
  30350. + pos = 0;
  30351. +
  30352. + while (pos < VCHIQ_SLOT_SIZE) {
  30353. + VCHIQ_HEADER_T *header =
  30354. + (VCHIQ_HEADER_T *)(data + pos);
  30355. + int msgid = header->msgid;
  30356. + if (VCHIQ_MSG_TYPE(msgid) == VCHIQ_MSG_DATA) {
  30357. + int port = VCHIQ_MSG_SRCPORT(msgid);
  30358. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  30359. + &state->service_quotas[port];
  30360. + int count;
  30361. + spin_lock(&quota_spinlock);
  30362. + count = service_quota->message_use_count;
  30363. + if (count > 0)
  30364. + service_quota->message_use_count =
  30365. + count - 1;
  30366. + spin_unlock(&quota_spinlock);
  30367. +
  30368. + if (count == service_quota->message_quota)
  30369. + /* Signal the service that it
  30370. + ** has dropped below its quota
  30371. + */
  30372. + up(&service_quota->quota_event);
  30373. + else if (count == 0) {
  30374. + vchiq_log_error(vchiq_core_log_level,
  30375. + "service %d "
  30376. + "message_use_count=%d "
  30377. + "(header %x, msgid %x, "
  30378. + "header->msgid %x, "
  30379. + "header->size %x)",
  30380. + port,
  30381. + service_quota->
  30382. + message_use_count,
  30383. + (unsigned int)header, msgid,
  30384. + header->msgid,
  30385. + header->size);
  30386. + WARN(1, "invalid message use count\n");
  30387. + }
  30388. + if (!BITSET_IS_SET(service_found, port)) {
  30389. + /* Set the found bit for this service */
  30390. + BITSET_SET(service_found, port);
  30391. +
  30392. + spin_lock(&quota_spinlock);
  30393. + count = service_quota->slot_use_count;
  30394. + if (count > 0)
  30395. + service_quota->slot_use_count =
  30396. + count - 1;
  30397. + spin_unlock(&quota_spinlock);
  30398. +
  30399. + if (count > 0) {
  30400. + /* Signal the service in case
  30401. + ** it has dropped below its
  30402. + ** quota */
  30403. + up(&service_quota->quota_event);
  30404. + vchiq_log_trace(
  30405. + vchiq_core_log_level,
  30406. + "%d: pfq:%d %x@%x - "
  30407. + "slot_use->%d",
  30408. + state->id, port,
  30409. + header->size,
  30410. + (unsigned int)header,
  30411. + count - 1);
  30412. + } else {
  30413. + vchiq_log_error(
  30414. + vchiq_core_log_level,
  30415. + "service %d "
  30416. + "slot_use_count"
  30417. + "=%d (header %x"
  30418. + ", msgid %x, "
  30419. + "header->msgid"
  30420. + " %x, header->"
  30421. + "size %x)",
  30422. + port, count,
  30423. + (unsigned int)header,
  30424. + msgid,
  30425. + header->msgid,
  30426. + header->size);
  30427. + WARN(1, "bad slot use count\n");
  30428. + }
  30429. + }
  30430. +
  30431. + data_found = 1;
  30432. + }
  30433. +
  30434. + pos += calc_stride(header->size);
  30435. + if (pos > VCHIQ_SLOT_SIZE) {
  30436. + vchiq_log_error(vchiq_core_log_level,
  30437. + "pfq - pos %x: header %x, msgid %x, "
  30438. + "header->msgid %x, header->size %x",
  30439. + pos, (unsigned int)header, msgid,
  30440. + header->msgid, header->size);
  30441. + WARN(1, "invalid slot position\n");
  30442. + }
  30443. + }
  30444. +
  30445. + if (data_found) {
  30446. + int count;
  30447. + spin_lock(&quota_spinlock);
  30448. + count = state->data_use_count;
  30449. + if (count > 0)
  30450. + state->data_use_count =
  30451. + count - 1;
  30452. + spin_unlock(&quota_spinlock);
  30453. + if (count == state->data_quota)
  30454. + up(&state->data_quota_event);
  30455. + }
  30456. +
  30457. + state->slot_queue_available = slot_queue_available;
  30458. + up(&state->slot_available_event);
  30459. + }
  30460. +}
  30461. +
  30462. +/* Called by the slot handler and application threads */
  30463. +static VCHIQ_STATUS_T
  30464. +queue_message(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  30465. + int msgid, const VCHIQ_ELEMENT_T *elements,
  30466. + int count, int size, int is_blocking)
  30467. +{
  30468. + VCHIQ_SHARED_STATE_T *local;
  30469. + VCHIQ_SERVICE_QUOTA_T *service_quota = NULL;
  30470. + VCHIQ_HEADER_T *header;
  30471. + int type = VCHIQ_MSG_TYPE(msgid);
  30472. +
  30473. + unsigned int stride;
  30474. +
  30475. + local = state->local;
  30476. +
  30477. + stride = calc_stride(size);
  30478. +
  30479. + WARN_ON(!(stride <= VCHIQ_SLOT_SIZE));
  30480. +
  30481. + if ((type != VCHIQ_MSG_RESUME) &&
  30482. + (mutex_lock_interruptible(&state->slot_mutex) != 0))
  30483. + return VCHIQ_RETRY;
  30484. +
  30485. + if (type == VCHIQ_MSG_DATA) {
  30486. + int tx_end_index;
  30487. +
  30488. + BUG_ON(!service);
  30489. +
  30490. + if (service->closing) {
  30491. + /* The service has been closed */
  30492. + mutex_unlock(&state->slot_mutex);
  30493. + return VCHIQ_ERROR;
  30494. + }
  30495. +
  30496. + service_quota = &state->service_quotas[service->localport];
  30497. +
  30498. + spin_lock(&quota_spinlock);
  30499. +
  30500. + /* Ensure this service doesn't use more than its quota of
  30501. + ** messages or slots */
  30502. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  30503. + state->local_tx_pos + stride - 1);
  30504. +
  30505. + /* Ensure data messages don't use more than their quota of
  30506. + ** slots */
  30507. + while ((tx_end_index != state->previous_data_index) &&
  30508. + (state->data_use_count == state->data_quota)) {
  30509. + VCHIQ_STATS_INC(state, data_stalls);
  30510. + spin_unlock(&quota_spinlock);
  30511. + mutex_unlock(&state->slot_mutex);
  30512. +
  30513. + if (down_interruptible(&state->data_quota_event)
  30514. + != 0)
  30515. + return VCHIQ_RETRY;
  30516. +
  30517. + mutex_lock(&state->slot_mutex);
  30518. + spin_lock(&quota_spinlock);
  30519. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  30520. + state->local_tx_pos + stride - 1);
  30521. + if ((tx_end_index == state->previous_data_index) ||
  30522. + (state->data_use_count < state->data_quota)) {
  30523. + /* Pass the signal on to other waiters */
  30524. + up(&state->data_quota_event);
  30525. + break;
  30526. + }
  30527. + }
  30528. +
  30529. + while ((service_quota->message_use_count ==
  30530. + service_quota->message_quota) ||
  30531. + ((tx_end_index != service_quota->previous_tx_index) &&
  30532. + (service_quota->slot_use_count ==
  30533. + service_quota->slot_quota))) {
  30534. + spin_unlock(&quota_spinlock);
  30535. + vchiq_log_trace(vchiq_core_log_level,
  30536. + "%d: qm:%d %s,%x - quota stall "
  30537. + "(msg %d, slot %d)",
  30538. + state->id, service->localport,
  30539. + msg_type_str(type), size,
  30540. + service_quota->message_use_count,
  30541. + service_quota->slot_use_count);
  30542. + VCHIQ_SERVICE_STATS_INC(service, quota_stalls);
  30543. + mutex_unlock(&state->slot_mutex);
  30544. + if (down_interruptible(&service_quota->quota_event)
  30545. + != 0)
  30546. + return VCHIQ_RETRY;
  30547. + if (service->closing)
  30548. + return VCHIQ_ERROR;
  30549. + if (mutex_lock_interruptible(&state->slot_mutex) != 0)
  30550. + return VCHIQ_RETRY;
  30551. + if (service->srvstate != VCHIQ_SRVSTATE_OPEN) {
  30552. + /* The service has been closed */
  30553. + mutex_unlock(&state->slot_mutex);
  30554. + return VCHIQ_ERROR;
  30555. + }
  30556. + spin_lock(&quota_spinlock);
  30557. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  30558. + state->local_tx_pos + stride - 1);
  30559. + }
  30560. +
  30561. + spin_unlock(&quota_spinlock);
  30562. + }
  30563. +
  30564. + header = reserve_space(state, stride, is_blocking);
  30565. +
  30566. + if (!header) {
  30567. + if (service)
  30568. + VCHIQ_SERVICE_STATS_INC(service, slot_stalls);
  30569. + mutex_unlock(&state->slot_mutex);
  30570. + return VCHIQ_RETRY;
  30571. + }
  30572. +
  30573. + if (type == VCHIQ_MSG_DATA) {
  30574. + int i, pos;
  30575. + int tx_end_index;
  30576. + int slot_use_count;
  30577. +
  30578. + vchiq_log_info(vchiq_core_log_level,
  30579. + "%d: qm %s@%x,%x (%d->%d)",
  30580. + state->id,
  30581. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  30582. + (unsigned int)header, size,
  30583. + VCHIQ_MSG_SRCPORT(msgid),
  30584. + VCHIQ_MSG_DSTPORT(msgid));
  30585. +
  30586. + BUG_ON(!service);
  30587. +
  30588. + for (i = 0, pos = 0; i < (unsigned int)count;
  30589. + pos += elements[i++].size)
  30590. + if (elements[i].size) {
  30591. + if (vchiq_copy_from_user
  30592. + (header->data + pos, elements[i].data,
  30593. + (size_t) elements[i].size) !=
  30594. + VCHIQ_SUCCESS) {
  30595. + mutex_unlock(&state->slot_mutex);
  30596. + VCHIQ_SERVICE_STATS_INC(service,
  30597. + error_count);
  30598. + return VCHIQ_ERROR;
  30599. + }
  30600. + if (i == 0) {
  30601. + if (SRVTRACE_ENABLED(service,
  30602. + VCHIQ_LOG_INFO))
  30603. + vchiq_log_dump_mem("Sent", 0,
  30604. + header->data + pos,
  30605. + min(64u,
  30606. + elements[0].size));
  30607. + }
  30608. + }
  30609. +
  30610. + spin_lock(&quota_spinlock);
  30611. + service_quota->message_use_count++;
  30612. +
  30613. + tx_end_index =
  30614. + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos - 1);
  30615. +
  30616. + /* If this transmission can't fit in the last slot used by any
  30617. + ** service, the data_use_count must be increased. */
  30618. + if (tx_end_index != state->previous_data_index) {
  30619. + state->previous_data_index = tx_end_index;
  30620. + state->data_use_count++;
  30621. + }
  30622. +
  30623. + /* If this isn't the same slot last used by this service,
  30624. + ** the service's slot_use_count must be increased. */
  30625. + if (tx_end_index != service_quota->previous_tx_index) {
  30626. + service_quota->previous_tx_index = tx_end_index;
  30627. + slot_use_count = ++service_quota->slot_use_count;
  30628. + } else {
  30629. + slot_use_count = 0;
  30630. + }
  30631. +
  30632. + spin_unlock(&quota_spinlock);
  30633. +
  30634. + if (slot_use_count)
  30635. + vchiq_log_trace(vchiq_core_log_level,
  30636. + "%d: qm:%d %s,%x - slot_use->%d (hdr %p)",
  30637. + state->id, service->localport,
  30638. + msg_type_str(VCHIQ_MSG_TYPE(msgid)), size,
  30639. + slot_use_count, header);
  30640. +
  30641. + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
  30642. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
  30643. + } else {
  30644. + vchiq_log_info(vchiq_core_log_level,
  30645. + "%d: qm %s@%x,%x (%d->%d)", state->id,
  30646. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  30647. + (unsigned int)header, size,
  30648. + VCHIQ_MSG_SRCPORT(msgid),
  30649. + VCHIQ_MSG_DSTPORT(msgid));
  30650. + if (size != 0) {
  30651. + WARN_ON(!((count == 1) && (size == elements[0].size)));
  30652. + memcpy(header->data, elements[0].data,
  30653. + elements[0].size);
  30654. + }
  30655. + VCHIQ_STATS_INC(state, ctrl_tx_count);
  30656. + }
  30657. +
  30658. + header->msgid = msgid;
  30659. + header->size = size;
  30660. +
  30661. + {
  30662. + int svc_fourcc;
  30663. +
  30664. + svc_fourcc = service
  30665. + ? service->base.fourcc
  30666. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  30667. +
  30668. + vchiq_log_info(SRVTRACE_LEVEL(service),
  30669. + "Sent Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
  30670. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  30671. + VCHIQ_MSG_TYPE(msgid),
  30672. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  30673. + VCHIQ_MSG_SRCPORT(msgid),
  30674. + VCHIQ_MSG_DSTPORT(msgid),
  30675. + size);
  30676. + }
  30677. +
  30678. + /* Make sure the new header is visible to the peer. */
  30679. + wmb();
  30680. +
  30681. + /* Make the new tx_pos visible to the peer. */
  30682. + local->tx_pos = state->local_tx_pos;
  30683. + wmb();
  30684. +
  30685. + if (service && (type == VCHIQ_MSG_CLOSE))
  30686. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSESENT);
  30687. +
  30688. + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
  30689. + mutex_unlock(&state->slot_mutex);
  30690. +
  30691. + remote_event_signal(&state->remote->trigger);
  30692. +
  30693. + return VCHIQ_SUCCESS;
  30694. +}
  30695. +
  30696. +/* Called by the slot handler and application threads */
  30697. +static VCHIQ_STATUS_T
  30698. +queue_message_sync(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  30699. + int msgid, const VCHIQ_ELEMENT_T *elements,
  30700. + int count, int size, int is_blocking)
  30701. +{
  30702. + VCHIQ_SHARED_STATE_T *local;
  30703. + VCHIQ_HEADER_T *header;
  30704. +
  30705. + local = state->local;
  30706. +
  30707. + if ((VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_RESUME) &&
  30708. + (mutex_lock_interruptible(&state->sync_mutex) != 0))
  30709. + return VCHIQ_RETRY;
  30710. +
  30711. + remote_event_wait(&local->sync_release);
  30712. +
  30713. + rmb();
  30714. +
  30715. + header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  30716. + local->slot_sync);
  30717. +
  30718. + {
  30719. + int oldmsgid = header->msgid;
  30720. + if (oldmsgid != VCHIQ_MSGID_PADDING)
  30721. + vchiq_log_error(vchiq_core_log_level,
  30722. + "%d: qms - msgid %x, not PADDING",
  30723. + state->id, oldmsgid);
  30724. + }
  30725. +
  30726. + if (service) {
  30727. + int i, pos;
  30728. +
  30729. + vchiq_log_info(vchiq_sync_log_level,
  30730. + "%d: qms %s@%x,%x (%d->%d)", state->id,
  30731. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  30732. + (unsigned int)header, size,
  30733. + VCHIQ_MSG_SRCPORT(msgid),
  30734. + VCHIQ_MSG_DSTPORT(msgid));
  30735. +
  30736. + for (i = 0, pos = 0; i < (unsigned int)count;
  30737. + pos += elements[i++].size)
  30738. + if (elements[i].size) {
  30739. + if (vchiq_copy_from_user
  30740. + (header->data + pos, elements[i].data,
  30741. + (size_t) elements[i].size) !=
  30742. + VCHIQ_SUCCESS) {
  30743. + mutex_unlock(&state->sync_mutex);
  30744. + VCHIQ_SERVICE_STATS_INC(service,
  30745. + error_count);
  30746. + return VCHIQ_ERROR;
  30747. + }
  30748. + if (i == 0) {
  30749. + if (vchiq_sync_log_level >=
  30750. + VCHIQ_LOG_TRACE)
  30751. + vchiq_log_dump_mem("Sent Sync",
  30752. + 0, header->data + pos,
  30753. + min(64u,
  30754. + elements[0].size));
  30755. + }
  30756. + }
  30757. +
  30758. + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
  30759. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
  30760. + } else {
  30761. + vchiq_log_info(vchiq_sync_log_level,
  30762. + "%d: qms %s@%x,%x (%d->%d)", state->id,
  30763. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  30764. + (unsigned int)header, size,
  30765. + VCHIQ_MSG_SRCPORT(msgid),
  30766. + VCHIQ_MSG_DSTPORT(msgid));
  30767. + if (size != 0) {
  30768. + WARN_ON(!((count == 1) && (size == elements[0].size)));
  30769. + memcpy(header->data, elements[0].data,
  30770. + elements[0].size);
  30771. + }
  30772. + VCHIQ_STATS_INC(state, ctrl_tx_count);
  30773. + }
  30774. +
  30775. + header->size = size;
  30776. + header->msgid = msgid;
  30777. +
  30778. + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
  30779. + int svc_fourcc;
  30780. +
  30781. + svc_fourcc = service
  30782. + ? service->base.fourcc
  30783. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  30784. +
  30785. + vchiq_log_trace(vchiq_sync_log_level,
  30786. + "Sent Sync Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
  30787. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  30788. + VCHIQ_MSG_TYPE(msgid),
  30789. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  30790. + VCHIQ_MSG_SRCPORT(msgid),
  30791. + VCHIQ_MSG_DSTPORT(msgid),
  30792. + size);
  30793. + }
  30794. +
  30795. + /* Make sure the new header is visible to the peer. */
  30796. + wmb();
  30797. +
  30798. + remote_event_signal(&state->remote->sync_trigger);
  30799. +
  30800. + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
  30801. + mutex_unlock(&state->sync_mutex);
  30802. +
  30803. + return VCHIQ_SUCCESS;
  30804. +}
  30805. +
  30806. +static inline void
  30807. +claim_slot(VCHIQ_SLOT_INFO_T *slot)
  30808. +{
  30809. + slot->use_count++;
  30810. +}
  30811. +
  30812. +static void
  30813. +release_slot(VCHIQ_STATE_T *state, VCHIQ_SLOT_INFO_T *slot_info,
  30814. + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_T *service)
  30815. +{
  30816. + int release_count;
  30817. +
  30818. + mutex_lock(&state->recycle_mutex);
  30819. +
  30820. + if (header) {
  30821. + int msgid = header->msgid;
  30822. + if (((msgid & VCHIQ_MSGID_CLAIMED) == 0) ||
  30823. + (service && service->closing)) {
  30824. + mutex_unlock(&state->recycle_mutex);
  30825. + return;
  30826. + }
  30827. +
  30828. + /* Rewrite the message header to prevent a double
  30829. + ** release */
  30830. + header->msgid = msgid & ~VCHIQ_MSGID_CLAIMED;
  30831. + }
  30832. +
  30833. + release_count = slot_info->release_count;
  30834. + slot_info->release_count = ++release_count;
  30835. +
  30836. + if (release_count == slot_info->use_count) {
  30837. + int slot_queue_recycle;
  30838. + /* Add to the freed queue */
  30839. +
  30840. + /* A read barrier is necessary here to prevent speculative
  30841. + ** fetches of remote->slot_queue_recycle from overtaking the
  30842. + ** mutex. */
  30843. + rmb();
  30844. +
  30845. + slot_queue_recycle = state->remote->slot_queue_recycle;
  30846. + state->remote->slot_queue[slot_queue_recycle &
  30847. + VCHIQ_SLOT_QUEUE_MASK] =
  30848. + SLOT_INDEX_FROM_INFO(state, slot_info);
  30849. + state->remote->slot_queue_recycle = slot_queue_recycle + 1;
  30850. + vchiq_log_info(vchiq_core_log_level,
  30851. + "%d: release_slot %d - recycle->%x",
  30852. + state->id, SLOT_INDEX_FROM_INFO(state, slot_info),
  30853. + state->remote->slot_queue_recycle);
  30854. +
  30855. + /* A write barrier is necessary, but remote_event_signal
  30856. + ** contains one. */
  30857. + remote_event_signal(&state->remote->recycle);
  30858. + }
  30859. +
  30860. + mutex_unlock(&state->recycle_mutex);
  30861. +}
  30862. +
  30863. +/* Called by the slot handler - don't hold the bulk mutex */
  30864. +static VCHIQ_STATUS_T
  30865. +notify_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue,
  30866. + int retry_poll)
  30867. +{
  30868. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  30869. +
  30870. + vchiq_log_trace(vchiq_core_log_level,
  30871. + "%d: nb:%d %cx - p=%x rn=%x r=%x",
  30872. + service->state->id, service->localport,
  30873. + (queue == &service->bulk_tx) ? 't' : 'r',
  30874. + queue->process, queue->remote_notify, queue->remove);
  30875. +
  30876. + if (service->state->is_master) {
  30877. + while (queue->remote_notify != queue->process) {
  30878. + VCHIQ_BULK_T *bulk =
  30879. + &queue->bulks[BULK_INDEX(queue->remote_notify)];
  30880. + int msgtype = (bulk->dir == VCHIQ_BULK_TRANSMIT) ?
  30881. + VCHIQ_MSG_BULK_RX_DONE : VCHIQ_MSG_BULK_TX_DONE;
  30882. + int msgid = VCHIQ_MAKE_MSG(msgtype, service->localport,
  30883. + service->remoteport);
  30884. + VCHIQ_ELEMENT_T element = { &bulk->actual, 4 };
  30885. + /* Only reply to non-dummy bulk requests */
  30886. + if (bulk->remote_data) {
  30887. + status = queue_message(service->state, NULL,
  30888. + msgid, &element, 1, 4, 0);
  30889. + if (status != VCHIQ_SUCCESS)
  30890. + break;
  30891. + }
  30892. + queue->remote_notify++;
  30893. + }
  30894. + } else {
  30895. + queue->remote_notify = queue->process;
  30896. + }
  30897. +
  30898. + if (status == VCHIQ_SUCCESS) {
  30899. + while (queue->remove != queue->remote_notify) {
  30900. + VCHIQ_BULK_T *bulk =
  30901. + &queue->bulks[BULK_INDEX(queue->remove)];
  30902. +
  30903. + /* Only generate callbacks for non-dummy bulk
  30904. + ** requests, and non-terminated services */
  30905. + if (bulk->data && service->instance) {
  30906. + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED) {
  30907. + if (bulk->dir == VCHIQ_BULK_TRANSMIT) {
  30908. + VCHIQ_SERVICE_STATS_INC(service,
  30909. + bulk_tx_count);
  30910. + VCHIQ_SERVICE_STATS_ADD(service,
  30911. + bulk_tx_bytes,
  30912. + bulk->actual);
  30913. + } else {
  30914. + VCHIQ_SERVICE_STATS_INC(service,
  30915. + bulk_rx_count);
  30916. + VCHIQ_SERVICE_STATS_ADD(service,
  30917. + bulk_rx_bytes,
  30918. + bulk->actual);
  30919. + }
  30920. + } else {
  30921. + VCHIQ_SERVICE_STATS_INC(service,
  30922. + bulk_aborted_count);
  30923. + }
  30924. + if (bulk->mode == VCHIQ_BULK_MODE_BLOCKING) {
  30925. + struct bulk_waiter *waiter;
  30926. + spin_lock(&bulk_waiter_spinlock);
  30927. + waiter = bulk->userdata;
  30928. + if (waiter) {
  30929. + waiter->actual = bulk->actual;
  30930. + up(&waiter->event);
  30931. + }
  30932. + spin_unlock(&bulk_waiter_spinlock);
  30933. + } else if (bulk->mode ==
  30934. + VCHIQ_BULK_MODE_CALLBACK) {
  30935. + VCHIQ_REASON_T reason = (bulk->dir ==
  30936. + VCHIQ_BULK_TRANSMIT) ?
  30937. + ((bulk->actual ==
  30938. + VCHIQ_BULK_ACTUAL_ABORTED) ?
  30939. + VCHIQ_BULK_TRANSMIT_ABORTED :
  30940. + VCHIQ_BULK_TRANSMIT_DONE) :
  30941. + ((bulk->actual ==
  30942. + VCHIQ_BULK_ACTUAL_ABORTED) ?
  30943. + VCHIQ_BULK_RECEIVE_ABORTED :
  30944. + VCHIQ_BULK_RECEIVE_DONE);
  30945. + status = make_service_callback(service,
  30946. + reason, NULL, bulk->userdata);
  30947. + if (status == VCHIQ_RETRY)
  30948. + break;
  30949. + }
  30950. + }
  30951. +
  30952. + queue->remove++;
  30953. + up(&service->bulk_remove_event);
  30954. + }
  30955. + if (!retry_poll)
  30956. + status = VCHIQ_SUCCESS;
  30957. + }
  30958. +
  30959. + if (status == VCHIQ_RETRY)
  30960. + request_poll(service->state, service,
  30961. + (queue == &service->bulk_tx) ?
  30962. + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
  30963. +
  30964. + return status;
  30965. +}
  30966. +
  30967. +/* Called by the slot handler thread */
  30968. +static void
  30969. +poll_services(VCHIQ_STATE_T *state)
  30970. +{
  30971. + int group, i;
  30972. +
  30973. + for (group = 0; group < BITSET_SIZE(state->unused_service); group++) {
  30974. + uint32_t flags;
  30975. + flags = atomic_xchg(&state->poll_services[group], 0);
  30976. + for (i = 0; flags; i++) {
  30977. + if (flags & (1 << i)) {
  30978. + VCHIQ_SERVICE_T *service =
  30979. + find_service_by_port(state,
  30980. + (group<<5) + i);
  30981. + uint32_t service_flags;
  30982. + flags &= ~(1 << i);
  30983. + if (!service)
  30984. + continue;
  30985. + service_flags =
  30986. + atomic_xchg(&service->poll_flags, 0);
  30987. + if (service_flags &
  30988. + (1 << VCHIQ_POLL_REMOVE)) {
  30989. + vchiq_log_info(vchiq_core_log_level,
  30990. + "%d: ps - remove %d<->%d",
  30991. + state->id, service->localport,
  30992. + service->remoteport);
  30993. +
  30994. + /* Make it look like a client, because
  30995. + it must be removed and not left in
  30996. + the LISTENING state. */
  30997. + service->public_fourcc =
  30998. + VCHIQ_FOURCC_INVALID;
  30999. +
  31000. + if (vchiq_close_service_internal(
  31001. + service, 0/*!close_recvd*/) !=
  31002. + VCHIQ_SUCCESS)
  31003. + request_poll(state, service,
  31004. + VCHIQ_POLL_REMOVE);
  31005. + } else if (service_flags &
  31006. + (1 << VCHIQ_POLL_TERMINATE)) {
  31007. + vchiq_log_info(vchiq_core_log_level,
  31008. + "%d: ps - terminate %d<->%d",
  31009. + state->id, service->localport,
  31010. + service->remoteport);
  31011. + if (vchiq_close_service_internal(
  31012. + service, 0/*!close_recvd*/) !=
  31013. + VCHIQ_SUCCESS)
  31014. + request_poll(state, service,
  31015. + VCHIQ_POLL_TERMINATE);
  31016. + }
  31017. + if (service_flags & (1 << VCHIQ_POLL_TXNOTIFY))
  31018. + notify_bulks(service,
  31019. + &service->bulk_tx,
  31020. + 1/*retry_poll*/);
  31021. + if (service_flags & (1 << VCHIQ_POLL_RXNOTIFY))
  31022. + notify_bulks(service,
  31023. + &service->bulk_rx,
  31024. + 1/*retry_poll*/);
  31025. + unlock_service(service);
  31026. + }
  31027. + }
  31028. + }
  31029. +}
  31030. +
  31031. +/* Called by the slot handler or application threads, holding the bulk mutex. */
  31032. +static int
  31033. +resolve_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
  31034. +{
  31035. + VCHIQ_STATE_T *state = service->state;
  31036. + int resolved = 0;
  31037. + int rc;
  31038. +
  31039. + while ((queue->process != queue->local_insert) &&
  31040. + (queue->process != queue->remote_insert)) {
  31041. + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
  31042. +
  31043. + vchiq_log_trace(vchiq_core_log_level,
  31044. + "%d: rb:%d %cx - li=%x ri=%x p=%x",
  31045. + state->id, service->localport,
  31046. + (queue == &service->bulk_tx) ? 't' : 'r',
  31047. + queue->local_insert, queue->remote_insert,
  31048. + queue->process);
  31049. +
  31050. + WARN_ON(!((int)(queue->local_insert - queue->process) > 0));
  31051. + WARN_ON(!((int)(queue->remote_insert - queue->process) > 0));
  31052. +
  31053. + rc = mutex_lock_interruptible(&state->bulk_transfer_mutex);
  31054. + if (rc != 0)
  31055. + break;
  31056. +
  31057. + vchiq_transfer_bulk(bulk);
  31058. + mutex_unlock(&state->bulk_transfer_mutex);
  31059. +
  31060. + if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
  31061. + const char *header = (queue == &service->bulk_tx) ?
  31062. + "Send Bulk to" : "Recv Bulk from";
  31063. + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED)
  31064. + vchiq_log_info(SRVTRACE_LEVEL(service),
  31065. + "%s %c%c%c%c d:%d len:%d %x<->%x",
  31066. + header,
  31067. + VCHIQ_FOURCC_AS_4CHARS(
  31068. + service->base.fourcc),
  31069. + service->remoteport,
  31070. + bulk->size,
  31071. + (unsigned int)bulk->data,
  31072. + (unsigned int)bulk->remote_data);
  31073. + else
  31074. + vchiq_log_info(SRVTRACE_LEVEL(service),
  31075. + "%s %c%c%c%c d:%d ABORTED - tx len:%d,"
  31076. + " rx len:%d %x<->%x",
  31077. + header,
  31078. + VCHIQ_FOURCC_AS_4CHARS(
  31079. + service->base.fourcc),
  31080. + service->remoteport,
  31081. + bulk->size,
  31082. + bulk->remote_size,
  31083. + (unsigned int)bulk->data,
  31084. + (unsigned int)bulk->remote_data);
  31085. + }
  31086. +
  31087. + vchiq_complete_bulk(bulk);
  31088. + queue->process++;
  31089. + resolved++;
  31090. + }
  31091. + return resolved;
  31092. +}
  31093. +
  31094. +/* Called with the bulk_mutex held */
  31095. +static void
  31096. +abort_outstanding_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
  31097. +{
  31098. + int is_tx = (queue == &service->bulk_tx);
  31099. + vchiq_log_trace(vchiq_core_log_level,
  31100. + "%d: aob:%d %cx - li=%x ri=%x p=%x",
  31101. + service->state->id, service->localport, is_tx ? 't' : 'r',
  31102. + queue->local_insert, queue->remote_insert, queue->process);
  31103. +
  31104. + WARN_ON(!((int)(queue->local_insert - queue->process) >= 0));
  31105. + WARN_ON(!((int)(queue->remote_insert - queue->process) >= 0));
  31106. +
  31107. + while ((queue->process != queue->local_insert) ||
  31108. + (queue->process != queue->remote_insert)) {
  31109. + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
  31110. +
  31111. + if (queue->process == queue->remote_insert) {
  31112. + /* fabricate a matching dummy bulk */
  31113. + bulk->remote_data = NULL;
  31114. + bulk->remote_size = 0;
  31115. + queue->remote_insert++;
  31116. + }
  31117. +
  31118. + if (queue->process != queue->local_insert) {
  31119. + vchiq_complete_bulk(bulk);
  31120. +
  31121. + vchiq_log_info(SRVTRACE_LEVEL(service),
  31122. + "%s %c%c%c%c d:%d ABORTED - tx len:%d, "
  31123. + "rx len:%d",
  31124. + is_tx ? "Send Bulk to" : "Recv Bulk from",
  31125. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  31126. + service->remoteport,
  31127. + bulk->size,
  31128. + bulk->remote_size);
  31129. + } else {
  31130. + /* fabricate a matching dummy bulk */
  31131. + bulk->data = NULL;
  31132. + bulk->size = 0;
  31133. + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
  31134. + bulk->dir = is_tx ? VCHIQ_BULK_TRANSMIT :
  31135. + VCHIQ_BULK_RECEIVE;
  31136. + queue->local_insert++;
  31137. + }
  31138. +
  31139. + queue->process++;
  31140. + }
  31141. +}
  31142. +
  31143. +/* Called from the slot handler thread */
  31144. +static void
  31145. +pause_bulks(VCHIQ_STATE_T *state)
  31146. +{
  31147. + if (unlikely(atomic_inc_return(&pause_bulks_count) != 1)) {
  31148. + WARN_ON_ONCE(1);
  31149. + atomic_set(&pause_bulks_count, 1);
  31150. + return;
  31151. + }
  31152. +
  31153. + /* Block bulk transfers from all services */
  31154. + mutex_lock(&state->bulk_transfer_mutex);
  31155. +}
  31156. +
  31157. +/* Called from the slot handler thread */
  31158. +static void
  31159. +resume_bulks(VCHIQ_STATE_T *state)
  31160. +{
  31161. + int i;
  31162. + if (unlikely(atomic_dec_return(&pause_bulks_count) != 0)) {
  31163. + WARN_ON_ONCE(1);
  31164. + atomic_set(&pause_bulks_count, 0);
  31165. + return;
  31166. + }
  31167. +
  31168. + /* Allow bulk transfers from all services */
  31169. + mutex_unlock(&state->bulk_transfer_mutex);
  31170. +
  31171. + if (state->deferred_bulks == 0)
  31172. + return;
  31173. +
  31174. + /* Deal with any bulks which had to be deferred due to being in
  31175. + * paused state. Don't try to match up to number of deferred bulks
  31176. + * in case we've had something come and close the service in the
  31177. + * interim - just process all bulk queues for all services */
  31178. + vchiq_log_info(vchiq_core_log_level, "%s: processing %d deferred bulks",
  31179. + __func__, state->deferred_bulks);
  31180. +
  31181. + for (i = 0; i < state->unused_service; i++) {
  31182. + VCHIQ_SERVICE_T *service = state->services[i];
  31183. + int resolved_rx = 0;
  31184. + int resolved_tx = 0;
  31185. + if (!service || (service->srvstate != VCHIQ_SRVSTATE_OPEN))
  31186. + continue;
  31187. +
  31188. + mutex_lock(&service->bulk_mutex);
  31189. + resolved_rx = resolve_bulks(service, &service->bulk_rx);
  31190. + resolved_tx = resolve_bulks(service, &service->bulk_tx);
  31191. + mutex_unlock(&service->bulk_mutex);
  31192. + if (resolved_rx)
  31193. + notify_bulks(service, &service->bulk_rx, 1);
  31194. + if (resolved_tx)
  31195. + notify_bulks(service, &service->bulk_tx, 1);
  31196. + }
  31197. + state->deferred_bulks = 0;
  31198. +}
  31199. +
  31200. +static int
  31201. +parse_open(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
  31202. +{
  31203. + VCHIQ_SERVICE_T *service = NULL;
  31204. + int msgid, size;
  31205. + int type;
  31206. + unsigned int localport, remoteport;
  31207. +
  31208. + msgid = header->msgid;
  31209. + size = header->size;
  31210. + type = VCHIQ_MSG_TYPE(msgid);
  31211. + localport = VCHIQ_MSG_DSTPORT(msgid);
  31212. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  31213. + if (size >= sizeof(struct vchiq_open_payload)) {
  31214. + const struct vchiq_open_payload *payload =
  31215. + (struct vchiq_open_payload *)header->data;
  31216. + unsigned int fourcc;
  31217. +
  31218. + fourcc = payload->fourcc;
  31219. + vchiq_log_info(vchiq_core_log_level,
  31220. + "%d: prs OPEN@%x (%d->'%c%c%c%c')",
  31221. + state->id, (unsigned int)header,
  31222. + localport,
  31223. + VCHIQ_FOURCC_AS_4CHARS(fourcc));
  31224. +
  31225. + service = get_listening_service(state, fourcc);
  31226. +
  31227. + if (service) {
  31228. + /* A matching service exists */
  31229. + short version = payload->version;
  31230. + short version_min = payload->version_min;
  31231. + if ((service->version < version_min) ||
  31232. + (version < service->version_min)) {
  31233. + /* Version mismatch */
  31234. + vchiq_loud_error_header();
  31235. + vchiq_loud_error("%d: service %d (%c%c%c%c) "
  31236. + "version mismatch - local (%d, min %d)"
  31237. + " vs. remote (%d, min %d)",
  31238. + state->id, service->localport,
  31239. + VCHIQ_FOURCC_AS_4CHARS(fourcc),
  31240. + service->version, service->version_min,
  31241. + version, version_min);
  31242. + vchiq_loud_error_footer();
  31243. + unlock_service(service);
  31244. + service = NULL;
  31245. + goto fail_open;
  31246. + }
  31247. + service->peer_version = version;
  31248. +
  31249. + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
  31250. + struct vchiq_openack_payload ack_payload = {
  31251. + service->version
  31252. + };
  31253. + VCHIQ_ELEMENT_T body = {
  31254. + &ack_payload,
  31255. + sizeof(ack_payload)
  31256. + };
  31257. +
  31258. + /* Acknowledge the OPEN */
  31259. + if (service->sync) {
  31260. + if (queue_message_sync(state, NULL,
  31261. + VCHIQ_MAKE_MSG(
  31262. + VCHIQ_MSG_OPENACK,
  31263. + service->localport,
  31264. + remoteport),
  31265. + &body, 1, sizeof(ack_payload),
  31266. + 0) == VCHIQ_RETRY)
  31267. + goto bail_not_ready;
  31268. + } else {
  31269. + if (queue_message(state, NULL,
  31270. + VCHIQ_MAKE_MSG(
  31271. + VCHIQ_MSG_OPENACK,
  31272. + service->localport,
  31273. + remoteport),
  31274. + &body, 1, sizeof(ack_payload),
  31275. + 0) == VCHIQ_RETRY)
  31276. + goto bail_not_ready;
  31277. + }
  31278. +
  31279. + /* The service is now open */
  31280. + vchiq_set_service_state(service,
  31281. + service->sync ? VCHIQ_SRVSTATE_OPENSYNC
  31282. + : VCHIQ_SRVSTATE_OPEN);
  31283. + }
  31284. +
  31285. + service->remoteport = remoteport;
  31286. + service->client_id = ((int *)header->data)[1];
  31287. + if (make_service_callback(service, VCHIQ_SERVICE_OPENED,
  31288. + NULL, NULL) == VCHIQ_RETRY) {
  31289. + /* Bail out if not ready */
  31290. + service->remoteport = VCHIQ_PORT_FREE;
  31291. + goto bail_not_ready;
  31292. + }
  31293. +
  31294. + /* Success - the message has been dealt with */
  31295. + unlock_service(service);
  31296. + return 1;
  31297. + }
  31298. + }
  31299. +
  31300. +fail_open:
  31301. + /* No available service, or an invalid request - send a CLOSE */
  31302. + if (queue_message(state, NULL,
  31303. + VCHIQ_MAKE_MSG(VCHIQ_MSG_CLOSE, 0, VCHIQ_MSG_SRCPORT(msgid)),
  31304. + NULL, 0, 0, 0) == VCHIQ_RETRY)
  31305. + goto bail_not_ready;
  31306. +
  31307. + return 1;
  31308. +
  31309. +bail_not_ready:
  31310. + if (service)
  31311. + unlock_service(service);
  31312. +
  31313. + return 0;
  31314. +}
  31315. +
  31316. +/* Called by the slot handler thread */
  31317. +static void
  31318. +parse_rx_slots(VCHIQ_STATE_T *state)
  31319. +{
  31320. + VCHIQ_SHARED_STATE_T *remote = state->remote;
  31321. + VCHIQ_SERVICE_T *service = NULL;
  31322. + int tx_pos;
  31323. + DEBUG_INITIALISE(state->local)
  31324. +
  31325. + tx_pos = remote->tx_pos;
  31326. +
  31327. + while (state->rx_pos != tx_pos) {
  31328. + VCHIQ_HEADER_T *header;
  31329. + int msgid, size;
  31330. + int type;
  31331. + unsigned int localport, remoteport;
  31332. +
  31333. + DEBUG_TRACE(PARSE_LINE);
  31334. + if (!state->rx_data) {
  31335. + int rx_index;
  31336. + WARN_ON(!((state->rx_pos & VCHIQ_SLOT_MASK) == 0));
  31337. + rx_index = remote->slot_queue[
  31338. + SLOT_QUEUE_INDEX_FROM_POS(state->rx_pos) &
  31339. + VCHIQ_SLOT_QUEUE_MASK];
  31340. + state->rx_data = (char *)SLOT_DATA_FROM_INDEX(state,
  31341. + rx_index);
  31342. + state->rx_info = SLOT_INFO_FROM_INDEX(state, rx_index);
  31343. +
  31344. + /* Initialise use_count to one, and increment
  31345. + ** release_count at the end of the slot to avoid
  31346. + ** releasing the slot prematurely. */
  31347. + state->rx_info->use_count = 1;
  31348. + state->rx_info->release_count = 0;
  31349. + }
  31350. +
  31351. + header = (VCHIQ_HEADER_T *)(state->rx_data +
  31352. + (state->rx_pos & VCHIQ_SLOT_MASK));
  31353. + DEBUG_VALUE(PARSE_HEADER, (int)header);
  31354. + msgid = header->msgid;
  31355. + DEBUG_VALUE(PARSE_MSGID, msgid);
  31356. + size = header->size;
  31357. + type = VCHIQ_MSG_TYPE(msgid);
  31358. + localport = VCHIQ_MSG_DSTPORT(msgid);
  31359. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  31360. +
  31361. + if (type != VCHIQ_MSG_DATA)
  31362. + VCHIQ_STATS_INC(state, ctrl_rx_count);
  31363. +
  31364. + switch (type) {
  31365. + case VCHIQ_MSG_OPENACK:
  31366. + case VCHIQ_MSG_CLOSE:
  31367. + case VCHIQ_MSG_DATA:
  31368. + case VCHIQ_MSG_BULK_RX:
  31369. + case VCHIQ_MSG_BULK_TX:
  31370. + case VCHIQ_MSG_BULK_RX_DONE:
  31371. + case VCHIQ_MSG_BULK_TX_DONE:
  31372. + service = find_service_by_port(state, localport);
  31373. + if ((!service ||
  31374. + ((service->remoteport != remoteport) &&
  31375. + (service->remoteport != VCHIQ_PORT_FREE))) &&
  31376. + (localport == 0) &&
  31377. + (type == VCHIQ_MSG_CLOSE)) {
  31378. + /* This could be a CLOSE from a client which
  31379. + hadn't yet received the OPENACK - look for
  31380. + the connected service */
  31381. + if (service)
  31382. + unlock_service(service);
  31383. + service = get_connected_service(state,
  31384. + remoteport);
  31385. + if (service)
  31386. + vchiq_log_warning(vchiq_core_log_level,
  31387. + "%d: prs %s@%x (%d->%d) - "
  31388. + "found connected service %d",
  31389. + state->id, msg_type_str(type),
  31390. + (unsigned int)header,
  31391. + remoteport, localport,
  31392. + service->localport);
  31393. + }
  31394. +
  31395. + if (!service) {
  31396. + vchiq_log_error(vchiq_core_log_level,
  31397. + "%d: prs %s@%x (%d->%d) - "
  31398. + "invalid/closed service %d",
  31399. + state->id, msg_type_str(type),
  31400. + (unsigned int)header,
  31401. + remoteport, localport, localport);
  31402. + goto skip_message;
  31403. + }
  31404. + break;
  31405. + default:
  31406. + break;
  31407. + }
  31408. +
  31409. + if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
  31410. + int svc_fourcc;
  31411. +
  31412. + svc_fourcc = service
  31413. + ? service->base.fourcc
  31414. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  31415. + vchiq_log_info(SRVTRACE_LEVEL(service),
  31416. + "Rcvd Msg %s(%u) from %c%c%c%c s:%d d:%d "
  31417. + "len:%d",
  31418. + msg_type_str(type), type,
  31419. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  31420. + remoteport, localport, size);
  31421. + if (size > 0)
  31422. + vchiq_log_dump_mem("Rcvd", 0, header->data,
  31423. + min(64, size));
  31424. + }
  31425. +
  31426. + if (((unsigned int)header & VCHIQ_SLOT_MASK) + calc_stride(size)
  31427. + > VCHIQ_SLOT_SIZE) {
  31428. + vchiq_log_error(vchiq_core_log_level,
  31429. + "header %x (msgid %x) - size %x too big for "
  31430. + "slot",
  31431. + (unsigned int)header, (unsigned int)msgid,
  31432. + (unsigned int)size);
  31433. + WARN(1, "oversized for slot\n");
  31434. + }
  31435. +
  31436. + switch (type) {
  31437. + case VCHIQ_MSG_OPEN:
  31438. + WARN_ON(!(VCHIQ_MSG_DSTPORT(msgid) == 0));
  31439. + if (!parse_open(state, header))
  31440. + goto bail_not_ready;
  31441. + break;
  31442. + case VCHIQ_MSG_OPENACK:
  31443. + if (size >= sizeof(struct vchiq_openack_payload)) {
  31444. + const struct vchiq_openack_payload *payload =
  31445. + (struct vchiq_openack_payload *)
  31446. + header->data;
  31447. + service->peer_version = payload->version;
  31448. + }
  31449. + vchiq_log_info(vchiq_core_log_level,
  31450. + "%d: prs OPENACK@%x,%x (%d->%d) v:%d",
  31451. + state->id, (unsigned int)header, size,
  31452. + remoteport, localport, service->peer_version);
  31453. + if (service->srvstate ==
  31454. + VCHIQ_SRVSTATE_OPENING) {
  31455. + service->remoteport = remoteport;
  31456. + vchiq_set_service_state(service,
  31457. + VCHIQ_SRVSTATE_OPEN);
  31458. + up(&service->remove_event);
  31459. + } else
  31460. + vchiq_log_error(vchiq_core_log_level,
  31461. + "OPENACK received in state %s",
  31462. + srvstate_names[service->srvstate]);
  31463. + break;
  31464. + case VCHIQ_MSG_CLOSE:
  31465. + WARN_ON(size != 0); /* There should be no data */
  31466. +
  31467. + vchiq_log_info(vchiq_core_log_level,
  31468. + "%d: prs CLOSE@%x (%d->%d)",
  31469. + state->id, (unsigned int)header,
  31470. + remoteport, localport);
  31471. +
  31472. + mark_service_closing_internal(service, 1);
  31473. +
  31474. + if (vchiq_close_service_internal(service,
  31475. + 1/*close_recvd*/) == VCHIQ_RETRY)
  31476. + goto bail_not_ready;
  31477. +
  31478. + vchiq_log_info(vchiq_core_log_level,
  31479. + "Close Service %c%c%c%c s:%u d:%d",
  31480. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  31481. + service->localport,
  31482. + service->remoteport);
  31483. + break;
  31484. + case VCHIQ_MSG_DATA:
  31485. + vchiq_log_trace(vchiq_core_log_level,
  31486. + "%d: prs DATA@%x,%x (%d->%d)",
  31487. + state->id, (unsigned int)header, size,
  31488. + remoteport, localport);
  31489. +
  31490. + if ((service->remoteport == remoteport)
  31491. + && (service->srvstate ==
  31492. + VCHIQ_SRVSTATE_OPEN)) {
  31493. + header->msgid = msgid | VCHIQ_MSGID_CLAIMED;
  31494. + claim_slot(state->rx_info);
  31495. + DEBUG_TRACE(PARSE_LINE);
  31496. + if (make_service_callback(service,
  31497. + VCHIQ_MESSAGE_AVAILABLE, header,
  31498. + NULL) == VCHIQ_RETRY) {
  31499. + DEBUG_TRACE(PARSE_LINE);
  31500. + goto bail_not_ready;
  31501. + }
  31502. + VCHIQ_SERVICE_STATS_INC(service, ctrl_rx_count);
  31503. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_rx_bytes,
  31504. + size);
  31505. + } else {
  31506. + VCHIQ_STATS_INC(state, error_count);
  31507. + }
  31508. + break;
  31509. + case VCHIQ_MSG_CONNECT:
  31510. + vchiq_log_info(vchiq_core_log_level,
  31511. + "%d: prs CONNECT@%x",
  31512. + state->id, (unsigned int)header);
  31513. + up(&state->connect);
  31514. + break;
  31515. + case VCHIQ_MSG_BULK_RX:
  31516. + case VCHIQ_MSG_BULK_TX: {
  31517. + VCHIQ_BULK_QUEUE_T *queue;
  31518. + WARN_ON(!state->is_master);
  31519. + queue = (type == VCHIQ_MSG_BULK_RX) ?
  31520. + &service->bulk_tx : &service->bulk_rx;
  31521. + if ((service->remoteport == remoteport)
  31522. + && (service->srvstate ==
  31523. + VCHIQ_SRVSTATE_OPEN)) {
  31524. + VCHIQ_BULK_T *bulk;
  31525. + int resolved = 0;
  31526. +
  31527. + DEBUG_TRACE(PARSE_LINE);
  31528. + if (mutex_lock_interruptible(
  31529. + &service->bulk_mutex) != 0) {
  31530. + DEBUG_TRACE(PARSE_LINE);
  31531. + goto bail_not_ready;
  31532. + }
  31533. +
  31534. + WARN_ON(!(queue->remote_insert < queue->remove +
  31535. + VCHIQ_NUM_SERVICE_BULKS));
  31536. + bulk = &queue->bulks[
  31537. + BULK_INDEX(queue->remote_insert)];
  31538. + bulk->remote_data =
  31539. + (void *)((int *)header->data)[0];
  31540. + bulk->remote_size = ((int *)header->data)[1];
  31541. + wmb();
  31542. +
  31543. + vchiq_log_info(vchiq_core_log_level,
  31544. + "%d: prs %s@%x (%d->%d) %x@%x",
  31545. + state->id, msg_type_str(type),
  31546. + (unsigned int)header,
  31547. + remoteport, localport,
  31548. + bulk->remote_size,
  31549. + (unsigned int)bulk->remote_data);
  31550. +
  31551. + queue->remote_insert++;
  31552. +
  31553. + if (atomic_read(&pause_bulks_count)) {
  31554. + state->deferred_bulks++;
  31555. + vchiq_log_info(vchiq_core_log_level,
  31556. + "%s: deferring bulk (%d)",
  31557. + __func__,
  31558. + state->deferred_bulks);
  31559. + if (state->conn_state !=
  31560. + VCHIQ_CONNSTATE_PAUSE_SENT)
  31561. + vchiq_log_error(
  31562. + vchiq_core_log_level,
  31563. + "%s: bulks paused in "
  31564. + "unexpected state %s",
  31565. + __func__,
  31566. + conn_state_names[
  31567. + state->conn_state]);
  31568. + } else if (state->conn_state ==
  31569. + VCHIQ_CONNSTATE_CONNECTED) {
  31570. + DEBUG_TRACE(PARSE_LINE);
  31571. + resolved = resolve_bulks(service,
  31572. + queue);
  31573. + }
  31574. +
  31575. + mutex_unlock(&service->bulk_mutex);
  31576. + if (resolved)
  31577. + notify_bulks(service, queue,
  31578. + 1/*retry_poll*/);
  31579. + }
  31580. + } break;
  31581. + case VCHIQ_MSG_BULK_RX_DONE:
  31582. + case VCHIQ_MSG_BULK_TX_DONE:
  31583. + WARN_ON(state->is_master);
  31584. + if ((service->remoteport == remoteport)
  31585. + && (service->srvstate !=
  31586. + VCHIQ_SRVSTATE_FREE)) {
  31587. + VCHIQ_BULK_QUEUE_T *queue;
  31588. + VCHIQ_BULK_T *bulk;
  31589. +
  31590. + queue = (type == VCHIQ_MSG_BULK_RX_DONE) ?
  31591. + &service->bulk_rx : &service->bulk_tx;
  31592. +
  31593. + DEBUG_TRACE(PARSE_LINE);
  31594. + if (mutex_lock_interruptible(
  31595. + &service->bulk_mutex) != 0) {
  31596. + DEBUG_TRACE(PARSE_LINE);
  31597. + goto bail_not_ready;
  31598. + }
  31599. + if ((int)(queue->remote_insert -
  31600. + queue->local_insert) >= 0) {
  31601. + vchiq_log_error(vchiq_core_log_level,
  31602. + "%d: prs %s@%x (%d->%d) "
  31603. + "unexpected (ri=%d,li=%d)",
  31604. + state->id, msg_type_str(type),
  31605. + (unsigned int)header,
  31606. + remoteport, localport,
  31607. + queue->remote_insert,
  31608. + queue->local_insert);
  31609. + mutex_unlock(&service->bulk_mutex);
  31610. + break;
  31611. + }
  31612. +
  31613. + BUG_ON(queue->process == queue->local_insert);
  31614. + BUG_ON(queue->process != queue->remote_insert);
  31615. +
  31616. + bulk = &queue->bulks[
  31617. + BULK_INDEX(queue->remote_insert)];
  31618. + bulk->actual = *(int *)header->data;
  31619. + queue->remote_insert++;
  31620. +
  31621. + vchiq_log_info(vchiq_core_log_level,
  31622. + "%d: prs %s@%x (%d->%d) %x@%x",
  31623. + state->id, msg_type_str(type),
  31624. + (unsigned int)header,
  31625. + remoteport, localport,
  31626. + bulk->actual, (unsigned int)bulk->data);
  31627. +
  31628. + vchiq_log_trace(vchiq_core_log_level,
  31629. + "%d: prs:%d %cx li=%x ri=%x p=%x",
  31630. + state->id, localport,
  31631. + (type == VCHIQ_MSG_BULK_RX_DONE) ?
  31632. + 'r' : 't',
  31633. + queue->local_insert,
  31634. + queue->remote_insert, queue->process);
  31635. +
  31636. + DEBUG_TRACE(PARSE_LINE);
  31637. + WARN_ON(queue->process == queue->local_insert);
  31638. + vchiq_complete_bulk(bulk);
  31639. + queue->process++;
  31640. + mutex_unlock(&service->bulk_mutex);
  31641. + DEBUG_TRACE(PARSE_LINE);
  31642. + notify_bulks(service, queue, 1/*retry_poll*/);
  31643. + DEBUG_TRACE(PARSE_LINE);
  31644. + }
  31645. + break;
  31646. + case VCHIQ_MSG_PADDING:
  31647. + vchiq_log_trace(vchiq_core_log_level,
  31648. + "%d: prs PADDING@%x,%x",
  31649. + state->id, (unsigned int)header, size);
  31650. + break;
  31651. + case VCHIQ_MSG_PAUSE:
  31652. + /* If initiated, signal the application thread */
  31653. + vchiq_log_trace(vchiq_core_log_level,
  31654. + "%d: prs PAUSE@%x,%x",
  31655. + state->id, (unsigned int)header, size);
  31656. + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
  31657. + vchiq_log_error(vchiq_core_log_level,
  31658. + "%d: PAUSE received in state PAUSED",
  31659. + state->id);
  31660. + break;
  31661. + }
  31662. + if (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT) {
  31663. + /* Send a PAUSE in response */
  31664. + if (queue_message(state, NULL,
  31665. + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
  31666. + NULL, 0, 0, 0) == VCHIQ_RETRY)
  31667. + goto bail_not_ready;
  31668. + if (state->is_master)
  31669. + pause_bulks(state);
  31670. + }
  31671. + /* At this point slot_mutex is held */
  31672. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSED);
  31673. + vchiq_platform_paused(state);
  31674. + break;
  31675. + case VCHIQ_MSG_RESUME:
  31676. + vchiq_log_trace(vchiq_core_log_level,
  31677. + "%d: prs RESUME@%x,%x",
  31678. + state->id, (unsigned int)header, size);
  31679. + /* Release the slot mutex */
  31680. + mutex_unlock(&state->slot_mutex);
  31681. + if (state->is_master)
  31682. + resume_bulks(state);
  31683. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
  31684. + vchiq_platform_resumed(state);
  31685. + break;
  31686. +
  31687. + case VCHIQ_MSG_REMOTE_USE:
  31688. + vchiq_on_remote_use(state);
  31689. + break;
  31690. + case VCHIQ_MSG_REMOTE_RELEASE:
  31691. + vchiq_on_remote_release(state);
  31692. + break;
  31693. + case VCHIQ_MSG_REMOTE_USE_ACTIVE:
  31694. + vchiq_on_remote_use_active(state);
  31695. + break;
  31696. +
  31697. + default:
  31698. + vchiq_log_error(vchiq_core_log_level,
  31699. + "%d: prs invalid msgid %x@%x,%x",
  31700. + state->id, msgid, (unsigned int)header, size);
  31701. + WARN(1, "invalid message\n");
  31702. + break;
  31703. + }
  31704. +
  31705. +skip_message:
  31706. + if (service) {
  31707. + unlock_service(service);
  31708. + service = NULL;
  31709. + }
  31710. +
  31711. + state->rx_pos += calc_stride(size);
  31712. +
  31713. + DEBUG_TRACE(PARSE_LINE);
  31714. + /* Perform some housekeeping when the end of the slot is
  31715. + ** reached. */
  31716. + if ((state->rx_pos & VCHIQ_SLOT_MASK) == 0) {
  31717. + /* Remove the extra reference count. */
  31718. + release_slot(state, state->rx_info, NULL, NULL);
  31719. + state->rx_data = NULL;
  31720. + }
  31721. + }
  31722. +
  31723. +bail_not_ready:
  31724. + if (service)
  31725. + unlock_service(service);
  31726. +}
  31727. +
  31728. +/* Called by the slot handler thread */
  31729. +static int
  31730. +slot_handler_func(void *v)
  31731. +{
  31732. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  31733. + VCHIQ_SHARED_STATE_T *local = state->local;
  31734. + DEBUG_INITIALISE(local)
  31735. +
  31736. + while (1) {
  31737. + DEBUG_COUNT(SLOT_HANDLER_COUNT);
  31738. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  31739. + remote_event_wait(&local->trigger);
  31740. +
  31741. + rmb();
  31742. +
  31743. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  31744. + if (state->poll_needed) {
  31745. + /* Check if we need to suspend - may change our
  31746. + * conn_state */
  31747. + vchiq_platform_check_suspend(state);
  31748. +
  31749. + state->poll_needed = 0;
  31750. +
  31751. + /* Handle service polling and other rare conditions here
  31752. + ** out of the mainline code */
  31753. + switch (state->conn_state) {
  31754. + case VCHIQ_CONNSTATE_CONNECTED:
  31755. + /* Poll the services as requested */
  31756. + poll_services(state);
  31757. + break;
  31758. +
  31759. + case VCHIQ_CONNSTATE_PAUSING:
  31760. + if (state->is_master)
  31761. + pause_bulks(state);
  31762. + if (queue_message(state, NULL,
  31763. + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
  31764. + NULL, 0, 0, 0) != VCHIQ_RETRY) {
  31765. + vchiq_set_conn_state(state,
  31766. + VCHIQ_CONNSTATE_PAUSE_SENT);
  31767. + } else {
  31768. + if (state->is_master)
  31769. + resume_bulks(state);
  31770. + /* Retry later */
  31771. + state->poll_needed = 1;
  31772. + }
  31773. + break;
  31774. +
  31775. + case VCHIQ_CONNSTATE_PAUSED:
  31776. + vchiq_platform_resume(state);
  31777. + break;
  31778. +
  31779. + case VCHIQ_CONNSTATE_RESUMING:
  31780. + if (queue_message(state, NULL,
  31781. + VCHIQ_MAKE_MSG(VCHIQ_MSG_RESUME, 0, 0),
  31782. + NULL, 0, 0, 0) != VCHIQ_RETRY) {
  31783. + if (state->is_master)
  31784. + resume_bulks(state);
  31785. + vchiq_set_conn_state(state,
  31786. + VCHIQ_CONNSTATE_CONNECTED);
  31787. + vchiq_platform_resumed(state);
  31788. + } else {
  31789. + /* This should really be impossible,
  31790. + ** since the PAUSE should have flushed
  31791. + ** through outstanding messages. */
  31792. + vchiq_log_error(vchiq_core_log_level,
  31793. + "Failed to send RESUME "
  31794. + "message");
  31795. + BUG();
  31796. + }
  31797. + break;
  31798. +
  31799. + case VCHIQ_CONNSTATE_PAUSE_TIMEOUT:
  31800. + case VCHIQ_CONNSTATE_RESUME_TIMEOUT:
  31801. + vchiq_platform_handle_timeout(state);
  31802. + break;
  31803. + default:
  31804. + break;
  31805. + }
  31806. +
  31807. +
  31808. + }
  31809. +
  31810. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  31811. + parse_rx_slots(state);
  31812. + }
  31813. + return 0;
  31814. +}
  31815. +
  31816. +
  31817. +/* Called by the recycle thread */
  31818. +static int
  31819. +recycle_func(void *v)
  31820. +{
  31821. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  31822. + VCHIQ_SHARED_STATE_T *local = state->local;
  31823. +
  31824. + while (1) {
  31825. + remote_event_wait(&local->recycle);
  31826. +
  31827. + process_free_queue(state);
  31828. + }
  31829. + return 0;
  31830. +}
  31831. +
  31832. +
  31833. +/* Called by the sync thread */
  31834. +static int
  31835. +sync_func(void *v)
  31836. +{
  31837. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  31838. + VCHIQ_SHARED_STATE_T *local = state->local;
  31839. + VCHIQ_HEADER_T *header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  31840. + state->remote->slot_sync);
  31841. +
  31842. + while (1) {
  31843. + VCHIQ_SERVICE_T *service;
  31844. + int msgid, size;
  31845. + int type;
  31846. + unsigned int localport, remoteport;
  31847. +
  31848. + remote_event_wait(&local->sync_trigger);
  31849. +
  31850. + rmb();
  31851. +
  31852. + msgid = header->msgid;
  31853. + size = header->size;
  31854. + type = VCHIQ_MSG_TYPE(msgid);
  31855. + localport = VCHIQ_MSG_DSTPORT(msgid);
  31856. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  31857. +
  31858. + service = find_service_by_port(state, localport);
  31859. +
  31860. + if (!service) {
  31861. + vchiq_log_error(vchiq_sync_log_level,
  31862. + "%d: sf %s@%x (%d->%d) - "
  31863. + "invalid/closed service %d",
  31864. + state->id, msg_type_str(type),
  31865. + (unsigned int)header,
  31866. + remoteport, localport, localport);
  31867. + release_message_sync(state, header);
  31868. + continue;
  31869. + }
  31870. +
  31871. + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
  31872. + int svc_fourcc;
  31873. +
  31874. + svc_fourcc = service
  31875. + ? service->base.fourcc
  31876. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  31877. + vchiq_log_trace(vchiq_sync_log_level,
  31878. + "Rcvd Msg %s from %c%c%c%c s:%d d:%d len:%d",
  31879. + msg_type_str(type),
  31880. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  31881. + remoteport, localport, size);
  31882. + if (size > 0)
  31883. + vchiq_log_dump_mem("Rcvd", 0, header->data,
  31884. + min(64, size));
  31885. + }
  31886. +
  31887. + switch (type) {
  31888. + case VCHIQ_MSG_OPENACK:
  31889. + if (size >= sizeof(struct vchiq_openack_payload)) {
  31890. + const struct vchiq_openack_payload *payload =
  31891. + (struct vchiq_openack_payload *)
  31892. + header->data;
  31893. + service->peer_version = payload->version;
  31894. + }
  31895. + vchiq_log_info(vchiq_sync_log_level,
  31896. + "%d: sf OPENACK@%x,%x (%d->%d) v:%d",
  31897. + state->id, (unsigned int)header, size,
  31898. + remoteport, localport, service->peer_version);
  31899. + if (service->srvstate == VCHIQ_SRVSTATE_OPENING) {
  31900. + service->remoteport = remoteport;
  31901. + vchiq_set_service_state(service,
  31902. + VCHIQ_SRVSTATE_OPENSYNC);
  31903. + up(&service->remove_event);
  31904. + }
  31905. + release_message_sync(state, header);
  31906. + break;
  31907. +
  31908. + case VCHIQ_MSG_DATA:
  31909. + vchiq_log_trace(vchiq_sync_log_level,
  31910. + "%d: sf DATA@%x,%x (%d->%d)",
  31911. + state->id, (unsigned int)header, size,
  31912. + remoteport, localport);
  31913. +
  31914. + if ((service->remoteport == remoteport) &&
  31915. + (service->srvstate ==
  31916. + VCHIQ_SRVSTATE_OPENSYNC)) {
  31917. + if (make_service_callback(service,
  31918. + VCHIQ_MESSAGE_AVAILABLE, header,
  31919. + NULL) == VCHIQ_RETRY)
  31920. + vchiq_log_error(vchiq_sync_log_level,
  31921. + "synchronous callback to "
  31922. + "service %d returns "
  31923. + "VCHIQ_RETRY",
  31924. + localport);
  31925. + }
  31926. + break;
  31927. +
  31928. + default:
  31929. + vchiq_log_error(vchiq_sync_log_level,
  31930. + "%d: sf unexpected msgid %x@%x,%x",
  31931. + state->id, msgid, (unsigned int)header, size);
  31932. + release_message_sync(state, header);
  31933. + break;
  31934. + }
  31935. +
  31936. + unlock_service(service);
  31937. + }
  31938. +
  31939. + return 0;
  31940. +}
  31941. +
  31942. +
  31943. +static void
  31944. +init_bulk_queue(VCHIQ_BULK_QUEUE_T *queue)
  31945. +{
  31946. + queue->local_insert = 0;
  31947. + queue->remote_insert = 0;
  31948. + queue->process = 0;
  31949. + queue->remote_notify = 0;
  31950. + queue->remove = 0;
  31951. +}
  31952. +
  31953. +
  31954. +inline const char *
  31955. +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state)
  31956. +{
  31957. + return conn_state_names[conn_state];
  31958. +}
  31959. +
  31960. +
  31961. +VCHIQ_SLOT_ZERO_T *
  31962. +vchiq_init_slots(void *mem_base, int mem_size)
  31963. +{
  31964. + int mem_align = (VCHIQ_SLOT_SIZE - (int)mem_base) & VCHIQ_SLOT_MASK;
  31965. + VCHIQ_SLOT_ZERO_T *slot_zero =
  31966. + (VCHIQ_SLOT_ZERO_T *)((char *)mem_base + mem_align);
  31967. + int num_slots = (mem_size - mem_align)/VCHIQ_SLOT_SIZE;
  31968. + int first_data_slot = VCHIQ_SLOT_ZERO_SLOTS;
  31969. +
  31970. + /* Ensure there is enough memory to run an absolutely minimum system */
  31971. + num_slots -= first_data_slot;
  31972. +
  31973. + if (num_slots < 4) {
  31974. + vchiq_log_error(vchiq_core_log_level,
  31975. + "vchiq_init_slots - insufficient memory %x bytes",
  31976. + mem_size);
  31977. + return NULL;
  31978. + }
  31979. +
  31980. + memset(slot_zero, 0, sizeof(VCHIQ_SLOT_ZERO_T));
  31981. +
  31982. + slot_zero->magic = VCHIQ_MAGIC;
  31983. + slot_zero->version = VCHIQ_VERSION;
  31984. + slot_zero->version_min = VCHIQ_VERSION_MIN;
  31985. + slot_zero->slot_zero_size = sizeof(VCHIQ_SLOT_ZERO_T);
  31986. + slot_zero->slot_size = VCHIQ_SLOT_SIZE;
  31987. + slot_zero->max_slots = VCHIQ_MAX_SLOTS;
  31988. + slot_zero->max_slots_per_side = VCHIQ_MAX_SLOTS_PER_SIDE;
  31989. +
  31990. + slot_zero->master.slot_sync = first_data_slot;
  31991. + slot_zero->master.slot_first = first_data_slot + 1;
  31992. + slot_zero->master.slot_last = first_data_slot + (num_slots/2) - 1;
  31993. + slot_zero->slave.slot_sync = first_data_slot + (num_slots/2);
  31994. + slot_zero->slave.slot_first = first_data_slot + (num_slots/2) + 1;
  31995. + slot_zero->slave.slot_last = first_data_slot + num_slots - 1;
  31996. +
  31997. + return slot_zero;
  31998. +}
  31999. +
  32000. +VCHIQ_STATUS_T
  32001. +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
  32002. + int is_master)
  32003. +{
  32004. + VCHIQ_SHARED_STATE_T *local;
  32005. + VCHIQ_SHARED_STATE_T *remote;
  32006. + VCHIQ_STATUS_T status;
  32007. + char threadname[10];
  32008. + static int id;
  32009. + int i;
  32010. +
  32011. + vchiq_log_warning(vchiq_core_log_level,
  32012. + "%s: slot_zero = 0x%08lx, is_master = %d",
  32013. + __func__, (unsigned long)slot_zero, is_master);
  32014. +
  32015. + /* Check the input configuration */
  32016. +
  32017. + if (slot_zero->magic != VCHIQ_MAGIC) {
  32018. + vchiq_loud_error_header();
  32019. + vchiq_loud_error("Invalid VCHIQ magic value found.");
  32020. + vchiq_loud_error("slot_zero=%x: magic=%x (expected %x)",
  32021. + (unsigned int)slot_zero, slot_zero->magic, VCHIQ_MAGIC);
  32022. + vchiq_loud_error_footer();
  32023. + return VCHIQ_ERROR;
  32024. + }
  32025. +
  32026. + if (slot_zero->version < VCHIQ_VERSION_MIN) {
  32027. + vchiq_loud_error_header();
  32028. + vchiq_loud_error("Incompatible VCHIQ versions found.");
  32029. + vchiq_loud_error("slot_zero=%x: VideoCore version=%d "
  32030. + "(minimum %d)",
  32031. + (unsigned int)slot_zero, slot_zero->version,
  32032. + VCHIQ_VERSION_MIN);
  32033. + vchiq_loud_error("Restart with a newer VideoCore image.");
  32034. + vchiq_loud_error_footer();
  32035. + return VCHIQ_ERROR;
  32036. + }
  32037. +
  32038. + if (VCHIQ_VERSION < slot_zero->version_min) {
  32039. + vchiq_loud_error_header();
  32040. + vchiq_loud_error("Incompatible VCHIQ versions found.");
  32041. + vchiq_loud_error("slot_zero=%x: version=%d (VideoCore "
  32042. + "minimum %d)",
  32043. + (unsigned int)slot_zero, VCHIQ_VERSION,
  32044. + slot_zero->version_min);
  32045. + vchiq_loud_error("Restart with a newer kernel.");
  32046. + vchiq_loud_error_footer();
  32047. + return VCHIQ_ERROR;
  32048. + }
  32049. +
  32050. + if ((slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T)) ||
  32051. + (slot_zero->slot_size != VCHIQ_SLOT_SIZE) ||
  32052. + (slot_zero->max_slots != VCHIQ_MAX_SLOTS) ||
  32053. + (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)) {
  32054. + vchiq_loud_error_header();
  32055. + if (slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T))
  32056. + vchiq_loud_error("slot_zero=%x: slot_zero_size=%x "
  32057. + "(expected %x)",
  32058. + (unsigned int)slot_zero,
  32059. + slot_zero->slot_zero_size,
  32060. + sizeof(VCHIQ_SLOT_ZERO_T));
  32061. + if (slot_zero->slot_size != VCHIQ_SLOT_SIZE)
  32062. + vchiq_loud_error("slot_zero=%x: slot_size=%d "
  32063. + "(expected %d",
  32064. + (unsigned int)slot_zero, slot_zero->slot_size,
  32065. + VCHIQ_SLOT_SIZE);
  32066. + if (slot_zero->max_slots != VCHIQ_MAX_SLOTS)
  32067. + vchiq_loud_error("slot_zero=%x: max_slots=%d "
  32068. + "(expected %d)",
  32069. + (unsigned int)slot_zero, slot_zero->max_slots,
  32070. + VCHIQ_MAX_SLOTS);
  32071. + if (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)
  32072. + vchiq_loud_error("slot_zero=%x: max_slots_per_side=%d "
  32073. + "(expected %d)",
  32074. + (unsigned int)slot_zero,
  32075. + slot_zero->max_slots_per_side,
  32076. + VCHIQ_MAX_SLOTS_PER_SIDE);
  32077. + vchiq_loud_error_footer();
  32078. + return VCHIQ_ERROR;
  32079. + }
  32080. +
  32081. + if (is_master) {
  32082. + local = &slot_zero->master;
  32083. + remote = &slot_zero->slave;
  32084. + } else {
  32085. + local = &slot_zero->slave;
  32086. + remote = &slot_zero->master;
  32087. + }
  32088. +
  32089. + if (local->initialised) {
  32090. + vchiq_loud_error_header();
  32091. + if (remote->initialised)
  32092. + vchiq_loud_error("local state has already been "
  32093. + "initialised");
  32094. + else
  32095. + vchiq_loud_error("master/slave mismatch - two %ss",
  32096. + is_master ? "master" : "slave");
  32097. + vchiq_loud_error_footer();
  32098. + return VCHIQ_ERROR;
  32099. + }
  32100. +
  32101. + memset(state, 0, sizeof(VCHIQ_STATE_T));
  32102. +
  32103. + state->id = id++;
  32104. + state->is_master = is_master;
  32105. +
  32106. + /*
  32107. + initialize shared state pointers
  32108. + */
  32109. +
  32110. + state->local = local;
  32111. + state->remote = remote;
  32112. + state->slot_data = (VCHIQ_SLOT_T *)slot_zero;
  32113. +
  32114. + /*
  32115. + initialize events and mutexes
  32116. + */
  32117. +
  32118. + sema_init(&state->connect, 0);
  32119. + mutex_init(&state->mutex);
  32120. + sema_init(&state->trigger_event, 0);
  32121. + sema_init(&state->recycle_event, 0);
  32122. + sema_init(&state->sync_trigger_event, 0);
  32123. + sema_init(&state->sync_release_event, 0);
  32124. +
  32125. + mutex_init(&state->slot_mutex);
  32126. + mutex_init(&state->recycle_mutex);
  32127. + mutex_init(&state->sync_mutex);
  32128. + mutex_init(&state->bulk_transfer_mutex);
  32129. +
  32130. + sema_init(&state->slot_available_event, 0);
  32131. + sema_init(&state->slot_remove_event, 0);
  32132. + sema_init(&state->data_quota_event, 0);
  32133. +
  32134. + state->slot_queue_available = 0;
  32135. +
  32136. + for (i = 0; i < VCHIQ_MAX_SERVICES; i++) {
  32137. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  32138. + &state->service_quotas[i];
  32139. + sema_init(&service_quota->quota_event, 0);
  32140. + }
  32141. +
  32142. + for (i = local->slot_first; i <= local->slot_last; i++) {
  32143. + local->slot_queue[state->slot_queue_available++] = i;
  32144. + up(&state->slot_available_event);
  32145. + }
  32146. +
  32147. + state->default_slot_quota = state->slot_queue_available/2;
  32148. + state->default_message_quota =
  32149. + min((unsigned short)(state->default_slot_quota * 256),
  32150. + (unsigned short)~0);
  32151. +
  32152. + state->previous_data_index = -1;
  32153. + state->data_use_count = 0;
  32154. + state->data_quota = state->slot_queue_available - 1;
  32155. +
  32156. + local->trigger.event = &state->trigger_event;
  32157. + remote_event_create(&local->trigger);
  32158. + local->tx_pos = 0;
  32159. +
  32160. + local->recycle.event = &state->recycle_event;
  32161. + remote_event_create(&local->recycle);
  32162. + local->slot_queue_recycle = state->slot_queue_available;
  32163. +
  32164. + local->sync_trigger.event = &state->sync_trigger_event;
  32165. + remote_event_create(&local->sync_trigger);
  32166. +
  32167. + local->sync_release.event = &state->sync_release_event;
  32168. + remote_event_create(&local->sync_release);
  32169. +
  32170. + /* At start-of-day, the slot is empty and available */
  32171. + ((VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state, local->slot_sync))->msgid
  32172. + = VCHIQ_MSGID_PADDING;
  32173. + remote_event_signal_local(&local->sync_release);
  32174. +
  32175. + local->debug[DEBUG_ENTRIES] = DEBUG_MAX;
  32176. +
  32177. + status = vchiq_platform_init_state(state);
  32178. +
  32179. + /*
  32180. + bring up slot handler thread
  32181. + */
  32182. + snprintf(threadname, sizeof(threadname), "VCHIQ-%d", state->id);
  32183. + state->slot_handler_thread = kthread_create(&slot_handler_func,
  32184. + (void *)state,
  32185. + threadname);
  32186. +
  32187. + if (state->slot_handler_thread == NULL) {
  32188. + vchiq_loud_error_header();
  32189. + vchiq_loud_error("couldn't create thread %s", threadname);
  32190. + vchiq_loud_error_footer();
  32191. + return VCHIQ_ERROR;
  32192. + }
  32193. + set_user_nice(state->slot_handler_thread, -19);
  32194. + wake_up_process(state->slot_handler_thread);
  32195. +
  32196. + snprintf(threadname, sizeof(threadname), "VCHIQr-%d", state->id);
  32197. + state->recycle_thread = kthread_create(&recycle_func,
  32198. + (void *)state,
  32199. + threadname);
  32200. + if (state->recycle_thread == NULL) {
  32201. + vchiq_loud_error_header();
  32202. + vchiq_loud_error("couldn't create thread %s", threadname);
  32203. + vchiq_loud_error_footer();
  32204. + return VCHIQ_ERROR;
  32205. + }
  32206. + set_user_nice(state->recycle_thread, -19);
  32207. + wake_up_process(state->recycle_thread);
  32208. +
  32209. + snprintf(threadname, sizeof(threadname), "VCHIQs-%d", state->id);
  32210. + state->sync_thread = kthread_create(&sync_func,
  32211. + (void *)state,
  32212. + threadname);
  32213. + if (state->sync_thread == NULL) {
  32214. + vchiq_loud_error_header();
  32215. + vchiq_loud_error("couldn't create thread %s", threadname);
  32216. + vchiq_loud_error_footer();
  32217. + return VCHIQ_ERROR;
  32218. + }
  32219. + set_user_nice(state->sync_thread, -20);
  32220. + wake_up_process(state->sync_thread);
  32221. +
  32222. + BUG_ON(state->id >= VCHIQ_MAX_STATES);
  32223. + vchiq_states[state->id] = state;
  32224. +
  32225. + /* Indicate readiness to the other side */
  32226. + local->initialised = 1;
  32227. +
  32228. + return status;
  32229. +}
  32230. +
  32231. +/* Called from application thread when a client or server service is created. */
  32232. +VCHIQ_SERVICE_T *
  32233. +vchiq_add_service_internal(VCHIQ_STATE_T *state,
  32234. + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
  32235. + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term)
  32236. +{
  32237. + VCHIQ_SERVICE_T *service;
  32238. +
  32239. + service = kmalloc(sizeof(VCHIQ_SERVICE_T), GFP_KERNEL);
  32240. + if (service) {
  32241. + service->base.fourcc = params->fourcc;
  32242. + service->base.callback = params->callback;
  32243. + service->base.userdata = params->userdata;
  32244. + service->handle = VCHIQ_SERVICE_HANDLE_INVALID;
  32245. + service->ref_count = 1;
  32246. + service->srvstate = VCHIQ_SRVSTATE_FREE;
  32247. + service->userdata_term = userdata_term;
  32248. + service->localport = VCHIQ_PORT_FREE;
  32249. + service->remoteport = VCHIQ_PORT_FREE;
  32250. +
  32251. + service->public_fourcc = (srvstate == VCHIQ_SRVSTATE_OPENING) ?
  32252. + VCHIQ_FOURCC_INVALID : params->fourcc;
  32253. + service->client_id = 0;
  32254. + service->auto_close = 1;
  32255. + service->sync = 0;
  32256. + service->closing = 0;
  32257. + service->trace = 0;
  32258. + atomic_set(&service->poll_flags, 0);
  32259. + service->version = params->version;
  32260. + service->version_min = params->version_min;
  32261. + service->state = state;
  32262. + service->instance = instance;
  32263. + service->service_use_count = 0;
  32264. + init_bulk_queue(&service->bulk_tx);
  32265. + init_bulk_queue(&service->bulk_rx);
  32266. + sema_init(&service->remove_event, 0);
  32267. + sema_init(&service->bulk_remove_event, 0);
  32268. + mutex_init(&service->bulk_mutex);
  32269. + memset(&service->stats, 0, sizeof(service->stats));
  32270. + } else {
  32271. + vchiq_log_error(vchiq_core_log_level,
  32272. + "Out of memory");
  32273. + }
  32274. +
  32275. + if (service) {
  32276. + VCHIQ_SERVICE_T **pservice = NULL;
  32277. + int i;
  32278. +
  32279. + /* Although it is perfectly possible to use service_spinlock
  32280. + ** to protect the creation of services, it is overkill as it
  32281. + ** disables interrupts while the array is searched.
  32282. + ** The only danger is of another thread trying to create a
  32283. + ** service - service deletion is safe.
  32284. + ** Therefore it is preferable to use state->mutex which,
  32285. + ** although slower to claim, doesn't block interrupts while
  32286. + ** it is held.
  32287. + */
  32288. +
  32289. + mutex_lock(&state->mutex);
  32290. +
  32291. + /* Prepare to use a previously unused service */
  32292. + if (state->unused_service < VCHIQ_MAX_SERVICES)
  32293. + pservice = &state->services[state->unused_service];
  32294. +
  32295. + if (srvstate == VCHIQ_SRVSTATE_OPENING) {
  32296. + for (i = 0; i < state->unused_service; i++) {
  32297. + VCHIQ_SERVICE_T *srv = state->services[i];
  32298. + if (!srv) {
  32299. + pservice = &state->services[i];
  32300. + break;
  32301. + }
  32302. + }
  32303. + } else {
  32304. + for (i = (state->unused_service - 1); i >= 0; i--) {
  32305. + VCHIQ_SERVICE_T *srv = state->services[i];
  32306. + if (!srv)
  32307. + pservice = &state->services[i];
  32308. + else if ((srv->public_fourcc == params->fourcc)
  32309. + && ((srv->instance != instance) ||
  32310. + (srv->base.callback !=
  32311. + params->callback))) {
  32312. + /* There is another server using this
  32313. + ** fourcc which doesn't match. */
  32314. + pservice = NULL;
  32315. + break;
  32316. + }
  32317. + }
  32318. + }
  32319. +
  32320. + if (pservice) {
  32321. + service->localport = (pservice - state->services);
  32322. + if (!handle_seq)
  32323. + handle_seq = VCHIQ_MAX_STATES *
  32324. + VCHIQ_MAX_SERVICES;
  32325. + service->handle = handle_seq |
  32326. + (state->id * VCHIQ_MAX_SERVICES) |
  32327. + service->localport;
  32328. + handle_seq += VCHIQ_MAX_STATES * VCHIQ_MAX_SERVICES;
  32329. + *pservice = service;
  32330. + if (pservice == &state->services[state->unused_service])
  32331. + state->unused_service++;
  32332. + }
  32333. +
  32334. + mutex_unlock(&state->mutex);
  32335. +
  32336. + if (!pservice) {
  32337. + kfree(service);
  32338. + service = NULL;
  32339. + }
  32340. + }
  32341. +
  32342. + if (service) {
  32343. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  32344. + &state->service_quotas[service->localport];
  32345. + service_quota->slot_quota = state->default_slot_quota;
  32346. + service_quota->message_quota = state->default_message_quota;
  32347. + if (service_quota->slot_use_count == 0)
  32348. + service_quota->previous_tx_index =
  32349. + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos)
  32350. + - 1;
  32351. +
  32352. + /* Bring this service online */
  32353. + vchiq_set_service_state(service, srvstate);
  32354. +
  32355. + vchiq_log_info(vchiq_core_msg_log_level,
  32356. + "%s Service %c%c%c%c SrcPort:%d",
  32357. + (srvstate == VCHIQ_SRVSTATE_OPENING)
  32358. + ? "Open" : "Add",
  32359. + VCHIQ_FOURCC_AS_4CHARS(params->fourcc),
  32360. + service->localport);
  32361. + }
  32362. +
  32363. + /* Don't unlock the service - leave it with a ref_count of 1. */
  32364. +
  32365. + return service;
  32366. +}
  32367. +
  32368. +VCHIQ_STATUS_T
  32369. +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id)
  32370. +{
  32371. + struct vchiq_open_payload payload = {
  32372. + service->base.fourcc,
  32373. + client_id,
  32374. + service->version,
  32375. + service->version_min
  32376. + };
  32377. + VCHIQ_ELEMENT_T body = { &payload, sizeof(payload) };
  32378. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  32379. +
  32380. + service->client_id = client_id;
  32381. + vchiq_use_service_internal(service);
  32382. + status = queue_message(service->state, NULL,
  32383. + VCHIQ_MAKE_MSG(VCHIQ_MSG_OPEN, service->localport, 0),
  32384. + &body, 1, sizeof(payload), 1);
  32385. + if (status == VCHIQ_SUCCESS) {
  32386. + /* Wait for the ACK/NAK */
  32387. + if (down_interruptible(&service->remove_event) != 0) {
  32388. + status = VCHIQ_RETRY;
  32389. + vchiq_release_service_internal(service);
  32390. + } else if ((service->srvstate != VCHIQ_SRVSTATE_OPEN) &&
  32391. + (service->srvstate != VCHIQ_SRVSTATE_OPENSYNC)) {
  32392. + if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT)
  32393. + vchiq_log_error(vchiq_core_log_level,
  32394. + "%d: osi - srvstate = %s (ref %d)",
  32395. + service->state->id,
  32396. + srvstate_names[service->srvstate],
  32397. + service->ref_count);
  32398. + status = VCHIQ_ERROR;
  32399. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  32400. + vchiq_release_service_internal(service);
  32401. + }
  32402. + }
  32403. + return status;
  32404. +}
  32405. +
  32406. +static void
  32407. +release_service_messages(VCHIQ_SERVICE_T *service)
  32408. +{
  32409. + VCHIQ_STATE_T *state = service->state;
  32410. + int slot_last = state->remote->slot_last;
  32411. + int i;
  32412. +
  32413. + /* Release any claimed messages */
  32414. + for (i = state->remote->slot_first; i <= slot_last; i++) {
  32415. + VCHIQ_SLOT_INFO_T *slot_info =
  32416. + SLOT_INFO_FROM_INDEX(state, i);
  32417. + if (slot_info->release_count != slot_info->use_count) {
  32418. + char *data =
  32419. + (char *)SLOT_DATA_FROM_INDEX(state, i);
  32420. + unsigned int pos, end;
  32421. +
  32422. + end = VCHIQ_SLOT_SIZE;
  32423. + if (data == state->rx_data)
  32424. + /* This buffer is still being read from - stop
  32425. + ** at the current read position */
  32426. + end = state->rx_pos & VCHIQ_SLOT_MASK;
  32427. +
  32428. + pos = 0;
  32429. +
  32430. + while (pos < end) {
  32431. + VCHIQ_HEADER_T *header =
  32432. + (VCHIQ_HEADER_T *)(data + pos);
  32433. + int msgid = header->msgid;
  32434. + int port = VCHIQ_MSG_DSTPORT(msgid);
  32435. + if ((port == service->localport) &&
  32436. + (msgid & VCHIQ_MSGID_CLAIMED)) {
  32437. + vchiq_log_info(vchiq_core_log_level,
  32438. + " fsi - hdr %x",
  32439. + (unsigned int)header);
  32440. + release_slot(state, slot_info, header,
  32441. + NULL);
  32442. + }
  32443. + pos += calc_stride(header->size);
  32444. + if (pos > VCHIQ_SLOT_SIZE) {
  32445. + vchiq_log_error(vchiq_core_log_level,
  32446. + "fsi - pos %x: header %x, "
  32447. + "msgid %x, header->msgid %x, "
  32448. + "header->size %x",
  32449. + pos, (unsigned int)header,
  32450. + msgid, header->msgid,
  32451. + header->size);
  32452. + WARN(1, "invalid slot position\n");
  32453. + }
  32454. + }
  32455. + }
  32456. + }
  32457. +}
  32458. +
  32459. +static int
  32460. +do_abort_bulks(VCHIQ_SERVICE_T *service)
  32461. +{
  32462. + VCHIQ_STATUS_T status;
  32463. +
  32464. + /* Abort any outstanding bulk transfers */
  32465. + if (mutex_lock_interruptible(&service->bulk_mutex) != 0)
  32466. + return 0;
  32467. + abort_outstanding_bulks(service, &service->bulk_tx);
  32468. + abort_outstanding_bulks(service, &service->bulk_rx);
  32469. + mutex_unlock(&service->bulk_mutex);
  32470. +
  32471. + status = notify_bulks(service, &service->bulk_tx, 0/*!retry_poll*/);
  32472. + if (status == VCHIQ_SUCCESS)
  32473. + status = notify_bulks(service, &service->bulk_rx,
  32474. + 0/*!retry_poll*/);
  32475. + return (status == VCHIQ_SUCCESS);
  32476. +}
  32477. +
  32478. +static VCHIQ_STATUS_T
  32479. +close_service_complete(VCHIQ_SERVICE_T *service, int failstate)
  32480. +{
  32481. + VCHIQ_STATUS_T status;
  32482. + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
  32483. + int newstate;
  32484. +
  32485. + switch (service->srvstate) {
  32486. + case VCHIQ_SRVSTATE_OPEN:
  32487. + case VCHIQ_SRVSTATE_CLOSESENT:
  32488. + case VCHIQ_SRVSTATE_CLOSERECVD:
  32489. + if (is_server) {
  32490. + if (service->auto_close) {
  32491. + service->client_id = 0;
  32492. + service->remoteport = VCHIQ_PORT_FREE;
  32493. + newstate = VCHIQ_SRVSTATE_LISTENING;
  32494. + } else
  32495. + newstate = VCHIQ_SRVSTATE_CLOSEWAIT;
  32496. + } else
  32497. + newstate = VCHIQ_SRVSTATE_CLOSED;
  32498. + vchiq_set_service_state(service, newstate);
  32499. + break;
  32500. + case VCHIQ_SRVSTATE_LISTENING:
  32501. + break;
  32502. + default:
  32503. + vchiq_log_error(vchiq_core_log_level,
  32504. + "close_service_complete(%x) called in state %s",
  32505. + service->handle, srvstate_names[service->srvstate]);
  32506. + WARN(1, "close_service_complete in unexpected state\n");
  32507. + return VCHIQ_ERROR;
  32508. + }
  32509. +
  32510. + status = make_service_callback(service,
  32511. + VCHIQ_SERVICE_CLOSED, NULL, NULL);
  32512. +
  32513. + if (status != VCHIQ_RETRY) {
  32514. + int uc = service->service_use_count;
  32515. + int i;
  32516. + /* Complete the close process */
  32517. + for (i = 0; i < uc; i++)
  32518. + /* cater for cases where close is forced and the
  32519. + ** client may not close all it's handles */
  32520. + vchiq_release_service_internal(service);
  32521. +
  32522. + service->client_id = 0;
  32523. + service->remoteport = VCHIQ_PORT_FREE;
  32524. +
  32525. + if (service->srvstate == VCHIQ_SRVSTATE_CLOSED)
  32526. + vchiq_free_service_internal(service);
  32527. + else if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT) {
  32528. + if (is_server)
  32529. + service->closing = 0;
  32530. +
  32531. + up(&service->remove_event);
  32532. + }
  32533. + } else
  32534. + vchiq_set_service_state(service, failstate);
  32535. +
  32536. + return status;
  32537. +}
  32538. +
  32539. +/* Called by the slot handler */
  32540. +VCHIQ_STATUS_T
  32541. +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd)
  32542. +{
  32543. + VCHIQ_STATE_T *state = service->state;
  32544. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  32545. + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
  32546. +
  32547. + vchiq_log_info(vchiq_core_log_level, "%d: csi:%d,%d (%s)",
  32548. + service->state->id, service->localport, close_recvd,
  32549. + srvstate_names[service->srvstate]);
  32550. +
  32551. + switch (service->srvstate) {
  32552. + case VCHIQ_SRVSTATE_CLOSED:
  32553. + case VCHIQ_SRVSTATE_HIDDEN:
  32554. + case VCHIQ_SRVSTATE_LISTENING:
  32555. + case VCHIQ_SRVSTATE_CLOSEWAIT:
  32556. + if (close_recvd)
  32557. + vchiq_log_error(vchiq_core_log_level,
  32558. + "vchiq_close_service_internal(1) called "
  32559. + "in state %s",
  32560. + srvstate_names[service->srvstate]);
  32561. + else if (is_server) {
  32562. + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
  32563. + status = VCHIQ_ERROR;
  32564. + } else {
  32565. + service->client_id = 0;
  32566. + service->remoteport = VCHIQ_PORT_FREE;
  32567. + if (service->srvstate ==
  32568. + VCHIQ_SRVSTATE_CLOSEWAIT)
  32569. + vchiq_set_service_state(service,
  32570. + VCHIQ_SRVSTATE_LISTENING);
  32571. + }
  32572. + up(&service->remove_event);
  32573. + } else
  32574. + vchiq_free_service_internal(service);
  32575. + break;
  32576. + case VCHIQ_SRVSTATE_OPENING:
  32577. + if (close_recvd) {
  32578. + /* The open was rejected - tell the user */
  32579. + vchiq_set_service_state(service,
  32580. + VCHIQ_SRVSTATE_CLOSEWAIT);
  32581. + up(&service->remove_event);
  32582. + } else {
  32583. + /* Shutdown mid-open - let the other side know */
  32584. + status = queue_message(state, service,
  32585. + VCHIQ_MAKE_MSG
  32586. + (VCHIQ_MSG_CLOSE,
  32587. + service->localport,
  32588. + VCHIQ_MSG_DSTPORT(service->remoteport)),
  32589. + NULL, 0, 0, 0);
  32590. + }
  32591. + break;
  32592. +
  32593. + case VCHIQ_SRVSTATE_OPENSYNC:
  32594. + mutex_lock(&state->sync_mutex);
  32595. + /* Drop through */
  32596. +
  32597. + case VCHIQ_SRVSTATE_OPEN:
  32598. + if (state->is_master || close_recvd) {
  32599. + if (!do_abort_bulks(service))
  32600. + status = VCHIQ_RETRY;
  32601. + }
  32602. +
  32603. + release_service_messages(service);
  32604. +
  32605. + if (status == VCHIQ_SUCCESS)
  32606. + status = queue_message(state, service,
  32607. + VCHIQ_MAKE_MSG
  32608. + (VCHIQ_MSG_CLOSE,
  32609. + service->localport,
  32610. + VCHIQ_MSG_DSTPORT(service->remoteport)),
  32611. + NULL, 0, 0, 0);
  32612. +
  32613. + if (status == VCHIQ_SUCCESS) {
  32614. + if (!close_recvd)
  32615. + break;
  32616. + } else if (service->srvstate == VCHIQ_SRVSTATE_OPENSYNC) {
  32617. + mutex_unlock(&state->sync_mutex);
  32618. + break;
  32619. + } else
  32620. + break;
  32621. +
  32622. + status = close_service_complete(service,
  32623. + VCHIQ_SRVSTATE_CLOSERECVD);
  32624. + break;
  32625. +
  32626. + case VCHIQ_SRVSTATE_CLOSESENT:
  32627. + if (!close_recvd)
  32628. + /* This happens when a process is killed mid-close */
  32629. + break;
  32630. +
  32631. + if (!state->is_master) {
  32632. + if (!do_abort_bulks(service)) {
  32633. + status = VCHIQ_RETRY;
  32634. + break;
  32635. + }
  32636. + }
  32637. +
  32638. + if (status == VCHIQ_SUCCESS)
  32639. + status = close_service_complete(service,
  32640. + VCHIQ_SRVSTATE_CLOSERECVD);
  32641. + break;
  32642. +
  32643. + case VCHIQ_SRVSTATE_CLOSERECVD:
  32644. + if (!close_recvd && is_server)
  32645. + /* Force into LISTENING mode */
  32646. + vchiq_set_service_state(service,
  32647. + VCHIQ_SRVSTATE_LISTENING);
  32648. + status = close_service_complete(service,
  32649. + VCHIQ_SRVSTATE_CLOSERECVD);
  32650. + break;
  32651. +
  32652. + default:
  32653. + vchiq_log_error(vchiq_core_log_level,
  32654. + "vchiq_close_service_internal(%d) called in state %s",
  32655. + close_recvd, srvstate_names[service->srvstate]);
  32656. + break;
  32657. + }
  32658. +
  32659. + return status;
  32660. +}
  32661. +
  32662. +/* Called from the application process upon process death */
  32663. +void
  32664. +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service)
  32665. +{
  32666. + VCHIQ_STATE_T *state = service->state;
  32667. +
  32668. + vchiq_log_info(vchiq_core_log_level, "%d: tsi - (%d<->%d)",
  32669. + state->id, service->localport, service->remoteport);
  32670. +
  32671. + mark_service_closing(service);
  32672. +
  32673. + /* Mark the service for removal by the slot handler */
  32674. + request_poll(state, service, VCHIQ_POLL_REMOVE);
  32675. +}
  32676. +
  32677. +/* Called from the slot handler */
  32678. +void
  32679. +vchiq_free_service_internal(VCHIQ_SERVICE_T *service)
  32680. +{
  32681. + VCHIQ_STATE_T *state = service->state;
  32682. +
  32683. + vchiq_log_info(vchiq_core_log_level, "%d: fsi - (%d)",
  32684. + state->id, service->localport);
  32685. +
  32686. + switch (service->srvstate) {
  32687. + case VCHIQ_SRVSTATE_OPENING:
  32688. + case VCHIQ_SRVSTATE_CLOSED:
  32689. + case VCHIQ_SRVSTATE_HIDDEN:
  32690. + case VCHIQ_SRVSTATE_LISTENING:
  32691. + case VCHIQ_SRVSTATE_CLOSEWAIT:
  32692. + break;
  32693. + default:
  32694. + vchiq_log_error(vchiq_core_log_level,
  32695. + "%d: fsi - (%d) in state %s",
  32696. + state->id, service->localport,
  32697. + srvstate_names[service->srvstate]);
  32698. + return;
  32699. + }
  32700. +
  32701. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_FREE);
  32702. +
  32703. + up(&service->remove_event);
  32704. +
  32705. + /* Release the initial lock */
  32706. + unlock_service(service);
  32707. +}
  32708. +
  32709. +VCHIQ_STATUS_T
  32710. +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
  32711. +{
  32712. + VCHIQ_SERVICE_T *service;
  32713. + int i;
  32714. +
  32715. + /* Find all services registered to this client and enable them. */
  32716. + i = 0;
  32717. + while ((service = next_service_by_instance(state, instance,
  32718. + &i)) != NULL) {
  32719. + if (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)
  32720. + vchiq_set_service_state(service,
  32721. + VCHIQ_SRVSTATE_LISTENING);
  32722. + unlock_service(service);
  32723. + }
  32724. +
  32725. + if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED) {
  32726. + if (queue_message(state, NULL,
  32727. + VCHIQ_MAKE_MSG(VCHIQ_MSG_CONNECT, 0, 0), NULL, 0,
  32728. + 0, 1) == VCHIQ_RETRY)
  32729. + return VCHIQ_RETRY;
  32730. +
  32731. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTING);
  32732. + }
  32733. +
  32734. + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTING) {
  32735. + if (down_interruptible(&state->connect) != 0)
  32736. + return VCHIQ_RETRY;
  32737. +
  32738. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
  32739. + up(&state->connect);
  32740. + }
  32741. +
  32742. + return VCHIQ_SUCCESS;
  32743. +}
  32744. +
  32745. +VCHIQ_STATUS_T
  32746. +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
  32747. +{
  32748. + VCHIQ_SERVICE_T *service;
  32749. + int i;
  32750. +
  32751. + /* Find all services registered to this client and enable them. */
  32752. + i = 0;
  32753. + while ((service = next_service_by_instance(state, instance,
  32754. + &i)) != NULL) {
  32755. + (void)vchiq_remove_service(service->handle);
  32756. + unlock_service(service);
  32757. + }
  32758. +
  32759. + return VCHIQ_SUCCESS;
  32760. +}
  32761. +
  32762. +VCHIQ_STATUS_T
  32763. +vchiq_pause_internal(VCHIQ_STATE_T *state)
  32764. +{
  32765. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  32766. +
  32767. + switch (state->conn_state) {
  32768. + case VCHIQ_CONNSTATE_CONNECTED:
  32769. + /* Request a pause */
  32770. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSING);
  32771. + request_poll(state, NULL, 0);
  32772. + break;
  32773. + default:
  32774. + vchiq_log_error(vchiq_core_log_level,
  32775. + "vchiq_pause_internal in state %s\n",
  32776. + conn_state_names[state->conn_state]);
  32777. + status = VCHIQ_ERROR;
  32778. + VCHIQ_STATS_INC(state, error_count);
  32779. + break;
  32780. + }
  32781. +
  32782. + return status;
  32783. +}
  32784. +
  32785. +VCHIQ_STATUS_T
  32786. +vchiq_resume_internal(VCHIQ_STATE_T *state)
  32787. +{
  32788. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  32789. +
  32790. + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
  32791. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_RESUMING);
  32792. + request_poll(state, NULL, 0);
  32793. + } else {
  32794. + status = VCHIQ_ERROR;
  32795. + VCHIQ_STATS_INC(state, error_count);
  32796. + }
  32797. +
  32798. + return status;
  32799. +}
  32800. +
  32801. +VCHIQ_STATUS_T
  32802. +vchiq_close_service(VCHIQ_SERVICE_HANDLE_T handle)
  32803. +{
  32804. + /* Unregister the service */
  32805. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  32806. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  32807. +
  32808. + if (!service)
  32809. + return VCHIQ_ERROR;
  32810. +
  32811. + vchiq_log_info(vchiq_core_log_level,
  32812. + "%d: close_service:%d",
  32813. + service->state->id, service->localport);
  32814. +
  32815. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  32816. + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  32817. + (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)) {
  32818. + unlock_service(service);
  32819. + return VCHIQ_ERROR;
  32820. + }
  32821. +
  32822. + mark_service_closing(service);
  32823. +
  32824. + if (current == service->state->slot_handler_thread) {
  32825. + status = vchiq_close_service_internal(service,
  32826. + 0/*!close_recvd*/);
  32827. + BUG_ON(status == VCHIQ_RETRY);
  32828. + } else {
  32829. + /* Mark the service for termination by the slot handler */
  32830. + request_poll(service->state, service, VCHIQ_POLL_TERMINATE);
  32831. + }
  32832. +
  32833. + while (1) {
  32834. + if (down_interruptible(&service->remove_event) != 0) {
  32835. + status = VCHIQ_RETRY;
  32836. + break;
  32837. + }
  32838. +
  32839. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  32840. + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  32841. + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
  32842. + break;
  32843. +
  32844. + vchiq_log_warning(vchiq_core_log_level,
  32845. + "%d: close_service:%d - waiting in state %s",
  32846. + service->state->id, service->localport,
  32847. + srvstate_names[service->srvstate]);
  32848. + }
  32849. +
  32850. + if ((status == VCHIQ_SUCCESS) &&
  32851. + (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  32852. + (service->srvstate != VCHIQ_SRVSTATE_LISTENING))
  32853. + status = VCHIQ_ERROR;
  32854. +
  32855. + unlock_service(service);
  32856. +
  32857. + return status;
  32858. +}
  32859. +
  32860. +VCHIQ_STATUS_T
  32861. +vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T handle)
  32862. +{
  32863. + /* Unregister the service */
  32864. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  32865. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  32866. +
  32867. + if (!service)
  32868. + return VCHIQ_ERROR;
  32869. +
  32870. + vchiq_log_info(vchiq_core_log_level,
  32871. + "%d: remove_service:%d",
  32872. + service->state->id, service->localport);
  32873. +
  32874. + if (service->srvstate == VCHIQ_SRVSTATE_FREE) {
  32875. + unlock_service(service);
  32876. + return VCHIQ_ERROR;
  32877. + }
  32878. +
  32879. + mark_service_closing(service);
  32880. +
  32881. + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
  32882. + (current == service->state->slot_handler_thread)) {
  32883. + /* Make it look like a client, because it must be removed and
  32884. + not left in the LISTENING state. */
  32885. + service->public_fourcc = VCHIQ_FOURCC_INVALID;
  32886. +
  32887. + status = vchiq_close_service_internal(service,
  32888. + 0/*!close_recvd*/);
  32889. + BUG_ON(status == VCHIQ_RETRY);
  32890. + } else {
  32891. + /* Mark the service for removal by the slot handler */
  32892. + request_poll(service->state, service, VCHIQ_POLL_REMOVE);
  32893. + }
  32894. + while (1) {
  32895. + if (down_interruptible(&service->remove_event) != 0) {
  32896. + status = VCHIQ_RETRY;
  32897. + break;
  32898. + }
  32899. +
  32900. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  32901. + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
  32902. + break;
  32903. +
  32904. + vchiq_log_warning(vchiq_core_log_level,
  32905. + "%d: remove_service:%d - waiting in state %s",
  32906. + service->state->id, service->localport,
  32907. + srvstate_names[service->srvstate]);
  32908. + }
  32909. +
  32910. + if ((status == VCHIQ_SUCCESS) &&
  32911. + (service->srvstate != VCHIQ_SRVSTATE_FREE))
  32912. + status = VCHIQ_ERROR;
  32913. +
  32914. + unlock_service(service);
  32915. +
  32916. + return status;
  32917. +}
  32918. +
  32919. +
  32920. +/* This function may be called by kernel threads or user threads.
  32921. + * User threads may receive VCHIQ_RETRY to indicate that a signal has been
  32922. + * received and the call should be retried after being returned to user
  32923. + * context.
  32924. + * When called in blocking mode, the userdata field points to a bulk_waiter
  32925. + * structure.
  32926. + */
  32927. +VCHIQ_STATUS_T
  32928. +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
  32929. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
  32930. + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir)
  32931. +{
  32932. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  32933. + VCHIQ_BULK_QUEUE_T *queue;
  32934. + VCHIQ_BULK_T *bulk;
  32935. + VCHIQ_STATE_T *state;
  32936. + struct bulk_waiter *bulk_waiter = NULL;
  32937. + const char dir_char = (dir == VCHIQ_BULK_TRANSMIT) ? 't' : 'r';
  32938. + const int dir_msgtype = (dir == VCHIQ_BULK_TRANSMIT) ?
  32939. + VCHIQ_MSG_BULK_TX : VCHIQ_MSG_BULK_RX;
  32940. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  32941. +
  32942. + if (!service ||
  32943. + (service->srvstate != VCHIQ_SRVSTATE_OPEN) ||
  32944. + ((memhandle == VCHI_MEM_HANDLE_INVALID) && (offset == NULL)) ||
  32945. + (vchiq_check_service(service) != VCHIQ_SUCCESS))
  32946. + goto error_exit;
  32947. +
  32948. + switch (mode) {
  32949. + case VCHIQ_BULK_MODE_NOCALLBACK:
  32950. + case VCHIQ_BULK_MODE_CALLBACK:
  32951. + break;
  32952. + case VCHIQ_BULK_MODE_BLOCKING:
  32953. + bulk_waiter = (struct bulk_waiter *)userdata;
  32954. + sema_init(&bulk_waiter->event, 0);
  32955. + bulk_waiter->actual = 0;
  32956. + bulk_waiter->bulk = NULL;
  32957. + break;
  32958. + case VCHIQ_BULK_MODE_WAITING:
  32959. + bulk_waiter = (struct bulk_waiter *)userdata;
  32960. + bulk = bulk_waiter->bulk;
  32961. + goto waiting;
  32962. + default:
  32963. + goto error_exit;
  32964. + }
  32965. +
  32966. + state = service->state;
  32967. +
  32968. + queue = (dir == VCHIQ_BULK_TRANSMIT) ?
  32969. + &service->bulk_tx : &service->bulk_rx;
  32970. +
  32971. + if (mutex_lock_interruptible(&service->bulk_mutex) != 0) {
  32972. + status = VCHIQ_RETRY;
  32973. + goto error_exit;
  32974. + }
  32975. +
  32976. + if (queue->local_insert == queue->remove + VCHIQ_NUM_SERVICE_BULKS) {
  32977. + VCHIQ_SERVICE_STATS_INC(service, bulk_stalls);
  32978. + do {
  32979. + mutex_unlock(&service->bulk_mutex);
  32980. + if (down_interruptible(&service->bulk_remove_event)
  32981. + != 0) {
  32982. + status = VCHIQ_RETRY;
  32983. + goto error_exit;
  32984. + }
  32985. + if (mutex_lock_interruptible(&service->bulk_mutex)
  32986. + != 0) {
  32987. + status = VCHIQ_RETRY;
  32988. + goto error_exit;
  32989. + }
  32990. + } while (queue->local_insert == queue->remove +
  32991. + VCHIQ_NUM_SERVICE_BULKS);
  32992. + }
  32993. +
  32994. + bulk = &queue->bulks[BULK_INDEX(queue->local_insert)];
  32995. +
  32996. + bulk->mode = mode;
  32997. + bulk->dir = dir;
  32998. + bulk->userdata = userdata;
  32999. + bulk->size = size;
  33000. + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
  33001. +
  33002. + if (vchiq_prepare_bulk_data(bulk, memhandle, offset, size, dir) !=
  33003. + VCHIQ_SUCCESS)
  33004. + goto unlock_error_exit;
  33005. +
  33006. + wmb();
  33007. +
  33008. + vchiq_log_info(vchiq_core_log_level,
  33009. + "%d: bt (%d->%d) %cx %x@%x %x",
  33010. + state->id,
  33011. + service->localport, service->remoteport, dir_char,
  33012. + size, (unsigned int)bulk->data, (unsigned int)userdata);
  33013. +
  33014. + if (state->is_master) {
  33015. + queue->local_insert++;
  33016. + if (resolve_bulks(service, queue))
  33017. + request_poll(state, service,
  33018. + (dir == VCHIQ_BULK_TRANSMIT) ?
  33019. + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
  33020. + } else {
  33021. + int payload[2] = { (int)bulk->data, bulk->size };
  33022. + VCHIQ_ELEMENT_T element = { payload, sizeof(payload) };
  33023. +
  33024. + status = queue_message(state, NULL,
  33025. + VCHIQ_MAKE_MSG(dir_msgtype,
  33026. + service->localport, service->remoteport),
  33027. + &element, 1, sizeof(payload), 1);
  33028. + if (status != VCHIQ_SUCCESS) {
  33029. + vchiq_complete_bulk(bulk);
  33030. + goto unlock_error_exit;
  33031. + }
  33032. + queue->local_insert++;
  33033. + }
  33034. +
  33035. + mutex_unlock(&service->bulk_mutex);
  33036. +
  33037. + vchiq_log_trace(vchiq_core_log_level,
  33038. + "%d: bt:%d %cx li=%x ri=%x p=%x",
  33039. + state->id,
  33040. + service->localport, dir_char,
  33041. + queue->local_insert, queue->remote_insert, queue->process);
  33042. +
  33043. +waiting:
  33044. + unlock_service(service);
  33045. +
  33046. + status = VCHIQ_SUCCESS;
  33047. +
  33048. + if (bulk_waiter) {
  33049. + bulk_waiter->bulk = bulk;
  33050. + if (down_interruptible(&bulk_waiter->event) != 0)
  33051. + status = VCHIQ_RETRY;
  33052. + else if (bulk_waiter->actual == VCHIQ_BULK_ACTUAL_ABORTED)
  33053. + status = VCHIQ_ERROR;
  33054. + }
  33055. +
  33056. + return status;
  33057. +
  33058. +unlock_error_exit:
  33059. + mutex_unlock(&service->bulk_mutex);
  33060. +
  33061. +error_exit:
  33062. + if (service)
  33063. + unlock_service(service);
  33064. + return status;
  33065. +}
  33066. +
  33067. +VCHIQ_STATUS_T
  33068. +vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T handle,
  33069. + const VCHIQ_ELEMENT_T *elements, unsigned int count)
  33070. +{
  33071. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  33072. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  33073. +
  33074. + unsigned int size = 0;
  33075. + unsigned int i;
  33076. +
  33077. + if (!service ||
  33078. + (vchiq_check_service(service) != VCHIQ_SUCCESS))
  33079. + goto error_exit;
  33080. +
  33081. + for (i = 0; i < (unsigned int)count; i++) {
  33082. + if (elements[i].size) {
  33083. + if (elements[i].data == NULL) {
  33084. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  33085. + goto error_exit;
  33086. + }
  33087. + size += elements[i].size;
  33088. + }
  33089. + }
  33090. +
  33091. + if (size > VCHIQ_MAX_MSG_SIZE) {
  33092. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  33093. + goto error_exit;
  33094. + }
  33095. +
  33096. + switch (service->srvstate) {
  33097. + case VCHIQ_SRVSTATE_OPEN:
  33098. + status = queue_message(service->state, service,
  33099. + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
  33100. + service->localport,
  33101. + service->remoteport),
  33102. + elements, count, size, 1);
  33103. + break;
  33104. + case VCHIQ_SRVSTATE_OPENSYNC:
  33105. + status = queue_message_sync(service->state, service,
  33106. + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
  33107. + service->localport,
  33108. + service->remoteport),
  33109. + elements, count, size, 1);
  33110. + break;
  33111. + default:
  33112. + status = VCHIQ_ERROR;
  33113. + break;
  33114. + }
  33115. +
  33116. +error_exit:
  33117. + if (service)
  33118. + unlock_service(service);
  33119. +
  33120. + return status;
  33121. +}
  33122. +
  33123. +void
  33124. +vchiq_release_message(VCHIQ_SERVICE_HANDLE_T handle, VCHIQ_HEADER_T *header)
  33125. +{
  33126. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  33127. + VCHIQ_SHARED_STATE_T *remote;
  33128. + VCHIQ_STATE_T *state;
  33129. + int slot_index;
  33130. +
  33131. + if (!service)
  33132. + return;
  33133. +
  33134. + state = service->state;
  33135. + remote = state->remote;
  33136. +
  33137. + slot_index = SLOT_INDEX_FROM_DATA(state, (void *)header);
  33138. +
  33139. + if ((slot_index >= remote->slot_first) &&
  33140. + (slot_index <= remote->slot_last)) {
  33141. + int msgid = header->msgid;
  33142. + if (msgid & VCHIQ_MSGID_CLAIMED) {
  33143. + VCHIQ_SLOT_INFO_T *slot_info =
  33144. + SLOT_INFO_FROM_INDEX(state, slot_index);
  33145. +
  33146. + release_slot(state, slot_info, header, service);
  33147. + }
  33148. + } else if (slot_index == remote->slot_sync)
  33149. + release_message_sync(state, header);
  33150. +
  33151. + unlock_service(service);
  33152. +}
  33153. +
  33154. +static void
  33155. +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
  33156. +{
  33157. + header->msgid = VCHIQ_MSGID_PADDING;
  33158. + wmb();
  33159. + remote_event_signal(&state->remote->sync_release);
  33160. +}
  33161. +
  33162. +VCHIQ_STATUS_T
  33163. +vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle, short *peer_version)
  33164. +{
  33165. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  33166. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  33167. +
  33168. + if (!service ||
  33169. + (vchiq_check_service(service) != VCHIQ_SUCCESS) ||
  33170. + !peer_version)
  33171. + goto exit;
  33172. + *peer_version = service->peer_version;
  33173. + status = VCHIQ_SUCCESS;
  33174. +
  33175. +exit:
  33176. + if (service)
  33177. + unlock_service(service);
  33178. + return status;
  33179. +}
  33180. +
  33181. +VCHIQ_STATUS_T
  33182. +vchiq_get_config(VCHIQ_INSTANCE_T instance,
  33183. + int config_size, VCHIQ_CONFIG_T *pconfig)
  33184. +{
  33185. + VCHIQ_CONFIG_T config;
  33186. +
  33187. + (void)instance;
  33188. +
  33189. + config.max_msg_size = VCHIQ_MAX_MSG_SIZE;
  33190. + config.bulk_threshold = VCHIQ_MAX_MSG_SIZE;
  33191. + config.max_outstanding_bulks = VCHIQ_NUM_SERVICE_BULKS;
  33192. + config.max_services = VCHIQ_MAX_SERVICES;
  33193. + config.version = VCHIQ_VERSION;
  33194. + config.version_min = VCHIQ_VERSION_MIN;
  33195. +
  33196. + if (config_size > sizeof(VCHIQ_CONFIG_T))
  33197. + return VCHIQ_ERROR;
  33198. +
  33199. + memcpy(pconfig, &config,
  33200. + min(config_size, (int)(sizeof(VCHIQ_CONFIG_T))));
  33201. +
  33202. + return VCHIQ_SUCCESS;
  33203. +}
  33204. +
  33205. +VCHIQ_STATUS_T
  33206. +vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T handle,
  33207. + VCHIQ_SERVICE_OPTION_T option, int value)
  33208. +{
  33209. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  33210. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  33211. +
  33212. + if (service) {
  33213. + switch (option) {
  33214. + case VCHIQ_SERVICE_OPTION_AUTOCLOSE:
  33215. + service->auto_close = value;
  33216. + status = VCHIQ_SUCCESS;
  33217. + break;
  33218. +
  33219. + case VCHIQ_SERVICE_OPTION_SLOT_QUOTA: {
  33220. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  33221. + &service->state->service_quotas[
  33222. + service->localport];
  33223. + if (value == 0)
  33224. + value = service->state->default_slot_quota;
  33225. + if ((value >= service_quota->slot_use_count) &&
  33226. + (value < (unsigned short)~0)) {
  33227. + service_quota->slot_quota = value;
  33228. + if ((value >= service_quota->slot_use_count) &&
  33229. + (service_quota->message_quota >=
  33230. + service_quota->message_use_count)) {
  33231. + /* Signal the service that it may have
  33232. + ** dropped below its quota */
  33233. + up(&service_quota->quota_event);
  33234. + }
  33235. + status = VCHIQ_SUCCESS;
  33236. + }
  33237. + } break;
  33238. +
  33239. + case VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA: {
  33240. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  33241. + &service->state->service_quotas[
  33242. + service->localport];
  33243. + if (value == 0)
  33244. + value = service->state->default_message_quota;
  33245. + if ((value >= service_quota->message_use_count) &&
  33246. + (value < (unsigned short)~0)) {
  33247. + service_quota->message_quota = value;
  33248. + if ((value >=
  33249. + service_quota->message_use_count) &&
  33250. + (service_quota->slot_quota >=
  33251. + service_quota->slot_use_count))
  33252. + /* Signal the service that it may have
  33253. + ** dropped below its quota */
  33254. + up(&service_quota->quota_event);
  33255. + status = VCHIQ_SUCCESS;
  33256. + }
  33257. + } break;
  33258. +
  33259. + case VCHIQ_SERVICE_OPTION_SYNCHRONOUS:
  33260. + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
  33261. + (service->srvstate ==
  33262. + VCHIQ_SRVSTATE_LISTENING)) {
  33263. + service->sync = value;
  33264. + status = VCHIQ_SUCCESS;
  33265. + }
  33266. + break;
  33267. +
  33268. + case VCHIQ_SERVICE_OPTION_TRACE:
  33269. + service->trace = value;
  33270. + status = VCHIQ_SUCCESS;
  33271. + break;
  33272. +
  33273. + default:
  33274. + break;
  33275. + }
  33276. + unlock_service(service);
  33277. + }
  33278. +
  33279. + return status;
  33280. +}
  33281. +
  33282. +void
  33283. +vchiq_dump_shared_state(void *dump_context, VCHIQ_STATE_T *state,
  33284. + VCHIQ_SHARED_STATE_T *shared, const char *label)
  33285. +{
  33286. + static const char *const debug_names[] = {
  33287. + "<entries>",
  33288. + "SLOT_HANDLER_COUNT",
  33289. + "SLOT_HANDLER_LINE",
  33290. + "PARSE_LINE",
  33291. + "PARSE_HEADER",
  33292. + "PARSE_MSGID",
  33293. + "AWAIT_COMPLETION_LINE",
  33294. + "DEQUEUE_MESSAGE_LINE",
  33295. + "SERVICE_CALLBACK_LINE",
  33296. + "MSG_QUEUE_FULL_COUNT",
  33297. + "COMPLETION_QUEUE_FULL_COUNT"
  33298. + };
  33299. + int i;
  33300. +
  33301. + char buf[80];
  33302. + int len;
  33303. + len = snprintf(buf, sizeof(buf),
  33304. + " %s: slots %d-%d tx_pos=%x recycle=%x",
  33305. + label, shared->slot_first, shared->slot_last,
  33306. + shared->tx_pos, shared->slot_queue_recycle);
  33307. + vchiq_dump(dump_context, buf, len + 1);
  33308. +
  33309. + len = snprintf(buf, sizeof(buf),
  33310. + " Slots claimed:");
  33311. + vchiq_dump(dump_context, buf, len + 1);
  33312. +
  33313. + for (i = shared->slot_first; i <= shared->slot_last; i++) {
  33314. + VCHIQ_SLOT_INFO_T slot_info = *SLOT_INFO_FROM_INDEX(state, i);
  33315. + if (slot_info.use_count != slot_info.release_count) {
  33316. + len = snprintf(buf, sizeof(buf),
  33317. + " %d: %d/%d", i, slot_info.use_count,
  33318. + slot_info.release_count);
  33319. + vchiq_dump(dump_context, buf, len + 1);
  33320. + }
  33321. + }
  33322. +
  33323. + for (i = 1; i < shared->debug[DEBUG_ENTRIES]; i++) {
  33324. + len = snprintf(buf, sizeof(buf), " DEBUG: %s = %d(%x)",
  33325. + debug_names[i], shared->debug[i], shared->debug[i]);
  33326. + vchiq_dump(dump_context, buf, len + 1);
  33327. + }
  33328. +}
  33329. +
  33330. +void
  33331. +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state)
  33332. +{
  33333. + char buf[80];
  33334. + int len;
  33335. + int i;
  33336. +
  33337. + len = snprintf(buf, sizeof(buf), "State %d: %s", state->id,
  33338. + conn_state_names[state->conn_state]);
  33339. + vchiq_dump(dump_context, buf, len + 1);
  33340. +
  33341. + len = snprintf(buf, sizeof(buf),
  33342. + " tx_pos=%x(@%x), rx_pos=%x(@%x)",
  33343. + state->local->tx_pos,
  33344. + (uint32_t)state->tx_data +
  33345. + (state->local_tx_pos & VCHIQ_SLOT_MASK),
  33346. + state->rx_pos,
  33347. + (uint32_t)state->rx_data +
  33348. + (state->rx_pos & VCHIQ_SLOT_MASK));
  33349. + vchiq_dump(dump_context, buf, len + 1);
  33350. +
  33351. + len = snprintf(buf, sizeof(buf),
  33352. + " Version: %d (min %d)",
  33353. + VCHIQ_VERSION, VCHIQ_VERSION_MIN);
  33354. + vchiq_dump(dump_context, buf, len + 1);
  33355. +
  33356. + if (VCHIQ_ENABLE_STATS) {
  33357. + len = snprintf(buf, sizeof(buf),
  33358. + " Stats: ctrl_tx_count=%d, ctrl_rx_count=%d, "
  33359. + "error_count=%d",
  33360. + state->stats.ctrl_tx_count, state->stats.ctrl_rx_count,
  33361. + state->stats.error_count);
  33362. + vchiq_dump(dump_context, buf, len + 1);
  33363. + }
  33364. +
  33365. + len = snprintf(buf, sizeof(buf),
  33366. + " Slots: %d available (%d data), %d recyclable, %d stalls "
  33367. + "(%d data)",
  33368. + ((state->slot_queue_available * VCHIQ_SLOT_SIZE) -
  33369. + state->local_tx_pos) / VCHIQ_SLOT_SIZE,
  33370. + state->data_quota - state->data_use_count,
  33371. + state->local->slot_queue_recycle - state->slot_queue_available,
  33372. + state->stats.slot_stalls, state->stats.data_stalls);
  33373. + vchiq_dump(dump_context, buf, len + 1);
  33374. +
  33375. + vchiq_dump_platform_state(dump_context);
  33376. +
  33377. + vchiq_dump_shared_state(dump_context, state, state->local, "Local");
  33378. + vchiq_dump_shared_state(dump_context, state, state->remote, "Remote");
  33379. +
  33380. + vchiq_dump_platform_instances(dump_context);
  33381. +
  33382. + for (i = 0; i < state->unused_service; i++) {
  33383. + VCHIQ_SERVICE_T *service = find_service_by_port(state, i);
  33384. +
  33385. + if (service) {
  33386. + vchiq_dump_service_state(dump_context, service);
  33387. + unlock_service(service);
  33388. + }
  33389. + }
  33390. +}
  33391. +
  33392. +void
  33393. +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
  33394. +{
  33395. + char buf[80];
  33396. + int len;
  33397. +
  33398. + len = snprintf(buf, sizeof(buf), "Service %d: %s (ref %u)",
  33399. + service->localport, srvstate_names[service->srvstate],
  33400. + service->ref_count - 1); /*Don't include the lock just taken*/
  33401. +
  33402. + if (service->srvstate != VCHIQ_SRVSTATE_FREE) {
  33403. + char remoteport[30];
  33404. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  33405. + &service->state->service_quotas[service->localport];
  33406. + int fourcc = service->base.fourcc;
  33407. + int tx_pending, rx_pending;
  33408. + if (service->remoteport != VCHIQ_PORT_FREE) {
  33409. + int len2 = snprintf(remoteport, sizeof(remoteport),
  33410. + "%d", service->remoteport);
  33411. + if (service->public_fourcc != VCHIQ_FOURCC_INVALID)
  33412. + snprintf(remoteport + len2,
  33413. + sizeof(remoteport) - len2,
  33414. + " (client %x)", service->client_id);
  33415. + } else
  33416. + strcpy(remoteport, "n/a");
  33417. +
  33418. + len += snprintf(buf + len, sizeof(buf) - len,
  33419. + " '%c%c%c%c' remote %s (msg use %d/%d, slot use %d/%d)",
  33420. + VCHIQ_FOURCC_AS_4CHARS(fourcc),
  33421. + remoteport,
  33422. + service_quota->message_use_count,
  33423. + service_quota->message_quota,
  33424. + service_quota->slot_use_count,
  33425. + service_quota->slot_quota);
  33426. +
  33427. + vchiq_dump(dump_context, buf, len + 1);
  33428. +
  33429. + tx_pending = service->bulk_tx.local_insert -
  33430. + service->bulk_tx.remote_insert;
  33431. +
  33432. + rx_pending = service->bulk_rx.local_insert -
  33433. + service->bulk_rx.remote_insert;
  33434. +
  33435. + len = snprintf(buf, sizeof(buf),
  33436. + " Bulk: tx_pending=%d (size %d),"
  33437. + " rx_pending=%d (size %d)",
  33438. + tx_pending,
  33439. + tx_pending ? service->bulk_tx.bulks[
  33440. + BULK_INDEX(service->bulk_tx.remove)].size : 0,
  33441. + rx_pending,
  33442. + rx_pending ? service->bulk_rx.bulks[
  33443. + BULK_INDEX(service->bulk_rx.remove)].size : 0);
  33444. +
  33445. + if (VCHIQ_ENABLE_STATS) {
  33446. + vchiq_dump(dump_context, buf, len + 1);
  33447. +
  33448. + len = snprintf(buf, sizeof(buf),
  33449. + " Ctrl: tx_count=%d, tx_bytes=%llu, "
  33450. + "rx_count=%d, rx_bytes=%llu",
  33451. + service->stats.ctrl_tx_count,
  33452. + service->stats.ctrl_tx_bytes,
  33453. + service->stats.ctrl_rx_count,
  33454. + service->stats.ctrl_rx_bytes);
  33455. + vchiq_dump(dump_context, buf, len + 1);
  33456. +
  33457. + len = snprintf(buf, sizeof(buf),
  33458. + " Bulk: tx_count=%d, tx_bytes=%llu, "
  33459. + "rx_count=%d, rx_bytes=%llu",
  33460. + service->stats.bulk_tx_count,
  33461. + service->stats.bulk_tx_bytes,
  33462. + service->stats.bulk_rx_count,
  33463. + service->stats.bulk_rx_bytes);
  33464. + vchiq_dump(dump_context, buf, len + 1);
  33465. +
  33466. + len = snprintf(buf, sizeof(buf),
  33467. + " %d quota stalls, %d slot stalls, "
  33468. + "%d bulk stalls, %d aborted, %d errors",
  33469. + service->stats.quota_stalls,
  33470. + service->stats.slot_stalls,
  33471. + service->stats.bulk_stalls,
  33472. + service->stats.bulk_aborted_count,
  33473. + service->stats.error_count);
  33474. + }
  33475. + }
  33476. +
  33477. + vchiq_dump(dump_context, buf, len + 1);
  33478. +
  33479. + if (service->srvstate != VCHIQ_SRVSTATE_FREE)
  33480. + vchiq_dump_platform_service_state(dump_context, service);
  33481. +}
  33482. +
  33483. +
  33484. +void
  33485. +vchiq_loud_error_header(void)
  33486. +{
  33487. + vchiq_log_error(vchiq_core_log_level,
  33488. + "============================================================"
  33489. + "================");
  33490. + vchiq_log_error(vchiq_core_log_level,
  33491. + "============================================================"
  33492. + "================");
  33493. + vchiq_log_error(vchiq_core_log_level, "=====");
  33494. +}
  33495. +
  33496. +void
  33497. +vchiq_loud_error_footer(void)
  33498. +{
  33499. + vchiq_log_error(vchiq_core_log_level, "=====");
  33500. + vchiq_log_error(vchiq_core_log_level,
  33501. + "============================================================"
  33502. + "================");
  33503. + vchiq_log_error(vchiq_core_log_level,
  33504. + "============================================================"
  33505. + "================");
  33506. +}
  33507. +
  33508. +
  33509. +VCHIQ_STATUS_T vchiq_send_remote_use(VCHIQ_STATE_T *state)
  33510. +{
  33511. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  33512. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  33513. + status = queue_message(state, NULL,
  33514. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE, 0, 0),
  33515. + NULL, 0, 0, 0);
  33516. + return status;
  33517. +}
  33518. +
  33519. +VCHIQ_STATUS_T vchiq_send_remote_release(VCHIQ_STATE_T *state)
  33520. +{
  33521. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  33522. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  33523. + status = queue_message(state, NULL,
  33524. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_RELEASE, 0, 0),
  33525. + NULL, 0, 0, 0);
  33526. + return status;
  33527. +}
  33528. +
  33529. +VCHIQ_STATUS_T vchiq_send_remote_use_active(VCHIQ_STATE_T *state)
  33530. +{
  33531. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  33532. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  33533. + status = queue_message(state, NULL,
  33534. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE_ACTIVE, 0, 0),
  33535. + NULL, 0, 0, 0);
  33536. + return status;
  33537. +}
  33538. +
  33539. +void vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
  33540. + size_t numBytes)
  33541. +{
  33542. + const uint8_t *mem = (const uint8_t *)voidMem;
  33543. + size_t offset;
  33544. + char lineBuf[100];
  33545. + char *s;
  33546. +
  33547. + while (numBytes > 0) {
  33548. + s = lineBuf;
  33549. +
  33550. + for (offset = 0; offset < 16; offset++) {
  33551. + if (offset < numBytes)
  33552. + s += snprintf(s, 4, "%02x ", mem[offset]);
  33553. + else
  33554. + s += snprintf(s, 4, " ");
  33555. + }
  33556. +
  33557. + for (offset = 0; offset < 16; offset++) {
  33558. + if (offset < numBytes) {
  33559. + uint8_t ch = mem[offset];
  33560. +
  33561. + if ((ch < ' ') || (ch > '~'))
  33562. + ch = '.';
  33563. + *s++ = (char)ch;
  33564. + }
  33565. + }
  33566. + *s++ = '\0';
  33567. +
  33568. + if ((label != NULL) && (*label != '\0'))
  33569. + vchiq_log_trace(VCHIQ_LOG_TRACE,
  33570. + "%s: %08x: %s", label, addr, lineBuf);
  33571. + else
  33572. + vchiq_log_trace(VCHIQ_LOG_TRACE,
  33573. + "%08x: %s", addr, lineBuf);
  33574. +
  33575. + addr += 16;
  33576. + mem += 16;
  33577. + if (numBytes > 16)
  33578. + numBytes -= 16;
  33579. + else
  33580. + numBytes = 0;
  33581. + }
  33582. +}
  33583. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h
  33584. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h 1969-12-31 18:00:00.000000000 -0600
  33585. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h 2014-12-03 19:13:38.228418001 -0600
  33586. @@ -0,0 +1,711 @@
  33587. +/**
  33588. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  33589. + *
  33590. + * Redistribution and use in source and binary forms, with or without
  33591. + * modification, are permitted provided that the following conditions
  33592. + * are met:
  33593. + * 1. Redistributions of source code must retain the above copyright
  33594. + * notice, this list of conditions, and the following disclaimer,
  33595. + * without modification.
  33596. + * 2. Redistributions in binary form must reproduce the above copyright
  33597. + * notice, this list of conditions and the following disclaimer in the
  33598. + * documentation and/or other materials provided with the distribution.
  33599. + * 3. The names of the above-listed copyright holders may not be used
  33600. + * to endorse or promote products derived from this software without
  33601. + * specific prior written permission.
  33602. + *
  33603. + * ALTERNATIVELY, this software may be distributed under the terms of the
  33604. + * GNU General Public License ("GPL") version 2, as published by the Free
  33605. + * Software Foundation.
  33606. + *
  33607. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  33608. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  33609. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33610. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33611. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  33612. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33613. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33614. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33615. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33616. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33617. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33618. + */
  33619. +
  33620. +#ifndef VCHIQ_CORE_H
  33621. +#define VCHIQ_CORE_H
  33622. +
  33623. +#include <linux/mutex.h>
  33624. +#include <linux/semaphore.h>
  33625. +#include <linux/kthread.h>
  33626. +
  33627. +#include "vchiq_cfg.h"
  33628. +
  33629. +#include "vchiq.h"
  33630. +
  33631. +/* Run time control of log level, based on KERN_XXX level. */
  33632. +#define VCHIQ_LOG_DEFAULT 4
  33633. +#define VCHIQ_LOG_ERROR 3
  33634. +#define VCHIQ_LOG_WARNING 4
  33635. +#define VCHIQ_LOG_INFO 6
  33636. +#define VCHIQ_LOG_TRACE 7
  33637. +
  33638. +#define VCHIQ_LOG_PREFIX KERN_INFO "vchiq: "
  33639. +
  33640. +#ifndef vchiq_log_error
  33641. +#define vchiq_log_error(cat, fmt, ...) \
  33642. + do { if (cat >= VCHIQ_LOG_ERROR) \
  33643. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  33644. +#endif
  33645. +#ifndef vchiq_log_warning
  33646. +#define vchiq_log_warning(cat, fmt, ...) \
  33647. + do { if (cat >= VCHIQ_LOG_WARNING) \
  33648. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  33649. +#endif
  33650. +#ifndef vchiq_log_info
  33651. +#define vchiq_log_info(cat, fmt, ...) \
  33652. + do { if (cat >= VCHIQ_LOG_INFO) \
  33653. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  33654. +#endif
  33655. +#ifndef vchiq_log_trace
  33656. +#define vchiq_log_trace(cat, fmt, ...) \
  33657. + do { if (cat >= VCHIQ_LOG_TRACE) \
  33658. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  33659. +#endif
  33660. +
  33661. +#define vchiq_loud_error(...) \
  33662. + vchiq_log_error(vchiq_core_log_level, "===== " __VA_ARGS__)
  33663. +
  33664. +#ifndef vchiq_static_assert
  33665. +#define vchiq_static_assert(cond) __attribute__((unused)) \
  33666. + extern int vchiq_static_assert[(cond) ? 1 : -1]
  33667. +#endif
  33668. +
  33669. +#define IS_POW2(x) (x && ((x & (x - 1)) == 0))
  33670. +
  33671. +/* Ensure that the slot size and maximum number of slots are powers of 2 */
  33672. +vchiq_static_assert(IS_POW2(VCHIQ_SLOT_SIZE));
  33673. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS));
  33674. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS_PER_SIDE));
  33675. +
  33676. +#define VCHIQ_SLOT_MASK (VCHIQ_SLOT_SIZE - 1)
  33677. +#define VCHIQ_SLOT_QUEUE_MASK (VCHIQ_MAX_SLOTS_PER_SIDE - 1)
  33678. +#define VCHIQ_SLOT_ZERO_SLOTS ((sizeof(VCHIQ_SLOT_ZERO_T) + \
  33679. + VCHIQ_SLOT_SIZE - 1) / VCHIQ_SLOT_SIZE)
  33680. +
  33681. +#define VCHIQ_MSG_PADDING 0 /* - */
  33682. +#define VCHIQ_MSG_CONNECT 1 /* - */
  33683. +#define VCHIQ_MSG_OPEN 2 /* + (srcport, -), fourcc, client_id */
  33684. +#define VCHIQ_MSG_OPENACK 3 /* + (srcport, dstport) */
  33685. +#define VCHIQ_MSG_CLOSE 4 /* + (srcport, dstport) */
  33686. +#define VCHIQ_MSG_DATA 5 /* + (srcport, dstport) */
  33687. +#define VCHIQ_MSG_BULK_RX 6 /* + (srcport, dstport), data, size */
  33688. +#define VCHIQ_MSG_BULK_TX 7 /* + (srcport, dstport), data, size */
  33689. +#define VCHIQ_MSG_BULK_RX_DONE 8 /* + (srcport, dstport), actual */
  33690. +#define VCHIQ_MSG_BULK_TX_DONE 9 /* + (srcport, dstport), actual */
  33691. +#define VCHIQ_MSG_PAUSE 10 /* - */
  33692. +#define VCHIQ_MSG_RESUME 11 /* - */
  33693. +#define VCHIQ_MSG_REMOTE_USE 12 /* - */
  33694. +#define VCHIQ_MSG_REMOTE_RELEASE 13 /* - */
  33695. +#define VCHIQ_MSG_REMOTE_USE_ACTIVE 14 /* - */
  33696. +
  33697. +#define VCHIQ_PORT_MAX (VCHIQ_MAX_SERVICES - 1)
  33698. +#define VCHIQ_PORT_FREE 0x1000
  33699. +#define VCHIQ_PORT_IS_VALID(port) (port < VCHIQ_PORT_FREE)
  33700. +#define VCHIQ_MAKE_MSG(type, srcport, dstport) \
  33701. + ((type<<24) | (srcport<<12) | (dstport<<0))
  33702. +#define VCHIQ_MSG_TYPE(msgid) ((unsigned int)msgid >> 24)
  33703. +#define VCHIQ_MSG_SRCPORT(msgid) \
  33704. + (unsigned short)(((unsigned int)msgid >> 12) & 0xfff)
  33705. +#define VCHIQ_MSG_DSTPORT(msgid) \
  33706. + ((unsigned short)msgid & 0xfff)
  33707. +
  33708. +#define VCHIQ_FOURCC_AS_4CHARS(fourcc) \
  33709. + ((fourcc) >> 24) & 0xff, \
  33710. + ((fourcc) >> 16) & 0xff, \
  33711. + ((fourcc) >> 8) & 0xff, \
  33712. + (fourcc) & 0xff
  33713. +
  33714. +/* Ensure the fields are wide enough */
  33715. +vchiq_static_assert(VCHIQ_MSG_SRCPORT(VCHIQ_MAKE_MSG(0, 0, VCHIQ_PORT_MAX))
  33716. + == 0);
  33717. +vchiq_static_assert(VCHIQ_MSG_TYPE(VCHIQ_MAKE_MSG(0, VCHIQ_PORT_MAX, 0)) == 0);
  33718. +vchiq_static_assert((unsigned int)VCHIQ_PORT_MAX <
  33719. + (unsigned int)VCHIQ_PORT_FREE);
  33720. +
  33721. +#define VCHIQ_MSGID_PADDING VCHIQ_MAKE_MSG(VCHIQ_MSG_PADDING, 0, 0)
  33722. +#define VCHIQ_MSGID_CLAIMED 0x40000000
  33723. +
  33724. +#define VCHIQ_FOURCC_INVALID 0x00000000
  33725. +#define VCHIQ_FOURCC_IS_LEGAL(fourcc) (fourcc != VCHIQ_FOURCC_INVALID)
  33726. +
  33727. +#define VCHIQ_BULK_ACTUAL_ABORTED -1
  33728. +
  33729. +typedef uint32_t BITSET_T;
  33730. +
  33731. +vchiq_static_assert((sizeof(BITSET_T) * 8) == 32);
  33732. +
  33733. +#define BITSET_SIZE(b) ((b + 31) >> 5)
  33734. +#define BITSET_WORD(b) (b >> 5)
  33735. +#define BITSET_BIT(b) (1 << (b & 31))
  33736. +#define BITSET_ZERO(bs) memset(bs, 0, sizeof(bs))
  33737. +#define BITSET_IS_SET(bs, b) (bs[BITSET_WORD(b)] & BITSET_BIT(b))
  33738. +#define BITSET_SET(bs, b) (bs[BITSET_WORD(b)] |= BITSET_BIT(b))
  33739. +#define BITSET_CLR(bs, b) (bs[BITSET_WORD(b)] &= ~BITSET_BIT(b))
  33740. +
  33741. +#if VCHIQ_ENABLE_STATS
  33742. +#define VCHIQ_STATS_INC(state, stat) (state->stats. stat++)
  33743. +#define VCHIQ_SERVICE_STATS_INC(service, stat) (service->stats. stat++)
  33744. +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) \
  33745. + (service->stats. stat += addend)
  33746. +#else
  33747. +#define VCHIQ_STATS_INC(state, stat) ((void)0)
  33748. +#define VCHIQ_SERVICE_STATS_INC(service, stat) ((void)0)
  33749. +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) ((void)0)
  33750. +#endif
  33751. +
  33752. +enum {
  33753. + DEBUG_ENTRIES,
  33754. +#if VCHIQ_ENABLE_DEBUG
  33755. + DEBUG_SLOT_HANDLER_COUNT,
  33756. + DEBUG_SLOT_HANDLER_LINE,
  33757. + DEBUG_PARSE_LINE,
  33758. + DEBUG_PARSE_HEADER,
  33759. + DEBUG_PARSE_MSGID,
  33760. + DEBUG_AWAIT_COMPLETION_LINE,
  33761. + DEBUG_DEQUEUE_MESSAGE_LINE,
  33762. + DEBUG_SERVICE_CALLBACK_LINE,
  33763. + DEBUG_MSG_QUEUE_FULL_COUNT,
  33764. + DEBUG_COMPLETION_QUEUE_FULL_COUNT,
  33765. +#endif
  33766. + DEBUG_MAX
  33767. +};
  33768. +
  33769. +#if VCHIQ_ENABLE_DEBUG
  33770. +
  33771. +#define DEBUG_INITIALISE(local) int *debug_ptr = (local)->debug;
  33772. +#define DEBUG_TRACE(d) \
  33773. + do { debug_ptr[DEBUG_ ## d] = __LINE__; dsb(); } while (0)
  33774. +#define DEBUG_VALUE(d, v) \
  33775. + do { debug_ptr[DEBUG_ ## d] = (v); dsb(); } while (0)
  33776. +#define DEBUG_COUNT(d) \
  33777. + do { debug_ptr[DEBUG_ ## d]++; dsb(); } while (0)
  33778. +
  33779. +#else /* VCHIQ_ENABLE_DEBUG */
  33780. +
  33781. +#define DEBUG_INITIALISE(local)
  33782. +#define DEBUG_TRACE(d)
  33783. +#define DEBUG_VALUE(d, v)
  33784. +#define DEBUG_COUNT(d)
  33785. +
  33786. +#endif /* VCHIQ_ENABLE_DEBUG */
  33787. +
  33788. +typedef enum {
  33789. + VCHIQ_CONNSTATE_DISCONNECTED,
  33790. + VCHIQ_CONNSTATE_CONNECTING,
  33791. + VCHIQ_CONNSTATE_CONNECTED,
  33792. + VCHIQ_CONNSTATE_PAUSING,
  33793. + VCHIQ_CONNSTATE_PAUSE_SENT,
  33794. + VCHIQ_CONNSTATE_PAUSED,
  33795. + VCHIQ_CONNSTATE_RESUMING,
  33796. + VCHIQ_CONNSTATE_PAUSE_TIMEOUT,
  33797. + VCHIQ_CONNSTATE_RESUME_TIMEOUT
  33798. +} VCHIQ_CONNSTATE_T;
  33799. +
  33800. +enum {
  33801. + VCHIQ_SRVSTATE_FREE,
  33802. + VCHIQ_SRVSTATE_HIDDEN,
  33803. + VCHIQ_SRVSTATE_LISTENING,
  33804. + VCHIQ_SRVSTATE_OPENING,
  33805. + VCHIQ_SRVSTATE_OPEN,
  33806. + VCHIQ_SRVSTATE_OPENSYNC,
  33807. + VCHIQ_SRVSTATE_CLOSESENT,
  33808. + VCHIQ_SRVSTATE_CLOSERECVD,
  33809. + VCHIQ_SRVSTATE_CLOSEWAIT,
  33810. + VCHIQ_SRVSTATE_CLOSED
  33811. +};
  33812. +
  33813. +enum {
  33814. + VCHIQ_POLL_TERMINATE,
  33815. + VCHIQ_POLL_REMOVE,
  33816. + VCHIQ_POLL_TXNOTIFY,
  33817. + VCHIQ_POLL_RXNOTIFY,
  33818. + VCHIQ_POLL_COUNT
  33819. +};
  33820. +
  33821. +typedef enum {
  33822. + VCHIQ_BULK_TRANSMIT,
  33823. + VCHIQ_BULK_RECEIVE
  33824. +} VCHIQ_BULK_DIR_T;
  33825. +
  33826. +typedef void (*VCHIQ_USERDATA_TERM_T)(void *userdata);
  33827. +
  33828. +typedef struct vchiq_bulk_struct {
  33829. + short mode;
  33830. + short dir;
  33831. + void *userdata;
  33832. + VCHI_MEM_HANDLE_T handle;
  33833. + void *data;
  33834. + int size;
  33835. + void *remote_data;
  33836. + int remote_size;
  33837. + int actual;
  33838. +} VCHIQ_BULK_T;
  33839. +
  33840. +typedef struct vchiq_bulk_queue_struct {
  33841. + int local_insert; /* Where to insert the next local bulk */
  33842. + int remote_insert; /* Where to insert the next remote bulk (master) */
  33843. + int process; /* Bulk to transfer next */
  33844. + int remote_notify; /* Bulk to notify the remote client of next (mstr) */
  33845. + int remove; /* Bulk to notify the local client of, and remove,
  33846. + ** next */
  33847. + VCHIQ_BULK_T bulks[VCHIQ_NUM_SERVICE_BULKS];
  33848. +} VCHIQ_BULK_QUEUE_T;
  33849. +
  33850. +typedef struct remote_event_struct {
  33851. + int armed;
  33852. + int fired;
  33853. + struct semaphore *event;
  33854. +} REMOTE_EVENT_T;
  33855. +
  33856. +typedef struct opaque_platform_state_t *VCHIQ_PLATFORM_STATE_T;
  33857. +
  33858. +typedef struct vchiq_state_struct VCHIQ_STATE_T;
  33859. +
  33860. +typedef struct vchiq_slot_struct {
  33861. + char data[VCHIQ_SLOT_SIZE];
  33862. +} VCHIQ_SLOT_T;
  33863. +
  33864. +typedef struct vchiq_slot_info_struct {
  33865. + /* Use two counters rather than one to avoid the need for a mutex. */
  33866. + short use_count;
  33867. + short release_count;
  33868. +} VCHIQ_SLOT_INFO_T;
  33869. +
  33870. +typedef struct vchiq_service_struct {
  33871. + VCHIQ_SERVICE_BASE_T base;
  33872. + VCHIQ_SERVICE_HANDLE_T handle;
  33873. + unsigned int ref_count;
  33874. + int srvstate;
  33875. + VCHIQ_USERDATA_TERM_T userdata_term;
  33876. + unsigned int localport;
  33877. + unsigned int remoteport;
  33878. + int public_fourcc;
  33879. + int client_id;
  33880. + char auto_close;
  33881. + char sync;
  33882. + char closing;
  33883. + char trace;
  33884. + atomic_t poll_flags;
  33885. + short version;
  33886. + short version_min;
  33887. + short peer_version;
  33888. +
  33889. + VCHIQ_STATE_T *state;
  33890. + VCHIQ_INSTANCE_T instance;
  33891. +
  33892. + int service_use_count;
  33893. +
  33894. + VCHIQ_BULK_QUEUE_T bulk_tx;
  33895. + VCHIQ_BULK_QUEUE_T bulk_rx;
  33896. +
  33897. + struct semaphore remove_event;
  33898. + struct semaphore bulk_remove_event;
  33899. + struct mutex bulk_mutex;
  33900. +
  33901. + struct service_stats_struct {
  33902. + int quota_stalls;
  33903. + int slot_stalls;
  33904. + int bulk_stalls;
  33905. + int error_count;
  33906. + int ctrl_tx_count;
  33907. + int ctrl_rx_count;
  33908. + int bulk_tx_count;
  33909. + int bulk_rx_count;
  33910. + int bulk_aborted_count;
  33911. + uint64_t ctrl_tx_bytes;
  33912. + uint64_t ctrl_rx_bytes;
  33913. + uint64_t bulk_tx_bytes;
  33914. + uint64_t bulk_rx_bytes;
  33915. + } stats;
  33916. +} VCHIQ_SERVICE_T;
  33917. +
  33918. +/* The quota information is outside VCHIQ_SERVICE_T so that it can be
  33919. + statically allocated, since for accounting reasons a service's slot
  33920. + usage is carried over between users of the same port number.
  33921. + */
  33922. +typedef struct vchiq_service_quota_struct {
  33923. + unsigned short slot_quota;
  33924. + unsigned short slot_use_count;
  33925. + unsigned short message_quota;
  33926. + unsigned short message_use_count;
  33927. + struct semaphore quota_event;
  33928. + int previous_tx_index;
  33929. +} VCHIQ_SERVICE_QUOTA_T;
  33930. +
  33931. +typedef struct vchiq_shared_state_struct {
  33932. +
  33933. + /* A non-zero value here indicates that the content is valid. */
  33934. + int initialised;
  33935. +
  33936. + /* The first and last (inclusive) slots allocated to the owner. */
  33937. + int slot_first;
  33938. + int slot_last;
  33939. +
  33940. + /* The slot allocated to synchronous messages from the owner. */
  33941. + int slot_sync;
  33942. +
  33943. + /* Signalling this event indicates that owner's slot handler thread
  33944. + ** should run. */
  33945. + REMOTE_EVENT_T trigger;
  33946. +
  33947. + /* Indicates the byte position within the stream where the next message
  33948. + ** will be written. The least significant bits are an index into the
  33949. + ** slot. The next bits are the index of the slot in slot_queue. */
  33950. + int tx_pos;
  33951. +
  33952. + /* This event should be signalled when a slot is recycled. */
  33953. + REMOTE_EVENT_T recycle;
  33954. +
  33955. + /* The slot_queue index where the next recycled slot will be written. */
  33956. + int slot_queue_recycle;
  33957. +
  33958. + /* This event should be signalled when a synchronous message is sent. */
  33959. + REMOTE_EVENT_T sync_trigger;
  33960. +
  33961. + /* This event should be signalled when a synchronous message has been
  33962. + ** released. */
  33963. + REMOTE_EVENT_T sync_release;
  33964. +
  33965. + /* A circular buffer of slot indexes. */
  33966. + int slot_queue[VCHIQ_MAX_SLOTS_PER_SIDE];
  33967. +
  33968. + /* Debugging state */
  33969. + int debug[DEBUG_MAX];
  33970. +} VCHIQ_SHARED_STATE_T;
  33971. +
  33972. +typedef struct vchiq_slot_zero_struct {
  33973. + int magic;
  33974. + short version;
  33975. + short version_min;
  33976. + int slot_zero_size;
  33977. + int slot_size;
  33978. + int max_slots;
  33979. + int max_slots_per_side;
  33980. + int platform_data[2];
  33981. + VCHIQ_SHARED_STATE_T master;
  33982. + VCHIQ_SHARED_STATE_T slave;
  33983. + VCHIQ_SLOT_INFO_T slots[VCHIQ_MAX_SLOTS];
  33984. +} VCHIQ_SLOT_ZERO_T;
  33985. +
  33986. +struct vchiq_state_struct {
  33987. + int id;
  33988. + int initialised;
  33989. + VCHIQ_CONNSTATE_T conn_state;
  33990. + int is_master;
  33991. +
  33992. + VCHIQ_SHARED_STATE_T *local;
  33993. + VCHIQ_SHARED_STATE_T *remote;
  33994. + VCHIQ_SLOT_T *slot_data;
  33995. +
  33996. + unsigned short default_slot_quota;
  33997. + unsigned short default_message_quota;
  33998. +
  33999. + /* Event indicating connect message received */
  34000. + struct semaphore connect;
  34001. +
  34002. + /* Mutex protecting services */
  34003. + struct mutex mutex;
  34004. + VCHIQ_INSTANCE_T *instance;
  34005. +
  34006. + /* Processes incoming messages */
  34007. + struct task_struct *slot_handler_thread;
  34008. +
  34009. + /* Processes recycled slots */
  34010. + struct task_struct *recycle_thread;
  34011. +
  34012. + /* Processes synchronous messages */
  34013. + struct task_struct *sync_thread;
  34014. +
  34015. + /* Local implementation of the trigger remote event */
  34016. + struct semaphore trigger_event;
  34017. +
  34018. + /* Local implementation of the recycle remote event */
  34019. + struct semaphore recycle_event;
  34020. +
  34021. + /* Local implementation of the sync trigger remote event */
  34022. + struct semaphore sync_trigger_event;
  34023. +
  34024. + /* Local implementation of the sync release remote event */
  34025. + struct semaphore sync_release_event;
  34026. +
  34027. + char *tx_data;
  34028. + char *rx_data;
  34029. + VCHIQ_SLOT_INFO_T *rx_info;
  34030. +
  34031. + struct mutex slot_mutex;
  34032. +
  34033. + struct mutex recycle_mutex;
  34034. +
  34035. + struct mutex sync_mutex;
  34036. +
  34037. + struct mutex bulk_transfer_mutex;
  34038. +
  34039. + /* Indicates the byte position within the stream from where the next
  34040. + ** message will be read. The least significant bits are an index into
  34041. + ** the slot.The next bits are the index of the slot in
  34042. + ** remote->slot_queue. */
  34043. + int rx_pos;
  34044. +
  34045. + /* A cached copy of local->tx_pos. Only write to local->tx_pos, and read
  34046. + from remote->tx_pos. */
  34047. + int local_tx_pos;
  34048. +
  34049. + /* The slot_queue index of the slot to become available next. */
  34050. + int slot_queue_available;
  34051. +
  34052. + /* A flag to indicate if any poll has been requested */
  34053. + int poll_needed;
  34054. +
  34055. + /* Ths index of the previous slot used for data messages. */
  34056. + int previous_data_index;
  34057. +
  34058. + /* The number of slots occupied by data messages. */
  34059. + unsigned short data_use_count;
  34060. +
  34061. + /* The maximum number of slots to be occupied by data messages. */
  34062. + unsigned short data_quota;
  34063. +
  34064. + /* An array of bit sets indicating which services must be polled. */
  34065. + atomic_t poll_services[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
  34066. +
  34067. + /* The number of the first unused service */
  34068. + int unused_service;
  34069. +
  34070. + /* Signalled when a free slot becomes available. */
  34071. + struct semaphore slot_available_event;
  34072. +
  34073. + struct semaphore slot_remove_event;
  34074. +
  34075. + /* Signalled when a free data slot becomes available. */
  34076. + struct semaphore data_quota_event;
  34077. +
  34078. + /* Incremented when there are bulk transfers which cannot be processed
  34079. + * whilst paused and must be processed on resume */
  34080. + int deferred_bulks;
  34081. +
  34082. + struct state_stats_struct {
  34083. + int slot_stalls;
  34084. + int data_stalls;
  34085. + int ctrl_tx_count;
  34086. + int ctrl_rx_count;
  34087. + int error_count;
  34088. + } stats;
  34089. +
  34090. + VCHIQ_SERVICE_T * services[VCHIQ_MAX_SERVICES];
  34091. + VCHIQ_SERVICE_QUOTA_T service_quotas[VCHIQ_MAX_SERVICES];
  34092. + VCHIQ_SLOT_INFO_T slot_info[VCHIQ_MAX_SLOTS];
  34093. +
  34094. + VCHIQ_PLATFORM_STATE_T platform_state;
  34095. +};
  34096. +
  34097. +struct bulk_waiter {
  34098. + VCHIQ_BULK_T *bulk;
  34099. + struct semaphore event;
  34100. + int actual;
  34101. +};
  34102. +
  34103. +extern spinlock_t bulk_waiter_spinlock;
  34104. +
  34105. +extern int vchiq_core_log_level;
  34106. +extern int vchiq_core_msg_log_level;
  34107. +extern int vchiq_sync_log_level;
  34108. +
  34109. +extern VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
  34110. +
  34111. +extern const char *
  34112. +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state);
  34113. +
  34114. +extern VCHIQ_SLOT_ZERO_T *
  34115. +vchiq_init_slots(void *mem_base, int mem_size);
  34116. +
  34117. +extern VCHIQ_STATUS_T
  34118. +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
  34119. + int is_master);
  34120. +
  34121. +extern VCHIQ_STATUS_T
  34122. +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
  34123. +
  34124. +extern VCHIQ_SERVICE_T *
  34125. +vchiq_add_service_internal(VCHIQ_STATE_T *state,
  34126. + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
  34127. + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term);
  34128. +
  34129. +extern VCHIQ_STATUS_T
  34130. +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id);
  34131. +
  34132. +extern VCHIQ_STATUS_T
  34133. +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd);
  34134. +
  34135. +extern void
  34136. +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service);
  34137. +
  34138. +extern void
  34139. +vchiq_free_service_internal(VCHIQ_SERVICE_T *service);
  34140. +
  34141. +extern VCHIQ_STATUS_T
  34142. +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
  34143. +
  34144. +extern VCHIQ_STATUS_T
  34145. +vchiq_pause_internal(VCHIQ_STATE_T *state);
  34146. +
  34147. +extern VCHIQ_STATUS_T
  34148. +vchiq_resume_internal(VCHIQ_STATE_T *state);
  34149. +
  34150. +extern void
  34151. +remote_event_pollall(VCHIQ_STATE_T *state);
  34152. +
  34153. +extern VCHIQ_STATUS_T
  34154. +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
  34155. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
  34156. + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir);
  34157. +
  34158. +extern void
  34159. +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state);
  34160. +
  34161. +extern void
  34162. +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service);
  34163. +
  34164. +extern void
  34165. +vchiq_loud_error_header(void);
  34166. +
  34167. +extern void
  34168. +vchiq_loud_error_footer(void);
  34169. +
  34170. +extern void
  34171. +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type);
  34172. +
  34173. +static inline VCHIQ_SERVICE_T *
  34174. +handle_to_service(VCHIQ_SERVICE_HANDLE_T handle)
  34175. +{
  34176. + VCHIQ_STATE_T *state = vchiq_states[(handle / VCHIQ_MAX_SERVICES) &
  34177. + (VCHIQ_MAX_STATES - 1)];
  34178. + if (!state)
  34179. + return NULL;
  34180. +
  34181. + return state->services[handle & (VCHIQ_MAX_SERVICES - 1)];
  34182. +}
  34183. +
  34184. +extern VCHIQ_SERVICE_T *
  34185. +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle);
  34186. +
  34187. +extern VCHIQ_SERVICE_T *
  34188. +find_service_by_port(VCHIQ_STATE_T *state, int localport);
  34189. +
  34190. +extern VCHIQ_SERVICE_T *
  34191. +find_service_for_instance(VCHIQ_INSTANCE_T instance,
  34192. + VCHIQ_SERVICE_HANDLE_T handle);
  34193. +
  34194. +extern VCHIQ_SERVICE_T *
  34195. +find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
  34196. + VCHIQ_SERVICE_HANDLE_T handle);
  34197. +
  34198. +extern VCHIQ_SERVICE_T *
  34199. +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
  34200. + int *pidx);
  34201. +
  34202. +extern void
  34203. +lock_service(VCHIQ_SERVICE_T *service);
  34204. +
  34205. +extern void
  34206. +unlock_service(VCHIQ_SERVICE_T *service);
  34207. +
  34208. +/* The following functions are called from vchiq_core, and external
  34209. +** implementations must be provided. */
  34210. +
  34211. +extern VCHIQ_STATUS_T
  34212. +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk,
  34213. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, int dir);
  34214. +
  34215. +extern void
  34216. +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk);
  34217. +
  34218. +extern void
  34219. +vchiq_complete_bulk(VCHIQ_BULK_T *bulk);
  34220. +
  34221. +extern VCHIQ_STATUS_T
  34222. +vchiq_copy_from_user(void *dst, const void *src, int size);
  34223. +
  34224. +extern void
  34225. +remote_event_signal(REMOTE_EVENT_T *event);
  34226. +
  34227. +void
  34228. +vchiq_platform_check_suspend(VCHIQ_STATE_T *state);
  34229. +
  34230. +extern void
  34231. +vchiq_platform_paused(VCHIQ_STATE_T *state);
  34232. +
  34233. +extern VCHIQ_STATUS_T
  34234. +vchiq_platform_resume(VCHIQ_STATE_T *state);
  34235. +
  34236. +extern void
  34237. +vchiq_platform_resumed(VCHIQ_STATE_T *state);
  34238. +
  34239. +extern void
  34240. +vchiq_dump(void *dump_context, const char *str, int len);
  34241. +
  34242. +extern void
  34243. +vchiq_dump_platform_state(void *dump_context);
  34244. +
  34245. +extern void
  34246. +vchiq_dump_platform_instances(void *dump_context);
  34247. +
  34248. +extern void
  34249. +vchiq_dump_platform_service_state(void *dump_context,
  34250. + VCHIQ_SERVICE_T *service);
  34251. +
  34252. +extern VCHIQ_STATUS_T
  34253. +vchiq_use_service_internal(VCHIQ_SERVICE_T *service);
  34254. +
  34255. +extern VCHIQ_STATUS_T
  34256. +vchiq_release_service_internal(VCHIQ_SERVICE_T *service);
  34257. +
  34258. +extern void
  34259. +vchiq_on_remote_use(VCHIQ_STATE_T *state);
  34260. +
  34261. +extern void
  34262. +vchiq_on_remote_release(VCHIQ_STATE_T *state);
  34263. +
  34264. +extern VCHIQ_STATUS_T
  34265. +vchiq_platform_init_state(VCHIQ_STATE_T *state);
  34266. +
  34267. +extern VCHIQ_STATUS_T
  34268. +vchiq_check_service(VCHIQ_SERVICE_T *service);
  34269. +
  34270. +extern void
  34271. +vchiq_on_remote_use_active(VCHIQ_STATE_T *state);
  34272. +
  34273. +extern VCHIQ_STATUS_T
  34274. +vchiq_send_remote_use(VCHIQ_STATE_T *state);
  34275. +
  34276. +extern VCHIQ_STATUS_T
  34277. +vchiq_send_remote_release(VCHIQ_STATE_T *state);
  34278. +
  34279. +extern VCHIQ_STATUS_T
  34280. +vchiq_send_remote_use_active(VCHIQ_STATE_T *state);
  34281. +
  34282. +extern void
  34283. +vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
  34284. + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate);
  34285. +
  34286. +extern void
  34287. +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state);
  34288. +
  34289. +extern void
  34290. +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate);
  34291. +
  34292. +
  34293. +extern void
  34294. +vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
  34295. + size_t numBytes);
  34296. +
  34297. +#endif
  34298. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.c linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.c
  34299. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.c 1969-12-31 18:00:00.000000000 -0600
  34300. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.c 2014-12-03 19:13:38.228418001 -0600
  34301. @@ -0,0 +1,383 @@
  34302. +/**
  34303. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  34304. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  34305. + *
  34306. + * Redistribution and use in source and binary forms, with or without
  34307. + * modification, are permitted provided that the following conditions
  34308. + * are met:
  34309. + * 1. Redistributions of source code must retain the above copyright
  34310. + * notice, this list of conditions, and the following disclaimer,
  34311. + * without modification.
  34312. + * 2. Redistributions in binary form must reproduce the above copyright
  34313. + * notice, this list of conditions and the following disclaimer in the
  34314. + * documentation and/or other materials provided with the distribution.
  34315. + * 3. The names of the above-listed copyright holders may not be used
  34316. + * to endorse or promote products derived from this software without
  34317. + * specific prior written permission.
  34318. + *
  34319. + * ALTERNATIVELY, this software may be distributed under the terms of the
  34320. + * GNU General Public License ("GPL") version 2, as published by the Free
  34321. + * Software Foundation.
  34322. + *
  34323. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  34324. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  34325. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34326. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34327. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  34328. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  34329. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34330. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34331. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34332. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34333. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34334. + */
  34335. +
  34336. +
  34337. +#include <linux/debugfs.h>
  34338. +#include "vchiq_core.h"
  34339. +#include "vchiq_arm.h"
  34340. +#include "vchiq_debugfs.h"
  34341. +
  34342. +#ifdef CONFIG_DEBUG_FS
  34343. +
  34344. +/****************************************************************************
  34345. +*
  34346. +* log category entries
  34347. +*
  34348. +***************************************************************************/
  34349. +#define DEBUGFS_WRITE_BUF_SIZE 256
  34350. +
  34351. +#define VCHIQ_LOG_ERROR_STR "error"
  34352. +#define VCHIQ_LOG_WARNING_STR "warning"
  34353. +#define VCHIQ_LOG_INFO_STR "info"
  34354. +#define VCHIQ_LOG_TRACE_STR "trace"
  34355. +
  34356. +
  34357. +/* Top-level debug info */
  34358. +struct vchiq_debugfs_info {
  34359. + /* Global 'vchiq' debugfs entry used by all instances */
  34360. + struct dentry *vchiq_cfg_dir;
  34361. +
  34362. + /* one entry per client process */
  34363. + struct dentry *clients;
  34364. +
  34365. + /* log categories */
  34366. + struct dentry *log_categories;
  34367. +};
  34368. +
  34369. +static struct vchiq_debugfs_info debugfs_info;
  34370. +
  34371. +/* Log category debugfs entries */
  34372. +struct vchiq_debugfs_log_entry {
  34373. + const char *name;
  34374. + int *plevel;
  34375. + struct dentry *dir;
  34376. +};
  34377. +
  34378. +static struct vchiq_debugfs_log_entry vchiq_debugfs_log_entries[] = {
  34379. + { "core", &vchiq_core_log_level },
  34380. + { "msg", &vchiq_core_msg_log_level },
  34381. + { "sync", &vchiq_sync_log_level },
  34382. + { "susp", &vchiq_susp_log_level },
  34383. + { "arm", &vchiq_arm_log_level },
  34384. +};
  34385. +static int n_log_entries =
  34386. + sizeof(vchiq_debugfs_log_entries)/sizeof(vchiq_debugfs_log_entries[0]);
  34387. +
  34388. +
  34389. +static struct dentry *vchiq_clients_top(void);
  34390. +static struct dentry *vchiq_debugfs_top(void);
  34391. +
  34392. +static int debugfs_log_show(struct seq_file *f, void *offset)
  34393. +{
  34394. + int *levp = f->private;
  34395. + char *log_value = NULL;
  34396. +
  34397. + switch (*levp) {
  34398. + case VCHIQ_LOG_ERROR:
  34399. + log_value = VCHIQ_LOG_ERROR_STR;
  34400. + break;
  34401. + case VCHIQ_LOG_WARNING:
  34402. + log_value = VCHIQ_LOG_WARNING_STR;
  34403. + break;
  34404. + case VCHIQ_LOG_INFO:
  34405. + log_value = VCHIQ_LOG_INFO_STR;
  34406. + break;
  34407. + case VCHIQ_LOG_TRACE:
  34408. + log_value = VCHIQ_LOG_TRACE_STR;
  34409. + break;
  34410. + default:
  34411. + break;
  34412. + }
  34413. +
  34414. + seq_printf(f, "%s\n", log_value ? log_value : "(null)");
  34415. +
  34416. + return 0;
  34417. +}
  34418. +
  34419. +static int debugfs_log_open(struct inode *inode, struct file *file)
  34420. +{
  34421. + return single_open(file, debugfs_log_show, inode->i_private);
  34422. +}
  34423. +
  34424. +static int debugfs_log_write(struct file *file,
  34425. + const char __user *buffer,
  34426. + size_t count, loff_t *ppos)
  34427. +{
  34428. + struct seq_file *f = (struct seq_file *)file->private_data;
  34429. + int *levp = f->private;
  34430. + char kbuf[DEBUGFS_WRITE_BUF_SIZE + 1];
  34431. +
  34432. + memset(kbuf, 0, DEBUGFS_WRITE_BUF_SIZE + 1);
  34433. + if (count >= DEBUGFS_WRITE_BUF_SIZE)
  34434. + count = DEBUGFS_WRITE_BUF_SIZE;
  34435. +
  34436. + if (copy_from_user(kbuf, buffer, count) != 0)
  34437. + return -EFAULT;
  34438. + kbuf[count - 1] = 0;
  34439. +
  34440. + if (strncmp("error", kbuf, strlen("error")) == 0)
  34441. + *levp = VCHIQ_LOG_ERROR;
  34442. + else if (strncmp("warning", kbuf, strlen("warning")) == 0)
  34443. + *levp = VCHIQ_LOG_WARNING;
  34444. + else if (strncmp("info", kbuf, strlen("info")) == 0)
  34445. + *levp = VCHIQ_LOG_INFO;
  34446. + else if (strncmp("trace", kbuf, strlen("trace")) == 0)
  34447. + *levp = VCHIQ_LOG_TRACE;
  34448. + else
  34449. + *levp = VCHIQ_LOG_DEFAULT;
  34450. +
  34451. + *ppos += count;
  34452. +
  34453. + return count;
  34454. +}
  34455. +
  34456. +static const struct file_operations debugfs_log_fops = {
  34457. + .owner = THIS_MODULE,
  34458. + .open = debugfs_log_open,
  34459. + .write = debugfs_log_write,
  34460. + .read = seq_read,
  34461. + .llseek = seq_lseek,
  34462. + .release = single_release,
  34463. +};
  34464. +
  34465. +/* create an entry under <debugfs>/vchiq/log for each log category */
  34466. +static int vchiq_debugfs_create_log_entries(struct dentry *top)
  34467. +{
  34468. + struct dentry *dir;
  34469. + size_t i;
  34470. + int ret = 0;
  34471. + dir = debugfs_create_dir("log", vchiq_debugfs_top());
  34472. + if (!dir)
  34473. + return -ENOMEM;
  34474. + debugfs_info.log_categories = dir;
  34475. +
  34476. + for (i = 0; i < n_log_entries; i++) {
  34477. + void *levp = (void *)vchiq_debugfs_log_entries[i].plevel;
  34478. + dir = debugfs_create_file(vchiq_debugfs_log_entries[i].name,
  34479. + 0644,
  34480. + debugfs_info.log_categories,
  34481. + levp,
  34482. + &debugfs_log_fops);
  34483. + if (!dir) {
  34484. + ret = -ENOMEM;
  34485. + break;
  34486. + }
  34487. +
  34488. + vchiq_debugfs_log_entries[i].dir = dir;
  34489. + }
  34490. + return ret;
  34491. +}
  34492. +
  34493. +static int debugfs_usecount_show(struct seq_file *f, void *offset)
  34494. +{
  34495. + VCHIQ_INSTANCE_T instance = f->private;
  34496. + int use_count;
  34497. +
  34498. + use_count = vchiq_instance_get_use_count(instance);
  34499. + seq_printf(f, "%d\n", use_count);
  34500. +
  34501. + return 0;
  34502. +}
  34503. +
  34504. +static int debugfs_usecount_open(struct inode *inode, struct file *file)
  34505. +{
  34506. + return single_open(file, debugfs_usecount_show, inode->i_private);
  34507. +}
  34508. +
  34509. +static const struct file_operations debugfs_usecount_fops = {
  34510. + .owner = THIS_MODULE,
  34511. + .open = debugfs_usecount_open,
  34512. + .read = seq_read,
  34513. + .llseek = seq_lseek,
  34514. + .release = single_release,
  34515. +};
  34516. +
  34517. +static int debugfs_trace_show(struct seq_file *f, void *offset)
  34518. +{
  34519. + VCHIQ_INSTANCE_T instance = f->private;
  34520. + int trace;
  34521. +
  34522. + trace = vchiq_instance_get_trace(instance);
  34523. + seq_printf(f, "%s\n", trace ? "Y" : "N");
  34524. +
  34525. + return 0;
  34526. +}
  34527. +
  34528. +static int debugfs_trace_open(struct inode *inode, struct file *file)
  34529. +{
  34530. + return single_open(file, debugfs_trace_show, inode->i_private);
  34531. +}
  34532. +
  34533. +static int debugfs_trace_write(struct file *file,
  34534. + const char __user *buffer,
  34535. + size_t count, loff_t *ppos)
  34536. +{
  34537. + struct seq_file *f = (struct seq_file *)file->private_data;
  34538. + VCHIQ_INSTANCE_T instance = f->private;
  34539. + char firstchar;
  34540. +
  34541. + if (copy_from_user(&firstchar, buffer, 1) != 0)
  34542. + return -EFAULT;
  34543. +
  34544. + switch (firstchar) {
  34545. + case 'Y':
  34546. + case 'y':
  34547. + case '1':
  34548. + vchiq_instance_set_trace(instance, 1);
  34549. + break;
  34550. + case 'N':
  34551. + case 'n':
  34552. + case '0':
  34553. + vchiq_instance_set_trace(instance, 0);
  34554. + break;
  34555. + default:
  34556. + break;
  34557. + }
  34558. +
  34559. + *ppos += count;
  34560. +
  34561. + return count;
  34562. +}
  34563. +
  34564. +static const struct file_operations debugfs_trace_fops = {
  34565. + .owner = THIS_MODULE,
  34566. + .open = debugfs_trace_open,
  34567. + .write = debugfs_trace_write,
  34568. + .read = seq_read,
  34569. + .llseek = seq_lseek,
  34570. + .release = single_release,
  34571. +};
  34572. +
  34573. +/* add an instance (process) to the debugfs entries */
  34574. +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
  34575. +{
  34576. + char pidstr[16];
  34577. + struct dentry *top, *use_count, *trace;
  34578. + struct dentry *clients = vchiq_clients_top();
  34579. +
  34580. + snprintf(pidstr, sizeof(pidstr), "%d",
  34581. + vchiq_instance_get_pid(instance));
  34582. +
  34583. + top = debugfs_create_dir(pidstr, clients);
  34584. + if (!top)
  34585. + goto fail_top;
  34586. +
  34587. + use_count = debugfs_create_file("use_count",
  34588. + 0444, top,
  34589. + instance,
  34590. + &debugfs_usecount_fops);
  34591. + if (!use_count)
  34592. + goto fail_use_count;
  34593. +
  34594. + trace = debugfs_create_file("trace",
  34595. + 0644, top,
  34596. + instance,
  34597. + &debugfs_trace_fops);
  34598. + if (!trace)
  34599. + goto fail_trace;
  34600. +
  34601. + vchiq_instance_get_debugfs_node(instance)->dentry = top;
  34602. +
  34603. + return 0;
  34604. +
  34605. +fail_trace:
  34606. + debugfs_remove(use_count);
  34607. +fail_use_count:
  34608. + debugfs_remove(top);
  34609. +fail_top:
  34610. + return -ENOMEM;
  34611. +}
  34612. +
  34613. +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
  34614. +{
  34615. + VCHIQ_DEBUGFS_NODE_T *node = vchiq_instance_get_debugfs_node(instance);
  34616. + debugfs_remove_recursive(node->dentry);
  34617. +}
  34618. +
  34619. +
  34620. +int vchiq_debugfs_init(void)
  34621. +{
  34622. + BUG_ON(debugfs_info.vchiq_cfg_dir != NULL);
  34623. +
  34624. + debugfs_info.vchiq_cfg_dir = debugfs_create_dir("vchiq", NULL);
  34625. + if (debugfs_info.vchiq_cfg_dir == NULL)
  34626. + goto fail;
  34627. +
  34628. + debugfs_info.clients = debugfs_create_dir("clients",
  34629. + vchiq_debugfs_top());
  34630. + if (!debugfs_info.clients)
  34631. + goto fail;
  34632. +
  34633. + if (vchiq_debugfs_create_log_entries(vchiq_debugfs_top()) != 0)
  34634. + goto fail;
  34635. +
  34636. + return 0;
  34637. +
  34638. +fail:
  34639. + vchiq_debugfs_deinit();
  34640. + vchiq_log_error(vchiq_arm_log_level,
  34641. + "%s: failed to create debugfs directory",
  34642. + __func__);
  34643. +
  34644. + return -ENOMEM;
  34645. +}
  34646. +
  34647. +/* remove all the debugfs entries */
  34648. +void vchiq_debugfs_deinit(void)
  34649. +{
  34650. + debugfs_remove_recursive(vchiq_debugfs_top());
  34651. +}
  34652. +
  34653. +static struct dentry *vchiq_clients_top(void)
  34654. +{
  34655. + return debugfs_info.clients;
  34656. +}
  34657. +
  34658. +static struct dentry *vchiq_debugfs_top(void)
  34659. +{
  34660. + BUG_ON(debugfs_info.vchiq_cfg_dir == NULL);
  34661. + return debugfs_info.vchiq_cfg_dir;
  34662. +}
  34663. +
  34664. +#else /* CONFIG_DEBUG_FS */
  34665. +
  34666. +int vchiq_debugfs_init(void)
  34667. +{
  34668. + return 0;
  34669. +}
  34670. +
  34671. +void vchiq_debugfs_deinit(void)
  34672. +{
  34673. +}
  34674. +
  34675. +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
  34676. +{
  34677. + return 0;
  34678. +}
  34679. +
  34680. +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
  34681. +{
  34682. +}
  34683. +
  34684. +#endif /* CONFIG_DEBUG_FS */
  34685. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.h
  34686. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.h 1969-12-31 18:00:00.000000000 -0600
  34687. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.h 2014-12-03 19:13:38.228418001 -0600
  34688. @@ -0,0 +1,52 @@
  34689. +/**
  34690. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  34691. + *
  34692. + * Redistribution and use in source and binary forms, with or without
  34693. + * modification, are permitted provided that the following conditions
  34694. + * are met:
  34695. + * 1. Redistributions of source code must retain the above copyright
  34696. + * notice, this list of conditions, and the following disclaimer,
  34697. + * without modification.
  34698. + * 2. Redistributions in binary form must reproduce the above copyright
  34699. + * notice, this list of conditions and the following disclaimer in the
  34700. + * documentation and/or other materials provided with the distribution.
  34701. + * 3. The names of the above-listed copyright holders may not be used
  34702. + * to endorse or promote products derived from this software without
  34703. + * specific prior written permission.
  34704. + *
  34705. + * ALTERNATIVELY, this software may be distributed under the terms of the
  34706. + * GNU General Public License ("GPL") version 2, as published by the Free
  34707. + * Software Foundation.
  34708. + *
  34709. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  34710. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  34711. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34712. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34713. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  34714. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  34715. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34716. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34717. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34718. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34719. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34720. + */
  34721. +
  34722. +#ifndef VCHIQ_DEBUGFS_H
  34723. +#define VCHIQ_DEBUGFS_H
  34724. +
  34725. +#include "vchiq_core.h"
  34726. +
  34727. +typedef struct vchiq_debugfs_node_struct
  34728. +{
  34729. + struct dentry *dentry;
  34730. +} VCHIQ_DEBUGFS_NODE_T;
  34731. +
  34732. +int vchiq_debugfs_init(void);
  34733. +
  34734. +void vchiq_debugfs_deinit(void);
  34735. +
  34736. +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance);
  34737. +
  34738. +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance);
  34739. +
  34740. +#endif /* VCHIQ_DEBUGFS_H */
  34741. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion
  34742. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion 1969-12-31 18:00:00.000000000 -0600
  34743. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion 2014-12-03 19:13:38.228418001 -0600
  34744. @@ -0,0 +1,87 @@
  34745. +#!/usr/bin/perl -w
  34746. +
  34747. +use strict;
  34748. +
  34749. +#
  34750. +# Generate a version from available information
  34751. +#
  34752. +
  34753. +my $prefix = shift @ARGV;
  34754. +my $root = shift @ARGV;
  34755. +
  34756. +
  34757. +if ( not defined $root ) {
  34758. + die "usage: $0 prefix root-dir\n";
  34759. +}
  34760. +
  34761. +if ( ! -d $root ) {
  34762. + die "root directory $root not found\n";
  34763. +}
  34764. +
  34765. +my $version = "unknown";
  34766. +my $tainted = "";
  34767. +
  34768. +if ( -d "$root/.git" ) {
  34769. + # attempt to work out git version. only do so
  34770. + # on a linux build host, as cygwin builds are
  34771. + # already slow enough
  34772. +
  34773. + if ( -f "/usr/bin/git" || -f "/usr/local/bin/git" ) {
  34774. + if (not open(F, "git --git-dir $root/.git rev-parse --verify HEAD|")) {
  34775. + $version = "no git version";
  34776. + }
  34777. + else {
  34778. + $version = <F>;
  34779. + $version =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  34780. + $version =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  34781. + }
  34782. +
  34783. + if (open(G, "git --git-dir $root/.git status --porcelain|")) {
  34784. + $tainted = <G>;
  34785. + $tainted =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  34786. + $tainted =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  34787. + if (length $tainted) {
  34788. + $version = join ' ', $version, "(tainted)";
  34789. + }
  34790. + else {
  34791. + $version = join ' ', $version, "(clean)";
  34792. + }
  34793. + }
  34794. + }
  34795. +}
  34796. +
  34797. +my $hostname = `hostname`;
  34798. +$hostname =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  34799. +$hostname =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  34800. +
  34801. +
  34802. +print STDERR "Version $version\n";
  34803. +print <<EOF;
  34804. +#include "${prefix}_build_info.h"
  34805. +#include <linux/broadcom/vc_debug_sym.h>
  34806. +
  34807. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_hostname, "$hostname" );
  34808. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_version, "$version" );
  34809. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_time, __TIME__ );
  34810. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_date, __DATE__ );
  34811. +
  34812. +const char *vchiq_get_build_hostname( void )
  34813. +{
  34814. + return vchiq_build_hostname;
  34815. +}
  34816. +
  34817. +const char *vchiq_get_build_version( void )
  34818. +{
  34819. + return vchiq_build_version;
  34820. +}
  34821. +
  34822. +const char *vchiq_get_build_date( void )
  34823. +{
  34824. + return vchiq_build_date;
  34825. +}
  34826. +
  34827. +const char *vchiq_get_build_time( void )
  34828. +{
  34829. + return vchiq_build_time;
  34830. +}
  34831. +EOF
  34832. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h
  34833. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h 1969-12-31 18:00:00.000000000 -0600
  34834. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h 2014-12-03 19:13:38.224418001 -0600
  34835. @@ -0,0 +1,40 @@
  34836. +/**
  34837. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  34838. + *
  34839. + * Redistribution and use in source and binary forms, with or without
  34840. + * modification, are permitted provided that the following conditions
  34841. + * are met:
  34842. + * 1. Redistributions of source code must retain the above copyright
  34843. + * notice, this list of conditions, and the following disclaimer,
  34844. + * without modification.
  34845. + * 2. Redistributions in binary form must reproduce the above copyright
  34846. + * notice, this list of conditions and the following disclaimer in the
  34847. + * documentation and/or other materials provided with the distribution.
  34848. + * 3. The names of the above-listed copyright holders may not be used
  34849. + * to endorse or promote products derived from this software without
  34850. + * specific prior written permission.
  34851. + *
  34852. + * ALTERNATIVELY, this software may be distributed under the terms of the
  34853. + * GNU General Public License ("GPL") version 2, as published by the Free
  34854. + * Software Foundation.
  34855. + *
  34856. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  34857. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  34858. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34859. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34860. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  34861. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  34862. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34863. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34864. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34865. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34866. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34867. + */
  34868. +
  34869. +#ifndef VCHIQ_VCHIQ_H
  34870. +#define VCHIQ_VCHIQ_H
  34871. +
  34872. +#include "vchiq_if.h"
  34873. +#include "vchiq_util.h"
  34874. +
  34875. +#endif
  34876. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h
  34877. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h 1969-12-31 18:00:00.000000000 -0600
  34878. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h 2014-12-03 19:13:38.228418001 -0600
  34879. @@ -0,0 +1,189 @@
  34880. +/**
  34881. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  34882. + *
  34883. + * Redistribution and use in source and binary forms, with or without
  34884. + * modification, are permitted provided that the following conditions
  34885. + * are met:
  34886. + * 1. Redistributions of source code must retain the above copyright
  34887. + * notice, this list of conditions, and the following disclaimer,
  34888. + * without modification.
  34889. + * 2. Redistributions in binary form must reproduce the above copyright
  34890. + * notice, this list of conditions and the following disclaimer in the
  34891. + * documentation and/or other materials provided with the distribution.
  34892. + * 3. The names of the above-listed copyright holders may not be used
  34893. + * to endorse or promote products derived from this software without
  34894. + * specific prior written permission.
  34895. + *
  34896. + * ALTERNATIVELY, this software may be distributed under the terms of the
  34897. + * GNU General Public License ("GPL") version 2, as published by the Free
  34898. + * Software Foundation.
  34899. + *
  34900. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  34901. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  34902. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34903. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34904. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  34905. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  34906. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34907. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34908. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34909. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34910. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34911. + */
  34912. +
  34913. +#ifndef VCHIQ_IF_H
  34914. +#define VCHIQ_IF_H
  34915. +
  34916. +#include "interface/vchi/vchi_mh.h"
  34917. +
  34918. +#define VCHIQ_SERVICE_HANDLE_INVALID 0
  34919. +
  34920. +#define VCHIQ_SLOT_SIZE 4096
  34921. +#define VCHIQ_MAX_MSG_SIZE (VCHIQ_SLOT_SIZE - sizeof(VCHIQ_HEADER_T))
  34922. +#define VCHIQ_CHANNEL_SIZE VCHIQ_MAX_MSG_SIZE /* For backwards compatibility */
  34923. +
  34924. +#define VCHIQ_MAKE_FOURCC(x0, x1, x2, x3) \
  34925. + (((x0) << 24) | ((x1) << 16) | ((x2) << 8) | (x3))
  34926. +#define VCHIQ_GET_SERVICE_USERDATA(service) vchiq_get_service_userdata(service)
  34927. +#define VCHIQ_GET_SERVICE_FOURCC(service) vchiq_get_service_fourcc(service)
  34928. +
  34929. +typedef enum {
  34930. + VCHIQ_SERVICE_OPENED, /* service, -, - */
  34931. + VCHIQ_SERVICE_CLOSED, /* service, -, - */
  34932. + VCHIQ_MESSAGE_AVAILABLE, /* service, header, - */
  34933. + VCHIQ_BULK_TRANSMIT_DONE, /* service, -, bulk_userdata */
  34934. + VCHIQ_BULK_RECEIVE_DONE, /* service, -, bulk_userdata */
  34935. + VCHIQ_BULK_TRANSMIT_ABORTED, /* service, -, bulk_userdata */
  34936. + VCHIQ_BULK_RECEIVE_ABORTED /* service, -, bulk_userdata */
  34937. +} VCHIQ_REASON_T;
  34938. +
  34939. +typedef enum {
  34940. + VCHIQ_ERROR = -1,
  34941. + VCHIQ_SUCCESS = 0,
  34942. + VCHIQ_RETRY = 1
  34943. +} VCHIQ_STATUS_T;
  34944. +
  34945. +typedef enum {
  34946. + VCHIQ_BULK_MODE_CALLBACK,
  34947. + VCHIQ_BULK_MODE_BLOCKING,
  34948. + VCHIQ_BULK_MODE_NOCALLBACK,
  34949. + VCHIQ_BULK_MODE_WAITING /* Reserved for internal use */
  34950. +} VCHIQ_BULK_MODE_T;
  34951. +
  34952. +typedef enum {
  34953. + VCHIQ_SERVICE_OPTION_AUTOCLOSE,
  34954. + VCHIQ_SERVICE_OPTION_SLOT_QUOTA,
  34955. + VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA,
  34956. + VCHIQ_SERVICE_OPTION_SYNCHRONOUS,
  34957. + VCHIQ_SERVICE_OPTION_TRACE
  34958. +} VCHIQ_SERVICE_OPTION_T;
  34959. +
  34960. +typedef struct vchiq_header_struct {
  34961. + /* The message identifier - opaque to applications. */
  34962. + int msgid;
  34963. +
  34964. + /* Size of message data. */
  34965. + unsigned int size;
  34966. +
  34967. + char data[0]; /* message */
  34968. +} VCHIQ_HEADER_T;
  34969. +
  34970. +typedef struct {
  34971. + const void *data;
  34972. + unsigned int size;
  34973. +} VCHIQ_ELEMENT_T;
  34974. +
  34975. +typedef unsigned int VCHIQ_SERVICE_HANDLE_T;
  34976. +
  34977. +typedef VCHIQ_STATUS_T (*VCHIQ_CALLBACK_T)(VCHIQ_REASON_T, VCHIQ_HEADER_T *,
  34978. + VCHIQ_SERVICE_HANDLE_T, void *);
  34979. +
  34980. +typedef struct vchiq_service_base_struct {
  34981. + int fourcc;
  34982. + VCHIQ_CALLBACK_T callback;
  34983. + void *userdata;
  34984. +} VCHIQ_SERVICE_BASE_T;
  34985. +
  34986. +typedef struct vchiq_service_params_struct {
  34987. + int fourcc;
  34988. + VCHIQ_CALLBACK_T callback;
  34989. + void *userdata;
  34990. + short version; /* Increment for non-trivial changes */
  34991. + short version_min; /* Update for incompatible changes */
  34992. +} VCHIQ_SERVICE_PARAMS_T;
  34993. +
  34994. +typedef struct vchiq_config_struct {
  34995. + unsigned int max_msg_size;
  34996. + unsigned int bulk_threshold; /* The message size above which it
  34997. + is better to use a bulk transfer
  34998. + (<= max_msg_size) */
  34999. + unsigned int max_outstanding_bulks;
  35000. + unsigned int max_services;
  35001. + short version; /* The version of VCHIQ */
  35002. + short version_min; /* The minimum compatible version of VCHIQ */
  35003. +} VCHIQ_CONFIG_T;
  35004. +
  35005. +typedef struct vchiq_instance_struct *VCHIQ_INSTANCE_T;
  35006. +typedef void (*VCHIQ_REMOTE_USE_CALLBACK_T)(void *cb_arg);
  35007. +
  35008. +extern VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *pinstance);
  35009. +extern VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance);
  35010. +extern VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance);
  35011. +extern VCHIQ_STATUS_T vchiq_add_service(VCHIQ_INSTANCE_T instance,
  35012. + const VCHIQ_SERVICE_PARAMS_T *params,
  35013. + VCHIQ_SERVICE_HANDLE_T *pservice);
  35014. +extern VCHIQ_STATUS_T vchiq_open_service(VCHIQ_INSTANCE_T instance,
  35015. + const VCHIQ_SERVICE_PARAMS_T *params,
  35016. + VCHIQ_SERVICE_HANDLE_T *pservice);
  35017. +extern VCHIQ_STATUS_T vchiq_close_service(VCHIQ_SERVICE_HANDLE_T service);
  35018. +extern VCHIQ_STATUS_T vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T service);
  35019. +extern VCHIQ_STATUS_T vchiq_use_service(VCHIQ_SERVICE_HANDLE_T service);
  35020. +extern VCHIQ_STATUS_T vchiq_use_service_no_resume(
  35021. + VCHIQ_SERVICE_HANDLE_T service);
  35022. +extern VCHIQ_STATUS_T vchiq_release_service(VCHIQ_SERVICE_HANDLE_T service);
  35023. +
  35024. +extern VCHIQ_STATUS_T vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T service,
  35025. + const VCHIQ_ELEMENT_T *elements, unsigned int count);
  35026. +extern void vchiq_release_message(VCHIQ_SERVICE_HANDLE_T service,
  35027. + VCHIQ_HEADER_T *header);
  35028. +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
  35029. + const void *data, unsigned int size, void *userdata);
  35030. +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
  35031. + void *data, unsigned int size, void *userdata);
  35032. +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit_handle(
  35033. + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
  35034. + const void *offset, unsigned int size, void *userdata);
  35035. +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive_handle(
  35036. + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
  35037. + void *offset, unsigned int size, void *userdata);
  35038. +extern VCHIQ_STATUS_T vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
  35039. + const void *data, unsigned int size, void *userdata,
  35040. + VCHIQ_BULK_MODE_T mode);
  35041. +extern VCHIQ_STATUS_T vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
  35042. + void *data, unsigned int size, void *userdata,
  35043. + VCHIQ_BULK_MODE_T mode);
  35044. +extern VCHIQ_STATUS_T vchiq_bulk_transmit_handle(VCHIQ_SERVICE_HANDLE_T service,
  35045. + VCHI_MEM_HANDLE_T handle, const void *offset, unsigned int size,
  35046. + void *userdata, VCHIQ_BULK_MODE_T mode);
  35047. +extern VCHIQ_STATUS_T vchiq_bulk_receive_handle(VCHIQ_SERVICE_HANDLE_T service,
  35048. + VCHI_MEM_HANDLE_T handle, void *offset, unsigned int size,
  35049. + void *userdata, VCHIQ_BULK_MODE_T mode);
  35050. +extern int vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T service);
  35051. +extern void *vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T service);
  35052. +extern int vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T service);
  35053. +extern VCHIQ_STATUS_T vchiq_get_config(VCHIQ_INSTANCE_T instance,
  35054. + int config_size, VCHIQ_CONFIG_T *pconfig);
  35055. +extern VCHIQ_STATUS_T vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T service,
  35056. + VCHIQ_SERVICE_OPTION_T option, int value);
  35057. +
  35058. +extern VCHIQ_STATUS_T vchiq_remote_use(VCHIQ_INSTANCE_T instance,
  35059. + VCHIQ_REMOTE_USE_CALLBACK_T callback, void *cb_arg);
  35060. +extern VCHIQ_STATUS_T vchiq_remote_release(VCHIQ_INSTANCE_T instance);
  35061. +
  35062. +extern VCHIQ_STATUS_T vchiq_dump_phys_mem(VCHIQ_SERVICE_HANDLE_T service,
  35063. + void *ptr, size_t num_bytes);
  35064. +
  35065. +extern VCHIQ_STATUS_T vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle,
  35066. + short *peer_version);
  35067. +
  35068. +#endif /* VCHIQ_IF_H */
  35069. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
  35070. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h 1969-12-31 18:00:00.000000000 -0600
  35071. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h 2014-12-03 19:13:38.228418001 -0600
  35072. @@ -0,0 +1,131 @@
  35073. +/**
  35074. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  35075. + *
  35076. + * Redistribution and use in source and binary forms, with or without
  35077. + * modification, are permitted provided that the following conditions
  35078. + * are met:
  35079. + * 1. Redistributions of source code must retain the above copyright
  35080. + * notice, this list of conditions, and the following disclaimer,
  35081. + * without modification.
  35082. + * 2. Redistributions in binary form must reproduce the above copyright
  35083. + * notice, this list of conditions and the following disclaimer in the
  35084. + * documentation and/or other materials provided with the distribution.
  35085. + * 3. The names of the above-listed copyright holders may not be used
  35086. + * to endorse or promote products derived from this software without
  35087. + * specific prior written permission.
  35088. + *
  35089. + * ALTERNATIVELY, this software may be distributed under the terms of the
  35090. + * GNU General Public License ("GPL") version 2, as published by the Free
  35091. + * Software Foundation.
  35092. + *
  35093. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  35094. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  35095. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35096. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35097. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  35098. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35099. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35100. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35101. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35102. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35103. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35104. + */
  35105. +
  35106. +#ifndef VCHIQ_IOCTLS_H
  35107. +#define VCHIQ_IOCTLS_H
  35108. +
  35109. +#include <linux/ioctl.h>
  35110. +#include "vchiq_if.h"
  35111. +
  35112. +#define VCHIQ_IOC_MAGIC 0xc4
  35113. +#define VCHIQ_INVALID_HANDLE (~0)
  35114. +
  35115. +typedef struct {
  35116. + VCHIQ_SERVICE_PARAMS_T params;
  35117. + int is_open;
  35118. + int is_vchi;
  35119. + unsigned int handle; /* OUT */
  35120. +} VCHIQ_CREATE_SERVICE_T;
  35121. +
  35122. +typedef struct {
  35123. + unsigned int handle;
  35124. + unsigned int count;
  35125. + const VCHIQ_ELEMENT_T *elements;
  35126. +} VCHIQ_QUEUE_MESSAGE_T;
  35127. +
  35128. +typedef struct {
  35129. + unsigned int handle;
  35130. + void *data;
  35131. + unsigned int size;
  35132. + void *userdata;
  35133. + VCHIQ_BULK_MODE_T mode;
  35134. +} VCHIQ_QUEUE_BULK_TRANSFER_T;
  35135. +
  35136. +typedef struct {
  35137. + VCHIQ_REASON_T reason;
  35138. + VCHIQ_HEADER_T *header;
  35139. + void *service_userdata;
  35140. + void *bulk_userdata;
  35141. +} VCHIQ_COMPLETION_DATA_T;
  35142. +
  35143. +typedef struct {
  35144. + unsigned int count;
  35145. + VCHIQ_COMPLETION_DATA_T *buf;
  35146. + unsigned int msgbufsize;
  35147. + unsigned int msgbufcount; /* IN/OUT */
  35148. + void **msgbufs;
  35149. +} VCHIQ_AWAIT_COMPLETION_T;
  35150. +
  35151. +typedef struct {
  35152. + unsigned int handle;
  35153. + int blocking;
  35154. + unsigned int bufsize;
  35155. + void *buf;
  35156. +} VCHIQ_DEQUEUE_MESSAGE_T;
  35157. +
  35158. +typedef struct {
  35159. + unsigned int config_size;
  35160. + VCHIQ_CONFIG_T *pconfig;
  35161. +} VCHIQ_GET_CONFIG_T;
  35162. +
  35163. +typedef struct {
  35164. + unsigned int handle;
  35165. + VCHIQ_SERVICE_OPTION_T option;
  35166. + int value;
  35167. +} VCHIQ_SET_SERVICE_OPTION_T;
  35168. +
  35169. +typedef struct {
  35170. + void *virt_addr;
  35171. + size_t num_bytes;
  35172. +} VCHIQ_DUMP_MEM_T;
  35173. +
  35174. +#define VCHIQ_IOC_CONNECT _IO(VCHIQ_IOC_MAGIC, 0)
  35175. +#define VCHIQ_IOC_SHUTDOWN _IO(VCHIQ_IOC_MAGIC, 1)
  35176. +#define VCHIQ_IOC_CREATE_SERVICE \
  35177. + _IOWR(VCHIQ_IOC_MAGIC, 2, VCHIQ_CREATE_SERVICE_T)
  35178. +#define VCHIQ_IOC_REMOVE_SERVICE _IO(VCHIQ_IOC_MAGIC, 3)
  35179. +#define VCHIQ_IOC_QUEUE_MESSAGE \
  35180. + _IOW(VCHIQ_IOC_MAGIC, 4, VCHIQ_QUEUE_MESSAGE_T)
  35181. +#define VCHIQ_IOC_QUEUE_BULK_TRANSMIT \
  35182. + _IOWR(VCHIQ_IOC_MAGIC, 5, VCHIQ_QUEUE_BULK_TRANSFER_T)
  35183. +#define VCHIQ_IOC_QUEUE_BULK_RECEIVE \
  35184. + _IOWR(VCHIQ_IOC_MAGIC, 6, VCHIQ_QUEUE_BULK_TRANSFER_T)
  35185. +#define VCHIQ_IOC_AWAIT_COMPLETION \
  35186. + _IOWR(VCHIQ_IOC_MAGIC, 7, VCHIQ_AWAIT_COMPLETION_T)
  35187. +#define VCHIQ_IOC_DEQUEUE_MESSAGE \
  35188. + _IOWR(VCHIQ_IOC_MAGIC, 8, VCHIQ_DEQUEUE_MESSAGE_T)
  35189. +#define VCHIQ_IOC_GET_CLIENT_ID _IO(VCHIQ_IOC_MAGIC, 9)
  35190. +#define VCHIQ_IOC_GET_CONFIG \
  35191. + _IOWR(VCHIQ_IOC_MAGIC, 10, VCHIQ_GET_CONFIG_T)
  35192. +#define VCHIQ_IOC_CLOSE_SERVICE _IO(VCHIQ_IOC_MAGIC, 11)
  35193. +#define VCHIQ_IOC_USE_SERVICE _IO(VCHIQ_IOC_MAGIC, 12)
  35194. +#define VCHIQ_IOC_RELEASE_SERVICE _IO(VCHIQ_IOC_MAGIC, 13)
  35195. +#define VCHIQ_IOC_SET_SERVICE_OPTION \
  35196. + _IOW(VCHIQ_IOC_MAGIC, 14, VCHIQ_SET_SERVICE_OPTION_T)
  35197. +#define VCHIQ_IOC_DUMP_PHYS_MEM \
  35198. + _IOW(VCHIQ_IOC_MAGIC, 15, VCHIQ_DUMP_MEM_T)
  35199. +#define VCHIQ_IOC_LIB_VERSION _IO(VCHIQ_IOC_MAGIC, 16)
  35200. +#define VCHIQ_IOC_CLOSE_DELIVERED _IO(VCHIQ_IOC_MAGIC, 17)
  35201. +#define VCHIQ_IOC_MAX 17
  35202. +
  35203. +#endif
  35204. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c
  35205. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c 1969-12-31 18:00:00.000000000 -0600
  35206. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c 2014-12-03 19:13:38.228418001 -0600
  35207. @@ -0,0 +1,458 @@
  35208. +/**
  35209. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  35210. + *
  35211. + * Redistribution and use in source and binary forms, with or without
  35212. + * modification, are permitted provided that the following conditions
  35213. + * are met:
  35214. + * 1. Redistributions of source code must retain the above copyright
  35215. + * notice, this list of conditions, and the following disclaimer,
  35216. + * without modification.
  35217. + * 2. Redistributions in binary form must reproduce the above copyright
  35218. + * notice, this list of conditions and the following disclaimer in the
  35219. + * documentation and/or other materials provided with the distribution.
  35220. + * 3. The names of the above-listed copyright holders may not be used
  35221. + * to endorse or promote products derived from this software without
  35222. + * specific prior written permission.
  35223. + *
  35224. + * ALTERNATIVELY, this software may be distributed under the terms of the
  35225. + * GNU General Public License ("GPL") version 2, as published by the Free
  35226. + * Software Foundation.
  35227. + *
  35228. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  35229. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  35230. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35231. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35232. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  35233. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35234. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35235. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35236. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35237. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35238. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35239. + */
  35240. +
  35241. +/* ---- Include Files ---------------------------------------------------- */
  35242. +
  35243. +#include <linux/kernel.h>
  35244. +#include <linux/module.h>
  35245. +#include <linux/mutex.h>
  35246. +
  35247. +#include "vchiq_core.h"
  35248. +#include "vchiq_arm.h"
  35249. +#include "vchiq_killable.h"
  35250. +
  35251. +/* ---- Public Variables ------------------------------------------------- */
  35252. +
  35253. +/* ---- Private Constants and Types -------------------------------------- */
  35254. +
  35255. +struct bulk_waiter_node {
  35256. + struct bulk_waiter bulk_waiter;
  35257. + int pid;
  35258. + struct list_head list;
  35259. +};
  35260. +
  35261. +struct vchiq_instance_struct {
  35262. + VCHIQ_STATE_T *state;
  35263. +
  35264. + int connected;
  35265. +
  35266. + struct list_head bulk_waiter_list;
  35267. + struct mutex bulk_waiter_list_mutex;
  35268. +};
  35269. +
  35270. +static VCHIQ_STATUS_T
  35271. +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  35272. + unsigned int size, VCHIQ_BULK_DIR_T dir);
  35273. +
  35274. +/****************************************************************************
  35275. +*
  35276. +* vchiq_initialise
  35277. +*
  35278. +***************************************************************************/
  35279. +#define VCHIQ_INIT_RETRIES 10
  35280. +VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *instanceOut)
  35281. +{
  35282. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  35283. + VCHIQ_STATE_T *state;
  35284. + VCHIQ_INSTANCE_T instance = NULL;
  35285. + int i;
  35286. +
  35287. + vchiq_log_trace(vchiq_core_log_level, "%s called", __func__);
  35288. +
  35289. + /* VideoCore may not be ready due to boot up timing.
  35290. + It may never be ready if kernel and firmware are mismatched, so don't block forever. */
  35291. + for (i=0; i<VCHIQ_INIT_RETRIES; i++) {
  35292. + state = vchiq_get_state();
  35293. + if (state)
  35294. + break;
  35295. + udelay(500);
  35296. + }
  35297. + if (i==VCHIQ_INIT_RETRIES) {
  35298. + vchiq_log_error(vchiq_core_log_level,
  35299. + "%s: videocore not initialized\n", __func__);
  35300. + goto failed;
  35301. + } else if (i>0) {
  35302. + vchiq_log_warning(vchiq_core_log_level,
  35303. + "%s: videocore initialized after %d retries\n", __func__, i);
  35304. + }
  35305. +
  35306. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  35307. + if (!instance) {
  35308. + vchiq_log_error(vchiq_core_log_level,
  35309. + "%s: error allocating vchiq instance\n", __func__);
  35310. + goto failed;
  35311. + }
  35312. +
  35313. + instance->connected = 0;
  35314. + instance->state = state;
  35315. + mutex_init(&instance->bulk_waiter_list_mutex);
  35316. + INIT_LIST_HEAD(&instance->bulk_waiter_list);
  35317. +
  35318. + *instanceOut = instance;
  35319. +
  35320. + status = VCHIQ_SUCCESS;
  35321. +
  35322. +failed:
  35323. + vchiq_log_trace(vchiq_core_log_level,
  35324. + "%s(%p): returning %d", __func__, instance, status);
  35325. +
  35326. + return status;
  35327. +}
  35328. +EXPORT_SYMBOL(vchiq_initialise);
  35329. +
  35330. +/****************************************************************************
  35331. +*
  35332. +* vchiq_shutdown
  35333. +*
  35334. +***************************************************************************/
  35335. +
  35336. +VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance)
  35337. +{
  35338. + VCHIQ_STATUS_T status;
  35339. + VCHIQ_STATE_T *state = instance->state;
  35340. +
  35341. + vchiq_log_trace(vchiq_core_log_level,
  35342. + "%s(%p) called", __func__, instance);
  35343. +
  35344. + if (mutex_lock_interruptible(&state->mutex) != 0)
  35345. + return VCHIQ_RETRY;
  35346. +
  35347. + /* Remove all services */
  35348. + status = vchiq_shutdown_internal(state, instance);
  35349. +
  35350. + mutex_unlock(&state->mutex);
  35351. +
  35352. + vchiq_log_trace(vchiq_core_log_level,
  35353. + "%s(%p): returning %d", __func__, instance, status);
  35354. +
  35355. + if (status == VCHIQ_SUCCESS) {
  35356. + struct list_head *pos, *next;
  35357. + list_for_each_safe(pos, next,
  35358. + &instance->bulk_waiter_list) {
  35359. + struct bulk_waiter_node *waiter;
  35360. + waiter = list_entry(pos,
  35361. + struct bulk_waiter_node,
  35362. + list);
  35363. + list_del(pos);
  35364. + vchiq_log_info(vchiq_arm_log_level,
  35365. + "bulk_waiter - cleaned up %x "
  35366. + "for pid %d",
  35367. + (unsigned int)waiter, waiter->pid);
  35368. + kfree(waiter);
  35369. + }
  35370. + kfree(instance);
  35371. + }
  35372. +
  35373. + return status;
  35374. +}
  35375. +EXPORT_SYMBOL(vchiq_shutdown);
  35376. +
  35377. +/****************************************************************************
  35378. +*
  35379. +* vchiq_is_connected
  35380. +*
  35381. +***************************************************************************/
  35382. +
  35383. +int vchiq_is_connected(VCHIQ_INSTANCE_T instance)
  35384. +{
  35385. + return instance->connected;
  35386. +}
  35387. +
  35388. +/****************************************************************************
  35389. +*
  35390. +* vchiq_connect
  35391. +*
  35392. +***************************************************************************/
  35393. +
  35394. +VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance)
  35395. +{
  35396. + VCHIQ_STATUS_T status;
  35397. + VCHIQ_STATE_T *state = instance->state;
  35398. +
  35399. + vchiq_log_trace(vchiq_core_log_level,
  35400. + "%s(%p) called", __func__, instance);
  35401. +
  35402. + if (mutex_lock_interruptible(&state->mutex) != 0) {
  35403. + vchiq_log_trace(vchiq_core_log_level,
  35404. + "%s: call to mutex_lock failed", __func__);
  35405. + status = VCHIQ_RETRY;
  35406. + goto failed;
  35407. + }
  35408. + status = vchiq_connect_internal(state, instance);
  35409. +
  35410. + if (status == VCHIQ_SUCCESS)
  35411. + instance->connected = 1;
  35412. +
  35413. + mutex_unlock(&state->mutex);
  35414. +
  35415. +failed:
  35416. + vchiq_log_trace(vchiq_core_log_level,
  35417. + "%s(%p): returning %d", __func__, instance, status);
  35418. +
  35419. + return status;
  35420. +}
  35421. +EXPORT_SYMBOL(vchiq_connect);
  35422. +
  35423. +/****************************************************************************
  35424. +*
  35425. +* vchiq_add_service
  35426. +*
  35427. +***************************************************************************/
  35428. +
  35429. +VCHIQ_STATUS_T vchiq_add_service(
  35430. + VCHIQ_INSTANCE_T instance,
  35431. + const VCHIQ_SERVICE_PARAMS_T *params,
  35432. + VCHIQ_SERVICE_HANDLE_T *phandle)
  35433. +{
  35434. + VCHIQ_STATUS_T status;
  35435. + VCHIQ_STATE_T *state = instance->state;
  35436. + VCHIQ_SERVICE_T *service = NULL;
  35437. + int srvstate;
  35438. +
  35439. + vchiq_log_trace(vchiq_core_log_level,
  35440. + "%s(%p) called", __func__, instance);
  35441. +
  35442. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  35443. +
  35444. + srvstate = vchiq_is_connected(instance)
  35445. + ? VCHIQ_SRVSTATE_LISTENING
  35446. + : VCHIQ_SRVSTATE_HIDDEN;
  35447. +
  35448. + service = vchiq_add_service_internal(
  35449. + state,
  35450. + params,
  35451. + srvstate,
  35452. + instance,
  35453. + NULL);
  35454. +
  35455. + if (service) {
  35456. + *phandle = service->handle;
  35457. + status = VCHIQ_SUCCESS;
  35458. + } else
  35459. + status = VCHIQ_ERROR;
  35460. +
  35461. + vchiq_log_trace(vchiq_core_log_level,
  35462. + "%s(%p): returning %d", __func__, instance, status);
  35463. +
  35464. + return status;
  35465. +}
  35466. +EXPORT_SYMBOL(vchiq_add_service);
  35467. +
  35468. +/****************************************************************************
  35469. +*
  35470. +* vchiq_open_service
  35471. +*
  35472. +***************************************************************************/
  35473. +
  35474. +VCHIQ_STATUS_T vchiq_open_service(
  35475. + VCHIQ_INSTANCE_T instance,
  35476. + const VCHIQ_SERVICE_PARAMS_T *params,
  35477. + VCHIQ_SERVICE_HANDLE_T *phandle)
  35478. +{
  35479. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  35480. + VCHIQ_STATE_T *state = instance->state;
  35481. + VCHIQ_SERVICE_T *service = NULL;
  35482. +
  35483. + vchiq_log_trace(vchiq_core_log_level,
  35484. + "%s(%p) called", __func__, instance);
  35485. +
  35486. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  35487. +
  35488. + if (!vchiq_is_connected(instance))
  35489. + goto failed;
  35490. +
  35491. + service = vchiq_add_service_internal(state,
  35492. + params,
  35493. + VCHIQ_SRVSTATE_OPENING,
  35494. + instance,
  35495. + NULL);
  35496. +
  35497. + if (service) {
  35498. + *phandle = service->handle;
  35499. + status = vchiq_open_service_internal(service, current->pid);
  35500. + if (status != VCHIQ_SUCCESS) {
  35501. + vchiq_remove_service(service->handle);
  35502. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  35503. + }
  35504. + }
  35505. +
  35506. +failed:
  35507. + vchiq_log_trace(vchiq_core_log_level,
  35508. + "%s(%p): returning %d", __func__, instance, status);
  35509. +
  35510. + return status;
  35511. +}
  35512. +EXPORT_SYMBOL(vchiq_open_service);
  35513. +
  35514. +VCHIQ_STATUS_T
  35515. +vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle,
  35516. + const void *data, unsigned int size, void *userdata)
  35517. +{
  35518. + return vchiq_bulk_transfer(handle,
  35519. + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
  35520. + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_TRANSMIT);
  35521. +}
  35522. +EXPORT_SYMBOL(vchiq_queue_bulk_transmit);
  35523. +
  35524. +VCHIQ_STATUS_T
  35525. +vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  35526. + unsigned int size, void *userdata)
  35527. +{
  35528. + return vchiq_bulk_transfer(handle,
  35529. + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
  35530. + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_RECEIVE);
  35531. +}
  35532. +EXPORT_SYMBOL(vchiq_queue_bulk_receive);
  35533. +
  35534. +VCHIQ_STATUS_T
  35535. +vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle, const void *data,
  35536. + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
  35537. +{
  35538. + VCHIQ_STATUS_T status;
  35539. +
  35540. + switch (mode) {
  35541. + case VCHIQ_BULK_MODE_NOCALLBACK:
  35542. + case VCHIQ_BULK_MODE_CALLBACK:
  35543. + status = vchiq_bulk_transfer(handle,
  35544. + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
  35545. + mode, VCHIQ_BULK_TRANSMIT);
  35546. + break;
  35547. + case VCHIQ_BULK_MODE_BLOCKING:
  35548. + status = vchiq_blocking_bulk_transfer(handle,
  35549. + (void *)data, size, VCHIQ_BULK_TRANSMIT);
  35550. + break;
  35551. + default:
  35552. + return VCHIQ_ERROR;
  35553. + }
  35554. +
  35555. + return status;
  35556. +}
  35557. +EXPORT_SYMBOL(vchiq_bulk_transmit);
  35558. +
  35559. +VCHIQ_STATUS_T
  35560. +vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  35561. + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
  35562. +{
  35563. + VCHIQ_STATUS_T status;
  35564. +
  35565. + switch (mode) {
  35566. + case VCHIQ_BULK_MODE_NOCALLBACK:
  35567. + case VCHIQ_BULK_MODE_CALLBACK:
  35568. + status = vchiq_bulk_transfer(handle,
  35569. + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
  35570. + mode, VCHIQ_BULK_RECEIVE);
  35571. + break;
  35572. + case VCHIQ_BULK_MODE_BLOCKING:
  35573. + status = vchiq_blocking_bulk_transfer(handle,
  35574. + (void *)data, size, VCHIQ_BULK_RECEIVE);
  35575. + break;
  35576. + default:
  35577. + return VCHIQ_ERROR;
  35578. + }
  35579. +
  35580. + return status;
  35581. +}
  35582. +EXPORT_SYMBOL(vchiq_bulk_receive);
  35583. +
  35584. +static VCHIQ_STATUS_T
  35585. +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  35586. + unsigned int size, VCHIQ_BULK_DIR_T dir)
  35587. +{
  35588. + VCHIQ_INSTANCE_T instance;
  35589. + VCHIQ_SERVICE_T *service;
  35590. + VCHIQ_STATUS_T status;
  35591. + struct bulk_waiter_node *waiter = NULL;
  35592. + struct list_head *pos;
  35593. +
  35594. + service = find_service_by_handle(handle);
  35595. + if (!service)
  35596. + return VCHIQ_ERROR;
  35597. +
  35598. + instance = service->instance;
  35599. +
  35600. + unlock_service(service);
  35601. +
  35602. + mutex_lock(&instance->bulk_waiter_list_mutex);
  35603. + list_for_each(pos, &instance->bulk_waiter_list) {
  35604. + if (list_entry(pos, struct bulk_waiter_node,
  35605. + list)->pid == current->pid) {
  35606. + waiter = list_entry(pos,
  35607. + struct bulk_waiter_node,
  35608. + list);
  35609. + list_del(pos);
  35610. + break;
  35611. + }
  35612. + }
  35613. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  35614. +
  35615. + if (waiter) {
  35616. + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
  35617. + if (bulk) {
  35618. + /* This thread has an outstanding bulk transfer. */
  35619. + if ((bulk->data != data) ||
  35620. + (bulk->size != size)) {
  35621. + /* This is not a retry of the previous one.
  35622. + ** Cancel the signal when the transfer
  35623. + ** completes. */
  35624. + spin_lock(&bulk_waiter_spinlock);
  35625. + bulk->userdata = NULL;
  35626. + spin_unlock(&bulk_waiter_spinlock);
  35627. + }
  35628. + }
  35629. + }
  35630. +
  35631. + if (!waiter) {
  35632. + waiter = kzalloc(sizeof(struct bulk_waiter_node), GFP_KERNEL);
  35633. + if (!waiter) {
  35634. + vchiq_log_error(vchiq_core_log_level,
  35635. + "%s - out of memory", __func__);
  35636. + return VCHIQ_ERROR;
  35637. + }
  35638. + }
  35639. +
  35640. + status = vchiq_bulk_transfer(handle, VCHI_MEM_HANDLE_INVALID,
  35641. + data, size, &waiter->bulk_waiter, VCHIQ_BULK_MODE_BLOCKING,
  35642. + dir);
  35643. + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
  35644. + !waiter->bulk_waiter.bulk) {
  35645. + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
  35646. + if (bulk) {
  35647. + /* Cancel the signal when the transfer
  35648. + ** completes. */
  35649. + spin_lock(&bulk_waiter_spinlock);
  35650. + bulk->userdata = NULL;
  35651. + spin_unlock(&bulk_waiter_spinlock);
  35652. + }
  35653. + kfree(waiter);
  35654. + } else {
  35655. + waiter->pid = current->pid;
  35656. + mutex_lock(&instance->bulk_waiter_list_mutex);
  35657. + list_add(&waiter->list, &instance->bulk_waiter_list);
  35658. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  35659. + vchiq_log_info(vchiq_arm_log_level,
  35660. + "saved bulk_waiter %x for pid %d",
  35661. + (unsigned int)waiter, current->pid);
  35662. + }
  35663. +
  35664. + return status;
  35665. +}
  35666. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_killable.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_killable.h
  35667. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_killable.h 1969-12-31 18:00:00.000000000 -0600
  35668. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_killable.h 2014-12-03 19:13:38.228418001 -0600
  35669. @@ -0,0 +1,69 @@
  35670. +/**
  35671. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  35672. + *
  35673. + * Redistribution and use in source and binary forms, with or without
  35674. + * modification, are permitted provided that the following conditions
  35675. + * are met:
  35676. + * 1. Redistributions of source code must retain the above copyright
  35677. + * notice, this list of conditions, and the following disclaimer,
  35678. + * without modification.
  35679. + * 2. Redistributions in binary form must reproduce the above copyright
  35680. + * notice, this list of conditions and the following disclaimer in the
  35681. + * documentation and/or other materials provided with the distribution.
  35682. + * 3. The names of the above-listed copyright holders may not be used
  35683. + * to endorse or promote products derived from this software without
  35684. + * specific prior written permission.
  35685. + *
  35686. + * ALTERNATIVELY, this software may be distributed under the terms of the
  35687. + * GNU General Public License ("GPL") version 2, as published by the Free
  35688. + * Software Foundation.
  35689. + *
  35690. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  35691. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  35692. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35693. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35694. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  35695. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35696. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35697. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35698. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35699. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35700. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35701. + */
  35702. +
  35703. +#ifndef VCHIQ_KILLABLE_H
  35704. +#define VCHIQ_KILLABLE_H
  35705. +
  35706. +#include <linux/mutex.h>
  35707. +#include <linux/semaphore.h>
  35708. +
  35709. +#define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTRAP) | sigmask(SIGSTOP) | sigmask(SIGCONT))
  35710. +
  35711. +static inline int __must_check down_interruptible_killable(struct semaphore *sem)
  35712. +{
  35713. + /* Allow interception of killable signals only. We don't want to be interrupted by harmless signals like SIGALRM */
  35714. + int ret;
  35715. + sigset_t blocked, oldset;
  35716. + siginitsetinv(&blocked, SHUTDOWN_SIGS);
  35717. + sigprocmask(SIG_SETMASK, &blocked, &oldset);
  35718. + ret = down_interruptible(sem);
  35719. + sigprocmask(SIG_SETMASK, &oldset, NULL);
  35720. + return ret;
  35721. +}
  35722. +#define down_interruptible down_interruptible_killable
  35723. +
  35724. +
  35725. +static inline int __must_check mutex_lock_interruptible_killable(struct mutex *lock)
  35726. +{
  35727. + /* Allow interception of killable signals only. We don't want to be interrupted by harmless signals like SIGALRM */
  35728. + int ret;
  35729. + sigset_t blocked, oldset;
  35730. + siginitsetinv(&blocked, SHUTDOWN_SIGS);
  35731. + sigprocmask(SIG_SETMASK, &blocked, &oldset);
  35732. + ret = mutex_lock_interruptible(lock);
  35733. + sigprocmask(SIG_SETMASK, &oldset, NULL);
  35734. + return ret;
  35735. +}
  35736. +#define mutex_lock_interruptible mutex_lock_interruptible_killable
  35737. +
  35738. +#endif
  35739. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h
  35740. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h 1969-12-31 18:00:00.000000000 -0600
  35741. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h 2014-12-03 19:13:38.228418001 -0600
  35742. @@ -0,0 +1,71 @@
  35743. +/**
  35744. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  35745. + *
  35746. + * Redistribution and use in source and binary forms, with or without
  35747. + * modification, are permitted provided that the following conditions
  35748. + * are met:
  35749. + * 1. Redistributions of source code must retain the above copyright
  35750. + * notice, this list of conditions, and the following disclaimer,
  35751. + * without modification.
  35752. + * 2. Redistributions in binary form must reproduce the above copyright
  35753. + * notice, this list of conditions and the following disclaimer in the
  35754. + * documentation and/or other materials provided with the distribution.
  35755. + * 3. The names of the above-listed copyright holders may not be used
  35756. + * to endorse or promote products derived from this software without
  35757. + * specific prior written permission.
  35758. + *
  35759. + * ALTERNATIVELY, this software may be distributed under the terms of the
  35760. + * GNU General Public License ("GPL") version 2, as published by the Free
  35761. + * Software Foundation.
  35762. + *
  35763. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  35764. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  35765. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35766. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35767. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  35768. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35769. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35770. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35771. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35772. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35773. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35774. + */
  35775. +
  35776. +#ifndef VCHIQ_MEMDRV_H
  35777. +#define VCHIQ_MEMDRV_H
  35778. +
  35779. +/* ---- Include Files ----------------------------------------------------- */
  35780. +
  35781. +#include <linux/kernel.h>
  35782. +#include "vchiq_if.h"
  35783. +
  35784. +/* ---- Constants and Types ---------------------------------------------- */
  35785. +
  35786. +typedef struct {
  35787. + void *armSharedMemVirt;
  35788. + dma_addr_t armSharedMemPhys;
  35789. + size_t armSharedMemSize;
  35790. +
  35791. + void *vcSharedMemVirt;
  35792. + dma_addr_t vcSharedMemPhys;
  35793. + size_t vcSharedMemSize;
  35794. +} VCHIQ_SHARED_MEM_INFO_T;
  35795. +
  35796. +/* ---- Variable Externs ------------------------------------------------- */
  35797. +
  35798. +/* ---- Function Prototypes ---------------------------------------------- */
  35799. +
  35800. +void vchiq_get_shared_mem_info(VCHIQ_SHARED_MEM_INFO_T *info);
  35801. +
  35802. +VCHIQ_STATUS_T vchiq_memdrv_initialise(void);
  35803. +
  35804. +VCHIQ_STATUS_T vchiq_userdrv_create_instance(
  35805. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  35806. +
  35807. +VCHIQ_STATUS_T vchiq_userdrv_suspend(
  35808. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  35809. +
  35810. +VCHIQ_STATUS_T vchiq_userdrv_resume(
  35811. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  35812. +
  35813. +#endif
  35814. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h
  35815. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h 1969-12-31 18:00:00.000000000 -0600
  35816. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h 2014-12-03 19:13:38.228418001 -0600
  35817. @@ -0,0 +1,58 @@
  35818. +/**
  35819. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  35820. + *
  35821. + * Redistribution and use in source and binary forms, with or without
  35822. + * modification, are permitted provided that the following conditions
  35823. + * are met:
  35824. + * 1. Redistributions of source code must retain the above copyright
  35825. + * notice, this list of conditions, and the following disclaimer,
  35826. + * without modification.
  35827. + * 2. Redistributions in binary form must reproduce the above copyright
  35828. + * notice, this list of conditions and the following disclaimer in the
  35829. + * documentation and/or other materials provided with the distribution.
  35830. + * 3. The names of the above-listed copyright holders may not be used
  35831. + * to endorse or promote products derived from this software without
  35832. + * specific prior written permission.
  35833. + *
  35834. + * ALTERNATIVELY, this software may be distributed under the terms of the
  35835. + * GNU General Public License ("GPL") version 2, as published by the Free
  35836. + * Software Foundation.
  35837. + *
  35838. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  35839. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  35840. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35841. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35842. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  35843. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35844. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35845. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35846. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35847. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35848. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35849. + */
  35850. +
  35851. +#ifndef VCHIQ_PAGELIST_H
  35852. +#define VCHIQ_PAGELIST_H
  35853. +
  35854. +#ifndef PAGE_SIZE
  35855. +#define PAGE_SIZE 4096
  35856. +#endif
  35857. +#define CACHE_LINE_SIZE 32
  35858. +#define PAGELIST_WRITE 0
  35859. +#define PAGELIST_READ 1
  35860. +#define PAGELIST_READ_WITH_FRAGMENTS 2
  35861. +
  35862. +typedef struct pagelist_struct {
  35863. + unsigned long length;
  35864. + unsigned short type;
  35865. + unsigned short offset;
  35866. + unsigned long addrs[1]; /* N.B. 12 LSBs hold the number of following
  35867. + pages at consecutive addresses. */
  35868. +} PAGELIST_T;
  35869. +
  35870. +typedef struct fragments_struct {
  35871. + char headbuf[CACHE_LINE_SIZE];
  35872. + char tailbuf[CACHE_LINE_SIZE];
  35873. +} FRAGMENTS_T;
  35874. +
  35875. +#endif /* VCHIQ_PAGELIST_H */
  35876. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c
  35877. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c 1969-12-31 18:00:00.000000000 -0600
  35878. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c 2014-12-03 19:13:38.228418001 -0600
  35879. @@ -0,0 +1,857 @@
  35880. +/**
  35881. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  35882. + *
  35883. + * Redistribution and use in source and binary forms, with or without
  35884. + * modification, are permitted provided that the following conditions
  35885. + * are met:
  35886. + * 1. Redistributions of source code must retain the above copyright
  35887. + * notice, this list of conditions, and the following disclaimer,
  35888. + * without modification.
  35889. + * 2. Redistributions in binary form must reproduce the above copyright
  35890. + * notice, this list of conditions and the following disclaimer in the
  35891. + * documentation and/or other materials provided with the distribution.
  35892. + * 3. The names of the above-listed copyright holders may not be used
  35893. + * to endorse or promote products derived from this software without
  35894. + * specific prior written permission.
  35895. + *
  35896. + * ALTERNATIVELY, this software may be distributed under the terms of the
  35897. + * GNU General Public License ("GPL") version 2, as published by the Free
  35898. + * Software Foundation.
  35899. + *
  35900. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  35901. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  35902. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35903. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35904. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  35905. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35906. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35907. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35908. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35909. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35910. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35911. + */
  35912. +#include <linux/module.h>
  35913. +#include <linux/types.h>
  35914. +
  35915. +#include "interface/vchi/vchi.h"
  35916. +#include "vchiq.h"
  35917. +#include "vchiq_core.h"
  35918. +
  35919. +#include "vchiq_util.h"
  35920. +
  35921. +#include <stddef.h>
  35922. +
  35923. +#define vchiq_status_to_vchi(status) ((int32_t)status)
  35924. +
  35925. +typedef struct {
  35926. + VCHIQ_SERVICE_HANDLE_T handle;
  35927. +
  35928. + VCHIU_QUEUE_T queue;
  35929. +
  35930. + VCHI_CALLBACK_T callback;
  35931. + void *callback_param;
  35932. +} SHIM_SERVICE_T;
  35933. +
  35934. +/* ----------------------------------------------------------------------
  35935. + * return pointer to the mphi message driver function table
  35936. + * -------------------------------------------------------------------- */
  35937. +const VCHI_MESSAGE_DRIVER_T *
  35938. +vchi_mphi_message_driver_func_table(void)
  35939. +{
  35940. + return NULL;
  35941. +}
  35942. +
  35943. +/* ----------------------------------------------------------------------
  35944. + * return a pointer to the 'single' connection driver fops
  35945. + * -------------------------------------------------------------------- */
  35946. +const VCHI_CONNECTION_API_T *
  35947. +single_get_func_table(void)
  35948. +{
  35949. + return NULL;
  35950. +}
  35951. +
  35952. +VCHI_CONNECTION_T *vchi_create_connection(
  35953. + const VCHI_CONNECTION_API_T *function_table,
  35954. + const VCHI_MESSAGE_DRIVER_T *low_level)
  35955. +{
  35956. + (void)function_table;
  35957. + (void)low_level;
  35958. + return NULL;
  35959. +}
  35960. +
  35961. +/***********************************************************
  35962. + * Name: vchi_msg_peek
  35963. + *
  35964. + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
  35965. + * void **data,
  35966. + * uint32_t *msg_size,
  35967. +
  35968. +
  35969. + * VCHI_FLAGS_T flags
  35970. + *
  35971. + * Description: Routine to return a pointer to the current message (to allow in
  35972. + * place processing). The message can be removed using
  35973. + * vchi_msg_remove when you're finished
  35974. + *
  35975. + * Returns: int32_t - success == 0
  35976. + *
  35977. + ***********************************************************/
  35978. +int32_t vchi_msg_peek(VCHI_SERVICE_HANDLE_T handle,
  35979. + void **data,
  35980. + uint32_t *msg_size,
  35981. + VCHI_FLAGS_T flags)
  35982. +{
  35983. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  35984. + VCHIQ_HEADER_T *header;
  35985. +
  35986. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  35987. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  35988. +
  35989. + if (flags == VCHI_FLAGS_NONE)
  35990. + if (vchiu_queue_is_empty(&service->queue))
  35991. + return -1;
  35992. +
  35993. + header = vchiu_queue_peek(&service->queue);
  35994. +
  35995. + *data = header->data;
  35996. + *msg_size = header->size;
  35997. +
  35998. + return 0;
  35999. +}
  36000. +EXPORT_SYMBOL(vchi_msg_peek);
  36001. +
  36002. +/***********************************************************
  36003. + * Name: vchi_msg_remove
  36004. + *
  36005. + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
  36006. + *
  36007. + * Description: Routine to remove a message (after it has been read with
  36008. + * vchi_msg_peek)
  36009. + *
  36010. + * Returns: int32_t - success == 0
  36011. + *
  36012. + ***********************************************************/
  36013. +int32_t vchi_msg_remove(VCHI_SERVICE_HANDLE_T handle)
  36014. +{
  36015. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  36016. + VCHIQ_HEADER_T *header;
  36017. +
  36018. + header = vchiu_queue_pop(&service->queue);
  36019. +
  36020. + vchiq_release_message(service->handle, header);
  36021. +
  36022. + return 0;
  36023. +}
  36024. +EXPORT_SYMBOL(vchi_msg_remove);
  36025. +
  36026. +/***********************************************************
  36027. + * Name: vchi_msg_queue
  36028. + *
  36029. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  36030. + * const void *data,
  36031. + * uint32_t data_size,
  36032. + * VCHI_FLAGS_T flags,
  36033. + * void *msg_handle,
  36034. + *
  36035. + * Description: Thin wrapper to queue a message onto a connection
  36036. + *
  36037. + * Returns: int32_t - success == 0
  36038. + *
  36039. + ***********************************************************/
  36040. +int32_t vchi_msg_queue(VCHI_SERVICE_HANDLE_T handle,
  36041. + const void *data,
  36042. + uint32_t data_size,
  36043. + VCHI_FLAGS_T flags,
  36044. + void *msg_handle)
  36045. +{
  36046. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  36047. + VCHIQ_ELEMENT_T element = {data, data_size};
  36048. + VCHIQ_STATUS_T status;
  36049. +
  36050. + (void)msg_handle;
  36051. +
  36052. + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
  36053. +
  36054. + status = vchiq_queue_message(service->handle, &element, 1);
  36055. +
  36056. + /* vchiq_queue_message() may return VCHIQ_RETRY, so we need to
  36057. + ** implement a retry mechanism since this function is supposed
  36058. + ** to block until queued
  36059. + */
  36060. + while (status == VCHIQ_RETRY) {
  36061. + msleep(1);
  36062. + status = vchiq_queue_message(service->handle, &element, 1);
  36063. + }
  36064. +
  36065. + return vchiq_status_to_vchi(status);
  36066. +}
  36067. +EXPORT_SYMBOL(vchi_msg_queue);
  36068. +
  36069. +/***********************************************************
  36070. + * Name: vchi_bulk_queue_receive
  36071. + *
  36072. + * Arguments: VCHI_BULK_HANDLE_T handle,
  36073. + * void *data_dst,
  36074. + * const uint32_t data_size,
  36075. + * VCHI_FLAGS_T flags
  36076. + * void *bulk_handle
  36077. + *
  36078. + * Description: Routine to setup a rcv buffer
  36079. + *
  36080. + * Returns: int32_t - success == 0
  36081. + *
  36082. + ***********************************************************/
  36083. +int32_t vchi_bulk_queue_receive(VCHI_SERVICE_HANDLE_T handle,
  36084. + void *data_dst,
  36085. + uint32_t data_size,
  36086. + VCHI_FLAGS_T flags,
  36087. + void *bulk_handle)
  36088. +{
  36089. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  36090. + VCHIQ_BULK_MODE_T mode;
  36091. + VCHIQ_STATUS_T status;
  36092. +
  36093. + switch ((int)flags) {
  36094. + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
  36095. + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  36096. + WARN_ON(!service->callback);
  36097. + mode = VCHIQ_BULK_MODE_CALLBACK;
  36098. + break;
  36099. + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
  36100. + mode = VCHIQ_BULK_MODE_BLOCKING;
  36101. + break;
  36102. + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  36103. + case VCHI_FLAGS_NONE:
  36104. + mode = VCHIQ_BULK_MODE_NOCALLBACK;
  36105. + break;
  36106. + default:
  36107. + WARN(1, "unsupported message\n");
  36108. + return vchiq_status_to_vchi(VCHIQ_ERROR);
  36109. + }
  36110. +
  36111. + status = vchiq_bulk_receive(service->handle, data_dst, data_size,
  36112. + bulk_handle, mode);
  36113. +
  36114. + /* vchiq_bulk_receive() may return VCHIQ_RETRY, so we need to
  36115. + ** implement a retry mechanism since this function is supposed
  36116. + ** to block until queued
  36117. + */
  36118. + while (status == VCHIQ_RETRY) {
  36119. + msleep(1);
  36120. + status = vchiq_bulk_receive(service->handle, data_dst,
  36121. + data_size, bulk_handle, mode);
  36122. + }
  36123. +
  36124. + return vchiq_status_to_vchi(status);
  36125. +}
  36126. +EXPORT_SYMBOL(vchi_bulk_queue_receive);
  36127. +
  36128. +/***********************************************************
  36129. + * Name: vchi_bulk_queue_transmit
  36130. + *
  36131. + * Arguments: VCHI_BULK_HANDLE_T handle,
  36132. + * const void *data_src,
  36133. + * uint32_t data_size,
  36134. + * VCHI_FLAGS_T flags,
  36135. + * void *bulk_handle
  36136. + *
  36137. + * Description: Routine to transmit some data
  36138. + *
  36139. + * Returns: int32_t - success == 0
  36140. + *
  36141. + ***********************************************************/
  36142. +int32_t vchi_bulk_queue_transmit(VCHI_SERVICE_HANDLE_T handle,
  36143. + const void *data_src,
  36144. + uint32_t data_size,
  36145. + VCHI_FLAGS_T flags,
  36146. + void *bulk_handle)
  36147. +{
  36148. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  36149. + VCHIQ_BULK_MODE_T mode;
  36150. + VCHIQ_STATUS_T status;
  36151. +
  36152. + switch ((int)flags) {
  36153. + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
  36154. + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  36155. + WARN_ON(!service->callback);
  36156. + mode = VCHIQ_BULK_MODE_CALLBACK;
  36157. + break;
  36158. + case VCHI_FLAGS_BLOCK_UNTIL_DATA_READ:
  36159. + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
  36160. + mode = VCHIQ_BULK_MODE_BLOCKING;
  36161. + break;
  36162. + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  36163. + case VCHI_FLAGS_NONE:
  36164. + mode = VCHIQ_BULK_MODE_NOCALLBACK;
  36165. + break;
  36166. + default:
  36167. + WARN(1, "unsupported message\n");
  36168. + return vchiq_status_to_vchi(VCHIQ_ERROR);
  36169. + }
  36170. +
  36171. + status = vchiq_bulk_transmit(service->handle, data_src, data_size,
  36172. + bulk_handle, mode);
  36173. +
  36174. + /* vchiq_bulk_transmit() may return VCHIQ_RETRY, so we need to
  36175. + ** implement a retry mechanism since this function is supposed
  36176. + ** to block until queued
  36177. + */
  36178. + while (status == VCHIQ_RETRY) {
  36179. + msleep(1);
  36180. + status = vchiq_bulk_transmit(service->handle, data_src,
  36181. + data_size, bulk_handle, mode);
  36182. + }
  36183. +
  36184. + return vchiq_status_to_vchi(status);
  36185. +}
  36186. +EXPORT_SYMBOL(vchi_bulk_queue_transmit);
  36187. +
  36188. +/***********************************************************
  36189. + * Name: vchi_msg_dequeue
  36190. + *
  36191. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  36192. + * void *data,
  36193. + * uint32_t max_data_size_to_read,
  36194. + * uint32_t *actual_msg_size
  36195. + * VCHI_FLAGS_T flags
  36196. + *
  36197. + * Description: Routine to dequeue a message into the supplied buffer
  36198. + *
  36199. + * Returns: int32_t - success == 0
  36200. + *
  36201. + ***********************************************************/
  36202. +int32_t vchi_msg_dequeue(VCHI_SERVICE_HANDLE_T handle,
  36203. + void *data,
  36204. + uint32_t max_data_size_to_read,
  36205. + uint32_t *actual_msg_size,
  36206. + VCHI_FLAGS_T flags)
  36207. +{
  36208. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  36209. + VCHIQ_HEADER_T *header;
  36210. +
  36211. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  36212. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  36213. +
  36214. + if (flags == VCHI_FLAGS_NONE)
  36215. + if (vchiu_queue_is_empty(&service->queue))
  36216. + return -1;
  36217. +
  36218. + header = vchiu_queue_pop(&service->queue);
  36219. +
  36220. + memcpy(data, header->data, header->size < max_data_size_to_read ?
  36221. + header->size : max_data_size_to_read);
  36222. +
  36223. + *actual_msg_size = header->size;
  36224. +
  36225. + vchiq_release_message(service->handle, header);
  36226. +
  36227. + return 0;
  36228. +}
  36229. +EXPORT_SYMBOL(vchi_msg_dequeue);
  36230. +
  36231. +/***********************************************************
  36232. + * Name: vchi_msg_queuev
  36233. + *
  36234. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  36235. + * VCHI_MSG_VECTOR_T *vector,
  36236. + * uint32_t count,
  36237. + * VCHI_FLAGS_T flags,
  36238. + * void *msg_handle
  36239. + *
  36240. + * Description: Thin wrapper to queue a message onto a connection
  36241. + *
  36242. + * Returns: int32_t - success == 0
  36243. + *
  36244. + ***********************************************************/
  36245. +
  36246. +vchiq_static_assert(sizeof(VCHI_MSG_VECTOR_T) == sizeof(VCHIQ_ELEMENT_T));
  36247. +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_base) ==
  36248. + offsetof(VCHIQ_ELEMENT_T, data));
  36249. +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_len) ==
  36250. + offsetof(VCHIQ_ELEMENT_T, size));
  36251. +
  36252. +int32_t vchi_msg_queuev(VCHI_SERVICE_HANDLE_T handle,
  36253. + VCHI_MSG_VECTOR_T *vector,
  36254. + uint32_t count,
  36255. + VCHI_FLAGS_T flags,
  36256. + void *msg_handle)
  36257. +{
  36258. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  36259. +
  36260. + (void)msg_handle;
  36261. +
  36262. + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
  36263. +
  36264. + return vchiq_status_to_vchi(vchiq_queue_message(service->handle,
  36265. + (const VCHIQ_ELEMENT_T *)vector, count));
  36266. +}
  36267. +EXPORT_SYMBOL(vchi_msg_queuev);
  36268. +
  36269. +/***********************************************************
  36270. + * Name: vchi_held_msg_release
  36271. + *
  36272. + * Arguments: VCHI_HELD_MSG_T *message
  36273. + *
  36274. + * Description: Routine to release a held message (after it has been read with
  36275. + * vchi_msg_hold)
  36276. + *
  36277. + * Returns: int32_t - success == 0
  36278. + *
  36279. + ***********************************************************/
  36280. +int32_t vchi_held_msg_release(VCHI_HELD_MSG_T *message)
  36281. +{
  36282. + vchiq_release_message((VCHIQ_SERVICE_HANDLE_T)message->service,
  36283. + (VCHIQ_HEADER_T *)message->message);
  36284. +
  36285. + return 0;
  36286. +}
  36287. +EXPORT_SYMBOL(vchi_held_msg_release);
  36288. +
  36289. +/***********************************************************
  36290. + * Name: vchi_msg_hold
  36291. + *
  36292. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  36293. + * void **data,
  36294. + * uint32_t *msg_size,
  36295. + * VCHI_FLAGS_T flags,
  36296. + * VCHI_HELD_MSG_T *message_handle
  36297. + *
  36298. + * Description: Routine to return a pointer to the current message (to allow
  36299. + * in place processing). The message is dequeued - don't forget
  36300. + * to release the message using vchi_held_msg_release when you're
  36301. + * finished.
  36302. + *
  36303. + * Returns: int32_t - success == 0
  36304. + *
  36305. + ***********************************************************/
  36306. +int32_t vchi_msg_hold(VCHI_SERVICE_HANDLE_T handle,
  36307. + void **data,
  36308. + uint32_t *msg_size,
  36309. + VCHI_FLAGS_T flags,
  36310. + VCHI_HELD_MSG_T *message_handle)
  36311. +{
  36312. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  36313. + VCHIQ_HEADER_T *header;
  36314. +
  36315. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  36316. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  36317. +
  36318. + if (flags == VCHI_FLAGS_NONE)
  36319. + if (vchiu_queue_is_empty(&service->queue))
  36320. + return -1;
  36321. +
  36322. + header = vchiu_queue_pop(&service->queue);
  36323. +
  36324. + *data = header->data;
  36325. + *msg_size = header->size;
  36326. +
  36327. + message_handle->service =
  36328. + (struct opaque_vchi_service_t *)service->handle;
  36329. + message_handle->message = header;
  36330. +
  36331. + return 0;
  36332. +}
  36333. +EXPORT_SYMBOL(vchi_msg_hold);
  36334. +
  36335. +/***********************************************************
  36336. + * Name: vchi_initialise
  36337. + *
  36338. + * Arguments: VCHI_INSTANCE_T *instance_handle
  36339. + *
  36340. + * Description: Initialises the hardware but does not transmit anything
  36341. + * When run as a Host App this will be called twice hence the need
  36342. + * to malloc the state information
  36343. + *
  36344. + * Returns: 0 if successful, failure otherwise
  36345. + *
  36346. + ***********************************************************/
  36347. +
  36348. +int32_t vchi_initialise(VCHI_INSTANCE_T *instance_handle)
  36349. +{
  36350. + VCHIQ_INSTANCE_T instance;
  36351. + VCHIQ_STATUS_T status;
  36352. +
  36353. + status = vchiq_initialise(&instance);
  36354. +
  36355. + *instance_handle = (VCHI_INSTANCE_T)instance;
  36356. +
  36357. + return vchiq_status_to_vchi(status);
  36358. +}
  36359. +EXPORT_SYMBOL(vchi_initialise);
  36360. +
  36361. +/***********************************************************
  36362. + * Name: vchi_connect
  36363. + *
  36364. + * Arguments: VCHI_CONNECTION_T **connections
  36365. + * const uint32_t num_connections
  36366. + * VCHI_INSTANCE_T instance_handle)
  36367. + *
  36368. + * Description: Starts the command service on each connection,
  36369. + * causing INIT messages to be pinged back and forth
  36370. + *
  36371. + * Returns: 0 if successful, failure otherwise
  36372. + *
  36373. + ***********************************************************/
  36374. +int32_t vchi_connect(VCHI_CONNECTION_T **connections,
  36375. + const uint32_t num_connections,
  36376. + VCHI_INSTANCE_T instance_handle)
  36377. +{
  36378. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  36379. +
  36380. + (void)connections;
  36381. + (void)num_connections;
  36382. +
  36383. + return vchiq_connect(instance);
  36384. +}
  36385. +EXPORT_SYMBOL(vchi_connect);
  36386. +
  36387. +
  36388. +/***********************************************************
  36389. + * Name: vchi_disconnect
  36390. + *
  36391. + * Arguments: VCHI_INSTANCE_T instance_handle
  36392. + *
  36393. + * Description: Stops the command service on each connection,
  36394. + * causing DE-INIT messages to be pinged back and forth
  36395. + *
  36396. + * Returns: 0 if successful, failure otherwise
  36397. + *
  36398. + ***********************************************************/
  36399. +int32_t vchi_disconnect(VCHI_INSTANCE_T instance_handle)
  36400. +{
  36401. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  36402. + return vchiq_status_to_vchi(vchiq_shutdown(instance));
  36403. +}
  36404. +EXPORT_SYMBOL(vchi_disconnect);
  36405. +
  36406. +
  36407. +/***********************************************************
  36408. + * Name: vchi_service_open
  36409. + * Name: vchi_service_create
  36410. + *
  36411. + * Arguments: VCHI_INSTANCE_T *instance_handle
  36412. + * SERVICE_CREATION_T *setup,
  36413. + * VCHI_SERVICE_HANDLE_T *handle
  36414. + *
  36415. + * Description: Routine to open a service
  36416. + *
  36417. + * Returns: int32_t - success == 0
  36418. + *
  36419. + ***********************************************************/
  36420. +
  36421. +static VCHIQ_STATUS_T shim_callback(VCHIQ_REASON_T reason,
  36422. + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_HANDLE_T handle, void *bulk_user)
  36423. +{
  36424. + SHIM_SERVICE_T *service =
  36425. + (SHIM_SERVICE_T *)VCHIQ_GET_SERVICE_USERDATA(handle);
  36426. +
  36427. + if (!service->callback)
  36428. + goto release;
  36429. +
  36430. + switch (reason) {
  36431. + case VCHIQ_MESSAGE_AVAILABLE:
  36432. + vchiu_queue_push(&service->queue, header);
  36433. +
  36434. + service->callback(service->callback_param,
  36435. + VCHI_CALLBACK_MSG_AVAILABLE, NULL);
  36436. +
  36437. + goto done;
  36438. + break;
  36439. +
  36440. + case VCHIQ_BULK_TRANSMIT_DONE:
  36441. + service->callback(service->callback_param,
  36442. + VCHI_CALLBACK_BULK_SENT, bulk_user);
  36443. + break;
  36444. +
  36445. + case VCHIQ_BULK_RECEIVE_DONE:
  36446. + service->callback(service->callback_param,
  36447. + VCHI_CALLBACK_BULK_RECEIVED, bulk_user);
  36448. + break;
  36449. +
  36450. + case VCHIQ_SERVICE_CLOSED:
  36451. + service->callback(service->callback_param,
  36452. + VCHI_CALLBACK_SERVICE_CLOSED, NULL);
  36453. + break;
  36454. +
  36455. + case VCHIQ_SERVICE_OPENED:
  36456. + /* No equivalent VCHI reason */
  36457. + break;
  36458. +
  36459. + case VCHIQ_BULK_TRANSMIT_ABORTED:
  36460. + service->callback(service->callback_param,
  36461. + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
  36462. + bulk_user);
  36463. + break;
  36464. +
  36465. + case VCHIQ_BULK_RECEIVE_ABORTED:
  36466. + service->callback(service->callback_param,
  36467. + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
  36468. + bulk_user);
  36469. + break;
  36470. +
  36471. + default:
  36472. + WARN(1, "not supported\n");
  36473. + break;
  36474. + }
  36475. +
  36476. +release:
  36477. + vchiq_release_message(service->handle, header);
  36478. +done:
  36479. + return VCHIQ_SUCCESS;
  36480. +}
  36481. +
  36482. +static SHIM_SERVICE_T *service_alloc(VCHIQ_INSTANCE_T instance,
  36483. + SERVICE_CREATION_T *setup)
  36484. +{
  36485. + SHIM_SERVICE_T *service = kzalloc(sizeof(SHIM_SERVICE_T), GFP_KERNEL);
  36486. +
  36487. + (void)instance;
  36488. +
  36489. + if (service) {
  36490. + if (vchiu_queue_init(&service->queue, 64)) {
  36491. + service->callback = setup->callback;
  36492. + service->callback_param = setup->callback_param;
  36493. + } else {
  36494. + kfree(service);
  36495. + service = NULL;
  36496. + }
  36497. + }
  36498. +
  36499. + return service;
  36500. +}
  36501. +
  36502. +static void service_free(SHIM_SERVICE_T *service)
  36503. +{
  36504. + if (service) {
  36505. + vchiu_queue_delete(&service->queue);
  36506. + kfree(service);
  36507. + }
  36508. +}
  36509. +
  36510. +int32_t vchi_service_open(VCHI_INSTANCE_T instance_handle,
  36511. + SERVICE_CREATION_T *setup,
  36512. + VCHI_SERVICE_HANDLE_T *handle)
  36513. +{
  36514. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  36515. + SHIM_SERVICE_T *service = service_alloc(instance, setup);
  36516. +
  36517. + *handle = (VCHI_SERVICE_HANDLE_T)service;
  36518. +
  36519. + if (service) {
  36520. + VCHIQ_SERVICE_PARAMS_T params;
  36521. + VCHIQ_STATUS_T status;
  36522. +
  36523. + memset(&params, 0, sizeof(params));
  36524. + params.fourcc = setup->service_id;
  36525. + params.callback = shim_callback;
  36526. + params.userdata = service;
  36527. + params.version = setup->version.version;
  36528. + params.version_min = setup->version.version_min;
  36529. +
  36530. + status = vchiq_open_service(instance, &params,
  36531. + &service->handle);
  36532. + if (status != VCHIQ_SUCCESS) {
  36533. + service_free(service);
  36534. + service = NULL;
  36535. + *handle = NULL;
  36536. + }
  36537. + }
  36538. +
  36539. + return (service != NULL) ? 0 : -1;
  36540. +}
  36541. +EXPORT_SYMBOL(vchi_service_open);
  36542. +
  36543. +int32_t vchi_service_create(VCHI_INSTANCE_T instance_handle,
  36544. + SERVICE_CREATION_T *setup,
  36545. + VCHI_SERVICE_HANDLE_T *handle)
  36546. +{
  36547. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  36548. + SHIM_SERVICE_T *service = service_alloc(instance, setup);
  36549. +
  36550. + *handle = (VCHI_SERVICE_HANDLE_T)service;
  36551. +
  36552. + if (service) {
  36553. + VCHIQ_SERVICE_PARAMS_T params;
  36554. + VCHIQ_STATUS_T status;
  36555. +
  36556. + memset(&params, 0, sizeof(params));
  36557. + params.fourcc = setup->service_id;
  36558. + params.callback = shim_callback;
  36559. + params.userdata = service;
  36560. + params.version = setup->version.version;
  36561. + params.version_min = setup->version.version_min;
  36562. + status = vchiq_add_service(instance, &params, &service->handle);
  36563. +
  36564. + if (status != VCHIQ_SUCCESS) {
  36565. + service_free(service);
  36566. + service = NULL;
  36567. + *handle = NULL;
  36568. + }
  36569. + }
  36570. +
  36571. + return (service != NULL) ? 0 : -1;
  36572. +}
  36573. +EXPORT_SYMBOL(vchi_service_create);
  36574. +
  36575. +int32_t vchi_service_close(const VCHI_SERVICE_HANDLE_T handle)
  36576. +{
  36577. + int32_t ret = -1;
  36578. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  36579. + if (service) {
  36580. + VCHIQ_STATUS_T status = vchiq_close_service(service->handle);
  36581. + if (status == VCHIQ_SUCCESS) {
  36582. + service_free(service);
  36583. + service = NULL;
  36584. + }
  36585. +
  36586. + ret = vchiq_status_to_vchi(status);
  36587. + }
  36588. + return ret;
  36589. +}
  36590. +EXPORT_SYMBOL(vchi_service_close);
  36591. +
  36592. +int32_t vchi_service_destroy(const VCHI_SERVICE_HANDLE_T handle)
  36593. +{
  36594. + int32_t ret = -1;
  36595. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  36596. + if (service) {
  36597. + VCHIQ_STATUS_T status = vchiq_remove_service(service->handle);
  36598. + if (status == VCHIQ_SUCCESS) {
  36599. + service_free(service);
  36600. + service = NULL;
  36601. + }
  36602. +
  36603. + ret = vchiq_status_to_vchi(status);
  36604. + }
  36605. + return ret;
  36606. +}
  36607. +EXPORT_SYMBOL(vchi_service_destroy);
  36608. +
  36609. +int32_t vchi_service_set_option(const VCHI_SERVICE_HANDLE_T handle,
  36610. + VCHI_SERVICE_OPTION_T option,
  36611. + int value)
  36612. +{
  36613. + int32_t ret = -1;
  36614. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  36615. + VCHIQ_SERVICE_OPTION_T vchiq_option;
  36616. + switch (option) {
  36617. + case VCHI_SERVICE_OPTION_TRACE:
  36618. + vchiq_option = VCHIQ_SERVICE_OPTION_TRACE;
  36619. + break;
  36620. + default:
  36621. + service = NULL;
  36622. + break;
  36623. + }
  36624. + if (service) {
  36625. + VCHIQ_STATUS_T status =
  36626. + vchiq_set_service_option(service->handle,
  36627. + vchiq_option,
  36628. + value);
  36629. +
  36630. + ret = vchiq_status_to_vchi(status);
  36631. + }
  36632. + return ret;
  36633. +}
  36634. +EXPORT_SYMBOL(vchi_service_set_option);
  36635. +
  36636. +int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle, short *peer_version )
  36637. +{
  36638. + int32_t ret = -1;
  36639. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  36640. + if(service)
  36641. + {
  36642. + VCHIQ_STATUS_T status = vchiq_get_peer_version(service->handle, peer_version);
  36643. + ret = vchiq_status_to_vchi( status );
  36644. + }
  36645. + return ret;
  36646. +}
  36647. +EXPORT_SYMBOL(vchi_get_peer_version);
  36648. +
  36649. +/* ----------------------------------------------------------------------
  36650. + * read a uint32_t from buffer.
  36651. + * network format is defined to be little endian
  36652. + * -------------------------------------------------------------------- */
  36653. +uint32_t
  36654. +vchi_readbuf_uint32(const void *_ptr)
  36655. +{
  36656. + const unsigned char *ptr = _ptr;
  36657. + return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
  36658. +}
  36659. +
  36660. +/* ----------------------------------------------------------------------
  36661. + * write a uint32_t to buffer.
  36662. + * network format is defined to be little endian
  36663. + * -------------------------------------------------------------------- */
  36664. +void
  36665. +vchi_writebuf_uint32(void *_ptr, uint32_t value)
  36666. +{
  36667. + unsigned char *ptr = _ptr;
  36668. + ptr[0] = (unsigned char)((value >> 0) & 0xFF);
  36669. + ptr[1] = (unsigned char)((value >> 8) & 0xFF);
  36670. + ptr[2] = (unsigned char)((value >> 16) & 0xFF);
  36671. + ptr[3] = (unsigned char)((value >> 24) & 0xFF);
  36672. +}
  36673. +
  36674. +/* ----------------------------------------------------------------------
  36675. + * read a uint16_t from buffer.
  36676. + * network format is defined to be little endian
  36677. + * -------------------------------------------------------------------- */
  36678. +uint16_t
  36679. +vchi_readbuf_uint16(const void *_ptr)
  36680. +{
  36681. + const unsigned char *ptr = _ptr;
  36682. + return ptr[0] | (ptr[1] << 8);
  36683. +}
  36684. +
  36685. +/* ----------------------------------------------------------------------
  36686. + * write a uint16_t into the buffer.
  36687. + * network format is defined to be little endian
  36688. + * -------------------------------------------------------------------- */
  36689. +void
  36690. +vchi_writebuf_uint16(void *_ptr, uint16_t value)
  36691. +{
  36692. + unsigned char *ptr = _ptr;
  36693. + ptr[0] = (value >> 0) & 0xFF;
  36694. + ptr[1] = (value >> 8) & 0xFF;
  36695. +}
  36696. +
  36697. +/***********************************************************
  36698. + * Name: vchi_service_use
  36699. + *
  36700. + * Arguments: const VCHI_SERVICE_HANDLE_T handle
  36701. + *
  36702. + * Description: Routine to increment refcount on a service
  36703. + *
  36704. + * Returns: void
  36705. + *
  36706. + ***********************************************************/
  36707. +int32_t vchi_service_use(const VCHI_SERVICE_HANDLE_T handle)
  36708. +{
  36709. + int32_t ret = -1;
  36710. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  36711. + if (service)
  36712. + ret = vchiq_status_to_vchi(vchiq_use_service(service->handle));
  36713. + return ret;
  36714. +}
  36715. +EXPORT_SYMBOL(vchi_service_use);
  36716. +
  36717. +/***********************************************************
  36718. + * Name: vchi_service_release
  36719. + *
  36720. + * Arguments: const VCHI_SERVICE_HANDLE_T handle
  36721. + *
  36722. + * Description: Routine to decrement refcount on a service
  36723. + *
  36724. + * Returns: void
  36725. + *
  36726. + ***********************************************************/
  36727. +int32_t vchi_service_release(const VCHI_SERVICE_HANDLE_T handle)
  36728. +{
  36729. + int32_t ret = -1;
  36730. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  36731. + if (service)
  36732. + ret = vchiq_status_to_vchi(
  36733. + vchiq_release_service(service->handle));
  36734. + return ret;
  36735. +}
  36736. +EXPORT_SYMBOL(vchi_service_release);
  36737. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c
  36738. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c 1969-12-31 18:00:00.000000000 -0600
  36739. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c 2014-12-03 19:13:38.228418001 -0600
  36740. @@ -0,0 +1,152 @@
  36741. +/**
  36742. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  36743. + *
  36744. + * Redistribution and use in source and binary forms, with or without
  36745. + * modification, are permitted provided that the following conditions
  36746. + * are met:
  36747. + * 1. Redistributions of source code must retain the above copyright
  36748. + * notice, this list of conditions, and the following disclaimer,
  36749. + * without modification.
  36750. + * 2. Redistributions in binary form must reproduce the above copyright
  36751. + * notice, this list of conditions and the following disclaimer in the
  36752. + * documentation and/or other materials provided with the distribution.
  36753. + * 3. The names of the above-listed copyright holders may not be used
  36754. + * to endorse or promote products derived from this software without
  36755. + * specific prior written permission.
  36756. + *
  36757. + * ALTERNATIVELY, this software may be distributed under the terms of the
  36758. + * GNU General Public License ("GPL") version 2, as published by the Free
  36759. + * Software Foundation.
  36760. + *
  36761. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  36762. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  36763. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  36764. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36765. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  36766. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  36767. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  36768. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  36769. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36770. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36771. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36772. + */
  36773. +
  36774. +#include "vchiq_util.h"
  36775. +#include "vchiq_killable.h"
  36776. +
  36777. +static inline int is_pow2(int i)
  36778. +{
  36779. + return i && !(i & (i - 1));
  36780. +}
  36781. +
  36782. +int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size)
  36783. +{
  36784. + WARN_ON(!is_pow2(size));
  36785. +
  36786. + queue->size = size;
  36787. + queue->read = 0;
  36788. + queue->write = 0;
  36789. +
  36790. + sema_init(&queue->pop, 0);
  36791. + sema_init(&queue->push, 0);
  36792. +
  36793. + queue->storage = kzalloc(size * sizeof(VCHIQ_HEADER_T *), GFP_KERNEL);
  36794. + if (queue->storage == NULL) {
  36795. + vchiu_queue_delete(queue);
  36796. + return 0;
  36797. + }
  36798. + return 1;
  36799. +}
  36800. +
  36801. +void vchiu_queue_delete(VCHIU_QUEUE_T *queue)
  36802. +{
  36803. + if (queue->storage != NULL)
  36804. + kfree(queue->storage);
  36805. +}
  36806. +
  36807. +int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue)
  36808. +{
  36809. + return queue->read == queue->write;
  36810. +}
  36811. +
  36812. +int vchiu_queue_is_full(VCHIU_QUEUE_T *queue)
  36813. +{
  36814. + return queue->write == queue->read + queue->size;
  36815. +}
  36816. +
  36817. +void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header)
  36818. +{
  36819. + while (queue->write == queue->read + queue->size) {
  36820. + if (down_interruptible(&queue->pop) != 0) {
  36821. + flush_signals(current);
  36822. + }
  36823. + }
  36824. +
  36825. + /*
  36826. + * Write to queue->storage must be visible after read from
  36827. + * queue->read
  36828. + */
  36829. + smp_mb();
  36830. +
  36831. + queue->storage[queue->write & (queue->size - 1)] = header;
  36832. +
  36833. + /*
  36834. + * Write to queue->storage must be visible before write to
  36835. + * queue->write
  36836. + */
  36837. + smp_wmb();
  36838. +
  36839. + queue->write++;
  36840. +
  36841. + up(&queue->push);
  36842. +}
  36843. +
  36844. +VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue)
  36845. +{
  36846. + while (queue->write == queue->read) {
  36847. + if (down_interruptible(&queue->push) != 0) {
  36848. + flush_signals(current);
  36849. + }
  36850. + }
  36851. +
  36852. + up(&queue->push); // We haven't removed anything from the queue.
  36853. +
  36854. + /*
  36855. + * Read from queue->storage must be visible after read from
  36856. + * queue->write
  36857. + */
  36858. + smp_rmb();
  36859. +
  36860. + return queue->storage[queue->read & (queue->size - 1)];
  36861. +}
  36862. +
  36863. +VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue)
  36864. +{
  36865. + VCHIQ_HEADER_T *header;
  36866. +
  36867. + while (queue->write == queue->read) {
  36868. + if (down_interruptible(&queue->push) != 0) {
  36869. + flush_signals(current);
  36870. + }
  36871. + }
  36872. +
  36873. + /*
  36874. + * Read from queue->storage must be visible after read from
  36875. + * queue->write
  36876. + */
  36877. + smp_rmb();
  36878. +
  36879. + header = queue->storage[queue->read & (queue->size - 1)];
  36880. +
  36881. + /*
  36882. + * Read from queue->storage must be visible before write to
  36883. + * queue->read
  36884. + */
  36885. + smp_mb();
  36886. +
  36887. + queue->read++;
  36888. +
  36889. + up(&queue->pop);
  36890. +
  36891. + return header;
  36892. +}
  36893. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h
  36894. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h 1969-12-31 18:00:00.000000000 -0600
  36895. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h 2014-12-03 19:13:38.228418001 -0600
  36896. @@ -0,0 +1,81 @@
  36897. +/**
  36898. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  36899. + *
  36900. + * Redistribution and use in source and binary forms, with or without
  36901. + * modification, are permitted provided that the following conditions
  36902. + * are met:
  36903. + * 1. Redistributions of source code must retain the above copyright
  36904. + * notice, this list of conditions, and the following disclaimer,
  36905. + * without modification.
  36906. + * 2. Redistributions in binary form must reproduce the above copyright
  36907. + * notice, this list of conditions and the following disclaimer in the
  36908. + * documentation and/or other materials provided with the distribution.
  36909. + * 3. The names of the above-listed copyright holders may not be used
  36910. + * to endorse or promote products derived from this software without
  36911. + * specific prior written permission.
  36912. + *
  36913. + * ALTERNATIVELY, this software may be distributed under the terms of the
  36914. + * GNU General Public License ("GPL") version 2, as published by the Free
  36915. + * Software Foundation.
  36916. + *
  36917. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  36918. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  36919. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  36920. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36921. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  36922. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  36923. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  36924. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  36925. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36926. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36927. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36928. + */
  36929. +
  36930. +#ifndef VCHIQ_UTIL_H
  36931. +#define VCHIQ_UTIL_H
  36932. +
  36933. +#include <linux/types.h>
  36934. +#include <linux/semaphore.h>
  36935. +#include <linux/mutex.h>
  36936. +#include <linux/bitops.h>
  36937. +#include <linux/kthread.h>
  36938. +#include <linux/wait.h>
  36939. +#include <linux/vmalloc.h>
  36940. +#include <linux/jiffies.h>
  36941. +#include <linux/delay.h>
  36942. +#include <linux/string.h>
  36943. +#include <linux/types.h>
  36944. +#include <linux/interrupt.h>
  36945. +#include <linux/random.h>
  36946. +#include <linux/sched.h>
  36947. +#include <linux/ctype.h>
  36948. +#include <linux/uaccess.h>
  36949. +#include <linux/time.h> /* for time_t */
  36950. +#include <linux/slab.h>
  36951. +#include <linux/vmalloc.h>
  36952. +
  36953. +#include "vchiq_if.h"
  36954. +
  36955. +typedef struct {
  36956. + int size;
  36957. + int read;
  36958. + int write;
  36959. +
  36960. + struct semaphore pop;
  36961. + struct semaphore push;
  36962. +
  36963. + VCHIQ_HEADER_T **storage;
  36964. +} VCHIU_QUEUE_T;
  36965. +
  36966. +extern int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size);
  36967. +extern void vchiu_queue_delete(VCHIU_QUEUE_T *queue);
  36968. +
  36969. +extern int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue);
  36970. +extern int vchiu_queue_is_full(VCHIU_QUEUE_T *queue);
  36971. +
  36972. +extern void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header);
  36973. +
  36974. +extern VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue);
  36975. +extern VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue);
  36976. +
  36977. +#endif
  36978. diff -Nur linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c
  36979. --- linux-3.12.33/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c 1969-12-31 18:00:00.000000000 -0600
  36980. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c 2014-12-03 19:13:38.228418001 -0600
  36981. @@ -0,0 +1,59 @@
  36982. +/**
  36983. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  36984. + *
  36985. + * Redistribution and use in source and binary forms, with or without
  36986. + * modification, are permitted provided that the following conditions
  36987. + * are met:
  36988. + * 1. Redistributions of source code must retain the above copyright
  36989. + * notice, this list of conditions, and the following disclaimer,
  36990. + * without modification.
  36991. + * 2. Redistributions in binary form must reproduce the above copyright
  36992. + * notice, this list of conditions and the following disclaimer in the
  36993. + * documentation and/or other materials provided with the distribution.
  36994. + * 3. The names of the above-listed copyright holders may not be used
  36995. + * to endorse or promote products derived from this software without
  36996. + * specific prior written permission.
  36997. + *
  36998. + * ALTERNATIVELY, this software may be distributed under the terms of the
  36999. + * GNU General Public License ("GPL") version 2, as published by the Free
  37000. + * Software Foundation.
  37001. + *
  37002. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  37003. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  37004. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  37005. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37006. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  37007. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  37008. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  37009. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  37010. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  37011. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37012. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37013. + */
  37014. +#include "vchiq_build_info.h"
  37015. +#include <linux/broadcom/vc_debug_sym.h>
  37016. +
  37017. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_hostname, "dc4-arm-01" );
  37018. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_version, "9245b4c35b99b3870e1f7dc598c5692b3c66a6f0 (tainted)" );
  37019. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_time, __TIME__ );
  37020. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_date, __DATE__ );
  37021. +
  37022. +const char *vchiq_get_build_hostname( void )
  37023. +{
  37024. + return vchiq_build_hostname;
  37025. +}
  37026. +
  37027. +const char *vchiq_get_build_version( void )
  37028. +{
  37029. + return vchiq_build_version;
  37030. +}
  37031. +
  37032. +const char *vchiq_get_build_date( void )
  37033. +{
  37034. + return vchiq_build_date;
  37035. +}
  37036. +
  37037. +const char *vchiq_get_build_time( void )
  37038. +{
  37039. + return vchiq_build_time;
  37040. +}
  37041. diff -Nur linux-3.12.33/drivers/misc/vc04_services/Kconfig linux-3.12.33-rpi/drivers/misc/vc04_services/Kconfig
  37042. --- linux-3.12.33/drivers/misc/vc04_services/Kconfig 1969-12-31 18:00:00.000000000 -0600
  37043. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/Kconfig 2014-12-03 19:13:38.224418001 -0600
  37044. @@ -0,0 +1,9 @@
  37045. +config BCM2708_VCHIQ
  37046. + tristate "Videocore VCHIQ"
  37047. + depends on MACH_BCM2708
  37048. + default y
  37049. + help
  37050. + Kernel to VideoCore communication interface for the
  37051. + BCM2708 family of products.
  37052. + Defaults to Y when the Broadcom Videocore services
  37053. + are included in the build, N otherwise.
  37054. diff -Nur linux-3.12.33/drivers/misc/vc04_services/Makefile linux-3.12.33-rpi/drivers/misc/vc04_services/Makefile
  37055. --- linux-3.12.33/drivers/misc/vc04_services/Makefile 1969-12-31 18:00:00.000000000 -0600
  37056. +++ linux-3.12.33-rpi/drivers/misc/vc04_services/Makefile 2014-12-03 19:13:38.224418001 -0600
  37057. @@ -0,0 +1,17 @@
  37058. +ifeq ($(CONFIG_MACH_BCM2708),y)
  37059. +
  37060. +obj-$(CONFIG_BCM2708_VCHIQ) += vchiq.o
  37061. +
  37062. +vchiq-objs := \
  37063. + interface/vchiq_arm/vchiq_core.o \
  37064. + interface/vchiq_arm/vchiq_arm.o \
  37065. + interface/vchiq_arm/vchiq_kern_lib.o \
  37066. + interface/vchiq_arm/vchiq_2835_arm.o \
  37067. + interface/vchiq_arm/vchiq_debugfs.o \
  37068. + interface/vchiq_arm/vchiq_shim.o \
  37069. + interface/vchiq_arm/vchiq_util.o \
  37070. + interface/vchiq_arm/vchiq_connected.o \
  37071. +
  37072. +ccflags-y += -DVCOS_VERIFY_BKPTS=1 -Idrivers/misc/vc04_services -DUSE_VCHIQ_ARM -D__VCCOREVER__=0x04000000
  37073. +
  37074. +endif
  37075. diff -Nur linux-3.12.33/drivers/mmc/card/block.c linux-3.12.33-rpi/drivers/mmc/card/block.c
  37076. --- linux-3.12.33/drivers/mmc/card/block.c 2014-11-15 06:28:07.000000000 -0600
  37077. +++ linux-3.12.33-rpi/drivers/mmc/card/block.c 2014-12-03 19:13:38.228418001 -0600
  37078. @@ -1361,7 +1361,7 @@
  37079. brq->data.blocks = 1;
  37080. }
  37081. - if (brq->data.blocks > 1 || do_rel_wr) {
  37082. + if (brq->data.blocks > 1 || do_rel_wr || card->host->caps2 & MMC_CAP2_FORCE_MULTIBLOCK) {
  37083. /* SPI multiblock writes terminate using a special
  37084. * token, not a STOP_TRANSMISSION request.
  37085. */
  37086. diff -Nur linux-3.12.33/drivers/mmc/core/sd.c linux-3.12.33-rpi/drivers/mmc/core/sd.c
  37087. --- linux-3.12.33/drivers/mmc/core/sd.c 2014-11-15 06:28:07.000000000 -0600
  37088. +++ linux-3.12.33-rpi/drivers/mmc/core/sd.c 2014-12-03 19:13:38.232418001 -0600
  37089. @@ -14,6 +14,8 @@
  37090. #include <linux/sizes.h>
  37091. #include <linux/slab.h>
  37092. #include <linux/stat.h>
  37093. +#include <linux/jiffies.h>
  37094. +#include <linux/nmi.h>
  37095. #include <linux/mmc/host.h>
  37096. #include <linux/mmc/card.h>
  37097. @@ -66,6 +68,15 @@
  37098. __res & __mask; \
  37099. })
  37100. +// timeout for tries
  37101. +static const unsigned long retry_timeout_ms= 10*1000;
  37102. +
  37103. +// try at least 10 times, even if timeout is reached
  37104. +static const int retry_min_tries= 10;
  37105. +
  37106. +// delay between tries
  37107. +static const unsigned long retry_delay_ms= 10;
  37108. +
  37109. /*
  37110. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  37111. */
  37112. @@ -218,12 +229,63 @@
  37113. }
  37114. /*
  37115. - * Fetch and process SD Status register.
  37116. + * Fetch and process SD Configuration Register.
  37117. + */
  37118. +static int mmc_read_scr(struct mmc_card *card)
  37119. +{
  37120. + unsigned long timeout_at;
  37121. + int err, tries;
  37122. +
  37123. + timeout_at= jiffies + msecs_to_jiffies( retry_timeout_ms );
  37124. + tries= 0;
  37125. +
  37126. + while( tries < retry_min_tries || time_before( jiffies, timeout_at ) )
  37127. + {
  37128. + unsigned long delay_at;
  37129. + tries++;
  37130. +
  37131. + err = mmc_app_send_scr(card, card->raw_scr);
  37132. + if( !err )
  37133. + break; // success!!!
  37134. +
  37135. + touch_nmi_watchdog(); // we are still alive!
  37136. +
  37137. + // delay
  37138. + delay_at= jiffies + msecs_to_jiffies( retry_delay_ms );
  37139. + while( time_before( jiffies, delay_at ) )
  37140. + {
  37141. + mdelay( 1 );
  37142. + touch_nmi_watchdog(); // we are still alive!
  37143. + }
  37144. + }
  37145. +
  37146. + if( err)
  37147. + {
  37148. + pr_err("%s: failed to read SD Configuration register (SCR) after %d tries during %lu ms, error %d\n", mmc_hostname(card->host), tries, retry_timeout_ms, err );
  37149. + return err;
  37150. + }
  37151. +
  37152. + if( tries > 1 )
  37153. + {
  37154. + pr_info("%s: could read SD Configuration register (SCR) at the %dth attempt\n", mmc_hostname(card->host), tries );
  37155. + }
  37156. +
  37157. + err = mmc_decode_scr(card);
  37158. + if (err)
  37159. + return err;
  37160. +
  37161. + return err;
  37162. +}
  37163. +
  37164. +/*
  37165. + * Fetch and process SD Status Register.
  37166. */
  37167. static int mmc_read_ssr(struct mmc_card *card)
  37168. {
  37169. + unsigned long timeout_at;
  37170. unsigned int au, es, et, eo;
  37171. int err, i;
  37172. + int tries;
  37173. u32 *ssr;
  37174. if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
  37175. @@ -236,14 +298,40 @@
  37176. if (!ssr)
  37177. return -ENOMEM;
  37178. - err = mmc_app_sd_status(card, ssr);
  37179. - if (err) {
  37180. - pr_warning("%s: problem reading SD Status "
  37181. - "register.\n", mmc_hostname(card->host));
  37182. - err = 0;
  37183. + timeout_at= jiffies + msecs_to_jiffies( retry_timeout_ms );
  37184. + tries= 0;
  37185. +
  37186. + while( tries < retry_min_tries || time_before( jiffies, timeout_at ) )
  37187. + {
  37188. + unsigned long delay_at;
  37189. + tries++;
  37190. +
  37191. + err= mmc_app_sd_status(card, ssr);
  37192. + if( !err )
  37193. + break; // sucess!!!
  37194. +
  37195. + touch_nmi_watchdog(); // we are still alive!
  37196. +
  37197. + // delay
  37198. + delay_at= jiffies + msecs_to_jiffies( retry_delay_ms );
  37199. + while( time_before( jiffies, delay_at ) )
  37200. + {
  37201. + mdelay( 1 );
  37202. + touch_nmi_watchdog(); // we are still alive!
  37203. + }
  37204. + }
  37205. +
  37206. + if( err)
  37207. + {
  37208. + pr_err("%s: failed to read SD Status register (SSR) after %d tries during %lu ms, error %d\n", mmc_hostname(card->host), tries, retry_timeout_ms, err );
  37209. goto out;
  37210. }
  37211. + if( tries > 1 )
  37212. + {
  37213. + pr_info("%s: read SD Status register (SSR) after %d attempts\n", mmc_hostname(card->host), tries );
  37214. + }
  37215. +
  37216. for (i = 0; i < 16; i++)
  37217. ssr[i] = be32_to_cpu(ssr[i]);
  37218. @@ -823,14 +911,10 @@
  37219. if (!reinit) {
  37220. /*
  37221. - * Fetch SCR from card.
  37222. + * Fetch and decode SD Configuration register.
  37223. */
  37224. - err = mmc_app_send_scr(card, card->raw_scr);
  37225. - if (err)
  37226. - return err;
  37227. -
  37228. - err = mmc_decode_scr(card);
  37229. - if (err)
  37230. + err = mmc_read_scr(card);
  37231. + if( err )
  37232. return err;
  37233. /*
  37234. diff -Nur linux-3.12.33/drivers/mmc/host/bcm2835-mmc.c linux-3.12.33-rpi/drivers/mmc/host/bcm2835-mmc.c
  37235. --- linux-3.12.33/drivers/mmc/host/bcm2835-mmc.c 1969-12-31 18:00:00.000000000 -0600
  37236. +++ linux-3.12.33-rpi/drivers/mmc/host/bcm2835-mmc.c 2014-12-03 19:13:38.236418001 -0600
  37237. @@ -0,0 +1,1547 @@
  37238. +/*
  37239. + * BCM2835 MMC host driver.
  37240. + *
  37241. + * Author: Gellert Weisz <gellert@raspberrypi.org>
  37242. + * Copyright 2014
  37243. + *
  37244. + * Based on
  37245. + * sdhci-bcm2708.c by Broadcom
  37246. + * sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
  37247. + * sdhci.c and sdhci-pci.c by Pierre Ossman
  37248. + *
  37249. + * This program is free software; you can redistribute it and/or modify it
  37250. + * under the terms and conditions of the GNU General Public License,
  37251. + * version 2, as published by the Free Software Foundation.
  37252. + *
  37253. + * This program is distributed in the hope it will be useful, but WITHOUT
  37254. + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  37255. + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  37256. + * more details.
  37257. + *
  37258. + * You should have received a copy of the GNU General Public License
  37259. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  37260. + */
  37261. +
  37262. +#include <linux/delay.h>
  37263. +#include <linux/module.h>
  37264. +#include <linux/io.h>
  37265. +#include <linux/mmc/mmc.h>
  37266. +#include <linux/mmc/host.h>
  37267. +#include <linux/mmc/sd.h>
  37268. +#include <linux/scatterlist.h>
  37269. +#include <linux/of_address.h>
  37270. +#include <linux/of_irq.h>
  37271. +#include <linux/clk.h>
  37272. +#include <linux/platform_device.h>
  37273. +#include <linux/err.h>
  37274. +#include <linux/blkdev.h>
  37275. +#include <linux/dmaengine.h>
  37276. +#include <linux/dma-mapping.h>
  37277. +#include <linux/of_dma.h>
  37278. +
  37279. +#include "sdhci.h"
  37280. +
  37281. +
  37282. +#ifndef CONFIG_OF
  37283. + #define BCM2835_CLOCK_FREQ 250000000
  37284. +#endif
  37285. +
  37286. +#define DRIVER_NAME "mmc-bcm2835"
  37287. +
  37288. +#define DBG(f, x...) \
  37289. +pr_debug(DRIVER_NAME " [%s()]: " f, __func__, ## x)
  37290. +
  37291. +#ifndef CONFIG_MMC_BCM2835_DMA
  37292. + #define FORCE_PIO
  37293. +#endif
  37294. +
  37295. +
  37296. +/* the inclusive limit in bytes under which PIO will be used instead of DMA */
  37297. +#ifdef CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
  37298. +#define PIO_DMA_BARRIER CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
  37299. +#else
  37300. +#define PIO_DMA_BARRIER 00
  37301. +#endif
  37302. +
  37303. +#define MIN_FREQ 400000
  37304. +#define TIMEOUT_VAL 0xE
  37305. +#define BCM2835_SDHCI_WRITE_DELAY(f) (((2 * 1000000) / f) + 1)
  37306. +
  37307. +#ifndef BCM2708_PERI_BASE
  37308. + #define BCM2708_PERI_BASE 0x20000000
  37309. +#endif
  37310. +
  37311. +/* FIXME: Needs IOMMU support */
  37312. +#define BCM2835_VCMMU_SHIFT (0x7E000000 - BCM2708_PERI_BASE)
  37313. +
  37314. +
  37315. +struct bcm2835_host {
  37316. + spinlock_t lock;
  37317. +
  37318. + void __iomem *ioaddr;
  37319. + u32 phys_addr;
  37320. +
  37321. + struct mmc_host *mmc;
  37322. +
  37323. + u32 timeout;
  37324. +
  37325. + int clock; /* Current clock speed */
  37326. + u8 pwr; /* Current voltage */
  37327. +
  37328. + unsigned int max_clk; /* Max possible freq */
  37329. + unsigned int timeout_clk; /* Timeout freq (KHz) */
  37330. + unsigned int clk_mul; /* Clock Muliplier value */
  37331. +
  37332. + struct tasklet_struct finish_tasklet; /* Tasklet structures */
  37333. +
  37334. + struct timer_list timer; /* Timer for timeouts */
  37335. +
  37336. + struct sg_mapping_iter sg_miter; /* SG state for PIO */
  37337. + unsigned int blocks; /* remaining PIO blocks */
  37338. +
  37339. + int irq; /* Device IRQ */
  37340. +
  37341. +
  37342. + u32 ier; /* cached registers */
  37343. +
  37344. + struct mmc_request *mrq; /* Current request */
  37345. + struct mmc_command *cmd; /* Current command */
  37346. + struct mmc_data *data; /* Current data request */
  37347. + unsigned int data_early:1; /* Data finished before cmd */
  37348. +
  37349. + wait_queue_head_t buf_ready_int; /* Waitqueue for Buffer Read Ready interrupt */
  37350. +
  37351. + u32 thread_isr;
  37352. +
  37353. + u32 shadow;
  37354. +
  37355. + /*DMA part*/
  37356. + struct dma_chan *dma_chan_rx; /* DMA channel for reads */
  37357. + struct dma_chan *dma_chan_tx; /* DMA channel for writes */
  37358. + struct dma_async_tx_descriptor *tx_desc; /* descriptor */
  37359. +
  37360. + bool have_dma;
  37361. + bool use_dma;
  37362. + /*end of DMA part*/
  37363. +
  37364. + int max_delay; /* maximum length of time spent waiting */
  37365. +
  37366. + int flags; /* Host attributes */
  37367. +#define SDHCI_USE_SDMA (1<<0) /* Host is SDMA capable */
  37368. +#define SDHCI_USE_ADMA (1<<1) /* Host is ADMA capable */
  37369. +#define SDHCI_REQ_USE_DMA (1<<2) /* Use DMA for this req. */
  37370. +#define SDHCI_DEVICE_DEAD (1<<3) /* Device unresponsive */
  37371. +#define SDHCI_AUTO_CMD12 (1<<6) /* Auto CMD12 support */
  37372. +#define SDHCI_AUTO_CMD23 (1<<7) /* Auto CMD23 support */
  37373. +#define SDHCI_PV_ENABLED (1<<8) /* Preset value enabled */
  37374. +#define SDHCI_SDIO_IRQ_ENABLED (1<<9) /* SDIO irq enabled */
  37375. +#define SDHCI_USE_PLATDMA (1<<12) /* Host uses 3rd party DMA */
  37376. +};
  37377. +
  37378. +
  37379. +static inline void bcm2835_mmc_writel(struct bcm2835_host *host, u32 val, int reg)
  37380. +{
  37381. + writel(val, host->ioaddr + reg);
  37382. + udelay(BCM2835_SDHCI_WRITE_DELAY(max(host->clock, MIN_FREQ)));
  37383. +}
  37384. +
  37385. +static inline void mmc_raw_writel(struct bcm2835_host *host, u32 val, int reg)
  37386. +{
  37387. + writel(val, host->ioaddr + reg);
  37388. +}
  37389. +
  37390. +static inline u32 bcm2835_mmc_readl(struct bcm2835_host *host, int reg)
  37391. +{
  37392. + return readl(host->ioaddr + reg);
  37393. +}
  37394. +
  37395. +static inline void bcm2835_mmc_writew(struct bcm2835_host *host, u16 val, int reg)
  37396. +{
  37397. + u32 oldval = (reg == SDHCI_COMMAND) ? host->shadow :
  37398. + bcm2835_mmc_readl(host, reg & ~3);
  37399. + u32 word_num = (reg >> 1) & 1;
  37400. + u32 word_shift = word_num * 16;
  37401. + u32 mask = 0xffff << word_shift;
  37402. + u32 newval = (oldval & ~mask) | (val << word_shift);
  37403. +
  37404. + if (reg == SDHCI_TRANSFER_MODE)
  37405. + host->shadow = newval;
  37406. + else
  37407. + bcm2835_mmc_writel(host, newval, reg & ~3);
  37408. +
  37409. +}
  37410. +
  37411. +static inline void bcm2835_mmc_writeb(struct bcm2835_host *host, u8 val, int reg)
  37412. +{
  37413. + u32 oldval = bcm2835_mmc_readl(host, reg & ~3);
  37414. + u32 byte_num = reg & 3;
  37415. + u32 byte_shift = byte_num * 8;
  37416. + u32 mask = 0xff << byte_shift;
  37417. + u32 newval = (oldval & ~mask) | (val << byte_shift);
  37418. +
  37419. + bcm2835_mmc_writel(host, newval, reg & ~3);
  37420. +}
  37421. +
  37422. +
  37423. +static inline u16 bcm2835_mmc_readw(struct bcm2835_host *host, int reg)
  37424. +{
  37425. + u32 val = bcm2835_mmc_readl(host, (reg & ~3));
  37426. + u32 word_num = (reg >> 1) & 1;
  37427. + u32 word_shift = word_num * 16;
  37428. + u32 word = (val >> word_shift) & 0xffff;
  37429. +
  37430. + return word;
  37431. +}
  37432. +
  37433. +static inline u8 bcm2835_mmc_readb(struct bcm2835_host *host, int reg)
  37434. +{
  37435. + u32 val = bcm2835_mmc_readl(host, (reg & ~3));
  37436. + u32 byte_num = reg & 3;
  37437. + u32 byte_shift = byte_num * 8;
  37438. + u32 byte = (val >> byte_shift) & 0xff;
  37439. +
  37440. + return byte;
  37441. +}
  37442. +
  37443. +static void bcm2835_mmc_unsignal_irqs(struct bcm2835_host *host, u32 clear)
  37444. +{
  37445. + u32 ier;
  37446. +
  37447. + ier = bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE);
  37448. + ier &= ~clear;
  37449. + /* change which requests generate IRQs - makes no difference to
  37450. + the content of SDHCI_INT_STATUS, or the need to acknowledge IRQs */
  37451. + bcm2835_mmc_writel(host, ier, SDHCI_SIGNAL_ENABLE);
  37452. +}
  37453. +
  37454. +
  37455. +static void bcm2835_mmc_dumpregs(struct bcm2835_host *host)
  37456. +{
  37457. + pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
  37458. + mmc_hostname(host->mmc));
  37459. +
  37460. + pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
  37461. + bcm2835_mmc_readl(host, SDHCI_DMA_ADDRESS),
  37462. + bcm2835_mmc_readw(host, SDHCI_HOST_VERSION));
  37463. + pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
  37464. + bcm2835_mmc_readw(host, SDHCI_BLOCK_SIZE),
  37465. + bcm2835_mmc_readw(host, SDHCI_BLOCK_COUNT));
  37466. + pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
  37467. + bcm2835_mmc_readl(host, SDHCI_ARGUMENT),
  37468. + bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE));
  37469. + pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
  37470. + bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE),
  37471. + bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL));
  37472. + pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
  37473. + bcm2835_mmc_readb(host, SDHCI_POWER_CONTROL),
  37474. + bcm2835_mmc_readb(host, SDHCI_BLOCK_GAP_CONTROL));
  37475. + pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
  37476. + bcm2835_mmc_readb(host, SDHCI_WAKE_UP_CONTROL),
  37477. + bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL));
  37478. + pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
  37479. + bcm2835_mmc_readb(host, SDHCI_TIMEOUT_CONTROL),
  37480. + bcm2835_mmc_readl(host, SDHCI_INT_STATUS));
  37481. + pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
  37482. + bcm2835_mmc_readl(host, SDHCI_INT_ENABLE),
  37483. + bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE));
  37484. + pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
  37485. + bcm2835_mmc_readw(host, SDHCI_ACMD12_ERR),
  37486. + bcm2835_mmc_readw(host, SDHCI_SLOT_INT_STATUS));
  37487. + pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
  37488. + bcm2835_mmc_readl(host, SDHCI_CAPABILITIES),
  37489. + bcm2835_mmc_readl(host, SDHCI_CAPABILITIES_1));
  37490. + pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
  37491. + bcm2835_mmc_readw(host, SDHCI_COMMAND),
  37492. + bcm2835_mmc_readl(host, SDHCI_MAX_CURRENT));
  37493. + pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
  37494. + bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2));
  37495. +
  37496. + pr_debug(DRIVER_NAME ": ===========================================\n");
  37497. +}
  37498. +
  37499. +
  37500. +static void bcm2835_mmc_reset(struct bcm2835_host *host, u8 mask)
  37501. +{
  37502. + unsigned long timeout;
  37503. +
  37504. + bcm2835_mmc_writeb(host, mask, SDHCI_SOFTWARE_RESET);
  37505. +
  37506. + if (mask & SDHCI_RESET_ALL)
  37507. + host->clock = 0;
  37508. +
  37509. + /* Wait max 100 ms */
  37510. + timeout = 100;
  37511. +
  37512. + /* hw clears the bit when it's done */
  37513. + while (bcm2835_mmc_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
  37514. + if (timeout == 0) {
  37515. + pr_err("%s: Reset 0x%x never completed.\n",
  37516. + mmc_hostname(host->mmc), (int)mask);
  37517. + bcm2835_mmc_dumpregs(host);
  37518. + return;
  37519. + }
  37520. + timeout--;
  37521. + mdelay(1);
  37522. + }
  37523. +
  37524. + if (100-timeout > 10 && 100-timeout > host->max_delay) {
  37525. + host->max_delay = 100-timeout;
  37526. + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
  37527. + }
  37528. +}
  37529. +
  37530. +static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
  37531. +
  37532. +static void bcm2835_mmc_init(struct bcm2835_host *host, int soft)
  37533. +{
  37534. + if (soft)
  37535. + bcm2835_mmc_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
  37536. + else
  37537. + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
  37538. +
  37539. + host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
  37540. + SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
  37541. + SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
  37542. + SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
  37543. + SDHCI_INT_RESPONSE;
  37544. +
  37545. + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE);
  37546. + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  37547. +
  37548. + if (soft) {
  37549. + /* force clock reconfiguration */
  37550. + host->clock = 0;
  37551. + bcm2835_mmc_set_ios(host->mmc, &host->mmc->ios);
  37552. + }
  37553. +}
  37554. +
  37555. +
  37556. +
  37557. +static void bcm2835_mmc_finish_data(struct bcm2835_host *host);
  37558. +
  37559. +static void bcm2835_mmc_dma_complete(void *param)
  37560. +{
  37561. + struct bcm2835_host *host = param;
  37562. + struct dma_chan *dma_chan;
  37563. + unsigned long flags;
  37564. + u32 dir_data;
  37565. +
  37566. + spin_lock_irqsave(&host->lock, flags);
  37567. +
  37568. + if (host->data && !(host->data->flags & MMC_DATA_WRITE)) {
  37569. + /* otherwise handled in SDHCI IRQ */
  37570. + dma_chan = host->dma_chan_rx;
  37571. + dir_data = DMA_FROM_DEVICE;
  37572. +
  37573. + dma_unmap_sg(dma_chan->device->dev,
  37574. + host->data->sg, host->data->sg_len,
  37575. + dir_data);
  37576. +
  37577. + bcm2835_mmc_finish_data(host);
  37578. + }
  37579. +
  37580. + spin_unlock_irqrestore(&host->lock, flags);
  37581. +}
  37582. +
  37583. +static void bcm2835_bcm2835_mmc_read_block_pio(struct bcm2835_host *host)
  37584. +{
  37585. + unsigned long flags;
  37586. + size_t blksize, len, chunk;
  37587. +
  37588. + u32 uninitialized_var(scratch);
  37589. + u8 *buf;
  37590. +
  37591. + blksize = host->data->blksz;
  37592. + chunk = 0;
  37593. +
  37594. + local_irq_save(flags);
  37595. +
  37596. + while (blksize) {
  37597. + if (!sg_miter_next(&host->sg_miter))
  37598. + BUG();
  37599. +
  37600. + len = min(host->sg_miter.length, blksize);
  37601. +
  37602. + blksize -= len;
  37603. + host->sg_miter.consumed = len;
  37604. +
  37605. + buf = host->sg_miter.addr;
  37606. +
  37607. + while (len) {
  37608. + if (chunk == 0) {
  37609. + scratch = bcm2835_mmc_readl(host, SDHCI_BUFFER);
  37610. + chunk = 4;
  37611. + }
  37612. +
  37613. + *buf = scratch & 0xFF;
  37614. +
  37615. + buf++;
  37616. + scratch >>= 8;
  37617. + chunk--;
  37618. + len--;
  37619. + }
  37620. + }
  37621. +
  37622. + sg_miter_stop(&host->sg_miter);
  37623. +
  37624. + local_irq_restore(flags);
  37625. +}
  37626. +
  37627. +static void bcm2835_bcm2835_mmc_write_block_pio(struct bcm2835_host *host)
  37628. +{
  37629. + unsigned long flags;
  37630. + size_t blksize, len, chunk;
  37631. + u32 scratch;
  37632. + u8 *buf;
  37633. +
  37634. + blksize = host->data->blksz;
  37635. + chunk = 0;
  37636. + chunk = 0;
  37637. + scratch = 0;
  37638. +
  37639. + local_irq_save(flags);
  37640. +
  37641. + while (blksize) {
  37642. + if (!sg_miter_next(&host->sg_miter))
  37643. + BUG();
  37644. +
  37645. + len = min(host->sg_miter.length, blksize);
  37646. +
  37647. + blksize -= len;
  37648. + host->sg_miter.consumed = len;
  37649. +
  37650. + buf = host->sg_miter.addr;
  37651. +
  37652. + while (len) {
  37653. + scratch |= (u32)*buf << (chunk * 8);
  37654. +
  37655. + buf++;
  37656. + chunk++;
  37657. + len--;
  37658. +
  37659. + if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
  37660. + mmc_raw_writel(host, scratch, SDHCI_BUFFER);
  37661. + chunk = 0;
  37662. + scratch = 0;
  37663. + }
  37664. + }
  37665. + }
  37666. +
  37667. + sg_miter_stop(&host->sg_miter);
  37668. +
  37669. + local_irq_restore(flags);
  37670. +}
  37671. +
  37672. +
  37673. +static void bcm2835_mmc_transfer_pio(struct bcm2835_host *host)
  37674. +{
  37675. + u32 mask;
  37676. +
  37677. + BUG_ON(!host->data);
  37678. +
  37679. + if (host->blocks == 0)
  37680. + return;
  37681. +
  37682. + if (host->data->flags & MMC_DATA_READ)
  37683. + mask = SDHCI_DATA_AVAILABLE;
  37684. + else
  37685. + mask = SDHCI_SPACE_AVAILABLE;
  37686. +
  37687. + while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
  37688. +
  37689. + if (host->data->flags & MMC_DATA_READ)
  37690. + bcm2835_bcm2835_mmc_read_block_pio(host);
  37691. + else
  37692. + bcm2835_bcm2835_mmc_write_block_pio(host);
  37693. +
  37694. + host->blocks--;
  37695. +
  37696. + /* QUIRK used in sdhci.c removes the 'if' */
  37697. + /* but it seems this is unnecessary */
  37698. + if (host->blocks == 0)
  37699. + break;
  37700. +
  37701. +
  37702. + }
  37703. +}
  37704. +
  37705. +
  37706. +static void bcm2835_mmc_transfer_dma(struct bcm2835_host *host)
  37707. +{
  37708. + u32 len, dir_data, dir_slave;
  37709. + struct dma_async_tx_descriptor *desc = NULL;
  37710. + struct dma_chan *dma_chan;
  37711. +
  37712. +
  37713. + WARN_ON(!host->data);
  37714. +
  37715. + if (!host->data)
  37716. + return;
  37717. +
  37718. + if (host->blocks == 0)
  37719. + return;
  37720. +
  37721. + if (host->data->flags & MMC_DATA_READ) {
  37722. + dma_chan = host->dma_chan_rx;
  37723. + dir_data = DMA_FROM_DEVICE;
  37724. + dir_slave = DMA_DEV_TO_MEM;
  37725. + } else {
  37726. + dma_chan = host->dma_chan_tx;
  37727. + dir_data = DMA_TO_DEVICE;
  37728. + dir_slave = DMA_MEM_TO_DEV;
  37729. + }
  37730. +
  37731. + BUG_ON(!dma_chan->device);
  37732. + BUG_ON(!dma_chan->device->dev);
  37733. + BUG_ON(!host->data->sg);
  37734. +
  37735. + len = dma_map_sg(dma_chan->device->dev, host->data->sg,
  37736. + host->data->sg_len, dir_data);
  37737. + if (len > 0) {
  37738. + desc = dmaengine_prep_slave_sg(dma_chan, host->data->sg,
  37739. + len, dir_slave,
  37740. + DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  37741. + } else {
  37742. + dev_err(mmc_dev(host->mmc), "dma_map_sg returned zero length\n");
  37743. + }
  37744. + if (desc) {
  37745. + bcm2835_mmc_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
  37746. + SDHCI_INT_SPACE_AVAIL);
  37747. + host->tx_desc = desc;
  37748. + desc->callback = bcm2835_mmc_dma_complete;
  37749. + desc->callback_param = host;
  37750. + dmaengine_submit(desc);
  37751. + dma_async_issue_pending(dma_chan);
  37752. + }
  37753. +
  37754. +}
  37755. +
  37756. +
  37757. +
  37758. +static void bcm2835_mmc_set_transfer_irqs(struct bcm2835_host *host)
  37759. +{
  37760. + u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
  37761. + u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
  37762. +
  37763. + if (host->use_dma)
  37764. + host->ier = (host->ier & ~pio_irqs) | dma_irqs;
  37765. + else
  37766. + host->ier = (host->ier & ~dma_irqs) | pio_irqs;
  37767. +
  37768. + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE);
  37769. + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  37770. +}
  37771. +
  37772. +
  37773. +static void bcm2835_mmc_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
  37774. +{
  37775. + u8 count;
  37776. + struct mmc_data *data = cmd->data;
  37777. +
  37778. + WARN_ON(host->data);
  37779. +
  37780. + if (data || (cmd->flags & MMC_RSP_BUSY)) {
  37781. + count = TIMEOUT_VAL;
  37782. + bcm2835_mmc_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
  37783. + }
  37784. +
  37785. + if (!data)
  37786. + return;
  37787. +
  37788. + /* Sanity checks */
  37789. + BUG_ON(data->blksz * data->blocks > 524288);
  37790. + BUG_ON(data->blksz > host->mmc->max_blk_size);
  37791. + BUG_ON(data->blocks > 65535);
  37792. +
  37793. + host->data = data;
  37794. + host->data_early = 0;
  37795. + host->data->bytes_xfered = 0;
  37796. +
  37797. +
  37798. + if (!(host->flags & SDHCI_REQ_USE_DMA)) {
  37799. + int flags;
  37800. +
  37801. + flags = SG_MITER_ATOMIC;
  37802. + if (host->data->flags & MMC_DATA_READ)
  37803. + flags |= SG_MITER_TO_SG;
  37804. + else
  37805. + flags |= SG_MITER_FROM_SG;
  37806. + sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  37807. + host->blocks = data->blocks;
  37808. + }
  37809. +
  37810. + host->use_dma = host->have_dma && data->blocks > PIO_DMA_BARRIER;
  37811. +
  37812. + bcm2835_mmc_set_transfer_irqs(host);
  37813. +
  37814. + /* Set the DMA boundary value and block size */
  37815. + bcm2835_mmc_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
  37816. + data->blksz), SDHCI_BLOCK_SIZE);
  37817. + bcm2835_mmc_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
  37818. +
  37819. + BUG_ON(!host->data);
  37820. +}
  37821. +
  37822. +static void bcm2835_mmc_set_transfer_mode(struct bcm2835_host *host,
  37823. + struct mmc_command *cmd)
  37824. +{
  37825. + u16 mode;
  37826. + struct mmc_data *data = cmd->data;
  37827. +
  37828. + if (data == NULL) {
  37829. + /* clear Auto CMD settings for no data CMDs */
  37830. + mode = bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE);
  37831. + bcm2835_mmc_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
  37832. + SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
  37833. + return;
  37834. + }
  37835. +
  37836. + WARN_ON(!host->data);
  37837. +
  37838. + mode = SDHCI_TRNS_BLK_CNT_EN;
  37839. +
  37840. + if ((mmc_op_multi(cmd->opcode) || data->blocks > 1)) {
  37841. + mode |= SDHCI_TRNS_MULTI;
  37842. +
  37843. + /*
  37844. + * If we are sending CMD23, CMD12 never gets sent
  37845. + * on successful completion (so no Auto-CMD12).
  37846. + */
  37847. + if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
  37848. + mode |= SDHCI_TRNS_AUTO_CMD12;
  37849. + else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
  37850. + mode |= SDHCI_TRNS_AUTO_CMD23;
  37851. + bcm2835_mmc_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
  37852. + }
  37853. + }
  37854. +
  37855. + if (data->flags & MMC_DATA_READ)
  37856. + mode |= SDHCI_TRNS_READ;
  37857. + if (host->flags & SDHCI_REQ_USE_DMA)
  37858. + mode |= SDHCI_TRNS_DMA;
  37859. +
  37860. + bcm2835_mmc_writew(host, mode, SDHCI_TRANSFER_MODE);
  37861. +}
  37862. +
  37863. +void bcm2835_mmc_send_command(struct bcm2835_host *host, struct mmc_command *cmd)
  37864. +{
  37865. + int flags;
  37866. + u32 mask;
  37867. + unsigned long timeout;
  37868. +
  37869. + WARN_ON(host->cmd);
  37870. +
  37871. + /* Wait max 10 ms */
  37872. + timeout = 1000;
  37873. +
  37874. + mask = SDHCI_CMD_INHIBIT;
  37875. + if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
  37876. + mask |= SDHCI_DATA_INHIBIT;
  37877. +
  37878. + /* We shouldn't wait for data inihibit for stop commands, even
  37879. + though they might use busy signaling */
  37880. + if (host->mrq->data && (cmd == host->mrq->data->stop))
  37881. + mask &= ~SDHCI_DATA_INHIBIT;
  37882. +
  37883. + while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
  37884. + if (timeout == 0) {
  37885. + pr_err("%s: Controller never released inhibit bit(s).\n",
  37886. + mmc_hostname(host->mmc));
  37887. + bcm2835_mmc_dumpregs(host);
  37888. + cmd->error = -EIO;
  37889. + tasklet_schedule(&host->finish_tasklet);
  37890. + return;
  37891. + }
  37892. + timeout--;
  37893. + udelay(10);
  37894. + }
  37895. +
  37896. + if ((1000-timeout)/100 > 1 && (1000-timeout)/100 > host->max_delay) {
  37897. + host->max_delay = (1000-timeout)/100;
  37898. + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
  37899. + }
  37900. +
  37901. + timeout = jiffies;
  37902. +#ifdef CONFIG_OF
  37903. + if (!cmd->data && cmd->busy_timeout > 9000)
  37904. + timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
  37905. + else
  37906. +#endif
  37907. + timeout += 10 * HZ;
  37908. + mod_timer(&host->timer, timeout);
  37909. +
  37910. + host->cmd = cmd;
  37911. +
  37912. + bcm2835_mmc_prepare_data(host, cmd);
  37913. +
  37914. + bcm2835_mmc_writel(host, cmd->arg, SDHCI_ARGUMENT);
  37915. +
  37916. + bcm2835_mmc_set_transfer_mode(host, cmd);
  37917. +
  37918. + if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
  37919. + pr_err("%s: Unsupported response type!\n",
  37920. + mmc_hostname(host->mmc));
  37921. + cmd->error = -EINVAL;
  37922. + tasklet_schedule(&host->finish_tasklet);
  37923. + return;
  37924. + }
  37925. +
  37926. + if (!(cmd->flags & MMC_RSP_PRESENT))
  37927. + flags = SDHCI_CMD_RESP_NONE;
  37928. + else if (cmd->flags & MMC_RSP_136)
  37929. + flags = SDHCI_CMD_RESP_LONG;
  37930. + else if (cmd->flags & MMC_RSP_BUSY)
  37931. + flags = SDHCI_CMD_RESP_SHORT_BUSY;
  37932. + else
  37933. + flags = SDHCI_CMD_RESP_SHORT;
  37934. +
  37935. + if (cmd->flags & MMC_RSP_CRC)
  37936. + flags |= SDHCI_CMD_CRC;
  37937. + if (cmd->flags & MMC_RSP_OPCODE)
  37938. + flags |= SDHCI_CMD_INDEX;
  37939. +
  37940. + if (cmd->data)
  37941. + flags |= SDHCI_CMD_DATA;
  37942. +
  37943. + bcm2835_mmc_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
  37944. +}
  37945. +
  37946. +
  37947. +static void bcm2835_mmc_finish_data(struct bcm2835_host *host)
  37948. +{
  37949. + struct mmc_data *data;
  37950. +
  37951. + BUG_ON(!host->data);
  37952. +
  37953. + data = host->data;
  37954. + host->data = NULL;
  37955. +
  37956. + if (data->error)
  37957. + data->bytes_xfered = 0;
  37958. + else
  37959. + data->bytes_xfered = data->blksz * data->blocks;
  37960. +
  37961. + /*
  37962. + * Need to send CMD12 if -
  37963. + * a) open-ended multiblock transfer (no CMD23)
  37964. + * b) error in multiblock transfer
  37965. + */
  37966. + if (data->stop &&
  37967. + (data->error ||
  37968. + !host->mrq->sbc)) {
  37969. +
  37970. + /*
  37971. + * The controller needs a reset of internal state machines
  37972. + * upon error conditions.
  37973. + */
  37974. + if (data->error) {
  37975. + bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
  37976. + bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
  37977. + }
  37978. +
  37979. + bcm2835_mmc_send_command(host, data->stop);
  37980. + } else
  37981. + tasklet_schedule(&host->finish_tasklet);
  37982. +}
  37983. +
  37984. +static void bcm2835_mmc_finish_command(struct bcm2835_host *host)
  37985. +{
  37986. + int i;
  37987. +
  37988. + BUG_ON(host->cmd == NULL);
  37989. +
  37990. + if (host->cmd->flags & MMC_RSP_PRESENT) {
  37991. + if (host->cmd->flags & MMC_RSP_136) {
  37992. + /* CRC is stripped so we need to do some shifting. */
  37993. + for (i = 0; i < 4; i++) {
  37994. + host->cmd->resp[i] = bcm2835_mmc_readl(host,
  37995. + SDHCI_RESPONSE + (3-i)*4) << 8;
  37996. + if (i != 3)
  37997. + host->cmd->resp[i] |=
  37998. + bcm2835_mmc_readb(host,
  37999. + SDHCI_RESPONSE + (3-i)*4-1);
  38000. + }
  38001. + } else {
  38002. + host->cmd->resp[0] = bcm2835_mmc_readl(host, SDHCI_RESPONSE);
  38003. + }
  38004. + }
  38005. +
  38006. + host->cmd->error = 0;
  38007. +
  38008. + /* Finished CMD23, now send actual command. */
  38009. + if (host->cmd == host->mrq->sbc) {
  38010. + host->cmd = NULL;
  38011. + bcm2835_mmc_send_command(host, host->mrq->cmd);
  38012. + } else {
  38013. +
  38014. + /* Processed actual command. */
  38015. + if (host->data && host->data_early)
  38016. + bcm2835_mmc_finish_data(host);
  38017. +
  38018. + if (!host->cmd->data)
  38019. + tasklet_schedule(&host->finish_tasklet);
  38020. +
  38021. + host->cmd = NULL;
  38022. + }
  38023. +}
  38024. +
  38025. +
  38026. +static void bcm2835_mmc_timeout_timer(unsigned long data)
  38027. +{
  38028. + struct bcm2835_host *host;
  38029. + unsigned long flags;
  38030. +
  38031. + host = (struct bcm2835_host *)data;
  38032. +
  38033. + spin_lock_irqsave(&host->lock, flags);
  38034. +
  38035. + if (host->mrq) {
  38036. + pr_err("%s: Timeout waiting for hardware interrupt.\n",
  38037. + mmc_hostname(host->mmc));
  38038. + bcm2835_mmc_dumpregs(host);
  38039. +
  38040. + if (host->data) {
  38041. + host->data->error = -ETIMEDOUT;
  38042. + bcm2835_mmc_finish_data(host);
  38043. + } else {
  38044. + if (host->cmd)
  38045. + host->cmd->error = -ETIMEDOUT;
  38046. + else
  38047. + host->mrq->cmd->error = -ETIMEDOUT;
  38048. +
  38049. + tasklet_schedule(&host->finish_tasklet);
  38050. + }
  38051. + }
  38052. +
  38053. + mmiowb();
  38054. + spin_unlock_irqrestore(&host->lock, flags);
  38055. +}
  38056. +
  38057. +
  38058. +static void bcm2835_mmc_enable_sdio_irq_nolock(struct bcm2835_host *host, int enable)
  38059. +{
  38060. + if (!(host->flags & SDHCI_DEVICE_DEAD)) {
  38061. + if (enable)
  38062. + host->ier |= SDHCI_INT_CARD_INT;
  38063. + else
  38064. + host->ier &= ~SDHCI_INT_CARD_INT;
  38065. +
  38066. + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE);
  38067. + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  38068. + mmiowb();
  38069. + }
  38070. +}
  38071. +
  38072. +static void bcm2835_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  38073. +{
  38074. + struct bcm2835_host *host = mmc_priv(mmc);
  38075. + unsigned long flags;
  38076. +
  38077. + spin_lock_irqsave(&host->lock, flags);
  38078. + if (enable)
  38079. + host->flags |= SDHCI_SDIO_IRQ_ENABLED;
  38080. + else
  38081. + host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
  38082. +
  38083. + bcm2835_mmc_enable_sdio_irq_nolock(host, enable);
  38084. + spin_unlock_irqrestore(&host->lock, flags);
  38085. +}
  38086. +
  38087. +static void bcm2835_mmc_cmd_irq(struct bcm2835_host *host, u32 intmask)
  38088. +{
  38089. +
  38090. + BUG_ON(intmask == 0);
  38091. +
  38092. + if (!host->cmd) {
  38093. + pr_err("%s: Got command interrupt 0x%08x even "
  38094. + "though no command operation was in progress.\n",
  38095. + mmc_hostname(host->mmc), (unsigned)intmask);
  38096. + bcm2835_mmc_dumpregs(host);
  38097. + return;
  38098. + }
  38099. +
  38100. + if (intmask & SDHCI_INT_TIMEOUT)
  38101. + host->cmd->error = -ETIMEDOUT;
  38102. + else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
  38103. + SDHCI_INT_INDEX)) {
  38104. + host->cmd->error = -EILSEQ;
  38105. + }
  38106. +
  38107. + if (host->cmd->error) {
  38108. + tasklet_schedule(&host->finish_tasklet);
  38109. + return;
  38110. + }
  38111. +
  38112. + if (intmask & SDHCI_INT_RESPONSE)
  38113. + bcm2835_mmc_finish_command(host);
  38114. +
  38115. +}
  38116. +
  38117. +static void bcm2835_mmc_data_irq(struct bcm2835_host *host, u32 intmask)
  38118. +{
  38119. + struct dma_chan *dma_chan;
  38120. + u32 dir_data;
  38121. +
  38122. + BUG_ON(intmask == 0);
  38123. +
  38124. + if (!host->data) {
  38125. + /*
  38126. + * The "data complete" interrupt is also used to
  38127. + * indicate that a busy state has ended. See comment
  38128. + * above in sdhci_cmd_irq().
  38129. + */
  38130. + if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
  38131. + if (intmask & SDHCI_INT_DATA_END) {
  38132. + bcm2835_mmc_finish_command(host);
  38133. + return;
  38134. + }
  38135. + }
  38136. +
  38137. + pr_debug("%s: Got data interrupt 0x%08x even "
  38138. + "though no data operation was in progress.\n",
  38139. + mmc_hostname(host->mmc), (unsigned)intmask);
  38140. + bcm2835_mmc_dumpregs(host);
  38141. +
  38142. + return;
  38143. + }
  38144. +
  38145. + if (intmask & SDHCI_INT_DATA_TIMEOUT)
  38146. + host->data->error = -ETIMEDOUT;
  38147. + else if (intmask & SDHCI_INT_DATA_END_BIT)
  38148. + host->data->error = -EILSEQ;
  38149. + else if ((intmask & SDHCI_INT_DATA_CRC) &&
  38150. + SDHCI_GET_CMD(bcm2835_mmc_readw(host, SDHCI_COMMAND))
  38151. + != MMC_BUS_TEST_R)
  38152. + host->data->error = -EILSEQ;
  38153. +
  38154. + if (host->use_dma) {
  38155. + if (host->data->flags & MMC_DATA_WRITE) {
  38156. + /* IRQ handled here */
  38157. +
  38158. + dma_chan = host->dma_chan_tx;
  38159. + dir_data = DMA_TO_DEVICE;
  38160. + dma_unmap_sg(dma_chan->device->dev,
  38161. + host->data->sg, host->data->sg_len,
  38162. + dir_data);
  38163. +
  38164. + bcm2835_mmc_finish_data(host);
  38165. + }
  38166. +
  38167. + } else {
  38168. + if (host->data->error)
  38169. + bcm2835_mmc_finish_data(host);
  38170. + else {
  38171. + if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  38172. + bcm2835_mmc_transfer_pio(host);
  38173. +
  38174. + if (intmask & SDHCI_INT_DATA_END) {
  38175. + if (host->cmd) {
  38176. + /*
  38177. + * Data managed to finish before the
  38178. + * command completed. Make sure we do
  38179. + * things in the proper order.
  38180. + */
  38181. + host->data_early = 1;
  38182. + } else {
  38183. + bcm2835_mmc_finish_data(host);
  38184. + }
  38185. + }
  38186. + }
  38187. + }
  38188. +}
  38189. +
  38190. +
  38191. +static irqreturn_t bcm2835_mmc_irq(int irq, void *dev_id)
  38192. +{
  38193. + irqreturn_t result = IRQ_NONE;
  38194. + struct bcm2835_host *host = dev_id;
  38195. + u32 intmask, mask, unexpected = 0;
  38196. + int max_loops = 16;
  38197. +#ifndef CONFIG_OF
  38198. + int cardint = 0;
  38199. +#endif
  38200. +
  38201. + spin_lock(&host->lock);
  38202. +
  38203. + intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
  38204. +
  38205. + if (!intmask || intmask == 0xffffffff) {
  38206. + result = IRQ_NONE;
  38207. + goto out;
  38208. + }
  38209. +
  38210. + do {
  38211. + /* Clear selected interrupts. */
  38212. + mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
  38213. + SDHCI_INT_BUS_POWER);
  38214. + bcm2835_mmc_writel(host, mask, SDHCI_INT_STATUS);
  38215. +
  38216. +
  38217. + if (intmask & SDHCI_INT_CMD_MASK)
  38218. + bcm2835_mmc_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
  38219. +
  38220. + if (intmask & SDHCI_INT_DATA_MASK)
  38221. + bcm2835_mmc_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  38222. +
  38223. + if (intmask & SDHCI_INT_BUS_POWER)
  38224. + pr_err("%s: Card is consuming too much power!\n",
  38225. + mmc_hostname(host->mmc));
  38226. +
  38227. + if (intmask & SDHCI_INT_CARD_INT) {
  38228. +#ifndef CONFIG_OF
  38229. + cardint = 1;
  38230. +#else
  38231. + bcm2835_mmc_enable_sdio_irq_nolock(host, false);
  38232. + host->thread_isr |= SDHCI_INT_CARD_INT;
  38233. + result = IRQ_WAKE_THREAD;
  38234. +#endif
  38235. + }
  38236. +
  38237. + intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
  38238. + SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
  38239. + SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
  38240. + SDHCI_INT_CARD_INT);
  38241. +
  38242. + if (intmask) {
  38243. + unexpected |= intmask;
  38244. + bcm2835_mmc_writel(host, intmask, SDHCI_INT_STATUS);
  38245. + }
  38246. +
  38247. + if (result == IRQ_NONE)
  38248. + result = IRQ_HANDLED;
  38249. +
  38250. + intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
  38251. + } while (intmask && --max_loops);
  38252. +out:
  38253. + spin_unlock(&host->lock);
  38254. +
  38255. + if (unexpected) {
  38256. + pr_err("%s: Unexpected interrupt 0x%08x.\n",
  38257. + mmc_hostname(host->mmc), unexpected);
  38258. + bcm2835_mmc_dumpregs(host);
  38259. + }
  38260. +
  38261. +#ifndef CONFIG_OF
  38262. + if (cardint)
  38263. + mmc_signal_sdio_irq(host->mmc);
  38264. +#endif
  38265. +
  38266. + return result;
  38267. +}
  38268. +
  38269. +#ifdef CONFIG_OF
  38270. +static irqreturn_t bcm2835_mmc_thread_irq(int irq, void *dev_id)
  38271. +{
  38272. + struct bcm2835_host *host = dev_id;
  38273. + unsigned long flags;
  38274. + u32 isr;
  38275. +
  38276. + spin_lock_irqsave(&host->lock, flags);
  38277. + isr = host->thread_isr;
  38278. + host->thread_isr = 0;
  38279. + spin_unlock_irqrestore(&host->lock, flags);
  38280. +
  38281. + if (isr & SDHCI_INT_CARD_INT) {
  38282. + sdio_run_irqs(host->mmc);
  38283. +
  38284. + spin_lock_irqsave(&host->lock, flags);
  38285. + if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
  38286. + bcm2835_mmc_enable_sdio_irq_nolock(host, true);
  38287. + spin_unlock_irqrestore(&host->lock, flags);
  38288. + }
  38289. +
  38290. + return isr ? IRQ_HANDLED : IRQ_NONE;
  38291. +}
  38292. +#endif
  38293. +
  38294. +
  38295. +
  38296. +void bcm2835_mmc_set_clock(struct bcm2835_host *host, unsigned int clock)
  38297. +{
  38298. + int div = 0; /* Initialized for compiler warning */
  38299. + int real_div = div, clk_mul = 1;
  38300. + u16 clk = 0;
  38301. + unsigned long timeout;
  38302. +
  38303. +
  38304. + host->mmc->actual_clock = 0;
  38305. +
  38306. + bcm2835_mmc_writew(host, 0, SDHCI_CLOCK_CONTROL);
  38307. +
  38308. + if (clock == 0)
  38309. + return;
  38310. +
  38311. + /* Version 3.00 divisors must be a multiple of 2. */
  38312. + if (host->max_clk <= clock)
  38313. + div = 1;
  38314. + else {
  38315. + for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
  38316. + div += 2) {
  38317. + if ((host->max_clk / div) <= clock)
  38318. + break;
  38319. + }
  38320. + }
  38321. +
  38322. + real_div = div;
  38323. + div >>= 1;
  38324. +
  38325. + if (real_div)
  38326. + host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
  38327. +
  38328. + clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
  38329. + clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
  38330. + << SDHCI_DIVIDER_HI_SHIFT;
  38331. + clk |= SDHCI_CLOCK_INT_EN;
  38332. + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
  38333. +
  38334. + /* Wait max 20 ms */
  38335. + timeout = 20;
  38336. + while (!((clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL))
  38337. + & SDHCI_CLOCK_INT_STABLE)) {
  38338. + if (timeout == 0) {
  38339. + pr_err("%s: Internal clock never "
  38340. + "stabilised.\n", mmc_hostname(host->mmc));
  38341. + bcm2835_mmc_dumpregs(host);
  38342. + return;
  38343. + }
  38344. + timeout--;
  38345. + mdelay(1);
  38346. + }
  38347. +
  38348. + if (20-timeout > 10 && 20-timeout > host->max_delay) {
  38349. + host->max_delay = 20-timeout;
  38350. + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
  38351. + }
  38352. +
  38353. + clk |= SDHCI_CLOCK_CARD_EN;
  38354. + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
  38355. +}
  38356. +
  38357. +static void bcm2835_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  38358. +{
  38359. + struct bcm2835_host *host;
  38360. + unsigned long flags;
  38361. +
  38362. + host = mmc_priv(mmc);
  38363. +
  38364. + spin_lock_irqsave(&host->lock, flags);
  38365. +
  38366. + WARN_ON(host->mrq != NULL);
  38367. +
  38368. + host->mrq = mrq;
  38369. + bcm2835_mmc_send_command(host, mrq->cmd);
  38370. + mmiowb();
  38371. + spin_unlock_irqrestore(&host->lock, flags);
  38372. +
  38373. + if (mrq->cmd->data && host->use_dma) {
  38374. + /* DMA transfer starts now, PIO starts after interrupt */
  38375. + bcm2835_mmc_transfer_dma(host);
  38376. + }
  38377. +}
  38378. +
  38379. +
  38380. +static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  38381. +{
  38382. +
  38383. + struct bcm2835_host *host = mmc_priv(mmc);
  38384. + unsigned long flags;
  38385. + u8 ctrl;
  38386. + u16 clk, ctrl_2;
  38387. +
  38388. +
  38389. + spin_lock_irqsave(&host->lock, flags);
  38390. +
  38391. + if (!ios->clock || ios->clock != host->clock) {
  38392. + bcm2835_mmc_set_clock(host, ios->clock);
  38393. + host->clock = ios->clock;
  38394. + }
  38395. +
  38396. + if (host->pwr != SDHCI_POWER_330) {
  38397. + host->pwr = SDHCI_POWER_330;
  38398. + bcm2835_mmc_writeb(host, SDHCI_POWER_330 | SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
  38399. + }
  38400. +
  38401. + ctrl = bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL);
  38402. +
  38403. + /* set bus width */
  38404. + ctrl &= ~SDHCI_CTRL_8BITBUS;
  38405. + if (ios->bus_width == MMC_BUS_WIDTH_4)
  38406. + ctrl |= SDHCI_CTRL_4BITBUS;
  38407. + else
  38408. + ctrl &= ~SDHCI_CTRL_4BITBUS;
  38409. +
  38410. + ctrl &= ~SDHCI_CTRL_HISPD; /* NO_HISPD_BIT */
  38411. +
  38412. +
  38413. + bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  38414. + /*
  38415. + * We only need to set Driver Strength if the
  38416. + * preset value enable is not set.
  38417. + */
  38418. + ctrl_2 = bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2);
  38419. + ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
  38420. + if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
  38421. + ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
  38422. + else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
  38423. + ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
  38424. +
  38425. + bcm2835_mmc_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  38426. +
  38427. + /* Reset SD Clock Enable */
  38428. + clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL);
  38429. + clk &= ~SDHCI_CLOCK_CARD_EN;
  38430. + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
  38431. +
  38432. + /* Re-enable SD Clock */
  38433. + bcm2835_mmc_set_clock(host, host->clock);
  38434. + bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  38435. +
  38436. + mmiowb();
  38437. +
  38438. + spin_unlock_irqrestore(&host->lock, flags);
  38439. +}
  38440. +
  38441. +
  38442. +static struct mmc_host_ops bcm2835_ops = {
  38443. + .request = bcm2835_mmc_request,
  38444. + .set_ios = bcm2835_mmc_set_ios,
  38445. + .enable_sdio_irq = bcm2835_mmc_enable_sdio_irq,
  38446. +};
  38447. +
  38448. +
  38449. +static void bcm2835_mmc_tasklet_finish(unsigned long param)
  38450. +{
  38451. + struct bcm2835_host *host;
  38452. + unsigned long flags;
  38453. + struct mmc_request *mrq;
  38454. +
  38455. + host = (struct bcm2835_host *)param;
  38456. +
  38457. + spin_lock_irqsave(&host->lock, flags);
  38458. +
  38459. + /*
  38460. + * If this tasklet gets rescheduled while running, it will
  38461. + * be run again afterwards but without any active request.
  38462. + */
  38463. + if (!host->mrq) {
  38464. + spin_unlock_irqrestore(&host->lock, flags);
  38465. + return;
  38466. + }
  38467. +
  38468. + del_timer(&host->timer);
  38469. +
  38470. + mrq = host->mrq;
  38471. +
  38472. + /*
  38473. + * The controller needs a reset of internal state machines
  38474. + * upon error conditions.
  38475. + */
  38476. + if (!(host->flags & SDHCI_DEVICE_DEAD) &&
  38477. + ((mrq->cmd && mrq->cmd->error) ||
  38478. + (mrq->data && (mrq->data->error ||
  38479. + (mrq->data->stop && mrq->data->stop->error))))) {
  38480. +
  38481. + bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
  38482. + bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
  38483. + }
  38484. +
  38485. + host->mrq = NULL;
  38486. + host->cmd = NULL;
  38487. + host->data = NULL;
  38488. +
  38489. + mmiowb();
  38490. +
  38491. + spin_unlock_irqrestore(&host->lock, flags);
  38492. + mmc_request_done(host->mmc, mrq);
  38493. +}
  38494. +
  38495. +
  38496. +
  38497. +int bcm2835_mmc_add_host(struct bcm2835_host *host)
  38498. +{
  38499. + struct mmc_host *mmc;
  38500. +#ifndef FORCE_PIO
  38501. + struct dma_slave_config cfg;
  38502. +#endif
  38503. + int ret;
  38504. +
  38505. + mmc = host->mmc;
  38506. +
  38507. + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
  38508. +
  38509. + host->clk_mul = 0;
  38510. +
  38511. + mmc->ops = &bcm2835_ops;
  38512. + mmc->f_max = host->max_clk;
  38513. + mmc->f_max = host->max_clk;
  38514. + mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
  38515. +
  38516. + /* SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK */
  38517. + host->timeout_clk = mmc->f_max / 1000;
  38518. +#ifdef CONFIG_OF
  38519. + mmc->max_busy_timeout = (1 << 27) / host->timeout_clk;
  38520. +#endif
  38521. + /* host controller capabilities */
  38522. + mmc->caps = MMC_CAP_CMD23 | MMC_CAP_ERASE | MMC_CAP_NEEDS_POLL | MMC_CAP_SDIO_IRQ |
  38523. + MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED | MMC_CAP_4_BIT_DATA;
  38524. +
  38525. + host->flags = SDHCI_AUTO_CMD23;
  38526. +
  38527. + spin_lock_init(&host->lock);
  38528. +
  38529. +
  38530. +#ifdef FORCE_PIO
  38531. + pr_info("Forcing PIO mode\n");
  38532. + host->have_dma = false;
  38533. +#else
  38534. + if (!host->dma_chan_tx || !host->dma_chan_rx ||
  38535. + IS_ERR(host->dma_chan_tx) || IS_ERR(host->dma_chan_rx)) {
  38536. + pr_err("%s: Unable to initialise DMA channels. Falling back to PIO\n", DRIVER_NAME);
  38537. + host->have_dma = false;
  38538. + } else {
  38539. + pr_info("DMA channels allocated for the MMC driver");
  38540. + host->have_dma = true;
  38541. +
  38542. + cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  38543. + cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  38544. + cfg.slave_id = 11; /* DREQ channel */
  38545. +
  38546. + cfg.direction = DMA_MEM_TO_DEV;
  38547. + cfg.src_addr = 0;
  38548. + cfg.dst_addr = host->phys_addr + SDHCI_BUFFER;
  38549. + ret = dmaengine_slave_config(host->dma_chan_tx, &cfg);
  38550. +
  38551. + cfg.direction = DMA_DEV_TO_MEM;
  38552. + cfg.src_addr = host->phys_addr + SDHCI_BUFFER;
  38553. + cfg.dst_addr = 0;
  38554. + ret = dmaengine_slave_config(host->dma_chan_rx, &cfg);
  38555. + }
  38556. +#endif
  38557. +
  38558. +
  38559. + mmc->max_segs = 128;
  38560. + mmc->max_req_size = 524288;
  38561. + mmc->max_seg_size = mmc->max_req_size;
  38562. + mmc->max_blk_size = 512;
  38563. + mmc->max_blk_count = 65535;
  38564. +
  38565. + /* report supported voltage ranges */
  38566. + mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  38567. +
  38568. + tasklet_init(&host->finish_tasklet,
  38569. + bcm2835_mmc_tasklet_finish, (unsigned long)host);
  38570. +
  38571. + setup_timer(&host->timer, bcm2835_mmc_timeout_timer, (unsigned long)host);
  38572. + init_waitqueue_head(&host->buf_ready_int);
  38573. +
  38574. + bcm2835_mmc_init(host, 0);
  38575. +#ifndef CONFIG_OF
  38576. + ret = request_irq(host->irq, bcm2835_mmc_irq, 0 /*IRQF_SHARED*/,
  38577. + mmc_hostname(mmc), host);
  38578. +#else
  38579. + ret = request_threaded_irq(host->irq, bcm2835_mmc_irq, bcm2835_mmc_thread_irq,
  38580. + IRQF_SHARED, mmc_hostname(mmc), host);
  38581. +#endif
  38582. + if (ret) {
  38583. + pr_err("%s: Failed to request IRQ %d: %d\n",
  38584. + mmc_hostname(mmc), host->irq, ret);
  38585. + goto untasklet;
  38586. + }
  38587. +
  38588. + mmiowb();
  38589. + mmc_add_host(mmc);
  38590. +
  38591. + pr_info("Load BCM2835 MMC driver\n");
  38592. +
  38593. + return 0;
  38594. +
  38595. +untasklet:
  38596. + tasklet_kill(&host->finish_tasklet);
  38597. +
  38598. + return ret;
  38599. +}
  38600. +
  38601. +static int bcm2835_mmc_probe(struct platform_device *pdev)
  38602. +{
  38603. + struct device *dev = &pdev->dev;
  38604. +#ifdef CONFIG_OF
  38605. + struct device_node *node = dev->of_node;
  38606. + struct clk *clk;
  38607. +#endif
  38608. + struct resource *iomem;
  38609. + struct bcm2835_host *host = NULL;
  38610. +
  38611. + int ret;
  38612. + struct mmc_host *mmc;
  38613. +#if !defined(CONFIG_OF) && !defined(FORCE_PIO)
  38614. + dma_cap_mask_t mask;
  38615. +#endif
  38616. +
  38617. + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  38618. + if (!iomem) {
  38619. + ret = -ENOMEM;
  38620. + goto err;
  38621. + }
  38622. +
  38623. + if (resource_size(iomem) < 0x100)
  38624. + dev_err(&pdev->dev, "Invalid iomem size!\n");
  38625. +
  38626. + mmc = mmc_alloc_host(sizeof(struct bcm2835_host), dev);
  38627. + host = mmc_priv(mmc);
  38628. + host->mmc = mmc;
  38629. +
  38630. +
  38631. + if (IS_ERR(host)) {
  38632. + ret = PTR_ERR(host);
  38633. + goto err;
  38634. + }
  38635. +
  38636. + host->phys_addr = iomem->start + BCM2835_VCMMU_SHIFT;
  38637. +
  38638. +#ifndef CONFIG_OF
  38639. +#ifndef FORCE_PIO
  38640. + dma_cap_zero(mask);
  38641. + /* we don't care about the channel, any would work */
  38642. + dma_cap_set(DMA_SLAVE, mask);
  38643. +
  38644. + host->dma_chan_tx = dma_request_channel(mask, NULL, NULL);
  38645. + host->dma_chan_rx = dma_request_channel(mask, NULL, NULL);
  38646. +#endif
  38647. + host->max_clk = BCM2835_CLOCK_FREQ;
  38648. +
  38649. +#else
  38650. +#ifndef FORCE_PIO
  38651. + host->dma_chan_tx = of_dma_request_slave_channel(node, "tx");
  38652. + host->dma_chan_rx = of_dma_request_slave_channel(node, "rx");
  38653. +#endif
  38654. + clk = of_clk_get(node, 0);
  38655. + if (IS_ERR(clk)) {
  38656. + dev_err(dev, "get CLOCK failed\n");
  38657. + ret = PTR_ERR(clk);
  38658. + goto out;
  38659. + }
  38660. + host->max_clk = (clk_get_rate(clk));
  38661. +#endif
  38662. + host->irq = platform_get_irq(pdev, 0);
  38663. +
  38664. + if (!request_mem_region(iomem->start, resource_size(iomem),
  38665. + mmc_hostname(host->mmc))) {
  38666. + dev_err(&pdev->dev, "cannot request region\n");
  38667. + ret = -EBUSY;
  38668. + goto err_request;
  38669. + }
  38670. +
  38671. + host->ioaddr = ioremap(iomem->start, resource_size(iomem));
  38672. + if (!host->ioaddr) {
  38673. + dev_err(&pdev->dev, "failed to remap registers\n");
  38674. + ret = -ENOMEM;
  38675. + goto err_remap;
  38676. + }
  38677. +
  38678. + platform_set_drvdata(pdev, host);
  38679. +
  38680. +
  38681. + if (host->irq <= 0) {
  38682. + dev_err(dev, "get IRQ failed\n");
  38683. + ret = -EINVAL;
  38684. + goto out;
  38685. + }
  38686. +
  38687. +
  38688. +#ifndef CONFIG_OF
  38689. + mmc->caps |= MMC_CAP_4_BIT_DATA;
  38690. +#else
  38691. + mmc_of_parse(mmc);
  38692. +#endif
  38693. + host->timeout = msecs_to_jiffies(1000);
  38694. + spin_lock_init(&host->lock);
  38695. + mmc->ops = &bcm2835_ops;
  38696. + return bcm2835_mmc_add_host(host);
  38697. +
  38698. +
  38699. +err_remap:
  38700. + release_mem_region(iomem->start, resource_size(iomem));
  38701. +err_request:
  38702. + mmc_free_host(host->mmc);
  38703. +err:
  38704. + dev_err(&pdev->dev, "%s failed %d\n", __func__, ret);
  38705. + return ret;
  38706. +out:
  38707. + if (mmc)
  38708. + mmc_free_host(mmc);
  38709. + return ret;
  38710. +}
  38711. +
  38712. +static int bcm2835_mmc_remove(struct platform_device *pdev)
  38713. +{
  38714. + struct bcm2835_host *host = platform_get_drvdata(pdev);
  38715. + struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  38716. + unsigned long flags;
  38717. + int dead;
  38718. + u32 scratch;
  38719. +
  38720. + dead = 0;
  38721. + scratch = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
  38722. + if (scratch == (u32)-1)
  38723. + dead = 1;
  38724. +
  38725. +
  38726. + if (dead) {
  38727. + spin_lock_irqsave(&host->lock, flags);
  38728. +
  38729. + host->flags |= SDHCI_DEVICE_DEAD;
  38730. +
  38731. + if (host->mrq) {
  38732. + pr_err("%s: Controller removed during "
  38733. + " transfer!\n", mmc_hostname(host->mmc));
  38734. +
  38735. + host->mrq->cmd->error = -ENOMEDIUM;
  38736. + tasklet_schedule(&host->finish_tasklet);
  38737. + }
  38738. +
  38739. + spin_unlock_irqrestore(&host->lock, flags);
  38740. + }
  38741. +
  38742. + mmc_remove_host(host->mmc);
  38743. +
  38744. + if (!dead)
  38745. + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
  38746. +
  38747. + free_irq(host->irq, host);
  38748. +
  38749. + del_timer_sync(&host->timer);
  38750. +
  38751. + tasklet_kill(&host->finish_tasklet);
  38752. +
  38753. + iounmap(host->ioaddr);
  38754. + release_mem_region(iomem->start, resource_size(iomem));
  38755. + mmc_free_host(host->mmc);
  38756. + platform_set_drvdata(pdev, NULL);
  38757. +
  38758. + return 0;
  38759. +}
  38760. +
  38761. +
  38762. +static const struct of_device_id bcm2835_mmc_match[] = {
  38763. + { .compatible = "brcm,bcm2835-mmc" },
  38764. + { }
  38765. +};
  38766. +MODULE_DEVICE_TABLE(of, bcm2835_mmc_match);
  38767. +
  38768. +
  38769. +
  38770. +static struct platform_driver bcm2835_mmc_driver = {
  38771. + .probe = bcm2835_mmc_probe,
  38772. + .remove = bcm2835_mmc_remove,
  38773. + .driver = {
  38774. + .name = DRIVER_NAME,
  38775. + .owner = THIS_MODULE,
  38776. + .of_match_table = bcm2835_mmc_match,
  38777. + },
  38778. +};
  38779. +module_platform_driver(bcm2835_mmc_driver);
  38780. +
  38781. +MODULE_ALIAS("platform:mmc-bcm2835");
  38782. +MODULE_DESCRIPTION("BCM2835 SDHCI driver");
  38783. +MODULE_LICENSE("GPL v2");
  38784. +MODULE_AUTHOR("Gellert Weisz");
  38785. diff -Nur linux-3.12.33/drivers/mmc/host/Kconfig linux-3.12.33-rpi/drivers/mmc/host/Kconfig
  38786. --- linux-3.12.33/drivers/mmc/host/Kconfig 2014-11-15 06:28:07.000000000 -0600
  38787. +++ linux-3.12.33-rpi/drivers/mmc/host/Kconfig 2014-12-03 19:13:38.232418001 -0600
  38788. @@ -260,6 +260,27 @@
  38789. If you have a controller with this interface, say Y or M here.
  38790. +config MMC_SDHCI_BCM2708
  38791. + tristate "SDHCI support on BCM2708"
  38792. + depends on MMC_SDHCI && MACH_BCM2708
  38793. + select MMC_SDHCI_IO_ACCESSORS
  38794. + help
  38795. + This selects the Secure Digital Host Controller Interface (SDHCI)
  38796. + often referrered to as the eMMC block.
  38797. +
  38798. + If you have a controller with this interface, say Y or M here.
  38799. +
  38800. + If unsure, say N.
  38801. +
  38802. +config MMC_SDHCI_BCM2708_DMA
  38803. + bool "DMA support on BCM2708 Arasan controller"
  38804. + depends on MMC_SDHCI_BCM2708
  38805. + help
  38806. + Enable DMA support on the Arasan SDHCI controller in Broadcom 2708
  38807. + based chips.
  38808. +
  38809. + If unsure, say N.
  38810. +
  38811. config MMC_SDHCI_BCM2835
  38812. tristate "SDHCI platform support for the BCM2835 SD/MMC Controller"
  38813. depends on ARCH_BCM2835
  38814. @@ -271,6 +292,35 @@
  38815. If unsure, say N.
  38816. +config MMC_BCM2835
  38817. + tristate "MMC support on BCM2835"
  38818. + depends on MACH_BCM2708
  38819. + help
  38820. + This selects the MMC Interface on BCM2835.
  38821. +
  38822. + If you have a controller with this interface, say Y or M here.
  38823. +
  38824. + If unsure, say N.
  38825. +
  38826. +config MMC_BCM2835_DMA
  38827. + bool "DMA support on BCM2835 Arasan controller"
  38828. + depends on MMC_BCM2835
  38829. + help
  38830. + Enable DMA support on the Arasan SDHCI controller in Broadcom 2708
  38831. + based chips.
  38832. +
  38833. + If unsure, say N.
  38834. +
  38835. +config MMC_BCM2835_PIO_DMA_BARRIER
  38836. + int "Block count limit for PIO transfers"
  38837. + depends on MMC_BCM2835 && MMC_BCM2835_DMA
  38838. + range 0 256
  38839. + default 2
  38840. + help
  38841. + The inclusive limit in bytes under which PIO will be used instead of DMA
  38842. +
  38843. + If unsure, say 2 here.
  38844. +
  38845. config MMC_OMAP
  38846. tristate "TI OMAP Multimedia Card Interface support"
  38847. depends on ARCH_OMAP
  38848. diff -Nur linux-3.12.33/drivers/mmc/host/Makefile linux-3.12.33-rpi/drivers/mmc/host/Makefile
  38849. --- linux-3.12.33/drivers/mmc/host/Makefile 2014-11-15 06:28:07.000000000 -0600
  38850. +++ linux-3.12.33-rpi/drivers/mmc/host/Makefile 2014-12-03 19:13:38.232418001 -0600
  38851. @@ -15,6 +15,8 @@
  38852. obj-$(CONFIG_MMC_SDHCI_S3C) += sdhci-s3c.o
  38853. obj-$(CONFIG_MMC_SDHCI_SIRF) += sdhci-sirf.o
  38854. obj-$(CONFIG_MMC_SDHCI_SPEAR) += sdhci-spear.o
  38855. +obj-$(CONFIG_MMC_SDHCI_BCM2708) += sdhci-bcm2708.o
  38856. +obj-$(CONFIG_MMC_BCM2835) += bcm2835-mmc.o
  38857. obj-$(CONFIG_MMC_WBSD) += wbsd.o
  38858. obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
  38859. obj-$(CONFIG_MMC_OMAP) += omap.o
  38860. diff -Nur linux-3.12.33/drivers/mmc/host/sdhci-bcm2708.c linux-3.12.33-rpi/drivers/mmc/host/sdhci-bcm2708.c
  38861. --- linux-3.12.33/drivers/mmc/host/sdhci-bcm2708.c 1969-12-31 18:00:00.000000000 -0600
  38862. +++ linux-3.12.33-rpi/drivers/mmc/host/sdhci-bcm2708.c 2014-12-03 19:13:38.240418001 -0600
  38863. @@ -0,0 +1,1433 @@
  38864. +/*
  38865. + * sdhci-bcm2708.c Support for SDHCI device on BCM2708
  38866. + * Copyright (c) 2010 Broadcom
  38867. + *
  38868. + * This program is free software; you can redistribute it and/or modify
  38869. + * it under the terms of the GNU General Public License version 2 as
  38870. + * published by the Free Software Foundation.
  38871. + *
  38872. + * This program is distributed in the hope that it will be useful,
  38873. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38874. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38875. + * GNU General Public License for more details.
  38876. + *
  38877. + * You should have received a copy of the GNU General Public License
  38878. + * along with this program; if not, write to the Free Software
  38879. + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  38880. + */
  38881. +
  38882. +/* Supports:
  38883. + * SDHCI platform device - Arasan SD controller in BCM2708
  38884. + *
  38885. + * Inspired by sdhci-pci.c, by Pierre Ossman
  38886. + */
  38887. +
  38888. +#include <linux/delay.h>
  38889. +#include <linux/highmem.h>
  38890. +#include <linux/platform_device.h>
  38891. +#include <linux/module.h>
  38892. +#include <linux/mmc/mmc.h>
  38893. +#include <linux/mmc/host.h>
  38894. +#include <linux/mmc/sd.h>
  38895. +
  38896. +#include <linux/io.h>
  38897. +#include <linux/dma-mapping.h>
  38898. +#include <mach/dma.h>
  38899. +
  38900. +#include "sdhci.h"
  38901. +
  38902. +/*****************************************************************************\
  38903. + * *
  38904. + * Configuration *
  38905. + * *
  38906. +\*****************************************************************************/
  38907. +
  38908. +#define DRIVER_NAME "bcm2708_sdhci"
  38909. +
  38910. +/* for the time being insist on DMA mode - PIO seems not to work */
  38911. +#ifndef CONFIG_MMC_SDHCI_BCM2708_DMA
  38912. +#warning Non-DMA (PIO) version of this driver currently unavailable
  38913. +#endif
  38914. +#undef CONFIG_MMC_SDHCI_BCM2708_DMA
  38915. +#define CONFIG_MMC_SDHCI_BCM2708_DMA y
  38916. +
  38917. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  38918. +/* #define CHECK_DMA_USE */
  38919. +#endif
  38920. +//#define LOG_REGISTERS
  38921. +
  38922. +#define USE_SCHED_TIME
  38923. +#define USE_SPACED_WRITES_2CLK 1 /* space consecutive register writes */
  38924. +#define USE_SOFTWARE_TIMEOUTS 1 /* not hardware timeouts */
  38925. +#define SOFTWARE_ERASE_TIMEOUT_SEC 30
  38926. +
  38927. +#define SDHCI_BCM_DMA_CHAN 4 /* this default is normally overriden */
  38928. +#define SDHCI_BCM_DMA_WAITS 0 /* delays slowing DMA transfers: 0-31 */
  38929. +/* We are worried that SD card DMA use may be blocking the AXI bus for others */
  38930. +
  38931. +/*! TODO: obtain these from the physical address */
  38932. +#define DMA_SDHCI_BASE 0x7e300000 /* EMMC register block on Videocore */
  38933. +#define DMA_SDHCI_BUFFER (DMA_SDHCI_BASE + SDHCI_BUFFER)
  38934. +
  38935. +#define MAX_LITE_TRANSFER 32768
  38936. +#define MAX_NORMAL_TRANSFER 1073741824
  38937. +
  38938. +#define BCM2708_SDHCI_SLEEP_TIMEOUT 1000 /* msecs */
  38939. +
  38940. +/* Mhz clock that the EMMC core is running at. Should match the platform clockman settings */
  38941. +#define BCM2708_EMMC_CLOCK_FREQ 50000000
  38942. +
  38943. +#define REG_EXRDFIFO_EN 0x80
  38944. +#define REG_EXRDFIFO_CFG 0x84
  38945. +
  38946. +int cycle_delay=2;
  38947. +
  38948. +/*****************************************************************************\
  38949. + * *
  38950. + * Debug *
  38951. + * *
  38952. +\*****************************************************************************/
  38953. +
  38954. +
  38955. +
  38956. +#define DBG(f, x...) \
  38957. + pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
  38958. +// printk(KERN_INFO DRIVER_NAME " [%s()]: " f, __func__,## x)//GRAYG
  38959. +
  38960. +
  38961. +/*****************************************************************************\
  38962. + * *
  38963. + * High Precision Time *
  38964. + * *
  38965. +\*****************************************************************************/
  38966. +
  38967. +#ifdef USE_SCHED_TIME
  38968. +
  38969. +#include <mach/frc.h>
  38970. +
  38971. +typedef unsigned long hptime_t;
  38972. +
  38973. +#define FMT_HPT "lu"
  38974. +
  38975. +static inline hptime_t hptime(void)
  38976. +{
  38977. + return frc_clock_ticks32();
  38978. +}
  38979. +
  38980. +#define HPTIME_CLK_NS 1000ul
  38981. +
  38982. +#else
  38983. +
  38984. +typedef unsigned long hptime_t;
  38985. +
  38986. +#define FMT_HPT "lu"
  38987. +
  38988. +static inline hptime_t hptime(void)
  38989. +{
  38990. + return jiffies;
  38991. +}
  38992. +
  38993. +#define HPTIME_CLK_NS (1000000000ul/HZ)
  38994. +
  38995. +#endif
  38996. +
  38997. +static inline unsigned long int since_ns(hptime_t t)
  38998. +{
  38999. + return (unsigned long)((hptime() - t) * HPTIME_CLK_NS);
  39000. +}
  39001. +
  39002. +static bool allow_highspeed = 1;
  39003. +static int emmc_clock_freq = BCM2708_EMMC_CLOCK_FREQ;
  39004. +static bool sync_after_dma = 1;
  39005. +static bool missing_status = 1;
  39006. +static bool spurious_crc_acmd51 = 0;
  39007. +bool enable_llm = 1;
  39008. +bool extra_messages = 0;
  39009. +
  39010. +#if 0
  39011. +static void hptime_test(void)
  39012. +{
  39013. + hptime_t now;
  39014. + hptime_t later;
  39015. +
  39016. + now = hptime();
  39017. + msleep(10);
  39018. + later = hptime();
  39019. +
  39020. + printk(KERN_INFO DRIVER_NAME": 10ms = %"FMT_HPT" clks "
  39021. + "(from %"FMT_HPT" to %"FMT_HPT") = %luns\n",
  39022. + later-now, now, later,
  39023. + (unsigned long)(HPTIME_CLK_NS * (later - now)));
  39024. +
  39025. + now = hptime();
  39026. + msleep(1000);
  39027. + later = hptime();
  39028. +
  39029. + printk(KERN_INFO DRIVER_NAME": 1s = %"FMT_HPT" clks "
  39030. + "(from %"FMT_HPT" to %"FMT_HPT") = %luns\n",
  39031. + later-now, now, later,
  39032. + (unsigned long)(HPTIME_CLK_NS * (later - now)));
  39033. +}
  39034. +#endif
  39035. +
  39036. +/*****************************************************************************\
  39037. + * *
  39038. + * SDHCI core callbacks *
  39039. + * *
  39040. +\*****************************************************************************/
  39041. +
  39042. +
  39043. +#ifdef CHECK_DMA_USE
  39044. +/*#define CHECK_DMA_REG_USE*/
  39045. +#endif
  39046. +
  39047. +#ifdef CHECK_DMA_REG_USE
  39048. +/* we don't expect anything to be using these registers during a
  39049. + DMA (except the IRQ status) - so check */
  39050. +static void check_dma_reg_use(struct sdhci_host *host, int reg);
  39051. +#else
  39052. +#define check_dma_reg_use(host, reg)
  39053. +#endif
  39054. +
  39055. +
  39056. +static inline u32 sdhci_bcm2708_raw_readl(struct sdhci_host *host, int reg)
  39057. +{
  39058. + return readl(host->ioaddr + reg);
  39059. +}
  39060. +
  39061. +u32 sdhci_bcm2708_readl(struct sdhci_host *host, int reg)
  39062. +{
  39063. + u32 l = sdhci_bcm2708_raw_readl(host, reg);
  39064. +
  39065. +#ifdef LOG_REGISTERS
  39066. + printk(KERN_ERR "%s: readl from 0x%02x, value 0x%08x\n",
  39067. + mmc_hostname(host->mmc), reg, l);
  39068. +#endif
  39069. + check_dma_reg_use(host, reg);
  39070. +
  39071. + return l;
  39072. +}
  39073. +
  39074. +u16 sdhci_bcm2708_readw(struct sdhci_host *host, int reg)
  39075. +{
  39076. + u32 l = sdhci_bcm2708_raw_readl(host, reg & ~3);
  39077. + u32 w = l >> (reg << 3 & 0x18) & 0xffff;
  39078. +
  39079. +#ifdef LOG_REGISTERS
  39080. + printk(KERN_ERR "%s: readw from 0x%02x, value 0x%04x\n",
  39081. + mmc_hostname(host->mmc), reg, w);
  39082. +#endif
  39083. + check_dma_reg_use(host, reg);
  39084. +
  39085. + return (u16)w;
  39086. +}
  39087. +
  39088. +u8 sdhci_bcm2708_readb(struct sdhci_host *host, int reg)
  39089. +{
  39090. + u32 l = sdhci_bcm2708_raw_readl(host, reg & ~3);
  39091. + u32 b = l >> (reg << 3 & 0x18) & 0xff;
  39092. +
  39093. +#ifdef LOG_REGISTERS
  39094. + printk(KERN_ERR "%s: readb from 0x%02x, value 0x%02x\n",
  39095. + mmc_hostname(host->mmc), reg, b);
  39096. +#endif
  39097. + check_dma_reg_use(host, reg);
  39098. +
  39099. + return (u8)b;
  39100. +}
  39101. +
  39102. +
  39103. +static void sdhci_bcm2708_raw_writel(struct sdhci_host *host, u32 val, int reg)
  39104. +{
  39105. + u32 ier;
  39106. +
  39107. +#if USE_SPACED_WRITES_2CLK
  39108. + static bool timeout_disabled = false;
  39109. + unsigned int ns_2clk = 0;
  39110. +
  39111. + /* The Arasan has a bugette whereby it may lose the content of
  39112. + * successive writes to registers that are within two SD-card clock
  39113. + * cycles of each other (a clock domain crossing problem).
  39114. + * It seems, however, that the data register does not have this problem.
  39115. + * (Which is just as well - otherwise we'd have to nobble the DMA engine
  39116. + * too)
  39117. + */
  39118. + if (reg != SDHCI_BUFFER && host->clock != 0) {
  39119. + /* host->clock is the clock freq in Hz */
  39120. + static hptime_t last_write_hpt;
  39121. + hptime_t now = hptime();
  39122. + ns_2clk = cycle_delay*1000000/(host->clock/1000);
  39123. +
  39124. + if (now == last_write_hpt || now == last_write_hpt+1) {
  39125. + /* we can't guarantee any significant time has
  39126. + * passed - we'll have to wait anyway ! */
  39127. + ndelay(ns_2clk);
  39128. + } else
  39129. + {
  39130. + /* we must have waited at least this many ns: */
  39131. + unsigned int ns_wait = HPTIME_CLK_NS *
  39132. + (now - last_write_hpt - 1);
  39133. + if (ns_wait < ns_2clk)
  39134. + ndelay(ns_2clk - ns_wait);
  39135. + }
  39136. + last_write_hpt = now;
  39137. + }
  39138. +#if USE_SOFTWARE_TIMEOUTS
  39139. + /* The Arasan is clocked for timeouts using the SD clock which is too
  39140. + * fast for ERASE commands and causes issues. So we disable timeouts
  39141. + * for ERASE */
  39142. + if (host->cmd != NULL && host->cmd->opcode == MMC_ERASE &&
  39143. + reg == (SDHCI_COMMAND & ~3)) {
  39144. + mod_timer(&host->timer,
  39145. + jiffies + SOFTWARE_ERASE_TIMEOUT_SEC * HZ);
  39146. + ier = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
  39147. + ier &= ~SDHCI_INT_DATA_TIMEOUT;
  39148. + writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
  39149. + timeout_disabled = true;
  39150. + ndelay(ns_2clk);
  39151. + } else if (timeout_disabled) {
  39152. + ier = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
  39153. + ier |= SDHCI_INT_DATA_TIMEOUT;
  39154. + writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
  39155. + timeout_disabled = false;
  39156. + ndelay(ns_2clk);
  39157. + }
  39158. +#endif
  39159. + writel(val, host->ioaddr + reg);
  39160. +#else
  39161. + void __iomem * regaddr = host->ioaddr + reg;
  39162. +
  39163. + writel(val, regaddr);
  39164. +
  39165. + if (reg != SDHCI_BUFFER && reg != SDHCI_INT_STATUS && host->clock != 0)
  39166. + {
  39167. + int timeout = 100000;
  39168. + while (val != readl(regaddr) && --timeout > 0)
  39169. + continue;
  39170. +
  39171. + if (timeout <= 0)
  39172. + printk(KERN_ERR "%s: writing 0x%X to reg 0x%X "
  39173. + "always gives 0x%X\n",
  39174. + mmc_hostname(host->mmc),
  39175. + val, reg, readl(regaddr));
  39176. + BUG_ON(timeout <= 0);
  39177. + }
  39178. +#endif
  39179. +}
  39180. +
  39181. +
  39182. +void sdhci_bcm2708_writel(struct sdhci_host *host, u32 val, int reg)
  39183. +{
  39184. +#ifdef LOG_REGISTERS
  39185. + printk(KERN_ERR "%s: writel to 0x%02x, value 0x%08x\n",
  39186. + mmc_hostname(host->mmc), reg, val);
  39187. +#endif
  39188. + check_dma_reg_use(host, reg);
  39189. +
  39190. + sdhci_bcm2708_raw_writel(host, val, reg);
  39191. +}
  39192. +
  39193. +void sdhci_bcm2708_writew(struct sdhci_host *host, u16 val, int reg)
  39194. +{
  39195. + static u32 shadow = 0;
  39196. +
  39197. + u32 p = reg == SDHCI_COMMAND ? shadow :
  39198. + sdhci_bcm2708_raw_readl(host, reg & ~3);
  39199. + u32 s = reg << 3 & 0x18;
  39200. + u32 l = val << s;
  39201. + u32 m = 0xffff << s;
  39202. +
  39203. +#ifdef LOG_REGISTERS
  39204. + printk(KERN_ERR "%s: writew to 0x%02x, value 0x%04x\n",
  39205. + mmc_hostname(host->mmc), reg, val);
  39206. +#endif
  39207. +
  39208. + if (reg == SDHCI_TRANSFER_MODE)
  39209. + shadow = (p & ~m) | l;
  39210. + else {
  39211. + check_dma_reg_use(host, reg);
  39212. + sdhci_bcm2708_raw_writel(host, (p & ~m) | l, reg & ~3);
  39213. + }
  39214. +}
  39215. +
  39216. +void sdhci_bcm2708_writeb(struct sdhci_host *host, u8 val, int reg)
  39217. +{
  39218. + u32 p = sdhci_bcm2708_raw_readl(host, reg & ~3);
  39219. + u32 s = reg << 3 & 0x18;
  39220. + u32 l = val << s;
  39221. + u32 m = 0xff << s;
  39222. +
  39223. +#ifdef LOG_REGISTERS
  39224. + printk(KERN_ERR "%s: writeb to 0x%02x, value 0x%02x\n",
  39225. + mmc_hostname(host->mmc), reg, val);
  39226. +#endif
  39227. +
  39228. + check_dma_reg_use(host, reg);
  39229. + sdhci_bcm2708_raw_writel(host, (p & ~m) | l, reg & ~3);
  39230. +}
  39231. +
  39232. +static unsigned int sdhci_bcm2708_get_max_clock(struct sdhci_host *host)
  39233. +{
  39234. + return emmc_clock_freq;
  39235. +}
  39236. +
  39237. +/*****************************************************************************\
  39238. + * *
  39239. + * DMA Operation *
  39240. + * *
  39241. +\*****************************************************************************/
  39242. +
  39243. +struct sdhci_bcm2708_priv {
  39244. + int dma_chan;
  39245. + int dma_irq;
  39246. + void __iomem *dma_chan_base;
  39247. + struct bcm2708_dma_cb *cb_base; /* DMA control blocks */
  39248. + dma_addr_t cb_handle;
  39249. + /* tracking scatter gather progress */
  39250. + unsigned sg_ix; /* scatter gather list index */
  39251. + unsigned sg_done; /* bytes in current sg_ix done */
  39252. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  39253. + unsigned char dma_wanted; /* DMA transfer requested */
  39254. + unsigned char dma_waits; /* wait states in DMAs */
  39255. +#ifdef CHECK_DMA_USE
  39256. + unsigned char dmas_pending; /* no of unfinished DMAs */
  39257. + hptime_t when_started;
  39258. + hptime_t when_reset;
  39259. + hptime_t when_stopped;
  39260. +#endif
  39261. +#endif
  39262. + /* signalling the end of a transfer */
  39263. + void (*complete)(struct sdhci_host *);
  39264. +};
  39265. +
  39266. +#define SDHCI_HOST_PRIV(host) \
  39267. + (struct sdhci_bcm2708_priv *)((struct sdhci_host *)(host)+1)
  39268. +
  39269. +
  39270. +
  39271. +#ifdef CHECK_DMA_REG_USE
  39272. +static void check_dma_reg_use(struct sdhci_host *host, int reg)
  39273. +{
  39274. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  39275. + if (host_priv->dma_wanted && reg != SDHCI_INT_STATUS) {
  39276. + printk(KERN_INFO"%s: accessing register 0x%x during DMA\n",
  39277. + mmc_hostname(host->mmc), reg);
  39278. + }
  39279. +}
  39280. +#endif
  39281. +
  39282. +
  39283. +
  39284. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  39285. +
  39286. +static void sdhci_clear_set_irqgen(struct sdhci_host *host, u32 clear, u32 set)
  39287. +{
  39288. + u32 ier;
  39289. +
  39290. + ier = sdhci_bcm2708_raw_readl(host, SDHCI_SIGNAL_ENABLE);
  39291. + ier &= ~clear;
  39292. + ier |= set;
  39293. + /* change which requests generate IRQs - makes no difference to
  39294. + the content of SDHCI_INT_STATUS, or the need to acknowledge IRQs */
  39295. + sdhci_bcm2708_raw_writel(host, ier, SDHCI_SIGNAL_ENABLE);
  39296. +}
  39297. +
  39298. +static void sdhci_signal_irqs(struct sdhci_host *host, u32 irqs)
  39299. +{
  39300. + sdhci_clear_set_irqgen(host, 0, irqs);
  39301. +}
  39302. +
  39303. +static void sdhci_unsignal_irqs(struct sdhci_host *host, u32 irqs)
  39304. +{
  39305. + sdhci_clear_set_irqgen(host, irqs, 0);
  39306. +}
  39307. +
  39308. +
  39309. +
  39310. +static void schci_bcm2708_cb_read(struct sdhci_bcm2708_priv *host,
  39311. + int ix,
  39312. + dma_addr_t dma_addr, unsigned len,
  39313. + int /*bool*/ is_last)
  39314. +{
  39315. + struct bcm2708_dma_cb *cb;
  39316. + unsigned char dmawaits = host->dma_waits;
  39317. + unsigned i, max_size;
  39318. +
  39319. + if (host->dma_chan >= 8) /* we have a LITE channel */
  39320. + max_size = MAX_LITE_TRANSFER;
  39321. + else
  39322. + max_size = MAX_NORMAL_TRANSFER;
  39323. +
  39324. + for (i = 0; i < len; i += max_size) {
  39325. + cb = &host->cb_base[ix+i/max_size];
  39326. +
  39327. + cb->info = BCM2708_DMA_PER_MAP(BCM2708_DMA_DREQ_EMMC) |
  39328. + BCM2708_DMA_WAITS(dmawaits) |
  39329. + BCM2708_DMA_WAIT_RESP |
  39330. + BCM2708_DMA_S_DREQ |
  39331. + BCM2708_DMA_D_WIDTH |
  39332. + BCM2708_DMA_D_INC;
  39333. + cb->src = DMA_SDHCI_BUFFER; /* DATA register DMA address */
  39334. + cb->dst = dma_addr + (dma_addr_t)i;
  39335. + cb->length = min(len-i, max_size);
  39336. + cb->stride = 0;
  39337. +
  39338. + if (is_last && len-i <= max_size) {
  39339. + cb->info |= BCM2708_DMA_INT_EN;
  39340. + cb->next = 0;
  39341. + } else
  39342. + cb->next = host->cb_handle +
  39343. + (ix+1 + i/max_size)*sizeof(struct bcm2708_dma_cb);
  39344. +
  39345. + cb->pad[0] = 0;
  39346. + cb->pad[1] = 0;
  39347. + }
  39348. +}
  39349. +
  39350. +static void schci_bcm2708_cb_write(struct sdhci_bcm2708_priv *host,
  39351. + int ix,
  39352. + dma_addr_t dma_addr, unsigned len,
  39353. + int /*bool*/ is_last)
  39354. +{
  39355. + struct bcm2708_dma_cb *cb = &host->cb_base[ix];
  39356. + unsigned char dmawaits = host->dma_waits;
  39357. + unsigned i, max_size;
  39358. +
  39359. + if (host->dma_chan >= 8) /* we have a LITE channel */
  39360. + max_size = MAX_LITE_TRANSFER;
  39361. + else
  39362. + max_size = MAX_NORMAL_TRANSFER;
  39363. +
  39364. + /* We can make arbitrarily large writes as long as we specify DREQ to
  39365. + pace the delivery of bytes to the Arasan hardware. However we need
  39366. + to take care when using LITE channels */
  39367. +
  39368. + for (i = 0; i < len; i += max_size) {
  39369. + cb = &host->cb_base[ix+i/max_size];
  39370. +
  39371. + cb->info = BCM2708_DMA_PER_MAP(BCM2708_DMA_DREQ_EMMC) |
  39372. + BCM2708_DMA_WAITS(dmawaits) |
  39373. + BCM2708_DMA_WAIT_RESP |
  39374. + BCM2708_DMA_D_DREQ |
  39375. + BCM2708_DMA_S_WIDTH |
  39376. + BCM2708_DMA_S_INC;
  39377. + cb->src = dma_addr + (dma_addr_t)i;
  39378. + cb->dst = DMA_SDHCI_BUFFER; /* DATA register DMA address */
  39379. + cb->length = min(len-i, max_size);
  39380. + cb->stride = 0;
  39381. +
  39382. + if (is_last && len-i <= max_size) {
  39383. + cb->info |= BCM2708_DMA_INT_EN;
  39384. + cb->next = 0;
  39385. + } else
  39386. + cb->next = host->cb_handle +
  39387. + (ix+1 + i/max_size)*sizeof(struct bcm2708_dma_cb);
  39388. +
  39389. + cb->pad[0] = 0;
  39390. + cb->pad[1] = 0;
  39391. + }
  39392. +}
  39393. +
  39394. +
  39395. +static void schci_bcm2708_dma_go(struct sdhci_host *host)
  39396. +{
  39397. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  39398. + void __iomem *dma_chan_base = host_priv->dma_chan_base;
  39399. +
  39400. + BUG_ON(host_priv->dma_wanted);
  39401. +#ifdef CHECK_DMA_USE
  39402. + if (host_priv->dma_wanted)
  39403. + printk(KERN_ERR "%s: DMA already in progress - "
  39404. + "now %"FMT_HPT", last started %lu "
  39405. + "reset %lu stopped %lu\n",
  39406. + mmc_hostname(host->mmc),
  39407. + hptime(), since_ns(host_priv->when_started),
  39408. + since_ns(host_priv->when_reset),
  39409. + since_ns(host_priv->when_stopped));
  39410. + else if (host_priv->dmas_pending > 0)
  39411. + printk(KERN_INFO "%s: note - new DMA when %d reset DMAs "
  39412. + "already in progress - "
  39413. + "now %"FMT_HPT", started %lu reset %lu stopped %lu\n",
  39414. + mmc_hostname(host->mmc),
  39415. + host_priv->dmas_pending,
  39416. + hptime(), since_ns(host_priv->when_started),
  39417. + since_ns(host_priv->when_reset),
  39418. + since_ns(host_priv->when_stopped));
  39419. + host_priv->dmas_pending += 1;
  39420. + host_priv->when_started = hptime();
  39421. +#endif
  39422. + host_priv->dma_wanted = 1;
  39423. + DBG("PDMA go - base %p handle %08X\n", dma_chan_base,
  39424. + host_priv->cb_handle);
  39425. + bcm_dma_start(dma_chan_base, host_priv->cb_handle);
  39426. +}
  39427. +
  39428. +
  39429. +static void
  39430. +sdhci_platdma_read(struct sdhci_host *host, dma_addr_t dma_addr, size_t len)
  39431. +{
  39432. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  39433. +
  39434. + DBG("PDMA to read %d bytes\n", len);
  39435. + host_priv->sg_done += len;
  39436. + schci_bcm2708_cb_read(host_priv, 0, dma_addr, len, 1/*TRUE*/);
  39437. + schci_bcm2708_dma_go(host);
  39438. +}
  39439. +
  39440. +
  39441. +static void
  39442. +sdhci_platdma_write(struct sdhci_host *host, dma_addr_t dma_addr, size_t len)
  39443. +{
  39444. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  39445. +
  39446. + DBG("PDMA to write %d bytes\n", len);
  39447. + //BUG_ON(0 != (len & 0x1ff));
  39448. +
  39449. + host_priv->sg_done += len;
  39450. + schci_bcm2708_cb_write(host_priv, 0, dma_addr, len, 1/*TRUE*/);
  39451. + schci_bcm2708_dma_go(host);
  39452. +}
  39453. +
  39454. +/*! space is avaiable to receive into or data is available to write
  39455. + Platform DMA exported function
  39456. +*/
  39457. +void
  39458. +sdhci_bcm2708_platdma_avail(struct sdhci_host *host, unsigned int *ref_intmask,
  39459. + void(*completion_callback)(struct sdhci_host *host))
  39460. +{
  39461. + struct mmc_data *data = host->data;
  39462. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  39463. + int sg_ix;
  39464. + size_t bytes;
  39465. + dma_addr_t addr;
  39466. +
  39467. + BUG_ON(NULL == data);
  39468. + BUG_ON(0 == data->blksz);
  39469. +
  39470. + host_priv->complete = completion_callback;
  39471. +
  39472. + sg_ix = host_priv->sg_ix;
  39473. + BUG_ON(sg_ix >= data->sg_len);
  39474. +
  39475. + /* we can DMA blocks larger than blksz - it may hang the DMA
  39476. + channel but we are its only user */
  39477. + bytes = sg_dma_len(&data->sg[sg_ix]) - host_priv->sg_done;
  39478. + addr = sg_dma_address(&data->sg[sg_ix]) + host_priv->sg_done;
  39479. +
  39480. + if (bytes > 0) {
  39481. + /* We're going to poll for read/write available state until
  39482. + we finish this DMA
  39483. + */
  39484. +
  39485. + if (data->flags & MMC_DATA_READ) {
  39486. + if (*ref_intmask & SDHCI_INT_DATA_AVAIL) {
  39487. + sdhci_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
  39488. + SDHCI_INT_SPACE_AVAIL);
  39489. + sdhci_platdma_read(host, addr, bytes);
  39490. + }
  39491. + } else {
  39492. + if (*ref_intmask & SDHCI_INT_SPACE_AVAIL) {
  39493. + sdhci_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
  39494. + SDHCI_INT_SPACE_AVAIL);
  39495. + sdhci_platdma_write(host, addr, bytes);
  39496. + }
  39497. + }
  39498. + }
  39499. + /* else:
  39500. + we have run out of bytes that need transferring (e.g. we may be in
  39501. + the middle of the last DMA transfer), or
  39502. + it is also possible that we've been called when another IRQ is
  39503. + signalled, even though we've turned off signalling of our own IRQ */
  39504. +
  39505. + *ref_intmask &= ~SDHCI_INT_DATA_END;
  39506. + /* don't let the main sdhci driver act on this .. we'll deal with it
  39507. + when we respond to the DMA - if one is currently in progress */
  39508. +}
  39509. +
  39510. +/* is it possible to DMA the given mmc_data structure?
  39511. + Platform DMA exported function
  39512. +*/
  39513. +int /*bool*/
  39514. +sdhci_bcm2708_platdma_dmaable(struct sdhci_host *host, struct mmc_data *data)
  39515. +{
  39516. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  39517. + int ok = bcm_sg_suitable_for_dma(data->sg, data->sg_len);
  39518. +
  39519. + if (!ok)
  39520. + DBG("Reverting to PIO - bad cache alignment\n");
  39521. +
  39522. + else {
  39523. + host_priv->sg_ix = 0; /* first SG index */
  39524. + host_priv->sg_done = 0; /* no bytes done */
  39525. + }
  39526. +
  39527. + return ok;
  39528. +}
  39529. +
  39530. +#include <mach/arm_control.h> //GRAYG
  39531. +/*! the current SD transacton has been abandonned
  39532. + We need to tidy up if we were in the middle of a DMA
  39533. + Platform DMA exported function
  39534. +*/
  39535. +void
  39536. +sdhci_bcm2708_platdma_reset(struct sdhci_host *host, struct mmc_data *data)
  39537. +{
  39538. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  39539. +// unsigned long flags;
  39540. +
  39541. + BUG_ON(NULL == host);
  39542. +
  39543. +// spin_lock_irqsave(&host->lock, flags);
  39544. +
  39545. + if (host_priv->dma_wanted) {
  39546. + if (NULL == data) {
  39547. + printk(KERN_ERR "%s: ongoing DMA reset - no data!\n",
  39548. + mmc_hostname(host->mmc));
  39549. + BUG_ON(NULL == data);
  39550. + } else {
  39551. + struct scatterlist *sg;
  39552. + int sg_len;
  39553. + int sg_todo;
  39554. + int rc;
  39555. + unsigned long cs;
  39556. +
  39557. + sg = data->sg;
  39558. + sg_len = data->sg_len;
  39559. + sg_todo = sg_dma_len(&sg[host_priv->sg_ix]);
  39560. +
  39561. + cs = readl(host_priv->dma_chan_base + BCM2708_DMA_CS);
  39562. +
  39563. + if (!(BCM2708_DMA_ACTIVE & cs))
  39564. + {
  39565. + if (extra_messages)
  39566. + printk(KERN_INFO "%s: missed completion of "
  39567. + "cmd %d DMA (%d/%d [%d]/[%d]) - "
  39568. + "ignoring it\n",
  39569. + mmc_hostname(host->mmc),
  39570. + host->last_cmdop,
  39571. + host_priv->sg_done, sg_todo,
  39572. + host_priv->sg_ix+1, sg_len);
  39573. + }
  39574. + else
  39575. + printk(KERN_INFO "%s: resetting ongoing cmd %d"
  39576. + "DMA before %d/%d [%d]/[%d] complete\n",
  39577. + mmc_hostname(host->mmc),
  39578. + host->last_cmdop,
  39579. + host_priv->sg_done, sg_todo,
  39580. + host_priv->sg_ix+1, sg_len);
  39581. +#ifdef CHECK_DMA_USE
  39582. + printk(KERN_INFO "%s: now %"FMT_HPT" started %lu "
  39583. + "last reset %lu last stopped %lu\n",
  39584. + mmc_hostname(host->mmc),
  39585. + hptime(), since_ns(host_priv->when_started),
  39586. + since_ns(host_priv->when_reset),
  39587. + since_ns(host_priv->when_stopped));
  39588. + { unsigned long info, debug;
  39589. + void __iomem *base;
  39590. + unsigned long pend0, pend1, pend2;
  39591. +
  39592. + base = host_priv->dma_chan_base;
  39593. + cs = readl(base + BCM2708_DMA_CS);
  39594. + info = readl(base + BCM2708_DMA_INFO);
  39595. + debug = readl(base + BCM2708_DMA_DEBUG);
  39596. + printk(KERN_INFO "%s: DMA%d CS=%08lX TI=%08lX "
  39597. + "DEBUG=%08lX\n",
  39598. + mmc_hostname(host->mmc),
  39599. + host_priv->dma_chan,
  39600. + cs, info, debug);
  39601. + pend0 = readl(__io_address(ARM_IRQ_PEND0));
  39602. + pend1 = readl(__io_address(ARM_IRQ_PEND1));
  39603. + pend2 = readl(__io_address(ARM_IRQ_PEND2));
  39604. +
  39605. + printk(KERN_INFO "%s: PEND0=%08lX "
  39606. + "PEND1=%08lX PEND2=%08lX\n",
  39607. + mmc_hostname(host->mmc),
  39608. + pend0, pend1, pend2);
  39609. +
  39610. + //gintsts = readl(__io_address(GINTSTS));
  39611. + //gintmsk = readl(__io_address(GINTMSK));
  39612. + //printk(KERN_INFO "%s: USB GINTSTS=%08lX"
  39613. + // "GINTMSK=%08lX\n",
  39614. + // mmc_hostname(host->mmc), gintsts, gintmsk);
  39615. + }
  39616. +#endif
  39617. + rc = bcm_dma_abort(host_priv->dma_chan_base);
  39618. + BUG_ON(rc != 0);
  39619. + }
  39620. + host_priv->dma_wanted = 0;
  39621. +#ifdef CHECK_DMA_USE
  39622. + host_priv->when_reset = hptime();
  39623. +#endif
  39624. + }
  39625. +
  39626. +// spin_unlock_irqrestore(&host->lock, flags);
  39627. +}
  39628. +
  39629. +
  39630. +static void sdhci_bcm2708_dma_complete_irq(struct sdhci_host *host,
  39631. + u32 dma_cs)
  39632. +{
  39633. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  39634. + struct mmc_data *data;
  39635. + struct scatterlist *sg;
  39636. + int sg_len;
  39637. + int sg_ix;
  39638. + int sg_todo;
  39639. +// unsigned long flags;
  39640. +
  39641. + BUG_ON(NULL == host);
  39642. +
  39643. +// spin_lock_irqsave(&host->lock, flags);
  39644. + data = host->data;
  39645. +
  39646. +#ifdef CHECK_DMA_USE
  39647. + if (host_priv->dmas_pending <= 0)
  39648. + DBG("on completion no DMA in progress - "
  39649. + "now %"FMT_HPT" started %lu reset %lu stopped %lu\n",
  39650. + hptime(), since_ns(host_priv->when_started),
  39651. + since_ns(host_priv->when_reset),
  39652. + since_ns(host_priv->when_stopped));
  39653. + else if (host_priv->dmas_pending > 1)
  39654. + DBG("still %d DMA in progress after completion - "
  39655. + "now %"FMT_HPT" started %lu reset %lu stopped %lu\n",
  39656. + host_priv->dmas_pending - 1,
  39657. + hptime(), since_ns(host_priv->when_started),
  39658. + since_ns(host_priv->when_reset),
  39659. + since_ns(host_priv->when_stopped));
  39660. + BUG_ON(host_priv->dmas_pending <= 0);
  39661. + host_priv->dmas_pending -= 1;
  39662. + host_priv->when_stopped = hptime();
  39663. +#endif
  39664. + host_priv->dma_wanted = 0;
  39665. +
  39666. + if (NULL == data) {
  39667. + DBG("PDMA unused completion - status 0x%X\n", dma_cs);
  39668. +// spin_unlock_irqrestore(&host->lock, flags);
  39669. + return;
  39670. + }
  39671. + sg = data->sg;
  39672. + sg_len = data->sg_len;
  39673. + sg_todo = sg_dma_len(&sg[host_priv->sg_ix]);
  39674. +
  39675. + DBG("PDMA complete %d/%d [%d]/[%d]..\n",
  39676. + host_priv->sg_done, sg_todo,
  39677. + host_priv->sg_ix+1, sg_len);
  39678. +
  39679. + BUG_ON(host_priv->sg_done > sg_todo);
  39680. +
  39681. + if (host_priv->sg_done >= sg_todo) {
  39682. + host_priv->sg_ix++;
  39683. + host_priv->sg_done = 0;
  39684. + }
  39685. +
  39686. + sg_ix = host_priv->sg_ix;
  39687. + if (sg_ix < sg_len) {
  39688. + u32 irq_mask;
  39689. + /* Set off next DMA if we've got the capacity */
  39690. +
  39691. + if (data->flags & MMC_DATA_READ)
  39692. + irq_mask = SDHCI_INT_DATA_AVAIL;
  39693. + else
  39694. + irq_mask = SDHCI_INT_SPACE_AVAIL;
  39695. +
  39696. + /* We have to use the interrupt status register on the BCM2708
  39697. + rather than the SDHCI_PRESENT_STATE register because latency
  39698. + in the glue logic means that the information retrieved from
  39699. + the latter is not always up-to-date w.r.t the DMA engine -
  39700. + it may not indicate that a read or a write is ready yet */
  39701. + if (sdhci_bcm2708_raw_readl(host, SDHCI_INT_STATUS) &
  39702. + irq_mask) {
  39703. + size_t bytes = sg_dma_len(&sg[sg_ix]) -
  39704. + host_priv->sg_done;
  39705. + dma_addr_t addr = sg_dma_address(&data->sg[sg_ix]) +
  39706. + host_priv->sg_done;
  39707. +
  39708. + /* acknowledge interrupt */
  39709. + sdhci_bcm2708_raw_writel(host, irq_mask,
  39710. + SDHCI_INT_STATUS);
  39711. +
  39712. + BUG_ON(0 == bytes);
  39713. +
  39714. + if (data->flags & MMC_DATA_READ)
  39715. + sdhci_platdma_read(host, addr, bytes);
  39716. + else
  39717. + sdhci_platdma_write(host, addr, bytes);
  39718. + } else {
  39719. + DBG("PDMA - wait avail\n");
  39720. + /* may generate an IRQ if already present */
  39721. + sdhci_signal_irqs(host, SDHCI_INT_DATA_AVAIL |
  39722. + SDHCI_INT_SPACE_AVAIL);
  39723. + }
  39724. + } else {
  39725. + if (sync_after_dma) {
  39726. + /* On the Arasan controller the stop command (which will be
  39727. + scheduled after this completes) does not seem to work
  39728. + properly if we allow it to be issued when we are
  39729. + transferring data to/from the SD card.
  39730. + We get CRC and DEND errors unless we wait for
  39731. + the SD controller to finish reading/writing to the card. */
  39732. + u32 state_mask;
  39733. + int timeout=3*1000*1000;
  39734. +
  39735. + DBG("PDMA over - sync card\n");
  39736. + if (data->flags & MMC_DATA_READ)
  39737. + state_mask = SDHCI_DOING_READ;
  39738. + else
  39739. + state_mask = SDHCI_DOING_WRITE;
  39740. +
  39741. + while (0 != (sdhci_bcm2708_raw_readl(host, SDHCI_PRESENT_STATE)
  39742. + & state_mask) && --timeout > 0)
  39743. + {
  39744. + udelay(1);
  39745. + continue;
  39746. + }
  39747. + if (timeout <= 0)
  39748. + printk(KERN_ERR"%s: final %s to SD card still "
  39749. + "running\n",
  39750. + mmc_hostname(host->mmc),
  39751. + data->flags & MMC_DATA_READ? "read": "write");
  39752. + }
  39753. + if (host_priv->complete) {
  39754. + (*host_priv->complete)(host);
  39755. + DBG("PDMA %s complete\n",
  39756. + data->flags & MMC_DATA_READ?"read":"write");
  39757. + sdhci_signal_irqs(host, SDHCI_INT_DATA_AVAIL |
  39758. + SDHCI_INT_SPACE_AVAIL);
  39759. + }
  39760. + }
  39761. +// spin_unlock_irqrestore(&host->lock, flags);
  39762. +}
  39763. +
  39764. +static irqreturn_t sdhci_bcm2708_dma_irq(int irq, void *dev_id)
  39765. +{
  39766. + irqreturn_t result = IRQ_NONE;
  39767. + struct sdhci_host *host = dev_id;
  39768. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  39769. + u32 dma_cs; /* control and status register */
  39770. +
  39771. + BUG_ON(NULL == dev_id);
  39772. + BUG_ON(NULL == host_priv->dma_chan_base);
  39773. +
  39774. + sdhci_spin_lock(host);
  39775. +
  39776. + dma_cs = readl(host_priv->dma_chan_base + BCM2708_DMA_CS);
  39777. +
  39778. + if (dma_cs & BCM2708_DMA_ERR) {
  39779. + unsigned long debug;
  39780. + debug = readl(host_priv->dma_chan_base +
  39781. + BCM2708_DMA_DEBUG);
  39782. + printk(KERN_ERR "%s: DMA error - CS %lX DEBUG %lX\n",
  39783. + mmc_hostname(host->mmc), (unsigned long)dma_cs,
  39784. + (unsigned long)debug);
  39785. + /* reset error */
  39786. + writel(debug, host_priv->dma_chan_base +
  39787. + BCM2708_DMA_DEBUG);
  39788. + }
  39789. + if (dma_cs & BCM2708_DMA_INT) {
  39790. + /* acknowledge interrupt */
  39791. + writel(BCM2708_DMA_INT,
  39792. + host_priv->dma_chan_base + BCM2708_DMA_CS);
  39793. +
  39794. + dsb(); /* ARM data synchronization (push) operation */
  39795. +
  39796. + if (!host_priv->dma_wanted) {
  39797. + /* ignore this interrupt - it was reset */
  39798. + if (extra_messages)
  39799. + printk(KERN_INFO "%s: DMA IRQ %X ignored - "
  39800. + "results were reset\n",
  39801. + mmc_hostname(host->mmc), dma_cs);
  39802. +#ifdef CHECK_DMA_USE
  39803. + printk(KERN_INFO "%s: now %"FMT_HPT
  39804. + " started %lu reset %lu stopped %lu\n",
  39805. + mmc_hostname(host->mmc), hptime(),
  39806. + since_ns(host_priv->when_started),
  39807. + since_ns(host_priv->when_reset),
  39808. + since_ns(host_priv->when_stopped));
  39809. + host_priv->dmas_pending--;
  39810. +#endif
  39811. + } else
  39812. + sdhci_bcm2708_dma_complete_irq(host, dma_cs);
  39813. +
  39814. + result = IRQ_HANDLED;
  39815. + }
  39816. + sdhci_spin_unlock(host);
  39817. +
  39818. + return result;
  39819. +}
  39820. +#endif /* CONFIG_MMC_SDHCI_BCM2708_DMA */
  39821. +
  39822. +
  39823. +/***************************************************************************** \
  39824. + * *
  39825. + * Device Attributes *
  39826. + * *
  39827. +\*****************************************************************************/
  39828. +
  39829. +
  39830. +/**
  39831. + * Show the DMA-using status
  39832. + */
  39833. +static ssize_t attr_dma_show(struct device *_dev,
  39834. + struct device_attribute *attr, char *buf)
  39835. +{
  39836. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  39837. +
  39838. + if (host) {
  39839. + int use_dma = (host->flags & SDHCI_USE_PLATDMA? 1:0);
  39840. + return sprintf(buf, "%d\n", use_dma);
  39841. + } else
  39842. + return -EINVAL;
  39843. +}
  39844. +
  39845. +/**
  39846. + * Set the DMA-using status
  39847. + */
  39848. +static ssize_t attr_dma_store(struct device *_dev,
  39849. + struct device_attribute *attr,
  39850. + const char *buf, size_t count)
  39851. +{
  39852. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  39853. +
  39854. + if (host) {
  39855. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  39856. + int on = simple_strtol(buf, NULL, 0);
  39857. + if (on) {
  39858. + host->flags |= SDHCI_USE_PLATDMA;
  39859. + sdhci_bcm2708_writel(host, 1, REG_EXRDFIFO_EN);
  39860. + printk(KERN_INFO "%s: DMA enabled\n",
  39861. + mmc_hostname(host->mmc));
  39862. + } else {
  39863. + host->flags &= ~(SDHCI_USE_PLATDMA | SDHCI_REQ_USE_DMA);
  39864. + sdhci_bcm2708_writel(host, 0, REG_EXRDFIFO_EN);
  39865. + printk(KERN_INFO "%s: DMA disabled\n",
  39866. + mmc_hostname(host->mmc));
  39867. + }
  39868. +#endif
  39869. + return count;
  39870. + } else
  39871. + return -EINVAL;
  39872. +}
  39873. +
  39874. +static DEVICE_ATTR(use_dma, S_IRUGO | S_IWUGO, attr_dma_show, attr_dma_store);
  39875. +
  39876. +
  39877. +/**
  39878. + * Show the DMA wait states used
  39879. + */
  39880. +static ssize_t attr_dmawait_show(struct device *_dev,
  39881. + struct device_attribute *attr, char *buf)
  39882. +{
  39883. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  39884. +
  39885. + if (host) {
  39886. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  39887. + int dmawait = host_priv->dma_waits;
  39888. + return sprintf(buf, "%d\n", dmawait);
  39889. + } else
  39890. + return -EINVAL;
  39891. +}
  39892. +
  39893. +/**
  39894. + * Set the DMA wait state used
  39895. + */
  39896. +static ssize_t attr_dmawait_store(struct device *_dev,
  39897. + struct device_attribute *attr,
  39898. + const char *buf, size_t count)
  39899. +{
  39900. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  39901. +
  39902. + if (host) {
  39903. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  39904. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  39905. + int dma_waits = simple_strtol(buf, NULL, 0);
  39906. + if (dma_waits >= 0 && dma_waits < 32)
  39907. + host_priv->dma_waits = dma_waits;
  39908. + else
  39909. + printk(KERN_ERR "%s: illegal dma_waits value - %d",
  39910. + mmc_hostname(host->mmc), dma_waits);
  39911. +#endif
  39912. + return count;
  39913. + } else
  39914. + return -EINVAL;
  39915. +}
  39916. +
  39917. +static DEVICE_ATTR(dma_wait, S_IRUGO | S_IWUGO,
  39918. + attr_dmawait_show, attr_dmawait_store);
  39919. +
  39920. +
  39921. +/**
  39922. + * Show the DMA-using status
  39923. + */
  39924. +static ssize_t attr_status_show(struct device *_dev,
  39925. + struct device_attribute *attr, char *buf)
  39926. +{
  39927. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  39928. +
  39929. + if (host) {
  39930. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  39931. + return sprintf(buf,
  39932. + "present: yes\n"
  39933. + "power: %s\n"
  39934. + "clock: %u Hz\n"
  39935. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  39936. + "dma: %s (%d waits)\n",
  39937. +#else
  39938. + "dma: unconfigured\n",
  39939. +#endif
  39940. + "always on",
  39941. + host->clock
  39942. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  39943. + , (host->flags & SDHCI_USE_PLATDMA)? "on": "off"
  39944. + , host_priv->dma_waits
  39945. +#endif
  39946. + );
  39947. + } else
  39948. + return -EINVAL;
  39949. +}
  39950. +
  39951. +static DEVICE_ATTR(status, S_IRUGO, attr_status_show, NULL);
  39952. +
  39953. +/***************************************************************************** \
  39954. + * *
  39955. + * Power Management *
  39956. + * *
  39957. +\*****************************************************************************/
  39958. +
  39959. +
  39960. +#ifdef CONFIG_PM
  39961. +static int sdhci_bcm2708_suspend(struct platform_device *dev, pm_message_t state)
  39962. +{
  39963. + struct sdhci_host *host = (struct sdhci_host *)
  39964. + platform_get_drvdata(dev);
  39965. + int ret = 0;
  39966. +
  39967. + if (host->mmc) {
  39968. + //ret = mmc_suspend_host(host->mmc);
  39969. + }
  39970. +
  39971. + return ret;
  39972. +}
  39973. +
  39974. +static int sdhci_bcm2708_resume(struct platform_device *dev)
  39975. +{
  39976. + struct sdhci_host *host = (struct sdhci_host *)
  39977. + platform_get_drvdata(dev);
  39978. + int ret = 0;
  39979. +
  39980. + if (host->mmc) {
  39981. + //ret = mmc_resume_host(host->mmc);
  39982. + }
  39983. +
  39984. + return ret;
  39985. +}
  39986. +#endif
  39987. +
  39988. +
  39989. +/*****************************************************************************\
  39990. + * *
  39991. + * Device quirk functions. Implemented as local ops because the flags *
  39992. + * field is out of space with newer kernels. This implementation can be *
  39993. + * back ported to older kernels as well. *
  39994. +\****************************************************************************/
  39995. +static unsigned int sdhci_bcm2708_quirk_extra_ints(struct sdhci_host *host)
  39996. +{
  39997. + return 1;
  39998. +}
  39999. +
  40000. +static unsigned int sdhci_bcm2708_quirk_spurious_crc_acmd51(struct sdhci_host *host)
  40001. +{
  40002. + return 1;
  40003. +}
  40004. +
  40005. +static unsigned int sdhci_bcm2708_missing_status(struct sdhci_host *host)
  40006. +{
  40007. + return 1;
  40008. +}
  40009. +
  40010. +/***************************************************************************** \
  40011. + * *
  40012. + * Device ops *
  40013. + * *
  40014. +\*****************************************************************************/
  40015. +
  40016. +static struct sdhci_ops sdhci_bcm2708_ops = {
  40017. +#ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS
  40018. + .read_l = sdhci_bcm2708_readl,
  40019. + .read_w = sdhci_bcm2708_readw,
  40020. + .read_b = sdhci_bcm2708_readb,
  40021. + .write_l = sdhci_bcm2708_writel,
  40022. + .write_w = sdhci_bcm2708_writew,
  40023. + .write_b = sdhci_bcm2708_writeb,
  40024. +#else
  40025. +#error The BCM2708 SDHCI driver needs CONFIG_MMC_SDHCI_IO_ACCESSORS to be set
  40026. +#endif
  40027. + .get_max_clock = sdhci_bcm2708_get_max_clock,
  40028. +
  40029. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  40030. + // Platform DMA operations
  40031. + .pdma_able = sdhci_bcm2708_platdma_dmaable,
  40032. + .pdma_avail = sdhci_bcm2708_platdma_avail,
  40033. + .pdma_reset = sdhci_bcm2708_platdma_reset,
  40034. +#endif
  40035. + .extra_ints = sdhci_bcm2708_quirk_extra_ints,
  40036. +};
  40037. +
  40038. +/*****************************************************************************\
  40039. + * *
  40040. + * Device probing/removal *
  40041. + * *
  40042. +\*****************************************************************************/
  40043. +
  40044. +static int sdhci_bcm2708_probe(struct platform_device *pdev)
  40045. +{
  40046. + struct sdhci_host *host;
  40047. + struct resource *iomem;
  40048. + struct sdhci_bcm2708_priv *host_priv;
  40049. + int ret;
  40050. +
  40051. + BUG_ON(pdev == NULL);
  40052. +
  40053. + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  40054. + if (!iomem) {
  40055. + ret = -ENOMEM;
  40056. + goto err;
  40057. + }
  40058. +
  40059. + if (resource_size(iomem) != 0x100)
  40060. + dev_err(&pdev->dev, "Invalid iomem size. You may "
  40061. + "experience problems.\n");
  40062. +
  40063. + if (pdev->dev.parent)
  40064. + host = sdhci_alloc_host(pdev->dev.parent,
  40065. + sizeof(struct sdhci_bcm2708_priv));
  40066. + else
  40067. + host = sdhci_alloc_host(&pdev->dev,
  40068. + sizeof(struct sdhci_bcm2708_priv));
  40069. +
  40070. + if (IS_ERR(host)) {
  40071. + ret = PTR_ERR(host);
  40072. + goto err;
  40073. + }
  40074. + if (missing_status) {
  40075. + sdhci_bcm2708_ops.missing_status = sdhci_bcm2708_missing_status;
  40076. + }
  40077. +
  40078. + if( spurious_crc_acmd51 ) {
  40079. + sdhci_bcm2708_ops.spurious_crc_acmd51 = sdhci_bcm2708_quirk_spurious_crc_acmd51;
  40080. + }
  40081. +
  40082. +
  40083. + printk("sdhci: %s low-latency mode\n",enable_llm?"Enable":"Disable");
  40084. +
  40085. + host->hw_name = "BCM2708_Arasan";
  40086. + host->ops = &sdhci_bcm2708_ops;
  40087. + host->irq = platform_get_irq(pdev, 0);
  40088. + host->second_irq = 0;
  40089. +
  40090. + host->quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  40091. + SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  40092. + SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  40093. + SDHCI_QUIRK_MISSING_CAPS |
  40094. + SDHCI_QUIRK_NO_HISPD_BIT |
  40095. + (sync_after_dma ? 0:SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12);
  40096. +
  40097. +
  40098. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  40099. + host->flags = SDHCI_USE_PLATDMA;
  40100. +#endif
  40101. +
  40102. + if (!request_mem_region(iomem->start, resource_size(iomem),
  40103. + mmc_hostname(host->mmc))) {
  40104. + dev_err(&pdev->dev, "cannot request region\n");
  40105. + ret = -EBUSY;
  40106. + goto err_request;
  40107. + }
  40108. +
  40109. + host->ioaddr = ioremap(iomem->start, resource_size(iomem));
  40110. + if (!host->ioaddr) {
  40111. + dev_err(&pdev->dev, "failed to remap registers\n");
  40112. + ret = -ENOMEM;
  40113. + goto err_remap;
  40114. + }
  40115. +
  40116. + host_priv = SDHCI_HOST_PRIV(host);
  40117. +
  40118. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  40119. + host_priv->dma_wanted = 0;
  40120. +#ifdef CHECK_DMA_USE
  40121. + host_priv->dmas_pending = 0;
  40122. + host_priv->when_started = 0;
  40123. + host_priv->when_reset = 0;
  40124. + host_priv->when_stopped = 0;
  40125. +#endif
  40126. + host_priv->sg_ix = 0;
  40127. + host_priv->sg_done = 0;
  40128. + host_priv->complete = NULL;
  40129. + host_priv->dma_waits = SDHCI_BCM_DMA_WAITS;
  40130. +
  40131. + host_priv->cb_base = dma_alloc_writecombine(&pdev->dev, SZ_4K,
  40132. + &host_priv->cb_handle,
  40133. + GFP_KERNEL);
  40134. + if (!host_priv->cb_base) {
  40135. + dev_err(&pdev->dev, "cannot allocate DMA CBs\n");
  40136. + ret = -ENOMEM;
  40137. + goto err_alloc_cb;
  40138. + }
  40139. +
  40140. + ret = bcm_dma_chan_alloc(BCM_DMA_FEATURE_FAST,
  40141. + &host_priv->dma_chan_base,
  40142. + &host_priv->dma_irq);
  40143. + if (ret < 0) {
  40144. + dev_err(&pdev->dev, "couldn't allocate a DMA channel\n");
  40145. + goto err_add_dma;
  40146. + }
  40147. + host_priv->dma_chan = ret;
  40148. +
  40149. + ret = request_irq(host_priv->dma_irq, sdhci_bcm2708_dma_irq,
  40150. + 0 /*IRQF_SHARED*/, DRIVER_NAME " (dma)", host);
  40151. + if (ret) {
  40152. + dev_err(&pdev->dev, "cannot set DMA IRQ\n");
  40153. + goto err_add_dma_irq;
  40154. + }
  40155. + host->second_irq = host_priv->dma_irq;
  40156. + DBG("DMA CBs %p handle %08X DMA%d %p DMA IRQ %d\n",
  40157. + host_priv->cb_base, (unsigned)host_priv->cb_handle,
  40158. + host_priv->dma_chan, host_priv->dma_chan_base,
  40159. + host_priv->dma_irq);
  40160. +
  40161. + // we support 3.3V
  40162. + host->caps |= SDHCI_CAN_VDD_330;
  40163. + if (allow_highspeed)
  40164. + host->mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  40165. +
  40166. + /* single block writes cause data loss with some SD cards! */
  40167. + host->mmc->caps2 |= MMC_CAP2_FORCE_MULTIBLOCK;
  40168. +#endif
  40169. +
  40170. + ret = sdhci_add_host(host);
  40171. + if (ret)
  40172. + goto err_add_host;
  40173. +
  40174. + platform_set_drvdata(pdev, host);
  40175. + ret = device_create_file(&pdev->dev, &dev_attr_use_dma);
  40176. + ret = device_create_file(&pdev->dev, &dev_attr_dma_wait);
  40177. + ret = device_create_file(&pdev->dev, &dev_attr_status);
  40178. +
  40179. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  40180. + /* enable extension fifo for paced DMA transfers */
  40181. + sdhci_bcm2708_writel(host, 1, REG_EXRDFIFO_EN);
  40182. + sdhci_bcm2708_writel(host, 4, REG_EXRDFIFO_CFG);
  40183. +#endif
  40184. +
  40185. + printk(KERN_INFO "%s: BCM2708 SDHC host at 0x%08llx DMA %d IRQ %d\n",
  40186. + mmc_hostname(host->mmc), (unsigned long long)iomem->start,
  40187. + host_priv->dma_chan, host_priv->dma_irq);
  40188. +
  40189. + return 0;
  40190. +
  40191. +err_add_host:
  40192. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  40193. + free_irq(host_priv->dma_irq, host);
  40194. +err_add_dma_irq:
  40195. + bcm_dma_chan_free(host_priv->dma_chan);
  40196. +err_add_dma:
  40197. + dma_free_writecombine(&pdev->dev, SZ_4K, host_priv->cb_base,
  40198. + host_priv->cb_handle);
  40199. +err_alloc_cb:
  40200. +#endif
  40201. + iounmap(host->ioaddr);
  40202. +err_remap:
  40203. + release_mem_region(iomem->start, resource_size(iomem));
  40204. +err_request:
  40205. + sdhci_free_host(host);
  40206. +err:
  40207. + dev_err(&pdev->dev, "probe failed, err %d\n", ret);
  40208. + return ret;
  40209. +}
  40210. +
  40211. +static int sdhci_bcm2708_remove(struct platform_device *pdev)
  40212. +{
  40213. + struct sdhci_host *host = platform_get_drvdata(pdev);
  40214. + struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  40215. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  40216. + int dead;
  40217. + u32 scratch;
  40218. +
  40219. + dead = 0;
  40220. + scratch = sdhci_bcm2708_readl(host, SDHCI_INT_STATUS);
  40221. + if (scratch == (u32)-1)
  40222. + dead = 1;
  40223. +
  40224. + device_remove_file(&pdev->dev, &dev_attr_status);
  40225. + device_remove_file(&pdev->dev, &dev_attr_dma_wait);
  40226. + device_remove_file(&pdev->dev, &dev_attr_use_dma);
  40227. +
  40228. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  40229. + free_irq(host_priv->dma_irq, host);
  40230. + dma_free_writecombine(&pdev->dev, SZ_4K, host_priv->cb_base,
  40231. + host_priv->cb_handle);
  40232. +#endif
  40233. + sdhci_remove_host(host, dead);
  40234. + iounmap(host->ioaddr);
  40235. + release_mem_region(iomem->start, resource_size(iomem));
  40236. + sdhci_free_host(host);
  40237. + platform_set_drvdata(pdev, NULL);
  40238. +
  40239. + return 0;
  40240. +}
  40241. +
  40242. +static struct platform_driver sdhci_bcm2708_driver = {
  40243. + .driver = {
  40244. + .name = DRIVER_NAME,
  40245. + .owner = THIS_MODULE,
  40246. + },
  40247. + .probe = sdhci_bcm2708_probe,
  40248. + .remove = sdhci_bcm2708_remove,
  40249. +
  40250. +#ifdef CONFIG_PM
  40251. + .suspend = sdhci_bcm2708_suspend,
  40252. + .resume = sdhci_bcm2708_resume,
  40253. +#endif
  40254. +
  40255. +};
  40256. +
  40257. +/*****************************************************************************\
  40258. + * *
  40259. + * Driver init/exit *
  40260. + * *
  40261. +\*****************************************************************************/
  40262. +
  40263. +static int __init sdhci_drv_init(void)
  40264. +{
  40265. + return platform_driver_register(&sdhci_bcm2708_driver);
  40266. +}
  40267. +
  40268. +static void __exit sdhci_drv_exit(void)
  40269. +{
  40270. + platform_driver_unregister(&sdhci_bcm2708_driver);
  40271. +}
  40272. +
  40273. +module_init(sdhci_drv_init);
  40274. +module_exit(sdhci_drv_exit);
  40275. +
  40276. +module_param(allow_highspeed, bool, 0444);
  40277. +module_param(emmc_clock_freq, int, 0444);
  40278. +module_param(sync_after_dma, bool, 0444);
  40279. +module_param(missing_status, bool, 0444);
  40280. +module_param(spurious_crc_acmd51, bool, 0444);
  40281. +module_param(enable_llm, bool, 0444);
  40282. +module_param(cycle_delay, int, 0444);
  40283. +module_param(extra_messages, bool, 0444);
  40284. +
  40285. +MODULE_DESCRIPTION("Secure Digital Host Controller Interface platform driver");
  40286. +MODULE_AUTHOR("Broadcom <info@broadcom.com>");
  40287. +MODULE_LICENSE("GPL v2");
  40288. +MODULE_ALIAS("platform:"DRIVER_NAME);
  40289. +
  40290. +MODULE_PARM_DESC(allow_highspeed, "Allow high speed transfers modes");
  40291. +MODULE_PARM_DESC(emmc_clock_freq, "Specify the speed of emmc clock");
  40292. +MODULE_PARM_DESC(sync_after_dma, "Block in driver until dma complete");
  40293. +MODULE_PARM_DESC(missing_status, "Use the missing status quirk");
  40294. +MODULE_PARM_DESC(spurious_crc_acmd51, "Use the spurious crc quirk for reading SCR (ACMD51)");
  40295. +MODULE_PARM_DESC(enable_llm, "Enable low-latency mode");
  40296. +MODULE_PARM_DESC(extra_messages, "Enable more sdcard warning messages");
  40297. diff -Nur linux-3.12.33/drivers/mmc/host/sdhci.c linux-3.12.33-rpi/drivers/mmc/host/sdhci.c
  40298. --- linux-3.12.33/drivers/mmc/host/sdhci.c 2014-11-15 06:28:07.000000000 -0600
  40299. +++ linux-3.12.33-rpi/drivers/mmc/host/sdhci.c 2014-12-03 19:13:38.244418001 -0600
  40300. @@ -28,6 +28,7 @@
  40301. #include <linux/mmc/mmc.h>
  40302. #include <linux/mmc/host.h>
  40303. #include <linux/mmc/card.h>
  40304. +#include <linux/mmc/sd.h>
  40305. #include <linux/mmc/slot-gpio.h>
  40306. #include "sdhci.h"
  40307. @@ -131,6 +132,99 @@
  40308. * Low level functions *
  40309. * *
  40310. \*****************************************************************************/
  40311. +extern bool enable_llm;
  40312. +static int sdhci_locked=0;
  40313. +void sdhci_spin_lock(struct sdhci_host *host)
  40314. +{
  40315. + spin_lock(&host->lock);
  40316. +#ifdef CONFIG_PREEMPT
  40317. + if(enable_llm)
  40318. + {
  40319. + disable_irq_nosync(host->irq);
  40320. + if(host->second_irq)
  40321. + disable_irq_nosync(host->second_irq);
  40322. + local_irq_enable();
  40323. + }
  40324. +#endif
  40325. +}
  40326. +
  40327. +void sdhci_spin_unlock(struct sdhci_host *host)
  40328. +{
  40329. +#ifdef CONFIG_PREEMPT
  40330. + if(enable_llm)
  40331. + {
  40332. + local_irq_disable();
  40333. + if(host->second_irq)
  40334. + enable_irq(host->second_irq);
  40335. + enable_irq(host->irq);
  40336. + }
  40337. +#endif
  40338. + spin_unlock(&host->lock);
  40339. +}
  40340. +
  40341. +void sdhci_spin_lock_irqsave(struct sdhci_host *host,unsigned long *flags)
  40342. +{
  40343. +#ifdef CONFIG_PREEMPT
  40344. + if(enable_llm)
  40345. + {
  40346. + while(sdhci_locked)
  40347. + {
  40348. + preempt_schedule();
  40349. + }
  40350. + spin_lock_irqsave(&host->lock,*flags);
  40351. + disable_irq(host->irq);
  40352. + if(host->second_irq)
  40353. + disable_irq(host->second_irq);
  40354. + local_irq_enable();
  40355. + }
  40356. + else
  40357. +#endif
  40358. + spin_lock_irqsave(&host->lock,*flags);
  40359. +}
  40360. +
  40361. +void sdhci_spin_unlock_irqrestore(struct sdhci_host *host,unsigned long flags)
  40362. +{
  40363. +#ifdef CONFIG_PREEMPT
  40364. + if(enable_llm)
  40365. + {
  40366. + local_irq_disable();
  40367. + if(host->second_irq)
  40368. + enable_irq(host->second_irq);
  40369. + enable_irq(host->irq);
  40370. + }
  40371. +#endif
  40372. + spin_unlock_irqrestore(&host->lock,flags);
  40373. +}
  40374. +
  40375. +static void sdhci_spin_enable_schedule(struct sdhci_host *host)
  40376. +{
  40377. +#ifdef CONFIG_PREEMPT
  40378. + if(enable_llm)
  40379. + {
  40380. + sdhci_locked = 1;
  40381. + preempt_enable();
  40382. + }
  40383. +#endif
  40384. +}
  40385. +
  40386. +static void sdhci_spin_disable_schedule(struct sdhci_host *host)
  40387. +{
  40388. +#ifdef CONFIG_PREEMPT
  40389. + if(enable_llm)
  40390. + {
  40391. + preempt_disable();
  40392. + sdhci_locked = 0;
  40393. + }
  40394. +#endif
  40395. +}
  40396. +
  40397. +
  40398. +#undef spin_lock_irqsave
  40399. +#define spin_lock_irqsave(host_lock, flags) sdhci_spin_lock_irqsave(container_of(host_lock, struct sdhci_host, lock), &flags)
  40400. +#define spin_unlock_irqrestore(host_lock, flags) sdhci_spin_unlock_irqrestore(container_of(host_lock, struct sdhci_host, lock), flags)
  40401. +
  40402. +#define spin_lock(host_lock) sdhci_spin_lock(container_of(host_lock, struct sdhci_host, lock))
  40403. +#define spin_unlock(host_lock) sdhci_spin_unlock(container_of(host_lock, struct sdhci_host, lock))
  40404. static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
  40405. {
  40406. @@ -300,7 +394,7 @@
  40407. struct sdhci_host *host = container_of(led, struct sdhci_host, led);
  40408. unsigned long flags;
  40409. - spin_lock_irqsave(&host->lock, flags);
  40410. + sdhci_spin_lock_irqsave(host, &flags);
  40411. if (host->runtime_suspended)
  40412. goto out;
  40413. @@ -310,7 +404,7 @@
  40414. else
  40415. sdhci_activate_led(host);
  40416. out:
  40417. - spin_unlock_irqrestore(&host->lock, flags);
  40418. + sdhci_spin_unlock_irqrestore(host, flags);
  40419. }
  40420. #endif
  40421. @@ -327,7 +421,7 @@
  40422. u32 uninitialized_var(scratch);
  40423. u8 *buf;
  40424. - DBG("PIO reading\n");
  40425. + DBG("PIO reading %db\n", host->data->blksz);
  40426. blksize = host->data->blksz;
  40427. chunk = 0;
  40428. @@ -372,7 +466,7 @@
  40429. u32 scratch;
  40430. u8 *buf;
  40431. - DBG("PIO writing\n");
  40432. + DBG("PIO writing %db\n", host->data->blksz);
  40433. blksize = host->data->blksz;
  40434. chunk = 0;
  40435. @@ -411,19 +505,28 @@
  40436. local_irq_restore(flags);
  40437. }
  40438. -static void sdhci_transfer_pio(struct sdhci_host *host)
  40439. +static void sdhci_transfer_pio(struct sdhci_host *host, u32 intstate)
  40440. {
  40441. u32 mask;
  40442. + u32 state = 0;
  40443. + u32 intmask;
  40444. + int available;
  40445. BUG_ON(!host->data);
  40446. if (host->blocks == 0)
  40447. return;
  40448. - if (host->data->flags & MMC_DATA_READ)
  40449. + if (host->data->flags & MMC_DATA_READ) {
  40450. mask = SDHCI_DATA_AVAILABLE;
  40451. - else
  40452. + intmask = SDHCI_INT_DATA_AVAIL;
  40453. + } else {
  40454. mask = SDHCI_SPACE_AVAILABLE;
  40455. + intmask = SDHCI_INT_SPACE_AVAIL;
  40456. + }
  40457. +
  40458. + /* initially we can see whether we can procede using intstate */
  40459. + available = (intstate & intmask);
  40460. /*
  40461. * Some controllers (JMicron JMB38x) mess up the buffer bits
  40462. @@ -434,7 +537,7 @@
  40463. (host->data->blocks == 1))
  40464. mask = ~0;
  40465. - while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  40466. + while (available) {
  40467. if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
  40468. udelay(100);
  40469. @@ -446,9 +549,12 @@
  40470. host->blocks--;
  40471. if (host->blocks == 0)
  40472. break;
  40473. + state = sdhci_readl(host, SDHCI_PRESENT_STATE);
  40474. + available = state & mask;
  40475. + break;
  40476. }
  40477. - DBG("PIO transfer complete.\n");
  40478. + DBG("PIO transfer complete - %d blocks left.\n", host->blocks);
  40479. }
  40480. static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
  40481. @@ -721,7 +827,9 @@
  40482. u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
  40483. u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
  40484. - if (host->flags & SDHCI_REQ_USE_DMA)
  40485. + /* platform DMA will begin on receipt of PIO irqs */
  40486. + if ((host->flags & SDHCI_REQ_USE_DMA) &&
  40487. + !(host->flags & SDHCI_USE_PLATDMA))
  40488. sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
  40489. else
  40490. sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
  40491. @@ -753,44 +861,25 @@
  40492. host->data_early = 0;
  40493. host->data->bytes_xfered = 0;
  40494. - if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
  40495. + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA | SDHCI_USE_PLATDMA))
  40496. host->flags |= SDHCI_REQ_USE_DMA;
  40497. /*
  40498. * FIXME: This doesn't account for merging when mapping the
  40499. * scatterlist.
  40500. */
  40501. - if (host->flags & SDHCI_REQ_USE_DMA) {
  40502. - int broken, i;
  40503. - struct scatterlist *sg;
  40504. -
  40505. - broken = 0;
  40506. - if (host->flags & SDHCI_USE_ADMA) {
  40507. - if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
  40508. - broken = 1;
  40509. - } else {
  40510. - if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
  40511. - broken = 1;
  40512. - }
  40513. -
  40514. - if (unlikely(broken)) {
  40515. - for_each_sg(data->sg, sg, data->sg_len, i) {
  40516. - if (sg->length & 0x3) {
  40517. - DBG("Reverting to PIO because of "
  40518. - "transfer size (%d)\n",
  40519. - sg->length);
  40520. - host->flags &= ~SDHCI_REQ_USE_DMA;
  40521. - break;
  40522. - }
  40523. - }
  40524. - }
  40525. - }
  40526. /*
  40527. * The assumption here being that alignment is the same after
  40528. * translation to device address space.
  40529. */
  40530. - if (host->flags & SDHCI_REQ_USE_DMA) {
  40531. + if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_PLATDMA)) ==
  40532. + (SDHCI_REQ_USE_DMA | SDHCI_USE_PLATDMA)) {
  40533. +
  40534. + if (! sdhci_platdma_dmaable(host, data))
  40535. + host->flags &= ~SDHCI_REQ_USE_DMA;
  40536. +
  40537. + } else if (host->flags & SDHCI_REQ_USE_DMA) {
  40538. int broken, i;
  40539. struct scatterlist *sg;
  40540. @@ -849,7 +938,8 @@
  40541. */
  40542. WARN_ON(1);
  40543. host->flags &= ~SDHCI_REQ_USE_DMA;
  40544. - } else {
  40545. + } else
  40546. + if (!(host->flags & SDHCI_USE_PLATDMA)) {
  40547. WARN_ON(sg_cnt != 1);
  40548. sdhci_writel(host, sg_dma_address(data->sg),
  40549. SDHCI_DMA_ADDRESS);
  40550. @@ -865,11 +955,13 @@
  40551. if (host->version >= SDHCI_SPEC_200) {
  40552. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  40553. ctrl &= ~SDHCI_CTRL_DMA_MASK;
  40554. + if (! (host->flags & SDHCI_USE_PLATDMA)) {
  40555. if ((host->flags & SDHCI_REQ_USE_DMA) &&
  40556. (host->flags & SDHCI_USE_ADMA))
  40557. ctrl |= SDHCI_CTRL_ADMA32;
  40558. else
  40559. ctrl |= SDHCI_CTRL_SDMA;
  40560. + }
  40561. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  40562. }
  40563. @@ -921,7 +1013,8 @@
  40564. if (data->flags & MMC_DATA_READ)
  40565. mode |= SDHCI_TRNS_READ;
  40566. - if (host->flags & SDHCI_REQ_USE_DMA)
  40567. + if ((host->flags & SDHCI_REQ_USE_DMA) &&
  40568. + !(host->flags & SDHCI_USE_PLATDMA))
  40569. mode |= SDHCI_TRNS_DMA;
  40570. sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
  40571. @@ -937,13 +1030,16 @@
  40572. host->data = NULL;
  40573. if (host->flags & SDHCI_REQ_USE_DMA) {
  40574. - if (host->flags & SDHCI_USE_ADMA)
  40575. - sdhci_adma_table_post(host, data);
  40576. - else {
  40577. + /* we may have to abandon an ongoing platform DMA */
  40578. + if (host->flags & SDHCI_USE_PLATDMA)
  40579. + sdhci_platdma_reset(host, data);
  40580. +
  40581. + if (host->flags & (SDHCI_USE_PLATDMA | SDHCI_USE_SDMA)) {
  40582. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  40583. data->sg_len, (data->flags & MMC_DATA_READ) ?
  40584. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  40585. - }
  40586. + } else if (host->flags & SDHCI_USE_ADMA)
  40587. + sdhci_adma_table_post(host, data);
  40588. }
  40589. /*
  40590. @@ -996,6 +1092,12 @@
  40591. if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
  40592. mask |= SDHCI_DATA_INHIBIT;
  40593. + if(host->ops->missing_status && (cmd->opcode == MMC_SEND_STATUS)) {
  40594. + timeout = 5000; // Really obscenely large delay to send the status, due to bug in controller
  40595. + // which might cause the STATUS command to get stuck when a data operation is in flow
  40596. + mask |= SDHCI_DATA_INHIBIT;
  40597. + }
  40598. +
  40599. /* We shouldn't wait for data inihibit for stop commands, even
  40600. though they might use busy signaling */
  40601. if (host->mrq->data && (cmd == host->mrq->data->stop))
  40602. @@ -1011,12 +1113,20 @@
  40603. return;
  40604. }
  40605. timeout--;
  40606. + sdhci_spin_enable_schedule(host);
  40607. mdelay(1);
  40608. + sdhci_spin_disable_schedule(host);
  40609. }
  40610. + DBG("send cmd %d - wait 0x%X irq 0x%x\n", cmd->opcode, mask,
  40611. + sdhci_readl(host, SDHCI_INT_STATUS));
  40612. mod_timer(&host->timer, jiffies + 10 * HZ);
  40613. host->cmd = cmd;
  40614. + if (host->last_cmdop == MMC_APP_CMD)
  40615. + host->last_cmdop = -cmd->opcode;
  40616. + else
  40617. + host->last_cmdop = cmd->opcode;
  40618. sdhci_prepare_data(host, cmd);
  40619. @@ -1232,7 +1342,9 @@
  40620. return;
  40621. }
  40622. timeout--;
  40623. + sdhci_spin_enable_schedule(host);
  40624. mdelay(1);
  40625. + sdhci_spin_disable_schedule(host);
  40626. }
  40627. clk |= SDHCI_CLOCK_CARD_EN;
  40628. @@ -1333,7 +1445,7 @@
  40629. sdhci_runtime_pm_get(host);
  40630. - spin_lock_irqsave(&host->lock, flags);
  40631. + sdhci_spin_lock_irqsave(host, &flags);
  40632. WARN_ON(host->mrq != NULL);
  40633. @@ -1391,9 +1503,9 @@
  40634. mmc->card->type == MMC_TYPE_MMC ?
  40635. MMC_SEND_TUNING_BLOCK_HS200 :
  40636. MMC_SEND_TUNING_BLOCK;
  40637. - spin_unlock_irqrestore(&host->lock, flags);
  40638. + sdhci_spin_unlock_irqrestore(host, flags);
  40639. sdhci_execute_tuning(mmc, tuning_opcode);
  40640. - spin_lock_irqsave(&host->lock, flags);
  40641. + sdhci_spin_lock_irqsave(host, &flags);
  40642. /* Restore original mmc_request structure */
  40643. host->mrq = mrq;
  40644. @@ -1407,7 +1519,7 @@
  40645. }
  40646. mmiowb();
  40647. - spin_unlock_irqrestore(&host->lock, flags);
  40648. + sdhci_spin_unlock_irqrestore(host, flags);
  40649. }
  40650. static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
  40651. @@ -1416,10 +1528,10 @@
  40652. int vdd_bit = -1;
  40653. u8 ctrl;
  40654. - spin_lock_irqsave(&host->lock, flags);
  40655. + sdhci_spin_lock_irqsave(host, &flags);
  40656. if (host->flags & SDHCI_DEVICE_DEAD) {
  40657. - spin_unlock_irqrestore(&host->lock, flags);
  40658. + sdhci_spin_unlock_irqrestore(host, flags);
  40659. if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
  40660. mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
  40661. return;
  40662. @@ -1446,9 +1558,9 @@
  40663. vdd_bit = sdhci_set_power(host, ios->vdd);
  40664. if (host->vmmc && vdd_bit != -1) {
  40665. - spin_unlock_irqrestore(&host->lock, flags);
  40666. + sdhci_spin_unlock_irqrestore(host, flags);
  40667. mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
  40668. - spin_lock_irqsave(&host->lock, flags);
  40669. + sdhci_spin_lock_irqsave(host, &flags);
  40670. }
  40671. if (host->ops->platform_send_init_74_clocks)
  40672. @@ -1585,7 +1697,7 @@
  40673. sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  40674. mmiowb();
  40675. - spin_unlock_irqrestore(&host->lock, flags);
  40676. + sdhci_spin_unlock_irqrestore(host, flags);
  40677. }
  40678. static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  40679. @@ -1633,7 +1745,7 @@
  40680. unsigned long flags;
  40681. int is_readonly;
  40682. - spin_lock_irqsave(&host->lock, flags);
  40683. + sdhci_spin_lock_irqsave(host, &flags);
  40684. if (host->flags & SDHCI_DEVICE_DEAD)
  40685. is_readonly = 0;
  40686. @@ -1643,7 +1755,7 @@
  40687. is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
  40688. & SDHCI_WRITE_PROTECT);
  40689. - spin_unlock_irqrestore(&host->lock, flags);
  40690. + sdhci_spin_unlock_irqrestore(host, flags);
  40691. /* This quirk needs to be replaced by a callback-function later */
  40692. return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
  40693. @@ -1716,9 +1828,9 @@
  40694. struct sdhci_host *host = mmc_priv(mmc);
  40695. unsigned long flags;
  40696. - spin_lock_irqsave(&host->lock, flags);
  40697. + sdhci_spin_lock_irqsave(host, &flags);
  40698. sdhci_enable_sdio_irq_nolock(host, enable);
  40699. - spin_unlock_irqrestore(&host->lock, flags);
  40700. + sdhci_spin_unlock_irqrestore(host, flags);
  40701. }
  40702. static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
  40703. @@ -2066,7 +2178,7 @@
  40704. if (host->ops->card_event)
  40705. host->ops->card_event(host);
  40706. - spin_lock_irqsave(&host->lock, flags);
  40707. + sdhci_spin_lock_irqsave(host, &flags);
  40708. /* Check host->mrq first in case we are runtime suspended */
  40709. if (host->mrq && !sdhci_do_get_cd(host)) {
  40710. @@ -2082,7 +2194,7 @@
  40711. tasklet_schedule(&host->finish_tasklet);
  40712. }
  40713. - spin_unlock_irqrestore(&host->lock, flags);
  40714. + sdhci_spin_unlock_irqrestore(host, flags);
  40715. }
  40716. static const struct mmc_host_ops sdhci_ops = {
  40717. @@ -2121,14 +2233,14 @@
  40718. host = (struct sdhci_host*)param;
  40719. - spin_lock_irqsave(&host->lock, flags);
  40720. + sdhci_spin_lock_irqsave(host, &flags);
  40721. /*
  40722. * If this tasklet gets rescheduled while running, it will
  40723. * be run again afterwards but without any active request.
  40724. */
  40725. if (!host->mrq) {
  40726. - spin_unlock_irqrestore(&host->lock, flags);
  40727. + sdhci_spin_unlock_irqrestore(host, flags);
  40728. return;
  40729. }
  40730. @@ -2166,7 +2278,7 @@
  40731. #endif
  40732. mmiowb();
  40733. - spin_unlock_irqrestore(&host->lock, flags);
  40734. + sdhci_spin_unlock_irqrestore(host, flags);
  40735. mmc_request_done(host->mmc, mrq);
  40736. sdhci_runtime_pm_put(host);
  40737. @@ -2179,11 +2291,11 @@
  40738. host = (struct sdhci_host*)data;
  40739. - spin_lock_irqsave(&host->lock, flags);
  40740. + sdhci_spin_lock_irqsave(host, &flags);
  40741. if (host->mrq) {
  40742. pr_err("%s: Timeout waiting for hardware "
  40743. - "interrupt.\n", mmc_hostname(host->mmc));
  40744. + "interrupt - cmd%d.\n", mmc_hostname(host->mmc), host->last_cmdop);
  40745. sdhci_dumpregs(host);
  40746. if (host->data) {
  40747. @@ -2200,7 +2312,7 @@
  40748. }
  40749. mmiowb();
  40750. - spin_unlock_irqrestore(&host->lock, flags);
  40751. + sdhci_spin_unlock_irqrestore(host, flags);
  40752. }
  40753. static void sdhci_tuning_timer(unsigned long data)
  40754. @@ -2210,11 +2322,11 @@
  40755. host = (struct sdhci_host *)data;
  40756. - spin_lock_irqsave(&host->lock, flags);
  40757. + sdhci_spin_lock_irqsave(host, &flags);
  40758. host->flags |= SDHCI_NEEDS_RETUNING;
  40759. - spin_unlock_irqrestore(&host->lock, flags);
  40760. + sdhci_spin_unlock_irqrestore(host, flags);
  40761. }
  40762. /*****************************************************************************\
  40763. @@ -2228,10 +2340,13 @@
  40764. BUG_ON(intmask == 0);
  40765. if (!host->cmd) {
  40766. + if (!(host->ops->extra_ints)) {
  40767. pr_err("%s: Got command interrupt 0x%08x even "
  40768. "though no command operation was in progress.\n",
  40769. mmc_hostname(host->mmc), (unsigned)intmask);
  40770. sdhci_dumpregs(host);
  40771. + } else
  40772. + DBG("cmd irq 0x%08x cmd complete\n", (unsigned)intmask);
  40773. return;
  40774. }
  40775. @@ -2301,6 +2416,19 @@
  40776. static void sdhci_show_adma_error(struct sdhci_host *host) { }
  40777. #endif
  40778. +static void sdhci_data_end(struct sdhci_host *host)
  40779. +{
  40780. + if (host->cmd) {
  40781. + /*
  40782. + * Data managed to finish before the
  40783. + * command completed. Make sure we do
  40784. + * things in the proper order.
  40785. + */
  40786. + host->data_early = 1;
  40787. + } else
  40788. + sdhci_finish_data(host);
  40789. +}
  40790. +
  40791. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  40792. {
  40793. u32 command;
  40794. @@ -2330,23 +2458,39 @@
  40795. }
  40796. }
  40797. + if (!(host->ops->extra_ints)) {
  40798. pr_err("%s: Got data interrupt 0x%08x even "
  40799. "though no data operation was in progress.\n",
  40800. mmc_hostname(host->mmc), (unsigned)intmask);
  40801. sdhci_dumpregs(host);
  40802. + } else
  40803. + DBG("data irq 0x%08x but no data\n", (unsigned)intmask);
  40804. return;
  40805. }
  40806. if (intmask & SDHCI_INT_DATA_TIMEOUT)
  40807. host->data->error = -ETIMEDOUT;
  40808. - else if (intmask & SDHCI_INT_DATA_END_BIT)
  40809. + else if (intmask & SDHCI_INT_DATA_END_BIT) {
  40810. + DBG("end error in cmd %d\n", host->last_cmdop);
  40811. + if (host->ops->spurious_crc_acmd51 &&
  40812. + host->last_cmdop == -SD_APP_SEND_SCR) {
  40813. + DBG("ignoring spurious data_end_bit error\n");
  40814. + intmask = SDHCI_INT_DATA_AVAIL|SDHCI_INT_DATA_END;
  40815. + } else
  40816. host->data->error = -EILSEQ;
  40817. - else if ((intmask & SDHCI_INT_DATA_CRC) &&
  40818. + } else if ((intmask & SDHCI_INT_DATA_CRC) &&
  40819. SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
  40820. - != MMC_BUS_TEST_R)
  40821. + != MMC_BUS_TEST_R) {
  40822. + DBG("crc error in cmd %d\n", host->last_cmdop);
  40823. + if (host->ops->spurious_crc_acmd51 &&
  40824. + host->last_cmdop == -SD_APP_SEND_SCR) {
  40825. + DBG("ignoring spurious data_crc_bit error\n");
  40826. + intmask = SDHCI_INT_DATA_AVAIL|SDHCI_INT_DATA_END;
  40827. + } else {
  40828. host->data->error = -EILSEQ;
  40829. - else if (intmask & SDHCI_INT_ADMA_ERROR) {
  40830. + }
  40831. + } else if (intmask & SDHCI_INT_ADMA_ERROR) {
  40832. pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
  40833. sdhci_show_adma_error(host);
  40834. host->data->error = -EIO;
  40835. @@ -2354,11 +2498,18 @@
  40836. host->ops->adma_workaround(host, intmask);
  40837. }
  40838. - if (host->data->error)
  40839. + if (host->data->error) {
  40840. + DBG("finish request early on error %d\n", host->data->error);
  40841. sdhci_finish_data(host);
  40842. - else {
  40843. - if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  40844. - sdhci_transfer_pio(host);
  40845. + } else {
  40846. + if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
  40847. + if (host->flags & SDHCI_REQ_USE_DMA) {
  40848. + /* possible only in PLATDMA mode */
  40849. + sdhci_platdma_avail(host, &intmask,
  40850. + &sdhci_data_end);
  40851. + } else
  40852. + sdhci_transfer_pio(host, intmask);
  40853. + }
  40854. /*
  40855. * We currently don't do anything fancy with DMA
  40856. @@ -2387,18 +2538,8 @@
  40857. sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
  40858. }
  40859. - if (intmask & SDHCI_INT_DATA_END) {
  40860. - if (host->cmd) {
  40861. - /*
  40862. - * Data managed to finish before the
  40863. - * command completed. Make sure we do
  40864. - * things in the proper order.
  40865. - */
  40866. - host->data_early = 1;
  40867. - } else {
  40868. - sdhci_finish_data(host);
  40869. - }
  40870. - }
  40871. + if (intmask & SDHCI_INT_DATA_END)
  40872. + sdhci_data_end(host);
  40873. }
  40874. }
  40875. @@ -2409,10 +2550,10 @@
  40876. u32 intmask, unexpected = 0;
  40877. int cardint = 0, max_loops = 16;
  40878. - spin_lock(&host->lock);
  40879. + sdhci_spin_lock(host);
  40880. if (host->runtime_suspended) {
  40881. - spin_unlock(&host->lock);
  40882. + sdhci_spin_unlock(host);
  40883. pr_warning("%s: got irq while runtime suspended\n",
  40884. mmc_hostname(host->mmc));
  40885. return IRQ_HANDLED;
  40886. @@ -2454,6 +2595,22 @@
  40887. tasklet_schedule(&host->card_tasklet);
  40888. }
  40889. + if (intmask & SDHCI_INT_ERROR_MASK & ~SDHCI_INT_ERROR)
  40890. + DBG("controller reports error 0x%x -"
  40891. + "%s%s%s%s%s%s%s%s%s%s",
  40892. + intmask,
  40893. + intmask & SDHCI_INT_TIMEOUT? " timeout": "",
  40894. + intmask & SDHCI_INT_CRC ? " crc": "",
  40895. + intmask & SDHCI_INT_END_BIT? " endbit": "",
  40896. + intmask & SDHCI_INT_INDEX? " index": "",
  40897. + intmask & SDHCI_INT_DATA_TIMEOUT? " data_timeout": "",
  40898. + intmask & SDHCI_INT_DATA_CRC? " data_crc": "",
  40899. + intmask & SDHCI_INT_DATA_END_BIT? " data_endbit": "",
  40900. + intmask & SDHCI_INT_BUS_POWER? " buspower": "",
  40901. + intmask & SDHCI_INT_ACMD12ERR? " acmd12": "",
  40902. + intmask & SDHCI_INT_ADMA_ERROR? " adma": ""
  40903. + );
  40904. +
  40905. if (intmask & SDHCI_INT_CMD_MASK) {
  40906. sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
  40907. SDHCI_INT_STATUS);
  40908. @@ -2468,7 +2625,13 @@
  40909. intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
  40910. - intmask &= ~SDHCI_INT_ERROR;
  40911. + if (intmask & SDHCI_INT_ERROR_MASK) {
  40912. + /* collect any uncovered errors */
  40913. + sdhci_writel(host, intmask & SDHCI_INT_ERROR_MASK,
  40914. + SDHCI_INT_STATUS);
  40915. + }
  40916. +
  40917. + intmask &= ~SDHCI_INT_ERROR_MASK;
  40918. if (intmask & SDHCI_INT_BUS_POWER) {
  40919. pr_err("%s: Card is consuming too much power!\n",
  40920. @@ -2494,7 +2657,7 @@
  40921. if (intmask && --max_loops)
  40922. goto again;
  40923. out:
  40924. - spin_unlock(&host->lock);
  40925. + sdhci_spin_unlock(host);
  40926. if (unexpected) {
  40927. pr_err("%s: Unexpected interrupt 0x%08x.\n",
  40928. @@ -2588,13 +2751,14 @@
  40929. {
  40930. int ret;
  40931. - if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  40932. + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA |
  40933. + SDHCI_USE_PLATDMA)) {
  40934. if (host->ops->enable_dma)
  40935. host->ops->enable_dma(host);
  40936. }
  40937. if (!device_may_wakeup(mmc_dev(host->mmc))) {
  40938. - ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  40939. + ret = request_irq(host->irq, sdhci_irq, 0 /*IRQF_SHARED*/,
  40940. mmc_hostname(host->mmc), host);
  40941. if (ret)
  40942. return ret;
  40943. @@ -2671,15 +2835,15 @@
  40944. host->flags &= ~SDHCI_NEEDS_RETUNING;
  40945. }
  40946. - spin_lock_irqsave(&host->lock, flags);
  40947. + sdhci_spin_lock_irqsave(host, &flags);
  40948. sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
  40949. - spin_unlock_irqrestore(&host->lock, flags);
  40950. + sdhci_spin_unlock_irqrestore(host, flags);
  40951. synchronize_irq(host->irq);
  40952. - spin_lock_irqsave(&host->lock, flags);
  40953. + sdhci_spin_lock_irqsave(host, &flags);
  40954. host->runtime_suspended = true;
  40955. - spin_unlock_irqrestore(&host->lock, flags);
  40956. + sdhci_spin_unlock_irqrestore(host, flags);
  40957. return ret;
  40958. }
  40959. @@ -2705,16 +2869,16 @@
  40960. sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
  40961. if ((host_flags & SDHCI_PV_ENABLED) &&
  40962. !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
  40963. - spin_lock_irqsave(&host->lock, flags);
  40964. + sdhci_spin_lock_irqsave(host, &flags);
  40965. sdhci_enable_preset_value(host, true);
  40966. - spin_unlock_irqrestore(&host->lock, flags);
  40967. + sdhci_spin_unlock_irqrestore(host, flags);
  40968. }
  40969. /* Set the re-tuning expiration flag */
  40970. if (host->flags & SDHCI_USING_RETUNING_TIMER)
  40971. host->flags |= SDHCI_NEEDS_RETUNING;
  40972. - spin_lock_irqsave(&host->lock, flags);
  40973. + sdhci_spin_lock_irqsave(host, &flags);
  40974. host->runtime_suspended = false;
  40975. @@ -2725,7 +2889,7 @@
  40976. /* Enable Card Detection */
  40977. sdhci_enable_card_detection(host);
  40978. - spin_unlock_irqrestore(&host->lock, flags);
  40979. + sdhci_spin_unlock_irqrestore(host, flags);
  40980. return ret;
  40981. }
  40982. @@ -2820,14 +2984,16 @@
  40983. host->flags &= ~SDHCI_USE_ADMA;
  40984. }
  40985. - if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  40986. + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA |
  40987. + SDHCI_USE_PLATDMA)) {
  40988. if (host->ops->enable_dma) {
  40989. if (host->ops->enable_dma(host)) {
  40990. pr_warning("%s: No suitable DMA "
  40991. "available. Falling back to PIO.\n",
  40992. mmc_hostname(mmc));
  40993. host->flags &=
  40994. - ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
  40995. + ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA |
  40996. + SDHCI_USE_PLATDMA);
  40997. }
  40998. }
  40999. }
  41000. @@ -3218,8 +3384,8 @@
  41001. sdhci_init(host, 0);
  41002. - ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  41003. - mmc_hostname(mmc), host);
  41004. + ret = request_irq(host->irq, sdhci_irq, 0 /*IRQF_SHARED*/,
  41005. + mmc_hostname(mmc), host);
  41006. if (ret) {
  41007. pr_err("%s: Failed to request IRQ %d: %d\n",
  41008. mmc_hostname(mmc), host->irq, ret);
  41009. @@ -3252,6 +3418,7 @@
  41010. pr_info("%s: SDHCI controller on %s [%s] using %s\n",
  41011. mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
  41012. + (host->flags & SDHCI_USE_PLATDMA) ? "platform's DMA" :
  41013. (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
  41014. (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
  41015. @@ -3279,7 +3446,7 @@
  41016. unsigned long flags;
  41017. if (dead) {
  41018. - spin_lock_irqsave(&host->lock, flags);
  41019. + sdhci_spin_lock_irqsave(host, &flags);
  41020. host->flags |= SDHCI_DEVICE_DEAD;
  41021. @@ -3291,7 +3458,7 @@
  41022. tasklet_schedule(&host->finish_tasklet);
  41023. }
  41024. - spin_unlock_irqrestore(&host->lock, flags);
  41025. + sdhci_spin_unlock_irqrestore(host, flags);
  41026. }
  41027. sdhci_disable_card_detection(host);
  41028. diff -Nur linux-3.12.33/drivers/mmc/host/sdhci.h linux-3.12.33-rpi/drivers/mmc/host/sdhci.h
  41029. --- linux-3.12.33/drivers/mmc/host/sdhci.h 2014-11-15 06:28:07.000000000 -0600
  41030. +++ linux-3.12.33-rpi/drivers/mmc/host/sdhci.h 2014-12-03 19:13:38.244418001 -0600
  41031. @@ -289,6 +289,18 @@
  41032. void (*platform_reset_enter)(struct sdhci_host *host, u8 mask);
  41033. void (*platform_reset_exit)(struct sdhci_host *host, u8 mask);
  41034. int (*set_uhs_signaling)(struct sdhci_host *host, unsigned int uhs);
  41035. +
  41036. + int (*pdma_able)(struct sdhci_host *host,
  41037. + struct mmc_data *data);
  41038. + void (*pdma_avail)(struct sdhci_host *host,
  41039. + unsigned int *ref_intmask,
  41040. + void(*complete)(struct sdhci_host *));
  41041. + void (*pdma_reset)(struct sdhci_host *host,
  41042. + struct mmc_data *data);
  41043. + unsigned int (*extra_ints)(struct sdhci_host *host);
  41044. + unsigned int (*spurious_crc_acmd51)(struct sdhci_host *host);
  41045. + unsigned int (*missing_status)(struct sdhci_host *host);
  41046. +
  41047. void (*hw_reset)(struct sdhci_host *host);
  41048. void (*platform_suspend)(struct sdhci_host *host);
  41049. void (*platform_resume)(struct sdhci_host *host);
  41050. @@ -400,9 +412,38 @@
  41051. extern void sdhci_enable_irq_wakeups(struct sdhci_host *host);
  41052. #endif
  41053. +static inline int /*bool*/
  41054. +sdhci_platdma_dmaable(struct sdhci_host *host, struct mmc_data *data)
  41055. +{
  41056. + if (host->ops->pdma_able)
  41057. + return host->ops->pdma_able(host, data);
  41058. + else
  41059. + return 1;
  41060. +}
  41061. +static inline void
  41062. +sdhci_platdma_avail(struct sdhci_host *host, unsigned int *ref_intmask,
  41063. + void(*completion_callback)(struct sdhci_host *))
  41064. +{
  41065. + if (host->ops->pdma_avail)
  41066. + host->ops->pdma_avail(host, ref_intmask, completion_callback);
  41067. +}
  41068. +
  41069. +static inline void
  41070. +sdhci_platdma_reset(struct sdhci_host *host, struct mmc_data *data)
  41071. +{
  41072. + if (host->ops->pdma_reset)
  41073. + host->ops->pdma_reset(host, data);
  41074. +}
  41075. +
  41076. #ifdef CONFIG_PM_RUNTIME
  41077. extern int sdhci_runtime_suspend_host(struct sdhci_host *host);
  41078. extern int sdhci_runtime_resume_host(struct sdhci_host *host);
  41079. #endif
  41080. +extern void sdhci_spin_lock_irqsave(struct sdhci_host *host,unsigned long *flags);
  41081. +extern void sdhci_spin_unlock_irqrestore(struct sdhci_host *host,unsigned long flags);
  41082. +extern void sdhci_spin_lock(struct sdhci_host *host);
  41083. +extern void sdhci_spin_unlock(struct sdhci_host *host);
  41084. +
  41085. +
  41086. #endif /* __SDHCI_HW_H */
  41087. diff -Nur linux-3.12.33/drivers/net/usb/smsc95xx.c linux-3.12.33-rpi/drivers/net/usb/smsc95xx.c
  41088. --- linux-3.12.33/drivers/net/usb/smsc95xx.c 2014-11-15 06:28:07.000000000 -0600
  41089. +++ linux-3.12.33-rpi/drivers/net/usb/smsc95xx.c 2014-12-03 19:13:38.600418001 -0600
  41090. @@ -61,6 +61,7 @@
  41091. #define SUSPEND_SUSPEND3 (0x08)
  41092. #define SUSPEND_ALLMODES (SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
  41093. SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
  41094. +#define MAC_ADDR_LEN (6)
  41095. struct smsc95xx_priv {
  41096. u32 mac_cr;
  41097. @@ -76,6 +77,10 @@
  41098. module_param(turbo_mode, bool, 0644);
  41099. MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
  41100. +static char *macaddr = ":";
  41101. +module_param(macaddr, charp, 0);
  41102. +MODULE_PARM_DESC(macaddr, "MAC address");
  41103. +
  41104. static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
  41105. u32 *data, int in_pm)
  41106. {
  41107. @@ -765,8 +770,59 @@
  41108. return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
  41109. }
  41110. +/* Check the macaddr module parameter for a MAC address */
  41111. +static int smsc95xx_is_macaddr_param(struct usbnet *dev, u8 *dev_mac)
  41112. +{
  41113. + int i, j, got_num, num;
  41114. + u8 mtbl[MAC_ADDR_LEN];
  41115. +
  41116. + if (macaddr[0] == ':')
  41117. + return 0;
  41118. +
  41119. + i = 0;
  41120. + j = 0;
  41121. + num = 0;
  41122. + got_num = 0;
  41123. + while (j < MAC_ADDR_LEN) {
  41124. + if (macaddr[i] && macaddr[i] != ':') {
  41125. + got_num++;
  41126. + if ('0' <= macaddr[i] && macaddr[i] <= '9')
  41127. + num = num * 16 + macaddr[i] - '0';
  41128. + else if ('A' <= macaddr[i] && macaddr[i] <= 'F')
  41129. + num = num * 16 + 10 + macaddr[i] - 'A';
  41130. + else if ('a' <= macaddr[i] && macaddr[i] <= 'f')
  41131. + num = num * 16 + 10 + macaddr[i] - 'a';
  41132. + else
  41133. + break;
  41134. + i++;
  41135. + } else if (got_num == 2) {
  41136. + mtbl[j++] = (u8) num;
  41137. + num = 0;
  41138. + got_num = 0;
  41139. + i++;
  41140. + } else {
  41141. + break;
  41142. + }
  41143. + }
  41144. +
  41145. + if (j == MAC_ADDR_LEN) {
  41146. + netif_dbg(dev, ifup, dev->net, "Overriding MAC address with: "
  41147. + "%02x:%02x:%02x:%02x:%02x:%02x\n", mtbl[0], mtbl[1], mtbl[2],
  41148. + mtbl[3], mtbl[4], mtbl[5]);
  41149. + for (i = 0; i < MAC_ADDR_LEN; i++)
  41150. + dev_mac[i] = mtbl[i];
  41151. + return 1;
  41152. + } else {
  41153. + return 0;
  41154. + }
  41155. +}
  41156. +
  41157. static void smsc95xx_init_mac_address(struct usbnet *dev)
  41158. {
  41159. + /* Check module parameters */
  41160. + if (smsc95xx_is_macaddr_param(dev, dev->net->dev_addr))
  41161. + return;
  41162. +
  41163. /* try reading mac address from EEPROM */
  41164. if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
  41165. dev->net->dev_addr) == 0) {
  41166. diff -Nur linux-3.12.33/drivers/spi/Kconfig linux-3.12.33-rpi/drivers/spi/Kconfig
  41167. --- linux-3.12.33/drivers/spi/Kconfig 2014-11-15 06:28:07.000000000 -0600
  41168. +++ linux-3.12.33-rpi/drivers/spi/Kconfig 2014-12-03 19:13:39.436418001 -0600
  41169. @@ -85,6 +85,14 @@
  41170. is for the regular SPI controller. Slave mode operation is not also
  41171. not supported.
  41172. +config SPI_BCM2708
  41173. + tristate "BCM2708 SPI controller driver (SPI0)"
  41174. + depends on MACH_BCM2708
  41175. + help
  41176. + This selects a driver for the Broadcom BCM2708 SPI master (SPI0). This
  41177. + driver is not compatible with the "Universal SPI Master" or the SPI slave
  41178. + device.
  41179. +
  41180. config SPI_BFIN5XX
  41181. tristate "SPI controller driver for ADI Blackfin5xx"
  41182. depends on BLACKFIN && !BF60x
  41183. diff -Nur linux-3.12.33/drivers/spi/Makefile linux-3.12.33-rpi/drivers/spi/Makefile
  41184. --- linux-3.12.33/drivers/spi/Makefile 2014-11-15 06:28:07.000000000 -0600
  41185. +++ linux-3.12.33-rpi/drivers/spi/Makefile 2014-12-03 19:13:39.436418001 -0600
  41186. @@ -18,6 +18,7 @@
  41187. obj-$(CONFIG_SPI_BCM63XX) += spi-bcm63xx.o
  41188. obj-$(CONFIG_SPI_BFIN5XX) += spi-bfin5xx.o
  41189. obj-$(CONFIG_SPI_BFIN_V3) += spi-bfin-v3.o
  41190. +obj-$(CONFIG_SPI_BCM2708) += spi-bcm2708.o
  41191. obj-$(CONFIG_SPI_BFIN_SPORT) += spi-bfin-sport.o
  41192. obj-$(CONFIG_SPI_BITBANG) += spi-bitbang.o
  41193. obj-$(CONFIG_SPI_BUTTERFLY) += spi-butterfly.o
  41194. diff -Nur linux-3.12.33/drivers/spi/spi-bcm2708.c linux-3.12.33-rpi/drivers/spi/spi-bcm2708.c
  41195. --- linux-3.12.33/drivers/spi/spi-bcm2708.c 1969-12-31 18:00:00.000000000 -0600
  41196. +++ linux-3.12.33-rpi/drivers/spi/spi-bcm2708.c 2014-12-03 19:13:39.436418001 -0600
  41197. @@ -0,0 +1,626 @@
  41198. +/*
  41199. + * Driver for Broadcom BCM2708 SPI Controllers
  41200. + *
  41201. + * Copyright (C) 2012 Chris Boot
  41202. + *
  41203. + * This driver is inspired by:
  41204. + * spi-ath79.c, Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
  41205. + * spi-atmel.c, Copyright (C) 2006 Atmel Corporation
  41206. + *
  41207. + * This program is free software; you can redistribute it and/or modify
  41208. + * it under the terms of the GNU General Public License as published by
  41209. + * the Free Software Foundation; either version 2 of the License, or
  41210. + * (at your option) any later version.
  41211. + *
  41212. + * This program is distributed in the hope that it will be useful,
  41213. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  41214. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41215. + * GNU General Public License for more details.
  41216. + *
  41217. + * You should have received a copy of the GNU General Public License
  41218. + * along with this program; if not, write to the Free Software
  41219. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  41220. + */
  41221. +
  41222. +#include <linux/kernel.h>
  41223. +#include <linux/module.h>
  41224. +#include <linux/spinlock.h>
  41225. +#include <linux/clk.h>
  41226. +#include <linux/err.h>
  41227. +#include <linux/platform_device.h>
  41228. +#include <linux/io.h>
  41229. +#include <linux/spi/spi.h>
  41230. +#include <linux/interrupt.h>
  41231. +#include <linux/delay.h>
  41232. +#include <linux/log2.h>
  41233. +#include <linux/sched.h>
  41234. +#include <linux/wait.h>
  41235. +
  41236. +/* SPI register offsets */
  41237. +#define SPI_CS 0x00
  41238. +#define SPI_FIFO 0x04
  41239. +#define SPI_CLK 0x08
  41240. +#define SPI_DLEN 0x0c
  41241. +#define SPI_LTOH 0x10
  41242. +#define SPI_DC 0x14
  41243. +
  41244. +/* Bitfields in CS */
  41245. +#define SPI_CS_LEN_LONG 0x02000000
  41246. +#define SPI_CS_DMA_LEN 0x01000000
  41247. +#define SPI_CS_CSPOL2 0x00800000
  41248. +#define SPI_CS_CSPOL1 0x00400000
  41249. +#define SPI_CS_CSPOL0 0x00200000
  41250. +#define SPI_CS_RXF 0x00100000
  41251. +#define SPI_CS_RXR 0x00080000
  41252. +#define SPI_CS_TXD 0x00040000
  41253. +#define SPI_CS_RXD 0x00020000
  41254. +#define SPI_CS_DONE 0x00010000
  41255. +#define SPI_CS_LEN 0x00002000
  41256. +#define SPI_CS_REN 0x00001000
  41257. +#define SPI_CS_ADCS 0x00000800
  41258. +#define SPI_CS_INTR 0x00000400
  41259. +#define SPI_CS_INTD 0x00000200
  41260. +#define SPI_CS_DMAEN 0x00000100
  41261. +#define SPI_CS_TA 0x00000080
  41262. +#define SPI_CS_CSPOL 0x00000040
  41263. +#define SPI_CS_CLEAR_RX 0x00000020
  41264. +#define SPI_CS_CLEAR_TX 0x00000010
  41265. +#define SPI_CS_CPOL 0x00000008
  41266. +#define SPI_CS_CPHA 0x00000004
  41267. +#define SPI_CS_CS_10 0x00000002
  41268. +#define SPI_CS_CS_01 0x00000001
  41269. +
  41270. +#define SPI_TIMEOUT_MS 150
  41271. +
  41272. +#define DRV_NAME "bcm2708_spi"
  41273. +
  41274. +struct bcm2708_spi {
  41275. + spinlock_t lock;
  41276. + void __iomem *base;
  41277. + int irq;
  41278. + struct clk *clk;
  41279. + bool stopping;
  41280. +
  41281. + struct list_head queue;
  41282. + struct workqueue_struct *workq;
  41283. + struct work_struct work;
  41284. + struct completion done;
  41285. +
  41286. + const u8 *tx_buf;
  41287. + u8 *rx_buf;
  41288. + int len;
  41289. +};
  41290. +
  41291. +struct bcm2708_spi_state {
  41292. + u32 cs;
  41293. + u16 cdiv;
  41294. +};
  41295. +
  41296. +/*
  41297. + * This function sets the ALT mode on the SPI pins so that we can use them with
  41298. + * the SPI hardware.
  41299. + *
  41300. + * FIXME: This is a hack. Use pinmux / pinctrl.
  41301. + */
  41302. +static void bcm2708_init_pinmode(void)
  41303. +{
  41304. +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  41305. +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  41306. +
  41307. + int pin;
  41308. + u32 *gpio = ioremap(GPIO_BASE, SZ_16K);
  41309. +
  41310. + /* SPI is on GPIO 7..11 */
  41311. + for (pin = 7; pin <= 11; pin++) {
  41312. + INP_GPIO(pin); /* set mode to GPIO input first */
  41313. + SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
  41314. + }
  41315. +
  41316. + iounmap(gpio);
  41317. +
  41318. +#undef INP_GPIO
  41319. +#undef SET_GPIO_ALT
  41320. +}
  41321. +
  41322. +static inline u32 bcm2708_rd(struct bcm2708_spi *bs, unsigned reg)
  41323. +{
  41324. + return readl(bs->base + reg);
  41325. +}
  41326. +
  41327. +static inline void bcm2708_wr(struct bcm2708_spi *bs, unsigned reg, u32 val)
  41328. +{
  41329. + writel(val, bs->base + reg);
  41330. +}
  41331. +
  41332. +static inline void bcm2708_rd_fifo(struct bcm2708_spi *bs, int len)
  41333. +{
  41334. + u8 byte;
  41335. +
  41336. + while (len--) {
  41337. + byte = bcm2708_rd(bs, SPI_FIFO);
  41338. + if (bs->rx_buf)
  41339. + *bs->rx_buf++ = byte;
  41340. + }
  41341. +}
  41342. +
  41343. +static inline void bcm2708_wr_fifo(struct bcm2708_spi *bs, int len)
  41344. +{
  41345. + u8 byte;
  41346. + u16 val;
  41347. +
  41348. + if (len > bs->len)
  41349. + len = bs->len;
  41350. +
  41351. + if (unlikely(bcm2708_rd(bs, SPI_CS) & SPI_CS_LEN)) {
  41352. + /* LoSSI mode */
  41353. + if (unlikely(len % 2)) {
  41354. + printk(KERN_ERR"bcm2708_wr_fifo: length must be even, skipping.\n");
  41355. + bs->len = 0;
  41356. + return;
  41357. + }
  41358. + while (len) {
  41359. + if (bs->tx_buf) {
  41360. + val = *(const u16 *)bs->tx_buf;
  41361. + bs->tx_buf += 2;
  41362. + } else
  41363. + val = 0;
  41364. + bcm2708_wr(bs, SPI_FIFO, val);
  41365. + bs->len -= 2;
  41366. + len -= 2;
  41367. + }
  41368. + return;
  41369. + }
  41370. +
  41371. + while (len--) {
  41372. + byte = bs->tx_buf ? *bs->tx_buf++ : 0;
  41373. + bcm2708_wr(bs, SPI_FIFO, byte);
  41374. + bs->len--;
  41375. + }
  41376. +}
  41377. +
  41378. +static irqreturn_t bcm2708_spi_interrupt(int irq, void *dev_id)
  41379. +{
  41380. + struct spi_master *master = dev_id;
  41381. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  41382. + u32 cs;
  41383. +
  41384. + spin_lock(&bs->lock);
  41385. +
  41386. + cs = bcm2708_rd(bs, SPI_CS);
  41387. +
  41388. + if (cs & SPI_CS_DONE) {
  41389. + if (bs->len) { /* first interrupt in a transfer */
  41390. + /* fill the TX fifo with up to 16 bytes */
  41391. + bcm2708_wr_fifo(bs, 16);
  41392. + } else { /* transfer complete */
  41393. + /* disable interrupts */
  41394. + cs &= ~(SPI_CS_INTR | SPI_CS_INTD);
  41395. + bcm2708_wr(bs, SPI_CS, cs);
  41396. +
  41397. + /* drain RX FIFO */
  41398. + while (cs & SPI_CS_RXD) {
  41399. + bcm2708_rd_fifo(bs, 1);
  41400. + cs = bcm2708_rd(bs, SPI_CS);
  41401. + }
  41402. +
  41403. + /* wake up our bh */
  41404. + complete(&bs->done);
  41405. + }
  41406. + } else if (cs & SPI_CS_RXR) {
  41407. + /* read 12 bytes of data */
  41408. + bcm2708_rd_fifo(bs, 12);
  41409. +
  41410. + /* write up to 12 bytes */
  41411. + bcm2708_wr_fifo(bs, 12);
  41412. + }
  41413. +
  41414. + spin_unlock(&bs->lock);
  41415. +
  41416. + return IRQ_HANDLED;
  41417. +}
  41418. +
  41419. +static int bcm2708_setup_state(struct spi_master *master,
  41420. + struct device *dev, struct bcm2708_spi_state *state,
  41421. + u32 hz, u8 csel, u8 mode, u8 bpw)
  41422. +{
  41423. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  41424. + int cdiv;
  41425. + unsigned long bus_hz;
  41426. + u32 cs = 0;
  41427. +
  41428. + bus_hz = clk_get_rate(bs->clk);
  41429. +
  41430. + if (hz >= bus_hz) {
  41431. + cdiv = 2; /* bus_hz / 2 is as fast as we can go */
  41432. + } else if (hz) {
  41433. + cdiv = DIV_ROUND_UP(bus_hz, hz);
  41434. +
  41435. + /* CDIV must be a power of 2, so round up */
  41436. + cdiv = roundup_pow_of_two(cdiv);
  41437. +
  41438. + if (cdiv > 65536) {
  41439. + dev_dbg(dev,
  41440. + "setup: %d Hz too slow, cdiv %u; min %ld Hz\n",
  41441. + hz, cdiv, bus_hz / 65536);
  41442. + return -EINVAL;
  41443. + } else if (cdiv == 65536) {
  41444. + cdiv = 0;
  41445. + } else if (cdiv == 1) {
  41446. + cdiv = 2; /* 1 gets rounded down to 0; == 65536 */
  41447. + }
  41448. + } else {
  41449. + cdiv = 0;
  41450. + }
  41451. +
  41452. + switch (bpw) {
  41453. + case 8:
  41454. + break;
  41455. + case 9:
  41456. + /* Reading in LoSSI mode is a special case. See 'BCM2835 ARM Peripherals' datasheet */
  41457. + cs |= SPI_CS_LEN;
  41458. + break;
  41459. + default:
  41460. + dev_dbg(dev, "setup: invalid bits_per_word %u (must be 8 or 9)\n",
  41461. + bpw);
  41462. + return -EINVAL;
  41463. + }
  41464. +
  41465. + if (mode & SPI_CPOL)
  41466. + cs |= SPI_CS_CPOL;
  41467. + if (mode & SPI_CPHA)
  41468. + cs |= SPI_CS_CPHA;
  41469. +
  41470. + if (!(mode & SPI_NO_CS)) {
  41471. + if (mode & SPI_CS_HIGH) {
  41472. + cs |= SPI_CS_CSPOL;
  41473. + cs |= SPI_CS_CSPOL0 << csel;
  41474. + }
  41475. +
  41476. + cs |= csel;
  41477. + } else {
  41478. + cs |= SPI_CS_CS_10 | SPI_CS_CS_01;
  41479. + }
  41480. +
  41481. + if (state) {
  41482. + state->cs = cs;
  41483. + state->cdiv = cdiv;
  41484. + dev_dbg(dev, "setup: want %d Hz; "
  41485. + "bus_hz=%lu / cdiv=%u == %lu Hz; "
  41486. + "mode %u: cs 0x%08X\n",
  41487. + hz, bus_hz, cdiv, bus_hz/cdiv, mode, cs);
  41488. + }
  41489. +
  41490. + return 0;
  41491. +}
  41492. +
  41493. +static int bcm2708_process_transfer(struct bcm2708_spi *bs,
  41494. + struct spi_message *msg, struct spi_transfer *xfer)
  41495. +{
  41496. + struct spi_device *spi = msg->spi;
  41497. + struct bcm2708_spi_state state, *stp;
  41498. + int ret;
  41499. + u32 cs;
  41500. +
  41501. + if (bs->stopping)
  41502. + return -ESHUTDOWN;
  41503. +
  41504. + if (xfer->bits_per_word || xfer->speed_hz) {
  41505. + ret = bcm2708_setup_state(spi->master, &spi->dev, &state,
  41506. + xfer->speed_hz ? xfer->speed_hz : spi->max_speed_hz,
  41507. + spi->chip_select, spi->mode,
  41508. + xfer->bits_per_word ? xfer->bits_per_word :
  41509. + spi->bits_per_word);
  41510. + if (ret)
  41511. + return ret;
  41512. +
  41513. + stp = &state;
  41514. + } else {
  41515. + stp = spi->controller_state;
  41516. + }
  41517. +
  41518. + INIT_COMPLETION(bs->done);
  41519. + bs->tx_buf = xfer->tx_buf;
  41520. + bs->rx_buf = xfer->rx_buf;
  41521. + bs->len = xfer->len;
  41522. +
  41523. + cs = stp->cs | SPI_CS_INTR | SPI_CS_INTD | SPI_CS_TA;
  41524. +
  41525. + bcm2708_wr(bs, SPI_CLK, stp->cdiv);
  41526. + bcm2708_wr(bs, SPI_CS, cs);
  41527. +
  41528. + ret = wait_for_completion_timeout(&bs->done,
  41529. + msecs_to_jiffies(SPI_TIMEOUT_MS));
  41530. + if (ret == 0) {
  41531. + dev_err(&spi->dev, "transfer timed out\n");
  41532. + return -ETIMEDOUT;
  41533. + }
  41534. +
  41535. + if (xfer->delay_usecs)
  41536. + udelay(xfer->delay_usecs);
  41537. +
  41538. + if (list_is_last(&xfer->transfer_list, &msg->transfers) ||
  41539. + xfer->cs_change) {
  41540. + /* clear TA and interrupt flags */
  41541. + bcm2708_wr(bs, SPI_CS, stp->cs);
  41542. + }
  41543. +
  41544. + msg->actual_length += (xfer->len - bs->len);
  41545. +
  41546. + return 0;
  41547. +}
  41548. +
  41549. +static void bcm2708_work(struct work_struct *work)
  41550. +{
  41551. + struct bcm2708_spi *bs = container_of(work, struct bcm2708_spi, work);
  41552. + unsigned long flags;
  41553. + struct spi_message *msg;
  41554. + struct spi_transfer *xfer;
  41555. + int status = 0;
  41556. +
  41557. + spin_lock_irqsave(&bs->lock, flags);
  41558. + while (!list_empty(&bs->queue)) {
  41559. + msg = list_first_entry(&bs->queue, struct spi_message, queue);
  41560. + list_del_init(&msg->queue);
  41561. + spin_unlock_irqrestore(&bs->lock, flags);
  41562. +
  41563. + list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  41564. + status = bcm2708_process_transfer(bs, msg, xfer);
  41565. + if (status)
  41566. + break;
  41567. + }
  41568. +
  41569. + msg->status = status;
  41570. + msg->complete(msg->context);
  41571. +
  41572. + spin_lock_irqsave(&bs->lock, flags);
  41573. + }
  41574. + spin_unlock_irqrestore(&bs->lock, flags);
  41575. +}
  41576. +
  41577. +static int bcm2708_spi_setup(struct spi_device *spi)
  41578. +{
  41579. + struct bcm2708_spi *bs = spi_master_get_devdata(spi->master);
  41580. + struct bcm2708_spi_state *state;
  41581. + int ret;
  41582. +
  41583. + if (bs->stopping)
  41584. + return -ESHUTDOWN;
  41585. +
  41586. + if (!(spi->mode & SPI_NO_CS) &&
  41587. + (spi->chip_select > spi->master->num_chipselect)) {
  41588. + dev_dbg(&spi->dev,
  41589. + "setup: invalid chipselect %u (%u defined)\n",
  41590. + spi->chip_select, spi->master->num_chipselect);
  41591. + return -EINVAL;
  41592. + }
  41593. +
  41594. + state = spi->controller_state;
  41595. + if (!state) {
  41596. + state = kzalloc(sizeof(*state), GFP_KERNEL);
  41597. + if (!state)
  41598. + return -ENOMEM;
  41599. +
  41600. + spi->controller_state = state;
  41601. + }
  41602. +
  41603. + ret = bcm2708_setup_state(spi->master, &spi->dev, state,
  41604. + spi->max_speed_hz, spi->chip_select, spi->mode,
  41605. + spi->bits_per_word);
  41606. + if (ret < 0) {
  41607. + kfree(state);
  41608. + spi->controller_state = NULL;
  41609. + return ret;
  41610. + }
  41611. +
  41612. + dev_dbg(&spi->dev,
  41613. + "setup: cd %d: %d Hz, bpw %u, mode 0x%x -> CS=%08x CDIV=%04x\n",
  41614. + spi->chip_select, spi->max_speed_hz, spi->bits_per_word,
  41615. + spi->mode, state->cs, state->cdiv);
  41616. +
  41617. + return 0;
  41618. +}
  41619. +
  41620. +static int bcm2708_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  41621. +{
  41622. + struct bcm2708_spi *bs = spi_master_get_devdata(spi->master);
  41623. + struct spi_transfer *xfer;
  41624. + int ret;
  41625. + unsigned long flags;
  41626. +
  41627. + if (unlikely(list_empty(&msg->transfers)))
  41628. + return -EINVAL;
  41629. +
  41630. + if (bs->stopping)
  41631. + return -ESHUTDOWN;
  41632. +
  41633. + list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  41634. + if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
  41635. + dev_dbg(&spi->dev, "missing rx or tx buf\n");
  41636. + return -EINVAL;
  41637. + }
  41638. +
  41639. + if (!xfer->bits_per_word || xfer->speed_hz)
  41640. + continue;
  41641. +
  41642. + ret = bcm2708_setup_state(spi->master, &spi->dev, NULL,
  41643. + xfer->speed_hz ? xfer->speed_hz : spi->max_speed_hz,
  41644. + spi->chip_select, spi->mode,
  41645. + xfer->bits_per_word ? xfer->bits_per_word :
  41646. + spi->bits_per_word);
  41647. + if (ret)
  41648. + return ret;
  41649. + }
  41650. +
  41651. + msg->status = -EINPROGRESS;
  41652. + msg->actual_length = 0;
  41653. +
  41654. + spin_lock_irqsave(&bs->lock, flags);
  41655. + list_add_tail(&msg->queue, &bs->queue);
  41656. + queue_work(bs->workq, &bs->work);
  41657. + spin_unlock_irqrestore(&bs->lock, flags);
  41658. +
  41659. + return 0;
  41660. +}
  41661. +
  41662. +static void bcm2708_spi_cleanup(struct spi_device *spi)
  41663. +{
  41664. + if (spi->controller_state) {
  41665. + kfree(spi->controller_state);
  41666. + spi->controller_state = NULL;
  41667. + }
  41668. +}
  41669. +
  41670. +static int bcm2708_spi_probe(struct platform_device *pdev)
  41671. +{
  41672. + struct resource *regs;
  41673. + int irq, err = -ENOMEM;
  41674. + struct clk *clk;
  41675. + struct spi_master *master;
  41676. + struct bcm2708_spi *bs;
  41677. +
  41678. + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  41679. + if (!regs) {
  41680. + dev_err(&pdev->dev, "could not get IO memory\n");
  41681. + return -ENXIO;
  41682. + }
  41683. +
  41684. + irq = platform_get_irq(pdev, 0);
  41685. + if (irq < 0) {
  41686. + dev_err(&pdev->dev, "could not get IRQ\n");
  41687. + return irq;
  41688. + }
  41689. +
  41690. + clk = clk_get(&pdev->dev, NULL);
  41691. + if (IS_ERR(clk)) {
  41692. + dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
  41693. + return PTR_ERR(clk);
  41694. + }
  41695. +
  41696. + bcm2708_init_pinmode();
  41697. +
  41698. + master = spi_alloc_master(&pdev->dev, sizeof(*bs));
  41699. + if (!master) {
  41700. + dev_err(&pdev->dev, "spi_alloc_master() failed\n");
  41701. + goto out_clk_put;
  41702. + }
  41703. +
  41704. + /* the spi->mode bits understood by this driver: */
  41705. + master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_NO_CS;
  41706. +
  41707. + master->bus_num = pdev->id;
  41708. + master->num_chipselect = 3;
  41709. + master->setup = bcm2708_spi_setup;
  41710. + master->transfer = bcm2708_spi_transfer;
  41711. + master->cleanup = bcm2708_spi_cleanup;
  41712. + platform_set_drvdata(pdev, master);
  41713. +
  41714. + bs = spi_master_get_devdata(master);
  41715. +
  41716. + spin_lock_init(&bs->lock);
  41717. + INIT_LIST_HEAD(&bs->queue);
  41718. + init_completion(&bs->done);
  41719. + INIT_WORK(&bs->work, bcm2708_work);
  41720. +
  41721. + bs->base = ioremap(regs->start, resource_size(regs));
  41722. + if (!bs->base) {
  41723. + dev_err(&pdev->dev, "could not remap memory\n");
  41724. + goto out_master_put;
  41725. + }
  41726. +
  41727. + bs->workq = create_singlethread_workqueue(dev_name(&pdev->dev));
  41728. + if (!bs->workq) {
  41729. + dev_err(&pdev->dev, "could not create workqueue\n");
  41730. + goto out_iounmap;
  41731. + }
  41732. +
  41733. + bs->irq = irq;
  41734. + bs->clk = clk;
  41735. + bs->stopping = false;
  41736. +
  41737. + err = request_irq(irq, bcm2708_spi_interrupt, 0, dev_name(&pdev->dev),
  41738. + master);
  41739. + if (err) {
  41740. + dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  41741. + goto out_workqueue;
  41742. + }
  41743. +
  41744. + /* initialise the hardware */
  41745. + clk_enable(clk);
  41746. + bcm2708_wr(bs, SPI_CS, SPI_CS_REN | SPI_CS_CLEAR_RX | SPI_CS_CLEAR_TX);
  41747. +
  41748. + err = spi_register_master(master);
  41749. + if (err) {
  41750. + dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
  41751. + goto out_free_irq;
  41752. + }
  41753. +
  41754. + dev_info(&pdev->dev, "SPI Controller at 0x%08lx (irq %d)\n",
  41755. + (unsigned long)regs->start, irq);
  41756. +
  41757. + return 0;
  41758. +
  41759. +out_free_irq:
  41760. + free_irq(bs->irq, master);
  41761. +out_workqueue:
  41762. + destroy_workqueue(bs->workq);
  41763. +out_iounmap:
  41764. + iounmap(bs->base);
  41765. +out_master_put:
  41766. + spi_master_put(master);
  41767. +out_clk_put:
  41768. + clk_put(clk);
  41769. + return err;
  41770. +}
  41771. +
  41772. +static int bcm2708_spi_remove(struct platform_device *pdev)
  41773. +{
  41774. + struct spi_master *master = platform_get_drvdata(pdev);
  41775. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  41776. +
  41777. + /* reset the hardware and block queue progress */
  41778. + spin_lock_irq(&bs->lock);
  41779. + bs->stopping = true;
  41780. + bcm2708_wr(bs, SPI_CS, SPI_CS_CLEAR_RX | SPI_CS_CLEAR_TX);
  41781. + spin_unlock_irq(&bs->lock);
  41782. +
  41783. + flush_work_sync(&bs->work);
  41784. +
  41785. + clk_disable(bs->clk);
  41786. + clk_put(bs->clk);
  41787. + free_irq(bs->irq, master);
  41788. + iounmap(bs->base);
  41789. +
  41790. + spi_unregister_master(master);
  41791. +
  41792. + return 0;
  41793. +}
  41794. +
  41795. +static struct platform_driver bcm2708_spi_driver = {
  41796. + .driver = {
  41797. + .name = DRV_NAME,
  41798. + .owner = THIS_MODULE,
  41799. + },
  41800. + .probe = bcm2708_spi_probe,
  41801. + .remove = bcm2708_spi_remove,
  41802. +};
  41803. +
  41804. +
  41805. +static int __init bcm2708_spi_init(void)
  41806. +{
  41807. + return platform_driver_probe(&bcm2708_spi_driver, bcm2708_spi_probe);
  41808. +}
  41809. +module_init(bcm2708_spi_init);
  41810. +
  41811. +static void __exit bcm2708_spi_exit(void)
  41812. +{
  41813. + platform_driver_unregister(&bcm2708_spi_driver);
  41814. +}
  41815. +module_exit(bcm2708_spi_exit);
  41816. +
  41817. +
  41818. +//module_platform_driver(bcm2708_spi_driver);
  41819. +
  41820. +MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2708");
  41821. +MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  41822. +MODULE_LICENSE("GPL v2");
  41823. +MODULE_ALIAS("platform:" DRV_NAME);
  41824. diff -Nur linux-3.12.33/drivers/staging/media/lirc/Kconfig linux-3.12.33-rpi/drivers/staging/media/lirc/Kconfig
  41825. --- linux-3.12.33/drivers/staging/media/lirc/Kconfig 2014-11-15 06:28:07.000000000 -0600
  41826. +++ linux-3.12.33-rpi/drivers/staging/media/lirc/Kconfig 2014-12-03 19:13:39.736418001 -0600
  41827. @@ -38,6 +38,12 @@
  41828. help
  41829. Driver for Homebrew Parallel Port Receivers
  41830. +config LIRC_RPI
  41831. + tristate "Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi"
  41832. + depends on LIRC
  41833. + help
  41834. + Driver for Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi
  41835. +
  41836. config LIRC_SASEM
  41837. tristate "Sasem USB IR Remote"
  41838. depends on LIRC && USB
  41839. diff -Nur linux-3.12.33/drivers/staging/media/lirc/lirc_rpi.c linux-3.12.33-rpi/drivers/staging/media/lirc/lirc_rpi.c
  41840. --- linux-3.12.33/drivers/staging/media/lirc/lirc_rpi.c 1969-12-31 18:00:00.000000000 -0600
  41841. +++ linux-3.12.33-rpi/drivers/staging/media/lirc/lirc_rpi.c 2014-12-03 19:13:39.736418001 -0600
  41842. @@ -0,0 +1,689 @@
  41843. +/*
  41844. + * lirc_rpi.c
  41845. + *
  41846. + * lirc_rpi - Device driver that records pulse- and pause-lengths
  41847. + * (space-lengths) (just like the lirc_serial driver does)
  41848. + * between GPIO interrupt events on the Raspberry Pi.
  41849. + * Lots of code has been taken from the lirc_serial module,
  41850. + * so I would like say thanks to the authors.
  41851. + *
  41852. + * Copyright (C) 2012 Aron Robert Szabo <aron@reon.hu>,
  41853. + * Michael Bishop <cleverca22@gmail.com>
  41854. + * This program is free software; you can redistribute it and/or modify
  41855. + * it under the terms of the GNU General Public License as published by
  41856. + * the Free Software Foundation; either version 2 of the License, or
  41857. + * (at your option) any later version.
  41858. + *
  41859. + * This program is distributed in the hope that it will be useful,
  41860. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  41861. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41862. + * GNU General Public License for more details.
  41863. + *
  41864. + * You should have received a copy of the GNU General Public License
  41865. + * along with this program; if not, write to the Free Software
  41866. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  41867. + */
  41868. +
  41869. +#include <linux/module.h>
  41870. +#include <linux/errno.h>
  41871. +#include <linux/interrupt.h>
  41872. +#include <linux/sched.h>
  41873. +#include <linux/kernel.h>
  41874. +#include <linux/time.h>
  41875. +#include <linux/timex.h>
  41876. +#include <linux/string.h>
  41877. +#include <linux/delay.h>
  41878. +#include <linux/platform_device.h>
  41879. +#include <linux/irq.h>
  41880. +#include <linux/spinlock.h>
  41881. +#include <media/lirc.h>
  41882. +#include <media/lirc_dev.h>
  41883. +#include <mach/gpio.h>
  41884. +#include <linux/gpio.h>
  41885. +
  41886. +#include <linux/platform_data/bcm2708.h>
  41887. +
  41888. +#define LIRC_DRIVER_NAME "lirc_rpi"
  41889. +#define RBUF_LEN 256
  41890. +#define LIRC_TRANSMITTER_LATENCY 50
  41891. +
  41892. +#ifndef MAX_UDELAY_MS
  41893. +#define MAX_UDELAY_US 5000
  41894. +#else
  41895. +#define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
  41896. +#endif
  41897. +
  41898. +#define dprintk(fmt, args...) \
  41899. + do { \
  41900. + if (debug) \
  41901. + printk(KERN_DEBUG LIRC_DRIVER_NAME ": " \
  41902. + fmt, ## args); \
  41903. + } while (0)
  41904. +
  41905. +/* module parameters */
  41906. +
  41907. +/* set the default GPIO input pin */
  41908. +static int gpio_in_pin = 18;
  41909. +/* set the default pull behaviour for input pin */
  41910. +static int gpio_in_pull = BCM2708_PULL_DOWN;
  41911. +/* set the default GPIO output pin */
  41912. +static int gpio_out_pin = 17;
  41913. +/* enable debugging messages */
  41914. +static bool debug;
  41915. +/* -1 = auto, 0 = active high, 1 = active low */
  41916. +static int sense = -1;
  41917. +/* use softcarrier by default */
  41918. +static bool softcarrier = 1;
  41919. +/* 0 = do not invert output, 1 = invert output */
  41920. +static bool invert = 0;
  41921. +
  41922. +struct gpio_chip *gpiochip;
  41923. +struct irq_chip *irqchip;
  41924. +struct irq_data *irqdata;
  41925. +
  41926. +/* forward declarations */
  41927. +static long send_pulse(unsigned long length);
  41928. +static void send_space(long length);
  41929. +static void lirc_rpi_exit(void);
  41930. +
  41931. +static struct platform_device *lirc_rpi_dev;
  41932. +static struct timeval lasttv = { 0, 0 };
  41933. +static struct lirc_buffer rbuf;
  41934. +static spinlock_t lock;
  41935. +
  41936. +/* initialized/set in init_timing_params() */
  41937. +static unsigned int freq = 38000;
  41938. +static unsigned int duty_cycle = 50;
  41939. +static unsigned long period;
  41940. +static unsigned long pulse_width;
  41941. +static unsigned long space_width;
  41942. +
  41943. +static void safe_udelay(unsigned long usecs)
  41944. +{
  41945. + while (usecs > MAX_UDELAY_US) {
  41946. + udelay(MAX_UDELAY_US);
  41947. + usecs -= MAX_UDELAY_US;
  41948. + }
  41949. + udelay(usecs);
  41950. +}
  41951. +
  41952. +static int init_timing_params(unsigned int new_duty_cycle,
  41953. + unsigned int new_freq)
  41954. +{
  41955. + if (1000 * 1000000L / new_freq * new_duty_cycle / 100 <=
  41956. + LIRC_TRANSMITTER_LATENCY)
  41957. + return -EINVAL;
  41958. + if (1000 * 1000000L / new_freq * (100 - new_duty_cycle) / 100 <=
  41959. + LIRC_TRANSMITTER_LATENCY)
  41960. + return -EINVAL;
  41961. + duty_cycle = new_duty_cycle;
  41962. + freq = new_freq;
  41963. + period = 1000 * 1000000L / freq;
  41964. + pulse_width = period * duty_cycle / 100;
  41965. + space_width = period - pulse_width;
  41966. + dprintk("in init_timing_params, freq=%d pulse=%ld, "
  41967. + "space=%ld\n", freq, pulse_width, space_width);
  41968. + return 0;
  41969. +}
  41970. +
  41971. +static long send_pulse_softcarrier(unsigned long length)
  41972. +{
  41973. + int flag;
  41974. + unsigned long actual, target;
  41975. + unsigned long actual_us, initial_us, target_us;
  41976. +
  41977. + length *= 1000;
  41978. +
  41979. + actual = 0; target = 0; flag = 0;
  41980. + read_current_timer(&actual_us);
  41981. +
  41982. + while (actual < length) {
  41983. + if (flag) {
  41984. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  41985. + target += space_width;
  41986. + } else {
  41987. + gpiochip->set(gpiochip, gpio_out_pin, !invert);
  41988. + target += pulse_width;
  41989. + }
  41990. + initial_us = actual_us;
  41991. + target_us = actual_us + (target - actual) / 1000;
  41992. + /*
  41993. + * Note - we've checked in ioctl that the pulse/space
  41994. + * widths are big enough so that d is > 0
  41995. + */
  41996. + if ((int)(target_us - actual_us) > 0)
  41997. + udelay(target_us - actual_us);
  41998. + read_current_timer(&actual_us);
  41999. + actual += (actual_us - initial_us) * 1000;
  42000. + flag = !flag;
  42001. + }
  42002. + return (actual-length) / 1000;
  42003. +}
  42004. +
  42005. +static long send_pulse(unsigned long length)
  42006. +{
  42007. + if (length <= 0)
  42008. + return 0;
  42009. +
  42010. + if (softcarrier) {
  42011. + return send_pulse_softcarrier(length);
  42012. + } else {
  42013. + gpiochip->set(gpiochip, gpio_out_pin, !invert);
  42014. + safe_udelay(length);
  42015. + return 0;
  42016. + }
  42017. +}
  42018. +
  42019. +static void send_space(long length)
  42020. +{
  42021. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  42022. + if (length <= 0)
  42023. + return;
  42024. + safe_udelay(length);
  42025. +}
  42026. +
  42027. +static void rbwrite(int l)
  42028. +{
  42029. + if (lirc_buffer_full(&rbuf)) {
  42030. + /* no new signals will be accepted */
  42031. + dprintk("Buffer overrun\n");
  42032. + return;
  42033. + }
  42034. + lirc_buffer_write(&rbuf, (void *)&l);
  42035. +}
  42036. +
  42037. +static void frbwrite(int l)
  42038. +{
  42039. + /* simple noise filter */
  42040. + static int pulse, space;
  42041. + static unsigned int ptr;
  42042. +
  42043. + if (ptr > 0 && (l & PULSE_BIT)) {
  42044. + pulse += l & PULSE_MASK;
  42045. + if (pulse > 250) {
  42046. + rbwrite(space);
  42047. + rbwrite(pulse | PULSE_BIT);
  42048. + ptr = 0;
  42049. + pulse = 0;
  42050. + }
  42051. + return;
  42052. + }
  42053. + if (!(l & PULSE_BIT)) {
  42054. + if (ptr == 0) {
  42055. + if (l > 20000) {
  42056. + space = l;
  42057. + ptr++;
  42058. + return;
  42059. + }
  42060. + } else {
  42061. + if (l > 20000) {
  42062. + space += pulse;
  42063. + if (space > PULSE_MASK)
  42064. + space = PULSE_MASK;
  42065. + space += l;
  42066. + if (space > PULSE_MASK)
  42067. + space = PULSE_MASK;
  42068. + pulse = 0;
  42069. + return;
  42070. + }
  42071. + rbwrite(space);
  42072. + rbwrite(pulse | PULSE_BIT);
  42073. + ptr = 0;
  42074. + pulse = 0;
  42075. + }
  42076. + }
  42077. + rbwrite(l);
  42078. +}
  42079. +
  42080. +static irqreturn_t irq_handler(int i, void *blah, struct pt_regs *regs)
  42081. +{
  42082. + struct timeval tv;
  42083. + long deltv;
  42084. + int data;
  42085. + int signal;
  42086. +
  42087. + /* use the GPIO signal level */
  42088. + signal = gpiochip->get(gpiochip, gpio_in_pin);
  42089. +
  42090. + /* unmask the irq */
  42091. + irqchip->irq_unmask(irqdata);
  42092. +
  42093. + if (sense != -1) {
  42094. + /* get current time */
  42095. + do_gettimeofday(&tv);
  42096. +
  42097. + /* calc time since last interrupt in microseconds */
  42098. + deltv = tv.tv_sec-lasttv.tv_sec;
  42099. + if (tv.tv_sec < lasttv.tv_sec ||
  42100. + (tv.tv_sec == lasttv.tv_sec &&
  42101. + tv.tv_usec < lasttv.tv_usec)) {
  42102. + printk(KERN_WARNING LIRC_DRIVER_NAME
  42103. + ": AIEEEE: your clock just jumped backwards\n");
  42104. + printk(KERN_WARNING LIRC_DRIVER_NAME
  42105. + ": %d %d %lx %lx %lx %lx\n", signal, sense,
  42106. + tv.tv_sec, lasttv.tv_sec,
  42107. + tv.tv_usec, lasttv.tv_usec);
  42108. + data = PULSE_MASK;
  42109. + } else if (deltv > 15) {
  42110. + data = PULSE_MASK; /* really long time */
  42111. + if (!(signal^sense)) {
  42112. + /* sanity check */
  42113. + printk(KERN_WARNING LIRC_DRIVER_NAME
  42114. + ": AIEEEE: %d %d %lx %lx %lx %lx\n",
  42115. + signal, sense, tv.tv_sec, lasttv.tv_sec,
  42116. + tv.tv_usec, lasttv.tv_usec);
  42117. + /*
  42118. + * detecting pulse while this
  42119. + * MUST be a space!
  42120. + */
  42121. + sense = sense ? 0 : 1;
  42122. + }
  42123. + } else {
  42124. + data = (int) (deltv*1000000 +
  42125. + (tv.tv_usec - lasttv.tv_usec));
  42126. + }
  42127. + frbwrite(signal^sense ? data : (data|PULSE_BIT));
  42128. + lasttv = tv;
  42129. + wake_up_interruptible(&rbuf.wait_poll);
  42130. + }
  42131. +
  42132. + return IRQ_HANDLED;
  42133. +}
  42134. +
  42135. +static int is_right_chip(struct gpio_chip *chip, void *data)
  42136. +{
  42137. + dprintk("is_right_chip %s %d\n", chip->label, strcmp(data, chip->label));
  42138. +
  42139. + if (strcmp(data, chip->label) == 0)
  42140. + return 1;
  42141. + return 0;
  42142. +}
  42143. +
  42144. +static int init_port(void)
  42145. +{
  42146. + int i, nlow, nhigh, ret, irq;
  42147. +
  42148. + gpiochip = gpiochip_find("bcm2708_gpio", is_right_chip);
  42149. +
  42150. + if (!gpiochip)
  42151. + return -ENODEV;
  42152. +
  42153. + if (gpio_request(gpio_out_pin, LIRC_DRIVER_NAME " ir/out")) {
  42154. + printk(KERN_ALERT LIRC_DRIVER_NAME
  42155. + ": cant claim gpio pin %d\n", gpio_out_pin);
  42156. + ret = -ENODEV;
  42157. + goto exit_init_port;
  42158. + }
  42159. +
  42160. + if (gpio_request(gpio_in_pin, LIRC_DRIVER_NAME " ir/in")) {
  42161. + printk(KERN_ALERT LIRC_DRIVER_NAME
  42162. + ": cant claim gpio pin %d\n", gpio_in_pin);
  42163. + ret = -ENODEV;
  42164. + goto exit_gpio_free_out_pin;
  42165. + }
  42166. +
  42167. + bcm2708_gpio_setpull(gpiochip, gpio_in_pin, gpio_in_pull);
  42168. + gpiochip->direction_input(gpiochip, gpio_in_pin);
  42169. + gpiochip->direction_output(gpiochip, gpio_out_pin, 1);
  42170. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  42171. +
  42172. + irq = gpiochip->to_irq(gpiochip, gpio_in_pin);
  42173. + dprintk("to_irq %d\n", irq);
  42174. + irqdata = irq_get_irq_data(irq);
  42175. +
  42176. + if (irqdata && irqdata->chip) {
  42177. + irqchip = irqdata->chip;
  42178. + } else {
  42179. + ret = -ENODEV;
  42180. + goto exit_gpio_free_in_pin;
  42181. + }
  42182. +
  42183. + /* if pin is high, then this must be an active low receiver. */
  42184. + if (sense == -1) {
  42185. + /* wait 1/2 sec for the power supply */
  42186. + msleep(500);
  42187. +
  42188. + /*
  42189. + * probe 9 times every 0.04s, collect "votes" for
  42190. + * active high/low
  42191. + */
  42192. + nlow = 0;
  42193. + nhigh = 0;
  42194. + for (i = 0; i < 9; i++) {
  42195. + if (gpiochip->get(gpiochip, gpio_in_pin))
  42196. + nlow++;
  42197. + else
  42198. + nhigh++;
  42199. + msleep(40);
  42200. + }
  42201. + sense = (nlow >= nhigh ? 1 : 0);
  42202. + printk(KERN_INFO LIRC_DRIVER_NAME
  42203. + ": auto-detected active %s receiver on GPIO pin %d\n",
  42204. + sense ? "low" : "high", gpio_in_pin);
  42205. + } else {
  42206. + printk(KERN_INFO LIRC_DRIVER_NAME
  42207. + ": manually using active %s receiver on GPIO pin %d\n",
  42208. + sense ? "low" : "high", gpio_in_pin);
  42209. + }
  42210. +
  42211. + return 0;
  42212. +
  42213. + exit_gpio_free_in_pin:
  42214. + gpio_free(gpio_in_pin);
  42215. +
  42216. + exit_gpio_free_out_pin:
  42217. + gpio_free(gpio_out_pin);
  42218. +
  42219. + exit_init_port:
  42220. + return ret;
  42221. +}
  42222. +
  42223. +// called when the character device is opened
  42224. +static int set_use_inc(void *data)
  42225. +{
  42226. + int result;
  42227. + unsigned long flags;
  42228. +
  42229. + /* initialize timestamp */
  42230. + do_gettimeofday(&lasttv);
  42231. +
  42232. + result = request_irq(gpiochip->to_irq(gpiochip, gpio_in_pin),
  42233. + (irq_handler_t) irq_handler, 0,
  42234. + LIRC_DRIVER_NAME, (void*) 0);
  42235. +
  42236. + switch (result) {
  42237. + case -EBUSY:
  42238. + printk(KERN_ERR LIRC_DRIVER_NAME
  42239. + ": IRQ %d is busy\n",
  42240. + gpiochip->to_irq(gpiochip, gpio_in_pin));
  42241. + return -EBUSY;
  42242. + case -EINVAL:
  42243. + printk(KERN_ERR LIRC_DRIVER_NAME
  42244. + ": Bad irq number or handler\n");
  42245. + return -EINVAL;
  42246. + default:
  42247. + dprintk("Interrupt %d obtained\n",
  42248. + gpiochip->to_irq(gpiochip, gpio_in_pin));
  42249. + break;
  42250. + };
  42251. +
  42252. + /* initialize pulse/space widths */
  42253. + init_timing_params(duty_cycle, freq);
  42254. +
  42255. + spin_lock_irqsave(&lock, flags);
  42256. +
  42257. + /* GPIO Pin Falling/Rising Edge Detect Enable */
  42258. + irqchip->irq_set_type(irqdata,
  42259. + IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING);
  42260. +
  42261. + /* unmask the irq */
  42262. + irqchip->irq_unmask(irqdata);
  42263. +
  42264. + spin_unlock_irqrestore(&lock, flags);
  42265. +
  42266. + return 0;
  42267. +}
  42268. +
  42269. +static void set_use_dec(void *data)
  42270. +{
  42271. + unsigned long flags;
  42272. +
  42273. + spin_lock_irqsave(&lock, flags);
  42274. +
  42275. + /* GPIO Pin Falling/Rising Edge Detect Disable */
  42276. + irqchip->irq_set_type(irqdata, 0);
  42277. + irqchip->irq_mask(irqdata);
  42278. +
  42279. + spin_unlock_irqrestore(&lock, flags);
  42280. +
  42281. + free_irq(gpiochip->to_irq(gpiochip, gpio_in_pin), (void *) 0);
  42282. +
  42283. + dprintk(KERN_INFO LIRC_DRIVER_NAME
  42284. + ": freed IRQ %d\n", gpiochip->to_irq(gpiochip, gpio_in_pin));
  42285. +}
  42286. +
  42287. +static ssize_t lirc_write(struct file *file, const char *buf,
  42288. + size_t n, loff_t *ppos)
  42289. +{
  42290. + int i, count;
  42291. + unsigned long flags;
  42292. + long delta = 0;
  42293. + int *wbuf;
  42294. +
  42295. + count = n / sizeof(int);
  42296. + if (n % sizeof(int) || count % 2 == 0)
  42297. + return -EINVAL;
  42298. + wbuf = memdup_user(buf, n);
  42299. + if (IS_ERR(wbuf))
  42300. + return PTR_ERR(wbuf);
  42301. + spin_lock_irqsave(&lock, flags);
  42302. +
  42303. + for (i = 0; i < count; i++) {
  42304. + if (i%2)
  42305. + send_space(wbuf[i] - delta);
  42306. + else
  42307. + delta = send_pulse(wbuf[i]);
  42308. + }
  42309. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  42310. +
  42311. + spin_unlock_irqrestore(&lock, flags);
  42312. + kfree(wbuf);
  42313. + return n;
  42314. +}
  42315. +
  42316. +static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  42317. +{
  42318. + int result;
  42319. + __u32 value;
  42320. +
  42321. + switch (cmd) {
  42322. + case LIRC_GET_SEND_MODE:
  42323. + return -ENOIOCTLCMD;
  42324. + break;
  42325. +
  42326. + case LIRC_SET_SEND_MODE:
  42327. + result = get_user(value, (__u32 *) arg);
  42328. + if (result)
  42329. + return result;
  42330. + /* only LIRC_MODE_PULSE supported */
  42331. + if (value != LIRC_MODE_PULSE)
  42332. + return -ENOSYS;
  42333. + break;
  42334. +
  42335. + case LIRC_GET_LENGTH:
  42336. + return -ENOSYS;
  42337. + break;
  42338. +
  42339. + case LIRC_SET_SEND_DUTY_CYCLE:
  42340. + dprintk("SET_SEND_DUTY_CYCLE\n");
  42341. + result = get_user(value, (__u32 *) arg);
  42342. + if (result)
  42343. + return result;
  42344. + if (value <= 0 || value > 100)
  42345. + return -EINVAL;
  42346. + return init_timing_params(value, freq);
  42347. + break;
  42348. +
  42349. + case LIRC_SET_SEND_CARRIER:
  42350. + dprintk("SET_SEND_CARRIER\n");
  42351. + result = get_user(value, (__u32 *) arg);
  42352. + if (result)
  42353. + return result;
  42354. + if (value > 500000 || value < 20000)
  42355. + return -EINVAL;
  42356. + return init_timing_params(duty_cycle, value);
  42357. + break;
  42358. +
  42359. + default:
  42360. + return lirc_dev_fop_ioctl(filep, cmd, arg);
  42361. + }
  42362. + return 0;
  42363. +}
  42364. +
  42365. +static const struct file_operations lirc_fops = {
  42366. + .owner = THIS_MODULE,
  42367. + .write = lirc_write,
  42368. + .unlocked_ioctl = lirc_ioctl,
  42369. + .read = lirc_dev_fop_read,
  42370. + .poll = lirc_dev_fop_poll,
  42371. + .open = lirc_dev_fop_open,
  42372. + .release = lirc_dev_fop_close,
  42373. + .llseek = no_llseek,
  42374. +};
  42375. +
  42376. +static struct lirc_driver driver = {
  42377. + .name = LIRC_DRIVER_NAME,
  42378. + .minor = -1,
  42379. + .code_length = 1,
  42380. + .sample_rate = 0,
  42381. + .data = NULL,
  42382. + .add_to_buf = NULL,
  42383. + .rbuf = &rbuf,
  42384. + .set_use_inc = set_use_inc,
  42385. + .set_use_dec = set_use_dec,
  42386. + .fops = &lirc_fops,
  42387. + .dev = NULL,
  42388. + .owner = THIS_MODULE,
  42389. +};
  42390. +
  42391. +static struct platform_driver lirc_rpi_driver = {
  42392. + .driver = {
  42393. + .name = LIRC_DRIVER_NAME,
  42394. + .owner = THIS_MODULE,
  42395. + },
  42396. +};
  42397. +
  42398. +static int __init lirc_rpi_init(void)
  42399. +{
  42400. + int result;
  42401. +
  42402. + /* Init read buffer. */
  42403. + result = lirc_buffer_init(&rbuf, sizeof(int), RBUF_LEN);
  42404. + if (result < 0)
  42405. + return -ENOMEM;
  42406. +
  42407. + result = platform_driver_register(&lirc_rpi_driver);
  42408. + if (result) {
  42409. + printk(KERN_ERR LIRC_DRIVER_NAME
  42410. + ": lirc register returned %d\n", result);
  42411. + goto exit_buffer_free;
  42412. + }
  42413. +
  42414. + lirc_rpi_dev = platform_device_alloc(LIRC_DRIVER_NAME, 0);
  42415. + if (!lirc_rpi_dev) {
  42416. + result = -ENOMEM;
  42417. + goto exit_driver_unregister;
  42418. + }
  42419. +
  42420. + result = platform_device_add(lirc_rpi_dev);
  42421. + if (result)
  42422. + goto exit_device_put;
  42423. +
  42424. + return 0;
  42425. +
  42426. + exit_device_put:
  42427. + platform_device_put(lirc_rpi_dev);
  42428. +
  42429. + exit_driver_unregister:
  42430. + platform_driver_unregister(&lirc_rpi_driver);
  42431. +
  42432. + exit_buffer_free:
  42433. + lirc_buffer_free(&rbuf);
  42434. +
  42435. + return result;
  42436. +}
  42437. +
  42438. +static void lirc_rpi_exit(void)
  42439. +{
  42440. + platform_device_unregister(lirc_rpi_dev);
  42441. + platform_driver_unregister(&lirc_rpi_driver);
  42442. + lirc_buffer_free(&rbuf);
  42443. +}
  42444. +
  42445. +static int __init lirc_rpi_init_module(void)
  42446. +{
  42447. + int result;
  42448. +
  42449. + result = lirc_rpi_init();
  42450. + if (result)
  42451. + return result;
  42452. +
  42453. + if (gpio_in_pin >= BCM2708_NR_GPIOS || gpio_out_pin >= BCM2708_NR_GPIOS) {
  42454. + result = -EINVAL;
  42455. + printk(KERN_ERR LIRC_DRIVER_NAME
  42456. + ": invalid GPIO pin(s) specified!\n");
  42457. + goto exit_rpi;
  42458. + }
  42459. +
  42460. + result = init_port();
  42461. + if (result < 0)
  42462. + goto exit_rpi;
  42463. +
  42464. + driver.features = LIRC_CAN_SET_SEND_DUTY_CYCLE |
  42465. + LIRC_CAN_SET_SEND_CARRIER |
  42466. + LIRC_CAN_SEND_PULSE |
  42467. + LIRC_CAN_REC_MODE2;
  42468. +
  42469. + driver.dev = &lirc_rpi_dev->dev;
  42470. + driver.minor = lirc_register_driver(&driver);
  42471. +
  42472. + if (driver.minor < 0) {
  42473. + printk(KERN_ERR LIRC_DRIVER_NAME
  42474. + ": device registration failed with %d\n", result);
  42475. + result = -EIO;
  42476. + goto exit_rpi;
  42477. + }
  42478. +
  42479. + printk(KERN_INFO LIRC_DRIVER_NAME ": driver registered!\n");
  42480. +
  42481. + return 0;
  42482. +
  42483. + exit_rpi:
  42484. + lirc_rpi_exit();
  42485. +
  42486. + return result;
  42487. +}
  42488. +
  42489. +static void __exit lirc_rpi_exit_module(void)
  42490. +{
  42491. + gpio_free(gpio_out_pin);
  42492. + gpio_free(gpio_in_pin);
  42493. +
  42494. + lirc_rpi_exit();
  42495. +
  42496. + lirc_unregister_driver(driver.minor);
  42497. + printk(KERN_INFO LIRC_DRIVER_NAME ": cleaned up module\n");
  42498. +}
  42499. +
  42500. +module_init(lirc_rpi_init_module);
  42501. +module_exit(lirc_rpi_exit_module);
  42502. +
  42503. +MODULE_DESCRIPTION("Infra-red receiver and blaster driver for Raspberry Pi GPIO.");
  42504. +MODULE_AUTHOR("Aron Robert Szabo <aron@reon.hu>");
  42505. +MODULE_AUTHOR("Michael Bishop <cleverca22@gmail.com>");
  42506. +MODULE_LICENSE("GPL");
  42507. +
  42508. +module_param(gpio_out_pin, int, S_IRUGO);
  42509. +MODULE_PARM_DESC(gpio_out_pin, "GPIO output/transmitter pin number of the BCM"
  42510. + " processor. (default 17");
  42511. +
  42512. +module_param(gpio_in_pin, int, S_IRUGO);
  42513. +MODULE_PARM_DESC(gpio_in_pin, "GPIO input pin number of the BCM processor."
  42514. + " (default 18");
  42515. +
  42516. +module_param(gpio_in_pull, int, S_IRUGO);
  42517. +MODULE_PARM_DESC(gpio_in_pull, "GPIO input pin pull configuration."
  42518. + " (0 = off, 1 = up, 2 = down, default down)");
  42519. +
  42520. +module_param(sense, int, S_IRUGO);
  42521. +MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit"
  42522. + " (0 = active high, 1 = active low )");
  42523. +
  42524. +module_param(softcarrier, bool, S_IRUGO);
  42525. +MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on, default on)");
  42526. +
  42527. +module_param(invert, bool, S_IRUGO);
  42528. +MODULE_PARM_DESC(invert, "Invert output (0 = off, 1 = on, default off");
  42529. +
  42530. +module_param(debug, bool, S_IRUGO | S_IWUSR);
  42531. +MODULE_PARM_DESC(debug, "Enable debugging messages");
  42532. diff -Nur linux-3.12.33/drivers/staging/media/lirc/Makefile linux-3.12.33-rpi/drivers/staging/media/lirc/Makefile
  42533. --- linux-3.12.33/drivers/staging/media/lirc/Makefile 2014-11-15 06:28:07.000000000 -0600
  42534. +++ linux-3.12.33-rpi/drivers/staging/media/lirc/Makefile 2014-12-03 19:13:39.736418001 -0600
  42535. @@ -7,6 +7,7 @@
  42536. obj-$(CONFIG_LIRC_IGORPLUGUSB) += lirc_igorplugusb.o
  42537. obj-$(CONFIG_LIRC_IMON) += lirc_imon.o
  42538. obj-$(CONFIG_LIRC_PARALLEL) += lirc_parallel.o
  42539. +obj-$(CONFIG_LIRC_RPI) += lirc_rpi.o
  42540. obj-$(CONFIG_LIRC_SASEM) += lirc_sasem.o
  42541. obj-$(CONFIG_LIRC_SERIAL) += lirc_serial.o
  42542. obj-$(CONFIG_LIRC_SIR) += lirc_sir.o
  42543. diff -Nur linux-3.12.33/drivers/thermal/bcm2835-thermal.c linux-3.12.33-rpi/drivers/thermal/bcm2835-thermal.c
  42544. --- linux-3.12.33/drivers/thermal/bcm2835-thermal.c 1969-12-31 18:00:00.000000000 -0600
  42545. +++ linux-3.12.33-rpi/drivers/thermal/bcm2835-thermal.c 2014-12-03 19:13:40.136418001 -0600
  42546. @@ -0,0 +1,184 @@
  42547. +/*****************************************************************************
  42548. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  42549. +*
  42550. +* Unless you and Broadcom execute a separate written software license
  42551. +* agreement governing use of this software, this software is licensed to you
  42552. +* under the terms of the GNU General Public License version 2, available at
  42553. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  42554. +*
  42555. +* Notwithstanding the above, under no circumstances may you combine this
  42556. +* software in any way with any other Broadcom software provided under a
  42557. +* license other than the GPL, without Broadcom's express prior written
  42558. +* consent.
  42559. +*****************************************************************************/
  42560. +
  42561. +#include <linux/kernel.h>
  42562. +#include <linux/module.h>
  42563. +#include <linux/init.h>
  42564. +#include <linux/platform_device.h>
  42565. +#include <linux/slab.h>
  42566. +#include <linux/sysfs.h>
  42567. +#include <mach/vcio.h>
  42568. +#include <linux/thermal.h>
  42569. +
  42570. +
  42571. +/* --- DEFINITIONS --- */
  42572. +#define MODULE_NAME "bcm2835_thermal"
  42573. +
  42574. +/*#define THERMAL_DEBUG_ENABLE*/
  42575. +
  42576. +#ifdef THERMAL_DEBUG_ENABLE
  42577. +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  42578. +#else
  42579. +#define print_debug(fmt,...)
  42580. +#endif
  42581. +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  42582. +
  42583. +#define VC_TAG_GET_TEMP 0x00030006
  42584. +#define VC_TAG_GET_MAX_TEMP 0x0003000A
  42585. +
  42586. +typedef enum {
  42587. + TEMP,
  42588. + MAX_TEMP,
  42589. +} temp_type;
  42590. +
  42591. +/* --- STRUCTS --- */
  42592. +/* tag part of the message */
  42593. +struct vc_msg_tag {
  42594. + uint32_t tag_id; /* the tag ID for the temperature */
  42595. + uint32_t buffer_size; /* size of the buffer (should be 8) */
  42596. + uint32_t request_code; /* identifies message as a request (should be 0) */
  42597. + uint32_t id; /* extra ID field (should be 0) */
  42598. + uint32_t val; /* returned value of the temperature */
  42599. +};
  42600. +
  42601. +/* message structure to be sent to videocore */
  42602. +struct vc_msg {
  42603. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  42604. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  42605. + struct vc_msg_tag tag; /* the tag structure above to make */
  42606. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  42607. +};
  42608. +
  42609. +struct bcm2835_thermal_data {
  42610. + struct thermal_zone_device *thermal_dev;
  42611. + struct vc_msg msg;
  42612. +};
  42613. +
  42614. +/* --- GLOBALS --- */
  42615. +static struct bcm2835_thermal_data bcm2835_data;
  42616. +
  42617. +/* Thermal Device Operations */
  42618. +static struct thermal_zone_device_ops ops;
  42619. +
  42620. +/* --- FUNCTIONS --- */
  42621. +
  42622. +static int bcm2835_get_temp_or_max(struct thermal_zone_device *thermal_dev, unsigned long *temp, unsigned tag_id)
  42623. +{
  42624. + int result = -1, retry = 3;
  42625. + print_debug("IN");
  42626. +
  42627. + *temp = 0;
  42628. + while (result != 0 && retry-- > 0) {
  42629. + /* wipe all previous message data */
  42630. + memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
  42631. +
  42632. + /* prepare message */
  42633. + bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
  42634. + bcm2835_data.msg.tag.buffer_size = 8;
  42635. + bcm2835_data.msg.tag.tag_id = tag_id;
  42636. +
  42637. + /* send the message */
  42638. + result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
  42639. + print_debug("Got %stemperature as %u (%d,%x)\n", tag_id==VC_TAG_GET_MAX_TEMP ? "max ":"", (uint)bcm2835_data.msg.tag.val, result, bcm2835_data.msg.request_code);
  42640. + if (!(bcm2835_data.msg.request_code & 0x80000000))
  42641. + result = -1;
  42642. + }
  42643. +
  42644. + /* check if it was all ok and return the rate in milli degrees C */
  42645. + if (result == 0)
  42646. + *temp = (uint)bcm2835_data.msg.tag.val;
  42647. + else
  42648. + print_err("Failed to get temperature! (%x:%d)\n", tag_id, result);
  42649. + print_debug("OUT");
  42650. + return result;
  42651. +}
  42652. +
  42653. +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
  42654. +{
  42655. + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_TEMP);
  42656. +}
  42657. +
  42658. +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
  42659. +{
  42660. + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_MAX_TEMP);
  42661. +}
  42662. +
  42663. +static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
  42664. +{
  42665. + *trip_type = THERMAL_TRIP_HOT;
  42666. + return 0;
  42667. +}
  42668. +
  42669. +
  42670. +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
  42671. +{
  42672. + *dev_mode = THERMAL_DEVICE_ENABLED;
  42673. + return 0;
  42674. +}
  42675. +
  42676. +
  42677. +static int bcm2835_thermal_probe(struct platform_device *pdev)
  42678. +{
  42679. + print_debug("IN");
  42680. + print_debug("THERMAL Driver has been probed!");
  42681. +
  42682. + /* check that the device isn't null!*/
  42683. + if(pdev == NULL)
  42684. + {
  42685. + print_debug("Platform device is empty!");
  42686. + return -ENODEV;
  42687. + }
  42688. +
  42689. + if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal", 1, 0, NULL, &ops, NULL, 0, 0)))
  42690. + {
  42691. + print_debug("Unable to register the thermal device!");
  42692. + return -EFAULT;
  42693. + }
  42694. + return 0;
  42695. +}
  42696. +
  42697. +
  42698. +static int bcm2835_thermal_remove(struct platform_device *pdev)
  42699. +{
  42700. + print_debug("IN");
  42701. +
  42702. + thermal_zone_device_unregister(bcm2835_data.thermal_dev);
  42703. +
  42704. + print_debug("OUT");
  42705. +
  42706. + return 0;
  42707. +}
  42708. +
  42709. +static struct thermal_zone_device_ops ops = {
  42710. + .get_temp = bcm2835_get_temp,
  42711. + .get_trip_temp = bcm2835_get_max_temp,
  42712. + .get_trip_type = bcm2835_get_trip_type,
  42713. + .get_mode = bcm2835_get_mode,
  42714. +};
  42715. +
  42716. +/* Thermal Driver */
  42717. +static struct platform_driver bcm2835_thermal_driver = {
  42718. + .probe = bcm2835_thermal_probe,
  42719. + .remove = bcm2835_thermal_remove,
  42720. + .driver = {
  42721. + .name = "bcm2835_thermal",
  42722. + .owner = THIS_MODULE,
  42723. + },
  42724. +};
  42725. +
  42726. +MODULE_LICENSE("GPL");
  42727. +MODULE_AUTHOR("Dorian Peake");
  42728. +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
  42729. +
  42730. +module_platform_driver(bcm2835_thermal_driver);
  42731. diff -Nur linux-3.12.33/drivers/thermal/Kconfig linux-3.12.33-rpi/drivers/thermal/Kconfig
  42732. --- linux-3.12.33/drivers/thermal/Kconfig 2014-11-15 06:28:07.000000000 -0600
  42733. +++ linux-3.12.33-rpi/drivers/thermal/Kconfig 2014-12-03 19:13:40.136418001 -0600
  42734. @@ -181,6 +181,12 @@
  42735. enforce idle time which results in more package C-state residency. The
  42736. user interface is exposed via generic thermal framework.
  42737. +config THERMAL_BCM2835
  42738. + tristate "BCM2835 Thermal Driver"
  42739. + help
  42740. + This will enable temperature monitoring for the Broadcom BCM2835
  42741. + chip. If built as a module, it will be called 'bcm2835-thermal'.
  42742. +
  42743. config X86_PKG_TEMP_THERMAL
  42744. tristate "X86 package temperature thermal driver"
  42745. depends on X86_THERMAL_VECTOR
  42746. diff -Nur linux-3.12.33/drivers/thermal/Makefile linux-3.12.33-rpi/drivers/thermal/Makefile
  42747. --- linux-3.12.33/drivers/thermal/Makefile 2014-11-15 06:28:07.000000000 -0600
  42748. +++ linux-3.12.33-rpi/drivers/thermal/Makefile 2014-12-03 19:13:40.136418001 -0600
  42749. @@ -27,5 +27,6 @@
  42750. obj-$(CONFIG_IMX_THERMAL) += imx_thermal.o
  42751. obj-$(CONFIG_DB8500_CPUFREQ_COOLING) += db8500_cpufreq_cooling.o
  42752. obj-$(CONFIG_INTEL_POWERCLAMP) += intel_powerclamp.o
  42753. +obj-$(CONFIG_THERMAL_BCM2835) += bcm2835-thermal.o
  42754. obj-$(CONFIG_X86_PKG_TEMP_THERMAL) += x86_pkg_temp_thermal.o
  42755. obj-$(CONFIG_TI_SOC_THERMAL) += ti-soc-thermal/
  42756. diff -Nur linux-3.12.33/drivers/tty/serial/amba-pl011.c linux-3.12.33-rpi/drivers/tty/serial/amba-pl011.c
  42757. --- linux-3.12.33/drivers/tty/serial/amba-pl011.c 2014-11-15 06:28:07.000000000 -0600
  42758. +++ linux-3.12.33-rpi/drivers/tty/serial/amba-pl011.c 2014-12-03 19:13:40.152418001 -0600
  42759. @@ -84,7 +84,7 @@
  42760. static unsigned int get_fifosize_arm(struct amba_device *dev)
  42761. {
  42762. - return amba_rev(dev) < 3 ? 16 : 32;
  42763. + return 16; //TODO: fix: amba_rev(dev) < 3 ? 16 : 32;
  42764. }
  42765. static struct vendor_data vendor_arm = {
  42766. diff -Nur linux-3.12.33/drivers/usb/core/generic.c linux-3.12.33-rpi/drivers/usb/core/generic.c
  42767. --- linux-3.12.33/drivers/usb/core/generic.c 2014-11-15 06:28:07.000000000 -0600
  42768. +++ linux-3.12.33-rpi/drivers/usb/core/generic.c 2014-12-03 19:13:40.184418001 -0600
  42769. @@ -152,6 +152,7 @@
  42770. dev_warn(&udev->dev,
  42771. "no configuration chosen from %d choice%s\n",
  42772. num_configs, plural(num_configs));
  42773. + dev_warn(&udev->dev, "No support over %dmA\n", udev->bus_mA);
  42774. }
  42775. return i;
  42776. }
  42777. diff -Nur linux-3.12.33/drivers/usb/core/hub.c linux-3.12.33-rpi/drivers/usb/core/hub.c
  42778. --- linux-3.12.33/drivers/usb/core/hub.c 2014-11-15 06:28:07.000000000 -0600
  42779. +++ linux-3.12.33-rpi/drivers/usb/core/hub.c 2014-12-03 19:13:40.184418001 -0600
  42780. @@ -4845,7 +4845,7 @@
  42781. u16 status = 0;
  42782. u16 unused;
  42783. - dev_dbg(hub_dev, "over-current change on port "
  42784. + dev_notice(hub_dev, "over-current change on port "
  42785. "%d\n", i);
  42786. usb_clear_port_feature(hdev, i,
  42787. USB_PORT_FEAT_C_OVER_CURRENT);
  42788. diff -Nur linux-3.12.33/drivers/usb/core/message.c linux-3.12.33-rpi/drivers/usb/core/message.c
  42789. --- linux-3.12.33/drivers/usb/core/message.c 2014-11-15 06:28:07.000000000 -0600
  42790. +++ linux-3.12.33-rpi/drivers/usb/core/message.c 2014-12-03 19:13:40.188418001 -0600
  42791. @@ -1885,6 +1885,85 @@
  42792. if (cp->string == NULL &&
  42793. !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
  42794. cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
  42795. +/* Uncomment this define to enable the HS Electrical Test support */
  42796. +#define DWC_HS_ELECT_TST 1
  42797. +#ifdef DWC_HS_ELECT_TST
  42798. + /* Here we implement the HS Electrical Test support. The
  42799. + * tester uses a vendor ID of 0x1A0A to indicate we should
  42800. + * run a special test sequence. The product ID tells us
  42801. + * which sequence to run. We invoke the test sequence by
  42802. + * sending a non-standard SetFeature command to our root
  42803. + * hub port. Our dwc_otg_hcd_hub_control() routine will
  42804. + * recognize the command and perform the desired test
  42805. + * sequence.
  42806. + */
  42807. + if (dev->descriptor.idVendor == 0x1A0A) {
  42808. + /* HSOTG Electrical Test */
  42809. + dev_warn(&dev->dev, "VID from HSOTG Electrical Test Fixture\n");
  42810. +
  42811. + if (dev->bus && dev->bus->root_hub) {
  42812. + struct usb_device *hdev = dev->bus->root_hub;
  42813. + dev_warn(&dev->dev, "Got PID 0x%x\n", dev->descriptor.idProduct);
  42814. +
  42815. + switch (dev->descriptor.idProduct) {
  42816. + case 0x0101: /* TEST_SE0_NAK */
  42817. + dev_warn(&dev->dev, "TEST_SE0_NAK\n");
  42818. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  42819. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  42820. + USB_PORT_FEAT_TEST, 0x300, NULL, 0, HZ);
  42821. + break;
  42822. +
  42823. + case 0x0102: /* TEST_J */
  42824. + dev_warn(&dev->dev, "TEST_J\n");
  42825. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  42826. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  42827. + USB_PORT_FEAT_TEST, 0x100, NULL, 0, HZ);
  42828. + break;
  42829. +
  42830. + case 0x0103: /* TEST_K */
  42831. + dev_warn(&dev->dev, "TEST_K\n");
  42832. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  42833. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  42834. + USB_PORT_FEAT_TEST, 0x200, NULL, 0, HZ);
  42835. + break;
  42836. +
  42837. + case 0x0104: /* TEST_PACKET */
  42838. + dev_warn(&dev->dev, "TEST_PACKET\n");
  42839. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  42840. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  42841. + USB_PORT_FEAT_TEST, 0x400, NULL, 0, HZ);
  42842. + break;
  42843. +
  42844. + case 0x0105: /* TEST_FORCE_ENABLE */
  42845. + dev_warn(&dev->dev, "TEST_FORCE_ENABLE\n");
  42846. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  42847. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  42848. + USB_PORT_FEAT_TEST, 0x500, NULL, 0, HZ);
  42849. + break;
  42850. +
  42851. + case 0x0106: /* HS_HOST_PORT_SUSPEND_RESUME */
  42852. + dev_warn(&dev->dev, "HS_HOST_PORT_SUSPEND_RESUME\n");
  42853. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  42854. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  42855. + USB_PORT_FEAT_TEST, 0x600, NULL, 0, 40 * HZ);
  42856. + break;
  42857. +
  42858. + case 0x0107: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  42859. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup\n");
  42860. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  42861. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  42862. + USB_PORT_FEAT_TEST, 0x700, NULL, 0, 40 * HZ);
  42863. + break;
  42864. +
  42865. + case 0x0108: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  42866. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute\n");
  42867. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  42868. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  42869. + USB_PORT_FEAT_TEST, 0x800, NULL, 0, 40 * HZ);
  42870. + }
  42871. + }
  42872. + }
  42873. +#endif /* DWC_HS_ELECT_TST */
  42874. /* Now that the interfaces are installed, re-enable LPM. */
  42875. usb_unlocked_enable_lpm(dev);
  42876. diff -Nur linux-3.12.33/drivers/usb/core/otg_whitelist.h linux-3.12.33-rpi/drivers/usb/core/otg_whitelist.h
  42877. --- linux-3.12.33/drivers/usb/core/otg_whitelist.h 2014-11-15 06:28:07.000000000 -0600
  42878. +++ linux-3.12.33-rpi/drivers/usb/core/otg_whitelist.h 2014-12-03 19:13:40.188418001 -0600
  42879. @@ -19,33 +19,82 @@
  42880. static struct usb_device_id whitelist_table [] = {
  42881. /* hubs are optional in OTG, but very handy ... */
  42882. +#define CERT_WITHOUT_HUBS
  42883. +#if defined(CERT_WITHOUT_HUBS)
  42884. +{ USB_DEVICE( 0x0000, 0x0000 ), }, /* Root HUB Only*/
  42885. +#else
  42886. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 0), },
  42887. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 1), },
  42888. +{ USB_DEVICE_INFO(USB_CLASS_HUB, 0, 2), },
  42889. +#endif
  42890. #ifdef CONFIG_USB_PRINTER /* ignoring nonstatic linkage! */
  42891. /* FIXME actually, printers are NOT supposed to use device classes;
  42892. * they're supposed to use interface classes...
  42893. */
  42894. -{ USB_DEVICE_INFO(7, 1, 1) },
  42895. -{ USB_DEVICE_INFO(7, 1, 2) },
  42896. -{ USB_DEVICE_INFO(7, 1, 3) },
  42897. +//{ USB_DEVICE_INFO(7, 1, 1) },
  42898. +//{ USB_DEVICE_INFO(7, 1, 2) },
  42899. +//{ USB_DEVICE_INFO(7, 1, 3) },
  42900. #endif
  42901. #ifdef CONFIG_USB_NET_CDCETHER
  42902. /* Linux-USB CDC Ethernet gadget */
  42903. -{ USB_DEVICE(0x0525, 0xa4a1), },
  42904. +//{ USB_DEVICE(0x0525, 0xa4a1), },
  42905. /* Linux-USB CDC Ethernet + RNDIS gadget */
  42906. -{ USB_DEVICE(0x0525, 0xa4a2), },
  42907. +//{ USB_DEVICE(0x0525, 0xa4a2), },
  42908. #endif
  42909. #if defined(CONFIG_USB_TEST) || defined(CONFIG_USB_TEST_MODULE)
  42910. /* gadget zero, for testing */
  42911. -{ USB_DEVICE(0x0525, 0xa4a0), },
  42912. +//{ USB_DEVICE(0x0525, 0xa4a0), },
  42913. #endif
  42914. +/* OPT Tester */
  42915. +{ USB_DEVICE( 0x1a0a, 0x0101 ), }, /* TEST_SE0_NAK */
  42916. +{ USB_DEVICE( 0x1a0a, 0x0102 ), }, /* Test_J */
  42917. +{ USB_DEVICE( 0x1a0a, 0x0103 ), }, /* Test_K */
  42918. +{ USB_DEVICE( 0x1a0a, 0x0104 ), }, /* Test_PACKET */
  42919. +{ USB_DEVICE( 0x1a0a, 0x0105 ), }, /* Test_FORCE_ENABLE */
  42920. +{ USB_DEVICE( 0x1a0a, 0x0106 ), }, /* HS_PORT_SUSPEND_RESUME */
  42921. +{ USB_DEVICE( 0x1a0a, 0x0107 ), }, /* SINGLE_STEP_GET_DESCRIPTOR setup */
  42922. +{ USB_DEVICE( 0x1a0a, 0x0108 ), }, /* SINGLE_STEP_GET_DESCRIPTOR execute */
  42923. +
  42924. +/* Sony cameras */
  42925. +{ USB_DEVICE_VER(0x054c,0x0010,0x0410, 0x0500), },
  42926. +
  42927. +/* Memory Devices */
  42928. +//{ USB_DEVICE( 0x0781, 0x5150 ), }, /* SanDisk */
  42929. +//{ USB_DEVICE( 0x05DC, 0x0080 ), }, /* Lexar */
  42930. +//{ USB_DEVICE( 0x4146, 0x9281 ), }, /* IOMEGA */
  42931. +//{ USB_DEVICE( 0x067b, 0x2507 ), }, /* Hammer 20GB External HD */
  42932. +{ USB_DEVICE( 0x0EA0, 0x2168 ), }, /* Ours Technology Inc. (BUFFALO ClipDrive)*/
  42933. +//{ USB_DEVICE( 0x0457, 0x0150 ), }, /* Silicon Integrated Systems Corp. */
  42934. +
  42935. +/* HP Printers */
  42936. +//{ USB_DEVICE( 0x03F0, 0x1102 ), }, /* HP Photosmart 245 */
  42937. +//{ USB_DEVICE( 0x03F0, 0x1302 ), }, /* HP Photosmart 370 Series */
  42938. +
  42939. +/* Speakers */
  42940. +//{ USB_DEVICE( 0x0499, 0x3002 ), }, /* YAMAHA YST-MS35D USB Speakers */
  42941. +//{ USB_DEVICE( 0x0672, 0x1041 ), }, /* Labtec USB Headset */
  42942. +
  42943. { } /* Terminating entry */
  42944. };
  42945. +static inline void report_errors(struct usb_device *dev)
  42946. +{
  42947. + /* OTG MESSAGE: report errors here, customize to match your product */
  42948. + dev_info(&dev->dev, "device Vendor:%04x Product:%04x is not supported\n",
  42949. + le16_to_cpu(dev->descriptor.idVendor),
  42950. + le16_to_cpu(dev->descriptor.idProduct));
  42951. + if (USB_CLASS_HUB == dev->descriptor.bDeviceClass){
  42952. + dev_printk(KERN_CRIT, &dev->dev, "Unsupported Hub Topology\n");
  42953. + } else {
  42954. + dev_printk(KERN_CRIT, &dev->dev, "Attached Device is not Supported\n");
  42955. + }
  42956. +}
  42957. +
  42958. +
  42959. static int is_targeted(struct usb_device *dev)
  42960. {
  42961. struct usb_device_id *id = whitelist_table;
  42962. @@ -55,58 +104,83 @@
  42963. return 1;
  42964. /* HNP test device is _never_ targeted (see OTG spec 6.6.6) */
  42965. - if ((le16_to_cpu(dev->descriptor.idVendor) == 0x1a0a &&
  42966. - le16_to_cpu(dev->descriptor.idProduct) == 0xbadd))
  42967. - return 0;
  42968. + if (dev->descriptor.idVendor == 0x1a0a &&
  42969. + dev->descriptor.idProduct == 0xbadd) {
  42970. + return 0;
  42971. + } else if (!enable_whitelist) {
  42972. + return 1;
  42973. + } else {
  42974. - /* NOTE: can't use usb_match_id() since interface caches
  42975. - * aren't set up yet. this is cut/paste from that code.
  42976. - */
  42977. - for (id = whitelist_table; id->match_flags; id++) {
  42978. - if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  42979. - id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  42980. - continue;
  42981. -
  42982. - if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  42983. - id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  42984. - continue;
  42985. -
  42986. - /* No need to test id->bcdDevice_lo != 0, since 0 is never
  42987. - greater than any unsigned number. */
  42988. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  42989. - (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  42990. - continue;
  42991. -
  42992. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  42993. - (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  42994. - continue;
  42995. -
  42996. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  42997. - (id->bDeviceClass != dev->descriptor.bDeviceClass))
  42998. - continue;
  42999. -
  43000. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  43001. - (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  43002. - continue;
  43003. -
  43004. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  43005. - (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  43006. - continue;
  43007. +#ifdef DEBUG
  43008. + dev_dbg(&dev->dev, "device V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
  43009. + dev->descriptor.idVendor,
  43010. + dev->descriptor.idProduct,
  43011. + dev->descriptor.bDeviceClass,
  43012. + dev->descriptor.bDeviceSubClass,
  43013. + dev->descriptor.bDeviceProtocol);
  43014. +#endif
  43015. return 1;
  43016. + /* NOTE: can't use usb_match_id() since interface caches
  43017. + * aren't set up yet. this is cut/paste from that code.
  43018. + */
  43019. + for (id = whitelist_table; id->match_flags; id++) {
  43020. +#ifdef DEBUG
  43021. + dev_dbg(&dev->dev,
  43022. + "ID: V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
  43023. + id->idVendor,
  43024. + id->idProduct,
  43025. + id->bDeviceClass,
  43026. + id->bDeviceSubClass,
  43027. + id->bDeviceProtocol);
  43028. +#endif
  43029. +
  43030. + if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  43031. + id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  43032. + continue;
  43033. +
  43034. + if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  43035. + id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  43036. + continue;
  43037. +
  43038. + /* No need to test id->bcdDevice_lo != 0, since 0 is never
  43039. + greater than any unsigned number. */
  43040. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  43041. + (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  43042. + continue;
  43043. +
  43044. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  43045. + (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  43046. + continue;
  43047. +
  43048. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  43049. + (id->bDeviceClass != dev->descriptor.bDeviceClass))
  43050. + continue;
  43051. +
  43052. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  43053. + (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  43054. + continue;
  43055. +
  43056. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  43057. + (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  43058. + continue;
  43059. +
  43060. + return 1;
  43061. + }
  43062. }
  43063. /* add other match criteria here ... */
  43064. -
  43065. - /* OTG MESSAGE: report errors here, customize to match your product */
  43066. - dev_err(&dev->dev, "device v%04x p%04x is not supported\n",
  43067. - le16_to_cpu(dev->descriptor.idVendor),
  43068. - le16_to_cpu(dev->descriptor.idProduct));
  43069. #ifdef CONFIG_USB_OTG_WHITELIST
  43070. + report_errors(dev);
  43071. return 0;
  43072. #else
  43073. - return 1;
  43074. + if (enable_whitelist) {
  43075. + report_errors(dev);
  43076. + return 0;
  43077. + } else {
  43078. + return 1;
  43079. + }
  43080. #endif
  43081. }
  43082. diff -Nur linux-3.12.33/drivers/usb/gadget/file_storage.c linux-3.12.33-rpi/drivers/usb/gadget/file_storage.c
  43083. --- linux-3.12.33/drivers/usb/gadget/file_storage.c 1969-12-31 18:00:00.000000000 -0600
  43084. +++ linux-3.12.33-rpi/drivers/usb/gadget/file_storage.c 2014-12-03 19:13:40.200418001 -0600
  43085. @@ -0,0 +1,3676 @@
  43086. +/*
  43087. + * file_storage.c -- File-backed USB Storage Gadget, for USB development
  43088. + *
  43089. + * Copyright (C) 2003-2008 Alan Stern
  43090. + * All rights reserved.
  43091. + *
  43092. + * Redistribution and use in source and binary forms, with or without
  43093. + * modification, are permitted provided that the following conditions
  43094. + * are met:
  43095. + * 1. Redistributions of source code must retain the above copyright
  43096. + * notice, this list of conditions, and the following disclaimer,
  43097. + * without modification.
  43098. + * 2. Redistributions in binary form must reproduce the above copyright
  43099. + * notice, this list of conditions and the following disclaimer in the
  43100. + * documentation and/or other materials provided with the distribution.
  43101. + * 3. The names of the above-listed copyright holders may not be used
  43102. + * to endorse or promote products derived from this software without
  43103. + * specific prior written permission.
  43104. + *
  43105. + * ALTERNATIVELY, this software may be distributed under the terms of the
  43106. + * GNU General Public License ("GPL") as published by the Free Software
  43107. + * Foundation, either version 2 of that License or (at your option) any
  43108. + * later version.
  43109. + *
  43110. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  43111. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  43112. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43113. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  43114. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  43115. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  43116. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  43117. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43118. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  43119. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  43120. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43121. + */
  43122. +
  43123. +
  43124. +/*
  43125. + * The File-backed Storage Gadget acts as a USB Mass Storage device,
  43126. + * appearing to the host as a disk drive or as a CD-ROM drive. In addition
  43127. + * to providing an example of a genuinely useful gadget driver for a USB
  43128. + * device, it also illustrates a technique of double-buffering for increased
  43129. + * throughput. Last but not least, it gives an easy way to probe the
  43130. + * behavior of the Mass Storage drivers in a USB host.
  43131. + *
  43132. + * Backing storage is provided by a regular file or a block device, specified
  43133. + * by the "file" module parameter. Access can be limited to read-only by
  43134. + * setting the optional "ro" module parameter. (For CD-ROM emulation,
  43135. + * access is always read-only.) The gadget will indicate that it has
  43136. + * removable media if the optional "removable" module parameter is set.
  43137. + *
  43138. + * The gadget supports the Control-Bulk (CB), Control-Bulk-Interrupt (CBI),
  43139. + * and Bulk-Only (also known as Bulk-Bulk-Bulk or BBB) transports, selected
  43140. + * by the optional "transport" module parameter. It also supports the
  43141. + * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
  43142. + * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
  43143. + * the optional "protocol" module parameter. In addition, the default
  43144. + * Vendor ID, Product ID, release number and serial number can be overridden.
  43145. + *
  43146. + * There is support for multiple logical units (LUNs), each of which has
  43147. + * its own backing file. The number of LUNs can be set using the optional
  43148. + * "luns" module parameter (anywhere from 1 to 8), and the corresponding
  43149. + * files are specified using comma-separated lists for "file" and "ro".
  43150. + * The default number of LUNs is taken from the number of "file" elements;
  43151. + * it is 1 if "file" is not given. If "removable" is not set then a backing
  43152. + * file must be specified for each LUN. If it is set, then an unspecified
  43153. + * or empty backing filename means the LUN's medium is not loaded. Ideally
  43154. + * each LUN would be settable independently as a disk drive or a CD-ROM
  43155. + * drive, but currently all LUNs have to be the same type. The CD-ROM
  43156. + * emulation includes a single data track and no audio tracks; hence there
  43157. + * need be only one backing file per LUN.
  43158. + *
  43159. + * Requirements are modest; only a bulk-in and a bulk-out endpoint are
  43160. + * needed (an interrupt-out endpoint is also needed for CBI). The memory
  43161. + * requirement amounts to two 16K buffers, size configurable by a parameter.
  43162. + * Support is included for both full-speed and high-speed operation.
  43163. + *
  43164. + * Note that the driver is slightly non-portable in that it assumes a
  43165. + * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
  43166. + * interrupt-in endpoints. With most device controllers this isn't an
  43167. + * issue, but there may be some with hardware restrictions that prevent
  43168. + * a buffer from being used by more than one endpoint.
  43169. + *
  43170. + * Module options:
  43171. + *
  43172. + * file=filename[,filename...]
  43173. + * Required if "removable" is not set, names of
  43174. + * the files or block devices used for
  43175. + * backing storage
  43176. + * serial=HHHH... Required serial number (string of hex chars)
  43177. + * ro=b[,b...] Default false, booleans for read-only access
  43178. + * removable Default false, boolean for removable media
  43179. + * luns=N Default N = number of filenames, number of
  43180. + * LUNs to support
  43181. + * nofua=b[,b...] Default false, booleans for ignore FUA flag
  43182. + * in SCSI WRITE(10,12) commands
  43183. + * stall Default determined according to the type of
  43184. + * USB device controller (usually true),
  43185. + * boolean to permit the driver to halt
  43186. + * bulk endpoints
  43187. + * cdrom Default false, boolean for whether to emulate
  43188. + * a CD-ROM drive
  43189. + * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
  43190. + * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
  43191. + * ATAPI, QIC, UFI, 8070, or SCSI;
  43192. + * also 1 - 6)
  43193. + * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
  43194. + * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
  43195. + * release=0xRRRR Override the USB release number (bcdDevice)
  43196. + * buflen=N Default N=16384, buffer size used (will be
  43197. + * rounded down to a multiple of
  43198. + * PAGE_CACHE_SIZE)
  43199. + *
  43200. + * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro",
  43201. + * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
  43202. + * default values are used for everything else.
  43203. + *
  43204. + * The pathnames of the backing files and the ro settings are available in
  43205. + * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of
  43206. + * the gadget's sysfs directory. If the "removable" option is set, writing to
  43207. + * these files will simulate ejecting/loading the medium (writing an empty
  43208. + * line means eject) and adjusting a write-enable tab. Changes to the ro
  43209. + * setting are not allowed when the medium is loaded or if CD-ROM emulation
  43210. + * is being used.
  43211. + *
  43212. + * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
  43213. + * The driver's SCSI command interface was based on the "Information
  43214. + * technology - Small Computer System Interface - 2" document from
  43215. + * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
  43216. + * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception
  43217. + * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
  43218. + * "Universal Serial Bus Mass Storage Class UFI Command Specification"
  43219. + * document, Revision 1.0, December 14, 1998, available at
  43220. + * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
  43221. + */
  43222. +
  43223. +
  43224. +/*
  43225. + * Driver Design
  43226. + *
  43227. + * The FSG driver is fairly straightforward. There is a main kernel
  43228. + * thread that handles most of the work. Interrupt routines field
  43229. + * callbacks from the controller driver: bulk- and interrupt-request
  43230. + * completion notifications, endpoint-0 events, and disconnect events.
  43231. + * Completion events are passed to the main thread by wakeup calls. Many
  43232. + * ep0 requests are handled at interrupt time, but SetInterface,
  43233. + * SetConfiguration, and device reset requests are forwarded to the
  43234. + * thread in the form of "exceptions" using SIGUSR1 signals (since they
  43235. + * should interrupt any ongoing file I/O operations).
  43236. + *
  43237. + * The thread's main routine implements the standard command/data/status
  43238. + * parts of a SCSI interaction. It and its subroutines are full of tests
  43239. + * for pending signals/exceptions -- all this polling is necessary since
  43240. + * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
  43241. + * indication that the driver really wants to be running in userspace.)
  43242. + * An important point is that so long as the thread is alive it keeps an
  43243. + * open reference to the backing file. This will prevent unmounting
  43244. + * the backing file's underlying filesystem and could cause problems
  43245. + * during system shutdown, for example. To prevent such problems, the
  43246. + * thread catches INT, TERM, and KILL signals and converts them into
  43247. + * an EXIT exception.
  43248. + *
  43249. + * In normal operation the main thread is started during the gadget's
  43250. + * fsg_bind() callback and stopped during fsg_unbind(). But it can also
  43251. + * exit when it receives a signal, and there's no point leaving the
  43252. + * gadget running when the thread is dead. So just before the thread
  43253. + * exits, it deregisters the gadget driver. This makes things a little
  43254. + * tricky: The driver is deregistered at two places, and the exiting
  43255. + * thread can indirectly call fsg_unbind() which in turn can tell the
  43256. + * thread to exit. The first problem is resolved through the use of the
  43257. + * REGISTERED atomic bitflag; the driver will only be deregistered once.
  43258. + * The second problem is resolved by having fsg_unbind() check
  43259. + * fsg->state; it won't try to stop the thread if the state is already
  43260. + * FSG_STATE_TERMINATED.
  43261. + *
  43262. + * To provide maximum throughput, the driver uses a circular pipeline of
  43263. + * buffer heads (struct fsg_buffhd). In principle the pipeline can be
  43264. + * arbitrarily long; in practice the benefits don't justify having more
  43265. + * than 2 stages (i.e., double buffering). But it helps to think of the
  43266. + * pipeline as being a long one. Each buffer head contains a bulk-in and
  43267. + * a bulk-out request pointer (since the buffer can be used for both
  43268. + * output and input -- directions always are given from the host's
  43269. + * point of view) as well as a pointer to the buffer and various state
  43270. + * variables.
  43271. + *
  43272. + * Use of the pipeline follows a simple protocol. There is a variable
  43273. + * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
  43274. + * At any time that buffer head may still be in use from an earlier
  43275. + * request, so each buffer head has a state variable indicating whether
  43276. + * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
  43277. + * buffer head to be EMPTY, filling the buffer either by file I/O or by
  43278. + * USB I/O (during which the buffer head is BUSY), and marking the buffer
  43279. + * head FULL when the I/O is complete. Then the buffer will be emptied
  43280. + * (again possibly by USB I/O, during which it is marked BUSY) and
  43281. + * finally marked EMPTY again (possibly by a completion routine).
  43282. + *
  43283. + * A module parameter tells the driver to avoid stalling the bulk
  43284. + * endpoints wherever the transport specification allows. This is
  43285. + * necessary for some UDCs like the SuperH, which cannot reliably clear a
  43286. + * halt on a bulk endpoint. However, under certain circumstances the
  43287. + * Bulk-only specification requires a stall. In such cases the driver
  43288. + * will halt the endpoint and set a flag indicating that it should clear
  43289. + * the halt in software during the next device reset. Hopefully this
  43290. + * will permit everything to work correctly. Furthermore, although the
  43291. + * specification allows the bulk-out endpoint to halt when the host sends
  43292. + * too much data, implementing this would cause an unavoidable race.
  43293. + * The driver will always use the "no-stall" approach for OUT transfers.
  43294. + *
  43295. + * One subtle point concerns sending status-stage responses for ep0
  43296. + * requests. Some of these requests, such as device reset, can involve
  43297. + * interrupting an ongoing file I/O operation, which might take an
  43298. + * arbitrarily long time. During that delay the host might give up on
  43299. + * the original ep0 request and issue a new one. When that happens the
  43300. + * driver should not notify the host about completion of the original
  43301. + * request, as the host will no longer be waiting for it. So the driver
  43302. + * assigns to each ep0 request a unique tag, and it keeps track of the
  43303. + * tag value of the request associated with a long-running exception
  43304. + * (device-reset, interface-change, or configuration-change). When the
  43305. + * exception handler is finished, the status-stage response is submitted
  43306. + * only if the current ep0 request tag is equal to the exception request
  43307. + * tag. Thus only the most recently received ep0 request will get a
  43308. + * status-stage response.
  43309. + *
  43310. + * Warning: This driver source file is too long. It ought to be split up
  43311. + * into a header file plus about 3 separate .c files, to handle the details
  43312. + * of the Gadget, USB Mass Storage, and SCSI protocols.
  43313. + */
  43314. +
  43315. +
  43316. +/* #define VERBOSE_DEBUG */
  43317. +/* #define DUMP_MSGS */
  43318. +
  43319. +
  43320. +#include <linux/blkdev.h>
  43321. +#include <linux/completion.h>
  43322. +#include <linux/dcache.h>
  43323. +#include <linux/delay.h>
  43324. +#include <linux/device.h>
  43325. +#include <linux/fcntl.h>
  43326. +#include <linux/file.h>
  43327. +#include <linux/fs.h>
  43328. +#include <linux/kref.h>
  43329. +#include <linux/kthread.h>
  43330. +#include <linux/limits.h>
  43331. +#include <linux/module.h>
  43332. +#include <linux/rwsem.h>
  43333. +#include <linux/slab.h>
  43334. +#include <linux/spinlock.h>
  43335. +#include <linux/string.h>
  43336. +#include <linux/freezer.h>
  43337. +#include <linux/utsname.h>
  43338. +
  43339. +#include <linux/usb/ch9.h>
  43340. +#include <linux/usb/gadget.h>
  43341. +
  43342. +#include "gadget_chips.h"
  43343. +
  43344. +
  43345. +
  43346. +/*
  43347. + * Kbuild is not very cooperative with respect to linking separately
  43348. + * compiled library objects into one module. So for now we won't use
  43349. + * separate compilation ... ensuring init/exit sections work to shrink
  43350. + * the runtime footprint, and giving us at least some parts of what
  43351. + * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  43352. + */
  43353. +#include "usbstring.c"
  43354. +#include "config.c"
  43355. +#include "epautoconf.c"
  43356. +
  43357. +/*-------------------------------------------------------------------------*/
  43358. +
  43359. +#define DRIVER_DESC "File-backed Storage Gadget"
  43360. +#define DRIVER_NAME "g_file_storage"
  43361. +#define DRIVER_VERSION "1 September 2010"
  43362. +
  43363. +static char fsg_string_manufacturer[64];
  43364. +static const char fsg_string_product[] = DRIVER_DESC;
  43365. +static const char fsg_string_config[] = "Self-powered";
  43366. +static const char fsg_string_interface[] = "Mass Storage";
  43367. +
  43368. +
  43369. +#include "storage_common.c"
  43370. +
  43371. +
  43372. +MODULE_DESCRIPTION(DRIVER_DESC);
  43373. +MODULE_AUTHOR("Alan Stern");
  43374. +MODULE_LICENSE("Dual BSD/GPL");
  43375. +
  43376. +/*
  43377. + * This driver assumes self-powered hardware and has no way for users to
  43378. + * trigger remote wakeup. It uses autoconfiguration to select endpoints
  43379. + * and endpoint addresses.
  43380. + */
  43381. +
  43382. +
  43383. +/*-------------------------------------------------------------------------*/
  43384. +
  43385. +
  43386. +/* Encapsulate the module parameter settings */
  43387. +
  43388. +static struct {
  43389. + char *file[FSG_MAX_LUNS];
  43390. + char *serial;
  43391. + bool ro[FSG_MAX_LUNS];
  43392. + bool nofua[FSG_MAX_LUNS];
  43393. + unsigned int num_filenames;
  43394. + unsigned int num_ros;
  43395. + unsigned int num_nofuas;
  43396. + unsigned int nluns;
  43397. +
  43398. + bool removable;
  43399. + bool can_stall;
  43400. + bool cdrom;
  43401. +
  43402. + char *transport_parm;
  43403. + char *protocol_parm;
  43404. + unsigned short vendor;
  43405. + unsigned short product;
  43406. + unsigned short release;
  43407. + unsigned int buflen;
  43408. +
  43409. + int transport_type;
  43410. + char *transport_name;
  43411. + int protocol_type;
  43412. + char *protocol_name;
  43413. +
  43414. +} mod_data = { // Default values
  43415. + .transport_parm = "BBB",
  43416. + .protocol_parm = "SCSI",
  43417. + .removable = 0,
  43418. + .can_stall = 1,
  43419. + .cdrom = 0,
  43420. + .vendor = FSG_VENDOR_ID,
  43421. + .product = FSG_PRODUCT_ID,
  43422. + .release = 0xffff, // Use controller chip type
  43423. + .buflen = 16384,
  43424. + };
  43425. +
  43426. +
  43427. +module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
  43428. + S_IRUGO);
  43429. +MODULE_PARM_DESC(file, "names of backing files or devices");
  43430. +
  43431. +module_param_named(serial, mod_data.serial, charp, S_IRUGO);
  43432. +MODULE_PARM_DESC(serial, "USB serial number");
  43433. +
  43434. +module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
  43435. +MODULE_PARM_DESC(ro, "true to force read-only");
  43436. +
  43437. +module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas,
  43438. + S_IRUGO);
  43439. +MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit");
  43440. +
  43441. +module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
  43442. +MODULE_PARM_DESC(luns, "number of LUNs");
  43443. +
  43444. +module_param_named(removable, mod_data.removable, bool, S_IRUGO);
  43445. +MODULE_PARM_DESC(removable, "true to simulate removable media");
  43446. +
  43447. +module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
  43448. +MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
  43449. +
  43450. +module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
  43451. +MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
  43452. +
  43453. +/* In the non-TEST version, only the module parameters listed above
  43454. + * are available. */
  43455. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  43456. +
  43457. +module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
  43458. +MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
  43459. +
  43460. +module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
  43461. +MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
  43462. + "8070, or SCSI)");
  43463. +
  43464. +module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
  43465. +MODULE_PARM_DESC(vendor, "USB Vendor ID");
  43466. +
  43467. +module_param_named(product, mod_data.product, ushort, S_IRUGO);
  43468. +MODULE_PARM_DESC(product, "USB Product ID");
  43469. +
  43470. +module_param_named(release, mod_data.release, ushort, S_IRUGO);
  43471. +MODULE_PARM_DESC(release, "USB release number");
  43472. +
  43473. +module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
  43474. +MODULE_PARM_DESC(buflen, "I/O buffer size");
  43475. +
  43476. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  43477. +
  43478. +
  43479. +/*
  43480. + * These definitions will permit the compiler to avoid generating code for
  43481. + * parts of the driver that aren't used in the non-TEST version. Even gcc
  43482. + * can recognize when a test of a constant expression yields a dead code
  43483. + * path.
  43484. + */
  43485. +
  43486. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  43487. +
  43488. +#define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
  43489. +#define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
  43490. +#define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
  43491. +
  43492. +#else
  43493. +
  43494. +#define transport_is_bbb() 1
  43495. +#define transport_is_cbi() 0
  43496. +#define protocol_is_scsi() 1
  43497. +
  43498. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  43499. +
  43500. +
  43501. +/*-------------------------------------------------------------------------*/
  43502. +
  43503. +
  43504. +struct fsg_dev {
  43505. + /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
  43506. + spinlock_t lock;
  43507. + struct usb_gadget *gadget;
  43508. +
  43509. + /* filesem protects: backing files in use */
  43510. + struct rw_semaphore filesem;
  43511. +
  43512. + /* reference counting: wait until all LUNs are released */
  43513. + struct kref ref;
  43514. +
  43515. + struct usb_ep *ep0; // Handy copy of gadget->ep0
  43516. + struct usb_request *ep0req; // For control responses
  43517. + unsigned int ep0_req_tag;
  43518. + const char *ep0req_name;
  43519. +
  43520. + struct usb_request *intreq; // For interrupt responses
  43521. + int intreq_busy;
  43522. + struct fsg_buffhd *intr_buffhd;
  43523. +
  43524. + unsigned int bulk_out_maxpacket;
  43525. + enum fsg_state state; // For exception handling
  43526. + unsigned int exception_req_tag;
  43527. +
  43528. + u8 config, new_config;
  43529. +
  43530. + unsigned int running : 1;
  43531. + unsigned int bulk_in_enabled : 1;
  43532. + unsigned int bulk_out_enabled : 1;
  43533. + unsigned int intr_in_enabled : 1;
  43534. + unsigned int phase_error : 1;
  43535. + unsigned int short_packet_received : 1;
  43536. + unsigned int bad_lun_okay : 1;
  43537. +
  43538. + unsigned long atomic_bitflags;
  43539. +#define REGISTERED 0
  43540. +#define IGNORE_BULK_OUT 1
  43541. +#define SUSPENDED 2
  43542. +
  43543. + struct usb_ep *bulk_in;
  43544. + struct usb_ep *bulk_out;
  43545. + struct usb_ep *intr_in;
  43546. +
  43547. + struct fsg_buffhd *next_buffhd_to_fill;
  43548. + struct fsg_buffhd *next_buffhd_to_drain;
  43549. +
  43550. + int thread_wakeup_needed;
  43551. + struct completion thread_notifier;
  43552. + struct task_struct *thread_task;
  43553. +
  43554. + int cmnd_size;
  43555. + u8 cmnd[MAX_COMMAND_SIZE];
  43556. + enum data_direction data_dir;
  43557. + u32 data_size;
  43558. + u32 data_size_from_cmnd;
  43559. + u32 tag;
  43560. + unsigned int lun;
  43561. + u32 residue;
  43562. + u32 usb_amount_left;
  43563. +
  43564. + /* The CB protocol offers no way for a host to know when a command
  43565. + * has completed. As a result the next command may arrive early,
  43566. + * and we will still have to handle it. For that reason we need
  43567. + * a buffer to store new commands when using CB (or CBI, which
  43568. + * does not oblige a host to wait for command completion either). */
  43569. + int cbbuf_cmnd_size;
  43570. + u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
  43571. +
  43572. + unsigned int nluns;
  43573. + struct fsg_lun *luns;
  43574. + struct fsg_lun *curlun;
  43575. + /* Must be the last entry */
  43576. + struct fsg_buffhd buffhds[];
  43577. +};
  43578. +
  43579. +typedef void (*fsg_routine_t)(struct fsg_dev *);
  43580. +
  43581. +static int exception_in_progress(struct fsg_dev *fsg)
  43582. +{
  43583. + return (fsg->state > FSG_STATE_IDLE);
  43584. +}
  43585. +
  43586. +/* Make bulk-out requests be divisible by the maxpacket size */
  43587. +static void set_bulk_out_req_length(struct fsg_dev *fsg,
  43588. + struct fsg_buffhd *bh, unsigned int length)
  43589. +{
  43590. + unsigned int rem;
  43591. +
  43592. + bh->bulk_out_intended_length = length;
  43593. + rem = length % fsg->bulk_out_maxpacket;
  43594. + if (rem > 0)
  43595. + length += fsg->bulk_out_maxpacket - rem;
  43596. + bh->outreq->length = length;
  43597. +}
  43598. +
  43599. +static struct fsg_dev *the_fsg;
  43600. +static struct usb_gadget_driver fsg_driver;
  43601. +
  43602. +
  43603. +/*-------------------------------------------------------------------------*/
  43604. +
  43605. +static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
  43606. +{
  43607. + const char *name;
  43608. +
  43609. + if (ep == fsg->bulk_in)
  43610. + name = "bulk-in";
  43611. + else if (ep == fsg->bulk_out)
  43612. + name = "bulk-out";
  43613. + else
  43614. + name = ep->name;
  43615. + DBG(fsg, "%s set halt\n", name);
  43616. + return usb_ep_set_halt(ep);
  43617. +}
  43618. +
  43619. +
  43620. +/*-------------------------------------------------------------------------*/
  43621. +
  43622. +/*
  43623. + * DESCRIPTORS ... most are static, but strings and (full) configuration
  43624. + * descriptors are built on demand. Also the (static) config and interface
  43625. + * descriptors are adjusted during fsg_bind().
  43626. + */
  43627. +
  43628. +/* There is only one configuration. */
  43629. +#define CONFIG_VALUE 1
  43630. +
  43631. +static struct usb_device_descriptor
  43632. +device_desc = {
  43633. + .bLength = sizeof device_desc,
  43634. + .bDescriptorType = USB_DT_DEVICE,
  43635. +
  43636. + .bcdUSB = cpu_to_le16(0x0200),
  43637. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  43638. +
  43639. + /* The next three values can be overridden by module parameters */
  43640. + .idVendor = cpu_to_le16(FSG_VENDOR_ID),
  43641. + .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
  43642. + .bcdDevice = cpu_to_le16(0xffff),
  43643. +
  43644. + .iManufacturer = FSG_STRING_MANUFACTURER,
  43645. + .iProduct = FSG_STRING_PRODUCT,
  43646. + .iSerialNumber = FSG_STRING_SERIAL,
  43647. + .bNumConfigurations = 1,
  43648. +};
  43649. +
  43650. +static struct usb_config_descriptor
  43651. +config_desc = {
  43652. + .bLength = sizeof config_desc,
  43653. + .bDescriptorType = USB_DT_CONFIG,
  43654. +
  43655. + /* wTotalLength computed by usb_gadget_config_buf() */
  43656. + .bNumInterfaces = 1,
  43657. + .bConfigurationValue = CONFIG_VALUE,
  43658. + .iConfiguration = FSG_STRING_CONFIG,
  43659. + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  43660. + .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
  43661. +};
  43662. +
  43663. +
  43664. +static struct usb_qualifier_descriptor
  43665. +dev_qualifier = {
  43666. + .bLength = sizeof dev_qualifier,
  43667. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  43668. +
  43669. + .bcdUSB = cpu_to_le16(0x0200),
  43670. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  43671. +
  43672. + .bNumConfigurations = 1,
  43673. +};
  43674. +
  43675. +static int populate_bos(struct fsg_dev *fsg, u8 *buf)
  43676. +{
  43677. + memcpy(buf, &fsg_bos_desc, USB_DT_BOS_SIZE);
  43678. + buf += USB_DT_BOS_SIZE;
  43679. +
  43680. + memcpy(buf, &fsg_ext_cap_desc, USB_DT_USB_EXT_CAP_SIZE);
  43681. + buf += USB_DT_USB_EXT_CAP_SIZE;
  43682. +
  43683. + memcpy(buf, &fsg_ss_cap_desc, USB_DT_USB_SS_CAP_SIZE);
  43684. +
  43685. + return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE
  43686. + + USB_DT_USB_EXT_CAP_SIZE;
  43687. +}
  43688. +
  43689. +/*
  43690. + * Config descriptors must agree with the code that sets configurations
  43691. + * and with code managing interfaces and their altsettings. They must
  43692. + * also handle different speeds and other-speed requests.
  43693. + */
  43694. +static int populate_config_buf(struct usb_gadget *gadget,
  43695. + u8 *buf, u8 type, unsigned index)
  43696. +{
  43697. + enum usb_device_speed speed = gadget->speed;
  43698. + int len;
  43699. + const struct usb_descriptor_header **function;
  43700. +
  43701. + if (index > 0)
  43702. + return -EINVAL;
  43703. +
  43704. + if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
  43705. + speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
  43706. + function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH
  43707. + ? (const struct usb_descriptor_header **)fsg_hs_function
  43708. + : (const struct usb_descriptor_header **)fsg_fs_function;
  43709. +
  43710. + /* for now, don't advertise srp-only devices */
  43711. + if (!gadget_is_otg(gadget))
  43712. + function++;
  43713. +
  43714. + len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
  43715. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  43716. + return len;
  43717. +}
  43718. +
  43719. +
  43720. +/*-------------------------------------------------------------------------*/
  43721. +
  43722. +/* These routines may be called in process context or in_irq */
  43723. +
  43724. +/* Caller must hold fsg->lock */
  43725. +static void wakeup_thread(struct fsg_dev *fsg)
  43726. +{
  43727. + /* Tell the main thread that something has happened */
  43728. + fsg->thread_wakeup_needed = 1;
  43729. + if (fsg->thread_task)
  43730. + wake_up_process(fsg->thread_task);
  43731. +}
  43732. +
  43733. +
  43734. +static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
  43735. +{
  43736. + unsigned long flags;
  43737. +
  43738. + /* Do nothing if a higher-priority exception is already in progress.
  43739. + * If a lower-or-equal priority exception is in progress, preempt it
  43740. + * and notify the main thread by sending it a signal. */
  43741. + spin_lock_irqsave(&fsg->lock, flags);
  43742. + if (fsg->state <= new_state) {
  43743. + fsg->exception_req_tag = fsg->ep0_req_tag;
  43744. + fsg->state = new_state;
  43745. + if (fsg->thread_task)
  43746. + send_sig_info(SIGUSR1, SEND_SIG_FORCED,
  43747. + fsg->thread_task);
  43748. + }
  43749. + spin_unlock_irqrestore(&fsg->lock, flags);
  43750. +}
  43751. +
  43752. +
  43753. +/*-------------------------------------------------------------------------*/
  43754. +
  43755. +/* The disconnect callback and ep0 routines. These always run in_irq,
  43756. + * except that ep0_queue() is called in the main thread to acknowledge
  43757. + * completion of various requests: set config, set interface, and
  43758. + * Bulk-only device reset. */
  43759. +
  43760. +static void fsg_disconnect(struct usb_gadget *gadget)
  43761. +{
  43762. + struct fsg_dev *fsg = get_gadget_data(gadget);
  43763. +
  43764. + DBG(fsg, "disconnect or port reset\n");
  43765. + raise_exception(fsg, FSG_STATE_DISCONNECT);
  43766. +}
  43767. +
  43768. +
  43769. +static int ep0_queue(struct fsg_dev *fsg)
  43770. +{
  43771. + int rc;
  43772. +
  43773. + rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
  43774. + if (rc != 0 && rc != -ESHUTDOWN) {
  43775. +
  43776. + /* We can't do much more than wait for a reset */
  43777. + WARNING(fsg, "error in submission: %s --> %d\n",
  43778. + fsg->ep0->name, rc);
  43779. + }
  43780. + return rc;
  43781. +}
  43782. +
  43783. +static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
  43784. +{
  43785. + struct fsg_dev *fsg = ep->driver_data;
  43786. +
  43787. + if (req->actual > 0)
  43788. + dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
  43789. + if (req->status || req->actual != req->length)
  43790. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  43791. + req->status, req->actual, req->length);
  43792. + if (req->status == -ECONNRESET) // Request was cancelled
  43793. + usb_ep_fifo_flush(ep);
  43794. +
  43795. + if (req->status == 0 && req->context)
  43796. + ((fsg_routine_t) (req->context))(fsg);
  43797. +}
  43798. +
  43799. +
  43800. +/*-------------------------------------------------------------------------*/
  43801. +
  43802. +/* Bulk and interrupt endpoint completion handlers.
  43803. + * These always run in_irq. */
  43804. +
  43805. +static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
  43806. +{
  43807. + struct fsg_dev *fsg = ep->driver_data;
  43808. + struct fsg_buffhd *bh = req->context;
  43809. +
  43810. + if (req->status || req->actual != req->length)
  43811. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  43812. + req->status, req->actual, req->length);
  43813. + if (req->status == -ECONNRESET) // Request was cancelled
  43814. + usb_ep_fifo_flush(ep);
  43815. +
  43816. + /* Hold the lock while we update the request and buffer states */
  43817. + smp_wmb();
  43818. + spin_lock(&fsg->lock);
  43819. + bh->inreq_busy = 0;
  43820. + bh->state = BUF_STATE_EMPTY;
  43821. + wakeup_thread(fsg);
  43822. + spin_unlock(&fsg->lock);
  43823. +}
  43824. +
  43825. +static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
  43826. +{
  43827. + struct fsg_dev *fsg = ep->driver_data;
  43828. + struct fsg_buffhd *bh = req->context;
  43829. +
  43830. + dump_msg(fsg, "bulk-out", req->buf, req->actual);
  43831. + if (req->status || req->actual != bh->bulk_out_intended_length)
  43832. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  43833. + req->status, req->actual,
  43834. + bh->bulk_out_intended_length);
  43835. + if (req->status == -ECONNRESET) // Request was cancelled
  43836. + usb_ep_fifo_flush(ep);
  43837. +
  43838. + /* Hold the lock while we update the request and buffer states */
  43839. + smp_wmb();
  43840. + spin_lock(&fsg->lock);
  43841. + bh->outreq_busy = 0;
  43842. + bh->state = BUF_STATE_FULL;
  43843. + wakeup_thread(fsg);
  43844. + spin_unlock(&fsg->lock);
  43845. +}
  43846. +
  43847. +
  43848. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  43849. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  43850. +{
  43851. + struct fsg_dev *fsg = ep->driver_data;
  43852. + struct fsg_buffhd *bh = req->context;
  43853. +
  43854. + if (req->status || req->actual != req->length)
  43855. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  43856. + req->status, req->actual, req->length);
  43857. + if (req->status == -ECONNRESET) // Request was cancelled
  43858. + usb_ep_fifo_flush(ep);
  43859. +
  43860. + /* Hold the lock while we update the request and buffer states */
  43861. + smp_wmb();
  43862. + spin_lock(&fsg->lock);
  43863. + fsg->intreq_busy = 0;
  43864. + bh->state = BUF_STATE_EMPTY;
  43865. + wakeup_thread(fsg);
  43866. + spin_unlock(&fsg->lock);
  43867. +}
  43868. +
  43869. +#else
  43870. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  43871. +{}
  43872. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  43873. +
  43874. +
  43875. +/*-------------------------------------------------------------------------*/
  43876. +
  43877. +/* Ep0 class-specific handlers. These always run in_irq. */
  43878. +
  43879. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  43880. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  43881. +{
  43882. + struct usb_request *req = fsg->ep0req;
  43883. + static u8 cbi_reset_cmnd[6] = {
  43884. + SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
  43885. +
  43886. + /* Error in command transfer? */
  43887. + if (req->status || req->length != req->actual ||
  43888. + req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
  43889. +
  43890. + /* Not all controllers allow a protocol stall after
  43891. + * receiving control-out data, but we'll try anyway. */
  43892. + fsg_set_halt(fsg, fsg->ep0);
  43893. + return; // Wait for reset
  43894. + }
  43895. +
  43896. + /* Is it the special reset command? */
  43897. + if (req->actual >= sizeof cbi_reset_cmnd &&
  43898. + memcmp(req->buf, cbi_reset_cmnd,
  43899. + sizeof cbi_reset_cmnd) == 0) {
  43900. +
  43901. + /* Raise an exception to stop the current operation
  43902. + * and reinitialize our state. */
  43903. + DBG(fsg, "cbi reset request\n");
  43904. + raise_exception(fsg, FSG_STATE_RESET);
  43905. + return;
  43906. + }
  43907. +
  43908. + VDBG(fsg, "CB[I] accept device-specific command\n");
  43909. + spin_lock(&fsg->lock);
  43910. +
  43911. + /* Save the command for later */
  43912. + if (fsg->cbbuf_cmnd_size)
  43913. + WARNING(fsg, "CB[I] overwriting previous command\n");
  43914. + fsg->cbbuf_cmnd_size = req->actual;
  43915. + memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
  43916. +
  43917. + wakeup_thread(fsg);
  43918. + spin_unlock(&fsg->lock);
  43919. +}
  43920. +
  43921. +#else
  43922. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  43923. +{}
  43924. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  43925. +
  43926. +
  43927. +static int class_setup_req(struct fsg_dev *fsg,
  43928. + const struct usb_ctrlrequest *ctrl)
  43929. +{
  43930. + struct usb_request *req = fsg->ep0req;
  43931. + int value = -EOPNOTSUPP;
  43932. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  43933. + u16 w_value = le16_to_cpu(ctrl->wValue);
  43934. + u16 w_length = le16_to_cpu(ctrl->wLength);
  43935. +
  43936. + if (!fsg->config)
  43937. + return value;
  43938. +
  43939. + /* Handle Bulk-only class-specific requests */
  43940. + if (transport_is_bbb()) {
  43941. + switch (ctrl->bRequest) {
  43942. +
  43943. + case US_BULK_RESET_REQUEST:
  43944. + if (ctrl->bRequestType != (USB_DIR_OUT |
  43945. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  43946. + break;
  43947. + if (w_index != 0 || w_value != 0 || w_length != 0) {
  43948. + value = -EDOM;
  43949. + break;
  43950. + }
  43951. +
  43952. + /* Raise an exception to stop the current operation
  43953. + * and reinitialize our state. */
  43954. + DBG(fsg, "bulk reset request\n");
  43955. + raise_exception(fsg, FSG_STATE_RESET);
  43956. + value = DELAYED_STATUS;
  43957. + break;
  43958. +
  43959. + case US_BULK_GET_MAX_LUN:
  43960. + if (ctrl->bRequestType != (USB_DIR_IN |
  43961. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  43962. + break;
  43963. + if (w_index != 0 || w_value != 0 || w_length != 1) {
  43964. + value = -EDOM;
  43965. + break;
  43966. + }
  43967. + VDBG(fsg, "get max LUN\n");
  43968. + *(u8 *) req->buf = fsg->nluns - 1;
  43969. + value = 1;
  43970. + break;
  43971. + }
  43972. + }
  43973. +
  43974. + /* Handle CBI class-specific requests */
  43975. + else {
  43976. + switch (ctrl->bRequest) {
  43977. +
  43978. + case USB_CBI_ADSC_REQUEST:
  43979. + if (ctrl->bRequestType != (USB_DIR_OUT |
  43980. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  43981. + break;
  43982. + if (w_index != 0 || w_value != 0) {
  43983. + value = -EDOM;
  43984. + break;
  43985. + }
  43986. + if (w_length > MAX_COMMAND_SIZE) {
  43987. + value = -EOVERFLOW;
  43988. + break;
  43989. + }
  43990. + value = w_length;
  43991. + fsg->ep0req->context = received_cbi_adsc;
  43992. + break;
  43993. + }
  43994. + }
  43995. +
  43996. + if (value == -EOPNOTSUPP)
  43997. + VDBG(fsg,
  43998. + "unknown class-specific control req "
  43999. + "%02x.%02x v%04x i%04x l%u\n",
  44000. + ctrl->bRequestType, ctrl->bRequest,
  44001. + le16_to_cpu(ctrl->wValue), w_index, w_length);
  44002. + return value;
  44003. +}
  44004. +
  44005. +
  44006. +/*-------------------------------------------------------------------------*/
  44007. +
  44008. +/* Ep0 standard request handlers. These always run in_irq. */
  44009. +
  44010. +static int standard_setup_req(struct fsg_dev *fsg,
  44011. + const struct usb_ctrlrequest *ctrl)
  44012. +{
  44013. + struct usb_request *req = fsg->ep0req;
  44014. + int value = -EOPNOTSUPP;
  44015. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  44016. + u16 w_value = le16_to_cpu(ctrl->wValue);
  44017. +
  44018. + /* Usually this just stores reply data in the pre-allocated ep0 buffer,
  44019. + * but config change events will also reconfigure hardware. */
  44020. + switch (ctrl->bRequest) {
  44021. +
  44022. + case USB_REQ_GET_DESCRIPTOR:
  44023. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  44024. + USB_RECIP_DEVICE))
  44025. + break;
  44026. + switch (w_value >> 8) {
  44027. +
  44028. + case USB_DT_DEVICE:
  44029. + VDBG(fsg, "get device descriptor\n");
  44030. + device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
  44031. + value = sizeof device_desc;
  44032. + memcpy(req->buf, &device_desc, value);
  44033. + break;
  44034. + case USB_DT_DEVICE_QUALIFIER:
  44035. + VDBG(fsg, "get device qualifier\n");
  44036. + if (!gadget_is_dualspeed(fsg->gadget) ||
  44037. + fsg->gadget->speed == USB_SPEED_SUPER)
  44038. + break;
  44039. + /*
  44040. + * Assume ep0 uses the same maxpacket value for both
  44041. + * speeds
  44042. + */
  44043. + dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
  44044. + value = sizeof dev_qualifier;
  44045. + memcpy(req->buf, &dev_qualifier, value);
  44046. + break;
  44047. +
  44048. + case USB_DT_OTHER_SPEED_CONFIG:
  44049. + VDBG(fsg, "get other-speed config descriptor\n");
  44050. + if (!gadget_is_dualspeed(fsg->gadget) ||
  44051. + fsg->gadget->speed == USB_SPEED_SUPER)
  44052. + break;
  44053. + goto get_config;
  44054. + case USB_DT_CONFIG:
  44055. + VDBG(fsg, "get configuration descriptor\n");
  44056. +get_config:
  44057. + value = populate_config_buf(fsg->gadget,
  44058. + req->buf,
  44059. + w_value >> 8,
  44060. + w_value & 0xff);
  44061. + break;
  44062. +
  44063. + case USB_DT_STRING:
  44064. + VDBG(fsg, "get string descriptor\n");
  44065. +
  44066. + /* wIndex == language code */
  44067. + value = usb_gadget_get_string(&fsg_stringtab,
  44068. + w_value & 0xff, req->buf);
  44069. + break;
  44070. +
  44071. + case USB_DT_BOS:
  44072. + VDBG(fsg, "get bos descriptor\n");
  44073. +
  44074. + if (gadget_is_superspeed(fsg->gadget))
  44075. + value = populate_bos(fsg, req->buf);
  44076. + break;
  44077. + }
  44078. +
  44079. + break;
  44080. +
  44081. + /* One config, two speeds */
  44082. + case USB_REQ_SET_CONFIGURATION:
  44083. + if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
  44084. + USB_RECIP_DEVICE))
  44085. + break;
  44086. + VDBG(fsg, "set configuration\n");
  44087. + if (w_value == CONFIG_VALUE || w_value == 0) {
  44088. + fsg->new_config = w_value;
  44089. +
  44090. + /* Raise an exception to wipe out previous transaction
  44091. + * state (queued bufs, etc) and set the new config. */
  44092. + raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
  44093. + value = DELAYED_STATUS;
  44094. + }
  44095. + break;
  44096. + case USB_REQ_GET_CONFIGURATION:
  44097. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  44098. + USB_RECIP_DEVICE))
  44099. + break;
  44100. + VDBG(fsg, "get configuration\n");
  44101. + *(u8 *) req->buf = fsg->config;
  44102. + value = 1;
  44103. + break;
  44104. +
  44105. + case USB_REQ_SET_INTERFACE:
  44106. + if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
  44107. + USB_RECIP_INTERFACE))
  44108. + break;
  44109. + if (fsg->config && w_index == 0) {
  44110. +
  44111. + /* Raise an exception to wipe out previous transaction
  44112. + * state (queued bufs, etc) and install the new
  44113. + * interface altsetting. */
  44114. + raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
  44115. + value = DELAYED_STATUS;
  44116. + }
  44117. + break;
  44118. + case USB_REQ_GET_INTERFACE:
  44119. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  44120. + USB_RECIP_INTERFACE))
  44121. + break;
  44122. + if (!fsg->config)
  44123. + break;
  44124. + if (w_index != 0) {
  44125. + value = -EDOM;
  44126. + break;
  44127. + }
  44128. + VDBG(fsg, "get interface\n");
  44129. + *(u8 *) req->buf = 0;
  44130. + value = 1;
  44131. + break;
  44132. +
  44133. + default:
  44134. + VDBG(fsg,
  44135. + "unknown control req %02x.%02x v%04x i%04x l%u\n",
  44136. + ctrl->bRequestType, ctrl->bRequest,
  44137. + w_value, w_index, le16_to_cpu(ctrl->wLength));
  44138. + }
  44139. +
  44140. + return value;
  44141. +}
  44142. +
  44143. +
  44144. +static int fsg_setup(struct usb_gadget *gadget,
  44145. + const struct usb_ctrlrequest *ctrl)
  44146. +{
  44147. + struct fsg_dev *fsg = get_gadget_data(gadget);
  44148. + int rc;
  44149. + int w_length = le16_to_cpu(ctrl->wLength);
  44150. +
  44151. + ++fsg->ep0_req_tag; // Record arrival of a new request
  44152. + fsg->ep0req->context = NULL;
  44153. + fsg->ep0req->length = 0;
  44154. + dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
  44155. +
  44156. + if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
  44157. + rc = class_setup_req(fsg, ctrl);
  44158. + else
  44159. + rc = standard_setup_req(fsg, ctrl);
  44160. +
  44161. + /* Respond with data/status or defer until later? */
  44162. + if (rc >= 0 && rc != DELAYED_STATUS) {
  44163. + rc = min(rc, w_length);
  44164. + fsg->ep0req->length = rc;
  44165. + fsg->ep0req->zero = rc < w_length;
  44166. + fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
  44167. + "ep0-in" : "ep0-out");
  44168. + rc = ep0_queue(fsg);
  44169. + }
  44170. +
  44171. + /* Device either stalls (rc < 0) or reports success */
  44172. + return rc;
  44173. +}
  44174. +
  44175. +
  44176. +/*-------------------------------------------------------------------------*/
  44177. +
  44178. +/* All the following routines run in process context */
  44179. +
  44180. +
  44181. +/* Use this for bulk or interrupt transfers, not ep0 */
  44182. +static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
  44183. + struct usb_request *req, int *pbusy,
  44184. + enum fsg_buffer_state *state)
  44185. +{
  44186. + int rc;
  44187. +
  44188. + if (ep == fsg->bulk_in)
  44189. + dump_msg(fsg, "bulk-in", req->buf, req->length);
  44190. + else if (ep == fsg->intr_in)
  44191. + dump_msg(fsg, "intr-in", req->buf, req->length);
  44192. +
  44193. + spin_lock_irq(&fsg->lock);
  44194. + *pbusy = 1;
  44195. + *state = BUF_STATE_BUSY;
  44196. + spin_unlock_irq(&fsg->lock);
  44197. + rc = usb_ep_queue(ep, req, GFP_KERNEL);
  44198. + if (rc != 0) {
  44199. + *pbusy = 0;
  44200. + *state = BUF_STATE_EMPTY;
  44201. +
  44202. + /* We can't do much more than wait for a reset */
  44203. +
  44204. + /* Note: currently the net2280 driver fails zero-length
  44205. + * submissions if DMA is enabled. */
  44206. + if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
  44207. + req->length == 0))
  44208. + WARNING(fsg, "error in submission: %s --> %d\n",
  44209. + ep->name, rc);
  44210. + }
  44211. +}
  44212. +
  44213. +
  44214. +static int sleep_thread(struct fsg_dev *fsg)
  44215. +{
  44216. + int rc = 0;
  44217. +
  44218. + /* Wait until a signal arrives or we are woken up */
  44219. + for (;;) {
  44220. + try_to_freeze();
  44221. + set_current_state(TASK_INTERRUPTIBLE);
  44222. + if (signal_pending(current)) {
  44223. + rc = -EINTR;
  44224. + break;
  44225. + }
  44226. + if (fsg->thread_wakeup_needed)
  44227. + break;
  44228. + schedule();
  44229. + }
  44230. + __set_current_state(TASK_RUNNING);
  44231. + fsg->thread_wakeup_needed = 0;
  44232. + return rc;
  44233. +}
  44234. +
  44235. +
  44236. +/*-------------------------------------------------------------------------*/
  44237. +
  44238. +static int do_read(struct fsg_dev *fsg)
  44239. +{
  44240. + struct fsg_lun *curlun = fsg->curlun;
  44241. + u32 lba;
  44242. + struct fsg_buffhd *bh;
  44243. + int rc;
  44244. + u32 amount_left;
  44245. + loff_t file_offset, file_offset_tmp;
  44246. + unsigned int amount;
  44247. + ssize_t nread;
  44248. +
  44249. + /* Get the starting Logical Block Address and check that it's
  44250. + * not too big */
  44251. + if (fsg->cmnd[0] == READ_6)
  44252. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  44253. + else {
  44254. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  44255. +
  44256. + /* We allow DPO (Disable Page Out = don't save data in the
  44257. + * cache) and FUA (Force Unit Access = don't read from the
  44258. + * cache), but we don't implement them. */
  44259. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  44260. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  44261. + return -EINVAL;
  44262. + }
  44263. + }
  44264. + if (lba >= curlun->num_sectors) {
  44265. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  44266. + return -EINVAL;
  44267. + }
  44268. + file_offset = ((loff_t) lba) << curlun->blkbits;
  44269. +
  44270. + /* Carry out the file reads */
  44271. + amount_left = fsg->data_size_from_cmnd;
  44272. + if (unlikely(amount_left == 0))
  44273. + return -EIO; // No default reply
  44274. +
  44275. + for (;;) {
  44276. +
  44277. + /* Figure out how much we need to read:
  44278. + * Try to read the remaining amount.
  44279. + * But don't read more than the buffer size.
  44280. + * And don't try to read past the end of the file.
  44281. + */
  44282. + amount = min((unsigned int) amount_left, mod_data.buflen);
  44283. + amount = min((loff_t) amount,
  44284. + curlun->file_length - file_offset);
  44285. +
  44286. + /* Wait for the next buffer to become available */
  44287. + bh = fsg->next_buffhd_to_fill;
  44288. + while (bh->state != BUF_STATE_EMPTY) {
  44289. + rc = sleep_thread(fsg);
  44290. + if (rc)
  44291. + return rc;
  44292. + }
  44293. +
  44294. + /* If we were asked to read past the end of file,
  44295. + * end with an empty buffer. */
  44296. + if (amount == 0) {
  44297. + curlun->sense_data =
  44298. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  44299. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  44300. + curlun->info_valid = 1;
  44301. + bh->inreq->length = 0;
  44302. + bh->state = BUF_STATE_FULL;
  44303. + break;
  44304. + }
  44305. +
  44306. + /* Perform the read */
  44307. + file_offset_tmp = file_offset;
  44308. + nread = vfs_read(curlun->filp,
  44309. + (char __user *) bh->buf,
  44310. + amount, &file_offset_tmp);
  44311. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  44312. + (unsigned long long) file_offset,
  44313. + (int) nread);
  44314. + if (signal_pending(current))
  44315. + return -EINTR;
  44316. +
  44317. + if (nread < 0) {
  44318. + LDBG(curlun, "error in file read: %d\n",
  44319. + (int) nread);
  44320. + nread = 0;
  44321. + } else if (nread < amount) {
  44322. + LDBG(curlun, "partial file read: %d/%u\n",
  44323. + (int) nread, amount);
  44324. + nread = round_down(nread, curlun->blksize);
  44325. + }
  44326. + file_offset += nread;
  44327. + amount_left -= nread;
  44328. + fsg->residue -= nread;
  44329. +
  44330. + /* Except at the end of the transfer, nread will be
  44331. + * equal to the buffer size, which is divisible by the
  44332. + * bulk-in maxpacket size.
  44333. + */
  44334. + bh->inreq->length = nread;
  44335. + bh->state = BUF_STATE_FULL;
  44336. +
  44337. + /* If an error occurred, report it and its position */
  44338. + if (nread < amount) {
  44339. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  44340. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  44341. + curlun->info_valid = 1;
  44342. + break;
  44343. + }
  44344. +
  44345. + if (amount_left == 0)
  44346. + break; // No more left to read
  44347. +
  44348. + /* Send this buffer and go read some more */
  44349. + bh->inreq->zero = 0;
  44350. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  44351. + &bh->inreq_busy, &bh->state);
  44352. + fsg->next_buffhd_to_fill = bh->next;
  44353. + }
  44354. +
  44355. + return -EIO; // No default reply
  44356. +}
  44357. +
  44358. +
  44359. +/*-------------------------------------------------------------------------*/
  44360. +
  44361. +static int do_write(struct fsg_dev *fsg)
  44362. +{
  44363. + struct fsg_lun *curlun = fsg->curlun;
  44364. + u32 lba;
  44365. + struct fsg_buffhd *bh;
  44366. + int get_some_more;
  44367. + u32 amount_left_to_req, amount_left_to_write;
  44368. + loff_t usb_offset, file_offset, file_offset_tmp;
  44369. + unsigned int amount;
  44370. + ssize_t nwritten;
  44371. + int rc;
  44372. +
  44373. + if (curlun->ro) {
  44374. + curlun->sense_data = SS_WRITE_PROTECTED;
  44375. + return -EINVAL;
  44376. + }
  44377. + spin_lock(&curlun->filp->f_lock);
  44378. + curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
  44379. + spin_unlock(&curlun->filp->f_lock);
  44380. +
  44381. + /* Get the starting Logical Block Address and check that it's
  44382. + * not too big */
  44383. + if (fsg->cmnd[0] == WRITE_6)
  44384. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  44385. + else {
  44386. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  44387. +
  44388. + /* We allow DPO (Disable Page Out = don't save data in the
  44389. + * cache) and FUA (Force Unit Access = write directly to the
  44390. + * medium). We don't implement DPO; we implement FUA by
  44391. + * performing synchronous output. */
  44392. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  44393. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  44394. + return -EINVAL;
  44395. + }
  44396. + /* FUA */
  44397. + if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
  44398. + spin_lock(&curlun->filp->f_lock);
  44399. + curlun->filp->f_flags |= O_DSYNC;
  44400. + spin_unlock(&curlun->filp->f_lock);
  44401. + }
  44402. + }
  44403. + if (lba >= curlun->num_sectors) {
  44404. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  44405. + return -EINVAL;
  44406. + }
  44407. +
  44408. + /* Carry out the file writes */
  44409. + get_some_more = 1;
  44410. + file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
  44411. + amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
  44412. +
  44413. + while (amount_left_to_write > 0) {
  44414. +
  44415. + /* Queue a request for more data from the host */
  44416. + bh = fsg->next_buffhd_to_fill;
  44417. + if (bh->state == BUF_STATE_EMPTY && get_some_more) {
  44418. +
  44419. + /* Figure out how much we want to get:
  44420. + * Try to get the remaining amount,
  44421. + * but not more than the buffer size.
  44422. + */
  44423. + amount = min(amount_left_to_req, mod_data.buflen);
  44424. +
  44425. + /* Beyond the end of the backing file? */
  44426. + if (usb_offset >= curlun->file_length) {
  44427. + get_some_more = 0;
  44428. + curlun->sense_data =
  44429. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  44430. + curlun->sense_data_info = usb_offset >> curlun->blkbits;
  44431. + curlun->info_valid = 1;
  44432. + continue;
  44433. + }
  44434. +
  44435. + /* Get the next buffer */
  44436. + usb_offset += amount;
  44437. + fsg->usb_amount_left -= amount;
  44438. + amount_left_to_req -= amount;
  44439. + if (amount_left_to_req == 0)
  44440. + get_some_more = 0;
  44441. +
  44442. + /* Except at the end of the transfer, amount will be
  44443. + * equal to the buffer size, which is divisible by
  44444. + * the bulk-out maxpacket size.
  44445. + */
  44446. + set_bulk_out_req_length(fsg, bh, amount);
  44447. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  44448. + &bh->outreq_busy, &bh->state);
  44449. + fsg->next_buffhd_to_fill = bh->next;
  44450. + continue;
  44451. + }
  44452. +
  44453. + /* Write the received data to the backing file */
  44454. + bh = fsg->next_buffhd_to_drain;
  44455. + if (bh->state == BUF_STATE_EMPTY && !get_some_more)
  44456. + break; // We stopped early
  44457. + if (bh->state == BUF_STATE_FULL) {
  44458. + smp_rmb();
  44459. + fsg->next_buffhd_to_drain = bh->next;
  44460. + bh->state = BUF_STATE_EMPTY;
  44461. +
  44462. + /* Did something go wrong with the transfer? */
  44463. + if (bh->outreq->status != 0) {
  44464. + curlun->sense_data = SS_COMMUNICATION_FAILURE;
  44465. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  44466. + curlun->info_valid = 1;
  44467. + break;
  44468. + }
  44469. +
  44470. + amount = bh->outreq->actual;
  44471. + if (curlun->file_length - file_offset < amount) {
  44472. + LERROR(curlun,
  44473. + "write %u @ %llu beyond end %llu\n",
  44474. + amount, (unsigned long long) file_offset,
  44475. + (unsigned long long) curlun->file_length);
  44476. + amount = curlun->file_length - file_offset;
  44477. + }
  44478. +
  44479. + /* Don't accept excess data. The spec doesn't say
  44480. + * what to do in this case. We'll ignore the error.
  44481. + */
  44482. + amount = min(amount, bh->bulk_out_intended_length);
  44483. +
  44484. + /* Don't write a partial block */
  44485. + amount = round_down(amount, curlun->blksize);
  44486. + if (amount == 0)
  44487. + goto empty_write;
  44488. +
  44489. + /* Perform the write */
  44490. + file_offset_tmp = file_offset;
  44491. + nwritten = vfs_write(curlun->filp,
  44492. + (char __user *) bh->buf,
  44493. + amount, &file_offset_tmp);
  44494. + VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
  44495. + (unsigned long long) file_offset,
  44496. + (int) nwritten);
  44497. + if (signal_pending(current))
  44498. + return -EINTR; // Interrupted!
  44499. +
  44500. + if (nwritten < 0) {
  44501. + LDBG(curlun, "error in file write: %d\n",
  44502. + (int) nwritten);
  44503. + nwritten = 0;
  44504. + } else if (nwritten < amount) {
  44505. + LDBG(curlun, "partial file write: %d/%u\n",
  44506. + (int) nwritten, amount);
  44507. + nwritten = round_down(nwritten, curlun->blksize);
  44508. + }
  44509. + file_offset += nwritten;
  44510. + amount_left_to_write -= nwritten;
  44511. + fsg->residue -= nwritten;
  44512. +
  44513. + /* If an error occurred, report it and its position */
  44514. + if (nwritten < amount) {
  44515. + curlun->sense_data = SS_WRITE_ERROR;
  44516. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  44517. + curlun->info_valid = 1;
  44518. + break;
  44519. + }
  44520. +
  44521. + empty_write:
  44522. + /* Did the host decide to stop early? */
  44523. + if (bh->outreq->actual < bh->bulk_out_intended_length) {
  44524. + fsg->short_packet_received = 1;
  44525. + break;
  44526. + }
  44527. + continue;
  44528. + }
  44529. +
  44530. + /* Wait for something to happen */
  44531. + rc = sleep_thread(fsg);
  44532. + if (rc)
  44533. + return rc;
  44534. + }
  44535. +
  44536. + return -EIO; // No default reply
  44537. +}
  44538. +
  44539. +
  44540. +/*-------------------------------------------------------------------------*/
  44541. +
  44542. +static int do_synchronize_cache(struct fsg_dev *fsg)
  44543. +{
  44544. + struct fsg_lun *curlun = fsg->curlun;
  44545. + int rc;
  44546. +
  44547. + /* We ignore the requested LBA and write out all file's
  44548. + * dirty data buffers. */
  44549. + rc = fsg_lun_fsync_sub(curlun);
  44550. + if (rc)
  44551. + curlun->sense_data = SS_WRITE_ERROR;
  44552. + return 0;
  44553. +}
  44554. +
  44555. +
  44556. +/*-------------------------------------------------------------------------*/
  44557. +
  44558. +static void invalidate_sub(struct fsg_lun *curlun)
  44559. +{
  44560. + struct file *filp = curlun->filp;
  44561. + struct inode *inode = filp->f_path.dentry->d_inode;
  44562. + unsigned long rc;
  44563. +
  44564. + rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
  44565. + VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
  44566. +}
  44567. +
  44568. +static int do_verify(struct fsg_dev *fsg)
  44569. +{
  44570. + struct fsg_lun *curlun = fsg->curlun;
  44571. + u32 lba;
  44572. + u32 verification_length;
  44573. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  44574. + loff_t file_offset, file_offset_tmp;
  44575. + u32 amount_left;
  44576. + unsigned int amount;
  44577. + ssize_t nread;
  44578. +
  44579. + /* Get the starting Logical Block Address and check that it's
  44580. + * not too big */
  44581. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  44582. + if (lba >= curlun->num_sectors) {
  44583. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  44584. + return -EINVAL;
  44585. + }
  44586. +
  44587. + /* We allow DPO (Disable Page Out = don't save data in the
  44588. + * cache) but we don't implement it. */
  44589. + if ((fsg->cmnd[1] & ~0x10) != 0) {
  44590. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  44591. + return -EINVAL;
  44592. + }
  44593. +
  44594. + verification_length = get_unaligned_be16(&fsg->cmnd[7]);
  44595. + if (unlikely(verification_length == 0))
  44596. + return -EIO; // No default reply
  44597. +
  44598. + /* Prepare to carry out the file verify */
  44599. + amount_left = verification_length << curlun->blkbits;
  44600. + file_offset = ((loff_t) lba) << curlun->blkbits;
  44601. +
  44602. + /* Write out all the dirty buffers before invalidating them */
  44603. + fsg_lun_fsync_sub(curlun);
  44604. + if (signal_pending(current))
  44605. + return -EINTR;
  44606. +
  44607. + invalidate_sub(curlun);
  44608. + if (signal_pending(current))
  44609. + return -EINTR;
  44610. +
  44611. + /* Just try to read the requested blocks */
  44612. + while (amount_left > 0) {
  44613. +
  44614. + /* Figure out how much we need to read:
  44615. + * Try to read the remaining amount, but not more than
  44616. + * the buffer size.
  44617. + * And don't try to read past the end of the file.
  44618. + */
  44619. + amount = min((unsigned int) amount_left, mod_data.buflen);
  44620. + amount = min((loff_t) amount,
  44621. + curlun->file_length - file_offset);
  44622. + if (amount == 0) {
  44623. + curlun->sense_data =
  44624. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  44625. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  44626. + curlun->info_valid = 1;
  44627. + break;
  44628. + }
  44629. +
  44630. + /* Perform the read */
  44631. + file_offset_tmp = file_offset;
  44632. + nread = vfs_read(curlun->filp,
  44633. + (char __user *) bh->buf,
  44634. + amount, &file_offset_tmp);
  44635. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  44636. + (unsigned long long) file_offset,
  44637. + (int) nread);
  44638. + if (signal_pending(current))
  44639. + return -EINTR;
  44640. +
  44641. + if (nread < 0) {
  44642. + LDBG(curlun, "error in file verify: %d\n",
  44643. + (int) nread);
  44644. + nread = 0;
  44645. + } else if (nread < amount) {
  44646. + LDBG(curlun, "partial file verify: %d/%u\n",
  44647. + (int) nread, amount);
  44648. + nread = round_down(nread, curlun->blksize);
  44649. + }
  44650. + if (nread == 0) {
  44651. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  44652. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  44653. + curlun->info_valid = 1;
  44654. + break;
  44655. + }
  44656. + file_offset += nread;
  44657. + amount_left -= nread;
  44658. + }
  44659. + return 0;
  44660. +}
  44661. +
  44662. +
  44663. +/*-------------------------------------------------------------------------*/
  44664. +
  44665. +static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  44666. +{
  44667. + u8 *buf = (u8 *) bh->buf;
  44668. +
  44669. + static char vendor_id[] = "Linux ";
  44670. + static char product_disk_id[] = "File-Stor Gadget";
  44671. + static char product_cdrom_id[] = "File-CD Gadget ";
  44672. +
  44673. + if (!fsg->curlun) { // Unsupported LUNs are okay
  44674. + fsg->bad_lun_okay = 1;
  44675. + memset(buf, 0, 36);
  44676. + buf[0] = 0x7f; // Unsupported, no device-type
  44677. + buf[4] = 31; // Additional length
  44678. + return 36;
  44679. + }
  44680. +
  44681. + memset(buf, 0, 8);
  44682. + buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
  44683. + if (mod_data.removable)
  44684. + buf[1] = 0x80;
  44685. + buf[2] = 2; // ANSI SCSI level 2
  44686. + buf[3] = 2; // SCSI-2 INQUIRY data format
  44687. + buf[4] = 31; // Additional length
  44688. + // No special options
  44689. + sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
  44690. + (mod_data.cdrom ? product_cdrom_id :
  44691. + product_disk_id),
  44692. + mod_data.release);
  44693. + return 36;
  44694. +}
  44695. +
  44696. +
  44697. +static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  44698. +{
  44699. + struct fsg_lun *curlun = fsg->curlun;
  44700. + u8 *buf = (u8 *) bh->buf;
  44701. + u32 sd, sdinfo;
  44702. + int valid;
  44703. +
  44704. + /*
  44705. + * From the SCSI-2 spec., section 7.9 (Unit attention condition):
  44706. + *
  44707. + * If a REQUEST SENSE command is received from an initiator
  44708. + * with a pending unit attention condition (before the target
  44709. + * generates the contingent allegiance condition), then the
  44710. + * target shall either:
  44711. + * a) report any pending sense data and preserve the unit
  44712. + * attention condition on the logical unit, or,
  44713. + * b) report the unit attention condition, may discard any
  44714. + * pending sense data, and clear the unit attention
  44715. + * condition on the logical unit for that initiator.
  44716. + *
  44717. + * FSG normally uses option a); enable this code to use option b).
  44718. + */
  44719. +#if 0
  44720. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
  44721. + curlun->sense_data = curlun->unit_attention_data;
  44722. + curlun->unit_attention_data = SS_NO_SENSE;
  44723. + }
  44724. +#endif
  44725. +
  44726. + if (!curlun) { // Unsupported LUNs are okay
  44727. + fsg->bad_lun_okay = 1;
  44728. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  44729. + sdinfo = 0;
  44730. + valid = 0;
  44731. + } else {
  44732. + sd = curlun->sense_data;
  44733. + sdinfo = curlun->sense_data_info;
  44734. + valid = curlun->info_valid << 7;
  44735. + curlun->sense_data = SS_NO_SENSE;
  44736. + curlun->sense_data_info = 0;
  44737. + curlun->info_valid = 0;
  44738. + }
  44739. +
  44740. + memset(buf, 0, 18);
  44741. + buf[0] = valid | 0x70; // Valid, current error
  44742. + buf[2] = SK(sd);
  44743. + put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
  44744. + buf[7] = 18 - 8; // Additional sense length
  44745. + buf[12] = ASC(sd);
  44746. + buf[13] = ASCQ(sd);
  44747. + return 18;
  44748. +}
  44749. +
  44750. +
  44751. +static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  44752. +{
  44753. + struct fsg_lun *curlun = fsg->curlun;
  44754. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  44755. + int pmi = fsg->cmnd[8];
  44756. + u8 *buf = (u8 *) bh->buf;
  44757. +
  44758. + /* Check the PMI and LBA fields */
  44759. + if (pmi > 1 || (pmi == 0 && lba != 0)) {
  44760. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  44761. + return -EINVAL;
  44762. + }
  44763. +
  44764. + put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
  44765. + /* Max logical block */
  44766. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  44767. + return 8;
  44768. +}
  44769. +
  44770. +
  44771. +static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  44772. +{
  44773. + struct fsg_lun *curlun = fsg->curlun;
  44774. + int msf = fsg->cmnd[1] & 0x02;
  44775. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  44776. + u8 *buf = (u8 *) bh->buf;
  44777. +
  44778. + if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
  44779. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  44780. + return -EINVAL;
  44781. + }
  44782. + if (lba >= curlun->num_sectors) {
  44783. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  44784. + return -EINVAL;
  44785. + }
  44786. +
  44787. + memset(buf, 0, 8);
  44788. + buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
  44789. + store_cdrom_address(&buf[4], msf, lba);
  44790. + return 8;
  44791. +}
  44792. +
  44793. +
  44794. +static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  44795. +{
  44796. + struct fsg_lun *curlun = fsg->curlun;
  44797. + int msf = fsg->cmnd[1] & 0x02;
  44798. + int start_track = fsg->cmnd[6];
  44799. + u8 *buf = (u8 *) bh->buf;
  44800. +
  44801. + if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
  44802. + start_track > 1) {
  44803. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  44804. + return -EINVAL;
  44805. + }
  44806. +
  44807. + memset(buf, 0, 20);
  44808. + buf[1] = (20-2); /* TOC data length */
  44809. + buf[2] = 1; /* First track number */
  44810. + buf[3] = 1; /* Last track number */
  44811. + buf[5] = 0x16; /* Data track, copying allowed */
  44812. + buf[6] = 0x01; /* Only track is number 1 */
  44813. + store_cdrom_address(&buf[8], msf, 0);
  44814. +
  44815. + buf[13] = 0x16; /* Lead-out track is data */
  44816. + buf[14] = 0xAA; /* Lead-out track number */
  44817. + store_cdrom_address(&buf[16], msf, curlun->num_sectors);
  44818. + return 20;
  44819. +}
  44820. +
  44821. +
  44822. +static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  44823. +{
  44824. + struct fsg_lun *curlun = fsg->curlun;
  44825. + int mscmnd = fsg->cmnd[0];
  44826. + u8 *buf = (u8 *) bh->buf;
  44827. + u8 *buf0 = buf;
  44828. + int pc, page_code;
  44829. + int changeable_values, all_pages;
  44830. + int valid_page = 0;
  44831. + int len, limit;
  44832. +
  44833. + if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
  44834. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  44835. + return -EINVAL;
  44836. + }
  44837. + pc = fsg->cmnd[2] >> 6;
  44838. + page_code = fsg->cmnd[2] & 0x3f;
  44839. + if (pc == 3) {
  44840. + curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
  44841. + return -EINVAL;
  44842. + }
  44843. + changeable_values = (pc == 1);
  44844. + all_pages = (page_code == 0x3f);
  44845. +
  44846. + /* Write the mode parameter header. Fixed values are: default
  44847. + * medium type, no cache control (DPOFUA), and no block descriptors.
  44848. + * The only variable value is the WriteProtect bit. We will fill in
  44849. + * the mode data length later. */
  44850. + memset(buf, 0, 8);
  44851. + if (mscmnd == MODE_SENSE) {
  44852. + buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  44853. + buf += 4;
  44854. + limit = 255;
  44855. + } else { // MODE_SENSE_10
  44856. + buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  44857. + buf += 8;
  44858. + limit = 65535; // Should really be mod_data.buflen
  44859. + }
  44860. +
  44861. + /* No block descriptors */
  44862. +
  44863. + /* The mode pages, in numerical order. The only page we support
  44864. + * is the Caching page. */
  44865. + if (page_code == 0x08 || all_pages) {
  44866. + valid_page = 1;
  44867. + buf[0] = 0x08; // Page code
  44868. + buf[1] = 10; // Page length
  44869. + memset(buf+2, 0, 10); // None of the fields are changeable
  44870. +
  44871. + if (!changeable_values) {
  44872. + buf[2] = 0x04; // Write cache enable,
  44873. + // Read cache not disabled
  44874. + // No cache retention priorities
  44875. + put_unaligned_be16(0xffff, &buf[4]);
  44876. + /* Don't disable prefetch */
  44877. + /* Minimum prefetch = 0 */
  44878. + put_unaligned_be16(0xffff, &buf[8]);
  44879. + /* Maximum prefetch */
  44880. + put_unaligned_be16(0xffff, &buf[10]);
  44881. + /* Maximum prefetch ceiling */
  44882. + }
  44883. + buf += 12;
  44884. + }
  44885. +
  44886. + /* Check that a valid page was requested and the mode data length
  44887. + * isn't too long. */
  44888. + len = buf - buf0;
  44889. + if (!valid_page || len > limit) {
  44890. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  44891. + return -EINVAL;
  44892. + }
  44893. +
  44894. + /* Store the mode data length */
  44895. + if (mscmnd == MODE_SENSE)
  44896. + buf0[0] = len - 1;
  44897. + else
  44898. + put_unaligned_be16(len - 2, buf0);
  44899. + return len;
  44900. +}
  44901. +
  44902. +
  44903. +static int do_start_stop(struct fsg_dev *fsg)
  44904. +{
  44905. + struct fsg_lun *curlun = fsg->curlun;
  44906. + int loej, start;
  44907. +
  44908. + if (!mod_data.removable) {
  44909. + curlun->sense_data = SS_INVALID_COMMAND;
  44910. + return -EINVAL;
  44911. + }
  44912. +
  44913. + // int immed = fsg->cmnd[1] & 0x01;
  44914. + loej = fsg->cmnd[4] & 0x02;
  44915. + start = fsg->cmnd[4] & 0x01;
  44916. +
  44917. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  44918. + if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
  44919. + (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
  44920. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  44921. + return -EINVAL;
  44922. + }
  44923. +
  44924. + if (!start) {
  44925. +
  44926. + /* Are we allowed to unload the media? */
  44927. + if (curlun->prevent_medium_removal) {
  44928. + LDBG(curlun, "unload attempt prevented\n");
  44929. + curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
  44930. + return -EINVAL;
  44931. + }
  44932. + if (loej) { // Simulate an unload/eject
  44933. + up_read(&fsg->filesem);
  44934. + down_write(&fsg->filesem);
  44935. + fsg_lun_close(curlun);
  44936. + up_write(&fsg->filesem);
  44937. + down_read(&fsg->filesem);
  44938. + }
  44939. + } else {
  44940. +
  44941. + /* Our emulation doesn't support mounting; the medium is
  44942. + * available for use as soon as it is loaded. */
  44943. + if (!fsg_lun_is_open(curlun)) {
  44944. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  44945. + return -EINVAL;
  44946. + }
  44947. + }
  44948. +#endif
  44949. + return 0;
  44950. +}
  44951. +
  44952. +
  44953. +static int do_prevent_allow(struct fsg_dev *fsg)
  44954. +{
  44955. + struct fsg_lun *curlun = fsg->curlun;
  44956. + int prevent;
  44957. +
  44958. + if (!mod_data.removable) {
  44959. + curlun->sense_data = SS_INVALID_COMMAND;
  44960. + return -EINVAL;
  44961. + }
  44962. +
  44963. + prevent = fsg->cmnd[4] & 0x01;
  44964. + if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
  44965. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  44966. + return -EINVAL;
  44967. + }
  44968. +
  44969. + if (curlun->prevent_medium_removal && !prevent)
  44970. + fsg_lun_fsync_sub(curlun);
  44971. + curlun->prevent_medium_removal = prevent;
  44972. + return 0;
  44973. +}
  44974. +
  44975. +
  44976. +static int do_read_format_capacities(struct fsg_dev *fsg,
  44977. + struct fsg_buffhd *bh)
  44978. +{
  44979. + struct fsg_lun *curlun = fsg->curlun;
  44980. + u8 *buf = (u8 *) bh->buf;
  44981. +
  44982. + buf[0] = buf[1] = buf[2] = 0;
  44983. + buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
  44984. + buf += 4;
  44985. +
  44986. + put_unaligned_be32(curlun->num_sectors, &buf[0]);
  44987. + /* Number of blocks */
  44988. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  44989. + buf[4] = 0x02; /* Current capacity */
  44990. + return 12;
  44991. +}
  44992. +
  44993. +
  44994. +static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  44995. +{
  44996. + struct fsg_lun *curlun = fsg->curlun;
  44997. +
  44998. + /* We don't support MODE SELECT */
  44999. + curlun->sense_data = SS_INVALID_COMMAND;
  45000. + return -EINVAL;
  45001. +}
  45002. +
  45003. +
  45004. +/*-------------------------------------------------------------------------*/
  45005. +
  45006. +static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
  45007. +{
  45008. + int rc;
  45009. +
  45010. + rc = fsg_set_halt(fsg, fsg->bulk_in);
  45011. + if (rc == -EAGAIN)
  45012. + VDBG(fsg, "delayed bulk-in endpoint halt\n");
  45013. + while (rc != 0) {
  45014. + if (rc != -EAGAIN) {
  45015. + WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
  45016. + rc = 0;
  45017. + break;
  45018. + }
  45019. +
  45020. + /* Wait for a short time and then try again */
  45021. + if (msleep_interruptible(100) != 0)
  45022. + return -EINTR;
  45023. + rc = usb_ep_set_halt(fsg->bulk_in);
  45024. + }
  45025. + return rc;
  45026. +}
  45027. +
  45028. +static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
  45029. +{
  45030. + int rc;
  45031. +
  45032. + DBG(fsg, "bulk-in set wedge\n");
  45033. + rc = usb_ep_set_wedge(fsg->bulk_in);
  45034. + if (rc == -EAGAIN)
  45035. + VDBG(fsg, "delayed bulk-in endpoint wedge\n");
  45036. + while (rc != 0) {
  45037. + if (rc != -EAGAIN) {
  45038. + WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
  45039. + rc = 0;
  45040. + break;
  45041. + }
  45042. +
  45043. + /* Wait for a short time and then try again */
  45044. + if (msleep_interruptible(100) != 0)
  45045. + return -EINTR;
  45046. + rc = usb_ep_set_wedge(fsg->bulk_in);
  45047. + }
  45048. + return rc;
  45049. +}
  45050. +
  45051. +static int throw_away_data(struct fsg_dev *fsg)
  45052. +{
  45053. + struct fsg_buffhd *bh;
  45054. + u32 amount;
  45055. + int rc;
  45056. +
  45057. + while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
  45058. + fsg->usb_amount_left > 0) {
  45059. +
  45060. + /* Throw away the data in a filled buffer */
  45061. + if (bh->state == BUF_STATE_FULL) {
  45062. + smp_rmb();
  45063. + bh->state = BUF_STATE_EMPTY;
  45064. + fsg->next_buffhd_to_drain = bh->next;
  45065. +
  45066. + /* A short packet or an error ends everything */
  45067. + if (bh->outreq->actual < bh->bulk_out_intended_length ||
  45068. + bh->outreq->status != 0) {
  45069. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  45070. + return -EINTR;
  45071. + }
  45072. + continue;
  45073. + }
  45074. +
  45075. + /* Try to submit another request if we need one */
  45076. + bh = fsg->next_buffhd_to_fill;
  45077. + if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
  45078. + amount = min(fsg->usb_amount_left,
  45079. + (u32) mod_data.buflen);
  45080. +
  45081. + /* Except at the end of the transfer, amount will be
  45082. + * equal to the buffer size, which is divisible by
  45083. + * the bulk-out maxpacket size.
  45084. + */
  45085. + set_bulk_out_req_length(fsg, bh, amount);
  45086. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  45087. + &bh->outreq_busy, &bh->state);
  45088. + fsg->next_buffhd_to_fill = bh->next;
  45089. + fsg->usb_amount_left -= amount;
  45090. + continue;
  45091. + }
  45092. +
  45093. + /* Otherwise wait for something to happen */
  45094. + rc = sleep_thread(fsg);
  45095. + if (rc)
  45096. + return rc;
  45097. + }
  45098. + return 0;
  45099. +}
  45100. +
  45101. +
  45102. +static int finish_reply(struct fsg_dev *fsg)
  45103. +{
  45104. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  45105. + int rc = 0;
  45106. +
  45107. + switch (fsg->data_dir) {
  45108. + case DATA_DIR_NONE:
  45109. + break; // Nothing to send
  45110. +
  45111. + /* If we don't know whether the host wants to read or write,
  45112. + * this must be CB or CBI with an unknown command. We mustn't
  45113. + * try to send or receive any data. So stall both bulk pipes
  45114. + * if we can and wait for a reset. */
  45115. + case DATA_DIR_UNKNOWN:
  45116. + if (mod_data.can_stall) {
  45117. + fsg_set_halt(fsg, fsg->bulk_out);
  45118. + rc = halt_bulk_in_endpoint(fsg);
  45119. + }
  45120. + break;
  45121. +
  45122. + /* All but the last buffer of data must have already been sent */
  45123. + case DATA_DIR_TO_HOST:
  45124. + if (fsg->data_size == 0)
  45125. + ; // Nothing to send
  45126. +
  45127. + /* If there's no residue, simply send the last buffer */
  45128. + else if (fsg->residue == 0) {
  45129. + bh->inreq->zero = 0;
  45130. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  45131. + &bh->inreq_busy, &bh->state);
  45132. + fsg->next_buffhd_to_fill = bh->next;
  45133. + }
  45134. +
  45135. + /* There is a residue. For CB and CBI, simply mark the end
  45136. + * of the data with a short packet. However, if we are
  45137. + * allowed to stall, there was no data at all (residue ==
  45138. + * data_size), and the command failed (invalid LUN or
  45139. + * sense data is set), then halt the bulk-in endpoint
  45140. + * instead. */
  45141. + else if (!transport_is_bbb()) {
  45142. + if (mod_data.can_stall &&
  45143. + fsg->residue == fsg->data_size &&
  45144. + (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
  45145. + bh->state = BUF_STATE_EMPTY;
  45146. + rc = halt_bulk_in_endpoint(fsg);
  45147. + } else {
  45148. + bh->inreq->zero = 1;
  45149. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  45150. + &bh->inreq_busy, &bh->state);
  45151. + fsg->next_buffhd_to_fill = bh->next;
  45152. + }
  45153. + }
  45154. +
  45155. + /*
  45156. + * For Bulk-only, mark the end of the data with a short
  45157. + * packet. If we are allowed to stall, halt the bulk-in
  45158. + * endpoint. (Note: This violates the Bulk-Only Transport
  45159. + * specification, which requires us to pad the data if we
  45160. + * don't halt the endpoint. Presumably nobody will mind.)
  45161. + */
  45162. + else {
  45163. + bh->inreq->zero = 1;
  45164. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  45165. + &bh->inreq_busy, &bh->state);
  45166. + fsg->next_buffhd_to_fill = bh->next;
  45167. + if (mod_data.can_stall)
  45168. + rc = halt_bulk_in_endpoint(fsg);
  45169. + }
  45170. + break;
  45171. +
  45172. + /* We have processed all we want from the data the host has sent.
  45173. + * There may still be outstanding bulk-out requests. */
  45174. + case DATA_DIR_FROM_HOST:
  45175. + if (fsg->residue == 0)
  45176. + ; // Nothing to receive
  45177. +
  45178. + /* Did the host stop sending unexpectedly early? */
  45179. + else if (fsg->short_packet_received) {
  45180. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  45181. + rc = -EINTR;
  45182. + }
  45183. +
  45184. + /* We haven't processed all the incoming data. Even though
  45185. + * we may be allowed to stall, doing so would cause a race.
  45186. + * The controller may already have ACK'ed all the remaining
  45187. + * bulk-out packets, in which case the host wouldn't see a
  45188. + * STALL. Not realizing the endpoint was halted, it wouldn't
  45189. + * clear the halt -- leading to problems later on. */
  45190. +#if 0
  45191. + else if (mod_data.can_stall) {
  45192. + fsg_set_halt(fsg, fsg->bulk_out);
  45193. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  45194. + rc = -EINTR;
  45195. + }
  45196. +#endif
  45197. +
  45198. + /* We can't stall. Read in the excess data and throw it
  45199. + * all away. */
  45200. + else
  45201. + rc = throw_away_data(fsg);
  45202. + break;
  45203. + }
  45204. + return rc;
  45205. +}
  45206. +
  45207. +
  45208. +static int send_status(struct fsg_dev *fsg)
  45209. +{
  45210. + struct fsg_lun *curlun = fsg->curlun;
  45211. + struct fsg_buffhd *bh;
  45212. + int rc;
  45213. + u8 status = US_BULK_STAT_OK;
  45214. + u32 sd, sdinfo = 0;
  45215. +
  45216. + /* Wait for the next buffer to become available */
  45217. + bh = fsg->next_buffhd_to_fill;
  45218. + while (bh->state != BUF_STATE_EMPTY) {
  45219. + rc = sleep_thread(fsg);
  45220. + if (rc)
  45221. + return rc;
  45222. + }
  45223. +
  45224. + if (curlun) {
  45225. + sd = curlun->sense_data;
  45226. + sdinfo = curlun->sense_data_info;
  45227. + } else if (fsg->bad_lun_okay)
  45228. + sd = SS_NO_SENSE;
  45229. + else
  45230. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  45231. +
  45232. + if (fsg->phase_error) {
  45233. + DBG(fsg, "sending phase-error status\n");
  45234. + status = US_BULK_STAT_PHASE;
  45235. + sd = SS_INVALID_COMMAND;
  45236. + } else if (sd != SS_NO_SENSE) {
  45237. + DBG(fsg, "sending command-failure status\n");
  45238. + status = US_BULK_STAT_FAIL;
  45239. + VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
  45240. + " info x%x\n",
  45241. + SK(sd), ASC(sd), ASCQ(sd), sdinfo);
  45242. + }
  45243. +
  45244. + if (transport_is_bbb()) {
  45245. + struct bulk_cs_wrap *csw = bh->buf;
  45246. +
  45247. + /* Store and send the Bulk-only CSW */
  45248. + csw->Signature = cpu_to_le32(US_BULK_CS_SIGN);
  45249. + csw->Tag = fsg->tag;
  45250. + csw->Residue = cpu_to_le32(fsg->residue);
  45251. + csw->Status = status;
  45252. +
  45253. + bh->inreq->length = US_BULK_CS_WRAP_LEN;
  45254. + bh->inreq->zero = 0;
  45255. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  45256. + &bh->inreq_busy, &bh->state);
  45257. +
  45258. + } else if (mod_data.transport_type == USB_PR_CB) {
  45259. +
  45260. + /* Control-Bulk transport has no status phase! */
  45261. + return 0;
  45262. +
  45263. + } else { // USB_PR_CBI
  45264. + struct interrupt_data *buf = bh->buf;
  45265. +
  45266. + /* Store and send the Interrupt data. UFI sends the ASC
  45267. + * and ASCQ bytes. Everything else sends a Type (which
  45268. + * is always 0) and the status Value. */
  45269. + if (mod_data.protocol_type == USB_SC_UFI) {
  45270. + buf->bType = ASC(sd);
  45271. + buf->bValue = ASCQ(sd);
  45272. + } else {
  45273. + buf->bType = 0;
  45274. + buf->bValue = status;
  45275. + }
  45276. + fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
  45277. +
  45278. + fsg->intr_buffhd = bh; // Point to the right buffhd
  45279. + fsg->intreq->buf = bh->inreq->buf;
  45280. + fsg->intreq->context = bh;
  45281. + start_transfer(fsg, fsg->intr_in, fsg->intreq,
  45282. + &fsg->intreq_busy, &bh->state);
  45283. + }
  45284. +
  45285. + fsg->next_buffhd_to_fill = bh->next;
  45286. + return 0;
  45287. +}
  45288. +
  45289. +
  45290. +/*-------------------------------------------------------------------------*/
  45291. +
  45292. +/* Check whether the command is properly formed and whether its data size
  45293. + * and direction agree with the values we already have. */
  45294. +static int check_command(struct fsg_dev *fsg, int cmnd_size,
  45295. + enum data_direction data_dir, unsigned int mask,
  45296. + int needs_medium, const char *name)
  45297. +{
  45298. + int i;
  45299. + int lun = fsg->cmnd[1] >> 5;
  45300. + static const char dirletter[4] = {'u', 'o', 'i', 'n'};
  45301. + char hdlen[20];
  45302. + struct fsg_lun *curlun;
  45303. +
  45304. + /* Adjust the expected cmnd_size for protocol encapsulation padding.
  45305. + * Transparent SCSI doesn't pad. */
  45306. + if (protocol_is_scsi())
  45307. + ;
  45308. +
  45309. + /* There's some disagreement as to whether RBC pads commands or not.
  45310. + * We'll play it safe and accept either form. */
  45311. + else if (mod_data.protocol_type == USB_SC_RBC) {
  45312. + if (fsg->cmnd_size == 12)
  45313. + cmnd_size = 12;
  45314. +
  45315. + /* All the other protocols pad to 12 bytes */
  45316. + } else
  45317. + cmnd_size = 12;
  45318. +
  45319. + hdlen[0] = 0;
  45320. + if (fsg->data_dir != DATA_DIR_UNKNOWN)
  45321. + sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
  45322. + fsg->data_size);
  45323. + VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
  45324. + name, cmnd_size, dirletter[(int) data_dir],
  45325. + fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
  45326. +
  45327. + /* We can't reply at all until we know the correct data direction
  45328. + * and size. */
  45329. + if (fsg->data_size_from_cmnd == 0)
  45330. + data_dir = DATA_DIR_NONE;
  45331. + if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
  45332. + fsg->data_dir = data_dir;
  45333. + fsg->data_size = fsg->data_size_from_cmnd;
  45334. +
  45335. + } else { // Bulk-only
  45336. + if (fsg->data_size < fsg->data_size_from_cmnd) {
  45337. +
  45338. + /* Host data size < Device data size is a phase error.
  45339. + * Carry out the command, but only transfer as much
  45340. + * as we are allowed. */
  45341. + fsg->data_size_from_cmnd = fsg->data_size;
  45342. + fsg->phase_error = 1;
  45343. + }
  45344. + }
  45345. + fsg->residue = fsg->usb_amount_left = fsg->data_size;
  45346. +
  45347. + /* Conflicting data directions is a phase error */
  45348. + if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
  45349. + fsg->phase_error = 1;
  45350. + return -EINVAL;
  45351. + }
  45352. +
  45353. + /* Verify the length of the command itself */
  45354. + if (cmnd_size != fsg->cmnd_size) {
  45355. +
  45356. + /* Special case workaround: There are plenty of buggy SCSI
  45357. + * implementations. Many have issues with cbw->Length
  45358. + * field passing a wrong command size. For those cases we
  45359. + * always try to work around the problem by using the length
  45360. + * sent by the host side provided it is at least as large
  45361. + * as the correct command length.
  45362. + * Examples of such cases would be MS-Windows, which issues
  45363. + * REQUEST SENSE with cbw->Length == 12 where it should
  45364. + * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
  45365. + * REQUEST SENSE with cbw->Length == 10 where it should
  45366. + * be 6 as well.
  45367. + */
  45368. + if (cmnd_size <= fsg->cmnd_size) {
  45369. + DBG(fsg, "%s is buggy! Expected length %d "
  45370. + "but we got %d\n", name,
  45371. + cmnd_size, fsg->cmnd_size);
  45372. + cmnd_size = fsg->cmnd_size;
  45373. + } else {
  45374. + fsg->phase_error = 1;
  45375. + return -EINVAL;
  45376. + }
  45377. + }
  45378. +
  45379. + /* Check that the LUN values are consistent */
  45380. + if (transport_is_bbb()) {
  45381. + if (fsg->lun != lun)
  45382. + DBG(fsg, "using LUN %d from CBW, "
  45383. + "not LUN %d from CDB\n",
  45384. + fsg->lun, lun);
  45385. + }
  45386. +
  45387. + /* Check the LUN */
  45388. + curlun = fsg->curlun;
  45389. + if (curlun) {
  45390. + if (fsg->cmnd[0] != REQUEST_SENSE) {
  45391. + curlun->sense_data = SS_NO_SENSE;
  45392. + curlun->sense_data_info = 0;
  45393. + curlun->info_valid = 0;
  45394. + }
  45395. + } else {
  45396. + fsg->bad_lun_okay = 0;
  45397. +
  45398. + /* INQUIRY and REQUEST SENSE commands are explicitly allowed
  45399. + * to use unsupported LUNs; all others may not. */
  45400. + if (fsg->cmnd[0] != INQUIRY &&
  45401. + fsg->cmnd[0] != REQUEST_SENSE) {
  45402. + DBG(fsg, "unsupported LUN %d\n", fsg->lun);
  45403. + return -EINVAL;
  45404. + }
  45405. + }
  45406. +
  45407. + /* If a unit attention condition exists, only INQUIRY and
  45408. + * REQUEST SENSE commands are allowed; anything else must fail. */
  45409. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
  45410. + fsg->cmnd[0] != INQUIRY &&
  45411. + fsg->cmnd[0] != REQUEST_SENSE) {
  45412. + curlun->sense_data = curlun->unit_attention_data;
  45413. + curlun->unit_attention_data = SS_NO_SENSE;
  45414. + return -EINVAL;
  45415. + }
  45416. +
  45417. + /* Check that only command bytes listed in the mask are non-zero */
  45418. + fsg->cmnd[1] &= 0x1f; // Mask away the LUN
  45419. + for (i = 1; i < cmnd_size; ++i) {
  45420. + if (fsg->cmnd[i] && !(mask & (1 << i))) {
  45421. + if (curlun)
  45422. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  45423. + return -EINVAL;
  45424. + }
  45425. + }
  45426. +
  45427. + /* If the medium isn't mounted and the command needs to access
  45428. + * it, return an error. */
  45429. + if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
  45430. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  45431. + return -EINVAL;
  45432. + }
  45433. +
  45434. + return 0;
  45435. +}
  45436. +
  45437. +/* wrapper of check_command for data size in blocks handling */
  45438. +static int check_command_size_in_blocks(struct fsg_dev *fsg, int cmnd_size,
  45439. + enum data_direction data_dir, unsigned int mask,
  45440. + int needs_medium, const char *name)
  45441. +{
  45442. + if (fsg->curlun)
  45443. + fsg->data_size_from_cmnd <<= fsg->curlun->blkbits;
  45444. + return check_command(fsg, cmnd_size, data_dir,
  45445. + mask, needs_medium, name);
  45446. +}
  45447. +
  45448. +static int do_scsi_command(struct fsg_dev *fsg)
  45449. +{
  45450. + struct fsg_buffhd *bh;
  45451. + int rc;
  45452. + int reply = -EINVAL;
  45453. + int i;
  45454. + static char unknown[16];
  45455. +
  45456. + dump_cdb(fsg);
  45457. +
  45458. + /* Wait for the next buffer to become available for data or status */
  45459. + bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
  45460. + while (bh->state != BUF_STATE_EMPTY) {
  45461. + rc = sleep_thread(fsg);
  45462. + if (rc)
  45463. + return rc;
  45464. + }
  45465. + fsg->phase_error = 0;
  45466. + fsg->short_packet_received = 0;
  45467. +
  45468. + down_read(&fsg->filesem); // We're using the backing file
  45469. + switch (fsg->cmnd[0]) {
  45470. +
  45471. + case INQUIRY:
  45472. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  45473. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  45474. + (1<<4), 0,
  45475. + "INQUIRY")) == 0)
  45476. + reply = do_inquiry(fsg, bh);
  45477. + break;
  45478. +
  45479. + case MODE_SELECT:
  45480. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  45481. + if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
  45482. + (1<<1) | (1<<4), 0,
  45483. + "MODE SELECT(6)")) == 0)
  45484. + reply = do_mode_select(fsg, bh);
  45485. + break;
  45486. +
  45487. + case MODE_SELECT_10:
  45488. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  45489. + if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
  45490. + (1<<1) | (3<<7), 0,
  45491. + "MODE SELECT(10)")) == 0)
  45492. + reply = do_mode_select(fsg, bh);
  45493. + break;
  45494. +
  45495. + case MODE_SENSE:
  45496. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  45497. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  45498. + (1<<1) | (1<<2) | (1<<4), 0,
  45499. + "MODE SENSE(6)")) == 0)
  45500. + reply = do_mode_sense(fsg, bh);
  45501. + break;
  45502. +
  45503. + case MODE_SENSE_10:
  45504. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  45505. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  45506. + (1<<1) | (1<<2) | (3<<7), 0,
  45507. + "MODE SENSE(10)")) == 0)
  45508. + reply = do_mode_sense(fsg, bh);
  45509. + break;
  45510. +
  45511. + case ALLOW_MEDIUM_REMOVAL:
  45512. + fsg->data_size_from_cmnd = 0;
  45513. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  45514. + (1<<4), 0,
  45515. + "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
  45516. + reply = do_prevent_allow(fsg);
  45517. + break;
  45518. +
  45519. + case READ_6:
  45520. + i = fsg->cmnd[4];
  45521. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  45522. + if ((reply = check_command_size_in_blocks(fsg, 6,
  45523. + DATA_DIR_TO_HOST,
  45524. + (7<<1) | (1<<4), 1,
  45525. + "READ(6)")) == 0)
  45526. + reply = do_read(fsg);
  45527. + break;
  45528. +
  45529. + case READ_10:
  45530. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  45531. + if ((reply = check_command_size_in_blocks(fsg, 10,
  45532. + DATA_DIR_TO_HOST,
  45533. + (1<<1) | (0xf<<2) | (3<<7), 1,
  45534. + "READ(10)")) == 0)
  45535. + reply = do_read(fsg);
  45536. + break;
  45537. +
  45538. + case READ_12:
  45539. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  45540. + if ((reply = check_command_size_in_blocks(fsg, 12,
  45541. + DATA_DIR_TO_HOST,
  45542. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  45543. + "READ(12)")) == 0)
  45544. + reply = do_read(fsg);
  45545. + break;
  45546. +
  45547. + case READ_CAPACITY:
  45548. + fsg->data_size_from_cmnd = 8;
  45549. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  45550. + (0xf<<2) | (1<<8), 1,
  45551. + "READ CAPACITY")) == 0)
  45552. + reply = do_read_capacity(fsg, bh);
  45553. + break;
  45554. +
  45555. + case READ_HEADER:
  45556. + if (!mod_data.cdrom)
  45557. + goto unknown_cmnd;
  45558. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  45559. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  45560. + (3<<7) | (0x1f<<1), 1,
  45561. + "READ HEADER")) == 0)
  45562. + reply = do_read_header(fsg, bh);
  45563. + break;
  45564. +
  45565. + case READ_TOC:
  45566. + if (!mod_data.cdrom)
  45567. + goto unknown_cmnd;
  45568. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  45569. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  45570. + (7<<6) | (1<<1), 1,
  45571. + "READ TOC")) == 0)
  45572. + reply = do_read_toc(fsg, bh);
  45573. + break;
  45574. +
  45575. + case READ_FORMAT_CAPACITIES:
  45576. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  45577. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  45578. + (3<<7), 1,
  45579. + "READ FORMAT CAPACITIES")) == 0)
  45580. + reply = do_read_format_capacities(fsg, bh);
  45581. + break;
  45582. +
  45583. + case REQUEST_SENSE:
  45584. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  45585. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  45586. + (1<<4), 0,
  45587. + "REQUEST SENSE")) == 0)
  45588. + reply = do_request_sense(fsg, bh);
  45589. + break;
  45590. +
  45591. + case START_STOP:
  45592. + fsg->data_size_from_cmnd = 0;
  45593. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  45594. + (1<<1) | (1<<4), 0,
  45595. + "START-STOP UNIT")) == 0)
  45596. + reply = do_start_stop(fsg);
  45597. + break;
  45598. +
  45599. + case SYNCHRONIZE_CACHE:
  45600. + fsg->data_size_from_cmnd = 0;
  45601. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  45602. + (0xf<<2) | (3<<7), 1,
  45603. + "SYNCHRONIZE CACHE")) == 0)
  45604. + reply = do_synchronize_cache(fsg);
  45605. + break;
  45606. +
  45607. + case TEST_UNIT_READY:
  45608. + fsg->data_size_from_cmnd = 0;
  45609. + reply = check_command(fsg, 6, DATA_DIR_NONE,
  45610. + 0, 1,
  45611. + "TEST UNIT READY");
  45612. + break;
  45613. +
  45614. + /* Although optional, this command is used by MS-Windows. We
  45615. + * support a minimal version: BytChk must be 0. */
  45616. + case VERIFY:
  45617. + fsg->data_size_from_cmnd = 0;
  45618. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  45619. + (1<<1) | (0xf<<2) | (3<<7), 1,
  45620. + "VERIFY")) == 0)
  45621. + reply = do_verify(fsg);
  45622. + break;
  45623. +
  45624. + case WRITE_6:
  45625. + i = fsg->cmnd[4];
  45626. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  45627. + if ((reply = check_command_size_in_blocks(fsg, 6,
  45628. + DATA_DIR_FROM_HOST,
  45629. + (7<<1) | (1<<4), 1,
  45630. + "WRITE(6)")) == 0)
  45631. + reply = do_write(fsg);
  45632. + break;
  45633. +
  45634. + case WRITE_10:
  45635. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  45636. + if ((reply = check_command_size_in_blocks(fsg, 10,
  45637. + DATA_DIR_FROM_HOST,
  45638. + (1<<1) | (0xf<<2) | (3<<7), 1,
  45639. + "WRITE(10)")) == 0)
  45640. + reply = do_write(fsg);
  45641. + break;
  45642. +
  45643. + case WRITE_12:
  45644. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  45645. + if ((reply = check_command_size_in_blocks(fsg, 12,
  45646. + DATA_DIR_FROM_HOST,
  45647. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  45648. + "WRITE(12)")) == 0)
  45649. + reply = do_write(fsg);
  45650. + break;
  45651. +
  45652. + /* Some mandatory commands that we recognize but don't implement.
  45653. + * They don't mean much in this setting. It's left as an exercise
  45654. + * for anyone interested to implement RESERVE and RELEASE in terms
  45655. + * of Posix locks. */
  45656. + case FORMAT_UNIT:
  45657. + case RELEASE:
  45658. + case RESERVE:
  45659. + case SEND_DIAGNOSTIC:
  45660. + // Fall through
  45661. +
  45662. + default:
  45663. + unknown_cmnd:
  45664. + fsg->data_size_from_cmnd = 0;
  45665. + sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
  45666. + if ((reply = check_command(fsg, fsg->cmnd_size,
  45667. + DATA_DIR_UNKNOWN, ~0, 0, unknown)) == 0) {
  45668. + fsg->curlun->sense_data = SS_INVALID_COMMAND;
  45669. + reply = -EINVAL;
  45670. + }
  45671. + break;
  45672. + }
  45673. + up_read(&fsg->filesem);
  45674. +
  45675. + if (reply == -EINTR || signal_pending(current))
  45676. + return -EINTR;
  45677. +
  45678. + /* Set up the single reply buffer for finish_reply() */
  45679. + if (reply == -EINVAL)
  45680. + reply = 0; // Error reply length
  45681. + if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
  45682. + reply = min((u32) reply, fsg->data_size_from_cmnd);
  45683. + bh->inreq->length = reply;
  45684. + bh->state = BUF_STATE_FULL;
  45685. + fsg->residue -= reply;
  45686. + } // Otherwise it's already set
  45687. +
  45688. + return 0;
  45689. +}
  45690. +
  45691. +
  45692. +/*-------------------------------------------------------------------------*/
  45693. +
  45694. +static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  45695. +{
  45696. + struct usb_request *req = bh->outreq;
  45697. + struct bulk_cb_wrap *cbw = req->buf;
  45698. +
  45699. + /* Was this a real packet? Should it be ignored? */
  45700. + if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  45701. + return -EINVAL;
  45702. +
  45703. + /* Is the CBW valid? */
  45704. + if (req->actual != US_BULK_CB_WRAP_LEN ||
  45705. + cbw->Signature != cpu_to_le32(
  45706. + US_BULK_CB_SIGN)) {
  45707. + DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
  45708. + req->actual,
  45709. + le32_to_cpu(cbw->Signature));
  45710. +
  45711. + /* The Bulk-only spec says we MUST stall the IN endpoint
  45712. + * (6.6.1), so it's unavoidable. It also says we must
  45713. + * retain this state until the next reset, but there's
  45714. + * no way to tell the controller driver it should ignore
  45715. + * Clear-Feature(HALT) requests.
  45716. + *
  45717. + * We aren't required to halt the OUT endpoint; instead
  45718. + * we can simply accept and discard any data received
  45719. + * until the next reset. */
  45720. + wedge_bulk_in_endpoint(fsg);
  45721. + set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  45722. + return -EINVAL;
  45723. + }
  45724. +
  45725. + /* Is the CBW meaningful? */
  45726. + if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~US_BULK_FLAG_IN ||
  45727. + cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
  45728. + DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
  45729. + "cmdlen %u\n",
  45730. + cbw->Lun, cbw->Flags, cbw->Length);
  45731. +
  45732. + /* We can do anything we want here, so let's stall the
  45733. + * bulk pipes if we are allowed to. */
  45734. + if (mod_data.can_stall) {
  45735. + fsg_set_halt(fsg, fsg->bulk_out);
  45736. + halt_bulk_in_endpoint(fsg);
  45737. + }
  45738. + return -EINVAL;
  45739. + }
  45740. +
  45741. + /* Save the command for later */
  45742. + fsg->cmnd_size = cbw->Length;
  45743. + memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
  45744. + if (cbw->Flags & US_BULK_FLAG_IN)
  45745. + fsg->data_dir = DATA_DIR_TO_HOST;
  45746. + else
  45747. + fsg->data_dir = DATA_DIR_FROM_HOST;
  45748. + fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
  45749. + if (fsg->data_size == 0)
  45750. + fsg->data_dir = DATA_DIR_NONE;
  45751. + fsg->lun = cbw->Lun;
  45752. + fsg->tag = cbw->Tag;
  45753. + return 0;
  45754. +}
  45755. +
  45756. +
  45757. +static int get_next_command(struct fsg_dev *fsg)
  45758. +{
  45759. + struct fsg_buffhd *bh;
  45760. + int rc = 0;
  45761. +
  45762. + if (transport_is_bbb()) {
  45763. +
  45764. + /* Wait for the next buffer to become available */
  45765. + bh = fsg->next_buffhd_to_fill;
  45766. + while (bh->state != BUF_STATE_EMPTY) {
  45767. + rc = sleep_thread(fsg);
  45768. + if (rc)
  45769. + return rc;
  45770. + }
  45771. +
  45772. + /* Queue a request to read a Bulk-only CBW */
  45773. + set_bulk_out_req_length(fsg, bh, US_BULK_CB_WRAP_LEN);
  45774. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  45775. + &bh->outreq_busy, &bh->state);
  45776. +
  45777. + /* We will drain the buffer in software, which means we
  45778. + * can reuse it for the next filling. No need to advance
  45779. + * next_buffhd_to_fill. */
  45780. +
  45781. + /* Wait for the CBW to arrive */
  45782. + while (bh->state != BUF_STATE_FULL) {
  45783. + rc = sleep_thread(fsg);
  45784. + if (rc)
  45785. + return rc;
  45786. + }
  45787. + smp_rmb();
  45788. + rc = received_cbw(fsg, bh);
  45789. + bh->state = BUF_STATE_EMPTY;
  45790. +
  45791. + } else { // USB_PR_CB or USB_PR_CBI
  45792. +
  45793. + /* Wait for the next command to arrive */
  45794. + while (fsg->cbbuf_cmnd_size == 0) {
  45795. + rc = sleep_thread(fsg);
  45796. + if (rc)
  45797. + return rc;
  45798. + }
  45799. +
  45800. + /* Is the previous status interrupt request still busy?
  45801. + * The host is allowed to skip reading the status,
  45802. + * so we must cancel it. */
  45803. + if (fsg->intreq_busy)
  45804. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  45805. +
  45806. + /* Copy the command and mark the buffer empty */
  45807. + fsg->data_dir = DATA_DIR_UNKNOWN;
  45808. + spin_lock_irq(&fsg->lock);
  45809. + fsg->cmnd_size = fsg->cbbuf_cmnd_size;
  45810. + memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
  45811. + fsg->cbbuf_cmnd_size = 0;
  45812. + spin_unlock_irq(&fsg->lock);
  45813. +
  45814. + /* Use LUN from the command */
  45815. + fsg->lun = fsg->cmnd[1] >> 5;
  45816. + }
  45817. +
  45818. + /* Update current lun */
  45819. + if (fsg->lun >= 0 && fsg->lun < fsg->nluns)
  45820. + fsg->curlun = &fsg->luns[fsg->lun];
  45821. + else
  45822. + fsg->curlun = NULL;
  45823. +
  45824. + return rc;
  45825. +}
  45826. +
  45827. +
  45828. +/*-------------------------------------------------------------------------*/
  45829. +
  45830. +static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
  45831. + const struct usb_endpoint_descriptor *d)
  45832. +{
  45833. + int rc;
  45834. +
  45835. + ep->driver_data = fsg;
  45836. + ep->desc = d;
  45837. + rc = usb_ep_enable(ep);
  45838. + if (rc)
  45839. + ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
  45840. + return rc;
  45841. +}
  45842. +
  45843. +static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
  45844. + struct usb_request **preq)
  45845. +{
  45846. + *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
  45847. + if (*preq)
  45848. + return 0;
  45849. + ERROR(fsg, "can't allocate request for %s\n", ep->name);
  45850. + return -ENOMEM;
  45851. +}
  45852. +
  45853. +/*
  45854. + * Reset interface setting and re-init endpoint state (toggle etc).
  45855. + * Call with altsetting < 0 to disable the interface. The only other
  45856. + * available altsetting is 0, which enables the interface.
  45857. + */
  45858. +static int do_set_interface(struct fsg_dev *fsg, int altsetting)
  45859. +{
  45860. + int rc = 0;
  45861. + int i;
  45862. + const struct usb_endpoint_descriptor *d;
  45863. +
  45864. + if (fsg->running)
  45865. + DBG(fsg, "reset interface\n");
  45866. +
  45867. +reset:
  45868. + /* Deallocate the requests */
  45869. + for (i = 0; i < fsg_num_buffers; ++i) {
  45870. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  45871. +
  45872. + if (bh->inreq) {
  45873. + usb_ep_free_request(fsg->bulk_in, bh->inreq);
  45874. + bh->inreq = NULL;
  45875. + }
  45876. + if (bh->outreq) {
  45877. + usb_ep_free_request(fsg->bulk_out, bh->outreq);
  45878. + bh->outreq = NULL;
  45879. + }
  45880. + }
  45881. + if (fsg->intreq) {
  45882. + usb_ep_free_request(fsg->intr_in, fsg->intreq);
  45883. + fsg->intreq = NULL;
  45884. + }
  45885. +
  45886. + /* Disable the endpoints */
  45887. + if (fsg->bulk_in_enabled) {
  45888. + usb_ep_disable(fsg->bulk_in);
  45889. + fsg->bulk_in_enabled = 0;
  45890. + }
  45891. + if (fsg->bulk_out_enabled) {
  45892. + usb_ep_disable(fsg->bulk_out);
  45893. + fsg->bulk_out_enabled = 0;
  45894. + }
  45895. + if (fsg->intr_in_enabled) {
  45896. + usb_ep_disable(fsg->intr_in);
  45897. + fsg->intr_in_enabled = 0;
  45898. + }
  45899. +
  45900. + fsg->running = 0;
  45901. + if (altsetting < 0 || rc != 0)
  45902. + return rc;
  45903. +
  45904. + DBG(fsg, "set interface %d\n", altsetting);
  45905. +
  45906. + /* Enable the endpoints */
  45907. + d = fsg_ep_desc(fsg->gadget,
  45908. + &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc,
  45909. + &fsg_ss_bulk_in_desc);
  45910. + if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
  45911. + goto reset;
  45912. + fsg->bulk_in_enabled = 1;
  45913. +
  45914. + d = fsg_ep_desc(fsg->gadget,
  45915. + &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc,
  45916. + &fsg_ss_bulk_out_desc);
  45917. + if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
  45918. + goto reset;
  45919. + fsg->bulk_out_enabled = 1;
  45920. + fsg->bulk_out_maxpacket = usb_endpoint_maxp(d);
  45921. + clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  45922. +
  45923. + if (transport_is_cbi()) {
  45924. + d = fsg_ep_desc(fsg->gadget,
  45925. + &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc,
  45926. + &fsg_ss_intr_in_desc);
  45927. + if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
  45928. + goto reset;
  45929. + fsg->intr_in_enabled = 1;
  45930. + }
  45931. +
  45932. + /* Allocate the requests */
  45933. + for (i = 0; i < fsg_num_buffers; ++i) {
  45934. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  45935. +
  45936. + if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
  45937. + goto reset;
  45938. + if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
  45939. + goto reset;
  45940. + bh->inreq->buf = bh->outreq->buf = bh->buf;
  45941. + bh->inreq->context = bh->outreq->context = bh;
  45942. + bh->inreq->complete = bulk_in_complete;
  45943. + bh->outreq->complete = bulk_out_complete;
  45944. + }
  45945. + if (transport_is_cbi()) {
  45946. + if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
  45947. + goto reset;
  45948. + fsg->intreq->complete = intr_in_complete;
  45949. + }
  45950. +
  45951. + fsg->running = 1;
  45952. + for (i = 0; i < fsg->nluns; ++i)
  45953. + fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  45954. + return rc;
  45955. +}
  45956. +
  45957. +
  45958. +/*
  45959. + * Change our operational configuration. This code must agree with the code
  45960. + * that returns config descriptors, and with interface altsetting code.
  45961. + *
  45962. + * It's also responsible for power management interactions. Some
  45963. + * configurations might not work with our current power sources.
  45964. + * For now we just assume the gadget is always self-powered.
  45965. + */
  45966. +static int do_set_config(struct fsg_dev *fsg, u8 new_config)
  45967. +{
  45968. + int rc = 0;
  45969. +
  45970. + /* Disable the single interface */
  45971. + if (fsg->config != 0) {
  45972. + DBG(fsg, "reset config\n");
  45973. + fsg->config = 0;
  45974. + rc = do_set_interface(fsg, -1);
  45975. + }
  45976. +
  45977. + /* Enable the interface */
  45978. + if (new_config != 0) {
  45979. + fsg->config = new_config;
  45980. + if ((rc = do_set_interface(fsg, 0)) != 0)
  45981. + fsg->config = 0; // Reset on errors
  45982. + else
  45983. + INFO(fsg, "%s config #%d\n",
  45984. + usb_speed_string(fsg->gadget->speed),
  45985. + fsg->config);
  45986. + }
  45987. + return rc;
  45988. +}
  45989. +
  45990. +
  45991. +/*-------------------------------------------------------------------------*/
  45992. +
  45993. +static void handle_exception(struct fsg_dev *fsg)
  45994. +{
  45995. + siginfo_t info;
  45996. + int sig;
  45997. + int i;
  45998. + int num_active;
  45999. + struct fsg_buffhd *bh;
  46000. + enum fsg_state old_state;
  46001. + u8 new_config;
  46002. + struct fsg_lun *curlun;
  46003. + unsigned int exception_req_tag;
  46004. + int rc;
  46005. +
  46006. + /* Clear the existing signals. Anything but SIGUSR1 is converted
  46007. + * into a high-priority EXIT exception. */
  46008. + for (;;) {
  46009. + sig = dequeue_signal_lock(current, &current->blocked, &info);
  46010. + if (!sig)
  46011. + break;
  46012. + if (sig != SIGUSR1) {
  46013. + if (fsg->state < FSG_STATE_EXIT)
  46014. + DBG(fsg, "Main thread exiting on signal\n");
  46015. + raise_exception(fsg, FSG_STATE_EXIT);
  46016. + }
  46017. + }
  46018. +
  46019. + /* Cancel all the pending transfers */
  46020. + if (fsg->intreq_busy)
  46021. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  46022. + for (i = 0; i < fsg_num_buffers; ++i) {
  46023. + bh = &fsg->buffhds[i];
  46024. + if (bh->inreq_busy)
  46025. + usb_ep_dequeue(fsg->bulk_in, bh->inreq);
  46026. + if (bh->outreq_busy)
  46027. + usb_ep_dequeue(fsg->bulk_out, bh->outreq);
  46028. + }
  46029. +
  46030. + /* Wait until everything is idle */
  46031. + for (;;) {
  46032. + num_active = fsg->intreq_busy;
  46033. + for (i = 0; i < fsg_num_buffers; ++i) {
  46034. + bh = &fsg->buffhds[i];
  46035. + num_active += bh->inreq_busy + bh->outreq_busy;
  46036. + }
  46037. + if (num_active == 0)
  46038. + break;
  46039. + if (sleep_thread(fsg))
  46040. + return;
  46041. + }
  46042. +
  46043. + /* Clear out the controller's fifos */
  46044. + if (fsg->bulk_in_enabled)
  46045. + usb_ep_fifo_flush(fsg->bulk_in);
  46046. + if (fsg->bulk_out_enabled)
  46047. + usb_ep_fifo_flush(fsg->bulk_out);
  46048. + if (fsg->intr_in_enabled)
  46049. + usb_ep_fifo_flush(fsg->intr_in);
  46050. +
  46051. + /* Reset the I/O buffer states and pointers, the SCSI
  46052. + * state, and the exception. Then invoke the handler. */
  46053. + spin_lock_irq(&fsg->lock);
  46054. +
  46055. + for (i = 0; i < fsg_num_buffers; ++i) {
  46056. + bh = &fsg->buffhds[i];
  46057. + bh->state = BUF_STATE_EMPTY;
  46058. + }
  46059. + fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
  46060. + &fsg->buffhds[0];
  46061. +
  46062. + exception_req_tag = fsg->exception_req_tag;
  46063. + new_config = fsg->new_config;
  46064. + old_state = fsg->state;
  46065. +
  46066. + if (old_state == FSG_STATE_ABORT_BULK_OUT)
  46067. + fsg->state = FSG_STATE_STATUS_PHASE;
  46068. + else {
  46069. + for (i = 0; i < fsg->nluns; ++i) {
  46070. + curlun = &fsg->luns[i];
  46071. + curlun->prevent_medium_removal = 0;
  46072. + curlun->sense_data = curlun->unit_attention_data =
  46073. + SS_NO_SENSE;
  46074. + curlun->sense_data_info = 0;
  46075. + curlun->info_valid = 0;
  46076. + }
  46077. + fsg->state = FSG_STATE_IDLE;
  46078. + }
  46079. + spin_unlock_irq(&fsg->lock);
  46080. +
  46081. + /* Carry out any extra actions required for the exception */
  46082. + switch (old_state) {
  46083. + default:
  46084. + break;
  46085. +
  46086. + case FSG_STATE_ABORT_BULK_OUT:
  46087. + send_status(fsg);
  46088. + spin_lock_irq(&fsg->lock);
  46089. + if (fsg->state == FSG_STATE_STATUS_PHASE)
  46090. + fsg->state = FSG_STATE_IDLE;
  46091. + spin_unlock_irq(&fsg->lock);
  46092. + break;
  46093. +
  46094. + case FSG_STATE_RESET:
  46095. + /* In case we were forced against our will to halt a
  46096. + * bulk endpoint, clear the halt now. (The SuperH UDC
  46097. + * requires this.) */
  46098. + if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  46099. + usb_ep_clear_halt(fsg->bulk_in);
  46100. +
  46101. + if (transport_is_bbb()) {
  46102. + if (fsg->ep0_req_tag == exception_req_tag)
  46103. + ep0_queue(fsg); // Complete the status stage
  46104. +
  46105. + } else if (transport_is_cbi())
  46106. + send_status(fsg); // Status by interrupt pipe
  46107. +
  46108. + /* Technically this should go here, but it would only be
  46109. + * a waste of time. Ditto for the INTERFACE_CHANGE and
  46110. + * CONFIG_CHANGE cases. */
  46111. + // for (i = 0; i < fsg->nluns; ++i)
  46112. + // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  46113. + break;
  46114. +
  46115. + case FSG_STATE_INTERFACE_CHANGE:
  46116. + rc = do_set_interface(fsg, 0);
  46117. + if (fsg->ep0_req_tag != exception_req_tag)
  46118. + break;
  46119. + if (rc != 0) // STALL on errors
  46120. + fsg_set_halt(fsg, fsg->ep0);
  46121. + else // Complete the status stage
  46122. + ep0_queue(fsg);
  46123. + break;
  46124. +
  46125. + case FSG_STATE_CONFIG_CHANGE:
  46126. + rc = do_set_config(fsg, new_config);
  46127. + if (fsg->ep0_req_tag != exception_req_tag)
  46128. + break;
  46129. + if (rc != 0) // STALL on errors
  46130. + fsg_set_halt(fsg, fsg->ep0);
  46131. + else // Complete the status stage
  46132. + ep0_queue(fsg);
  46133. + break;
  46134. +
  46135. + case FSG_STATE_DISCONNECT:
  46136. + for (i = 0; i < fsg->nluns; ++i)
  46137. + fsg_lun_fsync_sub(fsg->luns + i);
  46138. + do_set_config(fsg, 0); // Unconfigured state
  46139. + break;
  46140. +
  46141. + case FSG_STATE_EXIT:
  46142. + case FSG_STATE_TERMINATED:
  46143. + do_set_config(fsg, 0); // Free resources
  46144. + spin_lock_irq(&fsg->lock);
  46145. + fsg->state = FSG_STATE_TERMINATED; // Stop the thread
  46146. + spin_unlock_irq(&fsg->lock);
  46147. + break;
  46148. + }
  46149. +}
  46150. +
  46151. +
  46152. +/*-------------------------------------------------------------------------*/
  46153. +
  46154. +static int fsg_main_thread(void *fsg_)
  46155. +{
  46156. + struct fsg_dev *fsg = fsg_;
  46157. +
  46158. + /* Allow the thread to be killed by a signal, but set the signal mask
  46159. + * to block everything but INT, TERM, KILL, and USR1. */
  46160. + allow_signal(SIGINT);
  46161. + allow_signal(SIGTERM);
  46162. + allow_signal(SIGKILL);
  46163. + allow_signal(SIGUSR1);
  46164. +
  46165. + /* Allow the thread to be frozen */
  46166. + set_freezable();
  46167. +
  46168. + /* Arrange for userspace references to be interpreted as kernel
  46169. + * pointers. That way we can pass a kernel pointer to a routine
  46170. + * that expects a __user pointer and it will work okay. */
  46171. + set_fs(get_ds());
  46172. +
  46173. + /* The main loop */
  46174. + while (fsg->state != FSG_STATE_TERMINATED) {
  46175. + if (exception_in_progress(fsg) || signal_pending(current)) {
  46176. + handle_exception(fsg);
  46177. + continue;
  46178. + }
  46179. +
  46180. + if (!fsg->running) {
  46181. + sleep_thread(fsg);
  46182. + continue;
  46183. + }
  46184. +
  46185. + if (get_next_command(fsg))
  46186. + continue;
  46187. +
  46188. + spin_lock_irq(&fsg->lock);
  46189. + if (!exception_in_progress(fsg))
  46190. + fsg->state = FSG_STATE_DATA_PHASE;
  46191. + spin_unlock_irq(&fsg->lock);
  46192. +
  46193. + if (do_scsi_command(fsg) || finish_reply(fsg))
  46194. + continue;
  46195. +
  46196. + spin_lock_irq(&fsg->lock);
  46197. + if (!exception_in_progress(fsg))
  46198. + fsg->state = FSG_STATE_STATUS_PHASE;
  46199. + spin_unlock_irq(&fsg->lock);
  46200. +
  46201. + if (send_status(fsg))
  46202. + continue;
  46203. +
  46204. + spin_lock_irq(&fsg->lock);
  46205. + if (!exception_in_progress(fsg))
  46206. + fsg->state = FSG_STATE_IDLE;
  46207. + spin_unlock_irq(&fsg->lock);
  46208. + }
  46209. +
  46210. + spin_lock_irq(&fsg->lock);
  46211. + fsg->thread_task = NULL;
  46212. + spin_unlock_irq(&fsg->lock);
  46213. +
  46214. + /* If we are exiting because of a signal, unregister the
  46215. + * gadget driver. */
  46216. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  46217. + usb_gadget_unregister_driver(&fsg_driver);
  46218. +
  46219. + /* Let the unbind and cleanup routines know the thread has exited */
  46220. + complete_and_exit(&fsg->thread_notifier, 0);
  46221. +}
  46222. +
  46223. +
  46224. +/*-------------------------------------------------------------------------*/
  46225. +
  46226. +
  46227. +/* The write permissions and store_xxx pointers are set in fsg_bind() */
  46228. +static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
  46229. +static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
  46230. +static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
  46231. +
  46232. +
  46233. +/*-------------------------------------------------------------------------*/
  46234. +
  46235. +static void fsg_release(struct kref *ref)
  46236. +{
  46237. + struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
  46238. +
  46239. + kfree(fsg->luns);
  46240. + kfree(fsg);
  46241. +}
  46242. +
  46243. +static void lun_release(struct device *dev)
  46244. +{
  46245. + struct rw_semaphore *filesem = dev_get_drvdata(dev);
  46246. + struct fsg_dev *fsg =
  46247. + container_of(filesem, struct fsg_dev, filesem);
  46248. +
  46249. + kref_put(&fsg->ref, fsg_release);
  46250. +}
  46251. +
  46252. +static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
  46253. +{
  46254. + struct fsg_dev *fsg = get_gadget_data(gadget);
  46255. + int i;
  46256. + struct fsg_lun *curlun;
  46257. + struct usb_request *req = fsg->ep0req;
  46258. +
  46259. + DBG(fsg, "unbind\n");
  46260. + clear_bit(REGISTERED, &fsg->atomic_bitflags);
  46261. +
  46262. + /* If the thread isn't already dead, tell it to exit now */
  46263. + if (fsg->state != FSG_STATE_TERMINATED) {
  46264. + raise_exception(fsg, FSG_STATE_EXIT);
  46265. + wait_for_completion(&fsg->thread_notifier);
  46266. +
  46267. + /* The cleanup routine waits for this completion also */
  46268. + complete(&fsg->thread_notifier);
  46269. + }
  46270. +
  46271. + /* Unregister the sysfs attribute files and the LUNs */
  46272. + for (i = 0; i < fsg->nluns; ++i) {
  46273. + curlun = &fsg->luns[i];
  46274. + if (curlun->registered) {
  46275. + device_remove_file(&curlun->dev, &dev_attr_nofua);
  46276. + device_remove_file(&curlun->dev, &dev_attr_ro);
  46277. + device_remove_file(&curlun->dev, &dev_attr_file);
  46278. + fsg_lun_close(curlun);
  46279. + device_unregister(&curlun->dev);
  46280. + curlun->registered = 0;
  46281. + }
  46282. + }
  46283. +
  46284. + /* Free the data buffers */
  46285. + for (i = 0; i < fsg_num_buffers; ++i)
  46286. + kfree(fsg->buffhds[i].buf);
  46287. +
  46288. + /* Free the request and buffer for endpoint 0 */
  46289. + if (req) {
  46290. + kfree(req->buf);
  46291. + usb_ep_free_request(fsg->ep0, req);
  46292. + }
  46293. +
  46294. + set_gadget_data(gadget, NULL);
  46295. +}
  46296. +
  46297. +
  46298. +static int __init check_parameters(struct fsg_dev *fsg)
  46299. +{
  46300. + int prot;
  46301. + int gcnum;
  46302. +
  46303. + /* Store the default values */
  46304. + mod_data.transport_type = USB_PR_BULK;
  46305. + mod_data.transport_name = "Bulk-only";
  46306. + mod_data.protocol_type = USB_SC_SCSI;
  46307. + mod_data.protocol_name = "Transparent SCSI";
  46308. +
  46309. + /* Some peripheral controllers are known not to be able to
  46310. + * halt bulk endpoints correctly. If one of them is present,
  46311. + * disable stalls.
  46312. + */
  46313. + if (gadget_is_at91(fsg->gadget))
  46314. + mod_data.can_stall = 0;
  46315. +
  46316. + if (mod_data.release == 0xffff) { // Parameter wasn't set
  46317. + gcnum = usb_gadget_controller_number(fsg->gadget);
  46318. + if (gcnum >= 0)
  46319. + mod_data.release = 0x0300 + gcnum;
  46320. + else {
  46321. + WARNING(fsg, "controller '%s' not recognized\n",
  46322. + fsg->gadget->name);
  46323. + mod_data.release = 0x0399;
  46324. + }
  46325. + }
  46326. +
  46327. + prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
  46328. +
  46329. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  46330. + if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
  46331. + ; // Use default setting
  46332. + } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
  46333. + mod_data.transport_type = USB_PR_CB;
  46334. + mod_data.transport_name = "Control-Bulk";
  46335. + } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
  46336. + mod_data.transport_type = USB_PR_CBI;
  46337. + mod_data.transport_name = "Control-Bulk-Interrupt";
  46338. + } else {
  46339. + ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
  46340. + return -EINVAL;
  46341. + }
  46342. +
  46343. + if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
  46344. + prot == USB_SC_SCSI) {
  46345. + ; // Use default setting
  46346. + } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
  46347. + prot == USB_SC_RBC) {
  46348. + mod_data.protocol_type = USB_SC_RBC;
  46349. + mod_data.protocol_name = "RBC";
  46350. + } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
  46351. + strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
  46352. + prot == USB_SC_8020) {
  46353. + mod_data.protocol_type = USB_SC_8020;
  46354. + mod_data.protocol_name = "8020i (ATAPI)";
  46355. + } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
  46356. + prot == USB_SC_QIC) {
  46357. + mod_data.protocol_type = USB_SC_QIC;
  46358. + mod_data.protocol_name = "QIC-157";
  46359. + } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
  46360. + prot == USB_SC_UFI) {
  46361. + mod_data.protocol_type = USB_SC_UFI;
  46362. + mod_data.protocol_name = "UFI";
  46363. + } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
  46364. + prot == USB_SC_8070) {
  46365. + mod_data.protocol_type = USB_SC_8070;
  46366. + mod_data.protocol_name = "8070i";
  46367. + } else {
  46368. + ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
  46369. + return -EINVAL;
  46370. + }
  46371. +
  46372. + mod_data.buflen &= PAGE_CACHE_MASK;
  46373. + if (mod_data.buflen <= 0) {
  46374. + ERROR(fsg, "invalid buflen\n");
  46375. + return -ETOOSMALL;
  46376. + }
  46377. +
  46378. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  46379. +
  46380. + /* Serial string handling.
  46381. + * On a real device, the serial string would be loaded
  46382. + * from permanent storage. */
  46383. + if (mod_data.serial) {
  46384. + const char *ch;
  46385. + unsigned len = 0;
  46386. +
  46387. + /* Sanity check :
  46388. + * The CB[I] specification limits the serial string to
  46389. + * 12 uppercase hexadecimal characters.
  46390. + * BBB need at least 12 uppercase hexadecimal characters,
  46391. + * with a maximum of 126. */
  46392. + for (ch = mod_data.serial; *ch; ++ch) {
  46393. + ++len;
  46394. + if ((*ch < '0' || *ch > '9') &&
  46395. + (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
  46396. + WARNING(fsg,
  46397. + "Invalid serial string character: %c\n",
  46398. + *ch);
  46399. + goto no_serial;
  46400. + }
  46401. + }
  46402. + if (len > 126 ||
  46403. + (mod_data.transport_type == USB_PR_BULK && len < 12) ||
  46404. + (mod_data.transport_type != USB_PR_BULK && len > 12)) {
  46405. + WARNING(fsg, "Invalid serial string length!\n");
  46406. + goto no_serial;
  46407. + }
  46408. + fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
  46409. + } else {
  46410. + WARNING(fsg, "No serial-number string provided!\n");
  46411. + no_serial:
  46412. + device_desc.iSerialNumber = 0;
  46413. + }
  46414. +
  46415. + return 0;
  46416. +}
  46417. +
  46418. +
  46419. +static int __init fsg_bind(struct usb_gadget *gadget)
  46420. +{
  46421. + struct fsg_dev *fsg = the_fsg;
  46422. + int rc;
  46423. + int i;
  46424. + struct fsg_lun *curlun;
  46425. + struct usb_ep *ep;
  46426. + struct usb_request *req;
  46427. + char *pathbuf, *p;
  46428. +
  46429. + fsg->gadget = gadget;
  46430. + set_gadget_data(gadget, fsg);
  46431. + fsg->ep0 = gadget->ep0;
  46432. + fsg->ep0->driver_data = fsg;
  46433. +
  46434. + if ((rc = check_parameters(fsg)) != 0)
  46435. + goto out;
  46436. +
  46437. + if (mod_data.removable) { // Enable the store_xxx attributes
  46438. + dev_attr_file.attr.mode = 0644;
  46439. + dev_attr_file.store = fsg_store_file;
  46440. + if (!mod_data.cdrom) {
  46441. + dev_attr_ro.attr.mode = 0644;
  46442. + dev_attr_ro.store = fsg_store_ro;
  46443. + }
  46444. + }
  46445. +
  46446. + /* Only for removable media? */
  46447. + dev_attr_nofua.attr.mode = 0644;
  46448. + dev_attr_nofua.store = fsg_store_nofua;
  46449. +
  46450. + /* Find out how many LUNs there should be */
  46451. + i = mod_data.nluns;
  46452. + if (i == 0)
  46453. + i = max(mod_data.num_filenames, 1u);
  46454. + if (i > FSG_MAX_LUNS) {
  46455. + ERROR(fsg, "invalid number of LUNs: %d\n", i);
  46456. + rc = -EINVAL;
  46457. + goto out;
  46458. + }
  46459. +
  46460. + /* Create the LUNs, open their backing files, and register the
  46461. + * LUN devices in sysfs. */
  46462. + fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
  46463. + if (!fsg->luns) {
  46464. + rc = -ENOMEM;
  46465. + goto out;
  46466. + }
  46467. + fsg->nluns = i;
  46468. +
  46469. + for (i = 0; i < fsg->nluns; ++i) {
  46470. + curlun = &fsg->luns[i];
  46471. + curlun->cdrom = !!mod_data.cdrom;
  46472. + curlun->ro = mod_data.cdrom || mod_data.ro[i];
  46473. + curlun->initially_ro = curlun->ro;
  46474. + curlun->removable = mod_data.removable;
  46475. + curlun->nofua = mod_data.nofua[i];
  46476. + curlun->dev.release = lun_release;
  46477. + curlun->dev.parent = &gadget->dev;
  46478. + curlun->dev.driver = &fsg_driver.driver;
  46479. + dev_set_drvdata(&curlun->dev, &fsg->filesem);
  46480. + dev_set_name(&curlun->dev,"%s-lun%d",
  46481. + dev_name(&gadget->dev), i);
  46482. +
  46483. + kref_get(&fsg->ref);
  46484. + rc = device_register(&curlun->dev);
  46485. + if (rc) {
  46486. + INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
  46487. + put_device(&curlun->dev);
  46488. + goto out;
  46489. + }
  46490. + curlun->registered = 1;
  46491. +
  46492. + rc = device_create_file(&curlun->dev, &dev_attr_ro);
  46493. + if (rc)
  46494. + goto out;
  46495. + rc = device_create_file(&curlun->dev, &dev_attr_nofua);
  46496. + if (rc)
  46497. + goto out;
  46498. + rc = device_create_file(&curlun->dev, &dev_attr_file);
  46499. + if (rc)
  46500. + goto out;
  46501. +
  46502. + if (mod_data.file[i] && *mod_data.file[i]) {
  46503. + rc = fsg_lun_open(curlun, mod_data.file[i]);
  46504. + if (rc)
  46505. + goto out;
  46506. + } else if (!mod_data.removable) {
  46507. + ERROR(fsg, "no file given for LUN%d\n", i);
  46508. + rc = -EINVAL;
  46509. + goto out;
  46510. + }
  46511. + }
  46512. +
  46513. + /* Find all the endpoints we will use */
  46514. + usb_ep_autoconfig_reset(gadget);
  46515. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
  46516. + if (!ep)
  46517. + goto autoconf_fail;
  46518. + ep->driver_data = fsg; // claim the endpoint
  46519. + fsg->bulk_in = ep;
  46520. +
  46521. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
  46522. + if (!ep)
  46523. + goto autoconf_fail;
  46524. + ep->driver_data = fsg; // claim the endpoint
  46525. + fsg->bulk_out = ep;
  46526. +
  46527. + if (transport_is_cbi()) {
  46528. + ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
  46529. + if (!ep)
  46530. + goto autoconf_fail;
  46531. + ep->driver_data = fsg; // claim the endpoint
  46532. + fsg->intr_in = ep;
  46533. + }
  46534. +
  46535. + /* Fix up the descriptors */
  46536. + device_desc.idVendor = cpu_to_le16(mod_data.vendor);
  46537. + device_desc.idProduct = cpu_to_le16(mod_data.product);
  46538. + device_desc.bcdDevice = cpu_to_le16(mod_data.release);
  46539. +
  46540. + i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
  46541. + fsg_intf_desc.bNumEndpoints = i;
  46542. + fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
  46543. + fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
  46544. + fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  46545. +
  46546. + if (gadget_is_dualspeed(gadget)) {
  46547. + fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  46548. +
  46549. + /* Assume endpoint addresses are the same for both speeds */
  46550. + fsg_hs_bulk_in_desc.bEndpointAddress =
  46551. + fsg_fs_bulk_in_desc.bEndpointAddress;
  46552. + fsg_hs_bulk_out_desc.bEndpointAddress =
  46553. + fsg_fs_bulk_out_desc.bEndpointAddress;
  46554. + fsg_hs_intr_in_desc.bEndpointAddress =
  46555. + fsg_fs_intr_in_desc.bEndpointAddress;
  46556. + }
  46557. +
  46558. + if (gadget_is_superspeed(gadget)) {
  46559. + unsigned max_burst;
  46560. +
  46561. + fsg_ss_function[i + FSG_SS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  46562. +
  46563. + /* Calculate bMaxBurst, we know packet size is 1024 */
  46564. + max_burst = min_t(unsigned, mod_data.buflen / 1024, 15);
  46565. +
  46566. + /* Assume endpoint addresses are the same for both speeds */
  46567. + fsg_ss_bulk_in_desc.bEndpointAddress =
  46568. + fsg_fs_bulk_in_desc.bEndpointAddress;
  46569. + fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
  46570. +
  46571. + fsg_ss_bulk_out_desc.bEndpointAddress =
  46572. + fsg_fs_bulk_out_desc.bEndpointAddress;
  46573. + fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
  46574. + }
  46575. +
  46576. + if (gadget_is_otg(gadget))
  46577. + fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
  46578. +
  46579. + rc = -ENOMEM;
  46580. +
  46581. + /* Allocate the request and buffer for endpoint 0 */
  46582. + fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
  46583. + if (!req)
  46584. + goto out;
  46585. + req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL);
  46586. + if (!req->buf)
  46587. + goto out;
  46588. + req->complete = ep0_complete;
  46589. +
  46590. + /* Allocate the data buffers */
  46591. + for (i = 0; i < fsg_num_buffers; ++i) {
  46592. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  46593. +
  46594. + /* Allocate for the bulk-in endpoint. We assume that
  46595. + * the buffer will also work with the bulk-out (and
  46596. + * interrupt-in) endpoint. */
  46597. + bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL);
  46598. + if (!bh->buf)
  46599. + goto out;
  46600. + bh->next = bh + 1;
  46601. + }
  46602. + fsg->buffhds[fsg_num_buffers - 1].next = &fsg->buffhds[0];
  46603. +
  46604. + /* This should reflect the actual gadget power source */
  46605. + usb_gadget_set_selfpowered(gadget);
  46606. +
  46607. + snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
  46608. + "%s %s with %s",
  46609. + init_utsname()->sysname, init_utsname()->release,
  46610. + gadget->name);
  46611. +
  46612. + fsg->thread_task = kthread_create(fsg_main_thread, fsg,
  46613. + "file-storage-gadget");
  46614. + if (IS_ERR(fsg->thread_task)) {
  46615. + rc = PTR_ERR(fsg->thread_task);
  46616. + goto out;
  46617. + }
  46618. +
  46619. + INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  46620. + INFO(fsg, "NOTE: This driver is deprecated. "
  46621. + "Consider using g_mass_storage instead.\n");
  46622. + INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
  46623. +
  46624. + pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  46625. + for (i = 0; i < fsg->nluns; ++i) {
  46626. + curlun = &fsg->luns[i];
  46627. + if (fsg_lun_is_open(curlun)) {
  46628. + p = NULL;
  46629. + if (pathbuf) {
  46630. + p = d_path(&curlun->filp->f_path,
  46631. + pathbuf, PATH_MAX);
  46632. + if (IS_ERR(p))
  46633. + p = NULL;
  46634. + }
  46635. + LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
  46636. + curlun->ro, curlun->nofua, (p ? p : "(error)"));
  46637. + }
  46638. + }
  46639. + kfree(pathbuf);
  46640. +
  46641. + DBG(fsg, "transport=%s (x%02x)\n",
  46642. + mod_data.transport_name, mod_data.transport_type);
  46643. + DBG(fsg, "protocol=%s (x%02x)\n",
  46644. + mod_data.protocol_name, mod_data.protocol_type);
  46645. + DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
  46646. + mod_data.vendor, mod_data.product, mod_data.release);
  46647. + DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
  46648. + mod_data.removable, mod_data.can_stall,
  46649. + mod_data.cdrom, mod_data.buflen);
  46650. + DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
  46651. +
  46652. + set_bit(REGISTERED, &fsg->atomic_bitflags);
  46653. +
  46654. + /* Tell the thread to start working */
  46655. + wake_up_process(fsg->thread_task);
  46656. + return 0;
  46657. +
  46658. +autoconf_fail:
  46659. + ERROR(fsg, "unable to autoconfigure all endpoints\n");
  46660. + rc = -ENOTSUPP;
  46661. +
  46662. +out:
  46663. + fsg->state = FSG_STATE_TERMINATED; // The thread is dead
  46664. + fsg_unbind(gadget);
  46665. + complete(&fsg->thread_notifier);
  46666. + return rc;
  46667. +}
  46668. +
  46669. +
  46670. +/*-------------------------------------------------------------------------*/
  46671. +
  46672. +static void fsg_suspend(struct usb_gadget *gadget)
  46673. +{
  46674. + struct fsg_dev *fsg = get_gadget_data(gadget);
  46675. +
  46676. + DBG(fsg, "suspend\n");
  46677. + set_bit(SUSPENDED, &fsg->atomic_bitflags);
  46678. +}
  46679. +
  46680. +static void fsg_resume(struct usb_gadget *gadget)
  46681. +{
  46682. + struct fsg_dev *fsg = get_gadget_data(gadget);
  46683. +
  46684. + DBG(fsg, "resume\n");
  46685. + clear_bit(SUSPENDED, &fsg->atomic_bitflags);
  46686. +}
  46687. +
  46688. +
  46689. +/*-------------------------------------------------------------------------*/
  46690. +
  46691. +static struct usb_gadget_driver fsg_driver = {
  46692. + .max_speed = USB_SPEED_SUPER,
  46693. + .function = (char *) fsg_string_product,
  46694. + .unbind = fsg_unbind,
  46695. + .disconnect = fsg_disconnect,
  46696. + .setup = fsg_setup,
  46697. + .suspend = fsg_suspend,
  46698. + .resume = fsg_resume,
  46699. +
  46700. + .driver = {
  46701. + .name = DRIVER_NAME,
  46702. + .owner = THIS_MODULE,
  46703. + // .release = ...
  46704. + // .suspend = ...
  46705. + // .resume = ...
  46706. + },
  46707. +};
  46708. +
  46709. +
  46710. +static int __init fsg_alloc(void)
  46711. +{
  46712. + struct fsg_dev *fsg;
  46713. +
  46714. + fsg = kzalloc(sizeof *fsg +
  46715. + fsg_num_buffers * sizeof *(fsg->buffhds), GFP_KERNEL);
  46716. +
  46717. + if (!fsg)
  46718. + return -ENOMEM;
  46719. + spin_lock_init(&fsg->lock);
  46720. + init_rwsem(&fsg->filesem);
  46721. + kref_init(&fsg->ref);
  46722. + init_completion(&fsg->thread_notifier);
  46723. +
  46724. + the_fsg = fsg;
  46725. + return 0;
  46726. +}
  46727. +
  46728. +
  46729. +static int __init fsg_init(void)
  46730. +{
  46731. + int rc;
  46732. + struct fsg_dev *fsg;
  46733. +
  46734. + rc = fsg_num_buffers_validate();
  46735. + if (rc != 0)
  46736. + return rc;
  46737. +
  46738. + if ((rc = fsg_alloc()) != 0)
  46739. + return rc;
  46740. + fsg = the_fsg;
  46741. + if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0)
  46742. + kref_put(&fsg->ref, fsg_release);
  46743. + return rc;
  46744. +}
  46745. +module_init(fsg_init);
  46746. +
  46747. +
  46748. +static void __exit fsg_cleanup(void)
  46749. +{
  46750. + struct fsg_dev *fsg = the_fsg;
  46751. +
  46752. + /* Unregister the driver iff the thread hasn't already done so */
  46753. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  46754. + usb_gadget_unregister_driver(&fsg_driver);
  46755. +
  46756. + /* Wait for the thread to finish up */
  46757. + wait_for_completion(&fsg->thread_notifier);
  46758. +
  46759. + kref_put(&fsg->ref, fsg_release);
  46760. +}
  46761. +module_exit(fsg_cleanup);
  46762. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/changes.txt linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/changes.txt
  46763. --- linux-3.12.33/drivers/usb/host/dwc_common_port/changes.txt 1969-12-31 18:00:00.000000000 -0600
  46764. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/changes.txt 2014-12-03 19:13:40.212418001 -0600
  46765. @@ -0,0 +1,174 @@
  46766. +
  46767. +dwc_read_reg32() and friends now take an additional parameter, a pointer to an
  46768. +IO context struct. The IO context struct should live in an os-dependent struct
  46769. +in your driver. As an example, the dwc_usb3 driver has an os-dependent struct
  46770. +named 'os_dep' embedded in the main device struct. So there these calls look
  46771. +like this:
  46772. +
  46773. + dwc_read_reg32(&usb3_dev->os_dep.ioctx, &pcd->dev_global_regs->dcfg);
  46774. +
  46775. + dwc_write_reg32(&usb3_dev->os_dep.ioctx,
  46776. + &pcd->dev_global_regs->dcfg, 0);
  46777. +
  46778. +Note that for the existing Linux driver ports, it is not necessary to actually
  46779. +define the 'ioctx' member in the os-dependent struct. Since Linux does not
  46780. +require an IO context, its macros for dwc_read_reg32() and friends do not
  46781. +use the context pointer, so it is optimized away by the compiler. But it is
  46782. +necessary to add the pointer parameter to all of the call sites, to be ready
  46783. +for any future ports (such as FreeBSD) which do require an IO context.
  46784. +
  46785. +
  46786. +Similarly, dwc_alloc(), dwc_alloc_atomic(), dwc_strdup(), and dwc_free() now
  46787. +take an additional parameter, a pointer to a memory context. Examples:
  46788. +
  46789. + addr = dwc_alloc(&usb3_dev->os_dep.memctx, size);
  46790. +
  46791. + dwc_free(&usb3_dev->os_dep.memctx, addr);
  46792. +
  46793. +Again, for the Linux ports, it is not necessary to actually define the memctx
  46794. +member, but it is necessary to add the pointer parameter to all of the call
  46795. +sites.
  46796. +
  46797. +
  46798. +Same for dwc_dma_alloc() and dwc_dma_free(). Examples:
  46799. +
  46800. + virt_addr = dwc_dma_alloc(&usb3_dev->os_dep.dmactx, size, &phys_addr);
  46801. +
  46802. + dwc_dma_free(&usb3_dev->os_dep.dmactx, size, virt_addr, phys_addr);
  46803. +
  46804. +
  46805. +Same for dwc_mutex_alloc() and dwc_mutex_free(). Examples:
  46806. +
  46807. + mutex = dwc_mutex_alloc(&usb3_dev->os_dep.mtxctx);
  46808. +
  46809. + dwc_mutex_free(&usb3_dev->os_dep.mtxctx, mutex);
  46810. +
  46811. +
  46812. +Same for dwc_spinlock_alloc() and dwc_spinlock_free(). Examples:
  46813. +
  46814. + lock = dwc_spinlock_alloc(&usb3_dev->osdep.splctx);
  46815. +
  46816. + dwc_spinlock_free(&usb3_dev->osdep.splctx, lock);
  46817. +
  46818. +
  46819. +Same for dwc_timer_alloc(). Example:
  46820. +
  46821. + timer = dwc_timer_alloc(&usb3_dev->os_dep.tmrctx, "dwc_usb3_tmr1",
  46822. + cb_func, cb_data);
  46823. +
  46824. +
  46825. +Same for dwc_waitq_alloc(). Example:
  46826. +
  46827. + waitq = dwc_waitq_alloc(&usb3_dev->os_dep.wtqctx);
  46828. +
  46829. +
  46830. +Same for dwc_thread_run(). Example:
  46831. +
  46832. + thread = dwc_thread_run(&usb3_dev->os_dep.thdctx, func,
  46833. + "dwc_usb3_thd1", data);
  46834. +
  46835. +
  46836. +Same for dwc_workq_alloc(). Example:
  46837. +
  46838. + workq = dwc_workq_alloc(&usb3_dev->osdep.wkqctx, "dwc_usb3_wkq1");
  46839. +
  46840. +
  46841. +Same for dwc_task_alloc(). Example:
  46842. +
  46843. + task = dwc_task_alloc(&usb3_dev->os_dep.tskctx, "dwc_usb3_tsk1",
  46844. + cb_func, cb_data);
  46845. +
  46846. +
  46847. +In addition to the context pointer additions, a few core functions have had
  46848. +other changes made to their parameters:
  46849. +
  46850. +The 'flags' parameter to dwc_spinlock_irqsave() and dwc_spinunlock_irqrestore()
  46851. +has been changed from a uint64_t to a dwc_irqflags_t.
  46852. +
  46853. +dwc_thread_should_stop() now takes a 'dwc_thread_t *' parameter, because the
  46854. +FreeBSD equivalent of that function requires it.
  46855. +
  46856. +And, in addition to the context pointer, dwc_task_alloc() also adds a
  46857. +'char *name' parameter, to be consistent with dwc_thread_run() and
  46858. +dwc_workq_alloc(), and because the FreeBSD equivalent of that function
  46859. +requires a unique name.
  46860. +
  46861. +
  46862. +Here is a complete list of the core functions that now take a pointer to a
  46863. +context as their first parameter:
  46864. +
  46865. + dwc_read_reg32
  46866. + dwc_read_reg64
  46867. + dwc_write_reg32
  46868. + dwc_write_reg64
  46869. + dwc_modify_reg32
  46870. + dwc_modify_reg64
  46871. + dwc_alloc
  46872. + dwc_alloc_atomic
  46873. + dwc_strdup
  46874. + dwc_free
  46875. + dwc_dma_alloc
  46876. + dwc_dma_free
  46877. + dwc_mutex_alloc
  46878. + dwc_mutex_free
  46879. + dwc_spinlock_alloc
  46880. + dwc_spinlock_free
  46881. + dwc_timer_alloc
  46882. + dwc_waitq_alloc
  46883. + dwc_thread_run
  46884. + dwc_workq_alloc
  46885. + dwc_task_alloc Also adds a 'char *name' as its 2nd parameter
  46886. +
  46887. +And here are the core functions that have other changes to their parameters:
  46888. +
  46889. + dwc_spinlock_irqsave 'flags' param is now a 'dwc_irqflags_t *'
  46890. + dwc_spinunlock_irqrestore 'flags' param is now a 'dwc_irqflags_t'
  46891. + dwc_thread_should_stop Adds a 'dwc_thread_t *' parameter
  46892. +
  46893. +
  46894. +
  46895. +The changes to the core functions also require some of the other library
  46896. +functions to change:
  46897. +
  46898. + dwc_cc_if_alloc() and dwc_cc_if_free() now take a 'void *memctx'
  46899. + (for memory allocation) as the 1st param and a 'void *mtxctx'
  46900. + (for mutex allocation) as the 2nd param.
  46901. +
  46902. + dwc_cc_clear(), dwc_cc_add(), dwc_cc_change(), dwc_cc_remove(),
  46903. + dwc_cc_data_for_save(), and dwc_cc_restore_from_data() now take a
  46904. + 'void *memctx' as the 1st param.
  46905. +
  46906. + dwc_dh_modpow(), dwc_dh_pk(), and dwc_dh_derive_keys() now take a
  46907. + 'void *memctx' as the 1st param.
  46908. +
  46909. + dwc_modpow() now takes a 'void *memctx' as the 1st param.
  46910. +
  46911. + dwc_alloc_notification_manager() now takes a 'void *memctx' as the
  46912. + 1st param and a 'void *wkqctx' (for work queue allocation) as the 2nd
  46913. + param, and also now returns an integer value that is non-zero if
  46914. + allocation of its data structures or work queue fails.
  46915. +
  46916. + dwc_register_notifier() now takes a 'void *memctx' as the 1st param.
  46917. +
  46918. + dwc_memory_debug_start() now takes a 'void *mem_ctx' as the first
  46919. + param, and also now returns an integer value that is non-zero if
  46920. + allocation of its data structures fails.
  46921. +
  46922. +
  46923. +
  46924. +Other miscellaneous changes:
  46925. +
  46926. +The DEBUG_MEMORY and DEBUG_REGS #define's have been renamed to
  46927. +DWC_DEBUG_MEMORY and DWC_DEBUG_REGS.
  46928. +
  46929. +The following #define's have been added to allow selectively compiling library
  46930. +features:
  46931. +
  46932. + DWC_CCLIB
  46933. + DWC_CRYPTOLIB
  46934. + DWC_NOTIFYLIB
  46935. + DWC_UTFLIB
  46936. +
  46937. +A DWC_LIBMODULE #define has also been added. If this is not defined, then the
  46938. +module code in dwc_common_linux.c is not compiled in. This allows linking the
  46939. +library code directly into a driver module, instead of as a standalone module.
  46940. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/doc/doxygen.cfg linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/doc/doxygen.cfg
  46941. --- linux-3.12.33/drivers/usb/host/dwc_common_port/doc/doxygen.cfg 1969-12-31 18:00:00.000000000 -0600
  46942. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/doc/doxygen.cfg 2014-12-03 19:13:40.212418001 -0600
  46943. @@ -0,0 +1,270 @@
  46944. +# Doxyfile 1.4.5
  46945. +
  46946. +#---------------------------------------------------------------------------
  46947. +# Project related configuration options
  46948. +#---------------------------------------------------------------------------
  46949. +PROJECT_NAME = "Synopsys DWC Portability and Common Library for UWB"
  46950. +PROJECT_NUMBER =
  46951. +OUTPUT_DIRECTORY = doc
  46952. +CREATE_SUBDIRS = NO
  46953. +OUTPUT_LANGUAGE = English
  46954. +BRIEF_MEMBER_DESC = YES
  46955. +REPEAT_BRIEF = YES
  46956. +ABBREVIATE_BRIEF = "The $name class" \
  46957. + "The $name widget" \
  46958. + "The $name file" \
  46959. + is \
  46960. + provides \
  46961. + specifies \
  46962. + contains \
  46963. + represents \
  46964. + a \
  46965. + an \
  46966. + the
  46967. +ALWAYS_DETAILED_SEC = YES
  46968. +INLINE_INHERITED_MEMB = NO
  46969. +FULL_PATH_NAMES = NO
  46970. +STRIP_FROM_PATH = ..
  46971. +STRIP_FROM_INC_PATH =
  46972. +SHORT_NAMES = NO
  46973. +JAVADOC_AUTOBRIEF = YES
  46974. +MULTILINE_CPP_IS_BRIEF = NO
  46975. +DETAILS_AT_TOP = YES
  46976. +INHERIT_DOCS = YES
  46977. +SEPARATE_MEMBER_PAGES = NO
  46978. +TAB_SIZE = 8
  46979. +ALIASES =
  46980. +OPTIMIZE_OUTPUT_FOR_C = YES
  46981. +OPTIMIZE_OUTPUT_JAVA = NO
  46982. +BUILTIN_STL_SUPPORT = NO
  46983. +DISTRIBUTE_GROUP_DOC = NO
  46984. +SUBGROUPING = NO
  46985. +#---------------------------------------------------------------------------
  46986. +# Build related configuration options
  46987. +#---------------------------------------------------------------------------
  46988. +EXTRACT_ALL = NO
  46989. +EXTRACT_PRIVATE = NO
  46990. +EXTRACT_STATIC = YES
  46991. +EXTRACT_LOCAL_CLASSES = NO
  46992. +EXTRACT_LOCAL_METHODS = NO
  46993. +HIDE_UNDOC_MEMBERS = NO
  46994. +HIDE_UNDOC_CLASSES = NO
  46995. +HIDE_FRIEND_COMPOUNDS = NO
  46996. +HIDE_IN_BODY_DOCS = NO
  46997. +INTERNAL_DOCS = NO
  46998. +CASE_SENSE_NAMES = YES
  46999. +HIDE_SCOPE_NAMES = NO
  47000. +SHOW_INCLUDE_FILES = NO
  47001. +INLINE_INFO = YES
  47002. +SORT_MEMBER_DOCS = NO
  47003. +SORT_BRIEF_DOCS = NO
  47004. +SORT_BY_SCOPE_NAME = NO
  47005. +GENERATE_TODOLIST = YES
  47006. +GENERATE_TESTLIST = YES
  47007. +GENERATE_BUGLIST = YES
  47008. +GENERATE_DEPRECATEDLIST= YES
  47009. +ENABLED_SECTIONS =
  47010. +MAX_INITIALIZER_LINES = 30
  47011. +SHOW_USED_FILES = YES
  47012. +SHOW_DIRECTORIES = YES
  47013. +FILE_VERSION_FILTER =
  47014. +#---------------------------------------------------------------------------
  47015. +# configuration options related to warning and progress messages
  47016. +#---------------------------------------------------------------------------
  47017. +QUIET = YES
  47018. +WARNINGS = YES
  47019. +WARN_IF_UNDOCUMENTED = NO
  47020. +WARN_IF_DOC_ERROR = YES
  47021. +WARN_NO_PARAMDOC = YES
  47022. +WARN_FORMAT = "$file:$line: $text"
  47023. +WARN_LOGFILE =
  47024. +#---------------------------------------------------------------------------
  47025. +# configuration options related to the input files
  47026. +#---------------------------------------------------------------------------
  47027. +INPUT = .
  47028. +FILE_PATTERNS = *.c \
  47029. + *.cc \
  47030. + *.cxx \
  47031. + *.cpp \
  47032. + *.c++ \
  47033. + *.d \
  47034. + *.java \
  47035. + *.ii \
  47036. + *.ixx \
  47037. + *.ipp \
  47038. + *.i++ \
  47039. + *.inl \
  47040. + *.h \
  47041. + *.hh \
  47042. + *.hxx \
  47043. + *.hpp \
  47044. + *.h++ \
  47045. + *.idl \
  47046. + *.odl \
  47047. + *.cs \
  47048. + *.php \
  47049. + *.php3 \
  47050. + *.inc \
  47051. + *.m \
  47052. + *.mm \
  47053. + *.dox \
  47054. + *.py \
  47055. + *.C \
  47056. + *.CC \
  47057. + *.C++ \
  47058. + *.II \
  47059. + *.I++ \
  47060. + *.H \
  47061. + *.HH \
  47062. + *.H++ \
  47063. + *.CS \
  47064. + *.PHP \
  47065. + *.PHP3 \
  47066. + *.M \
  47067. + *.MM \
  47068. + *.PY
  47069. +RECURSIVE = NO
  47070. +EXCLUDE =
  47071. +EXCLUDE_SYMLINKS = NO
  47072. +EXCLUDE_PATTERNS =
  47073. +EXAMPLE_PATH =
  47074. +EXAMPLE_PATTERNS = *
  47075. +EXAMPLE_RECURSIVE = NO
  47076. +IMAGE_PATH =
  47077. +INPUT_FILTER =
  47078. +FILTER_PATTERNS =
  47079. +FILTER_SOURCE_FILES = NO
  47080. +#---------------------------------------------------------------------------
  47081. +# configuration options related to source browsing
  47082. +#---------------------------------------------------------------------------
  47083. +SOURCE_BROWSER = NO
  47084. +INLINE_SOURCES = NO
  47085. +STRIP_CODE_COMMENTS = YES
  47086. +REFERENCED_BY_RELATION = YES
  47087. +REFERENCES_RELATION = YES
  47088. +USE_HTAGS = NO
  47089. +VERBATIM_HEADERS = NO
  47090. +#---------------------------------------------------------------------------
  47091. +# configuration options related to the alphabetical class index
  47092. +#---------------------------------------------------------------------------
  47093. +ALPHABETICAL_INDEX = NO
  47094. +COLS_IN_ALPHA_INDEX = 5
  47095. +IGNORE_PREFIX =
  47096. +#---------------------------------------------------------------------------
  47097. +# configuration options related to the HTML output
  47098. +#---------------------------------------------------------------------------
  47099. +GENERATE_HTML = YES
  47100. +HTML_OUTPUT = html
  47101. +HTML_FILE_EXTENSION = .html
  47102. +HTML_HEADER =
  47103. +HTML_FOOTER =
  47104. +HTML_STYLESHEET =
  47105. +HTML_ALIGN_MEMBERS = YES
  47106. +GENERATE_HTMLHELP = NO
  47107. +CHM_FILE =
  47108. +HHC_LOCATION =
  47109. +GENERATE_CHI = NO
  47110. +BINARY_TOC = NO
  47111. +TOC_EXPAND = NO
  47112. +DISABLE_INDEX = NO
  47113. +ENUM_VALUES_PER_LINE = 4
  47114. +GENERATE_TREEVIEW = YES
  47115. +TREEVIEW_WIDTH = 250
  47116. +#---------------------------------------------------------------------------
  47117. +# configuration options related to the LaTeX output
  47118. +#---------------------------------------------------------------------------
  47119. +GENERATE_LATEX = NO
  47120. +LATEX_OUTPUT = latex
  47121. +LATEX_CMD_NAME = latex
  47122. +MAKEINDEX_CMD_NAME = makeindex
  47123. +COMPACT_LATEX = NO
  47124. +PAPER_TYPE = a4wide
  47125. +EXTRA_PACKAGES =
  47126. +LATEX_HEADER =
  47127. +PDF_HYPERLINKS = NO
  47128. +USE_PDFLATEX = NO
  47129. +LATEX_BATCHMODE = NO
  47130. +LATEX_HIDE_INDICES = NO
  47131. +#---------------------------------------------------------------------------
  47132. +# configuration options related to the RTF output
  47133. +#---------------------------------------------------------------------------
  47134. +GENERATE_RTF = NO
  47135. +RTF_OUTPUT = rtf
  47136. +COMPACT_RTF = NO
  47137. +RTF_HYPERLINKS = NO
  47138. +RTF_STYLESHEET_FILE =
  47139. +RTF_EXTENSIONS_FILE =
  47140. +#---------------------------------------------------------------------------
  47141. +# configuration options related to the man page output
  47142. +#---------------------------------------------------------------------------
  47143. +GENERATE_MAN = NO
  47144. +MAN_OUTPUT = man
  47145. +MAN_EXTENSION = .3
  47146. +MAN_LINKS = NO
  47147. +#---------------------------------------------------------------------------
  47148. +# configuration options related to the XML output
  47149. +#---------------------------------------------------------------------------
  47150. +GENERATE_XML = NO
  47151. +XML_OUTPUT = xml
  47152. +XML_SCHEMA =
  47153. +XML_DTD =
  47154. +XML_PROGRAMLISTING = YES
  47155. +#---------------------------------------------------------------------------
  47156. +# configuration options for the AutoGen Definitions output
  47157. +#---------------------------------------------------------------------------
  47158. +GENERATE_AUTOGEN_DEF = NO
  47159. +#---------------------------------------------------------------------------
  47160. +# configuration options related to the Perl module output
  47161. +#---------------------------------------------------------------------------
  47162. +GENERATE_PERLMOD = NO
  47163. +PERLMOD_LATEX = NO
  47164. +PERLMOD_PRETTY = YES
  47165. +PERLMOD_MAKEVAR_PREFIX =
  47166. +#---------------------------------------------------------------------------
  47167. +# Configuration options related to the preprocessor
  47168. +#---------------------------------------------------------------------------
  47169. +ENABLE_PREPROCESSING = YES
  47170. +MACRO_EXPANSION = NO
  47171. +EXPAND_ONLY_PREDEF = NO
  47172. +SEARCH_INCLUDES = YES
  47173. +INCLUDE_PATH =
  47174. +INCLUDE_FILE_PATTERNS =
  47175. +PREDEFINED = DEBUG DEBUG_MEMORY
  47176. +EXPAND_AS_DEFINED =
  47177. +SKIP_FUNCTION_MACROS = YES
  47178. +#---------------------------------------------------------------------------
  47179. +# Configuration::additions related to external references
  47180. +#---------------------------------------------------------------------------
  47181. +TAGFILES =
  47182. +GENERATE_TAGFILE =
  47183. +ALLEXTERNALS = NO
  47184. +EXTERNAL_GROUPS = YES
  47185. +PERL_PATH = /usr/bin/perl
  47186. +#---------------------------------------------------------------------------
  47187. +# Configuration options related to the dot tool
  47188. +#---------------------------------------------------------------------------
  47189. +CLASS_DIAGRAMS = YES
  47190. +HIDE_UNDOC_RELATIONS = YES
  47191. +HAVE_DOT = NO
  47192. +CLASS_GRAPH = YES
  47193. +COLLABORATION_GRAPH = YES
  47194. +GROUP_GRAPHS = YES
  47195. +UML_LOOK = NO
  47196. +TEMPLATE_RELATIONS = NO
  47197. +INCLUDE_GRAPH = NO
  47198. +INCLUDED_BY_GRAPH = YES
  47199. +CALL_GRAPH = NO
  47200. +GRAPHICAL_HIERARCHY = YES
  47201. +DIRECTORY_GRAPH = YES
  47202. +DOT_IMAGE_FORMAT = png
  47203. +DOT_PATH =
  47204. +DOTFILE_DIRS =
  47205. +MAX_DOT_GRAPH_DEPTH = 1000
  47206. +DOT_TRANSPARENT = NO
  47207. +DOT_MULTI_TARGETS = NO
  47208. +GENERATE_LEGEND = YES
  47209. +DOT_CLEANUP = YES
  47210. +#---------------------------------------------------------------------------
  47211. +# Configuration::additions related to the search engine
  47212. +#---------------------------------------------------------------------------
  47213. +SEARCHENGINE = NO
  47214. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_cc.c linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_cc.c
  47215. --- linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_cc.c 1969-12-31 18:00:00.000000000 -0600
  47216. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_cc.c 2014-12-03 19:13:40.212418001 -0600
  47217. @@ -0,0 +1,532 @@
  47218. +/* =========================================================================
  47219. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.c $
  47220. + * $Revision: #4 $
  47221. + * $Date: 2010/11/04 $
  47222. + * $Change: 1621692 $
  47223. + *
  47224. + * Synopsys Portability Library Software and documentation
  47225. + * (hereinafter, "Software") is an Unsupported proprietary work of
  47226. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  47227. + * between Synopsys and you.
  47228. + *
  47229. + * The Software IS NOT an item of Licensed Software or Licensed Product
  47230. + * under any End User Software License Agreement or Agreement for
  47231. + * Licensed Product with Synopsys or any supplement thereto. You are
  47232. + * permitted to use and redistribute this Software in source and binary
  47233. + * forms, with or without modification, provided that redistributions
  47234. + * of source code must retain this notice. You may not view, use,
  47235. + * disclose, copy or distribute this file or any information contained
  47236. + * herein except pursuant to this license grant from Synopsys. If you
  47237. + * do not agree with this notice, including the disclaimer below, then
  47238. + * you are not authorized to use the Software.
  47239. + *
  47240. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  47241. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  47242. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  47243. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  47244. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  47245. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  47246. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  47247. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  47248. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  47249. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  47250. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  47251. + * DAMAGE.
  47252. + * ========================================================================= */
  47253. +#ifdef DWC_CCLIB
  47254. +
  47255. +#include "dwc_cc.h"
  47256. +
  47257. +typedef struct dwc_cc
  47258. +{
  47259. + uint32_t uid;
  47260. + uint8_t chid[16];
  47261. + uint8_t cdid[16];
  47262. + uint8_t ck[16];
  47263. + uint8_t *name;
  47264. + uint8_t length;
  47265. + DWC_CIRCLEQ_ENTRY(dwc_cc) list_entry;
  47266. +} dwc_cc_t;
  47267. +
  47268. +DWC_CIRCLEQ_HEAD(context_list, dwc_cc);
  47269. +
  47270. +/** The main structure for CC management. */
  47271. +struct dwc_cc_if
  47272. +{
  47273. + dwc_mutex_t *mutex;
  47274. + char *filename;
  47275. +
  47276. + unsigned is_host:1;
  47277. +
  47278. + dwc_notifier_t *notifier;
  47279. +
  47280. + struct context_list list;
  47281. +};
  47282. +
  47283. +#ifdef DEBUG
  47284. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  47285. +{
  47286. + int i;
  47287. + DWC_PRINTF("%s: ", name);
  47288. + for (i=0; i<len; i++) {
  47289. + DWC_PRINTF("%02x ", bytes[i]);
  47290. + }
  47291. + DWC_PRINTF("\n");
  47292. +}
  47293. +#else
  47294. +#define dump_bytes(x...)
  47295. +#endif
  47296. +
  47297. +static dwc_cc_t *alloc_cc(void *mem_ctx, uint8_t *name, uint32_t length)
  47298. +{
  47299. + dwc_cc_t *cc = dwc_alloc(mem_ctx, sizeof(dwc_cc_t));
  47300. + if (!cc) {
  47301. + return NULL;
  47302. + }
  47303. + DWC_MEMSET(cc, 0, sizeof(dwc_cc_t));
  47304. +
  47305. + if (name) {
  47306. + cc->length = length;
  47307. + cc->name = dwc_alloc(mem_ctx, length);
  47308. + if (!cc->name) {
  47309. + dwc_free(mem_ctx, cc);
  47310. + return NULL;
  47311. + }
  47312. +
  47313. + DWC_MEMCPY(cc->name, name, length);
  47314. + }
  47315. +
  47316. + return cc;
  47317. +}
  47318. +
  47319. +static void free_cc(void *mem_ctx, dwc_cc_t *cc)
  47320. +{
  47321. + if (cc->name) {
  47322. + dwc_free(mem_ctx, cc->name);
  47323. + }
  47324. + dwc_free(mem_ctx, cc);
  47325. +}
  47326. +
  47327. +static uint32_t next_uid(dwc_cc_if_t *cc_if)
  47328. +{
  47329. + uint32_t uid = 0;
  47330. + dwc_cc_t *cc;
  47331. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  47332. + if (cc->uid > uid) {
  47333. + uid = cc->uid;
  47334. + }
  47335. + }
  47336. +
  47337. + if (uid == 0) {
  47338. + uid = 255;
  47339. + }
  47340. +
  47341. + return uid + 1;
  47342. +}
  47343. +
  47344. +static dwc_cc_t *cc_find(dwc_cc_if_t *cc_if, uint32_t uid)
  47345. +{
  47346. + dwc_cc_t *cc;
  47347. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  47348. + if (cc->uid == uid) {
  47349. + return cc;
  47350. + }
  47351. + }
  47352. + return NULL;
  47353. +}
  47354. +
  47355. +static unsigned int cc_data_size(dwc_cc_if_t *cc_if)
  47356. +{
  47357. + unsigned int size = 0;
  47358. + dwc_cc_t *cc;
  47359. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  47360. + size += (48 + 1);
  47361. + if (cc->name) {
  47362. + size += cc->length;
  47363. + }
  47364. + }
  47365. + return size;
  47366. +}
  47367. +
  47368. +static uint32_t cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  47369. +{
  47370. + uint32_t uid = 0;
  47371. + dwc_cc_t *cc;
  47372. +
  47373. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  47374. + if (DWC_MEMCMP(cc->chid, chid, 16) == 0) {
  47375. + uid = cc->uid;
  47376. + break;
  47377. + }
  47378. + }
  47379. + return uid;
  47380. +}
  47381. +static uint32_t cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  47382. +{
  47383. + uint32_t uid = 0;
  47384. + dwc_cc_t *cc;
  47385. +
  47386. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  47387. + if (DWC_MEMCMP(cc->cdid, cdid, 16) == 0) {
  47388. + uid = cc->uid;
  47389. + break;
  47390. + }
  47391. + }
  47392. + return uid;
  47393. +}
  47394. +
  47395. +/* Internal cc_add */
  47396. +static int32_t cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  47397. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  47398. +{
  47399. + dwc_cc_t *cc;
  47400. + uint32_t uid;
  47401. +
  47402. + if (cc_if->is_host) {
  47403. + uid = cc_match_cdid(cc_if, cdid);
  47404. + }
  47405. + else {
  47406. + uid = cc_match_chid(cc_if, chid);
  47407. + }
  47408. +
  47409. + if (uid) {
  47410. + DWC_DEBUGC("Replacing previous connection context id=%d name=%p name_len=%d", uid, name, length);
  47411. + cc = cc_find(cc_if, uid);
  47412. + }
  47413. + else {
  47414. + cc = alloc_cc(mem_ctx, name, length);
  47415. + cc->uid = next_uid(cc_if);
  47416. + DWC_CIRCLEQ_INSERT_TAIL(&cc_if->list, cc, list_entry);
  47417. + }
  47418. +
  47419. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  47420. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  47421. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  47422. +
  47423. + DWC_DEBUGC("Added connection context id=%d name=%p name_len=%d", cc->uid, name, length);
  47424. + dump_bytes("CHID", cc->chid, 16);
  47425. + dump_bytes("CDID", cc->cdid, 16);
  47426. + dump_bytes("CK", cc->ck, 16);
  47427. + return cc->uid;
  47428. +}
  47429. +
  47430. +/* Internal cc_clear */
  47431. +static void cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  47432. +{
  47433. + while (!DWC_CIRCLEQ_EMPTY(&cc_if->list)) {
  47434. + dwc_cc_t *cc = DWC_CIRCLEQ_FIRST(&cc_if->list);
  47435. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  47436. + free_cc(mem_ctx, cc);
  47437. + }
  47438. +}
  47439. +
  47440. +dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  47441. + dwc_notifier_t *notifier, unsigned is_host)
  47442. +{
  47443. + dwc_cc_if_t *cc_if = NULL;
  47444. +
  47445. + /* Allocate a common_cc_if structure */
  47446. + cc_if = dwc_alloc(mem_ctx, sizeof(dwc_cc_if_t));
  47447. +
  47448. + if (!cc_if)
  47449. + return NULL;
  47450. +
  47451. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  47452. + DWC_MUTEX_ALLOC_LINUX_DEBUG(cc_if->mutex);
  47453. +#else
  47454. + cc_if->mutex = dwc_mutex_alloc(mtx_ctx);
  47455. +#endif
  47456. + if (!cc_if->mutex) {
  47457. + dwc_free(mem_ctx, cc_if);
  47458. + return NULL;
  47459. + }
  47460. +
  47461. + DWC_CIRCLEQ_INIT(&cc_if->list);
  47462. + cc_if->is_host = is_host;
  47463. + cc_if->notifier = notifier;
  47464. + return cc_if;
  47465. +}
  47466. +
  47467. +void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if)
  47468. +{
  47469. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  47470. + DWC_MUTEX_FREE(cc_if->mutex);
  47471. +#else
  47472. + dwc_mutex_free(mtx_ctx, cc_if->mutex);
  47473. +#endif
  47474. + cc_clear(mem_ctx, cc_if);
  47475. + dwc_free(mem_ctx, cc_if);
  47476. +}
  47477. +
  47478. +static void cc_changed(dwc_cc_if_t *cc_if)
  47479. +{
  47480. + if (cc_if->notifier) {
  47481. + dwc_notify(cc_if->notifier, DWC_CC_LIST_CHANGED_NOTIFICATION, cc_if);
  47482. + }
  47483. +}
  47484. +
  47485. +void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  47486. +{
  47487. + DWC_MUTEX_LOCK(cc_if->mutex);
  47488. + cc_clear(mem_ctx, cc_if);
  47489. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  47490. + cc_changed(cc_if);
  47491. +}
  47492. +
  47493. +int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  47494. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  47495. +{
  47496. + uint32_t uid;
  47497. +
  47498. + DWC_MUTEX_LOCK(cc_if->mutex);
  47499. + uid = cc_add(mem_ctx, cc_if, chid, cdid, ck, name, length);
  47500. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  47501. + cc_changed(cc_if);
  47502. +
  47503. + return uid;
  47504. +}
  47505. +
  47506. +void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id, uint8_t *chid,
  47507. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  47508. +{
  47509. + dwc_cc_t* cc;
  47510. +
  47511. + DWC_DEBUGC("Change connection context %d", id);
  47512. +
  47513. + DWC_MUTEX_LOCK(cc_if->mutex);
  47514. + cc = cc_find(cc_if, id);
  47515. + if (!cc) {
  47516. + DWC_ERROR("Uid %d not found in cc list\n", id);
  47517. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  47518. + return;
  47519. + }
  47520. +
  47521. + if (chid) {
  47522. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  47523. + }
  47524. + if (cdid) {
  47525. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  47526. + }
  47527. + if (ck) {
  47528. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  47529. + }
  47530. +
  47531. + if (name) {
  47532. + if (cc->name) {
  47533. + dwc_free(mem_ctx, cc->name);
  47534. + }
  47535. + cc->name = dwc_alloc(mem_ctx, length);
  47536. + if (!cc->name) {
  47537. + DWC_ERROR("Out of memory in dwc_cc_change()\n");
  47538. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  47539. + return;
  47540. + }
  47541. + cc->length = length;
  47542. + DWC_MEMCPY(cc->name, name, length);
  47543. + }
  47544. +
  47545. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  47546. +
  47547. + cc_changed(cc_if);
  47548. +
  47549. + DWC_DEBUGC("Changed connection context id=%d\n", id);
  47550. + dump_bytes("New CHID", cc->chid, 16);
  47551. + dump_bytes("New CDID", cc->cdid, 16);
  47552. + dump_bytes("New CK", cc->ck, 16);
  47553. +}
  47554. +
  47555. +void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id)
  47556. +{
  47557. + dwc_cc_t *cc;
  47558. +
  47559. + DWC_DEBUGC("Removing connection context %d", id);
  47560. +
  47561. + DWC_MUTEX_LOCK(cc_if->mutex);
  47562. + cc = cc_find(cc_if, id);
  47563. + if (!cc) {
  47564. + DWC_ERROR("Uid %d not found in cc list\n", id);
  47565. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  47566. + return;
  47567. + }
  47568. +
  47569. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  47570. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  47571. + free_cc(mem_ctx, cc);
  47572. +
  47573. + cc_changed(cc_if);
  47574. +}
  47575. +
  47576. +uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if, unsigned int *length)
  47577. +{
  47578. + uint8_t *buf, *x;
  47579. + uint8_t zero = 0;
  47580. + dwc_cc_t *cc;
  47581. +
  47582. + DWC_MUTEX_LOCK(cc_if->mutex);
  47583. + *length = cc_data_size(cc_if);
  47584. + if (!(*length)) {
  47585. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  47586. + return NULL;
  47587. + }
  47588. +
  47589. + DWC_DEBUGC("Creating data for saving (length=%d)", *length);
  47590. +
  47591. + buf = dwc_alloc(mem_ctx, *length);
  47592. + if (!buf) {
  47593. + *length = 0;
  47594. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  47595. + return NULL;
  47596. + }
  47597. +
  47598. + x = buf;
  47599. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  47600. + DWC_MEMCPY(x, cc->chid, 16);
  47601. + x += 16;
  47602. + DWC_MEMCPY(x, cc->cdid, 16);
  47603. + x += 16;
  47604. + DWC_MEMCPY(x, cc->ck, 16);
  47605. + x += 16;
  47606. + if (cc->name) {
  47607. + DWC_MEMCPY(x, &cc->length, 1);
  47608. + x += 1;
  47609. + DWC_MEMCPY(x, cc->name, cc->length);
  47610. + x += cc->length;
  47611. + }
  47612. + else {
  47613. + DWC_MEMCPY(x, &zero, 1);
  47614. + x += 1;
  47615. + }
  47616. + }
  47617. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  47618. +
  47619. + return buf;
  47620. +}
  47621. +
  47622. +void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *data, uint32_t length)
  47623. +{
  47624. + uint8_t name_length;
  47625. + uint8_t *name;
  47626. + uint8_t *chid;
  47627. + uint8_t *cdid;
  47628. + uint8_t *ck;
  47629. + uint32_t i = 0;
  47630. +
  47631. + DWC_MUTEX_LOCK(cc_if->mutex);
  47632. + cc_clear(mem_ctx, cc_if);
  47633. +
  47634. + while (i < length) {
  47635. + chid = &data[i];
  47636. + i += 16;
  47637. + cdid = &data[i];
  47638. + i += 16;
  47639. + ck = &data[i];
  47640. + i += 16;
  47641. +
  47642. + name_length = data[i];
  47643. + i ++;
  47644. +
  47645. + if (name_length) {
  47646. + name = &data[i];
  47647. + i += name_length;
  47648. + }
  47649. + else {
  47650. + name = NULL;
  47651. + }
  47652. +
  47653. + /* check to see if we haven't overflown the buffer */
  47654. + if (i > length) {
  47655. + DWC_ERROR("Data format error while attempting to load CCs "
  47656. + "(nlen=%d, iter=%d, buflen=%d).\n", name_length, i, length);
  47657. + break;
  47658. + }
  47659. +
  47660. + cc_add(mem_ctx, cc_if, chid, cdid, ck, name, name_length);
  47661. + }
  47662. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  47663. +
  47664. + cc_changed(cc_if);
  47665. +}
  47666. +
  47667. +uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  47668. +{
  47669. + uint32_t uid = 0;
  47670. +
  47671. + DWC_MUTEX_LOCK(cc_if->mutex);
  47672. + uid = cc_match_chid(cc_if, chid);
  47673. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  47674. + return uid;
  47675. +}
  47676. +uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  47677. +{
  47678. + uint32_t uid = 0;
  47679. +
  47680. + DWC_MUTEX_LOCK(cc_if->mutex);
  47681. + uid = cc_match_cdid(cc_if, cdid);
  47682. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  47683. + return uid;
  47684. +}
  47685. +
  47686. +uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id)
  47687. +{
  47688. + uint8_t *ck = NULL;
  47689. + dwc_cc_t *cc;
  47690. +
  47691. + DWC_MUTEX_LOCK(cc_if->mutex);
  47692. + cc = cc_find(cc_if, id);
  47693. + if (cc) {
  47694. + ck = cc->ck;
  47695. + }
  47696. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  47697. +
  47698. + return ck;
  47699. +
  47700. +}
  47701. +
  47702. +uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id)
  47703. +{
  47704. + uint8_t *retval = NULL;
  47705. + dwc_cc_t *cc;
  47706. +
  47707. + DWC_MUTEX_LOCK(cc_if->mutex);
  47708. + cc = cc_find(cc_if, id);
  47709. + if (cc) {
  47710. + retval = cc->chid;
  47711. + }
  47712. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  47713. +
  47714. + return retval;
  47715. +}
  47716. +
  47717. +uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id)
  47718. +{
  47719. + uint8_t *retval = NULL;
  47720. + dwc_cc_t *cc;
  47721. +
  47722. + DWC_MUTEX_LOCK(cc_if->mutex);
  47723. + cc = cc_find(cc_if, id);
  47724. + if (cc) {
  47725. + retval = cc->cdid;
  47726. + }
  47727. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  47728. +
  47729. + return retval;
  47730. +}
  47731. +
  47732. +uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length)
  47733. +{
  47734. + uint8_t *retval = NULL;
  47735. + dwc_cc_t *cc;
  47736. +
  47737. + DWC_MUTEX_LOCK(cc_if->mutex);
  47738. + *length = 0;
  47739. + cc = cc_find(cc_if, id);
  47740. + if (cc) {
  47741. + *length = cc->length;
  47742. + retval = cc->name;
  47743. + }
  47744. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  47745. +
  47746. + return retval;
  47747. +}
  47748. +
  47749. +#endif /* DWC_CCLIB */
  47750. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_cc.h linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_cc.h
  47751. --- linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_cc.h 1969-12-31 18:00:00.000000000 -0600
  47752. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_cc.h 2014-12-03 19:13:40.212418001 -0600
  47753. @@ -0,0 +1,224 @@
  47754. +/* =========================================================================
  47755. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.h $
  47756. + * $Revision: #4 $
  47757. + * $Date: 2010/09/28 $
  47758. + * $Change: 1596182 $
  47759. + *
  47760. + * Synopsys Portability Library Software and documentation
  47761. + * (hereinafter, "Software") is an Unsupported proprietary work of
  47762. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  47763. + * between Synopsys and you.
  47764. + *
  47765. + * The Software IS NOT an item of Licensed Software or Licensed Product
  47766. + * under any End User Software License Agreement or Agreement for
  47767. + * Licensed Product with Synopsys or any supplement thereto. You are
  47768. + * permitted to use and redistribute this Software in source and binary
  47769. + * forms, with or without modification, provided that redistributions
  47770. + * of source code must retain this notice. You may not view, use,
  47771. + * disclose, copy or distribute this file or any information contained
  47772. + * herein except pursuant to this license grant from Synopsys. If you
  47773. + * do not agree with this notice, including the disclaimer below, then
  47774. + * you are not authorized to use the Software.
  47775. + *
  47776. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  47777. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  47778. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  47779. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  47780. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  47781. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  47782. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  47783. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  47784. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  47785. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  47786. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  47787. + * DAMAGE.
  47788. + * ========================================================================= */
  47789. +#ifndef _DWC_CC_H_
  47790. +#define _DWC_CC_H_
  47791. +
  47792. +#ifdef __cplusplus
  47793. +extern "C" {
  47794. +#endif
  47795. +
  47796. +/** @file
  47797. + *
  47798. + * This file defines the Context Context library.
  47799. + *
  47800. + * The main data structure is dwc_cc_if_t which is returned by either the
  47801. + * dwc_cc_if_alloc function or returned by the module to the user via a provided
  47802. + * function. The data structure is opaque and should only be manipulated via the
  47803. + * functions provied in this API.
  47804. + *
  47805. + * It manages a list of connection contexts and operations can be performed to
  47806. + * add, remove, query, search, and change, those contexts. Additionally,
  47807. + * a dwc_notifier_t object can be requested from the manager so that
  47808. + * the user can be notified whenever the context list has changed.
  47809. + */
  47810. +
  47811. +#include "dwc_os.h"
  47812. +#include "dwc_list.h"
  47813. +#include "dwc_notifier.h"
  47814. +
  47815. +
  47816. +/* Notifications */
  47817. +#define DWC_CC_LIST_CHANGED_NOTIFICATION "DWC_CC_LIST_CHANGED_NOTIFICATION"
  47818. +
  47819. +struct dwc_cc_if;
  47820. +typedef struct dwc_cc_if dwc_cc_if_t;
  47821. +
  47822. +
  47823. +/** @name Connection Context Operations */
  47824. +/** @{ */
  47825. +
  47826. +/** This function allocates memory for a dwc_cc_if_t structure, initializes
  47827. + * fields to default values, and returns a pointer to the structure or NULL on
  47828. + * error. */
  47829. +extern dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  47830. + dwc_notifier_t *notifier, unsigned is_host);
  47831. +
  47832. +/** Frees the memory for the specified CC structure allocated from
  47833. + * dwc_cc_if_alloc(). */
  47834. +extern void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if);
  47835. +
  47836. +/** Removes all contexts from the connection context list */
  47837. +extern void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if);
  47838. +
  47839. +/** Adds a connection context (CHID, CK, CDID, Name) to the connection context list.
  47840. + * If a CHID already exists, the CK and name are overwritten. Statistics are
  47841. + * not overwritten.
  47842. + *
  47843. + * @param cc_if The cc_if structure.
  47844. + * @param chid A pointer to the 16-byte CHID. This value will be copied.
  47845. + * @param ck A pointer to the 16-byte CK. This value will be copied.
  47846. + * @param cdid A pointer to the 16-byte CDID. This value will be copied.
  47847. + * @param name An optional host friendly name as defined in the association model
  47848. + * spec. Must be a UTF16-LE unicode string. Can be NULL to indicated no name.
  47849. + * @param length The length othe unicode string.
  47850. + * @return A unique identifier used to refer to this context that is valid for
  47851. + * as long as this context is still in the list. */
  47852. +extern int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  47853. + uint8_t *cdid, uint8_t *ck, uint8_t *name,
  47854. + uint8_t length);
  47855. +
  47856. +/** Changes the CHID, CK, CDID, or Name values of a connection context in the
  47857. + * list, preserving any accumulated statistics. This would typically be called
  47858. + * if the host decideds to change the context with a SET_CONNECTION request.
  47859. + *
  47860. + * @param cc_if The cc_if structure.
  47861. + * @param id The identifier of the connection context.
  47862. + * @param chid A pointer to the 16-byte CHID. This value will be copied. NULL
  47863. + * indicates no change.
  47864. + * @param cdid A pointer to the 16-byte CDID. This value will be copied. NULL
  47865. + * indicates no change.
  47866. + * @param ck A pointer to the 16-byte CK. This value will be copied. NULL
  47867. + * indicates no change.
  47868. + * @param name Host friendly name UTF16-LE. NULL indicates no change.
  47869. + * @param length Length of name. */
  47870. +extern void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id,
  47871. + uint8_t *chid, uint8_t *cdid, uint8_t *ck,
  47872. + uint8_t *name, uint8_t length);
  47873. +
  47874. +/** Remove the specified connection context.
  47875. + * @param cc_if The cc_if structure.
  47876. + * @param id The identifier of the connection context to remove. */
  47877. +extern void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id);
  47878. +
  47879. +/** Get a binary block of data for the connection context list and attributes.
  47880. + * This data can be used by the OS specific driver to save the connection
  47881. + * context list into non-volatile memory.
  47882. + *
  47883. + * @param cc_if The cc_if structure.
  47884. + * @param length Return the length of the data buffer.
  47885. + * @return A pointer to the data buffer. The memory for this buffer should be
  47886. + * freed with DWC_FREE() after use. */
  47887. +extern uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if,
  47888. + unsigned int *length);
  47889. +
  47890. +/** Restore the connection context list from the binary data that was previously
  47891. + * returned from a call to dwc_cc_data_for_save. This can be used by the OS specific
  47892. + * driver to load a connection context list from non-volatile memory.
  47893. + *
  47894. + * @param cc_if The cc_if structure.
  47895. + * @param data The data bytes as returned from dwc_cc_data_for_save.
  47896. + * @param length The length of the data. */
  47897. +extern void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if,
  47898. + uint8_t *data, unsigned int length);
  47899. +
  47900. +/** Find the connection context from the specified CHID.
  47901. + *
  47902. + * @param cc_if The cc_if structure.
  47903. + * @param chid A pointer to the CHID data.
  47904. + * @return A non-zero identifier of the connection context if the CHID matches.
  47905. + * Otherwise returns 0. */
  47906. +extern uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid);
  47907. +
  47908. +/** Find the connection context from the specified CDID.
  47909. + *
  47910. + * @param cc_if The cc_if structure.
  47911. + * @param cdid A pointer to the CDID data.
  47912. + * @return A non-zero identifier of the connection context if the CHID matches.
  47913. + * Otherwise returns 0. */
  47914. +extern uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid);
  47915. +
  47916. +/** Retrieve the CK from the specified connection context.
  47917. + *
  47918. + * @param cc_if The cc_if structure.
  47919. + * @param id The identifier of the connection context.
  47920. + * @return A pointer to the CK data. The memory does not need to be freed. */
  47921. +extern uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id);
  47922. +
  47923. +/** Retrieve the CHID from the specified connection context.
  47924. + *
  47925. + * @param cc_if The cc_if structure.
  47926. + * @param id The identifier of the connection context.
  47927. + * @return A pointer to the CHID data. The memory does not need to be freed. */
  47928. +extern uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id);
  47929. +
  47930. +/** Retrieve the CDID from the specified connection context.
  47931. + *
  47932. + * @param cc_if The cc_if structure.
  47933. + * @param id The identifier of the connection context.
  47934. + * @return A pointer to the CDID data. The memory does not need to be freed. */
  47935. +extern uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id);
  47936. +
  47937. +extern uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length);
  47938. +
  47939. +/** Checks a buffer for non-zero.
  47940. + * @param id A pointer to a 16 byte buffer.
  47941. + * @return true if the 16 byte value is non-zero. */
  47942. +static inline unsigned dwc_assoc_is_not_zero_id(uint8_t *id) {
  47943. + int i;
  47944. + for (i=0; i<16; i++) {
  47945. + if (id[i]) return 1;
  47946. + }
  47947. + return 0;
  47948. +}
  47949. +
  47950. +/** Checks a buffer for zero.
  47951. + * @param id A pointer to a 16 byte buffer.
  47952. + * @return true if the 16 byte value is zero. */
  47953. +static inline unsigned dwc_assoc_is_zero_id(uint8_t *id) {
  47954. + return !dwc_assoc_is_not_zero_id(id);
  47955. +}
  47956. +
  47957. +/** Prints an ASCII representation for the 16-byte chid, cdid, or ck, into
  47958. + * buffer. */
  47959. +static inline int dwc_print_id_string(char *buffer, uint8_t *id) {
  47960. + char *ptr = buffer;
  47961. + int i;
  47962. + for (i=0; i<16; i++) {
  47963. + ptr += DWC_SPRINTF(ptr, "%02x", id[i]);
  47964. + if (i < 15) {
  47965. + ptr += DWC_SPRINTF(ptr, " ");
  47966. + }
  47967. + }
  47968. + return ptr - buffer;
  47969. +}
  47970. +
  47971. +/** @} */
  47972. +
  47973. +#ifdef __cplusplus
  47974. +}
  47975. +#endif
  47976. +
  47977. +#endif /* _DWC_CC_H_ */
  47978. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
  47979. --- linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c 1969-12-31 18:00:00.000000000 -0600
  47980. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c 2014-12-03 19:13:40.212418001 -0600
  47981. @@ -0,0 +1,1308 @@
  47982. +#include "dwc_os.h"
  47983. +#include "dwc_list.h"
  47984. +
  47985. +#ifdef DWC_CCLIB
  47986. +# include "dwc_cc.h"
  47987. +#endif
  47988. +
  47989. +#ifdef DWC_CRYPTOLIB
  47990. +# include "dwc_modpow.h"
  47991. +# include "dwc_dh.h"
  47992. +# include "dwc_crypto.h"
  47993. +#endif
  47994. +
  47995. +#ifdef DWC_NOTIFYLIB
  47996. +# include "dwc_notifier.h"
  47997. +#endif
  47998. +
  47999. +/* OS-Level Implementations */
  48000. +
  48001. +/* This is the FreeBSD 7.0 kernel implementation of the DWC platform library. */
  48002. +
  48003. +
  48004. +/* MISC */
  48005. +
  48006. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  48007. +{
  48008. + return memset(dest, byte, size);
  48009. +}
  48010. +
  48011. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  48012. +{
  48013. + return memcpy(dest, src, size);
  48014. +}
  48015. +
  48016. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  48017. +{
  48018. + bcopy(src, dest, size);
  48019. + return dest;
  48020. +}
  48021. +
  48022. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  48023. +{
  48024. + return memcmp(m1, m2, size);
  48025. +}
  48026. +
  48027. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  48028. +{
  48029. + return strncmp(s1, s2, size);
  48030. +}
  48031. +
  48032. +int DWC_STRCMP(void *s1, void *s2)
  48033. +{
  48034. + return strcmp(s1, s2);
  48035. +}
  48036. +
  48037. +int DWC_STRLEN(char const *str)
  48038. +{
  48039. + return strlen(str);
  48040. +}
  48041. +
  48042. +char *DWC_STRCPY(char *to, char const *from)
  48043. +{
  48044. + return strcpy(to, from);
  48045. +}
  48046. +
  48047. +char *DWC_STRDUP(char const *str)
  48048. +{
  48049. + int len = DWC_STRLEN(str) + 1;
  48050. + char *new = DWC_ALLOC_ATOMIC(len);
  48051. +
  48052. + if (!new) {
  48053. + return NULL;
  48054. + }
  48055. +
  48056. + DWC_MEMCPY(new, str, len);
  48057. + return new;
  48058. +}
  48059. +
  48060. +int DWC_ATOI(char *str, int32_t *value)
  48061. +{
  48062. + char *end = NULL;
  48063. +
  48064. + *value = strtol(str, &end, 0);
  48065. + if (*end == '\0') {
  48066. + return 0;
  48067. + }
  48068. +
  48069. + return -1;
  48070. +}
  48071. +
  48072. +int DWC_ATOUI(char *str, uint32_t *value)
  48073. +{
  48074. + char *end = NULL;
  48075. +
  48076. + *value = strtoul(str, &end, 0);
  48077. + if (*end == '\0') {
  48078. + return 0;
  48079. + }
  48080. +
  48081. + return -1;
  48082. +}
  48083. +
  48084. +
  48085. +#ifdef DWC_UTFLIB
  48086. +/* From usbstring.c */
  48087. +
  48088. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  48089. +{
  48090. + int count = 0;
  48091. + u8 c;
  48092. + u16 uchar;
  48093. +
  48094. + /* this insists on correct encodings, though not minimal ones.
  48095. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  48096. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  48097. + */
  48098. + while (len != 0 && (c = (u8) *s++) != 0) {
  48099. + if (unlikely(c & 0x80)) {
  48100. + // 2-byte sequence:
  48101. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  48102. + if ((c & 0xe0) == 0xc0) {
  48103. + uchar = (c & 0x1f) << 6;
  48104. +
  48105. + c = (u8) *s++;
  48106. + if ((c & 0xc0) != 0xc0)
  48107. + goto fail;
  48108. + c &= 0x3f;
  48109. + uchar |= c;
  48110. +
  48111. + // 3-byte sequence (most CJKV characters):
  48112. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  48113. + } else if ((c & 0xf0) == 0xe0) {
  48114. + uchar = (c & 0x0f) << 12;
  48115. +
  48116. + c = (u8) *s++;
  48117. + if ((c & 0xc0) != 0xc0)
  48118. + goto fail;
  48119. + c &= 0x3f;
  48120. + uchar |= c << 6;
  48121. +
  48122. + c = (u8) *s++;
  48123. + if ((c & 0xc0) != 0xc0)
  48124. + goto fail;
  48125. + c &= 0x3f;
  48126. + uchar |= c;
  48127. +
  48128. + /* no bogus surrogates */
  48129. + if (0xd800 <= uchar && uchar <= 0xdfff)
  48130. + goto fail;
  48131. +
  48132. + // 4-byte sequence (surrogate pairs, currently rare):
  48133. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  48134. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  48135. + // (uuuuu = wwww + 1)
  48136. + // FIXME accept the surrogate code points (only)
  48137. + } else
  48138. + goto fail;
  48139. + } else
  48140. + uchar = c;
  48141. + put_unaligned (cpu_to_le16 (uchar), cp++);
  48142. + count++;
  48143. + len--;
  48144. + }
  48145. + return count;
  48146. +fail:
  48147. + return -1;
  48148. +}
  48149. +
  48150. +#endif /* DWC_UTFLIB */
  48151. +
  48152. +
  48153. +/* dwc_debug.h */
  48154. +
  48155. +dwc_bool_t DWC_IN_IRQ(void)
  48156. +{
  48157. +// return in_irq();
  48158. + return 0;
  48159. +}
  48160. +
  48161. +dwc_bool_t DWC_IN_BH(void)
  48162. +{
  48163. +// return in_softirq();
  48164. + return 0;
  48165. +}
  48166. +
  48167. +void DWC_VPRINTF(char *format, va_list args)
  48168. +{
  48169. + vprintf(format, args);
  48170. +}
  48171. +
  48172. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  48173. +{
  48174. + return vsnprintf(str, size, format, args);
  48175. +}
  48176. +
  48177. +void DWC_PRINTF(char *format, ...)
  48178. +{
  48179. + va_list args;
  48180. +
  48181. + va_start(args, format);
  48182. + DWC_VPRINTF(format, args);
  48183. + va_end(args);
  48184. +}
  48185. +
  48186. +int DWC_SPRINTF(char *buffer, char *format, ...)
  48187. +{
  48188. + int retval;
  48189. + va_list args;
  48190. +
  48191. + va_start(args, format);
  48192. + retval = vsprintf(buffer, format, args);
  48193. + va_end(args);
  48194. + return retval;
  48195. +}
  48196. +
  48197. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  48198. +{
  48199. + int retval;
  48200. + va_list args;
  48201. +
  48202. + va_start(args, format);
  48203. + retval = vsnprintf(buffer, size, format, args);
  48204. + va_end(args);
  48205. + return retval;
  48206. +}
  48207. +
  48208. +void __DWC_WARN(char *format, ...)
  48209. +{
  48210. + va_list args;
  48211. +
  48212. + va_start(args, format);
  48213. + DWC_VPRINTF(format, args);
  48214. + va_end(args);
  48215. +}
  48216. +
  48217. +void __DWC_ERROR(char *format, ...)
  48218. +{
  48219. + va_list args;
  48220. +
  48221. + va_start(args, format);
  48222. + DWC_VPRINTF(format, args);
  48223. + va_end(args);
  48224. +}
  48225. +
  48226. +void DWC_EXCEPTION(char *format, ...)
  48227. +{
  48228. + va_list args;
  48229. +
  48230. + va_start(args, format);
  48231. + DWC_VPRINTF(format, args);
  48232. + va_end(args);
  48233. +// BUG_ON(1); ???
  48234. +}
  48235. +
  48236. +#ifdef DEBUG
  48237. +void __DWC_DEBUG(char *format, ...)
  48238. +{
  48239. + va_list args;
  48240. +
  48241. + va_start(args, format);
  48242. + DWC_VPRINTF(format, args);
  48243. + va_end(args);
  48244. +}
  48245. +#endif
  48246. +
  48247. +
  48248. +/* dwc_mem.h */
  48249. +
  48250. +#if 0
  48251. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  48252. + uint32_t align,
  48253. + uint32_t alloc)
  48254. +{
  48255. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  48256. + size, align, alloc);
  48257. + return (dwc_pool_t *)pool;
  48258. +}
  48259. +
  48260. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  48261. +{
  48262. + dma_pool_destroy((struct dma_pool *)pool);
  48263. +}
  48264. +
  48265. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  48266. +{
  48267. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  48268. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  48269. +}
  48270. +
  48271. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  48272. +{
  48273. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  48274. + memset(..);
  48275. +}
  48276. +
  48277. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  48278. +{
  48279. + dma_pool_free(pool, vaddr, daddr);
  48280. +}
  48281. +#endif
  48282. +
  48283. +static void dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  48284. +{
  48285. + if (error)
  48286. + return;
  48287. + *(bus_addr_t *)arg = segs[0].ds_addr;
  48288. +}
  48289. +
  48290. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  48291. +{
  48292. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  48293. + int error;
  48294. +
  48295. + error = bus_dma_tag_create(
  48296. +#if __FreeBSD_version >= 700000
  48297. + bus_get_dma_tag(dma->dev), /* parent */
  48298. +#else
  48299. + NULL, /* parent */
  48300. +#endif
  48301. + 4, 0, /* alignment, bounds */
  48302. + BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
  48303. + BUS_SPACE_MAXADDR, /* highaddr */
  48304. + NULL, NULL, /* filter, filterarg */
  48305. + size, /* maxsize */
  48306. + 1, /* nsegments */
  48307. + size, /* maxsegsize */
  48308. + 0, /* flags */
  48309. + NULL, /* lockfunc */
  48310. + NULL, /* lockarg */
  48311. + &dma->dma_tag);
  48312. + if (error) {
  48313. + device_printf(dma->dev, "%s: bus_dma_tag_create failed: %d\n",
  48314. + __func__, error);
  48315. + goto fail_0;
  48316. + }
  48317. +
  48318. + error = bus_dmamem_alloc(dma->dma_tag, &dma->dma_vaddr,
  48319. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dma->dma_map);
  48320. + if (error) {
  48321. + device_printf(dma->dev, "%s: bus_dmamem_alloc(%ju) failed: %d\n",
  48322. + __func__, (uintmax_t)size, error);
  48323. + goto fail_1;
  48324. + }
  48325. +
  48326. + dma->dma_paddr = 0;
  48327. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, size,
  48328. + dmamap_cb, &dma->dma_paddr, BUS_DMA_NOWAIT);
  48329. + if (error || dma->dma_paddr == 0) {
  48330. + device_printf(dma->dev, "%s: bus_dmamap_load failed: %d\n",
  48331. + __func__, error);
  48332. + goto fail_2;
  48333. + }
  48334. +
  48335. + *dma_addr = dma->dma_paddr;
  48336. + return dma->dma_vaddr;
  48337. +
  48338. +fail_2:
  48339. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  48340. +fail_1:
  48341. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  48342. + bus_dma_tag_destroy(dma->dma_tag);
  48343. +fail_0:
  48344. + dma->dma_map = NULL;
  48345. + dma->dma_tag = NULL;
  48346. +
  48347. + return NULL;
  48348. +}
  48349. +
  48350. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  48351. +{
  48352. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  48353. +
  48354. + if (dma->dma_tag == NULL)
  48355. + return;
  48356. + if (dma->dma_map != NULL) {
  48357. + bus_dmamap_sync(dma->dma_tag, dma->dma_map,
  48358. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  48359. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  48360. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  48361. + dma->dma_map = NULL;
  48362. + }
  48363. +
  48364. + bus_dma_tag_destroy(dma->dma_tag);
  48365. + dma->dma_tag = NULL;
  48366. +}
  48367. +
  48368. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  48369. +{
  48370. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  48371. +}
  48372. +
  48373. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  48374. +{
  48375. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  48376. +}
  48377. +
  48378. +void __DWC_FREE(void *mem_ctx, void *addr)
  48379. +{
  48380. + free(addr, M_DEVBUF);
  48381. +}
  48382. +
  48383. +
  48384. +#ifdef DWC_CRYPTOLIB
  48385. +/* dwc_crypto.h */
  48386. +
  48387. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  48388. +{
  48389. + get_random_bytes(buffer, length);
  48390. +}
  48391. +
  48392. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  48393. +{
  48394. + struct crypto_blkcipher *tfm;
  48395. + struct blkcipher_desc desc;
  48396. + struct scatterlist sgd;
  48397. + struct scatterlist sgs;
  48398. +
  48399. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  48400. + if (tfm == NULL) {
  48401. + printk("failed to load transform for aes CBC\n");
  48402. + return -1;
  48403. + }
  48404. +
  48405. + crypto_blkcipher_setkey(tfm, key, keylen);
  48406. + crypto_blkcipher_set_iv(tfm, iv, 16);
  48407. +
  48408. + sg_init_one(&sgd, out, messagelen);
  48409. + sg_init_one(&sgs, message, messagelen);
  48410. +
  48411. + desc.tfm = tfm;
  48412. + desc.flags = 0;
  48413. +
  48414. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  48415. + crypto_free_blkcipher(tfm);
  48416. + DWC_ERROR("AES CBC encryption failed");
  48417. + return -1;
  48418. + }
  48419. +
  48420. + crypto_free_blkcipher(tfm);
  48421. + return 0;
  48422. +}
  48423. +
  48424. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  48425. +{
  48426. + struct crypto_hash *tfm;
  48427. + struct hash_desc desc;
  48428. + struct scatterlist sg;
  48429. +
  48430. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  48431. + if (IS_ERR(tfm)) {
  48432. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  48433. + return 0;
  48434. + }
  48435. + desc.tfm = tfm;
  48436. + desc.flags = 0;
  48437. +
  48438. + sg_init_one(&sg, message, len);
  48439. + crypto_hash_digest(&desc, &sg, len, out);
  48440. + crypto_free_hash(tfm);
  48441. +
  48442. + return 1;
  48443. +}
  48444. +
  48445. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  48446. + uint8_t *key, uint32_t keylen, uint8_t *out)
  48447. +{
  48448. + struct crypto_hash *tfm;
  48449. + struct hash_desc desc;
  48450. + struct scatterlist sg;
  48451. +
  48452. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  48453. + if (IS_ERR(tfm)) {
  48454. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  48455. + return 0;
  48456. + }
  48457. + desc.tfm = tfm;
  48458. + desc.flags = 0;
  48459. +
  48460. + sg_init_one(&sg, message, messagelen);
  48461. + crypto_hash_setkey(tfm, key, keylen);
  48462. + crypto_hash_digest(&desc, &sg, messagelen, out);
  48463. + crypto_free_hash(tfm);
  48464. +
  48465. + return 1;
  48466. +}
  48467. +
  48468. +#endif /* DWC_CRYPTOLIB */
  48469. +
  48470. +
  48471. +/* Byte Ordering Conversions */
  48472. +
  48473. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  48474. +{
  48475. +#ifdef __LITTLE_ENDIAN
  48476. + return *p;
  48477. +#else
  48478. + uint8_t *u_p = (uint8_t *)p;
  48479. +
  48480. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  48481. +#endif
  48482. +}
  48483. +
  48484. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  48485. +{
  48486. +#ifdef __BIG_ENDIAN
  48487. + return *p;
  48488. +#else
  48489. + uint8_t *u_p = (uint8_t *)p;
  48490. +
  48491. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  48492. +#endif
  48493. +}
  48494. +
  48495. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  48496. +{
  48497. +#ifdef __LITTLE_ENDIAN
  48498. + return *p;
  48499. +#else
  48500. + uint8_t *u_p = (uint8_t *)p;
  48501. +
  48502. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  48503. +#endif
  48504. +}
  48505. +
  48506. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  48507. +{
  48508. +#ifdef __BIG_ENDIAN
  48509. + return *p;
  48510. +#else
  48511. + uint8_t *u_p = (uint8_t *)p;
  48512. +
  48513. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  48514. +#endif
  48515. +}
  48516. +
  48517. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  48518. +{
  48519. +#ifdef __LITTLE_ENDIAN
  48520. + return *p;
  48521. +#else
  48522. + uint8_t *u_p = (uint8_t *)p;
  48523. + return (u_p[1] | (u_p[0] << 8));
  48524. +#endif
  48525. +}
  48526. +
  48527. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  48528. +{
  48529. +#ifdef __BIG_ENDIAN
  48530. + return *p;
  48531. +#else
  48532. + uint8_t *u_p = (uint8_t *)p;
  48533. + return (u_p[1] | (u_p[0] << 8));
  48534. +#endif
  48535. +}
  48536. +
  48537. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  48538. +{
  48539. +#ifdef __LITTLE_ENDIAN
  48540. + return *p;
  48541. +#else
  48542. + uint8_t *u_p = (uint8_t *)p;
  48543. + return (u_p[1] | (u_p[0] << 8));
  48544. +#endif
  48545. +}
  48546. +
  48547. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  48548. +{
  48549. +#ifdef __BIG_ENDIAN
  48550. + return *p;
  48551. +#else
  48552. + uint8_t *u_p = (uint8_t *)p;
  48553. + return (u_p[1] | (u_p[0] << 8));
  48554. +#endif
  48555. +}
  48556. +
  48557. +
  48558. +/* Registers */
  48559. +
  48560. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  48561. +{
  48562. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  48563. + bus_size_t ior = (bus_size_t)reg;
  48564. +
  48565. + return bus_space_read_4(io->iot, io->ioh, ior);
  48566. +}
  48567. +
  48568. +#if 0
  48569. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  48570. +{
  48571. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  48572. + bus_size_t ior = (bus_size_t)reg;
  48573. +
  48574. + return bus_space_read_8(io->iot, io->ioh, ior);
  48575. +}
  48576. +#endif
  48577. +
  48578. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  48579. +{
  48580. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  48581. + bus_size_t ior = (bus_size_t)reg;
  48582. +
  48583. + bus_space_write_4(io->iot, io->ioh, ior, value);
  48584. +}
  48585. +
  48586. +#if 0
  48587. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  48588. +{
  48589. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  48590. + bus_size_t ior = (bus_size_t)reg;
  48591. +
  48592. + bus_space_write_8(io->iot, io->ioh, ior, value);
  48593. +}
  48594. +#endif
  48595. +
  48596. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  48597. + uint32_t set_mask)
  48598. +{
  48599. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  48600. + bus_size_t ior = (bus_size_t)reg;
  48601. +
  48602. + bus_space_write_4(io->iot, io->ioh, ior,
  48603. + (bus_space_read_4(io->iot, io->ioh, ior) &
  48604. + ~clear_mask) | set_mask);
  48605. +}
  48606. +
  48607. +#if 0
  48608. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  48609. + uint64_t set_mask)
  48610. +{
  48611. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  48612. + bus_size_t ior = (bus_size_t)reg;
  48613. +
  48614. + bus_space_write_8(io->iot, io->ioh, ior,
  48615. + (bus_space_read_8(io->iot, io->ioh, ior) &
  48616. + ~clear_mask) | set_mask);
  48617. +}
  48618. +#endif
  48619. +
  48620. +
  48621. +/* Locking */
  48622. +
  48623. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  48624. +{
  48625. + struct mtx *sl = DWC_ALLOC(sizeof(*sl));
  48626. +
  48627. + if (!sl) {
  48628. + DWC_ERROR("Cannot allocate memory for spinlock");
  48629. + return NULL;
  48630. + }
  48631. +
  48632. + mtx_init(sl, "dw3spn", NULL, MTX_SPIN);
  48633. + return (dwc_spinlock_t *)sl;
  48634. +}
  48635. +
  48636. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  48637. +{
  48638. + struct mtx *sl = (struct mtx *)lock;
  48639. +
  48640. + mtx_destroy(sl);
  48641. + DWC_FREE(sl);
  48642. +}
  48643. +
  48644. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  48645. +{
  48646. + mtx_lock_spin((struct mtx *)lock); // ???
  48647. +}
  48648. +
  48649. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  48650. +{
  48651. + mtx_unlock_spin((struct mtx *)lock); // ???
  48652. +}
  48653. +
  48654. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  48655. +{
  48656. + mtx_lock_spin((struct mtx *)lock);
  48657. +}
  48658. +
  48659. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  48660. +{
  48661. + mtx_unlock_spin((struct mtx *)lock);
  48662. +}
  48663. +
  48664. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  48665. +{
  48666. + struct mtx *m;
  48667. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mtx));
  48668. +
  48669. + if (!mutex) {
  48670. + DWC_ERROR("Cannot allocate memory for mutex");
  48671. + return NULL;
  48672. + }
  48673. +
  48674. + m = (struct mtx *)mutex;
  48675. + mtx_init(m, "dw3mtx", NULL, MTX_DEF);
  48676. + return mutex;
  48677. +}
  48678. +
  48679. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  48680. +#else
  48681. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  48682. +{
  48683. + mtx_destroy((struct mtx *)mutex);
  48684. + DWC_FREE(mutex);
  48685. +}
  48686. +#endif
  48687. +
  48688. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  48689. +{
  48690. + struct mtx *m = (struct mtx *)mutex;
  48691. +
  48692. + mtx_lock(m);
  48693. +}
  48694. +
  48695. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  48696. +{
  48697. + struct mtx *m = (struct mtx *)mutex;
  48698. +
  48699. + return mtx_trylock(m);
  48700. +}
  48701. +
  48702. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  48703. +{
  48704. + struct mtx *m = (struct mtx *)mutex;
  48705. +
  48706. + mtx_unlock(m);
  48707. +}
  48708. +
  48709. +
  48710. +/* Timing */
  48711. +
  48712. +void DWC_UDELAY(uint32_t usecs)
  48713. +{
  48714. + DELAY(usecs);
  48715. +}
  48716. +
  48717. +void DWC_MDELAY(uint32_t msecs)
  48718. +{
  48719. + do {
  48720. + DELAY(1000);
  48721. + } while (--msecs);
  48722. +}
  48723. +
  48724. +void DWC_MSLEEP(uint32_t msecs)
  48725. +{
  48726. + struct timeval tv;
  48727. +
  48728. + tv.tv_sec = msecs / 1000;
  48729. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  48730. + pause("dw3slp", tvtohz(&tv));
  48731. +}
  48732. +
  48733. +uint32_t DWC_TIME(void)
  48734. +{
  48735. + struct timeval tv;
  48736. +
  48737. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  48738. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  48739. +}
  48740. +
  48741. +
  48742. +/* Timers */
  48743. +
  48744. +struct dwc_timer {
  48745. + struct callout t;
  48746. + char *name;
  48747. + dwc_spinlock_t *lock;
  48748. + dwc_timer_callback_t cb;
  48749. + void *data;
  48750. +};
  48751. +
  48752. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  48753. +{
  48754. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  48755. +
  48756. + if (!t) {
  48757. + DWC_ERROR("Cannot allocate memory for timer");
  48758. + return NULL;
  48759. + }
  48760. +
  48761. + callout_init(&t->t, 1);
  48762. +
  48763. + t->name = DWC_STRDUP(name);
  48764. + if (!t->name) {
  48765. + DWC_ERROR("Cannot allocate memory for timer->name");
  48766. + goto no_name;
  48767. + }
  48768. +
  48769. + t->lock = DWC_SPINLOCK_ALLOC();
  48770. + if (!t->lock) {
  48771. + DWC_ERROR("Cannot allocate memory for lock");
  48772. + goto no_lock;
  48773. + }
  48774. +
  48775. + t->cb = cb;
  48776. + t->data = data;
  48777. +
  48778. + return t;
  48779. +
  48780. + no_lock:
  48781. + DWC_FREE(t->name);
  48782. + no_name:
  48783. + DWC_FREE(t);
  48784. +
  48785. + return NULL;
  48786. +}
  48787. +
  48788. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  48789. +{
  48790. + callout_stop(&timer->t);
  48791. + DWC_SPINLOCK_FREE(timer->lock);
  48792. + DWC_FREE(timer->name);
  48793. + DWC_FREE(timer);
  48794. +}
  48795. +
  48796. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  48797. +{
  48798. + struct timeval tv;
  48799. +
  48800. + tv.tv_sec = time / 1000;
  48801. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  48802. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  48803. +}
  48804. +
  48805. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  48806. +{
  48807. + callout_stop(&timer->t);
  48808. +}
  48809. +
  48810. +
  48811. +/* Wait Queues */
  48812. +
  48813. +struct dwc_waitq {
  48814. + struct mtx lock;
  48815. + int abort;
  48816. +};
  48817. +
  48818. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  48819. +{
  48820. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  48821. +
  48822. + if (!wq) {
  48823. + DWC_ERROR("Cannot allocate memory for waitqueue");
  48824. + return NULL;
  48825. + }
  48826. +
  48827. + mtx_init(&wq->lock, "dw3wtq", NULL, MTX_DEF);
  48828. + wq->abort = 0;
  48829. +
  48830. + return wq;
  48831. +}
  48832. +
  48833. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  48834. +{
  48835. + mtx_destroy(&wq->lock);
  48836. + DWC_FREE(wq);
  48837. +}
  48838. +
  48839. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  48840. +{
  48841. +// intrmask_t ipl;
  48842. + int result = 0;
  48843. +
  48844. + mtx_lock(&wq->lock);
  48845. +// ipl = splbio();
  48846. +
  48847. + /* Skip the sleep if already aborted or triggered */
  48848. + if (!wq->abort && !cond(data)) {
  48849. +// splx(ipl);
  48850. + result = msleep(wq, &wq->lock, PCATCH, "dw3wat", 0); // infinite timeout
  48851. +// ipl = splbio();
  48852. + }
  48853. +
  48854. + if (result == ERESTART) { // signaled - restart
  48855. + result = -DWC_E_RESTART;
  48856. +
  48857. + } else if (result == EINTR) { // signaled - interrupt
  48858. + result = -DWC_E_ABORT;
  48859. +
  48860. + } else if (wq->abort) {
  48861. + result = -DWC_E_ABORT;
  48862. +
  48863. + } else {
  48864. + result = 0;
  48865. + }
  48866. +
  48867. + wq->abort = 0;
  48868. +// splx(ipl);
  48869. + mtx_unlock(&wq->lock);
  48870. + return result;
  48871. +}
  48872. +
  48873. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  48874. + void *data, int32_t msecs)
  48875. +{
  48876. + struct timeval tv, tv1, tv2;
  48877. +// intrmask_t ipl;
  48878. + int result = 0;
  48879. +
  48880. + tv.tv_sec = msecs / 1000;
  48881. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  48882. +
  48883. + mtx_lock(&wq->lock);
  48884. +// ipl = splbio();
  48885. +
  48886. + /* Skip the sleep if already aborted or triggered */
  48887. + if (!wq->abort && !cond(data)) {
  48888. +// splx(ipl);
  48889. + getmicrouptime(&tv1);
  48890. + result = msleep(wq, &wq->lock, PCATCH, "dw3wto", tvtohz(&tv));
  48891. + getmicrouptime(&tv2);
  48892. +// ipl = splbio();
  48893. + }
  48894. +
  48895. + if (result == 0) { // awoken
  48896. + if (wq->abort) {
  48897. + result = -DWC_E_ABORT;
  48898. + } else {
  48899. + tv2.tv_usec -= tv1.tv_usec;
  48900. + if (tv2.tv_usec < 0) {
  48901. + tv2.tv_usec += 1000000;
  48902. + tv2.tv_sec--;
  48903. + }
  48904. +
  48905. + tv2.tv_sec -= tv1.tv_sec;
  48906. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  48907. + result = msecs - result;
  48908. + if (result <= 0)
  48909. + result = 1;
  48910. + }
  48911. + } else if (result == ERESTART) { // signaled - restart
  48912. + result = -DWC_E_RESTART;
  48913. +
  48914. + } else if (result == EINTR) { // signaled - interrupt
  48915. + result = -DWC_E_ABORT;
  48916. +
  48917. + } else { // timed out
  48918. + result = -DWC_E_TIMEOUT;
  48919. + }
  48920. +
  48921. + wq->abort = 0;
  48922. +// splx(ipl);
  48923. + mtx_unlock(&wq->lock);
  48924. + return result;
  48925. +}
  48926. +
  48927. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  48928. +{
  48929. + wakeup(wq);
  48930. +}
  48931. +
  48932. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  48933. +{
  48934. +// intrmask_t ipl;
  48935. +
  48936. + mtx_lock(&wq->lock);
  48937. +// ipl = splbio();
  48938. + wq->abort = 1;
  48939. + wakeup(wq);
  48940. +// splx(ipl);
  48941. + mtx_unlock(&wq->lock);
  48942. +}
  48943. +
  48944. +
  48945. +/* Threading */
  48946. +
  48947. +struct dwc_thread {
  48948. + struct proc *proc;
  48949. + int abort;
  48950. +};
  48951. +
  48952. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  48953. +{
  48954. + int retval;
  48955. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  48956. +
  48957. + if (!thread) {
  48958. + return NULL;
  48959. + }
  48960. +
  48961. + thread->abort = 0;
  48962. + retval = kthread_create((void (*)(void *))func, data, &thread->proc,
  48963. + RFPROC | RFNOWAIT, 0, "%s", name);
  48964. + if (retval) {
  48965. + DWC_FREE(thread);
  48966. + return NULL;
  48967. + }
  48968. +
  48969. + return thread;
  48970. +}
  48971. +
  48972. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  48973. +{
  48974. + int retval;
  48975. +
  48976. + thread->abort = 1;
  48977. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  48978. +
  48979. + if (retval == 0) {
  48980. + /* DWC_THREAD_EXIT() will free the thread struct */
  48981. + return 0;
  48982. + }
  48983. +
  48984. + /* NOTE: We leak the thread struct if thread doesn't die */
  48985. +
  48986. + if (retval == EWOULDBLOCK) {
  48987. + return -DWC_E_TIMEOUT;
  48988. + }
  48989. +
  48990. + return -DWC_E_UNKNOWN;
  48991. +}
  48992. +
  48993. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  48994. +{
  48995. + return thread->abort;
  48996. +}
  48997. +
  48998. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  48999. +{
  49000. + wakeup(&thread->abort);
  49001. + DWC_FREE(thread);
  49002. + kthread_exit(0);
  49003. +}
  49004. +
  49005. +
  49006. +/* tasklets
  49007. + - Runs in interrupt context (cannot sleep)
  49008. + - Each tasklet runs on a single CPU [ How can we ensure this on FreeBSD? Does it matter? ]
  49009. + - Different tasklets can be running simultaneously on different CPUs [ shouldn't matter ]
  49010. + */
  49011. +struct dwc_tasklet {
  49012. + struct task t;
  49013. + dwc_tasklet_callback_t cb;
  49014. + void *data;
  49015. +};
  49016. +
  49017. +static void tasklet_callback(void *data, int pending) // what to do with pending ???
  49018. +{
  49019. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  49020. +
  49021. + task->cb(task->data);
  49022. +}
  49023. +
  49024. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  49025. +{
  49026. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  49027. +
  49028. + if (task) {
  49029. + task->cb = cb;
  49030. + task->data = data;
  49031. + TASK_INIT(&task->t, 0, tasklet_callback, task);
  49032. + } else {
  49033. + DWC_ERROR("Cannot allocate memory for tasklet");
  49034. + }
  49035. +
  49036. + return task;
  49037. +}
  49038. +
  49039. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  49040. +{
  49041. + taskqueue_drain(taskqueue_fast, &task->t); // ???
  49042. + DWC_FREE(task);
  49043. +}
  49044. +
  49045. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  49046. +{
  49047. + /* Uses predefined system queue */
  49048. + taskqueue_enqueue_fast(taskqueue_fast, &task->t);
  49049. +}
  49050. +
  49051. +
  49052. +/* workqueues
  49053. + - Runs in process context (can sleep)
  49054. + */
  49055. +typedef struct work_container {
  49056. + dwc_work_callback_t cb;
  49057. + void *data;
  49058. + dwc_workq_t *wq;
  49059. + char *name;
  49060. + int hz;
  49061. +
  49062. +#ifdef DEBUG
  49063. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  49064. +#endif
  49065. + struct task task;
  49066. +} work_container_t;
  49067. +
  49068. +#ifdef DEBUG
  49069. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  49070. +#endif
  49071. +
  49072. +struct dwc_workq {
  49073. + struct taskqueue *taskq;
  49074. + dwc_spinlock_t *lock;
  49075. + dwc_waitq_t *waitq;
  49076. + int pending;
  49077. +
  49078. +#ifdef DEBUG
  49079. + struct work_container_queue entries;
  49080. +#endif
  49081. +};
  49082. +
  49083. +static void do_work(void *data, int pending) // what to do with pending ???
  49084. +{
  49085. + work_container_t *container = (work_container_t *)data;
  49086. + dwc_workq_t *wq = container->wq;
  49087. + dwc_irqflags_t flags;
  49088. +
  49089. + if (container->hz) {
  49090. + pause("dw3wrk", container->hz);
  49091. + }
  49092. +
  49093. + container->cb(container->data);
  49094. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  49095. +
  49096. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  49097. +
  49098. +#ifdef DEBUG
  49099. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  49100. +#endif
  49101. + if (container->name)
  49102. + DWC_FREE(container->name);
  49103. + DWC_FREE(container);
  49104. + wq->pending--;
  49105. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  49106. + DWC_WAITQ_TRIGGER(wq->waitq);
  49107. +}
  49108. +
  49109. +static int work_done(void *data)
  49110. +{
  49111. + dwc_workq_t *workq = (dwc_workq_t *)data;
  49112. +
  49113. + return workq->pending == 0;
  49114. +}
  49115. +
  49116. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  49117. +{
  49118. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  49119. +}
  49120. +
  49121. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  49122. +{
  49123. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  49124. +
  49125. + if (!wq) {
  49126. + DWC_ERROR("Cannot allocate memory for workqueue");
  49127. + return NULL;
  49128. + }
  49129. +
  49130. + wq->taskq = taskqueue_create(name, M_NOWAIT, taskqueue_thread_enqueue, &wq->taskq);
  49131. + if (!wq->taskq) {
  49132. + DWC_ERROR("Cannot allocate memory for taskqueue");
  49133. + goto no_taskq;
  49134. + }
  49135. +
  49136. + wq->pending = 0;
  49137. +
  49138. + wq->lock = DWC_SPINLOCK_ALLOC();
  49139. + if (!wq->lock) {
  49140. + DWC_ERROR("Cannot allocate memory for spinlock");
  49141. + goto no_lock;
  49142. + }
  49143. +
  49144. + wq->waitq = DWC_WAITQ_ALLOC();
  49145. + if (!wq->waitq) {
  49146. + DWC_ERROR("Cannot allocate memory for waitqueue");
  49147. + goto no_waitq;
  49148. + }
  49149. +
  49150. + taskqueue_start_threads(&wq->taskq, 1, PWAIT, "%s taskq", "dw3tsk");
  49151. +
  49152. +#ifdef DEBUG
  49153. + DWC_CIRCLEQ_INIT(&wq->entries);
  49154. +#endif
  49155. + return wq;
  49156. +
  49157. + no_waitq:
  49158. + DWC_SPINLOCK_FREE(wq->lock);
  49159. + no_lock:
  49160. + taskqueue_free(wq->taskq);
  49161. + no_taskq:
  49162. + DWC_FREE(wq);
  49163. +
  49164. + return NULL;
  49165. +}
  49166. +
  49167. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  49168. +{
  49169. +#ifdef DEBUG
  49170. + dwc_irqflags_t flags;
  49171. +
  49172. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  49173. +
  49174. + if (wq->pending != 0) {
  49175. + struct work_container *container;
  49176. +
  49177. + DWC_ERROR("Destroying work queue with pending work");
  49178. +
  49179. + DWC_CIRCLEQ_FOREACH(container, &wq->entries, entry) {
  49180. + DWC_ERROR("Work %s still pending", container->name);
  49181. + }
  49182. + }
  49183. +
  49184. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  49185. +#endif
  49186. + DWC_WAITQ_FREE(wq->waitq);
  49187. + DWC_SPINLOCK_FREE(wq->lock);
  49188. + taskqueue_free(wq->taskq);
  49189. + DWC_FREE(wq);
  49190. +}
  49191. +
  49192. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  49193. + char *format, ...)
  49194. +{
  49195. + dwc_irqflags_t flags;
  49196. + work_container_t *container;
  49197. + static char name[128];
  49198. + va_list args;
  49199. +
  49200. + va_start(args, format);
  49201. + DWC_VSNPRINTF(name, 128, format, args);
  49202. + va_end(args);
  49203. +
  49204. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  49205. + wq->pending++;
  49206. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  49207. + DWC_WAITQ_TRIGGER(wq->waitq);
  49208. +
  49209. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  49210. + if (!container) {
  49211. + DWC_ERROR("Cannot allocate memory for container");
  49212. + return;
  49213. + }
  49214. +
  49215. + container->name = DWC_STRDUP(name);
  49216. + if (!container->name) {
  49217. + DWC_ERROR("Cannot allocate memory for container->name");
  49218. + DWC_FREE(container);
  49219. + return;
  49220. + }
  49221. +
  49222. + container->cb = cb;
  49223. + container->data = data;
  49224. + container->wq = wq;
  49225. + container->hz = 0;
  49226. +
  49227. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  49228. +
  49229. + TASK_INIT(&container->task, 0, do_work, container);
  49230. +
  49231. +#ifdef DEBUG
  49232. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  49233. +#endif
  49234. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  49235. +}
  49236. +
  49237. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  49238. + void *data, uint32_t time, char *format, ...)
  49239. +{
  49240. + dwc_irqflags_t flags;
  49241. + work_container_t *container;
  49242. + static char name[128];
  49243. + struct timeval tv;
  49244. + va_list args;
  49245. +
  49246. + va_start(args, format);
  49247. + DWC_VSNPRINTF(name, 128, format, args);
  49248. + va_end(args);
  49249. +
  49250. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  49251. + wq->pending++;
  49252. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  49253. + DWC_WAITQ_TRIGGER(wq->waitq);
  49254. +
  49255. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  49256. + if (!container) {
  49257. + DWC_ERROR("Cannot allocate memory for container");
  49258. + return;
  49259. + }
  49260. +
  49261. + container->name = DWC_STRDUP(name);
  49262. + if (!container->name) {
  49263. + DWC_ERROR("Cannot allocate memory for container->name");
  49264. + DWC_FREE(container);
  49265. + return;
  49266. + }
  49267. +
  49268. + container->cb = cb;
  49269. + container->data = data;
  49270. + container->wq = wq;
  49271. +
  49272. + tv.tv_sec = time / 1000;
  49273. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  49274. + container->hz = tvtohz(&tv);
  49275. +
  49276. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  49277. +
  49278. + TASK_INIT(&container->task, 0, do_work, container);
  49279. +
  49280. +#ifdef DEBUG
  49281. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  49282. +#endif
  49283. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  49284. +}
  49285. +
  49286. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  49287. +{
  49288. + return wq->pending;
  49289. +}
  49290. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_common_linux.c linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_common_linux.c
  49291. --- linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_common_linux.c 1969-12-31 18:00:00.000000000 -0600
  49292. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_common_linux.c 2014-12-03 19:13:40.212418001 -0600
  49293. @@ -0,0 +1,1432 @@
  49294. +#include <linux/kernel.h>
  49295. +#include <linux/init.h>
  49296. +#include <linux/module.h>
  49297. +#include <linux/kthread.h>
  49298. +
  49299. +#ifdef DWC_CCLIB
  49300. +# include "dwc_cc.h"
  49301. +#endif
  49302. +
  49303. +#ifdef DWC_CRYPTOLIB
  49304. +# include "dwc_modpow.h"
  49305. +# include "dwc_dh.h"
  49306. +# include "dwc_crypto.h"
  49307. +#endif
  49308. +
  49309. +#ifdef DWC_NOTIFYLIB
  49310. +# include "dwc_notifier.h"
  49311. +#endif
  49312. +
  49313. +/* OS-Level Implementations */
  49314. +
  49315. +/* This is the Linux kernel implementation of the DWC platform library. */
  49316. +#include <linux/moduleparam.h>
  49317. +#include <linux/ctype.h>
  49318. +#include <linux/crypto.h>
  49319. +#include <linux/delay.h>
  49320. +#include <linux/device.h>
  49321. +#include <linux/dma-mapping.h>
  49322. +#include <linux/cdev.h>
  49323. +#include <linux/errno.h>
  49324. +#include <linux/interrupt.h>
  49325. +#include <linux/jiffies.h>
  49326. +#include <linux/list.h>
  49327. +#include <linux/pci.h>
  49328. +#include <linux/random.h>
  49329. +#include <linux/scatterlist.h>
  49330. +#include <linux/slab.h>
  49331. +#include <linux/stat.h>
  49332. +#include <linux/string.h>
  49333. +#include <linux/timer.h>
  49334. +#include <linux/usb.h>
  49335. +
  49336. +#include <linux/version.h>
  49337. +
  49338. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  49339. +# include <linux/usb/gadget.h>
  49340. +#else
  49341. +# include <linux/usb_gadget.h>
  49342. +#endif
  49343. +
  49344. +#include <asm/io.h>
  49345. +#include <asm/page.h>
  49346. +#include <asm/uaccess.h>
  49347. +#include <asm/unaligned.h>
  49348. +
  49349. +#include "dwc_os.h"
  49350. +#include "dwc_list.h"
  49351. +
  49352. +
  49353. +/* MISC */
  49354. +
  49355. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  49356. +{
  49357. + return memset(dest, byte, size);
  49358. +}
  49359. +
  49360. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  49361. +{
  49362. + return memcpy(dest, src, size);
  49363. +}
  49364. +
  49365. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  49366. +{
  49367. + return memmove(dest, src, size);
  49368. +}
  49369. +
  49370. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  49371. +{
  49372. + return memcmp(m1, m2, size);
  49373. +}
  49374. +
  49375. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  49376. +{
  49377. + return strncmp(s1, s2, size);
  49378. +}
  49379. +
  49380. +int DWC_STRCMP(void *s1, void *s2)
  49381. +{
  49382. + return strcmp(s1, s2);
  49383. +}
  49384. +
  49385. +int DWC_STRLEN(char const *str)
  49386. +{
  49387. + return strlen(str);
  49388. +}
  49389. +
  49390. +char *DWC_STRCPY(char *to, char const *from)
  49391. +{
  49392. + return strcpy(to, from);
  49393. +}
  49394. +
  49395. +char *DWC_STRDUP(char const *str)
  49396. +{
  49397. + int len = DWC_STRLEN(str) + 1;
  49398. + char *new = DWC_ALLOC_ATOMIC(len);
  49399. +
  49400. + if (!new) {
  49401. + return NULL;
  49402. + }
  49403. +
  49404. + DWC_MEMCPY(new, str, len);
  49405. + return new;
  49406. +}
  49407. +
  49408. +int DWC_ATOI(const char *str, int32_t *value)
  49409. +{
  49410. + char *end = NULL;
  49411. +
  49412. + *value = simple_strtol(str, &end, 0);
  49413. + if (*end == '\0') {
  49414. + return 0;
  49415. + }
  49416. +
  49417. + return -1;
  49418. +}
  49419. +
  49420. +int DWC_ATOUI(const char *str, uint32_t *value)
  49421. +{
  49422. + char *end = NULL;
  49423. +
  49424. + *value = simple_strtoul(str, &end, 0);
  49425. + if (*end == '\0') {
  49426. + return 0;
  49427. + }
  49428. +
  49429. + return -1;
  49430. +}
  49431. +
  49432. +
  49433. +#ifdef DWC_UTFLIB
  49434. +/* From usbstring.c */
  49435. +
  49436. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  49437. +{
  49438. + int count = 0;
  49439. + u8 c;
  49440. + u16 uchar;
  49441. +
  49442. + /* this insists on correct encodings, though not minimal ones.
  49443. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  49444. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  49445. + */
  49446. + while (len != 0 && (c = (u8) *s++) != 0) {
  49447. + if (unlikely(c & 0x80)) {
  49448. + // 2-byte sequence:
  49449. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  49450. + if ((c & 0xe0) == 0xc0) {
  49451. + uchar = (c & 0x1f) << 6;
  49452. +
  49453. + c = (u8) *s++;
  49454. + if ((c & 0xc0) != 0xc0)
  49455. + goto fail;
  49456. + c &= 0x3f;
  49457. + uchar |= c;
  49458. +
  49459. + // 3-byte sequence (most CJKV characters):
  49460. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  49461. + } else if ((c & 0xf0) == 0xe0) {
  49462. + uchar = (c & 0x0f) << 12;
  49463. +
  49464. + c = (u8) *s++;
  49465. + if ((c & 0xc0) != 0xc0)
  49466. + goto fail;
  49467. + c &= 0x3f;
  49468. + uchar |= c << 6;
  49469. +
  49470. + c = (u8) *s++;
  49471. + if ((c & 0xc0) != 0xc0)
  49472. + goto fail;
  49473. + c &= 0x3f;
  49474. + uchar |= c;
  49475. +
  49476. + /* no bogus surrogates */
  49477. + if (0xd800 <= uchar && uchar <= 0xdfff)
  49478. + goto fail;
  49479. +
  49480. + // 4-byte sequence (surrogate pairs, currently rare):
  49481. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  49482. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  49483. + // (uuuuu = wwww + 1)
  49484. + // FIXME accept the surrogate code points (only)
  49485. + } else
  49486. + goto fail;
  49487. + } else
  49488. + uchar = c;
  49489. + put_unaligned (cpu_to_le16 (uchar), cp++);
  49490. + count++;
  49491. + len--;
  49492. + }
  49493. + return count;
  49494. +fail:
  49495. + return -1;
  49496. +}
  49497. +#endif /* DWC_UTFLIB */
  49498. +
  49499. +
  49500. +/* dwc_debug.h */
  49501. +
  49502. +dwc_bool_t DWC_IN_IRQ(void)
  49503. +{
  49504. + return in_irq();
  49505. +}
  49506. +
  49507. +dwc_bool_t DWC_IN_BH(void)
  49508. +{
  49509. + return in_softirq();
  49510. +}
  49511. +
  49512. +void DWC_VPRINTF(char *format, va_list args)
  49513. +{
  49514. + vprintk(format, args);
  49515. +}
  49516. +
  49517. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  49518. +{
  49519. + return vsnprintf(str, size, format, args);
  49520. +}
  49521. +
  49522. +void DWC_PRINTF(char *format, ...)
  49523. +{
  49524. + va_list args;
  49525. +
  49526. + va_start(args, format);
  49527. + DWC_VPRINTF(format, args);
  49528. + va_end(args);
  49529. +}
  49530. +
  49531. +int DWC_SPRINTF(char *buffer, char *format, ...)
  49532. +{
  49533. + int retval;
  49534. + va_list args;
  49535. +
  49536. + va_start(args, format);
  49537. + retval = vsprintf(buffer, format, args);
  49538. + va_end(args);
  49539. + return retval;
  49540. +}
  49541. +
  49542. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  49543. +{
  49544. + int retval;
  49545. + va_list args;
  49546. +
  49547. + va_start(args, format);
  49548. + retval = vsnprintf(buffer, size, format, args);
  49549. + va_end(args);
  49550. + return retval;
  49551. +}
  49552. +
  49553. +void __DWC_WARN(char *format, ...)
  49554. +{
  49555. + va_list args;
  49556. +
  49557. + va_start(args, format);
  49558. + DWC_PRINTF(KERN_WARNING);
  49559. + DWC_VPRINTF(format, args);
  49560. + va_end(args);
  49561. +}
  49562. +
  49563. +void __DWC_ERROR(char *format, ...)
  49564. +{
  49565. + va_list args;
  49566. +
  49567. + va_start(args, format);
  49568. + DWC_PRINTF(KERN_ERR);
  49569. + DWC_VPRINTF(format, args);
  49570. + va_end(args);
  49571. +}
  49572. +
  49573. +void DWC_EXCEPTION(char *format, ...)
  49574. +{
  49575. + va_list args;
  49576. +
  49577. + va_start(args, format);
  49578. + DWC_PRINTF(KERN_ERR);
  49579. + DWC_VPRINTF(format, args);
  49580. + va_end(args);
  49581. + BUG_ON(1);
  49582. +}
  49583. +
  49584. +#ifdef DEBUG
  49585. +void __DWC_DEBUG(char *format, ...)
  49586. +{
  49587. + va_list args;
  49588. +
  49589. + va_start(args, format);
  49590. + DWC_PRINTF(KERN_DEBUG);
  49591. + DWC_VPRINTF(format, args);
  49592. + va_end(args);
  49593. +}
  49594. +#endif
  49595. +
  49596. +
  49597. +/* dwc_mem.h */
  49598. +
  49599. +#if 0
  49600. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  49601. + uint32_t align,
  49602. + uint32_t alloc)
  49603. +{
  49604. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  49605. + size, align, alloc);
  49606. + return (dwc_pool_t *)pool;
  49607. +}
  49608. +
  49609. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  49610. +{
  49611. + dma_pool_destroy((struct dma_pool *)pool);
  49612. +}
  49613. +
  49614. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  49615. +{
  49616. + return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  49617. +}
  49618. +
  49619. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  49620. +{
  49621. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  49622. + memset(..);
  49623. +}
  49624. +
  49625. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  49626. +{
  49627. + dma_pool_free(pool, vaddr, daddr);
  49628. +}
  49629. +#endif
  49630. +
  49631. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  49632. +{
  49633. +#ifdef xxCOSIM /* Only works for 32-bit cosim */
  49634. + void *buf = dma_alloc_coherent(dma_ctx, (size_t)size, dma_addr, GFP_KERNEL);
  49635. +#else
  49636. + void *buf = dma_alloc_coherent(dma_ctx, (size_t)size, dma_addr, GFP_KERNEL | GFP_DMA32);
  49637. +#endif
  49638. + if (!buf) {
  49639. + return NULL;
  49640. + }
  49641. +
  49642. + memset(buf, 0, (size_t)size);
  49643. + return buf;
  49644. +}
  49645. +
  49646. +void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  49647. +{
  49648. + void *buf = dma_alloc_coherent(NULL, (size_t)size, dma_addr, GFP_ATOMIC);
  49649. + if (!buf) {
  49650. + return NULL;
  49651. + }
  49652. + memset(buf, 0, (size_t)size);
  49653. + return buf;
  49654. +}
  49655. +
  49656. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  49657. +{
  49658. + dma_free_coherent(dma_ctx, size, virt_addr, dma_addr);
  49659. +}
  49660. +
  49661. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  49662. +{
  49663. + return kzalloc(size, GFP_KERNEL);
  49664. +}
  49665. +
  49666. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  49667. +{
  49668. + return kzalloc(size, GFP_ATOMIC);
  49669. +}
  49670. +
  49671. +void __DWC_FREE(void *mem_ctx, void *addr)
  49672. +{
  49673. + kfree(addr);
  49674. +}
  49675. +
  49676. +
  49677. +#ifdef DWC_CRYPTOLIB
  49678. +/* dwc_crypto.h */
  49679. +
  49680. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  49681. +{
  49682. + get_random_bytes(buffer, length);
  49683. +}
  49684. +
  49685. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  49686. +{
  49687. + struct crypto_blkcipher *tfm;
  49688. + struct blkcipher_desc desc;
  49689. + struct scatterlist sgd;
  49690. + struct scatterlist sgs;
  49691. +
  49692. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  49693. + if (tfm == NULL) {
  49694. + printk("failed to load transform for aes CBC\n");
  49695. + return -1;
  49696. + }
  49697. +
  49698. + crypto_blkcipher_setkey(tfm, key, keylen);
  49699. + crypto_blkcipher_set_iv(tfm, iv, 16);
  49700. +
  49701. + sg_init_one(&sgd, out, messagelen);
  49702. + sg_init_one(&sgs, message, messagelen);
  49703. +
  49704. + desc.tfm = tfm;
  49705. + desc.flags = 0;
  49706. +
  49707. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  49708. + crypto_free_blkcipher(tfm);
  49709. + DWC_ERROR("AES CBC encryption failed");
  49710. + return -1;
  49711. + }
  49712. +
  49713. + crypto_free_blkcipher(tfm);
  49714. + return 0;
  49715. +}
  49716. +
  49717. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  49718. +{
  49719. + struct crypto_hash *tfm;
  49720. + struct hash_desc desc;
  49721. + struct scatterlist sg;
  49722. +
  49723. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  49724. + if (IS_ERR(tfm)) {
  49725. + DWC_ERROR("Failed to load transform for sha256: %ld\n", PTR_ERR(tfm));
  49726. + return 0;
  49727. + }
  49728. + desc.tfm = tfm;
  49729. + desc.flags = 0;
  49730. +
  49731. + sg_init_one(&sg, message, len);
  49732. + crypto_hash_digest(&desc, &sg, len, out);
  49733. + crypto_free_hash(tfm);
  49734. +
  49735. + return 1;
  49736. +}
  49737. +
  49738. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  49739. + uint8_t *key, uint32_t keylen, uint8_t *out)
  49740. +{
  49741. + struct crypto_hash *tfm;
  49742. + struct hash_desc desc;
  49743. + struct scatterlist sg;
  49744. +
  49745. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  49746. + if (IS_ERR(tfm)) {
  49747. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld\n", PTR_ERR(tfm));
  49748. + return 0;
  49749. + }
  49750. + desc.tfm = tfm;
  49751. + desc.flags = 0;
  49752. +
  49753. + sg_init_one(&sg, message, messagelen);
  49754. + crypto_hash_setkey(tfm, key, keylen);
  49755. + crypto_hash_digest(&desc, &sg, messagelen, out);
  49756. + crypto_free_hash(tfm);
  49757. +
  49758. + return 1;
  49759. +}
  49760. +#endif /* DWC_CRYPTOLIB */
  49761. +
  49762. +
  49763. +/* Byte Ordering Conversions */
  49764. +
  49765. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  49766. +{
  49767. +#ifdef __LITTLE_ENDIAN
  49768. + return *p;
  49769. +#else
  49770. + uint8_t *u_p = (uint8_t *)p;
  49771. +
  49772. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  49773. +#endif
  49774. +}
  49775. +
  49776. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  49777. +{
  49778. +#ifdef __BIG_ENDIAN
  49779. + return *p;
  49780. +#else
  49781. + uint8_t *u_p = (uint8_t *)p;
  49782. +
  49783. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  49784. +#endif
  49785. +}
  49786. +
  49787. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  49788. +{
  49789. +#ifdef __LITTLE_ENDIAN
  49790. + return *p;
  49791. +#else
  49792. + uint8_t *u_p = (uint8_t *)p;
  49793. +
  49794. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  49795. +#endif
  49796. +}
  49797. +
  49798. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  49799. +{
  49800. +#ifdef __BIG_ENDIAN
  49801. + return *p;
  49802. +#else
  49803. + uint8_t *u_p = (uint8_t *)p;
  49804. +
  49805. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  49806. +#endif
  49807. +}
  49808. +
  49809. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  49810. +{
  49811. +#ifdef __LITTLE_ENDIAN
  49812. + return *p;
  49813. +#else
  49814. + uint8_t *u_p = (uint8_t *)p;
  49815. + return (u_p[1] | (u_p[0] << 8));
  49816. +#endif
  49817. +}
  49818. +
  49819. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  49820. +{
  49821. +#ifdef __BIG_ENDIAN
  49822. + return *p;
  49823. +#else
  49824. + uint8_t *u_p = (uint8_t *)p;
  49825. + return (u_p[1] | (u_p[0] << 8));
  49826. +#endif
  49827. +}
  49828. +
  49829. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  49830. +{
  49831. +#ifdef __LITTLE_ENDIAN
  49832. + return *p;
  49833. +#else
  49834. + uint8_t *u_p = (uint8_t *)p;
  49835. + return (u_p[1] | (u_p[0] << 8));
  49836. +#endif
  49837. +}
  49838. +
  49839. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  49840. +{
  49841. +#ifdef __BIG_ENDIAN
  49842. + return *p;
  49843. +#else
  49844. + uint8_t *u_p = (uint8_t *)p;
  49845. + return (u_p[1] | (u_p[0] << 8));
  49846. +#endif
  49847. +}
  49848. +
  49849. +
  49850. +/* Registers */
  49851. +
  49852. +uint32_t DWC_READ_REG32(uint32_t volatile *reg)
  49853. +{
  49854. + return readl(reg);
  49855. +}
  49856. +
  49857. +#if 0
  49858. +uint64_t DWC_READ_REG64(uint64_t volatile *reg)
  49859. +{
  49860. +}
  49861. +#endif
  49862. +
  49863. +void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
  49864. +{
  49865. + writel(value, reg);
  49866. +}
  49867. +
  49868. +#if 0
  49869. +void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value)
  49870. +{
  49871. +}
  49872. +#endif
  49873. +
  49874. +void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
  49875. +{
  49876. + unsigned long flags;
  49877. +
  49878. + local_irq_save(flags);
  49879. + local_fiq_disable();
  49880. + writel((readl(reg) & ~clear_mask) | set_mask, reg);
  49881. + local_fiq_enable();
  49882. + local_irq_restore(flags);
  49883. +}
  49884. +
  49885. +#if 0
  49886. +void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask)
  49887. +{
  49888. +}
  49889. +#endif
  49890. +
  49891. +
  49892. +/* Locking */
  49893. +
  49894. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  49895. +{
  49896. + spinlock_t *sl = (spinlock_t *)1;
  49897. +
  49898. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  49899. + sl = DWC_ALLOC(sizeof(*sl));
  49900. + if (!sl) {
  49901. + DWC_ERROR("Cannot allocate memory for spinlock\n");
  49902. + return NULL;
  49903. + }
  49904. +
  49905. + spin_lock_init(sl);
  49906. +#endif
  49907. + return (dwc_spinlock_t *)sl;
  49908. +}
  49909. +
  49910. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  49911. +{
  49912. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  49913. + DWC_FREE(lock);
  49914. +#endif
  49915. +}
  49916. +
  49917. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  49918. +{
  49919. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  49920. + spin_lock((spinlock_t *)lock);
  49921. +#endif
  49922. +}
  49923. +
  49924. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  49925. +{
  49926. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  49927. + spin_unlock((spinlock_t *)lock);
  49928. +#endif
  49929. +}
  49930. +
  49931. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  49932. +{
  49933. + dwc_irqflags_t f;
  49934. +
  49935. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  49936. + spin_lock_irqsave((spinlock_t *)lock, f);
  49937. +#else
  49938. + local_irq_save(f);
  49939. +#endif
  49940. + *flags = f;
  49941. +}
  49942. +
  49943. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  49944. +{
  49945. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  49946. + spin_unlock_irqrestore((spinlock_t *)lock, flags);
  49947. +#else
  49948. + local_irq_restore(flags);
  49949. +#endif
  49950. +}
  49951. +
  49952. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  49953. +{
  49954. + struct mutex *m;
  49955. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
  49956. +
  49957. + if (!mutex) {
  49958. + DWC_ERROR("Cannot allocate memory for mutex\n");
  49959. + return NULL;
  49960. + }
  49961. +
  49962. + m = (struct mutex *)mutex;
  49963. + mutex_init(m);
  49964. + return mutex;
  49965. +}
  49966. +
  49967. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  49968. +#else
  49969. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  49970. +{
  49971. + mutex_destroy((struct mutex *)mutex);
  49972. + DWC_FREE(mutex);
  49973. +}
  49974. +#endif
  49975. +
  49976. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  49977. +{
  49978. + struct mutex *m = (struct mutex *)mutex;
  49979. + mutex_lock(m);
  49980. +}
  49981. +
  49982. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  49983. +{
  49984. + struct mutex *m = (struct mutex *)mutex;
  49985. + return mutex_trylock(m);
  49986. +}
  49987. +
  49988. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  49989. +{
  49990. + struct mutex *m = (struct mutex *)mutex;
  49991. + mutex_unlock(m);
  49992. +}
  49993. +
  49994. +
  49995. +/* Timing */
  49996. +
  49997. +void DWC_UDELAY(uint32_t usecs)
  49998. +{
  49999. + udelay(usecs);
  50000. +}
  50001. +
  50002. +void DWC_MDELAY(uint32_t msecs)
  50003. +{
  50004. + mdelay(msecs);
  50005. +}
  50006. +
  50007. +void DWC_MSLEEP(uint32_t msecs)
  50008. +{
  50009. + msleep(msecs);
  50010. +}
  50011. +
  50012. +uint32_t DWC_TIME(void)
  50013. +{
  50014. + return jiffies_to_msecs(jiffies);
  50015. +}
  50016. +
  50017. +
  50018. +/* Timers */
  50019. +
  50020. +struct dwc_timer {
  50021. + struct timer_list *t;
  50022. + char *name;
  50023. + dwc_timer_callback_t cb;
  50024. + void *data;
  50025. + uint8_t scheduled;
  50026. + dwc_spinlock_t *lock;
  50027. +};
  50028. +
  50029. +static void timer_callback(unsigned long data)
  50030. +{
  50031. + dwc_timer_t *timer = (dwc_timer_t *)data;
  50032. + dwc_irqflags_t flags;
  50033. +
  50034. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  50035. + timer->scheduled = 0;
  50036. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  50037. + DWC_DEBUGC("Timer %s callback", timer->name);
  50038. + timer->cb(timer->data);
  50039. +}
  50040. +
  50041. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  50042. +{
  50043. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  50044. +
  50045. + if (!t) {
  50046. + DWC_ERROR("Cannot allocate memory for timer");
  50047. + return NULL;
  50048. + }
  50049. +
  50050. + t->t = DWC_ALLOC(sizeof(*t->t));
  50051. + if (!t->t) {
  50052. + DWC_ERROR("Cannot allocate memory for timer->t");
  50053. + goto no_timer;
  50054. + }
  50055. +
  50056. + t->name = DWC_STRDUP(name);
  50057. + if (!t->name) {
  50058. + DWC_ERROR("Cannot allocate memory for timer->name");
  50059. + goto no_name;
  50060. + }
  50061. +
  50062. + t->lock = DWC_SPINLOCK_ALLOC();
  50063. + if (!t->lock) {
  50064. + DWC_ERROR("Cannot allocate memory for lock");
  50065. + goto no_lock;
  50066. + }
  50067. +
  50068. + t->scheduled = 0;
  50069. + t->t->base = &boot_tvec_bases;
  50070. + t->t->expires = jiffies;
  50071. + setup_timer(t->t, timer_callback, (unsigned long)t);
  50072. +
  50073. + t->cb = cb;
  50074. + t->data = data;
  50075. +
  50076. + return t;
  50077. +
  50078. + no_lock:
  50079. + DWC_FREE(t->name);
  50080. + no_name:
  50081. + DWC_FREE(t->t);
  50082. + no_timer:
  50083. + DWC_FREE(t);
  50084. + return NULL;
  50085. +}
  50086. +
  50087. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  50088. +{
  50089. + dwc_irqflags_t flags;
  50090. +
  50091. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  50092. +
  50093. + if (timer->scheduled) {
  50094. + del_timer(timer->t);
  50095. + timer->scheduled = 0;
  50096. + }
  50097. +
  50098. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  50099. + DWC_SPINLOCK_FREE(timer->lock);
  50100. + DWC_FREE(timer->t);
  50101. + DWC_FREE(timer->name);
  50102. + DWC_FREE(timer);
  50103. +}
  50104. +
  50105. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  50106. +{
  50107. + dwc_irqflags_t flags;
  50108. +
  50109. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  50110. +
  50111. + if (!timer->scheduled) {
  50112. + timer->scheduled = 1;
  50113. + DWC_DEBUGC("Scheduling timer %s to expire in +%d msec", timer->name, time);
  50114. + timer->t->expires = jiffies + msecs_to_jiffies(time);
  50115. + add_timer(timer->t);
  50116. + } else {
  50117. + DWC_DEBUGC("Modifying timer %s to expire in +%d msec", timer->name, time);
  50118. + mod_timer(timer->t, jiffies + msecs_to_jiffies(time));
  50119. + }
  50120. +
  50121. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  50122. +}
  50123. +
  50124. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  50125. +{
  50126. + del_timer(timer->t);
  50127. +}
  50128. +
  50129. +
  50130. +/* Wait Queues */
  50131. +
  50132. +struct dwc_waitq {
  50133. + wait_queue_head_t queue;
  50134. + int abort;
  50135. +};
  50136. +
  50137. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  50138. +{
  50139. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  50140. +
  50141. + if (!wq) {
  50142. + DWC_ERROR("Cannot allocate memory for waitqueue\n");
  50143. + return NULL;
  50144. + }
  50145. +
  50146. + init_waitqueue_head(&wq->queue);
  50147. + wq->abort = 0;
  50148. + return wq;
  50149. +}
  50150. +
  50151. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  50152. +{
  50153. + DWC_FREE(wq);
  50154. +}
  50155. +
  50156. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  50157. +{
  50158. + int result = wait_event_interruptible(wq->queue,
  50159. + cond(data) || wq->abort);
  50160. + if (result == -ERESTARTSYS) {
  50161. + wq->abort = 0;
  50162. + return -DWC_E_RESTART;
  50163. + }
  50164. +
  50165. + if (wq->abort == 1) {
  50166. + wq->abort = 0;
  50167. + return -DWC_E_ABORT;
  50168. + }
  50169. +
  50170. + wq->abort = 0;
  50171. +
  50172. + if (result == 0) {
  50173. + return 0;
  50174. + }
  50175. +
  50176. + return -DWC_E_UNKNOWN;
  50177. +}
  50178. +
  50179. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  50180. + void *data, int32_t msecs)
  50181. +{
  50182. + int32_t tmsecs;
  50183. + int result = wait_event_interruptible_timeout(wq->queue,
  50184. + cond(data) || wq->abort,
  50185. + msecs_to_jiffies(msecs));
  50186. + if (result == -ERESTARTSYS) {
  50187. + wq->abort = 0;
  50188. + return -DWC_E_RESTART;
  50189. + }
  50190. +
  50191. + if (wq->abort == 1) {
  50192. + wq->abort = 0;
  50193. + return -DWC_E_ABORT;
  50194. + }
  50195. +
  50196. + wq->abort = 0;
  50197. +
  50198. + if (result > 0) {
  50199. + tmsecs = jiffies_to_msecs(result);
  50200. + if (!tmsecs) {
  50201. + return 1;
  50202. + }
  50203. +
  50204. + return tmsecs;
  50205. + }
  50206. +
  50207. + if (result == 0) {
  50208. + return -DWC_E_TIMEOUT;
  50209. + }
  50210. +
  50211. + return -DWC_E_UNKNOWN;
  50212. +}
  50213. +
  50214. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  50215. +{
  50216. + wq->abort = 0;
  50217. + wake_up_interruptible(&wq->queue);
  50218. +}
  50219. +
  50220. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  50221. +{
  50222. + wq->abort = 1;
  50223. + wake_up_interruptible(&wq->queue);
  50224. +}
  50225. +
  50226. +
  50227. +/* Threading */
  50228. +
  50229. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  50230. +{
  50231. + struct task_struct *thread = kthread_run(func, data, name);
  50232. +
  50233. + if (thread == ERR_PTR(-ENOMEM)) {
  50234. + return NULL;
  50235. + }
  50236. +
  50237. + return (dwc_thread_t *)thread;
  50238. +}
  50239. +
  50240. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  50241. +{
  50242. + return kthread_stop((struct task_struct *)thread);
  50243. +}
  50244. +
  50245. +dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
  50246. +{
  50247. + return kthread_should_stop();
  50248. +}
  50249. +
  50250. +
  50251. +/* tasklets
  50252. + - run in interrupt context (cannot sleep)
  50253. + - each tasklet runs on a single CPU
  50254. + - different tasklets can be running simultaneously on different CPUs
  50255. + */
  50256. +struct dwc_tasklet {
  50257. + struct tasklet_struct t;
  50258. + dwc_tasklet_callback_t cb;
  50259. + void *data;
  50260. +};
  50261. +
  50262. +static void tasklet_callback(unsigned long data)
  50263. +{
  50264. + dwc_tasklet_t *t = (dwc_tasklet_t *)data;
  50265. + t->cb(t->data);
  50266. +}
  50267. +
  50268. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  50269. +{
  50270. + dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
  50271. +
  50272. + if (t) {
  50273. + t->cb = cb;
  50274. + t->data = data;
  50275. + tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
  50276. + } else {
  50277. + DWC_ERROR("Cannot allocate memory for tasklet\n");
  50278. + }
  50279. +
  50280. + return t;
  50281. +}
  50282. +
  50283. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  50284. +{
  50285. + DWC_FREE(task);
  50286. +}
  50287. +
  50288. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  50289. +{
  50290. + tasklet_schedule(&task->t);
  50291. +}
  50292. +
  50293. +void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
  50294. +{
  50295. + tasklet_hi_schedule(&task->t);
  50296. +}
  50297. +
  50298. +
  50299. +/* workqueues
  50300. + - run in process context (can sleep)
  50301. + */
  50302. +typedef struct work_container {
  50303. + dwc_work_callback_t cb;
  50304. + void *data;
  50305. + dwc_workq_t *wq;
  50306. + char *name;
  50307. +
  50308. +#ifdef DEBUG
  50309. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  50310. +#endif
  50311. + struct delayed_work work;
  50312. +} work_container_t;
  50313. +
  50314. +#ifdef DEBUG
  50315. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  50316. +#endif
  50317. +
  50318. +struct dwc_workq {
  50319. + struct workqueue_struct *wq;
  50320. + dwc_spinlock_t *lock;
  50321. + dwc_waitq_t *waitq;
  50322. + int pending;
  50323. +
  50324. +#ifdef DEBUG
  50325. + struct work_container_queue entries;
  50326. +#endif
  50327. +};
  50328. +
  50329. +static void do_work(struct work_struct *work)
  50330. +{
  50331. + dwc_irqflags_t flags;
  50332. + struct delayed_work *dw = container_of(work, struct delayed_work, work);
  50333. + work_container_t *container = container_of(dw, struct work_container, work);
  50334. + dwc_workq_t *wq = container->wq;
  50335. +
  50336. + container->cb(container->data);
  50337. +
  50338. +#ifdef DEBUG
  50339. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  50340. +#endif
  50341. + DWC_DEBUGC("Work done: %s, container=%p", container->name, container);
  50342. + if (container->name) {
  50343. + DWC_FREE(container->name);
  50344. + }
  50345. + DWC_FREE(container);
  50346. +
  50347. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  50348. + wq->pending--;
  50349. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  50350. + DWC_WAITQ_TRIGGER(wq->waitq);
  50351. +}
  50352. +
  50353. +static int work_done(void *data)
  50354. +{
  50355. + dwc_workq_t *workq = (dwc_workq_t *)data;
  50356. + return workq->pending == 0;
  50357. +}
  50358. +
  50359. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  50360. +{
  50361. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  50362. +}
  50363. +
  50364. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  50365. +{
  50366. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  50367. +
  50368. + if (!wq) {
  50369. + return NULL;
  50370. + }
  50371. +
  50372. + wq->wq = create_singlethread_workqueue(name);
  50373. + if (!wq->wq) {
  50374. + goto no_wq;
  50375. + }
  50376. +
  50377. + wq->pending = 0;
  50378. +
  50379. + wq->lock = DWC_SPINLOCK_ALLOC();
  50380. + if (!wq->lock) {
  50381. + goto no_lock;
  50382. + }
  50383. +
  50384. + wq->waitq = DWC_WAITQ_ALLOC();
  50385. + if (!wq->waitq) {
  50386. + goto no_waitq;
  50387. + }
  50388. +
  50389. +#ifdef DEBUG
  50390. + DWC_CIRCLEQ_INIT(&wq->entries);
  50391. +#endif
  50392. + return wq;
  50393. +
  50394. + no_waitq:
  50395. + DWC_SPINLOCK_FREE(wq->lock);
  50396. + no_lock:
  50397. + destroy_workqueue(wq->wq);
  50398. + no_wq:
  50399. + DWC_FREE(wq);
  50400. +
  50401. + return NULL;
  50402. +}
  50403. +
  50404. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  50405. +{
  50406. +#ifdef DEBUG
  50407. + if (wq->pending != 0) {
  50408. + struct work_container *wc;
  50409. + DWC_ERROR("Destroying work queue with pending work");
  50410. + DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
  50411. + DWC_ERROR("Work %s still pending", wc->name);
  50412. + }
  50413. + }
  50414. +#endif
  50415. + destroy_workqueue(wq->wq);
  50416. + DWC_SPINLOCK_FREE(wq->lock);
  50417. + DWC_WAITQ_FREE(wq->waitq);
  50418. + DWC_FREE(wq);
  50419. +}
  50420. +
  50421. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  50422. + char *format, ...)
  50423. +{
  50424. + dwc_irqflags_t flags;
  50425. + work_container_t *container;
  50426. + static char name[128];
  50427. + va_list args;
  50428. +
  50429. + va_start(args, format);
  50430. + DWC_VSNPRINTF(name, 128, format, args);
  50431. + va_end(args);
  50432. +
  50433. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  50434. + wq->pending++;
  50435. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  50436. + DWC_WAITQ_TRIGGER(wq->waitq);
  50437. +
  50438. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  50439. + if (!container) {
  50440. + DWC_ERROR("Cannot allocate memory for container\n");
  50441. + return;
  50442. + }
  50443. +
  50444. + container->name = DWC_STRDUP(name);
  50445. + if (!container->name) {
  50446. + DWC_ERROR("Cannot allocate memory for container->name\n");
  50447. + DWC_FREE(container);
  50448. + return;
  50449. + }
  50450. +
  50451. + container->cb = cb;
  50452. + container->data = data;
  50453. + container->wq = wq;
  50454. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  50455. + INIT_WORK(&container->work.work, do_work);
  50456. +
  50457. +#ifdef DEBUG
  50458. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  50459. +#endif
  50460. + queue_work(wq->wq, &container->work.work);
  50461. +}
  50462. +
  50463. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  50464. + void *data, uint32_t time, char *format, ...)
  50465. +{
  50466. + dwc_irqflags_t flags;
  50467. + work_container_t *container;
  50468. + static char name[128];
  50469. + va_list args;
  50470. +
  50471. + va_start(args, format);
  50472. + DWC_VSNPRINTF(name, 128, format, args);
  50473. + va_end(args);
  50474. +
  50475. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  50476. + wq->pending++;
  50477. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  50478. + DWC_WAITQ_TRIGGER(wq->waitq);
  50479. +
  50480. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  50481. + if (!container) {
  50482. + DWC_ERROR("Cannot allocate memory for container\n");
  50483. + return;
  50484. + }
  50485. +
  50486. + container->name = DWC_STRDUP(name);
  50487. + if (!container->name) {
  50488. + DWC_ERROR("Cannot allocate memory for container->name\n");
  50489. + DWC_FREE(container);
  50490. + return;
  50491. + }
  50492. +
  50493. + container->cb = cb;
  50494. + container->data = data;
  50495. + container->wq = wq;
  50496. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  50497. + INIT_DELAYED_WORK(&container->work, do_work);
  50498. +
  50499. +#ifdef DEBUG
  50500. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  50501. +#endif
  50502. + queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
  50503. +}
  50504. +
  50505. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  50506. +{
  50507. + return wq->pending;
  50508. +}
  50509. +
  50510. +
  50511. +#ifdef DWC_LIBMODULE
  50512. +
  50513. +#ifdef DWC_CCLIB
  50514. +/* CC */
  50515. +EXPORT_SYMBOL(dwc_cc_if_alloc);
  50516. +EXPORT_SYMBOL(dwc_cc_if_free);
  50517. +EXPORT_SYMBOL(dwc_cc_clear);
  50518. +EXPORT_SYMBOL(dwc_cc_add);
  50519. +EXPORT_SYMBOL(dwc_cc_remove);
  50520. +EXPORT_SYMBOL(dwc_cc_change);
  50521. +EXPORT_SYMBOL(dwc_cc_data_for_save);
  50522. +EXPORT_SYMBOL(dwc_cc_restore_from_data);
  50523. +EXPORT_SYMBOL(dwc_cc_match_chid);
  50524. +EXPORT_SYMBOL(dwc_cc_match_cdid);
  50525. +EXPORT_SYMBOL(dwc_cc_ck);
  50526. +EXPORT_SYMBOL(dwc_cc_chid);
  50527. +EXPORT_SYMBOL(dwc_cc_cdid);
  50528. +EXPORT_SYMBOL(dwc_cc_name);
  50529. +#endif /* DWC_CCLIB */
  50530. +
  50531. +#ifdef DWC_CRYPTOLIB
  50532. +# ifndef CONFIG_MACH_IPMATE
  50533. +/* Modpow */
  50534. +EXPORT_SYMBOL(dwc_modpow);
  50535. +
  50536. +/* DH */
  50537. +EXPORT_SYMBOL(dwc_dh_modpow);
  50538. +EXPORT_SYMBOL(dwc_dh_derive_keys);
  50539. +EXPORT_SYMBOL(dwc_dh_pk);
  50540. +# endif /* CONFIG_MACH_IPMATE */
  50541. +
  50542. +/* Crypto */
  50543. +EXPORT_SYMBOL(dwc_wusb_aes_encrypt);
  50544. +EXPORT_SYMBOL(dwc_wusb_cmf);
  50545. +EXPORT_SYMBOL(dwc_wusb_prf);
  50546. +EXPORT_SYMBOL(dwc_wusb_fill_ccm_nonce);
  50547. +EXPORT_SYMBOL(dwc_wusb_gen_nonce);
  50548. +EXPORT_SYMBOL(dwc_wusb_gen_key);
  50549. +EXPORT_SYMBOL(dwc_wusb_gen_mic);
  50550. +#endif /* DWC_CRYPTOLIB */
  50551. +
  50552. +/* Notification */
  50553. +#ifdef DWC_NOTIFYLIB
  50554. +EXPORT_SYMBOL(dwc_alloc_notification_manager);
  50555. +EXPORT_SYMBOL(dwc_free_notification_manager);
  50556. +EXPORT_SYMBOL(dwc_register_notifier);
  50557. +EXPORT_SYMBOL(dwc_unregister_notifier);
  50558. +EXPORT_SYMBOL(dwc_add_observer);
  50559. +EXPORT_SYMBOL(dwc_remove_observer);
  50560. +EXPORT_SYMBOL(dwc_notify);
  50561. +#endif
  50562. +
  50563. +/* Memory Debugging Routines */
  50564. +#ifdef DWC_DEBUG_MEMORY
  50565. +EXPORT_SYMBOL(dwc_alloc_debug);
  50566. +EXPORT_SYMBOL(dwc_alloc_atomic_debug);
  50567. +EXPORT_SYMBOL(dwc_free_debug);
  50568. +EXPORT_SYMBOL(dwc_dma_alloc_debug);
  50569. +EXPORT_SYMBOL(dwc_dma_free_debug);
  50570. +#endif
  50571. +
  50572. +EXPORT_SYMBOL(DWC_MEMSET);
  50573. +EXPORT_SYMBOL(DWC_MEMCPY);
  50574. +EXPORT_SYMBOL(DWC_MEMMOVE);
  50575. +EXPORT_SYMBOL(DWC_MEMCMP);
  50576. +EXPORT_SYMBOL(DWC_STRNCMP);
  50577. +EXPORT_SYMBOL(DWC_STRCMP);
  50578. +EXPORT_SYMBOL(DWC_STRLEN);
  50579. +EXPORT_SYMBOL(DWC_STRCPY);
  50580. +EXPORT_SYMBOL(DWC_STRDUP);
  50581. +EXPORT_SYMBOL(DWC_ATOI);
  50582. +EXPORT_SYMBOL(DWC_ATOUI);
  50583. +
  50584. +#ifdef DWC_UTFLIB
  50585. +EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
  50586. +#endif /* DWC_UTFLIB */
  50587. +
  50588. +EXPORT_SYMBOL(DWC_IN_IRQ);
  50589. +EXPORT_SYMBOL(DWC_IN_BH);
  50590. +EXPORT_SYMBOL(DWC_VPRINTF);
  50591. +EXPORT_SYMBOL(DWC_VSNPRINTF);
  50592. +EXPORT_SYMBOL(DWC_PRINTF);
  50593. +EXPORT_SYMBOL(DWC_SPRINTF);
  50594. +EXPORT_SYMBOL(DWC_SNPRINTF);
  50595. +EXPORT_SYMBOL(__DWC_WARN);
  50596. +EXPORT_SYMBOL(__DWC_ERROR);
  50597. +EXPORT_SYMBOL(DWC_EXCEPTION);
  50598. +
  50599. +#ifdef DEBUG
  50600. +EXPORT_SYMBOL(__DWC_DEBUG);
  50601. +#endif
  50602. +
  50603. +EXPORT_SYMBOL(__DWC_DMA_ALLOC);
  50604. +EXPORT_SYMBOL(__DWC_DMA_ALLOC_ATOMIC);
  50605. +EXPORT_SYMBOL(__DWC_DMA_FREE);
  50606. +EXPORT_SYMBOL(__DWC_ALLOC);
  50607. +EXPORT_SYMBOL(__DWC_ALLOC_ATOMIC);
  50608. +EXPORT_SYMBOL(__DWC_FREE);
  50609. +
  50610. +#ifdef DWC_CRYPTOLIB
  50611. +EXPORT_SYMBOL(DWC_RANDOM_BYTES);
  50612. +EXPORT_SYMBOL(DWC_AES_CBC);
  50613. +EXPORT_SYMBOL(DWC_SHA256);
  50614. +EXPORT_SYMBOL(DWC_HMAC_SHA256);
  50615. +#endif
  50616. +
  50617. +EXPORT_SYMBOL(DWC_CPU_TO_LE32);
  50618. +EXPORT_SYMBOL(DWC_CPU_TO_BE32);
  50619. +EXPORT_SYMBOL(DWC_LE32_TO_CPU);
  50620. +EXPORT_SYMBOL(DWC_BE32_TO_CPU);
  50621. +EXPORT_SYMBOL(DWC_CPU_TO_LE16);
  50622. +EXPORT_SYMBOL(DWC_CPU_TO_BE16);
  50623. +EXPORT_SYMBOL(DWC_LE16_TO_CPU);
  50624. +EXPORT_SYMBOL(DWC_BE16_TO_CPU);
  50625. +EXPORT_SYMBOL(DWC_READ_REG32);
  50626. +EXPORT_SYMBOL(DWC_WRITE_REG32);
  50627. +EXPORT_SYMBOL(DWC_MODIFY_REG32);
  50628. +
  50629. +#if 0
  50630. +EXPORT_SYMBOL(DWC_READ_REG64);
  50631. +EXPORT_SYMBOL(DWC_WRITE_REG64);
  50632. +EXPORT_SYMBOL(DWC_MODIFY_REG64);
  50633. +#endif
  50634. +
  50635. +EXPORT_SYMBOL(DWC_SPINLOCK_ALLOC);
  50636. +EXPORT_SYMBOL(DWC_SPINLOCK_FREE);
  50637. +EXPORT_SYMBOL(DWC_SPINLOCK);
  50638. +EXPORT_SYMBOL(DWC_SPINUNLOCK);
  50639. +EXPORT_SYMBOL(DWC_SPINLOCK_IRQSAVE);
  50640. +EXPORT_SYMBOL(DWC_SPINUNLOCK_IRQRESTORE);
  50641. +EXPORT_SYMBOL(DWC_MUTEX_ALLOC);
  50642. +
  50643. +#if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
  50644. +EXPORT_SYMBOL(DWC_MUTEX_FREE);
  50645. +#endif
  50646. +
  50647. +EXPORT_SYMBOL(DWC_MUTEX_LOCK);
  50648. +EXPORT_SYMBOL(DWC_MUTEX_TRYLOCK);
  50649. +EXPORT_SYMBOL(DWC_MUTEX_UNLOCK);
  50650. +EXPORT_SYMBOL(DWC_UDELAY);
  50651. +EXPORT_SYMBOL(DWC_MDELAY);
  50652. +EXPORT_SYMBOL(DWC_MSLEEP);
  50653. +EXPORT_SYMBOL(DWC_TIME);
  50654. +EXPORT_SYMBOL(DWC_TIMER_ALLOC);
  50655. +EXPORT_SYMBOL(DWC_TIMER_FREE);
  50656. +EXPORT_SYMBOL(DWC_TIMER_SCHEDULE);
  50657. +EXPORT_SYMBOL(DWC_TIMER_CANCEL);
  50658. +EXPORT_SYMBOL(DWC_WAITQ_ALLOC);
  50659. +EXPORT_SYMBOL(DWC_WAITQ_FREE);
  50660. +EXPORT_SYMBOL(DWC_WAITQ_WAIT);
  50661. +EXPORT_SYMBOL(DWC_WAITQ_WAIT_TIMEOUT);
  50662. +EXPORT_SYMBOL(DWC_WAITQ_TRIGGER);
  50663. +EXPORT_SYMBOL(DWC_WAITQ_ABORT);
  50664. +EXPORT_SYMBOL(DWC_THREAD_RUN);
  50665. +EXPORT_SYMBOL(DWC_THREAD_STOP);
  50666. +EXPORT_SYMBOL(DWC_THREAD_SHOULD_STOP);
  50667. +EXPORT_SYMBOL(DWC_TASK_ALLOC);
  50668. +EXPORT_SYMBOL(DWC_TASK_FREE);
  50669. +EXPORT_SYMBOL(DWC_TASK_SCHEDULE);
  50670. +EXPORT_SYMBOL(DWC_WORKQ_WAIT_WORK_DONE);
  50671. +EXPORT_SYMBOL(DWC_WORKQ_ALLOC);
  50672. +EXPORT_SYMBOL(DWC_WORKQ_FREE);
  50673. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE);
  50674. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE_DELAYED);
  50675. +EXPORT_SYMBOL(DWC_WORKQ_PENDING);
  50676. +
  50677. +static int dwc_common_port_init_module(void)
  50678. +{
  50679. + int result = 0;
  50680. +
  50681. + printk(KERN_DEBUG "Module dwc_common_port init\n" );
  50682. +
  50683. +#ifdef DWC_DEBUG_MEMORY
  50684. + result = dwc_memory_debug_start(NULL);
  50685. + if (result) {
  50686. + printk(KERN_ERR
  50687. + "dwc_memory_debug_start() failed with error %d\n",
  50688. + result);
  50689. + return result;
  50690. + }
  50691. +#endif
  50692. +
  50693. +#ifdef DWC_NOTIFYLIB
  50694. + result = dwc_alloc_notification_manager(NULL, NULL);
  50695. + if (result) {
  50696. + printk(KERN_ERR
  50697. + "dwc_alloc_notification_manager() failed with error %d\n",
  50698. + result);
  50699. + return result;
  50700. + }
  50701. +#endif
  50702. + return result;
  50703. +}
  50704. +
  50705. +static void dwc_common_port_exit_module(void)
  50706. +{
  50707. + printk(KERN_DEBUG "Module dwc_common_port exit\n" );
  50708. +
  50709. +#ifdef DWC_NOTIFYLIB
  50710. + dwc_free_notification_manager();
  50711. +#endif
  50712. +
  50713. +#ifdef DWC_DEBUG_MEMORY
  50714. + dwc_memory_debug_stop();
  50715. +#endif
  50716. +}
  50717. +
  50718. +module_init(dwc_common_port_init_module);
  50719. +module_exit(dwc_common_port_exit_module);
  50720. +
  50721. +MODULE_DESCRIPTION("DWC Common Library - Portable version");
  50722. +MODULE_AUTHOR("Synopsys Inc.");
  50723. +MODULE_LICENSE ("GPL");
  50724. +
  50725. +#endif /* DWC_LIBMODULE */
  50726. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
  50727. --- linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c 1969-12-31 18:00:00.000000000 -0600
  50728. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c 2014-12-03 19:13:40.212418001 -0600
  50729. @@ -0,0 +1,1275 @@
  50730. +#include "dwc_os.h"
  50731. +#include "dwc_list.h"
  50732. +
  50733. +#ifdef DWC_CCLIB
  50734. +# include "dwc_cc.h"
  50735. +#endif
  50736. +
  50737. +#ifdef DWC_CRYPTOLIB
  50738. +# include "dwc_modpow.h"
  50739. +# include "dwc_dh.h"
  50740. +# include "dwc_crypto.h"
  50741. +#endif
  50742. +
  50743. +#ifdef DWC_NOTIFYLIB
  50744. +# include "dwc_notifier.h"
  50745. +#endif
  50746. +
  50747. +/* OS-Level Implementations */
  50748. +
  50749. +/* This is the NetBSD 4.0.1 kernel implementation of the DWC platform library. */
  50750. +
  50751. +
  50752. +/* MISC */
  50753. +
  50754. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  50755. +{
  50756. + return memset(dest, byte, size);
  50757. +}
  50758. +
  50759. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  50760. +{
  50761. + return memcpy(dest, src, size);
  50762. +}
  50763. +
  50764. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  50765. +{
  50766. + bcopy(src, dest, size);
  50767. + return dest;
  50768. +}
  50769. +
  50770. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  50771. +{
  50772. + return memcmp(m1, m2, size);
  50773. +}
  50774. +
  50775. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  50776. +{
  50777. + return strncmp(s1, s2, size);
  50778. +}
  50779. +
  50780. +int DWC_STRCMP(void *s1, void *s2)
  50781. +{
  50782. + return strcmp(s1, s2);
  50783. +}
  50784. +
  50785. +int DWC_STRLEN(char const *str)
  50786. +{
  50787. + return strlen(str);
  50788. +}
  50789. +
  50790. +char *DWC_STRCPY(char *to, char const *from)
  50791. +{
  50792. + return strcpy(to, from);
  50793. +}
  50794. +
  50795. +char *DWC_STRDUP(char const *str)
  50796. +{
  50797. + int len = DWC_STRLEN(str) + 1;
  50798. + char *new = DWC_ALLOC_ATOMIC(len);
  50799. +
  50800. + if (!new) {
  50801. + return NULL;
  50802. + }
  50803. +
  50804. + DWC_MEMCPY(new, str, len);
  50805. + return new;
  50806. +}
  50807. +
  50808. +int DWC_ATOI(char *str, int32_t *value)
  50809. +{
  50810. + char *end = NULL;
  50811. +
  50812. + /* NetBSD doesn't have 'strtol' in the kernel, but 'strtoul'
  50813. + * should be equivalent on 2's complement machines
  50814. + */
  50815. + *value = strtoul(str, &end, 0);
  50816. + if (*end == '\0') {
  50817. + return 0;
  50818. + }
  50819. +
  50820. + return -1;
  50821. +}
  50822. +
  50823. +int DWC_ATOUI(char *str, uint32_t *value)
  50824. +{
  50825. + char *end = NULL;
  50826. +
  50827. + *value = strtoul(str, &end, 0);
  50828. + if (*end == '\0') {
  50829. + return 0;
  50830. + }
  50831. +
  50832. + return -1;
  50833. +}
  50834. +
  50835. +
  50836. +#ifdef DWC_UTFLIB
  50837. +/* From usbstring.c */
  50838. +
  50839. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  50840. +{
  50841. + int count = 0;
  50842. + u8 c;
  50843. + u16 uchar;
  50844. +
  50845. + /* this insists on correct encodings, though not minimal ones.
  50846. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  50847. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  50848. + */
  50849. + while (len != 0 && (c = (u8) *s++) != 0) {
  50850. + if (unlikely(c & 0x80)) {
  50851. + // 2-byte sequence:
  50852. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  50853. + if ((c & 0xe0) == 0xc0) {
  50854. + uchar = (c & 0x1f) << 6;
  50855. +
  50856. + c = (u8) *s++;
  50857. + if ((c & 0xc0) != 0xc0)
  50858. + goto fail;
  50859. + c &= 0x3f;
  50860. + uchar |= c;
  50861. +
  50862. + // 3-byte sequence (most CJKV characters):
  50863. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  50864. + } else if ((c & 0xf0) == 0xe0) {
  50865. + uchar = (c & 0x0f) << 12;
  50866. +
  50867. + c = (u8) *s++;
  50868. + if ((c & 0xc0) != 0xc0)
  50869. + goto fail;
  50870. + c &= 0x3f;
  50871. + uchar |= c << 6;
  50872. +
  50873. + c = (u8) *s++;
  50874. + if ((c & 0xc0) != 0xc0)
  50875. + goto fail;
  50876. + c &= 0x3f;
  50877. + uchar |= c;
  50878. +
  50879. + /* no bogus surrogates */
  50880. + if (0xd800 <= uchar && uchar <= 0xdfff)
  50881. + goto fail;
  50882. +
  50883. + // 4-byte sequence (surrogate pairs, currently rare):
  50884. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  50885. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  50886. + // (uuuuu = wwww + 1)
  50887. + // FIXME accept the surrogate code points (only)
  50888. + } else
  50889. + goto fail;
  50890. + } else
  50891. + uchar = c;
  50892. + put_unaligned (cpu_to_le16 (uchar), cp++);
  50893. + count++;
  50894. + len--;
  50895. + }
  50896. + return count;
  50897. +fail:
  50898. + return -1;
  50899. +}
  50900. +
  50901. +#endif /* DWC_UTFLIB */
  50902. +
  50903. +
  50904. +/* dwc_debug.h */
  50905. +
  50906. +dwc_bool_t DWC_IN_IRQ(void)
  50907. +{
  50908. +// return in_irq();
  50909. + return 0;
  50910. +}
  50911. +
  50912. +dwc_bool_t DWC_IN_BH(void)
  50913. +{
  50914. +// return in_softirq();
  50915. + return 0;
  50916. +}
  50917. +
  50918. +void DWC_VPRINTF(char *format, va_list args)
  50919. +{
  50920. + vprintf(format, args);
  50921. +}
  50922. +
  50923. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  50924. +{
  50925. + return vsnprintf(str, size, format, args);
  50926. +}
  50927. +
  50928. +void DWC_PRINTF(char *format, ...)
  50929. +{
  50930. + va_list args;
  50931. +
  50932. + va_start(args, format);
  50933. + DWC_VPRINTF(format, args);
  50934. + va_end(args);
  50935. +}
  50936. +
  50937. +int DWC_SPRINTF(char *buffer, char *format, ...)
  50938. +{
  50939. + int retval;
  50940. + va_list args;
  50941. +
  50942. + va_start(args, format);
  50943. + retval = vsprintf(buffer, format, args);
  50944. + va_end(args);
  50945. + return retval;
  50946. +}
  50947. +
  50948. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  50949. +{
  50950. + int retval;
  50951. + va_list args;
  50952. +
  50953. + va_start(args, format);
  50954. + retval = vsnprintf(buffer, size, format, args);
  50955. + va_end(args);
  50956. + return retval;
  50957. +}
  50958. +
  50959. +void __DWC_WARN(char *format, ...)
  50960. +{
  50961. + va_list args;
  50962. +
  50963. + va_start(args, format);
  50964. + DWC_VPRINTF(format, args);
  50965. + va_end(args);
  50966. +}
  50967. +
  50968. +void __DWC_ERROR(char *format, ...)
  50969. +{
  50970. + va_list args;
  50971. +
  50972. + va_start(args, format);
  50973. + DWC_VPRINTF(format, args);
  50974. + va_end(args);
  50975. +}
  50976. +
  50977. +void DWC_EXCEPTION(char *format, ...)
  50978. +{
  50979. + va_list args;
  50980. +
  50981. + va_start(args, format);
  50982. + DWC_VPRINTF(format, args);
  50983. + va_end(args);
  50984. +// BUG_ON(1); ???
  50985. +}
  50986. +
  50987. +#ifdef DEBUG
  50988. +void __DWC_DEBUG(char *format, ...)
  50989. +{
  50990. + va_list args;
  50991. +
  50992. + va_start(args, format);
  50993. + DWC_VPRINTF(format, args);
  50994. + va_end(args);
  50995. +}
  50996. +#endif
  50997. +
  50998. +
  50999. +/* dwc_mem.h */
  51000. +
  51001. +#if 0
  51002. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  51003. + uint32_t align,
  51004. + uint32_t alloc)
  51005. +{
  51006. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  51007. + size, align, alloc);
  51008. + return (dwc_pool_t *)pool;
  51009. +}
  51010. +
  51011. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  51012. +{
  51013. + dma_pool_destroy((struct dma_pool *)pool);
  51014. +}
  51015. +
  51016. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  51017. +{
  51018. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  51019. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  51020. +}
  51021. +
  51022. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  51023. +{
  51024. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  51025. + memset(..);
  51026. +}
  51027. +
  51028. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  51029. +{
  51030. + dma_pool_free(pool, vaddr, daddr);
  51031. +}
  51032. +#endif
  51033. +
  51034. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  51035. +{
  51036. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  51037. + int error;
  51038. +
  51039. + error = bus_dmamem_alloc(dma->dma_tag, size, 1, size, dma->segs,
  51040. + sizeof(dma->segs) / sizeof(dma->segs[0]),
  51041. + &dma->nsegs, BUS_DMA_NOWAIT);
  51042. + if (error) {
  51043. + printf("%s: bus_dmamem_alloc(%ju) failed: %d\n", __func__,
  51044. + (uintmax_t)size, error);
  51045. + goto fail_0;
  51046. + }
  51047. +
  51048. + error = bus_dmamem_map(dma->dma_tag, dma->segs, dma->nsegs, size,
  51049. + (caddr_t *)&dma->dma_vaddr,
  51050. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
  51051. + if (error) {
  51052. + printf("%s: bus_dmamem_map failed: %d\n", __func__, error);
  51053. + goto fail_1;
  51054. + }
  51055. +
  51056. + error = bus_dmamap_create(dma->dma_tag, size, 1, size, 0,
  51057. + BUS_DMA_NOWAIT, &dma->dma_map);
  51058. + if (error) {
  51059. + printf("%s: bus_dmamap_create failed: %d\n", __func__, error);
  51060. + goto fail_2;
  51061. + }
  51062. +
  51063. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
  51064. + size, NULL, BUS_DMA_NOWAIT);
  51065. + if (error) {
  51066. + printf("%s: bus_dmamap_load failed: %d\n", __func__, error);
  51067. + goto fail_3;
  51068. + }
  51069. +
  51070. + dma->dma_paddr = (bus_addr_t)dma->segs[0].ds_addr;
  51071. + *dma_addr = dma->dma_paddr;
  51072. + return dma->dma_vaddr;
  51073. +
  51074. +fail_3:
  51075. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  51076. +fail_2:
  51077. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  51078. +fail_1:
  51079. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  51080. +fail_0:
  51081. + dma->dma_map = NULL;
  51082. + dma->dma_vaddr = NULL;
  51083. + dma->nsegs = 0;
  51084. +
  51085. + return NULL;
  51086. +}
  51087. +
  51088. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  51089. +{
  51090. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  51091. +
  51092. + if (dma->dma_map != NULL) {
  51093. + bus_dmamap_sync(dma->dma_tag, dma->dma_map, 0, size,
  51094. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  51095. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  51096. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  51097. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  51098. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  51099. + dma->dma_paddr = 0;
  51100. + dma->dma_map = NULL;
  51101. + dma->dma_vaddr = NULL;
  51102. + dma->nsegs = 0;
  51103. + }
  51104. +}
  51105. +
  51106. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  51107. +{
  51108. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  51109. +}
  51110. +
  51111. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  51112. +{
  51113. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  51114. +}
  51115. +
  51116. +void __DWC_FREE(void *mem_ctx, void *addr)
  51117. +{
  51118. + free(addr, M_DEVBUF);
  51119. +}
  51120. +
  51121. +
  51122. +#ifdef DWC_CRYPTOLIB
  51123. +/* dwc_crypto.h */
  51124. +
  51125. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  51126. +{
  51127. + get_random_bytes(buffer, length);
  51128. +}
  51129. +
  51130. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  51131. +{
  51132. + struct crypto_blkcipher *tfm;
  51133. + struct blkcipher_desc desc;
  51134. + struct scatterlist sgd;
  51135. + struct scatterlist sgs;
  51136. +
  51137. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  51138. + if (tfm == NULL) {
  51139. + printk("failed to load transform for aes CBC\n");
  51140. + return -1;
  51141. + }
  51142. +
  51143. + crypto_blkcipher_setkey(tfm, key, keylen);
  51144. + crypto_blkcipher_set_iv(tfm, iv, 16);
  51145. +
  51146. + sg_init_one(&sgd, out, messagelen);
  51147. + sg_init_one(&sgs, message, messagelen);
  51148. +
  51149. + desc.tfm = tfm;
  51150. + desc.flags = 0;
  51151. +
  51152. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  51153. + crypto_free_blkcipher(tfm);
  51154. + DWC_ERROR("AES CBC encryption failed");
  51155. + return -1;
  51156. + }
  51157. +
  51158. + crypto_free_blkcipher(tfm);
  51159. + return 0;
  51160. +}
  51161. +
  51162. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  51163. +{
  51164. + struct crypto_hash *tfm;
  51165. + struct hash_desc desc;
  51166. + struct scatterlist sg;
  51167. +
  51168. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  51169. + if (IS_ERR(tfm)) {
  51170. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  51171. + return 0;
  51172. + }
  51173. + desc.tfm = tfm;
  51174. + desc.flags = 0;
  51175. +
  51176. + sg_init_one(&sg, message, len);
  51177. + crypto_hash_digest(&desc, &sg, len, out);
  51178. + crypto_free_hash(tfm);
  51179. +
  51180. + return 1;
  51181. +}
  51182. +
  51183. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  51184. + uint8_t *key, uint32_t keylen, uint8_t *out)
  51185. +{
  51186. + struct crypto_hash *tfm;
  51187. + struct hash_desc desc;
  51188. + struct scatterlist sg;
  51189. +
  51190. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  51191. + if (IS_ERR(tfm)) {
  51192. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  51193. + return 0;
  51194. + }
  51195. + desc.tfm = tfm;
  51196. + desc.flags = 0;
  51197. +
  51198. + sg_init_one(&sg, message, messagelen);
  51199. + crypto_hash_setkey(tfm, key, keylen);
  51200. + crypto_hash_digest(&desc, &sg, messagelen, out);
  51201. + crypto_free_hash(tfm);
  51202. +
  51203. + return 1;
  51204. +}
  51205. +
  51206. +#endif /* DWC_CRYPTOLIB */
  51207. +
  51208. +
  51209. +/* Byte Ordering Conversions */
  51210. +
  51211. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  51212. +{
  51213. +#ifdef __LITTLE_ENDIAN
  51214. + return *p;
  51215. +#else
  51216. + uint8_t *u_p = (uint8_t *)p;
  51217. +
  51218. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  51219. +#endif
  51220. +}
  51221. +
  51222. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  51223. +{
  51224. +#ifdef __BIG_ENDIAN
  51225. + return *p;
  51226. +#else
  51227. + uint8_t *u_p = (uint8_t *)p;
  51228. +
  51229. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  51230. +#endif
  51231. +}
  51232. +
  51233. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  51234. +{
  51235. +#ifdef __LITTLE_ENDIAN
  51236. + return *p;
  51237. +#else
  51238. + uint8_t *u_p = (uint8_t *)p;
  51239. +
  51240. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  51241. +#endif
  51242. +}
  51243. +
  51244. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  51245. +{
  51246. +#ifdef __BIG_ENDIAN
  51247. + return *p;
  51248. +#else
  51249. + uint8_t *u_p = (uint8_t *)p;
  51250. +
  51251. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  51252. +#endif
  51253. +}
  51254. +
  51255. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  51256. +{
  51257. +#ifdef __LITTLE_ENDIAN
  51258. + return *p;
  51259. +#else
  51260. + uint8_t *u_p = (uint8_t *)p;
  51261. + return (u_p[1] | (u_p[0] << 8));
  51262. +#endif
  51263. +}
  51264. +
  51265. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  51266. +{
  51267. +#ifdef __BIG_ENDIAN
  51268. + return *p;
  51269. +#else
  51270. + uint8_t *u_p = (uint8_t *)p;
  51271. + return (u_p[1] | (u_p[0] << 8));
  51272. +#endif
  51273. +}
  51274. +
  51275. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  51276. +{
  51277. +#ifdef __LITTLE_ENDIAN
  51278. + return *p;
  51279. +#else
  51280. + uint8_t *u_p = (uint8_t *)p;
  51281. + return (u_p[1] | (u_p[0] << 8));
  51282. +#endif
  51283. +}
  51284. +
  51285. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  51286. +{
  51287. +#ifdef __BIG_ENDIAN
  51288. + return *p;
  51289. +#else
  51290. + uint8_t *u_p = (uint8_t *)p;
  51291. + return (u_p[1] | (u_p[0] << 8));
  51292. +#endif
  51293. +}
  51294. +
  51295. +
  51296. +/* Registers */
  51297. +
  51298. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  51299. +{
  51300. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  51301. + bus_size_t ior = (bus_size_t)reg;
  51302. +
  51303. + return bus_space_read_4(io->iot, io->ioh, ior);
  51304. +}
  51305. +
  51306. +#if 0
  51307. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  51308. +{
  51309. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  51310. + bus_size_t ior = (bus_size_t)reg;
  51311. +
  51312. + return bus_space_read_8(io->iot, io->ioh, ior);
  51313. +}
  51314. +#endif
  51315. +
  51316. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  51317. +{
  51318. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  51319. + bus_size_t ior = (bus_size_t)reg;
  51320. +
  51321. + bus_space_write_4(io->iot, io->ioh, ior, value);
  51322. +}
  51323. +
  51324. +#if 0
  51325. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  51326. +{
  51327. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  51328. + bus_size_t ior = (bus_size_t)reg;
  51329. +
  51330. + bus_space_write_8(io->iot, io->ioh, ior, value);
  51331. +}
  51332. +#endif
  51333. +
  51334. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  51335. + uint32_t set_mask)
  51336. +{
  51337. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  51338. + bus_size_t ior = (bus_size_t)reg;
  51339. +
  51340. + bus_space_write_4(io->iot, io->ioh, ior,
  51341. + (bus_space_read_4(io->iot, io->ioh, ior) &
  51342. + ~clear_mask) | set_mask);
  51343. +}
  51344. +
  51345. +#if 0
  51346. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  51347. + uint64_t set_mask)
  51348. +{
  51349. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  51350. + bus_size_t ior = (bus_size_t)reg;
  51351. +
  51352. + bus_space_write_8(io->iot, io->ioh, ior,
  51353. + (bus_space_read_8(io->iot, io->ioh, ior) &
  51354. + ~clear_mask) | set_mask);
  51355. +}
  51356. +#endif
  51357. +
  51358. +
  51359. +/* Locking */
  51360. +
  51361. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  51362. +{
  51363. + struct simplelock *sl = DWC_ALLOC(sizeof(*sl));
  51364. +
  51365. + if (!sl) {
  51366. + DWC_ERROR("Cannot allocate memory for spinlock");
  51367. + return NULL;
  51368. + }
  51369. +
  51370. + simple_lock_init(sl);
  51371. + return (dwc_spinlock_t *)sl;
  51372. +}
  51373. +
  51374. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  51375. +{
  51376. + struct simplelock *sl = (struct simplelock *)lock;
  51377. +
  51378. + DWC_FREE(sl);
  51379. +}
  51380. +
  51381. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  51382. +{
  51383. + simple_lock((struct simplelock *)lock);
  51384. +}
  51385. +
  51386. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  51387. +{
  51388. + simple_unlock((struct simplelock *)lock);
  51389. +}
  51390. +
  51391. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  51392. +{
  51393. + simple_lock((struct simplelock *)lock);
  51394. + *flags = splbio();
  51395. +}
  51396. +
  51397. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  51398. +{
  51399. + splx(flags);
  51400. + simple_unlock((struct simplelock *)lock);
  51401. +}
  51402. +
  51403. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  51404. +{
  51405. + dwc_mutex_t *mutex = DWC_ALLOC(sizeof(struct lock));
  51406. +
  51407. + if (!mutex) {
  51408. + DWC_ERROR("Cannot allocate memory for mutex");
  51409. + return NULL;
  51410. + }
  51411. +
  51412. + lockinit((struct lock *)mutex, 0, "dw3mtx", 0, 0);
  51413. + return mutex;
  51414. +}
  51415. +
  51416. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  51417. +#else
  51418. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  51419. +{
  51420. + DWC_FREE(mutex);
  51421. +}
  51422. +#endif
  51423. +
  51424. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  51425. +{
  51426. + lockmgr((struct lock *)mutex, LK_EXCLUSIVE, NULL);
  51427. +}
  51428. +
  51429. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  51430. +{
  51431. + int status;
  51432. +
  51433. + status = lockmgr((struct lock *)mutex, LK_EXCLUSIVE | LK_NOWAIT, NULL);
  51434. + return status == 0;
  51435. +}
  51436. +
  51437. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  51438. +{
  51439. + lockmgr((struct lock *)mutex, LK_RELEASE, NULL);
  51440. +}
  51441. +
  51442. +
  51443. +/* Timing */
  51444. +
  51445. +void DWC_UDELAY(uint32_t usecs)
  51446. +{
  51447. + DELAY(usecs);
  51448. +}
  51449. +
  51450. +void DWC_MDELAY(uint32_t msecs)
  51451. +{
  51452. + do {
  51453. + DELAY(1000);
  51454. + } while (--msecs);
  51455. +}
  51456. +
  51457. +void DWC_MSLEEP(uint32_t msecs)
  51458. +{
  51459. + struct timeval tv;
  51460. +
  51461. + tv.tv_sec = msecs / 1000;
  51462. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  51463. + tsleep(&tv, 0, "dw3slp", tvtohz(&tv));
  51464. +}
  51465. +
  51466. +uint32_t DWC_TIME(void)
  51467. +{
  51468. + struct timeval tv;
  51469. +
  51470. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  51471. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  51472. +}
  51473. +
  51474. +
  51475. +/* Timers */
  51476. +
  51477. +struct dwc_timer {
  51478. + struct callout t;
  51479. + char *name;
  51480. + dwc_spinlock_t *lock;
  51481. + dwc_timer_callback_t cb;
  51482. + void *data;
  51483. +};
  51484. +
  51485. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  51486. +{
  51487. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  51488. +
  51489. + if (!t) {
  51490. + DWC_ERROR("Cannot allocate memory for timer");
  51491. + return NULL;
  51492. + }
  51493. +
  51494. + callout_init(&t->t);
  51495. +
  51496. + t->name = DWC_STRDUP(name);
  51497. + if (!t->name) {
  51498. + DWC_ERROR("Cannot allocate memory for timer->name");
  51499. + goto no_name;
  51500. + }
  51501. +
  51502. + t->lock = DWC_SPINLOCK_ALLOC();
  51503. + if (!t->lock) {
  51504. + DWC_ERROR("Cannot allocate memory for timer->lock");
  51505. + goto no_lock;
  51506. + }
  51507. +
  51508. + t->cb = cb;
  51509. + t->data = data;
  51510. +
  51511. + return t;
  51512. +
  51513. + no_lock:
  51514. + DWC_FREE(t->name);
  51515. + no_name:
  51516. + DWC_FREE(t);
  51517. +
  51518. + return NULL;
  51519. +}
  51520. +
  51521. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  51522. +{
  51523. + callout_stop(&timer->t);
  51524. + DWC_SPINLOCK_FREE(timer->lock);
  51525. + DWC_FREE(timer->name);
  51526. + DWC_FREE(timer);
  51527. +}
  51528. +
  51529. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  51530. +{
  51531. + struct timeval tv;
  51532. +
  51533. + tv.tv_sec = time / 1000;
  51534. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  51535. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  51536. +}
  51537. +
  51538. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  51539. +{
  51540. + callout_stop(&timer->t);
  51541. +}
  51542. +
  51543. +
  51544. +/* Wait Queues */
  51545. +
  51546. +struct dwc_waitq {
  51547. + struct simplelock lock;
  51548. + int abort;
  51549. +};
  51550. +
  51551. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  51552. +{
  51553. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  51554. +
  51555. + if (!wq) {
  51556. + DWC_ERROR("Cannot allocate memory for waitqueue");
  51557. + return NULL;
  51558. + }
  51559. +
  51560. + simple_lock_init(&wq->lock);
  51561. + wq->abort = 0;
  51562. +
  51563. + return wq;
  51564. +}
  51565. +
  51566. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  51567. +{
  51568. + DWC_FREE(wq);
  51569. +}
  51570. +
  51571. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  51572. +{
  51573. + int ipl;
  51574. + int result = 0;
  51575. +
  51576. + simple_lock(&wq->lock);
  51577. + ipl = splbio();
  51578. +
  51579. + /* Skip the sleep if already aborted or triggered */
  51580. + if (!wq->abort && !cond(data)) {
  51581. + splx(ipl);
  51582. + result = ltsleep(wq, PCATCH, "dw3wat", 0, &wq->lock); // infinite timeout
  51583. + ipl = splbio();
  51584. + }
  51585. +
  51586. + if (result == 0) { // awoken
  51587. + if (wq->abort) {
  51588. + wq->abort = 0;
  51589. + result = -DWC_E_ABORT;
  51590. + } else {
  51591. + result = 0;
  51592. + }
  51593. +
  51594. + splx(ipl);
  51595. + simple_unlock(&wq->lock);
  51596. + } else {
  51597. + wq->abort = 0;
  51598. + splx(ipl);
  51599. + simple_unlock(&wq->lock);
  51600. +
  51601. + if (result == ERESTART) { // signaled - restart
  51602. + result = -DWC_E_RESTART;
  51603. + } else { // signaled - must be EINTR
  51604. + result = -DWC_E_ABORT;
  51605. + }
  51606. + }
  51607. +
  51608. + return result;
  51609. +}
  51610. +
  51611. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  51612. + void *data, int32_t msecs)
  51613. +{
  51614. + struct timeval tv, tv1, tv2;
  51615. + int ipl;
  51616. + int result = 0;
  51617. +
  51618. + tv.tv_sec = msecs / 1000;
  51619. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  51620. +
  51621. + simple_lock(&wq->lock);
  51622. + ipl = splbio();
  51623. +
  51624. + /* Skip the sleep if already aborted or triggered */
  51625. + if (!wq->abort && !cond(data)) {
  51626. + splx(ipl);
  51627. + getmicrouptime(&tv1);
  51628. + result = ltsleep(wq, PCATCH, "dw3wto", tvtohz(&tv), &wq->lock);
  51629. + getmicrouptime(&tv2);
  51630. + ipl = splbio();
  51631. + }
  51632. +
  51633. + if (result == 0) { // awoken
  51634. + if (wq->abort) {
  51635. + wq->abort = 0;
  51636. + splx(ipl);
  51637. + simple_unlock(&wq->lock);
  51638. + result = -DWC_E_ABORT;
  51639. + } else {
  51640. + splx(ipl);
  51641. + simple_unlock(&wq->lock);
  51642. +
  51643. + tv2.tv_usec -= tv1.tv_usec;
  51644. + if (tv2.tv_usec < 0) {
  51645. + tv2.tv_usec += 1000000;
  51646. + tv2.tv_sec--;
  51647. + }
  51648. +
  51649. + tv2.tv_sec -= tv1.tv_sec;
  51650. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  51651. + result = msecs - result;
  51652. + if (result <= 0)
  51653. + result = 1;
  51654. + }
  51655. + } else {
  51656. + wq->abort = 0;
  51657. + splx(ipl);
  51658. + simple_unlock(&wq->lock);
  51659. +
  51660. + if (result == ERESTART) { // signaled - restart
  51661. + result = -DWC_E_RESTART;
  51662. +
  51663. + } else if (result == EINTR) { // signaled - interrupt
  51664. + result = -DWC_E_ABORT;
  51665. +
  51666. + } else { // timed out
  51667. + result = -DWC_E_TIMEOUT;
  51668. + }
  51669. + }
  51670. +
  51671. + return result;
  51672. +}
  51673. +
  51674. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  51675. +{
  51676. + wakeup(wq);
  51677. +}
  51678. +
  51679. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  51680. +{
  51681. + int ipl;
  51682. +
  51683. + simple_lock(&wq->lock);
  51684. + ipl = splbio();
  51685. + wq->abort = 1;
  51686. + wakeup(wq);
  51687. + splx(ipl);
  51688. + simple_unlock(&wq->lock);
  51689. +}
  51690. +
  51691. +
  51692. +/* Threading */
  51693. +
  51694. +struct dwc_thread {
  51695. + struct proc *proc;
  51696. + int abort;
  51697. +};
  51698. +
  51699. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  51700. +{
  51701. + int retval;
  51702. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  51703. +
  51704. + if (!thread) {
  51705. + return NULL;
  51706. + }
  51707. +
  51708. + thread->abort = 0;
  51709. + retval = kthread_create1((void (*)(void *))func, data, &thread->proc,
  51710. + "%s", name);
  51711. + if (retval) {
  51712. + DWC_FREE(thread);
  51713. + return NULL;
  51714. + }
  51715. +
  51716. + return thread;
  51717. +}
  51718. +
  51719. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  51720. +{
  51721. + int retval;
  51722. +
  51723. + thread->abort = 1;
  51724. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  51725. +
  51726. + if (retval == 0) {
  51727. + /* DWC_THREAD_EXIT() will free the thread struct */
  51728. + return 0;
  51729. + }
  51730. +
  51731. + /* NOTE: We leak the thread struct if thread doesn't die */
  51732. +
  51733. + if (retval == EWOULDBLOCK) {
  51734. + return -DWC_E_TIMEOUT;
  51735. + }
  51736. +
  51737. + return -DWC_E_UNKNOWN;
  51738. +}
  51739. +
  51740. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  51741. +{
  51742. + return thread->abort;
  51743. +}
  51744. +
  51745. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  51746. +{
  51747. + wakeup(&thread->abort);
  51748. + DWC_FREE(thread);
  51749. + kthread_exit(0);
  51750. +}
  51751. +
  51752. +/* tasklets
  51753. + - Runs in interrupt context (cannot sleep)
  51754. + - Each tasklet runs on a single CPU
  51755. + - Different tasklets can be running simultaneously on different CPUs
  51756. + [ On NetBSD there is no corresponding mechanism, drivers don't have bottom-
  51757. + halves. So we just call the callback directly from DWC_TASK_SCHEDULE() ]
  51758. + */
  51759. +struct dwc_tasklet {
  51760. + dwc_tasklet_callback_t cb;
  51761. + void *data;
  51762. +};
  51763. +
  51764. +static void tasklet_callback(void *data)
  51765. +{
  51766. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  51767. +
  51768. + task->cb(task->data);
  51769. +}
  51770. +
  51771. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  51772. +{
  51773. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  51774. +
  51775. + if (task) {
  51776. + task->cb = cb;
  51777. + task->data = data;
  51778. + } else {
  51779. + DWC_ERROR("Cannot allocate memory for tasklet");
  51780. + }
  51781. +
  51782. + return task;
  51783. +}
  51784. +
  51785. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  51786. +{
  51787. + DWC_FREE(task);
  51788. +}
  51789. +
  51790. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  51791. +{
  51792. + tasklet_callback(task);
  51793. +}
  51794. +
  51795. +
  51796. +/* workqueues
  51797. + - Runs in process context (can sleep)
  51798. + */
  51799. +typedef struct work_container {
  51800. + dwc_work_callback_t cb;
  51801. + void *data;
  51802. + dwc_workq_t *wq;
  51803. + char *name;
  51804. + int hz;
  51805. + struct work task;
  51806. +} work_container_t;
  51807. +
  51808. +struct dwc_workq {
  51809. + struct workqueue *taskq;
  51810. + dwc_spinlock_t *lock;
  51811. + dwc_waitq_t *waitq;
  51812. + int pending;
  51813. + struct work_container *container;
  51814. +};
  51815. +
  51816. +static void do_work(struct work *task, void *data)
  51817. +{
  51818. + dwc_workq_t *wq = (dwc_workq_t *)data;
  51819. + work_container_t *container = wq->container;
  51820. + dwc_irqflags_t flags;
  51821. +
  51822. + if (container->hz) {
  51823. + tsleep(container, 0, "dw3wrk", container->hz);
  51824. + }
  51825. +
  51826. + container->cb(container->data);
  51827. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  51828. +
  51829. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  51830. + if (container->name)
  51831. + DWC_FREE(container->name);
  51832. + DWC_FREE(container);
  51833. + wq->pending--;
  51834. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  51835. + DWC_WAITQ_TRIGGER(wq->waitq);
  51836. +}
  51837. +
  51838. +static int work_done(void *data)
  51839. +{
  51840. + dwc_workq_t *workq = (dwc_workq_t *)data;
  51841. +
  51842. + return workq->pending == 0;
  51843. +}
  51844. +
  51845. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  51846. +{
  51847. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  51848. +}
  51849. +
  51850. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  51851. +{
  51852. + int result;
  51853. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  51854. +
  51855. + if (!wq) {
  51856. + DWC_ERROR("Cannot allocate memory for workqueue");
  51857. + return NULL;
  51858. + }
  51859. +
  51860. + result = workqueue_create(&wq->taskq, name, do_work, wq, 0 /*PWAIT*/,
  51861. + IPL_BIO, 0);
  51862. + if (result) {
  51863. + DWC_ERROR("Cannot create workqueue");
  51864. + goto no_taskq;
  51865. + }
  51866. +
  51867. + wq->pending = 0;
  51868. +
  51869. + wq->lock = DWC_SPINLOCK_ALLOC();
  51870. + if (!wq->lock) {
  51871. + DWC_ERROR("Cannot allocate memory for spinlock");
  51872. + goto no_lock;
  51873. + }
  51874. +
  51875. + wq->waitq = DWC_WAITQ_ALLOC();
  51876. + if (!wq->waitq) {
  51877. + DWC_ERROR("Cannot allocate memory for waitqueue");
  51878. + goto no_waitq;
  51879. + }
  51880. +
  51881. + return wq;
  51882. +
  51883. + no_waitq:
  51884. + DWC_SPINLOCK_FREE(wq->lock);
  51885. + no_lock:
  51886. + workqueue_destroy(wq->taskq);
  51887. + no_taskq:
  51888. + DWC_FREE(wq);
  51889. +
  51890. + return NULL;
  51891. +}
  51892. +
  51893. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  51894. +{
  51895. +#ifdef DEBUG
  51896. + dwc_irqflags_t flags;
  51897. +
  51898. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  51899. +
  51900. + if (wq->pending != 0) {
  51901. + struct work_container *container = wq->container;
  51902. +
  51903. + DWC_ERROR("Destroying work queue with pending work");
  51904. +
  51905. + if (container && container->name) {
  51906. + DWC_ERROR("Work %s still pending", container->name);
  51907. + }
  51908. + }
  51909. +
  51910. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  51911. +#endif
  51912. + DWC_WAITQ_FREE(wq->waitq);
  51913. + DWC_SPINLOCK_FREE(wq->lock);
  51914. + workqueue_destroy(wq->taskq);
  51915. + DWC_FREE(wq);
  51916. +}
  51917. +
  51918. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  51919. + char *format, ...)
  51920. +{
  51921. + dwc_irqflags_t flags;
  51922. + work_container_t *container;
  51923. + static char name[128];
  51924. + va_list args;
  51925. +
  51926. + va_start(args, format);
  51927. + DWC_VSNPRINTF(name, 128, format, args);
  51928. + va_end(args);
  51929. +
  51930. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  51931. + wq->pending++;
  51932. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  51933. + DWC_WAITQ_TRIGGER(wq->waitq);
  51934. +
  51935. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  51936. + if (!container) {
  51937. + DWC_ERROR("Cannot allocate memory for container");
  51938. + return;
  51939. + }
  51940. +
  51941. + container->name = DWC_STRDUP(name);
  51942. + if (!container->name) {
  51943. + DWC_ERROR("Cannot allocate memory for container->name");
  51944. + DWC_FREE(container);
  51945. + return;
  51946. + }
  51947. +
  51948. + container->cb = cb;
  51949. + container->data = data;
  51950. + container->wq = wq;
  51951. + container->hz = 0;
  51952. + wq->container = container;
  51953. +
  51954. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  51955. + workqueue_enqueue(wq->taskq, &container->task);
  51956. +}
  51957. +
  51958. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  51959. + void *data, uint32_t time, char *format, ...)
  51960. +{
  51961. + dwc_irqflags_t flags;
  51962. + work_container_t *container;
  51963. + static char name[128];
  51964. + struct timeval tv;
  51965. + va_list args;
  51966. +
  51967. + va_start(args, format);
  51968. + DWC_VSNPRINTF(name, 128, format, args);
  51969. + va_end(args);
  51970. +
  51971. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  51972. + wq->pending++;
  51973. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  51974. + DWC_WAITQ_TRIGGER(wq->waitq);
  51975. +
  51976. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  51977. + if (!container) {
  51978. + DWC_ERROR("Cannot allocate memory for container");
  51979. + return;
  51980. + }
  51981. +
  51982. + container->name = DWC_STRDUP(name);
  51983. + if (!container->name) {
  51984. + DWC_ERROR("Cannot allocate memory for container->name");
  51985. + DWC_FREE(container);
  51986. + return;
  51987. + }
  51988. +
  51989. + container->cb = cb;
  51990. + container->data = data;
  51991. + container->wq = wq;
  51992. + tv.tv_sec = time / 1000;
  51993. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  51994. + container->hz = tvtohz(&tv);
  51995. + wq->container = container;
  51996. +
  51997. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  51998. + workqueue_enqueue(wq->taskq, &container->task);
  51999. +}
  52000. +
  52001. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  52002. +{
  52003. + return wq->pending;
  52004. +}
  52005. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_crypto.c linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_crypto.c
  52006. --- linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_crypto.c 1969-12-31 18:00:00.000000000 -0600
  52007. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_crypto.c 2014-12-03 19:13:40.216418001 -0600
  52008. @@ -0,0 +1,308 @@
  52009. +/* =========================================================================
  52010. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.c $
  52011. + * $Revision: #5 $
  52012. + * $Date: 2010/09/28 $
  52013. + * $Change: 1596182 $
  52014. + *
  52015. + * Synopsys Portability Library Software and documentation
  52016. + * (hereinafter, "Software") is an Unsupported proprietary work of
  52017. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  52018. + * between Synopsys and you.
  52019. + *
  52020. + * The Software IS NOT an item of Licensed Software or Licensed Product
  52021. + * under any End User Software License Agreement or Agreement for
  52022. + * Licensed Product with Synopsys or any supplement thereto. You are
  52023. + * permitted to use and redistribute this Software in source and binary
  52024. + * forms, with or without modification, provided that redistributions
  52025. + * of source code must retain this notice. You may not view, use,
  52026. + * disclose, copy or distribute this file or any information contained
  52027. + * herein except pursuant to this license grant from Synopsys. If you
  52028. + * do not agree with this notice, including the disclaimer below, then
  52029. + * you are not authorized to use the Software.
  52030. + *
  52031. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  52032. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52033. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  52034. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  52035. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  52036. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  52037. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52038. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  52039. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52040. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  52041. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  52042. + * DAMAGE.
  52043. + * ========================================================================= */
  52044. +
  52045. +/** @file
  52046. + * This file contains the WUSB cryptographic routines.
  52047. + */
  52048. +
  52049. +#ifdef DWC_CRYPTOLIB
  52050. +
  52051. +#include "dwc_crypto.h"
  52052. +#include "usb.h"
  52053. +
  52054. +#ifdef DEBUG
  52055. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  52056. +{
  52057. + int i;
  52058. + DWC_PRINTF("%s: ", name);
  52059. + for (i=0; i<len; i++) {
  52060. + DWC_PRINTF("%02x ", bytes[i]);
  52061. + }
  52062. + DWC_PRINTF("\n");
  52063. +}
  52064. +#else
  52065. +#define dump_bytes(x...)
  52066. +#endif
  52067. +
  52068. +/* Display a block */
  52069. +void show_block(const u8 *blk, const char *prefix, const char *suffix, int a)
  52070. +{
  52071. +#ifdef DWC_DEBUG_CRYPTO
  52072. + int i, blksize = 16;
  52073. +
  52074. + DWC_DEBUG("%s", prefix);
  52075. +
  52076. + if (suffix == NULL) {
  52077. + suffix = "\n";
  52078. + blksize = a;
  52079. + }
  52080. +
  52081. + for (i = 0; i < blksize; i++)
  52082. + DWC_PRINT("%02x%s", *blk++, ((i & 3) == 3) ? " " : " ");
  52083. + DWC_PRINT(suffix);
  52084. +#endif
  52085. +}
  52086. +
  52087. +/**
  52088. + * Encrypts an array of bytes using the AES encryption engine.
  52089. + * If <code>dst</code> == <code>src</code>, then the bytes will be encrypted
  52090. + * in-place.
  52091. + *
  52092. + * @return 0 on success, negative error code on error.
  52093. + */
  52094. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst)
  52095. +{
  52096. + u8 block_t[16];
  52097. + DWC_MEMSET(block_t, 0, 16);
  52098. +
  52099. + return DWC_AES_CBC(src, 16, key, 16, block_t, dst);
  52100. +}
  52101. +
  52102. +/**
  52103. + * The CCM-MAC-FUNCTION described in section 6.5 of the WUSB spec.
  52104. + * This function takes a data string and returns the encrypted CBC
  52105. + * Counter-mode MIC.
  52106. + *
  52107. + * @param key The 128-bit symmetric key.
  52108. + * @param nonce The CCM nonce.
  52109. + * @param label The unique 14-byte ASCII text label.
  52110. + * @param bytes The byte array to be encrypted.
  52111. + * @param len Length of the byte array.
  52112. + * @param result Byte array to receive the 8-byte encrypted MIC.
  52113. + */
  52114. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  52115. + char *label, u8 *bytes, int len, u8 *result)
  52116. +{
  52117. + u8 block_m[16];
  52118. + u8 block_x[16];
  52119. + u8 block_t[8];
  52120. + int idx, blkNum;
  52121. + u16 la = (u16)(len + 14);
  52122. +
  52123. + /* Set the AES-128 key */
  52124. + //dwc_aes_setkey(tfm, key, 16);
  52125. +
  52126. + /* Fill block B0 from flags = 0x59, N, and l(m) = 0 */
  52127. + block_m[0] = 0x59;
  52128. + for (idx = 0; idx < 13; idx++)
  52129. + block_m[idx + 1] = nonce[idx];
  52130. + block_m[14] = 0;
  52131. + block_m[15] = 0;
  52132. +
  52133. + /* Produce the CBC IV */
  52134. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  52135. + show_block(block_m, "CBC IV in: ", "\n", 0);
  52136. + show_block(block_x, "CBC IV out:", "\n", 0);
  52137. +
  52138. + /* Fill block B1 from l(a) = Blen + 14, and A */
  52139. + block_x[0] ^= (u8)(la >> 8);
  52140. + block_x[1] ^= (u8)la;
  52141. + for (idx = 0; idx < 14; idx++)
  52142. + block_x[idx + 2] ^= label[idx];
  52143. + show_block(block_x, "After xor: ", "b1\n", 16);
  52144. +
  52145. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  52146. + show_block(block_x, "After AES: ", "b1\n", 16);
  52147. +
  52148. + idx = 0;
  52149. + blkNum = 0;
  52150. +
  52151. + /* Fill remaining blocks with B */
  52152. + while (len-- > 0) {
  52153. + block_x[idx] ^= *bytes++;
  52154. + if (++idx >= 16) {
  52155. + idx = 0;
  52156. + show_block(block_x, "After xor: ", "\n", blkNum);
  52157. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  52158. + show_block(block_x, "After AES: ", "\n", blkNum);
  52159. + blkNum++;
  52160. + }
  52161. + }
  52162. +
  52163. + /* Handle partial last block */
  52164. + if (idx > 0) {
  52165. + show_block(block_x, "After xor: ", "\n", blkNum);
  52166. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  52167. + show_block(block_x, "After AES: ", "\n", blkNum);
  52168. + }
  52169. +
  52170. + /* Save the MIC tag */
  52171. + DWC_MEMCPY(block_t, block_x, 8);
  52172. + show_block(block_t, "MIC tag : ", NULL, 8);
  52173. +
  52174. + /* Fill block A0 from flags = 0x01, N, and counter = 0 */
  52175. + block_m[0] = 0x01;
  52176. + block_m[14] = 0;
  52177. + block_m[15] = 0;
  52178. +
  52179. + /* Encrypt the counter */
  52180. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  52181. + show_block(block_x, "CTR[MIC] : ", NULL, 8);
  52182. +
  52183. + /* XOR with MIC tag */
  52184. + for (idx = 0; idx < 8; idx++) {
  52185. + block_t[idx] ^= block_x[idx];
  52186. + }
  52187. +
  52188. + /* Return result to caller */
  52189. + DWC_MEMCPY(result, block_t, 8);
  52190. + show_block(result, "CCM-MIC : ", NULL, 8);
  52191. +
  52192. +}
  52193. +
  52194. +/**
  52195. + * The PRF function described in section 6.5 of the WUSB spec. This function
  52196. + * concatenates MIC values returned from dwc_cmf() to create a value of
  52197. + * the requested length.
  52198. + *
  52199. + * @param prf_len Length of the PRF function in bits (64, 128, or 256).
  52200. + * @param key, nonce, label, bytes, len Same as for dwc_cmf().
  52201. + * @param result Byte array to receive the result.
  52202. + */
  52203. +void dwc_wusb_prf(int prf_len, u8 *key,
  52204. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result)
  52205. +{
  52206. + int i;
  52207. +
  52208. + nonce[0] = 0;
  52209. + for (i = 0; i < prf_len >> 6; i++, nonce[0]++) {
  52210. + dwc_wusb_cmf(key, nonce, label, bytes, len, result);
  52211. + result += 8;
  52212. + }
  52213. +}
  52214. +
  52215. +/**
  52216. + * Fills in CCM Nonce per the WUSB spec.
  52217. + *
  52218. + * @param[in] haddr Host address.
  52219. + * @param[in] daddr Device address.
  52220. + * @param[in] tkid Session Key(PTK) identifier.
  52221. + * @param[out] nonce Pointer to where the CCM Nonce output is to be written.
  52222. + */
  52223. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  52224. + uint8_t *nonce)
  52225. +{
  52226. +
  52227. + DWC_DEBUG("%s %x %x\n", __func__, daddr, haddr);
  52228. +
  52229. + DWC_MEMSET(&nonce[0], 0, 16);
  52230. +
  52231. + DWC_MEMCPY(&nonce[6], tkid, 3);
  52232. + nonce[9] = daddr & 0xFF;
  52233. + nonce[10] = (daddr >> 8) & 0xFF;
  52234. + nonce[11] = haddr & 0xFF;
  52235. + nonce[12] = (haddr >> 8) & 0xFF;
  52236. +
  52237. + dump_bytes("CCM nonce", nonce, 16);
  52238. +}
  52239. +
  52240. +/**
  52241. + * Generates a 16-byte cryptographic-grade random number for the Host/Device
  52242. + * Nonce.
  52243. + */
  52244. +void dwc_wusb_gen_nonce(uint16_t addr, uint8_t *nonce)
  52245. +{
  52246. + uint8_t inonce[16];
  52247. + uint32_t temp[4];
  52248. +
  52249. + /* Fill in the Nonce */
  52250. + DWC_MEMSET(&inonce[0], 0, sizeof(inonce));
  52251. + inonce[9] = addr & 0xFF;
  52252. + inonce[10] = (addr >> 8) & 0xFF;
  52253. + inonce[11] = inonce[9];
  52254. + inonce[12] = inonce[10];
  52255. +
  52256. + /* Collect "randomness samples" */
  52257. + DWC_RANDOM_BYTES((uint8_t *)temp, 16);
  52258. +
  52259. + dwc_wusb_prf_128((uint8_t *)temp, nonce,
  52260. + "Random Numbers", (uint8_t *)temp, sizeof(temp),
  52261. + nonce);
  52262. +}
  52263. +
  52264. +/**
  52265. + * Generates the Session Key (PTK) and Key Confirmation Key (KCK) per the
  52266. + * WUSB spec.
  52267. + *
  52268. + * @param[in] ccm_nonce Pointer to CCM Nonce.
  52269. + * @param[in] mk Master Key to derive the session from
  52270. + * @param[in] hnonce Pointer to Host Nonce.
  52271. + * @param[in] dnonce Pointer to Device Nonce.
  52272. + * @param[out] kck Pointer to where the KCK output is to be written.
  52273. + * @param[out] ptk Pointer to where the PTK output is to be written.
  52274. + */
  52275. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk, uint8_t *hnonce,
  52276. + uint8_t *dnonce, uint8_t *kck, uint8_t *ptk)
  52277. +{
  52278. + uint8_t idata[32];
  52279. + uint8_t odata[32];
  52280. +
  52281. + dump_bytes("ck", mk, 16);
  52282. + dump_bytes("hnonce", hnonce, 16);
  52283. + dump_bytes("dnonce", dnonce, 16);
  52284. +
  52285. + /* The data is the HNonce and DNonce concatenated */
  52286. + DWC_MEMCPY(&idata[0], hnonce, 16);
  52287. + DWC_MEMCPY(&idata[16], dnonce, 16);
  52288. +
  52289. + dwc_wusb_prf_256(mk, ccm_nonce, "Pair-wise keys", idata, 32, odata);
  52290. +
  52291. + /* Low 16 bytes of the result is the KCK, high 16 is the PTK */
  52292. + DWC_MEMCPY(kck, &odata[0], 16);
  52293. + DWC_MEMCPY(ptk, &odata[16], 16);
  52294. +
  52295. + dump_bytes("kck", kck, 16);
  52296. + dump_bytes("ptk", ptk, 16);
  52297. +}
  52298. +
  52299. +/**
  52300. + * Generates the Message Integrity Code over the Handshake data per the
  52301. + * WUSB spec.
  52302. + *
  52303. + * @param ccm_nonce Pointer to CCM Nonce.
  52304. + * @param kck Pointer to Key Confirmation Key.
  52305. + * @param data Pointer to Handshake data to be checked.
  52306. + * @param mic Pointer to where the MIC output is to be written.
  52307. + */
  52308. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t *kck,
  52309. + uint8_t *data, uint8_t *mic)
  52310. +{
  52311. +
  52312. + dwc_wusb_prf_64(kck, ccm_nonce, "out-of-bandMIC",
  52313. + data, WUSB_HANDSHAKE_LEN_FOR_MIC, mic);
  52314. +}
  52315. +
  52316. +#endif /* DWC_CRYPTOLIB */
  52317. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_crypto.h linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_crypto.h
  52318. --- linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_crypto.h 1969-12-31 18:00:00.000000000 -0600
  52319. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_crypto.h 2014-12-03 19:13:40.216418001 -0600
  52320. @@ -0,0 +1,111 @@
  52321. +/* =========================================================================
  52322. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.h $
  52323. + * $Revision: #3 $
  52324. + * $Date: 2010/09/28 $
  52325. + * $Change: 1596182 $
  52326. + *
  52327. + * Synopsys Portability Library Software and documentation
  52328. + * (hereinafter, "Software") is an Unsupported proprietary work of
  52329. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  52330. + * between Synopsys and you.
  52331. + *
  52332. + * The Software IS NOT an item of Licensed Software or Licensed Product
  52333. + * under any End User Software License Agreement or Agreement for
  52334. + * Licensed Product with Synopsys or any supplement thereto. You are
  52335. + * permitted to use and redistribute this Software in source and binary
  52336. + * forms, with or without modification, provided that redistributions
  52337. + * of source code must retain this notice. You may not view, use,
  52338. + * disclose, copy or distribute this file or any information contained
  52339. + * herein except pursuant to this license grant from Synopsys. If you
  52340. + * do not agree with this notice, including the disclaimer below, then
  52341. + * you are not authorized to use the Software.
  52342. + *
  52343. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  52344. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52345. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  52346. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  52347. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  52348. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  52349. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52350. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  52351. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52352. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  52353. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  52354. + * DAMAGE.
  52355. + * ========================================================================= */
  52356. +
  52357. +#ifndef _DWC_CRYPTO_H_
  52358. +#define _DWC_CRYPTO_H_
  52359. +
  52360. +#ifdef __cplusplus
  52361. +extern "C" {
  52362. +#endif
  52363. +
  52364. +/** @file
  52365. + *
  52366. + * This file contains declarations for the WUSB Cryptographic routines as
  52367. + * defined in the WUSB spec. They are only to be used internally by the DWC UWB
  52368. + * modules.
  52369. + */
  52370. +
  52371. +#include "dwc_os.h"
  52372. +
  52373. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst);
  52374. +
  52375. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  52376. + char *label, u8 *bytes, int len, u8 *result);
  52377. +void dwc_wusb_prf(int prf_len, u8 *key,
  52378. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result);
  52379. +
  52380. +/**
  52381. + * The PRF-64 function described in section 6.5 of the WUSB spec.
  52382. + *
  52383. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  52384. + */
  52385. +static inline void dwc_wusb_prf_64(u8 *key, u8 *nonce,
  52386. + char *label, u8 *bytes, int len, u8 *result)
  52387. +{
  52388. + dwc_wusb_prf(64, key, nonce, label, bytes, len, result);
  52389. +}
  52390. +
  52391. +/**
  52392. + * The PRF-128 function described in section 6.5 of the WUSB spec.
  52393. + *
  52394. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  52395. + */
  52396. +static inline void dwc_wusb_prf_128(u8 *key, u8 *nonce,
  52397. + char *label, u8 *bytes, int len, u8 *result)
  52398. +{
  52399. + dwc_wusb_prf(128, key, nonce, label, bytes, len, result);
  52400. +}
  52401. +
  52402. +/**
  52403. + * The PRF-256 function described in section 6.5 of the WUSB spec.
  52404. + *
  52405. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  52406. + */
  52407. +static inline void dwc_wusb_prf_256(u8 *key, u8 *nonce,
  52408. + char *label, u8 *bytes, int len, u8 *result)
  52409. +{
  52410. + dwc_wusb_prf(256, key, nonce, label, bytes, len, result);
  52411. +}
  52412. +
  52413. +
  52414. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  52415. + uint8_t *nonce);
  52416. +void dwc_wusb_gen_nonce(uint16_t addr,
  52417. + uint8_t *nonce);
  52418. +
  52419. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk,
  52420. + uint8_t *hnonce, uint8_t *dnonce,
  52421. + uint8_t *kck, uint8_t *ptk);
  52422. +
  52423. +
  52424. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t
  52425. + *kck, uint8_t *data, uint8_t *mic);
  52426. +
  52427. +#ifdef __cplusplus
  52428. +}
  52429. +#endif
  52430. +
  52431. +#endif /* _DWC_CRYPTO_H_ */
  52432. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_dh.c linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_dh.c
  52433. --- linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_dh.c 1969-12-31 18:00:00.000000000 -0600
  52434. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_dh.c 2014-12-03 19:13:40.216418001 -0600
  52435. @@ -0,0 +1,291 @@
  52436. +/* =========================================================================
  52437. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.c $
  52438. + * $Revision: #3 $
  52439. + * $Date: 2010/09/28 $
  52440. + * $Change: 1596182 $
  52441. + *
  52442. + * Synopsys Portability Library Software and documentation
  52443. + * (hereinafter, "Software") is an Unsupported proprietary work of
  52444. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  52445. + * between Synopsys and you.
  52446. + *
  52447. + * The Software IS NOT an item of Licensed Software or Licensed Product
  52448. + * under any End User Software License Agreement or Agreement for
  52449. + * Licensed Product with Synopsys or any supplement thereto. You are
  52450. + * permitted to use and redistribute this Software in source and binary
  52451. + * forms, with or without modification, provided that redistributions
  52452. + * of source code must retain this notice. You may not view, use,
  52453. + * disclose, copy or distribute this file or any information contained
  52454. + * herein except pursuant to this license grant from Synopsys. If you
  52455. + * do not agree with this notice, including the disclaimer below, then
  52456. + * you are not authorized to use the Software.
  52457. + *
  52458. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  52459. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52460. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  52461. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  52462. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  52463. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  52464. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52465. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  52466. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52467. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  52468. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  52469. + * DAMAGE.
  52470. + * ========================================================================= */
  52471. +#ifdef DWC_CRYPTOLIB
  52472. +
  52473. +#ifndef CONFIG_MACH_IPMATE
  52474. +
  52475. +#include "dwc_dh.h"
  52476. +#include "dwc_modpow.h"
  52477. +
  52478. +#ifdef DEBUG
  52479. +/* This function prints out a buffer in the format described in the Association
  52480. + * Model specification. */
  52481. +static void dh_dump(char *str, void *_num, int len)
  52482. +{
  52483. + uint8_t *num = _num;
  52484. + int i;
  52485. + DWC_PRINTF("%s\n", str);
  52486. + for (i = 0; i < len; i ++) {
  52487. + DWC_PRINTF("%02x", num[i]);
  52488. + if (((i + 1) % 2) == 0) DWC_PRINTF(" ");
  52489. + if (((i + 1) % 26) == 0) DWC_PRINTF("\n");
  52490. + }
  52491. +
  52492. + DWC_PRINTF("\n");
  52493. +}
  52494. +#else
  52495. +#define dh_dump(_x...) do {; } while(0)
  52496. +#endif
  52497. +
  52498. +/* Constant g value */
  52499. +static __u32 dh_g[] = {
  52500. + 0x02000000,
  52501. +};
  52502. +
  52503. +/* Constant p value */
  52504. +static __u32 dh_p[] = {
  52505. + 0xFFFFFFFF, 0xFFFFFFFF, 0xA2DA0FC9, 0x34C26821, 0x8B62C6C4, 0xD11CDC80, 0x084E0229, 0x74CC678A,
  52506. + 0xA6BE0B02, 0x229B133B, 0x79084A51, 0xDD04348E, 0xB31995EF, 0x1B433ACD, 0x6D0A2B30, 0x37145FF2,
  52507. + 0x6D35E14F, 0x45C2516D, 0x76B585E4, 0xC67E5E62, 0xE9424CF4, 0x6BED37A6, 0xB65CFF0B, 0xEDB706F4,
  52508. + 0xFB6B38EE, 0xA59F895A, 0x11249FAE, 0xE61F4B7C, 0x51662849, 0x3D5BE4EC, 0xB87C00C2, 0x05BF63A1,
  52509. + 0x3648DA98, 0x9AD3551C, 0xA83F1669, 0x5FCF24FD, 0x235D6583, 0x96ADA3DC, 0x56F3621C, 0xBB528520,
  52510. + 0x0729D59E, 0x6D969670, 0x4E350C67, 0x0498BC4A, 0x086C74F1, 0x7C2118CA, 0x465E9032, 0x3BCE362E,
  52511. + 0x2C779EE3, 0x03860E18, 0xA283279B, 0x8FA207EC, 0xF05DC5B5, 0xC9524C6F, 0xF6CB2BDE, 0x18175895,
  52512. + 0x7C499539, 0xE56A95EA, 0x1826D215, 0x1005FA98, 0x5A8E7215, 0x2DC4AA8A, 0x0D1733AD, 0x337A5004,
  52513. + 0xAB2155A8, 0x64BA1CDF, 0x0485FBEC, 0x0AEFDB58, 0x5771EA8A, 0x7D0C065D, 0x850F97B3, 0xC7E4E1A6,
  52514. + 0x8CAEF5AB, 0xD73309DB, 0xE0948C1E, 0x9D61254A, 0x26D2E3CE, 0x6BEED21A, 0x06FA2FF1, 0x64088AD9,
  52515. + 0x730276D8, 0x646AC83E, 0x182B1F52, 0x0C207B17, 0x5717E1BB, 0x6C5D617A, 0xC0880977, 0xE246D9BA,
  52516. + 0xA04FE208, 0x31ABE574, 0xFC5BDB43, 0x8E10FDE0, 0x20D1824B, 0xCAD23AA9, 0xFFFFFFFF, 0xFFFFFFFF,
  52517. +};
  52518. +
  52519. +static void dh_swap_bytes(void *_in, void *_out, uint32_t len)
  52520. +{
  52521. + uint8_t *in = _in;
  52522. + uint8_t *out = _out;
  52523. + int i;
  52524. + for (i=0; i<len; i++) {
  52525. + out[i] = in[len-1-i];
  52526. + }
  52527. +}
  52528. +
  52529. +/* Computes the modular exponentiation (num^exp % mod). num, exp, and mod are
  52530. + * big endian numbers of size len, in bytes. Each len value must be a multiple
  52531. + * of 4. */
  52532. +int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  52533. + void *exp, uint32_t exp_len,
  52534. + void *mod, uint32_t mod_len,
  52535. + void *out)
  52536. +{
  52537. + /* modpow() takes little endian numbers. AM uses big-endian. This
  52538. + * function swaps bytes of numbers before passing onto modpow. */
  52539. +
  52540. + int retval = 0;
  52541. + uint32_t *result;
  52542. +
  52543. + uint32_t *bignum_num = dwc_alloc(mem_ctx, num_len + 4);
  52544. + uint32_t *bignum_exp = dwc_alloc(mem_ctx, exp_len + 4);
  52545. + uint32_t *bignum_mod = dwc_alloc(mem_ctx, mod_len + 4);
  52546. +
  52547. + dh_swap_bytes(num, &bignum_num[1], num_len);
  52548. + bignum_num[0] = num_len / 4;
  52549. +
  52550. + dh_swap_bytes(exp, &bignum_exp[1], exp_len);
  52551. + bignum_exp[0] = exp_len / 4;
  52552. +
  52553. + dh_swap_bytes(mod, &bignum_mod[1], mod_len);
  52554. + bignum_mod[0] = mod_len / 4;
  52555. +
  52556. + result = dwc_modpow(mem_ctx, bignum_num, bignum_exp, bignum_mod);
  52557. + if (!result) {
  52558. + retval = -1;
  52559. + goto dh_modpow_nomem;
  52560. + }
  52561. +
  52562. + dh_swap_bytes(&result[1], out, result[0] * 4);
  52563. + dwc_free(mem_ctx, result);
  52564. +
  52565. + dh_modpow_nomem:
  52566. + dwc_free(mem_ctx, bignum_num);
  52567. + dwc_free(mem_ctx, bignum_exp);
  52568. + dwc_free(mem_ctx, bignum_mod);
  52569. + return retval;
  52570. +}
  52571. +
  52572. +
  52573. +int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pk, uint8_t *hash)
  52574. +{
  52575. + int retval;
  52576. + uint8_t m3[385];
  52577. +
  52578. +#ifndef DH_TEST_VECTORS
  52579. + DWC_RANDOM_BYTES(exp, 32);
  52580. +#endif
  52581. +
  52582. + /* Compute the pkd */
  52583. + if ((retval = dwc_dh_modpow(mem_ctx, dh_g, 4,
  52584. + exp, 32,
  52585. + dh_p, 384, pk))) {
  52586. + return retval;
  52587. + }
  52588. +
  52589. + m3[384] = nd;
  52590. + DWC_MEMCPY(&m3[0], pk, 384);
  52591. + DWC_SHA256(m3, 385, hash);
  52592. +
  52593. + dh_dump("PK", pk, 384);
  52594. + dh_dump("SHA-256(M3)", hash, 32);
  52595. + return 0;
  52596. +}
  52597. +
  52598. +int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  52599. + uint8_t *exp, int is_host,
  52600. + char *dd, uint8_t *ck, uint8_t *kdk)
  52601. +{
  52602. + int retval;
  52603. + uint8_t mv[784];
  52604. + uint8_t sha_result[32];
  52605. + uint8_t dhkey[384];
  52606. + uint8_t shared_secret[384];
  52607. + char *message;
  52608. + uint32_t vd;
  52609. +
  52610. + uint8_t *pk;
  52611. +
  52612. + if (is_host) {
  52613. + pk = pkd;
  52614. + }
  52615. + else {
  52616. + pk = pkh;
  52617. + }
  52618. +
  52619. + if ((retval = dwc_dh_modpow(mem_ctx, pk, 384,
  52620. + exp, 32,
  52621. + dh_p, 384, shared_secret))) {
  52622. + return retval;
  52623. + }
  52624. + dh_dump("Shared Secret", shared_secret, 384);
  52625. +
  52626. + DWC_SHA256(shared_secret, 384, dhkey);
  52627. + dh_dump("DHKEY", dhkey, 384);
  52628. +
  52629. + DWC_MEMCPY(&mv[0], pkd, 384);
  52630. + DWC_MEMCPY(&mv[384], pkh, 384);
  52631. + DWC_MEMCPY(&mv[768], "displayed digest", 16);
  52632. + dh_dump("MV", mv, 784);
  52633. +
  52634. + DWC_SHA256(mv, 784, sha_result);
  52635. + dh_dump("SHA-256(MV)", sha_result, 32);
  52636. + dh_dump("First 32-bits of SHA-256(MV)", sha_result, 4);
  52637. +
  52638. + dh_swap_bytes(sha_result, &vd, 4);
  52639. +#ifdef DEBUG
  52640. + DWC_PRINTF("Vd (decimal) = %d\n", vd);
  52641. +#endif
  52642. +
  52643. + switch (nd) {
  52644. + case 2:
  52645. + vd = vd % 100;
  52646. + DWC_SPRINTF(dd, "%02d", vd);
  52647. + break;
  52648. + case 3:
  52649. + vd = vd % 1000;
  52650. + DWC_SPRINTF(dd, "%03d", vd);
  52651. + break;
  52652. + case 4:
  52653. + vd = vd % 10000;
  52654. + DWC_SPRINTF(dd, "%04d", vd);
  52655. + break;
  52656. + }
  52657. +#ifdef DEBUG
  52658. + DWC_PRINTF("Display Digits: %s\n", dd);
  52659. +#endif
  52660. +
  52661. + message = "connection key";
  52662. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  52663. + dh_dump("HMAC(SHA-256, DHKey, connection key)", sha_result, 32);
  52664. + DWC_MEMCPY(ck, sha_result, 16);
  52665. +
  52666. + message = "key derivation key";
  52667. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  52668. + dh_dump("HMAC(SHA-256, DHKey, key derivation key)", sha_result, 32);
  52669. + DWC_MEMCPY(kdk, sha_result, 32);
  52670. +
  52671. + return 0;
  52672. +}
  52673. +
  52674. +
  52675. +#ifdef DH_TEST_VECTORS
  52676. +
  52677. +static __u8 dh_a[] = {
  52678. + 0x44, 0x00, 0x51, 0xd6,
  52679. + 0xf0, 0xb5, 0x5e, 0xa9,
  52680. + 0x67, 0xab, 0x31, 0xc6,
  52681. + 0x8a, 0x8b, 0x5e, 0x37,
  52682. + 0xd9, 0x10, 0xda, 0xe0,
  52683. + 0xe2, 0xd4, 0x59, 0xa4,
  52684. + 0x86, 0x45, 0x9c, 0xaa,
  52685. + 0xdf, 0x36, 0x75, 0x16,
  52686. +};
  52687. +
  52688. +static __u8 dh_b[] = {
  52689. + 0x5d, 0xae, 0xc7, 0x86,
  52690. + 0x79, 0x80, 0xa3, 0x24,
  52691. + 0x8c, 0xe3, 0x57, 0x8f,
  52692. + 0xc7, 0x5f, 0x1b, 0x0f,
  52693. + 0x2d, 0xf8, 0x9d, 0x30,
  52694. + 0x6f, 0xa4, 0x52, 0xcd,
  52695. + 0xe0, 0x7a, 0x04, 0x8a,
  52696. + 0xde, 0xd9, 0x26, 0x56,
  52697. +};
  52698. +
  52699. +void dwc_run_dh_test_vectors(void *mem_ctx)
  52700. +{
  52701. + uint8_t pkd[384];
  52702. + uint8_t pkh[384];
  52703. + uint8_t hashd[32];
  52704. + uint8_t hashh[32];
  52705. + uint8_t ck[16];
  52706. + uint8_t kdk[32];
  52707. + char dd[5];
  52708. +
  52709. + DWC_PRINTF("\n\n\nDH_TEST_VECTORS\n\n");
  52710. +
  52711. + /* compute the PKd and SHA-256(PKd || Nd) */
  52712. + DWC_PRINTF("Computing PKd\n");
  52713. + dwc_dh_pk(mem_ctx, 2, dh_a, pkd, hashd);
  52714. +
  52715. + /* compute the PKd and SHA-256(PKh || Nd) */
  52716. + DWC_PRINTF("Computing PKh\n");
  52717. + dwc_dh_pk(mem_ctx, 2, dh_b, pkh, hashh);
  52718. +
  52719. + /* compute the dhkey */
  52720. + dwc_dh_derive_keys(mem_ctx, 2, pkh, pkd, dh_a, 0, dd, ck, kdk);
  52721. +}
  52722. +#endif /* DH_TEST_VECTORS */
  52723. +
  52724. +#endif /* !CONFIG_MACH_IPMATE */
  52725. +
  52726. +#endif /* DWC_CRYPTOLIB */
  52727. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_dh.h linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_dh.h
  52728. --- linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_dh.h 1969-12-31 18:00:00.000000000 -0600
  52729. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_dh.h 2014-12-03 19:13:40.216418001 -0600
  52730. @@ -0,0 +1,106 @@
  52731. +/* =========================================================================
  52732. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.h $
  52733. + * $Revision: #4 $
  52734. + * $Date: 2010/09/28 $
  52735. + * $Change: 1596182 $
  52736. + *
  52737. + * Synopsys Portability Library Software and documentation
  52738. + * (hereinafter, "Software") is an Unsupported proprietary work of
  52739. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  52740. + * between Synopsys and you.
  52741. + *
  52742. + * The Software IS NOT an item of Licensed Software or Licensed Product
  52743. + * under any End User Software License Agreement or Agreement for
  52744. + * Licensed Product with Synopsys or any supplement thereto. You are
  52745. + * permitted to use and redistribute this Software in source and binary
  52746. + * forms, with or without modification, provided that redistributions
  52747. + * of source code must retain this notice. You may not view, use,
  52748. + * disclose, copy or distribute this file or any information contained
  52749. + * herein except pursuant to this license grant from Synopsys. If you
  52750. + * do not agree with this notice, including the disclaimer below, then
  52751. + * you are not authorized to use the Software.
  52752. + *
  52753. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  52754. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52755. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  52756. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  52757. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  52758. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  52759. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  52760. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  52761. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52762. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  52763. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  52764. + * DAMAGE.
  52765. + * ========================================================================= */
  52766. +#ifndef _DWC_DH_H_
  52767. +#define _DWC_DH_H_
  52768. +
  52769. +#ifdef __cplusplus
  52770. +extern "C" {
  52771. +#endif
  52772. +
  52773. +#include "dwc_os.h"
  52774. +
  52775. +/** @file
  52776. + *
  52777. + * This file defines the common functions on device and host for performing
  52778. + * numeric association as defined in the WUSB spec. They are only to be
  52779. + * used internally by the DWC UWB modules. */
  52780. +
  52781. +extern int dwc_dh_sha256(uint8_t *message, uint32_t len, uint8_t *out);
  52782. +extern int dwc_dh_hmac_sha256(uint8_t *message, uint32_t messagelen,
  52783. + uint8_t *key, uint32_t keylen,
  52784. + uint8_t *out);
  52785. +extern int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  52786. + void *exp, uint32_t exp_len,
  52787. + void *mod, uint32_t mod_len,
  52788. + void *out);
  52789. +
  52790. +/** Computes PKD or PKH, and SHA-256(PKd || Nd)
  52791. + *
  52792. + * PK = g^exp mod p.
  52793. + *
  52794. + * Input:
  52795. + * Nd = Number of digits on the device.
  52796. + *
  52797. + * Output:
  52798. + * exp = A 32-byte buffer to be filled with a randomly generated number.
  52799. + * used as either A or B.
  52800. + * pk = A 384-byte buffer to be filled with the PKH or PKD.
  52801. + * hash = A 32-byte buffer to be filled with SHA-256(PK || ND).
  52802. + */
  52803. +extern int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pkd, uint8_t *hash);
  52804. +
  52805. +/** Computes the DHKEY, and VD.
  52806. + *
  52807. + * If called from host, then it will comput DHKEY=PKD^exp % p.
  52808. + * If called from device, then it will comput DHKEY=PKH^exp % p.
  52809. + *
  52810. + * Input:
  52811. + * pkd = The PKD value.
  52812. + * pkh = The PKH value.
  52813. + * exp = The A value (if device) or B value (if host) generated in dwc_wudev_dh_pk.
  52814. + * is_host = Set to non zero if a WUSB host is calling this function.
  52815. + *
  52816. + * Output:
  52817. +
  52818. + * dd = A pointer to an buffer to be set to the displayed digits string to be shown
  52819. + * to the user. This buffer should be at 5 bytes long to hold 4 digits plus a
  52820. + * null termination character. This buffer can be used directly for display.
  52821. + * ck = A 16-byte buffer to be filled with the CK.
  52822. + * kdk = A 32-byte buffer to be filled with the KDK.
  52823. + */
  52824. +extern int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  52825. + uint8_t *exp, int is_host,
  52826. + char *dd, uint8_t *ck, uint8_t *kdk);
  52827. +
  52828. +#ifdef DH_TEST_VECTORS
  52829. +extern void dwc_run_dh_test_vectors(void);
  52830. +#endif
  52831. +
  52832. +#ifdef __cplusplus
  52833. +}
  52834. +#endif
  52835. +
  52836. +#endif /* _DWC_DH_H_ */
  52837. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_list.h linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_list.h
  52838. --- linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_list.h 1969-12-31 18:00:00.000000000 -0600
  52839. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_list.h 2014-12-03 19:13:40.216418001 -0600
  52840. @@ -0,0 +1,594 @@
  52841. +/* $OpenBSD: queue.h,v 1.26 2004/05/04 16:59:32 grange Exp $ */
  52842. +/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
  52843. +
  52844. +/*
  52845. + * Copyright (c) 1991, 1993
  52846. + * The Regents of the University of California. All rights reserved.
  52847. + *
  52848. + * Redistribution and use in source and binary forms, with or without
  52849. + * modification, are permitted provided that the following conditions
  52850. + * are met:
  52851. + * 1. Redistributions of source code must retain the above copyright
  52852. + * notice, this list of conditions and the following disclaimer.
  52853. + * 2. Redistributions in binary form must reproduce the above copyright
  52854. + * notice, this list of conditions and the following disclaimer in the
  52855. + * documentation and/or other materials provided with the distribution.
  52856. + * 3. Neither the name of the University nor the names of its contributors
  52857. + * may be used to endorse or promote products derived from this software
  52858. + * without specific prior written permission.
  52859. + *
  52860. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  52861. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  52862. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  52863. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  52864. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  52865. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  52866. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  52867. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  52868. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  52869. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  52870. + * SUCH DAMAGE.
  52871. + *
  52872. + * @(#)queue.h 8.5 (Berkeley) 8/20/94
  52873. + */
  52874. +
  52875. +#ifndef _DWC_LIST_H_
  52876. +#define _DWC_LIST_H_
  52877. +
  52878. +#ifdef __cplusplus
  52879. +extern "C" {
  52880. +#endif
  52881. +
  52882. +/** @file
  52883. + *
  52884. + * This file defines linked list operations. It is derived from BSD with
  52885. + * only the MACRO names being prefixed with DWC_. This is because a few of
  52886. + * these names conflict with those on Linux. For documentation on use, see the
  52887. + * inline comments in the source code. The original license for this source
  52888. + * code applies and is preserved in the dwc_list.h source file.
  52889. + */
  52890. +
  52891. +/*
  52892. + * This file defines five types of data structures: singly-linked lists,
  52893. + * lists, simple queues, tail queues, and circular queues.
  52894. + *
  52895. + *
  52896. + * A singly-linked list is headed by a single forward pointer. The elements
  52897. + * are singly linked for minimum space and pointer manipulation overhead at
  52898. + * the expense of O(n) removal for arbitrary elements. New elements can be
  52899. + * added to the list after an existing element or at the head of the list.
  52900. + * Elements being removed from the head of the list should use the explicit
  52901. + * macro for this purpose for optimum efficiency. A singly-linked list may
  52902. + * only be traversed in the forward direction. Singly-linked lists are ideal
  52903. + * for applications with large datasets and few or no removals or for
  52904. + * implementing a LIFO queue.
  52905. + *
  52906. + * A list is headed by a single forward pointer (or an array of forward
  52907. + * pointers for a hash table header). The elements are doubly linked
  52908. + * so that an arbitrary element can be removed without a need to
  52909. + * traverse the list. New elements can be added to the list before
  52910. + * or after an existing element or at the head of the list. A list
  52911. + * may only be traversed in the forward direction.
  52912. + *
  52913. + * A simple queue is headed by a pair of pointers, one the head of the
  52914. + * list and the other to the tail of the list. The elements are singly
  52915. + * linked to save space, so elements can only be removed from the
  52916. + * head of the list. New elements can be added to the list before or after
  52917. + * an existing element, at the head of the list, or at the end of the
  52918. + * list. A simple queue may only be traversed in the forward direction.
  52919. + *
  52920. + * A tail queue is headed by a pair of pointers, one to the head of the
  52921. + * list and the other to the tail of the list. The elements are doubly
  52922. + * linked so that an arbitrary element can be removed without a need to
  52923. + * traverse the list. New elements can be added to the list before or
  52924. + * after an existing element, at the head of the list, or at the end of
  52925. + * the list. A tail queue may be traversed in either direction.
  52926. + *
  52927. + * A circle queue is headed by a pair of pointers, one to the head of the
  52928. + * list and the other to the tail of the list. The elements are doubly
  52929. + * linked so that an arbitrary element can be removed without a need to
  52930. + * traverse the list. New elements can be added to the list before or after
  52931. + * an existing element, at the head of the list, or at the end of the list.
  52932. + * A circle queue may be traversed in either direction, but has a more
  52933. + * complex end of list detection.
  52934. + *
  52935. + * For details on the use of these macros, see the queue(3) manual page.
  52936. + */
  52937. +
  52938. +/*
  52939. + * Double-linked List.
  52940. + */
  52941. +
  52942. +typedef struct dwc_list_link {
  52943. + struct dwc_list_link *next;
  52944. + struct dwc_list_link *prev;
  52945. +} dwc_list_link_t;
  52946. +
  52947. +#define DWC_LIST_INIT(link) do { \
  52948. + (link)->next = (link); \
  52949. + (link)->prev = (link); \
  52950. +} while (0)
  52951. +
  52952. +#define DWC_LIST_FIRST(link) ((link)->next)
  52953. +#define DWC_LIST_LAST(link) ((link)->prev)
  52954. +#define DWC_LIST_END(link) (link)
  52955. +#define DWC_LIST_NEXT(link) ((link)->next)
  52956. +#define DWC_LIST_PREV(link) ((link)->prev)
  52957. +#define DWC_LIST_EMPTY(link) \
  52958. + (DWC_LIST_FIRST(link) == DWC_LIST_END(link))
  52959. +#define DWC_LIST_ENTRY(link, type, field) \
  52960. + (type *)((uint8_t *)(link) - (size_t)(&((type *)0)->field))
  52961. +
  52962. +#if 0
  52963. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  52964. + (link)->next = (list)->next; \
  52965. + (link)->prev = (list); \
  52966. + (list)->next->prev = (link); \
  52967. + (list)->next = (link); \
  52968. +} while (0)
  52969. +
  52970. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  52971. + (link)->next = (list); \
  52972. + (link)->prev = (list)->prev; \
  52973. + (list)->prev->next = (link); \
  52974. + (list)->prev = (link); \
  52975. +} while (0)
  52976. +#else
  52977. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  52978. + dwc_list_link_t *__next__ = (list)->next; \
  52979. + __next__->prev = (link); \
  52980. + (link)->next = __next__; \
  52981. + (link)->prev = (list); \
  52982. + (list)->next = (link); \
  52983. +} while (0)
  52984. +
  52985. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  52986. + dwc_list_link_t *__prev__ = (list)->prev; \
  52987. + (list)->prev = (link); \
  52988. + (link)->next = (list); \
  52989. + (link)->prev = __prev__; \
  52990. + __prev__->next = (link); \
  52991. +} while (0)
  52992. +#endif
  52993. +
  52994. +#if 0
  52995. +static inline void __list_add(struct list_head *new,
  52996. + struct list_head *prev,
  52997. + struct list_head *next)
  52998. +{
  52999. + next->prev = new;
  53000. + new->next = next;
  53001. + new->prev = prev;
  53002. + prev->next = new;
  53003. +}
  53004. +
  53005. +static inline void list_add(struct list_head *new, struct list_head *head)
  53006. +{
  53007. + __list_add(new, head, head->next);
  53008. +}
  53009. +
  53010. +static inline void list_add_tail(struct list_head *new, struct list_head *head)
  53011. +{
  53012. + __list_add(new, head->prev, head);
  53013. +}
  53014. +
  53015. +static inline void __list_del(struct list_head * prev, struct list_head * next)
  53016. +{
  53017. + next->prev = prev;
  53018. + prev->next = next;
  53019. +}
  53020. +
  53021. +static inline void list_del(struct list_head *entry)
  53022. +{
  53023. + __list_del(entry->prev, entry->next);
  53024. + entry->next = LIST_POISON1;
  53025. + entry->prev = LIST_POISON2;
  53026. +}
  53027. +#endif
  53028. +
  53029. +#define DWC_LIST_REMOVE(link) do { \
  53030. + (link)->next->prev = (link)->prev; \
  53031. + (link)->prev->next = (link)->next; \
  53032. +} while (0)
  53033. +
  53034. +#define DWC_LIST_REMOVE_INIT(link) do { \
  53035. + DWC_LIST_REMOVE(link); \
  53036. + DWC_LIST_INIT(link); \
  53037. +} while (0)
  53038. +
  53039. +#define DWC_LIST_MOVE_HEAD(list, link) do { \
  53040. + DWC_LIST_REMOVE(link); \
  53041. + DWC_LIST_INSERT_HEAD(list, link); \
  53042. +} while (0)
  53043. +
  53044. +#define DWC_LIST_MOVE_TAIL(list, link) do { \
  53045. + DWC_LIST_REMOVE(link); \
  53046. + DWC_LIST_INSERT_TAIL(list, link); \
  53047. +} while (0)
  53048. +
  53049. +#define DWC_LIST_FOREACH(var, list) \
  53050. + for((var) = DWC_LIST_FIRST(list); \
  53051. + (var) != DWC_LIST_END(list); \
  53052. + (var) = DWC_LIST_NEXT(var))
  53053. +
  53054. +#define DWC_LIST_FOREACH_SAFE(var, var2, list) \
  53055. + for((var) = DWC_LIST_FIRST(list), (var2) = DWC_LIST_NEXT(var); \
  53056. + (var) != DWC_LIST_END(list); \
  53057. + (var) = (var2), (var2) = DWC_LIST_NEXT(var2))
  53058. +
  53059. +#define DWC_LIST_FOREACH_REVERSE(var, list) \
  53060. + for((var) = DWC_LIST_LAST(list); \
  53061. + (var) != DWC_LIST_END(list); \
  53062. + (var) = DWC_LIST_PREV(var))
  53063. +
  53064. +/*
  53065. + * Singly-linked List definitions.
  53066. + */
  53067. +#define DWC_SLIST_HEAD(name, type) \
  53068. +struct name { \
  53069. + struct type *slh_first; /* first element */ \
  53070. +}
  53071. +
  53072. +#define DWC_SLIST_HEAD_INITIALIZER(head) \
  53073. + { NULL }
  53074. +
  53075. +#define DWC_SLIST_ENTRY(type) \
  53076. +struct { \
  53077. + struct type *sle_next; /* next element */ \
  53078. +}
  53079. +
  53080. +/*
  53081. + * Singly-linked List access methods.
  53082. + */
  53083. +#define DWC_SLIST_FIRST(head) ((head)->slh_first)
  53084. +#define DWC_SLIST_END(head) NULL
  53085. +#define DWC_SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
  53086. +#define DWC_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  53087. +
  53088. +#define DWC_SLIST_FOREACH(var, head, field) \
  53089. + for((var) = SLIST_FIRST(head); \
  53090. + (var) != SLIST_END(head); \
  53091. + (var) = SLIST_NEXT(var, field))
  53092. +
  53093. +#define DWC_SLIST_FOREACH_PREVPTR(var, varp, head, field) \
  53094. + for((varp) = &SLIST_FIRST((head)); \
  53095. + ((var) = *(varp)) != SLIST_END(head); \
  53096. + (varp) = &SLIST_NEXT((var), field))
  53097. +
  53098. +/*
  53099. + * Singly-linked List functions.
  53100. + */
  53101. +#define DWC_SLIST_INIT(head) { \
  53102. + SLIST_FIRST(head) = SLIST_END(head); \
  53103. +}
  53104. +
  53105. +#define DWC_SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  53106. + (elm)->field.sle_next = (slistelm)->field.sle_next; \
  53107. + (slistelm)->field.sle_next = (elm); \
  53108. +} while (0)
  53109. +
  53110. +#define DWC_SLIST_INSERT_HEAD(head, elm, field) do { \
  53111. + (elm)->field.sle_next = (head)->slh_first; \
  53112. + (head)->slh_first = (elm); \
  53113. +} while (0)
  53114. +
  53115. +#define DWC_SLIST_REMOVE_NEXT(head, elm, field) do { \
  53116. + (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
  53117. +} while (0)
  53118. +
  53119. +#define DWC_SLIST_REMOVE_HEAD(head, field) do { \
  53120. + (head)->slh_first = (head)->slh_first->field.sle_next; \
  53121. +} while (0)
  53122. +
  53123. +#define DWC_SLIST_REMOVE(head, elm, type, field) do { \
  53124. + if ((head)->slh_first == (elm)) { \
  53125. + SLIST_REMOVE_HEAD((head), field); \
  53126. + } \
  53127. + else { \
  53128. + struct type *curelm = (head)->slh_first; \
  53129. + while( curelm->field.sle_next != (elm) ) \
  53130. + curelm = curelm->field.sle_next; \
  53131. + curelm->field.sle_next = \
  53132. + curelm->field.sle_next->field.sle_next; \
  53133. + } \
  53134. +} while (0)
  53135. +
  53136. +/*
  53137. + * Simple queue definitions.
  53138. + */
  53139. +#define DWC_SIMPLEQ_HEAD(name, type) \
  53140. +struct name { \
  53141. + struct type *sqh_first; /* first element */ \
  53142. + struct type **sqh_last; /* addr of last next element */ \
  53143. +}
  53144. +
  53145. +#define DWC_SIMPLEQ_HEAD_INITIALIZER(head) \
  53146. + { NULL, &(head).sqh_first }
  53147. +
  53148. +#define DWC_SIMPLEQ_ENTRY(type) \
  53149. +struct { \
  53150. + struct type *sqe_next; /* next element */ \
  53151. +}
  53152. +
  53153. +/*
  53154. + * Simple queue access methods.
  53155. + */
  53156. +#define DWC_SIMPLEQ_FIRST(head) ((head)->sqh_first)
  53157. +#define DWC_SIMPLEQ_END(head) NULL
  53158. +#define DWC_SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
  53159. +#define DWC_SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
  53160. +
  53161. +#define DWC_SIMPLEQ_FOREACH(var, head, field) \
  53162. + for((var) = SIMPLEQ_FIRST(head); \
  53163. + (var) != SIMPLEQ_END(head); \
  53164. + (var) = SIMPLEQ_NEXT(var, field))
  53165. +
  53166. +/*
  53167. + * Simple queue functions.
  53168. + */
  53169. +#define DWC_SIMPLEQ_INIT(head) do { \
  53170. + (head)->sqh_first = NULL; \
  53171. + (head)->sqh_last = &(head)->sqh_first; \
  53172. +} while (0)
  53173. +
  53174. +#define DWC_SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
  53175. + if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
  53176. + (head)->sqh_last = &(elm)->field.sqe_next; \
  53177. + (head)->sqh_first = (elm); \
  53178. +} while (0)
  53179. +
  53180. +#define DWC_SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
  53181. + (elm)->field.sqe_next = NULL; \
  53182. + *(head)->sqh_last = (elm); \
  53183. + (head)->sqh_last = &(elm)->field.sqe_next; \
  53184. +} while (0)
  53185. +
  53186. +#define DWC_SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  53187. + if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
  53188. + (head)->sqh_last = &(elm)->field.sqe_next; \
  53189. + (listelm)->field.sqe_next = (elm); \
  53190. +} while (0)
  53191. +
  53192. +#define DWC_SIMPLEQ_REMOVE_HEAD(head, field) do { \
  53193. + if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
  53194. + (head)->sqh_last = &(head)->sqh_first; \
  53195. +} while (0)
  53196. +
  53197. +/*
  53198. + * Tail queue definitions.
  53199. + */
  53200. +#define DWC_TAILQ_HEAD(name, type) \
  53201. +struct name { \
  53202. + struct type *tqh_first; /* first element */ \
  53203. + struct type **tqh_last; /* addr of last next element */ \
  53204. +}
  53205. +
  53206. +#define DWC_TAILQ_HEAD_INITIALIZER(head) \
  53207. + { NULL, &(head).tqh_first }
  53208. +
  53209. +#define DWC_TAILQ_ENTRY(type) \
  53210. +struct { \
  53211. + struct type *tqe_next; /* next element */ \
  53212. + struct type **tqe_prev; /* address of previous next element */ \
  53213. +}
  53214. +
  53215. +/*
  53216. + * tail queue access methods
  53217. + */
  53218. +#define DWC_TAILQ_FIRST(head) ((head)->tqh_first)
  53219. +#define DWC_TAILQ_END(head) NULL
  53220. +#define DWC_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  53221. +#define DWC_TAILQ_LAST(head, headname) \
  53222. + (*(((struct headname *)((head)->tqh_last))->tqh_last))
  53223. +/* XXX */
  53224. +#define DWC_TAILQ_PREV(elm, headname, field) \
  53225. + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  53226. +#define DWC_TAILQ_EMPTY(head) \
  53227. + (DWC_TAILQ_FIRST(head) == DWC_TAILQ_END(head))
  53228. +
  53229. +#define DWC_TAILQ_FOREACH(var, head, field) \
  53230. + for ((var) = DWC_TAILQ_FIRST(head); \
  53231. + (var) != DWC_TAILQ_END(head); \
  53232. + (var) = DWC_TAILQ_NEXT(var, field))
  53233. +
  53234. +#define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  53235. + for ((var) = DWC_TAILQ_LAST(head, headname); \
  53236. + (var) != DWC_TAILQ_END(head); \
  53237. + (var) = DWC_TAILQ_PREV(var, headname, field))
  53238. +
  53239. +/*
  53240. + * Tail queue functions.
  53241. + */
  53242. +#define DWC_TAILQ_INIT(head) do { \
  53243. + (head)->tqh_first = NULL; \
  53244. + (head)->tqh_last = &(head)->tqh_first; \
  53245. +} while (0)
  53246. +
  53247. +#define DWC_TAILQ_INSERT_HEAD(head, elm, field) do { \
  53248. + if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
  53249. + (head)->tqh_first->field.tqe_prev = \
  53250. + &(elm)->field.tqe_next; \
  53251. + else \
  53252. + (head)->tqh_last = &(elm)->field.tqe_next; \
  53253. + (head)->tqh_first = (elm); \
  53254. + (elm)->field.tqe_prev = &(head)->tqh_first; \
  53255. +} while (0)
  53256. +
  53257. +#define DWC_TAILQ_INSERT_TAIL(head, elm, field) do { \
  53258. + (elm)->field.tqe_next = NULL; \
  53259. + (elm)->field.tqe_prev = (head)->tqh_last; \
  53260. + *(head)->tqh_last = (elm); \
  53261. + (head)->tqh_last = &(elm)->field.tqe_next; \
  53262. +} while (0)
  53263. +
  53264. +#define DWC_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  53265. + if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
  53266. + (elm)->field.tqe_next->field.tqe_prev = \
  53267. + &(elm)->field.tqe_next; \
  53268. + else \
  53269. + (head)->tqh_last = &(elm)->field.tqe_next; \
  53270. + (listelm)->field.tqe_next = (elm); \
  53271. + (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
  53272. +} while (0)
  53273. +
  53274. +#define DWC_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  53275. + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  53276. + (elm)->field.tqe_next = (listelm); \
  53277. + *(listelm)->field.tqe_prev = (elm); \
  53278. + (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
  53279. +} while (0)
  53280. +
  53281. +#define DWC_TAILQ_REMOVE(head, elm, field) do { \
  53282. + if (((elm)->field.tqe_next) != NULL) \
  53283. + (elm)->field.tqe_next->field.tqe_prev = \
  53284. + (elm)->field.tqe_prev; \
  53285. + else \
  53286. + (head)->tqh_last = (elm)->field.tqe_prev; \
  53287. + *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
  53288. +} while (0)
  53289. +
  53290. +#define DWC_TAILQ_REPLACE(head, elm, elm2, field) do { \
  53291. + if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
  53292. + (elm2)->field.tqe_next->field.tqe_prev = \
  53293. + &(elm2)->field.tqe_next; \
  53294. + else \
  53295. + (head)->tqh_last = &(elm2)->field.tqe_next; \
  53296. + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
  53297. + *(elm2)->field.tqe_prev = (elm2); \
  53298. +} while (0)
  53299. +
  53300. +/*
  53301. + * Circular queue definitions.
  53302. + */
  53303. +#define DWC_CIRCLEQ_HEAD(name, type) \
  53304. +struct name { \
  53305. + struct type *cqh_first; /* first element */ \
  53306. + struct type *cqh_last; /* last element */ \
  53307. +}
  53308. +
  53309. +#define DWC_CIRCLEQ_HEAD_INITIALIZER(head) \
  53310. + { DWC_CIRCLEQ_END(&head), DWC_CIRCLEQ_END(&head) }
  53311. +
  53312. +#define DWC_CIRCLEQ_ENTRY(type) \
  53313. +struct { \
  53314. + struct type *cqe_next; /* next element */ \
  53315. + struct type *cqe_prev; /* previous element */ \
  53316. +}
  53317. +
  53318. +/*
  53319. + * Circular queue access methods
  53320. + */
  53321. +#define DWC_CIRCLEQ_FIRST(head) ((head)->cqh_first)
  53322. +#define DWC_CIRCLEQ_LAST(head) ((head)->cqh_last)
  53323. +#define DWC_CIRCLEQ_END(head) ((void *)(head))
  53324. +#define DWC_CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
  53325. +#define DWC_CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
  53326. +#define DWC_CIRCLEQ_EMPTY(head) \
  53327. + (DWC_CIRCLEQ_FIRST(head) == DWC_CIRCLEQ_END(head))
  53328. +
  53329. +#define DWC_CIRCLEQ_EMPTY_ENTRY(elm, field) (((elm)->field.cqe_next == NULL) && ((elm)->field.cqe_prev == NULL))
  53330. +
  53331. +#define DWC_CIRCLEQ_FOREACH(var, head, field) \
  53332. + for((var) = DWC_CIRCLEQ_FIRST(head); \
  53333. + (var) != DWC_CIRCLEQ_END(head); \
  53334. + (var) = DWC_CIRCLEQ_NEXT(var, field))
  53335. +
  53336. +#define DWC_CIRCLEQ_FOREACH_SAFE(var, var2, head, field) \
  53337. + for((var) = DWC_CIRCLEQ_FIRST(head), var2 = DWC_CIRCLEQ_NEXT(var, field); \
  53338. + (var) != DWC_CIRCLEQ_END(head); \
  53339. + (var) = var2, var2 = DWC_CIRCLEQ_NEXT(var, field))
  53340. +
  53341. +#define DWC_CIRCLEQ_FOREACH_REVERSE(var, head, field) \
  53342. + for((var) = DWC_CIRCLEQ_LAST(head); \
  53343. + (var) != DWC_CIRCLEQ_END(head); \
  53344. + (var) = DWC_CIRCLEQ_PREV(var, field))
  53345. +
  53346. +/*
  53347. + * Circular queue functions.
  53348. + */
  53349. +#define DWC_CIRCLEQ_INIT(head) do { \
  53350. + (head)->cqh_first = DWC_CIRCLEQ_END(head); \
  53351. + (head)->cqh_last = DWC_CIRCLEQ_END(head); \
  53352. +} while (0)
  53353. +
  53354. +#define DWC_CIRCLEQ_INIT_ENTRY(elm, field) do { \
  53355. + (elm)->field.cqe_next = NULL; \
  53356. + (elm)->field.cqe_prev = NULL; \
  53357. +} while (0)
  53358. +
  53359. +#define DWC_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  53360. + (elm)->field.cqe_next = (listelm)->field.cqe_next; \
  53361. + (elm)->field.cqe_prev = (listelm); \
  53362. + if ((listelm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  53363. + (head)->cqh_last = (elm); \
  53364. + else \
  53365. + (listelm)->field.cqe_next->field.cqe_prev = (elm); \
  53366. + (listelm)->field.cqe_next = (elm); \
  53367. +} while (0)
  53368. +
  53369. +#define DWC_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
  53370. + (elm)->field.cqe_next = (listelm); \
  53371. + (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
  53372. + if ((listelm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  53373. + (head)->cqh_first = (elm); \
  53374. + else \
  53375. + (listelm)->field.cqe_prev->field.cqe_next = (elm); \
  53376. + (listelm)->field.cqe_prev = (elm); \
  53377. +} while (0)
  53378. +
  53379. +#define DWC_CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
  53380. + (elm)->field.cqe_next = (head)->cqh_first; \
  53381. + (elm)->field.cqe_prev = DWC_CIRCLEQ_END(head); \
  53382. + if ((head)->cqh_last == DWC_CIRCLEQ_END(head)) \
  53383. + (head)->cqh_last = (elm); \
  53384. + else \
  53385. + (head)->cqh_first->field.cqe_prev = (elm); \
  53386. + (head)->cqh_first = (elm); \
  53387. +} while (0)
  53388. +
  53389. +#define DWC_CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
  53390. + (elm)->field.cqe_next = DWC_CIRCLEQ_END(head); \
  53391. + (elm)->field.cqe_prev = (head)->cqh_last; \
  53392. + if ((head)->cqh_first == DWC_CIRCLEQ_END(head)) \
  53393. + (head)->cqh_first = (elm); \
  53394. + else \
  53395. + (head)->cqh_last->field.cqe_next = (elm); \
  53396. + (head)->cqh_last = (elm); \
  53397. +} while (0)
  53398. +
  53399. +#define DWC_CIRCLEQ_REMOVE(head, elm, field) do { \
  53400. + if ((elm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  53401. + (head)->cqh_last = (elm)->field.cqe_prev; \
  53402. + else \
  53403. + (elm)->field.cqe_next->field.cqe_prev = \
  53404. + (elm)->field.cqe_prev; \
  53405. + if ((elm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  53406. + (head)->cqh_first = (elm)->field.cqe_next; \
  53407. + else \
  53408. + (elm)->field.cqe_prev->field.cqe_next = \
  53409. + (elm)->field.cqe_next; \
  53410. +} while (0)
  53411. +
  53412. +#define DWC_CIRCLEQ_REMOVE_INIT(head, elm, field) do { \
  53413. + DWC_CIRCLEQ_REMOVE(head, elm, field); \
  53414. + DWC_CIRCLEQ_INIT_ENTRY(elm, field); \
  53415. +} while (0)
  53416. +
  53417. +#define DWC_CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
  53418. + if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
  53419. + DWC_CIRCLEQ_END(head)) \
  53420. + (head).cqh_last = (elm2); \
  53421. + else \
  53422. + (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
  53423. + if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
  53424. + DWC_CIRCLEQ_END(head)) \
  53425. + (head).cqh_first = (elm2); \
  53426. + else \
  53427. + (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
  53428. +} while (0)
  53429. +
  53430. +#ifdef __cplusplus
  53431. +}
  53432. +#endif
  53433. +
  53434. +#endif /* _DWC_LIST_H_ */
  53435. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_mem.c linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_mem.c
  53436. --- linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_mem.c 1969-12-31 18:00:00.000000000 -0600
  53437. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_mem.c 2014-12-03 19:13:40.216418001 -0600
  53438. @@ -0,0 +1,245 @@
  53439. +/* Memory Debugging */
  53440. +#ifdef DWC_DEBUG_MEMORY
  53441. +
  53442. +#include "dwc_os.h"
  53443. +#include "dwc_list.h"
  53444. +
  53445. +struct allocation {
  53446. + void *addr;
  53447. + void *ctx;
  53448. + char *func;
  53449. + int line;
  53450. + uint32_t size;
  53451. + int dma;
  53452. + DWC_CIRCLEQ_ENTRY(allocation) entry;
  53453. +};
  53454. +
  53455. +DWC_CIRCLEQ_HEAD(allocation_queue, allocation);
  53456. +
  53457. +struct allocation_manager {
  53458. + void *mem_ctx;
  53459. + struct allocation_queue allocations;
  53460. +
  53461. + /* statistics */
  53462. + int num;
  53463. + int num_freed;
  53464. + int num_active;
  53465. + uint32_t total;
  53466. + uint32_t cur;
  53467. + uint32_t max;
  53468. +};
  53469. +
  53470. +static struct allocation_manager *manager = NULL;
  53471. +
  53472. +static int add_allocation(void *ctx, uint32_t size, char const *func, int line, void *addr,
  53473. + int dma)
  53474. +{
  53475. + struct allocation *a;
  53476. +
  53477. + DWC_ASSERT(manager != NULL, "manager not allocated");
  53478. +
  53479. + a = __DWC_ALLOC_ATOMIC(manager->mem_ctx, sizeof(*a));
  53480. + if (!a) {
  53481. + return -DWC_E_NO_MEMORY;
  53482. + }
  53483. +
  53484. + a->func = __DWC_ALLOC_ATOMIC(manager->mem_ctx, DWC_STRLEN(func) + 1);
  53485. + if (!a->func) {
  53486. + __DWC_FREE(manager->mem_ctx, a);
  53487. + return -DWC_E_NO_MEMORY;
  53488. + }
  53489. +
  53490. + DWC_MEMCPY(a->func, func, DWC_STRLEN(func) + 1);
  53491. + a->addr = addr;
  53492. + a->ctx = ctx;
  53493. + a->line = line;
  53494. + a->size = size;
  53495. + a->dma = dma;
  53496. + DWC_CIRCLEQ_INSERT_TAIL(&manager->allocations, a, entry);
  53497. +
  53498. + /* Update stats */
  53499. + manager->num++;
  53500. + manager->num_active++;
  53501. + manager->total += size;
  53502. + manager->cur += size;
  53503. +
  53504. + if (manager->max < manager->cur) {
  53505. + manager->max = manager->cur;
  53506. + }
  53507. +
  53508. + return 0;
  53509. +}
  53510. +
  53511. +static struct allocation *find_allocation(void *ctx, void *addr)
  53512. +{
  53513. + struct allocation *a;
  53514. +
  53515. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  53516. + if (a->ctx == ctx && a->addr == addr) {
  53517. + return a;
  53518. + }
  53519. + }
  53520. +
  53521. + return NULL;
  53522. +}
  53523. +
  53524. +static void free_allocation(void *ctx, void *addr, char const *func, int line)
  53525. +{
  53526. + struct allocation *a = find_allocation(ctx, addr);
  53527. +
  53528. + if (!a) {
  53529. + DWC_ASSERT(0,
  53530. + "Free of address %p that was never allocated or already freed %s:%d",
  53531. + addr, func, line);
  53532. + return;
  53533. + }
  53534. +
  53535. + DWC_CIRCLEQ_REMOVE(&manager->allocations, a, entry);
  53536. +
  53537. + manager->num_active--;
  53538. + manager->num_freed++;
  53539. + manager->cur -= a->size;
  53540. + __DWC_FREE(manager->mem_ctx, a->func);
  53541. + __DWC_FREE(manager->mem_ctx, a);
  53542. +}
  53543. +
  53544. +int dwc_memory_debug_start(void *mem_ctx)
  53545. +{
  53546. + DWC_ASSERT(manager == NULL, "Memory debugging has already started\n");
  53547. +
  53548. + if (manager) {
  53549. + return -DWC_E_BUSY;
  53550. + }
  53551. +
  53552. + manager = __DWC_ALLOC(mem_ctx, sizeof(*manager));
  53553. + if (!manager) {
  53554. + return -DWC_E_NO_MEMORY;
  53555. + }
  53556. +
  53557. + DWC_CIRCLEQ_INIT(&manager->allocations);
  53558. + manager->mem_ctx = mem_ctx;
  53559. + manager->num = 0;
  53560. + manager->num_freed = 0;
  53561. + manager->num_active = 0;
  53562. + manager->total = 0;
  53563. + manager->cur = 0;
  53564. + manager->max = 0;
  53565. +
  53566. + return 0;
  53567. +}
  53568. +
  53569. +void dwc_memory_debug_stop(void)
  53570. +{
  53571. + struct allocation *a;
  53572. +
  53573. + dwc_memory_debug_report();
  53574. +
  53575. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  53576. + DWC_ERROR("Memory leaked from %s:%d\n", a->func, a->line);
  53577. + free_allocation(a->ctx, a->addr, NULL, -1);
  53578. + }
  53579. +
  53580. + __DWC_FREE(manager->mem_ctx, manager);
  53581. +}
  53582. +
  53583. +void dwc_memory_debug_report(void)
  53584. +{
  53585. + struct allocation *a;
  53586. +
  53587. + DWC_PRINTF("\n\n\n----------------- Memory Debugging Report -----------------\n\n");
  53588. + DWC_PRINTF("Num Allocations = %d\n", manager->num);
  53589. + DWC_PRINTF("Freed = %d\n", manager->num_freed);
  53590. + DWC_PRINTF("Active = %d\n", manager->num_active);
  53591. + DWC_PRINTF("Current Memory Used = %d\n", manager->cur);
  53592. + DWC_PRINTF("Total Memory Used = %d\n", manager->total);
  53593. + DWC_PRINTF("Maximum Memory Used at Once = %d\n", manager->max);
  53594. + DWC_PRINTF("Unfreed allocations:\n");
  53595. +
  53596. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  53597. + DWC_PRINTF(" addr=%p, size=%d from %s:%d, DMA=%d\n",
  53598. + a->addr, a->size, a->func, a->line, a->dma);
  53599. + }
  53600. +}
  53601. +
  53602. +/* The replacement functions */
  53603. +void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line)
  53604. +{
  53605. + void *addr = __DWC_ALLOC(mem_ctx, size);
  53606. +
  53607. + if (!addr) {
  53608. + return NULL;
  53609. + }
  53610. +
  53611. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  53612. + __DWC_FREE(mem_ctx, addr);
  53613. + return NULL;
  53614. + }
  53615. +
  53616. + return addr;
  53617. +}
  53618. +
  53619. +void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func,
  53620. + int line)
  53621. +{
  53622. + void *addr = __DWC_ALLOC_ATOMIC(mem_ctx, size);
  53623. +
  53624. + if (!addr) {
  53625. + return NULL;
  53626. + }
  53627. +
  53628. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  53629. + __DWC_FREE(mem_ctx, addr);
  53630. + return NULL;
  53631. + }
  53632. +
  53633. + return addr;
  53634. +}
  53635. +
  53636. +void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line)
  53637. +{
  53638. + free_allocation(mem_ctx, addr, func, line);
  53639. + __DWC_FREE(mem_ctx, addr);
  53640. +}
  53641. +
  53642. +void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  53643. + char const *func, int line)
  53644. +{
  53645. + void *addr = __DWC_DMA_ALLOC(dma_ctx, size, dma_addr);
  53646. +
  53647. + if (!addr) {
  53648. + return NULL;
  53649. + }
  53650. +
  53651. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  53652. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  53653. + return NULL;
  53654. + }
  53655. +
  53656. + return addr;
  53657. +}
  53658. +
  53659. +void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size,
  53660. + dwc_dma_t *dma_addr, char const *func, int line)
  53661. +{
  53662. + void *addr = __DWC_DMA_ALLOC_ATOMIC(dma_ctx, size, dma_addr);
  53663. +
  53664. + if (!addr) {
  53665. + return NULL;
  53666. + }
  53667. +
  53668. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  53669. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  53670. + return NULL;
  53671. + }
  53672. +
  53673. + return addr;
  53674. +}
  53675. +
  53676. +void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  53677. + dwc_dma_t dma_addr, char const *func, int line)
  53678. +{
  53679. + free_allocation(dma_ctx, virt_addr, func, line);
  53680. + __DWC_DMA_FREE(dma_ctx, size, virt_addr, dma_addr);
  53681. +}
  53682. +
  53683. +#endif /* DWC_DEBUG_MEMORY */
  53684. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_modpow.c linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_modpow.c
  53685. --- linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_modpow.c 1969-12-31 18:00:00.000000000 -0600
  53686. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_modpow.c 2014-12-03 19:13:40.216418001 -0600
  53687. @@ -0,0 +1,636 @@
  53688. +/* Bignum routines adapted from PUTTY sources. PuTTY copyright notice follows.
  53689. + *
  53690. + * PuTTY is copyright 1997-2007 Simon Tatham.
  53691. + *
  53692. + * Portions copyright Robert de Bath, Joris van Rantwijk, Delian
  53693. + * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,
  53694. + * Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus
  53695. + * Kuhn, and CORE SDI S.A.
  53696. + *
  53697. + * Permission is hereby granted, free of charge, to any person
  53698. + * obtaining a copy of this software and associated documentation files
  53699. + * (the "Software"), to deal in the Software without restriction,
  53700. + * including without limitation the rights to use, copy, modify, merge,
  53701. + * publish, distribute, sublicense, and/or sell copies of the Software,
  53702. + * and to permit persons to whom the Software is furnished to do so,
  53703. + * subject to the following conditions:
  53704. + *
  53705. + * The above copyright notice and this permission notice shall be
  53706. + * included in all copies or substantial portions of the Software.
  53707. +
  53708. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  53709. + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  53710. + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  53711. + * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
  53712. + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  53713. + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  53714. + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  53715. + *
  53716. + */
  53717. +#ifdef DWC_CRYPTOLIB
  53718. +
  53719. +#ifndef CONFIG_MACH_IPMATE
  53720. +
  53721. +#include "dwc_modpow.h"
  53722. +
  53723. +#define BIGNUM_INT_MASK 0xFFFFFFFFUL
  53724. +#define BIGNUM_TOP_BIT 0x80000000UL
  53725. +#define BIGNUM_INT_BITS 32
  53726. +
  53727. +
  53728. +static void *snmalloc(void *mem_ctx, size_t n, size_t size)
  53729. +{
  53730. + void *p;
  53731. + size *= n;
  53732. + if (size == 0) size = 1;
  53733. + p = dwc_alloc(mem_ctx, size);
  53734. + return p;
  53735. +}
  53736. +
  53737. +#define snewn(ctx, n, type) ((type *)snmalloc((ctx), (n), sizeof(type)))
  53738. +#define sfree dwc_free
  53739. +
  53740. +/*
  53741. + * Usage notes:
  53742. + * * Do not call the DIVMOD_WORD macro with expressions such as array
  53743. + * subscripts, as some implementations object to this (see below).
  53744. + * * Note that none of the division methods below will cope if the
  53745. + * quotient won't fit into BIGNUM_INT_BITS. Callers should be careful
  53746. + * to avoid this case.
  53747. + * If this condition occurs, in the case of the x86 DIV instruction,
  53748. + * an overflow exception will occur, which (according to a correspondent)
  53749. + * will manifest on Windows as something like
  53750. + * 0xC0000095: Integer overflow
  53751. + * The C variant won't give the right answer, either.
  53752. + */
  53753. +
  53754. +#define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
  53755. +
  53756. +#if defined __GNUC__ && defined __i386__
  53757. +#define DIVMOD_WORD(q, r, hi, lo, w) \
  53758. + __asm__("div %2" : \
  53759. + "=d" (r), "=a" (q) : \
  53760. + "r" (w), "d" (hi), "a" (lo))
  53761. +#else
  53762. +#define DIVMOD_WORD(q, r, hi, lo, w) do { \
  53763. + BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \
  53764. + q = n / w; \
  53765. + r = n % w; \
  53766. +} while (0)
  53767. +#endif
  53768. +
  53769. +// q = n / w;
  53770. +// r = n % w;
  53771. +
  53772. +#define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
  53773. +
  53774. +#define BIGNUM_INTERNAL
  53775. +
  53776. +static Bignum newbn(void *mem_ctx, int length)
  53777. +{
  53778. + Bignum b = snewn(mem_ctx, length + 1, BignumInt);
  53779. + //if (!b)
  53780. + //abort(); /* FIXME */
  53781. + DWC_MEMSET(b, 0, (length + 1) * sizeof(*b));
  53782. + b[0] = length;
  53783. + return b;
  53784. +}
  53785. +
  53786. +void freebn(void *mem_ctx, Bignum b)
  53787. +{
  53788. + /*
  53789. + * Burn the evidence, just in case.
  53790. + */
  53791. + DWC_MEMSET(b, 0, sizeof(b[0]) * (b[0] + 1));
  53792. + sfree(mem_ctx, b);
  53793. +}
  53794. +
  53795. +/*
  53796. + * Compute c = a * b.
  53797. + * Input is in the first len words of a and b.
  53798. + * Result is returned in the first 2*len words of c.
  53799. + */
  53800. +static void internal_mul(BignumInt *a, BignumInt *b,
  53801. + BignumInt *c, int len)
  53802. +{
  53803. + int i, j;
  53804. + BignumDblInt t;
  53805. +
  53806. + for (j = 0; j < 2 * len; j++)
  53807. + c[j] = 0;
  53808. +
  53809. + for (i = len - 1; i >= 0; i--) {
  53810. + t = 0;
  53811. + for (j = len - 1; j >= 0; j--) {
  53812. + t += MUL_WORD(a[i], (BignumDblInt) b[j]);
  53813. + t += (BignumDblInt) c[i + j + 1];
  53814. + c[i + j + 1] = (BignumInt) t;
  53815. + t = t >> BIGNUM_INT_BITS;
  53816. + }
  53817. + c[i] = (BignumInt) t;
  53818. + }
  53819. +}
  53820. +
  53821. +static void internal_add_shifted(BignumInt *number,
  53822. + unsigned n, int shift)
  53823. +{
  53824. + int word = 1 + (shift / BIGNUM_INT_BITS);
  53825. + int bshift = shift % BIGNUM_INT_BITS;
  53826. + BignumDblInt addend;
  53827. +
  53828. + addend = (BignumDblInt)n << bshift;
  53829. +
  53830. + while (addend) {
  53831. + addend += number[word];
  53832. + number[word] = (BignumInt) addend & BIGNUM_INT_MASK;
  53833. + addend >>= BIGNUM_INT_BITS;
  53834. + word++;
  53835. + }
  53836. +}
  53837. +
  53838. +/*
  53839. + * Compute a = a % m.
  53840. + * Input in first alen words of a and first mlen words of m.
  53841. + * Output in first alen words of a
  53842. + * (of which first alen-mlen words will be zero).
  53843. + * The MSW of m MUST have its high bit set.
  53844. + * Quotient is accumulated in the `quotient' array, which is a Bignum
  53845. + * rather than the internal bigendian format. Quotient parts are shifted
  53846. + * left by `qshift' before adding into quot.
  53847. + */
  53848. +static void internal_mod(BignumInt *a, int alen,
  53849. + BignumInt *m, int mlen,
  53850. + BignumInt *quot, int qshift)
  53851. +{
  53852. + BignumInt m0, m1;
  53853. + unsigned int h;
  53854. + int i, k;
  53855. +
  53856. + m0 = m[0];
  53857. + if (mlen > 1)
  53858. + m1 = m[1];
  53859. + else
  53860. + m1 = 0;
  53861. +
  53862. + for (i = 0; i <= alen - mlen; i++) {
  53863. + BignumDblInt t;
  53864. + unsigned int q, r, c, ai1;
  53865. +
  53866. + if (i == 0) {
  53867. + h = 0;
  53868. + } else {
  53869. + h = a[i - 1];
  53870. + a[i - 1] = 0;
  53871. + }
  53872. +
  53873. + if (i == alen - 1)
  53874. + ai1 = 0;
  53875. + else
  53876. + ai1 = a[i + 1];
  53877. +
  53878. + /* Find q = h:a[i] / m0 */
  53879. + if (h >= m0) {
  53880. + /*
  53881. + * Special case.
  53882. + *
  53883. + * To illustrate it, suppose a BignumInt is 8 bits, and
  53884. + * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then
  53885. + * our initial division will be 0xA123 / 0xA1, which
  53886. + * will give a quotient of 0x100 and a divide overflow.
  53887. + * However, the invariants in this division algorithm
  53888. + * are not violated, since the full number A1:23:... is
  53889. + * _less_ than the quotient prefix A1:B2:... and so the
  53890. + * following correction loop would have sorted it out.
  53891. + *
  53892. + * In this situation we set q to be the largest
  53893. + * quotient we _can_ stomach (0xFF, of course).
  53894. + */
  53895. + q = BIGNUM_INT_MASK;
  53896. + } else {
  53897. + /* Macro doesn't want an array subscript expression passed
  53898. + * into it (see definition), so use a temporary. */
  53899. + BignumInt tmplo = a[i];
  53900. + DIVMOD_WORD(q, r, h, tmplo, m0);
  53901. +
  53902. + /* Refine our estimate of q by looking at
  53903. + h:a[i]:a[i+1] / m0:m1 */
  53904. + t = MUL_WORD(m1, q);
  53905. + if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
  53906. + q--;
  53907. + t -= m1;
  53908. + r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */
  53909. + if (r >= (BignumDblInt) m0 &&
  53910. + t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--;
  53911. + }
  53912. + }
  53913. +
  53914. + /* Subtract q * m from a[i...] */
  53915. + c = 0;
  53916. + for (k = mlen - 1; k >= 0; k--) {
  53917. + t = MUL_WORD(q, m[k]);
  53918. + t += c;
  53919. + c = (unsigned)(t >> BIGNUM_INT_BITS);
  53920. + if ((BignumInt) t > a[i + k])
  53921. + c++;
  53922. + a[i + k] -= (BignumInt) t;
  53923. + }
  53924. +
  53925. + /* Add back m in case of borrow */
  53926. + if (c != h) {
  53927. + t = 0;
  53928. + for (k = mlen - 1; k >= 0; k--) {
  53929. + t += m[k];
  53930. + t += a[i + k];
  53931. + a[i + k] = (BignumInt) t;
  53932. + t = t >> BIGNUM_INT_BITS;
  53933. + }
  53934. + q--;
  53935. + }
  53936. + if (quot)
  53937. + internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i));
  53938. + }
  53939. +}
  53940. +
  53941. +/*
  53942. + * Compute p % mod.
  53943. + * The most significant word of mod MUST be non-zero.
  53944. + * We assume that the result array is the same size as the mod array.
  53945. + * We optionally write out a quotient if `quotient' is non-NULL.
  53946. + * We can avoid writing out the result if `result' is NULL.
  53947. + */
  53948. +void bigdivmod(void *mem_ctx, Bignum p, Bignum mod, Bignum result, Bignum quotient)
  53949. +{
  53950. + BignumInt *n, *m;
  53951. + int mshift;
  53952. + int plen, mlen, i, j;
  53953. +
  53954. + /* Allocate m of size mlen, copy mod to m */
  53955. + /* We use big endian internally */
  53956. + mlen = mod[0];
  53957. + m = snewn(mem_ctx, mlen, BignumInt);
  53958. + //if (!m)
  53959. + //abort(); /* FIXME */
  53960. + for (j = 0; j < mlen; j++)
  53961. + m[j] = mod[mod[0] - j];
  53962. +
  53963. + /* Shift m left to make msb bit set */
  53964. + for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
  53965. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  53966. + break;
  53967. + if (mshift) {
  53968. + for (i = 0; i < mlen - 1; i++)
  53969. + m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
  53970. + m[mlen - 1] = m[mlen - 1] << mshift;
  53971. + }
  53972. +
  53973. + plen = p[0];
  53974. + /* Ensure plen > mlen */
  53975. + if (plen <= mlen)
  53976. + plen = mlen + 1;
  53977. +
  53978. + /* Allocate n of size plen, copy p to n */
  53979. + n = snewn(mem_ctx, plen, BignumInt);
  53980. + //if (!n)
  53981. + //abort(); /* FIXME */
  53982. + for (j = 0; j < plen; j++)
  53983. + n[j] = 0;
  53984. + for (j = 1; j <= (int)p[0]; j++)
  53985. + n[plen - j] = p[j];
  53986. +
  53987. + /* Main computation */
  53988. + internal_mod(n, plen, m, mlen, quotient, mshift);
  53989. +
  53990. + /* Fixup result in case the modulus was shifted */
  53991. + if (mshift) {
  53992. + for (i = plen - mlen - 1; i < plen - 1; i++)
  53993. + n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift));
  53994. + n[plen - 1] = n[plen - 1] << mshift;
  53995. + internal_mod(n, plen, m, mlen, quotient, 0);
  53996. + for (i = plen - 1; i >= plen - mlen; i--)
  53997. + n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift));
  53998. + }
  53999. +
  54000. + /* Copy result to buffer */
  54001. + if (result) {
  54002. + for (i = 1; i <= (int)result[0]; i++) {
  54003. + int j = plen - i;
  54004. + result[i] = j >= 0 ? n[j] : 0;
  54005. + }
  54006. + }
  54007. +
  54008. + /* Free temporary arrays */
  54009. + for (i = 0; i < mlen; i++)
  54010. + m[i] = 0;
  54011. + sfree(mem_ctx, m);
  54012. + for (i = 0; i < plen; i++)
  54013. + n[i] = 0;
  54014. + sfree(mem_ctx, n);
  54015. +}
  54016. +
  54017. +/*
  54018. + * Simple remainder.
  54019. + */
  54020. +Bignum bigmod(void *mem_ctx, Bignum a, Bignum b)
  54021. +{
  54022. + Bignum r = newbn(mem_ctx, b[0]);
  54023. + bigdivmod(mem_ctx, a, b, r, NULL);
  54024. + return r;
  54025. +}
  54026. +
  54027. +/*
  54028. + * Compute (base ^ exp) % mod.
  54029. + */
  54030. +Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod)
  54031. +{
  54032. + BignumInt *a, *b, *n, *m;
  54033. + int mshift;
  54034. + int mlen, i, j;
  54035. + Bignum base, result;
  54036. +
  54037. + /*
  54038. + * The most significant word of mod needs to be non-zero. It
  54039. + * should already be, but let's make sure.
  54040. + */
  54041. + //assert(mod[mod[0]] != 0);
  54042. +
  54043. + /*
  54044. + * Make sure the base is smaller than the modulus, by reducing
  54045. + * it modulo the modulus if not.
  54046. + */
  54047. + base = bigmod(mem_ctx, base_in, mod);
  54048. +
  54049. + /* Allocate m of size mlen, copy mod to m */
  54050. + /* We use big endian internally */
  54051. + mlen = mod[0];
  54052. + m = snewn(mem_ctx, mlen, BignumInt);
  54053. + //if (!m)
  54054. + //abort(); /* FIXME */
  54055. + for (j = 0; j < mlen; j++)
  54056. + m[j] = mod[mod[0] - j];
  54057. +
  54058. + /* Shift m left to make msb bit set */
  54059. + for (mshift = 0; mshift < BIGNUM_INT_BITS - 1; mshift++)
  54060. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  54061. + break;
  54062. + if (mshift) {
  54063. + for (i = 0; i < mlen - 1; i++)
  54064. + m[i] =
  54065. + (m[i] << mshift) | (m[i + 1] >>
  54066. + (BIGNUM_INT_BITS - mshift));
  54067. + m[mlen - 1] = m[mlen - 1] << mshift;
  54068. + }
  54069. +
  54070. + /* Allocate n of size mlen, copy base to n */
  54071. + n = snewn(mem_ctx, mlen, BignumInt);
  54072. + //if (!n)
  54073. + //abort(); /* FIXME */
  54074. + i = mlen - base[0];
  54075. + for (j = 0; j < i; j++)
  54076. + n[j] = 0;
  54077. + for (j = 0; j < base[0]; j++)
  54078. + n[i + j] = base[base[0] - j];
  54079. +
  54080. + /* Allocate a and b of size 2*mlen. Set a = 1 */
  54081. + a = snewn(mem_ctx, 2 * mlen, BignumInt);
  54082. + //if (!a)
  54083. + //abort(); /* FIXME */
  54084. + b = snewn(mem_ctx, 2 * mlen, BignumInt);
  54085. + //if (!b)
  54086. + //abort(); /* FIXME */
  54087. + for (i = 0; i < 2 * mlen; i++)
  54088. + a[i] = 0;
  54089. + a[2 * mlen - 1] = 1;
  54090. +
  54091. + /* Skip leading zero bits of exp. */
  54092. + i = 0;
  54093. + j = BIGNUM_INT_BITS - 1;
  54094. + while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
  54095. + j--;
  54096. + if (j < 0) {
  54097. + i++;
  54098. + j = BIGNUM_INT_BITS - 1;
  54099. + }
  54100. + }
  54101. +
  54102. + /* Main computation */
  54103. + while (i < exp[0]) {
  54104. + while (j >= 0) {
  54105. + internal_mul(a + mlen, a + mlen, b, mlen);
  54106. + internal_mod(b, mlen * 2, m, mlen, NULL, 0);
  54107. + if ((exp[exp[0] - i] & (1 << j)) != 0) {
  54108. + internal_mul(b + mlen, n, a, mlen);
  54109. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  54110. + } else {
  54111. + BignumInt *t;
  54112. + t = a;
  54113. + a = b;
  54114. + b = t;
  54115. + }
  54116. + j--;
  54117. + }
  54118. + i++;
  54119. + j = BIGNUM_INT_BITS - 1;
  54120. + }
  54121. +
  54122. + /* Fixup result in case the modulus was shifted */
  54123. + if (mshift) {
  54124. + for (i = mlen - 1; i < 2 * mlen - 1; i++)
  54125. + a[i] =
  54126. + (a[i] << mshift) | (a[i + 1] >>
  54127. + (BIGNUM_INT_BITS - mshift));
  54128. + a[2 * mlen - 1] = a[2 * mlen - 1] << mshift;
  54129. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  54130. + for (i = 2 * mlen - 1; i >= mlen; i--)
  54131. + a[i] =
  54132. + (a[i] >> mshift) | (a[i - 1] <<
  54133. + (BIGNUM_INT_BITS - mshift));
  54134. + }
  54135. +
  54136. + /* Copy result to buffer */
  54137. + result = newbn(mem_ctx, mod[0]);
  54138. + for (i = 0; i < mlen; i++)
  54139. + result[result[0] - i] = a[i + mlen];
  54140. + while (result[0] > 1 && result[result[0]] == 0)
  54141. + result[0]--;
  54142. +
  54143. + /* Free temporary arrays */
  54144. + for (i = 0; i < 2 * mlen; i++)
  54145. + a[i] = 0;
  54146. + sfree(mem_ctx, a);
  54147. + for (i = 0; i < 2 * mlen; i++)
  54148. + b[i] = 0;
  54149. + sfree(mem_ctx, b);
  54150. + for (i = 0; i < mlen; i++)
  54151. + m[i] = 0;
  54152. + sfree(mem_ctx, m);
  54153. + for (i = 0; i < mlen; i++)
  54154. + n[i] = 0;
  54155. + sfree(mem_ctx, n);
  54156. +
  54157. + freebn(mem_ctx, base);
  54158. +
  54159. + return result;
  54160. +}
  54161. +
  54162. +
  54163. +#ifdef UNITTEST
  54164. +
  54165. +static __u32 dh_p[] = {
  54166. + 96,
  54167. + 0xFFFFFFFF,
  54168. + 0xFFFFFFFF,
  54169. + 0xA93AD2CA,
  54170. + 0x4B82D120,
  54171. + 0xE0FD108E,
  54172. + 0x43DB5BFC,
  54173. + 0x74E5AB31,
  54174. + 0x08E24FA0,
  54175. + 0xBAD946E2,
  54176. + 0x770988C0,
  54177. + 0x7A615D6C,
  54178. + 0xBBE11757,
  54179. + 0x177B200C,
  54180. + 0x521F2B18,
  54181. + 0x3EC86A64,
  54182. + 0xD8760273,
  54183. + 0xD98A0864,
  54184. + 0xF12FFA06,
  54185. + 0x1AD2EE6B,
  54186. + 0xCEE3D226,
  54187. + 0x4A25619D,
  54188. + 0x1E8C94E0,
  54189. + 0xDB0933D7,
  54190. + 0xABF5AE8C,
  54191. + 0xA6E1E4C7,
  54192. + 0xB3970F85,
  54193. + 0x5D060C7D,
  54194. + 0x8AEA7157,
  54195. + 0x58DBEF0A,
  54196. + 0xECFB8504,
  54197. + 0xDF1CBA64,
  54198. + 0xA85521AB,
  54199. + 0x04507A33,
  54200. + 0xAD33170D,
  54201. + 0x8AAAC42D,
  54202. + 0x15728E5A,
  54203. + 0x98FA0510,
  54204. + 0x15D22618,
  54205. + 0xEA956AE5,
  54206. + 0x3995497C,
  54207. + 0x95581718,
  54208. + 0xDE2BCBF6,
  54209. + 0x6F4C52C9,
  54210. + 0xB5C55DF0,
  54211. + 0xEC07A28F,
  54212. + 0x9B2783A2,
  54213. + 0x180E8603,
  54214. + 0xE39E772C,
  54215. + 0x2E36CE3B,
  54216. + 0x32905E46,
  54217. + 0xCA18217C,
  54218. + 0xF1746C08,
  54219. + 0x4ABC9804,
  54220. + 0x670C354E,
  54221. + 0x7096966D,
  54222. + 0x9ED52907,
  54223. + 0x208552BB,
  54224. + 0x1C62F356,
  54225. + 0xDCA3AD96,
  54226. + 0x83655D23,
  54227. + 0xFD24CF5F,
  54228. + 0x69163FA8,
  54229. + 0x1C55D39A,
  54230. + 0x98DA4836,
  54231. + 0xA163BF05,
  54232. + 0xC2007CB8,
  54233. + 0xECE45B3D,
  54234. + 0x49286651,
  54235. + 0x7C4B1FE6,
  54236. + 0xAE9F2411,
  54237. + 0x5A899FA5,
  54238. + 0xEE386BFB,
  54239. + 0xF406B7ED,
  54240. + 0x0BFF5CB6,
  54241. + 0xA637ED6B,
  54242. + 0xF44C42E9,
  54243. + 0x625E7EC6,
  54244. + 0xE485B576,
  54245. + 0x6D51C245,
  54246. + 0x4FE1356D,
  54247. + 0xF25F1437,
  54248. + 0x302B0A6D,
  54249. + 0xCD3A431B,
  54250. + 0xEF9519B3,
  54251. + 0x8E3404DD,
  54252. + 0x514A0879,
  54253. + 0x3B139B22,
  54254. + 0x020BBEA6,
  54255. + 0x8A67CC74,
  54256. + 0x29024E08,
  54257. + 0x80DC1CD1,
  54258. + 0xC4C6628B,
  54259. + 0x2168C234,
  54260. + 0xC90FDAA2,
  54261. + 0xFFFFFFFF,
  54262. + 0xFFFFFFFF,
  54263. +};
  54264. +
  54265. +static __u32 dh_a[] = {
  54266. + 8,
  54267. + 0xdf367516,
  54268. + 0x86459caa,
  54269. + 0xe2d459a4,
  54270. + 0xd910dae0,
  54271. + 0x8a8b5e37,
  54272. + 0x67ab31c6,
  54273. + 0xf0b55ea9,
  54274. + 0x440051d6,
  54275. +};
  54276. +
  54277. +static __u32 dh_b[] = {
  54278. + 8,
  54279. + 0xded92656,
  54280. + 0xe07a048a,
  54281. + 0x6fa452cd,
  54282. + 0x2df89d30,
  54283. + 0xc75f1b0f,
  54284. + 0x8ce3578f,
  54285. + 0x7980a324,
  54286. + 0x5daec786,
  54287. +};
  54288. +
  54289. +static __u32 dh_g[] = {
  54290. + 1,
  54291. + 2,
  54292. +};
  54293. +
  54294. +int main(void)
  54295. +{
  54296. + int i;
  54297. + __u32 *k;
  54298. + k = dwc_modpow(NULL, dh_g, dh_a, dh_p);
  54299. +
  54300. + printf("\n\n");
  54301. + for (i=0; i<k[0]; i++) {
  54302. + __u32 word32 = k[k[0] - i];
  54303. + __u16 l = word32 & 0xffff;
  54304. + __u16 m = (word32 & 0xffff0000) >> 16;
  54305. + printf("%04x %04x ", m, l);
  54306. + if (!((i + 1)%13)) printf("\n");
  54307. + }
  54308. + printf("\n\n");
  54309. +
  54310. + if ((k[0] == 0x60) && (k[1] == 0x28e490e5) && (k[0x60] == 0x5a0d3d4e)) {
  54311. + printf("PASS\n\n");
  54312. + }
  54313. + else {
  54314. + printf("FAIL\n\n");
  54315. + }
  54316. +
  54317. +}
  54318. +
  54319. +#endif /* UNITTEST */
  54320. +
  54321. +#endif /* CONFIG_MACH_IPMATE */
  54322. +
  54323. +#endif /*DWC_CRYPTOLIB */
  54324. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_modpow.h linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_modpow.h
  54325. --- linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_modpow.h 1969-12-31 18:00:00.000000000 -0600
  54326. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_modpow.h 2014-12-03 19:13:40.216418001 -0600
  54327. @@ -0,0 +1,34 @@
  54328. +/*
  54329. + * dwc_modpow.h
  54330. + * See dwc_modpow.c for license and changes
  54331. + */
  54332. +#ifndef _DWC_MODPOW_H
  54333. +#define _DWC_MODPOW_H
  54334. +
  54335. +#ifdef __cplusplus
  54336. +extern "C" {
  54337. +#endif
  54338. +
  54339. +#include "dwc_os.h"
  54340. +
  54341. +/** @file
  54342. + *
  54343. + * This file defines the module exponentiation function which is only used
  54344. + * internally by the DWC UWB modules for calculation of PKs during numeric
  54345. + * association. The routine is taken from the PUTTY, an open source terminal
  54346. + * emulator. The PUTTY License is preserved in the dwc_modpow.c file.
  54347. + *
  54348. + */
  54349. +
  54350. +typedef uint32_t BignumInt;
  54351. +typedef uint64_t BignumDblInt;
  54352. +typedef BignumInt *Bignum;
  54353. +
  54354. +/* Compute modular exponentiaion */
  54355. +extern Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod);
  54356. +
  54357. +#ifdef __cplusplus
  54358. +}
  54359. +#endif
  54360. +
  54361. +#endif /* _LINUX_BIGNUM_H */
  54362. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_notifier.c linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_notifier.c
  54363. --- linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_notifier.c 1969-12-31 18:00:00.000000000 -0600
  54364. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_notifier.c 2014-12-03 19:13:40.216418001 -0600
  54365. @@ -0,0 +1,319 @@
  54366. +#ifdef DWC_NOTIFYLIB
  54367. +
  54368. +#include "dwc_notifier.h"
  54369. +#include "dwc_list.h"
  54370. +
  54371. +typedef struct dwc_observer {
  54372. + void *observer;
  54373. + dwc_notifier_callback_t callback;
  54374. + void *data;
  54375. + char *notification;
  54376. + DWC_CIRCLEQ_ENTRY(dwc_observer) list_entry;
  54377. +} observer_t;
  54378. +
  54379. +DWC_CIRCLEQ_HEAD(observer_queue, dwc_observer);
  54380. +
  54381. +typedef struct dwc_notifier {
  54382. + void *mem_ctx;
  54383. + void *object;
  54384. + struct observer_queue observers;
  54385. + DWC_CIRCLEQ_ENTRY(dwc_notifier) list_entry;
  54386. +} notifier_t;
  54387. +
  54388. +DWC_CIRCLEQ_HEAD(notifier_queue, dwc_notifier);
  54389. +
  54390. +typedef struct manager {
  54391. + void *mem_ctx;
  54392. + void *wkq_ctx;
  54393. + dwc_workq_t *wq;
  54394. +// dwc_mutex_t *mutex;
  54395. + struct notifier_queue notifiers;
  54396. +} manager_t;
  54397. +
  54398. +static manager_t *manager = NULL;
  54399. +
  54400. +static int create_manager(void *mem_ctx, void *wkq_ctx)
  54401. +{
  54402. + manager = dwc_alloc(mem_ctx, sizeof(manager_t));
  54403. + if (!manager) {
  54404. + return -DWC_E_NO_MEMORY;
  54405. + }
  54406. +
  54407. + DWC_CIRCLEQ_INIT(&manager->notifiers);
  54408. +
  54409. + manager->wq = dwc_workq_alloc(wkq_ctx, "DWC Notification WorkQ");
  54410. + if (!manager->wq) {
  54411. + return -DWC_E_NO_MEMORY;
  54412. + }
  54413. +
  54414. + return 0;
  54415. +}
  54416. +
  54417. +static void free_manager(void)
  54418. +{
  54419. + dwc_workq_free(manager->wq);
  54420. +
  54421. + /* All notifiers must have unregistered themselves before this module
  54422. + * can be removed. Hitting this assertion indicates a programmer
  54423. + * error. */
  54424. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&manager->notifiers),
  54425. + "Notification manager being freed before all notifiers have been removed");
  54426. + dwc_free(manager->mem_ctx, manager);
  54427. +}
  54428. +
  54429. +#ifdef DEBUG
  54430. +static void dump_manager(void)
  54431. +{
  54432. + notifier_t *n;
  54433. + observer_t *o;
  54434. +
  54435. + DWC_ASSERT(manager, "Notification manager not found");
  54436. +
  54437. + DWC_DEBUG("List of all notifiers and observers:\n");
  54438. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  54439. + DWC_DEBUG("Notifier %p has observers:\n", n->object);
  54440. + DWC_CIRCLEQ_FOREACH(o, &n->observers, list_entry) {
  54441. + DWC_DEBUG(" %p watching %s\n", o->observer, o->notification);
  54442. + }
  54443. + }
  54444. +}
  54445. +#else
  54446. +#define dump_manager(...)
  54447. +#endif
  54448. +
  54449. +static observer_t *alloc_observer(void *mem_ctx, void *observer, char *notification,
  54450. + dwc_notifier_callback_t callback, void *data)
  54451. +{
  54452. + observer_t *new_observer = dwc_alloc(mem_ctx, sizeof(observer_t));
  54453. +
  54454. + if (!new_observer) {
  54455. + return NULL;
  54456. + }
  54457. +
  54458. + DWC_CIRCLEQ_INIT_ENTRY(new_observer, list_entry);
  54459. + new_observer->observer = observer;
  54460. + new_observer->notification = notification;
  54461. + new_observer->callback = callback;
  54462. + new_observer->data = data;
  54463. + return new_observer;
  54464. +}
  54465. +
  54466. +static void free_observer(void *mem_ctx, observer_t *observer)
  54467. +{
  54468. + dwc_free(mem_ctx, observer);
  54469. +}
  54470. +
  54471. +static notifier_t *alloc_notifier(void *mem_ctx, void *object)
  54472. +{
  54473. + notifier_t *notifier;
  54474. +
  54475. + if (!object) {
  54476. + return NULL;
  54477. + }
  54478. +
  54479. + notifier = dwc_alloc(mem_ctx, sizeof(notifier_t));
  54480. + if (!notifier) {
  54481. + return NULL;
  54482. + }
  54483. +
  54484. + DWC_CIRCLEQ_INIT(&notifier->observers);
  54485. + DWC_CIRCLEQ_INIT_ENTRY(notifier, list_entry);
  54486. +
  54487. + notifier->mem_ctx = mem_ctx;
  54488. + notifier->object = object;
  54489. + return notifier;
  54490. +}
  54491. +
  54492. +static void free_notifier(notifier_t *notifier)
  54493. +{
  54494. + observer_t *observer;
  54495. +
  54496. + DWC_CIRCLEQ_FOREACH(observer, &notifier->observers, list_entry) {
  54497. + free_observer(notifier->mem_ctx, observer);
  54498. + }
  54499. +
  54500. + dwc_free(notifier->mem_ctx, notifier);
  54501. +}
  54502. +
  54503. +static notifier_t *find_notifier(void *object)
  54504. +{
  54505. + notifier_t *notifier;
  54506. +
  54507. + DWC_ASSERT(manager, "Notification manager not found");
  54508. +
  54509. + if (!object) {
  54510. + return NULL;
  54511. + }
  54512. +
  54513. + DWC_CIRCLEQ_FOREACH(notifier, &manager->notifiers, list_entry) {
  54514. + if (notifier->object == object) {
  54515. + return notifier;
  54516. + }
  54517. + }
  54518. +
  54519. + return NULL;
  54520. +}
  54521. +
  54522. +int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx)
  54523. +{
  54524. + return create_manager(mem_ctx, wkq_ctx);
  54525. +}
  54526. +
  54527. +void dwc_free_notification_manager(void)
  54528. +{
  54529. + free_manager();
  54530. +}
  54531. +
  54532. +dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object)
  54533. +{
  54534. + notifier_t *notifier;
  54535. +
  54536. + DWC_ASSERT(manager, "Notification manager not found");
  54537. +
  54538. + notifier = find_notifier(object);
  54539. + if (notifier) {
  54540. + DWC_ERROR("Notifier %p is already registered\n", object);
  54541. + return NULL;
  54542. + }
  54543. +
  54544. + notifier = alloc_notifier(mem_ctx, object);
  54545. + if (!notifier) {
  54546. + return NULL;
  54547. + }
  54548. +
  54549. + DWC_CIRCLEQ_INSERT_TAIL(&manager->notifiers, notifier, list_entry);
  54550. +
  54551. + DWC_INFO("Notifier %p registered", object);
  54552. + dump_manager();
  54553. +
  54554. + return notifier;
  54555. +}
  54556. +
  54557. +void dwc_unregister_notifier(dwc_notifier_t *notifier)
  54558. +{
  54559. + DWC_ASSERT(manager, "Notification manager not found");
  54560. +
  54561. + if (!DWC_CIRCLEQ_EMPTY(&notifier->observers)) {
  54562. + observer_t *o;
  54563. +
  54564. + DWC_ERROR("Notifier %p has active observers when removing\n", notifier->object);
  54565. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  54566. + DWC_DEBUGC(" %p watching %s\n", o->observer, o->notification);
  54567. + }
  54568. +
  54569. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&notifier->observers),
  54570. + "Notifier %p has active observers when removing", notifier);
  54571. + }
  54572. +
  54573. + DWC_CIRCLEQ_REMOVE_INIT(&manager->notifiers, notifier, list_entry);
  54574. + free_notifier(notifier);
  54575. +
  54576. + DWC_INFO("Notifier unregistered");
  54577. + dump_manager();
  54578. +}
  54579. +
  54580. +/* Add an observer to observe the notifier for a particular state, event, or notification. */
  54581. +int dwc_add_observer(void *observer, void *object, char *notification,
  54582. + dwc_notifier_callback_t callback, void *data)
  54583. +{
  54584. + notifier_t *notifier = find_notifier(object);
  54585. + observer_t *new_observer;
  54586. +
  54587. + if (!notifier) {
  54588. + DWC_ERROR("Notifier %p is not found when adding observer\n", object);
  54589. + return -DWC_E_INVALID;
  54590. + }
  54591. +
  54592. + new_observer = alloc_observer(notifier->mem_ctx, observer, notification, callback, data);
  54593. + if (!new_observer) {
  54594. + return -DWC_E_NO_MEMORY;
  54595. + }
  54596. +
  54597. + DWC_CIRCLEQ_INSERT_TAIL(&notifier->observers, new_observer, list_entry);
  54598. +
  54599. + DWC_INFO("Added observer %p to notifier %p observing notification %s, callback=%p, data=%p",
  54600. + observer, object, notification, callback, data);
  54601. +
  54602. + dump_manager();
  54603. + return 0;
  54604. +}
  54605. +
  54606. +int dwc_remove_observer(void *observer)
  54607. +{
  54608. + notifier_t *n;
  54609. +
  54610. + DWC_ASSERT(manager, "Notification manager not found");
  54611. +
  54612. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  54613. + observer_t *o;
  54614. + observer_t *o2;
  54615. +
  54616. + DWC_CIRCLEQ_FOREACH_SAFE(o, o2, &n->observers, list_entry) {
  54617. + if (o->observer == observer) {
  54618. + DWC_CIRCLEQ_REMOVE_INIT(&n->observers, o, list_entry);
  54619. + DWC_INFO("Removing observer %p from notifier %p watching notification %s:",
  54620. + o->observer, n->object, o->notification);
  54621. + free_observer(n->mem_ctx, o);
  54622. + }
  54623. + }
  54624. + }
  54625. +
  54626. + dump_manager();
  54627. + return 0;
  54628. +}
  54629. +
  54630. +typedef struct callback_data {
  54631. + void *mem_ctx;
  54632. + dwc_notifier_callback_t cb;
  54633. + void *observer;
  54634. + void *data;
  54635. + void *object;
  54636. + char *notification;
  54637. + void *notification_data;
  54638. +} cb_data_t;
  54639. +
  54640. +static void cb_task(void *data)
  54641. +{
  54642. + cb_data_t *cb = (cb_data_t *)data;
  54643. +
  54644. + cb->cb(cb->object, cb->notification, cb->observer, cb->notification_data, cb->data);
  54645. + dwc_free(cb->mem_ctx, cb);
  54646. +}
  54647. +
  54648. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data)
  54649. +{
  54650. + observer_t *o;
  54651. +
  54652. + DWC_ASSERT(manager, "Notification manager not found");
  54653. +
  54654. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  54655. + int len = DWC_STRLEN(notification);
  54656. +
  54657. + if (DWC_STRLEN(o->notification) != len) {
  54658. + continue;
  54659. + }
  54660. +
  54661. + if (DWC_STRNCMP(o->notification, notification, len) == 0) {
  54662. + cb_data_t *cb_data = dwc_alloc(notifier->mem_ctx, sizeof(cb_data_t));
  54663. +
  54664. + if (!cb_data) {
  54665. + DWC_ERROR("Failed to allocate callback data\n");
  54666. + return;
  54667. + }
  54668. +
  54669. + cb_data->mem_ctx = notifier->mem_ctx;
  54670. + cb_data->cb = o->callback;
  54671. + cb_data->observer = o->observer;
  54672. + cb_data->data = o->data;
  54673. + cb_data->object = notifier->object;
  54674. + cb_data->notification = notification;
  54675. + cb_data->notification_data = notification_data;
  54676. + DWC_DEBUGC("Observer found %p for notification %s\n", o->observer, notification);
  54677. + DWC_WORKQ_SCHEDULE(manager->wq, cb_task, cb_data,
  54678. + "Notify callback from %p for Notification %s, to observer %p",
  54679. + cb_data->object, notification, cb_data->observer);
  54680. + }
  54681. + }
  54682. +}
  54683. +
  54684. +#endif /* DWC_NOTIFYLIB */
  54685. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_notifier.h linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_notifier.h
  54686. --- linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_notifier.h 1969-12-31 18:00:00.000000000 -0600
  54687. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_notifier.h 2014-12-03 19:13:40.216418001 -0600
  54688. @@ -0,0 +1,122 @@
  54689. +
  54690. +#ifndef __DWC_NOTIFIER_H__
  54691. +#define __DWC_NOTIFIER_H__
  54692. +
  54693. +#ifdef __cplusplus
  54694. +extern "C" {
  54695. +#endif
  54696. +
  54697. +#include "dwc_os.h"
  54698. +
  54699. +/** @file
  54700. + *
  54701. + * A simple implementation of the Observer pattern. Any "module" can
  54702. + * register as an observer or notifier. The notion of "module" is abstract and
  54703. + * can mean anything used to identify either an observer or notifier. Usually
  54704. + * it will be a pointer to a data structure which contains some state, ie an
  54705. + * object.
  54706. + *
  54707. + * Before any notifiers can be added, the global notification manager must be
  54708. + * brought up with dwc_alloc_notification_manager().
  54709. + * dwc_free_notification_manager() will bring it down and free all resources.
  54710. + * These would typically be called upon module load and unload. The
  54711. + * notification manager is a single global instance that handles all registered
  54712. + * observable modules and observers so this should be done only once.
  54713. + *
  54714. + * A module can be observable by using Notifications to publicize some general
  54715. + * information about it's state or operation. It does not care who listens, or
  54716. + * even if anyone listens, or what they do with the information. The observable
  54717. + * modules do not need to know any information about it's observers or their
  54718. + * interface, or their state or data.
  54719. + *
  54720. + * Any module can register to emit Notifications. It should publish a list of
  54721. + * notifications that it can emit and their behavior, such as when they will get
  54722. + * triggered, and what information will be provided to the observer. Then it
  54723. + * should register itself as an observable module. See dwc_register_notifier().
  54724. + *
  54725. + * Any module can observe any observable, registered module, provided it has a
  54726. + * handle to the other module and knows what notifications to observe. See
  54727. + * dwc_add_observer().
  54728. + *
  54729. + * A function of type dwc_notifier_callback_t is called whenever a notification
  54730. + * is triggered with one or more observers observing it. This function is
  54731. + * called in it's own process so it may sleep or block if needed. It is
  54732. + * guaranteed to be called sometime after the notification has occurred and will
  54733. + * be called once per each time the notification is triggered. It will NOT be
  54734. + * called in the same process context used to trigger the notification.
  54735. + *
  54736. + * @section Limitiations
  54737. + *
  54738. + * Keep in mind that Notifications that can be triggered in rapid sucession may
  54739. + * schedule too many processes too handle. Be aware of this limitation when
  54740. + * designing to use notifications, and only add notifications for appropriate
  54741. + * observable information.
  54742. + *
  54743. + * Also Notification callbacks are not synchronous. If you need to synchronize
  54744. + * the behavior between module/observer you must use other means. And perhaps
  54745. + * that will mean Notifications are not the proper solution.
  54746. + */
  54747. +
  54748. +struct dwc_notifier;
  54749. +typedef struct dwc_notifier dwc_notifier_t;
  54750. +
  54751. +/** The callback function must be of this type.
  54752. + *
  54753. + * @param object This is the object that is being observed.
  54754. + * @param notification This is the notification that was triggered.
  54755. + * @param observer This is the observer
  54756. + * @param notification_data This is notification-specific data that the notifier
  54757. + * has included in this notification. The value of this should be published in
  54758. + * the documentation of the observable module with the notifications.
  54759. + * @param user_data This is any custom data that the observer provided when
  54760. + * adding itself as an observer to the notification. */
  54761. +typedef void (*dwc_notifier_callback_t)(void *object, char *notification, void *observer,
  54762. + void *notification_data, void *user_data);
  54763. +
  54764. +/** Brings up the notification manager. */
  54765. +extern int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx);
  54766. +/** Brings down the notification manager. */
  54767. +extern void dwc_free_notification_manager(void);
  54768. +
  54769. +/** This function registers an observable module. A dwc_notifier_t object is
  54770. + * returned to the observable module. This is an opaque object that is used by
  54771. + * the observable module to trigger notifications. This object should only be
  54772. + * accessible to functions that are authorized to trigger notifications for this
  54773. + * module. Observers do not need this object. */
  54774. +extern dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object);
  54775. +
  54776. +/** This function unregisters an observable module. All observers have to be
  54777. + * removed prior to unregistration. */
  54778. +extern void dwc_unregister_notifier(dwc_notifier_t *notifier);
  54779. +
  54780. +/** Add a module as an observer to the observable module. The observable module
  54781. + * needs to have previously registered with the notification manager.
  54782. + *
  54783. + * @param observer The observer module
  54784. + * @param object The module to observe
  54785. + * @param notification The notification to observe
  54786. + * @param callback The callback function to call
  54787. + * @param user_data Any additional user data to pass into the callback function */
  54788. +extern int dwc_add_observer(void *observer, void *object, char *notification,
  54789. + dwc_notifier_callback_t callback, void *user_data);
  54790. +
  54791. +/** Removes the specified observer from all notifications that it is currently
  54792. + * observing. */
  54793. +extern int dwc_remove_observer(void *observer);
  54794. +
  54795. +/** This function triggers a Notification. It should be called by the
  54796. + * observable module, or any module or library which the observable module
  54797. + * allows to trigger notification on it's behalf. Such as the dwc_cc_t.
  54798. + *
  54799. + * dwc_notify is a non-blocking function. Callbacks are scheduled called in
  54800. + * their own process context for each trigger. Callbacks can be blocking.
  54801. + * dwc_notify can be called from interrupt context if needed.
  54802. + *
  54803. + */
  54804. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data);
  54805. +
  54806. +#ifdef __cplusplus
  54807. +}
  54808. +#endif
  54809. +
  54810. +#endif /* __DWC_NOTIFIER_H__ */
  54811. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_os.h linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_os.h
  54812. --- linux-3.12.33/drivers/usb/host/dwc_common_port/dwc_os.h 1969-12-31 18:00:00.000000000 -0600
  54813. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/dwc_os.h 2014-12-03 19:13:40.216418001 -0600
  54814. @@ -0,0 +1,1262 @@
  54815. +/* =========================================================================
  54816. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_os.h $
  54817. + * $Revision: #14 $
  54818. + * $Date: 2010/11/04 $
  54819. + * $Change: 1621695 $
  54820. + *
  54821. + * Synopsys Portability Library Software and documentation
  54822. + * (hereinafter, "Software") is an Unsupported proprietary work of
  54823. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  54824. + * between Synopsys and you.
  54825. + *
  54826. + * The Software IS NOT an item of Licensed Software or Licensed Product
  54827. + * under any End User Software License Agreement or Agreement for
  54828. + * Licensed Product with Synopsys or any supplement thereto. You are
  54829. + * permitted to use and redistribute this Software in source and binary
  54830. + * forms, with or without modification, provided that redistributions
  54831. + * of source code must retain this notice. You may not view, use,
  54832. + * disclose, copy or distribute this file or any information contained
  54833. + * herein except pursuant to this license grant from Synopsys. If you
  54834. + * do not agree with this notice, including the disclaimer below, then
  54835. + * you are not authorized to use the Software.
  54836. + *
  54837. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  54838. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54839. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  54840. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  54841. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  54842. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  54843. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  54844. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  54845. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  54846. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  54847. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  54848. + * DAMAGE.
  54849. + * ========================================================================= */
  54850. +#ifndef _DWC_OS_H_
  54851. +#define _DWC_OS_H_
  54852. +
  54853. +#ifdef __cplusplus
  54854. +extern "C" {
  54855. +#endif
  54856. +
  54857. +/** @file
  54858. + *
  54859. + * DWC portability library, low level os-wrapper functions
  54860. + *
  54861. + */
  54862. +
  54863. +/* These basic types need to be defined by some OS header file or custom header
  54864. + * file for your specific target architecture.
  54865. + *
  54866. + * uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t
  54867. + *
  54868. + * Any custom or alternate header file must be added and enabled here.
  54869. + */
  54870. +
  54871. +#ifdef DWC_LINUX
  54872. +# include <linux/types.h>
  54873. +# ifdef CONFIG_DEBUG_MUTEXES
  54874. +# include <linux/mutex.h>
  54875. +# endif
  54876. +# include <linux/errno.h>
  54877. +# include <stdarg.h>
  54878. +#endif
  54879. +
  54880. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  54881. +# include <os_dep.h>
  54882. +#endif
  54883. +
  54884. +
  54885. +/** @name Primitive Types and Values */
  54886. +
  54887. +/** We define a boolean type for consistency. Can be either YES or NO */
  54888. +typedef uint8_t dwc_bool_t;
  54889. +#define YES 1
  54890. +#define NO 0
  54891. +
  54892. +#ifdef DWC_LINUX
  54893. +
  54894. +/** @name Error Codes */
  54895. +#define DWC_E_INVALID EINVAL
  54896. +#define DWC_E_NO_MEMORY ENOMEM
  54897. +#define DWC_E_NO_DEVICE ENODEV
  54898. +#define DWC_E_NOT_SUPPORTED EOPNOTSUPP
  54899. +#define DWC_E_TIMEOUT ETIMEDOUT
  54900. +#define DWC_E_BUSY EBUSY
  54901. +#define DWC_E_AGAIN EAGAIN
  54902. +#define DWC_E_RESTART ERESTART
  54903. +#define DWC_E_ABORT ECONNABORTED
  54904. +#define DWC_E_SHUTDOWN ESHUTDOWN
  54905. +#define DWC_E_NO_DATA ENODATA
  54906. +#define DWC_E_DISCONNECT ECONNRESET
  54907. +#define DWC_E_UNKNOWN EINVAL
  54908. +#define DWC_E_NO_STREAM_RES ENOSR
  54909. +#define DWC_E_COMMUNICATION ECOMM
  54910. +#define DWC_E_OVERFLOW EOVERFLOW
  54911. +#define DWC_E_PROTOCOL EPROTO
  54912. +#define DWC_E_IN_PROGRESS EINPROGRESS
  54913. +#define DWC_E_PIPE EPIPE
  54914. +#define DWC_E_IO EIO
  54915. +#define DWC_E_NO_SPACE ENOSPC
  54916. +
  54917. +#else
  54918. +
  54919. +/** @name Error Codes */
  54920. +#define DWC_E_INVALID 1001
  54921. +#define DWC_E_NO_MEMORY 1002
  54922. +#define DWC_E_NO_DEVICE 1003
  54923. +#define DWC_E_NOT_SUPPORTED 1004
  54924. +#define DWC_E_TIMEOUT 1005
  54925. +#define DWC_E_BUSY 1006
  54926. +#define DWC_E_AGAIN 1007
  54927. +#define DWC_E_RESTART 1008
  54928. +#define DWC_E_ABORT 1009
  54929. +#define DWC_E_SHUTDOWN 1010
  54930. +#define DWC_E_NO_DATA 1011
  54931. +#define DWC_E_DISCONNECT 2000
  54932. +#define DWC_E_UNKNOWN 3000
  54933. +#define DWC_E_NO_STREAM_RES 4001
  54934. +#define DWC_E_COMMUNICATION 4002
  54935. +#define DWC_E_OVERFLOW 4003
  54936. +#define DWC_E_PROTOCOL 4004
  54937. +#define DWC_E_IN_PROGRESS 4005
  54938. +#define DWC_E_PIPE 4006
  54939. +#define DWC_E_IO 4007
  54940. +#define DWC_E_NO_SPACE 4008
  54941. +
  54942. +#endif
  54943. +
  54944. +
  54945. +/** @name Tracing/Logging Functions
  54946. + *
  54947. + * These function provide the capability to add tracing, debugging, and error
  54948. + * messages, as well exceptions as assertions. The WUDEV uses these
  54949. + * extensively. These could be logged to the main console, the serial port, an
  54950. + * internal buffer, etc. These functions could also be no-op if they are too
  54951. + * expensive on your system. By default undefining the DEBUG macro already
  54952. + * no-ops some of these functions. */
  54953. +
  54954. +/** Returns non-zero if in interrupt context. */
  54955. +extern dwc_bool_t DWC_IN_IRQ(void);
  54956. +#define dwc_in_irq DWC_IN_IRQ
  54957. +
  54958. +/** Returns "IRQ" if DWC_IN_IRQ is true. */
  54959. +static inline char *dwc_irq(void) {
  54960. + return DWC_IN_IRQ() ? "IRQ" : "";
  54961. +}
  54962. +
  54963. +/** Returns non-zero if in bottom-half context. */
  54964. +extern dwc_bool_t DWC_IN_BH(void);
  54965. +#define dwc_in_bh DWC_IN_BH
  54966. +
  54967. +/** Returns "BH" if DWC_IN_BH is true. */
  54968. +static inline char *dwc_bh(void) {
  54969. + return DWC_IN_BH() ? "BH" : "";
  54970. +}
  54971. +
  54972. +/**
  54973. + * A vprintf() clone. Just call vprintf if you've got it.
  54974. + */
  54975. +extern void DWC_VPRINTF(char *format, va_list args);
  54976. +#define dwc_vprintf DWC_VPRINTF
  54977. +
  54978. +/**
  54979. + * A vsnprintf() clone. Just call vprintf if you've got it.
  54980. + */
  54981. +extern int DWC_VSNPRINTF(char *str, int size, char *format, va_list args);
  54982. +#define dwc_vsnprintf DWC_VSNPRINTF
  54983. +
  54984. +/**
  54985. + * printf() clone. Just call printf if you've go it.
  54986. + */
  54987. +extern void DWC_PRINTF(char *format, ...)
  54988. +/* This provides compiler level static checking of the parameters if you're
  54989. + * using GCC. */
  54990. +#ifdef __GNUC__
  54991. + __attribute__ ((format(printf, 1, 2)));
  54992. +#else
  54993. + ;
  54994. +#endif
  54995. +#define dwc_printf DWC_PRINTF
  54996. +
  54997. +/**
  54998. + * sprintf() clone. Just call sprintf if you've got it.
  54999. + */
  55000. +extern int DWC_SPRINTF(char *string, char *format, ...)
  55001. +#ifdef __GNUC__
  55002. + __attribute__ ((format(printf, 2, 3)));
  55003. +#else
  55004. + ;
  55005. +#endif
  55006. +#define dwc_sprintf DWC_SPRINTF
  55007. +
  55008. +/**
  55009. + * snprintf() clone. Just call snprintf if you've got it.
  55010. + */
  55011. +extern int DWC_SNPRINTF(char *string, int size, char *format, ...)
  55012. +#ifdef __GNUC__
  55013. + __attribute__ ((format(printf, 3, 4)));
  55014. +#else
  55015. + ;
  55016. +#endif
  55017. +#define dwc_snprintf DWC_SNPRINTF
  55018. +
  55019. +/**
  55020. + * Prints a WARNING message. On systems that don't differentiate between
  55021. + * warnings and regular log messages, just print it. Indicates that something
  55022. + * may be wrong with the driver. Works like printf().
  55023. + *
  55024. + * Use the DWC_WARN macro to call this function.
  55025. + */
  55026. +extern void __DWC_WARN(char *format, ...)
  55027. +#ifdef __GNUC__
  55028. + __attribute__ ((format(printf, 1, 2)));
  55029. +#else
  55030. + ;
  55031. +#endif
  55032. +
  55033. +/**
  55034. + * Prints an error message. On systems that don't differentiate between errors
  55035. + * and regular log messages, just print it. Indicates that something went wrong
  55036. + * with the driver. Works like printf().
  55037. + *
  55038. + * Use the DWC_ERROR macro to call this function.
  55039. + */
  55040. +extern void __DWC_ERROR(char *format, ...)
  55041. +#ifdef __GNUC__
  55042. + __attribute__ ((format(printf, 1, 2)));
  55043. +#else
  55044. + ;
  55045. +#endif
  55046. +
  55047. +/**
  55048. + * Prints an exception error message and takes some user-defined action such as
  55049. + * print out a backtrace or trigger a breakpoint. Indicates that something went
  55050. + * abnormally wrong with the driver such as programmer error, or other
  55051. + * exceptional condition. It should not be ignored so even on systems without
  55052. + * printing capability, some action should be taken to notify the developer of
  55053. + * it. Works like printf().
  55054. + */
  55055. +extern void DWC_EXCEPTION(char *format, ...)
  55056. +#ifdef __GNUC__
  55057. + __attribute__ ((format(printf, 1, 2)));
  55058. +#else
  55059. + ;
  55060. +#endif
  55061. +#define dwc_exception DWC_EXCEPTION
  55062. +
  55063. +#ifndef DWC_OTG_DEBUG_LEV
  55064. +#define DWC_OTG_DEBUG_LEV 0
  55065. +#endif
  55066. +
  55067. +#ifdef DEBUG
  55068. +/**
  55069. + * Prints out a debug message. Used for logging/trace messages.
  55070. + *
  55071. + * Use the DWC_DEBUG macro to call this function
  55072. + */
  55073. +extern void __DWC_DEBUG(char *format, ...)
  55074. +#ifdef __GNUC__
  55075. + __attribute__ ((format(printf, 1, 2)));
  55076. +#else
  55077. + ;
  55078. +#endif
  55079. +#else
  55080. +#define __DWC_DEBUG printk
  55081. +#endif
  55082. +
  55083. +/**
  55084. + * Prints out a Debug message.
  55085. + */
  55086. +#define DWC_DEBUG(_format, _args...) __DWC_DEBUG("DEBUG:%s:%s: " _format "\n", \
  55087. + __func__, dwc_irq(), ## _args)
  55088. +#define dwc_debug DWC_DEBUG
  55089. +/**
  55090. + * Prints out a Debug message if enabled at compile time.
  55091. + */
  55092. +#if DWC_OTG_DEBUG_LEV > 0
  55093. +#define DWC_DEBUGC(_format, _args...) DWC_DEBUG(_format, ##_args )
  55094. +#else
  55095. +#define DWC_DEBUGC(_format, _args...)
  55096. +#endif
  55097. +#define dwc_debugc DWC_DEBUGC
  55098. +/**
  55099. + * Prints out an informative message.
  55100. + */
  55101. +#define DWC_INFO(_format, _args...) DWC_PRINTF("INFO:%s: " _format "\n", \
  55102. + dwc_irq(), ## _args)
  55103. +#define dwc_info DWC_INFO
  55104. +/**
  55105. + * Prints out an informative message if enabled at compile time.
  55106. + */
  55107. +#if DWC_OTG_DEBUG_LEV > 1
  55108. +#define DWC_INFOC(_format, _args...) DWC_INFO(_format, ##_args )
  55109. +#else
  55110. +#define DWC_INFOC(_format, _args...)
  55111. +#endif
  55112. +#define dwc_infoc DWC_INFOC
  55113. +/**
  55114. + * Prints out a warning message.
  55115. + */
  55116. +#define DWC_WARN(_format, _args...) __DWC_WARN("WARN:%s:%s:%d: " _format "\n", \
  55117. + dwc_irq(), __func__, __LINE__, ## _args)
  55118. +#define dwc_warn DWC_WARN
  55119. +/**
  55120. + * Prints out an error message.
  55121. + */
  55122. +#define DWC_ERROR(_format, _args...) __DWC_ERROR("ERROR:%s:%s:%d: " _format "\n", \
  55123. + dwc_irq(), __func__, __LINE__, ## _args)
  55124. +#define dwc_error DWC_ERROR
  55125. +
  55126. +#define DWC_PROTO_ERROR(_format, _args...) __DWC_WARN("ERROR:%s:%s:%d: " _format "\n", \
  55127. + dwc_irq(), __func__, __LINE__, ## _args)
  55128. +#define dwc_proto_error DWC_PROTO_ERROR
  55129. +
  55130. +#ifdef DEBUG
  55131. +/** Prints out a exception error message if the _expr expression fails. Disabled
  55132. + * if DEBUG is not enabled. */
  55133. +#define DWC_ASSERT(_expr, _format, _args...) do { \
  55134. + if (!(_expr)) { DWC_EXCEPTION("%s:%s:%d: " _format "\n", dwc_irq(), \
  55135. + __FILE__, __LINE__, ## _args); } \
  55136. + } while (0)
  55137. +#else
  55138. +#define DWC_ASSERT(_x...)
  55139. +#endif
  55140. +#define dwc_assert DWC_ASSERT
  55141. +
  55142. +
  55143. +/** @name Byte Ordering
  55144. + * The following functions are for conversions between processor's byte ordering
  55145. + * and specific ordering you want.
  55146. + */
  55147. +
  55148. +/** Converts 32 bit data in CPU byte ordering to little endian. */
  55149. +extern uint32_t DWC_CPU_TO_LE32(uint32_t *p);
  55150. +#define dwc_cpu_to_le32 DWC_CPU_TO_LE32
  55151. +
  55152. +/** Converts 32 bit data in CPU byte orderint to big endian. */
  55153. +extern uint32_t DWC_CPU_TO_BE32(uint32_t *p);
  55154. +#define dwc_cpu_to_be32 DWC_CPU_TO_BE32
  55155. +
  55156. +/** Converts 32 bit little endian data to CPU byte ordering. */
  55157. +extern uint32_t DWC_LE32_TO_CPU(uint32_t *p);
  55158. +#define dwc_le32_to_cpu DWC_LE32_TO_CPU
  55159. +
  55160. +/** Converts 32 bit big endian data to CPU byte ordering. */
  55161. +extern uint32_t DWC_BE32_TO_CPU(uint32_t *p);
  55162. +#define dwc_be32_to_cpu DWC_BE32_TO_CPU
  55163. +
  55164. +/** Converts 16 bit data in CPU byte ordering to little endian. */
  55165. +extern uint16_t DWC_CPU_TO_LE16(uint16_t *p);
  55166. +#define dwc_cpu_to_le16 DWC_CPU_TO_LE16
  55167. +
  55168. +/** Converts 16 bit data in CPU byte orderint to big endian. */
  55169. +extern uint16_t DWC_CPU_TO_BE16(uint16_t *p);
  55170. +#define dwc_cpu_to_be16 DWC_CPU_TO_BE16
  55171. +
  55172. +/** Converts 16 bit little endian data to CPU byte ordering. */
  55173. +extern uint16_t DWC_LE16_TO_CPU(uint16_t *p);
  55174. +#define dwc_le16_to_cpu DWC_LE16_TO_CPU
  55175. +
  55176. +/** Converts 16 bit bi endian data to CPU byte ordering. */
  55177. +extern uint16_t DWC_BE16_TO_CPU(uint16_t *p);
  55178. +#define dwc_be16_to_cpu DWC_BE16_TO_CPU
  55179. +
  55180. +
  55181. +/** @name Register Read/Write
  55182. + *
  55183. + * The following six functions should be implemented to read/write registers of
  55184. + * 32-bit and 64-bit sizes. All modules use this to read/write register values.
  55185. + * The reg value is a pointer to the register calculated from the void *base
  55186. + * variable passed into the driver when it is started. */
  55187. +
  55188. +#ifdef DWC_LINUX
  55189. +/* Linux doesn't need any extra parameters for register read/write, so we
  55190. + * just throw away the IO context parameter.
  55191. + */
  55192. +/** Reads the content of a 32-bit register. */
  55193. +extern uint32_t DWC_READ_REG32(uint32_t volatile *reg);
  55194. +#define dwc_read_reg32(_ctx_,_reg_) DWC_READ_REG32(_reg_)
  55195. +
  55196. +/** Reads the content of a 64-bit register. */
  55197. +extern uint64_t DWC_READ_REG64(uint64_t volatile *reg);
  55198. +#define dwc_read_reg64(_ctx_,_reg_) DWC_READ_REG64(_reg_)
  55199. +
  55200. +/** Writes to a 32-bit register. */
  55201. +extern void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value);
  55202. +#define dwc_write_reg32(_ctx_,_reg_,_val_) DWC_WRITE_REG32(_reg_, _val_)
  55203. +
  55204. +/** Writes to a 64-bit register. */
  55205. +extern void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value);
  55206. +#define dwc_write_reg64(_ctx_,_reg_,_val_) DWC_WRITE_REG64(_reg_, _val_)
  55207. +
  55208. +/**
  55209. + * Modify bit values in a register. Using the
  55210. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  55211. + */
  55212. +extern void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  55213. +#define dwc_modify_reg32(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG32(_reg_,_cmsk_,_smsk_)
  55214. +extern void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  55215. +#define dwc_modify_reg64(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG64(_reg_,_cmsk_,_smsk_)
  55216. +
  55217. +#endif /* DWC_LINUX */
  55218. +
  55219. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  55220. +typedef struct dwc_ioctx {
  55221. + struct device *dev;
  55222. + bus_space_tag_t iot;
  55223. + bus_space_handle_t ioh;
  55224. +} dwc_ioctx_t;
  55225. +
  55226. +/** BSD needs two extra parameters for register read/write, so we pass
  55227. + * them in using the IO context parameter.
  55228. + */
  55229. +/** Reads the content of a 32-bit register. */
  55230. +extern uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg);
  55231. +#define dwc_read_reg32 DWC_READ_REG32
  55232. +
  55233. +/** Reads the content of a 64-bit register. */
  55234. +extern uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg);
  55235. +#define dwc_read_reg64 DWC_READ_REG64
  55236. +
  55237. +/** Writes to a 32-bit register. */
  55238. +extern void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value);
  55239. +#define dwc_write_reg32 DWC_WRITE_REG32
  55240. +
  55241. +/** Writes to a 64-bit register. */
  55242. +extern void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value);
  55243. +#define dwc_write_reg64 DWC_WRITE_REG64
  55244. +
  55245. +/**
  55246. + * Modify bit values in a register. Using the
  55247. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  55248. + */
  55249. +extern void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  55250. +#define dwc_modify_reg32 DWC_MODIFY_REG32
  55251. +extern void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  55252. +#define dwc_modify_reg64 DWC_MODIFY_REG64
  55253. +
  55254. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  55255. +
  55256. +/** @cond */
  55257. +
  55258. +/** @name Some convenience MACROS used internally. Define DWC_DEBUG_REGS to log the
  55259. + * register writes. */
  55260. +
  55261. +#ifdef DWC_LINUX
  55262. +
  55263. +# ifdef DWC_DEBUG_REGS
  55264. +
  55265. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  55266. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  55267. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  55268. +} \
  55269. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  55270. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  55271. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  55272. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  55273. +}
  55274. +
  55275. +#define dwc_define_read_write_reg(_reg,_container_type) \
  55276. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  55277. + return DWC_READ_REG32(&container->regs->_reg); \
  55278. +} \
  55279. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  55280. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  55281. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  55282. +}
  55283. +
  55284. +# else /* DWC_DEBUG_REGS */
  55285. +
  55286. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  55287. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  55288. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  55289. +} \
  55290. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  55291. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  55292. +}
  55293. +
  55294. +#define dwc_define_read_write_reg(_reg,_container_type) \
  55295. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  55296. + return DWC_READ_REG32(&container->regs->_reg); \
  55297. +} \
  55298. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  55299. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  55300. +}
  55301. +
  55302. +# endif /* DWC_DEBUG_REGS */
  55303. +
  55304. +#endif /* DWC_LINUX */
  55305. +
  55306. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  55307. +
  55308. +# ifdef DWC_DEBUG_REGS
  55309. +
  55310. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  55311. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  55312. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  55313. +} \
  55314. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  55315. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  55316. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  55317. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  55318. +}
  55319. +
  55320. +#define dwc_define_read_write_reg(_reg,_container_type) \
  55321. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  55322. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  55323. +} \
  55324. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  55325. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  55326. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  55327. +}
  55328. +
  55329. +# else /* DWC_DEBUG_REGS */
  55330. +
  55331. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  55332. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  55333. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  55334. +} \
  55335. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  55336. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  55337. +}
  55338. +
  55339. +#define dwc_define_read_write_reg(_reg,_container_type) \
  55340. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  55341. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  55342. +} \
  55343. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  55344. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  55345. +}
  55346. +
  55347. +# endif /* DWC_DEBUG_REGS */
  55348. +
  55349. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  55350. +
  55351. +/** @endcond */
  55352. +
  55353. +
  55354. +#ifdef DWC_CRYPTOLIB
  55355. +/** @name Crypto Functions
  55356. + *
  55357. + * These are the low-level cryptographic functions used by the driver. */
  55358. +
  55359. +/** Perform AES CBC */
  55360. +extern int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out);
  55361. +#define dwc_aes_cbc DWC_AES_CBC
  55362. +
  55363. +/** Fill the provided buffer with random bytes. These should be cryptographic grade random numbers. */
  55364. +extern void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length);
  55365. +#define dwc_random_bytes DWC_RANDOM_BYTES
  55366. +
  55367. +/** Perform the SHA-256 hash function */
  55368. +extern int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out);
  55369. +#define dwc_sha256 DWC_SHA256
  55370. +
  55371. +/** Calculated the HMAC-SHA256 */
  55372. +extern int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t *out);
  55373. +#define dwc_hmac_sha256 DWC_HMAC_SHA256
  55374. +
  55375. +#endif /* DWC_CRYPTOLIB */
  55376. +
  55377. +
  55378. +/** @name Memory Allocation
  55379. + *
  55380. + * These function provide access to memory allocation. There are only 2 DMA
  55381. + * functions and 3 Regular memory functions that need to be implemented. None
  55382. + * of the memory debugging routines need to be implemented. The allocation
  55383. + * routines all ZERO the contents of the memory.
  55384. + *
  55385. + * Defining DWC_DEBUG_MEMORY turns on memory debugging and statistic gathering.
  55386. + * This checks for memory leaks, keeping track of alloc/free pairs. It also
  55387. + * keeps track of how much memory the driver is using at any given time. */
  55388. +
  55389. +#define DWC_PAGE_SIZE 4096
  55390. +#define DWC_PAGE_OFFSET(addr) (((uint32_t)addr) & 0xfff)
  55391. +#define DWC_PAGE_ALIGNED(addr) ((((uint32_t)addr) & 0xfff) == 0)
  55392. +
  55393. +#define DWC_INVALID_DMA_ADDR 0x0
  55394. +
  55395. +#ifdef DWC_LINUX
  55396. +/** Type for a DMA address */
  55397. +typedef dma_addr_t dwc_dma_t;
  55398. +#endif
  55399. +
  55400. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  55401. +typedef bus_addr_t dwc_dma_t;
  55402. +#endif
  55403. +
  55404. +#ifdef DWC_FREEBSD
  55405. +typedef struct dwc_dmactx {
  55406. + struct device *dev;
  55407. + bus_dma_tag_t dma_tag;
  55408. + bus_dmamap_t dma_map;
  55409. + bus_addr_t dma_paddr;
  55410. + void *dma_vaddr;
  55411. +} dwc_dmactx_t;
  55412. +#endif
  55413. +
  55414. +#ifdef DWC_NETBSD
  55415. +typedef struct dwc_dmactx {
  55416. + struct device *dev;
  55417. + bus_dma_tag_t dma_tag;
  55418. + bus_dmamap_t dma_map;
  55419. + bus_dma_segment_t segs[1];
  55420. + int nsegs;
  55421. + bus_addr_t dma_paddr;
  55422. + void *dma_vaddr;
  55423. +} dwc_dmactx_t;
  55424. +#endif
  55425. +
  55426. +/* @todo these functions will be added in the future */
  55427. +#if 0
  55428. +/**
  55429. + * Creates a DMA pool from which you can allocate DMA buffers. Buffers
  55430. + * allocated from this pool will be guaranteed to meet the size, alignment, and
  55431. + * boundary requirements specified.
  55432. + *
  55433. + * @param[in] size Specifies the size of the buffers that will be allocated from
  55434. + * this pool.
  55435. + * @param[in] align Specifies the byte alignment requirements of the buffers
  55436. + * allocated from this pool. Must be a power of 2.
  55437. + * @param[in] boundary Specifies the N-byte boundary that buffers allocated from
  55438. + * this pool must not cross.
  55439. + *
  55440. + * @returns A pointer to an internal opaque structure which is not to be
  55441. + * accessed outside of these library functions. Use this handle to specify
  55442. + * which pools to allocate/free DMA buffers from and also to destroy the pool,
  55443. + * when you are done with it.
  55444. + */
  55445. +extern dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size, uint32_t align, uint32_t boundary);
  55446. +
  55447. +/**
  55448. + * Destroy a DMA pool. All buffers allocated from that pool must be freed first.
  55449. + */
  55450. +extern void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool);
  55451. +
  55452. +/**
  55453. + * Allocate a buffer from the specified DMA pool and zeros its contents.
  55454. + */
  55455. +extern void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr);
  55456. +
  55457. +/**
  55458. + * Free a previously allocated buffer from the DMA pool.
  55459. + */
  55460. +extern void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr);
  55461. +#endif
  55462. +
  55463. +/** Allocates a DMA capable buffer and zeroes its contents. */
  55464. +extern void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  55465. +
  55466. +/** Allocates a DMA capable buffer and zeroes its contents in atomic contest */
  55467. +extern void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  55468. +
  55469. +/** Frees a previously allocated buffer. */
  55470. +extern void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr);
  55471. +
  55472. +/** Allocates a block of memory and zeroes its contents. */
  55473. +extern void *__DWC_ALLOC(void *mem_ctx, uint32_t size);
  55474. +
  55475. +/** Allocates a block of memory and zeroes its contents, in an atomic manner
  55476. + * which can be used inside interrupt context. The size should be sufficiently
  55477. + * small, a few KB at most, such that failures are not likely to occur. Can just call
  55478. + * __DWC_ALLOC if it is atomic. */
  55479. +extern void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size);
  55480. +
  55481. +/** Frees a previously allocated buffer. */
  55482. +extern void __DWC_FREE(void *mem_ctx, void *addr);
  55483. +
  55484. +#ifndef DWC_DEBUG_MEMORY
  55485. +
  55486. +#define DWC_ALLOC(_size_) __DWC_ALLOC(NULL, _size_)
  55487. +#define DWC_ALLOC_ATOMIC(_size_) __DWC_ALLOC_ATOMIC(NULL, _size_)
  55488. +#define DWC_FREE(_addr_) __DWC_FREE(NULL, _addr_)
  55489. +
  55490. +# ifdef DWC_LINUX
  55491. +#define DWC_DMA_ALLOC(_size_,_dma_) __DWC_DMA_ALLOC(NULL, _size_, _dma_)
  55492. +#define DWC_DMA_ALLOC_ATOMIC(_size_,_dma_) __DWC_DMA_ALLOC_ATOMIC(NULL, _size_,_dma_)
  55493. +#define DWC_DMA_FREE(_size_,_virt_,_dma_) __DWC_DMA_FREE(NULL, _size_, _virt_, _dma_)
  55494. +# endif
  55495. +
  55496. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  55497. +#define DWC_DMA_ALLOC __DWC_DMA_ALLOC
  55498. +#define DWC_DMA_FREE __DWC_DMA_FREE
  55499. +# endif
  55500. +extern void *dwc_dma_alloc_atomic_debug(uint32_t size, dwc_dma_t *dma_addr, char const *func, int line);
  55501. +
  55502. +#else /* DWC_DEBUG_MEMORY */
  55503. +
  55504. +extern void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  55505. +extern void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  55506. +extern void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line);
  55507. +extern void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  55508. + char const *func, int line);
  55509. +extern void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  55510. + char const *func, int line);
  55511. +extern void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  55512. + dwc_dma_t dma_addr, char const *func, int line);
  55513. +
  55514. +extern int dwc_memory_debug_start(void *mem_ctx);
  55515. +extern void dwc_memory_debug_stop(void);
  55516. +extern void dwc_memory_debug_report(void);
  55517. +
  55518. +#define DWC_ALLOC(_size_) dwc_alloc_debug(NULL, _size_, __func__, __LINE__)
  55519. +#define DWC_ALLOC_ATOMIC(_size_) dwc_alloc_atomic_debug(NULL, _size_, \
  55520. + __func__, __LINE__)
  55521. +#define DWC_FREE(_addr_) dwc_free_debug(NULL, _addr_, __func__, __LINE__)
  55522. +
  55523. +# ifdef DWC_LINUX
  55524. +#define DWC_DMA_ALLOC(_size_,_dma_) dwc_dma_alloc_debug(NULL, _size_, \
  55525. + _dma_, __func__, __LINE__)
  55526. +#define DWC_DMA_ALLOC_ATOMIC(_size_,_dma_) dwc_dma_alloc_atomic_debug(NULL, _size_, \
  55527. + _dma_, __func__, __LINE__)
  55528. +#define DWC_DMA_FREE(_size_,_virt_,_dma_) dwc_dma_free_debug(NULL, _size_, \
  55529. + _virt_, _dma_, __func__, __LINE__)
  55530. +# endif
  55531. +
  55532. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  55533. +#define DWC_DMA_ALLOC(_ctx_,_size_,_dma_) dwc_dma_alloc_debug(_ctx_, _size_, \
  55534. + _dma_, __func__, __LINE__)
  55535. +#define DWC_DMA_FREE(_ctx_,_size_,_virt_,_dma_) dwc_dma_free_debug(_ctx_, _size_, \
  55536. + _virt_, _dma_, __func__, __LINE__)
  55537. +# endif
  55538. +
  55539. +#endif /* DWC_DEBUG_MEMORY */
  55540. +
  55541. +#define dwc_alloc(_ctx_,_size_) DWC_ALLOC(_size_)
  55542. +#define dwc_alloc_atomic(_ctx_,_size_) DWC_ALLOC_ATOMIC(_size_)
  55543. +#define dwc_free(_ctx_,_addr_) DWC_FREE(_addr_)
  55544. +
  55545. +#ifdef DWC_LINUX
  55546. +/* Linux doesn't need any extra parameters for DMA buffer allocation, so we
  55547. + * just throw away the DMA context parameter.
  55548. + */
  55549. +#define dwc_dma_alloc(_ctx_,_size_,_dma_) DWC_DMA_ALLOC(_size_, _dma_)
  55550. +#define dwc_dma_alloc_atomic(_ctx_,_size_,_dma_) DWC_DMA_ALLOC_ATOMIC(_size_, _dma_)
  55551. +#define dwc_dma_free(_ctx_,_size_,_virt_,_dma_) DWC_DMA_FREE(_size_, _virt_, _dma_)
  55552. +#endif
  55553. +
  55554. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  55555. +/** BSD needs several extra parameters for DMA buffer allocation, so we pass
  55556. + * them in using the DMA context parameter.
  55557. + */
  55558. +#define dwc_dma_alloc DWC_DMA_ALLOC
  55559. +#define dwc_dma_free DWC_DMA_FREE
  55560. +#endif
  55561. +
  55562. +
  55563. +/** @name Memory and String Processing */
  55564. +
  55565. +/** memset() clone */
  55566. +extern void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size);
  55567. +#define dwc_memset DWC_MEMSET
  55568. +
  55569. +/** memcpy() clone */
  55570. +extern void *DWC_MEMCPY(void *dest, void const *src, uint32_t size);
  55571. +#define dwc_memcpy DWC_MEMCPY
  55572. +
  55573. +/** memmove() clone */
  55574. +extern void *DWC_MEMMOVE(void *dest, void *src, uint32_t size);
  55575. +#define dwc_memmove DWC_MEMMOVE
  55576. +
  55577. +/** memcmp() clone */
  55578. +extern int DWC_MEMCMP(void *m1, void *m2, uint32_t size);
  55579. +#define dwc_memcmp DWC_MEMCMP
  55580. +
  55581. +/** strcmp() clone */
  55582. +extern int DWC_STRCMP(void *s1, void *s2);
  55583. +#define dwc_strcmp DWC_STRCMP
  55584. +
  55585. +/** strncmp() clone */
  55586. +extern int DWC_STRNCMP(void *s1, void *s2, uint32_t size);
  55587. +#define dwc_strncmp DWC_STRNCMP
  55588. +
  55589. +/** strlen() clone, for NULL terminated ASCII strings */
  55590. +extern int DWC_STRLEN(char const *str);
  55591. +#define dwc_strlen DWC_STRLEN
  55592. +
  55593. +/** strcpy() clone, for NULL terminated ASCII strings */
  55594. +extern char *DWC_STRCPY(char *to, const char *from);
  55595. +#define dwc_strcpy DWC_STRCPY
  55596. +
  55597. +/** strdup() clone. If you wish to use memory allocation debugging, this
  55598. + * implementation of strdup should use the DWC_* memory routines instead of
  55599. + * calling a predefined strdup. Otherwise the memory allocated by this routine
  55600. + * will not be seen by the debugging routines. */
  55601. +extern char *DWC_STRDUP(char const *str);
  55602. +#define dwc_strdup(_ctx_,_str_) DWC_STRDUP(_str_)
  55603. +
  55604. +/** NOT an atoi() clone. Read the description carefully. Returns an integer
  55605. + * converted from the string str in base 10 unless the string begins with a "0x"
  55606. + * in which case it is base 16. String must be a NULL terminated sequence of
  55607. + * ASCII characters and may optionally begin with whitespace, a + or -, and a
  55608. + * "0x" prefix if base 16. The remaining characters must be valid digits for
  55609. + * the number and end with a NULL character. If any invalid characters are
  55610. + * encountered or it returns with a negative error code and the results of the
  55611. + * conversion are undefined. On sucess it returns 0. Overflow conditions are
  55612. + * undefined. An example implementation using atoi() can be referenced from the
  55613. + * Linux implementation. */
  55614. +extern int DWC_ATOI(const char *str, int32_t *value);
  55615. +#define dwc_atoi DWC_ATOI
  55616. +
  55617. +/** Same as above but for unsigned. */
  55618. +extern int DWC_ATOUI(const char *str, uint32_t *value);
  55619. +#define dwc_atoui DWC_ATOUI
  55620. +
  55621. +#ifdef DWC_UTFLIB
  55622. +/** This routine returns a UTF16LE unicode encoded string from a UTF8 string. */
  55623. +extern int DWC_UTF8_TO_UTF16LE(uint8_t const *utf8string, uint16_t *utf16string, unsigned len);
  55624. +#define dwc_utf8_to_utf16le DWC_UTF8_TO_UTF16LE
  55625. +#endif
  55626. +
  55627. +
  55628. +/** @name Wait queues
  55629. + *
  55630. + * Wait queues provide a means of synchronizing between threads or processes. A
  55631. + * process can block on a waitq if some condition is not true, waiting for it to
  55632. + * become true. When the waitq is triggered all waiting process will get
  55633. + * unblocked and the condition will be check again. Waitqs should be triggered
  55634. + * every time a condition can potentially change.*/
  55635. +struct dwc_waitq;
  55636. +
  55637. +/** Type for a waitq */
  55638. +typedef struct dwc_waitq dwc_waitq_t;
  55639. +
  55640. +/** The type of waitq condition callback function. This is called every time
  55641. + * condition is evaluated. */
  55642. +typedef int (*dwc_waitq_condition_t)(void *data);
  55643. +
  55644. +/** Allocate a waitq */
  55645. +extern dwc_waitq_t *DWC_WAITQ_ALLOC(void);
  55646. +#define dwc_waitq_alloc(_ctx_) DWC_WAITQ_ALLOC()
  55647. +
  55648. +/** Free a waitq */
  55649. +extern void DWC_WAITQ_FREE(dwc_waitq_t *wq);
  55650. +#define dwc_waitq_free DWC_WAITQ_FREE
  55651. +
  55652. +/** Check the condition and if it is false, block on the waitq. When unblocked, check the
  55653. + * condition again. The function returns when the condition becomes true. The return value
  55654. + * is 0 on condition true, DWC_WAITQ_ABORTED on abort or killed, or DWC_WAITQ_UNKNOWN on error. */
  55655. +extern int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data);
  55656. +#define dwc_waitq_wait DWC_WAITQ_WAIT
  55657. +
  55658. +/** Check the condition and if it is false, block on the waitq. When unblocked,
  55659. + * check the condition again. The function returns when the condition become
  55660. + * true or the timeout has passed. The return value is 0 on condition true or
  55661. + * DWC_TIMED_OUT on timeout, or DWC_WAITQ_ABORTED, or DWC_WAITQ_UNKNOWN on
  55662. + * error. */
  55663. +extern int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  55664. + void *data, int32_t msecs);
  55665. +#define dwc_waitq_wait_timeout DWC_WAITQ_WAIT_TIMEOUT
  55666. +
  55667. +/** Trigger a waitq, unblocking all processes. This should be called whenever a condition
  55668. + * has potentially changed. */
  55669. +extern void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq);
  55670. +#define dwc_waitq_trigger DWC_WAITQ_TRIGGER
  55671. +
  55672. +/** Unblock all processes waiting on the waitq with an ABORTED result. */
  55673. +extern void DWC_WAITQ_ABORT(dwc_waitq_t *wq);
  55674. +#define dwc_waitq_abort DWC_WAITQ_ABORT
  55675. +
  55676. +
  55677. +/** @name Threads
  55678. + *
  55679. + * A thread must be explicitly stopped. It must check DWC_THREAD_SHOULD_STOP
  55680. + * whenever it is woken up, and then return. The DWC_THREAD_STOP function
  55681. + * returns the value from the thread.
  55682. + */
  55683. +
  55684. +struct dwc_thread;
  55685. +
  55686. +/** Type for a thread */
  55687. +typedef struct dwc_thread dwc_thread_t;
  55688. +
  55689. +/** The thread function */
  55690. +typedef int (*dwc_thread_function_t)(void *data);
  55691. +
  55692. +/** Create a thread and start it running the thread_function. Returns a handle
  55693. + * to the thread */
  55694. +extern dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data);
  55695. +#define dwc_thread_run(_ctx_,_func_,_name_,_data_) DWC_THREAD_RUN(_func_, _name_, _data_)
  55696. +
  55697. +/** Stops a thread. Return the value returned by the thread. Or will return
  55698. + * DWC_ABORT if the thread never started. */
  55699. +extern int DWC_THREAD_STOP(dwc_thread_t *thread);
  55700. +#define dwc_thread_stop DWC_THREAD_STOP
  55701. +
  55702. +/** Signifies to the thread that it must stop. */
  55703. +#ifdef DWC_LINUX
  55704. +/* Linux doesn't need any parameters for kthread_should_stop() */
  55705. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(void);
  55706. +#define dwc_thread_should_stop(_thrd_) DWC_THREAD_SHOULD_STOP()
  55707. +
  55708. +/* No thread_exit function in Linux */
  55709. +#define dwc_thread_exit(_thrd_)
  55710. +#endif
  55711. +
  55712. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  55713. +/** BSD needs the thread pointer for kthread_suspend_check() */
  55714. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread);
  55715. +#define dwc_thread_should_stop DWC_THREAD_SHOULD_STOP
  55716. +
  55717. +/** The thread must call this to exit. */
  55718. +extern void DWC_THREAD_EXIT(dwc_thread_t *thread);
  55719. +#define dwc_thread_exit DWC_THREAD_EXIT
  55720. +#endif
  55721. +
  55722. +
  55723. +/** @name Work queues
  55724. + *
  55725. + * Workqs are used to queue a callback function to be called at some later time,
  55726. + * in another thread. */
  55727. +struct dwc_workq;
  55728. +
  55729. +/** Type for a workq */
  55730. +typedef struct dwc_workq dwc_workq_t;
  55731. +
  55732. +/** The type of the callback function to be called. */
  55733. +typedef void (*dwc_work_callback_t)(void *data);
  55734. +
  55735. +/** Allocate a workq */
  55736. +extern dwc_workq_t *DWC_WORKQ_ALLOC(char *name);
  55737. +#define dwc_workq_alloc(_ctx_,_name_) DWC_WORKQ_ALLOC(_name_)
  55738. +
  55739. +/** Free a workq. All work must be completed before being freed. */
  55740. +extern void DWC_WORKQ_FREE(dwc_workq_t *workq);
  55741. +#define dwc_workq_free DWC_WORKQ_FREE
  55742. +
  55743. +/** Schedule a callback on the workq, passing in data. The function will be
  55744. + * scheduled at some later time. */
  55745. +extern void DWC_WORKQ_SCHEDULE(dwc_workq_t *workq, dwc_work_callback_t cb,
  55746. + void *data, char *format, ...)
  55747. +#ifdef __GNUC__
  55748. + __attribute__ ((format(printf, 4, 5)));
  55749. +#else
  55750. + ;
  55751. +#endif
  55752. +#define dwc_workq_schedule DWC_WORKQ_SCHEDULE
  55753. +
  55754. +/** Schedule a callback on the workq, that will be called until at least
  55755. + * given number miliseconds have passed. */
  55756. +extern void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *workq, dwc_work_callback_t cb,
  55757. + void *data, uint32_t time, char *format, ...)
  55758. +#ifdef __GNUC__
  55759. + __attribute__ ((format(printf, 5, 6)));
  55760. +#else
  55761. + ;
  55762. +#endif
  55763. +#define dwc_workq_schedule_delayed DWC_WORKQ_SCHEDULE_DELAYED
  55764. +
  55765. +/** The number of processes in the workq */
  55766. +extern int DWC_WORKQ_PENDING(dwc_workq_t *workq);
  55767. +#define dwc_workq_pending DWC_WORKQ_PENDING
  55768. +
  55769. +/** Blocks until all the work in the workq is complete or timed out. Returns <
  55770. + * 0 on timeout. */
  55771. +extern int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout);
  55772. +#define dwc_workq_wait_work_done DWC_WORKQ_WAIT_WORK_DONE
  55773. +
  55774. +
  55775. +/** @name Tasklets
  55776. + *
  55777. + */
  55778. +struct dwc_tasklet;
  55779. +
  55780. +/** Type for a tasklet */
  55781. +typedef struct dwc_tasklet dwc_tasklet_t;
  55782. +
  55783. +/** The type of the callback function to be called */
  55784. +typedef void (*dwc_tasklet_callback_t)(void *data);
  55785. +
  55786. +/** Allocates a tasklet */
  55787. +extern dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data);
  55788. +#define dwc_task_alloc(_ctx_,_name_,_cb_,_data_) DWC_TASK_ALLOC(_name_, _cb_, _data_)
  55789. +
  55790. +/** Frees a tasklet */
  55791. +extern void DWC_TASK_FREE(dwc_tasklet_t *task);
  55792. +#define dwc_task_free DWC_TASK_FREE
  55793. +
  55794. +/** Schedules a tasklet to run */
  55795. +extern void DWC_TASK_SCHEDULE(dwc_tasklet_t *task);
  55796. +#define dwc_task_schedule DWC_TASK_SCHEDULE
  55797. +
  55798. +extern void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task);
  55799. +#define dwc_task_hi_schedule DWC_TASK_HI_SCHEDULE
  55800. +
  55801. +/** @name Timer
  55802. + *
  55803. + * Callbacks must be small and atomic.
  55804. + */
  55805. +struct dwc_timer;
  55806. +
  55807. +/** Type for a timer */
  55808. +typedef struct dwc_timer dwc_timer_t;
  55809. +
  55810. +/** The type of the callback function to be called */
  55811. +typedef void (*dwc_timer_callback_t)(void *data);
  55812. +
  55813. +/** Allocates a timer */
  55814. +extern dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data);
  55815. +#define dwc_timer_alloc(_ctx_,_name_,_cb_,_data_) DWC_TIMER_ALLOC(_name_,_cb_,_data_)
  55816. +
  55817. +/** Frees a timer */
  55818. +extern void DWC_TIMER_FREE(dwc_timer_t *timer);
  55819. +#define dwc_timer_free DWC_TIMER_FREE
  55820. +
  55821. +/** Schedules the timer to run at time ms from now. And will repeat at every
  55822. + * repeat_interval msec therafter
  55823. + *
  55824. + * Modifies a timer that is still awaiting execution to a new expiration time.
  55825. + * The mod_time is added to the old time. */
  55826. +extern void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time);
  55827. +#define dwc_timer_schedule DWC_TIMER_SCHEDULE
  55828. +
  55829. +/** Disables the timer from execution. */
  55830. +extern void DWC_TIMER_CANCEL(dwc_timer_t *timer);
  55831. +#define dwc_timer_cancel DWC_TIMER_CANCEL
  55832. +
  55833. +
  55834. +/** @name Spinlocks
  55835. + *
  55836. + * These locks are used when the work between the lock/unlock is atomic and
  55837. + * short. Interrupts are also disabled during the lock/unlock and thus they are
  55838. + * suitable to lock between interrupt/non-interrupt context. They also lock
  55839. + * between processes if you have multiple CPUs or Preemption. If you don't have
  55840. + * multiple CPUS or Preemption, then the you can simply implement the
  55841. + * DWC_SPINLOCK and DWC_SPINUNLOCK to disable and enable interrupts. Because
  55842. + * the work between the lock/unlock is atomic, the process context will never
  55843. + * change, and so you never have to lock between processes. */
  55844. +
  55845. +struct dwc_spinlock;
  55846. +
  55847. +/** Type for a spinlock */
  55848. +typedef struct dwc_spinlock dwc_spinlock_t;
  55849. +
  55850. +/** Type for the 'flags' argument to spinlock funtions */
  55851. +typedef unsigned long dwc_irqflags_t;
  55852. +
  55853. +/** Returns an initialized lock variable. This function should allocate and
  55854. + * initialize the OS-specific data structure used for locking. This data
  55855. + * structure is to be used for the DWC_LOCK and DWC_UNLOCK functions and should
  55856. + * be freed by the DWC_FREE_LOCK when it is no longer used. */
  55857. +extern dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void);
  55858. +#define dwc_spinlock_alloc(_ctx_) DWC_SPINLOCK_ALLOC()
  55859. +
  55860. +/** Frees an initialized lock variable. */
  55861. +extern void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock);
  55862. +#define dwc_spinlock_free(_ctx_,_lock_) DWC_SPINLOCK_FREE(_lock_)
  55863. +
  55864. +/** Disables interrupts and blocks until it acquires the lock.
  55865. + *
  55866. + * @param lock Pointer to the spinlock.
  55867. + * @param flags Unsigned long for irq flags storage.
  55868. + */
  55869. +extern void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags);
  55870. +#define dwc_spinlock_irqsave DWC_SPINLOCK_IRQSAVE
  55871. +
  55872. +/** Re-enables the interrupt and releases the lock.
  55873. + *
  55874. + * @param lock Pointer to the spinlock.
  55875. + * @param flags Unsigned long for irq flags storage. Must be the same as was
  55876. + * passed into DWC_LOCK.
  55877. + */
  55878. +extern void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags);
  55879. +#define dwc_spinunlock_irqrestore DWC_SPINUNLOCK_IRQRESTORE
  55880. +
  55881. +/** Blocks until it acquires the lock.
  55882. + *
  55883. + * @param lock Pointer to the spinlock.
  55884. + */
  55885. +extern void DWC_SPINLOCK(dwc_spinlock_t *lock);
  55886. +#define dwc_spinlock DWC_SPINLOCK
  55887. +
  55888. +/** Releases the lock.
  55889. + *
  55890. + * @param lock Pointer to the spinlock.
  55891. + */
  55892. +extern void DWC_SPINUNLOCK(dwc_spinlock_t *lock);
  55893. +#define dwc_spinunlock DWC_SPINUNLOCK
  55894. +
  55895. +
  55896. +/** @name Mutexes
  55897. + *
  55898. + * Unlike spinlocks Mutexes lock only between processes and the work between the
  55899. + * lock/unlock CAN block, therefore it CANNOT be called from interrupt context.
  55900. + */
  55901. +
  55902. +struct dwc_mutex;
  55903. +
  55904. +/** Type for a mutex */
  55905. +typedef struct dwc_mutex dwc_mutex_t;
  55906. +
  55907. +/* For Linux Mutex Debugging make it inline because the debugging routines use
  55908. + * the symbol to determine recursive locking. This makes it falsely think
  55909. + * recursive locking occurs. */
  55910. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  55911. +#define DWC_MUTEX_ALLOC_LINUX_DEBUG(__mutexp) ({ \
  55912. + __mutexp = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex)); \
  55913. + mutex_init((struct mutex *)__mutexp); \
  55914. +})
  55915. +#endif
  55916. +
  55917. +/** Allocate a mutex */
  55918. +extern dwc_mutex_t *DWC_MUTEX_ALLOC(void);
  55919. +#define dwc_mutex_alloc(_ctx_) DWC_MUTEX_ALLOC()
  55920. +
  55921. +/* For memory leak debugging when using Linux Mutex Debugging */
  55922. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  55923. +#define DWC_MUTEX_FREE(__mutexp) do { \
  55924. + mutex_destroy((struct mutex *)__mutexp); \
  55925. + DWC_FREE(__mutexp); \
  55926. +} while(0)
  55927. +#else
  55928. +/** Free a mutex */
  55929. +extern void DWC_MUTEX_FREE(dwc_mutex_t *mutex);
  55930. +#define dwc_mutex_free(_ctx_,_mutex_) DWC_MUTEX_FREE(_mutex_)
  55931. +#endif
  55932. +
  55933. +/** Lock a mutex */
  55934. +extern void DWC_MUTEX_LOCK(dwc_mutex_t *mutex);
  55935. +#define dwc_mutex_lock DWC_MUTEX_LOCK
  55936. +
  55937. +/** Non-blocking lock returns 1 on successful lock. */
  55938. +extern int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex);
  55939. +#define dwc_mutex_trylock DWC_MUTEX_TRYLOCK
  55940. +
  55941. +/** Unlock a mutex */
  55942. +extern void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex);
  55943. +#define dwc_mutex_unlock DWC_MUTEX_UNLOCK
  55944. +
  55945. +
  55946. +/** @name Time */
  55947. +
  55948. +/** Microsecond delay.
  55949. + *
  55950. + * @param usecs Microseconds to delay.
  55951. + */
  55952. +extern void DWC_UDELAY(uint32_t usecs);
  55953. +#define dwc_udelay DWC_UDELAY
  55954. +
  55955. +/** Millisecond delay.
  55956. + *
  55957. + * @param msecs Milliseconds to delay.
  55958. + */
  55959. +extern void DWC_MDELAY(uint32_t msecs);
  55960. +#define dwc_mdelay DWC_MDELAY
  55961. +
  55962. +/** Non-busy waiting.
  55963. + * Sleeps for specified number of milliseconds.
  55964. + *
  55965. + * @param msecs Milliseconds to sleep.
  55966. + */
  55967. +extern void DWC_MSLEEP(uint32_t msecs);
  55968. +#define dwc_msleep DWC_MSLEEP
  55969. +
  55970. +/**
  55971. + * Returns number of milliseconds since boot.
  55972. + */
  55973. +extern uint32_t DWC_TIME(void);
  55974. +#define dwc_time DWC_TIME
  55975. +
  55976. +
  55977. +
  55978. +
  55979. +/* @mainpage DWC Portability and Common Library
  55980. + *
  55981. + * This is the documentation for the DWC Portability and Common Library.
  55982. + *
  55983. + * @section intro Introduction
  55984. + *
  55985. + * The DWC Portability library consists of wrapper calls and data structures to
  55986. + * all low-level functions which are typically provided by the OS. The WUDEV
  55987. + * driver uses only these functions. In order to port the WUDEV driver, only
  55988. + * the functions in this library need to be re-implemented, with the same
  55989. + * behavior as documented here.
  55990. + *
  55991. + * The Common library consists of higher level functions, which rely only on
  55992. + * calling the functions from the DWC Portability library. These common
  55993. + * routines are shared across modules. Some of the common libraries need to be
  55994. + * used directly by the driver programmer when porting WUDEV. Such as the
  55995. + * parameter and notification libraries.
  55996. + *
  55997. + * @section low Portability Library OS Wrapper Functions
  55998. + *
  55999. + * Any function starting with DWC and in all CAPS is a low-level OS-wrapper that
  56000. + * needs to be implemented when porting, for example DWC_MUTEX_ALLOC(). All of
  56001. + * these functions are included in the dwc_os.h file.
  56002. + *
  56003. + * There are many functions here covering a wide array of OS services. Please
  56004. + * see dwc_os.h for details, and implementation notes for each function.
  56005. + *
  56006. + * @section common Common Library Functions
  56007. + *
  56008. + * Any function starting with dwc and in all lowercase is a common library
  56009. + * routine. These functions have a portable implementation and do not need to
  56010. + * be reimplemented when porting. The common routines can be used by any
  56011. + * driver, and some must be used by the end user to control the drivers. For
  56012. + * example, you must use the Parameter common library in order to set the
  56013. + * parameters in the WUDEV module.
  56014. + *
  56015. + * The common libraries consist of the following:
  56016. + *
  56017. + * - Connection Contexts - Used internally and can be used by end-user. See dwc_cc.h
  56018. + * - Parameters - Used internally and can be used by end-user. See dwc_params.h
  56019. + * - Notifications - Used internally and can be used by end-user. See dwc_notifier.h
  56020. + * - Lists - Used internally and can be used by end-user. See dwc_list.h
  56021. + * - Memory Debugging - Used internally and can be used by end-user. See dwc_os.h
  56022. + * - Modpow - Used internally only. See dwc_modpow.h
  56023. + * - DH - Used internally only. See dwc_dh.h
  56024. + * - Crypto - Used internally only. See dwc_crypto.h
  56025. + *
  56026. + *
  56027. + * @section prereq Prerequistes For dwc_os.h
  56028. + * @subsection types Data Types
  56029. + *
  56030. + * The dwc_os.h file assumes that several low-level data types are pre defined for the
  56031. + * compilation environment. These data types are:
  56032. + *
  56033. + * - uint8_t - unsigned 8-bit data type
  56034. + * - int8_t - signed 8-bit data type
  56035. + * - uint16_t - unsigned 16-bit data type
  56036. + * - int16_t - signed 16-bit data type
  56037. + * - uint32_t - unsigned 32-bit data type
  56038. + * - int32_t - signed 32-bit data type
  56039. + * - uint64_t - unsigned 64-bit data type
  56040. + * - int64_t - signed 64-bit data type
  56041. + *
  56042. + * Ensure that these are defined before using dwc_os.h. The easiest way to do
  56043. + * that is to modify the top of the file to include the appropriate header.
  56044. + * This is already done for the Linux environment. If the DWC_LINUX macro is
  56045. + * defined, the correct header will be added. A standard header <stdint.h> is
  56046. + * also used for environments where standard C headers are available.
  56047. + *
  56048. + * @subsection stdarg Variable Arguments
  56049. + *
  56050. + * Variable arguments are provided by a standard C header <stdarg.h>. it is
  56051. + * available in Both the Linux and ANSI C enviornment. An equivalent must be
  56052. + * provided in your enviornment in order to use dwc_os.h with the debug and
  56053. + * tracing message functionality.
  56054. + *
  56055. + * @subsection thread Threading
  56056. + *
  56057. + * WUDEV Core must be run on an operating system that provides for multiple
  56058. + * threads/processes. Threading can be implemented in many ways, even in
  56059. + * embedded systems without an operating system. At the bare minimum, the
  56060. + * system should be able to start any number of processes at any time to handle
  56061. + * special work. It need not be a pre-emptive system. Process context can
  56062. + * change upon a call to a blocking function. The hardware interrupt context
  56063. + * that calls the module's ISR() function must be differentiable from process
  56064. + * context, even if your processes are impemented via a hardware interrupt.
  56065. + * Further locking mechanism between process must exist (or be implemented), and
  56066. + * process context must have a way to disable interrupts for a period of time to
  56067. + * lock them out. If all of this exists, the functions in dwc_os.h related to
  56068. + * threading should be able to be implemented with the defined behavior.
  56069. + *
  56070. + */
  56071. +
  56072. +#ifdef __cplusplus
  56073. +}
  56074. +#endif
  56075. +
  56076. +#endif /* _DWC_OS_H_ */
  56077. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/Makefile linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/Makefile
  56078. --- linux-3.12.33/drivers/usb/host/dwc_common_port/Makefile 1969-12-31 18:00:00.000000000 -0600
  56079. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/Makefile 2014-12-03 19:13:40.212418001 -0600
  56080. @@ -0,0 +1,58 @@
  56081. +#
  56082. +# Makefile for DWC_common library
  56083. +#
  56084. +
  56085. +ifneq ($(KERNELRELEASE),)
  56086. +
  56087. +ccflags-y += -DDWC_LINUX
  56088. +#ccflags-y += -DDEBUG
  56089. +#ccflags-y += -DDWC_DEBUG_REGS
  56090. +#ccflags-y += -DDWC_DEBUG_MEMORY
  56091. +
  56092. +ccflags-y += -DDWC_LIBMODULE
  56093. +ccflags-y += -DDWC_CCLIB
  56094. +#ccflags-y += -DDWC_CRYPTOLIB
  56095. +ccflags-y += -DDWC_NOTIFYLIB
  56096. +ccflags-y += -DDWC_UTFLIB
  56097. +
  56098. +obj-$(CONFIG_USB_DWCOTG) += dwc_common_port_lib.o
  56099. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  56100. + dwc_crypto.o dwc_notifier.o \
  56101. + dwc_common_linux.o dwc_mem.o
  56102. +
  56103. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  56104. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  56105. +
  56106. +ifneq ($(kernrel3),2.6.20)
  56107. +# grayg - I only know that we use ccflags-y in 2.6.31 actually
  56108. +ccflags-y += $(CPPFLAGS)
  56109. +endif
  56110. +
  56111. +else
  56112. +
  56113. +#ifeq ($(KDIR),)
  56114. +#$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  56115. +#endif
  56116. +
  56117. +ifeq ($(ARCH),)
  56118. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  56119. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  56120. +endif
  56121. +
  56122. +ifeq ($(DOXYGEN),)
  56123. +DOXYGEN := doxygen
  56124. +endif
  56125. +
  56126. +default:
  56127. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  56128. +
  56129. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  56130. + $(DOXYGEN) doc/doxygen.cfg
  56131. +
  56132. +tags: $(wildcard *.[hc])
  56133. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  56134. +
  56135. +endif
  56136. +
  56137. +clean:
  56138. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  56139. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/Makefile.fbsd linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/Makefile.fbsd
  56140. --- linux-3.12.33/drivers/usb/host/dwc_common_port/Makefile.fbsd 1969-12-31 18:00:00.000000000 -0600
  56141. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/Makefile.fbsd 2014-12-03 19:13:40.212418001 -0600
  56142. @@ -0,0 +1,17 @@
  56143. +CFLAGS += -I/sys/i386/compile/GENERIC -I/sys/i386/include -I/usr/include
  56144. +CFLAGS += -DDWC_FREEBSD
  56145. +CFLAGS += -DDEBUG
  56146. +#CFLAGS += -DDWC_DEBUG_REGS
  56147. +#CFLAGS += -DDWC_DEBUG_MEMORY
  56148. +
  56149. +#CFLAGS += -DDWC_LIBMODULE
  56150. +#CFLAGS += -DDWC_CCLIB
  56151. +#CFLAGS += -DDWC_CRYPTOLIB
  56152. +#CFLAGS += -DDWC_NOTIFYLIB
  56153. +#CFLAGS += -DDWC_UTFLIB
  56154. +
  56155. +KMOD = dwc_common_port_lib
  56156. +SRCS = dwc_cc.c dwc_modpow.c dwc_dh.c dwc_crypto.c dwc_notifier.c \
  56157. + dwc_common_fbsd.c dwc_mem.c
  56158. +
  56159. +.include <bsd.kmod.mk>
  56160. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/Makefile.linux linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/Makefile.linux
  56161. --- linux-3.12.33/drivers/usb/host/dwc_common_port/Makefile.linux 1969-12-31 18:00:00.000000000 -0600
  56162. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/Makefile.linux 2014-12-03 19:13:40.212418001 -0600
  56163. @@ -0,0 +1,49 @@
  56164. +#
  56165. +# Makefile for DWC_common library
  56166. +#
  56167. +ifneq ($(KERNELRELEASE),)
  56168. +
  56169. +ccflags-y += -DDWC_LINUX
  56170. +#ccflags-y += -DDEBUG
  56171. +#ccflags-y += -DDWC_DEBUG_REGS
  56172. +#ccflags-y += -DDWC_DEBUG_MEMORY
  56173. +
  56174. +ccflags-y += -DDWC_LIBMODULE
  56175. +ccflags-y += -DDWC_CCLIB
  56176. +ccflags-y += -DDWC_CRYPTOLIB
  56177. +ccflags-y += -DDWC_NOTIFYLIB
  56178. +ccflags-y += -DDWC_UTFLIB
  56179. +
  56180. +obj-m := dwc_common_port_lib.o
  56181. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  56182. + dwc_crypto.o dwc_notifier.o \
  56183. + dwc_common_linux.o dwc_mem.o
  56184. +
  56185. +else
  56186. +
  56187. +ifeq ($(KDIR),)
  56188. +$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  56189. +endif
  56190. +
  56191. +ifeq ($(ARCH),)
  56192. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  56193. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  56194. +endif
  56195. +
  56196. +ifeq ($(DOXYGEN),)
  56197. +DOXYGEN := doxygen
  56198. +endif
  56199. +
  56200. +default:
  56201. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  56202. +
  56203. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  56204. + $(DOXYGEN) doc/doxygen.cfg
  56205. +
  56206. +tags: $(wildcard *.[hc])
  56207. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  56208. +
  56209. +endif
  56210. +
  56211. +clean:
  56212. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  56213. diff -Nur linux-3.12.33/drivers/usb/host/dwc_common_port/usb.h linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/usb.h
  56214. --- linux-3.12.33/drivers/usb/host/dwc_common_port/usb.h 1969-12-31 18:00:00.000000000 -0600
  56215. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_common_port/usb.h 2014-12-03 19:13:40.216418001 -0600
  56216. @@ -0,0 +1,946 @@
  56217. +/*
  56218. + * Copyright (c) 1998 The NetBSD Foundation, Inc.
  56219. + * All rights reserved.
  56220. + *
  56221. + * This code is derived from software contributed to The NetBSD Foundation
  56222. + * by Lennart Augustsson (lennart@augustsson.net) at
  56223. + * Carlstedt Research & Technology.
  56224. + *
  56225. + * Redistribution and use in source and binary forms, with or without
  56226. + * modification, are permitted provided that the following conditions
  56227. + * are met:
  56228. + * 1. Redistributions of source code must retain the above copyright
  56229. + * notice, this list of conditions and the following disclaimer.
  56230. + * 2. Redistributions in binary form must reproduce the above copyright
  56231. + * notice, this list of conditions and the following disclaimer in the
  56232. + * documentation and/or other materials provided with the distribution.
  56233. + * 3. All advertising materials mentioning features or use of this software
  56234. + * must display the following acknowledgement:
  56235. + * This product includes software developed by the NetBSD
  56236. + * Foundation, Inc. and its contributors.
  56237. + * 4. Neither the name of The NetBSD Foundation nor the names of its
  56238. + * contributors may be used to endorse or promote products derived
  56239. + * from this software without specific prior written permission.
  56240. + *
  56241. + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  56242. + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  56243. + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  56244. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  56245. + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  56246. + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  56247. + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  56248. + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  56249. + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  56250. + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  56251. + * POSSIBILITY OF SUCH DAMAGE.
  56252. + */
  56253. +
  56254. +/* Modified by Synopsys, Inc, 12/12/2007 */
  56255. +
  56256. +
  56257. +#ifndef _USB_H_
  56258. +#define _USB_H_
  56259. +
  56260. +#ifdef __cplusplus
  56261. +extern "C" {
  56262. +#endif
  56263. +
  56264. +/*
  56265. + * The USB records contain some unaligned little-endian word
  56266. + * components. The U[SG]ETW macros take care of both the alignment
  56267. + * and endian problem and should always be used to access non-byte
  56268. + * values.
  56269. + */
  56270. +typedef u_int8_t uByte;
  56271. +typedef u_int8_t uWord[2];
  56272. +typedef u_int8_t uDWord[4];
  56273. +
  56274. +#define USETW2(w,h,l) ((w)[0] = (u_int8_t)(l), (w)[1] = (u_int8_t)(h))
  56275. +#define UCONSTW(x) { (x) & 0xff, ((x) >> 8) & 0xff }
  56276. +#define UCONSTDW(x) { (x) & 0xff, ((x) >> 8) & 0xff, \
  56277. + ((x) >> 16) & 0xff, ((x) >> 24) & 0xff }
  56278. +
  56279. +#if 1
  56280. +#define UGETW(w) ((w)[0] | ((w)[1] << 8))
  56281. +#define USETW(w,v) ((w)[0] = (u_int8_t)(v), (w)[1] = (u_int8_t)((v) >> 8))
  56282. +#define UGETDW(w) ((w)[0] | ((w)[1] << 8) | ((w)[2] << 16) | ((w)[3] << 24))
  56283. +#define USETDW(w,v) ((w)[0] = (u_int8_t)(v), \
  56284. + (w)[1] = (u_int8_t)((v) >> 8), \
  56285. + (w)[2] = (u_int8_t)((v) >> 16), \
  56286. + (w)[3] = (u_int8_t)((v) >> 24))
  56287. +#else
  56288. +/*
  56289. + * On little-endian machines that can handle unanliged accesses
  56290. + * (e.g. i386) these macros can be replaced by the following.
  56291. + */
  56292. +#define UGETW(w) (*(u_int16_t *)(w))
  56293. +#define USETW(w,v) (*(u_int16_t *)(w) = (v))
  56294. +#define UGETDW(w) (*(u_int32_t *)(w))
  56295. +#define USETDW(w,v) (*(u_int32_t *)(w) = (v))
  56296. +#endif
  56297. +
  56298. +/*
  56299. + * Macros for accessing UAS IU fields, which are big-endian
  56300. + */
  56301. +#define IUSETW2(w,h,l) ((w)[0] = (u_int8_t)(h), (w)[1] = (u_int8_t)(l))
  56302. +#define IUCONSTW(x) { ((x) >> 8) & 0xff, (x) & 0xff }
  56303. +#define IUCONSTDW(x) { ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
  56304. + ((x) >> 8) & 0xff, (x) & 0xff }
  56305. +#define IUGETW(w) (((w)[0] << 8) | (w)[1])
  56306. +#define IUSETW(w,v) ((w)[0] = (u_int8_t)((v) >> 8), (w)[1] = (u_int8_t)(v))
  56307. +#define IUGETDW(w) (((w)[0] << 24) | ((w)[1] << 16) | ((w)[2] << 8) | (w)[3])
  56308. +#define IUSETDW(w,v) ((w)[0] = (u_int8_t)((v) >> 24), \
  56309. + (w)[1] = (u_int8_t)((v) >> 16), \
  56310. + (w)[2] = (u_int8_t)((v) >> 8), \
  56311. + (w)[3] = (u_int8_t)(v))
  56312. +
  56313. +#define UPACKED __attribute__((__packed__))
  56314. +
  56315. +typedef struct {
  56316. + uByte bmRequestType;
  56317. + uByte bRequest;
  56318. + uWord wValue;
  56319. + uWord wIndex;
  56320. + uWord wLength;
  56321. +} UPACKED usb_device_request_t;
  56322. +
  56323. +#define UT_GET_DIR(a) ((a) & 0x80)
  56324. +#define UT_WRITE 0x00
  56325. +#define UT_READ 0x80
  56326. +
  56327. +#define UT_GET_TYPE(a) ((a) & 0x60)
  56328. +#define UT_STANDARD 0x00
  56329. +#define UT_CLASS 0x20
  56330. +#define UT_VENDOR 0x40
  56331. +
  56332. +#define UT_GET_RECIPIENT(a) ((a) & 0x1f)
  56333. +#define UT_DEVICE 0x00
  56334. +#define UT_INTERFACE 0x01
  56335. +#define UT_ENDPOINT 0x02
  56336. +#define UT_OTHER 0x03
  56337. +
  56338. +#define UT_READ_DEVICE (UT_READ | UT_STANDARD | UT_DEVICE)
  56339. +#define UT_READ_INTERFACE (UT_READ | UT_STANDARD | UT_INTERFACE)
  56340. +#define UT_READ_ENDPOINT (UT_READ | UT_STANDARD | UT_ENDPOINT)
  56341. +#define UT_WRITE_DEVICE (UT_WRITE | UT_STANDARD | UT_DEVICE)
  56342. +#define UT_WRITE_INTERFACE (UT_WRITE | UT_STANDARD | UT_INTERFACE)
  56343. +#define UT_WRITE_ENDPOINT (UT_WRITE | UT_STANDARD | UT_ENDPOINT)
  56344. +#define UT_READ_CLASS_DEVICE (UT_READ | UT_CLASS | UT_DEVICE)
  56345. +#define UT_READ_CLASS_INTERFACE (UT_READ | UT_CLASS | UT_INTERFACE)
  56346. +#define UT_READ_CLASS_OTHER (UT_READ | UT_CLASS | UT_OTHER)
  56347. +#define UT_READ_CLASS_ENDPOINT (UT_READ | UT_CLASS | UT_ENDPOINT)
  56348. +#define UT_WRITE_CLASS_DEVICE (UT_WRITE | UT_CLASS | UT_DEVICE)
  56349. +#define UT_WRITE_CLASS_INTERFACE (UT_WRITE | UT_CLASS | UT_INTERFACE)
  56350. +#define UT_WRITE_CLASS_OTHER (UT_WRITE | UT_CLASS | UT_OTHER)
  56351. +#define UT_WRITE_CLASS_ENDPOINT (UT_WRITE | UT_CLASS | UT_ENDPOINT)
  56352. +#define UT_READ_VENDOR_DEVICE (UT_READ | UT_VENDOR | UT_DEVICE)
  56353. +#define UT_READ_VENDOR_INTERFACE (UT_READ | UT_VENDOR | UT_INTERFACE)
  56354. +#define UT_READ_VENDOR_OTHER (UT_READ | UT_VENDOR | UT_OTHER)
  56355. +#define UT_READ_VENDOR_ENDPOINT (UT_READ | UT_VENDOR | UT_ENDPOINT)
  56356. +#define UT_WRITE_VENDOR_DEVICE (UT_WRITE | UT_VENDOR | UT_DEVICE)
  56357. +#define UT_WRITE_VENDOR_INTERFACE (UT_WRITE | UT_VENDOR | UT_INTERFACE)
  56358. +#define UT_WRITE_VENDOR_OTHER (UT_WRITE | UT_VENDOR | UT_OTHER)
  56359. +#define UT_WRITE_VENDOR_ENDPOINT (UT_WRITE | UT_VENDOR | UT_ENDPOINT)
  56360. +
  56361. +/* Requests */
  56362. +#define UR_GET_STATUS 0x00
  56363. +#define USTAT_STANDARD_STATUS 0x00
  56364. +#define WUSTAT_WUSB_FEATURE 0x01
  56365. +#define WUSTAT_CHANNEL_INFO 0x02
  56366. +#define WUSTAT_RECEIVED_DATA 0x03
  56367. +#define WUSTAT_MAS_AVAILABILITY 0x04
  56368. +#define WUSTAT_CURRENT_TRANSMIT_POWER 0x05
  56369. +#define UR_CLEAR_FEATURE 0x01
  56370. +#define UR_SET_FEATURE 0x03
  56371. +#define UR_SET_AND_TEST_FEATURE 0x0c
  56372. +#define UR_SET_ADDRESS 0x05
  56373. +#define UR_GET_DESCRIPTOR 0x06
  56374. +#define UDESC_DEVICE 0x01
  56375. +#define UDESC_CONFIG 0x02
  56376. +#define UDESC_STRING 0x03
  56377. +#define UDESC_INTERFACE 0x04
  56378. +#define UDESC_ENDPOINT 0x05
  56379. +#define UDESC_SS_USB_COMPANION 0x30
  56380. +#define UDESC_DEVICE_QUALIFIER 0x06
  56381. +#define UDESC_OTHER_SPEED_CONFIGURATION 0x07
  56382. +#define UDESC_INTERFACE_POWER 0x08
  56383. +#define UDESC_OTG 0x09
  56384. +#define WUDESC_SECURITY 0x0c
  56385. +#define WUDESC_KEY 0x0d
  56386. +#define WUD_GET_KEY_INDEX(_wValue_) ((_wValue_) & 0xf)
  56387. +#define WUD_GET_KEY_TYPE(_wValue_) (((_wValue_) & 0x30) >> 4)
  56388. +#define WUD_KEY_TYPE_ASSOC 0x01
  56389. +#define WUD_KEY_TYPE_GTK 0x02
  56390. +#define WUD_GET_KEY_ORIGIN(_wValue_) (((_wValue_) & 0x40) >> 6)
  56391. +#define WUD_KEY_ORIGIN_HOST 0x00
  56392. +#define WUD_KEY_ORIGIN_DEVICE 0x01
  56393. +#define WUDESC_ENCRYPTION_TYPE 0x0e
  56394. +#define WUDESC_BOS 0x0f
  56395. +#define WUDESC_DEVICE_CAPABILITY 0x10
  56396. +#define WUDESC_WIRELESS_ENDPOINT_COMPANION 0x11
  56397. +#define UDESC_BOS 0x0f
  56398. +#define UDESC_DEVICE_CAPABILITY 0x10
  56399. +#define UDESC_CS_DEVICE 0x21 /* class specific */
  56400. +#define UDESC_CS_CONFIG 0x22
  56401. +#define UDESC_CS_STRING 0x23
  56402. +#define UDESC_CS_INTERFACE 0x24
  56403. +#define UDESC_CS_ENDPOINT 0x25
  56404. +#define UDESC_HUB 0x29
  56405. +#define UR_SET_DESCRIPTOR 0x07
  56406. +#define UR_GET_CONFIG 0x08
  56407. +#define UR_SET_CONFIG 0x09
  56408. +#define UR_GET_INTERFACE 0x0a
  56409. +#define UR_SET_INTERFACE 0x0b
  56410. +#define UR_SYNCH_FRAME 0x0c
  56411. +#define WUR_SET_ENCRYPTION 0x0d
  56412. +#define WUR_GET_ENCRYPTION 0x0e
  56413. +#define WUR_SET_HANDSHAKE 0x0f
  56414. +#define WUR_GET_HANDSHAKE 0x10
  56415. +#define WUR_SET_CONNECTION 0x11
  56416. +#define WUR_SET_SECURITY_DATA 0x12
  56417. +#define WUR_GET_SECURITY_DATA 0x13
  56418. +#define WUR_SET_WUSB_DATA 0x14
  56419. +#define WUDATA_DRPIE_INFO 0x01
  56420. +#define WUDATA_TRANSMIT_DATA 0x02
  56421. +#define WUDATA_TRANSMIT_PARAMS 0x03
  56422. +#define WUDATA_RECEIVE_PARAMS 0x04
  56423. +#define WUDATA_TRANSMIT_POWER 0x05
  56424. +#define WUR_LOOPBACK_DATA_WRITE 0x15
  56425. +#define WUR_LOOPBACK_DATA_READ 0x16
  56426. +#define WUR_SET_INTERFACE_DS 0x17
  56427. +
  56428. +/* Feature numbers */
  56429. +#define UF_ENDPOINT_HALT 0
  56430. +#define UF_DEVICE_REMOTE_WAKEUP 1
  56431. +#define UF_TEST_MODE 2
  56432. +#define UF_DEVICE_B_HNP_ENABLE 3
  56433. +#define UF_DEVICE_A_HNP_SUPPORT 4
  56434. +#define UF_DEVICE_A_ALT_HNP_SUPPORT 5
  56435. +#define WUF_WUSB 3
  56436. +#define WUF_TX_DRPIE 0x0
  56437. +#define WUF_DEV_XMIT_PACKET 0x1
  56438. +#define WUF_COUNT_PACKETS 0x2
  56439. +#define WUF_CAPTURE_PACKETS 0x3
  56440. +#define UF_FUNCTION_SUSPEND 0
  56441. +#define UF_U1_ENABLE 48
  56442. +#define UF_U2_ENABLE 49
  56443. +#define UF_LTM_ENABLE 50
  56444. +
  56445. +/* Class requests from the USB 2.0 hub spec, table 11-15 */
  56446. +#define UCR_CLEAR_HUB_FEATURE (0x2000 | UR_CLEAR_FEATURE)
  56447. +#define UCR_CLEAR_PORT_FEATURE (0x2300 | UR_CLEAR_FEATURE)
  56448. +#define UCR_GET_HUB_DESCRIPTOR (0xa000 | UR_GET_DESCRIPTOR)
  56449. +#define UCR_GET_HUB_STATUS (0xa000 | UR_GET_STATUS)
  56450. +#define UCR_GET_PORT_STATUS (0xa300 | UR_GET_STATUS)
  56451. +#define UCR_SET_HUB_FEATURE (0x2000 | UR_SET_FEATURE)
  56452. +#define UCR_SET_PORT_FEATURE (0x2300 | UR_SET_FEATURE)
  56453. +#define UCR_SET_AND_TEST_PORT_FEATURE (0xa300 | UR_SET_AND_TEST_FEATURE)
  56454. +
  56455. +#ifdef _MSC_VER
  56456. +#include <pshpack1.h>
  56457. +#endif
  56458. +
  56459. +typedef struct {
  56460. + uByte bLength;
  56461. + uByte bDescriptorType;
  56462. + uByte bDescriptorSubtype;
  56463. +} UPACKED usb_descriptor_t;
  56464. +
  56465. +typedef struct {
  56466. + uByte bLength;
  56467. + uByte bDescriptorType;
  56468. +} UPACKED usb_descriptor_header_t;
  56469. +
  56470. +typedef struct {
  56471. + uByte bLength;
  56472. + uByte bDescriptorType;
  56473. + uWord bcdUSB;
  56474. +#define UD_USB_2_0 0x0200
  56475. +#define UD_IS_USB2(d) (UGETW((d)->bcdUSB) >= UD_USB_2_0)
  56476. + uByte bDeviceClass;
  56477. + uByte bDeviceSubClass;
  56478. + uByte bDeviceProtocol;
  56479. + uByte bMaxPacketSize;
  56480. + /* The fields below are not part of the initial descriptor. */
  56481. + uWord idVendor;
  56482. + uWord idProduct;
  56483. + uWord bcdDevice;
  56484. + uByte iManufacturer;
  56485. + uByte iProduct;
  56486. + uByte iSerialNumber;
  56487. + uByte bNumConfigurations;
  56488. +} UPACKED usb_device_descriptor_t;
  56489. +#define USB_DEVICE_DESCRIPTOR_SIZE 18
  56490. +
  56491. +typedef struct {
  56492. + uByte bLength;
  56493. + uByte bDescriptorType;
  56494. + uWord wTotalLength;
  56495. + uByte bNumInterface;
  56496. + uByte bConfigurationValue;
  56497. + uByte iConfiguration;
  56498. +#define UC_ATT_ONE (1 << 7) /* must be set */
  56499. +#define UC_ATT_SELFPOWER (1 << 6) /* self powered */
  56500. +#define UC_ATT_WAKEUP (1 << 5) /* can wakeup */
  56501. +#define UC_ATT_BATTERY (1 << 4) /* battery powered */
  56502. + uByte bmAttributes;
  56503. +#define UC_BUS_POWERED 0x80
  56504. +#define UC_SELF_POWERED 0x40
  56505. +#define UC_REMOTE_WAKEUP 0x20
  56506. + uByte bMaxPower; /* max current in 2 mA units */
  56507. +#define UC_POWER_FACTOR 2
  56508. +} UPACKED usb_config_descriptor_t;
  56509. +#define USB_CONFIG_DESCRIPTOR_SIZE 9
  56510. +
  56511. +typedef struct {
  56512. + uByte bLength;
  56513. + uByte bDescriptorType;
  56514. + uByte bInterfaceNumber;
  56515. + uByte bAlternateSetting;
  56516. + uByte bNumEndpoints;
  56517. + uByte bInterfaceClass;
  56518. + uByte bInterfaceSubClass;
  56519. + uByte bInterfaceProtocol;
  56520. + uByte iInterface;
  56521. +} UPACKED usb_interface_descriptor_t;
  56522. +#define USB_INTERFACE_DESCRIPTOR_SIZE 9
  56523. +
  56524. +typedef struct {
  56525. + uByte bLength;
  56526. + uByte bDescriptorType;
  56527. + uByte bEndpointAddress;
  56528. +#define UE_GET_DIR(a) ((a) & 0x80)
  56529. +#define UE_SET_DIR(a,d) ((a) | (((d)&1) << 7))
  56530. +#define UE_DIR_IN 0x80
  56531. +#define UE_DIR_OUT 0x00
  56532. +#define UE_ADDR 0x0f
  56533. +#define UE_GET_ADDR(a) ((a) & UE_ADDR)
  56534. + uByte bmAttributes;
  56535. +#define UE_XFERTYPE 0x03
  56536. +#define UE_CONTROL 0x00
  56537. +#define UE_ISOCHRONOUS 0x01
  56538. +#define UE_BULK 0x02
  56539. +#define UE_INTERRUPT 0x03
  56540. +#define UE_GET_XFERTYPE(a) ((a) & UE_XFERTYPE)
  56541. +#define UE_ISO_TYPE 0x0c
  56542. +#define UE_ISO_ASYNC 0x04
  56543. +#define UE_ISO_ADAPT 0x08
  56544. +#define UE_ISO_SYNC 0x0c
  56545. +#define UE_GET_ISO_TYPE(a) ((a) & UE_ISO_TYPE)
  56546. + uWord wMaxPacketSize;
  56547. + uByte bInterval;
  56548. +} UPACKED usb_endpoint_descriptor_t;
  56549. +#define USB_ENDPOINT_DESCRIPTOR_SIZE 7
  56550. +
  56551. +typedef struct ss_endpoint_companion_descriptor {
  56552. + uByte bLength;
  56553. + uByte bDescriptorType;
  56554. + uByte bMaxBurst;
  56555. +#define USSE_GET_MAX_STREAMS(a) ((a) & 0x1f)
  56556. +#define USSE_SET_MAX_STREAMS(a, b) ((a) | ((b) & 0x1f))
  56557. +#define USSE_GET_MAX_PACKET_NUM(a) ((a) & 0x03)
  56558. +#define USSE_SET_MAX_PACKET_NUM(a, b) ((a) | ((b) & 0x03))
  56559. + uByte bmAttributes;
  56560. + uWord wBytesPerInterval;
  56561. +} UPACKED ss_endpoint_companion_descriptor_t;
  56562. +#define USB_SS_ENDPOINT_COMPANION_DESCRIPTOR_SIZE 6
  56563. +
  56564. +typedef struct {
  56565. + uByte bLength;
  56566. + uByte bDescriptorType;
  56567. + uWord bString[127];
  56568. +} UPACKED usb_string_descriptor_t;
  56569. +#define USB_MAX_STRING_LEN 128
  56570. +#define USB_LANGUAGE_TABLE 0 /* # of the string language id table */
  56571. +
  56572. +/* Hub specific request */
  56573. +#define UR_GET_BUS_STATE 0x02
  56574. +#define UR_CLEAR_TT_BUFFER 0x08
  56575. +#define UR_RESET_TT 0x09
  56576. +#define UR_GET_TT_STATE 0x0a
  56577. +#define UR_STOP_TT 0x0b
  56578. +
  56579. +/* Hub features */
  56580. +#define UHF_C_HUB_LOCAL_POWER 0
  56581. +#define UHF_C_HUB_OVER_CURRENT 1
  56582. +#define UHF_PORT_CONNECTION 0
  56583. +#define UHF_PORT_ENABLE 1
  56584. +#define UHF_PORT_SUSPEND 2
  56585. +#define UHF_PORT_OVER_CURRENT 3
  56586. +#define UHF_PORT_RESET 4
  56587. +#define UHF_PORT_L1 5
  56588. +#define UHF_PORT_POWER 8
  56589. +#define UHF_PORT_LOW_SPEED 9
  56590. +#define UHF_PORT_HIGH_SPEED 10
  56591. +#define UHF_C_PORT_CONNECTION 16
  56592. +#define UHF_C_PORT_ENABLE 17
  56593. +#define UHF_C_PORT_SUSPEND 18
  56594. +#define UHF_C_PORT_OVER_CURRENT 19
  56595. +#define UHF_C_PORT_RESET 20
  56596. +#define UHF_C_PORT_L1 23
  56597. +#define UHF_PORT_TEST 21
  56598. +#define UHF_PORT_INDICATOR 22
  56599. +
  56600. +typedef struct {
  56601. + uByte bDescLength;
  56602. + uByte bDescriptorType;
  56603. + uByte bNbrPorts;
  56604. + uWord wHubCharacteristics;
  56605. +#define UHD_PWR 0x0003
  56606. +#define UHD_PWR_GANGED 0x0000
  56607. +#define UHD_PWR_INDIVIDUAL 0x0001
  56608. +#define UHD_PWR_NO_SWITCH 0x0002
  56609. +#define UHD_COMPOUND 0x0004
  56610. +#define UHD_OC 0x0018
  56611. +#define UHD_OC_GLOBAL 0x0000
  56612. +#define UHD_OC_INDIVIDUAL 0x0008
  56613. +#define UHD_OC_NONE 0x0010
  56614. +#define UHD_TT_THINK 0x0060
  56615. +#define UHD_TT_THINK_8 0x0000
  56616. +#define UHD_TT_THINK_16 0x0020
  56617. +#define UHD_TT_THINK_24 0x0040
  56618. +#define UHD_TT_THINK_32 0x0060
  56619. +#define UHD_PORT_IND 0x0080
  56620. + uByte bPwrOn2PwrGood; /* delay in 2 ms units */
  56621. +#define UHD_PWRON_FACTOR 2
  56622. + uByte bHubContrCurrent;
  56623. + uByte DeviceRemovable[32]; /* max 255 ports */
  56624. +#define UHD_NOT_REMOV(desc, i) \
  56625. + (((desc)->DeviceRemovable[(i)/8] >> ((i) % 8)) & 1)
  56626. + /* deprecated */ uByte PortPowerCtrlMask[1];
  56627. +} UPACKED usb_hub_descriptor_t;
  56628. +#define USB_HUB_DESCRIPTOR_SIZE 9 /* includes deprecated PortPowerCtrlMask */
  56629. +
  56630. +typedef struct {
  56631. + uByte bLength;
  56632. + uByte bDescriptorType;
  56633. + uWord bcdUSB;
  56634. + uByte bDeviceClass;
  56635. + uByte bDeviceSubClass;
  56636. + uByte bDeviceProtocol;
  56637. + uByte bMaxPacketSize0;
  56638. + uByte bNumConfigurations;
  56639. + uByte bReserved;
  56640. +} UPACKED usb_device_qualifier_t;
  56641. +#define USB_DEVICE_QUALIFIER_SIZE 10
  56642. +
  56643. +typedef struct {
  56644. + uByte bLength;
  56645. + uByte bDescriptorType;
  56646. + uByte bmAttributes;
  56647. +#define UOTG_SRP 0x01
  56648. +#define UOTG_HNP 0x02
  56649. +} UPACKED usb_otg_descriptor_t;
  56650. +
  56651. +/* OTG feature selectors */
  56652. +#define UOTG_B_HNP_ENABLE 3
  56653. +#define UOTG_A_HNP_SUPPORT 4
  56654. +#define UOTG_A_ALT_HNP_SUPPORT 5
  56655. +
  56656. +typedef struct {
  56657. + uWord wStatus;
  56658. +/* Device status flags */
  56659. +#define UDS_SELF_POWERED 0x0001
  56660. +#define UDS_REMOTE_WAKEUP 0x0002
  56661. +/* Endpoint status flags */
  56662. +#define UES_HALT 0x0001
  56663. +} UPACKED usb_status_t;
  56664. +
  56665. +typedef struct {
  56666. + uWord wHubStatus;
  56667. +#define UHS_LOCAL_POWER 0x0001
  56668. +#define UHS_OVER_CURRENT 0x0002
  56669. + uWord wHubChange;
  56670. +} UPACKED usb_hub_status_t;
  56671. +
  56672. +typedef struct {
  56673. + uWord wPortStatus;
  56674. +#define UPS_CURRENT_CONNECT_STATUS 0x0001
  56675. +#define UPS_PORT_ENABLED 0x0002
  56676. +#define UPS_SUSPEND 0x0004
  56677. +#define UPS_OVERCURRENT_INDICATOR 0x0008
  56678. +#define UPS_RESET 0x0010
  56679. +#define UPS_PORT_POWER 0x0100
  56680. +#define UPS_LOW_SPEED 0x0200
  56681. +#define UPS_HIGH_SPEED 0x0400
  56682. +#define UPS_PORT_TEST 0x0800
  56683. +#define UPS_PORT_INDICATOR 0x1000
  56684. + uWord wPortChange;
  56685. +#define UPS_C_CONNECT_STATUS 0x0001
  56686. +#define UPS_C_PORT_ENABLED 0x0002
  56687. +#define UPS_C_SUSPEND 0x0004
  56688. +#define UPS_C_OVERCURRENT_INDICATOR 0x0008
  56689. +#define UPS_C_PORT_RESET 0x0010
  56690. +} UPACKED usb_port_status_t;
  56691. +
  56692. +#ifdef _MSC_VER
  56693. +#include <poppack.h>
  56694. +#endif
  56695. +
  56696. +/* Device class codes */
  56697. +#define UDCLASS_IN_INTERFACE 0x00
  56698. +#define UDCLASS_COMM 0x02
  56699. +#define UDCLASS_HUB 0x09
  56700. +#define UDSUBCLASS_HUB 0x00
  56701. +#define UDPROTO_FSHUB 0x00
  56702. +#define UDPROTO_HSHUBSTT 0x01
  56703. +#define UDPROTO_HSHUBMTT 0x02
  56704. +#define UDCLASS_DIAGNOSTIC 0xdc
  56705. +#define UDCLASS_WIRELESS 0xe0
  56706. +#define UDSUBCLASS_RF 0x01
  56707. +#define UDPROTO_BLUETOOTH 0x01
  56708. +#define UDCLASS_VENDOR 0xff
  56709. +
  56710. +/* Interface class codes */
  56711. +#define UICLASS_UNSPEC 0x00
  56712. +
  56713. +#define UICLASS_AUDIO 0x01
  56714. +#define UISUBCLASS_AUDIOCONTROL 1
  56715. +#define UISUBCLASS_AUDIOSTREAM 2
  56716. +#define UISUBCLASS_MIDISTREAM 3
  56717. +
  56718. +#define UICLASS_CDC 0x02 /* communication */
  56719. +#define UISUBCLASS_DIRECT_LINE_CONTROL_MODEL 1
  56720. +#define UISUBCLASS_ABSTRACT_CONTROL_MODEL 2
  56721. +#define UISUBCLASS_TELEPHONE_CONTROL_MODEL 3
  56722. +#define UISUBCLASS_MULTICHANNEL_CONTROL_MODEL 4
  56723. +#define UISUBCLASS_CAPI_CONTROLMODEL 5
  56724. +#define UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL 6
  56725. +#define UISUBCLASS_ATM_NETWORKING_CONTROL_MODEL 7
  56726. +#define UIPROTO_CDC_AT 1
  56727. +
  56728. +#define UICLASS_HID 0x03
  56729. +#define UISUBCLASS_BOOT 1
  56730. +#define UIPROTO_BOOT_KEYBOARD 1
  56731. +
  56732. +#define UICLASS_PHYSICAL 0x05
  56733. +
  56734. +#define UICLASS_IMAGE 0x06
  56735. +
  56736. +#define UICLASS_PRINTER 0x07
  56737. +#define UISUBCLASS_PRINTER 1
  56738. +#define UIPROTO_PRINTER_UNI 1
  56739. +#define UIPROTO_PRINTER_BI 2
  56740. +#define UIPROTO_PRINTER_1284 3
  56741. +
  56742. +#define UICLASS_MASS 0x08
  56743. +#define UISUBCLASS_RBC 1
  56744. +#define UISUBCLASS_SFF8020I 2
  56745. +#define UISUBCLASS_QIC157 3
  56746. +#define UISUBCLASS_UFI 4
  56747. +#define UISUBCLASS_SFF8070I 5
  56748. +#define UISUBCLASS_SCSI 6
  56749. +#define UIPROTO_MASS_CBI_I 0
  56750. +#define UIPROTO_MASS_CBI 1
  56751. +#define UIPROTO_MASS_BBB_OLD 2 /* Not in the spec anymore */
  56752. +#define UIPROTO_MASS_BBB 80 /* 'P' for the Iomega Zip drive */
  56753. +
  56754. +#define UICLASS_HUB 0x09
  56755. +#define UISUBCLASS_HUB 0
  56756. +#define UIPROTO_FSHUB 0
  56757. +#define UIPROTO_HSHUBSTT 0 /* Yes, same as previous */
  56758. +#define UIPROTO_HSHUBMTT 1
  56759. +
  56760. +#define UICLASS_CDC_DATA 0x0a
  56761. +#define UISUBCLASS_DATA 0
  56762. +#define UIPROTO_DATA_ISDNBRI 0x30 /* Physical iface */
  56763. +#define UIPROTO_DATA_HDLC 0x31 /* HDLC */
  56764. +#define UIPROTO_DATA_TRANSPARENT 0x32 /* Transparent */
  56765. +#define UIPROTO_DATA_Q921M 0x50 /* Management for Q921 */
  56766. +#define UIPROTO_DATA_Q921 0x51 /* Data for Q921 */
  56767. +#define UIPROTO_DATA_Q921TM 0x52 /* TEI multiplexer for Q921 */
  56768. +#define UIPROTO_DATA_V42BIS 0x90 /* Data compression */
  56769. +#define UIPROTO_DATA_Q931 0x91 /* Euro-ISDN */
  56770. +#define UIPROTO_DATA_V120 0x92 /* V.24 rate adaption */
  56771. +#define UIPROTO_DATA_CAPI 0x93 /* CAPI 2.0 commands */
  56772. +#define UIPROTO_DATA_HOST_BASED 0xfd /* Host based driver */
  56773. +#define UIPROTO_DATA_PUF 0xfe /* see Prot. Unit Func. Desc.*/
  56774. +#define UIPROTO_DATA_VENDOR 0xff /* Vendor specific */
  56775. +
  56776. +#define UICLASS_SMARTCARD 0x0b
  56777. +
  56778. +/*#define UICLASS_FIRM_UPD 0x0c*/
  56779. +
  56780. +#define UICLASS_SECURITY 0x0d
  56781. +
  56782. +#define UICLASS_DIAGNOSTIC 0xdc
  56783. +
  56784. +#define UICLASS_WIRELESS 0xe0
  56785. +#define UISUBCLASS_RF 0x01
  56786. +#define UIPROTO_BLUETOOTH 0x01
  56787. +
  56788. +#define UICLASS_APPL_SPEC 0xfe
  56789. +#define UISUBCLASS_FIRMWARE_DOWNLOAD 1
  56790. +#define UISUBCLASS_IRDA 2
  56791. +#define UIPROTO_IRDA 0
  56792. +
  56793. +#define UICLASS_VENDOR 0xff
  56794. +
  56795. +#define USB_HUB_MAX_DEPTH 5
  56796. +
  56797. +/*
  56798. + * Minimum time a device needs to be powered down to go through
  56799. + * a power cycle. XXX Are these time in the spec?
  56800. + */
  56801. +#define USB_POWER_DOWN_TIME 200 /* ms */
  56802. +#define USB_PORT_POWER_DOWN_TIME 100 /* ms */
  56803. +
  56804. +#if 0
  56805. +/* These are the values from the spec. */
  56806. +#define USB_PORT_RESET_DELAY 10 /* ms */
  56807. +#define USB_PORT_ROOT_RESET_DELAY 50 /* ms */
  56808. +#define USB_PORT_RESET_RECOVERY 10 /* ms */
  56809. +#define USB_PORT_POWERUP_DELAY 100 /* ms */
  56810. +#define USB_SET_ADDRESS_SETTLE 2 /* ms */
  56811. +#define USB_RESUME_DELAY (20*5) /* ms */
  56812. +#define USB_RESUME_WAIT 10 /* ms */
  56813. +#define USB_RESUME_RECOVERY 10 /* ms */
  56814. +#define USB_EXTRA_POWER_UP_TIME 0 /* ms */
  56815. +#else
  56816. +/* Allow for marginal (i.e. non-conforming) devices. */
  56817. +#define USB_PORT_RESET_DELAY 50 /* ms */
  56818. +#define USB_PORT_ROOT_RESET_DELAY 250 /* ms */
  56819. +#define USB_PORT_RESET_RECOVERY 250 /* ms */
  56820. +#define USB_PORT_POWERUP_DELAY 300 /* ms */
  56821. +#define USB_SET_ADDRESS_SETTLE 10 /* ms */
  56822. +#define USB_RESUME_DELAY (50*5) /* ms */
  56823. +#define USB_RESUME_WAIT 50 /* ms */
  56824. +#define USB_RESUME_RECOVERY 50 /* ms */
  56825. +#define USB_EXTRA_POWER_UP_TIME 20 /* ms */
  56826. +#endif
  56827. +
  56828. +#define USB_MIN_POWER 100 /* mA */
  56829. +#define USB_MAX_POWER 500 /* mA */
  56830. +
  56831. +#define USB_BUS_RESET_DELAY 100 /* ms XXX?*/
  56832. +
  56833. +#define USB_UNCONFIG_NO 0
  56834. +#define USB_UNCONFIG_INDEX (-1)
  56835. +
  56836. +/*** ioctl() related stuff ***/
  56837. +
  56838. +struct usb_ctl_request {
  56839. + int ucr_addr;
  56840. + usb_device_request_t ucr_request;
  56841. + void *ucr_data;
  56842. + int ucr_flags;
  56843. +#define USBD_SHORT_XFER_OK 0x04 /* allow short reads */
  56844. + int ucr_actlen; /* actual length transferred */
  56845. +};
  56846. +
  56847. +struct usb_alt_interface {
  56848. + int uai_config_index;
  56849. + int uai_interface_index;
  56850. + int uai_alt_no;
  56851. +};
  56852. +
  56853. +#define USB_CURRENT_CONFIG_INDEX (-1)
  56854. +#define USB_CURRENT_ALT_INDEX (-1)
  56855. +
  56856. +struct usb_config_desc {
  56857. + int ucd_config_index;
  56858. + usb_config_descriptor_t ucd_desc;
  56859. +};
  56860. +
  56861. +struct usb_interface_desc {
  56862. + int uid_config_index;
  56863. + int uid_interface_index;
  56864. + int uid_alt_index;
  56865. + usb_interface_descriptor_t uid_desc;
  56866. +};
  56867. +
  56868. +struct usb_endpoint_desc {
  56869. + int ued_config_index;
  56870. + int ued_interface_index;
  56871. + int ued_alt_index;
  56872. + int ued_endpoint_index;
  56873. + usb_endpoint_descriptor_t ued_desc;
  56874. +};
  56875. +
  56876. +struct usb_full_desc {
  56877. + int ufd_config_index;
  56878. + u_int ufd_size;
  56879. + u_char *ufd_data;
  56880. +};
  56881. +
  56882. +struct usb_string_desc {
  56883. + int usd_string_index;
  56884. + int usd_language_id;
  56885. + usb_string_descriptor_t usd_desc;
  56886. +};
  56887. +
  56888. +struct usb_ctl_report_desc {
  56889. + int ucrd_size;
  56890. + u_char ucrd_data[1024]; /* filled data size will vary */
  56891. +};
  56892. +
  56893. +typedef struct { u_int32_t cookie; } usb_event_cookie_t;
  56894. +
  56895. +#define USB_MAX_DEVNAMES 4
  56896. +#define USB_MAX_DEVNAMELEN 16
  56897. +struct usb_device_info {
  56898. + u_int8_t udi_bus;
  56899. + u_int8_t udi_addr; /* device address */
  56900. + usb_event_cookie_t udi_cookie;
  56901. + char udi_product[USB_MAX_STRING_LEN];
  56902. + char udi_vendor[USB_MAX_STRING_LEN];
  56903. + char udi_release[8];
  56904. + u_int16_t udi_productNo;
  56905. + u_int16_t udi_vendorNo;
  56906. + u_int16_t udi_releaseNo;
  56907. + u_int8_t udi_class;
  56908. + u_int8_t udi_subclass;
  56909. + u_int8_t udi_protocol;
  56910. + u_int8_t udi_config;
  56911. + u_int8_t udi_speed;
  56912. +#define USB_SPEED_UNKNOWN 0
  56913. +#define USB_SPEED_LOW 1
  56914. +#define USB_SPEED_FULL 2
  56915. +#define USB_SPEED_HIGH 3
  56916. +#define USB_SPEED_VARIABLE 4
  56917. +#define USB_SPEED_SUPER 5
  56918. + int udi_power; /* power consumption in mA, 0 if selfpowered */
  56919. + int udi_nports;
  56920. + char udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN];
  56921. + u_int8_t udi_ports[16];/* hub only: addresses of devices on ports */
  56922. +#define USB_PORT_ENABLED 0xff
  56923. +#define USB_PORT_SUSPENDED 0xfe
  56924. +#define USB_PORT_POWERED 0xfd
  56925. +#define USB_PORT_DISABLED 0xfc
  56926. +};
  56927. +
  56928. +struct usb_ctl_report {
  56929. + int ucr_report;
  56930. + u_char ucr_data[1024]; /* filled data size will vary */
  56931. +};
  56932. +
  56933. +struct usb_device_stats {
  56934. + u_long uds_requests[4]; /* indexed by transfer type UE_* */
  56935. +};
  56936. +
  56937. +#define WUSB_MIN_IE 0x80
  56938. +#define WUSB_WCTA_IE 0x80
  56939. +#define WUSB_WCONNECTACK_IE 0x81
  56940. +#define WUSB_WHOSTINFO_IE 0x82
  56941. +#define WUHI_GET_CA(_bmAttributes_) ((_bmAttributes_) & 0x3)
  56942. +#define WUHI_CA_RECONN 0x00
  56943. +#define WUHI_CA_LIMITED 0x01
  56944. +#define WUHI_CA_ALL 0x03
  56945. +#define WUHI_GET_MLSI(_bmAttributes_) (((_bmAttributes_) & 0x38) >> 3)
  56946. +#define WUSB_WCHCHANGEANNOUNCE_IE 0x83
  56947. +#define WUSB_WDEV_DISCONNECT_IE 0x84
  56948. +#define WUSB_WHOST_DISCONNECT_IE 0x85
  56949. +#define WUSB_WRELEASE_CHANNEL_IE 0x86
  56950. +#define WUSB_WWORK_IE 0x87
  56951. +#define WUSB_WCHANNEL_STOP_IE 0x88
  56952. +#define WUSB_WDEV_KEEPALIVE_IE 0x89
  56953. +#define WUSB_WISOCH_DISCARD_IE 0x8A
  56954. +#define WUSB_WRESETDEVICE_IE 0x8B
  56955. +#define WUSB_WXMIT_PACKET_ADJUST_IE 0x8C
  56956. +#define WUSB_MAX_IE 0x8C
  56957. +
  56958. +/* Device Notification Types */
  56959. +
  56960. +#define WUSB_DN_MIN 0x01
  56961. +#define WUSB_DN_CONNECT 0x01
  56962. +# define WUSB_DA_OLDCONN 0x00
  56963. +# define WUSB_DA_NEWCONN 0x01
  56964. +# define WUSB_DA_SELF_BEACON 0x02
  56965. +# define WUSB_DA_DIR_BEACON 0x04
  56966. +# define WUSB_DA_NO_BEACON 0x06
  56967. +#define WUSB_DN_DISCONNECT 0x02
  56968. +#define WUSB_DN_EPRDY 0x03
  56969. +#define WUSB_DN_MASAVAILCHANGED 0x04
  56970. +#define WUSB_DN_REMOTEWAKEUP 0x05
  56971. +#define WUSB_DN_SLEEP 0x06
  56972. +#define WUSB_DN_ALIVE 0x07
  56973. +#define WUSB_DN_MAX 0x07
  56974. +
  56975. +#ifdef _MSC_VER
  56976. +#include <pshpack1.h>
  56977. +#endif
  56978. +
  56979. +/* WUSB Handshake Data. Used during the SET/GET HANDSHAKE requests */
  56980. +typedef struct wusb_hndshk_data {
  56981. + uByte bMessageNumber;
  56982. + uByte bStatus;
  56983. + uByte tTKID[3];
  56984. + uByte bReserved;
  56985. + uByte CDID[16];
  56986. + uByte Nonce[16];
  56987. + uByte MIC[8];
  56988. +} UPACKED wusb_hndshk_data_t;
  56989. +#define WUSB_HANDSHAKE_LEN_FOR_MIC 38
  56990. +
  56991. +/* WUSB Connection Context */
  56992. +typedef struct wusb_conn_context {
  56993. + uByte CHID [16];
  56994. + uByte CDID [16];
  56995. + uByte CK [16];
  56996. +} UPACKED wusb_conn_context_t;
  56997. +
  56998. +/* WUSB Security Descriptor */
  56999. +typedef struct wusb_security_desc {
  57000. + uByte bLength;
  57001. + uByte bDescriptorType;
  57002. + uWord wTotalLength;
  57003. + uByte bNumEncryptionTypes;
  57004. +} UPACKED wusb_security_desc_t;
  57005. +
  57006. +/* WUSB Encryption Type Descriptor */
  57007. +typedef struct wusb_encrypt_type_desc {
  57008. + uByte bLength;
  57009. + uByte bDescriptorType;
  57010. +
  57011. + uByte bEncryptionType;
  57012. +#define WUETD_UNSECURE 0
  57013. +#define WUETD_WIRED 1
  57014. +#define WUETD_CCM_1 2
  57015. +#define WUETD_RSA_1 3
  57016. +
  57017. + uByte bEncryptionValue;
  57018. + uByte bAuthKeyIndex;
  57019. +} UPACKED wusb_encrypt_type_desc_t;
  57020. +
  57021. +/* WUSB Key Descriptor */
  57022. +typedef struct wusb_key_desc {
  57023. + uByte bLength;
  57024. + uByte bDescriptorType;
  57025. + uByte tTKID[3];
  57026. + uByte bReserved;
  57027. + uByte KeyData[1]; /* variable length */
  57028. +} UPACKED wusb_key_desc_t;
  57029. +
  57030. +/* WUSB BOS Descriptor (Binary device Object Store) */
  57031. +typedef struct wusb_bos_desc {
  57032. + uByte bLength;
  57033. + uByte bDescriptorType;
  57034. + uWord wTotalLength;
  57035. + uByte bNumDeviceCaps;
  57036. +} UPACKED wusb_bos_desc_t;
  57037. +
  57038. +#define USB_DEVICE_CAPABILITY_20_EXTENSION 0x02
  57039. +typedef struct usb_dev_cap_20_ext_desc {
  57040. + uByte bLength;
  57041. + uByte bDescriptorType;
  57042. + uByte bDevCapabilityType;
  57043. +#define USB_20_EXT_LPM 0x02
  57044. + uDWord bmAttributes;
  57045. +} UPACKED usb_dev_cap_20_ext_desc_t;
  57046. +
  57047. +#define USB_DEVICE_CAPABILITY_SS_USB 0x03
  57048. +typedef struct usb_dev_cap_ss_usb {
  57049. + uByte bLength;
  57050. + uByte bDescriptorType;
  57051. + uByte bDevCapabilityType;
  57052. +#define USB_DC_SS_USB_LTM_CAPABLE 0x02
  57053. + uByte bmAttributes;
  57054. +#define USB_DC_SS_USB_SPEED_SUPPORT_LOW 0x01
  57055. +#define USB_DC_SS_USB_SPEED_SUPPORT_FULL 0x02
  57056. +#define USB_DC_SS_USB_SPEED_SUPPORT_HIGH 0x04
  57057. +#define USB_DC_SS_USB_SPEED_SUPPORT_SS 0x08
  57058. + uWord wSpeedsSupported;
  57059. + uByte bFunctionalitySupport;
  57060. + uByte bU1DevExitLat;
  57061. + uWord wU2DevExitLat;
  57062. +} UPACKED usb_dev_cap_ss_usb_t;
  57063. +
  57064. +#define USB_DEVICE_CAPABILITY_CONTAINER_ID 0x04
  57065. +typedef struct usb_dev_cap_container_id {
  57066. + uByte bLength;
  57067. + uByte bDescriptorType;
  57068. + uByte bDevCapabilityType;
  57069. + uByte bReserved;
  57070. + uByte containerID[16];
  57071. +} UPACKED usb_dev_cap_container_id_t;
  57072. +
  57073. +/* Device Capability Type Codes */
  57074. +#define WUSB_DEVICE_CAPABILITY_WIRELESS_USB 0x01
  57075. +
  57076. +/* Device Capability Descriptor */
  57077. +typedef struct wusb_dev_cap_desc {
  57078. + uByte bLength;
  57079. + uByte bDescriptorType;
  57080. + uByte bDevCapabilityType;
  57081. + uByte caps[1]; /* Variable length */
  57082. +} UPACKED wusb_dev_cap_desc_t;
  57083. +
  57084. +/* Device Capability Descriptor */
  57085. +typedef struct wusb_dev_cap_uwb_desc {
  57086. + uByte bLength;
  57087. + uByte bDescriptorType;
  57088. + uByte bDevCapabilityType;
  57089. + uByte bmAttributes;
  57090. + uWord wPHYRates; /* Bitmap */
  57091. + uByte bmTFITXPowerInfo;
  57092. + uByte bmFFITXPowerInfo;
  57093. + uWord bmBandGroup;
  57094. + uByte bReserved;
  57095. +} UPACKED wusb_dev_cap_uwb_desc_t;
  57096. +
  57097. +/* Wireless USB Endpoint Companion Descriptor */
  57098. +typedef struct wusb_endpoint_companion_desc {
  57099. + uByte bLength;
  57100. + uByte bDescriptorType;
  57101. + uByte bMaxBurst;
  57102. + uByte bMaxSequence;
  57103. + uWord wMaxStreamDelay;
  57104. + uWord wOverTheAirPacketSize;
  57105. + uByte bOverTheAirInterval;
  57106. + uByte bmCompAttributes;
  57107. +} UPACKED wusb_endpoint_companion_desc_t;
  57108. +
  57109. +/* Wireless USB Numeric Association M1 Data Structure */
  57110. +typedef struct wusb_m1_data {
  57111. + uByte version;
  57112. + uWord langId;
  57113. + uByte deviceFriendlyNameLength;
  57114. + uByte sha_256_m3[32];
  57115. + uByte deviceFriendlyName[256];
  57116. +} UPACKED wusb_m1_data_t;
  57117. +
  57118. +typedef struct wusb_m2_data {
  57119. + uByte version;
  57120. + uWord langId;
  57121. + uByte hostFriendlyNameLength;
  57122. + uByte pkh[384];
  57123. + uByte hostFriendlyName[256];
  57124. +} UPACKED wusb_m2_data_t;
  57125. +
  57126. +typedef struct wusb_m3_data {
  57127. + uByte pkd[384];
  57128. + uByte nd;
  57129. +} UPACKED wusb_m3_data_t;
  57130. +
  57131. +typedef struct wusb_m4_data {
  57132. + uDWord _attributeTypeIdAndLength_1;
  57133. + uWord associationTypeId;
  57134. +
  57135. + uDWord _attributeTypeIdAndLength_2;
  57136. + uWord associationSubTypeId;
  57137. +
  57138. + uDWord _attributeTypeIdAndLength_3;
  57139. + uDWord length;
  57140. +
  57141. + uDWord _attributeTypeIdAndLength_4;
  57142. + uDWord associationStatus;
  57143. +
  57144. + uDWord _attributeTypeIdAndLength_5;
  57145. + uByte chid[16];
  57146. +
  57147. + uDWord _attributeTypeIdAndLength_6;
  57148. + uByte cdid[16];
  57149. +
  57150. + uDWord _attributeTypeIdAndLength_7;
  57151. + uByte bandGroups[2];
  57152. +} UPACKED wusb_m4_data_t;
  57153. +
  57154. +#ifdef _MSC_VER
  57155. +#include <poppack.h>
  57156. +#endif
  57157. +
  57158. +#ifdef __cplusplus
  57159. +}
  57160. +#endif
  57161. +
  57162. +#endif /* _USB_H_ */
  57163. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/doc/doxygen.cfg linux-3.12.33-rpi/drivers/usb/host/dwc_otg/doc/doxygen.cfg
  57164. --- linux-3.12.33/drivers/usb/host/dwc_otg/doc/doxygen.cfg 1969-12-31 18:00:00.000000000 -0600
  57165. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/doc/doxygen.cfg 2014-12-03 19:13:40.216418001 -0600
  57166. @@ -0,0 +1,224 @@
  57167. +# Doxyfile 1.3.9.1
  57168. +
  57169. +#---------------------------------------------------------------------------
  57170. +# Project related configuration options
  57171. +#---------------------------------------------------------------------------
  57172. +PROJECT_NAME = "DesignWare USB 2.0 OTG Controller (DWC_otg) Device Driver"
  57173. +PROJECT_NUMBER = v3.00a
  57174. +OUTPUT_DIRECTORY = ./doc/
  57175. +CREATE_SUBDIRS = NO
  57176. +OUTPUT_LANGUAGE = English
  57177. +BRIEF_MEMBER_DESC = YES
  57178. +REPEAT_BRIEF = YES
  57179. +ABBREVIATE_BRIEF = "The $name class" \
  57180. + "The $name widget" \
  57181. + "The $name file" \
  57182. + is \
  57183. + provides \
  57184. + specifies \
  57185. + contains \
  57186. + represents \
  57187. + a \
  57188. + an \
  57189. + the
  57190. +ALWAYS_DETAILED_SEC = NO
  57191. +INLINE_INHERITED_MEMB = NO
  57192. +FULL_PATH_NAMES = NO
  57193. +STRIP_FROM_PATH =
  57194. +STRIP_FROM_INC_PATH =
  57195. +SHORT_NAMES = NO
  57196. +JAVADOC_AUTOBRIEF = YES
  57197. +MULTILINE_CPP_IS_BRIEF = NO
  57198. +INHERIT_DOCS = YES
  57199. +DISTRIBUTE_GROUP_DOC = NO
  57200. +TAB_SIZE = 8
  57201. +ALIASES =
  57202. +OPTIMIZE_OUTPUT_FOR_C = YES
  57203. +OPTIMIZE_OUTPUT_JAVA = NO
  57204. +SUBGROUPING = YES
  57205. +#---------------------------------------------------------------------------
  57206. +# Build related configuration options
  57207. +#---------------------------------------------------------------------------
  57208. +EXTRACT_ALL = NO
  57209. +EXTRACT_PRIVATE = YES
  57210. +EXTRACT_STATIC = YES
  57211. +EXTRACT_LOCAL_CLASSES = YES
  57212. +EXTRACT_LOCAL_METHODS = NO
  57213. +HIDE_UNDOC_MEMBERS = NO
  57214. +HIDE_UNDOC_CLASSES = NO
  57215. +HIDE_FRIEND_COMPOUNDS = NO
  57216. +HIDE_IN_BODY_DOCS = NO
  57217. +INTERNAL_DOCS = NO
  57218. +CASE_SENSE_NAMES = NO
  57219. +HIDE_SCOPE_NAMES = NO
  57220. +SHOW_INCLUDE_FILES = YES
  57221. +INLINE_INFO = YES
  57222. +SORT_MEMBER_DOCS = NO
  57223. +SORT_BRIEF_DOCS = NO
  57224. +SORT_BY_SCOPE_NAME = NO
  57225. +GENERATE_TODOLIST = YES
  57226. +GENERATE_TESTLIST = YES
  57227. +GENERATE_BUGLIST = YES
  57228. +GENERATE_DEPRECATEDLIST= YES
  57229. +ENABLED_SECTIONS =
  57230. +MAX_INITIALIZER_LINES = 30
  57231. +SHOW_USED_FILES = YES
  57232. +SHOW_DIRECTORIES = YES
  57233. +#---------------------------------------------------------------------------
  57234. +# configuration options related to warning and progress messages
  57235. +#---------------------------------------------------------------------------
  57236. +QUIET = YES
  57237. +WARNINGS = YES
  57238. +WARN_IF_UNDOCUMENTED = NO
  57239. +WARN_IF_DOC_ERROR = YES
  57240. +WARN_FORMAT = "$file:$line: $text"
  57241. +WARN_LOGFILE =
  57242. +#---------------------------------------------------------------------------
  57243. +# configuration options related to the input files
  57244. +#---------------------------------------------------------------------------
  57245. +INPUT = .
  57246. +FILE_PATTERNS = *.c \
  57247. + *.h \
  57248. + ./linux/*.c \
  57249. + ./linux/*.h
  57250. +RECURSIVE = NO
  57251. +EXCLUDE = ./test/ \
  57252. + ./dwc_otg/.AppleDouble/
  57253. +EXCLUDE_SYMLINKS = YES
  57254. +EXCLUDE_PATTERNS = *.mod.*
  57255. +EXAMPLE_PATH =
  57256. +EXAMPLE_PATTERNS = *
  57257. +EXAMPLE_RECURSIVE = NO
  57258. +IMAGE_PATH =
  57259. +INPUT_FILTER =
  57260. +FILTER_PATTERNS =
  57261. +FILTER_SOURCE_FILES = NO
  57262. +#---------------------------------------------------------------------------
  57263. +# configuration options related to source browsing
  57264. +#---------------------------------------------------------------------------
  57265. +SOURCE_BROWSER = YES
  57266. +INLINE_SOURCES = NO
  57267. +STRIP_CODE_COMMENTS = YES
  57268. +REFERENCED_BY_RELATION = NO
  57269. +REFERENCES_RELATION = NO
  57270. +VERBATIM_HEADERS = NO
  57271. +#---------------------------------------------------------------------------
  57272. +# configuration options related to the alphabetical class index
  57273. +#---------------------------------------------------------------------------
  57274. +ALPHABETICAL_INDEX = NO
  57275. +COLS_IN_ALPHA_INDEX = 5
  57276. +IGNORE_PREFIX =
  57277. +#---------------------------------------------------------------------------
  57278. +# configuration options related to the HTML output
  57279. +#---------------------------------------------------------------------------
  57280. +GENERATE_HTML = YES
  57281. +HTML_OUTPUT = html
  57282. +HTML_FILE_EXTENSION = .html
  57283. +HTML_HEADER =
  57284. +HTML_FOOTER =
  57285. +HTML_STYLESHEET =
  57286. +HTML_ALIGN_MEMBERS = YES
  57287. +GENERATE_HTMLHELP = NO
  57288. +CHM_FILE =
  57289. +HHC_LOCATION =
  57290. +GENERATE_CHI = NO
  57291. +BINARY_TOC = NO
  57292. +TOC_EXPAND = NO
  57293. +DISABLE_INDEX = NO
  57294. +ENUM_VALUES_PER_LINE = 4
  57295. +GENERATE_TREEVIEW = YES
  57296. +TREEVIEW_WIDTH = 250
  57297. +#---------------------------------------------------------------------------
  57298. +# configuration options related to the LaTeX output
  57299. +#---------------------------------------------------------------------------
  57300. +GENERATE_LATEX = NO
  57301. +LATEX_OUTPUT = latex
  57302. +LATEX_CMD_NAME = latex
  57303. +MAKEINDEX_CMD_NAME = makeindex
  57304. +COMPACT_LATEX = NO
  57305. +PAPER_TYPE = a4wide
  57306. +EXTRA_PACKAGES =
  57307. +LATEX_HEADER =
  57308. +PDF_HYPERLINKS = NO
  57309. +USE_PDFLATEX = NO
  57310. +LATEX_BATCHMODE = NO
  57311. +LATEX_HIDE_INDICES = NO
  57312. +#---------------------------------------------------------------------------
  57313. +# configuration options related to the RTF output
  57314. +#---------------------------------------------------------------------------
  57315. +GENERATE_RTF = NO
  57316. +RTF_OUTPUT = rtf
  57317. +COMPACT_RTF = NO
  57318. +RTF_HYPERLINKS = NO
  57319. +RTF_STYLESHEET_FILE =
  57320. +RTF_EXTENSIONS_FILE =
  57321. +#---------------------------------------------------------------------------
  57322. +# configuration options related to the man page output
  57323. +#---------------------------------------------------------------------------
  57324. +GENERATE_MAN = NO
  57325. +MAN_OUTPUT = man
  57326. +MAN_EXTENSION = .3
  57327. +MAN_LINKS = NO
  57328. +#---------------------------------------------------------------------------
  57329. +# configuration options related to the XML output
  57330. +#---------------------------------------------------------------------------
  57331. +GENERATE_XML = NO
  57332. +XML_OUTPUT = xml
  57333. +XML_SCHEMA =
  57334. +XML_DTD =
  57335. +XML_PROGRAMLISTING = YES
  57336. +#---------------------------------------------------------------------------
  57337. +# configuration options for the AutoGen Definitions output
  57338. +#---------------------------------------------------------------------------
  57339. +GENERATE_AUTOGEN_DEF = NO
  57340. +#---------------------------------------------------------------------------
  57341. +# configuration options related to the Perl module output
  57342. +#---------------------------------------------------------------------------
  57343. +GENERATE_PERLMOD = NO
  57344. +PERLMOD_LATEX = NO
  57345. +PERLMOD_PRETTY = YES
  57346. +PERLMOD_MAKEVAR_PREFIX =
  57347. +#---------------------------------------------------------------------------
  57348. +# Configuration options related to the preprocessor
  57349. +#---------------------------------------------------------------------------
  57350. +ENABLE_PREPROCESSING = YES
  57351. +MACRO_EXPANSION = YES
  57352. +EXPAND_ONLY_PREDEF = YES
  57353. +SEARCH_INCLUDES = YES
  57354. +INCLUDE_PATH =
  57355. +INCLUDE_FILE_PATTERNS =
  57356. +PREDEFINED = DEVICE_ATTR DWC_EN_ISOC
  57357. +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
  57358. +SKIP_FUNCTION_MACROS = NO
  57359. +#---------------------------------------------------------------------------
  57360. +# Configuration::additions related to external references
  57361. +#---------------------------------------------------------------------------
  57362. +TAGFILES =
  57363. +GENERATE_TAGFILE =
  57364. +ALLEXTERNALS = NO
  57365. +EXTERNAL_GROUPS = YES
  57366. +PERL_PATH = /usr/bin/perl
  57367. +#---------------------------------------------------------------------------
  57368. +# Configuration options related to the dot tool
  57369. +#---------------------------------------------------------------------------
  57370. +CLASS_DIAGRAMS = YES
  57371. +HIDE_UNDOC_RELATIONS = YES
  57372. +HAVE_DOT = NO
  57373. +CLASS_GRAPH = YES
  57374. +COLLABORATION_GRAPH = YES
  57375. +UML_LOOK = NO
  57376. +TEMPLATE_RELATIONS = NO
  57377. +INCLUDE_GRAPH = YES
  57378. +INCLUDED_BY_GRAPH = YES
  57379. +CALL_GRAPH = NO
  57380. +GRAPHICAL_HIERARCHY = YES
  57381. +DOT_IMAGE_FORMAT = png
  57382. +DOT_PATH =
  57383. +DOTFILE_DIRS =
  57384. +MAX_DOT_GRAPH_DEPTH = 1000
  57385. +GENERATE_LEGEND = YES
  57386. +DOT_CLEANUP = YES
  57387. +#---------------------------------------------------------------------------
  57388. +# Configuration::additions related to the search engine
  57389. +#---------------------------------------------------------------------------
  57390. +SEARCHENGINE = NO
  57391. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dummy_audio.c linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dummy_audio.c
  57392. --- linux-3.12.33/drivers/usb/host/dwc_otg/dummy_audio.c 1969-12-31 18:00:00.000000000 -0600
  57393. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dummy_audio.c 2014-12-03 19:13:40.216418001 -0600
  57394. @@ -0,0 +1,1575 @@
  57395. +/*
  57396. + * zero.c -- Gadget Zero, for USB development
  57397. + *
  57398. + * Copyright (C) 2003-2004 David Brownell
  57399. + * All rights reserved.
  57400. + *
  57401. + * Redistribution and use in source and binary forms, with or without
  57402. + * modification, are permitted provided that the following conditions
  57403. + * are met:
  57404. + * 1. Redistributions of source code must retain the above copyright
  57405. + * notice, this list of conditions, and the following disclaimer,
  57406. + * without modification.
  57407. + * 2. Redistributions in binary form must reproduce the above copyright
  57408. + * notice, this list of conditions and the following disclaimer in the
  57409. + * documentation and/or other materials provided with the distribution.
  57410. + * 3. The names of the above-listed copyright holders may not be used
  57411. + * to endorse or promote products derived from this software without
  57412. + * specific prior written permission.
  57413. + *
  57414. + * ALTERNATIVELY, this software may be distributed under the terms of the
  57415. + * GNU General Public License ("GPL") as published by the Free Software
  57416. + * Foundation, either version 2 of that License or (at your option) any
  57417. + * later version.
  57418. + *
  57419. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  57420. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  57421. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  57422. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  57423. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  57424. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  57425. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  57426. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  57427. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  57428. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  57429. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  57430. + */
  57431. +
  57432. +
  57433. +/*
  57434. + * Gadget Zero only needs two bulk endpoints, and is an example of how you
  57435. + * can write a hardware-agnostic gadget driver running inside a USB device.
  57436. + *
  57437. + * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
  57438. + * affect most of the driver.
  57439. + *
  57440. + * Use it with the Linux host/master side "usbtest" driver to get a basic
  57441. + * functional test of your device-side usb stack, or with "usb-skeleton".
  57442. + *
  57443. + * It supports two similar configurations. One sinks whatever the usb host
  57444. + * writes, and in return sources zeroes. The other loops whatever the host
  57445. + * writes back, so the host can read it. Module options include:
  57446. + *
  57447. + * buflen=N default N=4096, buffer size used
  57448. + * qlen=N default N=32, how many buffers in the loopback queue
  57449. + * loopdefault default false, list loopback config first
  57450. + *
  57451. + * Many drivers will only have one configuration, letting them be much
  57452. + * simpler if they also don't support high speed operation (like this
  57453. + * driver does).
  57454. + */
  57455. +
  57456. +#include <linux/config.h>
  57457. +#include <linux/module.h>
  57458. +#include <linux/kernel.h>
  57459. +#include <linux/delay.h>
  57460. +#include <linux/ioport.h>
  57461. +#include <linux/sched.h>
  57462. +#include <linux/slab.h>
  57463. +#include <linux/smp_lock.h>
  57464. +#include <linux/errno.h>
  57465. +#include <linux/init.h>
  57466. +#include <linux/timer.h>
  57467. +#include <linux/list.h>
  57468. +#include <linux/interrupt.h>
  57469. +#include <linux/uts.h>
  57470. +#include <linux/version.h>
  57471. +#include <linux/device.h>
  57472. +#include <linux/moduleparam.h>
  57473. +#include <linux/proc_fs.h>
  57474. +
  57475. +#include <asm/byteorder.h>
  57476. +#include <asm/io.h>
  57477. +#include <asm/irq.h>
  57478. +#include <asm/system.h>
  57479. +#include <asm/unaligned.h>
  57480. +
  57481. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  57482. +# include <linux/usb/ch9.h>
  57483. +#else
  57484. +# include <linux/usb_ch9.h>
  57485. +#endif
  57486. +
  57487. +#include <linux/usb_gadget.h>
  57488. +
  57489. +
  57490. +/*-------------------------------------------------------------------------*/
  57491. +/*-------------------------------------------------------------------------*/
  57492. +
  57493. +
  57494. +static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
  57495. +{
  57496. + int count = 0;
  57497. + u8 c;
  57498. + u16 uchar;
  57499. +
  57500. + /* this insists on correct encodings, though not minimal ones.
  57501. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  57502. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  57503. + */
  57504. + while (len != 0 && (c = (u8) *s++) != 0) {
  57505. + if (unlikely(c & 0x80)) {
  57506. + // 2-byte sequence:
  57507. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  57508. + if ((c & 0xe0) == 0xc0) {
  57509. + uchar = (c & 0x1f) << 6;
  57510. +
  57511. + c = (u8) *s++;
  57512. + if ((c & 0xc0) != 0xc0)
  57513. + goto fail;
  57514. + c &= 0x3f;
  57515. + uchar |= c;
  57516. +
  57517. + // 3-byte sequence (most CJKV characters):
  57518. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  57519. + } else if ((c & 0xf0) == 0xe0) {
  57520. + uchar = (c & 0x0f) << 12;
  57521. +
  57522. + c = (u8) *s++;
  57523. + if ((c & 0xc0) != 0xc0)
  57524. + goto fail;
  57525. + c &= 0x3f;
  57526. + uchar |= c << 6;
  57527. +
  57528. + c = (u8) *s++;
  57529. + if ((c & 0xc0) != 0xc0)
  57530. + goto fail;
  57531. + c &= 0x3f;
  57532. + uchar |= c;
  57533. +
  57534. + /* no bogus surrogates */
  57535. + if (0xd800 <= uchar && uchar <= 0xdfff)
  57536. + goto fail;
  57537. +
  57538. + // 4-byte sequence (surrogate pairs, currently rare):
  57539. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  57540. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  57541. + // (uuuuu = wwww + 1)
  57542. + // FIXME accept the surrogate code points (only)
  57543. +
  57544. + } else
  57545. + goto fail;
  57546. + } else
  57547. + uchar = c;
  57548. + put_unaligned (cpu_to_le16 (uchar), cp++);
  57549. + count++;
  57550. + len--;
  57551. + }
  57552. + return count;
  57553. +fail:
  57554. + return -1;
  57555. +}
  57556. +
  57557. +
  57558. +/**
  57559. + * usb_gadget_get_string - fill out a string descriptor
  57560. + * @table: of c strings encoded using UTF-8
  57561. + * @id: string id, from low byte of wValue in get string descriptor
  57562. + * @buf: at least 256 bytes
  57563. + *
  57564. + * Finds the UTF-8 string matching the ID, and converts it into a
  57565. + * string descriptor in utf16-le.
  57566. + * Returns length of descriptor (always even) or negative errno
  57567. + *
  57568. + * If your driver needs stings in multiple languages, you'll probably
  57569. + * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
  57570. + * using this routine after choosing which set of UTF-8 strings to use.
  57571. + * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
  57572. + * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
  57573. + * characters (which are also widely used in C strings).
  57574. + */
  57575. +int
  57576. +usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
  57577. +{
  57578. + struct usb_string *s;
  57579. + int len;
  57580. +
  57581. + /* descriptor 0 has the language id */
  57582. + if (id == 0) {
  57583. + buf [0] = 4;
  57584. + buf [1] = USB_DT_STRING;
  57585. + buf [2] = (u8) table->language;
  57586. + buf [3] = (u8) (table->language >> 8);
  57587. + return 4;
  57588. + }
  57589. + for (s = table->strings; s && s->s; s++)
  57590. + if (s->id == id)
  57591. + break;
  57592. +
  57593. + /* unrecognized: stall. */
  57594. + if (!s || !s->s)
  57595. + return -EINVAL;
  57596. +
  57597. + /* string descriptors have length, tag, then UTF16-LE text */
  57598. + len = min ((size_t) 126, strlen (s->s));
  57599. + memset (buf + 2, 0, 2 * len); /* zero all the bytes */
  57600. + len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
  57601. + if (len < 0)
  57602. + return -EINVAL;
  57603. + buf [0] = (len + 1) * 2;
  57604. + buf [1] = USB_DT_STRING;
  57605. + return buf [0];
  57606. +}
  57607. +
  57608. +
  57609. +/*-------------------------------------------------------------------------*/
  57610. +/*-------------------------------------------------------------------------*/
  57611. +
  57612. +
  57613. +/**
  57614. + * usb_descriptor_fillbuf - fill buffer with descriptors
  57615. + * @buf: Buffer to be filled
  57616. + * @buflen: Size of buf
  57617. + * @src: Array of descriptor pointers, terminated by null pointer.
  57618. + *
  57619. + * Copies descriptors into the buffer, returning the length or a
  57620. + * negative error code if they can't all be copied. Useful when
  57621. + * assembling descriptors for an associated set of interfaces used
  57622. + * as part of configuring a composite device; or in other cases where
  57623. + * sets of descriptors need to be marshaled.
  57624. + */
  57625. +int
  57626. +usb_descriptor_fillbuf(void *buf, unsigned buflen,
  57627. + const struct usb_descriptor_header **src)
  57628. +{
  57629. + u8 *dest = buf;
  57630. +
  57631. + if (!src)
  57632. + return -EINVAL;
  57633. +
  57634. + /* fill buffer from src[] until null descriptor ptr */
  57635. + for (; 0 != *src; src++) {
  57636. + unsigned len = (*src)->bLength;
  57637. +
  57638. + if (len > buflen)
  57639. + return -EINVAL;
  57640. + memcpy(dest, *src, len);
  57641. + buflen -= len;
  57642. + dest += len;
  57643. + }
  57644. + return dest - (u8 *)buf;
  57645. +}
  57646. +
  57647. +
  57648. +/**
  57649. + * usb_gadget_config_buf - builts a complete configuration descriptor
  57650. + * @config: Header for the descriptor, including characteristics such
  57651. + * as power requirements and number of interfaces.
  57652. + * @desc: Null-terminated vector of pointers to the descriptors (interface,
  57653. + * endpoint, etc) defining all functions in this device configuration.
  57654. + * @buf: Buffer for the resulting configuration descriptor.
  57655. + * @length: Length of buffer. If this is not big enough to hold the
  57656. + * entire configuration descriptor, an error code will be returned.
  57657. + *
  57658. + * This copies descriptors into the response buffer, building a descriptor
  57659. + * for that configuration. It returns the buffer length or a negative
  57660. + * status code. The config.wTotalLength field is set to match the length
  57661. + * of the result, but other descriptor fields (including power usage and
  57662. + * interface count) must be set by the caller.
  57663. + *
  57664. + * Gadget drivers could use this when constructing a config descriptor
  57665. + * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
  57666. + * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
  57667. + */
  57668. +int usb_gadget_config_buf(
  57669. + const struct usb_config_descriptor *config,
  57670. + void *buf,
  57671. + unsigned length,
  57672. + const struct usb_descriptor_header **desc
  57673. +)
  57674. +{
  57675. + struct usb_config_descriptor *cp = buf;
  57676. + int len;
  57677. +
  57678. + /* config descriptor first */
  57679. + if (length < USB_DT_CONFIG_SIZE || !desc)
  57680. + return -EINVAL;
  57681. + *cp = *config;
  57682. +
  57683. + /* then interface/endpoint/class/vendor/... */
  57684. + len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
  57685. + length - USB_DT_CONFIG_SIZE, desc);
  57686. + if (len < 0)
  57687. + return len;
  57688. + len += USB_DT_CONFIG_SIZE;
  57689. + if (len > 0xffff)
  57690. + return -EINVAL;
  57691. +
  57692. + /* patch up the config descriptor */
  57693. + cp->bLength = USB_DT_CONFIG_SIZE;
  57694. + cp->bDescriptorType = USB_DT_CONFIG;
  57695. + cp->wTotalLength = cpu_to_le16(len);
  57696. + cp->bmAttributes |= USB_CONFIG_ATT_ONE;
  57697. + return len;
  57698. +}
  57699. +
  57700. +/*-------------------------------------------------------------------------*/
  57701. +/*-------------------------------------------------------------------------*/
  57702. +
  57703. +
  57704. +#define RBUF_LEN (1024*1024)
  57705. +static int rbuf_start;
  57706. +static int rbuf_len;
  57707. +static __u8 rbuf[RBUF_LEN];
  57708. +
  57709. +/*-------------------------------------------------------------------------*/
  57710. +
  57711. +#define DRIVER_VERSION "St Patrick's Day 2004"
  57712. +
  57713. +static const char shortname [] = "zero";
  57714. +static const char longname [] = "YAMAHA YST-MS35D USB Speaker ";
  57715. +
  57716. +static const char source_sink [] = "source and sink data";
  57717. +static const char loopback [] = "loop input to output";
  57718. +
  57719. +/*-------------------------------------------------------------------------*/
  57720. +
  57721. +/*
  57722. + * driver assumes self-powered hardware, and
  57723. + * has no way for users to trigger remote wakeup.
  57724. + *
  57725. + * this version autoconfigures as much as possible,
  57726. + * which is reasonable for most "bulk-only" drivers.
  57727. + */
  57728. +static const char *EP_IN_NAME; /* source */
  57729. +static const char *EP_OUT_NAME; /* sink */
  57730. +
  57731. +/*-------------------------------------------------------------------------*/
  57732. +
  57733. +/* big enough to hold our biggest descriptor */
  57734. +#define USB_BUFSIZ 512
  57735. +
  57736. +struct zero_dev {
  57737. + spinlock_t lock;
  57738. + struct usb_gadget *gadget;
  57739. + struct usb_request *req; /* for control responses */
  57740. +
  57741. + /* when configured, we have one of two configs:
  57742. + * - source data (in to host) and sink it (out from host)
  57743. + * - or loop it back (out from host back in to host)
  57744. + */
  57745. + u8 config;
  57746. + struct usb_ep *in_ep, *out_ep;
  57747. +
  57748. + /* autoresume timer */
  57749. + struct timer_list resume;
  57750. +};
  57751. +
  57752. +#define xprintk(d,level,fmt,args...) \
  57753. + dev_printk(level , &(d)->gadget->dev , fmt , ## args)
  57754. +
  57755. +#ifdef DEBUG
  57756. +#define DBG(dev,fmt,args...) \
  57757. + xprintk(dev , KERN_DEBUG , fmt , ## args)
  57758. +#else
  57759. +#define DBG(dev,fmt,args...) \
  57760. + do { } while (0)
  57761. +#endif /* DEBUG */
  57762. +
  57763. +#ifdef VERBOSE
  57764. +#define VDBG DBG
  57765. +#else
  57766. +#define VDBG(dev,fmt,args...) \
  57767. + do { } while (0)
  57768. +#endif /* VERBOSE */
  57769. +
  57770. +#define ERROR(dev,fmt,args...) \
  57771. + xprintk(dev , KERN_ERR , fmt , ## args)
  57772. +#define WARN(dev,fmt,args...) \
  57773. + xprintk(dev , KERN_WARNING , fmt , ## args)
  57774. +#define INFO(dev,fmt,args...) \
  57775. + xprintk(dev , KERN_INFO , fmt , ## args)
  57776. +
  57777. +/*-------------------------------------------------------------------------*/
  57778. +
  57779. +static unsigned buflen = 4096;
  57780. +static unsigned qlen = 32;
  57781. +static unsigned pattern = 0;
  57782. +
  57783. +module_param (buflen, uint, S_IRUGO|S_IWUSR);
  57784. +module_param (qlen, uint, S_IRUGO|S_IWUSR);
  57785. +module_param (pattern, uint, S_IRUGO|S_IWUSR);
  57786. +
  57787. +/*
  57788. + * if it's nonzero, autoresume says how many seconds to wait
  57789. + * before trying to wake up the host after suspend.
  57790. + */
  57791. +static unsigned autoresume = 0;
  57792. +module_param (autoresume, uint, 0);
  57793. +
  57794. +/*
  57795. + * Normally the "loopback" configuration is second (index 1) so
  57796. + * it's not the default. Here's where to change that order, to
  57797. + * work better with hosts where config changes are problematic.
  57798. + * Or controllers (like superh) that only support one config.
  57799. + */
  57800. +static int loopdefault = 0;
  57801. +
  57802. +module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
  57803. +
  57804. +/*-------------------------------------------------------------------------*/
  57805. +
  57806. +/* Thanks to NetChip Technologies for donating this product ID.
  57807. + *
  57808. + * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  57809. + * Instead: allocate your own, using normal USB-IF procedures.
  57810. + */
  57811. +#ifndef CONFIG_USB_ZERO_HNPTEST
  57812. +#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  57813. +#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  57814. +#else
  57815. +#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  57816. +#define DRIVER_PRODUCT_NUM 0xbadd
  57817. +#endif
  57818. +
  57819. +/*-------------------------------------------------------------------------*/
  57820. +
  57821. +/*
  57822. + * DESCRIPTORS ... most are static, but strings and (full)
  57823. + * configuration descriptors are built on demand.
  57824. + */
  57825. +
  57826. +/*
  57827. +#define STRING_MANUFACTURER 25
  57828. +#define STRING_PRODUCT 42
  57829. +#define STRING_SERIAL 101
  57830. +*/
  57831. +#define STRING_MANUFACTURER 1
  57832. +#define STRING_PRODUCT 2
  57833. +#define STRING_SERIAL 3
  57834. +
  57835. +#define STRING_SOURCE_SINK 250
  57836. +#define STRING_LOOPBACK 251
  57837. +
  57838. +/*
  57839. + * This device advertises two configurations; these numbers work
  57840. + * on a pxa250 as well as more flexible hardware.
  57841. + */
  57842. +#define CONFIG_SOURCE_SINK 3
  57843. +#define CONFIG_LOOPBACK 2
  57844. +
  57845. +/*
  57846. +static struct usb_device_descriptor
  57847. +device_desc = {
  57848. + .bLength = sizeof device_desc,
  57849. + .bDescriptorType = USB_DT_DEVICE,
  57850. +
  57851. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  57852. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  57853. +
  57854. + .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
  57855. + .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
  57856. + .iManufacturer = STRING_MANUFACTURER,
  57857. + .iProduct = STRING_PRODUCT,
  57858. + .iSerialNumber = STRING_SERIAL,
  57859. + .bNumConfigurations = 2,
  57860. +};
  57861. +*/
  57862. +static struct usb_device_descriptor
  57863. +device_desc = {
  57864. + .bLength = sizeof device_desc,
  57865. + .bDescriptorType = USB_DT_DEVICE,
  57866. + .bcdUSB = __constant_cpu_to_le16 (0x0100),
  57867. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  57868. + .bDeviceSubClass = 0,
  57869. + .bDeviceProtocol = 0,
  57870. + .bMaxPacketSize0 = 64,
  57871. + .bcdDevice = __constant_cpu_to_le16 (0x0100),
  57872. + .idVendor = __constant_cpu_to_le16 (0x0499),
  57873. + .idProduct = __constant_cpu_to_le16 (0x3002),
  57874. + .iManufacturer = STRING_MANUFACTURER,
  57875. + .iProduct = STRING_PRODUCT,
  57876. + .iSerialNumber = STRING_SERIAL,
  57877. + .bNumConfigurations = 1,
  57878. +};
  57879. +
  57880. +static struct usb_config_descriptor
  57881. +z_config = {
  57882. + .bLength = sizeof z_config,
  57883. + .bDescriptorType = USB_DT_CONFIG,
  57884. +
  57885. + /* compute wTotalLength on the fly */
  57886. + .bNumInterfaces = 2,
  57887. + .bConfigurationValue = 1,
  57888. + .iConfiguration = 0,
  57889. + .bmAttributes = 0x40,
  57890. + .bMaxPower = 0, /* self-powered */
  57891. +};
  57892. +
  57893. +
  57894. +static struct usb_otg_descriptor
  57895. +otg_descriptor = {
  57896. + .bLength = sizeof otg_descriptor,
  57897. + .bDescriptorType = USB_DT_OTG,
  57898. +
  57899. + .bmAttributes = USB_OTG_SRP,
  57900. +};
  57901. +
  57902. +/* one interface in each configuration */
  57903. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  57904. +
  57905. +/*
  57906. + * usb 2.0 devices need to expose both high speed and full speed
  57907. + * descriptors, unless they only run at full speed.
  57908. + *
  57909. + * that means alternate endpoint descriptors (bigger packets)
  57910. + * and a "device qualifier" ... plus more construction options
  57911. + * for the config descriptor.
  57912. + */
  57913. +
  57914. +static struct usb_qualifier_descriptor
  57915. +dev_qualifier = {
  57916. + .bLength = sizeof dev_qualifier,
  57917. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  57918. +
  57919. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  57920. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  57921. +
  57922. + .bNumConfigurations = 2,
  57923. +};
  57924. +
  57925. +
  57926. +struct usb_cs_as_general_descriptor {
  57927. + __u8 bLength;
  57928. + __u8 bDescriptorType;
  57929. +
  57930. + __u8 bDescriptorSubType;
  57931. + __u8 bTerminalLink;
  57932. + __u8 bDelay;
  57933. + __u16 wFormatTag;
  57934. +} __attribute__ ((packed));
  57935. +
  57936. +struct usb_cs_as_format_descriptor {
  57937. + __u8 bLength;
  57938. + __u8 bDescriptorType;
  57939. +
  57940. + __u8 bDescriptorSubType;
  57941. + __u8 bFormatType;
  57942. + __u8 bNrChannels;
  57943. + __u8 bSubframeSize;
  57944. + __u8 bBitResolution;
  57945. + __u8 bSamfreqType;
  57946. + __u8 tLowerSamFreq[3];
  57947. + __u8 tUpperSamFreq[3];
  57948. +} __attribute__ ((packed));
  57949. +
  57950. +static const struct usb_interface_descriptor
  57951. +z_audio_control_if_desc = {
  57952. + .bLength = sizeof z_audio_control_if_desc,
  57953. + .bDescriptorType = USB_DT_INTERFACE,
  57954. + .bInterfaceNumber = 0,
  57955. + .bAlternateSetting = 0,
  57956. + .bNumEndpoints = 0,
  57957. + .bInterfaceClass = USB_CLASS_AUDIO,
  57958. + .bInterfaceSubClass = 0x1,
  57959. + .bInterfaceProtocol = 0,
  57960. + .iInterface = 0,
  57961. +};
  57962. +
  57963. +static const struct usb_interface_descriptor
  57964. +z_audio_if_desc = {
  57965. + .bLength = sizeof z_audio_if_desc,
  57966. + .bDescriptorType = USB_DT_INTERFACE,
  57967. + .bInterfaceNumber = 1,
  57968. + .bAlternateSetting = 0,
  57969. + .bNumEndpoints = 0,
  57970. + .bInterfaceClass = USB_CLASS_AUDIO,
  57971. + .bInterfaceSubClass = 0x2,
  57972. + .bInterfaceProtocol = 0,
  57973. + .iInterface = 0,
  57974. +};
  57975. +
  57976. +static const struct usb_interface_descriptor
  57977. +z_audio_if_desc2 = {
  57978. + .bLength = sizeof z_audio_if_desc,
  57979. + .bDescriptorType = USB_DT_INTERFACE,
  57980. + .bInterfaceNumber = 1,
  57981. + .bAlternateSetting = 1,
  57982. + .bNumEndpoints = 1,
  57983. + .bInterfaceClass = USB_CLASS_AUDIO,
  57984. + .bInterfaceSubClass = 0x2,
  57985. + .bInterfaceProtocol = 0,
  57986. + .iInterface = 0,
  57987. +};
  57988. +
  57989. +static const struct usb_cs_as_general_descriptor
  57990. +z_audio_cs_as_if_desc = {
  57991. + .bLength = 7,
  57992. + .bDescriptorType = 0x24,
  57993. +
  57994. + .bDescriptorSubType = 0x01,
  57995. + .bTerminalLink = 0x01,
  57996. + .bDelay = 0x0,
  57997. + .wFormatTag = __constant_cpu_to_le16 (0x0001)
  57998. +};
  57999. +
  58000. +
  58001. +static const struct usb_cs_as_format_descriptor
  58002. +z_audio_cs_as_format_desc = {
  58003. + .bLength = 0xe,
  58004. + .bDescriptorType = 0x24,
  58005. +
  58006. + .bDescriptorSubType = 2,
  58007. + .bFormatType = 1,
  58008. + .bNrChannels = 1,
  58009. + .bSubframeSize = 1,
  58010. + .bBitResolution = 8,
  58011. + .bSamfreqType = 0,
  58012. + .tLowerSamFreq = {0x7e, 0x13, 0x00},
  58013. + .tUpperSamFreq = {0xe2, 0xd6, 0x00},
  58014. +};
  58015. +
  58016. +static const struct usb_endpoint_descriptor
  58017. +z_iso_ep = {
  58018. + .bLength = 0x09,
  58019. + .bDescriptorType = 0x05,
  58020. + .bEndpointAddress = 0x04,
  58021. + .bmAttributes = 0x09,
  58022. + .wMaxPacketSize = 0x0038,
  58023. + .bInterval = 0x01,
  58024. + .bRefresh = 0x00,
  58025. + .bSynchAddress = 0x00,
  58026. +};
  58027. +
  58028. +static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  58029. +
  58030. +// 9 bytes
  58031. +static char z_ac_interface_header_desc[] =
  58032. +{ 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
  58033. +
  58034. +// 12 bytes
  58035. +static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
  58036. + 0x03, 0x00, 0x00, 0x00};
  58037. +// 13 bytes
  58038. +static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
  58039. + 0x02, 0x00, 0x02, 0x00, 0x00};
  58040. +// 9 bytes
  58041. +static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02,
  58042. + 0x00};
  58043. +
  58044. +static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00,
  58045. + 0x00};
  58046. +
  58047. +static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  58048. +
  58049. +static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00,
  58050. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  58051. +
  58052. +static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  58053. + 0x00};
  58054. +
  58055. +static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  58056. +
  58057. +static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
  58058. + 0x00};
  58059. +
  58060. +static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  58061. +
  58062. +static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
  58063. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  58064. +
  58065. +static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  58066. + 0x00};
  58067. +
  58068. +static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  58069. +
  58070. +static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
  58071. + 0x00};
  58072. +
  58073. +static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  58074. +
  58075. +static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
  58076. + 0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  58077. +
  58078. +static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
  58079. + 0x00};
  58080. +
  58081. +static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  58082. +
  58083. +static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00,
  58084. + 0x00};
  58085. +
  58086. +static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  58087. +
  58088. +static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00,
  58089. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  58090. +
  58091. +static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
  58092. + 0x00};
  58093. +
  58094. +static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  58095. +
  58096. +static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
  58097. + 0x00};
  58098. +
  58099. +static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  58100. +
  58101. +static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00,
  58102. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  58103. +
  58104. +static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
  58105. + 0x00};
  58106. +
  58107. +static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  58108. +
  58109. +
  58110. +
  58111. +static const struct usb_descriptor_header *z_function [] = {
  58112. + (struct usb_descriptor_header *) &z_audio_control_if_desc,
  58113. + (struct usb_descriptor_header *) &z_ac_interface_header_desc,
  58114. + (struct usb_descriptor_header *) &z_0,
  58115. + (struct usb_descriptor_header *) &z_1,
  58116. + (struct usb_descriptor_header *) &z_2,
  58117. + (struct usb_descriptor_header *) &z_audio_if_desc,
  58118. + (struct usb_descriptor_header *) &z_audio_if_desc2,
  58119. + (struct usb_descriptor_header *) &z_audio_cs_as_if_desc,
  58120. + (struct usb_descriptor_header *) &z_audio_cs_as_format_desc,
  58121. + (struct usb_descriptor_header *) &z_iso_ep,
  58122. + (struct usb_descriptor_header *) &z_iso_ep2,
  58123. + (struct usb_descriptor_header *) &za_0,
  58124. + (struct usb_descriptor_header *) &za_1,
  58125. + (struct usb_descriptor_header *) &za_2,
  58126. + (struct usb_descriptor_header *) &za_3,
  58127. + (struct usb_descriptor_header *) &za_4,
  58128. + (struct usb_descriptor_header *) &za_5,
  58129. + (struct usb_descriptor_header *) &za_6,
  58130. + (struct usb_descriptor_header *) &za_7,
  58131. + (struct usb_descriptor_header *) &za_8,
  58132. + (struct usb_descriptor_header *) &za_9,
  58133. + (struct usb_descriptor_header *) &za_10,
  58134. + (struct usb_descriptor_header *) &za_11,
  58135. + (struct usb_descriptor_header *) &za_12,
  58136. + (struct usb_descriptor_header *) &za_13,
  58137. + (struct usb_descriptor_header *) &za_14,
  58138. + (struct usb_descriptor_header *) &za_15,
  58139. + (struct usb_descriptor_header *) &za_16,
  58140. + (struct usb_descriptor_header *) &za_17,
  58141. + (struct usb_descriptor_header *) &za_18,
  58142. + (struct usb_descriptor_header *) &za_19,
  58143. + (struct usb_descriptor_header *) &za_20,
  58144. + (struct usb_descriptor_header *) &za_21,
  58145. + (struct usb_descriptor_header *) &za_22,
  58146. + (struct usb_descriptor_header *) &za_23,
  58147. + (struct usb_descriptor_header *) &za_24,
  58148. + NULL,
  58149. +};
  58150. +
  58151. +/* maxpacket and other transfer characteristics vary by speed. */
  58152. +#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
  58153. +
  58154. +#else
  58155. +
  58156. +/* if there's no high speed support, maxpacket doesn't change. */
  58157. +#define ep_desc(g,hs,fs) fs
  58158. +
  58159. +#endif /* !CONFIG_USB_GADGET_DUALSPEED */
  58160. +
  58161. +static char manufacturer [40];
  58162. +//static char serial [40];
  58163. +static char serial [] = "Ser 00 em";
  58164. +
  58165. +/* static strings, in UTF-8 */
  58166. +static struct usb_string strings [] = {
  58167. + { STRING_MANUFACTURER, manufacturer, },
  58168. + { STRING_PRODUCT, longname, },
  58169. + { STRING_SERIAL, serial, },
  58170. + { STRING_LOOPBACK, loopback, },
  58171. + { STRING_SOURCE_SINK, source_sink, },
  58172. + { } /* end of list */
  58173. +};
  58174. +
  58175. +static struct usb_gadget_strings stringtab = {
  58176. + .language = 0x0409, /* en-us */
  58177. + .strings = strings,
  58178. +};
  58179. +
  58180. +/*
  58181. + * config descriptors are also handcrafted. these must agree with code
  58182. + * that sets configurations, and with code managing interfaces and their
  58183. + * altsettings. other complexity may come from:
  58184. + *
  58185. + * - high speed support, including "other speed config" rules
  58186. + * - multiple configurations
  58187. + * - interfaces with alternate settings
  58188. + * - embedded class or vendor-specific descriptors
  58189. + *
  58190. + * this handles high speed, and has a second config that could as easily
  58191. + * have been an alternate interface setting (on most hardware).
  58192. + *
  58193. + * NOTE: to demonstrate (and test) more USB capabilities, this driver
  58194. + * should include an altsetting to test interrupt transfers, including
  58195. + * high bandwidth modes at high speed. (Maybe work like Intel's test
  58196. + * device?)
  58197. + */
  58198. +static int
  58199. +config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
  58200. +{
  58201. + int len;
  58202. + const struct usb_descriptor_header **function;
  58203. +
  58204. + function = z_function;
  58205. + len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
  58206. + if (len < 0)
  58207. + return len;
  58208. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  58209. + return len;
  58210. +}
  58211. +
  58212. +/*-------------------------------------------------------------------------*/
  58213. +
  58214. +static struct usb_request *
  58215. +alloc_ep_req (struct usb_ep *ep, unsigned length)
  58216. +{
  58217. + struct usb_request *req;
  58218. +
  58219. + req = usb_ep_alloc_request (ep, GFP_ATOMIC);
  58220. + if (req) {
  58221. + req->length = length;
  58222. + req->buf = usb_ep_alloc_buffer (ep, length,
  58223. + &req->dma, GFP_ATOMIC);
  58224. + if (!req->buf) {
  58225. + usb_ep_free_request (ep, req);
  58226. + req = NULL;
  58227. + }
  58228. + }
  58229. + return req;
  58230. +}
  58231. +
  58232. +static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
  58233. +{
  58234. + if (req->buf)
  58235. + usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
  58236. + usb_ep_free_request (ep, req);
  58237. +}
  58238. +
  58239. +/*-------------------------------------------------------------------------*/
  58240. +
  58241. +/* optionally require specific source/sink data patterns */
  58242. +
  58243. +static int
  58244. +check_read_data (
  58245. + struct zero_dev *dev,
  58246. + struct usb_ep *ep,
  58247. + struct usb_request *req
  58248. +)
  58249. +{
  58250. + unsigned i;
  58251. + u8 *buf = req->buf;
  58252. +
  58253. + for (i = 0; i < req->actual; i++, buf++) {
  58254. + switch (pattern) {
  58255. + /* all-zeroes has no synchronization issues */
  58256. + case 0:
  58257. + if (*buf == 0)
  58258. + continue;
  58259. + break;
  58260. + /* mod63 stays in sync with short-terminated transfers,
  58261. + * or otherwise when host and gadget agree on how large
  58262. + * each usb transfer request should be. resync is done
  58263. + * with set_interface or set_config.
  58264. + */
  58265. + case 1:
  58266. + if (*buf == (u8)(i % 63))
  58267. + continue;
  58268. + break;
  58269. + }
  58270. + ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
  58271. + usb_ep_set_halt (ep);
  58272. + return -EINVAL;
  58273. + }
  58274. + return 0;
  58275. +}
  58276. +
  58277. +/*-------------------------------------------------------------------------*/
  58278. +
  58279. +static void zero_reset_config (struct zero_dev *dev)
  58280. +{
  58281. + if (dev->config == 0)
  58282. + return;
  58283. +
  58284. + DBG (dev, "reset config\n");
  58285. +
  58286. + /* just disable endpoints, forcing completion of pending i/o.
  58287. + * all our completion handlers free their requests in this case.
  58288. + */
  58289. + if (dev->in_ep) {
  58290. + usb_ep_disable (dev->in_ep);
  58291. + dev->in_ep = NULL;
  58292. + }
  58293. + if (dev->out_ep) {
  58294. + usb_ep_disable (dev->out_ep);
  58295. + dev->out_ep = NULL;
  58296. + }
  58297. + dev->config = 0;
  58298. + del_timer (&dev->resume);
  58299. +}
  58300. +
  58301. +#define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
  58302. +
  58303. +static void
  58304. +zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
  58305. +{
  58306. + struct zero_dev *dev = ep->driver_data;
  58307. + int status = req->status;
  58308. + int i, j;
  58309. +
  58310. + switch (status) {
  58311. +
  58312. + case 0: /* normal completion? */
  58313. + //printk ("\nzero ---------------> isoc normal completion %d bytes\n", req->actual);
  58314. + for (i=0, j=rbuf_start; i<req->actual; i++) {
  58315. + //printk ("%02x ", ((__u8*)req->buf)[i]);
  58316. + rbuf[j] = ((__u8*)req->buf)[i];
  58317. + j++;
  58318. + if (j >= RBUF_LEN) j=0;
  58319. + }
  58320. + rbuf_start = j;
  58321. + //printk ("\n\n");
  58322. +
  58323. + if (rbuf_len < RBUF_LEN) {
  58324. + rbuf_len += req->actual;
  58325. + if (rbuf_len > RBUF_LEN) {
  58326. + rbuf_len = RBUF_LEN;
  58327. + }
  58328. + }
  58329. +
  58330. + break;
  58331. +
  58332. + /* this endpoint is normally active while we're configured */
  58333. + case -ECONNABORTED: /* hardware forced ep reset */
  58334. + case -ECONNRESET: /* request dequeued */
  58335. + case -ESHUTDOWN: /* disconnect from host */
  58336. + VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
  58337. + req->actual, req->length);
  58338. + if (ep == dev->out_ep)
  58339. + check_read_data (dev, ep, req);
  58340. + free_ep_req (ep, req);
  58341. + return;
  58342. +
  58343. + case -EOVERFLOW: /* buffer overrun on read means that
  58344. + * we didn't provide a big enough
  58345. + * buffer.
  58346. + */
  58347. + default:
  58348. +#if 1
  58349. + DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
  58350. + status, req->actual, req->length);
  58351. +#endif
  58352. + case -EREMOTEIO: /* short read */
  58353. + break;
  58354. + }
  58355. +
  58356. + status = usb_ep_queue (ep, req, GFP_ATOMIC);
  58357. + if (status) {
  58358. + ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
  58359. + ep->name, req->length, status);
  58360. + usb_ep_set_halt (ep);
  58361. + /* FIXME recover later ... somehow */
  58362. + }
  58363. +}
  58364. +
  58365. +static struct usb_request *
  58366. +zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
  58367. +{
  58368. + struct usb_request *req;
  58369. + int status;
  58370. +
  58371. + req = alloc_ep_req (ep, 512);
  58372. + if (!req)
  58373. + return NULL;
  58374. +
  58375. + req->complete = zero_isoc_complete;
  58376. +
  58377. + status = usb_ep_queue (ep, req, gfp_flags);
  58378. + if (status) {
  58379. + struct zero_dev *dev = ep->driver_data;
  58380. +
  58381. + ERROR (dev, "start %s --> %d\n", ep->name, status);
  58382. + free_ep_req (ep, req);
  58383. + req = NULL;
  58384. + }
  58385. +
  58386. + return req;
  58387. +}
  58388. +
  58389. +/* change our operational config. this code must agree with the code
  58390. + * that returns config descriptors, and altsetting code.
  58391. + *
  58392. + * it's also responsible for power management interactions. some
  58393. + * configurations might not work with our current power sources.
  58394. + *
  58395. + * note that some device controller hardware will constrain what this
  58396. + * code can do, perhaps by disallowing more than one configuration or
  58397. + * by limiting configuration choices (like the pxa2xx).
  58398. + */
  58399. +static int
  58400. +zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
  58401. +{
  58402. + int result = 0;
  58403. + struct usb_gadget *gadget = dev->gadget;
  58404. + const struct usb_endpoint_descriptor *d;
  58405. + struct usb_ep *ep;
  58406. +
  58407. + if (number == dev->config)
  58408. + return 0;
  58409. +
  58410. + zero_reset_config (dev);
  58411. +
  58412. + gadget_for_each_ep (ep, gadget) {
  58413. +
  58414. + if (strcmp (ep->name, "ep4") == 0) {
  58415. +
  58416. + d = (struct usb_endpoint_descripter *)&za_23; // isoc ep desc for audio i/f alt setting 6
  58417. + result = usb_ep_enable (ep, d);
  58418. +
  58419. + if (result == 0) {
  58420. + ep->driver_data = dev;
  58421. + dev->in_ep = ep;
  58422. +
  58423. + if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
  58424. +
  58425. + dev->in_ep = ep;
  58426. + continue;
  58427. + }
  58428. +
  58429. + usb_ep_disable (ep);
  58430. + result = -EIO;
  58431. + }
  58432. + }
  58433. +
  58434. + }
  58435. +
  58436. + dev->config = number;
  58437. + return result;
  58438. +}
  58439. +
  58440. +/*-------------------------------------------------------------------------*/
  58441. +
  58442. +static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
  58443. +{
  58444. + if (req->status || req->actual != req->length)
  58445. + DBG ((struct zero_dev *) ep->driver_data,
  58446. + "setup complete --> %d, %d/%d\n",
  58447. + req->status, req->actual, req->length);
  58448. +}
  58449. +
  58450. +/*
  58451. + * The setup() callback implements all the ep0 functionality that's
  58452. + * not handled lower down, in hardware or the hardware driver (like
  58453. + * device and endpoint feature flags, and their status). It's all
  58454. + * housekeeping for the gadget function we're implementing. Most of
  58455. + * the work is in config-specific setup.
  58456. + */
  58457. +static int
  58458. +zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  58459. +{
  58460. + struct zero_dev *dev = get_gadget_data (gadget);
  58461. + struct usb_request *req = dev->req;
  58462. + int value = -EOPNOTSUPP;
  58463. +
  58464. + /* usually this stores reply data in the pre-allocated ep0 buffer,
  58465. + * but config change events will reconfigure hardware.
  58466. + */
  58467. + req->zero = 0;
  58468. + switch (ctrl->bRequest) {
  58469. +
  58470. + case USB_REQ_GET_DESCRIPTOR:
  58471. +
  58472. + switch (ctrl->wValue >> 8) {
  58473. +
  58474. + case USB_DT_DEVICE:
  58475. + value = min (ctrl->wLength, (u16) sizeof device_desc);
  58476. + memcpy (req->buf, &device_desc, value);
  58477. + break;
  58478. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  58479. + case USB_DT_DEVICE_QUALIFIER:
  58480. + if (!gadget->is_dualspeed)
  58481. + break;
  58482. + value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
  58483. + memcpy (req->buf, &dev_qualifier, value);
  58484. + break;
  58485. +
  58486. + case USB_DT_OTHER_SPEED_CONFIG:
  58487. + if (!gadget->is_dualspeed)
  58488. + break;
  58489. + // FALLTHROUGH
  58490. +#endif /* CONFIG_USB_GADGET_DUALSPEED */
  58491. + case USB_DT_CONFIG:
  58492. + value = config_buf (gadget, req->buf,
  58493. + ctrl->wValue >> 8,
  58494. + ctrl->wValue & 0xff);
  58495. + if (value >= 0)
  58496. + value = min (ctrl->wLength, (u16) value);
  58497. + break;
  58498. +
  58499. + case USB_DT_STRING:
  58500. + /* wIndex == language code.
  58501. + * this driver only handles one language, you can
  58502. + * add string tables for other languages, using
  58503. + * any UTF-8 characters
  58504. + */
  58505. + value = usb_gadget_get_string (&stringtab,
  58506. + ctrl->wValue & 0xff, req->buf);
  58507. + if (value >= 0) {
  58508. + value = min (ctrl->wLength, (u16) value);
  58509. + }
  58510. + break;
  58511. + }
  58512. + break;
  58513. +
  58514. + /* currently two configs, two speeds */
  58515. + case USB_REQ_SET_CONFIGURATION:
  58516. + if (ctrl->bRequestType != 0)
  58517. + goto unknown;
  58518. +
  58519. + spin_lock (&dev->lock);
  58520. + value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
  58521. + spin_unlock (&dev->lock);
  58522. + break;
  58523. + case USB_REQ_GET_CONFIGURATION:
  58524. + if (ctrl->bRequestType != USB_DIR_IN)
  58525. + goto unknown;
  58526. + *(u8 *)req->buf = dev->config;
  58527. + value = min (ctrl->wLength, (u16) 1);
  58528. + break;
  58529. +
  58530. + /* until we add altsetting support, or other interfaces,
  58531. + * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
  58532. + * and already killed pending endpoint I/O.
  58533. + */
  58534. + case USB_REQ_SET_INTERFACE:
  58535. +
  58536. + if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  58537. + goto unknown;
  58538. + spin_lock (&dev->lock);
  58539. + if (dev->config) {
  58540. + u8 config = dev->config;
  58541. +
  58542. + /* resets interface configuration, forgets about
  58543. + * previous transaction state (queued bufs, etc)
  58544. + * and re-inits endpoint state (toggle etc)
  58545. + * no response queued, just zero status == success.
  58546. + * if we had more than one interface we couldn't
  58547. + * use this "reset the config" shortcut.
  58548. + */
  58549. + zero_reset_config (dev);
  58550. + zero_set_config (dev, config, GFP_ATOMIC);
  58551. + value = 0;
  58552. + }
  58553. + spin_unlock (&dev->lock);
  58554. + break;
  58555. + case USB_REQ_GET_INTERFACE:
  58556. + if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
  58557. + value = ctrl->wLength;
  58558. + break;
  58559. + }
  58560. + else {
  58561. + if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  58562. + goto unknown;
  58563. + if (!dev->config)
  58564. + break;
  58565. + if (ctrl->wIndex != 0) {
  58566. + value = -EDOM;
  58567. + break;
  58568. + }
  58569. + *(u8 *)req->buf = 0;
  58570. + value = min (ctrl->wLength, (u16) 1);
  58571. + }
  58572. + break;
  58573. +
  58574. + /*
  58575. + * These are the same vendor-specific requests supported by
  58576. + * Intel's USB 2.0 compliance test devices. We exceed that
  58577. + * device spec by allowing multiple-packet requests.
  58578. + */
  58579. + case 0x5b: /* control WRITE test -- fill the buffer */
  58580. + if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  58581. + goto unknown;
  58582. + if (ctrl->wValue || ctrl->wIndex)
  58583. + break;
  58584. + /* just read that many bytes into the buffer */
  58585. + if (ctrl->wLength > USB_BUFSIZ)
  58586. + break;
  58587. + value = ctrl->wLength;
  58588. + break;
  58589. + case 0x5c: /* control READ test -- return the buffer */
  58590. + if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  58591. + goto unknown;
  58592. + if (ctrl->wValue || ctrl->wIndex)
  58593. + break;
  58594. + /* expect those bytes are still in the buffer; send back */
  58595. + if (ctrl->wLength > USB_BUFSIZ
  58596. + || ctrl->wLength != req->length)
  58597. + break;
  58598. + value = ctrl->wLength;
  58599. + break;
  58600. +
  58601. + case 0x01: // SET_CUR
  58602. + case 0x02:
  58603. + case 0x03:
  58604. + case 0x04:
  58605. + case 0x05:
  58606. + value = ctrl->wLength;
  58607. + break;
  58608. + case 0x81:
  58609. + switch (ctrl->wValue) {
  58610. + case 0x0201:
  58611. + case 0x0202:
  58612. + ((u8*)req->buf)[0] = 0x00;
  58613. + ((u8*)req->buf)[1] = 0xe3;
  58614. + break;
  58615. + case 0x0300:
  58616. + case 0x0500:
  58617. + ((u8*)req->buf)[0] = 0x00;
  58618. + break;
  58619. + }
  58620. + //((u8*)req->buf)[0] = 0x81;
  58621. + //((u8*)req->buf)[1] = 0x81;
  58622. + value = ctrl->wLength;
  58623. + break;
  58624. + case 0x82:
  58625. + switch (ctrl->wValue) {
  58626. + case 0x0201:
  58627. + case 0x0202:
  58628. + ((u8*)req->buf)[0] = 0x00;
  58629. + ((u8*)req->buf)[1] = 0xc3;
  58630. + break;
  58631. + case 0x0300:
  58632. + case 0x0500:
  58633. + ((u8*)req->buf)[0] = 0x00;
  58634. + break;
  58635. + }
  58636. + //((u8*)req->buf)[0] = 0x82;
  58637. + //((u8*)req->buf)[1] = 0x82;
  58638. + value = ctrl->wLength;
  58639. + break;
  58640. + case 0x83:
  58641. + switch (ctrl->wValue) {
  58642. + case 0x0201:
  58643. + case 0x0202:
  58644. + ((u8*)req->buf)[0] = 0x00;
  58645. + ((u8*)req->buf)[1] = 0x00;
  58646. + break;
  58647. + case 0x0300:
  58648. + ((u8*)req->buf)[0] = 0x60;
  58649. + break;
  58650. + case 0x0500:
  58651. + ((u8*)req->buf)[0] = 0x18;
  58652. + break;
  58653. + }
  58654. + //((u8*)req->buf)[0] = 0x83;
  58655. + //((u8*)req->buf)[1] = 0x83;
  58656. + value = ctrl->wLength;
  58657. + break;
  58658. + case 0x84:
  58659. + switch (ctrl->wValue) {
  58660. + case 0x0201:
  58661. + case 0x0202:
  58662. + ((u8*)req->buf)[0] = 0x00;
  58663. + ((u8*)req->buf)[1] = 0x01;
  58664. + break;
  58665. + case 0x0300:
  58666. + case 0x0500:
  58667. + ((u8*)req->buf)[0] = 0x08;
  58668. + break;
  58669. + }
  58670. + //((u8*)req->buf)[0] = 0x84;
  58671. + //((u8*)req->buf)[1] = 0x84;
  58672. + value = ctrl->wLength;
  58673. + break;
  58674. + case 0x85:
  58675. + ((u8*)req->buf)[0] = 0x85;
  58676. + ((u8*)req->buf)[1] = 0x85;
  58677. + value = ctrl->wLength;
  58678. + break;
  58679. +
  58680. +
  58681. + default:
  58682. +unknown:
  58683. + printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
  58684. + ctrl->bRequestType, ctrl->bRequest,
  58685. + ctrl->wValue, ctrl->wIndex, ctrl->wLength);
  58686. + }
  58687. +
  58688. + /* respond with data transfer before status phase? */
  58689. + if (value >= 0) {
  58690. + req->length = value;
  58691. + req->zero = value < ctrl->wLength
  58692. + && (value % gadget->ep0->maxpacket) == 0;
  58693. + value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
  58694. + if (value < 0) {
  58695. + DBG (dev, "ep_queue < 0 --> %d\n", value);
  58696. + req->status = 0;
  58697. + zero_setup_complete (gadget->ep0, req);
  58698. + }
  58699. + }
  58700. +
  58701. + /* device either stalls (value < 0) or reports success */
  58702. + return value;
  58703. +}
  58704. +
  58705. +static void
  58706. +zero_disconnect (struct usb_gadget *gadget)
  58707. +{
  58708. + struct zero_dev *dev = get_gadget_data (gadget);
  58709. + unsigned long flags;
  58710. +
  58711. + spin_lock_irqsave (&dev->lock, flags);
  58712. + zero_reset_config (dev);
  58713. +
  58714. + /* a more significant application might have some non-usb
  58715. + * activities to quiesce here, saving resources like power
  58716. + * or pushing the notification up a network stack.
  58717. + */
  58718. + spin_unlock_irqrestore (&dev->lock, flags);
  58719. +
  58720. + /* next we may get setup() calls to enumerate new connections;
  58721. + * or an unbind() during shutdown (including removing module).
  58722. + */
  58723. +}
  58724. +
  58725. +static void
  58726. +zero_autoresume (unsigned long _dev)
  58727. +{
  58728. + struct zero_dev *dev = (struct zero_dev *) _dev;
  58729. + int status;
  58730. +
  58731. + /* normally the host would be woken up for something
  58732. + * more significant than just a timer firing...
  58733. + */
  58734. + if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
  58735. + status = usb_gadget_wakeup (dev->gadget);
  58736. + DBG (dev, "wakeup --> %d\n", status);
  58737. + }
  58738. +}
  58739. +
  58740. +/*-------------------------------------------------------------------------*/
  58741. +
  58742. +static void
  58743. +zero_unbind (struct usb_gadget *gadget)
  58744. +{
  58745. + struct zero_dev *dev = get_gadget_data (gadget);
  58746. +
  58747. + DBG (dev, "unbind\n");
  58748. +
  58749. + /* we've already been disconnected ... no i/o is active */
  58750. + if (dev->req)
  58751. + free_ep_req (gadget->ep0, dev->req);
  58752. + del_timer_sync (&dev->resume);
  58753. + kfree (dev);
  58754. + set_gadget_data (gadget, NULL);
  58755. +}
  58756. +
  58757. +static int
  58758. +zero_bind (struct usb_gadget *gadget)
  58759. +{
  58760. + struct zero_dev *dev;
  58761. + //struct usb_ep *ep;
  58762. +
  58763. + printk("binding\n");
  58764. + /*
  58765. + * DRIVER POLICY CHOICE: you may want to do this differently.
  58766. + * One thing to avoid is reusing a bcdDevice revision code
  58767. + * with different host-visible configurations or behavior
  58768. + * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
  58769. + */
  58770. + //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
  58771. +
  58772. +
  58773. + /* ok, we made sense of the hardware ... */
  58774. + dev = kmalloc (sizeof *dev, SLAB_KERNEL);
  58775. + if (!dev)
  58776. + return -ENOMEM;
  58777. + memset (dev, 0, sizeof *dev);
  58778. + spin_lock_init (&dev->lock);
  58779. + dev->gadget = gadget;
  58780. + set_gadget_data (gadget, dev);
  58781. +
  58782. + /* preallocate control response and buffer */
  58783. + dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
  58784. + if (!dev->req)
  58785. + goto enomem;
  58786. + dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
  58787. + &dev->req->dma, GFP_KERNEL);
  58788. + if (!dev->req->buf)
  58789. + goto enomem;
  58790. +
  58791. + dev->req->complete = zero_setup_complete;
  58792. +
  58793. + device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  58794. +
  58795. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  58796. + /* assume ep0 uses the same value for both speeds ... */
  58797. + dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  58798. +
  58799. + /* and that all endpoints are dual-speed */
  58800. + //hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  58801. + //hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  58802. +#endif
  58803. +
  58804. + usb_gadget_set_selfpowered (gadget);
  58805. +
  58806. + init_timer (&dev->resume);
  58807. + dev->resume.function = zero_autoresume;
  58808. + dev->resume.data = (unsigned long) dev;
  58809. +
  58810. + gadget->ep0->driver_data = dev;
  58811. +
  58812. + INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
  58813. + INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
  58814. + EP_OUT_NAME, EP_IN_NAME);
  58815. +
  58816. + snprintf (manufacturer, sizeof manufacturer,
  58817. + UTS_SYSNAME " " UTS_RELEASE " with %s",
  58818. + gadget->name);
  58819. +
  58820. + return 0;
  58821. +
  58822. +enomem:
  58823. + zero_unbind (gadget);
  58824. + return -ENOMEM;
  58825. +}
  58826. +
  58827. +/*-------------------------------------------------------------------------*/
  58828. +
  58829. +static void
  58830. +zero_suspend (struct usb_gadget *gadget)
  58831. +{
  58832. + struct zero_dev *dev = get_gadget_data (gadget);
  58833. +
  58834. + if (gadget->speed == USB_SPEED_UNKNOWN)
  58835. + return;
  58836. +
  58837. + if (autoresume) {
  58838. + mod_timer (&dev->resume, jiffies + (HZ * autoresume));
  58839. + DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
  58840. + } else
  58841. + DBG (dev, "suspend\n");
  58842. +}
  58843. +
  58844. +static void
  58845. +zero_resume (struct usb_gadget *gadget)
  58846. +{
  58847. + struct zero_dev *dev = get_gadget_data (gadget);
  58848. +
  58849. + DBG (dev, "resume\n");
  58850. + del_timer (&dev->resume);
  58851. +}
  58852. +
  58853. +
  58854. +/*-------------------------------------------------------------------------*/
  58855. +
  58856. +static struct usb_gadget_driver zero_driver = {
  58857. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  58858. + .speed = USB_SPEED_HIGH,
  58859. +#else
  58860. + .speed = USB_SPEED_FULL,
  58861. +#endif
  58862. + .function = (char *) longname,
  58863. + .bind = zero_bind,
  58864. + .unbind = zero_unbind,
  58865. +
  58866. + .setup = zero_setup,
  58867. + .disconnect = zero_disconnect,
  58868. +
  58869. + .suspend = zero_suspend,
  58870. + .resume = zero_resume,
  58871. +
  58872. + .driver = {
  58873. + .name = (char *) shortname,
  58874. + // .shutdown = ...
  58875. + // .suspend = ...
  58876. + // .resume = ...
  58877. + },
  58878. +};
  58879. +
  58880. +MODULE_AUTHOR ("David Brownell");
  58881. +MODULE_LICENSE ("Dual BSD/GPL");
  58882. +
  58883. +static struct proc_dir_entry *pdir, *pfile;
  58884. +
  58885. +static int isoc_read_data (char *page, char **start,
  58886. + off_t off, int count,
  58887. + int *eof, void *data)
  58888. +{
  58889. + int i;
  58890. + static int c = 0;
  58891. + static int done = 0;
  58892. + static int s = 0;
  58893. +
  58894. +/*
  58895. + printk ("\ncount: %d\n", count);
  58896. + printk ("rbuf_start: %d\n", rbuf_start);
  58897. + printk ("rbuf_len: %d\n", rbuf_len);
  58898. + printk ("off: %d\n", off);
  58899. + printk ("start: %p\n\n", *start);
  58900. +*/
  58901. + if (done) {
  58902. + c = 0;
  58903. + done = 0;
  58904. + *eof = 1;
  58905. + return 0;
  58906. + }
  58907. +
  58908. + if (c == 0) {
  58909. + if (rbuf_len == RBUF_LEN)
  58910. + s = rbuf_start;
  58911. + else s = 0;
  58912. + }
  58913. +
  58914. + for (i=0; i<count && c<rbuf_len; i++, c++) {
  58915. + page[i] = rbuf[(c+s) % RBUF_LEN];
  58916. + }
  58917. + *start = page;
  58918. +
  58919. + if (c >= rbuf_len) {
  58920. + *eof = 1;
  58921. + done = 1;
  58922. + }
  58923. +
  58924. +
  58925. + return i;
  58926. +}
  58927. +
  58928. +static int __init init (void)
  58929. +{
  58930. +
  58931. + int retval = 0;
  58932. +
  58933. + pdir = proc_mkdir("isoc_test", NULL);
  58934. + if(pdir == NULL) {
  58935. + retval = -ENOMEM;
  58936. + printk("Error creating dir\n");
  58937. + goto done;
  58938. + }
  58939. + pdir->owner = THIS_MODULE;
  58940. +
  58941. + pfile = create_proc_read_entry("isoc_data",
  58942. + 0444, pdir,
  58943. + isoc_read_data,
  58944. + NULL);
  58945. + if (pfile == NULL) {
  58946. + retval = -ENOMEM;
  58947. + printk("Error creating file\n");
  58948. + goto no_file;
  58949. + }
  58950. + pfile->owner = THIS_MODULE;
  58951. +
  58952. + return usb_gadget_register_driver (&zero_driver);
  58953. +
  58954. + no_file:
  58955. + remove_proc_entry("isoc_data", NULL);
  58956. + done:
  58957. + return retval;
  58958. +}
  58959. +module_init (init);
  58960. +
  58961. +static void __exit cleanup (void)
  58962. +{
  58963. +
  58964. + usb_gadget_unregister_driver (&zero_driver);
  58965. +
  58966. + remove_proc_entry("isoc_data", pdir);
  58967. + remove_proc_entry("isoc_test", NULL);
  58968. +}
  58969. +module_exit (cleanup);
  58970. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_cfi_common.h linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_cfi_common.h
  58971. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_cfi_common.h 1969-12-31 18:00:00.000000000 -0600
  58972. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_cfi_common.h 2014-12-03 19:13:40.216418001 -0600
  58973. @@ -0,0 +1,142 @@
  58974. +/* ==========================================================================
  58975. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  58976. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  58977. + * otherwise expressly agreed to in writing between Synopsys and you.
  58978. + *
  58979. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  58980. + * any End User Software License Agreement or Agreement for Licensed Product
  58981. + * with Synopsys or any supplement thereto. You are permitted to use and
  58982. + * redistribute this Software in source and binary forms, with or without
  58983. + * modification, provided that redistributions of source code must retain this
  58984. + * notice. You may not view, use, disclose, copy or distribute this file or
  58985. + * any information contained herein except pursuant to this license grant from
  58986. + * Synopsys. If you do not agree with this notice, including the disclaimer
  58987. + * below, then you are not authorized to use the Software.
  58988. + *
  58989. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  58990. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  58991. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  58992. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  58993. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  58994. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  58995. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  58996. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  58997. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  58998. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  58999. + * DAMAGE.
  59000. + * ========================================================================== */
  59001. +
  59002. +#if !defined(__DWC_CFI_COMMON_H__)
  59003. +#define __DWC_CFI_COMMON_H__
  59004. +
  59005. +//#include <linux/types.h>
  59006. +
  59007. +/**
  59008. + * @file
  59009. + *
  59010. + * This file contains the CFI specific common constants, interfaces
  59011. + * (functions and macros) and structures for Linux. No PCD specific
  59012. + * data structure or definition is to be included in this file.
  59013. + *
  59014. + */
  59015. +
  59016. +/** This is a request for all Core Features */
  59017. +#define VEN_CORE_GET_FEATURES 0xB1
  59018. +
  59019. +/** This is a request to get the value of a specific Core Feature */
  59020. +#define VEN_CORE_GET_FEATURE 0xB2
  59021. +
  59022. +/** This command allows the host to set the value of a specific Core Feature */
  59023. +#define VEN_CORE_SET_FEATURE 0xB3
  59024. +
  59025. +/** This command allows the host to set the default values of
  59026. + * either all or any specific Core Feature
  59027. + */
  59028. +#define VEN_CORE_RESET_FEATURES 0xB4
  59029. +
  59030. +/** This command forces the PCD to write the deferred values of a Core Features */
  59031. +#define VEN_CORE_ACTIVATE_FEATURES 0xB5
  59032. +
  59033. +/** This request reads a DWORD value from a register at the specified offset */
  59034. +#define VEN_CORE_READ_REGISTER 0xB6
  59035. +
  59036. +/** This request writes a DWORD value into a register at the specified offset */
  59037. +#define VEN_CORE_WRITE_REGISTER 0xB7
  59038. +
  59039. +/** This structure is the header of the Core Features dataset returned to
  59040. + * the Host
  59041. + */
  59042. +struct cfi_all_features_header {
  59043. +/** The features header structure length is */
  59044. +#define CFI_ALL_FEATURES_HDR_LEN 8
  59045. + /**
  59046. + * The total length of the features dataset returned to the Host
  59047. + */
  59048. + uint16_t wTotalLen;
  59049. +
  59050. + /**
  59051. + * CFI version number inBinary-Coded Decimal (i.e., 1.00 is 100H).
  59052. + * This field identifies the version of the CFI Specification with which
  59053. + * the device is compliant.
  59054. + */
  59055. + uint16_t wVersion;
  59056. +
  59057. + /** The ID of the Core */
  59058. + uint16_t wCoreID;
  59059. +#define CFI_CORE_ID_UDC 1
  59060. +#define CFI_CORE_ID_OTG 2
  59061. +#define CFI_CORE_ID_WUDEV 3
  59062. +
  59063. + /** Number of features returned by VEN_CORE_GET_FEATURES request */
  59064. + uint16_t wNumFeatures;
  59065. +} UPACKED;
  59066. +
  59067. +typedef struct cfi_all_features_header cfi_all_features_header_t;
  59068. +
  59069. +/** This structure is a header of the Core Feature descriptor dataset returned to
  59070. + * the Host after the VEN_CORE_GET_FEATURES request
  59071. + */
  59072. +struct cfi_feature_desc_header {
  59073. +#define CFI_FEATURE_DESC_HDR_LEN 8
  59074. +
  59075. + /** The feature ID */
  59076. + uint16_t wFeatureID;
  59077. +
  59078. + /** Length of this feature descriptor in bytes - including the
  59079. + * length of the feature name string
  59080. + */
  59081. + uint16_t wLength;
  59082. +
  59083. + /** The data length of this feature in bytes */
  59084. + uint16_t wDataLength;
  59085. +
  59086. + /**
  59087. + * Attributes of this features
  59088. + * D0: Access rights
  59089. + * 0 - Read/Write
  59090. + * 1 - Read only
  59091. + */
  59092. + uint8_t bmAttributes;
  59093. +#define CFI_FEATURE_ATTR_RO 1
  59094. +#define CFI_FEATURE_ATTR_RW 0
  59095. +
  59096. + /** Length of the feature name in bytes */
  59097. + uint8_t bNameLen;
  59098. +
  59099. + /** The feature name buffer */
  59100. + //uint8_t *name;
  59101. +} UPACKED;
  59102. +
  59103. +typedef struct cfi_feature_desc_header cfi_feature_desc_header_t;
  59104. +
  59105. +/**
  59106. + * This structure describes a NULL terminated string referenced by its id field.
  59107. + * It is very similar to usb_string structure but has the id field type set to 16-bit.
  59108. + */
  59109. +struct cfi_string {
  59110. + uint16_t id;
  59111. + const uint8_t *s;
  59112. +};
  59113. +typedef struct cfi_string cfi_string_t;
  59114. +
  59115. +#endif
  59116. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_adp.c linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_adp.c
  59117. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_adp.c 1969-12-31 18:00:00.000000000 -0600
  59118. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_adp.c 2014-12-03 19:13:40.216418001 -0600
  59119. @@ -0,0 +1,854 @@
  59120. +/* ==========================================================================
  59121. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.c $
  59122. + * $Revision: #12 $
  59123. + * $Date: 2011/10/26 $
  59124. + * $Change: 1873028 $
  59125. + *
  59126. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  59127. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  59128. + * otherwise expressly agreed to in writing between Synopsys and you.
  59129. + *
  59130. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  59131. + * any End User Software License Agreement or Agreement for Licensed Product
  59132. + * with Synopsys or any supplement thereto. You are permitted to use and
  59133. + * redistribute this Software in source and binary forms, with or without
  59134. + * modification, provided that redistributions of source code must retain this
  59135. + * notice. You may not view, use, disclose, copy or distribute this file or
  59136. + * any information contained herein except pursuant to this license grant from
  59137. + * Synopsys. If you do not agree with this notice, including the disclaimer
  59138. + * below, then you are not authorized to use the Software.
  59139. + *
  59140. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  59141. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  59142. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  59143. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  59144. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  59145. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  59146. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  59147. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  59148. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  59149. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  59150. + * DAMAGE.
  59151. + * ========================================================================== */
  59152. +
  59153. +#include "dwc_os.h"
  59154. +#include "dwc_otg_regs.h"
  59155. +#include "dwc_otg_cil.h"
  59156. +#include "dwc_otg_adp.h"
  59157. +
  59158. +/** @file
  59159. + *
  59160. + * This file contains the most of the Attach Detect Protocol implementation for
  59161. + * the driver to support OTG Rev2.0.
  59162. + *
  59163. + */
  59164. +
  59165. +void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value)
  59166. +{
  59167. + adpctl_data_t adpctl;
  59168. +
  59169. + adpctl.d32 = value;
  59170. + adpctl.b.ar = 0x2;
  59171. +
  59172. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  59173. +
  59174. + while (adpctl.b.ar) {
  59175. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  59176. + }
  59177. +
  59178. +}
  59179. +
  59180. +/**
  59181. + * Function is called to read ADP registers
  59182. + */
  59183. +uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if)
  59184. +{
  59185. + adpctl_data_t adpctl;
  59186. +
  59187. + adpctl.d32 = 0;
  59188. + adpctl.b.ar = 0x1;
  59189. +
  59190. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  59191. +
  59192. + while (adpctl.b.ar) {
  59193. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  59194. + }
  59195. +
  59196. + return adpctl.d32;
  59197. +}
  59198. +
  59199. +/**
  59200. + * Function is called to read ADPCTL register and filter Write-clear bits
  59201. + */
  59202. +uint32_t dwc_otg_adp_read_reg_filter(dwc_otg_core_if_t * core_if)
  59203. +{
  59204. + adpctl_data_t adpctl;
  59205. +
  59206. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  59207. + adpctl.b.adp_tmout_int = 0;
  59208. + adpctl.b.adp_prb_int = 0;
  59209. + adpctl.b.adp_tmout_int = 0;
  59210. +
  59211. + return adpctl.d32;
  59212. +}
  59213. +
  59214. +/**
  59215. + * Function is called to write ADP registers
  59216. + */
  59217. +void dwc_otg_adp_modify_reg(dwc_otg_core_if_t * core_if, uint32_t clr,
  59218. + uint32_t set)
  59219. +{
  59220. + dwc_otg_adp_write_reg(core_if,
  59221. + (dwc_otg_adp_read_reg(core_if) & (~clr)) | set);
  59222. +}
  59223. +
  59224. +static void adp_sense_timeout(void *ptr)
  59225. +{
  59226. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  59227. + core_if->adp.sense_timer_started = 0;
  59228. + DWC_PRINTF("ADP SENSE TIMEOUT\n");
  59229. + if (core_if->adp_enable) {
  59230. + dwc_otg_adp_sense_stop(core_if);
  59231. + dwc_otg_adp_probe_start(core_if);
  59232. + }
  59233. +}
  59234. +
  59235. +/**
  59236. + * This function is called when the ADP vbus timer expires. Timeout is 1.1s.
  59237. + */
  59238. +static void adp_vbuson_timeout(void *ptr)
  59239. +{
  59240. + gpwrdn_data_t gpwrdn;
  59241. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  59242. + hprt0_data_t hprt0 = {.d32 = 0 };
  59243. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  59244. + DWC_PRINTF("%s: 1.1 seconds expire after turning on VBUS\n",__FUNCTION__);
  59245. + if (core_if) {
  59246. + core_if->adp.vbuson_timer_started = 0;
  59247. + /* Turn off vbus */
  59248. + hprt0.b.prtpwr = 1;
  59249. + DWC_MODIFY_REG32(core_if->host_if->hprt0, hprt0.d32, 0);
  59250. + gpwrdn.d32 = 0;
  59251. +
  59252. + /* Power off the core */
  59253. + if (core_if->power_down == 2) {
  59254. + /* Enable Wakeup Logic */
  59255. +// gpwrdn.b.wkupactiv = 1;
  59256. + gpwrdn.b.pmuactv = 0;
  59257. + gpwrdn.b.pwrdnrstn = 1;
  59258. + gpwrdn.b.pwrdnclmp = 1;
  59259. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  59260. + gpwrdn.d32);
  59261. +
  59262. + /* Suspend the Phy Clock */
  59263. + pcgcctl.b.stoppclk = 1;
  59264. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  59265. +
  59266. + /* Switch on VDD */
  59267. +// gpwrdn.b.wkupactiv = 1;
  59268. + gpwrdn.b.pmuactv = 1;
  59269. + gpwrdn.b.pwrdnrstn = 1;
  59270. + gpwrdn.b.pwrdnclmp = 1;
  59271. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  59272. + gpwrdn.d32);
  59273. + } else {
  59274. + /* Enable Power Down Logic */
  59275. + gpwrdn.b.pmuintsel = 1;
  59276. + gpwrdn.b.pmuactv = 1;
  59277. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  59278. + }
  59279. +
  59280. + /* Power off the core */
  59281. + if (core_if->power_down == 2) {
  59282. + gpwrdn.d32 = 0;
  59283. + gpwrdn.b.pwrdnswtch = 1;
  59284. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn,
  59285. + gpwrdn.d32, 0);
  59286. + }
  59287. +
  59288. + /* Unmask SRP detected interrupt from Power Down Logic */
  59289. + gpwrdn.d32 = 0;
  59290. + gpwrdn.b.srp_det_msk = 1;
  59291. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  59292. +
  59293. + dwc_otg_adp_probe_start(core_if);
  59294. + dwc_otg_dump_global_registers(core_if);
  59295. + dwc_otg_dump_host_registers(core_if);
  59296. + }
  59297. +
  59298. +}
  59299. +
  59300. +/**
  59301. + * Start the ADP Initial Probe timer to detect if Port Connected interrupt is
  59302. + * not asserted within 1.1 seconds.
  59303. + *
  59304. + * @param core_if the pointer to core_if strucure.
  59305. + */
  59306. +void dwc_otg_adp_vbuson_timer_start(dwc_otg_core_if_t * core_if)
  59307. +{
  59308. + core_if->adp.vbuson_timer_started = 1;
  59309. + if (core_if->adp.vbuson_timer)
  59310. + {
  59311. + DWC_PRINTF("SCHEDULING VBUSON TIMER\n");
  59312. + /* 1.1 secs + 60ms necessary for cil_hcd_start*/
  59313. + DWC_TIMER_SCHEDULE(core_if->adp.vbuson_timer, 1160);
  59314. + } else {
  59315. + DWC_WARN("VBUSON_TIMER = %p\n",core_if->adp.vbuson_timer);
  59316. + }
  59317. +}
  59318. +
  59319. +#if 0
  59320. +/**
  59321. + * Masks all DWC OTG core interrupts
  59322. + *
  59323. + */
  59324. +static void mask_all_interrupts(dwc_otg_core_if_t * core_if)
  59325. +{
  59326. + int i;
  59327. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  59328. +
  59329. + /* Mask Host Interrupts */
  59330. +
  59331. + /* Clear and disable HCINTs */
  59332. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  59333. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk, 0);
  59334. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcint, 0xFFFFFFFF);
  59335. +
  59336. + }
  59337. +
  59338. + /* Clear and disable HAINT */
  59339. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk, 0x0000);
  59340. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haint, 0xFFFFFFFF);
  59341. +
  59342. + /* Mask Device Interrupts */
  59343. + if (!core_if->multiproc_int_enable) {
  59344. + /* Clear and disable IN Endpoint interrupts */
  59345. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, 0);
  59346. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  59347. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  59348. + diepint, 0xFFFFFFFF);
  59349. + }
  59350. +
  59351. + /* Clear and disable OUT Endpoint interrupts */
  59352. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, 0);
  59353. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  59354. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  59355. + doepint, 0xFFFFFFFF);
  59356. + }
  59357. +
  59358. + /* Clear and disable DAINT */
  59359. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daint,
  59360. + 0xFFFFFFFF);
  59361. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, 0);
  59362. + } else {
  59363. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  59364. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  59365. + diepeachintmsk[i], 0);
  59366. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  59367. + diepint, 0xFFFFFFFF);
  59368. + }
  59369. +
  59370. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  59371. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  59372. + doepeachintmsk[i], 0);
  59373. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  59374. + doepint, 0xFFFFFFFF);
  59375. + }
  59376. +
  59377. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  59378. + 0);
  59379. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachint,
  59380. + 0xFFFFFFFF);
  59381. +
  59382. + }
  59383. +
  59384. + /* Disable interrupts */
  59385. + ahbcfg.b.glblintrmsk = 1;
  59386. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  59387. +
  59388. + /* Disable all interrupts. */
  59389. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  59390. +
  59391. + /* Clear any pending interrupts */
  59392. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  59393. +
  59394. + /* Clear any pending OTG Interrupts */
  59395. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, 0xFFFFFFFF);
  59396. +}
  59397. +
  59398. +/**
  59399. + * Unmask Port Connection Detected interrupt
  59400. + *
  59401. + */
  59402. +static void unmask_conn_det_intr(dwc_otg_core_if_t * core_if)
  59403. +{
  59404. + gintmsk_data_t gintmsk = {.d32 = 0,.b.portintr = 1 };
  59405. +
  59406. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  59407. +}
  59408. +#endif
  59409. +
  59410. +/**
  59411. + * Starts the ADP Probing
  59412. + *
  59413. + * @param core_if the pointer to core_if structure.
  59414. + */
  59415. +uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if)
  59416. +{
  59417. +
  59418. + adpctl_data_t adpctl = {.d32 = 0};
  59419. + gpwrdn_data_t gpwrdn;
  59420. +#if 0
  59421. + adpctl_data_t adpctl_int = {.d32 = 0, .b.adp_prb_int = 1,
  59422. + .b.adp_sns_int = 1, b.adp_tmout_int};
  59423. +#endif
  59424. + dwc_otg_disable_global_interrupts(core_if);
  59425. + DWC_PRINTF("ADP Probe Start\n");
  59426. + core_if->adp.probe_enabled = 1;
  59427. +
  59428. + adpctl.b.adpres = 1;
  59429. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  59430. +
  59431. + while (adpctl.b.adpres) {
  59432. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  59433. + }
  59434. +
  59435. + adpctl.d32 = 0;
  59436. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  59437. +
  59438. + /* In Host mode unmask SRP detected interrupt */
  59439. + gpwrdn.d32 = 0;
  59440. + gpwrdn.b.sts_chngint_msk = 1;
  59441. + if (!gpwrdn.b.idsts) {
  59442. + gpwrdn.b.srp_det_msk = 1;
  59443. + }
  59444. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  59445. +
  59446. + adpctl.b.adp_tmout_int_msk = 1;
  59447. + adpctl.b.adp_prb_int_msk = 1;
  59448. + adpctl.b.prb_dschg = 1;
  59449. + adpctl.b.prb_delta = 1;
  59450. + adpctl.b.prb_per = 1;
  59451. + adpctl.b.adpen = 1;
  59452. + adpctl.b.enaprb = 1;
  59453. +
  59454. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  59455. + DWC_PRINTF("ADP Probe Finish\n");
  59456. + return 0;
  59457. +}
  59458. +
  59459. +/**
  59460. + * Starts the ADP Sense timer to detect if ADP Sense interrupt is not asserted
  59461. + * within 3 seconds.
  59462. + *
  59463. + * @param core_if the pointer to core_if strucure.
  59464. + */
  59465. +void dwc_otg_adp_sense_timer_start(dwc_otg_core_if_t * core_if)
  59466. +{
  59467. + core_if->adp.sense_timer_started = 1;
  59468. + DWC_TIMER_SCHEDULE(core_if->adp.sense_timer, 3000 /* 3 secs */ );
  59469. +}
  59470. +
  59471. +/**
  59472. + * Starts the ADP Sense
  59473. + *
  59474. + * @param core_if the pointer to core_if strucure.
  59475. + */
  59476. +uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if)
  59477. +{
  59478. + adpctl_data_t adpctl;
  59479. +
  59480. + DWC_PRINTF("ADP Sense Start\n");
  59481. +
  59482. + /* Unmask ADP sense interrupt and mask all other from the core */
  59483. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  59484. + adpctl.b.adp_sns_int_msk = 1;
  59485. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  59486. + dwc_otg_disable_global_interrupts(core_if); // vahrama
  59487. +
  59488. + /* Set ADP reset bit*/
  59489. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  59490. + adpctl.b.adpres = 1;
  59491. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  59492. +
  59493. + while (adpctl.b.adpres) {
  59494. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  59495. + }
  59496. +
  59497. + adpctl.b.adpres = 0;
  59498. + adpctl.b.adpen = 1;
  59499. + adpctl.b.enasns = 1;
  59500. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  59501. +
  59502. + dwc_otg_adp_sense_timer_start(core_if);
  59503. +
  59504. + return 0;
  59505. +}
  59506. +
  59507. +/**
  59508. + * Stops the ADP Probing
  59509. + *
  59510. + * @param core_if the pointer to core_if strucure.
  59511. + */
  59512. +uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if)
  59513. +{
  59514. +
  59515. + adpctl_data_t adpctl;
  59516. + DWC_PRINTF("Stop ADP probe\n");
  59517. + core_if->adp.probe_enabled = 0;
  59518. + core_if->adp.probe_counter = 0;
  59519. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  59520. +
  59521. + adpctl.b.adpen = 0;
  59522. + adpctl.b.adp_prb_int = 1;
  59523. + adpctl.b.adp_tmout_int = 1;
  59524. + adpctl.b.adp_sns_int = 1;
  59525. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  59526. +
  59527. + return 0;
  59528. +}
  59529. +
  59530. +/**
  59531. + * Stops the ADP Sensing
  59532. + *
  59533. + * @param core_if the pointer to core_if strucure.
  59534. + */
  59535. +uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if)
  59536. +{
  59537. + adpctl_data_t adpctl;
  59538. +
  59539. + core_if->adp.sense_enabled = 0;
  59540. +
  59541. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  59542. + adpctl.b.enasns = 0;
  59543. + adpctl.b.adp_sns_int = 1;
  59544. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  59545. +
  59546. + return 0;
  59547. +}
  59548. +
  59549. +/**
  59550. + * Called to turn on the VBUS after initial ADP probe in host mode.
  59551. + * If port power was already enabled in cil_hcd_start function then
  59552. + * only schedule a timer.
  59553. + *
  59554. + * @param core_if the pointer to core_if structure.
  59555. + */
  59556. +void dwc_otg_adp_turnon_vbus(dwc_otg_core_if_t * core_if)
  59557. +{
  59558. + hprt0_data_t hprt0 = {.d32 = 0 };
  59559. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  59560. + DWC_PRINTF("Turn on VBUS for 1.1s, port power is %d\n", hprt0.b.prtpwr);
  59561. +
  59562. + if (hprt0.b.prtpwr == 0) {
  59563. + hprt0.b.prtpwr = 1;
  59564. + //DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  59565. + }
  59566. +
  59567. + dwc_otg_adp_vbuson_timer_start(core_if);
  59568. +}
  59569. +
  59570. +/**
  59571. + * Called right after driver is loaded
  59572. + * to perform initial actions for ADP
  59573. + *
  59574. + * @param core_if the pointer to core_if structure.
  59575. + * @param is_host - flag for current mode of operation either from GINTSTS or GPWRDN
  59576. + */
  59577. +void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host)
  59578. +{
  59579. + gpwrdn_data_t gpwrdn;
  59580. +
  59581. + DWC_PRINTF("ADP Initial Start\n");
  59582. + core_if->adp.adp_started = 1;
  59583. +
  59584. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  59585. + dwc_otg_disable_global_interrupts(core_if);
  59586. + if (is_host) {
  59587. + DWC_PRINTF("HOST MODE\n");
  59588. + /* Enable Power Down Logic Interrupt*/
  59589. + gpwrdn.d32 = 0;
  59590. + gpwrdn.b.pmuintsel = 1;
  59591. + gpwrdn.b.pmuactv = 1;
  59592. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  59593. + /* Initialize first ADP probe to obtain Ramp Time value */
  59594. + core_if->adp.initial_probe = 1;
  59595. + dwc_otg_adp_probe_start(core_if);
  59596. + } else {
  59597. + gotgctl_data_t gotgctl;
  59598. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  59599. + DWC_PRINTF("DEVICE MODE\n");
  59600. + if (gotgctl.b.bsesvld == 0) {
  59601. + /* Enable Power Down Logic Interrupt*/
  59602. + gpwrdn.d32 = 0;
  59603. + DWC_PRINTF("VBUS is not valid - start ADP probe\n");
  59604. + gpwrdn.b.pmuintsel = 1;
  59605. + gpwrdn.b.pmuactv = 1;
  59606. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  59607. + core_if->adp.initial_probe = 1;
  59608. + dwc_otg_adp_probe_start(core_if);
  59609. + } else {
  59610. + DWC_PRINTF("VBUS is valid - initialize core as a Device\n");
  59611. + core_if->op_state = B_PERIPHERAL;
  59612. + dwc_otg_core_init(core_if);
  59613. + dwc_otg_enable_global_interrupts(core_if);
  59614. + cil_pcd_start(core_if);
  59615. + dwc_otg_dump_global_registers(core_if);
  59616. + dwc_otg_dump_dev_registers(core_if);
  59617. + }
  59618. + }
  59619. +}
  59620. +
  59621. +void dwc_otg_adp_init(dwc_otg_core_if_t * core_if)
  59622. +{
  59623. + core_if->adp.adp_started = 0;
  59624. + core_if->adp.initial_probe = 0;
  59625. + core_if->adp.probe_timer_values[0] = -1;
  59626. + core_if->adp.probe_timer_values[1] = -1;
  59627. + core_if->adp.probe_enabled = 0;
  59628. + core_if->adp.sense_enabled = 0;
  59629. + core_if->adp.sense_timer_started = 0;
  59630. + core_if->adp.vbuson_timer_started = 0;
  59631. + core_if->adp.probe_counter = 0;
  59632. + core_if->adp.gpwrdn = 0;
  59633. + core_if->adp.attached = DWC_OTG_ADP_UNKOWN;
  59634. + /* Initialize timers */
  59635. + core_if->adp.sense_timer =
  59636. + DWC_TIMER_ALLOC("ADP SENSE TIMER", adp_sense_timeout, core_if);
  59637. + core_if->adp.vbuson_timer =
  59638. + DWC_TIMER_ALLOC("ADP VBUS ON TIMER", adp_vbuson_timeout, core_if);
  59639. + if (!core_if->adp.sense_timer || !core_if->adp.vbuson_timer)
  59640. + {
  59641. + DWC_ERROR("Could not allocate memory for ADP timers\n");
  59642. + }
  59643. +}
  59644. +
  59645. +void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if)
  59646. +{
  59647. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  59648. + gpwrdn.b.pmuintsel = 1;
  59649. + gpwrdn.b.pmuactv = 1;
  59650. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  59651. +
  59652. + if (core_if->adp.probe_enabled)
  59653. + dwc_otg_adp_probe_stop(core_if);
  59654. + if (core_if->adp.sense_enabled)
  59655. + dwc_otg_adp_sense_stop(core_if);
  59656. + if (core_if->adp.sense_timer_started)
  59657. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  59658. + if (core_if->adp.vbuson_timer_started)
  59659. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  59660. + DWC_TIMER_FREE(core_if->adp.sense_timer);
  59661. + DWC_TIMER_FREE(core_if->adp.vbuson_timer);
  59662. +}
  59663. +
  59664. +/////////////////////////////////////////////////////////////////////
  59665. +////////////// ADP Interrupt Handlers ///////////////////////////////
  59666. +/////////////////////////////////////////////////////////////////////
  59667. +/**
  59668. + * This function sets Ramp Timer values
  59669. + */
  59670. +static uint32_t set_timer_value(dwc_otg_core_if_t * core_if, uint32_t val)
  59671. +{
  59672. + if (core_if->adp.probe_timer_values[0] == -1) {
  59673. + core_if->adp.probe_timer_values[0] = val;
  59674. + core_if->adp.probe_timer_values[1] = -1;
  59675. + return 1;
  59676. + } else {
  59677. + core_if->adp.probe_timer_values[1] =
  59678. + core_if->adp.probe_timer_values[0];
  59679. + core_if->adp.probe_timer_values[0] = val;
  59680. + return 0;
  59681. + }
  59682. +}
  59683. +
  59684. +/**
  59685. + * This function compares Ramp Timer values
  59686. + */
  59687. +static uint32_t compare_timer_values(dwc_otg_core_if_t * core_if)
  59688. +{
  59689. + uint32_t diff;
  59690. + if (core_if->adp.probe_timer_values[0]>=core_if->adp.probe_timer_values[1])
  59691. + diff = core_if->adp.probe_timer_values[0]-core_if->adp.probe_timer_values[1];
  59692. + else
  59693. + diff = core_if->adp.probe_timer_values[1]-core_if->adp.probe_timer_values[0];
  59694. + if(diff < 2) {
  59695. + return 0;
  59696. + } else {
  59697. + return 1;
  59698. + }
  59699. +}
  59700. +
  59701. +/**
  59702. + * This function handles ADP Probe Interrupts
  59703. + */
  59704. +static int32_t dwc_otg_adp_handle_prb_intr(dwc_otg_core_if_t * core_if,
  59705. + uint32_t val)
  59706. +{
  59707. + adpctl_data_t adpctl = {.d32 = 0 };
  59708. + gpwrdn_data_t gpwrdn, temp;
  59709. + adpctl.d32 = val;
  59710. +
  59711. + temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  59712. + core_if->adp.probe_counter++;
  59713. + core_if->adp.gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  59714. + if (adpctl.b.rtim == 0 && !temp.b.idsts){
  59715. + DWC_PRINTF("RTIM value is 0\n");
  59716. + goto exit;
  59717. + }
  59718. + if (set_timer_value(core_if, adpctl.b.rtim) &&
  59719. + core_if->adp.initial_probe) {
  59720. + core_if->adp.initial_probe = 0;
  59721. + dwc_otg_adp_probe_stop(core_if);
  59722. + gpwrdn.d32 = 0;
  59723. + gpwrdn.b.pmuactv = 1;
  59724. + gpwrdn.b.pmuintsel = 1;
  59725. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  59726. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  59727. +
  59728. + /* check which value is for device mode and which for Host mode */
  59729. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  59730. + /*
  59731. + * Turn on VBUS after initial ADP probe.
  59732. + */
  59733. + core_if->op_state = A_HOST;
  59734. + dwc_otg_enable_global_interrupts(core_if);
  59735. + DWC_SPINUNLOCK(core_if->lock);
  59736. + cil_hcd_start(core_if);
  59737. + dwc_otg_adp_turnon_vbus(core_if);
  59738. + DWC_SPINLOCK(core_if->lock);
  59739. + } else {
  59740. + /*
  59741. + * Initiate SRP after initial ADP probe.
  59742. + */
  59743. + dwc_otg_enable_global_interrupts(core_if);
  59744. + dwc_otg_initiate_srp(core_if);
  59745. + }
  59746. + } else if (core_if->adp.probe_counter > 2){
  59747. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  59748. + if (compare_timer_values(core_if)) {
  59749. + DWC_PRINTF("Difference in timer values !!! \n");
  59750. +// core_if->adp.attached = DWC_OTG_ADP_ATTACHED;
  59751. + dwc_otg_adp_probe_stop(core_if);
  59752. +
  59753. + /* Power on the core */
  59754. + if (core_if->power_down == 2) {
  59755. + gpwrdn.b.pwrdnswtch = 1;
  59756. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  59757. + gpwrdn, 0, gpwrdn.d32);
  59758. + }
  59759. +
  59760. + /* check which value is for device mode and which for Host mode */
  59761. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  59762. + /* Disable Interrupt from Power Down Logic */
  59763. + gpwrdn.d32 = 0;
  59764. + gpwrdn.b.pmuintsel = 1;
  59765. + gpwrdn.b.pmuactv = 1;
  59766. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  59767. + gpwrdn, gpwrdn.d32, 0);
  59768. +
  59769. + /*
  59770. + * Initialize the Core for Host mode.
  59771. + */
  59772. + core_if->op_state = A_HOST;
  59773. + dwc_otg_core_init(core_if);
  59774. + dwc_otg_enable_global_interrupts(core_if);
  59775. + cil_hcd_start(core_if);
  59776. + } else {
  59777. + gotgctl_data_t gotgctl;
  59778. + /* Mask SRP detected interrupt from Power Down Logic */
  59779. + gpwrdn.d32 = 0;
  59780. + gpwrdn.b.srp_det_msk = 1;
  59781. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  59782. + gpwrdn, gpwrdn.d32, 0);
  59783. +
  59784. + /* Disable Power Down Logic */
  59785. + gpwrdn.d32 = 0;
  59786. + gpwrdn.b.pmuintsel = 1;
  59787. + gpwrdn.b.pmuactv = 1;
  59788. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  59789. + gpwrdn, gpwrdn.d32, 0);
  59790. +
  59791. + /*
  59792. + * Initialize the Core for Device mode.
  59793. + */
  59794. + core_if->op_state = B_PERIPHERAL;
  59795. + dwc_otg_core_init(core_if);
  59796. + dwc_otg_enable_global_interrupts(core_if);
  59797. + cil_pcd_start(core_if);
  59798. +
  59799. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  59800. + if (!gotgctl.b.bsesvld) {
  59801. + dwc_otg_initiate_srp(core_if);
  59802. + }
  59803. + }
  59804. + }
  59805. + if (core_if->power_down == 2) {
  59806. + if (gpwrdn.b.bsessvld) {
  59807. + /* Mask SRP detected interrupt from Power Down Logic */
  59808. + gpwrdn.d32 = 0;
  59809. + gpwrdn.b.srp_det_msk = 1;
  59810. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  59811. +
  59812. + /* Disable Power Down Logic */
  59813. + gpwrdn.d32 = 0;
  59814. + gpwrdn.b.pmuactv = 1;
  59815. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  59816. +
  59817. + /*
  59818. + * Initialize the Core for Device mode.
  59819. + */
  59820. + core_if->op_state = B_PERIPHERAL;
  59821. + dwc_otg_core_init(core_if);
  59822. + dwc_otg_enable_global_interrupts(core_if);
  59823. + cil_pcd_start(core_if);
  59824. + }
  59825. + }
  59826. + }
  59827. +exit:
  59828. + /* Clear interrupt */
  59829. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  59830. + adpctl.b.adp_prb_int = 1;
  59831. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  59832. +
  59833. + return 0;
  59834. +}
  59835. +
  59836. +/**
  59837. + * This function hadles ADP Sense Interrupt
  59838. + */
  59839. +static int32_t dwc_otg_adp_handle_sns_intr(dwc_otg_core_if_t * core_if)
  59840. +{
  59841. + adpctl_data_t adpctl;
  59842. + /* Stop ADP Sense timer */
  59843. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  59844. +
  59845. + /* Restart ADP Sense timer */
  59846. + dwc_otg_adp_sense_timer_start(core_if);
  59847. +
  59848. + /* Clear interrupt */
  59849. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  59850. + adpctl.b.adp_sns_int = 1;
  59851. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  59852. +
  59853. + return 0;
  59854. +}
  59855. +
  59856. +/**
  59857. + * This function handles ADP Probe Interrupts
  59858. + */
  59859. +static int32_t dwc_otg_adp_handle_prb_tmout_intr(dwc_otg_core_if_t * core_if,
  59860. + uint32_t val)
  59861. +{
  59862. + adpctl_data_t adpctl = {.d32 = 0 };
  59863. + adpctl.d32 = val;
  59864. + set_timer_value(core_if, adpctl.b.rtim);
  59865. +
  59866. + /* Clear interrupt */
  59867. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  59868. + adpctl.b.adp_tmout_int = 1;
  59869. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  59870. +
  59871. + return 0;
  59872. +}
  59873. +
  59874. +/**
  59875. + * ADP Interrupt handler.
  59876. + *
  59877. + */
  59878. +int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if)
  59879. +{
  59880. + int retval = 0;
  59881. + adpctl_data_t adpctl = {.d32 = 0};
  59882. +
  59883. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  59884. + DWC_PRINTF("ADPCTL = %08x\n",adpctl.d32);
  59885. +
  59886. + if (adpctl.b.adp_sns_int & adpctl.b.adp_sns_int_msk) {
  59887. + DWC_PRINTF("ADP Sense interrupt\n");
  59888. + retval |= dwc_otg_adp_handle_sns_intr(core_if);
  59889. + }
  59890. + if (adpctl.b.adp_tmout_int & adpctl.b.adp_tmout_int_msk) {
  59891. + DWC_PRINTF("ADP timeout interrupt\n");
  59892. + retval |= dwc_otg_adp_handle_prb_tmout_intr(core_if, adpctl.d32);
  59893. + }
  59894. + if (adpctl.b.adp_prb_int & adpctl.b.adp_prb_int_msk) {
  59895. + DWC_PRINTF("ADP Probe interrupt\n");
  59896. + adpctl.b.adp_prb_int = 1;
  59897. + retval |= dwc_otg_adp_handle_prb_intr(core_if, adpctl.d32);
  59898. + }
  59899. +
  59900. +// dwc_otg_adp_modify_reg(core_if, adpctl.d32, 0);
  59901. + //dwc_otg_adp_write_reg(core_if, adpctl.d32);
  59902. + DWC_PRINTF("RETURN FROM ADP ISR\n");
  59903. +
  59904. + return retval;
  59905. +}
  59906. +
  59907. +/**
  59908. + *
  59909. + * @param core_if Programming view of DWC_otg controller.
  59910. + */
  59911. +int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if)
  59912. +{
  59913. +
  59914. +#ifndef DWC_HOST_ONLY
  59915. + hprt0_data_t hprt0;
  59916. + gpwrdn_data_t gpwrdn;
  59917. + DWC_DEBUGPL(DBG_ANY, "++ Power Down Logic Session Request Interrupt++\n");
  59918. +
  59919. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  59920. + /* check which value is for device mode and which for Host mode */
  59921. + if (!gpwrdn.b.idsts) { /* considered host mode value is 0 */
  59922. + DWC_PRINTF("SRP: Host mode\n");
  59923. +
  59924. + if (core_if->adp_enable) {
  59925. + dwc_otg_adp_probe_stop(core_if);
  59926. +
  59927. + /* Power on the core */
  59928. + if (core_if->power_down == 2) {
  59929. + gpwrdn.b.pwrdnswtch = 1;
  59930. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  59931. + gpwrdn, 0, gpwrdn.d32);
  59932. + }
  59933. +
  59934. + core_if->op_state = A_HOST;
  59935. + dwc_otg_core_init(core_if);
  59936. + dwc_otg_enable_global_interrupts(core_if);
  59937. + cil_hcd_start(core_if);
  59938. + }
  59939. +
  59940. + /* Turn on the port power bit. */
  59941. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  59942. + hprt0.b.prtpwr = 1;
  59943. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  59944. +
  59945. + /* Start the Connection timer. So a message can be displayed
  59946. + * if connect does not occur within 10 seconds. */
  59947. + cil_hcd_session_start(core_if);
  59948. + } else {
  59949. + DWC_PRINTF("SRP: Device mode %s\n", __FUNCTION__);
  59950. + if (core_if->adp_enable) {
  59951. + dwc_otg_adp_probe_stop(core_if);
  59952. +
  59953. + /* Power on the core */
  59954. + if (core_if->power_down == 2) {
  59955. + gpwrdn.b.pwrdnswtch = 1;
  59956. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  59957. + gpwrdn, 0, gpwrdn.d32);
  59958. + }
  59959. +
  59960. + gpwrdn.d32 = 0;
  59961. + gpwrdn.b.pmuactv = 0;
  59962. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  59963. + gpwrdn.d32);
  59964. +
  59965. + core_if->op_state = B_PERIPHERAL;
  59966. + dwc_otg_core_init(core_if);
  59967. + dwc_otg_enable_global_interrupts(core_if);
  59968. + cil_pcd_start(core_if);
  59969. + }
  59970. + }
  59971. +#endif
  59972. + return 1;
  59973. +}
  59974. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_adp.h linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_adp.h
  59975. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_adp.h 1969-12-31 18:00:00.000000000 -0600
  59976. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_adp.h 2014-12-03 19:13:40.216418001 -0600
  59977. @@ -0,0 +1,80 @@
  59978. +/* ==========================================================================
  59979. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.h $
  59980. + * $Revision: #7 $
  59981. + * $Date: 2011/10/24 $
  59982. + * $Change: 1871159 $
  59983. + *
  59984. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  59985. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  59986. + * otherwise expressly agreed to in writing between Synopsys and you.
  59987. + *
  59988. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  59989. + * any End User Software License Agreement or Agreement for Licensed Product
  59990. + * with Synopsys or any supplement thereto. You are permitted to use and
  59991. + * redistribute this Software in source and binary forms, with or without
  59992. + * modification, provided that redistributions of source code must retain this
  59993. + * notice. You may not view, use, disclose, copy or distribute this file or
  59994. + * any information contained herein except pursuant to this license grant from
  59995. + * Synopsys. If you do not agree with this notice, including the disclaimer
  59996. + * below, then you are not authorized to use the Software.
  59997. + *
  59998. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  59999. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  60000. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  60001. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  60002. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  60003. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  60004. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  60005. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  60006. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  60007. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  60008. + * DAMAGE.
  60009. + * ========================================================================== */
  60010. +
  60011. +#ifndef __DWC_OTG_ADP_H__
  60012. +#define __DWC_OTG_ADP_H__
  60013. +
  60014. +/**
  60015. + * @file
  60016. + *
  60017. + * This file contains the Attach Detect Protocol interfaces and defines
  60018. + * (functions) and structures for Linux.
  60019. + *
  60020. + */
  60021. +
  60022. +#define DWC_OTG_ADP_UNATTACHED 0
  60023. +#define DWC_OTG_ADP_ATTACHED 1
  60024. +#define DWC_OTG_ADP_UNKOWN 2
  60025. +
  60026. +typedef struct dwc_otg_adp {
  60027. + uint32_t adp_started;
  60028. + uint32_t initial_probe;
  60029. + int32_t probe_timer_values[2];
  60030. + uint32_t probe_enabled;
  60031. + uint32_t sense_enabled;
  60032. + dwc_timer_t *sense_timer;
  60033. + uint32_t sense_timer_started;
  60034. + dwc_timer_t *vbuson_timer;
  60035. + uint32_t vbuson_timer_started;
  60036. + uint32_t attached;
  60037. + uint32_t probe_counter;
  60038. + uint32_t gpwrdn;
  60039. +} dwc_otg_adp_t;
  60040. +
  60041. +/**
  60042. + * Attach Detect Protocol functions
  60043. + */
  60044. +
  60045. +extern void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value);
  60046. +extern uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if);
  60047. +extern uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if);
  60048. +extern uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if);
  60049. +extern uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if);
  60050. +extern uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if);
  60051. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  60052. +extern void dwc_otg_adp_init(dwc_otg_core_if_t * core_if);
  60053. +extern void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if);
  60054. +extern int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if);
  60055. +extern int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if);
  60056. +
  60057. +#endif //__DWC_OTG_ADP_H__
  60058. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_attr.c linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_attr.c
  60059. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_attr.c 1969-12-31 18:00:00.000000000 -0600
  60060. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_attr.c 2014-12-03 19:13:40.216418001 -0600
  60061. @@ -0,0 +1,1210 @@
  60062. +/* ==========================================================================
  60063. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.c $
  60064. + * $Revision: #44 $
  60065. + * $Date: 2010/11/29 $
  60066. + * $Change: 1636033 $
  60067. + *
  60068. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  60069. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  60070. + * otherwise expressly agreed to in writing between Synopsys and you.
  60071. + *
  60072. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  60073. + * any End User Software License Agreement or Agreement for Licensed Product
  60074. + * with Synopsys or any supplement thereto. You are permitted to use and
  60075. + * redistribute this Software in source and binary forms, with or without
  60076. + * modification, provided that redistributions of source code must retain this
  60077. + * notice. You may not view, use, disclose, copy or distribute this file or
  60078. + * any information contained herein except pursuant to this license grant from
  60079. + * Synopsys. If you do not agree with this notice, including the disclaimer
  60080. + * below, then you are not authorized to use the Software.
  60081. + *
  60082. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  60083. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  60084. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  60085. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  60086. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  60087. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  60088. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  60089. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  60090. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  60091. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  60092. + * DAMAGE.
  60093. + * ========================================================================== */
  60094. +
  60095. +/** @file
  60096. + *
  60097. + * The diagnostic interface will provide access to the controller for
  60098. + * bringing up the hardware and testing. The Linux driver attributes
  60099. + * feature will be used to provide the Linux Diagnostic
  60100. + * Interface. These attributes are accessed through sysfs.
  60101. + */
  60102. +
  60103. +/** @page "Linux Module Attributes"
  60104. + *
  60105. + * The Linux module attributes feature is used to provide the Linux
  60106. + * Diagnostic Interface. These attributes are accessed through sysfs.
  60107. + * The diagnostic interface will provide access to the controller for
  60108. + * bringing up the hardware and testing.
  60109. +
  60110. + The following table shows the attributes.
  60111. + <table>
  60112. + <tr>
  60113. + <td><b> Name</b></td>
  60114. + <td><b> Description</b></td>
  60115. + <td><b> Access</b></td>
  60116. + </tr>
  60117. +
  60118. + <tr>
  60119. + <td> mode </td>
  60120. + <td> Returns the current mode: 0 for device mode, 1 for host mode</td>
  60121. + <td> Read</td>
  60122. + </tr>
  60123. +
  60124. + <tr>
  60125. + <td> hnpcapable </td>
  60126. + <td> Gets or sets the "HNP-capable" bit in the Core USB Configuraton Register.
  60127. + Read returns the current value.</td>
  60128. + <td> Read/Write</td>
  60129. + </tr>
  60130. +
  60131. + <tr>
  60132. + <td> srpcapable </td>
  60133. + <td> Gets or sets the "SRP-capable" bit in the Core USB Configuraton Register.
  60134. + Read returns the current value.</td>
  60135. + <td> Read/Write</td>
  60136. + </tr>
  60137. +
  60138. + <tr>
  60139. + <td> hsic_connect </td>
  60140. + <td> Gets or sets the "HSIC-Connect" bit in the GLPMCFG Register.
  60141. + Read returns the current value.</td>
  60142. + <td> Read/Write</td>
  60143. + </tr>
  60144. +
  60145. + <tr>
  60146. + <td> inv_sel_hsic </td>
  60147. + <td> Gets or sets the "Invert Select HSIC" bit in the GLPMFG Register.
  60148. + Read returns the current value.</td>
  60149. + <td> Read/Write</td>
  60150. + </tr>
  60151. +
  60152. + <tr>
  60153. + <td> hnp </td>
  60154. + <td> Initiates the Host Negotiation Protocol. Read returns the status.</td>
  60155. + <td> Read/Write</td>
  60156. + </tr>
  60157. +
  60158. + <tr>
  60159. + <td> srp </td>
  60160. + <td> Initiates the Session Request Protocol. Read returns the status.</td>
  60161. + <td> Read/Write</td>
  60162. + </tr>
  60163. +
  60164. + <tr>
  60165. + <td> buspower </td>
  60166. + <td> Gets or sets the Power State of the bus (0 - Off or 1 - On)</td>
  60167. + <td> Read/Write</td>
  60168. + </tr>
  60169. +
  60170. + <tr>
  60171. + <td> bussuspend </td>
  60172. + <td> Suspends the USB bus.</td>
  60173. + <td> Read/Write</td>
  60174. + </tr>
  60175. +
  60176. + <tr>
  60177. + <td> busconnected </td>
  60178. + <td> Gets the connection status of the bus</td>
  60179. + <td> Read</td>
  60180. + </tr>
  60181. +
  60182. + <tr>
  60183. + <td> gotgctl </td>
  60184. + <td> Gets or sets the Core Control Status Register.</td>
  60185. + <td> Read/Write</td>
  60186. + </tr>
  60187. +
  60188. + <tr>
  60189. + <td> gusbcfg </td>
  60190. + <td> Gets or sets the Core USB Configuration Register</td>
  60191. + <td> Read/Write</td>
  60192. + </tr>
  60193. +
  60194. + <tr>
  60195. + <td> grxfsiz </td>
  60196. + <td> Gets or sets the Receive FIFO Size Register</td>
  60197. + <td> Read/Write</td>
  60198. + </tr>
  60199. +
  60200. + <tr>
  60201. + <td> gnptxfsiz </td>
  60202. + <td> Gets or sets the non-periodic Transmit Size Register</td>
  60203. + <td> Read/Write</td>
  60204. + </tr>
  60205. +
  60206. + <tr>
  60207. + <td> gpvndctl </td>
  60208. + <td> Gets or sets the PHY Vendor Control Register</td>
  60209. + <td> Read/Write</td>
  60210. + </tr>
  60211. +
  60212. + <tr>
  60213. + <td> ggpio </td>
  60214. + <td> Gets the value in the lower 16-bits of the General Purpose IO Register
  60215. + or sets the upper 16 bits.</td>
  60216. + <td> Read/Write</td>
  60217. + </tr>
  60218. +
  60219. + <tr>
  60220. + <td> guid </td>
  60221. + <td> Gets or sets the value of the User ID Register</td>
  60222. + <td> Read/Write</td>
  60223. + </tr>
  60224. +
  60225. + <tr>
  60226. + <td> gsnpsid </td>
  60227. + <td> Gets the value of the Synopsys ID Regester</td>
  60228. + <td> Read</td>
  60229. + </tr>
  60230. +
  60231. + <tr>
  60232. + <td> devspeed </td>
  60233. + <td> Gets or sets the device speed setting in the DCFG register</td>
  60234. + <td> Read/Write</td>
  60235. + </tr>
  60236. +
  60237. + <tr>
  60238. + <td> enumspeed </td>
  60239. + <td> Gets the device enumeration Speed.</td>
  60240. + <td> Read</td>
  60241. + </tr>
  60242. +
  60243. + <tr>
  60244. + <td> hptxfsiz </td>
  60245. + <td> Gets the value of the Host Periodic Transmit FIFO</td>
  60246. + <td> Read</td>
  60247. + </tr>
  60248. +
  60249. + <tr>
  60250. + <td> hprt0 </td>
  60251. + <td> Gets or sets the value in the Host Port Control and Status Register</td>
  60252. + <td> Read/Write</td>
  60253. + </tr>
  60254. +
  60255. + <tr>
  60256. + <td> regoffset </td>
  60257. + <td> Sets the register offset for the next Register Access</td>
  60258. + <td> Read/Write</td>
  60259. + </tr>
  60260. +
  60261. + <tr>
  60262. + <td> regvalue </td>
  60263. + <td> Gets or sets the value of the register at the offset in the regoffset attribute.</td>
  60264. + <td> Read/Write</td>
  60265. + </tr>
  60266. +
  60267. + <tr>
  60268. + <td> remote_wakeup </td>
  60269. + <td> On read, shows the status of Remote Wakeup. On write, initiates a remote
  60270. + wakeup of the host. When bit 0 is 1 and Remote Wakeup is enabled, the Remote
  60271. + Wakeup signalling bit in the Device Control Register is set for 1
  60272. + milli-second.</td>
  60273. + <td> Read/Write</td>
  60274. + </tr>
  60275. +
  60276. + <tr>
  60277. + <td> rem_wakeup_pwrdn </td>
  60278. + <td> On read, shows the status core - hibernated or not. On write, initiates
  60279. + a remote wakeup of the device from Hibernation. </td>
  60280. + <td> Read/Write</td>
  60281. + </tr>
  60282. +
  60283. + <tr>
  60284. + <td> mode_ch_tim_en </td>
  60285. + <td> This bit is used to enable or disable the host core to wait for 200 PHY
  60286. + clock cycles at the end of Resume to change the opmode signal to the PHY to 00
  60287. + after Suspend or LPM. </td>
  60288. + <td> Read/Write</td>
  60289. + </tr>
  60290. +
  60291. + <tr>
  60292. + <td> fr_interval </td>
  60293. + <td> On read, shows the value of HFIR Frame Interval. On write, dynamically
  60294. + reload HFIR register during runtime. The application can write a value to this
  60295. + register only after the Port Enable bit of the Host Port Control and Status
  60296. + register (HPRT.PrtEnaPort) has been set </td>
  60297. + <td> Read/Write</td>
  60298. + </tr>
  60299. +
  60300. + <tr>
  60301. + <td> disconnect_us </td>
  60302. + <td> On read, shows the status of disconnect_device_us. On write, sets disconnect_us
  60303. + which causes soft disconnect for 100us. Applicable only for device mode of operation.</td>
  60304. + <td> Read/Write</td>
  60305. + </tr>
  60306. +
  60307. + <tr>
  60308. + <td> regdump </td>
  60309. + <td> Dumps the contents of core registers.</td>
  60310. + <td> Read</td>
  60311. + </tr>
  60312. +
  60313. + <tr>
  60314. + <td> spramdump </td>
  60315. + <td> Dumps the contents of core registers.</td>
  60316. + <td> Read</td>
  60317. + </tr>
  60318. +
  60319. + <tr>
  60320. + <td> hcddump </td>
  60321. + <td> Dumps the current HCD state.</td>
  60322. + <td> Read</td>
  60323. + </tr>
  60324. +
  60325. + <tr>
  60326. + <td> hcd_frrem </td>
  60327. + <td> Shows the average value of the Frame Remaining
  60328. + field in the Host Frame Number/Frame Remaining register when an SOF interrupt
  60329. + occurs. This can be used to determine the average interrupt latency. Also
  60330. + shows the average Frame Remaining value for start_transfer and the "a" and
  60331. + "b" sample points. The "a" and "b" sample points may be used during debugging
  60332. + bto determine how long it takes to execute a section of the HCD code.</td>
  60333. + <td> Read</td>
  60334. + </tr>
  60335. +
  60336. + <tr>
  60337. + <td> rd_reg_test </td>
  60338. + <td> Displays the time required to read the GNPTXFSIZ register many times
  60339. + (the output shows the number of times the register is read).
  60340. + <td> Read</td>
  60341. + </tr>
  60342. +
  60343. + <tr>
  60344. + <td> wr_reg_test </td>
  60345. + <td> Displays the time required to write the GNPTXFSIZ register many times
  60346. + (the output shows the number of times the register is written).
  60347. + <td> Read</td>
  60348. + </tr>
  60349. +
  60350. + <tr>
  60351. + <td> lpm_response </td>
  60352. + <td> Gets or sets lpm_response mode. Applicable only in device mode.
  60353. + <td> Write</td>
  60354. + </tr>
  60355. +
  60356. + <tr>
  60357. + <td> sleep_status </td>
  60358. + <td> Shows sleep status of device.
  60359. + <td> Read</td>
  60360. + </tr>
  60361. +
  60362. + </table>
  60363. +
  60364. + Example usage:
  60365. + To get the current mode:
  60366. + cat /sys/devices/lm0/mode
  60367. +
  60368. + To power down the USB:
  60369. + echo 0 > /sys/devices/lm0/buspower
  60370. + */
  60371. +
  60372. +#include "dwc_otg_os_dep.h"
  60373. +#include "dwc_os.h"
  60374. +#include "dwc_otg_driver.h"
  60375. +#include "dwc_otg_attr.h"
  60376. +#include "dwc_otg_core_if.h"
  60377. +#include "dwc_otg_pcd_if.h"
  60378. +#include "dwc_otg_hcd_if.h"
  60379. +
  60380. +/*
  60381. + * MACROs for defining sysfs attribute
  60382. + */
  60383. +#ifdef LM_INTERFACE
  60384. +
  60385. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  60386. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  60387. +{ \
  60388. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  60389. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  60390. + uint32_t val; \
  60391. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  60392. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  60393. +}
  60394. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  60395. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  60396. + const char *buf, size_t count) \
  60397. +{ \
  60398. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  60399. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  60400. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  60401. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  60402. + return count; \
  60403. +}
  60404. +
  60405. +#elif defined(PCI_INTERFACE)
  60406. +
  60407. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  60408. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  60409. +{ \
  60410. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  60411. + uint32_t val; \
  60412. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  60413. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  60414. +}
  60415. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  60416. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  60417. + const char *buf, size_t count) \
  60418. +{ \
  60419. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  60420. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  60421. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  60422. + return count; \
  60423. +}
  60424. +
  60425. +#elif defined(PLATFORM_INTERFACE)
  60426. +
  60427. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  60428. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  60429. +{ \
  60430. + struct platform_device *platform_dev = \
  60431. + container_of(_dev, struct platform_device, dev); \
  60432. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  60433. + uint32_t val; \
  60434. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  60435. + __func__, _dev, platform_dev, otg_dev); \
  60436. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  60437. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  60438. +}
  60439. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  60440. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  60441. + const char *buf, size_t count) \
  60442. +{ \
  60443. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  60444. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  60445. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  60446. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  60447. + return count; \
  60448. +}
  60449. +#endif
  60450. +
  60451. +/*
  60452. + * MACROs for defining sysfs attribute for 32-bit registers
  60453. + */
  60454. +#ifdef LM_INTERFACE
  60455. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  60456. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  60457. +{ \
  60458. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  60459. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  60460. + uint32_t val; \
  60461. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  60462. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  60463. +}
  60464. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  60465. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  60466. + const char *buf, size_t count) \
  60467. +{ \
  60468. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  60469. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  60470. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  60471. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  60472. + return count; \
  60473. +}
  60474. +#elif defined(PCI_INTERFACE)
  60475. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  60476. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  60477. +{ \
  60478. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  60479. + uint32_t val; \
  60480. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  60481. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  60482. +}
  60483. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  60484. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  60485. + const char *buf, size_t count) \
  60486. +{ \
  60487. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  60488. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  60489. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  60490. + return count; \
  60491. +}
  60492. +
  60493. +#elif defined(PLATFORM_INTERFACE)
  60494. +#include "dwc_otg_dbg.h"
  60495. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  60496. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  60497. +{ \
  60498. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  60499. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  60500. + uint32_t val; \
  60501. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  60502. + __func__, _dev, platform_dev, otg_dev); \
  60503. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  60504. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  60505. +}
  60506. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  60507. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  60508. + const char *buf, size_t count) \
  60509. +{ \
  60510. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  60511. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  60512. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  60513. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  60514. + return count; \
  60515. +}
  60516. +
  60517. +#endif
  60518. +
  60519. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RW(_otg_attr_name_,_string_) \
  60520. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  60521. +DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  60522. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  60523. +
  60524. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RO(_otg_attr_name_,_string_) \
  60525. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  60526. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  60527. +
  60528. +#define DWC_OTG_DEVICE_ATTR_REG32_RW(_otg_attr_name_,_addr_,_string_) \
  60529. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  60530. +DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  60531. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  60532. +
  60533. +#define DWC_OTG_DEVICE_ATTR_REG32_RO(_otg_attr_name_,_addr_,_string_) \
  60534. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  60535. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  60536. +
  60537. +/** @name Functions for Show/Store of Attributes */
  60538. +/**@{*/
  60539. +
  60540. +/**
  60541. + * Helper function returning the otg_device structure of the given device
  60542. + */
  60543. +static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  60544. +{
  60545. + dwc_otg_device_t *otg_dev;
  60546. + DWC_OTG_GETDRVDEV(otg_dev, _dev);
  60547. + return otg_dev;
  60548. +}
  60549. +
  60550. +/**
  60551. + * Show the register offset of the Register Access.
  60552. + */
  60553. +static ssize_t regoffset_show(struct device *_dev,
  60554. + struct device_attribute *attr, char *buf)
  60555. +{
  60556. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60557. + return snprintf(buf, sizeof("0xFFFFFFFF\n") + 1, "0x%08x\n",
  60558. + otg_dev->os_dep.reg_offset);
  60559. +}
  60560. +
  60561. +/**
  60562. + * Set the register offset for the next Register Access Read/Write
  60563. + */
  60564. +static ssize_t regoffset_store(struct device *_dev,
  60565. + struct device_attribute *attr,
  60566. + const char *buf, size_t count)
  60567. +{
  60568. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60569. + uint32_t offset = simple_strtoul(buf, NULL, 16);
  60570. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  60571. + if (offset < SZ_256K) {
  60572. +#elif defined(PCI_INTERFACE)
  60573. + if (offset < 0x00040000) {
  60574. +#endif
  60575. + otg_dev->os_dep.reg_offset = offset;
  60576. + } else {
  60577. + dev_err(_dev, "invalid offset\n");
  60578. + }
  60579. +
  60580. + return count;
  60581. +}
  60582. +
  60583. +DEVICE_ATTR(regoffset, S_IRUGO | S_IWUSR, regoffset_show, regoffset_store);
  60584. +
  60585. +/**
  60586. + * Show the value of the register at the offset in the reg_offset
  60587. + * attribute.
  60588. + */
  60589. +static ssize_t regvalue_show(struct device *_dev,
  60590. + struct device_attribute *attr, char *buf)
  60591. +{
  60592. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60593. + uint32_t val;
  60594. + volatile uint32_t *addr;
  60595. +
  60596. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  60597. + /* Calculate the address */
  60598. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  60599. + (uint8_t *) otg_dev->os_dep.base);
  60600. + val = DWC_READ_REG32(addr);
  60601. + return snprintf(buf,
  60602. + sizeof("Reg@0xFFFFFFFF = 0xFFFFFFFF\n") + 1,
  60603. + "Reg@0x%06x = 0x%08x\n", otg_dev->os_dep.reg_offset,
  60604. + val);
  60605. + } else {
  60606. + dev_err(_dev, "Invalid offset (0x%0x)\n", otg_dev->os_dep.reg_offset);
  60607. + return sprintf(buf, "invalid offset\n");
  60608. + }
  60609. +}
  60610. +
  60611. +/**
  60612. + * Store the value in the register at the offset in the reg_offset
  60613. + * attribute.
  60614. + *
  60615. + */
  60616. +static ssize_t regvalue_store(struct device *_dev,
  60617. + struct device_attribute *attr,
  60618. + const char *buf, size_t count)
  60619. +{
  60620. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60621. + volatile uint32_t *addr;
  60622. + uint32_t val = simple_strtoul(buf, NULL, 16);
  60623. + //dev_dbg(_dev, "Offset=0x%08x Val=0x%08x\n", otg_dev->reg_offset, val);
  60624. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  60625. + /* Calculate the address */
  60626. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  60627. + (uint8_t *) otg_dev->os_dep.base);
  60628. + DWC_WRITE_REG32(addr, val);
  60629. + } else {
  60630. + dev_err(_dev, "Invalid Register Offset (0x%08x)\n",
  60631. + otg_dev->os_dep.reg_offset);
  60632. + }
  60633. + return count;
  60634. +}
  60635. +
  60636. +DEVICE_ATTR(regvalue, S_IRUGO | S_IWUSR, regvalue_show, regvalue_store);
  60637. +
  60638. +/*
  60639. + * Attributes
  60640. + */
  60641. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(mode, "Mode");
  60642. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hnpcapable, "HNPCapable");
  60643. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(srpcapable, "SRPCapable");
  60644. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hsic_connect, "HSIC Connect");
  60645. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(inv_sel_hsic, "Invert Select HSIC");
  60646. +
  60647. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(buspower,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  60648. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(bussuspend,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  60649. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(busconnected, "Bus Connected");
  60650. +
  60651. +DWC_OTG_DEVICE_ATTR_REG32_RW(gotgctl, 0, "GOTGCTL");
  60652. +DWC_OTG_DEVICE_ATTR_REG32_RW(gusbcfg,
  60653. + &(otg_dev->core_if->core_global_regs->gusbcfg),
  60654. + "GUSBCFG");
  60655. +DWC_OTG_DEVICE_ATTR_REG32_RW(grxfsiz,
  60656. + &(otg_dev->core_if->core_global_regs->grxfsiz),
  60657. + "GRXFSIZ");
  60658. +DWC_OTG_DEVICE_ATTR_REG32_RW(gnptxfsiz,
  60659. + &(otg_dev->core_if->core_global_regs->gnptxfsiz),
  60660. + "GNPTXFSIZ");
  60661. +DWC_OTG_DEVICE_ATTR_REG32_RW(gpvndctl,
  60662. + &(otg_dev->core_if->core_global_regs->gpvndctl),
  60663. + "GPVNDCTL");
  60664. +DWC_OTG_DEVICE_ATTR_REG32_RW(ggpio,
  60665. + &(otg_dev->core_if->core_global_regs->ggpio),
  60666. + "GGPIO");
  60667. +DWC_OTG_DEVICE_ATTR_REG32_RW(guid, &(otg_dev->core_if->core_global_regs->guid),
  60668. + "GUID");
  60669. +DWC_OTG_DEVICE_ATTR_REG32_RO(gsnpsid,
  60670. + &(otg_dev->core_if->core_global_regs->gsnpsid),
  60671. + "GSNPSID");
  60672. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(devspeed, "Device Speed");
  60673. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(enumspeed, "Device Enumeration Speed");
  60674. +
  60675. +DWC_OTG_DEVICE_ATTR_REG32_RO(hptxfsiz,
  60676. + &(otg_dev->core_if->core_global_regs->hptxfsiz),
  60677. + "HPTXFSIZ");
  60678. +DWC_OTG_DEVICE_ATTR_REG32_RW(hprt0, otg_dev->core_if->host_if->hprt0, "HPRT0");
  60679. +
  60680. +/**
  60681. + * @todo Add code to initiate the HNP.
  60682. + */
  60683. +/**
  60684. + * Show the HNP status bit
  60685. + */
  60686. +static ssize_t hnp_show(struct device *_dev,
  60687. + struct device_attribute *attr, char *buf)
  60688. +{
  60689. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60690. + return sprintf(buf, "HstNegScs = 0x%x\n",
  60691. + dwc_otg_get_hnpstatus(otg_dev->core_if));
  60692. +}
  60693. +
  60694. +/**
  60695. + * Set the HNP Request bit
  60696. + */
  60697. +static ssize_t hnp_store(struct device *_dev,
  60698. + struct device_attribute *attr,
  60699. + const char *buf, size_t count)
  60700. +{
  60701. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60702. + uint32_t in = simple_strtoul(buf, NULL, 16);
  60703. + dwc_otg_set_hnpreq(otg_dev->core_if, in);
  60704. + return count;
  60705. +}
  60706. +
  60707. +DEVICE_ATTR(hnp, 0644, hnp_show, hnp_store);
  60708. +
  60709. +/**
  60710. + * @todo Add code to initiate the SRP.
  60711. + */
  60712. +/**
  60713. + * Show the SRP status bit
  60714. + */
  60715. +static ssize_t srp_show(struct device *_dev,
  60716. + struct device_attribute *attr, char *buf)
  60717. +{
  60718. +#ifndef DWC_HOST_ONLY
  60719. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60720. + return sprintf(buf, "SesReqScs = 0x%x\n",
  60721. + dwc_otg_get_srpstatus(otg_dev->core_if));
  60722. +#else
  60723. + return sprintf(buf, "Host Only Mode!\n");
  60724. +#endif
  60725. +}
  60726. +
  60727. +/**
  60728. + * Set the SRP Request bit
  60729. + */
  60730. +static ssize_t srp_store(struct device *_dev,
  60731. + struct device_attribute *attr,
  60732. + const char *buf, size_t count)
  60733. +{
  60734. +#ifndef DWC_HOST_ONLY
  60735. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60736. + dwc_otg_pcd_initiate_srp(otg_dev->pcd);
  60737. +#endif
  60738. + return count;
  60739. +}
  60740. +
  60741. +DEVICE_ATTR(srp, 0644, srp_show, srp_store);
  60742. +
  60743. +/**
  60744. + * @todo Need to do more for power on/off?
  60745. + */
  60746. +/**
  60747. + * Show the Bus Power status
  60748. + */
  60749. +static ssize_t buspower_show(struct device *_dev,
  60750. + struct device_attribute *attr, char *buf)
  60751. +{
  60752. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60753. + return sprintf(buf, "Bus Power = 0x%x\n",
  60754. + dwc_otg_get_prtpower(otg_dev->core_if));
  60755. +}
  60756. +
  60757. +/**
  60758. + * Set the Bus Power status
  60759. + */
  60760. +static ssize_t buspower_store(struct device *_dev,
  60761. + struct device_attribute *attr,
  60762. + const char *buf, size_t count)
  60763. +{
  60764. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60765. + uint32_t on = simple_strtoul(buf, NULL, 16);
  60766. + dwc_otg_set_prtpower(otg_dev->core_if, on);
  60767. + return count;
  60768. +}
  60769. +
  60770. +DEVICE_ATTR(buspower, 0644, buspower_show, buspower_store);
  60771. +
  60772. +/**
  60773. + * @todo Need to do more for suspend?
  60774. + */
  60775. +/**
  60776. + * Show the Bus Suspend status
  60777. + */
  60778. +static ssize_t bussuspend_show(struct device *_dev,
  60779. + struct device_attribute *attr, char *buf)
  60780. +{
  60781. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60782. + return sprintf(buf, "Bus Suspend = 0x%x\n",
  60783. + dwc_otg_get_prtsuspend(otg_dev->core_if));
  60784. +}
  60785. +
  60786. +/**
  60787. + * Set the Bus Suspend status
  60788. + */
  60789. +static ssize_t bussuspend_store(struct device *_dev,
  60790. + struct device_attribute *attr,
  60791. + const char *buf, size_t count)
  60792. +{
  60793. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60794. + uint32_t in = simple_strtoul(buf, NULL, 16);
  60795. + dwc_otg_set_prtsuspend(otg_dev->core_if, in);
  60796. + return count;
  60797. +}
  60798. +
  60799. +DEVICE_ATTR(bussuspend, 0644, bussuspend_show, bussuspend_store);
  60800. +
  60801. +/**
  60802. + * Show the Mode Change Ready Timer status
  60803. + */
  60804. +static ssize_t mode_ch_tim_en_show(struct device *_dev,
  60805. + struct device_attribute *attr, char *buf)
  60806. +{
  60807. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60808. + return sprintf(buf, "Mode Change Ready Timer Enable = 0x%x\n",
  60809. + dwc_otg_get_mode_ch_tim(otg_dev->core_if));
  60810. +}
  60811. +
  60812. +/**
  60813. + * Set the Mode Change Ready Timer status
  60814. + */
  60815. +static ssize_t mode_ch_tim_en_store(struct device *_dev,
  60816. + struct device_attribute *attr,
  60817. + const char *buf, size_t count)
  60818. +{
  60819. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60820. + uint32_t in = simple_strtoul(buf, NULL, 16);
  60821. + dwc_otg_set_mode_ch_tim(otg_dev->core_if, in);
  60822. + return count;
  60823. +}
  60824. +
  60825. +DEVICE_ATTR(mode_ch_tim_en, 0644, mode_ch_tim_en_show, mode_ch_tim_en_store);
  60826. +
  60827. +/**
  60828. + * Show the value of HFIR Frame Interval bitfield
  60829. + */
  60830. +static ssize_t fr_interval_show(struct device *_dev,
  60831. + struct device_attribute *attr, char *buf)
  60832. +{
  60833. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60834. + return sprintf(buf, "Frame Interval = 0x%x\n",
  60835. + dwc_otg_get_fr_interval(otg_dev->core_if));
  60836. +}
  60837. +
  60838. +/**
  60839. + * Set the HFIR Frame Interval value
  60840. + */
  60841. +static ssize_t fr_interval_store(struct device *_dev,
  60842. + struct device_attribute *attr,
  60843. + const char *buf, size_t count)
  60844. +{
  60845. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60846. + uint32_t in = simple_strtoul(buf, NULL, 10);
  60847. + dwc_otg_set_fr_interval(otg_dev->core_if, in);
  60848. + return count;
  60849. +}
  60850. +
  60851. +DEVICE_ATTR(fr_interval, 0644, fr_interval_show, fr_interval_store);
  60852. +
  60853. +/**
  60854. + * Show the status of Remote Wakeup.
  60855. + */
  60856. +static ssize_t remote_wakeup_show(struct device *_dev,
  60857. + struct device_attribute *attr, char *buf)
  60858. +{
  60859. +#ifndef DWC_HOST_ONLY
  60860. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60861. +
  60862. + return sprintf(buf,
  60863. + "Remote Wakeup Sig = %d Enabled = %d LPM Remote Wakeup = %d\n",
  60864. + dwc_otg_get_remotewakesig(otg_dev->core_if),
  60865. + dwc_otg_pcd_get_rmwkup_enable(otg_dev->pcd),
  60866. + dwc_otg_get_lpm_remotewakeenabled(otg_dev->core_if));
  60867. +#else
  60868. + return sprintf(buf, "Host Only Mode!\n");
  60869. +#endif /* DWC_HOST_ONLY */
  60870. +}
  60871. +
  60872. +/**
  60873. + * Initiate a remote wakeup of the host. The Device control register
  60874. + * Remote Wakeup Signal bit is written if the PCD Remote wakeup enable
  60875. + * flag is set.
  60876. + *
  60877. + */
  60878. +static ssize_t remote_wakeup_store(struct device *_dev,
  60879. + struct device_attribute *attr,
  60880. + const char *buf, size_t count)
  60881. +{
  60882. +#ifndef DWC_HOST_ONLY
  60883. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60884. + uint32_t val = simple_strtoul(buf, NULL, 16);
  60885. +
  60886. + if (val & 1) {
  60887. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 1);
  60888. + } else {
  60889. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 0);
  60890. + }
  60891. +#endif /* DWC_HOST_ONLY */
  60892. + return count;
  60893. +}
  60894. +
  60895. +DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR, remote_wakeup_show,
  60896. + remote_wakeup_store);
  60897. +
  60898. +/**
  60899. + * Show the whether core is hibernated or not.
  60900. + */
  60901. +static ssize_t rem_wakeup_pwrdn_show(struct device *_dev,
  60902. + struct device_attribute *attr, char *buf)
  60903. +{
  60904. +#ifndef DWC_HOST_ONLY
  60905. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60906. +
  60907. + if (dwc_otg_get_core_state(otg_dev->core_if)) {
  60908. + DWC_PRINTF("Core is in hibernation\n");
  60909. + } else {
  60910. + DWC_PRINTF("Core is not in hibernation\n");
  60911. + }
  60912. +#endif /* DWC_HOST_ONLY */
  60913. + return 0;
  60914. +}
  60915. +
  60916. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  60917. + int rem_wakeup, int reset);
  60918. +
  60919. +/**
  60920. + * Initiate a remote wakeup of the device to exit from hibernation.
  60921. + */
  60922. +static ssize_t rem_wakeup_pwrdn_store(struct device *_dev,
  60923. + struct device_attribute *attr,
  60924. + const char *buf, size_t count)
  60925. +{
  60926. +#ifndef DWC_HOST_ONLY
  60927. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60928. + dwc_otg_device_hibernation_restore(otg_dev->core_if, 1, 0);
  60929. +#endif
  60930. + return count;
  60931. +}
  60932. +
  60933. +DEVICE_ATTR(rem_wakeup_pwrdn, S_IRUGO | S_IWUSR, rem_wakeup_pwrdn_show,
  60934. + rem_wakeup_pwrdn_store);
  60935. +
  60936. +static ssize_t disconnect_us(struct device *_dev,
  60937. + struct device_attribute *attr,
  60938. + const char *buf, size_t count)
  60939. +{
  60940. +
  60941. +#ifndef DWC_HOST_ONLY
  60942. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60943. + uint32_t val = simple_strtoul(buf, NULL, 16);
  60944. + DWC_PRINTF("The Passed value is %04x\n", val);
  60945. +
  60946. + dwc_otg_pcd_disconnect_us(otg_dev->pcd, 50);
  60947. +
  60948. +#endif /* DWC_HOST_ONLY */
  60949. + return count;
  60950. +}
  60951. +
  60952. +DEVICE_ATTR(disconnect_us, S_IWUSR, 0, disconnect_us);
  60953. +
  60954. +/**
  60955. + * Dump global registers and either host or device registers (depending on the
  60956. + * current mode of the core).
  60957. + */
  60958. +static ssize_t regdump_show(struct device *_dev,
  60959. + struct device_attribute *attr, char *buf)
  60960. +{
  60961. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60962. +
  60963. + dwc_otg_dump_global_registers(otg_dev->core_if);
  60964. + if (dwc_otg_is_host_mode(otg_dev->core_if)) {
  60965. + dwc_otg_dump_host_registers(otg_dev->core_if);
  60966. + } else {
  60967. + dwc_otg_dump_dev_registers(otg_dev->core_if);
  60968. +
  60969. + }
  60970. + return sprintf(buf, "Register Dump\n");
  60971. +}
  60972. +
  60973. +DEVICE_ATTR(regdump, S_IRUGO, regdump_show, 0);
  60974. +
  60975. +/**
  60976. + * Dump global registers and either host or device registers (depending on the
  60977. + * current mode of the core).
  60978. + */
  60979. +static ssize_t spramdump_show(struct device *_dev,
  60980. + struct device_attribute *attr, char *buf)
  60981. +{
  60982. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60983. +
  60984. + //dwc_otg_dump_spram(otg_dev->core_if);
  60985. +
  60986. + return sprintf(buf, "SPRAM Dump\n");
  60987. +}
  60988. +
  60989. +DEVICE_ATTR(spramdump, S_IRUGO, spramdump_show, 0);
  60990. +
  60991. +/**
  60992. + * Dump the current hcd state.
  60993. + */
  60994. +static ssize_t hcddump_show(struct device *_dev,
  60995. + struct device_attribute *attr, char *buf)
  60996. +{
  60997. +#ifndef DWC_DEVICE_ONLY
  60998. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  60999. + dwc_otg_hcd_dump_state(otg_dev->hcd);
  61000. +#endif /* DWC_DEVICE_ONLY */
  61001. + return sprintf(buf, "HCD Dump\n");
  61002. +}
  61003. +
  61004. +DEVICE_ATTR(hcddump, S_IRUGO, hcddump_show, 0);
  61005. +
  61006. +/**
  61007. + * Dump the average frame remaining at SOF. This can be used to
  61008. + * determine average interrupt latency. Frame remaining is also shown for
  61009. + * start transfer and two additional sample points.
  61010. + */
  61011. +static ssize_t hcd_frrem_show(struct device *_dev,
  61012. + struct device_attribute *attr, char *buf)
  61013. +{
  61014. +#ifndef DWC_DEVICE_ONLY
  61015. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  61016. +
  61017. + dwc_otg_hcd_dump_frrem(otg_dev->hcd);
  61018. +#endif /* DWC_DEVICE_ONLY */
  61019. + return sprintf(buf, "HCD Dump Frame Remaining\n");
  61020. +}
  61021. +
  61022. +DEVICE_ATTR(hcd_frrem, S_IRUGO, hcd_frrem_show, 0);
  61023. +
  61024. +/**
  61025. + * Displays the time required to read the GNPTXFSIZ register many times (the
  61026. + * output shows the number of times the register is read).
  61027. + */
  61028. +#define RW_REG_COUNT 10000000
  61029. +#define MSEC_PER_JIFFIE 1000/HZ
  61030. +static ssize_t rd_reg_test_show(struct device *_dev,
  61031. + struct device_attribute *attr, char *buf)
  61032. +{
  61033. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  61034. + int i;
  61035. + int time;
  61036. + int start_jiffies;
  61037. +
  61038. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  61039. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  61040. + start_jiffies = jiffies;
  61041. + for (i = 0; i < RW_REG_COUNT; i++) {
  61042. + dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  61043. + }
  61044. + time = jiffies - start_jiffies;
  61045. + return sprintf(buf,
  61046. + "Time to read GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  61047. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  61048. +}
  61049. +
  61050. +DEVICE_ATTR(rd_reg_test, S_IRUGO, rd_reg_test_show, 0);
  61051. +
  61052. +/**
  61053. + * Displays the time required to write the GNPTXFSIZ register many times (the
  61054. + * output shows the number of times the register is written).
  61055. + */
  61056. +static ssize_t wr_reg_test_show(struct device *_dev,
  61057. + struct device_attribute *attr, char *buf)
  61058. +{
  61059. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  61060. + uint32_t reg_val;
  61061. + int i;
  61062. + int time;
  61063. + int start_jiffies;
  61064. +
  61065. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  61066. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  61067. + reg_val = dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  61068. + start_jiffies = jiffies;
  61069. + for (i = 0; i < RW_REG_COUNT; i++) {
  61070. + dwc_otg_set_gnptxfsiz(otg_dev->core_if, reg_val);
  61071. + }
  61072. + time = jiffies - start_jiffies;
  61073. + return sprintf(buf,
  61074. + "Time to write GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  61075. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  61076. +}
  61077. +
  61078. +DEVICE_ATTR(wr_reg_test, S_IRUGO, wr_reg_test_show, 0);
  61079. +
  61080. +#ifdef CONFIG_USB_DWC_OTG_LPM
  61081. +
  61082. +/**
  61083. +* Show the lpm_response attribute.
  61084. +*/
  61085. +static ssize_t lpmresp_show(struct device *_dev,
  61086. + struct device_attribute *attr, char *buf)
  61087. +{
  61088. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  61089. +
  61090. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if))
  61091. + return sprintf(buf, "** LPM is DISABLED **\n");
  61092. +
  61093. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  61094. + return sprintf(buf, "** Current mode is not device mode\n");
  61095. + }
  61096. + return sprintf(buf, "lpm_response = %d\n",
  61097. + dwc_otg_get_lpmresponse(otg_dev->core_if));
  61098. +}
  61099. +
  61100. +/**
  61101. +* Store the lpm_response attribute.
  61102. +*/
  61103. +static ssize_t lpmresp_store(struct device *_dev,
  61104. + struct device_attribute *attr,
  61105. + const char *buf, size_t count)
  61106. +{
  61107. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  61108. + uint32_t val = simple_strtoul(buf, NULL, 16);
  61109. +
  61110. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if)) {
  61111. + return 0;
  61112. + }
  61113. +
  61114. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  61115. + return 0;
  61116. + }
  61117. +
  61118. + dwc_otg_set_lpmresponse(otg_dev->core_if, val);
  61119. + return count;
  61120. +}
  61121. +
  61122. +DEVICE_ATTR(lpm_response, S_IRUGO | S_IWUSR, lpmresp_show, lpmresp_store);
  61123. +
  61124. +/**
  61125. +* Show the sleep_status attribute.
  61126. +*/
  61127. +static ssize_t sleepstatus_show(struct device *_dev,
  61128. + struct device_attribute *attr, char *buf)
  61129. +{
  61130. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  61131. + return sprintf(buf, "Sleep Status = %d\n",
  61132. + dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if));
  61133. +}
  61134. +
  61135. +/**
  61136. + * Store the sleep_status attribure.
  61137. + */
  61138. +static ssize_t sleepstatus_store(struct device *_dev,
  61139. + struct device_attribute *attr,
  61140. + const char *buf, size_t count)
  61141. +{
  61142. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  61143. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  61144. +
  61145. + if (dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if)) {
  61146. + if (dwc_otg_is_host_mode(core_if)) {
  61147. +
  61148. + DWC_PRINTF("Host initiated resume\n");
  61149. + dwc_otg_set_prtresume(otg_dev->core_if, 1);
  61150. + }
  61151. + }
  61152. +
  61153. + return count;
  61154. +}
  61155. +
  61156. +DEVICE_ATTR(sleep_status, S_IRUGO | S_IWUSR, sleepstatus_show,
  61157. + sleepstatus_store);
  61158. +
  61159. +#endif /* CONFIG_USB_DWC_OTG_LPM_ENABLE */
  61160. +
  61161. +/**@}*/
  61162. +
  61163. +/**
  61164. + * Create the device files
  61165. + */
  61166. +void dwc_otg_attr_create(
  61167. +#ifdef LM_INTERFACE
  61168. + struct lm_device *dev
  61169. +#elif defined(PCI_INTERFACE)
  61170. + struct pci_dev *dev
  61171. +#elif defined(PLATFORM_INTERFACE)
  61172. + struct platform_device *dev
  61173. +#endif
  61174. + )
  61175. +{
  61176. + int error;
  61177. +
  61178. + error = device_create_file(&dev->dev, &dev_attr_regoffset);
  61179. + error = device_create_file(&dev->dev, &dev_attr_regvalue);
  61180. + error = device_create_file(&dev->dev, &dev_attr_mode);
  61181. + error = device_create_file(&dev->dev, &dev_attr_hnpcapable);
  61182. + error = device_create_file(&dev->dev, &dev_attr_srpcapable);
  61183. + error = device_create_file(&dev->dev, &dev_attr_hsic_connect);
  61184. + error = device_create_file(&dev->dev, &dev_attr_inv_sel_hsic);
  61185. + error = device_create_file(&dev->dev, &dev_attr_hnp);
  61186. + error = device_create_file(&dev->dev, &dev_attr_srp);
  61187. + error = device_create_file(&dev->dev, &dev_attr_buspower);
  61188. + error = device_create_file(&dev->dev, &dev_attr_bussuspend);
  61189. + error = device_create_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  61190. + error = device_create_file(&dev->dev, &dev_attr_fr_interval);
  61191. + error = device_create_file(&dev->dev, &dev_attr_busconnected);
  61192. + error = device_create_file(&dev->dev, &dev_attr_gotgctl);
  61193. + error = device_create_file(&dev->dev, &dev_attr_gusbcfg);
  61194. + error = device_create_file(&dev->dev, &dev_attr_grxfsiz);
  61195. + error = device_create_file(&dev->dev, &dev_attr_gnptxfsiz);
  61196. + error = device_create_file(&dev->dev, &dev_attr_gpvndctl);
  61197. + error = device_create_file(&dev->dev, &dev_attr_ggpio);
  61198. + error = device_create_file(&dev->dev, &dev_attr_guid);
  61199. + error = device_create_file(&dev->dev, &dev_attr_gsnpsid);
  61200. + error = device_create_file(&dev->dev, &dev_attr_devspeed);
  61201. + error = device_create_file(&dev->dev, &dev_attr_enumspeed);
  61202. + error = device_create_file(&dev->dev, &dev_attr_hptxfsiz);
  61203. + error = device_create_file(&dev->dev, &dev_attr_hprt0);
  61204. + error = device_create_file(&dev->dev, &dev_attr_remote_wakeup);
  61205. + error = device_create_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  61206. + error = device_create_file(&dev->dev, &dev_attr_disconnect_us);
  61207. + error = device_create_file(&dev->dev, &dev_attr_regdump);
  61208. + error = device_create_file(&dev->dev, &dev_attr_spramdump);
  61209. + error = device_create_file(&dev->dev, &dev_attr_hcddump);
  61210. + error = device_create_file(&dev->dev, &dev_attr_hcd_frrem);
  61211. + error = device_create_file(&dev->dev, &dev_attr_rd_reg_test);
  61212. + error = device_create_file(&dev->dev, &dev_attr_wr_reg_test);
  61213. +#ifdef CONFIG_USB_DWC_OTG_LPM
  61214. + error = device_create_file(&dev->dev, &dev_attr_lpm_response);
  61215. + error = device_create_file(&dev->dev, &dev_attr_sleep_status);
  61216. +#endif
  61217. +}
  61218. +
  61219. +/**
  61220. + * Remove the device files
  61221. + */
  61222. +void dwc_otg_attr_remove(
  61223. +#ifdef LM_INTERFACE
  61224. + struct lm_device *dev
  61225. +#elif defined(PCI_INTERFACE)
  61226. + struct pci_dev *dev
  61227. +#elif defined(PLATFORM_INTERFACE)
  61228. + struct platform_device *dev
  61229. +#endif
  61230. + )
  61231. +{
  61232. + device_remove_file(&dev->dev, &dev_attr_regoffset);
  61233. + device_remove_file(&dev->dev, &dev_attr_regvalue);
  61234. + device_remove_file(&dev->dev, &dev_attr_mode);
  61235. + device_remove_file(&dev->dev, &dev_attr_hnpcapable);
  61236. + device_remove_file(&dev->dev, &dev_attr_srpcapable);
  61237. + device_remove_file(&dev->dev, &dev_attr_hsic_connect);
  61238. + device_remove_file(&dev->dev, &dev_attr_inv_sel_hsic);
  61239. + device_remove_file(&dev->dev, &dev_attr_hnp);
  61240. + device_remove_file(&dev->dev, &dev_attr_srp);
  61241. + device_remove_file(&dev->dev, &dev_attr_buspower);
  61242. + device_remove_file(&dev->dev, &dev_attr_bussuspend);
  61243. + device_remove_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  61244. + device_remove_file(&dev->dev, &dev_attr_fr_interval);
  61245. + device_remove_file(&dev->dev, &dev_attr_busconnected);
  61246. + device_remove_file(&dev->dev, &dev_attr_gotgctl);
  61247. + device_remove_file(&dev->dev, &dev_attr_gusbcfg);
  61248. + device_remove_file(&dev->dev, &dev_attr_grxfsiz);
  61249. + device_remove_file(&dev->dev, &dev_attr_gnptxfsiz);
  61250. + device_remove_file(&dev->dev, &dev_attr_gpvndctl);
  61251. + device_remove_file(&dev->dev, &dev_attr_ggpio);
  61252. + device_remove_file(&dev->dev, &dev_attr_guid);
  61253. + device_remove_file(&dev->dev, &dev_attr_gsnpsid);
  61254. + device_remove_file(&dev->dev, &dev_attr_devspeed);
  61255. + device_remove_file(&dev->dev, &dev_attr_enumspeed);
  61256. + device_remove_file(&dev->dev, &dev_attr_hptxfsiz);
  61257. + device_remove_file(&dev->dev, &dev_attr_hprt0);
  61258. + device_remove_file(&dev->dev, &dev_attr_remote_wakeup);
  61259. + device_remove_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  61260. + device_remove_file(&dev->dev, &dev_attr_disconnect_us);
  61261. + device_remove_file(&dev->dev, &dev_attr_regdump);
  61262. + device_remove_file(&dev->dev, &dev_attr_spramdump);
  61263. + device_remove_file(&dev->dev, &dev_attr_hcddump);
  61264. + device_remove_file(&dev->dev, &dev_attr_hcd_frrem);
  61265. + device_remove_file(&dev->dev, &dev_attr_rd_reg_test);
  61266. + device_remove_file(&dev->dev, &dev_attr_wr_reg_test);
  61267. +#ifdef CONFIG_USB_DWC_OTG_LPM
  61268. + device_remove_file(&dev->dev, &dev_attr_lpm_response);
  61269. + device_remove_file(&dev->dev, &dev_attr_sleep_status);
  61270. +#endif
  61271. +}
  61272. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_attr.h linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_attr.h
  61273. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_attr.h 1969-12-31 18:00:00.000000000 -0600
  61274. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_attr.h 2014-12-03 19:13:40.216418001 -0600
  61275. @@ -0,0 +1,89 @@
  61276. +/* ==========================================================================
  61277. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.h $
  61278. + * $Revision: #13 $
  61279. + * $Date: 2010/06/21 $
  61280. + * $Change: 1532021 $
  61281. + *
  61282. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  61283. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  61284. + * otherwise expressly agreed to in writing between Synopsys and you.
  61285. + *
  61286. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  61287. + * any End User Software License Agreement or Agreement for Licensed Product
  61288. + * with Synopsys or any supplement thereto. You are permitted to use and
  61289. + * redistribute this Software in source and binary forms, with or without
  61290. + * modification, provided that redistributions of source code must retain this
  61291. + * notice. You may not view, use, disclose, copy or distribute this file or
  61292. + * any information contained herein except pursuant to this license grant from
  61293. + * Synopsys. If you do not agree with this notice, including the disclaimer
  61294. + * below, then you are not authorized to use the Software.
  61295. + *
  61296. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  61297. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  61298. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  61299. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  61300. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  61301. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  61302. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  61303. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  61304. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  61305. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  61306. + * DAMAGE.
  61307. + * ========================================================================== */
  61308. +
  61309. +#if !defined(__DWC_OTG_ATTR_H__)
  61310. +#define __DWC_OTG_ATTR_H__
  61311. +
  61312. +/** @file
  61313. + * This file contains the interface to the Linux device attributes.
  61314. + */
  61315. +extern struct device_attribute dev_attr_regoffset;
  61316. +extern struct device_attribute dev_attr_regvalue;
  61317. +
  61318. +extern struct device_attribute dev_attr_mode;
  61319. +extern struct device_attribute dev_attr_hnpcapable;
  61320. +extern struct device_attribute dev_attr_srpcapable;
  61321. +extern struct device_attribute dev_attr_hnp;
  61322. +extern struct device_attribute dev_attr_srp;
  61323. +extern struct device_attribute dev_attr_buspower;
  61324. +extern struct device_attribute dev_attr_bussuspend;
  61325. +extern struct device_attribute dev_attr_mode_ch_tim_en;
  61326. +extern struct device_attribute dev_attr_fr_interval;
  61327. +extern struct device_attribute dev_attr_busconnected;
  61328. +extern struct device_attribute dev_attr_gotgctl;
  61329. +extern struct device_attribute dev_attr_gusbcfg;
  61330. +extern struct device_attribute dev_attr_grxfsiz;
  61331. +extern struct device_attribute dev_attr_gnptxfsiz;
  61332. +extern struct device_attribute dev_attr_gpvndctl;
  61333. +extern struct device_attribute dev_attr_ggpio;
  61334. +extern struct device_attribute dev_attr_guid;
  61335. +extern struct device_attribute dev_attr_gsnpsid;
  61336. +extern struct device_attribute dev_attr_devspeed;
  61337. +extern struct device_attribute dev_attr_enumspeed;
  61338. +extern struct device_attribute dev_attr_hptxfsiz;
  61339. +extern struct device_attribute dev_attr_hprt0;
  61340. +#ifdef CONFIG_USB_DWC_OTG_LPM
  61341. +extern struct device_attribute dev_attr_lpm_response;
  61342. +extern struct device_attribute devi_attr_sleep_status;
  61343. +#endif
  61344. +
  61345. +void dwc_otg_attr_create(
  61346. +#ifdef LM_INTERFACE
  61347. + struct lm_device *dev
  61348. +#elif defined(PCI_INTERFACE)
  61349. + struct pci_dev *dev
  61350. +#elif defined(PLATFORM_INTERFACE)
  61351. + struct platform_device *dev
  61352. +#endif
  61353. + );
  61354. +
  61355. +void dwc_otg_attr_remove(
  61356. +#ifdef LM_INTERFACE
  61357. + struct lm_device *dev
  61358. +#elif defined(PCI_INTERFACE)
  61359. + struct pci_dev *dev
  61360. +#elif defined(PLATFORM_INTERFACE)
  61361. + struct platform_device *dev
  61362. +#endif
  61363. + );
  61364. +#endif
  61365. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_cfi.c linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_cfi.c
  61366. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_cfi.c 1969-12-31 18:00:00.000000000 -0600
  61367. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_cfi.c 2014-12-03 19:13:40.216418001 -0600
  61368. @@ -0,0 +1,1876 @@
  61369. +/* ==========================================================================
  61370. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  61371. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  61372. + * otherwise expressly agreed to in writing between Synopsys and you.
  61373. + *
  61374. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  61375. + * any End User Software License Agreement or Agreement for Licensed Product
  61376. + * with Synopsys or any supplement thereto. You are permitted to use and
  61377. + * redistribute this Software in source and binary forms, with or without
  61378. + * modification, provided that redistributions of source code must retain this
  61379. + * notice. You may not view, use, disclose, copy or distribute this file or
  61380. + * any information contained herein except pursuant to this license grant from
  61381. + * Synopsys. If you do not agree with this notice, including the disclaimer
  61382. + * below, then you are not authorized to use the Software.
  61383. + *
  61384. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  61385. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  61386. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  61387. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  61388. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  61389. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  61390. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  61391. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  61392. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  61393. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  61394. + * DAMAGE.
  61395. + * ========================================================================== */
  61396. +
  61397. +/** @file
  61398. + *
  61399. + * This file contains the most of the CFI(Core Feature Interface)
  61400. + * implementation for the OTG.
  61401. + */
  61402. +
  61403. +#ifdef DWC_UTE_CFI
  61404. +
  61405. +#include "dwc_otg_pcd.h"
  61406. +#include "dwc_otg_cfi.h"
  61407. +
  61408. +/** This definition should actually migrate to the Portability Library */
  61409. +#define DWC_CONSTANT_CPU_TO_LE16(x) (x)
  61410. +
  61411. +extern dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex);
  61412. +
  61413. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen);
  61414. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  61415. + struct dwc_otg_pcd *pcd,
  61416. + struct cfi_usb_ctrlrequest *ctrl_req);
  61417. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd);
  61418. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  61419. + struct cfi_usb_ctrlrequest *req);
  61420. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  61421. + struct cfi_usb_ctrlrequest *req);
  61422. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  61423. + struct cfi_usb_ctrlrequest *req);
  61424. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  61425. + struct cfi_usb_ctrlrequest *req);
  61426. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep);
  61427. +
  61428. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if);
  61429. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue);
  61430. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue);
  61431. +
  61432. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if);
  61433. +
  61434. +/** This is the header of the all features descriptor */
  61435. +static cfi_all_features_header_t all_props_desc_header = {
  61436. + .wVersion = DWC_CONSTANT_CPU_TO_LE16(0x100),
  61437. + .wCoreID = DWC_CONSTANT_CPU_TO_LE16(CFI_CORE_ID_OTG),
  61438. + .wNumFeatures = DWC_CONSTANT_CPU_TO_LE16(9),
  61439. +};
  61440. +
  61441. +/** This is an array of statically allocated feature descriptors */
  61442. +static cfi_feature_desc_header_t prop_descs[] = {
  61443. +
  61444. + /* FT_ID_DMA_MODE */
  61445. + {
  61446. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_MODE),
  61447. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  61448. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(1),
  61449. + },
  61450. +
  61451. + /* FT_ID_DMA_BUFFER_SETUP */
  61452. + {
  61453. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFFER_SETUP),
  61454. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  61455. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  61456. + },
  61457. +
  61458. + /* FT_ID_DMA_BUFF_ALIGN */
  61459. + {
  61460. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFF_ALIGN),
  61461. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  61462. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  61463. + },
  61464. +
  61465. + /* FT_ID_DMA_CONCAT_SETUP */
  61466. + {
  61467. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CONCAT_SETUP),
  61468. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  61469. + //.wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  61470. + },
  61471. +
  61472. + /* FT_ID_DMA_CIRCULAR */
  61473. + {
  61474. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CIRCULAR),
  61475. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  61476. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  61477. + },
  61478. +
  61479. + /* FT_ID_THRESHOLD_SETUP */
  61480. + {
  61481. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_THRESHOLD_SETUP),
  61482. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  61483. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  61484. + },
  61485. +
  61486. + /* FT_ID_DFIFO_DEPTH */
  61487. + {
  61488. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DFIFO_DEPTH),
  61489. + .bmAttributes = CFI_FEATURE_ATTR_RO,
  61490. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  61491. + },
  61492. +
  61493. + /* FT_ID_TX_FIFO_DEPTH */
  61494. + {
  61495. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_TX_FIFO_DEPTH),
  61496. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  61497. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  61498. + },
  61499. +
  61500. + /* FT_ID_RX_FIFO_DEPTH */
  61501. + {
  61502. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_RX_FIFO_DEPTH),
  61503. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  61504. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  61505. + }
  61506. +};
  61507. +
  61508. +/** The table of feature names */
  61509. +cfi_string_t prop_name_table[] = {
  61510. + {FT_ID_DMA_MODE, "dma_mode"},
  61511. + {FT_ID_DMA_BUFFER_SETUP, "buffer_setup"},
  61512. + {FT_ID_DMA_BUFF_ALIGN, "buffer_align"},
  61513. + {FT_ID_DMA_CONCAT_SETUP, "concat_setup"},
  61514. + {FT_ID_DMA_CIRCULAR, "buffer_circular"},
  61515. + {FT_ID_THRESHOLD_SETUP, "threshold_setup"},
  61516. + {FT_ID_DFIFO_DEPTH, "dfifo_depth"},
  61517. + {FT_ID_TX_FIFO_DEPTH, "txfifo_depth"},
  61518. + {FT_ID_RX_FIFO_DEPTH, "rxfifo_depth"},
  61519. + {}
  61520. +};
  61521. +
  61522. +/************************************************************************/
  61523. +
  61524. +/**
  61525. + * Returns the name of the feature by its ID
  61526. + * or NULL if no featute ID matches.
  61527. + *
  61528. + */
  61529. +const uint8_t *get_prop_name(uint16_t prop_id, int *len)
  61530. +{
  61531. + cfi_string_t *pstr;
  61532. + *len = 0;
  61533. +
  61534. + for (pstr = prop_name_table; pstr && pstr->s; pstr++) {
  61535. + if (pstr->id == prop_id) {
  61536. + *len = DWC_STRLEN(pstr->s);
  61537. + return pstr->s;
  61538. + }
  61539. + }
  61540. + return NULL;
  61541. +}
  61542. +
  61543. +/**
  61544. + * This function handles all CFI specific control requests.
  61545. + *
  61546. + * Return a negative value to stall the DCE.
  61547. + */
  61548. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl)
  61549. +{
  61550. + int retval = 0;
  61551. + dwc_otg_pcd_ep_t *ep = NULL;
  61552. + cfiobject_t *cfi = pcd->cfi;
  61553. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  61554. + uint16_t wLen = DWC_LE16_TO_CPU(&ctrl->wLength);
  61555. + uint16_t wValue = DWC_LE16_TO_CPU(&ctrl->wValue);
  61556. + uint16_t wIndex = DWC_LE16_TO_CPU(&ctrl->wIndex);
  61557. + uint32_t regaddr = 0;
  61558. + uint32_t regval = 0;
  61559. +
  61560. + /* Save this Control Request in the CFI object.
  61561. + * The data field will be assigned in the data stage completion CB function.
  61562. + */
  61563. + cfi->ctrl_req = *ctrl;
  61564. + cfi->ctrl_req.data = NULL;
  61565. +
  61566. + cfi->need_gadget_att = 0;
  61567. + cfi->need_status_in_complete = 0;
  61568. +
  61569. + switch (ctrl->bRequest) {
  61570. + case VEN_CORE_GET_FEATURES:
  61571. + retval = cfi_core_features_buf(cfi->buf_in.buf, CFI_IN_BUF_LEN);
  61572. + if (retval >= 0) {
  61573. + //dump_msg(cfi->buf_in.buf, retval);
  61574. + ep = &pcd->ep0;
  61575. +
  61576. + retval = min((uint16_t) retval, wLen);
  61577. + /* Transfer this buffer to the host through the EP0-IN EP */
  61578. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  61579. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  61580. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  61581. + ep->dwc_ep.xfer_len = retval;
  61582. + ep->dwc_ep.xfer_count = 0;
  61583. + ep->dwc_ep.sent_zlp = 0;
  61584. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  61585. +
  61586. + pcd->ep0_pending = 1;
  61587. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  61588. + }
  61589. + retval = 0;
  61590. + break;
  61591. +
  61592. + case VEN_CORE_GET_FEATURE:
  61593. + CFI_INFO("VEN_CORE_GET_FEATURE\n");
  61594. + retval = cfi_get_feature_value(cfi->buf_in.buf, CFI_IN_BUF_LEN,
  61595. + pcd, ctrl);
  61596. + if (retval >= 0) {
  61597. + ep = &pcd->ep0;
  61598. +
  61599. + retval = min((uint16_t) retval, wLen);
  61600. + /* Transfer this buffer to the host through the EP0-IN EP */
  61601. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  61602. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  61603. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  61604. + ep->dwc_ep.xfer_len = retval;
  61605. + ep->dwc_ep.xfer_count = 0;
  61606. + ep->dwc_ep.sent_zlp = 0;
  61607. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  61608. +
  61609. + pcd->ep0_pending = 1;
  61610. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  61611. + }
  61612. + CFI_INFO("VEN_CORE_GET_FEATURE=%d\n", retval);
  61613. + dump_msg(cfi->buf_in.buf, retval);
  61614. + break;
  61615. +
  61616. + case VEN_CORE_SET_FEATURE:
  61617. + CFI_INFO("VEN_CORE_SET_FEATURE\n");
  61618. + /* Set up an XFER to get the data stage of the control request,
  61619. + * which is the new value of the feature to be modified.
  61620. + */
  61621. + ep = &pcd->ep0;
  61622. + ep->dwc_ep.is_in = 0;
  61623. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  61624. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  61625. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  61626. + ep->dwc_ep.xfer_len = wLen;
  61627. + ep->dwc_ep.xfer_count = 0;
  61628. + ep->dwc_ep.sent_zlp = 0;
  61629. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  61630. +
  61631. + pcd->ep0_pending = 1;
  61632. + /* Read the control write's data stage */
  61633. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  61634. + retval = 0;
  61635. + break;
  61636. +
  61637. + case VEN_CORE_RESET_FEATURES:
  61638. + CFI_INFO("VEN_CORE_RESET_FEATURES\n");
  61639. + cfi->need_gadget_att = 1;
  61640. + cfi->need_status_in_complete = 1;
  61641. + retval = cfi_preproc_reset(pcd, ctrl);
  61642. + CFI_INFO("VEN_CORE_RESET_FEATURES = (%d)\n", retval);
  61643. + break;
  61644. +
  61645. + case VEN_CORE_ACTIVATE_FEATURES:
  61646. + CFI_INFO("VEN_CORE_ACTIVATE_FEATURES\n");
  61647. + break;
  61648. +
  61649. + case VEN_CORE_READ_REGISTER:
  61650. + CFI_INFO("VEN_CORE_READ_REGISTER\n");
  61651. + /* wValue optionally contains the HI WORD of the register offset and
  61652. + * wIndex contains the LOW WORD of the register offset
  61653. + */
  61654. + if (wValue == 0) {
  61655. + /* @TODO - MAS - fix the access to the base field */
  61656. + regaddr = 0;
  61657. + //regaddr = (uint32_t) pcd->otg_dev->os_dep.base;
  61658. + //GET_CORE_IF(pcd)->co
  61659. + regaddr |= wIndex;
  61660. + } else {
  61661. + regaddr = (wValue << 16) | wIndex;
  61662. + }
  61663. +
  61664. + /* Read a 32-bit value of the memory at the regaddr */
  61665. + regval = DWC_READ_REG32((uint32_t *) regaddr);
  61666. +
  61667. + ep = &pcd->ep0;
  61668. + dwc_memcpy(cfi->buf_in.buf, &regval, sizeof(uint32_t));
  61669. + ep->dwc_ep.is_in = 1;
  61670. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  61671. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  61672. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  61673. + ep->dwc_ep.xfer_len = wLen;
  61674. + ep->dwc_ep.xfer_count = 0;
  61675. + ep->dwc_ep.sent_zlp = 0;
  61676. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  61677. +
  61678. + pcd->ep0_pending = 1;
  61679. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  61680. + cfi->need_gadget_att = 0;
  61681. + retval = 0;
  61682. + break;
  61683. +
  61684. + case VEN_CORE_WRITE_REGISTER:
  61685. + CFI_INFO("VEN_CORE_WRITE_REGISTER\n");
  61686. + /* Set up an XFER to get the data stage of the control request,
  61687. + * which is the new value of the register to be modified.
  61688. + */
  61689. + ep = &pcd->ep0;
  61690. + ep->dwc_ep.is_in = 0;
  61691. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  61692. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  61693. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  61694. + ep->dwc_ep.xfer_len = wLen;
  61695. + ep->dwc_ep.xfer_count = 0;
  61696. + ep->dwc_ep.sent_zlp = 0;
  61697. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  61698. +
  61699. + pcd->ep0_pending = 1;
  61700. + /* Read the control write's data stage */
  61701. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  61702. + retval = 0;
  61703. + break;
  61704. +
  61705. + default:
  61706. + retval = -DWC_E_NOT_SUPPORTED;
  61707. + break;
  61708. + }
  61709. +
  61710. + return retval;
  61711. +}
  61712. +
  61713. +/**
  61714. + * This function prepares the core features descriptors and copies its
  61715. + * raw representation into the buffer <buf>.
  61716. + *
  61717. + * The buffer structure is as follows:
  61718. + * all_features_header (8 bytes)
  61719. + * features_#1 (8 bytes + feature name string length)
  61720. + * features_#2 (8 bytes + feature name string length)
  61721. + * .....
  61722. + * features_#n - where n=the total count of feature descriptors
  61723. + */
  61724. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen)
  61725. +{
  61726. + cfi_feature_desc_header_t *prop_hdr = prop_descs;
  61727. + cfi_feature_desc_header_t *prop;
  61728. + cfi_all_features_header_t *all_props_hdr = &all_props_desc_header;
  61729. + cfi_all_features_header_t *tmp;
  61730. + uint8_t *tmpbuf = buf;
  61731. + const uint8_t *pname = NULL;
  61732. + int i, j, namelen = 0, totlen;
  61733. +
  61734. + /* Prepare and copy the core features into the buffer */
  61735. + CFI_INFO("%s:\n", __func__);
  61736. +
  61737. + tmp = (cfi_all_features_header_t *) tmpbuf;
  61738. + *tmp = *all_props_hdr;
  61739. + tmpbuf += CFI_ALL_FEATURES_HDR_LEN;
  61740. +
  61741. + j = sizeof(prop_descs) / sizeof(cfi_all_features_header_t);
  61742. + for (i = 0; i < j; i++, prop_hdr++) {
  61743. + pname = get_prop_name(prop_hdr->wFeatureID, &namelen);
  61744. + prop = (cfi_feature_desc_header_t *) tmpbuf;
  61745. + *prop = *prop_hdr;
  61746. +
  61747. + prop->bNameLen = namelen;
  61748. + prop->wLength =
  61749. + DWC_CONSTANT_CPU_TO_LE16(CFI_FEATURE_DESC_HDR_LEN +
  61750. + namelen);
  61751. +
  61752. + tmpbuf += CFI_FEATURE_DESC_HDR_LEN;
  61753. + dwc_memcpy(tmpbuf, pname, namelen);
  61754. + tmpbuf += namelen;
  61755. + }
  61756. +
  61757. + totlen = tmpbuf - buf;
  61758. +
  61759. + if (totlen > 0) {
  61760. + tmp = (cfi_all_features_header_t *) buf;
  61761. + tmp->wTotalLen = DWC_CONSTANT_CPU_TO_LE16(totlen);
  61762. + }
  61763. +
  61764. + return totlen;
  61765. +}
  61766. +
  61767. +/**
  61768. + * This function releases all the dynamic memory in the CFI object.
  61769. + */
  61770. +static void cfi_release(cfiobject_t * cfiobj)
  61771. +{
  61772. + cfi_ep_t *cfiep;
  61773. + dwc_list_link_t *tmp;
  61774. +
  61775. + CFI_INFO("%s\n", __func__);
  61776. +
  61777. + if (cfiobj->buf_in.buf) {
  61778. + DWC_DMA_FREE(CFI_IN_BUF_LEN, cfiobj->buf_in.buf,
  61779. + cfiobj->buf_in.addr);
  61780. + cfiobj->buf_in.buf = NULL;
  61781. + }
  61782. +
  61783. + if (cfiobj->buf_out.buf) {
  61784. + DWC_DMA_FREE(CFI_OUT_BUF_LEN, cfiobj->buf_out.buf,
  61785. + cfiobj->buf_out.addr);
  61786. + cfiobj->buf_out.buf = NULL;
  61787. + }
  61788. +
  61789. + /* Free the Buffer Setup values for each EP */
  61790. + //list_for_each_entry(cfiep, &cfiobj->active_eps, lh) {
  61791. + DWC_LIST_FOREACH(tmp, &cfiobj->active_eps) {
  61792. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  61793. + cfi_free_ep_bs_dyn_data(cfiep);
  61794. + }
  61795. +}
  61796. +
  61797. +/**
  61798. + * This function frees the dynamically allocated EP buffer setup data.
  61799. + */
  61800. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep)
  61801. +{
  61802. + if (cfiep->bm_sg) {
  61803. + DWC_FREE(cfiep->bm_sg);
  61804. + cfiep->bm_sg = NULL;
  61805. + }
  61806. +
  61807. + if (cfiep->bm_align) {
  61808. + DWC_FREE(cfiep->bm_align);
  61809. + cfiep->bm_align = NULL;
  61810. + }
  61811. +
  61812. + if (cfiep->bm_concat) {
  61813. + if (NULL != cfiep->bm_concat->wTxBytes) {
  61814. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  61815. + cfiep->bm_concat->wTxBytes = NULL;
  61816. + }
  61817. + DWC_FREE(cfiep->bm_concat);
  61818. + cfiep->bm_concat = NULL;
  61819. + }
  61820. +}
  61821. +
  61822. +/**
  61823. + * This function initializes the default values of the features
  61824. + * for a specific endpoint and should be called only once when
  61825. + * the EP is enabled first time.
  61826. + */
  61827. +static int cfi_ep_init_defaults(struct dwc_otg_pcd *pcd, cfi_ep_t * cfiep)
  61828. +{
  61829. + int retval = 0;
  61830. +
  61831. + cfiep->bm_sg = DWC_ALLOC(sizeof(ddma_sg_buffer_setup_t));
  61832. + if (NULL == cfiep->bm_sg) {
  61833. + CFI_INFO("Failed to allocate memory for SG feature value\n");
  61834. + return -DWC_E_NO_MEMORY;
  61835. + }
  61836. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  61837. +
  61838. + /* For the Concatenation feature's default value we do not allocate
  61839. + * memory for the wTxBytes field - it will be done in the set_feature_value
  61840. + * request handler.
  61841. + */
  61842. + cfiep->bm_concat = DWC_ALLOC(sizeof(ddma_concat_buffer_setup_t));
  61843. + if (NULL == cfiep->bm_concat) {
  61844. + CFI_INFO
  61845. + ("Failed to allocate memory for CONCATENATION feature value\n");
  61846. + DWC_FREE(cfiep->bm_sg);
  61847. + return -DWC_E_NO_MEMORY;
  61848. + }
  61849. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  61850. +
  61851. + cfiep->bm_align = DWC_ALLOC(sizeof(ddma_align_buffer_setup_t));
  61852. + if (NULL == cfiep->bm_align) {
  61853. + CFI_INFO
  61854. + ("Failed to allocate memory for Alignment feature value\n");
  61855. + DWC_FREE(cfiep->bm_sg);
  61856. + DWC_FREE(cfiep->bm_concat);
  61857. + return -DWC_E_NO_MEMORY;
  61858. + }
  61859. + dwc_memset(cfiep->bm_align, 0, sizeof(ddma_align_buffer_setup_t));
  61860. +
  61861. + return retval;
  61862. +}
  61863. +
  61864. +/**
  61865. + * The callback function that notifies the CFI on the activation of
  61866. + * an endpoint in the PCD. The following steps are done in this function:
  61867. + *
  61868. + * Create a dynamically allocated cfi_ep_t object (a CFI wrapper to the PCD's
  61869. + * active endpoint)
  61870. + * Create MAX_DMA_DESCS_PER_EP count DMA Descriptors for the EP
  61871. + * Set the Buffer Mode to standard
  61872. + * Initialize the default values for all EP modes (SG, Circular, Concat, Align)
  61873. + * Add the cfi_ep_t object to the list of active endpoints in the CFI object
  61874. + */
  61875. +static int cfi_ep_enable(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  61876. + struct dwc_otg_pcd_ep *ep)
  61877. +{
  61878. + cfi_ep_t *cfiep;
  61879. + int retval = -DWC_E_NOT_SUPPORTED;
  61880. +
  61881. + CFI_INFO("%s: epname=%s; epnum=0x%02x\n", __func__,
  61882. + "EP_" /*ep->ep.name */ , ep->desc->bEndpointAddress);
  61883. + /* MAS - Check whether this endpoint already is in the list */
  61884. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  61885. +
  61886. + if (NULL == cfiep) {
  61887. + /* Allocate a cfi_ep_t object */
  61888. + cfiep = DWC_ALLOC(sizeof(cfi_ep_t));
  61889. + if (NULL == cfiep) {
  61890. + CFI_INFO
  61891. + ("Unable to allocate memory for <cfiep> in function %s\n",
  61892. + __func__);
  61893. + return -DWC_E_NO_MEMORY;
  61894. + }
  61895. + dwc_memset(cfiep, 0, sizeof(cfi_ep_t));
  61896. +
  61897. + /* Save the dwc_otg_pcd_ep pointer in the cfiep object */
  61898. + cfiep->ep = ep;
  61899. +
  61900. + /* Allocate the DMA Descriptors chain of MAX_DMA_DESCS_PER_EP count */
  61901. + ep->dwc_ep.descs =
  61902. + DWC_DMA_ALLOC(MAX_DMA_DESCS_PER_EP *
  61903. + sizeof(dwc_otg_dma_desc_t),
  61904. + &ep->dwc_ep.descs_dma_addr);
  61905. +
  61906. + if (NULL == ep->dwc_ep.descs) {
  61907. + DWC_FREE(cfiep);
  61908. + return -DWC_E_NO_MEMORY;
  61909. + }
  61910. +
  61911. + DWC_LIST_INIT(&cfiep->lh);
  61912. +
  61913. + /* Set the buffer mode to BM_STANDARD. It will be modified
  61914. + * when building descriptors for a specific buffer mode */
  61915. + ep->dwc_ep.buff_mode = BM_STANDARD;
  61916. +
  61917. + /* Create and initialize the default values for this EP's Buffer modes */
  61918. + if ((retval = cfi_ep_init_defaults(pcd, cfiep)) < 0)
  61919. + return retval;
  61920. +
  61921. + /* Add the cfi_ep_t object to the CFI object's list of active endpoints */
  61922. + DWC_LIST_INSERT_TAIL(&cfi->active_eps, &cfiep->lh);
  61923. + retval = 0;
  61924. + } else { /* The sought EP already is in the list */
  61925. + CFI_INFO("%s: The sought EP already is in the list\n",
  61926. + __func__);
  61927. + }
  61928. +
  61929. + return retval;
  61930. +}
  61931. +
  61932. +/**
  61933. + * This function is called when the data stage of a 3-stage Control Write request
  61934. + * is complete.
  61935. + *
  61936. + */
  61937. +static int cfi_ctrl_write_complete(struct cfiobject *cfi,
  61938. + struct dwc_otg_pcd *pcd)
  61939. +{
  61940. + uint32_t addr, reg_value;
  61941. + uint16_t wIndex, wValue;
  61942. + uint8_t bRequest;
  61943. + uint8_t *buf = cfi->buf_out.buf;
  61944. + //struct usb_ctrlrequest *ctrl_req = &cfi->ctrl_req_saved;
  61945. + struct cfi_usb_ctrlrequest *ctrl_req = &cfi->ctrl_req;
  61946. + int retval = -DWC_E_NOT_SUPPORTED;
  61947. +
  61948. + CFI_INFO("%s\n", __func__);
  61949. +
  61950. + bRequest = ctrl_req->bRequest;
  61951. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  61952. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  61953. +
  61954. + /*
  61955. + * Save the pointer to the data stage in the ctrl_req's <data> field.
  61956. + * The request should be already saved in the command stage by now.
  61957. + */
  61958. + ctrl_req->data = cfi->buf_out.buf;
  61959. + cfi->need_status_in_complete = 0;
  61960. + cfi->need_gadget_att = 0;
  61961. +
  61962. + switch (bRequest) {
  61963. + case VEN_CORE_WRITE_REGISTER:
  61964. + /* The buffer contains raw data of the new value for the register */
  61965. + reg_value = *((uint32_t *) buf);
  61966. + if (wValue == 0) {
  61967. + addr = 0;
  61968. + //addr = (uint32_t) pcd->otg_dev->os_dep.base;
  61969. + addr += wIndex;
  61970. + } else {
  61971. + addr = (wValue << 16) | wIndex;
  61972. + }
  61973. +
  61974. + //writel(reg_value, addr);
  61975. +
  61976. + retval = 0;
  61977. + cfi->need_status_in_complete = 1;
  61978. + break;
  61979. +
  61980. + case VEN_CORE_SET_FEATURE:
  61981. + /* The buffer contains raw data of the new value of the feature */
  61982. + retval = cfi_set_feature_value(pcd);
  61983. + if (retval < 0)
  61984. + return retval;
  61985. +
  61986. + cfi->need_status_in_complete = 1;
  61987. + break;
  61988. +
  61989. + default:
  61990. + break;
  61991. + }
  61992. +
  61993. + return retval;
  61994. +}
  61995. +
  61996. +/**
  61997. + * This function builds the DMA descriptors for the SG buffer mode.
  61998. + */
  61999. +static void cfi_build_sg_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  62000. + dwc_otg_pcd_request_t * req)
  62001. +{
  62002. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  62003. + ddma_sg_buffer_setup_t *sgval = cfiep->bm_sg;
  62004. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  62005. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  62006. + dma_addr_t buff_addr = req->dma;
  62007. + int i;
  62008. + uint32_t txsize, off;
  62009. +
  62010. + txsize = sgval->wSize;
  62011. + off = sgval->bOffset;
  62012. +
  62013. +// CFI_INFO("%s: %s TXSIZE=0x%08x; OFFSET=0x%08x\n",
  62014. +// __func__, cfiep->ep->ep.name, txsize, off);
  62015. +
  62016. + for (i = 0; i < sgval->bCount; i++) {
  62017. + desc->status.b.bs = BS_HOST_BUSY;
  62018. + desc->buf = buff_addr;
  62019. + desc->status.b.l = 0;
  62020. + desc->status.b.ioc = 0;
  62021. + desc->status.b.sp = 0;
  62022. + desc->status.b.bytes = txsize;
  62023. + desc->status.b.bs = BS_HOST_READY;
  62024. +
  62025. + /* Set the next address of the buffer */
  62026. + buff_addr += txsize + off;
  62027. + desc_last = desc;
  62028. + desc++;
  62029. + }
  62030. +
  62031. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  62032. + desc_last->status.b.l = 1;
  62033. + desc_last->status.b.ioc = 1;
  62034. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  62035. + /* Save the last DMA descriptor pointer */
  62036. + cfiep->dma_desc_last = desc_last;
  62037. + cfiep->desc_count = sgval->bCount;
  62038. +}
  62039. +
  62040. +/**
  62041. + * This function builds the DMA descriptors for the Concatenation buffer mode.
  62042. + */
  62043. +static void cfi_build_concat_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  62044. + dwc_otg_pcd_request_t * req)
  62045. +{
  62046. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  62047. + ddma_concat_buffer_setup_t *concatval = cfiep->bm_concat;
  62048. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  62049. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  62050. + dma_addr_t buff_addr = req->dma;
  62051. + int i;
  62052. + uint16_t *txsize;
  62053. +
  62054. + txsize = concatval->wTxBytes;
  62055. +
  62056. + for (i = 0; i < concatval->hdr.bDescCount; i++) {
  62057. + desc->buf = buff_addr;
  62058. + desc->status.b.bs = BS_HOST_BUSY;
  62059. + desc->status.b.l = 0;
  62060. + desc->status.b.ioc = 0;
  62061. + desc->status.b.sp = 0;
  62062. + desc->status.b.bytes = *txsize;
  62063. + desc->status.b.bs = BS_HOST_READY;
  62064. +
  62065. + txsize++;
  62066. + /* Set the next address of the buffer */
  62067. + buff_addr += UGETW(ep->desc->wMaxPacketSize);
  62068. + desc_last = desc;
  62069. + desc++;
  62070. + }
  62071. +
  62072. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  62073. + desc_last->status.b.l = 1;
  62074. + desc_last->status.b.ioc = 1;
  62075. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  62076. + cfiep->dma_desc_last = desc_last;
  62077. + cfiep->desc_count = concatval->hdr.bDescCount;
  62078. +}
  62079. +
  62080. +/**
  62081. + * This function builds the DMA descriptors for the Circular buffer mode
  62082. + */
  62083. +static void cfi_build_circ_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  62084. + dwc_otg_pcd_request_t * req)
  62085. +{
  62086. + /* @todo: MAS - add implementation when this feature needs to be tested */
  62087. +}
  62088. +
  62089. +/**
  62090. + * This function builds the DMA descriptors for the Alignment buffer mode
  62091. + */
  62092. +static void cfi_build_align_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  62093. + dwc_otg_pcd_request_t * req)
  62094. +{
  62095. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  62096. + ddma_align_buffer_setup_t *alignval = cfiep->bm_align;
  62097. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  62098. + dma_addr_t buff_addr = req->dma;
  62099. +
  62100. + desc->status.b.bs = BS_HOST_BUSY;
  62101. + desc->status.b.l = 1;
  62102. + desc->status.b.ioc = 1;
  62103. + desc->status.b.sp = ep->dwc_ep.sent_zlp;
  62104. + desc->status.b.bytes = req->length;
  62105. + /* Adjust the buffer alignment */
  62106. + desc->buf = (buff_addr + alignval->bAlign);
  62107. + desc->status.b.bs = BS_HOST_READY;
  62108. + cfiep->dma_desc_last = desc;
  62109. + cfiep->desc_count = 1;
  62110. +}
  62111. +
  62112. +/**
  62113. + * This function builds the DMA descriptors chain for different modes of the
  62114. + * buffer setup of an endpoint.
  62115. + */
  62116. +static void cfi_build_descriptors(struct cfiobject *cfi,
  62117. + struct dwc_otg_pcd *pcd,
  62118. + struct dwc_otg_pcd_ep *ep,
  62119. + dwc_otg_pcd_request_t * req)
  62120. +{
  62121. + cfi_ep_t *cfiep;
  62122. +
  62123. + /* Get the cfiep by the dwc_otg_pcd_ep */
  62124. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  62125. + if (NULL == cfiep) {
  62126. + CFI_INFO("%s: Unable to find a matching active endpoint\n",
  62127. + __func__);
  62128. + return;
  62129. + }
  62130. +
  62131. + cfiep->xfer_len = req->length;
  62132. +
  62133. + /* Iterate through all the DMA descriptors */
  62134. + switch (cfiep->ep->dwc_ep.buff_mode) {
  62135. + case BM_SG:
  62136. + cfi_build_sg_descs(cfi, cfiep, req);
  62137. + break;
  62138. +
  62139. + case BM_CONCAT:
  62140. + cfi_build_concat_descs(cfi, cfiep, req);
  62141. + break;
  62142. +
  62143. + case BM_CIRCULAR:
  62144. + cfi_build_circ_descs(cfi, cfiep, req);
  62145. + break;
  62146. +
  62147. + case BM_ALIGN:
  62148. + cfi_build_align_descs(cfi, cfiep, req);
  62149. + break;
  62150. +
  62151. + default:
  62152. + break;
  62153. + }
  62154. +}
  62155. +
  62156. +/**
  62157. + * Allocate DMA buffer for different Buffer modes.
  62158. + */
  62159. +static void *cfi_ep_alloc_buf(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  62160. + struct dwc_otg_pcd_ep *ep, dma_addr_t * dma,
  62161. + unsigned size, gfp_t flags)
  62162. +{
  62163. + return DWC_DMA_ALLOC(size, dma);
  62164. +}
  62165. +
  62166. +/**
  62167. + * This function initializes the CFI object.
  62168. + */
  62169. +int init_cfi(cfiobject_t * cfiobj)
  62170. +{
  62171. + CFI_INFO("%s\n", __func__);
  62172. +
  62173. + /* Allocate a buffer for IN XFERs */
  62174. + cfiobj->buf_in.buf =
  62175. + DWC_DMA_ALLOC(CFI_IN_BUF_LEN, &cfiobj->buf_in.addr);
  62176. + if (NULL == cfiobj->buf_in.buf) {
  62177. + CFI_INFO("Unable to allocate buffer for INs\n");
  62178. + return -DWC_E_NO_MEMORY;
  62179. + }
  62180. +
  62181. + /* Allocate a buffer for OUT XFERs */
  62182. + cfiobj->buf_out.buf =
  62183. + DWC_DMA_ALLOC(CFI_OUT_BUF_LEN, &cfiobj->buf_out.addr);
  62184. + if (NULL == cfiobj->buf_out.buf) {
  62185. + CFI_INFO("Unable to allocate buffer for OUT\n");
  62186. + return -DWC_E_NO_MEMORY;
  62187. + }
  62188. +
  62189. + /* Initialize the callback function pointers */
  62190. + cfiobj->ops.release = cfi_release;
  62191. + cfiobj->ops.ep_enable = cfi_ep_enable;
  62192. + cfiobj->ops.ctrl_write_complete = cfi_ctrl_write_complete;
  62193. + cfiobj->ops.build_descriptors = cfi_build_descriptors;
  62194. + cfiobj->ops.ep_alloc_buf = cfi_ep_alloc_buf;
  62195. +
  62196. + /* Initialize the list of active endpoints in the CFI object */
  62197. + DWC_LIST_INIT(&cfiobj->active_eps);
  62198. +
  62199. + return 0;
  62200. +}
  62201. +
  62202. +/**
  62203. + * This function reads the required feature's current value into the buffer
  62204. + *
  62205. + * @retval: Returns negative as error, or the data length of the feature
  62206. + */
  62207. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  62208. + struct dwc_otg_pcd *pcd,
  62209. + struct cfi_usb_ctrlrequest *ctrl_req)
  62210. +{
  62211. + int retval = -DWC_E_NOT_SUPPORTED;
  62212. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  62213. + uint16_t dfifo, rxfifo, txfifo;
  62214. +
  62215. + switch (ctrl_req->wIndex) {
  62216. + /* Whether the DDMA is enabled or not */
  62217. + case FT_ID_DMA_MODE:
  62218. + *buf = (coreif->dma_enable && coreif->dma_desc_enable) ? 1 : 0;
  62219. + retval = 1;
  62220. + break;
  62221. +
  62222. + case FT_ID_DMA_BUFFER_SETUP:
  62223. + retval = cfi_ep_get_sg_val(buf, pcd, ctrl_req);
  62224. + break;
  62225. +
  62226. + case FT_ID_DMA_BUFF_ALIGN:
  62227. + retval = cfi_ep_get_align_val(buf, pcd, ctrl_req);
  62228. + break;
  62229. +
  62230. + case FT_ID_DMA_CONCAT_SETUP:
  62231. + retval = cfi_ep_get_concat_val(buf, pcd, ctrl_req);
  62232. + break;
  62233. +
  62234. + case FT_ID_DMA_CIRCULAR:
  62235. + CFI_INFO("GetFeature value (FT_ID_DMA_CIRCULAR)\n");
  62236. + break;
  62237. +
  62238. + case FT_ID_THRESHOLD_SETUP:
  62239. + CFI_INFO("GetFeature value (FT_ID_THRESHOLD_SETUP)\n");
  62240. + break;
  62241. +
  62242. + case FT_ID_DFIFO_DEPTH:
  62243. + dfifo = get_dfifo_size(coreif);
  62244. + *((uint16_t *) buf) = dfifo;
  62245. + retval = sizeof(uint16_t);
  62246. + break;
  62247. +
  62248. + case FT_ID_TX_FIFO_DEPTH:
  62249. + retval = get_txfifo_size(pcd, ctrl_req->wValue);
  62250. + if (retval >= 0) {
  62251. + txfifo = retval;
  62252. + *((uint16_t *) buf) = txfifo;
  62253. + retval = sizeof(uint16_t);
  62254. + }
  62255. + break;
  62256. +
  62257. + case FT_ID_RX_FIFO_DEPTH:
  62258. + retval = get_rxfifo_size(coreif, ctrl_req->wValue);
  62259. + if (retval >= 0) {
  62260. + rxfifo = retval;
  62261. + *((uint16_t *) buf) = rxfifo;
  62262. + retval = sizeof(uint16_t);
  62263. + }
  62264. + break;
  62265. + }
  62266. +
  62267. + return retval;
  62268. +}
  62269. +
  62270. +/**
  62271. + * This function resets the SG for the specified EP to its default value
  62272. + */
  62273. +static int cfi_reset_sg_val(cfi_ep_t * cfiep)
  62274. +{
  62275. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  62276. + return 0;
  62277. +}
  62278. +
  62279. +/**
  62280. + * This function resets the Alignment for the specified EP to its default value
  62281. + */
  62282. +static int cfi_reset_align_val(cfi_ep_t * cfiep)
  62283. +{
  62284. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  62285. + return 0;
  62286. +}
  62287. +
  62288. +/**
  62289. + * This function resets the Concatenation for the specified EP to its default value
  62290. + * This function will also set the value of the wTxBytes field to NULL after
  62291. + * freeing the memory previously allocated for this field.
  62292. + */
  62293. +static int cfi_reset_concat_val(cfi_ep_t * cfiep)
  62294. +{
  62295. + /* First we need to free the wTxBytes field */
  62296. + if (cfiep->bm_concat->wTxBytes) {
  62297. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  62298. + cfiep->bm_concat->wTxBytes = NULL;
  62299. + }
  62300. +
  62301. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  62302. + return 0;
  62303. +}
  62304. +
  62305. +/**
  62306. + * This function resets all the buffer setups of the specified endpoint
  62307. + */
  62308. +static int cfi_ep_reset_all_setup_vals(cfi_ep_t * cfiep)
  62309. +{
  62310. + cfi_reset_sg_val(cfiep);
  62311. + cfi_reset_align_val(cfiep);
  62312. + cfi_reset_concat_val(cfiep);
  62313. + return 0;
  62314. +}
  62315. +
  62316. +static int cfi_handle_reset_fifo_val(struct dwc_otg_pcd *pcd, uint8_t ep_addr,
  62317. + uint8_t rx_rst, uint8_t tx_rst)
  62318. +{
  62319. + int retval = -DWC_E_INVALID;
  62320. + uint16_t tx_siz[15];
  62321. + uint16_t rx_siz = 0;
  62322. + dwc_otg_pcd_ep_t *ep = NULL;
  62323. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  62324. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  62325. +
  62326. + if (rx_rst) {
  62327. + rx_siz = params->dev_rx_fifo_size;
  62328. + params->dev_rx_fifo_size = GET_CORE_IF(pcd)->init_rxfsiz;
  62329. + }
  62330. +
  62331. + if (tx_rst) {
  62332. + if (ep_addr == 0) {
  62333. + int i;
  62334. +
  62335. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  62336. + tx_siz[i] =
  62337. + core_if->core_params->dev_tx_fifo_size[i];
  62338. + core_if->core_params->dev_tx_fifo_size[i] =
  62339. + core_if->init_txfsiz[i];
  62340. + }
  62341. + } else {
  62342. +
  62343. + ep = get_ep_by_addr(pcd, ep_addr);
  62344. +
  62345. + if (NULL == ep) {
  62346. + CFI_INFO
  62347. + ("%s: Unable to get the endpoint addr=0x%02x\n",
  62348. + __func__, ep_addr);
  62349. + return -DWC_E_INVALID;
  62350. + }
  62351. +
  62352. + tx_siz[0] =
  62353. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num -
  62354. + 1];
  62355. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] =
  62356. + GET_CORE_IF(pcd)->init_txfsiz[ep->
  62357. + dwc_ep.tx_fifo_num -
  62358. + 1];
  62359. + }
  62360. + }
  62361. +
  62362. + if (resize_fifos(GET_CORE_IF(pcd))) {
  62363. + retval = 0;
  62364. + } else {
  62365. + CFI_INFO
  62366. + ("%s: Error resetting the feature Reset All(FIFO size)\n",
  62367. + __func__);
  62368. + if (rx_rst) {
  62369. + params->dev_rx_fifo_size = rx_siz;
  62370. + }
  62371. +
  62372. + if (tx_rst) {
  62373. + if (ep_addr == 0) {
  62374. + int i;
  62375. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps;
  62376. + i++) {
  62377. + core_if->
  62378. + core_params->dev_tx_fifo_size[i] =
  62379. + tx_siz[i];
  62380. + }
  62381. + } else {
  62382. + params->dev_tx_fifo_size[ep->
  62383. + dwc_ep.tx_fifo_num -
  62384. + 1] = tx_siz[0];
  62385. + }
  62386. + }
  62387. + retval = -DWC_E_INVALID;
  62388. + }
  62389. + return retval;
  62390. +}
  62391. +
  62392. +static int cfi_handle_reset_all(struct dwc_otg_pcd *pcd, uint8_t addr)
  62393. +{
  62394. + int retval = 0;
  62395. + cfi_ep_t *cfiep;
  62396. + cfiobject_t *cfi = pcd->cfi;
  62397. + dwc_list_link_t *tmp;
  62398. +
  62399. + retval = cfi_handle_reset_fifo_val(pcd, addr, 1, 1);
  62400. + if (retval < 0) {
  62401. + return retval;
  62402. + }
  62403. +
  62404. + /* If the EP address is known then reset the features for only that EP */
  62405. + if (addr) {
  62406. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  62407. + if (NULL == cfiep) {
  62408. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  62409. + __func__, addr);
  62410. + return -DWC_E_INVALID;
  62411. + }
  62412. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  62413. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  62414. + }
  62415. + /* Otherwise (wValue == 0), reset all features of all EP's */
  62416. + else {
  62417. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  62418. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  62419. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  62420. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  62421. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  62422. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  62423. + if (retval < 0) {
  62424. + CFI_INFO
  62425. + ("%s: Error resetting the feature Reset All\n",
  62426. + __func__);
  62427. + return retval;
  62428. + }
  62429. + }
  62430. + }
  62431. + return retval;
  62432. +}
  62433. +
  62434. +static int cfi_handle_reset_dma_buff_setup(struct dwc_otg_pcd *pcd,
  62435. + uint8_t addr)
  62436. +{
  62437. + int retval = 0;
  62438. + cfi_ep_t *cfiep;
  62439. + cfiobject_t *cfi = pcd->cfi;
  62440. + dwc_list_link_t *tmp;
  62441. +
  62442. + /* If the EP address is known then reset the features for only that EP */
  62443. + if (addr) {
  62444. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  62445. + if (NULL == cfiep) {
  62446. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  62447. + __func__, addr);
  62448. + return -DWC_E_INVALID;
  62449. + }
  62450. + retval = cfi_reset_sg_val(cfiep);
  62451. + }
  62452. + /* Otherwise (wValue == 0), reset all features of all EP's */
  62453. + else {
  62454. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  62455. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  62456. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  62457. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  62458. + retval = cfi_reset_sg_val(cfiep);
  62459. + if (retval < 0) {
  62460. + CFI_INFO
  62461. + ("%s: Error resetting the feature Buffer Setup\n",
  62462. + __func__);
  62463. + return retval;
  62464. + }
  62465. + }
  62466. + }
  62467. + return retval;
  62468. +}
  62469. +
  62470. +static int cfi_handle_reset_concat_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  62471. +{
  62472. + int retval = 0;
  62473. + cfi_ep_t *cfiep;
  62474. + cfiobject_t *cfi = pcd->cfi;
  62475. + dwc_list_link_t *tmp;
  62476. +
  62477. + /* If the EP address is known then reset the features for only that EP */
  62478. + if (addr) {
  62479. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  62480. + if (NULL == cfiep) {
  62481. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  62482. + __func__, addr);
  62483. + return -DWC_E_INVALID;
  62484. + }
  62485. + retval = cfi_reset_concat_val(cfiep);
  62486. + }
  62487. + /* Otherwise (wValue == 0), reset all features of all EP's */
  62488. + else {
  62489. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  62490. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  62491. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  62492. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  62493. + retval = cfi_reset_concat_val(cfiep);
  62494. + if (retval < 0) {
  62495. + CFI_INFO
  62496. + ("%s: Error resetting the feature Concatenation Value\n",
  62497. + __func__);
  62498. + return retval;
  62499. + }
  62500. + }
  62501. + }
  62502. + return retval;
  62503. +}
  62504. +
  62505. +static int cfi_handle_reset_align_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  62506. +{
  62507. + int retval = 0;
  62508. + cfi_ep_t *cfiep;
  62509. + cfiobject_t *cfi = pcd->cfi;
  62510. + dwc_list_link_t *tmp;
  62511. +
  62512. + /* If the EP address is known then reset the features for only that EP */
  62513. + if (addr) {
  62514. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  62515. + if (NULL == cfiep) {
  62516. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  62517. + __func__, addr);
  62518. + return -DWC_E_INVALID;
  62519. + }
  62520. + retval = cfi_reset_align_val(cfiep);
  62521. + }
  62522. + /* Otherwise (wValue == 0), reset all features of all EP's */
  62523. + else {
  62524. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  62525. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  62526. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  62527. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  62528. + retval = cfi_reset_align_val(cfiep);
  62529. + if (retval < 0) {
  62530. + CFI_INFO
  62531. + ("%s: Error resetting the feature Aliignment Value\n",
  62532. + __func__);
  62533. + return retval;
  62534. + }
  62535. + }
  62536. + }
  62537. + return retval;
  62538. +
  62539. +}
  62540. +
  62541. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  62542. + struct cfi_usb_ctrlrequest *req)
  62543. +{
  62544. + int retval = 0;
  62545. +
  62546. + switch (req->wIndex) {
  62547. + case 0:
  62548. + /* Reset all features */
  62549. + retval = cfi_handle_reset_all(pcd, req->wValue & 0xff);
  62550. + break;
  62551. +
  62552. + case FT_ID_DMA_BUFFER_SETUP:
  62553. + /* Reset the SG buffer setup */
  62554. + retval =
  62555. + cfi_handle_reset_dma_buff_setup(pcd, req->wValue & 0xff);
  62556. + break;
  62557. +
  62558. + case FT_ID_DMA_CONCAT_SETUP:
  62559. + /* Reset the Concatenation buffer setup */
  62560. + retval = cfi_handle_reset_concat_val(pcd, req->wValue & 0xff);
  62561. + break;
  62562. +
  62563. + case FT_ID_DMA_BUFF_ALIGN:
  62564. + /* Reset the Alignment buffer setup */
  62565. + retval = cfi_handle_reset_align_val(pcd, req->wValue & 0xff);
  62566. + break;
  62567. +
  62568. + case FT_ID_TX_FIFO_DEPTH:
  62569. + retval =
  62570. + cfi_handle_reset_fifo_val(pcd, req->wValue & 0xff, 0, 1);
  62571. + pcd->cfi->need_gadget_att = 0;
  62572. + break;
  62573. +
  62574. + case FT_ID_RX_FIFO_DEPTH:
  62575. + retval = cfi_handle_reset_fifo_val(pcd, 0, 1, 0);
  62576. + pcd->cfi->need_gadget_att = 0;
  62577. + break;
  62578. + default:
  62579. + break;
  62580. + }
  62581. + return retval;
  62582. +}
  62583. +
  62584. +/**
  62585. + * This function sets a new value for the SG buffer setup.
  62586. + */
  62587. +static int cfi_ep_set_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  62588. +{
  62589. + uint8_t inaddr, outaddr;
  62590. + cfi_ep_t *epin, *epout;
  62591. + ddma_sg_buffer_setup_t *psgval;
  62592. + uint32_t desccount, size;
  62593. +
  62594. + CFI_INFO("%s\n", __func__);
  62595. +
  62596. + psgval = (ddma_sg_buffer_setup_t *) buf;
  62597. + desccount = (uint32_t) psgval->bCount;
  62598. + size = (uint32_t) psgval->wSize;
  62599. +
  62600. + /* Check the DMA descriptor count */
  62601. + if ((desccount > MAX_DMA_DESCS_PER_EP) || (desccount == 0)) {
  62602. + CFI_INFO
  62603. + ("%s: The count of DMA Descriptors should be between 1 and %d\n",
  62604. + __func__, MAX_DMA_DESCS_PER_EP);
  62605. + return -DWC_E_INVALID;
  62606. + }
  62607. +
  62608. + /* Check the DMA descriptor count */
  62609. +
  62610. + if (size == 0) {
  62611. +
  62612. + CFI_INFO("%s: The transfer size should be at least 1 byte\n",
  62613. + __func__);
  62614. +
  62615. + return -DWC_E_INVALID;
  62616. +
  62617. + }
  62618. +
  62619. + inaddr = psgval->bInEndpointAddress;
  62620. + outaddr = psgval->bOutEndpointAddress;
  62621. +
  62622. + epin = get_cfi_ep_by_addr(pcd->cfi, inaddr);
  62623. + epout = get_cfi_ep_by_addr(pcd->cfi, outaddr);
  62624. +
  62625. + if (NULL == epin || NULL == epout) {
  62626. + CFI_INFO
  62627. + ("%s: Unable to get the endpoints inaddr=0x%02x outaddr=0x%02x\n",
  62628. + __func__, inaddr, outaddr);
  62629. + return -DWC_E_INVALID;
  62630. + }
  62631. +
  62632. + epin->ep->dwc_ep.buff_mode = BM_SG;
  62633. + dwc_memcpy(epin->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  62634. +
  62635. + epout->ep->dwc_ep.buff_mode = BM_SG;
  62636. + dwc_memcpy(epout->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  62637. +
  62638. + return 0;
  62639. +}
  62640. +
  62641. +/**
  62642. + * This function sets a new value for the buffer Alignment setup.
  62643. + */
  62644. +static int cfi_ep_set_alignment_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  62645. +{
  62646. + cfi_ep_t *ep;
  62647. + uint8_t addr;
  62648. + ddma_align_buffer_setup_t *palignval;
  62649. +
  62650. + palignval = (ddma_align_buffer_setup_t *) buf;
  62651. + addr = palignval->bEndpointAddress;
  62652. +
  62653. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  62654. +
  62655. + if (NULL == ep) {
  62656. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  62657. + __func__, addr);
  62658. + return -DWC_E_INVALID;
  62659. + }
  62660. +
  62661. + ep->ep->dwc_ep.buff_mode = BM_ALIGN;
  62662. + dwc_memcpy(ep->bm_align, palignval, sizeof(ddma_align_buffer_setup_t));
  62663. +
  62664. + return 0;
  62665. +}
  62666. +
  62667. +/**
  62668. + * This function sets a new value for the Concatenation buffer setup.
  62669. + */
  62670. +static int cfi_ep_set_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  62671. +{
  62672. + uint8_t addr;
  62673. + cfi_ep_t *ep;
  62674. + struct _ddma_concat_buffer_setup_hdr *pConcatValHdr;
  62675. + uint16_t *pVals;
  62676. + uint32_t desccount;
  62677. + int i;
  62678. + uint16_t mps;
  62679. +
  62680. + pConcatValHdr = (struct _ddma_concat_buffer_setup_hdr *)buf;
  62681. + desccount = (uint32_t) pConcatValHdr->bDescCount;
  62682. + pVals = (uint16_t *) (buf + BS_CONCAT_VAL_HDR_LEN);
  62683. +
  62684. + /* Check the DMA descriptor count */
  62685. + if (desccount > MAX_DMA_DESCS_PER_EP) {
  62686. + CFI_INFO("%s: Maximum DMA Descriptor count should be %d\n",
  62687. + __func__, MAX_DMA_DESCS_PER_EP);
  62688. + return -DWC_E_INVALID;
  62689. + }
  62690. +
  62691. + addr = pConcatValHdr->bEndpointAddress;
  62692. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  62693. + if (NULL == ep) {
  62694. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  62695. + __func__, addr);
  62696. + return -DWC_E_INVALID;
  62697. + }
  62698. +
  62699. + mps = UGETW(ep->ep->desc->wMaxPacketSize);
  62700. +
  62701. +#if 0
  62702. + for (i = 0; i < desccount; i++) {
  62703. + CFI_INFO("%s: wTxSize[%d]=0x%04x\n", __func__, i, pVals[i]);
  62704. + }
  62705. + CFI_INFO("%s: epname=%s; mps=%d\n", __func__, ep->ep->ep.name, mps);
  62706. +#endif
  62707. +
  62708. + /* Check the wTxSizes to be less than or equal to the mps */
  62709. + for (i = 0; i < desccount; i++) {
  62710. + if (pVals[i] > mps) {
  62711. + CFI_INFO
  62712. + ("%s: ERROR - the wTxSize[%d] should be <= MPS (wTxSize=%d)\n",
  62713. + __func__, i, pVals[i]);
  62714. + return -DWC_E_INVALID;
  62715. + }
  62716. + }
  62717. +
  62718. + ep->ep->dwc_ep.buff_mode = BM_CONCAT;
  62719. + dwc_memcpy(ep->bm_concat, pConcatValHdr, BS_CONCAT_VAL_HDR_LEN);
  62720. +
  62721. + /* Free the previously allocated storage for the wTxBytes */
  62722. + if (ep->bm_concat->wTxBytes) {
  62723. + DWC_FREE(ep->bm_concat->wTxBytes);
  62724. + }
  62725. +
  62726. + /* Allocate a new storage for the wTxBytes field */
  62727. + ep->bm_concat->wTxBytes =
  62728. + DWC_ALLOC(sizeof(uint16_t) * pConcatValHdr->bDescCount);
  62729. + if (NULL == ep->bm_concat->wTxBytes) {
  62730. + CFI_INFO("%s: Unable to allocate memory\n", __func__);
  62731. + return -DWC_E_NO_MEMORY;
  62732. + }
  62733. +
  62734. + /* Copy the new values into the wTxBytes filed */
  62735. + dwc_memcpy(ep->bm_concat->wTxBytes, buf + BS_CONCAT_VAL_HDR_LEN,
  62736. + sizeof(uint16_t) * pConcatValHdr->bDescCount);
  62737. +
  62738. + return 0;
  62739. +}
  62740. +
  62741. +/**
  62742. + * This function calculates the total of all FIFO sizes
  62743. + *
  62744. + * @param core_if Programming view of DWC_otg controller
  62745. + *
  62746. + * @return The total of data FIFO sizes.
  62747. + *
  62748. + */
  62749. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if)
  62750. +{
  62751. + dwc_otg_core_params_t *params = core_if->core_params;
  62752. + uint16_t dfifo_total = 0;
  62753. + int i;
  62754. +
  62755. + /* The shared RxFIFO size */
  62756. + dfifo_total =
  62757. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  62758. +
  62759. + /* Add up each TxFIFO size to the total */
  62760. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  62761. + dfifo_total += params->dev_tx_fifo_size[i];
  62762. + }
  62763. +
  62764. + return dfifo_total;
  62765. +}
  62766. +
  62767. +/**
  62768. + * This function returns Rx FIFO size
  62769. + *
  62770. + * @param core_if Programming view of DWC_otg controller
  62771. + *
  62772. + * @return The total of data FIFO sizes.
  62773. + *
  62774. + */
  62775. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue)
  62776. +{
  62777. + switch (wValue >> 8) {
  62778. + case 0:
  62779. + return (core_if->pwron_rxfsiz <
  62780. + 32768) ? core_if->pwron_rxfsiz : 32768;
  62781. + break;
  62782. + case 1:
  62783. + return core_if->core_params->dev_rx_fifo_size;
  62784. + break;
  62785. + default:
  62786. + return -DWC_E_INVALID;
  62787. + break;
  62788. + }
  62789. +}
  62790. +
  62791. +/**
  62792. + * This function returns Tx FIFO size for IN EP
  62793. + *
  62794. + * @param core_if Programming view of DWC_otg controller
  62795. + *
  62796. + * @return The total of data FIFO sizes.
  62797. + *
  62798. + */
  62799. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue)
  62800. +{
  62801. + dwc_otg_pcd_ep_t *ep;
  62802. +
  62803. + ep = get_ep_by_addr(pcd, wValue & 0xff);
  62804. +
  62805. + if (NULL == ep) {
  62806. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  62807. + __func__, wValue & 0xff);
  62808. + return -DWC_E_INVALID;
  62809. + }
  62810. +
  62811. + if (!ep->dwc_ep.is_in) {
  62812. + CFI_INFO
  62813. + ("%s: No Tx FIFO assingned to the Out endpoint addr=0x%02x\n",
  62814. + __func__, wValue & 0xff);
  62815. + return -DWC_E_INVALID;
  62816. + }
  62817. +
  62818. + switch (wValue >> 8) {
  62819. + case 0:
  62820. + return (GET_CORE_IF(pcd)->pwron_txfsiz
  62821. + [ep->dwc_ep.tx_fifo_num - 1] <
  62822. + 768) ? GET_CORE_IF(pcd)->pwron_txfsiz[ep->
  62823. + dwc_ep.tx_fifo_num
  62824. + - 1] : 32768;
  62825. + break;
  62826. + case 1:
  62827. + return GET_CORE_IF(pcd)->core_params->
  62828. + dev_tx_fifo_size[ep->dwc_ep.num - 1];
  62829. + break;
  62830. + default:
  62831. + return -DWC_E_INVALID;
  62832. + break;
  62833. + }
  62834. +}
  62835. +
  62836. +/**
  62837. + * This function checks if the submitted combination of
  62838. + * device mode FIFO sizes is possible or not.
  62839. + *
  62840. + * @param core_if Programming view of DWC_otg controller
  62841. + *
  62842. + * @return 1 if possible, 0 otherwise.
  62843. + *
  62844. + */
  62845. +static uint8_t check_fifo_sizes(dwc_otg_core_if_t * core_if)
  62846. +{
  62847. + uint16_t dfifo_actual = 0;
  62848. + dwc_otg_core_params_t *params = core_if->core_params;
  62849. + uint16_t start_addr = 0;
  62850. + int i;
  62851. +
  62852. + dfifo_actual =
  62853. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  62854. +
  62855. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  62856. + dfifo_actual += params->dev_tx_fifo_size[i];
  62857. + }
  62858. +
  62859. + if (dfifo_actual > core_if->total_fifo_size) {
  62860. + return 0;
  62861. + }
  62862. +
  62863. + if (params->dev_rx_fifo_size > 32768 || params->dev_rx_fifo_size < 16)
  62864. + return 0;
  62865. +
  62866. + if (params->dev_nperio_tx_fifo_size > 32768
  62867. + || params->dev_nperio_tx_fifo_size < 16)
  62868. + return 0;
  62869. +
  62870. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  62871. +
  62872. + if (params->dev_tx_fifo_size[i] > 768
  62873. + || params->dev_tx_fifo_size[i] < 4)
  62874. + return 0;
  62875. + }
  62876. +
  62877. + if (params->dev_rx_fifo_size > core_if->pwron_rxfsiz)
  62878. + return 0;
  62879. + start_addr = params->dev_rx_fifo_size;
  62880. +
  62881. + if (params->dev_nperio_tx_fifo_size > core_if->pwron_gnptxfsiz)
  62882. + return 0;
  62883. + start_addr += params->dev_nperio_tx_fifo_size;
  62884. +
  62885. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  62886. +
  62887. + if (params->dev_tx_fifo_size[i] > core_if->pwron_txfsiz[i])
  62888. + return 0;
  62889. + start_addr += params->dev_tx_fifo_size[i];
  62890. + }
  62891. +
  62892. + return 1;
  62893. +}
  62894. +
  62895. +/**
  62896. + * This function resizes Device mode FIFOs
  62897. + *
  62898. + * @param core_if Programming view of DWC_otg controller
  62899. + *
  62900. + * @return 1 if successful, 0 otherwise
  62901. + *
  62902. + */
  62903. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if)
  62904. +{
  62905. + int i = 0;
  62906. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  62907. + dwc_otg_core_params_t *params = core_if->core_params;
  62908. + uint32_t rx_fifo_size;
  62909. + fifosize_data_t nptxfifosize;
  62910. + fifosize_data_t txfifosize[15];
  62911. +
  62912. + uint32_t rx_fsz_bak;
  62913. + uint32_t nptxfsz_bak;
  62914. + uint32_t txfsz_bak[15];
  62915. +
  62916. + uint16_t start_address;
  62917. + uint8_t retval = 1;
  62918. +
  62919. + if (!check_fifo_sizes(core_if)) {
  62920. + return 0;
  62921. + }
  62922. +
  62923. + /* Configure data FIFO sizes */
  62924. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  62925. + rx_fsz_bak = DWC_READ_REG32(&global_regs->grxfsiz);
  62926. + rx_fifo_size = params->dev_rx_fifo_size;
  62927. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  62928. +
  62929. + /*
  62930. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  62931. + * Indexes of the FIFO size module parameters in the
  62932. + * dev_tx_fifo_size array and the FIFO size registers in
  62933. + * the dtxfsiz array run from 0 to 14.
  62934. + */
  62935. +
  62936. + /* Non-periodic Tx FIFO */
  62937. + nptxfsz_bak = DWC_READ_REG32(&global_regs->gnptxfsiz);
  62938. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  62939. + start_address = params->dev_rx_fifo_size;
  62940. + nptxfifosize.b.startaddr = start_address;
  62941. +
  62942. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  62943. +
  62944. + start_address += nptxfifosize.b.depth;
  62945. +
  62946. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  62947. + txfsz_bak[i] = DWC_READ_REG32(&global_regs->dtxfsiz[i]);
  62948. +
  62949. + txfifosize[i].b.depth = params->dev_tx_fifo_size[i];
  62950. + txfifosize[i].b.startaddr = start_address;
  62951. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  62952. + txfifosize[i].d32);
  62953. +
  62954. + start_address += txfifosize[i].b.depth;
  62955. + }
  62956. +
  62957. + /** Check if register values are set correctly */
  62958. + if (rx_fifo_size != DWC_READ_REG32(&global_regs->grxfsiz)) {
  62959. + retval = 0;
  62960. + }
  62961. +
  62962. + if (nptxfifosize.d32 != DWC_READ_REG32(&global_regs->gnptxfsiz)) {
  62963. + retval = 0;
  62964. + }
  62965. +
  62966. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  62967. + if (txfifosize[i].d32 !=
  62968. + DWC_READ_REG32(&global_regs->dtxfsiz[i])) {
  62969. + retval = 0;
  62970. + }
  62971. + }
  62972. +
  62973. + /** If register values are not set correctly, reset old values */
  62974. + if (retval == 0) {
  62975. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fsz_bak);
  62976. +
  62977. + /* Non-periodic Tx FIFO */
  62978. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfsz_bak);
  62979. +
  62980. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  62981. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  62982. + txfsz_bak[i]);
  62983. + }
  62984. + }
  62985. + } else {
  62986. + return 0;
  62987. + }
  62988. +
  62989. + /* Flush the FIFOs */
  62990. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  62991. + dwc_otg_flush_rx_fifo(core_if);
  62992. +
  62993. + return retval;
  62994. +}
  62995. +
  62996. +/**
  62997. + * This function sets a new value for the buffer Alignment setup.
  62998. + */
  62999. +static int cfi_ep_set_tx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  63000. +{
  63001. + int retval;
  63002. + uint32_t fsiz;
  63003. + uint16_t size;
  63004. + uint16_t ep_addr;
  63005. + dwc_otg_pcd_ep_t *ep;
  63006. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  63007. + tx_fifo_size_setup_t *ptxfifoval;
  63008. +
  63009. + ptxfifoval = (tx_fifo_size_setup_t *) buf;
  63010. + ep_addr = ptxfifoval->bEndpointAddress;
  63011. + size = ptxfifoval->wDepth;
  63012. +
  63013. + ep = get_ep_by_addr(pcd, ep_addr);
  63014. +
  63015. + CFI_INFO
  63016. + ("%s: Set Tx FIFO size: endpoint addr=0x%02x, depth=%d, FIFO Num=%d\n",
  63017. + __func__, ep_addr, size, ep->dwc_ep.tx_fifo_num);
  63018. +
  63019. + if (NULL == ep) {
  63020. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  63021. + __func__, ep_addr);
  63022. + return -DWC_E_INVALID;
  63023. + }
  63024. +
  63025. + fsiz = params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1];
  63026. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = size;
  63027. +
  63028. + if (resize_fifos(GET_CORE_IF(pcd))) {
  63029. + retval = 0;
  63030. + } else {
  63031. + CFI_INFO
  63032. + ("%s: Error setting the feature Tx FIFO Size for EP%d\n",
  63033. + __func__, ep_addr);
  63034. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = fsiz;
  63035. + retval = -DWC_E_INVALID;
  63036. + }
  63037. +
  63038. + return retval;
  63039. +}
  63040. +
  63041. +/**
  63042. + * This function sets a new value for the buffer Alignment setup.
  63043. + */
  63044. +static int cfi_set_rx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  63045. +{
  63046. + int retval;
  63047. + uint32_t fsiz;
  63048. + uint16_t size;
  63049. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  63050. + rx_fifo_size_setup_t *prxfifoval;
  63051. +
  63052. + prxfifoval = (rx_fifo_size_setup_t *) buf;
  63053. + size = prxfifoval->wDepth;
  63054. +
  63055. + fsiz = params->dev_rx_fifo_size;
  63056. + params->dev_rx_fifo_size = size;
  63057. +
  63058. + if (resize_fifos(GET_CORE_IF(pcd))) {
  63059. + retval = 0;
  63060. + } else {
  63061. + CFI_INFO("%s: Error setting the feature Rx FIFO Size\n",
  63062. + __func__);
  63063. + params->dev_rx_fifo_size = fsiz;
  63064. + retval = -DWC_E_INVALID;
  63065. + }
  63066. +
  63067. + return retval;
  63068. +}
  63069. +
  63070. +/**
  63071. + * This function reads the SG of an EP's buffer setup into the buffer buf
  63072. + */
  63073. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  63074. + struct cfi_usb_ctrlrequest *req)
  63075. +{
  63076. + int retval = -DWC_E_INVALID;
  63077. + uint8_t addr;
  63078. + cfi_ep_t *ep;
  63079. +
  63080. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  63081. + addr = req->wValue & 0xFF;
  63082. + if (addr == 0) /* The address should be non-zero */
  63083. + return retval;
  63084. +
  63085. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  63086. + if (NULL == ep) {
  63087. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  63088. + __func__, addr);
  63089. + return retval;
  63090. + }
  63091. +
  63092. + dwc_memcpy(buf, ep->bm_sg, BS_SG_VAL_DESC_LEN);
  63093. + retval = BS_SG_VAL_DESC_LEN;
  63094. + return retval;
  63095. +}
  63096. +
  63097. +/**
  63098. + * This function reads the Concatenation value of an EP's buffer mode into
  63099. + * the buffer buf
  63100. + */
  63101. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  63102. + struct cfi_usb_ctrlrequest *req)
  63103. +{
  63104. + int retval = -DWC_E_INVALID;
  63105. + uint8_t addr;
  63106. + cfi_ep_t *ep;
  63107. + uint8_t desc_count;
  63108. +
  63109. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  63110. + addr = req->wValue & 0xFF;
  63111. + if (addr == 0) /* The address should be non-zero */
  63112. + return retval;
  63113. +
  63114. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  63115. + if (NULL == ep) {
  63116. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  63117. + __func__, addr);
  63118. + return retval;
  63119. + }
  63120. +
  63121. + /* Copy the header to the buffer */
  63122. + dwc_memcpy(buf, ep->bm_concat, BS_CONCAT_VAL_HDR_LEN);
  63123. + /* Advance the buffer pointer by the header size */
  63124. + buf += BS_CONCAT_VAL_HDR_LEN;
  63125. +
  63126. + desc_count = ep->bm_concat->hdr.bDescCount;
  63127. + /* Copy alll the wTxBytes to the buffer */
  63128. + dwc_memcpy(buf, ep->bm_concat->wTxBytes, sizeof(uid16_t) * desc_count);
  63129. +
  63130. + retval = BS_CONCAT_VAL_HDR_LEN + sizeof(uid16_t) * desc_count;
  63131. + return retval;
  63132. +}
  63133. +
  63134. +/**
  63135. + * This function reads the buffer Alignment value of an EP's buffer mode into
  63136. + * the buffer buf
  63137. + *
  63138. + * @return The total number of bytes copied to the buffer or negative error code.
  63139. + */
  63140. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  63141. + struct cfi_usb_ctrlrequest *req)
  63142. +{
  63143. + int retval = -DWC_E_INVALID;
  63144. + uint8_t addr;
  63145. + cfi_ep_t *ep;
  63146. +
  63147. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  63148. + addr = req->wValue & 0xFF;
  63149. + if (addr == 0) /* The address should be non-zero */
  63150. + return retval;
  63151. +
  63152. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  63153. + if (NULL == ep) {
  63154. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  63155. + __func__, addr);
  63156. + return retval;
  63157. + }
  63158. +
  63159. + dwc_memcpy(buf, ep->bm_align, BS_ALIGN_VAL_HDR_LEN);
  63160. + retval = BS_ALIGN_VAL_HDR_LEN;
  63161. +
  63162. + return retval;
  63163. +}
  63164. +
  63165. +/**
  63166. + * This function sets a new value for the specified feature
  63167. + *
  63168. + * @param pcd A pointer to the PCD object
  63169. + *
  63170. + * @return 0 if successful, negative error code otherwise to stall the DCE.
  63171. + */
  63172. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd)
  63173. +{
  63174. + int retval = -DWC_E_NOT_SUPPORTED;
  63175. + uint16_t wIndex, wValue;
  63176. + uint8_t bRequest;
  63177. + struct dwc_otg_core_if *coreif;
  63178. + cfiobject_t *cfi = pcd->cfi;
  63179. + struct cfi_usb_ctrlrequest *ctrl_req;
  63180. + uint8_t *buf;
  63181. + ctrl_req = &cfi->ctrl_req;
  63182. +
  63183. + buf = pcd->cfi->ctrl_req.data;
  63184. +
  63185. + coreif = GET_CORE_IF(pcd);
  63186. + bRequest = ctrl_req->bRequest;
  63187. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  63188. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  63189. +
  63190. + /* See which feature is to be modified */
  63191. + switch (wIndex) {
  63192. + case FT_ID_DMA_BUFFER_SETUP:
  63193. + /* Modify the feature */
  63194. + if ((retval = cfi_ep_set_sg_val(buf, pcd)) < 0)
  63195. + return retval;
  63196. +
  63197. + /* And send this request to the gadget */
  63198. + cfi->need_gadget_att = 1;
  63199. + break;
  63200. +
  63201. + case FT_ID_DMA_BUFF_ALIGN:
  63202. + if ((retval = cfi_ep_set_alignment_val(buf, pcd)) < 0)
  63203. + return retval;
  63204. + cfi->need_gadget_att = 1;
  63205. + break;
  63206. +
  63207. + case FT_ID_DMA_CONCAT_SETUP:
  63208. + /* Modify the feature */
  63209. + if ((retval = cfi_ep_set_concat_val(buf, pcd)) < 0)
  63210. + return retval;
  63211. + cfi->need_gadget_att = 1;
  63212. + break;
  63213. +
  63214. + case FT_ID_DMA_CIRCULAR:
  63215. + CFI_INFO("FT_ID_DMA_CIRCULAR\n");
  63216. + break;
  63217. +
  63218. + case FT_ID_THRESHOLD_SETUP:
  63219. + CFI_INFO("FT_ID_THRESHOLD_SETUP\n");
  63220. + break;
  63221. +
  63222. + case FT_ID_DFIFO_DEPTH:
  63223. + CFI_INFO("FT_ID_DFIFO_DEPTH\n");
  63224. + break;
  63225. +
  63226. + case FT_ID_TX_FIFO_DEPTH:
  63227. + CFI_INFO("FT_ID_TX_FIFO_DEPTH\n");
  63228. + if ((retval = cfi_ep_set_tx_fifo_val(buf, pcd)) < 0)
  63229. + return retval;
  63230. + cfi->need_gadget_att = 0;
  63231. + break;
  63232. +
  63233. + case FT_ID_RX_FIFO_DEPTH:
  63234. + CFI_INFO("FT_ID_RX_FIFO_DEPTH\n");
  63235. + if ((retval = cfi_set_rx_fifo_val(buf, pcd)) < 0)
  63236. + return retval;
  63237. + cfi->need_gadget_att = 0;
  63238. + break;
  63239. + }
  63240. +
  63241. + return retval;
  63242. +}
  63243. +
  63244. +#endif //DWC_UTE_CFI
  63245. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_cfi.h linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_cfi.h
  63246. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_cfi.h 1969-12-31 18:00:00.000000000 -0600
  63247. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_cfi.h 2014-12-03 19:13:40.216418001 -0600
  63248. @@ -0,0 +1,320 @@
  63249. +/* ==========================================================================
  63250. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  63251. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  63252. + * otherwise expressly agreed to in writing between Synopsys and you.
  63253. + *
  63254. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  63255. + * any End User Software License Agreement or Agreement for Licensed Product
  63256. + * with Synopsys or any supplement thereto. You are permitted to use and
  63257. + * redistribute this Software in source and binary forms, with or without
  63258. + * modification, provided that redistributions of source code must retain this
  63259. + * notice. You may not view, use, disclose, copy or distribute this file or
  63260. + * any information contained herein except pursuant to this license grant from
  63261. + * Synopsys. If you do not agree with this notice, including the disclaimer
  63262. + * below, then you are not authorized to use the Software.
  63263. + *
  63264. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  63265. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  63266. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  63267. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  63268. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  63269. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  63270. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  63271. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  63272. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  63273. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  63274. + * DAMAGE.
  63275. + * ========================================================================== */
  63276. +
  63277. +#if !defined(__DWC_OTG_CFI_H__)
  63278. +#define __DWC_OTG_CFI_H__
  63279. +
  63280. +#include "dwc_otg_pcd.h"
  63281. +#include "dwc_cfi_common.h"
  63282. +
  63283. +/**
  63284. + * @file
  63285. + * This file contains the CFI related OTG PCD specific common constants,
  63286. + * interfaces(functions and macros) and data structures.The CFI Protocol is an
  63287. + * optional interface for internal testing purposes that a DUT may implement to
  63288. + * support testing of configurable features.
  63289. + *
  63290. + */
  63291. +
  63292. +struct dwc_otg_pcd;
  63293. +struct dwc_otg_pcd_ep;
  63294. +
  63295. +/** OTG CFI Features (properties) ID constants */
  63296. +/** This is a request for all Core Features */
  63297. +#define FT_ID_DMA_MODE 0x0001
  63298. +#define FT_ID_DMA_BUFFER_SETUP 0x0002
  63299. +#define FT_ID_DMA_BUFF_ALIGN 0x0003
  63300. +#define FT_ID_DMA_CONCAT_SETUP 0x0004
  63301. +#define FT_ID_DMA_CIRCULAR 0x0005
  63302. +#define FT_ID_THRESHOLD_SETUP 0x0006
  63303. +#define FT_ID_DFIFO_DEPTH 0x0007
  63304. +#define FT_ID_TX_FIFO_DEPTH 0x0008
  63305. +#define FT_ID_RX_FIFO_DEPTH 0x0009
  63306. +
  63307. +/**********************************************************/
  63308. +#define CFI_INFO_DEF
  63309. +
  63310. +#ifdef CFI_INFO_DEF
  63311. +#define CFI_INFO(fmt...) DWC_PRINTF("CFI: " fmt);
  63312. +#else
  63313. +#define CFI_INFO(fmt...)
  63314. +#endif
  63315. +
  63316. +#define min(x,y) ({ \
  63317. + x < y ? x : y; })
  63318. +
  63319. +#define max(x,y) ({ \
  63320. + x > y ? x : y; })
  63321. +
  63322. +/**
  63323. + * Descriptor DMA SG Buffer setup structure (SG buffer). This structure is
  63324. + * also used for setting up a buffer for Circular DDMA.
  63325. + */
  63326. +struct _ddma_sg_buffer_setup {
  63327. +#define BS_SG_VAL_DESC_LEN 6
  63328. + /* The OUT EP address */
  63329. + uint8_t bOutEndpointAddress;
  63330. + /* The IN EP address */
  63331. + uint8_t bInEndpointAddress;
  63332. + /* Number of bytes to put between transfer segments (must be DWORD boundaries) */
  63333. + uint8_t bOffset;
  63334. + /* The number of transfer segments (a DMA descriptors per each segment) */
  63335. + uint8_t bCount;
  63336. + /* Size (in byte) of each transfer segment */
  63337. + uint16_t wSize;
  63338. +} __attribute__ ((packed));
  63339. +typedef struct _ddma_sg_buffer_setup ddma_sg_buffer_setup_t;
  63340. +
  63341. +/** Descriptor DMA Concatenation Buffer setup structure */
  63342. +struct _ddma_concat_buffer_setup_hdr {
  63343. +#define BS_CONCAT_VAL_HDR_LEN 4
  63344. + /* The endpoint for which the buffer is to be set up */
  63345. + uint8_t bEndpointAddress;
  63346. + /* The count of descriptors to be used */
  63347. + uint8_t bDescCount;
  63348. + /* The total size of the transfer */
  63349. + uint16_t wSize;
  63350. +} __attribute__ ((packed));
  63351. +typedef struct _ddma_concat_buffer_setup_hdr ddma_concat_buffer_setup_hdr_t;
  63352. +
  63353. +/** Descriptor DMA Concatenation Buffer setup structure */
  63354. +struct _ddma_concat_buffer_setup {
  63355. + /* The SG header */
  63356. + ddma_concat_buffer_setup_hdr_t hdr;
  63357. +
  63358. + /* The XFER sizes pointer (allocated dynamically) */
  63359. + uint16_t *wTxBytes;
  63360. +} __attribute__ ((packed));
  63361. +typedef struct _ddma_concat_buffer_setup ddma_concat_buffer_setup_t;
  63362. +
  63363. +/** Descriptor DMA Alignment Buffer setup structure */
  63364. +struct _ddma_align_buffer_setup {
  63365. +#define BS_ALIGN_VAL_HDR_LEN 2
  63366. + uint8_t bEndpointAddress;
  63367. + uint8_t bAlign;
  63368. +} __attribute__ ((packed));
  63369. +typedef struct _ddma_align_buffer_setup ddma_align_buffer_setup_t;
  63370. +
  63371. +/** Transmit FIFO Size setup structure */
  63372. +struct _tx_fifo_size_setup {
  63373. + uint8_t bEndpointAddress;
  63374. + uint16_t wDepth;
  63375. +} __attribute__ ((packed));
  63376. +typedef struct _tx_fifo_size_setup tx_fifo_size_setup_t;
  63377. +
  63378. +/** Transmit FIFO Size setup structure */
  63379. +struct _rx_fifo_size_setup {
  63380. + uint16_t wDepth;
  63381. +} __attribute__ ((packed));
  63382. +typedef struct _rx_fifo_size_setup rx_fifo_size_setup_t;
  63383. +
  63384. +/**
  63385. + * struct cfi_usb_ctrlrequest - the CFI implementation of the struct usb_ctrlrequest
  63386. + * This structure encapsulates the standard usb_ctrlrequest and adds a pointer
  63387. + * to the data returned in the data stage of a 3-stage Control Write requests.
  63388. + */
  63389. +struct cfi_usb_ctrlrequest {
  63390. + uint8_t bRequestType;
  63391. + uint8_t bRequest;
  63392. + uint16_t wValue;
  63393. + uint16_t wIndex;
  63394. + uint16_t wLength;
  63395. + uint8_t *data;
  63396. +} UPACKED;
  63397. +
  63398. +/*---------------------------------------------------------------------------*/
  63399. +
  63400. +/**
  63401. + * The CFI wrapper of the enabled and activated dwc_otg_pcd_ep structures.
  63402. + * This structure is used to store the buffer setup data for any
  63403. + * enabled endpoint in the PCD.
  63404. + */
  63405. +struct cfi_ep {
  63406. + /* Entry for the list container */
  63407. + dwc_list_link_t lh;
  63408. + /* Pointer to the active PCD endpoint structure */
  63409. + struct dwc_otg_pcd_ep *ep;
  63410. + /* The last descriptor in the chain of DMA descriptors of the endpoint */
  63411. + struct dwc_otg_dma_desc *dma_desc_last;
  63412. + /* The SG feature value */
  63413. + ddma_sg_buffer_setup_t *bm_sg;
  63414. + /* The Circular feature value */
  63415. + ddma_sg_buffer_setup_t *bm_circ;
  63416. + /* The Concatenation feature value */
  63417. + ddma_concat_buffer_setup_t *bm_concat;
  63418. + /* The Alignment feature value */
  63419. + ddma_align_buffer_setup_t *bm_align;
  63420. + /* XFER length */
  63421. + uint32_t xfer_len;
  63422. + /*
  63423. + * Count of DMA descriptors currently used.
  63424. + * The total should not exceed the MAX_DMA_DESCS_PER_EP value
  63425. + * defined in the dwc_otg_cil.h
  63426. + */
  63427. + uint32_t desc_count;
  63428. +};
  63429. +typedef struct cfi_ep cfi_ep_t;
  63430. +
  63431. +typedef struct cfi_dma_buff {
  63432. +#define CFI_IN_BUF_LEN 1024
  63433. +#define CFI_OUT_BUF_LEN 1024
  63434. + dma_addr_t addr;
  63435. + uint8_t *buf;
  63436. +} cfi_dma_buff_t;
  63437. +
  63438. +struct cfiobject;
  63439. +
  63440. +/**
  63441. + * This is the interface for the CFI operations.
  63442. + *
  63443. + * @param ep_enable Called when any endpoint is enabled and activated.
  63444. + * @param release Called when the CFI object is released and it needs to correctly
  63445. + * deallocate the dynamic memory
  63446. + * @param ctrl_write_complete Called when the data stage of the request is complete
  63447. + */
  63448. +typedef struct cfi_ops {
  63449. + int (*ep_enable) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  63450. + struct dwc_otg_pcd_ep * ep);
  63451. + void *(*ep_alloc_buf) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  63452. + struct dwc_otg_pcd_ep * ep, dma_addr_t * dma,
  63453. + unsigned size, gfp_t flags);
  63454. + void (*release) (struct cfiobject * cfi);
  63455. + int (*ctrl_write_complete) (struct cfiobject * cfi,
  63456. + struct dwc_otg_pcd * pcd);
  63457. + void (*build_descriptors) (struct cfiobject * cfi,
  63458. + struct dwc_otg_pcd * pcd,
  63459. + struct dwc_otg_pcd_ep * ep,
  63460. + dwc_otg_pcd_request_t * req);
  63461. +} cfi_ops_t;
  63462. +
  63463. +struct cfiobject {
  63464. + cfi_ops_t ops;
  63465. + struct dwc_otg_pcd *pcd;
  63466. + struct usb_gadget *gadget;
  63467. +
  63468. + /* Buffers used to send/receive CFI-related request data */
  63469. + cfi_dma_buff_t buf_in;
  63470. + cfi_dma_buff_t buf_out;
  63471. +
  63472. + /* CFI specific Control request wrapper */
  63473. + struct cfi_usb_ctrlrequest ctrl_req;
  63474. +
  63475. + /* The list of active EP's in the PCD of type cfi_ep_t */
  63476. + dwc_list_link_t active_eps;
  63477. +
  63478. + /* This flag shall control the propagation of a specific request
  63479. + * to the gadget's processing routines.
  63480. + * 0 - no gadget handling
  63481. + * 1 - the gadget needs to know about this request (w/o completing a status
  63482. + * phase - just return a 0 to the _setup callback)
  63483. + */
  63484. + uint8_t need_gadget_att;
  63485. +
  63486. + /* Flag indicating whether the status IN phase needs to be
  63487. + * completed by the PCD
  63488. + */
  63489. + uint8_t need_status_in_complete;
  63490. +};
  63491. +typedef struct cfiobject cfiobject_t;
  63492. +
  63493. +#define DUMP_MSG
  63494. +
  63495. +#if defined(DUMP_MSG)
  63496. +static inline void dump_msg(const u8 * buf, unsigned int length)
  63497. +{
  63498. + unsigned int start, num, i;
  63499. + char line[52], *p;
  63500. +
  63501. + if (length >= 512)
  63502. + return;
  63503. +
  63504. + start = 0;
  63505. + while (length > 0) {
  63506. + num = min(length, 16u);
  63507. + p = line;
  63508. + for (i = 0; i < num; ++i) {
  63509. + if (i == 8)
  63510. + *p++ = ' ';
  63511. + DWC_SPRINTF(p, " %02x", buf[i]);
  63512. + p += 3;
  63513. + }
  63514. + *p = 0;
  63515. + DWC_DEBUG("%6x: %s\n", start, line);
  63516. + buf += num;
  63517. + start += num;
  63518. + length -= num;
  63519. + }
  63520. +}
  63521. +#else
  63522. +static inline void dump_msg(const u8 * buf, unsigned int length)
  63523. +{
  63524. +}
  63525. +#endif
  63526. +
  63527. +/**
  63528. + * This function returns a pointer to cfi_ep_t object with the addr address.
  63529. + */
  63530. +static inline struct cfi_ep *get_cfi_ep_by_addr(struct cfiobject *cfi,
  63531. + uint8_t addr)
  63532. +{
  63533. + struct cfi_ep *pcfiep;
  63534. + dwc_list_link_t *tmp;
  63535. +
  63536. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  63537. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  63538. +
  63539. + if (pcfiep->ep->desc->bEndpointAddress == addr) {
  63540. + return pcfiep;
  63541. + }
  63542. + }
  63543. +
  63544. + return NULL;
  63545. +}
  63546. +
  63547. +/**
  63548. + * This function returns a pointer to cfi_ep_t object that matches
  63549. + * the dwc_otg_pcd_ep object.
  63550. + */
  63551. +static inline struct cfi_ep *get_cfi_ep_by_pcd_ep(struct cfiobject *cfi,
  63552. + struct dwc_otg_pcd_ep *ep)
  63553. +{
  63554. + struct cfi_ep *pcfiep = NULL;
  63555. + dwc_list_link_t *tmp;
  63556. +
  63557. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  63558. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  63559. + if (pcfiep->ep == ep) {
  63560. + return pcfiep;
  63561. + }
  63562. + }
  63563. + return NULL;
  63564. +}
  63565. +
  63566. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl);
  63567. +
  63568. +#endif /* (__DWC_OTG_CFI_H__) */
  63569. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_cil.c linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil.c
  63570. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_cil.c 1969-12-31 18:00:00.000000000 -0600
  63571. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil.c 2014-12-03 19:13:40.216418001 -0600
  63572. @@ -0,0 +1,7151 @@
  63573. +/* ==========================================================================
  63574. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.c $
  63575. + * $Revision: #191 $
  63576. + * $Date: 2012/08/10 $
  63577. + * $Change: 2047372 $
  63578. + *
  63579. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  63580. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  63581. + * otherwise expressly agreed to in writing between Synopsys and you.
  63582. + *
  63583. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  63584. + * any End User Software License Agreement or Agreement for Licensed Product
  63585. + * with Synopsys or any supplement thereto. You are permitted to use and
  63586. + * redistribute this Software in source and binary forms, with or without
  63587. + * modification, provided that redistributions of source code must retain this
  63588. + * notice. You may not view, use, disclose, copy or distribute this file or
  63589. + * any information contained herein except pursuant to this license grant from
  63590. + * Synopsys. If you do not agree with this notice, including the disclaimer
  63591. + * below, then you are not authorized to use the Software.
  63592. + *
  63593. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  63594. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  63595. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  63596. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  63597. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  63598. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  63599. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  63600. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  63601. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  63602. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  63603. + * DAMAGE.
  63604. + * ========================================================================== */
  63605. +
  63606. +/** @file
  63607. + *
  63608. + * The Core Interface Layer provides basic services for accessing and
  63609. + * managing the DWC_otg hardware. These services are used by both the
  63610. + * Host Controller Driver and the Peripheral Controller Driver.
  63611. + *
  63612. + * The CIL manages the memory map for the core so that the HCD and PCD
  63613. + * don't have to do this separately. It also handles basic tasks like
  63614. + * reading/writing the registers and data FIFOs in the controller.
  63615. + * Some of the data access functions provide encapsulation of several
  63616. + * operations required to perform a task, such as writing multiple
  63617. + * registers to start a transfer. Finally, the CIL performs basic
  63618. + * services that are not specific to either the host or device modes
  63619. + * of operation. These services include management of the OTG Host
  63620. + * Negotiation Protocol (HNP) and Session Request Protocol (SRP). A
  63621. + * Diagnostic API is also provided to allow testing of the controller
  63622. + * hardware.
  63623. + *
  63624. + * The Core Interface Layer has the following requirements:
  63625. + * - Provides basic controller operations.
  63626. + * - Minimal use of OS services.
  63627. + * - The OS services used will be abstracted by using inline functions
  63628. + * or macros.
  63629. + *
  63630. + */
  63631. +
  63632. +#include "dwc_os.h"
  63633. +#include "dwc_otg_regs.h"
  63634. +#include "dwc_otg_cil.h"
  63635. +
  63636. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if);
  63637. +
  63638. +/**
  63639. + * This function is called to initialize the DWC_otg CSR data
  63640. + * structures. The register addresses in the device and host
  63641. + * structures are initialized from the base address supplied by the
  63642. + * caller. The calling function must make the OS calls to get the
  63643. + * base address of the DWC_otg controller registers. The core_params
  63644. + * argument holds the parameters that specify how the core should be
  63645. + * configured.
  63646. + *
  63647. + * @param reg_base_addr Base address of DWC_otg core registers
  63648. + *
  63649. + */
  63650. +dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * reg_base_addr)
  63651. +{
  63652. + dwc_otg_core_if_t *core_if = 0;
  63653. + dwc_otg_dev_if_t *dev_if = 0;
  63654. + dwc_otg_host_if_t *host_if = 0;
  63655. + uint8_t *reg_base = (uint8_t *) reg_base_addr;
  63656. + int i = 0;
  63657. +
  63658. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, reg_base_addr);
  63659. +
  63660. + core_if = DWC_ALLOC(sizeof(dwc_otg_core_if_t));
  63661. +
  63662. + if (core_if == NULL) {
  63663. + DWC_DEBUGPL(DBG_CIL,
  63664. + "Allocation of dwc_otg_core_if_t failed\n");
  63665. + return 0;
  63666. + }
  63667. + core_if->core_global_regs = (dwc_otg_core_global_regs_t *) reg_base;
  63668. +
  63669. + /*
  63670. + * Allocate the Device Mode structures.
  63671. + */
  63672. + dev_if = DWC_ALLOC(sizeof(dwc_otg_dev_if_t));
  63673. +
  63674. + if (dev_if == NULL) {
  63675. + DWC_DEBUGPL(DBG_CIL, "Allocation of dwc_otg_dev_if_t failed\n");
  63676. + DWC_FREE(core_if);
  63677. + return 0;
  63678. + }
  63679. +
  63680. + dev_if->dev_global_regs =
  63681. + (dwc_otg_device_global_regs_t *) (reg_base +
  63682. + DWC_DEV_GLOBAL_REG_OFFSET);
  63683. +
  63684. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  63685. + dev_if->in_ep_regs[i] = (dwc_otg_dev_in_ep_regs_t *)
  63686. + (reg_base + DWC_DEV_IN_EP_REG_OFFSET +
  63687. + (i * DWC_EP_REG_OFFSET));
  63688. +
  63689. + dev_if->out_ep_regs[i] = (dwc_otg_dev_out_ep_regs_t *)
  63690. + (reg_base + DWC_DEV_OUT_EP_REG_OFFSET +
  63691. + (i * DWC_EP_REG_OFFSET));
  63692. + DWC_DEBUGPL(DBG_CILV, "in_ep_regs[%d]->diepctl=%p\n",
  63693. + i, &dev_if->in_ep_regs[i]->diepctl);
  63694. + DWC_DEBUGPL(DBG_CILV, "out_ep_regs[%d]->doepctl=%p\n",
  63695. + i, &dev_if->out_ep_regs[i]->doepctl);
  63696. + }
  63697. +
  63698. + dev_if->speed = 0; // unknown
  63699. +
  63700. + core_if->dev_if = dev_if;
  63701. +
  63702. + /*
  63703. + * Allocate the Host Mode structures.
  63704. + */
  63705. + host_if = DWC_ALLOC(sizeof(dwc_otg_host_if_t));
  63706. +
  63707. + if (host_if == NULL) {
  63708. + DWC_DEBUGPL(DBG_CIL,
  63709. + "Allocation of dwc_otg_host_if_t failed\n");
  63710. + DWC_FREE(dev_if);
  63711. + DWC_FREE(core_if);
  63712. + return 0;
  63713. + }
  63714. +
  63715. + host_if->host_global_regs = (dwc_otg_host_global_regs_t *)
  63716. + (reg_base + DWC_OTG_HOST_GLOBAL_REG_OFFSET);
  63717. +
  63718. + host_if->hprt0 =
  63719. + (uint32_t *) (reg_base + DWC_OTG_HOST_PORT_REGS_OFFSET);
  63720. +
  63721. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  63722. + host_if->hc_regs[i] = (dwc_otg_hc_regs_t *)
  63723. + (reg_base + DWC_OTG_HOST_CHAN_REGS_OFFSET +
  63724. + (i * DWC_OTG_CHAN_REGS_OFFSET));
  63725. + DWC_DEBUGPL(DBG_CILV, "hc_reg[%d]->hcchar=%p\n",
  63726. + i, &host_if->hc_regs[i]->hcchar);
  63727. + }
  63728. +
  63729. + host_if->num_host_channels = MAX_EPS_CHANNELS;
  63730. + core_if->host_if = host_if;
  63731. +
  63732. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  63733. + core_if->data_fifo[i] =
  63734. + (uint32_t *) (reg_base + DWC_OTG_DATA_FIFO_OFFSET +
  63735. + (i * DWC_OTG_DATA_FIFO_SIZE));
  63736. + DWC_DEBUGPL(DBG_CILV, "data_fifo[%d]=0x%08lx\n",
  63737. + i, (unsigned long)core_if->data_fifo[i]);
  63738. + }
  63739. +
  63740. + core_if->pcgcctl = (uint32_t *) (reg_base + DWC_OTG_PCGCCTL_OFFSET);
  63741. +
  63742. + /* Initiate lx_state to L3 disconnected state */
  63743. + core_if->lx_state = DWC_OTG_L3;
  63744. + /*
  63745. + * Store the contents of the hardware configuration registers here for
  63746. + * easy access later.
  63747. + */
  63748. + core_if->hwcfg1.d32 =
  63749. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg1);
  63750. + core_if->hwcfg2.d32 =
  63751. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  63752. + core_if->hwcfg3.d32 =
  63753. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg3);
  63754. + core_if->hwcfg4.d32 =
  63755. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  63756. +
  63757. + /* Force host mode to get HPTXFSIZ exact power on value */
  63758. + {
  63759. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  63760. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  63761. + gusbcfg.b.force_host_mode = 1;
  63762. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  63763. + dwc_mdelay(100);
  63764. + core_if->hptxfsiz.d32 =
  63765. + DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  63766. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  63767. + gusbcfg.b.force_host_mode = 0;
  63768. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  63769. + dwc_mdelay(100);
  63770. + }
  63771. +
  63772. + DWC_DEBUGPL(DBG_CILV, "hwcfg1=%08x\n", core_if->hwcfg1.d32);
  63773. + DWC_DEBUGPL(DBG_CILV, "hwcfg2=%08x\n", core_if->hwcfg2.d32);
  63774. + DWC_DEBUGPL(DBG_CILV, "hwcfg3=%08x\n", core_if->hwcfg3.d32);
  63775. + DWC_DEBUGPL(DBG_CILV, "hwcfg4=%08x\n", core_if->hwcfg4.d32);
  63776. +
  63777. + core_if->hcfg.d32 =
  63778. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  63779. + core_if->dcfg.d32 =
  63780. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  63781. +
  63782. + DWC_DEBUGPL(DBG_CILV, "hcfg=%08x\n", core_if->hcfg.d32);
  63783. + DWC_DEBUGPL(DBG_CILV, "dcfg=%08x\n", core_if->dcfg.d32);
  63784. +
  63785. + DWC_DEBUGPL(DBG_CILV, "op_mode=%0x\n", core_if->hwcfg2.b.op_mode);
  63786. + DWC_DEBUGPL(DBG_CILV, "arch=%0x\n", core_if->hwcfg2.b.architecture);
  63787. + DWC_DEBUGPL(DBG_CILV, "num_dev_ep=%d\n", core_if->hwcfg2.b.num_dev_ep);
  63788. + DWC_DEBUGPL(DBG_CILV, "num_host_chan=%d\n",
  63789. + core_if->hwcfg2.b.num_host_chan);
  63790. + DWC_DEBUGPL(DBG_CILV, "nonperio_tx_q_depth=0x%0x\n",
  63791. + core_if->hwcfg2.b.nonperio_tx_q_depth);
  63792. + DWC_DEBUGPL(DBG_CILV, "host_perio_tx_q_depth=0x%0x\n",
  63793. + core_if->hwcfg2.b.host_perio_tx_q_depth);
  63794. + DWC_DEBUGPL(DBG_CILV, "dev_token_q_depth=0x%0x\n",
  63795. + core_if->hwcfg2.b.dev_token_q_depth);
  63796. +
  63797. + DWC_DEBUGPL(DBG_CILV, "Total FIFO SZ=%d\n",
  63798. + core_if->hwcfg3.b.dfifo_depth);
  63799. + DWC_DEBUGPL(DBG_CILV, "xfer_size_cntr_width=%0x\n",
  63800. + core_if->hwcfg3.b.xfer_size_cntr_width);
  63801. +
  63802. + /*
  63803. + * Set the SRP sucess bit for FS-I2c
  63804. + */
  63805. + core_if->srp_success = 0;
  63806. + core_if->srp_timer_started = 0;
  63807. +
  63808. + /*
  63809. + * Create new workqueue and init works
  63810. + */
  63811. + core_if->wq_otg = DWC_WORKQ_ALLOC("dwc_otg");
  63812. + if (core_if->wq_otg == 0) {
  63813. + DWC_WARN("DWC_WORKQ_ALLOC failed\n");
  63814. + DWC_FREE(host_if);
  63815. + DWC_FREE(dev_if);
  63816. + DWC_FREE(core_if);
  63817. + return 0;
  63818. + }
  63819. +
  63820. + core_if->snpsid = DWC_READ_REG32(&core_if->core_global_regs->gsnpsid);
  63821. +
  63822. + DWC_PRINTF("Core Release: %x.%x%x%x\n",
  63823. + (core_if->snpsid >> 12 & 0xF),
  63824. + (core_if->snpsid >> 8 & 0xF),
  63825. + (core_if->snpsid >> 4 & 0xF), (core_if->snpsid & 0xF));
  63826. +
  63827. + core_if->wkp_timer = DWC_TIMER_ALLOC("Wake Up Timer",
  63828. + w_wakeup_detected, core_if);
  63829. + if (core_if->wkp_timer == 0) {
  63830. + DWC_WARN("DWC_TIMER_ALLOC failed\n");
  63831. + DWC_FREE(host_if);
  63832. + DWC_FREE(dev_if);
  63833. + DWC_WORKQ_FREE(core_if->wq_otg);
  63834. + DWC_FREE(core_if);
  63835. + return 0;
  63836. + }
  63837. +
  63838. + if (dwc_otg_setup_params(core_if)) {
  63839. + DWC_WARN("Error while setting core params\n");
  63840. + }
  63841. +
  63842. + core_if->hibernation_suspend = 0;
  63843. +
  63844. + /** ADP initialization */
  63845. + dwc_otg_adp_init(core_if);
  63846. +
  63847. + return core_if;
  63848. +}
  63849. +
  63850. +/**
  63851. + * This function frees the structures allocated by dwc_otg_cil_init().
  63852. + *
  63853. + * @param core_if The core interface pointer returned from
  63854. + * dwc_otg_cil_init().
  63855. + *
  63856. + */
  63857. +void dwc_otg_cil_remove(dwc_otg_core_if_t * core_if)
  63858. +{
  63859. + dctl_data_t dctl = {.d32 = 0 };
  63860. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  63861. +
  63862. + /* Disable all interrupts */
  63863. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 1, 0);
  63864. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  63865. +
  63866. + dctl.b.sftdiscon = 1;
  63867. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  63868. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0,
  63869. + dctl.d32);
  63870. + }
  63871. +
  63872. + if (core_if->wq_otg) {
  63873. + DWC_WORKQ_WAIT_WORK_DONE(core_if->wq_otg, 500);
  63874. + DWC_WORKQ_FREE(core_if->wq_otg);
  63875. + }
  63876. + if (core_if->dev_if) {
  63877. + DWC_FREE(core_if->dev_if);
  63878. + }
  63879. + if (core_if->host_if) {
  63880. + DWC_FREE(core_if->host_if);
  63881. + }
  63882. +
  63883. + /** Remove ADP Stuff */
  63884. + dwc_otg_adp_remove(core_if);
  63885. + if (core_if->core_params) {
  63886. + DWC_FREE(core_if->core_params);
  63887. + }
  63888. + if (core_if->wkp_timer) {
  63889. + DWC_TIMER_FREE(core_if->wkp_timer);
  63890. + }
  63891. + if (core_if->srp_timer) {
  63892. + DWC_TIMER_FREE(core_if->srp_timer);
  63893. + }
  63894. + DWC_FREE(core_if);
  63895. +}
  63896. +
  63897. +/**
  63898. + * This function enables the controller's Global Interrupt in the AHB Config
  63899. + * register.
  63900. + *
  63901. + * @param core_if Programming view of DWC_otg controller.
  63902. + */
  63903. +void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * core_if)
  63904. +{
  63905. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  63906. + ahbcfg.b.glblintrmsk = 1; /* Enable interrupts */
  63907. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 0, ahbcfg.d32);
  63908. +}
  63909. +
  63910. +/**
  63911. + * This function disables the controller's Global Interrupt in the AHB Config
  63912. + * register.
  63913. + *
  63914. + * @param core_if Programming view of DWC_otg controller.
  63915. + */
  63916. +void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * core_if)
  63917. +{
  63918. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  63919. + ahbcfg.b.glblintrmsk = 1; /* Disable interrupts */
  63920. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  63921. +}
  63922. +
  63923. +/**
  63924. + * This function initializes the commmon interrupts, used in both
  63925. + * device and host modes.
  63926. + *
  63927. + * @param core_if Programming view of the DWC_otg controller
  63928. + *
  63929. + */
  63930. +static void dwc_otg_enable_common_interrupts(dwc_otg_core_if_t * core_if)
  63931. +{
  63932. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  63933. + gintmsk_data_t intr_mask = {.d32 = 0 };
  63934. +
  63935. + /* Clear any pending OTG Interrupts */
  63936. + DWC_WRITE_REG32(&global_regs->gotgint, 0xFFFFFFFF);
  63937. +
  63938. + /* Clear any pending interrupts */
  63939. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  63940. +
  63941. + /*
  63942. + * Enable the interrupts in the GINTMSK.
  63943. + */
  63944. + intr_mask.b.modemismatch = 1;
  63945. + intr_mask.b.otgintr = 1;
  63946. +
  63947. + if (!core_if->dma_enable) {
  63948. + intr_mask.b.rxstsqlvl = 1;
  63949. + }
  63950. +
  63951. + intr_mask.b.conidstschng = 1;
  63952. + intr_mask.b.wkupintr = 1;
  63953. + intr_mask.b.disconnect = 0;
  63954. + intr_mask.b.usbsuspend = 1;
  63955. + intr_mask.b.sessreqintr = 1;
  63956. +#ifdef CONFIG_USB_DWC_OTG_LPM
  63957. + if (core_if->core_params->lpm_enable) {
  63958. + intr_mask.b.lpmtranrcvd = 1;
  63959. + }
  63960. +#endif
  63961. + DWC_WRITE_REG32(&global_regs->gintmsk, intr_mask.d32);
  63962. +}
  63963. +
  63964. +/*
  63965. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  63966. + * Hibernation. This function is for exiting from Device mode hibernation by
  63967. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  63968. + * @param core_if Programming view of DWC_otg controller.
  63969. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  63970. + * @param reset - indicates whether resume is initiated by Reset.
  63971. + */
  63972. +int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  63973. + int rem_wakeup, int reset)
  63974. +{
  63975. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  63976. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  63977. + dctl_data_t dctl = {.d32 = 0 };
  63978. +
  63979. + int timeout = 2000;
  63980. +
  63981. + if (!core_if->hibernation_suspend) {
  63982. + DWC_PRINTF("Already exited from Hibernation\n");
  63983. + return 1;
  63984. + }
  63985. +
  63986. + DWC_DEBUGPL(DBG_PCD, "%s called\n", __FUNCTION__);
  63987. + /* Switch-on voltage to the core */
  63988. + gpwrdn.b.pwrdnswtch = 1;
  63989. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  63990. + dwc_udelay(10);
  63991. +
  63992. + /* Reset core */
  63993. + gpwrdn.d32 = 0;
  63994. + gpwrdn.b.pwrdnrstn = 1;
  63995. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  63996. + dwc_udelay(10);
  63997. +
  63998. + /* Assert Restore signal */
  63999. + gpwrdn.d32 = 0;
  64000. + gpwrdn.b.restore = 1;
  64001. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  64002. + dwc_udelay(10);
  64003. +
  64004. + /* Disable power clamps */
  64005. + gpwrdn.d32 = 0;
  64006. + gpwrdn.b.pwrdnclmp = 1;
  64007. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64008. +
  64009. + if (rem_wakeup) {
  64010. + dwc_udelay(70);
  64011. + }
  64012. +
  64013. + /* Deassert Reset core */
  64014. + gpwrdn.d32 = 0;
  64015. + gpwrdn.b.pwrdnrstn = 1;
  64016. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  64017. + dwc_udelay(10);
  64018. +
  64019. + /* Disable PMU interrupt */
  64020. + gpwrdn.d32 = 0;
  64021. + gpwrdn.b.pmuintsel = 1;
  64022. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64023. +
  64024. + /* Mask interrupts from gpwrdn */
  64025. + gpwrdn.d32 = 0;
  64026. + gpwrdn.b.connect_det_msk = 1;
  64027. + gpwrdn.b.srp_det_msk = 1;
  64028. + gpwrdn.b.disconn_det_msk = 1;
  64029. + gpwrdn.b.rst_det_msk = 1;
  64030. + gpwrdn.b.lnstchng_msk = 1;
  64031. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64032. +
  64033. + /* Indicates that we are going out from hibernation */
  64034. + core_if->hibernation_suspend = 0;
  64035. +
  64036. + /*
  64037. + * Set Restore Essential Regs bit in PCGCCTL register, restore_mode = 1
  64038. + * indicates restore from remote_wakeup
  64039. + */
  64040. + restore_essential_regs(core_if, rem_wakeup, 0);
  64041. +
  64042. + /*
  64043. + * Wait a little for seeing new value of variable hibernation_suspend if
  64044. + * Restore done interrupt received before polling
  64045. + */
  64046. + dwc_udelay(10);
  64047. +
  64048. + if (core_if->hibernation_suspend == 0) {
  64049. + /*
  64050. + * Wait For Restore_done Interrupt. This mechanism of polling the
  64051. + * interrupt is introduced to avoid any possible race conditions
  64052. + */
  64053. + do {
  64054. + gintsts_data_t gintsts;
  64055. + gintsts.d32 =
  64056. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  64057. + if (gintsts.b.restoredone) {
  64058. + gintsts.d32 = 0;
  64059. + gintsts.b.restoredone = 1;
  64060. + DWC_WRITE_REG32(&core_if->core_global_regs->
  64061. + gintsts, gintsts.d32);
  64062. + DWC_PRINTF("Restore Done Interrupt seen\n");
  64063. + break;
  64064. + }
  64065. + dwc_udelay(10);
  64066. + } while (--timeout);
  64067. + if (!timeout) {
  64068. + DWC_PRINTF("Restore Done interrupt wasn't generated here\n");
  64069. + }
  64070. + }
  64071. + /* Clear all pending interupts */
  64072. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  64073. +
  64074. + /* De-assert Restore */
  64075. + gpwrdn.d32 = 0;
  64076. + gpwrdn.b.restore = 1;
  64077. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64078. + dwc_udelay(10);
  64079. +
  64080. + if (!rem_wakeup) {
  64081. + pcgcctl.d32 = 0;
  64082. + pcgcctl.b.rstpdwnmodule = 1;
  64083. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  64084. + }
  64085. +
  64086. + /* Restore GUSBCFG and DCFG */
  64087. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  64088. + core_if->gr_backup->gusbcfg_local);
  64089. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  64090. + core_if->dr_backup->dcfg);
  64091. +
  64092. + /* De-assert Wakeup Logic */
  64093. + gpwrdn.d32 = 0;
  64094. + gpwrdn.b.pmuactv = 1;
  64095. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64096. + dwc_udelay(10);
  64097. +
  64098. + if (!rem_wakeup) {
  64099. + /* Set Device programming done bit */
  64100. + dctl.b.pwronprgdone = 1;
  64101. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  64102. + } else {
  64103. + /* Start Remote Wakeup Signaling */
  64104. + dctl.d32 = core_if->dr_backup->dctl;
  64105. + dctl.b.rmtwkupsig = 1;
  64106. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  64107. + }
  64108. +
  64109. + dwc_mdelay(2);
  64110. + /* Clear all pending interupts */
  64111. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  64112. +
  64113. + /* Restore global registers */
  64114. + dwc_otg_restore_global_regs(core_if);
  64115. + /* Restore device global registers */
  64116. + dwc_otg_restore_dev_regs(core_if, rem_wakeup);
  64117. +
  64118. + if (rem_wakeup) {
  64119. + dwc_mdelay(7);
  64120. + dctl.d32 = 0;
  64121. + dctl.b.rmtwkupsig = 1;
  64122. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  64123. + }
  64124. +
  64125. + core_if->hibernation_suspend = 0;
  64126. + /* The core will be in ON STATE */
  64127. + core_if->lx_state = DWC_OTG_L0;
  64128. + DWC_PRINTF("Hibernation recovery completes here\n");
  64129. +
  64130. + return 1;
  64131. +}
  64132. +
  64133. +/*
  64134. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  64135. + * Hibernation. This function is for exiting from Host mode hibernation by
  64136. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  64137. + * @param core_if Programming view of DWC_otg controller.
  64138. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  64139. + * @param reset - indicates whether resume is initiated by Reset.
  64140. + */
  64141. +int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  64142. + int rem_wakeup, int reset)
  64143. +{
  64144. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64145. + hprt0_data_t hprt0 = {.d32 = 0 };
  64146. +
  64147. + int timeout = 2000;
  64148. +
  64149. + DWC_DEBUGPL(DBG_HCD, "%s called\n", __FUNCTION__);
  64150. + /* Switch-on voltage to the core */
  64151. + gpwrdn.b.pwrdnswtch = 1;
  64152. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64153. + dwc_udelay(10);
  64154. +
  64155. + /* Reset core */
  64156. + gpwrdn.d32 = 0;
  64157. + gpwrdn.b.pwrdnrstn = 1;
  64158. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64159. + dwc_udelay(10);
  64160. +
  64161. + /* Assert Restore signal */
  64162. + gpwrdn.d32 = 0;
  64163. + gpwrdn.b.restore = 1;
  64164. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  64165. + dwc_udelay(10);
  64166. +
  64167. + /* Disable power clamps */
  64168. + gpwrdn.d32 = 0;
  64169. + gpwrdn.b.pwrdnclmp = 1;
  64170. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64171. +
  64172. + if (!rem_wakeup) {
  64173. + dwc_udelay(50);
  64174. + }
  64175. +
  64176. + /* Deassert Reset core */
  64177. + gpwrdn.d32 = 0;
  64178. + gpwrdn.b.pwrdnrstn = 1;
  64179. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  64180. + dwc_udelay(10);
  64181. +
  64182. + /* Disable PMU interrupt */
  64183. + gpwrdn.d32 = 0;
  64184. + gpwrdn.b.pmuintsel = 1;
  64185. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64186. +
  64187. + gpwrdn.d32 = 0;
  64188. + gpwrdn.b.connect_det_msk = 1;
  64189. + gpwrdn.b.srp_det_msk = 1;
  64190. + gpwrdn.b.disconn_det_msk = 1;
  64191. + gpwrdn.b.rst_det_msk = 1;
  64192. + gpwrdn.b.lnstchng_msk = 1;
  64193. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64194. +
  64195. + /* Indicates that we are going out from hibernation */
  64196. + core_if->hibernation_suspend = 0;
  64197. +
  64198. + /* Set Restore Essential Regs bit in PCGCCTL register */
  64199. + restore_essential_regs(core_if, rem_wakeup, 1);
  64200. +
  64201. + /* Wait a little for seeing new value of variable hibernation_suspend if
  64202. + * Restore done interrupt received before polling */
  64203. + dwc_udelay(10);
  64204. +
  64205. + if (core_if->hibernation_suspend == 0) {
  64206. + /* Wait For Restore_done Interrupt. This mechanism of polling the
  64207. + * interrupt is introduced to avoid any possible race conditions
  64208. + */
  64209. + do {
  64210. + gintsts_data_t gintsts;
  64211. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  64212. + if (gintsts.b.restoredone) {
  64213. + gintsts.d32 = 0;
  64214. + gintsts.b.restoredone = 1;
  64215. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  64216. + DWC_DEBUGPL(DBG_HCD,"Restore Done Interrupt seen\n");
  64217. + break;
  64218. + }
  64219. + dwc_udelay(10);
  64220. + } while (--timeout);
  64221. + if (!timeout) {
  64222. + DWC_WARN("Restore Done interrupt wasn't generated\n");
  64223. + }
  64224. + }
  64225. +
  64226. + /* Set the flag's value to 0 again after receiving restore done interrupt */
  64227. + core_if->hibernation_suspend = 0;
  64228. +
  64229. + /* This step is not described in functional spec but if not wait for this
  64230. + * delay, mismatch interrupts occurred because just after restore core is
  64231. + * in Device mode(gintsts.curmode == 0) */
  64232. + dwc_mdelay(100);
  64233. +
  64234. + /* Clear all pending interrupts */
  64235. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  64236. +
  64237. + /* De-assert Restore */
  64238. + gpwrdn.d32 = 0;
  64239. + gpwrdn.b.restore = 1;
  64240. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64241. + dwc_udelay(10);
  64242. +
  64243. + /* Restore GUSBCFG and HCFG */
  64244. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  64245. + core_if->gr_backup->gusbcfg_local);
  64246. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  64247. + core_if->hr_backup->hcfg_local);
  64248. +
  64249. + /* De-assert Wakeup Logic */
  64250. + gpwrdn.d32 = 0;
  64251. + gpwrdn.b.pmuactv = 1;
  64252. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64253. + dwc_udelay(10);
  64254. +
  64255. + /* Start the Resume operation by programming HPRT0 */
  64256. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  64257. + hprt0.b.prtpwr = 1;
  64258. + hprt0.b.prtena = 0;
  64259. + hprt0.b.prtsusp = 0;
  64260. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  64261. +
  64262. + DWC_PRINTF("Resume Starts Now\n");
  64263. + if (!reset) { // Indicates it is Resume Operation
  64264. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  64265. + hprt0.b.prtres = 1;
  64266. + hprt0.b.prtpwr = 1;
  64267. + hprt0.b.prtena = 0;
  64268. + hprt0.b.prtsusp = 0;
  64269. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  64270. +
  64271. + if (!rem_wakeup)
  64272. + hprt0.b.prtres = 0;
  64273. + /* Wait for Resume time and then program HPRT again */
  64274. + dwc_mdelay(100);
  64275. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  64276. +
  64277. + } else { // Indicates it is Reset Operation
  64278. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  64279. + hprt0.b.prtrst = 1;
  64280. + hprt0.b.prtpwr = 1;
  64281. + hprt0.b.prtena = 0;
  64282. + hprt0.b.prtsusp = 0;
  64283. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  64284. + /* Wait for Reset time and then program HPRT again */
  64285. + dwc_mdelay(60);
  64286. + hprt0.b.prtrst = 0;
  64287. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  64288. + }
  64289. + /* Clear all interrupt status */
  64290. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  64291. + hprt0.b.prtconndet = 1;
  64292. + hprt0.b.prtenchng = 1;
  64293. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  64294. +
  64295. + /* Clear all pending interupts */
  64296. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  64297. +
  64298. + /* Restore global registers */
  64299. + dwc_otg_restore_global_regs(core_if);
  64300. + /* Restore host global registers */
  64301. + dwc_otg_restore_host_regs(core_if, reset);
  64302. +
  64303. + /* The core will be in ON STATE */
  64304. + core_if->lx_state = DWC_OTG_L0;
  64305. + DWC_PRINTF("Hibernation recovery is complete here\n");
  64306. + return 0;
  64307. +}
  64308. +
  64309. +/** Saves some register values into system memory. */
  64310. +int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if)
  64311. +{
  64312. + struct dwc_otg_global_regs_backup *gr;
  64313. + int i;
  64314. +
  64315. + gr = core_if->gr_backup;
  64316. + if (!gr) {
  64317. + gr = DWC_ALLOC(sizeof(*gr));
  64318. + if (!gr) {
  64319. + return -DWC_E_NO_MEMORY;
  64320. + }
  64321. + core_if->gr_backup = gr;
  64322. + }
  64323. +
  64324. + gr->gotgctl_local = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  64325. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  64326. + gr->gahbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  64327. + gr->gusbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  64328. + gr->grxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  64329. + gr->gnptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  64330. + gr->hptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  64331. +#ifdef CONFIG_USB_DWC_OTG_LPM
  64332. + gr->glpmcfg_local = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  64333. +#endif
  64334. + gr->gi2cctl_local = DWC_READ_REG32(&core_if->core_global_regs->gi2cctl);
  64335. + gr->pcgcctl_local = DWC_READ_REG32(core_if->pcgcctl);
  64336. + gr->gdfifocfg_local =
  64337. + DWC_READ_REG32(&core_if->core_global_regs->gdfifocfg);
  64338. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  64339. + gr->dtxfsiz_local[i] =
  64340. + DWC_READ_REG32(&(core_if->core_global_regs->dtxfsiz[i]));
  64341. + }
  64342. +
  64343. + DWC_DEBUGPL(DBG_ANY, "===========Backing Global registers==========\n");
  64344. + DWC_DEBUGPL(DBG_ANY, "Backed up gotgctl = %08x\n", gr->gotgctl_local);
  64345. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  64346. + DWC_DEBUGPL(DBG_ANY, "Backed up gahbcfg = %08x\n", gr->gahbcfg_local);
  64347. + DWC_DEBUGPL(DBG_ANY, "Backed up gusbcfg = %08x\n", gr->gusbcfg_local);
  64348. + DWC_DEBUGPL(DBG_ANY, "Backed up grxfsiz = %08x\n", gr->grxfsiz_local);
  64349. + DWC_DEBUGPL(DBG_ANY, "Backed up gnptxfsiz = %08x\n",
  64350. + gr->gnptxfsiz_local);
  64351. + DWC_DEBUGPL(DBG_ANY, "Backed up hptxfsiz = %08x\n",
  64352. + gr->hptxfsiz_local);
  64353. +#ifdef CONFIG_USB_DWC_OTG_LPM
  64354. + DWC_DEBUGPL(DBG_ANY, "Backed up glpmcfg = %08x\n", gr->glpmcfg_local);
  64355. +#endif
  64356. + DWC_DEBUGPL(DBG_ANY, "Backed up gi2cctl = %08x\n", gr->gi2cctl_local);
  64357. + DWC_DEBUGPL(DBG_ANY, "Backed up pcgcctl = %08x\n", gr->pcgcctl_local);
  64358. + DWC_DEBUGPL(DBG_ANY,"Backed up gdfifocfg = %08x\n",gr->gdfifocfg_local);
  64359. +
  64360. + return 0;
  64361. +}
  64362. +
  64363. +/** Saves GINTMSK register before setting the msk bits. */
  64364. +int dwc_otg_save_gintmsk_reg(dwc_otg_core_if_t * core_if)
  64365. +{
  64366. + struct dwc_otg_global_regs_backup *gr;
  64367. +
  64368. + gr = core_if->gr_backup;
  64369. + if (!gr) {
  64370. + gr = DWC_ALLOC(sizeof(*gr));
  64371. + if (!gr) {
  64372. + return -DWC_E_NO_MEMORY;
  64373. + }
  64374. + core_if->gr_backup = gr;
  64375. + }
  64376. +
  64377. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  64378. +
  64379. + DWC_DEBUGPL(DBG_ANY,"=============Backing GINTMSK registers============\n");
  64380. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  64381. +
  64382. + return 0;
  64383. +}
  64384. +
  64385. +int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if)
  64386. +{
  64387. + struct dwc_otg_dev_regs_backup *dr;
  64388. + int i;
  64389. +
  64390. + dr = core_if->dr_backup;
  64391. + if (!dr) {
  64392. + dr = DWC_ALLOC(sizeof(*dr));
  64393. + if (!dr) {
  64394. + return -DWC_E_NO_MEMORY;
  64395. + }
  64396. + core_if->dr_backup = dr;
  64397. + }
  64398. +
  64399. + dr->dcfg = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  64400. + dr->dctl = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  64401. + dr->daintmsk =
  64402. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  64403. + dr->diepmsk =
  64404. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->diepmsk);
  64405. + dr->doepmsk =
  64406. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->doepmsk);
  64407. +
  64408. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  64409. + dr->diepctl[i] =
  64410. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  64411. + dr->dieptsiz[i] =
  64412. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz);
  64413. + dr->diepdma[i] =
  64414. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma);
  64415. + }
  64416. +
  64417. + DWC_DEBUGPL(DBG_ANY,
  64418. + "=============Backing Host registers==============\n");
  64419. + DWC_DEBUGPL(DBG_ANY, "Backed up dcfg = %08x\n", dr->dcfg);
  64420. + DWC_DEBUGPL(DBG_ANY, "Backed up dctl = %08x\n", dr->dctl);
  64421. + DWC_DEBUGPL(DBG_ANY, "Backed up daintmsk = %08x\n",
  64422. + dr->daintmsk);
  64423. + DWC_DEBUGPL(DBG_ANY, "Backed up diepmsk = %08x\n", dr->diepmsk);
  64424. + DWC_DEBUGPL(DBG_ANY, "Backed up doepmsk = %08x\n", dr->doepmsk);
  64425. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  64426. + DWC_DEBUGPL(DBG_ANY, "Backed up diepctl[%d] = %08x\n", i,
  64427. + dr->diepctl[i]);
  64428. + DWC_DEBUGPL(DBG_ANY, "Backed up dieptsiz[%d] = %08x\n",
  64429. + i, dr->dieptsiz[i]);
  64430. + DWC_DEBUGPL(DBG_ANY, "Backed up diepdma[%d] = %08x\n", i,
  64431. + dr->diepdma[i]);
  64432. + }
  64433. +
  64434. + return 0;
  64435. +}
  64436. +
  64437. +int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if)
  64438. +{
  64439. + struct dwc_otg_host_regs_backup *hr;
  64440. + int i;
  64441. +
  64442. + hr = core_if->hr_backup;
  64443. + if (!hr) {
  64444. + hr = DWC_ALLOC(sizeof(*hr));
  64445. + if (!hr) {
  64446. + return -DWC_E_NO_MEMORY;
  64447. + }
  64448. + core_if->hr_backup = hr;
  64449. + }
  64450. +
  64451. + hr->hcfg_local =
  64452. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  64453. + hr->haintmsk_local =
  64454. + DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
  64455. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  64456. + hr->hcintmsk_local[i] =
  64457. + DWC_READ_REG32(&core_if->host_if->hc_regs[i]->hcintmsk);
  64458. + }
  64459. + hr->hprt0_local = DWC_READ_REG32(core_if->host_if->hprt0);
  64460. + hr->hfir_local =
  64461. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  64462. +
  64463. + DWC_DEBUGPL(DBG_ANY,
  64464. + "=============Backing Host registers===============\n");
  64465. + DWC_DEBUGPL(DBG_ANY, "Backed up hcfg = %08x\n",
  64466. + hr->hcfg_local);
  64467. + DWC_DEBUGPL(DBG_ANY, "Backed up haintmsk = %08x\n", hr->haintmsk_local);
  64468. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  64469. + DWC_DEBUGPL(DBG_ANY, "Backed up hcintmsk[%02d]=%08x\n", i,
  64470. + hr->hcintmsk_local[i]);
  64471. + }
  64472. + DWC_DEBUGPL(DBG_ANY, "Backed up hprt0 = %08x\n",
  64473. + hr->hprt0_local);
  64474. + DWC_DEBUGPL(DBG_ANY, "Backed up hfir = %08x\n",
  64475. + hr->hfir_local);
  64476. +
  64477. + return 0;
  64478. +}
  64479. +
  64480. +int dwc_otg_restore_global_regs(dwc_otg_core_if_t *core_if)
  64481. +{
  64482. + struct dwc_otg_global_regs_backup *gr;
  64483. + int i;
  64484. +
  64485. + gr = core_if->gr_backup;
  64486. + if (!gr) {
  64487. + return -DWC_E_INVALID;
  64488. + }
  64489. +
  64490. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, gr->gotgctl_local);
  64491. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gr->gintmsk_local);
  64492. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gr->gusbcfg_local);
  64493. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gr->gahbcfg_local);
  64494. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, gr->grxfsiz_local);
  64495. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz,
  64496. + gr->gnptxfsiz_local);
  64497. + DWC_WRITE_REG32(&core_if->core_global_regs->hptxfsiz,
  64498. + gr->hptxfsiz_local);
  64499. + DWC_WRITE_REG32(&core_if->core_global_regs->gdfifocfg,
  64500. + gr->gdfifocfg_local);
  64501. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  64502. + DWC_WRITE_REG32(&core_if->core_global_regs->dtxfsiz[i],
  64503. + gr->dtxfsiz_local[i]);
  64504. + }
  64505. +
  64506. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  64507. + DWC_WRITE_REG32(core_if->host_if->hprt0, 0x0000100A);
  64508. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg,
  64509. + (gr->gahbcfg_local));
  64510. + return 0;
  64511. +}
  64512. +
  64513. +int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if, int rem_wakeup)
  64514. +{
  64515. + struct dwc_otg_dev_regs_backup *dr;
  64516. + int i;
  64517. +
  64518. + dr = core_if->dr_backup;
  64519. +
  64520. + if (!dr) {
  64521. + return -DWC_E_INVALID;
  64522. + }
  64523. +
  64524. + if (!rem_wakeup) {
  64525. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  64526. + dr->dctl);
  64527. + }
  64528. +
  64529. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, dr->daintmsk);
  64530. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, dr->diepmsk);
  64531. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, dr->doepmsk);
  64532. +
  64533. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  64534. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz, dr->dieptsiz[i]);
  64535. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma, dr->diepdma[i]);
  64536. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl, dr->diepctl[i]);
  64537. + }
  64538. +
  64539. + return 0;
  64540. +}
  64541. +
  64542. +int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset)
  64543. +{
  64544. + struct dwc_otg_host_regs_backup *hr;
  64545. + int i;
  64546. + hr = core_if->hr_backup;
  64547. +
  64548. + if (!hr) {
  64549. + return -DWC_E_INVALID;
  64550. + }
  64551. +
  64552. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hr->hcfg_local);
  64553. + //if (!reset)
  64554. + //{
  64555. + // DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hr->hfir_local);
  64556. + //}
  64557. +
  64558. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk,
  64559. + hr->haintmsk_local);
  64560. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  64561. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk,
  64562. + hr->hcintmsk_local[i]);
  64563. + }
  64564. +
  64565. + return 0;
  64566. +}
  64567. +
  64568. +int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if)
  64569. +{
  64570. + struct dwc_otg_global_regs_backup *gr;
  64571. +
  64572. + gr = core_if->gr_backup;
  64573. +
  64574. + /* Restore values for LPM and I2C */
  64575. +#ifdef CONFIG_USB_DWC_OTG_LPM
  64576. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, gr->glpmcfg_local);
  64577. +#endif
  64578. + DWC_WRITE_REG32(&core_if->core_global_regs->gi2cctl, gr->gi2cctl_local);
  64579. +
  64580. + return 0;
  64581. +}
  64582. +
  64583. +int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode, int is_host)
  64584. +{
  64585. + struct dwc_otg_global_regs_backup *gr;
  64586. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  64587. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  64588. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  64589. + gintmsk_data_t gintmsk = {.d32 = 0 };
  64590. +
  64591. + /* Restore LPM and I2C registers */
  64592. + restore_lpm_i2c_regs(core_if);
  64593. +
  64594. + /* Set PCGCCTL to 0 */
  64595. + DWC_WRITE_REG32(core_if->pcgcctl, 0x00000000);
  64596. +
  64597. + gr = core_if->gr_backup;
  64598. + /* Load restore values for [31:14] bits */
  64599. + DWC_WRITE_REG32(core_if->pcgcctl,
  64600. + ((gr->pcgcctl_local & 0xffffc000) | 0x00020000));
  64601. +
  64602. + /* Umnask global Interrupt in GAHBCFG and restore it */
  64603. + gahbcfg.d32 = gr->gahbcfg_local;
  64604. + gahbcfg.b.glblintrmsk = 1;
  64605. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  64606. +
  64607. + /* Clear all pending interupts */
  64608. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  64609. +
  64610. + /* Unmask restore done interrupt */
  64611. + gintmsk.b.restoredone = 1;
  64612. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  64613. +
  64614. + /* Restore GUSBCFG and HCFG/DCFG */
  64615. + gusbcfg.d32 = core_if->gr_backup->gusbcfg_local;
  64616. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  64617. +
  64618. + if (is_host) {
  64619. + hcfg_data_t hcfg = {.d32 = 0 };
  64620. + hcfg.d32 = core_if->hr_backup->hcfg_local;
  64621. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  64622. + hcfg.d32);
  64623. +
  64624. + /* Load restore values for [31:14] bits */
  64625. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  64626. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  64627. +
  64628. + if (rmode)
  64629. + pcgcctl.b.restoremode = 1;
  64630. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  64631. + dwc_udelay(10);
  64632. +
  64633. + /* Load restore values for [31:14] bits and set EssRegRestored bit */
  64634. + pcgcctl.d32 = gr->pcgcctl_local | 0xffffc000;
  64635. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  64636. + pcgcctl.b.ess_reg_restored = 1;
  64637. + if (rmode)
  64638. + pcgcctl.b.restoremode = 1;
  64639. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  64640. + } else {
  64641. + dcfg_data_t dcfg = {.d32 = 0 };
  64642. + dcfg.d32 = core_if->dr_backup->dcfg;
  64643. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  64644. +
  64645. + /* Load restore values for [31:14] bits */
  64646. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  64647. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  64648. + if (!rmode) {
  64649. + pcgcctl.d32 |= 0x208;
  64650. + }
  64651. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  64652. + dwc_udelay(10);
  64653. +
  64654. + /* Load restore values for [31:14] bits */
  64655. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  64656. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  64657. + pcgcctl.b.ess_reg_restored = 1;
  64658. + if (!rmode)
  64659. + pcgcctl.d32 |= 0x208;
  64660. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  64661. + }
  64662. +
  64663. + return 0;
  64664. +}
  64665. +
  64666. +/**
  64667. + * Initializes the FSLSPClkSel field of the HCFG register depending on the PHY
  64668. + * type.
  64669. + */
  64670. +static void init_fslspclksel(dwc_otg_core_if_t * core_if)
  64671. +{
  64672. + uint32_t val;
  64673. + hcfg_data_t hcfg;
  64674. +
  64675. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  64676. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  64677. + (core_if->core_params->ulpi_fs_ls)) ||
  64678. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  64679. + /* Full speed PHY */
  64680. + val = DWC_HCFG_48_MHZ;
  64681. + } else {
  64682. + /* High speed PHY running at full speed or high speed */
  64683. + val = DWC_HCFG_30_60_MHZ;
  64684. + }
  64685. +
  64686. + DWC_DEBUGPL(DBG_CIL, "Initializing HCFG.FSLSPClkSel to 0x%1x\n", val);
  64687. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  64688. + hcfg.b.fslspclksel = val;
  64689. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  64690. +}
  64691. +
  64692. +/**
  64693. + * Initializes the DevSpd field of the DCFG register depending on the PHY type
  64694. + * and the enumeration speed of the device.
  64695. + */
  64696. +static void init_devspd(dwc_otg_core_if_t * core_if)
  64697. +{
  64698. + uint32_t val;
  64699. + dcfg_data_t dcfg;
  64700. +
  64701. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  64702. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  64703. + (core_if->core_params->ulpi_fs_ls)) ||
  64704. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  64705. + /* Full speed PHY */
  64706. + val = 0x3;
  64707. + } else if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  64708. + /* High speed PHY running at full speed */
  64709. + val = 0x1;
  64710. + } else {
  64711. + /* High speed PHY running at high speed */
  64712. + val = 0x0;
  64713. + }
  64714. +
  64715. + DWC_DEBUGPL(DBG_CIL, "Initializing DCFG.DevSpd to 0x%1x\n", val);
  64716. +
  64717. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  64718. + dcfg.b.devspd = val;
  64719. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  64720. +}
  64721. +
  64722. +/**
  64723. + * This function calculates the number of IN EPS
  64724. + * using GHWCFG1 and GHWCFG2 registers values
  64725. + *
  64726. + * @param core_if Programming view of the DWC_otg controller
  64727. + */
  64728. +static uint32_t calc_num_in_eps(dwc_otg_core_if_t * core_if)
  64729. +{
  64730. + uint32_t num_in_eps = 0;
  64731. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  64732. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 3;
  64733. + uint32_t num_tx_fifos = core_if->hwcfg4.b.num_in_eps;
  64734. + int i;
  64735. +
  64736. + for (i = 0; i < num_eps; ++i) {
  64737. + if (!(hwcfg1 & 0x1))
  64738. + num_in_eps++;
  64739. +
  64740. + hwcfg1 >>= 2;
  64741. + }
  64742. +
  64743. + if (core_if->hwcfg4.b.ded_fifo_en) {
  64744. + num_in_eps =
  64745. + (num_in_eps > num_tx_fifos) ? num_tx_fifos : num_in_eps;
  64746. + }
  64747. +
  64748. + return num_in_eps;
  64749. +}
  64750. +
  64751. +/**
  64752. + * This function calculates the number of OUT EPS
  64753. + * using GHWCFG1 and GHWCFG2 registers values
  64754. + *
  64755. + * @param core_if Programming view of the DWC_otg controller
  64756. + */
  64757. +static uint32_t calc_num_out_eps(dwc_otg_core_if_t * core_if)
  64758. +{
  64759. + uint32_t num_out_eps = 0;
  64760. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  64761. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 2;
  64762. + int i;
  64763. +
  64764. + for (i = 0; i < num_eps; ++i) {
  64765. + if (!(hwcfg1 & 0x1))
  64766. + num_out_eps++;
  64767. +
  64768. + hwcfg1 >>= 2;
  64769. + }
  64770. + return num_out_eps;
  64771. +}
  64772. +
  64773. +/**
  64774. + * This function initializes the DWC_otg controller registers and
  64775. + * prepares the core for device mode or host mode operation.
  64776. + *
  64777. + * @param core_if Programming view of the DWC_otg controller
  64778. + *
  64779. + */
  64780. +void dwc_otg_core_init(dwc_otg_core_if_t * core_if)
  64781. +{
  64782. + int i = 0;
  64783. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  64784. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  64785. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  64786. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  64787. + gi2cctl_data_t i2cctl = {.d32 = 0 };
  64788. +
  64789. + DWC_DEBUGPL(DBG_CILV, "dwc_otg_core_init(%p) regs at %p\n",
  64790. + core_if, global_regs);
  64791. +
  64792. + /* Common Initialization */
  64793. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  64794. +
  64795. + /* Program the ULPI External VBUS bit if needed */
  64796. + usbcfg.b.ulpi_ext_vbus_drv =
  64797. + (core_if->core_params->phy_ulpi_ext_vbus ==
  64798. + DWC_PHY_ULPI_EXTERNAL_VBUS) ? 1 : 0;
  64799. +
  64800. + /* Set external TS Dline pulsing */
  64801. + usbcfg.b.term_sel_dl_pulse =
  64802. + (core_if->core_params->ts_dline == 1) ? 1 : 0;
  64803. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  64804. +
  64805. + /* Reset the Controller */
  64806. + dwc_otg_core_reset(core_if);
  64807. +
  64808. + core_if->adp_enable = core_if->core_params->adp_supp_enable;
  64809. + core_if->power_down = core_if->core_params->power_down;
  64810. + core_if->otg_sts = 0;
  64811. +
  64812. + /* Initialize parameters from Hardware configuration registers. */
  64813. + dev_if->num_in_eps = calc_num_in_eps(core_if);
  64814. + dev_if->num_out_eps = calc_num_out_eps(core_if);
  64815. +
  64816. + DWC_DEBUGPL(DBG_CIL, "num_dev_perio_in_ep=%d\n",
  64817. + core_if->hwcfg4.b.num_dev_perio_in_ep);
  64818. +
  64819. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  64820. + dev_if->perio_tx_fifo_size[i] =
  64821. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  64822. + DWC_DEBUGPL(DBG_CIL, "Periodic Tx FIFO SZ #%d=0x%0x\n",
  64823. + i, dev_if->perio_tx_fifo_size[i]);
  64824. + }
  64825. +
  64826. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  64827. + dev_if->tx_fifo_size[i] =
  64828. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  64829. + DWC_DEBUGPL(DBG_CIL, "Tx FIFO SZ #%d=0x%0x\n",
  64830. + i, dev_if->tx_fifo_size[i]);
  64831. + }
  64832. +
  64833. + core_if->total_fifo_size = core_if->hwcfg3.b.dfifo_depth;
  64834. + core_if->rx_fifo_size = DWC_READ_REG32(&global_regs->grxfsiz);
  64835. + core_if->nperio_tx_fifo_size =
  64836. + DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16;
  64837. +
  64838. + DWC_DEBUGPL(DBG_CIL, "Total FIFO SZ=%d\n", core_if->total_fifo_size);
  64839. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO SZ=%d\n", core_if->rx_fifo_size);
  64840. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO SZ=%d\n",
  64841. + core_if->nperio_tx_fifo_size);
  64842. +
  64843. + /* This programming sequence needs to happen in FS mode before any other
  64844. + * programming occurs */
  64845. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) &&
  64846. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  64847. + /* If FS mode with FS PHY */
  64848. +
  64849. + /* core_init() is now called on every switch so only call the
  64850. + * following for the first time through. */
  64851. + if (!core_if->phy_init_done) {
  64852. + core_if->phy_init_done = 1;
  64853. + DWC_DEBUGPL(DBG_CIL, "FS_PHY detected\n");
  64854. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  64855. + usbcfg.b.physel = 1;
  64856. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  64857. +
  64858. + /* Reset after a PHY select */
  64859. + dwc_otg_core_reset(core_if);
  64860. + }
  64861. +
  64862. + /* Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
  64863. + * do this on HNP Dev/Host mode switches (done in dev_init and
  64864. + * host_init). */
  64865. + if (dwc_otg_is_host_mode(core_if)) {
  64866. + init_fslspclksel(core_if);
  64867. + } else {
  64868. + init_devspd(core_if);
  64869. + }
  64870. +
  64871. + if (core_if->core_params->i2c_enable) {
  64872. + DWC_DEBUGPL(DBG_CIL, "FS_PHY Enabling I2c\n");
  64873. + /* Program GUSBCFG.OtgUtmifsSel to I2C */
  64874. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  64875. + usbcfg.b.otgutmifssel = 1;
  64876. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  64877. +
  64878. + /* Program GI2CCTL.I2CEn */
  64879. + i2cctl.d32 = DWC_READ_REG32(&global_regs->gi2cctl);
  64880. + i2cctl.b.i2cdevaddr = 1;
  64881. + i2cctl.b.i2cen = 0;
  64882. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  64883. + i2cctl.b.i2cen = 1;
  64884. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  64885. + }
  64886. +
  64887. + } /* endif speed == DWC_SPEED_PARAM_FULL */
  64888. + else {
  64889. + /* High speed PHY. */
  64890. + if (!core_if->phy_init_done) {
  64891. + core_if->phy_init_done = 1;
  64892. + /* HS PHY parameters. These parameters are preserved
  64893. + * during soft reset so only program the first time. Do
  64894. + * a soft reset immediately after setting phyif. */
  64895. +
  64896. + if (core_if->core_params->phy_type == 2) {
  64897. + /* ULPI interface */
  64898. + usbcfg.b.ulpi_utmi_sel = 1;
  64899. + usbcfg.b.phyif = 0;
  64900. + usbcfg.b.ddrsel =
  64901. + core_if->core_params->phy_ulpi_ddr;
  64902. + } else if (core_if->core_params->phy_type == 1) {
  64903. + /* UTMI+ interface */
  64904. + usbcfg.b.ulpi_utmi_sel = 0;
  64905. + if (core_if->core_params->phy_utmi_width == 16) {
  64906. + usbcfg.b.phyif = 1;
  64907. +
  64908. + } else {
  64909. + usbcfg.b.phyif = 0;
  64910. + }
  64911. + } else {
  64912. + DWC_ERROR("FS PHY TYPE\n");
  64913. + }
  64914. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  64915. + /* Reset after setting the PHY parameters */
  64916. + dwc_otg_core_reset(core_if);
  64917. + }
  64918. + }
  64919. +
  64920. + if ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  64921. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  64922. + (core_if->core_params->ulpi_fs_ls)) {
  64923. + DWC_DEBUGPL(DBG_CIL, "Setting ULPI FSLS\n");
  64924. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  64925. + usbcfg.b.ulpi_fsls = 1;
  64926. + usbcfg.b.ulpi_clk_sus_m = 1;
  64927. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  64928. + } else {
  64929. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  64930. + usbcfg.b.ulpi_fsls = 0;
  64931. + usbcfg.b.ulpi_clk_sus_m = 0;
  64932. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  64933. + }
  64934. +
  64935. + /* Program the GAHBCFG Register. */
  64936. + switch (core_if->hwcfg2.b.architecture) {
  64937. +
  64938. + case DWC_SLAVE_ONLY_ARCH:
  64939. + DWC_DEBUGPL(DBG_CIL, "Slave Only Mode\n");
  64940. + ahbcfg.b.nptxfemplvl_txfemplvl =
  64941. + DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  64942. + ahbcfg.b.ptxfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  64943. + core_if->dma_enable = 0;
  64944. + core_if->dma_desc_enable = 0;
  64945. + break;
  64946. +
  64947. + case DWC_EXT_DMA_ARCH:
  64948. + DWC_DEBUGPL(DBG_CIL, "External DMA Mode\n");
  64949. + {
  64950. + uint8_t brst_sz = core_if->core_params->dma_burst_size;
  64951. + ahbcfg.b.hburstlen = 0;
  64952. + while (brst_sz > 1) {
  64953. + ahbcfg.b.hburstlen++;
  64954. + brst_sz >>= 1;
  64955. + }
  64956. + }
  64957. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  64958. + core_if->dma_desc_enable =
  64959. + (core_if->core_params->dma_desc_enable != 0);
  64960. + break;
  64961. +
  64962. + case DWC_INT_DMA_ARCH:
  64963. + DWC_DEBUGPL(DBG_CIL, "Internal DMA Mode\n");
  64964. + /* Old value was DWC_GAHBCFG_INT_DMA_BURST_INCR - done for
  64965. + Host mode ISOC in issue fix - vahrama */
  64966. + /* Broadcom had altered to (1<<3)|(0<<0) - WRESP=1, max 4 beats */
  64967. + ahbcfg.b.hburstlen = (1<<3)|(0<<0);//DWC_GAHBCFG_INT_DMA_BURST_INCR4;
  64968. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  64969. + core_if->dma_desc_enable =
  64970. + (core_if->core_params->dma_desc_enable != 0);
  64971. + break;
  64972. +
  64973. + }
  64974. + if (core_if->dma_enable) {
  64975. + if (core_if->dma_desc_enable) {
  64976. + DWC_PRINTF("Using Descriptor DMA mode\n");
  64977. + } else {
  64978. + DWC_PRINTF("Using Buffer DMA mode\n");
  64979. +
  64980. + }
  64981. + } else {
  64982. + DWC_PRINTF("Using Slave mode\n");
  64983. + core_if->dma_desc_enable = 0;
  64984. + }
  64985. +
  64986. + if (core_if->core_params->ahb_single) {
  64987. + ahbcfg.b.ahbsingle = 1;
  64988. + }
  64989. +
  64990. + ahbcfg.b.dmaenable = core_if->dma_enable;
  64991. + DWC_WRITE_REG32(&global_regs->gahbcfg, ahbcfg.d32);
  64992. +
  64993. + core_if->en_multiple_tx_fifo = core_if->hwcfg4.b.ded_fifo_en;
  64994. +
  64995. + core_if->pti_enh_enable = core_if->core_params->pti_enable != 0;
  64996. + core_if->multiproc_int_enable = core_if->core_params->mpi_enable;
  64997. + DWC_PRINTF("Periodic Transfer Interrupt Enhancement - %s\n",
  64998. + ((core_if->pti_enh_enable) ? "enabled" : "disabled"));
  64999. + DWC_PRINTF("Multiprocessor Interrupt Enhancement - %s\n",
  65000. + ((core_if->multiproc_int_enable) ? "enabled" : "disabled"));
  65001. +
  65002. + /*
  65003. + * Program the GUSBCFG register.
  65004. + */
  65005. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  65006. +
  65007. + switch (core_if->hwcfg2.b.op_mode) {
  65008. + case DWC_MODE_HNP_SRP_CAPABLE:
  65009. + usbcfg.b.hnpcap = (core_if->core_params->otg_cap ==
  65010. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  65011. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  65012. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  65013. + break;
  65014. +
  65015. + case DWC_MODE_SRP_ONLY_CAPABLE:
  65016. + usbcfg.b.hnpcap = 0;
  65017. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  65018. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  65019. + break;
  65020. +
  65021. + case DWC_MODE_NO_HNP_SRP_CAPABLE:
  65022. + usbcfg.b.hnpcap = 0;
  65023. + usbcfg.b.srpcap = 0;
  65024. + break;
  65025. +
  65026. + case DWC_MODE_SRP_CAPABLE_DEVICE:
  65027. + usbcfg.b.hnpcap = 0;
  65028. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  65029. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  65030. + break;
  65031. +
  65032. + case DWC_MODE_NO_SRP_CAPABLE_DEVICE:
  65033. + usbcfg.b.hnpcap = 0;
  65034. + usbcfg.b.srpcap = 0;
  65035. + break;
  65036. +
  65037. + case DWC_MODE_SRP_CAPABLE_HOST:
  65038. + usbcfg.b.hnpcap = 0;
  65039. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  65040. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  65041. + break;
  65042. +
  65043. + case DWC_MODE_NO_SRP_CAPABLE_HOST:
  65044. + usbcfg.b.hnpcap = 0;
  65045. + usbcfg.b.srpcap = 0;
  65046. + break;
  65047. + }
  65048. +
  65049. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  65050. +
  65051. +#ifdef CONFIG_USB_DWC_OTG_LPM
  65052. + if (core_if->core_params->lpm_enable) {
  65053. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  65054. +
  65055. + /* To enable LPM support set lpm_cap_en bit */
  65056. + lpmcfg.b.lpm_cap_en = 1;
  65057. +
  65058. + /* Make AppL1Res ACK */
  65059. + lpmcfg.b.appl_resp = 1;
  65060. +
  65061. + /* Retry 3 times */
  65062. + lpmcfg.b.retry_count = 3;
  65063. +
  65064. + DWC_MODIFY_REG32(&core_if->core_global_regs->glpmcfg,
  65065. + 0, lpmcfg.d32);
  65066. +
  65067. + }
  65068. +#endif
  65069. + if (core_if->core_params->ic_usb_cap) {
  65070. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  65071. + gusbcfg.b.ic_usb_cap = 1;
  65072. + DWC_MODIFY_REG32(&core_if->core_global_regs->gusbcfg,
  65073. + 0, gusbcfg.d32);
  65074. + }
  65075. + {
  65076. + gotgctl_data_t gotgctl = {.d32 = 0 };
  65077. + gotgctl.b.otgver = core_if->core_params->otg_ver;
  65078. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl, 0,
  65079. + gotgctl.d32);
  65080. + /* Set OTG version supported */
  65081. + core_if->otg_ver = core_if->core_params->otg_ver;
  65082. + DWC_PRINTF("OTG VER PARAM: %d, OTG VER FLAG: %d\n",
  65083. + core_if->core_params->otg_ver, core_if->otg_ver);
  65084. + }
  65085. +
  65086. +
  65087. + /* Enable common interrupts */
  65088. + dwc_otg_enable_common_interrupts(core_if);
  65089. +
  65090. + /* Do device or host intialization based on mode during PCD
  65091. + * and HCD initialization */
  65092. + if (dwc_otg_is_host_mode(core_if)) {
  65093. + DWC_DEBUGPL(DBG_ANY, "Host Mode\n");
  65094. + core_if->op_state = A_HOST;
  65095. + } else {
  65096. + DWC_DEBUGPL(DBG_ANY, "Device Mode\n");
  65097. + core_if->op_state = B_PERIPHERAL;
  65098. +#ifdef DWC_DEVICE_ONLY
  65099. + dwc_otg_core_dev_init(core_if);
  65100. +#endif
  65101. + }
  65102. +}
  65103. +
  65104. +/**
  65105. + * This function enables the Device mode interrupts.
  65106. + *
  65107. + * @param core_if Programming view of DWC_otg controller
  65108. + */
  65109. +void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * core_if)
  65110. +{
  65111. + gintmsk_data_t intr_mask = {.d32 = 0 };
  65112. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  65113. +
  65114. + DWC_DEBUGPL(DBG_CIL, "%s()\n", __func__);
  65115. +
  65116. + /* Disable all interrupts. */
  65117. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  65118. +
  65119. + /* Clear any pending interrupts */
  65120. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  65121. +
  65122. + /* Enable the common interrupts */
  65123. + dwc_otg_enable_common_interrupts(core_if);
  65124. +
  65125. + /* Enable interrupts */
  65126. + intr_mask.b.usbreset = 1;
  65127. + intr_mask.b.enumdone = 1;
  65128. + /* Disable Disconnect interrupt in Device mode */
  65129. + intr_mask.b.disconnect = 0;
  65130. +
  65131. + if (!core_if->multiproc_int_enable) {
  65132. + intr_mask.b.inepintr = 1;
  65133. + intr_mask.b.outepintr = 1;
  65134. + }
  65135. +
  65136. + intr_mask.b.erlysuspend = 1;
  65137. +
  65138. + if (core_if->en_multiple_tx_fifo == 0) {
  65139. + intr_mask.b.epmismatch = 1;
  65140. + }
  65141. +
  65142. + //intr_mask.b.incomplisoout = 1;
  65143. + intr_mask.b.incomplisoin = 1;
  65144. +
  65145. +/* Enable the ignore frame number for ISOC xfers - MAS */
  65146. +/* Disable to support high bandwith ISOC transfers - manukz */
  65147. +#if 0
  65148. +#ifdef DWC_UTE_PER_IO
  65149. + if (core_if->dma_enable) {
  65150. + if (core_if->dma_desc_enable) {
  65151. + dctl_data_t dctl1 = {.d32 = 0 };
  65152. + dctl1.b.ifrmnum = 1;
  65153. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  65154. + dctl, 0, dctl1.d32);
  65155. + DWC_DEBUG("----Enabled Ignore frame number (0x%08x)",
  65156. + DWC_READ_REG32(&core_if->dev_if->
  65157. + dev_global_regs->dctl));
  65158. + }
  65159. + }
  65160. +#endif
  65161. +#endif
  65162. +#ifdef DWC_EN_ISOC
  65163. + if (core_if->dma_enable) {
  65164. + if (core_if->dma_desc_enable == 0) {
  65165. + if (core_if->pti_enh_enable) {
  65166. + dctl_data_t dctl = {.d32 = 0 };
  65167. + dctl.b.ifrmnum = 1;
  65168. + DWC_MODIFY_REG32(&core_if->
  65169. + dev_if->dev_global_regs->dctl,
  65170. + 0, dctl.d32);
  65171. + } else {
  65172. + intr_mask.b.incomplisoin = 1;
  65173. + intr_mask.b.incomplisoout = 1;
  65174. + }
  65175. + }
  65176. + } else {
  65177. + intr_mask.b.incomplisoin = 1;
  65178. + intr_mask.b.incomplisoout = 1;
  65179. + }
  65180. +#endif /* DWC_EN_ISOC */
  65181. +
  65182. + /** @todo NGS: Should this be a module parameter? */
  65183. +#ifdef USE_PERIODIC_EP
  65184. + intr_mask.b.isooutdrop = 1;
  65185. + intr_mask.b.eopframe = 1;
  65186. + intr_mask.b.incomplisoin = 1;
  65187. + intr_mask.b.incomplisoout = 1;
  65188. +#endif
  65189. +
  65190. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  65191. +
  65192. + DWC_DEBUGPL(DBG_CIL, "%s() gintmsk=%0x\n", __func__,
  65193. + DWC_READ_REG32(&global_regs->gintmsk));
  65194. +}
  65195. +
  65196. +/**
  65197. + * This function initializes the DWC_otg controller registers for
  65198. + * device mode.
  65199. + *
  65200. + * @param core_if Programming view of DWC_otg controller
  65201. + *
  65202. + */
  65203. +void dwc_otg_core_dev_init(dwc_otg_core_if_t * core_if)
  65204. +{
  65205. + int i;
  65206. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  65207. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  65208. + dwc_otg_core_params_t *params = core_if->core_params;
  65209. + dcfg_data_t dcfg = {.d32 = 0 };
  65210. + depctl_data_t diepctl = {.d32 = 0 };
  65211. + grstctl_t resetctl = {.d32 = 0 };
  65212. + uint32_t rx_fifo_size;
  65213. + fifosize_data_t nptxfifosize;
  65214. + fifosize_data_t txfifosize;
  65215. + dthrctl_data_t dthrctl;
  65216. + fifosize_data_t ptxfifosize;
  65217. + uint16_t rxfsiz, nptxfsiz;
  65218. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  65219. + hwcfg3_data_t hwcfg3 = {.d32 = 0 };
  65220. +
  65221. + /* Restart the Phy Clock */
  65222. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  65223. +
  65224. + /* Device configuration register */
  65225. + init_devspd(core_if);
  65226. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  65227. + dcfg.b.descdma = (core_if->dma_desc_enable) ? 1 : 0;
  65228. + dcfg.b.perfrint = DWC_DCFG_FRAME_INTERVAL_80;
  65229. + /* Enable Device OUT NAK in case of DDMA mode*/
  65230. + if (core_if->core_params->dev_out_nak) {
  65231. + dcfg.b.endevoutnak = 1;
  65232. + }
  65233. +
  65234. + if (core_if->core_params->cont_on_bna) {
  65235. + dctl_data_t dctl = {.d32 = 0 };
  65236. + dctl.b.encontonbna = 1;
  65237. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  65238. + }
  65239. +
  65240. +
  65241. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  65242. +
  65243. + /* Configure data FIFO sizes */
  65244. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  65245. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  65246. + core_if->total_fifo_size);
  65247. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  65248. + params->dev_rx_fifo_size);
  65249. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  65250. + params->dev_nperio_tx_fifo_size);
  65251. +
  65252. + /* Rx FIFO */
  65253. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  65254. + DWC_READ_REG32(&global_regs->grxfsiz));
  65255. +
  65256. +#ifdef DWC_UTE_CFI
  65257. + core_if->pwron_rxfsiz = DWC_READ_REG32(&global_regs->grxfsiz);
  65258. + core_if->init_rxfsiz = params->dev_rx_fifo_size;
  65259. +#endif
  65260. + rx_fifo_size = params->dev_rx_fifo_size;
  65261. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  65262. +
  65263. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  65264. + DWC_READ_REG32(&global_regs->grxfsiz));
  65265. +
  65266. + /** Set Periodic Tx FIFO Mask all bits 0 */
  65267. + core_if->p_tx_msk = 0;
  65268. +
  65269. + /** Set Tx FIFO Mask all bits 0 */
  65270. + core_if->tx_msk = 0;
  65271. +
  65272. + if (core_if->en_multiple_tx_fifo == 0) {
  65273. + /* Non-periodic Tx FIFO */
  65274. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  65275. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  65276. +
  65277. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  65278. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  65279. +
  65280. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  65281. + nptxfifosize.d32);
  65282. +
  65283. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  65284. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  65285. +
  65286. + /**@todo NGS: Fix Periodic FIFO Sizing! */
  65287. + /*
  65288. + * Periodic Tx FIFOs These FIFOs are numbered from 1 to 15.
  65289. + * Indexes of the FIFO size module parameters in the
  65290. + * dev_perio_tx_fifo_size array and the FIFO size registers in
  65291. + * the dptxfsiz array run from 0 to 14.
  65292. + */
  65293. + /** @todo Finish debug of this */
  65294. + ptxfifosize.b.startaddr =
  65295. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  65296. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  65297. + ptxfifosize.b.depth =
  65298. + params->dev_perio_tx_fifo_size[i];
  65299. + DWC_DEBUGPL(DBG_CIL,
  65300. + "initial dtxfsiz[%d]=%08x\n", i,
  65301. + DWC_READ_REG32(&global_regs->dtxfsiz
  65302. + [i]));
  65303. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  65304. + ptxfifosize.d32);
  65305. + DWC_DEBUGPL(DBG_CIL, "new dtxfsiz[%d]=%08x\n",
  65306. + i,
  65307. + DWC_READ_REG32(&global_regs->dtxfsiz
  65308. + [i]));
  65309. + ptxfifosize.b.startaddr += ptxfifosize.b.depth;
  65310. + }
  65311. + } else {
  65312. + /*
  65313. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  65314. + * Indexes of the FIFO size module parameters in the
  65315. + * dev_tx_fifo_size array and the FIFO size registers in
  65316. + * the dtxfsiz array run from 0 to 14.
  65317. + */
  65318. +
  65319. + /* Non-periodic Tx FIFO */
  65320. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  65321. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  65322. +
  65323. +#ifdef DWC_UTE_CFI
  65324. + core_if->pwron_gnptxfsiz =
  65325. + (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  65326. + core_if->init_gnptxfsiz =
  65327. + params->dev_nperio_tx_fifo_size;
  65328. +#endif
  65329. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  65330. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  65331. +
  65332. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  65333. + nptxfifosize.d32);
  65334. +
  65335. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  65336. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  65337. +
  65338. + txfifosize.b.startaddr =
  65339. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  65340. +
  65341. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  65342. +
  65343. + txfifosize.b.depth =
  65344. + params->dev_tx_fifo_size[i];
  65345. +
  65346. + DWC_DEBUGPL(DBG_CIL,
  65347. + "initial dtxfsiz[%d]=%08x\n",
  65348. + i,
  65349. + DWC_READ_REG32(&global_regs->dtxfsiz
  65350. + [i]));
  65351. +
  65352. +#ifdef DWC_UTE_CFI
  65353. + core_if->pwron_txfsiz[i] =
  65354. + (DWC_READ_REG32
  65355. + (&global_regs->dtxfsiz[i]) >> 16);
  65356. + core_if->init_txfsiz[i] =
  65357. + params->dev_tx_fifo_size[i];
  65358. +#endif
  65359. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  65360. + txfifosize.d32);
  65361. +
  65362. + DWC_DEBUGPL(DBG_CIL,
  65363. + "new dtxfsiz[%d]=%08x\n",
  65364. + i,
  65365. + DWC_READ_REG32(&global_regs->dtxfsiz
  65366. + [i]));
  65367. +
  65368. + txfifosize.b.startaddr += txfifosize.b.depth;
  65369. + }
  65370. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  65371. + /* Calculating DFIFOCFG for Device mode to include RxFIFO and NPTXFIFO */
  65372. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  65373. + hwcfg3.d32 = DWC_READ_REG32(&global_regs->ghwcfg3);
  65374. + gdfifocfg.b.gdfifocfg = (DWC_READ_REG32(&global_regs->ghwcfg3) >> 16);
  65375. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  65376. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  65377. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  65378. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz;
  65379. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  65380. + }
  65381. + }
  65382. +
  65383. + /* Flush the FIFOs */
  65384. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  65385. + dwc_otg_flush_rx_fifo(core_if);
  65386. +
  65387. + /* Flush the Learning Queue. */
  65388. + resetctl.b.intknqflsh = 1;
  65389. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  65390. +
  65391. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  65392. + core_if->start_predict = 0;
  65393. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  65394. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  65395. + }
  65396. + core_if->nextep_seq[0] = 0;
  65397. + core_if->first_in_nextep_seq = 0;
  65398. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  65399. + diepctl.b.nextep = 0;
  65400. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  65401. +
  65402. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  65403. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  65404. + dcfg.b.epmscnt = 2;
  65405. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  65406. +
  65407. + DWC_DEBUGPL(DBG_CILV,"%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  65408. + __func__, core_if->first_in_nextep_seq);
  65409. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  65410. + DWC_DEBUGPL(DBG_CILV, "%2d ", core_if->nextep_seq[i]);
  65411. + }
  65412. + DWC_DEBUGPL(DBG_CILV,"\n");
  65413. + }
  65414. +
  65415. + /* Clear all pending Device Interrupts */
  65416. + /** @todo - if the condition needed to be checked
  65417. + * or in any case all pending interrutps should be cleared?
  65418. + */
  65419. + if (core_if->multiproc_int_enable) {
  65420. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  65421. + DWC_WRITE_REG32(&dev_if->
  65422. + dev_global_regs->diepeachintmsk[i], 0);
  65423. + }
  65424. + }
  65425. +
  65426. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  65427. + DWC_WRITE_REG32(&dev_if->
  65428. + dev_global_regs->doepeachintmsk[i], 0);
  65429. + }
  65430. +
  65431. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachint, 0xFFFFFFFF);
  65432. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk, 0);
  65433. + } else {
  65434. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, 0);
  65435. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, 0);
  65436. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daint, 0xFFFFFFFF);
  65437. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk, 0);
  65438. + }
  65439. +
  65440. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  65441. + depctl_data_t depctl;
  65442. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  65443. + if (depctl.b.epena) {
  65444. + depctl.d32 = 0;
  65445. + depctl.b.epdis = 1;
  65446. + depctl.b.snak = 1;
  65447. + } else {
  65448. + depctl.d32 = 0;
  65449. + }
  65450. +
  65451. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  65452. +
  65453. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, 0);
  65454. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, 0);
  65455. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepint, 0xFF);
  65456. + }
  65457. +
  65458. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  65459. + depctl_data_t depctl;
  65460. + depctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  65461. + if (depctl.b.epena) {
  65462. + dctl_data_t dctl = {.d32 = 0 };
  65463. + gintmsk_data_t gintsts = {.d32 = 0 };
  65464. + doepint_data_t doepint = {.d32 = 0 };
  65465. + dctl.b.sgoutnak = 1;
  65466. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  65467. + do {
  65468. + dwc_udelay(10);
  65469. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  65470. + } while (!gintsts.b.goutnakeff);
  65471. + gintsts.d32 = 0;
  65472. + gintsts.b.goutnakeff = 1;
  65473. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  65474. +
  65475. + depctl.d32 = 0;
  65476. + depctl.b.epdis = 1;
  65477. + depctl.b.snak = 1;
  65478. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  65479. + do {
  65480. + dwc_udelay(10);
  65481. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  65482. + out_ep_regs[i]->doepint);
  65483. + } while (!doepint.b.epdisabled);
  65484. +
  65485. + doepint.b.epdisabled = 1;
  65486. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepint, doepint.d32);
  65487. +
  65488. + dctl.d32 = 0;
  65489. + dctl.b.cgoutnak = 1;
  65490. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  65491. + } else {
  65492. + depctl.d32 = 0;
  65493. + }
  65494. +
  65495. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  65496. +
  65497. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doeptsiz, 0);
  65498. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepdma, 0);
  65499. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepint, 0xFF);
  65500. + }
  65501. +
  65502. + if (core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  65503. + dev_if->non_iso_tx_thr_en = params->thr_ctl & 0x1;
  65504. + dev_if->iso_tx_thr_en = (params->thr_ctl >> 1) & 0x1;
  65505. + dev_if->rx_thr_en = (params->thr_ctl >> 2) & 0x1;
  65506. +
  65507. + dev_if->rx_thr_length = params->rx_thr_length;
  65508. + dev_if->tx_thr_length = params->tx_thr_length;
  65509. +
  65510. + dev_if->setup_desc_index = 0;
  65511. +
  65512. + dthrctl.d32 = 0;
  65513. + dthrctl.b.non_iso_thr_en = dev_if->non_iso_tx_thr_en;
  65514. + dthrctl.b.iso_thr_en = dev_if->iso_tx_thr_en;
  65515. + dthrctl.b.tx_thr_len = dev_if->tx_thr_length;
  65516. + dthrctl.b.rx_thr_en = dev_if->rx_thr_en;
  65517. + dthrctl.b.rx_thr_len = dev_if->rx_thr_length;
  65518. + dthrctl.b.ahb_thr_ratio = params->ahb_thr_ratio;
  65519. +
  65520. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dtknqr3_dthrctl,
  65521. + dthrctl.d32);
  65522. +
  65523. + DWC_DEBUGPL(DBG_CIL,
  65524. + "Non ISO Tx Thr - %d\nISO Tx Thr - %d\nRx Thr - %d\nTx Thr Len - %d\nRx Thr Len - %d\n",
  65525. + dthrctl.b.non_iso_thr_en, dthrctl.b.iso_thr_en,
  65526. + dthrctl.b.rx_thr_en, dthrctl.b.tx_thr_len,
  65527. + dthrctl.b.rx_thr_len);
  65528. +
  65529. + }
  65530. +
  65531. + dwc_otg_enable_device_interrupts(core_if);
  65532. +
  65533. + {
  65534. + diepmsk_data_t msk = {.d32 = 0 };
  65535. + msk.b.txfifoundrn = 1;
  65536. + if (core_if->multiproc_int_enable) {
  65537. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->
  65538. + diepeachintmsk[0], msk.d32, msk.d32);
  65539. + } else {
  65540. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk,
  65541. + msk.d32, msk.d32);
  65542. + }
  65543. + }
  65544. +
  65545. + if (core_if->multiproc_int_enable) {
  65546. + /* Set NAK on Babble */
  65547. + dctl_data_t dctl = {.d32 = 0 };
  65548. + dctl.b.nakonbble = 1;
  65549. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  65550. + }
  65551. +
  65552. + if (core_if->snpsid >= OTG_CORE_REV_2_94a) {
  65553. + dctl_data_t dctl = {.d32 = 0 };
  65554. + dctl.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  65555. + dctl.b.sftdiscon = 0;
  65556. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl, dctl.d32);
  65557. + }
  65558. +}
  65559. +
  65560. +/**
  65561. + * This function enables the Host mode interrupts.
  65562. + *
  65563. + * @param core_if Programming view of DWC_otg controller
  65564. + */
  65565. +void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * core_if)
  65566. +{
  65567. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  65568. + gintmsk_data_t intr_mask = {.d32 = 0 };
  65569. +
  65570. + DWC_DEBUGPL(DBG_CIL, "%s(%p)\n", __func__, core_if);
  65571. +
  65572. + /* Disable all interrupts. */
  65573. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  65574. +
  65575. + /* Clear any pending interrupts. */
  65576. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  65577. +
  65578. + /* Enable the common interrupts */
  65579. + dwc_otg_enable_common_interrupts(core_if);
  65580. +
  65581. + /*
  65582. + * Enable host mode interrupts without disturbing common
  65583. + * interrupts.
  65584. + */
  65585. +
  65586. + intr_mask.b.disconnect = 1;
  65587. + intr_mask.b.portintr = 1;
  65588. + intr_mask.b.hcintr = 1;
  65589. +
  65590. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  65591. +}
  65592. +
  65593. +/**
  65594. + * This function disables the Host Mode interrupts.
  65595. + *
  65596. + * @param core_if Programming view of DWC_otg controller
  65597. + */
  65598. +void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * core_if)
  65599. +{
  65600. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  65601. + gintmsk_data_t intr_mask = {.d32 = 0 };
  65602. +
  65603. + DWC_DEBUGPL(DBG_CILV, "%s()\n", __func__);
  65604. +
  65605. + /*
  65606. + * Disable host mode interrupts without disturbing common
  65607. + * interrupts.
  65608. + */
  65609. + intr_mask.b.sofintr = 1;
  65610. + intr_mask.b.portintr = 1;
  65611. + intr_mask.b.hcintr = 1;
  65612. + intr_mask.b.ptxfempty = 1;
  65613. + intr_mask.b.nptxfempty = 1;
  65614. +
  65615. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, 0);
  65616. +}
  65617. +
  65618. +/**
  65619. + * This function initializes the DWC_otg controller registers for
  65620. + * host mode.
  65621. + *
  65622. + * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
  65623. + * request queues. Host channels are reset to ensure that they are ready for
  65624. + * performing transfers.
  65625. + *
  65626. + * @param core_if Programming view of DWC_otg controller
  65627. + *
  65628. + */
  65629. +void dwc_otg_core_host_init(dwc_otg_core_if_t * core_if)
  65630. +{
  65631. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  65632. + dwc_otg_host_if_t *host_if = core_if->host_if;
  65633. + dwc_otg_core_params_t *params = core_if->core_params;
  65634. + hprt0_data_t hprt0 = {.d32 = 0 };
  65635. + fifosize_data_t nptxfifosize;
  65636. + fifosize_data_t ptxfifosize;
  65637. + uint16_t rxfsiz, nptxfsiz, hptxfsiz;
  65638. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  65639. + int i;
  65640. + hcchar_data_t hcchar;
  65641. + hcfg_data_t hcfg;
  65642. + hfir_data_t hfir;
  65643. + dwc_otg_hc_regs_t *hc_regs;
  65644. + int num_channels;
  65645. + gotgctl_data_t gotgctl = {.d32 = 0 };
  65646. +
  65647. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  65648. +
  65649. + /* Restart the Phy Clock */
  65650. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  65651. +
  65652. + /* Initialize Host Configuration Register */
  65653. + init_fslspclksel(core_if);
  65654. + if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  65655. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  65656. + hcfg.b.fslssupp = 1;
  65657. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  65658. +
  65659. + }
  65660. +
  65661. + /* This bit allows dynamic reloading of the HFIR register
  65662. + * during runtime. This bit needs to be programmed during
  65663. + * initial configuration and its value must not be changed
  65664. + * during runtime.*/
  65665. + if (core_if->core_params->reload_ctl == 1) {
  65666. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  65667. + hfir.b.hfirrldctrl = 1;
  65668. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  65669. + }
  65670. +
  65671. + if (core_if->core_params->dma_desc_enable) {
  65672. + uint8_t op_mode = core_if->hwcfg2.b.op_mode;
  65673. + if (!
  65674. + (core_if->hwcfg4.b.desc_dma
  65675. + && (core_if->snpsid >= OTG_CORE_REV_2_90a)
  65676. + && ((op_mode == DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  65677. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  65678. + || (op_mode ==
  65679. + DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG)
  65680. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)
  65681. + || (op_mode ==
  65682. + DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST)))) {
  65683. +
  65684. + DWC_ERROR("Host can't operate in Descriptor DMA mode.\n"
  65685. + "Either core version is below 2.90a or "
  65686. + "GHWCFG2, GHWCFG4 registers' values do not allow Descriptor DMA in host mode.\n"
  65687. + "To run the driver in Buffer DMA host mode set dma_desc_enable "
  65688. + "module parameter to 0.\n");
  65689. + return;
  65690. + }
  65691. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  65692. + hcfg.b.descdma = 1;
  65693. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  65694. + }
  65695. +
  65696. + /* Configure data FIFO sizes */
  65697. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  65698. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  65699. + core_if->total_fifo_size);
  65700. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  65701. + params->host_rx_fifo_size);
  65702. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  65703. + params->host_nperio_tx_fifo_size);
  65704. + DWC_DEBUGPL(DBG_CIL, "P Tx FIFO Size=%d\n",
  65705. + params->host_perio_tx_fifo_size);
  65706. +
  65707. + /* Rx FIFO */
  65708. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  65709. + DWC_READ_REG32(&global_regs->grxfsiz));
  65710. + DWC_WRITE_REG32(&global_regs->grxfsiz,
  65711. + params->host_rx_fifo_size);
  65712. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  65713. + DWC_READ_REG32(&global_regs->grxfsiz));
  65714. +
  65715. + /* Non-periodic Tx FIFO */
  65716. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  65717. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  65718. + nptxfifosize.b.depth = params->host_nperio_tx_fifo_size;
  65719. + nptxfifosize.b.startaddr = params->host_rx_fifo_size;
  65720. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  65721. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  65722. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  65723. +
  65724. + /* Periodic Tx FIFO */
  65725. + DWC_DEBUGPL(DBG_CIL, "initial hptxfsiz=%08x\n",
  65726. + DWC_READ_REG32(&global_regs->hptxfsiz));
  65727. + ptxfifosize.b.depth = params->host_perio_tx_fifo_size;
  65728. + ptxfifosize.b.startaddr =
  65729. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  65730. + DWC_WRITE_REG32(&global_regs->hptxfsiz, ptxfifosize.d32);
  65731. + DWC_DEBUGPL(DBG_CIL, "new hptxfsiz=%08x\n",
  65732. + DWC_READ_REG32(&global_regs->hptxfsiz));
  65733. +
  65734. + if (core_if->en_multiple_tx_fifo
  65735. + && core_if->snpsid <= OTG_CORE_REV_2_94a) {
  65736. + /* Global DFIFOCFG calculation for Host mode - include RxFIFO, NPTXFIFO and HPTXFIFO */
  65737. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  65738. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  65739. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  65740. + hptxfsiz = (DWC_READ_REG32(&global_regs->hptxfsiz) >> 16);
  65741. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz + hptxfsiz;
  65742. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  65743. + }
  65744. + }
  65745. +
  65746. + /* TODO - check this */
  65747. + /* Clear Host Set HNP Enable in the OTG Control Register */
  65748. + gotgctl.b.hstsethnpen = 1;
  65749. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  65750. + /* Make sure the FIFOs are flushed. */
  65751. + dwc_otg_flush_tx_fifo(core_if, 0x10 /* all TX FIFOs */ );
  65752. + dwc_otg_flush_rx_fifo(core_if);
  65753. +
  65754. + /* Clear Host Set HNP Enable in the OTG Control Register */
  65755. + gotgctl.b.hstsethnpen = 1;
  65756. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  65757. +
  65758. + if (!core_if->core_params->dma_desc_enable) {
  65759. + /* Flush out any leftover queued requests. */
  65760. + num_channels = core_if->core_params->host_channels;
  65761. +
  65762. + for (i = 0; i < num_channels; i++) {
  65763. + hc_regs = core_if->host_if->hc_regs[i];
  65764. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  65765. + hcchar.b.chen = 0;
  65766. + hcchar.b.chdis = 1;
  65767. + hcchar.b.epdir = 0;
  65768. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  65769. + }
  65770. +
  65771. + /* Halt all channels to put them into a known state. */
  65772. + for (i = 0; i < num_channels; i++) {
  65773. + int count = 0;
  65774. + hc_regs = core_if->host_if->hc_regs[i];
  65775. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  65776. + hcchar.b.chen = 1;
  65777. + hcchar.b.chdis = 1;
  65778. + hcchar.b.epdir = 0;
  65779. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  65780. + DWC_DEBUGPL(DBG_HCDV, "%s: Halt channel %d regs %p\n", __func__, i, hc_regs);
  65781. + do {
  65782. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  65783. + if (++count > 1000) {
  65784. + DWC_ERROR
  65785. + ("%s: Unable to clear halt on channel %d (timeout HCCHAR 0x%X @%p)\n",
  65786. + __func__, i, hcchar.d32, &hc_regs->hcchar);
  65787. + break;
  65788. + }
  65789. + dwc_udelay(1);
  65790. + } while (hcchar.b.chen);
  65791. + }
  65792. + }
  65793. +
  65794. + /* Turn on the vbus power. */
  65795. + DWC_PRINTF("Init: Port Power? op_state=%d\n", core_if->op_state);
  65796. + if (core_if->op_state == A_HOST) {
  65797. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  65798. + DWC_PRINTF("Init: Power Port (%d)\n", hprt0.b.prtpwr);
  65799. + if (hprt0.b.prtpwr == 0) {
  65800. + hprt0.b.prtpwr = 1;
  65801. + DWC_WRITE_REG32(host_if->hprt0, hprt0.d32);
  65802. + }
  65803. + }
  65804. +
  65805. + dwc_otg_enable_host_interrupts(core_if);
  65806. +}
  65807. +
  65808. +/**
  65809. + * Prepares a host channel for transferring packets to/from a specific
  65810. + * endpoint. The HCCHARn register is set up with the characteristics specified
  65811. + * in _hc. Host channel interrupts that may need to be serviced while this
  65812. + * transfer is in progress are enabled.
  65813. + *
  65814. + * @param core_if Programming view of DWC_otg controller
  65815. + * @param hc Information needed to initialize the host channel
  65816. + */
  65817. +void dwc_otg_hc_init(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  65818. +{
  65819. + uint32_t intr_enable;
  65820. + hcintmsk_data_t hc_intr_mask;
  65821. + gintmsk_data_t gintmsk = {.d32 = 0 };
  65822. + hcchar_data_t hcchar;
  65823. + hcsplt_data_t hcsplt;
  65824. +
  65825. + uint8_t hc_num = hc->hc_num;
  65826. + dwc_otg_host_if_t *host_if = core_if->host_if;
  65827. + dwc_otg_hc_regs_t *hc_regs = host_if->hc_regs[hc_num];
  65828. +
  65829. + /* Clear old interrupt conditions for this host channel. */
  65830. + hc_intr_mask.d32 = 0xFFFFFFFF;
  65831. + hc_intr_mask.b.reserved14_31 = 0;
  65832. + DWC_WRITE_REG32(&hc_regs->hcint, hc_intr_mask.d32);
  65833. +
  65834. + /* Enable channel interrupts required for this transfer. */
  65835. + hc_intr_mask.d32 = 0;
  65836. + hc_intr_mask.b.chhltd = 1;
  65837. + if (core_if->dma_enable) {
  65838. + /* For Descriptor DMA mode core halts the channel on AHB error. Interrupt is not required */
  65839. + if (!core_if->dma_desc_enable)
  65840. + hc_intr_mask.b.ahberr = 1;
  65841. + else {
  65842. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  65843. + hc_intr_mask.b.xfercompl = 1;
  65844. + }
  65845. +
  65846. + if (hc->error_state && !hc->do_split &&
  65847. + hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  65848. + hc_intr_mask.b.ack = 1;
  65849. + if (hc->ep_is_in) {
  65850. + hc_intr_mask.b.datatglerr = 1;
  65851. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  65852. + hc_intr_mask.b.nak = 1;
  65853. + }
  65854. + }
  65855. + }
  65856. + } else {
  65857. + switch (hc->ep_type) {
  65858. + case DWC_OTG_EP_TYPE_CONTROL:
  65859. + case DWC_OTG_EP_TYPE_BULK:
  65860. + hc_intr_mask.b.xfercompl = 1;
  65861. + hc_intr_mask.b.stall = 1;
  65862. + hc_intr_mask.b.xacterr = 1;
  65863. + hc_intr_mask.b.datatglerr = 1;
  65864. + if (hc->ep_is_in) {
  65865. + hc_intr_mask.b.bblerr = 1;
  65866. + } else {
  65867. + hc_intr_mask.b.nak = 1;
  65868. + hc_intr_mask.b.nyet = 1;
  65869. + if (hc->do_ping) {
  65870. + hc_intr_mask.b.ack = 1;
  65871. + }
  65872. + }
  65873. +
  65874. + if (hc->do_split) {
  65875. + hc_intr_mask.b.nak = 1;
  65876. + if (hc->complete_split) {
  65877. + hc_intr_mask.b.nyet = 1;
  65878. + } else {
  65879. + hc_intr_mask.b.ack = 1;
  65880. + }
  65881. + }
  65882. +
  65883. + if (hc->error_state) {
  65884. + hc_intr_mask.b.ack = 1;
  65885. + }
  65886. + break;
  65887. + case DWC_OTG_EP_TYPE_INTR:
  65888. + hc_intr_mask.b.xfercompl = 1;
  65889. + hc_intr_mask.b.nak = 1;
  65890. + hc_intr_mask.b.stall = 1;
  65891. + hc_intr_mask.b.xacterr = 1;
  65892. + hc_intr_mask.b.datatglerr = 1;
  65893. + hc_intr_mask.b.frmovrun = 1;
  65894. +
  65895. + if (hc->ep_is_in) {
  65896. + hc_intr_mask.b.bblerr = 1;
  65897. + }
  65898. + if (hc->error_state) {
  65899. + hc_intr_mask.b.ack = 1;
  65900. + }
  65901. + if (hc->do_split) {
  65902. + if (hc->complete_split) {
  65903. + hc_intr_mask.b.nyet = 1;
  65904. + } else {
  65905. + hc_intr_mask.b.ack = 1;
  65906. + }
  65907. + }
  65908. + break;
  65909. + case DWC_OTG_EP_TYPE_ISOC:
  65910. + hc_intr_mask.b.xfercompl = 1;
  65911. + hc_intr_mask.b.frmovrun = 1;
  65912. + hc_intr_mask.b.ack = 1;
  65913. +
  65914. + if (hc->ep_is_in) {
  65915. + hc_intr_mask.b.xacterr = 1;
  65916. + hc_intr_mask.b.bblerr = 1;
  65917. + }
  65918. + break;
  65919. + }
  65920. + }
  65921. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hc_intr_mask.d32);
  65922. +
  65923. + /* Enable the top level host channel interrupt. */
  65924. + intr_enable = (1 << hc_num);
  65925. + DWC_MODIFY_REG32(&host_if->host_global_regs->haintmsk, 0, intr_enable);
  65926. +
  65927. + /* Make sure host channel interrupts are enabled. */
  65928. + gintmsk.b.hcintr = 1;
  65929. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  65930. +
  65931. + /*
  65932. + * Program the HCCHARn register with the endpoint characteristics for
  65933. + * the current transfer.
  65934. + */
  65935. + hcchar.d32 = 0;
  65936. + hcchar.b.devaddr = hc->dev_addr;
  65937. + hcchar.b.epnum = hc->ep_num;
  65938. + hcchar.b.epdir = hc->ep_is_in;
  65939. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  65940. + hcchar.b.eptype = hc->ep_type;
  65941. + hcchar.b.mps = hc->max_packet;
  65942. +
  65943. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcchar, hcchar.d32);
  65944. +
  65945. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d, Dev Addr %d, EP #%d\n",
  65946. + __func__, hc->hc_num, hcchar.b.devaddr, hcchar.b.epnum);
  65947. + DWC_DEBUGPL(DBG_HCDV, " Is In %d, Is Low Speed %d, EP Type %d, "
  65948. + "Max Pkt %d, Multi Cnt %d\n",
  65949. + hcchar.b.epdir, hcchar.b.lspddev, hcchar.b.eptype,
  65950. + hcchar.b.mps, hcchar.b.multicnt);
  65951. +
  65952. + /*
  65953. + * Program the HCSPLIT register for SPLITs
  65954. + */
  65955. + hcsplt.d32 = 0;
  65956. + if (hc->do_split) {
  65957. + DWC_DEBUGPL(DBG_HCDV, "Programming HC %d with split --> %s\n",
  65958. + hc->hc_num,
  65959. + hc->complete_split ? "CSPLIT" : "SSPLIT");
  65960. + hcsplt.b.compsplt = hc->complete_split;
  65961. + hcsplt.b.xactpos = hc->xact_pos;
  65962. + hcsplt.b.hubaddr = hc->hub_addr;
  65963. + hcsplt.b.prtaddr = hc->port_addr;
  65964. + DWC_DEBUGPL(DBG_HCDV, "\t comp split %d\n", hc->complete_split);
  65965. + DWC_DEBUGPL(DBG_HCDV, "\t xact pos %d\n", hc->xact_pos);
  65966. + DWC_DEBUGPL(DBG_HCDV, "\t hub addr %d\n", hc->hub_addr);
  65967. + DWC_DEBUGPL(DBG_HCDV, "\t port addr %d\n", hc->port_addr);
  65968. + DWC_DEBUGPL(DBG_HCDV, "\t is_in %d\n", hc->ep_is_in);
  65969. + DWC_DEBUGPL(DBG_HCDV, "\t Max Pkt: %d\n", hcchar.b.mps);
  65970. + DWC_DEBUGPL(DBG_HCDV, "\t xferlen: %d\n", hc->xfer_len);
  65971. + }
  65972. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcsplt, hcsplt.d32);
  65973. +
  65974. +}
  65975. +
  65976. +/**
  65977. + * Attempts to halt a host channel. This function should only be called in
  65978. + * Slave mode or to abort a transfer in either Slave mode or DMA mode. Under
  65979. + * normal circumstances in DMA mode, the controller halts the channel when the
  65980. + * transfer is complete or a condition occurs that requires application
  65981. + * intervention.
  65982. + *
  65983. + * In slave mode, checks for a free request queue entry, then sets the Channel
  65984. + * Enable and Channel Disable bits of the Host Channel Characteristics
  65985. + * register of the specified channel to intiate the halt. If there is no free
  65986. + * request queue entry, sets only the Channel Disable bit of the HCCHARn
  65987. + * register to flush requests for this channel. In the latter case, sets a
  65988. + * flag to indicate that the host channel needs to be halted when a request
  65989. + * queue slot is open.
  65990. + *
  65991. + * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
  65992. + * HCCHARn register. The controller ensures there is space in the request
  65993. + * queue before submitting the halt request.
  65994. + *
  65995. + * Some time may elapse before the core flushes any posted requests for this
  65996. + * host channel and halts. The Channel Halted interrupt handler completes the
  65997. + * deactivation of the host channel.
  65998. + *
  65999. + * @param core_if Controller register interface.
  66000. + * @param hc Host channel to halt.
  66001. + * @param halt_status Reason for halting the channel.
  66002. + */
  66003. +void dwc_otg_hc_halt(dwc_otg_core_if_t * core_if,
  66004. + dwc_hc_t * hc, dwc_otg_halt_status_e halt_status)
  66005. +{
  66006. + gnptxsts_data_t nptxsts;
  66007. + hptxsts_data_t hptxsts;
  66008. + hcchar_data_t hcchar;
  66009. + dwc_otg_hc_regs_t *hc_regs;
  66010. + dwc_otg_core_global_regs_t *global_regs;
  66011. + dwc_otg_host_global_regs_t *host_global_regs;
  66012. +
  66013. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  66014. + global_regs = core_if->core_global_regs;
  66015. + host_global_regs = core_if->host_if->host_global_regs;
  66016. +
  66017. + DWC_ASSERT(!(halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS),
  66018. + "halt_status = %d\n", halt_status);
  66019. +
  66020. + if (halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  66021. + halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  66022. + /*
  66023. + * Disable all channel interrupts except Ch Halted. The QTD
  66024. + * and QH state associated with this transfer has been cleared
  66025. + * (in the case of URB_DEQUEUE), so the channel needs to be
  66026. + * shut down carefully to prevent crashes.
  66027. + */
  66028. + hcintmsk_data_t hcintmsk;
  66029. + hcintmsk.d32 = 0;
  66030. + hcintmsk.b.chhltd = 1;
  66031. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hcintmsk.d32);
  66032. +
  66033. + /*
  66034. + * Make sure no other interrupts besides halt are currently
  66035. + * pending. Handling another interrupt could cause a crash due
  66036. + * to the QTD and QH state.
  66037. + */
  66038. + DWC_WRITE_REG32(&hc_regs->hcint, ~hcintmsk.d32);
  66039. +
  66040. + /*
  66041. + * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
  66042. + * even if the channel was already halted for some other
  66043. + * reason.
  66044. + */
  66045. + hc->halt_status = halt_status;
  66046. +
  66047. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  66048. + if (hcchar.b.chen == 0) {
  66049. + /*
  66050. + * The channel is either already halted or it hasn't
  66051. + * started yet. In DMA mode, the transfer may halt if
  66052. + * it finishes normally or a condition occurs that
  66053. + * requires driver intervention. Don't want to halt
  66054. + * the channel again. In either Slave or DMA mode,
  66055. + * it's possible that the transfer has been assigned
  66056. + * to a channel, but not started yet when an URB is
  66057. + * dequeued. Don't want to halt a channel that hasn't
  66058. + * started yet.
  66059. + */
  66060. + return;
  66061. + }
  66062. + }
  66063. + if (hc->halt_pending) {
  66064. + /*
  66065. + * A halt has already been issued for this channel. This might
  66066. + * happen when a transfer is aborted by a higher level in
  66067. + * the stack.
  66068. + */
  66069. +#ifdef DEBUG
  66070. + DWC_PRINTF
  66071. + ("*** %s: Channel %d, _hc->halt_pending already set ***\n",
  66072. + __func__, hc->hc_num);
  66073. +
  66074. +#endif
  66075. + return;
  66076. + }
  66077. +
  66078. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  66079. +
  66080. + /* No need to set the bit in DDMA for disabling the channel */
  66081. + //TODO check it everywhere channel is disabled
  66082. + if (!core_if->core_params->dma_desc_enable)
  66083. + hcchar.b.chen = 1;
  66084. + hcchar.b.chdis = 1;
  66085. +
  66086. + if (!core_if->dma_enable) {
  66087. + /* Check for space in the request queue to issue the halt. */
  66088. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  66089. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  66090. + nptxsts.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  66091. + if (nptxsts.b.nptxqspcavail == 0) {
  66092. + hcchar.b.chen = 0;
  66093. + }
  66094. + } else {
  66095. + hptxsts.d32 =
  66096. + DWC_READ_REG32(&host_global_regs->hptxsts);
  66097. + if ((hptxsts.b.ptxqspcavail == 0)
  66098. + || (core_if->queuing_high_bandwidth)) {
  66099. + hcchar.b.chen = 0;
  66100. + }
  66101. + }
  66102. + }
  66103. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  66104. +
  66105. + hc->halt_status = halt_status;
  66106. +
  66107. + if (hcchar.b.chen) {
  66108. + hc->halt_pending = 1;
  66109. + hc->halt_on_queue = 0;
  66110. + } else {
  66111. + hc->halt_on_queue = 1;
  66112. + }
  66113. +
  66114. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  66115. + DWC_DEBUGPL(DBG_HCDV, " hcchar: 0x%08x\n", hcchar.d32);
  66116. + DWC_DEBUGPL(DBG_HCDV, " halt_pending: %d\n", hc->halt_pending);
  66117. + DWC_DEBUGPL(DBG_HCDV, " halt_on_queue: %d\n", hc->halt_on_queue);
  66118. + DWC_DEBUGPL(DBG_HCDV, " halt_status: %d\n", hc->halt_status);
  66119. +
  66120. + return;
  66121. +}
  66122. +
  66123. +/**
  66124. + * Clears the transfer state for a host channel. This function is normally
  66125. + * called after a transfer is done and the host channel is being released.
  66126. + *
  66127. + * @param core_if Programming view of DWC_otg controller.
  66128. + * @param hc Identifies the host channel to clean up.
  66129. + */
  66130. +void dwc_otg_hc_cleanup(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  66131. +{
  66132. + dwc_otg_hc_regs_t *hc_regs;
  66133. +
  66134. + hc->xfer_started = 0;
  66135. +
  66136. + /*
  66137. + * Clear channel interrupt enables and any unhandled channel interrupt
  66138. + * conditions.
  66139. + */
  66140. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  66141. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0);
  66142. + DWC_WRITE_REG32(&hc_regs->hcint, 0xFFFFFFFF);
  66143. +#ifdef DEBUG
  66144. + DWC_TIMER_CANCEL(core_if->hc_xfer_timer[hc->hc_num]);
  66145. +#endif
  66146. +}
  66147. +
  66148. +/**
  66149. + * Sets the channel property that indicates in which frame a periodic transfer
  66150. + * should occur. This is always set to the _next_ frame. This function has no
  66151. + * effect on non-periodic transfers.
  66152. + *
  66153. + * @param core_if Programming view of DWC_otg controller.
  66154. + * @param hc Identifies the host channel to set up and its properties.
  66155. + * @param hcchar Current value of the HCCHAR register for the specified host
  66156. + * channel.
  66157. + */
  66158. +static inline void hc_set_even_odd_frame(dwc_otg_core_if_t * core_if,
  66159. + dwc_hc_t * hc, hcchar_data_t * hcchar)
  66160. +{
  66161. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  66162. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  66163. + hfnum_data_t hfnum;
  66164. + hfnum.d32 =
  66165. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfnum);
  66166. +
  66167. + /* 1 if _next_ frame is odd, 0 if it's even */
  66168. + hcchar->b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
  66169. +#ifdef DEBUG
  66170. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR && hc->do_split
  66171. + && !hc->complete_split) {
  66172. + switch (hfnum.b.frnum & 0x7) {
  66173. + case 7:
  66174. + core_if->hfnum_7_samples++;
  66175. + core_if->hfnum_7_frrem_accum += hfnum.b.frrem;
  66176. + break;
  66177. + case 0:
  66178. + core_if->hfnum_0_samples++;
  66179. + core_if->hfnum_0_frrem_accum += hfnum.b.frrem;
  66180. + break;
  66181. + default:
  66182. + core_if->hfnum_other_samples++;
  66183. + core_if->hfnum_other_frrem_accum +=
  66184. + hfnum.b.frrem;
  66185. + break;
  66186. + }
  66187. + }
  66188. +#endif
  66189. + }
  66190. +}
  66191. +
  66192. +#ifdef DEBUG
  66193. +void hc_xfer_timeout(void *ptr)
  66194. +{
  66195. + hc_xfer_info_t *xfer_info = NULL;
  66196. + int hc_num = 0;
  66197. +
  66198. + if (ptr)
  66199. + xfer_info = (hc_xfer_info_t *) ptr;
  66200. +
  66201. + if (!xfer_info->hc) {
  66202. + DWC_ERROR("xfer_info->hc = %p\n", xfer_info->hc);
  66203. + return;
  66204. + }
  66205. +
  66206. + hc_num = xfer_info->hc->hc_num;
  66207. + DWC_WARN("%s: timeout on channel %d\n", __func__, hc_num);
  66208. + DWC_WARN(" start_hcchar_val 0x%08x\n",
  66209. + xfer_info->core_if->start_hcchar_val[hc_num]);
  66210. +}
  66211. +#endif
  66212. +
  66213. +void ep_xfer_timeout(void *ptr)
  66214. +{
  66215. + ep_xfer_info_t *xfer_info = NULL;
  66216. + int ep_num = 0;
  66217. + dctl_data_t dctl = {.d32 = 0 };
  66218. + gintsts_data_t gintsts = {.d32 = 0 };
  66219. + gintmsk_data_t gintmsk = {.d32 = 0 };
  66220. +
  66221. + if (ptr)
  66222. + xfer_info = (ep_xfer_info_t *) ptr;
  66223. +
  66224. + if (!xfer_info->ep) {
  66225. + DWC_ERROR("xfer_info->ep = %p\n", xfer_info->ep);
  66226. + return;
  66227. + }
  66228. +
  66229. + ep_num = xfer_info->ep->num;
  66230. + DWC_WARN("%s: timeout on endpoit %d\n", __func__, ep_num);
  66231. + /* Put the sate to 2 as it was time outed */
  66232. + xfer_info->state = 2;
  66233. +
  66234. + dctl.d32 =
  66235. + DWC_READ_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl);
  66236. + gintsts.d32 =
  66237. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintsts);
  66238. + gintmsk.d32 =
  66239. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintmsk);
  66240. +
  66241. + if (!gintmsk.b.goutnakeff) {
  66242. + /* Unmask it */
  66243. + gintmsk.b.goutnakeff = 1;
  66244. + DWC_WRITE_REG32(&xfer_info->core_if->core_global_regs->gintmsk,
  66245. + gintmsk.d32);
  66246. +
  66247. + }
  66248. +
  66249. + if (!gintsts.b.goutnakeff) {
  66250. + dctl.b.sgoutnak = 1;
  66251. + }
  66252. + DWC_WRITE_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl,
  66253. + dctl.d32);
  66254. +
  66255. +}
  66256. +
  66257. +void set_pid_isoc(dwc_hc_t * hc)
  66258. +{
  66259. + /* Set up the initial PID for the transfer. */
  66260. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  66261. + if (hc->ep_is_in) {
  66262. + if (hc->multi_count == 1) {
  66263. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  66264. + } else if (hc->multi_count == 2) {
  66265. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  66266. + } else {
  66267. + hc->data_pid_start = DWC_OTG_HC_PID_DATA2;
  66268. + }
  66269. + } else {
  66270. + if (hc->multi_count == 1) {
  66271. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  66272. + } else {
  66273. + hc->data_pid_start = DWC_OTG_HC_PID_MDATA;
  66274. + }
  66275. + }
  66276. + } else {
  66277. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  66278. + }
  66279. +}
  66280. +
  66281. +/**
  66282. + * This function does the setup for a data transfer for a host channel and
  66283. + * starts the transfer. May be called in either Slave mode or DMA mode. In
  66284. + * Slave mode, the caller must ensure that there is sufficient space in the
  66285. + * request queue and Tx Data FIFO.
  66286. + *
  66287. + * For an OUT transfer in Slave mode, it loads a data packet into the
  66288. + * appropriate FIFO. If necessary, additional data packets will be loaded in
  66289. + * the Host ISR.
  66290. + *
  66291. + * For an IN transfer in Slave mode, a data packet is requested. The data
  66292. + * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
  66293. + * additional data packets are requested in the Host ISR.
  66294. + *
  66295. + * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
  66296. + * register along with a packet count of 1 and the channel is enabled. This
  66297. + * causes a single PING transaction to occur. Other fields in HCTSIZ are
  66298. + * simply set to 0 since no data transfer occurs in this case.
  66299. + *
  66300. + * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
  66301. + * all the information required to perform the subsequent data transfer. In
  66302. + * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
  66303. + * controller performs the entire PING protocol, then starts the data
  66304. + * transfer.
  66305. + *
  66306. + * @param core_if Programming view of DWC_otg controller.
  66307. + * @param hc Information needed to initialize the host channel. The xfer_len
  66308. + * value may be reduced to accommodate the max widths of the XferSize and
  66309. + * PktCnt fields in the HCTSIZn register. The multi_count value may be changed
  66310. + * to reflect the final xfer_len value.
  66311. + */
  66312. +void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  66313. +{
  66314. + hcchar_data_t hcchar;
  66315. + hctsiz_data_t hctsiz;
  66316. + uint16_t num_packets;
  66317. + uint32_t max_hc_xfer_size = core_if->core_params->max_transfer_size;
  66318. + uint16_t max_hc_pkt_count = core_if->core_params->max_packet_count;
  66319. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  66320. +
  66321. + hctsiz.d32 = 0;
  66322. +
  66323. + if (hc->do_ping) {
  66324. + if (!core_if->dma_enable) {
  66325. + dwc_otg_hc_do_ping(core_if, hc);
  66326. + hc->xfer_started = 1;
  66327. + return;
  66328. + } else {
  66329. + hctsiz.b.dopng = 1;
  66330. + }
  66331. + }
  66332. +
  66333. + if (hc->do_split) {
  66334. + num_packets = 1;
  66335. +
  66336. + if (hc->complete_split && !hc->ep_is_in) {
  66337. + /* For CSPLIT OUT Transfer, set the size to 0 so the
  66338. + * core doesn't expect any data written to the FIFO */
  66339. + hc->xfer_len = 0;
  66340. + } else if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
  66341. + hc->xfer_len = hc->max_packet;
  66342. + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
  66343. + hc->xfer_len = 188;
  66344. + }
  66345. +
  66346. + hctsiz.b.xfersize = hc->xfer_len;
  66347. + } else {
  66348. + /*
  66349. + * Ensure that the transfer length and packet count will fit
  66350. + * in the widths allocated for them in the HCTSIZn register.
  66351. + */
  66352. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  66353. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  66354. + /*
  66355. + * Make sure the transfer size is no larger than one
  66356. + * (micro)frame's worth of data. (A check was done
  66357. + * when the periodic transfer was accepted to ensure
  66358. + * that a (micro)frame's worth of data can be
  66359. + * programmed into a channel.)
  66360. + */
  66361. + uint32_t max_periodic_len =
  66362. + hc->multi_count * hc->max_packet;
  66363. + if (hc->xfer_len > max_periodic_len) {
  66364. + hc->xfer_len = max_periodic_len;
  66365. + } else {
  66366. + }
  66367. + } else if (hc->xfer_len > max_hc_xfer_size) {
  66368. + /* Make sure that xfer_len is a multiple of max packet size. */
  66369. + hc->xfer_len = max_hc_xfer_size - hc->max_packet + 1;
  66370. + }
  66371. +
  66372. + if (hc->xfer_len > 0) {
  66373. + num_packets =
  66374. + (hc->xfer_len + hc->max_packet -
  66375. + 1) / hc->max_packet;
  66376. + if (num_packets > max_hc_pkt_count) {
  66377. + num_packets = max_hc_pkt_count;
  66378. + hc->xfer_len = num_packets * hc->max_packet;
  66379. + }
  66380. + } else {
  66381. + /* Need 1 packet for transfer length of 0. */
  66382. + num_packets = 1;
  66383. + }
  66384. +
  66385. + if (hc->ep_is_in) {
  66386. + /* Always program an integral # of max packets for IN transfers. */
  66387. + hc->xfer_len = num_packets * hc->max_packet;
  66388. + }
  66389. +
  66390. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  66391. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  66392. + /*
  66393. + * Make sure that the multi_count field matches the
  66394. + * actual transfer length.
  66395. + */
  66396. + hc->multi_count = num_packets;
  66397. + }
  66398. +
  66399. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  66400. + set_pid_isoc(hc);
  66401. +
  66402. + hctsiz.b.xfersize = hc->xfer_len;
  66403. + }
  66404. +
  66405. + hc->start_pkt_count = num_packets;
  66406. + hctsiz.b.pktcnt = num_packets;
  66407. + hctsiz.b.pid = hc->data_pid_start;
  66408. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  66409. +
  66410. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  66411. + DWC_DEBUGPL(DBG_HCDV, " Xfer Size: %d\n", hctsiz.b.xfersize);
  66412. + DWC_DEBUGPL(DBG_HCDV, " Num Pkts: %d\n", hctsiz.b.pktcnt);
  66413. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  66414. +
  66415. + if (core_if->dma_enable) {
  66416. + dwc_dma_t dma_addr;
  66417. + if (hc->align_buff) {
  66418. + dma_addr = hc->align_buff;
  66419. + } else {
  66420. + dma_addr = ((unsigned long)hc->xfer_buff & 0xffffffff);
  66421. + }
  66422. + DWC_WRITE_REG32(&hc_regs->hcdma, dma_addr);
  66423. + }
  66424. +
  66425. + /* Start the split */
  66426. + if (hc->do_split) {
  66427. + hcsplt_data_t hcsplt;
  66428. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  66429. + hcsplt.b.spltena = 1;
  66430. + DWC_WRITE_REG32(&hc_regs->hcsplt, hcsplt.d32);
  66431. + }
  66432. +
  66433. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  66434. + hcchar.b.multicnt = hc->multi_count;
  66435. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  66436. +#ifdef DEBUG
  66437. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  66438. + if (hcchar.b.chdis) {
  66439. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  66440. + __func__, hc->hc_num, hcchar.d32);
  66441. + }
  66442. +#endif
  66443. +
  66444. + /* Set host channel enable after all other setup is complete. */
  66445. + hcchar.b.chen = 1;
  66446. + hcchar.b.chdis = 0;
  66447. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  66448. +
  66449. + hc->xfer_started = 1;
  66450. + hc->requests++;
  66451. +
  66452. + if (!core_if->dma_enable && !hc->ep_is_in && hc->xfer_len > 0) {
  66453. + /* Load OUT packet into the appropriate Tx FIFO. */
  66454. + dwc_otg_hc_write_packet(core_if, hc);
  66455. + }
  66456. +#ifdef DEBUG
  66457. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  66458. + DWC_DEBUGPL(DBG_HCDV, "transfer %d from core_if %p\n",
  66459. + hc->hc_num, core_if);//GRAYG
  66460. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  66461. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  66462. +
  66463. + /* Start a timer for this transfer. */
  66464. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  66465. + }
  66466. +#endif
  66467. +}
  66468. +
  66469. +/**
  66470. + * This function does the setup for a data transfer for a host channel
  66471. + * and starts the transfer in Descriptor DMA mode.
  66472. + *
  66473. + * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
  66474. + * Sets PID and NTD values. For periodic transfers
  66475. + * initializes SCHED_INFO field with micro-frame bitmap.
  66476. + *
  66477. + * Initializes HCDMA register with descriptor list address and CTD value
  66478. + * then starts the transfer via enabling the channel.
  66479. + *
  66480. + * @param core_if Programming view of DWC_otg controller.
  66481. + * @param hc Information needed to initialize the host channel.
  66482. + */
  66483. +void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  66484. +{
  66485. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  66486. + hcchar_data_t hcchar;
  66487. + hctsiz_data_t hctsiz;
  66488. + hcdma_data_t hcdma;
  66489. +
  66490. + hctsiz.d32 = 0;
  66491. +
  66492. + if (hc->do_ping)
  66493. + hctsiz.b_ddma.dopng = 1;
  66494. +
  66495. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  66496. + set_pid_isoc(hc);
  66497. +
  66498. + /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
  66499. + hctsiz.b_ddma.pid = hc->data_pid_start;
  66500. + hctsiz.b_ddma.ntd = hc->ntd - 1; /* 0 - 1 descriptor, 1 - 2 descriptors, etc. */
  66501. + hctsiz.b_ddma.schinfo = hc->schinfo; /* Non-zero only for high-speed interrupt endpoints */
  66502. +
  66503. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  66504. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  66505. + DWC_DEBUGPL(DBG_HCDV, " NTD: %d\n", hctsiz.b_ddma.ntd);
  66506. +
  66507. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  66508. +
  66509. + hcdma.d32 = 0;
  66510. + hcdma.b.dma_addr = ((uint32_t) hc->desc_list_addr) >> 11;
  66511. +
  66512. + /* Always start from first descriptor. */
  66513. + hcdma.b.ctd = 0;
  66514. + DWC_WRITE_REG32(&hc_regs->hcdma, hcdma.d32);
  66515. +
  66516. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  66517. + hcchar.b.multicnt = hc->multi_count;
  66518. +
  66519. +#ifdef DEBUG
  66520. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  66521. + if (hcchar.b.chdis) {
  66522. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  66523. + __func__, hc->hc_num, hcchar.d32);
  66524. + }
  66525. +#endif
  66526. +
  66527. + /* Set host channel enable after all other setup is complete. */
  66528. + hcchar.b.chen = 1;
  66529. + hcchar.b.chdis = 0;
  66530. +
  66531. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  66532. +
  66533. + hc->xfer_started = 1;
  66534. + hc->requests++;
  66535. +
  66536. +#ifdef DEBUG
  66537. + if ((hc->ep_type != DWC_OTG_EP_TYPE_INTR)
  66538. + && (hc->ep_type != DWC_OTG_EP_TYPE_ISOC)) {
  66539. + DWC_DEBUGPL(DBG_HCDV, "DMA transfer %d from core_if %p\n",
  66540. + hc->hc_num, core_if);//GRAYG
  66541. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  66542. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  66543. + /* Start a timer for this transfer. */
  66544. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  66545. + }
  66546. +#endif
  66547. +
  66548. +}
  66549. +
  66550. +/**
  66551. + * This function continues a data transfer that was started by previous call
  66552. + * to <code>dwc_otg_hc_start_transfer</code>. The caller must ensure there is
  66553. + * sufficient space in the request queue and Tx Data FIFO. This function
  66554. + * should only be called in Slave mode. In DMA mode, the controller acts
  66555. + * autonomously to complete transfers programmed to a host channel.
  66556. + *
  66557. + * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
  66558. + * if there is any data remaining to be queued. For an IN transfer, another
  66559. + * data packet is always requested. For the SETUP phase of a control transfer,
  66560. + * this function does nothing.
  66561. + *
  66562. + * @return 1 if a new request is queued, 0 if no more requests are required
  66563. + * for this transfer.
  66564. + */
  66565. +int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  66566. +{
  66567. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  66568. +
  66569. + if (hc->do_split) {
  66570. + /* SPLITs always queue just once per channel */
  66571. + return 0;
  66572. + } else if (hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  66573. + /* SETUPs are queued only once since they can't be NAKed. */
  66574. + return 0;
  66575. + } else if (hc->ep_is_in) {
  66576. + /*
  66577. + * Always queue another request for other IN transfers. If
  66578. + * back-to-back INs are issued and NAKs are received for both,
  66579. + * the driver may still be processing the first NAK when the
  66580. + * second NAK is received. When the interrupt handler clears
  66581. + * the NAK interrupt for the first NAK, the second NAK will
  66582. + * not be seen. So we can't depend on the NAK interrupt
  66583. + * handler to requeue a NAKed request. Instead, IN requests
  66584. + * are issued each time this function is called. When the
  66585. + * transfer completes, the extra requests for the channel will
  66586. + * be flushed.
  66587. + */
  66588. + hcchar_data_t hcchar;
  66589. + dwc_otg_hc_regs_t *hc_regs =
  66590. + core_if->host_if->hc_regs[hc->hc_num];
  66591. +
  66592. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  66593. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  66594. + hcchar.b.chen = 1;
  66595. + hcchar.b.chdis = 0;
  66596. + DWC_DEBUGPL(DBG_HCDV, " IN xfer: hcchar = 0x%08x\n",
  66597. + hcchar.d32);
  66598. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  66599. + hc->requests++;
  66600. + return 1;
  66601. + } else {
  66602. + /* OUT transfers. */
  66603. + if (hc->xfer_count < hc->xfer_len) {
  66604. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  66605. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  66606. + hcchar_data_t hcchar;
  66607. + dwc_otg_hc_regs_t *hc_regs;
  66608. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  66609. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  66610. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  66611. + }
  66612. +
  66613. + /* Load OUT packet into the appropriate Tx FIFO. */
  66614. + dwc_otg_hc_write_packet(core_if, hc);
  66615. + hc->requests++;
  66616. + return 1;
  66617. + } else {
  66618. + return 0;
  66619. + }
  66620. + }
  66621. +}
  66622. +
  66623. +/**
  66624. + * Starts a PING transfer. This function should only be called in Slave mode.
  66625. + * The Do Ping bit is set in the HCTSIZ register, then the channel is enabled.
  66626. + */
  66627. +void dwc_otg_hc_do_ping(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  66628. +{
  66629. + hcchar_data_t hcchar;
  66630. + hctsiz_data_t hctsiz;
  66631. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  66632. +
  66633. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  66634. +
  66635. + hctsiz.d32 = 0;
  66636. + hctsiz.b.dopng = 1;
  66637. + hctsiz.b.pktcnt = 1;
  66638. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  66639. +
  66640. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  66641. + hcchar.b.chen = 1;
  66642. + hcchar.b.chdis = 0;
  66643. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  66644. +}
  66645. +
  66646. +/*
  66647. + * This function writes a packet into the Tx FIFO associated with the Host
  66648. + * Channel. For a channel associated with a non-periodic EP, the non-periodic
  66649. + * Tx FIFO is written. For a channel associated with a periodic EP, the
  66650. + * periodic Tx FIFO is written. This function should only be called in Slave
  66651. + * mode.
  66652. + *
  66653. + * Upon return the xfer_buff and xfer_count fields in _hc are incremented by
  66654. + * then number of bytes written to the Tx FIFO.
  66655. + */
  66656. +void dwc_otg_hc_write_packet(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  66657. +{
  66658. + uint32_t i;
  66659. + uint32_t remaining_count;
  66660. + uint32_t byte_count;
  66661. + uint32_t dword_count;
  66662. +
  66663. + uint32_t *data_buff = (uint32_t *) (hc->xfer_buff);
  66664. + uint32_t *data_fifo = core_if->data_fifo[hc->hc_num];
  66665. +
  66666. + remaining_count = hc->xfer_len - hc->xfer_count;
  66667. + if (remaining_count > hc->max_packet) {
  66668. + byte_count = hc->max_packet;
  66669. + } else {
  66670. + byte_count = remaining_count;
  66671. + }
  66672. +
  66673. + dword_count = (byte_count + 3) / 4;
  66674. +
  66675. + if ((((unsigned long)data_buff) & 0x3) == 0) {
  66676. + /* xfer_buff is DWORD aligned. */
  66677. + for (i = 0; i < dword_count; i++, data_buff++) {
  66678. + DWC_WRITE_REG32(data_fifo, *data_buff);
  66679. + }
  66680. + } else {
  66681. + /* xfer_buff is not DWORD aligned. */
  66682. + for (i = 0; i < dword_count; i++, data_buff++) {
  66683. + uint32_t data;
  66684. + data =
  66685. + (data_buff[0] | data_buff[1] << 8 | data_buff[2] <<
  66686. + 16 | data_buff[3] << 24);
  66687. + DWC_WRITE_REG32(data_fifo, data);
  66688. + }
  66689. + }
  66690. +
  66691. + hc->xfer_count += byte_count;
  66692. + hc->xfer_buff += byte_count;
  66693. +}
  66694. +
  66695. +/**
  66696. + * Gets the current USB frame number. This is the frame number from the last
  66697. + * SOF packet.
  66698. + */
  66699. +uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * core_if)
  66700. +{
  66701. + dsts_data_t dsts;
  66702. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  66703. +
  66704. + /* read current frame/microframe number from DSTS register */
  66705. + return dsts.b.soffn;
  66706. +}
  66707. +
  66708. +/**
  66709. + * Calculates and gets the frame Interval value of HFIR register according PHY
  66710. + * type and speed.The application can modify a value of HFIR register only after
  66711. + * the Port Enable bit of the Host Port Control and Status register
  66712. + * (HPRT.PrtEnaPort) has been set.
  66713. +*/
  66714. +
  66715. +uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if)
  66716. +{
  66717. + gusbcfg_data_t usbcfg;
  66718. + hwcfg2_data_t hwcfg2;
  66719. + hprt0_data_t hprt0;
  66720. + int clock = 60; // default value
  66721. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  66722. + hwcfg2.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  66723. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  66724. + if (!usbcfg.b.physel && usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  66725. + clock = 60;
  66726. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 3)
  66727. + clock = 48;
  66728. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  66729. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  66730. + clock = 30;
  66731. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  66732. + !usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  66733. + clock = 60;
  66734. + if (usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  66735. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  66736. + clock = 48;
  66737. + if (usbcfg.b.physel && !usbcfg.b.phyif && hwcfg2.b.fs_phy_type == 2)
  66738. + clock = 48;
  66739. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 1)
  66740. + clock = 48;
  66741. + if (hprt0.b.prtspd == 0)
  66742. + /* High speed case */
  66743. + return 125 * clock;
  66744. + else
  66745. + /* FS/LS case */
  66746. + return 1000 * clock;
  66747. +}
  66748. +
  66749. +/**
  66750. + * This function reads a setup packet from the Rx FIFO into the destination
  66751. + * buffer. This function is called from the Rx Status Queue Level (RxStsQLvl)
  66752. + * Interrupt routine when a SETUP packet has been received in Slave mode.
  66753. + *
  66754. + * @param core_if Programming view of DWC_otg controller.
  66755. + * @param dest Destination buffer for packet data.
  66756. + */
  66757. +void dwc_otg_read_setup_packet(dwc_otg_core_if_t * core_if, uint32_t * dest)
  66758. +{
  66759. + device_grxsts_data_t status;
  66760. + /* Get the 8 bytes of a setup transaction data */
  66761. +
  66762. + /* Pop 2 DWORDS off the receive data FIFO into memory */
  66763. + dest[0] = DWC_READ_REG32(core_if->data_fifo[0]);
  66764. + dest[1] = DWC_READ_REG32(core_if->data_fifo[0]);
  66765. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  66766. + status.d32 =
  66767. + DWC_READ_REG32(&core_if->core_global_regs->grxstsp);
  66768. + DWC_DEBUGPL(DBG_ANY,
  66769. + "EP:%d BCnt:%d " "pktsts:%x Frame:%d(0x%0x)\n",
  66770. + status.b.epnum, status.b.bcnt, status.b.pktsts,
  66771. + status.b.fn, status.b.fn);
  66772. + }
  66773. +}
  66774. +
  66775. +/**
  66776. + * This function enables EP0 OUT to receive SETUP packets and configures EP0
  66777. + * IN for transmitting packets. It is normally called when the
  66778. + * "Enumeration Done" interrupt occurs.
  66779. + *
  66780. + * @param core_if Programming view of DWC_otg controller.
  66781. + * @param ep The EP0 data.
  66782. + */
  66783. +void dwc_otg_ep0_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  66784. +{
  66785. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  66786. + dsts_data_t dsts;
  66787. + depctl_data_t diepctl;
  66788. + depctl_data_t doepctl;
  66789. + dctl_data_t dctl = {.d32 = 0 };
  66790. +
  66791. + ep->stp_rollover = 0;
  66792. + /* Read the Device Status and Endpoint 0 Control registers */
  66793. + dsts.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dsts);
  66794. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  66795. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  66796. +
  66797. + /* Set the MPS of the IN EP based on the enumeration speed */
  66798. + switch (dsts.b.enumspd) {
  66799. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  66800. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  66801. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  66802. + diepctl.b.mps = DWC_DEP0CTL_MPS_64;
  66803. + break;
  66804. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  66805. + diepctl.b.mps = DWC_DEP0CTL_MPS_8;
  66806. + break;
  66807. + }
  66808. +
  66809. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  66810. +
  66811. + /* Enable OUT EP for receive */
  66812. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  66813. + doepctl.b.epena = 1;
  66814. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  66815. + }
  66816. +#ifdef VERBOSE
  66817. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  66818. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  66819. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  66820. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  66821. +#endif
  66822. + dctl.b.cgnpinnak = 1;
  66823. +
  66824. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  66825. + DWC_DEBUGPL(DBG_PCDV, "dctl=%0x\n",
  66826. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl));
  66827. +
  66828. +}
  66829. +
  66830. +/**
  66831. + * This function activates an EP. The Device EP control register for
  66832. + * the EP is configured as defined in the ep structure. Note: This
  66833. + * function is not used for EP0.
  66834. + *
  66835. + * @param core_if Programming view of DWC_otg controller.
  66836. + * @param ep The EP to activate.
  66837. + */
  66838. +void dwc_otg_ep_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  66839. +{
  66840. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  66841. + depctl_data_t depctl;
  66842. + volatile uint32_t *addr;
  66843. + daint_data_t daintmsk = {.d32 = 0 };
  66844. + dcfg_data_t dcfg;
  66845. + uint8_t i;
  66846. +
  66847. + DWC_DEBUGPL(DBG_PCDV, "%s() EP%d-%s\n", __func__, ep->num,
  66848. + (ep->is_in ? "IN" : "OUT"));
  66849. +
  66850. +#ifdef DWC_UTE_PER_IO
  66851. + ep->xiso_frame_num = 0xFFFFFFFF;
  66852. + ep->xiso_active_xfers = 0;
  66853. + ep->xiso_queued_xfers = 0;
  66854. +#endif
  66855. + /* Read DEPCTLn register */
  66856. + if (ep->is_in == 1) {
  66857. + addr = &dev_if->in_ep_regs[ep->num]->diepctl;
  66858. + daintmsk.ep.in = 1 << ep->num;
  66859. + } else {
  66860. + addr = &dev_if->out_ep_regs[ep->num]->doepctl;
  66861. + daintmsk.ep.out = 1 << ep->num;
  66862. + }
  66863. +
  66864. + /* If the EP is already active don't change the EP Control
  66865. + * register. */
  66866. + depctl.d32 = DWC_READ_REG32(addr);
  66867. + if (!depctl.b.usbactep) {
  66868. + depctl.b.mps = ep->maxpacket;
  66869. + depctl.b.eptype = ep->type;
  66870. + depctl.b.txfnum = ep->tx_fifo_num;
  66871. +
  66872. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  66873. + depctl.b.setd0pid = 1; // ???
  66874. + } else {
  66875. + depctl.b.setd0pid = 1;
  66876. + }
  66877. + depctl.b.usbactep = 1;
  66878. +
  66879. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  66880. + if (!(depctl.b.eptype & 1) && (ep->is_in == 1)) { // NP IN EP
  66881. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  66882. + if (core_if->nextep_seq[i] == core_if->first_in_nextep_seq)
  66883. + break;
  66884. + }
  66885. + core_if->nextep_seq[i] = ep->num;
  66886. + core_if->nextep_seq[ep->num] = core_if->first_in_nextep_seq;
  66887. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  66888. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  66889. + dcfg.b.epmscnt++;
  66890. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  66891. +
  66892. + DWC_DEBUGPL(DBG_PCDV,
  66893. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  66894. + __func__, core_if->first_in_nextep_seq);
  66895. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  66896. + DWC_DEBUGPL(DBG_PCDV, "%2d\n",
  66897. + core_if->nextep_seq[i]);
  66898. + }
  66899. +
  66900. + }
  66901. +
  66902. +
  66903. + DWC_WRITE_REG32(addr, depctl.d32);
  66904. + DWC_DEBUGPL(DBG_PCDV, "DEPCTL=%08x\n", DWC_READ_REG32(addr));
  66905. + }
  66906. +
  66907. + /* Enable the Interrupt for this EP */
  66908. + if (core_if->multiproc_int_enable) {
  66909. + if (ep->is_in == 1) {
  66910. + diepmsk_data_t diepmsk = {.d32 = 0 };
  66911. + diepmsk.b.xfercompl = 1;
  66912. + diepmsk.b.timeout = 1;
  66913. + diepmsk.b.epdisabled = 1;
  66914. + diepmsk.b.ahberr = 1;
  66915. + diepmsk.b.intknepmis = 1;
  66916. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  66917. + diepmsk.b.intknepmis = 0;
  66918. + diepmsk.b.txfifoundrn = 1; //?????
  66919. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  66920. + diepmsk.b.nak = 1;
  66921. + }
  66922. +
  66923. +
  66924. +
  66925. +/*
  66926. + if (core_if->dma_desc_enable) {
  66927. + diepmsk.b.bna = 1;
  66928. + }
  66929. +*/
  66930. +/*
  66931. + if (core_if->dma_enable) {
  66932. + doepmsk.b.nak = 1;
  66933. + }
  66934. +*/
  66935. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  66936. + diepeachintmsk[ep->num], diepmsk.d32);
  66937. +
  66938. + } else {
  66939. + doepmsk_data_t doepmsk = {.d32 = 0 };
  66940. + doepmsk.b.xfercompl = 1;
  66941. + doepmsk.b.ahberr = 1;
  66942. + doepmsk.b.epdisabled = 1;
  66943. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  66944. + doepmsk.b.outtknepdis = 1;
  66945. +
  66946. +/*
  66947. +
  66948. + if (core_if->dma_desc_enable) {
  66949. + doepmsk.b.bna = 1;
  66950. + }
  66951. +*/
  66952. +/*
  66953. + doepmsk.b.babble = 1;
  66954. + doepmsk.b.nyet = 1;
  66955. + doepmsk.b.nak = 1;
  66956. +*/
  66957. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  66958. + doepeachintmsk[ep->num], doepmsk.d32);
  66959. + }
  66960. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->deachintmsk,
  66961. + 0, daintmsk.d32);
  66962. + } else {
  66963. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  66964. + if (ep->is_in) {
  66965. + diepmsk_data_t diepmsk = {.d32 = 0 };
  66966. + diepmsk.b.nak = 1;
  66967. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk, 0, diepmsk.d32);
  66968. + } else {
  66969. + doepmsk_data_t doepmsk = {.d32 = 0 };
  66970. + doepmsk.b.outtknepdis = 1;
  66971. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->doepmsk, 0, doepmsk.d32);
  66972. + }
  66973. + }
  66974. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->daintmsk,
  66975. + 0, daintmsk.d32);
  66976. + }
  66977. +
  66978. + DWC_DEBUGPL(DBG_PCDV, "DAINTMSK=%0x\n",
  66979. + DWC_READ_REG32(&dev_if->dev_global_regs->daintmsk));
  66980. +
  66981. + ep->stall_clear_flag = 0;
  66982. +
  66983. + return;
  66984. +}
  66985. +
  66986. +/**
  66987. + * This function deactivates an EP. This is done by clearing the USB Active
  66988. + * EP bit in the Device EP control register. Note: This function is not used
  66989. + * for EP0. EP0 cannot be deactivated.
  66990. + *
  66991. + * @param core_if Programming view of DWC_otg controller.
  66992. + * @param ep The EP to deactivate.
  66993. + */
  66994. +void dwc_otg_ep_deactivate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  66995. +{
  66996. + depctl_data_t depctl = {.d32 = 0 };
  66997. + volatile uint32_t *addr;
  66998. + daint_data_t daintmsk = {.d32 = 0 };
  66999. + dcfg_data_t dcfg;
  67000. + uint8_t i = 0;
  67001. +
  67002. +#ifdef DWC_UTE_PER_IO
  67003. + ep->xiso_frame_num = 0xFFFFFFFF;
  67004. + ep->xiso_active_xfers = 0;
  67005. + ep->xiso_queued_xfers = 0;
  67006. +#endif
  67007. +
  67008. + /* Read DEPCTLn register */
  67009. + if (ep->is_in == 1) {
  67010. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  67011. + daintmsk.ep.in = 1 << ep->num;
  67012. + } else {
  67013. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  67014. + daintmsk.ep.out = 1 << ep->num;
  67015. + }
  67016. +
  67017. + depctl.d32 = DWC_READ_REG32(addr);
  67018. +
  67019. + depctl.b.usbactep = 0;
  67020. +
  67021. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  67022. + if (!(depctl.b.eptype & 1) && ep->is_in == 1) { // NP EP IN
  67023. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  67024. + if (core_if->nextep_seq[i] == ep->num)
  67025. + break;
  67026. + }
  67027. + core_if->nextep_seq[i] = core_if->nextep_seq[ep->num];
  67028. + if (core_if->first_in_nextep_seq == ep->num)
  67029. + core_if->first_in_nextep_seq = i;
  67030. + core_if->nextep_seq[ep->num] = 0xff;
  67031. + depctl.b.nextep = 0;
  67032. + dcfg.d32 =
  67033. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  67034. + dcfg.b.epmscnt--;
  67035. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  67036. + dcfg.d32);
  67037. +
  67038. + DWC_DEBUGPL(DBG_PCDV,
  67039. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  67040. + __func__, core_if->first_in_nextep_seq);
  67041. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  67042. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  67043. + }
  67044. + }
  67045. +
  67046. + if (ep->is_in == 1)
  67047. + depctl.b.txfnum = 0;
  67048. +
  67049. + if (core_if->dma_desc_enable)
  67050. + depctl.b.epdis = 1;
  67051. +
  67052. + DWC_WRITE_REG32(addr, depctl.d32);
  67053. + depctl.d32 = DWC_READ_REG32(addr);
  67054. + if (core_if->dma_enable && ep->type == DWC_OTG_EP_TYPE_ISOC
  67055. + && depctl.b.epena) {
  67056. + depctl_data_t depctl = {.d32 = 0};
  67057. + if (ep->is_in) {
  67058. + diepint_data_t diepint = {.d32 = 0};
  67059. +
  67060. + depctl.b.snak = 1;
  67061. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  67062. + diepctl, depctl.d32);
  67063. + do {
  67064. + dwc_udelay(10);
  67065. + diepint.d32 =
  67066. + DWC_READ_REG32(&core_if->
  67067. + dev_if->in_ep_regs[ep->num]->
  67068. + diepint);
  67069. + } while (!diepint.b.inepnakeff);
  67070. + diepint.b.inepnakeff = 1;
  67071. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  67072. + diepint, diepint.d32);
  67073. + depctl.d32 = 0;
  67074. + depctl.b.epdis = 1;
  67075. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  67076. + diepctl, depctl.d32);
  67077. + do {
  67078. + dwc_udelay(10);
  67079. + diepint.d32 =
  67080. + DWC_READ_REG32(&core_if->
  67081. + dev_if->in_ep_regs[ep->num]->
  67082. + diepint);
  67083. + } while (!diepint.b.epdisabled);
  67084. + diepint.b.epdisabled = 1;
  67085. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  67086. + diepint, diepint.d32);
  67087. + } else {
  67088. + dctl_data_t dctl = {.d32 = 0};
  67089. + gintmsk_data_t gintsts = {.d32 = 0};
  67090. + doepint_data_t doepint = {.d32 = 0};
  67091. + dctl.b.sgoutnak = 1;
  67092. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  67093. + dctl, 0, dctl.d32);
  67094. + do {
  67095. + dwc_udelay(10);
  67096. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  67097. + } while (!gintsts.b.goutnakeff);
  67098. + gintsts.d32 = 0;
  67099. + gintsts.b.goutnakeff = 1;
  67100. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  67101. +
  67102. + depctl.d32 = 0;
  67103. + depctl.b.epdis = 1;
  67104. + depctl.b.snak = 1;
  67105. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepctl, depctl.d32);
  67106. + do
  67107. + {
  67108. + dwc_udelay(10);
  67109. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  67110. + out_ep_regs[ep->num]->doepint);
  67111. + } while (!doepint.b.epdisabled);
  67112. +
  67113. + doepint.b.epdisabled = 1;
  67114. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepint, doepint.d32);
  67115. +
  67116. + dctl.d32 = 0;
  67117. + dctl.b.cgoutnak = 1;
  67118. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  67119. + }
  67120. + }
  67121. +
  67122. + /* Disable the Interrupt for this EP */
  67123. + if (core_if->multiproc_int_enable) {
  67124. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  67125. + daintmsk.d32, 0);
  67126. +
  67127. + if (ep->is_in == 1) {
  67128. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  67129. + diepeachintmsk[ep->num], 0);
  67130. + } else {
  67131. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  67132. + doepeachintmsk[ep->num], 0);
  67133. + }
  67134. + } else {
  67135. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->daintmsk,
  67136. + daintmsk.d32, 0);
  67137. + }
  67138. +
  67139. +}
  67140. +
  67141. +/**
  67142. + * This function initializes dma descriptor chain.
  67143. + *
  67144. + * @param core_if Programming view of DWC_otg controller.
  67145. + * @param ep The EP to start the transfer on.
  67146. + */
  67147. +static void init_dma_desc_chain(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  67148. +{
  67149. + dwc_otg_dev_dma_desc_t *dma_desc;
  67150. + uint32_t offset;
  67151. + uint32_t xfer_est;
  67152. + int i;
  67153. + unsigned maxxfer_local, total_len;
  67154. +
  67155. + if (!ep->is_in && ep->type == DWC_OTG_EP_TYPE_INTR &&
  67156. + (ep->maxpacket%4)) {
  67157. + maxxfer_local = ep->maxpacket;
  67158. + total_len = ep->xfer_len;
  67159. + } else {
  67160. + maxxfer_local = ep->maxxfer;
  67161. + total_len = ep->total_len;
  67162. + }
  67163. +
  67164. + ep->desc_cnt = (total_len / maxxfer_local) +
  67165. + ((total_len % maxxfer_local) ? 1 : 0);
  67166. +
  67167. + if (!ep->desc_cnt)
  67168. + ep->desc_cnt = 1;
  67169. +
  67170. + if (ep->desc_cnt > MAX_DMA_DESC_CNT)
  67171. + ep->desc_cnt = MAX_DMA_DESC_CNT;
  67172. +
  67173. + dma_desc = ep->desc_addr;
  67174. + if (maxxfer_local == ep->maxpacket) {
  67175. + if ((total_len % maxxfer_local) &&
  67176. + (total_len/maxxfer_local < MAX_DMA_DESC_CNT)) {
  67177. + xfer_est = (ep->desc_cnt - 1) * maxxfer_local +
  67178. + (total_len % maxxfer_local);
  67179. + } else
  67180. + xfer_est = ep->desc_cnt * maxxfer_local;
  67181. + } else
  67182. + xfer_est = total_len;
  67183. + offset = 0;
  67184. + for (i = 0; i < ep->desc_cnt; ++i) {
  67185. + /** DMA Descriptor Setup */
  67186. + if (xfer_est > maxxfer_local) {
  67187. + dma_desc->status.b.bs = BS_HOST_BUSY;
  67188. + dma_desc->status.b.l = 0;
  67189. + dma_desc->status.b.ioc = 0;
  67190. + dma_desc->status.b.sp = 0;
  67191. + dma_desc->status.b.bytes = maxxfer_local;
  67192. + dma_desc->buf = ep->dma_addr + offset;
  67193. + dma_desc->status.b.sts = 0;
  67194. + dma_desc->status.b.bs = BS_HOST_READY;
  67195. +
  67196. + xfer_est -= maxxfer_local;
  67197. + offset += maxxfer_local;
  67198. + } else {
  67199. + dma_desc->status.b.bs = BS_HOST_BUSY;
  67200. + dma_desc->status.b.l = 1;
  67201. + dma_desc->status.b.ioc = 1;
  67202. + if (ep->is_in) {
  67203. + dma_desc->status.b.sp =
  67204. + (xfer_est %
  67205. + ep->maxpacket) ? 1 : ((ep->
  67206. + sent_zlp) ? 1 : 0);
  67207. + dma_desc->status.b.bytes = xfer_est;
  67208. + } else {
  67209. + if (maxxfer_local == ep->maxpacket)
  67210. + dma_desc->status.b.bytes = xfer_est;
  67211. + else
  67212. + dma_desc->status.b.bytes =
  67213. + xfer_est + ((4 - (xfer_est & 0x3)) & 0x3);
  67214. + }
  67215. +
  67216. + dma_desc->buf = ep->dma_addr + offset;
  67217. + dma_desc->status.b.sts = 0;
  67218. + dma_desc->status.b.bs = BS_HOST_READY;
  67219. + }
  67220. + dma_desc++;
  67221. + }
  67222. +}
  67223. +/**
  67224. + * This function is called when to write ISOC data into appropriate dedicated
  67225. + * periodic FIFO.
  67226. + */
  67227. +static int32_t write_isoc_tx_fifo(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  67228. +{
  67229. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  67230. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  67231. + dtxfsts_data_t txstatus = {.d32 = 0 };
  67232. + uint32_t len = 0;
  67233. + int epnum = dwc_ep->num;
  67234. + int dwords;
  67235. +
  67236. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  67237. +
  67238. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  67239. +
  67240. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  67241. +
  67242. + if (len > dwc_ep->maxpacket) {
  67243. + len = dwc_ep->maxpacket;
  67244. + }
  67245. +
  67246. + dwords = (len + 3) / 4;
  67247. +
  67248. + /* While there is space in the queue and space in the FIFO and
  67249. + * More data to tranfer, Write packets to the Tx FIFO */
  67250. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  67251. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  67252. +
  67253. + while (txstatus.b.txfspcavail > dwords &&
  67254. + dwc_ep->xfer_count < dwc_ep->xfer_len && dwc_ep->xfer_len != 0) {
  67255. + /* Write the FIFO */
  67256. + dwc_otg_ep_write_packet(core_if, dwc_ep, 0);
  67257. +
  67258. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  67259. + if (len > dwc_ep->maxpacket) {
  67260. + len = dwc_ep->maxpacket;
  67261. + }
  67262. +
  67263. + dwords = (len + 3) / 4;
  67264. + txstatus.d32 =
  67265. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  67266. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  67267. + txstatus.d32);
  67268. + }
  67269. +
  67270. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  67271. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  67272. +
  67273. + return 1;
  67274. +}
  67275. +/**
  67276. + * This function does the setup for a data transfer for an EP and
  67277. + * starts the transfer. For an IN transfer, the packets will be
  67278. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  67279. + * the packets are unloaded from the Rx FIFO in the ISR. the ISR.
  67280. + *
  67281. + * @param core_if Programming view of DWC_otg controller.
  67282. + * @param ep The EP to start the transfer on.
  67283. + */
  67284. +
  67285. +void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  67286. +{
  67287. + depctl_data_t depctl;
  67288. + deptsiz_data_t deptsiz;
  67289. + gintmsk_data_t intr_mask = {.d32 = 0 };
  67290. +
  67291. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  67292. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  67293. + "xfer_buff=%p start_xfer_buff=%p, total_len = %d\n",
  67294. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  67295. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff,
  67296. + ep->total_len);
  67297. + /* IN endpoint */
  67298. + if (ep->is_in == 1) {
  67299. + dwc_otg_dev_in_ep_regs_t *in_regs =
  67300. + core_if->dev_if->in_ep_regs[ep->num];
  67301. +
  67302. + gnptxsts_data_t gtxstatus;
  67303. +
  67304. + gtxstatus.d32 =
  67305. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  67306. +
  67307. + if (core_if->en_multiple_tx_fifo == 0
  67308. + && gtxstatus.b.nptxqspcavail == 0 && !core_if->dma_enable) {
  67309. +#ifdef DEBUG
  67310. + DWC_PRINTF("TX Queue Full (0x%0x)\n", gtxstatus.d32);
  67311. +#endif
  67312. + return;
  67313. + }
  67314. +
  67315. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  67316. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  67317. +
  67318. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  67319. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  67320. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  67321. + else
  67322. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len - ep->xfer_len)) ?
  67323. + MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  67324. +
  67325. +
  67326. + /* Zero Length Packet? */
  67327. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  67328. + deptsiz.b.xfersize = 0;
  67329. + deptsiz.b.pktcnt = 1;
  67330. + } else {
  67331. + /* Program the transfer size and packet count
  67332. + * as follows: xfersize = N * maxpacket +
  67333. + * short_packet pktcnt = N + (short_packet
  67334. + * exist ? 1 : 0)
  67335. + */
  67336. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  67337. + deptsiz.b.pktcnt =
  67338. + (ep->xfer_len - ep->xfer_count - 1 +
  67339. + ep->maxpacket) / ep->maxpacket;
  67340. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  67341. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  67342. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  67343. + }
  67344. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  67345. + deptsiz.b.mc = deptsiz.b.pktcnt;
  67346. + }
  67347. +
  67348. + /* Write the DMA register */
  67349. + if (core_if->dma_enable) {
  67350. + if (core_if->dma_desc_enable == 0) {
  67351. + if (ep->type != DWC_OTG_EP_TYPE_ISOC)
  67352. + deptsiz.b.mc = 1;
  67353. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  67354. + deptsiz.d32);
  67355. + DWC_WRITE_REG32(&(in_regs->diepdma),
  67356. + (uint32_t) ep->dma_addr);
  67357. + } else {
  67358. +#ifdef DWC_UTE_CFI
  67359. + /* The descriptor chain should be already initialized by now */
  67360. + if (ep->buff_mode != BM_STANDARD) {
  67361. + DWC_WRITE_REG32(&in_regs->diepdma,
  67362. + ep->descs_dma_addr);
  67363. + } else {
  67364. +#endif
  67365. + init_dma_desc_chain(core_if, ep);
  67366. + /** DIEPDMAn Register write */
  67367. + DWC_WRITE_REG32(&in_regs->diepdma,
  67368. + ep->dma_desc_addr);
  67369. +#ifdef DWC_UTE_CFI
  67370. + }
  67371. +#endif
  67372. + }
  67373. + } else {
  67374. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  67375. + if (ep->type != DWC_OTG_EP_TYPE_ISOC) {
  67376. + /**
  67377. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  67378. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  67379. + * the data will be written into the fifo by the ISR.
  67380. + */
  67381. + if (core_if->en_multiple_tx_fifo == 0) {
  67382. + intr_mask.b.nptxfempty = 1;
  67383. + DWC_MODIFY_REG32
  67384. + (&core_if->core_global_regs->gintmsk,
  67385. + intr_mask.d32, intr_mask.d32);
  67386. + } else {
  67387. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  67388. + if (ep->xfer_len > 0) {
  67389. + uint32_t fifoemptymsk = 0;
  67390. + fifoemptymsk = 1 << ep->num;
  67391. + DWC_MODIFY_REG32
  67392. + (&core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  67393. + 0, fifoemptymsk);
  67394. +
  67395. + }
  67396. + }
  67397. + } else {
  67398. + write_isoc_tx_fifo(core_if, ep);
  67399. + }
  67400. + }
  67401. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  67402. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  67403. +
  67404. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  67405. + dsts_data_t dsts = {.d32 = 0};
  67406. + if (ep->bInterval == 1) {
  67407. + dsts.d32 =
  67408. + DWC_READ_REG32(&core_if->dev_if->
  67409. + dev_global_regs->dsts);
  67410. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  67411. + if (ep->frame_num > 0x3FFF) {
  67412. + ep->frm_overrun = 1;
  67413. + ep->frame_num &= 0x3FFF;
  67414. + } else
  67415. + ep->frm_overrun = 0;
  67416. + if (ep->frame_num & 0x1) {
  67417. + depctl.b.setd1pid = 1;
  67418. + } else {
  67419. + depctl.b.setd0pid = 1;
  67420. + }
  67421. + }
  67422. + }
  67423. + /* EP enable, IN data in FIFO */
  67424. + depctl.b.cnak = 1;
  67425. + depctl.b.epena = 1;
  67426. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  67427. +
  67428. + } else {
  67429. + /* OUT endpoint */
  67430. + dwc_otg_dev_out_ep_regs_t *out_regs =
  67431. + core_if->dev_if->out_ep_regs[ep->num];
  67432. +
  67433. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  67434. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  67435. +
  67436. + if (!core_if->dma_desc_enable) {
  67437. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  67438. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  67439. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  67440. + else
  67441. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len
  67442. + - ep->xfer_len)) ? MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  67443. + }
  67444. +
  67445. + /* Program the transfer size and packet count as follows:
  67446. + *
  67447. + * pktcnt = N
  67448. + * xfersize = N * maxpacket
  67449. + */
  67450. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  67451. + /* Zero Length Packet */
  67452. + deptsiz.b.xfersize = ep->maxpacket;
  67453. + deptsiz.b.pktcnt = 1;
  67454. + } else {
  67455. + deptsiz.b.pktcnt =
  67456. + (ep->xfer_len - ep->xfer_count +
  67457. + (ep->maxpacket - 1)) / ep->maxpacket;
  67458. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  67459. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  67460. + }
  67461. + if (!core_if->dma_desc_enable) {
  67462. + ep->xfer_len =
  67463. + deptsiz.b.pktcnt * ep->maxpacket + ep->xfer_count;
  67464. + }
  67465. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  67466. + }
  67467. +
  67468. + DWC_DEBUGPL(DBG_PCDV, "ep%d xfersize=%d pktcnt=%d\n",
  67469. + ep->num, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  67470. +
  67471. + if (core_if->dma_enable) {
  67472. + if (!core_if->dma_desc_enable) {
  67473. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  67474. + deptsiz.d32);
  67475. +
  67476. + DWC_WRITE_REG32(&(out_regs->doepdma),
  67477. + (uint32_t) ep->dma_addr);
  67478. + } else {
  67479. +#ifdef DWC_UTE_CFI
  67480. + /* The descriptor chain should be already initialized by now */
  67481. + if (ep->buff_mode != BM_STANDARD) {
  67482. + DWC_WRITE_REG32(&out_regs->doepdma,
  67483. + ep->descs_dma_addr);
  67484. + } else {
  67485. +#endif
  67486. + /** This is used for interrupt out transfers*/
  67487. + if (!ep->xfer_len)
  67488. + ep->xfer_len = ep->total_len;
  67489. + init_dma_desc_chain(core_if, ep);
  67490. +
  67491. + if (core_if->core_params->dev_out_nak) {
  67492. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  67493. + deptsiz.b.pktcnt = (ep->total_len +
  67494. + (ep->maxpacket - 1)) / ep->maxpacket;
  67495. + deptsiz.b.xfersize = ep->total_len;
  67496. + /* Remember initial value of doeptsiz */
  67497. + core_if->start_doeptsiz_val[ep->num] = deptsiz.d32;
  67498. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  67499. + deptsiz.d32);
  67500. + }
  67501. + }
  67502. + /** DOEPDMAn Register write */
  67503. + DWC_WRITE_REG32(&out_regs->doepdma,
  67504. + ep->dma_desc_addr);
  67505. +#ifdef DWC_UTE_CFI
  67506. + }
  67507. +#endif
  67508. + }
  67509. + } else {
  67510. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  67511. + }
  67512. +
  67513. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  67514. + dsts_data_t dsts = {.d32 = 0};
  67515. + if (ep->bInterval == 1) {
  67516. + dsts.d32 =
  67517. + DWC_READ_REG32(&core_if->dev_if->
  67518. + dev_global_regs->dsts);
  67519. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  67520. + if (ep->frame_num > 0x3FFF) {
  67521. + ep->frm_overrun = 1;
  67522. + ep->frame_num &= 0x3FFF;
  67523. + } else
  67524. + ep->frm_overrun = 0;
  67525. +
  67526. + if (ep->frame_num & 0x1) {
  67527. + depctl.b.setd1pid = 1;
  67528. + } else {
  67529. + depctl.b.setd0pid = 1;
  67530. + }
  67531. + }
  67532. + }
  67533. +
  67534. + /* EP enable */
  67535. + depctl.b.cnak = 1;
  67536. + depctl.b.epena = 1;
  67537. +
  67538. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  67539. +
  67540. + DWC_DEBUGPL(DBG_PCD, "DOEPCTL=%08x DOEPTSIZ=%08x\n",
  67541. + DWC_READ_REG32(&out_regs->doepctl),
  67542. + DWC_READ_REG32(&out_regs->doeptsiz));
  67543. + DWC_DEBUGPL(DBG_PCD, "DAINTMSK=%08x GINTMSK=%08x\n",
  67544. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  67545. + daintmsk),
  67546. + DWC_READ_REG32(&core_if->core_global_regs->
  67547. + gintmsk));
  67548. +
  67549. + /* Timer is scheduling only for out bulk transfers for
  67550. + * "Device DDMA OUT NAK Enhancement" feature to inform user
  67551. + * about received data payload in case of timeout
  67552. + */
  67553. + if (core_if->core_params->dev_out_nak) {
  67554. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  67555. + core_if->ep_xfer_info[ep->num].core_if = core_if;
  67556. + core_if->ep_xfer_info[ep->num].ep = ep;
  67557. + core_if->ep_xfer_info[ep->num].state = 1;
  67558. +
  67559. + /* Start a timer for this transfer. */
  67560. + DWC_TIMER_SCHEDULE(core_if->ep_xfer_timer[ep->num], 10000);
  67561. + }
  67562. + }
  67563. + }
  67564. +}
  67565. +
  67566. +/**
  67567. + * This function setup a zero length transfer in Buffer DMA and
  67568. + * Slave modes for usb requests with zero field set
  67569. + *
  67570. + * @param core_if Programming view of DWC_otg controller.
  67571. + * @param ep The EP to start the transfer on.
  67572. + *
  67573. + */
  67574. +void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  67575. +{
  67576. +
  67577. + depctl_data_t depctl;
  67578. + deptsiz_data_t deptsiz;
  67579. + gintmsk_data_t intr_mask = {.d32 = 0 };
  67580. +
  67581. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  67582. + DWC_PRINTF("zero length transfer is called\n");
  67583. +
  67584. + /* IN endpoint */
  67585. + if (ep->is_in == 1) {
  67586. + dwc_otg_dev_in_ep_regs_t *in_regs =
  67587. + core_if->dev_if->in_ep_regs[ep->num];
  67588. +
  67589. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  67590. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  67591. +
  67592. + deptsiz.b.xfersize = 0;
  67593. + deptsiz.b.pktcnt = 1;
  67594. +
  67595. + /* Write the DMA register */
  67596. + if (core_if->dma_enable) {
  67597. + if (core_if->dma_desc_enable == 0) {
  67598. + deptsiz.b.mc = 1;
  67599. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  67600. + deptsiz.d32);
  67601. + DWC_WRITE_REG32(&(in_regs->diepdma),
  67602. + (uint32_t) ep->dma_addr);
  67603. + }
  67604. + } else {
  67605. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  67606. + /**
  67607. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  67608. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  67609. + * the data will be written into the fifo by the ISR.
  67610. + */
  67611. + if (core_if->en_multiple_tx_fifo == 0) {
  67612. + intr_mask.b.nptxfempty = 1;
  67613. + DWC_MODIFY_REG32(&core_if->
  67614. + core_global_regs->gintmsk,
  67615. + intr_mask.d32, intr_mask.d32);
  67616. + } else {
  67617. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  67618. + if (ep->xfer_len > 0) {
  67619. + uint32_t fifoemptymsk = 0;
  67620. + fifoemptymsk = 1 << ep->num;
  67621. + DWC_MODIFY_REG32(&core_if->
  67622. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  67623. + 0, fifoemptymsk);
  67624. + }
  67625. + }
  67626. + }
  67627. +
  67628. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  67629. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  67630. + /* EP enable, IN data in FIFO */
  67631. + depctl.b.cnak = 1;
  67632. + depctl.b.epena = 1;
  67633. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  67634. +
  67635. + } else {
  67636. + /* OUT endpoint */
  67637. + dwc_otg_dev_out_ep_regs_t *out_regs =
  67638. + core_if->dev_if->out_ep_regs[ep->num];
  67639. +
  67640. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  67641. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  67642. +
  67643. + /* Zero Length Packet */
  67644. + deptsiz.b.xfersize = ep->maxpacket;
  67645. + deptsiz.b.pktcnt = 1;
  67646. +
  67647. + if (core_if->dma_enable) {
  67648. + if (!core_if->dma_desc_enable) {
  67649. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  67650. + deptsiz.d32);
  67651. +
  67652. + DWC_WRITE_REG32(&(out_regs->doepdma),
  67653. + (uint32_t) ep->dma_addr);
  67654. + }
  67655. + } else {
  67656. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  67657. + }
  67658. +
  67659. + /* EP enable */
  67660. + depctl.b.cnak = 1;
  67661. + depctl.b.epena = 1;
  67662. +
  67663. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  67664. +
  67665. + }
  67666. +}
  67667. +
  67668. +/**
  67669. + * This function does the setup for a data transfer for EP0 and starts
  67670. + * the transfer. For an IN transfer, the packets will be loaded into
  67671. + * the appropriate Tx FIFO in the ISR. For OUT transfers, the packets are
  67672. + * unloaded from the Rx FIFO in the ISR.
  67673. + *
  67674. + * @param core_if Programming view of DWC_otg controller.
  67675. + * @param ep The EP0 data.
  67676. + */
  67677. +void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  67678. +{
  67679. + depctl_data_t depctl;
  67680. + deptsiz0_data_t deptsiz;
  67681. + gintmsk_data_t intr_mask = {.d32 = 0 };
  67682. + dwc_otg_dev_dma_desc_t *dma_desc;
  67683. +
  67684. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  67685. + "xfer_buff=%p start_xfer_buff=%p \n",
  67686. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  67687. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff);
  67688. +
  67689. + ep->total_len = ep->xfer_len;
  67690. +
  67691. + /* IN endpoint */
  67692. + if (ep->is_in == 1) {
  67693. + dwc_otg_dev_in_ep_regs_t *in_regs =
  67694. + core_if->dev_if->in_ep_regs[0];
  67695. +
  67696. + gnptxsts_data_t gtxstatus;
  67697. +
  67698. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  67699. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  67700. + if (depctl.b.epena)
  67701. + return;
  67702. + }
  67703. +
  67704. + gtxstatus.d32 =
  67705. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  67706. +
  67707. + /* If dedicated FIFO every time flush fifo before enable ep*/
  67708. + if (core_if->en_multiple_tx_fifo && core_if->snpsid >= OTG_CORE_REV_3_00a)
  67709. + dwc_otg_flush_tx_fifo(core_if, ep->tx_fifo_num);
  67710. +
  67711. + if (core_if->en_multiple_tx_fifo == 0
  67712. + && gtxstatus.b.nptxqspcavail == 0
  67713. + && !core_if->dma_enable) {
  67714. +#ifdef DEBUG
  67715. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  67716. + DWC_DEBUGPL(DBG_PCD, "DIEPCTL0=%0x\n",
  67717. + DWC_READ_REG32(&in_regs->diepctl));
  67718. + DWC_DEBUGPL(DBG_PCD, "DIEPTSIZ0=%0x (sz=%d, pcnt=%d)\n",
  67719. + deptsiz.d32,
  67720. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  67721. + DWC_PRINTF("TX Queue or FIFO Full (0x%0x)\n",
  67722. + gtxstatus.d32);
  67723. +#endif
  67724. + return;
  67725. + }
  67726. +
  67727. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  67728. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  67729. +
  67730. + /* Zero Length Packet? */
  67731. + if (ep->xfer_len == 0) {
  67732. + deptsiz.b.xfersize = 0;
  67733. + deptsiz.b.pktcnt = 1;
  67734. + } else {
  67735. + /* Program the transfer size and packet count
  67736. + * as follows: xfersize = N * maxpacket +
  67737. + * short_packet pktcnt = N + (short_packet
  67738. + * exist ? 1 : 0)
  67739. + */
  67740. + if (ep->xfer_len > ep->maxpacket) {
  67741. + ep->xfer_len = ep->maxpacket;
  67742. + deptsiz.b.xfersize = ep->maxpacket;
  67743. + } else {
  67744. + deptsiz.b.xfersize = ep->xfer_len;
  67745. + }
  67746. + deptsiz.b.pktcnt = 1;
  67747. +
  67748. + }
  67749. + DWC_DEBUGPL(DBG_PCDV,
  67750. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  67751. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  67752. + deptsiz.d32);
  67753. +
  67754. + /* Write the DMA register */
  67755. + if (core_if->dma_enable) {
  67756. + if (core_if->dma_desc_enable == 0) {
  67757. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  67758. + deptsiz.d32);
  67759. +
  67760. + DWC_WRITE_REG32(&(in_regs->diepdma),
  67761. + (uint32_t) ep->dma_addr);
  67762. + } else {
  67763. + dma_desc = core_if->dev_if->in_desc_addr;
  67764. +
  67765. + /** DMA Descriptor Setup */
  67766. + dma_desc->status.b.bs = BS_HOST_BUSY;
  67767. + dma_desc->status.b.l = 1;
  67768. + dma_desc->status.b.ioc = 1;
  67769. + dma_desc->status.b.sp =
  67770. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  67771. + dma_desc->status.b.bytes = ep->xfer_len;
  67772. + dma_desc->buf = ep->dma_addr;
  67773. + dma_desc->status.b.sts = 0;
  67774. + dma_desc->status.b.bs = BS_HOST_READY;
  67775. +
  67776. + /** DIEPDMA0 Register write */
  67777. + DWC_WRITE_REG32(&in_regs->diepdma,
  67778. + core_if->
  67779. + dev_if->dma_in_desc_addr);
  67780. + }
  67781. + } else {
  67782. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  67783. + }
  67784. +
  67785. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  67786. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  67787. + /* EP enable, IN data in FIFO */
  67788. + depctl.b.cnak = 1;
  67789. + depctl.b.epena = 1;
  67790. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  67791. +
  67792. + /**
  67793. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  67794. + * data will be written into the fifo by the ISR.
  67795. + */
  67796. + if (!core_if->dma_enable) {
  67797. + if (core_if->en_multiple_tx_fifo == 0) {
  67798. + intr_mask.b.nptxfempty = 1;
  67799. + DWC_MODIFY_REG32(&core_if->
  67800. + core_global_regs->gintmsk,
  67801. + intr_mask.d32, intr_mask.d32);
  67802. + } else {
  67803. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  67804. + if (ep->xfer_len > 0) {
  67805. + uint32_t fifoemptymsk = 0;
  67806. + fifoemptymsk |= 1 << ep->num;
  67807. + DWC_MODIFY_REG32(&core_if->
  67808. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  67809. + 0, fifoemptymsk);
  67810. + }
  67811. + }
  67812. + }
  67813. + } else {
  67814. + /* OUT endpoint */
  67815. + dwc_otg_dev_out_ep_regs_t *out_regs =
  67816. + core_if->dev_if->out_ep_regs[0];
  67817. +
  67818. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  67819. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  67820. +
  67821. + /* Program the transfer size and packet count as follows:
  67822. + * xfersize = N * (maxpacket + 4 - (maxpacket % 4))
  67823. + * pktcnt = N */
  67824. + /* Zero Length Packet */
  67825. + deptsiz.b.xfersize = ep->maxpacket;
  67826. + deptsiz.b.pktcnt = 1;
  67827. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  67828. + deptsiz.b.supcnt = 3;
  67829. +
  67830. + DWC_DEBUGPL(DBG_PCDV, "len=%d xfersize=%d pktcnt=%d\n",
  67831. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  67832. +
  67833. + if (core_if->dma_enable) {
  67834. + if (!core_if->dma_desc_enable) {
  67835. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  67836. + deptsiz.d32);
  67837. +
  67838. + DWC_WRITE_REG32(&(out_regs->doepdma),
  67839. + (uint32_t) ep->dma_addr);
  67840. + } else {
  67841. + dma_desc = core_if->dev_if->out_desc_addr;
  67842. +
  67843. + /** DMA Descriptor Setup */
  67844. + dma_desc->status.b.bs = BS_HOST_BUSY;
  67845. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  67846. + dma_desc->status.b.mtrf = 0;
  67847. + dma_desc->status.b.sr = 0;
  67848. + }
  67849. + dma_desc->status.b.l = 1;
  67850. + dma_desc->status.b.ioc = 1;
  67851. + dma_desc->status.b.bytes = ep->maxpacket;
  67852. + dma_desc->buf = ep->dma_addr;
  67853. + dma_desc->status.b.sts = 0;
  67854. + dma_desc->status.b.bs = BS_HOST_READY;
  67855. +
  67856. + /** DOEPDMA0 Register write */
  67857. + DWC_WRITE_REG32(&out_regs->doepdma,
  67858. + core_if->dev_if->
  67859. + dma_out_desc_addr);
  67860. + }
  67861. + } else {
  67862. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  67863. + }
  67864. +
  67865. + /* EP enable */
  67866. + depctl.b.cnak = 1;
  67867. + depctl.b.epena = 1;
  67868. + DWC_WRITE_REG32(&(out_regs->doepctl), depctl.d32);
  67869. + }
  67870. +}
  67871. +
  67872. +/**
  67873. + * This function continues control IN transfers started by
  67874. + * dwc_otg_ep0_start_transfer, when the transfer does not fit in a
  67875. + * single packet. NOTE: The DIEPCTL0/DOEPCTL0 registers only have one
  67876. + * bit for the packet count.
  67877. + *
  67878. + * @param core_if Programming view of DWC_otg controller.
  67879. + * @param ep The EP0 data.
  67880. + */
  67881. +void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  67882. +{
  67883. + depctl_data_t depctl;
  67884. + deptsiz0_data_t deptsiz;
  67885. + gintmsk_data_t intr_mask = {.d32 = 0 };
  67886. + dwc_otg_dev_dma_desc_t *dma_desc;
  67887. +
  67888. + if (ep->is_in == 1) {
  67889. + dwc_otg_dev_in_ep_regs_t *in_regs =
  67890. + core_if->dev_if->in_ep_regs[0];
  67891. + gnptxsts_data_t tx_status = {.d32 = 0 };
  67892. +
  67893. + tx_status.d32 =
  67894. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  67895. + /** @todo Should there be check for room in the Tx
  67896. + * Status Queue. If not remove the code above this comment. */
  67897. +
  67898. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  67899. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  67900. +
  67901. + /* Program the transfer size and packet count
  67902. + * as follows: xfersize = N * maxpacket +
  67903. + * short_packet pktcnt = N + (short_packet
  67904. + * exist ? 1 : 0)
  67905. + */
  67906. +
  67907. + if (core_if->dma_desc_enable == 0) {
  67908. + deptsiz.b.xfersize =
  67909. + (ep->total_len - ep->xfer_count) >
  67910. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  67911. + ep->xfer_count);
  67912. + deptsiz.b.pktcnt = 1;
  67913. + if (core_if->dma_enable == 0) {
  67914. + ep->xfer_len += deptsiz.b.xfersize;
  67915. + } else {
  67916. + ep->xfer_len = deptsiz.b.xfersize;
  67917. + }
  67918. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  67919. + } else {
  67920. + ep->xfer_len =
  67921. + (ep->total_len - ep->xfer_count) >
  67922. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  67923. + ep->xfer_count);
  67924. +
  67925. + dma_desc = core_if->dev_if->in_desc_addr;
  67926. +
  67927. + /** DMA Descriptor Setup */
  67928. + dma_desc->status.b.bs = BS_HOST_BUSY;
  67929. + dma_desc->status.b.l = 1;
  67930. + dma_desc->status.b.ioc = 1;
  67931. + dma_desc->status.b.sp =
  67932. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  67933. + dma_desc->status.b.bytes = ep->xfer_len;
  67934. + dma_desc->buf = ep->dma_addr;
  67935. + dma_desc->status.b.sts = 0;
  67936. + dma_desc->status.b.bs = BS_HOST_READY;
  67937. +
  67938. + /** DIEPDMA0 Register write */
  67939. + DWC_WRITE_REG32(&in_regs->diepdma,
  67940. + core_if->dev_if->dma_in_desc_addr);
  67941. + }
  67942. +
  67943. + DWC_DEBUGPL(DBG_PCDV,
  67944. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  67945. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  67946. + deptsiz.d32);
  67947. +
  67948. + /* Write the DMA register */
  67949. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  67950. + if (core_if->dma_desc_enable == 0)
  67951. + DWC_WRITE_REG32(&(in_regs->diepdma),
  67952. + (uint32_t) ep->dma_addr);
  67953. + }
  67954. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  67955. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  67956. + /* EP enable, IN data in FIFO */
  67957. + depctl.b.cnak = 1;
  67958. + depctl.b.epena = 1;
  67959. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  67960. +
  67961. + /**
  67962. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  67963. + * data will be written into the fifo by the ISR.
  67964. + */
  67965. + if (!core_if->dma_enable) {
  67966. + if (core_if->en_multiple_tx_fifo == 0) {
  67967. + /* First clear it from GINTSTS */
  67968. + intr_mask.b.nptxfempty = 1;
  67969. + DWC_MODIFY_REG32(&core_if->
  67970. + core_global_regs->gintmsk,
  67971. + intr_mask.d32, intr_mask.d32);
  67972. +
  67973. + } else {
  67974. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  67975. + if (ep->xfer_len > 0) {
  67976. + uint32_t fifoemptymsk = 0;
  67977. + fifoemptymsk |= 1 << ep->num;
  67978. + DWC_MODIFY_REG32(&core_if->
  67979. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  67980. + 0, fifoemptymsk);
  67981. + }
  67982. + }
  67983. + }
  67984. + } else {
  67985. + dwc_otg_dev_out_ep_regs_t *out_regs =
  67986. + core_if->dev_if->out_ep_regs[0];
  67987. +
  67988. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  67989. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  67990. +
  67991. + /* Program the transfer size and packet count
  67992. + * as follows: xfersize = N * maxpacket +
  67993. + * short_packet pktcnt = N + (short_packet
  67994. + * exist ? 1 : 0)
  67995. + */
  67996. + deptsiz.b.xfersize = ep->maxpacket;
  67997. + deptsiz.b.pktcnt = 1;
  67998. +
  67999. + if (core_if->dma_desc_enable == 0) {
  68000. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  68001. + } else {
  68002. + dma_desc = core_if->dev_if->out_desc_addr;
  68003. +
  68004. + /** DMA Descriptor Setup */
  68005. + dma_desc->status.b.bs = BS_HOST_BUSY;
  68006. + dma_desc->status.b.l = 1;
  68007. + dma_desc->status.b.ioc = 1;
  68008. + dma_desc->status.b.bytes = ep->maxpacket;
  68009. + dma_desc->buf = ep->dma_addr;
  68010. + dma_desc->status.b.sts = 0;
  68011. + dma_desc->status.b.bs = BS_HOST_READY;
  68012. +
  68013. + /** DOEPDMA0 Register write */
  68014. + DWC_WRITE_REG32(&out_regs->doepdma,
  68015. + core_if->dev_if->dma_out_desc_addr);
  68016. + }
  68017. +
  68018. + DWC_DEBUGPL(DBG_PCDV,
  68019. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  68020. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  68021. + deptsiz.d32);
  68022. +
  68023. + /* Write the DMA register */
  68024. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  68025. + if (core_if->dma_desc_enable == 0)
  68026. + DWC_WRITE_REG32(&(out_regs->doepdma),
  68027. + (uint32_t) ep->dma_addr);
  68028. +
  68029. + }
  68030. +
  68031. + /* EP enable, IN data in FIFO */
  68032. + depctl.b.cnak = 1;
  68033. + depctl.b.epena = 1;
  68034. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  68035. +
  68036. + }
  68037. +}
  68038. +
  68039. +#ifdef DEBUG
  68040. +void dump_msg(const u8 * buf, unsigned int length)
  68041. +{
  68042. + unsigned int start, num, i;
  68043. + char line[52], *p;
  68044. +
  68045. + if (length >= 512)
  68046. + return;
  68047. + start = 0;
  68048. + while (length > 0) {
  68049. + num = length < 16u ? length : 16u;
  68050. + p = line;
  68051. + for (i = 0; i < num; ++i) {
  68052. + if (i == 8)
  68053. + *p++ = ' ';
  68054. + DWC_SPRINTF(p, " %02x", buf[i]);
  68055. + p += 3;
  68056. + }
  68057. + *p = 0;
  68058. + DWC_PRINTF("%6x: %s\n", start, line);
  68059. + buf += num;
  68060. + start += num;
  68061. + length -= num;
  68062. + }
  68063. +}
  68064. +#else
  68065. +static inline void dump_msg(const u8 * buf, unsigned int length)
  68066. +{
  68067. +}
  68068. +#endif
  68069. +
  68070. +/**
  68071. + * This function writes a packet into the Tx FIFO associated with the
  68072. + * EP. For non-periodic EPs the non-periodic Tx FIFO is written. For
  68073. + * periodic EPs the periodic Tx FIFO associated with the EP is written
  68074. + * with all packets for the next micro-frame.
  68075. + *
  68076. + * @param core_if Programming view of DWC_otg controller.
  68077. + * @param ep The EP to write packet for.
  68078. + * @param dma Indicates if DMA is being used.
  68079. + */
  68080. +void dwc_otg_ep_write_packet(dwc_otg_core_if_t * core_if, dwc_ep_t * ep,
  68081. + int dma)
  68082. +{
  68083. + /**
  68084. + * The buffer is padded to DWORD on a per packet basis in
  68085. + * slave/dma mode if the MPS is not DWORD aligned. The last
  68086. + * packet, if short, is also padded to a multiple of DWORD.
  68087. + *
  68088. + * ep->xfer_buff always starts DWORD aligned in memory and is a
  68089. + * multiple of DWORD in length
  68090. + *
  68091. + * ep->xfer_len can be any number of bytes
  68092. + *
  68093. + * ep->xfer_count is a multiple of ep->maxpacket until the last
  68094. + * packet
  68095. + *
  68096. + * FIFO access is DWORD */
  68097. +
  68098. + uint32_t i;
  68099. + uint32_t byte_count;
  68100. + uint32_t dword_count;
  68101. + uint32_t *fifo;
  68102. + uint32_t *data_buff = (uint32_t *) ep->xfer_buff;
  68103. +
  68104. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p)\n", __func__, core_if,
  68105. + ep);
  68106. + if (ep->xfer_count >= ep->xfer_len) {
  68107. + DWC_WARN("%s() No data for EP%d!!!\n", __func__, ep->num);
  68108. + return;
  68109. + }
  68110. +
  68111. + /* Find the byte length of the packet either short packet or MPS */
  68112. + if ((ep->xfer_len - ep->xfer_count) < ep->maxpacket) {
  68113. + byte_count = ep->xfer_len - ep->xfer_count;
  68114. + } else {
  68115. + byte_count = ep->maxpacket;
  68116. + }
  68117. +
  68118. + /* Find the DWORD length, padded by extra bytes as neccessary if MPS
  68119. + * is not a multiple of DWORD */
  68120. + dword_count = (byte_count + 3) / 4;
  68121. +
  68122. +#ifdef VERBOSE
  68123. + dump_msg(ep->xfer_buff, byte_count);
  68124. +#endif
  68125. +
  68126. + /**@todo NGS Where are the Periodic Tx FIFO addresses
  68127. + * intialized? What should this be? */
  68128. +
  68129. + fifo = core_if->data_fifo[ep->num];
  68130. +
  68131. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "fifo=%p buff=%p *p=%08x bc=%d\n",
  68132. + fifo, data_buff, *data_buff, byte_count);
  68133. +
  68134. + if (!dma) {
  68135. + for (i = 0; i < dword_count; i++, data_buff++) {
  68136. + DWC_WRITE_REG32(fifo, *data_buff);
  68137. + }
  68138. + }
  68139. +
  68140. + ep->xfer_count += byte_count;
  68141. + ep->xfer_buff += byte_count;
  68142. + ep->dma_addr += byte_count;
  68143. +}
  68144. +
  68145. +/**
  68146. + * Set the EP STALL.
  68147. + *
  68148. + * @param core_if Programming view of DWC_otg controller.
  68149. + * @param ep The EP to set the stall on.
  68150. + */
  68151. +void dwc_otg_ep_set_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  68152. +{
  68153. + depctl_data_t depctl;
  68154. + volatile uint32_t *depctl_addr;
  68155. +
  68156. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  68157. + (ep->is_in ? "IN" : "OUT"));
  68158. +
  68159. + if (ep->is_in == 1) {
  68160. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  68161. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  68162. +
  68163. + /* set the disable and stall bits */
  68164. + if (depctl.b.epena) {
  68165. + depctl.b.epdis = 1;
  68166. + }
  68167. + depctl.b.stall = 1;
  68168. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  68169. + } else {
  68170. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  68171. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  68172. +
  68173. + /* set the stall bit */
  68174. + depctl.b.stall = 1;
  68175. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  68176. + }
  68177. +
  68178. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  68179. +
  68180. + return;
  68181. +}
  68182. +
  68183. +/**
  68184. + * Clear the EP STALL.
  68185. + *
  68186. + * @param core_if Programming view of DWC_otg controller.
  68187. + * @param ep The EP to clear stall from.
  68188. + */
  68189. +void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  68190. +{
  68191. + depctl_data_t depctl;
  68192. + volatile uint32_t *depctl_addr;
  68193. +
  68194. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  68195. + (ep->is_in ? "IN" : "OUT"));
  68196. +
  68197. + if (ep->is_in == 1) {
  68198. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  68199. + } else {
  68200. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  68201. + }
  68202. +
  68203. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  68204. +
  68205. + /* clear the stall bits */
  68206. + depctl.b.stall = 0;
  68207. +
  68208. + /*
  68209. + * USB Spec 9.4.5: For endpoints using data toggle, regardless
  68210. + * of whether an endpoint has the Halt feature set, a
  68211. + * ClearFeature(ENDPOINT_HALT) request always results in the
  68212. + * data toggle being reinitialized to DATA0.
  68213. + */
  68214. + if (ep->type == DWC_OTG_EP_TYPE_INTR ||
  68215. + ep->type == DWC_OTG_EP_TYPE_BULK) {
  68216. + depctl.b.setd0pid = 1; /* DATA0 */
  68217. + }
  68218. +
  68219. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  68220. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  68221. + return;
  68222. +}
  68223. +
  68224. +/**
  68225. + * This function reads a packet from the Rx FIFO into the destination
  68226. + * buffer. To read SETUP data use dwc_otg_read_setup_packet.
  68227. + *
  68228. + * @param core_if Programming view of DWC_otg controller.
  68229. + * @param dest Destination buffer for the packet.
  68230. + * @param bytes Number of bytes to copy to the destination.
  68231. + */
  68232. +void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  68233. + uint8_t * dest, uint16_t bytes)
  68234. +{
  68235. + int i;
  68236. + int word_count = (bytes + 3) / 4;
  68237. +
  68238. + volatile uint32_t *fifo = core_if->data_fifo[0];
  68239. + uint32_t *data_buff = (uint32_t *) dest;
  68240. +
  68241. + /**
  68242. + * @todo Account for the case where _dest is not dword aligned. This
  68243. + * requires reading data from the FIFO into a uint32_t temp buffer,
  68244. + * then moving it into the data buffer.
  68245. + */
  68246. +
  68247. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p,%d)\n", __func__,
  68248. + core_if, dest, bytes);
  68249. +
  68250. + for (i = 0; i < word_count; i++, data_buff++) {
  68251. + *data_buff = DWC_READ_REG32(fifo);
  68252. + }
  68253. +
  68254. + return;
  68255. +}
  68256. +
  68257. +/**
  68258. + * This functions reads the device registers and prints them
  68259. + *
  68260. + * @param core_if Programming view of DWC_otg controller.
  68261. + */
  68262. +void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * core_if)
  68263. +{
  68264. + int i;
  68265. + volatile uint32_t *addr;
  68266. +
  68267. + DWC_PRINTF("Device Global Registers\n");
  68268. + addr = &core_if->dev_if->dev_global_regs->dcfg;
  68269. + DWC_PRINTF("DCFG @0x%08lX : 0x%08X\n",
  68270. + (unsigned long)addr, DWC_READ_REG32(addr));
  68271. + addr = &core_if->dev_if->dev_global_regs->dctl;
  68272. + DWC_PRINTF("DCTL @0x%08lX : 0x%08X\n",
  68273. + (unsigned long)addr, DWC_READ_REG32(addr));
  68274. + addr = &core_if->dev_if->dev_global_regs->dsts;
  68275. + DWC_PRINTF("DSTS @0x%08lX : 0x%08X\n",
  68276. + (unsigned long)addr, DWC_READ_REG32(addr));
  68277. + addr = &core_if->dev_if->dev_global_regs->diepmsk;
  68278. + DWC_PRINTF("DIEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68279. + DWC_READ_REG32(addr));
  68280. + addr = &core_if->dev_if->dev_global_regs->doepmsk;
  68281. + DWC_PRINTF("DOEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68282. + DWC_READ_REG32(addr));
  68283. + addr = &core_if->dev_if->dev_global_regs->daint;
  68284. + DWC_PRINTF("DAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68285. + DWC_READ_REG32(addr));
  68286. + addr = &core_if->dev_if->dev_global_regs->daintmsk;
  68287. + DWC_PRINTF("DAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68288. + DWC_READ_REG32(addr));
  68289. + addr = &core_if->dev_if->dev_global_regs->dtknqr1;
  68290. + DWC_PRINTF("DTKNQR1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68291. + DWC_READ_REG32(addr));
  68292. + if (core_if->hwcfg2.b.dev_token_q_depth > 6) {
  68293. + addr = &core_if->dev_if->dev_global_regs->dtknqr2;
  68294. + DWC_PRINTF("DTKNQR2 @0x%08lX : 0x%08X\n",
  68295. + (unsigned long)addr, DWC_READ_REG32(addr));
  68296. + }
  68297. +
  68298. + addr = &core_if->dev_if->dev_global_regs->dvbusdis;
  68299. + DWC_PRINTF("DVBUSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68300. + DWC_READ_REG32(addr));
  68301. +
  68302. + addr = &core_if->dev_if->dev_global_regs->dvbuspulse;
  68303. + DWC_PRINTF("DVBUSPULSE @0x%08lX : 0x%08X\n",
  68304. + (unsigned long)addr, DWC_READ_REG32(addr));
  68305. +
  68306. + addr = &core_if->dev_if->dev_global_regs->dtknqr3_dthrctl;
  68307. + DWC_PRINTF("DTKNQR3_DTHRCTL @0x%08lX : 0x%08X\n",
  68308. + (unsigned long)addr, DWC_READ_REG32(addr));
  68309. +
  68310. + if (core_if->hwcfg2.b.dev_token_q_depth > 22) {
  68311. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  68312. + DWC_PRINTF("DTKNQR4 @0x%08lX : 0x%08X\n",
  68313. + (unsigned long)addr, DWC_READ_REG32(addr));
  68314. + }
  68315. +
  68316. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  68317. + DWC_PRINTF("FIFOEMPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68318. + DWC_READ_REG32(addr));
  68319. +
  68320. + if (core_if->hwcfg2.b.multi_proc_int) {
  68321. +
  68322. + addr = &core_if->dev_if->dev_global_regs->deachint;
  68323. + DWC_PRINTF("DEACHINT @0x%08lX : 0x%08X\n",
  68324. + (unsigned long)addr, DWC_READ_REG32(addr));
  68325. + addr = &core_if->dev_if->dev_global_regs->deachintmsk;
  68326. + DWC_PRINTF("DEACHINTMSK @0x%08lX : 0x%08X\n",
  68327. + (unsigned long)addr, DWC_READ_REG32(addr));
  68328. +
  68329. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  68330. + addr =
  68331. + &core_if->dev_if->
  68332. + dev_global_regs->diepeachintmsk[i];
  68333. + DWC_PRINTF("DIEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  68334. + i, (unsigned long)addr,
  68335. + DWC_READ_REG32(addr));
  68336. + }
  68337. +
  68338. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  68339. + addr =
  68340. + &core_if->dev_if->
  68341. + dev_global_regs->doepeachintmsk[i];
  68342. + DWC_PRINTF("DOEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  68343. + i, (unsigned long)addr,
  68344. + DWC_READ_REG32(addr));
  68345. + }
  68346. + }
  68347. +
  68348. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  68349. + DWC_PRINTF("Device IN EP %d Registers\n", i);
  68350. + addr = &core_if->dev_if->in_ep_regs[i]->diepctl;
  68351. + DWC_PRINTF("DIEPCTL @0x%08lX : 0x%08X\n",
  68352. + (unsigned long)addr, DWC_READ_REG32(addr));
  68353. + addr = &core_if->dev_if->in_ep_regs[i]->diepint;
  68354. + DWC_PRINTF("DIEPINT @0x%08lX : 0x%08X\n",
  68355. + (unsigned long)addr, DWC_READ_REG32(addr));
  68356. + addr = &core_if->dev_if->in_ep_regs[i]->dieptsiz;
  68357. + DWC_PRINTF("DIETSIZ @0x%08lX : 0x%08X\n",
  68358. + (unsigned long)addr, DWC_READ_REG32(addr));
  68359. + addr = &core_if->dev_if->in_ep_regs[i]->diepdma;
  68360. + DWC_PRINTF("DIEPDMA @0x%08lX : 0x%08X\n",
  68361. + (unsigned long)addr, DWC_READ_REG32(addr));
  68362. + addr = &core_if->dev_if->in_ep_regs[i]->dtxfsts;
  68363. + DWC_PRINTF("DTXFSTS @0x%08lX : 0x%08X\n",
  68364. + (unsigned long)addr, DWC_READ_REG32(addr));
  68365. + addr = &core_if->dev_if->in_ep_regs[i]->diepdmab;
  68366. + DWC_PRINTF("DIEPDMAB @0x%08lX : 0x%08X\n",
  68367. + (unsigned long)addr, 0 /*DWC_READ_REG32(addr) */ );
  68368. + }
  68369. +
  68370. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  68371. + DWC_PRINTF("Device OUT EP %d Registers\n", i);
  68372. + addr = &core_if->dev_if->out_ep_regs[i]->doepctl;
  68373. + DWC_PRINTF("DOEPCTL @0x%08lX : 0x%08X\n",
  68374. + (unsigned long)addr, DWC_READ_REG32(addr));
  68375. + addr = &core_if->dev_if->out_ep_regs[i]->doepint;
  68376. + DWC_PRINTF("DOEPINT @0x%08lX : 0x%08X\n",
  68377. + (unsigned long)addr, DWC_READ_REG32(addr));
  68378. + addr = &core_if->dev_if->out_ep_regs[i]->doeptsiz;
  68379. + DWC_PRINTF("DOETSIZ @0x%08lX : 0x%08X\n",
  68380. + (unsigned long)addr, DWC_READ_REG32(addr));
  68381. + addr = &core_if->dev_if->out_ep_regs[i]->doepdma;
  68382. + DWC_PRINTF("DOEPDMA @0x%08lX : 0x%08X\n",
  68383. + (unsigned long)addr, DWC_READ_REG32(addr));
  68384. + if (core_if->dma_enable) { /* Don't access this register in SLAVE mode */
  68385. + addr = &core_if->dev_if->out_ep_regs[i]->doepdmab;
  68386. + DWC_PRINTF("DOEPDMAB @0x%08lX : 0x%08X\n",
  68387. + (unsigned long)addr, DWC_READ_REG32(addr));
  68388. + }
  68389. +
  68390. + }
  68391. +}
  68392. +
  68393. +/**
  68394. + * This functions reads the SPRAM and prints its content
  68395. + *
  68396. + * @param core_if Programming view of DWC_otg controller.
  68397. + */
  68398. +void dwc_otg_dump_spram(dwc_otg_core_if_t * core_if)
  68399. +{
  68400. + volatile uint8_t *addr, *start_addr, *end_addr;
  68401. +
  68402. + DWC_PRINTF("SPRAM Data:\n");
  68403. + start_addr = (void *)core_if->core_global_regs;
  68404. + DWC_PRINTF("Base Address: 0x%8lX\n", (unsigned long)start_addr);
  68405. + start_addr += 0x00028000;
  68406. + end_addr = (void *)core_if->core_global_regs;
  68407. + end_addr += 0x000280e0;
  68408. +
  68409. + for (addr = start_addr; addr < end_addr; addr += 16) {
  68410. + DWC_PRINTF
  68411. + ("0x%8lX:\t%2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X\n",
  68412. + (unsigned long)addr, addr[0], addr[1], addr[2], addr[3],
  68413. + addr[4], addr[5], addr[6], addr[7], addr[8], addr[9],
  68414. + addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]
  68415. + );
  68416. + }
  68417. +
  68418. + return;
  68419. +}
  68420. +
  68421. +/**
  68422. + * This function reads the host registers and prints them
  68423. + *
  68424. + * @param core_if Programming view of DWC_otg controller.
  68425. + */
  68426. +void dwc_otg_dump_host_registers(dwc_otg_core_if_t * core_if)
  68427. +{
  68428. + int i;
  68429. + volatile uint32_t *addr;
  68430. +
  68431. + DWC_PRINTF("Host Global Registers\n");
  68432. + addr = &core_if->host_if->host_global_regs->hcfg;
  68433. + DWC_PRINTF("HCFG @0x%08lX : 0x%08X\n",
  68434. + (unsigned long)addr, DWC_READ_REG32(addr));
  68435. + addr = &core_if->host_if->host_global_regs->hfir;
  68436. + DWC_PRINTF("HFIR @0x%08lX : 0x%08X\n",
  68437. + (unsigned long)addr, DWC_READ_REG32(addr));
  68438. + addr = &core_if->host_if->host_global_regs->hfnum;
  68439. + DWC_PRINTF("HFNUM @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68440. + DWC_READ_REG32(addr));
  68441. + addr = &core_if->host_if->host_global_regs->hptxsts;
  68442. + DWC_PRINTF("HPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68443. + DWC_READ_REG32(addr));
  68444. + addr = &core_if->host_if->host_global_regs->haint;
  68445. + DWC_PRINTF("HAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68446. + DWC_READ_REG32(addr));
  68447. + addr = &core_if->host_if->host_global_regs->haintmsk;
  68448. + DWC_PRINTF("HAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68449. + DWC_READ_REG32(addr));
  68450. + if (core_if->dma_desc_enable) {
  68451. + addr = &core_if->host_if->host_global_regs->hflbaddr;
  68452. + DWC_PRINTF("HFLBADDR @0x%08lX : 0x%08X\n",
  68453. + (unsigned long)addr, DWC_READ_REG32(addr));
  68454. + }
  68455. +
  68456. + addr = core_if->host_if->hprt0;
  68457. + DWC_PRINTF("HPRT0 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68458. + DWC_READ_REG32(addr));
  68459. +
  68460. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  68461. + DWC_PRINTF("Host Channel %d Specific Registers\n", i);
  68462. + addr = &core_if->host_if->hc_regs[i]->hcchar;
  68463. + DWC_PRINTF("HCCHAR @0x%08lX : 0x%08X\n",
  68464. + (unsigned long)addr, DWC_READ_REG32(addr));
  68465. + addr = &core_if->host_if->hc_regs[i]->hcsplt;
  68466. + DWC_PRINTF("HCSPLT @0x%08lX : 0x%08X\n",
  68467. + (unsigned long)addr, DWC_READ_REG32(addr));
  68468. + addr = &core_if->host_if->hc_regs[i]->hcint;
  68469. + DWC_PRINTF("HCINT @0x%08lX : 0x%08X\n",
  68470. + (unsigned long)addr, DWC_READ_REG32(addr));
  68471. + addr = &core_if->host_if->hc_regs[i]->hcintmsk;
  68472. + DWC_PRINTF("HCINTMSK @0x%08lX : 0x%08X\n",
  68473. + (unsigned long)addr, DWC_READ_REG32(addr));
  68474. + addr = &core_if->host_if->hc_regs[i]->hctsiz;
  68475. + DWC_PRINTF("HCTSIZ @0x%08lX : 0x%08X\n",
  68476. + (unsigned long)addr, DWC_READ_REG32(addr));
  68477. + addr = &core_if->host_if->hc_regs[i]->hcdma;
  68478. + DWC_PRINTF("HCDMA @0x%08lX : 0x%08X\n",
  68479. + (unsigned long)addr, DWC_READ_REG32(addr));
  68480. + if (core_if->dma_desc_enable) {
  68481. + addr = &core_if->host_if->hc_regs[i]->hcdmab;
  68482. + DWC_PRINTF("HCDMAB @0x%08lX : 0x%08X\n",
  68483. + (unsigned long)addr, DWC_READ_REG32(addr));
  68484. + }
  68485. +
  68486. + }
  68487. + return;
  68488. +}
  68489. +
  68490. +/**
  68491. + * This function reads the core global registers and prints them
  68492. + *
  68493. + * @param core_if Programming view of DWC_otg controller.
  68494. + */
  68495. +void dwc_otg_dump_global_registers(dwc_otg_core_if_t * core_if)
  68496. +{
  68497. + int i, ep_num;
  68498. + volatile uint32_t *addr;
  68499. + char *txfsiz;
  68500. +
  68501. + DWC_PRINTF("Core Global Registers\n");
  68502. + addr = &core_if->core_global_regs->gotgctl;
  68503. + DWC_PRINTF("GOTGCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68504. + DWC_READ_REG32(addr));
  68505. + addr = &core_if->core_global_regs->gotgint;
  68506. + DWC_PRINTF("GOTGINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68507. + DWC_READ_REG32(addr));
  68508. + addr = &core_if->core_global_regs->gahbcfg;
  68509. + DWC_PRINTF("GAHBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68510. + DWC_READ_REG32(addr));
  68511. + addr = &core_if->core_global_regs->gusbcfg;
  68512. + DWC_PRINTF("GUSBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68513. + DWC_READ_REG32(addr));
  68514. + addr = &core_if->core_global_regs->grstctl;
  68515. + DWC_PRINTF("GRSTCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68516. + DWC_READ_REG32(addr));
  68517. + addr = &core_if->core_global_regs->gintsts;
  68518. + DWC_PRINTF("GINTSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68519. + DWC_READ_REG32(addr));
  68520. + addr = &core_if->core_global_regs->gintmsk;
  68521. + DWC_PRINTF("GINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68522. + DWC_READ_REG32(addr));
  68523. + addr = &core_if->core_global_regs->grxstsr;
  68524. + DWC_PRINTF("GRXSTSR @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68525. + DWC_READ_REG32(addr));
  68526. + addr = &core_if->core_global_regs->grxfsiz;
  68527. + DWC_PRINTF("GRXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68528. + DWC_READ_REG32(addr));
  68529. + addr = &core_if->core_global_regs->gnptxfsiz;
  68530. + DWC_PRINTF("GNPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68531. + DWC_READ_REG32(addr));
  68532. + addr = &core_if->core_global_regs->gnptxsts;
  68533. + DWC_PRINTF("GNPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68534. + DWC_READ_REG32(addr));
  68535. + addr = &core_if->core_global_regs->gi2cctl;
  68536. + DWC_PRINTF("GI2CCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68537. + DWC_READ_REG32(addr));
  68538. + addr = &core_if->core_global_regs->gpvndctl;
  68539. + DWC_PRINTF("GPVNDCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68540. + DWC_READ_REG32(addr));
  68541. + addr = &core_if->core_global_regs->ggpio;
  68542. + DWC_PRINTF("GGPIO @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68543. + DWC_READ_REG32(addr));
  68544. + addr = &core_if->core_global_regs->guid;
  68545. + DWC_PRINTF("GUID @0x%08lX : 0x%08X\n",
  68546. + (unsigned long)addr, DWC_READ_REG32(addr));
  68547. + addr = &core_if->core_global_regs->gsnpsid;
  68548. + DWC_PRINTF("GSNPSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68549. + DWC_READ_REG32(addr));
  68550. + addr = &core_if->core_global_regs->ghwcfg1;
  68551. + DWC_PRINTF("GHWCFG1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68552. + DWC_READ_REG32(addr));
  68553. + addr = &core_if->core_global_regs->ghwcfg2;
  68554. + DWC_PRINTF("GHWCFG2 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68555. + DWC_READ_REG32(addr));
  68556. + addr = &core_if->core_global_regs->ghwcfg3;
  68557. + DWC_PRINTF("GHWCFG3 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68558. + DWC_READ_REG32(addr));
  68559. + addr = &core_if->core_global_regs->ghwcfg4;
  68560. + DWC_PRINTF("GHWCFG4 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68561. + DWC_READ_REG32(addr));
  68562. + addr = &core_if->core_global_regs->glpmcfg;
  68563. + DWC_PRINTF("GLPMCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68564. + DWC_READ_REG32(addr));
  68565. + addr = &core_if->core_global_regs->gpwrdn;
  68566. + DWC_PRINTF("GPWRDN @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68567. + DWC_READ_REG32(addr));
  68568. + addr = &core_if->core_global_regs->gdfifocfg;
  68569. + DWC_PRINTF("GDFIFOCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68570. + DWC_READ_REG32(addr));
  68571. + addr = &core_if->core_global_regs->adpctl;
  68572. + DWC_PRINTF("ADPCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68573. + dwc_otg_adp_read_reg(core_if));
  68574. + addr = &core_if->core_global_regs->hptxfsiz;
  68575. + DWC_PRINTF("HPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68576. + DWC_READ_REG32(addr));
  68577. +
  68578. + if (core_if->en_multiple_tx_fifo == 0) {
  68579. + ep_num = core_if->hwcfg4.b.num_dev_perio_in_ep;
  68580. + txfsiz = "DPTXFSIZ";
  68581. + } else {
  68582. + ep_num = core_if->hwcfg4.b.num_in_eps;
  68583. + txfsiz = "DIENPTXF";
  68584. + }
  68585. + for (i = 0; i < ep_num; i++) {
  68586. + addr = &core_if->core_global_regs->dtxfsiz[i];
  68587. + DWC_PRINTF("%s[%d] @0x%08lX : 0x%08X\n", txfsiz, i + 1,
  68588. + (unsigned long)addr, DWC_READ_REG32(addr));
  68589. + }
  68590. + addr = core_if->pcgcctl;
  68591. + DWC_PRINTF("PCGCCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  68592. + DWC_READ_REG32(addr));
  68593. +}
  68594. +
  68595. +/**
  68596. + * Flush a Tx FIFO.
  68597. + *
  68598. + * @param core_if Programming view of DWC_otg controller.
  68599. + * @param num Tx FIFO to flush.
  68600. + */
  68601. +void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * core_if, const int num)
  68602. +{
  68603. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  68604. + volatile grstctl_t greset = {.d32 = 0 };
  68605. + int count = 0;
  68606. +
  68607. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "Flush Tx FIFO %d\n", num);
  68608. +
  68609. + greset.b.txfflsh = 1;
  68610. + greset.b.txfnum = num;
  68611. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  68612. +
  68613. + do {
  68614. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  68615. + if (++count > 10000) {
  68616. + DWC_WARN("%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
  68617. + __func__, greset.d32,
  68618. + DWC_READ_REG32(&global_regs->gnptxsts));
  68619. + break;
  68620. + }
  68621. + dwc_udelay(1);
  68622. + } while (greset.b.txfflsh == 1);
  68623. +
  68624. + /* Wait for 3 PHY Clocks */
  68625. + dwc_udelay(1);
  68626. +}
  68627. +
  68628. +/**
  68629. + * Flush Rx FIFO.
  68630. + *
  68631. + * @param core_if Programming view of DWC_otg controller.
  68632. + */
  68633. +void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * core_if)
  68634. +{
  68635. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  68636. + volatile grstctl_t greset = {.d32 = 0 };
  68637. + int count = 0;
  68638. +
  68639. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "%s\n", __func__);
  68640. + /*
  68641. + *
  68642. + */
  68643. + greset.b.rxfflsh = 1;
  68644. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  68645. +
  68646. + do {
  68647. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  68648. + if (++count > 10000) {
  68649. + DWC_WARN("%s() HANG! GRSTCTL=%0x\n", __func__,
  68650. + greset.d32);
  68651. + break;
  68652. + }
  68653. + dwc_udelay(1);
  68654. + } while (greset.b.rxfflsh == 1);
  68655. +
  68656. + /* Wait for 3 PHY Clocks */
  68657. + dwc_udelay(1);
  68658. +}
  68659. +
  68660. +/**
  68661. + * Do core a soft reset of the core. Be careful with this because it
  68662. + * resets all the internal state machines of the core.
  68663. + */
  68664. +void dwc_otg_core_reset(dwc_otg_core_if_t * core_if)
  68665. +{
  68666. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  68667. + volatile grstctl_t greset = {.d32 = 0 };
  68668. + int count = 0;
  68669. +
  68670. + DWC_DEBUGPL(DBG_CILV, "%s\n", __func__);
  68671. + /* Wait for AHB master IDLE state. */
  68672. + do {
  68673. + dwc_udelay(10);
  68674. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  68675. + if (++count > 100000) {
  68676. + DWC_WARN("%s() HANG! AHB Idle GRSTCTL=%0x\n", __func__,
  68677. + greset.d32);
  68678. + return;
  68679. + }
  68680. + }
  68681. + while (greset.b.ahbidle == 0);
  68682. +
  68683. + /* Core Soft Reset */
  68684. + count = 0;
  68685. + greset.b.csftrst = 1;
  68686. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  68687. + do {
  68688. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  68689. + if (++count > 10000) {
  68690. + DWC_WARN("%s() HANG! Soft Reset GRSTCTL=%0x\n",
  68691. + __func__, greset.d32);
  68692. + break;
  68693. + }
  68694. + dwc_udelay(1);
  68695. + }
  68696. + while (greset.b.csftrst == 1);
  68697. +
  68698. + /* Wait for 3 PHY Clocks */
  68699. + dwc_mdelay(100);
  68700. +}
  68701. +
  68702. +uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if)
  68703. +{
  68704. + return (dwc_otg_mode(_core_if) != DWC_HOST_MODE);
  68705. +}
  68706. +
  68707. +uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if)
  68708. +{
  68709. + return (dwc_otg_mode(_core_if) == DWC_HOST_MODE);
  68710. +}
  68711. +
  68712. +/**
  68713. + * Register HCD callbacks. The callbacks are used to start and stop
  68714. + * the HCD for interrupt processing.
  68715. + *
  68716. + * @param core_if Programming view of DWC_otg controller.
  68717. + * @param cb the HCD callback structure.
  68718. + * @param p pointer to be passed to callback function (usb_hcd*).
  68719. + */
  68720. +void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * core_if,
  68721. + dwc_otg_cil_callbacks_t * cb, void *p)
  68722. +{
  68723. + core_if->hcd_cb = cb;
  68724. + cb->p = p;
  68725. +}
  68726. +
  68727. +/**
  68728. + * Register PCD callbacks. The callbacks are used to start and stop
  68729. + * the PCD for interrupt processing.
  68730. + *
  68731. + * @param core_if Programming view of DWC_otg controller.
  68732. + * @param cb the PCD callback structure.
  68733. + * @param p pointer to be passed to callback function (pcd*).
  68734. + */
  68735. +void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * core_if,
  68736. + dwc_otg_cil_callbacks_t * cb, void *p)
  68737. +{
  68738. + core_if->pcd_cb = cb;
  68739. + cb->p = p;
  68740. +}
  68741. +
  68742. +#ifdef DWC_EN_ISOC
  68743. +
  68744. +/**
  68745. + * This function writes isoc data per 1 (micro)frame into tx fifo
  68746. + *
  68747. + * @param core_if Programming view of DWC_otg controller.
  68748. + * @param ep The EP to start the transfer on.
  68749. + *
  68750. + */
  68751. +void write_isoc_frame_data(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  68752. +{
  68753. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  68754. + dtxfsts_data_t txstatus = {.d32 = 0 };
  68755. + uint32_t len = 0;
  68756. + uint32_t dwords;
  68757. +
  68758. + ep->xfer_len = ep->data_per_frame;
  68759. + ep->xfer_count = 0;
  68760. +
  68761. + ep_regs = core_if->dev_if->in_ep_regs[ep->num];
  68762. +
  68763. + len = ep->xfer_len - ep->xfer_count;
  68764. +
  68765. + if (len > ep->maxpacket) {
  68766. + len = ep->maxpacket;
  68767. + }
  68768. +
  68769. + dwords = (len + 3) / 4;
  68770. +
  68771. + /* While there is space in the queue and space in the FIFO and
  68772. + * More data to tranfer, Write packets to the Tx FIFO */
  68773. + txstatus.d32 =
  68774. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dtxfsts);
  68775. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", ep->num, txstatus.d32);
  68776. +
  68777. + while (txstatus.b.txfspcavail > dwords &&
  68778. + ep->xfer_count < ep->xfer_len && ep->xfer_len != 0) {
  68779. + /* Write the FIFO */
  68780. + dwc_otg_ep_write_packet(core_if, ep, 0);
  68781. +
  68782. + len = ep->xfer_len - ep->xfer_count;
  68783. + if (len > ep->maxpacket) {
  68784. + len = ep->maxpacket;
  68785. + }
  68786. +
  68787. + dwords = (len + 3) / 4;
  68788. + txstatus.d32 =
  68789. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  68790. + dtxfsts);
  68791. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", ep->num,
  68792. + txstatus.d32);
  68793. + }
  68794. +}
  68795. +
  68796. +/**
  68797. + * This function initializes a descriptor chain for Isochronous transfer
  68798. + *
  68799. + * @param core_if Programming view of DWC_otg controller.
  68800. + * @param ep The EP to start the transfer on.
  68801. + *
  68802. + */
  68803. +void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  68804. + dwc_ep_t * ep)
  68805. +{
  68806. + deptsiz_data_t deptsiz = {.d32 = 0 };
  68807. + depctl_data_t depctl = {.d32 = 0 };
  68808. + dsts_data_t dsts = {.d32 = 0 };
  68809. + volatile uint32_t *addr;
  68810. +
  68811. + if (ep->is_in) {
  68812. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  68813. + } else {
  68814. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  68815. + }
  68816. +
  68817. + ep->xfer_len = ep->data_per_frame;
  68818. + ep->xfer_count = 0;
  68819. + ep->xfer_buff = ep->cur_pkt_addr;
  68820. + ep->dma_addr = ep->cur_pkt_dma_addr;
  68821. +
  68822. + if (ep->is_in) {
  68823. + /* Program the transfer size and packet count
  68824. + * as follows: xfersize = N * maxpacket +
  68825. + * short_packet pktcnt = N + (short_packet
  68826. + * exist ? 1 : 0)
  68827. + */
  68828. + deptsiz.b.xfersize = ep->xfer_len;
  68829. + deptsiz.b.pktcnt =
  68830. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  68831. + deptsiz.b.mc = deptsiz.b.pktcnt;
  68832. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dieptsiz,
  68833. + deptsiz.d32);
  68834. +
  68835. + /* Write the DMA register */
  68836. + if (core_if->dma_enable) {
  68837. + DWC_WRITE_REG32(&
  68838. + (core_if->dev_if->in_ep_regs[ep->num]->
  68839. + diepdma), (uint32_t) ep->dma_addr);
  68840. + }
  68841. + } else {
  68842. + deptsiz.b.pktcnt =
  68843. + (ep->xfer_len + (ep->maxpacket - 1)) / ep->maxpacket;
  68844. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  68845. +
  68846. + DWC_WRITE_REG32(&core_if->dev_if->
  68847. + out_ep_regs[ep->num]->doeptsiz, deptsiz.d32);
  68848. +
  68849. + if (core_if->dma_enable) {
  68850. + DWC_WRITE_REG32(&
  68851. + (core_if->dev_if->
  68852. + out_ep_regs[ep->num]->doepdma),
  68853. + (uint32_t) ep->dma_addr);
  68854. + }
  68855. + }
  68856. +
  68857. + /** Enable endpoint, clear nak */
  68858. +
  68859. + depctl.d32 = 0;
  68860. + if (ep->bInterval == 1) {
  68861. + dsts.d32 =
  68862. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  68863. + ep->next_frame = dsts.b.soffn + ep->bInterval;
  68864. +
  68865. + if (ep->next_frame & 0x1) {
  68866. + depctl.b.setd1pid = 1;
  68867. + } else {
  68868. + depctl.b.setd0pid = 1;
  68869. + }
  68870. + } else {
  68871. + ep->next_frame += ep->bInterval;
  68872. +
  68873. + if (ep->next_frame & 0x1) {
  68874. + depctl.b.setd1pid = 1;
  68875. + } else {
  68876. + depctl.b.setd0pid = 1;
  68877. + }
  68878. + }
  68879. + depctl.b.epena = 1;
  68880. + depctl.b.cnak = 1;
  68881. +
  68882. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  68883. + depctl.d32 = DWC_READ_REG32(addr);
  68884. +
  68885. + if (ep->is_in && core_if->dma_enable == 0) {
  68886. + write_isoc_frame_data(core_if, ep);
  68887. + }
  68888. +
  68889. +}
  68890. +#endif /* DWC_EN_ISOC */
  68891. +
  68892. +static void dwc_otg_set_uninitialized(int32_t * p, int size)
  68893. +{
  68894. + int i;
  68895. + for (i = 0; i < size; i++) {
  68896. + p[i] = -1;
  68897. + }
  68898. +}
  68899. +
  68900. +static int dwc_otg_param_initialized(int32_t val)
  68901. +{
  68902. + return val != -1;
  68903. +}
  68904. +
  68905. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if)
  68906. +{
  68907. + int i;
  68908. + core_if->core_params = DWC_ALLOC(sizeof(*core_if->core_params));
  68909. + if (!core_if->core_params) {
  68910. + return -DWC_E_NO_MEMORY;
  68911. + }
  68912. + dwc_otg_set_uninitialized((int32_t *) core_if->core_params,
  68913. + sizeof(*core_if->core_params) /
  68914. + sizeof(int32_t));
  68915. + DWC_PRINTF("Setting default values for core params\n");
  68916. + dwc_otg_set_param_otg_cap(core_if, dwc_param_otg_cap_default);
  68917. + dwc_otg_set_param_dma_enable(core_if, dwc_param_dma_enable_default);
  68918. + dwc_otg_set_param_dma_desc_enable(core_if,
  68919. + dwc_param_dma_desc_enable_default);
  68920. + dwc_otg_set_param_opt(core_if, dwc_param_opt_default);
  68921. + dwc_otg_set_param_dma_burst_size(core_if,
  68922. + dwc_param_dma_burst_size_default);
  68923. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  68924. + dwc_param_host_support_fs_ls_low_power_default);
  68925. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  68926. + dwc_param_enable_dynamic_fifo_default);
  68927. + dwc_otg_set_param_data_fifo_size(core_if,
  68928. + dwc_param_data_fifo_size_default);
  68929. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  68930. + dwc_param_dev_rx_fifo_size_default);
  68931. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  68932. + dwc_param_dev_nperio_tx_fifo_size_default);
  68933. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  68934. + dwc_param_host_rx_fifo_size_default);
  68935. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  68936. + dwc_param_host_nperio_tx_fifo_size_default);
  68937. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  68938. + dwc_param_host_perio_tx_fifo_size_default);
  68939. + dwc_otg_set_param_max_transfer_size(core_if,
  68940. + dwc_param_max_transfer_size_default);
  68941. + dwc_otg_set_param_max_packet_count(core_if,
  68942. + dwc_param_max_packet_count_default);
  68943. + dwc_otg_set_param_host_channels(core_if,
  68944. + dwc_param_host_channels_default);
  68945. + dwc_otg_set_param_dev_endpoints(core_if,
  68946. + dwc_param_dev_endpoints_default);
  68947. + dwc_otg_set_param_phy_type(core_if, dwc_param_phy_type_default);
  68948. + dwc_otg_set_param_speed(core_if, dwc_param_speed_default);
  68949. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  68950. + dwc_param_host_ls_low_power_phy_clk_default);
  68951. + dwc_otg_set_param_phy_ulpi_ddr(core_if, dwc_param_phy_ulpi_ddr_default);
  68952. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  68953. + dwc_param_phy_ulpi_ext_vbus_default);
  68954. + dwc_otg_set_param_phy_utmi_width(core_if,
  68955. + dwc_param_phy_utmi_width_default);
  68956. + dwc_otg_set_param_ts_dline(core_if, dwc_param_ts_dline_default);
  68957. + dwc_otg_set_param_i2c_enable(core_if, dwc_param_i2c_enable_default);
  68958. + dwc_otg_set_param_ulpi_fs_ls(core_if, dwc_param_ulpi_fs_ls_default);
  68959. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  68960. + dwc_param_en_multiple_tx_fifo_default);
  68961. + for (i = 0; i < 15; i++) {
  68962. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  68963. + dwc_param_dev_perio_tx_fifo_size_default,
  68964. + i);
  68965. + }
  68966. +
  68967. + for (i = 0; i < 15; i++) {
  68968. + dwc_otg_set_param_dev_tx_fifo_size(core_if,
  68969. + dwc_param_dev_tx_fifo_size_default,
  68970. + i);
  68971. + }
  68972. + dwc_otg_set_param_thr_ctl(core_if, dwc_param_thr_ctl_default);
  68973. + dwc_otg_set_param_mpi_enable(core_if, dwc_param_mpi_enable_default);
  68974. + dwc_otg_set_param_pti_enable(core_if, dwc_param_pti_enable_default);
  68975. + dwc_otg_set_param_lpm_enable(core_if, dwc_param_lpm_enable_default);
  68976. + dwc_otg_set_param_ic_usb_cap(core_if, dwc_param_ic_usb_cap_default);
  68977. + dwc_otg_set_param_tx_thr_length(core_if,
  68978. + dwc_param_tx_thr_length_default);
  68979. + dwc_otg_set_param_rx_thr_length(core_if,
  68980. + dwc_param_rx_thr_length_default);
  68981. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  68982. + dwc_param_ahb_thr_ratio_default);
  68983. + dwc_otg_set_param_power_down(core_if, dwc_param_power_down_default);
  68984. + dwc_otg_set_param_reload_ctl(core_if, dwc_param_reload_ctl_default);
  68985. + dwc_otg_set_param_dev_out_nak(core_if, dwc_param_dev_out_nak_default);
  68986. + dwc_otg_set_param_cont_on_bna(core_if, dwc_param_cont_on_bna_default);
  68987. + dwc_otg_set_param_ahb_single(core_if, dwc_param_ahb_single_default);
  68988. + dwc_otg_set_param_otg_ver(core_if, dwc_param_otg_ver_default);
  68989. + dwc_otg_set_param_adp_enable(core_if, dwc_param_adp_enable_default);
  68990. + DWC_PRINTF("Finished setting default values for core params\n");
  68991. +
  68992. + return 0;
  68993. +}
  68994. +
  68995. +uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if)
  68996. +{
  68997. + return core_if->dma_enable;
  68998. +}
  68999. +
  69000. +/* Checks if the parameter is outside of its valid range of values */
  69001. +#define DWC_OTG_PARAM_TEST(_param_, _low_, _high_) \
  69002. + (((_param_) < (_low_)) || \
  69003. + ((_param_) > (_high_)))
  69004. +
  69005. +/* Parameter access functions */
  69006. +int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val)
  69007. +{
  69008. + int valid;
  69009. + int retval = 0;
  69010. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  69011. + DWC_WARN("Wrong value for otg_cap parameter\n");
  69012. + DWC_WARN("otg_cap parameter must be 0,1 or 2\n");
  69013. + retval = -DWC_E_INVALID;
  69014. + goto out;
  69015. + }
  69016. +
  69017. + valid = 1;
  69018. + switch (val) {
  69019. + case DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE:
  69020. + if (core_if->hwcfg2.b.op_mode !=
  69021. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  69022. + valid = 0;
  69023. + break;
  69024. + case DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE:
  69025. + if ((core_if->hwcfg2.b.op_mode !=
  69026. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  69027. + && (core_if->hwcfg2.b.op_mode !=
  69028. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  69029. + && (core_if->hwcfg2.b.op_mode !=
  69030. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  69031. + && (core_if->hwcfg2.b.op_mode !=
  69032. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) {
  69033. + valid = 0;
  69034. + }
  69035. + break;
  69036. + case DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE:
  69037. + /* always valid */
  69038. + break;
  69039. + }
  69040. + if (!valid) {
  69041. + if (dwc_otg_param_initialized(core_if->core_params->otg_cap)) {
  69042. + DWC_ERROR
  69043. + ("%d invalid for otg_cap paremter. Check HW configuration.\n",
  69044. + val);
  69045. + }
  69046. + val =
  69047. + (((core_if->hwcfg2.b.op_mode ==
  69048. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  69049. + || (core_if->hwcfg2.b.op_mode ==
  69050. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  69051. + || (core_if->hwcfg2.b.op_mode ==
  69052. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  69053. + || (core_if->hwcfg2.b.op_mode ==
  69054. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) ?
  69055. + DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE :
  69056. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  69057. + retval = -DWC_E_INVALID;
  69058. + }
  69059. +
  69060. + core_if->core_params->otg_cap = val;
  69061. +out:
  69062. + return retval;
  69063. +}
  69064. +
  69065. +int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if)
  69066. +{
  69067. + return core_if->core_params->otg_cap;
  69068. +}
  69069. +
  69070. +int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val)
  69071. +{
  69072. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  69073. + DWC_WARN("Wrong value for opt parameter\n");
  69074. + return -DWC_E_INVALID;
  69075. + }
  69076. + core_if->core_params->opt = val;
  69077. + return 0;
  69078. +}
  69079. +
  69080. +int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if)
  69081. +{
  69082. + return core_if->core_params->opt;
  69083. +}
  69084. +
  69085. +int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if, int32_t val)
  69086. +{
  69087. + int retval = 0;
  69088. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  69089. + DWC_WARN("Wrong value for dma enable\n");
  69090. + return -DWC_E_INVALID;
  69091. + }
  69092. +
  69093. + if ((val == 1) && (core_if->hwcfg2.b.architecture == 0)) {
  69094. + if (dwc_otg_param_initialized(core_if->core_params->dma_enable)) {
  69095. + DWC_ERROR
  69096. + ("%d invalid for dma_enable paremter. Check HW configuration.\n",
  69097. + val);
  69098. + }
  69099. + val = 0;
  69100. + retval = -DWC_E_INVALID;
  69101. + }
  69102. +
  69103. + core_if->core_params->dma_enable = val;
  69104. + if (val == 0) {
  69105. + dwc_otg_set_param_dma_desc_enable(core_if, 0);
  69106. + }
  69107. + return retval;
  69108. +}
  69109. +
  69110. +int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if)
  69111. +{
  69112. + return core_if->core_params->dma_enable;
  69113. +}
  69114. +
  69115. +int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if, int32_t val)
  69116. +{
  69117. + int retval = 0;
  69118. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  69119. + DWC_WARN("Wrong value for dma_enable\n");
  69120. + DWC_WARN("dma_desc_enable must be 0 or 1\n");
  69121. + return -DWC_E_INVALID;
  69122. + }
  69123. +
  69124. + if ((val == 1)
  69125. + && ((dwc_otg_get_param_dma_enable(core_if) == 0)
  69126. + || (core_if->hwcfg4.b.desc_dma == 0))) {
  69127. + if (dwc_otg_param_initialized
  69128. + (core_if->core_params->dma_desc_enable)) {
  69129. + DWC_ERROR
  69130. + ("%d invalid for dma_desc_enable paremter. Check HW configuration.\n",
  69131. + val);
  69132. + }
  69133. + val = 0;
  69134. + retval = -DWC_E_INVALID;
  69135. + }
  69136. + core_if->core_params->dma_desc_enable = val;
  69137. + return retval;
  69138. +}
  69139. +
  69140. +int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if)
  69141. +{
  69142. + return core_if->core_params->dma_desc_enable;
  69143. +}
  69144. +
  69145. +int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t * core_if,
  69146. + int32_t val)
  69147. +{
  69148. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  69149. + DWC_WARN("Wrong value for host_support_fs_low_power\n");
  69150. + DWC_WARN("host_support_fs_low_power must be 0 or 1\n");
  69151. + return -DWC_E_INVALID;
  69152. + }
  69153. + core_if->core_params->host_support_fs_ls_low_power = val;
  69154. + return 0;
  69155. +}
  69156. +
  69157. +int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  69158. + core_if)
  69159. +{
  69160. + return core_if->core_params->host_support_fs_ls_low_power;
  69161. +}
  69162. +
  69163. +int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  69164. + int32_t val)
  69165. +{
  69166. + int retval = 0;
  69167. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  69168. + DWC_WARN("Wrong value for enable_dynamic_fifo\n");
  69169. + DWC_WARN("enable_dynamic_fifo must be 0 or 1\n");
  69170. + return -DWC_E_INVALID;
  69171. + }
  69172. +
  69173. + if ((val == 1) && (core_if->hwcfg2.b.dynamic_fifo == 0)) {
  69174. + if (dwc_otg_param_initialized
  69175. + (core_if->core_params->enable_dynamic_fifo)) {
  69176. + DWC_ERROR
  69177. + ("%d invalid for enable_dynamic_fifo paremter. Check HW configuration.\n",
  69178. + val);
  69179. + }
  69180. + val = 0;
  69181. + retval = -DWC_E_INVALID;
  69182. + }
  69183. + core_if->core_params->enable_dynamic_fifo = val;
  69184. + return retval;
  69185. +}
  69186. +
  69187. +int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if)
  69188. +{
  69189. + return core_if->core_params->enable_dynamic_fifo;
  69190. +}
  69191. +
  69192. +int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  69193. +{
  69194. + int retval = 0;
  69195. + if (DWC_OTG_PARAM_TEST(val, 32, 32768)) {
  69196. + DWC_WARN("Wrong value for data_fifo_size\n");
  69197. + DWC_WARN("data_fifo_size must be 32-32768\n");
  69198. + return -DWC_E_INVALID;
  69199. + }
  69200. +
  69201. + if (val > core_if->hwcfg3.b.dfifo_depth) {
  69202. + if (dwc_otg_param_initialized
  69203. + (core_if->core_params->data_fifo_size)) {
  69204. + DWC_ERROR
  69205. + ("%d invalid for data_fifo_size parameter. Check HW configuration.\n",
  69206. + val);
  69207. + }
  69208. + val = core_if->hwcfg3.b.dfifo_depth;
  69209. + retval = -DWC_E_INVALID;
  69210. + }
  69211. +
  69212. + core_if->core_params->data_fifo_size = val;
  69213. + return retval;
  69214. +}
  69215. +
  69216. +int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if)
  69217. +{
  69218. + return core_if->core_params->data_fifo_size;
  69219. +}
  69220. +
  69221. +int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  69222. +{
  69223. + int retval = 0;
  69224. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  69225. + DWC_WARN("Wrong value for dev_rx_fifo_size\n");
  69226. + DWC_WARN("dev_rx_fifo_size must be 16-32768\n");
  69227. + return -DWC_E_INVALID;
  69228. + }
  69229. +
  69230. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  69231. + if (dwc_otg_param_initialized(core_if->core_params->dev_rx_fifo_size)) {
  69232. + DWC_WARN("%d invalid for dev_rx_fifo_size parameter\n", val);
  69233. + }
  69234. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  69235. + retval = -DWC_E_INVALID;
  69236. + }
  69237. +
  69238. + core_if->core_params->dev_rx_fifo_size = val;
  69239. + return retval;
  69240. +}
  69241. +
  69242. +int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if)
  69243. +{
  69244. + return core_if->core_params->dev_rx_fifo_size;
  69245. +}
  69246. +
  69247. +int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  69248. + int32_t val)
  69249. +{
  69250. + int retval = 0;
  69251. +
  69252. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  69253. + DWC_WARN("Wrong value for dev_nperio_tx_fifo\n");
  69254. + DWC_WARN("dev_nperio_tx_fifo must be 16-32768\n");
  69255. + return -DWC_E_INVALID;
  69256. + }
  69257. +
  69258. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  69259. + if (dwc_otg_param_initialized
  69260. + (core_if->core_params->dev_nperio_tx_fifo_size)) {
  69261. + DWC_ERROR
  69262. + ("%d invalid for dev_nperio_tx_fifo_size. Check HW configuration.\n",
  69263. + val);
  69264. + }
  69265. + val =
  69266. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  69267. + 16);
  69268. + retval = -DWC_E_INVALID;
  69269. + }
  69270. +
  69271. + core_if->core_params->dev_nperio_tx_fifo_size = val;
  69272. + return retval;
  69273. +}
  69274. +
  69275. +int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  69276. +{
  69277. + return core_if->core_params->dev_nperio_tx_fifo_size;
  69278. +}
  69279. +
  69280. +int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  69281. + int32_t val)
  69282. +{
  69283. + int retval = 0;
  69284. +
  69285. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  69286. + DWC_WARN("Wrong value for host_rx_fifo_size\n");
  69287. + DWC_WARN("host_rx_fifo_size must be 16-32768\n");
  69288. + return -DWC_E_INVALID;
  69289. + }
  69290. +
  69291. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  69292. + if (dwc_otg_param_initialized
  69293. + (core_if->core_params->host_rx_fifo_size)) {
  69294. + DWC_ERROR
  69295. + ("%d invalid for host_rx_fifo_size. Check HW configuration.\n",
  69296. + val);
  69297. + }
  69298. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  69299. + retval = -DWC_E_INVALID;
  69300. + }
  69301. +
  69302. + core_if->core_params->host_rx_fifo_size = val;
  69303. + return retval;
  69304. +
  69305. +}
  69306. +
  69307. +int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if)
  69308. +{
  69309. + return core_if->core_params->host_rx_fifo_size;
  69310. +}
  69311. +
  69312. +int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  69313. + int32_t val)
  69314. +{
  69315. + int retval = 0;
  69316. +
  69317. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  69318. + DWC_WARN("Wrong value for host_nperio_tx_fifo_size\n");
  69319. + DWC_WARN("host_nperio_tx_fifo_size must be 16-32768\n");
  69320. + return -DWC_E_INVALID;
  69321. + }
  69322. +
  69323. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  69324. + if (dwc_otg_param_initialized
  69325. + (core_if->core_params->host_nperio_tx_fifo_size)) {
  69326. + DWC_ERROR
  69327. + ("%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
  69328. + val);
  69329. + }
  69330. + val =
  69331. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  69332. + 16);
  69333. + retval = -DWC_E_INVALID;
  69334. + }
  69335. +
  69336. + core_if->core_params->host_nperio_tx_fifo_size = val;
  69337. + return retval;
  69338. +}
  69339. +
  69340. +int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  69341. +{
  69342. + return core_if->core_params->host_nperio_tx_fifo_size;
  69343. +}
  69344. +
  69345. +int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  69346. + int32_t val)
  69347. +{
  69348. + int retval = 0;
  69349. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  69350. + DWC_WARN("Wrong value for host_perio_tx_fifo_size\n");
  69351. + DWC_WARN("host_perio_tx_fifo_size must be 16-32768\n");
  69352. + return -DWC_E_INVALID;
  69353. + }
  69354. +
  69355. + if (val > ((core_if->hptxfsiz.d32) >> 16)) {
  69356. + if (dwc_otg_param_initialized
  69357. + (core_if->core_params->host_perio_tx_fifo_size)) {
  69358. + DWC_ERROR
  69359. + ("%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
  69360. + val);
  69361. + }
  69362. + val = (core_if->hptxfsiz.d32) >> 16;
  69363. + retval = -DWC_E_INVALID;
  69364. + }
  69365. +
  69366. + core_if->core_params->host_perio_tx_fifo_size = val;
  69367. + return retval;
  69368. +}
  69369. +
  69370. +int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  69371. +{
  69372. + return core_if->core_params->host_perio_tx_fifo_size;
  69373. +}
  69374. +
  69375. +int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  69376. + int32_t val)
  69377. +{
  69378. + int retval = 0;
  69379. +
  69380. + if (DWC_OTG_PARAM_TEST(val, 2047, 524288)) {
  69381. + DWC_WARN("Wrong value for max_transfer_size\n");
  69382. + DWC_WARN("max_transfer_size must be 2047-524288\n");
  69383. + return -DWC_E_INVALID;
  69384. + }
  69385. +
  69386. + if (val >= (1 << (core_if->hwcfg3.b.xfer_size_cntr_width + 11))) {
  69387. + if (dwc_otg_param_initialized
  69388. + (core_if->core_params->max_transfer_size)) {
  69389. + DWC_ERROR
  69390. + ("%d invalid for max_transfer_size. Check HW configuration.\n",
  69391. + val);
  69392. + }
  69393. + val =
  69394. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 11)) -
  69395. + 1);
  69396. + retval = -DWC_E_INVALID;
  69397. + }
  69398. +
  69399. + core_if->core_params->max_transfer_size = val;
  69400. + return retval;
  69401. +}
  69402. +
  69403. +int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if)
  69404. +{
  69405. + return core_if->core_params->max_transfer_size;
  69406. +}
  69407. +
  69408. +int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if, int32_t val)
  69409. +{
  69410. + int retval = 0;
  69411. +
  69412. + if (DWC_OTG_PARAM_TEST(val, 15, 511)) {
  69413. + DWC_WARN("Wrong value for max_packet_count\n");
  69414. + DWC_WARN("max_packet_count must be 15-511\n");
  69415. + return -DWC_E_INVALID;
  69416. + }
  69417. +
  69418. + if (val > (1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4))) {
  69419. + if (dwc_otg_param_initialized
  69420. + (core_if->core_params->max_packet_count)) {
  69421. + DWC_ERROR
  69422. + ("%d invalid for max_packet_count. Check HW configuration.\n",
  69423. + val);
  69424. + }
  69425. + val =
  69426. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4)) - 1);
  69427. + retval = -DWC_E_INVALID;
  69428. + }
  69429. +
  69430. + core_if->core_params->max_packet_count = val;
  69431. + return retval;
  69432. +}
  69433. +
  69434. +int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if)
  69435. +{
  69436. + return core_if->core_params->max_packet_count;
  69437. +}
  69438. +
  69439. +int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if, int32_t val)
  69440. +{
  69441. + int retval = 0;
  69442. +
  69443. + if (DWC_OTG_PARAM_TEST(val, 1, 16)) {
  69444. + DWC_WARN("Wrong value for host_channels\n");
  69445. + DWC_WARN("host_channels must be 1-16\n");
  69446. + return -DWC_E_INVALID;
  69447. + }
  69448. +
  69449. + if (val > (core_if->hwcfg2.b.num_host_chan + 1)) {
  69450. + if (dwc_otg_param_initialized
  69451. + (core_if->core_params->host_channels)) {
  69452. + DWC_ERROR
  69453. + ("%d invalid for host_channels. Check HW configurations.\n",
  69454. + val);
  69455. + }
  69456. + val = (core_if->hwcfg2.b.num_host_chan + 1);
  69457. + retval = -DWC_E_INVALID;
  69458. + }
  69459. +
  69460. + core_if->core_params->host_channels = val;
  69461. + return retval;
  69462. +}
  69463. +
  69464. +int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if)
  69465. +{
  69466. + return core_if->core_params->host_channels;
  69467. +}
  69468. +
  69469. +int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if, int32_t val)
  69470. +{
  69471. + int retval = 0;
  69472. +
  69473. + if (DWC_OTG_PARAM_TEST(val, 1, 15)) {
  69474. + DWC_WARN("Wrong value for dev_endpoints\n");
  69475. + DWC_WARN("dev_endpoints must be 1-15\n");
  69476. + return -DWC_E_INVALID;
  69477. + }
  69478. +
  69479. + if (val > (core_if->hwcfg2.b.num_dev_ep)) {
  69480. + if (dwc_otg_param_initialized
  69481. + (core_if->core_params->dev_endpoints)) {
  69482. + DWC_ERROR
  69483. + ("%d invalid for dev_endpoints. Check HW configurations.\n",
  69484. + val);
  69485. + }
  69486. + val = core_if->hwcfg2.b.num_dev_ep;
  69487. + retval = -DWC_E_INVALID;
  69488. + }
  69489. +
  69490. + core_if->core_params->dev_endpoints = val;
  69491. + return retval;
  69492. +}
  69493. +
  69494. +int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if)
  69495. +{
  69496. + return core_if->core_params->dev_endpoints;
  69497. +}
  69498. +
  69499. +int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val)
  69500. +{
  69501. + int retval = 0;
  69502. + int valid = 0;
  69503. +
  69504. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  69505. + DWC_WARN("Wrong value for phy_type\n");
  69506. + DWC_WARN("phy_type must be 0,1 or 2\n");
  69507. + return -DWC_E_INVALID;
  69508. + }
  69509. +#ifndef NO_FS_PHY_HW_CHECKS
  69510. + if ((val == DWC_PHY_TYPE_PARAM_UTMI) &&
  69511. + ((core_if->hwcfg2.b.hs_phy_type == 1) ||
  69512. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  69513. + valid = 1;
  69514. + } else if ((val == DWC_PHY_TYPE_PARAM_ULPI) &&
  69515. + ((core_if->hwcfg2.b.hs_phy_type == 2) ||
  69516. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  69517. + valid = 1;
  69518. + } else if ((val == DWC_PHY_TYPE_PARAM_FS) &&
  69519. + (core_if->hwcfg2.b.fs_phy_type == 1)) {
  69520. + valid = 1;
  69521. + }
  69522. + if (!valid) {
  69523. + if (dwc_otg_param_initialized(core_if->core_params->phy_type)) {
  69524. + DWC_ERROR
  69525. + ("%d invalid for phy_type. Check HW configurations.\n",
  69526. + val);
  69527. + }
  69528. + if (core_if->hwcfg2.b.hs_phy_type) {
  69529. + if ((core_if->hwcfg2.b.hs_phy_type == 3) ||
  69530. + (core_if->hwcfg2.b.hs_phy_type == 1)) {
  69531. + val = DWC_PHY_TYPE_PARAM_UTMI;
  69532. + } else {
  69533. + val = DWC_PHY_TYPE_PARAM_ULPI;
  69534. + }
  69535. + }
  69536. + retval = -DWC_E_INVALID;
  69537. + }
  69538. +#endif
  69539. + core_if->core_params->phy_type = val;
  69540. + return retval;
  69541. +}
  69542. +
  69543. +int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if)
  69544. +{
  69545. + return core_if->core_params->phy_type;
  69546. +}
  69547. +
  69548. +int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val)
  69549. +{
  69550. + int retval = 0;
  69551. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  69552. + DWC_WARN("Wrong value for speed parameter\n");
  69553. + DWC_WARN("max_speed parameter must be 0 or 1\n");
  69554. + return -DWC_E_INVALID;
  69555. + }
  69556. + if ((val == 0)
  69557. + && dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS) {
  69558. + if (dwc_otg_param_initialized(core_if->core_params->speed)) {
  69559. + DWC_ERROR
  69560. + ("%d invalid for speed paremter. Check HW configuration.\n",
  69561. + val);
  69562. + }
  69563. + val =
  69564. + (dwc_otg_get_param_phy_type(core_if) ==
  69565. + DWC_PHY_TYPE_PARAM_FS ? 1 : 0);
  69566. + retval = -DWC_E_INVALID;
  69567. + }
  69568. + core_if->core_params->speed = val;
  69569. + return retval;
  69570. +}
  69571. +
  69572. +int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if)
  69573. +{
  69574. + return core_if->core_params->speed;
  69575. +}
  69576. +
  69577. +int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if,
  69578. + int32_t val)
  69579. +{
  69580. + int retval = 0;
  69581. +
  69582. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  69583. + DWC_WARN
  69584. + ("Wrong value for host_ls_low_power_phy_clk parameter\n");
  69585. + DWC_WARN("host_ls_low_power_phy_clk must be 0 or 1\n");
  69586. + return -DWC_E_INVALID;
  69587. + }
  69588. +
  69589. + if ((val == DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ)
  69590. + && (dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS)) {
  69591. + if (dwc_otg_param_initialized
  69592. + (core_if->core_params->host_ls_low_power_phy_clk)) {
  69593. + DWC_ERROR
  69594. + ("%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
  69595. + val);
  69596. + }
  69597. + val =
  69598. + (dwc_otg_get_param_phy_type(core_if) ==
  69599. + DWC_PHY_TYPE_PARAM_FS) ?
  69600. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ :
  69601. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
  69602. + retval = -DWC_E_INVALID;
  69603. + }
  69604. +
  69605. + core_if->core_params->host_ls_low_power_phy_clk = val;
  69606. + return retval;
  69607. +}
  69608. +
  69609. +int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if)
  69610. +{
  69611. + return core_if->core_params->host_ls_low_power_phy_clk;
  69612. +}
  69613. +
  69614. +int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if, int32_t val)
  69615. +{
  69616. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  69617. + DWC_WARN("Wrong value for phy_ulpi_ddr\n");
  69618. + DWC_WARN("phy_upli_ddr must be 0 or 1\n");
  69619. + return -DWC_E_INVALID;
  69620. + }
  69621. +
  69622. + core_if->core_params->phy_ulpi_ddr = val;
  69623. + return 0;
  69624. +}
  69625. +
  69626. +int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if)
  69627. +{
  69628. + return core_if->core_params->phy_ulpi_ddr;
  69629. +}
  69630. +
  69631. +int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  69632. + int32_t val)
  69633. +{
  69634. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  69635. + DWC_WARN("Wrong valaue for phy_ulpi_ext_vbus\n");
  69636. + DWC_WARN("phy_ulpi_ext_vbus must be 0 or 1\n");
  69637. + return -DWC_E_INVALID;
  69638. + }
  69639. +
  69640. + core_if->core_params->phy_ulpi_ext_vbus = val;
  69641. + return 0;
  69642. +}
  69643. +
  69644. +int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if)
  69645. +{
  69646. + return core_if->core_params->phy_ulpi_ext_vbus;
  69647. +}
  69648. +
  69649. +int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if, int32_t val)
  69650. +{
  69651. + if (DWC_OTG_PARAM_TEST(val, 8, 8) && DWC_OTG_PARAM_TEST(val, 16, 16)) {
  69652. + DWC_WARN("Wrong valaue for phy_utmi_width\n");
  69653. + DWC_WARN("phy_utmi_width must be 8 or 16\n");
  69654. + return -DWC_E_INVALID;
  69655. + }
  69656. +
  69657. + core_if->core_params->phy_utmi_width = val;
  69658. + return 0;
  69659. +}
  69660. +
  69661. +int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if)
  69662. +{
  69663. + return core_if->core_params->phy_utmi_width;
  69664. +}
  69665. +
  69666. +int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if, int32_t val)
  69667. +{
  69668. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  69669. + DWC_WARN("Wrong valaue for ulpi_fs_ls\n");
  69670. + DWC_WARN("ulpi_fs_ls must be 0 or 1\n");
  69671. + return -DWC_E_INVALID;
  69672. + }
  69673. +
  69674. + core_if->core_params->ulpi_fs_ls = val;
  69675. + return 0;
  69676. +}
  69677. +
  69678. +int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if)
  69679. +{
  69680. + return core_if->core_params->ulpi_fs_ls;
  69681. +}
  69682. +
  69683. +int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val)
  69684. +{
  69685. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  69686. + DWC_WARN("Wrong valaue for ts_dline\n");
  69687. + DWC_WARN("ts_dline must be 0 or 1\n");
  69688. + return -DWC_E_INVALID;
  69689. + }
  69690. +
  69691. + core_if->core_params->ts_dline = val;
  69692. + return 0;
  69693. +}
  69694. +
  69695. +int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if)
  69696. +{
  69697. + return core_if->core_params->ts_dline;
  69698. +}
  69699. +
  69700. +int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if, int32_t val)
  69701. +{
  69702. + int retval = 0;
  69703. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  69704. + DWC_WARN("Wrong valaue for i2c_enable\n");
  69705. + DWC_WARN("i2c_enable must be 0 or 1\n");
  69706. + return -DWC_E_INVALID;
  69707. + }
  69708. +#ifndef NO_FS_PHY_HW_CHECK
  69709. + if (val == 1 && core_if->hwcfg3.b.i2c == 0) {
  69710. + if (dwc_otg_param_initialized(core_if->core_params->i2c_enable)) {
  69711. + DWC_ERROR
  69712. + ("%d invalid for i2c_enable. Check HW configuration.\n",
  69713. + val);
  69714. + }
  69715. + val = 0;
  69716. + retval = -DWC_E_INVALID;
  69717. + }
  69718. +#endif
  69719. +
  69720. + core_if->core_params->i2c_enable = val;
  69721. + return retval;
  69722. +}
  69723. +
  69724. +int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if)
  69725. +{
  69726. + return core_if->core_params->i2c_enable;
  69727. +}
  69728. +
  69729. +int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  69730. + int32_t val, int fifo_num)
  69731. +{
  69732. + int retval = 0;
  69733. +
  69734. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  69735. + DWC_WARN("Wrong value for dev_perio_tx_fifo_size\n");
  69736. + DWC_WARN("dev_perio_tx_fifo_size must be 4-768\n");
  69737. + return -DWC_E_INVALID;
  69738. + }
  69739. +
  69740. + if (val >
  69741. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  69742. + if (dwc_otg_param_initialized
  69743. + (core_if->core_params->dev_perio_tx_fifo_size[fifo_num])) {
  69744. + DWC_ERROR
  69745. + ("`%d' invalid for parameter `dev_perio_fifo_size_%d'. Check HW configuration.\n",
  69746. + val, fifo_num);
  69747. + }
  69748. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  69749. + retval = -DWC_E_INVALID;
  69750. + }
  69751. +
  69752. + core_if->core_params->dev_perio_tx_fifo_size[fifo_num] = val;
  69753. + return retval;
  69754. +}
  69755. +
  69756. +int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  69757. + int fifo_num)
  69758. +{
  69759. + return core_if->core_params->dev_perio_tx_fifo_size[fifo_num];
  69760. +}
  69761. +
  69762. +int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  69763. + int32_t val)
  69764. +{
  69765. + int retval = 0;
  69766. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  69767. + DWC_WARN("Wrong valaue for en_multiple_tx_fifo,\n");
  69768. + DWC_WARN("en_multiple_tx_fifo must be 0 or 1\n");
  69769. + return -DWC_E_INVALID;
  69770. + }
  69771. +
  69772. + if (val == 1 && core_if->hwcfg4.b.ded_fifo_en == 0) {
  69773. + if (dwc_otg_param_initialized
  69774. + (core_if->core_params->en_multiple_tx_fifo)) {
  69775. + DWC_ERROR
  69776. + ("%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
  69777. + val);
  69778. + }
  69779. + val = 0;
  69780. + retval = -DWC_E_INVALID;
  69781. + }
  69782. +
  69783. + core_if->core_params->en_multiple_tx_fifo = val;
  69784. + return retval;
  69785. +}
  69786. +
  69787. +int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if)
  69788. +{
  69789. + return core_if->core_params->en_multiple_tx_fifo;
  69790. +}
  69791. +
  69792. +int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val,
  69793. + int fifo_num)
  69794. +{
  69795. + int retval = 0;
  69796. +
  69797. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  69798. + DWC_WARN("Wrong value for dev_tx_fifo_size\n");
  69799. + DWC_WARN("dev_tx_fifo_size must be 4-768\n");
  69800. + return -DWC_E_INVALID;
  69801. + }
  69802. +
  69803. + if (val >
  69804. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  69805. + if (dwc_otg_param_initialized
  69806. + (core_if->core_params->dev_tx_fifo_size[fifo_num])) {
  69807. + DWC_ERROR
  69808. + ("`%d' invalid for parameter `dev_tx_fifo_size_%d'. Check HW configuration.\n",
  69809. + val, fifo_num);
  69810. + }
  69811. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  69812. + retval = -DWC_E_INVALID;
  69813. + }
  69814. +
  69815. + core_if->core_params->dev_tx_fifo_size[fifo_num] = val;
  69816. + return retval;
  69817. +}
  69818. +
  69819. +int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  69820. + int fifo_num)
  69821. +{
  69822. + return core_if->core_params->dev_tx_fifo_size[fifo_num];
  69823. +}
  69824. +
  69825. +int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  69826. +{
  69827. + int retval = 0;
  69828. +
  69829. + if (DWC_OTG_PARAM_TEST(val, 0, 7)) {
  69830. + DWC_WARN("Wrong value for thr_ctl\n");
  69831. + DWC_WARN("thr_ctl must be 0-7\n");
  69832. + return -DWC_E_INVALID;
  69833. + }
  69834. +
  69835. + if ((val != 0) &&
  69836. + (!dwc_otg_get_param_dma_enable(core_if) ||
  69837. + !core_if->hwcfg4.b.ded_fifo_en)) {
  69838. + if (dwc_otg_param_initialized(core_if->core_params->thr_ctl)) {
  69839. + DWC_ERROR
  69840. + ("%d invalid for parameter thr_ctl. Check HW configuration.\n",
  69841. + val);
  69842. + }
  69843. + val = 0;
  69844. + retval = -DWC_E_INVALID;
  69845. + }
  69846. +
  69847. + core_if->core_params->thr_ctl = val;
  69848. + return retval;
  69849. +}
  69850. +
  69851. +int32_t dwc_otg_get_param_thr_ctl(dwc_otg_core_if_t * core_if)
  69852. +{
  69853. + return core_if->core_params->thr_ctl;
  69854. +}
  69855. +
  69856. +int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if, int32_t val)
  69857. +{
  69858. + int retval = 0;
  69859. +
  69860. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  69861. + DWC_WARN("Wrong value for lpm_enable\n");
  69862. + DWC_WARN("lpm_enable must be 0 or 1\n");
  69863. + return -DWC_E_INVALID;
  69864. + }
  69865. +
  69866. + if (val && !core_if->hwcfg3.b.otg_lpm_en) {
  69867. + if (dwc_otg_param_initialized(core_if->core_params->lpm_enable)) {
  69868. + DWC_ERROR
  69869. + ("%d invalid for parameter lpm_enable. Check HW configuration.\n",
  69870. + val);
  69871. + }
  69872. + val = 0;
  69873. + retval = -DWC_E_INVALID;
  69874. + }
  69875. +
  69876. + core_if->core_params->lpm_enable = val;
  69877. + return retval;
  69878. +}
  69879. +
  69880. +int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if)
  69881. +{
  69882. + return core_if->core_params->lpm_enable;
  69883. +}
  69884. +
  69885. +int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  69886. +{
  69887. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  69888. + DWC_WARN("Wrong valaue for tx_thr_length\n");
  69889. + DWC_WARN("tx_thr_length must be 8 - 128\n");
  69890. + return -DWC_E_INVALID;
  69891. + }
  69892. +
  69893. + core_if->core_params->tx_thr_length = val;
  69894. + return 0;
  69895. +}
  69896. +
  69897. +int32_t dwc_otg_get_param_tx_thr_length(dwc_otg_core_if_t * core_if)
  69898. +{
  69899. + return core_if->core_params->tx_thr_length;
  69900. +}
  69901. +
  69902. +int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  69903. +{
  69904. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  69905. + DWC_WARN("Wrong valaue for rx_thr_length\n");
  69906. + DWC_WARN("rx_thr_length must be 8 - 128\n");
  69907. + return -DWC_E_INVALID;
  69908. + }
  69909. +
  69910. + core_if->core_params->rx_thr_length = val;
  69911. + return 0;
  69912. +}
  69913. +
  69914. +int32_t dwc_otg_get_param_rx_thr_length(dwc_otg_core_if_t * core_if)
  69915. +{
  69916. + return core_if->core_params->rx_thr_length;
  69917. +}
  69918. +
  69919. +int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if, int32_t val)
  69920. +{
  69921. + if (DWC_OTG_PARAM_TEST(val, 1, 1) &&
  69922. + DWC_OTG_PARAM_TEST(val, 4, 4) &&
  69923. + DWC_OTG_PARAM_TEST(val, 8, 8) &&
  69924. + DWC_OTG_PARAM_TEST(val, 16, 16) &&
  69925. + DWC_OTG_PARAM_TEST(val, 32, 32) &&
  69926. + DWC_OTG_PARAM_TEST(val, 64, 64) &&
  69927. + DWC_OTG_PARAM_TEST(val, 128, 128) &&
  69928. + DWC_OTG_PARAM_TEST(val, 256, 256)) {
  69929. + DWC_WARN("`%d' invalid for parameter `dma_burst_size'\n", val);
  69930. + return -DWC_E_INVALID;
  69931. + }
  69932. + core_if->core_params->dma_burst_size = val;
  69933. + return 0;
  69934. +}
  69935. +
  69936. +int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if)
  69937. +{
  69938. + return core_if->core_params->dma_burst_size;
  69939. +}
  69940. +
  69941. +int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if, int32_t val)
  69942. +{
  69943. + int retval = 0;
  69944. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  69945. + DWC_WARN("`%d' invalid for parameter `pti_enable'\n", val);
  69946. + return -DWC_E_INVALID;
  69947. + }
  69948. + if (val && (core_if->snpsid < OTG_CORE_REV_2_72a)) {
  69949. + if (dwc_otg_param_initialized(core_if->core_params->pti_enable)) {
  69950. + DWC_ERROR
  69951. + ("%d invalid for parameter pti_enable. Check HW configuration.\n",
  69952. + val);
  69953. + }
  69954. + retval = -DWC_E_INVALID;
  69955. + val = 0;
  69956. + }
  69957. + core_if->core_params->pti_enable = val;
  69958. + return retval;
  69959. +}
  69960. +
  69961. +int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if)
  69962. +{
  69963. + return core_if->core_params->pti_enable;
  69964. +}
  69965. +
  69966. +int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if, int32_t val)
  69967. +{
  69968. + int retval = 0;
  69969. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  69970. + DWC_WARN("`%d' invalid for parameter `mpi_enable'\n", val);
  69971. + return -DWC_E_INVALID;
  69972. + }
  69973. + if (val && (core_if->hwcfg2.b.multi_proc_int == 0)) {
  69974. + if (dwc_otg_param_initialized(core_if->core_params->mpi_enable)) {
  69975. + DWC_ERROR
  69976. + ("%d invalid for parameter mpi_enable. Check HW configuration.\n",
  69977. + val);
  69978. + }
  69979. + retval = -DWC_E_INVALID;
  69980. + val = 0;
  69981. + }
  69982. + core_if->core_params->mpi_enable = val;
  69983. + return retval;
  69984. +}
  69985. +
  69986. +int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if)
  69987. +{
  69988. + return core_if->core_params->mpi_enable;
  69989. +}
  69990. +
  69991. +int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if, int32_t val)
  69992. +{
  69993. + int retval = 0;
  69994. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  69995. + DWC_WARN("`%d' invalid for parameter `adp_enable'\n", val);
  69996. + return -DWC_E_INVALID;
  69997. + }
  69998. + if (val && (core_if->hwcfg3.b.adp_supp == 0)) {
  69999. + if (dwc_otg_param_initialized
  70000. + (core_if->core_params->adp_supp_enable)) {
  70001. + DWC_ERROR
  70002. + ("%d invalid for parameter adp_enable. Check HW configuration.\n",
  70003. + val);
  70004. + }
  70005. + retval = -DWC_E_INVALID;
  70006. + val = 0;
  70007. + }
  70008. + core_if->core_params->adp_supp_enable = val;
  70009. + /*Set OTG version 2.0 in case of enabling ADP*/
  70010. + if (val)
  70011. + dwc_otg_set_param_otg_ver(core_if, 1);
  70012. +
  70013. + return retval;
  70014. +}
  70015. +
  70016. +int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if)
  70017. +{
  70018. + return core_if->core_params->adp_supp_enable;
  70019. +}
  70020. +
  70021. +int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if, int32_t val)
  70022. +{
  70023. + int retval = 0;
  70024. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  70025. + DWC_WARN("`%d' invalid for parameter `ic_usb_cap'\n", val);
  70026. + DWC_WARN("ic_usb_cap must be 0 or 1\n");
  70027. + return -DWC_E_INVALID;
  70028. + }
  70029. +
  70030. + if (val && (core_if->hwcfg2.b.otg_enable_ic_usb == 0)) {
  70031. + if (dwc_otg_param_initialized(core_if->core_params->ic_usb_cap)) {
  70032. + DWC_ERROR
  70033. + ("%d invalid for parameter ic_usb_cap. Check HW configuration.\n",
  70034. + val);
  70035. + }
  70036. + retval = -DWC_E_INVALID;
  70037. + val = 0;
  70038. + }
  70039. + core_if->core_params->ic_usb_cap = val;
  70040. + return retval;
  70041. +}
  70042. +
  70043. +int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if)
  70044. +{
  70045. + return core_if->core_params->ic_usb_cap;
  70046. +}
  70047. +
  70048. +int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if, int32_t val)
  70049. +{
  70050. + int retval = 0;
  70051. + int valid = 1;
  70052. +
  70053. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  70054. + DWC_WARN("`%d' invalid for parameter `ahb_thr_ratio'\n", val);
  70055. + DWC_WARN("ahb_thr_ratio must be 0 - 3\n");
  70056. + return -DWC_E_INVALID;
  70057. + }
  70058. +
  70059. + if (val
  70060. + && (core_if->snpsid < OTG_CORE_REV_2_81a
  70061. + || !dwc_otg_get_param_thr_ctl(core_if))) {
  70062. + valid = 0;
  70063. + } else if (val
  70064. + && ((dwc_otg_get_param_tx_thr_length(core_if) / (1 << val)) <
  70065. + 4)) {
  70066. + valid = 0;
  70067. + }
  70068. + if (valid == 0) {
  70069. + if (dwc_otg_param_initialized
  70070. + (core_if->core_params->ahb_thr_ratio)) {
  70071. + DWC_ERROR
  70072. + ("%d invalid for parameter ahb_thr_ratio. Check HW configuration.\n",
  70073. + val);
  70074. + }
  70075. + retval = -DWC_E_INVALID;
  70076. + val = 0;
  70077. + }
  70078. +
  70079. + core_if->core_params->ahb_thr_ratio = val;
  70080. + return retval;
  70081. +}
  70082. +
  70083. +int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if)
  70084. +{
  70085. + return core_if->core_params->ahb_thr_ratio;
  70086. +}
  70087. +
  70088. +int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if, int32_t val)
  70089. +{
  70090. + int retval = 0;
  70091. + int valid = 1;
  70092. + hwcfg4_data_t hwcfg4 = {.d32 = 0 };
  70093. + hwcfg4.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  70094. +
  70095. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  70096. + DWC_WARN("`%d' invalid for parameter `power_down'\n", val);
  70097. + DWC_WARN("power_down must be 0 - 2\n");
  70098. + return -DWC_E_INVALID;
  70099. + }
  70100. +
  70101. + if ((val == 2) && (core_if->snpsid < OTG_CORE_REV_2_91a)) {
  70102. + valid = 0;
  70103. + }
  70104. + if ((val == 3)
  70105. + && ((core_if->snpsid < OTG_CORE_REV_3_00a)
  70106. + || (hwcfg4.b.xhiber == 0))) {
  70107. + valid = 0;
  70108. + }
  70109. + if (valid == 0) {
  70110. + if (dwc_otg_param_initialized(core_if->core_params->power_down)) {
  70111. + DWC_ERROR
  70112. + ("%d invalid for parameter power_down. Check HW configuration.\n",
  70113. + val);
  70114. + }
  70115. + retval = -DWC_E_INVALID;
  70116. + val = 0;
  70117. + }
  70118. + core_if->core_params->power_down = val;
  70119. + return retval;
  70120. +}
  70121. +
  70122. +int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if)
  70123. +{
  70124. + return core_if->core_params->power_down;
  70125. +}
  70126. +
  70127. +int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  70128. +{
  70129. + int retval = 0;
  70130. + int valid = 1;
  70131. +
  70132. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  70133. + DWC_WARN("`%d' invalid for parameter `reload_ctl'\n", val);
  70134. + DWC_WARN("reload_ctl must be 0 or 1\n");
  70135. + return -DWC_E_INVALID;
  70136. + }
  70137. +
  70138. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_92a)) {
  70139. + valid = 0;
  70140. + }
  70141. + if (valid == 0) {
  70142. + if (dwc_otg_param_initialized(core_if->core_params->reload_ctl)) {
  70143. + DWC_ERROR("%d invalid for parameter reload_ctl."
  70144. + "Check HW configuration.\n", val);
  70145. + }
  70146. + retval = -DWC_E_INVALID;
  70147. + val = 0;
  70148. + }
  70149. + core_if->core_params->reload_ctl = val;
  70150. + return retval;
  70151. +}
  70152. +
  70153. +int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if)
  70154. +{
  70155. + return core_if->core_params->reload_ctl;
  70156. +}
  70157. +
  70158. +int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if, int32_t val)
  70159. +{
  70160. + int retval = 0;
  70161. + int valid = 1;
  70162. +
  70163. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  70164. + DWC_WARN("`%d' invalid for parameter `dev_out_nak'\n", val);
  70165. + DWC_WARN("dev_out_nak must be 0 or 1\n");
  70166. + return -DWC_E_INVALID;
  70167. + }
  70168. +
  70169. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_93a) ||
  70170. + !(core_if->core_params->dma_desc_enable))) {
  70171. + valid = 0;
  70172. + }
  70173. + if (valid == 0) {
  70174. + if (dwc_otg_param_initialized(core_if->core_params->dev_out_nak)) {
  70175. + DWC_ERROR("%d invalid for parameter dev_out_nak."
  70176. + "Check HW configuration.\n", val);
  70177. + }
  70178. + retval = -DWC_E_INVALID;
  70179. + val = 0;
  70180. + }
  70181. + core_if->core_params->dev_out_nak = val;
  70182. + return retval;
  70183. +}
  70184. +
  70185. +int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if)
  70186. +{
  70187. + return core_if->core_params->dev_out_nak;
  70188. +}
  70189. +
  70190. +int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if, int32_t val)
  70191. +{
  70192. + int retval = 0;
  70193. + int valid = 1;
  70194. +
  70195. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  70196. + DWC_WARN("`%d' invalid for parameter `cont_on_bna'\n", val);
  70197. + DWC_WARN("cont_on_bna must be 0 or 1\n");
  70198. + return -DWC_E_INVALID;
  70199. + }
  70200. +
  70201. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_94a) ||
  70202. + !(core_if->core_params->dma_desc_enable))) {
  70203. + valid = 0;
  70204. + }
  70205. + if (valid == 0) {
  70206. + if (dwc_otg_param_initialized(core_if->core_params->cont_on_bna)) {
  70207. + DWC_ERROR("%d invalid for parameter cont_on_bna."
  70208. + "Check HW configuration.\n", val);
  70209. + }
  70210. + retval = -DWC_E_INVALID;
  70211. + val = 0;
  70212. + }
  70213. + core_if->core_params->cont_on_bna = val;
  70214. + return retval;
  70215. +}
  70216. +
  70217. +int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if)
  70218. +{
  70219. + return core_if->core_params->cont_on_bna;
  70220. +}
  70221. +
  70222. +int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if, int32_t val)
  70223. +{
  70224. + int retval = 0;
  70225. + int valid = 1;
  70226. +
  70227. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  70228. + DWC_WARN("`%d' invalid for parameter `ahb_single'\n", val);
  70229. + DWC_WARN("ahb_single must be 0 or 1\n");
  70230. + return -DWC_E_INVALID;
  70231. + }
  70232. +
  70233. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  70234. + valid = 0;
  70235. + }
  70236. + if (valid == 0) {
  70237. + if (dwc_otg_param_initialized(core_if->core_params->ahb_single)) {
  70238. + DWC_ERROR("%d invalid for parameter ahb_single."
  70239. + "Check HW configuration.\n", val);
  70240. + }
  70241. + retval = -DWC_E_INVALID;
  70242. + val = 0;
  70243. + }
  70244. + core_if->core_params->ahb_single = val;
  70245. + return retval;
  70246. +}
  70247. +
  70248. +int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if)
  70249. +{
  70250. + return core_if->core_params->ahb_single;
  70251. +}
  70252. +
  70253. +int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val)
  70254. +{
  70255. + int retval = 0;
  70256. +
  70257. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  70258. + DWC_WARN("`%d' invalid for parameter `otg_ver'\n", val);
  70259. + DWC_WARN
  70260. + ("otg_ver must be 0(for OTG 1.3 support) or 1(for OTG 2.0 support)\n");
  70261. + return -DWC_E_INVALID;
  70262. + }
  70263. +
  70264. + core_if->core_params->otg_ver = val;
  70265. + return retval;
  70266. +}
  70267. +
  70268. +int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if)
  70269. +{
  70270. + return core_if->core_params->otg_ver;
  70271. +}
  70272. +
  70273. +uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if)
  70274. +{
  70275. + gotgctl_data_t otgctl;
  70276. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  70277. + return otgctl.b.hstnegscs;
  70278. +}
  70279. +
  70280. +uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if)
  70281. +{
  70282. + gotgctl_data_t otgctl;
  70283. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  70284. + return otgctl.b.sesreqscs;
  70285. +}
  70286. +
  70287. +void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val)
  70288. +{
  70289. + if(core_if->otg_ver == 0) {
  70290. + gotgctl_data_t otgctl;
  70291. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  70292. + otgctl.b.hnpreq = val;
  70293. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, otgctl.d32);
  70294. + } else {
  70295. + core_if->otg_sts = val;
  70296. + }
  70297. +}
  70298. +
  70299. +uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if)
  70300. +{
  70301. + return core_if->snpsid;
  70302. +}
  70303. +
  70304. +uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if)
  70305. +{
  70306. + gintsts_data_t gintsts;
  70307. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  70308. + return gintsts.b.curmode;
  70309. +}
  70310. +
  70311. +uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if)
  70312. +{
  70313. + gusbcfg_data_t usbcfg;
  70314. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  70315. + return usbcfg.b.hnpcap;
  70316. +}
  70317. +
  70318. +void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  70319. +{
  70320. + gusbcfg_data_t usbcfg;
  70321. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  70322. + usbcfg.b.hnpcap = val;
  70323. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  70324. +}
  70325. +
  70326. +uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if)
  70327. +{
  70328. + gusbcfg_data_t usbcfg;
  70329. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  70330. + return usbcfg.b.srpcap;
  70331. +}
  70332. +
  70333. +void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  70334. +{
  70335. + gusbcfg_data_t usbcfg;
  70336. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  70337. + usbcfg.b.srpcap = val;
  70338. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  70339. +}
  70340. +
  70341. +uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if)
  70342. +{
  70343. + dcfg_data_t dcfg;
  70344. + /* originally: dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg); */
  70345. +
  70346. + dcfg.d32 = -1; //GRAYG
  70347. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)\n", __func__, core_if);
  70348. + if (NULL == core_if)
  70349. + DWC_ERROR("reg request with NULL core_if\n");
  70350. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)\n", __func__,
  70351. + core_if, core_if->dev_if);
  70352. + if (NULL == core_if->dev_if)
  70353. + DWC_ERROR("reg request with NULL dev_if\n");
  70354. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)->"
  70355. + "dev_global_regs(%p)\n", __func__,
  70356. + core_if, core_if->dev_if,
  70357. + core_if->dev_if->dev_global_regs);
  70358. + if (NULL == core_if->dev_if->dev_global_regs)
  70359. + DWC_ERROR("reg request with NULL dev_global_regs\n");
  70360. + else {
  70361. + DWC_DEBUGPL(DBG_CILV, "%s - &core_if(%p)->dev_if(%p)->"
  70362. + "dev_global_regs(%p)->dcfg = %p\n", __func__,
  70363. + core_if, core_if->dev_if,
  70364. + core_if->dev_if->dev_global_regs,
  70365. + &core_if->dev_if->dev_global_regs->dcfg);
  70366. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  70367. + }
  70368. + return dcfg.b.devspd;
  70369. +}
  70370. +
  70371. +void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val)
  70372. +{
  70373. + dcfg_data_t dcfg;
  70374. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  70375. + dcfg.b.devspd = val;
  70376. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  70377. +}
  70378. +
  70379. +uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if)
  70380. +{
  70381. + hprt0_data_t hprt0;
  70382. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  70383. + return hprt0.b.prtconnsts;
  70384. +}
  70385. +
  70386. +uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if)
  70387. +{
  70388. + dsts_data_t dsts;
  70389. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  70390. + return dsts.b.enumspd;
  70391. +}
  70392. +
  70393. +uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if)
  70394. +{
  70395. + hprt0_data_t hprt0;
  70396. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  70397. + return hprt0.b.prtpwr;
  70398. +
  70399. +}
  70400. +
  70401. +uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if)
  70402. +{
  70403. + return core_if->hibernation_suspend;
  70404. +}
  70405. +
  70406. +void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val)
  70407. +{
  70408. + hprt0_data_t hprt0;
  70409. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70410. + hprt0.b.prtpwr = val;
  70411. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70412. +}
  70413. +
  70414. +uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if)
  70415. +{
  70416. + hprt0_data_t hprt0;
  70417. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  70418. + return hprt0.b.prtsusp;
  70419. +
  70420. +}
  70421. +
  70422. +void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val)
  70423. +{
  70424. + hprt0_data_t hprt0;
  70425. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70426. + hprt0.b.prtsusp = val;
  70427. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70428. +}
  70429. +
  70430. +uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if)
  70431. +{
  70432. + hfir_data_t hfir;
  70433. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  70434. + return hfir.b.frint;
  70435. +
  70436. +}
  70437. +
  70438. +void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val)
  70439. +{
  70440. + hfir_data_t hfir;
  70441. + uint32_t fram_int;
  70442. + fram_int = calc_frame_interval(core_if);
  70443. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  70444. + if (!core_if->core_params->reload_ctl) {
  70445. + DWC_WARN("\nCannot reload HFIR register.HFIR.HFIRRldCtrl bit is"
  70446. + "not set to 1.\nShould load driver with reload_ctl=1"
  70447. + " module parameter\n");
  70448. + return;
  70449. + }
  70450. + switch (fram_int) {
  70451. + case 3750:
  70452. + if ((val < 3350) || (val > 4150)) {
  70453. + DWC_WARN("HFIR interval for HS core and 30 MHz"
  70454. + "clock freq should be from 3350 to 4150\n");
  70455. + return;
  70456. + }
  70457. + break;
  70458. + case 30000:
  70459. + if ((val < 26820) || (val > 33180)) {
  70460. + DWC_WARN("HFIR interval for FS/LS core and 30 MHz"
  70461. + "clock freq should be from 26820 to 33180\n");
  70462. + return;
  70463. + }
  70464. + break;
  70465. + case 6000:
  70466. + if ((val < 5360) || (val > 6640)) {
  70467. + DWC_WARN("HFIR interval for HS core and 48 MHz"
  70468. + "clock freq should be from 5360 to 6640\n");
  70469. + return;
  70470. + }
  70471. + break;
  70472. + case 48000:
  70473. + if ((val < 42912) || (val > 53088)) {
  70474. + DWC_WARN("HFIR interval for FS/LS core and 48 MHz"
  70475. + "clock freq should be from 42912 to 53088\n");
  70476. + return;
  70477. + }
  70478. + break;
  70479. + case 7500:
  70480. + if ((val < 6700) || (val > 8300)) {
  70481. + DWC_WARN("HFIR interval for HS core and 60 MHz"
  70482. + "clock freq should be from 6700 to 8300\n");
  70483. + return;
  70484. + }
  70485. + break;
  70486. + case 60000:
  70487. + if ((val < 53640) || (val > 65536)) {
  70488. + DWC_WARN("HFIR interval for FS/LS core and 60 MHz"
  70489. + "clock freq should be from 53640 to 65536\n");
  70490. + return;
  70491. + }
  70492. + break;
  70493. + default:
  70494. + DWC_WARN("Unknown frame interval\n");
  70495. + return;
  70496. + break;
  70497. +
  70498. + }
  70499. + hfir.b.frint = val;
  70500. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hfir.d32);
  70501. +}
  70502. +
  70503. +uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if)
  70504. +{
  70505. + hcfg_data_t hcfg;
  70506. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  70507. + return hcfg.b.modechtimen;
  70508. +
  70509. +}
  70510. +
  70511. +void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val)
  70512. +{
  70513. + hcfg_data_t hcfg;
  70514. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  70515. + hcfg.b.modechtimen = val;
  70516. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  70517. +}
  70518. +
  70519. +void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val)
  70520. +{
  70521. + hprt0_data_t hprt0;
  70522. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70523. + hprt0.b.prtres = val;
  70524. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70525. +}
  70526. +
  70527. +uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if)
  70528. +{
  70529. + dctl_data_t dctl;
  70530. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  70531. + return dctl.b.rmtwkupsig;
  70532. +}
  70533. +
  70534. +uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if)
  70535. +{
  70536. + glpmcfg_data_t lpmcfg;
  70537. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  70538. +
  70539. + DWC_ASSERT(!
  70540. + ((core_if->lx_state == DWC_OTG_L1) ^ lpmcfg.b.prt_sleep_sts),
  70541. + "lx_state = %d, lmpcfg.prt_sleep_sts = %d\n",
  70542. + core_if->lx_state, lpmcfg.b.prt_sleep_sts);
  70543. +
  70544. + return lpmcfg.b.prt_sleep_sts;
  70545. +}
  70546. +
  70547. +uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if)
  70548. +{
  70549. + glpmcfg_data_t lpmcfg;
  70550. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  70551. + return lpmcfg.b.rem_wkup_en;
  70552. +}
  70553. +
  70554. +uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if)
  70555. +{
  70556. + glpmcfg_data_t lpmcfg;
  70557. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  70558. + return lpmcfg.b.appl_resp;
  70559. +}
  70560. +
  70561. +void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val)
  70562. +{
  70563. + glpmcfg_data_t lpmcfg;
  70564. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  70565. + lpmcfg.b.appl_resp = val;
  70566. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  70567. +}
  70568. +
  70569. +uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if)
  70570. +{
  70571. + glpmcfg_data_t lpmcfg;
  70572. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  70573. + return lpmcfg.b.hsic_connect;
  70574. +}
  70575. +
  70576. +void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val)
  70577. +{
  70578. + glpmcfg_data_t lpmcfg;
  70579. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  70580. + lpmcfg.b.hsic_connect = val;
  70581. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  70582. +}
  70583. +
  70584. +uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if)
  70585. +{
  70586. + glpmcfg_data_t lpmcfg;
  70587. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  70588. + return lpmcfg.b.inv_sel_hsic;
  70589. +
  70590. +}
  70591. +
  70592. +void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val)
  70593. +{
  70594. + glpmcfg_data_t lpmcfg;
  70595. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  70596. + lpmcfg.b.inv_sel_hsic = val;
  70597. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  70598. +}
  70599. +
  70600. +uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if)
  70601. +{
  70602. + return DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  70603. +}
  70604. +
  70605. +void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val)
  70606. +{
  70607. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, val);
  70608. +}
  70609. +
  70610. +uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if)
  70611. +{
  70612. + return DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  70613. +}
  70614. +
  70615. +void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val)
  70616. +{
  70617. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, val);
  70618. +}
  70619. +
  70620. +uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if)
  70621. +{
  70622. + return DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  70623. +}
  70624. +
  70625. +void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  70626. +{
  70627. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, val);
  70628. +}
  70629. +
  70630. +uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if)
  70631. +{
  70632. + return DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  70633. +}
  70634. +
  70635. +void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  70636. +{
  70637. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz, val);
  70638. +}
  70639. +
  70640. +uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if)
  70641. +{
  70642. + return DWC_READ_REG32(&core_if->core_global_regs->gpvndctl);
  70643. +}
  70644. +
  70645. +void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val)
  70646. +{
  70647. + DWC_WRITE_REG32(&core_if->core_global_regs->gpvndctl, val);
  70648. +}
  70649. +
  70650. +uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if)
  70651. +{
  70652. + return DWC_READ_REG32(&core_if->core_global_regs->ggpio);
  70653. +}
  70654. +
  70655. +void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val)
  70656. +{
  70657. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, val);
  70658. +}
  70659. +
  70660. +uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if)
  70661. +{
  70662. + return DWC_READ_REG32(core_if->host_if->hprt0);
  70663. +
  70664. +}
  70665. +
  70666. +void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val)
  70667. +{
  70668. + DWC_WRITE_REG32(core_if->host_if->hprt0, val);
  70669. +}
  70670. +
  70671. +uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if)
  70672. +{
  70673. + return DWC_READ_REG32(&core_if->core_global_regs->guid);
  70674. +}
  70675. +
  70676. +void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val)
  70677. +{
  70678. + DWC_WRITE_REG32(&core_if->core_global_regs->guid, val);
  70679. +}
  70680. +
  70681. +uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if)
  70682. +{
  70683. + return DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  70684. +}
  70685. +
  70686. +uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if)
  70687. +{
  70688. + return ((core_if->otg_ver == 1) ? (uint16_t)0x0200 : (uint16_t)0x0103);
  70689. +}
  70690. +
  70691. +/**
  70692. + * Start the SRP timer to detect when the SRP does not complete within
  70693. + * 6 seconds.
  70694. + *
  70695. + * @param core_if the pointer to core_if strucure.
  70696. + */
  70697. +void dwc_otg_pcd_start_srp_timer(dwc_otg_core_if_t * core_if)
  70698. +{
  70699. + core_if->srp_timer_started = 1;
  70700. + DWC_TIMER_SCHEDULE(core_if->srp_timer, 6000 /* 6 secs */ );
  70701. +}
  70702. +
  70703. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if)
  70704. +{
  70705. + uint32_t *addr = (uint32_t *) & (core_if->core_global_regs->gotgctl);
  70706. + gotgctl_data_t mem;
  70707. + gotgctl_data_t val;
  70708. +
  70709. + val.d32 = DWC_READ_REG32(addr);
  70710. + if (val.b.sesreq) {
  70711. + DWC_ERROR("Session Request Already active!\n");
  70712. + return;
  70713. + }
  70714. +
  70715. + DWC_INFO("Session Request Initated\n"); //NOTICE
  70716. + mem.d32 = DWC_READ_REG32(addr);
  70717. + mem.b.sesreq = 1;
  70718. + DWC_WRITE_REG32(addr, mem.d32);
  70719. +
  70720. + /* Start the SRP timer */
  70721. + dwc_otg_pcd_start_srp_timer(core_if);
  70722. + return;
  70723. +}
  70724. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_cil.h linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil.h
  70725. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_cil.h 1969-12-31 18:00:00.000000000 -0600
  70726. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil.h 2014-12-03 19:13:40.216418001 -0600
  70727. @@ -0,0 +1,1464 @@
  70728. +/* ==========================================================================
  70729. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.h $
  70730. + * $Revision: #123 $
  70731. + * $Date: 2012/08/10 $
  70732. + * $Change: 2047372 $
  70733. + *
  70734. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  70735. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  70736. + * otherwise expressly agreed to in writing between Synopsys and you.
  70737. + *
  70738. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  70739. + * any End User Software License Agreement or Agreement for Licensed Product
  70740. + * with Synopsys or any supplement thereto. You are permitted to use and
  70741. + * redistribute this Software in source and binary forms, with or without
  70742. + * modification, provided that redistributions of source code must retain this
  70743. + * notice. You may not view, use, disclose, copy or distribute this file or
  70744. + * any information contained herein except pursuant to this license grant from
  70745. + * Synopsys. If you do not agree with this notice, including the disclaimer
  70746. + * below, then you are not authorized to use the Software.
  70747. + *
  70748. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  70749. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  70750. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  70751. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  70752. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  70753. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  70754. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  70755. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  70756. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  70757. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  70758. + * DAMAGE.
  70759. + * ========================================================================== */
  70760. +
  70761. +#if !defined(__DWC_CIL_H__)
  70762. +#define __DWC_CIL_H__
  70763. +
  70764. +#include "dwc_list.h"
  70765. +#include "dwc_otg_dbg.h"
  70766. +#include "dwc_otg_regs.h"
  70767. +
  70768. +#include "dwc_otg_core_if.h"
  70769. +#include "dwc_otg_adp.h"
  70770. +
  70771. +/**
  70772. + * @file
  70773. + * This file contains the interface to the Core Interface Layer.
  70774. + */
  70775. +
  70776. +#ifdef DWC_UTE_CFI
  70777. +
  70778. +#define MAX_DMA_DESCS_PER_EP 256
  70779. +
  70780. +/**
  70781. + * Enumeration for the data buffer mode
  70782. + */
  70783. +typedef enum _data_buffer_mode {
  70784. + BM_STANDARD = 0, /* data buffer is in normal mode */
  70785. + BM_SG = 1, /* data buffer uses the scatter/gather mode */
  70786. + BM_CONCAT = 2, /* data buffer uses the concatenation mode */
  70787. + BM_CIRCULAR = 3, /* data buffer uses the circular DMA mode */
  70788. + BM_ALIGN = 4 /* data buffer is in buffer alignment mode */
  70789. +} data_buffer_mode_e;
  70790. +#endif //DWC_UTE_CFI
  70791. +
  70792. +/** Macros defined for DWC OTG HW Release version */
  70793. +
  70794. +#define OTG_CORE_REV_2_60a 0x4F54260A
  70795. +#define OTG_CORE_REV_2_71a 0x4F54271A
  70796. +#define OTG_CORE_REV_2_72a 0x4F54272A
  70797. +#define OTG_CORE_REV_2_80a 0x4F54280A
  70798. +#define OTG_CORE_REV_2_81a 0x4F54281A
  70799. +#define OTG_CORE_REV_2_90a 0x4F54290A
  70800. +#define OTG_CORE_REV_2_91a 0x4F54291A
  70801. +#define OTG_CORE_REV_2_92a 0x4F54292A
  70802. +#define OTG_CORE_REV_2_93a 0x4F54293A
  70803. +#define OTG_CORE_REV_2_94a 0x4F54294A
  70804. +#define OTG_CORE_REV_3_00a 0x4F54300A
  70805. +
  70806. +/**
  70807. + * Information for each ISOC packet.
  70808. + */
  70809. +typedef struct iso_pkt_info {
  70810. + uint32_t offset;
  70811. + uint32_t length;
  70812. + int32_t status;
  70813. +} iso_pkt_info_t;
  70814. +
  70815. +/**
  70816. + * The <code>dwc_ep</code> structure represents the state of a single
  70817. + * endpoint when acting in device mode. It contains the data items
  70818. + * needed for an endpoint to be activated and transfer packets.
  70819. + */
  70820. +typedef struct dwc_ep {
  70821. + /** EP number used for register address lookup */
  70822. + uint8_t num;
  70823. + /** EP direction 0 = OUT */
  70824. + unsigned is_in:1;
  70825. + /** EP active. */
  70826. + unsigned active:1;
  70827. +
  70828. + /**
  70829. + * Periodic Tx FIFO # for IN EPs For INTR EP set to 0 to use non-periodic
  70830. + * Tx FIFO. If dedicated Tx FIFOs are enabled Tx FIFO # FOR IN EPs*/
  70831. + unsigned tx_fifo_num:4;
  70832. + /** EP type: 0 - Control, 1 - ISOC, 2 - BULK, 3 - INTR */
  70833. + unsigned type:2;
  70834. +#define DWC_OTG_EP_TYPE_CONTROL 0
  70835. +#define DWC_OTG_EP_TYPE_ISOC 1
  70836. +#define DWC_OTG_EP_TYPE_BULK 2
  70837. +#define DWC_OTG_EP_TYPE_INTR 3
  70838. +
  70839. + /** DATA start PID for INTR and BULK EP */
  70840. + unsigned data_pid_start:1;
  70841. + /** Frame (even/odd) for ISOC EP */
  70842. + unsigned even_odd_frame:1;
  70843. + /** Max Packet bytes */
  70844. + unsigned maxpacket:11;
  70845. +
  70846. + /** Max Transfer size */
  70847. + uint32_t maxxfer;
  70848. +
  70849. + /** @name Transfer state */
  70850. + /** @{ */
  70851. +
  70852. + /**
  70853. + * Pointer to the beginning of the transfer buffer -- do not modify
  70854. + * during transfer.
  70855. + */
  70856. +
  70857. + dwc_dma_t dma_addr;
  70858. +
  70859. + dwc_dma_t dma_desc_addr;
  70860. + dwc_otg_dev_dma_desc_t *desc_addr;
  70861. +
  70862. + uint8_t *start_xfer_buff;
  70863. + /** pointer to the transfer buffer */
  70864. + uint8_t *xfer_buff;
  70865. + /** Number of bytes to transfer */
  70866. + unsigned xfer_len:19;
  70867. + /** Number of bytes transferred. */
  70868. + unsigned xfer_count:19;
  70869. + /** Sent ZLP */
  70870. + unsigned sent_zlp:1;
  70871. + /** Total len for control transfer */
  70872. + unsigned total_len:19;
  70873. +
  70874. + /** stall clear flag */
  70875. + unsigned stall_clear_flag:1;
  70876. +
  70877. + /** SETUP pkt cnt rollover flag for EP0 out*/
  70878. + unsigned stp_rollover;
  70879. +
  70880. +#ifdef DWC_UTE_CFI
  70881. + /* The buffer mode */
  70882. + data_buffer_mode_e buff_mode;
  70883. +
  70884. + /* The chain of DMA descriptors.
  70885. + * MAX_DMA_DESCS_PER_EP will be allocated for each active EP.
  70886. + */
  70887. + dwc_otg_dma_desc_t *descs;
  70888. +
  70889. + /* The DMA address of the descriptors chain start */
  70890. + dma_addr_t descs_dma_addr;
  70891. + /** This variable stores the length of the last enqueued request */
  70892. + uint32_t cfi_req_len;
  70893. +#endif //DWC_UTE_CFI
  70894. +
  70895. +/** Max DMA Descriptor count for any EP */
  70896. +#define MAX_DMA_DESC_CNT 256
  70897. + /** Allocated DMA Desc count */
  70898. + uint32_t desc_cnt;
  70899. +
  70900. + /** bInterval */
  70901. + uint32_t bInterval;
  70902. + /** Next frame num to setup next ISOC transfer */
  70903. + uint32_t frame_num;
  70904. + /** Indicates SOF number overrun in DSTS */
  70905. + uint8_t frm_overrun;
  70906. +
  70907. +#ifdef DWC_UTE_PER_IO
  70908. + /** Next frame num for which will be setup DMA Desc */
  70909. + uint32_t xiso_frame_num;
  70910. + /** bInterval */
  70911. + uint32_t xiso_bInterval;
  70912. + /** Count of currently active transfers - shall be either 0 or 1 */
  70913. + int xiso_active_xfers;
  70914. + int xiso_queued_xfers;
  70915. +#endif
  70916. +#ifdef DWC_EN_ISOC
  70917. + /**
  70918. + * Variables specific for ISOC EPs
  70919. + *
  70920. + */
  70921. + /** DMA addresses of ISOC buffers */
  70922. + dwc_dma_t dma_addr0;
  70923. + dwc_dma_t dma_addr1;
  70924. +
  70925. + dwc_dma_t iso_dma_desc_addr;
  70926. + dwc_otg_dev_dma_desc_t *iso_desc_addr;
  70927. +
  70928. + /** pointer to the transfer buffers */
  70929. + uint8_t *xfer_buff0;
  70930. + uint8_t *xfer_buff1;
  70931. +
  70932. + /** number of ISOC Buffer is processing */
  70933. + uint32_t proc_buf_num;
  70934. + /** Interval of ISOC Buffer processing */
  70935. + uint32_t buf_proc_intrvl;
  70936. + /** Data size for regular frame */
  70937. + uint32_t data_per_frame;
  70938. +
  70939. + /* todo - pattern data support is to be implemented in the future */
  70940. + /** Data size for pattern frame */
  70941. + uint32_t data_pattern_frame;
  70942. + /** Frame number of pattern data */
  70943. + uint32_t sync_frame;
  70944. +
  70945. + /** bInterval */
  70946. + uint32_t bInterval;
  70947. + /** ISO Packet number per frame */
  70948. + uint32_t pkt_per_frm;
  70949. + /** Next frame num for which will be setup DMA Desc */
  70950. + uint32_t next_frame;
  70951. + /** Number of packets per buffer processing */
  70952. + uint32_t pkt_cnt;
  70953. + /** Info for all isoc packets */
  70954. + iso_pkt_info_t *pkt_info;
  70955. + /** current pkt number */
  70956. + uint32_t cur_pkt;
  70957. + /** current pkt number */
  70958. + uint8_t *cur_pkt_addr;
  70959. + /** current pkt number */
  70960. + uint32_t cur_pkt_dma_addr;
  70961. +#endif /* DWC_EN_ISOC */
  70962. +
  70963. +/** @} */
  70964. +} dwc_ep_t;
  70965. +
  70966. +/*
  70967. + * Reasons for halting a host channel.
  70968. + */
  70969. +typedef enum dwc_otg_halt_status {
  70970. + DWC_OTG_HC_XFER_NO_HALT_STATUS,
  70971. + DWC_OTG_HC_XFER_COMPLETE,
  70972. + DWC_OTG_HC_XFER_URB_COMPLETE,
  70973. + DWC_OTG_HC_XFER_ACK,
  70974. + DWC_OTG_HC_XFER_NAK,
  70975. + DWC_OTG_HC_XFER_NYET,
  70976. + DWC_OTG_HC_XFER_STALL,
  70977. + DWC_OTG_HC_XFER_XACT_ERR,
  70978. + DWC_OTG_HC_XFER_FRAME_OVERRUN,
  70979. + DWC_OTG_HC_XFER_BABBLE_ERR,
  70980. + DWC_OTG_HC_XFER_DATA_TOGGLE_ERR,
  70981. + DWC_OTG_HC_XFER_AHB_ERR,
  70982. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE,
  70983. + DWC_OTG_HC_XFER_URB_DEQUEUE
  70984. +} dwc_otg_halt_status_e;
  70985. +
  70986. +/**
  70987. + * Host channel descriptor. This structure represents the state of a single
  70988. + * host channel when acting in host mode. It contains the data items needed to
  70989. + * transfer packets to an endpoint via a host channel.
  70990. + */
  70991. +typedef struct dwc_hc {
  70992. + /** Host channel number used for register address lookup */
  70993. + uint8_t hc_num;
  70994. +
  70995. + /** Device to access */
  70996. + unsigned dev_addr:7;
  70997. +
  70998. + /** EP to access */
  70999. + unsigned ep_num:4;
  71000. +
  71001. + /** EP direction. 0: OUT, 1: IN */
  71002. + unsigned ep_is_in:1;
  71003. +
  71004. + /**
  71005. + * EP speed.
  71006. + * One of the following values:
  71007. + * - DWC_OTG_EP_SPEED_LOW
  71008. + * - DWC_OTG_EP_SPEED_FULL
  71009. + * - DWC_OTG_EP_SPEED_HIGH
  71010. + */
  71011. + unsigned speed:2;
  71012. +#define DWC_OTG_EP_SPEED_LOW 0
  71013. +#define DWC_OTG_EP_SPEED_FULL 1
  71014. +#define DWC_OTG_EP_SPEED_HIGH 2
  71015. +
  71016. + /**
  71017. + * Endpoint type.
  71018. + * One of the following values:
  71019. + * - DWC_OTG_EP_TYPE_CONTROL: 0
  71020. + * - DWC_OTG_EP_TYPE_ISOC: 1
  71021. + * - DWC_OTG_EP_TYPE_BULK: 2
  71022. + * - DWC_OTG_EP_TYPE_INTR: 3
  71023. + */
  71024. + unsigned ep_type:2;
  71025. +
  71026. + /** Max packet size in bytes */
  71027. + unsigned max_packet:11;
  71028. +
  71029. + /**
  71030. + * PID for initial transaction.
  71031. + * 0: DATA0,<br>
  71032. + * 1: DATA2,<br>
  71033. + * 2: DATA1,<br>
  71034. + * 3: MDATA (non-Control EP),
  71035. + * SETUP (Control EP)
  71036. + */
  71037. + unsigned data_pid_start:2;
  71038. +#define DWC_OTG_HC_PID_DATA0 0
  71039. +#define DWC_OTG_HC_PID_DATA2 1
  71040. +#define DWC_OTG_HC_PID_DATA1 2
  71041. +#define DWC_OTG_HC_PID_MDATA 3
  71042. +#define DWC_OTG_HC_PID_SETUP 3
  71043. +
  71044. + /** Number of periodic transactions per (micro)frame */
  71045. + unsigned multi_count:2;
  71046. +
  71047. + /** @name Transfer State */
  71048. + /** @{ */
  71049. +
  71050. + /** Pointer to the current transfer buffer position. */
  71051. + uint8_t *xfer_buff;
  71052. + /**
  71053. + * In Buffer DMA mode this buffer will be used
  71054. + * if xfer_buff is not DWORD aligned.
  71055. + */
  71056. + dwc_dma_t align_buff;
  71057. + /** Total number of bytes to transfer. */
  71058. + uint32_t xfer_len;
  71059. + /** Number of bytes transferred so far. */
  71060. + uint32_t xfer_count;
  71061. + /** Packet count at start of transfer.*/
  71062. + uint16_t start_pkt_count;
  71063. +
  71064. + /**
  71065. + * Flag to indicate whether the transfer has been started. Set to 1 if
  71066. + * it has been started, 0 otherwise.
  71067. + */
  71068. + uint8_t xfer_started;
  71069. +
  71070. + /**
  71071. + * Set to 1 to indicate that a PING request should be issued on this
  71072. + * channel. If 0, process normally.
  71073. + */
  71074. + uint8_t do_ping;
  71075. +
  71076. + /**
  71077. + * Set to 1 to indicate that the error count for this transaction is
  71078. + * non-zero. Set to 0 if the error count is 0.
  71079. + */
  71080. + uint8_t error_state;
  71081. +
  71082. + /**
  71083. + * Set to 1 to indicate that this channel should be halted the next
  71084. + * time a request is queued for the channel. This is necessary in
  71085. + * slave mode if no request queue space is available when an attempt
  71086. + * is made to halt the channel.
  71087. + */
  71088. + uint8_t halt_on_queue;
  71089. +
  71090. + /**
  71091. + * Set to 1 if the host channel has been halted, but the core is not
  71092. + * finished flushing queued requests. Otherwise 0.
  71093. + */
  71094. + uint8_t halt_pending;
  71095. +
  71096. + /**
  71097. + * Reason for halting the host channel.
  71098. + */
  71099. + dwc_otg_halt_status_e halt_status;
  71100. +
  71101. + /*
  71102. + * Split settings for the host channel
  71103. + */
  71104. + uint8_t do_split; /**< Enable split for the channel */
  71105. + uint8_t complete_split; /**< Enable complete split */
  71106. + uint8_t hub_addr; /**< Address of high speed hub */
  71107. +
  71108. + uint8_t port_addr; /**< Port of the low/full speed device */
  71109. + /** Split transaction position
  71110. + * One of the following values:
  71111. + * - DWC_HCSPLIT_XACTPOS_MID
  71112. + * - DWC_HCSPLIT_XACTPOS_BEGIN
  71113. + * - DWC_HCSPLIT_XACTPOS_END
  71114. + * - DWC_HCSPLIT_XACTPOS_ALL */
  71115. + uint8_t xact_pos;
  71116. +
  71117. + /** Set when the host channel does a short read. */
  71118. + uint8_t short_read;
  71119. +
  71120. + /**
  71121. + * Number of requests issued for this channel since it was assigned to
  71122. + * the current transfer (not counting PINGs).
  71123. + */
  71124. + uint8_t requests;
  71125. +
  71126. + /**
  71127. + * Queue Head for the transfer being processed by this channel.
  71128. + */
  71129. + struct dwc_otg_qh *qh;
  71130. +
  71131. + /** @} */
  71132. +
  71133. + /** Entry in list of host channels. */
  71134. + DWC_CIRCLEQ_ENTRY(dwc_hc) hc_list_entry;
  71135. +
  71136. + /** @name Descriptor DMA support */
  71137. + /** @{ */
  71138. +
  71139. + /** Number of Transfer Descriptors */
  71140. + uint16_t ntd;
  71141. +
  71142. + /** Descriptor List DMA address */
  71143. + dwc_dma_t desc_list_addr;
  71144. +
  71145. + /** Scheduling micro-frame bitmap. */
  71146. + uint8_t schinfo;
  71147. +
  71148. + /** @} */
  71149. +} dwc_hc_t;
  71150. +
  71151. +/**
  71152. + * The following parameters may be specified when starting the module. These
  71153. + * parameters define how the DWC_otg controller should be configured.
  71154. + */
  71155. +typedef struct dwc_otg_core_params {
  71156. + int32_t opt;
  71157. +
  71158. + /**
  71159. + * Specifies the OTG capabilities. The driver will automatically
  71160. + * detect the value for this parameter if none is specified.
  71161. + * 0 - HNP and SRP capable (default)
  71162. + * 1 - SRP Only capable
  71163. + * 2 - No HNP/SRP capable
  71164. + */
  71165. + int32_t otg_cap;
  71166. +
  71167. + /**
  71168. + * Specifies whether to use slave or DMA mode for accessing the data
  71169. + * FIFOs. The driver will automatically detect the value for this
  71170. + * parameter if none is specified.
  71171. + * 0 - Slave
  71172. + * 1 - DMA (default, if available)
  71173. + */
  71174. + int32_t dma_enable;
  71175. +
  71176. + /**
  71177. + * When DMA mode is enabled specifies whether to use address DMA or DMA
  71178. + * Descriptor mode for accessing the data FIFOs in device mode. The driver
  71179. + * will automatically detect the value for this if none is specified.
  71180. + * 0 - address DMA
  71181. + * 1 - DMA Descriptor(default, if available)
  71182. + */
  71183. + int32_t dma_desc_enable;
  71184. + /** The DMA Burst size (applicable only for External DMA
  71185. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  71186. + */
  71187. + int32_t dma_burst_size; /* Translate this to GAHBCFG values */
  71188. +
  71189. + /**
  71190. + * Specifies the maximum speed of operation in host and device mode.
  71191. + * The actual speed depends on the speed of the attached device and
  71192. + * the value of phy_type. The actual speed depends on the speed of the
  71193. + * attached device.
  71194. + * 0 - High Speed (default)
  71195. + * 1 - Full Speed
  71196. + */
  71197. + int32_t speed;
  71198. + /** Specifies whether low power mode is supported when attached
  71199. + * to a Full Speed or Low Speed device in host mode.
  71200. + * 0 - Don't support low power mode (default)
  71201. + * 1 - Support low power mode
  71202. + */
  71203. + int32_t host_support_fs_ls_low_power;
  71204. +
  71205. + /** Specifies the PHY clock rate in low power mode when connected to a
  71206. + * Low Speed device in host mode. This parameter is applicable only if
  71207. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  71208. + * then defaults to 6 MHZ otherwise 48 MHZ.
  71209. + *
  71210. + * 0 - 48 MHz
  71211. + * 1 - 6 MHz
  71212. + */
  71213. + int32_t host_ls_low_power_phy_clk;
  71214. +
  71215. + /**
  71216. + * 0 - Use cC FIFO size parameters
  71217. + * 1 - Allow dynamic FIFO sizing (default)
  71218. + */
  71219. + int32_t enable_dynamic_fifo;
  71220. +
  71221. + /** Total number of 4-byte words in the data FIFO memory. This
  71222. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  71223. + * Tx FIFOs.
  71224. + * 32 to 32768 (default 8192)
  71225. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  71226. + */
  71227. + int32_t data_fifo_size;
  71228. +
  71229. + /** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  71230. + * FIFO sizing is enabled.
  71231. + * 16 to 32768 (default 1064)
  71232. + */
  71233. + int32_t dev_rx_fifo_size;
  71234. +
  71235. + /** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  71236. + * when dynamic FIFO sizing is enabled.
  71237. + * 16 to 32768 (default 1024)
  71238. + */
  71239. + int32_t dev_nperio_tx_fifo_size;
  71240. +
  71241. + /** Number of 4-byte words in each of the periodic Tx FIFOs in device
  71242. + * mode when dynamic FIFO sizing is enabled.
  71243. + * 4 to 768 (default 256)
  71244. + */
  71245. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  71246. +
  71247. + /** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  71248. + * FIFO sizing is enabled.
  71249. + * 16 to 32768 (default 1024)
  71250. + */
  71251. + int32_t host_rx_fifo_size;
  71252. +
  71253. + /** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  71254. + * when Dynamic FIFO sizing is enabled in the core.
  71255. + * 16 to 32768 (default 1024)
  71256. + */
  71257. + int32_t host_nperio_tx_fifo_size;
  71258. +
  71259. + /** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  71260. + * FIFO sizing is enabled.
  71261. + * 16 to 32768 (default 1024)
  71262. + */
  71263. + int32_t host_perio_tx_fifo_size;
  71264. +
  71265. + /** The maximum transfer size supported in bytes.
  71266. + * 2047 to 65,535 (default 65,535)
  71267. + */
  71268. + int32_t max_transfer_size;
  71269. +
  71270. + /** The maximum number of packets in a transfer.
  71271. + * 15 to 511 (default 511)
  71272. + */
  71273. + int32_t max_packet_count;
  71274. +
  71275. + /** The number of host channel registers to use.
  71276. + * 1 to 16 (default 12)
  71277. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  71278. + */
  71279. + int32_t host_channels;
  71280. +
  71281. + /** The number of endpoints in addition to EP0 available for device
  71282. + * mode operations.
  71283. + * 1 to 15 (default 6 IN and OUT)
  71284. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  71285. + * endpoints in addition to EP0.
  71286. + */
  71287. + int32_t dev_endpoints;
  71288. +
  71289. + /**
  71290. + * Specifies the type of PHY interface to use. By default, the driver
  71291. + * will automatically detect the phy_type.
  71292. + *
  71293. + * 0 - Full Speed PHY
  71294. + * 1 - UTMI+ (default)
  71295. + * 2 - ULPI
  71296. + */
  71297. + int32_t phy_type;
  71298. +
  71299. + /**
  71300. + * Specifies the UTMI+ Data Width. This parameter is
  71301. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  71302. + * PHY_TYPE, this parameter indicates the data width between
  71303. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  71304. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  71305. + * to "8 and 16 bits", meaning that the core has been
  71306. + * configured to work at either data path width.
  71307. + *
  71308. + * 8 or 16 bits (default 16)
  71309. + */
  71310. + int32_t phy_utmi_width;
  71311. +
  71312. + /**
  71313. + * Specifies whether the ULPI operates at double or single
  71314. + * data rate. This parameter is only applicable if PHY_TYPE is
  71315. + * ULPI.
  71316. + *
  71317. + * 0 - single data rate ULPI interface with 8 bit wide data
  71318. + * bus (default)
  71319. + * 1 - double data rate ULPI interface with 4 bit wide data
  71320. + * bus
  71321. + */
  71322. + int32_t phy_ulpi_ddr;
  71323. +
  71324. + /**
  71325. + * Specifies whether to use the internal or external supply to
  71326. + * drive the vbus with a ULPI phy.
  71327. + */
  71328. + int32_t phy_ulpi_ext_vbus;
  71329. +
  71330. + /**
  71331. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  71332. + * parameter is only applicable if PHY_TYPE is FS.
  71333. + * 0 - No (default)
  71334. + * 1 - Yes
  71335. + */
  71336. + int32_t i2c_enable;
  71337. +
  71338. + int32_t ulpi_fs_ls;
  71339. +
  71340. + int32_t ts_dline;
  71341. +
  71342. + /**
  71343. + * Specifies whether dedicated transmit FIFOs are
  71344. + * enabled for non periodic IN endpoints in device mode
  71345. + * 0 - No
  71346. + * 1 - Yes
  71347. + */
  71348. + int32_t en_multiple_tx_fifo;
  71349. +
  71350. + /** Number of 4-byte words in each of the Tx FIFOs in device
  71351. + * mode when dynamic FIFO sizing is enabled.
  71352. + * 4 to 768 (default 256)
  71353. + */
  71354. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  71355. +
  71356. + /** Thresholding enable flag-
  71357. + * bit 0 - enable non-ISO Tx thresholding
  71358. + * bit 1 - enable ISO Tx thresholding
  71359. + * bit 2 - enable Rx thresholding
  71360. + */
  71361. + uint32_t thr_ctl;
  71362. +
  71363. + /** Thresholding length for Tx
  71364. + * FIFOs in 32 bit DWORDs
  71365. + */
  71366. + uint32_t tx_thr_length;
  71367. +
  71368. + /** Thresholding length for Rx
  71369. + * FIFOs in 32 bit DWORDs
  71370. + */
  71371. + uint32_t rx_thr_length;
  71372. +
  71373. + /**
  71374. + * Specifies whether LPM (Link Power Management) support is enabled
  71375. + */
  71376. + int32_t lpm_enable;
  71377. +
  71378. + /** Per Transfer Interrupt
  71379. + * mode enable flag
  71380. + * 1 - Enabled
  71381. + * 0 - Disabled
  71382. + */
  71383. + int32_t pti_enable;
  71384. +
  71385. + /** Multi Processor Interrupt
  71386. + * mode enable flag
  71387. + * 1 - Enabled
  71388. + * 0 - Disabled
  71389. + */
  71390. + int32_t mpi_enable;
  71391. +
  71392. + /** IS_USB Capability
  71393. + * 1 - Enabled
  71394. + * 0 - Disabled
  71395. + */
  71396. + int32_t ic_usb_cap;
  71397. +
  71398. + /** AHB Threshold Ratio
  71399. + * 2'b00 AHB Threshold = MAC Threshold
  71400. + * 2'b01 AHB Threshold = 1/2 MAC Threshold
  71401. + * 2'b10 AHB Threshold = 1/4 MAC Threshold
  71402. + * 2'b11 AHB Threshold = 1/8 MAC Threshold
  71403. + */
  71404. + int32_t ahb_thr_ratio;
  71405. +
  71406. + /** ADP Support
  71407. + * 1 - Enabled
  71408. + * 0 - Disabled
  71409. + */
  71410. + int32_t adp_supp_enable;
  71411. +
  71412. + /** HFIR Reload Control
  71413. + * 0 - The HFIR cannot be reloaded dynamically.
  71414. + * 1 - Allow dynamic reloading of the HFIR register during runtime.
  71415. + */
  71416. + int32_t reload_ctl;
  71417. +
  71418. + /** DCFG: Enable device Out NAK
  71419. + * 0 - The core does not set NAK after Bulk Out transfer complete.
  71420. + * 1 - The core sets NAK after Bulk OUT transfer complete.
  71421. + */
  71422. + int32_t dev_out_nak;
  71423. +
  71424. + /** DCFG: Enable Continue on BNA
  71425. + * After receiving BNA interrupt the core disables the endpoint,when the
  71426. + * endpoint is re-enabled by the application the core starts processing
  71427. + * 0 - from the DOEPDMA descriptor
  71428. + * 1 - from the descriptor which received the BNA.
  71429. + */
  71430. + int32_t cont_on_bna;
  71431. +
  71432. + /** GAHBCFG: AHB Single Support
  71433. + * This bit when programmed supports SINGLE transfers for remainder
  71434. + * data in a transfer for DMA mode of operation.
  71435. + * 0 - in this case the remainder data will be sent using INCR burst size.
  71436. + * 1 - in this case the remainder data will be sent using SINGLE burst size.
  71437. + */
  71438. + int32_t ahb_single;
  71439. +
  71440. + /** Core Power down mode
  71441. + * 0 - No Power Down is enabled
  71442. + * 1 - Reserved
  71443. + * 2 - Complete Power Down (Hibernation)
  71444. + */
  71445. + int32_t power_down;
  71446. +
  71447. + /** OTG revision supported
  71448. + * 0 - OTG 1.3 revision
  71449. + * 1 - OTG 2.0 revision
  71450. + */
  71451. + int32_t otg_ver;
  71452. +
  71453. +} dwc_otg_core_params_t;
  71454. +
  71455. +#ifdef DEBUG
  71456. +struct dwc_otg_core_if;
  71457. +typedef struct hc_xfer_info {
  71458. + struct dwc_otg_core_if *core_if;
  71459. + dwc_hc_t *hc;
  71460. +} hc_xfer_info_t;
  71461. +#endif
  71462. +
  71463. +typedef struct ep_xfer_info {
  71464. + struct dwc_otg_core_if *core_if;
  71465. + dwc_ep_t *ep;
  71466. + uint8_t state;
  71467. +} ep_xfer_info_t;
  71468. +/*
  71469. + * Device States
  71470. + */
  71471. +typedef enum dwc_otg_lx_state {
  71472. + /** On state */
  71473. + DWC_OTG_L0,
  71474. + /** LPM sleep state*/
  71475. + DWC_OTG_L1,
  71476. + /** USB suspend state*/
  71477. + DWC_OTG_L2,
  71478. + /** Off state*/
  71479. + DWC_OTG_L3
  71480. +} dwc_otg_lx_state_e;
  71481. +
  71482. +struct dwc_otg_global_regs_backup {
  71483. + uint32_t gotgctl_local;
  71484. + uint32_t gintmsk_local;
  71485. + uint32_t gahbcfg_local;
  71486. + uint32_t gusbcfg_local;
  71487. + uint32_t grxfsiz_local;
  71488. + uint32_t gnptxfsiz_local;
  71489. +#ifdef CONFIG_USB_DWC_OTG_LPM
  71490. + uint32_t glpmcfg_local;
  71491. +#endif
  71492. + uint32_t gi2cctl_local;
  71493. + uint32_t hptxfsiz_local;
  71494. + uint32_t pcgcctl_local;
  71495. + uint32_t gdfifocfg_local;
  71496. + uint32_t dtxfsiz_local[MAX_EPS_CHANNELS];
  71497. + uint32_t gpwrdn_local;
  71498. + uint32_t xhib_pcgcctl;
  71499. + uint32_t xhib_gpwrdn;
  71500. +};
  71501. +
  71502. +struct dwc_otg_host_regs_backup {
  71503. + uint32_t hcfg_local;
  71504. + uint32_t haintmsk_local;
  71505. + uint32_t hcintmsk_local[MAX_EPS_CHANNELS];
  71506. + uint32_t hprt0_local;
  71507. + uint32_t hfir_local;
  71508. +};
  71509. +
  71510. +struct dwc_otg_dev_regs_backup {
  71511. + uint32_t dcfg;
  71512. + uint32_t dctl;
  71513. + uint32_t daintmsk;
  71514. + uint32_t diepmsk;
  71515. + uint32_t doepmsk;
  71516. + uint32_t diepctl[MAX_EPS_CHANNELS];
  71517. + uint32_t dieptsiz[MAX_EPS_CHANNELS];
  71518. + uint32_t diepdma[MAX_EPS_CHANNELS];
  71519. +};
  71520. +/**
  71521. + * The <code>dwc_otg_core_if</code> structure contains information needed to manage
  71522. + * the DWC_otg controller acting in either host or device mode. It
  71523. + * represents the programming view of the controller as a whole.
  71524. + */
  71525. +struct dwc_otg_core_if {
  71526. + /** Parameters that define how the core should be configured.*/
  71527. + dwc_otg_core_params_t *core_params;
  71528. +
  71529. + /** Core Global registers starting at offset 000h. */
  71530. + dwc_otg_core_global_regs_t *core_global_regs;
  71531. +
  71532. + /** Device-specific information */
  71533. + dwc_otg_dev_if_t *dev_if;
  71534. + /** Host-specific information */
  71535. + dwc_otg_host_if_t *host_if;
  71536. +
  71537. + /** Value from SNPSID register */
  71538. + uint32_t snpsid;
  71539. +
  71540. + /*
  71541. + * Set to 1 if the core PHY interface bits in USBCFG have been
  71542. + * initialized.
  71543. + */
  71544. + uint8_t phy_init_done;
  71545. +
  71546. + /*
  71547. + * SRP Success flag, set by srp success interrupt in FS I2C mode
  71548. + */
  71549. + uint8_t srp_success;
  71550. + uint8_t srp_timer_started;
  71551. + /** Timer for SRP. If it expires before SRP is successful
  71552. + * clear the SRP. */
  71553. + dwc_timer_t *srp_timer;
  71554. +
  71555. +#ifdef DWC_DEV_SRPCAP
  71556. + /* This timer is needed to power on the hibernated host core if SRP is not
  71557. + * initiated on connected SRP capable device for limited period of time
  71558. + */
  71559. + uint8_t pwron_timer_started;
  71560. + dwc_timer_t *pwron_timer;
  71561. +#endif
  71562. + /* Common configuration information */
  71563. + /** Power and Clock Gating Control Register */
  71564. + volatile uint32_t *pcgcctl;
  71565. +#define DWC_OTG_PCGCCTL_OFFSET 0xE00
  71566. +
  71567. + /** Push/pop addresses for endpoints or host channels.*/
  71568. + uint32_t *data_fifo[MAX_EPS_CHANNELS];
  71569. +#define DWC_OTG_DATA_FIFO_OFFSET 0x1000
  71570. +#define DWC_OTG_DATA_FIFO_SIZE 0x1000
  71571. +
  71572. + /** Total RAM for FIFOs (Bytes) */
  71573. + uint16_t total_fifo_size;
  71574. + /** Size of Rx FIFO (Bytes) */
  71575. + uint16_t rx_fifo_size;
  71576. + /** Size of Non-periodic Tx FIFO (Bytes) */
  71577. + uint16_t nperio_tx_fifo_size;
  71578. +
  71579. + /** 1 if DMA is enabled, 0 otherwise. */
  71580. + uint8_t dma_enable;
  71581. +
  71582. + /** 1 if DMA descriptor is enabled, 0 otherwise. */
  71583. + uint8_t dma_desc_enable;
  71584. +
  71585. + /** 1 if PTI Enhancement mode is enabled, 0 otherwise. */
  71586. + uint8_t pti_enh_enable;
  71587. +
  71588. + /** 1 if MPI Enhancement mode is enabled, 0 otherwise. */
  71589. + uint8_t multiproc_int_enable;
  71590. +
  71591. + /** 1 if dedicated Tx FIFOs are enabled, 0 otherwise. */
  71592. + uint8_t en_multiple_tx_fifo;
  71593. +
  71594. + /** Set to 1 if multiple packets of a high-bandwidth transfer is in
  71595. + * process of being queued */
  71596. + uint8_t queuing_high_bandwidth;
  71597. +
  71598. + /** Hardware Configuration -- stored here for convenience.*/
  71599. + hwcfg1_data_t hwcfg1;
  71600. + hwcfg2_data_t hwcfg2;
  71601. + hwcfg3_data_t hwcfg3;
  71602. + hwcfg4_data_t hwcfg4;
  71603. + fifosize_data_t hptxfsiz;
  71604. +
  71605. + /** Host and Device Configuration -- stored here for convenience.*/
  71606. + hcfg_data_t hcfg;
  71607. + dcfg_data_t dcfg;
  71608. +
  71609. + /** The operational State, during transations
  71610. + * (a_host>>a_peripherial and b_device=>b_host) this may not
  71611. + * match the core but allows the software to determine
  71612. + * transitions.
  71613. + */
  71614. + uint8_t op_state;
  71615. +
  71616. + /**
  71617. + * Set to 1 if the HCD needs to be restarted on a session request
  71618. + * interrupt. This is required if no connector ID status change has
  71619. + * occurred since the HCD was last disconnected.
  71620. + */
  71621. + uint8_t restart_hcd_on_session_req;
  71622. +
  71623. + /** HCD callbacks */
  71624. + /** A-Device is a_host */
  71625. +#define A_HOST (1)
  71626. + /** A-Device is a_suspend */
  71627. +#define A_SUSPEND (2)
  71628. + /** A-Device is a_peripherial */
  71629. +#define A_PERIPHERAL (3)
  71630. + /** B-Device is operating as a Peripheral. */
  71631. +#define B_PERIPHERAL (4)
  71632. + /** B-Device is operating as a Host. */
  71633. +#define B_HOST (5)
  71634. +
  71635. + /** HCD callbacks */
  71636. + struct dwc_otg_cil_callbacks *hcd_cb;
  71637. + /** PCD callbacks */
  71638. + struct dwc_otg_cil_callbacks *pcd_cb;
  71639. +
  71640. + /** Device mode Periodic Tx FIFO Mask */
  71641. + uint32_t p_tx_msk;
  71642. + /** Device mode Periodic Tx FIFO Mask */
  71643. + uint32_t tx_msk;
  71644. +
  71645. + /** Workqueue object used for handling several interrupts */
  71646. + dwc_workq_t *wq_otg;
  71647. +
  71648. + /** Timer object used for handling "Wakeup Detected" Interrupt */
  71649. + dwc_timer_t *wkp_timer;
  71650. + /** This arrays used for debug purposes for DEV OUT NAK enhancement */
  71651. + uint32_t start_doeptsiz_val[MAX_EPS_CHANNELS];
  71652. + ep_xfer_info_t ep_xfer_info[MAX_EPS_CHANNELS];
  71653. + dwc_timer_t *ep_xfer_timer[MAX_EPS_CHANNELS];
  71654. +#ifdef DEBUG
  71655. + uint32_t start_hcchar_val[MAX_EPS_CHANNELS];
  71656. +
  71657. + hc_xfer_info_t hc_xfer_info[MAX_EPS_CHANNELS];
  71658. + dwc_timer_t *hc_xfer_timer[MAX_EPS_CHANNELS];
  71659. +
  71660. + uint32_t hfnum_7_samples;
  71661. + uint64_t hfnum_7_frrem_accum;
  71662. + uint32_t hfnum_0_samples;
  71663. + uint64_t hfnum_0_frrem_accum;
  71664. + uint32_t hfnum_other_samples;
  71665. + uint64_t hfnum_other_frrem_accum;
  71666. +#endif
  71667. +
  71668. +#ifdef DWC_UTE_CFI
  71669. + uint16_t pwron_rxfsiz;
  71670. + uint16_t pwron_gnptxfsiz;
  71671. + uint16_t pwron_txfsiz[15];
  71672. +
  71673. + uint16_t init_rxfsiz;
  71674. + uint16_t init_gnptxfsiz;
  71675. + uint16_t init_txfsiz[15];
  71676. +#endif
  71677. +
  71678. + /** Lx state of device */
  71679. + dwc_otg_lx_state_e lx_state;
  71680. +
  71681. + /** Saved Core Global registers */
  71682. + struct dwc_otg_global_regs_backup *gr_backup;
  71683. + /** Saved Host registers */
  71684. + struct dwc_otg_host_regs_backup *hr_backup;
  71685. + /** Saved Device registers */
  71686. + struct dwc_otg_dev_regs_backup *dr_backup;
  71687. +
  71688. + /** Power Down Enable */
  71689. + uint32_t power_down;
  71690. +
  71691. + /** ADP support Enable */
  71692. + uint32_t adp_enable;
  71693. +
  71694. + /** ADP structure object */
  71695. + dwc_otg_adp_t adp;
  71696. +
  71697. + /** hibernation/suspend flag */
  71698. + int hibernation_suspend;
  71699. +
  71700. + /** Device mode extended hibernation flag */
  71701. + int xhib;
  71702. +
  71703. + /** OTG revision supported */
  71704. + uint32_t otg_ver;
  71705. +
  71706. + /** OTG status flag used for HNP polling */
  71707. + uint8_t otg_sts;
  71708. +
  71709. + /** Pointer to either hcd->lock or pcd->lock */
  71710. + dwc_spinlock_t *lock;
  71711. +
  71712. + /** Start predict NextEP based on Learning Queue if equal 1,
  71713. + * also used as counter of disabled NP IN EP's */
  71714. + uint8_t start_predict;
  71715. +
  71716. + /** NextEp sequence, including EP0: nextep_seq[] = EP if non-periodic and
  71717. + * active, 0xff otherwise */
  71718. + uint8_t nextep_seq[MAX_EPS_CHANNELS];
  71719. +
  71720. + /** Index of fisrt EP in nextep_seq array which should be re-enabled **/
  71721. + uint8_t first_in_nextep_seq;
  71722. +
  71723. + /** Frame number while entering to ISR - needed for ISOCs **/
  71724. + uint32_t frame_num;
  71725. +
  71726. +};
  71727. +
  71728. +#ifdef DEBUG
  71729. +/*
  71730. + * This function is called when transfer is timed out.
  71731. + */
  71732. +extern void hc_xfer_timeout(void *ptr);
  71733. +#endif
  71734. +
  71735. +/*
  71736. + * This function is called when transfer is timed out on endpoint.
  71737. + */
  71738. +extern void ep_xfer_timeout(void *ptr);
  71739. +
  71740. +/*
  71741. + * The following functions are functions for works
  71742. + * using during handling some interrupts
  71743. + */
  71744. +extern void w_conn_id_status_change(void *p);
  71745. +
  71746. +extern void w_wakeup_detected(void *p);
  71747. +
  71748. +/** Saves global register values into system memory. */
  71749. +extern int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if);
  71750. +/** Saves device register values into system memory. */
  71751. +extern int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if);
  71752. +/** Saves host register values into system memory. */
  71753. +extern int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if);
  71754. +/** Restore global register values. */
  71755. +extern int dwc_otg_restore_global_regs(dwc_otg_core_if_t * core_if);
  71756. +/** Restore host register values. */
  71757. +extern int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset);
  71758. +/** Restore device register values. */
  71759. +extern int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if,
  71760. + int rem_wakeup);
  71761. +extern int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if);
  71762. +extern int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode,
  71763. + int is_host);
  71764. +
  71765. +extern int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  71766. + int restore_mode, int reset);
  71767. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  71768. + int rem_wakeup, int reset);
  71769. +
  71770. +/*
  71771. + * The following functions support initialization of the CIL driver component
  71772. + * and the DWC_otg controller.
  71773. + */
  71774. +extern void dwc_otg_core_host_init(dwc_otg_core_if_t * _core_if);
  71775. +extern void dwc_otg_core_dev_init(dwc_otg_core_if_t * _core_if);
  71776. +
  71777. +/** @name Device CIL Functions
  71778. + * The following functions support managing the DWC_otg controller in device
  71779. + * mode.
  71780. + */
  71781. +/**@{*/
  71782. +extern void dwc_otg_wakeup(dwc_otg_core_if_t * _core_if);
  71783. +extern void dwc_otg_read_setup_packet(dwc_otg_core_if_t * _core_if,
  71784. + uint32_t * _dest);
  71785. +extern uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * _core_if);
  71786. +extern void dwc_otg_ep0_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  71787. +extern void dwc_otg_ep_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  71788. +extern void dwc_otg_ep_deactivate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  71789. +extern void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * _core_if,
  71790. + dwc_ep_t * _ep);
  71791. +extern void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * _core_if,
  71792. + dwc_ep_t * _ep);
  71793. +extern void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * _core_if,
  71794. + dwc_ep_t * _ep);
  71795. +extern void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * _core_if,
  71796. + dwc_ep_t * _ep);
  71797. +extern void dwc_otg_ep_write_packet(dwc_otg_core_if_t * _core_if,
  71798. + dwc_ep_t * _ep, int _dma);
  71799. +extern void dwc_otg_ep_set_stall(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  71800. +extern void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * _core_if,
  71801. + dwc_ep_t * _ep);
  71802. +extern void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * _core_if);
  71803. +
  71804. +#ifdef DWC_EN_ISOC
  71805. +extern void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  71806. + dwc_ep_t * ep);
  71807. +extern void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  71808. + dwc_ep_t * ep);
  71809. +#endif /* DWC_EN_ISOC */
  71810. +/**@}*/
  71811. +
  71812. +/** @name Host CIL Functions
  71813. + * The following functions support managing the DWC_otg controller in host
  71814. + * mode.
  71815. + */
  71816. +/**@{*/
  71817. +extern void dwc_otg_hc_init(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  71818. +extern void dwc_otg_hc_halt(dwc_otg_core_if_t * _core_if,
  71819. + dwc_hc_t * _hc, dwc_otg_halt_status_e _halt_status);
  71820. +extern void dwc_otg_hc_cleanup(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  71821. +extern void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * _core_if,
  71822. + dwc_hc_t * _hc);
  71823. +extern int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * _core_if,
  71824. + dwc_hc_t * _hc);
  71825. +extern void dwc_otg_hc_do_ping(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  71826. +extern void dwc_otg_hc_write_packet(dwc_otg_core_if_t * _core_if,
  71827. + dwc_hc_t * _hc);
  71828. +extern void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * _core_if);
  71829. +extern void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * _core_if);
  71830. +
  71831. +extern void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if,
  71832. + dwc_hc_t * hc);
  71833. +
  71834. +extern uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if);
  71835. +
  71836. +/* Macro used to clear one channel interrupt */
  71837. +#define clear_hc_int(_hc_regs_, _intr_) \
  71838. +do { \
  71839. + hcint_data_t hcint_clear = {.d32 = 0}; \
  71840. + hcint_clear.b._intr_ = 1; \
  71841. + DWC_WRITE_REG32(&(_hc_regs_)->hcint, hcint_clear.d32); \
  71842. +} while (0)
  71843. +
  71844. +/*
  71845. + * Macro used to disable one channel interrupt. Channel interrupts are
  71846. + * disabled when the channel is halted or released by the interrupt handler.
  71847. + * There is no need to handle further interrupts of that type until the
  71848. + * channel is re-assigned. In fact, subsequent handling may cause crashes
  71849. + * because the channel structures are cleaned up when the channel is released.
  71850. + */
  71851. +#define disable_hc_int(_hc_regs_, _intr_) \
  71852. +do { \
  71853. + hcintmsk_data_t hcintmsk = {.d32 = 0}; \
  71854. + hcintmsk.b._intr_ = 1; \
  71855. + DWC_MODIFY_REG32(&(_hc_regs_)->hcintmsk, hcintmsk.d32, 0); \
  71856. +} while (0)
  71857. +
  71858. +/**
  71859. + * This function Reads HPRT0 in preparation to modify. It keeps the
  71860. + * WC bits 0 so that if they are read as 1, they won't clear when you
  71861. + * write it back
  71862. + */
  71863. +static inline uint32_t dwc_otg_read_hprt0(dwc_otg_core_if_t * _core_if)
  71864. +{
  71865. + hprt0_data_t hprt0;
  71866. + hprt0.d32 = DWC_READ_REG32(_core_if->host_if->hprt0);
  71867. + hprt0.b.prtena = 0;
  71868. + hprt0.b.prtconndet = 0;
  71869. + hprt0.b.prtenchng = 0;
  71870. + hprt0.b.prtovrcurrchng = 0;
  71871. + return hprt0.d32;
  71872. +}
  71873. +
  71874. +/**@}*/
  71875. +
  71876. +/** @name Common CIL Functions
  71877. + * The following functions support managing the DWC_otg controller in either
  71878. + * device or host mode.
  71879. + */
  71880. +/**@{*/
  71881. +
  71882. +extern void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  71883. + uint8_t * dest, uint16_t bytes);
  71884. +
  71885. +extern void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * _core_if, const int _num);
  71886. +extern void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * _core_if);
  71887. +extern void dwc_otg_core_reset(dwc_otg_core_if_t * _core_if);
  71888. +
  71889. +/**
  71890. + * This function returns the Core Interrupt register.
  71891. + */
  71892. +static inline uint32_t dwc_otg_read_core_intr(dwc_otg_core_if_t * core_if)
  71893. +{
  71894. + return (DWC_READ_REG32(&core_if->core_global_regs->gintsts) &
  71895. + DWC_READ_REG32(&core_if->core_global_regs->gintmsk));
  71896. +}
  71897. +
  71898. +/**
  71899. + * This function returns the OTG Interrupt register.
  71900. + */
  71901. +static inline uint32_t dwc_otg_read_otg_intr(dwc_otg_core_if_t * core_if)
  71902. +{
  71903. + return (DWC_READ_REG32(&core_if->core_global_regs->gotgint));
  71904. +}
  71905. +
  71906. +/**
  71907. + * This function reads the Device All Endpoints Interrupt register and
  71908. + * returns the IN endpoint interrupt bits.
  71909. + */
  71910. +static inline uint32_t dwc_otg_read_dev_all_in_ep_intr(dwc_otg_core_if_t *
  71911. + core_if)
  71912. +{
  71913. +
  71914. + uint32_t v;
  71915. +
  71916. + if (core_if->multiproc_int_enable) {
  71917. + v = DWC_READ_REG32(&core_if->dev_if->
  71918. + dev_global_regs->deachint) &
  71919. + DWC_READ_REG32(&core_if->
  71920. + dev_if->dev_global_regs->deachintmsk);
  71921. + } else {
  71922. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  71923. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  71924. + }
  71925. + return (v & 0xffff);
  71926. +}
  71927. +
  71928. +/**
  71929. + * This function reads the Device All Endpoints Interrupt register and
  71930. + * returns the OUT endpoint interrupt bits.
  71931. + */
  71932. +static inline uint32_t dwc_otg_read_dev_all_out_ep_intr(dwc_otg_core_if_t *
  71933. + core_if)
  71934. +{
  71935. + uint32_t v;
  71936. +
  71937. + if (core_if->multiproc_int_enable) {
  71938. + v = DWC_READ_REG32(&core_if->dev_if->
  71939. + dev_global_regs->deachint) &
  71940. + DWC_READ_REG32(&core_if->
  71941. + dev_if->dev_global_regs->deachintmsk);
  71942. + } else {
  71943. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  71944. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  71945. + }
  71946. +
  71947. + return ((v & 0xffff0000) >> 16);
  71948. +}
  71949. +
  71950. +/**
  71951. + * This function returns the Device IN EP Interrupt register
  71952. + */
  71953. +static inline uint32_t dwc_otg_read_dev_in_ep_intr(dwc_otg_core_if_t * core_if,
  71954. + dwc_ep_t * ep)
  71955. +{
  71956. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  71957. + uint32_t v, msk, emp;
  71958. +
  71959. + if (core_if->multiproc_int_enable) {
  71960. + msk =
  71961. + DWC_READ_REG32(&dev_if->
  71962. + dev_global_regs->diepeachintmsk[ep->num]);
  71963. + emp =
  71964. + DWC_READ_REG32(&dev_if->
  71965. + dev_global_regs->dtknqr4_fifoemptymsk);
  71966. + msk |= ((emp >> ep->num) & 0x1) << 7;
  71967. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  71968. + } else {
  71969. + msk = DWC_READ_REG32(&dev_if->dev_global_regs->diepmsk);
  71970. + emp =
  71971. + DWC_READ_REG32(&dev_if->
  71972. + dev_global_regs->dtknqr4_fifoemptymsk);
  71973. + msk |= ((emp >> ep->num) & 0x1) << 7;
  71974. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  71975. + }
  71976. +
  71977. + return v;
  71978. +}
  71979. +
  71980. +/**
  71981. + * This function returns the Device OUT EP Interrupt register
  71982. + */
  71983. +static inline uint32_t dwc_otg_read_dev_out_ep_intr(dwc_otg_core_if_t *
  71984. + _core_if, dwc_ep_t * _ep)
  71985. +{
  71986. + dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
  71987. + uint32_t v;
  71988. + doepmsk_data_t msk = {.d32 = 0 };
  71989. +
  71990. + if (_core_if->multiproc_int_enable) {
  71991. + msk.d32 =
  71992. + DWC_READ_REG32(&dev_if->
  71993. + dev_global_regs->doepeachintmsk[_ep->num]);
  71994. + if (_core_if->pti_enh_enable) {
  71995. + msk.b.pktdrpsts = 1;
  71996. + }
  71997. + v = DWC_READ_REG32(&dev_if->
  71998. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  71999. + } else {
  72000. + msk.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->doepmsk);
  72001. + if (_core_if->pti_enh_enable) {
  72002. + msk.b.pktdrpsts = 1;
  72003. + }
  72004. + v = DWC_READ_REG32(&dev_if->
  72005. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  72006. + }
  72007. + return v;
  72008. +}
  72009. +
  72010. +/**
  72011. + * This function returns the Host All Channel Interrupt register
  72012. + */
  72013. +static inline uint32_t dwc_otg_read_host_all_channels_intr(dwc_otg_core_if_t *
  72014. + _core_if)
  72015. +{
  72016. + return (DWC_READ_REG32(&_core_if->host_if->host_global_regs->haint));
  72017. +}
  72018. +
  72019. +static inline uint32_t dwc_otg_read_host_channel_intr(dwc_otg_core_if_t *
  72020. + _core_if, dwc_hc_t * _hc)
  72021. +{
  72022. + return (DWC_READ_REG32
  72023. + (&_core_if->host_if->hc_regs[_hc->hc_num]->hcint));
  72024. +}
  72025. +
  72026. +/**
  72027. + * This function returns the mode of the operation, host or device.
  72028. + *
  72029. + * @return 0 - Device Mode, 1 - Host Mode
  72030. + */
  72031. +static inline uint32_t dwc_otg_mode(dwc_otg_core_if_t * _core_if)
  72032. +{
  72033. + return (DWC_READ_REG32(&_core_if->core_global_regs->gintsts) & 0x1);
  72034. +}
  72035. +
  72036. +/**@}*/
  72037. +
  72038. +/**
  72039. + * DWC_otg CIL callback structure. This structure allows the HCD and
  72040. + * PCD to register functions used for starting and stopping the PCD
  72041. + * and HCD for role change on for a DRD.
  72042. + */
  72043. +typedef struct dwc_otg_cil_callbacks {
  72044. + /** Start function for role change */
  72045. + int (*start) (void *_p);
  72046. + /** Stop Function for role change */
  72047. + int (*stop) (void *_p);
  72048. + /** Disconnect Function for role change */
  72049. + int (*disconnect) (void *_p);
  72050. + /** Resume/Remote wakeup Function */
  72051. + int (*resume_wakeup) (void *_p);
  72052. + /** Suspend function */
  72053. + int (*suspend) (void *_p);
  72054. + /** Session Start (SRP) */
  72055. + int (*session_start) (void *_p);
  72056. +#ifdef CONFIG_USB_DWC_OTG_LPM
  72057. + /** Sleep (switch to L0 state) */
  72058. + int (*sleep) (void *_p);
  72059. +#endif
  72060. + /** Pointer passed to start() and stop() */
  72061. + void *p;
  72062. +} dwc_otg_cil_callbacks_t;
  72063. +
  72064. +extern void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * _core_if,
  72065. + dwc_otg_cil_callbacks_t * _cb,
  72066. + void *_p);
  72067. +extern void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * _core_if,
  72068. + dwc_otg_cil_callbacks_t * _cb,
  72069. + void *_p);
  72070. +
  72071. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if);
  72072. +
  72073. +//////////////////////////////////////////////////////////////////////
  72074. +/** Start the HCD. Helper function for using the HCD callbacks.
  72075. + *
  72076. + * @param core_if Programming view of DWC_otg controller.
  72077. + */
  72078. +static inline void cil_hcd_start(dwc_otg_core_if_t * core_if)
  72079. +{
  72080. + if (core_if->hcd_cb && core_if->hcd_cb->start) {
  72081. + core_if->hcd_cb->start(core_if->hcd_cb->p);
  72082. + }
  72083. +}
  72084. +
  72085. +/** Stop the HCD. Helper function for using the HCD callbacks.
  72086. + *
  72087. + * @param core_if Programming view of DWC_otg controller.
  72088. + */
  72089. +static inline void cil_hcd_stop(dwc_otg_core_if_t * core_if)
  72090. +{
  72091. + if (core_if->hcd_cb && core_if->hcd_cb->stop) {
  72092. + core_if->hcd_cb->stop(core_if->hcd_cb->p);
  72093. + }
  72094. +}
  72095. +
  72096. +/** Disconnect the HCD. Helper function for using the HCD callbacks.
  72097. + *
  72098. + * @param core_if Programming view of DWC_otg controller.
  72099. + */
  72100. +static inline void cil_hcd_disconnect(dwc_otg_core_if_t * core_if)
  72101. +{
  72102. + if (core_if->hcd_cb && core_if->hcd_cb->disconnect) {
  72103. + core_if->hcd_cb->disconnect(core_if->hcd_cb->p);
  72104. + }
  72105. +}
  72106. +
  72107. +/** Inform the HCD the a New Session has begun. Helper function for
  72108. + * using the HCD callbacks.
  72109. + *
  72110. + * @param core_if Programming view of DWC_otg controller.
  72111. + */
  72112. +static inline void cil_hcd_session_start(dwc_otg_core_if_t * core_if)
  72113. +{
  72114. + if (core_if->hcd_cb && core_if->hcd_cb->session_start) {
  72115. + core_if->hcd_cb->session_start(core_if->hcd_cb->p);
  72116. + }
  72117. +}
  72118. +
  72119. +#ifdef CONFIG_USB_DWC_OTG_LPM
  72120. +/**
  72121. + * Inform the HCD about LPM sleep.
  72122. + * Helper function for using the HCD callbacks.
  72123. + *
  72124. + * @param core_if Programming view of DWC_otg controller.
  72125. + */
  72126. +static inline void cil_hcd_sleep(dwc_otg_core_if_t * core_if)
  72127. +{
  72128. + if (core_if->hcd_cb && core_if->hcd_cb->sleep) {
  72129. + core_if->hcd_cb->sleep(core_if->hcd_cb->p);
  72130. + }
  72131. +}
  72132. +#endif
  72133. +
  72134. +/** Resume the HCD. Helper function for using the HCD callbacks.
  72135. + *
  72136. + * @param core_if Programming view of DWC_otg controller.
  72137. + */
  72138. +static inline void cil_hcd_resume(dwc_otg_core_if_t * core_if)
  72139. +{
  72140. + if (core_if->hcd_cb && core_if->hcd_cb->resume_wakeup) {
  72141. + core_if->hcd_cb->resume_wakeup(core_if->hcd_cb->p);
  72142. + }
  72143. +}
  72144. +
  72145. +/** Start the PCD. Helper function for using the PCD callbacks.
  72146. + *
  72147. + * @param core_if Programming view of DWC_otg controller.
  72148. + */
  72149. +static inline void cil_pcd_start(dwc_otg_core_if_t * core_if)
  72150. +{
  72151. + if (core_if->pcd_cb && core_if->pcd_cb->start) {
  72152. + core_if->pcd_cb->start(core_if->pcd_cb->p);
  72153. + }
  72154. +}
  72155. +
  72156. +/** Stop the PCD. Helper function for using the PCD callbacks.
  72157. + *
  72158. + * @param core_if Programming view of DWC_otg controller.
  72159. + */
  72160. +static inline void cil_pcd_stop(dwc_otg_core_if_t * core_if)
  72161. +{
  72162. + if (core_if->pcd_cb && core_if->pcd_cb->stop) {
  72163. + core_if->pcd_cb->stop(core_if->pcd_cb->p);
  72164. + }
  72165. +}
  72166. +
  72167. +/** Suspend the PCD. Helper function for using the PCD callbacks.
  72168. + *
  72169. + * @param core_if Programming view of DWC_otg controller.
  72170. + */
  72171. +static inline void cil_pcd_suspend(dwc_otg_core_if_t * core_if)
  72172. +{
  72173. + if (core_if->pcd_cb && core_if->pcd_cb->suspend) {
  72174. + core_if->pcd_cb->suspend(core_if->pcd_cb->p);
  72175. + }
  72176. +}
  72177. +
  72178. +/** Resume the PCD. Helper function for using the PCD callbacks.
  72179. + *
  72180. + * @param core_if Programming view of DWC_otg controller.
  72181. + */
  72182. +static inline void cil_pcd_resume(dwc_otg_core_if_t * core_if)
  72183. +{
  72184. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  72185. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  72186. + }
  72187. +}
  72188. +
  72189. +//////////////////////////////////////////////////////////////////////
  72190. +
  72191. +#endif
  72192. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
  72193. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c 1969-12-31 18:00:00.000000000 -0600
  72194. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c 2014-12-03 19:13:40.216418001 -0600
  72195. @@ -0,0 +1,1594 @@
  72196. +/* ==========================================================================
  72197. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil_intr.c $
  72198. + * $Revision: #32 $
  72199. + * $Date: 2012/08/10 $
  72200. + * $Change: 2047372 $
  72201. + *
  72202. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  72203. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  72204. + * otherwise expressly agreed to in writing between Synopsys and you.
  72205. + *
  72206. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  72207. + * any End User Software License Agreement or Agreement for Licensed Product
  72208. + * with Synopsys or any supplement thereto. You are permitted to use and
  72209. + * redistribute this Software in source and binary forms, with or without
  72210. + * modification, provided that redistributions of source code must retain this
  72211. + * notice. You may not view, use, disclose, copy or distribute this file or
  72212. + * any information contained herein except pursuant to this license grant from
  72213. + * Synopsys. If you do not agree with this notice, including the disclaimer
  72214. + * below, then you are not authorized to use the Software.
  72215. + *
  72216. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  72217. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  72218. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  72219. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  72220. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  72221. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  72222. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  72223. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  72224. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  72225. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  72226. + * DAMAGE.
  72227. + * ========================================================================== */
  72228. +
  72229. +/** @file
  72230. + *
  72231. + * The Core Interface Layer provides basic services for accessing and
  72232. + * managing the DWC_otg hardware. These services are used by both the
  72233. + * Host Controller Driver and the Peripheral Controller Driver.
  72234. + *
  72235. + * This file contains the Common Interrupt handlers.
  72236. + */
  72237. +#include "dwc_os.h"
  72238. +#include "dwc_otg_regs.h"
  72239. +#include "dwc_otg_cil.h"
  72240. +#include "dwc_otg_driver.h"
  72241. +#include "dwc_otg_pcd.h"
  72242. +#include "dwc_otg_hcd.h"
  72243. +
  72244. +#ifdef DEBUG
  72245. +inline const char *op_state_str(dwc_otg_core_if_t * core_if)
  72246. +{
  72247. + return (core_if->op_state == A_HOST ? "a_host" :
  72248. + (core_if->op_state == A_SUSPEND ? "a_suspend" :
  72249. + (core_if->op_state == A_PERIPHERAL ? "a_peripheral" :
  72250. + (core_if->op_state == B_PERIPHERAL ? "b_peripheral" :
  72251. + (core_if->op_state == B_HOST ? "b_host" : "unknown")))));
  72252. +}
  72253. +#endif
  72254. +
  72255. +/** This function will log a debug message
  72256. + *
  72257. + * @param core_if Programming view of DWC_otg controller.
  72258. + */
  72259. +int32_t dwc_otg_handle_mode_mismatch_intr(dwc_otg_core_if_t * core_if)
  72260. +{
  72261. + gintsts_data_t gintsts;
  72262. + DWC_WARN("Mode Mismatch Interrupt: currently in %s mode\n",
  72263. + dwc_otg_mode(core_if) ? "Host" : "Device");
  72264. +
  72265. + /* Clear interrupt */
  72266. + gintsts.d32 = 0;
  72267. + gintsts.b.modemismatch = 1;
  72268. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  72269. + return 1;
  72270. +}
  72271. +
  72272. +/**
  72273. + * This function handles the OTG Interrupts. It reads the OTG
  72274. + * Interrupt Register (GOTGINT) to determine what interrupt has
  72275. + * occurred.
  72276. + *
  72277. + * @param core_if Programming view of DWC_otg controller.
  72278. + */
  72279. +int32_t dwc_otg_handle_otg_intr(dwc_otg_core_if_t * core_if)
  72280. +{
  72281. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  72282. + gotgint_data_t gotgint;
  72283. + gotgctl_data_t gotgctl;
  72284. + gintmsk_data_t gintmsk;
  72285. + gpwrdn_data_t gpwrdn;
  72286. +
  72287. + gotgint.d32 = DWC_READ_REG32(&global_regs->gotgint);
  72288. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  72289. + DWC_DEBUGPL(DBG_CIL, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint.d32,
  72290. + op_state_str(core_if));
  72291. +
  72292. + if (gotgint.b.sesenddet) {
  72293. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  72294. + "Session End Detected++ (%s)\n",
  72295. + op_state_str(core_if));
  72296. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  72297. +
  72298. + if (core_if->op_state == B_HOST) {
  72299. + cil_pcd_start(core_if);
  72300. + core_if->op_state = B_PERIPHERAL;
  72301. + } else {
  72302. + /* If not B_HOST and Device HNP still set. HNP
  72303. + * Did not succeed!*/
  72304. + if (gotgctl.b.devhnpen) {
  72305. + DWC_DEBUGPL(DBG_ANY, "Session End Detected\n");
  72306. + __DWC_ERROR("Device Not Connected/Responding!\n");
  72307. + }
  72308. +
  72309. + /* If Session End Detected the B-Cable has
  72310. + * been disconnected. */
  72311. + /* Reset PCD and Gadget driver to a
  72312. + * clean state. */
  72313. + core_if->lx_state = DWC_OTG_L0;
  72314. + DWC_SPINUNLOCK(core_if->lock);
  72315. + cil_pcd_stop(core_if);
  72316. + DWC_SPINLOCK(core_if->lock);
  72317. +
  72318. + if (core_if->adp_enable) {
  72319. + if (core_if->power_down == 2) {
  72320. + gpwrdn.d32 = 0;
  72321. + gpwrdn.b.pwrdnswtch = 1;
  72322. + DWC_MODIFY_REG32(&core_if->
  72323. + core_global_regs->
  72324. + gpwrdn, gpwrdn.d32, 0);
  72325. + }
  72326. +
  72327. + gpwrdn.d32 = 0;
  72328. + gpwrdn.b.pmuintsel = 1;
  72329. + gpwrdn.b.pmuactv = 1;
  72330. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  72331. + gpwrdn, 0, gpwrdn.d32);
  72332. +
  72333. + dwc_otg_adp_sense_start(core_if);
  72334. + }
  72335. + }
  72336. +
  72337. + gotgctl.d32 = 0;
  72338. + gotgctl.b.devhnpen = 1;
  72339. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  72340. + }
  72341. + if (gotgint.b.sesreqsucstschng) {
  72342. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  72343. + "Session Reqeust Success Status Change++\n");
  72344. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  72345. + if (gotgctl.b.sesreqscs) {
  72346. +
  72347. + if ((core_if->core_params->phy_type ==
  72348. + DWC_PHY_TYPE_PARAM_FS) && (core_if->core_params->i2c_enable)) {
  72349. + core_if->srp_success = 1;
  72350. + } else {
  72351. + DWC_SPINUNLOCK(core_if->lock);
  72352. + cil_pcd_resume(core_if);
  72353. + DWC_SPINLOCK(core_if->lock);
  72354. + /* Clear Session Request */
  72355. + gotgctl.d32 = 0;
  72356. + gotgctl.b.sesreq = 1;
  72357. + DWC_MODIFY_REG32(&global_regs->gotgctl,
  72358. + gotgctl.d32, 0);
  72359. + }
  72360. + }
  72361. + }
  72362. + if (gotgint.b.hstnegsucstschng) {
  72363. + /* Print statements during the HNP interrupt handling
  72364. + * can cause it to fail.*/
  72365. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  72366. + /* WA for 3.00a- HW is not setting cur_mode, even sometimes
  72367. + * this does not help*/
  72368. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  72369. + dwc_udelay(100);
  72370. + if (gotgctl.b.hstnegscs) {
  72371. + if (dwc_otg_is_host_mode(core_if)) {
  72372. + core_if->op_state = B_HOST;
  72373. + /*
  72374. + * Need to disable SOF interrupt immediately.
  72375. + * When switching from device to host, the PCD
  72376. + * interrupt handler won't handle the
  72377. + * interrupt if host mode is already set. The
  72378. + * HCD interrupt handler won't get called if
  72379. + * the HCD state is HALT. This means that the
  72380. + * interrupt does not get handled and Linux
  72381. + * complains loudly.
  72382. + */
  72383. + gintmsk.d32 = 0;
  72384. + gintmsk.b.sofintr = 1;
  72385. + DWC_MODIFY_REG32(&global_regs->gintmsk,
  72386. + gintmsk.d32, 0);
  72387. + /* Call callback function with spin lock released */
  72388. + DWC_SPINUNLOCK(core_if->lock);
  72389. + cil_pcd_stop(core_if);
  72390. + /*
  72391. + * Initialize the Core for Host mode.
  72392. + */
  72393. + cil_hcd_start(core_if);
  72394. + DWC_SPINLOCK(core_if->lock);
  72395. + core_if->op_state = B_HOST;
  72396. + }
  72397. + } else {
  72398. + gotgctl.d32 = 0;
  72399. + gotgctl.b.hnpreq = 1;
  72400. + gotgctl.b.devhnpen = 1;
  72401. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  72402. + DWC_DEBUGPL(DBG_ANY, "HNP Failed\n");
  72403. + __DWC_ERROR("Device Not Connected/Responding\n");
  72404. + }
  72405. + }
  72406. + if (gotgint.b.hstnegdet) {
  72407. + /* The disconnect interrupt is set at the same time as
  72408. + * Host Negotiation Detected. During the mode
  72409. + * switch all interrupts are cleared so the disconnect
  72410. + * interrupt handler will not get executed.
  72411. + */
  72412. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  72413. + "Host Negotiation Detected++ (%s)\n",
  72414. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  72415. + "Device"));
  72416. + if (dwc_otg_is_device_mode(core_if)) {
  72417. + DWC_DEBUGPL(DBG_ANY, "a_suspend->a_peripheral (%d)\n",
  72418. + core_if->op_state);
  72419. + DWC_SPINUNLOCK(core_if->lock);
  72420. + cil_hcd_disconnect(core_if);
  72421. + cil_pcd_start(core_if);
  72422. + DWC_SPINLOCK(core_if->lock);
  72423. + core_if->op_state = A_PERIPHERAL;
  72424. + } else {
  72425. + /*
  72426. + * Need to disable SOF interrupt immediately. When
  72427. + * switching from device to host, the PCD interrupt
  72428. + * handler won't handle the interrupt if host mode is
  72429. + * already set. The HCD interrupt handler won't get
  72430. + * called if the HCD state is HALT. This means that
  72431. + * the interrupt does not get handled and Linux
  72432. + * complains loudly.
  72433. + */
  72434. + gintmsk.d32 = 0;
  72435. + gintmsk.b.sofintr = 1;
  72436. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmsk.d32, 0);
  72437. + DWC_SPINUNLOCK(core_if->lock);
  72438. + cil_pcd_stop(core_if);
  72439. + cil_hcd_start(core_if);
  72440. + DWC_SPINLOCK(core_if->lock);
  72441. + core_if->op_state = A_HOST;
  72442. + }
  72443. + }
  72444. + if (gotgint.b.adevtoutchng) {
  72445. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  72446. + "A-Device Timeout Change++\n");
  72447. + }
  72448. + if (gotgint.b.debdone) {
  72449. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: " "Debounce Done++\n");
  72450. + }
  72451. +
  72452. + /* Clear GOTGINT */
  72453. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, gotgint.d32);
  72454. +
  72455. + return 1;
  72456. +}
  72457. +
  72458. +void w_conn_id_status_change(void *p)
  72459. +{
  72460. + dwc_otg_core_if_t *core_if = p;
  72461. + uint32_t count = 0;
  72462. + gotgctl_data_t gotgctl = {.d32 = 0 };
  72463. +
  72464. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  72465. + DWC_DEBUGPL(DBG_CIL, "gotgctl=%0x\n", gotgctl.d32);
  72466. + DWC_DEBUGPL(DBG_CIL, "gotgctl.b.conidsts=%d\n", gotgctl.b.conidsts);
  72467. +
  72468. + /* B-Device connector (Device Mode) */
  72469. + if (gotgctl.b.conidsts) {
  72470. + /* Wait for switch to device mode. */
  72471. + while (!dwc_otg_is_device_mode(core_if)) {
  72472. + DWC_PRINTF("Waiting for Peripheral Mode, Mode=%s\n",
  72473. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  72474. + "Peripheral"));
  72475. + dwc_mdelay(100);
  72476. + if (++count > 10000)
  72477. + break;
  72478. + }
  72479. + DWC_ASSERT(++count < 10000,
  72480. + "Connection id status change timed out");
  72481. + core_if->op_state = B_PERIPHERAL;
  72482. + dwc_otg_core_init(core_if);
  72483. + dwc_otg_enable_global_interrupts(core_if);
  72484. + cil_pcd_start(core_if);
  72485. + } else {
  72486. + /* A-Device connector (Host Mode) */
  72487. + while (!dwc_otg_is_host_mode(core_if)) {
  72488. + DWC_PRINTF("Waiting for Host Mode, Mode=%s\n",
  72489. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  72490. + "Peripheral"));
  72491. + dwc_mdelay(100);
  72492. + if (++count > 10000)
  72493. + break;
  72494. + }
  72495. + DWC_ASSERT(++count < 10000,
  72496. + "Connection id status change timed out");
  72497. + core_if->op_state = A_HOST;
  72498. + /*
  72499. + * Initialize the Core for Host mode.
  72500. + */
  72501. + dwc_otg_core_init(core_if);
  72502. + dwc_otg_enable_global_interrupts(core_if);
  72503. + cil_hcd_start(core_if);
  72504. + }
  72505. +}
  72506. +
  72507. +/**
  72508. + * This function handles the Connector ID Status Change Interrupt. It
  72509. + * reads the OTG Interrupt Register (GOTCTL) to determine whether this
  72510. + * is a Device to Host Mode transition or a Host Mode to Device
  72511. + * Transition.
  72512. + *
  72513. + * This only occurs when the cable is connected/removed from the PHY
  72514. + * connector.
  72515. + *
  72516. + * @param core_if Programming view of DWC_otg controller.
  72517. + */
  72518. +int32_t dwc_otg_handle_conn_id_status_change_intr(dwc_otg_core_if_t * core_if)
  72519. +{
  72520. +
  72521. + /*
  72522. + * Need to disable SOF interrupt immediately. If switching from device
  72523. + * to host, the PCD interrupt handler won't handle the interrupt if
  72524. + * host mode is already set. The HCD interrupt handler won't get
  72525. + * called if the HCD state is HALT. This means that the interrupt does
  72526. + * not get handled and Linux complains loudly.
  72527. + */
  72528. + gintmsk_data_t gintmsk = {.d32 = 0 };
  72529. + gintsts_data_t gintsts = {.d32 = 0 };
  72530. +
  72531. + gintmsk.b.sofintr = 1;
  72532. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  72533. +
  72534. + DWC_DEBUGPL(DBG_CIL,
  72535. + " ++Connector ID Status Change Interrupt++ (%s)\n",
  72536. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"));
  72537. +
  72538. + DWC_SPINUNLOCK(core_if->lock);
  72539. +
  72540. + /*
  72541. + * Need to schedule a work, as there are possible DELAY function calls
  72542. + * Release lock before scheduling workq as it holds spinlock during scheduling
  72543. + */
  72544. +
  72545. + DWC_WORKQ_SCHEDULE(core_if->wq_otg, w_conn_id_status_change,
  72546. + core_if, "connection id status change");
  72547. + DWC_SPINLOCK(core_if->lock);
  72548. +
  72549. + /* Set flag and clear interrupt */
  72550. + gintsts.b.conidstschng = 1;
  72551. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  72552. +
  72553. + return 1;
  72554. +}
  72555. +
  72556. +/**
  72557. + * This interrupt indicates that a device is initiating the Session
  72558. + * Request Protocol to request the host to turn on bus power so a new
  72559. + * session can begin. The handler responds by turning on bus power. If
  72560. + * the DWC_otg controller is in low power mode, the handler brings the
  72561. + * controller out of low power mode before turning on bus power.
  72562. + *
  72563. + * @param core_if Programming view of DWC_otg controller.
  72564. + */
  72565. +int32_t dwc_otg_handle_session_req_intr(dwc_otg_core_if_t * core_if)
  72566. +{
  72567. + gintsts_data_t gintsts;
  72568. +
  72569. +#ifndef DWC_HOST_ONLY
  72570. + DWC_DEBUGPL(DBG_ANY, "++Session Request Interrupt++\n");
  72571. +
  72572. + if (dwc_otg_is_device_mode(core_if)) {
  72573. + DWC_PRINTF("SRP: Device mode\n");
  72574. + } else {
  72575. + hprt0_data_t hprt0;
  72576. + DWC_PRINTF("SRP: Host mode\n");
  72577. +
  72578. + /* Turn on the port power bit. */
  72579. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  72580. + hprt0.b.prtpwr = 1;
  72581. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  72582. +
  72583. + /* Start the Connection timer. So a message can be displayed
  72584. + * if connect does not occur within 10 seconds. */
  72585. + cil_hcd_session_start(core_if);
  72586. + }
  72587. +#endif
  72588. +
  72589. + /* Clear interrupt */
  72590. + gintsts.d32 = 0;
  72591. + gintsts.b.sessreqintr = 1;
  72592. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  72593. +
  72594. + return 1;
  72595. +}
  72596. +
  72597. +void w_wakeup_detected(void *p)
  72598. +{
  72599. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) p;
  72600. + /*
  72601. + * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
  72602. + * so that OPT tests pass with all PHYs).
  72603. + */
  72604. + hprt0_data_t hprt0 = {.d32 = 0 };
  72605. +#if 0
  72606. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  72607. + /* Restart the Phy Clock */
  72608. + pcgcctl.b.stoppclk = 1;
  72609. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  72610. + dwc_udelay(10);
  72611. +#endif //0
  72612. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  72613. + DWC_DEBUGPL(DBG_ANY, "Resume: HPRT0=%0x\n", hprt0.d32);
  72614. +// dwc_mdelay(70);
  72615. + hprt0.b.prtres = 0; /* Resume */
  72616. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  72617. + DWC_DEBUGPL(DBG_ANY, "Clear Resume: HPRT0=%0x\n",
  72618. + DWC_READ_REG32(core_if->host_if->hprt0));
  72619. +
  72620. + cil_hcd_resume(core_if);
  72621. +
  72622. + /** Change to L0 state*/
  72623. + core_if->lx_state = DWC_OTG_L0;
  72624. +}
  72625. +
  72626. +/**
  72627. + * This interrupt indicates that the DWC_otg controller has detected a
  72628. + * resume or remote wakeup sequence. If the DWC_otg controller is in
  72629. + * low power mode, the handler must brings the controller out of low
  72630. + * power mode. The controller automatically begins resume
  72631. + * signaling. The handler schedules a time to stop resume signaling.
  72632. + */
  72633. +int32_t dwc_otg_handle_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  72634. +{
  72635. + gintsts_data_t gintsts;
  72636. +
  72637. + DWC_DEBUGPL(DBG_ANY,
  72638. + "++Resume and Remote Wakeup Detected Interrupt++\n");
  72639. +
  72640. + DWC_PRINTF("%s lxstate = %d\n", __func__, core_if->lx_state);
  72641. +
  72642. + if (dwc_otg_is_device_mode(core_if)) {
  72643. + dctl_data_t dctl = {.d32 = 0 };
  72644. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n",
  72645. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  72646. + dsts));
  72647. + if (core_if->lx_state == DWC_OTG_L2) {
  72648. +#ifdef PARTIAL_POWER_DOWN
  72649. + if (core_if->hwcfg4.b.power_optimiz) {
  72650. + pcgcctl_data_t power = {.d32 = 0 };
  72651. +
  72652. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  72653. + DWC_DEBUGPL(DBG_CIL, "PCGCCTL=%0x\n",
  72654. + power.d32);
  72655. +
  72656. + power.b.stoppclk = 0;
  72657. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  72658. +
  72659. + power.b.pwrclmp = 0;
  72660. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  72661. +
  72662. + power.b.rstpdwnmodule = 0;
  72663. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  72664. + }
  72665. +#endif
  72666. + /* Clear the Remote Wakeup Signaling */
  72667. + dctl.b.rmtwkupsig = 1;
  72668. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  72669. + dctl, dctl.d32, 0);
  72670. +
  72671. + DWC_SPINUNLOCK(core_if->lock);
  72672. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  72673. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  72674. + }
  72675. + DWC_SPINLOCK(core_if->lock);
  72676. + } else {
  72677. + glpmcfg_data_t lpmcfg;
  72678. + lpmcfg.d32 =
  72679. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  72680. + lpmcfg.b.hird_thres &= (~(1 << 4));
  72681. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  72682. + lpmcfg.d32);
  72683. + }
  72684. + /** Change to L0 state*/
  72685. + core_if->lx_state = DWC_OTG_L0;
  72686. + } else {
  72687. + if (core_if->lx_state != DWC_OTG_L1) {
  72688. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  72689. +
  72690. + /* Restart the Phy Clock */
  72691. + pcgcctl.b.stoppclk = 1;
  72692. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  72693. + DWC_TIMER_SCHEDULE(core_if->wkp_timer, 71);
  72694. + } else {
  72695. + /** Change to L0 state*/
  72696. + core_if->lx_state = DWC_OTG_L0;
  72697. + }
  72698. + }
  72699. +
  72700. + /* Clear interrupt */
  72701. + gintsts.d32 = 0;
  72702. + gintsts.b.wkupintr = 1;
  72703. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  72704. +
  72705. + return 1;
  72706. +}
  72707. +
  72708. +/**
  72709. + * This interrupt indicates that the Wakeup Logic has detected a
  72710. + * Device disconnect.
  72711. + */
  72712. +static int32_t dwc_otg_handle_pwrdn_disconnect_intr(dwc_otg_core_if_t *core_if)
  72713. +{
  72714. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  72715. + gpwrdn_data_t gpwrdn_temp = { .d32 = 0 };
  72716. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  72717. +
  72718. + DWC_PRINTF("%s called\n", __FUNCTION__);
  72719. +
  72720. + if (!core_if->hibernation_suspend) {
  72721. + DWC_PRINTF("Already exited from Hibernation\n");
  72722. + return 1;
  72723. + }
  72724. +
  72725. + /* Switch on the voltage to the core */
  72726. + gpwrdn.b.pwrdnswtch = 1;
  72727. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  72728. + dwc_udelay(10);
  72729. +
  72730. + /* Reset the core */
  72731. + gpwrdn.d32 = 0;
  72732. + gpwrdn.b.pwrdnrstn = 1;
  72733. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  72734. + dwc_udelay(10);
  72735. +
  72736. + /* Disable power clamps*/
  72737. + gpwrdn.d32 = 0;
  72738. + gpwrdn.b.pwrdnclmp = 1;
  72739. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  72740. +
  72741. + /* Remove reset the core signal */
  72742. + gpwrdn.d32 = 0;
  72743. + gpwrdn.b.pwrdnrstn = 1;
  72744. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  72745. + dwc_udelay(10);
  72746. +
  72747. + /* Disable PMU interrupt */
  72748. + gpwrdn.d32 = 0;
  72749. + gpwrdn.b.pmuintsel = 1;
  72750. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  72751. +
  72752. + core_if->hibernation_suspend = 0;
  72753. +
  72754. + /* Disable PMU */
  72755. + gpwrdn.d32 = 0;
  72756. + gpwrdn.b.pmuactv = 1;
  72757. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  72758. + dwc_udelay(10);
  72759. +
  72760. + if (gpwrdn_temp.b.idsts) {
  72761. + core_if->op_state = B_PERIPHERAL;
  72762. + dwc_otg_core_init(core_if);
  72763. + dwc_otg_enable_global_interrupts(core_if);
  72764. + cil_pcd_start(core_if);
  72765. + } else {
  72766. + core_if->op_state = A_HOST;
  72767. + dwc_otg_core_init(core_if);
  72768. + dwc_otg_enable_global_interrupts(core_if);
  72769. + cil_hcd_start(core_if);
  72770. + }
  72771. +
  72772. + return 1;
  72773. +}
  72774. +
  72775. +/**
  72776. + * This interrupt indicates that the Wakeup Logic has detected a
  72777. + * remote wakeup sequence.
  72778. + */
  72779. +static int32_t dwc_otg_handle_pwrdn_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  72780. +{
  72781. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  72782. + DWC_DEBUGPL(DBG_ANY,
  72783. + "++Powerdown Remote Wakeup Detected Interrupt++\n");
  72784. +
  72785. + if (!core_if->hibernation_suspend) {
  72786. + DWC_PRINTF("Already exited from Hibernation\n");
  72787. + return 1;
  72788. + }
  72789. +
  72790. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  72791. + if (gpwrdn.b.idsts) { // Device Mode
  72792. + if ((core_if->power_down == 2)
  72793. + && (core_if->hibernation_suspend == 1)) {
  72794. + dwc_otg_device_hibernation_restore(core_if, 0, 0);
  72795. + }
  72796. + } else {
  72797. + if ((core_if->power_down == 2)
  72798. + && (core_if->hibernation_suspend == 1)) {
  72799. + dwc_otg_host_hibernation_restore(core_if, 1, 0);
  72800. + }
  72801. + }
  72802. + return 1;
  72803. +}
  72804. +
  72805. +static int32_t dwc_otg_handle_pwrdn_idsts_change(dwc_otg_device_t *otg_dev)
  72806. +{
  72807. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  72808. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  72809. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  72810. +
  72811. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  72812. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  72813. + if (core_if->power_down == 2) {
  72814. + if (!core_if->hibernation_suspend) {
  72815. + DWC_PRINTF("Already exited from Hibernation\n");
  72816. + return 1;
  72817. + }
  72818. + DWC_DEBUGPL(DBG_ANY, "Exit from hibernation on ID sts change\n");
  72819. + /* Switch on the voltage to the core */
  72820. + gpwrdn.b.pwrdnswtch = 1;
  72821. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  72822. + dwc_udelay(10);
  72823. +
  72824. + /* Reset the core */
  72825. + gpwrdn.d32 = 0;
  72826. + gpwrdn.b.pwrdnrstn = 1;
  72827. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  72828. + dwc_udelay(10);
  72829. +
  72830. + /* Disable power clamps */
  72831. + gpwrdn.d32 = 0;
  72832. + gpwrdn.b.pwrdnclmp = 1;
  72833. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  72834. +
  72835. + /* Remove reset the core signal */
  72836. + gpwrdn.d32 = 0;
  72837. + gpwrdn.b.pwrdnrstn = 1;
  72838. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  72839. + dwc_udelay(10);
  72840. +
  72841. + /* Disable PMU interrupt */
  72842. + gpwrdn.d32 = 0;
  72843. + gpwrdn.b.pmuintsel = 1;
  72844. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  72845. +
  72846. + /*Indicates that we are exiting from hibernation */
  72847. + core_if->hibernation_suspend = 0;
  72848. +
  72849. + /* Disable PMU */
  72850. + gpwrdn.d32 = 0;
  72851. + gpwrdn.b.pmuactv = 1;
  72852. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  72853. + dwc_udelay(10);
  72854. +
  72855. + gpwrdn.d32 = core_if->gr_backup->gpwrdn_local;
  72856. + if (gpwrdn.b.dis_vbus == 1) {
  72857. + gpwrdn.d32 = 0;
  72858. + gpwrdn.b.dis_vbus = 1;
  72859. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  72860. + }
  72861. +
  72862. + if (gpwrdn_temp.b.idsts) {
  72863. + core_if->op_state = B_PERIPHERAL;
  72864. + dwc_otg_core_init(core_if);
  72865. + dwc_otg_enable_global_interrupts(core_if);
  72866. + cil_pcd_start(core_if);
  72867. + } else {
  72868. + core_if->op_state = A_HOST;
  72869. + dwc_otg_core_init(core_if);
  72870. + dwc_otg_enable_global_interrupts(core_if);
  72871. + cil_hcd_start(core_if);
  72872. + }
  72873. + }
  72874. +
  72875. + if (core_if->adp_enable) {
  72876. + uint8_t is_host = 0;
  72877. + DWC_SPINUNLOCK(core_if->lock);
  72878. + /* Change the core_if's lock to hcd/pcd lock depend on mode? */
  72879. +#ifndef DWC_HOST_ONLY
  72880. + if (gpwrdn_temp.b.idsts)
  72881. + core_if->lock = otg_dev->pcd->lock;
  72882. +#endif
  72883. +#ifndef DWC_DEVICE_ONLY
  72884. + if (!gpwrdn_temp.b.idsts) {
  72885. + core_if->lock = otg_dev->hcd->lock;
  72886. + is_host = 1;
  72887. + }
  72888. +#endif
  72889. + DWC_PRINTF("RESTART ADP\n");
  72890. + if (core_if->adp.probe_enabled)
  72891. + dwc_otg_adp_probe_stop(core_if);
  72892. + if (core_if->adp.sense_enabled)
  72893. + dwc_otg_adp_sense_stop(core_if);
  72894. + if (core_if->adp.sense_timer_started)
  72895. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  72896. + if (core_if->adp.vbuson_timer_started)
  72897. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  72898. + core_if->adp.probe_timer_values[0] = -1;
  72899. + core_if->adp.probe_timer_values[1] = -1;
  72900. + core_if->adp.sense_timer_started = 0;
  72901. + core_if->adp.vbuson_timer_started = 0;
  72902. + core_if->adp.probe_counter = 0;
  72903. + core_if->adp.gpwrdn = 0;
  72904. +
  72905. + /* Disable PMU and restart ADP */
  72906. + gpwrdn_temp.d32 = 0;
  72907. + gpwrdn_temp.b.pmuactv = 1;
  72908. + gpwrdn_temp.b.pmuintsel = 1;
  72909. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  72910. + DWC_PRINTF("Check point 1\n");
  72911. + dwc_mdelay(110);
  72912. + dwc_otg_adp_start(core_if, is_host);
  72913. + DWC_SPINLOCK(core_if->lock);
  72914. + }
  72915. +
  72916. +
  72917. + return 1;
  72918. +}
  72919. +
  72920. +static int32_t dwc_otg_handle_pwrdn_session_change(dwc_otg_core_if_t * core_if)
  72921. +{
  72922. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  72923. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  72924. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  72925. +
  72926. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  72927. + if (core_if->power_down == 2) {
  72928. + if (!core_if->hibernation_suspend) {
  72929. + DWC_PRINTF("Already exited from Hibernation\n");
  72930. + return 1;
  72931. + }
  72932. +
  72933. + if ((otg_cap_param != DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  72934. + otg_cap_param != DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) &&
  72935. + gpwrdn.b.bsessvld == 0) {
  72936. + /* Save gpwrdn register for further usage if stschng interrupt */
  72937. + core_if->gr_backup->gpwrdn_local =
  72938. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  72939. + /*Exit from ISR and wait for stschng interrupt with bsessvld = 1 */
  72940. + return 1;
  72941. + }
  72942. +
  72943. + /* Switch on the voltage to the core */
  72944. + gpwrdn.d32 = 0;
  72945. + gpwrdn.b.pwrdnswtch = 1;
  72946. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  72947. + dwc_udelay(10);
  72948. +
  72949. + /* Reset the core */
  72950. + gpwrdn.d32 = 0;
  72951. + gpwrdn.b.pwrdnrstn = 1;
  72952. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  72953. + dwc_udelay(10);
  72954. +
  72955. + /* Disable power clamps */
  72956. + gpwrdn.d32 = 0;
  72957. + gpwrdn.b.pwrdnclmp = 1;
  72958. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  72959. +
  72960. + /* Remove reset the core signal */
  72961. + gpwrdn.d32 = 0;
  72962. + gpwrdn.b.pwrdnrstn = 1;
  72963. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  72964. + dwc_udelay(10);
  72965. +
  72966. + /* Disable PMU interrupt */
  72967. + gpwrdn.d32 = 0;
  72968. + gpwrdn.b.pmuintsel = 1;
  72969. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  72970. + dwc_udelay(10);
  72971. +
  72972. + /*Indicates that we are exiting from hibernation */
  72973. + core_if->hibernation_suspend = 0;
  72974. +
  72975. + /* Disable PMU */
  72976. + gpwrdn.d32 = 0;
  72977. + gpwrdn.b.pmuactv = 1;
  72978. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  72979. + dwc_udelay(10);
  72980. +
  72981. + core_if->op_state = B_PERIPHERAL;
  72982. + dwc_otg_core_init(core_if);
  72983. + dwc_otg_enable_global_interrupts(core_if);
  72984. + cil_pcd_start(core_if);
  72985. +
  72986. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  72987. + otg_cap_param == DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) {
  72988. + /*
  72989. + * Initiate SRP after initial ADP probe.
  72990. + */
  72991. + dwc_otg_initiate_srp(core_if);
  72992. + }
  72993. + }
  72994. +
  72995. + return 1;
  72996. +}
  72997. +/**
  72998. + * This interrupt indicates that the Wakeup Logic has detected a
  72999. + * status change either on IDDIG or BSessVld.
  73000. + */
  73001. +static uint32_t dwc_otg_handle_pwrdn_stschng_intr(dwc_otg_device_t *otg_dev)
  73002. +{
  73003. + int retval;
  73004. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  73005. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  73006. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  73007. +
  73008. + DWC_PRINTF("%s called\n", __FUNCTION__);
  73009. +
  73010. + if (core_if->power_down == 2) {
  73011. + if (core_if->hibernation_suspend <= 0) {
  73012. + DWC_PRINTF("Already exited from Hibernation\n");
  73013. + return 1;
  73014. + } else
  73015. + gpwrdn_temp.d32 = core_if->gr_backup->gpwrdn_local;
  73016. +
  73017. + } else {
  73018. + gpwrdn_temp.d32 = core_if->adp.gpwrdn;
  73019. + }
  73020. +
  73021. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  73022. +
  73023. + if (gpwrdn.b.idsts ^ gpwrdn_temp.b.idsts) {
  73024. + retval = dwc_otg_handle_pwrdn_idsts_change(otg_dev);
  73025. + } else if (gpwrdn.b.bsessvld ^ gpwrdn_temp.b.bsessvld) {
  73026. + retval = dwc_otg_handle_pwrdn_session_change(core_if);
  73027. + }
  73028. +
  73029. + return retval;
  73030. +}
  73031. +
  73032. +/**
  73033. + * This interrupt indicates that the Wakeup Logic has detected a
  73034. + * SRP.
  73035. + */
  73036. +static int32_t dwc_otg_handle_pwrdn_srp_intr(dwc_otg_core_if_t * core_if)
  73037. +{
  73038. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  73039. +
  73040. + DWC_PRINTF("%s called\n", __FUNCTION__);
  73041. +
  73042. + if (!core_if->hibernation_suspend) {
  73043. + DWC_PRINTF("Already exited from Hibernation\n");
  73044. + return 1;
  73045. + }
  73046. +#ifdef DWC_DEV_SRPCAP
  73047. + if (core_if->pwron_timer_started) {
  73048. + core_if->pwron_timer_started = 0;
  73049. + DWC_TIMER_CANCEL(core_if->pwron_timer);
  73050. + }
  73051. +#endif
  73052. +
  73053. + /* Switch on the voltage to the core */
  73054. + gpwrdn.b.pwrdnswtch = 1;
  73055. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  73056. + dwc_udelay(10);
  73057. +
  73058. + /* Reset the core */
  73059. + gpwrdn.d32 = 0;
  73060. + gpwrdn.b.pwrdnrstn = 1;
  73061. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  73062. + dwc_udelay(10);
  73063. +
  73064. + /* Disable power clamps */
  73065. + gpwrdn.d32 = 0;
  73066. + gpwrdn.b.pwrdnclmp = 1;
  73067. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  73068. +
  73069. + /* Remove reset the core signal */
  73070. + gpwrdn.d32 = 0;
  73071. + gpwrdn.b.pwrdnrstn = 1;
  73072. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  73073. + dwc_udelay(10);
  73074. +
  73075. + /* Disable PMU interrupt */
  73076. + gpwrdn.d32 = 0;
  73077. + gpwrdn.b.pmuintsel = 1;
  73078. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  73079. +
  73080. + /* Indicates that we are exiting from hibernation */
  73081. + core_if->hibernation_suspend = 0;
  73082. +
  73083. + /* Disable PMU */
  73084. + gpwrdn.d32 = 0;
  73085. + gpwrdn.b.pmuactv = 1;
  73086. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  73087. + dwc_udelay(10);
  73088. +
  73089. + /* Programm Disable VBUS to 0 */
  73090. + gpwrdn.d32 = 0;
  73091. + gpwrdn.b.dis_vbus = 1;
  73092. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  73093. +
  73094. + /*Initialize the core as Host */
  73095. + core_if->op_state = A_HOST;
  73096. + dwc_otg_core_init(core_if);
  73097. + dwc_otg_enable_global_interrupts(core_if);
  73098. + cil_hcd_start(core_if);
  73099. +
  73100. + return 1;
  73101. +}
  73102. +
  73103. +/** This interrupt indicates that restore command after Hibernation
  73104. + * was completed by the core. */
  73105. +int32_t dwc_otg_handle_restore_done_intr(dwc_otg_core_if_t * core_if)
  73106. +{
  73107. + pcgcctl_data_t pcgcctl;
  73108. + DWC_DEBUGPL(DBG_ANY, "++Restore Done Interrupt++\n");
  73109. +
  73110. + //TODO De-assert restore signal. 8.a
  73111. + pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
  73112. + if (pcgcctl.b.restoremode == 1) {
  73113. + gintmsk_data_t gintmsk = {.d32 = 0 };
  73114. + /*
  73115. + * If restore mode is Remote Wakeup,
  73116. + * unmask Remote Wakeup interrupt.
  73117. + */
  73118. + gintmsk.b.wkupintr = 1;
  73119. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  73120. + 0, gintmsk.d32);
  73121. + }
  73122. +
  73123. + return 1;
  73124. +}
  73125. +
  73126. +/**
  73127. + * This interrupt indicates that a device has been disconnected from
  73128. + * the root port.
  73129. + */
  73130. +int32_t dwc_otg_handle_disconnect_intr(dwc_otg_core_if_t * core_if)
  73131. +{
  73132. + gintsts_data_t gintsts;
  73133. +
  73134. + DWC_DEBUGPL(DBG_ANY, "++Disconnect Detected Interrupt++ (%s) %s\n",
  73135. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"),
  73136. + op_state_str(core_if));
  73137. +
  73138. +/** @todo Consolidate this if statement. */
  73139. +#ifndef DWC_HOST_ONLY
  73140. + if (core_if->op_state == B_HOST) {
  73141. + /* If in device mode Disconnect and stop the HCD, then
  73142. + * start the PCD. */
  73143. + DWC_SPINUNLOCK(core_if->lock);
  73144. + cil_hcd_disconnect(core_if);
  73145. + cil_pcd_start(core_if);
  73146. + DWC_SPINLOCK(core_if->lock);
  73147. + core_if->op_state = B_PERIPHERAL;
  73148. + } else if (dwc_otg_is_device_mode(core_if)) {
  73149. + gotgctl_data_t gotgctl = {.d32 = 0 };
  73150. + gotgctl.d32 =
  73151. + DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  73152. + if (gotgctl.b.hstsethnpen == 1) {
  73153. + /* Do nothing, if HNP in process the OTG
  73154. + * interrupt "Host Negotiation Detected"
  73155. + * interrupt will do the mode switch.
  73156. + */
  73157. + } else if (gotgctl.b.devhnpen == 0) {
  73158. + /* If in device mode Disconnect and stop the HCD, then
  73159. + * start the PCD. */
  73160. + DWC_SPINUNLOCK(core_if->lock);
  73161. + cil_hcd_disconnect(core_if);
  73162. + cil_pcd_start(core_if);
  73163. + DWC_SPINLOCK(core_if->lock);
  73164. + core_if->op_state = B_PERIPHERAL;
  73165. + } else {
  73166. + DWC_DEBUGPL(DBG_ANY, "!a_peripheral && !devhnpen\n");
  73167. + }
  73168. + } else {
  73169. + if (core_if->op_state == A_HOST) {
  73170. + /* A-Cable still connected but device disconnected. */
  73171. + cil_hcd_disconnect(core_if);
  73172. + if (core_if->adp_enable) {
  73173. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  73174. + cil_hcd_stop(core_if);
  73175. + /* Enable Power Down Logic */
  73176. + gpwrdn.b.pmuintsel = 1;
  73177. + gpwrdn.b.pmuactv = 1;
  73178. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  73179. + gpwrdn, 0, gpwrdn.d32);
  73180. + dwc_otg_adp_probe_start(core_if);
  73181. +
  73182. + /* Power off the core */
  73183. + if (core_if->power_down == 2) {
  73184. + gpwrdn.d32 = 0;
  73185. + gpwrdn.b.pwrdnswtch = 1;
  73186. + DWC_MODIFY_REG32
  73187. + (&core_if->core_global_regs->gpwrdn,
  73188. + gpwrdn.d32, 0);
  73189. + }
  73190. + }
  73191. + }
  73192. + }
  73193. +#endif
  73194. + /* Change to L3(OFF) state */
  73195. + core_if->lx_state = DWC_OTG_L3;
  73196. +
  73197. + gintsts.d32 = 0;
  73198. + gintsts.b.disconnect = 1;
  73199. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  73200. + return 1;
  73201. +}
  73202. +
  73203. +/**
  73204. + * This interrupt indicates that SUSPEND state has been detected on
  73205. + * the USB.
  73206. + *
  73207. + * For HNP the USB Suspend interrupt signals the change from
  73208. + * "a_peripheral" to "a_host".
  73209. + *
  73210. + * When power management is enabled the core will be put in low power
  73211. + * mode.
  73212. + */
  73213. +int32_t dwc_otg_handle_usb_suspend_intr(dwc_otg_core_if_t * core_if)
  73214. +{
  73215. + dsts_data_t dsts;
  73216. + gintsts_data_t gintsts;
  73217. + dcfg_data_t dcfg;
  73218. +
  73219. + DWC_DEBUGPL(DBG_ANY, "USB SUSPEND\n");
  73220. +
  73221. + if (dwc_otg_is_device_mode(core_if)) {
  73222. + /* Check the Device status register to determine if the Suspend
  73223. + * state is active. */
  73224. + dsts.d32 =
  73225. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  73226. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n", dsts.d32);
  73227. + DWC_DEBUGPL(DBG_PCD, "DSTS.Suspend Status=%d "
  73228. + "HWCFG4.power Optimize=%d\n",
  73229. + dsts.b.suspsts, core_if->hwcfg4.b.power_optimiz);
  73230. +
  73231. +#ifdef PARTIAL_POWER_DOWN
  73232. +/** @todo Add a module parameter for power management. */
  73233. +
  73234. + if (dsts.b.suspsts && core_if->hwcfg4.b.power_optimiz) {
  73235. + pcgcctl_data_t power = {.d32 = 0 };
  73236. + DWC_DEBUGPL(DBG_CIL, "suspend\n");
  73237. +
  73238. + power.b.pwrclmp = 1;
  73239. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  73240. +
  73241. + power.b.rstpdwnmodule = 1;
  73242. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  73243. +
  73244. + power.b.stoppclk = 1;
  73245. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  73246. +
  73247. + } else {
  73248. + DWC_DEBUGPL(DBG_ANY, "disconnect?\n");
  73249. + }
  73250. +#endif
  73251. + /* PCD callback for suspend. Release the lock inside of callback function */
  73252. + cil_pcd_suspend(core_if);
  73253. + if (core_if->power_down == 2)
  73254. + {
  73255. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  73256. + DWC_DEBUGPL(DBG_ANY,"lx_state = %08x\n",core_if->lx_state);
  73257. + DWC_DEBUGPL(DBG_ANY," device address = %08d\n",dcfg.b.devaddr);
  73258. +
  73259. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  73260. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  73261. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  73262. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  73263. +
  73264. + /* Change to L2(suspend) state */
  73265. + core_if->lx_state = DWC_OTG_L2;
  73266. +
  73267. + /* Clear interrupt in gintsts */
  73268. + gintsts.d32 = 0;
  73269. + gintsts.b.usbsuspend = 1;
  73270. + DWC_WRITE_REG32(&core_if->core_global_regs->
  73271. + gintsts, gintsts.d32);
  73272. + DWC_PRINTF("Start of hibernation completed\n");
  73273. + dwc_otg_save_global_regs(core_if);
  73274. + dwc_otg_save_dev_regs(core_if);
  73275. +
  73276. + gusbcfg.d32 =
  73277. + DWC_READ_REG32(&core_if->core_global_regs->
  73278. + gusbcfg);
  73279. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  73280. + /* ULPI interface */
  73281. + /* Suspend the Phy Clock */
  73282. + pcgcctl.d32 = 0;
  73283. + pcgcctl.b.stoppclk = 1;
  73284. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  73285. + pcgcctl.d32);
  73286. + dwc_udelay(10);
  73287. + gpwrdn.b.pmuactv = 1;
  73288. + DWC_MODIFY_REG32(&core_if->
  73289. + core_global_regs->
  73290. + gpwrdn, 0, gpwrdn.d32);
  73291. + } else {
  73292. + /* UTMI+ Interface */
  73293. + gpwrdn.b.pmuactv = 1;
  73294. + DWC_MODIFY_REG32(&core_if->
  73295. + core_global_regs->
  73296. + gpwrdn, 0, gpwrdn.d32);
  73297. + dwc_udelay(10);
  73298. + pcgcctl.b.stoppclk = 1;
  73299. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  73300. + pcgcctl.d32);
  73301. + dwc_udelay(10);
  73302. + }
  73303. +
  73304. + /* Set flag to indicate that we are in hibernation */
  73305. + core_if->hibernation_suspend = 1;
  73306. + /* Enable interrupts from wake up logic */
  73307. + gpwrdn.d32 = 0;
  73308. + gpwrdn.b.pmuintsel = 1;
  73309. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  73310. + gpwrdn, 0, gpwrdn.d32);
  73311. + dwc_udelay(10);
  73312. +
  73313. + /* Unmask device mode interrupts in GPWRDN */
  73314. + gpwrdn.d32 = 0;
  73315. + gpwrdn.b.rst_det_msk = 1;
  73316. + gpwrdn.b.lnstchng_msk = 1;
  73317. + gpwrdn.b.sts_chngint_msk = 1;
  73318. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  73319. + gpwrdn, 0, gpwrdn.d32);
  73320. + dwc_udelay(10);
  73321. +
  73322. + /* Enable Power Down Clamp */
  73323. + gpwrdn.d32 = 0;
  73324. + gpwrdn.b.pwrdnclmp = 1;
  73325. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  73326. + gpwrdn, 0, gpwrdn.d32);
  73327. + dwc_udelay(10);
  73328. +
  73329. + /* Switch off VDD */
  73330. + gpwrdn.d32 = 0;
  73331. + gpwrdn.b.pwrdnswtch = 1;
  73332. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  73333. + gpwrdn, 0, gpwrdn.d32);
  73334. +
  73335. + /* Save gpwrdn register for further usage if stschng interrupt */
  73336. + core_if->gr_backup->gpwrdn_local =
  73337. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  73338. + DWC_PRINTF("Hibernation completed\n");
  73339. +
  73340. + return 1;
  73341. + }
  73342. + } else if (core_if->power_down == 3) {
  73343. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  73344. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  73345. + DWC_DEBUGPL(DBG_ANY, "lx_state = %08x\n",core_if->lx_state);
  73346. + DWC_DEBUGPL(DBG_ANY, " device address = %08d\n",dcfg.b.devaddr);
  73347. +
  73348. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  73349. + DWC_DEBUGPL(DBG_ANY, "Start entering to extended hibernation\n");
  73350. + core_if->xhib = 1;
  73351. +
  73352. + /* Clear interrupt in gintsts */
  73353. + gintsts.d32 = 0;
  73354. + gintsts.b.usbsuspend = 1;
  73355. + DWC_WRITE_REG32(&core_if->core_global_regs->
  73356. + gintsts, gintsts.d32);
  73357. +
  73358. + dwc_otg_save_global_regs(core_if);
  73359. + dwc_otg_save_dev_regs(core_if);
  73360. +
  73361. + /* Wait for 10 PHY clocks */
  73362. + dwc_udelay(10);
  73363. +
  73364. + /* Program GPIO register while entering to xHib */
  73365. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x1);
  73366. +
  73367. + pcgcctl.b.enbl_extnd_hiber = 1;
  73368. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  73369. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  73370. +
  73371. + pcgcctl.d32 = 0;
  73372. + pcgcctl.b.extnd_hiber_pwrclmp = 1;
  73373. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  73374. +
  73375. + pcgcctl.d32 = 0;
  73376. + pcgcctl.b.extnd_hiber_switch = 1;
  73377. + core_if->gr_backup->xhib_gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  73378. + core_if->gr_backup->xhib_pcgcctl = DWC_READ_REG32(core_if->pcgcctl) | pcgcctl.d32;
  73379. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  73380. +
  73381. + DWC_DEBUGPL(DBG_ANY, "Finished entering to extended hibernation\n");
  73382. +
  73383. + return 1;
  73384. + }
  73385. + }
  73386. + } else {
  73387. + if (core_if->op_state == A_PERIPHERAL) {
  73388. + DWC_DEBUGPL(DBG_ANY, "a_peripheral->a_host\n");
  73389. + /* Clear the a_peripheral flag, back to a_host. */
  73390. + DWC_SPINUNLOCK(core_if->lock);
  73391. + cil_pcd_stop(core_if);
  73392. + cil_hcd_start(core_if);
  73393. + DWC_SPINLOCK(core_if->lock);
  73394. + core_if->op_state = A_HOST;
  73395. + }
  73396. + }
  73397. +
  73398. + /* Change to L2(suspend) state */
  73399. + core_if->lx_state = DWC_OTG_L2;
  73400. +
  73401. + /* Clear interrupt */
  73402. + gintsts.d32 = 0;
  73403. + gintsts.b.usbsuspend = 1;
  73404. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  73405. +
  73406. + return 1;
  73407. +}
  73408. +
  73409. +static int32_t dwc_otg_handle_xhib_exit_intr(dwc_otg_core_if_t * core_if)
  73410. +{
  73411. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  73412. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  73413. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  73414. +
  73415. + dwc_udelay(10);
  73416. +
  73417. + /* Program GPIO register while entering to xHib */
  73418. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x0);
  73419. +
  73420. + pcgcctl.d32 = core_if->gr_backup->xhib_pcgcctl;
  73421. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  73422. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  73423. + dwc_udelay(10);
  73424. +
  73425. + gpwrdn.d32 = core_if->gr_backup->xhib_gpwrdn;
  73426. + gpwrdn.b.restore = 1;
  73427. + DWC_WRITE_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32);
  73428. + dwc_udelay(10);
  73429. +
  73430. + restore_lpm_i2c_regs(core_if);
  73431. +
  73432. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  73433. + pcgcctl.b.max_xcvrselect = 1;
  73434. + pcgcctl.b.ess_reg_restored = 0;
  73435. + pcgcctl.b.extnd_hiber_switch = 0;
  73436. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  73437. + pcgcctl.b.enbl_extnd_hiber = 1;
  73438. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  73439. +
  73440. + gahbcfg.d32 = core_if->gr_backup->gahbcfg_local;
  73441. + gahbcfg.b.glblintrmsk = 1;
  73442. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  73443. +
  73444. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  73445. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0x1 << 16);
  73446. +
  73447. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  73448. + core_if->gr_backup->gusbcfg_local);
  73449. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  73450. + core_if->dr_backup->dcfg);
  73451. +
  73452. + pcgcctl.d32 = 0;
  73453. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  73454. + pcgcctl.b.max_xcvrselect = 1;
  73455. + pcgcctl.d32 |= 0x608;
  73456. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  73457. + dwc_udelay(10);
  73458. +
  73459. + pcgcctl.d32 = 0;
  73460. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  73461. + pcgcctl.b.max_xcvrselect = 1;
  73462. + pcgcctl.b.ess_reg_restored = 1;
  73463. + pcgcctl.b.enbl_extnd_hiber = 1;
  73464. + pcgcctl.b.rstpdwnmodule = 1;
  73465. + pcgcctl.b.restoremode = 1;
  73466. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  73467. +
  73468. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  73469. +
  73470. + return 1;
  73471. +}
  73472. +
  73473. +#ifdef CONFIG_USB_DWC_OTG_LPM
  73474. +/**
  73475. + * This function hadles LPM transaction received interrupt.
  73476. + */
  73477. +static int32_t dwc_otg_handle_lpm_intr(dwc_otg_core_if_t * core_if)
  73478. +{
  73479. + glpmcfg_data_t lpmcfg;
  73480. + gintsts_data_t gintsts;
  73481. +
  73482. + if (!core_if->core_params->lpm_enable) {
  73483. + DWC_PRINTF("Unexpected LPM interrupt\n");
  73484. + }
  73485. +
  73486. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  73487. + DWC_PRINTF("LPM config register = 0x%08x\n", lpmcfg.d32);
  73488. +
  73489. + if (dwc_otg_is_host_mode(core_if)) {
  73490. + cil_hcd_sleep(core_if);
  73491. + } else {
  73492. + lpmcfg.b.hird_thres |= (1 << 4);
  73493. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  73494. + lpmcfg.d32);
  73495. + }
  73496. +
  73497. + /* Examine prt_sleep_sts after TL1TokenTetry period max (10 us) */
  73498. + dwc_udelay(10);
  73499. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  73500. + if (lpmcfg.b.prt_sleep_sts) {
  73501. + /* Save the current state */
  73502. + core_if->lx_state = DWC_OTG_L1;
  73503. + }
  73504. +
  73505. + /* Clear interrupt */
  73506. + gintsts.d32 = 0;
  73507. + gintsts.b.lpmtranrcvd = 1;
  73508. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  73509. + return 1;
  73510. +}
  73511. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  73512. +
  73513. +/**
  73514. + * This function returns the Core Interrupt register.
  73515. + */
  73516. +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)
  73517. +{
  73518. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  73519. + gintsts_data_t gintsts;
  73520. + gintmsk_data_t gintmsk;
  73521. + gintmsk_data_t gintmsk_common = {.d32 = 0 };
  73522. + gintmsk_common.b.wkupintr = 1;
  73523. + gintmsk_common.b.sessreqintr = 1;
  73524. + gintmsk_common.b.conidstschng = 1;
  73525. + gintmsk_common.b.otgintr = 1;
  73526. + gintmsk_common.b.modemismatch = 1;
  73527. + gintmsk_common.b.disconnect = 1;
  73528. + gintmsk_common.b.usbsuspend = 1;
  73529. +#ifdef CONFIG_USB_DWC_OTG_LPM
  73530. + gintmsk_common.b.lpmtranrcvd = 1;
  73531. +#endif
  73532. + gintmsk_common.b.restoredone = 1;
  73533. + if(dwc_otg_is_device_mode(core_if))
  73534. + {
  73535. + /** @todo: The port interrupt occurs while in device
  73536. + * mode. Added code to CIL to clear the interrupt for now!
  73537. + */
  73538. + gintmsk_common.b.portintr = 1;
  73539. + }
  73540. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  73541. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  73542. + if(fiq_enable) {
  73543. + local_fiq_disable();
  73544. + /* Pull in the interrupts that the FIQ has masked */
  73545. + gintmsk.d32 |= ~(hcd->fiq_state->gintmsk_saved.d32);
  73546. + gintmsk.d32 |= gintmsk_common.d32;
  73547. + /* for the upstairs function to reenable - have to read it here in case FIQ triggers again */
  73548. + reenable_gintmsk->d32 = gintmsk.d32;
  73549. + local_fiq_enable();
  73550. + }
  73551. +
  73552. + gahbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  73553. +
  73554. +#ifdef DEBUG
  73555. + /* if any common interrupts set */
  73556. + if (gintsts.d32 & gintmsk_common.d32) {
  73557. + DWC_DEBUGPL(DBG_ANY, "common_intr: gintsts=%08x gintmsk=%08x\n",
  73558. + gintsts.d32, gintmsk.d32);
  73559. + }
  73560. +#endif
  73561. + if (!fiq_enable){
  73562. + if (gahbcfg.b.glblintrmsk)
  73563. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  73564. + else
  73565. + return 0;
  73566. + } else {
  73567. + /* Our IRQ kicker is no longer the USB hardware, it's the MPHI interface.
  73568. + * Can't trust the global interrupt mask bit in this case.
  73569. + */
  73570. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  73571. + }
  73572. +
  73573. +}
  73574. +
  73575. +/* MACRO for clearing interupt bits in GPWRDN register */
  73576. +#define CLEAR_GPWRDN_INTR(__core_if,__intr) \
  73577. +do { \
  73578. + gpwrdn_data_t gpwrdn = {.d32=0}; \
  73579. + gpwrdn.b.__intr = 1; \
  73580. + DWC_MODIFY_REG32(&__core_if->core_global_regs->gpwrdn, \
  73581. + 0, gpwrdn.d32); \
  73582. +} while (0)
  73583. +
  73584. +/**
  73585. + * Common interrupt handler.
  73586. + *
  73587. + * The common interrupts are those that occur in both Host and Device mode.
  73588. + * This handler handles the following interrupts:
  73589. + * - Mode Mismatch Interrupt
  73590. + * - Disconnect Interrupt
  73591. + * - OTG Interrupt
  73592. + * - Connector ID Status Change Interrupt
  73593. + * - Session Request Interrupt.
  73594. + * - Resume / Remote Wakeup Detected Interrupt.
  73595. + * - LPM Transaction Received Interrupt
  73596. + * - ADP Transaction Received Interrupt
  73597. + *
  73598. + */
  73599. +int32_t dwc_otg_handle_common_intr(void *dev)
  73600. +{
  73601. + int retval = 0;
  73602. + gintsts_data_t gintsts;
  73603. + gintmsk_data_t gintmsk_reenable = { .d32 = 0 };
  73604. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  73605. + dwc_otg_device_t *otg_dev = dev;
  73606. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  73607. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  73608. + if (dwc_otg_is_device_mode(core_if))
  73609. + core_if->frame_num = dwc_otg_get_frame_number(core_if);
  73610. +
  73611. + if (core_if->lock)
  73612. + DWC_SPINLOCK(core_if->lock);
  73613. +
  73614. + if (core_if->power_down == 3 && core_if->xhib == 1) {
  73615. + DWC_DEBUGPL(DBG_ANY, "Exiting from xHIB state\n");
  73616. + retval |= dwc_otg_handle_xhib_exit_intr(core_if);
  73617. + core_if->xhib = 2;
  73618. + if (core_if->lock)
  73619. + DWC_SPINUNLOCK(core_if->lock);
  73620. +
  73621. + return retval;
  73622. + }
  73623. +
  73624. + if (core_if->hibernation_suspend <= 0) {
  73625. + /* read_common will have to poke the FIQ's saved mask. We must then clear this mask at the end
  73626. + * of this handler - god only knows why it's done like this
  73627. + */
  73628. + gintsts.d32 = dwc_otg_read_common_intr(core_if, &gintmsk_reenable, otg_dev->hcd);
  73629. +
  73630. + if (gintsts.b.modemismatch) {
  73631. + retval |= dwc_otg_handle_mode_mismatch_intr(core_if);
  73632. + }
  73633. + if (gintsts.b.otgintr) {
  73634. + retval |= dwc_otg_handle_otg_intr(core_if);
  73635. + }
  73636. + if (gintsts.b.conidstschng) {
  73637. + retval |=
  73638. + dwc_otg_handle_conn_id_status_change_intr(core_if);
  73639. + }
  73640. + if (gintsts.b.disconnect) {
  73641. + retval |= dwc_otg_handle_disconnect_intr(core_if);
  73642. + }
  73643. + if (gintsts.b.sessreqintr) {
  73644. + retval |= dwc_otg_handle_session_req_intr(core_if);
  73645. + }
  73646. + if (gintsts.b.wkupintr) {
  73647. + retval |= dwc_otg_handle_wakeup_detected_intr(core_if);
  73648. + }
  73649. + if (gintsts.b.usbsuspend) {
  73650. + retval |= dwc_otg_handle_usb_suspend_intr(core_if);
  73651. + }
  73652. +#ifdef CONFIG_USB_DWC_OTG_LPM
  73653. + if (gintsts.b.lpmtranrcvd) {
  73654. + retval |= dwc_otg_handle_lpm_intr(core_if);
  73655. + }
  73656. +#endif
  73657. + if (gintsts.b.restoredone) {
  73658. + gintsts.d32 = 0;
  73659. + if (core_if->power_down == 2)
  73660. + core_if->hibernation_suspend = -1;
  73661. + else if (core_if->power_down == 3 && core_if->xhib == 2) {
  73662. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  73663. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  73664. + dctl_data_t dctl = {.d32 = 0 };
  73665. +
  73666. + DWC_WRITE_REG32(&core_if->core_global_regs->
  73667. + gintsts, 0xFFFFFFFF);
  73668. +
  73669. + DWC_DEBUGPL(DBG_ANY,
  73670. + "RESTORE DONE generated\n");
  73671. +
  73672. + gpwrdn.b.restore = 1;
  73673. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  73674. + dwc_udelay(10);
  73675. +
  73676. + pcgcctl.b.rstpdwnmodule = 1;
  73677. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  73678. +
  73679. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, core_if->gr_backup->gusbcfg_local);
  73680. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, core_if->dr_backup->dcfg);
  73681. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, core_if->dr_backup->dctl);
  73682. + dwc_udelay(50);
  73683. +
  73684. + dctl.b.pwronprgdone = 1;
  73685. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  73686. + dwc_udelay(10);
  73687. +
  73688. + dwc_otg_restore_global_regs(core_if);
  73689. + dwc_otg_restore_dev_regs(core_if, 0);
  73690. +
  73691. + dctl.d32 = 0;
  73692. + dctl.b.pwronprgdone = 1;
  73693. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  73694. + dwc_udelay(10);
  73695. +
  73696. + pcgcctl.d32 = 0;
  73697. + pcgcctl.b.enbl_extnd_hiber = 1;
  73698. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  73699. +
  73700. + /* The core will be in ON STATE */
  73701. + core_if->lx_state = DWC_OTG_L0;
  73702. + core_if->xhib = 0;
  73703. +
  73704. + DWC_SPINUNLOCK(core_if->lock);
  73705. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  73706. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  73707. + }
  73708. + DWC_SPINLOCK(core_if->lock);
  73709. +
  73710. + }
  73711. +
  73712. + gintsts.b.restoredone = 1;
  73713. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  73714. + DWC_PRINTF(" --Restore done interrupt received-- \n");
  73715. + retval |= 1;
  73716. + }
  73717. + if (gintsts.b.portintr && dwc_otg_is_device_mode(core_if)) {
  73718. + /* The port interrupt occurs while in device mode with HPRT0
  73719. + * Port Enable/Disable.
  73720. + */
  73721. + gintsts.d32 = 0;
  73722. + gintsts.b.portintr = 1;
  73723. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  73724. + retval |= 1;
  73725. + gintmsk_reenable.b.portintr = 1;
  73726. +
  73727. + }
  73728. + /* Did we actually handle anything? if so, unmask the interrupt */
  73729. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "CILOUT %1d", retval);
  73730. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintsts.d32);
  73731. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintmsk_reenable.d32);
  73732. + if (retval && fiq_enable) {
  73733. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_reenable.d32);
  73734. + }
  73735. +
  73736. + } else {
  73737. + DWC_DEBUGPL(DBG_ANY, "gpwrdn=%08x\n", gpwrdn.d32);
  73738. +
  73739. + if (gpwrdn.b.disconn_det && gpwrdn.b.disconn_det_msk) {
  73740. + CLEAR_GPWRDN_INTR(core_if, disconn_det);
  73741. + if (gpwrdn.b.linestate == 0) {
  73742. + dwc_otg_handle_pwrdn_disconnect_intr(core_if);
  73743. + } else {
  73744. + DWC_PRINTF("Disconnect detected while linestate is not 0\n");
  73745. + }
  73746. +
  73747. + retval |= 1;
  73748. + }
  73749. + if (gpwrdn.b.lnstschng && gpwrdn.b.lnstchng_msk) {
  73750. + CLEAR_GPWRDN_INTR(core_if, lnstschng);
  73751. + /* remote wakeup from hibernation */
  73752. + if (gpwrdn.b.linestate == 2 || gpwrdn.b.linestate == 1) {
  73753. + dwc_otg_handle_pwrdn_wakeup_detected_intr(core_if);
  73754. + } else {
  73755. + DWC_PRINTF("gpwrdn.linestate = %d\n", gpwrdn.b.linestate);
  73756. + }
  73757. + retval |= 1;
  73758. + }
  73759. + if (gpwrdn.b.rst_det && gpwrdn.b.rst_det_msk) {
  73760. + CLEAR_GPWRDN_INTR(core_if, rst_det);
  73761. + if (gpwrdn.b.linestate == 0) {
  73762. + DWC_PRINTF("Reset detected\n");
  73763. + retval |= dwc_otg_device_hibernation_restore(core_if, 0, 1);
  73764. + }
  73765. + }
  73766. + if (gpwrdn.b.srp_det && gpwrdn.b.srp_det_msk) {
  73767. + CLEAR_GPWRDN_INTR(core_if, srp_det);
  73768. + dwc_otg_handle_pwrdn_srp_intr(core_if);
  73769. + retval |= 1;
  73770. + }
  73771. + }
  73772. + /* Handle ADP interrupt here */
  73773. + if (gpwrdn.b.adp_int) {
  73774. + DWC_PRINTF("ADP interrupt\n");
  73775. + CLEAR_GPWRDN_INTR(core_if, adp_int);
  73776. + dwc_otg_adp_handle_intr(core_if);
  73777. + retval |= 1;
  73778. + }
  73779. + if (gpwrdn.b.sts_chngint && gpwrdn.b.sts_chngint_msk) {
  73780. + DWC_PRINTF("STS CHNG interrupt asserted\n");
  73781. + CLEAR_GPWRDN_INTR(core_if, sts_chngint);
  73782. + dwc_otg_handle_pwrdn_stschng_intr(otg_dev);
  73783. +
  73784. + retval |= 1;
  73785. + }
  73786. + if (core_if->lock)
  73787. + DWC_SPINUNLOCK(core_if->lock);
  73788. + return retval;
  73789. +}
  73790. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_core_if.h linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_core_if.h
  73791. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_core_if.h 1969-12-31 18:00:00.000000000 -0600
  73792. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_core_if.h 2014-12-03 19:13:40.216418001 -0600
  73793. @@ -0,0 +1,705 @@
  73794. +/* ==========================================================================
  73795. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_core_if.h $
  73796. + * $Revision: #13 $
  73797. + * $Date: 2012/08/10 $
  73798. + * $Change: 2047372 $
  73799. + *
  73800. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  73801. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  73802. + * otherwise expressly agreed to in writing between Synopsys and you.
  73803. + *
  73804. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  73805. + * any End User Software License Agreement or Agreement for Licensed Product
  73806. + * with Synopsys or any supplement thereto. You are permitted to use and
  73807. + * redistribute this Software in source and binary forms, with or without
  73808. + * modification, provided that redistributions of source code must retain this
  73809. + * notice. You may not view, use, disclose, copy or distribute this file or
  73810. + * any information contained herein except pursuant to this license grant from
  73811. + * Synopsys. If you do not agree with this notice, including the disclaimer
  73812. + * below, then you are not authorized to use the Software.
  73813. + *
  73814. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  73815. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  73816. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  73817. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  73818. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  73819. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  73820. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  73821. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  73822. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  73823. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  73824. + * DAMAGE.
  73825. + * ========================================================================== */
  73826. +#if !defined(__DWC_CORE_IF_H__)
  73827. +#define __DWC_CORE_IF_H__
  73828. +
  73829. +#include "dwc_os.h"
  73830. +
  73831. +/** @file
  73832. + * This file defines DWC_OTG Core API
  73833. + */
  73834. +
  73835. +struct dwc_otg_core_if;
  73836. +typedef struct dwc_otg_core_if dwc_otg_core_if_t;
  73837. +
  73838. +/** Maximum number of Periodic FIFOs */
  73839. +#define MAX_PERIO_FIFOS 15
  73840. +/** Maximum number of Periodic FIFOs */
  73841. +#define MAX_TX_FIFOS 15
  73842. +
  73843. +/** Maximum number of Endpoints/HostChannels */
  73844. +#define MAX_EPS_CHANNELS 16
  73845. +
  73846. +extern dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * _reg_base_addr);
  73847. +extern void dwc_otg_core_init(dwc_otg_core_if_t * _core_if);
  73848. +extern void dwc_otg_cil_remove(dwc_otg_core_if_t * _core_if);
  73849. +
  73850. +extern void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * _core_if);
  73851. +extern void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * _core_if);
  73852. +
  73853. +extern uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if);
  73854. +extern uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if);
  73855. +
  73856. +extern uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if);
  73857. +
  73858. +/** This function should be called on every hardware interrupt. */
  73859. +extern int32_t dwc_otg_handle_common_intr(void *otg_dev);
  73860. +
  73861. +/** @name OTG Core Parameters */
  73862. +/** @{ */
  73863. +
  73864. +/**
  73865. + * Specifies the OTG capabilities. The driver will automatically
  73866. + * detect the value for this parameter if none is specified.
  73867. + * 0 - HNP and SRP capable (default)
  73868. + * 1 - SRP Only capable
  73869. + * 2 - No HNP/SRP capable
  73870. + */
  73871. +extern int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val);
  73872. +extern int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if);
  73873. +#define DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE 0
  73874. +#define DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE 1
  73875. +#define DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE 2
  73876. +#define dwc_param_otg_cap_default DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE
  73877. +
  73878. +extern int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val);
  73879. +extern int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if);
  73880. +#define dwc_param_opt_default 1
  73881. +
  73882. +/**
  73883. + * Specifies whether to use slave or DMA mode for accessing the data
  73884. + * FIFOs. The driver will automatically detect the value for this
  73885. + * parameter if none is specified.
  73886. + * 0 - Slave
  73887. + * 1 - DMA (default, if available)
  73888. + */
  73889. +extern int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if,
  73890. + int32_t val);
  73891. +extern int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if);
  73892. +#define dwc_param_dma_enable_default 1
  73893. +
  73894. +/**
  73895. + * When DMA mode is enabled specifies whether to use
  73896. + * address DMA or DMA Descritor mode for accessing the data
  73897. + * FIFOs in device mode. The driver will automatically detect
  73898. + * the value for this parameter if none is specified.
  73899. + * 0 - address DMA
  73900. + * 1 - DMA Descriptor(default, if available)
  73901. + */
  73902. +extern int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if,
  73903. + int32_t val);
  73904. +extern int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if);
  73905. +//#define dwc_param_dma_desc_enable_default 1
  73906. +#define dwc_param_dma_desc_enable_default 0 // Broadcom BCM2708
  73907. +
  73908. +/** The DMA Burst size (applicable only for External DMA
  73909. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  73910. + */
  73911. +extern int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if,
  73912. + int32_t val);
  73913. +extern int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if);
  73914. +#define dwc_param_dma_burst_size_default 32
  73915. +
  73916. +/**
  73917. + * Specifies the maximum speed of operation in host and device mode.
  73918. + * The actual speed depends on the speed of the attached device and
  73919. + * the value of phy_type. The actual speed depends on the speed of the
  73920. + * attached device.
  73921. + * 0 - High Speed (default)
  73922. + * 1 - Full Speed
  73923. + */
  73924. +extern int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val);
  73925. +extern int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if);
  73926. +#define dwc_param_speed_default 0
  73927. +#define DWC_SPEED_PARAM_HIGH 0
  73928. +#define DWC_SPEED_PARAM_FULL 1
  73929. +
  73930. +/** Specifies whether low power mode is supported when attached
  73931. + * to a Full Speed or Low Speed device in host mode.
  73932. + * 0 - Don't support low power mode (default)
  73933. + * 1 - Support low power mode
  73934. + */
  73935. +extern int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  73936. + core_if, int32_t val);
  73937. +extern int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t
  73938. + * core_if);
  73939. +#define dwc_param_host_support_fs_ls_low_power_default 0
  73940. +
  73941. +/** Specifies the PHY clock rate in low power mode when connected to a
  73942. + * Low Speed device in host mode. This parameter is applicable only if
  73943. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  73944. + * then defaults to 6 MHZ otherwise 48 MHZ.
  73945. + *
  73946. + * 0 - 48 MHz
  73947. + * 1 - 6 MHz
  73948. + */
  73949. +extern int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  73950. + core_if, int32_t val);
  73951. +extern int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  73952. + core_if);
  73953. +#define dwc_param_host_ls_low_power_phy_clk_default 0
  73954. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ 0
  73955. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ 1
  73956. +
  73957. +/**
  73958. + * 0 - Use cC FIFO size parameters
  73959. + * 1 - Allow dynamic FIFO sizing (default)
  73960. + */
  73961. +extern int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  73962. + int32_t val);
  73963. +extern int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t *
  73964. + core_if);
  73965. +#define dwc_param_enable_dynamic_fifo_default 1
  73966. +
  73967. +/** Total number of 4-byte words in the data FIFO memory. This
  73968. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  73969. + * Tx FIFOs.
  73970. + * 32 to 32768 (default 8192)
  73971. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  73972. + */
  73973. +extern int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if,
  73974. + int32_t val);
  73975. +extern int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if);
  73976. +//#define dwc_param_data_fifo_size_default 8192
  73977. +#define dwc_param_data_fifo_size_default 0xFF0 // Broadcom BCM2708
  73978. +
  73979. +/** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  73980. + * FIFO sizing is enabled.
  73981. + * 16 to 32768 (default 1064)
  73982. + */
  73983. +extern int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if,
  73984. + int32_t val);
  73985. +extern int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if);
  73986. +#define dwc_param_dev_rx_fifo_size_default 1064
  73987. +
  73988. +/** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  73989. + * when dynamic FIFO sizing is enabled.
  73990. + * 16 to 32768 (default 1024)
  73991. + */
  73992. +extern int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  73993. + core_if, int32_t val);
  73994. +extern int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  73995. + core_if);
  73996. +#define dwc_param_dev_nperio_tx_fifo_size_default 1024
  73997. +
  73998. +/** Number of 4-byte words in each of the periodic Tx FIFOs in device
  73999. + * mode when dynamic FIFO sizing is enabled.
  74000. + * 4 to 768 (default 256)
  74001. + */
  74002. +extern int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  74003. + int32_t val, int fifo_num);
  74004. +extern int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t *
  74005. + core_if, int fifo_num);
  74006. +#define dwc_param_dev_perio_tx_fifo_size_default 256
  74007. +
  74008. +/** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  74009. + * FIFO sizing is enabled.
  74010. + * 16 to 32768 (default 1024)
  74011. + */
  74012. +extern int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  74013. + int32_t val);
  74014. +extern int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if);
  74015. +//#define dwc_param_host_rx_fifo_size_default 1024
  74016. +#define dwc_param_host_rx_fifo_size_default 774 // Broadcom BCM2708
  74017. +
  74018. +/** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  74019. + * when Dynamic FIFO sizing is enabled in the core.
  74020. + * 16 to 32768 (default 1024)
  74021. + */
  74022. +extern int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  74023. + core_if, int32_t val);
  74024. +extern int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  74025. + core_if);
  74026. +//#define dwc_param_host_nperio_tx_fifo_size_default 1024
  74027. +#define dwc_param_host_nperio_tx_fifo_size_default 0x100 // Broadcom BCM2708
  74028. +
  74029. +/** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  74030. + * FIFO sizing is enabled.
  74031. + * 16 to 32768 (default 1024)
  74032. + */
  74033. +extern int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  74034. + core_if, int32_t val);
  74035. +extern int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  74036. + core_if);
  74037. +//#define dwc_param_host_perio_tx_fifo_size_default 1024
  74038. +#define dwc_param_host_perio_tx_fifo_size_default 0x200 // Broadcom BCM2708
  74039. +
  74040. +/** The maximum transfer size supported in bytes.
  74041. + * 2047 to 65,535 (default 65,535)
  74042. + */
  74043. +extern int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  74044. + int32_t val);
  74045. +extern int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if);
  74046. +#define dwc_param_max_transfer_size_default 65535
  74047. +
  74048. +/** The maximum number of packets in a transfer.
  74049. + * 15 to 511 (default 511)
  74050. + */
  74051. +extern int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if,
  74052. + int32_t val);
  74053. +extern int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if);
  74054. +#define dwc_param_max_packet_count_default 511
  74055. +
  74056. +/** The number of host channel registers to use.
  74057. + * 1 to 16 (default 12)
  74058. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  74059. + */
  74060. +extern int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if,
  74061. + int32_t val);
  74062. +extern int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if);
  74063. +//#define dwc_param_host_channels_default 12
  74064. +#define dwc_param_host_channels_default 8 // Broadcom BCM2708
  74065. +
  74066. +/** The number of endpoints in addition to EP0 available for device
  74067. + * mode operations.
  74068. + * 1 to 15 (default 6 IN and OUT)
  74069. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  74070. + * endpoints in addition to EP0.
  74071. + */
  74072. +extern int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if,
  74073. + int32_t val);
  74074. +extern int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if);
  74075. +#define dwc_param_dev_endpoints_default 6
  74076. +
  74077. +/**
  74078. + * Specifies the type of PHY interface to use. By default, the driver
  74079. + * will automatically detect the phy_type.
  74080. + *
  74081. + * 0 - Full Speed PHY
  74082. + * 1 - UTMI+ (default)
  74083. + * 2 - ULPI
  74084. + */
  74085. +extern int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val);
  74086. +extern int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if);
  74087. +#define DWC_PHY_TYPE_PARAM_FS 0
  74088. +#define DWC_PHY_TYPE_PARAM_UTMI 1
  74089. +#define DWC_PHY_TYPE_PARAM_ULPI 2
  74090. +#define dwc_param_phy_type_default DWC_PHY_TYPE_PARAM_UTMI
  74091. +
  74092. +/**
  74093. + * Specifies the UTMI+ Data Width. This parameter is
  74094. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  74095. + * PHY_TYPE, this parameter indicates the data width between
  74096. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  74097. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  74098. + * to "8 and 16 bits", meaning that the core has been
  74099. + * configured to work at either data path width.
  74100. + *
  74101. + * 8 or 16 bits (default 16)
  74102. + */
  74103. +extern int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if,
  74104. + int32_t val);
  74105. +extern int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if);
  74106. +//#define dwc_param_phy_utmi_width_default 16
  74107. +#define dwc_param_phy_utmi_width_default 8 // Broadcom BCM2708
  74108. +
  74109. +/**
  74110. + * Specifies whether the ULPI operates at double or single
  74111. + * data rate. This parameter is only applicable if PHY_TYPE is
  74112. + * ULPI.
  74113. + *
  74114. + * 0 - single data rate ULPI interface with 8 bit wide data
  74115. + * bus (default)
  74116. + * 1 - double data rate ULPI interface with 4 bit wide data
  74117. + * bus
  74118. + */
  74119. +extern int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if,
  74120. + int32_t val);
  74121. +extern int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if);
  74122. +#define dwc_param_phy_ulpi_ddr_default 0
  74123. +
  74124. +/**
  74125. + * Specifies whether to use the internal or external supply to
  74126. + * drive the vbus with a ULPI phy.
  74127. + */
  74128. +extern int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  74129. + int32_t val);
  74130. +extern int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if);
  74131. +#define DWC_PHY_ULPI_INTERNAL_VBUS 0
  74132. +#define DWC_PHY_ULPI_EXTERNAL_VBUS 1
  74133. +#define dwc_param_phy_ulpi_ext_vbus_default DWC_PHY_ULPI_INTERNAL_VBUS
  74134. +
  74135. +/**
  74136. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  74137. + * parameter is only applicable if PHY_TYPE is FS.
  74138. + * 0 - No (default)
  74139. + * 1 - Yes
  74140. + */
  74141. +extern int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if,
  74142. + int32_t val);
  74143. +extern int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if);
  74144. +#define dwc_param_i2c_enable_default 0
  74145. +
  74146. +extern int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if,
  74147. + int32_t val);
  74148. +extern int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if);
  74149. +#define dwc_param_ulpi_fs_ls_default 0
  74150. +
  74151. +extern int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val);
  74152. +extern int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if);
  74153. +#define dwc_param_ts_dline_default 0
  74154. +
  74155. +/**
  74156. + * Specifies whether dedicated transmit FIFOs are
  74157. + * enabled for non periodic IN endpoints in device mode
  74158. + * 0 - No
  74159. + * 1 - Yes
  74160. + */
  74161. +extern int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  74162. + int32_t val);
  74163. +extern int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t *
  74164. + core_if);
  74165. +#define dwc_param_en_multiple_tx_fifo_default 1
  74166. +
  74167. +/** Number of 4-byte words in each of the Tx FIFOs in device
  74168. + * mode when dynamic FIFO sizing is enabled.
  74169. + * 4 to 768 (default 256)
  74170. + */
  74171. +extern int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  74172. + int fifo_num, int32_t val);
  74173. +extern int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  74174. + int fifo_num);
  74175. +#define dwc_param_dev_tx_fifo_size_default 768
  74176. +
  74177. +/** Thresholding enable flag-
  74178. + * bit 0 - enable non-ISO Tx thresholding
  74179. + * bit 1 - enable ISO Tx thresholding
  74180. + * bit 2 - enable Rx thresholding
  74181. + */
  74182. +extern int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val);
  74183. +extern int32_t dwc_otg_get_thr_ctl(dwc_otg_core_if_t * core_if, int fifo_num);
  74184. +#define dwc_param_thr_ctl_default 0
  74185. +
  74186. +/** Thresholding length for Tx
  74187. + * FIFOs in 32 bit DWORDs
  74188. + */
  74189. +extern int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if,
  74190. + int32_t val);
  74191. +extern int32_t dwc_otg_get_tx_thr_length(dwc_otg_core_if_t * core_if);
  74192. +#define dwc_param_tx_thr_length_default 64
  74193. +
  74194. +/** Thresholding length for Rx
  74195. + * FIFOs in 32 bit DWORDs
  74196. + */
  74197. +extern int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if,
  74198. + int32_t val);
  74199. +extern int32_t dwc_otg_get_rx_thr_length(dwc_otg_core_if_t * core_if);
  74200. +#define dwc_param_rx_thr_length_default 64
  74201. +
  74202. +/**
  74203. + * Specifies whether LPM (Link Power Management) support is enabled
  74204. + */
  74205. +extern int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if,
  74206. + int32_t val);
  74207. +extern int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if);
  74208. +#define dwc_param_lpm_enable_default 1
  74209. +
  74210. +/**
  74211. + * Specifies whether PTI enhancement is enabled
  74212. + */
  74213. +extern int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if,
  74214. + int32_t val);
  74215. +extern int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if);
  74216. +#define dwc_param_pti_enable_default 0
  74217. +
  74218. +/**
  74219. + * Specifies whether MPI enhancement is enabled
  74220. + */
  74221. +extern int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if,
  74222. + int32_t val);
  74223. +extern int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if);
  74224. +#define dwc_param_mpi_enable_default 0
  74225. +
  74226. +/**
  74227. + * Specifies whether ADP capability is enabled
  74228. + */
  74229. +extern int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if,
  74230. + int32_t val);
  74231. +extern int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if);
  74232. +#define dwc_param_adp_enable_default 0
  74233. +
  74234. +/**
  74235. + * Specifies whether IC_USB capability is enabled
  74236. + */
  74237. +
  74238. +extern int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if,
  74239. + int32_t val);
  74240. +extern int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if);
  74241. +#define dwc_param_ic_usb_cap_default 0
  74242. +
  74243. +extern int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if,
  74244. + int32_t val);
  74245. +extern int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if);
  74246. +#define dwc_param_ahb_thr_ratio_default 0
  74247. +
  74248. +extern int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if,
  74249. + int32_t val);
  74250. +extern int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if);
  74251. +#define dwc_param_power_down_default 0
  74252. +
  74253. +extern int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if,
  74254. + int32_t val);
  74255. +extern int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if);
  74256. +#define dwc_param_reload_ctl_default 0
  74257. +
  74258. +extern int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if,
  74259. + int32_t val);
  74260. +extern int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if);
  74261. +#define dwc_param_dev_out_nak_default 0
  74262. +
  74263. +extern int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if,
  74264. + int32_t val);
  74265. +extern int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if);
  74266. +#define dwc_param_cont_on_bna_default 0
  74267. +
  74268. +extern int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if,
  74269. + int32_t val);
  74270. +extern int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if);
  74271. +#define dwc_param_ahb_single_default 0
  74272. +
  74273. +extern int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val);
  74274. +extern int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if);
  74275. +#define dwc_param_otg_ver_default 0
  74276. +
  74277. +/** @} */
  74278. +
  74279. +/** @name Access to registers and bit-fields */
  74280. +
  74281. +/**
  74282. + * Dump core registers and SPRAM
  74283. + */
  74284. +extern void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * _core_if);
  74285. +extern void dwc_otg_dump_spram(dwc_otg_core_if_t * _core_if);
  74286. +extern void dwc_otg_dump_host_registers(dwc_otg_core_if_t * _core_if);
  74287. +extern void dwc_otg_dump_global_registers(dwc_otg_core_if_t * _core_if);
  74288. +
  74289. +/**
  74290. + * Get host negotiation status.
  74291. + */
  74292. +extern uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if);
  74293. +
  74294. +/**
  74295. + * Get srp status
  74296. + */
  74297. +extern uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if);
  74298. +
  74299. +/**
  74300. + * Set hnpreq bit in the GOTGCTL register.
  74301. + */
  74302. +extern void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val);
  74303. +
  74304. +/**
  74305. + * Get Content of SNPSID register.
  74306. + */
  74307. +extern uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if);
  74308. +
  74309. +/**
  74310. + * Get current mode.
  74311. + * Returns 0 if in device mode, and 1 if in host mode.
  74312. + */
  74313. +extern uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if);
  74314. +
  74315. +/**
  74316. + * Get value of hnpcapable field in the GUSBCFG register
  74317. + */
  74318. +extern uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if);
  74319. +/**
  74320. + * Set value of hnpcapable field in the GUSBCFG register
  74321. + */
  74322. +extern void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  74323. +
  74324. +/**
  74325. + * Get value of srpcapable field in the GUSBCFG register
  74326. + */
  74327. +extern uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if);
  74328. +/**
  74329. + * Set value of srpcapable field in the GUSBCFG register
  74330. + */
  74331. +extern void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  74332. +
  74333. +/**
  74334. + * Get value of devspeed field in the DCFG register
  74335. + */
  74336. +extern uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if);
  74337. +/**
  74338. + * Set value of devspeed field in the DCFG register
  74339. + */
  74340. +extern void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val);
  74341. +
  74342. +/**
  74343. + * Get the value of busconnected field from the HPRT0 register
  74344. + */
  74345. +extern uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if);
  74346. +
  74347. +/**
  74348. + * Gets the device enumeration Speed.
  74349. + */
  74350. +extern uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if);
  74351. +
  74352. +/**
  74353. + * Get value of prtpwr field from the HPRT0 register
  74354. + */
  74355. +extern uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if);
  74356. +
  74357. +/**
  74358. + * Get value of flag indicating core state - hibernated or not
  74359. + */
  74360. +extern uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if);
  74361. +
  74362. +/**
  74363. + * Set value of prtpwr field from the HPRT0 register
  74364. + */
  74365. +extern void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val);
  74366. +
  74367. +/**
  74368. + * Get value of prtsusp field from the HPRT0 regsiter
  74369. + */
  74370. +extern uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if);
  74371. +/**
  74372. + * Set value of prtpwr field from the HPRT0 register
  74373. + */
  74374. +extern void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val);
  74375. +
  74376. +/**
  74377. + * Get value of ModeChTimEn field from the HCFG regsiter
  74378. + */
  74379. +extern uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if);
  74380. +/**
  74381. + * Set value of ModeChTimEn field from the HCFG regsiter
  74382. + */
  74383. +extern void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val);
  74384. +
  74385. +/**
  74386. + * Get value of Fram Interval field from the HFIR regsiter
  74387. + */
  74388. +extern uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if);
  74389. +/**
  74390. + * Set value of Frame Interval field from the HFIR regsiter
  74391. + */
  74392. +extern void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val);
  74393. +
  74394. +/**
  74395. + * Set value of prtres field from the HPRT0 register
  74396. + *FIXME Remove?
  74397. + */
  74398. +extern void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val);
  74399. +
  74400. +/**
  74401. + * Get value of rmtwkupsig bit in DCTL register
  74402. + */
  74403. +extern uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if);
  74404. +
  74405. +/**
  74406. + * Get value of prt_sleep_sts field from the GLPMCFG register
  74407. + */
  74408. +extern uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if);
  74409. +
  74410. +/**
  74411. + * Get value of rem_wkup_en field from the GLPMCFG register
  74412. + */
  74413. +extern uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if);
  74414. +
  74415. +/**
  74416. + * Get value of appl_resp field from the GLPMCFG register
  74417. + */
  74418. +extern uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if);
  74419. +/**
  74420. + * Set value of appl_resp field from the GLPMCFG register
  74421. + */
  74422. +extern void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val);
  74423. +
  74424. +/**
  74425. + * Get value of hsic_connect field from the GLPMCFG register
  74426. + */
  74427. +extern uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if);
  74428. +/**
  74429. + * Set value of hsic_connect field from the GLPMCFG register
  74430. + */
  74431. +extern void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val);
  74432. +
  74433. +/**
  74434. + * Get value of inv_sel_hsic field from the GLPMCFG register.
  74435. + */
  74436. +extern uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if);
  74437. +/**
  74438. + * Set value of inv_sel_hsic field from the GLPMFG register.
  74439. + */
  74440. +extern void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val);
  74441. +
  74442. +/*
  74443. + * Some functions for accessing registers
  74444. + */
  74445. +
  74446. +/**
  74447. + * GOTGCTL register
  74448. + */
  74449. +extern uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if);
  74450. +extern void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val);
  74451. +
  74452. +/**
  74453. + * GUSBCFG register
  74454. + */
  74455. +extern uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if);
  74456. +extern void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val);
  74457. +
  74458. +/**
  74459. + * GRXFSIZ register
  74460. + */
  74461. +extern uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if);
  74462. +extern void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  74463. +
  74464. +/**
  74465. + * GNPTXFSIZ register
  74466. + */
  74467. +extern uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if);
  74468. +extern void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  74469. +
  74470. +extern uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if);
  74471. +extern void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val);
  74472. +
  74473. +/**
  74474. + * GGPIO register
  74475. + */
  74476. +extern uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if);
  74477. +extern void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val);
  74478. +
  74479. +/**
  74480. + * GUID register
  74481. + */
  74482. +extern uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if);
  74483. +extern void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val);
  74484. +
  74485. +/**
  74486. + * HPRT0 register
  74487. + */
  74488. +extern uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if);
  74489. +extern void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val);
  74490. +
  74491. +/**
  74492. + * GHPTXFSIZE
  74493. + */
  74494. +extern uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if);
  74495. +
  74496. +/** @} */
  74497. +
  74498. +#endif /* __DWC_CORE_IF_H__ */
  74499. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_dbg.h linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_dbg.h
  74500. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_dbg.h 1969-12-31 18:00:00.000000000 -0600
  74501. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_dbg.h 2014-12-03 19:13:40.216418001 -0600
  74502. @@ -0,0 +1,117 @@
  74503. +/* ==========================================================================
  74504. + *
  74505. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  74506. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  74507. + * otherwise expressly agreed to in writing between Synopsys and you.
  74508. + *
  74509. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  74510. + * any End User Software License Agreement or Agreement for Licensed Product
  74511. + * with Synopsys or any supplement thereto. You are permitted to use and
  74512. + * redistribute this Software in source and binary forms, with or without
  74513. + * modification, provided that redistributions of source code must retain this
  74514. + * notice. You may not view, use, disclose, copy or distribute this file or
  74515. + * any information contained herein except pursuant to this license grant from
  74516. + * Synopsys. If you do not agree with this notice, including the disclaimer
  74517. + * below, then you are not authorized to use the Software.
  74518. + *
  74519. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  74520. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  74521. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  74522. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  74523. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  74524. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  74525. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  74526. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  74527. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  74528. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  74529. + * DAMAGE.
  74530. + * ========================================================================== */
  74531. +
  74532. +#ifndef __DWC_OTG_DBG_H__
  74533. +#define __DWC_OTG_DBG_H__
  74534. +
  74535. +/** @file
  74536. + * This file defines debug levels.
  74537. + * Debugging support vanishes in non-debug builds.
  74538. + */
  74539. +
  74540. +/**
  74541. + * The Debug Level bit-mask variable.
  74542. + */
  74543. +extern uint32_t g_dbg_lvl;
  74544. +/**
  74545. + * Set the Debug Level variable.
  74546. + */
  74547. +static inline uint32_t SET_DEBUG_LEVEL(const uint32_t new)
  74548. +{
  74549. + uint32_t old = g_dbg_lvl;
  74550. + g_dbg_lvl = new;
  74551. + return old;
  74552. +}
  74553. +
  74554. +#define DBG_USER (0x1)
  74555. +/** When debug level has the DBG_CIL bit set, display CIL Debug messages. */
  74556. +#define DBG_CIL (0x2)
  74557. +/** When debug level has the DBG_CILV bit set, display CIL Verbose debug
  74558. + * messages */
  74559. +#define DBG_CILV (0x20)
  74560. +/** When debug level has the DBG_PCD bit set, display PCD (Device) debug
  74561. + * messages */
  74562. +#define DBG_PCD (0x4)
  74563. +/** When debug level has the DBG_PCDV set, display PCD (Device) Verbose debug
  74564. + * messages */
  74565. +#define DBG_PCDV (0x40)
  74566. +/** When debug level has the DBG_HCD bit set, display Host debug messages */
  74567. +#define DBG_HCD (0x8)
  74568. +/** When debug level has the DBG_HCDV bit set, display Verbose Host debug
  74569. + * messages */
  74570. +#define DBG_HCDV (0x80)
  74571. +/** When debug level has the DBG_HCD_URB bit set, display enqueued URBs in host
  74572. + * mode. */
  74573. +#define DBG_HCD_URB (0x800)
  74574. +/** When debug level has the DBG_HCDI bit set, display host interrupt
  74575. + * messages. */
  74576. +#define DBG_HCDI (0x1000)
  74577. +
  74578. +/** When debug level has any bit set, display debug messages */
  74579. +#define DBG_ANY (0xFF)
  74580. +
  74581. +/** All debug messages off */
  74582. +#define DBG_OFF 0
  74583. +
  74584. +/** Prefix string for DWC_DEBUG print macros. */
  74585. +#define USB_DWC "DWC_otg: "
  74586. +
  74587. +/**
  74588. + * Print a debug message when the Global debug level variable contains
  74589. + * the bit defined in <code>lvl</code>.
  74590. + *
  74591. + * @param[in] lvl - Debug level, use one of the DBG_ constants above.
  74592. + * @param[in] x - like printf
  74593. + *
  74594. + * Example:<p>
  74595. + * <code>
  74596. + * DWC_DEBUGPL( DBG_ANY, "%s(%p)\n", __func__, _reg_base_addr);
  74597. + * </code>
  74598. + * <br>
  74599. + * results in:<br>
  74600. + * <code>
  74601. + * usb-DWC_otg: dwc_otg_cil_init(ca867000)
  74602. + * </code>
  74603. + */
  74604. +#ifdef DEBUG
  74605. +
  74606. +# define DWC_DEBUGPL(lvl, x...) do{ if ((lvl)&g_dbg_lvl)__DWC_DEBUG(USB_DWC x ); }while(0)
  74607. +# define DWC_DEBUGP(x...) DWC_DEBUGPL(DBG_ANY, x )
  74608. +
  74609. +# define CHK_DEBUG_LEVEL(level) ((level) & g_dbg_lvl)
  74610. +
  74611. +#else
  74612. +
  74613. +# define DWC_DEBUGPL(lvl, x...) do{}while(0)
  74614. +# define DWC_DEBUGP(x...)
  74615. +
  74616. +# define CHK_DEBUG_LEVEL(level) (0)
  74617. +
  74618. +#endif /*DEBUG*/
  74619. +#endif
  74620. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_driver.c linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_driver.c
  74621. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_driver.c 1969-12-31 18:00:00.000000000 -0600
  74622. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_driver.c 2014-12-03 19:13:40.220418001 -0600
  74623. @@ -0,0 +1,1749 @@
  74624. +/* ==========================================================================
  74625. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.c $
  74626. + * $Revision: #92 $
  74627. + * $Date: 2012/08/10 $
  74628. + * $Change: 2047372 $
  74629. + *
  74630. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  74631. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  74632. + * otherwise expressly agreed to in writing between Synopsys and you.
  74633. + *
  74634. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  74635. + * any End User Software License Agreement or Agreement for Licensed Product
  74636. + * with Synopsys or any supplement thereto. You are permitted to use and
  74637. + * redistribute this Software in source and binary forms, with or without
  74638. + * modification, provided that redistributions of source code must retain this
  74639. + * notice. You may not view, use, disclose, copy or distribute this file or
  74640. + * any information contained herein except pursuant to this license grant from
  74641. + * Synopsys. If you do not agree with this notice, including the disclaimer
  74642. + * below, then you are not authorized to use the Software.
  74643. + *
  74644. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  74645. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  74646. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  74647. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  74648. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  74649. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  74650. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  74651. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  74652. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  74653. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  74654. + * DAMAGE.
  74655. + * ========================================================================== */
  74656. +
  74657. +/** @file
  74658. + * The dwc_otg_driver module provides the initialization and cleanup entry
  74659. + * points for the DWC_otg driver. This module will be dynamically installed
  74660. + * after Linux is booted using the insmod command. When the module is
  74661. + * installed, the dwc_otg_driver_init function is called. When the module is
  74662. + * removed (using rmmod), the dwc_otg_driver_cleanup function is called.
  74663. + *
  74664. + * This module also defines a data structure for the dwc_otg_driver, which is
  74665. + * used in conjunction with the standard ARM lm_device structure. These
  74666. + * structures allow the OTG driver to comply with the standard Linux driver
  74667. + * model in which devices and drivers are registered with a bus driver. This
  74668. + * has the benefit that Linux can expose attributes of the driver and device
  74669. + * in its special sysfs file system. Users can then read or write files in
  74670. + * this file system to perform diagnostics on the driver components or the
  74671. + * device.
  74672. + */
  74673. +
  74674. +#include "dwc_otg_os_dep.h"
  74675. +#include "dwc_os.h"
  74676. +#include "dwc_otg_dbg.h"
  74677. +#include "dwc_otg_driver.h"
  74678. +#include "dwc_otg_attr.h"
  74679. +#include "dwc_otg_core_if.h"
  74680. +#include "dwc_otg_pcd_if.h"
  74681. +#include "dwc_otg_hcd_if.h"
  74682. +#include "dwc_otg_fiq_fsm.h"
  74683. +
  74684. +#define DWC_DRIVER_VERSION "3.00a 10-AUG-2012"
  74685. +#define DWC_DRIVER_DESC "HS OTG USB Controller driver"
  74686. +
  74687. +bool microframe_schedule=true;
  74688. +
  74689. +static const char dwc_driver_name[] = "dwc_otg";
  74690. +
  74691. +
  74692. +extern int pcd_init(
  74693. +#ifdef LM_INTERFACE
  74694. + struct lm_device *_dev
  74695. +#elif defined(PCI_INTERFACE)
  74696. + struct pci_dev *_dev
  74697. +#elif defined(PLATFORM_INTERFACE)
  74698. + struct platform_device *dev
  74699. +#endif
  74700. + );
  74701. +extern int hcd_init(
  74702. +#ifdef LM_INTERFACE
  74703. + struct lm_device *_dev
  74704. +#elif defined(PCI_INTERFACE)
  74705. + struct pci_dev *_dev
  74706. +#elif defined(PLATFORM_INTERFACE)
  74707. + struct platform_device *dev
  74708. +#endif
  74709. + );
  74710. +
  74711. +extern int pcd_remove(
  74712. +#ifdef LM_INTERFACE
  74713. + struct lm_device *_dev
  74714. +#elif defined(PCI_INTERFACE)
  74715. + struct pci_dev *_dev
  74716. +#elif defined(PLATFORM_INTERFACE)
  74717. + struct platform_device *_dev
  74718. +#endif
  74719. + );
  74720. +
  74721. +extern void hcd_remove(
  74722. +#ifdef LM_INTERFACE
  74723. + struct lm_device *_dev
  74724. +#elif defined(PCI_INTERFACE)
  74725. + struct pci_dev *_dev
  74726. +#elif defined(PLATFORM_INTERFACE)
  74727. + struct platform_device *_dev
  74728. +#endif
  74729. + );
  74730. +
  74731. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  74732. +
  74733. +/*-------------------------------------------------------------------------*/
  74734. +/* Encapsulate the module parameter settings */
  74735. +
  74736. +struct dwc_otg_driver_module_params {
  74737. + int32_t opt;
  74738. + int32_t otg_cap;
  74739. + int32_t dma_enable;
  74740. + int32_t dma_desc_enable;
  74741. + int32_t dma_burst_size;
  74742. + int32_t speed;
  74743. + int32_t host_support_fs_ls_low_power;
  74744. + int32_t host_ls_low_power_phy_clk;
  74745. + int32_t enable_dynamic_fifo;
  74746. + int32_t data_fifo_size;
  74747. + int32_t dev_rx_fifo_size;
  74748. + int32_t dev_nperio_tx_fifo_size;
  74749. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  74750. + int32_t host_rx_fifo_size;
  74751. + int32_t host_nperio_tx_fifo_size;
  74752. + int32_t host_perio_tx_fifo_size;
  74753. + int32_t max_transfer_size;
  74754. + int32_t max_packet_count;
  74755. + int32_t host_channels;
  74756. + int32_t dev_endpoints;
  74757. + int32_t phy_type;
  74758. + int32_t phy_utmi_width;
  74759. + int32_t phy_ulpi_ddr;
  74760. + int32_t phy_ulpi_ext_vbus;
  74761. + int32_t i2c_enable;
  74762. + int32_t ulpi_fs_ls;
  74763. + int32_t ts_dline;
  74764. + int32_t en_multiple_tx_fifo;
  74765. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  74766. + uint32_t thr_ctl;
  74767. + uint32_t tx_thr_length;
  74768. + uint32_t rx_thr_length;
  74769. + int32_t pti_enable;
  74770. + int32_t mpi_enable;
  74771. + int32_t lpm_enable;
  74772. + int32_t ic_usb_cap;
  74773. + int32_t ahb_thr_ratio;
  74774. + int32_t power_down;
  74775. + int32_t reload_ctl;
  74776. + int32_t dev_out_nak;
  74777. + int32_t cont_on_bna;
  74778. + int32_t ahb_single;
  74779. + int32_t otg_ver;
  74780. + int32_t adp_enable;
  74781. +};
  74782. +
  74783. +static struct dwc_otg_driver_module_params dwc_otg_module_params = {
  74784. + .opt = -1,
  74785. + .otg_cap = -1,
  74786. + .dma_enable = -1,
  74787. + .dma_desc_enable = -1,
  74788. + .dma_burst_size = -1,
  74789. + .speed = -1,
  74790. + .host_support_fs_ls_low_power = -1,
  74791. + .host_ls_low_power_phy_clk = -1,
  74792. + .enable_dynamic_fifo = -1,
  74793. + .data_fifo_size = -1,
  74794. + .dev_rx_fifo_size = -1,
  74795. + .dev_nperio_tx_fifo_size = -1,
  74796. + .dev_perio_tx_fifo_size = {
  74797. + /* dev_perio_tx_fifo_size_1 */
  74798. + -1,
  74799. + -1,
  74800. + -1,
  74801. + -1,
  74802. + -1,
  74803. + -1,
  74804. + -1,
  74805. + -1,
  74806. + -1,
  74807. + -1,
  74808. + -1,
  74809. + -1,
  74810. + -1,
  74811. + -1,
  74812. + -1
  74813. + /* 15 */
  74814. + },
  74815. + .host_rx_fifo_size = -1,
  74816. + .host_nperio_tx_fifo_size = -1,
  74817. + .host_perio_tx_fifo_size = -1,
  74818. + .max_transfer_size = -1,
  74819. + .max_packet_count = -1,
  74820. + .host_channels = -1,
  74821. + .dev_endpoints = -1,
  74822. + .phy_type = -1,
  74823. + .phy_utmi_width = -1,
  74824. + .phy_ulpi_ddr = -1,
  74825. + .phy_ulpi_ext_vbus = -1,
  74826. + .i2c_enable = -1,
  74827. + .ulpi_fs_ls = -1,
  74828. + .ts_dline = -1,
  74829. + .en_multiple_tx_fifo = -1,
  74830. + .dev_tx_fifo_size = {
  74831. + /* dev_tx_fifo_size */
  74832. + -1,
  74833. + -1,
  74834. + -1,
  74835. + -1,
  74836. + -1,
  74837. + -1,
  74838. + -1,
  74839. + -1,
  74840. + -1,
  74841. + -1,
  74842. + -1,
  74843. + -1,
  74844. + -1,
  74845. + -1,
  74846. + -1
  74847. + /* 15 */
  74848. + },
  74849. + .thr_ctl = -1,
  74850. + .tx_thr_length = -1,
  74851. + .rx_thr_length = -1,
  74852. + .pti_enable = -1,
  74853. + .mpi_enable = -1,
  74854. + .lpm_enable = 0,
  74855. + .ic_usb_cap = -1,
  74856. + .ahb_thr_ratio = -1,
  74857. + .power_down = -1,
  74858. + .reload_ctl = -1,
  74859. + .dev_out_nak = -1,
  74860. + .cont_on_bna = -1,
  74861. + .ahb_single = -1,
  74862. + .otg_ver = -1,
  74863. + .adp_enable = -1,
  74864. +};
  74865. +
  74866. +//Global variable to switch the fiq fix on or off
  74867. +bool fiq_enable = 1;
  74868. +// Global variable to enable the split transaction fix
  74869. +bool fiq_fsm_enable = true;
  74870. +//Bulk split-transaction NAK holdoff in microframes
  74871. +uint16_t nak_holdoff = 8;
  74872. +
  74873. +unsigned short fiq_fsm_mask = 0x07;
  74874. +
  74875. +/**
  74876. + * This function shows the Driver Version.
  74877. + */
  74878. +static ssize_t version_show(struct device_driver *dev, char *buf)
  74879. +{
  74880. + return snprintf(buf, sizeof(DWC_DRIVER_VERSION) + 2, "%s\n",
  74881. + DWC_DRIVER_VERSION);
  74882. +}
  74883. +
  74884. +static DRIVER_ATTR(version, S_IRUGO, version_show, NULL);
  74885. +
  74886. +/**
  74887. + * Global Debug Level Mask.
  74888. + */
  74889. +uint32_t g_dbg_lvl = 0; /* OFF */
  74890. +
  74891. +/**
  74892. + * This function shows the driver Debug Level.
  74893. + */
  74894. +static ssize_t dbg_level_show(struct device_driver *drv, char *buf)
  74895. +{
  74896. + return sprintf(buf, "0x%0x\n", g_dbg_lvl);
  74897. +}
  74898. +
  74899. +/**
  74900. + * This function stores the driver Debug Level.
  74901. + */
  74902. +static ssize_t dbg_level_store(struct device_driver *drv, const char *buf,
  74903. + size_t count)
  74904. +{
  74905. + g_dbg_lvl = simple_strtoul(buf, NULL, 16);
  74906. + return count;
  74907. +}
  74908. +
  74909. +static DRIVER_ATTR(debuglevel, S_IRUGO | S_IWUSR, dbg_level_show,
  74910. + dbg_level_store);
  74911. +
  74912. +/**
  74913. + * This function is called during module intialization
  74914. + * to pass module parameters to the DWC_OTG CORE.
  74915. + */
  74916. +static int set_parameters(dwc_otg_core_if_t * core_if)
  74917. +{
  74918. + int retval = 0;
  74919. + int i;
  74920. +
  74921. + if (dwc_otg_module_params.otg_cap != -1) {
  74922. + retval +=
  74923. + dwc_otg_set_param_otg_cap(core_if,
  74924. + dwc_otg_module_params.otg_cap);
  74925. + }
  74926. + if (dwc_otg_module_params.dma_enable != -1) {
  74927. + retval +=
  74928. + dwc_otg_set_param_dma_enable(core_if,
  74929. + dwc_otg_module_params.
  74930. + dma_enable);
  74931. + }
  74932. + if (dwc_otg_module_params.dma_desc_enable != -1) {
  74933. + retval +=
  74934. + dwc_otg_set_param_dma_desc_enable(core_if,
  74935. + dwc_otg_module_params.
  74936. + dma_desc_enable);
  74937. + }
  74938. + if (dwc_otg_module_params.opt != -1) {
  74939. + retval +=
  74940. + dwc_otg_set_param_opt(core_if, dwc_otg_module_params.opt);
  74941. + }
  74942. + if (dwc_otg_module_params.dma_burst_size != -1) {
  74943. + retval +=
  74944. + dwc_otg_set_param_dma_burst_size(core_if,
  74945. + dwc_otg_module_params.
  74946. + dma_burst_size);
  74947. + }
  74948. + if (dwc_otg_module_params.host_support_fs_ls_low_power != -1) {
  74949. + retval +=
  74950. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  74951. + dwc_otg_module_params.
  74952. + host_support_fs_ls_low_power);
  74953. + }
  74954. + if (dwc_otg_module_params.enable_dynamic_fifo != -1) {
  74955. + retval +=
  74956. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  74957. + dwc_otg_module_params.
  74958. + enable_dynamic_fifo);
  74959. + }
  74960. + if (dwc_otg_module_params.data_fifo_size != -1) {
  74961. + retval +=
  74962. + dwc_otg_set_param_data_fifo_size(core_if,
  74963. + dwc_otg_module_params.
  74964. + data_fifo_size);
  74965. + }
  74966. + if (dwc_otg_module_params.dev_rx_fifo_size != -1) {
  74967. + retval +=
  74968. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  74969. + dwc_otg_module_params.
  74970. + dev_rx_fifo_size);
  74971. + }
  74972. + if (dwc_otg_module_params.dev_nperio_tx_fifo_size != -1) {
  74973. + retval +=
  74974. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  74975. + dwc_otg_module_params.
  74976. + dev_nperio_tx_fifo_size);
  74977. + }
  74978. + if (dwc_otg_module_params.host_rx_fifo_size != -1) {
  74979. + retval +=
  74980. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  74981. + dwc_otg_module_params.host_rx_fifo_size);
  74982. + }
  74983. + if (dwc_otg_module_params.host_nperio_tx_fifo_size != -1) {
  74984. + retval +=
  74985. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  74986. + dwc_otg_module_params.
  74987. + host_nperio_tx_fifo_size);
  74988. + }
  74989. + if (dwc_otg_module_params.host_perio_tx_fifo_size != -1) {
  74990. + retval +=
  74991. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  74992. + dwc_otg_module_params.
  74993. + host_perio_tx_fifo_size);
  74994. + }
  74995. + if (dwc_otg_module_params.max_transfer_size != -1) {
  74996. + retval +=
  74997. + dwc_otg_set_param_max_transfer_size(core_if,
  74998. + dwc_otg_module_params.
  74999. + max_transfer_size);
  75000. + }
  75001. + if (dwc_otg_module_params.max_packet_count != -1) {
  75002. + retval +=
  75003. + dwc_otg_set_param_max_packet_count(core_if,
  75004. + dwc_otg_module_params.
  75005. + max_packet_count);
  75006. + }
  75007. + if (dwc_otg_module_params.host_channels != -1) {
  75008. + retval +=
  75009. + dwc_otg_set_param_host_channels(core_if,
  75010. + dwc_otg_module_params.
  75011. + host_channels);
  75012. + }
  75013. + if (dwc_otg_module_params.dev_endpoints != -1) {
  75014. + retval +=
  75015. + dwc_otg_set_param_dev_endpoints(core_if,
  75016. + dwc_otg_module_params.
  75017. + dev_endpoints);
  75018. + }
  75019. + if (dwc_otg_module_params.phy_type != -1) {
  75020. + retval +=
  75021. + dwc_otg_set_param_phy_type(core_if,
  75022. + dwc_otg_module_params.phy_type);
  75023. + }
  75024. + if (dwc_otg_module_params.speed != -1) {
  75025. + retval +=
  75026. + dwc_otg_set_param_speed(core_if,
  75027. + dwc_otg_module_params.speed);
  75028. + }
  75029. + if (dwc_otg_module_params.host_ls_low_power_phy_clk != -1) {
  75030. + retval +=
  75031. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  75032. + dwc_otg_module_params.
  75033. + host_ls_low_power_phy_clk);
  75034. + }
  75035. + if (dwc_otg_module_params.phy_ulpi_ddr != -1) {
  75036. + retval +=
  75037. + dwc_otg_set_param_phy_ulpi_ddr(core_if,
  75038. + dwc_otg_module_params.
  75039. + phy_ulpi_ddr);
  75040. + }
  75041. + if (dwc_otg_module_params.phy_ulpi_ext_vbus != -1) {
  75042. + retval +=
  75043. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  75044. + dwc_otg_module_params.
  75045. + phy_ulpi_ext_vbus);
  75046. + }
  75047. + if (dwc_otg_module_params.phy_utmi_width != -1) {
  75048. + retval +=
  75049. + dwc_otg_set_param_phy_utmi_width(core_if,
  75050. + dwc_otg_module_params.
  75051. + phy_utmi_width);
  75052. + }
  75053. + if (dwc_otg_module_params.ulpi_fs_ls != -1) {
  75054. + retval +=
  75055. + dwc_otg_set_param_ulpi_fs_ls(core_if,
  75056. + dwc_otg_module_params.ulpi_fs_ls);
  75057. + }
  75058. + if (dwc_otg_module_params.ts_dline != -1) {
  75059. + retval +=
  75060. + dwc_otg_set_param_ts_dline(core_if,
  75061. + dwc_otg_module_params.ts_dline);
  75062. + }
  75063. + if (dwc_otg_module_params.i2c_enable != -1) {
  75064. + retval +=
  75065. + dwc_otg_set_param_i2c_enable(core_if,
  75066. + dwc_otg_module_params.
  75067. + i2c_enable);
  75068. + }
  75069. + if (dwc_otg_module_params.en_multiple_tx_fifo != -1) {
  75070. + retval +=
  75071. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  75072. + dwc_otg_module_params.
  75073. + en_multiple_tx_fifo);
  75074. + }
  75075. + for (i = 0; i < 15; i++) {
  75076. + if (dwc_otg_module_params.dev_perio_tx_fifo_size[i] != -1) {
  75077. + retval +=
  75078. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  75079. + dwc_otg_module_params.
  75080. + dev_perio_tx_fifo_size
  75081. + [i], i);
  75082. + }
  75083. + }
  75084. +
  75085. + for (i = 0; i < 15; i++) {
  75086. + if (dwc_otg_module_params.dev_tx_fifo_size[i] != -1) {
  75087. + retval += dwc_otg_set_param_dev_tx_fifo_size(core_if,
  75088. + dwc_otg_module_params.
  75089. + dev_tx_fifo_size
  75090. + [i], i);
  75091. + }
  75092. + }
  75093. + if (dwc_otg_module_params.thr_ctl != -1) {
  75094. + retval +=
  75095. + dwc_otg_set_param_thr_ctl(core_if,
  75096. + dwc_otg_module_params.thr_ctl);
  75097. + }
  75098. + if (dwc_otg_module_params.mpi_enable != -1) {
  75099. + retval +=
  75100. + dwc_otg_set_param_mpi_enable(core_if,
  75101. + dwc_otg_module_params.
  75102. + mpi_enable);
  75103. + }
  75104. + if (dwc_otg_module_params.pti_enable != -1) {
  75105. + retval +=
  75106. + dwc_otg_set_param_pti_enable(core_if,
  75107. + dwc_otg_module_params.
  75108. + pti_enable);
  75109. + }
  75110. + if (dwc_otg_module_params.lpm_enable != -1) {
  75111. + retval +=
  75112. + dwc_otg_set_param_lpm_enable(core_if,
  75113. + dwc_otg_module_params.
  75114. + lpm_enable);
  75115. + }
  75116. + if (dwc_otg_module_params.ic_usb_cap != -1) {
  75117. + retval +=
  75118. + dwc_otg_set_param_ic_usb_cap(core_if,
  75119. + dwc_otg_module_params.
  75120. + ic_usb_cap);
  75121. + }
  75122. + if (dwc_otg_module_params.tx_thr_length != -1) {
  75123. + retval +=
  75124. + dwc_otg_set_param_tx_thr_length(core_if,
  75125. + dwc_otg_module_params.tx_thr_length);
  75126. + }
  75127. + if (dwc_otg_module_params.rx_thr_length != -1) {
  75128. + retval +=
  75129. + dwc_otg_set_param_rx_thr_length(core_if,
  75130. + dwc_otg_module_params.
  75131. + rx_thr_length);
  75132. + }
  75133. + if (dwc_otg_module_params.ahb_thr_ratio != -1) {
  75134. + retval +=
  75135. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  75136. + dwc_otg_module_params.ahb_thr_ratio);
  75137. + }
  75138. + if (dwc_otg_module_params.power_down != -1) {
  75139. + retval +=
  75140. + dwc_otg_set_param_power_down(core_if,
  75141. + dwc_otg_module_params.power_down);
  75142. + }
  75143. + if (dwc_otg_module_params.reload_ctl != -1) {
  75144. + retval +=
  75145. + dwc_otg_set_param_reload_ctl(core_if,
  75146. + dwc_otg_module_params.reload_ctl);
  75147. + }
  75148. +
  75149. + if (dwc_otg_module_params.dev_out_nak != -1) {
  75150. + retval +=
  75151. + dwc_otg_set_param_dev_out_nak(core_if,
  75152. + dwc_otg_module_params.dev_out_nak);
  75153. + }
  75154. +
  75155. + if (dwc_otg_module_params.cont_on_bna != -1) {
  75156. + retval +=
  75157. + dwc_otg_set_param_cont_on_bna(core_if,
  75158. + dwc_otg_module_params.cont_on_bna);
  75159. + }
  75160. +
  75161. + if (dwc_otg_module_params.ahb_single != -1) {
  75162. + retval +=
  75163. + dwc_otg_set_param_ahb_single(core_if,
  75164. + dwc_otg_module_params.ahb_single);
  75165. + }
  75166. +
  75167. + if (dwc_otg_module_params.otg_ver != -1) {
  75168. + retval +=
  75169. + dwc_otg_set_param_otg_ver(core_if,
  75170. + dwc_otg_module_params.otg_ver);
  75171. + }
  75172. + if (dwc_otg_module_params.adp_enable != -1) {
  75173. + retval +=
  75174. + dwc_otg_set_param_adp_enable(core_if,
  75175. + dwc_otg_module_params.
  75176. + adp_enable);
  75177. + }
  75178. + return retval;
  75179. +}
  75180. +
  75181. +/**
  75182. + * This function is the top level interrupt handler for the Common
  75183. + * (Device and host modes) interrupts.
  75184. + */
  75185. +static irqreturn_t dwc_otg_common_irq(int irq, void *dev)
  75186. +{
  75187. + int32_t retval = IRQ_NONE;
  75188. +
  75189. + retval = dwc_otg_handle_common_intr(dev);
  75190. + if (retval != 0) {
  75191. + S3C2410X_CLEAR_EINTPEND();
  75192. + }
  75193. + return IRQ_RETVAL(retval);
  75194. +}
  75195. +
  75196. +/**
  75197. + * This function is called when a lm_device is unregistered with the
  75198. + * dwc_otg_driver. This happens, for example, when the rmmod command is
  75199. + * executed. The device may or may not be electrically present. If it is
  75200. + * present, the driver stops device processing. Any resources used on behalf
  75201. + * of this device are freed.
  75202. + *
  75203. + * @param _dev
  75204. + */
  75205. +#ifdef LM_INTERFACE
  75206. +#define REM_RETVAL(n)
  75207. +static void dwc_otg_driver_remove( struct lm_device *_dev )
  75208. +{ dwc_otg_device_t *otg_dev = lm_get_drvdata(_dev);
  75209. +#elif defined(PCI_INTERFACE)
  75210. +#define REM_RETVAL(n)
  75211. +static void dwc_otg_driver_remove( struct pci_dev *_dev )
  75212. +{ dwc_otg_device_t *otg_dev = pci_get_drvdata(_dev);
  75213. +#elif defined(PLATFORM_INTERFACE)
  75214. +#define REM_RETVAL(n) n
  75215. +static int dwc_otg_driver_remove( struct platform_device *_dev )
  75216. +{ dwc_otg_device_t *otg_dev = platform_get_drvdata(_dev);
  75217. +#endif
  75218. +
  75219. + DWC_DEBUGPL(DBG_ANY, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  75220. +
  75221. + if (!otg_dev) {
  75222. + /* Memory allocation for the dwc_otg_device failed. */
  75223. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  75224. + return REM_RETVAL(-ENOMEM);
  75225. + }
  75226. +#ifndef DWC_DEVICE_ONLY
  75227. + if (otg_dev->hcd) {
  75228. + hcd_remove(_dev);
  75229. + } else {
  75230. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  75231. + return REM_RETVAL(-EINVAL);
  75232. + }
  75233. +#endif
  75234. +
  75235. +#ifndef DWC_HOST_ONLY
  75236. + if (otg_dev->pcd) {
  75237. + pcd_remove(_dev);
  75238. + } else {
  75239. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->pcd NULL!\n", __func__);
  75240. + return REM_RETVAL(-EINVAL);
  75241. + }
  75242. +#endif
  75243. + /*
  75244. + * Free the IRQ
  75245. + */
  75246. + if (otg_dev->common_irq_installed) {
  75247. +#ifdef PLATFORM_INTERFACE
  75248. + free_irq(platform_get_irq(_dev, 0), otg_dev);
  75249. +#else
  75250. + free_irq(_dev->irq, otg_dev);
  75251. +#endif
  75252. + } else {
  75253. + DWC_DEBUGPL(DBG_ANY, "%s: There is no installed irq!\n", __func__);
  75254. + return REM_RETVAL(-ENXIO);
  75255. + }
  75256. +
  75257. + if (otg_dev->core_if) {
  75258. + dwc_otg_cil_remove(otg_dev->core_if);
  75259. + } else {
  75260. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->core_if NULL!\n", __func__);
  75261. + return REM_RETVAL(-ENXIO);
  75262. + }
  75263. +
  75264. + /*
  75265. + * Remove the device attributes
  75266. + */
  75267. + dwc_otg_attr_remove(_dev);
  75268. +
  75269. + /*
  75270. + * Return the memory.
  75271. + */
  75272. + if (otg_dev->os_dep.base) {
  75273. + iounmap(otg_dev->os_dep.base);
  75274. + }
  75275. + DWC_FREE(otg_dev);
  75276. +
  75277. + /*
  75278. + * Clear the drvdata pointer.
  75279. + */
  75280. +#ifdef LM_INTERFACE
  75281. + lm_set_drvdata(_dev, 0);
  75282. +#elif defined(PCI_INTERFACE)
  75283. + release_mem_region(otg_dev->os_dep.rsrc_start,
  75284. + otg_dev->os_dep.rsrc_len);
  75285. + pci_set_drvdata(_dev, 0);
  75286. +#elif defined(PLATFORM_INTERFACE)
  75287. + platform_set_drvdata(_dev, 0);
  75288. +#endif
  75289. + return REM_RETVAL(0);
  75290. +}
  75291. +
  75292. +/**
  75293. + * This function is called when an lm_device is bound to a
  75294. + * dwc_otg_driver. It creates the driver components required to
  75295. + * control the device (CIL, HCD, and PCD) and it initializes the
  75296. + * device. The driver components are stored in a dwc_otg_device
  75297. + * structure. A reference to the dwc_otg_device is saved in the
  75298. + * lm_device. This allows the driver to access the dwc_otg_device
  75299. + * structure on subsequent calls to driver methods for this device.
  75300. + *
  75301. + * @param _dev Bus device
  75302. + */
  75303. +static int dwc_otg_driver_probe(
  75304. +#ifdef LM_INTERFACE
  75305. + struct lm_device *_dev
  75306. +#elif defined(PCI_INTERFACE)
  75307. + struct pci_dev *_dev,
  75308. + const struct pci_device_id *id
  75309. +#elif defined(PLATFORM_INTERFACE)
  75310. + struct platform_device *_dev
  75311. +#endif
  75312. + )
  75313. +{
  75314. + int retval = 0;
  75315. + dwc_otg_device_t *dwc_otg_device;
  75316. + int devirq;
  75317. +
  75318. + dev_dbg(&_dev->dev, "dwc_otg_driver_probe(%p)\n", _dev);
  75319. +#ifdef LM_INTERFACE
  75320. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)_dev->resource.start);
  75321. +#elif defined(PCI_INTERFACE)
  75322. + if (!id) {
  75323. + DWC_ERROR("Invalid pci_device_id %p", id);
  75324. + return -EINVAL;
  75325. + }
  75326. +
  75327. + if (!_dev || (pci_enable_device(_dev) < 0)) {
  75328. + DWC_ERROR("Invalid pci_device %p", _dev);
  75329. + return -ENODEV;
  75330. + }
  75331. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)pci_resource_start(_dev,0));
  75332. + /* other stuff needed as well? */
  75333. +
  75334. +#elif defined(PLATFORM_INTERFACE)
  75335. + dev_dbg(&_dev->dev, "start=0x%08x (len 0x%x)\n",
  75336. + (unsigned)_dev->resource->start,
  75337. + (unsigned)(_dev->resource->end - _dev->resource->start));
  75338. +#endif
  75339. +
  75340. + dwc_otg_device = DWC_ALLOC(sizeof(dwc_otg_device_t));
  75341. +
  75342. + if (!dwc_otg_device) {
  75343. + dev_err(&_dev->dev, "kmalloc of dwc_otg_device failed\n");
  75344. + return -ENOMEM;
  75345. + }
  75346. +
  75347. + memset(dwc_otg_device, 0, sizeof(*dwc_otg_device));
  75348. + dwc_otg_device->os_dep.reg_offset = 0xFFFFFFFF;
  75349. +
  75350. + /*
  75351. + * Map the DWC_otg Core memory into virtual address space.
  75352. + */
  75353. +#ifdef LM_INTERFACE
  75354. + dwc_otg_device->os_dep.base = ioremap(_dev->resource.start, SZ_256K);
  75355. +
  75356. + if (!dwc_otg_device->os_dep.base) {
  75357. + dev_err(&_dev->dev, "ioremap() failed\n");
  75358. + DWC_FREE(dwc_otg_device);
  75359. + return -ENOMEM;
  75360. + }
  75361. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  75362. + (unsigned)dwc_otg_device->os_dep.base);
  75363. +#elif defined(PCI_INTERFACE)
  75364. + _dev->current_state = PCI_D0;
  75365. + _dev->dev.power.power_state = PMSG_ON;
  75366. +
  75367. + if (!_dev->irq) {
  75368. + DWC_ERROR("Found HC with no IRQ. Check BIOS/PCI %s setup!",
  75369. + pci_name(_dev));
  75370. + iounmap(dwc_otg_device->os_dep.base);
  75371. + DWC_FREE(dwc_otg_device);
  75372. + return -ENODEV;
  75373. + }
  75374. +
  75375. + dwc_otg_device->os_dep.rsrc_start = pci_resource_start(_dev, 0);
  75376. + dwc_otg_device->os_dep.rsrc_len = pci_resource_len(_dev, 0);
  75377. + DWC_DEBUGPL(DBG_ANY, "PCI resource: start=%08x, len=%08x\n",
  75378. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  75379. + (unsigned)dwc_otg_device->os_dep.rsrc_len);
  75380. + if (!request_mem_region
  75381. + (dwc_otg_device->os_dep.rsrc_start, dwc_otg_device->os_dep.rsrc_len,
  75382. + "dwc_otg")) {
  75383. + dev_dbg(&_dev->dev, "error requesting memory\n");
  75384. + iounmap(dwc_otg_device->os_dep.base);
  75385. + DWC_FREE(dwc_otg_device);
  75386. + return -EFAULT;
  75387. + }
  75388. +
  75389. + dwc_otg_device->os_dep.base =
  75390. + ioremap_nocache(dwc_otg_device->os_dep.rsrc_start,
  75391. + dwc_otg_device->os_dep.rsrc_len);
  75392. + if (dwc_otg_device->os_dep.base == NULL) {
  75393. + dev_dbg(&_dev->dev, "error mapping memory\n");
  75394. + release_mem_region(dwc_otg_device->os_dep.rsrc_start,
  75395. + dwc_otg_device->os_dep.rsrc_len);
  75396. + iounmap(dwc_otg_device->os_dep.base);
  75397. + DWC_FREE(dwc_otg_device);
  75398. + return -EFAULT;
  75399. + }
  75400. + dev_dbg(&_dev->dev, "base=0x%p (before adjust) \n",
  75401. + dwc_otg_device->os_dep.base);
  75402. + dwc_otg_device->os_dep.base = (char *)dwc_otg_device->os_dep.base;
  75403. + dev_dbg(&_dev->dev, "base=0x%p (after adjust) \n",
  75404. + dwc_otg_device->os_dep.base);
  75405. + dev_dbg(&_dev->dev, "%s: mapped PA 0x%x to VA 0x%p\n", __func__,
  75406. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  75407. + dwc_otg_device->os_dep.base);
  75408. +
  75409. + pci_set_master(_dev);
  75410. + pci_set_drvdata(_dev, dwc_otg_device);
  75411. +#elif defined(PLATFORM_INTERFACE)
  75412. + DWC_DEBUGPL(DBG_ANY,"Platform resource: start=%08x, len=%08x\n",
  75413. + _dev->resource->start,
  75414. + _dev->resource->end - _dev->resource->start + 1);
  75415. +#if 1
  75416. + if (!request_mem_region(_dev->resource[0].start,
  75417. + _dev->resource[0].end - _dev->resource[0].start + 1,
  75418. + "dwc_otg")) {
  75419. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  75420. + retval = -EFAULT;
  75421. + goto fail;
  75422. + }
  75423. +
  75424. + dwc_otg_device->os_dep.base = ioremap_nocache(_dev->resource[0].start,
  75425. + _dev->resource[0].end -
  75426. + _dev->resource[0].start+1);
  75427. + if (fiq_enable)
  75428. + {
  75429. + if (!request_mem_region(_dev->resource[1].start,
  75430. + _dev->resource[1].end - _dev->resource[1].start + 1,
  75431. + "dwc_otg")) {
  75432. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  75433. + retval = -EFAULT;
  75434. + goto fail;
  75435. + }
  75436. +
  75437. + dwc_otg_device->os_dep.mphi_base = ioremap_nocache(_dev->resource[1].start,
  75438. + _dev->resource[1].end -
  75439. + _dev->resource[1].start + 1);
  75440. + }
  75441. +
  75442. +#else
  75443. + {
  75444. + struct map_desc desc = {
  75445. + .virtual = IO_ADDRESS((unsigned)_dev->resource->start),
  75446. + .pfn = __phys_to_pfn((unsigned)_dev->resource->start),
  75447. + .length = SZ_128K,
  75448. + .type = MT_DEVICE
  75449. + };
  75450. + iotable_init(&desc, 1);
  75451. + dwc_otg_device->os_dep.base = (void *)desc.virtual;
  75452. + }
  75453. +#endif
  75454. + if (!dwc_otg_device->os_dep.base) {
  75455. + dev_err(&_dev->dev, "ioremap() failed\n");
  75456. + retval = -ENOMEM;
  75457. + goto fail;
  75458. + }
  75459. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  75460. + (unsigned)dwc_otg_device->os_dep.base);
  75461. +#endif
  75462. +
  75463. + /*
  75464. + * Initialize driver data to point to the global DWC_otg
  75465. + * Device structure.
  75466. + */
  75467. +#ifdef LM_INTERFACE
  75468. + lm_set_drvdata(_dev, dwc_otg_device);
  75469. +#elif defined(PLATFORM_INTERFACE)
  75470. + platform_set_drvdata(_dev, dwc_otg_device);
  75471. +#endif
  75472. + dev_dbg(&_dev->dev, "dwc_otg_device=0x%p\n", dwc_otg_device);
  75473. +
  75474. + dwc_otg_device->core_if = dwc_otg_cil_init(dwc_otg_device->os_dep.base);
  75475. + DWC_DEBUGPL(DBG_HCDV, "probe of device %p given core_if %p\n",
  75476. + dwc_otg_device, dwc_otg_device->core_if);//GRAYG
  75477. +
  75478. + if (!dwc_otg_device->core_if) {
  75479. + dev_err(&_dev->dev, "CIL initialization failed!\n");
  75480. + retval = -ENOMEM;
  75481. + goto fail;
  75482. + }
  75483. +
  75484. + dev_dbg(&_dev->dev, "Calling get_gsnpsid\n");
  75485. + /*
  75486. + * Attempt to ensure this device is really a DWC_otg Controller.
  75487. + * Read and verify the SNPSID register contents. The value should be
  75488. + * 0x45F42XXX or 0x45F42XXX, which corresponds to either "OT2" or "OTG3",
  75489. + * as in "OTG version 2.XX" or "OTG version 3.XX".
  75490. + */
  75491. +
  75492. + if (((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F542000) &&
  75493. + ((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F543000)) {
  75494. + dev_err(&_dev->dev, "Bad value for SNPSID: 0x%08x\n",
  75495. + dwc_otg_get_gsnpsid(dwc_otg_device->core_if));
  75496. + retval = -EINVAL;
  75497. + goto fail;
  75498. + }
  75499. +
  75500. + /*
  75501. + * Validate parameter values.
  75502. + */
  75503. + dev_dbg(&_dev->dev, "Calling set_parameters\n");
  75504. + if (set_parameters(dwc_otg_device->core_if)) {
  75505. + retval = -EINVAL;
  75506. + goto fail;
  75507. + }
  75508. +
  75509. + /*
  75510. + * Create Device Attributes in sysfs
  75511. + */
  75512. + dev_dbg(&_dev->dev, "Calling attr_create\n");
  75513. + dwc_otg_attr_create(_dev);
  75514. +
  75515. + /*
  75516. + * Disable the global interrupt until all the interrupt
  75517. + * handlers are installed.
  75518. + */
  75519. + dev_dbg(&_dev->dev, "Calling disable_global_interrupts\n");
  75520. + dwc_otg_disable_global_interrupts(dwc_otg_device->core_if);
  75521. +
  75522. + /*
  75523. + * Install the interrupt handler for the common interrupts before
  75524. + * enabling common interrupts in core_init below.
  75525. + */
  75526. +
  75527. +#if defined(PLATFORM_INTERFACE)
  75528. + devirq = platform_get_irq(_dev, fiq_enable ? 0 : 1);
  75529. +#else
  75530. + devirq = _dev->irq;
  75531. +#endif
  75532. + DWC_DEBUGPL(DBG_CIL, "registering (common) handler for irq%d\n",
  75533. + devirq);
  75534. + dev_dbg(&_dev->dev, "Calling request_irq(%d)\n", devirq);
  75535. + retval = request_irq(devirq, dwc_otg_common_irq,
  75536. + IRQF_SHARED,
  75537. + "dwc_otg", dwc_otg_device);
  75538. + if (retval) {
  75539. + DWC_ERROR("request of irq%d failed\n", devirq);
  75540. + retval = -EBUSY;
  75541. + goto fail;
  75542. + } else {
  75543. + dwc_otg_device->common_irq_installed = 1;
  75544. + }
  75545. +
  75546. +#ifndef IRQF_TRIGGER_LOW
  75547. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  75548. + dev_dbg(&_dev->dev, "Calling set_irq_type\n");
  75549. + set_irq_type(devirq,
  75550. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  75551. + IRQT_LOW
  75552. +#else
  75553. + IRQ_TYPE_LEVEL_LOW
  75554. +#endif
  75555. + );
  75556. +#endif
  75557. +#endif /*IRQF_TRIGGER_LOW*/
  75558. +
  75559. + /*
  75560. + * Initialize the DWC_otg core.
  75561. + */
  75562. + dev_dbg(&_dev->dev, "Calling dwc_otg_core_init\n");
  75563. + dwc_otg_core_init(dwc_otg_device->core_if);
  75564. +
  75565. +#ifndef DWC_HOST_ONLY
  75566. + /*
  75567. + * Initialize the PCD
  75568. + */
  75569. + dev_dbg(&_dev->dev, "Calling pcd_init\n");
  75570. + retval = pcd_init(_dev);
  75571. + if (retval != 0) {
  75572. + DWC_ERROR("pcd_init failed\n");
  75573. + dwc_otg_device->pcd = NULL;
  75574. + goto fail;
  75575. + }
  75576. +#endif
  75577. +#ifndef DWC_DEVICE_ONLY
  75578. + /*
  75579. + * Initialize the HCD
  75580. + */
  75581. + dev_dbg(&_dev->dev, "Calling hcd_init\n");
  75582. + retval = hcd_init(_dev);
  75583. + if (retval != 0) {
  75584. + DWC_ERROR("hcd_init failed\n");
  75585. + dwc_otg_device->hcd = NULL;
  75586. + goto fail;
  75587. + }
  75588. +#endif
  75589. + /* Recover from drvdata having been overwritten by hcd_init() */
  75590. +#ifdef LM_INTERFACE
  75591. + lm_set_drvdata(_dev, dwc_otg_device);
  75592. +#elif defined(PLATFORM_INTERFACE)
  75593. + platform_set_drvdata(_dev, dwc_otg_device);
  75594. +#elif defined(PCI_INTERFACE)
  75595. + pci_set_drvdata(_dev, dwc_otg_device);
  75596. + dwc_otg_device->os_dep.pcidev = _dev;
  75597. +#endif
  75598. +
  75599. + /*
  75600. + * Enable the global interrupt after all the interrupt
  75601. + * handlers are installed if there is no ADP support else
  75602. + * perform initial actions required for Internal ADP logic.
  75603. + */
  75604. + if (!dwc_otg_get_param_adp_enable(dwc_otg_device->core_if)) {
  75605. + dev_dbg(&_dev->dev, "Calling enable_global_interrupts\n");
  75606. + dwc_otg_enable_global_interrupts(dwc_otg_device->core_if);
  75607. + dev_dbg(&_dev->dev, "Done\n");
  75608. + } else
  75609. + dwc_otg_adp_start(dwc_otg_device->core_if,
  75610. + dwc_otg_is_host_mode(dwc_otg_device->core_if));
  75611. +
  75612. + return 0;
  75613. +
  75614. +fail:
  75615. + dwc_otg_driver_remove(_dev);
  75616. + return retval;
  75617. +}
  75618. +
  75619. +/**
  75620. + * This structure defines the methods to be called by a bus driver
  75621. + * during the lifecycle of a device on that bus. Both drivers and
  75622. + * devices are registered with a bus driver. The bus driver matches
  75623. + * devices to drivers based on information in the device and driver
  75624. + * structures.
  75625. + *
  75626. + * The probe function is called when the bus driver matches a device
  75627. + * to this driver. The remove function is called when a device is
  75628. + * unregistered with the bus driver.
  75629. + */
  75630. +#ifdef LM_INTERFACE
  75631. +static struct lm_driver dwc_otg_driver = {
  75632. + .drv = {.name = (char *)dwc_driver_name,},
  75633. + .probe = dwc_otg_driver_probe,
  75634. + .remove = dwc_otg_driver_remove,
  75635. + // 'suspend' and 'resume' absent
  75636. +};
  75637. +#elif defined(PCI_INTERFACE)
  75638. +static const struct pci_device_id pci_ids[] = { {
  75639. + PCI_DEVICE(0x16c3, 0xabcd),
  75640. + .driver_data =
  75641. + (unsigned long)0xdeadbeef,
  75642. + }, { /* end: all zeroes */ }
  75643. +};
  75644. +
  75645. +MODULE_DEVICE_TABLE(pci, pci_ids);
  75646. +
  75647. +/* pci driver glue; this is a "new style" PCI driver module */
  75648. +static struct pci_driver dwc_otg_driver = {
  75649. + .name = "dwc_otg",
  75650. + .id_table = pci_ids,
  75651. +
  75652. + .probe = dwc_otg_driver_probe,
  75653. + .remove = dwc_otg_driver_remove,
  75654. +
  75655. + .driver = {
  75656. + .name = (char *)dwc_driver_name,
  75657. + },
  75658. +};
  75659. +#elif defined(PLATFORM_INTERFACE)
  75660. +static struct platform_device_id platform_ids[] = {
  75661. + {
  75662. + .name = "bcm2708_usb",
  75663. + .driver_data = (kernel_ulong_t) 0xdeadbeef,
  75664. + },
  75665. + { /* end: all zeroes */ }
  75666. +};
  75667. +MODULE_DEVICE_TABLE(platform, platform_ids);
  75668. +
  75669. +static struct platform_driver dwc_otg_driver = {
  75670. + .driver = {
  75671. + .name = (char *)dwc_driver_name,
  75672. + },
  75673. + .id_table = platform_ids,
  75674. +
  75675. + .probe = dwc_otg_driver_probe,
  75676. + .remove = dwc_otg_driver_remove,
  75677. + // no 'shutdown', 'suspend', 'resume', 'suspend_late' or 'resume_early'
  75678. +};
  75679. +#endif
  75680. +
  75681. +/**
  75682. + * This function is called when the dwc_otg_driver is installed with the
  75683. + * insmod command. It registers the dwc_otg_driver structure with the
  75684. + * appropriate bus driver. This will cause the dwc_otg_driver_probe function
  75685. + * to be called. In addition, the bus driver will automatically expose
  75686. + * attributes defined for the device and driver in the special sysfs file
  75687. + * system.
  75688. + *
  75689. + * @return
  75690. + */
  75691. +static int __init dwc_otg_driver_init(void)
  75692. +{
  75693. + int retval = 0;
  75694. + int error;
  75695. + struct device_driver *drv;
  75696. +
  75697. + if(fiq_fsm_enable && !fiq_enable) {
  75698. + printk(KERN_WARNING "dwc_otg: fiq_fsm_enable was set without fiq_enable! Correcting.\n");
  75699. + fiq_enable = 1;
  75700. + }
  75701. +
  75702. + printk(KERN_INFO "%s: version %s (%s bus)\n", dwc_driver_name,
  75703. + DWC_DRIVER_VERSION,
  75704. +#ifdef LM_INTERFACE
  75705. + "logicmodule");
  75706. + retval = lm_driver_register(&dwc_otg_driver);
  75707. + drv = &dwc_otg_driver.drv;
  75708. +#elif defined(PCI_INTERFACE)
  75709. + "pci");
  75710. + retval = pci_register_driver(&dwc_otg_driver);
  75711. + drv = &dwc_otg_driver.driver;
  75712. +#elif defined(PLATFORM_INTERFACE)
  75713. + "platform");
  75714. + retval = platform_driver_register(&dwc_otg_driver);
  75715. + drv = &dwc_otg_driver.driver;
  75716. +#endif
  75717. + if (retval < 0) {
  75718. + printk(KERN_ERR "%s retval=%d\n", __func__, retval);
  75719. + return retval;
  75720. + }
  75721. + printk(KERN_DEBUG "dwc_otg: FIQ %s\n", fiq_enable ? "enabled":"disabled");
  75722. + printk(KERN_DEBUG "dwc_otg: NAK holdoff %s\n", nak_holdoff ? "enabled":"disabled");
  75723. + printk(KERN_DEBUG "dwc_otg: FIQ split-transaction FSM %s\n", fiq_fsm_enable ? "enabled":"disabled");
  75724. +
  75725. + error = driver_create_file(drv, &driver_attr_version);
  75726. +#ifdef DEBUG
  75727. + error = driver_create_file(drv, &driver_attr_debuglevel);
  75728. +#endif
  75729. + return retval;
  75730. +}
  75731. +
  75732. +module_init(dwc_otg_driver_init);
  75733. +
  75734. +/**
  75735. + * This function is called when the driver is removed from the kernel
  75736. + * with the rmmod command. The driver unregisters itself with its bus
  75737. + * driver.
  75738. + *
  75739. + */
  75740. +static void __exit dwc_otg_driver_cleanup(void)
  75741. +{
  75742. + printk(KERN_DEBUG "dwc_otg_driver_cleanup()\n");
  75743. +
  75744. +#ifdef LM_INTERFACE
  75745. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_debuglevel);
  75746. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_version);
  75747. + lm_driver_unregister(&dwc_otg_driver);
  75748. +#elif defined(PCI_INTERFACE)
  75749. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  75750. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  75751. + pci_unregister_driver(&dwc_otg_driver);
  75752. +#elif defined(PLATFORM_INTERFACE)
  75753. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  75754. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  75755. + platform_driver_unregister(&dwc_otg_driver);
  75756. +#endif
  75757. +
  75758. + printk(KERN_INFO "%s module removed\n", dwc_driver_name);
  75759. +}
  75760. +
  75761. +module_exit(dwc_otg_driver_cleanup);
  75762. +
  75763. +MODULE_DESCRIPTION(DWC_DRIVER_DESC);
  75764. +MODULE_AUTHOR("Synopsys Inc.");
  75765. +MODULE_LICENSE("GPL");
  75766. +
  75767. +module_param_named(otg_cap, dwc_otg_module_params.otg_cap, int, 0444);
  75768. +MODULE_PARM_DESC(otg_cap, "OTG Capabilities 0=HNP&SRP 1=SRP Only 2=None");
  75769. +module_param_named(opt, dwc_otg_module_params.opt, int, 0444);
  75770. +MODULE_PARM_DESC(opt, "OPT Mode");
  75771. +module_param_named(dma_enable, dwc_otg_module_params.dma_enable, int, 0444);
  75772. +MODULE_PARM_DESC(dma_enable, "DMA Mode 0=Slave 1=DMA enabled");
  75773. +
  75774. +module_param_named(dma_desc_enable, dwc_otg_module_params.dma_desc_enable, int,
  75775. + 0444);
  75776. +MODULE_PARM_DESC(dma_desc_enable,
  75777. + "DMA Desc Mode 0=Address DMA 1=DMA Descriptor enabled");
  75778. +
  75779. +module_param_named(dma_burst_size, dwc_otg_module_params.dma_burst_size, int,
  75780. + 0444);
  75781. +MODULE_PARM_DESC(dma_burst_size,
  75782. + "DMA Burst Size 1, 4, 8, 16, 32, 64, 128, 256");
  75783. +module_param_named(speed, dwc_otg_module_params.speed, int, 0444);
  75784. +MODULE_PARM_DESC(speed, "Speed 0=High Speed 1=Full Speed");
  75785. +module_param_named(host_support_fs_ls_low_power,
  75786. + dwc_otg_module_params.host_support_fs_ls_low_power, int,
  75787. + 0444);
  75788. +MODULE_PARM_DESC(host_support_fs_ls_low_power,
  75789. + "Support Low Power w/FS or LS 0=Support 1=Don't Support");
  75790. +module_param_named(host_ls_low_power_phy_clk,
  75791. + dwc_otg_module_params.host_ls_low_power_phy_clk, int, 0444);
  75792. +MODULE_PARM_DESC(host_ls_low_power_phy_clk,
  75793. + "Low Speed Low Power Clock 0=48Mhz 1=6Mhz");
  75794. +module_param_named(enable_dynamic_fifo,
  75795. + dwc_otg_module_params.enable_dynamic_fifo, int, 0444);
  75796. +MODULE_PARM_DESC(enable_dynamic_fifo, "0=cC Setting 1=Allow Dynamic Sizing");
  75797. +module_param_named(data_fifo_size, dwc_otg_module_params.data_fifo_size, int,
  75798. + 0444);
  75799. +MODULE_PARM_DESC(data_fifo_size,
  75800. + "Total number of words in the data FIFO memory 32-32768");
  75801. +module_param_named(dev_rx_fifo_size, dwc_otg_module_params.dev_rx_fifo_size,
  75802. + int, 0444);
  75803. +MODULE_PARM_DESC(dev_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  75804. +module_param_named(dev_nperio_tx_fifo_size,
  75805. + dwc_otg_module_params.dev_nperio_tx_fifo_size, int, 0444);
  75806. +MODULE_PARM_DESC(dev_nperio_tx_fifo_size,
  75807. + "Number of words in the non-periodic Tx FIFO 16-32768");
  75808. +module_param_named(dev_perio_tx_fifo_size_1,
  75809. + dwc_otg_module_params.dev_perio_tx_fifo_size[0], int, 0444);
  75810. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_1,
  75811. + "Number of words in the periodic Tx FIFO 4-768");
  75812. +module_param_named(dev_perio_tx_fifo_size_2,
  75813. + dwc_otg_module_params.dev_perio_tx_fifo_size[1], int, 0444);
  75814. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_2,
  75815. + "Number of words in the periodic Tx FIFO 4-768");
  75816. +module_param_named(dev_perio_tx_fifo_size_3,
  75817. + dwc_otg_module_params.dev_perio_tx_fifo_size[2], int, 0444);
  75818. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_3,
  75819. + "Number of words in the periodic Tx FIFO 4-768");
  75820. +module_param_named(dev_perio_tx_fifo_size_4,
  75821. + dwc_otg_module_params.dev_perio_tx_fifo_size[3], int, 0444);
  75822. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_4,
  75823. + "Number of words in the periodic Tx FIFO 4-768");
  75824. +module_param_named(dev_perio_tx_fifo_size_5,
  75825. + dwc_otg_module_params.dev_perio_tx_fifo_size[4], int, 0444);
  75826. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_5,
  75827. + "Number of words in the periodic Tx FIFO 4-768");
  75828. +module_param_named(dev_perio_tx_fifo_size_6,
  75829. + dwc_otg_module_params.dev_perio_tx_fifo_size[5], int, 0444);
  75830. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_6,
  75831. + "Number of words in the periodic Tx FIFO 4-768");
  75832. +module_param_named(dev_perio_tx_fifo_size_7,
  75833. + dwc_otg_module_params.dev_perio_tx_fifo_size[6], int, 0444);
  75834. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_7,
  75835. + "Number of words in the periodic Tx FIFO 4-768");
  75836. +module_param_named(dev_perio_tx_fifo_size_8,
  75837. + dwc_otg_module_params.dev_perio_tx_fifo_size[7], int, 0444);
  75838. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_8,
  75839. + "Number of words in the periodic Tx FIFO 4-768");
  75840. +module_param_named(dev_perio_tx_fifo_size_9,
  75841. + dwc_otg_module_params.dev_perio_tx_fifo_size[8], int, 0444);
  75842. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_9,
  75843. + "Number of words in the periodic Tx FIFO 4-768");
  75844. +module_param_named(dev_perio_tx_fifo_size_10,
  75845. + dwc_otg_module_params.dev_perio_tx_fifo_size[9], int, 0444);
  75846. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_10,
  75847. + "Number of words in the periodic Tx FIFO 4-768");
  75848. +module_param_named(dev_perio_tx_fifo_size_11,
  75849. + dwc_otg_module_params.dev_perio_tx_fifo_size[10], int, 0444);
  75850. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_11,
  75851. + "Number of words in the periodic Tx FIFO 4-768");
  75852. +module_param_named(dev_perio_tx_fifo_size_12,
  75853. + dwc_otg_module_params.dev_perio_tx_fifo_size[11], int, 0444);
  75854. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_12,
  75855. + "Number of words in the periodic Tx FIFO 4-768");
  75856. +module_param_named(dev_perio_tx_fifo_size_13,
  75857. + dwc_otg_module_params.dev_perio_tx_fifo_size[12], int, 0444);
  75858. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_13,
  75859. + "Number of words in the periodic Tx FIFO 4-768");
  75860. +module_param_named(dev_perio_tx_fifo_size_14,
  75861. + dwc_otg_module_params.dev_perio_tx_fifo_size[13], int, 0444);
  75862. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_14,
  75863. + "Number of words in the periodic Tx FIFO 4-768");
  75864. +module_param_named(dev_perio_tx_fifo_size_15,
  75865. + dwc_otg_module_params.dev_perio_tx_fifo_size[14], int, 0444);
  75866. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_15,
  75867. + "Number of words in the periodic Tx FIFO 4-768");
  75868. +module_param_named(host_rx_fifo_size, dwc_otg_module_params.host_rx_fifo_size,
  75869. + int, 0444);
  75870. +MODULE_PARM_DESC(host_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  75871. +module_param_named(host_nperio_tx_fifo_size,
  75872. + dwc_otg_module_params.host_nperio_tx_fifo_size, int, 0444);
  75873. +MODULE_PARM_DESC(host_nperio_tx_fifo_size,
  75874. + "Number of words in the non-periodic Tx FIFO 16-32768");
  75875. +module_param_named(host_perio_tx_fifo_size,
  75876. + dwc_otg_module_params.host_perio_tx_fifo_size, int, 0444);
  75877. +MODULE_PARM_DESC(host_perio_tx_fifo_size,
  75878. + "Number of words in the host periodic Tx FIFO 16-32768");
  75879. +module_param_named(max_transfer_size, dwc_otg_module_params.max_transfer_size,
  75880. + int, 0444);
  75881. +/** @todo Set the max to 512K, modify checks */
  75882. +MODULE_PARM_DESC(max_transfer_size,
  75883. + "The maximum transfer size supported in bytes 2047-65535");
  75884. +module_param_named(max_packet_count, dwc_otg_module_params.max_packet_count,
  75885. + int, 0444);
  75886. +MODULE_PARM_DESC(max_packet_count,
  75887. + "The maximum number of packets in a transfer 15-511");
  75888. +module_param_named(host_channels, dwc_otg_module_params.host_channels, int,
  75889. + 0444);
  75890. +MODULE_PARM_DESC(host_channels,
  75891. + "The number of host channel registers to use 1-16");
  75892. +module_param_named(dev_endpoints, dwc_otg_module_params.dev_endpoints, int,
  75893. + 0444);
  75894. +MODULE_PARM_DESC(dev_endpoints,
  75895. + "The number of endpoints in addition to EP0 available for device mode 1-15");
  75896. +module_param_named(phy_type, dwc_otg_module_params.phy_type, int, 0444);
  75897. +MODULE_PARM_DESC(phy_type, "0=Reserved 1=UTMI+ 2=ULPI");
  75898. +module_param_named(phy_utmi_width, dwc_otg_module_params.phy_utmi_width, int,
  75899. + 0444);
  75900. +MODULE_PARM_DESC(phy_utmi_width, "Specifies the UTMI+ Data Width 8 or 16 bits");
  75901. +module_param_named(phy_ulpi_ddr, dwc_otg_module_params.phy_ulpi_ddr, int, 0444);
  75902. +MODULE_PARM_DESC(phy_ulpi_ddr,
  75903. + "ULPI at double or single data rate 0=Single 1=Double");
  75904. +module_param_named(phy_ulpi_ext_vbus, dwc_otg_module_params.phy_ulpi_ext_vbus,
  75905. + int, 0444);
  75906. +MODULE_PARM_DESC(phy_ulpi_ext_vbus,
  75907. + "ULPI PHY using internal or external vbus 0=Internal");
  75908. +module_param_named(i2c_enable, dwc_otg_module_params.i2c_enable, int, 0444);
  75909. +MODULE_PARM_DESC(i2c_enable, "FS PHY Interface");
  75910. +module_param_named(ulpi_fs_ls, dwc_otg_module_params.ulpi_fs_ls, int, 0444);
  75911. +MODULE_PARM_DESC(ulpi_fs_ls, "ULPI PHY FS/LS mode only");
  75912. +module_param_named(ts_dline, dwc_otg_module_params.ts_dline, int, 0444);
  75913. +MODULE_PARM_DESC(ts_dline, "Term select Dline pulsing for all PHYs");
  75914. +module_param_named(debug, g_dbg_lvl, int, 0444);
  75915. +MODULE_PARM_DESC(debug, "");
  75916. +
  75917. +module_param_named(en_multiple_tx_fifo,
  75918. + dwc_otg_module_params.en_multiple_tx_fifo, int, 0444);
  75919. +MODULE_PARM_DESC(en_multiple_tx_fifo,
  75920. + "Dedicated Non Periodic Tx FIFOs 0=disabled 1=enabled");
  75921. +module_param_named(dev_tx_fifo_size_1,
  75922. + dwc_otg_module_params.dev_tx_fifo_size[0], int, 0444);
  75923. +MODULE_PARM_DESC(dev_tx_fifo_size_1, "Number of words in the Tx FIFO 4-768");
  75924. +module_param_named(dev_tx_fifo_size_2,
  75925. + dwc_otg_module_params.dev_tx_fifo_size[1], int, 0444);
  75926. +MODULE_PARM_DESC(dev_tx_fifo_size_2, "Number of words in the Tx FIFO 4-768");
  75927. +module_param_named(dev_tx_fifo_size_3,
  75928. + dwc_otg_module_params.dev_tx_fifo_size[2], int, 0444);
  75929. +MODULE_PARM_DESC(dev_tx_fifo_size_3, "Number of words in the Tx FIFO 4-768");
  75930. +module_param_named(dev_tx_fifo_size_4,
  75931. + dwc_otg_module_params.dev_tx_fifo_size[3], int, 0444);
  75932. +MODULE_PARM_DESC(dev_tx_fifo_size_4, "Number of words in the Tx FIFO 4-768");
  75933. +module_param_named(dev_tx_fifo_size_5,
  75934. + dwc_otg_module_params.dev_tx_fifo_size[4], int, 0444);
  75935. +MODULE_PARM_DESC(dev_tx_fifo_size_5, "Number of words in the Tx FIFO 4-768");
  75936. +module_param_named(dev_tx_fifo_size_6,
  75937. + dwc_otg_module_params.dev_tx_fifo_size[5], int, 0444);
  75938. +MODULE_PARM_DESC(dev_tx_fifo_size_6, "Number of words in the Tx FIFO 4-768");
  75939. +module_param_named(dev_tx_fifo_size_7,
  75940. + dwc_otg_module_params.dev_tx_fifo_size[6], int, 0444);
  75941. +MODULE_PARM_DESC(dev_tx_fifo_size_7, "Number of words in the Tx FIFO 4-768");
  75942. +module_param_named(dev_tx_fifo_size_8,
  75943. + dwc_otg_module_params.dev_tx_fifo_size[7], int, 0444);
  75944. +MODULE_PARM_DESC(dev_tx_fifo_size_8, "Number of words in the Tx FIFO 4-768");
  75945. +module_param_named(dev_tx_fifo_size_9,
  75946. + dwc_otg_module_params.dev_tx_fifo_size[8], int, 0444);
  75947. +MODULE_PARM_DESC(dev_tx_fifo_size_9, "Number of words in the Tx FIFO 4-768");
  75948. +module_param_named(dev_tx_fifo_size_10,
  75949. + dwc_otg_module_params.dev_tx_fifo_size[9], int, 0444);
  75950. +MODULE_PARM_DESC(dev_tx_fifo_size_10, "Number of words in the Tx FIFO 4-768");
  75951. +module_param_named(dev_tx_fifo_size_11,
  75952. + dwc_otg_module_params.dev_tx_fifo_size[10], int, 0444);
  75953. +MODULE_PARM_DESC(dev_tx_fifo_size_11, "Number of words in the Tx FIFO 4-768");
  75954. +module_param_named(dev_tx_fifo_size_12,
  75955. + dwc_otg_module_params.dev_tx_fifo_size[11], int, 0444);
  75956. +MODULE_PARM_DESC(dev_tx_fifo_size_12, "Number of words in the Tx FIFO 4-768");
  75957. +module_param_named(dev_tx_fifo_size_13,
  75958. + dwc_otg_module_params.dev_tx_fifo_size[12], int, 0444);
  75959. +MODULE_PARM_DESC(dev_tx_fifo_size_13, "Number of words in the Tx FIFO 4-768");
  75960. +module_param_named(dev_tx_fifo_size_14,
  75961. + dwc_otg_module_params.dev_tx_fifo_size[13], int, 0444);
  75962. +MODULE_PARM_DESC(dev_tx_fifo_size_14, "Number of words in the Tx FIFO 4-768");
  75963. +module_param_named(dev_tx_fifo_size_15,
  75964. + dwc_otg_module_params.dev_tx_fifo_size[14], int, 0444);
  75965. +MODULE_PARM_DESC(dev_tx_fifo_size_15, "Number of words in the Tx FIFO 4-768");
  75966. +
  75967. +module_param_named(thr_ctl, dwc_otg_module_params.thr_ctl, int, 0444);
  75968. +MODULE_PARM_DESC(thr_ctl,
  75969. + "Thresholding enable flag bit 0 - non ISO Tx thr., 1 - ISO Tx thr., 2 - Rx thr.- bit 0=disabled 1=enabled");
  75970. +module_param_named(tx_thr_length, dwc_otg_module_params.tx_thr_length, int,
  75971. + 0444);
  75972. +MODULE_PARM_DESC(tx_thr_length, "Tx Threshold length in 32 bit DWORDs");
  75973. +module_param_named(rx_thr_length, dwc_otg_module_params.rx_thr_length, int,
  75974. + 0444);
  75975. +MODULE_PARM_DESC(rx_thr_length, "Rx Threshold length in 32 bit DWORDs");
  75976. +
  75977. +module_param_named(pti_enable, dwc_otg_module_params.pti_enable, int, 0444);
  75978. +module_param_named(mpi_enable, dwc_otg_module_params.mpi_enable, int, 0444);
  75979. +module_param_named(lpm_enable, dwc_otg_module_params.lpm_enable, int, 0444);
  75980. +MODULE_PARM_DESC(lpm_enable, "LPM Enable 0=LPM Disabled 1=LPM Enabled");
  75981. +module_param_named(ic_usb_cap, dwc_otg_module_params.ic_usb_cap, int, 0444);
  75982. +MODULE_PARM_DESC(ic_usb_cap,
  75983. + "IC_USB Capability 0=IC_USB Disabled 1=IC_USB Enabled");
  75984. +module_param_named(ahb_thr_ratio, dwc_otg_module_params.ahb_thr_ratio, int,
  75985. + 0444);
  75986. +MODULE_PARM_DESC(ahb_thr_ratio, "AHB Threshold Ratio");
  75987. +module_param_named(power_down, dwc_otg_module_params.power_down, int, 0444);
  75988. +MODULE_PARM_DESC(power_down, "Power Down Mode");
  75989. +module_param_named(reload_ctl, dwc_otg_module_params.reload_ctl, int, 0444);
  75990. +MODULE_PARM_DESC(reload_ctl, "HFIR Reload Control");
  75991. +module_param_named(dev_out_nak, dwc_otg_module_params.dev_out_nak, int, 0444);
  75992. +MODULE_PARM_DESC(dev_out_nak, "Enable Device OUT NAK");
  75993. +module_param_named(cont_on_bna, dwc_otg_module_params.cont_on_bna, int, 0444);
  75994. +MODULE_PARM_DESC(cont_on_bna, "Enable Enable Continue on BNA");
  75995. +module_param_named(ahb_single, dwc_otg_module_params.ahb_single, int, 0444);
  75996. +MODULE_PARM_DESC(ahb_single, "Enable AHB Single Support");
  75997. +module_param_named(adp_enable, dwc_otg_module_params.adp_enable, int, 0444);
  75998. +MODULE_PARM_DESC(adp_enable, "ADP Enable 0=ADP Disabled 1=ADP Enabled");
  75999. +module_param_named(otg_ver, dwc_otg_module_params.otg_ver, int, 0444);
  76000. +MODULE_PARM_DESC(otg_ver, "OTG revision supported 0=OTG 1.3 1=OTG 2.0");
  76001. +module_param(microframe_schedule, bool, 0444);
  76002. +MODULE_PARM_DESC(microframe_schedule, "Enable the microframe scheduler");
  76003. +
  76004. +module_param(fiq_enable, bool, 0444);
  76005. +MODULE_PARM_DESC(fiq_enable, "Enable the FIQ");
  76006. +module_param(nak_holdoff, ushort, 0644);
  76007. +MODULE_PARM_DESC(nak_holdoff, "Throttle duration for bulk split-transaction endpoints on a NAK. Default 8");
  76008. +module_param(fiq_fsm_enable, bool, 0444);
  76009. +MODULE_PARM_DESC(fiq_fsm_enable, "Enable the FIQ to perform split transactions as defined by fiq_fsm_mask");
  76010. +module_param(fiq_fsm_mask, ushort, 0444);
  76011. +MODULE_PARM_DESC(fiq_fsm_mask, "Bitmask of transactions to perform in the FIQ.\n"
  76012. + "Bit 0 : Non-periodic split transactions\n"
  76013. + "Bit 1 : Periodic split transactions\n"
  76014. + "Bit 2 : High-speed multi-transfer isochronous\n"
  76015. + "All other bits should be set 0.");
  76016. +
  76017. +
  76018. +/** @page "Module Parameters"
  76019. + *
  76020. + * The following parameters may be specified when starting the module.
  76021. + * These parameters define how the DWC_otg controller should be
  76022. + * configured. Parameter values are passed to the CIL initialization
  76023. + * function dwc_otg_cil_init
  76024. + *
  76025. + * Example: <code>modprobe dwc_otg speed=1 otg_cap=1</code>
  76026. + *
  76027. +
  76028. + <table>
  76029. + <tr><td>Parameter Name</td><td>Meaning</td></tr>
  76030. +
  76031. + <tr>
  76032. + <td>otg_cap</td>
  76033. + <td>Specifies the OTG capabilities. The driver will automatically detect the
  76034. + value for this parameter if none is specified.
  76035. + - 0: HNP and SRP capable (default, if available)
  76036. + - 1: SRP Only capable
  76037. + - 2: No HNP/SRP capable
  76038. + </td></tr>
  76039. +
  76040. + <tr>
  76041. + <td>dma_enable</td>
  76042. + <td>Specifies whether to use slave or DMA mode for accessing the data FIFOs.
  76043. + The driver will automatically detect the value for this parameter if none is
  76044. + specified.
  76045. + - 0: Slave
  76046. + - 1: DMA (default, if available)
  76047. + </td></tr>
  76048. +
  76049. + <tr>
  76050. + <td>dma_burst_size</td>
  76051. + <td>The DMA Burst size (applicable only for External DMA Mode).
  76052. + - Values: 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  76053. + </td></tr>
  76054. +
  76055. + <tr>
  76056. + <td>speed</td>
  76057. + <td>Specifies the maximum speed of operation in host and device mode. The
  76058. + actual speed depends on the speed of the attached device and the value of
  76059. + phy_type.
  76060. + - 0: High Speed (default)
  76061. + - 1: Full Speed
  76062. + </td></tr>
  76063. +
  76064. + <tr>
  76065. + <td>host_support_fs_ls_low_power</td>
  76066. + <td>Specifies whether low power mode is supported when attached to a Full
  76067. + Speed or Low Speed device in host mode.
  76068. + - 0: Don't support low power mode (default)
  76069. + - 1: Support low power mode
  76070. + </td></tr>
  76071. +
  76072. + <tr>
  76073. + <td>host_ls_low_power_phy_clk</td>
  76074. + <td>Specifies the PHY clock rate in low power mode when connected to a Low
  76075. + Speed device in host mode. This parameter is applicable only if
  76076. + HOST_SUPPORT_FS_LS_LOW_POWER is enabled.
  76077. + - 0: 48 MHz (default)
  76078. + - 1: 6 MHz
  76079. + </td></tr>
  76080. +
  76081. + <tr>
  76082. + <td>enable_dynamic_fifo</td>
  76083. + <td> Specifies whether FIFOs may be resized by the driver software.
  76084. + - 0: Use cC FIFO size parameters
  76085. + - 1: Allow dynamic FIFO sizing (default)
  76086. + </td></tr>
  76087. +
  76088. + <tr>
  76089. + <td>data_fifo_size</td>
  76090. + <td>Total number of 4-byte words in the data FIFO memory. This memory
  76091. + includes the Rx FIFO, non-periodic Tx FIFO, and periodic Tx FIFOs.
  76092. + - Values: 32 to 32768 (default 8192)
  76093. +
  76094. + Note: The total FIFO memory depth in the FPGA configuration is 8192.
  76095. + </td></tr>
  76096. +
  76097. + <tr>
  76098. + <td>dev_rx_fifo_size</td>
  76099. + <td>Number of 4-byte words in the Rx FIFO in device mode when dynamic
  76100. + FIFO sizing is enabled.
  76101. + - Values: 16 to 32768 (default 1064)
  76102. + </td></tr>
  76103. +
  76104. + <tr>
  76105. + <td>dev_nperio_tx_fifo_size</td>
  76106. + <td>Number of 4-byte words in the non-periodic Tx FIFO in device mode when
  76107. + dynamic FIFO sizing is enabled.
  76108. + - Values: 16 to 32768 (default 1024)
  76109. + </td></tr>
  76110. +
  76111. + <tr>
  76112. + <td>dev_perio_tx_fifo_size_n (n = 1 to 15)</td>
  76113. + <td>Number of 4-byte words in each of the periodic Tx FIFOs in device mode
  76114. + when dynamic FIFO sizing is enabled.
  76115. + - Values: 4 to 768 (default 256)
  76116. + </td></tr>
  76117. +
  76118. + <tr>
  76119. + <td>host_rx_fifo_size</td>
  76120. + <td>Number of 4-byte words in the Rx FIFO in host mode when dynamic FIFO
  76121. + sizing is enabled.
  76122. + - Values: 16 to 32768 (default 1024)
  76123. + </td></tr>
  76124. +
  76125. + <tr>
  76126. + <td>host_nperio_tx_fifo_size</td>
  76127. + <td>Number of 4-byte words in the non-periodic Tx FIFO in host mode when
  76128. + dynamic FIFO sizing is enabled in the core.
  76129. + - Values: 16 to 32768 (default 1024)
  76130. + </td></tr>
  76131. +
  76132. + <tr>
  76133. + <td>host_perio_tx_fifo_size</td>
  76134. + <td>Number of 4-byte words in the host periodic Tx FIFO when dynamic FIFO
  76135. + sizing is enabled.
  76136. + - Values: 16 to 32768 (default 1024)
  76137. + </td></tr>
  76138. +
  76139. + <tr>
  76140. + <td>max_transfer_size</td>
  76141. + <td>The maximum transfer size supported in bytes.
  76142. + - Values: 2047 to 65,535 (default 65,535)
  76143. + </td></tr>
  76144. +
  76145. + <tr>
  76146. + <td>max_packet_count</td>
  76147. + <td>The maximum number of packets in a transfer.
  76148. + - Values: 15 to 511 (default 511)
  76149. + </td></tr>
  76150. +
  76151. + <tr>
  76152. + <td>host_channels</td>
  76153. + <td>The number of host channel registers to use.
  76154. + - Values: 1 to 16 (default 12)
  76155. +
  76156. + Note: The FPGA configuration supports a maximum of 12 host channels.
  76157. + </td></tr>
  76158. +
  76159. + <tr>
  76160. + <td>dev_endpoints</td>
  76161. + <td>The number of endpoints in addition to EP0 available for device mode
  76162. + operations.
  76163. + - Values: 1 to 15 (default 6 IN and OUT)
  76164. +
  76165. + Note: The FPGA configuration supports a maximum of 6 IN and OUT endpoints in
  76166. + addition to EP0.
  76167. + </td></tr>
  76168. +
  76169. + <tr>
  76170. + <td>phy_type</td>
  76171. + <td>Specifies the type of PHY interface to use. By default, the driver will
  76172. + automatically detect the phy_type.
  76173. + - 0: Full Speed
  76174. + - 1: UTMI+ (default, if available)
  76175. + - 2: ULPI
  76176. + </td></tr>
  76177. +
  76178. + <tr>
  76179. + <td>phy_utmi_width</td>
  76180. + <td>Specifies the UTMI+ Data Width. This parameter is applicable for a
  76181. + phy_type of UTMI+. Also, this parameter is applicable only if the
  76182. + OTG_HSPHY_WIDTH cC parameter was set to "8 and 16 bits", meaning that the
  76183. + core has been configured to work at either data path width.
  76184. + - Values: 8 or 16 bits (default 16)
  76185. + </td></tr>
  76186. +
  76187. + <tr>
  76188. + <td>phy_ulpi_ddr</td>
  76189. + <td>Specifies whether the ULPI operates at double or single data rate. This
  76190. + parameter is only applicable if phy_type is ULPI.
  76191. + - 0: single data rate ULPI interface with 8 bit wide data bus (default)
  76192. + - 1: double data rate ULPI interface with 4 bit wide data bus
  76193. + </td></tr>
  76194. +
  76195. + <tr>
  76196. + <td>i2c_enable</td>
  76197. + <td>Specifies whether to use the I2C interface for full speed PHY. This
  76198. + parameter is only applicable if PHY_TYPE is FS.
  76199. + - 0: Disabled (default)
  76200. + - 1: Enabled
  76201. + </td></tr>
  76202. +
  76203. + <tr>
  76204. + <td>ulpi_fs_ls</td>
  76205. + <td>Specifies whether to use ULPI FS/LS mode only.
  76206. + - 0: Disabled (default)
  76207. + - 1: Enabled
  76208. + </td></tr>
  76209. +
  76210. + <tr>
  76211. + <td>ts_dline</td>
  76212. + <td>Specifies whether term select D-Line pulsing for all PHYs is enabled.
  76213. + - 0: Disabled (default)
  76214. + - 1: Enabled
  76215. + </td></tr>
  76216. +
  76217. + <tr>
  76218. + <td>en_multiple_tx_fifo</td>
  76219. + <td>Specifies whether dedicatedto tx fifos are enabled for non periodic IN EPs.
  76220. + The driver will automatically detect the value for this parameter if none is
  76221. + specified.
  76222. + - 0: Disabled
  76223. + - 1: Enabled (default, if available)
  76224. + </td></tr>
  76225. +
  76226. + <tr>
  76227. + <td>dev_tx_fifo_size_n (n = 1 to 15)</td>
  76228. + <td>Number of 4-byte words in each of the Tx FIFOs in device mode
  76229. + when dynamic FIFO sizing is enabled.
  76230. + - Values: 4 to 768 (default 256)
  76231. + </td></tr>
  76232. +
  76233. + <tr>
  76234. + <td>tx_thr_length</td>
  76235. + <td>Transmit Threshold length in 32 bit double words
  76236. + - Values: 8 to 128 (default 64)
  76237. + </td></tr>
  76238. +
  76239. + <tr>
  76240. + <td>rx_thr_length</td>
  76241. + <td>Receive Threshold length in 32 bit double words
  76242. + - Values: 8 to 128 (default 64)
  76243. + </td></tr>
  76244. +
  76245. +<tr>
  76246. + <td>thr_ctl</td>
  76247. + <td>Specifies whether to enable Thresholding for Device mode. Bits 0, 1, 2 of
  76248. + this parmater specifies if thresholding is enabled for non-Iso Tx, Iso Tx and
  76249. + Rx transfers accordingly.
  76250. + The driver will automatically detect the value for this parameter if none is
  76251. + specified.
  76252. + - Values: 0 to 7 (default 0)
  76253. + Bit values indicate:
  76254. + - 0: Thresholding disabled
  76255. + - 1: Thresholding enabled
  76256. + </td></tr>
  76257. +
  76258. +<tr>
  76259. + <td>dma_desc_enable</td>
  76260. + <td>Specifies whether to enable Descriptor DMA mode.
  76261. + The driver will automatically detect the value for this parameter if none is
  76262. + specified.
  76263. + - 0: Descriptor DMA disabled
  76264. + - 1: Descriptor DMA (default, if available)
  76265. + </td></tr>
  76266. +
  76267. +<tr>
  76268. + <td>mpi_enable</td>
  76269. + <td>Specifies whether to enable MPI enhancement mode.
  76270. + The driver will automatically detect the value for this parameter if none is
  76271. + specified.
  76272. + - 0: MPI disabled (default)
  76273. + - 1: MPI enable
  76274. + </td></tr>
  76275. +
  76276. +<tr>
  76277. + <td>pti_enable</td>
  76278. + <td>Specifies whether to enable PTI enhancement support.
  76279. + The driver will automatically detect the value for this parameter if none is
  76280. + specified.
  76281. + - 0: PTI disabled (default)
  76282. + - 1: PTI enable
  76283. + </td></tr>
  76284. +
  76285. +<tr>
  76286. + <td>lpm_enable</td>
  76287. + <td>Specifies whether to enable LPM support.
  76288. + The driver will automatically detect the value for this parameter if none is
  76289. + specified.
  76290. + - 0: LPM disabled
  76291. + - 1: LPM enable (default, if available)
  76292. + </td></tr>
  76293. +
  76294. +<tr>
  76295. + <td>ic_usb_cap</td>
  76296. + <td>Specifies whether to enable IC_USB capability.
  76297. + The driver will automatically detect the value for this parameter if none is
  76298. + specified.
  76299. + - 0: IC_USB disabled (default, if available)
  76300. + - 1: IC_USB enable
  76301. + </td></tr>
  76302. +
  76303. +<tr>
  76304. + <td>ahb_thr_ratio</td>
  76305. + <td>Specifies AHB Threshold ratio.
  76306. + - Values: 0 to 3 (default 0)
  76307. + </td></tr>
  76308. +
  76309. +<tr>
  76310. + <td>power_down</td>
  76311. + <td>Specifies Power Down(Hibernation) Mode.
  76312. + The driver will automatically detect the value for this parameter if none is
  76313. + specified.
  76314. + - 0: Power Down disabled (default)
  76315. + - 2: Power Down enabled
  76316. + </td></tr>
  76317. +
  76318. + <tr>
  76319. + <td>reload_ctl</td>
  76320. + <td>Specifies whether dynamic reloading of the HFIR register is allowed during
  76321. + run time. The driver will automatically detect the value for this parameter if
  76322. + none is specified. In case the HFIR value is reloaded when HFIR.RldCtrl == 1'b0
  76323. + the core might misbehave.
  76324. + - 0: Reload Control disabled (default)
  76325. + - 1: Reload Control enabled
  76326. + </td></tr>
  76327. +
  76328. + <tr>
  76329. + <td>dev_out_nak</td>
  76330. + <td>Specifies whether Device OUT NAK enhancement enabled or no.
  76331. + The driver will automatically detect the value for this parameter if
  76332. + none is specified. This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  76333. + - 0: The core does not set NAK after Bulk OUT transfer complete (default)
  76334. + - 1: The core sets NAK after Bulk OUT transfer complete
  76335. + </td></tr>
  76336. +
  76337. + <tr>
  76338. + <td>cont_on_bna</td>
  76339. + <td>Specifies whether Enable Continue on BNA enabled or no.
  76340. + After receiving BNA interrupt the core disables the endpoint,when the
  76341. + endpoint is re-enabled by the application the
  76342. + - 0: Core starts processing from the DOEPDMA descriptor (default)
  76343. + - 1: Core starts processing from the descriptor which received the BNA.
  76344. + This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  76345. + </td></tr>
  76346. +
  76347. + <tr>
  76348. + <td>ahb_single</td>
  76349. + <td>This bit when programmed supports SINGLE transfers for remainder data
  76350. + in a transfer for DMA mode of operation.
  76351. + - 0: The remainder data will be sent using INCR burst size (default)
  76352. + - 1: The remainder data will be sent using SINGLE burst size.
  76353. + </td></tr>
  76354. +
  76355. +<tr>
  76356. + <td>adp_enable</td>
  76357. + <td>Specifies whether ADP feature is enabled.
  76358. + The driver will automatically detect the value for this parameter if none is
  76359. + specified.
  76360. + - 0: ADP feature disabled (default)
  76361. + - 1: ADP feature enabled
  76362. + </td></tr>
  76363. +
  76364. + <tr>
  76365. + <td>otg_ver</td>
  76366. + <td>Specifies whether OTG is performing as USB OTG Revision 2.0 or Revision 1.3
  76367. + USB OTG device.
  76368. + - 0: OTG 2.0 support disabled (default)
  76369. + - 1: OTG 2.0 support enabled
  76370. + </td></tr>
  76371. +
  76372. +*/
  76373. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_driver.h linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_driver.h
  76374. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_driver.h 1969-12-31 18:00:00.000000000 -0600
  76375. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_driver.h 2014-12-03 19:13:40.220418001 -0600
  76376. @@ -0,0 +1,86 @@
  76377. +/* ==========================================================================
  76378. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.h $
  76379. + * $Revision: #19 $
  76380. + * $Date: 2010/11/15 $
  76381. + * $Change: 1627671 $
  76382. + *
  76383. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  76384. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  76385. + * otherwise expressly agreed to in writing between Synopsys and you.
  76386. + *
  76387. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  76388. + * any End User Software License Agreement or Agreement for Licensed Product
  76389. + * with Synopsys or any supplement thereto. You are permitted to use and
  76390. + * redistribute this Software in source and binary forms, with or without
  76391. + * modification, provided that redistributions of source code must retain this
  76392. + * notice. You may not view, use, disclose, copy or distribute this file or
  76393. + * any information contained herein except pursuant to this license grant from
  76394. + * Synopsys. If you do not agree with this notice, including the disclaimer
  76395. + * below, then you are not authorized to use the Software.
  76396. + *
  76397. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  76398. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  76399. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  76400. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  76401. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  76402. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  76403. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  76404. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  76405. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  76406. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  76407. + * DAMAGE.
  76408. + * ========================================================================== */
  76409. +
  76410. +#ifndef __DWC_OTG_DRIVER_H__
  76411. +#define __DWC_OTG_DRIVER_H__
  76412. +
  76413. +/** @file
  76414. + * This file contains the interface to the Linux driver.
  76415. + */
  76416. +#include "dwc_otg_os_dep.h"
  76417. +#include "dwc_otg_core_if.h"
  76418. +
  76419. +/* Type declarations */
  76420. +struct dwc_otg_pcd;
  76421. +struct dwc_otg_hcd;
  76422. +
  76423. +/**
  76424. + * This structure is a wrapper that encapsulates the driver components used to
  76425. + * manage a single DWC_otg controller.
  76426. + */
  76427. +typedef struct dwc_otg_device {
  76428. + /** Structure containing OS-dependent stuff. KEEP THIS STRUCT AT THE
  76429. + * VERY BEGINNING OF THE DEVICE STRUCT. OSes such as FreeBSD and NetBSD
  76430. + * require this. */
  76431. + struct os_dependent os_dep;
  76432. +
  76433. + /** Pointer to the core interface structure. */
  76434. + dwc_otg_core_if_t *core_if;
  76435. +
  76436. + /** Pointer to the PCD structure. */
  76437. + struct dwc_otg_pcd *pcd;
  76438. +
  76439. + /** Pointer to the HCD structure. */
  76440. + struct dwc_otg_hcd *hcd;
  76441. +
  76442. + /** Flag to indicate whether the common IRQ handler is installed. */
  76443. + uint8_t common_irq_installed;
  76444. +
  76445. +} dwc_otg_device_t;
  76446. +
  76447. +/*We must clear S3C24XX_EINTPEND external interrupt register
  76448. + * because after clearing in this register trigerred IRQ from
  76449. + * H/W core in kernel interrupt can be occured again before OTG
  76450. + * handlers clear all IRQ sources of Core registers because of
  76451. + * timing latencies and Low Level IRQ Type.
  76452. + */
  76453. +#ifdef CONFIG_MACH_IPMATE
  76454. +#define S3C2410X_CLEAR_EINTPEND() \
  76455. +do { \
  76456. + __raw_writel(1UL << 11,S3C24XX_EINTPEND); \
  76457. +} while (0)
  76458. +#else
  76459. +#define S3C2410X_CLEAR_EINTPEND() do { } while (0)
  76460. +#endif
  76461. +
  76462. +#endif
  76463. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
  76464. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c 1969-12-31 18:00:00.000000000 -0600
  76465. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c 2014-12-03 19:13:40.220418001 -0600
  76466. @@ -0,0 +1,1294 @@
  76467. +/*
  76468. + * dwc_otg_fiq_fsm.c - The finite state machine FIQ
  76469. + *
  76470. + * Copyright (c) 2013 Raspberry Pi Foundation
  76471. + *
  76472. + * Author: Jonathan Bell <jonathan@raspberrypi.org>
  76473. + * All rights reserved.
  76474. + *
  76475. + * Redistribution and use in source and binary forms, with or without
  76476. + * modification, are permitted provided that the following conditions are met:
  76477. + * * Redistributions of source code must retain the above copyright
  76478. + * notice, this list of conditions and the following disclaimer.
  76479. + * * Redistributions in binary form must reproduce the above copyright
  76480. + * notice, this list of conditions and the following disclaimer in the
  76481. + * documentation and/or other materials provided with the distribution.
  76482. + * * Neither the name of Raspberry Pi nor the
  76483. + * names of its contributors may be used to endorse or promote products
  76484. + * derived from this software without specific prior written permission.
  76485. + *
  76486. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  76487. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  76488. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  76489. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  76490. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  76491. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  76492. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  76493. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  76494. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  76495. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  76496. + *
  76497. + * This FIQ implements functionality that performs split transactions on
  76498. + * the dwc_otg hardware without any outside intervention. A split transaction
  76499. + * is "queued" by nominating a specific host channel to perform the entirety
  76500. + * of a split transaction. This FIQ will then perform the microframe-precise
  76501. + * scheduling required in each phase of the transaction until completion.
  76502. + *
  76503. + * The FIQ functionality is glued into the Synopsys driver via the entry point
  76504. + * in the FSM enqueue function, and at the exit point in handling a HC interrupt
  76505. + * for a FSM-enabled channel.
  76506. + *
  76507. + * NB: Large parts of this implementation have architecture-specific code.
  76508. + * For porting this functionality to other ARM machines, the minimum is required:
  76509. + * - An interrupt controller allowing the top-level dwc USB interrupt to be routed
  76510. + * to the FIQ
  76511. + * - A method of forcing a software generated interrupt from FIQ mode that then
  76512. + * triggers an IRQ entry (with the dwc USB handler called by this IRQ number)
  76513. + * - Guaranteed interrupt routing such that both the FIQ and SGI occur on the same
  76514. + * processor core - there is no locking between the FIQ and IRQ (aside from
  76515. + * local_fiq_disable)
  76516. + *
  76517. + */
  76518. +
  76519. +#include "dwc_otg_fiq_fsm.h"
  76520. +
  76521. +
  76522. +char buffer[1000*16];
  76523. +int wptr;
  76524. +void notrace _fiq_print(enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...)
  76525. +{
  76526. + enum fiq_debug_level dbg_lvl_req = FIQDBG_ERR;
  76527. + va_list args;
  76528. + char text[17];
  76529. + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + 0x408) };
  76530. +
  76531. + if((dbg_lvl & dbg_lvl_req) || dbg_lvl == FIQDBG_ERR)
  76532. + {
  76533. + snprintf(text, 9, " %4d:%1u ", hfnum.b.frnum/8, hfnum.b.frnum & 7);
  76534. + va_start(args, fmt);
  76535. + vsnprintf(text+8, 9, fmt, args);
  76536. + va_end(args);
  76537. +
  76538. + memcpy(buffer + wptr, text, 16);
  76539. + wptr = (wptr + 16) % sizeof(buffer);
  76540. + }
  76541. +}
  76542. +
  76543. +/**
  76544. + * fiq_fsm_restart_channel() - Poke channel enable bit for a split transaction
  76545. + * @channel: channel to re-enable
  76546. + */
  76547. +static void fiq_fsm_restart_channel(struct fiq_state *st, int n, int force)
  76548. +{
  76549. + hcchar_data_t hcchar = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR) };
  76550. +
  76551. + hcchar.b.chen = 0;
  76552. + if (st->channel[n].hcchar_copy.b.eptype & 0x1) {
  76553. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  76554. + /* Hardware bug workaround: update the ssplit index */
  76555. + if (st->channel[n].hcsplt_copy.b.spltena)
  76556. + st->channel[n].expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
  76557. +
  76558. + hcchar.b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
  76559. + }
  76560. +
  76561. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
  76562. + hcchar.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  76563. + hcchar.b.chen = 1;
  76564. +
  76565. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
  76566. + fiq_print(FIQDBG_INT, st, "HCGO %01d %01d", n, force);
  76567. +}
  76568. +
  76569. +/**
  76570. + * fiq_fsm_setup_csplit() - Prepare a host channel for a CSplit transaction stage
  76571. + * @st: Pointer to the channel's state
  76572. + * @n : channel number
  76573. + *
  76574. + * Change host channel registers to perform a complete-split transaction. Being mindful of the
  76575. + * endpoint direction, set control regs up correctly.
  76576. + */
  76577. +static void notrace fiq_fsm_setup_csplit(struct fiq_state *st, int n)
  76578. +{
  76579. + hcsplt_data_t hcsplt = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT) };
  76580. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  76581. +
  76582. + hcsplt.b.compsplt = 1;
  76583. + if (st->channel[n].hcchar_copy.b.epdir == 1) {
  76584. + // If IN, the CSPLIT result contains the data or a hub handshake. hctsiz = maxpacket.
  76585. + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
  76586. + } else {
  76587. + // If OUT, the CSPLIT result contains handshake only.
  76588. + hctsiz.b.xfersize = 0;
  76589. + }
  76590. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
  76591. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  76592. + mb();
  76593. +}
  76594. +
  76595. +static inline int notrace fiq_get_xfer_len(struct fiq_state *st, int n)
  76596. +{
  76597. + /* The xfersize register is a bit wonky. For IN transfers, it decrements by the packet size. */
  76598. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  76599. +
  76600. + if (st->channel[n].hcchar_copy.b.epdir == 0) {
  76601. + return st->channel[n].hctsiz_copy.b.xfersize;
  76602. + } else {
  76603. + return st->channel[n].hctsiz_copy.b.xfersize - hctsiz.b.xfersize;
  76604. + }
  76605. +
  76606. +}
  76607. +
  76608. +
  76609. +/**
  76610. + * fiq_increment_dma_buf() - update DMA address for bounce buffers after a CSPLIT
  76611. + *
  76612. + * Of use only for IN periodic transfers.
  76613. + */
  76614. +static int notrace fiq_increment_dma_buf(struct fiq_state *st, int num_channels, int n)
  76615. +{
  76616. + hcdma_data_t hcdma;
  76617. + int i = st->channel[n].dma_info.index;
  76618. + int len;
  76619. + struct fiq_dma_blob *blob = (struct fiq_dma_blob *) st->dma_base;
  76620. +
  76621. + len = fiq_get_xfer_len(st, n);
  76622. + fiq_print(FIQDBG_INT, st, "LEN: %03d", len);
  76623. + st->channel[n].dma_info.slot_len[i] = len;
  76624. + i++;
  76625. + if (i > 6)
  76626. + BUG();
  76627. +
  76628. + hcdma.d32 = (dma_addr_t) &blob->channel[n].index[i].buf[0];
  76629. + FIQ_WRITE(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
  76630. + st->channel[n].dma_info.index = i;
  76631. + return 0;
  76632. +}
  76633. +
  76634. +/**
  76635. + * fiq_reload_hctsiz() - for IN transactions, reset HCTSIZ
  76636. + */
  76637. +static void notrace fiq_fsm_reload_hctsiz(struct fiq_state *st, int n)
  76638. +{
  76639. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  76640. + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
  76641. + hctsiz.b.pktcnt = 1;
  76642. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  76643. +}
  76644. +
  76645. +/**
  76646. + * fiq_iso_out_advance() - update DMA address and split position bits
  76647. + * for isochronous OUT transactions.
  76648. + *
  76649. + * Returns 1 if this is the last packet queued, 0 otherwise. Split-ALL and
  76650. + * Split-BEGIN states are not handled - this is done when the transaction was queued.
  76651. + *
  76652. + * This function must only be called from the FIQ_ISO_OUT_ACTIVE state.
  76653. + */
  76654. +static int notrace fiq_iso_out_advance(struct fiq_state *st, int num_channels, int n)
  76655. +{
  76656. + hcsplt_data_t hcsplt;
  76657. + hctsiz_data_t hctsiz;
  76658. + hcdma_data_t hcdma;
  76659. + struct fiq_dma_blob *blob = (struct fiq_dma_blob *) st->dma_base;
  76660. + int last = 0;
  76661. + int i = st->channel[n].dma_info.index;
  76662. +
  76663. + fiq_print(FIQDBG_INT, st, "ADV %01d %01d ", n, i);
  76664. + i++;
  76665. + if (i == 4)
  76666. + last = 1;
  76667. + if (st->channel[n].dma_info.slot_len[i+1] == 255)
  76668. + last = 1;
  76669. +
  76670. + /* New DMA address - address of bounce buffer referred to in index */
  76671. + hcdma.d32 = (uint32_t) &blob->channel[n].index[i].buf[0];
  76672. + //hcdma.d32 = FIQ_READ(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n));
  76673. + //hcdma.d32 += st->channel[n].dma_info.slot_len[i];
  76674. + fiq_print(FIQDBG_INT, st, "LAST: %01d ", last);
  76675. + fiq_print(FIQDBG_INT, st, "LEN: %03d", st->channel[n].dma_info.slot_len[i]);
  76676. + hcsplt.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT);
  76677. + hctsiz.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ);
  76678. + hcsplt.b.xactpos = (last) ? ISOC_XACTPOS_END : ISOC_XACTPOS_MID;
  76679. + /* Set up new packet length */
  76680. + hctsiz.b.pktcnt = 1;
  76681. + hctsiz.b.xfersize = st->channel[n].dma_info.slot_len[i];
  76682. + fiq_print(FIQDBG_INT, st, "%08x", hctsiz.d32);
  76683. +
  76684. + st->channel[n].dma_info.index++;
  76685. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
  76686. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  76687. + FIQ_WRITE(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
  76688. + return last;
  76689. +}
  76690. +
  76691. +/**
  76692. + * fiq_fsm_tt_next_isoc() - queue next pending isochronous out start-split on a TT
  76693. + *
  76694. + * Despite the limitations of the DWC core, we can force a microframe pipeline of
  76695. + * isochronous OUT start-split transactions while waiting for a corresponding other-type
  76696. + * of endpoint to finish its CSPLITs. TTs have big periodic buffers therefore it
  76697. + * is very unlikely that filling the start-split FIFO will cause data loss.
  76698. + * This allows much better interleaving of transactions in an order-independent way-
  76699. + * there is no requirement to prioritise isochronous, just a state-space search has
  76700. + * to be performed on each periodic start-split complete interrupt.
  76701. + */
  76702. +static int notrace fiq_fsm_tt_next_isoc(struct fiq_state *st, int num_channels, int n)
  76703. +{
  76704. + int hub_addr = st->channel[n].hub_addr;
  76705. + int port_addr = st->channel[n].port_addr;
  76706. + int i, poked = 0;
  76707. + for (i = 0; i < num_channels; i++) {
  76708. + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
  76709. + continue;
  76710. + if (st->channel[i].hub_addr == hub_addr &&
  76711. + st->channel[i].port_addr == port_addr) {
  76712. + switch (st->channel[i].fsm) {
  76713. + case FIQ_PER_ISO_OUT_PENDING:
  76714. + if (st->channel[i].nrpackets == 1) {
  76715. + st->channel[i].fsm = FIQ_PER_ISO_OUT_LAST;
  76716. + } else {
  76717. + st->channel[i].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  76718. + }
  76719. + fiq_fsm_restart_channel(st, i, 0);
  76720. + poked = 1;
  76721. + break;
  76722. +
  76723. + default:
  76724. + break;
  76725. + }
  76726. + }
  76727. + if (poked)
  76728. + break;
  76729. + }
  76730. + return poked;
  76731. +}
  76732. +
  76733. +/**
  76734. + * fiq_fsm_tt_in_use() - search for host channels using this TT
  76735. + * @n: Channel to use as reference
  76736. + *
  76737. + */
  76738. +int notrace noinline fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n)
  76739. +{
  76740. + int hub_addr = st->channel[n].hub_addr;
  76741. + int port_addr = st->channel[n].port_addr;
  76742. + int i, in_use = 0;
  76743. + for (i = 0; i < num_channels; i++) {
  76744. + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
  76745. + continue;
  76746. + switch (st->channel[i].fsm) {
  76747. + /* TT is reserved for channels that are in the middle of a periodic
  76748. + * split transaction.
  76749. + */
  76750. + case FIQ_PER_SSPLIT_STARTED:
  76751. + case FIQ_PER_CSPLIT_WAIT:
  76752. + case FIQ_PER_CSPLIT_NYET1:
  76753. + //case FIQ_PER_CSPLIT_POLL:
  76754. + case FIQ_PER_ISO_OUT_ACTIVE:
  76755. + case FIQ_PER_ISO_OUT_LAST:
  76756. + if (st->channel[i].hub_addr == hub_addr &&
  76757. + st->channel[i].port_addr == port_addr) {
  76758. + in_use = 1;
  76759. + }
  76760. + break;
  76761. + default:
  76762. + break;
  76763. + }
  76764. + if (in_use)
  76765. + break;
  76766. + }
  76767. + return in_use;
  76768. +}
  76769. +
  76770. +/**
  76771. + * fiq_fsm_more_csplits() - determine whether additional CSPLITs need
  76772. + * to be issued for this IN transaction.
  76773. + *
  76774. + * We cannot tell the inbound PID of a data packet due to hardware limitations.
  76775. + * we need to make an educated guess as to whether we need to queue another CSPLIT
  76776. + * or not. A no-brainer is when we have received enough data to fill the endpoint
  76777. + * size, but for endpoints that give variable-length data then we have to resort
  76778. + * to heuristics.
  76779. + *
  76780. + * We also return whether this is the last CSPLIT to be queued, again based on
  76781. + * heuristics. This is to allow a 1-uframe overlap of periodic split transactions.
  76782. + * Note: requires at least 1 CSPLIT to have been performed prior to being called.
  76783. + */
  76784. +
  76785. +/*
  76786. + * We need some way of guaranteeing if a returned periodic packet of size X
  76787. + * has a DATA0 PID.
  76788. + * The heuristic value of 144 bytes assumes that the received data has maximal
  76789. + * bit-stuffing and the clock frequency of the transmitting device is at the lowest
  76790. + * permissible limit. If the transfer length results in a final packet size
  76791. + * 144 < p <= 188, then an erroneous CSPLIT will be issued.
  76792. + * Also used to ensure that an endpoint will nominally only return a single
  76793. + * complete-split worth of data.
  76794. + */
  76795. +#define DATA0_PID_HEURISTIC 144
  76796. +
  76797. +static int notrace noinline fiq_fsm_more_csplits(struct fiq_state *state, int n, int *probably_last)
  76798. +{
  76799. +
  76800. + int i;
  76801. + int total_len = 0;
  76802. + int more_needed = 1;
  76803. + struct fiq_channel_state *st = &state->channel[n];
  76804. +
  76805. + for (i = 0; i < st->dma_info.index; i++) {
  76806. + total_len += st->dma_info.slot_len[i];
  76807. + }
  76808. +
  76809. + *probably_last = 0;
  76810. +
  76811. + if (st->hcchar_copy.b.eptype == 0x3) {
  76812. + /*
  76813. + * An interrupt endpoint will take max 2 CSPLITs. if we are receiving data
  76814. + * then this is definitely the last CSPLIT.
  76815. + */
  76816. + *probably_last = 1;
  76817. + } else {
  76818. + /* Isoc IN. This is a bit risky if we are the first transaction:
  76819. + * we may have been held off slightly. */
  76820. + if (i > 1 && st->dma_info.slot_len[st->dma_info.index-1] <= DATA0_PID_HEURISTIC) {
  76821. + more_needed = 0;
  76822. + }
  76823. + /* If in the next uframe we will receive enough data to fill the endpoint,
  76824. + * then only issue 1 more csplit.
  76825. + */
  76826. + if (st->hctsiz_copy.b.xfersize - total_len <= DATA0_PID_HEURISTIC)
  76827. + *probably_last = 1;
  76828. + }
  76829. +
  76830. + if (total_len >= st->hctsiz_copy.b.xfersize ||
  76831. + i == 6 || total_len == 0)
  76832. + /* Note: due to bit stuffing it is possible to have > 6 CSPLITs for
  76833. + * a single endpoint. Accepting more would completely break our scheduling mechanism though
  76834. + * - in these extreme cases we will pass through a truncated packet.
  76835. + */
  76836. + more_needed = 0;
  76837. +
  76838. + return more_needed;
  76839. +}
  76840. +
  76841. +/**
  76842. + * fiq_fsm_too_late() - Test transaction for lateness
  76843. + *
  76844. + * If a SSPLIT for a large IN transaction is issued too late in a frame,
  76845. + * the hub will disable the port to the device and respond with ERR handshakes.
  76846. + * The hub status endpoint will not reflect this change.
  76847. + * Returns 1 if we will issue a SSPLIT that will result in a device babble.
  76848. + */
  76849. +int notrace fiq_fsm_too_late(struct fiq_state *st, int n)
  76850. +{
  76851. + int uframe;
  76852. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  76853. + uframe = hfnum.b.frnum & 0x7;
  76854. + if ((uframe < 6) && (st->channel[n].nrpackets + 1 + uframe > 7)) {
  76855. + return 1;
  76856. + } else {
  76857. + return 0;
  76858. + }
  76859. +}
  76860. +
  76861. +
  76862. +/**
  76863. + * fiq_fsm_start_next_periodic() - A half-arsed attempt at a microframe pipeline
  76864. + *
  76865. + * Search pending transactions in the start-split pending state and queue them.
  76866. + * Don't queue packets in uframe .5 (comes out in .6) (USB2.0 11.18.4).
  76867. + * Note: we specifically don't do isochronous OUT transactions first because better
  76868. + * use of the TT's start-split fifo can be achieved by pipelining an IN before an OUT.
  76869. + */
  76870. +static void notrace noinline fiq_fsm_start_next_periodic(struct fiq_state *st, int num_channels)
  76871. +{
  76872. + int n;
  76873. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  76874. + if ((hfnum.b.frnum & 0x7) == 5)
  76875. + return;
  76876. + for (n = 0; n < num_channels; n++) {
  76877. + if (st->channel[n].fsm == FIQ_PER_SSPLIT_QUEUED) {
  76878. + /* Check to see if any other transactions are using this TT */
  76879. + if(!fiq_fsm_tt_in_use(st, num_channels, n)) {
  76880. + if (!fiq_fsm_too_late(st, n)) {
  76881. + st->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
  76882. + fiq_print(FIQDBG_INT, st, "NEXTPER ");
  76883. + fiq_fsm_restart_channel(st, n, 0);
  76884. + } else {
  76885. + st->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  76886. + }
  76887. + break;
  76888. + }
  76889. + }
  76890. + }
  76891. + for (n = 0; n < num_channels; n++) {
  76892. + if (st->channel[n].fsm == FIQ_PER_ISO_OUT_PENDING) {
  76893. + if (!fiq_fsm_tt_in_use(st, num_channels, n)) {
  76894. + fiq_print(FIQDBG_INT, st, "NEXTISO ");
  76895. + st->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  76896. + fiq_fsm_restart_channel(st, n, 0);
  76897. + break;
  76898. + }
  76899. + }
  76900. + }
  76901. +}
  76902. +
  76903. +/**
  76904. + * fiq_fsm_update_hs_isoc() - update isochronous frame and transfer data
  76905. + * @state: Pointer to fiq_state
  76906. + * @n: Channel transaction is active on
  76907. + * @hcint: Copy of host channel interrupt register
  76908. + *
  76909. + * Returns 0 if there are no more transactions for this HC to do, 1
  76910. + * otherwise.
  76911. + */
  76912. +static int notrace noinline fiq_fsm_update_hs_isoc(struct fiq_state *state, int n, hcint_data_t hcint)
  76913. +{
  76914. + struct fiq_channel_state *st = &state->channel[n];
  76915. + int xfer_len = 0, nrpackets = 0;
  76916. + hcdma_data_t hcdma;
  76917. + fiq_print(FIQDBG_INT, state, "HSISO %02d", n);
  76918. +
  76919. + xfer_len = fiq_get_xfer_len(state, n);
  76920. + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].actual_length = xfer_len;
  76921. +
  76922. + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].status = hcint.d32;
  76923. +
  76924. + st->hs_isoc_info.index++;
  76925. + if (st->hs_isoc_info.index == st->hs_isoc_info.nrframes) {
  76926. + return 0;
  76927. + }
  76928. +
  76929. + /* grab the next DMA address offset from the array */
  76930. + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].offset;
  76931. + FIQ_WRITE(state->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
  76932. +
  76933. + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
  76934. + * the core needs to be told to send the correct number. Caution: for IN transfers,
  76935. + * this is always set to the maximum size of the endpoint. */
  76936. + xfer_len = st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].length;
  76937. + /* Integer divide in a FIQ: fun. FIXME: make this not suck */
  76938. + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
  76939. + if (nrpackets == 0)
  76940. + nrpackets = 1;
  76941. + st->hcchar_copy.b.multicnt = nrpackets;
  76942. + st->hctsiz_copy.b.pktcnt = nrpackets;
  76943. +
  76944. + /* Initial PID also needs to be set */
  76945. + if (st->hcchar_copy.b.epdir == 0) {
  76946. + st->hctsiz_copy.b.xfersize = xfer_len;
  76947. + switch (st->hcchar_copy.b.multicnt) {
  76948. + case 1:
  76949. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  76950. + break;
  76951. + case 2:
  76952. + case 3:
  76953. + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
  76954. + break;
  76955. + }
  76956. +
  76957. + } else {
  76958. + switch (st->hcchar_copy.b.multicnt) {
  76959. + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
  76960. + case 1:
  76961. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  76962. + break;
  76963. + case 2:
  76964. + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
  76965. + break;
  76966. + case 3:
  76967. + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
  76968. + break;
  76969. + }
  76970. + }
  76971. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, st->hctsiz_copy.d32);
  76972. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, st->hcchar_copy.d32);
  76973. + /* Channel is enabled on hcint handler exit */
  76974. + fiq_print(FIQDBG_INT, state, "HSISOOUT");
  76975. + return 1;
  76976. +}
  76977. +
  76978. +
  76979. +/**
  76980. + * fiq_fsm_do_sof() - FSM start-of-frame interrupt handler
  76981. + * @state: Pointer to the state struct passed from banked FIQ mode registers.
  76982. + * @num_channels: set according to the DWC hardware configuration
  76983. + *
  76984. + * The SOF handler in FSM mode has two functions
  76985. + * 1. Hold off SOF from causing schedule advancement in IRQ context if there's
  76986. + * nothing to do
  76987. + * 2. Advance certain FSM states that require either a microframe delay, or a microframe
  76988. + * of holdoff.
  76989. + *
  76990. + * The second part is architecture-specific to mach-bcm2835 -
  76991. + * a sane interrupt controller would have a mask register for ARM interrupt sources
  76992. + * to be promoted to the nFIQ line, but it doesn't. Instead a single interrupt
  76993. + * number (USB) can be enabled. This means that certain parts of the USB specification
  76994. + * that require "wait a little while, then issue another packet" cannot be fulfilled with
  76995. + * the timing granularity required to achieve optimal throughout. The workaround is to use
  76996. + * the SOF "timer" (125uS) to perform this task.
  76997. + */
  76998. +static int notrace noinline fiq_fsm_do_sof(struct fiq_state *state, int num_channels)
  76999. +{
  77000. + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + HFNUM) };
  77001. + int n;
  77002. + int kick_irq = 0;
  77003. +
  77004. + if ((hfnum.b.frnum & 0x7) == 1) {
  77005. + /* We cannot issue csplits for transactions in the last frame past (n+1).1
  77006. + * Check to see if there are any transactions that are stale.
  77007. + * Boot them out.
  77008. + */
  77009. + for (n = 0; n < num_channels; n++) {
  77010. + switch (state->channel[n].fsm) {
  77011. + case FIQ_PER_CSPLIT_WAIT:
  77012. + case FIQ_PER_CSPLIT_NYET1:
  77013. + case FIQ_PER_CSPLIT_POLL:
  77014. + case FIQ_PER_CSPLIT_LAST:
  77015. + /* Check if we are no longer in the same full-speed frame. */
  77016. + if (((state->channel[n].expected_uframe & 0x3FFF) & ~0x7) <
  77017. + (hfnum.b.frnum & ~0x7))
  77018. + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  77019. + break;
  77020. + default:
  77021. + break;
  77022. + }
  77023. + }
  77024. + }
  77025. +
  77026. + for (n = 0; n < num_channels; n++) {
  77027. + switch (state->channel[n].fsm) {
  77028. +
  77029. + case FIQ_NP_SSPLIT_RETRY:
  77030. + case FIQ_NP_IN_CSPLIT_RETRY:
  77031. + case FIQ_NP_OUT_CSPLIT_RETRY:
  77032. + fiq_fsm_restart_channel(state, n, 0);
  77033. + break;
  77034. +
  77035. + case FIQ_HS_ISOC_SLEEPING:
  77036. + state->channel[n].fsm = FIQ_HS_ISOC_TURBO;
  77037. + fiq_fsm_restart_channel(state, n, 0);
  77038. + break;
  77039. +
  77040. + case FIQ_PER_SSPLIT_QUEUED:
  77041. + if ((hfnum.b.frnum & 0x7) == 5)
  77042. + break;
  77043. + if(!fiq_fsm_tt_in_use(state, num_channels, n)) {
  77044. + if (!fiq_fsm_too_late(state, n)) {
  77045. + fiq_print(FIQDBG_INT, st, "SOF GO %01d", n);
  77046. + fiq_fsm_restart_channel(state, n, 0);
  77047. + state->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
  77048. + } else {
  77049. + /* Transaction cannot be started without risking a device babble error */
  77050. + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  77051. + state->haintmsk_saved.b2.chint &= ~(1 << n);
  77052. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
  77053. + kick_irq |= 1;
  77054. + }
  77055. + }
  77056. + break;
  77057. +
  77058. + case FIQ_PER_ISO_OUT_PENDING:
  77059. + /* Ordinarily, this should be poked after the SSPLIT
  77060. + * complete interrupt for a competing transfer on the same
  77061. + * TT. Doesn't happen for aborted transactions though.
  77062. + */
  77063. + if ((hfnum.b.frnum & 0x7) >= 5)
  77064. + break;
  77065. + if (!fiq_fsm_tt_in_use(state, num_channels, n)) {
  77066. + /* Hardware bug. SOF can sometimes occur after the channel halt interrupt
  77067. + * that caused this.
  77068. + */
  77069. + fiq_fsm_restart_channel(state, n, 0);
  77070. + fiq_print(FIQDBG_INT, state, "SOF ISOC");
  77071. + if (state->channel[n].nrpackets == 1) {
  77072. + state->channel[n].fsm = FIQ_PER_ISO_OUT_LAST;
  77073. + } else {
  77074. + state->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  77075. + }
  77076. + }
  77077. + break;
  77078. +
  77079. + case FIQ_PER_CSPLIT_WAIT:
  77080. + /* we are guaranteed to be in this state if and only if the SSPLIT interrupt
  77081. + * occurred when the bus transaction occurred. The SOF interrupt reversal bug
  77082. + * will utterly bugger this up though.
  77083. + */
  77084. + if (hfnum.b.frnum != state->channel[n].expected_uframe) {
  77085. + fiq_print(FIQDBG_INT, state, "SOFCS %d ", n);
  77086. + state->channel[n].fsm = FIQ_PER_CSPLIT_POLL;
  77087. + fiq_fsm_restart_channel(state, n, 0);
  77088. + fiq_fsm_start_next_periodic(state, num_channels);
  77089. +
  77090. + }
  77091. + break;
  77092. +
  77093. + case FIQ_PER_SPLIT_TIMEOUT:
  77094. + case FIQ_DEQUEUE_ISSUED:
  77095. + /* Ugly: we have to force a HCD interrupt.
  77096. + * Poke the mask for the channel in question.
  77097. + * We will take a fake SOF because of this, but
  77098. + * that's OK.
  77099. + */
  77100. + state->haintmsk_saved.b2.chint &= ~(1 << n);
  77101. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
  77102. + kick_irq |= 1;
  77103. + break;
  77104. +
  77105. + default:
  77106. + break;
  77107. + }
  77108. + }
  77109. +
  77110. + if (state->kick_np_queues ||
  77111. + dwc_frame_num_le(state->next_sched_frame, hfnum.b.frnum))
  77112. + kick_irq |= 1;
  77113. +
  77114. + return !kick_irq;
  77115. +}
  77116. +
  77117. +
  77118. +/**
  77119. + * fiq_fsm_do_hcintr() - FSM host channel interrupt handler
  77120. + * @state: Pointer to the FIQ state struct
  77121. + * @num_channels: Number of channels as per hardware config
  77122. + * @n: channel for which HAINT(i) was raised
  77123. + *
  77124. + * An important property is that only the CHHLT interrupt is unmasked. Unfortunately, AHBerr is as well.
  77125. + */
  77126. +static int notrace noinline fiq_fsm_do_hcintr(struct fiq_state *state, int num_channels, int n)
  77127. +{
  77128. + hcint_data_t hcint;
  77129. + hcintmsk_data_t hcintmsk;
  77130. + hcint_data_t hcint_probe;
  77131. + hcchar_data_t hcchar;
  77132. + int handled = 0;
  77133. + int restart = 0;
  77134. + int last_csplit = 0;
  77135. + int start_next_periodic = 0;
  77136. + struct fiq_channel_state *st = &state->channel[n];
  77137. + hfnum_data_t hfnum;
  77138. +
  77139. + hcint.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT);
  77140. + hcintmsk.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK);
  77141. + hcint_probe.d32 = hcint.d32 & hcintmsk.d32;
  77142. +
  77143. + if (st->fsm != FIQ_PASSTHROUGH) {
  77144. + fiq_print(FIQDBG_INT, state, "HC%01d ST%02d", n, st->fsm);
  77145. + fiq_print(FIQDBG_INT, state, "%08x", hcint.d32);
  77146. + }
  77147. +
  77148. + switch (st->fsm) {
  77149. +
  77150. + case FIQ_PASSTHROUGH:
  77151. + case FIQ_DEQUEUE_ISSUED:
  77152. + /* doesn't belong to us, kick it upstairs */
  77153. + break;
  77154. +
  77155. + case FIQ_PASSTHROUGH_ERRORSTATE:
  77156. + /* We are here to emulate the error recovery mechanism of the dwc HCD.
  77157. + * Several interrupts are unmasked if a previous transaction failed - it's
  77158. + * death for the FIQ to attempt to handle them as the channel isn't halted.
  77159. + * Emulate what the HCD does in this situation: mask and continue.
  77160. + * The FSM has no other state setup so this has to be handled out-of-band.
  77161. + */
  77162. + fiq_print(FIQDBG_ERR, state, "ERRST %02d", n);
  77163. + if (hcint_probe.b.nak || hcint_probe.b.ack || hcint_probe.b.datatglerr) {
  77164. + fiq_print(FIQDBG_ERR, state, "RESET %02d", n);
  77165. + /* In some random cases we can get a NAK interrupt coincident with a Xacterr
  77166. + * interrupt, after the device has disappeared.
  77167. + */
  77168. + if (!hcint.b.xacterr)
  77169. + st->nr_errors = 0;
  77170. + hcintmsk.b.nak = 0;
  77171. + hcintmsk.b.ack = 0;
  77172. + hcintmsk.b.datatglerr = 0;
  77173. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, hcintmsk.d32);
  77174. + return 1;
  77175. + }
  77176. + if (hcint_probe.b.chhltd) {
  77177. + fiq_print(FIQDBG_ERR, state, "CHHLT %02d", n);
  77178. + fiq_print(FIQDBG_ERR, state, "%08x", hcint.d32);
  77179. + return 0;
  77180. + }
  77181. + break;
  77182. +
  77183. + /* Non-periodic state groups */
  77184. + case FIQ_NP_SSPLIT_STARTED:
  77185. + case FIQ_NP_SSPLIT_RETRY:
  77186. + /* Got a HCINT for a NP SSPLIT. Expected ACK / NAK / fail */
  77187. + if (hcint.b.ack) {
  77188. + /* SSPLIT complete. For OUT, the data has been sent. For IN, the LS transaction
  77189. + * will start shortly. SOF needs to kick the transaction to prevent a NYET flood.
  77190. + */
  77191. + if(st->hcchar_copy.b.epdir == 1)
  77192. + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
  77193. + else
  77194. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  77195. + st->nr_errors = 0;
  77196. + handled = 1;
  77197. + fiq_fsm_setup_csplit(state, n);
  77198. + } else if (hcint.b.nak) {
  77199. + // No buffer space in TT. Retry on a uframe boundary.
  77200. + st->fsm = FIQ_NP_SSPLIT_RETRY;
  77201. + handled = 1;
  77202. + } else if (hcint.b.xacterr) {
  77203. + // The only other one we care about is xacterr. This implies HS bus error - retry.
  77204. + st->nr_errors++;
  77205. + st->fsm = FIQ_NP_SSPLIT_RETRY;
  77206. + if (st->nr_errors >= 3) {
  77207. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  77208. + } else {
  77209. + handled = 1;
  77210. + restart = 1;
  77211. + }
  77212. + } else {
  77213. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  77214. + handled = 0;
  77215. + restart = 0;
  77216. + }
  77217. + break;
  77218. +
  77219. + case FIQ_NP_IN_CSPLIT_RETRY:
  77220. + /* Received a CSPLIT done interrupt.
  77221. + * Expected Data/NAK/STALL/NYET for IN.
  77222. + */
  77223. + if (hcint.b.xfercomp) {
  77224. + /* For IN, data is present. */
  77225. + st->fsm = FIQ_NP_SPLIT_DONE;
  77226. + } else if (hcint.b.nak) {
  77227. + /* no endpoint data. Punt it upstairs */
  77228. + st->fsm = FIQ_NP_SPLIT_DONE;
  77229. + } else if (hcint.b.nyet) {
  77230. + /* CSPLIT NYET - retry on a uframe boundary. */
  77231. + handled = 1;
  77232. + st->nr_errors = 0;
  77233. + } else if (hcint.b.datatglerr) {
  77234. + /* data toggle errors do not set the xfercomp bit. */
  77235. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  77236. + } else if (hcint.b.xacterr) {
  77237. + /* HS error. Retry immediate */
  77238. + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
  77239. + st->nr_errors++;
  77240. + if (st->nr_errors >= 3) {
  77241. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  77242. + } else {
  77243. + handled = 1;
  77244. + restart = 1;
  77245. + }
  77246. + } else if (hcint.b.stall || hcint.b.bblerr) {
  77247. + /* A STALL implies either a LS bus error or a genuine STALL. */
  77248. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  77249. + } else {
  77250. + /* Hardware bug. It's possible in some cases to
  77251. + * get a channel halt with nothing else set when
  77252. + * the response was a NYET. Treat as local 3-strikes retry.
  77253. + */
  77254. + hcint_data_t hcint_test = hcint;
  77255. + hcint_test.b.chhltd = 0;
  77256. + if (!hcint_test.d32) {
  77257. + st->nr_errors++;
  77258. + if (st->nr_errors >= 3) {
  77259. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  77260. + } else {
  77261. + handled = 1;
  77262. + }
  77263. + } else {
  77264. + /* Bail out if something unexpected happened */
  77265. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  77266. + }
  77267. + }
  77268. + break;
  77269. +
  77270. + case FIQ_NP_OUT_CSPLIT_RETRY:
  77271. + /* Received a CSPLIT done interrupt.
  77272. + * Expected ACK/NAK/STALL/NYET/XFERCOMP for OUT.*/
  77273. + if (hcint.b.xfercomp) {
  77274. + st->fsm = FIQ_NP_SPLIT_DONE;
  77275. + } else if (hcint.b.nak) {
  77276. + // The HCD will implement the holdoff on frame boundaries.
  77277. + st->fsm = FIQ_NP_SPLIT_DONE;
  77278. + } else if (hcint.b.nyet) {
  77279. + // Hub still processing.
  77280. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  77281. + handled = 1;
  77282. + st->nr_errors = 0;
  77283. + //restart = 1;
  77284. + } else if (hcint.b.xacterr) {
  77285. + /* HS error. retry immediate */
  77286. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  77287. + st->nr_errors++;
  77288. + if (st->nr_errors >= 3) {
  77289. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  77290. + } else {
  77291. + handled = 1;
  77292. + restart = 1;
  77293. + }
  77294. + } else if (hcint.b.stall) {
  77295. + /* LS bus error or genuine stall */
  77296. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  77297. + } else {
  77298. + /*
  77299. + * Hardware bug. It's possible in some cases to get a
  77300. + * channel halt with nothing else set when the response was a NYET.
  77301. + * Treat as local 3-strikes retry.
  77302. + */
  77303. + hcint_data_t hcint_test = hcint;
  77304. + hcint_test.b.chhltd = 0;
  77305. + if (!hcint_test.d32) {
  77306. + st->nr_errors++;
  77307. + if (st->nr_errors >= 3) {
  77308. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  77309. + } else {
  77310. + handled = 1;
  77311. + }
  77312. + } else {
  77313. + // Something unexpected happened. AHBerror or babble perhaps. Let the IRQ deal with it.
  77314. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  77315. + }
  77316. + }
  77317. + break;
  77318. +
  77319. + /* Periodic split states (except isoc out) */
  77320. + case FIQ_PER_SSPLIT_STARTED:
  77321. + /* Expect an ACK or failure for SSPLIT */
  77322. + if (hcint.b.ack) {
  77323. + /*
  77324. + * SSPLIT transfer complete interrupt - the generation of this interrupt is fraught with bugs.
  77325. + * For a packet queued in microframe n-3 to appear in n-2, if the channel is enabled near the EOF1
  77326. + * point for microframe n-3, the packet will not appear on the bus until microframe n.
  77327. + * Additionally, the generation of the actual interrupt is dodgy. For a packet appearing on the bus
  77328. + * in microframe n, sometimes the interrupt is generated immediately. Sometimes, it appears in n+1
  77329. + * coincident with SOF for n+1.
  77330. + * SOF is also buggy. It can sometimes be raised AFTER the first bus transaction has taken place.
  77331. + * These appear to be caused by timing/clock crossing bugs within the core itself.
  77332. + * State machine workaround.
  77333. + */
  77334. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  77335. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  77336. + fiq_fsm_setup_csplit(state, n);
  77337. + /* Poke the oddfrm bit. If we are equivalent, we received the interrupt at the correct
  77338. + * time. If not, then we're in the next SOF.
  77339. + */
  77340. + if ((hfnum.b.frnum & 0x1) == hcchar.b.oddfrm) {
  77341. + fiq_print(FIQDBG_INT, state, "CSWAIT %01d", n);
  77342. + st->expected_uframe = hfnum.b.frnum;
  77343. + st->fsm = FIQ_PER_CSPLIT_WAIT;
  77344. + } else {
  77345. + fiq_print(FIQDBG_INT, state, "CSPOL %01d", n);
  77346. + /* For isochronous IN endpoints,
  77347. + * we need to hold off if we are expecting a lot of data */
  77348. + if (st->hcchar_copy.b.mps < DATA0_PID_HEURISTIC) {
  77349. + start_next_periodic = 1;
  77350. + }
  77351. + /* Danger will robinson: we are in a broken state. If our first interrupt after
  77352. + * this is a NYET, it will be delayed by 1 uframe and result in an unrecoverable
  77353. + * lag. Unmask the NYET interrupt.
  77354. + */
  77355. + st->expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
  77356. + st->fsm = FIQ_PER_CSPLIT_BROKEN_NYET1;
  77357. + restart = 1;
  77358. + }
  77359. + handled = 1;
  77360. + } else if (hcint.b.xacterr) {
  77361. + /* 3-strikes retry is enabled, we have hit our max nr_errors */
  77362. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  77363. + start_next_periodic = 1;
  77364. + } else {
  77365. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  77366. + start_next_periodic = 1;
  77367. + }
  77368. + /* We can now queue the next isochronous OUT transaction, if one is pending. */
  77369. + if(fiq_fsm_tt_next_isoc(state, num_channels, n)) {
  77370. + fiq_print(FIQDBG_INT, state, "NEXTISO ");
  77371. + }
  77372. + break;
  77373. +
  77374. + case FIQ_PER_CSPLIT_NYET1:
  77375. + /* First CSPLIT attempt was a NYET. If we get a subsequent NYET,
  77376. + * we are too late and the TT has dropped its CSPLIT fifo.
  77377. + */
  77378. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  77379. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  77380. + start_next_periodic = 1;
  77381. + if (hcint.b.nak) {
  77382. + st->fsm = FIQ_PER_SPLIT_DONE;
  77383. + } else if (hcint.b.xfercomp) {
  77384. + fiq_increment_dma_buf(state, num_channels, n);
  77385. + st->fsm = FIQ_PER_CSPLIT_POLL;
  77386. + st->nr_errors = 0;
  77387. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  77388. + handled = 1;
  77389. + restart = 1;
  77390. + if (!last_csplit)
  77391. + start_next_periodic = 0;
  77392. + } else {
  77393. + st->fsm = FIQ_PER_SPLIT_DONE;
  77394. + }
  77395. + } else if (hcint.b.nyet) {
  77396. + /* Doh. Data lost. */
  77397. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  77398. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  77399. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  77400. + } else {
  77401. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  77402. + }
  77403. + break;
  77404. +
  77405. + case FIQ_PER_CSPLIT_BROKEN_NYET1:
  77406. + /*
  77407. + * we got here because our host channel is in the delayed-interrupt
  77408. + * state and we cannot take a NYET interrupt any later than when it
  77409. + * occurred. Disable then re-enable the channel if this happens to force
  77410. + * CSPLITs to occur at the right time.
  77411. + */
  77412. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  77413. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  77414. + fiq_print(FIQDBG_INT, state, "BROK: %01d ", n);
  77415. + if (hcint.b.nak) {
  77416. + st->fsm = FIQ_PER_SPLIT_DONE;
  77417. + start_next_periodic = 1;
  77418. + } else if (hcint.b.xfercomp) {
  77419. + fiq_increment_dma_buf(state, num_channels, n);
  77420. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  77421. + st->fsm = FIQ_PER_CSPLIT_POLL;
  77422. + handled = 1;
  77423. + restart = 1;
  77424. + start_next_periodic = 1;
  77425. + /* Reload HCTSIZ for the next transfer */
  77426. + fiq_fsm_reload_hctsiz(state, n);
  77427. + if (!last_csplit)
  77428. + start_next_periodic = 0;
  77429. + } else {
  77430. + st->fsm = FIQ_PER_SPLIT_DONE;
  77431. + }
  77432. + } else if (hcint.b.nyet) {
  77433. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  77434. + start_next_periodic = 1;
  77435. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  77436. + /* Local 3-strikes retry is handled by the core. This is a ERR response.*/
  77437. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  77438. + } else {
  77439. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  77440. + }
  77441. + break;
  77442. +
  77443. + case FIQ_PER_CSPLIT_POLL:
  77444. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  77445. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  77446. + start_next_periodic = 1;
  77447. + if (hcint.b.nak) {
  77448. + st->fsm = FIQ_PER_SPLIT_DONE;
  77449. + } else if (hcint.b.xfercomp) {
  77450. + fiq_increment_dma_buf(state, num_channels, n);
  77451. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  77452. + handled = 1;
  77453. + restart = 1;
  77454. + /* Reload HCTSIZ for the next transfer */
  77455. + fiq_fsm_reload_hctsiz(state, n);
  77456. + if (!last_csplit)
  77457. + start_next_periodic = 0;
  77458. + } else {
  77459. + st->fsm = FIQ_PER_SPLIT_DONE;
  77460. + }
  77461. + } else if (hcint.b.nyet) {
  77462. + /* Are we a NYET after the first data packet? */
  77463. + if (st->nrpackets == 0) {
  77464. + st->fsm = FIQ_PER_CSPLIT_NYET1;
  77465. + handled = 1;
  77466. + restart = 1;
  77467. + } else {
  77468. + /* We got a NYET when polling CSPLITs. Can happen
  77469. + * if our heuristic fails, or if someone disables us
  77470. + * for any significant length of time.
  77471. + */
  77472. + if (st->nr_errors >= 3) {
  77473. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  77474. + } else {
  77475. + st->fsm = FIQ_PER_SPLIT_DONE;
  77476. + }
  77477. + }
  77478. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  77479. + /* For xacterr, Local 3-strikes retry is handled by the core. This is a ERR response.*/
  77480. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  77481. + } else {
  77482. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  77483. + }
  77484. + break;
  77485. +
  77486. + case FIQ_HS_ISOC_TURBO:
  77487. + if (fiq_fsm_update_hs_isoc(state, n, hcint)) {
  77488. + /* more transactions to come */
  77489. + handled = 1;
  77490. + restart = 1;
  77491. + fiq_print(FIQDBG_INT, state, "HSISO M ");
  77492. + } else {
  77493. + st->fsm = FIQ_HS_ISOC_DONE;
  77494. + fiq_print(FIQDBG_INT, state, "HSISO F ");
  77495. + }
  77496. + break;
  77497. +
  77498. + case FIQ_HS_ISOC_ABORTED:
  77499. + /* This abort is called by the driver rewriting the state mid-transaction
  77500. + * which allows the dequeue mechanism to work more effectively.
  77501. + */
  77502. + break;
  77503. +
  77504. + case FIQ_PER_ISO_OUT_ACTIVE:
  77505. + if (hcint.b.ack) {
  77506. + if(fiq_iso_out_advance(state, num_channels, n)) {
  77507. + /* last OUT transfer */
  77508. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  77509. + /*
  77510. + * Assuming the periodic FIFO in the dwc core
  77511. + * actually does its job properly, we can queue
  77512. + * the next ssplit now and in theory, the wire
  77513. + * transactions will be in-order.
  77514. + */
  77515. + // No it doesn't. It appears to process requests in host channel order.
  77516. + //start_next_periodic = 1;
  77517. + }
  77518. + handled = 1;
  77519. + restart = 1;
  77520. + } else {
  77521. + /*
  77522. + * Isochronous transactions carry on regardless. Log the error
  77523. + * and continue.
  77524. + */
  77525. + //explode += 1;
  77526. + st->nr_errors++;
  77527. + if(fiq_iso_out_advance(state, num_channels, n)) {
  77528. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  77529. + //start_next_periodic = 1;
  77530. + }
  77531. + handled = 1;
  77532. + restart = 1;
  77533. + }
  77534. + break;
  77535. +
  77536. + case FIQ_PER_ISO_OUT_LAST:
  77537. + if (hcint.b.ack) {
  77538. + /* All done here */
  77539. + st->fsm = FIQ_PER_ISO_OUT_DONE;
  77540. + } else {
  77541. + st->fsm = FIQ_PER_ISO_OUT_DONE;
  77542. + st->nr_errors++;
  77543. + }
  77544. + start_next_periodic = 1;
  77545. + break;
  77546. +
  77547. + case FIQ_PER_SPLIT_TIMEOUT:
  77548. + /* SOF kicked us because we overran. */
  77549. + start_next_periodic = 1;
  77550. + break;
  77551. +
  77552. + default:
  77553. + break;
  77554. + }
  77555. +
  77556. + if (handled) {
  77557. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT, hcint.d32);
  77558. + } else {
  77559. + /* Copy the regs into the state so the IRQ knows what to do */
  77560. + st->hcint_copy.d32 = hcint.d32;
  77561. + }
  77562. +
  77563. + if (restart) {
  77564. + /* Restart always implies handled. */
  77565. + if (restart == 2) {
  77566. + /* For complete-split INs, the show must go on.
  77567. + * Force a channel restart */
  77568. + fiq_fsm_restart_channel(state, n, 1);
  77569. + } else {
  77570. + fiq_fsm_restart_channel(state, n, 0);
  77571. + }
  77572. + }
  77573. + if (start_next_periodic) {
  77574. + fiq_fsm_start_next_periodic(state, num_channels);
  77575. + }
  77576. + if (st->fsm != FIQ_PASSTHROUGH)
  77577. + fiq_print(FIQDBG_INT, state, "FSMOUT%02d", st->fsm);
  77578. +
  77579. + return handled;
  77580. +}
  77581. +
  77582. +
  77583. +/**
  77584. + * dwc_otg_fiq_fsm() - Flying State Machine (monster) FIQ
  77585. + * @state: pointer to state struct passed from the banked FIQ mode registers.
  77586. + * @num_channels: set according to the DWC hardware configuration
  77587. + * @dma: pointer to DMA bounce buffers for split transaction slots
  77588. + *
  77589. + * The FSM FIQ performs the low-level tasks that normally would be performed by the microcode
  77590. + * inside an EHCI or similar host controller regarding split transactions. The DWC core
  77591. + * interrupts each and every time a split transaction packet is received or sent successfully.
  77592. + * This results in either an interrupt storm when everything is working "properly", or
  77593. + * the interrupt latency of the system in general breaks time-sensitive periodic split
  77594. + * transactions. Pushing the low-level, but relatively easy state machine work into the FIQ
  77595. + * solves these problems.
  77596. + *
  77597. + * Return: void
  77598. + */
  77599. +void notrace dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels)
  77600. +{
  77601. + gintsts_data_t gintsts, gintsts_handled;
  77602. + gintmsk_data_t gintmsk;
  77603. + //hfnum_data_t hfnum;
  77604. + haint_data_t haint, haint_handled;
  77605. + haintmsk_data_t haintmsk;
  77606. + int kick_irq = 0;
  77607. +
  77608. + gintsts_handled.d32 = 0;
  77609. + haint_handled.d32 = 0;
  77610. +
  77611. + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
  77612. + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
  77613. + gintsts.d32 &= gintmsk.d32;
  77614. +
  77615. + if (gintsts.b.sofintr) {
  77616. + /* For FSM mode, SOF is required to keep the state machine advance for
  77617. + * certain stages of the periodic pipeline. It's death to mask this
  77618. + * interrupt in that case.
  77619. + */
  77620. +
  77621. + if (!fiq_fsm_do_sof(state, num_channels)) {
  77622. + /* Kick IRQ once. Queue advancement means that all pending transactions
  77623. + * will get serviced when the IRQ finally executes.
  77624. + */
  77625. + if (state->gintmsk_saved.b.sofintr == 1)
  77626. + kick_irq |= 1;
  77627. + state->gintmsk_saved.b.sofintr = 0;
  77628. + }
  77629. + gintsts_handled.b.sofintr = 1;
  77630. + }
  77631. +
  77632. + if (gintsts.b.hcintr) {
  77633. + int i;
  77634. + haint.d32 = FIQ_READ(state->dwc_regs_base + HAINT);
  77635. + haintmsk.d32 = FIQ_READ(state->dwc_regs_base + HAINTMSK);
  77636. + haint.d32 &= haintmsk.d32;
  77637. + haint_handled.d32 = 0;
  77638. + for (i=0; i<num_channels; i++) {
  77639. + if (haint.b2.chint & (1 << i)) {
  77640. + if(!fiq_fsm_do_hcintr(state, num_channels, i)) {
  77641. + /* HCINT was not handled in FIQ
  77642. + * HAINT is level-sensitive, leading to level-sensitive ginststs.b.hcint bit.
  77643. + * Mask HAINT(i) but keep top-level hcint unmasked.
  77644. + */
  77645. + state->haintmsk_saved.b2.chint &= ~(1 << i);
  77646. + } else {
  77647. + /* do_hcintr cleaned up after itself, but clear haint */
  77648. + haint_handled.b2.chint |= (1 << i);
  77649. + }
  77650. + }
  77651. + }
  77652. +
  77653. + if (haint_handled.b2.chint) {
  77654. + FIQ_WRITE(state->dwc_regs_base + HAINT, haint_handled.d32);
  77655. + }
  77656. +
  77657. + if (haintmsk.d32 != (haintmsk.d32 & state->haintmsk_saved.d32)) {
  77658. + /*
  77659. + * This is necessary to avoid multiple retriggers of the MPHI in the case
  77660. + * where interrupts are held off and HCINTs start to pile up.
  77661. + * Only wake up the IRQ if a new interrupt came in, was not handled and was
  77662. + * masked.
  77663. + */
  77664. + haintmsk.d32 &= state->haintmsk_saved.d32;
  77665. + FIQ_WRITE(state->dwc_regs_base + HAINTMSK, haintmsk.d32);
  77666. + kick_irq |= 1;
  77667. + }
  77668. + /* Top-Level interrupt - always handled because it's level-sensitive */
  77669. + gintsts_handled.b.hcintr = 1;
  77670. + }
  77671. +
  77672. +
  77673. + /* Clear the bits in the saved register that were not handled but were triggered. */
  77674. + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
  77675. +
  77676. + /* FIQ didn't handle something - mask has changed - write new mask */
  77677. + if (gintmsk.d32 != (gintmsk.d32 & state->gintmsk_saved.d32)) {
  77678. + gintmsk.d32 &= state->gintmsk_saved.d32;
  77679. + gintmsk.b.sofintr = 1;
  77680. + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
  77681. +// fiq_print(FIQDBG_INT, state, "KICKGINT");
  77682. +// fiq_print(FIQDBG_INT, state, "%08x", gintmsk.d32);
  77683. +// fiq_print(FIQDBG_INT, state, "%08x", state->gintmsk_saved.d32);
  77684. + kick_irq |= 1;
  77685. + }
  77686. +
  77687. + if (gintsts_handled.d32) {
  77688. + /* Only applies to edge-sensitive bits in GINTSTS */
  77689. + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
  77690. + }
  77691. +
  77692. + /* We got an interrupt, didn't handle it. */
  77693. + if (kick_irq) {
  77694. + state->mphi_int_count++;
  77695. + FIQ_WRITE(state->mphi_regs.outdda, (int) state->dummy_send);
  77696. + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
  77697. +
  77698. + }
  77699. + state->fiq_done++;
  77700. + mb();
  77701. +}
  77702. +
  77703. +
  77704. +/**
  77705. + * dwc_otg_fiq_nop() - FIQ "lite"
  77706. + * @state: pointer to state struct passed from the banked FIQ mode registers.
  77707. + *
  77708. + * The "nop" handler does not intervene on any interrupts other than SOF.
  77709. + * It is limited in scope to deciding at each SOF if the IRQ SOF handler (which deals
  77710. + * with non-periodic/periodic queues) needs to be kicked.
  77711. + *
  77712. + * This is done to hold off the SOF interrupt, which occurs at a rate of 8000 per second.
  77713. + *
  77714. + * Return: void
  77715. + */
  77716. +void notrace dwc_otg_fiq_nop(struct fiq_state *state)
  77717. +{
  77718. + gintsts_data_t gintsts, gintsts_handled;
  77719. + gintmsk_data_t gintmsk;
  77720. + hfnum_data_t hfnum;
  77721. +
  77722. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  77723. + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
  77724. + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
  77725. + gintsts.d32 &= gintmsk.d32;
  77726. + gintsts_handled.d32 = 0;
  77727. +
  77728. + if (gintsts.b.sofintr) {
  77729. + if (!state->kick_np_queues &&
  77730. + dwc_frame_num_gt(state->next_sched_frame, hfnum.b.frnum)) {
  77731. + /* SOF handled, no work to do, just ACK interrupt */
  77732. + gintsts_handled.b.sofintr = 1;
  77733. + } else {
  77734. + /* Kick IRQ */
  77735. + state->gintmsk_saved.b.sofintr = 0;
  77736. + }
  77737. + }
  77738. +
  77739. + /* Reset handled interrupts */
  77740. + if(gintsts_handled.d32) {
  77741. + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
  77742. + }
  77743. +
  77744. + /* Clear the bits in the saved register that were not handled but were triggered. */
  77745. + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
  77746. +
  77747. + /* We got an interrupt, didn't handle it and want to mask it */
  77748. + if (~(state->gintmsk_saved.d32)) {
  77749. + state->mphi_int_count++;
  77750. + gintmsk.d32 &= state->gintmsk_saved.d32;
  77751. + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
  77752. + /* Force a clear before another dummy send */
  77753. + FIQ_WRITE(state->mphi_regs.intstat, (1<<29));
  77754. + FIQ_WRITE(state->mphi_regs.outdda, (int) state->dummy_send);
  77755. + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
  77756. +
  77757. + }
  77758. + state->fiq_done++;
  77759. + mb();
  77760. +}
  77761. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
  77762. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h 1969-12-31 18:00:00.000000000 -0600
  77763. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h 2014-12-03 19:13:40.220418001 -0600
  77764. @@ -0,0 +1,353 @@
  77765. +/*
  77766. + * dwc_otg_fiq_fsm.h - Finite state machine FIQ header definitions
  77767. + *
  77768. + * Copyright (c) 2013 Raspberry Pi Foundation
  77769. + *
  77770. + * Author: Jonathan Bell <jonathan@raspberrypi.org>
  77771. + * All rights reserved.
  77772. + *
  77773. + * Redistribution and use in source and binary forms, with or without
  77774. + * modification, are permitted provided that the following conditions are met:
  77775. + * * Redistributions of source code must retain the above copyright
  77776. + * notice, this list of conditions and the following disclaimer.
  77777. + * * Redistributions in binary form must reproduce the above copyright
  77778. + * notice, this list of conditions and the following disclaimer in the
  77779. + * documentation and/or other materials provided with the distribution.
  77780. + * * Neither the name of Raspberry Pi nor the
  77781. + * names of its contributors may be used to endorse or promote products
  77782. + * derived from this software without specific prior written permission.
  77783. + *
  77784. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  77785. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  77786. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  77787. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  77788. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  77789. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  77790. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  77791. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  77792. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  77793. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  77794. + *
  77795. + * This FIQ implements functionality that performs split transactions on
  77796. + * the dwc_otg hardware without any outside intervention. A split transaction
  77797. + * is "queued" by nominating a specific host channel to perform the entirety
  77798. + * of a split transaction. This FIQ will then perform the microframe-precise
  77799. + * scheduling required in each phase of the transaction until completion.
  77800. + *
  77801. + * The FIQ functionality has been surgically implanted into the Synopsys
  77802. + * vendor-provided driver.
  77803. + *
  77804. + */
  77805. +
  77806. +#ifndef DWC_OTG_FIQ_FSM_H_
  77807. +#define DWC_OTG_FIQ_FSM_H_
  77808. +
  77809. +#include "dwc_otg_regs.h"
  77810. +#include "dwc_otg_cil.h"
  77811. +#include "dwc_otg_hcd.h"
  77812. +#include <linux/kernel.h>
  77813. +#include <linux/irqflags.h>
  77814. +#include <linux/string.h>
  77815. +#include <asm/barrier.h>
  77816. +
  77817. +#if 0
  77818. +#define FLAME_ON(x) \
  77819. +do { \
  77820. + int gpioreg; \
  77821. + \
  77822. + gpioreg = readl(__io_address(0x20200000+0x8)); \
  77823. + gpioreg &= ~(7 << (x-20)*3); \
  77824. + gpioreg |= 0x1 << (x-20)*3; \
  77825. + writel(gpioreg, __io_address(0x20200000+0x8)); \
  77826. + \
  77827. + writel(1<<x, __io_address(0x20200000+(0x1C))); \
  77828. +} while (0)
  77829. +
  77830. +#define FLAME_OFF(x) \
  77831. +do { \
  77832. + writel(1<<x, __io_address(0x20200000+(0x28))); \
  77833. +} while (0)
  77834. +#else
  77835. +#define FLAME_ON(x) do { } while (0)
  77836. +#define FLAME_OFF(X) do { } while (0)
  77837. +#endif
  77838. +
  77839. +/* This is a quick-and-dirty arch-specific register read/write. We know that
  77840. + * writes to a peripheral on BCM2835 will always arrive in-order, also that
  77841. + * reads and writes are executed in-order therefore the need for memory barriers
  77842. + * is obviated if we're only talking to USB.
  77843. + */
  77844. +#define FIQ_WRITE(_addr_,_data_) (*(volatile unsigned int *) (_addr_) = (_data_))
  77845. +#define FIQ_READ(_addr_) (*(volatile unsigned int *) (_addr_))
  77846. +
  77847. +/* FIQ-ified register definitions. Offsets are from dwc_regs_base. */
  77848. +#define GINTSTS 0x014
  77849. +#define GINTMSK 0x018
  77850. +/* Debug register. Poll the top of the received packets FIFO. */
  77851. +#define GRXSTSR 0x01C
  77852. +#define HFNUM 0x408
  77853. +#define HAINT 0x414
  77854. +#define HAINTMSK 0x418
  77855. +#define HPRT0 0x440
  77856. +
  77857. +/* HC_regs start from an offset of 0x500 */
  77858. +#define HC_START 0x500
  77859. +#define HC_OFFSET 0x020
  77860. +
  77861. +#define HC_DMA 0x514
  77862. +
  77863. +#define HCCHAR 0x00
  77864. +#define HCSPLT 0x04
  77865. +#define HCINT 0x08
  77866. +#define HCINTMSK 0x0C
  77867. +#define HCTSIZ 0x10
  77868. +
  77869. +#define ISOC_XACTPOS_ALL 0b11
  77870. +#define ISOC_XACTPOS_BEGIN 0b10
  77871. +#define ISOC_XACTPOS_MID 0b00
  77872. +#define ISOC_XACTPOS_END 0b01
  77873. +
  77874. +#define DWC_PID_DATA2 0b01
  77875. +#define DWC_PID_MDATA 0b11
  77876. +#define DWC_PID_DATA1 0b10
  77877. +#define DWC_PID_DATA0 0b00
  77878. +
  77879. +typedef struct {
  77880. + volatile void* base;
  77881. + volatile void* ctrl;
  77882. + volatile void* outdda;
  77883. + volatile void* outddb;
  77884. + volatile void* intstat;
  77885. +} mphi_regs_t;
  77886. +
  77887. +
  77888. +enum fiq_debug_level {
  77889. + FIQDBG_SCHED = (1 << 0),
  77890. + FIQDBG_INT = (1 << 1),
  77891. + FIQDBG_ERR = (1 << 2),
  77892. + FIQDBG_PORTHUB = (1 << 3),
  77893. +};
  77894. +
  77895. +struct fiq_state;
  77896. +
  77897. +extern void _fiq_print (enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...);
  77898. +#if 0
  77899. +#define fiq_print _fiq_print
  77900. +#else
  77901. +#define fiq_print(x, y, ...)
  77902. +#endif
  77903. +
  77904. +extern bool fiq_enable, fiq_fsm_enable;
  77905. +extern ushort nak_holdoff;
  77906. +
  77907. +/**
  77908. + * enum fiq_fsm_state - The FIQ FSM states.
  77909. + *
  77910. + * This is the "core" of the FIQ FSM. Broadly, the FSM states follow the
  77911. + * USB2.0 specification for host responses to various transaction states.
  77912. + * There are modifications to this host state machine because of a variety of
  77913. + * quirks and limitations in the dwc_otg hardware.
  77914. + *
  77915. + * The fsm state is also used to communicate back to the driver on completion of
  77916. + * a split transaction. The end states are used in conjunction with the interrupts
  77917. + * raised by the final transaction.
  77918. + */
  77919. +enum fiq_fsm_state {
  77920. + /* FIQ isn't enabled for this host channel */
  77921. + FIQ_PASSTHROUGH = 0,
  77922. + /* For the first interrupt received for this channel,
  77923. + * the FIQ has to ack any interrupts indicating success. */
  77924. + FIQ_PASSTHROUGH_ERRORSTATE = 31,
  77925. + /* Nonperiodic state groups */
  77926. + FIQ_NP_SSPLIT_STARTED = 1,
  77927. + FIQ_NP_SSPLIT_RETRY = 2,
  77928. + FIQ_NP_OUT_CSPLIT_RETRY = 3,
  77929. + FIQ_NP_IN_CSPLIT_RETRY = 4,
  77930. + FIQ_NP_SPLIT_DONE = 5,
  77931. + FIQ_NP_SPLIT_LS_ABORTED = 6,
  77932. + /* This differentiates a HS transaction error from a LS one
  77933. + * (handling the hub state is different) */
  77934. + FIQ_NP_SPLIT_HS_ABORTED = 7,
  77935. +
  77936. + /* Periodic state groups */
  77937. + /* Periodic transactions are either started directly by the IRQ handler
  77938. + * or deferred if the TT is already in use.
  77939. + */
  77940. + FIQ_PER_SSPLIT_QUEUED = 8,
  77941. + FIQ_PER_SSPLIT_STARTED = 9,
  77942. + FIQ_PER_SSPLIT_LAST = 10,
  77943. +
  77944. +
  77945. + FIQ_PER_ISO_OUT_PENDING = 11,
  77946. + FIQ_PER_ISO_OUT_ACTIVE = 12,
  77947. + FIQ_PER_ISO_OUT_LAST = 13,
  77948. + FIQ_PER_ISO_OUT_DONE = 27,
  77949. +
  77950. + FIQ_PER_CSPLIT_WAIT = 14,
  77951. + FIQ_PER_CSPLIT_NYET1 = 15,
  77952. + FIQ_PER_CSPLIT_BROKEN_NYET1 = 28,
  77953. + FIQ_PER_CSPLIT_NYET_FAFF = 29,
  77954. + /* For multiple CSPLITs (large isoc IN, or delayed interrupt) */
  77955. + FIQ_PER_CSPLIT_POLL = 16,
  77956. + /* The last CSPLIT for a transaction has been issued, differentiates
  77957. + * for the state machine to queue the next packet.
  77958. + */
  77959. + FIQ_PER_CSPLIT_LAST = 17,
  77960. +
  77961. + FIQ_PER_SPLIT_DONE = 18,
  77962. + FIQ_PER_SPLIT_LS_ABORTED = 19,
  77963. + FIQ_PER_SPLIT_HS_ABORTED = 20,
  77964. + FIQ_PER_SPLIT_NYET_ABORTED = 21,
  77965. + /* Frame rollover has occurred without the transaction finishing. */
  77966. + FIQ_PER_SPLIT_TIMEOUT = 22,
  77967. +
  77968. + /* FIQ-accelerated HS Isochronous state groups */
  77969. + FIQ_HS_ISOC_TURBO = 23,
  77970. + /* For interval > 1, SOF wakes up the isochronous FSM */
  77971. + FIQ_HS_ISOC_SLEEPING = 24,
  77972. + FIQ_HS_ISOC_DONE = 25,
  77973. + FIQ_HS_ISOC_ABORTED = 26,
  77974. + FIQ_DEQUEUE_ISSUED = 30,
  77975. + FIQ_TEST = 32,
  77976. +};
  77977. +
  77978. +struct fiq_stack {
  77979. + int magic1;
  77980. + uint8_t stack[2048];
  77981. + int magic2;
  77982. +};
  77983. +
  77984. +
  77985. +/**
  77986. + * struct fiq_dma_info - DMA bounce buffer utilisation information (per-channel)
  77987. + * @index: Number of slots reported used for IN transactions / number of slots
  77988. + * transmitted for an OUT transaction
  77989. + * @slot_len[6]: Number of actual transfer bytes in each slot (255 if unused)
  77990. + *
  77991. + * Split transaction transfers can have variable length depending on other bus
  77992. + * traffic. The OTG core DMA engine requires 4-byte aligned addresses therefore
  77993. + * each transaction needs a guaranteed aligned address. A maximum of 6 split transfers
  77994. + * can happen per-frame.
  77995. + */
  77996. +struct fiq_dma_info {
  77997. + u8 index;
  77998. + u8 slot_len[6];
  77999. +};
  78000. +
  78001. +struct __attribute__((packed)) fiq_split_dma_slot {
  78002. + u8 buf[188];
  78003. +};
  78004. +
  78005. +struct fiq_dma_channel {
  78006. + struct __attribute__((packed)) fiq_split_dma_slot index[6];
  78007. +};
  78008. +
  78009. +struct fiq_dma_blob {
  78010. + struct __attribute__((packed)) fiq_dma_channel channel[0];
  78011. +};
  78012. +
  78013. +/**
  78014. + * struct fiq_hs_isoc_info - USB2.0 isochronous data
  78015. + * @iso_frame: Pointer to the array of OTG URB iso_frame_descs.
  78016. + * @nrframes: Total length of iso_frame_desc array
  78017. + * @index: Current index (FIQ-maintained)
  78018. + *
  78019. + */
  78020. +struct fiq_hs_isoc_info {
  78021. + struct dwc_otg_hcd_iso_packet_desc *iso_desc;
  78022. + unsigned int nrframes;
  78023. + unsigned int index;
  78024. +};
  78025. +
  78026. +/**
  78027. + * struct fiq_channel_state - FIQ state machine storage
  78028. + * @fsm: Current state of the channel as understood by the FIQ
  78029. + * @nr_errors: Number of transaction errors on this split-transaction
  78030. + * @hub_addr: SSPLIT/CSPLIT destination hub
  78031. + * @port_addr: SSPLIT/CSPLIT destination port - always 1 if single TT hub
  78032. + * @nrpackets: For isoc OUT, the number of split-OUT packets to transmit. For
  78033. + * split-IN, number of CSPLIT data packets that were received.
  78034. + * @hcchar_copy:
  78035. + * @hcsplt_copy:
  78036. + * @hcintmsk_copy:
  78037. + * @hctsiz_copy: Copies of the host channel registers.
  78038. + * For use as scratch, or for returning state.
  78039. + *
  78040. + * The fiq_channel_state is state storage between interrupts for a host channel. The
  78041. + * FSM state is stored here. Members of this structure must only be set up by the
  78042. + * driver prior to enabling the FIQ for this host channel, and not touched until the FIQ
  78043. + * has updated the state to either a COMPLETE state group or ABORT state group.
  78044. + */
  78045. +
  78046. +struct fiq_channel_state {
  78047. + enum fiq_fsm_state fsm;
  78048. + unsigned int nr_errors;
  78049. + unsigned int hub_addr;
  78050. + unsigned int port_addr;
  78051. + /* Hardware bug workaround: sometimes channel halt interrupts are
  78052. + * delayed until the next SOF. Keep track of when we expected to get interrupted. */
  78053. + unsigned int expected_uframe;
  78054. + /* in/out for communicating number of dma buffers used, or number of ISOC to do */
  78055. + unsigned int nrpackets;
  78056. + struct fiq_dma_info dma_info;
  78057. + struct fiq_hs_isoc_info hs_isoc_info;
  78058. + /* Copies of HC registers - in/out communication from/to IRQ handler
  78059. + * and for ease of channel setup. A bit of mungeing is performed - for
  78060. + * example the hctsiz.b.maxp is _always_ the max packet size of the endpoint.
  78061. + */
  78062. + hcchar_data_t hcchar_copy;
  78063. + hcsplt_data_t hcsplt_copy;
  78064. + hcint_data_t hcint_copy;
  78065. + hcintmsk_data_t hcintmsk_copy;
  78066. + hctsiz_data_t hctsiz_copy;
  78067. + hcdma_data_t hcdma_copy;
  78068. +};
  78069. +
  78070. +/**
  78071. + * struct fiq_state - top-level FIQ state machine storage
  78072. + * @mphi_regs: virtual address of the MPHI peripheral register file
  78073. + * @dwc_regs_base: virtual address of the base of the DWC core register file
  78074. + * @dma_base: physical address for the base of the DMA bounce buffers
  78075. + * @dummy_send: Scratch area for sending a fake message to the MPHI peripheral
  78076. + * @gintmsk_saved: Top-level mask of interrupts that the FIQ has not handled.
  78077. + * Used for determining which interrupts fired to set off the IRQ handler.
  78078. + * @haintmsk_saved: Mask of interrupts from host channels that the FIQ did not handle internally.
  78079. + * @np_count: Non-periodic transactions in the active queue
  78080. + * @np_sent: Count of non-periodic transactions that have completed
  78081. + * @next_sched_frame: For periodic transactions handled by the driver's SOF-driven queuing mechanism,
  78082. + * this is the next frame on which a SOF interrupt is required. Used to hold off
  78083. + * passing SOF through to the driver until necessary.
  78084. + * @channel[n]: Per-channel FIQ state. Allocated during init depending on the number of host
  78085. + * channels configured into the core logic.
  78086. + *
  78087. + * This is passed as the first argument to the dwc_otg_fiq_fsm top-level FIQ handler from the asm stub.
  78088. + * It contains top-level state information.
  78089. + */
  78090. +struct fiq_state {
  78091. + mphi_regs_t mphi_regs;
  78092. + void *dwc_regs_base;
  78093. + dma_addr_t dma_base;
  78094. + struct fiq_dma_blob *fiq_dmab;
  78095. + void *dummy_send;
  78096. + gintmsk_data_t gintmsk_saved;
  78097. + haintmsk_data_t haintmsk_saved;
  78098. + int mphi_int_count;
  78099. + unsigned int fiq_done;
  78100. + unsigned int kick_np_queues;
  78101. + unsigned int next_sched_frame;
  78102. +#ifdef FIQ_DEBUG
  78103. + char * buffer;
  78104. + unsigned int bufsiz;
  78105. +#endif
  78106. + struct fiq_channel_state channel[0];
  78107. +};
  78108. +
  78109. +extern int fiq_fsm_too_late(struct fiq_state *st, int n);
  78110. +
  78111. +extern int fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n);
  78112. +
  78113. +extern void dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels);
  78114. +
  78115. +extern void dwc_otg_fiq_nop(struct fiq_state *state);
  78116. +
  78117. +#endif /* DWC_OTG_FIQ_FSM_H_ */
  78118. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
  78119. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S 1969-12-31 18:00:00.000000000 -0600
  78120. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S 2014-12-03 19:13:40.220418001 -0600
  78121. @@ -0,0 +1,81 @@
  78122. +/*
  78123. + * dwc_otg_fiq_fsm.S - assembly stub for the FSM FIQ
  78124. + *
  78125. + * Copyright (c) 2013 Raspberry Pi Foundation
  78126. + *
  78127. + * Author: Jonathan Bell <jonathan@raspberrypi.org>
  78128. + * All rights reserved.
  78129. + *
  78130. + * Redistribution and use in source and binary forms, with or without
  78131. + * modification, are permitted provided that the following conditions are met:
  78132. + * * Redistributions of source code must retain the above copyright
  78133. + * notice, this list of conditions and the following disclaimer.
  78134. + * * Redistributions in binary form must reproduce the above copyright
  78135. + * notice, this list of conditions and the following disclaimer in the
  78136. + * documentation and/or other materials provided with the distribution.
  78137. + * * Neither the name of Raspberry Pi nor the
  78138. + * names of its contributors may be used to endorse or promote products
  78139. + * derived from this software without specific prior written permission.
  78140. + *
  78141. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  78142. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  78143. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  78144. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  78145. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  78146. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  78147. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  78148. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  78149. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  78150. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  78151. + */
  78152. +
  78153. +
  78154. +#include <asm/assembler.h>
  78155. +#include <linux/linkage.h>
  78156. +
  78157. +
  78158. +.text
  78159. +
  78160. +.global _dwc_otg_fiq_stub_end;
  78161. +
  78162. +/**
  78163. + * _dwc_otg_fiq_stub() - entry copied to the FIQ vector page to allow
  78164. + * a C-style function call with arguments from the FIQ banked registers.
  78165. + * r0 = &hcd->fiq_state
  78166. + * r1 = &hcd->num_channels
  78167. + * r2 = &hcd->dma_buffers
  78168. + * Tramples: r0, r1, r2, r4, fp, ip
  78169. + */
  78170. +
  78171. +ENTRY(_dwc_otg_fiq_stub)
  78172. + /* Stash unbanked regs - SP will have been set up for us */
  78173. + mov ip, sp;
  78174. + stmdb sp!, {r0-r12, lr};
  78175. +#ifdef FIQ_DEBUG
  78176. + // Cycle profiling - read cycle counter at start
  78177. + mrc p15, 0, r5, c15, c12, 1;
  78178. +#endif
  78179. + /* r11 = fp, don't trample it */
  78180. + mov r4, fp;
  78181. + /* set EABI frame size */
  78182. + sub fp, ip, #512;
  78183. +
  78184. + /* for fiq NOP mode - just need state */
  78185. + mov r0, r8;
  78186. + /* r9 = num_channels */
  78187. + mov r1, r9;
  78188. + /* r10 = struct *dma_bufs */
  78189. +// mov r2, r10;
  78190. +
  78191. + /* r4 = &fiq_c_function */
  78192. + blx r4;
  78193. +#ifdef FIQ_DEBUG
  78194. + mrc p15, 0, r4, c15, c12, 1;
  78195. + subs r5, r5, r4;
  78196. + // r5 is now the cycle count time for executing the FIQ. Store it somewhere?
  78197. +#endif
  78198. + ldmia sp!, {r0-r12, lr};
  78199. + subs pc, lr, #4;
  78200. +_dwc_otg_fiq_stub_end:
  78201. +END(_dwc_otg_fiq_stub)
  78202. +
  78203. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_hcd.c linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
  78204. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_hcd.c 1969-12-31 18:00:00.000000000 -0600
  78205. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd.c 2014-12-03 19:13:40.220418001 -0600
  78206. @@ -0,0 +1,4212 @@
  78207. +
  78208. +/* ==========================================================================
  78209. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.c $
  78210. + * $Revision: #104 $
  78211. + * $Date: 2011/10/24 $
  78212. + * $Change: 1871159 $
  78213. + *
  78214. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  78215. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  78216. + * otherwise expressly agreed to in writing between Synopsys and you.
  78217. + *
  78218. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  78219. + * any End User Software License Agreement or Agreement for Licensed Product
  78220. + * with Synopsys or any supplement thereto. You are permitted to use and
  78221. + * redistribute this Software in source and binary forms, with or without
  78222. + * modification, provided that redistributions of source code must retain this
  78223. + * notice. You may not view, use, disclose, copy or distribute this file or
  78224. + * any information contained herein except pursuant to this license grant from
  78225. + * Synopsys. If you do not agree with this notice, including the disclaimer
  78226. + * below, then you are not authorized to use the Software.
  78227. + *
  78228. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  78229. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  78230. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  78231. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  78232. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  78233. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  78234. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  78235. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  78236. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  78237. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  78238. + * DAMAGE.
  78239. + * ========================================================================== */
  78240. +#ifndef DWC_DEVICE_ONLY
  78241. +
  78242. +/** @file
  78243. + * This file implements HCD Core. All code in this file is portable and doesn't
  78244. + * use any OS specific functions.
  78245. + * Interface provided by HCD Core is defined in <code><hcd_if.h></code>
  78246. + * header file.
  78247. + */
  78248. +
  78249. +#include <linux/usb.h>
  78250. +#include <linux/usb/hcd.h>
  78251. +
  78252. +#include "dwc_otg_hcd.h"
  78253. +#include "dwc_otg_regs.h"
  78254. +#include "dwc_otg_fiq_fsm.h"
  78255. +
  78256. +extern bool microframe_schedule;
  78257. +extern uint16_t fiq_fsm_mask, nak_holdoff;
  78258. +
  78259. +//#define DEBUG_HOST_CHANNELS
  78260. +#ifdef DEBUG_HOST_CHANNELS
  78261. +static int last_sel_trans_num_per_scheduled = 0;
  78262. +static int last_sel_trans_num_nonper_scheduled = 0;
  78263. +static int last_sel_trans_num_avail_hc_at_start = 0;
  78264. +static int last_sel_trans_num_avail_hc_at_end = 0;
  78265. +#endif /* DEBUG_HOST_CHANNELS */
  78266. +
  78267. +
  78268. +dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void)
  78269. +{
  78270. + return DWC_ALLOC(sizeof(dwc_otg_hcd_t));
  78271. +}
  78272. +
  78273. +/**
  78274. + * Connection timeout function. An OTG host is required to display a
  78275. + * message if the device does not connect within 10 seconds.
  78276. + */
  78277. +void dwc_otg_hcd_connect_timeout(void *ptr)
  78278. +{
  78279. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, ptr);
  78280. + DWC_PRINTF("Connect Timeout\n");
  78281. + __DWC_ERROR("Device Not Connected/Responding\n");
  78282. +}
  78283. +
  78284. +#if defined(DEBUG)
  78285. +static void dump_channel_info(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78286. +{
  78287. + if (qh->channel != NULL) {
  78288. + dwc_hc_t *hc = qh->channel;
  78289. + dwc_list_link_t *item;
  78290. + dwc_otg_qh_t *qh_item;
  78291. + int num_channels = hcd->core_if->core_params->host_channels;
  78292. + int i;
  78293. +
  78294. + dwc_otg_hc_regs_t *hc_regs;
  78295. + hcchar_data_t hcchar;
  78296. + hcsplt_data_t hcsplt;
  78297. + hctsiz_data_t hctsiz;
  78298. + uint32_t hcdma;
  78299. +
  78300. + hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  78301. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78302. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  78303. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  78304. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  78305. +
  78306. + DWC_PRINTF(" Assigned to channel %p:\n", hc);
  78307. + DWC_PRINTF(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32,
  78308. + hcsplt.d32);
  78309. + DWC_PRINTF(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32,
  78310. + hcdma);
  78311. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  78312. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  78313. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  78314. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  78315. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  78316. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  78317. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  78318. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  78319. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  78320. + DWC_PRINTF(" qh: %p\n", hc->qh);
  78321. + DWC_PRINTF(" NP inactive sched:\n");
  78322. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_inactive) {
  78323. + qh_item =
  78324. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  78325. + DWC_PRINTF(" %p\n", qh_item);
  78326. + }
  78327. + DWC_PRINTF(" NP active sched:\n");
  78328. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_active) {
  78329. + qh_item =
  78330. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  78331. + DWC_PRINTF(" %p\n", qh_item);
  78332. + }
  78333. + DWC_PRINTF(" Channels: \n");
  78334. + for (i = 0; i < num_channels; i++) {
  78335. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  78336. + DWC_PRINTF(" %2d: %p\n", i, hc);
  78337. + }
  78338. + }
  78339. +}
  78340. +#else
  78341. +#define dump_channel_info(hcd, qh)
  78342. +#endif /* DEBUG */
  78343. +
  78344. +/**
  78345. + * Work queue function for starting the HCD when A-Cable is connected.
  78346. + * The hcd_start() must be called in a process context.
  78347. + */
  78348. +static void hcd_start_func(void *_vp)
  78349. +{
  78350. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) _vp;
  78351. +
  78352. + DWC_DEBUGPL(DBG_HCDV, "%s() %p\n", __func__, hcd);
  78353. + if (hcd) {
  78354. + hcd->fops->start(hcd);
  78355. + }
  78356. +}
  78357. +
  78358. +static void del_xfer_timers(dwc_otg_hcd_t * hcd)
  78359. +{
  78360. +#ifdef DEBUG
  78361. + int i;
  78362. + int num_channels = hcd->core_if->core_params->host_channels;
  78363. + for (i = 0; i < num_channels; i++) {
  78364. + DWC_TIMER_CANCEL(hcd->core_if->hc_xfer_timer[i]);
  78365. + }
  78366. +#endif
  78367. +}
  78368. +
  78369. +static void del_timers(dwc_otg_hcd_t * hcd)
  78370. +{
  78371. + del_xfer_timers(hcd);
  78372. + DWC_TIMER_CANCEL(hcd->conn_timer);
  78373. +}
  78374. +
  78375. +/**
  78376. + * Processes all the URBs in a single list of QHs. Completes them with
  78377. + * -ESHUTDOWN and frees the QTD.
  78378. + */
  78379. +static void kill_urbs_in_qh_list(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  78380. +{
  78381. + dwc_list_link_t *qh_item, *qh_tmp;
  78382. + dwc_otg_qh_t *qh;
  78383. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  78384. +
  78385. + DWC_LIST_FOREACH_SAFE(qh_item, qh_tmp, qh_list) {
  78386. + qh = DWC_LIST_ENTRY(qh_item, dwc_otg_qh_t, qh_list_entry);
  78387. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp,
  78388. + &qh->qtd_list, qtd_list_entry) {
  78389. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  78390. + if (qtd->urb != NULL) {
  78391. + hcd->fops->complete(hcd, qtd->urb->priv,
  78392. + qtd->urb, -DWC_E_SHUTDOWN);
  78393. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  78394. + }
  78395. +
  78396. + }
  78397. + if(qh->channel) {
  78398. + /* Using hcchar.chen == 1 is not a reliable test.
  78399. + * It is possible that the channel has already halted
  78400. + * but not yet been through the IRQ handler.
  78401. + */
  78402. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  78403. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  78404. + if(microframe_schedule)
  78405. + hcd->available_host_channels++;
  78406. + qh->channel = NULL;
  78407. + }
  78408. + dwc_otg_hcd_qh_remove(hcd, qh);
  78409. + }
  78410. +}
  78411. +
  78412. +/**
  78413. + * Responds with an error status of ESHUTDOWN to all URBs in the non-periodic
  78414. + * and periodic schedules. The QTD associated with each URB is removed from
  78415. + * the schedule and freed. This function may be called when a disconnect is
  78416. + * detected or when the HCD is being stopped.
  78417. + */
  78418. +static void kill_all_urbs(dwc_otg_hcd_t * hcd)
  78419. +{
  78420. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_inactive);
  78421. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_active);
  78422. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_inactive);
  78423. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_ready);
  78424. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_assigned);
  78425. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_queued);
  78426. +}
  78427. +
  78428. +/**
  78429. + * Start the connection timer. An OTG host is required to display a
  78430. + * message if the device does not connect within 10 seconds. The
  78431. + * timer is deleted if a port connect interrupt occurs before the
  78432. + * timer expires.
  78433. + */
  78434. +static void dwc_otg_hcd_start_connect_timer(dwc_otg_hcd_t * hcd)
  78435. +{
  78436. + DWC_TIMER_SCHEDULE(hcd->conn_timer, 10000 /* 10 secs */ );
  78437. +}
  78438. +
  78439. +/**
  78440. + * HCD Callback function for disconnect of the HCD.
  78441. + *
  78442. + * @param p void pointer to the <code>struct usb_hcd</code>
  78443. + */
  78444. +static int32_t dwc_otg_hcd_session_start_cb(void *p)
  78445. +{
  78446. + dwc_otg_hcd_t *dwc_otg_hcd;
  78447. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  78448. + dwc_otg_hcd = p;
  78449. + dwc_otg_hcd_start_connect_timer(dwc_otg_hcd);
  78450. + return 1;
  78451. +}
  78452. +
  78453. +/**
  78454. + * HCD Callback function for starting the HCD when A-Cable is
  78455. + * connected.
  78456. + *
  78457. + * @param p void pointer to the <code>struct usb_hcd</code>
  78458. + */
  78459. +static int32_t dwc_otg_hcd_start_cb(void *p)
  78460. +{
  78461. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  78462. + dwc_otg_core_if_t *core_if;
  78463. + hprt0_data_t hprt0;
  78464. +
  78465. + core_if = dwc_otg_hcd->core_if;
  78466. +
  78467. + if (core_if->op_state == B_HOST) {
  78468. + /*
  78469. + * Reset the port. During a HNP mode switch the reset
  78470. + * needs to occur within 1ms and have a duration of at
  78471. + * least 50ms.
  78472. + */
  78473. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  78474. + hprt0.b.prtrst = 1;
  78475. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  78476. + }
  78477. + DWC_WORKQ_SCHEDULE_DELAYED(core_if->wq_otg,
  78478. + hcd_start_func, dwc_otg_hcd, 50,
  78479. + "start hcd");
  78480. +
  78481. + return 1;
  78482. +}
  78483. +
  78484. +/**
  78485. + * HCD Callback function for disconnect of the HCD.
  78486. + *
  78487. + * @param p void pointer to the <code>struct usb_hcd</code>
  78488. + */
  78489. +static int32_t dwc_otg_hcd_disconnect_cb(void *p)
  78490. +{
  78491. + gintsts_data_t intr;
  78492. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  78493. +
  78494. + /*
  78495. + * Set status flags for the hub driver.
  78496. + */
  78497. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  78498. + dwc_otg_hcd->flags.b.port_connect_status = 0;
  78499. + if(fiq_enable)
  78500. + local_fiq_disable();
  78501. + /*
  78502. + * Shutdown any transfers in process by clearing the Tx FIFO Empty
  78503. + * interrupt mask and status bits and disabling subsequent host
  78504. + * channel interrupts.
  78505. + */
  78506. + intr.d32 = 0;
  78507. + intr.b.nptxfempty = 1;
  78508. + intr.b.ptxfempty = 1;
  78509. + intr.b.hcintr = 1;
  78510. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk,
  78511. + intr.d32, 0);
  78512. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintsts,
  78513. + intr.d32, 0);
  78514. +
  78515. + del_timers(dwc_otg_hcd);
  78516. +
  78517. + /*
  78518. + * Turn off the vbus power only if the core has transitioned to device
  78519. + * mode. If still in host mode, need to keep power on to detect a
  78520. + * reconnection.
  78521. + */
  78522. + if (dwc_otg_is_device_mode(dwc_otg_hcd->core_if)) {
  78523. + if (dwc_otg_hcd->core_if->op_state != A_SUSPEND) {
  78524. + hprt0_data_t hprt0 = {.d32 = 0 };
  78525. + DWC_PRINTF("Disconnect: PortPower off\n");
  78526. + hprt0.b.prtpwr = 0;
  78527. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0,
  78528. + hprt0.d32);
  78529. + }
  78530. +
  78531. + dwc_otg_disable_host_interrupts(dwc_otg_hcd->core_if);
  78532. + }
  78533. +
  78534. + /* Respond with an error status to all URBs in the schedule. */
  78535. + kill_all_urbs(dwc_otg_hcd);
  78536. +
  78537. + if (dwc_otg_is_host_mode(dwc_otg_hcd->core_if)) {
  78538. + /* Clean up any host channels that were in use. */
  78539. + int num_channels;
  78540. + int i;
  78541. + dwc_hc_t *channel;
  78542. + dwc_otg_hc_regs_t *hc_regs;
  78543. + hcchar_data_t hcchar;
  78544. +
  78545. + num_channels = dwc_otg_hcd->core_if->core_params->host_channels;
  78546. +
  78547. + if (!dwc_otg_hcd->core_if->dma_enable) {
  78548. + /* Flush out any channel requests in slave mode. */
  78549. + for (i = 0; i < num_channels; i++) {
  78550. + channel = dwc_otg_hcd->hc_ptr_array[i];
  78551. + if (DWC_CIRCLEQ_EMPTY_ENTRY
  78552. + (channel, hc_list_entry)) {
  78553. + hc_regs =
  78554. + dwc_otg_hcd->core_if->
  78555. + host_if->hc_regs[i];
  78556. + hcchar.d32 =
  78557. + DWC_READ_REG32(&hc_regs->hcchar);
  78558. + if (hcchar.b.chen) {
  78559. + hcchar.b.chen = 0;
  78560. + hcchar.b.chdis = 1;
  78561. + hcchar.b.epdir = 0;
  78562. + DWC_WRITE_REG32
  78563. + (&hc_regs->hcchar,
  78564. + hcchar.d32);
  78565. + }
  78566. + }
  78567. + }
  78568. + }
  78569. +
  78570. + for (i = 0; i < num_channels; i++) {
  78571. + channel = dwc_otg_hcd->hc_ptr_array[i];
  78572. + if (DWC_CIRCLEQ_EMPTY_ENTRY(channel, hc_list_entry)) {
  78573. + hc_regs =
  78574. + dwc_otg_hcd->core_if->host_if->hc_regs[i];
  78575. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  78576. + if (hcchar.b.chen) {
  78577. + /* Halt the channel. */
  78578. + hcchar.b.chdis = 1;
  78579. + DWC_WRITE_REG32(&hc_regs->hcchar,
  78580. + hcchar.d32);
  78581. + }
  78582. +
  78583. + dwc_otg_hc_cleanup(dwc_otg_hcd->core_if,
  78584. + channel);
  78585. + DWC_CIRCLEQ_INSERT_TAIL
  78586. + (&dwc_otg_hcd->free_hc_list, channel,
  78587. + hc_list_entry);
  78588. + /*
  78589. + * Added for Descriptor DMA to prevent channel double cleanup
  78590. + * in release_channel_ddma(). Which called from ep_disable
  78591. + * when device disconnect.
  78592. + */
  78593. + channel->qh = NULL;
  78594. + }
  78595. + }
  78596. + if(fiq_fsm_enable) {
  78597. + for(i=0; i < 128; i++) {
  78598. + dwc_otg_hcd->hub_port[i] = 0;
  78599. + }
  78600. + }
  78601. +
  78602. + }
  78603. +
  78604. + if(fiq_enable)
  78605. + local_fiq_enable();
  78606. +
  78607. + if (dwc_otg_hcd->fops->disconnect) {
  78608. + dwc_otg_hcd->fops->disconnect(dwc_otg_hcd);
  78609. + }
  78610. +
  78611. + return 1;
  78612. +}
  78613. +
  78614. +/**
  78615. + * HCD Callback function for stopping the HCD.
  78616. + *
  78617. + * @param p void pointer to the <code>struct usb_hcd</code>
  78618. + */
  78619. +static int32_t dwc_otg_hcd_stop_cb(void *p)
  78620. +{
  78621. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  78622. +
  78623. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  78624. + dwc_otg_hcd_stop(dwc_otg_hcd);
  78625. + return 1;
  78626. +}
  78627. +
  78628. +#ifdef CONFIG_USB_DWC_OTG_LPM
  78629. +/**
  78630. + * HCD Callback function for sleep of HCD.
  78631. + *
  78632. + * @param p void pointer to the <code>struct usb_hcd</code>
  78633. + */
  78634. +static int dwc_otg_hcd_sleep_cb(void *p)
  78635. +{
  78636. + dwc_otg_hcd_t *hcd = p;
  78637. +
  78638. + dwc_otg_hcd_free_hc_from_lpm(hcd);
  78639. +
  78640. + return 0;
  78641. +}
  78642. +#endif
  78643. +
  78644. +
  78645. +/**
  78646. + * HCD Callback function for Remote Wakeup.
  78647. + *
  78648. + * @param p void pointer to the <code>struct usb_hcd</code>
  78649. + */
  78650. +static int dwc_otg_hcd_rem_wakeup_cb(void *p)
  78651. +{
  78652. + dwc_otg_hcd_t *hcd = p;
  78653. +
  78654. + if (hcd->core_if->lx_state == DWC_OTG_L2) {
  78655. + hcd->flags.b.port_suspend_change = 1;
  78656. + }
  78657. +#ifdef CONFIG_USB_DWC_OTG_LPM
  78658. + else {
  78659. + hcd->flags.b.port_l1_change = 1;
  78660. + }
  78661. +#endif
  78662. + return 0;
  78663. +}
  78664. +
  78665. +/**
  78666. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  78667. + * stopped.
  78668. + */
  78669. +void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd)
  78670. +{
  78671. + hprt0_data_t hprt0 = {.d32 = 0 };
  78672. +
  78673. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD STOP\n");
  78674. +
  78675. + /*
  78676. + * The root hub should be disconnected before this function is called.
  78677. + * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
  78678. + * and the QH lists (via ..._hcd_endpoint_disable).
  78679. + */
  78680. +
  78681. + /* Turn off all host-specific interrupts. */
  78682. + dwc_otg_disable_host_interrupts(hcd->core_if);
  78683. +
  78684. + /* Turn off the vbus power */
  78685. + DWC_PRINTF("PortPower off\n");
  78686. + hprt0.b.prtpwr = 0;
  78687. + DWC_WRITE_REG32(hcd->core_if->host_if->hprt0, hprt0.d32);
  78688. + dwc_mdelay(1);
  78689. +}
  78690. +
  78691. +int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * hcd,
  78692. + dwc_otg_hcd_urb_t * dwc_otg_urb, void **ep_handle,
  78693. + int atomic_alloc)
  78694. +{
  78695. + int retval = 0;
  78696. + uint8_t needs_scheduling = 0;
  78697. + dwc_otg_transaction_type_e tr_type;
  78698. + dwc_otg_qtd_t *qtd;
  78699. + gintmsk_data_t intr_mask = {.d32 = 0 };
  78700. + hprt0_data_t hprt0 = { .d32 = 0 };
  78701. +
  78702. +#ifdef DEBUG /* integrity checks (Broadcom) */
  78703. + if (NULL == hcd->core_if) {
  78704. + DWC_ERROR("**** DWC OTG HCD URB Enqueue - HCD has NULL core_if\n");
  78705. + /* No longer connected. */
  78706. + return -DWC_E_INVALID;
  78707. + }
  78708. +#endif
  78709. + if (!hcd->flags.b.port_connect_status) {
  78710. + /* No longer connected. */
  78711. + DWC_ERROR("Not connected\n");
  78712. + return -DWC_E_NO_DEVICE;
  78713. + }
  78714. +
  78715. + /* Some core configurations cannot support LS traffic on a FS root port */
  78716. + if ((hcd->fops->speed(hcd, dwc_otg_urb->priv) == USB_SPEED_LOW) &&
  78717. + (hcd->core_if->hwcfg2.b.fs_phy_type == 1) &&
  78718. + (hcd->core_if->hwcfg2.b.hs_phy_type == 1)) {
  78719. + hprt0.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  78720. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_FULL_SPEED) {
  78721. + return -DWC_E_NO_DEVICE;
  78722. + }
  78723. + }
  78724. +
  78725. + qtd = dwc_otg_hcd_qtd_create(dwc_otg_urb, atomic_alloc);
  78726. + if (qtd == NULL) {
  78727. + DWC_ERROR("DWC OTG HCD URB Enqueue failed creating QTD\n");
  78728. + return -DWC_E_NO_MEMORY;
  78729. + }
  78730. +#ifdef DEBUG /* integrity checks (Broadcom) */
  78731. + if (qtd->urb == NULL) {
  78732. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD with no URBs\n");
  78733. + return -DWC_E_NO_MEMORY;
  78734. + }
  78735. + if (qtd->urb->priv == NULL) {
  78736. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD URB with no URB handle\n");
  78737. + return -DWC_E_NO_MEMORY;
  78738. + }
  78739. +#endif
  78740. + intr_mask.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->gintmsk);
  78741. + if(!intr_mask.b.sofintr || fiq_enable) needs_scheduling = 1;
  78742. + if((((dwc_otg_qh_t *)ep_handle)->ep_type == UE_BULK) && !(qtd->urb->flags & URB_GIVEBACK_ASAP))
  78743. + /* Do not schedule SG transactions until qtd has URB_GIVEBACK_ASAP set */
  78744. + needs_scheduling = 0;
  78745. +
  78746. + retval = dwc_otg_hcd_qtd_add(qtd, hcd, (dwc_otg_qh_t **) ep_handle, atomic_alloc);
  78747. + // creates a new queue in ep_handle if it doesn't exist already
  78748. + if (retval < 0) {
  78749. + DWC_ERROR("DWC OTG HCD URB Enqueue failed adding QTD. "
  78750. + "Error status %d\n", retval);
  78751. + dwc_otg_hcd_qtd_free(qtd);
  78752. + return retval;
  78753. + }
  78754. +
  78755. + if(needs_scheduling) {
  78756. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  78757. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  78758. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  78759. + }
  78760. + }
  78761. + return retval;
  78762. +}
  78763. +
  78764. +int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * hcd,
  78765. + dwc_otg_hcd_urb_t * dwc_otg_urb)
  78766. +{
  78767. + dwc_otg_qh_t *qh;
  78768. + dwc_otg_qtd_t *urb_qtd;
  78769. + BUG_ON(!hcd);
  78770. + BUG_ON(!dwc_otg_urb);
  78771. +
  78772. +#ifdef DEBUG /* integrity checks (Broadcom) */
  78773. +
  78774. + if (hcd == NULL) {
  78775. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL HCD\n");
  78776. + return -DWC_E_INVALID;
  78777. + }
  78778. + if (dwc_otg_urb == NULL) {
  78779. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL URB\n");
  78780. + return -DWC_E_INVALID;
  78781. + }
  78782. + if (dwc_otg_urb->qtd == NULL) {
  78783. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with NULL QTD\n");
  78784. + return -DWC_E_INVALID;
  78785. + }
  78786. + urb_qtd = dwc_otg_urb->qtd;
  78787. + BUG_ON(!urb_qtd);
  78788. + if (urb_qtd->qh == NULL) {
  78789. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with QTD with NULL Q handler\n");
  78790. + return -DWC_E_INVALID;
  78791. + }
  78792. +#else
  78793. + urb_qtd = dwc_otg_urb->qtd;
  78794. + BUG_ON(!urb_qtd);
  78795. +#endif
  78796. + qh = urb_qtd->qh;
  78797. + BUG_ON(!qh);
  78798. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  78799. + if (urb_qtd->in_process) {
  78800. + dump_channel_info(hcd, qh);
  78801. + }
  78802. + }
  78803. +#ifdef DEBUG /* integrity checks (Broadcom) */
  78804. + if (hcd->core_if == NULL) {
  78805. + DWC_ERROR("**** DWC OTG HCD URB Dequeue HCD has NULL core_if\n");
  78806. + return -DWC_E_INVALID;
  78807. + }
  78808. +#endif
  78809. + if (urb_qtd->in_process && qh->channel) {
  78810. + /* The QTD is in process (it has been assigned to a channel). */
  78811. + if (hcd->flags.b.port_connect_status) {
  78812. + int n = qh->channel->hc_num;
  78813. + /*
  78814. + * If still connected (i.e. in host mode), halt the
  78815. + * channel so it can be used for other transfers. If
  78816. + * no longer connected, the host registers can't be
  78817. + * written to halt the channel since the core is in
  78818. + * device mode.
  78819. + */
  78820. + /* In FIQ FSM mode, we need to shut down carefully.
  78821. + * The FIQ may attempt to restart a disabled channel */
  78822. + if (fiq_fsm_enable && (hcd->fiq_state->channel[n].fsm != FIQ_PASSTHROUGH)) {
  78823. + qh->channel->halt_status = DWC_OTG_HC_XFER_URB_DEQUEUE;
  78824. + qh->channel->halt_pending = 1;
  78825. + hcd->fiq_state->channel[n].fsm = FIQ_DEQUEUE_ISSUED;
  78826. + } else {
  78827. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  78828. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  78829. + }
  78830. + }
  78831. + }
  78832. +
  78833. + /*
  78834. + * Free the QTD and clean up the associated QH. Leave the QH in the
  78835. + * schedule if it has any remaining QTDs.
  78836. + */
  78837. +
  78838. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue - "
  78839. + "delete %sQueue handler\n",
  78840. + hcd->core_if->dma_desc_enable?"DMA ":"");
  78841. + if (!hcd->core_if->dma_desc_enable) {
  78842. + uint8_t b = urb_qtd->in_process;
  78843. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  78844. + if (b) {
  78845. + dwc_otg_hcd_qh_deactivate(hcd, qh, 0);
  78846. + qh->channel = NULL;
  78847. + } else if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  78848. + dwc_otg_hcd_qh_remove(hcd, qh);
  78849. + }
  78850. + } else {
  78851. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  78852. + }
  78853. + return 0;
  78854. +}
  78855. +
  78856. +int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  78857. + int retry)
  78858. +{
  78859. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  78860. + int retval = 0;
  78861. + dwc_irqflags_t flags;
  78862. +
  78863. + if (retry < 0) {
  78864. + retval = -DWC_E_INVALID;
  78865. + goto done;
  78866. + }
  78867. +
  78868. + if (!qh) {
  78869. + retval = -DWC_E_INVALID;
  78870. + goto done;
  78871. + }
  78872. +
  78873. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  78874. +
  78875. + while (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list) && retry) {
  78876. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  78877. + retry--;
  78878. + dwc_msleep(5);
  78879. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  78880. + }
  78881. +
  78882. + dwc_otg_hcd_qh_remove(hcd, qh);
  78883. +
  78884. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  78885. + /*
  78886. + * Split dwc_otg_hcd_qh_remove_and_free() into qh_remove
  78887. + * and qh_free to prevent stack dump on DWC_DMA_FREE() with
  78888. + * irq_disabled (spinlock_irqsave) in dwc_otg_hcd_desc_list_free()
  78889. + * and dwc_otg_hcd_frame_list_alloc().
  78890. + */
  78891. + dwc_otg_hcd_qh_free(hcd, qh);
  78892. +
  78893. +done:
  78894. + return retval;
  78895. +}
  78896. +
  78897. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  78898. +int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle)
  78899. +{
  78900. + int retval = 0;
  78901. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  78902. + if (!qh)
  78903. + return -DWC_E_INVALID;
  78904. +
  78905. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  78906. + return retval;
  78907. +}
  78908. +#endif
  78909. +
  78910. +/**
  78911. + * HCD Callback structure for handling mode switching.
  78912. + */
  78913. +static dwc_otg_cil_callbacks_t hcd_cil_callbacks = {
  78914. + .start = dwc_otg_hcd_start_cb,
  78915. + .stop = dwc_otg_hcd_stop_cb,
  78916. + .disconnect = dwc_otg_hcd_disconnect_cb,
  78917. + .session_start = dwc_otg_hcd_session_start_cb,
  78918. + .resume_wakeup = dwc_otg_hcd_rem_wakeup_cb,
  78919. +#ifdef CONFIG_USB_DWC_OTG_LPM
  78920. + .sleep = dwc_otg_hcd_sleep_cb,
  78921. +#endif
  78922. + .p = 0,
  78923. +};
  78924. +
  78925. +/**
  78926. + * Reset tasklet function
  78927. + */
  78928. +static void reset_tasklet_func(void *data)
  78929. +{
  78930. + dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *) data;
  78931. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  78932. + hprt0_data_t hprt0;
  78933. +
  78934. + DWC_DEBUGPL(DBG_HCDV, "USB RESET tasklet called\n");
  78935. +
  78936. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  78937. + hprt0.b.prtrst = 1;
  78938. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  78939. + dwc_mdelay(60);
  78940. +
  78941. + hprt0.b.prtrst = 0;
  78942. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  78943. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  78944. +}
  78945. +
  78946. +static void completion_tasklet_func(void *ptr)
  78947. +{
  78948. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) ptr;
  78949. + struct urb *urb;
  78950. + urb_tq_entry_t *item;
  78951. + dwc_irqflags_t flags;
  78952. +
  78953. + /* This could just be spin_lock_irq */
  78954. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  78955. + while (!DWC_TAILQ_EMPTY(&hcd->completed_urb_list)) {
  78956. + item = DWC_TAILQ_FIRST(&hcd->completed_urb_list);
  78957. + urb = item->urb;
  78958. + DWC_TAILQ_REMOVE(&hcd->completed_urb_list, item,
  78959. + urb_tq_entries);
  78960. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  78961. + DWC_FREE(item);
  78962. +
  78963. + usb_hcd_giveback_urb(hcd->priv, urb, urb->status);
  78964. +
  78965. +
  78966. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  78967. + }
  78968. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  78969. + return;
  78970. +}
  78971. +
  78972. +static void qh_list_free(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  78973. +{
  78974. + dwc_list_link_t *item;
  78975. + dwc_otg_qh_t *qh;
  78976. + dwc_irqflags_t flags;
  78977. +
  78978. + if (!qh_list->next) {
  78979. + /* The list hasn't been initialized yet. */
  78980. + return;
  78981. + }
  78982. + /*
  78983. + * Hold spinlock here. Not needed in that case if bellow
  78984. + * function is being called from ISR
  78985. + */
  78986. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  78987. + /* Ensure there are no QTDs or URBs left. */
  78988. + kill_urbs_in_qh_list(hcd, qh_list);
  78989. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  78990. +
  78991. + DWC_LIST_FOREACH(item, qh_list) {
  78992. + qh = DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  78993. + dwc_otg_hcd_qh_remove_and_free(hcd, qh);
  78994. + }
  78995. +}
  78996. +
  78997. +/**
  78998. + * Exit from Hibernation if Host did not detect SRP from connected SRP capable
  78999. + * Device during SRP time by host power up.
  79000. + */
  79001. +void dwc_otg_hcd_power_up(void *ptr)
  79002. +{
  79003. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  79004. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  79005. +
  79006. + DWC_PRINTF("%s called\n", __FUNCTION__);
  79007. +
  79008. + if (!core_if->hibernation_suspend) {
  79009. + DWC_PRINTF("Already exited from Hibernation\n");
  79010. + return;
  79011. + }
  79012. +
  79013. + /* Switch on the voltage to the core */
  79014. + gpwrdn.b.pwrdnswtch = 1;
  79015. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  79016. + dwc_udelay(10);
  79017. +
  79018. + /* Reset the core */
  79019. + gpwrdn.d32 = 0;
  79020. + gpwrdn.b.pwrdnrstn = 1;
  79021. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  79022. + dwc_udelay(10);
  79023. +
  79024. + /* Disable power clamps */
  79025. + gpwrdn.d32 = 0;
  79026. + gpwrdn.b.pwrdnclmp = 1;
  79027. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  79028. +
  79029. + /* Remove reset the core signal */
  79030. + gpwrdn.d32 = 0;
  79031. + gpwrdn.b.pwrdnrstn = 1;
  79032. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  79033. + dwc_udelay(10);
  79034. +
  79035. + /* Disable PMU interrupt */
  79036. + gpwrdn.d32 = 0;
  79037. + gpwrdn.b.pmuintsel = 1;
  79038. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  79039. +
  79040. + core_if->hibernation_suspend = 0;
  79041. +
  79042. + /* Disable PMU */
  79043. + gpwrdn.d32 = 0;
  79044. + gpwrdn.b.pmuactv = 1;
  79045. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  79046. + dwc_udelay(10);
  79047. +
  79048. + /* Enable VBUS */
  79049. + gpwrdn.d32 = 0;
  79050. + gpwrdn.b.dis_vbus = 1;
  79051. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  79052. +
  79053. + core_if->op_state = A_HOST;
  79054. + dwc_otg_core_init(core_if);
  79055. + dwc_otg_enable_global_interrupts(core_if);
  79056. + cil_hcd_start(core_if);
  79057. +}
  79058. +
  79059. +void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num)
  79060. +{
  79061. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  79062. + struct fiq_dma_blob *blob = hcd->fiq_dmab;
  79063. + int i;
  79064. +
  79065. + st->fsm = FIQ_PASSTHROUGH;
  79066. + st->hcchar_copy.d32 = 0;
  79067. + st->hcsplt_copy.d32 = 0;
  79068. + st->hcint_copy.d32 = 0;
  79069. + st->hcintmsk_copy.d32 = 0;
  79070. + st->hctsiz_copy.d32 = 0;
  79071. + st->hcdma_copy.d32 = 0;
  79072. + st->nr_errors = 0;
  79073. + st->hub_addr = 0;
  79074. + st->port_addr = 0;
  79075. + st->expected_uframe = 0;
  79076. + st->nrpackets = 0;
  79077. + st->dma_info.index = 0;
  79078. + for (i = 0; i < 6; i++)
  79079. + st->dma_info.slot_len[i] = 255;
  79080. + st->hs_isoc_info.index = 0;
  79081. + st->hs_isoc_info.iso_desc = NULL;
  79082. + st->hs_isoc_info.nrframes = 0;
  79083. +
  79084. + DWC_MEMSET(&blob->channel[num].index[0], 0x6b, 1128);
  79085. +}
  79086. +
  79087. +/**
  79088. + * Frees secondary storage associated with the dwc_otg_hcd structure contained
  79089. + * in the struct usb_hcd field.
  79090. + */
  79091. +static void dwc_otg_hcd_free(dwc_otg_hcd_t * dwc_otg_hcd)
  79092. +{
  79093. + int i;
  79094. +
  79095. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD FREE\n");
  79096. +
  79097. + del_timers(dwc_otg_hcd);
  79098. +
  79099. + /* Free memory for QH/QTD lists */
  79100. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_inactive);
  79101. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_active);
  79102. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_inactive);
  79103. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_ready);
  79104. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_assigned);
  79105. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_queued);
  79106. +
  79107. + /* Free memory for the host channels. */
  79108. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  79109. + dwc_hc_t *hc = dwc_otg_hcd->hc_ptr_array[i];
  79110. +
  79111. +#ifdef DEBUG
  79112. + if (dwc_otg_hcd->core_if->hc_xfer_timer[i]) {
  79113. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->hc_xfer_timer[i]);
  79114. + }
  79115. +#endif
  79116. + if (hc != NULL) {
  79117. + DWC_DEBUGPL(DBG_HCDV, "HCD Free channel #%i, hc=%p\n",
  79118. + i, hc);
  79119. + DWC_FREE(hc);
  79120. + }
  79121. + }
  79122. +
  79123. + if (dwc_otg_hcd->core_if->dma_enable) {
  79124. + if (dwc_otg_hcd->status_buf_dma) {
  79125. + DWC_DMA_FREE(DWC_OTG_HCD_STATUS_BUF_SIZE,
  79126. + dwc_otg_hcd->status_buf,
  79127. + dwc_otg_hcd->status_buf_dma);
  79128. + }
  79129. + } else if (dwc_otg_hcd->status_buf != NULL) {
  79130. + DWC_FREE(dwc_otg_hcd->status_buf);
  79131. + }
  79132. + DWC_SPINLOCK_FREE(dwc_otg_hcd->channel_lock);
  79133. + DWC_SPINLOCK_FREE(dwc_otg_hcd->lock);
  79134. + /* Set core_if's lock pointer to NULL */
  79135. + dwc_otg_hcd->core_if->lock = NULL;
  79136. +
  79137. + DWC_TIMER_FREE(dwc_otg_hcd->conn_timer);
  79138. + DWC_TASK_FREE(dwc_otg_hcd->reset_tasklet);
  79139. + DWC_TASK_FREE(dwc_otg_hcd->completion_tasklet);
  79140. + DWC_FREE(dwc_otg_hcd->fiq_state);
  79141. +
  79142. +#ifdef DWC_DEV_SRPCAP
  79143. + if (dwc_otg_hcd->core_if->power_down == 2 &&
  79144. + dwc_otg_hcd->core_if->pwron_timer) {
  79145. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->pwron_timer);
  79146. + }
  79147. +#endif
  79148. + DWC_FREE(dwc_otg_hcd);
  79149. +}
  79150. +
  79151. +int init_hcd_usecs(dwc_otg_hcd_t *_hcd);
  79152. +
  79153. +int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if)
  79154. +{
  79155. + int retval = 0;
  79156. + int num_channels;
  79157. + int i;
  79158. + dwc_hc_t *channel;
  79159. +
  79160. + hcd->lock = DWC_SPINLOCK_ALLOC();
  79161. + hcd->channel_lock = DWC_SPINLOCK_ALLOC();
  79162. + DWC_DEBUGPL(DBG_HCDV, "init of HCD %p given core_if %p\n",
  79163. + hcd, core_if);
  79164. + if (!hcd->lock) {
  79165. + DWC_ERROR("Could not allocate lock for pcd");
  79166. + DWC_FREE(hcd);
  79167. + retval = -DWC_E_NO_MEMORY;
  79168. + goto out;
  79169. + }
  79170. + hcd->core_if = core_if;
  79171. +
  79172. + /* Register the HCD CIL Callbacks */
  79173. + dwc_otg_cil_register_hcd_callbacks(hcd->core_if,
  79174. + &hcd_cil_callbacks, hcd);
  79175. +
  79176. + /* Initialize the non-periodic schedule. */
  79177. + DWC_LIST_INIT(&hcd->non_periodic_sched_inactive);
  79178. + DWC_LIST_INIT(&hcd->non_periodic_sched_active);
  79179. +
  79180. + /* Initialize the periodic schedule. */
  79181. + DWC_LIST_INIT(&hcd->periodic_sched_inactive);
  79182. + DWC_LIST_INIT(&hcd->periodic_sched_ready);
  79183. + DWC_LIST_INIT(&hcd->periodic_sched_assigned);
  79184. + DWC_LIST_INIT(&hcd->periodic_sched_queued);
  79185. + DWC_TAILQ_INIT(&hcd->completed_urb_list);
  79186. + /*
  79187. + * Create a host channel descriptor for each host channel implemented
  79188. + * in the controller. Initialize the channel descriptor array.
  79189. + */
  79190. + DWC_CIRCLEQ_INIT(&hcd->free_hc_list);
  79191. + num_channels = hcd->core_if->core_params->host_channels;
  79192. + DWC_MEMSET(hcd->hc_ptr_array, 0, sizeof(hcd->hc_ptr_array));
  79193. + for (i = 0; i < num_channels; i++) {
  79194. + channel = DWC_ALLOC(sizeof(dwc_hc_t));
  79195. + if (channel == NULL) {
  79196. + retval = -DWC_E_NO_MEMORY;
  79197. + DWC_ERROR("%s: host channel allocation failed\n",
  79198. + __func__);
  79199. + dwc_otg_hcd_free(hcd);
  79200. + goto out;
  79201. + }
  79202. + channel->hc_num = i;
  79203. + hcd->hc_ptr_array[i] = channel;
  79204. +#ifdef DEBUG
  79205. + hcd->core_if->hc_xfer_timer[i] =
  79206. + DWC_TIMER_ALLOC("hc timer", hc_xfer_timeout,
  79207. + &hcd->core_if->hc_xfer_info[i]);
  79208. +#endif
  79209. + DWC_DEBUGPL(DBG_HCDV, "HCD Added channel #%d, hc=%p\n", i,
  79210. + channel);
  79211. + }
  79212. +
  79213. + if (fiq_enable) {
  79214. + hcd->fiq_state = DWC_ALLOC(sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels));
  79215. + if (!hcd->fiq_state) {
  79216. + retval = -DWC_E_NO_MEMORY;
  79217. + DWC_ERROR("%s: cannot allocate fiq_state structure\n", __func__);
  79218. + dwc_otg_hcd_free(hcd);
  79219. + goto out;
  79220. + }
  79221. + DWC_MEMSET(hcd->fiq_state, 0, (sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels)));
  79222. +
  79223. + for (i = 0; i < num_channels; i++) {
  79224. + hcd->fiq_state->channel[i].fsm = FIQ_PASSTHROUGH;
  79225. + }
  79226. + hcd->fiq_state->dummy_send = DWC_ALLOC_ATOMIC(16);
  79227. +
  79228. + hcd->fiq_stack = DWC_ALLOC(sizeof(struct fiq_stack));
  79229. + if (!hcd->fiq_stack) {
  79230. + retval = -DWC_E_NO_MEMORY;
  79231. + DWC_ERROR("%s: cannot allocate fiq_stack structure\n", __func__);
  79232. + dwc_otg_hcd_free(hcd);
  79233. + goto out;
  79234. + }
  79235. + hcd->fiq_stack->magic1 = 0xDEADBEEF;
  79236. + hcd->fiq_stack->magic2 = 0xD00DFEED;
  79237. + hcd->fiq_state->gintmsk_saved.d32 = ~0;
  79238. + hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
  79239. +
  79240. + /* This bit is terrible and uses no API, but necessary. The FIQ has no concept of DMA pools
  79241. + * (and if it did, would be a lot slower). This allocates a chunk of memory (~9kiB for 8 host channels)
  79242. + * for use as transaction bounce buffers in a 2-D array. Our access into this chunk is done by some
  79243. + * moderately readable array casts.
  79244. + */
  79245. + hcd->fiq_dmab = DWC_DMA_ALLOC((sizeof(struct fiq_dma_channel) * num_channels), &hcd->fiq_state->dma_base);
  79246. + DWC_WARN("FIQ DMA bounce buffers: virt = 0x%08x dma = 0x%08x len=%d",
  79247. + (unsigned int)hcd->fiq_dmab, (unsigned int)hcd->fiq_state->dma_base,
  79248. + sizeof(struct fiq_dma_channel) * num_channels);
  79249. +
  79250. + DWC_MEMSET(hcd->fiq_dmab, 0x6b, 9024);
  79251. +
  79252. + /* pointer for debug in fiq_print */
  79253. + hcd->fiq_state->fiq_dmab = hcd->fiq_dmab;
  79254. + if (fiq_fsm_enable) {
  79255. + int i;
  79256. + for (i=0; i < hcd->core_if->core_params->host_channels; i++) {
  79257. + dwc_otg_cleanup_fiq_channel(hcd, i);
  79258. + }
  79259. + DWC_PRINTF("FIQ FSM acceleration enabled for :\n%s%s%s%s",
  79260. + (fiq_fsm_mask & 0x1) ? "Non-periodic Split Transactions\n" : "",
  79261. + (fiq_fsm_mask & 0x2) ? "Periodic Split Transactions\n" : "",
  79262. + (fiq_fsm_mask & 0x4) ? "High-Speed Isochronous Endpoints\n" : "",
  79263. + (fiq_fsm_mask & 0x8) ? "Interrupt/Control Split Transaction hack enabled\n" : "");
  79264. + }
  79265. + }
  79266. +
  79267. + /* Initialize the Connection timeout timer. */
  79268. + hcd->conn_timer = DWC_TIMER_ALLOC("Connection timer",
  79269. + dwc_otg_hcd_connect_timeout, 0);
  79270. +
  79271. + printk(KERN_DEBUG "dwc_otg: Microframe scheduler %s\n", microframe_schedule ? "enabled":"disabled");
  79272. + if (microframe_schedule)
  79273. + init_hcd_usecs(hcd);
  79274. +
  79275. + /* Initialize reset tasklet. */
  79276. + hcd->reset_tasklet = DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
  79277. +
  79278. + hcd->completion_tasklet = DWC_TASK_ALLOC("completion_tasklet",
  79279. + completion_tasklet_func, hcd);
  79280. +#ifdef DWC_DEV_SRPCAP
  79281. + if (hcd->core_if->power_down == 2) {
  79282. + /* Initialize Power on timer for Host power up in case hibernation */
  79283. + hcd->core_if->pwron_timer = DWC_TIMER_ALLOC("PWRON TIMER",
  79284. + dwc_otg_hcd_power_up, core_if);
  79285. + }
  79286. +#endif
  79287. +
  79288. + /*
  79289. + * Allocate space for storing data on status transactions. Normally no
  79290. + * data is sent, but this space acts as a bit bucket. This must be
  79291. + * done after usb_add_hcd since that function allocates the DMA buffer
  79292. + * pool.
  79293. + */
  79294. + if (hcd->core_if->dma_enable) {
  79295. + hcd->status_buf =
  79296. + DWC_DMA_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE,
  79297. + &hcd->status_buf_dma);
  79298. + } else {
  79299. + hcd->status_buf = DWC_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE);
  79300. + }
  79301. + if (!hcd->status_buf) {
  79302. + retval = -DWC_E_NO_MEMORY;
  79303. + DWC_ERROR("%s: status_buf allocation failed\n", __func__);
  79304. + dwc_otg_hcd_free(hcd);
  79305. + goto out;
  79306. + }
  79307. +
  79308. + hcd->otg_port = 1;
  79309. + hcd->frame_list = NULL;
  79310. + hcd->frame_list_dma = 0;
  79311. + hcd->periodic_qh_count = 0;
  79312. +
  79313. + DWC_MEMSET(hcd->hub_port, 0, sizeof(hcd->hub_port));
  79314. +#ifdef FIQ_DEBUG
  79315. + DWC_MEMSET(hcd->hub_port_alloc, -1, sizeof(hcd->hub_port_alloc));
  79316. +#endif
  79317. +
  79318. +out:
  79319. + return retval;
  79320. +}
  79321. +
  79322. +void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd)
  79323. +{
  79324. + /* Turn off all host-specific interrupts. */
  79325. + dwc_otg_disable_host_interrupts(hcd->core_if);
  79326. +
  79327. + dwc_otg_hcd_free(hcd);
  79328. +}
  79329. +
  79330. +/**
  79331. + * Initializes dynamic portions of the DWC_otg HCD state.
  79332. + */
  79333. +static void dwc_otg_hcd_reinit(dwc_otg_hcd_t * hcd)
  79334. +{
  79335. + int num_channels;
  79336. + int i;
  79337. + dwc_hc_t *channel;
  79338. + dwc_hc_t *channel_tmp;
  79339. +
  79340. + hcd->flags.d32 = 0;
  79341. +
  79342. + hcd->non_periodic_qh_ptr = &hcd->non_periodic_sched_active;
  79343. + if (!microframe_schedule) {
  79344. + hcd->non_periodic_channels = 0;
  79345. + hcd->periodic_channels = 0;
  79346. + } else {
  79347. + hcd->available_host_channels = hcd->core_if->core_params->host_channels;
  79348. + }
  79349. + /*
  79350. + * Put all channels in the free channel list and clean up channel
  79351. + * states.
  79352. + */
  79353. + DWC_CIRCLEQ_FOREACH_SAFE(channel, channel_tmp,
  79354. + &hcd->free_hc_list, hc_list_entry) {
  79355. + DWC_CIRCLEQ_REMOVE(&hcd->free_hc_list, channel, hc_list_entry);
  79356. + }
  79357. +
  79358. + num_channels = hcd->core_if->core_params->host_channels;
  79359. + for (i = 0; i < num_channels; i++) {
  79360. + channel = hcd->hc_ptr_array[i];
  79361. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, channel,
  79362. + hc_list_entry);
  79363. + dwc_otg_hc_cleanup(hcd->core_if, channel);
  79364. + }
  79365. +
  79366. + /* Initialize the DWC core for host mode operation. */
  79367. + dwc_otg_core_host_init(hcd->core_if);
  79368. +
  79369. + /* Set core_if's lock pointer to the hcd->lock */
  79370. + hcd->core_if->lock = hcd->lock;
  79371. +}
  79372. +
  79373. +/**
  79374. + * Assigns transactions from a QTD to a free host channel and initializes the
  79375. + * host channel to perform the transactions. The host channel is removed from
  79376. + * the free list.
  79377. + *
  79378. + * @param hcd The HCD state structure.
  79379. + * @param qh Transactions from the first QTD for this QH are selected and
  79380. + * assigned to a free host channel.
  79381. + */
  79382. +static void assign_and_init_hc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  79383. +{
  79384. + dwc_hc_t *hc;
  79385. + dwc_otg_qtd_t *qtd;
  79386. + dwc_otg_hcd_urb_t *urb;
  79387. + void* ptr = NULL;
  79388. +
  79389. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  79390. +
  79391. + urb = qtd->urb;
  79392. +
  79393. + DWC_DEBUGPL(DBG_HCDV, "%s(%p,%p) - urb %x, actual_length %d\n", __func__, hcd, qh, (unsigned int)urb, urb->actual_length);
  79394. +
  79395. + if (((urb->actual_length < 0) || (urb->actual_length > urb->length)) && !dwc_otg_hcd_is_pipe_in(&urb->pipe_info))
  79396. + urb->actual_length = urb->length;
  79397. +
  79398. +
  79399. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  79400. +
  79401. + /* Remove the host channel from the free list. */
  79402. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  79403. +
  79404. + qh->channel = hc;
  79405. +
  79406. + qtd->in_process = 1;
  79407. +
  79408. + /*
  79409. + * Use usb_pipedevice to determine device address. This address is
  79410. + * 0 before the SET_ADDRESS command and the correct address afterward.
  79411. + */
  79412. + hc->dev_addr = dwc_otg_hcd_get_dev_addr(&urb->pipe_info);
  79413. + hc->ep_num = dwc_otg_hcd_get_ep_num(&urb->pipe_info);
  79414. + hc->speed = qh->dev_speed;
  79415. + hc->max_packet = dwc_max_packet(qh->maxp);
  79416. +
  79417. + hc->xfer_started = 0;
  79418. + hc->halt_status = DWC_OTG_HC_XFER_NO_HALT_STATUS;
  79419. + hc->error_state = (qtd->error_count > 0);
  79420. + hc->halt_on_queue = 0;
  79421. + hc->halt_pending = 0;
  79422. + hc->requests = 0;
  79423. +
  79424. + /*
  79425. + * The following values may be modified in the transfer type section
  79426. + * below. The xfer_len value may be reduced when the transfer is
  79427. + * started to accommodate the max widths of the XferSize and PktCnt
  79428. + * fields in the HCTSIZn register.
  79429. + */
  79430. +
  79431. + hc->ep_is_in = (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) != 0);
  79432. + if (hc->ep_is_in) {
  79433. + hc->do_ping = 0;
  79434. + } else {
  79435. + hc->do_ping = qh->ping_state;
  79436. + }
  79437. +
  79438. + hc->data_pid_start = qh->data_toggle;
  79439. + hc->multi_count = 1;
  79440. +
  79441. + if (hcd->core_if->dma_enable) {
  79442. + hc->xfer_buff = (uint8_t *) urb->dma + urb->actual_length;
  79443. +
  79444. + /* For non-dword aligned case */
  79445. + if (((unsigned long)hc->xfer_buff & 0x3)
  79446. + && !hcd->core_if->dma_desc_enable) {
  79447. + ptr = (uint8_t *) urb->buf + urb->actual_length;
  79448. + }
  79449. + } else {
  79450. + hc->xfer_buff = (uint8_t *) urb->buf + urb->actual_length;
  79451. + }
  79452. + hc->xfer_len = urb->length - urb->actual_length;
  79453. + hc->xfer_count = 0;
  79454. +
  79455. + /*
  79456. + * Set the split attributes
  79457. + */
  79458. + hc->do_split = 0;
  79459. + if (qh->do_split) {
  79460. + uint32_t hub_addr, port_addr;
  79461. + hc->do_split = 1;
  79462. + hc->xact_pos = qtd->isoc_split_pos;
  79463. + /* We don't need to do complete splits anymore */
  79464. +// if(fiq_fsm_enable)
  79465. + if (0)
  79466. + hc->complete_split = qtd->complete_split = 0;
  79467. + else
  79468. + hc->complete_split = qtd->complete_split;
  79469. +
  79470. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &port_addr);
  79471. + hc->hub_addr = (uint8_t) hub_addr;
  79472. + hc->port_addr = (uint8_t) port_addr;
  79473. + }
  79474. +
  79475. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  79476. + case UE_CONTROL:
  79477. + hc->ep_type = DWC_OTG_EP_TYPE_CONTROL;
  79478. + switch (qtd->control_phase) {
  79479. + case DWC_OTG_CONTROL_SETUP:
  79480. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction\n");
  79481. + hc->do_ping = 0;
  79482. + hc->ep_is_in = 0;
  79483. + hc->data_pid_start = DWC_OTG_HC_PID_SETUP;
  79484. + if (hcd->core_if->dma_enable) {
  79485. + hc->xfer_buff = (uint8_t *) urb->setup_dma;
  79486. + } else {
  79487. + hc->xfer_buff = (uint8_t *) urb->setup_packet;
  79488. + }
  79489. + hc->xfer_len = 8;
  79490. + ptr = NULL;
  79491. + break;
  79492. + case DWC_OTG_CONTROL_DATA:
  79493. + DWC_DEBUGPL(DBG_HCDV, " Control data transaction\n");
  79494. + hc->data_pid_start = qtd->data_toggle;
  79495. + break;
  79496. + case DWC_OTG_CONTROL_STATUS:
  79497. + /*
  79498. + * Direction is opposite of data direction or IN if no
  79499. + * data.
  79500. + */
  79501. + DWC_DEBUGPL(DBG_HCDV, " Control status transaction\n");
  79502. + if (urb->length == 0) {
  79503. + hc->ep_is_in = 1;
  79504. + } else {
  79505. + hc->ep_is_in =
  79506. + dwc_otg_hcd_is_pipe_out(&urb->pipe_info);
  79507. + }
  79508. + if (hc->ep_is_in) {
  79509. + hc->do_ping = 0;
  79510. + }
  79511. +
  79512. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  79513. +
  79514. + hc->xfer_len = 0;
  79515. + if (hcd->core_if->dma_enable) {
  79516. + hc->xfer_buff = (uint8_t *) hcd->status_buf_dma;
  79517. + } else {
  79518. + hc->xfer_buff = (uint8_t *) hcd->status_buf;
  79519. + }
  79520. + ptr = NULL;
  79521. + break;
  79522. + }
  79523. + break;
  79524. + case UE_BULK:
  79525. + hc->ep_type = DWC_OTG_EP_TYPE_BULK;
  79526. + break;
  79527. + case UE_INTERRUPT:
  79528. + hc->ep_type = DWC_OTG_EP_TYPE_INTR;
  79529. + break;
  79530. + case UE_ISOCHRONOUS:
  79531. + {
  79532. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  79533. +
  79534. + hc->ep_type = DWC_OTG_EP_TYPE_ISOC;
  79535. +
  79536. + if (hcd->core_if->dma_desc_enable)
  79537. + break;
  79538. +
  79539. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  79540. +
  79541. + frame_desc->status = 0;
  79542. +
  79543. + if (hcd->core_if->dma_enable) {
  79544. + hc->xfer_buff = (uint8_t *) urb->dma;
  79545. + } else {
  79546. + hc->xfer_buff = (uint8_t *) urb->buf;
  79547. + }
  79548. + hc->xfer_buff +=
  79549. + frame_desc->offset + qtd->isoc_split_offset;
  79550. + hc->xfer_len =
  79551. + frame_desc->length - qtd->isoc_split_offset;
  79552. +
  79553. + /* For non-dword aligned buffers */
  79554. + if (((unsigned long)hc->xfer_buff & 0x3)
  79555. + && hcd->core_if->dma_enable) {
  79556. + ptr =
  79557. + (uint8_t *) urb->buf + frame_desc->offset +
  79558. + qtd->isoc_split_offset;
  79559. + } else
  79560. + ptr = NULL;
  79561. +
  79562. + if (hc->xact_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  79563. + if (hc->xfer_len <= 188) {
  79564. + hc->xact_pos = DWC_HCSPLIT_XACTPOS_ALL;
  79565. + } else {
  79566. + hc->xact_pos =
  79567. + DWC_HCSPLIT_XACTPOS_BEGIN;
  79568. + }
  79569. + }
  79570. + }
  79571. + break;
  79572. + }
  79573. + /* non DWORD-aligned buffer case */
  79574. + if (ptr) {
  79575. + uint32_t buf_size;
  79576. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  79577. + buf_size = hcd->core_if->core_params->max_transfer_size;
  79578. + } else {
  79579. + buf_size = 4096;
  79580. + }
  79581. + if (!qh->dw_align_buf) {
  79582. + qh->dw_align_buf = DWC_DMA_ALLOC_ATOMIC(buf_size,
  79583. + &qh->dw_align_buf_dma);
  79584. + if (!qh->dw_align_buf) {
  79585. + DWC_ERROR
  79586. + ("%s: Failed to allocate memory to handle "
  79587. + "non-dword aligned buffer case\n",
  79588. + __func__);
  79589. + return;
  79590. + }
  79591. + }
  79592. + if (!hc->ep_is_in) {
  79593. + dwc_memcpy(qh->dw_align_buf, ptr, hc->xfer_len);
  79594. + }
  79595. + hc->align_buff = qh->dw_align_buf_dma;
  79596. + } else {
  79597. + hc->align_buff = 0;
  79598. + }
  79599. +
  79600. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  79601. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  79602. + /*
  79603. + * This value may be modified when the transfer is started to
  79604. + * reflect the actual transfer length.
  79605. + */
  79606. + hc->multi_count = dwc_hb_mult(qh->maxp);
  79607. + }
  79608. +
  79609. + if (hcd->core_if->dma_desc_enable)
  79610. + hc->desc_list_addr = qh->desc_list_dma;
  79611. +
  79612. + dwc_otg_hc_init(hcd->core_if, hc);
  79613. + hc->qh = qh;
  79614. +}
  79615. +
  79616. +
  79617. +/**
  79618. + * fiq_fsm_transaction_suitable() - Test a QH for compatibility with the FIQ
  79619. + * @qh: pointer to the endpoint's queue head
  79620. + *
  79621. + * Transaction start/end control flow is grafted onto the existing dwc_otg
  79622. + * mechanisms, to avoid spaghettifying the functions more than they already are.
  79623. + * This function's eligibility check is altered by debug parameter.
  79624. + *
  79625. + * Returns: 0 for unsuitable, 1 implies the FIQ can be enabled for this transaction.
  79626. + */
  79627. +
  79628. +int fiq_fsm_transaction_suitable(dwc_otg_qh_t *qh)
  79629. +{
  79630. + if (qh->do_split) {
  79631. + switch (qh->ep_type) {
  79632. + case UE_CONTROL:
  79633. + case UE_BULK:
  79634. + if (fiq_fsm_mask & (1 << 0))
  79635. + return 1;
  79636. + break;
  79637. + case UE_INTERRUPT:
  79638. + case UE_ISOCHRONOUS:
  79639. + if (fiq_fsm_mask & (1 << 1))
  79640. + return 1;
  79641. + break;
  79642. + default:
  79643. + break;
  79644. + }
  79645. + } else if (qh->ep_type == UE_ISOCHRONOUS) {
  79646. + if (fiq_fsm_mask & (1 << 2)) {
  79647. + /* HS ISOCH support. We test for compatibility:
  79648. + * - DWORD aligned buffers
  79649. + * - Must be at least 2 transfers (otherwise pointless to use the FIQ)
  79650. + * If yes, then the fsm enqueue function will handle the state machine setup.
  79651. + */
  79652. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  79653. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  79654. + struct dwc_otg_hcd_iso_packet_desc (*iso_descs)[0] = &urb->iso_descs;
  79655. + int nr_iso_frames = urb->packet_count;
  79656. + int i;
  79657. + uint32_t ptr;
  79658. +
  79659. + if (nr_iso_frames < 2)
  79660. + return 0;
  79661. + for (i = 0; i < nr_iso_frames; i++) {
  79662. + ptr = urb->dma + iso_descs[i]->offset;
  79663. + if (ptr & 0x3) {
  79664. + printk_ratelimited("%s: Non-Dword aligned isochronous frame offset."
  79665. + " Cannot queue FIQ-accelerated transfer to device %d endpoint %d\n",
  79666. + __FUNCTION__, qh->channel->dev_addr, qh->channel->ep_num);
  79667. + return 0;
  79668. + }
  79669. + }
  79670. + return 1;
  79671. + }
  79672. + }
  79673. + return 0;
  79674. +}
  79675. +
  79676. +/**
  79677. + * fiq_fsm_setup_periodic_dma() - Set up DMA bounce buffers
  79678. + * @hcd: Pointer to the dwc_otg_hcd struct
  79679. + * @qh: Pointer to the endpoint's queue head
  79680. + *
  79681. + * Periodic split transactions are transmitted modulo 188 bytes.
  79682. + * This necessitates slicing data up into buckets for isochronous out
  79683. + * and fixing up the DMA address for all IN transfers.
  79684. + *
  79685. + * Returns 1 if the DMA bounce buffers have been used, 0 if the default
  79686. + * HC buffer has been used.
  79687. + */
  79688. +int fiq_fsm_setup_periodic_dma(dwc_otg_hcd_t *hcd, struct fiq_channel_state *st, dwc_otg_qh_t *qh)
  79689. + {
  79690. + int frame_length, i = 0;
  79691. + uint8_t *ptr = NULL;
  79692. + dwc_hc_t *hc = qh->channel;
  79693. + struct fiq_dma_blob *blob;
  79694. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  79695. +
  79696. + for (i = 0; i < 6; i++) {
  79697. + st->dma_info.slot_len[i] = 255;
  79698. + }
  79699. + st->dma_info.index = 0;
  79700. + i = 0;
  79701. + if (hc->ep_is_in) {
  79702. + /*
  79703. + * Set dma_regs to bounce buffer. FIQ will update the
  79704. + * state depending on transaction progress.
  79705. + */
  79706. + blob = (struct fiq_dma_blob *) hcd->fiq_state->dma_base;
  79707. + st->hcdma_copy.d32 = (uint32_t) &blob->channel[hc->hc_num].index[0].buf[0];
  79708. + /* Calculate the max number of CSPLITS such that the FIQ can time out
  79709. + * a transaction if it fails.
  79710. + */
  79711. + frame_length = st->hcchar_copy.b.mps;
  79712. + do {
  79713. + i++;
  79714. + frame_length -= 188;
  79715. + } while (frame_length >= 0);
  79716. + st->nrpackets = i;
  79717. + return 1;
  79718. + } else {
  79719. + if (qh->ep_type == UE_ISOCHRONOUS) {
  79720. +
  79721. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  79722. +
  79723. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  79724. + frame_length = frame_desc->length;
  79725. +
  79726. + /* Virtual address for bounce buffers */
  79727. + blob = hcd->fiq_dmab;
  79728. +
  79729. + ptr = qtd->urb->buf + frame_desc->offset;
  79730. + if (frame_length == 0) {
  79731. + /*
  79732. + * for isochronous transactions, we must still transmit a packet
  79733. + * even if the length is zero.
  79734. + */
  79735. + st->dma_info.slot_len[0] = 0;
  79736. + st->nrpackets = 1;
  79737. + } else {
  79738. + do {
  79739. + if (frame_length <= 188) {
  79740. + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, frame_length);
  79741. + st->dma_info.slot_len[i] = frame_length;
  79742. + ptr += frame_length;
  79743. + } else {
  79744. + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, 188);
  79745. + st->dma_info.slot_len[i] = 188;
  79746. + ptr += 188;
  79747. + }
  79748. + i++;
  79749. + frame_length -= 188;
  79750. + } while (frame_length > 0);
  79751. + st->nrpackets = i;
  79752. + }
  79753. + ptr = qtd->urb->buf + frame_desc->offset;
  79754. + /* Point the HC at the DMA address of the bounce buffers */
  79755. + blob = (struct fiq_dma_blob *) hcd->fiq_state->dma_base;
  79756. + st->hcdma_copy.d32 = (uint32_t) &blob->channel[hc->hc_num].index[0].buf[0];
  79757. +
  79758. + /* fixup xfersize to the actual packet size */
  79759. + st->hctsiz_copy.b.pid = 0;
  79760. + st->hctsiz_copy.b.xfersize = st->dma_info.slot_len[0];
  79761. + return 1;
  79762. + } else {
  79763. + /* For interrupt, single OUT packet required, goes in the SSPLIT from hc_buff. */
  79764. + return 0;
  79765. + }
  79766. + }
  79767. +}
  79768. +
  79769. +/*
  79770. + * Pushing a periodic request into the queue near the EOF1 point
  79771. + * in a microframe causes erroneous behaviour (frmovrun) interrupt.
  79772. + * Usually, the request goes out on the bus causing a transfer but
  79773. + * the core does not transfer the data to memory.
  79774. + * This guard interval (in number of 60MHz clocks) is required which
  79775. + * must cater for CPU latency between reading the value and enabling
  79776. + * the channel.
  79777. + */
  79778. +#define PERIODIC_FRREM_BACKOFF 1000
  79779. +
  79780. +int fiq_fsm_queue_isoc_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
  79781. +{
  79782. + dwc_hc_t *hc = qh->channel;
  79783. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  79784. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  79785. + int frame;
  79786. + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
  79787. + int xfer_len, nrpackets;
  79788. + hcdma_data_t hcdma;
  79789. + hfnum_data_t hfnum;
  79790. +
  79791. + if (st->fsm != FIQ_PASSTHROUGH)
  79792. + return 0;
  79793. +
  79794. + st->nr_errors = 0;
  79795. +
  79796. + st->hcchar_copy.d32 = 0;
  79797. + st->hcchar_copy.b.mps = hc->max_packet;
  79798. + st->hcchar_copy.b.epdir = hc->ep_is_in;
  79799. + st->hcchar_copy.b.devaddr = hc->dev_addr;
  79800. + st->hcchar_copy.b.epnum = hc->ep_num;
  79801. + st->hcchar_copy.b.eptype = hc->ep_type;
  79802. +
  79803. + st->hcintmsk_copy.b.chhltd = 1;
  79804. +
  79805. + frame = dwc_otg_hcd_get_frame_number(hcd);
  79806. + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
  79807. +
  79808. + st->hcchar_copy.b.lspddev = 0;
  79809. + /* Enable the channel later as a final register write. */
  79810. +
  79811. + st->hcsplt_copy.d32 = 0;
  79812. +
  79813. + st->hs_isoc_info.iso_desc = (struct dwc_otg_hcd_iso_packet_desc *) &qtd->urb->iso_descs;
  79814. + st->hs_isoc_info.nrframes = qtd->urb->packet_count;
  79815. + /* grab the next DMA address offset from the array */
  79816. + st->hcdma_copy.d32 = qtd->urb->dma;
  79817. + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[0].offset;
  79818. +
  79819. + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
  79820. + * the core needs to be told to send the correct number. Caution: for IN transfers,
  79821. + * this is always set to the maximum size of the endpoint. */
  79822. + xfer_len = st->hs_isoc_info.iso_desc[0].length;
  79823. + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
  79824. + if (nrpackets == 0)
  79825. + nrpackets = 1;
  79826. + st->hcchar_copy.b.multicnt = nrpackets;
  79827. + st->hctsiz_copy.b.pktcnt = nrpackets;
  79828. +
  79829. + /* Initial PID also needs to be set */
  79830. + if (st->hcchar_copy.b.epdir == 0) {
  79831. + st->hctsiz_copy.b.xfersize = xfer_len;
  79832. + switch (st->hcchar_copy.b.multicnt) {
  79833. + case 1:
  79834. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  79835. + break;
  79836. + case 2:
  79837. + case 3:
  79838. + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
  79839. + break;
  79840. + }
  79841. +
  79842. + } else {
  79843. + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
  79844. + switch (st->hcchar_copy.b.multicnt) {
  79845. + case 1:
  79846. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  79847. + break;
  79848. + case 2:
  79849. + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
  79850. + break;
  79851. + case 3:
  79852. + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
  79853. + break;
  79854. + }
  79855. + }
  79856. +
  79857. + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d ", hc->hc_num);
  79858. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcchar_copy.d32);
  79859. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
  79860. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
  79861. + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  79862. + local_fiq_disable();
  79863. + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
  79864. + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
  79865. + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
  79866. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  79867. + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
  79868. + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
  79869. + /* Prevent queueing near EOF1. Bad things happen if a periodic
  79870. + * split transaction is queued very close to EOF.
  79871. + */
  79872. + st->fsm = FIQ_HS_ISOC_SLEEPING;
  79873. + } else {
  79874. + st->fsm = FIQ_HS_ISOC_TURBO;
  79875. + st->hcchar_copy.b.chen = 1;
  79876. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  79877. + }
  79878. + mb();
  79879. + st->hcchar_copy.b.chen = 0;
  79880. + local_fiq_enable();
  79881. + return 0;
  79882. +}
  79883. +
  79884. +
  79885. +/**
  79886. + * fiq_fsm_queue_split_transaction() - Set up a host channel and FIQ state
  79887. + * @hcd: Pointer to the dwc_otg_hcd struct
  79888. + * @qh: Pointer to the endpoint's queue head
  79889. + *
  79890. + * This overrides the dwc_otg driver's normal method of queueing a transaction.
  79891. + * Called from dwc_otg_hcd_queue_transactions(), this performs specific setup
  79892. + * for the nominated host channel.
  79893. + *
  79894. + * For periodic transfers, it also peeks at the FIQ state to see if an immediate
  79895. + * start is possible. If not, then the FIQ is left to start the transfer.
  79896. + */
  79897. +int fiq_fsm_queue_split_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
  79898. +{
  79899. + int start_immediate = 1, i;
  79900. + hfnum_data_t hfnum;
  79901. + dwc_hc_t *hc = qh->channel;
  79902. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  79903. + /* Program HC registers, setup FIQ_state, examine FIQ if periodic, start transfer (not if uframe 5) */
  79904. + int hub_addr, port_addr, frame, uframe;
  79905. + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
  79906. +
  79907. + if (st->fsm != FIQ_PASSTHROUGH)
  79908. + return 0;
  79909. + st->nr_errors = 0;
  79910. +
  79911. + st->hcchar_copy.d32 = 0;
  79912. + st->hcchar_copy.b.mps = hc->max_packet;
  79913. + st->hcchar_copy.b.epdir = hc->ep_is_in;
  79914. + st->hcchar_copy.b.devaddr = hc->dev_addr;
  79915. + st->hcchar_copy.b.epnum = hc->ep_num;
  79916. + st->hcchar_copy.b.eptype = hc->ep_type;
  79917. + if (hc->ep_type & 0x1) {
  79918. + if (hc->ep_is_in)
  79919. + st->hcchar_copy.b.multicnt = 3;
  79920. + else
  79921. + /* Docs say set this to 1, but driver sets to 0! */
  79922. + st->hcchar_copy.b.multicnt = 0;
  79923. + } else {
  79924. + st->hcchar_copy.b.multicnt = 1;
  79925. + st->hcchar_copy.b.oddfrm = 0;
  79926. + }
  79927. + st->hcchar_copy.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW) ? 1 : 0;
  79928. + /* Enable the channel later as a final register write. */
  79929. +
  79930. + st->hcsplt_copy.d32 = 0;
  79931. + if(qh->do_split) {
  79932. + hcd->fops->hub_info(hcd, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->priv, &hub_addr, &port_addr);
  79933. + st->hcsplt_copy.b.compsplt = 0;
  79934. + st->hcsplt_copy.b.spltena = 1;
  79935. + // XACTPOS is for isoc-out only but needs initialising anyway.
  79936. + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_ALL;
  79937. + if((qh->ep_type == DWC_OTG_EP_TYPE_ISOC) && (!qh->ep_is_in)) {
  79938. + /* For packetsize 0 < L < 188, ISOC_XACTPOS_ALL.
  79939. + * for longer than this, ISOC_XACTPOS_BEGIN and the FIQ
  79940. + * will update as necessary.
  79941. + */
  79942. + if (hc->xfer_len > 188) {
  79943. + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_BEGIN;
  79944. + }
  79945. + }
  79946. + st->hcsplt_copy.b.hubaddr = (uint8_t) hub_addr;
  79947. + st->hcsplt_copy.b.prtaddr = (uint8_t) port_addr;
  79948. + st->hub_addr = hub_addr;
  79949. + st->port_addr = port_addr;
  79950. + }
  79951. +
  79952. + st->hctsiz_copy.d32 = 0;
  79953. + st->hctsiz_copy.b.dopng = 0;
  79954. + st->hctsiz_copy.b.pid = hc->data_pid_start;
  79955. +
  79956. + if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
  79957. + hc->xfer_len = hc->max_packet;
  79958. + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
  79959. + hc->xfer_len = 188;
  79960. + }
  79961. + st->hctsiz_copy.b.xfersize = hc->xfer_len;
  79962. +
  79963. + st->hctsiz_copy.b.pktcnt = 1;
  79964. +
  79965. + if (hc->ep_type & 0x1) {
  79966. + /*
  79967. + * For potentially multi-packet transfers, must use the DMA bounce buffers. For IN transfers,
  79968. + * the DMA address is the address of the first 188byte slot buffer in the bounce buffer array.
  79969. + * For multi-packet OUT transfers, we need to copy the data into the bounce buffer array so the FIQ can punt
  79970. + * the right address out as necessary. hc->xfer_buff and hc->xfer_len have already been set
  79971. + * in assign_and_init_hc(), but this is for the eventual transaction completion only. The FIQ
  79972. + * must not touch internal driver state.
  79973. + */
  79974. + if(!fiq_fsm_setup_periodic_dma(hcd, st, qh)) {
  79975. + if (hc->align_buff) {
  79976. + st->hcdma_copy.d32 = hc->align_buff;
  79977. + } else {
  79978. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  79979. + }
  79980. + }
  79981. + } else {
  79982. + if (hc->align_buff) {
  79983. + st->hcdma_copy.d32 = hc->align_buff;
  79984. + } else {
  79985. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  79986. + }
  79987. + }
  79988. + /* The FIQ depends upon no other interrupts being enabled except channel halt.
  79989. + * Fixup channel interrupt mask. */
  79990. + st->hcintmsk_copy.d32 = 0;
  79991. + st->hcintmsk_copy.b.chhltd = 1;
  79992. + st->hcintmsk_copy.b.ahberr = 1;
  79993. +
  79994. + /* Hack courtesy of FreeBSD: apparently forcing Interrupt Split transactions
  79995. + * as Control puts the transfer into the non-periodic request queue and the
  79996. + * non-periodic handler in the hub. Makes things lots easier.
  79997. + */
  79998. + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT) {
  79999. + st->hcchar_copy.b.multicnt = 0;
  80000. + st->hcchar_copy.b.oddfrm = 0;
  80001. + st->hcchar_copy.b.eptype = UE_CONTROL;
  80002. + if (hc->align_buff) {
  80003. + st->hcdma_copy.d32 = hc->align_buff;
  80004. + } else {
  80005. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  80006. + }
  80007. + }
  80008. + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
  80009. + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
  80010. + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
  80011. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  80012. + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
  80013. +
  80014. + local_fiq_disable();
  80015. + mb();
  80016. +
  80017. + if (hc->ep_type & 0x1) {
  80018. + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  80019. + frame = (hfnum.b.frnum & ~0x7) >> 3;
  80020. + uframe = hfnum.b.frnum & 0x7;
  80021. + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
  80022. + /* Prevent queueing near EOF1. Bad things happen if a periodic
  80023. + * split transaction is queued very close to EOF.
  80024. + */
  80025. + start_immediate = 0;
  80026. + } else if (uframe == 5) {
  80027. + start_immediate = 0;
  80028. + } else if (hc->ep_type == UE_ISOCHRONOUS && !hc->ep_is_in) {
  80029. + start_immediate = 0;
  80030. + } else if (hc->ep_is_in && fiq_fsm_too_late(hcd->fiq_state, hc->hc_num)) {
  80031. + start_immediate = 0;
  80032. + } else {
  80033. + /* Search through all host channels to determine if a transaction
  80034. + * is currently in progress */
  80035. + for (i = 0; i < hcd->core_if->core_params->host_channels; i++) {
  80036. + if (i == hc->hc_num || hcd->fiq_state->channel[i].fsm == FIQ_PASSTHROUGH)
  80037. + continue;
  80038. + switch (hcd->fiq_state->channel[i].fsm) {
  80039. + /* TT is reserved for channels that are in the middle of a periodic
  80040. + * split transaction.
  80041. + */
  80042. + case FIQ_PER_SSPLIT_STARTED:
  80043. + case FIQ_PER_CSPLIT_WAIT:
  80044. + case FIQ_PER_CSPLIT_NYET1:
  80045. + case FIQ_PER_CSPLIT_POLL:
  80046. + case FIQ_PER_ISO_OUT_ACTIVE:
  80047. + case FIQ_PER_ISO_OUT_LAST:
  80048. + if (hcd->fiq_state->channel[i].hub_addr == hub_addr &&
  80049. + hcd->fiq_state->channel[i].port_addr == port_addr) {
  80050. + start_immediate = 0;
  80051. + }
  80052. + break;
  80053. + default:
  80054. + break;
  80055. + }
  80056. + if (!start_immediate)
  80057. + break;
  80058. + }
  80059. + }
  80060. + }
  80061. + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT)
  80062. + start_immediate = 1;
  80063. +
  80064. + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d %01d", hc->hc_num, start_immediate);
  80065. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08d", hfnum.b.frrem);
  80066. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "H:%02dP:%02d", hub_addr, port_addr);
  80067. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
  80068. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
  80069. + switch (hc->ep_type) {
  80070. + case UE_CONTROL:
  80071. + case UE_BULK:
  80072. + st->fsm = FIQ_NP_SSPLIT_STARTED;
  80073. + break;
  80074. + case UE_ISOCHRONOUS:
  80075. + if (hc->ep_is_in) {
  80076. + if (start_immediate) {
  80077. + st->fsm = FIQ_PER_SSPLIT_STARTED;
  80078. + } else {
  80079. + st->fsm = FIQ_PER_SSPLIT_QUEUED;
  80080. + }
  80081. + } else {
  80082. + if (start_immediate) {
  80083. + /* Single-isoc OUT packets don't require FIQ involvement */
  80084. + if (st->nrpackets == 1) {
  80085. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  80086. + } else {
  80087. + st->fsm = FIQ_PER_ISO_OUT_ACTIVE;
  80088. + }
  80089. + } else {
  80090. + st->fsm = FIQ_PER_ISO_OUT_PENDING;
  80091. + }
  80092. + }
  80093. + break;
  80094. + case UE_INTERRUPT:
  80095. + if (fiq_fsm_mask & 0x8) {
  80096. + st->fsm = FIQ_NP_SSPLIT_STARTED;
  80097. + } else if (start_immediate) {
  80098. + st->fsm = FIQ_PER_SSPLIT_STARTED;
  80099. + } else {
  80100. + st->fsm = FIQ_PER_SSPLIT_QUEUED;
  80101. + }
  80102. + default:
  80103. + break;
  80104. + }
  80105. + if (start_immediate) {
  80106. + /* Set the oddfrm bit as close as possible to actual queueing */
  80107. + frame = dwc_otg_hcd_get_frame_number(hcd);
  80108. + st->expected_uframe = (frame + 1) & 0x3FFF;
  80109. + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
  80110. + st->hcchar_copy.b.chen = 1;
  80111. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  80112. + }
  80113. + mb();
  80114. + local_fiq_enable();
  80115. + return 0;
  80116. +}
  80117. +
  80118. +
  80119. +/**
  80120. + * This function selects transactions from the HCD transfer schedule and
  80121. + * assigns them to available host channels. It is called from HCD interrupt
  80122. + * handler functions.
  80123. + *
  80124. + * @param hcd The HCD state structure.
  80125. + *
  80126. + * @return The types of new transactions that were assigned to host channels.
  80127. + */
  80128. +dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t * hcd)
  80129. +{
  80130. + dwc_list_link_t *qh_ptr;
  80131. + dwc_otg_qh_t *qh;
  80132. + int num_channels;
  80133. + dwc_irqflags_t flags;
  80134. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  80135. + dwc_otg_transaction_type_e ret_val = DWC_OTG_TRANSACTION_NONE;
  80136. +
  80137. +#ifdef DEBUG_HOST_CHANNELS
  80138. + last_sel_trans_num_per_scheduled = 0;
  80139. + last_sel_trans_num_nonper_scheduled = 0;
  80140. + last_sel_trans_num_avail_hc_at_start = hcd->available_host_channels;
  80141. +#endif /* DEBUG_HOST_CHANNELS */
  80142. +
  80143. + /* Process entries in the periodic ready list. */
  80144. + qh_ptr = DWC_LIST_FIRST(&hcd->periodic_sched_ready);
  80145. +
  80146. + while (qh_ptr != &hcd->periodic_sched_ready &&
  80147. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  80148. +
  80149. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  80150. +
  80151. + if (microframe_schedule) {
  80152. + // Make sure we leave one channel for non periodic transactions.
  80153. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  80154. + if (hcd->available_host_channels <= 1) {
  80155. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  80156. + break;
  80157. + }
  80158. + hcd->available_host_channels--;
  80159. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  80160. +#ifdef DEBUG_HOST_CHANNELS
  80161. + last_sel_trans_num_per_scheduled++;
  80162. +#endif /* DEBUG_HOST_CHANNELS */
  80163. + }
  80164. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  80165. + assign_and_init_hc(hcd, qh);
  80166. +
  80167. + /*
  80168. + * Move the QH from the periodic ready schedule to the
  80169. + * periodic assigned schedule.
  80170. + */
  80171. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  80172. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  80173. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  80174. + &qh->qh_list_entry);
  80175. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  80176. + }
  80177. +
  80178. + /*
  80179. + * Process entries in the inactive portion of the non-periodic
  80180. + * schedule. Some free host channels may not be used if they are
  80181. + * reserved for periodic transfers.
  80182. + */
  80183. + qh_ptr = hcd->non_periodic_sched_inactive.next;
  80184. + num_channels = hcd->core_if->core_params->host_channels;
  80185. + while (qh_ptr != &hcd->non_periodic_sched_inactive &&
  80186. + (microframe_schedule || hcd->non_periodic_channels <
  80187. + num_channels - hcd->periodic_channels) &&
  80188. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  80189. +
  80190. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  80191. + /*
  80192. + * Check to see if this is a NAK'd retransmit, in which case ignore for retransmission
  80193. + * we hold off on bulk retransmissions to reduce NAK interrupt overhead for full-speed
  80194. + * cheeky devices that just hold off using NAKs
  80195. + */
  80196. + if (nak_holdoff && qh->do_split) {
  80197. + if (qh->nak_frame != 0xffff) {
  80198. + uint16_t next_frame = dwc_frame_num_inc(qh->nak_frame, (qh->ep_type == UE_BULK) ? nak_holdoff : 8);
  80199. + uint16_t frame = dwc_otg_hcd_get_frame_number(hcd);
  80200. + if (dwc_frame_num_le(frame, next_frame)) {
  80201. + if(dwc_frame_num_le(next_frame, hcd->fiq_state->next_sched_frame)) {
  80202. + hcd->fiq_state->next_sched_frame = next_frame;
  80203. + }
  80204. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  80205. + continue;
  80206. + } else {
  80207. + qh->nak_frame = 0xFFFF;
  80208. + }
  80209. + }
  80210. + }
  80211. +
  80212. + if (microframe_schedule) {
  80213. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  80214. + if (hcd->available_host_channels < 1) {
  80215. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  80216. + break;
  80217. + }
  80218. + hcd->available_host_channels--;
  80219. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  80220. +#ifdef DEBUG_HOST_CHANNELS
  80221. + last_sel_trans_num_nonper_scheduled++;
  80222. +#endif /* DEBUG_HOST_CHANNELS */
  80223. + }
  80224. +
  80225. + assign_and_init_hc(hcd, qh);
  80226. +
  80227. + /*
  80228. + * Move the QH from the non-periodic inactive schedule to the
  80229. + * non-periodic active schedule.
  80230. + */
  80231. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  80232. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  80233. + DWC_LIST_MOVE_HEAD(&hcd->non_periodic_sched_active,
  80234. + &qh->qh_list_entry);
  80235. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  80236. +
  80237. +
  80238. + if (!microframe_schedule)
  80239. + hcd->non_periodic_channels++;
  80240. + }
  80241. + /* we moved a non-periodic QH to the active schedule. If the inactive queue is empty,
  80242. + * stop the FIQ from kicking us. We could potentially still have elements here if we
  80243. + * ran out of host channels.
  80244. + */
  80245. + if (fiq_enable) {
  80246. + if (DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive)) {
  80247. + hcd->fiq_state->kick_np_queues = 0;
  80248. + } else {
  80249. + /* For each entry remaining in the NP inactive queue,
  80250. + * if this a NAK'd retransmit then don't set the kick flag.
  80251. + */
  80252. + if(nak_holdoff) {
  80253. + DWC_LIST_FOREACH(qh_ptr, &hcd->non_periodic_sched_inactive) {
  80254. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  80255. + if (qh->nak_frame == 0xFFFF) {
  80256. + hcd->fiq_state->kick_np_queues = 1;
  80257. + }
  80258. + }
  80259. + }
  80260. + }
  80261. + }
  80262. + if(!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned))
  80263. + ret_val |= DWC_OTG_TRANSACTION_PERIODIC;
  80264. +
  80265. + if(!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active))
  80266. + ret_val |= DWC_OTG_TRANSACTION_NON_PERIODIC;
  80267. +
  80268. +
  80269. +#ifdef DEBUG_HOST_CHANNELS
  80270. + last_sel_trans_num_avail_hc_at_end = hcd->available_host_channels;
  80271. +#endif /* DEBUG_HOST_CHANNELS */
  80272. + return ret_val;
  80273. +}
  80274. +
  80275. +/**
  80276. + * Attempts to queue a single transaction request for a host channel
  80277. + * associated with either a periodic or non-periodic transfer. This function
  80278. + * assumes that there is space available in the appropriate request queue. For
  80279. + * an OUT transfer or SETUP transaction in Slave mode, it checks whether space
  80280. + * is available in the appropriate Tx FIFO.
  80281. + *
  80282. + * @param hcd The HCD state structure.
  80283. + * @param hc Host channel descriptor associated with either a periodic or
  80284. + * non-periodic transfer.
  80285. + * @param fifo_dwords_avail Number of DWORDs available in the periodic Tx
  80286. + * FIFO for periodic transfers or the non-periodic Tx FIFO for non-periodic
  80287. + * transfers.
  80288. + *
  80289. + * @return 1 if a request is queued and more requests may be needed to
  80290. + * complete the transfer, 0 if no more requests are required for this
  80291. + * transfer, -1 if there is insufficient space in the Tx FIFO.
  80292. + */
  80293. +static int queue_transaction(dwc_otg_hcd_t * hcd,
  80294. + dwc_hc_t * hc, uint16_t fifo_dwords_avail)
  80295. +{
  80296. + int retval;
  80297. +
  80298. + if (hcd->core_if->dma_enable) {
  80299. + if (hcd->core_if->dma_desc_enable) {
  80300. + if (!hc->xfer_started
  80301. + || (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)) {
  80302. + dwc_otg_hcd_start_xfer_ddma(hcd, hc->qh);
  80303. + hc->qh->ping_state = 0;
  80304. + }
  80305. + } else if (!hc->xfer_started) {
  80306. + if (fiq_fsm_enable && hc->error_state) {
  80307. + hcd->fiq_state->channel[hc->hc_num].nr_errors =
  80308. + DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list)->error_count;
  80309. + hcd->fiq_state->channel[hc->hc_num].fsm =
  80310. + FIQ_PASSTHROUGH_ERRORSTATE;
  80311. + }
  80312. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  80313. + hc->qh->ping_state = 0;
  80314. + }
  80315. + retval = 0;
  80316. + } else if (hc->halt_pending) {
  80317. + /* Don't queue a request if the channel has been halted. */
  80318. + retval = 0;
  80319. + } else if (hc->halt_on_queue) {
  80320. + dwc_otg_hc_halt(hcd->core_if, hc, hc->halt_status);
  80321. + retval = 0;
  80322. + } else if (hc->do_ping) {
  80323. + if (!hc->xfer_started) {
  80324. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  80325. + }
  80326. + retval = 0;
  80327. + } else if (!hc->ep_is_in || hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  80328. + if ((fifo_dwords_avail * 4) >= hc->max_packet) {
  80329. + if (!hc->xfer_started) {
  80330. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  80331. + retval = 1;
  80332. + } else {
  80333. + retval =
  80334. + dwc_otg_hc_continue_transfer(hcd->core_if,
  80335. + hc);
  80336. + }
  80337. + } else {
  80338. + retval = -1;
  80339. + }
  80340. + } else {
  80341. + if (!hc->xfer_started) {
  80342. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  80343. + retval = 1;
  80344. + } else {
  80345. + retval = dwc_otg_hc_continue_transfer(hcd->core_if, hc);
  80346. + }
  80347. + }
  80348. +
  80349. + return retval;
  80350. +}
  80351. +
  80352. +/**
  80353. + * Processes periodic channels for the next frame and queues transactions for
  80354. + * these channels to the DWC_otg controller. After queueing transactions, the
  80355. + * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
  80356. + * to queue as Periodic Tx FIFO or request queue space becomes available.
  80357. + * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
  80358. + */
  80359. +static void process_periodic_channels(dwc_otg_hcd_t * hcd)
  80360. +{
  80361. + hptxsts_data_t tx_status;
  80362. + dwc_list_link_t *qh_ptr;
  80363. + dwc_otg_qh_t *qh;
  80364. + int status = 0;
  80365. + int no_queue_space = 0;
  80366. + int no_fifo_space = 0;
  80367. +
  80368. + dwc_otg_host_global_regs_t *host_regs;
  80369. + host_regs = hcd->core_if->host_if->host_global_regs;
  80370. +
  80371. + DWC_DEBUGPL(DBG_HCDV, "Queue periodic transactions\n");
  80372. +#ifdef DEBUG
  80373. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  80374. + DWC_DEBUGPL(DBG_HCDV,
  80375. + " P Tx Req Queue Space Avail (before queue): %d\n",
  80376. + tx_status.b.ptxqspcavail);
  80377. + DWC_DEBUGPL(DBG_HCDV, " P Tx FIFO Space Avail (before queue): %d\n",
  80378. + tx_status.b.ptxfspcavail);
  80379. +#endif
  80380. +
  80381. + qh_ptr = hcd->periodic_sched_assigned.next;
  80382. + while (qh_ptr != &hcd->periodic_sched_assigned) {
  80383. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  80384. + if (tx_status.b.ptxqspcavail == 0) {
  80385. + no_queue_space = 1;
  80386. + break;
  80387. + }
  80388. +
  80389. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  80390. +
  80391. + // Do not send a split start transaction any later than frame .6
  80392. + // Note, we have to schedule a periodic in .5 to make it go in .6
  80393. + if(fiq_fsm_enable && qh->do_split && ((dwc_otg_hcd_get_frame_number(hcd) + 1) & 7) > 6)
  80394. + {
  80395. + qh_ptr = qh_ptr->next;
  80396. + hcd->fiq_state->next_sched_frame = dwc_otg_hcd_get_frame_number(hcd) | 7;
  80397. + continue;
  80398. + }
  80399. +
  80400. + if (fiq_fsm_enable && fiq_fsm_transaction_suitable(qh)) {
  80401. + if (qh->do_split)
  80402. + fiq_fsm_queue_split_transaction(hcd, qh);
  80403. + else
  80404. + fiq_fsm_queue_isoc_transaction(hcd, qh);
  80405. + } else {
  80406. +
  80407. + /*
  80408. + * Set a flag if we're queueing high-bandwidth in slave mode.
  80409. + * The flag prevents any halts to get into the request queue in
  80410. + * the middle of multiple high-bandwidth packets getting queued.
  80411. + */
  80412. + if (!hcd->core_if->dma_enable && qh->channel->multi_count > 1) {
  80413. + hcd->core_if->queuing_high_bandwidth = 1;
  80414. + }
  80415. + status = queue_transaction(hcd, qh->channel,
  80416. + tx_status.b.ptxfspcavail);
  80417. + if (status < 0) {
  80418. + no_fifo_space = 1;
  80419. + break;
  80420. + }
  80421. + }
  80422. +
  80423. + /*
  80424. + * In Slave mode, stay on the current transfer until there is
  80425. + * nothing more to do or the high-bandwidth request count is
  80426. + * reached. In DMA mode, only need to queue one request. The
  80427. + * controller automatically handles multiple packets for
  80428. + * high-bandwidth transfers.
  80429. + */
  80430. + if (hcd->core_if->dma_enable || status == 0 ||
  80431. + qh->channel->requests == qh->channel->multi_count) {
  80432. + qh_ptr = qh_ptr->next;
  80433. + /*
  80434. + * Move the QH from the periodic assigned schedule to
  80435. + * the periodic queued schedule.
  80436. + */
  80437. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_queued,
  80438. + &qh->qh_list_entry);
  80439. +
  80440. + /* done queuing high bandwidth */
  80441. + hcd->core_if->queuing_high_bandwidth = 0;
  80442. + }
  80443. + }
  80444. +
  80445. + if (!hcd->core_if->dma_enable) {
  80446. + dwc_otg_core_global_regs_t *global_regs;
  80447. + gintmsk_data_t intr_mask = {.d32 = 0 };
  80448. +
  80449. + global_regs = hcd->core_if->core_global_regs;
  80450. + intr_mask.b.ptxfempty = 1;
  80451. +#ifdef DEBUG
  80452. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  80453. + DWC_DEBUGPL(DBG_HCDV,
  80454. + " P Tx Req Queue Space Avail (after queue): %d\n",
  80455. + tx_status.b.ptxqspcavail);
  80456. + DWC_DEBUGPL(DBG_HCDV,
  80457. + " P Tx FIFO Space Avail (after queue): %d\n",
  80458. + tx_status.b.ptxfspcavail);
  80459. +#endif
  80460. + if (!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned) ||
  80461. + no_queue_space || no_fifo_space) {
  80462. + /*
  80463. + * May need to queue more transactions as the request
  80464. + * queue or Tx FIFO empties. Enable the periodic Tx
  80465. + * FIFO empty interrupt. (Always use the half-empty
  80466. + * level to ensure that new requests are loaded as
  80467. + * soon as possible.)
  80468. + */
  80469. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  80470. + intr_mask.d32);
  80471. + } else {
  80472. + /*
  80473. + * Disable the Tx FIFO empty interrupt since there are
  80474. + * no more transactions that need to be queued right
  80475. + * now. This function is called from interrupt
  80476. + * handlers to queue more transactions as transfer
  80477. + * states change.
  80478. + */
  80479. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  80480. + 0);
  80481. + }
  80482. + }
  80483. +}
  80484. +
  80485. +/**
  80486. + * Processes active non-periodic channels and queues transactions for these
  80487. + * channels to the DWC_otg controller. After queueing transactions, the NP Tx
  80488. + * FIFO Empty interrupt is enabled if there are more transactions to queue as
  80489. + * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
  80490. + * FIFO Empty interrupt is disabled.
  80491. + */
  80492. +static void process_non_periodic_channels(dwc_otg_hcd_t * hcd)
  80493. +{
  80494. + gnptxsts_data_t tx_status;
  80495. + dwc_list_link_t *orig_qh_ptr;
  80496. + dwc_otg_qh_t *qh;
  80497. + int status;
  80498. + int no_queue_space = 0;
  80499. + int no_fifo_space = 0;
  80500. + int more_to_do = 0;
  80501. +
  80502. + dwc_otg_core_global_regs_t *global_regs =
  80503. + hcd->core_if->core_global_regs;
  80504. +
  80505. + DWC_DEBUGPL(DBG_HCDV, "Queue non-periodic transactions\n");
  80506. +#ifdef DEBUG
  80507. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  80508. + DWC_DEBUGPL(DBG_HCDV,
  80509. + " NP Tx Req Queue Space Avail (before queue): %d\n",
  80510. + tx_status.b.nptxqspcavail);
  80511. + DWC_DEBUGPL(DBG_HCDV, " NP Tx FIFO Space Avail (before queue): %d\n",
  80512. + tx_status.b.nptxfspcavail);
  80513. +#endif
  80514. + /*
  80515. + * Keep track of the starting point. Skip over the start-of-list
  80516. + * entry.
  80517. + */
  80518. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  80519. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  80520. + }
  80521. + orig_qh_ptr = hcd->non_periodic_qh_ptr;
  80522. +
  80523. + /*
  80524. + * Process once through the active list or until no more space is
  80525. + * available in the request queue or the Tx FIFO.
  80526. + */
  80527. + do {
  80528. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  80529. + if (!hcd->core_if->dma_enable && tx_status.b.nptxqspcavail == 0) {
  80530. + no_queue_space = 1;
  80531. + break;
  80532. + }
  80533. +
  80534. + qh = DWC_LIST_ENTRY(hcd->non_periodic_qh_ptr, dwc_otg_qh_t,
  80535. + qh_list_entry);
  80536. +
  80537. + if(fiq_fsm_enable && fiq_fsm_transaction_suitable(qh)) {
  80538. + fiq_fsm_queue_split_transaction(hcd, qh);
  80539. + } else {
  80540. + status = queue_transaction(hcd, qh->channel,
  80541. + tx_status.b.nptxfspcavail);
  80542. +
  80543. + if (status > 0) {
  80544. + more_to_do = 1;
  80545. + } else if (status < 0) {
  80546. + no_fifo_space = 1;
  80547. + break;
  80548. + }
  80549. + }
  80550. + /* Advance to next QH, skipping start-of-list entry. */
  80551. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  80552. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  80553. + hcd->non_periodic_qh_ptr =
  80554. + hcd->non_periodic_qh_ptr->next;
  80555. + }
  80556. +
  80557. + } while (hcd->non_periodic_qh_ptr != orig_qh_ptr);
  80558. +
  80559. + if (!hcd->core_if->dma_enable) {
  80560. + gintmsk_data_t intr_mask = {.d32 = 0 };
  80561. + intr_mask.b.nptxfempty = 1;
  80562. +
  80563. +#ifdef DEBUG
  80564. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  80565. + DWC_DEBUGPL(DBG_HCDV,
  80566. + " NP Tx Req Queue Space Avail (after queue): %d\n",
  80567. + tx_status.b.nptxqspcavail);
  80568. + DWC_DEBUGPL(DBG_HCDV,
  80569. + " NP Tx FIFO Space Avail (after queue): %d\n",
  80570. + tx_status.b.nptxfspcavail);
  80571. +#endif
  80572. + if (more_to_do || no_queue_space || no_fifo_space) {
  80573. + /*
  80574. + * May need to queue more transactions as the request
  80575. + * queue or Tx FIFO empties. Enable the non-periodic
  80576. + * Tx FIFO empty interrupt. (Always use the half-empty
  80577. + * level to ensure that new requests are loaded as
  80578. + * soon as possible.)
  80579. + */
  80580. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  80581. + intr_mask.d32);
  80582. + } else {
  80583. + /*
  80584. + * Disable the Tx FIFO empty interrupt since there are
  80585. + * no more transactions that need to be queued right
  80586. + * now. This function is called from interrupt
  80587. + * handlers to queue more transactions as transfer
  80588. + * states change.
  80589. + */
  80590. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  80591. + 0);
  80592. + }
  80593. + }
  80594. +}
  80595. +
  80596. +/**
  80597. + * This function processes the currently active host channels and queues
  80598. + * transactions for these channels to the DWC_otg controller. It is called
  80599. + * from HCD interrupt handler functions.
  80600. + *
  80601. + * @param hcd The HCD state structure.
  80602. + * @param tr_type The type(s) of transactions to queue (non-periodic,
  80603. + * periodic, or both).
  80604. + */
  80605. +void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  80606. + dwc_otg_transaction_type_e tr_type)
  80607. +{
  80608. +#ifdef DEBUG_SOF
  80609. + DWC_DEBUGPL(DBG_HCD, "Queue Transactions\n");
  80610. +#endif
  80611. + /* Process host channels associated with periodic transfers. */
  80612. + if ((tr_type == DWC_OTG_TRANSACTION_PERIODIC ||
  80613. + tr_type == DWC_OTG_TRANSACTION_ALL) &&
  80614. + !DWC_LIST_EMPTY(&hcd->periodic_sched_assigned)) {
  80615. +
  80616. + process_periodic_channels(hcd);
  80617. + }
  80618. +
  80619. + /* Process host channels associated with non-periodic transfers. */
  80620. + if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC ||
  80621. + tr_type == DWC_OTG_TRANSACTION_ALL) {
  80622. + if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active)) {
  80623. + process_non_periodic_channels(hcd);
  80624. + } else {
  80625. + /*
  80626. + * Ensure NP Tx FIFO empty interrupt is disabled when
  80627. + * there are no non-periodic transfers to process.
  80628. + */
  80629. + gintmsk_data_t gintmsk = {.d32 = 0 };
  80630. + gintmsk.b.nptxfempty = 1;
  80631. + DWC_MODIFY_REG32(&hcd->core_if->
  80632. + core_global_regs->gintmsk, gintmsk.d32,
  80633. + 0);
  80634. + }
  80635. + }
  80636. +}
  80637. +
  80638. +#ifdef DWC_HS_ELECT_TST
  80639. +/*
  80640. + * Quick and dirty hack to implement the HS Electrical Test
  80641. + * SINGLE_STEP_GET_DEVICE_DESCRIPTOR feature.
  80642. + *
  80643. + * This code was copied from our userspace app "hset". It sends a
  80644. + * Get Device Descriptor control sequence in two parts, first the
  80645. + * Setup packet by itself, followed some time later by the In and
  80646. + * Ack packets. Rather than trying to figure out how to add this
  80647. + * functionality to the normal driver code, we just hijack the
  80648. + * hardware, using these two function to drive the hardware
  80649. + * directly.
  80650. + */
  80651. +
  80652. +static dwc_otg_core_global_regs_t *global_regs;
  80653. +static dwc_otg_host_global_regs_t *hc_global_regs;
  80654. +static dwc_otg_hc_regs_t *hc_regs;
  80655. +static uint32_t *data_fifo;
  80656. +
  80657. +static void do_setup(void)
  80658. +{
  80659. + gintsts_data_t gintsts;
  80660. + hctsiz_data_t hctsiz;
  80661. + hcchar_data_t hcchar;
  80662. + haint_data_t haint;
  80663. + hcint_data_t hcint;
  80664. +
  80665. + /* Enable HAINTs */
  80666. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  80667. +
  80668. + /* Enable HCINTs */
  80669. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  80670. +
  80671. + /* Read GINTSTS */
  80672. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  80673. +
  80674. + /* Read HAINT */
  80675. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  80676. +
  80677. + /* Read HCINT */
  80678. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  80679. +
  80680. + /* Read HCCHAR */
  80681. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  80682. +
  80683. + /* Clear HCINT */
  80684. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  80685. +
  80686. + /* Clear HAINT */
  80687. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  80688. +
  80689. + /* Clear GINTSTS */
  80690. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  80691. +
  80692. + /* Read GINTSTS */
  80693. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  80694. +
  80695. + /*
  80696. + * Send Setup packet (Get Device Descriptor)
  80697. + */
  80698. +
  80699. + /* Make sure channel is disabled */
  80700. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  80701. + if (hcchar.b.chen) {
  80702. + hcchar.b.chdis = 1;
  80703. +// hcchar.b.chen = 1;
  80704. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  80705. + //sleep(1);
  80706. + dwc_mdelay(1000);
  80707. +
  80708. + /* Read GINTSTS */
  80709. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  80710. +
  80711. + /* Read HAINT */
  80712. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  80713. +
  80714. + /* Read HCINT */
  80715. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  80716. +
  80717. + /* Read HCCHAR */
  80718. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  80719. +
  80720. + /* Clear HCINT */
  80721. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  80722. +
  80723. + /* Clear HAINT */
  80724. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  80725. +
  80726. + /* Clear GINTSTS */
  80727. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  80728. +
  80729. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  80730. + }
  80731. +
  80732. + /* Set HCTSIZ */
  80733. + hctsiz.d32 = 0;
  80734. + hctsiz.b.xfersize = 8;
  80735. + hctsiz.b.pktcnt = 1;
  80736. + hctsiz.b.pid = DWC_OTG_HC_PID_SETUP;
  80737. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  80738. +
  80739. + /* Set HCCHAR */
  80740. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  80741. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  80742. + hcchar.b.epdir = 0;
  80743. + hcchar.b.epnum = 0;
  80744. + hcchar.b.mps = 8;
  80745. + hcchar.b.chen = 1;
  80746. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  80747. +
  80748. + /* Fill FIFO with Setup data for Get Device Descriptor */
  80749. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  80750. + DWC_WRITE_REG32(data_fifo++, 0x01000680);
  80751. + DWC_WRITE_REG32(data_fifo++, 0x00080000);
  80752. +
  80753. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  80754. +
  80755. + /* Wait for host channel interrupt */
  80756. + do {
  80757. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  80758. + } while (gintsts.b.hcintr == 0);
  80759. +
  80760. + /* Disable HCINTs */
  80761. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  80762. +
  80763. + /* Disable HAINTs */
  80764. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  80765. +
  80766. + /* Read HAINT */
  80767. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  80768. +
  80769. + /* Read HCINT */
  80770. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  80771. +
  80772. + /* Read HCCHAR */
  80773. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  80774. +
  80775. + /* Clear HCINT */
  80776. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  80777. +
  80778. + /* Clear HAINT */
  80779. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  80780. +
  80781. + /* Clear GINTSTS */
  80782. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  80783. +
  80784. + /* Read GINTSTS */
  80785. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  80786. +}
  80787. +
  80788. +static void do_in_ack(void)
  80789. +{
  80790. + gintsts_data_t gintsts;
  80791. + hctsiz_data_t hctsiz;
  80792. + hcchar_data_t hcchar;
  80793. + haint_data_t haint;
  80794. + hcint_data_t hcint;
  80795. + host_grxsts_data_t grxsts;
  80796. +
  80797. + /* Enable HAINTs */
  80798. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  80799. +
  80800. + /* Enable HCINTs */
  80801. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  80802. +
  80803. + /* Read GINTSTS */
  80804. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  80805. +
  80806. + /* Read HAINT */
  80807. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  80808. +
  80809. + /* Read HCINT */
  80810. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  80811. +
  80812. + /* Read HCCHAR */
  80813. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  80814. +
  80815. + /* Clear HCINT */
  80816. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  80817. +
  80818. + /* Clear HAINT */
  80819. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  80820. +
  80821. + /* Clear GINTSTS */
  80822. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  80823. +
  80824. + /* Read GINTSTS */
  80825. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  80826. +
  80827. + /*
  80828. + * Receive Control In packet
  80829. + */
  80830. +
  80831. + /* Make sure channel is disabled */
  80832. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  80833. + if (hcchar.b.chen) {
  80834. + hcchar.b.chdis = 1;
  80835. + hcchar.b.chen = 1;
  80836. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  80837. + //sleep(1);
  80838. + dwc_mdelay(1000);
  80839. +
  80840. + /* Read GINTSTS */
  80841. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  80842. +
  80843. + /* Read HAINT */
  80844. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  80845. +
  80846. + /* Read HCINT */
  80847. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  80848. +
  80849. + /* Read HCCHAR */
  80850. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  80851. +
  80852. + /* Clear HCINT */
  80853. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  80854. +
  80855. + /* Clear HAINT */
  80856. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  80857. +
  80858. + /* Clear GINTSTS */
  80859. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  80860. +
  80861. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  80862. + }
  80863. +
  80864. + /* Set HCTSIZ */
  80865. + hctsiz.d32 = 0;
  80866. + hctsiz.b.xfersize = 8;
  80867. + hctsiz.b.pktcnt = 1;
  80868. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  80869. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  80870. +
  80871. + /* Set HCCHAR */
  80872. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  80873. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  80874. + hcchar.b.epdir = 1;
  80875. + hcchar.b.epnum = 0;
  80876. + hcchar.b.mps = 8;
  80877. + hcchar.b.chen = 1;
  80878. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  80879. +
  80880. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  80881. +
  80882. + /* Wait for receive status queue interrupt */
  80883. + do {
  80884. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  80885. + } while (gintsts.b.rxstsqlvl == 0);
  80886. +
  80887. + /* Read RXSTS */
  80888. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  80889. +
  80890. + /* Clear RXSTSQLVL in GINTSTS */
  80891. + gintsts.d32 = 0;
  80892. + gintsts.b.rxstsqlvl = 1;
  80893. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  80894. +
  80895. + switch (grxsts.b.pktsts) {
  80896. + case DWC_GRXSTS_PKTSTS_IN:
  80897. + /* Read the data into the host buffer */
  80898. + if (grxsts.b.bcnt > 0) {
  80899. + int i;
  80900. + int word_count = (grxsts.b.bcnt + 3) / 4;
  80901. +
  80902. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  80903. +
  80904. + for (i = 0; i < word_count; i++) {
  80905. + (void)DWC_READ_REG32(data_fifo++);
  80906. + }
  80907. + }
  80908. + break;
  80909. +
  80910. + default:
  80911. + break;
  80912. + }
  80913. +
  80914. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  80915. +
  80916. + /* Wait for receive status queue interrupt */
  80917. + do {
  80918. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  80919. + } while (gintsts.b.rxstsqlvl == 0);
  80920. +
  80921. + /* Read RXSTS */
  80922. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  80923. +
  80924. + /* Clear RXSTSQLVL in GINTSTS */
  80925. + gintsts.d32 = 0;
  80926. + gintsts.b.rxstsqlvl = 1;
  80927. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  80928. +
  80929. + switch (grxsts.b.pktsts) {
  80930. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  80931. + break;
  80932. +
  80933. + default:
  80934. + break;
  80935. + }
  80936. +
  80937. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  80938. +
  80939. + /* Wait for host channel interrupt */
  80940. + do {
  80941. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  80942. + } while (gintsts.b.hcintr == 0);
  80943. +
  80944. + /* Read HAINT */
  80945. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  80946. +
  80947. + /* Read HCINT */
  80948. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  80949. +
  80950. + /* Read HCCHAR */
  80951. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  80952. +
  80953. + /* Clear HCINT */
  80954. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  80955. +
  80956. + /* Clear HAINT */
  80957. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  80958. +
  80959. + /* Clear GINTSTS */
  80960. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  80961. +
  80962. + /* Read GINTSTS */
  80963. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  80964. +
  80965. +// usleep(100000);
  80966. +// mdelay(100);
  80967. + dwc_mdelay(1);
  80968. +
  80969. + /*
  80970. + * Send handshake packet
  80971. + */
  80972. +
  80973. + /* Read HAINT */
  80974. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  80975. +
  80976. + /* Read HCINT */
  80977. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  80978. +
  80979. + /* Read HCCHAR */
  80980. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  80981. +
  80982. + /* Clear HCINT */
  80983. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  80984. +
  80985. + /* Clear HAINT */
  80986. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  80987. +
  80988. + /* Clear GINTSTS */
  80989. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  80990. +
  80991. + /* Read GINTSTS */
  80992. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  80993. +
  80994. + /* Make sure channel is disabled */
  80995. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  80996. + if (hcchar.b.chen) {
  80997. + hcchar.b.chdis = 1;
  80998. + hcchar.b.chen = 1;
  80999. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  81000. + //sleep(1);
  81001. + dwc_mdelay(1000);
  81002. +
  81003. + /* Read GINTSTS */
  81004. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  81005. +
  81006. + /* Read HAINT */
  81007. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  81008. +
  81009. + /* Read HCINT */
  81010. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  81011. +
  81012. + /* Read HCCHAR */
  81013. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  81014. +
  81015. + /* Clear HCINT */
  81016. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  81017. +
  81018. + /* Clear HAINT */
  81019. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  81020. +
  81021. + /* Clear GINTSTS */
  81022. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  81023. +
  81024. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  81025. + }
  81026. +
  81027. + /* Set HCTSIZ */
  81028. + hctsiz.d32 = 0;
  81029. + hctsiz.b.xfersize = 0;
  81030. + hctsiz.b.pktcnt = 1;
  81031. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  81032. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  81033. +
  81034. + /* Set HCCHAR */
  81035. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  81036. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  81037. + hcchar.b.epdir = 0;
  81038. + hcchar.b.epnum = 0;
  81039. + hcchar.b.mps = 8;
  81040. + hcchar.b.chen = 1;
  81041. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  81042. +
  81043. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  81044. +
  81045. + /* Wait for host channel interrupt */
  81046. + do {
  81047. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  81048. + } while (gintsts.b.hcintr == 0);
  81049. +
  81050. + /* Disable HCINTs */
  81051. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  81052. +
  81053. + /* Disable HAINTs */
  81054. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  81055. +
  81056. + /* Read HAINT */
  81057. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  81058. +
  81059. + /* Read HCINT */
  81060. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  81061. +
  81062. + /* Read HCCHAR */
  81063. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  81064. +
  81065. + /* Clear HCINT */
  81066. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  81067. +
  81068. + /* Clear HAINT */
  81069. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  81070. +
  81071. + /* Clear GINTSTS */
  81072. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  81073. +
  81074. + /* Read GINTSTS */
  81075. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  81076. +}
  81077. +#endif
  81078. +
  81079. +/** Handles hub class-specific requests. */
  81080. +int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  81081. + uint16_t typeReq,
  81082. + uint16_t wValue,
  81083. + uint16_t wIndex, uint8_t * buf, uint16_t wLength)
  81084. +{
  81085. + int retval = 0;
  81086. +
  81087. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  81088. + usb_hub_descriptor_t *hub_desc;
  81089. + hprt0_data_t hprt0 = {.d32 = 0 };
  81090. +
  81091. + uint32_t port_status;
  81092. +
  81093. + switch (typeReq) {
  81094. + case UCR_CLEAR_HUB_FEATURE:
  81095. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  81096. + "ClearHubFeature 0x%x\n", wValue);
  81097. + switch (wValue) {
  81098. + case UHF_C_HUB_LOCAL_POWER:
  81099. + case UHF_C_HUB_OVER_CURRENT:
  81100. + /* Nothing required here */
  81101. + break;
  81102. + default:
  81103. + retval = -DWC_E_INVALID;
  81104. + DWC_ERROR("DWC OTG HCD - "
  81105. + "ClearHubFeature request %xh unknown\n",
  81106. + wValue);
  81107. + }
  81108. + break;
  81109. + case UCR_CLEAR_PORT_FEATURE:
  81110. +#ifdef CONFIG_USB_DWC_OTG_LPM
  81111. + if (wValue != UHF_PORT_L1)
  81112. +#endif
  81113. + if (!wIndex || wIndex > 1)
  81114. + goto error;
  81115. +
  81116. + switch (wValue) {
  81117. + case UHF_PORT_ENABLE:
  81118. + DWC_DEBUGPL(DBG_ANY, "DWC OTG HCD HUB CONTROL - "
  81119. + "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
  81120. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  81121. + hprt0.b.prtena = 1;
  81122. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  81123. + break;
  81124. + case UHF_PORT_SUSPEND:
  81125. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  81126. + "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
  81127. +
  81128. + if (core_if->power_down == 2) {
  81129. + dwc_otg_host_hibernation_restore(core_if, 0, 0);
  81130. + } else {
  81131. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  81132. + dwc_mdelay(5);
  81133. +
  81134. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  81135. + hprt0.b.prtres = 1;
  81136. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  81137. + hprt0.b.prtsusp = 0;
  81138. + /* Clear Resume bit */
  81139. + dwc_mdelay(100);
  81140. + hprt0.b.prtres = 0;
  81141. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  81142. + }
  81143. + break;
  81144. +#ifdef CONFIG_USB_DWC_OTG_LPM
  81145. + case UHF_PORT_L1:
  81146. + {
  81147. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  81148. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  81149. +
  81150. + lpmcfg.d32 =
  81151. + DWC_READ_REG32(&core_if->
  81152. + core_global_regs->glpmcfg);
  81153. + lpmcfg.b.en_utmi_sleep = 0;
  81154. + lpmcfg.b.hird_thres &= (~(1 << 4));
  81155. + lpmcfg.b.prt_sleep_sts = 1;
  81156. + DWC_WRITE_REG32(&core_if->
  81157. + core_global_regs->glpmcfg,
  81158. + lpmcfg.d32);
  81159. +
  81160. + /* Clear Enbl_L1Gating bit. */
  81161. + pcgcctl.b.enbl_sleep_gating = 1;
  81162. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
  81163. + 0);
  81164. +
  81165. + dwc_mdelay(5);
  81166. +
  81167. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  81168. + hprt0.b.prtres = 1;
  81169. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  81170. + hprt0.d32);
  81171. + /* This bit will be cleared in wakeup interrupt handle */
  81172. + break;
  81173. + }
  81174. +#endif
  81175. + case UHF_PORT_POWER:
  81176. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  81177. + "ClearPortFeature USB_PORT_FEAT_POWER\n");
  81178. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  81179. + hprt0.b.prtpwr = 0;
  81180. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  81181. + break;
  81182. + case UHF_PORT_INDICATOR:
  81183. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  81184. + "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
  81185. + /* Port inidicator not supported */
  81186. + break;
  81187. + case UHF_C_PORT_CONNECTION:
  81188. + /* Clears drivers internal connect status change
  81189. + * flag */
  81190. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  81191. + "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
  81192. + dwc_otg_hcd->flags.b.port_connect_status_change = 0;
  81193. + break;
  81194. + case UHF_C_PORT_RESET:
  81195. + /* Clears the driver's internal Port Reset Change
  81196. + * flag */
  81197. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  81198. + "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
  81199. + dwc_otg_hcd->flags.b.port_reset_change = 0;
  81200. + break;
  81201. + case UHF_C_PORT_ENABLE:
  81202. + /* Clears the driver's internal Port
  81203. + * Enable/Disable Change flag */
  81204. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  81205. + "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
  81206. + dwc_otg_hcd->flags.b.port_enable_change = 0;
  81207. + break;
  81208. + case UHF_C_PORT_SUSPEND:
  81209. + /* Clears the driver's internal Port Suspend
  81210. + * Change flag, which is set when resume signaling on
  81211. + * the host port is complete */
  81212. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  81213. + "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
  81214. + dwc_otg_hcd->flags.b.port_suspend_change = 0;
  81215. + break;
  81216. +#ifdef CONFIG_USB_DWC_OTG_LPM
  81217. + case UHF_C_PORT_L1:
  81218. + dwc_otg_hcd->flags.b.port_l1_change = 0;
  81219. + break;
  81220. +#endif
  81221. + case UHF_C_PORT_OVER_CURRENT:
  81222. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  81223. + "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
  81224. + dwc_otg_hcd->flags.b.port_over_current_change = 0;
  81225. + break;
  81226. + default:
  81227. + retval = -DWC_E_INVALID;
  81228. + DWC_ERROR("DWC OTG HCD - "
  81229. + "ClearPortFeature request %xh "
  81230. + "unknown or unsupported\n", wValue);
  81231. + }
  81232. + break;
  81233. + case UCR_GET_HUB_DESCRIPTOR:
  81234. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  81235. + "GetHubDescriptor\n");
  81236. + hub_desc = (usb_hub_descriptor_t *) buf;
  81237. + hub_desc->bDescLength = 9;
  81238. + hub_desc->bDescriptorType = 0x29;
  81239. + hub_desc->bNbrPorts = 1;
  81240. + USETW(hub_desc->wHubCharacteristics, 0x08);
  81241. + hub_desc->bPwrOn2PwrGood = 1;
  81242. + hub_desc->bHubContrCurrent = 0;
  81243. + hub_desc->DeviceRemovable[0] = 0;
  81244. + hub_desc->DeviceRemovable[1] = 0xff;
  81245. + break;
  81246. + case UCR_GET_HUB_STATUS:
  81247. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  81248. + "GetHubStatus\n");
  81249. + DWC_MEMSET(buf, 0, 4);
  81250. + break;
  81251. + case UCR_GET_PORT_STATUS:
  81252. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  81253. + "GetPortStatus wIndex = 0x%04x FLAGS=0x%08x\n",
  81254. + wIndex, dwc_otg_hcd->flags.d32);
  81255. + if (!wIndex || wIndex > 1)
  81256. + goto error;
  81257. +
  81258. + port_status = 0;
  81259. +
  81260. + if (dwc_otg_hcd->flags.b.port_connect_status_change)
  81261. + port_status |= (1 << UHF_C_PORT_CONNECTION);
  81262. +
  81263. + if (dwc_otg_hcd->flags.b.port_enable_change)
  81264. + port_status |= (1 << UHF_C_PORT_ENABLE);
  81265. +
  81266. + if (dwc_otg_hcd->flags.b.port_suspend_change)
  81267. + port_status |= (1 << UHF_C_PORT_SUSPEND);
  81268. +
  81269. + if (dwc_otg_hcd->flags.b.port_l1_change)
  81270. + port_status |= (1 << UHF_C_PORT_L1);
  81271. +
  81272. + if (dwc_otg_hcd->flags.b.port_reset_change) {
  81273. + port_status |= (1 << UHF_C_PORT_RESET);
  81274. + }
  81275. +
  81276. + if (dwc_otg_hcd->flags.b.port_over_current_change) {
  81277. + DWC_WARN("Overcurrent change detected\n");
  81278. + port_status |= (1 << UHF_C_PORT_OVER_CURRENT);
  81279. + }
  81280. +
  81281. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  81282. + /*
  81283. + * The port is disconnected, which means the core is
  81284. + * either in device mode or it soon will be. Just
  81285. + * return 0's for the remainder of the port status
  81286. + * since the port register can't be read if the core
  81287. + * is in device mode.
  81288. + */
  81289. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  81290. + break;
  81291. + }
  81292. +
  81293. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  81294. + DWC_DEBUGPL(DBG_HCDV, " HPRT0: 0x%08x\n", hprt0.d32);
  81295. +
  81296. + if (hprt0.b.prtconnsts)
  81297. + port_status |= (1 << UHF_PORT_CONNECTION);
  81298. +
  81299. + if (hprt0.b.prtena)
  81300. + port_status |= (1 << UHF_PORT_ENABLE);
  81301. +
  81302. + if (hprt0.b.prtsusp)
  81303. + port_status |= (1 << UHF_PORT_SUSPEND);
  81304. +
  81305. + if (hprt0.b.prtovrcurract)
  81306. + port_status |= (1 << UHF_PORT_OVER_CURRENT);
  81307. +
  81308. + if (hprt0.b.prtrst)
  81309. + port_status |= (1 << UHF_PORT_RESET);
  81310. +
  81311. + if (hprt0.b.prtpwr)
  81312. + port_status |= (1 << UHF_PORT_POWER);
  81313. +
  81314. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
  81315. + port_status |= (1 << UHF_PORT_HIGH_SPEED);
  81316. + else if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED)
  81317. + port_status |= (1 << UHF_PORT_LOW_SPEED);
  81318. +
  81319. + if (hprt0.b.prttstctl)
  81320. + port_status |= (1 << UHF_PORT_TEST);
  81321. + if (dwc_otg_get_lpm_portsleepstatus(dwc_otg_hcd->core_if)) {
  81322. + port_status |= (1 << UHF_PORT_L1);
  81323. + }
  81324. + /*
  81325. + For Synopsys HW emulation of Power down wkup_control asserts the
  81326. + hreset_n and prst_n on suspned. This causes the HPRT0 to be zero.
  81327. + We intentionally tell the software that port is in L2Suspend state.
  81328. + Only for STE.
  81329. + */
  81330. + if ((core_if->power_down == 2)
  81331. + && (core_if->hibernation_suspend == 1)) {
  81332. + port_status |= (1 << UHF_PORT_SUSPEND);
  81333. + }
  81334. + /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
  81335. +
  81336. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  81337. +
  81338. + break;
  81339. + case UCR_SET_HUB_FEATURE:
  81340. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  81341. + "SetHubFeature\n");
  81342. + /* No HUB features supported */
  81343. + break;
  81344. + case UCR_SET_PORT_FEATURE:
  81345. + if (wValue != UHF_PORT_TEST && (!wIndex || wIndex > 1))
  81346. + goto error;
  81347. +
  81348. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  81349. + /*
  81350. + * The port is disconnected, which means the core is
  81351. + * either in device mode or it soon will be. Just
  81352. + * return without doing anything since the port
  81353. + * register can't be written if the core is in device
  81354. + * mode.
  81355. + */
  81356. + break;
  81357. + }
  81358. +
  81359. + switch (wValue) {
  81360. + case UHF_PORT_SUSPEND:
  81361. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  81362. + "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
  81363. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) != wIndex) {
  81364. + goto error;
  81365. + }
  81366. + if (core_if->power_down == 2) {
  81367. + int timeout = 300;
  81368. + dwc_irqflags_t flags;
  81369. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  81370. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  81371. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  81372. +#ifdef DWC_DEV_SRPCAP
  81373. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  81374. +#endif
  81375. + DWC_PRINTF("Preparing for complete power-off\n");
  81376. +
  81377. + /* Save registers before hibernation */
  81378. + dwc_otg_save_global_regs(core_if);
  81379. + dwc_otg_save_host_regs(core_if);
  81380. +
  81381. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  81382. + hprt0.b.prtsusp = 1;
  81383. + hprt0.b.prtena = 0;
  81384. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  81385. + /* Spin hprt0.b.prtsusp to became 1 */
  81386. + do {
  81387. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  81388. + if (hprt0.b.prtsusp) {
  81389. + break;
  81390. + }
  81391. + dwc_mdelay(1);
  81392. + } while (--timeout);
  81393. + if (!timeout) {
  81394. + DWC_WARN("Suspend wasn't genereted\n");
  81395. + }
  81396. + dwc_udelay(10);
  81397. +
  81398. + /*
  81399. + * We need to disable interrupts to prevent servicing of any IRQ
  81400. + * during going to hibernation
  81401. + */
  81402. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  81403. + core_if->lx_state = DWC_OTG_L2;
  81404. +#ifdef DWC_DEV_SRPCAP
  81405. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  81406. + hprt0.b.prtpwr = 0;
  81407. + hprt0.b.prtena = 0;
  81408. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  81409. + hprt0.d32);
  81410. +#endif
  81411. + gusbcfg.d32 =
  81412. + DWC_READ_REG32(&core_if->core_global_regs->
  81413. + gusbcfg);
  81414. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  81415. + /* ULPI interface */
  81416. + /* Suspend the Phy Clock */
  81417. + pcgcctl.d32 = 0;
  81418. + pcgcctl.b.stoppclk = 1;
  81419. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  81420. + pcgcctl.d32);
  81421. + dwc_udelay(10);
  81422. + gpwrdn.b.pmuactv = 1;
  81423. + DWC_MODIFY_REG32(&core_if->
  81424. + core_global_regs->
  81425. + gpwrdn, 0, gpwrdn.d32);
  81426. + } else {
  81427. + /* UTMI+ Interface */
  81428. + gpwrdn.b.pmuactv = 1;
  81429. + DWC_MODIFY_REG32(&core_if->
  81430. + core_global_regs->
  81431. + gpwrdn, 0, gpwrdn.d32);
  81432. + dwc_udelay(10);
  81433. + pcgcctl.b.stoppclk = 1;
  81434. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  81435. + dwc_udelay(10);
  81436. + }
  81437. +#ifdef DWC_DEV_SRPCAP
  81438. + gpwrdn.d32 = 0;
  81439. + gpwrdn.b.dis_vbus = 1;
  81440. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  81441. + gpwrdn, 0, gpwrdn.d32);
  81442. +#endif
  81443. + gpwrdn.d32 = 0;
  81444. + gpwrdn.b.pmuintsel = 1;
  81445. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  81446. + gpwrdn, 0, gpwrdn.d32);
  81447. + dwc_udelay(10);
  81448. +
  81449. + gpwrdn.d32 = 0;
  81450. +#ifdef DWC_DEV_SRPCAP
  81451. + gpwrdn.b.srp_det_msk = 1;
  81452. +#endif
  81453. + gpwrdn.b.disconn_det_msk = 1;
  81454. + gpwrdn.b.lnstchng_msk = 1;
  81455. + gpwrdn.b.sts_chngint_msk = 1;
  81456. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  81457. + gpwrdn, 0, gpwrdn.d32);
  81458. + dwc_udelay(10);
  81459. +
  81460. + /* Enable Power Down Clamp and all interrupts in GPWRDN */
  81461. + gpwrdn.d32 = 0;
  81462. + gpwrdn.b.pwrdnclmp = 1;
  81463. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  81464. + gpwrdn, 0, gpwrdn.d32);
  81465. + dwc_udelay(10);
  81466. +
  81467. + /* Switch off VDD */
  81468. + gpwrdn.d32 = 0;
  81469. + gpwrdn.b.pwrdnswtch = 1;
  81470. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  81471. + gpwrdn, 0, gpwrdn.d32);
  81472. +
  81473. +#ifdef DWC_DEV_SRPCAP
  81474. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE)
  81475. + {
  81476. + core_if->pwron_timer_started = 1;
  81477. + DWC_TIMER_SCHEDULE(core_if->pwron_timer, 6000 /* 6 secs */ );
  81478. + }
  81479. +#endif
  81480. + /* Save gpwrdn register for further usage if stschng interrupt */
  81481. + core_if->gr_backup->gpwrdn_local =
  81482. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  81483. +
  81484. + /* Set flag to indicate that we are in hibernation */
  81485. + core_if->hibernation_suspend = 1;
  81486. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock,flags);
  81487. +
  81488. + DWC_PRINTF("Host hibernation completed\n");
  81489. + // Exit from case statement
  81490. + break;
  81491. +
  81492. + }
  81493. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) == wIndex &&
  81494. + dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  81495. + gotgctl_data_t gotgctl = {.d32 = 0 };
  81496. + gotgctl.b.hstsethnpen = 1;
  81497. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  81498. + gotgctl, 0, gotgctl.d32);
  81499. + core_if->op_state = A_SUSPEND;
  81500. + }
  81501. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  81502. + hprt0.b.prtsusp = 1;
  81503. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  81504. + {
  81505. + dwc_irqflags_t flags;
  81506. + /* Update lx_state */
  81507. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  81508. + core_if->lx_state = DWC_OTG_L2;
  81509. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  81510. + }
  81511. + /* Suspend the Phy Clock */
  81512. + {
  81513. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  81514. + pcgcctl.b.stoppclk = 1;
  81515. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  81516. + pcgcctl.d32);
  81517. + dwc_udelay(10);
  81518. + }
  81519. +
  81520. + /* For HNP the bus must be suspended for at least 200ms. */
  81521. + if (dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  81522. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  81523. + pcgcctl.b.stoppclk = 1;
  81524. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  81525. + dwc_mdelay(200);
  81526. + }
  81527. +
  81528. + /** @todo - check how sw can wait for 1 sec to check asesvld??? */
  81529. +#if 0 //vahrama !!!!!!!!!!!!!!!!!!
  81530. + if (core_if->adp_enable) {
  81531. + gotgctl_data_t gotgctl = {.d32 = 0 };
  81532. + gpwrdn_data_t gpwrdn;
  81533. +
  81534. + while (gotgctl.b.asesvld == 1) {
  81535. + gotgctl.d32 =
  81536. + DWC_READ_REG32(&core_if->
  81537. + core_global_regs->
  81538. + gotgctl);
  81539. + dwc_mdelay(100);
  81540. + }
  81541. +
  81542. + /* Enable Power Down Logic */
  81543. + gpwrdn.d32 = 0;
  81544. + gpwrdn.b.pmuactv = 1;
  81545. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  81546. + gpwrdn, 0, gpwrdn.d32);
  81547. +
  81548. + /* Unmask SRP detected interrupt from Power Down Logic */
  81549. + gpwrdn.d32 = 0;
  81550. + gpwrdn.b.srp_det_msk = 1;
  81551. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  81552. + gpwrdn, 0, gpwrdn.d32);
  81553. +
  81554. + dwc_otg_adp_probe_start(core_if);
  81555. + }
  81556. +#endif
  81557. + break;
  81558. + case UHF_PORT_POWER:
  81559. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  81560. + "SetPortFeature - USB_PORT_FEAT_POWER\n");
  81561. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  81562. + hprt0.b.prtpwr = 1;
  81563. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  81564. + break;
  81565. + case UHF_PORT_RESET:
  81566. + if ((core_if->power_down == 2)
  81567. + && (core_if->hibernation_suspend == 1)) {
  81568. + /* If we are going to exit from Hibernated
  81569. + * state via USB RESET.
  81570. + */
  81571. + dwc_otg_host_hibernation_restore(core_if, 0, 1);
  81572. + } else {
  81573. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  81574. +
  81575. + DWC_DEBUGPL(DBG_HCD,
  81576. + "DWC OTG HCD HUB CONTROL - "
  81577. + "SetPortFeature - USB_PORT_FEAT_RESET\n");
  81578. + {
  81579. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  81580. + pcgcctl.b.enbl_sleep_gating = 1;
  81581. + pcgcctl.b.stoppclk = 1;
  81582. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  81583. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  81584. + }
  81585. +#ifdef CONFIG_USB_DWC_OTG_LPM
  81586. + {
  81587. + glpmcfg_data_t lpmcfg;
  81588. + lpmcfg.d32 =
  81589. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  81590. + if (lpmcfg.b.prt_sleep_sts) {
  81591. + lpmcfg.b.en_utmi_sleep = 0;
  81592. + lpmcfg.b.hird_thres &= (~(1 << 4));
  81593. + DWC_WRITE_REG32
  81594. + (&core_if->core_global_regs->glpmcfg,
  81595. + lpmcfg.d32);
  81596. + dwc_mdelay(1);
  81597. + }
  81598. + }
  81599. +#endif
  81600. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  81601. + /* Clear suspend bit if resetting from suspended state. */
  81602. + hprt0.b.prtsusp = 0;
  81603. + /* When B-Host the Port reset bit is set in
  81604. + * the Start HCD Callback function, so that
  81605. + * the reset is started within 1ms of the HNP
  81606. + * success interrupt. */
  81607. + if (!dwc_otg_hcd_is_b_host(dwc_otg_hcd)) {
  81608. + hprt0.b.prtpwr = 1;
  81609. + hprt0.b.prtrst = 1;
  81610. + DWC_PRINTF("Indeed it is in host mode hprt0 = %08x\n",hprt0.d32);
  81611. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  81612. + hprt0.d32);
  81613. + }
  81614. + /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
  81615. + dwc_mdelay(60);
  81616. + hprt0.b.prtrst = 0;
  81617. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  81618. + core_if->lx_state = DWC_OTG_L0; /* Now back to the on state */
  81619. + }
  81620. + break;
  81621. +#ifdef DWC_HS_ELECT_TST
  81622. + case UHF_PORT_TEST:
  81623. + {
  81624. + uint32_t t;
  81625. + gintmsk_data_t gintmsk;
  81626. +
  81627. + t = (wIndex >> 8); /* MSB wIndex USB */
  81628. + DWC_DEBUGPL(DBG_HCD,
  81629. + "DWC OTG HCD HUB CONTROL - "
  81630. + "SetPortFeature - USB_PORT_FEAT_TEST %d\n",
  81631. + t);
  81632. + DWC_WARN("USB_PORT_FEAT_TEST %d\n", t);
  81633. + if (t < 6) {
  81634. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  81635. + hprt0.b.prttstctl = t;
  81636. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  81637. + hprt0.d32);
  81638. + } else {
  81639. + /* Setup global vars with reg addresses (quick and
  81640. + * dirty hack, should be cleaned up)
  81641. + */
  81642. + global_regs = core_if->core_global_regs;
  81643. + hc_global_regs =
  81644. + core_if->host_if->host_global_regs;
  81645. + hc_regs =
  81646. + (dwc_otg_hc_regs_t *) ((char *)
  81647. + global_regs +
  81648. + 0x500);
  81649. + data_fifo =
  81650. + (uint32_t *) ((char *)global_regs +
  81651. + 0x1000);
  81652. +
  81653. + if (t == 6) { /* HS_HOST_PORT_SUSPEND_RESUME */
  81654. + /* Save current interrupt mask */
  81655. + gintmsk.d32 =
  81656. + DWC_READ_REG32
  81657. + (&global_regs->gintmsk);
  81658. +
  81659. + /* Disable all interrupts while we muck with
  81660. + * the hardware directly
  81661. + */
  81662. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  81663. +
  81664. + /* 15 second delay per the test spec */
  81665. + dwc_mdelay(15000);
  81666. +
  81667. + /* Drive suspend on the root port */
  81668. + hprt0.d32 =
  81669. + dwc_otg_read_hprt0(core_if);
  81670. + hprt0.b.prtsusp = 1;
  81671. + hprt0.b.prtres = 0;
  81672. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  81673. +
  81674. + /* 15 second delay per the test spec */
  81675. + dwc_mdelay(15000);
  81676. +
  81677. + /* Drive resume on the root port */
  81678. + hprt0.d32 =
  81679. + dwc_otg_read_hprt0(core_if);
  81680. + hprt0.b.prtsusp = 0;
  81681. + hprt0.b.prtres = 1;
  81682. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  81683. + dwc_mdelay(100);
  81684. +
  81685. + /* Clear the resume bit */
  81686. + hprt0.b.prtres = 0;
  81687. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  81688. +
  81689. + /* Restore interrupts */
  81690. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  81691. + } else if (t == 7) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  81692. + /* Save current interrupt mask */
  81693. + gintmsk.d32 =
  81694. + DWC_READ_REG32
  81695. + (&global_regs->gintmsk);
  81696. +
  81697. + /* Disable all interrupts while we muck with
  81698. + * the hardware directly
  81699. + */
  81700. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  81701. +
  81702. + /* 15 second delay per the test spec */
  81703. + dwc_mdelay(15000);
  81704. +
  81705. + /* Send the Setup packet */
  81706. + do_setup();
  81707. +
  81708. + /* 15 second delay so nothing else happens for awhile */
  81709. + dwc_mdelay(15000);
  81710. +
  81711. + /* Restore interrupts */
  81712. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  81713. + } else if (t == 8) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  81714. + /* Save current interrupt mask */
  81715. + gintmsk.d32 =
  81716. + DWC_READ_REG32
  81717. + (&global_regs->gintmsk);
  81718. +
  81719. + /* Disable all interrupts while we muck with
  81720. + * the hardware directly
  81721. + */
  81722. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  81723. +
  81724. + /* Send the Setup packet */
  81725. + do_setup();
  81726. +
  81727. + /* 15 second delay so nothing else happens for awhile */
  81728. + dwc_mdelay(15000);
  81729. +
  81730. + /* Send the In and Ack packets */
  81731. + do_in_ack();
  81732. +
  81733. + /* 15 second delay so nothing else happens for awhile */
  81734. + dwc_mdelay(15000);
  81735. +
  81736. + /* Restore interrupts */
  81737. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  81738. + }
  81739. + }
  81740. + break;
  81741. + }
  81742. +#endif /* DWC_HS_ELECT_TST */
  81743. +
  81744. + case UHF_PORT_INDICATOR:
  81745. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  81746. + "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
  81747. + /* Not supported */
  81748. + break;
  81749. + default:
  81750. + retval = -DWC_E_INVALID;
  81751. + DWC_ERROR("DWC OTG HCD - "
  81752. + "SetPortFeature request %xh "
  81753. + "unknown or unsupported\n", wValue);
  81754. + break;
  81755. + }
  81756. + break;
  81757. +#ifdef CONFIG_USB_DWC_OTG_LPM
  81758. + case UCR_SET_AND_TEST_PORT_FEATURE:
  81759. + if (wValue != UHF_PORT_L1) {
  81760. + goto error;
  81761. + }
  81762. + {
  81763. + int portnum, hird, devaddr, remwake;
  81764. + glpmcfg_data_t lpmcfg;
  81765. + uint32_t time_usecs;
  81766. + gintsts_data_t gintsts;
  81767. + gintmsk_data_t gintmsk;
  81768. +
  81769. + if (!dwc_otg_get_param_lpm_enable(core_if)) {
  81770. + goto error;
  81771. + }
  81772. + if (wValue != UHF_PORT_L1 || wLength != 1) {
  81773. + goto error;
  81774. + }
  81775. + /* Check if the port currently is in SLEEP state */
  81776. + lpmcfg.d32 =
  81777. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  81778. + if (lpmcfg.b.prt_sleep_sts) {
  81779. + DWC_INFO("Port is already in sleep mode\n");
  81780. + buf[0] = 0; /* Return success */
  81781. + break;
  81782. + }
  81783. +
  81784. + portnum = wIndex & 0xf;
  81785. + hird = (wIndex >> 4) & 0xf;
  81786. + devaddr = (wIndex >> 8) & 0x7f;
  81787. + remwake = (wIndex >> 15);
  81788. +
  81789. + if (portnum != 1) {
  81790. + retval = -DWC_E_INVALID;
  81791. + DWC_WARN
  81792. + ("Wrong port number(%d) in SetandTestPortFeature request\n",
  81793. + portnum);
  81794. + break;
  81795. + }
  81796. +
  81797. + DWC_PRINTF
  81798. + ("SetandTestPortFeature request: portnum = %d, hird = %d, devaddr = %d, rewake = %d\n",
  81799. + portnum, hird, devaddr, remwake);
  81800. + /* Disable LPM interrupt */
  81801. + gintmsk.d32 = 0;
  81802. + gintmsk.b.lpmtranrcvd = 1;
  81803. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  81804. + gintmsk.d32, 0);
  81805. +
  81806. + if (dwc_otg_hcd_send_lpm
  81807. + (dwc_otg_hcd, devaddr, hird, remwake)) {
  81808. + retval = -DWC_E_INVALID;
  81809. + break;
  81810. + }
  81811. +
  81812. + time_usecs = 10 * (lpmcfg.b.retry_count + 1);
  81813. + /* We will consider timeout if time_usecs microseconds pass,
  81814. + * and we don't receive LPM transaction status.
  81815. + * After receiving non-error responce(ACK/NYET/STALL) from device,
  81816. + * core will set lpmtranrcvd bit.
  81817. + */
  81818. + do {
  81819. + gintsts.d32 =
  81820. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  81821. + if (gintsts.b.lpmtranrcvd) {
  81822. + break;
  81823. + }
  81824. + dwc_udelay(1);
  81825. + } while (--time_usecs);
  81826. + /* lpm_int bit will be cleared in LPM interrupt handler */
  81827. +
  81828. + /* Now fill status
  81829. + * 0x00 - Success
  81830. + * 0x10 - NYET
  81831. + * 0x11 - Timeout
  81832. + */
  81833. + if (!gintsts.b.lpmtranrcvd) {
  81834. + buf[0] = 0x3; /* Completion code is Timeout */
  81835. + dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd);
  81836. + } else {
  81837. + lpmcfg.d32 =
  81838. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  81839. + if (lpmcfg.b.lpm_resp == 0x3) {
  81840. + /* ACK responce from the device */
  81841. + buf[0] = 0x00; /* Success */
  81842. + } else if (lpmcfg.b.lpm_resp == 0x2) {
  81843. + /* NYET responce from the device */
  81844. + buf[0] = 0x2;
  81845. + } else {
  81846. + /* Otherwise responce with Timeout */
  81847. + buf[0] = 0x3;
  81848. + }
  81849. + }
  81850. + DWC_PRINTF("Device responce to LPM trans is %x\n",
  81851. + lpmcfg.b.lpm_resp);
  81852. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0,
  81853. + gintmsk.d32);
  81854. +
  81855. + break;
  81856. + }
  81857. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  81858. + default:
  81859. +error:
  81860. + retval = -DWC_E_INVALID;
  81861. + DWC_WARN("DWC OTG HCD - "
  81862. + "Unknown hub control request type or invalid typeReq: %xh wIndex: %xh wValue: %xh\n",
  81863. + typeReq, wIndex, wValue);
  81864. + break;
  81865. + }
  81866. +
  81867. + return retval;
  81868. +}
  81869. +
  81870. +#ifdef CONFIG_USB_DWC_OTG_LPM
  81871. +/** Returns index of host channel to perform LPM transaction. */
  81872. +int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd, uint8_t devaddr)
  81873. +{
  81874. + dwc_otg_core_if_t *core_if = hcd->core_if;
  81875. + dwc_hc_t *hc;
  81876. + hcchar_data_t hcchar;
  81877. + gintmsk_data_t gintmsk = {.d32 = 0 };
  81878. +
  81879. + if (DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  81880. + DWC_PRINTF("No free channel to select for LPM transaction\n");
  81881. + return -1;
  81882. + }
  81883. +
  81884. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  81885. +
  81886. + /* Mask host channel interrupts. */
  81887. + gintmsk.b.hcintr = 1;
  81888. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  81889. +
  81890. + /* Fill fields that core needs for LPM transaction */
  81891. + hcchar.b.devaddr = devaddr;
  81892. + hcchar.b.epnum = 0;
  81893. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  81894. + hcchar.b.mps = 64;
  81895. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  81896. + hcchar.b.epdir = 0; /* OUT */
  81897. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[hc->hc_num]->hcchar,
  81898. + hcchar.d32);
  81899. +
  81900. + /* Remove the host channel from the free list. */
  81901. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  81902. +
  81903. + DWC_PRINTF("hcnum = %d devaddr = %d\n", hc->hc_num, devaddr);
  81904. +
  81905. + return hc->hc_num;
  81906. +}
  81907. +
  81908. +/** Release hc after performing LPM transaction */
  81909. +void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd)
  81910. +{
  81911. + dwc_hc_t *hc;
  81912. + glpmcfg_data_t lpmcfg;
  81913. + uint8_t hc_num;
  81914. +
  81915. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  81916. + hc_num = lpmcfg.b.lpm_chan_index;
  81917. +
  81918. + hc = hcd->hc_ptr_array[hc_num];
  81919. +
  81920. + DWC_PRINTF("Freeing channel %d after LPM\n", hc_num);
  81921. + /* Return host channel to free list */
  81922. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  81923. +}
  81924. +
  81925. +int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr, uint8_t hird,
  81926. + uint8_t bRemoteWake)
  81927. +{
  81928. + glpmcfg_data_t lpmcfg;
  81929. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  81930. + int channel;
  81931. +
  81932. + channel = dwc_otg_hcd_get_hc_for_lpm_tran(hcd, devaddr);
  81933. + if (channel < 0) {
  81934. + return channel;
  81935. + }
  81936. +
  81937. + pcgcctl.b.enbl_sleep_gating = 1;
  81938. + DWC_MODIFY_REG32(hcd->core_if->pcgcctl, 0, pcgcctl.d32);
  81939. +
  81940. + /* Read LPM config register */
  81941. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  81942. +
  81943. + /* Program LPM transaction fields */
  81944. + lpmcfg.b.rem_wkup_en = bRemoteWake;
  81945. + lpmcfg.b.hird = hird;
  81946. + lpmcfg.b.hird_thres = 0x1c;
  81947. + lpmcfg.b.lpm_chan_index = channel;
  81948. + lpmcfg.b.en_utmi_sleep = 1;
  81949. + /* Program LPM config register */
  81950. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  81951. +
  81952. + /* Send LPM transaction */
  81953. + lpmcfg.b.send_lpm = 1;
  81954. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  81955. +
  81956. + return 0;
  81957. +}
  81958. +
  81959. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  81960. +
  81961. +int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port)
  81962. +{
  81963. + int retval;
  81964. +
  81965. + if (port != 1) {
  81966. + return -DWC_E_INVALID;
  81967. + }
  81968. +
  81969. + retval = (hcd->flags.b.port_connect_status_change ||
  81970. + hcd->flags.b.port_reset_change ||
  81971. + hcd->flags.b.port_enable_change ||
  81972. + hcd->flags.b.port_suspend_change ||
  81973. + hcd->flags.b.port_over_current_change);
  81974. +#ifdef DEBUG
  81975. + if (retval) {
  81976. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB STATUS DATA:"
  81977. + " Root port status changed\n");
  81978. + DWC_DEBUGPL(DBG_HCDV, " port_connect_status_change: %d\n",
  81979. + hcd->flags.b.port_connect_status_change);
  81980. + DWC_DEBUGPL(DBG_HCDV, " port_reset_change: %d\n",
  81981. + hcd->flags.b.port_reset_change);
  81982. + DWC_DEBUGPL(DBG_HCDV, " port_enable_change: %d\n",
  81983. + hcd->flags.b.port_enable_change);
  81984. + DWC_DEBUGPL(DBG_HCDV, " port_suspend_change: %d\n",
  81985. + hcd->flags.b.port_suspend_change);
  81986. + DWC_DEBUGPL(DBG_HCDV, " port_over_current_change: %d\n",
  81987. + hcd->flags.b.port_over_current_change);
  81988. + }
  81989. +#endif
  81990. + return retval;
  81991. +}
  81992. +
  81993. +int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * dwc_otg_hcd)
  81994. +{
  81995. + hfnum_data_t hfnum;
  81996. + hfnum.d32 =
  81997. + DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->
  81998. + hfnum);
  81999. +
  82000. +#ifdef DEBUG_SOF
  82001. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD GET FRAME NUMBER %d\n",
  82002. + hfnum.b.frnum);
  82003. +#endif
  82004. + return hfnum.b.frnum;
  82005. +}
  82006. +
  82007. +int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  82008. + struct dwc_otg_hcd_function_ops *fops)
  82009. +{
  82010. + int retval = 0;
  82011. +
  82012. + hcd->fops = fops;
  82013. + if (!dwc_otg_is_device_mode(hcd->core_if) &&
  82014. + (!hcd->core_if->adp_enable || hcd->core_if->adp.adp_started)) {
  82015. + dwc_otg_hcd_reinit(hcd);
  82016. + } else {
  82017. + retval = -DWC_E_NO_DEVICE;
  82018. + }
  82019. +
  82020. + return retval;
  82021. +}
  82022. +
  82023. +void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd)
  82024. +{
  82025. + return hcd->priv;
  82026. +}
  82027. +
  82028. +void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data)
  82029. +{
  82030. + hcd->priv = priv_data;
  82031. +}
  82032. +
  82033. +uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd)
  82034. +{
  82035. + return hcd->otg_port;
  82036. +}
  82037. +
  82038. +uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd)
  82039. +{
  82040. + uint32_t is_b_host;
  82041. + if (hcd->core_if->op_state == B_HOST) {
  82042. + is_b_host = 1;
  82043. + } else {
  82044. + is_b_host = 0;
  82045. + }
  82046. +
  82047. + return is_b_host;
  82048. +}
  82049. +
  82050. +dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  82051. + int iso_desc_count, int atomic_alloc)
  82052. +{
  82053. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  82054. + uint32_t size;
  82055. +
  82056. + size =
  82057. + sizeof(*dwc_otg_urb) +
  82058. + iso_desc_count * sizeof(struct dwc_otg_hcd_iso_packet_desc);
  82059. + if (atomic_alloc)
  82060. + dwc_otg_urb = DWC_ALLOC_ATOMIC(size);
  82061. + else
  82062. + dwc_otg_urb = DWC_ALLOC(size);
  82063. +
  82064. + if (dwc_otg_urb)
  82065. + dwc_otg_urb->packet_count = iso_desc_count;
  82066. + else {
  82067. + DWC_ERROR("**** DWC OTG HCD URB alloc - "
  82068. + "%salloc of %db failed\n",
  82069. + atomic_alloc?"atomic ":"", size);
  82070. + }
  82071. + return dwc_otg_urb;
  82072. +}
  82073. +
  82074. +void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * dwc_otg_urb,
  82075. + uint8_t dev_addr, uint8_t ep_num,
  82076. + uint8_t ep_type, uint8_t ep_dir, uint16_t mps)
  82077. +{
  82078. + dwc_otg_hcd_fill_pipe(&dwc_otg_urb->pipe_info, dev_addr, ep_num,
  82079. + ep_type, ep_dir, mps);
  82080. +#if 0
  82081. + DWC_PRINTF
  82082. + ("addr = %d, ep_num = %d, ep_dir = 0x%x, ep_type = 0x%x, mps = %d\n",
  82083. + dev_addr, ep_num, ep_dir, ep_type, mps);
  82084. +#endif
  82085. +}
  82086. +
  82087. +void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  82088. + void *urb_handle, void *buf, dwc_dma_t dma,
  82089. + uint32_t buflen, void *setup_packet,
  82090. + dwc_dma_t setup_dma, uint32_t flags,
  82091. + uint16_t interval)
  82092. +{
  82093. + dwc_otg_urb->priv = urb_handle;
  82094. + dwc_otg_urb->buf = buf;
  82095. + dwc_otg_urb->dma = dma;
  82096. + dwc_otg_urb->length = buflen;
  82097. + dwc_otg_urb->setup_packet = setup_packet;
  82098. + dwc_otg_urb->setup_dma = setup_dma;
  82099. + dwc_otg_urb->flags = flags;
  82100. + dwc_otg_urb->interval = interval;
  82101. + dwc_otg_urb->status = -DWC_E_IN_PROGRESS;
  82102. +}
  82103. +
  82104. +uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb)
  82105. +{
  82106. + return dwc_otg_urb->status;
  82107. +}
  82108. +
  82109. +uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t * dwc_otg_urb)
  82110. +{
  82111. + return dwc_otg_urb->actual_length;
  82112. +}
  82113. +
  82114. +uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t * dwc_otg_urb)
  82115. +{
  82116. + return dwc_otg_urb->error_count;
  82117. +}
  82118. +
  82119. +void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  82120. + int desc_num, uint32_t offset,
  82121. + uint32_t length)
  82122. +{
  82123. + dwc_otg_urb->iso_descs[desc_num].offset = offset;
  82124. + dwc_otg_urb->iso_descs[desc_num].length = length;
  82125. +}
  82126. +
  82127. +uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t * dwc_otg_urb,
  82128. + int desc_num)
  82129. +{
  82130. + return dwc_otg_urb->iso_descs[desc_num].status;
  82131. +}
  82132. +
  82133. +uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  82134. + dwc_otg_urb, int desc_num)
  82135. +{
  82136. + return dwc_otg_urb->iso_descs[desc_num].actual_length;
  82137. +}
  82138. +
  82139. +int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd, void *ep_handle)
  82140. +{
  82141. + int allocated = 0;
  82142. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  82143. +
  82144. + if (qh) {
  82145. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  82146. + allocated = 1;
  82147. + }
  82148. + }
  82149. + return allocated;
  82150. +}
  82151. +
  82152. +int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle)
  82153. +{
  82154. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  82155. + int freed = 0;
  82156. + DWC_ASSERT(qh, "qh is not allocated\n");
  82157. +
  82158. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  82159. + freed = 1;
  82160. + }
  82161. +
  82162. + return freed;
  82163. +}
  82164. +
  82165. +uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd, void *ep_handle)
  82166. +{
  82167. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  82168. + DWC_ASSERT(qh, "qh is not allocated\n");
  82169. + return qh->usecs;
  82170. +}
  82171. +
  82172. +void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd)
  82173. +{
  82174. +#ifdef DEBUG
  82175. + int num_channels;
  82176. + int i;
  82177. + gnptxsts_data_t np_tx_status;
  82178. + hptxsts_data_t p_tx_status;
  82179. +
  82180. + num_channels = hcd->core_if->core_params->host_channels;
  82181. + DWC_PRINTF("\n");
  82182. + DWC_PRINTF
  82183. + ("************************************************************\n");
  82184. + DWC_PRINTF("HCD State:\n");
  82185. + DWC_PRINTF(" Num channels: %d\n", num_channels);
  82186. + for (i = 0; i < num_channels; i++) {
  82187. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  82188. + DWC_PRINTF(" Channel %d:\n", i);
  82189. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  82190. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  82191. + DWC_PRINTF(" speed: %d\n", hc->speed);
  82192. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  82193. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  82194. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  82195. + DWC_PRINTF(" multi_count: %d\n", hc->multi_count);
  82196. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  82197. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  82198. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  82199. + DWC_PRINTF(" xfer_count: %d\n", hc->xfer_count);
  82200. + DWC_PRINTF(" halt_on_queue: %d\n", hc->halt_on_queue);
  82201. + DWC_PRINTF(" halt_pending: %d\n", hc->halt_pending);
  82202. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  82203. + DWC_PRINTF(" do_split: %d\n", hc->do_split);
  82204. + DWC_PRINTF(" complete_split: %d\n", hc->complete_split);
  82205. + DWC_PRINTF(" hub_addr: %d\n", hc->hub_addr);
  82206. + DWC_PRINTF(" port_addr: %d\n", hc->port_addr);
  82207. + DWC_PRINTF(" xact_pos: %d\n", hc->xact_pos);
  82208. + DWC_PRINTF(" requests: %d\n", hc->requests);
  82209. + DWC_PRINTF(" qh: %p\n", hc->qh);
  82210. + if (hc->xfer_started) {
  82211. + hfnum_data_t hfnum;
  82212. + hcchar_data_t hcchar;
  82213. + hctsiz_data_t hctsiz;
  82214. + hcint_data_t hcint;
  82215. + hcintmsk_data_t hcintmsk;
  82216. + hfnum.d32 =
  82217. + DWC_READ_REG32(&hcd->core_if->
  82218. + host_if->host_global_regs->hfnum);
  82219. + hcchar.d32 =
  82220. + DWC_READ_REG32(&hcd->core_if->host_if->
  82221. + hc_regs[i]->hcchar);
  82222. + hctsiz.d32 =
  82223. + DWC_READ_REG32(&hcd->core_if->host_if->
  82224. + hc_regs[i]->hctsiz);
  82225. + hcint.d32 =
  82226. + DWC_READ_REG32(&hcd->core_if->host_if->
  82227. + hc_regs[i]->hcint);
  82228. + hcintmsk.d32 =
  82229. + DWC_READ_REG32(&hcd->core_if->host_if->
  82230. + hc_regs[i]->hcintmsk);
  82231. + DWC_PRINTF(" hfnum: 0x%08x\n", hfnum.d32);
  82232. + DWC_PRINTF(" hcchar: 0x%08x\n", hcchar.d32);
  82233. + DWC_PRINTF(" hctsiz: 0x%08x\n", hctsiz.d32);
  82234. + DWC_PRINTF(" hcint: 0x%08x\n", hcint.d32);
  82235. + DWC_PRINTF(" hcintmsk: 0x%08x\n", hcintmsk.d32);
  82236. + }
  82237. + if (hc->xfer_started && hc->qh) {
  82238. + dwc_otg_qtd_t *qtd;
  82239. + dwc_otg_hcd_urb_t *urb;
  82240. +
  82241. + DWC_CIRCLEQ_FOREACH(qtd, &hc->qh->qtd_list, qtd_list_entry) {
  82242. + if (!qtd->in_process)
  82243. + break;
  82244. +
  82245. + urb = qtd->urb;
  82246. + DWC_PRINTF(" URB Info:\n");
  82247. + DWC_PRINTF(" qtd: %p, urb: %p\n", qtd, urb);
  82248. + if (urb) {
  82249. + DWC_PRINTF(" Dev: %d, EP: %d %s\n",
  82250. + dwc_otg_hcd_get_dev_addr(&urb->
  82251. + pipe_info),
  82252. + dwc_otg_hcd_get_ep_num(&urb->
  82253. + pipe_info),
  82254. + dwc_otg_hcd_is_pipe_in(&urb->
  82255. + pipe_info) ?
  82256. + "IN" : "OUT");
  82257. + DWC_PRINTF(" Max packet size: %d\n",
  82258. + dwc_otg_hcd_get_mps(&urb->
  82259. + pipe_info));
  82260. + DWC_PRINTF(" transfer_buffer: %p\n",
  82261. + urb->buf);
  82262. + DWC_PRINTF(" transfer_dma: %p\n",
  82263. + (void *)urb->dma);
  82264. + DWC_PRINTF(" transfer_buffer_length: %d\n",
  82265. + urb->length);
  82266. + DWC_PRINTF(" actual_length: %d\n",
  82267. + urb->actual_length);
  82268. + }
  82269. + }
  82270. + }
  82271. + }
  82272. + DWC_PRINTF(" non_periodic_channels: %d\n", hcd->non_periodic_channels);
  82273. + DWC_PRINTF(" periodic_channels: %d\n", hcd->periodic_channels);
  82274. + DWC_PRINTF(" periodic_usecs: %d\n", hcd->periodic_usecs);
  82275. + np_tx_status.d32 =
  82276. + DWC_READ_REG32(&hcd->core_if->core_global_regs->gnptxsts);
  82277. + DWC_PRINTF(" NP Tx Req Queue Space Avail: %d\n",
  82278. + np_tx_status.b.nptxqspcavail);
  82279. + DWC_PRINTF(" NP Tx FIFO Space Avail: %d\n",
  82280. + np_tx_status.b.nptxfspcavail);
  82281. + p_tx_status.d32 =
  82282. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hptxsts);
  82283. + DWC_PRINTF(" P Tx Req Queue Space Avail: %d\n",
  82284. + p_tx_status.b.ptxqspcavail);
  82285. + DWC_PRINTF(" P Tx FIFO Space Avail: %d\n", p_tx_status.b.ptxfspcavail);
  82286. + dwc_otg_hcd_dump_frrem(hcd);
  82287. + dwc_otg_dump_global_registers(hcd->core_if);
  82288. + dwc_otg_dump_host_registers(hcd->core_if);
  82289. + DWC_PRINTF
  82290. + ("************************************************************\n");
  82291. + DWC_PRINTF("\n");
  82292. +#endif
  82293. +}
  82294. +
  82295. +#ifdef DEBUG
  82296. +void dwc_print_setup_data(uint8_t * setup)
  82297. +{
  82298. + int i;
  82299. + if (CHK_DEBUG_LEVEL(DBG_HCD)) {
  82300. + DWC_PRINTF("Setup Data = MSB ");
  82301. + for (i = 7; i >= 0; i--)
  82302. + DWC_PRINTF("%02x ", setup[i]);
  82303. + DWC_PRINTF("\n");
  82304. + DWC_PRINTF(" bmRequestType Tranfer = %s\n",
  82305. + (setup[0] & 0x80) ? "Device-to-Host" :
  82306. + "Host-to-Device");
  82307. + DWC_PRINTF(" bmRequestType Type = ");
  82308. + switch ((setup[0] & 0x60) >> 5) {
  82309. + case 0:
  82310. + DWC_PRINTF("Standard\n");
  82311. + break;
  82312. + case 1:
  82313. + DWC_PRINTF("Class\n");
  82314. + break;
  82315. + case 2:
  82316. + DWC_PRINTF("Vendor\n");
  82317. + break;
  82318. + case 3:
  82319. + DWC_PRINTF("Reserved\n");
  82320. + break;
  82321. + }
  82322. + DWC_PRINTF(" bmRequestType Recipient = ");
  82323. + switch (setup[0] & 0x1f) {
  82324. + case 0:
  82325. + DWC_PRINTF("Device\n");
  82326. + break;
  82327. + case 1:
  82328. + DWC_PRINTF("Interface\n");
  82329. + break;
  82330. + case 2:
  82331. + DWC_PRINTF("Endpoint\n");
  82332. + break;
  82333. + case 3:
  82334. + DWC_PRINTF("Other\n");
  82335. + break;
  82336. + default:
  82337. + DWC_PRINTF("Reserved\n");
  82338. + break;
  82339. + }
  82340. + DWC_PRINTF(" bRequest = 0x%0x\n", setup[1]);
  82341. + DWC_PRINTF(" wValue = 0x%0x\n", *((uint16_t *) & setup[2]));
  82342. + DWC_PRINTF(" wIndex = 0x%0x\n", *((uint16_t *) & setup[4]));
  82343. + DWC_PRINTF(" wLength = 0x%0x\n\n", *((uint16_t *) & setup[6]));
  82344. + }
  82345. +}
  82346. +#endif
  82347. +
  82348. +void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd)
  82349. +{
  82350. +#if 0
  82351. + DWC_PRINTF("Frame remaining at SOF:\n");
  82352. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  82353. + hcd->frrem_samples, hcd->frrem_accum,
  82354. + (hcd->frrem_samples > 0) ?
  82355. + hcd->frrem_accum / hcd->frrem_samples : 0);
  82356. +
  82357. + DWC_PRINTF("\n");
  82358. + DWC_PRINTF("Frame remaining at start_transfer (uframe 7):\n");
  82359. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  82360. + hcd->core_if->hfnum_7_samples,
  82361. + hcd->core_if->hfnum_7_frrem_accum,
  82362. + (hcd->core_if->hfnum_7_samples >
  82363. + 0) ? hcd->core_if->hfnum_7_frrem_accum /
  82364. + hcd->core_if->hfnum_7_samples : 0);
  82365. + DWC_PRINTF("Frame remaining at start_transfer (uframe 0):\n");
  82366. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  82367. + hcd->core_if->hfnum_0_samples,
  82368. + hcd->core_if->hfnum_0_frrem_accum,
  82369. + (hcd->core_if->hfnum_0_samples >
  82370. + 0) ? hcd->core_if->hfnum_0_frrem_accum /
  82371. + hcd->core_if->hfnum_0_samples : 0);
  82372. + DWC_PRINTF("Frame remaining at start_transfer (uframe 1-6):\n");
  82373. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  82374. + hcd->core_if->hfnum_other_samples,
  82375. + hcd->core_if->hfnum_other_frrem_accum,
  82376. + (hcd->core_if->hfnum_other_samples >
  82377. + 0) ? hcd->core_if->hfnum_other_frrem_accum /
  82378. + hcd->core_if->hfnum_other_samples : 0);
  82379. +
  82380. + DWC_PRINTF("\n");
  82381. + DWC_PRINTF("Frame remaining at sample point A (uframe 7):\n");
  82382. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  82383. + hcd->hfnum_7_samples_a, hcd->hfnum_7_frrem_accum_a,
  82384. + (hcd->hfnum_7_samples_a > 0) ?
  82385. + hcd->hfnum_7_frrem_accum_a / hcd->hfnum_7_samples_a : 0);
  82386. + DWC_PRINTF("Frame remaining at sample point A (uframe 0):\n");
  82387. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  82388. + hcd->hfnum_0_samples_a, hcd->hfnum_0_frrem_accum_a,
  82389. + (hcd->hfnum_0_samples_a > 0) ?
  82390. + hcd->hfnum_0_frrem_accum_a / hcd->hfnum_0_samples_a : 0);
  82391. + DWC_PRINTF("Frame remaining at sample point A (uframe 1-6):\n");
  82392. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  82393. + hcd->hfnum_other_samples_a, hcd->hfnum_other_frrem_accum_a,
  82394. + (hcd->hfnum_other_samples_a > 0) ?
  82395. + hcd->hfnum_other_frrem_accum_a /
  82396. + hcd->hfnum_other_samples_a : 0);
  82397. +
  82398. + DWC_PRINTF("\n");
  82399. + DWC_PRINTF("Frame remaining at sample point B (uframe 7):\n");
  82400. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  82401. + hcd->hfnum_7_samples_b, hcd->hfnum_7_frrem_accum_b,
  82402. + (hcd->hfnum_7_samples_b > 0) ?
  82403. + hcd->hfnum_7_frrem_accum_b / hcd->hfnum_7_samples_b : 0);
  82404. + DWC_PRINTF("Frame remaining at sample point B (uframe 0):\n");
  82405. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  82406. + hcd->hfnum_0_samples_b, hcd->hfnum_0_frrem_accum_b,
  82407. + (hcd->hfnum_0_samples_b > 0) ?
  82408. + hcd->hfnum_0_frrem_accum_b / hcd->hfnum_0_samples_b : 0);
  82409. + DWC_PRINTF("Frame remaining at sample point B (uframe 1-6):\n");
  82410. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  82411. + hcd->hfnum_other_samples_b, hcd->hfnum_other_frrem_accum_b,
  82412. + (hcd->hfnum_other_samples_b > 0) ?
  82413. + hcd->hfnum_other_frrem_accum_b /
  82414. + hcd->hfnum_other_samples_b : 0);
  82415. +#endif
  82416. +}
  82417. +
  82418. +#endif /* DWC_DEVICE_ONLY */
  82419. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
  82420. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c 1969-12-31 18:00:00.000000000 -0600
  82421. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c 2014-12-03 19:13:40.220418001 -0600
  82422. @@ -0,0 +1,1132 @@
  82423. +/*==========================================================================
  82424. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_ddma.c $
  82425. + * $Revision: #10 $
  82426. + * $Date: 2011/10/20 $
  82427. + * $Change: 1869464 $
  82428. + *
  82429. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  82430. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  82431. + * otherwise expressly agreed to in writing between Synopsys and you.
  82432. + *
  82433. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  82434. + * any End User Software License Agreement or Agreement for Licensed Product
  82435. + * with Synopsys or any supplement thereto. You are permitted to use and
  82436. + * redistribute this Software in source and binary forms, with or without
  82437. + * modification, provided that redistributions of source code must retain this
  82438. + * notice. You may not view, use, disclose, copy or distribute this file or
  82439. + * any information contained herein except pursuant to this license grant from
  82440. + * Synopsys. If you do not agree with this notice, including the disclaimer
  82441. + * below, then you are not authorized to use the Software.
  82442. + *
  82443. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  82444. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  82445. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  82446. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  82447. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  82448. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  82449. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  82450. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  82451. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  82452. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  82453. + * DAMAGE.
  82454. + * ========================================================================== */
  82455. +#ifndef DWC_DEVICE_ONLY
  82456. +
  82457. +/** @file
  82458. + * This file contains Descriptor DMA support implementation for host mode.
  82459. + */
  82460. +
  82461. +#include "dwc_otg_hcd.h"
  82462. +#include "dwc_otg_regs.h"
  82463. +
  82464. +extern bool microframe_schedule;
  82465. +
  82466. +static inline uint8_t frame_list_idx(uint16_t frame)
  82467. +{
  82468. + return (frame & (MAX_FRLIST_EN_NUM - 1));
  82469. +}
  82470. +
  82471. +static inline uint16_t desclist_idx_inc(uint16_t idx, uint16_t inc, uint8_t speed)
  82472. +{
  82473. + return (idx + inc) &
  82474. + (((speed ==
  82475. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  82476. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  82477. +}
  82478. +
  82479. +static inline uint16_t desclist_idx_dec(uint16_t idx, uint16_t inc, uint8_t speed)
  82480. +{
  82481. + return (idx - inc) &
  82482. + (((speed ==
  82483. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  82484. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  82485. +}
  82486. +
  82487. +static inline uint16_t max_desc_num(dwc_otg_qh_t * qh)
  82488. +{
  82489. + return (((qh->ep_type == UE_ISOCHRONOUS)
  82490. + && (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH))
  82491. + ? MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC);
  82492. +}
  82493. +static inline uint16_t frame_incr_val(dwc_otg_qh_t * qh)
  82494. +{
  82495. + return ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH)
  82496. + ? ((qh->interval + 8 - 1) / 8)
  82497. + : qh->interval);
  82498. +}
  82499. +
  82500. +static int desc_list_alloc(dwc_otg_qh_t * qh)
  82501. +{
  82502. + int retval = 0;
  82503. +
  82504. + qh->desc_list = (dwc_otg_host_dma_desc_t *)
  82505. + DWC_DMA_ALLOC(sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh),
  82506. + &qh->desc_list_dma);
  82507. +
  82508. + if (!qh->desc_list) {
  82509. + retval = -DWC_E_NO_MEMORY;
  82510. + DWC_ERROR("%s: DMA descriptor list allocation failed\n", __func__);
  82511. +
  82512. + }
  82513. +
  82514. + dwc_memset(qh->desc_list, 0x00,
  82515. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  82516. +
  82517. + qh->n_bytes =
  82518. + (uint32_t *) DWC_ALLOC(sizeof(uint32_t) * max_desc_num(qh));
  82519. +
  82520. + if (!qh->n_bytes) {
  82521. + retval = -DWC_E_NO_MEMORY;
  82522. + DWC_ERROR
  82523. + ("%s: Failed to allocate array for descriptors' size actual values\n",
  82524. + __func__);
  82525. +
  82526. + }
  82527. + return retval;
  82528. +
  82529. +}
  82530. +
  82531. +static void desc_list_free(dwc_otg_qh_t * qh)
  82532. +{
  82533. + if (qh->desc_list) {
  82534. + DWC_DMA_FREE(max_desc_num(qh), qh->desc_list,
  82535. + qh->desc_list_dma);
  82536. + qh->desc_list = NULL;
  82537. + }
  82538. +
  82539. + if (qh->n_bytes) {
  82540. + DWC_FREE(qh->n_bytes);
  82541. + qh->n_bytes = NULL;
  82542. + }
  82543. +}
  82544. +
  82545. +static int frame_list_alloc(dwc_otg_hcd_t * hcd)
  82546. +{
  82547. + int retval = 0;
  82548. + if (hcd->frame_list)
  82549. + return 0;
  82550. +
  82551. + hcd->frame_list = DWC_DMA_ALLOC(4 * MAX_FRLIST_EN_NUM,
  82552. + &hcd->frame_list_dma);
  82553. + if (!hcd->frame_list) {
  82554. + retval = -DWC_E_NO_MEMORY;
  82555. + DWC_ERROR("%s: Frame List allocation failed\n", __func__);
  82556. + }
  82557. +
  82558. + dwc_memset(hcd->frame_list, 0x00, 4 * MAX_FRLIST_EN_NUM);
  82559. +
  82560. + return retval;
  82561. +}
  82562. +
  82563. +static void frame_list_free(dwc_otg_hcd_t * hcd)
  82564. +{
  82565. + if (!hcd->frame_list)
  82566. + return;
  82567. +
  82568. + DWC_DMA_FREE(4 * MAX_FRLIST_EN_NUM, hcd->frame_list, hcd->frame_list_dma);
  82569. + hcd->frame_list = NULL;
  82570. +}
  82571. +
  82572. +static void per_sched_enable(dwc_otg_hcd_t * hcd, uint16_t fr_list_en)
  82573. +{
  82574. +
  82575. + hcfg_data_t hcfg;
  82576. +
  82577. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  82578. +
  82579. + if (hcfg.b.perschedena) {
  82580. + /* already enabled */
  82581. + return;
  82582. + }
  82583. +
  82584. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hflbaddr,
  82585. + hcd->frame_list_dma);
  82586. +
  82587. + switch (fr_list_en) {
  82588. + case 64:
  82589. + hcfg.b.frlisten = 3;
  82590. + break;
  82591. + case 32:
  82592. + hcfg.b.frlisten = 2;
  82593. + break;
  82594. + case 16:
  82595. + hcfg.b.frlisten = 1;
  82596. + break;
  82597. + case 8:
  82598. + hcfg.b.frlisten = 0;
  82599. + break;
  82600. + default:
  82601. + break;
  82602. + }
  82603. +
  82604. + hcfg.b.perschedena = 1;
  82605. +
  82606. + DWC_DEBUGPL(DBG_HCD, "Enabling Periodic schedule\n");
  82607. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  82608. +
  82609. +}
  82610. +
  82611. +static void per_sched_disable(dwc_otg_hcd_t * hcd)
  82612. +{
  82613. + hcfg_data_t hcfg;
  82614. +
  82615. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  82616. +
  82617. + if (!hcfg.b.perschedena) {
  82618. + /* already disabled */
  82619. + return;
  82620. + }
  82621. + hcfg.b.perschedena = 0;
  82622. +
  82623. + DWC_DEBUGPL(DBG_HCD, "Disabling Periodic schedule\n");
  82624. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  82625. +}
  82626. +
  82627. +/*
  82628. + * Activates/Deactivates FrameList entries for the channel
  82629. + * based on endpoint servicing period.
  82630. + */
  82631. +void update_frame_list(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, uint8_t enable)
  82632. +{
  82633. + uint16_t i, j, inc;
  82634. + dwc_hc_t *hc = NULL;
  82635. +
  82636. + if (!qh->channel) {
  82637. + DWC_ERROR("qh->channel = %p", qh->channel);
  82638. + return;
  82639. + }
  82640. +
  82641. + if (!hcd) {
  82642. + DWC_ERROR("------hcd = %p", hcd);
  82643. + return;
  82644. + }
  82645. +
  82646. + if (!hcd->frame_list) {
  82647. + DWC_ERROR("-------hcd->frame_list = %p", hcd->frame_list);
  82648. + return;
  82649. + }
  82650. +
  82651. + hc = qh->channel;
  82652. + inc = frame_incr_val(qh);
  82653. + if (qh->ep_type == UE_ISOCHRONOUS)
  82654. + i = frame_list_idx(qh->sched_frame);
  82655. + else
  82656. + i = 0;
  82657. +
  82658. + j = i;
  82659. + do {
  82660. + if (enable)
  82661. + hcd->frame_list[j] |= (1 << hc->hc_num);
  82662. + else
  82663. + hcd->frame_list[j] &= ~(1 << hc->hc_num);
  82664. + j = (j + inc) & (MAX_FRLIST_EN_NUM - 1);
  82665. + }
  82666. + while (j != i);
  82667. + if (!enable)
  82668. + return;
  82669. + hc->schinfo = 0;
  82670. + if (qh->channel->speed == DWC_OTG_EP_SPEED_HIGH) {
  82671. + j = 1;
  82672. + /* TODO - check this */
  82673. + inc = (8 + qh->interval - 1) / qh->interval;
  82674. + for (i = 0; i < inc; i++) {
  82675. + hc->schinfo |= j;
  82676. + j = j << qh->interval;
  82677. + }
  82678. + } else {
  82679. + hc->schinfo = 0xff;
  82680. + }
  82681. +}
  82682. +
  82683. +#if 1
  82684. +void dump_frame_list(dwc_otg_hcd_t * hcd)
  82685. +{
  82686. + int i = 0;
  82687. + DWC_PRINTF("--FRAME LIST (hex) --\n");
  82688. + for (i = 0; i < MAX_FRLIST_EN_NUM; i++) {
  82689. + DWC_PRINTF("%x\t", hcd->frame_list[i]);
  82690. + if (!(i % 8) && i)
  82691. + DWC_PRINTF("\n");
  82692. + }
  82693. + DWC_PRINTF("\n----\n");
  82694. +
  82695. +}
  82696. +#endif
  82697. +
  82698. +static void release_channel_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  82699. +{
  82700. + dwc_irqflags_t flags;
  82701. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  82702. +
  82703. + dwc_hc_t *hc = qh->channel;
  82704. + if (dwc_qh_is_non_per(qh)) {
  82705. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  82706. + if (!microframe_schedule)
  82707. + hcd->non_periodic_channels--;
  82708. + else
  82709. + hcd->available_host_channels++;
  82710. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  82711. + } else
  82712. + update_frame_list(hcd, qh, 0);
  82713. +
  82714. + /*
  82715. + * The condition is added to prevent double cleanup try in case of device
  82716. + * disconnect. See channel cleanup in dwc_otg_hcd_disconnect_cb().
  82717. + */
  82718. + if (hc->qh) {
  82719. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  82720. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  82721. + hc->qh = NULL;
  82722. + }
  82723. +
  82724. + qh->channel = NULL;
  82725. + qh->ntd = 0;
  82726. +
  82727. + if (qh->desc_list) {
  82728. + dwc_memset(qh->desc_list, 0x00,
  82729. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  82730. + }
  82731. +}
  82732. +
  82733. +/**
  82734. + * Initializes a QH structure's Descriptor DMA related members.
  82735. + * Allocates memory for descriptor list.
  82736. + * On first periodic QH, allocates memory for FrameList
  82737. + * and enables periodic scheduling.
  82738. + *
  82739. + * @param hcd The HCD state structure for the DWC OTG controller.
  82740. + * @param qh The QH to init.
  82741. + *
  82742. + * @return 0 if successful, negative error code otherwise.
  82743. + */
  82744. +int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  82745. +{
  82746. + int retval = 0;
  82747. +
  82748. + if (qh->do_split) {
  82749. + DWC_ERROR("SPLIT Transfers are not supported in Descriptor DMA.\n");
  82750. + return -1;
  82751. + }
  82752. +
  82753. + retval = desc_list_alloc(qh);
  82754. +
  82755. + if ((retval == 0)
  82756. + && (qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)) {
  82757. + if (!hcd->frame_list) {
  82758. + retval = frame_list_alloc(hcd);
  82759. + /* Enable periodic schedule on first periodic QH */
  82760. + if (retval == 0)
  82761. + per_sched_enable(hcd, MAX_FRLIST_EN_NUM);
  82762. + }
  82763. + }
  82764. +
  82765. + qh->ntd = 0;
  82766. +
  82767. + return retval;
  82768. +}
  82769. +
  82770. +/**
  82771. + * Frees descriptor list memory associated with the QH.
  82772. + * If QH is periodic and the last, frees FrameList memory
  82773. + * and disables periodic scheduling.
  82774. + *
  82775. + * @param hcd The HCD state structure for the DWC OTG controller.
  82776. + * @param qh The QH to init.
  82777. + */
  82778. +void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  82779. +{
  82780. + desc_list_free(qh);
  82781. +
  82782. + /*
  82783. + * Channel still assigned due to some reasons.
  82784. + * Seen on Isoc URB dequeue. Channel halted but no subsequent
  82785. + * ChHalted interrupt to release the channel. Afterwards
  82786. + * when it comes here from endpoint disable routine
  82787. + * channel remains assigned.
  82788. + */
  82789. + if (qh->channel)
  82790. + release_channel_ddma(hcd, qh);
  82791. +
  82792. + if ((qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)
  82793. + && (microframe_schedule || !hcd->periodic_channels) && hcd->frame_list) {
  82794. +
  82795. + per_sched_disable(hcd);
  82796. + frame_list_free(hcd);
  82797. + }
  82798. +}
  82799. +
  82800. +static uint8_t frame_to_desc_idx(dwc_otg_qh_t * qh, uint16_t frame_idx)
  82801. +{
  82802. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  82803. + /*
  82804. + * Descriptor set(8 descriptors) index
  82805. + * which is 8-aligned.
  82806. + */
  82807. + return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
  82808. + } else {
  82809. + return (frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1));
  82810. + }
  82811. +}
  82812. +
  82813. +/*
  82814. + * Determine starting frame for Isochronous transfer.
  82815. + * Few frames skipped to prevent race condition with HC.
  82816. + */
  82817. +static uint8_t calc_starting_frame(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  82818. + uint8_t * skip_frames)
  82819. +{
  82820. + uint16_t frame = 0;
  82821. + hcd->frame_number = dwc_otg_hcd_get_frame_number(hcd);
  82822. +
  82823. + /* sched_frame is always frame number(not uFrame) both in FS and HS !! */
  82824. +
  82825. + /*
  82826. + * skip_frames is used to limit activated descriptors number
  82827. + * to avoid the situation when HC services the last activated
  82828. + * descriptor firstly.
  82829. + * Example for FS:
  82830. + * Current frame is 1, scheduled frame is 3. Since HC always fetches the descriptor
  82831. + * corresponding to curr_frame+1, the descriptor corresponding to frame 2
  82832. + * will be fetched. If the number of descriptors is max=64 (or greather) the
  82833. + * list will be fully programmed with Active descriptors and it is possible
  82834. + * case(rare) that the latest descriptor(considering rollback) corresponding
  82835. + * to frame 2 will be serviced first. HS case is more probable because, in fact,
  82836. + * up to 11 uframes(16 in the code) may be skipped.
  82837. + */
  82838. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  82839. + /*
  82840. + * Consider uframe counter also, to start xfer asap.
  82841. + * If half of the frame elapsed skip 2 frames otherwise
  82842. + * just 1 frame.
  82843. + * Starting descriptor index must be 8-aligned, so
  82844. + * if the current frame is near to complete the next one
  82845. + * is skipped as well.
  82846. + */
  82847. +
  82848. + if (dwc_micro_frame_num(hcd->frame_number) >= 5) {
  82849. + *skip_frames = 2 * 8;
  82850. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  82851. + } else {
  82852. + *skip_frames = 1 * 8;
  82853. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  82854. + }
  82855. +
  82856. + frame = dwc_full_frame_num(frame);
  82857. + } else {
  82858. + /*
  82859. + * Two frames are skipped for FS - the current and the next.
  82860. + * But for descriptor programming, 1 frame(descriptor) is enough,
  82861. + * see example above.
  82862. + */
  82863. + *skip_frames = 1;
  82864. + frame = dwc_frame_num_inc(hcd->frame_number, 2);
  82865. + }
  82866. +
  82867. + return frame;
  82868. +}
  82869. +
  82870. +/*
  82871. + * Calculate initial descriptor index for isochronous transfer
  82872. + * based on scheduled frame.
  82873. + */
  82874. +static uint8_t recalc_initial_desc_idx(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  82875. +{
  82876. + uint16_t frame = 0, fr_idx, fr_idx_tmp;
  82877. + uint8_t skip_frames = 0;
  82878. + /*
  82879. + * With current ISOC processing algorithm the channel is being
  82880. + * released when no more QTDs in the list(qh->ntd == 0).
  82881. + * Thus this function is called only when qh->ntd == 0 and qh->channel == 0.
  82882. + *
  82883. + * So qh->channel != NULL branch is not used and just not removed from the
  82884. + * source file. It is required for another possible approach which is,
  82885. + * do not disable and release the channel when ISOC session completed,
  82886. + * just move QH to inactive schedule until new QTD arrives.
  82887. + * On new QTD, the QH moved back to 'ready' schedule,
  82888. + * starting frame and therefore starting desc_index are recalculated.
  82889. + * In this case channel is released only on ep_disable.
  82890. + */
  82891. +
  82892. + /* Calculate starting descriptor index. For INTERRUPT endpoint it is always 0. */
  82893. + if (qh->channel) {
  82894. + frame = calc_starting_frame(hcd, qh, &skip_frames);
  82895. + /*
  82896. + * Calculate initial descriptor index based on FrameList current bitmap
  82897. + * and servicing period.
  82898. + */
  82899. + fr_idx_tmp = frame_list_idx(frame);
  82900. + fr_idx =
  82901. + (MAX_FRLIST_EN_NUM + frame_list_idx(qh->sched_frame) -
  82902. + fr_idx_tmp)
  82903. + % frame_incr_val(qh);
  82904. + fr_idx = (fr_idx + fr_idx_tmp) % MAX_FRLIST_EN_NUM;
  82905. + } else {
  82906. + qh->sched_frame = calc_starting_frame(hcd, qh, &skip_frames);
  82907. + fr_idx = frame_list_idx(qh->sched_frame);
  82908. + }
  82909. +
  82910. + qh->td_first = qh->td_last = frame_to_desc_idx(qh, fr_idx);
  82911. +
  82912. + return skip_frames;
  82913. +}
  82914. +
  82915. +#define ISOC_URB_GIVEBACK_ASAP
  82916. +
  82917. +#define MAX_ISOC_XFER_SIZE_FS 1023
  82918. +#define MAX_ISOC_XFER_SIZE_HS 3072
  82919. +#define DESCNUM_THRESHOLD 4
  82920. +
  82921. +static void init_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  82922. + uint8_t skip_frames)
  82923. +{
  82924. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  82925. + dwc_otg_qtd_t *qtd;
  82926. + dwc_otg_host_dma_desc_t *dma_desc;
  82927. + uint16_t idx, inc, n_desc, ntd_max, max_xfer_size;
  82928. +
  82929. + idx = qh->td_last;
  82930. + inc = qh->interval;
  82931. + n_desc = 0;
  82932. +
  82933. + ntd_max = (max_desc_num(qh) + qh->interval - 1) / qh->interval;
  82934. + if (skip_frames && !qh->channel)
  82935. + ntd_max = ntd_max - skip_frames / qh->interval;
  82936. +
  82937. + max_xfer_size =
  82938. + (qh->dev_speed ==
  82939. + DWC_OTG_EP_SPEED_HIGH) ? MAX_ISOC_XFER_SIZE_HS :
  82940. + MAX_ISOC_XFER_SIZE_FS;
  82941. +
  82942. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  82943. + while ((qh->ntd < ntd_max)
  82944. + && (qtd->isoc_frame_index_last <
  82945. + qtd->urb->packet_count)) {
  82946. +
  82947. + dma_desc = &qh->desc_list[idx];
  82948. + dwc_memset(dma_desc, 0x00, sizeof(dwc_otg_host_dma_desc_t));
  82949. +
  82950. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
  82951. +
  82952. + if (frame_desc->length > max_xfer_size)
  82953. + qh->n_bytes[idx] = max_xfer_size;
  82954. + else
  82955. + qh->n_bytes[idx] = frame_desc->length;
  82956. + dma_desc->status.b_isoc.n_bytes = qh->n_bytes[idx];
  82957. + dma_desc->status.b_isoc.a = 1;
  82958. + dma_desc->status.b_isoc.sts = 0;
  82959. +
  82960. + dma_desc->buf = qtd->urb->dma + frame_desc->offset;
  82961. +
  82962. + qh->ntd++;
  82963. +
  82964. + qtd->isoc_frame_index_last++;
  82965. +
  82966. +#ifdef ISOC_URB_GIVEBACK_ASAP
  82967. + /*
  82968. + * Set IOC for each descriptor corresponding to the
  82969. + * last frame of the URB.
  82970. + */
  82971. + if (qtd->isoc_frame_index_last ==
  82972. + qtd->urb->packet_count)
  82973. + dma_desc->status.b_isoc.ioc = 1;
  82974. +
  82975. +#endif
  82976. + idx = desclist_idx_inc(idx, inc, qh->dev_speed);
  82977. + n_desc++;
  82978. +
  82979. + }
  82980. + qtd->in_process = 1;
  82981. + }
  82982. +
  82983. + qh->td_last = idx;
  82984. +
  82985. +#ifdef ISOC_URB_GIVEBACK_ASAP
  82986. + /* Set IOC for the last descriptor if descriptor list is full */
  82987. + if (qh->ntd == ntd_max) {
  82988. + idx = desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  82989. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  82990. + }
  82991. +#else
  82992. + /*
  82993. + * Set IOC bit only for one descriptor.
  82994. + * Always try to be ahead of HW processing,
  82995. + * i.e. on IOC generation driver activates next descriptors but
  82996. + * core continues to process descriptors followed the one with IOC set.
  82997. + */
  82998. +
  82999. + if (n_desc > DESCNUM_THRESHOLD) {
  83000. + /*
  83001. + * Move IOC "up". Required even if there is only one QTD
  83002. + * in the list, cause QTDs migth continue to be queued,
  83003. + * but during the activation it was only one queued.
  83004. + * Actually more than one QTD might be in the list if this function called
  83005. + * from XferCompletion - QTDs was queued during HW processing of the previous
  83006. + * descriptor chunk.
  83007. + */
  83008. + idx = dwc_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2), qh->dev_speed);
  83009. + } else {
  83010. + /*
  83011. + * Set the IOC for the latest descriptor
  83012. + * if either number of descriptor is not greather than threshold
  83013. + * or no more new descriptors activated.
  83014. + */
  83015. + idx = dwc_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  83016. + }
  83017. +
  83018. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  83019. +#endif
  83020. +}
  83021. +
  83022. +static void init_non_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  83023. +{
  83024. +
  83025. + dwc_hc_t *hc;
  83026. + dwc_otg_host_dma_desc_t *dma_desc;
  83027. + dwc_otg_qtd_t *qtd;
  83028. + int num_packets, len, n_desc = 0;
  83029. +
  83030. + hc = qh->channel;
  83031. +
  83032. + /*
  83033. + * Start with hc->xfer_buff initialized in
  83034. + * assign_and_init_hc(), then if SG transfer consists of multiple URBs,
  83035. + * this pointer re-assigned to the buffer of the currently processed QTD.
  83036. + * For non-SG request there is always one QTD active.
  83037. + */
  83038. +
  83039. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  83040. +
  83041. + if (n_desc) {
  83042. + /* SG request - more than 1 QTDs */
  83043. + hc->xfer_buff = (uint8_t *)qtd->urb->dma + qtd->urb->actual_length;
  83044. + hc->xfer_len = qtd->urb->length - qtd->urb->actual_length;
  83045. + }
  83046. +
  83047. + qtd->n_desc = 0;
  83048. +
  83049. + do {
  83050. + dma_desc = &qh->desc_list[n_desc];
  83051. + len = hc->xfer_len;
  83052. +
  83053. + if (len > MAX_DMA_DESC_SIZE)
  83054. + len = MAX_DMA_DESC_SIZE - hc->max_packet + 1;
  83055. +
  83056. + if (hc->ep_is_in) {
  83057. + if (len > 0) {
  83058. + num_packets = (len + hc->max_packet - 1) / hc->max_packet;
  83059. + } else {
  83060. + /* Need 1 packet for transfer length of 0. */
  83061. + num_packets = 1;
  83062. + }
  83063. + /* Always program an integral # of max packets for IN transfers. */
  83064. + len = num_packets * hc->max_packet;
  83065. + }
  83066. +
  83067. + dma_desc->status.b.n_bytes = len;
  83068. +
  83069. + qh->n_bytes[n_desc] = len;
  83070. +
  83071. + if ((qh->ep_type == UE_CONTROL)
  83072. + && (qtd->control_phase == DWC_OTG_CONTROL_SETUP))
  83073. + dma_desc->status.b.sup = 1; /* Setup Packet */
  83074. +
  83075. + dma_desc->status.b.a = 1; /* Active descriptor */
  83076. + dma_desc->status.b.sts = 0;
  83077. +
  83078. + dma_desc->buf =
  83079. + ((unsigned long)hc->xfer_buff & 0xffffffff);
  83080. +
  83081. + /*
  83082. + * Last descriptor(or single) of IN transfer
  83083. + * with actual size less than MaxPacket.
  83084. + */
  83085. + if (len > hc->xfer_len) {
  83086. + hc->xfer_len = 0;
  83087. + } else {
  83088. + hc->xfer_buff += len;
  83089. + hc->xfer_len -= len;
  83090. + }
  83091. +
  83092. + qtd->n_desc++;
  83093. + n_desc++;
  83094. + }
  83095. + while ((hc->xfer_len > 0) && (n_desc != MAX_DMA_DESC_NUM_GENERIC));
  83096. +
  83097. +
  83098. + qtd->in_process = 1;
  83099. +
  83100. + if (qh->ep_type == UE_CONTROL)
  83101. + break;
  83102. +
  83103. + if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
  83104. + break;
  83105. + }
  83106. +
  83107. + if (n_desc) {
  83108. + /* Request Transfer Complete interrupt for the last descriptor */
  83109. + qh->desc_list[n_desc - 1].status.b.ioc = 1;
  83110. + /* End of List indicator */
  83111. + qh->desc_list[n_desc - 1].status.b.eol = 1;
  83112. +
  83113. + hc->ntd = n_desc;
  83114. + }
  83115. +}
  83116. +
  83117. +/**
  83118. + * For Control and Bulk endpoints initializes descriptor list
  83119. + * and starts the transfer.
  83120. + *
  83121. + * For Interrupt and Isochronous endpoints initializes descriptor list
  83122. + * then updates FrameList, marking appropriate entries as active.
  83123. + * In case of Isochronous, the starting descriptor index is calculated based
  83124. + * on the scheduled frame, but only on the first transfer descriptor within a session.
  83125. + * Then starts the transfer via enabling the channel.
  83126. + * For Isochronous endpoint the channel is not halted on XferComplete
  83127. + * interrupt so remains assigned to the endpoint(QH) until session is done.
  83128. + *
  83129. + * @param hcd The HCD state structure for the DWC OTG controller.
  83130. + * @param qh The QH to init.
  83131. + *
  83132. + * @return 0 if successful, negative error code otherwise.
  83133. + */
  83134. +void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  83135. +{
  83136. + /* Channel is already assigned */
  83137. + dwc_hc_t *hc = qh->channel;
  83138. + uint8_t skip_frames = 0;
  83139. +
  83140. + switch (hc->ep_type) {
  83141. + case DWC_OTG_EP_TYPE_CONTROL:
  83142. + case DWC_OTG_EP_TYPE_BULK:
  83143. + init_non_isoc_dma_desc(hcd, qh);
  83144. +
  83145. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  83146. + break;
  83147. + case DWC_OTG_EP_TYPE_INTR:
  83148. + init_non_isoc_dma_desc(hcd, qh);
  83149. +
  83150. + update_frame_list(hcd, qh, 1);
  83151. +
  83152. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  83153. + break;
  83154. + case DWC_OTG_EP_TYPE_ISOC:
  83155. +
  83156. + if (!qh->ntd)
  83157. + skip_frames = recalc_initial_desc_idx(hcd, qh);
  83158. +
  83159. + init_isoc_dma_desc(hcd, qh, skip_frames);
  83160. +
  83161. + if (!hc->xfer_started) {
  83162. +
  83163. + update_frame_list(hcd, qh, 1);
  83164. +
  83165. + /*
  83166. + * Always set to max, instead of actual size.
  83167. + * Otherwise ntd will be changed with
  83168. + * channel being enabled. Not recommended.
  83169. + *
  83170. + */
  83171. + hc->ntd = max_desc_num(qh);
  83172. + /* Enable channel only once for ISOC */
  83173. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  83174. + }
  83175. +
  83176. + break;
  83177. + default:
  83178. +
  83179. + break;
  83180. + }
  83181. +}
  83182. +
  83183. +static void complete_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  83184. + dwc_hc_t * hc,
  83185. + dwc_otg_hc_regs_t * hc_regs,
  83186. + dwc_otg_halt_status_e halt_status)
  83187. +{
  83188. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  83189. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  83190. + dwc_otg_qh_t *qh;
  83191. + dwc_otg_host_dma_desc_t *dma_desc;
  83192. + uint16_t idx, remain;
  83193. + uint8_t urb_compl;
  83194. +
  83195. + qh = hc->qh;
  83196. + idx = qh->td_first;
  83197. +
  83198. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  83199. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry)
  83200. + qtd->in_process = 0;
  83201. + return;
  83202. + } else if ((halt_status == DWC_OTG_HC_XFER_AHB_ERR) ||
  83203. + (halt_status == DWC_OTG_HC_XFER_BABBLE_ERR)) {
  83204. + /*
  83205. + * Channel is halted in these error cases.
  83206. + * Considered as serious issues.
  83207. + * Complete all URBs marking all frames as failed,
  83208. + * irrespective whether some of the descriptors(frames) succeeded or no.
  83209. + * Pass error code to completion routine as well, to
  83210. + * update urb->status, some of class drivers might use it to stop
  83211. + * queing transfer requests.
  83212. + */
  83213. + int err = (halt_status == DWC_OTG_HC_XFER_AHB_ERR)
  83214. + ? (-DWC_E_IO)
  83215. + : (-DWC_E_OVERFLOW);
  83216. +
  83217. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  83218. + for (idx = 0; idx < qtd->urb->packet_count; idx++) {
  83219. + frame_desc = &qtd->urb->iso_descs[idx];
  83220. + frame_desc->status = err;
  83221. + }
  83222. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, err);
  83223. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  83224. + }
  83225. + return;
  83226. + }
  83227. +
  83228. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  83229. +
  83230. + if (!qtd->in_process)
  83231. + break;
  83232. +
  83233. + urb_compl = 0;
  83234. +
  83235. + do {
  83236. +
  83237. + dma_desc = &qh->desc_list[idx];
  83238. +
  83239. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  83240. + remain = hc->ep_is_in ? dma_desc->status.b_isoc.n_bytes : 0;
  83241. +
  83242. + if (dma_desc->status.b_isoc.sts == DMA_DESC_STS_PKTERR) {
  83243. + /*
  83244. + * XactError or, unable to complete all the transactions
  83245. + * in the scheduled micro-frame/frame,
  83246. + * both indicated by DMA_DESC_STS_PKTERR.
  83247. + */
  83248. + qtd->urb->error_count++;
  83249. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  83250. + frame_desc->status = -DWC_E_PROTOCOL;
  83251. + } else {
  83252. + /* Success */
  83253. +
  83254. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  83255. + frame_desc->status = 0;
  83256. + }
  83257. +
  83258. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  83259. + /*
  83260. + * urb->status is not used for isoc transfers here.
  83261. + * The individual frame_desc status are used instead.
  83262. + */
  83263. +
  83264. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  83265. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  83266. +
  83267. + /*
  83268. + * This check is necessary because urb_dequeue can be called
  83269. + * from urb complete callback(sound driver example).
  83270. + * All pending URBs are dequeued there, so no need for
  83271. + * further processing.
  83272. + */
  83273. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  83274. + return;
  83275. + }
  83276. +
  83277. + urb_compl = 1;
  83278. +
  83279. + }
  83280. +
  83281. + qh->ntd--;
  83282. +
  83283. + /* Stop if IOC requested descriptor reached */
  83284. + if (dma_desc->status.b_isoc.ioc) {
  83285. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  83286. + goto stop_scan;
  83287. + }
  83288. +
  83289. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  83290. +
  83291. + if (urb_compl)
  83292. + break;
  83293. + }
  83294. + while (idx != qh->td_first);
  83295. + }
  83296. +stop_scan:
  83297. + qh->td_first = idx;
  83298. +}
  83299. +
  83300. +uint8_t update_non_isoc_urb_state_ddma(dwc_otg_hcd_t * hcd,
  83301. + dwc_hc_t * hc,
  83302. + dwc_otg_qtd_t * qtd,
  83303. + dwc_otg_host_dma_desc_t * dma_desc,
  83304. + dwc_otg_halt_status_e halt_status,
  83305. + uint32_t n_bytes, uint8_t * xfer_done)
  83306. +{
  83307. +
  83308. + uint16_t remain = hc->ep_is_in ? dma_desc->status.b.n_bytes : 0;
  83309. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  83310. +
  83311. + if (halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  83312. + urb->status = -DWC_E_IO;
  83313. + return 1;
  83314. + }
  83315. + if (dma_desc->status.b.sts == DMA_DESC_STS_PKTERR) {
  83316. + switch (halt_status) {
  83317. + case DWC_OTG_HC_XFER_STALL:
  83318. + urb->status = -DWC_E_PIPE;
  83319. + break;
  83320. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  83321. + urb->status = -DWC_E_OVERFLOW;
  83322. + break;
  83323. + case DWC_OTG_HC_XFER_XACT_ERR:
  83324. + urb->status = -DWC_E_PROTOCOL;
  83325. + break;
  83326. + default:
  83327. + DWC_ERROR("%s: Unhandled descriptor error status (%d)\n", __func__,
  83328. + halt_status);
  83329. + break;
  83330. + }
  83331. + return 1;
  83332. + }
  83333. +
  83334. + if (dma_desc->status.b.a == 1) {
  83335. + DWC_DEBUGPL(DBG_HCDV,
  83336. + "Active descriptor encountered on channel %d\n",
  83337. + hc->hc_num);
  83338. + return 0;
  83339. + }
  83340. +
  83341. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL) {
  83342. + if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  83343. + urb->actual_length += n_bytes - remain;
  83344. + if (remain || urb->actual_length == urb->length) {
  83345. + /*
  83346. + * For Control Data stage do not set urb->status=0 to prevent
  83347. + * URB callback. Set it when Status phase done. See below.
  83348. + */
  83349. + *xfer_done = 1;
  83350. + }
  83351. +
  83352. + } else if (qtd->control_phase == DWC_OTG_CONTROL_STATUS) {
  83353. + urb->status = 0;
  83354. + *xfer_done = 1;
  83355. + }
  83356. + /* No handling for SETUP stage */
  83357. + } else {
  83358. + /* BULK and INTR */
  83359. + urb->actual_length += n_bytes - remain;
  83360. + if (remain || urb->actual_length == urb->length) {
  83361. + urb->status = 0;
  83362. + *xfer_done = 1;
  83363. + }
  83364. + }
  83365. +
  83366. + return 0;
  83367. +}
  83368. +
  83369. +static void complete_non_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  83370. + dwc_hc_t * hc,
  83371. + dwc_otg_hc_regs_t * hc_regs,
  83372. + dwc_otg_halt_status_e halt_status)
  83373. +{
  83374. + dwc_otg_hcd_urb_t *urb = NULL;
  83375. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  83376. + dwc_otg_qh_t *qh;
  83377. + dwc_otg_host_dma_desc_t *dma_desc;
  83378. + uint32_t n_bytes, n_desc, i;
  83379. + uint8_t failed = 0, xfer_done;
  83380. +
  83381. + n_desc = 0;
  83382. +
  83383. + qh = hc->qh;
  83384. +
  83385. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  83386. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  83387. + qtd->in_process = 0;
  83388. + }
  83389. + return;
  83390. + }
  83391. +
  83392. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  83393. +
  83394. + urb = qtd->urb;
  83395. +
  83396. + n_bytes = 0;
  83397. + xfer_done = 0;
  83398. +
  83399. + for (i = 0; i < qtd->n_desc; i++) {
  83400. + dma_desc = &qh->desc_list[n_desc];
  83401. +
  83402. + n_bytes = qh->n_bytes[n_desc];
  83403. +
  83404. + failed =
  83405. + update_non_isoc_urb_state_ddma(hcd, hc, qtd,
  83406. + dma_desc,
  83407. + halt_status, n_bytes,
  83408. + &xfer_done);
  83409. +
  83410. + if (failed
  83411. + || (xfer_done
  83412. + && (urb->status != -DWC_E_IN_PROGRESS))) {
  83413. +
  83414. + hcd->fops->complete(hcd, urb->priv, urb,
  83415. + urb->status);
  83416. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  83417. +
  83418. + if (failed)
  83419. + goto stop_scan;
  83420. + } else if (qh->ep_type == UE_CONTROL) {
  83421. + if (qtd->control_phase == DWC_OTG_CONTROL_SETUP) {
  83422. + if (urb->length > 0) {
  83423. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  83424. + } else {
  83425. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  83426. + }
  83427. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction done\n");
  83428. + } else if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  83429. + if (xfer_done) {
  83430. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  83431. + DWC_DEBUGPL(DBG_HCDV, " Control data transfer done\n");
  83432. + } else if (i + 1 == qtd->n_desc) {
  83433. + /*
  83434. + * Last descriptor for Control data stage which is
  83435. + * not completed yet.
  83436. + */
  83437. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  83438. + }
  83439. + }
  83440. + }
  83441. +
  83442. + n_desc++;
  83443. + }
  83444. +
  83445. + }
  83446. +
  83447. +stop_scan:
  83448. +
  83449. + if (qh->ep_type != UE_CONTROL) {
  83450. + /*
  83451. + * Resetting the data toggle for bulk
  83452. + * and interrupt endpoints in case of stall. See handle_hc_stall_intr()
  83453. + */
  83454. + if (halt_status == DWC_OTG_HC_XFER_STALL)
  83455. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  83456. + else
  83457. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  83458. + }
  83459. +
  83460. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  83461. + hcint_data_t hcint;
  83462. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  83463. + if (hcint.b.nyet) {
  83464. + /*
  83465. + * Got a NYET on the last transaction of the transfer. It
  83466. + * means that the endpoint should be in the PING state at the
  83467. + * beginning of the next transfer.
  83468. + */
  83469. + qh->ping_state = 1;
  83470. + clear_hc_int(hc_regs, nyet);
  83471. + }
  83472. +
  83473. + }
  83474. +
  83475. +}
  83476. +
  83477. +/**
  83478. + * This function is called from interrupt handlers.
  83479. + * Scans the descriptor list, updates URB's status and
  83480. + * calls completion routine for the URB if it's done.
  83481. + * Releases the channel to be used by other transfers.
  83482. + * In case of Isochronous endpoint the channel is not halted until
  83483. + * the end of the session, i.e. QTD list is empty.
  83484. + * If periodic channel released the FrameList is updated accordingly.
  83485. + *
  83486. + * Calls transaction selection routines to activate pending transfers.
  83487. + *
  83488. + * @param hcd The HCD state structure for the DWC OTG controller.
  83489. + * @param hc Host channel, the transfer is completed on.
  83490. + * @param hc_regs Host channel registers.
  83491. + * @param halt_status Reason the channel is being halted,
  83492. + * or just XferComplete for isochronous transfer
  83493. + */
  83494. +void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  83495. + dwc_hc_t * hc,
  83496. + dwc_otg_hc_regs_t * hc_regs,
  83497. + dwc_otg_halt_status_e halt_status)
  83498. +{
  83499. + uint8_t continue_isoc_xfer = 0;
  83500. + dwc_otg_transaction_type_e tr_type;
  83501. + dwc_otg_qh_t *qh = hc->qh;
  83502. +
  83503. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  83504. +
  83505. + complete_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  83506. +
  83507. + /* Release the channel if halted or session completed */
  83508. + if (halt_status != DWC_OTG_HC_XFER_COMPLETE ||
  83509. + DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  83510. +
  83511. + /* Halt the channel if session completed */
  83512. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  83513. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  83514. + }
  83515. +
  83516. + release_channel_ddma(hcd, qh);
  83517. + dwc_otg_hcd_qh_remove(hcd, qh);
  83518. + } else {
  83519. + /* Keep in assigned schedule to continue transfer */
  83520. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  83521. + &qh->qh_list_entry);
  83522. + continue_isoc_xfer = 1;
  83523. +
  83524. + }
  83525. + /** @todo Consider the case when period exceeds FrameList size.
  83526. + * Frame Rollover interrupt should be used.
  83527. + */
  83528. + } else {
  83529. + /* Scan descriptor list to complete the URB(s), then release the channel */
  83530. + complete_non_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  83531. +
  83532. + release_channel_ddma(hcd, qh);
  83533. + dwc_otg_hcd_qh_remove(hcd, qh);
  83534. +
  83535. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  83536. + /* Add back to inactive non-periodic schedule on normal completion */
  83537. + dwc_otg_hcd_qh_add(hcd, qh);
  83538. + }
  83539. +
  83540. + }
  83541. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  83542. + if (tr_type != DWC_OTG_TRANSACTION_NONE || continue_isoc_xfer) {
  83543. + if (continue_isoc_xfer) {
  83544. + if (tr_type == DWC_OTG_TRANSACTION_NONE) {
  83545. + tr_type = DWC_OTG_TRANSACTION_PERIODIC;
  83546. + } else if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC) {
  83547. + tr_type = DWC_OTG_TRANSACTION_ALL;
  83548. + }
  83549. + }
  83550. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  83551. + }
  83552. +}
  83553. +
  83554. +#endif /* DWC_DEVICE_ONLY */
  83555. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_hcd.h linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
  83556. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_hcd.h 1969-12-31 18:00:00.000000000 -0600
  83557. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd.h 2014-12-03 19:13:40.220418001 -0600
  83558. @@ -0,0 +1,862 @@
  83559. +/* ==========================================================================
  83560. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.h $
  83561. + * $Revision: #58 $
  83562. + * $Date: 2011/09/15 $
  83563. + * $Change: 1846647 $
  83564. + *
  83565. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  83566. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  83567. + * otherwise expressly agreed to in writing between Synopsys and you.
  83568. + *
  83569. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  83570. + * any End User Software License Agreement or Agreement for Licensed Product
  83571. + * with Synopsys or any supplement thereto. You are permitted to use and
  83572. + * redistribute this Software in source and binary forms, with or without
  83573. + * modification, provided that redistributions of source code must retain this
  83574. + * notice. You may not view, use, disclose, copy or distribute this file or
  83575. + * any information contained herein except pursuant to this license grant from
  83576. + * Synopsys. If you do not agree with this notice, including the disclaimer
  83577. + * below, then you are not authorized to use the Software.
  83578. + *
  83579. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  83580. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  83581. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  83582. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  83583. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  83584. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  83585. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  83586. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  83587. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  83588. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  83589. + * DAMAGE.
  83590. + * ========================================================================== */
  83591. +#ifndef DWC_DEVICE_ONLY
  83592. +#ifndef __DWC_HCD_H__
  83593. +#define __DWC_HCD_H__
  83594. +
  83595. +#include "dwc_otg_os_dep.h"
  83596. +#include "usb.h"
  83597. +#include "dwc_otg_hcd_if.h"
  83598. +#include "dwc_otg_core_if.h"
  83599. +#include "dwc_list.h"
  83600. +#include "dwc_otg_cil.h"
  83601. +#include "dwc_otg_fiq_fsm.h"
  83602. +
  83603. +
  83604. +/**
  83605. + * @file
  83606. + *
  83607. + * This file contains the structures, constants, and interfaces for
  83608. + * the Host Contoller Driver (HCD).
  83609. + *
  83610. + * The Host Controller Driver (HCD) is responsible for translating requests
  83611. + * from the USB Driver into the appropriate actions on the DWC_otg controller.
  83612. + * It isolates the USBD from the specifics of the controller by providing an
  83613. + * API to the USBD.
  83614. + */
  83615. +
  83616. +struct dwc_otg_hcd_pipe_info {
  83617. + uint8_t dev_addr;
  83618. + uint8_t ep_num;
  83619. + uint8_t pipe_type;
  83620. + uint8_t pipe_dir;
  83621. + uint16_t mps;
  83622. +};
  83623. +
  83624. +struct dwc_otg_hcd_iso_packet_desc {
  83625. + uint32_t offset;
  83626. + uint32_t length;
  83627. + uint32_t actual_length;
  83628. + uint32_t status;
  83629. +};
  83630. +
  83631. +struct dwc_otg_qtd;
  83632. +
  83633. +struct dwc_otg_hcd_urb {
  83634. + void *priv;
  83635. + struct dwc_otg_qtd *qtd;
  83636. + void *buf;
  83637. + dwc_dma_t dma;
  83638. + void *setup_packet;
  83639. + dwc_dma_t setup_dma;
  83640. + uint32_t length;
  83641. + uint32_t actual_length;
  83642. + uint32_t status;
  83643. + uint32_t error_count;
  83644. + uint32_t packet_count;
  83645. + uint32_t flags;
  83646. + uint16_t interval;
  83647. + struct dwc_otg_hcd_pipe_info pipe_info;
  83648. + struct dwc_otg_hcd_iso_packet_desc iso_descs[0];
  83649. +};
  83650. +
  83651. +static inline uint8_t dwc_otg_hcd_get_ep_num(struct dwc_otg_hcd_pipe_info *pipe)
  83652. +{
  83653. + return pipe->ep_num;
  83654. +}
  83655. +
  83656. +static inline uint8_t dwc_otg_hcd_get_pipe_type(struct dwc_otg_hcd_pipe_info
  83657. + *pipe)
  83658. +{
  83659. + return pipe->pipe_type;
  83660. +}
  83661. +
  83662. +static inline uint16_t dwc_otg_hcd_get_mps(struct dwc_otg_hcd_pipe_info *pipe)
  83663. +{
  83664. + return pipe->mps;
  83665. +}
  83666. +
  83667. +static inline uint8_t dwc_otg_hcd_get_dev_addr(struct dwc_otg_hcd_pipe_info
  83668. + *pipe)
  83669. +{
  83670. + return pipe->dev_addr;
  83671. +}
  83672. +
  83673. +static inline uint8_t dwc_otg_hcd_is_pipe_isoc(struct dwc_otg_hcd_pipe_info
  83674. + *pipe)
  83675. +{
  83676. + return (pipe->pipe_type == UE_ISOCHRONOUS);
  83677. +}
  83678. +
  83679. +static inline uint8_t dwc_otg_hcd_is_pipe_int(struct dwc_otg_hcd_pipe_info
  83680. + *pipe)
  83681. +{
  83682. + return (pipe->pipe_type == UE_INTERRUPT);
  83683. +}
  83684. +
  83685. +static inline uint8_t dwc_otg_hcd_is_pipe_bulk(struct dwc_otg_hcd_pipe_info
  83686. + *pipe)
  83687. +{
  83688. + return (pipe->pipe_type == UE_BULK);
  83689. +}
  83690. +
  83691. +static inline uint8_t dwc_otg_hcd_is_pipe_control(struct dwc_otg_hcd_pipe_info
  83692. + *pipe)
  83693. +{
  83694. + return (pipe->pipe_type == UE_CONTROL);
  83695. +}
  83696. +
  83697. +static inline uint8_t dwc_otg_hcd_is_pipe_in(struct dwc_otg_hcd_pipe_info *pipe)
  83698. +{
  83699. + return (pipe->pipe_dir == UE_DIR_IN);
  83700. +}
  83701. +
  83702. +static inline uint8_t dwc_otg_hcd_is_pipe_out(struct dwc_otg_hcd_pipe_info
  83703. + *pipe)
  83704. +{
  83705. + return (!dwc_otg_hcd_is_pipe_in(pipe));
  83706. +}
  83707. +
  83708. +static inline void dwc_otg_hcd_fill_pipe(struct dwc_otg_hcd_pipe_info *pipe,
  83709. + uint8_t devaddr, uint8_t ep_num,
  83710. + uint8_t pipe_type, uint8_t pipe_dir,
  83711. + uint16_t mps)
  83712. +{
  83713. + pipe->dev_addr = devaddr;
  83714. + pipe->ep_num = ep_num;
  83715. + pipe->pipe_type = pipe_type;
  83716. + pipe->pipe_dir = pipe_dir;
  83717. + pipe->mps = mps;
  83718. +}
  83719. +
  83720. +/**
  83721. + * Phases for control transfers.
  83722. + */
  83723. +typedef enum dwc_otg_control_phase {
  83724. + DWC_OTG_CONTROL_SETUP,
  83725. + DWC_OTG_CONTROL_DATA,
  83726. + DWC_OTG_CONTROL_STATUS
  83727. +} dwc_otg_control_phase_e;
  83728. +
  83729. +/** Transaction types. */
  83730. +typedef enum dwc_otg_transaction_type {
  83731. + DWC_OTG_TRANSACTION_NONE = 0,
  83732. + DWC_OTG_TRANSACTION_PERIODIC = 1,
  83733. + DWC_OTG_TRANSACTION_NON_PERIODIC = 2,
  83734. + DWC_OTG_TRANSACTION_ALL = DWC_OTG_TRANSACTION_PERIODIC + DWC_OTG_TRANSACTION_NON_PERIODIC
  83735. +} dwc_otg_transaction_type_e;
  83736. +
  83737. +struct dwc_otg_qh;
  83738. +
  83739. +/**
  83740. + * A Queue Transfer Descriptor (QTD) holds the state of a bulk, control,
  83741. + * interrupt, or isochronous transfer. A single QTD is created for each URB
  83742. + * (of one of these types) submitted to the HCD. The transfer associated with
  83743. + * a QTD may require one or multiple transactions.
  83744. + *
  83745. + * A QTD is linked to a Queue Head, which is entered in either the
  83746. + * non-periodic or periodic schedule for execution. When a QTD is chosen for
  83747. + * execution, some or all of its transactions may be executed. After
  83748. + * execution, the state of the QTD is updated. The QTD may be retired if all
  83749. + * its transactions are complete or if an error occurred. Otherwise, it
  83750. + * remains in the schedule so more transactions can be executed later.
  83751. + */
  83752. +typedef struct dwc_otg_qtd {
  83753. + /**
  83754. + * Determines the PID of the next data packet for the data phase of
  83755. + * control transfers. Ignored for other transfer types.<br>
  83756. + * One of the following values:
  83757. + * - DWC_OTG_HC_PID_DATA0
  83758. + * - DWC_OTG_HC_PID_DATA1
  83759. + */
  83760. + uint8_t data_toggle;
  83761. +
  83762. + /** Current phase for control transfers (Setup, Data, or Status). */
  83763. + dwc_otg_control_phase_e control_phase;
  83764. +
  83765. + /** Keep track of the current split type
  83766. + * for FS/LS endpoints on a HS Hub */
  83767. + uint8_t complete_split;
  83768. +
  83769. + /** How many bytes transferred during SSPLIT OUT */
  83770. + uint32_t ssplit_out_xfer_count;
  83771. +
  83772. + /**
  83773. + * Holds the number of bus errors that have occurred for a transaction
  83774. + * within this transfer.
  83775. + */
  83776. + uint8_t error_count;
  83777. +
  83778. + /**
  83779. + * Index of the next frame descriptor for an isochronous transfer. A
  83780. + * frame descriptor describes the buffer position and length of the
  83781. + * data to be transferred in the next scheduled (micro)frame of an
  83782. + * isochronous transfer. It also holds status for that transaction.
  83783. + * The frame index starts at 0.
  83784. + */
  83785. + uint16_t isoc_frame_index;
  83786. +
  83787. + /** Position of the ISOC split on full/low speed */
  83788. + uint8_t isoc_split_pos;
  83789. +
  83790. + /** Position of the ISOC split in the buffer for the current frame */
  83791. + uint16_t isoc_split_offset;
  83792. +
  83793. + /** URB for this transfer */
  83794. + struct dwc_otg_hcd_urb *urb;
  83795. +
  83796. + struct dwc_otg_qh *qh;
  83797. +
  83798. + /** This list of QTDs */
  83799. + DWC_CIRCLEQ_ENTRY(dwc_otg_qtd) qtd_list_entry;
  83800. +
  83801. + /** Indicates if this QTD is currently processed by HW. */
  83802. + uint8_t in_process;
  83803. +
  83804. + /** Number of DMA descriptors for this QTD */
  83805. + uint8_t n_desc;
  83806. +
  83807. + /**
  83808. + * Last activated frame(packet) index.
  83809. + * Used in Descriptor DMA mode only.
  83810. + */
  83811. + uint16_t isoc_frame_index_last;
  83812. +
  83813. +} dwc_otg_qtd_t;
  83814. +
  83815. +DWC_CIRCLEQ_HEAD(dwc_otg_qtd_list, dwc_otg_qtd);
  83816. +
  83817. +/**
  83818. + * A Queue Head (QH) holds the static characteristics of an endpoint and
  83819. + * maintains a list of transfers (QTDs) for that endpoint. A QH structure may
  83820. + * be entered in either the non-periodic or periodic schedule.
  83821. + */
  83822. +typedef struct dwc_otg_qh {
  83823. + /**
  83824. + * Endpoint type.
  83825. + * One of the following values:
  83826. + * - UE_CONTROL
  83827. + * - UE_BULK
  83828. + * - UE_INTERRUPT
  83829. + * - UE_ISOCHRONOUS
  83830. + */
  83831. + uint8_t ep_type;
  83832. + uint8_t ep_is_in;
  83833. +
  83834. + /** wMaxPacketSize Field of Endpoint Descriptor. */
  83835. + uint16_t maxp;
  83836. +
  83837. + /**
  83838. + * Device speed.
  83839. + * One of the following values:
  83840. + * - DWC_OTG_EP_SPEED_LOW
  83841. + * - DWC_OTG_EP_SPEED_FULL
  83842. + * - DWC_OTG_EP_SPEED_HIGH
  83843. + */
  83844. + uint8_t dev_speed;
  83845. +
  83846. + /**
  83847. + * Determines the PID of the next data packet for non-control
  83848. + * transfers. Ignored for control transfers.<br>
  83849. + * One of the following values:
  83850. + * - DWC_OTG_HC_PID_DATA0
  83851. + * - DWC_OTG_HC_PID_DATA1
  83852. + */
  83853. + uint8_t data_toggle;
  83854. +
  83855. + /** Ping state if 1. */
  83856. + uint8_t ping_state;
  83857. +
  83858. + /**
  83859. + * List of QTDs for this QH.
  83860. + */
  83861. + struct dwc_otg_qtd_list qtd_list;
  83862. +
  83863. + /** Host channel currently processing transfers for this QH. */
  83864. + struct dwc_hc *channel;
  83865. +
  83866. + /** Full/low speed endpoint on high-speed hub requires split. */
  83867. + uint8_t do_split;
  83868. +
  83869. + /** @name Periodic schedule information */
  83870. + /** @{ */
  83871. +
  83872. + /** Bandwidth in microseconds per (micro)frame. */
  83873. + uint16_t usecs;
  83874. +
  83875. + /** Interval between transfers in (micro)frames. */
  83876. + uint16_t interval;
  83877. +
  83878. + /**
  83879. + * (micro)frame to initialize a periodic transfer. The transfer
  83880. + * executes in the following (micro)frame.
  83881. + */
  83882. + uint16_t sched_frame;
  83883. +
  83884. + /*
  83885. + ** Frame a NAK was received on this queue head, used to minimise NAK retransmission
  83886. + */
  83887. + uint16_t nak_frame;
  83888. +
  83889. + /** (micro)frame at which last start split was initialized. */
  83890. + uint16_t start_split_frame;
  83891. +
  83892. + /** @} */
  83893. +
  83894. + /**
  83895. + * Used instead of original buffer if
  83896. + * it(physical address) is not dword-aligned.
  83897. + */
  83898. + uint8_t *dw_align_buf;
  83899. + dwc_dma_t dw_align_buf_dma;
  83900. +
  83901. + /** Entry for QH in either the periodic or non-periodic schedule. */
  83902. + dwc_list_link_t qh_list_entry;
  83903. +
  83904. + /** @name Descriptor DMA support */
  83905. + /** @{ */
  83906. +
  83907. + /** Descriptor List. */
  83908. + dwc_otg_host_dma_desc_t *desc_list;
  83909. +
  83910. + /** Descriptor List physical address. */
  83911. + dwc_dma_t desc_list_dma;
  83912. +
  83913. + /**
  83914. + * Xfer Bytes array.
  83915. + * Each element corresponds to a descriptor and indicates
  83916. + * original XferSize size value for the descriptor.
  83917. + */
  83918. + uint32_t *n_bytes;
  83919. +
  83920. + /** Actual number of transfer descriptors in a list. */
  83921. + uint16_t ntd;
  83922. +
  83923. + /** First activated isochronous transfer descriptor index. */
  83924. + uint8_t td_first;
  83925. + /** Last activated isochronous transfer descriptor index. */
  83926. + uint8_t td_last;
  83927. +
  83928. + /** @} */
  83929. +
  83930. +
  83931. + uint16_t speed;
  83932. + uint16_t frame_usecs[8];
  83933. +
  83934. + uint32_t skip_count;
  83935. +} dwc_otg_qh_t;
  83936. +
  83937. +DWC_CIRCLEQ_HEAD(hc_list, dwc_hc);
  83938. +
  83939. +typedef struct urb_tq_entry {
  83940. + struct urb *urb;
  83941. + DWC_TAILQ_ENTRY(urb_tq_entry) urb_tq_entries;
  83942. +} urb_tq_entry_t;
  83943. +
  83944. +DWC_TAILQ_HEAD(urb_list, urb_tq_entry);
  83945. +
  83946. +/**
  83947. + * This structure holds the state of the HCD, including the non-periodic and
  83948. + * periodic schedules.
  83949. + */
  83950. +struct dwc_otg_hcd {
  83951. + /** The DWC otg device pointer */
  83952. + struct dwc_otg_device *otg_dev;
  83953. + /** DWC OTG Core Interface Layer */
  83954. + dwc_otg_core_if_t *core_if;
  83955. +
  83956. + /** Function HCD driver callbacks */
  83957. + struct dwc_otg_hcd_function_ops *fops;
  83958. +
  83959. + /** Internal DWC HCD Flags */
  83960. + volatile union dwc_otg_hcd_internal_flags {
  83961. + uint32_t d32;
  83962. + struct {
  83963. + unsigned port_connect_status_change:1;
  83964. + unsigned port_connect_status:1;
  83965. + unsigned port_reset_change:1;
  83966. + unsigned port_enable_change:1;
  83967. + unsigned port_suspend_change:1;
  83968. + unsigned port_over_current_change:1;
  83969. + unsigned port_l1_change:1;
  83970. + unsigned reserved:26;
  83971. + } b;
  83972. + } flags;
  83973. +
  83974. + /**
  83975. + * Inactive items in the non-periodic schedule. This is a list of
  83976. + * Queue Heads. Transfers associated with these Queue Heads are not
  83977. + * currently assigned to a host channel.
  83978. + */
  83979. + dwc_list_link_t non_periodic_sched_inactive;
  83980. +
  83981. + /**
  83982. + * Active items in the non-periodic schedule. This is a list of
  83983. + * Queue Heads. Transfers associated with these Queue Heads are
  83984. + * currently assigned to a host channel.
  83985. + */
  83986. + dwc_list_link_t non_periodic_sched_active;
  83987. +
  83988. + /**
  83989. + * Pointer to the next Queue Head to process in the active
  83990. + * non-periodic schedule.
  83991. + */
  83992. + dwc_list_link_t *non_periodic_qh_ptr;
  83993. +
  83994. + /**
  83995. + * Inactive items in the periodic schedule. This is a list of QHs for
  83996. + * periodic transfers that are _not_ scheduled for the next frame.
  83997. + * Each QH in the list has an interval counter that determines when it
  83998. + * needs to be scheduled for execution. This scheduling mechanism
  83999. + * allows only a simple calculation for periodic bandwidth used (i.e.
  84000. + * must assume that all periodic transfers may need to execute in the
  84001. + * same frame). However, it greatly simplifies scheduling and should
  84002. + * be sufficient for the vast majority of OTG hosts, which need to
  84003. + * connect to a small number of peripherals at one time.
  84004. + *
  84005. + * Items move from this list to periodic_sched_ready when the QH
  84006. + * interval counter is 0 at SOF.
  84007. + */
  84008. + dwc_list_link_t periodic_sched_inactive;
  84009. +
  84010. + /**
  84011. + * List of periodic QHs that are ready for execution in the next
  84012. + * frame, but have not yet been assigned to host channels.
  84013. + *
  84014. + * Items move from this list to periodic_sched_assigned as host
  84015. + * channels become available during the current frame.
  84016. + */
  84017. + dwc_list_link_t periodic_sched_ready;
  84018. +
  84019. + /**
  84020. + * List of periodic QHs to be executed in the next frame that are
  84021. + * assigned to host channels.
  84022. + *
  84023. + * Items move from this list to periodic_sched_queued as the
  84024. + * transactions for the QH are queued to the DWC_otg controller.
  84025. + */
  84026. + dwc_list_link_t periodic_sched_assigned;
  84027. +
  84028. + /**
  84029. + * List of periodic QHs that have been queued for execution.
  84030. + *
  84031. + * Items move from this list to either periodic_sched_inactive or
  84032. + * periodic_sched_ready when the channel associated with the transfer
  84033. + * is released. If the interval for the QH is 1, the item moves to
  84034. + * periodic_sched_ready because it must be rescheduled for the next
  84035. + * frame. Otherwise, the item moves to periodic_sched_inactive.
  84036. + */
  84037. + dwc_list_link_t periodic_sched_queued;
  84038. +
  84039. + /**
  84040. + * Total bandwidth claimed so far for periodic transfers. This value
  84041. + * is in microseconds per (micro)frame. The assumption is that all
  84042. + * periodic transfers may occur in the same (micro)frame.
  84043. + */
  84044. + uint16_t periodic_usecs;
  84045. +
  84046. + /**
  84047. + * Total bandwidth claimed so far for all periodic transfers
  84048. + * in a frame.
  84049. + * This will include a mixture of HS and FS transfers.
  84050. + * Units are microseconds per (micro)frame.
  84051. + * We have a budget per frame and have to schedule
  84052. + * transactions accordingly.
  84053. + * Watch out for the fact that things are actually scheduled for the
  84054. + * "next frame".
  84055. + */
  84056. + uint16_t frame_usecs[8];
  84057. +
  84058. +
  84059. + /**
  84060. + * Frame number read from the core at SOF. The value ranges from 0 to
  84061. + * DWC_HFNUM_MAX_FRNUM.
  84062. + */
  84063. + uint16_t frame_number;
  84064. +
  84065. + /**
  84066. + * Count of periodic QHs, if using several eps. For SOF enable/disable.
  84067. + */
  84068. + uint16_t periodic_qh_count;
  84069. +
  84070. + /**
  84071. + * Free host channels in the controller. This is a list of
  84072. + * dwc_hc_t items.
  84073. + */
  84074. + struct hc_list free_hc_list;
  84075. + /**
  84076. + * Number of host channels assigned to periodic transfers. Currently
  84077. + * assuming that there is a dedicated host channel for each periodic
  84078. + * transaction and at least one host channel available for
  84079. + * non-periodic transactions.
  84080. + */
  84081. + int periodic_channels; /* microframe_schedule==0 */
  84082. +
  84083. + /**
  84084. + * Number of host channels assigned to non-periodic transfers.
  84085. + */
  84086. + int non_periodic_channels; /* microframe_schedule==0 */
  84087. +
  84088. + /**
  84089. + * Number of host channels assigned to non-periodic transfers.
  84090. + */
  84091. + int available_host_channels;
  84092. +
  84093. + /**
  84094. + * Array of pointers to the host channel descriptors. Allows accessing
  84095. + * a host channel descriptor given the host channel number. This is
  84096. + * useful in interrupt handlers.
  84097. + */
  84098. + struct dwc_hc *hc_ptr_array[MAX_EPS_CHANNELS];
  84099. +
  84100. + /**
  84101. + * Buffer to use for any data received during the status phase of a
  84102. + * control transfer. Normally no data is transferred during the status
  84103. + * phase. This buffer is used as a bit bucket.
  84104. + */
  84105. + uint8_t *status_buf;
  84106. +
  84107. + /**
  84108. + * DMA address for status_buf.
  84109. + */
  84110. + dma_addr_t status_buf_dma;
  84111. +#define DWC_OTG_HCD_STATUS_BUF_SIZE 64
  84112. +
  84113. + /**
  84114. + * Connection timer. An OTG host must display a message if the device
  84115. + * does not connect. Started when the VBus power is turned on via
  84116. + * sysfs attribute "buspower".
  84117. + */
  84118. + dwc_timer_t *conn_timer;
  84119. +
  84120. + /* Tasket to do a reset */
  84121. + dwc_tasklet_t *reset_tasklet;
  84122. +
  84123. + dwc_tasklet_t *completion_tasklet;
  84124. + struct urb_list completed_urb_list;
  84125. +
  84126. + /* */
  84127. + dwc_spinlock_t *lock;
  84128. + dwc_spinlock_t *channel_lock;
  84129. + /**
  84130. + * Private data that could be used by OS wrapper.
  84131. + */
  84132. + void *priv;
  84133. +
  84134. + uint8_t otg_port;
  84135. +
  84136. + /** Frame List */
  84137. + uint32_t *frame_list;
  84138. +
  84139. + /** Hub - Port assignment */
  84140. + int hub_port[128];
  84141. +#ifdef FIQ_DEBUG
  84142. + int hub_port_alloc[2048];
  84143. +#endif
  84144. +
  84145. + /** Frame List DMA address */
  84146. + dma_addr_t frame_list_dma;
  84147. +
  84148. + struct fiq_stack *fiq_stack;
  84149. + struct fiq_state *fiq_state;
  84150. +
  84151. + /** Virtual address for split transaction DMA bounce buffers */
  84152. + struct fiq_dma_blob *fiq_dmab;
  84153. +
  84154. +#ifdef DEBUG
  84155. + uint32_t frrem_samples;
  84156. + uint64_t frrem_accum;
  84157. +
  84158. + uint32_t hfnum_7_samples_a;
  84159. + uint64_t hfnum_7_frrem_accum_a;
  84160. + uint32_t hfnum_0_samples_a;
  84161. + uint64_t hfnum_0_frrem_accum_a;
  84162. + uint32_t hfnum_other_samples_a;
  84163. + uint64_t hfnum_other_frrem_accum_a;
  84164. +
  84165. + uint32_t hfnum_7_samples_b;
  84166. + uint64_t hfnum_7_frrem_accum_b;
  84167. + uint32_t hfnum_0_samples_b;
  84168. + uint64_t hfnum_0_frrem_accum_b;
  84169. + uint32_t hfnum_other_samples_b;
  84170. + uint64_t hfnum_other_frrem_accum_b;
  84171. +#endif
  84172. +};
  84173. +
  84174. +/** @name Transaction Execution Functions */
  84175. +/** @{ */
  84176. +extern dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t
  84177. + * hcd);
  84178. +extern void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  84179. + dwc_otg_transaction_type_e tr_type);
  84180. +
  84181. +int dwc_otg_hcd_allocate_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh);
  84182. +void dwc_otg_hcd_release_port(dwc_otg_hcd_t * dwc_otg_hcd, dwc_otg_qh_t *qh);
  84183. +
  84184. +extern int fiq_fsm_queue_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh);
  84185. +extern int fiq_fsm_transaction_suitable(dwc_otg_qh_t *qh);
  84186. +extern void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num);
  84187. +
  84188. +/** @} */
  84189. +
  84190. +/** @name Interrupt Handler Functions */
  84191. +/** @{ */
  84192. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  84193. +extern int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  84194. +extern int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t *
  84195. + dwc_otg_hcd);
  84196. +extern int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t *
  84197. + dwc_otg_hcd);
  84198. +extern int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t *
  84199. + dwc_otg_hcd);
  84200. +extern int32_t dwc_otg_hcd_handle_incomplete_periodic_intr(dwc_otg_hcd_t *
  84201. + dwc_otg_hcd);
  84202. +extern int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  84203. +extern int32_t dwc_otg_hcd_handle_conn_id_status_change_intr(dwc_otg_hcd_t *
  84204. + dwc_otg_hcd);
  84205. +extern int32_t dwc_otg_hcd_handle_disconnect_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  84206. +extern int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  84207. +extern int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd,
  84208. + uint32_t num);
  84209. +extern int32_t dwc_otg_hcd_handle_session_req_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  84210. +extern int32_t dwc_otg_hcd_handle_wakeup_detected_intr(dwc_otg_hcd_t *
  84211. + dwc_otg_hcd);
  84212. +/** @} */
  84213. +
  84214. +/** @name Schedule Queue Functions */
  84215. +/** @{ */
  84216. +
  84217. +/* Implemented in dwc_otg_hcd_queue.c */
  84218. +extern dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  84219. + dwc_otg_hcd_urb_t * urb, int atomic_alloc);
  84220. +extern void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  84221. +extern int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  84222. +extern void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  84223. +extern void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  84224. + int sched_csplit);
  84225. +
  84226. +/** Remove and free a QH */
  84227. +static inline void dwc_otg_hcd_qh_remove_and_free(dwc_otg_hcd_t * hcd,
  84228. + dwc_otg_qh_t * qh)
  84229. +{
  84230. + dwc_irqflags_t flags;
  84231. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  84232. + dwc_otg_hcd_qh_remove(hcd, qh);
  84233. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  84234. + dwc_otg_hcd_qh_free(hcd, qh);
  84235. +}
  84236. +
  84237. +/** Allocates memory for a QH structure.
  84238. + * @return Returns the memory allocate or NULL on error. */
  84239. +static inline dwc_otg_qh_t *dwc_otg_hcd_qh_alloc(int atomic_alloc)
  84240. +{
  84241. + if (atomic_alloc)
  84242. + return (dwc_otg_qh_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qh_t));
  84243. + else
  84244. + return (dwc_otg_qh_t *) DWC_ALLOC(sizeof(dwc_otg_qh_t));
  84245. +}
  84246. +
  84247. +extern dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb,
  84248. + int atomic_alloc);
  84249. +extern void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb);
  84250. +extern int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd, dwc_otg_hcd_t * dwc_otg_hcd,
  84251. + dwc_otg_qh_t ** qh, int atomic_alloc);
  84252. +
  84253. +/** Allocates memory for a QTD structure.
  84254. + * @return Returns the memory allocate or NULL on error. */
  84255. +static inline dwc_otg_qtd_t *dwc_otg_hcd_qtd_alloc(int atomic_alloc)
  84256. +{
  84257. + if (atomic_alloc)
  84258. + return (dwc_otg_qtd_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qtd_t));
  84259. + else
  84260. + return (dwc_otg_qtd_t *) DWC_ALLOC(sizeof(dwc_otg_qtd_t));
  84261. +}
  84262. +
  84263. +/** Frees the memory for a QTD structure. QTD should already be removed from
  84264. + * list.
  84265. + * @param qtd QTD to free.*/
  84266. +static inline void dwc_otg_hcd_qtd_free(dwc_otg_qtd_t * qtd)
  84267. +{
  84268. + DWC_FREE(qtd);
  84269. +}
  84270. +
  84271. +/** Removes a QTD from list.
  84272. + * @param hcd HCD instance.
  84273. + * @param qtd QTD to remove from list.
  84274. + * @param qh QTD belongs to.
  84275. + */
  84276. +static inline void dwc_otg_hcd_qtd_remove(dwc_otg_hcd_t * hcd,
  84277. + dwc_otg_qtd_t * qtd,
  84278. + dwc_otg_qh_t * qh)
  84279. +{
  84280. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  84281. +}
  84282. +
  84283. +/** Remove and free a QTD
  84284. + * Need to disable IRQ and hold hcd lock while calling this function out of
  84285. + * interrupt servicing chain */
  84286. +static inline void dwc_otg_hcd_qtd_remove_and_free(dwc_otg_hcd_t * hcd,
  84287. + dwc_otg_qtd_t * qtd,
  84288. + dwc_otg_qh_t * qh)
  84289. +{
  84290. + dwc_otg_hcd_qtd_remove(hcd, qtd, qh);
  84291. + dwc_otg_hcd_qtd_free(qtd);
  84292. +}
  84293. +
  84294. +/** @} */
  84295. +
  84296. +/** @name Descriptor DMA Supporting Functions */
  84297. +/** @{ */
  84298. +
  84299. +extern void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  84300. +extern void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  84301. + dwc_hc_t * hc,
  84302. + dwc_otg_hc_regs_t * hc_regs,
  84303. + dwc_otg_halt_status_e halt_status);
  84304. +
  84305. +extern int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  84306. +extern void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  84307. +
  84308. +/** @} */
  84309. +
  84310. +/** @name Internal Functions */
  84311. +/** @{ */
  84312. +dwc_otg_qh_t *dwc_urb_to_qh(dwc_otg_hcd_urb_t * urb);
  84313. +/** @} */
  84314. +
  84315. +#ifdef CONFIG_USB_DWC_OTG_LPM
  84316. +extern int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd,
  84317. + uint8_t devaddr);
  84318. +extern void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd);
  84319. +#endif
  84320. +
  84321. +/** Gets the QH that contains the list_head */
  84322. +#define dwc_list_to_qh(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qh_t, qh_list_entry)
  84323. +
  84324. +/** Gets the QTD that contains the list_head */
  84325. +#define dwc_list_to_qtd(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qtd_t, qtd_list_entry)
  84326. +
  84327. +/** Check if QH is non-periodic */
  84328. +#define dwc_qh_is_non_per(_qh_ptr_) ((_qh_ptr_->ep_type == UE_BULK) || \
  84329. + (_qh_ptr_->ep_type == UE_CONTROL))
  84330. +
  84331. +/** High bandwidth multiplier as encoded in highspeed endpoint descriptors */
  84332. +#define dwc_hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
  84333. +
  84334. +/** Packet size for any kind of endpoint descriptor */
  84335. +#define dwc_max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  84336. +
  84337. +/**
  84338. + * Returns true if _frame1 is less than or equal to _frame2. The comparison is
  84339. + * done modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the
  84340. + * frame number when the max frame number is reached.
  84341. + */
  84342. +static inline int dwc_frame_num_le(uint16_t frame1, uint16_t frame2)
  84343. +{
  84344. + return ((frame2 - frame1) & DWC_HFNUM_MAX_FRNUM) <=
  84345. + (DWC_HFNUM_MAX_FRNUM >> 1);
  84346. +}
  84347. +
  84348. +/**
  84349. + * Returns true if _frame1 is greater than _frame2. The comparison is done
  84350. + * modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the frame
  84351. + * number when the max frame number is reached.
  84352. + */
  84353. +static inline int dwc_frame_num_gt(uint16_t frame1, uint16_t frame2)
  84354. +{
  84355. + return (frame1 != frame2) &&
  84356. + (((frame1 - frame2) & DWC_HFNUM_MAX_FRNUM) <
  84357. + (DWC_HFNUM_MAX_FRNUM >> 1));
  84358. +}
  84359. +
  84360. +/**
  84361. + * Increments _frame by the amount specified by _inc. The addition is done
  84362. + * modulo DWC_HFNUM_MAX_FRNUM. Returns the incremented value.
  84363. + */
  84364. +static inline uint16_t dwc_frame_num_inc(uint16_t frame, uint16_t inc)
  84365. +{
  84366. + return (frame + inc) & DWC_HFNUM_MAX_FRNUM;
  84367. +}
  84368. +
  84369. +static inline uint16_t dwc_full_frame_num(uint16_t frame)
  84370. +{
  84371. + return (frame & DWC_HFNUM_MAX_FRNUM) >> 3;
  84372. +}
  84373. +
  84374. +static inline uint16_t dwc_micro_frame_num(uint16_t frame)
  84375. +{
  84376. + return frame & 0x7;
  84377. +}
  84378. +
  84379. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  84380. + dwc_otg_hc_regs_t * hc_regs,
  84381. + dwc_otg_qtd_t * qtd);
  84382. +
  84383. +#ifdef DEBUG
  84384. +/**
  84385. + * Macro to sample the remaining PHY clocks left in the current frame. This
  84386. + * may be used during debugging to determine the average time it takes to
  84387. + * execute sections of code. There are two possible sample points, "a" and
  84388. + * "b", so the _letter argument must be one of these values.
  84389. + *
  84390. + * To dump the average sample times, read the "hcd_frrem" sysfs attribute. For
  84391. + * example, "cat /sys/devices/lm0/hcd_frrem".
  84392. + */
  84393. +#define dwc_sample_frrem(_hcd, _qh, _letter) \
  84394. +{ \
  84395. + hfnum_data_t hfnum; \
  84396. + dwc_otg_qtd_t *qtd; \
  84397. + qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry); \
  84398. + if (usb_pipeint(qtd->urb->pipe) && _qh->start_split_frame != 0 && !qtd->complete_split) { \
  84399. + hfnum.d32 = DWC_READ_REG32(&_hcd->core_if->host_if->host_global_regs->hfnum); \
  84400. + switch (hfnum.b.frnum & 0x7) { \
  84401. + case 7: \
  84402. + _hcd->hfnum_7_samples_##_letter++; \
  84403. + _hcd->hfnum_7_frrem_accum_##_letter += hfnum.b.frrem; \
  84404. + break; \
  84405. + case 0: \
  84406. + _hcd->hfnum_0_samples_##_letter++; \
  84407. + _hcd->hfnum_0_frrem_accum_##_letter += hfnum.b.frrem; \
  84408. + break; \
  84409. + default: \
  84410. + _hcd->hfnum_other_samples_##_letter++; \
  84411. + _hcd->hfnum_other_frrem_accum_##_letter += hfnum.b.frrem; \
  84412. + break; \
  84413. + } \
  84414. + } \
  84415. +}
  84416. +#else
  84417. +#define dwc_sample_frrem(_hcd, _qh, _letter)
  84418. +#endif
  84419. +#endif
  84420. +#endif /* DWC_DEVICE_ONLY */
  84421. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
  84422. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h 1969-12-31 18:00:00.000000000 -0600
  84423. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h 2014-12-03 19:13:40.220418001 -0600
  84424. @@ -0,0 +1,417 @@
  84425. +/* ==========================================================================
  84426. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_if.h $
  84427. + * $Revision: #12 $
  84428. + * $Date: 2011/10/26 $
  84429. + * $Change: 1873028 $
  84430. + *
  84431. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  84432. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  84433. + * otherwise expressly agreed to in writing between Synopsys and you.
  84434. + *
  84435. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  84436. + * any End User Software License Agreement or Agreement for Licensed Product
  84437. + * with Synopsys or any supplement thereto. You are permitted to use and
  84438. + * redistribute this Software in source and binary forms, with or without
  84439. + * modification, provided that redistributions of source code must retain this
  84440. + * notice. You may not view, use, disclose, copy or distribute this file or
  84441. + * any information contained herein except pursuant to this license grant from
  84442. + * Synopsys. If you do not agree with this notice, including the disclaimer
  84443. + * below, then you are not authorized to use the Software.
  84444. + *
  84445. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  84446. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  84447. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  84448. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  84449. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  84450. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  84451. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  84452. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  84453. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  84454. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  84455. + * DAMAGE.
  84456. + * ========================================================================== */
  84457. +#ifndef DWC_DEVICE_ONLY
  84458. +#ifndef __DWC_HCD_IF_H__
  84459. +#define __DWC_HCD_IF_H__
  84460. +
  84461. +#include "dwc_otg_core_if.h"
  84462. +
  84463. +/** @file
  84464. + * This file defines DWC_OTG HCD Core API.
  84465. + */
  84466. +
  84467. +struct dwc_otg_hcd;
  84468. +typedef struct dwc_otg_hcd dwc_otg_hcd_t;
  84469. +
  84470. +struct dwc_otg_hcd_urb;
  84471. +typedef struct dwc_otg_hcd_urb dwc_otg_hcd_urb_t;
  84472. +
  84473. +/** @name HCD Function Driver Callbacks */
  84474. +/** @{ */
  84475. +
  84476. +/** This function is called whenever core switches to host mode. */
  84477. +typedef int (*dwc_otg_hcd_start_cb_t) (dwc_otg_hcd_t * hcd);
  84478. +
  84479. +/** This function is called when device has been disconnected */
  84480. +typedef int (*dwc_otg_hcd_disconnect_cb_t) (dwc_otg_hcd_t * hcd);
  84481. +
  84482. +/** Wrapper provides this function to HCD to core, so it can get hub information to which device is connected */
  84483. +typedef int (*dwc_otg_hcd_hub_info_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  84484. + void *urb_handle,
  84485. + uint32_t * hub_addr,
  84486. + uint32_t * port_addr);
  84487. +/** Via this function HCD core gets device speed */
  84488. +typedef int (*dwc_otg_hcd_speed_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  84489. + void *urb_handle);
  84490. +
  84491. +/** This function is called when urb is completed */
  84492. +typedef int (*dwc_otg_hcd_complete_urb_cb_t) (dwc_otg_hcd_t * hcd,
  84493. + void *urb_handle,
  84494. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  84495. + int32_t status);
  84496. +
  84497. +/** Via this function HCD core gets b_hnp_enable parameter */
  84498. +typedef int (*dwc_otg_hcd_get_b_hnp_enable) (dwc_otg_hcd_t * hcd);
  84499. +
  84500. +struct dwc_otg_hcd_function_ops {
  84501. + dwc_otg_hcd_start_cb_t start;
  84502. + dwc_otg_hcd_disconnect_cb_t disconnect;
  84503. + dwc_otg_hcd_hub_info_from_urb_cb_t hub_info;
  84504. + dwc_otg_hcd_speed_from_urb_cb_t speed;
  84505. + dwc_otg_hcd_complete_urb_cb_t complete;
  84506. + dwc_otg_hcd_get_b_hnp_enable get_b_hnp_enable;
  84507. +};
  84508. +/** @} */
  84509. +
  84510. +/** @name HCD Core API */
  84511. +/** @{ */
  84512. +/** This function allocates dwc_otg_hcd structure and returns pointer on it. */
  84513. +extern dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void);
  84514. +
  84515. +/** This function should be called to initiate HCD Core.
  84516. + *
  84517. + * @param hcd The HCD
  84518. + * @param core_if The DWC_OTG Core
  84519. + *
  84520. + * Returns -DWC_E_NO_MEMORY if no enough memory.
  84521. + * Returns 0 on success
  84522. + */
  84523. +extern int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if);
  84524. +
  84525. +/** Frees HCD
  84526. + *
  84527. + * @param hcd The HCD
  84528. + */
  84529. +extern void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd);
  84530. +
  84531. +/** This function should be called on every hardware interrupt.
  84532. + *
  84533. + * @param dwc_otg_hcd The HCD
  84534. + *
  84535. + * Returns non zero if interrupt is handled
  84536. + * Return 0 if interrupt is not handled
  84537. + */
  84538. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  84539. +
  84540. +/** This function is used to handle the fast interrupt
  84541. + *
  84542. + */
  84543. +extern void __attribute__ ((naked)) dwc_otg_hcd_handle_fiq(void);
  84544. +
  84545. +/**
  84546. + * Returns private data set by
  84547. + * dwc_otg_hcd_set_priv_data function.
  84548. + *
  84549. + * @param hcd The HCD
  84550. + */
  84551. +extern void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd);
  84552. +
  84553. +/**
  84554. + * Set private data.
  84555. + *
  84556. + * @param hcd The HCD
  84557. + * @param priv_data pointer to be stored in private data
  84558. + */
  84559. +extern void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data);
  84560. +
  84561. +/**
  84562. + * This function initializes the HCD Core.
  84563. + *
  84564. + * @param hcd The HCD
  84565. + * @param fops The Function Driver Operations data structure containing pointers to all callbacks.
  84566. + *
  84567. + * Returns -DWC_E_NO_DEVICE if Core is currently is in device mode.
  84568. + * Returns 0 on success
  84569. + */
  84570. +extern int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  84571. + struct dwc_otg_hcd_function_ops *fops);
  84572. +
  84573. +/**
  84574. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  84575. + * stopped.
  84576. + *
  84577. + * @param hcd The HCD
  84578. + */
  84579. +extern void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd);
  84580. +
  84581. +/**
  84582. + * Handles hub class-specific requests.
  84583. + *
  84584. + * @param dwc_otg_hcd The HCD
  84585. + * @param typeReq Request Type
  84586. + * @param wValue wValue from control request
  84587. + * @param wIndex wIndex from control request
  84588. + * @param buf data buffer
  84589. + * @param wLength data buffer length
  84590. + *
  84591. + * Returns -DWC_E_INVALID if invalid argument is passed
  84592. + * Returns 0 on success
  84593. + */
  84594. +extern int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  84595. + uint16_t typeReq, uint16_t wValue,
  84596. + uint16_t wIndex, uint8_t * buf,
  84597. + uint16_t wLength);
  84598. +
  84599. +/**
  84600. + * Returns otg port number.
  84601. + *
  84602. + * @param hcd The HCD
  84603. + */
  84604. +extern uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd);
  84605. +
  84606. +/**
  84607. + * Returns OTG version - either 1.3 or 2.0.
  84608. + *
  84609. + * @param core_if The core_if structure pointer
  84610. + */
  84611. +extern uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if);
  84612. +
  84613. +/**
  84614. + * Returns 1 if currently core is acting as B host, and 0 otherwise.
  84615. + *
  84616. + * @param hcd The HCD
  84617. + */
  84618. +extern uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd);
  84619. +
  84620. +/**
  84621. + * Returns current frame number.
  84622. + *
  84623. + * @param hcd The HCD
  84624. + */
  84625. +extern int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * hcd);
  84626. +
  84627. +/**
  84628. + * Dumps hcd state.
  84629. + *
  84630. + * @param hcd The HCD
  84631. + */
  84632. +extern void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd);
  84633. +
  84634. +/**
  84635. + * Dump the average frame remaining at SOF. This can be used to
  84636. + * determine average interrupt latency. Frame remaining is also shown for
  84637. + * start transfer and two additional sample points.
  84638. + * Currently this function is not implemented.
  84639. + *
  84640. + * @param hcd The HCD
  84641. + */
  84642. +extern void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd);
  84643. +
  84644. +/**
  84645. + * Sends LPM transaction to the local device.
  84646. + *
  84647. + * @param hcd The HCD
  84648. + * @param devaddr Device Address
  84649. + * @param hird Host initiated resume duration
  84650. + * @param bRemoteWake Value of bRemoteWake field in LPM transaction
  84651. + *
  84652. + * Returns negative value if sending LPM transaction was not succeeded.
  84653. + * Returns 0 on success.
  84654. + */
  84655. +extern int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr,
  84656. + uint8_t hird, uint8_t bRemoteWake);
  84657. +
  84658. +/* URB interface */
  84659. +
  84660. +/**
  84661. + * Allocates memory for dwc_otg_hcd_urb structure.
  84662. + * Allocated memory should be freed by call of DWC_FREE.
  84663. + *
  84664. + * @param hcd The HCD
  84665. + * @param iso_desc_count Count of ISOC descriptors
  84666. + * @param atomic_alloc Specefies whether to perform atomic allocation.
  84667. + */
  84668. +extern dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  84669. + int iso_desc_count,
  84670. + int atomic_alloc);
  84671. +
  84672. +/**
  84673. + * Set pipe information in URB.
  84674. + *
  84675. + * @param hcd_urb DWC_OTG URB
  84676. + * @param devaddr Device Address
  84677. + * @param ep_num Endpoint Number
  84678. + * @param ep_type Endpoint Type
  84679. + * @param ep_dir Endpoint Direction
  84680. + * @param mps Max Packet Size
  84681. + */
  84682. +extern void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * hcd_urb,
  84683. + uint8_t devaddr, uint8_t ep_num,
  84684. + uint8_t ep_type, uint8_t ep_dir,
  84685. + uint16_t mps);
  84686. +
  84687. +/* Transfer flags */
  84688. +#define URB_GIVEBACK_ASAP 0x1
  84689. +#define URB_SEND_ZERO_PACKET 0x2
  84690. +
  84691. +/**
  84692. + * Sets dwc_otg_hcd_urb parameters.
  84693. + *
  84694. + * @param urb DWC_OTG URB allocated by dwc_otg_hcd_urb_alloc function.
  84695. + * @param urb_handle Unique handle for request, this will be passed back
  84696. + * to function driver in completion callback.
  84697. + * @param buf The buffer for the data
  84698. + * @param dma The DMA buffer for the data
  84699. + * @param buflen Transfer length
  84700. + * @param sp Buffer for setup data
  84701. + * @param sp_dma DMA address of setup data buffer
  84702. + * @param flags Transfer flags
  84703. + * @param interval Polling interval for interrupt or isochronous transfers.
  84704. + */
  84705. +extern void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * urb,
  84706. + void *urb_handle, void *buf,
  84707. + dwc_dma_t dma, uint32_t buflen, void *sp,
  84708. + dwc_dma_t sp_dma, uint32_t flags,
  84709. + uint16_t interval);
  84710. +
  84711. +/** Gets status from dwc_otg_hcd_urb
  84712. + *
  84713. + * @param dwc_otg_urb DWC_OTG URB
  84714. + */
  84715. +extern uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb);
  84716. +
  84717. +/** Gets actual length from dwc_otg_hcd_urb
  84718. + *
  84719. + * @param dwc_otg_urb DWC_OTG URB
  84720. + */
  84721. +extern uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t *
  84722. + dwc_otg_urb);
  84723. +
  84724. +/** Gets error count from dwc_otg_hcd_urb. Only for ISOC URBs
  84725. + *
  84726. + * @param dwc_otg_urb DWC_OTG URB
  84727. + */
  84728. +extern uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t *
  84729. + dwc_otg_urb);
  84730. +
  84731. +/** Set ISOC descriptor offset and length
  84732. + *
  84733. + * @param dwc_otg_urb DWC_OTG URB
  84734. + * @param desc_num ISOC descriptor number
  84735. + * @param offset Offset from beginig of buffer.
  84736. + * @param length Transaction length
  84737. + */
  84738. +extern void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  84739. + int desc_num, uint32_t offset,
  84740. + uint32_t length);
  84741. +
  84742. +/** Get status of ISOC descriptor, specified by desc_num
  84743. + *
  84744. + * @param dwc_otg_urb DWC_OTG URB
  84745. + * @param desc_num ISOC descriptor number
  84746. + */
  84747. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t *
  84748. + dwc_otg_urb, int desc_num);
  84749. +
  84750. +/** Get actual length of ISOC descriptor, specified by desc_num
  84751. + *
  84752. + * @param dwc_otg_urb DWC_OTG URB
  84753. + * @param desc_num ISOC descriptor number
  84754. + */
  84755. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  84756. + dwc_otg_urb,
  84757. + int desc_num);
  84758. +
  84759. +/** Queue URB. After transfer is completes, the complete callback will be called with the URB status
  84760. + *
  84761. + * @param dwc_otg_hcd The HCD
  84762. + * @param dwc_otg_urb DWC_OTG URB
  84763. + * @param ep_handle Out parameter for returning endpoint handle
  84764. + * @param atomic_alloc Flag to do atomic allocation if needed
  84765. + *
  84766. + * Returns -DWC_E_NO_DEVICE if no device is connected.
  84767. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  84768. + * Returns 0 on success.
  84769. + */
  84770. +extern int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * dwc_otg_hcd,
  84771. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  84772. + void **ep_handle, int atomic_alloc);
  84773. +
  84774. +/** De-queue the specified URB
  84775. + *
  84776. + * @param dwc_otg_hcd The HCD
  84777. + * @param dwc_otg_urb DWC_OTG URB
  84778. + */
  84779. +extern int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * dwc_otg_hcd,
  84780. + dwc_otg_hcd_urb_t * dwc_otg_urb);
  84781. +
  84782. +/** Frees resources in the DWC_otg controller related to a given endpoint.
  84783. + * Any URBs for the endpoint must already be dequeued.
  84784. + *
  84785. + * @param hcd The HCD
  84786. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  84787. + * @param retry Number of retries if there are queued transfers.
  84788. + *
  84789. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  84790. + * Returns 0 on success
  84791. + */
  84792. +extern int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  84793. + int retry);
  84794. +
  84795. +/* Resets the data toggle in qh structure. This function can be called from
  84796. + * usb_clear_halt routine.
  84797. + *
  84798. + * @param hcd The HCD
  84799. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  84800. + *
  84801. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  84802. + * Returns 0 on success
  84803. + */
  84804. +extern int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle);
  84805. +
  84806. +/** Returns 1 if status of specified port is changed and 0 otherwise.
  84807. + *
  84808. + * @param hcd The HCD
  84809. + * @param port Port number
  84810. + */
  84811. +extern int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port);
  84812. +
  84813. +/** Call this function to check if bandwidth was allocated for specified endpoint.
  84814. + * Only for ISOC and INTERRUPT endpoints.
  84815. + *
  84816. + * @param hcd The HCD
  84817. + * @param ep_handle Endpoint handle
  84818. + */
  84819. +extern int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd,
  84820. + void *ep_handle);
  84821. +
  84822. +/** Call this function to check if bandwidth was freed for specified endpoint.
  84823. + *
  84824. + * @param hcd The HCD
  84825. + * @param ep_handle Endpoint handle
  84826. + */
  84827. +extern int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle);
  84828. +
  84829. +/** Returns bandwidth allocated for specified endpoint in microseconds.
  84830. + * Only for ISOC and INTERRUPT endpoints.
  84831. + *
  84832. + * @param hcd The HCD
  84833. + * @param ep_handle Endpoint handle
  84834. + */
  84835. +extern uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd,
  84836. + void *ep_handle);
  84837. +
  84838. +/** @} */
  84839. +
  84840. +#endif /* __DWC_HCD_IF_H__ */
  84841. +#endif /* DWC_DEVICE_ONLY */
  84842. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
  84843. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c 1969-12-31 18:00:00.000000000 -0600
  84844. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c 2014-12-03 19:13:40.220418001 -0600
  84845. @@ -0,0 +1,2688 @@
  84846. +/* ==========================================================================
  84847. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_intr.c $
  84848. + * $Revision: #89 $
  84849. + * $Date: 2011/10/20 $
  84850. + * $Change: 1869487 $
  84851. + *
  84852. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  84853. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  84854. + * otherwise expressly agreed to in writing between Synopsys and you.
  84855. + *
  84856. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  84857. + * any End User Software License Agreement or Agreement for Licensed Product
  84858. + * with Synopsys or any supplement thereto. You are permitted to use and
  84859. + * redistribute this Software in source and binary forms, with or without
  84860. + * modification, provided that redistributions of source code must retain this
  84861. + * notice. You may not view, use, disclose, copy or distribute this file or
  84862. + * any information contained herein except pursuant to this license grant from
  84863. + * Synopsys. If you do not agree with this notice, including the disclaimer
  84864. + * below, then you are not authorized to use the Software.
  84865. + *
  84866. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  84867. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  84868. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  84869. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  84870. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  84871. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  84872. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  84873. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  84874. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  84875. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  84876. + * DAMAGE.
  84877. + * ========================================================================== */
  84878. +#ifndef DWC_DEVICE_ONLY
  84879. +
  84880. +#include "dwc_otg_hcd.h"
  84881. +#include "dwc_otg_regs.h"
  84882. +
  84883. +#include <linux/jiffies.h>
  84884. +#include <mach/hardware.h>
  84885. +#include <asm/fiq.h>
  84886. +
  84887. +
  84888. +extern bool microframe_schedule;
  84889. +
  84890. +/** @file
  84891. + * This file contains the implementation of the HCD Interrupt handlers.
  84892. + */
  84893. +
  84894. +int fiq_done, int_done;
  84895. +
  84896. +#ifdef FIQ_DEBUG
  84897. +char buffer[1000*16];
  84898. +int wptr;
  84899. +void notrace _fiq_print(FIQDBG_T dbg_lvl, char *fmt, ...)
  84900. +{
  84901. + FIQDBG_T dbg_lvl_req = FIQDBG_PORTHUB;
  84902. + va_list args;
  84903. + char text[17];
  84904. + hfnum_data_t hfnum = { .d32 = FIQ_READ(dwc_regs_base + 0x408) };
  84905. +
  84906. + if(dbg_lvl & dbg_lvl_req || dbg_lvl == FIQDBG_ERR)
  84907. + {
  84908. + local_fiq_disable();
  84909. + snprintf(text, 9, "%4d%d:%d ", hfnum.b.frnum/8, hfnum.b.frnum%8, 8 - hfnum.b.frrem/937);
  84910. + va_start(args, fmt);
  84911. + vsnprintf(text+8, 9, fmt, args);
  84912. + va_end(args);
  84913. +
  84914. + memcpy(buffer + wptr, text, 16);
  84915. + wptr = (wptr + 16) % sizeof(buffer);
  84916. + local_fiq_enable();
  84917. + }
  84918. +}
  84919. +#endif
  84920. +
  84921. +/** This function handles interrupts for the HCD. */
  84922. +int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  84923. +{
  84924. + int retval = 0;
  84925. + static int last_time;
  84926. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  84927. + gintsts_data_t gintsts;
  84928. + gintmsk_data_t gintmsk;
  84929. + hfnum_data_t hfnum;
  84930. + haintmsk_data_t haintmsk;
  84931. +
  84932. +#ifdef DEBUG
  84933. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  84934. +
  84935. +#endif
  84936. +
  84937. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  84938. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  84939. +
  84940. + /* Exit from ISR if core is hibernated */
  84941. + if (core_if->hibernation_suspend == 1) {
  84942. + goto exit_handler_routine;
  84943. + }
  84944. + DWC_SPINLOCK(dwc_otg_hcd->lock);
  84945. + /* Check if HOST Mode */
  84946. + if (dwc_otg_is_host_mode(core_if)) {
  84947. + if (fiq_enable) {
  84948. + local_fiq_disable();
  84949. + /* Pull in from the FIQ's disabled mask */
  84950. + gintmsk.d32 = gintmsk.d32 | ~(dwc_otg_hcd->fiq_state->gintmsk_saved.d32);
  84951. + dwc_otg_hcd->fiq_state->gintmsk_saved.d32 = ~0;
  84952. + }
  84953. +
  84954. + if (fiq_fsm_enable && ( 0x0000FFFF & ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint))) {
  84955. + gintsts.b.hcintr = 1;
  84956. + }
  84957. +
  84958. + /* Danger will robinson: fake a SOF if necessary */
  84959. + if (fiq_fsm_enable && (dwc_otg_hcd->fiq_state->gintmsk_saved.b.sofintr == 1)) {
  84960. + gintsts.b.sofintr = 1;
  84961. + }
  84962. + gintsts.d32 &= gintmsk.d32;
  84963. +
  84964. + if (fiq_enable)
  84965. + local_fiq_enable();
  84966. +
  84967. + if (!gintsts.d32) {
  84968. + goto exit_handler_routine;
  84969. + }
  84970. +
  84971. +#ifdef DEBUG
  84972. + // We should be OK doing this because the common interrupts should already have been serviced
  84973. + /* Don't print debug message in the interrupt handler on SOF */
  84974. +#ifndef DEBUG_SOF
  84975. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  84976. +#endif
  84977. + DWC_DEBUGPL(DBG_HCDI, "\n");
  84978. +#endif
  84979. +
  84980. +#ifdef DEBUG
  84981. +#ifndef DEBUG_SOF
  84982. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  84983. +#endif
  84984. + DWC_DEBUGPL(DBG_HCDI,
  84985. + "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x core_if=%p\n",
  84986. + gintsts.d32, core_if);
  84987. +#endif
  84988. + hfnum.d32 = DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->hfnum);
  84989. + if (gintsts.b.sofintr) {
  84990. + retval |= dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd);
  84991. + }
  84992. +
  84993. + if (gintsts.b.rxstsqlvl) {
  84994. + retval |=
  84995. + dwc_otg_hcd_handle_rx_status_q_level_intr
  84996. + (dwc_otg_hcd);
  84997. + }
  84998. + if (gintsts.b.nptxfempty) {
  84999. + retval |=
  85000. + dwc_otg_hcd_handle_np_tx_fifo_empty_intr
  85001. + (dwc_otg_hcd);
  85002. + }
  85003. + if (gintsts.b.i2cintr) {
  85004. + /** @todo Implement i2cintr handler. */
  85005. + }
  85006. + if (gintsts.b.portintr) {
  85007. +
  85008. + gintmsk_data_t gintmsk = { .b.portintr = 1};
  85009. + retval |= dwc_otg_hcd_handle_port_intr(dwc_otg_hcd);
  85010. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  85011. + }
  85012. + if (gintsts.b.hcintr) {
  85013. + retval |= dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd);
  85014. + }
  85015. + if (gintsts.b.ptxfempty) {
  85016. + retval |=
  85017. + dwc_otg_hcd_handle_perio_tx_fifo_empty_intr
  85018. + (dwc_otg_hcd);
  85019. + }
  85020. +#ifdef DEBUG
  85021. +#ifndef DEBUG_SOF
  85022. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  85023. +#endif
  85024. + {
  85025. + DWC_DEBUGPL(DBG_HCDI,
  85026. + "DWC OTG HCD Finished Servicing Interrupts\n");
  85027. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintsts=0x%08x\n",
  85028. + DWC_READ_REG32(&global_regs->gintsts));
  85029. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintmsk=0x%08x\n",
  85030. + DWC_READ_REG32(&global_regs->gintmsk));
  85031. + }
  85032. +#endif
  85033. +
  85034. +#ifdef DEBUG
  85035. +#ifndef DEBUG_SOF
  85036. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  85037. +#endif
  85038. + DWC_DEBUGPL(DBG_HCDI, "\n");
  85039. +#endif
  85040. +
  85041. + }
  85042. +
  85043. +exit_handler_routine:
  85044. + if (fiq_enable) {
  85045. + gintmsk_data_t gintmsk_new;
  85046. + haintmsk_data_t haintmsk_new;
  85047. + local_fiq_disable();
  85048. + gintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->gintmsk_saved.d32;
  85049. + if(fiq_fsm_enable)
  85050. + haintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->haintmsk_saved.d32;
  85051. + else
  85052. + haintmsk_new.d32 = 0x0000FFFF;
  85053. +
  85054. + /* The FIQ could have sneaked another interrupt in. If so, don't clear MPHI */
  85055. + if ((gintmsk_new.d32 == ~0) && (haintmsk_new.d32 == 0x0000FFFF)) {
  85056. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.intstat, (1<<16));
  85057. + if (dwc_otg_hcd->fiq_state->mphi_int_count >= 50) {
  85058. + fiq_print(FIQDBG_INT, dwc_otg_hcd->fiq_state, "MPHI CLR");
  85059. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, ((1<<31) + (1<<16)));
  85060. + while (!(DWC_READ_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & (1 << 17)))
  85061. + ;
  85062. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, (1<<31));
  85063. + dwc_otg_hcd->fiq_state->mphi_int_count = 0;
  85064. + }
  85065. + int_done++;
  85066. + }
  85067. + haintmsk.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
  85068. + /* Re-enable interrupts that the FIQ masked (first time round) */
  85069. + FIQ_WRITE(dwc_otg_hcd->fiq_state->dwc_regs_base + GINTMSK, gintmsk.d32);
  85070. + local_fiq_enable();
  85071. +
  85072. + if ((jiffies / HZ) > last_time) {
  85073. + //dwc_otg_qh_t *qh;
  85074. + //dwc_list_link_t *cur;
  85075. + /* Once a second output the fiq and irq numbers, useful for debug */
  85076. + last_time = jiffies / HZ;
  85077. + // DWC_WARN("np_kick=%d AHC=%d sched_frame=%d cur_frame=%d int_done=%d fiq_done=%d",
  85078. + // dwc_otg_hcd->fiq_state->kick_np_queues, dwc_otg_hcd->available_host_channels,
  85079. + // dwc_otg_hcd->fiq_state->next_sched_frame, hfnum.b.frnum, int_done, dwc_otg_hcd->fiq_state->fiq_done);
  85080. + //printk(KERN_WARNING "Periodic queues:\n");
  85081. + }
  85082. + }
  85083. +
  85084. + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
  85085. + return retval;
  85086. +}
  85087. +
  85088. +#ifdef DWC_TRACK_MISSED_SOFS
  85089. +
  85090. +#warning Compiling code to track missed SOFs
  85091. +#define FRAME_NUM_ARRAY_SIZE 1000
  85092. +/**
  85093. + * This function is for debug only.
  85094. + */
  85095. +static inline void track_missed_sofs(uint16_t curr_frame_number)
  85096. +{
  85097. + static uint16_t frame_num_array[FRAME_NUM_ARRAY_SIZE];
  85098. + static uint16_t last_frame_num_array[FRAME_NUM_ARRAY_SIZE];
  85099. + static int frame_num_idx = 0;
  85100. + static uint16_t last_frame_num = DWC_HFNUM_MAX_FRNUM;
  85101. + static int dumped_frame_num_array = 0;
  85102. +
  85103. + if (frame_num_idx < FRAME_NUM_ARRAY_SIZE) {
  85104. + if (((last_frame_num + 1) & DWC_HFNUM_MAX_FRNUM) !=
  85105. + curr_frame_number) {
  85106. + frame_num_array[frame_num_idx] = curr_frame_number;
  85107. + last_frame_num_array[frame_num_idx++] = last_frame_num;
  85108. + }
  85109. + } else if (!dumped_frame_num_array) {
  85110. + int i;
  85111. + DWC_PRINTF("Frame Last Frame\n");
  85112. + DWC_PRINTF("----- ----------\n");
  85113. + for (i = 0; i < FRAME_NUM_ARRAY_SIZE; i++) {
  85114. + DWC_PRINTF("0x%04x 0x%04x\n",
  85115. + frame_num_array[i], last_frame_num_array[i]);
  85116. + }
  85117. + dumped_frame_num_array = 1;
  85118. + }
  85119. + last_frame_num = curr_frame_number;
  85120. +}
  85121. +#endif
  85122. +
  85123. +/**
  85124. + * Handles the start-of-frame interrupt in host mode. Non-periodic
  85125. + * transactions may be queued to the DWC_otg controller for the current
  85126. + * (micro)frame. Periodic transactions may be queued to the controller for the
  85127. + * next (micro)frame.
  85128. + */
  85129. +int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * hcd)
  85130. +{
  85131. + hfnum_data_t hfnum;
  85132. + gintsts_data_t gintsts = { .d32 = 0 };
  85133. + dwc_list_link_t *qh_entry;
  85134. + dwc_otg_qh_t *qh;
  85135. + dwc_otg_transaction_type_e tr_type;
  85136. + int did_something = 0;
  85137. + int32_t next_sched_frame = -1;
  85138. +
  85139. + hfnum.d32 =
  85140. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  85141. +
  85142. +#ifdef DEBUG_SOF
  85143. + DWC_DEBUGPL(DBG_HCD, "--Start of Frame Interrupt--\n");
  85144. +#endif
  85145. + hcd->frame_number = hfnum.b.frnum;
  85146. +
  85147. +#ifdef DEBUG
  85148. + hcd->frrem_accum += hfnum.b.frrem;
  85149. + hcd->frrem_samples++;
  85150. +#endif
  85151. +
  85152. +#ifdef DWC_TRACK_MISSED_SOFS
  85153. + track_missed_sofs(hcd->frame_number);
  85154. +#endif
  85155. + /* Determine whether any periodic QHs should be executed. */
  85156. + qh_entry = DWC_LIST_FIRST(&hcd->periodic_sched_inactive);
  85157. + while (qh_entry != &hcd->periodic_sched_inactive) {
  85158. + qh = DWC_LIST_ENTRY(qh_entry, dwc_otg_qh_t, qh_list_entry);
  85159. + qh_entry = qh_entry->next;
  85160. + if (dwc_frame_num_le(qh->sched_frame, hcd->frame_number)) {
  85161. +
  85162. + /*
  85163. + * Move QH to the ready list to be executed next
  85164. + * (micro)frame.
  85165. + */
  85166. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  85167. + &qh->qh_list_entry);
  85168. +
  85169. + did_something = 1;
  85170. + }
  85171. + else
  85172. + {
  85173. + if(next_sched_frame < 0 || dwc_frame_num_le(qh->sched_frame, next_sched_frame))
  85174. + {
  85175. + next_sched_frame = qh->sched_frame;
  85176. + }
  85177. + }
  85178. + }
  85179. + if (fiq_enable)
  85180. + hcd->fiq_state->next_sched_frame = next_sched_frame;
  85181. +
  85182. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  85183. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  85184. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  85185. + did_something = 1;
  85186. + }
  85187. +
  85188. + /* Clear interrupt - but do not trample on the FIQ sof */
  85189. + if (!fiq_fsm_enable) {
  85190. + gintsts.b.sofintr = 1;
  85191. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->gintsts, gintsts.d32);
  85192. + }
  85193. + return 1;
  85194. +}
  85195. +
  85196. +/** Handles the Rx Status Queue Level Interrupt, which indicates that there is at
  85197. + * least one packet in the Rx FIFO. The packets are moved from the FIFO to
  85198. + * memory if the DWC_otg controller is operating in Slave mode. */
  85199. +int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  85200. +{
  85201. + host_grxsts_data_t grxsts;
  85202. + dwc_hc_t *hc = NULL;
  85203. +
  85204. + DWC_DEBUGPL(DBG_HCD, "--RxStsQ Level Interrupt--\n");
  85205. +
  85206. + grxsts.d32 =
  85207. + DWC_READ_REG32(&dwc_otg_hcd->core_if->core_global_regs->grxstsp);
  85208. +
  85209. + hc = dwc_otg_hcd->hc_ptr_array[grxsts.b.chnum];
  85210. + if (!hc) {
  85211. + DWC_ERROR("Unable to get corresponding channel\n");
  85212. + return 0;
  85213. + }
  85214. +
  85215. + /* Packet Status */
  85216. + DWC_DEBUGPL(DBG_HCDV, " Ch num = %d\n", grxsts.b.chnum);
  85217. + DWC_DEBUGPL(DBG_HCDV, " Count = %d\n", grxsts.b.bcnt);
  85218. + DWC_DEBUGPL(DBG_HCDV, " DPID = %d, hc.dpid = %d\n", grxsts.b.dpid,
  85219. + hc->data_pid_start);
  85220. + DWC_DEBUGPL(DBG_HCDV, " PStatus = %d\n", grxsts.b.pktsts);
  85221. +
  85222. + switch (grxsts.b.pktsts) {
  85223. + case DWC_GRXSTS_PKTSTS_IN:
  85224. + /* Read the data into the host buffer. */
  85225. + if (grxsts.b.bcnt > 0) {
  85226. + dwc_otg_read_packet(dwc_otg_hcd->core_if,
  85227. + hc->xfer_buff, grxsts.b.bcnt);
  85228. +
  85229. + /* Update the HC fields for the next packet received. */
  85230. + hc->xfer_count += grxsts.b.bcnt;
  85231. + hc->xfer_buff += grxsts.b.bcnt;
  85232. + }
  85233. +
  85234. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  85235. + case DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
  85236. + case DWC_GRXSTS_PKTSTS_CH_HALTED:
  85237. + /* Handled in interrupt, just ignore data */
  85238. + break;
  85239. + default:
  85240. + DWC_ERROR("RX_STS_Q Interrupt: Unknown status %d\n",
  85241. + grxsts.b.pktsts);
  85242. + break;
  85243. + }
  85244. +
  85245. + return 1;
  85246. +}
  85247. +
  85248. +/** This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
  85249. + * data packets may be written to the FIFO for OUT transfers. More requests
  85250. + * may be written to the non-periodic request queue for IN transfers. This
  85251. + * interrupt is enabled only in Slave mode. */
  85252. +int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  85253. +{
  85254. + DWC_DEBUGPL(DBG_HCD, "--Non-Periodic TxFIFO Empty Interrupt--\n");
  85255. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  85256. + DWC_OTG_TRANSACTION_NON_PERIODIC);
  85257. + return 1;
  85258. +}
  85259. +
  85260. +/** This interrupt occurs when the periodic Tx FIFO is half-empty. More data
  85261. + * packets may be written to the FIFO for OUT transfers. More requests may be
  85262. + * written to the periodic request queue for IN transfers. This interrupt is
  85263. + * enabled only in Slave mode. */
  85264. +int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  85265. +{
  85266. + DWC_DEBUGPL(DBG_HCD, "--Periodic TxFIFO Empty Interrupt--\n");
  85267. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  85268. + DWC_OTG_TRANSACTION_PERIODIC);
  85269. + return 1;
  85270. +}
  85271. +
  85272. +/** There are multiple conditions that can cause a port interrupt. This function
  85273. + * determines which interrupt conditions have occurred and handles them
  85274. + * appropriately. */
  85275. +int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  85276. +{
  85277. + int retval = 0;
  85278. + hprt0_data_t hprt0;
  85279. + hprt0_data_t hprt0_modify;
  85280. +
  85281. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  85282. + hprt0_modify.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  85283. +
  85284. + /* Clear appropriate bits in HPRT0 to clear the interrupt bit in
  85285. + * GINTSTS */
  85286. +
  85287. + hprt0_modify.b.prtena = 0;
  85288. + hprt0_modify.b.prtconndet = 0;
  85289. + hprt0_modify.b.prtenchng = 0;
  85290. + hprt0_modify.b.prtovrcurrchng = 0;
  85291. +
  85292. + /* Port Connect Detected
  85293. + * Set flag and clear if detected */
  85294. + if (dwc_otg_hcd->core_if->hibernation_suspend == 1) {
  85295. + // Dont modify port status if we are in hibernation state
  85296. + hprt0_modify.b.prtconndet = 1;
  85297. + hprt0_modify.b.prtenchng = 1;
  85298. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  85299. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  85300. + return retval;
  85301. + }
  85302. +
  85303. + if (hprt0.b.prtconndet) {
  85304. + /** @todo - check if steps performed in 'else' block should be perfromed regardles adp */
  85305. + if (dwc_otg_hcd->core_if->adp_enable &&
  85306. + dwc_otg_hcd->core_if->adp.vbuson_timer_started == 1) {
  85307. + DWC_PRINTF("PORT CONNECT DETECTED ----------------\n");
  85308. + DWC_TIMER_CANCEL(dwc_otg_hcd->core_if->adp.vbuson_timer);
  85309. + dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  85310. + /* TODO - check if this is required, as
  85311. + * host initialization was already performed
  85312. + * after initial ADP probing
  85313. + */
  85314. + /*dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  85315. + dwc_otg_core_init(dwc_otg_hcd->core_if);
  85316. + dwc_otg_enable_global_interrupts(dwc_otg_hcd->core_if);
  85317. + cil_hcd_start(dwc_otg_hcd->core_if);*/
  85318. + } else {
  85319. +
  85320. + DWC_DEBUGPL(DBG_HCD, "--Port Interrupt HPRT0=0x%08x "
  85321. + "Port Connect Detected--\n", hprt0.d32);
  85322. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  85323. + dwc_otg_hcd->flags.b.port_connect_status = 1;
  85324. + hprt0_modify.b.prtconndet = 1;
  85325. +
  85326. + /* B-Device has connected, Delete the connection timer. */
  85327. + DWC_TIMER_CANCEL(dwc_otg_hcd->conn_timer);
  85328. + }
  85329. + /* The Hub driver asserts a reset when it sees port connect
  85330. + * status change flag */
  85331. + retval |= 1;
  85332. + }
  85333. +
  85334. + /* Port Enable Changed
  85335. + * Clear if detected - Set internal flag if disabled */
  85336. + if (hprt0.b.prtenchng) {
  85337. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  85338. + "Port Enable Changed--\n", hprt0.d32);
  85339. + hprt0_modify.b.prtenchng = 1;
  85340. + if (hprt0.b.prtena == 1) {
  85341. + hfir_data_t hfir;
  85342. + int do_reset = 0;
  85343. + dwc_otg_core_params_t *params =
  85344. + dwc_otg_hcd->core_if->core_params;
  85345. + dwc_otg_core_global_regs_t *global_regs =
  85346. + dwc_otg_hcd->core_if->core_global_regs;
  85347. + dwc_otg_host_if_t *host_if =
  85348. + dwc_otg_hcd->core_if->host_if;
  85349. +
  85350. + /* Every time when port enables calculate
  85351. + * HFIR.FrInterval
  85352. + */
  85353. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  85354. + hfir.b.frint = calc_frame_interval(dwc_otg_hcd->core_if);
  85355. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  85356. +
  85357. + /* Check if we need to adjust the PHY clock speed for
  85358. + * low power and adjust it */
  85359. + if (params->host_support_fs_ls_low_power) {
  85360. + gusbcfg_data_t usbcfg;
  85361. +
  85362. + usbcfg.d32 =
  85363. + DWC_READ_REG32(&global_regs->gusbcfg);
  85364. +
  85365. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED
  85366. + || hprt0.b.prtspd ==
  85367. + DWC_HPRT0_PRTSPD_FULL_SPEED) {
  85368. + /*
  85369. + * Low power
  85370. + */
  85371. + hcfg_data_t hcfg;
  85372. + if (usbcfg.b.phylpwrclksel == 0) {
  85373. + /* Set PHY low power clock select for FS/LS devices */
  85374. + usbcfg.b.phylpwrclksel = 1;
  85375. + DWC_WRITE_REG32
  85376. + (&global_regs->gusbcfg,
  85377. + usbcfg.d32);
  85378. + do_reset = 1;
  85379. + }
  85380. +
  85381. + hcfg.d32 =
  85382. + DWC_READ_REG32
  85383. + (&host_if->host_global_regs->hcfg);
  85384. +
  85385. + if (hprt0.b.prtspd ==
  85386. + DWC_HPRT0_PRTSPD_LOW_SPEED
  85387. + && params->host_ls_low_power_phy_clk
  85388. + ==
  85389. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)
  85390. + {
  85391. + /* 6 MHZ */
  85392. + DWC_DEBUGPL(DBG_CIL,
  85393. + "FS_PHY programming HCFG to 6 MHz (Low Power)\n");
  85394. + if (hcfg.b.fslspclksel !=
  85395. + DWC_HCFG_6_MHZ) {
  85396. + hcfg.b.fslspclksel =
  85397. + DWC_HCFG_6_MHZ;
  85398. + DWC_WRITE_REG32
  85399. + (&host_if->host_global_regs->hcfg,
  85400. + hcfg.d32);
  85401. + do_reset = 1;
  85402. + }
  85403. + } else {
  85404. + /* 48 MHZ */
  85405. + DWC_DEBUGPL(DBG_CIL,
  85406. + "FS_PHY programming HCFG to 48 MHz ()\n");
  85407. + if (hcfg.b.fslspclksel !=
  85408. + DWC_HCFG_48_MHZ) {
  85409. + hcfg.b.fslspclksel =
  85410. + DWC_HCFG_48_MHZ;
  85411. + DWC_WRITE_REG32
  85412. + (&host_if->host_global_regs->hcfg,
  85413. + hcfg.d32);
  85414. + do_reset = 1;
  85415. + }
  85416. + }
  85417. + } else {
  85418. + /*
  85419. + * Not low power
  85420. + */
  85421. + if (usbcfg.b.phylpwrclksel == 1) {
  85422. + usbcfg.b.phylpwrclksel = 0;
  85423. + DWC_WRITE_REG32
  85424. + (&global_regs->gusbcfg,
  85425. + usbcfg.d32);
  85426. + do_reset = 1;
  85427. + }
  85428. + }
  85429. +
  85430. + if (do_reset) {
  85431. + DWC_TASK_SCHEDULE(dwc_otg_hcd->reset_tasklet);
  85432. + }
  85433. + }
  85434. +
  85435. + if (!do_reset) {
  85436. + /* Port has been enabled set the reset change flag */
  85437. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  85438. + }
  85439. + } else {
  85440. + dwc_otg_hcd->flags.b.port_enable_change = 1;
  85441. + }
  85442. + retval |= 1;
  85443. + }
  85444. +
  85445. + /** Overcurrent Change Interrupt */
  85446. + if (hprt0.b.prtovrcurrchng) {
  85447. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  85448. + "Port Overcurrent Changed--\n", hprt0.d32);
  85449. + dwc_otg_hcd->flags.b.port_over_current_change = 1;
  85450. + hprt0_modify.b.prtovrcurrchng = 1;
  85451. + retval |= 1;
  85452. + }
  85453. +
  85454. + /* Clear Port Interrupts */
  85455. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  85456. +
  85457. + return retval;
  85458. +}
  85459. +
  85460. +/** This interrupt indicates that one or more host channels has a pending
  85461. + * interrupt. There are multiple conditions that can cause each host channel
  85462. + * interrupt. This function determines which conditions have occurred for each
  85463. + * host channel interrupt and handles them appropriately. */
  85464. +int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  85465. +{
  85466. + int i;
  85467. + int retval = 0;
  85468. + haint_data_t haint = { .d32 = 0 } ;
  85469. +
  85470. + /* Clear appropriate bits in HCINTn to clear the interrupt bit in
  85471. + * GINTSTS */
  85472. +
  85473. + if (!fiq_fsm_enable)
  85474. + haint.d32 = dwc_otg_read_host_all_channels_intr(dwc_otg_hcd->core_if);
  85475. +
  85476. + // Overwrite with saved interrupts from fiq handler
  85477. + if(fiq_fsm_enable)
  85478. + {
  85479. + /* check the mask? */
  85480. + local_fiq_disable();
  85481. + haint.b2.chint |= ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint);
  85482. + dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
  85483. + local_fiq_enable();
  85484. + }
  85485. +
  85486. + for (i = 0; i < dwc_otg_hcd->core_if->core_params->host_channels; i++) {
  85487. + if (haint.b2.chint & (1 << i)) {
  85488. + retval |= dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd, i);
  85489. + }
  85490. + }
  85491. +
  85492. + return retval;
  85493. +}
  85494. +
  85495. +/**
  85496. + * Gets the actual length of a transfer after the transfer halts. _halt_status
  85497. + * holds the reason for the halt.
  85498. + *
  85499. + * For IN transfers where halt_status is DWC_OTG_HC_XFER_COMPLETE,
  85500. + * *short_read is set to 1 upon return if less than the requested
  85501. + * number of bytes were transferred. Otherwise, *short_read is set to 0 upon
  85502. + * return. short_read may also be NULL on entry, in which case it remains
  85503. + * unchanged.
  85504. + */
  85505. +static uint32_t get_actual_xfer_length(dwc_hc_t * hc,
  85506. + dwc_otg_hc_regs_t * hc_regs,
  85507. + dwc_otg_qtd_t * qtd,
  85508. + dwc_otg_halt_status_e halt_status,
  85509. + int *short_read)
  85510. +{
  85511. + hctsiz_data_t hctsiz;
  85512. + uint32_t length;
  85513. +
  85514. + if (short_read != NULL) {
  85515. + *short_read = 0;
  85516. + }
  85517. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  85518. +
  85519. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  85520. + if (hc->ep_is_in) {
  85521. + length = hc->xfer_len - hctsiz.b.xfersize;
  85522. + if (short_read != NULL) {
  85523. + *short_read = (hctsiz.b.xfersize != 0);
  85524. + }
  85525. + } else if (hc->qh->do_split) {
  85526. + //length = split_out_xfersize[hc->hc_num];
  85527. + length = qtd->ssplit_out_xfer_count;
  85528. + } else {
  85529. + length = hc->xfer_len;
  85530. + }
  85531. + } else {
  85532. + /*
  85533. + * Must use the hctsiz.pktcnt field to determine how much data
  85534. + * has been transferred. This field reflects the number of
  85535. + * packets that have been transferred via the USB. This is
  85536. + * always an integral number of packets if the transfer was
  85537. + * halted before its normal completion. (Can't use the
  85538. + * hctsiz.xfersize field because that reflects the number of
  85539. + * bytes transferred via the AHB, not the USB).
  85540. + */
  85541. + length =
  85542. + (hc->start_pkt_count - hctsiz.b.pktcnt) * hc->max_packet;
  85543. + }
  85544. +
  85545. + return length;
  85546. +}
  85547. +
  85548. +/**
  85549. + * Updates the state of the URB after a Transfer Complete interrupt on the
  85550. + * host channel. Updates the actual_length field of the URB based on the
  85551. + * number of bytes transferred via the host channel. Sets the URB status
  85552. + * if the data transfer is finished.
  85553. + *
  85554. + * @return 1 if the data transfer specified by the URB is completely finished,
  85555. + * 0 otherwise.
  85556. + */
  85557. +static int update_urb_state_xfer_comp(dwc_hc_t * hc,
  85558. + dwc_otg_hc_regs_t * hc_regs,
  85559. + dwc_otg_hcd_urb_t * urb,
  85560. + dwc_otg_qtd_t * qtd)
  85561. +{
  85562. + int xfer_done = 0;
  85563. + int short_read = 0;
  85564. +
  85565. + int xfer_length;
  85566. +
  85567. + xfer_length = get_actual_xfer_length(hc, hc_regs, qtd,
  85568. + DWC_OTG_HC_XFER_COMPLETE,
  85569. + &short_read);
  85570. +
  85571. + /* non DWORD-aligned buffer case handling. */
  85572. + if (hc->align_buff && xfer_length && hc->ep_is_in) {
  85573. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  85574. + xfer_length);
  85575. + }
  85576. +
  85577. + urb->actual_length += xfer_length;
  85578. +
  85579. + if (xfer_length && (hc->ep_type == DWC_OTG_EP_TYPE_BULK) &&
  85580. + (urb->flags & URB_SEND_ZERO_PACKET)
  85581. + && (urb->actual_length == urb->length)
  85582. + && !(urb->length % hc->max_packet)) {
  85583. + xfer_done = 0;
  85584. + } else if (short_read || urb->actual_length >= urb->length) {
  85585. + xfer_done = 1;
  85586. + urb->status = 0;
  85587. + }
  85588. +
  85589. +#ifdef DEBUG
  85590. + {
  85591. + hctsiz_data_t hctsiz;
  85592. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  85593. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  85594. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  85595. + hc->hc_num);
  85596. + DWC_DEBUGPL(DBG_HCDV, " hc->xfer_len %d\n", hc->xfer_len);
  85597. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.xfersize %d\n",
  85598. + hctsiz.b.xfersize);
  85599. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  85600. + urb->length);
  85601. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  85602. + urb->actual_length);
  85603. + DWC_DEBUGPL(DBG_HCDV, " short_read %d, xfer_done %d\n",
  85604. + short_read, xfer_done);
  85605. + }
  85606. +#endif
  85607. +
  85608. + return xfer_done;
  85609. +}
  85610. +
  85611. +/*
  85612. + * Save the starting data toggle for the next transfer. The data toggle is
  85613. + * saved in the QH for non-control transfers and it's saved in the QTD for
  85614. + * control transfers.
  85615. + */
  85616. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  85617. + dwc_otg_hc_regs_t * hc_regs, dwc_otg_qtd_t * qtd)
  85618. +{
  85619. + hctsiz_data_t hctsiz;
  85620. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  85621. +
  85622. + if (hc->ep_type != DWC_OTG_EP_TYPE_CONTROL) {
  85623. + dwc_otg_qh_t *qh = hc->qh;
  85624. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  85625. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  85626. + } else {
  85627. + qh->data_toggle = DWC_OTG_HC_PID_DATA1;
  85628. + }
  85629. + } else {
  85630. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  85631. + qtd->data_toggle = DWC_OTG_HC_PID_DATA0;
  85632. + } else {
  85633. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  85634. + }
  85635. + }
  85636. +}
  85637. +
  85638. +/**
  85639. + * Updates the state of an Isochronous URB when the transfer is stopped for
  85640. + * any reason. The fields of the current entry in the frame descriptor array
  85641. + * are set based on the transfer state and the input _halt_status. Completes
  85642. + * the Isochronous URB if all the URB frames have been completed.
  85643. + *
  85644. + * @return DWC_OTG_HC_XFER_COMPLETE if there are more frames remaining to be
  85645. + * transferred in the URB. Otherwise return DWC_OTG_HC_XFER_URB_COMPLETE.
  85646. + */
  85647. +static dwc_otg_halt_status_e
  85648. +update_isoc_urb_state(dwc_otg_hcd_t * hcd,
  85649. + dwc_hc_t * hc,
  85650. + dwc_otg_hc_regs_t * hc_regs,
  85651. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  85652. +{
  85653. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  85654. + dwc_otg_halt_status_e ret_val = halt_status;
  85655. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  85656. +
  85657. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  85658. + switch (halt_status) {
  85659. + case DWC_OTG_HC_XFER_COMPLETE:
  85660. + frame_desc->status = 0;
  85661. + frame_desc->actual_length =
  85662. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  85663. +
  85664. + /* non DWORD-aligned buffer case handling. */
  85665. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  85666. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  85667. + hc->qh->dw_align_buf, frame_desc->actual_length);
  85668. + }
  85669. +
  85670. + break;
  85671. + case DWC_OTG_HC_XFER_FRAME_OVERRUN:
  85672. + urb->error_count++;
  85673. + if (hc->ep_is_in) {
  85674. + frame_desc->status = -DWC_E_NO_STREAM_RES;
  85675. + } else {
  85676. + frame_desc->status = -DWC_E_COMMUNICATION;
  85677. + }
  85678. + frame_desc->actual_length = 0;
  85679. + break;
  85680. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  85681. + urb->error_count++;
  85682. + frame_desc->status = -DWC_E_OVERFLOW;
  85683. + /* Don't need to update actual_length in this case. */
  85684. + break;
  85685. + case DWC_OTG_HC_XFER_XACT_ERR:
  85686. + urb->error_count++;
  85687. + frame_desc->status = -DWC_E_PROTOCOL;
  85688. + frame_desc->actual_length =
  85689. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  85690. +
  85691. + /* non DWORD-aligned buffer case handling. */
  85692. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  85693. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  85694. + hc->qh->dw_align_buf, frame_desc->actual_length);
  85695. + }
  85696. + /* Skip whole frame */
  85697. + if (hc->qh->do_split && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) &&
  85698. + hc->ep_is_in && hcd->core_if->dma_enable) {
  85699. + qtd->complete_split = 0;
  85700. + qtd->isoc_split_offset = 0;
  85701. + }
  85702. +
  85703. + break;
  85704. + default:
  85705. + DWC_ASSERT(1, "Unhandled _halt_status (%d)\n", halt_status);
  85706. + break;
  85707. + }
  85708. + if (++qtd->isoc_frame_index == urb->packet_count) {
  85709. + /*
  85710. + * urb->status is not used for isoc transfers.
  85711. + * The individual frame_desc statuses are used instead.
  85712. + */
  85713. + hcd->fops->complete(hcd, urb->priv, urb, 0);
  85714. + ret_val = DWC_OTG_HC_XFER_URB_COMPLETE;
  85715. + } else {
  85716. + ret_val = DWC_OTG_HC_XFER_COMPLETE;
  85717. + }
  85718. + return ret_val;
  85719. +}
  85720. +
  85721. +/**
  85722. + * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
  85723. + * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
  85724. + * still linked to the QH, the QH is added to the end of the inactive
  85725. + * non-periodic schedule. For periodic QHs, removes the QH from the periodic
  85726. + * schedule if no more QTDs are linked to the QH.
  85727. + */
  85728. +static void deactivate_qh(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, int free_qtd)
  85729. +{
  85730. + int continue_split = 0;
  85731. + dwc_otg_qtd_t *qtd;
  85732. +
  85733. + DWC_DEBUGPL(DBG_HCDV, " %s(%p,%p,%d)\n", __func__, hcd, qh, free_qtd);
  85734. +
  85735. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  85736. +
  85737. + if (qtd->complete_split) {
  85738. + continue_split = 1;
  85739. + } else if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_MID ||
  85740. + qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_END) {
  85741. + continue_split = 1;
  85742. + }
  85743. +
  85744. + if (free_qtd) {
  85745. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  85746. + continue_split = 0;
  85747. + }
  85748. +
  85749. + qh->channel = NULL;
  85750. + dwc_otg_hcd_qh_deactivate(hcd, qh, continue_split);
  85751. +}
  85752. +
  85753. +/**
  85754. + * Releases a host channel for use by other transfers. Attempts to select and
  85755. + * queue more transactions since at least one host channel is available.
  85756. + *
  85757. + * @param hcd The HCD state structure.
  85758. + * @param hc The host channel to release.
  85759. + * @param qtd The QTD associated with the host channel. This QTD may be freed
  85760. + * if the transfer is complete or an error has occurred.
  85761. + * @param halt_status Reason the channel is being released. This status
  85762. + * determines the actions taken by this function.
  85763. + */
  85764. +static void release_channel(dwc_otg_hcd_t * hcd,
  85765. + dwc_hc_t * hc,
  85766. + dwc_otg_qtd_t * qtd,
  85767. + dwc_otg_halt_status_e halt_status)
  85768. +{
  85769. + dwc_otg_transaction_type_e tr_type;
  85770. + int free_qtd;
  85771. + dwc_irqflags_t flags;
  85772. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  85773. +
  85774. + int hog_port = 0;
  85775. +
  85776. + DWC_DEBUGPL(DBG_HCDV, " %s: channel %d, halt_status %d, xfer_len %d\n",
  85777. + __func__, hc->hc_num, halt_status, hc->xfer_len);
  85778. +
  85779. + if(fiq_fsm_enable && hc->do_split) {
  85780. + if(!hc->ep_is_in && hc->ep_type == UE_ISOCHRONOUS) {
  85781. + if(hc->xact_pos == DWC_HCSPLIT_XACTPOS_MID ||
  85782. + hc->xact_pos == DWC_HCSPLIT_XACTPOS_BEGIN) {
  85783. + hog_port = 0;
  85784. + }
  85785. + }
  85786. + }
  85787. +
  85788. + switch (halt_status) {
  85789. + case DWC_OTG_HC_XFER_URB_COMPLETE:
  85790. + free_qtd = 1;
  85791. + break;
  85792. + case DWC_OTG_HC_XFER_AHB_ERR:
  85793. + case DWC_OTG_HC_XFER_STALL:
  85794. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  85795. + free_qtd = 1;
  85796. + break;
  85797. + case DWC_OTG_HC_XFER_XACT_ERR:
  85798. + if (qtd->error_count >= 3) {
  85799. + DWC_DEBUGPL(DBG_HCDV,
  85800. + " Complete URB with transaction error\n");
  85801. + free_qtd = 1;
  85802. + qtd->urb->status = -DWC_E_PROTOCOL;
  85803. + hcd->fops->complete(hcd, qtd->urb->priv,
  85804. + qtd->urb, -DWC_E_PROTOCOL);
  85805. + } else {
  85806. + free_qtd = 0;
  85807. + }
  85808. + break;
  85809. + case DWC_OTG_HC_XFER_URB_DEQUEUE:
  85810. + /*
  85811. + * The QTD has already been removed and the QH has been
  85812. + * deactivated. Don't want to do anything except release the
  85813. + * host channel and try to queue more transfers.
  85814. + */
  85815. + goto cleanup;
  85816. + case DWC_OTG_HC_XFER_NO_HALT_STATUS:
  85817. + free_qtd = 0;
  85818. + break;
  85819. + case DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE:
  85820. + DWC_DEBUGPL(DBG_HCDV,
  85821. + " Complete URB with I/O error\n");
  85822. + free_qtd = 1;
  85823. + qtd->urb->status = -DWC_E_IO;
  85824. + hcd->fops->complete(hcd, qtd->urb->priv,
  85825. + qtd->urb, -DWC_E_IO);
  85826. + break;
  85827. + default:
  85828. + free_qtd = 0;
  85829. + break;
  85830. + }
  85831. +
  85832. + deactivate_qh(hcd, hc->qh, free_qtd);
  85833. +
  85834. +cleanup:
  85835. + /*
  85836. + * Release the host channel for use by other transfers. The cleanup
  85837. + * function clears the channel interrupt enables and conditions, so
  85838. + * there's no need to clear the Channel Halted interrupt separately.
  85839. + */
  85840. + if (fiq_fsm_enable && hcd->fiq_state->channel[hc->hc_num].fsm != FIQ_PASSTHROUGH)
  85841. + dwc_otg_cleanup_fiq_channel(hcd, hc->hc_num);
  85842. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  85843. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  85844. +
  85845. + if (!microframe_schedule) {
  85846. + switch (hc->ep_type) {
  85847. + case DWC_OTG_EP_TYPE_CONTROL:
  85848. + case DWC_OTG_EP_TYPE_BULK:
  85849. + hcd->non_periodic_channels--;
  85850. + break;
  85851. +
  85852. + default:
  85853. + /*
  85854. + * Don't release reservations for periodic channels here.
  85855. + * That's done when a periodic transfer is descheduled (i.e.
  85856. + * when the QH is removed from the periodic schedule).
  85857. + */
  85858. + break;
  85859. + }
  85860. + } else {
  85861. +
  85862. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  85863. + hcd->available_host_channels++;
  85864. + fiq_print(FIQDBG_INT, hcd->fiq_state, "AHC = %d ", hcd->available_host_channels);
  85865. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  85866. + }
  85867. +
  85868. + /* Try to queue more transfers now that there's a free channel. */
  85869. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  85870. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  85871. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  85872. + }
  85873. +}
  85874. +
  85875. +/**
  85876. + * Halts a host channel. If the channel cannot be halted immediately because
  85877. + * the request queue is full, this function ensures that the FIFO empty
  85878. + * interrupt for the appropriate queue is enabled so that the halt request can
  85879. + * be queued when there is space in the request queue.
  85880. + *
  85881. + * This function may also be called in DMA mode. In that case, the channel is
  85882. + * simply released since the core always halts the channel automatically in
  85883. + * DMA mode.
  85884. + */
  85885. +static void halt_channel(dwc_otg_hcd_t * hcd,
  85886. + dwc_hc_t * hc,
  85887. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  85888. +{
  85889. + if (hcd->core_if->dma_enable) {
  85890. + release_channel(hcd, hc, qtd, halt_status);
  85891. + return;
  85892. + }
  85893. +
  85894. + /* Slave mode processing... */
  85895. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  85896. +
  85897. + if (hc->halt_on_queue) {
  85898. + gintmsk_data_t gintmsk = {.d32 = 0 };
  85899. + dwc_otg_core_global_regs_t *global_regs;
  85900. + global_regs = hcd->core_if->core_global_regs;
  85901. +
  85902. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  85903. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  85904. + /*
  85905. + * Make sure the Non-periodic Tx FIFO empty interrupt
  85906. + * is enabled so that the non-periodic schedule will
  85907. + * be processed.
  85908. + */
  85909. + gintmsk.b.nptxfempty = 1;
  85910. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  85911. + } else {
  85912. + /*
  85913. + * Move the QH from the periodic queued schedule to
  85914. + * the periodic assigned schedule. This allows the
  85915. + * halt to be queued when the periodic schedule is
  85916. + * processed.
  85917. + */
  85918. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  85919. + &hc->qh->qh_list_entry);
  85920. +
  85921. + /*
  85922. + * Make sure the Periodic Tx FIFO Empty interrupt is
  85923. + * enabled so that the periodic schedule will be
  85924. + * processed.
  85925. + */
  85926. + gintmsk.b.ptxfempty = 1;
  85927. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  85928. + }
  85929. + }
  85930. +}
  85931. +
  85932. +/**
  85933. + * Performs common cleanup for non-periodic transfers after a Transfer
  85934. + * Complete interrupt. This function should be called after any endpoint type
  85935. + * specific handling is finished to release the host channel.
  85936. + */
  85937. +static void complete_non_periodic_xfer(dwc_otg_hcd_t * hcd,
  85938. + dwc_hc_t * hc,
  85939. + dwc_otg_hc_regs_t * hc_regs,
  85940. + dwc_otg_qtd_t * qtd,
  85941. + dwc_otg_halt_status_e halt_status)
  85942. +{
  85943. + hcint_data_t hcint;
  85944. +
  85945. + qtd->error_count = 0;
  85946. +
  85947. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  85948. + if (hcint.b.nyet) {
  85949. + /*
  85950. + * Got a NYET on the last transaction of the transfer. This
  85951. + * means that the endpoint should be in the PING state at the
  85952. + * beginning of the next transfer.
  85953. + */
  85954. + hc->qh->ping_state = 1;
  85955. + clear_hc_int(hc_regs, nyet);
  85956. + }
  85957. +
  85958. + /*
  85959. + * Always halt and release the host channel to make it available for
  85960. + * more transfers. There may still be more phases for a control
  85961. + * transfer or more data packets for a bulk transfer at this point,
  85962. + * but the host channel is still halted. A channel will be reassigned
  85963. + * to the transfer when the non-periodic schedule is processed after
  85964. + * the channel is released. This allows transactions to be queued
  85965. + * properly via dwc_otg_hcd_queue_transactions, which also enables the
  85966. + * Tx FIFO Empty interrupt if necessary.
  85967. + */
  85968. + if (hc->ep_is_in) {
  85969. + /*
  85970. + * IN transfers in Slave mode require an explicit disable to
  85971. + * halt the channel. (In DMA mode, this call simply releases
  85972. + * the channel.)
  85973. + */
  85974. + halt_channel(hcd, hc, qtd, halt_status);
  85975. + } else {
  85976. + /*
  85977. + * The channel is automatically disabled by the core for OUT
  85978. + * transfers in Slave mode.
  85979. + */
  85980. + release_channel(hcd, hc, qtd, halt_status);
  85981. + }
  85982. +}
  85983. +
  85984. +/**
  85985. + * Performs common cleanup for periodic transfers after a Transfer Complete
  85986. + * interrupt. This function should be called after any endpoint type specific
  85987. + * handling is finished to release the host channel.
  85988. + */
  85989. +static void complete_periodic_xfer(dwc_otg_hcd_t * hcd,
  85990. + dwc_hc_t * hc,
  85991. + dwc_otg_hc_regs_t * hc_regs,
  85992. + dwc_otg_qtd_t * qtd,
  85993. + dwc_otg_halt_status_e halt_status)
  85994. +{
  85995. + hctsiz_data_t hctsiz;
  85996. + qtd->error_count = 0;
  85997. +
  85998. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  85999. + if (!hc->ep_is_in || hctsiz.b.pktcnt == 0) {
  86000. + /* Core halts channel in these cases. */
  86001. + release_channel(hcd, hc, qtd, halt_status);
  86002. + } else {
  86003. + /* Flush any outstanding requests from the Tx queue. */
  86004. + halt_channel(hcd, hc, qtd, halt_status);
  86005. + }
  86006. +}
  86007. +
  86008. +static int32_t handle_xfercomp_isoc_split_in(dwc_otg_hcd_t * hcd,
  86009. + dwc_hc_t * hc,
  86010. + dwc_otg_hc_regs_t * hc_regs,
  86011. + dwc_otg_qtd_t * qtd)
  86012. +{
  86013. + uint32_t len;
  86014. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  86015. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  86016. +
  86017. + len = get_actual_xfer_length(hc, hc_regs, qtd,
  86018. + DWC_OTG_HC_XFER_COMPLETE, NULL);
  86019. +
  86020. + if (!len) {
  86021. + qtd->complete_split = 0;
  86022. + qtd->isoc_split_offset = 0;
  86023. + return 0;
  86024. + }
  86025. + frame_desc->actual_length += len;
  86026. +
  86027. + if (hc->align_buff && len)
  86028. + dwc_memcpy(qtd->urb->buf + frame_desc->offset +
  86029. + qtd->isoc_split_offset, hc->qh->dw_align_buf, len);
  86030. + qtd->isoc_split_offset += len;
  86031. +
  86032. + if (frame_desc->length == frame_desc->actual_length) {
  86033. + frame_desc->status = 0;
  86034. + qtd->isoc_frame_index++;
  86035. + qtd->complete_split = 0;
  86036. + qtd->isoc_split_offset = 0;
  86037. + }
  86038. +
  86039. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  86040. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  86041. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  86042. + } else {
  86043. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  86044. + }
  86045. +
  86046. + return 1; /* Indicates that channel released */
  86047. +}
  86048. +
  86049. +/**
  86050. + * Handles a host channel Transfer Complete interrupt. This handler may be
  86051. + * called in either DMA mode or Slave mode.
  86052. + */
  86053. +static int32_t handle_hc_xfercomp_intr(dwc_otg_hcd_t * hcd,
  86054. + dwc_hc_t * hc,
  86055. + dwc_otg_hc_regs_t * hc_regs,
  86056. + dwc_otg_qtd_t * qtd)
  86057. +{
  86058. + int urb_xfer_done;
  86059. + dwc_otg_halt_status_e halt_status = DWC_OTG_HC_XFER_COMPLETE;
  86060. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  86061. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  86062. +
  86063. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  86064. + "Transfer Complete--\n", hc->hc_num);
  86065. +
  86066. + if (hcd->core_if->dma_desc_enable) {
  86067. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, halt_status);
  86068. + if (pipe_type == UE_ISOCHRONOUS) {
  86069. + /* Do not disable the interrupt, just clear it */
  86070. + clear_hc_int(hc_regs, xfercomp);
  86071. + return 1;
  86072. + }
  86073. + goto handle_xfercomp_done;
  86074. + }
  86075. +
  86076. + /*
  86077. + * Handle xfer complete on CSPLIT.
  86078. + */
  86079. +
  86080. + if (hc->qh->do_split) {
  86081. + if ((hc->ep_type == DWC_OTG_EP_TYPE_ISOC) && hc->ep_is_in
  86082. + && hcd->core_if->dma_enable) {
  86083. + if (qtd->complete_split
  86084. + && handle_xfercomp_isoc_split_in(hcd, hc, hc_regs,
  86085. + qtd))
  86086. + goto handle_xfercomp_done;
  86087. + } else {
  86088. + qtd->complete_split = 0;
  86089. + }
  86090. + }
  86091. +
  86092. + /* Update the QTD and URB states. */
  86093. + switch (pipe_type) {
  86094. + case UE_CONTROL:
  86095. + switch (qtd->control_phase) {
  86096. + case DWC_OTG_CONTROL_SETUP:
  86097. + if (urb->length > 0) {
  86098. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  86099. + } else {
  86100. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  86101. + }
  86102. + DWC_DEBUGPL(DBG_HCDV,
  86103. + " Control setup transaction done\n");
  86104. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  86105. + break;
  86106. + case DWC_OTG_CONTROL_DATA:{
  86107. + urb_xfer_done =
  86108. + update_urb_state_xfer_comp(hc, hc_regs, urb,
  86109. + qtd);
  86110. + if (urb_xfer_done) {
  86111. + qtd->control_phase =
  86112. + DWC_OTG_CONTROL_STATUS;
  86113. + DWC_DEBUGPL(DBG_HCDV,
  86114. + " Control data transfer done\n");
  86115. + } else {
  86116. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  86117. + }
  86118. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  86119. + break;
  86120. + }
  86121. + case DWC_OTG_CONTROL_STATUS:
  86122. + DWC_DEBUGPL(DBG_HCDV, " Control transfer complete\n");
  86123. + if (urb->status == -DWC_E_IN_PROGRESS) {
  86124. + urb->status = 0;
  86125. + }
  86126. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  86127. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  86128. + break;
  86129. + }
  86130. +
  86131. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  86132. + break;
  86133. + case UE_BULK:
  86134. + DWC_DEBUGPL(DBG_HCDV, " Bulk transfer complete\n");
  86135. + urb_xfer_done =
  86136. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  86137. + if (urb_xfer_done) {
  86138. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  86139. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  86140. + } else {
  86141. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  86142. + }
  86143. +
  86144. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  86145. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  86146. + break;
  86147. + case UE_INTERRUPT:
  86148. + DWC_DEBUGPL(DBG_HCDV, " Interrupt transfer complete\n");
  86149. + urb_xfer_done =
  86150. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  86151. +
  86152. + /*
  86153. + * Interrupt URB is done on the first transfer complete
  86154. + * interrupt.
  86155. + */
  86156. + if (urb_xfer_done) {
  86157. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  86158. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  86159. + } else {
  86160. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  86161. + }
  86162. +
  86163. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  86164. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  86165. + break;
  86166. + case UE_ISOCHRONOUS:
  86167. + DWC_DEBUGPL(DBG_HCDV, " Isochronous transfer complete\n");
  86168. + if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  86169. + halt_status =
  86170. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  86171. + DWC_OTG_HC_XFER_COMPLETE);
  86172. + }
  86173. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  86174. + break;
  86175. + }
  86176. +
  86177. +handle_xfercomp_done:
  86178. + disable_hc_int(hc_regs, xfercompl);
  86179. +
  86180. + return 1;
  86181. +}
  86182. +
  86183. +/**
  86184. + * Handles a host channel STALL interrupt. This handler may be called in
  86185. + * either DMA mode or Slave mode.
  86186. + */
  86187. +static int32_t handle_hc_stall_intr(dwc_otg_hcd_t * hcd,
  86188. + dwc_hc_t * hc,
  86189. + dwc_otg_hc_regs_t * hc_regs,
  86190. + dwc_otg_qtd_t * qtd)
  86191. +{
  86192. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  86193. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  86194. +
  86195. + DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
  86196. + "STALL Received--\n", hc->hc_num);
  86197. +
  86198. + if (hcd->core_if->dma_desc_enable) {
  86199. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, DWC_OTG_HC_XFER_STALL);
  86200. + goto handle_stall_done;
  86201. + }
  86202. +
  86203. + if (pipe_type == UE_CONTROL) {
  86204. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  86205. + }
  86206. +
  86207. + if (pipe_type == UE_BULK || pipe_type == UE_INTERRUPT) {
  86208. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  86209. + /*
  86210. + * USB protocol requires resetting the data toggle for bulk
  86211. + * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
  86212. + * setup command is issued to the endpoint. Anticipate the
  86213. + * CLEAR_FEATURE command since a STALL has occurred and reset
  86214. + * the data toggle now.
  86215. + */
  86216. + hc->qh->data_toggle = 0;
  86217. + }
  86218. +
  86219. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_STALL);
  86220. +
  86221. +handle_stall_done:
  86222. + disable_hc_int(hc_regs, stall);
  86223. +
  86224. + return 1;
  86225. +}
  86226. +
  86227. +/*
  86228. + * Updates the state of the URB when a transfer has been stopped due to an
  86229. + * abnormal condition before the transfer completes. Modifies the
  86230. + * actual_length field of the URB to reflect the number of bytes that have
  86231. + * actually been transferred via the host channel.
  86232. + */
  86233. +static void update_urb_state_xfer_intr(dwc_hc_t * hc,
  86234. + dwc_otg_hc_regs_t * hc_regs,
  86235. + dwc_otg_hcd_urb_t * urb,
  86236. + dwc_otg_qtd_t * qtd,
  86237. + dwc_otg_halt_status_e halt_status)
  86238. +{
  86239. + uint32_t bytes_transferred = get_actual_xfer_length(hc, hc_regs, qtd,
  86240. + halt_status, NULL);
  86241. + /* non DWORD-aligned buffer case handling. */
  86242. + if (hc->align_buff && bytes_transferred && hc->ep_is_in) {
  86243. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  86244. + bytes_transferred);
  86245. + }
  86246. +
  86247. + urb->actual_length += bytes_transferred;
  86248. +
  86249. +#ifdef DEBUG
  86250. + {
  86251. + hctsiz_data_t hctsiz;
  86252. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  86253. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  86254. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  86255. + hc->hc_num);
  86256. + DWC_DEBUGPL(DBG_HCDV, " hc->start_pkt_count %d\n",
  86257. + hc->start_pkt_count);
  86258. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.pktcnt %d\n", hctsiz.b.pktcnt);
  86259. + DWC_DEBUGPL(DBG_HCDV, " hc->max_packet %d\n", hc->max_packet);
  86260. + DWC_DEBUGPL(DBG_HCDV, " bytes_transferred %d\n",
  86261. + bytes_transferred);
  86262. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  86263. + urb->actual_length);
  86264. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  86265. + urb->length);
  86266. + }
  86267. +#endif
  86268. +}
  86269. +
  86270. +/**
  86271. + * Handles a host channel NAK interrupt. This handler may be called in either
  86272. + * DMA mode or Slave mode.
  86273. + */
  86274. +static int32_t handle_hc_nak_intr(dwc_otg_hcd_t * hcd,
  86275. + dwc_hc_t * hc,
  86276. + dwc_otg_hc_regs_t * hc_regs,
  86277. + dwc_otg_qtd_t * qtd)
  86278. +{
  86279. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  86280. + "NAK Received--\n", hc->hc_num);
  86281. +
  86282. + /*
  86283. + * When we get bulk NAKs then remember this so we holdoff on this qh until
  86284. + * the beginning of the next frame
  86285. + */
  86286. + switch(dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  86287. + case UE_BULK:
  86288. + case UE_CONTROL:
  86289. + if (nak_holdoff && qtd->qh->do_split)
  86290. + hc->qh->nak_frame = dwc_otg_hcd_get_frame_number(hcd);
  86291. + }
  86292. +
  86293. + /*
  86294. + * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
  86295. + * interrupt. Re-start the SSPLIT transfer.
  86296. + */
  86297. + if (hc->do_split) {
  86298. + if (hc->complete_split) {
  86299. + qtd->error_count = 0;
  86300. + }
  86301. + qtd->complete_split = 0;
  86302. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  86303. + goto handle_nak_done;
  86304. + }
  86305. +
  86306. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  86307. + case UE_CONTROL:
  86308. + case UE_BULK:
  86309. + if (hcd->core_if->dma_enable && hc->ep_is_in) {
  86310. + /*
  86311. + * NAK interrupts are enabled on bulk/control IN
  86312. + * transfers in DMA mode for the sole purpose of
  86313. + * resetting the error count after a transaction error
  86314. + * occurs. The core will continue transferring data.
  86315. + * Disable other interrupts unmasked for the same
  86316. + * reason.
  86317. + */
  86318. + disable_hc_int(hc_regs, datatglerr);
  86319. + disable_hc_int(hc_regs, ack);
  86320. + qtd->error_count = 0;
  86321. + goto handle_nak_done;
  86322. + }
  86323. +
  86324. + /*
  86325. + * NAK interrupts normally occur during OUT transfers in DMA
  86326. + * or Slave mode. For IN transfers, more requests will be
  86327. + * queued as request queue space is available.
  86328. + */
  86329. + qtd->error_count = 0;
  86330. +
  86331. + if (!hc->qh->ping_state) {
  86332. + update_urb_state_xfer_intr(hc, hc_regs,
  86333. + qtd->urb, qtd,
  86334. + DWC_OTG_HC_XFER_NAK);
  86335. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  86336. +
  86337. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH)
  86338. + hc->qh->ping_state = 1;
  86339. + }
  86340. +
  86341. + /*
  86342. + * Halt the channel so the transfer can be re-started from
  86343. + * the appropriate point or the PING protocol will
  86344. + * start/continue.
  86345. + */
  86346. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  86347. + break;
  86348. + case UE_INTERRUPT:
  86349. + qtd->error_count = 0;
  86350. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  86351. + break;
  86352. + case UE_ISOCHRONOUS:
  86353. + /* Should never get called for isochronous transfers. */
  86354. + DWC_ASSERT(1, "NACK interrupt for ISOC transfer\n");
  86355. + break;
  86356. + }
  86357. +
  86358. +handle_nak_done:
  86359. + disable_hc_int(hc_regs, nak);
  86360. +
  86361. + return 1;
  86362. +}
  86363. +
  86364. +/**
  86365. + * Handles a host channel ACK interrupt. This interrupt is enabled when
  86366. + * performing the PING protocol in Slave mode, when errors occur during
  86367. + * either Slave mode or DMA mode, and during Start Split transactions.
  86368. + */
  86369. +static int32_t handle_hc_ack_intr(dwc_otg_hcd_t * hcd,
  86370. + dwc_hc_t * hc,
  86371. + dwc_otg_hc_regs_t * hc_regs,
  86372. + dwc_otg_qtd_t * qtd)
  86373. +{
  86374. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  86375. + "ACK Received--\n", hc->hc_num);
  86376. +
  86377. + if (hc->do_split) {
  86378. + /*
  86379. + * Handle ACK on SSPLIT.
  86380. + * ACK should not occur in CSPLIT.
  86381. + */
  86382. + if (!hc->ep_is_in && hc->data_pid_start != DWC_OTG_HC_PID_SETUP) {
  86383. + qtd->ssplit_out_xfer_count = hc->xfer_len;
  86384. + }
  86385. + if (!(hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in)) {
  86386. + /* Don't need complete for isochronous out transfers. */
  86387. + qtd->complete_split = 1;
  86388. + }
  86389. +
  86390. + /* ISOC OUT */
  86391. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  86392. + switch (hc->xact_pos) {
  86393. + case DWC_HCSPLIT_XACTPOS_ALL:
  86394. + break;
  86395. + case DWC_HCSPLIT_XACTPOS_END:
  86396. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  86397. + qtd->isoc_split_offset = 0;
  86398. + break;
  86399. + case DWC_HCSPLIT_XACTPOS_BEGIN:
  86400. + case DWC_HCSPLIT_XACTPOS_MID:
  86401. + /*
  86402. + * For BEGIN or MID, calculate the length for
  86403. + * the next microframe to determine the correct
  86404. + * SSPLIT token, either MID or END.
  86405. + */
  86406. + {
  86407. + struct dwc_otg_hcd_iso_packet_desc
  86408. + *frame_desc;
  86409. +
  86410. + frame_desc =
  86411. + &qtd->urb->
  86412. + iso_descs[qtd->isoc_frame_index];
  86413. + qtd->isoc_split_offset += 188;
  86414. +
  86415. + if ((frame_desc->length -
  86416. + qtd->isoc_split_offset) <= 188) {
  86417. + qtd->isoc_split_pos =
  86418. + DWC_HCSPLIT_XACTPOS_END;
  86419. + } else {
  86420. + qtd->isoc_split_pos =
  86421. + DWC_HCSPLIT_XACTPOS_MID;
  86422. + }
  86423. +
  86424. + }
  86425. + break;
  86426. + }
  86427. + } else {
  86428. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  86429. + }
  86430. + } else {
  86431. + /*
  86432. + * An unmasked ACK on a non-split DMA transaction is
  86433. + * for the sole purpose of resetting error counts. Disable other
  86434. + * interrupts unmasked for the same reason.
  86435. + */
  86436. + if(hcd->core_if->dma_enable) {
  86437. + disable_hc_int(hc_regs, datatglerr);
  86438. + disable_hc_int(hc_regs, nak);
  86439. + }
  86440. + qtd->error_count = 0;
  86441. +
  86442. + if (hc->qh->ping_state) {
  86443. + hc->qh->ping_state = 0;
  86444. + /*
  86445. + * Halt the channel so the transfer can be re-started
  86446. + * from the appropriate point. This only happens in
  86447. + * Slave mode. In DMA mode, the ping_state is cleared
  86448. + * when the transfer is started because the core
  86449. + * automatically executes the PING, then the transfer.
  86450. + */
  86451. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  86452. + }
  86453. + }
  86454. +
  86455. + /*
  86456. + * If the ACK occurred when _not_ in the PING state, let the channel
  86457. + * continue transferring data after clearing the error count.
  86458. + */
  86459. +
  86460. + disable_hc_int(hc_regs, ack);
  86461. +
  86462. + return 1;
  86463. +}
  86464. +
  86465. +/**
  86466. + * Handles a host channel NYET interrupt. This interrupt should only occur on
  86467. + * Bulk and Control OUT endpoints and for complete split transactions. If a
  86468. + * NYET occurs at the same time as a Transfer Complete interrupt, it is
  86469. + * handled in the xfercomp interrupt handler, not here. This handler may be
  86470. + * called in either DMA mode or Slave mode.
  86471. + */
  86472. +static int32_t handle_hc_nyet_intr(dwc_otg_hcd_t * hcd,
  86473. + dwc_hc_t * hc,
  86474. + dwc_otg_hc_regs_t * hc_regs,
  86475. + dwc_otg_qtd_t * qtd)
  86476. +{
  86477. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  86478. + "NYET Received--\n", hc->hc_num);
  86479. +
  86480. + /*
  86481. + * NYET on CSPLIT
  86482. + * re-do the CSPLIT immediately on non-periodic
  86483. + */
  86484. + if (hc->do_split && hc->complete_split) {
  86485. + if (hc->ep_is_in && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  86486. + && hcd->core_if->dma_enable) {
  86487. + qtd->complete_split = 0;
  86488. + qtd->isoc_split_offset = 0;
  86489. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  86490. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  86491. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  86492. + }
  86493. + else
  86494. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  86495. + goto handle_nyet_done;
  86496. + }
  86497. +
  86498. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  86499. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  86500. + int frnum = dwc_otg_hcd_get_frame_number(hcd);
  86501. +
  86502. + // With the FIQ running we only ever see the failed NYET
  86503. + if (dwc_full_frame_num(frnum) !=
  86504. + dwc_full_frame_num(hc->qh->sched_frame) ||
  86505. + fiq_fsm_enable) {
  86506. + /*
  86507. + * No longer in the same full speed frame.
  86508. + * Treat this as a transaction error.
  86509. + */
  86510. +#if 0
  86511. + /** @todo Fix system performance so this can
  86512. + * be treated as an error. Right now complete
  86513. + * splits cannot be scheduled precisely enough
  86514. + * due to other system activity, so this error
  86515. + * occurs regularly in Slave mode.
  86516. + */
  86517. + qtd->error_count++;
  86518. +#endif
  86519. + qtd->complete_split = 0;
  86520. + halt_channel(hcd, hc, qtd,
  86521. + DWC_OTG_HC_XFER_XACT_ERR);
  86522. + /** @todo add support for isoc release */
  86523. + goto handle_nyet_done;
  86524. + }
  86525. + }
  86526. +
  86527. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  86528. + goto handle_nyet_done;
  86529. + }
  86530. +
  86531. + hc->qh->ping_state = 1;
  86532. + qtd->error_count = 0;
  86533. +
  86534. + update_urb_state_xfer_intr(hc, hc_regs, qtd->urb, qtd,
  86535. + DWC_OTG_HC_XFER_NYET);
  86536. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  86537. +
  86538. + /*
  86539. + * Halt the channel and re-start the transfer so the PING
  86540. + * protocol will start.
  86541. + */
  86542. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  86543. +
  86544. +handle_nyet_done:
  86545. + disable_hc_int(hc_regs, nyet);
  86546. + return 1;
  86547. +}
  86548. +
  86549. +/**
  86550. + * Handles a host channel babble interrupt. This handler may be called in
  86551. + * either DMA mode or Slave mode.
  86552. + */
  86553. +static int32_t handle_hc_babble_intr(dwc_otg_hcd_t * hcd,
  86554. + dwc_hc_t * hc,
  86555. + dwc_otg_hc_regs_t * hc_regs,
  86556. + dwc_otg_qtd_t * qtd)
  86557. +{
  86558. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  86559. + "Babble Error--\n", hc->hc_num);
  86560. +
  86561. + if (hcd->core_if->dma_desc_enable) {
  86562. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  86563. + DWC_OTG_HC_XFER_BABBLE_ERR);
  86564. + goto handle_babble_done;
  86565. + }
  86566. +
  86567. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  86568. + hcd->fops->complete(hcd, qtd->urb->priv,
  86569. + qtd->urb, -DWC_E_OVERFLOW);
  86570. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_BABBLE_ERR);
  86571. + } else {
  86572. + dwc_otg_halt_status_e halt_status;
  86573. + halt_status = update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  86574. + DWC_OTG_HC_XFER_BABBLE_ERR);
  86575. + halt_channel(hcd, hc, qtd, halt_status);
  86576. + }
  86577. +
  86578. +handle_babble_done:
  86579. + disable_hc_int(hc_regs, bblerr);
  86580. + return 1;
  86581. +}
  86582. +
  86583. +/**
  86584. + * Handles a host channel AHB error interrupt. This handler is only called in
  86585. + * DMA mode.
  86586. + */
  86587. +static int32_t handle_hc_ahberr_intr(dwc_otg_hcd_t * hcd,
  86588. + dwc_hc_t * hc,
  86589. + dwc_otg_hc_regs_t * hc_regs,
  86590. + dwc_otg_qtd_t * qtd)
  86591. +{
  86592. + hcchar_data_t hcchar;
  86593. + hcsplt_data_t hcsplt;
  86594. + hctsiz_data_t hctsiz;
  86595. + uint32_t hcdma;
  86596. + char *pipetype, *speed;
  86597. +
  86598. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  86599. +
  86600. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  86601. + "AHB Error--\n", hc->hc_num);
  86602. +
  86603. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  86604. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  86605. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  86606. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  86607. +
  86608. + DWC_ERROR("AHB ERROR, Channel %d\n", hc->hc_num);
  86609. + DWC_ERROR(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32, hcsplt.d32);
  86610. + DWC_ERROR(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32, hcdma);
  86611. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Enqueue\n");
  86612. + DWC_ERROR(" Device address: %d\n",
  86613. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  86614. + DWC_ERROR(" Endpoint: %d, %s\n",
  86615. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  86616. + (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT"));
  86617. +
  86618. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  86619. + case UE_CONTROL:
  86620. + pipetype = "CONTROL";
  86621. + break;
  86622. + case UE_BULK:
  86623. + pipetype = "BULK";
  86624. + break;
  86625. + case UE_INTERRUPT:
  86626. + pipetype = "INTERRUPT";
  86627. + break;
  86628. + case UE_ISOCHRONOUS:
  86629. + pipetype = "ISOCHRONOUS";
  86630. + break;
  86631. + default:
  86632. + pipetype = "UNKNOWN";
  86633. + break;
  86634. + }
  86635. +
  86636. + DWC_ERROR(" Endpoint type: %s\n", pipetype);
  86637. +
  86638. + switch (hc->speed) {
  86639. + case DWC_OTG_EP_SPEED_HIGH:
  86640. + speed = "HIGH";
  86641. + break;
  86642. + case DWC_OTG_EP_SPEED_FULL:
  86643. + speed = "FULL";
  86644. + break;
  86645. + case DWC_OTG_EP_SPEED_LOW:
  86646. + speed = "LOW";
  86647. + break;
  86648. + default:
  86649. + speed = "UNKNOWN";
  86650. + break;
  86651. + };
  86652. +
  86653. + DWC_ERROR(" Speed: %s\n", speed);
  86654. +
  86655. + DWC_ERROR(" Max packet size: %d\n",
  86656. + dwc_otg_hcd_get_mps(&urb->pipe_info));
  86657. + DWC_ERROR(" Data buffer length: %d\n", urb->length);
  86658. + DWC_ERROR(" Transfer buffer: %p, Transfer DMA: %p\n",
  86659. + urb->buf, (void *)urb->dma);
  86660. + DWC_ERROR(" Setup buffer: %p, Setup DMA: %p\n",
  86661. + urb->setup_packet, (void *)urb->setup_dma);
  86662. + DWC_ERROR(" Interval: %d\n", urb->interval);
  86663. +
  86664. + /* Core haltes the channel for Descriptor DMA mode */
  86665. + if (hcd->core_if->dma_desc_enable) {
  86666. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  86667. + DWC_OTG_HC_XFER_AHB_ERR);
  86668. + goto handle_ahberr_done;
  86669. + }
  86670. +
  86671. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_IO);
  86672. +
  86673. + /*
  86674. + * Force a channel halt. Don't call halt_channel because that won't
  86675. + * write to the HCCHARn register in DMA mode to force the halt.
  86676. + */
  86677. + dwc_otg_hc_halt(hcd->core_if, hc, DWC_OTG_HC_XFER_AHB_ERR);
  86678. +handle_ahberr_done:
  86679. + disable_hc_int(hc_regs, ahberr);
  86680. + return 1;
  86681. +}
  86682. +
  86683. +/**
  86684. + * Handles a host channel transaction error interrupt. This handler may be
  86685. + * called in either DMA mode or Slave mode.
  86686. + */
  86687. +static int32_t handle_hc_xacterr_intr(dwc_otg_hcd_t * hcd,
  86688. + dwc_hc_t * hc,
  86689. + dwc_otg_hc_regs_t * hc_regs,
  86690. + dwc_otg_qtd_t * qtd)
  86691. +{
  86692. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  86693. + "Transaction Error--\n", hc->hc_num);
  86694. +
  86695. + if (hcd->core_if->dma_desc_enable) {
  86696. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  86697. + DWC_OTG_HC_XFER_XACT_ERR);
  86698. + goto handle_xacterr_done;
  86699. + }
  86700. +
  86701. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  86702. + case UE_CONTROL:
  86703. + case UE_BULK:
  86704. + qtd->error_count++;
  86705. + if (!hc->qh->ping_state) {
  86706. +
  86707. + update_urb_state_xfer_intr(hc, hc_regs,
  86708. + qtd->urb, qtd,
  86709. + DWC_OTG_HC_XFER_XACT_ERR);
  86710. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  86711. + if (!hc->ep_is_in && hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  86712. + hc->qh->ping_state = 1;
  86713. + }
  86714. + }
  86715. +
  86716. + /*
  86717. + * Halt the channel so the transfer can be re-started from
  86718. + * the appropriate point or the PING protocol will start.
  86719. + */
  86720. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  86721. + break;
  86722. + case UE_INTERRUPT:
  86723. + qtd->error_count++;
  86724. + if (hc->do_split && hc->complete_split) {
  86725. + qtd->complete_split = 0;
  86726. + }
  86727. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  86728. + break;
  86729. + case UE_ISOCHRONOUS:
  86730. + {
  86731. + dwc_otg_halt_status_e halt_status;
  86732. + halt_status =
  86733. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  86734. + DWC_OTG_HC_XFER_XACT_ERR);
  86735. +
  86736. + halt_channel(hcd, hc, qtd, halt_status);
  86737. + }
  86738. + break;
  86739. + }
  86740. +handle_xacterr_done:
  86741. + disable_hc_int(hc_regs, xacterr);
  86742. +
  86743. + return 1;
  86744. +}
  86745. +
  86746. +/**
  86747. + * Handles a host channel frame overrun interrupt. This handler may be called
  86748. + * in either DMA mode or Slave mode.
  86749. + */
  86750. +static int32_t handle_hc_frmovrun_intr(dwc_otg_hcd_t * hcd,
  86751. + dwc_hc_t * hc,
  86752. + dwc_otg_hc_regs_t * hc_regs,
  86753. + dwc_otg_qtd_t * qtd)
  86754. +{
  86755. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  86756. + "Frame Overrun--\n", hc->hc_num);
  86757. +
  86758. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  86759. + case UE_CONTROL:
  86760. + case UE_BULK:
  86761. + break;
  86762. + case UE_INTERRUPT:
  86763. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_FRAME_OVERRUN);
  86764. + break;
  86765. + case UE_ISOCHRONOUS:
  86766. + {
  86767. + dwc_otg_halt_status_e halt_status;
  86768. + halt_status =
  86769. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  86770. + DWC_OTG_HC_XFER_FRAME_OVERRUN);
  86771. +
  86772. + halt_channel(hcd, hc, qtd, halt_status);
  86773. + }
  86774. + break;
  86775. + }
  86776. +
  86777. + disable_hc_int(hc_regs, frmovrun);
  86778. +
  86779. + return 1;
  86780. +}
  86781. +
  86782. +/**
  86783. + * Handles a host channel data toggle error interrupt. This handler may be
  86784. + * called in either DMA mode or Slave mode.
  86785. + */
  86786. +static int32_t handle_hc_datatglerr_intr(dwc_otg_hcd_t * hcd,
  86787. + dwc_hc_t * hc,
  86788. + dwc_otg_hc_regs_t * hc_regs,
  86789. + dwc_otg_qtd_t * qtd)
  86790. +{
  86791. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  86792. + "Data Toggle Error on %s transfer--\n",
  86793. + hc->hc_num, (hc->ep_is_in ? "IN" : "OUT"));
  86794. +
  86795. + /* Data toggles on split transactions cause the hc to halt.
  86796. + * restart transfer */
  86797. + if(hc->qh->do_split)
  86798. + {
  86799. + qtd->error_count++;
  86800. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  86801. + update_urb_state_xfer_intr(hc, hc_regs,
  86802. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  86803. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  86804. + } else if (hc->ep_is_in) {
  86805. + /* An unmasked data toggle error on a non-split DMA transaction is
  86806. + * for the sole purpose of resetting error counts. Disable other
  86807. + * interrupts unmasked for the same reason.
  86808. + */
  86809. + if(hcd->core_if->dma_enable) {
  86810. + disable_hc_int(hc_regs, ack);
  86811. + disable_hc_int(hc_regs, nak);
  86812. + }
  86813. + qtd->error_count = 0;
  86814. + }
  86815. +
  86816. + disable_hc_int(hc_regs, datatglerr);
  86817. +
  86818. + return 1;
  86819. +}
  86820. +
  86821. +#ifdef DEBUG
  86822. +/**
  86823. + * This function is for debug only. It checks that a valid halt status is set
  86824. + * and that HCCHARn.chdis is clear. If there's a problem, corrective action is
  86825. + * taken and a warning is issued.
  86826. + * @return 1 if halt status is ok, 0 otherwise.
  86827. + */
  86828. +static inline int halt_status_ok(dwc_otg_hcd_t * hcd,
  86829. + dwc_hc_t * hc,
  86830. + dwc_otg_hc_regs_t * hc_regs,
  86831. + dwc_otg_qtd_t * qtd)
  86832. +{
  86833. + hcchar_data_t hcchar;
  86834. + hctsiz_data_t hctsiz;
  86835. + hcint_data_t hcint;
  86836. + hcintmsk_data_t hcintmsk;
  86837. + hcsplt_data_t hcsplt;
  86838. +
  86839. + if (hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS) {
  86840. + /*
  86841. + * This code is here only as a check. This condition should
  86842. + * never happen. Ignore the halt if it does occur.
  86843. + */
  86844. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  86845. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  86846. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  86847. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  86848. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  86849. + DWC_WARN
  86850. + ("%s: hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS, "
  86851. + "channel %d, hcchar 0x%08x, hctsiz 0x%08x, "
  86852. + "hcint 0x%08x, hcintmsk 0x%08x, "
  86853. + "hcsplt 0x%08x, qtd->complete_split %d\n", __func__,
  86854. + hc->hc_num, hcchar.d32, hctsiz.d32, hcint.d32,
  86855. + hcintmsk.d32, hcsplt.d32, qtd->complete_split);
  86856. +
  86857. + DWC_WARN("%s: no halt status, channel %d, ignoring interrupt\n",
  86858. + __func__, hc->hc_num);
  86859. + DWC_WARN("\n");
  86860. + clear_hc_int(hc_regs, chhltd);
  86861. + return 0;
  86862. + }
  86863. +
  86864. + /*
  86865. + * This code is here only as a check. hcchar.chdis should
  86866. + * never be set when the halt interrupt occurs. Halt the
  86867. + * channel again if it does occur.
  86868. + */
  86869. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  86870. + if (hcchar.b.chdis) {
  86871. + DWC_WARN("%s: hcchar.chdis set unexpectedly, "
  86872. + "hcchar 0x%08x, trying to halt again\n",
  86873. + __func__, hcchar.d32);
  86874. + clear_hc_int(hc_regs, chhltd);
  86875. + hc->halt_pending = 0;
  86876. + halt_channel(hcd, hc, qtd, hc->halt_status);
  86877. + return 0;
  86878. + }
  86879. +
  86880. + return 1;
  86881. +}
  86882. +#endif
  86883. +
  86884. +/**
  86885. + * Handles a host Channel Halted interrupt in DMA mode. This handler
  86886. + * determines the reason the channel halted and proceeds accordingly.
  86887. + */
  86888. +static void handle_hc_chhltd_intr_dma(dwc_otg_hcd_t * hcd,
  86889. + dwc_hc_t * hc,
  86890. + dwc_otg_hc_regs_t * hc_regs,
  86891. + dwc_otg_qtd_t * qtd)
  86892. +{
  86893. + int out_nak_enh = 0;
  86894. + hcint_data_t hcint;
  86895. + hcintmsk_data_t hcintmsk;
  86896. + /* For core with OUT NAK enhancement, the flow for high-
  86897. + * speed CONTROL/BULK OUT is handled a little differently.
  86898. + */
  86899. + if (hcd->core_if->snpsid >= OTG_CORE_REV_2_71a) {
  86900. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH && !hc->ep_is_in &&
  86901. + (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  86902. + hc->ep_type == DWC_OTG_EP_TYPE_BULK)) {
  86903. + out_nak_enh = 1;
  86904. + }
  86905. + }
  86906. +
  86907. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  86908. + (hc->halt_status == DWC_OTG_HC_XFER_AHB_ERR
  86909. + && !hcd->core_if->dma_desc_enable)) {
  86910. + /*
  86911. + * Just release the channel. A dequeue can happen on a
  86912. + * transfer timeout. In the case of an AHB Error, the channel
  86913. + * was forced to halt because there's no way to gracefully
  86914. + * recover.
  86915. + */
  86916. + if (hcd->core_if->dma_desc_enable)
  86917. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  86918. + hc->halt_status);
  86919. + else
  86920. + release_channel(hcd, hc, qtd, hc->halt_status);
  86921. + return;
  86922. + }
  86923. +
  86924. + /* Read the HCINTn register to determine the cause for the halt. */
  86925. +
  86926. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  86927. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  86928. +
  86929. + if (hcint.b.xfercomp) {
  86930. + /** @todo This is here because of a possible hardware bug. Spec
  86931. + * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
  86932. + * interrupt w/ACK bit set should occur, but I only see the
  86933. + * XFERCOMP bit, even with it masked out. This is a workaround
  86934. + * for that behavior. Should fix this when hardware is fixed.
  86935. + */
  86936. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  86937. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  86938. + }
  86939. + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
  86940. + } else if (hcint.b.stall) {
  86941. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  86942. + } else if (hcint.b.xacterr && !hcd->core_if->dma_desc_enable) {
  86943. + if (out_nak_enh) {
  86944. + if (hcint.b.nyet || hcint.b.nak || hcint.b.ack) {
  86945. + DWC_DEBUGPL(DBG_HCD, "XactErr with NYET/NAK/ACK\n");
  86946. + qtd->error_count = 0;
  86947. + } else {
  86948. + DWC_DEBUGPL(DBG_HCD, "XactErr without NYET/NAK/ACK\n");
  86949. + }
  86950. + }
  86951. +
  86952. + /*
  86953. + * Must handle xacterr before nak or ack. Could get a xacterr
  86954. + * at the same time as either of these on a BULK/CONTROL OUT
  86955. + * that started with a PING. The xacterr takes precedence.
  86956. + */
  86957. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  86958. + } else if (hcint.b.xcs_xact && hcd->core_if->dma_desc_enable) {
  86959. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  86960. + } else if (hcint.b.ahberr && hcd->core_if->dma_desc_enable) {
  86961. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  86962. + } else if (hcint.b.bblerr) {
  86963. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  86964. + } else if (hcint.b.frmovrun) {
  86965. + handle_hc_frmovrun_intr(hcd, hc, hc_regs, qtd);
  86966. + } else if (hcint.b.datatglerr) {
  86967. + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
  86968. + } else if (!out_nak_enh) {
  86969. + if (hcint.b.nyet) {
  86970. + /*
  86971. + * Must handle nyet before nak or ack. Could get a nyet at the
  86972. + * same time as either of those on a BULK/CONTROL OUT that
  86973. + * started with a PING. The nyet takes precedence.
  86974. + */
  86975. + handle_hc_nyet_intr(hcd, hc, hc_regs, qtd);
  86976. + } else if (hcint.b.nak && !hcintmsk.b.nak) {
  86977. + /*
  86978. + * If nak is not masked, it's because a non-split IN transfer
  86979. + * is in an error state. In that case, the nak is handled by
  86980. + * the nak interrupt handler, not here. Handle nak here for
  86981. + * BULK/CONTROL OUT transfers, which halt on a NAK to allow
  86982. + * rewinding the buffer pointer.
  86983. + */
  86984. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  86985. + } else if (hcint.b.ack && !hcintmsk.b.ack) {
  86986. + /*
  86987. + * If ack is not masked, it's because a non-split IN transfer
  86988. + * is in an error state. In that case, the ack is handled by
  86989. + * the ack interrupt handler, not here. Handle ack here for
  86990. + * split transfers. Start splits halt on ACK.
  86991. + */
  86992. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  86993. + } else {
  86994. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  86995. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  86996. + /*
  86997. + * A periodic transfer halted with no other channel
  86998. + * interrupts set. Assume it was halted by the core
  86999. + * because it could not be completed in its scheduled
  87000. + * (micro)frame.
  87001. + */
  87002. +#ifdef DEBUG
  87003. + DWC_PRINTF
  87004. + ("%s: Halt channel %d (assume incomplete periodic transfer)\n",
  87005. + __func__, hc->hc_num);
  87006. +#endif
  87007. + halt_channel(hcd, hc, qtd,
  87008. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE);
  87009. + } else {
  87010. + DWC_ERROR
  87011. + ("%s: Channel %d, DMA Mode -- ChHltd set, but reason "
  87012. + "for halting is unknown, hcint 0x%08x, intsts 0x%08x\n",
  87013. + __func__, hc->hc_num, hcint.d32,
  87014. + DWC_READ_REG32(&hcd->
  87015. + core_if->core_global_regs->
  87016. + gintsts));
  87017. + /* Failthrough: use 3-strikes rule */
  87018. + qtd->error_count++;
  87019. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  87020. + update_urb_state_xfer_intr(hc, hc_regs,
  87021. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  87022. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  87023. + }
  87024. +
  87025. + }
  87026. + } else {
  87027. + DWC_PRINTF("NYET/NAK/ACK/other in non-error case, 0x%08x\n",
  87028. + hcint.d32);
  87029. + /* Failthrough: use 3-strikes rule */
  87030. + qtd->error_count++;
  87031. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  87032. + update_urb_state_xfer_intr(hc, hc_regs,
  87033. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  87034. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  87035. + }
  87036. +}
  87037. +
  87038. +/**
  87039. + * Handles a host channel Channel Halted interrupt.
  87040. + *
  87041. + * In slave mode, this handler is called only when the driver specifically
  87042. + * requests a halt. This occurs during handling other host channel interrupts
  87043. + * (e.g. nak, xacterr, stall, nyet, etc.).
  87044. + *
  87045. + * In DMA mode, this is the interrupt that occurs when the core has finished
  87046. + * processing a transfer on a channel. Other host channel interrupts (except
  87047. + * ahberr) are disabled in DMA mode.
  87048. + */
  87049. +static int32_t handle_hc_chhltd_intr(dwc_otg_hcd_t * hcd,
  87050. + dwc_hc_t * hc,
  87051. + dwc_otg_hc_regs_t * hc_regs,
  87052. + dwc_otg_qtd_t * qtd)
  87053. +{
  87054. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  87055. + "Channel Halted--\n", hc->hc_num);
  87056. +
  87057. + if (hcd->core_if->dma_enable) {
  87058. + handle_hc_chhltd_intr_dma(hcd, hc, hc_regs, qtd);
  87059. + } else {
  87060. +#ifdef DEBUG
  87061. + if (!halt_status_ok(hcd, hc, hc_regs, qtd)) {
  87062. + return 1;
  87063. + }
  87064. +#endif
  87065. + release_channel(hcd, hc, qtd, hc->halt_status);
  87066. + }
  87067. +
  87068. + return 1;
  87069. +}
  87070. +
  87071. +
  87072. +/**
  87073. + * dwc_otg_fiq_unmangle_isoc() - Update the iso_frame_desc structure on
  87074. + * FIQ transfer completion
  87075. + * @hcd: Pointer to dwc_otg_hcd struct
  87076. + * @num: Host channel number
  87077. + *
  87078. + * 1. Un-mangle the status as recorded in each iso_frame_desc status
  87079. + * 2. Copy it from the dwc_otg_urb into the real URB
  87080. + */
  87081. +void dwc_otg_fiq_unmangle_isoc(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh, dwc_otg_qtd_t *qtd, uint32_t num)
  87082. +{
  87083. + struct dwc_otg_hcd_urb *dwc_urb = qtd->urb;
  87084. + int nr_frames = dwc_urb->packet_count;
  87085. + int i;
  87086. + hcint_data_t frame_hcint;
  87087. +
  87088. + for (i = 0; i < nr_frames; i++) {
  87089. + frame_hcint.d32 = dwc_urb->iso_descs[i].status;
  87090. + if (frame_hcint.b.xfercomp) {
  87091. + dwc_urb->iso_descs[i].status = 0;
  87092. + dwc_urb->actual_length += dwc_urb->iso_descs[i].actual_length;
  87093. + } else if (frame_hcint.b.frmovrun) {
  87094. + if (qh->ep_is_in)
  87095. + dwc_urb->iso_descs[i].status = -DWC_E_NO_STREAM_RES;
  87096. + else
  87097. + dwc_urb->iso_descs[i].status = -DWC_E_COMMUNICATION;
  87098. + dwc_urb->error_count++;
  87099. + dwc_urb->iso_descs[i].actual_length = 0;
  87100. + } else if (frame_hcint.b.xacterr) {
  87101. + dwc_urb->iso_descs[i].status = -DWC_E_PROTOCOL;
  87102. + dwc_urb->error_count++;
  87103. + dwc_urb->iso_descs[i].actual_length = 0;
  87104. + } else if (frame_hcint.b.bblerr) {
  87105. + dwc_urb->iso_descs[i].status = -DWC_E_OVERFLOW;
  87106. + dwc_urb->error_count++;
  87107. + dwc_urb->iso_descs[i].actual_length = 0;
  87108. + } else {
  87109. + /* Something went wrong */
  87110. + dwc_urb->iso_descs[i].status = -1;
  87111. + dwc_urb->iso_descs[i].actual_length = 0;
  87112. + dwc_urb->error_count++;
  87113. + }
  87114. + }
  87115. + //printk_ratelimited(KERN_INFO "%s: HS isochronous of %d/%d frames with %d errors complete\n",
  87116. + // __FUNCTION__, i, dwc_urb->packet_count, dwc_urb->error_count);
  87117. + hcd->fops->complete(hcd, dwc_urb->priv, dwc_urb, 0);
  87118. + release_channel(hcd, qh->channel, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  87119. +}
  87120. +
  87121. +/**
  87122. + * dwc_otg_fiq_unsetup_per_dma() - Remove data from bounce buffers for split transactions
  87123. + * @hcd: Pointer to dwc_otg_hcd struct
  87124. + * @num: Host channel number
  87125. + *
  87126. + * Copies data from the FIQ bounce buffers into the URB's transfer buffer. Does not modify URB state.
  87127. + * Returns total length of data or -1 if the buffers were not used.
  87128. + *
  87129. + */
  87130. +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)
  87131. +{
  87132. + dwc_hc_t *hc = qh->channel;
  87133. + struct fiq_dma_blob *blob = hcd->fiq_dmab;
  87134. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  87135. + uint8_t *ptr = NULL;
  87136. + int index = 0, len = 0;
  87137. + int i = 0;
  87138. + if (hc->ep_is_in) {
  87139. + /* Copy data out of the DMA bounce buffers to the URB's buffer.
  87140. + * The align_buf is ignored as this is ignored on FSM enqueue. */
  87141. + ptr = qtd->urb->buf;
  87142. + if (qh->ep_type == UE_ISOCHRONOUS) {
  87143. + /* Isoc IN transactions - grab the offset of the iso_frame_desc into the URB transfer buffer */
  87144. + index = qtd->isoc_frame_index;
  87145. + ptr += qtd->urb->iso_descs[index].offset;
  87146. + } else {
  87147. + /* Need to increment by actual_length for interrupt IN */
  87148. + ptr += qtd->urb->actual_length;
  87149. + }
  87150. +
  87151. + for (i = 0; i < st->dma_info.index; i++) {
  87152. + len += st->dma_info.slot_len[i];
  87153. + dwc_memcpy(ptr, &blob->channel[num].index[i].buf[0], st->dma_info.slot_len[i]);
  87154. + ptr += st->dma_info.slot_len[i];
  87155. + }
  87156. + return len;
  87157. + } else {
  87158. + /* OUT endpoints - nothing to do. */
  87159. + return -1;
  87160. + }
  87161. +
  87162. +}
  87163. +/**
  87164. + * dwc_otg_hcd_handle_hc_fsm() - handle an unmasked channel interrupt
  87165. + * from a channel handled in the FIQ
  87166. + * @hcd: Pointer to dwc_otg_hcd struct
  87167. + * @num: Host channel number
  87168. + *
  87169. + * If a host channel interrupt was received by the IRQ and this was a channel
  87170. + * used by the FIQ, the execution flow for transfer completion is substantially
  87171. + * different from the normal (messy) path. This function and its friends handles
  87172. + * channel cleanup and transaction completion from a FIQ transaction.
  87173. + */
  87174. +int32_t dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd_t *hcd, uint32_t num)
  87175. +{
  87176. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  87177. + dwc_hc_t *hc = hcd->hc_ptr_array[num];
  87178. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
  87179. + dwc_otg_qh_t *qh = hc->qh;
  87180. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[num];
  87181. + hcint_data_t hcint = hcd->fiq_state->channel[num].hcint_copy;
  87182. + int hostchannels = 0;
  87183. + int ret = 0;
  87184. + fiq_print(FIQDBG_INT, hcd->fiq_state, "OUT %01d %01d ", num , st->fsm);
  87185. +
  87186. + hostchannels = hcd->available_host_channels;
  87187. + switch (st->fsm) {
  87188. + case FIQ_TEST:
  87189. + break;
  87190. +
  87191. + case FIQ_DEQUEUE_ISSUED:
  87192. + /* hc_halt was called. QTD no longer exists. */
  87193. + /* TODO: for a nonperiodic split transaction, need to issue a
  87194. + * CLEAR_TT_BUFFER hub command if we were in the start-split phase.
  87195. + */
  87196. + release_channel(hcd, hc, NULL, hc->halt_status);
  87197. + ret = 1;
  87198. + break;
  87199. +
  87200. + case FIQ_NP_SPLIT_DONE:
  87201. + /* Nonperiodic transaction complete. */
  87202. + if (!hc->ep_is_in) {
  87203. + qtd->ssplit_out_xfer_count = hc->xfer_len;
  87204. + }
  87205. + if (hcint.b.xfercomp) {
  87206. + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
  87207. + } else if (hcint.b.nak) {
  87208. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  87209. + }
  87210. + ret = 1;
  87211. + break;
  87212. +
  87213. + case FIQ_NP_SPLIT_HS_ABORTED:
  87214. + /* A HS abort is a 3-strikes on the HS bus at any point in the transaction.
  87215. + * Normally a CLEAR_TT_BUFFER hub command would be required: we can't do that
  87216. + * because there's no guarantee which order a non-periodic split happened in.
  87217. + * We could end up clearing a perfectly good transaction out of the buffer.
  87218. + */
  87219. + if (hcint.b.xacterr) {
  87220. + qtd->error_count += st->nr_errors;
  87221. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  87222. + } else if (hcint.b.ahberr) {
  87223. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  87224. + } else {
  87225. + local_fiq_disable();
  87226. + BUG();
  87227. + }
  87228. + break;
  87229. +
  87230. + case FIQ_NP_SPLIT_LS_ABORTED:
  87231. + /* A few cases can cause this - either an unknown state on a SSPLIT or
  87232. + * STALL/data toggle error response on a CSPLIT */
  87233. + if (hcint.b.stall) {
  87234. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  87235. + } else if (hcint.b.datatglerr) {
  87236. + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
  87237. + } else if (hcint.b.bblerr) {
  87238. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  87239. + } else if (hcint.b.ahberr) {
  87240. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  87241. + } else {
  87242. + local_fiq_disable();
  87243. + BUG();
  87244. + }
  87245. + break;
  87246. +
  87247. + case FIQ_PER_SPLIT_DONE:
  87248. + /* Isoc IN or Interrupt IN/OUT */
  87249. +
  87250. + /* Flow control here is different from the normal execution by the driver.
  87251. + * We need to completely ignore most of the driver's method of handling
  87252. + * split transactions and do it ourselves.
  87253. + */
  87254. + if (hc->ep_type == UE_INTERRUPT) {
  87255. + if (hcint.b.nak) {
  87256. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  87257. + } else if (hc->ep_is_in) {
  87258. + int len;
  87259. + len = dwc_otg_fiq_unsetup_per_dma(hcd, hc->qh, qtd, num);
  87260. + //printk(KERN_NOTICE "FIQ Transaction: hc=%d len=%d urb_len = %d\n", num, len, qtd->urb->length);
  87261. + qtd->urb->actual_length += len;
  87262. + if (qtd->urb->actual_length >= qtd->urb->length) {
  87263. + qtd->urb->status = 0;
  87264. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  87265. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  87266. + } else {
  87267. + /* Interrupt transfer not complete yet - is it a short read? */
  87268. + if (len < hc->max_packet) {
  87269. + /* Interrupt transaction complete */
  87270. + qtd->urb->status = 0;
  87271. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  87272. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  87273. + } else {
  87274. + /* Further transactions required */
  87275. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  87276. + }
  87277. + }
  87278. + } else {
  87279. + /* Interrupt OUT complete. */
  87280. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  87281. + qtd->urb->actual_length += hc->xfer_len;
  87282. + if (qtd->urb->actual_length >= qtd->urb->length) {
  87283. + qtd->urb->status = 0;
  87284. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  87285. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  87286. + } else {
  87287. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  87288. + }
  87289. + }
  87290. + } else {
  87291. + /* ISOC IN complete. */
  87292. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  87293. + int len = 0;
  87294. + /* Record errors, update qtd. */
  87295. + if (st->nr_errors) {
  87296. + frame_desc->actual_length = 0;
  87297. + frame_desc->status = -DWC_E_PROTOCOL;
  87298. + } else {
  87299. + frame_desc->status = 0;
  87300. + /* Unswizzle dma */
  87301. + len = dwc_otg_fiq_unsetup_per_dma(hcd, qh, qtd, num);
  87302. + frame_desc->actual_length = len;
  87303. + }
  87304. + qtd->isoc_frame_index++;
  87305. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  87306. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  87307. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  87308. + } else {
  87309. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  87310. + }
  87311. + }
  87312. + break;
  87313. +
  87314. + case FIQ_PER_ISO_OUT_DONE: {
  87315. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  87316. + /* Record errors, update qtd. */
  87317. + if (st->nr_errors) {
  87318. + frame_desc->actual_length = 0;
  87319. + frame_desc->status = -DWC_E_PROTOCOL;
  87320. + } else {
  87321. + frame_desc->status = 0;
  87322. + frame_desc->actual_length = frame_desc->length;
  87323. + }
  87324. + qtd->isoc_frame_index++;
  87325. + qtd->isoc_split_offset = 0;
  87326. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  87327. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  87328. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  87329. + } else {
  87330. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  87331. + }
  87332. + }
  87333. + break;
  87334. +
  87335. + case FIQ_PER_SPLIT_NYET_ABORTED:
  87336. + /* Doh. lost the data. */
  87337. + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
  87338. + "- FIQ reported NYET. Data may have been lost.\n",
  87339. + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
  87340. + if (hc->ep_type == UE_ISOCHRONOUS) {
  87341. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  87342. + /* Record errors, update qtd. */
  87343. + frame_desc->actual_length = 0;
  87344. + frame_desc->status = -DWC_E_PROTOCOL;
  87345. + qtd->isoc_frame_index++;
  87346. + qtd->isoc_split_offset = 0;
  87347. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  87348. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  87349. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  87350. + } else {
  87351. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  87352. + }
  87353. + } else {
  87354. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  87355. + }
  87356. + break;
  87357. +
  87358. + case FIQ_HS_ISOC_DONE:
  87359. + /* The FIQ has performed a whole pile of isochronous transactions.
  87360. + * The status is recorded as the interrupt state should the transaction
  87361. + * fail.
  87362. + */
  87363. + dwc_otg_fiq_unmangle_isoc(hcd, qh, qtd, num);
  87364. + break;
  87365. +
  87366. + case FIQ_PER_SPLIT_LS_ABORTED:
  87367. + if (hcint.b.xacterr) {
  87368. + /* Hub has responded with an ERR packet. Device
  87369. + * has been unplugged or the port has been disabled.
  87370. + * TODO: need to issue a reset to the hub port. */
  87371. + qtd->error_count += 3;
  87372. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  87373. + } else if (hcint.b.stall) {
  87374. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  87375. + } else if (hcint.b.bblerr) {
  87376. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  87377. + } else {
  87378. + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x failed "
  87379. + "- FIQ reported FSM=%d. Data may have been lost.\n",
  87380. + st->fsm, hc->dev_addr, hc->ep_num);
  87381. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  87382. + }
  87383. + break;
  87384. +
  87385. + case FIQ_PER_SPLIT_HS_ABORTED:
  87386. + /* Either the SSPLIT phase suffered transaction errors or something
  87387. + * unexpected happened.
  87388. + */
  87389. + qtd->error_count += 3;
  87390. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  87391. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  87392. + break;
  87393. +
  87394. + case FIQ_PER_SPLIT_TIMEOUT:
  87395. + /* Couldn't complete in the nominated frame */
  87396. + printk(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
  87397. + "- FIQ timed out. Data may have been lost.\n",
  87398. + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
  87399. + if (hc->ep_type == UE_ISOCHRONOUS) {
  87400. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  87401. + /* Record errors, update qtd. */
  87402. + frame_desc->actual_length = 0;
  87403. + if (hc->ep_is_in) {
  87404. + frame_desc->status = -DWC_E_NO_STREAM_RES;
  87405. + } else {
  87406. + frame_desc->status = -DWC_E_COMMUNICATION;
  87407. + }
  87408. + qtd->isoc_frame_index++;
  87409. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  87410. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  87411. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  87412. + } else {
  87413. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  87414. + }
  87415. + } else {
  87416. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  87417. + }
  87418. + break;
  87419. +
  87420. + default:
  87421. + local_fiq_disable();
  87422. + DWC_WARN("unexpected state received on hc=%d fsm=%d", hc->hc_num, st->fsm);
  87423. + BUG();
  87424. + }
  87425. + //if (hostchannels != hcd->available_host_channels) {
  87426. + /* should have incremented by now! */
  87427. + // BUG();
  87428. +// }
  87429. + return ret;
  87430. +}
  87431. +
  87432. +/** Handles interrupt for a specific Host Channel */
  87433. +int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd, uint32_t num)
  87434. +{
  87435. + int retval = 0;
  87436. + hcint_data_t hcint;
  87437. + hcintmsk_data_t hcintmsk;
  87438. + dwc_hc_t *hc;
  87439. + dwc_otg_hc_regs_t *hc_regs;
  87440. + dwc_otg_qtd_t *qtd;
  87441. +
  87442. + DWC_DEBUGPL(DBG_HCDV, "--Host Channel Interrupt--, Channel %d\n", num);
  87443. +
  87444. + hc = dwc_otg_hcd->hc_ptr_array[num];
  87445. + hc_regs = dwc_otg_hcd->core_if->host_if->hc_regs[num];
  87446. + if(hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  87447. + /* We are responding to a channel disable. Driver
  87448. + * state is cleared - our qtd has gone away.
  87449. + */
  87450. + release_channel(dwc_otg_hcd, hc, NULL, hc->halt_status);
  87451. + return 1;
  87452. + }
  87453. + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
  87454. +
  87455. + /*
  87456. + * FSM mode: Check to see if this is a HC interrupt from a channel handled by the FIQ.
  87457. + * Execution path is fundamentally different for the channels after a FIQ has completed
  87458. + * a split transaction.
  87459. + */
  87460. + if (fiq_fsm_enable) {
  87461. + switch (dwc_otg_hcd->fiq_state->channel[num].fsm) {
  87462. + case FIQ_PASSTHROUGH:
  87463. + break;
  87464. + case FIQ_PASSTHROUGH_ERRORSTATE:
  87465. + /* Hook into the error count */
  87466. + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "HCDERR%02d", num);
  87467. + if (!dwc_otg_hcd->fiq_state->channel[num].nr_errors) {
  87468. + qtd->error_count = 0;
  87469. + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "RESET ");
  87470. + }
  87471. + break;
  87472. + default:
  87473. + dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd, num);
  87474. + return 1;
  87475. + }
  87476. + }
  87477. +
  87478. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  87479. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  87480. + hcint.d32 = hcint.d32 & hcintmsk.d32;
  87481. + if (!dwc_otg_hcd->core_if->dma_enable) {
  87482. + if (hcint.b.chhltd && hcint.d32 != 0x2) {
  87483. + hcint.b.chhltd = 0;
  87484. + }
  87485. + }
  87486. +
  87487. + if (hcint.b.xfercomp) {
  87488. + retval |=
  87489. + handle_hc_xfercomp_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  87490. + /*
  87491. + * If NYET occurred at same time as Xfer Complete, the NYET is
  87492. + * handled by the Xfer Complete interrupt handler. Don't want
  87493. + * to call the NYET interrupt handler in this case.
  87494. + */
  87495. + hcint.b.nyet = 0;
  87496. + }
  87497. + if (hcint.b.chhltd) {
  87498. + retval |= handle_hc_chhltd_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  87499. + }
  87500. + if (hcint.b.ahberr) {
  87501. + retval |= handle_hc_ahberr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  87502. + }
  87503. + if (hcint.b.stall) {
  87504. + retval |= handle_hc_stall_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  87505. + }
  87506. + if (hcint.b.nak) {
  87507. + retval |= handle_hc_nak_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  87508. + }
  87509. + if (hcint.b.ack) {
  87510. + if(!hcint.b.chhltd)
  87511. + retval |= handle_hc_ack_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  87512. + }
  87513. + if (hcint.b.nyet) {
  87514. + retval |= handle_hc_nyet_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  87515. + }
  87516. + if (hcint.b.xacterr) {
  87517. + retval |= handle_hc_xacterr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  87518. + }
  87519. + if (hcint.b.bblerr) {
  87520. + retval |= handle_hc_babble_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  87521. + }
  87522. + if (hcint.b.frmovrun) {
  87523. + retval |=
  87524. + handle_hc_frmovrun_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  87525. + }
  87526. + if (hcint.b.datatglerr) {
  87527. + retval |=
  87528. + handle_hc_datatglerr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  87529. + }
  87530. +
  87531. + return retval;
  87532. +}
  87533. +#endif /* DWC_DEVICE_ONLY */
  87534. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
  87535. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c 1969-12-31 18:00:00.000000000 -0600
  87536. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c 2014-12-03 19:13:40.220418001 -0600
  87537. @@ -0,0 +1,985 @@
  87538. +
  87539. +/* ==========================================================================
  87540. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_linux.c $
  87541. + * $Revision: #20 $
  87542. + * $Date: 2011/10/26 $
  87543. + * $Change: 1872981 $
  87544. + *
  87545. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  87546. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  87547. + * otherwise expressly agreed to in writing between Synopsys and you.
  87548. + *
  87549. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  87550. + * any End User Software License Agreement or Agreement for Licensed Product
  87551. + * with Synopsys or any supplement thereto. You are permitted to use and
  87552. + * redistribute this Software in source and binary forms, with or without
  87553. + * modification, provided that redistributions of source code must retain this
  87554. + * notice. You may not view, use, disclose, copy or distribute this file or
  87555. + * any information contained herein except pursuant to this license grant from
  87556. + * Synopsys. If you do not agree with this notice, including the disclaimer
  87557. + * below, then you are not authorized to use the Software.
  87558. + *
  87559. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  87560. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  87561. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  87562. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  87563. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  87564. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  87565. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  87566. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  87567. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  87568. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  87569. + * DAMAGE.
  87570. + * ========================================================================== */
  87571. +#ifndef DWC_DEVICE_ONLY
  87572. +
  87573. +/**
  87574. + * @file
  87575. + *
  87576. + * This file contains the implementation of the HCD. In Linux, the HCD
  87577. + * implements the hc_driver API.
  87578. + */
  87579. +#include <linux/kernel.h>
  87580. +#include <linux/module.h>
  87581. +#include <linux/moduleparam.h>
  87582. +#include <linux/init.h>
  87583. +#include <linux/device.h>
  87584. +#include <linux/errno.h>
  87585. +#include <linux/list.h>
  87586. +#include <linux/interrupt.h>
  87587. +#include <linux/string.h>
  87588. +#include <linux/dma-mapping.h>
  87589. +#include <linux/version.h>
  87590. +#include <asm/io.h>
  87591. +#include <asm/fiq.h>
  87592. +#include <linux/usb.h>
  87593. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
  87594. +#include <../drivers/usb/core/hcd.h>
  87595. +#else
  87596. +#include <linux/usb/hcd.h>
  87597. +#endif
  87598. +#include <asm/bug.h>
  87599. +
  87600. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  87601. +#define USB_URB_EP_LINKING 1
  87602. +#else
  87603. +#define USB_URB_EP_LINKING 0
  87604. +#endif
  87605. +
  87606. +#include "dwc_otg_hcd_if.h"
  87607. +#include "dwc_otg_dbg.h"
  87608. +#include "dwc_otg_driver.h"
  87609. +#include "dwc_otg_hcd.h"
  87610. +
  87611. +extern unsigned char _dwc_otg_fiq_stub, _dwc_otg_fiq_stub_end;
  87612. +
  87613. +/**
  87614. + * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
  87615. + * qualified with its direction (possible 32 endpoints per device).
  87616. + */
  87617. +#define dwc_ep_addr_to_endpoint(_bEndpointAddress_) ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
  87618. + ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
  87619. +
  87620. +static const char dwc_otg_hcd_name[] = "dwc_otg_hcd";
  87621. +
  87622. +extern bool fiq_enable;
  87623. +
  87624. +/** @name Linux HC Driver API Functions */
  87625. +/** @{ */
  87626. +/* manage i/o requests, device state */
  87627. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  87628. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  87629. + struct usb_host_endpoint *ep,
  87630. +#endif
  87631. + struct urb *urb, gfp_t mem_flags);
  87632. +
  87633. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  87634. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  87635. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
  87636. +#endif
  87637. +#else /* kernels at or post 2.6.30 */
  87638. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd,
  87639. + struct urb *urb, int status);
  87640. +#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30) */
  87641. +
  87642. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  87643. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  87644. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  87645. +#endif
  87646. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd);
  87647. +extern int hcd_start(struct usb_hcd *hcd);
  87648. +extern void hcd_stop(struct usb_hcd *hcd);
  87649. +static int get_frame_number(struct usb_hcd *hcd);
  87650. +extern int hub_status_data(struct usb_hcd *hcd, char *buf);
  87651. +extern int hub_control(struct usb_hcd *hcd,
  87652. + u16 typeReq,
  87653. + u16 wValue, u16 wIndex, char *buf, u16 wLength);
  87654. +
  87655. +struct wrapper_priv_data {
  87656. + dwc_otg_hcd_t *dwc_otg_hcd;
  87657. +};
  87658. +
  87659. +/** @} */
  87660. +
  87661. +static struct hc_driver dwc_otg_hc_driver = {
  87662. +
  87663. + .description = dwc_otg_hcd_name,
  87664. + .product_desc = "DWC OTG Controller",
  87665. + .hcd_priv_size = sizeof(struct wrapper_priv_data),
  87666. +
  87667. + .irq = dwc_otg_hcd_irq,
  87668. +
  87669. + .flags = HCD_MEMORY | HCD_USB2,
  87670. +
  87671. + //.reset =
  87672. + .start = hcd_start,
  87673. + //.suspend =
  87674. + //.resume =
  87675. + .stop = hcd_stop,
  87676. +
  87677. + .urb_enqueue = dwc_otg_urb_enqueue,
  87678. + .urb_dequeue = dwc_otg_urb_dequeue,
  87679. + .endpoint_disable = endpoint_disable,
  87680. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  87681. + .endpoint_reset = endpoint_reset,
  87682. +#endif
  87683. + .get_frame_number = get_frame_number,
  87684. +
  87685. + .hub_status_data = hub_status_data,
  87686. + .hub_control = hub_control,
  87687. + //.bus_suspend =
  87688. + //.bus_resume =
  87689. +};
  87690. +
  87691. +/** Gets the dwc_otg_hcd from a struct usb_hcd */
  87692. +static inline dwc_otg_hcd_t *hcd_to_dwc_otg_hcd(struct usb_hcd *hcd)
  87693. +{
  87694. + struct wrapper_priv_data *p;
  87695. + p = (struct wrapper_priv_data *)(hcd->hcd_priv);
  87696. + return p->dwc_otg_hcd;
  87697. +}
  87698. +
  87699. +/** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
  87700. +static inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t * dwc_otg_hcd)
  87701. +{
  87702. + return dwc_otg_hcd_get_priv_data(dwc_otg_hcd);
  87703. +}
  87704. +
  87705. +/** Gets the usb_host_endpoint associated with an URB. */
  87706. +inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *urb)
  87707. +{
  87708. + struct usb_device *dev = urb->dev;
  87709. + int ep_num = usb_pipeendpoint(urb->pipe);
  87710. +
  87711. + if (usb_pipein(urb->pipe))
  87712. + return dev->ep_in[ep_num];
  87713. + else
  87714. + return dev->ep_out[ep_num];
  87715. +}
  87716. +
  87717. +static int _disconnect(dwc_otg_hcd_t * hcd)
  87718. +{
  87719. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  87720. +
  87721. + usb_hcd->self.is_b_host = 0;
  87722. + return 0;
  87723. +}
  87724. +
  87725. +static int _start(dwc_otg_hcd_t * hcd)
  87726. +{
  87727. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  87728. +
  87729. + usb_hcd->self.is_b_host = dwc_otg_hcd_is_b_host(hcd);
  87730. + hcd_start(usb_hcd);
  87731. +
  87732. + return 0;
  87733. +}
  87734. +
  87735. +static int _hub_info(dwc_otg_hcd_t * hcd, void *urb_handle, uint32_t * hub_addr,
  87736. + uint32_t * port_addr)
  87737. +{
  87738. + struct urb *urb = (struct urb *)urb_handle;
  87739. + struct usb_bus *bus;
  87740. +#if 1 //GRAYG - temporary
  87741. + if (NULL == urb_handle)
  87742. + DWC_ERROR("**** %s - NULL URB handle\n", __func__);//GRAYG
  87743. + if (NULL == urb->dev)
  87744. + DWC_ERROR("**** %s - URB has no device\n", __func__);//GRAYG
  87745. + if (NULL == port_addr)
  87746. + DWC_ERROR("**** %s - NULL port_address\n", __func__);//GRAYG
  87747. +#endif
  87748. + if (urb->dev->tt) {
  87749. + if (NULL == urb->dev->tt->hub) {
  87750. + DWC_ERROR("**** %s - (URB's transactor has no TT - giving no hub)\n",
  87751. + __func__); //GRAYG
  87752. + //*hub_addr = (u8)usb_pipedevice(urb->pipe); //GRAYG
  87753. + *hub_addr = 0; //GRAYG
  87754. + // we probably shouldn't have a transaction translator if
  87755. + // there's no associated hub?
  87756. + } else {
  87757. + bus = hcd_to_bus(dwc_otg_hcd_to_hcd(hcd));
  87758. + if (urb->dev->tt->hub == bus->root_hub)
  87759. + *hub_addr = 0;
  87760. + else
  87761. + *hub_addr = urb->dev->tt->hub->devnum;
  87762. + }
  87763. + *port_addr = urb->dev->tt->multi ? urb->dev->ttport : 1;
  87764. + } else {
  87765. + *hub_addr = 0;
  87766. + *port_addr = urb->dev->ttport;
  87767. + }
  87768. + return 0;
  87769. +}
  87770. +
  87771. +static int _speed(dwc_otg_hcd_t * hcd, void *urb_handle)
  87772. +{
  87773. + struct urb *urb = (struct urb *)urb_handle;
  87774. + return urb->dev->speed;
  87775. +}
  87776. +
  87777. +static int _get_b_hnp_enable(dwc_otg_hcd_t * hcd)
  87778. +{
  87779. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  87780. + return usb_hcd->self.b_hnp_enable;
  87781. +}
  87782. +
  87783. +static void allocate_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  87784. + struct urb *urb)
  87785. +{
  87786. + hcd_to_bus(hcd)->bandwidth_allocated += bw / urb->interval;
  87787. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  87788. + hcd_to_bus(hcd)->bandwidth_isoc_reqs++;
  87789. + } else {
  87790. + hcd_to_bus(hcd)->bandwidth_int_reqs++;
  87791. + }
  87792. +}
  87793. +
  87794. +static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  87795. + struct urb *urb)
  87796. +{
  87797. + hcd_to_bus(hcd)->bandwidth_allocated -= bw / urb->interval;
  87798. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  87799. + hcd_to_bus(hcd)->bandwidth_isoc_reqs--;
  87800. + } else {
  87801. + hcd_to_bus(hcd)->bandwidth_int_reqs--;
  87802. + }
  87803. +}
  87804. +
  87805. +/**
  87806. + * Sets the final status of an URB and returns it to the device driver. Any
  87807. + * required cleanup of the URB is performed. The HCD lock should be held on
  87808. + * entry.
  87809. + */
  87810. +static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
  87811. + dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
  87812. +{
  87813. + struct urb *urb = (struct urb *)urb_handle;
  87814. + urb_tq_entry_t *new_entry;
  87815. + int rc = 0;
  87816. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  87817. + DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
  87818. + __func__, urb, usb_pipedevice(urb->pipe),
  87819. + usb_pipeendpoint(urb->pipe),
  87820. + usb_pipein(urb->pipe) ? "IN" : "OUT", status);
  87821. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  87822. + int i;
  87823. + for (i = 0; i < urb->number_of_packets; i++) {
  87824. + DWC_PRINTF(" ISO Desc %d status: %d\n",
  87825. + i, urb->iso_frame_desc[i].status);
  87826. + }
  87827. + }
  87828. + }
  87829. + new_entry = DWC_ALLOC_ATOMIC(sizeof(urb_tq_entry_t));
  87830. + urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
  87831. + /* Convert status value. */
  87832. + switch (status) {
  87833. + case -DWC_E_PROTOCOL:
  87834. + status = -EPROTO;
  87835. + break;
  87836. + case -DWC_E_IN_PROGRESS:
  87837. + status = -EINPROGRESS;
  87838. + break;
  87839. + case -DWC_E_PIPE:
  87840. + status = -EPIPE;
  87841. + break;
  87842. + case -DWC_E_IO:
  87843. + status = -EIO;
  87844. + break;
  87845. + case -DWC_E_TIMEOUT:
  87846. + status = -ETIMEDOUT;
  87847. + break;
  87848. + case -DWC_E_OVERFLOW:
  87849. + status = -EOVERFLOW;
  87850. + break;
  87851. + case -DWC_E_SHUTDOWN:
  87852. + status = -ESHUTDOWN;
  87853. + break;
  87854. + default:
  87855. + if (status) {
  87856. + DWC_PRINTF("Uknown urb status %d\n", status);
  87857. +
  87858. + }
  87859. + }
  87860. +
  87861. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  87862. + int i;
  87863. +
  87864. + urb->error_count = dwc_otg_hcd_urb_get_error_count(dwc_otg_urb);
  87865. + for (i = 0; i < urb->number_of_packets; ++i) {
  87866. + urb->iso_frame_desc[i].actual_length =
  87867. + dwc_otg_hcd_urb_get_iso_desc_actual_length
  87868. + (dwc_otg_urb, i);
  87869. + urb->iso_frame_desc[i].status =
  87870. + dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
  87871. + }
  87872. + }
  87873. +
  87874. + urb->status = status;
  87875. + urb->hcpriv = NULL;
  87876. + if (!status) {
  87877. + if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  87878. + (urb->actual_length < urb->transfer_buffer_length)) {
  87879. + urb->status = -EREMOTEIO;
  87880. + }
  87881. + }
  87882. +
  87883. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) ||
  87884. + (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  87885. + struct usb_host_endpoint *ep = dwc_urb_to_endpoint(urb);
  87886. + if (ep) {
  87887. + free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
  87888. + dwc_otg_hcd_get_ep_bandwidth(hcd,
  87889. + ep->hcpriv),
  87890. + urb);
  87891. + }
  87892. + }
  87893. + DWC_FREE(dwc_otg_urb);
  87894. + if (!new_entry) {
  87895. + DWC_ERROR("dwc_otg_hcd: complete: cannot allocate URB TQ entry\n");
  87896. + urb->status = -EPROTO;
  87897. + /* don't schedule the tasklet -
  87898. + * directly return the packet here with error. */
  87899. +#if USB_URB_EP_LINKING
  87900. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  87901. +#endif
  87902. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  87903. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
  87904. +#else
  87905. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  87906. +#endif
  87907. + } else {
  87908. + new_entry->urb = urb;
  87909. +#if USB_URB_EP_LINKING
  87910. + rc = usb_hcd_check_unlink_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  87911. + if(0 == rc) {
  87912. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  87913. + }
  87914. +#endif
  87915. + if(0 == rc) {
  87916. + DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
  87917. + urb_tq_entries);
  87918. + DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);
  87919. + }
  87920. + }
  87921. + return 0;
  87922. +}
  87923. +
  87924. +static struct dwc_otg_hcd_function_ops hcd_fops = {
  87925. + .start = _start,
  87926. + .disconnect = _disconnect,
  87927. + .hub_info = _hub_info,
  87928. + .speed = _speed,
  87929. + .complete = _complete,
  87930. + .get_b_hnp_enable = _get_b_hnp_enable,
  87931. +};
  87932. +
  87933. +static struct fiq_handler fh = {
  87934. + .name = "usb_fiq",
  87935. +};
  87936. +
  87937. +
  87938. +
  87939. +/**
  87940. + * Initializes the HCD. This function allocates memory for and initializes the
  87941. + * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
  87942. + * USB bus with the core and calls the hc_driver->start() function. It returns
  87943. + * a negative error on failure.
  87944. + */
  87945. +int hcd_init(dwc_bus_dev_t *_dev)
  87946. +{
  87947. + struct usb_hcd *hcd = NULL;
  87948. + dwc_otg_hcd_t *dwc_otg_hcd = NULL;
  87949. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  87950. + int retval = 0;
  87951. + u64 dmamask;
  87952. + struct pt_regs regs;
  87953. +
  87954. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT otg_dev=%p\n", otg_dev);
  87955. +
  87956. + /* Set device flags indicating whether the HCD supports DMA. */
  87957. + if (dwc_otg_is_dma_enable(otg_dev->core_if))
  87958. + dmamask = DMA_BIT_MASK(32);
  87959. + else
  87960. + dmamask = 0;
  87961. +
  87962. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  87963. + dma_set_mask(&_dev->dev, dmamask);
  87964. + dma_set_coherent_mask(&_dev->dev, dmamask);
  87965. +#elif defined(PCI_INTERFACE)
  87966. + pci_set_dma_mask(_dev, dmamask);
  87967. + pci_set_consistent_dma_mask(_dev, dmamask);
  87968. +#endif
  87969. +
  87970. + /*
  87971. + * Allocate memory for the base HCD plus the DWC OTG HCD.
  87972. + * Initialize the base HCD.
  87973. + */
  87974. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  87975. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
  87976. +#else
  87977. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
  87978. + hcd->has_tt = 1;
  87979. +// hcd->uses_new_polling = 1;
  87980. +// hcd->poll_rh = 0;
  87981. +#endif
  87982. + if (!hcd) {
  87983. + retval = -ENOMEM;
  87984. + goto error1;
  87985. + }
  87986. +
  87987. + hcd->regs = otg_dev->os_dep.base;
  87988. +
  87989. +
  87990. + /* Initialize the DWC OTG HCD. */
  87991. + dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
  87992. + if (!dwc_otg_hcd) {
  87993. + goto error2;
  87994. + }
  87995. + ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
  87996. + dwc_otg_hcd;
  87997. + otg_dev->hcd = dwc_otg_hcd;
  87998. +
  87999. + if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
  88000. + goto error2;
  88001. + }
  88002. +
  88003. + if (fiq_enable)
  88004. + {
  88005. + if (claim_fiq(&fh)) {
  88006. + DWC_ERROR("Can't claim FIQ");
  88007. + goto error2;
  88008. + }
  88009. +
  88010. + DWC_WARN("FIQ at 0x%08x", (fiq_fsm_enable ? (int)&dwc_otg_fiq_fsm : (int)&dwc_otg_fiq_nop));
  88011. + DWC_WARN("FIQ ASM at 0x%08x length %d", (int)&_dwc_otg_fiq_stub, (int)(&_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub));
  88012. +
  88013. + set_fiq_handler((void *) &_dwc_otg_fiq_stub, &_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub);
  88014. + memset(&regs,0,sizeof(regs));
  88015. +
  88016. + regs.ARM_r8 = (long) dwc_otg_hcd->fiq_state;
  88017. + if (fiq_fsm_enable) {
  88018. + regs.ARM_r9 = dwc_otg_hcd->core_if->core_params->host_channels;
  88019. + //regs.ARM_r10 = dwc_otg_hcd->dma;
  88020. + regs.ARM_fp = (long) dwc_otg_fiq_fsm;
  88021. + } else {
  88022. + regs.ARM_fp = (long) dwc_otg_fiq_nop;
  88023. + }
  88024. +
  88025. + regs.ARM_sp = (long) dwc_otg_hcd->fiq_stack + (sizeof(struct fiq_stack) - 4);
  88026. +
  88027. +// __show_regs(&regs);
  88028. + set_fiq_regs(&regs);
  88029. +
  88030. + //Set the mphi periph to the required registers
  88031. + dwc_otg_hcd->fiq_state->mphi_regs.base = otg_dev->os_dep.mphi_base;
  88032. + dwc_otg_hcd->fiq_state->mphi_regs.ctrl = otg_dev->os_dep.mphi_base + 0x4c;
  88033. + dwc_otg_hcd->fiq_state->mphi_regs.outdda = otg_dev->os_dep.mphi_base + 0x28;
  88034. + dwc_otg_hcd->fiq_state->mphi_regs.outddb = otg_dev->os_dep.mphi_base + 0x2c;
  88035. + dwc_otg_hcd->fiq_state->mphi_regs.intstat = otg_dev->os_dep.mphi_base + 0x50;
  88036. + dwc_otg_hcd->fiq_state->dwc_regs_base = otg_dev->os_dep.base;
  88037. + DWC_WARN("MPHI regs_base at 0x%08x", (int)dwc_otg_hcd->fiq_state->mphi_regs.base);
  88038. + //Enable mphi peripheral
  88039. + writel((1<<31),dwc_otg_hcd->fiq_state->mphi_regs.ctrl);
  88040. +#ifdef DEBUG
  88041. + if (readl(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & 0x80000000)
  88042. + DWC_WARN("MPHI periph has been enabled");
  88043. + else
  88044. + DWC_WARN("MPHI periph has NOT been enabled");
  88045. +#endif
  88046. + // Enable FIQ interrupt from USB peripheral
  88047. + enable_fiq(INTERRUPT_VC_USB);
  88048. + local_fiq_enable();
  88049. + }
  88050. +
  88051. +
  88052. + otg_dev->hcd->otg_dev = otg_dev;
  88053. + hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
  88054. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
  88055. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) //version field absent later
  88056. + hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
  88057. +#endif
  88058. + /* Don't support SG list at this point */
  88059. + hcd->self.sg_tablesize = 0;
  88060. +#endif
  88061. + /*
  88062. + * Finish generic HCD initialization and start the HCD. This function
  88063. + * allocates the DMA buffer pool, registers the USB bus, requests the
  88064. + * IRQ line, and calls hcd_start method.
  88065. + */
  88066. +#ifdef PLATFORM_INTERFACE
  88067. + retval = usb_add_hcd(hcd, platform_get_irq(_dev, fiq_enable ? 0 : 1), IRQF_SHARED | IRQF_DISABLED);
  88068. +#else
  88069. + retval = usb_add_hcd(hcd, _dev->irq, IRQF_SHARED | IRQF_DISABLED);
  88070. +#endif
  88071. + if (retval < 0) {
  88072. + goto error2;
  88073. + }
  88074. +
  88075. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
  88076. + return 0;
  88077. +
  88078. +error2:
  88079. + usb_put_hcd(hcd);
  88080. +error1:
  88081. + return retval;
  88082. +}
  88083. +
  88084. +/**
  88085. + * Removes the HCD.
  88086. + * Frees memory and resources associated with the HCD and deregisters the bus.
  88087. + */
  88088. +void hcd_remove(dwc_bus_dev_t *_dev)
  88089. +{
  88090. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  88091. + dwc_otg_hcd_t *dwc_otg_hcd;
  88092. + struct usb_hcd *hcd;
  88093. +
  88094. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE otg_dev=%p\n", otg_dev);
  88095. +
  88096. + if (!otg_dev) {
  88097. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  88098. + return;
  88099. + }
  88100. +
  88101. + dwc_otg_hcd = otg_dev->hcd;
  88102. +
  88103. + if (!dwc_otg_hcd) {
  88104. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  88105. + return;
  88106. + }
  88107. +
  88108. + hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
  88109. +
  88110. + if (!hcd) {
  88111. + DWC_DEBUGPL(DBG_ANY,
  88112. + "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
  88113. + __func__);
  88114. + return;
  88115. + }
  88116. + usb_remove_hcd(hcd);
  88117. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
  88118. + dwc_otg_hcd_remove(dwc_otg_hcd);
  88119. + usb_put_hcd(hcd);
  88120. +}
  88121. +
  88122. +/* =========================================================================
  88123. + * Linux HC Driver Functions
  88124. + * ========================================================================= */
  88125. +
  88126. +/** Initializes the DWC_otg controller and its root hub and prepares it for host
  88127. + * mode operation. Activates the root port. Returns 0 on success and a negative
  88128. + * error code on failure. */
  88129. +int hcd_start(struct usb_hcd *hcd)
  88130. +{
  88131. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  88132. + struct usb_bus *bus;
  88133. +
  88134. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
  88135. + bus = hcd_to_bus(hcd);
  88136. +
  88137. + hcd->state = HC_STATE_RUNNING;
  88138. + if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
  88139. + return 0;
  88140. + }
  88141. +
  88142. + /* Initialize and connect root hub if one is not already attached */
  88143. + if (bus->root_hub) {
  88144. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
  88145. + /* Inform the HUB driver to resume. */
  88146. + usb_hcd_resume_root_hub(hcd);
  88147. + }
  88148. +
  88149. + return 0;
  88150. +}
  88151. +
  88152. +/**
  88153. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  88154. + * stopped.
  88155. + */
  88156. +void hcd_stop(struct usb_hcd *hcd)
  88157. +{
  88158. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  88159. +
  88160. + dwc_otg_hcd_stop(dwc_otg_hcd);
  88161. +}
  88162. +
  88163. +/** Returns the current frame number. */
  88164. +static int get_frame_number(struct usb_hcd *hcd)
  88165. +{
  88166. + hprt0_data_t hprt0;
  88167. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  88168. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  88169. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
  88170. + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd) >> 3;
  88171. + else
  88172. + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
  88173. +}
  88174. +
  88175. +#ifdef DEBUG
  88176. +static void dump_urb_info(struct urb *urb, char *fn_name)
  88177. +{
  88178. + DWC_PRINTF("%s, urb %p\n", fn_name, urb);
  88179. + DWC_PRINTF(" Device address: %d\n", usb_pipedevice(urb->pipe));
  88180. + DWC_PRINTF(" Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
  88181. + (usb_pipein(urb->pipe) ? "IN" : "OUT"));
  88182. + DWC_PRINTF(" Endpoint type: %s\n", ( {
  88183. + char *pipetype;
  88184. + switch (usb_pipetype(urb->pipe)) {
  88185. +case PIPE_CONTROL:
  88186. +pipetype = "CONTROL"; break; case PIPE_BULK:
  88187. +pipetype = "BULK"; break; case PIPE_INTERRUPT:
  88188. +pipetype = "INTERRUPT"; break; case PIPE_ISOCHRONOUS:
  88189. +pipetype = "ISOCHRONOUS"; break; default:
  88190. + pipetype = "UNKNOWN"; break;};
  88191. + pipetype;}
  88192. + )) ;
  88193. + DWC_PRINTF(" Speed: %s\n", ( {
  88194. + char *speed; switch (urb->dev->speed) {
  88195. +case USB_SPEED_HIGH:
  88196. +speed = "HIGH"; break; case USB_SPEED_FULL:
  88197. +speed = "FULL"; break; case USB_SPEED_LOW:
  88198. +speed = "LOW"; break; default:
  88199. + speed = "UNKNOWN"; break;};
  88200. + speed;}
  88201. + )) ;
  88202. + DWC_PRINTF(" Max packet size: %d\n",
  88203. + usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
  88204. + DWC_PRINTF(" Data buffer length: %d\n", urb->transfer_buffer_length);
  88205. + DWC_PRINTF(" Transfer buffer: %p, Transfer DMA: %p\n",
  88206. + urb->transfer_buffer, (void *)urb->transfer_dma);
  88207. + DWC_PRINTF(" Setup buffer: %p, Setup DMA: %p\n",
  88208. + urb->setup_packet, (void *)urb->setup_dma);
  88209. + DWC_PRINTF(" Interval: %d\n", urb->interval);
  88210. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  88211. + int i;
  88212. + for (i = 0; i < urb->number_of_packets; i++) {
  88213. + DWC_PRINTF(" ISO Desc %d:\n", i);
  88214. + DWC_PRINTF(" offset: %d, length %d\n",
  88215. + urb->iso_frame_desc[i].offset,
  88216. + urb->iso_frame_desc[i].length);
  88217. + }
  88218. + }
  88219. +}
  88220. +#endif
  88221. +
  88222. +/** Starts processing a USB transfer request specified by a USB Request Block
  88223. + * (URB). mem_flags indicates the type of memory allocation to use while
  88224. + * processing this URB. */
  88225. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  88226. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  88227. + struct usb_host_endpoint *ep,
  88228. +#endif
  88229. + struct urb *urb, gfp_t mem_flags)
  88230. +{
  88231. + int retval = 0;
  88232. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
  88233. + struct usb_host_endpoint *ep = urb->ep;
  88234. +#endif
  88235. + dwc_irqflags_t irqflags;
  88236. + void **ref_ep_hcpriv = &ep->hcpriv;
  88237. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  88238. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  88239. + int i;
  88240. + int alloc_bandwidth = 0;
  88241. + uint8_t ep_type = 0;
  88242. + uint32_t flags = 0;
  88243. + void *buf;
  88244. +
  88245. +#ifdef DEBUG
  88246. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  88247. + dump_urb_info(urb, "dwc_otg_urb_enqueue");
  88248. + }
  88249. +#endif
  88250. +
  88251. + if (!urb->transfer_buffer && urb->transfer_buffer_length)
  88252. + return -EINVAL;
  88253. +
  88254. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  88255. + || (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  88256. + if (!dwc_otg_hcd_is_bandwidth_allocated
  88257. + (dwc_otg_hcd, ref_ep_hcpriv)) {
  88258. + alloc_bandwidth = 1;
  88259. + }
  88260. + }
  88261. +
  88262. + switch (usb_pipetype(urb->pipe)) {
  88263. + case PIPE_CONTROL:
  88264. + ep_type = USB_ENDPOINT_XFER_CONTROL;
  88265. + break;
  88266. + case PIPE_ISOCHRONOUS:
  88267. + ep_type = USB_ENDPOINT_XFER_ISOC;
  88268. + break;
  88269. + case PIPE_BULK:
  88270. + ep_type = USB_ENDPOINT_XFER_BULK;
  88271. + break;
  88272. + case PIPE_INTERRUPT:
  88273. + ep_type = USB_ENDPOINT_XFER_INT;
  88274. + break;
  88275. + default:
  88276. + DWC_WARN("Wrong EP type - %d\n", usb_pipetype(urb->pipe));
  88277. + }
  88278. +
  88279. + /* # of packets is often 0 - do we really need to call this then? */
  88280. + dwc_otg_urb = dwc_otg_hcd_urb_alloc(dwc_otg_hcd,
  88281. + urb->number_of_packets,
  88282. + mem_flags == GFP_ATOMIC ? 1 : 0);
  88283. +
  88284. + if(dwc_otg_urb == NULL)
  88285. + return -ENOMEM;
  88286. +
  88287. + if (!dwc_otg_urb && urb->number_of_packets)
  88288. + return -ENOMEM;
  88289. +
  88290. + dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_urb, usb_pipedevice(urb->pipe),
  88291. + usb_pipeendpoint(urb->pipe), ep_type,
  88292. + usb_pipein(urb->pipe),
  88293. + usb_maxpacket(urb->dev, urb->pipe,
  88294. + !(usb_pipein(urb->pipe))));
  88295. +
  88296. + buf = urb->transfer_buffer;
  88297. + if (hcd->self.uses_dma) {
  88298. + /*
  88299. + * Calculate virtual address from physical address,
  88300. + * because some class driver may not fill transfer_buffer.
  88301. + * In Buffer DMA mode virual address is used,
  88302. + * when handling non DWORD aligned buffers.
  88303. + */
  88304. + //buf = phys_to_virt(urb->transfer_dma);
  88305. + // DMA addresses are bus addresses not physical addresses!
  88306. + buf = dma_to_virt(&urb->dev->dev, urb->transfer_dma);
  88307. + }
  88308. +
  88309. + if (!(urb->transfer_flags & URB_NO_INTERRUPT))
  88310. + flags |= URB_GIVEBACK_ASAP;
  88311. + if (urb->transfer_flags & URB_ZERO_PACKET)
  88312. + flags |= URB_SEND_ZERO_PACKET;
  88313. +
  88314. + dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
  88315. + urb->transfer_dma,
  88316. + urb->transfer_buffer_length,
  88317. + urb->setup_packet,
  88318. + urb->setup_dma, flags, urb->interval);
  88319. +
  88320. + for (i = 0; i < urb->number_of_packets; ++i) {
  88321. + dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
  88322. + urb->
  88323. + iso_frame_desc[i].offset,
  88324. + urb->
  88325. + iso_frame_desc[i].length);
  88326. + }
  88327. +
  88328. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &irqflags);
  88329. + urb->hcpriv = dwc_otg_urb;
  88330. +#if USB_URB_EP_LINKING
  88331. + retval = usb_hcd_link_urb_to_ep(hcd, urb);
  88332. + if (0 == retval)
  88333. +#endif
  88334. + {
  88335. + retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb,
  88336. + /*(dwc_otg_qh_t **)*/
  88337. + ref_ep_hcpriv, 1);
  88338. + if (0 == retval) {
  88339. + if (alloc_bandwidth) {
  88340. + allocate_bus_bandwidth(hcd,
  88341. + dwc_otg_hcd_get_ep_bandwidth(
  88342. + dwc_otg_hcd, *ref_ep_hcpriv),
  88343. + urb);
  88344. + }
  88345. + } else {
  88346. + DWC_DEBUGPL(DBG_HCD, "DWC OTG dwc_otg_hcd_urb_enqueue failed rc %d\n", retval);
  88347. +#if USB_URB_EP_LINKING
  88348. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  88349. +#endif
  88350. + DWC_FREE(dwc_otg_urb);
  88351. + urb->hcpriv = NULL;
  88352. + if (retval == -DWC_E_NO_DEVICE)
  88353. + retval = -ENODEV;
  88354. + }
  88355. + }
  88356. +#if USB_URB_EP_LINKING
  88357. + else
  88358. + {
  88359. + DWC_FREE(dwc_otg_urb);
  88360. + urb->hcpriv = NULL;
  88361. + }
  88362. +#endif
  88363. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, irqflags);
  88364. + return retval;
  88365. +}
  88366. +
  88367. +/** Aborts/cancels a USB transfer request. Always returns 0 to indicate
  88368. + * success. */
  88369. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  88370. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
  88371. +#else
  88372. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  88373. +#endif
  88374. +{
  88375. + dwc_irqflags_t flags;
  88376. + dwc_otg_hcd_t *dwc_otg_hcd;
  88377. + int rc;
  88378. +
  88379. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
  88380. +
  88381. + dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  88382. +
  88383. +#ifdef DEBUG
  88384. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  88385. + dump_urb_info(urb, "dwc_otg_urb_dequeue");
  88386. + }
  88387. +#endif
  88388. +
  88389. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  88390. + rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  88391. + if (0 == rc) {
  88392. + if(urb->hcpriv != NULL) {
  88393. + dwc_otg_hcd_urb_dequeue(dwc_otg_hcd,
  88394. + (dwc_otg_hcd_urb_t *)urb->hcpriv);
  88395. +
  88396. + DWC_FREE(urb->hcpriv);
  88397. + urb->hcpriv = NULL;
  88398. + }
  88399. + }
  88400. +
  88401. + if (0 == rc) {
  88402. + /* Higher layer software sets URB status. */
  88403. +#if USB_URB_EP_LINKING
  88404. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  88405. +#endif
  88406. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  88407. +
  88408. +
  88409. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  88410. + usb_hcd_giveback_urb(hcd, urb);
  88411. +#else
  88412. + usb_hcd_giveback_urb(hcd, urb, status);
  88413. +#endif
  88414. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  88415. + DWC_PRINTF("Called usb_hcd_giveback_urb() \n");
  88416. + DWC_PRINTF(" 1urb->status = %d\n", urb->status);
  88417. + }
  88418. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue OK\n");
  88419. + } else {
  88420. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  88421. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue failed - rc %d\n",
  88422. + rc);
  88423. + }
  88424. +
  88425. + return rc;
  88426. +}
  88427. +
  88428. +/* Frees resources in the DWC_otg controller related to a given endpoint. Also
  88429. + * clears state in the HCD related to the endpoint. Any URBs for the endpoint
  88430. + * must already be dequeued. */
  88431. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  88432. +{
  88433. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  88434. +
  88435. + DWC_DEBUGPL(DBG_HCD,
  88436. + "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
  88437. + "endpoint=%d\n", ep->desc.bEndpointAddress,
  88438. + dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
  88439. + dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
  88440. + ep->hcpriv = NULL;
  88441. +}
  88442. +
  88443. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  88444. +/* Resets endpoint specific parameter values, in current version used to reset
  88445. + * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
  88446. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  88447. +{
  88448. + dwc_irqflags_t flags;
  88449. + struct usb_device *udev = NULL;
  88450. + int epnum = usb_endpoint_num(&ep->desc);
  88451. + int is_out = usb_endpoint_dir_out(&ep->desc);
  88452. + int is_control = usb_endpoint_xfer_control(&ep->desc);
  88453. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  88454. + struct device *dev = DWC_OTG_OS_GETDEV(dwc_otg_hcd->otg_dev->os_dep);
  88455. +
  88456. + if (dev)
  88457. + udev = to_usb_device(dev);
  88458. + else
  88459. + return;
  88460. +
  88461. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n", epnum);
  88462. +
  88463. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  88464. + usb_settoggle(udev, epnum, is_out, 0);
  88465. + if (is_control)
  88466. + usb_settoggle(udev, epnum, !is_out, 0);
  88467. +
  88468. + if (ep->hcpriv) {
  88469. + dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
  88470. + }
  88471. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  88472. +}
  88473. +#endif
  88474. +
  88475. +/** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
  88476. + * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
  88477. + * interrupt.
  88478. + *
  88479. + * This function is called by the USB core when an interrupt occurs */
  88480. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
  88481. +{
  88482. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  88483. + int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
  88484. + if (retval != 0) {
  88485. + S3C2410X_CLEAR_EINTPEND();
  88486. + }
  88487. + return IRQ_RETVAL(retval);
  88488. +}
  88489. +
  88490. +/** Creates Status Change bitmap for the root hub and root port. The bitmap is
  88491. + * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
  88492. + * is the status change indicator for the single root port. Returns 1 if either
  88493. + * change indicator is 1, otherwise returns 0. */
  88494. +int hub_status_data(struct usb_hcd *hcd, char *buf)
  88495. +{
  88496. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  88497. +
  88498. + buf[0] = 0;
  88499. + buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
  88500. +
  88501. + return (buf[0] != 0);
  88502. +}
  88503. +
  88504. +/** Handles hub class-specific requests. */
  88505. +int hub_control(struct usb_hcd *hcd,
  88506. + u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
  88507. +{
  88508. + int retval;
  88509. +
  88510. + retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
  88511. + typeReq, wValue, wIndex, buf, wLength);
  88512. +
  88513. + switch (retval) {
  88514. + case -DWC_E_INVALID:
  88515. + retval = -EINVAL;
  88516. + break;
  88517. + }
  88518. +
  88519. + return retval;
  88520. +}
  88521. +
  88522. +#endif /* DWC_DEVICE_ONLY */
  88523. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
  88524. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c 1969-12-31 18:00:00.000000000 -0600
  88525. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c 2014-12-03 19:13:40.220418001 -0600
  88526. @@ -0,0 +1,942 @@
  88527. +/* ==========================================================================
  88528. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_queue.c $
  88529. + * $Revision: #44 $
  88530. + * $Date: 2011/10/26 $
  88531. + * $Change: 1873028 $
  88532. + *
  88533. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  88534. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  88535. + * otherwise expressly agreed to in writing between Synopsys and you.
  88536. + *
  88537. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  88538. + * any End User Software License Agreement or Agreement for Licensed Product
  88539. + * with Synopsys or any supplement thereto. You are permitted to use and
  88540. + * redistribute this Software in source and binary forms, with or without
  88541. + * modification, provided that redistributions of source code must retain this
  88542. + * notice. You may not view, use, disclose, copy or distribute this file or
  88543. + * any information contained herein except pursuant to this license grant from
  88544. + * Synopsys. If you do not agree with this notice, including the disclaimer
  88545. + * below, then you are not authorized to use the Software.
  88546. + *
  88547. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  88548. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  88549. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  88550. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  88551. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  88552. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  88553. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  88554. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  88555. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  88556. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  88557. + * DAMAGE.
  88558. + * ========================================================================== */
  88559. +#ifndef DWC_DEVICE_ONLY
  88560. +
  88561. +/**
  88562. + * @file
  88563. + *
  88564. + * This file contains the functions to manage Queue Heads and Queue
  88565. + * Transfer Descriptors.
  88566. + */
  88567. +
  88568. +#include "dwc_otg_hcd.h"
  88569. +#include "dwc_otg_regs.h"
  88570. +
  88571. +extern bool microframe_schedule;
  88572. +
  88573. +/**
  88574. + * Free each QTD in the QH's QTD-list then free the QH. QH should already be
  88575. + * removed from a list. QTD list should already be empty if called from URB
  88576. + * Dequeue.
  88577. + *
  88578. + * @param hcd HCD instance.
  88579. + * @param qh The QH to free.
  88580. + */
  88581. +void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  88582. +{
  88583. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  88584. +
  88585. + /* Free each QTD in the QTD list */
  88586. + DWC_SPINLOCK(hcd->lock);
  88587. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  88588. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  88589. + dwc_otg_hcd_qtd_free(qtd);
  88590. + }
  88591. +
  88592. + if (hcd->core_if->dma_desc_enable) {
  88593. + dwc_otg_hcd_qh_free_ddma(hcd, qh);
  88594. + } else if (qh->dw_align_buf) {
  88595. + uint32_t buf_size;
  88596. + if (qh->ep_type == UE_ISOCHRONOUS) {
  88597. + buf_size = 4096;
  88598. + } else {
  88599. + buf_size = hcd->core_if->core_params->max_transfer_size;
  88600. + }
  88601. + DWC_DMA_FREE(buf_size, qh->dw_align_buf, qh->dw_align_buf_dma);
  88602. + }
  88603. +
  88604. + DWC_FREE(qh);
  88605. + DWC_SPINUNLOCK(hcd->lock);
  88606. + return;
  88607. +}
  88608. +
  88609. +#define BitStuffTime(bytecount) ((8 * 7* bytecount) / 6)
  88610. +#define HS_HOST_DELAY 5 /* nanoseconds */
  88611. +#define FS_LS_HOST_DELAY 1000 /* nanoseconds */
  88612. +#define HUB_LS_SETUP 333 /* nanoseconds */
  88613. +#define NS_TO_US(ns) ((ns + 500) / 1000)
  88614. + /* convert & round nanoseconds to microseconds */
  88615. +
  88616. +static uint32_t calc_bus_time(int speed, int is_in, int is_isoc, int bytecount)
  88617. +{
  88618. + unsigned long retval;
  88619. +
  88620. + switch (speed) {
  88621. + case USB_SPEED_HIGH:
  88622. + if (is_isoc) {
  88623. + retval =
  88624. + ((38 * 8 * 2083) +
  88625. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  88626. + HS_HOST_DELAY;
  88627. + } else {
  88628. + retval =
  88629. + ((55 * 8 * 2083) +
  88630. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  88631. + HS_HOST_DELAY;
  88632. + }
  88633. + break;
  88634. + case USB_SPEED_FULL:
  88635. + if (is_isoc) {
  88636. + retval =
  88637. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  88638. + if (is_in) {
  88639. + retval = 7268 + FS_LS_HOST_DELAY + retval;
  88640. + } else {
  88641. + retval = 6265 + FS_LS_HOST_DELAY + retval;
  88642. + }
  88643. + } else {
  88644. + retval =
  88645. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  88646. + retval = 9107 + FS_LS_HOST_DELAY + retval;
  88647. + }
  88648. + break;
  88649. + case USB_SPEED_LOW:
  88650. + if (is_in) {
  88651. + retval =
  88652. + (67667 * (31 + 10 * BitStuffTime(bytecount))) /
  88653. + 1000;
  88654. + retval =
  88655. + 64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  88656. + retval;
  88657. + } else {
  88658. + retval =
  88659. + (66700 * (31 + 10 * BitStuffTime(bytecount))) /
  88660. + 1000;
  88661. + retval =
  88662. + 64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  88663. + retval;
  88664. + }
  88665. + break;
  88666. + default:
  88667. + DWC_WARN("Unknown device speed\n");
  88668. + retval = -1;
  88669. + }
  88670. +
  88671. + return NS_TO_US(retval);
  88672. +}
  88673. +
  88674. +/**
  88675. + * Initializes a QH structure.
  88676. + *
  88677. + * @param hcd The HCD state structure for the DWC OTG controller.
  88678. + * @param qh The QH to init.
  88679. + * @param urb Holds the information about the device/endpoint that we need
  88680. + * to initialize the QH.
  88681. + */
  88682. +#define SCHEDULE_SLOP 10
  88683. +void qh_init(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, dwc_otg_hcd_urb_t * urb)
  88684. +{
  88685. + char *speed, *type;
  88686. + int dev_speed;
  88687. + uint32_t hub_addr, hub_port;
  88688. +
  88689. + dwc_memset(qh, 0, sizeof(dwc_otg_qh_t));
  88690. +
  88691. + /* Initialize QH */
  88692. + qh->ep_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  88693. + qh->ep_is_in = dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
  88694. +
  88695. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  88696. + qh->maxp = dwc_otg_hcd_get_mps(&urb->pipe_info);
  88697. + DWC_CIRCLEQ_INIT(&qh->qtd_list);
  88698. + DWC_LIST_INIT(&qh->qh_list_entry);
  88699. + qh->channel = NULL;
  88700. +
  88701. + /* FS/LS Enpoint on HS Hub
  88702. + * NOT virtual root hub */
  88703. + dev_speed = hcd->fops->speed(hcd, urb->priv);
  88704. +
  88705. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &hub_port);
  88706. + qh->do_split = 0;
  88707. + if (microframe_schedule)
  88708. + qh->speed = dev_speed;
  88709. +
  88710. + qh->nak_frame = 0xffff;
  88711. +
  88712. + if (((dev_speed == USB_SPEED_LOW) ||
  88713. + (dev_speed == USB_SPEED_FULL)) &&
  88714. + (hub_addr != 0 && hub_addr != 1)) {
  88715. + DWC_DEBUGPL(DBG_HCD,
  88716. + "QH init: EP %d: TT found at hub addr %d, for port %d\n",
  88717. + dwc_otg_hcd_get_ep_num(&urb->pipe_info), hub_addr,
  88718. + hub_port);
  88719. + qh->do_split = 1;
  88720. + qh->skip_count = 0;
  88721. + }
  88722. +
  88723. + if (qh->ep_type == UE_INTERRUPT || qh->ep_type == UE_ISOCHRONOUS) {
  88724. + /* Compute scheduling parameters once and save them. */
  88725. + hprt0_data_t hprt;
  88726. +
  88727. + /** @todo Account for split transfers in the bus time. */
  88728. + int bytecount =
  88729. + dwc_hb_mult(qh->maxp) * dwc_max_packet(qh->maxp);
  88730. +
  88731. + qh->usecs =
  88732. + calc_bus_time((qh->do_split ? USB_SPEED_HIGH : dev_speed),
  88733. + qh->ep_is_in, (qh->ep_type == UE_ISOCHRONOUS),
  88734. + bytecount);
  88735. + /* Start in a slightly future (micro)frame. */
  88736. + qh->sched_frame = dwc_frame_num_inc(hcd->frame_number,
  88737. + SCHEDULE_SLOP);
  88738. + qh->interval = urb->interval;
  88739. +
  88740. +#if 0
  88741. + /* Increase interrupt polling rate for debugging. */
  88742. + if (qh->ep_type == UE_INTERRUPT) {
  88743. + qh->interval = 8;
  88744. + }
  88745. +#endif
  88746. + hprt.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  88747. + if ((hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED) &&
  88748. + ((dev_speed == USB_SPEED_LOW) ||
  88749. + (dev_speed == USB_SPEED_FULL))) {
  88750. + qh->interval *= 8;
  88751. + qh->sched_frame |= 0x7;
  88752. + qh->start_split_frame = qh->sched_frame;
  88753. + }
  88754. +
  88755. + }
  88756. +
  88757. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD QH Initialized\n");
  88758. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - qh = %p\n", qh);
  88759. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Device Address = %d\n",
  88760. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  88761. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Endpoint %d, %s\n",
  88762. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  88763. + dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
  88764. + switch (dev_speed) {
  88765. + case USB_SPEED_LOW:
  88766. + qh->dev_speed = DWC_OTG_EP_SPEED_LOW;
  88767. + speed = "low";
  88768. + break;
  88769. + case USB_SPEED_FULL:
  88770. + qh->dev_speed = DWC_OTG_EP_SPEED_FULL;
  88771. + speed = "full";
  88772. + break;
  88773. + case USB_SPEED_HIGH:
  88774. + qh->dev_speed = DWC_OTG_EP_SPEED_HIGH;
  88775. + speed = "high";
  88776. + break;
  88777. + default:
  88778. + speed = "?";
  88779. + break;
  88780. + }
  88781. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Speed = %s\n", speed);
  88782. +
  88783. + switch (qh->ep_type) {
  88784. + case UE_ISOCHRONOUS:
  88785. + type = "isochronous";
  88786. + break;
  88787. + case UE_INTERRUPT:
  88788. + type = "interrupt";
  88789. + break;
  88790. + case UE_CONTROL:
  88791. + type = "control";
  88792. + break;
  88793. + case UE_BULK:
  88794. + type = "bulk";
  88795. + break;
  88796. + default:
  88797. + type = "?";
  88798. + break;
  88799. + }
  88800. +
  88801. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Type = %s\n", type);
  88802. +
  88803. +#ifdef DEBUG
  88804. + if (qh->ep_type == UE_INTERRUPT) {
  88805. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - usecs = %d\n",
  88806. + qh->usecs);
  88807. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - interval = %d\n",
  88808. + qh->interval);
  88809. + }
  88810. +#endif
  88811. +
  88812. +}
  88813. +
  88814. +/**
  88815. + * This function allocates and initializes a QH.
  88816. + *
  88817. + * @param hcd The HCD state structure for the DWC OTG controller.
  88818. + * @param urb Holds the information about the device/endpoint that we need
  88819. + * to initialize the QH.
  88820. + * @param atomic_alloc Flag to do atomic allocation if needed
  88821. + *
  88822. + * @return Returns pointer to the newly allocated QH, or NULL on error. */
  88823. +dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  88824. + dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  88825. +{
  88826. + dwc_otg_qh_t *qh;
  88827. +
  88828. + /* Allocate memory */
  88829. + /** @todo add memflags argument */
  88830. + qh = dwc_otg_hcd_qh_alloc(atomic_alloc);
  88831. + if (qh == NULL) {
  88832. + DWC_ERROR("qh allocation failed");
  88833. + return NULL;
  88834. + }
  88835. +
  88836. + qh_init(hcd, qh, urb);
  88837. +
  88838. + if (hcd->core_if->dma_desc_enable
  88839. + && (dwc_otg_hcd_qh_init_ddma(hcd, qh) < 0)) {
  88840. + dwc_otg_hcd_qh_free(hcd, qh);
  88841. + return NULL;
  88842. + }
  88843. +
  88844. + return qh;
  88845. +}
  88846. +
  88847. +/* microframe_schedule=0 start */
  88848. +
  88849. +/**
  88850. + * Checks that a channel is available for a periodic transfer.
  88851. + *
  88852. + * @return 0 if successful, negative error code otherise.
  88853. + */
  88854. +static int periodic_channel_available(dwc_otg_hcd_t * hcd)
  88855. +{
  88856. + /*
  88857. + * Currently assuming that there is a dedicated host channnel for each
  88858. + * periodic transaction plus at least one host channel for
  88859. + * non-periodic transactions.
  88860. + */
  88861. + int status;
  88862. + int num_channels;
  88863. +
  88864. + num_channels = hcd->core_if->core_params->host_channels;
  88865. + if ((hcd->periodic_channels + hcd->non_periodic_channels < num_channels)
  88866. + && (hcd->periodic_channels < num_channels - 1)) {
  88867. + status = 0;
  88868. + } else {
  88869. + DWC_INFO("%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
  88870. + __func__, num_channels, hcd->periodic_channels, hcd->non_periodic_channels); //NOTICE
  88871. + status = -DWC_E_NO_SPACE;
  88872. + }
  88873. +
  88874. + return status;
  88875. +}
  88876. +
  88877. +/**
  88878. + * Checks that there is sufficient bandwidth for the specified QH in the
  88879. + * periodic schedule. For simplicity, this calculation assumes that all the
  88880. + * transfers in the periodic schedule may occur in the same (micro)frame.
  88881. + *
  88882. + * @param hcd The HCD state structure for the DWC OTG controller.
  88883. + * @param qh QH containing periodic bandwidth required.
  88884. + *
  88885. + * @return 0 if successful, negative error code otherwise.
  88886. + */
  88887. +static int check_periodic_bandwidth(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  88888. +{
  88889. + int status;
  88890. + int16_t max_claimed_usecs;
  88891. +
  88892. + status = 0;
  88893. +
  88894. + if ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) || qh->do_split) {
  88895. + /*
  88896. + * High speed mode.
  88897. + * Max periodic usecs is 80% x 125 usec = 100 usec.
  88898. + */
  88899. +
  88900. + max_claimed_usecs = 100 - qh->usecs;
  88901. + } else {
  88902. + /*
  88903. + * Full speed mode.
  88904. + * Max periodic usecs is 90% x 1000 usec = 900 usec.
  88905. + */
  88906. + max_claimed_usecs = 900 - qh->usecs;
  88907. + }
  88908. +
  88909. + if (hcd->periodic_usecs > max_claimed_usecs) {
  88910. + DWC_INFO("%s: already claimed usecs %d, required usecs %d\n", __func__, hcd->periodic_usecs, qh->usecs); //NOTICE
  88911. + status = -DWC_E_NO_SPACE;
  88912. + }
  88913. +
  88914. + return status;
  88915. +}
  88916. +
  88917. +/* microframe_schedule=0 end */
  88918. +
  88919. +/**
  88920. + * Microframe scheduler
  88921. + * track the total use in hcd->frame_usecs
  88922. + * keep each qh use in qh->frame_usecs
  88923. + * when surrendering the qh then donate the time back
  88924. + */
  88925. +const unsigned short max_uframe_usecs[]={ 100, 100, 100, 100, 100, 100, 30, 0 };
  88926. +
  88927. +/*
  88928. + * called from dwc_otg_hcd.c:dwc_otg_hcd_init
  88929. + */
  88930. +int init_hcd_usecs(dwc_otg_hcd_t *_hcd)
  88931. +{
  88932. + int i;
  88933. + for (i=0; i<8; i++) {
  88934. + _hcd->frame_usecs[i] = max_uframe_usecs[i];
  88935. + }
  88936. + return 0;
  88937. +}
  88938. +
  88939. +static int find_single_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  88940. +{
  88941. + int i;
  88942. + unsigned short utime;
  88943. + int t_left;
  88944. + int ret;
  88945. + int done;
  88946. +
  88947. + ret = -1;
  88948. + utime = _qh->usecs;
  88949. + t_left = utime;
  88950. + i = 0;
  88951. + done = 0;
  88952. + while (done == 0) {
  88953. + /* At the start _hcd->frame_usecs[i] = max_uframe_usecs[i]; */
  88954. + if (utime <= _hcd->frame_usecs[i]) {
  88955. + _hcd->frame_usecs[i] -= utime;
  88956. + _qh->frame_usecs[i] += utime;
  88957. + t_left -= utime;
  88958. + ret = i;
  88959. + done = 1;
  88960. + return ret;
  88961. + } else {
  88962. + i++;
  88963. + if (i == 8) {
  88964. + done = 1;
  88965. + ret = -1;
  88966. + }
  88967. + }
  88968. + }
  88969. + return ret;
  88970. + }
  88971. +
  88972. +/*
  88973. + * use this for FS apps that can span multiple uframes
  88974. + */
  88975. +static int find_multi_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  88976. +{
  88977. + int i;
  88978. + int j;
  88979. + unsigned short utime;
  88980. + int t_left;
  88981. + int ret;
  88982. + int done;
  88983. + unsigned short xtime;
  88984. +
  88985. + ret = -1;
  88986. + utime = _qh->usecs;
  88987. + t_left = utime;
  88988. + i = 0;
  88989. + done = 0;
  88990. +loop:
  88991. + while (done == 0) {
  88992. + if(_hcd->frame_usecs[i] <= 0) {
  88993. + i++;
  88994. + if (i == 8) {
  88995. + done = 1;
  88996. + ret = -1;
  88997. + }
  88998. + goto loop;
  88999. + }
  89000. +
  89001. + /*
  89002. + * we need n consecutive slots
  89003. + * so use j as a start slot j plus j+1 must be enough time (for now)
  89004. + */
  89005. + xtime= _hcd->frame_usecs[i];
  89006. + for (j = i+1 ; j < 8 ; j++ ) {
  89007. + /*
  89008. + * if we add this frame remaining time to xtime we may
  89009. + * be OK, if not we need to test j for a complete frame
  89010. + */
  89011. + if ((xtime+_hcd->frame_usecs[j]) < utime) {
  89012. + if (_hcd->frame_usecs[j] < max_uframe_usecs[j]) {
  89013. + j = 8;
  89014. + ret = -1;
  89015. + continue;
  89016. + }
  89017. + }
  89018. + if (xtime >= utime) {
  89019. + ret = i;
  89020. + j = 8; /* stop loop with a good value ret */
  89021. + continue;
  89022. + }
  89023. + /* add the frame time to x time */
  89024. + xtime += _hcd->frame_usecs[j];
  89025. + /* we must have a fully available next frame or break */
  89026. + if ((xtime < utime)
  89027. + && (_hcd->frame_usecs[j] == max_uframe_usecs[j])) {
  89028. + ret = -1;
  89029. + j = 8; /* stop loop with a bad value ret */
  89030. + continue;
  89031. + }
  89032. + }
  89033. + if (ret >= 0) {
  89034. + t_left = utime;
  89035. + for (j = i; (t_left>0) && (j < 8); j++ ) {
  89036. + t_left -= _hcd->frame_usecs[j];
  89037. + if ( t_left <= 0 ) {
  89038. + _qh->frame_usecs[j] += _hcd->frame_usecs[j] + t_left;
  89039. + _hcd->frame_usecs[j]= -t_left;
  89040. + ret = i;
  89041. + done = 1;
  89042. + } else {
  89043. + _qh->frame_usecs[j] += _hcd->frame_usecs[j];
  89044. + _hcd->frame_usecs[j] = 0;
  89045. + }
  89046. + }
  89047. + } else {
  89048. + i++;
  89049. + if (i == 8) {
  89050. + done = 1;
  89051. + ret = -1;
  89052. + }
  89053. + }
  89054. + }
  89055. + return ret;
  89056. +}
  89057. +
  89058. +static int find_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  89059. +{
  89060. + int ret;
  89061. + ret = -1;
  89062. +
  89063. + if (_qh->speed == USB_SPEED_HIGH) {
  89064. + /* if this is a hs transaction we need a full frame */
  89065. + ret = find_single_uframe(_hcd, _qh);
  89066. + } else {
  89067. + /* if this is a fs transaction we may need a sequence of frames */
  89068. + ret = find_multi_uframe(_hcd, _qh);
  89069. + }
  89070. + return ret;
  89071. +}
  89072. +
  89073. +/**
  89074. + * Checks that the max transfer size allowed in a host channel is large enough
  89075. + * to handle the maximum data transfer in a single (micro)frame for a periodic
  89076. + * transfer.
  89077. + *
  89078. + * @param hcd The HCD state structure for the DWC OTG controller.
  89079. + * @param qh QH for a periodic endpoint.
  89080. + *
  89081. + * @return 0 if successful, negative error code otherwise.
  89082. + */
  89083. +static int check_max_xfer_size(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  89084. +{
  89085. + int status;
  89086. + uint32_t max_xfer_size;
  89087. + uint32_t max_channel_xfer_size;
  89088. +
  89089. + status = 0;
  89090. +
  89091. + max_xfer_size = dwc_max_packet(qh->maxp) * dwc_hb_mult(qh->maxp);
  89092. + max_channel_xfer_size = hcd->core_if->core_params->max_transfer_size;
  89093. +
  89094. + if (max_xfer_size > max_channel_xfer_size) {
  89095. + DWC_INFO("%s: Periodic xfer length %d > " "max xfer length for channel %d\n",
  89096. + __func__, max_xfer_size, max_channel_xfer_size); //NOTICE
  89097. + status = -DWC_E_NO_SPACE;
  89098. + }
  89099. +
  89100. + return status;
  89101. +}
  89102. +
  89103. +
  89104. +
  89105. +/**
  89106. + * Schedules an interrupt or isochronous transfer in the periodic schedule.
  89107. + *
  89108. + * @param hcd The HCD state structure for the DWC OTG controller.
  89109. + * @param qh QH for the periodic transfer. The QH should already contain the
  89110. + * scheduling information.
  89111. + *
  89112. + * @return 0 if successful, negative error code otherwise.
  89113. + */
  89114. +static int schedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  89115. +{
  89116. + int status = 0;
  89117. +
  89118. + if (microframe_schedule) {
  89119. + int frame;
  89120. + status = find_uframe(hcd, qh);
  89121. + frame = -1;
  89122. + if (status == 0) {
  89123. + frame = 7;
  89124. + } else {
  89125. + if (status > 0 )
  89126. + frame = status-1;
  89127. + }
  89128. +
  89129. + /* Set the new frame up */
  89130. + if (frame > -1) {
  89131. + qh->sched_frame &= ~0x7;
  89132. + qh->sched_frame |= (frame & 7);
  89133. + }
  89134. +
  89135. + if (status != -1)
  89136. + status = 0;
  89137. + } else {
  89138. + status = periodic_channel_available(hcd);
  89139. + if (status) {
  89140. + DWC_INFO("%s: No host channel available for periodic " "transfer.\n", __func__); //NOTICE
  89141. + return status;
  89142. + }
  89143. +
  89144. + status = check_periodic_bandwidth(hcd, qh);
  89145. + }
  89146. + if (status) {
  89147. + DWC_INFO("%s: Insufficient periodic bandwidth for "
  89148. + "periodic transfer.\n", __func__);
  89149. + return status;
  89150. + }
  89151. + status = check_max_xfer_size(hcd, qh);
  89152. + if (status) {
  89153. + DWC_INFO("%s: Channel max transfer size too small "
  89154. + "for periodic transfer.\n", __func__);
  89155. + return status;
  89156. + }
  89157. +
  89158. + if (hcd->core_if->dma_desc_enable) {
  89159. + /* Don't rely on SOF and start in ready schedule */
  89160. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_ready, &qh->qh_list_entry);
  89161. + }
  89162. + else {
  89163. + if(fiq_enable && (DWC_LIST_EMPTY(&hcd->periodic_sched_inactive) || dwc_frame_num_le(qh->sched_frame, hcd->fiq_state->next_sched_frame)))
  89164. + {
  89165. + hcd->fiq_state->next_sched_frame = qh->sched_frame;
  89166. +
  89167. + }
  89168. + /* Always start in the inactive schedule. */
  89169. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_inactive, &qh->qh_list_entry);
  89170. + }
  89171. +
  89172. + if (!microframe_schedule) {
  89173. + /* Reserve the periodic channel. */
  89174. + hcd->periodic_channels++;
  89175. + }
  89176. +
  89177. + /* Update claimed usecs per (micro)frame. */
  89178. + hcd->periodic_usecs += qh->usecs;
  89179. +
  89180. + return status;
  89181. +}
  89182. +
  89183. +
  89184. +/**
  89185. + * This function adds a QH to either the non periodic or periodic schedule if
  89186. + * it is not already in the schedule. If the QH is already in the schedule, no
  89187. + * action is taken.
  89188. + *
  89189. + * @return 0 if successful, negative error code otherwise.
  89190. + */
  89191. +int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  89192. +{
  89193. + int status = 0;
  89194. + gintmsk_data_t intr_mask = {.d32 = 0 };
  89195. +
  89196. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  89197. + /* QH already in a schedule. */
  89198. + return status;
  89199. + }
  89200. +
  89201. + /* Add the new QH to the appropriate schedule */
  89202. + if (dwc_qh_is_non_per(qh)) {
  89203. + /* Always start in the inactive schedule. */
  89204. + DWC_LIST_INSERT_TAIL(&hcd->non_periodic_sched_inactive,
  89205. + &qh->qh_list_entry);
  89206. + //hcd->fiq_state->kick_np_queues = 1;
  89207. + } else {
  89208. + status = schedule_periodic(hcd, qh);
  89209. + if ( !hcd->periodic_qh_count ) {
  89210. + intr_mask.b.sofintr = 1;
  89211. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk,
  89212. + intr_mask.d32, intr_mask.d32);
  89213. + }
  89214. + hcd->periodic_qh_count++;
  89215. + }
  89216. +
  89217. + return status;
  89218. +}
  89219. +
  89220. +/**
  89221. + * Removes an interrupt or isochronous transfer from the periodic schedule.
  89222. + *
  89223. + * @param hcd The HCD state structure for the DWC OTG controller.
  89224. + * @param qh QH for the periodic transfer.
  89225. + */
  89226. +static void deschedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  89227. +{
  89228. + int i;
  89229. + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
  89230. +
  89231. + /* Update claimed usecs per (micro)frame. */
  89232. + hcd->periodic_usecs -= qh->usecs;
  89233. +
  89234. + if (!microframe_schedule) {
  89235. + /* Release the periodic channel reservation. */
  89236. + hcd->periodic_channels--;
  89237. + } else {
  89238. + for (i = 0; i < 8; i++) {
  89239. + hcd->frame_usecs[i] += qh->frame_usecs[i];
  89240. + qh->frame_usecs[i] = 0;
  89241. + }
  89242. + }
  89243. +}
  89244. +
  89245. +/**
  89246. + * Removes a QH from either the non-periodic or periodic schedule. Memory is
  89247. + * not freed.
  89248. + *
  89249. + * @param hcd The HCD state structure.
  89250. + * @param qh QH to remove from schedule. */
  89251. +void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  89252. +{
  89253. + gintmsk_data_t intr_mask = {.d32 = 0 };
  89254. +
  89255. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  89256. + /* QH is not in a schedule. */
  89257. + return;
  89258. + }
  89259. +
  89260. + if (dwc_qh_is_non_per(qh)) {
  89261. + if (hcd->non_periodic_qh_ptr == &qh->qh_list_entry) {
  89262. + hcd->non_periodic_qh_ptr =
  89263. + hcd->non_periodic_qh_ptr->next;
  89264. + }
  89265. + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
  89266. + //if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive))
  89267. + // hcd->fiq_state->kick_np_queues = 1;
  89268. + } else {
  89269. + deschedule_periodic(hcd, qh);
  89270. + hcd->periodic_qh_count--;
  89271. + if( !hcd->periodic_qh_count && !fiq_fsm_enable ) {
  89272. + intr_mask.b.sofintr = 1;
  89273. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk,
  89274. + intr_mask.d32, 0);
  89275. + }
  89276. + }
  89277. +}
  89278. +
  89279. +/**
  89280. + * Deactivates a QH. For non-periodic QHs, removes the QH from the active
  89281. + * non-periodic schedule. The QH is added to the inactive non-periodic
  89282. + * schedule if any QTDs are still attached to the QH.
  89283. + *
  89284. + * For periodic QHs, the QH is removed from the periodic queued schedule. If
  89285. + * there are any QTDs still attached to the QH, the QH is added to either the
  89286. + * periodic inactive schedule or the periodic ready schedule and its next
  89287. + * scheduled frame is calculated. The QH is placed in the ready schedule if
  89288. + * the scheduled frame has been reached already. Otherwise it's placed in the
  89289. + * inactive schedule. If there are no QTDs attached to the QH, the QH is
  89290. + * completely removed from the periodic schedule.
  89291. + */
  89292. +void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  89293. + int sched_next_periodic_split)
  89294. +{
  89295. + if (dwc_qh_is_non_per(qh)) {
  89296. + dwc_otg_hcd_qh_remove(hcd, qh);
  89297. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  89298. + /* Add back to inactive non-periodic schedule. */
  89299. + dwc_otg_hcd_qh_add(hcd, qh);
  89300. + //hcd->fiq_state->kick_np_queues = 1;
  89301. + }
  89302. + } else {
  89303. + uint16_t frame_number = dwc_otg_hcd_get_frame_number(hcd);
  89304. +
  89305. + if (qh->do_split) {
  89306. + /* Schedule the next continuing periodic split transfer */
  89307. + if (sched_next_periodic_split) {
  89308. +
  89309. + qh->sched_frame = frame_number;
  89310. +
  89311. + if (dwc_frame_num_le(frame_number,
  89312. + dwc_frame_num_inc
  89313. + (qh->start_split_frame,
  89314. + 1))) {
  89315. + /*
  89316. + * Allow one frame to elapse after start
  89317. + * split microframe before scheduling
  89318. + * complete split, but DONT if we are
  89319. + * doing the next start split in the
  89320. + * same frame for an ISOC out.
  89321. + */
  89322. + if ((qh->ep_type != UE_ISOCHRONOUS) ||
  89323. + (qh->ep_is_in != 0)) {
  89324. + qh->sched_frame =
  89325. + dwc_frame_num_inc(qh->sched_frame, 1);
  89326. + }
  89327. + }
  89328. + } else {
  89329. + qh->sched_frame =
  89330. + dwc_frame_num_inc(qh->start_split_frame,
  89331. + qh->interval);
  89332. + if (dwc_frame_num_le
  89333. + (qh->sched_frame, frame_number)) {
  89334. + qh->sched_frame = frame_number;
  89335. + }
  89336. + qh->sched_frame |= 0x7;
  89337. + qh->start_split_frame = qh->sched_frame;
  89338. + }
  89339. + } else {
  89340. + qh->sched_frame =
  89341. + dwc_frame_num_inc(qh->sched_frame, qh->interval);
  89342. + if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
  89343. + qh->sched_frame = frame_number;
  89344. + }
  89345. + }
  89346. +
  89347. + if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  89348. + dwc_otg_hcd_qh_remove(hcd, qh);
  89349. + } else {
  89350. + /*
  89351. + * Remove from periodic_sched_queued and move to
  89352. + * appropriate queue.
  89353. + */
  89354. + if ((microframe_schedule && dwc_frame_num_le(qh->sched_frame, frame_number)) ||
  89355. + (!microframe_schedule && qh->sched_frame == frame_number)) {
  89356. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  89357. + &qh->qh_list_entry);
  89358. + } else {
  89359. + if(fiq_enable && !dwc_frame_num_le(hcd->fiq_state->next_sched_frame, qh->sched_frame))
  89360. + {
  89361. + hcd->fiq_state->next_sched_frame = qh->sched_frame;
  89362. + }
  89363. +
  89364. + DWC_LIST_MOVE_HEAD
  89365. + (&hcd->periodic_sched_inactive,
  89366. + &qh->qh_list_entry);
  89367. + }
  89368. + }
  89369. + }
  89370. +}
  89371. +
  89372. +/**
  89373. + * This function allocates and initializes a QTD.
  89374. + *
  89375. + * @param urb The URB to create a QTD from. Each URB-QTD pair will end up
  89376. + * pointing to each other so each pair should have a unique correlation.
  89377. + * @param atomic_alloc Flag to do atomic alloc if needed
  89378. + *
  89379. + * @return Returns pointer to the newly allocated QTD, or NULL on error. */
  89380. +dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  89381. +{
  89382. + dwc_otg_qtd_t *qtd;
  89383. +
  89384. + qtd = dwc_otg_hcd_qtd_alloc(atomic_alloc);
  89385. + if (qtd == NULL) {
  89386. + return NULL;
  89387. + }
  89388. +
  89389. + dwc_otg_hcd_qtd_init(qtd, urb);
  89390. + return qtd;
  89391. +}
  89392. +
  89393. +/**
  89394. + * Initializes a QTD structure.
  89395. + *
  89396. + * @param qtd The QTD to initialize.
  89397. + * @param urb The URB to use for initialization. */
  89398. +void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb)
  89399. +{
  89400. + dwc_memset(qtd, 0, sizeof(dwc_otg_qtd_t));
  89401. + qtd->urb = urb;
  89402. + if (dwc_otg_hcd_get_pipe_type(&urb->pipe_info) == UE_CONTROL) {
  89403. + /*
  89404. + * The only time the QTD data toggle is used is on the data
  89405. + * phase of control transfers. This phase always starts with
  89406. + * DATA1.
  89407. + */
  89408. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  89409. + qtd->control_phase = DWC_OTG_CONTROL_SETUP;
  89410. + }
  89411. +
  89412. + /* start split */
  89413. + qtd->complete_split = 0;
  89414. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  89415. + qtd->isoc_split_offset = 0;
  89416. + qtd->in_process = 0;
  89417. +
  89418. + /* Store the qtd ptr in the urb to reference what QTD. */
  89419. + urb->qtd = qtd;
  89420. + return;
  89421. +}
  89422. +
  89423. +/**
  89424. + * This function adds a QTD to the QTD-list of a QH. It will find the correct
  89425. + * QH to place the QTD into. If it does not find a QH, then it will create a
  89426. + * new QH. If the QH to which the QTD is added is not currently scheduled, it
  89427. + * is placed into the proper schedule based on its EP type.
  89428. + * HCD lock must be held and interrupts must be disabled on entry
  89429. + *
  89430. + * @param[in] qtd The QTD to add
  89431. + * @param[in] hcd The DWC HCD structure
  89432. + * @param[out] qh out parameter to return queue head
  89433. + * @param atomic_alloc Flag to do atomic alloc if needed
  89434. + *
  89435. + * @return 0 if successful, negative error code otherwise.
  89436. + */
  89437. +int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd,
  89438. + dwc_otg_hcd_t * hcd, dwc_otg_qh_t ** qh, int atomic_alloc)
  89439. +{
  89440. + int retval = 0;
  89441. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  89442. +
  89443. + /*
  89444. + * Get the QH which holds the QTD-list to insert to. Create QH if it
  89445. + * doesn't exist.
  89446. + */
  89447. + if (*qh == NULL) {
  89448. + *qh = dwc_otg_hcd_qh_create(hcd, urb, atomic_alloc);
  89449. + if (*qh == NULL) {
  89450. + retval = -DWC_E_NO_MEMORY;
  89451. + goto done;
  89452. + } else {
  89453. + if (fiq_enable)
  89454. + hcd->fiq_state->kick_np_queues = 1;
  89455. + }
  89456. + }
  89457. + retval = dwc_otg_hcd_qh_add(hcd, *qh);
  89458. + if (retval == 0) {
  89459. + DWC_CIRCLEQ_INSERT_TAIL(&((*qh)->qtd_list), qtd,
  89460. + qtd_list_entry);
  89461. + qtd->qh = *qh;
  89462. + }
  89463. +done:
  89464. +
  89465. + return retval;
  89466. +}
  89467. +
  89468. +#endif /* DWC_DEVICE_ONLY */
  89469. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
  89470. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h 1969-12-31 18:00:00.000000000 -0600
  89471. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h 2014-12-03 19:13:40.220418001 -0600
  89472. @@ -0,0 +1,188 @@
  89473. +#ifndef _DWC_OS_DEP_H_
  89474. +#define _DWC_OS_DEP_H_
  89475. +
  89476. +/**
  89477. + * @file
  89478. + *
  89479. + * This file contains OS dependent structures.
  89480. + *
  89481. + */
  89482. +
  89483. +#include <linux/kernel.h>
  89484. +#include <linux/module.h>
  89485. +#include <linux/moduleparam.h>
  89486. +#include <linux/init.h>
  89487. +#include <linux/device.h>
  89488. +#include <linux/errno.h>
  89489. +#include <linux/types.h>
  89490. +#include <linux/slab.h>
  89491. +#include <linux/list.h>
  89492. +#include <linux/interrupt.h>
  89493. +#include <linux/ctype.h>
  89494. +#include <linux/string.h>
  89495. +#include <linux/dma-mapping.h>
  89496. +#include <linux/jiffies.h>
  89497. +#include <linux/delay.h>
  89498. +#include <linux/timer.h>
  89499. +#include <linux/workqueue.h>
  89500. +#include <linux/stat.h>
  89501. +#include <linux/pci.h>
  89502. +
  89503. +#include <linux/version.h>
  89504. +
  89505. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  89506. +# include <linux/irq.h>
  89507. +#endif
  89508. +
  89509. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  89510. +# include <linux/usb/ch9.h>
  89511. +#else
  89512. +# include <linux/usb_ch9.h>
  89513. +#endif
  89514. +
  89515. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  89516. +# include <linux/usb/gadget.h>
  89517. +#else
  89518. +# include <linux/usb_gadget.h>
  89519. +#endif
  89520. +
  89521. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
  89522. +# include <asm/irq.h>
  89523. +#endif
  89524. +
  89525. +#ifdef PCI_INTERFACE
  89526. +# include <asm/io.h>
  89527. +#endif
  89528. +
  89529. +#ifdef LM_INTERFACE
  89530. +# include <asm/unaligned.h>
  89531. +# include <asm/sizes.h>
  89532. +# include <asm/param.h>
  89533. +# include <asm/io.h>
  89534. +# if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  89535. +# include <asm/arch/hardware.h>
  89536. +# include <asm/arch/lm.h>
  89537. +# include <asm/arch/irqs.h>
  89538. +# include <asm/arch/regs-irq.h>
  89539. +# else
  89540. +/* in 2.6.31, at least, we seem to have lost the generic LM infrastructure -
  89541. + here we assume that the machine architecture provides definitions
  89542. + in its own header
  89543. +*/
  89544. +# include <mach/lm.h>
  89545. +# include <mach/hardware.h>
  89546. +# endif
  89547. +#endif
  89548. +
  89549. +#ifdef PLATFORM_INTERFACE
  89550. +#include <linux/platform_device.h>
  89551. +#include <asm/mach/map.h>
  89552. +#endif
  89553. +
  89554. +/** The OS page size */
  89555. +#define DWC_OS_PAGE_SIZE PAGE_SIZE
  89556. +
  89557. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
  89558. +typedef int gfp_t;
  89559. +#endif
  89560. +
  89561. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
  89562. +# define IRQF_SHARED SA_SHIRQ
  89563. +#endif
  89564. +
  89565. +typedef struct os_dependent {
  89566. + /** Base address returned from ioremap() */
  89567. + void *base;
  89568. +
  89569. + /** Register offset for Diagnostic API */
  89570. + uint32_t reg_offset;
  89571. +
  89572. + /** Base address for MPHI peripheral */
  89573. + void *mphi_base;
  89574. +
  89575. +#ifdef LM_INTERFACE
  89576. + struct lm_device *lmdev;
  89577. +#elif defined(PCI_INTERFACE)
  89578. + struct pci_dev *pcidev;
  89579. +
  89580. + /** Start address of a PCI region */
  89581. + resource_size_t rsrc_start;
  89582. +
  89583. + /** Length address of a PCI region */
  89584. + resource_size_t rsrc_len;
  89585. +#elif defined(PLATFORM_INTERFACE)
  89586. + struct platform_device *platformdev;
  89587. +#endif
  89588. +
  89589. +} os_dependent_t;
  89590. +
  89591. +#ifdef __cplusplus
  89592. +}
  89593. +#endif
  89594. +
  89595. +
  89596. +
  89597. +/* Type for the our device on the chosen bus */
  89598. +#if defined(LM_INTERFACE)
  89599. +typedef struct lm_device dwc_bus_dev_t;
  89600. +#elif defined(PCI_INTERFACE)
  89601. +typedef struct pci_dev dwc_bus_dev_t;
  89602. +#elif defined(PLATFORM_INTERFACE)
  89603. +typedef struct platform_device dwc_bus_dev_t;
  89604. +#endif
  89605. +
  89606. +/* Helper macro to retrieve drvdata from the device on the chosen bus */
  89607. +#if defined(LM_INTERFACE)
  89608. +#define DWC_OTG_BUSDRVDATA(_dev) lm_get_drvdata(_dev)
  89609. +#elif defined(PCI_INTERFACE)
  89610. +#define DWC_OTG_BUSDRVDATA(_dev) pci_get_drvdata(_dev)
  89611. +#elif defined(PLATFORM_INTERFACE)
  89612. +#define DWC_OTG_BUSDRVDATA(_dev) platform_get_drvdata(_dev)
  89613. +#endif
  89614. +
  89615. +/**
  89616. + * Helper macro returning the otg_device structure of a given struct device
  89617. + *
  89618. + * c.f. static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  89619. + */
  89620. +#ifdef LM_INTERFACE
  89621. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  89622. + struct lm_device *lm_dev = \
  89623. + container_of(_dev, struct lm_device, dev); \
  89624. + _var = lm_get_drvdata(lm_dev); \
  89625. + } while (0)
  89626. +
  89627. +#elif defined(PCI_INTERFACE)
  89628. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  89629. + _var = dev_get_drvdata(_dev); \
  89630. + } while (0)
  89631. +
  89632. +#elif defined(PLATFORM_INTERFACE)
  89633. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  89634. + struct platform_device *platform_dev = \
  89635. + container_of(_dev, struct platform_device, dev); \
  89636. + _var = platform_get_drvdata(platform_dev); \
  89637. + } while (0)
  89638. +#endif
  89639. +
  89640. +
  89641. +/**
  89642. + * Helper macro returning the struct dev of the given struct os_dependent
  89643. + *
  89644. + * c.f. static struct device *dwc_otg_getdev(struct os_dependent *osdep)
  89645. + */
  89646. +#ifdef LM_INTERFACE
  89647. +#define DWC_OTG_OS_GETDEV(_osdep) \
  89648. + ((_osdep).lmdev == NULL? NULL: &(_osdep).lmdev->dev)
  89649. +#elif defined(PCI_INTERFACE)
  89650. +#define DWC_OTG_OS_GETDEV(_osdep) \
  89651. + ((_osdep).pci_dev == NULL? NULL: &(_osdep).pci_dev->dev)
  89652. +#elif defined(PLATFORM_INTERFACE)
  89653. +#define DWC_OTG_OS_GETDEV(_osdep) \
  89654. + ((_osdep).platformdev == NULL? NULL: &(_osdep).platformdev->dev)
  89655. +#endif
  89656. +
  89657. +
  89658. +
  89659. +
  89660. +#endif /* _DWC_OS_DEP_H_ */
  89661. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_pcd.c linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd.c
  89662. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_pcd.c 1969-12-31 18:00:00.000000000 -0600
  89663. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd.c 2014-12-03 19:13:40.220418001 -0600
  89664. @@ -0,0 +1,2708 @@
  89665. +/* ==========================================================================
  89666. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.c $
  89667. + * $Revision: #101 $
  89668. + * $Date: 2012/08/10 $
  89669. + * $Change: 2047372 $
  89670. + *
  89671. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  89672. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  89673. + * otherwise expressly agreed to in writing between Synopsys and you.
  89674. + *
  89675. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  89676. + * any End User Software License Agreement or Agreement for Licensed Product
  89677. + * with Synopsys or any supplement thereto. You are permitted to use and
  89678. + * redistribute this Software in source and binary forms, with or without
  89679. + * modification, provided that redistributions of source code must retain this
  89680. + * notice. You may not view, use, disclose, copy or distribute this file or
  89681. + * any information contained herein except pursuant to this license grant from
  89682. + * Synopsys. If you do not agree with this notice, including the disclaimer
  89683. + * below, then you are not authorized to use the Software.
  89684. + *
  89685. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  89686. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  89687. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  89688. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  89689. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  89690. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  89691. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  89692. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  89693. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  89694. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  89695. + * DAMAGE.
  89696. + * ========================================================================== */
  89697. +#ifndef DWC_HOST_ONLY
  89698. +
  89699. +/** @file
  89700. + * This file implements PCD Core. All code in this file is portable and doesn't
  89701. + * use any OS specific functions.
  89702. + * PCD Core provides Interface, defined in <code><dwc_otg_pcd_if.h></code>
  89703. + * header file, which can be used to implement OS specific PCD interface.
  89704. + *
  89705. + * An important function of the PCD is managing interrupts generated
  89706. + * by the DWC_otg controller. The implementation of the DWC_otg device
  89707. + * mode interrupt service routines is in dwc_otg_pcd_intr.c.
  89708. + *
  89709. + * @todo Add Device Mode test modes (Test J mode, Test K mode, etc).
  89710. + * @todo Does it work when the request size is greater than DEPTSIZ
  89711. + * transfer size
  89712. + *
  89713. + */
  89714. +
  89715. +#include "dwc_otg_pcd.h"
  89716. +
  89717. +#ifdef DWC_UTE_CFI
  89718. +#include "dwc_otg_cfi.h"
  89719. +
  89720. +extern int init_cfi(cfiobject_t * cfiobj);
  89721. +#endif
  89722. +
  89723. +/**
  89724. + * Choose endpoint from ep arrays using usb_ep structure.
  89725. + */
  89726. +static dwc_otg_pcd_ep_t *get_ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  89727. +{
  89728. + int i;
  89729. + if (pcd->ep0.priv == handle) {
  89730. + return &pcd->ep0;
  89731. + }
  89732. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  89733. + if (pcd->in_ep[i].priv == handle)
  89734. + return &pcd->in_ep[i];
  89735. + if (pcd->out_ep[i].priv == handle)
  89736. + return &pcd->out_ep[i];
  89737. + }
  89738. +
  89739. + return NULL;
  89740. +}
  89741. +
  89742. +/**
  89743. + * This function completes a request. It call's the request call back.
  89744. + */
  89745. +void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req,
  89746. + int32_t status)
  89747. +{
  89748. + unsigned stopped = ep->stopped;
  89749. +
  89750. + DWC_DEBUGPL(DBG_PCDV, "%s(ep %p req %p)\n", __func__, ep, req);
  89751. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  89752. +
  89753. + /* don't modify queue heads during completion callback */
  89754. + ep->stopped = 1;
  89755. + /* spin_unlock/spin_lock now done in fops->complete() */
  89756. + ep->pcd->fops->complete(ep->pcd, ep->priv, req->priv, status,
  89757. + req->actual);
  89758. +
  89759. + if (ep->pcd->request_pending > 0) {
  89760. + --ep->pcd->request_pending;
  89761. + }
  89762. +
  89763. + ep->stopped = stopped;
  89764. + DWC_FREE(req);
  89765. +}
  89766. +
  89767. +/**
  89768. + * This function terminates all the requsts in the EP request queue.
  89769. + */
  89770. +void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep)
  89771. +{
  89772. + dwc_otg_pcd_request_t *req;
  89773. +
  89774. + ep->stopped = 1;
  89775. +
  89776. + /* called with irqs blocked?? */
  89777. + while (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  89778. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  89779. + dwc_otg_request_done(ep, req, -DWC_E_SHUTDOWN);
  89780. + }
  89781. +}
  89782. +
  89783. +void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  89784. + const struct dwc_otg_pcd_function_ops *fops)
  89785. +{
  89786. + pcd->fops = fops;
  89787. +}
  89788. +
  89789. +/**
  89790. + * PCD Callback function for initializing the PCD when switching to
  89791. + * device mode.
  89792. + *
  89793. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  89794. + */
  89795. +static int32_t dwc_otg_pcd_start_cb(void *p)
  89796. +{
  89797. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  89798. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  89799. +
  89800. + /*
  89801. + * Initialized the Core for Device mode.
  89802. + */
  89803. + if (dwc_otg_is_device_mode(core_if)) {
  89804. + dwc_otg_core_dev_init(core_if);
  89805. + /* Set core_if's lock pointer to the pcd->lock */
  89806. + core_if->lock = pcd->lock;
  89807. + }
  89808. + return 1;
  89809. +}
  89810. +
  89811. +/** CFI-specific buffer allocation function for EP */
  89812. +#ifdef DWC_UTE_CFI
  89813. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  89814. + size_t buflen, int flags)
  89815. +{
  89816. + dwc_otg_pcd_ep_t *ep;
  89817. + ep = get_ep_from_handle(pcd, pep);
  89818. + if (!ep) {
  89819. + DWC_WARN("bad ep\n");
  89820. + return -DWC_E_INVALID;
  89821. + }
  89822. +
  89823. + return pcd->cfi->ops.ep_alloc_buf(pcd->cfi, pcd, ep, addr, buflen,
  89824. + flags);
  89825. +}
  89826. +#else
  89827. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  89828. + size_t buflen, int flags);
  89829. +#endif
  89830. +
  89831. +/**
  89832. + * PCD Callback function for notifying the PCD when resuming from
  89833. + * suspend.
  89834. + *
  89835. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  89836. + */
  89837. +static int32_t dwc_otg_pcd_resume_cb(void *p)
  89838. +{
  89839. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  89840. +
  89841. + if (pcd->fops->resume) {
  89842. + pcd->fops->resume(pcd);
  89843. + }
  89844. +
  89845. + /* Stop the SRP timeout timer. */
  89846. + if ((GET_CORE_IF(pcd)->core_params->phy_type != DWC_PHY_TYPE_PARAM_FS)
  89847. + || (!GET_CORE_IF(pcd)->core_params->i2c_enable)) {
  89848. + if (GET_CORE_IF(pcd)->srp_timer_started) {
  89849. + GET_CORE_IF(pcd)->srp_timer_started = 0;
  89850. + DWC_TIMER_CANCEL(GET_CORE_IF(pcd)->srp_timer);
  89851. + }
  89852. + }
  89853. + return 1;
  89854. +}
  89855. +
  89856. +/**
  89857. + * PCD Callback function for notifying the PCD device is suspended.
  89858. + *
  89859. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  89860. + */
  89861. +static int32_t dwc_otg_pcd_suspend_cb(void *p)
  89862. +{
  89863. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  89864. +
  89865. + if (pcd->fops->suspend) {
  89866. + DWC_SPINUNLOCK(pcd->lock);
  89867. + pcd->fops->suspend(pcd);
  89868. + DWC_SPINLOCK(pcd->lock);
  89869. + }
  89870. +
  89871. + return 1;
  89872. +}
  89873. +
  89874. +/**
  89875. + * PCD Callback function for stopping the PCD when switching to Host
  89876. + * mode.
  89877. + *
  89878. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  89879. + */
  89880. +static int32_t dwc_otg_pcd_stop_cb(void *p)
  89881. +{
  89882. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  89883. + extern void dwc_otg_pcd_stop(dwc_otg_pcd_t * _pcd);
  89884. +
  89885. + dwc_otg_pcd_stop(pcd);
  89886. + return 1;
  89887. +}
  89888. +
  89889. +/**
  89890. + * PCD Callback structure for handling mode switching.
  89891. + */
  89892. +static dwc_otg_cil_callbacks_t pcd_callbacks = {
  89893. + .start = dwc_otg_pcd_start_cb,
  89894. + .stop = dwc_otg_pcd_stop_cb,
  89895. + .suspend = dwc_otg_pcd_suspend_cb,
  89896. + .resume_wakeup = dwc_otg_pcd_resume_cb,
  89897. + .p = 0, /* Set at registration */
  89898. +};
  89899. +
  89900. +/**
  89901. + * This function allocates a DMA Descriptor chain for the Endpoint
  89902. + * buffer to be used for a transfer to/from the specified endpoint.
  89903. + */
  89904. +dwc_otg_dev_dma_desc_t *dwc_otg_ep_alloc_desc_chain(dwc_dma_t * dma_desc_addr,
  89905. + uint32_t count)
  89906. +{
  89907. + return DWC_DMA_ALLOC_ATOMIC(count * sizeof(dwc_otg_dev_dma_desc_t),
  89908. + dma_desc_addr);
  89909. +}
  89910. +
  89911. +/**
  89912. + * This function frees a DMA Descriptor chain that was allocated by ep_alloc_desc.
  89913. + */
  89914. +void dwc_otg_ep_free_desc_chain(dwc_otg_dev_dma_desc_t * desc_addr,
  89915. + uint32_t dma_desc_addr, uint32_t count)
  89916. +{
  89917. + DWC_DMA_FREE(count * sizeof(dwc_otg_dev_dma_desc_t), desc_addr,
  89918. + dma_desc_addr);
  89919. +}
  89920. +
  89921. +#ifdef DWC_EN_ISOC
  89922. +
  89923. +/**
  89924. + * This function initializes a descriptor chain for Isochronous transfer
  89925. + *
  89926. + * @param core_if Programming view of DWC_otg controller.
  89927. + * @param dwc_ep The EP to start the transfer on.
  89928. + *
  89929. + */
  89930. +void dwc_otg_iso_ep_start_ddma_transfer(dwc_otg_core_if_t * core_if,
  89931. + dwc_ep_t * dwc_ep)
  89932. +{
  89933. +
  89934. + dsts_data_t dsts = {.d32 = 0 };
  89935. + depctl_data_t depctl = {.d32 = 0 };
  89936. + volatile uint32_t *addr;
  89937. + int i, j;
  89938. + uint32_t len;
  89939. +
  89940. + if (dwc_ep->is_in)
  89941. + dwc_ep->desc_cnt = dwc_ep->buf_proc_intrvl / dwc_ep->bInterval;
  89942. + else
  89943. + dwc_ep->desc_cnt =
  89944. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  89945. + dwc_ep->bInterval;
  89946. +
  89947. + /** Allocate descriptors for double buffering */
  89948. + dwc_ep->iso_desc_addr =
  89949. + dwc_otg_ep_alloc_desc_chain(&dwc_ep->iso_dma_desc_addr,
  89950. + dwc_ep->desc_cnt * 2);
  89951. + if (dwc_ep->desc_addr) {
  89952. + DWC_WARN("%s, can't allocate DMA descriptor chain\n", __func__);
  89953. + return;
  89954. + }
  89955. +
  89956. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  89957. +
  89958. + /** ISO OUT EP */
  89959. + if (dwc_ep->is_in == 0) {
  89960. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  89961. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  89962. + dma_addr_t dma_ad;
  89963. + uint32_t data_per_desc;
  89964. + dwc_otg_dev_out_ep_regs_t *out_regs =
  89965. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  89966. + int offset;
  89967. +
  89968. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  89969. + dma_ad = (dma_addr_t) DWC_READ_REG32(&(out_regs->doepdma));
  89970. +
  89971. + /** Buffer 0 descriptors setup */
  89972. + dma_ad = dwc_ep->dma_addr0;
  89973. +
  89974. + sts.b_iso_out.bs = BS_HOST_READY;
  89975. + sts.b_iso_out.rxsts = 0;
  89976. + sts.b_iso_out.l = 0;
  89977. + sts.b_iso_out.sp = 0;
  89978. + sts.b_iso_out.ioc = 0;
  89979. + sts.b_iso_out.pid = 0;
  89980. + sts.b_iso_out.framenum = 0;
  89981. +
  89982. + offset = 0;
  89983. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  89984. + i += dwc_ep->pkt_per_frm) {
  89985. +
  89986. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  89987. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  89988. + if (len > dwc_ep->data_per_frame)
  89989. + data_per_desc =
  89990. + dwc_ep->data_per_frame -
  89991. + j * dwc_ep->maxpacket;
  89992. + else
  89993. + data_per_desc = dwc_ep->maxpacket;
  89994. + len = data_per_desc % 4;
  89995. + if (len)
  89996. + data_per_desc += 4 - len;
  89997. +
  89998. + sts.b_iso_out.rxbytes = data_per_desc;
  89999. + dma_desc->buf = dma_ad;
  90000. + dma_desc->status.d32 = sts.d32;
  90001. +
  90002. + offset += data_per_desc;
  90003. + dma_desc++;
  90004. + dma_ad += data_per_desc;
  90005. + }
  90006. + }
  90007. +
  90008. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  90009. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  90010. + if (len > dwc_ep->data_per_frame)
  90011. + data_per_desc =
  90012. + dwc_ep->data_per_frame -
  90013. + j * dwc_ep->maxpacket;
  90014. + else
  90015. + data_per_desc = dwc_ep->maxpacket;
  90016. + len = data_per_desc % 4;
  90017. + if (len)
  90018. + data_per_desc += 4 - len;
  90019. + sts.b_iso_out.rxbytes = data_per_desc;
  90020. + dma_desc->buf = dma_ad;
  90021. + dma_desc->status.d32 = sts.d32;
  90022. +
  90023. + offset += data_per_desc;
  90024. + dma_desc++;
  90025. + dma_ad += data_per_desc;
  90026. + }
  90027. +
  90028. + sts.b_iso_out.ioc = 1;
  90029. + len = (j + 1) * dwc_ep->maxpacket;
  90030. + if (len > dwc_ep->data_per_frame)
  90031. + data_per_desc =
  90032. + dwc_ep->data_per_frame - j * dwc_ep->maxpacket;
  90033. + else
  90034. + data_per_desc = dwc_ep->maxpacket;
  90035. + len = data_per_desc % 4;
  90036. + if (len)
  90037. + data_per_desc += 4 - len;
  90038. + sts.b_iso_out.rxbytes = data_per_desc;
  90039. +
  90040. + dma_desc->buf = dma_ad;
  90041. + dma_desc->status.d32 = sts.d32;
  90042. + dma_desc++;
  90043. +
  90044. + /** Buffer 1 descriptors setup */
  90045. + sts.b_iso_out.ioc = 0;
  90046. + dma_ad = dwc_ep->dma_addr1;
  90047. +
  90048. + offset = 0;
  90049. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  90050. + i += dwc_ep->pkt_per_frm) {
  90051. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  90052. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  90053. + if (len > dwc_ep->data_per_frame)
  90054. + data_per_desc =
  90055. + dwc_ep->data_per_frame -
  90056. + j * dwc_ep->maxpacket;
  90057. + else
  90058. + data_per_desc = dwc_ep->maxpacket;
  90059. + len = data_per_desc % 4;
  90060. + if (len)
  90061. + data_per_desc += 4 - len;
  90062. +
  90063. + data_per_desc =
  90064. + sts.b_iso_out.rxbytes = data_per_desc;
  90065. + dma_desc->buf = dma_ad;
  90066. + dma_desc->status.d32 = sts.d32;
  90067. +
  90068. + offset += data_per_desc;
  90069. + dma_desc++;
  90070. + dma_ad += data_per_desc;
  90071. + }
  90072. + }
  90073. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  90074. + data_per_desc =
  90075. + ((j + 1) * dwc_ep->maxpacket >
  90076. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  90077. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  90078. + data_per_desc +=
  90079. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  90080. + sts.b_iso_out.rxbytes = data_per_desc;
  90081. + dma_desc->buf = dma_ad;
  90082. + dma_desc->status.d32 = sts.d32;
  90083. +
  90084. + offset += data_per_desc;
  90085. + dma_desc++;
  90086. + dma_ad += data_per_desc;
  90087. + }
  90088. +
  90089. + sts.b_iso_out.ioc = 1;
  90090. + sts.b_iso_out.l = 1;
  90091. + data_per_desc =
  90092. + ((j + 1) * dwc_ep->maxpacket >
  90093. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  90094. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  90095. + data_per_desc +=
  90096. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  90097. + sts.b_iso_out.rxbytes = data_per_desc;
  90098. +
  90099. + dma_desc->buf = dma_ad;
  90100. + dma_desc->status.d32 = sts.d32;
  90101. +
  90102. + dwc_ep->next_frame = 0;
  90103. +
  90104. + /** Write dma_ad into DOEPDMA register */
  90105. + DWC_WRITE_REG32(&(out_regs->doepdma),
  90106. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  90107. +
  90108. + }
  90109. + /** ISO IN EP */
  90110. + else {
  90111. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  90112. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  90113. + dma_addr_t dma_ad;
  90114. + dwc_otg_dev_in_ep_regs_t *in_regs =
  90115. + core_if->dev_if->in_ep_regs[dwc_ep->num];
  90116. + unsigned int frmnumber;
  90117. + fifosize_data_t txfifosize, rxfifosize;
  90118. +
  90119. + txfifosize.d32 =
  90120. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[dwc_ep->num]->
  90121. + dtxfsts);
  90122. + rxfifosize.d32 =
  90123. + DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  90124. +
  90125. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  90126. +
  90127. + dma_ad = dwc_ep->dma_addr0;
  90128. +
  90129. + dsts.d32 =
  90130. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  90131. +
  90132. + sts.b_iso_in.bs = BS_HOST_READY;
  90133. + sts.b_iso_in.txsts = 0;
  90134. + sts.b_iso_in.sp =
  90135. + (dwc_ep->data_per_frame % dwc_ep->maxpacket) ? 1 : 0;
  90136. + sts.b_iso_in.ioc = 0;
  90137. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  90138. +
  90139. + frmnumber = dwc_ep->next_frame;
  90140. +
  90141. + sts.b_iso_in.framenum = frmnumber;
  90142. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  90143. + sts.b_iso_in.l = 0;
  90144. +
  90145. + /** Buffer 0 descriptors setup */
  90146. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  90147. + dma_desc->buf = dma_ad;
  90148. + dma_desc->status.d32 = sts.d32;
  90149. + dma_desc++;
  90150. +
  90151. + dma_ad += dwc_ep->data_per_frame;
  90152. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  90153. + }
  90154. +
  90155. + sts.b_iso_in.ioc = 1;
  90156. + dma_desc->buf = dma_ad;
  90157. + dma_desc->status.d32 = sts.d32;
  90158. + ++dma_desc;
  90159. +
  90160. + /** Buffer 1 descriptors setup */
  90161. + sts.b_iso_in.ioc = 0;
  90162. + dma_ad = dwc_ep->dma_addr1;
  90163. +
  90164. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  90165. + i += dwc_ep->pkt_per_frm) {
  90166. + dma_desc->buf = dma_ad;
  90167. + dma_desc->status.d32 = sts.d32;
  90168. + dma_desc++;
  90169. +
  90170. + dma_ad += dwc_ep->data_per_frame;
  90171. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  90172. +
  90173. + sts.b_iso_in.ioc = 0;
  90174. + }
  90175. + sts.b_iso_in.ioc = 1;
  90176. + sts.b_iso_in.l = 1;
  90177. +
  90178. + dma_desc->buf = dma_ad;
  90179. + dma_desc->status.d32 = sts.d32;
  90180. +
  90181. + dwc_ep->next_frame = sts.b_iso_in.framenum + dwc_ep->bInterval;
  90182. +
  90183. + /** Write dma_ad into diepdma register */
  90184. + DWC_WRITE_REG32(&(in_regs->diepdma),
  90185. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  90186. + }
  90187. + /** Enable endpoint, clear nak */
  90188. + depctl.d32 = 0;
  90189. + depctl.b.epena = 1;
  90190. + depctl.b.usbactep = 1;
  90191. + depctl.b.cnak = 1;
  90192. +
  90193. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  90194. + depctl.d32 = DWC_READ_REG32(addr);
  90195. +}
  90196. +
  90197. +/**
  90198. + * This function initializes a descriptor chain for Isochronous transfer
  90199. + *
  90200. + * @param core_if Programming view of DWC_otg controller.
  90201. + * @param ep The EP to start the transfer on.
  90202. + *
  90203. + */
  90204. +void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  90205. + dwc_ep_t * ep)
  90206. +{
  90207. + depctl_data_t depctl = {.d32 = 0 };
  90208. + volatile uint32_t *addr;
  90209. +
  90210. + if (ep->is_in) {
  90211. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  90212. + } else {
  90213. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  90214. + }
  90215. +
  90216. + if (core_if->dma_enable == 0 || core_if->dma_desc_enable != 0) {
  90217. + return;
  90218. + } else {
  90219. + deptsiz_data_t deptsiz = {.d32 = 0 };
  90220. +
  90221. + ep->xfer_len =
  90222. + ep->data_per_frame * ep->buf_proc_intrvl / ep->bInterval;
  90223. + ep->pkt_cnt =
  90224. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  90225. + ep->xfer_count = 0;
  90226. + ep->xfer_buff =
  90227. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  90228. + ep->dma_addr =
  90229. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  90230. +
  90231. + if (ep->is_in) {
  90232. + /* Program the transfer size and packet count
  90233. + * as follows: xfersize = N * maxpacket +
  90234. + * short_packet pktcnt = N + (short_packet
  90235. + * exist ? 1 : 0)
  90236. + */
  90237. + deptsiz.b.mc = ep->pkt_per_frm;
  90238. + deptsiz.b.xfersize = ep->xfer_len;
  90239. + deptsiz.b.pktcnt =
  90240. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  90241. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  90242. + dieptsiz, deptsiz.d32);
  90243. +
  90244. + /* Write the DMA register */
  90245. + DWC_WRITE_REG32(&
  90246. + (core_if->dev_if->in_ep_regs[ep->num]->
  90247. + diepdma), (uint32_t) ep->dma_addr);
  90248. +
  90249. + } else {
  90250. + deptsiz.b.pktcnt =
  90251. + (ep->xfer_len + (ep->maxpacket - 1)) /
  90252. + ep->maxpacket;
  90253. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  90254. +
  90255. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  90256. + doeptsiz, deptsiz.d32);
  90257. +
  90258. + /* Write the DMA register */
  90259. + DWC_WRITE_REG32(&
  90260. + (core_if->dev_if->out_ep_regs[ep->num]->
  90261. + doepdma), (uint32_t) ep->dma_addr);
  90262. +
  90263. + }
  90264. + /** Enable endpoint, clear nak */
  90265. + depctl.d32 = 0;
  90266. + depctl.b.epena = 1;
  90267. + depctl.b.cnak = 1;
  90268. +
  90269. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  90270. + }
  90271. +}
  90272. +
  90273. +/**
  90274. + * This function does the setup for a data transfer for an EP and
  90275. + * starts the transfer. For an IN transfer, the packets will be
  90276. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  90277. + * the packets are unloaded from the Rx FIFO in the ISR.
  90278. + *
  90279. + * @param core_if Programming view of DWC_otg controller.
  90280. + * @param ep The EP to start the transfer on.
  90281. + */
  90282. +
  90283. +static void dwc_otg_iso_ep_start_transfer(dwc_otg_core_if_t * core_if,
  90284. + dwc_ep_t * ep)
  90285. +{
  90286. + if (core_if->dma_enable) {
  90287. + if (core_if->dma_desc_enable) {
  90288. + if (ep->is_in) {
  90289. + ep->desc_cnt = ep->pkt_cnt / ep->pkt_per_frm;
  90290. + } else {
  90291. + ep->desc_cnt = ep->pkt_cnt;
  90292. + }
  90293. + dwc_otg_iso_ep_start_ddma_transfer(core_if, ep);
  90294. + } else {
  90295. + if (core_if->pti_enh_enable) {
  90296. + dwc_otg_iso_ep_start_buf_transfer(core_if, ep);
  90297. + } else {
  90298. + ep->cur_pkt_addr =
  90299. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->
  90300. + xfer_buff0;
  90301. + ep->cur_pkt_dma_addr =
  90302. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->
  90303. + dma_addr0;
  90304. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  90305. + }
  90306. + }
  90307. + } else {
  90308. + ep->cur_pkt_addr =
  90309. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  90310. + ep->cur_pkt_dma_addr =
  90311. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  90312. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  90313. + }
  90314. +}
  90315. +
  90316. +/**
  90317. + * This function stops transfer for an EP and
  90318. + * resets the ep's variables.
  90319. + *
  90320. + * @param core_if Programming view of DWC_otg controller.
  90321. + * @param ep The EP to start the transfer on.
  90322. + */
  90323. +
  90324. +void dwc_otg_iso_ep_stop_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  90325. +{
  90326. + depctl_data_t depctl = {.d32 = 0 };
  90327. + volatile uint32_t *addr;
  90328. +
  90329. + if (ep->is_in == 1) {
  90330. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  90331. + } else {
  90332. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  90333. + }
  90334. +
  90335. + /* disable the ep */
  90336. + depctl.d32 = DWC_READ_REG32(addr);
  90337. +
  90338. + depctl.b.epdis = 1;
  90339. + depctl.b.snak = 1;
  90340. +
  90341. + DWC_WRITE_REG32(addr, depctl.d32);
  90342. +
  90343. + if (core_if->dma_desc_enable &&
  90344. + ep->iso_desc_addr && ep->iso_dma_desc_addr) {
  90345. + dwc_otg_ep_free_desc_chain(ep->iso_desc_addr,
  90346. + ep->iso_dma_desc_addr,
  90347. + ep->desc_cnt * 2);
  90348. + }
  90349. +
  90350. + /* reset varibales */
  90351. + ep->dma_addr0 = 0;
  90352. + ep->dma_addr1 = 0;
  90353. + ep->xfer_buff0 = 0;
  90354. + ep->xfer_buff1 = 0;
  90355. + ep->data_per_frame = 0;
  90356. + ep->data_pattern_frame = 0;
  90357. + ep->sync_frame = 0;
  90358. + ep->buf_proc_intrvl = 0;
  90359. + ep->bInterval = 0;
  90360. + ep->proc_buf_num = 0;
  90361. + ep->pkt_per_frm = 0;
  90362. + ep->pkt_per_frm = 0;
  90363. + ep->desc_cnt = 0;
  90364. + ep->iso_desc_addr = 0;
  90365. + ep->iso_dma_desc_addr = 0;
  90366. +}
  90367. +
  90368. +int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  90369. + uint8_t * buf0, uint8_t * buf1, dwc_dma_t dma0,
  90370. + dwc_dma_t dma1, int sync_frame, int dp_frame,
  90371. + int data_per_frame, int start_frame,
  90372. + int buf_proc_intrvl, void *req_handle,
  90373. + int atomic_alloc)
  90374. +{
  90375. + dwc_otg_pcd_ep_t *ep;
  90376. + dwc_irqflags_t flags = 0;
  90377. + dwc_ep_t *dwc_ep;
  90378. + int32_t frm_data;
  90379. + dsts_data_t dsts;
  90380. + dwc_otg_core_if_t *core_if;
  90381. +
  90382. + ep = get_ep_from_handle(pcd, ep_handle);
  90383. +
  90384. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  90385. + DWC_WARN("bad ep\n");
  90386. + return -DWC_E_INVALID;
  90387. + }
  90388. +
  90389. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  90390. + core_if = GET_CORE_IF(pcd);
  90391. + dwc_ep = &ep->dwc_ep;
  90392. +
  90393. + if (ep->iso_req_handle) {
  90394. + DWC_WARN("ISO request in progress\n");
  90395. + }
  90396. +
  90397. + dwc_ep->dma_addr0 = dma0;
  90398. + dwc_ep->dma_addr1 = dma1;
  90399. +
  90400. + dwc_ep->xfer_buff0 = buf0;
  90401. + dwc_ep->xfer_buff1 = buf1;
  90402. +
  90403. + dwc_ep->data_per_frame = data_per_frame;
  90404. +
  90405. + /** @todo - pattern data support is to be implemented in the future */
  90406. + dwc_ep->data_pattern_frame = dp_frame;
  90407. + dwc_ep->sync_frame = sync_frame;
  90408. +
  90409. + dwc_ep->buf_proc_intrvl = buf_proc_intrvl;
  90410. +
  90411. + dwc_ep->bInterval = 1 << (ep->desc->bInterval - 1);
  90412. +
  90413. + dwc_ep->proc_buf_num = 0;
  90414. +
  90415. + dwc_ep->pkt_per_frm = 0;
  90416. + frm_data = ep->dwc_ep.data_per_frame;
  90417. + while (frm_data > 0) {
  90418. + dwc_ep->pkt_per_frm++;
  90419. + frm_data -= ep->dwc_ep.maxpacket;
  90420. + }
  90421. +
  90422. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  90423. +
  90424. + if (start_frame == -1) {
  90425. + dwc_ep->next_frame = dsts.b.soffn + 1;
  90426. + if (dwc_ep->bInterval != 1) {
  90427. + dwc_ep->next_frame =
  90428. + dwc_ep->next_frame + (dwc_ep->bInterval - 1 -
  90429. + dwc_ep->next_frame %
  90430. + dwc_ep->bInterval);
  90431. + }
  90432. + } else {
  90433. + dwc_ep->next_frame = start_frame;
  90434. + }
  90435. +
  90436. + if (!core_if->pti_enh_enable) {
  90437. + dwc_ep->pkt_cnt =
  90438. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  90439. + dwc_ep->bInterval;
  90440. + } else {
  90441. + dwc_ep->pkt_cnt =
  90442. + (dwc_ep->data_per_frame *
  90443. + (dwc_ep->buf_proc_intrvl / dwc_ep->bInterval)
  90444. + - 1 + dwc_ep->maxpacket) / dwc_ep->maxpacket;
  90445. + }
  90446. +
  90447. + if (core_if->dma_desc_enable) {
  90448. + dwc_ep->desc_cnt =
  90449. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  90450. + dwc_ep->bInterval;
  90451. + }
  90452. +
  90453. + if (atomic_alloc) {
  90454. + dwc_ep->pkt_info =
  90455. + DWC_ALLOC_ATOMIC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  90456. + } else {
  90457. + dwc_ep->pkt_info =
  90458. + DWC_ALLOC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  90459. + }
  90460. + if (!dwc_ep->pkt_info) {
  90461. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90462. + return -DWC_E_NO_MEMORY;
  90463. + }
  90464. + if (core_if->pti_enh_enable) {
  90465. + dwc_memset(dwc_ep->pkt_info, 0,
  90466. + sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  90467. + }
  90468. +
  90469. + dwc_ep->cur_pkt = 0;
  90470. + ep->iso_req_handle = req_handle;
  90471. +
  90472. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90473. + dwc_otg_iso_ep_start_transfer(core_if, dwc_ep);
  90474. + return 0;
  90475. +}
  90476. +
  90477. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  90478. + void *req_handle)
  90479. +{
  90480. + dwc_irqflags_t flags = 0;
  90481. + dwc_otg_pcd_ep_t *ep;
  90482. + dwc_ep_t *dwc_ep;
  90483. +
  90484. + ep = get_ep_from_handle(pcd, ep_handle);
  90485. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  90486. + DWC_WARN("bad ep\n");
  90487. + return -DWC_E_INVALID;
  90488. + }
  90489. + dwc_ep = &ep->dwc_ep;
  90490. +
  90491. + dwc_otg_iso_ep_stop_transfer(GET_CORE_IF(pcd), dwc_ep);
  90492. +
  90493. + DWC_FREE(dwc_ep->pkt_info);
  90494. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  90495. + if (ep->iso_req_handle != req_handle) {
  90496. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90497. + return -DWC_E_INVALID;
  90498. + }
  90499. +
  90500. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90501. +
  90502. + ep->iso_req_handle = 0;
  90503. + return 0;
  90504. +}
  90505. +
  90506. +/**
  90507. + * This function is used for perodical data exchnage between PCD and gadget drivers.
  90508. + * for Isochronous EPs
  90509. + *
  90510. + * - Every time a sync period completes this function is called to
  90511. + * perform data exchange between PCD and gadget
  90512. + */
  90513. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  90514. + void *req_handle)
  90515. +{
  90516. + int i;
  90517. + dwc_ep_t *dwc_ep;
  90518. +
  90519. + dwc_ep = &ep->dwc_ep;
  90520. +
  90521. + DWC_SPINUNLOCK(ep->pcd->lock);
  90522. + pcd->fops->isoc_complete(pcd, ep->priv, ep->iso_req_handle,
  90523. + dwc_ep->proc_buf_num ^ 0x1);
  90524. + DWC_SPINLOCK(ep->pcd->lock);
  90525. +
  90526. + for (i = 0; i < dwc_ep->pkt_cnt; ++i) {
  90527. + dwc_ep->pkt_info[i].status = 0;
  90528. + dwc_ep->pkt_info[i].offset = 0;
  90529. + dwc_ep->pkt_info[i].length = 0;
  90530. + }
  90531. +}
  90532. +
  90533. +int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd, void *ep_handle,
  90534. + void *iso_req_handle)
  90535. +{
  90536. + dwc_otg_pcd_ep_t *ep;
  90537. + dwc_ep_t *dwc_ep;
  90538. +
  90539. + ep = get_ep_from_handle(pcd, ep_handle);
  90540. + if (!ep->desc || ep->dwc_ep.num == 0) {
  90541. + DWC_WARN("bad ep\n");
  90542. + return -DWC_E_INVALID;
  90543. + }
  90544. + dwc_ep = &ep->dwc_ep;
  90545. +
  90546. + return dwc_ep->pkt_cnt;
  90547. +}
  90548. +
  90549. +void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd, void *ep_handle,
  90550. + void *iso_req_handle, int packet,
  90551. + int *status, int *actual, int *offset)
  90552. +{
  90553. + dwc_otg_pcd_ep_t *ep;
  90554. + dwc_ep_t *dwc_ep;
  90555. +
  90556. + ep = get_ep_from_handle(pcd, ep_handle);
  90557. + if (!ep)
  90558. + DWC_WARN("bad ep\n");
  90559. +
  90560. + dwc_ep = &ep->dwc_ep;
  90561. +
  90562. + *status = dwc_ep->pkt_info[packet].status;
  90563. + *actual = dwc_ep->pkt_info[packet].length;
  90564. + *offset = dwc_ep->pkt_info[packet].offset;
  90565. +}
  90566. +
  90567. +#endif /* DWC_EN_ISOC */
  90568. +
  90569. +static void dwc_otg_pcd_init_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * pcd_ep,
  90570. + uint32_t is_in, uint32_t ep_num)
  90571. +{
  90572. + /* Init EP structure */
  90573. + pcd_ep->desc = 0;
  90574. + pcd_ep->pcd = pcd;
  90575. + pcd_ep->stopped = 1;
  90576. + pcd_ep->queue_sof = 0;
  90577. +
  90578. + /* Init DWC ep structure */
  90579. + pcd_ep->dwc_ep.is_in = is_in;
  90580. + pcd_ep->dwc_ep.num = ep_num;
  90581. + pcd_ep->dwc_ep.active = 0;
  90582. + pcd_ep->dwc_ep.tx_fifo_num = 0;
  90583. + /* Control until ep is actvated */
  90584. + pcd_ep->dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  90585. + pcd_ep->dwc_ep.maxpacket = MAX_PACKET_SIZE;
  90586. + pcd_ep->dwc_ep.dma_addr = 0;
  90587. + pcd_ep->dwc_ep.start_xfer_buff = 0;
  90588. + pcd_ep->dwc_ep.xfer_buff = 0;
  90589. + pcd_ep->dwc_ep.xfer_len = 0;
  90590. + pcd_ep->dwc_ep.xfer_count = 0;
  90591. + pcd_ep->dwc_ep.sent_zlp = 0;
  90592. + pcd_ep->dwc_ep.total_len = 0;
  90593. + pcd_ep->dwc_ep.desc_addr = 0;
  90594. + pcd_ep->dwc_ep.dma_desc_addr = 0;
  90595. + DWC_CIRCLEQ_INIT(&pcd_ep->queue);
  90596. +}
  90597. +
  90598. +/**
  90599. + * Initialize ep's
  90600. + */
  90601. +static void dwc_otg_pcd_reinit(dwc_otg_pcd_t * pcd)
  90602. +{
  90603. + int i;
  90604. + uint32_t hwcfg1;
  90605. + dwc_otg_pcd_ep_t *ep;
  90606. + int in_ep_cntr, out_ep_cntr;
  90607. + uint32_t num_in_eps = (GET_CORE_IF(pcd))->dev_if->num_in_eps;
  90608. + uint32_t num_out_eps = (GET_CORE_IF(pcd))->dev_if->num_out_eps;
  90609. +
  90610. + /**
  90611. + * Initialize the EP0 structure.
  90612. + */
  90613. + ep = &pcd->ep0;
  90614. + dwc_otg_pcd_init_ep(pcd, ep, 0, 0);
  90615. +
  90616. + in_ep_cntr = 0;
  90617. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 3;
  90618. + for (i = 1; in_ep_cntr < num_in_eps; i++) {
  90619. + if ((hwcfg1 & 0x1) == 0) {
  90620. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[in_ep_cntr];
  90621. + in_ep_cntr++;
  90622. + /**
  90623. + * @todo NGS: Add direction to EP, based on contents
  90624. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  90625. + * sprintf(";r
  90626. + */
  90627. + dwc_otg_pcd_init_ep(pcd, ep, 1 /* IN */ , i);
  90628. +
  90629. + DWC_CIRCLEQ_INIT(&ep->queue);
  90630. + }
  90631. + hwcfg1 >>= 2;
  90632. + }
  90633. +
  90634. + out_ep_cntr = 0;
  90635. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 2;
  90636. + for (i = 1; out_ep_cntr < num_out_eps; i++) {
  90637. + if ((hwcfg1 & 0x1) == 0) {
  90638. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[out_ep_cntr];
  90639. + out_ep_cntr++;
  90640. + /**
  90641. + * @todo NGS: Add direction to EP, based on contents
  90642. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  90643. + * sprintf(";r
  90644. + */
  90645. + dwc_otg_pcd_init_ep(pcd, ep, 0 /* OUT */ , i);
  90646. + DWC_CIRCLEQ_INIT(&ep->queue);
  90647. + }
  90648. + hwcfg1 >>= 2;
  90649. + }
  90650. +
  90651. + pcd->ep0state = EP0_DISCONNECT;
  90652. + pcd->ep0.dwc_ep.maxpacket = MAX_EP0_SIZE;
  90653. + pcd->ep0.dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  90654. +}
  90655. +
  90656. +/**
  90657. + * This function is called when the SRP timer expires. The SRP should
  90658. + * complete within 6 seconds.
  90659. + */
  90660. +static void srp_timeout(void *ptr)
  90661. +{
  90662. + gotgctl_data_t gotgctl;
  90663. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  90664. + volatile uint32_t *addr = &core_if->core_global_regs->gotgctl;
  90665. +
  90666. + gotgctl.d32 = DWC_READ_REG32(addr);
  90667. +
  90668. + core_if->srp_timer_started = 0;
  90669. +
  90670. + if (core_if->adp_enable) {
  90671. + if (gotgctl.b.bsesvld == 0) {
  90672. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  90673. + DWC_PRINTF("SRP Timeout BSESSVLD = 0\n");
  90674. + /* Power off the core */
  90675. + if (core_if->power_down == 2) {
  90676. + gpwrdn.b.pwrdnswtch = 1;
  90677. + DWC_MODIFY_REG32(&core_if->
  90678. + core_global_regs->gpwrdn,
  90679. + gpwrdn.d32, 0);
  90680. + }
  90681. +
  90682. + gpwrdn.d32 = 0;
  90683. + gpwrdn.b.pmuintsel = 1;
  90684. + gpwrdn.b.pmuactv = 1;
  90685. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  90686. + gpwrdn.d32);
  90687. + dwc_otg_adp_probe_start(core_if);
  90688. + } else {
  90689. + DWC_PRINTF("SRP Timeout BSESSVLD = 1\n");
  90690. + core_if->op_state = B_PERIPHERAL;
  90691. + dwc_otg_core_init(core_if);
  90692. + dwc_otg_enable_global_interrupts(core_if);
  90693. + cil_pcd_start(core_if);
  90694. + }
  90695. + }
  90696. +
  90697. + if ((core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS) &&
  90698. + (core_if->core_params->i2c_enable)) {
  90699. + DWC_PRINTF("SRP Timeout\n");
  90700. +
  90701. + if ((core_if->srp_success) && (gotgctl.b.bsesvld)) {
  90702. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  90703. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  90704. + }
  90705. +
  90706. + /* Clear Session Request */
  90707. + gotgctl.d32 = 0;
  90708. + gotgctl.b.sesreq = 1;
  90709. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl,
  90710. + gotgctl.d32, 0);
  90711. +
  90712. + core_if->srp_success = 0;
  90713. + } else {
  90714. + __DWC_ERROR("Device not connected/responding\n");
  90715. + gotgctl.b.sesreq = 0;
  90716. + DWC_WRITE_REG32(addr, gotgctl.d32);
  90717. + }
  90718. + } else if (gotgctl.b.sesreq) {
  90719. + DWC_PRINTF("SRP Timeout\n");
  90720. +
  90721. + __DWC_ERROR("Device not connected/responding\n");
  90722. + gotgctl.b.sesreq = 0;
  90723. + DWC_WRITE_REG32(addr, gotgctl.d32);
  90724. + } else {
  90725. + DWC_PRINTF(" SRP GOTGCTL=%0x\n", gotgctl.d32);
  90726. + }
  90727. +}
  90728. +
  90729. +/**
  90730. + * Tasklet
  90731. + *
  90732. + */
  90733. +extern void start_next_request(dwc_otg_pcd_ep_t * ep);
  90734. +
  90735. +static void start_xfer_tasklet_func(void *data)
  90736. +{
  90737. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  90738. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  90739. +
  90740. + int i;
  90741. + depctl_data_t diepctl;
  90742. +
  90743. + DWC_DEBUGPL(DBG_PCDV, "Start xfer tasklet\n");
  90744. +
  90745. + diepctl.d32 = DWC_READ_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl);
  90746. +
  90747. + if (pcd->ep0.queue_sof) {
  90748. + pcd->ep0.queue_sof = 0;
  90749. + start_next_request(&pcd->ep0);
  90750. + // break;
  90751. + }
  90752. +
  90753. + for (i = 0; i < core_if->dev_if->num_in_eps; i++) {
  90754. + depctl_data_t diepctl;
  90755. + diepctl.d32 =
  90756. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  90757. +
  90758. + if (pcd->in_ep[i].queue_sof) {
  90759. + pcd->in_ep[i].queue_sof = 0;
  90760. + start_next_request(&pcd->in_ep[i]);
  90761. + // break;
  90762. + }
  90763. + }
  90764. +
  90765. + return;
  90766. +}
  90767. +
  90768. +/**
  90769. + * This function initialized the PCD portion of the driver.
  90770. + *
  90771. + */
  90772. +dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_core_if_t * core_if)
  90773. +{
  90774. + dwc_otg_pcd_t *pcd = NULL;
  90775. + dwc_otg_dev_if_t *dev_if;
  90776. + int i;
  90777. +
  90778. + /*
  90779. + * Allocate PCD structure
  90780. + */
  90781. + pcd = DWC_ALLOC(sizeof(dwc_otg_pcd_t));
  90782. +
  90783. + if (pcd == NULL) {
  90784. + return NULL;
  90785. + }
  90786. +
  90787. + pcd->lock = DWC_SPINLOCK_ALLOC();
  90788. + DWC_DEBUGPL(DBG_HCDV, "Init of PCD %p given core_if %p\n",
  90789. + pcd, core_if);//GRAYG
  90790. + if (!pcd->lock) {
  90791. + DWC_ERROR("Could not allocate lock for pcd");
  90792. + DWC_FREE(pcd);
  90793. + return NULL;
  90794. + }
  90795. + /* Set core_if's lock pointer to hcd->lock */
  90796. + core_if->lock = pcd->lock;
  90797. + pcd->core_if = core_if;
  90798. +
  90799. + dev_if = core_if->dev_if;
  90800. + dev_if->isoc_ep = NULL;
  90801. +
  90802. + if (core_if->hwcfg4.b.ded_fifo_en) {
  90803. + DWC_PRINTF("Dedicated Tx FIFOs mode\n");
  90804. + } else {
  90805. + DWC_PRINTF("Shared Tx FIFO mode\n");
  90806. + }
  90807. +
  90808. + /*
  90809. + * Initialized the Core for Device mode here if there is nod ADP support.
  90810. + * Otherwise it will be done later in dwc_otg_adp_start routine.
  90811. + */
  90812. + if (dwc_otg_is_device_mode(core_if) /*&& !core_if->adp_enable*/) {
  90813. + dwc_otg_core_dev_init(core_if);
  90814. + }
  90815. +
  90816. + /*
  90817. + * Register the PCD Callbacks.
  90818. + */
  90819. + dwc_otg_cil_register_pcd_callbacks(core_if, &pcd_callbacks, pcd);
  90820. +
  90821. + /*
  90822. + * Initialize the DMA buffer for SETUP packets
  90823. + */
  90824. + if (GET_CORE_IF(pcd)->dma_enable) {
  90825. + pcd->setup_pkt =
  90826. + DWC_DMA_ALLOC(sizeof(*pcd->setup_pkt) * 5,
  90827. + &pcd->setup_pkt_dma_handle);
  90828. + if (pcd->setup_pkt == NULL) {
  90829. + DWC_FREE(pcd);
  90830. + return NULL;
  90831. + }
  90832. +
  90833. + pcd->status_buf =
  90834. + DWC_DMA_ALLOC(sizeof(uint16_t),
  90835. + &pcd->status_buf_dma_handle);
  90836. + if (pcd->status_buf == NULL) {
  90837. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5,
  90838. + pcd->setup_pkt, pcd->setup_pkt_dma_handle);
  90839. + DWC_FREE(pcd);
  90840. + return NULL;
  90841. + }
  90842. +
  90843. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  90844. + dev_if->setup_desc_addr[0] =
  90845. + dwc_otg_ep_alloc_desc_chain
  90846. + (&dev_if->dma_setup_desc_addr[0], 1);
  90847. + dev_if->setup_desc_addr[1] =
  90848. + dwc_otg_ep_alloc_desc_chain
  90849. + (&dev_if->dma_setup_desc_addr[1], 1);
  90850. + dev_if->in_desc_addr =
  90851. + dwc_otg_ep_alloc_desc_chain
  90852. + (&dev_if->dma_in_desc_addr, 1);
  90853. + dev_if->out_desc_addr =
  90854. + dwc_otg_ep_alloc_desc_chain
  90855. + (&dev_if->dma_out_desc_addr, 1);
  90856. + pcd->data_terminated = 0;
  90857. +
  90858. + if (dev_if->setup_desc_addr[0] == 0
  90859. + || dev_if->setup_desc_addr[1] == 0
  90860. + || dev_if->in_desc_addr == 0
  90861. + || dev_if->out_desc_addr == 0) {
  90862. +
  90863. + if (dev_if->out_desc_addr)
  90864. + dwc_otg_ep_free_desc_chain
  90865. + (dev_if->out_desc_addr,
  90866. + dev_if->dma_out_desc_addr, 1);
  90867. + if (dev_if->in_desc_addr)
  90868. + dwc_otg_ep_free_desc_chain
  90869. + (dev_if->in_desc_addr,
  90870. + dev_if->dma_in_desc_addr, 1);
  90871. + if (dev_if->setup_desc_addr[1])
  90872. + dwc_otg_ep_free_desc_chain
  90873. + (dev_if->setup_desc_addr[1],
  90874. + dev_if->dma_setup_desc_addr[1], 1);
  90875. + if (dev_if->setup_desc_addr[0])
  90876. + dwc_otg_ep_free_desc_chain
  90877. + (dev_if->setup_desc_addr[0],
  90878. + dev_if->dma_setup_desc_addr[0], 1);
  90879. +
  90880. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5,
  90881. + pcd->setup_pkt,
  90882. + pcd->setup_pkt_dma_handle);
  90883. + DWC_DMA_FREE(sizeof(*pcd->status_buf),
  90884. + pcd->status_buf,
  90885. + pcd->status_buf_dma_handle);
  90886. +
  90887. + DWC_FREE(pcd);
  90888. +
  90889. + return NULL;
  90890. + }
  90891. + }
  90892. + } else {
  90893. + pcd->setup_pkt = DWC_ALLOC(sizeof(*pcd->setup_pkt) * 5);
  90894. + if (pcd->setup_pkt == NULL) {
  90895. + DWC_FREE(pcd);
  90896. + return NULL;
  90897. + }
  90898. +
  90899. + pcd->status_buf = DWC_ALLOC(sizeof(uint16_t));
  90900. + if (pcd->status_buf == NULL) {
  90901. + DWC_FREE(pcd->setup_pkt);
  90902. + DWC_FREE(pcd);
  90903. + return NULL;
  90904. + }
  90905. + }
  90906. +
  90907. + dwc_otg_pcd_reinit(pcd);
  90908. +
  90909. + /* Allocate the cfi object for the PCD */
  90910. +#ifdef DWC_UTE_CFI
  90911. + pcd->cfi = DWC_ALLOC(sizeof(cfiobject_t));
  90912. + if (NULL == pcd->cfi)
  90913. + goto fail;
  90914. + if (init_cfi(pcd->cfi)) {
  90915. + CFI_INFO("%s: Failed to init the CFI object\n", __func__);
  90916. + goto fail;
  90917. + }
  90918. +#endif
  90919. +
  90920. + /* Initialize tasklets */
  90921. + pcd->start_xfer_tasklet = DWC_TASK_ALLOC("xfer_tasklet",
  90922. + start_xfer_tasklet_func, pcd);
  90923. + pcd->test_mode_tasklet = DWC_TASK_ALLOC("test_mode_tasklet",
  90924. + do_test_mode, pcd);
  90925. +
  90926. + /* Initialize SRP timer */
  90927. + core_if->srp_timer = DWC_TIMER_ALLOC("SRP TIMER", srp_timeout, core_if);
  90928. +
  90929. + if (core_if->core_params->dev_out_nak) {
  90930. + /**
  90931. + * Initialize xfer timeout timer. Implemented for
  90932. + * 2.93a feature "Device DDMA OUT NAK Enhancement"
  90933. + */
  90934. + for(i = 0; i < MAX_EPS_CHANNELS; i++) {
  90935. + pcd->core_if->ep_xfer_timer[i] =
  90936. + DWC_TIMER_ALLOC("ep timer", ep_xfer_timeout,
  90937. + &pcd->core_if->ep_xfer_info[i]);
  90938. + }
  90939. + }
  90940. +
  90941. + return pcd;
  90942. +#ifdef DWC_UTE_CFI
  90943. +fail:
  90944. +#endif
  90945. + if (pcd->setup_pkt)
  90946. + DWC_FREE(pcd->setup_pkt);
  90947. + if (pcd->status_buf)
  90948. + DWC_FREE(pcd->status_buf);
  90949. +#ifdef DWC_UTE_CFI
  90950. + if (pcd->cfi)
  90951. + DWC_FREE(pcd->cfi);
  90952. +#endif
  90953. + if (pcd)
  90954. + DWC_FREE(pcd);
  90955. + return NULL;
  90956. +
  90957. +}
  90958. +
  90959. +/**
  90960. + * Remove PCD specific data
  90961. + */
  90962. +void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd)
  90963. +{
  90964. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  90965. + int i;
  90966. + if (pcd->core_if->core_params->dev_out_nak) {
  90967. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  90968. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[i]);
  90969. + pcd->core_if->ep_xfer_info[i].state = 0;
  90970. + }
  90971. + }
  90972. +
  90973. + if (GET_CORE_IF(pcd)->dma_enable) {
  90974. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5, pcd->setup_pkt,
  90975. + pcd->setup_pkt_dma_handle);
  90976. + DWC_DMA_FREE(sizeof(uint16_t), pcd->status_buf,
  90977. + pcd->status_buf_dma_handle);
  90978. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  90979. + dwc_otg_ep_free_desc_chain(dev_if->setup_desc_addr[0],
  90980. + dev_if->dma_setup_desc_addr
  90981. + [0], 1);
  90982. + dwc_otg_ep_free_desc_chain(dev_if->setup_desc_addr[1],
  90983. + dev_if->dma_setup_desc_addr
  90984. + [1], 1);
  90985. + dwc_otg_ep_free_desc_chain(dev_if->in_desc_addr,
  90986. + dev_if->dma_in_desc_addr, 1);
  90987. + dwc_otg_ep_free_desc_chain(dev_if->out_desc_addr,
  90988. + dev_if->dma_out_desc_addr,
  90989. + 1);
  90990. + }
  90991. + } else {
  90992. + DWC_FREE(pcd->setup_pkt);
  90993. + DWC_FREE(pcd->status_buf);
  90994. + }
  90995. + DWC_SPINLOCK_FREE(pcd->lock);
  90996. + /* Set core_if's lock pointer to NULL */
  90997. + pcd->core_if->lock = NULL;
  90998. +
  90999. + DWC_TASK_FREE(pcd->start_xfer_tasklet);
  91000. + DWC_TASK_FREE(pcd->test_mode_tasklet);
  91001. + if (pcd->core_if->core_params->dev_out_nak) {
  91002. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  91003. + if (pcd->core_if->ep_xfer_timer[i]) {
  91004. + DWC_TIMER_FREE(pcd->core_if->ep_xfer_timer[i]);
  91005. + }
  91006. + }
  91007. + }
  91008. +
  91009. +/* Release the CFI object's dynamic memory */
  91010. +#ifdef DWC_UTE_CFI
  91011. + if (pcd->cfi->ops.release) {
  91012. + pcd->cfi->ops.release(pcd->cfi);
  91013. + }
  91014. +#endif
  91015. +
  91016. + DWC_FREE(pcd);
  91017. +}
  91018. +
  91019. +/**
  91020. + * Returns whether registered pcd is dual speed or not
  91021. + */
  91022. +uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd)
  91023. +{
  91024. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  91025. +
  91026. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) ||
  91027. + ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  91028. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  91029. + (core_if->core_params->ulpi_fs_ls))) {
  91030. + return 0;
  91031. + }
  91032. +
  91033. + return 1;
  91034. +}
  91035. +
  91036. +/**
  91037. + * Returns whether registered pcd is OTG capable or not
  91038. + */
  91039. +uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd)
  91040. +{
  91041. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  91042. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  91043. +
  91044. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  91045. + if (!usbcfg.b.srpcap || !usbcfg.b.hnpcap) {
  91046. + return 0;
  91047. + }
  91048. +
  91049. + return 1;
  91050. +}
  91051. +
  91052. +/**
  91053. + * This function assigns periodic Tx FIFO to an periodic EP
  91054. + * in shared Tx FIFO mode
  91055. + */
  91056. +static uint32_t assign_tx_fifo(dwc_otg_core_if_t * core_if)
  91057. +{
  91058. + uint32_t TxMsk = 1;
  91059. + int i;
  91060. +
  91061. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; ++i) {
  91062. + if ((TxMsk & core_if->tx_msk) == 0) {
  91063. + core_if->tx_msk |= TxMsk;
  91064. + return i + 1;
  91065. + }
  91066. + TxMsk <<= 1;
  91067. + }
  91068. + return 0;
  91069. +}
  91070. +
  91071. +/**
  91072. + * This function assigns periodic Tx FIFO to an periodic EP
  91073. + * in shared Tx FIFO mode
  91074. + */
  91075. +static uint32_t assign_perio_tx_fifo(dwc_otg_core_if_t * core_if)
  91076. +{
  91077. + uint32_t PerTxMsk = 1;
  91078. + int i;
  91079. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; ++i) {
  91080. + if ((PerTxMsk & core_if->p_tx_msk) == 0) {
  91081. + core_if->p_tx_msk |= PerTxMsk;
  91082. + return i + 1;
  91083. + }
  91084. + PerTxMsk <<= 1;
  91085. + }
  91086. + return 0;
  91087. +}
  91088. +
  91089. +/**
  91090. + * This function releases periodic Tx FIFO
  91091. + * in shared Tx FIFO mode
  91092. + */
  91093. +static void release_perio_tx_fifo(dwc_otg_core_if_t * core_if,
  91094. + uint32_t fifo_num)
  91095. +{
  91096. + core_if->p_tx_msk =
  91097. + (core_if->p_tx_msk & (1 << (fifo_num - 1))) ^ core_if->p_tx_msk;
  91098. +}
  91099. +
  91100. +/**
  91101. + * This function releases periodic Tx FIFO
  91102. + * in shared Tx FIFO mode
  91103. + */
  91104. +static void release_tx_fifo(dwc_otg_core_if_t * core_if, uint32_t fifo_num)
  91105. +{
  91106. + core_if->tx_msk =
  91107. + (core_if->tx_msk & (1 << (fifo_num - 1))) ^ core_if->tx_msk;
  91108. +}
  91109. +
  91110. +/**
  91111. + * This function is being called from gadget
  91112. + * to enable PCD endpoint.
  91113. + */
  91114. +int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  91115. + const uint8_t * ep_desc, void *usb_ep)
  91116. +{
  91117. + int num, dir;
  91118. + dwc_otg_pcd_ep_t *ep = NULL;
  91119. + const usb_endpoint_descriptor_t *desc;
  91120. + dwc_irqflags_t flags;
  91121. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  91122. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  91123. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  91124. + int retval = 0;
  91125. + int i, epcount;
  91126. +
  91127. + desc = (const usb_endpoint_descriptor_t *)ep_desc;
  91128. +
  91129. + if (!desc) {
  91130. + pcd->ep0.priv = usb_ep;
  91131. + ep = &pcd->ep0;
  91132. + retval = -DWC_E_INVALID;
  91133. + goto out;
  91134. + }
  91135. +
  91136. + num = UE_GET_ADDR(desc->bEndpointAddress);
  91137. + dir = UE_GET_DIR(desc->bEndpointAddress);
  91138. +
  91139. + if (!desc->wMaxPacketSize) {
  91140. + DWC_WARN("bad maxpacketsize\n");
  91141. + retval = -DWC_E_INVALID;
  91142. + goto out;
  91143. + }
  91144. +
  91145. + if (dir == UE_DIR_IN) {
  91146. + epcount = pcd->core_if->dev_if->num_in_eps;
  91147. + for (i = 0; i < epcount; i++) {
  91148. + if (num == pcd->in_ep[i].dwc_ep.num) {
  91149. + ep = &pcd->in_ep[i];
  91150. + break;
  91151. + }
  91152. + }
  91153. + } else {
  91154. + epcount = pcd->core_if->dev_if->num_out_eps;
  91155. + for (i = 0; i < epcount; i++) {
  91156. + if (num == pcd->out_ep[i].dwc_ep.num) {
  91157. + ep = &pcd->out_ep[i];
  91158. + break;
  91159. + }
  91160. + }
  91161. + }
  91162. +
  91163. + if (!ep) {
  91164. + DWC_WARN("bad address\n");
  91165. + retval = -DWC_E_INVALID;
  91166. + goto out;
  91167. + }
  91168. +
  91169. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  91170. +
  91171. + ep->desc = desc;
  91172. + ep->priv = usb_ep;
  91173. +
  91174. + /*
  91175. + * Activate the EP
  91176. + */
  91177. + ep->stopped = 0;
  91178. +
  91179. + ep->dwc_ep.is_in = (dir == UE_DIR_IN);
  91180. + ep->dwc_ep.maxpacket = UGETW(desc->wMaxPacketSize);
  91181. +
  91182. + ep->dwc_ep.type = desc->bmAttributes & UE_XFERTYPE;
  91183. +
  91184. + if (ep->dwc_ep.is_in) {
  91185. + if (!GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  91186. + ep->dwc_ep.tx_fifo_num = 0;
  91187. +
  91188. + if (ep->dwc_ep.type == UE_ISOCHRONOUS) {
  91189. + /*
  91190. + * if ISOC EP then assign a Periodic Tx FIFO.
  91191. + */
  91192. + ep->dwc_ep.tx_fifo_num =
  91193. + assign_perio_tx_fifo(GET_CORE_IF(pcd));
  91194. + }
  91195. + } else {
  91196. + /*
  91197. + * if Dedicated FIFOs mode is on then assign a Tx FIFO.
  91198. + */
  91199. + ep->dwc_ep.tx_fifo_num =
  91200. + assign_tx_fifo(GET_CORE_IF(pcd));
  91201. + }
  91202. +
  91203. + /* Calculating EP info controller base address */
  91204. + if (ep->dwc_ep.tx_fifo_num
  91205. + && GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  91206. + gdfifocfg.d32 =
  91207. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  91208. + core_global_regs->gdfifocfg);
  91209. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  91210. + dptxfsiz.d32 =
  91211. + (DWC_READ_REG32
  91212. + (&GET_CORE_IF(pcd)->core_global_regs->
  91213. + dtxfsiz[ep->dwc_ep.tx_fifo_num - 1]) >> 16);
  91214. + gdfifocfg.b.epinfobase =
  91215. + gdfifocfgbase.d32 + dptxfsiz.d32;
  91216. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  91217. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  91218. + core_global_regs->gdfifocfg,
  91219. + gdfifocfg.d32);
  91220. + }
  91221. + }
  91222. + }
  91223. + /* Set initial data PID. */
  91224. + if (ep->dwc_ep.type == UE_BULK) {
  91225. + ep->dwc_ep.data_pid_start = 0;
  91226. + }
  91227. +
  91228. + /* Alloc DMA Descriptors */
  91229. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  91230. +#ifndef DWC_UTE_PER_IO
  91231. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  91232. +#endif
  91233. + ep->dwc_ep.desc_addr =
  91234. + dwc_otg_ep_alloc_desc_chain(&ep->
  91235. + dwc_ep.dma_desc_addr,
  91236. + MAX_DMA_DESC_CNT);
  91237. + if (!ep->dwc_ep.desc_addr) {
  91238. + DWC_WARN("%s, can't allocate DMA descriptor\n",
  91239. + __func__);
  91240. + retval = -DWC_E_SHUTDOWN;
  91241. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  91242. + goto out;
  91243. + }
  91244. +#ifndef DWC_UTE_PER_IO
  91245. + }
  91246. +#endif
  91247. + }
  91248. +
  91249. + DWC_DEBUGPL(DBG_PCD, "Activate %s: type=%d, mps=%d desc=%p\n",
  91250. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  91251. + ep->dwc_ep.type, ep->dwc_ep.maxpacket, ep->desc);
  91252. +#ifdef DWC_UTE_PER_IO
  91253. + ep->dwc_ep.xiso_bInterval = 1 << (ep->desc->bInterval - 1);
  91254. +#endif
  91255. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  91256. + ep->dwc_ep.bInterval = 1 << (ep->desc->bInterval - 1);
  91257. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  91258. + }
  91259. +
  91260. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  91261. +
  91262. +#ifdef DWC_UTE_CFI
  91263. + if (pcd->cfi->ops.ep_enable) {
  91264. + pcd->cfi->ops.ep_enable(pcd->cfi, pcd, ep);
  91265. + }
  91266. +#endif
  91267. +
  91268. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  91269. +
  91270. +out:
  91271. + return retval;
  91272. +}
  91273. +
  91274. +/**
  91275. + * This function is being called from gadget
  91276. + * to disable PCD endpoint.
  91277. + */
  91278. +int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle)
  91279. +{
  91280. + dwc_otg_pcd_ep_t *ep;
  91281. + dwc_irqflags_t flags;
  91282. + dwc_otg_dev_dma_desc_t *desc_addr;
  91283. + dwc_dma_t dma_desc_addr;
  91284. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  91285. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  91286. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  91287. +
  91288. + ep = get_ep_from_handle(pcd, ep_handle);
  91289. +
  91290. + if (!ep || !ep->desc) {
  91291. + DWC_DEBUGPL(DBG_PCD, "bad ep address\n");
  91292. + return -DWC_E_INVALID;
  91293. + }
  91294. +
  91295. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  91296. +
  91297. + dwc_otg_request_nuke(ep);
  91298. +
  91299. + dwc_otg_ep_deactivate(GET_CORE_IF(pcd), &ep->dwc_ep);
  91300. + if (pcd->core_if->core_params->dev_out_nak) {
  91301. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[ep->dwc_ep.num]);
  91302. + pcd->core_if->ep_xfer_info[ep->dwc_ep.num].state = 0;
  91303. + }
  91304. + ep->desc = NULL;
  91305. + ep->stopped = 1;
  91306. +
  91307. + gdfifocfg.d32 =
  91308. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg);
  91309. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  91310. +
  91311. + if (ep->dwc_ep.is_in) {
  91312. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  91313. + /* Flush the Tx FIFO */
  91314. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd),
  91315. + ep->dwc_ep.tx_fifo_num);
  91316. + }
  91317. + release_perio_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  91318. + release_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  91319. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  91320. + /* Decreasing EPinfo Base Addr */
  91321. + dptxfsiz.d32 =
  91322. + (DWC_READ_REG32
  91323. + (&GET_CORE_IF(pcd)->
  91324. + core_global_regs->dtxfsiz[ep->dwc_ep.tx_fifo_num-1]) >> 16);
  91325. + gdfifocfg.b.epinfobase = gdfifocfgbase.d32 - dptxfsiz.d32;
  91326. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  91327. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg,
  91328. + gdfifocfg.d32);
  91329. + }
  91330. + }
  91331. + }
  91332. +
  91333. + /* Free DMA Descriptors */
  91334. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  91335. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  91336. + desc_addr = ep->dwc_ep.desc_addr;
  91337. + dma_desc_addr = ep->dwc_ep.dma_desc_addr;
  91338. +
  91339. + /* Cannot call dma_free_coherent() with IRQs disabled */
  91340. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  91341. + dwc_otg_ep_free_desc_chain(desc_addr, dma_desc_addr,
  91342. + MAX_DMA_DESC_CNT);
  91343. +
  91344. + goto out_unlocked;
  91345. + }
  91346. + }
  91347. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  91348. +
  91349. +out_unlocked:
  91350. + DWC_DEBUGPL(DBG_PCD, "%d %s disabled\n", ep->dwc_ep.num,
  91351. + ep->dwc_ep.is_in ? "IN" : "OUT");
  91352. + return 0;
  91353. +
  91354. +}
  91355. +
  91356. +/******************************************************************************/
  91357. +#ifdef DWC_UTE_PER_IO
  91358. +
  91359. +/**
  91360. + * Free the request and its extended parts
  91361. + *
  91362. + */
  91363. +void dwc_pcd_xiso_ereq_free(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req)
  91364. +{
  91365. + DWC_FREE(req->ext_req.per_io_frame_descs);
  91366. + DWC_FREE(req);
  91367. +}
  91368. +
  91369. +/**
  91370. + * Start the next request in the endpoint's queue.
  91371. + *
  91372. + */
  91373. +int dwc_otg_pcd_xiso_start_next_request(dwc_otg_pcd_t * pcd,
  91374. + dwc_otg_pcd_ep_t * ep)
  91375. +{
  91376. + int i;
  91377. + dwc_otg_pcd_request_t *req = NULL;
  91378. + dwc_ep_t *dwcep = NULL;
  91379. + struct dwc_iso_xreq_port *ereq = NULL;
  91380. + struct dwc_iso_pkt_desc_port *ddesc_iso;
  91381. + uint16_t nat;
  91382. + depctl_data_t diepctl;
  91383. +
  91384. + dwcep = &ep->dwc_ep;
  91385. +
  91386. + if (dwcep->xiso_active_xfers > 0) {
  91387. +#if 0 //Disable this to decrease s/w overhead that is crucial for Isoc transfers
  91388. + DWC_WARN("There are currently active transfers for EP%d \
  91389. + (active=%d; queued=%d)", dwcep->num, dwcep->xiso_active_xfers,
  91390. + dwcep->xiso_queued_xfers);
  91391. +#endif
  91392. + return 0;
  91393. + }
  91394. +
  91395. + nat = UGETW(ep->desc->wMaxPacketSize);
  91396. + nat = (nat >> 11) & 0x03;
  91397. +
  91398. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  91399. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  91400. + ereq = &req->ext_req;
  91401. + ep->stopped = 0;
  91402. +
  91403. + /* Get the frame number */
  91404. + dwcep->xiso_frame_num =
  91405. + dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  91406. + DWC_DEBUG("FRM_NUM=%d", dwcep->xiso_frame_num);
  91407. +
  91408. + ddesc_iso = ereq->per_io_frame_descs;
  91409. +
  91410. + if (dwcep->is_in) {
  91411. + /* Setup DMA Descriptor chain for IN Isoc request */
  91412. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  91413. + //if ((i % (nat + 1)) == 0)
  91414. + if ( i > 0 )
  91415. + dwcep->xiso_frame_num =
  91416. + (dwcep->xiso_bInterval +
  91417. + dwcep->xiso_frame_num) & 0x3FFF;
  91418. + dwcep->desc_addr[i].buf =
  91419. + req->dma + ddesc_iso[i].offset;
  91420. + dwcep->desc_addr[i].status.b_iso_in.txbytes =
  91421. + ddesc_iso[i].length;
  91422. + dwcep->desc_addr[i].status.b_iso_in.framenum =
  91423. + dwcep->xiso_frame_num;
  91424. + dwcep->desc_addr[i].status.b_iso_in.bs =
  91425. + BS_HOST_READY;
  91426. + dwcep->desc_addr[i].status.b_iso_in.txsts = 0;
  91427. + dwcep->desc_addr[i].status.b_iso_in.sp =
  91428. + (ddesc_iso[i].length %
  91429. + dwcep->maxpacket) ? 1 : 0;
  91430. + dwcep->desc_addr[i].status.b_iso_in.ioc = 0;
  91431. + dwcep->desc_addr[i].status.b_iso_in.pid = nat + 1;
  91432. + dwcep->desc_addr[i].status.b_iso_in.l = 0;
  91433. +
  91434. + /* Process the last descriptor */
  91435. + if (i == ereq->pio_pkt_count - 1) {
  91436. + dwcep->desc_addr[i].status.b_iso_in.ioc = 1;
  91437. + dwcep->desc_addr[i].status.b_iso_in.l = 1;
  91438. + }
  91439. + }
  91440. +
  91441. + /* Setup and start the transfer for this endpoint */
  91442. + dwcep->xiso_active_xfers++;
  91443. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->dev_if->
  91444. + in_ep_regs[dwcep->num]->diepdma,
  91445. + dwcep->dma_desc_addr);
  91446. + diepctl.d32 = 0;
  91447. + diepctl.b.epena = 1;
  91448. + diepctl.b.cnak = 1;
  91449. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->dev_if->
  91450. + in_ep_regs[dwcep->num]->diepctl, 0,
  91451. + diepctl.d32);
  91452. + } else {
  91453. + /* Setup DMA Descriptor chain for OUT Isoc request */
  91454. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  91455. + //if ((i % (nat + 1)) == 0)
  91456. + dwcep->xiso_frame_num = (dwcep->xiso_bInterval +
  91457. + dwcep->xiso_frame_num) & 0x3FFF;
  91458. + dwcep->desc_addr[i].buf =
  91459. + req->dma + ddesc_iso[i].offset;
  91460. + dwcep->desc_addr[i].status.b_iso_out.rxbytes =
  91461. + ddesc_iso[i].length;
  91462. + dwcep->desc_addr[i].status.b_iso_out.framenum =
  91463. + dwcep->xiso_frame_num;
  91464. + dwcep->desc_addr[i].status.b_iso_out.bs =
  91465. + BS_HOST_READY;
  91466. + dwcep->desc_addr[i].status.b_iso_out.rxsts = 0;
  91467. + dwcep->desc_addr[i].status.b_iso_out.sp =
  91468. + (ddesc_iso[i].length %
  91469. + dwcep->maxpacket) ? 1 : 0;
  91470. + dwcep->desc_addr[i].status.b_iso_out.ioc = 0;
  91471. + dwcep->desc_addr[i].status.b_iso_out.pid = nat + 1;
  91472. + dwcep->desc_addr[i].status.b_iso_out.l = 0;
  91473. +
  91474. + /* Process the last descriptor */
  91475. + if (i == ereq->pio_pkt_count - 1) {
  91476. + dwcep->desc_addr[i].status.b_iso_out.ioc = 1;
  91477. + dwcep->desc_addr[i].status.b_iso_out.l = 1;
  91478. + }
  91479. + }
  91480. +
  91481. + /* Setup and start the transfer for this endpoint */
  91482. + dwcep->xiso_active_xfers++;
  91483. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  91484. + dev_if->out_ep_regs[dwcep->num]->
  91485. + doepdma, dwcep->dma_desc_addr);
  91486. + diepctl.d32 = 0;
  91487. + diepctl.b.epena = 1;
  91488. + diepctl.b.cnak = 1;
  91489. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  91490. + dev_if->out_ep_regs[dwcep->num]->
  91491. + doepctl, 0, diepctl.d32);
  91492. + }
  91493. +
  91494. + } else {
  91495. + ep->stopped = 1;
  91496. + }
  91497. +
  91498. + return 0;
  91499. +}
  91500. +
  91501. +/**
  91502. + * - Remove the request from the queue
  91503. + */
  91504. +void complete_xiso_ep(dwc_otg_pcd_ep_t * ep)
  91505. +{
  91506. + dwc_otg_pcd_request_t *req = NULL;
  91507. + struct dwc_iso_xreq_port *ereq = NULL;
  91508. + struct dwc_iso_pkt_desc_port *ddesc_iso = NULL;
  91509. + dwc_ep_t *dwcep = NULL;
  91510. + int i;
  91511. +
  91512. + //DWC_DEBUG();
  91513. + dwcep = &ep->dwc_ep;
  91514. +
  91515. + /* Get the first pending request from the queue */
  91516. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  91517. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  91518. + if (!req) {
  91519. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  91520. + return;
  91521. + }
  91522. + dwcep->xiso_active_xfers--;
  91523. + dwcep->xiso_queued_xfers--;
  91524. + /* Remove this request from the queue */
  91525. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  91526. + } else {
  91527. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  91528. + return;
  91529. + }
  91530. +
  91531. + ep->stopped = 1;
  91532. + ereq = &req->ext_req;
  91533. + ddesc_iso = ereq->per_io_frame_descs;
  91534. +
  91535. + if (dwcep->xiso_active_xfers < 0) {
  91536. + DWC_WARN("EP#%d (xiso_active_xfers=%d)", dwcep->num,
  91537. + dwcep->xiso_active_xfers);
  91538. + }
  91539. +
  91540. + /* Fill the Isoc descs of portable extended req from dma descriptors */
  91541. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  91542. + if (dwcep->is_in) { /* IN endpoints */
  91543. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  91544. + dwcep->desc_addr[i].status.b_iso_in.txbytes;
  91545. + ddesc_iso[i].status =
  91546. + dwcep->desc_addr[i].status.b_iso_in.txsts;
  91547. + } else { /* OUT endpoints */
  91548. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  91549. + dwcep->desc_addr[i].status.b_iso_out.rxbytes;
  91550. + ddesc_iso[i].status =
  91551. + dwcep->desc_addr[i].status.b_iso_out.rxsts;
  91552. + }
  91553. + }
  91554. +
  91555. + DWC_SPINUNLOCK(ep->pcd->lock);
  91556. +
  91557. + /* Call the completion function in the non-portable logic */
  91558. + ep->pcd->fops->xisoc_complete(ep->pcd, ep->priv, req->priv, 0,
  91559. + &req->ext_req);
  91560. +
  91561. + DWC_SPINLOCK(ep->pcd->lock);
  91562. +
  91563. + /* Free the request - specific freeing needed for extended request object */
  91564. + dwc_pcd_xiso_ereq_free(ep, req);
  91565. +
  91566. + /* Start the next request */
  91567. + dwc_otg_pcd_xiso_start_next_request(ep->pcd, ep);
  91568. +
  91569. + return;
  91570. +}
  91571. +
  91572. +/**
  91573. + * Create and initialize the Isoc pkt descriptors of the extended request.
  91574. + *
  91575. + */
  91576. +static int dwc_otg_pcd_xiso_create_pkt_descs(dwc_otg_pcd_request_t * req,
  91577. + void *ereq_nonport,
  91578. + int atomic_alloc)
  91579. +{
  91580. + struct dwc_iso_xreq_port *ereq = NULL;
  91581. + struct dwc_iso_xreq_port *req_mapped = NULL;
  91582. + struct dwc_iso_pkt_desc_port *ipds = NULL; /* To be created in this function */
  91583. + uint32_t pkt_count;
  91584. + int i;
  91585. +
  91586. + ereq = &req->ext_req;
  91587. + req_mapped = (struct dwc_iso_xreq_port *)ereq_nonport;
  91588. + pkt_count = req_mapped->pio_pkt_count;
  91589. +
  91590. + /* Create the isoc descs */
  91591. + if (atomic_alloc) {
  91592. + ipds = DWC_ALLOC_ATOMIC(sizeof(*ipds) * pkt_count);
  91593. + } else {
  91594. + ipds = DWC_ALLOC(sizeof(*ipds) * pkt_count);
  91595. + }
  91596. +
  91597. + if (!ipds) {
  91598. + DWC_ERROR("Failed to allocate isoc descriptors");
  91599. + return -DWC_E_NO_MEMORY;
  91600. + }
  91601. +
  91602. + /* Initialize the extended request fields */
  91603. + ereq->per_io_frame_descs = ipds;
  91604. + ereq->error_count = 0;
  91605. + ereq->pio_alloc_pkt_count = pkt_count;
  91606. + ereq->pio_pkt_count = pkt_count;
  91607. + ereq->tr_sub_flags = req_mapped->tr_sub_flags;
  91608. +
  91609. + /* Init the Isoc descriptors */
  91610. + for (i = 0; i < pkt_count; i++) {
  91611. + ipds[i].length = req_mapped->per_io_frame_descs[i].length;
  91612. + ipds[i].offset = req_mapped->per_io_frame_descs[i].offset;
  91613. + ipds[i].status = req_mapped->per_io_frame_descs[i].status; /* 0 */
  91614. + ipds[i].actual_length =
  91615. + req_mapped->per_io_frame_descs[i].actual_length;
  91616. + }
  91617. +
  91618. + return 0;
  91619. +}
  91620. +
  91621. +static void prn_ext_request(struct dwc_iso_xreq_port *ereq)
  91622. +{
  91623. + struct dwc_iso_pkt_desc_port *xfd = NULL;
  91624. + int i;
  91625. +
  91626. + DWC_DEBUG("per_io_frame_descs=%p", ereq->per_io_frame_descs);
  91627. + DWC_DEBUG("tr_sub_flags=%d", ereq->tr_sub_flags);
  91628. + DWC_DEBUG("error_count=%d", ereq->error_count);
  91629. + DWC_DEBUG("pio_alloc_pkt_count=%d", ereq->pio_alloc_pkt_count);
  91630. + DWC_DEBUG("pio_pkt_count=%d", ereq->pio_pkt_count);
  91631. + DWC_DEBUG("res=%d", ereq->res);
  91632. +
  91633. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  91634. + xfd = &ereq->per_io_frame_descs[0];
  91635. + DWC_DEBUG("FD #%d", i);
  91636. +
  91637. + DWC_DEBUG("xfd->actual_length=%d", xfd->actual_length);
  91638. + DWC_DEBUG("xfd->length=%d", xfd->length);
  91639. + DWC_DEBUG("xfd->offset=%d", xfd->offset);
  91640. + DWC_DEBUG("xfd->status=%d", xfd->status);
  91641. + }
  91642. +}
  91643. +
  91644. +/**
  91645. + *
  91646. + */
  91647. +int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  91648. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  91649. + int zero, void *req_handle, int atomic_alloc,
  91650. + void *ereq_nonport)
  91651. +{
  91652. + dwc_otg_pcd_request_t *req = NULL;
  91653. + dwc_otg_pcd_ep_t *ep;
  91654. + dwc_irqflags_t flags;
  91655. + int res;
  91656. +
  91657. + ep = get_ep_from_handle(pcd, ep_handle);
  91658. + if (!ep) {
  91659. + DWC_WARN("bad ep\n");
  91660. + return -DWC_E_INVALID;
  91661. + }
  91662. +
  91663. + /* We support this extension only for DDMA mode */
  91664. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  91665. + if (!GET_CORE_IF(pcd)->dma_desc_enable)
  91666. + return -DWC_E_INVALID;
  91667. +
  91668. + /* Create a dwc_otg_pcd_request_t object */
  91669. + if (atomic_alloc) {
  91670. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  91671. + } else {
  91672. + req = DWC_ALLOC(sizeof(*req));
  91673. + }
  91674. +
  91675. + if (!req) {
  91676. + return -DWC_E_NO_MEMORY;
  91677. + }
  91678. +
  91679. + /* Create the Isoc descs for this request which shall be the exact match
  91680. + * of the structure sent to us from the non-portable logic */
  91681. + res =
  91682. + dwc_otg_pcd_xiso_create_pkt_descs(req, ereq_nonport, atomic_alloc);
  91683. + if (res) {
  91684. + DWC_WARN("Failed to init the Isoc descriptors");
  91685. + DWC_FREE(req);
  91686. + return res;
  91687. + }
  91688. +
  91689. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  91690. +
  91691. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  91692. + req->buf = buf;
  91693. + req->dma = dma_buf;
  91694. + req->length = buflen;
  91695. + req->sent_zlp = zero;
  91696. + req->priv = req_handle;
  91697. +
  91698. + //DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  91699. + ep->dwc_ep.dma_addr = dma_buf;
  91700. + ep->dwc_ep.start_xfer_buff = buf;
  91701. + ep->dwc_ep.xfer_buff = buf;
  91702. + ep->dwc_ep.xfer_len = 0;
  91703. + ep->dwc_ep.xfer_count = 0;
  91704. + ep->dwc_ep.sent_zlp = 0;
  91705. + ep->dwc_ep.total_len = buflen;
  91706. +
  91707. + /* Add this request to the tail */
  91708. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  91709. + ep->dwc_ep.xiso_queued_xfers++;
  91710. +
  91711. +//DWC_DEBUG("CP_0");
  91712. +//DWC_DEBUG("req->ext_req.tr_sub_flags=%d", req->ext_req.tr_sub_flags);
  91713. +//prn_ext_request((struct dwc_iso_xreq_port *) ereq_nonport);
  91714. +//prn_ext_request(&req->ext_req);
  91715. +
  91716. + //DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  91717. +
  91718. + /* If the req->status == ASAP then check if there is any active transfer
  91719. + * for this endpoint. If no active transfers, then get the first entry
  91720. + * from the queue and start that transfer
  91721. + */
  91722. + if (req->ext_req.tr_sub_flags == DWC_EREQ_TF_ASAP) {
  91723. + res = dwc_otg_pcd_xiso_start_next_request(pcd, ep);
  91724. + if (res) {
  91725. + DWC_WARN("Failed to start the next Isoc transfer");
  91726. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  91727. + DWC_FREE(req);
  91728. + return res;
  91729. + }
  91730. + }
  91731. +
  91732. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  91733. + return 0;
  91734. +}
  91735. +
  91736. +#endif
  91737. +/* END ifdef DWC_UTE_PER_IO ***************************************************/
  91738. +int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  91739. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  91740. + int zero, void *req_handle, int atomic_alloc)
  91741. +{
  91742. + dwc_irqflags_t flags;
  91743. + dwc_otg_pcd_request_t *req;
  91744. + dwc_otg_pcd_ep_t *ep;
  91745. + uint32_t max_transfer;
  91746. +
  91747. + ep = get_ep_from_handle(pcd, ep_handle);
  91748. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  91749. + DWC_WARN("bad ep\n");
  91750. + return -DWC_E_INVALID;
  91751. + }
  91752. +
  91753. + if (atomic_alloc) {
  91754. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  91755. + } else {
  91756. + req = DWC_ALLOC(sizeof(*req));
  91757. + }
  91758. +
  91759. + if (!req) {
  91760. + return -DWC_E_NO_MEMORY;
  91761. + }
  91762. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  91763. + if (!GET_CORE_IF(pcd)->core_params->opt) {
  91764. + if (ep->dwc_ep.num != 0) {
  91765. + DWC_ERROR("queue req %p, len %d buf %p\n",
  91766. + req_handle, buflen, buf);
  91767. + }
  91768. + }
  91769. +
  91770. + req->buf = buf;
  91771. + req->dma = dma_buf;
  91772. + req->length = buflen;
  91773. + req->sent_zlp = zero;
  91774. + req->priv = req_handle;
  91775. + req->dw_align_buf = NULL;
  91776. + if ((dma_buf & 0x3) && GET_CORE_IF(pcd)->dma_enable
  91777. + && !GET_CORE_IF(pcd)->dma_desc_enable)
  91778. + req->dw_align_buf = DWC_DMA_ALLOC(buflen,
  91779. + &req->dw_align_buf_dma);
  91780. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  91781. +
  91782. + /*
  91783. + * After adding request to the queue for IN ISOC wait for In Token Received
  91784. + * when TX FIFO is empty interrupt and for OUT ISOC wait for OUT Token
  91785. + * Received when EP is disabled interrupt to obtain starting microframe
  91786. + * (odd/even) start transfer
  91787. + */
  91788. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  91789. + if (req != 0) {
  91790. + depctl_data_t depctl = {.d32 =
  91791. + DWC_READ_REG32(&pcd->core_if->dev_if->
  91792. + in_ep_regs[ep->dwc_ep.num]->
  91793. + diepctl) };
  91794. + ++pcd->request_pending;
  91795. +
  91796. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  91797. + if (ep->dwc_ep.is_in) {
  91798. + depctl.b.cnak = 1;
  91799. + DWC_WRITE_REG32(&pcd->core_if->dev_if->
  91800. + in_ep_regs[ep->dwc_ep.num]->
  91801. + diepctl, depctl.d32);
  91802. + }
  91803. +
  91804. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  91805. + }
  91806. + return 0;
  91807. + }
  91808. +
  91809. + /*
  91810. + * For EP0 IN without premature status, zlp is required?
  91811. + */
  91812. + if (ep->dwc_ep.num == 0 && ep->dwc_ep.is_in) {
  91813. + DWC_DEBUGPL(DBG_PCDV, "%d-OUT ZLP\n", ep->dwc_ep.num);
  91814. + //_req->zero = 1;
  91815. + }
  91816. +
  91817. + /* Start the transfer */
  91818. + if (DWC_CIRCLEQ_EMPTY(&ep->queue) && !ep->stopped) {
  91819. + /* EP0 Transfer? */
  91820. + if (ep->dwc_ep.num == 0) {
  91821. + switch (pcd->ep0state) {
  91822. + case EP0_IN_DATA_PHASE:
  91823. + DWC_DEBUGPL(DBG_PCD,
  91824. + "%s ep0: EP0_IN_DATA_PHASE\n",
  91825. + __func__);
  91826. + break;
  91827. +
  91828. + case EP0_OUT_DATA_PHASE:
  91829. + DWC_DEBUGPL(DBG_PCD,
  91830. + "%s ep0: EP0_OUT_DATA_PHASE\n",
  91831. + __func__);
  91832. + if (pcd->request_config) {
  91833. + /* Complete STATUS PHASE */
  91834. + ep->dwc_ep.is_in = 1;
  91835. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  91836. + }
  91837. + break;
  91838. +
  91839. + case EP0_IN_STATUS_PHASE:
  91840. + DWC_DEBUGPL(DBG_PCD,
  91841. + "%s ep0: EP0_IN_STATUS_PHASE\n",
  91842. + __func__);
  91843. + break;
  91844. +
  91845. + default:
  91846. + DWC_DEBUGPL(DBG_ANY, "ep0: odd state %d\n",
  91847. + pcd->ep0state);
  91848. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  91849. + return -DWC_E_SHUTDOWN;
  91850. + }
  91851. +
  91852. + ep->dwc_ep.dma_addr = dma_buf;
  91853. + ep->dwc_ep.start_xfer_buff = buf;
  91854. + ep->dwc_ep.xfer_buff = buf;
  91855. + ep->dwc_ep.xfer_len = buflen;
  91856. + ep->dwc_ep.xfer_count = 0;
  91857. + ep->dwc_ep.sent_zlp = 0;
  91858. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  91859. +
  91860. + if (zero) {
  91861. + if ((ep->dwc_ep.xfer_len %
  91862. + ep->dwc_ep.maxpacket == 0)
  91863. + && (ep->dwc_ep.xfer_len != 0)) {
  91864. + ep->dwc_ep.sent_zlp = 1;
  91865. + }
  91866. +
  91867. + }
  91868. +
  91869. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  91870. + &ep->dwc_ep);
  91871. + } // non-ep0 endpoints
  91872. + else {
  91873. +#ifdef DWC_UTE_CFI
  91874. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  91875. + /* store the request length */
  91876. + ep->dwc_ep.cfi_req_len = buflen;
  91877. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd,
  91878. + ep, req);
  91879. + } else {
  91880. +#endif
  91881. + max_transfer =
  91882. + GET_CORE_IF(ep->pcd)->core_params->
  91883. + max_transfer_size;
  91884. +
  91885. + /* Setup and start the Transfer */
  91886. + if (req->dw_align_buf){
  91887. + if (ep->dwc_ep.is_in)
  91888. + dwc_memcpy(req->dw_align_buf,
  91889. + buf, buflen);
  91890. + ep->dwc_ep.dma_addr =
  91891. + req->dw_align_buf_dma;
  91892. + ep->dwc_ep.start_xfer_buff =
  91893. + req->dw_align_buf;
  91894. + ep->dwc_ep.xfer_buff =
  91895. + req->dw_align_buf;
  91896. + } else {
  91897. + ep->dwc_ep.dma_addr = dma_buf;
  91898. + ep->dwc_ep.start_xfer_buff = buf;
  91899. + ep->dwc_ep.xfer_buff = buf;
  91900. + }
  91901. + ep->dwc_ep.xfer_len = 0;
  91902. + ep->dwc_ep.xfer_count = 0;
  91903. + ep->dwc_ep.sent_zlp = 0;
  91904. + ep->dwc_ep.total_len = buflen;
  91905. +
  91906. + ep->dwc_ep.maxxfer = max_transfer;
  91907. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  91908. + uint32_t out_max_xfer =
  91909. + DDMA_MAX_TRANSFER_SIZE -
  91910. + (DDMA_MAX_TRANSFER_SIZE % 4);
  91911. + if (ep->dwc_ep.is_in) {
  91912. + if (ep->dwc_ep.maxxfer >
  91913. + DDMA_MAX_TRANSFER_SIZE) {
  91914. + ep->dwc_ep.maxxfer =
  91915. + DDMA_MAX_TRANSFER_SIZE;
  91916. + }
  91917. + } else {
  91918. + if (ep->dwc_ep.maxxfer >
  91919. + out_max_xfer) {
  91920. + ep->dwc_ep.maxxfer =
  91921. + out_max_xfer;
  91922. + }
  91923. + }
  91924. + }
  91925. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  91926. + ep->dwc_ep.maxxfer -=
  91927. + (ep->dwc_ep.maxxfer %
  91928. + ep->dwc_ep.maxpacket);
  91929. + }
  91930. +
  91931. + if (zero) {
  91932. + if ((ep->dwc_ep.total_len %
  91933. + ep->dwc_ep.maxpacket == 0)
  91934. + && (ep->dwc_ep.total_len != 0)) {
  91935. + ep->dwc_ep.sent_zlp = 1;
  91936. + }
  91937. + }
  91938. +#ifdef DWC_UTE_CFI
  91939. + }
  91940. +#endif
  91941. + dwc_otg_ep_start_transfer(GET_CORE_IF(pcd),
  91942. + &ep->dwc_ep);
  91943. + }
  91944. + }
  91945. +
  91946. + if (req != 0) {
  91947. + ++pcd->request_pending;
  91948. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  91949. + if (ep->dwc_ep.is_in && ep->stopped
  91950. + && !(GET_CORE_IF(pcd)->dma_enable)) {
  91951. + /** @todo NGS Create a function for this. */
  91952. + diepmsk_data_t diepmsk = {.d32 = 0 };
  91953. + diepmsk.b.intktxfemp = 1;
  91954. + if (GET_CORE_IF(pcd)->multiproc_int_enable) {
  91955. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  91956. + dev_if->dev_global_regs->diepeachintmsk
  91957. + [ep->dwc_ep.num], 0,
  91958. + diepmsk.d32);
  91959. + } else {
  91960. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  91961. + dev_if->dev_global_regs->
  91962. + diepmsk, 0, diepmsk.d32);
  91963. + }
  91964. +
  91965. + }
  91966. + }
  91967. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  91968. +
  91969. + return 0;
  91970. +}
  91971. +
  91972. +int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  91973. + void *req_handle)
  91974. +{
  91975. + dwc_irqflags_t flags;
  91976. + dwc_otg_pcd_request_t *req;
  91977. + dwc_otg_pcd_ep_t *ep;
  91978. +
  91979. + ep = get_ep_from_handle(pcd, ep_handle);
  91980. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  91981. + DWC_WARN("bad argument\n");
  91982. + return -DWC_E_INVALID;
  91983. + }
  91984. +
  91985. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  91986. +
  91987. + /* make sure it's actually queued on this endpoint */
  91988. + DWC_CIRCLEQ_FOREACH(req, &ep->queue, queue_entry) {
  91989. + if (req->priv == (void *)req_handle) {
  91990. + break;
  91991. + }
  91992. + }
  91993. +
  91994. + if (req->priv != (void *)req_handle) {
  91995. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  91996. + return -DWC_E_INVALID;
  91997. + }
  91998. +
  91999. + if (!DWC_CIRCLEQ_EMPTY_ENTRY(req, queue_entry)) {
  92000. + dwc_otg_request_done(ep, req, -DWC_E_RESTART);
  92001. + } else {
  92002. + req = NULL;
  92003. + }
  92004. +
  92005. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  92006. +
  92007. + return req ? 0 : -DWC_E_SHUTDOWN;
  92008. +
  92009. +}
  92010. +
  92011. +/**
  92012. + * dwc_otg_pcd_ep_wedge - sets the halt feature and ignores clear requests
  92013. + *
  92014. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  92015. + * requests. If the gadget driver clears the halt status, it will
  92016. + * automatically unwedge the endpoint.
  92017. + *
  92018. + * Returns zero on success, else negative DWC error code.
  92019. + */
  92020. +int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle)
  92021. +{
  92022. + dwc_otg_pcd_ep_t *ep;
  92023. + dwc_irqflags_t flags;
  92024. + int retval = 0;
  92025. +
  92026. + ep = get_ep_from_handle(pcd, ep_handle);
  92027. +
  92028. + if ((!ep->desc && ep != &pcd->ep0) ||
  92029. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  92030. + DWC_WARN("%s, bad ep\n", __func__);
  92031. + return -DWC_E_INVALID;
  92032. + }
  92033. +
  92034. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  92035. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  92036. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  92037. + ep->dwc_ep.is_in ? "IN" : "OUT");
  92038. + retval = -DWC_E_AGAIN;
  92039. + } else {
  92040. + /* This code needs to be reviewed */
  92041. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  92042. + dtxfsts_data_t txstatus;
  92043. + fifosize_data_t txfifosize;
  92044. +
  92045. + txfifosize.d32 =
  92046. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  92047. + core_global_regs->dtxfsiz[ep->dwc_ep.
  92048. + tx_fifo_num]);
  92049. + txstatus.d32 =
  92050. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  92051. + dev_if->in_ep_regs[ep->dwc_ep.num]->
  92052. + dtxfsts);
  92053. +
  92054. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  92055. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  92056. + retval = -DWC_E_AGAIN;
  92057. + } else {
  92058. + if (ep->dwc_ep.num == 0) {
  92059. + pcd->ep0state = EP0_STALL;
  92060. + }
  92061. +
  92062. + ep->stopped = 1;
  92063. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  92064. + &ep->dwc_ep);
  92065. + }
  92066. + } else {
  92067. + if (ep->dwc_ep.num == 0) {
  92068. + pcd->ep0state = EP0_STALL;
  92069. + }
  92070. +
  92071. + ep->stopped = 1;
  92072. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  92073. + }
  92074. + }
  92075. +
  92076. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  92077. +
  92078. + return retval;
  92079. +}
  92080. +
  92081. +int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value)
  92082. +{
  92083. + dwc_otg_pcd_ep_t *ep;
  92084. + dwc_irqflags_t flags;
  92085. + int retval = 0;
  92086. +
  92087. + ep = get_ep_from_handle(pcd, ep_handle);
  92088. +
  92089. + if (!ep || (!ep->desc && ep != &pcd->ep0) ||
  92090. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  92091. + DWC_WARN("%s, bad ep\n", __func__);
  92092. + return -DWC_E_INVALID;
  92093. + }
  92094. +
  92095. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  92096. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  92097. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  92098. + ep->dwc_ep.is_in ? "IN" : "OUT");
  92099. + retval = -DWC_E_AGAIN;
  92100. + } else if (value == 0) {
  92101. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  92102. + } else if (value == 1) {
  92103. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  92104. + dtxfsts_data_t txstatus;
  92105. + fifosize_data_t txfifosize;
  92106. +
  92107. + txfifosize.d32 =
  92108. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->
  92109. + dtxfsiz[ep->dwc_ep.tx_fifo_num]);
  92110. + txstatus.d32 =
  92111. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  92112. + in_ep_regs[ep->dwc_ep.num]->dtxfsts);
  92113. +
  92114. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  92115. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  92116. + retval = -DWC_E_AGAIN;
  92117. + } else {
  92118. + if (ep->dwc_ep.num == 0) {
  92119. + pcd->ep0state = EP0_STALL;
  92120. + }
  92121. +
  92122. + ep->stopped = 1;
  92123. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  92124. + &ep->dwc_ep);
  92125. + }
  92126. + } else {
  92127. + if (ep->dwc_ep.num == 0) {
  92128. + pcd->ep0state = EP0_STALL;
  92129. + }
  92130. +
  92131. + ep->stopped = 1;
  92132. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  92133. + }
  92134. + } else if (value == 2) {
  92135. + ep->dwc_ep.stall_clear_flag = 0;
  92136. + } else if (value == 3) {
  92137. + ep->dwc_ep.stall_clear_flag = 1;
  92138. + }
  92139. +
  92140. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  92141. +
  92142. + return retval;
  92143. +}
  92144. +
  92145. +/**
  92146. + * This function initiates remote wakeup of the host from suspend state.
  92147. + */
  92148. +void dwc_otg_pcd_rem_wkup_from_suspend(dwc_otg_pcd_t * pcd, int set)
  92149. +{
  92150. + dctl_data_t dctl = { 0 };
  92151. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  92152. + dsts_data_t dsts;
  92153. +
  92154. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  92155. + if (!dsts.b.suspsts) {
  92156. + DWC_WARN("Remote wakeup while is not in suspend state\n");
  92157. + }
  92158. + /* Check if DEVICE_REMOTE_WAKEUP feature enabled */
  92159. + if (pcd->remote_wakeup_enable) {
  92160. + if (set) {
  92161. +
  92162. + if (core_if->adp_enable) {
  92163. + gpwrdn_data_t gpwrdn;
  92164. +
  92165. + dwc_otg_adp_probe_stop(core_if);
  92166. +
  92167. + /* Mask SRP detected interrupt from Power Down Logic */
  92168. + gpwrdn.d32 = 0;
  92169. + gpwrdn.b.srp_det_msk = 1;
  92170. + DWC_MODIFY_REG32(&core_if->
  92171. + core_global_regs->gpwrdn,
  92172. + gpwrdn.d32, 0);
  92173. +
  92174. + /* Disable Power Down Logic */
  92175. + gpwrdn.d32 = 0;
  92176. + gpwrdn.b.pmuactv = 1;
  92177. + DWC_MODIFY_REG32(&core_if->
  92178. + core_global_regs->gpwrdn,
  92179. + gpwrdn.d32, 0);
  92180. +
  92181. + /*
  92182. + * Initialize the Core for Device mode.
  92183. + */
  92184. + core_if->op_state = B_PERIPHERAL;
  92185. + dwc_otg_core_init(core_if);
  92186. + dwc_otg_enable_global_interrupts(core_if);
  92187. + cil_pcd_start(core_if);
  92188. +
  92189. + dwc_otg_initiate_srp(core_if);
  92190. + }
  92191. +
  92192. + dctl.b.rmtwkupsig = 1;
  92193. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  92194. + dctl, 0, dctl.d32);
  92195. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  92196. +
  92197. + dwc_mdelay(2);
  92198. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  92199. + dctl, dctl.d32, 0);
  92200. + DWC_DEBUGPL(DBG_PCD, "Clear Remote Wakeup\n");
  92201. + }
  92202. + } else {
  92203. + DWC_DEBUGPL(DBG_PCD, "Remote Wakeup is disabled\n");
  92204. + }
  92205. +}
  92206. +
  92207. +#ifdef CONFIG_USB_DWC_OTG_LPM
  92208. +/**
  92209. + * This function initiates remote wakeup of the host from L1 sleep state.
  92210. + */
  92211. +void dwc_otg_pcd_rem_wkup_from_sleep(dwc_otg_pcd_t * pcd, int set)
  92212. +{
  92213. + glpmcfg_data_t lpmcfg;
  92214. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  92215. +
  92216. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  92217. +
  92218. + /* Check if we are in L1 state */
  92219. + if (!lpmcfg.b.prt_sleep_sts) {
  92220. + DWC_DEBUGPL(DBG_PCD, "Device is not in sleep state\n");
  92221. + return;
  92222. + }
  92223. +
  92224. + /* Check if host allows remote wakeup */
  92225. + if (!lpmcfg.b.rem_wkup_en) {
  92226. + DWC_DEBUGPL(DBG_PCD, "Host does not allow remote wakeup\n");
  92227. + return;
  92228. + }
  92229. +
  92230. + /* Check if Resume OK */
  92231. + if (!lpmcfg.b.sleep_state_resumeok) {
  92232. + DWC_DEBUGPL(DBG_PCD, "Sleep state resume is not OK\n");
  92233. + return;
  92234. + }
  92235. +
  92236. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  92237. + lpmcfg.b.en_utmi_sleep = 0;
  92238. + lpmcfg.b.hird_thres &= (~(1 << 4));
  92239. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  92240. +
  92241. + if (set) {
  92242. + dctl_data_t dctl = {.d32 = 0 };
  92243. + dctl.b.rmtwkupsig = 1;
  92244. + /* Set RmtWkUpSig bit to start remote wakup signaling.
  92245. + * Hardware will automatically clear this bit.
  92246. + */
  92247. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl,
  92248. + 0, dctl.d32);
  92249. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  92250. + }
  92251. +
  92252. +}
  92253. +#endif
  92254. +
  92255. +/**
  92256. + * Performs remote wakeup.
  92257. + */
  92258. +void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set)
  92259. +{
  92260. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  92261. + dwc_irqflags_t flags;
  92262. + if (dwc_otg_is_device_mode(core_if)) {
  92263. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  92264. +#ifdef CONFIG_USB_DWC_OTG_LPM
  92265. + if (core_if->lx_state == DWC_OTG_L1) {
  92266. + dwc_otg_pcd_rem_wkup_from_sleep(pcd, set);
  92267. + } else {
  92268. +#endif
  92269. + dwc_otg_pcd_rem_wkup_from_suspend(pcd, set);
  92270. +#ifdef CONFIG_USB_DWC_OTG_LPM
  92271. + }
  92272. +#endif
  92273. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  92274. + }
  92275. + return;
  92276. +}
  92277. +
  92278. +void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs)
  92279. +{
  92280. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  92281. + dctl_data_t dctl = { 0 };
  92282. +
  92283. + if (dwc_otg_is_device_mode(core_if)) {
  92284. + dctl.b.sftdiscon = 1;
  92285. + DWC_PRINTF("Soft disconnect for %d useconds\n",no_of_usecs);
  92286. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  92287. + dwc_udelay(no_of_usecs);
  92288. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32,0);
  92289. +
  92290. + } else{
  92291. + DWC_PRINTF("NOT SUPPORTED IN HOST MODE\n");
  92292. + }
  92293. + return;
  92294. +
  92295. +}
  92296. +
  92297. +int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd)
  92298. +{
  92299. + dsts_data_t dsts;
  92300. + gotgctl_data_t gotgctl;
  92301. +
  92302. + /*
  92303. + * This function starts the Protocol if no session is in progress. If
  92304. + * a session is already in progress, but the device is suspended,
  92305. + * remote wakeup signaling is started.
  92306. + */
  92307. +
  92308. + /* Check if valid session */
  92309. + gotgctl.d32 =
  92310. + DWC_READ_REG32(&(GET_CORE_IF(pcd)->core_global_regs->gotgctl));
  92311. + if (gotgctl.b.bsesvld) {
  92312. + /* Check if suspend state */
  92313. + dsts.d32 =
  92314. + DWC_READ_REG32(&
  92315. + (GET_CORE_IF(pcd)->dev_if->
  92316. + dev_global_regs->dsts));
  92317. + if (dsts.b.suspsts) {
  92318. + dwc_otg_pcd_remote_wakeup(pcd, 1);
  92319. + }
  92320. + } else {
  92321. + dwc_otg_pcd_initiate_srp(pcd);
  92322. + }
  92323. +
  92324. + return 0;
  92325. +
  92326. +}
  92327. +
  92328. +/**
  92329. + * Start the SRP timer to detect when the SRP does not complete within
  92330. + * 6 seconds.
  92331. + *
  92332. + * @param pcd the pcd structure.
  92333. + */
  92334. +void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd)
  92335. +{
  92336. + dwc_irqflags_t flags;
  92337. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  92338. + dwc_otg_initiate_srp(GET_CORE_IF(pcd));
  92339. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  92340. +}
  92341. +
  92342. +int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd)
  92343. +{
  92344. + return dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  92345. +}
  92346. +
  92347. +int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd)
  92348. +{
  92349. + return GET_CORE_IF(pcd)->core_params->lpm_enable;
  92350. +}
  92351. +
  92352. +uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd)
  92353. +{
  92354. + return pcd->b_hnp_enable;
  92355. +}
  92356. +
  92357. +uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd)
  92358. +{
  92359. + return pcd->a_hnp_support;
  92360. +}
  92361. +
  92362. +uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd)
  92363. +{
  92364. + return pcd->a_alt_hnp_support;
  92365. +}
  92366. +
  92367. +int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd)
  92368. +{
  92369. + return pcd->remote_wakeup_enable;
  92370. +}
  92371. +
  92372. +#endif /* DWC_HOST_ONLY */
  92373. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_pcd.h linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd.h
  92374. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_pcd.h 1969-12-31 18:00:00.000000000 -0600
  92375. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd.h 2014-12-03 19:13:40.220418001 -0600
  92376. @@ -0,0 +1,266 @@
  92377. +/* ==========================================================================
  92378. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.h $
  92379. + * $Revision: #48 $
  92380. + * $Date: 2012/08/10 $
  92381. + * $Change: 2047372 $
  92382. + *
  92383. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  92384. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  92385. + * otherwise expressly agreed to in writing between Synopsys and you.
  92386. + *
  92387. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  92388. + * any End User Software License Agreement or Agreement for Licensed Product
  92389. + * with Synopsys or any supplement thereto. You are permitted to use and
  92390. + * redistribute this Software in source and binary forms, with or without
  92391. + * modification, provided that redistributions of source code must retain this
  92392. + * notice. You may not view, use, disclose, copy or distribute this file or
  92393. + * any information contained herein except pursuant to this license grant from
  92394. + * Synopsys. If you do not agree with this notice, including the disclaimer
  92395. + * below, then you are not authorized to use the Software.
  92396. + *
  92397. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  92398. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  92399. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  92400. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  92401. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  92402. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  92403. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  92404. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  92405. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  92406. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  92407. + * DAMAGE.
  92408. + * ========================================================================== */
  92409. +#ifndef DWC_HOST_ONLY
  92410. +#if !defined(__DWC_PCD_H__)
  92411. +#define __DWC_PCD_H__
  92412. +
  92413. +#include "dwc_otg_os_dep.h"
  92414. +#include "usb.h"
  92415. +#include "dwc_otg_cil.h"
  92416. +#include "dwc_otg_pcd_if.h"
  92417. +struct cfiobject;
  92418. +
  92419. +/**
  92420. + * @file
  92421. + *
  92422. + * This file contains the structures, constants, and interfaces for
  92423. + * the Perpherial Contoller Driver (PCD).
  92424. + *
  92425. + * The Peripheral Controller Driver (PCD) for Linux will implement the
  92426. + * Gadget API, so that the existing Gadget drivers can be used. For
  92427. + * the Mass Storage Function driver the File-backed USB Storage Gadget
  92428. + * (FBS) driver will be used. The FBS driver supports the
  92429. + * Control-Bulk (CB), Control-Bulk-Interrupt (CBI), and Bulk-Only
  92430. + * transports.
  92431. + *
  92432. + */
  92433. +
  92434. +/** Invalid DMA Address */
  92435. +#define DWC_DMA_ADDR_INVALID (~(dwc_dma_t)0)
  92436. +
  92437. +/** Max Transfer size for any EP */
  92438. +#define DDMA_MAX_TRANSFER_SIZE 65535
  92439. +
  92440. +/**
  92441. + * Get the pointer to the core_if from the pcd pointer.
  92442. + */
  92443. +#define GET_CORE_IF( _pcd ) (_pcd->core_if)
  92444. +
  92445. +/**
  92446. + * States of EP0.
  92447. + */
  92448. +typedef enum ep0_state {
  92449. + EP0_DISCONNECT, /* no host */
  92450. + EP0_IDLE,
  92451. + EP0_IN_DATA_PHASE,
  92452. + EP0_OUT_DATA_PHASE,
  92453. + EP0_IN_STATUS_PHASE,
  92454. + EP0_OUT_STATUS_PHASE,
  92455. + EP0_STALL,
  92456. +} ep0state_e;
  92457. +
  92458. +/** Fordward declaration.*/
  92459. +struct dwc_otg_pcd;
  92460. +
  92461. +/** DWC_otg iso request structure.
  92462. + *
  92463. + */
  92464. +typedef struct usb_iso_request dwc_otg_pcd_iso_request_t;
  92465. +
  92466. +#ifdef DWC_UTE_PER_IO
  92467. +
  92468. +/**
  92469. + * This shall be the exact analogy of the same type structure defined in the
  92470. + * usb_gadget.h. Each descriptor contains
  92471. + */
  92472. +struct dwc_iso_pkt_desc_port {
  92473. + uint32_t offset;
  92474. + uint32_t length; /* expected length */
  92475. + uint32_t actual_length;
  92476. + uint32_t status;
  92477. +};
  92478. +
  92479. +struct dwc_iso_xreq_port {
  92480. + /** transfer/submission flag */
  92481. + uint32_t tr_sub_flags;
  92482. + /** Start the request ASAP */
  92483. +#define DWC_EREQ_TF_ASAP 0x00000002
  92484. + /** Just enqueue the request w/o initiating a transfer */
  92485. +#define DWC_EREQ_TF_ENQUEUE 0x00000004
  92486. +
  92487. + /**
  92488. + * count of ISO packets attached to this request - shall
  92489. + * not exceed the pio_alloc_pkt_count
  92490. + */
  92491. + uint32_t pio_pkt_count;
  92492. + /** count of ISO packets allocated for this request */
  92493. + uint32_t pio_alloc_pkt_count;
  92494. + /** number of ISO packet errors */
  92495. + uint32_t error_count;
  92496. + /** reserved for future extension */
  92497. + uint32_t res;
  92498. + /** Will be allocated and freed in the UTE gadget and based on the CFC value */
  92499. + struct dwc_iso_pkt_desc_port *per_io_frame_descs;
  92500. +};
  92501. +#endif
  92502. +/** DWC_otg request structure.
  92503. + * This structure is a list of requests.
  92504. + */
  92505. +typedef struct dwc_otg_pcd_request {
  92506. + void *priv;
  92507. + void *buf;
  92508. + dwc_dma_t dma;
  92509. + uint32_t length;
  92510. + uint32_t actual;
  92511. + unsigned sent_zlp:1;
  92512. + /**
  92513. + * Used instead of original buffer if
  92514. + * it(physical address) is not dword-aligned.
  92515. + **/
  92516. + uint8_t *dw_align_buf;
  92517. + dwc_dma_t dw_align_buf_dma;
  92518. +
  92519. + DWC_CIRCLEQ_ENTRY(dwc_otg_pcd_request) queue_entry;
  92520. +#ifdef DWC_UTE_PER_IO
  92521. + struct dwc_iso_xreq_port ext_req;
  92522. + //void *priv_ereq_nport; /* */
  92523. +#endif
  92524. +} dwc_otg_pcd_request_t;
  92525. +
  92526. +DWC_CIRCLEQ_HEAD(req_list, dwc_otg_pcd_request);
  92527. +
  92528. +/** PCD EP structure.
  92529. + * This structure describes an EP, there is an array of EPs in the PCD
  92530. + * structure.
  92531. + */
  92532. +typedef struct dwc_otg_pcd_ep {
  92533. + /** USB EP Descriptor */
  92534. + const usb_endpoint_descriptor_t *desc;
  92535. +
  92536. + /** queue of dwc_otg_pcd_requests. */
  92537. + struct req_list queue;
  92538. + unsigned stopped:1;
  92539. + unsigned disabling:1;
  92540. + unsigned dma:1;
  92541. + unsigned queue_sof:1;
  92542. +
  92543. +#ifdef DWC_EN_ISOC
  92544. + /** ISOC req handle passed */
  92545. + void *iso_req_handle;
  92546. +#endif //_EN_ISOC_
  92547. +
  92548. + /** DWC_otg ep data. */
  92549. + dwc_ep_t dwc_ep;
  92550. +
  92551. + /** Pointer to PCD */
  92552. + struct dwc_otg_pcd *pcd;
  92553. +
  92554. + void *priv;
  92555. +} dwc_otg_pcd_ep_t;
  92556. +
  92557. +/** DWC_otg PCD Structure.
  92558. + * This structure encapsulates the data for the dwc_otg PCD.
  92559. + */
  92560. +struct dwc_otg_pcd {
  92561. + const struct dwc_otg_pcd_function_ops *fops;
  92562. + /** The DWC otg device pointer */
  92563. + struct dwc_otg_device *otg_dev;
  92564. + /** Core Interface */
  92565. + dwc_otg_core_if_t *core_if;
  92566. + /** State of EP0 */
  92567. + ep0state_e ep0state;
  92568. + /** EP0 Request is pending */
  92569. + unsigned ep0_pending:1;
  92570. + /** Indicates when SET CONFIGURATION Request is in process */
  92571. + unsigned request_config:1;
  92572. + /** The state of the Remote Wakeup Enable. */
  92573. + unsigned remote_wakeup_enable:1;
  92574. + /** The state of the B-Device HNP Enable. */
  92575. + unsigned b_hnp_enable:1;
  92576. + /** The state of A-Device HNP Support. */
  92577. + unsigned a_hnp_support:1;
  92578. + /** The state of the A-Device Alt HNP support. */
  92579. + unsigned a_alt_hnp_support:1;
  92580. + /** Count of pending Requests */
  92581. + unsigned request_pending;
  92582. +
  92583. + /** SETUP packet for EP0
  92584. + * This structure is allocated as a DMA buffer on PCD initialization
  92585. + * with enough space for up to 3 setup packets.
  92586. + */
  92587. + union {
  92588. + usb_device_request_t req;
  92589. + uint32_t d32[2];
  92590. + } *setup_pkt;
  92591. +
  92592. + dwc_dma_t setup_pkt_dma_handle;
  92593. +
  92594. + /* Additional buffer and flag for CTRL_WR premature case */
  92595. + uint8_t *backup_buf;
  92596. + unsigned data_terminated;
  92597. +
  92598. + /** 2-byte dma buffer used to return status from GET_STATUS */
  92599. + uint16_t *status_buf;
  92600. + dwc_dma_t status_buf_dma_handle;
  92601. +
  92602. + /** EP0 */
  92603. + dwc_otg_pcd_ep_t ep0;
  92604. +
  92605. + /** Array of IN EPs. */
  92606. + dwc_otg_pcd_ep_t in_ep[MAX_EPS_CHANNELS - 1];
  92607. + /** Array of OUT EPs. */
  92608. + dwc_otg_pcd_ep_t out_ep[MAX_EPS_CHANNELS - 1];
  92609. + /** number of valid EPs in the above array. */
  92610. +// unsigned num_eps : 4;
  92611. + dwc_spinlock_t *lock;
  92612. +
  92613. + /** Tasklet to defer starting of TEST mode transmissions until
  92614. + * Status Phase has been completed.
  92615. + */
  92616. + dwc_tasklet_t *test_mode_tasklet;
  92617. +
  92618. + /** Tasklet to delay starting of xfer in DMA mode */
  92619. + dwc_tasklet_t *start_xfer_tasklet;
  92620. +
  92621. + /** The test mode to enter when the tasklet is executed. */
  92622. + unsigned test_mode;
  92623. + /** The cfi_api structure that implements most of the CFI API
  92624. + * and OTG specific core configuration functionality
  92625. + */
  92626. +#ifdef DWC_UTE_CFI
  92627. + struct cfiobject *cfi;
  92628. +#endif
  92629. +
  92630. +};
  92631. +
  92632. +//FIXME this functions should be static, and this prototypes should be removed
  92633. +extern void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep);
  92634. +extern void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep,
  92635. + dwc_otg_pcd_request_t * req, int32_t status);
  92636. +
  92637. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  92638. + void *req_handle);
  92639. +
  92640. +extern void do_test_mode(void *data);
  92641. +#endif
  92642. +#endif /* DWC_HOST_ONLY */
  92643. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
  92644. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h 1969-12-31 18:00:00.000000000 -0600
  92645. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h 2014-12-03 19:13:40.220418001 -0600
  92646. @@ -0,0 +1,360 @@
  92647. +/* ==========================================================================
  92648. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_if.h $
  92649. + * $Revision: #11 $
  92650. + * $Date: 2011/10/26 $
  92651. + * $Change: 1873028 $
  92652. + *
  92653. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  92654. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  92655. + * otherwise expressly agreed to in writing between Synopsys and you.
  92656. + *
  92657. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  92658. + * any End User Software License Agreement or Agreement for Licensed Product
  92659. + * with Synopsys or any supplement thereto. You are permitted to use and
  92660. + * redistribute this Software in source and binary forms, with or without
  92661. + * modification, provided that redistributions of source code must retain this
  92662. + * notice. You may not view, use, disclose, copy or distribute this file or
  92663. + * any information contained herein except pursuant to this license grant from
  92664. + * Synopsys. If you do not agree with this notice, including the disclaimer
  92665. + * below, then you are not authorized to use the Software.
  92666. + *
  92667. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  92668. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  92669. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  92670. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  92671. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  92672. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  92673. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  92674. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  92675. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  92676. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  92677. + * DAMAGE.
  92678. + * ========================================================================== */
  92679. +#ifndef DWC_HOST_ONLY
  92680. +
  92681. +#if !defined(__DWC_PCD_IF_H__)
  92682. +#define __DWC_PCD_IF_H__
  92683. +
  92684. +//#include "dwc_os.h"
  92685. +#include "dwc_otg_core_if.h"
  92686. +
  92687. +/** @file
  92688. + * This file defines DWC_OTG PCD Core API.
  92689. + */
  92690. +
  92691. +struct dwc_otg_pcd;
  92692. +typedef struct dwc_otg_pcd dwc_otg_pcd_t;
  92693. +
  92694. +/** Maxpacket size for EP0 */
  92695. +#define MAX_EP0_SIZE 64
  92696. +/** Maxpacket size for any EP */
  92697. +#define MAX_PACKET_SIZE 1024
  92698. +
  92699. +/** @name Function Driver Callbacks */
  92700. +/** @{ */
  92701. +
  92702. +/** This function will be called whenever a previously queued request has
  92703. + * completed. The status value will be set to -DWC_E_SHUTDOWN to indicated a
  92704. + * failed or aborted transfer, or -DWC_E_RESTART to indicate the device was reset,
  92705. + * or -DWC_E_TIMEOUT to indicate it timed out, or -DWC_E_INVALID to indicate invalid
  92706. + * parameters. */
  92707. +typedef int (*dwc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  92708. + void *req_handle, int32_t status,
  92709. + uint32_t actual);
  92710. +/**
  92711. + * This function will be called whenever a previousle queued ISOC request has
  92712. + * completed. Count of ISOC packets could be read using dwc_otg_pcd_get_iso_packet_count
  92713. + * function.
  92714. + * The status of each ISOC packet could be read using dwc_otg_pcd_get_iso_packet_*
  92715. + * functions.
  92716. + */
  92717. +typedef int (*dwc_isoc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  92718. + void *req_handle, int proc_buf_num);
  92719. +/** This function should handle any SETUP request that cannot be handled by the
  92720. + * PCD Core. This includes most GET_DESCRIPTORs, SET_CONFIGS, Any
  92721. + * class-specific requests, etc. The function must non-blocking.
  92722. + *
  92723. + * Returns 0 on success.
  92724. + * Returns -DWC_E_NOT_SUPPORTED if the request is not supported.
  92725. + * Returns -DWC_E_INVALID if the setup request had invalid parameters or bytes.
  92726. + * Returns -DWC_E_SHUTDOWN on any other error. */
  92727. +typedef int (*dwc_setup_cb_t) (dwc_otg_pcd_t * pcd, uint8_t * bytes);
  92728. +/** This is called whenever the device has been disconnected. The function
  92729. + * driver should take appropriate action to clean up all pending requests in the
  92730. + * PCD Core, remove all endpoints (except ep0), and initialize back to reset
  92731. + * state. */
  92732. +typedef int (*dwc_disconnect_cb_t) (dwc_otg_pcd_t * pcd);
  92733. +/** This function is called when device has been connected. */
  92734. +typedef int (*dwc_connect_cb_t) (dwc_otg_pcd_t * pcd, int speed);
  92735. +/** This function is called when device has been suspended */
  92736. +typedef int (*dwc_suspend_cb_t) (dwc_otg_pcd_t * pcd);
  92737. +/** This function is called when device has received LPM tokens, i.e.
  92738. + * device has been sent to sleep state. */
  92739. +typedef int (*dwc_sleep_cb_t) (dwc_otg_pcd_t * pcd);
  92740. +/** This function is called when device has been resumed
  92741. + * from suspend(L2) or L1 sleep state. */
  92742. +typedef int (*dwc_resume_cb_t) (dwc_otg_pcd_t * pcd);
  92743. +/** This function is called whenever hnp params has been changed.
  92744. + * User can call get_b_hnp_enable, get_a_hnp_support, get_a_alt_hnp_support functions
  92745. + * to get hnp parameters. */
  92746. +typedef int (*dwc_hnp_params_changed_cb_t) (dwc_otg_pcd_t * pcd);
  92747. +/** This function is called whenever USB RESET is detected. */
  92748. +typedef int (*dwc_reset_cb_t) (dwc_otg_pcd_t * pcd);
  92749. +
  92750. +typedef int (*cfi_setup_cb_t) (dwc_otg_pcd_t * pcd, void *ctrl_req_bytes);
  92751. +
  92752. +/**
  92753. + *
  92754. + * @param ep_handle Void pointer to the usb_ep structure
  92755. + * @param ereq_port Pointer to the extended request structure created in the
  92756. + * portable part.
  92757. + */
  92758. +typedef int (*xiso_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  92759. + void *req_handle, int32_t status,
  92760. + void *ereq_port);
  92761. +/** Function Driver Ops Data Structure */
  92762. +struct dwc_otg_pcd_function_ops {
  92763. + dwc_connect_cb_t connect;
  92764. + dwc_disconnect_cb_t disconnect;
  92765. + dwc_setup_cb_t setup;
  92766. + dwc_completion_cb_t complete;
  92767. + dwc_isoc_completion_cb_t isoc_complete;
  92768. + dwc_suspend_cb_t suspend;
  92769. + dwc_sleep_cb_t sleep;
  92770. + dwc_resume_cb_t resume;
  92771. + dwc_reset_cb_t reset;
  92772. + dwc_hnp_params_changed_cb_t hnp_changed;
  92773. + cfi_setup_cb_t cfi_setup;
  92774. +#ifdef DWC_UTE_PER_IO
  92775. + xiso_completion_cb_t xisoc_complete;
  92776. +#endif
  92777. +};
  92778. +/** @} */
  92779. +
  92780. +/** @name Function Driver Functions */
  92781. +/** @{ */
  92782. +
  92783. +/** Call this function to get pointer on dwc_otg_pcd_t,
  92784. + * this pointer will be used for all PCD API functions.
  92785. + *
  92786. + * @param core_if The DWC_OTG Core
  92787. + */
  92788. +extern dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_core_if_t * core_if);
  92789. +
  92790. +/** Frees PCD allocated by dwc_otg_pcd_init
  92791. + *
  92792. + * @param pcd The PCD
  92793. + */
  92794. +extern void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd);
  92795. +
  92796. +/** Call this to bind the function driver to the PCD Core.
  92797. + *
  92798. + * @param pcd Pointer on dwc_otg_pcd_t returned by dwc_otg_pcd_init function.
  92799. + * @param fops The Function Driver Ops data structure containing pointers to all callbacks.
  92800. + */
  92801. +extern void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  92802. + const struct dwc_otg_pcd_function_ops *fops);
  92803. +
  92804. +/** Enables an endpoint for use. This function enables an endpoint in
  92805. + * the PCD. The endpoint is described by the ep_desc which has the
  92806. + * same format as a USB ep descriptor. The ep_handle parameter is used to refer
  92807. + * to the endpoint from other API functions and in callbacks. Normally this
  92808. + * should be called after a SET_CONFIGURATION/SET_INTERFACE to configure the
  92809. + * core for that interface.
  92810. + *
  92811. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  92812. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  92813. + * Returns 0 on success.
  92814. + *
  92815. + * @param pcd The PCD
  92816. + * @param ep_desc Endpoint descriptor
  92817. + * @param usb_ep Handle on endpoint, that will be used to identify endpoint.
  92818. + */
  92819. +extern int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  92820. + const uint8_t * ep_desc, void *usb_ep);
  92821. +
  92822. +/** Disable the endpoint referenced by ep_handle.
  92823. + *
  92824. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  92825. + * Returns -DWC_E_SHUTDOWN if any other error occurred.
  92826. + * Returns 0 on success. */
  92827. +extern int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle);
  92828. +
  92829. +/** Queue a data transfer request on the endpoint referenced by ep_handle.
  92830. + * After the transfer is completes, the complete callback will be called with
  92831. + * the request status.
  92832. + *
  92833. + * @param pcd The PCD
  92834. + * @param ep_handle The handle of the endpoint
  92835. + * @param buf The buffer for the data
  92836. + * @param dma_buf The DMA buffer for the data
  92837. + * @param buflen The length of the data transfer
  92838. + * @param zero Specifies whether to send zero length last packet.
  92839. + * @param req_handle Set this handle to any value to use to reference this
  92840. + * request in the ep_dequeue function or from the complete callback
  92841. + * @param atomic_alloc If driver need to perform atomic allocations
  92842. + * for internal data structures.
  92843. + *
  92844. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  92845. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  92846. + * Returns 0 on success. */
  92847. +extern int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  92848. + uint8_t * buf, dwc_dma_t dma_buf,
  92849. + uint32_t buflen, int zero, void *req_handle,
  92850. + int atomic_alloc);
  92851. +#ifdef DWC_UTE_PER_IO
  92852. +/**
  92853. + *
  92854. + * @param ereq_nonport Pointer to the extended request part of the
  92855. + * usb_request structure defined in usb_gadget.h file.
  92856. + */
  92857. +extern int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  92858. + uint8_t * buf, dwc_dma_t dma_buf,
  92859. + uint32_t buflen, int zero,
  92860. + void *req_handle, int atomic_alloc,
  92861. + void *ereq_nonport);
  92862. +
  92863. +#endif
  92864. +
  92865. +/** De-queue the specified data transfer that has not yet completed.
  92866. + *
  92867. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  92868. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  92869. + * Returns 0 on success. */
  92870. +extern int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  92871. + void *req_handle);
  92872. +
  92873. +/** Halt (STALL) an endpoint or clear it.
  92874. + *
  92875. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  92876. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  92877. + * Returns -DWC_E_AGAIN if the STALL cannot be sent and must be tried again later
  92878. + * Returns 0 on success. */
  92879. +extern int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value);
  92880. +
  92881. +/** This function */
  92882. +extern int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle);
  92883. +
  92884. +/** This function should be called on every hardware interrupt */
  92885. +extern int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd);
  92886. +
  92887. +/** This function returns current frame number */
  92888. +extern int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd);
  92889. +
  92890. +/**
  92891. + * Start isochronous transfers on the endpoint referenced by ep_handle.
  92892. + * For isochronous transfers duble buffering is used.
  92893. + * After processing each of buffers comlete callback will be called with
  92894. + * status for each transaction.
  92895. + *
  92896. + * @param pcd The PCD
  92897. + * @param ep_handle The handle of the endpoint
  92898. + * @param buf0 The virtual address of first data buffer
  92899. + * @param buf1 The virtual address of second data buffer
  92900. + * @param dma0 The DMA address of first data buffer
  92901. + * @param dma1 The DMA address of second data buffer
  92902. + * @param sync_frame Data pattern frame number
  92903. + * @param dp_frame Data size for pattern frame
  92904. + * @param data_per_frame Data size for regular frame
  92905. + * @param start_frame Frame number to start transfers, if -1 then start transfers ASAP.
  92906. + * @param buf_proc_intrvl Interval of ISOC Buffer processing
  92907. + * @param req_handle Handle of ISOC request
  92908. + * @param atomic_alloc Specefies whether to perform atomic allocation for
  92909. + * internal data structures.
  92910. + *
  92911. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  92912. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function.
  92913. + * Returns -DW_E_SHUTDOWN for any other error.
  92914. + * Returns 0 on success
  92915. + */
  92916. +extern int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  92917. + uint8_t * buf0, uint8_t * buf1,
  92918. + dwc_dma_t dma0, dwc_dma_t dma1,
  92919. + int sync_frame, int dp_frame,
  92920. + int data_per_frame, int start_frame,
  92921. + int buf_proc_intrvl, void *req_handle,
  92922. + int atomic_alloc);
  92923. +
  92924. +/** Stop ISOC transfers on endpoint referenced by ep_handle.
  92925. + *
  92926. + * @param pcd The PCD
  92927. + * @param ep_handle The handle of the endpoint
  92928. + * @param req_handle Handle of ISOC request
  92929. + *
  92930. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function
  92931. + * Returns 0 on success
  92932. + */
  92933. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  92934. + void *req_handle);
  92935. +
  92936. +/** Get ISOC packet status.
  92937. + *
  92938. + * @param pcd The PCD
  92939. + * @param ep_handle The handle of the endpoint
  92940. + * @param iso_req_handle Isochronoush request handle
  92941. + * @param packet Number of packet
  92942. + * @param status Out parameter for returning status
  92943. + * @param actual Out parameter for returning actual length
  92944. + * @param offset Out parameter for returning offset
  92945. + *
  92946. + */
  92947. +extern void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd,
  92948. + void *ep_handle,
  92949. + void *iso_req_handle, int packet,
  92950. + int *status, int *actual,
  92951. + int *offset);
  92952. +
  92953. +/** Get ISOC packet count.
  92954. + *
  92955. + * @param pcd The PCD
  92956. + * @param ep_handle The handle of the endpoint
  92957. + * @param iso_req_handle
  92958. + */
  92959. +extern int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd,
  92960. + void *ep_handle,
  92961. + void *iso_req_handle);
  92962. +
  92963. +/** This function starts the SRP Protocol if no session is in progress. If
  92964. + * a session is already in progress, but the device is suspended,
  92965. + * remote wakeup signaling is started.
  92966. + */
  92967. +extern int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd);
  92968. +
  92969. +/** This function returns 1 if LPM support is enabled, and 0 otherwise. */
  92970. +extern int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd);
  92971. +
  92972. +/** This function returns 1 if remote wakeup is allowed and 0, otherwise. */
  92973. +extern int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd);
  92974. +
  92975. +/** Initiate SRP */
  92976. +extern void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd);
  92977. +
  92978. +/** Starts remote wakeup signaling. */
  92979. +extern void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set);
  92980. +
  92981. +/** Starts micorsecond soft disconnect. */
  92982. +extern void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs);
  92983. +/** This function returns whether device is dualspeed.*/
  92984. +extern uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd);
  92985. +
  92986. +/** This function returns whether device is otg. */
  92987. +extern uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd);
  92988. +
  92989. +/** These functions allow to get hnp parameters */
  92990. +extern uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd);
  92991. +extern uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd);
  92992. +extern uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd);
  92993. +
  92994. +/** CFI specific Interface functions */
  92995. +/** Allocate a cfi buffer */
  92996. +extern uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep,
  92997. + dwc_dma_t * addr, size_t buflen,
  92998. + int flags);
  92999. +
  93000. +/******************************************************************************/
  93001. +
  93002. +/** @} */
  93003. +
  93004. +#endif /* __DWC_PCD_IF_H__ */
  93005. +
  93006. +#endif /* DWC_HOST_ONLY */
  93007. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
  93008. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c 1969-12-31 18:00:00.000000000 -0600
  93009. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c 2014-12-03 19:13:40.220418001 -0600
  93010. @@ -0,0 +1,5147 @@
  93011. +/* ==========================================================================
  93012. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_intr.c $
  93013. + * $Revision: #116 $
  93014. + * $Date: 2012/08/10 $
  93015. + * $Change: 2047372 $
  93016. + *
  93017. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  93018. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  93019. + * otherwise expressly agreed to in writing between Synopsys and you.
  93020. + *
  93021. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  93022. + * any End User Software License Agreement or Agreement for Licensed Product
  93023. + * with Synopsys or any supplement thereto. You are permitted to use and
  93024. + * redistribute this Software in source and binary forms, with or without
  93025. + * modification, provided that redistributions of source code must retain this
  93026. + * notice. You may not view, use, disclose, copy or distribute this file or
  93027. + * any information contained herein except pursuant to this license grant from
  93028. + * Synopsys. If you do not agree with this notice, including the disclaimer
  93029. + * below, then you are not authorized to use the Software.
  93030. + *
  93031. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  93032. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93033. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  93034. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  93035. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  93036. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  93037. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  93038. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  93039. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  93040. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  93041. + * DAMAGE.
  93042. + * ========================================================================== */
  93043. +#ifndef DWC_HOST_ONLY
  93044. +
  93045. +#include "dwc_otg_pcd.h"
  93046. +
  93047. +#ifdef DWC_UTE_CFI
  93048. +#include "dwc_otg_cfi.h"
  93049. +#endif
  93050. +
  93051. +#ifdef DWC_UTE_PER_IO
  93052. +extern void complete_xiso_ep(dwc_otg_pcd_ep_t * ep);
  93053. +#endif
  93054. +//#define PRINT_CFI_DMA_DESCS
  93055. +
  93056. +#define DEBUG_EP0
  93057. +
  93058. +/**
  93059. + * This function updates OTG.
  93060. + */
  93061. +static void dwc_otg_pcd_update_otg(dwc_otg_pcd_t * pcd, const unsigned reset)
  93062. +{
  93063. +
  93064. + if (reset) {
  93065. + pcd->b_hnp_enable = 0;
  93066. + pcd->a_hnp_support = 0;
  93067. + pcd->a_alt_hnp_support = 0;
  93068. + }
  93069. +
  93070. + if (pcd->fops->hnp_changed) {
  93071. + pcd->fops->hnp_changed(pcd);
  93072. + }
  93073. +}
  93074. +
  93075. +/** @file
  93076. + * This file contains the implementation of the PCD Interrupt handlers.
  93077. + *
  93078. + * The PCD handles the device interrupts. Many conditions can cause a
  93079. + * device interrupt. When an interrupt occurs, the device interrupt
  93080. + * service routine determines the cause of the interrupt and
  93081. + * dispatches handling to the appropriate function. These interrupt
  93082. + * handling functions are described below.
  93083. + * All interrupt registers are processed from LSB to MSB.
  93084. + */
  93085. +
  93086. +/**
  93087. + * This function prints the ep0 state for debug purposes.
  93088. + */
  93089. +static inline void print_ep0_state(dwc_otg_pcd_t * pcd)
  93090. +{
  93091. +#ifdef DEBUG
  93092. + char str[40];
  93093. +
  93094. + switch (pcd->ep0state) {
  93095. + case EP0_DISCONNECT:
  93096. + dwc_strcpy(str, "EP0_DISCONNECT");
  93097. + break;
  93098. + case EP0_IDLE:
  93099. + dwc_strcpy(str, "EP0_IDLE");
  93100. + break;
  93101. + case EP0_IN_DATA_PHASE:
  93102. + dwc_strcpy(str, "EP0_IN_DATA_PHASE");
  93103. + break;
  93104. + case EP0_OUT_DATA_PHASE:
  93105. + dwc_strcpy(str, "EP0_OUT_DATA_PHASE");
  93106. + break;
  93107. + case EP0_IN_STATUS_PHASE:
  93108. + dwc_strcpy(str, "EP0_IN_STATUS_PHASE");
  93109. + break;
  93110. + case EP0_OUT_STATUS_PHASE:
  93111. + dwc_strcpy(str, "EP0_OUT_STATUS_PHASE");
  93112. + break;
  93113. + case EP0_STALL:
  93114. + dwc_strcpy(str, "EP0_STALL");
  93115. + break;
  93116. + default:
  93117. + dwc_strcpy(str, "EP0_INVALID");
  93118. + }
  93119. +
  93120. + DWC_DEBUGPL(DBG_ANY, "%s(%d)\n", str, pcd->ep0state);
  93121. +#endif
  93122. +}
  93123. +
  93124. +/**
  93125. + * This function calculate the size of the payload in the memory
  93126. + * for out endpoints and prints size for debug purposes(used in
  93127. + * 2.93a DevOutNak feature).
  93128. + */
  93129. +static inline void print_memory_payload(dwc_otg_pcd_t * pcd, dwc_ep_t * ep)
  93130. +{
  93131. +#ifdef DEBUG
  93132. + deptsiz_data_t deptsiz_init = {.d32 = 0 };
  93133. + deptsiz_data_t deptsiz_updt = {.d32 = 0 };
  93134. + int pack_num;
  93135. + unsigned payload;
  93136. +
  93137. + deptsiz_init.d32 = pcd->core_if->start_doeptsiz_val[ep->num];
  93138. + deptsiz_updt.d32 =
  93139. + DWC_READ_REG32(&pcd->core_if->dev_if->
  93140. + out_ep_regs[ep->num]->doeptsiz);
  93141. + /* Payload will be */
  93142. + payload = deptsiz_init.b.xfersize - deptsiz_updt.b.xfersize;
  93143. + /* Packet count is decremented every time a packet
  93144. + * is written to the RxFIFO not in to the external memory
  93145. + * So, if payload == 0, then it means no packet was sent to ext memory*/
  93146. + pack_num = (!payload) ? 0 : (deptsiz_init.b.pktcnt - deptsiz_updt.b.pktcnt);
  93147. + DWC_DEBUGPL(DBG_PCDV,
  93148. + "Payload for EP%d-%s\n",
  93149. + ep->num, (ep->is_in ? "IN" : "OUT"));
  93150. + DWC_DEBUGPL(DBG_PCDV,
  93151. + "Number of transfered bytes = 0x%08x\n", payload);
  93152. + DWC_DEBUGPL(DBG_PCDV,
  93153. + "Number of transfered packets = %d\n", pack_num);
  93154. +#endif
  93155. +}
  93156. +
  93157. +
  93158. +#ifdef DWC_UTE_CFI
  93159. +static inline void print_desc(struct dwc_otg_dma_desc *ddesc,
  93160. + const uint8_t * epname, int descnum)
  93161. +{
  93162. + CFI_INFO
  93163. + ("%s DMA_DESC(%d) buf=0x%08x bytes=0x%04x; sp=0x%x; l=0x%x; sts=0x%02x; bs=0x%02x\n",
  93164. + epname, descnum, ddesc->buf, ddesc->status.b.bytes,
  93165. + ddesc->status.b.sp, ddesc->status.b.l, ddesc->status.b.sts,
  93166. + ddesc->status.b.bs);
  93167. +}
  93168. +#endif
  93169. +
  93170. +/**
  93171. + * This function returns pointer to in ep struct with number ep_num
  93172. + */
  93173. +static inline dwc_otg_pcd_ep_t *get_in_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  93174. +{
  93175. + int i;
  93176. + int num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  93177. + if (ep_num == 0) {
  93178. + return &pcd->ep0;
  93179. + } else {
  93180. + for (i = 0; i < num_in_eps; ++i) {
  93181. + if (pcd->in_ep[i].dwc_ep.num == ep_num)
  93182. + return &pcd->in_ep[i];
  93183. + }
  93184. + return 0;
  93185. + }
  93186. +}
  93187. +
  93188. +/**
  93189. + * This function returns pointer to out ep struct with number ep_num
  93190. + */
  93191. +static inline dwc_otg_pcd_ep_t *get_out_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  93192. +{
  93193. + int i;
  93194. + int num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  93195. + if (ep_num == 0) {
  93196. + return &pcd->ep0;
  93197. + } else {
  93198. + for (i = 0; i < num_out_eps; ++i) {
  93199. + if (pcd->out_ep[i].dwc_ep.num == ep_num)
  93200. + return &pcd->out_ep[i];
  93201. + }
  93202. + return 0;
  93203. + }
  93204. +}
  93205. +
  93206. +/**
  93207. + * This functions gets a pointer to an EP from the wIndex address
  93208. + * value of the control request.
  93209. + */
  93210. +dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex)
  93211. +{
  93212. + dwc_otg_pcd_ep_t *ep;
  93213. + uint32_t ep_num = UE_GET_ADDR(wIndex);
  93214. +
  93215. + if (ep_num == 0) {
  93216. + ep = &pcd->ep0;
  93217. + } else if (UE_GET_DIR(wIndex) == UE_DIR_IN) { /* in ep */
  93218. + ep = &pcd->in_ep[ep_num - 1];
  93219. + } else {
  93220. + ep = &pcd->out_ep[ep_num - 1];
  93221. + }
  93222. +
  93223. + return ep;
  93224. +}
  93225. +
  93226. +/**
  93227. + * This function checks the EP request queue, if the queue is not
  93228. + * empty the next request is started.
  93229. + */
  93230. +void start_next_request(dwc_otg_pcd_ep_t * ep)
  93231. +{
  93232. + dwc_otg_pcd_request_t *req = 0;
  93233. + uint32_t max_transfer =
  93234. + GET_CORE_IF(ep->pcd)->core_params->max_transfer_size;
  93235. +
  93236. +#ifdef DWC_UTE_CFI
  93237. + struct dwc_otg_pcd *pcd;
  93238. + pcd = ep->pcd;
  93239. +#endif
  93240. +
  93241. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  93242. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  93243. +
  93244. +#ifdef DWC_UTE_CFI
  93245. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  93246. + ep->dwc_ep.cfi_req_len = req->length;
  93247. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd, ep, req);
  93248. + } else {
  93249. +#endif
  93250. + /* Setup and start the Transfer */
  93251. + if (req->dw_align_buf) {
  93252. + ep->dwc_ep.dma_addr = req->dw_align_buf_dma;
  93253. + ep->dwc_ep.start_xfer_buff = req->dw_align_buf;
  93254. + ep->dwc_ep.xfer_buff = req->dw_align_buf;
  93255. + } else {
  93256. + ep->dwc_ep.dma_addr = req->dma;
  93257. + ep->dwc_ep.start_xfer_buff = req->buf;
  93258. + ep->dwc_ep.xfer_buff = req->buf;
  93259. + }
  93260. + ep->dwc_ep.sent_zlp = 0;
  93261. + ep->dwc_ep.total_len = req->length;
  93262. + ep->dwc_ep.xfer_len = 0;
  93263. + ep->dwc_ep.xfer_count = 0;
  93264. +
  93265. + ep->dwc_ep.maxxfer = max_transfer;
  93266. + if (GET_CORE_IF(ep->pcd)->dma_desc_enable) {
  93267. + uint32_t out_max_xfer = DDMA_MAX_TRANSFER_SIZE
  93268. + - (DDMA_MAX_TRANSFER_SIZE % 4);
  93269. + if (ep->dwc_ep.is_in) {
  93270. + if (ep->dwc_ep.maxxfer >
  93271. + DDMA_MAX_TRANSFER_SIZE) {
  93272. + ep->dwc_ep.maxxfer =
  93273. + DDMA_MAX_TRANSFER_SIZE;
  93274. + }
  93275. + } else {
  93276. + if (ep->dwc_ep.maxxfer > out_max_xfer) {
  93277. + ep->dwc_ep.maxxfer =
  93278. + out_max_xfer;
  93279. + }
  93280. + }
  93281. + }
  93282. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  93283. + ep->dwc_ep.maxxfer -=
  93284. + (ep->dwc_ep.maxxfer % ep->dwc_ep.maxpacket);
  93285. + }
  93286. + if (req->sent_zlp) {
  93287. + if ((ep->dwc_ep.total_len %
  93288. + ep->dwc_ep.maxpacket == 0)
  93289. + && (ep->dwc_ep.total_len != 0)) {
  93290. + ep->dwc_ep.sent_zlp = 1;
  93291. + }
  93292. +
  93293. + }
  93294. +#ifdef DWC_UTE_CFI
  93295. + }
  93296. +#endif
  93297. + dwc_otg_ep_start_transfer(GET_CORE_IF(ep->pcd), &ep->dwc_ep);
  93298. + } else if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  93299. + DWC_PRINTF("There are no more ISOC requests \n");
  93300. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  93301. + }
  93302. +}
  93303. +
  93304. +/**
  93305. + * This function handles the SOF Interrupts. At this time the SOF
  93306. + * Interrupt is disabled.
  93307. + */
  93308. +int32_t dwc_otg_pcd_handle_sof_intr(dwc_otg_pcd_t * pcd)
  93309. +{
  93310. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  93311. +
  93312. + gintsts_data_t gintsts;
  93313. +
  93314. + DWC_DEBUGPL(DBG_PCD, "SOF\n");
  93315. +
  93316. + /* Clear interrupt */
  93317. + gintsts.d32 = 0;
  93318. + gintsts.b.sofintr = 1;
  93319. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  93320. +
  93321. + return 1;
  93322. +}
  93323. +
  93324. +/**
  93325. + * This function handles the Rx Status Queue Level Interrupt, which
  93326. + * indicates that there is a least one packet in the Rx FIFO. The
  93327. + * packets are moved from the FIFO to memory, where they will be
  93328. + * processed when the Endpoint Interrupt Register indicates Transfer
  93329. + * Complete or SETUP Phase Done.
  93330. + *
  93331. + * Repeat the following until the Rx Status Queue is empty:
  93332. + * -# Read the Receive Status Pop Register (GRXSTSP) to get Packet
  93333. + * info
  93334. + * -# If Receive FIFO is empty then skip to step Clear the interrupt
  93335. + * and exit
  93336. + * -# If SETUP Packet call dwc_otg_read_setup_packet to copy the
  93337. + * SETUP data to the buffer
  93338. + * -# If OUT Data Packet call dwc_otg_read_packet to copy the data
  93339. + * to the destination buffer
  93340. + */
  93341. +int32_t dwc_otg_pcd_handle_rx_status_q_level_intr(dwc_otg_pcd_t * pcd)
  93342. +{
  93343. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  93344. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  93345. + gintmsk_data_t gintmask = {.d32 = 0 };
  93346. + device_grxsts_data_t status;
  93347. + dwc_otg_pcd_ep_t *ep;
  93348. + gintsts_data_t gintsts;
  93349. +#ifdef DEBUG
  93350. + static char *dpid_str[] = { "D0", "D2", "D1", "MDATA" };
  93351. +#endif
  93352. +
  93353. + //DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _pcd);
  93354. + /* Disable the Rx Status Queue Level interrupt */
  93355. + gintmask.b.rxstsqlvl = 1;
  93356. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmask.d32, 0);
  93357. +
  93358. + /* Get the Status from the top of the FIFO */
  93359. + status.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  93360. +
  93361. + DWC_DEBUGPL(DBG_PCD, "EP:%d BCnt:%d DPID:%s "
  93362. + "pktsts:%x Frame:%d(0x%0x)\n",
  93363. + status.b.epnum, status.b.bcnt,
  93364. + dpid_str[status.b.dpid],
  93365. + status.b.pktsts, status.b.fn, status.b.fn);
  93366. + /* Get pointer to EP structure */
  93367. + ep = get_out_ep(pcd, status.b.epnum);
  93368. +
  93369. + switch (status.b.pktsts) {
  93370. + case DWC_DSTS_GOUT_NAK:
  93371. + DWC_DEBUGPL(DBG_PCDV, "Global OUT NAK\n");
  93372. + break;
  93373. + case DWC_STS_DATA_UPDT:
  93374. + DWC_DEBUGPL(DBG_PCDV, "OUT Data Packet\n");
  93375. + if (status.b.bcnt && ep->dwc_ep.xfer_buff) {
  93376. + /** @todo NGS Check for buffer overflow? */
  93377. + dwc_otg_read_packet(core_if,
  93378. + ep->dwc_ep.xfer_buff,
  93379. + status.b.bcnt);
  93380. + ep->dwc_ep.xfer_count += status.b.bcnt;
  93381. + ep->dwc_ep.xfer_buff += status.b.bcnt;
  93382. + }
  93383. + break;
  93384. + case DWC_STS_XFER_COMP:
  93385. + DWC_DEBUGPL(DBG_PCDV, "OUT Complete\n");
  93386. + break;
  93387. + case DWC_DSTS_SETUP_COMP:
  93388. +#ifdef DEBUG_EP0
  93389. + DWC_DEBUGPL(DBG_PCDV, "Setup Complete\n");
  93390. +#endif
  93391. + break;
  93392. + case DWC_DSTS_SETUP_UPDT:
  93393. + dwc_otg_read_setup_packet(core_if, pcd->setup_pkt->d32);
  93394. +#ifdef DEBUG_EP0
  93395. + DWC_DEBUGPL(DBG_PCD,
  93396. + "SETUP PKT: %02x.%02x v%04x i%04x l%04x\n",
  93397. + pcd->setup_pkt->req.bmRequestType,
  93398. + pcd->setup_pkt->req.bRequest,
  93399. + UGETW(pcd->setup_pkt->req.wValue),
  93400. + UGETW(pcd->setup_pkt->req.wIndex),
  93401. + UGETW(pcd->setup_pkt->req.wLength));
  93402. +#endif
  93403. + ep->dwc_ep.xfer_count += status.b.bcnt;
  93404. + break;
  93405. + default:
  93406. + DWC_DEBUGPL(DBG_PCDV, "Invalid Packet Status (0x%0x)\n",
  93407. + status.b.pktsts);
  93408. + break;
  93409. + }
  93410. +
  93411. + /* Enable the Rx Status Queue Level interrupt */
  93412. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmask.d32);
  93413. + /* Clear interrupt */
  93414. + gintsts.d32 = 0;
  93415. + gintsts.b.rxstsqlvl = 1;
  93416. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  93417. +
  93418. + //DWC_DEBUGPL(DBG_PCDV, "EXIT: %s\n", __func__);
  93419. + return 1;
  93420. +}
  93421. +
  93422. +/**
  93423. + * This function examines the Device IN Token Learning Queue to
  93424. + * determine the EP number of the last IN token received. This
  93425. + * implementation is for the Mass Storage device where there are only
  93426. + * 2 IN EPs (Control-IN and BULK-IN).
  93427. + *
  93428. + * The EP numbers for the first six IN Tokens are in DTKNQR1 and there
  93429. + * are 8 EP Numbers in each of the other possible DTKNQ Registers.
  93430. + *
  93431. + * @param core_if Programming view of DWC_otg controller.
  93432. + *
  93433. + */
  93434. +static inline int get_ep_of_last_in_token(dwc_otg_core_if_t * core_if)
  93435. +{
  93436. + dwc_otg_device_global_regs_t *dev_global_regs =
  93437. + core_if->dev_if->dev_global_regs;
  93438. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  93439. + /* Number of Token Queue Registers */
  93440. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  93441. + dtknq1_data_t dtknqr1;
  93442. + uint32_t in_tkn_epnums[4];
  93443. + int ndx = 0;
  93444. + int i = 0;
  93445. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  93446. + int epnum = 0;
  93447. +
  93448. + //DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  93449. +
  93450. + /* Read the DTKNQ Registers */
  93451. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  93452. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  93453. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  93454. + in_tkn_epnums[i]);
  93455. + if (addr == &dev_global_regs->dvbusdis) {
  93456. + addr = &dev_global_regs->dtknqr3_dthrctl;
  93457. + } else {
  93458. + ++addr;
  93459. + }
  93460. +
  93461. + }
  93462. +
  93463. + /* Copy the DTKNQR1 data to the bit field. */
  93464. + dtknqr1.d32 = in_tkn_epnums[0];
  93465. + /* Get the EP numbers */
  93466. + in_tkn_epnums[0] = dtknqr1.b.epnums0_5;
  93467. + ndx = dtknqr1.b.intknwptr - 1;
  93468. +
  93469. + //DWC_DEBUGPL(DBG_PCDV,"ndx=%d\n",ndx);
  93470. + if (ndx == -1) {
  93471. + /** @todo Find a simpler way to calculate the max
  93472. + * queue position.*/
  93473. + int cnt = TOKEN_Q_DEPTH;
  93474. + if (TOKEN_Q_DEPTH <= 6) {
  93475. + cnt = TOKEN_Q_DEPTH - 1;
  93476. + } else if (TOKEN_Q_DEPTH <= 14) {
  93477. + cnt = TOKEN_Q_DEPTH - 7;
  93478. + } else if (TOKEN_Q_DEPTH <= 22) {
  93479. + cnt = TOKEN_Q_DEPTH - 15;
  93480. + } else {
  93481. + cnt = TOKEN_Q_DEPTH - 23;
  93482. + }
  93483. + epnum = (in_tkn_epnums[DTKNQ_REG_CNT - 1] >> (cnt * 4)) & 0xF;
  93484. + } else {
  93485. + if (ndx <= 5) {
  93486. + epnum = (in_tkn_epnums[0] >> (ndx * 4)) & 0xF;
  93487. + } else if (ndx <= 13) {
  93488. + ndx -= 6;
  93489. + epnum = (in_tkn_epnums[1] >> (ndx * 4)) & 0xF;
  93490. + } else if (ndx <= 21) {
  93491. + ndx -= 14;
  93492. + epnum = (in_tkn_epnums[2] >> (ndx * 4)) & 0xF;
  93493. + } else if (ndx <= 29) {
  93494. + ndx -= 22;
  93495. + epnum = (in_tkn_epnums[3] >> (ndx * 4)) & 0xF;
  93496. + }
  93497. + }
  93498. + //DWC_DEBUGPL(DBG_PCD,"epnum=%d\n",epnum);
  93499. + return epnum;
  93500. +}
  93501. +
  93502. +/**
  93503. + * This interrupt occurs when the non-periodic Tx FIFO is half-empty.
  93504. + * The active request is checked for the next packet to be loaded into
  93505. + * the non-periodic Tx FIFO.
  93506. + */
  93507. +int32_t dwc_otg_pcd_handle_np_tx_fifo_empty_intr(dwc_otg_pcd_t * pcd)
  93508. +{
  93509. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  93510. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  93511. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  93512. + gnptxsts_data_t txstatus = {.d32 = 0 };
  93513. + gintsts_data_t gintsts;
  93514. +
  93515. + int epnum = 0;
  93516. + dwc_otg_pcd_ep_t *ep = 0;
  93517. + uint32_t len = 0;
  93518. + int dwords;
  93519. +
  93520. + /* Get the epnum from the IN Token Learning Queue. */
  93521. + epnum = get_ep_of_last_in_token(core_if);
  93522. + ep = get_in_ep(pcd, epnum);
  93523. +
  93524. + DWC_DEBUGPL(DBG_PCD, "NP TxFifo Empty: %d \n", epnum);
  93525. +
  93526. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  93527. +
  93528. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  93529. + if (len > ep->dwc_ep.maxpacket) {
  93530. + len = ep->dwc_ep.maxpacket;
  93531. + }
  93532. + dwords = (len + 3) / 4;
  93533. +
  93534. + /* While there is space in the queue and space in the FIFO and
  93535. + * More data to tranfer, Write packets to the Tx FIFO */
  93536. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  93537. + DWC_DEBUGPL(DBG_PCDV, "b4 GNPTXSTS=0x%08x\n", txstatus.d32);
  93538. +
  93539. + while (txstatus.b.nptxqspcavail > 0 &&
  93540. + txstatus.b.nptxfspcavail > dwords &&
  93541. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len) {
  93542. + /* Write the FIFO */
  93543. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  93544. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  93545. +
  93546. + if (len > ep->dwc_ep.maxpacket) {
  93547. + len = ep->dwc_ep.maxpacket;
  93548. + }
  93549. +
  93550. + dwords = (len + 3) / 4;
  93551. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  93552. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n", txstatus.d32);
  93553. + }
  93554. +
  93555. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n",
  93556. + DWC_READ_REG32(&global_regs->gnptxsts));
  93557. +
  93558. + /* Clear interrupt */
  93559. + gintsts.d32 = 0;
  93560. + gintsts.b.nptxfempty = 1;
  93561. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  93562. +
  93563. + return 1;
  93564. +}
  93565. +
  93566. +/**
  93567. + * This function is called when dedicated Tx FIFO Empty interrupt occurs.
  93568. + * The active request is checked for the next packet to be loaded into
  93569. + * apropriate Tx FIFO.
  93570. + */
  93571. +static int32_t write_empty_tx_fifo(dwc_otg_pcd_t * pcd, uint32_t epnum)
  93572. +{
  93573. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  93574. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  93575. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  93576. + dtxfsts_data_t txstatus = {.d32 = 0 };
  93577. + dwc_otg_pcd_ep_t *ep = 0;
  93578. + uint32_t len = 0;
  93579. + int dwords;
  93580. +
  93581. + ep = get_in_ep(pcd, epnum);
  93582. +
  93583. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  93584. +
  93585. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  93586. +
  93587. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  93588. +
  93589. + if (len > ep->dwc_ep.maxpacket) {
  93590. + len = ep->dwc_ep.maxpacket;
  93591. + }
  93592. +
  93593. + dwords = (len + 3) / 4;
  93594. +
  93595. + /* While there is space in the queue and space in the FIFO and
  93596. + * More data to tranfer, Write packets to the Tx FIFO */
  93597. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  93598. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  93599. +
  93600. + while (txstatus.b.txfspcavail > dwords &&
  93601. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len &&
  93602. + ep->dwc_ep.xfer_len != 0) {
  93603. + /* Write the FIFO */
  93604. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  93605. +
  93606. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  93607. + if (len > ep->dwc_ep.maxpacket) {
  93608. + len = ep->dwc_ep.maxpacket;
  93609. + }
  93610. +
  93611. + dwords = (len + 3) / 4;
  93612. + txstatus.d32 =
  93613. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  93614. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  93615. + txstatus.d32);
  93616. + }
  93617. +
  93618. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  93619. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  93620. +
  93621. + return 1;
  93622. +}
  93623. +
  93624. +/**
  93625. + * This function is called when the Device is disconnected. It stops
  93626. + * any active requests and informs the Gadget driver of the
  93627. + * disconnect.
  93628. + */
  93629. +void dwc_otg_pcd_stop(dwc_otg_pcd_t * pcd)
  93630. +{
  93631. + int i, num_in_eps, num_out_eps;
  93632. + dwc_otg_pcd_ep_t *ep;
  93633. +
  93634. + gintmsk_data_t intr_mask = {.d32 = 0 };
  93635. +
  93636. + DWC_SPINLOCK(pcd->lock);
  93637. +
  93638. + num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  93639. + num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  93640. +
  93641. + DWC_DEBUGPL(DBG_PCDV, "%s() \n", __func__);
  93642. + /* don't disconnect drivers more than once */
  93643. + if (pcd->ep0state == EP0_DISCONNECT) {
  93644. + DWC_DEBUGPL(DBG_ANY, "%s() Already Disconnected\n", __func__);
  93645. + DWC_SPINUNLOCK(pcd->lock);
  93646. + return;
  93647. + }
  93648. + pcd->ep0state = EP0_DISCONNECT;
  93649. +
  93650. + /* Reset the OTG state. */
  93651. + dwc_otg_pcd_update_otg(pcd, 1);
  93652. +
  93653. + /* Disable the NP Tx Fifo Empty Interrupt. */
  93654. + intr_mask.b.nptxfempty = 1;
  93655. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  93656. + intr_mask.d32, 0);
  93657. +
  93658. + /* Flush the FIFOs */
  93659. + /**@todo NGS Flush Periodic FIFOs */
  93660. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd), 0x10);
  93661. + dwc_otg_flush_rx_fifo(GET_CORE_IF(pcd));
  93662. +
  93663. + /* prevent new request submissions, kill any outstanding requests */
  93664. + ep = &pcd->ep0;
  93665. + dwc_otg_request_nuke(ep);
  93666. + /* prevent new request submissions, kill any outstanding requests */
  93667. + for (i = 0; i < num_in_eps; i++) {
  93668. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[i];
  93669. + dwc_otg_request_nuke(ep);
  93670. + }
  93671. + /* prevent new request submissions, kill any outstanding requests */
  93672. + for (i = 0; i < num_out_eps; i++) {
  93673. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[i];
  93674. + dwc_otg_request_nuke(ep);
  93675. + }
  93676. +
  93677. + /* report disconnect; the driver is already quiesced */
  93678. + if (pcd->fops->disconnect) {
  93679. + DWC_SPINUNLOCK(pcd->lock);
  93680. + pcd->fops->disconnect(pcd);
  93681. + DWC_SPINLOCK(pcd->lock);
  93682. + }
  93683. + DWC_SPINUNLOCK(pcd->lock);
  93684. +}
  93685. +
  93686. +/**
  93687. + * This interrupt indicates that ...
  93688. + */
  93689. +int32_t dwc_otg_pcd_handle_i2c_intr(dwc_otg_pcd_t * pcd)
  93690. +{
  93691. + gintmsk_data_t intr_mask = {.d32 = 0 };
  93692. + gintsts_data_t gintsts;
  93693. +
  93694. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "i2cintr");
  93695. + intr_mask.b.i2cintr = 1;
  93696. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  93697. + intr_mask.d32, 0);
  93698. +
  93699. + /* Clear interrupt */
  93700. + gintsts.d32 = 0;
  93701. + gintsts.b.i2cintr = 1;
  93702. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  93703. + gintsts.d32);
  93704. + return 1;
  93705. +}
  93706. +
  93707. +/**
  93708. + * This interrupt indicates that ...
  93709. + */
  93710. +int32_t dwc_otg_pcd_handle_early_suspend_intr(dwc_otg_pcd_t * pcd)
  93711. +{
  93712. + gintsts_data_t gintsts;
  93713. +#if defined(VERBOSE)
  93714. + DWC_PRINTF("Early Suspend Detected\n");
  93715. +#endif
  93716. +
  93717. + /* Clear interrupt */
  93718. + gintsts.d32 = 0;
  93719. + gintsts.b.erlysuspend = 1;
  93720. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  93721. + gintsts.d32);
  93722. + return 1;
  93723. +}
  93724. +
  93725. +/**
  93726. + * This function configures EPO to receive SETUP packets.
  93727. + *
  93728. + * @todo NGS: Update the comments from the HW FS.
  93729. + *
  93730. + * -# Program the following fields in the endpoint specific registers
  93731. + * for Control OUT EP 0, in order to receive a setup packet
  93732. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  93733. + * setup packets)
  93734. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  93735. + * to back setup packets)
  93736. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  93737. + * store any setup packets received
  93738. + *
  93739. + * @param core_if Programming view of DWC_otg controller.
  93740. + * @param pcd Programming view of the PCD.
  93741. + */
  93742. +static inline void ep0_out_start(dwc_otg_core_if_t * core_if,
  93743. + dwc_otg_pcd_t * pcd)
  93744. +{
  93745. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  93746. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  93747. + dwc_otg_dev_dma_desc_t *dma_desc;
  93748. + depctl_data_t doepctl = {.d32 = 0 };
  93749. +
  93750. +#ifdef VERBOSE
  93751. + DWC_DEBUGPL(DBG_PCDV, "%s() doepctl0=%0x\n", __func__,
  93752. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  93753. +#endif
  93754. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  93755. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  93756. + if (doepctl.b.epena) {
  93757. + return;
  93758. + }
  93759. + }
  93760. +
  93761. + doeptsize0.b.supcnt = 3;
  93762. + doeptsize0.b.pktcnt = 1;
  93763. + doeptsize0.b.xfersize = 8 * 3;
  93764. +
  93765. + if (core_if->dma_enable) {
  93766. + if (!core_if->dma_desc_enable) {
  93767. + /** put here as for Hermes mode deptisz register should not be written */
  93768. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  93769. + doeptsize0.d32);
  93770. +
  93771. + /** @todo dma needs to handle multiple setup packets (up to 3) */
  93772. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  93773. + pcd->setup_pkt_dma_handle);
  93774. + } else {
  93775. + dev_if->setup_desc_index =
  93776. + (dev_if->setup_desc_index + 1) & 1;
  93777. + dma_desc =
  93778. + dev_if->setup_desc_addr[dev_if->setup_desc_index];
  93779. +
  93780. + /** DMA Descriptor Setup */
  93781. + dma_desc->status.b.bs = BS_HOST_BUSY;
  93782. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  93783. + dma_desc->status.b.sr = 0;
  93784. + dma_desc->status.b.mtrf = 0;
  93785. + }
  93786. + dma_desc->status.b.l = 1;
  93787. + dma_desc->status.b.ioc = 1;
  93788. + dma_desc->status.b.bytes = pcd->ep0.dwc_ep.maxpacket;
  93789. + dma_desc->buf = pcd->setup_pkt_dma_handle;
  93790. + dma_desc->status.b.sts = 0;
  93791. + dma_desc->status.b.bs = BS_HOST_READY;
  93792. +
  93793. + /** DOEPDMA0 Register write */
  93794. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  93795. + dev_if->dma_setup_desc_addr
  93796. + [dev_if->setup_desc_index]);
  93797. + }
  93798. +
  93799. + } else {
  93800. + /** put here as for Hermes mode deptisz register should not be written */
  93801. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  93802. + doeptsize0.d32);
  93803. + }
  93804. +
  93805. + /** DOEPCTL0 Register write cnak will be set after setup interrupt */
  93806. + doepctl.d32 = 0;
  93807. + doepctl.b.epena = 1;
  93808. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  93809. + doepctl.b.cnak = 1;
  93810. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  93811. + } else {
  93812. + DWC_MODIFY_REG32(&dev_if->out_ep_regs[0]->doepctl, 0, doepctl.d32);
  93813. + }
  93814. +
  93815. +#ifdef VERBOSE
  93816. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  93817. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  93818. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  93819. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  93820. +#endif
  93821. +}
  93822. +
  93823. +/**
  93824. + * This interrupt occurs when a USB Reset is detected. When the USB
  93825. + * Reset Interrupt occurs the device state is set to DEFAULT and the
  93826. + * EP0 state is set to IDLE.
  93827. + * -# Set the NAK bit for all OUT endpoints (DOEPCTLn.SNAK = 1)
  93828. + * -# Unmask the following interrupt bits
  93829. + * - DAINTMSK.INEP0 = 1 (Control 0 IN endpoint)
  93830. + * - DAINTMSK.OUTEP0 = 1 (Control 0 OUT endpoint)
  93831. + * - DOEPMSK.SETUP = 1
  93832. + * - DOEPMSK.XferCompl = 1
  93833. + * - DIEPMSK.XferCompl = 1
  93834. + * - DIEPMSK.TimeOut = 1
  93835. + * -# Program the following fields in the endpoint specific registers
  93836. + * for Control OUT EP 0, in order to receive a setup packet
  93837. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  93838. + * setup packets)
  93839. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  93840. + * to back setup packets)
  93841. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  93842. + * store any setup packets received
  93843. + * At this point, all the required initialization, except for enabling
  93844. + * the control 0 OUT endpoint is done, for receiving SETUP packets.
  93845. + */
  93846. +int32_t dwc_otg_pcd_handle_usb_reset_intr(dwc_otg_pcd_t * pcd)
  93847. +{
  93848. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  93849. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  93850. + depctl_data_t doepctl = {.d32 = 0 };
  93851. + depctl_data_t diepctl = {.d32 = 0 };
  93852. + daint_data_t daintmsk = {.d32 = 0 };
  93853. + doepmsk_data_t doepmsk = {.d32 = 0 };
  93854. + diepmsk_data_t diepmsk = {.d32 = 0 };
  93855. + dcfg_data_t dcfg = {.d32 = 0 };
  93856. + grstctl_t resetctl = {.d32 = 0 };
  93857. + dctl_data_t dctl = {.d32 = 0 };
  93858. + int i = 0;
  93859. + gintsts_data_t gintsts;
  93860. + pcgcctl_data_t power = {.d32 = 0 };
  93861. +
  93862. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  93863. + if (power.b.stoppclk) {
  93864. + power.d32 = 0;
  93865. + power.b.stoppclk = 1;
  93866. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  93867. +
  93868. + power.b.pwrclmp = 1;
  93869. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  93870. +
  93871. + power.b.rstpdwnmodule = 1;
  93872. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  93873. + }
  93874. +
  93875. + core_if->lx_state = DWC_OTG_L0;
  93876. +
  93877. + DWC_PRINTF("USB RESET\n");
  93878. +#ifdef DWC_EN_ISOC
  93879. + for (i = 1; i < 16; ++i) {
  93880. + dwc_otg_pcd_ep_t *ep;
  93881. + dwc_ep_t *dwc_ep;
  93882. + ep = get_in_ep(pcd, i);
  93883. + if (ep != 0) {
  93884. + dwc_ep = &ep->dwc_ep;
  93885. + dwc_ep->next_frame = 0xffffffff;
  93886. + }
  93887. + }
  93888. +#endif /* DWC_EN_ISOC */
  93889. +
  93890. + /* reset the HNP settings */
  93891. + dwc_otg_pcd_update_otg(pcd, 1);
  93892. +
  93893. + /* Clear the Remote Wakeup Signalling */
  93894. + dctl.b.rmtwkupsig = 1;
  93895. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  93896. +
  93897. + /* Set NAK for all OUT EPs */
  93898. + doepctl.b.snak = 1;
  93899. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  93900. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  93901. + }
  93902. +
  93903. + /* Flush the NP Tx FIFO */
  93904. + dwc_otg_flush_tx_fifo(core_if, 0x10);
  93905. + /* Flush the Learning Queue */
  93906. + resetctl.b.intknqflsh = 1;
  93907. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  93908. +
  93909. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  93910. + core_if->start_predict = 0;
  93911. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  93912. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  93913. + }
  93914. + core_if->nextep_seq[0] = 0;
  93915. + core_if->first_in_nextep_seq = 0;
  93916. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  93917. + diepctl.b.nextep = 0;
  93918. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  93919. +
  93920. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  93921. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  93922. + dcfg.b.epmscnt = 2;
  93923. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  93924. +
  93925. + DWC_DEBUGPL(DBG_PCDV,
  93926. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  93927. + __func__, core_if->first_in_nextep_seq);
  93928. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  93929. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  93930. + }
  93931. + }
  93932. +
  93933. + if (core_if->multiproc_int_enable) {
  93934. + daintmsk.b.inep0 = 1;
  93935. + daintmsk.b.outep0 = 1;
  93936. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk,
  93937. + daintmsk.d32);
  93938. +
  93939. + doepmsk.b.setup = 1;
  93940. + doepmsk.b.xfercompl = 1;
  93941. + doepmsk.b.ahberr = 1;
  93942. + doepmsk.b.epdisabled = 1;
  93943. +
  93944. + if ((core_if->dma_desc_enable) ||
  93945. + (core_if->dma_enable
  93946. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  93947. + doepmsk.b.stsphsercvd = 1;
  93948. + }
  93949. + if (core_if->dma_desc_enable)
  93950. + doepmsk.b.bna = 1;
  93951. +/*
  93952. + doepmsk.b.babble = 1;
  93953. + doepmsk.b.nyet = 1;
  93954. +
  93955. + if (core_if->dma_enable) {
  93956. + doepmsk.b.nak = 1;
  93957. + }
  93958. +*/
  93959. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepeachintmsk[0],
  93960. + doepmsk.d32);
  93961. +
  93962. + diepmsk.b.xfercompl = 1;
  93963. + diepmsk.b.timeout = 1;
  93964. + diepmsk.b.epdisabled = 1;
  93965. + diepmsk.b.ahberr = 1;
  93966. + diepmsk.b.intknepmis = 1;
  93967. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  93968. + diepmsk.b.intknepmis = 0;
  93969. +
  93970. +/* if (core_if->dma_desc_enable) {
  93971. + diepmsk.b.bna = 1;
  93972. + }
  93973. +*/
  93974. +/*
  93975. + if (core_if->dma_enable) {
  93976. + diepmsk.b.nak = 1;
  93977. + }
  93978. +*/
  93979. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepeachintmsk[0],
  93980. + diepmsk.d32);
  93981. + } else {
  93982. + daintmsk.b.inep0 = 1;
  93983. + daintmsk.b.outep0 = 1;
  93984. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk,
  93985. + daintmsk.d32);
  93986. +
  93987. + doepmsk.b.setup = 1;
  93988. + doepmsk.b.xfercompl = 1;
  93989. + doepmsk.b.ahberr = 1;
  93990. + doepmsk.b.epdisabled = 1;
  93991. +
  93992. + if ((core_if->dma_desc_enable) ||
  93993. + (core_if->dma_enable
  93994. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  93995. + doepmsk.b.stsphsercvd = 1;
  93996. + }
  93997. + if (core_if->dma_desc_enable)
  93998. + doepmsk.b.bna = 1;
  93999. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, doepmsk.d32);
  94000. +
  94001. + diepmsk.b.xfercompl = 1;
  94002. + diepmsk.b.timeout = 1;
  94003. + diepmsk.b.epdisabled = 1;
  94004. + diepmsk.b.ahberr = 1;
  94005. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  94006. + diepmsk.b.intknepmis = 0;
  94007. +/*
  94008. + if (core_if->dma_desc_enable) {
  94009. + diepmsk.b.bna = 1;
  94010. + }
  94011. +*/
  94012. +
  94013. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, diepmsk.d32);
  94014. + }
  94015. +
  94016. + /* Reset Device Address */
  94017. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  94018. + dcfg.b.devaddr = 0;
  94019. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  94020. +
  94021. + /* setup EP0 to receive SETUP packets */
  94022. + if (core_if->snpsid <= OTG_CORE_REV_2_94a)
  94023. + ep0_out_start(core_if, pcd);
  94024. +
  94025. + /* Clear interrupt */
  94026. + gintsts.d32 = 0;
  94027. + gintsts.b.usbreset = 1;
  94028. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  94029. +
  94030. + return 1;
  94031. +}
  94032. +
  94033. +/**
  94034. + * Get the device speed from the device status register and convert it
  94035. + * to USB speed constant.
  94036. + *
  94037. + * @param core_if Programming view of DWC_otg controller.
  94038. + */
  94039. +static int get_device_speed(dwc_otg_core_if_t * core_if)
  94040. +{
  94041. + dsts_data_t dsts;
  94042. + int speed = 0;
  94043. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  94044. +
  94045. + switch (dsts.b.enumspd) {
  94046. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  94047. + speed = USB_SPEED_HIGH;
  94048. + break;
  94049. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  94050. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  94051. + speed = USB_SPEED_FULL;
  94052. + break;
  94053. +
  94054. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  94055. + speed = USB_SPEED_LOW;
  94056. + break;
  94057. + }
  94058. +
  94059. + return speed;
  94060. +}
  94061. +
  94062. +/**
  94063. + * Read the device status register and set the device speed in the
  94064. + * data structure.
  94065. + * Set up EP0 to receive SETUP packets by calling dwc_ep0_activate.
  94066. + */
  94067. +int32_t dwc_otg_pcd_handle_enum_done_intr(dwc_otg_pcd_t * pcd)
  94068. +{
  94069. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  94070. + gintsts_data_t gintsts;
  94071. + gusbcfg_data_t gusbcfg;
  94072. + dwc_otg_core_global_regs_t *global_regs =
  94073. + GET_CORE_IF(pcd)->core_global_regs;
  94074. + uint8_t utmi16b, utmi8b;
  94075. + int speed;
  94076. + DWC_DEBUGPL(DBG_PCD, "SPEED ENUM\n");
  94077. +
  94078. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_2_60a) {
  94079. + utmi16b = 6; //vahrama old value was 6;
  94080. + utmi8b = 9;
  94081. + } else {
  94082. + utmi16b = 4;
  94083. + utmi8b = 8;
  94084. + }
  94085. + dwc_otg_ep0_activate(GET_CORE_IF(pcd), &ep0->dwc_ep);
  94086. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a) {
  94087. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  94088. + }
  94089. +
  94090. +#ifdef DEBUG_EP0
  94091. + print_ep0_state(pcd);
  94092. +#endif
  94093. +
  94094. + if (pcd->ep0state == EP0_DISCONNECT) {
  94095. + pcd->ep0state = EP0_IDLE;
  94096. + } else if (pcd->ep0state == EP0_STALL) {
  94097. + pcd->ep0state = EP0_IDLE;
  94098. + }
  94099. +
  94100. + pcd->ep0state = EP0_IDLE;
  94101. +
  94102. + ep0->stopped = 0;
  94103. +
  94104. + speed = get_device_speed(GET_CORE_IF(pcd));
  94105. + pcd->fops->connect(pcd, speed);
  94106. +
  94107. + /* Set USB turnaround time based on device speed and PHY interface. */
  94108. + gusbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  94109. + if (speed == USB_SPEED_HIGH) {
  94110. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  94111. + DWC_HWCFG2_HS_PHY_TYPE_ULPI) {
  94112. + /* ULPI interface */
  94113. + gusbcfg.b.usbtrdtim = 9;
  94114. + }
  94115. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  94116. + DWC_HWCFG2_HS_PHY_TYPE_UTMI) {
  94117. + /* UTMI+ interface */
  94118. + if (GET_CORE_IF(pcd)->hwcfg4.b.utmi_phy_data_width == 0) {
  94119. + gusbcfg.b.usbtrdtim = utmi8b;
  94120. + } else if (GET_CORE_IF(pcd)->hwcfg4.
  94121. + b.utmi_phy_data_width == 1) {
  94122. + gusbcfg.b.usbtrdtim = utmi16b;
  94123. + } else if (GET_CORE_IF(pcd)->
  94124. + core_params->phy_utmi_width == 8) {
  94125. + gusbcfg.b.usbtrdtim = utmi8b;
  94126. + } else {
  94127. + gusbcfg.b.usbtrdtim = utmi16b;
  94128. + }
  94129. + }
  94130. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  94131. + DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI) {
  94132. + /* UTMI+ OR ULPI interface */
  94133. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  94134. + /* ULPI interface */
  94135. + gusbcfg.b.usbtrdtim = 9;
  94136. + } else {
  94137. + /* UTMI+ interface */
  94138. + if (GET_CORE_IF(pcd)->
  94139. + core_params->phy_utmi_width == 16) {
  94140. + gusbcfg.b.usbtrdtim = utmi16b;
  94141. + } else {
  94142. + gusbcfg.b.usbtrdtim = utmi8b;
  94143. + }
  94144. + }
  94145. + }
  94146. + } else {
  94147. + /* Full or low speed */
  94148. + gusbcfg.b.usbtrdtim = 9;
  94149. + }
  94150. + DWC_WRITE_REG32(&global_regs->gusbcfg, gusbcfg.d32);
  94151. +
  94152. + /* Clear interrupt */
  94153. + gintsts.d32 = 0;
  94154. + gintsts.b.enumdone = 1;
  94155. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  94156. + gintsts.d32);
  94157. + return 1;
  94158. +}
  94159. +
  94160. +/**
  94161. + * This interrupt indicates that the ISO OUT Packet was dropped due to
  94162. + * Rx FIFO full or Rx Status Queue Full. If this interrupt occurs
  94163. + * read all the data from the Rx FIFO.
  94164. + */
  94165. +int32_t dwc_otg_pcd_handle_isoc_out_packet_dropped_intr(dwc_otg_pcd_t * pcd)
  94166. +{
  94167. + gintmsk_data_t intr_mask = {.d32 = 0 };
  94168. + gintsts_data_t gintsts;
  94169. +
  94170. + DWC_WARN("INTERRUPT Handler not implemented for %s\n",
  94171. + "ISOC Out Dropped");
  94172. +
  94173. + intr_mask.b.isooutdrop = 1;
  94174. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  94175. + intr_mask.d32, 0);
  94176. +
  94177. + /* Clear interrupt */
  94178. + gintsts.d32 = 0;
  94179. + gintsts.b.isooutdrop = 1;
  94180. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  94181. + gintsts.d32);
  94182. +
  94183. + return 1;
  94184. +}
  94185. +
  94186. +/**
  94187. + * This interrupt indicates the end of the portion of the micro-frame
  94188. + * for periodic transactions. If there is a periodic transaction for
  94189. + * the next frame, load the packets into the EP periodic Tx FIFO.
  94190. + */
  94191. +int32_t dwc_otg_pcd_handle_end_periodic_frame_intr(dwc_otg_pcd_t * pcd)
  94192. +{
  94193. + gintmsk_data_t intr_mask = {.d32 = 0 };
  94194. + gintsts_data_t gintsts;
  94195. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "EOP");
  94196. +
  94197. + intr_mask.b.eopframe = 1;
  94198. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  94199. + intr_mask.d32, 0);
  94200. +
  94201. + /* Clear interrupt */
  94202. + gintsts.d32 = 0;
  94203. + gintsts.b.eopframe = 1;
  94204. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  94205. + gintsts.d32);
  94206. +
  94207. + return 1;
  94208. +}
  94209. +
  94210. +/**
  94211. + * This interrupt indicates that EP of the packet on the top of the
  94212. + * non-periodic Tx FIFO does not match EP of the IN Token received.
  94213. + *
  94214. + * The "Device IN Token Queue" Registers are read to determine the
  94215. + * order the IN Tokens have been received. The non-periodic Tx FIFO
  94216. + * is flushed, so it can be reloaded in the order seen in the IN Token
  94217. + * Queue.
  94218. + */
  94219. +int32_t dwc_otg_pcd_handle_ep_mismatch_intr(dwc_otg_pcd_t * pcd)
  94220. +{
  94221. + gintsts_data_t gintsts;
  94222. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  94223. + dctl_data_t dctl;
  94224. + gintmsk_data_t intr_mask = {.d32 = 0 };
  94225. +
  94226. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  94227. + core_if->start_predict = 1;
  94228. +
  94229. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  94230. +
  94231. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  94232. + if (!gintsts.b.ginnakeff) {
  94233. + /* Disable EP Mismatch interrupt */
  94234. + intr_mask.d32 = 0;
  94235. + intr_mask.b.epmismatch = 1;
  94236. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
  94237. + /* Enable the Global IN NAK Effective Interrupt */
  94238. + intr_mask.d32 = 0;
  94239. + intr_mask.b.ginnakeff = 1;
  94240. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  94241. + /* Set the global non-periodic IN NAK handshake */
  94242. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  94243. + dctl.b.sgnpinnak = 1;
  94244. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  94245. + } else {
  94246. + DWC_PRINTF("gintsts.b.ginnakeff = 1! dctl.b.sgnpinnak not set\n");
  94247. + }
  94248. + /* Disabling of all EP's will be done in dwc_otg_pcd_handle_in_nak_effective()
  94249. + * handler after Global IN NAK Effective interrupt will be asserted */
  94250. + }
  94251. + /* Clear interrupt */
  94252. + gintsts.d32 = 0;
  94253. + gintsts.b.epmismatch = 1;
  94254. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  94255. +
  94256. + return 1;
  94257. +}
  94258. +
  94259. +/**
  94260. + * This interrupt is valid only in DMA mode. This interrupt indicates that the
  94261. + * core has stopped fetching data for IN endpoints due to the unavailability of
  94262. + * TxFIFO space or Request Queue space. This interrupt is used by the
  94263. + * application for an endpoint mismatch algorithm.
  94264. + *
  94265. + * @param pcd The PCD
  94266. + */
  94267. +int32_t dwc_otg_pcd_handle_ep_fetsusp_intr(dwc_otg_pcd_t * pcd)
  94268. +{
  94269. + gintsts_data_t gintsts;
  94270. + gintmsk_data_t gintmsk_data;
  94271. + dctl_data_t dctl;
  94272. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  94273. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  94274. +
  94275. + /* Clear the global non-periodic IN NAK handshake */
  94276. + dctl.d32 = 0;
  94277. + dctl.b.cgnpinnak = 1;
  94278. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  94279. +
  94280. + /* Mask GINTSTS.FETSUSP interrupt */
  94281. + gintmsk_data.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  94282. + gintmsk_data.b.fetsusp = 0;
  94283. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_data.d32);
  94284. +
  94285. + /* Clear interrupt */
  94286. + gintsts.d32 = 0;
  94287. + gintsts.b.fetsusp = 1;
  94288. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  94289. +
  94290. + return 1;
  94291. +}
  94292. +/**
  94293. + * This funcion stalls EP0.
  94294. + */
  94295. +static inline void ep0_do_stall(dwc_otg_pcd_t * pcd, const int err_val)
  94296. +{
  94297. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  94298. + usb_device_request_t *ctrl = &pcd->setup_pkt->req;
  94299. + DWC_WARN("req %02x.%02x protocol STALL; err %d\n",
  94300. + ctrl->bmRequestType, ctrl->bRequest, err_val);
  94301. +
  94302. + ep0->dwc_ep.is_in = 1;
  94303. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep0->dwc_ep);
  94304. + pcd->ep0.stopped = 1;
  94305. + pcd->ep0state = EP0_IDLE;
  94306. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  94307. +}
  94308. +
  94309. +/**
  94310. + * This functions delegates the setup command to the gadget driver.
  94311. + */
  94312. +static inline void do_gadget_setup(dwc_otg_pcd_t * pcd,
  94313. + usb_device_request_t * ctrl)
  94314. +{
  94315. + int ret = 0;
  94316. + DWC_SPINUNLOCK(pcd->lock);
  94317. + ret = pcd->fops->setup(pcd, (uint8_t *) ctrl);
  94318. + DWC_SPINLOCK(pcd->lock);
  94319. + if (ret < 0) {
  94320. + ep0_do_stall(pcd, ret);
  94321. + }
  94322. +
  94323. + /** @todo This is a g_file_storage gadget driver specific
  94324. + * workaround: a DELAYED_STATUS result from the fsg_setup
  94325. + * routine will result in the gadget queueing a EP0 IN status
  94326. + * phase for a two-stage control transfer. Exactly the same as
  94327. + * a SET_CONFIGURATION/SET_INTERFACE except that this is a class
  94328. + * specific request. Need a generic way to know when the gadget
  94329. + * driver will queue the status phase. Can we assume when we
  94330. + * call the gadget driver setup() function that it will always
  94331. + * queue and require the following flag? Need to look into
  94332. + * this.
  94333. + */
  94334. +
  94335. + if (ret == 256 + 999) {
  94336. + pcd->request_config = 1;
  94337. + }
  94338. +}
  94339. +
  94340. +#ifdef DWC_UTE_CFI
  94341. +/**
  94342. + * This functions delegates the CFI setup commands to the gadget driver.
  94343. + * This function will return a negative value to indicate a failure.
  94344. + */
  94345. +static inline int cfi_gadget_setup(dwc_otg_pcd_t * pcd,
  94346. + struct cfi_usb_ctrlrequest *ctrl_req)
  94347. +{
  94348. + int ret = 0;
  94349. +
  94350. + if (pcd->fops && pcd->fops->cfi_setup) {
  94351. + DWC_SPINUNLOCK(pcd->lock);
  94352. + ret = pcd->fops->cfi_setup(pcd, ctrl_req);
  94353. + DWC_SPINLOCK(pcd->lock);
  94354. + if (ret < 0) {
  94355. + ep0_do_stall(pcd, ret);
  94356. + return ret;
  94357. + }
  94358. + }
  94359. +
  94360. + return ret;
  94361. +}
  94362. +#endif
  94363. +
  94364. +/**
  94365. + * This function starts the Zero-Length Packet for the IN status phase
  94366. + * of a 2 stage control transfer.
  94367. + */
  94368. +static inline void do_setup_in_status_phase(dwc_otg_pcd_t * pcd)
  94369. +{
  94370. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  94371. + if (pcd->ep0state == EP0_STALL) {
  94372. + return;
  94373. + }
  94374. +
  94375. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  94376. +
  94377. + /* Prepare for more SETUP Packets */
  94378. + DWC_DEBUGPL(DBG_PCD, "EP0 IN ZLP\n");
  94379. + if ((GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a)
  94380. + && (pcd->core_if->dma_desc_enable)
  94381. + && (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len)) {
  94382. + DWC_DEBUGPL(DBG_PCDV,
  94383. + "Data terminated wait next packet in out_desc_addr\n");
  94384. + pcd->backup_buf = phys_to_virt(ep0->dwc_ep.dma_addr);
  94385. + pcd->data_terminated = 1;
  94386. + }
  94387. + ep0->dwc_ep.xfer_len = 0;
  94388. + ep0->dwc_ep.xfer_count = 0;
  94389. + ep0->dwc_ep.is_in = 1;
  94390. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  94391. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  94392. +
  94393. + /* Prepare for more SETUP Packets */
  94394. + //ep0_out_start(GET_CORE_IF(pcd), pcd);
  94395. +}
  94396. +
  94397. +/**
  94398. + * This function starts the Zero-Length Packet for the OUT status phase
  94399. + * of a 2 stage control transfer.
  94400. + */
  94401. +static inline void do_setup_out_status_phase(dwc_otg_pcd_t * pcd)
  94402. +{
  94403. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  94404. + if (pcd->ep0state == EP0_STALL) {
  94405. + DWC_DEBUGPL(DBG_PCD, "EP0 STALLED\n");
  94406. + return;
  94407. + }
  94408. + pcd->ep0state = EP0_OUT_STATUS_PHASE;
  94409. +
  94410. + DWC_DEBUGPL(DBG_PCD, "EP0 OUT ZLP\n");
  94411. + ep0->dwc_ep.xfer_len = 0;
  94412. + ep0->dwc_ep.xfer_count = 0;
  94413. + ep0->dwc_ep.is_in = 0;
  94414. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  94415. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  94416. +
  94417. + /* Prepare for more SETUP Packets */
  94418. + if (GET_CORE_IF(pcd)->dma_enable == 0) {
  94419. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  94420. + }
  94421. +}
  94422. +
  94423. +/**
  94424. + * Clear the EP halt (STALL) and if pending requests start the
  94425. + * transfer.
  94426. + */
  94427. +static inline void pcd_clear_halt(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  94428. +{
  94429. + if (ep->dwc_ep.stall_clear_flag == 0)
  94430. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  94431. +
  94432. + /* Reactive the EP */
  94433. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  94434. + if (ep->stopped) {
  94435. + ep->stopped = 0;
  94436. + /* If there is a request in the EP queue start it */
  94437. +
  94438. + /** @todo FIXME: this causes an EP mismatch in DMA mode.
  94439. + * epmismatch not yet implemented. */
  94440. +
  94441. + /*
  94442. + * Above fixme is solved by implmenting a tasklet to call the
  94443. + * start_next_request(), outside of interrupt context at some
  94444. + * time after the current time, after a clear-halt setup packet.
  94445. + * Still need to implement ep mismatch in the future if a gadget
  94446. + * ever uses more than one endpoint at once
  94447. + */
  94448. + ep->queue_sof = 1;
  94449. + DWC_TASK_SCHEDULE(pcd->start_xfer_tasklet);
  94450. + }
  94451. + /* Start Control Status Phase */
  94452. + do_setup_in_status_phase(pcd);
  94453. +}
  94454. +
  94455. +/**
  94456. + * This function is called when the SET_FEATURE TEST_MODE Setup packet
  94457. + * is sent from the host. The Device Control register is written with
  94458. + * the Test Mode bits set to the specified Test Mode. This is done as
  94459. + * a tasklet so that the "Status" phase of the control transfer
  94460. + * completes before transmitting the TEST packets.
  94461. + *
  94462. + * @todo This has not been tested since the tasklet struct was put
  94463. + * into the PCD struct!
  94464. + *
  94465. + */
  94466. +void do_test_mode(void *data)
  94467. +{
  94468. + dctl_data_t dctl;
  94469. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  94470. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  94471. + int test_mode = pcd->test_mode;
  94472. +
  94473. +// DWC_WARN("%s() has not been tested since being rewritten!\n", __func__);
  94474. +
  94475. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  94476. + switch (test_mode) {
  94477. + case 1: // TEST_J
  94478. + dctl.b.tstctl = 1;
  94479. + break;
  94480. +
  94481. + case 2: // TEST_K
  94482. + dctl.b.tstctl = 2;
  94483. + break;
  94484. +
  94485. + case 3: // TEST_SE0_NAK
  94486. + dctl.b.tstctl = 3;
  94487. + break;
  94488. +
  94489. + case 4: // TEST_PACKET
  94490. + dctl.b.tstctl = 4;
  94491. + break;
  94492. +
  94493. + case 5: // TEST_FORCE_ENABLE
  94494. + dctl.b.tstctl = 5;
  94495. + break;
  94496. + }
  94497. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  94498. +}
  94499. +
  94500. +/**
  94501. + * This function process the GET_STATUS Setup Commands.
  94502. + */
  94503. +static inline void do_get_status(dwc_otg_pcd_t * pcd)
  94504. +{
  94505. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  94506. + dwc_otg_pcd_ep_t *ep;
  94507. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  94508. + uint16_t *status = pcd->status_buf;
  94509. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  94510. +
  94511. +#ifdef DEBUG_EP0
  94512. + DWC_DEBUGPL(DBG_PCD,
  94513. + "GET_STATUS %02x.%02x v%04x i%04x l%04x\n",
  94514. + ctrl.bmRequestType, ctrl.bRequest,
  94515. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  94516. + UGETW(ctrl.wLength));
  94517. +#endif
  94518. +
  94519. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  94520. + case UT_DEVICE:
  94521. + if(UGETW(ctrl.wIndex) == 0xF000) { /* OTG Status selector */
  94522. + DWC_PRINTF("wIndex - %d\n", UGETW(ctrl.wIndex));
  94523. + DWC_PRINTF("OTG VERSION - %d\n", core_if->otg_ver);
  94524. + DWC_PRINTF("OTG CAP - %d, %d\n",
  94525. + core_if->core_params->otg_cap,
  94526. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  94527. + if (core_if->otg_ver == 1
  94528. + && core_if->core_params->otg_cap ==
  94529. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  94530. + uint8_t *otgsts = (uint8_t*)pcd->status_buf;
  94531. + *otgsts = (core_if->otg_sts & 0x1);
  94532. + pcd->ep0_pending = 1;
  94533. + ep0->dwc_ep.start_xfer_buff =
  94534. + (uint8_t *) otgsts;
  94535. + ep0->dwc_ep.xfer_buff = (uint8_t *) otgsts;
  94536. + ep0->dwc_ep.dma_addr =
  94537. + pcd->status_buf_dma_handle;
  94538. + ep0->dwc_ep.xfer_len = 1;
  94539. + ep0->dwc_ep.xfer_count = 0;
  94540. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  94541. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  94542. + &ep0->dwc_ep);
  94543. + return;
  94544. + } else {
  94545. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  94546. + return;
  94547. + }
  94548. + break;
  94549. + } else {
  94550. + *status = 0x1; /* Self powered */
  94551. + *status |= pcd->remote_wakeup_enable << 1;
  94552. + break;
  94553. + }
  94554. + case UT_INTERFACE:
  94555. + *status = 0;
  94556. + break;
  94557. +
  94558. + case UT_ENDPOINT:
  94559. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  94560. + if (ep == 0 || UGETW(ctrl.wLength) > 2) {
  94561. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  94562. + return;
  94563. + }
  94564. + /** @todo check for EP stall */
  94565. + *status = ep->stopped;
  94566. + break;
  94567. + }
  94568. + pcd->ep0_pending = 1;
  94569. + ep0->dwc_ep.start_xfer_buff = (uint8_t *) status;
  94570. + ep0->dwc_ep.xfer_buff = (uint8_t *) status;
  94571. + ep0->dwc_ep.dma_addr = pcd->status_buf_dma_handle;
  94572. + ep0->dwc_ep.xfer_len = 2;
  94573. + ep0->dwc_ep.xfer_count = 0;
  94574. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  94575. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  94576. +}
  94577. +
  94578. +/**
  94579. + * This function process the SET_FEATURE Setup Commands.
  94580. + */
  94581. +static inline void do_set_feature(dwc_otg_pcd_t * pcd)
  94582. +{
  94583. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  94584. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  94585. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  94586. + dwc_otg_pcd_ep_t *ep = 0;
  94587. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  94588. + gotgctl_data_t gotgctl = {.d32 = 0 };
  94589. +
  94590. + DWC_DEBUGPL(DBG_PCD, "SET_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  94591. + ctrl.bmRequestType, ctrl.bRequest,
  94592. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  94593. + UGETW(ctrl.wLength));
  94594. + DWC_DEBUGPL(DBG_PCD, "otg_cap=%d\n", otg_cap_param);
  94595. +
  94596. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  94597. + case UT_DEVICE:
  94598. + switch (UGETW(ctrl.wValue)) {
  94599. + case UF_DEVICE_REMOTE_WAKEUP:
  94600. + pcd->remote_wakeup_enable = 1;
  94601. + break;
  94602. +
  94603. + case UF_TEST_MODE:
  94604. + /* Setup the Test Mode tasklet to do the Test
  94605. + * Packet generation after the SETUP Status
  94606. + * phase has completed. */
  94607. +
  94608. + /** @todo This has not been tested since the
  94609. + * tasklet struct was put into the PCD
  94610. + * struct! */
  94611. + pcd->test_mode = UGETW(ctrl.wIndex) >> 8;
  94612. + DWC_TASK_SCHEDULE(pcd->test_mode_tasklet);
  94613. + break;
  94614. +
  94615. + case UF_DEVICE_B_HNP_ENABLE:
  94616. + DWC_DEBUGPL(DBG_PCDV,
  94617. + "SET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
  94618. +
  94619. + /* dev may initiate HNP */
  94620. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  94621. + pcd->b_hnp_enable = 1;
  94622. + dwc_otg_pcd_update_otg(pcd, 0);
  94623. + DWC_DEBUGPL(DBG_PCD, "Request B HNP\n");
  94624. + /**@todo Is the gotgctl.devhnpen cleared
  94625. + * by a USB Reset? */
  94626. + gotgctl.b.devhnpen = 1;
  94627. + gotgctl.b.hnpreq = 1;
  94628. + DWC_WRITE_REG32(&global_regs->gotgctl,
  94629. + gotgctl.d32);
  94630. + } else {
  94631. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  94632. + return;
  94633. + }
  94634. + break;
  94635. +
  94636. + case UF_DEVICE_A_HNP_SUPPORT:
  94637. + /* RH port supports HNP */
  94638. + DWC_DEBUGPL(DBG_PCDV,
  94639. + "SET_FEATURE: USB_DEVICE_A_HNP_SUPPORT\n");
  94640. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  94641. + pcd->a_hnp_support = 1;
  94642. + dwc_otg_pcd_update_otg(pcd, 0);
  94643. + } else {
  94644. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  94645. + return;
  94646. + }
  94647. + break;
  94648. +
  94649. + case UF_DEVICE_A_ALT_HNP_SUPPORT:
  94650. + /* other RH port does */
  94651. + DWC_DEBUGPL(DBG_PCDV,
  94652. + "SET_FEATURE: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
  94653. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  94654. + pcd->a_alt_hnp_support = 1;
  94655. + dwc_otg_pcd_update_otg(pcd, 0);
  94656. + } else {
  94657. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  94658. + return;
  94659. + }
  94660. + break;
  94661. +
  94662. + default:
  94663. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  94664. + return;
  94665. +
  94666. + }
  94667. + do_setup_in_status_phase(pcd);
  94668. + break;
  94669. +
  94670. + case UT_INTERFACE:
  94671. + do_gadget_setup(pcd, &ctrl);
  94672. + break;
  94673. +
  94674. + case UT_ENDPOINT:
  94675. + if (UGETW(ctrl.wValue) == UF_ENDPOINT_HALT) {
  94676. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  94677. + if (ep == 0) {
  94678. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  94679. + return;
  94680. + }
  94681. + ep->stopped = 1;
  94682. + dwc_otg_ep_set_stall(core_if, &ep->dwc_ep);
  94683. + }
  94684. + do_setup_in_status_phase(pcd);
  94685. + break;
  94686. + }
  94687. +}
  94688. +
  94689. +/**
  94690. + * This function process the CLEAR_FEATURE Setup Commands.
  94691. + */
  94692. +static inline void do_clear_feature(dwc_otg_pcd_t * pcd)
  94693. +{
  94694. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  94695. + dwc_otg_pcd_ep_t *ep = 0;
  94696. +
  94697. + DWC_DEBUGPL(DBG_PCD,
  94698. + "CLEAR_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  94699. + ctrl.bmRequestType, ctrl.bRequest,
  94700. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  94701. + UGETW(ctrl.wLength));
  94702. +
  94703. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  94704. + case UT_DEVICE:
  94705. + switch (UGETW(ctrl.wValue)) {
  94706. + case UF_DEVICE_REMOTE_WAKEUP:
  94707. + pcd->remote_wakeup_enable = 0;
  94708. + break;
  94709. +
  94710. + case UF_TEST_MODE:
  94711. + /** @todo Add CLEAR_FEATURE for TEST modes. */
  94712. + break;
  94713. +
  94714. + default:
  94715. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  94716. + return;
  94717. + }
  94718. + do_setup_in_status_phase(pcd);
  94719. + break;
  94720. +
  94721. + case UT_ENDPOINT:
  94722. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  94723. + if (ep == 0) {
  94724. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  94725. + return;
  94726. + }
  94727. +
  94728. + pcd_clear_halt(pcd, ep);
  94729. +
  94730. + break;
  94731. + }
  94732. +}
  94733. +
  94734. +/**
  94735. + * This function process the SET_ADDRESS Setup Commands.
  94736. + */
  94737. +static inline void do_set_address(dwc_otg_pcd_t * pcd)
  94738. +{
  94739. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  94740. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  94741. +
  94742. + if (ctrl.bmRequestType == UT_DEVICE) {
  94743. + dcfg_data_t dcfg = {.d32 = 0 };
  94744. +
  94745. +#ifdef DEBUG_EP0
  94746. +// DWC_DEBUGPL(DBG_PCDV, "SET_ADDRESS:%d\n", ctrl.wValue);
  94747. +#endif
  94748. + dcfg.b.devaddr = UGETW(ctrl.wValue);
  94749. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dcfg, 0, dcfg.d32);
  94750. + do_setup_in_status_phase(pcd);
  94751. + }
  94752. +}
  94753. +
  94754. +/**
  94755. + * This function processes SETUP commands. In Linux, the USB Command
  94756. + * processing is done in two places - the first being the PCD and the
  94757. + * second in the Gadget Driver (for example, the File-Backed Storage
  94758. + * Gadget Driver).
  94759. + *
  94760. + * <table>
  94761. + * <tr><td>Command </td><td>Driver </td><td>Description</td></tr>
  94762. + *
  94763. + * <tr><td>GET_STATUS </td><td>PCD </td><td>Command is processed as
  94764. + * defined in chapter 9 of the USB 2.0 Specification chapter 9
  94765. + * </td></tr>
  94766. + *
  94767. + * <tr><td>CLEAR_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  94768. + * requests are the ENDPOINT_HALT feature is procesed, all others the
  94769. + * interface requests are ignored.</td></tr>
  94770. + *
  94771. + * <tr><td>SET_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  94772. + * requests are processed by the PCD. Interface requests are passed
  94773. + * to the Gadget Driver.</td></tr>
  94774. + *
  94775. + * <tr><td>SET_ADDRESS </td><td>PCD </td><td>Program the DCFG reg,
  94776. + * with device address received </td></tr>
  94777. + *
  94778. + * <tr><td>GET_DESCRIPTOR </td><td>Gadget Driver </td><td>Return the
  94779. + * requested descriptor</td></tr>
  94780. + *
  94781. + * <tr><td>SET_DESCRIPTOR </td><td>Gadget Driver </td><td>Optional -
  94782. + * not implemented by any of the existing Gadget Drivers.</td></tr>
  94783. + *
  94784. + * <tr><td>SET_CONFIGURATION </td><td>Gadget Driver </td><td>Disable
  94785. + * all EPs and enable EPs for new configuration.</td></tr>
  94786. + *
  94787. + * <tr><td>GET_CONFIGURATION </td><td>Gadget Driver </td><td>Return
  94788. + * the current configuration</td></tr>
  94789. + *
  94790. + * <tr><td>SET_INTERFACE </td><td>Gadget Driver </td><td>Disable all
  94791. + * EPs and enable EPs for new configuration.</td></tr>
  94792. + *
  94793. + * <tr><td>GET_INTERFACE </td><td>Gadget Driver </td><td>Return the
  94794. + * current interface.</td></tr>
  94795. + *
  94796. + * <tr><td>SYNC_FRAME </td><td>PCD </td><td>Display debug
  94797. + * message.</td></tr>
  94798. + * </table>
  94799. + *
  94800. + * When the SETUP Phase Done interrupt occurs, the PCD SETUP commands are
  94801. + * processed by pcd_setup. Calling the Function Driver's setup function from
  94802. + * pcd_setup processes the gadget SETUP commands.
  94803. + */
  94804. +static inline void pcd_setup(dwc_otg_pcd_t * pcd)
  94805. +{
  94806. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  94807. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  94808. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  94809. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  94810. +
  94811. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  94812. +
  94813. +#ifdef DWC_UTE_CFI
  94814. + int retval = 0;
  94815. + struct cfi_usb_ctrlrequest cfi_req;
  94816. +#endif
  94817. +
  94818. + doeptsize0.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doeptsiz);
  94819. +
  94820. + /** In BDMA more then 1 setup packet is not supported till 3.00a */
  94821. + if (core_if->dma_enable && core_if->dma_desc_enable == 0
  94822. + && (doeptsize0.b.supcnt < 2)
  94823. + && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  94824. + DWC_ERROR
  94825. + ("\n\n----------- CANNOT handle > 1 setup packet in DMA mode\n\n");
  94826. + }
  94827. + if ((core_if->snpsid >= OTG_CORE_REV_3_00a)
  94828. + && (core_if->dma_enable == 1) && (core_if->dma_desc_enable == 0)) {
  94829. + ctrl =
  94830. + (pcd->setup_pkt +
  94831. + (3 - doeptsize0.b.supcnt - 1 +
  94832. + ep0->dwc_ep.stp_rollover))->req;
  94833. + }
  94834. +#ifdef DEBUG_EP0
  94835. + DWC_DEBUGPL(DBG_PCD, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  94836. + ctrl.bmRequestType, ctrl.bRequest,
  94837. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  94838. + UGETW(ctrl.wLength));
  94839. +#endif
  94840. +
  94841. + /* Clean up the request queue */
  94842. + dwc_otg_request_nuke(ep0);
  94843. + ep0->stopped = 0;
  94844. +
  94845. + if (ctrl.bmRequestType & UE_DIR_IN) {
  94846. + ep0->dwc_ep.is_in = 1;
  94847. + pcd->ep0state = EP0_IN_DATA_PHASE;
  94848. + } else {
  94849. + ep0->dwc_ep.is_in = 0;
  94850. + pcd->ep0state = EP0_OUT_DATA_PHASE;
  94851. + }
  94852. +
  94853. + if (UGETW(ctrl.wLength) == 0) {
  94854. + ep0->dwc_ep.is_in = 1;
  94855. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  94856. + }
  94857. +
  94858. + if (UT_GET_TYPE(ctrl.bmRequestType) != UT_STANDARD) {
  94859. +
  94860. +#ifdef DWC_UTE_CFI
  94861. + DWC_MEMCPY(&cfi_req, &ctrl, sizeof(usb_device_request_t));
  94862. +
  94863. + //printk(KERN_ALERT "CFI: req_type=0x%02x; req=0x%02x\n",
  94864. + ctrl.bRequestType, ctrl.bRequest);
  94865. + if (UT_GET_TYPE(cfi_req.bRequestType) == UT_VENDOR) {
  94866. + if (cfi_req.bRequest > 0xB0 && cfi_req.bRequest < 0xBF) {
  94867. + retval = cfi_setup(pcd, &cfi_req);
  94868. + if (retval < 0) {
  94869. + ep0_do_stall(pcd, retval);
  94870. + pcd->ep0_pending = 0;
  94871. + return;
  94872. + }
  94873. +
  94874. + /* if need gadget setup then call it and check the retval */
  94875. + if (pcd->cfi->need_gadget_att) {
  94876. + retval =
  94877. + cfi_gadget_setup(pcd,
  94878. + &pcd->
  94879. + cfi->ctrl_req);
  94880. + if (retval < 0) {
  94881. + pcd->ep0_pending = 0;
  94882. + return;
  94883. + }
  94884. + }
  94885. +
  94886. + if (pcd->cfi->need_status_in_complete) {
  94887. + do_setup_in_status_phase(pcd);
  94888. + }
  94889. + return;
  94890. + }
  94891. + }
  94892. +#endif
  94893. +
  94894. + /* handle non-standard (class/vendor) requests in the gadget driver */
  94895. + do_gadget_setup(pcd, &ctrl);
  94896. + return;
  94897. + }
  94898. +
  94899. + /** @todo NGS: Handle bad setup packet? */
  94900. +
  94901. +///////////////////////////////////////////
  94902. +//// --- Standard Request handling --- ////
  94903. +
  94904. + switch (ctrl.bRequest) {
  94905. + case UR_GET_STATUS:
  94906. + do_get_status(pcd);
  94907. + break;
  94908. +
  94909. + case UR_CLEAR_FEATURE:
  94910. + do_clear_feature(pcd);
  94911. + break;
  94912. +
  94913. + case UR_SET_FEATURE:
  94914. + do_set_feature(pcd);
  94915. + break;
  94916. +
  94917. + case UR_SET_ADDRESS:
  94918. + do_set_address(pcd);
  94919. + break;
  94920. +
  94921. + case UR_SET_INTERFACE:
  94922. + case UR_SET_CONFIG:
  94923. +// _pcd->request_config = 1; /* Configuration changed */
  94924. + do_gadget_setup(pcd, &ctrl);
  94925. + break;
  94926. +
  94927. + case UR_SYNCH_FRAME:
  94928. + do_gadget_setup(pcd, &ctrl);
  94929. + break;
  94930. +
  94931. + default:
  94932. + /* Call the Gadget Driver's setup functions */
  94933. + do_gadget_setup(pcd, &ctrl);
  94934. + break;
  94935. + }
  94936. +}
  94937. +
  94938. +/**
  94939. + * This function completes the ep0 control transfer.
  94940. + */
  94941. +static int32_t ep0_complete_request(dwc_otg_pcd_ep_t * ep)
  94942. +{
  94943. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  94944. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  94945. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  94946. + dev_if->in_ep_regs[ep->dwc_ep.num];
  94947. +#ifdef DEBUG_EP0
  94948. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  94949. + dev_if->out_ep_regs[ep->dwc_ep.num];
  94950. +#endif
  94951. + deptsiz0_data_t deptsiz;
  94952. + dev_dma_desc_sts_t desc_sts;
  94953. + dwc_otg_pcd_request_t *req;
  94954. + int is_last = 0;
  94955. + dwc_otg_pcd_t *pcd = ep->pcd;
  94956. +
  94957. +#ifdef DWC_UTE_CFI
  94958. + struct cfi_usb_ctrlrequest *ctrlreq;
  94959. + int retval = -DWC_E_NOT_SUPPORTED;
  94960. +#endif
  94961. +
  94962. + desc_sts.b.bytes = 0;
  94963. +
  94964. + if (pcd->ep0_pending && DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  94965. + if (ep->dwc_ep.is_in) {
  94966. +#ifdef DEBUG_EP0
  94967. + DWC_DEBUGPL(DBG_PCDV, "Do setup OUT status phase\n");
  94968. +#endif
  94969. + do_setup_out_status_phase(pcd);
  94970. + } else {
  94971. +#ifdef DEBUG_EP0
  94972. + DWC_DEBUGPL(DBG_PCDV, "Do setup IN status phase\n");
  94973. +#endif
  94974. +
  94975. +#ifdef DWC_UTE_CFI
  94976. + ctrlreq = &pcd->cfi->ctrl_req;
  94977. +
  94978. + if (UT_GET_TYPE(ctrlreq->bRequestType) == UT_VENDOR) {
  94979. + if (ctrlreq->bRequest > 0xB0
  94980. + && ctrlreq->bRequest < 0xBF) {
  94981. +
  94982. + /* Return if the PCD failed to handle the request */
  94983. + if ((retval =
  94984. + pcd->cfi->ops.
  94985. + ctrl_write_complete(pcd->cfi,
  94986. + pcd)) < 0) {
  94987. + CFI_INFO
  94988. + ("ERROR setting a new value in the PCD(%d)\n",
  94989. + retval);
  94990. + ep0_do_stall(pcd, retval);
  94991. + pcd->ep0_pending = 0;
  94992. + return 0;
  94993. + }
  94994. +
  94995. + /* If the gadget needs to be notified on the request */
  94996. + if (pcd->cfi->need_gadget_att == 1) {
  94997. + //retval = do_gadget_setup(pcd, &pcd->cfi->ctrl_req);
  94998. + retval =
  94999. + cfi_gadget_setup(pcd,
  95000. + &pcd->cfi->
  95001. + ctrl_req);
  95002. +
  95003. + /* Return from the function if the gadget failed to process
  95004. + * the request properly - this should never happen !!!
  95005. + */
  95006. + if (retval < 0) {
  95007. + CFI_INFO
  95008. + ("ERROR setting a new value in the gadget(%d)\n",
  95009. + retval);
  95010. + pcd->ep0_pending = 0;
  95011. + return 0;
  95012. + }
  95013. + }
  95014. +
  95015. + CFI_INFO("%s: RETVAL=%d\n", __func__,
  95016. + retval);
  95017. + /* If we hit here then the PCD and the gadget has properly
  95018. + * handled the request - so send the ZLP IN to the host.
  95019. + */
  95020. + /* @todo: MAS - decide whether we need to start the setup
  95021. + * stage based on the need_setup value of the cfi object
  95022. + */
  95023. + do_setup_in_status_phase(pcd);
  95024. + pcd->ep0_pending = 0;
  95025. + return 1;
  95026. + }
  95027. + }
  95028. +#endif
  95029. +
  95030. + do_setup_in_status_phase(pcd);
  95031. + }
  95032. + pcd->ep0_pending = 0;
  95033. + return 1;
  95034. + }
  95035. +
  95036. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  95037. + return 0;
  95038. + }
  95039. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  95040. +
  95041. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE
  95042. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  95043. + is_last = 1;
  95044. + } else if (ep->dwc_ep.is_in) {
  95045. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  95046. + if (core_if->dma_desc_enable != 0)
  95047. + desc_sts = dev_if->in_desc_addr->status;
  95048. +#ifdef DEBUG_EP0
  95049. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xfersize=%d pktcnt=%d\n",
  95050. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  95051. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  95052. +#endif
  95053. +
  95054. + if (((core_if->dma_desc_enable == 0)
  95055. + && (deptsiz.b.xfersize == 0))
  95056. + || ((core_if->dma_desc_enable != 0)
  95057. + && (desc_sts.b.bytes == 0))) {
  95058. + req->actual = ep->dwc_ep.xfer_count;
  95059. + /* Is a Zero Len Packet needed? */
  95060. + if (req->sent_zlp) {
  95061. +#ifdef DEBUG_EP0
  95062. + DWC_DEBUGPL(DBG_PCD, "Setup Rx ZLP\n");
  95063. +#endif
  95064. + req->sent_zlp = 0;
  95065. + }
  95066. + do_setup_out_status_phase(pcd);
  95067. + }
  95068. + } else {
  95069. + /* ep0-OUT */
  95070. +#ifdef DEBUG_EP0
  95071. + deptsiz.d32 = DWC_READ_REG32(&out_ep_regs->doeptsiz);
  95072. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xsize=%d pktcnt=%d\n",
  95073. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  95074. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  95075. +#endif
  95076. + req->actual = ep->dwc_ep.xfer_count;
  95077. +
  95078. + /* Is a Zero Len Packet needed? */
  95079. + if (req->sent_zlp) {
  95080. +#ifdef DEBUG_EP0
  95081. + DWC_DEBUGPL(DBG_PCDV, "Setup Tx ZLP\n");
  95082. +#endif
  95083. + req->sent_zlp = 0;
  95084. + }
  95085. + /* For older cores do setup in status phase in Slave/BDMA modes,
  95086. + * starting from 3.00 do that only in slave, and for DMA modes
  95087. + * just re-enable ep 0 OUT here*/
  95088. + if (core_if->dma_enable == 0
  95089. + || (core_if->dma_desc_enable == 0
  95090. + && core_if->snpsid <= OTG_CORE_REV_2_94a)) {
  95091. + do_setup_in_status_phase(pcd);
  95092. + } else if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  95093. + DWC_DEBUGPL(DBG_PCDV,
  95094. + "Enable out ep before in status phase\n");
  95095. + ep0_out_start(core_if, pcd);
  95096. + }
  95097. + }
  95098. +
  95099. + /* Complete the request */
  95100. + if (is_last) {
  95101. + dwc_otg_request_done(ep, req, 0);
  95102. + ep->dwc_ep.start_xfer_buff = 0;
  95103. + ep->dwc_ep.xfer_buff = 0;
  95104. + ep->dwc_ep.xfer_len = 0;
  95105. + return 1;
  95106. + }
  95107. + return 0;
  95108. +}
  95109. +
  95110. +#ifdef DWC_UTE_CFI
  95111. +/**
  95112. + * This function calculates traverses all the CFI DMA descriptors and
  95113. + * and accumulates the bytes that are left to be transfered.
  95114. + *
  95115. + * @return The total bytes left to transfered, or a negative value as failure
  95116. + */
  95117. +static inline int cfi_calc_desc_residue(dwc_otg_pcd_ep_t * ep)
  95118. +{
  95119. + int32_t ret = 0;
  95120. + int i;
  95121. + struct dwc_otg_dma_desc *ddesc = NULL;
  95122. + struct cfi_ep *cfiep;
  95123. +
  95124. + /* See if the pcd_ep has its respective cfi_ep mapped */
  95125. + cfiep = get_cfi_ep_by_pcd_ep(ep->pcd->cfi, ep);
  95126. + if (!cfiep) {
  95127. + CFI_INFO("%s: Failed to find ep\n", __func__);
  95128. + return -1;
  95129. + }
  95130. +
  95131. + ddesc = ep->dwc_ep.descs;
  95132. +
  95133. + for (i = 0; (i < cfiep->desc_count) && (i < MAX_DMA_DESCS_PER_EP); i++) {
  95134. +
  95135. +#if defined(PRINT_CFI_DMA_DESCS)
  95136. + print_desc(ddesc, ep->ep.name, i);
  95137. +#endif
  95138. + ret += ddesc->status.b.bytes;
  95139. + ddesc++;
  95140. + }
  95141. +
  95142. + if (ret)
  95143. + CFI_INFO("!!!!!!!!!! WARNING (%s) - residue=%d\n", __func__,
  95144. + ret);
  95145. +
  95146. + return ret;
  95147. +}
  95148. +#endif
  95149. +
  95150. +/**
  95151. + * This function completes the request for the EP. If there are
  95152. + * additional requests for the EP in the queue they will be started.
  95153. + */
  95154. +static void complete_ep(dwc_otg_pcd_ep_t * ep)
  95155. +{
  95156. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  95157. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  95158. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  95159. + dev_if->in_ep_regs[ep->dwc_ep.num];
  95160. + deptsiz_data_t deptsiz;
  95161. + dev_dma_desc_sts_t desc_sts;
  95162. + dwc_otg_pcd_request_t *req = 0;
  95163. + dwc_otg_dev_dma_desc_t *dma_desc;
  95164. + uint32_t byte_count = 0;
  95165. + int is_last = 0;
  95166. + int i;
  95167. +
  95168. + DWC_DEBUGPL(DBG_PCDV, "%s() %d-%s\n", __func__, ep->dwc_ep.num,
  95169. + (ep->dwc_ep.is_in ? "IN" : "OUT"));
  95170. +
  95171. + /* Get any pending requests */
  95172. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  95173. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  95174. + if (!req) {
  95175. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  95176. + return;
  95177. + }
  95178. + } else {
  95179. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  95180. + return;
  95181. + }
  95182. +
  95183. + DWC_DEBUGPL(DBG_PCD, "Requests %d\n", ep->pcd->request_pending);
  95184. +
  95185. + if (ep->dwc_ep.is_in) {
  95186. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  95187. +
  95188. + if (core_if->dma_enable) {
  95189. + if (core_if->dma_desc_enable == 0) {
  95190. + if (deptsiz.b.xfersize == 0
  95191. + && deptsiz.b.pktcnt == 0) {
  95192. + byte_count =
  95193. + ep->dwc_ep.xfer_len -
  95194. + ep->dwc_ep.xfer_count;
  95195. +
  95196. + ep->dwc_ep.xfer_buff += byte_count;
  95197. + ep->dwc_ep.dma_addr += byte_count;
  95198. + ep->dwc_ep.xfer_count += byte_count;
  95199. +
  95200. + DWC_DEBUGPL(DBG_PCDV,
  95201. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  95202. + ep->dwc_ep.num,
  95203. + (ep->dwc_ep.
  95204. + is_in ? "IN" : "OUT"),
  95205. + ep->dwc_ep.xfer_len,
  95206. + deptsiz.b.xfersize,
  95207. + deptsiz.b.pktcnt);
  95208. +
  95209. + if (ep->dwc_ep.xfer_len <
  95210. + ep->dwc_ep.total_len) {
  95211. + dwc_otg_ep_start_transfer
  95212. + (core_if, &ep->dwc_ep);
  95213. + } else if (ep->dwc_ep.sent_zlp) {
  95214. + /*
  95215. + * This fragment of code should initiate 0
  95216. + * length transfer in case if it is queued
  95217. + * a transfer with size divisible to EPs max
  95218. + * packet size and with usb_request zero field
  95219. + * is set, which means that after data is transfered,
  95220. + * it is also should be transfered
  95221. + * a 0 length packet at the end. For Slave and
  95222. + * Buffer DMA modes in this case SW has
  95223. + * to initiate 2 transfers one with transfer size,
  95224. + * and the second with 0 size. For Descriptor
  95225. + * DMA mode SW is able to initiate a transfer,
  95226. + * which will handle all the packets including
  95227. + * the last 0 length.
  95228. + */
  95229. + ep->dwc_ep.sent_zlp = 0;
  95230. + dwc_otg_ep_start_zl_transfer
  95231. + (core_if, &ep->dwc_ep);
  95232. + } else {
  95233. + is_last = 1;
  95234. + }
  95235. + } else {
  95236. + if (ep->dwc_ep.type ==
  95237. + DWC_OTG_EP_TYPE_ISOC) {
  95238. + req->actual = 0;
  95239. + dwc_otg_request_done(ep, req, 0);
  95240. +
  95241. + ep->dwc_ep.start_xfer_buff = 0;
  95242. + ep->dwc_ep.xfer_buff = 0;
  95243. + ep->dwc_ep.xfer_len = 0;
  95244. +
  95245. + /* If there is a request in the queue start it. */
  95246. + start_next_request(ep);
  95247. + } else
  95248. + DWC_WARN
  95249. + ("Incomplete transfer (%d - %s [siz=%d pkt=%d])\n",
  95250. + ep->dwc_ep.num,
  95251. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  95252. + deptsiz.b.xfersize,
  95253. + deptsiz.b.pktcnt);
  95254. + }
  95255. + } else {
  95256. + dma_desc = ep->dwc_ep.desc_addr;
  95257. + byte_count = 0;
  95258. + ep->dwc_ep.sent_zlp = 0;
  95259. +
  95260. +#ifdef DWC_UTE_CFI
  95261. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  95262. + ep->dwc_ep.buff_mode);
  95263. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  95264. + int residue;
  95265. +
  95266. + residue = cfi_calc_desc_residue(ep);
  95267. + if (residue < 0)
  95268. + return;
  95269. +
  95270. + byte_count = residue;
  95271. + } else {
  95272. +#endif
  95273. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  95274. + ++i) {
  95275. + desc_sts = dma_desc->status;
  95276. + byte_count += desc_sts.b.bytes;
  95277. + dma_desc++;
  95278. + }
  95279. +#ifdef DWC_UTE_CFI
  95280. + }
  95281. +#endif
  95282. + if (byte_count == 0) {
  95283. + ep->dwc_ep.xfer_count =
  95284. + ep->dwc_ep.total_len;
  95285. + is_last = 1;
  95286. + } else {
  95287. + DWC_WARN("Incomplete transfer\n");
  95288. + }
  95289. + }
  95290. + } else {
  95291. + if (deptsiz.b.xfersize == 0 && deptsiz.b.pktcnt == 0) {
  95292. + DWC_DEBUGPL(DBG_PCDV,
  95293. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  95294. + ep->dwc_ep.num,
  95295. + ep->dwc_ep.is_in ? "IN" : "OUT",
  95296. + ep->dwc_ep.xfer_len,
  95297. + deptsiz.b.xfersize,
  95298. + deptsiz.b.pktcnt);
  95299. +
  95300. + /* Check if the whole transfer was completed,
  95301. + * if no, setup transfer for next portion of data
  95302. + */
  95303. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  95304. + dwc_otg_ep_start_transfer(core_if,
  95305. + &ep->dwc_ep);
  95306. + } else if (ep->dwc_ep.sent_zlp) {
  95307. + /*
  95308. + * This fragment of code should initiate 0
  95309. + * length trasfer in case if it is queued
  95310. + * a trasfer with size divisible to EPs max
  95311. + * packet size and with usb_request zero field
  95312. + * is set, which means that after data is transfered,
  95313. + * it is also should be transfered
  95314. + * a 0 length packet at the end. For Slave and
  95315. + * Buffer DMA modes in this case SW has
  95316. + * to initiate 2 transfers one with transfer size,
  95317. + * and the second with 0 size. For Desriptor
  95318. + * DMA mode SW is able to initiate a transfer,
  95319. + * which will handle all the packets including
  95320. + * the last 0 legth.
  95321. + */
  95322. + ep->dwc_ep.sent_zlp = 0;
  95323. + dwc_otg_ep_start_zl_transfer(core_if,
  95324. + &ep->dwc_ep);
  95325. + } else {
  95326. + is_last = 1;
  95327. + }
  95328. + } else {
  95329. + DWC_WARN
  95330. + ("Incomplete transfer (%d-%s [siz=%d pkt=%d])\n",
  95331. + ep->dwc_ep.num,
  95332. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  95333. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  95334. + }
  95335. + }
  95336. + } else {
  95337. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  95338. + dev_if->out_ep_regs[ep->dwc_ep.num];
  95339. + desc_sts.d32 = 0;
  95340. + if (core_if->dma_enable) {
  95341. + if (core_if->dma_desc_enable) {
  95342. + dma_desc = ep->dwc_ep.desc_addr;
  95343. + byte_count = 0;
  95344. + ep->dwc_ep.sent_zlp = 0;
  95345. +
  95346. +#ifdef DWC_UTE_CFI
  95347. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  95348. + ep->dwc_ep.buff_mode);
  95349. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  95350. + int residue;
  95351. + residue = cfi_calc_desc_residue(ep);
  95352. + if (residue < 0)
  95353. + return;
  95354. + byte_count = residue;
  95355. + } else {
  95356. +#endif
  95357. +
  95358. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  95359. + ++i) {
  95360. + desc_sts = dma_desc->status;
  95361. + byte_count += desc_sts.b.bytes;
  95362. + dma_desc++;
  95363. + }
  95364. +
  95365. +#ifdef DWC_UTE_CFI
  95366. + }
  95367. +#endif
  95368. + /* Checking for interrupt Out transfers with not
  95369. + * dword aligned mps sizes
  95370. + */
  95371. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_INTR &&
  95372. + (ep->dwc_ep.maxpacket%4)) {
  95373. + ep->dwc_ep.xfer_count =
  95374. + ep->dwc_ep.total_len - byte_count;
  95375. + if ((ep->dwc_ep.xfer_len %
  95376. + ep->dwc_ep.maxpacket)
  95377. + && (ep->dwc_ep.xfer_len /
  95378. + ep->dwc_ep.maxpacket <
  95379. + MAX_DMA_DESC_CNT))
  95380. + ep->dwc_ep.xfer_len -=
  95381. + (ep->dwc_ep.desc_cnt -
  95382. + 1) * ep->dwc_ep.maxpacket +
  95383. + ep->dwc_ep.xfer_len %
  95384. + ep->dwc_ep.maxpacket;
  95385. + else
  95386. + ep->dwc_ep.xfer_len -=
  95387. + ep->dwc_ep.desc_cnt *
  95388. + ep->dwc_ep.maxpacket;
  95389. + if (ep->dwc_ep.xfer_len > 0) {
  95390. + dwc_otg_ep_start_transfer
  95391. + (core_if, &ep->dwc_ep);
  95392. + } else {
  95393. + is_last = 1;
  95394. + }
  95395. + } else {
  95396. + ep->dwc_ep.xfer_count =
  95397. + ep->dwc_ep.total_len - byte_count +
  95398. + ((4 -
  95399. + (ep->dwc_ep.
  95400. + total_len & 0x3)) & 0x3);
  95401. + is_last = 1;
  95402. + }
  95403. + } else {
  95404. + deptsiz.d32 = 0;
  95405. + deptsiz.d32 =
  95406. + DWC_READ_REG32(&out_ep_regs->doeptsiz);
  95407. +
  95408. + byte_count = (ep->dwc_ep.xfer_len -
  95409. + ep->dwc_ep.xfer_count -
  95410. + deptsiz.b.xfersize);
  95411. + ep->dwc_ep.xfer_buff += byte_count;
  95412. + ep->dwc_ep.dma_addr += byte_count;
  95413. + ep->dwc_ep.xfer_count += byte_count;
  95414. +
  95415. + /* Check if the whole transfer was completed,
  95416. + * if no, setup transfer for next portion of data
  95417. + */
  95418. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  95419. + dwc_otg_ep_start_transfer(core_if,
  95420. + &ep->dwc_ep);
  95421. + } else if (ep->dwc_ep.sent_zlp) {
  95422. + /*
  95423. + * This fragment of code should initiate 0
  95424. + * length trasfer in case if it is queued
  95425. + * a trasfer with size divisible to EPs max
  95426. + * packet size and with usb_request zero field
  95427. + * is set, which means that after data is transfered,
  95428. + * it is also should be transfered
  95429. + * a 0 length packet at the end. For Slave and
  95430. + * Buffer DMA modes in this case SW has
  95431. + * to initiate 2 transfers one with transfer size,
  95432. + * and the second with 0 size. For Desriptor
  95433. + * DMA mode SW is able to initiate a transfer,
  95434. + * which will handle all the packets including
  95435. + * the last 0 legth.
  95436. + */
  95437. + ep->dwc_ep.sent_zlp = 0;
  95438. + dwc_otg_ep_start_zl_transfer(core_if,
  95439. + &ep->dwc_ep);
  95440. + } else {
  95441. + is_last = 1;
  95442. + }
  95443. + }
  95444. + } else {
  95445. + /* Check if the whole transfer was completed,
  95446. + * if no, setup transfer for next portion of data
  95447. + */
  95448. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  95449. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  95450. + } else if (ep->dwc_ep.sent_zlp) {
  95451. + /*
  95452. + * This fragment of code should initiate 0
  95453. + * length transfer in case if it is queued
  95454. + * a transfer with size divisible to EPs max
  95455. + * packet size and with usb_request zero field
  95456. + * is set, which means that after data is transfered,
  95457. + * it is also should be transfered
  95458. + * a 0 length packet at the end. For Slave and
  95459. + * Buffer DMA modes in this case SW has
  95460. + * to initiate 2 transfers one with transfer size,
  95461. + * and the second with 0 size. For Descriptor
  95462. + * DMA mode SW is able to initiate a transfer,
  95463. + * which will handle all the packets including
  95464. + * the last 0 length.
  95465. + */
  95466. + ep->dwc_ep.sent_zlp = 0;
  95467. + dwc_otg_ep_start_zl_transfer(core_if,
  95468. + &ep->dwc_ep);
  95469. + } else {
  95470. + is_last = 1;
  95471. + }
  95472. + }
  95473. +
  95474. + DWC_DEBUGPL(DBG_PCDV,
  95475. + "addr %p, %d-%s len=%d cnt=%d xsize=%d pktcnt=%d\n",
  95476. + &out_ep_regs->doeptsiz, ep->dwc_ep.num,
  95477. + ep->dwc_ep.is_in ? "IN" : "OUT",
  95478. + ep->dwc_ep.xfer_len, ep->dwc_ep.xfer_count,
  95479. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  95480. + }
  95481. +
  95482. + /* Complete the request */
  95483. + if (is_last) {
  95484. +#ifdef DWC_UTE_CFI
  95485. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  95486. + req->actual = ep->dwc_ep.cfi_req_len - byte_count;
  95487. + } else {
  95488. +#endif
  95489. + req->actual = ep->dwc_ep.xfer_count;
  95490. +#ifdef DWC_UTE_CFI
  95491. + }
  95492. +#endif
  95493. + if (req->dw_align_buf) {
  95494. + if (!ep->dwc_ep.is_in) {
  95495. + dwc_memcpy(req->buf, req->dw_align_buf, req->length);
  95496. + }
  95497. + DWC_DMA_FREE(req->length, req->dw_align_buf,
  95498. + req->dw_align_buf_dma);
  95499. + }
  95500. +
  95501. + dwc_otg_request_done(ep, req, 0);
  95502. +
  95503. + ep->dwc_ep.start_xfer_buff = 0;
  95504. + ep->dwc_ep.xfer_buff = 0;
  95505. + ep->dwc_ep.xfer_len = 0;
  95506. +
  95507. + /* If there is a request in the queue start it. */
  95508. + start_next_request(ep);
  95509. + }
  95510. +}
  95511. +
  95512. +#ifdef DWC_EN_ISOC
  95513. +
  95514. +/**
  95515. + * This function BNA interrupt for Isochronous EPs
  95516. + *
  95517. + */
  95518. +static void dwc_otg_pcd_handle_iso_bna(dwc_otg_pcd_ep_t * ep)
  95519. +{
  95520. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  95521. + volatile uint32_t *addr;
  95522. + depctl_data_t depctl = {.d32 = 0 };
  95523. + dwc_otg_pcd_t *pcd = ep->pcd;
  95524. + dwc_otg_dev_dma_desc_t *dma_desc;
  95525. + int i;
  95526. +
  95527. + dma_desc =
  95528. + dwc_ep->iso_desc_addr + dwc_ep->desc_cnt * (dwc_ep->proc_buf_num);
  95529. +
  95530. + if (dwc_ep->is_in) {
  95531. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  95532. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  95533. + sts.d32 = dma_desc->status.d32;
  95534. + sts.b_iso_in.bs = BS_HOST_READY;
  95535. + dma_desc->status.d32 = sts.d32;
  95536. + }
  95537. + } else {
  95538. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  95539. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  95540. + sts.d32 = dma_desc->status.d32;
  95541. + sts.b_iso_out.bs = BS_HOST_READY;
  95542. + dma_desc->status.d32 = sts.d32;
  95543. + }
  95544. + }
  95545. +
  95546. + if (dwc_ep->is_in == 0) {
  95547. + addr =
  95548. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->
  95549. + num]->doepctl;
  95550. + } else {
  95551. + addr =
  95552. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  95553. + }
  95554. + depctl.b.epena = 1;
  95555. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  95556. +}
  95557. +
  95558. +/**
  95559. + * This function sets latest iso packet information(non-PTI mode)
  95560. + *
  95561. + * @param core_if Programming view of DWC_otg controller.
  95562. + * @param ep The EP to start the transfer on.
  95563. + *
  95564. + */
  95565. +void set_current_pkt_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  95566. +{
  95567. + deptsiz_data_t deptsiz = {.d32 = 0 };
  95568. + dma_addr_t dma_addr;
  95569. + uint32_t offset;
  95570. +
  95571. + if (ep->proc_buf_num)
  95572. + dma_addr = ep->dma_addr1;
  95573. + else
  95574. + dma_addr = ep->dma_addr0;
  95575. +
  95576. + if (ep->is_in) {
  95577. + deptsiz.d32 =
  95578. + DWC_READ_REG32(&core_if->dev_if->
  95579. + in_ep_regs[ep->num]->dieptsiz);
  95580. + offset = ep->data_per_frame;
  95581. + } else {
  95582. + deptsiz.d32 =
  95583. + DWC_READ_REG32(&core_if->dev_if->
  95584. + out_ep_regs[ep->num]->doeptsiz);
  95585. + offset =
  95586. + ep->data_per_frame +
  95587. + (0x4 & (0x4 - (ep->data_per_frame & 0x3)));
  95588. + }
  95589. +
  95590. + if (!deptsiz.b.xfersize) {
  95591. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  95592. + ep->pkt_info[ep->cur_pkt].offset =
  95593. + ep->cur_pkt_dma_addr - dma_addr;
  95594. + ep->pkt_info[ep->cur_pkt].status = 0;
  95595. + } else {
  95596. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  95597. + ep->pkt_info[ep->cur_pkt].offset =
  95598. + ep->cur_pkt_dma_addr - dma_addr;
  95599. + ep->pkt_info[ep->cur_pkt].status = -DWC_E_NO_DATA;
  95600. + }
  95601. + ep->cur_pkt_addr += offset;
  95602. + ep->cur_pkt_dma_addr += offset;
  95603. + ep->cur_pkt++;
  95604. +}
  95605. +
  95606. +/**
  95607. + * This function sets latest iso packet information(DDMA mode)
  95608. + *
  95609. + * @param core_if Programming view of DWC_otg controller.
  95610. + * @param dwc_ep The EP to start the transfer on.
  95611. + *
  95612. + */
  95613. +static void set_ddma_iso_pkts_info(dwc_otg_core_if_t * core_if,
  95614. + dwc_ep_t * dwc_ep)
  95615. +{
  95616. + dwc_otg_dev_dma_desc_t *dma_desc;
  95617. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  95618. + iso_pkt_info_t *iso_packet;
  95619. + uint32_t data_per_desc;
  95620. + uint32_t offset;
  95621. + int i, j;
  95622. +
  95623. + iso_packet = dwc_ep->pkt_info;
  95624. +
  95625. + /** Reinit closed DMA Descriptors*/
  95626. + /** ISO OUT EP */
  95627. + if (dwc_ep->is_in == 0) {
  95628. + dma_desc =
  95629. + dwc_ep->iso_desc_addr +
  95630. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  95631. + offset = 0;
  95632. +
  95633. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  95634. + i += dwc_ep->pkt_per_frm) {
  95635. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  95636. + data_per_desc =
  95637. + ((j + 1) * dwc_ep->maxpacket >
  95638. + dwc_ep->
  95639. + data_per_frame) ? dwc_ep->data_per_frame -
  95640. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  95641. + data_per_desc +=
  95642. + (data_per_desc % 4) ? (4 -
  95643. + data_per_desc %
  95644. + 4) : 0;
  95645. +
  95646. + sts.d32 = dma_desc->status.d32;
  95647. +
  95648. + /* Write status in iso_packet_decsriptor */
  95649. + iso_packet->status =
  95650. + sts.b_iso_out.rxsts +
  95651. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  95652. + if (iso_packet->status) {
  95653. + iso_packet->status = -DWC_E_NO_DATA;
  95654. + }
  95655. +
  95656. + /* Received data length */
  95657. + if (!sts.b_iso_out.rxbytes) {
  95658. + iso_packet->length =
  95659. + data_per_desc -
  95660. + sts.b_iso_out.rxbytes;
  95661. + } else {
  95662. + iso_packet->length =
  95663. + data_per_desc -
  95664. + sts.b_iso_out.rxbytes + (4 -
  95665. + dwc_ep->data_per_frame
  95666. + % 4);
  95667. + }
  95668. +
  95669. + iso_packet->offset = offset;
  95670. +
  95671. + offset += data_per_desc;
  95672. + dma_desc++;
  95673. + iso_packet++;
  95674. + }
  95675. + }
  95676. +
  95677. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  95678. + data_per_desc =
  95679. + ((j + 1) * dwc_ep->maxpacket >
  95680. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  95681. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  95682. + data_per_desc +=
  95683. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  95684. +
  95685. + sts.d32 = dma_desc->status.d32;
  95686. +
  95687. + /* Write status in iso_packet_decsriptor */
  95688. + iso_packet->status =
  95689. + sts.b_iso_out.rxsts +
  95690. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  95691. + if (iso_packet->status) {
  95692. + iso_packet->status = -DWC_E_NO_DATA;
  95693. + }
  95694. +
  95695. + /* Received data length */
  95696. + iso_packet->length =
  95697. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  95698. +
  95699. + iso_packet->offset = offset;
  95700. +
  95701. + offset += data_per_desc;
  95702. + iso_packet++;
  95703. + dma_desc++;
  95704. + }
  95705. +
  95706. + sts.d32 = dma_desc->status.d32;
  95707. +
  95708. + /* Write status in iso_packet_decsriptor */
  95709. + iso_packet->status =
  95710. + sts.b_iso_out.rxsts + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  95711. + if (iso_packet->status) {
  95712. + iso_packet->status = -DWC_E_NO_DATA;
  95713. + }
  95714. + /* Received data length */
  95715. + if (!sts.b_iso_out.rxbytes) {
  95716. + iso_packet->length =
  95717. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  95718. + } else {
  95719. + iso_packet->length =
  95720. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes +
  95721. + (4 - dwc_ep->data_per_frame % 4);
  95722. + }
  95723. +
  95724. + iso_packet->offset = offset;
  95725. + } else {
  95726. +/** ISO IN EP */
  95727. +
  95728. + dma_desc =
  95729. + dwc_ep->iso_desc_addr +
  95730. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  95731. +
  95732. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  95733. + sts.d32 = dma_desc->status.d32;
  95734. +
  95735. + /* Write status in iso packet descriptor */
  95736. + iso_packet->status =
  95737. + sts.b_iso_in.txsts +
  95738. + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  95739. + if (iso_packet->status != 0) {
  95740. + iso_packet->status = -DWC_E_NO_DATA;
  95741. +
  95742. + }
  95743. + /* Bytes has been transfered */
  95744. + iso_packet->length =
  95745. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  95746. +
  95747. + dma_desc++;
  95748. + iso_packet++;
  95749. + }
  95750. +
  95751. + sts.d32 = dma_desc->status.d32;
  95752. + while (sts.b_iso_in.bs == BS_DMA_BUSY) {
  95753. + sts.d32 = dma_desc->status.d32;
  95754. + }
  95755. +
  95756. + /* Write status in iso packet descriptor ??? do be done with ERROR codes */
  95757. + iso_packet->status =
  95758. + sts.b_iso_in.txsts + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  95759. + if (iso_packet->status != 0) {
  95760. + iso_packet->status = -DWC_E_NO_DATA;
  95761. + }
  95762. +
  95763. + /* Bytes has been transfered */
  95764. + iso_packet->length =
  95765. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  95766. + }
  95767. +}
  95768. +
  95769. +/**
  95770. + * This function reinitialize DMA Descriptors for Isochronous transfer
  95771. + *
  95772. + * @param core_if Programming view of DWC_otg controller.
  95773. + * @param dwc_ep The EP to start the transfer on.
  95774. + *
  95775. + */
  95776. +static void reinit_ddma_iso_xfer(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  95777. +{
  95778. + int i, j;
  95779. + dwc_otg_dev_dma_desc_t *dma_desc;
  95780. + dma_addr_t dma_ad;
  95781. + volatile uint32_t *addr;
  95782. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  95783. + uint32_t data_per_desc;
  95784. +
  95785. + if (dwc_ep->is_in == 0) {
  95786. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  95787. + } else {
  95788. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  95789. + }
  95790. +
  95791. + if (dwc_ep->proc_buf_num == 0) {
  95792. + /** Buffer 0 descriptors setup */
  95793. + dma_ad = dwc_ep->dma_addr0;
  95794. + } else {
  95795. + /** Buffer 1 descriptors setup */
  95796. + dma_ad = dwc_ep->dma_addr1;
  95797. + }
  95798. +
  95799. + /** Reinit closed DMA Descriptors*/
  95800. + /** ISO OUT EP */
  95801. + if (dwc_ep->is_in == 0) {
  95802. + dma_desc =
  95803. + dwc_ep->iso_desc_addr +
  95804. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  95805. +
  95806. + sts.b_iso_out.bs = BS_HOST_READY;
  95807. + sts.b_iso_out.rxsts = 0;
  95808. + sts.b_iso_out.l = 0;
  95809. + sts.b_iso_out.sp = 0;
  95810. + sts.b_iso_out.ioc = 0;
  95811. + sts.b_iso_out.pid = 0;
  95812. + sts.b_iso_out.framenum = 0;
  95813. +
  95814. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  95815. + i += dwc_ep->pkt_per_frm) {
  95816. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  95817. + data_per_desc =
  95818. + ((j + 1) * dwc_ep->maxpacket >
  95819. + dwc_ep->
  95820. + data_per_frame) ? dwc_ep->data_per_frame -
  95821. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  95822. + data_per_desc +=
  95823. + (data_per_desc % 4) ? (4 -
  95824. + data_per_desc %
  95825. + 4) : 0;
  95826. + sts.b_iso_out.rxbytes = data_per_desc;
  95827. + dma_desc->buf = dma_ad;
  95828. + dma_desc->status.d32 = sts.d32;
  95829. +
  95830. + dma_ad += data_per_desc;
  95831. + dma_desc++;
  95832. + }
  95833. + }
  95834. +
  95835. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  95836. +
  95837. + data_per_desc =
  95838. + ((j + 1) * dwc_ep->maxpacket >
  95839. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  95840. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  95841. + data_per_desc +=
  95842. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  95843. + sts.b_iso_out.rxbytes = data_per_desc;
  95844. +
  95845. + dma_desc->buf = dma_ad;
  95846. + dma_desc->status.d32 = sts.d32;
  95847. +
  95848. + dma_desc++;
  95849. + dma_ad += data_per_desc;
  95850. + }
  95851. +
  95852. + sts.b_iso_out.ioc = 1;
  95853. + sts.b_iso_out.l = dwc_ep->proc_buf_num;
  95854. +
  95855. + data_per_desc =
  95856. + ((j + 1) * dwc_ep->maxpacket >
  95857. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  95858. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  95859. + data_per_desc +=
  95860. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  95861. + sts.b_iso_out.rxbytes = data_per_desc;
  95862. +
  95863. + dma_desc->buf = dma_ad;
  95864. + dma_desc->status.d32 = sts.d32;
  95865. + } else {
  95866. +/** ISO IN EP */
  95867. +
  95868. + dma_desc =
  95869. + dwc_ep->iso_desc_addr +
  95870. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  95871. +
  95872. + sts.b_iso_in.bs = BS_HOST_READY;
  95873. + sts.b_iso_in.txsts = 0;
  95874. + sts.b_iso_in.sp = 0;
  95875. + sts.b_iso_in.ioc = 0;
  95876. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  95877. + sts.b_iso_in.framenum = dwc_ep->next_frame;
  95878. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  95879. + sts.b_iso_in.l = 0;
  95880. +
  95881. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  95882. + dma_desc->buf = dma_ad;
  95883. + dma_desc->status.d32 = sts.d32;
  95884. +
  95885. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  95886. + dma_ad += dwc_ep->data_per_frame;
  95887. + dma_desc++;
  95888. + }
  95889. +
  95890. + sts.b_iso_in.ioc = 1;
  95891. + sts.b_iso_in.l = dwc_ep->proc_buf_num;
  95892. +
  95893. + dma_desc->buf = dma_ad;
  95894. + dma_desc->status.d32 = sts.d32;
  95895. +
  95896. + dwc_ep->next_frame =
  95897. + sts.b_iso_in.framenum + dwc_ep->bInterval * 1;
  95898. + }
  95899. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  95900. +}
  95901. +
  95902. +/**
  95903. + * This function is to handle Iso EP transfer complete interrupt
  95904. + * in case Iso out packet was dropped
  95905. + *
  95906. + * @param core_if Programming view of DWC_otg controller.
  95907. + * @param dwc_ep The EP for wihich transfer complete was asserted
  95908. + *
  95909. + */
  95910. +static uint32_t handle_iso_out_pkt_dropped(dwc_otg_core_if_t * core_if,
  95911. + dwc_ep_t * dwc_ep)
  95912. +{
  95913. + uint32_t dma_addr;
  95914. + uint32_t drp_pkt;
  95915. + uint32_t drp_pkt_cnt;
  95916. + deptsiz_data_t deptsiz = {.d32 = 0 };
  95917. + depctl_data_t depctl = {.d32 = 0 };
  95918. + int i;
  95919. +
  95920. + deptsiz.d32 =
  95921. + DWC_READ_REG32(&core_if->dev_if->
  95922. + out_ep_regs[dwc_ep->num]->doeptsiz);
  95923. +
  95924. + drp_pkt = dwc_ep->pkt_cnt - deptsiz.b.pktcnt;
  95925. + drp_pkt_cnt = dwc_ep->pkt_per_frm - (drp_pkt % dwc_ep->pkt_per_frm);
  95926. +
  95927. + /* Setting dropped packets status */
  95928. + for (i = 0; i < drp_pkt_cnt; ++i) {
  95929. + dwc_ep->pkt_info[drp_pkt].status = -DWC_E_NO_DATA;
  95930. + drp_pkt++;
  95931. + deptsiz.b.pktcnt--;
  95932. + }
  95933. +
  95934. + if (deptsiz.b.pktcnt > 0) {
  95935. + deptsiz.b.xfersize =
  95936. + dwc_ep->xfer_len - (dwc_ep->pkt_cnt -
  95937. + deptsiz.b.pktcnt) * dwc_ep->maxpacket;
  95938. + } else {
  95939. + deptsiz.b.xfersize = 0;
  95940. + deptsiz.b.pktcnt = 0;
  95941. + }
  95942. +
  95943. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz,
  95944. + deptsiz.d32);
  95945. +
  95946. + if (deptsiz.b.pktcnt > 0) {
  95947. + if (dwc_ep->proc_buf_num) {
  95948. + dma_addr =
  95949. + dwc_ep->dma_addr1 + dwc_ep->xfer_len -
  95950. + deptsiz.b.xfersize;
  95951. + } else {
  95952. + dma_addr =
  95953. + dwc_ep->dma_addr0 + dwc_ep->xfer_len -
  95954. + deptsiz.b.xfersize;;
  95955. + }
  95956. +
  95957. + DWC_WRITE_REG32(&core_if->dev_if->
  95958. + out_ep_regs[dwc_ep->num]->doepdma, dma_addr);
  95959. +
  95960. + /** Re-enable endpoint, clear nak */
  95961. + depctl.d32 = 0;
  95962. + depctl.b.epena = 1;
  95963. + depctl.b.cnak = 1;
  95964. +
  95965. + DWC_MODIFY_REG32(&core_if->dev_if->
  95966. + out_ep_regs[dwc_ep->num]->doepctl, depctl.d32,
  95967. + depctl.d32);
  95968. + return 0;
  95969. + } else {
  95970. + return 1;
  95971. + }
  95972. +}
  95973. +
  95974. +/**
  95975. + * This function sets iso packets information(PTI mode)
  95976. + *
  95977. + * @param core_if Programming view of DWC_otg controller.
  95978. + * @param ep The EP to start the transfer on.
  95979. + *
  95980. + */
  95981. +static uint32_t set_iso_pkts_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  95982. +{
  95983. + int i, j;
  95984. + dma_addr_t dma_ad;
  95985. + iso_pkt_info_t *packet_info = ep->pkt_info;
  95986. + uint32_t offset;
  95987. + uint32_t frame_data;
  95988. + deptsiz_data_t deptsiz;
  95989. +
  95990. + if (ep->proc_buf_num == 0) {
  95991. + /** Buffer 0 descriptors setup */
  95992. + dma_ad = ep->dma_addr0;
  95993. + } else {
  95994. + /** Buffer 1 descriptors setup */
  95995. + dma_ad = ep->dma_addr1;
  95996. + }
  95997. +
  95998. + if (ep->is_in) {
  95999. + deptsiz.d32 =
  96000. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  96001. + dieptsiz);
  96002. + } else {
  96003. + deptsiz.d32 =
  96004. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  96005. + doeptsiz);
  96006. + }
  96007. +
  96008. + if (!deptsiz.b.xfersize) {
  96009. + offset = 0;
  96010. + for (i = 0; i < ep->pkt_cnt; i += ep->pkt_per_frm) {
  96011. + frame_data = ep->data_per_frame;
  96012. + for (j = 0; j < ep->pkt_per_frm; ++j) {
  96013. +
  96014. + /* Packet status - is not set as initially
  96015. + * it is set to 0 and if packet was sent
  96016. + successfully, status field will remain 0*/
  96017. +
  96018. + /* Bytes has been transfered */
  96019. + packet_info->length =
  96020. + (ep->maxpacket <
  96021. + frame_data) ? ep->maxpacket : frame_data;
  96022. +
  96023. + /* Received packet offset */
  96024. + packet_info->offset = offset;
  96025. + offset += packet_info->length;
  96026. + frame_data -= packet_info->length;
  96027. +
  96028. + packet_info++;
  96029. + }
  96030. + }
  96031. + return 1;
  96032. + } else {
  96033. + /* This is a workaround for in case of Transfer Complete with
  96034. + * PktDrpSts interrupts merging - in this case Transfer complete
  96035. + * interrupt for Isoc Out Endpoint is asserted without PktDrpSts
  96036. + * set and with DOEPTSIZ register non zero. Investigations showed,
  96037. + * that this happens when Out packet is dropped, but because of
  96038. + * interrupts merging during first interrupt handling PktDrpSts
  96039. + * bit is cleared and for next merged interrupts it is not reset.
  96040. + * In this case SW hadles the interrupt as if PktDrpSts bit is set.
  96041. + */
  96042. + if (ep->is_in) {
  96043. + return 1;
  96044. + } else {
  96045. + return handle_iso_out_pkt_dropped(core_if, ep);
  96046. + }
  96047. + }
  96048. +}
  96049. +
  96050. +/**
  96051. + * This function is to handle Iso EP transfer complete interrupt
  96052. + *
  96053. + * @param pcd The PCD
  96054. + * @param ep The EP for which transfer complete was asserted
  96055. + *
  96056. + */
  96057. +static void complete_iso_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  96058. +{
  96059. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  96060. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  96061. + uint8_t is_last = 0;
  96062. +
  96063. + if (ep->dwc_ep.next_frame == 0xffffffff) {
  96064. + DWC_WARN("Next frame is not set!\n");
  96065. + return;
  96066. + }
  96067. +
  96068. + if (core_if->dma_enable) {
  96069. + if (core_if->dma_desc_enable) {
  96070. + set_ddma_iso_pkts_info(core_if, dwc_ep);
  96071. + reinit_ddma_iso_xfer(core_if, dwc_ep);
  96072. + is_last = 1;
  96073. + } else {
  96074. + if (core_if->pti_enh_enable) {
  96075. + if (set_iso_pkts_info(core_if, dwc_ep)) {
  96076. + dwc_ep->proc_buf_num =
  96077. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  96078. + dwc_otg_iso_ep_start_buf_transfer
  96079. + (core_if, dwc_ep);
  96080. + is_last = 1;
  96081. + }
  96082. + } else {
  96083. + set_current_pkt_info(core_if, dwc_ep);
  96084. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  96085. + is_last = 1;
  96086. + dwc_ep->cur_pkt = 0;
  96087. + dwc_ep->proc_buf_num =
  96088. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  96089. + if (dwc_ep->proc_buf_num) {
  96090. + dwc_ep->cur_pkt_addr =
  96091. + dwc_ep->xfer_buff1;
  96092. + dwc_ep->cur_pkt_dma_addr =
  96093. + dwc_ep->dma_addr1;
  96094. + } else {
  96095. + dwc_ep->cur_pkt_addr =
  96096. + dwc_ep->xfer_buff0;
  96097. + dwc_ep->cur_pkt_dma_addr =
  96098. + dwc_ep->dma_addr0;
  96099. + }
  96100. +
  96101. + }
  96102. + dwc_otg_iso_ep_start_frm_transfer(core_if,
  96103. + dwc_ep);
  96104. + }
  96105. + }
  96106. + } else {
  96107. + set_current_pkt_info(core_if, dwc_ep);
  96108. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  96109. + is_last = 1;
  96110. + dwc_ep->cur_pkt = 0;
  96111. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  96112. + if (dwc_ep->proc_buf_num) {
  96113. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff1;
  96114. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr1;
  96115. + } else {
  96116. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff0;
  96117. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr0;
  96118. + }
  96119. +
  96120. + }
  96121. + dwc_otg_iso_ep_start_frm_transfer(core_if, dwc_ep);
  96122. + }
  96123. + if (is_last)
  96124. + dwc_otg_iso_buffer_done(pcd, ep, ep->iso_req_handle);
  96125. +}
  96126. +#endif /* DWC_EN_ISOC */
  96127. +
  96128. +/**
  96129. + * This function handle BNA interrupt for Non Isochronous EPs
  96130. + *
  96131. + */
  96132. +static void dwc_otg_pcd_handle_noniso_bna(dwc_otg_pcd_ep_t * ep)
  96133. +{
  96134. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  96135. + volatile uint32_t *addr;
  96136. + depctl_data_t depctl = {.d32 = 0 };
  96137. + dwc_otg_pcd_t *pcd = ep->pcd;
  96138. + dwc_otg_dev_dma_desc_t *dma_desc;
  96139. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  96140. + dwc_otg_core_if_t *core_if = ep->pcd->core_if;
  96141. + int i, start;
  96142. +
  96143. + if (!dwc_ep->desc_cnt)
  96144. + DWC_WARN("Ep%d %s Descriptor count = %d \n", dwc_ep->num,
  96145. + (dwc_ep->is_in ? "IN" : "OUT"), dwc_ep->desc_cnt);
  96146. +
  96147. + if (core_if->core_params->cont_on_bna && !dwc_ep->is_in
  96148. + && dwc_ep->type != DWC_OTG_EP_TYPE_CONTROL) {
  96149. + uint32_t doepdma;
  96150. + dwc_otg_dev_out_ep_regs_t *out_regs =
  96151. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  96152. + doepdma = DWC_READ_REG32(&(out_regs->doepdma));
  96153. + start = (doepdma - dwc_ep->dma_desc_addr)/sizeof(dwc_otg_dev_dma_desc_t);
  96154. + dma_desc = &(dwc_ep->desc_addr[start]);
  96155. + } else {
  96156. + start = 0;
  96157. + dma_desc = dwc_ep->desc_addr;
  96158. + }
  96159. +
  96160. +
  96161. + for (i = start; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  96162. + sts.d32 = dma_desc->status.d32;
  96163. + sts.b.bs = BS_HOST_READY;
  96164. + dma_desc->status.d32 = sts.d32;
  96165. + }
  96166. +
  96167. + if (dwc_ep->is_in == 0) {
  96168. + addr =
  96169. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->num]->
  96170. + doepctl;
  96171. + } else {
  96172. + addr =
  96173. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  96174. + }
  96175. + depctl.b.epena = 1;
  96176. + depctl.b.cnak = 1;
  96177. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  96178. +}
  96179. +
  96180. +/**
  96181. + * This function handles EP0 Control transfers.
  96182. + *
  96183. + * The state of the control transfers are tracked in
  96184. + * <code>ep0state</code>.
  96185. + */
  96186. +static void handle_ep0(dwc_otg_pcd_t * pcd)
  96187. +{
  96188. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  96189. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  96190. + dev_dma_desc_sts_t desc_sts;
  96191. + deptsiz0_data_t deptsiz;
  96192. + uint32_t byte_count;
  96193. +
  96194. +#ifdef DEBUG_EP0
  96195. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  96196. + print_ep0_state(pcd);
  96197. +#endif
  96198. +
  96199. +// DWC_PRINTF("HANDLE EP0\n");
  96200. +
  96201. + switch (pcd->ep0state) {
  96202. + case EP0_DISCONNECT:
  96203. + break;
  96204. +
  96205. + case EP0_IDLE:
  96206. + pcd->request_config = 0;
  96207. +
  96208. + pcd_setup(pcd);
  96209. + break;
  96210. +
  96211. + case EP0_IN_DATA_PHASE:
  96212. +#ifdef DEBUG_EP0
  96213. + DWC_DEBUGPL(DBG_PCD, "DATA_IN EP%d-%s: type=%d, mps=%d\n",
  96214. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  96215. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  96216. +#endif
  96217. +
  96218. + if (core_if->dma_enable != 0) {
  96219. + /*
  96220. + * For EP0 we can only program 1 packet at a time so we
  96221. + * need to do the make calculations after each complete.
  96222. + * Call write_packet to make the calculations, as in
  96223. + * slave mode, and use those values to determine if we
  96224. + * can complete.
  96225. + */
  96226. + if (core_if->dma_desc_enable == 0) {
  96227. + deptsiz.d32 =
  96228. + DWC_READ_REG32(&core_if->
  96229. + dev_if->in_ep_regs[0]->
  96230. + dieptsiz);
  96231. + byte_count =
  96232. + ep0->dwc_ep.xfer_len - deptsiz.b.xfersize;
  96233. + } else {
  96234. + desc_sts =
  96235. + core_if->dev_if->in_desc_addr->status;
  96236. + byte_count =
  96237. + ep0->dwc_ep.xfer_len - desc_sts.b.bytes;
  96238. + }
  96239. + ep0->dwc_ep.xfer_count += byte_count;
  96240. + ep0->dwc_ep.xfer_buff += byte_count;
  96241. + ep0->dwc_ep.dma_addr += byte_count;
  96242. + }
  96243. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  96244. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  96245. + &ep0->dwc_ep);
  96246. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  96247. + } else if (ep0->dwc_ep.sent_zlp) {
  96248. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  96249. + &ep0->dwc_ep);
  96250. + ep0->dwc_ep.sent_zlp = 0;
  96251. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  96252. + } else {
  96253. + ep0_complete_request(ep0);
  96254. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  96255. + }
  96256. + break;
  96257. + case EP0_OUT_DATA_PHASE:
  96258. +#ifdef DEBUG_EP0
  96259. + DWC_DEBUGPL(DBG_PCD, "DATA_OUT EP%d-%s: type=%d, mps=%d\n",
  96260. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  96261. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  96262. +#endif
  96263. + if (core_if->dma_enable != 0) {
  96264. + if (core_if->dma_desc_enable == 0) {
  96265. + deptsiz.d32 =
  96266. + DWC_READ_REG32(&core_if->
  96267. + dev_if->out_ep_regs[0]->
  96268. + doeptsiz);
  96269. + byte_count =
  96270. + ep0->dwc_ep.maxpacket - deptsiz.b.xfersize;
  96271. + } else {
  96272. + desc_sts =
  96273. + core_if->dev_if->out_desc_addr->status;
  96274. + byte_count =
  96275. + ep0->dwc_ep.maxpacket - desc_sts.b.bytes;
  96276. + }
  96277. + ep0->dwc_ep.xfer_count += byte_count;
  96278. + ep0->dwc_ep.xfer_buff += byte_count;
  96279. + ep0->dwc_ep.dma_addr += byte_count;
  96280. + }
  96281. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  96282. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  96283. + &ep0->dwc_ep);
  96284. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  96285. + } else if (ep0->dwc_ep.sent_zlp) {
  96286. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  96287. + &ep0->dwc_ep);
  96288. + ep0->dwc_ep.sent_zlp = 0;
  96289. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  96290. + } else {
  96291. + ep0_complete_request(ep0);
  96292. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  96293. + }
  96294. + break;
  96295. +
  96296. + case EP0_IN_STATUS_PHASE:
  96297. + case EP0_OUT_STATUS_PHASE:
  96298. + DWC_DEBUGPL(DBG_PCD, "CASE: EP0_STATUS\n");
  96299. + ep0_complete_request(ep0);
  96300. + pcd->ep0state = EP0_IDLE;
  96301. + ep0->stopped = 1;
  96302. + ep0->dwc_ep.is_in = 0; /* OUT for next SETUP */
  96303. +
  96304. + /* Prepare for more SETUP Packets */
  96305. + if (core_if->dma_enable) {
  96306. + ep0_out_start(core_if, pcd);
  96307. + }
  96308. + break;
  96309. +
  96310. + case EP0_STALL:
  96311. + DWC_ERROR("EP0 STALLed, should not get here pcd_setup()\n");
  96312. + break;
  96313. + }
  96314. +#ifdef DEBUG_EP0
  96315. + print_ep0_state(pcd);
  96316. +#endif
  96317. +}
  96318. +
  96319. +/**
  96320. + * Restart transfer
  96321. + */
  96322. +static void restart_transfer(dwc_otg_pcd_t * pcd, const uint32_t epnum)
  96323. +{
  96324. + dwc_otg_core_if_t *core_if;
  96325. + dwc_otg_dev_if_t *dev_if;
  96326. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  96327. + dwc_otg_pcd_ep_t *ep;
  96328. +
  96329. + ep = get_in_ep(pcd, epnum);
  96330. +
  96331. +#ifdef DWC_EN_ISOC
  96332. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  96333. + return;
  96334. + }
  96335. +#endif /* DWC_EN_ISOC */
  96336. +
  96337. + core_if = GET_CORE_IF(pcd);
  96338. + dev_if = core_if->dev_if;
  96339. +
  96340. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  96341. +
  96342. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x xfer_len=%0x"
  96343. + " stopped=%d\n", ep->dwc_ep.xfer_buff,
  96344. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len, ep->stopped);
  96345. + /*
  96346. + * If xfersize is 0 and pktcnt in not 0, resend the last packet.
  96347. + */
  96348. + if (dieptsiz.b.pktcnt && dieptsiz.b.xfersize == 0 &&
  96349. + ep->dwc_ep.start_xfer_buff != 0) {
  96350. + if (ep->dwc_ep.total_len <= ep->dwc_ep.maxpacket) {
  96351. + ep->dwc_ep.xfer_count = 0;
  96352. + ep->dwc_ep.xfer_buff = ep->dwc_ep.start_xfer_buff;
  96353. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  96354. + } else {
  96355. + ep->dwc_ep.xfer_count -= ep->dwc_ep.maxpacket;
  96356. + /* convert packet size to dwords. */
  96357. + ep->dwc_ep.xfer_buff -= ep->dwc_ep.maxpacket;
  96358. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  96359. + }
  96360. + ep->stopped = 0;
  96361. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x "
  96362. + "xfer_len=%0x stopped=%d\n",
  96363. + ep->dwc_ep.xfer_buff,
  96364. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len,
  96365. + ep->stopped);
  96366. + if (epnum == 0) {
  96367. + dwc_otg_ep0_start_transfer(core_if, &ep->dwc_ep);
  96368. + } else {
  96369. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  96370. + }
  96371. + }
  96372. +}
  96373. +
  96374. +/*
  96375. + * This function create new nextep sequnce based on Learn Queue.
  96376. + *
  96377. + * @param core_if Programming view of DWC_otg controller
  96378. + */
  96379. +void predict_nextep_seq( dwc_otg_core_if_t * core_if)
  96380. +{
  96381. + dwc_otg_device_global_regs_t *dev_global_regs =
  96382. + core_if->dev_if->dev_global_regs;
  96383. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  96384. + /* Number of Token Queue Registers */
  96385. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  96386. + dtknq1_data_t dtknqr1;
  96387. + uint32_t in_tkn_epnums[4];
  96388. + uint8_t seqnum[MAX_EPS_CHANNELS];
  96389. + uint8_t intkn_seq[TOKEN_Q_DEPTH];
  96390. + grstctl_t resetctl = {.d32 = 0 };
  96391. + uint8_t temp;
  96392. + int ndx = 0;
  96393. + int start = 0;
  96394. + int end = 0;
  96395. + int sort_done = 0;
  96396. + int i = 0;
  96397. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  96398. +
  96399. +
  96400. + DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  96401. +
  96402. + /* Read the DTKNQ Registers */
  96403. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  96404. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  96405. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  96406. + in_tkn_epnums[i]);
  96407. + if (addr == &dev_global_regs->dvbusdis) {
  96408. + addr = &dev_global_regs->dtknqr3_dthrctl;
  96409. + } else {
  96410. + ++addr;
  96411. + }
  96412. +
  96413. + }
  96414. +
  96415. + /* Copy the DTKNQR1 data to the bit field. */
  96416. + dtknqr1.d32 = in_tkn_epnums[0];
  96417. + if (dtknqr1.b.wrap_bit) {
  96418. + ndx = dtknqr1.b.intknwptr;
  96419. + end = ndx -1;
  96420. + if (end < 0)
  96421. + end = TOKEN_Q_DEPTH -1;
  96422. + } else {
  96423. + ndx = 0;
  96424. + end = dtknqr1.b.intknwptr -1;
  96425. + if (end < 0)
  96426. + end = 0;
  96427. + }
  96428. + start = ndx;
  96429. +
  96430. + /* Fill seqnum[] by initial values: EP number + 31 */
  96431. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  96432. + seqnum[i] = i +31;
  96433. + }
  96434. +
  96435. + /* Fill intkn_seq[] from in_tkn_epnums[0] */
  96436. + for (i=0; i < 6; i++)
  96437. + intkn_seq[i] = (in_tkn_epnums[0] >> ((7-i) * 4)) & 0xf;
  96438. +
  96439. + if (TOKEN_Q_DEPTH > 6) {
  96440. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  96441. + for (i=6; i < 14; i++)
  96442. + intkn_seq[i] =
  96443. + (in_tkn_epnums[1] >> ((7 - (i - 6)) * 4)) & 0xf;
  96444. + }
  96445. +
  96446. + if (TOKEN_Q_DEPTH > 14) {
  96447. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  96448. + for (i=14; i < 22; i++)
  96449. + intkn_seq[i] =
  96450. + (in_tkn_epnums[2] >> ((7 - (i - 14)) * 4)) & 0xf;
  96451. + }
  96452. +
  96453. + if (TOKEN_Q_DEPTH > 22) {
  96454. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  96455. + for (i=22; i < 30; i++)
  96456. + intkn_seq[i] =
  96457. + (in_tkn_epnums[3] >> ((7 - (i - 22)) * 4)) & 0xf;
  96458. + }
  96459. +
  96460. + DWC_DEBUGPL(DBG_PCDV, "%s start=%d end=%d intkn_seq[]:\n", __func__,
  96461. + start, end);
  96462. + for (i=0; i<TOKEN_Q_DEPTH; i++)
  96463. + DWC_DEBUGPL(DBG_PCDV,"%d\n", intkn_seq[i]);
  96464. +
  96465. + /* Update seqnum based on intkn_seq[] */
  96466. + i = 0;
  96467. + do {
  96468. + seqnum[intkn_seq[ndx]] = i;
  96469. + ndx++;
  96470. + i++;
  96471. + if (ndx == TOKEN_Q_DEPTH)
  96472. + ndx = 0;
  96473. + } while ( i < TOKEN_Q_DEPTH );
  96474. +
  96475. + /* Mark non active EP's in seqnum[] by 0xff */
  96476. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  96477. + if (core_if->nextep_seq[i] == 0xff )
  96478. + seqnum[i] = 0xff;
  96479. + }
  96480. +
  96481. + /* Sort seqnum[] */
  96482. + sort_done = 0;
  96483. + while (!sort_done) {
  96484. + sort_done = 1;
  96485. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  96486. + if (seqnum[i] > seqnum[i+1]) {
  96487. + temp = seqnum[i];
  96488. + seqnum[i] = seqnum[i+1];
  96489. + seqnum[i+1] = temp;
  96490. + sort_done = 0;
  96491. + }
  96492. + }
  96493. + }
  96494. +
  96495. + ndx = start + seqnum[0];
  96496. + if (ndx >= TOKEN_Q_DEPTH)
  96497. + ndx = ndx % TOKEN_Q_DEPTH;
  96498. + core_if->first_in_nextep_seq = intkn_seq[ndx];
  96499. +
  96500. + /* Update seqnum[] by EP numbers */
  96501. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  96502. + ndx = start + i;
  96503. + if (seqnum[i] < 31) {
  96504. + ndx = start + seqnum[i];
  96505. + if (ndx >= TOKEN_Q_DEPTH)
  96506. + ndx = ndx % TOKEN_Q_DEPTH;
  96507. + seqnum[i] = intkn_seq[ndx];
  96508. + } else {
  96509. + if (seqnum[i] < 0xff) {
  96510. + seqnum[i] = seqnum[i] - 31;
  96511. + } else {
  96512. + break;
  96513. + }
  96514. + }
  96515. + }
  96516. +
  96517. + /* Update nextep_seq[] based on seqnum[] */
  96518. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  96519. + if (seqnum[i] != 0xff) {
  96520. + if (seqnum[i+1] != 0xff) {
  96521. + core_if->nextep_seq[seqnum[i]] = seqnum[i+1];
  96522. + } else {
  96523. + core_if->nextep_seq[seqnum[i]] = core_if->first_in_nextep_seq;
  96524. + break;
  96525. + }
  96526. + } else {
  96527. + break;
  96528. + }
  96529. + }
  96530. +
  96531. + DWC_DEBUGPL(DBG_PCDV, "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  96532. + __func__, core_if->first_in_nextep_seq);
  96533. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  96534. + DWC_DEBUGPL(DBG_PCDV,"%2d\n", core_if->nextep_seq[i]);
  96535. + }
  96536. +
  96537. + /* Flush the Learning Queue */
  96538. + resetctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->grstctl);
  96539. + resetctl.b.intknqflsh = 1;
  96540. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  96541. +
  96542. +
  96543. +}
  96544. +
  96545. +/**
  96546. + * handle the IN EP disable interrupt.
  96547. + */
  96548. +static inline void handle_in_ep_disable_intr(dwc_otg_pcd_t * pcd,
  96549. + const uint32_t epnum)
  96550. +{
  96551. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  96552. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  96553. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  96554. + dctl_data_t dctl = {.d32 = 0 };
  96555. + dwc_otg_pcd_ep_t *ep;
  96556. + dwc_ep_t *dwc_ep;
  96557. + gintmsk_data_t gintmsk_data;
  96558. + depctl_data_t depctl;
  96559. + uint32_t diepdma;
  96560. + uint32_t remain_to_transfer = 0;
  96561. + uint8_t i;
  96562. + uint32_t xfer_size;
  96563. +
  96564. + ep = get_in_ep(pcd, epnum);
  96565. + dwc_ep = &ep->dwc_ep;
  96566. +
  96567. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  96568. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  96569. + complete_ep(ep);
  96570. + return;
  96571. + }
  96572. +
  96573. + DWC_DEBUGPL(DBG_PCD, "diepctl%d=%0x\n", epnum,
  96574. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl));
  96575. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  96576. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  96577. +
  96578. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  96579. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  96580. +
  96581. + if ((core_if->start_predict == 0) || (depctl.b.eptype & 1)) {
  96582. + if (ep->stopped) {
  96583. + if (core_if->en_multiple_tx_fifo)
  96584. + /* Flush the Tx FIFO */
  96585. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  96586. + /* Clear the Global IN NP NAK */
  96587. + dctl.d32 = 0;
  96588. + dctl.b.cgnpinnak = 1;
  96589. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  96590. + /* Restart the transaction */
  96591. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  96592. + restart_transfer(pcd, epnum);
  96593. + }
  96594. + } else {
  96595. + /* Restart the transaction */
  96596. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  96597. + restart_transfer(pcd, epnum);
  96598. + }
  96599. + DWC_DEBUGPL(DBG_ANY, "STOPPED!!!\n");
  96600. + }
  96601. + return;
  96602. + }
  96603. +
  96604. + if (core_if->start_predict > 2) { // NP IN EP
  96605. + core_if->start_predict--;
  96606. + return;
  96607. + }
  96608. +
  96609. + core_if->start_predict--;
  96610. +
  96611. + if (core_if->start_predict == 1) { // All NP IN Ep's disabled now
  96612. +
  96613. + predict_nextep_seq(core_if);
  96614. +
  96615. + /* Update all active IN EP's NextEP field based of nextep_seq[] */
  96616. + for ( i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  96617. + depctl.d32 =
  96618. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  96619. + if (core_if->nextep_seq[i] != 0xff) { // Active NP IN EP
  96620. + depctl.b.nextep = core_if->nextep_seq[i];
  96621. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  96622. + }
  96623. + }
  96624. + /* Flush Shared NP TxFIFO */
  96625. + dwc_otg_flush_tx_fifo(core_if, 0);
  96626. + /* Rewind buffers */
  96627. + if (!core_if->dma_desc_enable) {
  96628. + i = core_if->first_in_nextep_seq;
  96629. + do {
  96630. + ep = get_in_ep(pcd, i);
  96631. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  96632. + xfer_size = ep->dwc_ep.total_len - ep->dwc_ep.xfer_count;
  96633. + if (xfer_size > ep->dwc_ep.maxxfer)
  96634. + xfer_size = ep->dwc_ep.maxxfer;
  96635. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  96636. + if (dieptsiz.b.pktcnt != 0) {
  96637. + if (xfer_size == 0) {
  96638. + remain_to_transfer = 0;
  96639. + } else {
  96640. + if ((xfer_size % ep->dwc_ep.maxpacket) == 0) {
  96641. + remain_to_transfer =
  96642. + dieptsiz.b.pktcnt * ep->dwc_ep.maxpacket;
  96643. + } else {
  96644. + remain_to_transfer = ((dieptsiz.b.pktcnt -1) * ep->dwc_ep.maxpacket)
  96645. + + (xfer_size % ep->dwc_ep.maxpacket);
  96646. + }
  96647. + }
  96648. + diepdma = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepdma);
  96649. + dieptsiz.b.xfersize = remain_to_transfer;
  96650. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, dieptsiz.d32);
  96651. + diepdma = ep->dwc_ep.dma_addr + (xfer_size - remain_to_transfer);
  96652. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, diepdma);
  96653. + }
  96654. + i = core_if->nextep_seq[i];
  96655. + } while (i != core_if->first_in_nextep_seq);
  96656. + } else { // dma_desc_enable
  96657. + DWC_PRINTF("%s Learning Queue not supported in DDMA\n", __func__);
  96658. + }
  96659. +
  96660. + /* Restart transfers in predicted sequences */
  96661. + i = core_if->first_in_nextep_seq;
  96662. + do {
  96663. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  96664. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  96665. + if (dieptsiz.b.pktcnt != 0) {
  96666. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  96667. + depctl.b.epena = 1;
  96668. + depctl.b.cnak = 1;
  96669. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  96670. + }
  96671. + i = core_if->nextep_seq[i];
  96672. + } while (i != core_if->first_in_nextep_seq);
  96673. +
  96674. + /* Clear the global non-periodic IN NAK handshake */
  96675. + dctl.d32 = 0;
  96676. + dctl.b.cgnpinnak = 1;
  96677. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  96678. +
  96679. + /* Unmask EP Mismatch interrupt */
  96680. + gintmsk_data.d32 = 0;
  96681. + gintmsk_data.b.epmismatch = 1;
  96682. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk_data.d32);
  96683. +
  96684. + core_if->start_predict = 0;
  96685. +
  96686. + }
  96687. +}
  96688. +
  96689. +/**
  96690. + * Handler for the IN EP timeout handshake interrupt.
  96691. + */
  96692. +static inline void handle_in_ep_timeout_intr(dwc_otg_pcd_t * pcd,
  96693. + const uint32_t epnum)
  96694. +{
  96695. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  96696. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  96697. +
  96698. +#ifdef DEBUG
  96699. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  96700. + uint32_t num = 0;
  96701. +#endif
  96702. + dctl_data_t dctl = {.d32 = 0 };
  96703. + dwc_otg_pcd_ep_t *ep;
  96704. +
  96705. + gintmsk_data_t intr_mask = {.d32 = 0 };
  96706. +
  96707. + ep = get_in_ep(pcd, epnum);
  96708. +
  96709. + /* Disable the NP Tx Fifo Empty Interrrupt */
  96710. + if (!core_if->dma_enable) {
  96711. + intr_mask.b.nptxfempty = 1;
  96712. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  96713. + intr_mask.d32, 0);
  96714. + }
  96715. + /** @todo NGS Check EP type.
  96716. + * Implement for Periodic EPs */
  96717. + /*
  96718. + * Non-periodic EP
  96719. + */
  96720. + /* Enable the Global IN NAK Effective Interrupt */
  96721. + intr_mask.b.ginnakeff = 1;
  96722. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  96723. +
  96724. + /* Set Global IN NAK */
  96725. + dctl.b.sgnpinnak = 1;
  96726. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  96727. +
  96728. + ep->stopped = 1;
  96729. +
  96730. +#ifdef DEBUG
  96731. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[num]->dieptsiz);
  96732. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  96733. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  96734. +#endif
  96735. +
  96736. +#ifdef DISABLE_PERIODIC_EP
  96737. + /*
  96738. + * Set the NAK bit for this EP to
  96739. + * start the disable process.
  96740. + */
  96741. + diepctl.d32 = 0;
  96742. + diepctl.b.snak = 1;
  96743. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[num]->diepctl, diepctl.d32,
  96744. + diepctl.d32);
  96745. + ep->disabling = 1;
  96746. + ep->stopped = 1;
  96747. +#endif
  96748. +}
  96749. +
  96750. +/**
  96751. + * Handler for the IN EP NAK interrupt.
  96752. + */
  96753. +static inline int32_t handle_in_ep_nak_intr(dwc_otg_pcd_t * pcd,
  96754. + const uint32_t epnum)
  96755. +{
  96756. + /** @todo implement ISR */
  96757. + dwc_otg_core_if_t *core_if;
  96758. + diepmsk_data_t intr_mask = {.d32 = 0 };
  96759. +
  96760. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "IN EP NAK");
  96761. + core_if = GET_CORE_IF(pcd);
  96762. + intr_mask.b.nak = 1;
  96763. +
  96764. + if (core_if->multiproc_int_enable) {
  96765. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  96766. + diepeachintmsk[epnum], intr_mask.d32, 0);
  96767. + } else {
  96768. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->diepmsk,
  96769. + intr_mask.d32, 0);
  96770. + }
  96771. +
  96772. + return 1;
  96773. +}
  96774. +
  96775. +/**
  96776. + * Handler for the OUT EP Babble interrupt.
  96777. + */
  96778. +static inline int32_t handle_out_ep_babble_intr(dwc_otg_pcd_t * pcd,
  96779. + const uint32_t epnum)
  96780. +{
  96781. + /** @todo implement ISR */
  96782. + dwc_otg_core_if_t *core_if;
  96783. + doepmsk_data_t intr_mask = {.d32 = 0 };
  96784. +
  96785. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  96786. + "OUT EP Babble");
  96787. + core_if = GET_CORE_IF(pcd);
  96788. + intr_mask.b.babble = 1;
  96789. +
  96790. + if (core_if->multiproc_int_enable) {
  96791. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  96792. + doepeachintmsk[epnum], intr_mask.d32, 0);
  96793. + } else {
  96794. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  96795. + intr_mask.d32, 0);
  96796. + }
  96797. +
  96798. + return 1;
  96799. +}
  96800. +
  96801. +/**
  96802. + * Handler for the OUT EP NAK interrupt.
  96803. + */
  96804. +static inline int32_t handle_out_ep_nak_intr(dwc_otg_pcd_t * pcd,
  96805. + const uint32_t epnum)
  96806. +{
  96807. + /** @todo implement ISR */
  96808. + dwc_otg_core_if_t *core_if;
  96809. + doepmsk_data_t intr_mask = {.d32 = 0 };
  96810. +
  96811. + DWC_DEBUGPL(DBG_ANY, "INTERRUPT Handler not implemented for %s\n", "OUT EP NAK");
  96812. + core_if = GET_CORE_IF(pcd);
  96813. + intr_mask.b.nak = 1;
  96814. +
  96815. + if (core_if->multiproc_int_enable) {
  96816. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  96817. + doepeachintmsk[epnum], intr_mask.d32, 0);
  96818. + } else {
  96819. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  96820. + intr_mask.d32, 0);
  96821. + }
  96822. +
  96823. + return 1;
  96824. +}
  96825. +
  96826. +/**
  96827. + * Handler for the OUT EP NYET interrupt.
  96828. + */
  96829. +static inline int32_t handle_out_ep_nyet_intr(dwc_otg_pcd_t * pcd,
  96830. + const uint32_t epnum)
  96831. +{
  96832. + /** @todo implement ISR */
  96833. + dwc_otg_core_if_t *core_if;
  96834. + doepmsk_data_t intr_mask = {.d32 = 0 };
  96835. +
  96836. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "OUT EP NYET");
  96837. + core_if = GET_CORE_IF(pcd);
  96838. + intr_mask.b.nyet = 1;
  96839. +
  96840. + if (core_if->multiproc_int_enable) {
  96841. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  96842. + doepeachintmsk[epnum], intr_mask.d32, 0);
  96843. + } else {
  96844. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  96845. + intr_mask.d32, 0);
  96846. + }
  96847. +
  96848. + return 1;
  96849. +}
  96850. +
  96851. +/**
  96852. + * This interrupt indicates that an IN EP has a pending Interrupt.
  96853. + * The sequence for handling the IN EP interrupt is shown below:
  96854. + * -# Read the Device All Endpoint Interrupt register
  96855. + * -# Repeat the following for each IN EP interrupt bit set (from
  96856. + * LSB to MSB).
  96857. + * -# Read the Device Endpoint Interrupt (DIEPINTn) register
  96858. + * -# If "Transfer Complete" call the request complete function
  96859. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  96860. + * -# If "AHB Error Interrupt" log error
  96861. + * -# If "Time-out Handshake" log error
  96862. + * -# If "IN Token Received when TxFIFO Empty" write packet to Tx
  96863. + * FIFO.
  96864. + * -# If "IN Token EP Mismatch" (disable, this is handled by EP
  96865. + * Mismatch Interrupt)
  96866. + */
  96867. +static int32_t dwc_otg_pcd_handle_in_ep_intr(dwc_otg_pcd_t * pcd)
  96868. +{
  96869. +#define CLEAR_IN_EP_INTR(__core_if,__epnum,__intr) \
  96870. +do { \
  96871. + diepint_data_t diepint = {.d32=0}; \
  96872. + diepint.b.__intr = 1; \
  96873. + DWC_WRITE_REG32(&__core_if->dev_if->in_ep_regs[__epnum]->diepint, \
  96874. + diepint.d32); \
  96875. +} while (0)
  96876. +
  96877. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  96878. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  96879. + diepint_data_t diepint = {.d32 = 0 };
  96880. + depctl_data_t depctl = {.d32 = 0 };
  96881. + uint32_t ep_intr;
  96882. + uint32_t epnum = 0;
  96883. + dwc_otg_pcd_ep_t *ep;
  96884. + dwc_ep_t *dwc_ep;
  96885. + gintmsk_data_t intr_mask = {.d32 = 0 };
  96886. +
  96887. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, pcd);
  96888. +
  96889. + /* Read in the device interrupt bits */
  96890. + ep_intr = dwc_otg_read_dev_all_in_ep_intr(core_if);
  96891. +
  96892. + /* Service the Device IN interrupts for each endpoint */
  96893. + while (ep_intr) {
  96894. + if (ep_intr & 0x1) {
  96895. + uint32_t empty_msk;
  96896. + /* Get EP pointer */
  96897. + ep = get_in_ep(pcd, epnum);
  96898. + dwc_ep = &ep->dwc_ep;
  96899. +
  96900. + depctl.d32 =
  96901. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  96902. + empty_msk =
  96903. + DWC_READ_REG32(&dev_if->
  96904. + dev_global_regs->dtknqr4_fifoemptymsk);
  96905. +
  96906. + DWC_DEBUGPL(DBG_PCDV,
  96907. + "IN EP INTERRUPT - %d\nepmty_msk - %8x diepctl - %8x\n",
  96908. + epnum, empty_msk, depctl.d32);
  96909. +
  96910. + DWC_DEBUGPL(DBG_PCD,
  96911. + "EP%d-%s: type=%d, mps=%d\n",
  96912. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  96913. + dwc_ep->type, dwc_ep->maxpacket);
  96914. +
  96915. + diepint.d32 =
  96916. + dwc_otg_read_dev_in_ep_intr(core_if, dwc_ep);
  96917. +
  96918. + DWC_DEBUGPL(DBG_PCDV,
  96919. + "EP %d Interrupt Register - 0x%x\n", epnum,
  96920. + diepint.d32);
  96921. + /* Transfer complete */
  96922. + if (diepint.b.xfercompl) {
  96923. + /* Disable the NP Tx FIFO Empty
  96924. + * Interrupt */
  96925. + if (core_if->en_multiple_tx_fifo == 0) {
  96926. + intr_mask.b.nptxfempty = 1;
  96927. + DWC_MODIFY_REG32
  96928. + (&core_if->core_global_regs->gintmsk,
  96929. + intr_mask.d32, 0);
  96930. + } else {
  96931. + /* Disable the Tx FIFO Empty Interrupt for this EP */
  96932. + uint32_t fifoemptymsk =
  96933. + 0x1 << dwc_ep->num;
  96934. + DWC_MODIFY_REG32(&core_if->
  96935. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  96936. + fifoemptymsk, 0);
  96937. + }
  96938. + /* Clear the bit in DIEPINTn for this interrupt */
  96939. + CLEAR_IN_EP_INTR(core_if, epnum, xfercompl);
  96940. +
  96941. + /* Complete the transfer */
  96942. + if (epnum == 0) {
  96943. + handle_ep0(pcd);
  96944. + }
  96945. +#ifdef DWC_EN_ISOC
  96946. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  96947. + if (!ep->stopped)
  96948. + complete_iso_ep(pcd, ep);
  96949. + }
  96950. +#endif /* DWC_EN_ISOC */
  96951. +#ifdef DWC_UTE_PER_IO
  96952. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  96953. + if (!ep->stopped)
  96954. + complete_xiso_ep(ep);
  96955. + }
  96956. +#endif /* DWC_UTE_PER_IO */
  96957. + else {
  96958. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC &&
  96959. + dwc_ep->bInterval > 1) {
  96960. + dwc_ep->frame_num += dwc_ep->bInterval;
  96961. + if (dwc_ep->frame_num > 0x3FFF)
  96962. + {
  96963. + dwc_ep->frm_overrun = 1;
  96964. + dwc_ep->frame_num &= 0x3FFF;
  96965. + } else
  96966. + dwc_ep->frm_overrun = 0;
  96967. + }
  96968. + complete_ep(ep);
  96969. + if(diepint.b.nak)
  96970. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  96971. + }
  96972. + }
  96973. + /* Endpoint disable */
  96974. + if (diepint.b.epdisabled) {
  96975. + DWC_DEBUGPL(DBG_ANY, "EP%d IN disabled\n",
  96976. + epnum);
  96977. + handle_in_ep_disable_intr(pcd, epnum);
  96978. +
  96979. + /* Clear the bit in DIEPINTn for this interrupt */
  96980. + CLEAR_IN_EP_INTR(core_if, epnum, epdisabled);
  96981. + }
  96982. + /* AHB Error */
  96983. + if (diepint.b.ahberr) {
  96984. + DWC_ERROR("EP%d IN AHB Error\n", epnum);
  96985. + /* Clear the bit in DIEPINTn for this interrupt */
  96986. + CLEAR_IN_EP_INTR(core_if, epnum, ahberr);
  96987. + }
  96988. + /* TimeOUT Handshake (non-ISOC IN EPs) */
  96989. + if (diepint.b.timeout) {
  96990. + DWC_ERROR("EP%d IN Time-out\n", epnum);
  96991. + handle_in_ep_timeout_intr(pcd, epnum);
  96992. +
  96993. + CLEAR_IN_EP_INTR(core_if, epnum, timeout);
  96994. + }
  96995. + /** IN Token received with TxF Empty */
  96996. + if (diepint.b.intktxfemp) {
  96997. + DWC_DEBUGPL(DBG_ANY,
  96998. + "EP%d IN TKN TxFifo Empty\n",
  96999. + epnum);
  97000. + if (!ep->stopped && epnum != 0) {
  97001. +
  97002. + diepmsk_data_t diepmsk = {.d32 = 0 };
  97003. + diepmsk.b.intktxfemp = 1;
  97004. +
  97005. + if (core_if->multiproc_int_enable) {
  97006. + DWC_MODIFY_REG32
  97007. + (&dev_if->dev_global_regs->diepeachintmsk
  97008. + [epnum], diepmsk.d32, 0);
  97009. + } else {
  97010. + DWC_MODIFY_REG32
  97011. + (&dev_if->dev_global_regs->diepmsk,
  97012. + diepmsk.d32, 0);
  97013. + }
  97014. + } else if (core_if->dma_desc_enable
  97015. + && epnum == 0
  97016. + && pcd->ep0state ==
  97017. + EP0_OUT_STATUS_PHASE) {
  97018. + // EP0 IN set STALL
  97019. + depctl.d32 =
  97020. + DWC_READ_REG32(&dev_if->in_ep_regs
  97021. + [epnum]->diepctl);
  97022. +
  97023. + /* set the disable and stall bits */
  97024. + if (depctl.b.epena) {
  97025. + depctl.b.epdis = 1;
  97026. + }
  97027. + depctl.b.stall = 1;
  97028. + DWC_WRITE_REG32(&dev_if->in_ep_regs
  97029. + [epnum]->diepctl,
  97030. + depctl.d32);
  97031. + }
  97032. + CLEAR_IN_EP_INTR(core_if, epnum, intktxfemp);
  97033. + }
  97034. + /** IN Token Received with EP mismatch */
  97035. + if (diepint.b.intknepmis) {
  97036. + DWC_DEBUGPL(DBG_ANY,
  97037. + "EP%d IN TKN EP Mismatch\n", epnum);
  97038. + CLEAR_IN_EP_INTR(core_if, epnum, intknepmis);
  97039. + }
  97040. + /** IN Endpoint NAK Effective */
  97041. + if (diepint.b.inepnakeff) {
  97042. + DWC_DEBUGPL(DBG_ANY,
  97043. + "EP%d IN EP NAK Effective\n",
  97044. + epnum);
  97045. + /* Periodic EP */
  97046. + if (ep->disabling) {
  97047. + depctl.d32 = 0;
  97048. + depctl.b.snak = 1;
  97049. + depctl.b.epdis = 1;
  97050. + DWC_MODIFY_REG32(&dev_if->in_ep_regs
  97051. + [epnum]->diepctl,
  97052. + depctl.d32,
  97053. + depctl.d32);
  97054. + }
  97055. + CLEAR_IN_EP_INTR(core_if, epnum, inepnakeff);
  97056. +
  97057. + }
  97058. +
  97059. + /** IN EP Tx FIFO Empty Intr */
  97060. + if (diepint.b.emptyintr) {
  97061. + DWC_DEBUGPL(DBG_ANY,
  97062. + "EP%d Tx FIFO Empty Intr \n",
  97063. + epnum);
  97064. + write_empty_tx_fifo(pcd, epnum);
  97065. +
  97066. + CLEAR_IN_EP_INTR(core_if, epnum, emptyintr);
  97067. +
  97068. + }
  97069. +
  97070. + /** IN EP BNA Intr */
  97071. + if (diepint.b.bna) {
  97072. + CLEAR_IN_EP_INTR(core_if, epnum, bna);
  97073. + if (core_if->dma_desc_enable) {
  97074. +#ifdef DWC_EN_ISOC
  97075. + if (dwc_ep->type ==
  97076. + DWC_OTG_EP_TYPE_ISOC) {
  97077. + /*
  97078. + * This checking is performed to prevent first "false" BNA
  97079. + * handling occuring right after reconnect
  97080. + */
  97081. + if (dwc_ep->next_frame !=
  97082. + 0xffffffff)
  97083. + dwc_otg_pcd_handle_iso_bna(ep);
  97084. + } else
  97085. +#endif /* DWC_EN_ISOC */
  97086. + {
  97087. + dwc_otg_pcd_handle_noniso_bna(ep);
  97088. + }
  97089. + }
  97090. + }
  97091. + /* NAK Interrutp */
  97092. + if (diepint.b.nak) {
  97093. + DWC_DEBUGPL(DBG_ANY, "EP%d IN NAK Interrupt\n",
  97094. + epnum);
  97095. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  97096. + depctl_data_t depctl;
  97097. + if (ep->dwc_ep.frame_num == 0xFFFFFFFF) {
  97098. + ep->dwc_ep.frame_num = core_if->frame_num;
  97099. + if (ep->dwc_ep.bInterval > 1) {
  97100. + depctl.d32 = 0;
  97101. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  97102. + if (ep->dwc_ep.frame_num & 0x1) {
  97103. + depctl.b.setd1pid = 1;
  97104. + depctl.b.setd0pid = 0;
  97105. + } else {
  97106. + depctl.b.setd0pid = 1;
  97107. + depctl.b.setd1pid = 0;
  97108. + }
  97109. + DWC_WRITE_REG32(&dev_if->in_ep_regs[epnum]->diepctl, depctl.d32);
  97110. + }
  97111. + start_next_request(ep);
  97112. + }
  97113. + ep->dwc_ep.frame_num += ep->dwc_ep.bInterval;
  97114. + if (dwc_ep->frame_num > 0x3FFF) {
  97115. + dwc_ep->frm_overrun = 1;
  97116. + dwc_ep->frame_num &= 0x3FFF;
  97117. + } else
  97118. + dwc_ep->frm_overrun = 0;
  97119. + }
  97120. +
  97121. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  97122. + }
  97123. + }
  97124. + epnum++;
  97125. + ep_intr >>= 1;
  97126. + }
  97127. +
  97128. + return 1;
  97129. +#undef CLEAR_IN_EP_INTR
  97130. +}
  97131. +
  97132. +/**
  97133. + * This interrupt indicates that an OUT EP has a pending Interrupt.
  97134. + * The sequence for handling the OUT EP interrupt is shown below:
  97135. + * -# Read the Device All Endpoint Interrupt register
  97136. + * -# Repeat the following for each OUT EP interrupt bit set (from
  97137. + * LSB to MSB).
  97138. + * -# Read the Device Endpoint Interrupt (DOEPINTn) register
  97139. + * -# If "Transfer Complete" call the request complete function
  97140. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  97141. + * -# If "AHB Error Interrupt" log error
  97142. + * -# If "Setup Phase Done" process Setup Packet (See Standard USB
  97143. + * Command Processing)
  97144. + */
  97145. +static int32_t dwc_otg_pcd_handle_out_ep_intr(dwc_otg_pcd_t * pcd)
  97146. +{
  97147. +#define CLEAR_OUT_EP_INTR(__core_if,__epnum,__intr) \
  97148. +do { \
  97149. + doepint_data_t doepint = {.d32=0}; \
  97150. + doepint.b.__intr = 1; \
  97151. + DWC_WRITE_REG32(&__core_if->dev_if->out_ep_regs[__epnum]->doepint, \
  97152. + doepint.d32); \
  97153. +} while (0)
  97154. +
  97155. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  97156. + uint32_t ep_intr;
  97157. + doepint_data_t doepint = {.d32 = 0 };
  97158. + uint32_t epnum = 0;
  97159. + dwc_otg_pcd_ep_t *ep;
  97160. + dwc_ep_t *dwc_ep;
  97161. + dctl_data_t dctl = {.d32 = 0 };
  97162. + gintmsk_data_t gintmsk = {.d32 = 0 };
  97163. +
  97164. +
  97165. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  97166. +
  97167. + /* Read in the device interrupt bits */
  97168. + ep_intr = dwc_otg_read_dev_all_out_ep_intr(core_if);
  97169. +
  97170. + while (ep_intr) {
  97171. + if (ep_intr & 0x1) {
  97172. + /* Get EP pointer */
  97173. + ep = get_out_ep(pcd, epnum);
  97174. + dwc_ep = &ep->dwc_ep;
  97175. +
  97176. +#ifdef VERBOSE
  97177. + DWC_DEBUGPL(DBG_PCDV,
  97178. + "EP%d-%s: type=%d, mps=%d\n",
  97179. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  97180. + dwc_ep->type, dwc_ep->maxpacket);
  97181. +#endif
  97182. + doepint.d32 =
  97183. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep);
  97184. + /* Moved this interrupt upper due to core deffect of asserting
  97185. + * OUT EP 0 xfercompl along with stsphsrcvd in BDMA */
  97186. + if (doepint.b.stsphsercvd) {
  97187. + deptsiz0_data_t deptsiz;
  97188. + CLEAR_OUT_EP_INTR(core_if, epnum, stsphsercvd);
  97189. + deptsiz.d32 =
  97190. + DWC_READ_REG32(&core_if->dev_if->
  97191. + out_ep_regs[0]->doeptsiz);
  97192. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  97193. + && core_if->dma_enable
  97194. + && core_if->dma_desc_enable == 0
  97195. + && doepint.b.xfercompl
  97196. + && deptsiz.b.xfersize == 24) {
  97197. + CLEAR_OUT_EP_INTR(core_if, epnum,
  97198. + xfercompl);
  97199. + doepint.b.xfercompl = 0;
  97200. + ep0_out_start(core_if, pcd);
  97201. + }
  97202. + if ((core_if->dma_desc_enable) ||
  97203. + (core_if->dma_enable
  97204. + && core_if->snpsid >=
  97205. + OTG_CORE_REV_3_00a)) {
  97206. + do_setup_in_status_phase(pcd);
  97207. + }
  97208. + }
  97209. + /* Transfer complete */
  97210. + if (doepint.b.xfercompl) {
  97211. +
  97212. + if (epnum == 0) {
  97213. + /* Clear the bit in DOEPINTn for this interrupt */
  97214. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  97215. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  97216. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  97217. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepint),
  97218. + doepint.d32);
  97219. + DWC_DEBUGPL(DBG_PCDV, "DOEPCTL=%x \n",
  97220. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepctl));
  97221. +
  97222. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  97223. + && core_if->dma_enable == 0) {
  97224. + doepint_data_t doepint;
  97225. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  97226. + out_ep_regs[0]->doepint);
  97227. + if (pcd->ep0state == EP0_IDLE && doepint.b.sr) {
  97228. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  97229. + goto exit_xfercompl;
  97230. + }
  97231. + }
  97232. + /* In case of DDMA look at SR bit to go to the Data Stage */
  97233. + if (core_if->dma_desc_enable) {
  97234. + dev_dma_desc_sts_t status = {.d32 = 0};
  97235. + if (pcd->ep0state == EP0_IDLE) {
  97236. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  97237. + dev_if->setup_desc_index]->status.d32;
  97238. + if(pcd->data_terminated) {
  97239. + pcd->data_terminated = 0;
  97240. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  97241. + dwc_memcpy(&pcd->setup_pkt->req, pcd->backup_buf, 8);
  97242. + }
  97243. + if (status.b.sr) {
  97244. + if (doepint.b.setup) {
  97245. + DWC_DEBUGPL(DBG_PCDV, "DMA DESC EP0_IDLE SR=1 setup=1\n");
  97246. + /* Already started data stage, clear setup */
  97247. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  97248. + doepint.b.setup = 0;
  97249. + handle_ep0(pcd);
  97250. + /* Prepare for more setup packets */
  97251. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  97252. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  97253. + ep0_out_start(core_if, pcd);
  97254. + }
  97255. +
  97256. + goto exit_xfercompl;
  97257. + } else {
  97258. + /* Prepare for more setup packets */
  97259. + DWC_DEBUGPL(DBG_PCDV,
  97260. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  97261. + ep0_out_start(core_if, pcd);
  97262. + }
  97263. + }
  97264. + } else {
  97265. + dwc_otg_pcd_request_t *req;
  97266. + dev_dma_desc_sts_t status = {.d32 = 0};
  97267. + diepint_data_t diepint0;
  97268. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  97269. + in_ep_regs[0]->diepint);
  97270. +
  97271. + if (pcd->ep0state == EP0_STALL || pcd->ep0state == EP0_DISCONNECT) {
  97272. + DWC_ERROR("EP0 is stalled/disconnected\n");
  97273. + }
  97274. +
  97275. + /* Clear IN xfercompl if set */
  97276. + if (diepint0.b.xfercompl && (pcd->ep0state == EP0_IN_STATUS_PHASE
  97277. + || pcd->ep0state == EP0_IN_DATA_PHASE)) {
  97278. + DWC_WRITE_REG32(&core_if->dev_if->
  97279. + in_ep_regs[0]->diepint, diepint0.d32);
  97280. + }
  97281. +
  97282. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  97283. + dev_if->setup_desc_index]->status.d32;
  97284. +
  97285. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len
  97286. + && (pcd->ep0state == EP0_OUT_DATA_PHASE))
  97287. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  97288. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE)
  97289. + status.d32 = core_if->dev_if->
  97290. + out_desc_addr->status.d32;
  97291. +
  97292. + if (status.b.sr) {
  97293. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  97294. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  97295. + } else {
  97296. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  97297. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  97298. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  97299. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  97300. + /* Read arrived setup packet from req->buf */
  97301. + dwc_memcpy(&pcd->setup_pkt->req,
  97302. + req->buf + ep->dwc_ep.xfer_count, 8);
  97303. + }
  97304. + req->actual = ep->dwc_ep.xfer_count;
  97305. + dwc_otg_request_done(ep, req, -ECONNRESET);
  97306. + ep->dwc_ep.start_xfer_buff = 0;
  97307. + ep->dwc_ep.xfer_buff = 0;
  97308. + ep->dwc_ep.xfer_len = 0;
  97309. + }
  97310. + pcd->ep0state = EP0_IDLE;
  97311. + if (doepint.b.setup) {
  97312. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  97313. + /* Data stage started, clear setup */
  97314. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  97315. + doepint.b.setup = 0;
  97316. + handle_ep0(pcd);
  97317. + /* Prepare for setup packets if ep0in was enabled*/
  97318. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  97319. + ep0_out_start(core_if, pcd);
  97320. + }
  97321. +
  97322. + goto exit_xfercompl;
  97323. + } else {
  97324. + /* Prepare for more setup packets */
  97325. + DWC_DEBUGPL(DBG_PCDV,
  97326. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  97327. + ep0_out_start(core_if, pcd);
  97328. + }
  97329. + }
  97330. + }
  97331. + }
  97332. + if (core_if->snpsid >= OTG_CORE_REV_2_94a && core_if->dma_enable
  97333. + && core_if->dma_desc_enable == 0) {
  97334. + doepint_data_t doepint_temp = {.d32 = 0};
  97335. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  97336. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  97337. + out_ep_regs[ep->dwc_ep.num]->doepint);
  97338. + doeptsize0.d32 = DWC_READ_REG32(&core_if->dev_if->
  97339. + out_ep_regs[ep->dwc_ep.num]->doeptsiz);
  97340. + if (pcd->ep0state == EP0_IDLE) {
  97341. + if (doepint_temp.b.sr) {
  97342. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  97343. + }
  97344. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  97345. + out_ep_regs[0]->doepint);
  97346. + if (doeptsize0.b.supcnt == 3) {
  97347. + DWC_DEBUGPL(DBG_ANY, "Rolling over!!!!!!!\n");
  97348. + ep->dwc_ep.stp_rollover = 1;
  97349. + }
  97350. + if (doepint.b.setup) {
  97351. +retry:
  97352. + /* Already started data stage, clear setup */
  97353. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  97354. + doepint.b.setup = 0;
  97355. + handle_ep0(pcd);
  97356. + ep->dwc_ep.stp_rollover = 0;
  97357. + /* Prepare for more setup packets */
  97358. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  97359. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  97360. + ep0_out_start(core_if, pcd);
  97361. + }
  97362. + goto exit_xfercompl;
  97363. + } else {
  97364. + /* Prepare for more setup packets */
  97365. + DWC_DEBUGPL(DBG_ANY,
  97366. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  97367. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  97368. + out_ep_regs[0]->doepint);
  97369. + if(doepint.b.setup)
  97370. + goto retry;
  97371. + ep0_out_start(core_if, pcd);
  97372. + }
  97373. + } else {
  97374. + dwc_otg_pcd_request_t *req;
  97375. + diepint_data_t diepint0 = {.d32 = 0};
  97376. + doepint_data_t doepint_temp = {.d32 = 0};
  97377. + depctl_data_t diepctl0;
  97378. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  97379. + in_ep_regs[0]->diepint);
  97380. + diepctl0.d32 = DWC_READ_REG32(&core_if->dev_if->
  97381. + in_ep_regs[0]->diepctl);
  97382. +
  97383. + if (pcd->ep0state == EP0_IN_DATA_PHASE
  97384. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  97385. + if (diepint0.b.xfercompl) {
  97386. + DWC_WRITE_REG32(&core_if->dev_if->
  97387. + in_ep_regs[0]->diepint, diepint0.d32);
  97388. + }
  97389. + if (diepctl0.b.epena) {
  97390. + diepint_data_t diepint = {.d32 = 0};
  97391. + diepctl0.b.snak = 1;
  97392. + DWC_WRITE_REG32(&core_if->dev_if->
  97393. + in_ep_regs[0]->diepctl, diepctl0.d32);
  97394. + do {
  97395. + dwc_udelay(10);
  97396. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  97397. + in_ep_regs[0]->diepint);
  97398. + } while (!diepint.b.inepnakeff);
  97399. + diepint.b.inepnakeff = 1;
  97400. + DWC_WRITE_REG32(&core_if->dev_if->
  97401. + in_ep_regs[0]->diepint, diepint.d32);
  97402. + diepctl0.d32 = 0;
  97403. + diepctl0.b.epdis = 1;
  97404. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl,
  97405. + diepctl0.d32);
  97406. + do {
  97407. + dwc_udelay(10);
  97408. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  97409. + in_ep_regs[0]->diepint);
  97410. + } while (!diepint.b.epdisabled);
  97411. + diepint.b.epdisabled = 1;
  97412. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepint,
  97413. + diepint.d32);
  97414. + }
  97415. + }
  97416. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  97417. + out_ep_regs[ep->dwc_ep.num]->doepint);
  97418. + if (doepint_temp.b.sr) {
  97419. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  97420. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  97421. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  97422. + } else {
  97423. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  97424. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  97425. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  97426. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  97427. + /* Read arrived setup packet from req->buf */
  97428. + dwc_memcpy(&pcd->setup_pkt->req,
  97429. + req->buf + ep->dwc_ep.xfer_count, 8);
  97430. + }
  97431. + req->actual = ep->dwc_ep.xfer_count;
  97432. + dwc_otg_request_done(ep, req, -ECONNRESET);
  97433. + ep->dwc_ep.start_xfer_buff = 0;
  97434. + ep->dwc_ep.xfer_buff = 0;
  97435. + ep->dwc_ep.xfer_len = 0;
  97436. + }
  97437. + pcd->ep0state = EP0_IDLE;
  97438. + if (doepint.b.setup) {
  97439. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  97440. + /* Data stage started, clear setup */
  97441. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  97442. + doepint.b.setup = 0;
  97443. + handle_ep0(pcd);
  97444. + /* Prepare for setup packets if ep0in was enabled*/
  97445. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  97446. + ep0_out_start(core_if, pcd);
  97447. + }
  97448. + goto exit_xfercompl;
  97449. + } else {
  97450. + /* Prepare for more setup packets */
  97451. + DWC_DEBUGPL(DBG_PCDV,
  97452. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  97453. + ep0_out_start(core_if, pcd);
  97454. + }
  97455. + }
  97456. + }
  97457. + }
  97458. + if (core_if->dma_enable == 0 || pcd->ep0state != EP0_IDLE)
  97459. + handle_ep0(pcd);
  97460. +exit_xfercompl:
  97461. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  97462. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep), doepint.d32);
  97463. + } else {
  97464. + if (core_if->dma_desc_enable == 0
  97465. + || pcd->ep0state != EP0_IDLE)
  97466. + handle_ep0(pcd);
  97467. + }
  97468. +#ifdef DWC_EN_ISOC
  97469. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  97470. + if (doepint.b.pktdrpsts == 0) {
  97471. + /* Clear the bit in DOEPINTn for this interrupt */
  97472. + CLEAR_OUT_EP_INTR(core_if,
  97473. + epnum,
  97474. + xfercompl);
  97475. + complete_iso_ep(pcd, ep);
  97476. + } else {
  97477. +
  97478. + doepint_data_t doepint = {.d32 = 0 };
  97479. + doepint.b.xfercompl = 1;
  97480. + doepint.b.pktdrpsts = 1;
  97481. + DWC_WRITE_REG32
  97482. + (&core_if->dev_if->out_ep_regs
  97483. + [epnum]->doepint,
  97484. + doepint.d32);
  97485. + if (handle_iso_out_pkt_dropped
  97486. + (core_if, dwc_ep)) {
  97487. + complete_iso_ep(pcd,
  97488. + ep);
  97489. + }
  97490. + }
  97491. +#endif /* DWC_EN_ISOC */
  97492. +#ifdef DWC_UTE_PER_IO
  97493. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  97494. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  97495. + if (!ep->stopped)
  97496. + complete_xiso_ep(ep);
  97497. +#endif /* DWC_UTE_PER_IO */
  97498. + } else {
  97499. + /* Clear the bit in DOEPINTn for this interrupt */
  97500. + CLEAR_OUT_EP_INTR(core_if, epnum,
  97501. + xfercompl);
  97502. +
  97503. + if (core_if->core_params->dev_out_nak) {
  97504. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[epnum]);
  97505. + pcd->core_if->ep_xfer_info[epnum].state = 0;
  97506. +#ifdef DEBUG
  97507. + print_memory_payload(pcd, dwc_ep);
  97508. +#endif
  97509. + }
  97510. + complete_ep(ep);
  97511. + }
  97512. +
  97513. + }
  97514. +
  97515. + /* Endpoint disable */
  97516. + if (doepint.b.epdisabled) {
  97517. +
  97518. + /* Clear the bit in DOEPINTn for this interrupt */
  97519. + CLEAR_OUT_EP_INTR(core_if, epnum, epdisabled);
  97520. + if (core_if->core_params->dev_out_nak) {
  97521. +#ifdef DEBUG
  97522. + print_memory_payload(pcd, dwc_ep);
  97523. +#endif
  97524. + /* In case of timeout condition */
  97525. + if (core_if->ep_xfer_info[epnum].state == 2) {
  97526. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  97527. + dev_global_regs->dctl);
  97528. + dctl.b.cgoutnak = 1;
  97529. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  97530. + dctl.d32);
  97531. + /* Unmask goutnakeff interrupt which was masked
  97532. + * during handle nak out interrupt */
  97533. + gintmsk.b.goutnakeff = 1;
  97534. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  97535. + 0, gintmsk.d32);
  97536. +
  97537. + complete_ep(ep);
  97538. + }
  97539. + }
  97540. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  97541. + {
  97542. + dctl_data_t dctl;
  97543. + gintmsk_data_t intr_mask = {.d32 = 0};
  97544. + dwc_otg_pcd_request_t *req = 0;
  97545. +
  97546. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  97547. + dev_global_regs->dctl);
  97548. + dctl.b.cgoutnak = 1;
  97549. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  97550. + dctl.d32);
  97551. +
  97552. + intr_mask.d32 = 0;
  97553. + intr_mask.b.incomplisoout = 1;
  97554. +
  97555. + /* Get any pending requests */
  97556. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  97557. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  97558. + if (!req) {
  97559. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  97560. + } else {
  97561. + dwc_otg_request_done(ep, req, 0);
  97562. + start_next_request(ep);
  97563. + }
  97564. + } else {
  97565. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  97566. + }
  97567. + }
  97568. + }
  97569. + /* AHB Error */
  97570. + if (doepint.b.ahberr) {
  97571. + DWC_ERROR("EP%d OUT AHB Error\n", epnum);
  97572. + DWC_ERROR("EP%d DEPDMA=0x%08x \n",
  97573. + epnum, core_if->dev_if->out_ep_regs[epnum]->doepdma);
  97574. + CLEAR_OUT_EP_INTR(core_if, epnum, ahberr);
  97575. + }
  97576. + /* Setup Phase Done (contorl EPs) */
  97577. + if (doepint.b.setup) {
  97578. +#ifdef DEBUG_EP0
  97579. + DWC_DEBUGPL(DBG_PCD, "EP%d SETUP Done\n", epnum);
  97580. +#endif
  97581. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  97582. +
  97583. + handle_ep0(pcd);
  97584. + }
  97585. +
  97586. + /** OUT EP BNA Intr */
  97587. + if (doepint.b.bna) {
  97588. + CLEAR_OUT_EP_INTR(core_if, epnum, bna);
  97589. + if (core_if->dma_desc_enable) {
  97590. +#ifdef DWC_EN_ISOC
  97591. + if (dwc_ep->type ==
  97592. + DWC_OTG_EP_TYPE_ISOC) {
  97593. + /*
  97594. + * This checking is performed to prevent first "false" BNA
  97595. + * handling occuring right after reconnect
  97596. + */
  97597. + if (dwc_ep->next_frame !=
  97598. + 0xffffffff)
  97599. + dwc_otg_pcd_handle_iso_bna(ep);
  97600. + } else
  97601. +#endif /* DWC_EN_ISOC */
  97602. + {
  97603. + dwc_otg_pcd_handle_noniso_bna(ep);
  97604. + }
  97605. + }
  97606. + }
  97607. + /* Babble Interrupt */
  97608. + if (doepint.b.babble) {
  97609. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Babble\n",
  97610. + epnum);
  97611. + handle_out_ep_babble_intr(pcd, epnum);
  97612. +
  97613. + CLEAR_OUT_EP_INTR(core_if, epnum, babble);
  97614. + }
  97615. + if (doepint.b.outtknepdis) {
  97616. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Token received when EP is \
  97617. + disabled\n",epnum);
  97618. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  97619. + doepmsk_data_t doepmsk = {.d32 = 0};
  97620. + ep->dwc_ep.frame_num = core_if->frame_num;
  97621. + if (ep->dwc_ep.bInterval > 1) {
  97622. + depctl_data_t depctl;
  97623. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  97624. + out_ep_regs[epnum]->doepctl);
  97625. + if (ep->dwc_ep.frame_num & 0x1) {
  97626. + depctl.b.setd1pid = 1;
  97627. + depctl.b.setd0pid = 0;
  97628. + } else {
  97629. + depctl.b.setd0pid = 1;
  97630. + depctl.b.setd1pid = 0;
  97631. + }
  97632. + DWC_WRITE_REG32(&core_if->dev_if->
  97633. + out_ep_regs[epnum]->doepctl, depctl.d32);
  97634. + }
  97635. + start_next_request(ep);
  97636. + doepmsk.b.outtknepdis = 1;
  97637. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  97638. + doepmsk.d32, 0);
  97639. + }
  97640. + CLEAR_OUT_EP_INTR(core_if, epnum, outtknepdis);
  97641. + }
  97642. +
  97643. + /* NAK Interrutp */
  97644. + if (doepint.b.nak) {
  97645. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NAK\n", epnum);
  97646. + handle_out_ep_nak_intr(pcd, epnum);
  97647. +
  97648. + CLEAR_OUT_EP_INTR(core_if, epnum, nak);
  97649. + }
  97650. + /* NYET Interrutp */
  97651. + if (doepint.b.nyet) {
  97652. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NYET\n", epnum);
  97653. + handle_out_ep_nyet_intr(pcd, epnum);
  97654. +
  97655. + CLEAR_OUT_EP_INTR(core_if, epnum, nyet);
  97656. + }
  97657. + }
  97658. +
  97659. + epnum++;
  97660. + ep_intr >>= 1;
  97661. + }
  97662. +
  97663. + return 1;
  97664. +
  97665. +#undef CLEAR_OUT_EP_INTR
  97666. +}
  97667. +static int drop_transfer(uint32_t trgt_fr, uint32_t curr_fr, uint8_t frm_overrun)
  97668. +{
  97669. + int retval = 0;
  97670. + if(!frm_overrun && curr_fr >= trgt_fr)
  97671. + retval = 1;
  97672. + else if (frm_overrun
  97673. + && (curr_fr >= trgt_fr && ((curr_fr - trgt_fr) < 0x3FFF / 2)))
  97674. + retval = 1;
  97675. + return retval;
  97676. +}
  97677. +/**
  97678. + * Incomplete ISO IN Transfer Interrupt.
  97679. + * This interrupt indicates one of the following conditions occurred
  97680. + * while transmitting an ISOC transaction.
  97681. + * - Corrupted IN Token for ISOC EP.
  97682. + * - Packet not complete in FIFO.
  97683. + * The follow actions will be taken:
  97684. + * -# Determine the EP
  97685. + * -# Set incomplete flag in dwc_ep structure
  97686. + * -# Disable EP; when "Endpoint Disabled" interrupt is received
  97687. + * Flush FIFO
  97688. + */
  97689. +int32_t dwc_otg_pcd_handle_incomplete_isoc_in_intr(dwc_otg_pcd_t * pcd)
  97690. +{
  97691. + gintsts_data_t gintsts;
  97692. +
  97693. +#ifdef DWC_EN_ISOC
  97694. + dwc_otg_dev_if_t *dev_if;
  97695. + deptsiz_data_t deptsiz = {.d32 = 0 };
  97696. + depctl_data_t depctl = {.d32 = 0 };
  97697. + dsts_data_t dsts = {.d32 = 0 };
  97698. + dwc_ep_t *dwc_ep;
  97699. + int i;
  97700. +
  97701. + dev_if = GET_CORE_IF(pcd)->dev_if;
  97702. +
  97703. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  97704. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  97705. + if (dwc_ep->active && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  97706. + deptsiz.d32 =
  97707. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  97708. + depctl.d32 =
  97709. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  97710. +
  97711. + if (depctl.b.epdis && deptsiz.d32) {
  97712. + set_current_pkt_info(GET_CORE_IF(pcd), dwc_ep);
  97713. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  97714. + dwc_ep->cur_pkt = 0;
  97715. + dwc_ep->proc_buf_num =
  97716. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  97717. +
  97718. + if (dwc_ep->proc_buf_num) {
  97719. + dwc_ep->cur_pkt_addr =
  97720. + dwc_ep->xfer_buff1;
  97721. + dwc_ep->cur_pkt_dma_addr =
  97722. + dwc_ep->dma_addr1;
  97723. + } else {
  97724. + dwc_ep->cur_pkt_addr =
  97725. + dwc_ep->xfer_buff0;
  97726. + dwc_ep->cur_pkt_dma_addr =
  97727. + dwc_ep->dma_addr0;
  97728. + }
  97729. +
  97730. + }
  97731. +
  97732. + dsts.d32 =
  97733. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  97734. + dev_global_regs->dsts);
  97735. + dwc_ep->next_frame = dsts.b.soffn;
  97736. +
  97737. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  97738. + (pcd),
  97739. + dwc_ep);
  97740. + }
  97741. + }
  97742. + }
  97743. +
  97744. +#else
  97745. + depctl_data_t depctl = {.d32 = 0 };
  97746. + dwc_ep_t *dwc_ep;
  97747. + dwc_otg_dev_if_t *dev_if;
  97748. + int i;
  97749. + dev_if = GET_CORE_IF(pcd)->dev_if;
  97750. +
  97751. + DWC_DEBUGPL(DBG_PCD,"Incomplete ISO IN \n");
  97752. +
  97753. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  97754. + dwc_ep = &pcd->in_ep[i-1].dwc_ep;
  97755. + depctl.d32 =
  97756. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  97757. + if (depctl.b.epena && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  97758. + if (drop_transfer(dwc_ep->frame_num, GET_CORE_IF(pcd)->frame_num,
  97759. + dwc_ep->frm_overrun))
  97760. + {
  97761. + depctl.d32 =
  97762. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  97763. + depctl.b.snak = 1;
  97764. + depctl.b.epdis = 1;
  97765. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32, depctl.d32);
  97766. + }
  97767. + }
  97768. + }
  97769. +
  97770. + /*intr_mask.b.incomplisoin = 1;
  97771. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  97772. + intr_mask.d32, 0); */
  97773. +#endif //DWC_EN_ISOC
  97774. +
  97775. + /* Clear interrupt */
  97776. + gintsts.d32 = 0;
  97777. + gintsts.b.incomplisoin = 1;
  97778. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  97779. + gintsts.d32);
  97780. +
  97781. + return 1;
  97782. +}
  97783. +
  97784. +/**
  97785. + * Incomplete ISO OUT Transfer Interrupt.
  97786. + *
  97787. + * This interrupt indicates that the core has dropped an ISO OUT
  97788. + * packet. The following conditions can be the cause:
  97789. + * - FIFO Full, the entire packet would not fit in the FIFO.
  97790. + * - CRC Error
  97791. + * - Corrupted Token
  97792. + * The follow actions will be taken:
  97793. + * -# Determine the EP
  97794. + * -# Set incomplete flag in dwc_ep structure
  97795. + * -# Read any data from the FIFO
  97796. + * -# Disable EP. When "Endpoint Disabled" interrupt is received
  97797. + * re-enable EP.
  97798. + */
  97799. +int32_t dwc_otg_pcd_handle_incomplete_isoc_out_intr(dwc_otg_pcd_t * pcd)
  97800. +{
  97801. +
  97802. + gintsts_data_t gintsts;
  97803. +
  97804. +#ifdef DWC_EN_ISOC
  97805. + dwc_otg_dev_if_t *dev_if;
  97806. + deptsiz_data_t deptsiz = {.d32 = 0 };
  97807. + depctl_data_t depctl = {.d32 = 0 };
  97808. + dsts_data_t dsts = {.d32 = 0 };
  97809. + dwc_ep_t *dwc_ep;
  97810. + int i;
  97811. +
  97812. + dev_if = GET_CORE_IF(pcd)->dev_if;
  97813. +
  97814. + for (i = 1; i <= dev_if->num_out_eps; ++i) {
  97815. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  97816. + if (pcd->out_ep[i].dwc_ep.active &&
  97817. + pcd->out_ep[i].dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  97818. + deptsiz.d32 =
  97819. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doeptsiz);
  97820. + depctl.d32 =
  97821. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  97822. +
  97823. + if (depctl.b.epdis && deptsiz.d32) {
  97824. + set_current_pkt_info(GET_CORE_IF(pcd),
  97825. + &pcd->out_ep[i].dwc_ep);
  97826. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  97827. + dwc_ep->cur_pkt = 0;
  97828. + dwc_ep->proc_buf_num =
  97829. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  97830. +
  97831. + if (dwc_ep->proc_buf_num) {
  97832. + dwc_ep->cur_pkt_addr =
  97833. + dwc_ep->xfer_buff1;
  97834. + dwc_ep->cur_pkt_dma_addr =
  97835. + dwc_ep->dma_addr1;
  97836. + } else {
  97837. + dwc_ep->cur_pkt_addr =
  97838. + dwc_ep->xfer_buff0;
  97839. + dwc_ep->cur_pkt_dma_addr =
  97840. + dwc_ep->dma_addr0;
  97841. + }
  97842. +
  97843. + }
  97844. +
  97845. + dsts.d32 =
  97846. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  97847. + dev_global_regs->dsts);
  97848. + dwc_ep->next_frame = dsts.b.soffn;
  97849. +
  97850. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  97851. + (pcd),
  97852. + dwc_ep);
  97853. + }
  97854. + }
  97855. + }
  97856. +#else
  97857. + /** @todo implement ISR */
  97858. + gintmsk_data_t intr_mask = {.d32 = 0 };
  97859. + dwc_otg_core_if_t *core_if;
  97860. + deptsiz_data_t deptsiz = {.d32 = 0 };
  97861. + depctl_data_t depctl = {.d32 = 0 };
  97862. + dctl_data_t dctl = {.d32 = 0 };
  97863. + dwc_ep_t *dwc_ep = NULL;
  97864. + int i;
  97865. + core_if = GET_CORE_IF(pcd);
  97866. +
  97867. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  97868. + dwc_ep = &pcd->out_ep[i].dwc_ep;
  97869. + depctl.d32 =
  97870. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  97871. + if (depctl.b.epena && depctl.b.dpid == (core_if->frame_num & 0x1)) {
  97872. + core_if->dev_if->isoc_ep = dwc_ep;
  97873. + deptsiz.d32 =
  97874. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz);
  97875. + break;
  97876. + }
  97877. + }
  97878. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  97879. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  97880. + intr_mask.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  97881. +
  97882. + if (!intr_mask.b.goutnakeff) {
  97883. + /* Unmask it */
  97884. + intr_mask.b.goutnakeff = 1;
  97885. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32);
  97886. + }
  97887. + if (!gintsts.b.goutnakeff) {
  97888. + dctl.b.sgoutnak = 1;
  97889. + }
  97890. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  97891. +
  97892. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  97893. + if (depctl.b.epena) {
  97894. + depctl.b.epdis = 1;
  97895. + depctl.b.snak = 1;
  97896. + }
  97897. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl, depctl.d32);
  97898. +
  97899. + intr_mask.d32 = 0;
  97900. + intr_mask.b.incomplisoout = 1;
  97901. +
  97902. +#endif /* DWC_EN_ISOC */
  97903. +
  97904. + /* Clear interrupt */
  97905. + gintsts.d32 = 0;
  97906. + gintsts.b.incomplisoout = 1;
  97907. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  97908. + gintsts.d32);
  97909. +
  97910. + return 1;
  97911. +}
  97912. +
  97913. +/**
  97914. + * This function handles the Global IN NAK Effective interrupt.
  97915. + *
  97916. + */
  97917. +int32_t dwc_otg_pcd_handle_in_nak_effective(dwc_otg_pcd_t * pcd)
  97918. +{
  97919. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  97920. + depctl_data_t diepctl = {.d32 = 0 };
  97921. + gintmsk_data_t intr_mask = {.d32 = 0 };
  97922. + gintsts_data_t gintsts;
  97923. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  97924. + int i;
  97925. +
  97926. + DWC_DEBUGPL(DBG_PCD, "Global IN NAK Effective\n");
  97927. +
  97928. + /* Disable all active IN EPs */
  97929. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  97930. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  97931. + if (!(diepctl.b.eptype & 1) && diepctl.b.epena) {
  97932. + if (core_if->start_predict > 0)
  97933. + core_if->start_predict++;
  97934. + diepctl.b.epdis = 1;
  97935. + diepctl.b.snak = 1;
  97936. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, diepctl.d32);
  97937. + }
  97938. + }
  97939. +
  97940. +
  97941. + /* Disable the Global IN NAK Effective Interrupt */
  97942. + intr_mask.b.ginnakeff = 1;
  97943. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  97944. + intr_mask.d32, 0);
  97945. +
  97946. + /* Clear interrupt */
  97947. + gintsts.d32 = 0;
  97948. + gintsts.b.ginnakeff = 1;
  97949. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  97950. + gintsts.d32);
  97951. +
  97952. + return 1;
  97953. +}
  97954. +
  97955. +/**
  97956. + * OUT NAK Effective.
  97957. + *
  97958. + */
  97959. +int32_t dwc_otg_pcd_handle_out_nak_effective(dwc_otg_pcd_t * pcd)
  97960. +{
  97961. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  97962. + gintmsk_data_t intr_mask = {.d32 = 0 };
  97963. + gintsts_data_t gintsts;
  97964. + depctl_data_t doepctl;
  97965. + int i;
  97966. +
  97967. + /* Disable the Global OUT NAK Effective Interrupt */
  97968. + intr_mask.b.goutnakeff = 1;
  97969. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  97970. + intr_mask.d32, 0);
  97971. +
  97972. + /* If DEV OUT NAK enabled*/
  97973. + if (pcd->core_if->core_params->dev_out_nak) {
  97974. + /* Run over all out endpoints to determine the ep number on
  97975. + * which the timeout has happened
  97976. + */
  97977. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  97978. + if ( pcd->core_if->ep_xfer_info[i].state == 2 )
  97979. + break;
  97980. + }
  97981. + if (i > dev_if->num_out_eps) {
  97982. + dctl_data_t dctl;
  97983. + dctl.d32 =
  97984. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  97985. + dctl.b.cgoutnak = 1;
  97986. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl,
  97987. + dctl.d32);
  97988. + goto out;
  97989. + }
  97990. +
  97991. + /* Disable the endpoint */
  97992. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  97993. + if (doepctl.b.epena) {
  97994. + doepctl.b.epdis = 1;
  97995. + doepctl.b.snak = 1;
  97996. + }
  97997. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  97998. + return 1;
  97999. + }
  98000. + /* We come here from Incomplete ISO OUT handler */
  98001. + if (dev_if->isoc_ep) {
  98002. + dwc_ep_t *dwc_ep = (dwc_ep_t *)dev_if->isoc_ep;
  98003. + uint32_t epnum = dwc_ep->num;
  98004. + doepint_data_t doepint;
  98005. + doepint.d32 =
  98006. + DWC_READ_REG32(&dev_if->out_ep_regs[dwc_ep->num]->doepint);
  98007. + dev_if->isoc_ep = NULL;
  98008. + doepctl.d32 =
  98009. + DWC_READ_REG32(&dev_if->out_ep_regs[epnum]->doepctl);
  98010. + DWC_PRINTF("Before disable DOEPCTL = %08x\n", doepctl.d32);
  98011. + if (doepctl.b.epena) {
  98012. + doepctl.b.epdis = 1;
  98013. + doepctl.b.snak = 1;
  98014. + }
  98015. + DWC_WRITE_REG32(&dev_if->out_ep_regs[epnum]->doepctl,
  98016. + doepctl.d32);
  98017. + return 1;
  98018. + } else
  98019. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  98020. + "Global OUT NAK Effective\n");
  98021. +
  98022. +out:
  98023. + /* Clear interrupt */
  98024. + gintsts.d32 = 0;
  98025. + gintsts.b.goutnakeff = 1;
  98026. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  98027. + gintsts.d32);
  98028. +
  98029. + return 1;
  98030. +}
  98031. +
  98032. +/**
  98033. + * PCD interrupt handler.
  98034. + *
  98035. + * The PCD handles the device interrupts. Many conditions can cause a
  98036. + * device interrupt. When an interrupt occurs, the device interrupt
  98037. + * service routine determines the cause of the interrupt and
  98038. + * dispatches handling to the appropriate function. These interrupt
  98039. + * handling functions are described below.
  98040. + *
  98041. + * All interrupt registers are processed from LSB to MSB.
  98042. + *
  98043. + */
  98044. +int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd)
  98045. +{
  98046. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  98047. +#ifdef VERBOSE
  98048. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  98049. +#endif
  98050. + gintsts_data_t gintr_status;
  98051. + int32_t retval = 0;
  98052. +
  98053. + /* Exit from ISR if core is hibernated */
  98054. + if (core_if->hibernation_suspend == 1) {
  98055. + return retval;
  98056. + }
  98057. +#ifdef VERBOSE
  98058. + DWC_DEBUGPL(DBG_ANY, "%s() gintsts=%08x gintmsk=%08x\n",
  98059. + __func__,
  98060. + DWC_READ_REG32(&global_regs->gintsts),
  98061. + DWC_READ_REG32(&global_regs->gintmsk));
  98062. +#endif
  98063. +
  98064. + if (dwc_otg_is_device_mode(core_if)) {
  98065. + DWC_SPINLOCK(pcd->lock);
  98066. +#ifdef VERBOSE
  98067. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%08x gintmsk=%08x\n",
  98068. + __func__,
  98069. + DWC_READ_REG32(&global_regs->gintsts),
  98070. + DWC_READ_REG32(&global_regs->gintmsk));
  98071. +#endif
  98072. +
  98073. + gintr_status.d32 = dwc_otg_read_core_intr(core_if);
  98074. +
  98075. + DWC_DEBUGPL(DBG_PCDV, "%s: gintsts&gintmsk=%08x\n",
  98076. + __func__, gintr_status.d32);
  98077. +
  98078. + if (gintr_status.b.sofintr) {
  98079. + retval |= dwc_otg_pcd_handle_sof_intr(pcd);
  98080. + }
  98081. + if (gintr_status.b.rxstsqlvl) {
  98082. + retval |=
  98083. + dwc_otg_pcd_handle_rx_status_q_level_intr(pcd);
  98084. + }
  98085. + if (gintr_status.b.nptxfempty) {
  98086. + retval |= dwc_otg_pcd_handle_np_tx_fifo_empty_intr(pcd);
  98087. + }
  98088. + if (gintr_status.b.goutnakeff) {
  98089. + retval |= dwc_otg_pcd_handle_out_nak_effective(pcd);
  98090. + }
  98091. + if (gintr_status.b.i2cintr) {
  98092. + retval |= dwc_otg_pcd_handle_i2c_intr(pcd);
  98093. + }
  98094. + if (gintr_status.b.erlysuspend) {
  98095. + retval |= dwc_otg_pcd_handle_early_suspend_intr(pcd);
  98096. + }
  98097. + if (gintr_status.b.usbreset) {
  98098. + retval |= dwc_otg_pcd_handle_usb_reset_intr(pcd);
  98099. + }
  98100. + if (gintr_status.b.enumdone) {
  98101. + retval |= dwc_otg_pcd_handle_enum_done_intr(pcd);
  98102. + }
  98103. + if (gintr_status.b.isooutdrop) {
  98104. + retval |=
  98105. + dwc_otg_pcd_handle_isoc_out_packet_dropped_intr
  98106. + (pcd);
  98107. + }
  98108. + if (gintr_status.b.eopframe) {
  98109. + retval |=
  98110. + dwc_otg_pcd_handle_end_periodic_frame_intr(pcd);
  98111. + }
  98112. + if (gintr_status.b.inepint) {
  98113. + if (!core_if->multiproc_int_enable) {
  98114. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  98115. + }
  98116. + }
  98117. + if (gintr_status.b.outepintr) {
  98118. + if (!core_if->multiproc_int_enable) {
  98119. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  98120. + }
  98121. + }
  98122. + if (gintr_status.b.epmismatch) {
  98123. + retval |= dwc_otg_pcd_handle_ep_mismatch_intr(pcd);
  98124. + }
  98125. + if (gintr_status.b.fetsusp) {
  98126. + retval |= dwc_otg_pcd_handle_ep_fetsusp_intr(pcd);
  98127. + }
  98128. + if (gintr_status.b.ginnakeff) {
  98129. + retval |= dwc_otg_pcd_handle_in_nak_effective(pcd);
  98130. + }
  98131. + if (gintr_status.b.incomplisoin) {
  98132. + retval |=
  98133. + dwc_otg_pcd_handle_incomplete_isoc_in_intr(pcd);
  98134. + }
  98135. + if (gintr_status.b.incomplisoout) {
  98136. + retval |=
  98137. + dwc_otg_pcd_handle_incomplete_isoc_out_intr(pcd);
  98138. + }
  98139. +
  98140. + /* In MPI mode Device Endpoints interrupts are asserted
  98141. + * without setting outepintr and inepint bits set, so these
  98142. + * Interrupt handlers are called without checking these bit-fields
  98143. + */
  98144. + if (core_if->multiproc_int_enable) {
  98145. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  98146. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  98147. + }
  98148. +#ifdef VERBOSE
  98149. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%0x\n", __func__,
  98150. + DWC_READ_REG32(&global_regs->gintsts));
  98151. +#endif
  98152. + DWC_SPINUNLOCK(pcd->lock);
  98153. + }
  98154. + return retval;
  98155. +}
  98156. +
  98157. +#endif /* DWC_HOST_ONLY */
  98158. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
  98159. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c 1969-12-31 18:00:00.000000000 -0600
  98160. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c 2014-12-03 19:13:40.224418001 -0600
  98161. @@ -0,0 +1,1360 @@
  98162. + /* ==========================================================================
  98163. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_linux.c $
  98164. + * $Revision: #21 $
  98165. + * $Date: 2012/08/10 $
  98166. + * $Change: 2047372 $
  98167. + *
  98168. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  98169. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  98170. + * otherwise expressly agreed to in writing between Synopsys and you.
  98171. + *
  98172. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  98173. + * any End User Software License Agreement or Agreement for Licensed Product
  98174. + * with Synopsys or any supplement thereto. You are permitted to use and
  98175. + * redistribute this Software in source and binary forms, with or without
  98176. + * modification, provided that redistributions of source code must retain this
  98177. + * notice. You may not view, use, disclose, copy or distribute this file or
  98178. + * any information contained herein except pursuant to this license grant from
  98179. + * Synopsys. If you do not agree with this notice, including the disclaimer
  98180. + * below, then you are not authorized to use the Software.
  98181. + *
  98182. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  98183. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  98184. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  98185. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  98186. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  98187. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  98188. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  98189. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  98190. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  98191. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  98192. + * DAMAGE.
  98193. + * ========================================================================== */
  98194. +#ifndef DWC_HOST_ONLY
  98195. +
  98196. +/** @file
  98197. + * This file implements the Peripheral Controller Driver.
  98198. + *
  98199. + * The Peripheral Controller Driver (PCD) is responsible for
  98200. + * translating requests from the Function Driver into the appropriate
  98201. + * actions on the DWC_otg controller. It isolates the Function Driver
  98202. + * from the specifics of the controller by providing an API to the
  98203. + * Function Driver.
  98204. + *
  98205. + * The Peripheral Controller Driver for Linux will implement the
  98206. + * Gadget API, so that the existing Gadget drivers can be used.
  98207. + * (Gadget Driver is the Linux terminology for a Function Driver.)
  98208. + *
  98209. + * The Linux Gadget API is defined in the header file
  98210. + * <code><linux/usb_gadget.h></code>. The USB EP operations API is
  98211. + * defined in the structure <code>usb_ep_ops</code> and the USB
  98212. + * Controller API is defined in the structure
  98213. + * <code>usb_gadget_ops</code>.
  98214. + *
  98215. + */
  98216. +
  98217. +#include "dwc_otg_os_dep.h"
  98218. +#include "dwc_otg_pcd_if.h"
  98219. +#include "dwc_otg_pcd.h"
  98220. +#include "dwc_otg_driver.h"
  98221. +#include "dwc_otg_dbg.h"
  98222. +
  98223. +extern bool fiq_enable;
  98224. +
  98225. +static struct gadget_wrapper {
  98226. + dwc_otg_pcd_t *pcd;
  98227. +
  98228. + struct usb_gadget gadget;
  98229. + struct usb_gadget_driver *driver;
  98230. +
  98231. + struct usb_ep ep0;
  98232. + struct usb_ep in_ep[16];
  98233. + struct usb_ep out_ep[16];
  98234. +
  98235. +} *gadget_wrapper;
  98236. +
  98237. +/* Display the contents of the buffer */
  98238. +extern void dump_msg(const u8 * buf, unsigned int length);
  98239. +/**
  98240. + * Get the dwc_otg_pcd_ep_t* from usb_ep* pointer - NULL in case
  98241. + * if the endpoint is not found
  98242. + */
  98243. +static struct dwc_otg_pcd_ep *ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  98244. +{
  98245. + int i;
  98246. + if (pcd->ep0.priv == handle) {
  98247. + return &pcd->ep0;
  98248. + }
  98249. +
  98250. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  98251. + if (pcd->in_ep[i].priv == handle)
  98252. + return &pcd->in_ep[i];
  98253. + if (pcd->out_ep[i].priv == handle)
  98254. + return &pcd->out_ep[i];
  98255. + }
  98256. +
  98257. + return NULL;
  98258. +}
  98259. +
  98260. +/* USB Endpoint Operations */
  98261. +/*
  98262. + * The following sections briefly describe the behavior of the Gadget
  98263. + * API endpoint operations implemented in the DWC_otg driver
  98264. + * software. Detailed descriptions of the generic behavior of each of
  98265. + * these functions can be found in the Linux header file
  98266. + * include/linux/usb_gadget.h.
  98267. + *
  98268. + * The Gadget API provides wrapper functions for each of the function
  98269. + * pointers defined in usb_ep_ops. The Gadget Driver calls the wrapper
  98270. + * function, which then calls the underlying PCD function. The
  98271. + * following sections are named according to the wrapper
  98272. + * functions. Within each section, the corresponding DWC_otg PCD
  98273. + * function name is specified.
  98274. + *
  98275. + */
  98276. +
  98277. +/**
  98278. + * This function is called by the Gadget Driver for each EP to be
  98279. + * configured for the current configuration (SET_CONFIGURATION).
  98280. + *
  98281. + * This function initializes the dwc_otg_ep_t data structure, and then
  98282. + * calls dwc_otg_ep_activate.
  98283. + */
  98284. +static int ep_enable(struct usb_ep *usb_ep,
  98285. + const struct usb_endpoint_descriptor *ep_desc)
  98286. +{
  98287. + int retval;
  98288. +
  98289. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, ep_desc);
  98290. +
  98291. + if (!usb_ep || !ep_desc || ep_desc->bDescriptorType != USB_DT_ENDPOINT) {
  98292. + DWC_WARN("%s, bad ep or descriptor\n", __func__);
  98293. + return -EINVAL;
  98294. + }
  98295. + if (usb_ep == &gadget_wrapper->ep0) {
  98296. + DWC_WARN("%s, bad ep(0)\n", __func__);
  98297. + return -EINVAL;
  98298. + }
  98299. +
  98300. + /* Check FIFO size? */
  98301. + if (!ep_desc->wMaxPacketSize) {
  98302. + DWC_WARN("%s, bad %s maxpacket\n", __func__, usb_ep->name);
  98303. + return -ERANGE;
  98304. + }
  98305. +
  98306. + if (!gadget_wrapper->driver ||
  98307. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  98308. + DWC_WARN("%s, bogus device state\n", __func__);
  98309. + return -ESHUTDOWN;
  98310. + }
  98311. +
  98312. + /* Delete after check - MAS */
  98313. +#if 0
  98314. + nat = (uint32_t) ep_desc->wMaxPacketSize;
  98315. + printk(KERN_ALERT "%s: nat (before) =%d\n", __func__, nat);
  98316. + nat = (nat >> 11) & 0x03;
  98317. + printk(KERN_ALERT "%s: nat (after) =%d\n", __func__, nat);
  98318. +#endif
  98319. + retval = dwc_otg_pcd_ep_enable(gadget_wrapper->pcd,
  98320. + (const uint8_t *)ep_desc,
  98321. + (void *)usb_ep);
  98322. + if (retval) {
  98323. + DWC_WARN("dwc_otg_pcd_ep_enable failed\n");
  98324. + return -EINVAL;
  98325. + }
  98326. +
  98327. + usb_ep->maxpacket = le16_to_cpu(ep_desc->wMaxPacketSize);
  98328. +
  98329. + return 0;
  98330. +}
  98331. +
  98332. +/**
  98333. + * This function is called when an EP is disabled due to disconnect or
  98334. + * change in configuration. Any pending requests will terminate with a
  98335. + * status of -ESHUTDOWN.
  98336. + *
  98337. + * This function modifies the dwc_otg_ep_t data structure for this EP,
  98338. + * and then calls dwc_otg_ep_deactivate.
  98339. + */
  98340. +static int ep_disable(struct usb_ep *usb_ep)
  98341. +{
  98342. + int retval;
  98343. +
  98344. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, usb_ep);
  98345. + if (!usb_ep) {
  98346. + DWC_DEBUGPL(DBG_PCD, "%s, %s not enabled\n", __func__,
  98347. + usb_ep ? usb_ep->name : NULL);
  98348. + return -EINVAL;
  98349. + }
  98350. +
  98351. + retval = dwc_otg_pcd_ep_disable(gadget_wrapper->pcd, usb_ep);
  98352. + if (retval) {
  98353. + retval = -EINVAL;
  98354. + }
  98355. +
  98356. + return retval;
  98357. +}
  98358. +
  98359. +/**
  98360. + * This function allocates a request object to use with the specified
  98361. + * endpoint.
  98362. + *
  98363. + * @param ep The endpoint to be used with with the request
  98364. + * @param gfp_flags the GFP_* flags to use.
  98365. + */
  98366. +static struct usb_request *dwc_otg_pcd_alloc_request(struct usb_ep *ep,
  98367. + gfp_t gfp_flags)
  98368. +{
  98369. + struct usb_request *usb_req;
  98370. +
  98371. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d)\n", __func__, ep, gfp_flags);
  98372. + if (0 == ep) {
  98373. + DWC_WARN("%s() %s\n", __func__, "Invalid EP!\n");
  98374. + return 0;
  98375. + }
  98376. + usb_req = kmalloc(sizeof(*usb_req), gfp_flags);
  98377. + if (0 == usb_req) {
  98378. + DWC_WARN("%s() %s\n", __func__, "request allocation failed!\n");
  98379. + return 0;
  98380. + }
  98381. + memset(usb_req, 0, sizeof(*usb_req));
  98382. + usb_req->dma = DWC_DMA_ADDR_INVALID;
  98383. +
  98384. + return usb_req;
  98385. +}
  98386. +
  98387. +/**
  98388. + * This function frees a request object.
  98389. + *
  98390. + * @param ep The endpoint associated with the request
  98391. + * @param req The request being freed
  98392. + */
  98393. +static void dwc_otg_pcd_free_request(struct usb_ep *ep, struct usb_request *req)
  98394. +{
  98395. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, ep, req);
  98396. +
  98397. + if (0 == ep || 0 == req) {
  98398. + DWC_WARN("%s() %s\n", __func__,
  98399. + "Invalid ep or req argument!\n");
  98400. + return;
  98401. + }
  98402. +
  98403. + kfree(req);
  98404. +}
  98405. +
  98406. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  98407. +/**
  98408. + * This function allocates an I/O buffer to be used for a transfer
  98409. + * to/from the specified endpoint.
  98410. + *
  98411. + * @param usb_ep The endpoint to be used with with the request
  98412. + * @param bytes The desired number of bytes for the buffer
  98413. + * @param dma Pointer to the buffer's DMA address; must be valid
  98414. + * @param gfp_flags the GFP_* flags to use.
  98415. + * @return address of a new buffer or null is buffer could not be allocated.
  98416. + */
  98417. +static void *dwc_otg_pcd_alloc_buffer(struct usb_ep *usb_ep, unsigned bytes,
  98418. + dma_addr_t * dma, gfp_t gfp_flags)
  98419. +{
  98420. + void *buf;
  98421. + dwc_otg_pcd_t *pcd = 0;
  98422. +
  98423. + pcd = gadget_wrapper->pcd;
  98424. +
  98425. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d,%p,%0x)\n", __func__, usb_ep, bytes,
  98426. + dma, gfp_flags);
  98427. +
  98428. + /* Check dword alignment */
  98429. + if ((bytes & 0x3UL) != 0) {
  98430. + DWC_WARN("%s() Buffer size is not a multiple of"
  98431. + "DWORD size (%d)", __func__, bytes);
  98432. + }
  98433. +
  98434. + buf = dma_alloc_coherent(NULL, bytes, dma, gfp_flags);
  98435. +
  98436. + /* Check dword alignment */
  98437. + if (((int)buf & 0x3UL) != 0) {
  98438. + DWC_WARN("%s() Buffer is not DWORD aligned (%p)",
  98439. + __func__, buf);
  98440. + }
  98441. +
  98442. + return buf;
  98443. +}
  98444. +
  98445. +/**
  98446. + * This function frees an I/O buffer that was allocated by alloc_buffer.
  98447. + *
  98448. + * @param usb_ep the endpoint associated with the buffer
  98449. + * @param buf address of the buffer
  98450. + * @param dma The buffer's DMA address
  98451. + * @param bytes The number of bytes of the buffer
  98452. + */
  98453. +static void dwc_otg_pcd_free_buffer(struct usb_ep *usb_ep, void *buf,
  98454. + dma_addr_t dma, unsigned bytes)
  98455. +{
  98456. + dwc_otg_pcd_t *pcd = 0;
  98457. +
  98458. + pcd = gadget_wrapper->pcd;
  98459. +
  98460. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%0x,%d)\n", __func__, buf, dma, bytes);
  98461. +
  98462. + dma_free_coherent(NULL, bytes, buf, dma);
  98463. +}
  98464. +#endif
  98465. +
  98466. +/**
  98467. + * This function is used to submit an I/O Request to an EP.
  98468. + *
  98469. + * - When the request completes the request's completion callback
  98470. + * is called to return the request to the driver.
  98471. + * - An EP, except control EPs, may have multiple requests
  98472. + * pending.
  98473. + * - Once submitted the request cannot be examined or modified.
  98474. + * - Each request is turned into one or more packets.
  98475. + * - A BULK EP can queue any amount of data; the transfer is
  98476. + * packetized.
  98477. + * - Zero length Packets are specified with the request 'zero'
  98478. + * flag.
  98479. + */
  98480. +static int ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
  98481. + gfp_t gfp_flags)
  98482. +{
  98483. + dwc_otg_pcd_t *pcd;
  98484. + struct dwc_otg_pcd_ep *ep = NULL;
  98485. + int retval = 0, is_isoc_ep = 0;
  98486. + dma_addr_t dma_addr = DWC_DMA_ADDR_INVALID;
  98487. +
  98488. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p,%d)\n",
  98489. + __func__, usb_ep, usb_req, gfp_flags);
  98490. +
  98491. + if (!usb_req || !usb_req->complete || !usb_req->buf) {
  98492. + DWC_WARN("bad params\n");
  98493. + return -EINVAL;
  98494. + }
  98495. +
  98496. + if (!usb_ep) {
  98497. + DWC_WARN("bad ep\n");
  98498. + return -EINVAL;
  98499. + }
  98500. +
  98501. + pcd = gadget_wrapper->pcd;
  98502. + if (!gadget_wrapper->driver ||
  98503. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  98504. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  98505. + gadget_wrapper->gadget.speed);
  98506. + DWC_WARN("bogus device state\n");
  98507. + return -ESHUTDOWN;
  98508. + }
  98509. +
  98510. + DWC_DEBUGPL(DBG_PCD, "%s queue req %p, len %d buf %p\n",
  98511. + usb_ep->name, usb_req, usb_req->length, usb_req->buf);
  98512. +
  98513. + usb_req->status = -EINPROGRESS;
  98514. + usb_req->actual = 0;
  98515. +
  98516. + ep = ep_from_handle(pcd, usb_ep);
  98517. + if (ep == NULL)
  98518. + is_isoc_ep = 0;
  98519. + else
  98520. + is_isoc_ep = (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) ? 1 : 0;
  98521. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  98522. + dma_addr = usb_req->dma;
  98523. +#else
  98524. + if (GET_CORE_IF(pcd)->dma_enable) {
  98525. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  98526. + struct device *dev = NULL;
  98527. +
  98528. + if (otg_dev != NULL)
  98529. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  98530. +
  98531. + if (usb_req->length != 0 &&
  98532. + usb_req->dma == DWC_DMA_ADDR_INVALID) {
  98533. + dma_addr = dma_map_single(dev, usb_req->buf,
  98534. + usb_req->length,
  98535. + ep->dwc_ep.is_in ?
  98536. + DMA_TO_DEVICE:
  98537. + DMA_FROM_DEVICE);
  98538. + }
  98539. + }
  98540. +#endif
  98541. +
  98542. +#ifdef DWC_UTE_PER_IO
  98543. + if (is_isoc_ep == 1) {
  98544. + retval = dwc_otg_pcd_xiso_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  98545. + usb_req->length, usb_req->zero, usb_req,
  98546. + gfp_flags == GFP_ATOMIC ? 1 : 0, &usb_req->ext_req);
  98547. + if (retval)
  98548. + return -EINVAL;
  98549. +
  98550. + return 0;
  98551. + }
  98552. +#endif
  98553. + retval = dwc_otg_pcd_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  98554. + usb_req->length, usb_req->zero, usb_req,
  98555. + gfp_flags == GFP_ATOMIC ? 1 : 0);
  98556. + if (retval) {
  98557. + return -EINVAL;
  98558. + }
  98559. +
  98560. + return 0;
  98561. +}
  98562. +
  98563. +/**
  98564. + * This function cancels an I/O request from an EP.
  98565. + */
  98566. +static int ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
  98567. +{
  98568. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, usb_req);
  98569. +
  98570. + if (!usb_ep || !usb_req) {
  98571. + DWC_WARN("bad argument\n");
  98572. + return -EINVAL;
  98573. + }
  98574. + if (!gadget_wrapper->driver ||
  98575. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  98576. + DWC_WARN("bogus device state\n");
  98577. + return -ESHUTDOWN;
  98578. + }
  98579. + if (dwc_otg_pcd_ep_dequeue(gadget_wrapper->pcd, usb_ep, usb_req)) {
  98580. + return -EINVAL;
  98581. + }
  98582. +
  98583. + return 0;
  98584. +}
  98585. +
  98586. +/**
  98587. + * usb_ep_set_halt stalls an endpoint.
  98588. + *
  98589. + * usb_ep_clear_halt clears an endpoint halt and resets its data
  98590. + * toggle.
  98591. + *
  98592. + * Both of these functions are implemented with the same underlying
  98593. + * function. The behavior depends on the value argument.
  98594. + *
  98595. + * @param[in] usb_ep the Endpoint to halt or clear halt.
  98596. + * @param[in] value
  98597. + * - 0 means clear_halt.
  98598. + * - 1 means set_halt,
  98599. + * - 2 means clear stall lock flag.
  98600. + * - 3 means set stall lock flag.
  98601. + */
  98602. +static int ep_halt(struct usb_ep *usb_ep, int value)
  98603. +{
  98604. + int retval = 0;
  98605. +
  98606. + DWC_DEBUGPL(DBG_PCD, "HALT %s %d\n", usb_ep->name, value);
  98607. +
  98608. + if (!usb_ep) {
  98609. + DWC_WARN("bad ep\n");
  98610. + return -EINVAL;
  98611. + }
  98612. +
  98613. + retval = dwc_otg_pcd_ep_halt(gadget_wrapper->pcd, usb_ep, value);
  98614. + if (retval == -DWC_E_AGAIN) {
  98615. + return -EAGAIN;
  98616. + } else if (retval) {
  98617. + retval = -EINVAL;
  98618. + }
  98619. +
  98620. + return retval;
  98621. +}
  98622. +
  98623. +//#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  98624. +#if 0
  98625. +/**
  98626. + * ep_wedge: sets the halt feature and ignores clear requests
  98627. + *
  98628. + * @usb_ep: the endpoint being wedged
  98629. + *
  98630. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  98631. + * requests. If the gadget driver clears the halt status, it will
  98632. + * automatically unwedge the endpoint.
  98633. + *
  98634. + * Returns zero on success, else negative errno. *
  98635. + * Check usb_ep_set_wedge() at "usb_gadget.h" for details
  98636. + */
  98637. +static int ep_wedge(struct usb_ep *usb_ep)
  98638. +{
  98639. + int retval = 0;
  98640. +
  98641. + DWC_DEBUGPL(DBG_PCD, "WEDGE %s\n", usb_ep->name);
  98642. +
  98643. + if (!usb_ep) {
  98644. + DWC_WARN("bad ep\n");
  98645. + return -EINVAL;
  98646. + }
  98647. +
  98648. + retval = dwc_otg_pcd_ep_wedge(gadget_wrapper->pcd, usb_ep);
  98649. + if (retval == -DWC_E_AGAIN) {
  98650. + retval = -EAGAIN;
  98651. + } else if (retval) {
  98652. + retval = -EINVAL;
  98653. + }
  98654. +
  98655. + return retval;
  98656. +}
  98657. +#endif
  98658. +
  98659. +#ifdef DWC_EN_ISOC
  98660. +/**
  98661. + * This function is used to submit an ISOC Transfer Request to an EP.
  98662. + *
  98663. + * - Every time a sync period completes the request's completion callback
  98664. + * is called to provide data to the gadget driver.
  98665. + * - Once submitted the request cannot be modified.
  98666. + * - Each request is turned into periodic data packets untill ISO
  98667. + * Transfer is stopped..
  98668. + */
  98669. +static int iso_ep_start(struct usb_ep *usb_ep, struct usb_iso_request *req,
  98670. + gfp_t gfp_flags)
  98671. +{
  98672. + int retval = 0;
  98673. +
  98674. + if (!req || !req->process_buffer || !req->buf0 || !req->buf1) {
  98675. + DWC_WARN("bad params\n");
  98676. + return -EINVAL;
  98677. + }
  98678. +
  98679. + if (!usb_ep) {
  98680. + DWC_PRINTF("bad params\n");
  98681. + return -EINVAL;
  98682. + }
  98683. +
  98684. + req->status = -EINPROGRESS;
  98685. +
  98686. + retval =
  98687. + dwc_otg_pcd_iso_ep_start(gadget_wrapper->pcd, usb_ep, req->buf0,
  98688. + req->buf1, req->dma0, req->dma1,
  98689. + req->sync_frame, req->data_pattern_frame,
  98690. + req->data_per_frame,
  98691. + req->
  98692. + flags & USB_REQ_ISO_ASAP ? -1 :
  98693. + req->start_frame, req->buf_proc_intrvl,
  98694. + req, gfp_flags == GFP_ATOMIC ? 1 : 0);
  98695. +
  98696. + if (retval) {
  98697. + return -EINVAL;
  98698. + }
  98699. +
  98700. + return retval;
  98701. +}
  98702. +
  98703. +/**
  98704. + * This function stops ISO EP Periodic Data Transfer.
  98705. + */
  98706. +static int iso_ep_stop(struct usb_ep *usb_ep, struct usb_iso_request *req)
  98707. +{
  98708. + int retval = 0;
  98709. + if (!usb_ep) {
  98710. + DWC_WARN("bad ep\n");
  98711. + }
  98712. +
  98713. + if (!gadget_wrapper->driver ||
  98714. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  98715. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  98716. + gadget_wrapper->gadget.speed);
  98717. + DWC_WARN("bogus device state\n");
  98718. + }
  98719. +
  98720. + dwc_otg_pcd_iso_ep_stop(gadget_wrapper->pcd, usb_ep, req);
  98721. + if (retval) {
  98722. + retval = -EINVAL;
  98723. + }
  98724. +
  98725. + return retval;
  98726. +}
  98727. +
  98728. +static struct usb_iso_request *alloc_iso_request(struct usb_ep *ep,
  98729. + int packets, gfp_t gfp_flags)
  98730. +{
  98731. + struct usb_iso_request *pReq = NULL;
  98732. + uint32_t req_size;
  98733. +
  98734. + req_size = sizeof(struct usb_iso_request);
  98735. + req_size +=
  98736. + (2 * packets * (sizeof(struct usb_gadget_iso_packet_descriptor)));
  98737. +
  98738. + pReq = kmalloc(req_size, gfp_flags);
  98739. + if (!pReq) {
  98740. + DWC_WARN("Can't allocate Iso Request\n");
  98741. + return 0;
  98742. + }
  98743. + pReq->iso_packet_desc0 = (void *)(pReq + 1);
  98744. +
  98745. + pReq->iso_packet_desc1 = pReq->iso_packet_desc0 + packets;
  98746. +
  98747. + return pReq;
  98748. +}
  98749. +
  98750. +static void free_iso_request(struct usb_ep *ep, struct usb_iso_request *req)
  98751. +{
  98752. + kfree(req);
  98753. +}
  98754. +
  98755. +static struct usb_isoc_ep_ops dwc_otg_pcd_ep_ops = {
  98756. + .ep_ops = {
  98757. + .enable = ep_enable,
  98758. + .disable = ep_disable,
  98759. +
  98760. + .alloc_request = dwc_otg_pcd_alloc_request,
  98761. + .free_request = dwc_otg_pcd_free_request,
  98762. +
  98763. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  98764. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  98765. + .free_buffer = dwc_otg_pcd_free_buffer,
  98766. +#endif
  98767. +
  98768. + .queue = ep_queue,
  98769. + .dequeue = ep_dequeue,
  98770. +
  98771. + .set_halt = ep_halt,
  98772. + .fifo_status = 0,
  98773. + .fifo_flush = 0,
  98774. + },
  98775. + .iso_ep_start = iso_ep_start,
  98776. + .iso_ep_stop = iso_ep_stop,
  98777. + .alloc_iso_request = alloc_iso_request,
  98778. + .free_iso_request = free_iso_request,
  98779. +};
  98780. +
  98781. +#else
  98782. +
  98783. + int (*enable) (struct usb_ep *ep,
  98784. + const struct usb_endpoint_descriptor *desc);
  98785. + int (*disable) (struct usb_ep *ep);
  98786. +
  98787. + struct usb_request *(*alloc_request) (struct usb_ep *ep,
  98788. + gfp_t gfp_flags);
  98789. + void (*free_request) (struct usb_ep *ep, struct usb_request *req);
  98790. +
  98791. + int (*queue) (struct usb_ep *ep, struct usb_request *req,
  98792. + gfp_t gfp_flags);
  98793. + int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
  98794. +
  98795. + int (*set_halt) (struct usb_ep *ep, int value);
  98796. + int (*set_wedge) (struct usb_ep *ep);
  98797. +
  98798. + int (*fifo_status) (struct usb_ep *ep);
  98799. + void (*fifo_flush) (struct usb_ep *ep);
  98800. +static struct usb_ep_ops dwc_otg_pcd_ep_ops = {
  98801. + .enable = ep_enable,
  98802. + .disable = ep_disable,
  98803. +
  98804. + .alloc_request = dwc_otg_pcd_alloc_request,
  98805. + .free_request = dwc_otg_pcd_free_request,
  98806. +
  98807. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  98808. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  98809. + .free_buffer = dwc_otg_pcd_free_buffer,
  98810. +#else
  98811. + /* .set_wedge = ep_wedge, */
  98812. + .set_wedge = NULL, /* uses set_halt instead */
  98813. +#endif
  98814. +
  98815. + .queue = ep_queue,
  98816. + .dequeue = ep_dequeue,
  98817. +
  98818. + .set_halt = ep_halt,
  98819. + .fifo_status = 0,
  98820. + .fifo_flush = 0,
  98821. +
  98822. +};
  98823. +
  98824. +#endif /* _EN_ISOC_ */
  98825. +/* Gadget Operations */
  98826. +/**
  98827. + * The following gadget operations will be implemented in the DWC_otg
  98828. + * PCD. Functions in the API that are not described below are not
  98829. + * implemented.
  98830. + *
  98831. + * The Gadget API provides wrapper functions for each of the function
  98832. + * pointers defined in usb_gadget_ops. The Gadget Driver calls the
  98833. + * wrapper function, which then calls the underlying PCD function. The
  98834. + * following sections are named according to the wrapper functions
  98835. + * (except for ioctl, which doesn't have a wrapper function). Within
  98836. + * each section, the corresponding DWC_otg PCD function name is
  98837. + * specified.
  98838. + *
  98839. + */
  98840. +
  98841. +/**
  98842. + *Gets the USB Frame number of the last SOF.
  98843. + */
  98844. +static int get_frame_number(struct usb_gadget *gadget)
  98845. +{
  98846. + struct gadget_wrapper *d;
  98847. +
  98848. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  98849. +
  98850. + if (gadget == 0) {
  98851. + return -ENODEV;
  98852. + }
  98853. +
  98854. + d = container_of(gadget, struct gadget_wrapper, gadget);
  98855. + return dwc_otg_pcd_get_frame_number(d->pcd);
  98856. +}
  98857. +
  98858. +#ifdef CONFIG_USB_DWC_OTG_LPM
  98859. +static int test_lpm_enabled(struct usb_gadget *gadget)
  98860. +{
  98861. + struct gadget_wrapper *d;
  98862. +
  98863. + d = container_of(gadget, struct gadget_wrapper, gadget);
  98864. +
  98865. + return dwc_otg_pcd_is_lpm_enabled(d->pcd);
  98866. +}
  98867. +#endif
  98868. +
  98869. +/**
  98870. + * Initiates Session Request Protocol (SRP) to wakeup the host if no
  98871. + * session is in progress. If a session is already in progress, but
  98872. + * the device is suspended, remote wakeup signaling is started.
  98873. + *
  98874. + */
  98875. +static int wakeup(struct usb_gadget *gadget)
  98876. +{
  98877. + struct gadget_wrapper *d;
  98878. +
  98879. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  98880. +
  98881. + if (gadget == 0) {
  98882. + return -ENODEV;
  98883. + } else {
  98884. + d = container_of(gadget, struct gadget_wrapper, gadget);
  98885. + }
  98886. + dwc_otg_pcd_wakeup(d->pcd);
  98887. + return 0;
  98888. +}
  98889. +
  98890. +static const struct usb_gadget_ops dwc_otg_pcd_ops = {
  98891. + .get_frame = get_frame_number,
  98892. + .wakeup = wakeup,
  98893. +#ifdef CONFIG_USB_DWC_OTG_LPM
  98894. + .lpm_support = test_lpm_enabled,
  98895. +#endif
  98896. + // current versions must always be self-powered
  98897. +};
  98898. +
  98899. +static int _setup(dwc_otg_pcd_t * pcd, uint8_t * bytes)
  98900. +{
  98901. + int retval = -DWC_E_NOT_SUPPORTED;
  98902. + if (gadget_wrapper->driver && gadget_wrapper->driver->setup) {
  98903. + retval = gadget_wrapper->driver->setup(&gadget_wrapper->gadget,
  98904. + (struct usb_ctrlrequest
  98905. + *)bytes);
  98906. + }
  98907. +
  98908. + if (retval == -ENOTSUPP) {
  98909. + retval = -DWC_E_NOT_SUPPORTED;
  98910. + } else if (retval < 0) {
  98911. + retval = -DWC_E_INVALID;
  98912. + }
  98913. +
  98914. + return retval;
  98915. +}
  98916. +
  98917. +#ifdef DWC_EN_ISOC
  98918. +static int _isoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  98919. + void *req_handle, int proc_buf_num)
  98920. +{
  98921. + int i, packet_count;
  98922. + struct usb_gadget_iso_packet_descriptor *iso_packet = 0;
  98923. + struct usb_iso_request *iso_req = req_handle;
  98924. +
  98925. + if (proc_buf_num) {
  98926. + iso_packet = iso_req->iso_packet_desc1;
  98927. + } else {
  98928. + iso_packet = iso_req->iso_packet_desc0;
  98929. + }
  98930. + packet_count =
  98931. + dwc_otg_pcd_get_iso_packet_count(pcd, ep_handle, req_handle);
  98932. + for (i = 0; i < packet_count; ++i) {
  98933. + int status;
  98934. + int actual;
  98935. + int offset;
  98936. + dwc_otg_pcd_get_iso_packet_params(pcd, ep_handle, req_handle,
  98937. + i, &status, &actual, &offset);
  98938. + switch (status) {
  98939. + case -DWC_E_NO_DATA:
  98940. + status = -ENODATA;
  98941. + break;
  98942. + default:
  98943. + if (status) {
  98944. + DWC_PRINTF("unknown status in isoc packet\n");
  98945. + }
  98946. +
  98947. + }
  98948. + iso_packet[i].status = status;
  98949. + iso_packet[i].offset = offset;
  98950. + iso_packet[i].actual_length = actual;
  98951. + }
  98952. +
  98953. + iso_req->status = 0;
  98954. + iso_req->process_buffer(ep_handle, iso_req);
  98955. +
  98956. + return 0;
  98957. +}
  98958. +#endif /* DWC_EN_ISOC */
  98959. +
  98960. +#ifdef DWC_UTE_PER_IO
  98961. +/**
  98962. + * Copy the contents of the extended request to the Linux usb_request's
  98963. + * extended part and call the gadget's completion.
  98964. + *
  98965. + * @param pcd Pointer to the pcd structure
  98966. + * @param ep_handle Void pointer to the usb_ep structure
  98967. + * @param req_handle Void pointer to the usb_request structure
  98968. + * @param status Request status returned from the portable logic
  98969. + * @param ereq_port Void pointer to the extended request structure
  98970. + * created in the the portable part that contains the
  98971. + * results of the processed iso packets.
  98972. + */
  98973. +static int _xisoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  98974. + void *req_handle, int32_t status, void *ereq_port)
  98975. +{
  98976. + struct dwc_ute_iso_req_ext *ereqorg = NULL;
  98977. + struct dwc_iso_xreq_port *ereqport = NULL;
  98978. + struct dwc_ute_iso_packet_descriptor *desc_org = NULL;
  98979. + int i;
  98980. + struct usb_request *req;
  98981. + //struct dwc_ute_iso_packet_descriptor *
  98982. + //int status = 0;
  98983. +
  98984. + req = (struct usb_request *)req_handle;
  98985. + ereqorg = &req->ext_req;
  98986. + ereqport = (struct dwc_iso_xreq_port *)ereq_port;
  98987. + desc_org = ereqorg->per_io_frame_descs;
  98988. +
  98989. + if (req && req->complete) {
  98990. + /* Copy the request data from the portable logic to our request */
  98991. + for (i = 0; i < ereqport->pio_pkt_count; i++) {
  98992. + desc_org[i].actual_length =
  98993. + ereqport->per_io_frame_descs[i].actual_length;
  98994. + desc_org[i].status =
  98995. + ereqport->per_io_frame_descs[i].status;
  98996. + }
  98997. +
  98998. + switch (status) {
  98999. + case -DWC_E_SHUTDOWN:
  99000. + req->status = -ESHUTDOWN;
  99001. + break;
  99002. + case -DWC_E_RESTART:
  99003. + req->status = -ECONNRESET;
  99004. + break;
  99005. + case -DWC_E_INVALID:
  99006. + req->status = -EINVAL;
  99007. + break;
  99008. + case -DWC_E_TIMEOUT:
  99009. + req->status = -ETIMEDOUT;
  99010. + break;
  99011. + default:
  99012. + req->status = status;
  99013. + }
  99014. +
  99015. + /* And call the gadget's completion */
  99016. + req->complete(ep_handle, req);
  99017. + }
  99018. +
  99019. + return 0;
  99020. +}
  99021. +#endif /* DWC_UTE_PER_IO */
  99022. +
  99023. +static int _complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  99024. + void *req_handle, int32_t status, uint32_t actual)
  99025. +{
  99026. + struct usb_request *req = (struct usb_request *)req_handle;
  99027. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  99028. + struct dwc_otg_pcd_ep *ep = NULL;
  99029. +#endif
  99030. +
  99031. + if (req && req->complete) {
  99032. + switch (status) {
  99033. + case -DWC_E_SHUTDOWN:
  99034. + req->status = -ESHUTDOWN;
  99035. + break;
  99036. + case -DWC_E_RESTART:
  99037. + req->status = -ECONNRESET;
  99038. + break;
  99039. + case -DWC_E_INVALID:
  99040. + req->status = -EINVAL;
  99041. + break;
  99042. + case -DWC_E_TIMEOUT:
  99043. + req->status = -ETIMEDOUT;
  99044. + break;
  99045. + default:
  99046. + req->status = status;
  99047. +
  99048. + }
  99049. +
  99050. + req->actual = actual;
  99051. + DWC_SPINUNLOCK(pcd->lock);
  99052. + req->complete(ep_handle, req);
  99053. + DWC_SPINLOCK(pcd->lock);
  99054. + }
  99055. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  99056. + ep = ep_from_handle(pcd, ep_handle);
  99057. + if (GET_CORE_IF(pcd)->dma_enable) {
  99058. + if (req->length != 0) {
  99059. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  99060. + struct device *dev = NULL;
  99061. +
  99062. + if (otg_dev != NULL)
  99063. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  99064. +
  99065. + dma_unmap_single(dev, req->dma, req->length,
  99066. + ep->dwc_ep.is_in ?
  99067. + DMA_TO_DEVICE: DMA_FROM_DEVICE);
  99068. + }
  99069. + }
  99070. +#endif
  99071. +
  99072. + return 0;
  99073. +}
  99074. +
  99075. +static int _connect(dwc_otg_pcd_t * pcd, int speed)
  99076. +{
  99077. + gadget_wrapper->gadget.speed = speed;
  99078. + return 0;
  99079. +}
  99080. +
  99081. +static int _disconnect(dwc_otg_pcd_t * pcd)
  99082. +{
  99083. + if (gadget_wrapper->driver && gadget_wrapper->driver->disconnect) {
  99084. + gadget_wrapper->driver->disconnect(&gadget_wrapper->gadget);
  99085. + }
  99086. + return 0;
  99087. +}
  99088. +
  99089. +static int _resume(dwc_otg_pcd_t * pcd)
  99090. +{
  99091. + if (gadget_wrapper->driver && gadget_wrapper->driver->resume) {
  99092. + gadget_wrapper->driver->resume(&gadget_wrapper->gadget);
  99093. + }
  99094. +
  99095. + return 0;
  99096. +}
  99097. +
  99098. +static int _suspend(dwc_otg_pcd_t * pcd)
  99099. +{
  99100. + if (gadget_wrapper->driver && gadget_wrapper->driver->suspend) {
  99101. + gadget_wrapper->driver->suspend(&gadget_wrapper->gadget);
  99102. + }
  99103. + return 0;
  99104. +}
  99105. +
  99106. +/**
  99107. + * This function updates the otg values in the gadget structure.
  99108. + */
  99109. +static int _hnp_changed(dwc_otg_pcd_t * pcd)
  99110. +{
  99111. +
  99112. + if (!gadget_wrapper->gadget.is_otg)
  99113. + return 0;
  99114. +
  99115. + gadget_wrapper->gadget.b_hnp_enable = get_b_hnp_enable(pcd);
  99116. + gadget_wrapper->gadget.a_hnp_support = get_a_hnp_support(pcd);
  99117. + gadget_wrapper->gadget.a_alt_hnp_support = get_a_alt_hnp_support(pcd);
  99118. + return 0;
  99119. +}
  99120. +
  99121. +static int _reset(dwc_otg_pcd_t * pcd)
  99122. +{
  99123. + return 0;
  99124. +}
  99125. +
  99126. +#ifdef DWC_UTE_CFI
  99127. +static int _cfi_setup(dwc_otg_pcd_t * pcd, void *cfi_req)
  99128. +{
  99129. + int retval = -DWC_E_INVALID;
  99130. + if (gadget_wrapper->driver->cfi_feature_setup) {
  99131. + retval =
  99132. + gadget_wrapper->driver->
  99133. + cfi_feature_setup(&gadget_wrapper->gadget,
  99134. + (struct cfi_usb_ctrlrequest *)cfi_req);
  99135. + }
  99136. +
  99137. + return retval;
  99138. +}
  99139. +#endif
  99140. +
  99141. +static const struct dwc_otg_pcd_function_ops fops = {
  99142. + .complete = _complete,
  99143. +#ifdef DWC_EN_ISOC
  99144. + .isoc_complete = _isoc_complete,
  99145. +#endif
  99146. + .setup = _setup,
  99147. + .disconnect = _disconnect,
  99148. + .connect = _connect,
  99149. + .resume = _resume,
  99150. + .suspend = _suspend,
  99151. + .hnp_changed = _hnp_changed,
  99152. + .reset = _reset,
  99153. +#ifdef DWC_UTE_CFI
  99154. + .cfi_setup = _cfi_setup,
  99155. +#endif
  99156. +#ifdef DWC_UTE_PER_IO
  99157. + .xisoc_complete = _xisoc_complete,
  99158. +#endif
  99159. +};
  99160. +
  99161. +/**
  99162. + * This function is the top level PCD interrupt handler.
  99163. + */
  99164. +static irqreturn_t dwc_otg_pcd_irq(int irq, void *dev)
  99165. +{
  99166. + dwc_otg_pcd_t *pcd = dev;
  99167. + int32_t retval = IRQ_NONE;
  99168. +
  99169. + retval = dwc_otg_pcd_handle_intr(pcd);
  99170. + if (retval != 0) {
  99171. + S3C2410X_CLEAR_EINTPEND();
  99172. + }
  99173. + return IRQ_RETVAL(retval);
  99174. +}
  99175. +
  99176. +/**
  99177. + * This function initialized the usb_ep structures to there default
  99178. + * state.
  99179. + *
  99180. + * @param d Pointer on gadget_wrapper.
  99181. + */
  99182. +void gadget_add_eps(struct gadget_wrapper *d)
  99183. +{
  99184. + static const char *names[] = {
  99185. +
  99186. + "ep0",
  99187. + "ep1in",
  99188. + "ep2in",
  99189. + "ep3in",
  99190. + "ep4in",
  99191. + "ep5in",
  99192. + "ep6in",
  99193. + "ep7in",
  99194. + "ep8in",
  99195. + "ep9in",
  99196. + "ep10in",
  99197. + "ep11in",
  99198. + "ep12in",
  99199. + "ep13in",
  99200. + "ep14in",
  99201. + "ep15in",
  99202. + "ep1out",
  99203. + "ep2out",
  99204. + "ep3out",
  99205. + "ep4out",
  99206. + "ep5out",
  99207. + "ep6out",
  99208. + "ep7out",
  99209. + "ep8out",
  99210. + "ep9out",
  99211. + "ep10out",
  99212. + "ep11out",
  99213. + "ep12out",
  99214. + "ep13out",
  99215. + "ep14out",
  99216. + "ep15out"
  99217. + };
  99218. +
  99219. + int i;
  99220. + struct usb_ep *ep;
  99221. + int8_t dev_endpoints;
  99222. +
  99223. + DWC_DEBUGPL(DBG_PCDV, "%s\n", __func__);
  99224. +
  99225. + INIT_LIST_HEAD(&d->gadget.ep_list);
  99226. + d->gadget.ep0 = &d->ep0;
  99227. + d->gadget.speed = USB_SPEED_UNKNOWN;
  99228. +
  99229. + INIT_LIST_HEAD(&d->gadget.ep0->ep_list);
  99230. +
  99231. + /**
  99232. + * Initialize the EP0 structure.
  99233. + */
  99234. + ep = &d->ep0;
  99235. +
  99236. + /* Init the usb_ep structure. */
  99237. + ep->name = names[0];
  99238. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  99239. +
  99240. + /**
  99241. + * @todo NGS: What should the max packet size be set to
  99242. + * here? Before EP type is set?
  99243. + */
  99244. + ep->maxpacket = MAX_PACKET_SIZE;
  99245. + dwc_otg_pcd_ep_enable(d->pcd, NULL, ep);
  99246. +
  99247. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  99248. +
  99249. + /**
  99250. + * Initialize the EP structures.
  99251. + */
  99252. + dev_endpoints = d->pcd->core_if->dev_if->num_in_eps;
  99253. +
  99254. + for (i = 0; i < dev_endpoints; i++) {
  99255. + ep = &d->in_ep[i];
  99256. +
  99257. + /* Init the usb_ep structure. */
  99258. + ep->name = names[d->pcd->in_ep[i].dwc_ep.num];
  99259. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  99260. +
  99261. + /**
  99262. + * @todo NGS: What should the max packet size be set to
  99263. + * here? Before EP type is set?
  99264. + */
  99265. + ep->maxpacket = MAX_PACKET_SIZE;
  99266. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  99267. + }
  99268. +
  99269. + dev_endpoints = d->pcd->core_if->dev_if->num_out_eps;
  99270. +
  99271. + for (i = 0; i < dev_endpoints; i++) {
  99272. + ep = &d->out_ep[i];
  99273. +
  99274. + /* Init the usb_ep structure. */
  99275. + ep->name = names[15 + d->pcd->out_ep[i].dwc_ep.num];
  99276. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  99277. +
  99278. + /**
  99279. + * @todo NGS: What should the max packet size be set to
  99280. + * here? Before EP type is set?
  99281. + */
  99282. + ep->maxpacket = MAX_PACKET_SIZE;
  99283. +
  99284. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  99285. + }
  99286. +
  99287. + /* remove ep0 from the list. There is a ep0 pointer. */
  99288. + list_del_init(&d->ep0.ep_list);
  99289. +
  99290. + d->ep0.maxpacket = MAX_EP0_SIZE;
  99291. +}
  99292. +
  99293. +/**
  99294. + * This function releases the Gadget device.
  99295. + * required by device_unregister().
  99296. + *
  99297. + * @todo Should this do something? Should it free the PCD?
  99298. + */
  99299. +static void dwc_otg_pcd_gadget_release(struct device *dev)
  99300. +{
  99301. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, dev);
  99302. +}
  99303. +
  99304. +static struct gadget_wrapper *alloc_wrapper(dwc_bus_dev_t *_dev)
  99305. +{
  99306. + static char pcd_name[] = "dwc_otg_pcd";
  99307. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  99308. + struct gadget_wrapper *d;
  99309. + int retval;
  99310. +
  99311. + d = DWC_ALLOC(sizeof(*d));
  99312. + if (d == NULL) {
  99313. + return NULL;
  99314. + }
  99315. +
  99316. + memset(d, 0, sizeof(*d));
  99317. +
  99318. + d->gadget.name = pcd_name;
  99319. + d->pcd = otg_dev->pcd;
  99320. +
  99321. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  99322. + strcpy(d->gadget.dev.bus_id, "gadget");
  99323. +#else
  99324. + dev_set_name(&d->gadget.dev, "%s", "gadget");
  99325. +#endif
  99326. +
  99327. + d->gadget.dev.parent = &_dev->dev;
  99328. + d->gadget.dev.release = dwc_otg_pcd_gadget_release;
  99329. + d->gadget.ops = &dwc_otg_pcd_ops;
  99330. + d->gadget.max_speed = dwc_otg_pcd_is_dualspeed(otg_dev->pcd) ? USB_SPEED_HIGH:USB_SPEED_FULL;
  99331. + d->gadget.is_otg = dwc_otg_pcd_is_otg(otg_dev->pcd);
  99332. +
  99333. + d->driver = 0;
  99334. + /* Register the gadget device */
  99335. + retval = device_register(&d->gadget.dev);
  99336. + if (retval != 0) {
  99337. + DWC_ERROR("device_register failed\n");
  99338. + DWC_FREE(d);
  99339. + return NULL;
  99340. + }
  99341. +
  99342. + return d;
  99343. +}
  99344. +
  99345. +static void free_wrapper(struct gadget_wrapper *d)
  99346. +{
  99347. + if (d->driver) {
  99348. + /* should have been done already by driver model core */
  99349. + DWC_WARN("driver '%s' is still registered\n",
  99350. + d->driver->driver.name);
  99351. + usb_gadget_unregister_driver(d->driver);
  99352. + }
  99353. +
  99354. + device_unregister(&d->gadget.dev);
  99355. + DWC_FREE(d);
  99356. +}
  99357. +
  99358. +/**
  99359. + * This function initialized the PCD portion of the driver.
  99360. + *
  99361. + */
  99362. +int pcd_init(dwc_bus_dev_t *_dev)
  99363. +{
  99364. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  99365. + int retval = 0;
  99366. +
  99367. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev=%p\n", __func__, _dev, otg_dev);
  99368. +
  99369. + otg_dev->pcd = dwc_otg_pcd_init(otg_dev->core_if);
  99370. +
  99371. + if (!otg_dev->pcd) {
  99372. + DWC_ERROR("dwc_otg_pcd_init failed\n");
  99373. + return -ENOMEM;
  99374. + }
  99375. +
  99376. + otg_dev->pcd->otg_dev = otg_dev;
  99377. + gadget_wrapper = alloc_wrapper(_dev);
  99378. +
  99379. + /*
  99380. + * Initialize EP structures
  99381. + */
  99382. + gadget_add_eps(gadget_wrapper);
  99383. + /*
  99384. + * Setup interupt handler
  99385. + */
  99386. +#ifdef PLATFORM_INTERFACE
  99387. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  99388. + platform_get_irq(_dev, fiq_enable ? 0 : 1));
  99389. + retval = request_irq(platform_get_irq(_dev, fiq_enable ? 0 : 1), dwc_otg_pcd_irq,
  99390. + IRQF_SHARED, gadget_wrapper->gadget.name,
  99391. + otg_dev->pcd);
  99392. + if (retval != 0) {
  99393. + DWC_ERROR("request of irq%d failed\n",
  99394. + platform_get_irq(_dev, fiq_enable ? 0 : 1));
  99395. + free_wrapper(gadget_wrapper);
  99396. + return -EBUSY;
  99397. + }
  99398. +#else
  99399. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  99400. + _dev->irq);
  99401. + retval = request_irq(_dev->irq, dwc_otg_pcd_irq,
  99402. + IRQF_SHARED | IRQF_DISABLED,
  99403. + gadget_wrapper->gadget.name, otg_dev->pcd);
  99404. + if (retval != 0) {
  99405. + DWC_ERROR("request of irq%d failed\n", _dev->irq);
  99406. + free_wrapper(gadget_wrapper);
  99407. + return -EBUSY;
  99408. + }
  99409. +#endif
  99410. +
  99411. + dwc_otg_pcd_start(gadget_wrapper->pcd, &fops);
  99412. +
  99413. + return retval;
  99414. +}
  99415. +
  99416. +/**
  99417. + * Cleanup the PCD.
  99418. + */
  99419. +void pcd_remove(dwc_bus_dev_t *_dev)
  99420. +{
  99421. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  99422. + dwc_otg_pcd_t *pcd = otg_dev->pcd;
  99423. +
  99424. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  99425. +
  99426. + /*
  99427. + * Free the IRQ
  99428. + */
  99429. +#ifdef PLATFORM_INTERFACE
  99430. + free_irq(platform_get_irq(_dev, 0), pcd);
  99431. +#else
  99432. + free_irq(_dev->irq, pcd);
  99433. +#endif
  99434. + dwc_otg_pcd_remove(otg_dev->pcd);
  99435. + free_wrapper(gadget_wrapper);
  99436. + otg_dev->pcd = 0;
  99437. +}
  99438. +
  99439. +/**
  99440. + * This function registers a gadget driver with the PCD.
  99441. + *
  99442. + * When a driver is successfully registered, it will receive control
  99443. + * requests including set_configuration(), which enables non-control
  99444. + * requests. then usb traffic follows until a disconnect is reported.
  99445. + * then a host may connect again, or the driver might get unbound.
  99446. + *
  99447. + * @param driver The driver being registered
  99448. + * @param bind The bind function of gadget driver
  99449. + */
  99450. +
  99451. +int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
  99452. +{
  99453. + int retval;
  99454. +
  99455. + DWC_DEBUGPL(DBG_PCD, "registering gadget driver '%s'\n",
  99456. + driver->driver.name);
  99457. +
  99458. + if (!driver || driver->max_speed == USB_SPEED_UNKNOWN ||
  99459. + !driver->bind ||
  99460. + !driver->unbind || !driver->disconnect || !driver->setup) {
  99461. + DWC_DEBUGPL(DBG_PCDV, "EINVAL\n");
  99462. + return -EINVAL;
  99463. + }
  99464. + if (gadget_wrapper == 0) {
  99465. + DWC_DEBUGPL(DBG_PCDV, "ENODEV\n");
  99466. + return -ENODEV;
  99467. + }
  99468. + if (gadget_wrapper->driver != 0) {
  99469. + DWC_DEBUGPL(DBG_PCDV, "EBUSY (%p)\n", gadget_wrapper->driver);
  99470. + return -EBUSY;
  99471. + }
  99472. +
  99473. + /* hook up the driver */
  99474. + gadget_wrapper->driver = driver;
  99475. + gadget_wrapper->gadget.dev.driver = &driver->driver;
  99476. +
  99477. + DWC_DEBUGPL(DBG_PCD, "bind to driver %s\n", driver->driver.name);
  99478. + retval = driver->bind(&gadget_wrapper->gadget, gadget_wrapper->driver);
  99479. + if (retval) {
  99480. + DWC_ERROR("bind to driver %s --> error %d\n",
  99481. + driver->driver.name, retval);
  99482. + gadget_wrapper->driver = 0;
  99483. + gadget_wrapper->gadget.dev.driver = 0;
  99484. + return retval;
  99485. + }
  99486. + DWC_DEBUGPL(DBG_ANY, "registered gadget driver '%s'\n",
  99487. + driver->driver.name);
  99488. + return 0;
  99489. +}
  99490. +EXPORT_SYMBOL(usb_gadget_probe_driver);
  99491. +
  99492. +/**
  99493. + * This function unregisters a gadget driver
  99494. + *
  99495. + * @param driver The driver being unregistered
  99496. + */
  99497. +int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  99498. +{
  99499. + //DWC_DEBUGPL(DBG_PCDV,"%s(%p)\n", __func__, _driver);
  99500. +
  99501. + if (gadget_wrapper == 0) {
  99502. + DWC_DEBUGPL(DBG_ANY, "%s Return(%d): s_pcd==0\n", __func__,
  99503. + -ENODEV);
  99504. + return -ENODEV;
  99505. + }
  99506. + if (driver == 0 || driver != gadget_wrapper->driver) {
  99507. + DWC_DEBUGPL(DBG_ANY, "%s Return(%d): driver?\n", __func__,
  99508. + -EINVAL);
  99509. + return -EINVAL;
  99510. + }
  99511. +
  99512. + driver->unbind(&gadget_wrapper->gadget);
  99513. + gadget_wrapper->driver = 0;
  99514. +
  99515. + DWC_DEBUGPL(DBG_ANY, "unregistered driver '%s'\n", driver->driver.name);
  99516. + return 0;
  99517. +}
  99518. +
  99519. +EXPORT_SYMBOL(usb_gadget_unregister_driver);
  99520. +
  99521. +#endif /* DWC_HOST_ONLY */
  99522. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_regs.h linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_regs.h
  99523. --- linux-3.12.33/drivers/usb/host/dwc_otg/dwc_otg_regs.h 1969-12-31 18:00:00.000000000 -0600
  99524. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/dwc_otg_regs.h 2014-12-03 19:13:40.224418001 -0600
  99525. @@ -0,0 +1,2550 @@
  99526. +/* ==========================================================================
  99527. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_regs.h $
  99528. + * $Revision: #98 $
  99529. + * $Date: 2012/08/10 $
  99530. + * $Change: 2047372 $
  99531. + *
  99532. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  99533. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  99534. + * otherwise expressly agreed to in writing between Synopsys and you.
  99535. + *
  99536. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  99537. + * any End User Software License Agreement or Agreement for Licensed Product
  99538. + * with Synopsys or any supplement thereto. You are permitted to use and
  99539. + * redistribute this Software in source and binary forms, with or without
  99540. + * modification, provided that redistributions of source code must retain this
  99541. + * notice. You may not view, use, disclose, copy or distribute this file or
  99542. + * any information contained herein except pursuant to this license grant from
  99543. + * Synopsys. If you do not agree with this notice, including the disclaimer
  99544. + * below, then you are not authorized to use the Software.
  99545. + *
  99546. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  99547. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  99548. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  99549. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  99550. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  99551. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  99552. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  99553. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  99554. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  99555. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  99556. + * DAMAGE.
  99557. + * ========================================================================== */
  99558. +
  99559. +#ifndef __DWC_OTG_REGS_H__
  99560. +#define __DWC_OTG_REGS_H__
  99561. +
  99562. +#include "dwc_otg_core_if.h"
  99563. +
  99564. +/**
  99565. + * @file
  99566. + *
  99567. + * This file contains the data structures for accessing the DWC_otg core registers.
  99568. + *
  99569. + * The application interfaces with the HS OTG core by reading from and
  99570. + * writing to the Control and Status Register (CSR) space through the
  99571. + * AHB Slave interface. These registers are 32 bits wide, and the
  99572. + * addresses are 32-bit-block aligned.
  99573. + * CSRs are classified as follows:
  99574. + * - Core Global Registers
  99575. + * - Device Mode Registers
  99576. + * - Device Global Registers
  99577. + * - Device Endpoint Specific Registers
  99578. + * - Host Mode Registers
  99579. + * - Host Global Registers
  99580. + * - Host Port CSRs
  99581. + * - Host Channel Specific Registers
  99582. + *
  99583. + * Only the Core Global registers can be accessed in both Device and
  99584. + * Host modes. When the HS OTG core is operating in one mode, either
  99585. + * Device or Host, the application must not access registers from the
  99586. + * other mode. When the core switches from one mode to another, the
  99587. + * registers in the new mode of operation must be reprogrammed as they
  99588. + * would be after a power-on reset.
  99589. + */
  99590. +
  99591. +/****************************************************************************/
  99592. +/** DWC_otg Core registers .
  99593. + * The dwc_otg_core_global_regs structure defines the size
  99594. + * and relative field offsets for the Core Global registers.
  99595. + */
  99596. +typedef struct dwc_otg_core_global_regs {
  99597. + /** OTG Control and Status Register. <i>Offset: 000h</i> */
  99598. + volatile uint32_t gotgctl;
  99599. + /** OTG Interrupt Register. <i>Offset: 004h</i> */
  99600. + volatile uint32_t gotgint;
  99601. + /**Core AHB Configuration Register. <i>Offset: 008h</i> */
  99602. + volatile uint32_t gahbcfg;
  99603. +
  99604. +#define DWC_GLBINTRMASK 0x0001
  99605. +#define DWC_DMAENABLE 0x0020
  99606. +#define DWC_NPTXEMPTYLVL_EMPTY 0x0080
  99607. +#define DWC_NPTXEMPTYLVL_HALFEMPTY 0x0000
  99608. +#define DWC_PTXEMPTYLVL_EMPTY 0x0100
  99609. +#define DWC_PTXEMPTYLVL_HALFEMPTY 0x0000
  99610. +
  99611. + /**Core USB Configuration Register. <i>Offset: 00Ch</i> */
  99612. + volatile uint32_t gusbcfg;
  99613. + /**Core Reset Register. <i>Offset: 010h</i> */
  99614. + volatile uint32_t grstctl;
  99615. + /**Core Interrupt Register. <i>Offset: 014h</i> */
  99616. + volatile uint32_t gintsts;
  99617. + /**Core Interrupt Mask Register. <i>Offset: 018h</i> */
  99618. + volatile uint32_t gintmsk;
  99619. + /**Receive Status Queue Read Register (Read Only). <i>Offset: 01Ch</i> */
  99620. + volatile uint32_t grxstsr;
  99621. + /**Receive Status Queue Read & POP Register (Read Only). <i>Offset: 020h</i>*/
  99622. + volatile uint32_t grxstsp;
  99623. + /**Receive FIFO Size Register. <i>Offset: 024h</i> */
  99624. + volatile uint32_t grxfsiz;
  99625. + /**Non Periodic Transmit FIFO Size Register. <i>Offset: 028h</i> */
  99626. + volatile uint32_t gnptxfsiz;
  99627. + /**Non Periodic Transmit FIFO/Queue Status Register (Read
  99628. + * Only). <i>Offset: 02Ch</i> */
  99629. + volatile uint32_t gnptxsts;
  99630. + /**I2C Access Register. <i>Offset: 030h</i> */
  99631. + volatile uint32_t gi2cctl;
  99632. + /**PHY Vendor Control Register. <i>Offset: 034h</i> */
  99633. + volatile uint32_t gpvndctl;
  99634. + /**General Purpose Input/Output Register. <i>Offset: 038h</i> */
  99635. + volatile uint32_t ggpio;
  99636. + /**User ID Register. <i>Offset: 03Ch</i> */
  99637. + volatile uint32_t guid;
  99638. + /**Synopsys ID Register (Read Only). <i>Offset: 040h</i> */
  99639. + volatile uint32_t gsnpsid;
  99640. + /**User HW Config1 Register (Read Only). <i>Offset: 044h</i> */
  99641. + volatile uint32_t ghwcfg1;
  99642. + /**User HW Config2 Register (Read Only). <i>Offset: 048h</i> */
  99643. + volatile uint32_t ghwcfg2;
  99644. +#define DWC_SLAVE_ONLY_ARCH 0
  99645. +#define DWC_EXT_DMA_ARCH 1
  99646. +#define DWC_INT_DMA_ARCH 2
  99647. +
  99648. +#define DWC_MODE_HNP_SRP_CAPABLE 0
  99649. +#define DWC_MODE_SRP_ONLY_CAPABLE 1
  99650. +#define DWC_MODE_NO_HNP_SRP_CAPABLE 2
  99651. +#define DWC_MODE_SRP_CAPABLE_DEVICE 3
  99652. +#define DWC_MODE_NO_SRP_CAPABLE_DEVICE 4
  99653. +#define DWC_MODE_SRP_CAPABLE_HOST 5
  99654. +#define DWC_MODE_NO_SRP_CAPABLE_HOST 6
  99655. +
  99656. + /**User HW Config3 Register (Read Only). <i>Offset: 04Ch</i> */
  99657. + volatile uint32_t ghwcfg3;
  99658. + /**User HW Config4 Register (Read Only). <i>Offset: 050h</i>*/
  99659. + volatile uint32_t ghwcfg4;
  99660. + /** Core LPM Configuration register <i>Offset: 054h</i>*/
  99661. + volatile uint32_t glpmcfg;
  99662. + /** Global PowerDn Register <i>Offset: 058h</i> */
  99663. + volatile uint32_t gpwrdn;
  99664. + /** Global DFIFO SW Config Register <i>Offset: 05Ch</i> */
  99665. + volatile uint32_t gdfifocfg;
  99666. + /** ADP Control Register <i>Offset: 060h</i> */
  99667. + volatile uint32_t adpctl;
  99668. + /** Reserved <i>Offset: 064h-0FFh</i> */
  99669. + volatile uint32_t reserved39[39];
  99670. + /** Host Periodic Transmit FIFO Size Register. <i>Offset: 100h</i> */
  99671. + volatile uint32_t hptxfsiz;
  99672. + /** Device Periodic Transmit FIFO#n Register if dedicated fifos are disabled,
  99673. + otherwise Device Transmit FIFO#n Register.
  99674. + * <i>Offset: 104h + (FIFO_Number-1)*04h, 1 <= FIFO Number <= 15 (1<=n<=15).</i> */
  99675. + volatile uint32_t dtxfsiz[15];
  99676. +} dwc_otg_core_global_regs_t;
  99677. +
  99678. +/**
  99679. + * This union represents the bit fields of the Core OTG Control
  99680. + * and Status Register (GOTGCTL). Set the bits using the bit
  99681. + * fields then write the <i>d32</i> value to the register.
  99682. + */
  99683. +typedef union gotgctl_data {
  99684. + /** raw register data */
  99685. + uint32_t d32;
  99686. + /** register bits */
  99687. + struct {
  99688. + unsigned sesreqscs:1;
  99689. + unsigned sesreq:1;
  99690. + unsigned vbvalidoven:1;
  99691. + unsigned vbvalidovval:1;
  99692. + unsigned avalidoven:1;
  99693. + unsigned avalidovval:1;
  99694. + unsigned bvalidoven:1;
  99695. + unsigned bvalidovval:1;
  99696. + unsigned hstnegscs:1;
  99697. + unsigned hnpreq:1;
  99698. + unsigned hstsethnpen:1;
  99699. + unsigned devhnpen:1;
  99700. + unsigned reserved12_15:4;
  99701. + unsigned conidsts:1;
  99702. + unsigned dbnctime:1;
  99703. + unsigned asesvld:1;
  99704. + unsigned bsesvld:1;
  99705. + unsigned otgver:1;
  99706. + unsigned reserved1:1;
  99707. + unsigned multvalidbc:5;
  99708. + unsigned chirpen:1;
  99709. + unsigned reserved28_31:4;
  99710. + } b;
  99711. +} gotgctl_data_t;
  99712. +
  99713. +/**
  99714. + * This union represents the bit fields of the Core OTG Interrupt Register
  99715. + * (GOTGINT). Set/clear the bits using the bit fields then write the <i>d32</i>
  99716. + * value to the register.
  99717. + */
  99718. +typedef union gotgint_data {
  99719. + /** raw register data */
  99720. + uint32_t d32;
  99721. + /** register bits */
  99722. + struct {
  99723. + /** Current Mode */
  99724. + unsigned reserved0_1:2;
  99725. +
  99726. + /** Session End Detected */
  99727. + unsigned sesenddet:1;
  99728. +
  99729. + unsigned reserved3_7:5;
  99730. +
  99731. + /** Session Request Success Status Change */
  99732. + unsigned sesreqsucstschng:1;
  99733. + /** Host Negotiation Success Status Change */
  99734. + unsigned hstnegsucstschng:1;
  99735. +
  99736. + unsigned reserved10_16:7;
  99737. +
  99738. + /** Host Negotiation Detected */
  99739. + unsigned hstnegdet:1;
  99740. + /** A-Device Timeout Change */
  99741. + unsigned adevtoutchng:1;
  99742. + /** Debounce Done */
  99743. + unsigned debdone:1;
  99744. + /** Multi-Valued input changed */
  99745. + unsigned mvic:1;
  99746. +
  99747. + unsigned reserved31_21:11;
  99748. +
  99749. + } b;
  99750. +} gotgint_data_t;
  99751. +
  99752. +/**
  99753. + * This union represents the bit fields of the Core AHB Configuration
  99754. + * Register (GAHBCFG). Set/clear the bits using the bit fields then
  99755. + * write the <i>d32</i> value to the register.
  99756. + */
  99757. +typedef union gahbcfg_data {
  99758. + /** raw register data */
  99759. + uint32_t d32;
  99760. + /** register bits */
  99761. + struct {
  99762. + unsigned glblintrmsk:1;
  99763. +#define DWC_GAHBCFG_GLBINT_ENABLE 1
  99764. +
  99765. + unsigned hburstlen:4;
  99766. +#define DWC_GAHBCFG_INT_DMA_BURST_SINGLE 0
  99767. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR 1
  99768. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR4 3
  99769. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR8 5
  99770. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR16 7
  99771. +
  99772. + unsigned dmaenable:1;
  99773. +#define DWC_GAHBCFG_DMAENABLE 1
  99774. + unsigned reserved:1;
  99775. + unsigned nptxfemplvl_txfemplvl:1;
  99776. + unsigned ptxfemplvl:1;
  99777. +#define DWC_GAHBCFG_TXFEMPTYLVL_EMPTY 1
  99778. +#define DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY 0
  99779. + unsigned reserved9_20:12;
  99780. + unsigned remmemsupp:1;
  99781. + unsigned notialldmawrit:1;
  99782. + unsigned ahbsingle:1;
  99783. + unsigned reserved24_31:8;
  99784. + } b;
  99785. +} gahbcfg_data_t;
  99786. +
  99787. +/**
  99788. + * This union represents the bit fields of the Core USB Configuration
  99789. + * Register (GUSBCFG). Set the bits using the bit fields then write
  99790. + * the <i>d32</i> value to the register.
  99791. + */
  99792. +typedef union gusbcfg_data {
  99793. + /** raw register data */
  99794. + uint32_t d32;
  99795. + /** register bits */
  99796. + struct {
  99797. + unsigned toutcal:3;
  99798. + unsigned phyif:1;
  99799. + unsigned ulpi_utmi_sel:1;
  99800. + unsigned fsintf:1;
  99801. + unsigned physel:1;
  99802. + unsigned ddrsel:1;
  99803. + unsigned srpcap:1;
  99804. + unsigned hnpcap:1;
  99805. + unsigned usbtrdtim:4;
  99806. + unsigned reserved1:1;
  99807. + unsigned phylpwrclksel:1;
  99808. + unsigned otgutmifssel:1;
  99809. + unsigned ulpi_fsls:1;
  99810. + unsigned ulpi_auto_res:1;
  99811. + unsigned ulpi_clk_sus_m:1;
  99812. + unsigned ulpi_ext_vbus_drv:1;
  99813. + unsigned ulpi_int_vbus_indicator:1;
  99814. + unsigned term_sel_dl_pulse:1;
  99815. + unsigned indicator_complement:1;
  99816. + unsigned indicator_pass_through:1;
  99817. + unsigned ulpi_int_prot_dis:1;
  99818. + unsigned ic_usb_cap:1;
  99819. + unsigned ic_traffic_pull_remove:1;
  99820. + unsigned tx_end_delay:1;
  99821. + unsigned force_host_mode:1;
  99822. + unsigned force_dev_mode:1;
  99823. + unsigned reserved31:1;
  99824. + } b;
  99825. +} gusbcfg_data_t;
  99826. +
  99827. +/**
  99828. + * This union represents the bit fields of the Core Reset Register
  99829. + * (GRSTCTL). Set/clear the bits using the bit fields then write the
  99830. + * <i>d32</i> value to the register.
  99831. + */
  99832. +typedef union grstctl_data {
  99833. + /** raw register data */
  99834. + uint32_t d32;
  99835. + /** register bits */
  99836. + struct {
  99837. + /** Core Soft Reset (CSftRst) (Device and Host)
  99838. + *
  99839. + * The application can flush the control logic in the
  99840. + * entire core using this bit. This bit resets the
  99841. + * pipelines in the AHB Clock domain as well as the
  99842. + * PHY Clock domain.
  99843. + *
  99844. + * The state machines are reset to an IDLE state, the
  99845. + * control bits in the CSRs are cleared, all the
  99846. + * transmit FIFOs and the receive FIFO are flushed.
  99847. + *
  99848. + * The status mask bits that control the generation of
  99849. + * the interrupt, are cleared, to clear the
  99850. + * interrupt. The interrupt status bits are not
  99851. + * cleared, so the application can get the status of
  99852. + * any events that occurred in the core after it has
  99853. + * set this bit.
  99854. + *
  99855. + * Any transactions on the AHB are terminated as soon
  99856. + * as possible following the protocol. Any
  99857. + * transactions on the USB are terminated immediately.
  99858. + *
  99859. + * The configuration settings in the CSRs are
  99860. + * unchanged, so the software doesn't have to
  99861. + * reprogram these registers (Device
  99862. + * Configuration/Host Configuration/Core System
  99863. + * Configuration/Core PHY Configuration).
  99864. + *
  99865. + * The application can write to this bit, any time it
  99866. + * wants to reset the core. This is a self clearing
  99867. + * bit and the core clears this bit after all the
  99868. + * necessary logic is reset in the core, which may
  99869. + * take several clocks, depending on the current state
  99870. + * of the core.
  99871. + */
  99872. + unsigned csftrst:1;
  99873. + /** Hclk Soft Reset
  99874. + *
  99875. + * The application uses this bit to reset the control logic in
  99876. + * the AHB clock domain. Only AHB clock domain pipelines are
  99877. + * reset.
  99878. + */
  99879. + unsigned hsftrst:1;
  99880. + /** Host Frame Counter Reset (Host Only)<br>
  99881. + *
  99882. + * The application can reset the (micro)frame number
  99883. + * counter inside the core, using this bit. When the
  99884. + * (micro)frame counter is reset, the subsequent SOF
  99885. + * sent out by the core, will have a (micro)frame
  99886. + * number of 0.
  99887. + */
  99888. + unsigned hstfrm:1;
  99889. + /** In Token Sequence Learning Queue Flush
  99890. + * (INTknQFlsh) (Device Only)
  99891. + */
  99892. + unsigned intknqflsh:1;
  99893. + /** RxFIFO Flush (RxFFlsh) (Device and Host)
  99894. + *
  99895. + * The application can flush the entire Receive FIFO
  99896. + * using this bit. The application must first
  99897. + * ensure that the core is not in the middle of a
  99898. + * transaction. The application should write into
  99899. + * this bit, only after making sure that neither the
  99900. + * DMA engine is reading from the RxFIFO nor the MAC
  99901. + * is writing the data in to the FIFO. The
  99902. + * application should wait until the bit is cleared
  99903. + * before performing any other operations. This bit
  99904. + * will takes 8 clocks (slowest of PHY or AHB clock)
  99905. + * to clear.
  99906. + */
  99907. + unsigned rxfflsh:1;
  99908. + /** TxFIFO Flush (TxFFlsh) (Device and Host).
  99909. + *
  99910. + * This bit is used to selectively flush a single or
  99911. + * all transmit FIFOs. The application must first
  99912. + * ensure that the core is not in the middle of a
  99913. + * transaction. The application should write into
  99914. + * this bit, only after making sure that neither the
  99915. + * DMA engine is writing into the TxFIFO nor the MAC
  99916. + * is reading the data out of the FIFO. The
  99917. + * application should wait until the core clears this
  99918. + * bit, before performing any operations. This bit
  99919. + * will takes 8 clocks (slowest of PHY or AHB clock)
  99920. + * to clear.
  99921. + */
  99922. + unsigned txfflsh:1;
  99923. +
  99924. + /** TxFIFO Number (TxFNum) (Device and Host).
  99925. + *
  99926. + * This is the FIFO number which needs to be flushed,
  99927. + * using the TxFIFO Flush bit. This field should not
  99928. + * be changed until the TxFIFO Flush bit is cleared by
  99929. + * the core.
  99930. + * - 0x0 : Non Periodic TxFIFO Flush
  99931. + * - 0x1 : Periodic TxFIFO #1 Flush in device mode
  99932. + * or Periodic TxFIFO in host mode
  99933. + * - 0x2 : Periodic TxFIFO #2 Flush in device mode.
  99934. + * - ...
  99935. + * - 0xF : Periodic TxFIFO #15 Flush in device mode
  99936. + * - 0x10: Flush all the Transmit NonPeriodic and
  99937. + * Transmit Periodic FIFOs in the core
  99938. + */
  99939. + unsigned txfnum:5;
  99940. + /** Reserved */
  99941. + unsigned reserved11_29:19;
  99942. + /** DMA Request Signal. Indicated DMA request is in
  99943. + * probress. Used for debug purpose. */
  99944. + unsigned dmareq:1;
  99945. + /** AHB Master Idle. Indicates the AHB Master State
  99946. + * Machine is in IDLE condition. */
  99947. + unsigned ahbidle:1;
  99948. + } b;
  99949. +} grstctl_t;
  99950. +
  99951. +/**
  99952. + * This union represents the bit fields of the Core Interrupt Mask
  99953. + * Register (GINTMSK). Set/clear the bits using the bit fields then
  99954. + * write the <i>d32</i> value to the register.
  99955. + */
  99956. +typedef union gintmsk_data {
  99957. + /** raw register data */
  99958. + uint32_t d32;
  99959. + /** register bits */
  99960. + struct {
  99961. + unsigned reserved0:1;
  99962. + unsigned modemismatch:1;
  99963. + unsigned otgintr:1;
  99964. + unsigned sofintr:1;
  99965. + unsigned rxstsqlvl:1;
  99966. + unsigned nptxfempty:1;
  99967. + unsigned ginnakeff:1;
  99968. + unsigned goutnakeff:1;
  99969. + unsigned ulpickint:1;
  99970. + unsigned i2cintr:1;
  99971. + unsigned erlysuspend:1;
  99972. + unsigned usbsuspend:1;
  99973. + unsigned usbreset:1;
  99974. + unsigned enumdone:1;
  99975. + unsigned isooutdrop:1;
  99976. + unsigned eopframe:1;
  99977. + unsigned restoredone:1;
  99978. + unsigned epmismatch:1;
  99979. + unsigned inepintr:1;
  99980. + unsigned outepintr:1;
  99981. + unsigned incomplisoin:1;
  99982. + unsigned incomplisoout:1;
  99983. + unsigned fetsusp:1;
  99984. + unsigned resetdet:1;
  99985. + unsigned portintr:1;
  99986. + unsigned hcintr:1;
  99987. + unsigned ptxfempty:1;
  99988. + unsigned lpmtranrcvd:1;
  99989. + unsigned conidstschng:1;
  99990. + unsigned disconnect:1;
  99991. + unsigned sessreqintr:1;
  99992. + unsigned wkupintr:1;
  99993. + } b;
  99994. +} gintmsk_data_t;
  99995. +/**
  99996. + * This union represents the bit fields of the Core Interrupt Register
  99997. + * (GINTSTS). Set/clear the bits using the bit fields then write the
  99998. + * <i>d32</i> value to the register.
  99999. + */
  100000. +typedef union gintsts_data {
  100001. + /** raw register data */
  100002. + uint32_t d32;
  100003. +#define DWC_SOF_INTR_MASK 0x0008
  100004. + /** register bits */
  100005. + struct {
  100006. +#define DWC_HOST_MODE 1
  100007. + unsigned curmode:1;
  100008. + unsigned modemismatch:1;
  100009. + unsigned otgintr:1;
  100010. + unsigned sofintr:1;
  100011. + unsigned rxstsqlvl:1;
  100012. + unsigned nptxfempty:1;
  100013. + unsigned ginnakeff:1;
  100014. + unsigned goutnakeff:1;
  100015. + unsigned ulpickint:1;
  100016. + unsigned i2cintr:1;
  100017. + unsigned erlysuspend:1;
  100018. + unsigned usbsuspend:1;
  100019. + unsigned usbreset:1;
  100020. + unsigned enumdone:1;
  100021. + unsigned isooutdrop:1;
  100022. + unsigned eopframe:1;
  100023. + unsigned restoredone:1;
  100024. + unsigned epmismatch:1;
  100025. + unsigned inepint:1;
  100026. + unsigned outepintr:1;
  100027. + unsigned incomplisoin:1;
  100028. + unsigned incomplisoout:1;
  100029. + unsigned fetsusp:1;
  100030. + unsigned resetdet:1;
  100031. + unsigned portintr:1;
  100032. + unsigned hcintr:1;
  100033. + unsigned ptxfempty:1;
  100034. + unsigned lpmtranrcvd:1;
  100035. + unsigned conidstschng:1;
  100036. + unsigned disconnect:1;
  100037. + unsigned sessreqintr:1;
  100038. + unsigned wkupintr:1;
  100039. + } b;
  100040. +} gintsts_data_t;
  100041. +
  100042. +/**
  100043. + * This union represents the bit fields in the Device Receive Status Read and
  100044. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  100045. + * element then read out the bits using the <i>b</i>it elements.
  100046. + */
  100047. +typedef union device_grxsts_data {
  100048. + /** raw register data */
  100049. + uint32_t d32;
  100050. + /** register bits */
  100051. + struct {
  100052. + unsigned epnum:4;
  100053. + unsigned bcnt:11;
  100054. + unsigned dpid:2;
  100055. +
  100056. +#define DWC_STS_DATA_UPDT 0x2 // OUT Data Packet
  100057. +#define DWC_STS_XFER_COMP 0x3 // OUT Data Transfer Complete
  100058. +
  100059. +#define DWC_DSTS_GOUT_NAK 0x1 // Global OUT NAK
  100060. +#define DWC_DSTS_SETUP_COMP 0x4 // Setup Phase Complete
  100061. +#define DWC_DSTS_SETUP_UPDT 0x6 // SETUP Packet
  100062. + unsigned pktsts:4;
  100063. + unsigned fn:4;
  100064. + unsigned reserved25_31:7;
  100065. + } b;
  100066. +} device_grxsts_data_t;
  100067. +
  100068. +/**
  100069. + * This union represents the bit fields in the Host Receive Status Read and
  100070. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  100071. + * element then read out the bits using the <i>b</i>it elements.
  100072. + */
  100073. +typedef union host_grxsts_data {
  100074. + /** raw register data */
  100075. + uint32_t d32;
  100076. + /** register bits */
  100077. + struct {
  100078. + unsigned chnum:4;
  100079. + unsigned bcnt:11;
  100080. + unsigned dpid:2;
  100081. +
  100082. + unsigned pktsts:4;
  100083. +#define DWC_GRXSTS_PKTSTS_IN 0x2
  100084. +#define DWC_GRXSTS_PKTSTS_IN_XFER_COMP 0x3
  100085. +#define DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR 0x5
  100086. +#define DWC_GRXSTS_PKTSTS_CH_HALTED 0x7
  100087. +
  100088. + unsigned reserved21_31:11;
  100089. + } b;
  100090. +} host_grxsts_data_t;
  100091. +
  100092. +/**
  100093. + * This union represents the bit fields in the FIFO Size Registers (HPTXFSIZ,
  100094. + * GNPTXFSIZ, DPTXFSIZn, DIEPTXFn). Read the register into the <i>d32</i> element
  100095. + * then read out the bits using the <i>b</i>it elements.
  100096. + */
  100097. +typedef union fifosize_data {
  100098. + /** raw register data */
  100099. + uint32_t d32;
  100100. + /** register bits */
  100101. + struct {
  100102. + unsigned startaddr:16;
  100103. + unsigned depth:16;
  100104. + } b;
  100105. +} fifosize_data_t;
  100106. +
  100107. +/**
  100108. + * This union represents the bit fields in the Non-Periodic Transmit
  100109. + * FIFO/Queue Status Register (GNPTXSTS). Read the register into the
  100110. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  100111. + * elements.
  100112. + */
  100113. +typedef union gnptxsts_data {
  100114. + /** raw register data */
  100115. + uint32_t d32;
  100116. + /** register bits */
  100117. + struct {
  100118. + unsigned nptxfspcavail:16;
  100119. + unsigned nptxqspcavail:8;
  100120. + /** Top of the Non-Periodic Transmit Request Queue
  100121. + * - bit 24 - Terminate (Last entry for the selected
  100122. + * channel/EP)
  100123. + * - bits 26:25 - Token Type
  100124. + * - 2'b00 - IN/OUT
  100125. + * - 2'b01 - Zero Length OUT
  100126. + * - 2'b10 - PING/Complete Split
  100127. + * - 2'b11 - Channel Halt
  100128. + * - bits 30:27 - Channel/EP Number
  100129. + */
  100130. + unsigned nptxqtop_terminate:1;
  100131. + unsigned nptxqtop_token:2;
  100132. + unsigned nptxqtop_chnep:4;
  100133. + unsigned reserved:1;
  100134. + } b;
  100135. +} gnptxsts_data_t;
  100136. +
  100137. +/**
  100138. + * This union represents the bit fields in the Transmit
  100139. + * FIFO Status Register (DTXFSTS). Read the register into the
  100140. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  100141. + * elements.
  100142. + */
  100143. +typedef union dtxfsts_data {
  100144. + /** raw register data */
  100145. + uint32_t d32;
  100146. + /** register bits */
  100147. + struct {
  100148. + unsigned txfspcavail:16;
  100149. + unsigned reserved:16;
  100150. + } b;
  100151. +} dtxfsts_data_t;
  100152. +
  100153. +/**
  100154. + * This union represents the bit fields in the I2C Control Register
  100155. + * (I2CCTL). Read the register into the <i>d32</i> element then read out the
  100156. + * bits using the <i>b</i>it elements.
  100157. + */
  100158. +typedef union gi2cctl_data {
  100159. + /** raw register data */
  100160. + uint32_t d32;
  100161. + /** register bits */
  100162. + struct {
  100163. + unsigned rwdata:8;
  100164. + unsigned regaddr:8;
  100165. + unsigned addr:7;
  100166. + unsigned i2cen:1;
  100167. + unsigned ack:1;
  100168. + unsigned i2csuspctl:1;
  100169. + unsigned i2cdevaddr:2;
  100170. + unsigned i2cdatse0:1;
  100171. + unsigned reserved:1;
  100172. + unsigned rw:1;
  100173. + unsigned bsydne:1;
  100174. + } b;
  100175. +} gi2cctl_data_t;
  100176. +
  100177. +/**
  100178. + * This union represents the bit fields in the PHY Vendor Control Register
  100179. + * (GPVNDCTL). Read the register into the <i>d32</i> element then read out the
  100180. + * bits using the <i>b</i>it elements.
  100181. + */
  100182. +typedef union gpvndctl_data {
  100183. + /** raw register data */
  100184. + uint32_t d32;
  100185. + /** register bits */
  100186. + struct {
  100187. + unsigned regdata:8;
  100188. + unsigned vctrl:8;
  100189. + unsigned regaddr16_21:6;
  100190. + unsigned regwr:1;
  100191. + unsigned reserved23_24:2;
  100192. + unsigned newregreq:1;
  100193. + unsigned vstsbsy:1;
  100194. + unsigned vstsdone:1;
  100195. + unsigned reserved28_30:3;
  100196. + unsigned disulpidrvr:1;
  100197. + } b;
  100198. +} gpvndctl_data_t;
  100199. +
  100200. +/**
  100201. + * This union represents the bit fields in the General Purpose
  100202. + * Input/Output Register (GGPIO).
  100203. + * Read the register into the <i>d32</i> element then read out the
  100204. + * bits using the <i>b</i>it elements.
  100205. + */
  100206. +typedef union ggpio_data {
  100207. + /** raw register data */
  100208. + uint32_t d32;
  100209. + /** register bits */
  100210. + struct {
  100211. + unsigned gpi:16;
  100212. + unsigned gpo:16;
  100213. + } b;
  100214. +} ggpio_data_t;
  100215. +
  100216. +/**
  100217. + * This union represents the bit fields in the User ID Register
  100218. + * (GUID). Read the register into the <i>d32</i> element then read out the
  100219. + * bits using the <i>b</i>it elements.
  100220. + */
  100221. +typedef union guid_data {
  100222. + /** raw register data */
  100223. + uint32_t d32;
  100224. + /** register bits */
  100225. + struct {
  100226. + unsigned rwdata:32;
  100227. + } b;
  100228. +} guid_data_t;
  100229. +
  100230. +/**
  100231. + * This union represents the bit fields in the Synopsys ID Register
  100232. + * (GSNPSID). Read the register into the <i>d32</i> element then read out the
  100233. + * bits using the <i>b</i>it elements.
  100234. + */
  100235. +typedef union gsnpsid_data {
  100236. + /** raw register data */
  100237. + uint32_t d32;
  100238. + /** register bits */
  100239. + struct {
  100240. + unsigned rwdata:32;
  100241. + } b;
  100242. +} gsnpsid_data_t;
  100243. +
  100244. +/**
  100245. + * This union represents the bit fields in the User HW Config1
  100246. + * Register. Read the register into the <i>d32</i> element then read
  100247. + * out the bits using the <i>b</i>it elements.
  100248. + */
  100249. +typedef union hwcfg1_data {
  100250. + /** raw register data */
  100251. + uint32_t d32;
  100252. + /** register bits */
  100253. + struct {
  100254. + unsigned ep_dir0:2;
  100255. + unsigned ep_dir1:2;
  100256. + unsigned ep_dir2:2;
  100257. + unsigned ep_dir3:2;
  100258. + unsigned ep_dir4:2;
  100259. + unsigned ep_dir5:2;
  100260. + unsigned ep_dir6:2;
  100261. + unsigned ep_dir7:2;
  100262. + unsigned ep_dir8:2;
  100263. + unsigned ep_dir9:2;
  100264. + unsigned ep_dir10:2;
  100265. + unsigned ep_dir11:2;
  100266. + unsigned ep_dir12:2;
  100267. + unsigned ep_dir13:2;
  100268. + unsigned ep_dir14:2;
  100269. + unsigned ep_dir15:2;
  100270. + } b;
  100271. +} hwcfg1_data_t;
  100272. +
  100273. +/**
  100274. + * This union represents the bit fields in the User HW Config2
  100275. + * Register. Read the register into the <i>d32</i> element then read
  100276. + * out the bits using the <i>b</i>it elements.
  100277. + */
  100278. +typedef union hwcfg2_data {
  100279. + /** raw register data */
  100280. + uint32_t d32;
  100281. + /** register bits */
  100282. + struct {
  100283. + /* GHWCFG2 */
  100284. + unsigned op_mode:3;
  100285. +#define DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG 0
  100286. +#define DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG 1
  100287. +#define DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG 2
  100288. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE 3
  100289. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE 4
  100290. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST 5
  100291. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST 6
  100292. +
  100293. + unsigned architecture:2;
  100294. + unsigned point2point:1;
  100295. + unsigned hs_phy_type:2;
  100296. +#define DWC_HWCFG2_HS_PHY_TYPE_NOT_SUPPORTED 0
  100297. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI 1
  100298. +#define DWC_HWCFG2_HS_PHY_TYPE_ULPI 2
  100299. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI 3
  100300. +
  100301. + unsigned fs_phy_type:2;
  100302. + unsigned num_dev_ep:4;
  100303. + unsigned num_host_chan:4;
  100304. + unsigned perio_ep_supported:1;
  100305. + unsigned dynamic_fifo:1;
  100306. + unsigned multi_proc_int:1;
  100307. + unsigned reserved21:1;
  100308. + unsigned nonperio_tx_q_depth:2;
  100309. + unsigned host_perio_tx_q_depth:2;
  100310. + unsigned dev_token_q_depth:5;
  100311. + unsigned otg_enable_ic_usb:1;
  100312. + } b;
  100313. +} hwcfg2_data_t;
  100314. +
  100315. +/**
  100316. + * This union represents the bit fields in the User HW Config3
  100317. + * Register. Read the register into the <i>d32</i> element then read
  100318. + * out the bits using the <i>b</i>it elements.
  100319. + */
  100320. +typedef union hwcfg3_data {
  100321. + /** raw register data */
  100322. + uint32_t d32;
  100323. + /** register bits */
  100324. + struct {
  100325. + /* GHWCFG3 */
  100326. + unsigned xfer_size_cntr_width:4;
  100327. + unsigned packet_size_cntr_width:3;
  100328. + unsigned otg_func:1;
  100329. + unsigned i2c:1;
  100330. + unsigned vendor_ctrl_if:1;
  100331. + unsigned optional_features:1;
  100332. + unsigned synch_reset_type:1;
  100333. + unsigned adp_supp:1;
  100334. + unsigned otg_enable_hsic:1;
  100335. + unsigned bc_support:1;
  100336. + unsigned otg_lpm_en:1;
  100337. + unsigned dfifo_depth:16;
  100338. + } b;
  100339. +} hwcfg3_data_t;
  100340. +
  100341. +/**
  100342. + * This union represents the bit fields in the User HW Config4
  100343. + * Register. Read the register into the <i>d32</i> element then read
  100344. + * out the bits using the <i>b</i>it elements.
  100345. + */
  100346. +typedef union hwcfg4_data {
  100347. + /** raw register data */
  100348. + uint32_t d32;
  100349. + /** register bits */
  100350. + struct {
  100351. + unsigned num_dev_perio_in_ep:4;
  100352. + unsigned power_optimiz:1;
  100353. + unsigned min_ahb_freq:1;
  100354. + unsigned hiber:1;
  100355. + unsigned xhiber:1;
  100356. + unsigned reserved:6;
  100357. + unsigned utmi_phy_data_width:2;
  100358. + unsigned num_dev_mode_ctrl_ep:4;
  100359. + unsigned iddig_filt_en:1;
  100360. + unsigned vbus_valid_filt_en:1;
  100361. + unsigned a_valid_filt_en:1;
  100362. + unsigned b_valid_filt_en:1;
  100363. + unsigned session_end_filt_en:1;
  100364. + unsigned ded_fifo_en:1;
  100365. + unsigned num_in_eps:4;
  100366. + unsigned desc_dma:1;
  100367. + unsigned desc_dma_dyn:1;
  100368. + } b;
  100369. +} hwcfg4_data_t;
  100370. +
  100371. +/**
  100372. + * This union represents the bit fields of the Core LPM Configuration
  100373. + * Register (GLPMCFG). Set the bits using bit fields then write
  100374. + * the <i>d32</i> value to the register.
  100375. + */
  100376. +typedef union glpmctl_data {
  100377. + /** raw register data */
  100378. + uint32_t d32;
  100379. + /** register bits */
  100380. + struct {
  100381. + /** LPM-Capable (LPMCap) (Device and Host)
  100382. + * The application uses this bit to control
  100383. + * the DWC_otg core LPM capabilities.
  100384. + */
  100385. + unsigned lpm_cap_en:1;
  100386. + /** LPM response programmed by application (AppL1Res) (Device)
  100387. + * Handshake response to LPM token pre-programmed
  100388. + * by device application software.
  100389. + */
  100390. + unsigned appl_resp:1;
  100391. + /** Host Initiated Resume Duration (HIRD) (Device and Host)
  100392. + * In Host mode this field indicates the value of HIRD
  100393. + * to be sent in an LPM transaction.
  100394. + * In Device mode this field is updated with the
  100395. + * Received LPM Token HIRD bmAttribute
  100396. + * when an ACK/NYET/STALL response is sent
  100397. + * to an LPM transaction.
  100398. + */
  100399. + unsigned hird:4;
  100400. + /** RemoteWakeEnable (bRemoteWake) (Device and Host)
  100401. + * In Host mode this bit indicates the value of remote
  100402. + * wake up to be sent in wIndex field of LPM transaction.
  100403. + * In Device mode this field is updated with the
  100404. + * Received LPM Token bRemoteWake bmAttribute
  100405. + * when an ACK/NYET/STALL response is sent
  100406. + * to an LPM transaction.
  100407. + */
  100408. + unsigned rem_wkup_en:1;
  100409. + /** Enable utmi_sleep_n (EnblSlpM) (Device and Host)
  100410. + * The application uses this bit to control
  100411. + * the utmi_sleep_n assertion to the PHY when in L1 state.
  100412. + */
  100413. + unsigned en_utmi_sleep:1;
  100414. + /** HIRD Threshold (HIRD_Thres) (Device and Host)
  100415. + */
  100416. + unsigned hird_thres:5;
  100417. + /** LPM Response (CoreL1Res) (Device and Host)
  100418. + * In Host mode this bit contains handsake response to
  100419. + * LPM transaction.
  100420. + * In Device mode the response of the core to
  100421. + * LPM transaction received is reflected in these two bits.
  100422. + - 0x0 : ERROR (No handshake response)
  100423. + - 0x1 : STALL
  100424. + - 0x2 : NYET
  100425. + - 0x3 : ACK
  100426. + */
  100427. + unsigned lpm_resp:2;
  100428. + /** Port Sleep Status (SlpSts) (Device and Host)
  100429. + * This bit is set as long as a Sleep condition
  100430. + * is present on the USB bus.
  100431. + */
  100432. + unsigned prt_sleep_sts:1;
  100433. + /** Sleep State Resume OK (L1ResumeOK) (Device and Host)
  100434. + * Indicates that the application or host
  100435. + * can start resume from Sleep state.
  100436. + */
  100437. + unsigned sleep_state_resumeok:1;
  100438. + /** LPM channel Index (LPM_Chnl_Indx) (Host)
  100439. + * The channel number on which the LPM transaction
  100440. + * has to be applied while sending
  100441. + * an LPM transaction to the local device.
  100442. + */
  100443. + unsigned lpm_chan_index:4;
  100444. + /** LPM Retry Count (LPM_Retry_Cnt) (Host)
  100445. + * Number host retries that would be performed
  100446. + * if the device response was not valid response.
  100447. + */
  100448. + unsigned retry_count:3;
  100449. + /** Send LPM Transaction (SndLPM) (Host)
  100450. + * When set by application software,
  100451. + * an LPM transaction containing two tokens
  100452. + * is sent.
  100453. + */
  100454. + unsigned send_lpm:1;
  100455. + /** LPM Retry status (LPM_RetryCnt_Sts) (Host)
  100456. + * Number of LPM Host Retries still remaining
  100457. + * to be transmitted for the current LPM sequence
  100458. + */
  100459. + unsigned retry_count_sts:3;
  100460. + unsigned reserved28_29:2;
  100461. + /** In host mode once this bit is set, the host
  100462. + * configures to drive the HSIC Idle state on the bus.
  100463. + * It then waits for the device to initiate the Connect sequence.
  100464. + * In device mode once this bit is set, the device waits for
  100465. + * the HSIC Idle line state on the bus. Upon receving the Idle
  100466. + * line state, it initiates the HSIC Connect sequence.
  100467. + */
  100468. + unsigned hsic_connect:1;
  100469. + /** This bit overrides and functionally inverts
  100470. + * the if_select_hsic input port signal.
  100471. + */
  100472. + unsigned inv_sel_hsic:1;
  100473. + } b;
  100474. +} glpmcfg_data_t;
  100475. +
  100476. +/**
  100477. + * This union represents the bit fields of the Core ADP Timer, Control and
  100478. + * Status Register (ADPTIMCTLSTS). Set the bits using bit fields then write
  100479. + * the <i>d32</i> value to the register.
  100480. + */
  100481. +typedef union adpctl_data {
  100482. + /** raw register data */
  100483. + uint32_t d32;
  100484. + /** register bits */
  100485. + struct {
  100486. + /** Probe Discharge (PRB_DSCHG)
  100487. + * These bits set the times for TADP_DSCHG.
  100488. + * These bits are defined as follows:
  100489. + * 2'b00 - 4 msec
  100490. + * 2'b01 - 8 msec
  100491. + * 2'b10 - 16 msec
  100492. + * 2'b11 - 32 msec
  100493. + */
  100494. + unsigned prb_dschg:2;
  100495. + /** Probe Delta (PRB_DELTA)
  100496. + * These bits set the resolution for RTIM value.
  100497. + * The bits are defined in units of 32 kHz clock cycles as follows:
  100498. + * 2'b00 - 1 cycles
  100499. + * 2'b01 - 2 cycles
  100500. + * 2'b10 - 3 cycles
  100501. + * 2'b11 - 4 cycles
  100502. + * For example if this value is chosen to 2'b01, it means that RTIM
  100503. + * increments for every 3(three) 32Khz clock cycles.
  100504. + */
  100505. + unsigned prb_delta:2;
  100506. + /** Probe Period (PRB_PER)
  100507. + * These bits sets the TADP_PRD as shown in Figure 4 as follows:
  100508. + * 2'b00 - 0.625 to 0.925 sec (typical 0.775 sec)
  100509. + * 2'b01 - 1.25 to 1.85 sec (typical 1.55 sec)
  100510. + * 2'b10 - 1.9 to 2.6 sec (typical 2.275 sec)
  100511. + * 2'b11 - Reserved
  100512. + */
  100513. + unsigned prb_per:2;
  100514. + /** These bits capture the latest time it took for VBUS to ramp from
  100515. + * VADP_SINK to VADP_PRB.
  100516. + * 0x000 - 1 cycles
  100517. + * 0x001 - 2 cycles
  100518. + * 0x002 - 3 cycles
  100519. + * etc
  100520. + * 0x7FF - 2048 cycles
  100521. + * A time of 1024 cycles at 32 kHz corresponds to a time of 32 msec.
  100522. + */
  100523. + unsigned rtim:11;
  100524. + /** Enable Probe (EnaPrb)
  100525. + * When programmed to 1'b1, the core performs a probe operation.
  100526. + * This bit is valid only if OTG_Ver = 1'b1.
  100527. + */
  100528. + unsigned enaprb:1;
  100529. + /** Enable Sense (EnaSns)
  100530. + * When programmed to 1'b1, the core performs a Sense operation.
  100531. + * This bit is valid only if OTG_Ver = 1'b1.
  100532. + */
  100533. + unsigned enasns:1;
  100534. + /** ADP Reset (ADPRes)
  100535. + * When set, ADP controller is reset.
  100536. + * This bit is valid only if OTG_Ver = 1'b1.
  100537. + */
  100538. + unsigned adpres:1;
  100539. + /** ADP Enable (ADPEn)
  100540. + * When set, the core performs either ADP probing or sensing
  100541. + * based on EnaPrb or EnaSns.
  100542. + * This bit is valid only if OTG_Ver = 1'b1.
  100543. + */
  100544. + unsigned adpen:1;
  100545. + /** ADP Probe Interrupt (ADP_PRB_INT)
  100546. + * When this bit is set, it means that the VBUS
  100547. + * voltage is greater than VADP_PRB or VADP_PRB is reached.
  100548. + * This bit is valid only if OTG_Ver = 1'b1.
  100549. + */
  100550. + unsigned adp_prb_int:1;
  100551. + /**
  100552. + * ADP Sense Interrupt (ADP_SNS_INT)
  100553. + * When this bit is set, it means that the VBUS voltage is greater than
  100554. + * VADP_SNS value or VADP_SNS is reached.
  100555. + * This bit is valid only if OTG_Ver = 1'b1.
  100556. + */
  100557. + unsigned adp_sns_int:1;
  100558. + /** ADP Tomeout Interrupt (ADP_TMOUT_INT)
  100559. + * This bit is relevant only for an ADP probe.
  100560. + * When this bit is set, it means that the ramp time has
  100561. + * completed ie ADPCTL.RTIM has reached its terminal value
  100562. + * of 0x7FF. This is a debug feature that allows software
  100563. + * to read the ramp time after each cycle.
  100564. + * This bit is valid only if OTG_Ver = 1'b1.
  100565. + */
  100566. + unsigned adp_tmout_int:1;
  100567. + /** ADP Probe Interrupt Mask (ADP_PRB_INT_MSK)
  100568. + * When this bit is set, it unmasks the interrupt due to ADP_PRB_INT.
  100569. + * This bit is valid only if OTG_Ver = 1'b1.
  100570. + */
  100571. + unsigned adp_prb_int_msk:1;
  100572. + /** ADP Sense Interrupt Mask (ADP_SNS_INT_MSK)
  100573. + * When this bit is set, it unmasks the interrupt due to ADP_SNS_INT.
  100574. + * This bit is valid only if OTG_Ver = 1'b1.
  100575. + */
  100576. + unsigned adp_sns_int_msk:1;
  100577. + /** ADP Timoeout Interrupt Mask (ADP_TMOUT_MSK)
  100578. + * When this bit is set, it unmasks the interrupt due to ADP_TMOUT_INT.
  100579. + * This bit is valid only if OTG_Ver = 1'b1.
  100580. + */
  100581. + unsigned adp_tmout_int_msk:1;
  100582. + /** Access Request
  100583. + * 2'b00 - Read/Write Valid (updated by the core)
  100584. + * 2'b01 - Read
  100585. + * 2'b00 - Write
  100586. + * 2'b00 - Reserved
  100587. + */
  100588. + unsigned ar:2;
  100589. + /** Reserved */
  100590. + unsigned reserved29_31:3;
  100591. + } b;
  100592. +} adpctl_data_t;
  100593. +
  100594. +////////////////////////////////////////////
  100595. +// Device Registers
  100596. +/**
  100597. + * Device Global Registers. <i>Offsets 800h-BFFh</i>
  100598. + *
  100599. + * The following structures define the size and relative field offsets
  100600. + * for the Device Mode Registers.
  100601. + *
  100602. + * <i>These registers are visible only in Device mode and must not be
  100603. + * accessed in Host mode, as the results are unknown.</i>
  100604. + */
  100605. +typedef struct dwc_otg_dev_global_regs {
  100606. + /** Device Configuration Register. <i>Offset 800h</i> */
  100607. + volatile uint32_t dcfg;
  100608. + /** Device Control Register. <i>Offset: 804h</i> */
  100609. + volatile uint32_t dctl;
  100610. + /** Device Status Register (Read Only). <i>Offset: 808h</i> */
  100611. + volatile uint32_t dsts;
  100612. + /** Reserved. <i>Offset: 80Ch</i> */
  100613. + uint32_t unused;
  100614. + /** Device IN Endpoint Common Interrupt Mask
  100615. + * Register. <i>Offset: 810h</i> */
  100616. + volatile uint32_t diepmsk;
  100617. + /** Device OUT Endpoint Common Interrupt Mask
  100618. + * Register. <i>Offset: 814h</i> */
  100619. + volatile uint32_t doepmsk;
  100620. + /** Device All Endpoints Interrupt Register. <i>Offset: 818h</i> */
  100621. + volatile uint32_t daint;
  100622. + /** Device All Endpoints Interrupt Mask Register. <i>Offset:
  100623. + * 81Ch</i> */
  100624. + volatile uint32_t daintmsk;
  100625. + /** Device IN Token Queue Read Register-1 (Read Only).
  100626. + * <i>Offset: 820h</i> */
  100627. + volatile uint32_t dtknqr1;
  100628. + /** Device IN Token Queue Read Register-2 (Read Only).
  100629. + * <i>Offset: 824h</i> */
  100630. + volatile uint32_t dtknqr2;
  100631. + /** Device VBUS discharge Register. <i>Offset: 828h</i> */
  100632. + volatile uint32_t dvbusdis;
  100633. + /** Device VBUS Pulse Register. <i>Offset: 82Ch</i> */
  100634. + volatile uint32_t dvbuspulse;
  100635. + /** Device IN Token Queue Read Register-3 (Read Only). /
  100636. + * Device Thresholding control register (Read/Write)
  100637. + * <i>Offset: 830h</i> */
  100638. + volatile uint32_t dtknqr3_dthrctl;
  100639. + /** Device IN Token Queue Read Register-4 (Read Only). /
  100640. + * Device IN EPs empty Inr. Mask Register (Read/Write)
  100641. + * <i>Offset: 834h</i> */
  100642. + volatile uint32_t dtknqr4_fifoemptymsk;
  100643. + /** Device Each Endpoint Interrupt Register (Read Only). /
  100644. + * <i>Offset: 838h</i> */
  100645. + volatile uint32_t deachint;
  100646. + /** Device Each Endpoint Interrupt mask Register (Read/Write). /
  100647. + * <i>Offset: 83Ch</i> */
  100648. + volatile uint32_t deachintmsk;
  100649. + /** Device Each In Endpoint Interrupt mask Register (Read/Write). /
  100650. + * <i>Offset: 840h</i> */
  100651. + volatile uint32_t diepeachintmsk[MAX_EPS_CHANNELS];
  100652. + /** Device Each Out Endpoint Interrupt mask Register (Read/Write). /
  100653. + * <i>Offset: 880h</i> */
  100654. + volatile uint32_t doepeachintmsk[MAX_EPS_CHANNELS];
  100655. +} dwc_otg_device_global_regs_t;
  100656. +
  100657. +/**
  100658. + * This union represents the bit fields in the Device Configuration
  100659. + * Register. Read the register into the <i>d32</i> member then
  100660. + * set/clear the bits using the <i>b</i>it elements. Write the
  100661. + * <i>d32</i> member to the dcfg register.
  100662. + */
  100663. +typedef union dcfg_data {
  100664. + /** raw register data */
  100665. + uint32_t d32;
  100666. + /** register bits */
  100667. + struct {
  100668. + /** Device Speed */
  100669. + unsigned devspd:2;
  100670. + /** Non Zero Length Status OUT Handshake */
  100671. + unsigned nzstsouthshk:1;
  100672. +#define DWC_DCFG_SEND_STALL 1
  100673. +
  100674. + unsigned ena32khzs:1;
  100675. + /** Device Addresses */
  100676. + unsigned devaddr:7;
  100677. + /** Periodic Frame Interval */
  100678. + unsigned perfrint:2;
  100679. +#define DWC_DCFG_FRAME_INTERVAL_80 0
  100680. +#define DWC_DCFG_FRAME_INTERVAL_85 1
  100681. +#define DWC_DCFG_FRAME_INTERVAL_90 2
  100682. +#define DWC_DCFG_FRAME_INTERVAL_95 3
  100683. +
  100684. + /** Enable Device OUT NAK for bulk in DDMA mode */
  100685. + unsigned endevoutnak:1;
  100686. +
  100687. + unsigned reserved14_17:4;
  100688. + /** In Endpoint Mis-match count */
  100689. + unsigned epmscnt:5;
  100690. + /** Enable Descriptor DMA in Device mode */
  100691. + unsigned descdma:1;
  100692. + unsigned perschintvl:2;
  100693. + unsigned resvalid:6;
  100694. + } b;
  100695. +} dcfg_data_t;
  100696. +
  100697. +/**
  100698. + * This union represents the bit fields in the Device Control
  100699. + * Register. Read the register into the <i>d32</i> member then
  100700. + * set/clear the bits using the <i>b</i>it elements.
  100701. + */
  100702. +typedef union dctl_data {
  100703. + /** raw register data */
  100704. + uint32_t d32;
  100705. + /** register bits */
  100706. + struct {
  100707. + /** Remote Wakeup */
  100708. + unsigned rmtwkupsig:1;
  100709. + /** Soft Disconnect */
  100710. + unsigned sftdiscon:1;
  100711. + /** Global Non-Periodic IN NAK Status */
  100712. + unsigned gnpinnaksts:1;
  100713. + /** Global OUT NAK Status */
  100714. + unsigned goutnaksts:1;
  100715. + /** Test Control */
  100716. + unsigned tstctl:3;
  100717. + /** Set Global Non-Periodic IN NAK */
  100718. + unsigned sgnpinnak:1;
  100719. + /** Clear Global Non-Periodic IN NAK */
  100720. + unsigned cgnpinnak:1;
  100721. + /** Set Global OUT NAK */
  100722. + unsigned sgoutnak:1;
  100723. + /** Clear Global OUT NAK */
  100724. + unsigned cgoutnak:1;
  100725. + /** Power-On Programming Done */
  100726. + unsigned pwronprgdone:1;
  100727. + /** Reserved */
  100728. + unsigned reserved:1;
  100729. + /** Global Multi Count */
  100730. + unsigned gmc:2;
  100731. + /** Ignore Frame Number for ISOC EPs */
  100732. + unsigned ifrmnum:1;
  100733. + /** NAK on Babble */
  100734. + unsigned nakonbble:1;
  100735. + /** Enable Continue on BNA */
  100736. + unsigned encontonbna:1;
  100737. +
  100738. + unsigned reserved18_31:14;
  100739. + } b;
  100740. +} dctl_data_t;
  100741. +
  100742. +/**
  100743. + * This union represents the bit fields in the Device Status
  100744. + * Register. Read the register into the <i>d32</i> member then
  100745. + * set/clear the bits using the <i>b</i>it elements.
  100746. + */
  100747. +typedef union dsts_data {
  100748. + /** raw register data */
  100749. + uint32_t d32;
  100750. + /** register bits */
  100751. + struct {
  100752. + /** Suspend Status */
  100753. + unsigned suspsts:1;
  100754. + /** Enumerated Speed */
  100755. + unsigned enumspd:2;
  100756. +#define DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ 0
  100757. +#define DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ 1
  100758. +#define DWC_DSTS_ENUMSPD_LS_PHY_6MHZ 2
  100759. +#define DWC_DSTS_ENUMSPD_FS_PHY_48MHZ 3
  100760. + /** Erratic Error */
  100761. + unsigned errticerr:1;
  100762. + unsigned reserved4_7:4;
  100763. + /** Frame or Microframe Number of the received SOF */
  100764. + unsigned soffn:14;
  100765. + unsigned reserved22_31:10;
  100766. + } b;
  100767. +} dsts_data_t;
  100768. +
  100769. +/**
  100770. + * This union represents the bit fields in the Device IN EP Interrupt
  100771. + * Register and the Device IN EP Common Mask Register.
  100772. + *
  100773. + * - Read the register into the <i>d32</i> member then set/clear the
  100774. + * bits using the <i>b</i>it elements.
  100775. + */
  100776. +typedef union diepint_data {
  100777. + /** raw register data */
  100778. + uint32_t d32;
  100779. + /** register bits */
  100780. + struct {
  100781. + /** Transfer complete mask */
  100782. + unsigned xfercompl:1;
  100783. + /** Endpoint disable mask */
  100784. + unsigned epdisabled:1;
  100785. + /** AHB Error mask */
  100786. + unsigned ahberr:1;
  100787. + /** TimeOUT Handshake mask (non-ISOC EPs) */
  100788. + unsigned timeout:1;
  100789. + /** IN Token received with TxF Empty mask */
  100790. + unsigned intktxfemp:1;
  100791. + /** IN Token Received with EP mismatch mask */
  100792. + unsigned intknepmis:1;
  100793. + /** IN Endpoint NAK Effective mask */
  100794. + unsigned inepnakeff:1;
  100795. + /** Reserved */
  100796. + unsigned emptyintr:1;
  100797. +
  100798. + unsigned txfifoundrn:1;
  100799. +
  100800. + /** BNA Interrupt mask */
  100801. + unsigned bna:1;
  100802. +
  100803. + unsigned reserved10_12:3;
  100804. + /** BNA Interrupt mask */
  100805. + unsigned nak:1;
  100806. +
  100807. + unsigned reserved14_31:18;
  100808. + } b;
  100809. +} diepint_data_t;
  100810. +
  100811. +/**
  100812. + * This union represents the bit fields in the Device IN EP
  100813. + * Common/Dedicated Interrupt Mask Register.
  100814. + */
  100815. +typedef union diepint_data diepmsk_data_t;
  100816. +
  100817. +/**
  100818. + * This union represents the bit fields in the Device OUT EP Interrupt
  100819. + * Registerand Device OUT EP Common Interrupt Mask Register.
  100820. + *
  100821. + * - Read the register into the <i>d32</i> member then set/clear the
  100822. + * bits using the <i>b</i>it elements.
  100823. + */
  100824. +typedef union doepint_data {
  100825. + /** raw register data */
  100826. + uint32_t d32;
  100827. + /** register bits */
  100828. + struct {
  100829. + /** Transfer complete */
  100830. + unsigned xfercompl:1;
  100831. + /** Endpoint disable */
  100832. + unsigned epdisabled:1;
  100833. + /** AHB Error */
  100834. + unsigned ahberr:1;
  100835. + /** Setup Phase Done (contorl EPs) */
  100836. + unsigned setup:1;
  100837. + /** OUT Token Received when Endpoint Disabled */
  100838. + unsigned outtknepdis:1;
  100839. +
  100840. + unsigned stsphsercvd:1;
  100841. + /** Back-to-Back SETUP Packets Received */
  100842. + unsigned back2backsetup:1;
  100843. +
  100844. + unsigned reserved7:1;
  100845. + /** OUT packet Error */
  100846. + unsigned outpkterr:1;
  100847. + /** BNA Interrupt */
  100848. + unsigned bna:1;
  100849. +
  100850. + unsigned reserved10:1;
  100851. + /** Packet Drop Status */
  100852. + unsigned pktdrpsts:1;
  100853. + /** Babble Interrupt */
  100854. + unsigned babble:1;
  100855. + /** NAK Interrupt */
  100856. + unsigned nak:1;
  100857. + /** NYET Interrupt */
  100858. + unsigned nyet:1;
  100859. + /** Bit indicating setup packet received */
  100860. + unsigned sr:1;
  100861. +
  100862. + unsigned reserved16_31:16;
  100863. + } b;
  100864. +} doepint_data_t;
  100865. +
  100866. +/**
  100867. + * This union represents the bit fields in the Device OUT EP
  100868. + * Common/Dedicated Interrupt Mask Register.
  100869. + */
  100870. +typedef union doepint_data doepmsk_data_t;
  100871. +
  100872. +/**
  100873. + * This union represents the bit fields in the Device All EP Interrupt
  100874. + * and Mask Registers.
  100875. + * - Read the register into the <i>d32</i> member then set/clear the
  100876. + * bits using the <i>b</i>it elements.
  100877. + */
  100878. +typedef union daint_data {
  100879. + /** raw register data */
  100880. + uint32_t d32;
  100881. + /** register bits */
  100882. + struct {
  100883. + /** IN Endpoint bits */
  100884. + unsigned in:16;
  100885. + /** OUT Endpoint bits */
  100886. + unsigned out:16;
  100887. + } ep;
  100888. + struct {
  100889. + /** IN Endpoint bits */
  100890. + unsigned inep0:1;
  100891. + unsigned inep1:1;
  100892. + unsigned inep2:1;
  100893. + unsigned inep3:1;
  100894. + unsigned inep4:1;
  100895. + unsigned inep5:1;
  100896. + unsigned inep6:1;
  100897. + unsigned inep7:1;
  100898. + unsigned inep8:1;
  100899. + unsigned inep9:1;
  100900. + unsigned inep10:1;
  100901. + unsigned inep11:1;
  100902. + unsigned inep12:1;
  100903. + unsigned inep13:1;
  100904. + unsigned inep14:1;
  100905. + unsigned inep15:1;
  100906. + /** OUT Endpoint bits */
  100907. + unsigned outep0:1;
  100908. + unsigned outep1:1;
  100909. + unsigned outep2:1;
  100910. + unsigned outep3:1;
  100911. + unsigned outep4:1;
  100912. + unsigned outep5:1;
  100913. + unsigned outep6:1;
  100914. + unsigned outep7:1;
  100915. + unsigned outep8:1;
  100916. + unsigned outep9:1;
  100917. + unsigned outep10:1;
  100918. + unsigned outep11:1;
  100919. + unsigned outep12:1;
  100920. + unsigned outep13:1;
  100921. + unsigned outep14:1;
  100922. + unsigned outep15:1;
  100923. + } b;
  100924. +} daint_data_t;
  100925. +
  100926. +/**
  100927. + * This union represents the bit fields in the Device IN Token Queue
  100928. + * Read Registers.
  100929. + * - Read the register into the <i>d32</i> member.
  100930. + * - READ-ONLY Register
  100931. + */
  100932. +typedef union dtknq1_data {
  100933. + /** raw register data */
  100934. + uint32_t d32;
  100935. + /** register bits */
  100936. + struct {
  100937. + /** In Token Queue Write Pointer */
  100938. + unsigned intknwptr:5;
  100939. + /** Reserved */
  100940. + unsigned reserved05_06:2;
  100941. + /** write pointer has wrapped. */
  100942. + unsigned wrap_bit:1;
  100943. + /** EP Numbers of IN Tokens 0 ... 4 */
  100944. + unsigned epnums0_5:24;
  100945. + } b;
  100946. +} dtknq1_data_t;
  100947. +
  100948. +/**
  100949. + * This union represents Threshold control Register
  100950. + * - Read and write the register into the <i>d32</i> member.
  100951. + * - READ-WRITABLE Register
  100952. + */
  100953. +typedef union dthrctl_data {
  100954. + /** raw register data */
  100955. + uint32_t d32;
  100956. + /** register bits */
  100957. + struct {
  100958. + /** non ISO Tx Thr. Enable */
  100959. + unsigned non_iso_thr_en:1;
  100960. + /** ISO Tx Thr. Enable */
  100961. + unsigned iso_thr_en:1;
  100962. + /** Tx Thr. Length */
  100963. + unsigned tx_thr_len:9;
  100964. + /** AHB Threshold ratio */
  100965. + unsigned ahb_thr_ratio:2;
  100966. + /** Reserved */
  100967. + unsigned reserved13_15:3;
  100968. + /** Rx Thr. Enable */
  100969. + unsigned rx_thr_en:1;
  100970. + /** Rx Thr. Length */
  100971. + unsigned rx_thr_len:9;
  100972. + unsigned reserved26:1;
  100973. + /** Arbiter Parking Enable*/
  100974. + unsigned arbprken:1;
  100975. + /** Reserved */
  100976. + unsigned reserved28_31:4;
  100977. + } b;
  100978. +} dthrctl_data_t;
  100979. +
  100980. +/**
  100981. + * Device Logical IN Endpoint-Specific Registers. <i>Offsets
  100982. + * 900h-AFCh</i>
  100983. + *
  100984. + * There will be one set of endpoint registers per logical endpoint
  100985. + * implemented.
  100986. + *
  100987. + * <i>These registers are visible only in Device mode and must not be
  100988. + * accessed in Host mode, as the results are unknown.</i>
  100989. + */
  100990. +typedef struct dwc_otg_dev_in_ep_regs {
  100991. + /** Device IN Endpoint Control Register. <i>Offset:900h +
  100992. + * (ep_num * 20h) + 00h</i> */
  100993. + volatile uint32_t diepctl;
  100994. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 04h</i> */
  100995. + uint32_t reserved04;
  100996. + /** Device IN Endpoint Interrupt Register. <i>Offset:900h +
  100997. + * (ep_num * 20h) + 08h</i> */
  100998. + volatile uint32_t diepint;
  100999. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 0Ch</i> */
  101000. + uint32_t reserved0C;
  101001. + /** Device IN Endpoint Transfer Size
  101002. + * Register. <i>Offset:900h + (ep_num * 20h) + 10h</i> */
  101003. + volatile uint32_t dieptsiz;
  101004. + /** Device IN Endpoint DMA Address Register. <i>Offset:900h +
  101005. + * (ep_num * 20h) + 14h</i> */
  101006. + volatile uint32_t diepdma;
  101007. + /** Device IN Endpoint Transmit FIFO Status Register. <i>Offset:900h +
  101008. + * (ep_num * 20h) + 18h</i> */
  101009. + volatile uint32_t dtxfsts;
  101010. + /** Device IN Endpoint DMA Buffer Register. <i>Offset:900h +
  101011. + * (ep_num * 20h) + 1Ch</i> */
  101012. + volatile uint32_t diepdmab;
  101013. +} dwc_otg_dev_in_ep_regs_t;
  101014. +
  101015. +/**
  101016. + * Device Logical OUT Endpoint-Specific Registers. <i>Offsets:
  101017. + * B00h-CFCh</i>
  101018. + *
  101019. + * There will be one set of endpoint registers per logical endpoint
  101020. + * implemented.
  101021. + *
  101022. + * <i>These registers are visible only in Device mode and must not be
  101023. + * accessed in Host mode, as the results are unknown.</i>
  101024. + */
  101025. +typedef struct dwc_otg_dev_out_ep_regs {
  101026. + /** Device OUT Endpoint Control Register. <i>Offset:B00h +
  101027. + * (ep_num * 20h) + 00h</i> */
  101028. + volatile uint32_t doepctl;
  101029. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 04h</i> */
  101030. + uint32_t reserved04;
  101031. + /** Device OUT Endpoint Interrupt Register. <i>Offset:B00h +
  101032. + * (ep_num * 20h) + 08h</i> */
  101033. + volatile uint32_t doepint;
  101034. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 0Ch</i> */
  101035. + uint32_t reserved0C;
  101036. + /** Device OUT Endpoint Transfer Size Register. <i>Offset:
  101037. + * B00h + (ep_num * 20h) + 10h</i> */
  101038. + volatile uint32_t doeptsiz;
  101039. + /** Device OUT Endpoint DMA Address Register. <i>Offset:B00h
  101040. + * + (ep_num * 20h) + 14h</i> */
  101041. + volatile uint32_t doepdma;
  101042. + /** Reserved. <i>Offset:B00h + * (ep_num * 20h) + 18h</i> */
  101043. + uint32_t unused;
  101044. + /** Device OUT Endpoint DMA Buffer Register. <i>Offset:B00h
  101045. + * + (ep_num * 20h) + 1Ch</i> */
  101046. + uint32_t doepdmab;
  101047. +} dwc_otg_dev_out_ep_regs_t;
  101048. +
  101049. +/**
  101050. + * This union represents the bit fields in the Device EP Control
  101051. + * Register. Read the register into the <i>d32</i> member then
  101052. + * set/clear the bits using the <i>b</i>it elements.
  101053. + */
  101054. +typedef union depctl_data {
  101055. + /** raw register data */
  101056. + uint32_t d32;
  101057. + /** register bits */
  101058. + struct {
  101059. + /** Maximum Packet Size
  101060. + * IN/OUT EPn
  101061. + * IN/OUT EP0 - 2 bits
  101062. + * 2'b00: 64 Bytes
  101063. + * 2'b01: 32
  101064. + * 2'b10: 16
  101065. + * 2'b11: 8 */
  101066. + unsigned mps:11;
  101067. +#define DWC_DEP0CTL_MPS_64 0
  101068. +#define DWC_DEP0CTL_MPS_32 1
  101069. +#define DWC_DEP0CTL_MPS_16 2
  101070. +#define DWC_DEP0CTL_MPS_8 3
  101071. +
  101072. + /** Next Endpoint
  101073. + * IN EPn/IN EP0
  101074. + * OUT EPn/OUT EP0 - reserved */
  101075. + unsigned nextep:4;
  101076. +
  101077. + /** USB Active Endpoint */
  101078. + unsigned usbactep:1;
  101079. +
  101080. + /** Endpoint DPID (INTR/Bulk IN and OUT endpoints)
  101081. + * This field contains the PID of the packet going to
  101082. + * be received or transmitted on this endpoint. The
  101083. + * application should program the PID of the first
  101084. + * packet going to be received or transmitted on this
  101085. + * endpoint , after the endpoint is
  101086. + * activated. Application use the SetD1PID and
  101087. + * SetD0PID fields of this register to program either
  101088. + * D0 or D1 PID.
  101089. + *
  101090. + * The encoding for this field is
  101091. + * - 0: D0
  101092. + * - 1: D1
  101093. + */
  101094. + unsigned dpid:1;
  101095. +
  101096. + /** NAK Status */
  101097. + unsigned naksts:1;
  101098. +
  101099. + /** Endpoint Type
  101100. + * 2'b00: Control
  101101. + * 2'b01: Isochronous
  101102. + * 2'b10: Bulk
  101103. + * 2'b11: Interrupt */
  101104. + unsigned eptype:2;
  101105. +
  101106. + /** Snoop Mode
  101107. + * OUT EPn/OUT EP0
  101108. + * IN EPn/IN EP0 - reserved */
  101109. + unsigned snp:1;
  101110. +
  101111. + /** Stall Handshake */
  101112. + unsigned stall:1;
  101113. +
  101114. + /** Tx Fifo Number
  101115. + * IN EPn/IN EP0
  101116. + * OUT EPn/OUT EP0 - reserved */
  101117. + unsigned txfnum:4;
  101118. +
  101119. + /** Clear NAK */
  101120. + unsigned cnak:1;
  101121. + /** Set NAK */
  101122. + unsigned snak:1;
  101123. + /** Set DATA0 PID (INTR/Bulk IN and OUT endpoints)
  101124. + * Writing to this field sets the Endpoint DPID (DPID)
  101125. + * field in this register to DATA0. Set Even
  101126. + * (micro)frame (SetEvenFr) (ISO IN and OUT Endpoints)
  101127. + * Writing to this field sets the Even/Odd
  101128. + * (micro)frame (EO_FrNum) field to even (micro)
  101129. + * frame.
  101130. + */
  101131. + unsigned setd0pid:1;
  101132. + /** Set DATA1 PID (INTR/Bulk IN and OUT endpoints)
  101133. + * Writing to this field sets the Endpoint DPID (DPID)
  101134. + * field in this register to DATA1 Set Odd
  101135. + * (micro)frame (SetOddFr) (ISO IN and OUT Endpoints)
  101136. + * Writing to this field sets the Even/Odd
  101137. + * (micro)frame (EO_FrNum) field to odd (micro) frame.
  101138. + */
  101139. + unsigned setd1pid:1;
  101140. +
  101141. + /** Endpoint Disable */
  101142. + unsigned epdis:1;
  101143. + /** Endpoint Enable */
  101144. + unsigned epena:1;
  101145. + } b;
  101146. +} depctl_data_t;
  101147. +
  101148. +/**
  101149. + * This union represents the bit fields in the Device EP Transfer
  101150. + * Size Register. Read the register into the <i>d32</i> member then
  101151. + * set/clear the bits using the <i>b</i>it elements.
  101152. + */
  101153. +typedef union deptsiz_data {
  101154. + /** raw register data */
  101155. + uint32_t d32;
  101156. + /** register bits */
  101157. + struct {
  101158. + /** Transfer size */
  101159. + unsigned xfersize:19;
  101160. +/** Max packet count for EP (pow(2,10)-1) */
  101161. +#define MAX_PKT_CNT 1023
  101162. + /** Packet Count */
  101163. + unsigned pktcnt:10;
  101164. + /** Multi Count - Periodic IN endpoints */
  101165. + unsigned mc:2;
  101166. + unsigned reserved:1;
  101167. + } b;
  101168. +} deptsiz_data_t;
  101169. +
  101170. +/**
  101171. + * This union represents the bit fields in the Device EP 0 Transfer
  101172. + * Size Register. Read the register into the <i>d32</i> member then
  101173. + * set/clear the bits using the <i>b</i>it elements.
  101174. + */
  101175. +typedef union deptsiz0_data {
  101176. + /** raw register data */
  101177. + uint32_t d32;
  101178. + /** register bits */
  101179. + struct {
  101180. + /** Transfer size */
  101181. + unsigned xfersize:7;
  101182. + /** Reserved */
  101183. + unsigned reserved7_18:12;
  101184. + /** Packet Count */
  101185. + unsigned pktcnt:2;
  101186. + /** Reserved */
  101187. + unsigned reserved21_28:8;
  101188. + /**Setup Packet Count (DOEPTSIZ0 Only) */
  101189. + unsigned supcnt:2;
  101190. + unsigned reserved31;
  101191. + } b;
  101192. +} deptsiz0_data_t;
  101193. +
  101194. +/////////////////////////////////////////////////
  101195. +// DMA Descriptor Specific Structures
  101196. +//
  101197. +
  101198. +/** Buffer status definitions */
  101199. +
  101200. +#define BS_HOST_READY 0x0
  101201. +#define BS_DMA_BUSY 0x1
  101202. +#define BS_DMA_DONE 0x2
  101203. +#define BS_HOST_BUSY 0x3
  101204. +
  101205. +/** Receive/Transmit status definitions */
  101206. +
  101207. +#define RTS_SUCCESS 0x0
  101208. +#define RTS_BUFFLUSH 0x1
  101209. +#define RTS_RESERVED 0x2
  101210. +#define RTS_BUFERR 0x3
  101211. +
  101212. +/**
  101213. + * This union represents the bit fields in the DMA Descriptor
  101214. + * status quadlet. Read the quadlet into the <i>d32</i> member then
  101215. + * set/clear the bits using the <i>b</i>it, <i>b_iso_out</i> and
  101216. + * <i>b_iso_in</i> elements.
  101217. + */
  101218. +typedef union dev_dma_desc_sts {
  101219. + /** raw register data */
  101220. + uint32_t d32;
  101221. + /** quadlet bits */
  101222. + struct {
  101223. + /** Received number of bytes */
  101224. + unsigned bytes:16;
  101225. + /** NAK bit - only for OUT EPs */
  101226. + unsigned nak:1;
  101227. + unsigned reserved17_22:6;
  101228. + /** Multiple Transfer - only for OUT EPs */
  101229. + unsigned mtrf:1;
  101230. + /** Setup Packet received - only for OUT EPs */
  101231. + unsigned sr:1;
  101232. + /** Interrupt On Complete */
  101233. + unsigned ioc:1;
  101234. + /** Short Packet */
  101235. + unsigned sp:1;
  101236. + /** Last */
  101237. + unsigned l:1;
  101238. + /** Receive Status */
  101239. + unsigned sts:2;
  101240. + /** Buffer Status */
  101241. + unsigned bs:2;
  101242. + } b;
  101243. +
  101244. +//#ifdef DWC_EN_ISOC
  101245. + /** iso out quadlet bits */
  101246. + struct {
  101247. + /** Received number of bytes */
  101248. + unsigned rxbytes:11;
  101249. +
  101250. + unsigned reserved11:1;
  101251. + /** Frame Number */
  101252. + unsigned framenum:11;
  101253. + /** Received ISO Data PID */
  101254. + unsigned pid:2;
  101255. + /** Interrupt On Complete */
  101256. + unsigned ioc:1;
  101257. + /** Short Packet */
  101258. + unsigned sp:1;
  101259. + /** Last */
  101260. + unsigned l:1;
  101261. + /** Receive Status */
  101262. + unsigned rxsts:2;
  101263. + /** Buffer Status */
  101264. + unsigned bs:2;
  101265. + } b_iso_out;
  101266. +
  101267. + /** iso in quadlet bits */
  101268. + struct {
  101269. + /** Transmited number of bytes */
  101270. + unsigned txbytes:12;
  101271. + /** Frame Number */
  101272. + unsigned framenum:11;
  101273. + /** Transmited ISO Data PID */
  101274. + unsigned pid:2;
  101275. + /** Interrupt On Complete */
  101276. + unsigned ioc:1;
  101277. + /** Short Packet */
  101278. + unsigned sp:1;
  101279. + /** Last */
  101280. + unsigned l:1;
  101281. + /** Transmit Status */
  101282. + unsigned txsts:2;
  101283. + /** Buffer Status */
  101284. + unsigned bs:2;
  101285. + } b_iso_in;
  101286. +//#endif /* DWC_EN_ISOC */
  101287. +} dev_dma_desc_sts_t;
  101288. +
  101289. +/**
  101290. + * DMA Descriptor structure
  101291. + *
  101292. + * DMA Descriptor structure contains two quadlets:
  101293. + * Status quadlet and Data buffer pointer.
  101294. + */
  101295. +typedef struct dwc_otg_dev_dma_desc {
  101296. + /** DMA Descriptor status quadlet */
  101297. + dev_dma_desc_sts_t status;
  101298. + /** DMA Descriptor data buffer pointer */
  101299. + uint32_t buf;
  101300. +} dwc_otg_dev_dma_desc_t;
  101301. +
  101302. +/**
  101303. + * The dwc_otg_dev_if structure contains information needed to manage
  101304. + * the DWC_otg controller acting in device mode. It represents the
  101305. + * programming view of the device-specific aspects of the controller.
  101306. + */
  101307. +typedef struct dwc_otg_dev_if {
  101308. + /** Pointer to device Global registers.
  101309. + * Device Global Registers starting at offset 800h
  101310. + */
  101311. + dwc_otg_device_global_regs_t *dev_global_regs;
  101312. +#define DWC_DEV_GLOBAL_REG_OFFSET 0x800
  101313. +
  101314. + /**
  101315. + * Device Logical IN Endpoint-Specific Registers 900h-AFCh
  101316. + */
  101317. + dwc_otg_dev_in_ep_regs_t *in_ep_regs[MAX_EPS_CHANNELS];
  101318. +#define DWC_DEV_IN_EP_REG_OFFSET 0x900
  101319. +#define DWC_EP_REG_OFFSET 0x20
  101320. +
  101321. + /** Device Logical OUT Endpoint-Specific Registers B00h-CFCh */
  101322. + dwc_otg_dev_out_ep_regs_t *out_ep_regs[MAX_EPS_CHANNELS];
  101323. +#define DWC_DEV_OUT_EP_REG_OFFSET 0xB00
  101324. +
  101325. + /* Device configuration information */
  101326. + uint8_t speed; /**< Device Speed 0: Unknown, 1: LS, 2:FS, 3: HS */
  101327. + uint8_t num_in_eps; /**< Number # of Tx EP range: 0-15 exept ep0 */
  101328. + uint8_t num_out_eps; /**< Number # of Rx EP range: 0-15 exept ep 0*/
  101329. +
  101330. + /** Size of periodic FIFOs (Bytes) */
  101331. + uint16_t perio_tx_fifo_size[MAX_PERIO_FIFOS];
  101332. +
  101333. + /** Size of Tx FIFOs (Bytes) */
  101334. + uint16_t tx_fifo_size[MAX_TX_FIFOS];
  101335. +
  101336. + /** Thresholding enable flags and length varaiables **/
  101337. + uint16_t rx_thr_en;
  101338. + uint16_t iso_tx_thr_en;
  101339. + uint16_t non_iso_tx_thr_en;
  101340. +
  101341. + uint16_t rx_thr_length;
  101342. + uint16_t tx_thr_length;
  101343. +
  101344. + /**
  101345. + * Pointers to the DMA Descriptors for EP0 Control
  101346. + * transfers (virtual and physical)
  101347. + */
  101348. +
  101349. + /** 2 descriptors for SETUP packets */
  101350. + dwc_dma_t dma_setup_desc_addr[2];
  101351. + dwc_otg_dev_dma_desc_t *setup_desc_addr[2];
  101352. +
  101353. + /** Pointer to Descriptor with latest SETUP packet */
  101354. + dwc_otg_dev_dma_desc_t *psetup;
  101355. +
  101356. + /** Index of current SETUP handler descriptor */
  101357. + uint32_t setup_desc_index;
  101358. +
  101359. + /** Descriptor for Data In or Status In phases */
  101360. + dwc_dma_t dma_in_desc_addr;
  101361. + dwc_otg_dev_dma_desc_t *in_desc_addr;
  101362. +
  101363. + /** Descriptor for Data Out or Status Out phases */
  101364. + dwc_dma_t dma_out_desc_addr;
  101365. + dwc_otg_dev_dma_desc_t *out_desc_addr;
  101366. +
  101367. + /** Setup Packet Detected - if set clear NAK when queueing */
  101368. + uint32_t spd;
  101369. + /** Isoc ep pointer on which incomplete happens */
  101370. + void *isoc_ep;
  101371. +
  101372. +} dwc_otg_dev_if_t;
  101373. +
  101374. +/////////////////////////////////////////////////
  101375. +// Host Mode Register Structures
  101376. +//
  101377. +/**
  101378. + * The Host Global Registers structure defines the size and relative
  101379. + * field offsets for the Host Mode Global Registers. Host Global
  101380. + * Registers offsets 400h-7FFh.
  101381. +*/
  101382. +typedef struct dwc_otg_host_global_regs {
  101383. + /** Host Configuration Register. <i>Offset: 400h</i> */
  101384. + volatile uint32_t hcfg;
  101385. + /** Host Frame Interval Register. <i>Offset: 404h</i> */
  101386. + volatile uint32_t hfir;
  101387. + /** Host Frame Number / Frame Remaining Register. <i>Offset: 408h</i> */
  101388. + volatile uint32_t hfnum;
  101389. + /** Reserved. <i>Offset: 40Ch</i> */
  101390. + uint32_t reserved40C;
  101391. + /** Host Periodic Transmit FIFO/ Queue Status Register. <i>Offset: 410h</i> */
  101392. + volatile uint32_t hptxsts;
  101393. + /** Host All Channels Interrupt Register. <i>Offset: 414h</i> */
  101394. + volatile uint32_t haint;
  101395. + /** Host All Channels Interrupt Mask Register. <i>Offset: 418h</i> */
  101396. + volatile uint32_t haintmsk;
  101397. + /** Host Frame List Base Address Register . <i>Offset: 41Ch</i> */
  101398. + volatile uint32_t hflbaddr;
  101399. +} dwc_otg_host_global_regs_t;
  101400. +
  101401. +/**
  101402. + * This union represents the bit fields in the Host Configuration Register.
  101403. + * Read the register into the <i>d32</i> member then set/clear the bits using
  101404. + * the <i>b</i>it elements. Write the <i>d32</i> member to the hcfg register.
  101405. + */
  101406. +typedef union hcfg_data {
  101407. + /** raw register data */
  101408. + uint32_t d32;
  101409. +
  101410. + /** register bits */
  101411. + struct {
  101412. + /** FS/LS Phy Clock Select */
  101413. + unsigned fslspclksel:2;
  101414. +#define DWC_HCFG_30_60_MHZ 0
  101415. +#define DWC_HCFG_48_MHZ 1
  101416. +#define DWC_HCFG_6_MHZ 2
  101417. +
  101418. + /** FS/LS Only Support */
  101419. + unsigned fslssupp:1;
  101420. + unsigned reserved3_6:4;
  101421. + /** Enable 32-KHz Suspend Mode */
  101422. + unsigned ena32khzs:1;
  101423. + /** Resume Validation Periiod */
  101424. + unsigned resvalid:8;
  101425. + unsigned reserved16_22:7;
  101426. + /** Enable Scatter/gather DMA in Host mode */
  101427. + unsigned descdma:1;
  101428. + /** Frame List Entries */
  101429. + unsigned frlisten:2;
  101430. + /** Enable Periodic Scheduling */
  101431. + unsigned perschedena:1;
  101432. + unsigned reserved27_30:4;
  101433. + unsigned modechtimen:1;
  101434. + } b;
  101435. +} hcfg_data_t;
  101436. +
  101437. +/**
  101438. + * This union represents the bit fields in the Host Frame Remaing/Number
  101439. + * Register.
  101440. + */
  101441. +typedef union hfir_data {
  101442. + /** raw register data */
  101443. + uint32_t d32;
  101444. +
  101445. + /** register bits */
  101446. + struct {
  101447. + unsigned frint:16;
  101448. + unsigned hfirrldctrl:1;
  101449. + unsigned reserved:15;
  101450. + } b;
  101451. +} hfir_data_t;
  101452. +
  101453. +/**
  101454. + * This union represents the bit fields in the Host Frame Remaing/Number
  101455. + * Register.
  101456. + */
  101457. +typedef union hfnum_data {
  101458. + /** raw register data */
  101459. + uint32_t d32;
  101460. +
  101461. + /** register bits */
  101462. + struct {
  101463. + unsigned frnum:16;
  101464. +#define DWC_HFNUM_MAX_FRNUM 0x3FFF
  101465. + unsigned frrem:16;
  101466. + } b;
  101467. +} hfnum_data_t;
  101468. +
  101469. +typedef union hptxsts_data {
  101470. + /** raw register data */
  101471. + uint32_t d32;
  101472. +
  101473. + /** register bits */
  101474. + struct {
  101475. + unsigned ptxfspcavail:16;
  101476. + unsigned ptxqspcavail:8;
  101477. + /** Top of the Periodic Transmit Request Queue
  101478. + * - bit 24 - Terminate (last entry for the selected channel)
  101479. + * - bits 26:25 - Token Type
  101480. + * - 2'b00 - Zero length
  101481. + * - 2'b01 - Ping
  101482. + * - 2'b10 - Disable
  101483. + * - bits 30:27 - Channel Number
  101484. + * - bit 31 - Odd/even microframe
  101485. + */
  101486. + unsigned ptxqtop_terminate:1;
  101487. + unsigned ptxqtop_token:2;
  101488. + unsigned ptxqtop_chnum:4;
  101489. + unsigned ptxqtop_odd:1;
  101490. + } b;
  101491. +} hptxsts_data_t;
  101492. +
  101493. +/**
  101494. + * This union represents the bit fields in the Host Port Control and Status
  101495. + * Register. Read the register into the <i>d32</i> member then set/clear the
  101496. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  101497. + * hprt0 register.
  101498. + */
  101499. +typedef union hprt0_data {
  101500. + /** raw register data */
  101501. + uint32_t d32;
  101502. + /** register bits */
  101503. + struct {
  101504. + unsigned prtconnsts:1;
  101505. + unsigned prtconndet:1;
  101506. + unsigned prtena:1;
  101507. + unsigned prtenchng:1;
  101508. + unsigned prtovrcurract:1;
  101509. + unsigned prtovrcurrchng:1;
  101510. + unsigned prtres:1;
  101511. + unsigned prtsusp:1;
  101512. + unsigned prtrst:1;
  101513. + unsigned reserved9:1;
  101514. + unsigned prtlnsts:2;
  101515. + unsigned prtpwr:1;
  101516. + unsigned prttstctl:4;
  101517. + unsigned prtspd:2;
  101518. +#define DWC_HPRT0_PRTSPD_HIGH_SPEED 0
  101519. +#define DWC_HPRT0_PRTSPD_FULL_SPEED 1
  101520. +#define DWC_HPRT0_PRTSPD_LOW_SPEED 2
  101521. + unsigned reserved19_31:13;
  101522. + } b;
  101523. +} hprt0_data_t;
  101524. +
  101525. +/**
  101526. + * This union represents the bit fields in the Host All Interrupt
  101527. + * Register.
  101528. + */
  101529. +typedef union haint_data {
  101530. + /** raw register data */
  101531. + uint32_t d32;
  101532. + /** register bits */
  101533. + struct {
  101534. + unsigned ch0:1;
  101535. + unsigned ch1:1;
  101536. + unsigned ch2:1;
  101537. + unsigned ch3:1;
  101538. + unsigned ch4:1;
  101539. + unsigned ch5:1;
  101540. + unsigned ch6:1;
  101541. + unsigned ch7:1;
  101542. + unsigned ch8:1;
  101543. + unsigned ch9:1;
  101544. + unsigned ch10:1;
  101545. + unsigned ch11:1;
  101546. + unsigned ch12:1;
  101547. + unsigned ch13:1;
  101548. + unsigned ch14:1;
  101549. + unsigned ch15:1;
  101550. + unsigned reserved:16;
  101551. + } b;
  101552. +
  101553. + struct {
  101554. + unsigned chint:16;
  101555. + unsigned reserved:16;
  101556. + } b2;
  101557. +} haint_data_t;
  101558. +
  101559. +/**
  101560. + * This union represents the bit fields in the Host All Interrupt
  101561. + * Register.
  101562. + */
  101563. +typedef union haintmsk_data {
  101564. + /** raw register data */
  101565. + uint32_t d32;
  101566. + /** register bits */
  101567. + struct {
  101568. + unsigned ch0:1;
  101569. + unsigned ch1:1;
  101570. + unsigned ch2:1;
  101571. + unsigned ch3:1;
  101572. + unsigned ch4:1;
  101573. + unsigned ch5:1;
  101574. + unsigned ch6:1;
  101575. + unsigned ch7:1;
  101576. + unsigned ch8:1;
  101577. + unsigned ch9:1;
  101578. + unsigned ch10:1;
  101579. + unsigned ch11:1;
  101580. + unsigned ch12:1;
  101581. + unsigned ch13:1;
  101582. + unsigned ch14:1;
  101583. + unsigned ch15:1;
  101584. + unsigned reserved:16;
  101585. + } b;
  101586. +
  101587. + struct {
  101588. + unsigned chint:16;
  101589. + unsigned reserved:16;
  101590. + } b2;
  101591. +} haintmsk_data_t;
  101592. +
  101593. +/**
  101594. + * Host Channel Specific Registers. <i>500h-5FCh</i>
  101595. + */
  101596. +typedef struct dwc_otg_hc_regs {
  101597. + /** Host Channel 0 Characteristic Register. <i>Offset: 500h + (chan_num * 20h) + 00h</i> */
  101598. + volatile uint32_t hcchar;
  101599. + /** Host Channel 0 Split Control Register. <i>Offset: 500h + (chan_num * 20h) + 04h</i> */
  101600. + volatile uint32_t hcsplt;
  101601. + /** Host Channel 0 Interrupt Register. <i>Offset: 500h + (chan_num * 20h) + 08h</i> */
  101602. + volatile uint32_t hcint;
  101603. + /** Host Channel 0 Interrupt Mask Register. <i>Offset: 500h + (chan_num * 20h) + 0Ch</i> */
  101604. + volatile uint32_t hcintmsk;
  101605. + /** Host Channel 0 Transfer Size Register. <i>Offset: 500h + (chan_num * 20h) + 10h</i> */
  101606. + volatile uint32_t hctsiz;
  101607. + /** Host Channel 0 DMA Address Register. <i>Offset: 500h + (chan_num * 20h) + 14h</i> */
  101608. + volatile uint32_t hcdma;
  101609. + volatile uint32_t reserved;
  101610. + /** Host Channel 0 DMA Buffer Address Register. <i>Offset: 500h + (chan_num * 20h) + 1Ch</i> */
  101611. + volatile uint32_t hcdmab;
  101612. +} dwc_otg_hc_regs_t;
  101613. +
  101614. +/**
  101615. + * This union represents the bit fields in the Host Channel Characteristics
  101616. + * Register. Read the register into the <i>d32</i> member then set/clear the
  101617. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  101618. + * hcchar register.
  101619. + */
  101620. +typedef union hcchar_data {
  101621. + /** raw register data */
  101622. + uint32_t d32;
  101623. +
  101624. + /** register bits */
  101625. + struct {
  101626. + /** Maximum packet size in bytes */
  101627. + unsigned mps:11;
  101628. +
  101629. + /** Endpoint number */
  101630. + unsigned epnum:4;
  101631. +
  101632. + /** 0: OUT, 1: IN */
  101633. + unsigned epdir:1;
  101634. +
  101635. + unsigned reserved:1;
  101636. +
  101637. + /** 0: Full/high speed device, 1: Low speed device */
  101638. + unsigned lspddev:1;
  101639. +
  101640. + /** 0: Control, 1: Isoc, 2: Bulk, 3: Intr */
  101641. + unsigned eptype:2;
  101642. +
  101643. + /** Packets per frame for periodic transfers. 0 is reserved. */
  101644. + unsigned multicnt:2;
  101645. +
  101646. + /** Device address */
  101647. + unsigned devaddr:7;
  101648. +
  101649. + /**
  101650. + * Frame to transmit periodic transaction.
  101651. + * 0: even, 1: odd
  101652. + */
  101653. + unsigned oddfrm:1;
  101654. +
  101655. + /** Channel disable */
  101656. + unsigned chdis:1;
  101657. +
  101658. + /** Channel enable */
  101659. + unsigned chen:1;
  101660. + } b;
  101661. +} hcchar_data_t;
  101662. +
  101663. +typedef union hcsplt_data {
  101664. + /** raw register data */
  101665. + uint32_t d32;
  101666. +
  101667. + /** register bits */
  101668. + struct {
  101669. + /** Port Address */
  101670. + unsigned prtaddr:7;
  101671. +
  101672. + /** Hub Address */
  101673. + unsigned hubaddr:7;
  101674. +
  101675. + /** Transaction Position */
  101676. + unsigned xactpos:2;
  101677. +#define DWC_HCSPLIT_XACTPOS_MID 0
  101678. +#define DWC_HCSPLIT_XACTPOS_END 1
  101679. +#define DWC_HCSPLIT_XACTPOS_BEGIN 2
  101680. +#define DWC_HCSPLIT_XACTPOS_ALL 3
  101681. +
  101682. + /** Do Complete Split */
  101683. + unsigned compsplt:1;
  101684. +
  101685. + /** Reserved */
  101686. + unsigned reserved:14;
  101687. +
  101688. + /** Split Enble */
  101689. + unsigned spltena:1;
  101690. + } b;
  101691. +} hcsplt_data_t;
  101692. +
  101693. +/**
  101694. + * This union represents the bit fields in the Host All Interrupt
  101695. + * Register.
  101696. + */
  101697. +typedef union hcint_data {
  101698. + /** raw register data */
  101699. + uint32_t d32;
  101700. + /** register bits */
  101701. + struct {
  101702. + /** Transfer Complete */
  101703. + unsigned xfercomp:1;
  101704. + /** Channel Halted */
  101705. + unsigned chhltd:1;
  101706. + /** AHB Error */
  101707. + unsigned ahberr:1;
  101708. + /** STALL Response Received */
  101709. + unsigned stall:1;
  101710. + /** NAK Response Received */
  101711. + unsigned nak:1;
  101712. + /** ACK Response Received */
  101713. + unsigned ack:1;
  101714. + /** NYET Response Received */
  101715. + unsigned nyet:1;
  101716. + /** Transaction Err */
  101717. + unsigned xacterr:1;
  101718. + /** Babble Error */
  101719. + unsigned bblerr:1;
  101720. + /** Frame Overrun */
  101721. + unsigned frmovrun:1;
  101722. + /** Data Toggle Error */
  101723. + unsigned datatglerr:1;
  101724. + /** Buffer Not Available (only for DDMA mode) */
  101725. + unsigned bna:1;
  101726. + /** Exessive transaction error (only for DDMA mode) */
  101727. + unsigned xcs_xact:1;
  101728. + /** Frame List Rollover interrupt */
  101729. + unsigned frm_list_roll:1;
  101730. + /** Reserved */
  101731. + unsigned reserved14_31:18;
  101732. + } b;
  101733. +} hcint_data_t;
  101734. +
  101735. +/**
  101736. + * This union represents the bit fields in the Host Channel Interrupt Mask
  101737. + * Register. Read the register into the <i>d32</i> member then set/clear the
  101738. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  101739. + * hcintmsk register.
  101740. + */
  101741. +typedef union hcintmsk_data {
  101742. + /** raw register data */
  101743. + uint32_t d32;
  101744. +
  101745. + /** register bits */
  101746. + struct {
  101747. + unsigned xfercompl:1;
  101748. + unsigned chhltd:1;
  101749. + unsigned ahberr:1;
  101750. + unsigned stall:1;
  101751. + unsigned nak:1;
  101752. + unsigned ack:1;
  101753. + unsigned nyet:1;
  101754. + unsigned xacterr:1;
  101755. + unsigned bblerr:1;
  101756. + unsigned frmovrun:1;
  101757. + unsigned datatglerr:1;
  101758. + unsigned bna:1;
  101759. + unsigned xcs_xact:1;
  101760. + unsigned frm_list_roll:1;
  101761. + unsigned reserved14_31:18;
  101762. + } b;
  101763. +} hcintmsk_data_t;
  101764. +
  101765. +/**
  101766. + * This union represents the bit fields in the Host Channel Transfer Size
  101767. + * Register. Read the register into the <i>d32</i> member then set/clear the
  101768. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  101769. + * hcchar register.
  101770. + */
  101771. +
  101772. +typedef union hctsiz_data {
  101773. + /** raw register data */
  101774. + uint32_t d32;
  101775. +
  101776. + /** register bits */
  101777. + struct {
  101778. + /** Total transfer size in bytes */
  101779. + unsigned xfersize:19;
  101780. +
  101781. + /** Data packets to transfer */
  101782. + unsigned pktcnt:10;
  101783. +
  101784. + /**
  101785. + * Packet ID for next data packet
  101786. + * 0: DATA0
  101787. + * 1: DATA2
  101788. + * 2: DATA1
  101789. + * 3: MDATA (non-Control), SETUP (Control)
  101790. + */
  101791. + unsigned pid:2;
  101792. +#define DWC_HCTSIZ_DATA0 0
  101793. +#define DWC_HCTSIZ_DATA1 2
  101794. +#define DWC_HCTSIZ_DATA2 1
  101795. +#define DWC_HCTSIZ_MDATA 3
  101796. +#define DWC_HCTSIZ_SETUP 3
  101797. +
  101798. + /** Do PING protocol when 1 */
  101799. + unsigned dopng:1;
  101800. + } b;
  101801. +
  101802. + /** register bits */
  101803. + struct {
  101804. + /** Scheduling information */
  101805. + unsigned schinfo:8;
  101806. +
  101807. + /** Number of transfer descriptors.
  101808. + * Max value:
  101809. + * 64 in general,
  101810. + * 256 only for HS isochronous endpoint.
  101811. + */
  101812. + unsigned ntd:8;
  101813. +
  101814. + /** Data packets to transfer */
  101815. + unsigned reserved16_28:13;
  101816. +
  101817. + /**
  101818. + * Packet ID for next data packet
  101819. + * 0: DATA0
  101820. + * 1: DATA2
  101821. + * 2: DATA1
  101822. + * 3: MDATA (non-Control)
  101823. + */
  101824. + unsigned pid:2;
  101825. +
  101826. + /** Do PING protocol when 1 */
  101827. + unsigned dopng:1;
  101828. + } b_ddma;
  101829. +} hctsiz_data_t;
  101830. +
  101831. +/**
  101832. + * This union represents the bit fields in the Host DMA Address
  101833. + * Register used in Descriptor DMA mode.
  101834. + */
  101835. +typedef union hcdma_data {
  101836. + /** raw register data */
  101837. + uint32_t d32;
  101838. + /** register bits */
  101839. + struct {
  101840. + unsigned reserved0_2:3;
  101841. + /** Current Transfer Descriptor. Not used for ISOC */
  101842. + unsigned ctd:8;
  101843. + /** Start Address of Descriptor List */
  101844. + unsigned dma_addr:21;
  101845. + } b;
  101846. +} hcdma_data_t;
  101847. +
  101848. +/**
  101849. + * This union represents the bit fields in the DMA Descriptor
  101850. + * status quadlet for host mode. Read the quadlet into the <i>d32</i> member then
  101851. + * set/clear the bits using the <i>b</i>it elements.
  101852. + */
  101853. +typedef union host_dma_desc_sts {
  101854. + /** raw register data */
  101855. + uint32_t d32;
  101856. + /** quadlet bits */
  101857. +
  101858. + /* for non-isochronous */
  101859. + struct {
  101860. + /** Number of bytes */
  101861. + unsigned n_bytes:17;
  101862. + /** QTD offset to jump when Short Packet received - only for IN EPs */
  101863. + unsigned qtd_offset:6;
  101864. + /**
  101865. + * Set to request the core to jump to alternate QTD if
  101866. + * Short Packet received - only for IN EPs
  101867. + */
  101868. + unsigned a_qtd:1;
  101869. + /**
  101870. + * Setup Packet bit. When set indicates that buffer contains
  101871. + * setup packet.
  101872. + */
  101873. + unsigned sup:1;
  101874. + /** Interrupt On Complete */
  101875. + unsigned ioc:1;
  101876. + /** End of List */
  101877. + unsigned eol:1;
  101878. + unsigned reserved27:1;
  101879. + /** Rx/Tx Status */
  101880. + unsigned sts:2;
  101881. +#define DMA_DESC_STS_PKTERR 1
  101882. + unsigned reserved30:1;
  101883. + /** Active Bit */
  101884. + unsigned a:1;
  101885. + } b;
  101886. + /* for isochronous */
  101887. + struct {
  101888. + /** Number of bytes */
  101889. + unsigned n_bytes:12;
  101890. + unsigned reserved12_24:13;
  101891. + /** Interrupt On Complete */
  101892. + unsigned ioc:1;
  101893. + unsigned reserved26_27:2;
  101894. + /** Rx/Tx Status */
  101895. + unsigned sts:2;
  101896. + unsigned reserved30:1;
  101897. + /** Active Bit */
  101898. + unsigned a:1;
  101899. + } b_isoc;
  101900. +} host_dma_desc_sts_t;
  101901. +
  101902. +#define MAX_DMA_DESC_SIZE 131071
  101903. +#define MAX_DMA_DESC_NUM_GENERIC 64
  101904. +#define MAX_DMA_DESC_NUM_HS_ISOC 256
  101905. +#define MAX_FRLIST_EN_NUM 64
  101906. +/**
  101907. + * Host-mode DMA Descriptor structure
  101908. + *
  101909. + * DMA Descriptor structure contains two quadlets:
  101910. + * Status quadlet and Data buffer pointer.
  101911. + */
  101912. +typedef struct dwc_otg_host_dma_desc {
  101913. + /** DMA Descriptor status quadlet */
  101914. + host_dma_desc_sts_t status;
  101915. + /** DMA Descriptor data buffer pointer */
  101916. + uint32_t buf;
  101917. +} dwc_otg_host_dma_desc_t;
  101918. +
  101919. +/** OTG Host Interface Structure.
  101920. + *
  101921. + * The OTG Host Interface Structure structure contains information
  101922. + * needed to manage the DWC_otg controller acting in host mode. It
  101923. + * represents the programming view of the host-specific aspects of the
  101924. + * controller.
  101925. + */
  101926. +typedef struct dwc_otg_host_if {
  101927. + /** Host Global Registers starting at offset 400h.*/
  101928. + dwc_otg_host_global_regs_t *host_global_regs;
  101929. +#define DWC_OTG_HOST_GLOBAL_REG_OFFSET 0x400
  101930. +
  101931. + /** Host Port 0 Control and Status Register */
  101932. + volatile uint32_t *hprt0;
  101933. +#define DWC_OTG_HOST_PORT_REGS_OFFSET 0x440
  101934. +
  101935. + /** Host Channel Specific Registers at offsets 500h-5FCh. */
  101936. + dwc_otg_hc_regs_t *hc_regs[MAX_EPS_CHANNELS];
  101937. +#define DWC_OTG_HOST_CHAN_REGS_OFFSET 0x500
  101938. +#define DWC_OTG_CHAN_REGS_OFFSET 0x20
  101939. +
  101940. + /* Host configuration information */
  101941. + /** Number of Host Channels (range: 1-16) */
  101942. + uint8_t num_host_channels;
  101943. + /** Periodic EPs supported (0: no, 1: yes) */
  101944. + uint8_t perio_eps_supported;
  101945. + /** Periodic Tx FIFO Size (Only 1 host periodic Tx FIFO) */
  101946. + uint16_t perio_tx_fifo_size;
  101947. +
  101948. +} dwc_otg_host_if_t;
  101949. +
  101950. +/**
  101951. + * This union represents the bit fields in the Power and Clock Gating Control
  101952. + * Register. Read the register into the <i>d32</i> member then set/clear the
  101953. + * bits using the <i>b</i>it elements.
  101954. + */
  101955. +typedef union pcgcctl_data {
  101956. + /** raw register data */
  101957. + uint32_t d32;
  101958. +
  101959. + /** register bits */
  101960. + struct {
  101961. + /** Stop Pclk */
  101962. + unsigned stoppclk:1;
  101963. + /** Gate Hclk */
  101964. + unsigned gatehclk:1;
  101965. + /** Power Clamp */
  101966. + unsigned pwrclmp:1;
  101967. + /** Reset Power Down Modules */
  101968. + unsigned rstpdwnmodule:1;
  101969. + /** Reserved */
  101970. + unsigned reserved:1;
  101971. + /** Enable Sleep Clock Gating (Enbl_L1Gating) */
  101972. + unsigned enbl_sleep_gating:1;
  101973. + /** PHY In Sleep (PhySleep) */
  101974. + unsigned phy_in_sleep:1;
  101975. + /** Deep Sleep*/
  101976. + unsigned deep_sleep:1;
  101977. + unsigned resetaftsusp:1;
  101978. + unsigned restoremode:1;
  101979. + unsigned enbl_extnd_hiber:1;
  101980. + unsigned extnd_hiber_pwrclmp:1;
  101981. + unsigned extnd_hiber_switch:1;
  101982. + unsigned ess_reg_restored:1;
  101983. + unsigned prt_clk_sel:2;
  101984. + unsigned port_power:1;
  101985. + unsigned max_xcvrselect:2;
  101986. + unsigned max_termsel:1;
  101987. + unsigned mac_dev_addr:7;
  101988. + unsigned p2hd_dev_enum_spd:2;
  101989. + unsigned p2hd_prt_spd:2;
  101990. + unsigned if_dev_mode:1;
  101991. + } b;
  101992. +} pcgcctl_data_t;
  101993. +
  101994. +/**
  101995. + * This union represents the bit fields in the Global Data FIFO Software
  101996. + * Configuration Register. Read the register into the <i>d32</i> member then
  101997. + * set/clear the bits using the <i>b</i>it elements.
  101998. + */
  101999. +typedef union gdfifocfg_data {
  102000. + /* raw register data */
  102001. + uint32_t d32;
  102002. + /** register bits */
  102003. + struct {
  102004. + /** OTG Data FIFO depth */
  102005. + unsigned gdfifocfg:16;
  102006. + /** Start address of EP info controller */
  102007. + unsigned epinfobase:16;
  102008. + } b;
  102009. +} gdfifocfg_data_t;
  102010. +
  102011. +/**
  102012. + * This union represents the bit fields in the Global Power Down Register
  102013. + * Register. Read the register into the <i>d32</i> member then set/clear the
  102014. + * bits using the <i>b</i>it elements.
  102015. + */
  102016. +typedef union gpwrdn_data {
  102017. + /* raw register data */
  102018. + uint32_t d32;
  102019. +
  102020. + /** register bits */
  102021. + struct {
  102022. + /** PMU Interrupt Select */
  102023. + unsigned pmuintsel:1;
  102024. + /** PMU Active */
  102025. + unsigned pmuactv:1;
  102026. + /** Restore */
  102027. + unsigned restore:1;
  102028. + /** Power Down Clamp */
  102029. + unsigned pwrdnclmp:1;
  102030. + /** Power Down Reset */
  102031. + unsigned pwrdnrstn:1;
  102032. + /** Power Down Switch */
  102033. + unsigned pwrdnswtch:1;
  102034. + /** Disable VBUS */
  102035. + unsigned dis_vbus:1;
  102036. + /** Line State Change */
  102037. + unsigned lnstschng:1;
  102038. + /** Line state change mask */
  102039. + unsigned lnstchng_msk:1;
  102040. + /** Reset Detected */
  102041. + unsigned rst_det:1;
  102042. + /** Reset Detect mask */
  102043. + unsigned rst_det_msk:1;
  102044. + /** Disconnect Detected */
  102045. + unsigned disconn_det:1;
  102046. + /** Disconnect Detect mask */
  102047. + unsigned disconn_det_msk:1;
  102048. + /** Connect Detected*/
  102049. + unsigned connect_det:1;
  102050. + /** Connect Detected Mask*/
  102051. + unsigned connect_det_msk:1;
  102052. + /** SRP Detected */
  102053. + unsigned srp_det:1;
  102054. + /** SRP Detect mask */
  102055. + unsigned srp_det_msk:1;
  102056. + /** Status Change Interrupt */
  102057. + unsigned sts_chngint:1;
  102058. + /** Status Change Interrupt Mask */
  102059. + unsigned sts_chngint_msk:1;
  102060. + /** Line State */
  102061. + unsigned linestate:2;
  102062. + /** Indicates current mode(status of IDDIG signal) */
  102063. + unsigned idsts:1;
  102064. + /** B Session Valid signal status*/
  102065. + unsigned bsessvld:1;
  102066. + /** ADP Event Detected */
  102067. + unsigned adp_int:1;
  102068. + /** Multi Valued ID pin */
  102069. + unsigned mult_val_id_bc:5;
  102070. + /** Reserved 24_31 */
  102071. + unsigned reserved29_31:3;
  102072. + } b;
  102073. +} gpwrdn_data_t;
  102074. +
  102075. +#endif
  102076. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/Makefile linux-3.12.33-rpi/drivers/usb/host/dwc_otg/Makefile
  102077. --- linux-3.12.33/drivers/usb/host/dwc_otg/Makefile 1969-12-31 18:00:00.000000000 -0600
  102078. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/Makefile 2014-12-03 19:13:40.216418001 -0600
  102079. @@ -0,0 +1,82 @@
  102080. +#
  102081. +# Makefile for DWC_otg Highspeed USB controller driver
  102082. +#
  102083. +
  102084. +ifneq ($(KERNELRELEASE),)
  102085. +
  102086. +# Use the BUS_INTERFACE variable to compile the software for either
  102087. +# PCI(PCI_INTERFACE) or LM(LM_INTERFACE) bus.
  102088. +ifeq ($(BUS_INTERFACE),)
  102089. +# BUS_INTERFACE = -DPCI_INTERFACE
  102090. +# BUS_INTERFACE = -DLM_INTERFACE
  102091. + BUS_INTERFACE = -DPLATFORM_INTERFACE
  102092. +endif
  102093. +
  102094. +#ccflags-y += -DDEBUG
  102095. +#ccflags-y += -DDWC_OTG_DEBUGLEV=1 # reduce common debug msgs
  102096. +
  102097. +# Use one of the following flags to compile the software in host-only or
  102098. +# device-only mode.
  102099. +#ccflags-y += -DDWC_HOST_ONLY
  102100. +#ccflags-y += -DDWC_DEVICE_ONLY
  102101. +
  102102. +ccflags-y += -Dlinux -DDWC_HS_ELECT_TST
  102103. +#ccflags-y += -DDWC_EN_ISOC
  102104. +ccflags-y += -I$(obj)/../dwc_common_port
  102105. +#ccflags-y += -I$(PORTLIB)
  102106. +ccflags-y += -DDWC_LINUX
  102107. +ccflags-y += $(CFI)
  102108. +ccflags-y += $(BUS_INTERFACE)
  102109. +#ccflags-y += -DDWC_DEV_SRPCAP
  102110. +
  102111. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg.o
  102112. +
  102113. +dwc_otg-objs := dwc_otg_driver.o dwc_otg_attr.o
  102114. +dwc_otg-objs += dwc_otg_cil.o dwc_otg_cil_intr.o
  102115. +dwc_otg-objs += dwc_otg_pcd_linux.o dwc_otg_pcd.o dwc_otg_pcd_intr.o
  102116. +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
  102117. +dwc_otg-objs += dwc_otg_adp.o
  102118. +dwc_otg-objs += dwc_otg_fiq_fsm.o
  102119. +dwc_otg-objs += dwc_otg_fiq_stub.o
  102120. +ifneq ($(CFI),)
  102121. +dwc_otg-objs += dwc_otg_cfi.o
  102122. +endif
  102123. +
  102124. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  102125. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  102126. +
  102127. +ifneq ($(kernrel3),2.6.20)
  102128. +ccflags-y += $(CPPFLAGS)
  102129. +endif
  102130. +
  102131. +else
  102132. +
  102133. +PWD := $(shell pwd)
  102134. +PORTLIB := $(PWD)/../dwc_common_port
  102135. +
  102136. +# Command paths
  102137. +CTAGS := $(CTAGS)
  102138. +DOXYGEN := $(DOXYGEN)
  102139. +
  102140. +default: portlib
  102141. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  102142. +
  102143. +install: default
  102144. + $(MAKE) -C$(KDIR) M=$(PORTLIB) modules_install
  102145. + $(MAKE) -C$(KDIR) M=$(PWD) modules_install
  102146. +
  102147. +portlib:
  102148. + $(MAKE) -C$(KDIR) M=$(PORTLIB) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  102149. + cp $(PORTLIB)/Module.symvers $(PWD)/
  102150. +
  102151. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  102152. + $(DOXYGEN) doc/doxygen.cfg
  102153. +
  102154. +tags: $(wildcard *.[hc])
  102155. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  102156. +
  102157. +
  102158. +clean:
  102159. + rm -rf *.o *.ko .*cmd *.mod.c .tmp_versions Module.symvers
  102160. +
  102161. +endif
  102162. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm linux-3.12.33-rpi/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
  102163. --- linux-3.12.33/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm 1969-12-31 18:00:00.000000000 -0600
  102164. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm 2014-12-03 19:13:40.224418001 -0600
  102165. @@ -0,0 +1,337 @@
  102166. +package dwc_otg_test;
  102167. +
  102168. +use strict;
  102169. +use Exporter ();
  102170. +
  102171. +use vars qw(@ISA @EXPORT
  102172. +$sysfsdir $paramdir $errors $params
  102173. +);
  102174. +
  102175. +@ISA = qw(Exporter);
  102176. +
  102177. +#
  102178. +# Globals
  102179. +#
  102180. +$sysfsdir = "/sys/devices/lm0";
  102181. +$paramdir = "/sys/module/dwc_otg";
  102182. +$errors = 0;
  102183. +
  102184. +$params = [
  102185. + {
  102186. + NAME => "otg_cap",
  102187. + DEFAULT => 0,
  102188. + ENUM => [],
  102189. + LOW => 0,
  102190. + HIGH => 2
  102191. + },
  102192. + {
  102193. + NAME => "dma_enable",
  102194. + DEFAULT => 0,
  102195. + ENUM => [],
  102196. + LOW => 0,
  102197. + HIGH => 1
  102198. + },
  102199. + {
  102200. + NAME => "dma_burst_size",
  102201. + DEFAULT => 32,
  102202. + ENUM => [1, 4, 8, 16, 32, 64, 128, 256],
  102203. + LOW => 1,
  102204. + HIGH => 256
  102205. + },
  102206. + {
  102207. + NAME => "host_speed",
  102208. + DEFAULT => 0,
  102209. + ENUM => [],
  102210. + LOW => 0,
  102211. + HIGH => 1
  102212. + },
  102213. + {
  102214. + NAME => "host_support_fs_ls_low_power",
  102215. + DEFAULT => 0,
  102216. + ENUM => [],
  102217. + LOW => 0,
  102218. + HIGH => 1
  102219. + },
  102220. + {
  102221. + NAME => "host_ls_low_power_phy_clk",
  102222. + DEFAULT => 0,
  102223. + ENUM => [],
  102224. + LOW => 0,
  102225. + HIGH => 1
  102226. + },
  102227. + {
  102228. + NAME => "dev_speed",
  102229. + DEFAULT => 0,
  102230. + ENUM => [],
  102231. + LOW => 0,
  102232. + HIGH => 1
  102233. + },
  102234. + {
  102235. + NAME => "enable_dynamic_fifo",
  102236. + DEFAULT => 1,
  102237. + ENUM => [],
  102238. + LOW => 0,
  102239. + HIGH => 1
  102240. + },
  102241. + {
  102242. + NAME => "data_fifo_size",
  102243. + DEFAULT => 8192,
  102244. + ENUM => [],
  102245. + LOW => 32,
  102246. + HIGH => 32768
  102247. + },
  102248. + {
  102249. + NAME => "dev_rx_fifo_size",
  102250. + DEFAULT => 1064,
  102251. + ENUM => [],
  102252. + LOW => 16,
  102253. + HIGH => 32768
  102254. + },
  102255. + {
  102256. + NAME => "dev_nperio_tx_fifo_size",
  102257. + DEFAULT => 1024,
  102258. + ENUM => [],
  102259. + LOW => 16,
  102260. + HIGH => 32768
  102261. + },
  102262. + {
  102263. + NAME => "dev_perio_tx_fifo_size_1",
  102264. + DEFAULT => 256,
  102265. + ENUM => [],
  102266. + LOW => 4,
  102267. + HIGH => 768
  102268. + },
  102269. + {
  102270. + NAME => "dev_perio_tx_fifo_size_2",
  102271. + DEFAULT => 256,
  102272. + ENUM => [],
  102273. + LOW => 4,
  102274. + HIGH => 768
  102275. + },
  102276. + {
  102277. + NAME => "dev_perio_tx_fifo_size_3",
  102278. + DEFAULT => 256,
  102279. + ENUM => [],
  102280. + LOW => 4,
  102281. + HIGH => 768
  102282. + },
  102283. + {
  102284. + NAME => "dev_perio_tx_fifo_size_4",
  102285. + DEFAULT => 256,
  102286. + ENUM => [],
  102287. + LOW => 4,
  102288. + HIGH => 768
  102289. + },
  102290. + {
  102291. + NAME => "dev_perio_tx_fifo_size_5",
  102292. + DEFAULT => 256,
  102293. + ENUM => [],
  102294. + LOW => 4,
  102295. + HIGH => 768
  102296. + },
  102297. + {
  102298. + NAME => "dev_perio_tx_fifo_size_6",
  102299. + DEFAULT => 256,
  102300. + ENUM => [],
  102301. + LOW => 4,
  102302. + HIGH => 768
  102303. + },
  102304. + {
  102305. + NAME => "dev_perio_tx_fifo_size_7",
  102306. + DEFAULT => 256,
  102307. + ENUM => [],
  102308. + LOW => 4,
  102309. + HIGH => 768
  102310. + },
  102311. + {
  102312. + NAME => "dev_perio_tx_fifo_size_8",
  102313. + DEFAULT => 256,
  102314. + ENUM => [],
  102315. + LOW => 4,
  102316. + HIGH => 768
  102317. + },
  102318. + {
  102319. + NAME => "dev_perio_tx_fifo_size_9",
  102320. + DEFAULT => 256,
  102321. + ENUM => [],
  102322. + LOW => 4,
  102323. + HIGH => 768
  102324. + },
  102325. + {
  102326. + NAME => "dev_perio_tx_fifo_size_10",
  102327. + DEFAULT => 256,
  102328. + ENUM => [],
  102329. + LOW => 4,
  102330. + HIGH => 768
  102331. + },
  102332. + {
  102333. + NAME => "dev_perio_tx_fifo_size_11",
  102334. + DEFAULT => 256,
  102335. + ENUM => [],
  102336. + LOW => 4,
  102337. + HIGH => 768
  102338. + },
  102339. + {
  102340. + NAME => "dev_perio_tx_fifo_size_12",
  102341. + DEFAULT => 256,
  102342. + ENUM => [],
  102343. + LOW => 4,
  102344. + HIGH => 768
  102345. + },
  102346. + {
  102347. + NAME => "dev_perio_tx_fifo_size_13",
  102348. + DEFAULT => 256,
  102349. + ENUM => [],
  102350. + LOW => 4,
  102351. + HIGH => 768
  102352. + },
  102353. + {
  102354. + NAME => "dev_perio_tx_fifo_size_14",
  102355. + DEFAULT => 256,
  102356. + ENUM => [],
  102357. + LOW => 4,
  102358. + HIGH => 768
  102359. + },
  102360. + {
  102361. + NAME => "dev_perio_tx_fifo_size_15",
  102362. + DEFAULT => 256,
  102363. + ENUM => [],
  102364. + LOW => 4,
  102365. + HIGH => 768
  102366. + },
  102367. + {
  102368. + NAME => "host_rx_fifo_size",
  102369. + DEFAULT => 1024,
  102370. + ENUM => [],
  102371. + LOW => 16,
  102372. + HIGH => 32768
  102373. + },
  102374. + {
  102375. + NAME => "host_nperio_tx_fifo_size",
  102376. + DEFAULT => 1024,
  102377. + ENUM => [],
  102378. + LOW => 16,
  102379. + HIGH => 32768
  102380. + },
  102381. + {
  102382. + NAME => "host_perio_tx_fifo_size",
  102383. + DEFAULT => 1024,
  102384. + ENUM => [],
  102385. + LOW => 16,
  102386. + HIGH => 32768
  102387. + },
  102388. + {
  102389. + NAME => "max_transfer_size",
  102390. + DEFAULT => 65535,
  102391. + ENUM => [],
  102392. + LOW => 2047,
  102393. + HIGH => 65535
  102394. + },
  102395. + {
  102396. + NAME => "max_packet_count",
  102397. + DEFAULT => 511,
  102398. + ENUM => [],
  102399. + LOW => 15,
  102400. + HIGH => 511
  102401. + },
  102402. + {
  102403. + NAME => "host_channels",
  102404. + DEFAULT => 12,
  102405. + ENUM => [],
  102406. + LOW => 1,
  102407. + HIGH => 16
  102408. + },
  102409. + {
  102410. + NAME => "dev_endpoints",
  102411. + DEFAULT => 6,
  102412. + ENUM => [],
  102413. + LOW => 1,
  102414. + HIGH => 15
  102415. + },
  102416. + {
  102417. + NAME => "phy_type",
  102418. + DEFAULT => 1,
  102419. + ENUM => [],
  102420. + LOW => 0,
  102421. + HIGH => 2
  102422. + },
  102423. + {
  102424. + NAME => "phy_utmi_width",
  102425. + DEFAULT => 16,
  102426. + ENUM => [8, 16],
  102427. + LOW => 8,
  102428. + HIGH => 16
  102429. + },
  102430. + {
  102431. + NAME => "phy_ulpi_ddr",
  102432. + DEFAULT => 0,
  102433. + ENUM => [],
  102434. + LOW => 0,
  102435. + HIGH => 1
  102436. + },
  102437. + ];
  102438. +
  102439. +
  102440. +#
  102441. +#
  102442. +sub check_arch {
  102443. + $_ = `uname -m`;
  102444. + chomp;
  102445. + unless (m/armv4tl/) {
  102446. + warn "# \n# Can't execute on $_. Run on integrator platform.\n# \n";
  102447. + return 0;
  102448. + }
  102449. + return 1;
  102450. +}
  102451. +
  102452. +#
  102453. +#
  102454. +sub load_module {
  102455. + my $params = shift;
  102456. + print "\nRemoving Module\n";
  102457. + system "rmmod dwc_otg";
  102458. + print "Loading Module\n";
  102459. + if ($params ne "") {
  102460. + print "Module Parameters: $params\n";
  102461. + }
  102462. + if (system("modprobe dwc_otg $params")) {
  102463. + warn "Unable to load module\n";
  102464. + return 0;
  102465. + }
  102466. + return 1;
  102467. +}
  102468. +
  102469. +#
  102470. +#
  102471. +sub test_status {
  102472. + my $arg = shift;
  102473. +
  102474. + print "\n";
  102475. +
  102476. + if (defined $arg) {
  102477. + warn "WARNING: $arg\n";
  102478. + }
  102479. +
  102480. + if ($errors > 0) {
  102481. + warn "TEST FAILED with $errors errors\n";
  102482. + return 0;
  102483. + } else {
  102484. + print "TEST PASSED\n";
  102485. + return 0 if (defined $arg);
  102486. + }
  102487. + return 1;
  102488. +}
  102489. +
  102490. +#
  102491. +#
  102492. +@EXPORT = qw(
  102493. +$sysfsdir
  102494. +$paramdir
  102495. +$params
  102496. +$errors
  102497. +check_arch
  102498. +load_module
  102499. +test_status
  102500. +);
  102501. +
  102502. +1;
  102503. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/test/Makefile linux-3.12.33-rpi/drivers/usb/host/dwc_otg/test/Makefile
  102504. --- linux-3.12.33/drivers/usb/host/dwc_otg/test/Makefile 1969-12-31 18:00:00.000000000 -0600
  102505. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/test/Makefile 2014-12-03 19:13:40.224418001 -0600
  102506. @@ -0,0 +1,16 @@
  102507. +
  102508. +PERL=/usr/bin/perl
  102509. +PL_TESTS=test_sysfs.pl test_mod_param.pl
  102510. +
  102511. +.PHONY : test
  102512. +test : perl_tests
  102513. +
  102514. +perl_tests :
  102515. + @echo
  102516. + @echo Running perl tests
  102517. + @for test in $(PL_TESTS); do \
  102518. + if $(PERL) ./$$test ; then \
  102519. + echo "=======> $$test, PASSED" ; \
  102520. + else echo "=======> $$test, FAILED" ; \
  102521. + fi \
  102522. + done
  102523. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/test/test_mod_param.pl linux-3.12.33-rpi/drivers/usb/host/dwc_otg/test/test_mod_param.pl
  102524. --- linux-3.12.33/drivers/usb/host/dwc_otg/test/test_mod_param.pl 1969-12-31 18:00:00.000000000 -0600
  102525. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/test/test_mod_param.pl 2014-12-03 19:13:40.224418001 -0600
  102526. @@ -0,0 +1,133 @@
  102527. +#!/usr/bin/perl -w
  102528. +#
  102529. +# Run this program on the integrator.
  102530. +#
  102531. +# - Tests module parameter default values.
  102532. +# - Tests setting of valid module parameter values via modprobe.
  102533. +# - Tests invalid module parameter values.
  102534. +# -----------------------------------------------------------------------------
  102535. +use strict;
  102536. +use dwc_otg_test;
  102537. +
  102538. +check_arch() or die;
  102539. +
  102540. +#
  102541. +#
  102542. +sub test {
  102543. + my ($param,$expected) = @_;
  102544. + my $value = get($param);
  102545. +
  102546. + if ($value == $expected) {
  102547. + print "$param = $value, okay\n";
  102548. + }
  102549. +
  102550. + else {
  102551. + warn "ERROR: value of $param != $expected, $value\n";
  102552. + $errors ++;
  102553. + }
  102554. +}
  102555. +
  102556. +#
  102557. +#
  102558. +sub get {
  102559. + my $param = shift;
  102560. + my $tmp = `cat $paramdir/$param`;
  102561. + chomp $tmp;
  102562. + return $tmp;
  102563. +}
  102564. +
  102565. +#
  102566. +#
  102567. +sub test_main {
  102568. +
  102569. + print "\nTesting Module Parameters\n";
  102570. +
  102571. + load_module("") or die;
  102572. +
  102573. + # Test initial values
  102574. + print "\nTesting Default Values\n";
  102575. + foreach (@{$params}) {
  102576. + test ($_->{NAME}, $_->{DEFAULT});
  102577. + }
  102578. +
  102579. + # Test low value
  102580. + print "\nTesting Low Value\n";
  102581. + my $cmd_params = "";
  102582. + foreach (@{$params}) {
  102583. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{LOW} ";
  102584. + }
  102585. + load_module($cmd_params) or die;
  102586. +
  102587. + foreach (@{$params}) {
  102588. + test ($_->{NAME}, $_->{LOW});
  102589. + }
  102590. +
  102591. + # Test high value
  102592. + print "\nTesting High Value\n";
  102593. + $cmd_params = "";
  102594. + foreach (@{$params}) {
  102595. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{HIGH} ";
  102596. + }
  102597. + load_module($cmd_params) or die;
  102598. +
  102599. + foreach (@{$params}) {
  102600. + test ($_->{NAME}, $_->{HIGH});
  102601. + }
  102602. +
  102603. + # Test Enum
  102604. + print "\nTesting Enumerated\n";
  102605. + foreach (@{$params}) {
  102606. + if (defined $_->{ENUM}) {
  102607. + my $value;
  102608. + foreach $value (@{$_->{ENUM}}) {
  102609. + $cmd_params = "$_->{NAME}=$value";
  102610. + load_module($cmd_params) or die;
  102611. + test ($_->{NAME}, $value);
  102612. + }
  102613. + }
  102614. + }
  102615. +
  102616. + # Test Invalid Values
  102617. + print "\nTesting Invalid Values\n";
  102618. + $cmd_params = "";
  102619. + foreach (@{$params}) {
  102620. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{LOW}-1;
  102621. + }
  102622. + load_module($cmd_params) or die;
  102623. +
  102624. + foreach (@{$params}) {
  102625. + test ($_->{NAME}, $_->{DEFAULT});
  102626. + }
  102627. +
  102628. + $cmd_params = "";
  102629. + foreach (@{$params}) {
  102630. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{HIGH}+1;
  102631. + }
  102632. + load_module($cmd_params) or die;
  102633. +
  102634. + foreach (@{$params}) {
  102635. + test ($_->{NAME}, $_->{DEFAULT});
  102636. + }
  102637. +
  102638. + print "\nTesting Enumerated\n";
  102639. + foreach (@{$params}) {
  102640. + if (defined $_->{ENUM}) {
  102641. + my $value;
  102642. + foreach $value (@{$_->{ENUM}}) {
  102643. + $value = $value + 1;
  102644. + $cmd_params = "$_->{NAME}=$value";
  102645. + load_module($cmd_params) or die;
  102646. + test ($_->{NAME}, $_->{DEFAULT});
  102647. + $value = $value - 2;
  102648. + $cmd_params = "$_->{NAME}=$value";
  102649. + load_module($cmd_params) or die;
  102650. + test ($_->{NAME}, $_->{DEFAULT});
  102651. + }
  102652. + }
  102653. + }
  102654. +
  102655. + test_status() or die;
  102656. +}
  102657. +
  102658. +test_main();
  102659. +0;
  102660. diff -Nur linux-3.12.33/drivers/usb/host/dwc_otg/test/test_sysfs.pl linux-3.12.33-rpi/drivers/usb/host/dwc_otg/test/test_sysfs.pl
  102661. --- linux-3.12.33/drivers/usb/host/dwc_otg/test/test_sysfs.pl 1969-12-31 18:00:00.000000000 -0600
  102662. +++ linux-3.12.33-rpi/drivers/usb/host/dwc_otg/test/test_sysfs.pl 2014-12-03 19:13:40.224418001 -0600
  102663. @@ -0,0 +1,193 @@
  102664. +#!/usr/bin/perl -w
  102665. +#
  102666. +# Run this program on the integrator
  102667. +# - Tests select sysfs attributes.
  102668. +# - Todo ... test more attributes, hnp/srp, buspower/bussuspend, etc.
  102669. +# -----------------------------------------------------------------------------
  102670. +use strict;
  102671. +use dwc_otg_test;
  102672. +
  102673. +check_arch() or die;
  102674. +
  102675. +#
  102676. +#
  102677. +sub test {
  102678. + my ($attr,$expected) = @_;
  102679. + my $string = get($attr);
  102680. +
  102681. + if ($string eq $expected) {
  102682. + printf("$attr = $string, okay\n");
  102683. + }
  102684. + else {
  102685. + warn "ERROR: value of $attr != $expected, $string\n";
  102686. + $errors ++;
  102687. + }
  102688. +}
  102689. +
  102690. +#
  102691. +#
  102692. +sub set {
  102693. + my ($reg, $value) = @_;
  102694. + system "echo $value > $sysfsdir/$reg";
  102695. +}
  102696. +
  102697. +#
  102698. +#
  102699. +sub get {
  102700. + my $attr = shift;
  102701. + my $string = `cat $sysfsdir/$attr`;
  102702. + chomp $string;
  102703. + if ($string =~ m/\s\=\s/) {
  102704. + my $tmp;
  102705. + ($tmp, $string) = split /\s=\s/, $string;
  102706. + }
  102707. + return $string;
  102708. +}
  102709. +
  102710. +#
  102711. +#
  102712. +sub test_main {
  102713. + print("\nTesting Sysfs Attributes\n");
  102714. +
  102715. + load_module("") or die;
  102716. +
  102717. + # Test initial values of regoffset/regvalue/guid/gsnpsid
  102718. + print("\nTesting Default Values\n");
  102719. +
  102720. + test("regoffset", "0xffffffff");
  102721. + test("regvalue", "invalid offset");
  102722. + test("guid", "0x12345678"); # this will fail if it has been changed
  102723. + test("gsnpsid", "0x4f54200a");
  102724. +
  102725. + # Test operation of regoffset/regvalue
  102726. + print("\nTesting regoffset\n");
  102727. + set('regoffset', '5a5a5a5a');
  102728. + test("regoffset", "0xffffffff");
  102729. +
  102730. + set('regoffset', '0');
  102731. + test("regoffset", "0x00000000");
  102732. +
  102733. + set('regoffset', '40000');
  102734. + test("regoffset", "0x00000000");
  102735. +
  102736. + set('regoffset', '3ffff');
  102737. + test("regoffset", "0x0003ffff");
  102738. +
  102739. + set('regoffset', '1');
  102740. + test("regoffset", "0x00000001");
  102741. +
  102742. + print("\nTesting regvalue\n");
  102743. + set('regoffset', '3c');
  102744. + test("regvalue", "0x12345678");
  102745. + set('regvalue', '5a5a5a5a');
  102746. + test("regvalue", "0x5a5a5a5a");
  102747. + set('regvalue','a5a5a5a5');
  102748. + test("regvalue", "0xa5a5a5a5");
  102749. + set('guid','12345678');
  102750. +
  102751. + # Test HNP Capable
  102752. + print("\nTesting HNP Capable bit\n");
  102753. + set('hnpcapable', '1');
  102754. + test("hnpcapable", "0x1");
  102755. + set('hnpcapable','0');
  102756. + test("hnpcapable", "0x0");
  102757. +
  102758. + set('regoffset','0c');
  102759. +
  102760. + my $old = get('gusbcfg');
  102761. + print("setting hnpcapable\n");
  102762. + set('hnpcapable', '1');
  102763. + test("hnpcapable", "0x1");
  102764. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<9)));
  102765. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<9)));
  102766. +
  102767. + $old = get('gusbcfg');
  102768. + print("clearing hnpcapable\n");
  102769. + set('hnpcapable', '0');
  102770. + test("hnpcapable", "0x0");
  102771. + test ('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  102772. + test ('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  102773. +
  102774. + # Test SRP Capable
  102775. + print("\nTesting SRP Capable bit\n");
  102776. + set('srpcapable', '1');
  102777. + test("srpcapable", "0x1");
  102778. + set('srpcapable','0');
  102779. + test("srpcapable", "0x0");
  102780. +
  102781. + set('regoffset','0c');
  102782. +
  102783. + $old = get('gusbcfg');
  102784. + print("setting srpcapable\n");
  102785. + set('srpcapable', '1');
  102786. + test("srpcapable", "0x1");
  102787. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<8)));
  102788. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<8)));
  102789. +
  102790. + $old = get('gusbcfg');
  102791. + print("clearing srpcapable\n");
  102792. + set('srpcapable', '0');
  102793. + test("srpcapable", "0x0");
  102794. + test('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  102795. + test('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  102796. +
  102797. + # Test GGPIO
  102798. + print("\nTesting GGPIO\n");
  102799. + set('ggpio','5a5a5a5a');
  102800. + test('ggpio','0x5a5a0000');
  102801. + set('ggpio','a5a5a5a5');
  102802. + test('ggpio','0xa5a50000');
  102803. + set('ggpio','11110000');
  102804. + test('ggpio','0x11110000');
  102805. + set('ggpio','00001111');
  102806. + test('ggpio','0x00000000');
  102807. +
  102808. + # Test DEVSPEED
  102809. + print("\nTesting DEVSPEED\n");
  102810. + set('regoffset','800');
  102811. + $old = get('regvalue');
  102812. + set('devspeed','0');
  102813. + test('devspeed','0x0');
  102814. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  102815. + set('devspeed','1');
  102816. + test('devspeed','0x1');
  102817. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  102818. + set('devspeed','2');
  102819. + test('devspeed','0x2');
  102820. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 2));
  102821. + set('devspeed','3');
  102822. + test('devspeed','0x3');
  102823. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 3));
  102824. + set('devspeed','4');
  102825. + test('devspeed','0x0');
  102826. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  102827. + set('devspeed','5');
  102828. + test('devspeed','0x1');
  102829. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  102830. +
  102831. +
  102832. + # mode Returns the current mode:0 for device mode1 for host mode Read
  102833. + # hnp Initiate the Host Negotiation Protocol. Read returns the status. Read/Write
  102834. + # srp Initiate the Session Request Protocol. Read returns the status. Read/Write
  102835. + # buspower Get or Set the Power State of the bus (0 - Off or 1 - On) Read/Write
  102836. + # bussuspend Suspend the USB bus. Read/Write
  102837. + # busconnected Get the connection status of the bus Read
  102838. +
  102839. + # gotgctl Get or set the Core Control Status Register. Read/Write
  102840. + ## gusbcfg Get or set the Core USB Configuration Register Read/Write
  102841. + # grxfsiz Get or set the Receive FIFO Size Register Read/Write
  102842. + # gnptxfsiz Get or set the non-periodic Transmit Size Register Read/Write
  102843. + # gpvndctl Get or set the PHY Vendor Control Register Read/Write
  102844. + ## ggpio Get the value in the lower 16-bits of the General Purpose IO Register or Set the upper 16 bits. Read/Write
  102845. + ## guid Get or set the value of the User ID Register Read/Write
  102846. + ## gsnpsid Get the value of the Synopsys ID Regester Read
  102847. + ## devspeed Get or set the device speed setting in the DCFG register Read/Write
  102848. + # enumspeed Gets the device enumeration Speed. Read
  102849. + # hptxfsiz Get the value of the Host Periodic Transmit FIFO Read
  102850. + # hprt0 Get or Set the value in the Host Port Control and Status Register Read/Write
  102851. +
  102852. + test_status("TEST NYI") or die;
  102853. +}
  102854. +
  102855. +test_main();
  102856. +0;
  102857. diff -Nur linux-3.12.33/drivers/usb/host/Kconfig linux-3.12.33-rpi/drivers/usb/host/Kconfig
  102858. --- linux-3.12.33/drivers/usb/host/Kconfig 2014-11-15 06:28:07.000000000 -0600
  102859. +++ linux-3.12.33-rpi/drivers/usb/host/Kconfig 2014-12-03 19:13:40.212418001 -0600
  102860. @@ -650,6 +650,19 @@
  102861. To compile this driver a module, choose M here: the module
  102862. will be called "hwa-hc".
  102863. +config USB_DWCOTG
  102864. + tristate "Synopsis DWC host support"
  102865. + depends on USB
  102866. + help
  102867. + The Synopsis DWC controller is a dual-role
  102868. + host/peripheral/OTG ("On The Go") USB controllers.
  102869. +
  102870. + Enable this option to support this IP in host controller mode.
  102871. + If unsure, say N.
  102872. +
  102873. + To compile this driver as a module, choose M here: the
  102874. + modules built will be called dwc_otg and dwc_common_port.
  102875. +
  102876. config USB_IMX21_HCD
  102877. tristate "i.MX21 HCD support"
  102878. depends on ARM && ARCH_MXC
  102879. diff -Nur linux-3.12.33/drivers/usb/host/Makefile linux-3.12.33-rpi/drivers/usb/host/Makefile
  102880. --- linux-3.12.33/drivers/usb/host/Makefile 2014-11-15 06:28:07.000000000 -0600
  102881. +++ linux-3.12.33-rpi/drivers/usb/host/Makefile 2014-12-03 19:13:40.212418001 -0600
  102882. @@ -56,6 +56,8 @@
  102883. obj-$(CONFIG_USB_R8A66597_HCD) += r8a66597-hcd.o
  102884. obj-$(CONFIG_USB_ISP1760_HCD) += isp1760.o
  102885. obj-$(CONFIG_USB_HWA_HCD) += hwa-hc.o
  102886. +
  102887. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg/ dwc_common_port/
  102888. obj-$(CONFIG_USB_IMX21_HCD) += imx21-hcd.o
  102889. obj-$(CONFIG_USB_FSL_MPH_DR_OF) += fsl-mph-dr-of.o
  102890. obj-$(CONFIG_USB_OCTEON2_COMMON) += octeon2-common.o
  102891. diff -Nur linux-3.12.33/drivers/usb/Makefile linux-3.12.33-rpi/drivers/usb/Makefile
  102892. --- linux-3.12.33/drivers/usb/Makefile 2014-11-15 06:28:07.000000000 -0600
  102893. +++ linux-3.12.33-rpi/drivers/usb/Makefile 2014-12-03 19:13:40.176418001 -0600
  102894. @@ -23,6 +23,7 @@
  102895. obj-$(CONFIG_USB_R8A66597_HCD) += host/
  102896. obj-$(CONFIG_USB_HWA_HCD) += host/
  102897. obj-$(CONFIG_USB_ISP1760_HCD) += host/
  102898. +obj-$(CONFIG_USB_DWCOTG) += host/
  102899. obj-$(CONFIG_USB_IMX21_HCD) += host/
  102900. obj-$(CONFIG_USB_FSL_MPH_DR_OF) += host/
  102901. obj-$(CONFIG_USB_FUSBH200_HCD) += host/
  102902. diff -Nur linux-3.12.33/drivers/video/bcm2708_fb.c linux-3.12.33-rpi/drivers/video/bcm2708_fb.c
  102903. --- linux-3.12.33/drivers/video/bcm2708_fb.c 1969-12-31 18:00:00.000000000 -0600
  102904. +++ linux-3.12.33-rpi/drivers/video/bcm2708_fb.c 2014-12-03 19:13:42.492418001 -0600
  102905. @@ -0,0 +1,817 @@
  102906. +/*
  102907. + * linux/drivers/video/bcm2708_fb.c
  102908. + *
  102909. + * Copyright (C) 2010 Broadcom
  102910. + *
  102911. + * This file is subject to the terms and conditions of the GNU General Public
  102912. + * License. See the file COPYING in the main directory of this archive
  102913. + * for more details.
  102914. + *
  102915. + * Broadcom simple framebuffer driver
  102916. + *
  102917. + * This file is derived from cirrusfb.c
  102918. + * Copyright 1999-2001 Jeff Garzik <jgarzik@pobox.com>
  102919. + *
  102920. + */
  102921. +#include <linux/module.h>
  102922. +#include <linux/kernel.h>
  102923. +#include <linux/errno.h>
  102924. +#include <linux/string.h>
  102925. +#include <linux/slab.h>
  102926. +#include <linux/mm.h>
  102927. +#include <linux/fb.h>
  102928. +#include <linux/init.h>
  102929. +#include <linux/interrupt.h>
  102930. +#include <linux/ioport.h>
  102931. +#include <linux/list.h>
  102932. +#include <linux/platform_device.h>
  102933. +#include <linux/clk.h>
  102934. +#include <linux/printk.h>
  102935. +#include <linux/console.h>
  102936. +#include <linux/debugfs.h>
  102937. +
  102938. +#include <mach/dma.h>
  102939. +#include <mach/platform.h>
  102940. +#include <mach/vcio.h>
  102941. +
  102942. +#include <asm/sizes.h>
  102943. +#include <linux/io.h>
  102944. +#include <linux/dma-mapping.h>
  102945. +
  102946. +//#define BCM2708_FB_DEBUG
  102947. +#define MODULE_NAME "bcm2708_fb"
  102948. +
  102949. +#ifdef BCM2708_FB_DEBUG
  102950. +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  102951. +#else
  102952. +#define print_debug(fmt,...)
  102953. +#endif
  102954. +
  102955. +/* This is limited to 16 characters when displayed by X startup */
  102956. +static const char *bcm2708_name = "BCM2708 FB";
  102957. +
  102958. +#define DRIVER_NAME "bcm2708_fb"
  102959. +
  102960. +static int fbwidth = 800; /* module parameter */
  102961. +static int fbheight = 480; /* module parameter */
  102962. +static int fbdepth = 16; /* module parameter */
  102963. +static int fbswap = 0; /* module parameter */
  102964. +
  102965. +static u32 dma_busy_wait_threshold = 1<<15;
  102966. +module_param(dma_busy_wait_threshold, int, 0644);
  102967. +MODULE_PARM_DESC(dma_busy_wait_threshold, "Busy-wait for DMA completion below this area");
  102968. +
  102969. +/* this data structure describes each frame buffer device we find */
  102970. +
  102971. +struct fbinfo_s {
  102972. + u32 xres, yres, xres_virtual, yres_virtual;
  102973. + u32 pitch, bpp;
  102974. + u32 xoffset, yoffset;
  102975. + u32 base;
  102976. + u32 screen_size;
  102977. + u16 cmap[256];
  102978. +};
  102979. +
  102980. +struct bcm2708_fb_stats {
  102981. + struct debugfs_regset32 regset;
  102982. + u32 dma_copies;
  102983. + u32 dma_irqs;
  102984. +};
  102985. +
  102986. +struct bcm2708_fb {
  102987. + struct fb_info fb;
  102988. + struct platform_device *dev;
  102989. + struct fbinfo_s *info;
  102990. + dma_addr_t dma;
  102991. + u32 cmap[16];
  102992. + int dma_chan;
  102993. + int dma_irq;
  102994. + void __iomem *dma_chan_base;
  102995. + void *cb_base; /* DMA control blocks */
  102996. + dma_addr_t cb_handle;
  102997. + struct dentry *debugfs_dir;
  102998. + wait_queue_head_t dma_waitq;
  102999. + struct bcm2708_fb_stats stats;
  103000. + unsigned long fb_bus_address;
  103001. +};
  103002. +
  103003. +#define to_bcm2708(info) container_of(info, struct bcm2708_fb, fb)
  103004. +
  103005. +static void bcm2708_fb_debugfs_deinit(struct bcm2708_fb *fb)
  103006. +{
  103007. + debugfs_remove_recursive(fb->debugfs_dir);
  103008. + fb->debugfs_dir = NULL;
  103009. +}
  103010. +
  103011. +static int bcm2708_fb_debugfs_init(struct bcm2708_fb *fb)
  103012. +{
  103013. + static struct debugfs_reg32 stats_registers[] = {
  103014. + {
  103015. + "dma_copies",
  103016. + offsetof(struct bcm2708_fb_stats, dma_copies)
  103017. + },
  103018. + {
  103019. + "dma_irqs",
  103020. + offsetof(struct bcm2708_fb_stats, dma_irqs)
  103021. + },
  103022. + };
  103023. +
  103024. + fb->debugfs_dir = debugfs_create_dir(DRIVER_NAME, NULL);
  103025. + if (!fb->debugfs_dir) {
  103026. + pr_warn("%s: could not create debugfs entry\n",
  103027. + __func__);
  103028. + return -EFAULT;
  103029. + }
  103030. +
  103031. + fb->stats.regset.regs = stats_registers;
  103032. + fb->stats.regset.nregs = ARRAY_SIZE(stats_registers);
  103033. + fb->stats.regset.base = &fb->stats;
  103034. +
  103035. + if (!debugfs_create_regset32(
  103036. + "stats", 0444, fb->debugfs_dir, &fb->stats.regset)) {
  103037. + pr_warn("%s: could not create statistics registers\n",
  103038. + __func__);
  103039. + goto fail;
  103040. + }
  103041. + return 0;
  103042. +
  103043. +fail:
  103044. + bcm2708_fb_debugfs_deinit(fb);
  103045. + return -EFAULT;
  103046. +}
  103047. +
  103048. +static int bcm2708_fb_set_bitfields(struct fb_var_screeninfo *var)
  103049. +{
  103050. + int ret = 0;
  103051. +
  103052. + memset(&var->transp, 0, sizeof(var->transp));
  103053. +
  103054. + var->red.msb_right = 0;
  103055. + var->green.msb_right = 0;
  103056. + var->blue.msb_right = 0;
  103057. +
  103058. + switch (var->bits_per_pixel) {
  103059. + case 1:
  103060. + case 2:
  103061. + case 4:
  103062. + case 8:
  103063. + var->red.length = var->bits_per_pixel;
  103064. + var->red.offset = 0;
  103065. + var->green.length = var->bits_per_pixel;
  103066. + var->green.offset = 0;
  103067. + var->blue.length = var->bits_per_pixel;
  103068. + var->blue.offset = 0;
  103069. + break;
  103070. + case 16:
  103071. + var->red.length = 5;
  103072. + var->blue.length = 5;
  103073. + /*
  103074. + * Green length can be 5 or 6 depending whether
  103075. + * we're operating in RGB555 or RGB565 mode.
  103076. + */
  103077. + if (var->green.length != 5 && var->green.length != 6)
  103078. + var->green.length = 6;
  103079. + break;
  103080. + case 24:
  103081. + var->red.length = 8;
  103082. + var->blue.length = 8;
  103083. + var->green.length = 8;
  103084. + break;
  103085. + case 32:
  103086. + var->red.length = 8;
  103087. + var->green.length = 8;
  103088. + var->blue.length = 8;
  103089. + var->transp.length = 8;
  103090. + break;
  103091. + default:
  103092. + ret = -EINVAL;
  103093. + break;
  103094. + }
  103095. +
  103096. + /*
  103097. + * >= 16bpp displays have separate colour component bitfields
  103098. + * encoded in the pixel data. Calculate their position from
  103099. + * the bitfield length defined above.
  103100. + */
  103101. + if (ret == 0 && var->bits_per_pixel >= 24 && fbswap) {
  103102. + var->blue.offset = 0;
  103103. + var->green.offset = var->blue.offset + var->blue.length;
  103104. + var->red.offset = var->green.offset + var->green.length;
  103105. + var->transp.offset = var->red.offset + var->red.length;
  103106. + } else if (ret == 0 && var->bits_per_pixel >= 24) {
  103107. + var->red.offset = 0;
  103108. + var->green.offset = var->red.offset + var->red.length;
  103109. + var->blue.offset = var->green.offset + var->green.length;
  103110. + var->transp.offset = var->blue.offset + var->blue.length;
  103111. + } else if (ret == 0 && var->bits_per_pixel >= 16) {
  103112. + var->blue.offset = 0;
  103113. + var->green.offset = var->blue.offset + var->blue.length;
  103114. + var->red.offset = var->green.offset + var->green.length;
  103115. + var->transp.offset = var->red.offset + var->red.length;
  103116. + }
  103117. +
  103118. + return ret;
  103119. +}
  103120. +
  103121. +static int bcm2708_fb_check_var(struct fb_var_screeninfo *var,
  103122. + struct fb_info *info)
  103123. +{
  103124. + /* info input, var output */
  103125. + int yres;
  103126. +
  103127. + /* info input, var output */
  103128. + print_debug("bcm2708_fb_check_var info(%p) %dx%d (%dx%d), %d, %d\n", info,
  103129. + info->var.xres, info->var.yres, info->var.xres_virtual,
  103130. + info->var.yres_virtual, (int)info->screen_size,
  103131. + info->var.bits_per_pixel);
  103132. + print_debug("bcm2708_fb_check_var var(%p) %dx%d (%dx%d), %d\n", var,
  103133. + var->xres, var->yres, var->xres_virtual, var->yres_virtual,
  103134. + var->bits_per_pixel);
  103135. +
  103136. + if (!var->bits_per_pixel)
  103137. + var->bits_per_pixel = 16;
  103138. +
  103139. + if (bcm2708_fb_set_bitfields(var) != 0) {
  103140. + pr_err("bcm2708_fb_check_var: invalid bits_per_pixel %d\n",
  103141. + var->bits_per_pixel);
  103142. + return -EINVAL;
  103143. + }
  103144. +
  103145. +
  103146. + if (var->xres_virtual < var->xres)
  103147. + var->xres_virtual = var->xres;
  103148. + /* use highest possible virtual resolution */
  103149. + if (var->yres_virtual == -1) {
  103150. + var->yres_virtual = 480;
  103151. +
  103152. + pr_err
  103153. + ("bcm2708_fb_check_var: virtual resolution set to maximum of %dx%d\n",
  103154. + var->xres_virtual, var->yres_virtual);
  103155. + }
  103156. + if (var->yres_virtual < var->yres)
  103157. + var->yres_virtual = var->yres;
  103158. +
  103159. + if (var->xoffset < 0)
  103160. + var->xoffset = 0;
  103161. + if (var->yoffset < 0)
  103162. + var->yoffset = 0;
  103163. +
  103164. + /* truncate xoffset and yoffset to maximum if too high */
  103165. + if (var->xoffset > var->xres_virtual - var->xres)
  103166. + var->xoffset = var->xres_virtual - var->xres - 1;
  103167. + if (var->yoffset > var->yres_virtual - var->yres)
  103168. + var->yoffset = var->yres_virtual - var->yres - 1;
  103169. +
  103170. + yres = var->yres;
  103171. + if (var->vmode & FB_VMODE_DOUBLE)
  103172. + yres *= 2;
  103173. + else if (var->vmode & FB_VMODE_INTERLACED)
  103174. + yres = (yres + 1) / 2;
  103175. +
  103176. + return 0;
  103177. +}
  103178. +
  103179. +static int bcm2708_fb_set_par(struct fb_info *info)
  103180. +{
  103181. + uint32_t val = 0;
  103182. + struct bcm2708_fb *fb = to_bcm2708(info);
  103183. + volatile struct fbinfo_s *fbinfo = fb->info;
  103184. + fbinfo->xres = info->var.xres;
  103185. + fbinfo->yres = info->var.yres;
  103186. + fbinfo->xres_virtual = info->var.xres_virtual;
  103187. + fbinfo->yres_virtual = info->var.yres_virtual;
  103188. + fbinfo->bpp = info->var.bits_per_pixel;
  103189. + fbinfo->xoffset = info->var.xoffset;
  103190. + fbinfo->yoffset = info->var.yoffset;
  103191. + fbinfo->base = 0; /* filled in by VC */
  103192. + fbinfo->pitch = 0; /* filled in by VC */
  103193. +
  103194. + print_debug("bcm2708_fb_set_par info(%p) %dx%d (%dx%d), %d, %d\n", info,
  103195. + info->var.xres, info->var.yres, info->var.xres_virtual,
  103196. + info->var.yres_virtual, (int)info->screen_size,
  103197. + info->var.bits_per_pixel);
  103198. +
  103199. + /* ensure last write to fbinfo is visible to GPU */
  103200. + wmb();
  103201. +
  103202. + /* inform vc about new framebuffer */
  103203. + bcm_mailbox_write(MBOX_CHAN_FB, fb->dma);
  103204. +
  103205. + /* TODO: replace fb driver with vchiq version */
  103206. + /* wait for response */
  103207. + bcm_mailbox_read(MBOX_CHAN_FB, &val);
  103208. +
  103209. + /* ensure GPU writes are visible to us */
  103210. + rmb();
  103211. +
  103212. + if (val == 0) {
  103213. + fb->fb.fix.line_length = fbinfo->pitch;
  103214. +
  103215. + if (info->var.bits_per_pixel <= 8)
  103216. + fb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
  103217. + else
  103218. + fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
  103219. +
  103220. + fb->fb_bus_address = fbinfo->base;
  103221. + fbinfo->base &= ~0xc0000000;
  103222. + fb->fb.fix.smem_start = fbinfo->base;
  103223. + fb->fb.fix.smem_len = fbinfo->pitch * fbinfo->yres_virtual;
  103224. + fb->fb.screen_size = fbinfo->screen_size;
  103225. + if (fb->fb.screen_base)
  103226. + iounmap(fb->fb.screen_base);
  103227. + fb->fb.screen_base =
  103228. + (void *)ioremap_wc(fbinfo->base, fb->fb.screen_size);
  103229. + if (!fb->fb.screen_base) {
  103230. + /* the console may currently be locked */
  103231. + console_trylock();
  103232. + console_unlock();
  103233. +
  103234. + BUG(); /* what can we do here */
  103235. + }
  103236. + }
  103237. + print_debug
  103238. + ("BCM2708FB: start = %p,%p width=%d, height=%d, bpp=%d, pitch=%d size=%d success=%d\n",
  103239. + (void *)fb->fb.screen_base, (void *)fb->fb_bus_address,
  103240. + fbinfo->xres, fbinfo->yres, fbinfo->bpp,
  103241. + fbinfo->pitch, (int)fb->fb.screen_size, val);
  103242. +
  103243. + return val;
  103244. +}
  103245. +
  103246. +static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
  103247. +{
  103248. + unsigned int mask = (1 << bf->length) - 1;
  103249. +
  103250. + return (val >> (16 - bf->length) & mask) << bf->offset;
  103251. +}
  103252. +
  103253. +
  103254. +static int bcm2708_fb_setcolreg(unsigned int regno, unsigned int red,
  103255. + unsigned int green, unsigned int blue,
  103256. + unsigned int transp, struct fb_info *info)
  103257. +{
  103258. + struct bcm2708_fb *fb = to_bcm2708(info);
  103259. +
  103260. + /*print_debug("BCM2708FB: setcolreg %d:(%02x,%02x,%02x,%02x) %x\n", regno, red, green, blue, transp, fb->fb.fix.visual);*/
  103261. + if (fb->fb.var.bits_per_pixel <= 8) {
  103262. + if (regno < 256) {
  103263. + /* blue [0:4], green [5:10], red [11:15] */
  103264. + fb->info->cmap[regno] = ((red >> (16-5)) & 0x1f) << 11 |
  103265. + ((green >> (16-6)) & 0x3f) << 5 |
  103266. + ((blue >> (16-5)) & 0x1f) << 0;
  103267. + }
  103268. + /* Hack: we need to tell GPU the palette has changed, but currently bcm2708_fb_set_par takes noticable time when called for every (256) colour */
  103269. + /* So just call it for what looks like the last colour in a list for now. */
  103270. + if (regno == 15 || regno == 255)
  103271. + bcm2708_fb_set_par(info);
  103272. + } else if (regno < 16) {
  103273. + fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
  103274. + convert_bitfield(blue, &fb->fb.var.blue) |
  103275. + convert_bitfield(green, &fb->fb.var.green) |
  103276. + convert_bitfield(red, &fb->fb.var.red);
  103277. + }
  103278. + return regno > 255;
  103279. +}
  103280. +
  103281. +static int bcm2708_fb_blank(int blank_mode, struct fb_info *info)
  103282. +{
  103283. + s32 result = -1;
  103284. + u32 p[7];
  103285. + if ( (blank_mode == FB_BLANK_NORMAL) ||
  103286. + (blank_mode == FB_BLANK_UNBLANK)) {
  103287. +
  103288. + p[0] = 28; // size = sizeof u32 * length of p
  103289. + p[1] = VCMSG_PROCESS_REQUEST; // process request
  103290. + p[2] = VCMSG_SET_BLANK_SCREEN; // (the tag id)
  103291. + p[3] = 4; // (size of the response buffer)
  103292. + p[4] = 4; // (size of the request data)
  103293. + p[5] = blank_mode;
  103294. + p[6] = VCMSG_PROPERTY_END; // end tag
  103295. +
  103296. + bcm_mailbox_property(&p, p[0]);
  103297. +
  103298. + if ( p[1] == VCMSG_REQUEST_SUCCESSFUL )
  103299. + result = 0;
  103300. + else
  103301. + pr_err("bcm2708_fb_blank(%d) returns=%d p[1]=0x%x\n", blank_mode, p[5], p[1]);
  103302. + }
  103303. + return result;
  103304. +}
  103305. +
  103306. +static int bcm2708_fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
  103307. +{
  103308. + s32 result = -1;
  103309. + info->var.xoffset = var->xoffset;
  103310. + info->var.yoffset = var->yoffset;
  103311. + result = bcm2708_fb_set_par(info);
  103312. + if (result != 0)
  103313. + pr_err("bcm2708_fb_pan_display(%d,%d) returns=%d\n", var->xoffset, var->yoffset, result);
  103314. + return result;
  103315. +}
  103316. +
  103317. +static int bcm2708_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
  103318. +{
  103319. + s32 result = -1;
  103320. + u32 p[7];
  103321. + if (cmd == FBIO_WAITFORVSYNC) {
  103322. + p[0] = 28; // size = sizeof u32 * length of p
  103323. + p[1] = VCMSG_PROCESS_REQUEST; // process request
  103324. + p[2] = VCMSG_SET_VSYNC; // (the tag id)
  103325. + p[3] = 4; // (size of the response buffer)
  103326. + p[4] = 4; // (size of the request data)
  103327. + p[5] = 0; // dummy
  103328. + p[6] = VCMSG_PROPERTY_END; // end tag
  103329. +
  103330. + bcm_mailbox_property(&p, p[0]);
  103331. +
  103332. + pr_info("bcm2708_fb_ioctl %x,%lx returns=%d p[1]=0x%x\n", cmd, arg, p[5], p[1]);
  103333. +
  103334. + if ( p[1] == VCMSG_REQUEST_SUCCESSFUL )
  103335. + result = 0;
  103336. + }
  103337. + return result;
  103338. +}
  103339. +static void bcm2708_fb_fillrect(struct fb_info *info,
  103340. + const struct fb_fillrect *rect)
  103341. +{
  103342. + /* (is called) print_debug("bcm2708_fb_fillrect\n"); */
  103343. + cfb_fillrect(info, rect);
  103344. +}
  103345. +
  103346. +/* A helper function for configuring dma control block */
  103347. +static void set_dma_cb(struct bcm2708_dma_cb *cb,
  103348. + int burst_size,
  103349. + dma_addr_t dst,
  103350. + int dst_stride,
  103351. + dma_addr_t src,
  103352. + int src_stride,
  103353. + int w,
  103354. + int h)
  103355. +{
  103356. + cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
  103357. + BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
  103358. + BCM2708_DMA_D_INC | BCM2708_DMA_TDMODE;
  103359. + cb->dst = dst;
  103360. + cb->src = src;
  103361. + /*
  103362. + * This is not really obvious from the DMA documentation,
  103363. + * but the top 16 bits must be programmmed to "height -1"
  103364. + * and not "height" in 2D mode.
  103365. + */
  103366. + cb->length = ((h - 1) << 16) | w;
  103367. + cb->stride = ((dst_stride - w) << 16) | (u16)(src_stride - w);
  103368. + cb->pad[0] = 0;
  103369. + cb->pad[1] = 0;
  103370. +}
  103371. +
  103372. +static void bcm2708_fb_copyarea(struct fb_info *info,
  103373. + const struct fb_copyarea *region)
  103374. +{
  103375. + struct bcm2708_fb *fb = to_bcm2708(info);
  103376. + struct bcm2708_dma_cb *cb = fb->cb_base;
  103377. + int bytes_per_pixel = (info->var.bits_per_pixel + 7) >> 3;
  103378. + /* Channel 0 supports larger bursts and is a bit faster */
  103379. + int burst_size = (fb->dma_chan == 0) ? 8 : 2;
  103380. + int pixels = region->width * region->height;
  103381. +
  103382. + /* Fallback to cfb_copyarea() if we don't like something */
  103383. + if (in_atomic() ||
  103384. + bytes_per_pixel > 4 ||
  103385. + info->var.xres * info->var.yres > 1920 * 1200 ||
  103386. + region->width <= 0 || region->width > info->var.xres ||
  103387. + region->height <= 0 || region->height > info->var.yres ||
  103388. + region->sx < 0 || region->sx >= info->var.xres ||
  103389. + region->sy < 0 || region->sy >= info->var.yres ||
  103390. + region->dx < 0 || region->dx >= info->var.xres ||
  103391. + region->dy < 0 || region->dy >= info->var.yres ||
  103392. + region->sx + region->width > info->var.xres ||
  103393. + region->dx + region->width > info->var.xres ||
  103394. + region->sy + region->height > info->var.yres ||
  103395. + region->dy + region->height > info->var.yres) {
  103396. + cfb_copyarea(info, region);
  103397. + return;
  103398. + }
  103399. +
  103400. + if (region->dy == region->sy && region->dx > region->sx) {
  103401. + /*
  103402. + * A difficult case of overlapped copy. Because DMA can't
  103403. + * copy individual scanlines in backwards direction, we need
  103404. + * two-pass processing. We do it by programming a chain of dma
  103405. + * control blocks in the first 16K part of the buffer and use
  103406. + * the remaining 48K as the intermediate temporary scratch
  103407. + * buffer. The buffer size is sufficient to handle up to
  103408. + * 1920x1200 resolution at 32bpp pixel depth.
  103409. + */
  103410. + int y;
  103411. + dma_addr_t control_block_pa = fb->cb_handle;
  103412. + dma_addr_t scratchbuf = fb->cb_handle + 16 * 1024;
  103413. + int scanline_size = bytes_per_pixel * region->width;
  103414. + int scanlines_per_cb = (64 * 1024 - 16 * 1024) / scanline_size;
  103415. +
  103416. + for (y = 0; y < region->height; y += scanlines_per_cb) {
  103417. + dma_addr_t src =
  103418. + fb->fb_bus_address +
  103419. + bytes_per_pixel * region->sx +
  103420. + (region->sy + y) * fb->fb.fix.line_length;
  103421. + dma_addr_t dst =
  103422. + fb->fb_bus_address +
  103423. + bytes_per_pixel * region->dx +
  103424. + (region->dy + y) * fb->fb.fix.line_length;
  103425. +
  103426. + if (region->height - y < scanlines_per_cb)
  103427. + scanlines_per_cb = region->height - y;
  103428. +
  103429. + set_dma_cb(cb, burst_size, scratchbuf, scanline_size,
  103430. + src, fb->fb.fix.line_length,
  103431. + scanline_size, scanlines_per_cb);
  103432. + control_block_pa += sizeof(struct bcm2708_dma_cb);
  103433. + cb->next = control_block_pa;
  103434. + cb++;
  103435. +
  103436. + set_dma_cb(cb, burst_size, dst, fb->fb.fix.line_length,
  103437. + scratchbuf, scanline_size,
  103438. + scanline_size, scanlines_per_cb);
  103439. + control_block_pa += sizeof(struct bcm2708_dma_cb);
  103440. + cb->next = control_block_pa;
  103441. + cb++;
  103442. + }
  103443. + /* move the pointer back to the last dma control block */
  103444. + cb--;
  103445. + } else {
  103446. + /* A single dma control block is enough. */
  103447. + int sy, dy, stride;
  103448. + if (region->dy <= region->sy) {
  103449. + /* processing from top to bottom */
  103450. + dy = region->dy;
  103451. + sy = region->sy;
  103452. + stride = fb->fb.fix.line_length;
  103453. + } else {
  103454. + /* processing from bottom to top */
  103455. + dy = region->dy + region->height - 1;
  103456. + sy = region->sy + region->height - 1;
  103457. + stride = -fb->fb.fix.line_length;
  103458. + }
  103459. + set_dma_cb(cb, burst_size,
  103460. + fb->fb_bus_address + dy * fb->fb.fix.line_length +
  103461. + bytes_per_pixel * region->dx,
  103462. + stride,
  103463. + fb->fb_bus_address + sy * fb->fb.fix.line_length +
  103464. + bytes_per_pixel * region->sx,
  103465. + stride,
  103466. + region->width * bytes_per_pixel,
  103467. + region->height);
  103468. + }
  103469. +
  103470. + /* end of dma control blocks chain */
  103471. + cb->next = 0;
  103472. +
  103473. +
  103474. + if (pixels < dma_busy_wait_threshold) {
  103475. + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
  103476. + bcm_dma_wait_idle(fb->dma_chan_base);
  103477. + } else {
  103478. + void __iomem *dma_chan = fb->dma_chan_base;
  103479. + cb->info |= BCM2708_DMA_INT_EN;
  103480. + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
  103481. + while (bcm_dma_is_busy(dma_chan)) {
  103482. + wait_event_interruptible(
  103483. + fb->dma_waitq,
  103484. + !bcm_dma_is_busy(dma_chan));
  103485. + }
  103486. + fb->stats.dma_irqs++;
  103487. + }
  103488. + fb->stats.dma_copies++;
  103489. +}
  103490. +
  103491. +static void bcm2708_fb_imageblit(struct fb_info *info,
  103492. + const struct fb_image *image)
  103493. +{
  103494. + /* (is called) print_debug("bcm2708_fb_imageblit\n"); */
  103495. + cfb_imageblit(info, image);
  103496. +}
  103497. +
  103498. +static irqreturn_t bcm2708_fb_dma_irq(int irq, void *cxt)
  103499. +{
  103500. + struct bcm2708_fb *fb = cxt;
  103501. +
  103502. + /* FIXME: should read status register to check if this is
  103503. + * actually interrupting us or not, in case this interrupt
  103504. + * ever becomes shared amongst several DMA channels
  103505. + *
  103506. + * readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_IRQ;
  103507. + */
  103508. +
  103509. + /* acknowledge the interrupt */
  103510. + writel(BCM2708_DMA_INT, fb->dma_chan_base + BCM2708_DMA_CS);
  103511. +
  103512. + wake_up(&fb->dma_waitq);
  103513. + return IRQ_HANDLED;
  103514. +}
  103515. +
  103516. +static struct fb_ops bcm2708_fb_ops = {
  103517. + .owner = THIS_MODULE,
  103518. + .fb_check_var = bcm2708_fb_check_var,
  103519. + .fb_set_par = bcm2708_fb_set_par,
  103520. + .fb_setcolreg = bcm2708_fb_setcolreg,
  103521. + .fb_blank = bcm2708_fb_blank,
  103522. + .fb_fillrect = bcm2708_fb_fillrect,
  103523. + .fb_copyarea = bcm2708_fb_copyarea,
  103524. + .fb_imageblit = bcm2708_fb_imageblit,
  103525. + .fb_pan_display = bcm2708_fb_pan_display,
  103526. + .fb_ioctl = bcm2708_ioctl,
  103527. +};
  103528. +
  103529. +static int bcm2708_fb_register(struct bcm2708_fb *fb)
  103530. +{
  103531. + int ret;
  103532. + dma_addr_t dma;
  103533. + void *mem;
  103534. +
  103535. + mem =
  103536. + dma_alloc_coherent(NULL, PAGE_ALIGN(sizeof(*fb->info)), &dma,
  103537. + GFP_KERNEL);
  103538. +
  103539. + if (NULL == mem) {
  103540. + pr_err(": unable to allocate fbinfo buffer\n");
  103541. + ret = -ENOMEM;
  103542. + } else {
  103543. + fb->info = (struct fbinfo_s *)mem;
  103544. + fb->dma = dma;
  103545. + }
  103546. + fb->fb.fbops = &bcm2708_fb_ops;
  103547. + fb->fb.flags = FBINFO_FLAG_DEFAULT | FBINFO_HWACCEL_COPYAREA;
  103548. + fb->fb.pseudo_palette = fb->cmap;
  103549. +
  103550. + strncpy(fb->fb.fix.id, bcm2708_name, sizeof(fb->fb.fix.id));
  103551. + fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  103552. + fb->fb.fix.type_aux = 0;
  103553. + fb->fb.fix.xpanstep = 1;
  103554. + fb->fb.fix.ypanstep = 1;
  103555. + fb->fb.fix.ywrapstep = 0;
  103556. + fb->fb.fix.accel = FB_ACCEL_NONE;
  103557. +
  103558. + fb->fb.var.xres = fbwidth;
  103559. + fb->fb.var.yres = fbheight;
  103560. + fb->fb.var.xres_virtual = fbwidth;
  103561. + fb->fb.var.yres_virtual = fbheight;
  103562. + fb->fb.var.bits_per_pixel = fbdepth;
  103563. + fb->fb.var.vmode = FB_VMODE_NONINTERLACED;
  103564. + fb->fb.var.activate = FB_ACTIVATE_NOW;
  103565. + fb->fb.var.nonstd = 0;
  103566. + fb->fb.var.height = -1; /* height of picture in mm */
  103567. + fb->fb.var.width = -1; /* width of picture in mm */
  103568. + fb->fb.var.accel_flags = 0;
  103569. +
  103570. + fb->fb.monspecs.hfmin = 0;
  103571. + fb->fb.monspecs.hfmax = 100000;
  103572. + fb->fb.monspecs.vfmin = 0;
  103573. + fb->fb.monspecs.vfmax = 400;
  103574. + fb->fb.monspecs.dclkmin = 1000000;
  103575. + fb->fb.monspecs.dclkmax = 100000000;
  103576. +
  103577. + bcm2708_fb_set_bitfields(&fb->fb.var);
  103578. + init_waitqueue_head(&fb->dma_waitq);
  103579. +
  103580. + /*
  103581. + * Allocate colourmap.
  103582. + */
  103583. +
  103584. + fb_set_var(&fb->fb, &fb->fb.var);
  103585. +
  103586. + print_debug("BCM2708FB: registering framebuffer (%dx%d@%d) (%d)\n", fbwidth
  103587. + fbheight, fbdepth, fbswap);
  103588. +
  103589. + ret = register_framebuffer(&fb->fb);
  103590. + print_debug("BCM2708FB: register framebuffer (%d)\n", ret);
  103591. + if (ret == 0)
  103592. + goto out;
  103593. +
  103594. + print_debug("BCM2708FB: cannot register framebuffer (%d)\n", ret);
  103595. +out:
  103596. + return ret;
  103597. +}
  103598. +
  103599. +static int bcm2708_fb_probe(struct platform_device *dev)
  103600. +{
  103601. + struct bcm2708_fb *fb;
  103602. + int ret;
  103603. +
  103604. + fb = kzalloc(sizeof(struct bcm2708_fb), GFP_KERNEL);
  103605. + if (!fb) {
  103606. + dev_err(&dev->dev,
  103607. + "could not allocate new bcm2708_fb struct\n");
  103608. + ret = -ENOMEM;
  103609. + goto free_region;
  103610. + }
  103611. +
  103612. + bcm2708_fb_debugfs_init(fb);
  103613. +
  103614. + fb->cb_base = dma_alloc_writecombine(&dev->dev, SZ_64K,
  103615. + &fb->cb_handle, GFP_KERNEL);
  103616. + if (!fb->cb_base) {
  103617. + dev_err(&dev->dev, "cannot allocate DMA CBs\n");
  103618. + ret = -ENOMEM;
  103619. + goto free_fb;
  103620. + }
  103621. +
  103622. + pr_info("BCM2708FB: allocated DMA memory %08x\n",
  103623. + fb->cb_handle);
  103624. +
  103625. + ret = bcm_dma_chan_alloc(BCM_DMA_FEATURE_BULK,
  103626. + &fb->dma_chan_base, &fb->dma_irq);
  103627. + if (ret < 0) {
  103628. + dev_err(&dev->dev, "couldn't allocate a DMA channel\n");
  103629. + goto free_cb;
  103630. + }
  103631. + fb->dma_chan = ret;
  103632. +
  103633. + ret = request_irq(fb->dma_irq, bcm2708_fb_dma_irq,
  103634. + 0, "bcm2708_fb dma", fb);
  103635. + if (ret) {
  103636. + pr_err("%s: failed to request DMA irq\n", __func__);
  103637. + goto free_dma_chan;
  103638. + }
  103639. +
  103640. +
  103641. + pr_info("BCM2708FB: allocated DMA channel %d @ %p\n",
  103642. + fb->dma_chan, fb->dma_chan_base);
  103643. +
  103644. + fb->dev = dev;
  103645. +
  103646. + ret = bcm2708_fb_register(fb);
  103647. + if (ret == 0) {
  103648. + platform_set_drvdata(dev, fb);
  103649. + goto out;
  103650. + }
  103651. +
  103652. +free_dma_chan:
  103653. + bcm_dma_chan_free(fb->dma_chan);
  103654. +free_cb:
  103655. + dma_free_writecombine(&dev->dev, SZ_64K, fb->cb_base, fb->cb_handle);
  103656. +free_fb:
  103657. + kfree(fb);
  103658. +free_region:
  103659. + dev_err(&dev->dev, "probe failed, err %d\n", ret);
  103660. +out:
  103661. + return ret;
  103662. +}
  103663. +
  103664. +static int bcm2708_fb_remove(struct platform_device *dev)
  103665. +{
  103666. + struct bcm2708_fb *fb = platform_get_drvdata(dev);
  103667. +
  103668. + platform_set_drvdata(dev, NULL);
  103669. +
  103670. + if (fb->fb.screen_base)
  103671. + iounmap(fb->fb.screen_base);
  103672. + unregister_framebuffer(&fb->fb);
  103673. +
  103674. + dma_free_writecombine(&dev->dev, SZ_64K, fb->cb_base, fb->cb_handle);
  103675. + bcm_dma_chan_free(fb->dma_chan);
  103676. +
  103677. + dma_free_coherent(NULL, PAGE_ALIGN(sizeof(*fb->info)), (void *)fb->info,
  103678. + fb->dma);
  103679. + bcm2708_fb_debugfs_deinit(fb);
  103680. +
  103681. + free_irq(fb->dma_irq, fb);
  103682. +
  103683. + kfree(fb);
  103684. +
  103685. + return 0;
  103686. +}
  103687. +
  103688. +static struct platform_driver bcm2708_fb_driver = {
  103689. + .probe = bcm2708_fb_probe,
  103690. + .remove = bcm2708_fb_remove,
  103691. + .driver = {
  103692. + .name = DRIVER_NAME,
  103693. + .owner = THIS_MODULE,
  103694. + },
  103695. +};
  103696. +
  103697. +static int __init bcm2708_fb_init(void)
  103698. +{
  103699. + return platform_driver_register(&bcm2708_fb_driver);
  103700. +}
  103701. +
  103702. +module_init(bcm2708_fb_init);
  103703. +
  103704. +static void __exit bcm2708_fb_exit(void)
  103705. +{
  103706. + platform_driver_unregister(&bcm2708_fb_driver);
  103707. +}
  103708. +
  103709. +module_exit(bcm2708_fb_exit);
  103710. +
  103711. +module_param(fbwidth, int, 0644);
  103712. +module_param(fbheight, int, 0644);
  103713. +module_param(fbdepth, int, 0644);
  103714. +module_param(fbswap, int, 0644);
  103715. +
  103716. +MODULE_DESCRIPTION("BCM2708 framebuffer driver");
  103717. +MODULE_LICENSE("GPL");
  103718. +
  103719. +MODULE_PARM_DESC(fbwidth, "Width of ARM Framebuffer");
  103720. +MODULE_PARM_DESC(fbheight, "Height of ARM Framebuffer");
  103721. +MODULE_PARM_DESC(fbdepth, "Bit depth of ARM Framebuffer");
  103722. +MODULE_PARM_DESC(fbswap, "Swap order of red and blue in 24 and 32 bit modes");
  103723. diff -Nur linux-3.12.33/drivers/video/cfbimgblt.c linux-3.12.33-rpi/drivers/video/cfbimgblt.c
  103724. --- linux-3.12.33/drivers/video/cfbimgblt.c 2014-11-15 06:28:07.000000000 -0600
  103725. +++ linux-3.12.33-rpi/drivers/video/cfbimgblt.c 2014-12-03 19:13:42.492418001 -0600
  103726. @@ -28,6 +28,11 @@
  103727. *
  103728. * Also need to add code to deal with cards endians that are different than
  103729. * the native cpu endians. I also need to deal with MSB position in the word.
  103730. + * Modified by Harm Hanemaaijer (fgenfb@yahoo.com) 2013:
  103731. + * - Provide optimized versions of fast_imageblit for 16 and 32bpp that are
  103732. + * significantly faster than the previous implementation.
  103733. + * - Simplify the fast/slow_imageblit selection code, avoiding integer
  103734. + * divides.
  103735. */
  103736. #include <linux/module.h>
  103737. #include <linux/string.h>
  103738. @@ -262,6 +267,133 @@
  103739. }
  103740. }
  103741. +/*
  103742. + * Optimized fast_imageblit for bpp == 16. ppw = 2, bit_mask = 3 folded
  103743. + * into the code, main loop unrolled.
  103744. + */
  103745. +
  103746. +static inline void fast_imageblit16(const struct fb_image *image,
  103747. + struct fb_info *p, u8 __iomem * dst1,
  103748. + u32 fgcolor, u32 bgcolor)
  103749. +{
  103750. + u32 fgx = fgcolor, bgx = bgcolor;
  103751. + u32 spitch = (image->width + 7) / 8;
  103752. + u32 end_mask, eorx;
  103753. + const char *s = image->data, *src;
  103754. + u32 __iomem *dst;
  103755. + const u32 *tab = NULL;
  103756. + int i, j, k;
  103757. +
  103758. + tab = fb_be_math(p) ? cfb_tab16_be : cfb_tab16_le;
  103759. +
  103760. + fgx <<= 16;
  103761. + bgx <<= 16;
  103762. + fgx |= fgcolor;
  103763. + bgx |= bgcolor;
  103764. +
  103765. + eorx = fgx ^ bgx;
  103766. + k = image->width / 2;
  103767. +
  103768. + for (i = image->height; i--;) {
  103769. + dst = (u32 __iomem *) dst1;
  103770. + src = s;
  103771. +
  103772. + j = k;
  103773. + while (j >= 4) {
  103774. + u8 bits = *src;
  103775. + end_mask = tab[(bits >> 6) & 3];
  103776. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  103777. + end_mask = tab[(bits >> 4) & 3];
  103778. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  103779. + end_mask = tab[(bits >> 2) & 3];
  103780. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  103781. + end_mask = tab[bits & 3];
  103782. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  103783. + src++;
  103784. + j -= 4;
  103785. + }
  103786. + if (j != 0) {
  103787. + u8 bits = *src;
  103788. + end_mask = tab[(bits >> 6) & 3];
  103789. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  103790. + if (j >= 2) {
  103791. + end_mask = tab[(bits >> 4) & 3];
  103792. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  103793. + if (j == 3) {
  103794. + end_mask = tab[(bits >> 2) & 3];
  103795. + FB_WRITEL((end_mask & eorx) ^ bgx, dst);
  103796. + }
  103797. + }
  103798. + }
  103799. + dst1 += p->fix.line_length;
  103800. + s += spitch;
  103801. + }
  103802. +}
  103803. +
  103804. +/*
  103805. + * Optimized fast_imageblit for bpp == 32. ppw = 1, bit_mask = 1 folded
  103806. + * into the code, main loop unrolled.
  103807. + */
  103808. +
  103809. +static inline void fast_imageblit32(const struct fb_image *image,
  103810. + struct fb_info *p, u8 __iomem * dst1,
  103811. + u32 fgcolor, u32 bgcolor)
  103812. +{
  103813. + u32 fgx = fgcolor, bgx = bgcolor;
  103814. + u32 spitch = (image->width + 7) / 8;
  103815. + u32 end_mask, eorx;
  103816. + const char *s = image->data, *src;
  103817. + u32 __iomem *dst;
  103818. + const u32 *tab = NULL;
  103819. + int i, j, k;
  103820. +
  103821. + tab = cfb_tab32;
  103822. +
  103823. + eorx = fgx ^ bgx;
  103824. + k = image->width;
  103825. +
  103826. + for (i = image->height; i--;) {
  103827. + dst = (u32 __iomem *) dst1;
  103828. + src = s;
  103829. +
  103830. + j = k;
  103831. + while (j >= 8) {
  103832. + u8 bits = *src;
  103833. + end_mask = tab[(bits >> 7) & 1];
  103834. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  103835. + end_mask = tab[(bits >> 6) & 1];
  103836. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  103837. + end_mask = tab[(bits >> 5) & 1];
  103838. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  103839. + end_mask = tab[(bits >> 4) & 1];
  103840. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  103841. + end_mask = tab[(bits >> 3) & 1];
  103842. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  103843. + end_mask = tab[(bits >> 2) & 1];
  103844. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  103845. + end_mask = tab[(bits >> 1) & 1];
  103846. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  103847. + end_mask = tab[bits & 1];
  103848. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  103849. + src++;
  103850. + j -= 8;
  103851. + }
  103852. + if (j != 0) {
  103853. + u32 bits = (u32) * src;
  103854. + while (j > 1) {
  103855. + end_mask = tab[(bits >> 7) & 1];
  103856. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  103857. + bits <<= 1;
  103858. + j--;
  103859. + }
  103860. + end_mask = tab[(bits >> 7) & 1];
  103861. + FB_WRITEL((end_mask & eorx) ^ bgx, dst);
  103862. + }
  103863. + dst1 += p->fix.line_length;
  103864. + s += spitch;
  103865. + }
  103866. +}
  103867. +
  103868. void cfb_imageblit(struct fb_info *p, const struct fb_image *image)
  103869. {
  103870. u32 fgcolor, bgcolor, start_index, bitstart, pitch_index = 0;
  103871. @@ -294,11 +426,21 @@
  103872. bgcolor = image->bg_color;
  103873. }
  103874. - if (32 % bpp == 0 && !start_index && !pitch_index &&
  103875. - ((width & (32/bpp-1)) == 0) &&
  103876. - bpp >= 8 && bpp <= 32)
  103877. - fast_imageblit(image, p, dst1, fgcolor, bgcolor);
  103878. - else
  103879. + if (!start_index && !pitch_index) {
  103880. + if (bpp == 32)
  103881. + fast_imageblit32(image, p, dst1, fgcolor,
  103882. + bgcolor);
  103883. + else if (bpp == 16 && (width & 1) == 0)
  103884. + fast_imageblit16(image, p, dst1, fgcolor,
  103885. + bgcolor);
  103886. + else if (bpp == 8 && (width & 3) == 0)
  103887. + fast_imageblit(image, p, dst1, fgcolor,
  103888. + bgcolor);
  103889. + else
  103890. + slow_imageblit(image, p, dst1, fgcolor,
  103891. + bgcolor,
  103892. + start_index, pitch_index);
  103893. + } else
  103894. slow_imageblit(image, p, dst1, fgcolor, bgcolor,
  103895. start_index, pitch_index);
  103896. } else
  103897. diff -Nur linux-3.12.33/drivers/video/fbmem.c linux-3.12.33-rpi/drivers/video/fbmem.c
  103898. --- linux-3.12.33/drivers/video/fbmem.c 2014-11-15 06:28:07.000000000 -0600
  103899. +++ linux-3.12.33-rpi/drivers/video/fbmem.c 2014-12-03 19:13:42.500418001 -0600
  103900. @@ -1083,6 +1083,25 @@
  103901. }
  103902. EXPORT_SYMBOL(fb_blank);
  103903. +static int fb_copyarea_user(struct fb_info *info,
  103904. + struct fb_copyarea *copy)
  103905. +{
  103906. + int ret = 0;
  103907. + if (!lock_fb_info(info))
  103908. + return -ENODEV;
  103909. + if (copy->dx + copy->width > info->var.xres ||
  103910. + copy->sx + copy->width > info->var.xres ||
  103911. + copy->dy + copy->height > info->var.yres ||
  103912. + copy->sy + copy->height > info->var.yres) {
  103913. + ret = -EINVAL;
  103914. + goto out;
  103915. + }
  103916. + info->fbops->fb_copyarea(info, copy);
  103917. +out:
  103918. + unlock_fb_info(info);
  103919. + return ret;
  103920. +}
  103921. +
  103922. static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
  103923. unsigned long arg)
  103924. {
  103925. @@ -1093,6 +1112,7 @@
  103926. struct fb_cmap cmap_from;
  103927. struct fb_cmap_user cmap;
  103928. struct fb_event event;
  103929. + struct fb_copyarea copy;
  103930. void __user *argp = (void __user *)arg;
  103931. long ret = 0;
  103932. @@ -1210,6 +1230,15 @@
  103933. unlock_fb_info(info);
  103934. console_unlock();
  103935. break;
  103936. + case FBIOCOPYAREA:
  103937. + if (info->flags & FBINFO_HWACCEL_COPYAREA) {
  103938. + /* only provide this ioctl if it is accelerated */
  103939. + if (copy_from_user(&copy, argp, sizeof(copy)))
  103940. + return -EFAULT;
  103941. + ret = fb_copyarea_user(info, &copy);
  103942. + break;
  103943. + }
  103944. + /* fall through */
  103945. default:
  103946. if (!lock_fb_info(info))
  103947. return -ENODEV;
  103948. @@ -1364,6 +1393,7 @@
  103949. case FBIOPAN_DISPLAY:
  103950. case FBIOGET_CON2FBMAP:
  103951. case FBIOPUT_CON2FBMAP:
  103952. + case FBIOCOPYAREA:
  103953. arg = (unsigned long) compat_ptr(arg);
  103954. case FBIOBLANK:
  103955. ret = do_fb_ioctl(info, cmd, arg);
  103956. diff -Nur linux-3.12.33/drivers/video/Kconfig linux-3.12.33-rpi/drivers/video/Kconfig
  103957. --- linux-3.12.33/drivers/video/Kconfig 2014-11-15 06:28:07.000000000 -0600
  103958. +++ linux-3.12.33-rpi/drivers/video/Kconfig 2014-12-03 19:13:40.336418001 -0600
  103959. @@ -310,6 +310,20 @@
  103960. help
  103961. Support the Permedia2 FIFO disconnect feature.
  103962. +config FB_BCM2708
  103963. + tristate "BCM2708 framebuffer support"
  103964. + depends on FB && ARM
  103965. + select FB_CFB_FILLRECT
  103966. + select FB_CFB_COPYAREA
  103967. + select FB_CFB_IMAGEBLIT
  103968. + help
  103969. + This framebuffer device driver is for the BCM2708 framebuffer.
  103970. +
  103971. + If you want to compile this as a module (=code which can be
  103972. + inserted into and removed from the running kernel), say M
  103973. + here and read <file:Documentation/kbuild/modules.txt>. The module
  103974. + will be called bcm2708_fb.
  103975. +
  103976. config FB_ARMCLCD
  103977. tristate "ARM PrimeCell PL110 support"
  103978. depends on FB && ARM && ARM_AMBA
  103979. diff -Nur linux-3.12.33/drivers/video/logo/logo_linux_clut224.ppm linux-3.12.33-rpi/drivers/video/logo/logo_linux_clut224.ppm
  103980. --- linux-3.12.33/drivers/video/logo/logo_linux_clut224.ppm 2014-11-15 06:28:07.000000000 -0600
  103981. +++ linux-3.12.33-rpi/drivers/video/logo/logo_linux_clut224.ppm 2014-12-03 19:13:42.524418001 -0600
  103982. @@ -1,1604 +1,883 @@
  103983. P3
  103984. -# Standard 224-color Linux logo
  103985. -80 80
  103986. +63 80
  103987. 255
  103988. - 0 0 0 0 0 0 0 0 0 0 0 0
  103989. - 0 0 0 0 0 0 0 0 0 0 0 0
  103990. - 0 0 0 0 0 0 0 0 0 0 0 0
  103991. - 0 0 0 0 0 0 0 0 0 0 0 0
  103992. - 0 0 0 0 0 0 0 0 0 0 0 0
  103993. - 0 0 0 0 0 0 0 0 0 0 0 0
  103994. - 0 0 0 0 0 0 0 0 0 0 0 0
  103995. - 0 0 0 0 0 0 0 0 0 0 0 0
  103996. - 0 0 0 0 0 0 0 0 0 0 0 0
  103997. - 6 6 6 6 6 6 10 10 10 10 10 10
  103998. - 10 10 10 6 6 6 6 6 6 6 6 6
  103999. - 0 0 0 0 0 0 0 0 0 0 0 0
  104000. - 0 0 0 0 0 0 0 0 0 0 0 0
  104001. - 0 0 0 0 0 0 0 0 0 0 0 0
  104002. - 0 0 0 0 0 0 0 0 0 0 0 0
  104003. - 0 0 0 0 0 0 0 0 0 0 0 0
  104004. - 0 0 0 0 0 0 0 0 0 0 0 0
  104005. - 0 0 0 0 0 0 0 0 0 0 0 0
  104006. - 0 0 0 0 0 0 0 0 0 0 0 0
  104007. - 0 0 0 0 0 0 0 0 0 0 0 0
  104008. - 0 0 0 0 0 0 0 0 0 0 0 0
  104009. - 0 0 0 0 0 0 0 0 0 0 0 0
  104010. - 0 0 0 0 0 0 0 0 0 0 0 0
  104011. - 0 0 0 0 0 0 0 0 0 0 0 0
  104012. - 0 0 0 0 0 0 0 0 0 0 0 0
  104013. - 0 0 0 0 0 0 0 0 0 0 0 0
  104014. - 0 0 0 0 0 0 0 0 0 0 0 0
  104015. - 0 0 0 0 0 0 0 0 0 0 0 0
  104016. - 0 0 0 6 6 6 10 10 10 14 14 14
  104017. - 22 22 22 26 26 26 30 30 30 34 34 34
  104018. - 30 30 30 30 30 30 26 26 26 18 18 18
  104019. - 14 14 14 10 10 10 6 6 6 0 0 0
  104020. - 0 0 0 0 0 0 0 0 0 0 0 0
  104021. - 0 0 0 0 0 0 0 0 0 0 0 0
  104022. - 0 0 0 0 0 0 0 0 0 0 0 0
  104023. - 0 0 0 0 0 0 0 0 0 0 0 0
  104024. - 0 0 0 0 0 0 0 0 0 0 0 0
  104025. - 0 0 0 0 0 0 0 0 0 0 0 0
  104026. - 0 0 0 0 0 0 0 0 0 0 0 0
  104027. - 0 0 0 0 0 0 0 0 0 0 0 0
  104028. - 0 0 0 0 0 0 0 0 0 0 0 0
  104029. - 0 0 0 0 0 1 0 0 1 0 0 0
  104030. - 0 0 0 0 0 0 0 0 0 0 0 0
  104031. - 0 0 0 0 0 0 0 0 0 0 0 0
  104032. - 0 0 0 0 0 0 0 0 0 0 0 0
  104033. - 0 0 0 0 0 0 0 0 0 0 0 0
  104034. - 0 0 0 0 0 0 0 0 0 0 0 0
  104035. - 0 0 0 0 0 0 0 0 0 0 0 0
  104036. - 6 6 6 14 14 14 26 26 26 42 42 42
  104037. - 54 54 54 66 66 66 78 78 78 78 78 78
  104038. - 78 78 78 74 74 74 66 66 66 54 54 54
  104039. - 42 42 42 26 26 26 18 18 18 10 10 10
  104040. - 6 6 6 0 0 0 0 0 0 0 0 0
  104041. - 0 0 0 0 0 0 0 0 0 0 0 0
  104042. - 0 0 0 0 0 0 0 0 0 0 0 0
  104043. - 0 0 0 0 0 0 0 0 0 0 0 0
  104044. - 0 0 0 0 0 0 0 0 0 0 0 0
  104045. - 0 0 0 0 0 0 0 0 0 0 0 0
  104046. - 0 0 0 0 0 0 0 0 0 0 0 0
  104047. - 0 0 0 0 0 0 0 0 0 0 0 0
  104048. - 0 0 0 0 0 0 0 0 0 0 0 0
  104049. - 0 0 1 0 0 0 0 0 0 0 0 0
  104050. - 0 0 0 0 0 0 0 0 0 0 0 0
  104051. - 0 0 0 0 0 0 0 0 0 0 0 0
  104052. - 0 0 0 0 0 0 0 0 0 0 0 0
  104053. - 0 0 0 0 0 0 0 0 0 0 0 0
  104054. - 0 0 0 0 0 0 0 0 0 0 0 0
  104055. - 0 0 0 0 0 0 0 0 0 10 10 10
  104056. - 22 22 22 42 42 42 66 66 66 86 86 86
  104057. - 66 66 66 38 38 38 38 38 38 22 22 22
  104058. - 26 26 26 34 34 34 54 54 54 66 66 66
  104059. - 86 86 86 70 70 70 46 46 46 26 26 26
  104060. - 14 14 14 6 6 6 0 0 0 0 0 0
  104061. - 0 0 0 0 0 0 0 0 0 0 0 0
  104062. - 0 0 0 0 0 0 0 0 0 0 0 0
  104063. - 0 0 0 0 0 0 0 0 0 0 0 0
  104064. - 0 0 0 0 0 0 0 0 0 0 0 0
  104065. - 0 0 0 0 0 0 0 0 0 0 0 0
  104066. - 0 0 0 0 0 0 0 0 0 0 0 0
  104067. - 0 0 0 0 0 0 0 0 0 0 0 0
  104068. - 0 0 0 0 0 0 0 0 0 0 0 0
  104069. - 0 0 1 0 0 1 0 0 1 0 0 0
  104070. - 0 0 0 0 0 0 0 0 0 0 0 0
  104071. - 0 0 0 0 0 0 0 0 0 0 0 0
  104072. - 0 0 0 0 0 0 0 0 0 0 0 0
  104073. - 0 0 0 0 0 0 0 0 0 0 0 0
  104074. - 0 0 0 0 0 0 0 0 0 0 0 0
  104075. - 0 0 0 0 0 0 10 10 10 26 26 26
  104076. - 50 50 50 82 82 82 58 58 58 6 6 6
  104077. - 2 2 6 2 2 6 2 2 6 2 2 6
  104078. - 2 2 6 2 2 6 2 2 6 2 2 6
  104079. - 6 6 6 54 54 54 86 86 86 66 66 66
  104080. - 38 38 38 18 18 18 6 6 6 0 0 0
  104081. - 0 0 0 0 0 0 0 0 0 0 0 0
  104082. - 0 0 0 0 0 0 0 0 0 0 0 0
  104083. - 0 0 0 0 0 0 0 0 0 0 0 0
  104084. - 0 0 0 0 0 0 0 0 0 0 0 0
  104085. - 0 0 0 0 0 0 0 0 0 0 0 0
  104086. - 0 0 0 0 0 0 0 0 0 0 0 0
  104087. - 0 0 0 0 0 0 0 0 0 0 0 0
  104088. - 0 0 0 0 0 0 0 0 0 0 0 0
  104089. - 0 0 0 0 0 0 0 0 0 0 0 0
  104090. - 0 0 0 0 0 0 0 0 0 0 0 0
  104091. - 0 0 0 0 0 0 0 0 0 0 0 0
  104092. - 0 0 0 0 0 0 0 0 0 0 0 0
  104093. - 0 0 0 0 0 0 0 0 0 0 0 0
  104094. - 0 0 0 0 0 0 0 0 0 0 0 0
  104095. - 0 0 0 6 6 6 22 22 22 50 50 50
  104096. - 78 78 78 34 34 34 2 2 6 2 2 6
  104097. - 2 2 6 2 2 6 2 2 6 2 2 6
  104098. - 2 2 6 2 2 6 2 2 6 2 2 6
  104099. - 2 2 6 2 2 6 6 6 6 70 70 70
  104100. - 78 78 78 46 46 46 22 22 22 6 6 6
  104101. - 0 0 0 0 0 0 0 0 0 0 0 0
  104102. - 0 0 0 0 0 0 0 0 0 0 0 0
  104103. - 0 0 0 0 0 0 0 0 0 0 0 0
  104104. - 0 0 0 0 0 0 0 0 0 0 0 0
  104105. - 0 0 0 0 0 0 0 0 0 0 0 0
  104106. - 0 0 0 0 0 0 0 0 0 0 0 0
  104107. - 0 0 0 0 0 0 0 0 0 0 0 0
  104108. - 0 0 0 0 0 0 0 0 0 0 0 0
  104109. - 0 0 1 0 0 1 0 0 1 0 0 0
  104110. - 0 0 0 0 0 0 0 0 0 0 0 0
  104111. - 0 0 0 0 0 0 0 0 0 0 0 0
  104112. - 0 0 0 0 0 0 0 0 0 0 0 0
  104113. - 0 0 0 0 0 0 0 0 0 0 0 0
  104114. - 0 0 0 0 0 0 0 0 0 0 0 0
  104115. - 6 6 6 18 18 18 42 42 42 82 82 82
  104116. - 26 26 26 2 2 6 2 2 6 2 2 6
  104117. - 2 2 6 2 2 6 2 2 6 2 2 6
  104118. - 2 2 6 2 2 6 2 2 6 14 14 14
  104119. - 46 46 46 34 34 34 6 6 6 2 2 6
  104120. - 42 42 42 78 78 78 42 42 42 18 18 18
  104121. - 6 6 6 0 0 0 0 0 0 0 0 0
  104122. - 0 0 0 0 0 0 0 0 0 0 0 0
  104123. - 0 0 0 0 0 0 0 0 0 0 0 0
  104124. - 0 0 0 0 0 0 0 0 0 0 0 0
  104125. - 0 0 0 0 0 0 0 0 0 0 0 0
  104126. - 0 0 0 0 0 0 0 0 0 0 0 0
  104127. - 0 0 0 0 0 0 0 0 0 0 0 0
  104128. - 0 0 0 0 0 0 0 0 0 0 0 0
  104129. - 0 0 1 0 0 0 0 0 1 0 0 0
  104130. - 0 0 0 0 0 0 0 0 0 0 0 0
  104131. - 0 0 0 0 0 0 0 0 0 0 0 0
  104132. - 0 0 0 0 0 0 0 0 0 0 0 0
  104133. - 0 0 0 0 0 0 0 0 0 0 0 0
  104134. - 0 0 0 0 0 0 0 0 0 0 0 0
  104135. - 10 10 10 30 30 30 66 66 66 58 58 58
  104136. - 2 2 6 2 2 6 2 2 6 2 2 6
  104137. - 2 2 6 2 2 6 2 2 6 2 2 6
  104138. - 2 2 6 2 2 6 2 2 6 26 26 26
  104139. - 86 86 86 101 101 101 46 46 46 10 10 10
  104140. - 2 2 6 58 58 58 70 70 70 34 34 34
  104141. - 10 10 10 0 0 0 0 0 0 0 0 0
  104142. - 0 0 0 0 0 0 0 0 0 0 0 0
  104143. - 0 0 0 0 0 0 0 0 0 0 0 0
  104144. - 0 0 0 0 0 0 0 0 0 0 0 0
  104145. - 0 0 0 0 0 0 0 0 0 0 0 0
  104146. - 0 0 0 0 0 0 0 0 0 0 0 0
  104147. - 0 0 0 0 0 0 0 0 0 0 0 0
  104148. - 0 0 0 0 0 0 0 0 0 0 0 0
  104149. - 0 0 1 0 0 1 0 0 1 0 0 0
  104150. - 0 0 0 0 0 0 0 0 0 0 0 0
  104151. - 0 0 0 0 0 0 0 0 0 0 0 0
  104152. - 0 0 0 0 0 0 0 0 0 0 0 0
  104153. - 0 0 0 0 0 0 0 0 0 0 0 0
  104154. - 0 0 0 0 0 0 0 0 0 0 0 0
  104155. - 14 14 14 42 42 42 86 86 86 10 10 10
  104156. - 2 2 6 2 2 6 2 2 6 2 2 6
  104157. - 2 2 6 2 2 6 2 2 6 2 2 6
  104158. - 2 2 6 2 2 6 2 2 6 30 30 30
  104159. - 94 94 94 94 94 94 58 58 58 26 26 26
  104160. - 2 2 6 6 6 6 78 78 78 54 54 54
  104161. - 22 22 22 6 6 6 0 0 0 0 0 0
  104162. - 0 0 0 0 0 0 0 0 0 0 0 0
  104163. - 0 0 0 0 0 0 0 0 0 0 0 0
  104164. - 0 0 0 0 0 0 0 0 0 0 0 0
  104165. - 0 0 0 0 0 0 0 0 0 0 0 0
  104166. - 0 0 0 0 0 0 0 0 0 0 0 0
  104167. - 0 0 0 0 0 0 0 0 0 0 0 0
  104168. - 0 0 0 0 0 0 0 0 0 0 0 0
  104169. - 0 0 0 0 0 0 0 0 0 0 0 0
  104170. - 0 0 0 0 0 0 0 0 0 0 0 0
  104171. - 0 0 0 0 0 0 0 0 0 0 0 0
  104172. - 0 0 0 0 0 0 0 0 0 0 0 0
  104173. - 0 0 0 0 0 0 0 0 0 0 0 0
  104174. - 0 0 0 0 0 0 0 0 0 6 6 6
  104175. - 22 22 22 62 62 62 62 62 62 2 2 6
  104176. - 2 2 6 2 2 6 2 2 6 2 2 6
  104177. - 2 2 6 2 2 6 2 2 6 2 2 6
  104178. - 2 2 6 2 2 6 2 2 6 26 26 26
  104179. - 54 54 54 38 38 38 18 18 18 10 10 10
  104180. - 2 2 6 2 2 6 34 34 34 82 82 82
  104181. - 38 38 38 14 14 14 0 0 0 0 0 0
  104182. - 0 0 0 0 0 0 0 0 0 0 0 0
  104183. - 0 0 0 0 0 0 0 0 0 0 0 0
  104184. - 0 0 0 0 0 0 0 0 0 0 0 0
  104185. - 0 0 0 0 0 0 0 0 0 0 0 0
  104186. - 0 0 0 0 0 0 0 0 0 0 0 0
  104187. - 0 0 0 0 0 0 0 0 0 0 0 0
  104188. - 0 0 0 0 0 0 0 0 0 0 0 0
  104189. - 0 0 0 0 0 1 0 0 1 0 0 0
  104190. - 0 0 0 0 0 0 0 0 0 0 0 0
  104191. - 0 0 0 0 0 0 0 0 0 0 0 0
  104192. - 0 0 0 0 0 0 0 0 0 0 0 0
  104193. - 0 0 0 0 0 0 0 0 0 0 0 0
  104194. - 0 0 0 0 0 0 0 0 0 6 6 6
  104195. - 30 30 30 78 78 78 30 30 30 2 2 6
  104196. - 2 2 6 2 2 6 2 2 6 2 2 6
  104197. - 2 2 6 2 2 6 2 2 6 2 2 6
  104198. - 2 2 6 2 2 6 2 2 6 10 10 10
  104199. - 10 10 10 2 2 6 2 2 6 2 2 6
  104200. - 2 2 6 2 2 6 2 2 6 78 78 78
  104201. - 50 50 50 18 18 18 6 6 6 0 0 0
  104202. - 0 0 0 0 0 0 0 0 0 0 0 0
  104203. - 0 0 0 0 0 0 0 0 0 0 0 0
  104204. - 0 0 0 0 0 0 0 0 0 0 0 0
  104205. - 0 0 0 0 0 0 0 0 0 0 0 0
  104206. - 0 0 0 0 0 0 0 0 0 0 0 0
  104207. - 0 0 0 0 0 0 0 0 0 0 0 0
  104208. - 0 0 0 0 0 0 0 0 0 0 0 0
  104209. - 0 0 1 0 0 0 0 0 0 0 0 0
  104210. - 0 0 0 0 0 0 0 0 0 0 0 0
  104211. - 0 0 0 0 0 0 0 0 0 0 0 0
  104212. - 0 0 0 0 0 0 0 0 0 0 0 0
  104213. - 0 0 0 0 0 0 0 0 0 0 0 0
  104214. - 0 0 0 0 0 0 0 0 0 10 10 10
  104215. - 38 38 38 86 86 86 14 14 14 2 2 6
  104216. - 2 2 6 2 2 6 2 2 6 2 2 6
  104217. - 2 2 6 2 2 6 2 2 6 2 2 6
  104218. - 2 2 6 2 2 6 2 2 6 2 2 6
  104219. - 2 2 6 2 2 6 2 2 6 2 2 6
  104220. - 2 2 6 2 2 6 2 2 6 54 54 54
  104221. - 66 66 66 26 26 26 6 6 6 0 0 0
  104222. - 0 0 0 0 0 0 0 0 0 0 0 0
  104223. - 0 0 0 0 0 0 0 0 0 0 0 0
  104224. - 0 0 0 0 0 0 0 0 0 0 0 0
  104225. - 0 0 0 0 0 0 0 0 0 0 0 0
  104226. - 0 0 0 0 0 0 0 0 0 0 0 0
  104227. - 0 0 0 0 0 0 0 0 0 0 0 0
  104228. - 0 0 0 0 0 0 0 0 0 0 0 0
  104229. - 0 0 0 0 0 1 0 0 1 0 0 0
  104230. - 0 0 0 0 0 0 0 0 0 0 0 0
  104231. - 0 0 0 0 0 0 0 0 0 0 0 0
  104232. - 0 0 0 0 0 0 0 0 0 0 0 0
  104233. - 0 0 0 0 0 0 0 0 0 0 0 0
  104234. - 0 0 0 0 0 0 0 0 0 14 14 14
  104235. - 42 42 42 82 82 82 2 2 6 2 2 6
  104236. - 2 2 6 6 6 6 10 10 10 2 2 6
  104237. - 2 2 6 2 2 6 2 2 6 2 2 6
  104238. - 2 2 6 2 2 6 2 2 6 6 6 6
  104239. - 14 14 14 10 10 10 2 2 6 2 2 6
  104240. - 2 2 6 2 2 6 2 2 6 18 18 18
  104241. - 82 82 82 34 34 34 10 10 10 0 0 0
  104242. - 0 0 0 0 0 0 0 0 0 0 0 0
  104243. - 0 0 0 0 0 0 0 0 0 0 0 0
  104244. - 0 0 0 0 0 0 0 0 0 0 0 0
  104245. - 0 0 0 0 0 0 0 0 0 0 0 0
  104246. - 0 0 0 0 0 0 0 0 0 0 0 0
  104247. - 0 0 0 0 0 0 0 0 0 0 0 0
  104248. - 0 0 0 0 0 0 0 0 0 0 0 0
  104249. - 0 0 1 0 0 0 0 0 0 0 0 0
  104250. - 0 0 0 0 0 0 0 0 0 0 0 0
  104251. - 0 0 0 0 0 0 0 0 0 0 0 0
  104252. - 0 0 0 0 0 0 0 0 0 0 0 0
  104253. - 0 0 0 0 0 0 0 0 0 0 0 0
  104254. - 0 0 0 0 0 0 0 0 0 14 14 14
  104255. - 46 46 46 86 86 86 2 2 6 2 2 6
  104256. - 6 6 6 6 6 6 22 22 22 34 34 34
  104257. - 6 6 6 2 2 6 2 2 6 2 2 6
  104258. - 2 2 6 2 2 6 18 18 18 34 34 34
  104259. - 10 10 10 50 50 50 22 22 22 2 2 6
  104260. - 2 2 6 2 2 6 2 2 6 10 10 10
  104261. - 86 86 86 42 42 42 14 14 14 0 0 0
  104262. - 0 0 0 0 0 0 0 0 0 0 0 0
  104263. - 0 0 0 0 0 0 0 0 0 0 0 0
  104264. - 0 0 0 0 0 0 0 0 0 0 0 0
  104265. - 0 0 0 0 0 0 0 0 0 0 0 0
  104266. - 0 0 0 0 0 0 0 0 0 0 0 0
  104267. - 0 0 0 0 0 0 0 0 0 0 0 0
  104268. - 0 0 0 0 0 0 0 0 0 0 0 0
  104269. - 0 0 1 0 0 1 0 0 1 0 0 0
  104270. - 0 0 0 0 0 0 0 0 0 0 0 0
  104271. - 0 0 0 0 0 0 0 0 0 0 0 0
  104272. - 0 0 0 0 0 0 0 0 0 0 0 0
  104273. - 0 0 0 0 0 0 0 0 0 0 0 0
  104274. - 0 0 0 0 0 0 0 0 0 14 14 14
  104275. - 46 46 46 86 86 86 2 2 6 2 2 6
  104276. - 38 38 38 116 116 116 94 94 94 22 22 22
  104277. - 22 22 22 2 2 6 2 2 6 2 2 6
  104278. - 14 14 14 86 86 86 138 138 138 162 162 162
  104279. -154 154 154 38 38 38 26 26 26 6 6 6
  104280. - 2 2 6 2 2 6 2 2 6 2 2 6
  104281. - 86 86 86 46 46 46 14 14 14 0 0 0
  104282. - 0 0 0 0 0 0 0 0 0 0 0 0
  104283. - 0 0 0 0 0 0 0 0 0 0 0 0
  104284. - 0 0 0 0 0 0 0 0 0 0 0 0
  104285. - 0 0 0 0 0 0 0 0 0 0 0 0
  104286. - 0 0 0 0 0 0 0 0 0 0 0 0
  104287. - 0 0 0 0 0 0 0 0 0 0 0 0
  104288. - 0 0 0 0 0 0 0 0 0 0 0 0
  104289. - 0 0 0 0 0 0 0 0 0 0 0 0
  104290. - 0 0 0 0 0 0 0 0 0 0 0 0
  104291. - 0 0 0 0 0 0 0 0 0 0 0 0
  104292. - 0 0 0 0 0 0 0 0 0 0 0 0
  104293. - 0 0 0 0 0 0 0 0 0 0 0 0
  104294. - 0 0 0 0 0 0 0 0 0 14 14 14
  104295. - 46 46 46 86 86 86 2 2 6 14 14 14
  104296. -134 134 134 198 198 198 195 195 195 116 116 116
  104297. - 10 10 10 2 2 6 2 2 6 6 6 6
  104298. -101 98 89 187 187 187 210 210 210 218 218 218
  104299. -214 214 214 134 134 134 14 14 14 6 6 6
  104300. - 2 2 6 2 2 6 2 2 6 2 2 6
  104301. - 86 86 86 50 50 50 18 18 18 6 6 6
  104302. - 0 0 0 0 0 0 0 0 0 0 0 0
  104303. - 0 0 0 0 0 0 0 0 0 0 0 0
  104304. - 0 0 0 0 0 0 0 0 0 0 0 0
  104305. - 0 0 0 0 0 0 0 0 0 0 0 0
  104306. - 0 0 0 0 0 0 0 0 0 0 0 0
  104307. - 0 0 0 0 0 0 0 0 0 0 0 0
  104308. - 0 0 0 0 0 0 0 0 1 0 0 0
  104309. - 0 0 1 0 0 1 0 0 1 0 0 0
  104310. - 0 0 0 0 0 0 0 0 0 0 0 0
  104311. - 0 0 0 0 0 0 0 0 0 0 0 0
  104312. - 0 0 0 0 0 0 0 0 0 0 0 0
  104313. - 0 0 0 0 0 0 0 0 0 0 0 0
  104314. - 0 0 0 0 0 0 0 0 0 14 14 14
  104315. - 46 46 46 86 86 86 2 2 6 54 54 54
  104316. -218 218 218 195 195 195 226 226 226 246 246 246
  104317. - 58 58 58 2 2 6 2 2 6 30 30 30
  104318. -210 210 210 253 253 253 174 174 174 123 123 123
  104319. -221 221 221 234 234 234 74 74 74 2 2 6
  104320. - 2 2 6 2 2 6 2 2 6 2 2 6
  104321. - 70 70 70 58 58 58 22 22 22 6 6 6
  104322. - 0 0 0 0 0 0 0 0 0 0 0 0
  104323. - 0 0 0 0 0 0 0 0 0 0 0 0
  104324. - 0 0 0 0 0 0 0 0 0 0 0 0
  104325. - 0 0 0 0 0 0 0 0 0 0 0 0
  104326. - 0 0 0 0 0 0 0 0 0 0 0 0
  104327. - 0 0 0 0 0 0 0 0 0 0 0 0
  104328. - 0 0 0 0 0 0 0 0 0 0 0 0
  104329. - 0 0 0 0 0 0 0 0 0 0 0 0
  104330. - 0 0 0 0 0 0 0 0 0 0 0 0
  104331. - 0 0 0 0 0 0 0 0 0 0 0 0
  104332. - 0 0 0 0 0 0 0 0 0 0 0 0
  104333. - 0 0 0 0 0 0 0 0 0 0 0 0
  104334. - 0 0 0 0 0 0 0 0 0 14 14 14
  104335. - 46 46 46 82 82 82 2 2 6 106 106 106
  104336. -170 170 170 26 26 26 86 86 86 226 226 226
  104337. -123 123 123 10 10 10 14 14 14 46 46 46
  104338. -231 231 231 190 190 190 6 6 6 70 70 70
  104339. - 90 90 90 238 238 238 158 158 158 2 2 6
  104340. - 2 2 6 2 2 6 2 2 6 2 2 6
  104341. - 70 70 70 58 58 58 22 22 22 6 6 6
  104342. - 0 0 0 0 0 0 0 0 0 0 0 0
  104343. - 0 0 0 0 0 0 0 0 0 0 0 0
  104344. - 0 0 0 0 0 0 0 0 0 0 0 0
  104345. - 0 0 0 0 0 0 0 0 0 0 0 0
  104346. - 0 0 0 0 0 0 0 0 0 0 0 0
  104347. - 0 0 0 0 0 0 0 0 0 0 0 0
  104348. - 0 0 0 0 0 0 0 0 1 0 0 0
  104349. - 0 0 1 0 0 1 0 0 1 0 0 0
  104350. - 0 0 0 0 0 0 0 0 0 0 0 0
  104351. - 0 0 0 0 0 0 0 0 0 0 0 0
  104352. - 0 0 0 0 0 0 0 0 0 0 0 0
  104353. - 0 0 0 0 0 0 0 0 0 0 0 0
  104354. - 0 0 0 0 0 0 0 0 0 14 14 14
  104355. - 42 42 42 86 86 86 6 6 6 116 116 116
  104356. -106 106 106 6 6 6 70 70 70 149 149 149
  104357. -128 128 128 18 18 18 38 38 38 54 54 54
  104358. -221 221 221 106 106 106 2 2 6 14 14 14
  104359. - 46 46 46 190 190 190 198 198 198 2 2 6
  104360. - 2 2 6 2 2 6 2 2 6 2 2 6
  104361. - 74 74 74 62 62 62 22 22 22 6 6 6
  104362. - 0 0 0 0 0 0 0 0 0 0 0 0
  104363. - 0 0 0 0 0 0 0 0 0 0 0 0
  104364. - 0 0 0 0 0 0 0 0 0 0 0 0
  104365. - 0 0 0 0 0 0 0 0 0 0 0 0
  104366. - 0 0 0 0 0 0 0 0 0 0 0 0
  104367. - 0 0 0 0 0 0 0 0 0 0 0 0
  104368. - 0 0 0 0 0 0 0 0 1 0 0 0
  104369. - 0 0 1 0 0 0 0 0 1 0 0 0
  104370. - 0 0 0 0 0 0 0 0 0 0 0 0
  104371. - 0 0 0 0 0 0 0 0 0 0 0 0
  104372. - 0 0 0 0 0 0 0 0 0 0 0 0
  104373. - 0 0 0 0 0 0 0 0 0 0 0 0
  104374. - 0 0 0 0 0 0 0 0 0 14 14 14
  104375. - 42 42 42 94 94 94 14 14 14 101 101 101
  104376. -128 128 128 2 2 6 18 18 18 116 116 116
  104377. -118 98 46 121 92 8 121 92 8 98 78 10
  104378. -162 162 162 106 106 106 2 2 6 2 2 6
  104379. - 2 2 6 195 195 195 195 195 195 6 6 6
  104380. - 2 2 6 2 2 6 2 2 6 2 2 6
  104381. - 74 74 74 62 62 62 22 22 22 6 6 6
  104382. - 0 0 0 0 0 0 0 0 0 0 0 0
  104383. - 0 0 0 0 0 0 0 0 0 0 0 0
  104384. - 0 0 0 0 0 0 0 0 0 0 0 0
  104385. - 0 0 0 0 0 0 0 0 0 0 0 0
  104386. - 0 0 0 0 0 0 0 0 0 0 0 0
  104387. - 0 0 0 0 0 0 0 0 0 0 0 0
  104388. - 0 0 0 0 0 0 0 0 1 0 0 1
  104389. - 0 0 1 0 0 0 0 0 1 0 0 0
  104390. - 0 0 0 0 0 0 0 0 0 0 0 0
  104391. - 0 0 0 0 0 0 0 0 0 0 0 0
  104392. - 0 0 0 0 0 0 0 0 0 0 0 0
  104393. - 0 0 0 0 0 0 0 0 0 0 0 0
  104394. - 0 0 0 0 0 0 0 0 0 10 10 10
  104395. - 38 38 38 90 90 90 14 14 14 58 58 58
  104396. -210 210 210 26 26 26 54 38 6 154 114 10
  104397. -226 170 11 236 186 11 225 175 15 184 144 12
  104398. -215 174 15 175 146 61 37 26 9 2 2 6
  104399. - 70 70 70 246 246 246 138 138 138 2 2 6
  104400. - 2 2 6 2 2 6 2 2 6 2 2 6
  104401. - 70 70 70 66 66 66 26 26 26 6 6 6
  104402. - 0 0 0 0 0 0 0 0 0 0 0 0
  104403. - 0 0 0 0 0 0 0 0 0 0 0 0
  104404. - 0 0 0 0 0 0 0 0 0 0 0 0
  104405. - 0 0 0 0 0 0 0 0 0 0 0 0
  104406. - 0 0 0 0 0 0 0 0 0 0 0 0
  104407. - 0 0 0 0 0 0 0 0 0 0 0 0
  104408. - 0 0 0 0 0 0 0 0 0 0 0 0
  104409. - 0 0 0 0 0 0 0 0 0 0 0 0
  104410. - 0 0 0 0 0 0 0 0 0 0 0 0
  104411. - 0 0 0 0 0 0 0 0 0 0 0 0
  104412. - 0 0 0 0 0 0 0 0 0 0 0 0
  104413. - 0 0 0 0 0 0 0 0 0 0 0 0
  104414. - 0 0 0 0 0 0 0 0 0 10 10 10
  104415. - 38 38 38 86 86 86 14 14 14 10 10 10
  104416. -195 195 195 188 164 115 192 133 9 225 175 15
  104417. -239 182 13 234 190 10 232 195 16 232 200 30
  104418. -245 207 45 241 208 19 232 195 16 184 144 12
  104419. -218 194 134 211 206 186 42 42 42 2 2 6
  104420. - 2 2 6 2 2 6 2 2 6 2 2 6
  104421. - 50 50 50 74 74 74 30 30 30 6 6 6
  104422. - 0 0 0 0 0 0 0 0 0 0 0 0
  104423. - 0 0 0 0 0 0 0 0 0 0 0 0
  104424. - 0 0 0 0 0 0 0 0 0 0 0 0
  104425. - 0 0 0 0 0 0 0 0 0 0 0 0
  104426. - 0 0 0 0 0 0 0 0 0 0 0 0
  104427. - 0 0 0 0 0 0 0 0 0 0 0 0
  104428. - 0 0 0 0 0 0 0 0 0 0 0 0
  104429. - 0 0 0 0 0 0 0 0 0 0 0 0
  104430. - 0 0 0 0 0 0 0 0 0 0 0 0
  104431. - 0 0 0 0 0 0 0 0 0 0 0 0
  104432. - 0 0 0 0 0 0 0 0 0 0 0 0
  104433. - 0 0 0 0 0 0 0 0 0 0 0 0
  104434. - 0 0 0 0 0 0 0 0 0 10 10 10
  104435. - 34 34 34 86 86 86 14 14 14 2 2 6
  104436. -121 87 25 192 133 9 219 162 10 239 182 13
  104437. -236 186 11 232 195 16 241 208 19 244 214 54
  104438. -246 218 60 246 218 38 246 215 20 241 208 19
  104439. -241 208 19 226 184 13 121 87 25 2 2 6
  104440. - 2 2 6 2 2 6 2 2 6 2 2 6
  104441. - 50 50 50 82 82 82 34 34 34 10 10 10
  104442. - 0 0 0 0 0 0 0 0 0 0 0 0
  104443. - 0 0 0 0 0 0 0 0 0 0 0 0
  104444. - 0 0 0 0 0 0 0 0 0 0 0 0
  104445. - 0 0 0 0 0 0 0 0 0 0 0 0
  104446. - 0 0 0 0 0 0 0 0 0 0 0 0
  104447. - 0 0 0 0 0 0 0 0 0 0 0 0
  104448. - 0 0 0 0 0 0 0 0 0 0 0 0
  104449. - 0 0 0 0 0 0 0 0 0 0 0 0
  104450. - 0 0 0 0 0 0 0 0 0 0 0 0
  104451. - 0 0 0 0 0 0 0 0 0 0 0 0
  104452. - 0 0 0 0 0 0 0 0 0 0 0 0
  104453. - 0 0 0 0 0 0 0 0 0 0 0 0
  104454. - 0 0 0 0 0 0 0 0 0 10 10 10
  104455. - 34 34 34 82 82 82 30 30 30 61 42 6
  104456. -180 123 7 206 145 10 230 174 11 239 182 13
  104457. -234 190 10 238 202 15 241 208 19 246 218 74
  104458. -246 218 38 246 215 20 246 215 20 246 215 20
  104459. -226 184 13 215 174 15 184 144 12 6 6 6
  104460. - 2 2 6 2 2 6 2 2 6 2 2 6
  104461. - 26 26 26 94 94 94 42 42 42 14 14 14
  104462. - 0 0 0 0 0 0 0 0 0 0 0 0
  104463. - 0 0 0 0 0 0 0 0 0 0 0 0
  104464. - 0 0 0 0 0 0 0 0 0 0 0 0
  104465. - 0 0 0 0 0 0 0 0 0 0 0 0
  104466. - 0 0 0 0 0 0 0 0 0 0 0 0
  104467. - 0 0 0 0 0 0 0 0 0 0 0 0
  104468. - 0 0 0 0 0 0 0 0 0 0 0 0
  104469. - 0 0 0 0 0 0 0 0 0 0 0 0
  104470. - 0 0 0 0 0 0 0 0 0 0 0 0
  104471. - 0 0 0 0 0 0 0 0 0 0 0 0
  104472. - 0 0 0 0 0 0 0 0 0 0 0 0
  104473. - 0 0 0 0 0 0 0 0 0 0 0 0
  104474. - 0 0 0 0 0 0 0 0 0 10 10 10
  104475. - 30 30 30 78 78 78 50 50 50 104 69 6
  104476. -192 133 9 216 158 10 236 178 12 236 186 11
  104477. -232 195 16 241 208 19 244 214 54 245 215 43
  104478. -246 215 20 246 215 20 241 208 19 198 155 10
  104479. -200 144 11 216 158 10 156 118 10 2 2 6
  104480. - 2 2 6 2 2 6 2 2 6 2 2 6
  104481. - 6 6 6 90 90 90 54 54 54 18 18 18
  104482. - 6 6 6 0 0 0 0 0 0 0 0 0
  104483. - 0 0 0 0 0 0 0 0 0 0 0 0
  104484. - 0 0 0 0 0 0 0 0 0 0 0 0
  104485. - 0 0 0 0 0 0 0 0 0 0 0 0
  104486. - 0 0 0 0 0 0 0 0 0 0 0 0
  104487. - 0 0 0 0 0 0 0 0 0 0 0 0
  104488. - 0 0 0 0 0 0 0 0 0 0 0 0
  104489. - 0 0 0 0 0 0 0 0 0 0 0 0
  104490. - 0 0 0 0 0 0 0 0 0 0 0 0
  104491. - 0 0 0 0 0 0 0 0 0 0 0 0
  104492. - 0 0 0 0 0 0 0 0 0 0 0 0
  104493. - 0 0 0 0 0 0 0 0 0 0 0 0
  104494. - 0 0 0 0 0 0 0 0 0 10 10 10
  104495. - 30 30 30 78 78 78 46 46 46 22 22 22
  104496. -137 92 6 210 162 10 239 182 13 238 190 10
  104497. -238 202 15 241 208 19 246 215 20 246 215 20
  104498. -241 208 19 203 166 17 185 133 11 210 150 10
  104499. -216 158 10 210 150 10 102 78 10 2 2 6
  104500. - 6 6 6 54 54 54 14 14 14 2 2 6
  104501. - 2 2 6 62 62 62 74 74 74 30 30 30
  104502. - 10 10 10 0 0 0 0 0 0 0 0 0
  104503. - 0 0 0 0 0 0 0 0 0 0 0 0
  104504. - 0 0 0 0 0 0 0 0 0 0 0 0
  104505. - 0 0 0 0 0 0 0 0 0 0 0 0
  104506. - 0 0 0 0 0 0 0 0 0 0 0 0
  104507. - 0 0 0 0 0 0 0 0 0 0 0 0
  104508. - 0 0 0 0 0 0 0 0 0 0 0 0
  104509. - 0 0 0 0 0 0 0 0 0 0 0 0
  104510. - 0 0 0 0 0 0 0 0 0 0 0 0
  104511. - 0 0 0 0 0 0 0 0 0 0 0 0
  104512. - 0 0 0 0 0 0 0 0 0 0 0 0
  104513. - 0 0 0 0 0 0 0 0 0 0 0 0
  104514. - 0 0 0 0 0 0 0 0 0 10 10 10
  104515. - 34 34 34 78 78 78 50 50 50 6 6 6
  104516. - 94 70 30 139 102 15 190 146 13 226 184 13
  104517. -232 200 30 232 195 16 215 174 15 190 146 13
  104518. -168 122 10 192 133 9 210 150 10 213 154 11
  104519. -202 150 34 182 157 106 101 98 89 2 2 6
  104520. - 2 2 6 78 78 78 116 116 116 58 58 58
  104521. - 2 2 6 22 22 22 90 90 90 46 46 46
  104522. - 18 18 18 6 6 6 0 0 0 0 0 0
  104523. - 0 0 0 0 0 0 0 0 0 0 0 0
  104524. - 0 0 0 0 0 0 0 0 0 0 0 0
  104525. - 0 0 0 0 0 0 0 0 0 0 0 0
  104526. - 0 0 0 0 0 0 0 0 0 0 0 0
  104527. - 0 0 0 0 0 0 0 0 0 0 0 0
  104528. - 0 0 0 0 0 0 0 0 0 0 0 0
  104529. - 0 0 0 0 0 0 0 0 0 0 0 0
  104530. - 0 0 0 0 0 0 0 0 0 0 0 0
  104531. - 0 0 0 0 0 0 0 0 0 0 0 0
  104532. - 0 0 0 0 0 0 0 0 0 0 0 0
  104533. - 0 0 0 0 0 0 0 0 0 0 0 0
  104534. - 0 0 0 0 0 0 0 0 0 10 10 10
  104535. - 38 38 38 86 86 86 50 50 50 6 6 6
  104536. -128 128 128 174 154 114 156 107 11 168 122 10
  104537. -198 155 10 184 144 12 197 138 11 200 144 11
  104538. -206 145 10 206 145 10 197 138 11 188 164 115
  104539. -195 195 195 198 198 198 174 174 174 14 14 14
  104540. - 2 2 6 22 22 22 116 116 116 116 116 116
  104541. - 22 22 22 2 2 6 74 74 74 70 70 70
  104542. - 30 30 30 10 10 10 0 0 0 0 0 0
  104543. - 0 0 0 0 0 0 0 0 0 0 0 0
  104544. - 0 0 0 0 0 0 0 0 0 0 0 0
  104545. - 0 0 0 0 0 0 0 0 0 0 0 0
  104546. - 0 0 0 0 0 0 0 0 0 0 0 0
  104547. - 0 0 0 0 0 0 0 0 0 0 0 0
  104548. - 0 0 0 0 0 0 0 0 0 0 0 0
  104549. - 0 0 0 0 0 0 0 0 0 0 0 0
  104550. - 0 0 0 0 0 0 0 0 0 0 0 0
  104551. - 0 0 0 0 0 0 0 0 0 0 0 0
  104552. - 0 0 0 0 0 0 0 0 0 0 0 0
  104553. - 0 0 0 0 0 0 0 0 0 0 0 0
  104554. - 0 0 0 0 0 0 6 6 6 18 18 18
  104555. - 50 50 50 101 101 101 26 26 26 10 10 10
  104556. -138 138 138 190 190 190 174 154 114 156 107 11
  104557. -197 138 11 200 144 11 197 138 11 192 133 9
  104558. -180 123 7 190 142 34 190 178 144 187 187 187
  104559. -202 202 202 221 221 221 214 214 214 66 66 66
  104560. - 2 2 6 2 2 6 50 50 50 62 62 62
  104561. - 6 6 6 2 2 6 10 10 10 90 90 90
  104562. - 50 50 50 18 18 18 6 6 6 0 0 0
  104563. - 0 0 0 0 0 0 0 0 0 0 0 0
  104564. - 0 0 0 0 0 0 0 0 0 0 0 0
  104565. - 0 0 0 0 0 0 0 0 0 0 0 0
  104566. - 0 0 0 0 0 0 0 0 0 0 0 0
  104567. - 0 0 0 0 0 0 0 0 0 0 0 0
  104568. - 0 0 0 0 0 0 0 0 0 0 0 0
  104569. - 0 0 0 0 0 0 0 0 0 0 0 0
  104570. - 0 0 0 0 0 0 0 0 0 0 0 0
  104571. - 0 0 0 0 0 0 0 0 0 0 0 0
  104572. - 0 0 0 0 0 0 0 0 0 0 0 0
  104573. - 0 0 0 0 0 0 0 0 0 0 0 0
  104574. - 0 0 0 0 0 0 10 10 10 34 34 34
  104575. - 74 74 74 74 74 74 2 2 6 6 6 6
  104576. -144 144 144 198 198 198 190 190 190 178 166 146
  104577. -154 121 60 156 107 11 156 107 11 168 124 44
  104578. -174 154 114 187 187 187 190 190 190 210 210 210
  104579. -246 246 246 253 253 253 253 253 253 182 182 182
  104580. - 6 6 6 2 2 6 2 2 6 2 2 6
  104581. - 2 2 6 2 2 6 2 2 6 62 62 62
  104582. - 74 74 74 34 34 34 14 14 14 0 0 0
  104583. - 0 0 0 0 0 0 0 0 0 0 0 0
  104584. - 0 0 0 0 0 0 0 0 0 0 0 0
  104585. - 0 0 0 0 0 0 0 0 0 0 0 0
  104586. - 0 0 0 0 0 0 0 0 0 0 0 0
  104587. - 0 0 0 0 0 0 0 0 0 0 0 0
  104588. - 0 0 0 0 0 0 0 0 0 0 0 0
  104589. - 0 0 0 0 0 0 0 0 0 0 0 0
  104590. - 0 0 0 0 0 0 0 0 0 0 0 0
  104591. - 0 0 0 0 0 0 0 0 0 0 0 0
  104592. - 0 0 0 0 0 0 0 0 0 0 0 0
  104593. - 0 0 0 0 0 0 0 0 0 0 0 0
  104594. - 0 0 0 10 10 10 22 22 22 54 54 54
  104595. - 94 94 94 18 18 18 2 2 6 46 46 46
  104596. -234 234 234 221 221 221 190 190 190 190 190 190
  104597. -190 190 190 187 187 187 187 187 187 190 190 190
  104598. -190 190 190 195 195 195 214 214 214 242 242 242
  104599. -253 253 253 253 253 253 253 253 253 253 253 253
  104600. - 82 82 82 2 2 6 2 2 6 2 2 6
  104601. - 2 2 6 2 2 6 2 2 6 14 14 14
  104602. - 86 86 86 54 54 54 22 22 22 6 6 6
  104603. - 0 0 0 0 0 0 0 0 0 0 0 0
  104604. - 0 0 0 0 0 0 0 0 0 0 0 0
  104605. - 0 0 0 0 0 0 0 0 0 0 0 0
  104606. - 0 0 0 0 0 0 0 0 0 0 0 0
  104607. - 0 0 0 0 0 0 0 0 0 0 0 0
  104608. - 0 0 0 0 0 0 0 0 0 0 0 0
  104609. - 0 0 0 0 0 0 0 0 0 0 0 0
  104610. - 0 0 0 0 0 0 0 0 0 0 0 0
  104611. - 0 0 0 0 0 0 0 0 0 0 0 0
  104612. - 0 0 0 0 0 0 0 0 0 0 0 0
  104613. - 0 0 0 0 0 0 0 0 0 0 0 0
  104614. - 6 6 6 18 18 18 46 46 46 90 90 90
  104615. - 46 46 46 18 18 18 6 6 6 182 182 182
  104616. -253 253 253 246 246 246 206 206 206 190 190 190
  104617. -190 190 190 190 190 190 190 190 190 190 190 190
  104618. -206 206 206 231 231 231 250 250 250 253 253 253
  104619. -253 253 253 253 253 253 253 253 253 253 253 253
  104620. -202 202 202 14 14 14 2 2 6 2 2 6
  104621. - 2 2 6 2 2 6 2 2 6 2 2 6
  104622. - 42 42 42 86 86 86 42 42 42 18 18 18
  104623. - 6 6 6 0 0 0 0 0 0 0 0 0
  104624. - 0 0 0 0 0 0 0 0 0 0 0 0
  104625. - 0 0 0 0 0 0 0 0 0 0 0 0
  104626. - 0 0 0 0 0 0 0 0 0 0 0 0
  104627. - 0 0 0 0 0 0 0 0 0 0 0 0
  104628. - 0 0 0 0 0 0 0 0 0 0 0 0
  104629. - 0 0 0 0 0 0 0 0 0 0 0 0
  104630. - 0 0 0 0 0 0 0 0 0 0 0 0
  104631. - 0 0 0 0 0 0 0 0 0 0 0 0
  104632. - 0 0 0 0 0 0 0 0 0 0 0 0
  104633. - 0 0 0 0 0 0 0 0 0 6 6 6
  104634. - 14 14 14 38 38 38 74 74 74 66 66 66
  104635. - 2 2 6 6 6 6 90 90 90 250 250 250
  104636. -253 253 253 253 253 253 238 238 238 198 198 198
  104637. -190 190 190 190 190 190 195 195 195 221 221 221
  104638. -246 246 246 253 253 253 253 253 253 253 253 253
  104639. -253 253 253 253 253 253 253 253 253 253 253 253
  104640. -253 253 253 82 82 82 2 2 6 2 2 6
  104641. - 2 2 6 2 2 6 2 2 6 2 2 6
  104642. - 2 2 6 78 78 78 70 70 70 34 34 34
  104643. - 14 14 14 6 6 6 0 0 0 0 0 0
  104644. - 0 0 0 0 0 0 0 0 0 0 0 0
  104645. - 0 0 0 0 0 0 0 0 0 0 0 0
  104646. - 0 0 0 0 0 0 0 0 0 0 0 0
  104647. - 0 0 0 0 0 0 0 0 0 0 0 0
  104648. - 0 0 0 0 0 0 0 0 0 0 0 0
  104649. - 0 0 0 0 0 0 0 0 0 0 0 0
  104650. - 0 0 0 0 0 0 0 0 0 0 0 0
  104651. - 0 0 0 0 0 0 0 0 0 0 0 0
  104652. - 0 0 0 0 0 0 0 0 0 0 0 0
  104653. - 0 0 0 0 0 0 0 0 0 14 14 14
  104654. - 34 34 34 66 66 66 78 78 78 6 6 6
  104655. - 2 2 6 18 18 18 218 218 218 253 253 253
  104656. -253 253 253 253 253 253 253 253 253 246 246 246
  104657. -226 226 226 231 231 231 246 246 246 253 253 253
  104658. -253 253 253 253 253 253 253 253 253 253 253 253
  104659. -253 253 253 253 253 253 253 253 253 253 253 253
  104660. -253 253 253 178 178 178 2 2 6 2 2 6
  104661. - 2 2 6 2 2 6 2 2 6 2 2 6
  104662. - 2 2 6 18 18 18 90 90 90 62 62 62
  104663. - 30 30 30 10 10 10 0 0 0 0 0 0
  104664. - 0 0 0 0 0 0 0 0 0 0 0 0
  104665. - 0 0 0 0 0 0 0 0 0 0 0 0
  104666. - 0 0 0 0 0 0 0 0 0 0 0 0
  104667. - 0 0 0 0 0 0 0 0 0 0 0 0
  104668. - 0 0 0 0 0 0 0 0 0 0 0 0
  104669. - 0 0 0 0 0 0 0 0 0 0 0 0
  104670. - 0 0 0 0 0 0 0 0 0 0 0 0
  104671. - 0 0 0 0 0 0 0 0 0 0 0 0
  104672. - 0 0 0 0 0 0 0 0 0 0 0 0
  104673. - 0 0 0 0 0 0 10 10 10 26 26 26
  104674. - 58 58 58 90 90 90 18 18 18 2 2 6
  104675. - 2 2 6 110 110 110 253 253 253 253 253 253
  104676. -253 253 253 253 253 253 253 253 253 253 253 253
  104677. -250 250 250 253 253 253 253 253 253 253 253 253
  104678. -253 253 253 253 253 253 253 253 253 253 253 253
  104679. -253 253 253 253 253 253 253 253 253 253 253 253
  104680. -253 253 253 231 231 231 18 18 18 2 2 6
  104681. - 2 2 6 2 2 6 2 2 6 2 2 6
  104682. - 2 2 6 2 2 6 18 18 18 94 94 94
  104683. - 54 54 54 26 26 26 10 10 10 0 0 0
  104684. - 0 0 0 0 0 0 0 0 0 0 0 0
  104685. - 0 0 0 0 0 0 0 0 0 0 0 0
  104686. - 0 0 0 0 0 0 0 0 0 0 0 0
  104687. - 0 0 0 0 0 0 0 0 0 0 0 0
  104688. - 0 0 0 0 0 0 0 0 0 0 0 0
  104689. - 0 0 0 0 0 0 0 0 0 0 0 0
  104690. - 0 0 0 0 0 0 0 0 0 0 0 0
  104691. - 0 0 0 0 0 0 0 0 0 0 0 0
  104692. - 0 0 0 0 0 0 0 0 0 0 0 0
  104693. - 0 0 0 6 6 6 22 22 22 50 50 50
  104694. - 90 90 90 26 26 26 2 2 6 2 2 6
  104695. - 14 14 14 195 195 195 250 250 250 253 253 253
  104696. -253 253 253 253 253 253 253 253 253 253 253 253
  104697. -253 253 253 253 253 253 253 253 253 253 253 253
  104698. -253 253 253 253 253 253 253 253 253 253 253 253
  104699. -253 253 253 253 253 253 253 253 253 253 253 253
  104700. -250 250 250 242 242 242 54 54 54 2 2 6
  104701. - 2 2 6 2 2 6 2 2 6 2 2 6
  104702. - 2 2 6 2 2 6 2 2 6 38 38 38
  104703. - 86 86 86 50 50 50 22 22 22 6 6 6
  104704. - 0 0 0 0 0 0 0 0 0 0 0 0
  104705. - 0 0 0 0 0 0 0 0 0 0 0 0
  104706. - 0 0 0 0 0 0 0 0 0 0 0 0
  104707. - 0 0 0 0 0 0 0 0 0 0 0 0
  104708. - 0 0 0 0 0 0 0 0 0 0 0 0
  104709. - 0 0 0 0 0 0 0 0 0 0 0 0
  104710. - 0 0 0 0 0 0 0 0 0 0 0 0
  104711. - 0 0 0 0 0 0 0 0 0 0 0 0
  104712. - 0 0 0 0 0 0 0 0 0 0 0 0
  104713. - 6 6 6 14 14 14 38 38 38 82 82 82
  104714. - 34 34 34 2 2 6 2 2 6 2 2 6
  104715. - 42 42 42 195 195 195 246 246 246 253 253 253
  104716. -253 253 253 253 253 253 253 253 253 250 250 250
  104717. -242 242 242 242 242 242 250 250 250 253 253 253
  104718. -253 253 253 253 253 253 253 253 253 253 253 253
  104719. -253 253 253 250 250 250 246 246 246 238 238 238
  104720. -226 226 226 231 231 231 101 101 101 6 6 6
  104721. - 2 2 6 2 2 6 2 2 6 2 2 6
  104722. - 2 2 6 2 2 6 2 2 6 2 2 6
  104723. - 38 38 38 82 82 82 42 42 42 14 14 14
  104724. - 6 6 6 0 0 0 0 0 0 0 0 0
  104725. - 0 0 0 0 0 0 0 0 0 0 0 0
  104726. - 0 0 0 0 0 0 0 0 0 0 0 0
  104727. - 0 0 0 0 0 0 0 0 0 0 0 0
  104728. - 0 0 0 0 0 0 0 0 0 0 0 0
  104729. - 0 0 0 0 0 0 0 0 0 0 0 0
  104730. - 0 0 0 0 0 0 0 0 0 0 0 0
  104731. - 0 0 0 0 0 0 0 0 0 0 0 0
  104732. - 0 0 0 0 0 0 0 0 0 0 0 0
  104733. - 10 10 10 26 26 26 62 62 62 66 66 66
  104734. - 2 2 6 2 2 6 2 2 6 6 6 6
  104735. - 70 70 70 170 170 170 206 206 206 234 234 234
  104736. -246 246 246 250 250 250 250 250 250 238 238 238
  104737. -226 226 226 231 231 231 238 238 238 250 250 250
  104738. -250 250 250 250 250 250 246 246 246 231 231 231
  104739. -214 214 214 206 206 206 202 202 202 202 202 202
  104740. -198 198 198 202 202 202 182 182 182 18 18 18
  104741. - 2 2 6 2 2 6 2 2 6 2 2 6
  104742. - 2 2 6 2 2 6 2 2 6 2 2 6
  104743. - 2 2 6 62 62 62 66 66 66 30 30 30
  104744. - 10 10 10 0 0 0 0 0 0 0 0 0
  104745. - 0 0 0 0 0 0 0 0 0 0 0 0
  104746. - 0 0 0 0 0 0 0 0 0 0 0 0
  104747. - 0 0 0 0 0 0 0 0 0 0 0 0
  104748. - 0 0 0 0 0 0 0 0 0 0 0 0
  104749. - 0 0 0 0 0 0 0 0 0 0 0 0
  104750. - 0 0 0 0 0 0 0 0 0 0 0 0
  104751. - 0 0 0 0 0 0 0 0 0 0 0 0
  104752. - 0 0 0 0 0 0 0 0 0 0 0 0
  104753. - 14 14 14 42 42 42 82 82 82 18 18 18
  104754. - 2 2 6 2 2 6 2 2 6 10 10 10
  104755. - 94 94 94 182 182 182 218 218 218 242 242 242
  104756. -250 250 250 253 253 253 253 253 253 250 250 250
  104757. -234 234 234 253 253 253 253 253 253 253 253 253
  104758. -253 253 253 253 253 253 253 253 253 246 246 246
  104759. -238 238 238 226 226 226 210 210 210 202 202 202
  104760. -195 195 195 195 195 195 210 210 210 158 158 158
  104761. - 6 6 6 14 14 14 50 50 50 14 14 14
  104762. - 2 2 6 2 2 6 2 2 6 2 2 6
  104763. - 2 2 6 6 6 6 86 86 86 46 46 46
  104764. - 18 18 18 6 6 6 0 0 0 0 0 0
  104765. - 0 0 0 0 0 0 0 0 0 0 0 0
  104766. - 0 0 0 0 0 0 0 0 0 0 0 0
  104767. - 0 0 0 0 0 0 0 0 0 0 0 0
  104768. - 0 0 0 0 0 0 0 0 0 0 0 0
  104769. - 0 0 0 0 0 0 0 0 0 0 0 0
  104770. - 0 0 0 0 0 0 0 0 0 0 0 0
  104771. - 0 0 0 0 0 0 0 0 0 0 0 0
  104772. - 0 0 0 0 0 0 0 0 0 6 6 6
  104773. - 22 22 22 54 54 54 70 70 70 2 2 6
  104774. - 2 2 6 10 10 10 2 2 6 22 22 22
  104775. -166 166 166 231 231 231 250 250 250 253 253 253
  104776. -253 253 253 253 253 253 253 253 253 250 250 250
  104777. -242 242 242 253 253 253 253 253 253 253 253 253
  104778. -253 253 253 253 253 253 253 253 253 253 253 253
  104779. -253 253 253 253 253 253 253 253 253 246 246 246
  104780. -231 231 231 206 206 206 198 198 198 226 226 226
  104781. - 94 94 94 2 2 6 6 6 6 38 38 38
  104782. - 30 30 30 2 2 6 2 2 6 2 2 6
  104783. - 2 2 6 2 2 6 62 62 62 66 66 66
  104784. - 26 26 26 10 10 10 0 0 0 0 0 0
  104785. - 0 0 0 0 0 0 0 0 0 0 0 0
  104786. - 0 0 0 0 0 0 0 0 0 0 0 0
  104787. - 0 0 0 0 0 0 0 0 0 0 0 0
  104788. - 0 0 0 0 0 0 0 0 0 0 0 0
  104789. - 0 0 0 0 0 0 0 0 0 0 0 0
  104790. - 0 0 0 0 0 0 0 0 0 0 0 0
  104791. - 0 0 0 0 0 0 0 0 0 0 0 0
  104792. - 0 0 0 0 0 0 0 0 0 10 10 10
  104793. - 30 30 30 74 74 74 50 50 50 2 2 6
  104794. - 26 26 26 26 26 26 2 2 6 106 106 106
  104795. -238 238 238 253 253 253 253 253 253 253 253 253
  104796. -253 253 253 253 253 253 253 253 253 253 253 253
  104797. -253 253 253 253 253 253 253 253 253 253 253 253
  104798. -253 253 253 253 253 253 253 253 253 253 253 253
  104799. -253 253 253 253 253 253 253 253 253 253 253 253
  104800. -253 253 253 246 246 246 218 218 218 202 202 202
  104801. -210 210 210 14 14 14 2 2 6 2 2 6
  104802. - 30 30 30 22 22 22 2 2 6 2 2 6
  104803. - 2 2 6 2 2 6 18 18 18 86 86 86
  104804. - 42 42 42 14 14 14 0 0 0 0 0 0
  104805. - 0 0 0 0 0 0 0 0 0 0 0 0
  104806. - 0 0 0 0 0 0 0 0 0 0 0 0
  104807. - 0 0 0 0 0 0 0 0 0 0 0 0
  104808. - 0 0 0 0 0 0 0 0 0 0 0 0
  104809. - 0 0 0 0 0 0 0 0 0 0 0 0
  104810. - 0 0 0 0 0 0 0 0 0 0 0 0
  104811. - 0 0 0 0 0 0 0 0 0 0 0 0
  104812. - 0 0 0 0 0 0 0 0 0 14 14 14
  104813. - 42 42 42 90 90 90 22 22 22 2 2 6
  104814. - 42 42 42 2 2 6 18 18 18 218 218 218
  104815. -253 253 253 253 253 253 253 253 253 253 253 253
  104816. -253 253 253 253 253 253 253 253 253 253 253 253
  104817. -253 253 253 253 253 253 253 253 253 253 253 253
  104818. -253 253 253 253 253 253 253 253 253 253 253 253
  104819. -253 253 253 253 253 253 253 253 253 253 253 253
  104820. -253 253 253 253 253 253 250 250 250 221 221 221
  104821. -218 218 218 101 101 101 2 2 6 14 14 14
  104822. - 18 18 18 38 38 38 10 10 10 2 2 6
  104823. - 2 2 6 2 2 6 2 2 6 78 78 78
  104824. - 58 58 58 22 22 22 6 6 6 0 0 0
  104825. - 0 0 0 0 0 0 0 0 0 0 0 0
  104826. - 0 0 0 0 0 0 0 0 0 0 0 0
  104827. - 0 0 0 0 0 0 0 0 0 0 0 0
  104828. - 0 0 0 0 0 0 0 0 0 0 0 0
  104829. - 0 0 0 0 0 0 0 0 0 0 0 0
  104830. - 0 0 0 0 0 0 0 0 0 0 0 0
  104831. - 0 0 0 0 0 0 0 0 0 0 0 0
  104832. - 0 0 0 0 0 0 6 6 6 18 18 18
  104833. - 54 54 54 82 82 82 2 2 6 26 26 26
  104834. - 22 22 22 2 2 6 123 123 123 253 253 253
  104835. -253 253 253 253 253 253 253 253 253 253 253 253
  104836. -253 253 253 253 253 253 253 253 253 253 253 253
  104837. -253 253 253 253 253 253 253 253 253 253 253 253
  104838. -253 253 253 253 253 253 253 253 253 253 253 253
  104839. -253 253 253 253 253 253 253 253 253 253 253 253
  104840. -253 253 253 253 253 253 253 253 253 250 250 250
  104841. -238 238 238 198 198 198 6 6 6 38 38 38
  104842. - 58 58 58 26 26 26 38 38 38 2 2 6
  104843. - 2 2 6 2 2 6 2 2 6 46 46 46
  104844. - 78 78 78 30 30 30 10 10 10 0 0 0
  104845. - 0 0 0 0 0 0 0 0 0 0 0 0
  104846. - 0 0 0 0 0 0 0 0 0 0 0 0
  104847. - 0 0 0 0 0 0 0 0 0 0 0 0
  104848. - 0 0 0 0 0 0 0 0 0 0 0 0
  104849. - 0 0 0 0 0 0 0 0 0 0 0 0
  104850. - 0 0 0 0 0 0 0 0 0 0 0 0
  104851. - 0 0 0 0 0 0 0 0 0 0 0 0
  104852. - 0 0 0 0 0 0 10 10 10 30 30 30
  104853. - 74 74 74 58 58 58 2 2 6 42 42 42
  104854. - 2 2 6 22 22 22 231 231 231 253 253 253
  104855. -253 253 253 253 253 253 253 253 253 253 253 253
  104856. -253 253 253 253 253 253 253 253 253 250 250 250
  104857. -253 253 253 253 253 253 253 253 253 253 253 253
  104858. -253 253 253 253 253 253 253 253 253 253 253 253
  104859. -253 253 253 253 253 253 253 253 253 253 253 253
  104860. -253 253 253 253 253 253 253 253 253 253 253 253
  104861. -253 253 253 246 246 246 46 46 46 38 38 38
  104862. - 42 42 42 14 14 14 38 38 38 14 14 14
  104863. - 2 2 6 2 2 6 2 2 6 6 6 6
  104864. - 86 86 86 46 46 46 14 14 14 0 0 0
  104865. - 0 0 0 0 0 0 0 0 0 0 0 0
  104866. - 0 0 0 0 0 0 0 0 0 0 0 0
  104867. - 0 0 0 0 0 0 0 0 0 0 0 0
  104868. - 0 0 0 0 0 0 0 0 0 0 0 0
  104869. - 0 0 0 0 0 0 0 0 0 0 0 0
  104870. - 0 0 0 0 0 0 0 0 0 0 0 0
  104871. - 0 0 0 0 0 0 0 0 0 0 0 0
  104872. - 0 0 0 6 6 6 14 14 14 42 42 42
  104873. - 90 90 90 18 18 18 18 18 18 26 26 26
  104874. - 2 2 6 116 116 116 253 253 253 253 253 253
  104875. -253 253 253 253 253 253 253 253 253 253 253 253
  104876. -253 253 253 253 253 253 250 250 250 238 238 238
  104877. -253 253 253 253 253 253 253 253 253 253 253 253
  104878. -253 253 253 253 253 253 253 253 253 253 253 253
  104879. -253 253 253 253 253 253 253 253 253 253 253 253
  104880. -253 253 253 253 253 253 253 253 253 253 253 253
  104881. -253 253 253 253 253 253 94 94 94 6 6 6
  104882. - 2 2 6 2 2 6 10 10 10 34 34 34
  104883. - 2 2 6 2 2 6 2 2 6 2 2 6
  104884. - 74 74 74 58 58 58 22 22 22 6 6 6
  104885. - 0 0 0 0 0 0 0 0 0 0 0 0
  104886. - 0 0 0 0 0 0 0 0 0 0 0 0
  104887. - 0 0 0 0 0 0 0 0 0 0 0 0
  104888. - 0 0 0 0 0 0 0 0 0 0 0 0
  104889. - 0 0 0 0 0 0 0 0 0 0 0 0
  104890. - 0 0 0 0 0 0 0 0 0 0 0 0
  104891. - 0 0 0 0 0 0 0 0 0 0 0 0
  104892. - 0 0 0 10 10 10 26 26 26 66 66 66
  104893. - 82 82 82 2 2 6 38 38 38 6 6 6
  104894. - 14 14 14 210 210 210 253 253 253 253 253 253
  104895. -253 253 253 253 253 253 253 253 253 253 253 253
  104896. -253 253 253 253 253 253 246 246 246 242 242 242
  104897. -253 253 253 253 253 253 253 253 253 253 253 253
  104898. -253 253 253 253 253 253 253 253 253 253 253 253
  104899. -253 253 253 253 253 253 253 253 253 253 253 253
  104900. -253 253 253 253 253 253 253 253 253 253 253 253
  104901. -253 253 253 253 253 253 144 144 144 2 2 6
  104902. - 2 2 6 2 2 6 2 2 6 46 46 46
  104903. - 2 2 6 2 2 6 2 2 6 2 2 6
  104904. - 42 42 42 74 74 74 30 30 30 10 10 10
  104905. - 0 0 0 0 0 0 0 0 0 0 0 0
  104906. - 0 0 0 0 0 0 0 0 0 0 0 0
  104907. - 0 0 0 0 0 0 0 0 0 0 0 0
  104908. - 0 0 0 0 0 0 0 0 0 0 0 0
  104909. - 0 0 0 0 0 0 0 0 0 0 0 0
  104910. - 0 0 0 0 0 0 0 0 0 0 0 0
  104911. - 0 0 0 0 0 0 0 0 0 0 0 0
  104912. - 6 6 6 14 14 14 42 42 42 90 90 90
  104913. - 26 26 26 6 6 6 42 42 42 2 2 6
  104914. - 74 74 74 250 250 250 253 253 253 253 253 253
  104915. -253 253 253 253 253 253 253 253 253 253 253 253
  104916. -253 253 253 253 253 253 242 242 242 242 242 242
  104917. -253 253 253 253 253 253 253 253 253 253 253 253
  104918. -253 253 253 253 253 253 253 253 253 253 253 253
  104919. -253 253 253 253 253 253 253 253 253 253 253 253
  104920. -253 253 253 253 253 253 253 253 253 253 253 253
  104921. -253 253 253 253 253 253 182 182 182 2 2 6
  104922. - 2 2 6 2 2 6 2 2 6 46 46 46
  104923. - 2 2 6 2 2 6 2 2 6 2 2 6
  104924. - 10 10 10 86 86 86 38 38 38 10 10 10
  104925. - 0 0 0 0 0 0 0 0 0 0 0 0
  104926. - 0 0 0 0 0 0 0 0 0 0 0 0
  104927. - 0 0 0 0 0 0 0 0 0 0 0 0
  104928. - 0 0 0 0 0 0 0 0 0 0 0 0
  104929. - 0 0 0 0 0 0 0 0 0 0 0 0
  104930. - 0 0 0 0 0 0 0 0 0 0 0 0
  104931. - 0 0 0 0 0 0 0 0 0 0 0 0
  104932. - 10 10 10 26 26 26 66 66 66 82 82 82
  104933. - 2 2 6 22 22 22 18 18 18 2 2 6
  104934. -149 149 149 253 253 253 253 253 253 253 253 253
  104935. -253 253 253 253 253 253 253 253 253 253 253 253
  104936. -253 253 253 253 253 253 234 234 234 242 242 242
  104937. -253 253 253 253 253 253 253 253 253 253 253 253
  104938. -253 253 253 253 253 253 253 253 253 253 253 253
  104939. -253 253 253 253 253 253 253 253 253 253 253 253
  104940. -253 253 253 253 253 253 253 253 253 253 253 253
  104941. -253 253 253 253 253 253 206 206 206 2 2 6
  104942. - 2 2 6 2 2 6 2 2 6 38 38 38
  104943. - 2 2 6 2 2 6 2 2 6 2 2 6
  104944. - 6 6 6 86 86 86 46 46 46 14 14 14
  104945. - 0 0 0 0 0 0 0 0 0 0 0 0
  104946. - 0 0 0 0 0 0 0 0 0 0 0 0
  104947. - 0 0 0 0 0 0 0 0 0 0 0 0
  104948. - 0 0 0 0 0 0 0 0 0 0 0 0
  104949. - 0 0 0 0 0 0 0 0 0 0 0 0
  104950. - 0 0 0 0 0 0 0 0 0 0 0 0
  104951. - 0 0 0 0 0 0 0 0 0 6 6 6
  104952. - 18 18 18 46 46 46 86 86 86 18 18 18
  104953. - 2 2 6 34 34 34 10 10 10 6 6 6
  104954. -210 210 210 253 253 253 253 253 253 253 253 253
  104955. -253 253 253 253 253 253 253 253 253 253 253 253
  104956. -253 253 253 253 253 253 234 234 234 242 242 242
  104957. -253 253 253 253 253 253 253 253 253 253 253 253
  104958. -253 253 253 253 253 253 253 253 253 253 253 253
  104959. -253 253 253 253 253 253 253 253 253 253 253 253
  104960. -253 253 253 253 253 253 253 253 253 253 253 253
  104961. -253 253 253 253 253 253 221 221 221 6 6 6
  104962. - 2 2 6 2 2 6 6 6 6 30 30 30
  104963. - 2 2 6 2 2 6 2 2 6 2 2 6
  104964. - 2 2 6 82 82 82 54 54 54 18 18 18
  104965. - 6 6 6 0 0 0 0 0 0 0 0 0
  104966. - 0 0 0 0 0 0 0 0 0 0 0 0
  104967. - 0 0 0 0 0 0 0 0 0 0 0 0
  104968. - 0 0 0 0 0 0 0 0 0 0 0 0
  104969. - 0 0 0 0 0 0 0 0 0 0 0 0
  104970. - 0 0 0 0 0 0 0 0 0 0 0 0
  104971. - 0 0 0 0 0 0 0 0 0 10 10 10
  104972. - 26 26 26 66 66 66 62 62 62 2 2 6
  104973. - 2 2 6 38 38 38 10 10 10 26 26 26
  104974. -238 238 238 253 253 253 253 253 253 253 253 253
  104975. -253 253 253 253 253 253 253 253 253 253 253 253
  104976. -253 253 253 253 253 253 231 231 231 238 238 238
  104977. -253 253 253 253 253 253 253 253 253 253 253 253
  104978. -253 253 253 253 253 253 253 253 253 253 253 253
  104979. -253 253 253 253 253 253 253 253 253 253 253 253
  104980. -253 253 253 253 253 253 253 253 253 253 253 253
  104981. -253 253 253 253 253 253 231 231 231 6 6 6
  104982. - 2 2 6 2 2 6 10 10 10 30 30 30
  104983. - 2 2 6 2 2 6 2 2 6 2 2 6
  104984. - 2 2 6 66 66 66 58 58 58 22 22 22
  104985. - 6 6 6 0 0 0 0 0 0 0 0 0
  104986. - 0 0 0 0 0 0 0 0 0 0 0 0
  104987. - 0 0 0 0 0 0 0 0 0 0 0 0
  104988. - 0 0 0 0 0 0 0 0 0 0 0 0
  104989. - 0 0 0 0 0 0 0 0 0 0 0 0
  104990. - 0 0 0 0 0 0 0 0 0 0 0 0
  104991. - 0 0 0 0 0 0 0 0 0 10 10 10
  104992. - 38 38 38 78 78 78 6 6 6 2 2 6
  104993. - 2 2 6 46 46 46 14 14 14 42 42 42
  104994. -246 246 246 253 253 253 253 253 253 253 253 253
  104995. -253 253 253 253 253 253 253 253 253 253 253 253
  104996. -253 253 253 253 253 253 231 231 231 242 242 242
  104997. -253 253 253 253 253 253 253 253 253 253 253 253
  104998. -253 253 253 253 253 253 253 253 253 253 253 253
  104999. -253 253 253 253 253 253 253 253 253 253 253 253
  105000. -253 253 253 253 253 253 253 253 253 253 253 253
  105001. -253 253 253 253 253 253 234 234 234 10 10 10
  105002. - 2 2 6 2 2 6 22 22 22 14 14 14
  105003. - 2 2 6 2 2 6 2 2 6 2 2 6
  105004. - 2 2 6 66 66 66 62 62 62 22 22 22
  105005. - 6 6 6 0 0 0 0 0 0 0 0 0
  105006. - 0 0 0 0 0 0 0 0 0 0 0 0
  105007. - 0 0 0 0 0 0 0 0 0 0 0 0
  105008. - 0 0 0 0 0 0 0 0 0 0 0 0
  105009. - 0 0 0 0 0 0 0 0 0 0 0 0
  105010. - 0 0 0 0 0 0 0 0 0 0 0 0
  105011. - 0 0 0 0 0 0 6 6 6 18 18 18
  105012. - 50 50 50 74 74 74 2 2 6 2 2 6
  105013. - 14 14 14 70 70 70 34 34 34 62 62 62
  105014. -250 250 250 253 253 253 253 253 253 253 253 253
  105015. -253 253 253 253 253 253 253 253 253 253 253 253
  105016. -253 253 253 253 253 253 231 231 231 246 246 246
  105017. -253 253 253 253 253 253 253 253 253 253 253 253
  105018. -253 253 253 253 253 253 253 253 253 253 253 253
  105019. -253 253 253 253 253 253 253 253 253 253 253 253
  105020. -253 253 253 253 253 253 253 253 253 253 253 253
  105021. -253 253 253 253 253 253 234 234 234 14 14 14
  105022. - 2 2 6 2 2 6 30 30 30 2 2 6
  105023. - 2 2 6 2 2 6 2 2 6 2 2 6
  105024. - 2 2 6 66 66 66 62 62 62 22 22 22
  105025. - 6 6 6 0 0 0 0 0 0 0 0 0
  105026. - 0 0 0 0 0 0 0 0 0 0 0 0
  105027. - 0 0 0 0 0 0 0 0 0 0 0 0
  105028. - 0 0 0 0 0 0 0 0 0 0 0 0
  105029. - 0 0 0 0 0 0 0 0 0 0 0 0
  105030. - 0 0 0 0 0 0 0 0 0 0 0 0
  105031. - 0 0 0 0 0 0 6 6 6 18 18 18
  105032. - 54 54 54 62 62 62 2 2 6 2 2 6
  105033. - 2 2 6 30 30 30 46 46 46 70 70 70
  105034. -250 250 250 253 253 253 253 253 253 253 253 253
  105035. -253 253 253 253 253 253 253 253 253 253 253 253
  105036. -253 253 253 253 253 253 231 231 231 246 246 246
  105037. -253 253 253 253 253 253 253 253 253 253 253 253
  105038. -253 253 253 253 253 253 253 253 253 253 253 253
  105039. -253 253 253 253 253 253 253 253 253 253 253 253
  105040. -253 253 253 253 253 253 253 253 253 253 253 253
  105041. -253 253 253 253 253 253 226 226 226 10 10 10
  105042. - 2 2 6 6 6 6 30 30 30 2 2 6
  105043. - 2 2 6 2 2 6 2 2 6 2 2 6
  105044. - 2 2 6 66 66 66 58 58 58 22 22 22
  105045. - 6 6 6 0 0 0 0 0 0 0 0 0
  105046. - 0 0 0 0 0 0 0 0 0 0 0 0
  105047. - 0 0 0 0 0 0 0 0 0 0 0 0
  105048. - 0 0 0 0 0 0 0 0 0 0 0 0
  105049. - 0 0 0 0 0 0 0 0 0 0 0 0
  105050. - 0 0 0 0 0 0 0 0 0 0 0 0
  105051. - 0 0 0 0 0 0 6 6 6 22 22 22
  105052. - 58 58 58 62 62 62 2 2 6 2 2 6
  105053. - 2 2 6 2 2 6 30 30 30 78 78 78
  105054. -250 250 250 253 253 253 253 253 253 253 253 253
  105055. -253 253 253 253 253 253 253 253 253 253 253 253
  105056. -253 253 253 253 253 253 231 231 231 246 246 246
  105057. -253 253 253 253 253 253 253 253 253 253 253 253
  105058. -253 253 253 253 253 253 253 253 253 253 253 253
  105059. -253 253 253 253 253 253 253 253 253 253 253 253
  105060. -253 253 253 253 253 253 253 253 253 253 253 253
  105061. -253 253 253 253 253 253 206 206 206 2 2 6
  105062. - 22 22 22 34 34 34 18 14 6 22 22 22
  105063. - 26 26 26 18 18 18 6 6 6 2 2 6
  105064. - 2 2 6 82 82 82 54 54 54 18 18 18
  105065. - 6 6 6 0 0 0 0 0 0 0 0 0
  105066. - 0 0 0 0 0 0 0 0 0 0 0 0
  105067. - 0 0 0 0 0 0 0 0 0 0 0 0
  105068. - 0 0 0 0 0 0 0 0 0 0 0 0
  105069. - 0 0 0 0 0 0 0 0 0 0 0 0
  105070. - 0 0 0 0 0 0 0 0 0 0 0 0
  105071. - 0 0 0 0 0 0 6 6 6 26 26 26
  105072. - 62 62 62 106 106 106 74 54 14 185 133 11
  105073. -210 162 10 121 92 8 6 6 6 62 62 62
  105074. -238 238 238 253 253 253 253 253 253 253 253 253
  105075. -253 253 253 253 253 253 253 253 253 253 253 253
  105076. -253 253 253 253 253 253 231 231 231 246 246 246
  105077. -253 253 253 253 253 253 253 253 253 253 253 253
  105078. -253 253 253 253 253 253 253 253 253 253 253 253
  105079. -253 253 253 253 253 253 253 253 253 253 253 253
  105080. -253 253 253 253 253 253 253 253 253 253 253 253
  105081. -253 253 253 253 253 253 158 158 158 18 18 18
  105082. - 14 14 14 2 2 6 2 2 6 2 2 6
  105083. - 6 6 6 18 18 18 66 66 66 38 38 38
  105084. - 6 6 6 94 94 94 50 50 50 18 18 18
  105085. - 6 6 6 0 0 0 0 0 0 0 0 0
  105086. - 0 0 0 0 0 0 0 0 0 0 0 0
  105087. - 0 0 0 0 0 0 0 0 0 0 0 0
  105088. - 0 0 0 0 0 0 0 0 0 0 0 0
  105089. - 0 0 0 0 0 0 0 0 0 0 0 0
  105090. - 0 0 0 0 0 0 0 0 0 6 6 6
  105091. - 10 10 10 10 10 10 18 18 18 38 38 38
  105092. - 78 78 78 142 134 106 216 158 10 242 186 14
  105093. -246 190 14 246 190 14 156 118 10 10 10 10
  105094. - 90 90 90 238 238 238 253 253 253 253 253 253
  105095. -253 253 253 253 253 253 253 253 253 253 253 253
  105096. -253 253 253 253 253 253 231 231 231 250 250 250
  105097. -253 253 253 253 253 253 253 253 253 253 253 253
  105098. -253 253 253 253 253 253 253 253 253 253 253 253
  105099. -253 253 253 253 253 253 253 253 253 253 253 253
  105100. -253 253 253 253 253 253 253 253 253 246 230 190
  105101. -238 204 91 238 204 91 181 142 44 37 26 9
  105102. - 2 2 6 2 2 6 2 2 6 2 2 6
  105103. - 2 2 6 2 2 6 38 38 38 46 46 46
  105104. - 26 26 26 106 106 106 54 54 54 18 18 18
  105105. - 6 6 6 0 0 0 0 0 0 0 0 0
  105106. - 0 0 0 0 0 0 0 0 0 0 0 0
  105107. - 0 0 0 0 0 0 0 0 0 0 0 0
  105108. - 0 0 0 0 0 0 0 0 0 0 0 0
  105109. - 0 0 0 0 0 0 0 0 0 0 0 0
  105110. - 0 0 0 6 6 6 14 14 14 22 22 22
  105111. - 30 30 30 38 38 38 50 50 50 70 70 70
  105112. -106 106 106 190 142 34 226 170 11 242 186 14
  105113. -246 190 14 246 190 14 246 190 14 154 114 10
  105114. - 6 6 6 74 74 74 226 226 226 253 253 253
  105115. -253 253 253 253 253 253 253 253 253 253 253 253
  105116. -253 253 253 253 253 253 231 231 231 250 250 250
  105117. -253 253 253 253 253 253 253 253 253 253 253 253
  105118. -253 253 253 253 253 253 253 253 253 253 253 253
  105119. -253 253 253 253 253 253 253 253 253 253 253 253
  105120. -253 253 253 253 253 253 253 253 253 228 184 62
  105121. -241 196 14 241 208 19 232 195 16 38 30 10
  105122. - 2 2 6 2 2 6 2 2 6 2 2 6
  105123. - 2 2 6 6 6 6 30 30 30 26 26 26
  105124. -203 166 17 154 142 90 66 66 66 26 26 26
  105125. - 6 6 6 0 0 0 0 0 0 0 0 0
  105126. - 0 0 0 0 0 0 0 0 0 0 0 0
  105127. - 0 0 0 0 0 0 0 0 0 0 0 0
  105128. - 0 0 0 0 0 0 0 0 0 0 0 0
  105129. - 0 0 0 0 0 0 0 0 0 0 0 0
  105130. - 6 6 6 18 18 18 38 38 38 58 58 58
  105131. - 78 78 78 86 86 86 101 101 101 123 123 123
  105132. -175 146 61 210 150 10 234 174 13 246 186 14
  105133. -246 190 14 246 190 14 246 190 14 238 190 10
  105134. -102 78 10 2 2 6 46 46 46 198 198 198
  105135. -253 253 253 253 253 253 253 253 253 253 253 253
  105136. -253 253 253 253 253 253 234 234 234 242 242 242
  105137. -253 253 253 253 253 253 253 253 253 253 253 253
  105138. -253 253 253 253 253 253 253 253 253 253 253 253
  105139. -253 253 253 253 253 253 253 253 253 253 253 253
  105140. -253 253 253 253 253 253 253 253 253 224 178 62
  105141. -242 186 14 241 196 14 210 166 10 22 18 6
  105142. - 2 2 6 2 2 6 2 2 6 2 2 6
  105143. - 2 2 6 2 2 6 6 6 6 121 92 8
  105144. -238 202 15 232 195 16 82 82 82 34 34 34
  105145. - 10 10 10 0 0 0 0 0 0 0 0 0
  105146. - 0 0 0 0 0 0 0 0 0 0 0 0
  105147. - 0 0 0 0 0 0 0 0 0 0 0 0
  105148. - 0 0 0 0 0 0 0 0 0 0 0 0
  105149. - 0 0 0 0 0 0 0 0 0 0 0 0
  105150. - 14 14 14 38 38 38 70 70 70 154 122 46
  105151. -190 142 34 200 144 11 197 138 11 197 138 11
  105152. -213 154 11 226 170 11 242 186 14 246 190 14
  105153. -246 190 14 246 190 14 246 190 14 246 190 14
  105154. -225 175 15 46 32 6 2 2 6 22 22 22
  105155. -158 158 158 250 250 250 253 253 253 253 253 253
  105156. -253 253 253 253 253 253 253 253 253 253 253 253
  105157. -253 253 253 253 253 253 253 253 253 253 253 253
  105158. -253 253 253 253 253 253 253 253 253 253 253 253
  105159. -253 253 253 253 253 253 253 253 253 253 253 253
  105160. -253 253 253 250 250 250 242 242 242 224 178 62
  105161. -239 182 13 236 186 11 213 154 11 46 32 6
  105162. - 2 2 6 2 2 6 2 2 6 2 2 6
  105163. - 2 2 6 2 2 6 61 42 6 225 175 15
  105164. -238 190 10 236 186 11 112 100 78 42 42 42
  105165. - 14 14 14 0 0 0 0 0 0 0 0 0
  105166. - 0 0 0 0 0 0 0 0 0 0 0 0
  105167. - 0 0 0 0 0 0 0 0 0 0 0 0
  105168. - 0 0 0 0 0 0 0 0 0 0 0 0
  105169. - 0 0 0 0 0 0 0 0 0 6 6 6
  105170. - 22 22 22 54 54 54 154 122 46 213 154 11
  105171. -226 170 11 230 174 11 226 170 11 226 170 11
  105172. -236 178 12 242 186 14 246 190 14 246 190 14
  105173. -246 190 14 246 190 14 246 190 14 246 190 14
  105174. -241 196 14 184 144 12 10 10 10 2 2 6
  105175. - 6 6 6 116 116 116 242 242 242 253 253 253
  105176. -253 253 253 253 253 253 253 253 253 253 253 253
  105177. -253 253 253 253 253 253 253 253 253 253 253 253
  105178. -253 253 253 253 253 253 253 253 253 253 253 253
  105179. -253 253 253 253 253 253 253 253 253 253 253 253
  105180. -253 253 253 231 231 231 198 198 198 214 170 54
  105181. -236 178 12 236 178 12 210 150 10 137 92 6
  105182. - 18 14 6 2 2 6 2 2 6 2 2 6
  105183. - 6 6 6 70 47 6 200 144 11 236 178 12
  105184. -239 182 13 239 182 13 124 112 88 58 58 58
  105185. - 22 22 22 6 6 6 0 0 0 0 0 0
  105186. - 0 0 0 0 0 0 0 0 0 0 0 0
  105187. - 0 0 0 0 0 0 0 0 0 0 0 0
  105188. - 0 0 0 0 0 0 0 0 0 0 0 0
  105189. - 0 0 0 0 0 0 0 0 0 10 10 10
  105190. - 30 30 30 70 70 70 180 133 36 226 170 11
  105191. -239 182 13 242 186 14 242 186 14 246 186 14
  105192. -246 190 14 246 190 14 246 190 14 246 190 14
  105193. -246 190 14 246 190 14 246 190 14 246 190 14
  105194. -246 190 14 232 195 16 98 70 6 2 2 6
  105195. - 2 2 6 2 2 6 66 66 66 221 221 221
  105196. -253 253 253 253 253 253 253 253 253 253 253 253
  105197. -253 253 253 253 253 253 253 253 253 253 253 253
  105198. -253 253 253 253 253 253 253 253 253 253 253 253
  105199. -253 253 253 253 253 253 253 253 253 253 253 253
  105200. -253 253 253 206 206 206 198 198 198 214 166 58
  105201. -230 174 11 230 174 11 216 158 10 192 133 9
  105202. -163 110 8 116 81 8 102 78 10 116 81 8
  105203. -167 114 7 197 138 11 226 170 11 239 182 13
  105204. -242 186 14 242 186 14 162 146 94 78 78 78
  105205. - 34 34 34 14 14 14 6 6 6 0 0 0
  105206. - 0 0 0 0 0 0 0 0 0 0 0 0
  105207. - 0 0 0 0 0 0 0 0 0 0 0 0
  105208. - 0 0 0 0 0 0 0 0 0 0 0 0
  105209. - 0 0 0 0 0 0 0 0 0 6 6 6
  105210. - 30 30 30 78 78 78 190 142 34 226 170 11
  105211. -239 182 13 246 190 14 246 190 14 246 190 14
  105212. -246 190 14 246 190 14 246 190 14 246 190 14
  105213. -246 190 14 246 190 14 246 190 14 246 190 14
  105214. -246 190 14 241 196 14 203 166 17 22 18 6
  105215. - 2 2 6 2 2 6 2 2 6 38 38 38
  105216. -218 218 218 253 253 253 253 253 253 253 253 253
  105217. -253 253 253 253 253 253 253 253 253 253 253 253
  105218. -253 253 253 253 253 253 253 253 253 253 253 253
  105219. -253 253 253 253 253 253 253 253 253 253 253 253
  105220. -250 250 250 206 206 206 198 198 198 202 162 69
  105221. -226 170 11 236 178 12 224 166 10 210 150 10
  105222. -200 144 11 197 138 11 192 133 9 197 138 11
  105223. -210 150 10 226 170 11 242 186 14 246 190 14
  105224. -246 190 14 246 186 14 225 175 15 124 112 88
  105225. - 62 62 62 30 30 30 14 14 14 6 6 6
  105226. - 0 0 0 0 0 0 0 0 0 0 0 0
  105227. - 0 0 0 0 0 0 0 0 0 0 0 0
  105228. - 0 0 0 0 0 0 0 0 0 0 0 0
  105229. - 0 0 0 0 0 0 0 0 0 10 10 10
  105230. - 30 30 30 78 78 78 174 135 50 224 166 10
  105231. -239 182 13 246 190 14 246 190 14 246 190 14
  105232. -246 190 14 246 190 14 246 190 14 246 190 14
  105233. -246 190 14 246 190 14 246 190 14 246 190 14
  105234. -246 190 14 246 190 14 241 196 14 139 102 15
  105235. - 2 2 6 2 2 6 2 2 6 2 2 6
  105236. - 78 78 78 250 250 250 253 253 253 253 253 253
  105237. -253 253 253 253 253 253 253 253 253 253 253 253
  105238. -253 253 253 253 253 253 253 253 253 253 253 253
  105239. -253 253 253 253 253 253 253 253 253 253 253 253
  105240. -250 250 250 214 214 214 198 198 198 190 150 46
  105241. -219 162 10 236 178 12 234 174 13 224 166 10
  105242. -216 158 10 213 154 11 213 154 11 216 158 10
  105243. -226 170 11 239 182 13 246 190 14 246 190 14
  105244. -246 190 14 246 190 14 242 186 14 206 162 42
  105245. -101 101 101 58 58 58 30 30 30 14 14 14
  105246. - 6 6 6 0 0 0 0 0 0 0 0 0
  105247. - 0 0 0 0 0 0 0 0 0 0 0 0
  105248. - 0 0 0 0 0 0 0 0 0 0 0 0
  105249. - 0 0 0 0 0 0 0 0 0 10 10 10
  105250. - 30 30 30 74 74 74 174 135 50 216 158 10
  105251. -236 178 12 246 190 14 246 190 14 246 190 14
  105252. -246 190 14 246 190 14 246 190 14 246 190 14
  105253. -246 190 14 246 190 14 246 190 14 246 190 14
  105254. -246 190 14 246 190 14 241 196 14 226 184 13
  105255. - 61 42 6 2 2 6 2 2 6 2 2 6
  105256. - 22 22 22 238 238 238 253 253 253 253 253 253
  105257. -253 253 253 253 253 253 253 253 253 253 253 253
  105258. -253 253 253 253 253 253 253 253 253 253 253 253
  105259. -253 253 253 253 253 253 253 253 253 253 253 253
  105260. -253 253 253 226 226 226 187 187 187 180 133 36
  105261. -216 158 10 236 178 12 239 182 13 236 178 12
  105262. -230 174 11 226 170 11 226 170 11 230 174 11
  105263. -236 178 12 242 186 14 246 190 14 246 190 14
  105264. -246 190 14 246 190 14 246 186 14 239 182 13
  105265. -206 162 42 106 106 106 66 66 66 34 34 34
  105266. - 14 14 14 6 6 6 0 0 0 0 0 0
  105267. - 0 0 0 0 0 0 0 0 0 0 0 0
  105268. - 0 0 0 0 0 0 0 0 0 0 0 0
  105269. - 0 0 0 0 0 0 0 0 0 6 6 6
  105270. - 26 26 26 70 70 70 163 133 67 213 154 11
  105271. -236 178 12 246 190 14 246 190 14 246 190 14
  105272. -246 190 14 246 190 14 246 190 14 246 190 14
  105273. -246 190 14 246 190 14 246 190 14 246 190 14
  105274. -246 190 14 246 190 14 246 190 14 241 196 14
  105275. -190 146 13 18 14 6 2 2 6 2 2 6
  105276. - 46 46 46 246 246 246 253 253 253 253 253 253
  105277. -253 253 253 253 253 253 253 253 253 253 253 253
  105278. -253 253 253 253 253 253 253 253 253 253 253 253
  105279. -253 253 253 253 253 253 253 253 253 253 253 253
  105280. -253 253 253 221 221 221 86 86 86 156 107 11
  105281. -216 158 10 236 178 12 242 186 14 246 186 14
  105282. -242 186 14 239 182 13 239 182 13 242 186 14
  105283. -242 186 14 246 186 14 246 190 14 246 190 14
  105284. -246 190 14 246 190 14 246 190 14 246 190 14
  105285. -242 186 14 225 175 15 142 122 72 66 66 66
  105286. - 30 30 30 10 10 10 0 0 0 0 0 0
  105287. - 0 0 0 0 0 0 0 0 0 0 0 0
  105288. - 0 0 0 0 0 0 0 0 0 0 0 0
  105289. - 0 0 0 0 0 0 0 0 0 6 6 6
  105290. - 26 26 26 70 70 70 163 133 67 210 150 10
  105291. -236 178 12 246 190 14 246 190 14 246 190 14
  105292. -246 190 14 246 190 14 246 190 14 246 190 14
  105293. -246 190 14 246 190 14 246 190 14 246 190 14
  105294. -246 190 14 246 190 14 246 190 14 246 190 14
  105295. -232 195 16 121 92 8 34 34 34 106 106 106
  105296. -221 221 221 253 253 253 253 253 253 253 253 253
  105297. -253 253 253 253 253 253 253 253 253 253 253 253
  105298. -253 253 253 253 253 253 253 253 253 253 253 253
  105299. -253 253 253 253 253 253 253 253 253 253 253 253
  105300. -242 242 242 82 82 82 18 14 6 163 110 8
  105301. -216 158 10 236 178 12 242 186 14 246 190 14
  105302. -246 190 14 246 190 14 246 190 14 246 190 14
  105303. -246 190 14 246 190 14 246 190 14 246 190 14
  105304. -246 190 14 246 190 14 246 190 14 246 190 14
  105305. -246 190 14 246 190 14 242 186 14 163 133 67
  105306. - 46 46 46 18 18 18 6 6 6 0 0 0
  105307. - 0 0 0 0 0 0 0 0 0 0 0 0
  105308. - 0 0 0 0 0 0 0 0 0 0 0 0
  105309. - 0 0 0 0 0 0 0 0 0 10 10 10
  105310. - 30 30 30 78 78 78 163 133 67 210 150 10
  105311. -236 178 12 246 186 14 246 190 14 246 190 14
  105312. -246 190 14 246 190 14 246 190 14 246 190 14
  105313. -246 190 14 246 190 14 246 190 14 246 190 14
  105314. -246 190 14 246 190 14 246 190 14 246 190 14
  105315. -241 196 14 215 174 15 190 178 144 253 253 253
  105316. -253 253 253 253 253 253 253 253 253 253 253 253
  105317. -253 253 253 253 253 253 253 253 253 253 253 253
  105318. -253 253 253 253 253 253 253 253 253 253 253 253
  105319. -253 253 253 253 253 253 253 253 253 218 218 218
  105320. - 58 58 58 2 2 6 22 18 6 167 114 7
  105321. -216 158 10 236 178 12 246 186 14 246 190 14
  105322. -246 190 14 246 190 14 246 190 14 246 190 14
  105323. -246 190 14 246 190 14 246 190 14 246 190 14
  105324. -246 190 14 246 190 14 246 190 14 246 190 14
  105325. -246 190 14 246 186 14 242 186 14 190 150 46
  105326. - 54 54 54 22 22 22 6 6 6 0 0 0
  105327. - 0 0 0 0 0 0 0 0 0 0 0 0
  105328. - 0 0 0 0 0 0 0 0 0 0 0 0
  105329. - 0 0 0 0 0 0 0 0 0 14 14 14
  105330. - 38 38 38 86 86 86 180 133 36 213 154 11
  105331. -236 178 12 246 186 14 246 190 14 246 190 14
  105332. -246 190 14 246 190 14 246 190 14 246 190 14
  105333. -246 190 14 246 190 14 246 190 14 246 190 14
  105334. -246 190 14 246 190 14 246 190 14 246 190 14
  105335. -246 190 14 232 195 16 190 146 13 214 214 214
  105336. -253 253 253 253 253 253 253 253 253 253 253 253
  105337. -253 253 253 253 253 253 253 253 253 253 253 253
  105338. -253 253 253 253 253 253 253 253 253 253 253 253
  105339. -253 253 253 250 250 250 170 170 170 26 26 26
  105340. - 2 2 6 2 2 6 37 26 9 163 110 8
  105341. -219 162 10 239 182 13 246 186 14 246 190 14
  105342. -246 190 14 246 190 14 246 190 14 246 190 14
  105343. -246 190 14 246 190 14 246 190 14 246 190 14
  105344. -246 190 14 246 190 14 246 190 14 246 190 14
  105345. -246 186 14 236 178 12 224 166 10 142 122 72
  105346. - 46 46 46 18 18 18 6 6 6 0 0 0
  105347. - 0 0 0 0 0 0 0 0 0 0 0 0
  105348. - 0 0 0 0 0 0 0 0 0 0 0 0
  105349. - 0 0 0 0 0 0 6 6 6 18 18 18
  105350. - 50 50 50 109 106 95 192 133 9 224 166 10
  105351. -242 186 14 246 190 14 246 190 14 246 190 14
  105352. -246 190 14 246 190 14 246 190 14 246 190 14
  105353. -246 190 14 246 190 14 246 190 14 246 190 14
  105354. -246 190 14 246 190 14 246 190 14 246 190 14
  105355. -242 186 14 226 184 13 210 162 10 142 110 46
  105356. -226 226 226 253 253 253 253 253 253 253 253 253
  105357. -253 253 253 253 253 253 253 253 253 253 253 253
  105358. -253 253 253 253 253 253 253 253 253 253 253 253
  105359. -198 198 198 66 66 66 2 2 6 2 2 6
  105360. - 2 2 6 2 2 6 50 34 6 156 107 11
  105361. -219 162 10 239 182 13 246 186 14 246 190 14
  105362. -246 190 14 246 190 14 246 190 14 246 190 14
  105363. -246 190 14 246 190 14 246 190 14 246 190 14
  105364. -246 190 14 246 190 14 246 190 14 242 186 14
  105365. -234 174 13 213 154 11 154 122 46 66 66 66
  105366. - 30 30 30 10 10 10 0 0 0 0 0 0
  105367. - 0 0 0 0 0 0 0 0 0 0 0 0
  105368. - 0 0 0 0 0 0 0 0 0 0 0 0
  105369. - 0 0 0 0 0 0 6 6 6 22 22 22
  105370. - 58 58 58 154 121 60 206 145 10 234 174 13
  105371. -242 186 14 246 186 14 246 190 14 246 190 14
  105372. -246 190 14 246 190 14 246 190 14 246 190 14
  105373. -246 190 14 246 190 14 246 190 14 246 190 14
  105374. -246 190 14 246 190 14 246 190 14 246 190 14
  105375. -246 186 14 236 178 12 210 162 10 163 110 8
  105376. - 61 42 6 138 138 138 218 218 218 250 250 250
  105377. -253 253 253 253 253 253 253 253 253 250 250 250
  105378. -242 242 242 210 210 210 144 144 144 66 66 66
  105379. - 6 6 6 2 2 6 2 2 6 2 2 6
  105380. - 2 2 6 2 2 6 61 42 6 163 110 8
  105381. -216 158 10 236 178 12 246 190 14 246 190 14
  105382. -246 190 14 246 190 14 246 190 14 246 190 14
  105383. -246 190 14 246 190 14 246 190 14 246 190 14
  105384. -246 190 14 239 182 13 230 174 11 216 158 10
  105385. -190 142 34 124 112 88 70 70 70 38 38 38
  105386. - 18 18 18 6 6 6 0 0 0 0 0 0
  105387. - 0 0 0 0 0 0 0 0 0 0 0 0
  105388. - 0 0 0 0 0 0 0 0 0 0 0 0
  105389. - 0 0 0 0 0 0 6 6 6 22 22 22
  105390. - 62 62 62 168 124 44 206 145 10 224 166 10
  105391. -236 178 12 239 182 13 242 186 14 242 186 14
  105392. -246 186 14 246 190 14 246 190 14 246 190 14
  105393. -246 190 14 246 190 14 246 190 14 246 190 14
  105394. -246 190 14 246 190 14 246 190 14 246 190 14
  105395. -246 190 14 236 178 12 216 158 10 175 118 6
  105396. - 80 54 7 2 2 6 6 6 6 30 30 30
  105397. - 54 54 54 62 62 62 50 50 50 38 38 38
  105398. - 14 14 14 2 2 6 2 2 6 2 2 6
  105399. - 2 2 6 2 2 6 2 2 6 2 2 6
  105400. - 2 2 6 6 6 6 80 54 7 167 114 7
  105401. -213 154 11 236 178 12 246 190 14 246 190 14
  105402. -246 190 14 246 190 14 246 190 14 246 190 14
  105403. -246 190 14 242 186 14 239 182 13 239 182 13
  105404. -230 174 11 210 150 10 174 135 50 124 112 88
  105405. - 82 82 82 54 54 54 34 34 34 18 18 18
  105406. - 6 6 6 0 0 0 0 0 0 0 0 0
  105407. - 0 0 0 0 0 0 0 0 0 0 0 0
  105408. - 0 0 0 0 0 0 0 0 0 0 0 0
  105409. - 0 0 0 0 0 0 6 6 6 18 18 18
  105410. - 50 50 50 158 118 36 192 133 9 200 144 11
  105411. -216 158 10 219 162 10 224 166 10 226 170 11
  105412. -230 174 11 236 178 12 239 182 13 239 182 13
  105413. -242 186 14 246 186 14 246 190 14 246 190 14
  105414. -246 190 14 246 190 14 246 190 14 246 190 14
  105415. -246 186 14 230 174 11 210 150 10 163 110 8
  105416. -104 69 6 10 10 10 2 2 6 2 2 6
  105417. - 2 2 6 2 2 6 2 2 6 2 2 6
  105418. - 2 2 6 2 2 6 2 2 6 2 2 6
  105419. - 2 2 6 2 2 6 2 2 6 2 2 6
  105420. - 2 2 6 6 6 6 91 60 6 167 114 7
  105421. -206 145 10 230 174 11 242 186 14 246 190 14
  105422. -246 190 14 246 190 14 246 186 14 242 186 14
  105423. -239 182 13 230 174 11 224 166 10 213 154 11
  105424. -180 133 36 124 112 88 86 86 86 58 58 58
  105425. - 38 38 38 22 22 22 10 10 10 6 6 6
  105426. - 0 0 0 0 0 0 0 0 0 0 0 0
  105427. - 0 0 0 0 0 0 0 0 0 0 0 0
  105428. - 0 0 0 0 0 0 0 0 0 0 0 0
  105429. - 0 0 0 0 0 0 0 0 0 14 14 14
  105430. - 34 34 34 70 70 70 138 110 50 158 118 36
  105431. -167 114 7 180 123 7 192 133 9 197 138 11
  105432. -200 144 11 206 145 10 213 154 11 219 162 10
  105433. -224 166 10 230 174 11 239 182 13 242 186 14
  105434. -246 186 14 246 186 14 246 186 14 246 186 14
  105435. -239 182 13 216 158 10 185 133 11 152 99 6
  105436. -104 69 6 18 14 6 2 2 6 2 2 6
  105437. - 2 2 6 2 2 6 2 2 6 2 2 6
  105438. - 2 2 6 2 2 6 2 2 6 2 2 6
  105439. - 2 2 6 2 2 6 2 2 6 2 2 6
  105440. - 2 2 6 6 6 6 80 54 7 152 99 6
  105441. -192 133 9 219 162 10 236 178 12 239 182 13
  105442. -246 186 14 242 186 14 239 182 13 236 178 12
  105443. -224 166 10 206 145 10 192 133 9 154 121 60
  105444. - 94 94 94 62 62 62 42 42 42 22 22 22
  105445. - 14 14 14 6 6 6 0 0 0 0 0 0
  105446. - 0 0 0 0 0 0 0 0 0 0 0 0
  105447. - 0 0 0 0 0 0 0 0 0 0 0 0
  105448. - 0 0 0 0 0 0 0 0 0 0 0 0
  105449. - 0 0 0 0 0 0 0 0 0 6 6 6
  105450. - 18 18 18 34 34 34 58 58 58 78 78 78
  105451. -101 98 89 124 112 88 142 110 46 156 107 11
  105452. -163 110 8 167 114 7 175 118 6 180 123 7
  105453. -185 133 11 197 138 11 210 150 10 219 162 10
  105454. -226 170 11 236 178 12 236 178 12 234 174 13
  105455. -219 162 10 197 138 11 163 110 8 130 83 6
  105456. - 91 60 6 10 10 10 2 2 6 2 2 6
  105457. - 18 18 18 38 38 38 38 38 38 38 38 38
  105458. - 38 38 38 38 38 38 38 38 38 38 38 38
  105459. - 38 38 38 38 38 38 26 26 26 2 2 6
  105460. - 2 2 6 6 6 6 70 47 6 137 92 6
  105461. -175 118 6 200 144 11 219 162 10 230 174 11
  105462. -234 174 13 230 174 11 219 162 10 210 150 10
  105463. -192 133 9 163 110 8 124 112 88 82 82 82
  105464. - 50 50 50 30 30 30 14 14 14 6 6 6
  105465. - 0 0 0 0 0 0 0 0 0 0 0 0
  105466. - 0 0 0 0 0 0 0 0 0 0 0 0
  105467. - 0 0 0 0 0 0 0 0 0 0 0 0
  105468. - 0 0 0 0 0 0 0 0 0 0 0 0
  105469. - 0 0 0 0 0 0 0 0 0 0 0 0
  105470. - 6 6 6 14 14 14 22 22 22 34 34 34
  105471. - 42 42 42 58 58 58 74 74 74 86 86 86
  105472. -101 98 89 122 102 70 130 98 46 121 87 25
  105473. -137 92 6 152 99 6 163 110 8 180 123 7
  105474. -185 133 11 197 138 11 206 145 10 200 144 11
  105475. -180 123 7 156 107 11 130 83 6 104 69 6
  105476. - 50 34 6 54 54 54 110 110 110 101 98 89
  105477. - 86 86 86 82 82 82 78 78 78 78 78 78
  105478. - 78 78 78 78 78 78 78 78 78 78 78 78
  105479. - 78 78 78 82 82 82 86 86 86 94 94 94
  105480. -106 106 106 101 101 101 86 66 34 124 80 6
  105481. -156 107 11 180 123 7 192 133 9 200 144 11
  105482. -206 145 10 200 144 11 192 133 9 175 118 6
  105483. -139 102 15 109 106 95 70 70 70 42 42 42
  105484. - 22 22 22 10 10 10 0 0 0 0 0 0
  105485. - 0 0 0 0 0 0 0 0 0 0 0 0
  105486. - 0 0 0 0 0 0 0 0 0 0 0 0
  105487. - 0 0 0 0 0 0 0 0 0 0 0 0
  105488. - 0 0 0 0 0 0 0 0 0 0 0 0
  105489. - 0 0 0 0 0 0 0 0 0 0 0 0
  105490. - 0 0 0 0 0 0 6 6 6 10 10 10
  105491. - 14 14 14 22 22 22 30 30 30 38 38 38
  105492. - 50 50 50 62 62 62 74 74 74 90 90 90
  105493. -101 98 89 112 100 78 121 87 25 124 80 6
  105494. -137 92 6 152 99 6 152 99 6 152 99 6
  105495. -138 86 6 124 80 6 98 70 6 86 66 30
  105496. -101 98 89 82 82 82 58 58 58 46 46 46
  105497. - 38 38 38 34 34 34 34 34 34 34 34 34
  105498. - 34 34 34 34 34 34 34 34 34 34 34 34
  105499. - 34 34 34 34 34 34 38 38 38 42 42 42
  105500. - 54 54 54 82 82 82 94 86 76 91 60 6
  105501. -134 86 6 156 107 11 167 114 7 175 118 6
  105502. -175 118 6 167 114 7 152 99 6 121 87 25
  105503. -101 98 89 62 62 62 34 34 34 18 18 18
  105504. - 6 6 6 0 0 0 0 0 0 0 0 0
  105505. - 0 0 0 0 0 0 0 0 0 0 0 0
  105506. - 0 0 0 0 0 0 0 0 0 0 0 0
  105507. - 0 0 0 0 0 0 0 0 0 0 0 0
  105508. - 0 0 0 0 0 0 0 0 0 0 0 0
  105509. - 0 0 0 0 0 0 0 0 0 0 0 0
  105510. - 0 0 0 0 0 0 0 0 0 0 0 0
  105511. - 0 0 0 6 6 6 6 6 6 10 10 10
  105512. - 18 18 18 22 22 22 30 30 30 42 42 42
  105513. - 50 50 50 66 66 66 86 86 86 101 98 89
  105514. -106 86 58 98 70 6 104 69 6 104 69 6
  105515. -104 69 6 91 60 6 82 62 34 90 90 90
  105516. - 62 62 62 38 38 38 22 22 22 14 14 14
  105517. - 10 10 10 10 10 10 10 10 10 10 10 10
  105518. - 10 10 10 10 10 10 6 6 6 10 10 10
  105519. - 10 10 10 10 10 10 10 10 10 14 14 14
  105520. - 22 22 22 42 42 42 70 70 70 89 81 66
  105521. - 80 54 7 104 69 6 124 80 6 137 92 6
  105522. -134 86 6 116 81 8 100 82 52 86 86 86
  105523. - 58 58 58 30 30 30 14 14 14 6 6 6
  105524. - 0 0 0 0 0 0 0 0 0 0 0 0
  105525. - 0 0 0 0 0 0 0 0 0 0 0 0
  105526. - 0 0 0 0 0 0 0 0 0 0 0 0
  105527. - 0 0 0 0 0 0 0 0 0 0 0 0
  105528. - 0 0 0 0 0 0 0 0 0 0 0 0
  105529. - 0 0 0 0 0 0 0 0 0 0 0 0
  105530. - 0 0 0 0 0 0 0 0 0 0 0 0
  105531. - 0 0 0 0 0 0 0 0 0 0 0 0
  105532. - 0 0 0 6 6 6 10 10 10 14 14 14
  105533. - 18 18 18 26 26 26 38 38 38 54 54 54
  105534. - 70 70 70 86 86 86 94 86 76 89 81 66
  105535. - 89 81 66 86 86 86 74 74 74 50 50 50
  105536. - 30 30 30 14 14 14 6 6 6 0 0 0
  105537. - 0 0 0 0 0 0 0 0 0 0 0 0
  105538. - 0 0 0 0 0 0 0 0 0 0 0 0
  105539. - 0 0 0 0 0 0 0 0 0 0 0 0
  105540. - 6 6 6 18 18 18 34 34 34 58 58 58
  105541. - 82 82 82 89 81 66 89 81 66 89 81 66
  105542. - 94 86 66 94 86 76 74 74 74 50 50 50
  105543. - 26 26 26 14 14 14 6 6 6 0 0 0
  105544. - 0 0 0 0 0 0 0 0 0 0 0 0
  105545. - 0 0 0 0 0 0 0 0 0 0 0 0
  105546. - 0 0 0 0 0 0 0 0 0 0 0 0
  105547. - 0 0 0 0 0 0 0 0 0 0 0 0
  105548. - 0 0 0 0 0 0 0 0 0 0 0 0
  105549. - 0 0 0 0 0 0 0 0 0 0 0 0
  105550. - 0 0 0 0 0 0 0 0 0 0 0 0
  105551. - 0 0 0 0 0 0 0 0 0 0 0 0
  105552. - 0 0 0 0 0 0 0 0 0 0 0 0
  105553. - 6 6 6 6 6 6 14 14 14 18 18 18
  105554. - 30 30 30 38 38 38 46 46 46 54 54 54
  105555. - 50 50 50 42 42 42 30 30 30 18 18 18
  105556. - 10 10 10 0 0 0 0 0 0 0 0 0
  105557. - 0 0 0 0 0 0 0 0 0 0 0 0
  105558. - 0 0 0 0 0 0 0 0 0 0 0 0
  105559. - 0 0 0 0 0 0 0 0 0 0 0 0
  105560. - 0 0 0 6 6 6 14 14 14 26 26 26
  105561. - 38 38 38 50 50 50 58 58 58 58 58 58
  105562. - 54 54 54 42 42 42 30 30 30 18 18 18
  105563. - 10 10 10 0 0 0 0 0 0 0 0 0
  105564. - 0 0 0 0 0 0 0 0 0 0 0 0
  105565. - 0 0 0 0 0 0 0 0 0 0 0 0
  105566. - 0 0 0 0 0 0 0 0 0 0 0 0
  105567. - 0 0 0 0 0 0 0 0 0 0 0 0
  105568. - 0 0 0 0 0 0 0 0 0 0 0 0
  105569. - 0 0 0 0 0 0 0 0 0 0 0 0
  105570. - 0 0 0 0 0 0 0 0 0 0 0 0
  105571. - 0 0 0 0 0 0 0 0 0 0 0 0
  105572. - 0 0 0 0 0 0 0 0 0 0 0 0
  105573. - 0 0 0 0 0 0 0 0 0 6 6 6
  105574. - 6 6 6 10 10 10 14 14 14 18 18 18
  105575. - 18 18 18 14 14 14 10 10 10 6 6 6
  105576. - 0 0 0 0 0 0 0 0 0 0 0 0
  105577. - 0 0 0 0 0 0 0 0 0 0 0 0
  105578. - 0 0 0 0 0 0 0 0 0 0 0 0
  105579. - 0 0 0 0 0 0 0 0 0 0 0 0
  105580. - 0 0 0 0 0 0 0 0 0 6 6 6
  105581. - 14 14 14 18 18 18 22 22 22 22 22 22
  105582. - 18 18 18 14 14 14 10 10 10 6 6 6
  105583. - 0 0 0 0 0 0 0 0 0 0 0 0
  105584. - 0 0 0 0 0 0 0 0 0 0 0 0
  105585. - 0 0 0 0 0 0 0 0 0 0 0 0
  105586. - 0 0 0 0 0 0 0 0 0 0 0 0
  105587. - 0 0 0 0 0 0 0 0 0 0 0 0
  105588. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105589. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105590. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105591. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105592. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105593. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105594. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105595. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105596. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105597. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105598. +0 0 0 0 0 0 0 0 0
  105599. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105600. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105601. +0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0
  105602. +0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105603. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105604. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105605. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105606. +0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0
  105607. +0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105608. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105609. +0 0 0 0 0 0 0 0 0
  105610. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105611. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
  105612. +10 15 3 2 3 1 12 18 4 42 61 14 19 27 6 11 16 4
  105613. +38 55 13 10 15 3 3 4 1 10 15 3 0 0 0 0 0 0
  105614. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105615. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105616. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 1
  105617. +12 18 4 1 1 0 23 34 8 31 45 11 10 15 3 32 47 11
  105618. +34 49 12 3 4 1 3 4 1 3 4 1 0 0 0 0 0 0
  105619. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105620. +0 0 0 0 0 0 0 0 0
  105621. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105622. +0 0 0 0 0 0 10 15 3 29 42 10 26 37 9 12 18 4
  105623. +55 80 19 81 118 28 55 80 19 92 132 31 106 153 36 69 100 23
  105624. +100 144 34 80 116 27 42 61 14 81 118 28 23 34 8 27 40 9
  105625. +15 21 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105626. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105627. +0 0 0 0 0 0 1 1 0 29 42 10 15 21 5 50 72 17
  105628. +74 107 25 45 64 15 102 148 35 80 116 27 84 121 28 111 160 38
  105629. +69 100 23 65 94 22 81 118 28 29 42 10 17 25 6 29 42 10
  105630. +23 34 8 2 3 1 0 0 0 0 0 0 0 0 0 0 0 0
  105631. +0 0 0 0 0 0 0 0 0
  105632. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 4 1
  105633. +15 21 5 15 21 5 34 49 12 101 146 34 111 161 38 97 141 33
  105634. +97 141 33 119 172 41 117 170 40 116 167 40 118 170 40 118 171 40
  105635. +117 169 40 118 170 40 111 160 38 118 170 40 96 138 32 89 128 30
  105636. +81 118 28 11 16 4 10 15 3 1 1 0 0 0 0 0 0 0
  105637. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105638. +3 4 1 3 4 1 34 49 12 101 146 34 79 115 27 111 160 38
  105639. +114 165 39 113 163 39 118 170 40 117 169 40 118 171 40 117 169 40
  105640. +116 167 40 119 172 41 113 163 39 92 132 31 105 151 36 113 163 39
  105641. +75 109 26 19 27 6 16 23 5 11 16 4 0 1 0 0 0 0
  105642. +0 0 0 0 0 0 0 0 0
  105643. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 15 3
  105644. +80 116 27 106 153 36 105 151 36 114 165 39 118 170 40 118 171 40
  105645. +118 171 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105646. +117 169 40 117 169 40 117 170 40 117 169 40 118 170 40 118 170 40
  105647. +117 170 40 75 109 26 75 109 26 34 49 12 0 0 0 0 0 0
  105648. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 4 1
  105649. +64 92 22 65 94 22 100 144 34 118 171 40 118 170 40 117 169 40
  105650. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105651. +117 169 40 117 169 40 117 169 40 118 171 41 118 170 40 117 169 40
  105652. +109 158 37 105 151 36 104 150 35 47 69 16 0 0 0 0 0 0
  105653. +0 0 0 0 0 0 0 0 0
  105654. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105655. +42 61 14 115 167 39 118 170 40 117 169 40 117 169 40 117 169 40
  105656. +117 170 40 117 170 40 117 169 40 117 169 40 117 169 40 117 169 40
  105657. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105658. +117 169 40 117 169 40 118 170 40 96 138 32 17 25 6 0 0 0
  105659. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47 69 16
  105660. +114 165 39 117 168 40 117 170 40 117 169 40 117 169 40 117 169 40
  105661. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105662. +117 169 40 117 169 40 118 170 40 117 169 40 117 169 40 117 169 40
  105663. +117 170 40 119 172 41 96 138 32 12 18 4 0 0 0 0 0 0
  105664. +0 0 0 0 0 0 0 0 0
  105665. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 15 3
  105666. +32 47 11 105 151 36 118 170 40 117 169 40 117 169 40 116 168 40
  105667. +109 157 37 111 160 38 117 169 40 118 171 40 117 169 40 117 169 40
  105668. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105669. +117 169 40 117 169 40 117 169 40 118 171 40 69 100 23 2 3 1
  105670. +0 0 0 0 0 0 0 0 0 0 0 0 19 27 6 101 146 34
  105671. +118 171 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105672. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 170 40
  105673. +118 171 40 115 166 39 107 154 36 111 161 38 117 169 40 117 169 40
  105674. +117 169 40 118 171 40 75 109 26 19 27 6 2 3 1 0 0 0
  105675. +0 0 0 0 0 0 0 0 0
  105676. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 23 5
  105677. +89 128 30 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105678. +111 160 38 92 132 31 79 115 27 96 138 32 115 166 39 119 171 41
  105679. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105680. +117 169 40 117 169 40 117 169 40 118 170 40 109 157 37 26 37 9
  105681. +0 0 0 0 0 0 0 0 0 0 0 0 64 92 22 118 171 40
  105682. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105683. +117 169 40 117 169 40 117 169 40 118 170 40 118 171 40 109 157 37
  105684. +89 128 30 81 118 28 100 144 34 115 166 39 117 169 40 117 169 40
  105685. +117 169 40 117 170 40 113 163 39 60 86 20 1 1 0 0 0 0
  105686. +0 0 0 0 0 0 0 0 0
  105687. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105688. +27 40 9 96 138 32 118 170 40 117 169 40 117 169 40 117 169 40
  105689. +117 170 40 117 169 40 101 146 34 67 96 23 55 80 19 84 121 28
  105690. +113 163 39 119 171 41 117 169 40 117 169 40 117 169 40 117 169 40
  105691. +117 169 40 117 169 40 117 169 40 117 169 40 119 171 41 65 94 22
  105692. +0 0 0 0 0 0 0 0 0 15 21 5 101 146 34 118 171 40
  105693. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105694. +117 169 40 118 170 40 118 171 40 104 150 35 69 100 23 53 76 18
  105695. +81 118 28 111 160 38 118 170 40 117 169 40 117 169 40 117 169 40
  105696. +117 169 40 114 165 39 69 100 23 10 15 3 0 0 0 0 0 0
  105697. +0 0 0 0 0 0 0 0 0
  105698. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
  105699. +31 45 11 77 111 26 117 169 40 117 169 40 117 169 40 117 169 40
  105700. +117 169 40 117 169 40 118 170 40 116 168 40 92 132 31 47 69 16
  105701. +38 55 13 81 118 28 113 163 39 119 171 41 117 169 40 117 169 40
  105702. +117 169 40 117 169 40 117 169 40 117 169 40 118 171 41 92 132 31
  105703. +10 15 3 0 0 0 0 0 0 36 52 12 115 166 39 117 169 40
  105704. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 118 170 40
  105705. +118 171 40 102 148 35 64 92 22 34 49 12 65 94 22 106 153 36
  105706. +118 171 40 117 170 40 117 169 40 117 169 40 117 169 40 117 169 40
  105707. +118 170 40 107 154 36 55 80 19 15 21 5 0 0 0 0 0 0
  105708. +0 0 0 0 0 0 0 0 0
  105709. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105710. +29 42 10 101 146 34 118 171 40 117 169 40 117 169 40 117 169 40
  105711. +117 169 40 117 169 40 117 169 40 117 169 40 118 171 40 113 163 39
  105712. +75 109 26 27 40 9 36 52 12 89 128 30 116 167 40 118 171 40
  105713. +117 169 40 117 169 40 117 169 40 117 169 40 118 170 40 104 150 35
  105714. +16 23 5 0 0 0 0 0 0 53 76 18 118 171 40 117 169 40
  105715. +117 169 40 117 169 40 117 169 40 117 169 40 119 171 41 109 157 37
  105716. +67 96 23 23 34 8 42 61 14 96 138 32 118 170 40 118 170 40
  105717. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105718. +117 169 40 117 169 40 74 107 25 10 15 3 0 0 0 0 0 0
  105719. +0 0 0 0 0 0 0 0 0
  105720. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105721. +0 0 0 31 45 11 101 146 34 118 170 40 117 169 40 117 169 40
  105722. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105723. +119 171 41 102 148 35 47 69 16 14 20 5 50 72 17 102 148 35
  105724. +118 171 40 117 169 40 117 169 40 117 169 40 118 170 40 102 148 35
  105725. +15 21 5 0 0 0 0 0 0 50 72 17 118 170 40 117 169 40
  105726. +117 169 40 117 169 40 118 170 40 116 167 40 84 121 28 27 40 9
  105727. +19 27 6 74 107 25 114 165 39 118 171 40 117 169 40 117 169 40
  105728. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105729. +117 169 40 75 109 26 10 15 4 0 0 0 0 0 0 0 0 0
  105730. +0 0 0 0 0 0 0 0 0
  105731. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105732. +0 0 0 38 55 13 102 148 35 118 171 40 117 169 40 117 169 40
  105733. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105734. +117 169 40 118 170 40 115 167 39 77 111 26 17 25 6 19 27 6
  105735. +77 111 26 115 166 39 118 170 40 117 169 40 119 172 41 81 118 28
  105736. +3 4 1 0 0 0 0 0 0 27 40 9 111 160 38 118 170 40
  105737. +117 169 40 118 171 40 105 151 36 50 72 17 10 15 3 38 55 13
  105738. +100 144 34 118 171 40 117 169 40 117 169 40 117 169 40 117 169 40
  105739. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105740. +117 169 40 79 115 27 15 21 5 0 0 0 0 0 0 0 0 0
  105741. +0 0 0 0 0 0 0 0 0
  105742. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105743. +0 0 0 10 15 3 64 92 22 111 160 38 117 169 40 117 169 40
  105744. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105745. +117 169 40 117 169 40 117 169 40 118 171 40 96 138 32 32 47 11
  105746. +3 4 1 50 72 17 107 154 36 120 173 41 105 151 36 31 45 11
  105747. +0 0 0 0 0 0 0 0 0 3 4 1 65 94 22 117 169 40
  105748. +118 170 40 89 128 30 26 37 9 3 4 1 60 86 20 111 161 38
  105749. +118 171 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105750. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105751. +97 141 33 36 52 12 1 1 0 0 0 0 0 0 0 0 0 0
  105752. +0 0 0 0 0 0 0 0 0
  105753. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105754. +0 0 0 0 0 0 14 20 5 75 109 26 117 168 40 117 169 40
  105755. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105756. +117 169 40 117 169 40 117 169 40 117 169 40 118 171 40 107 154 36
  105757. +45 64 15 2 3 1 31 45 11 75 109 26 32 47 11 0 1 0
  105758. +0 0 0 0 0 0 0 0 0 0 0 0 10 15 3 55 80 19
  105759. +65 94 22 11 16 4 11 16 4 75 109 26 116 168 40 118 170 40
  105760. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105761. +117 169 40 117 169 40 117 169 40 117 169 40 118 170 40 107 154 36
  105762. +47 69 16 3 4 1 0 0 0 0 0 0 0 0 0 0 0 0
  105763. +0 0 0 0 0 0 0 0 0
  105764. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105765. +0 0 0 0 0 0 12 18 4 69 100 23 111 161 38 118 171 40
  105766. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105767. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 118 170 40
  105768. +111 160 38 50 72 17 2 3 1 2 3 1 0 0 0 0 0 0
  105769. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
  105770. +1 1 0 12 18 4 81 118 28 118 170 40 117 169 40 117 169 40
  105771. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105772. +117 169 40 117 169 40 117 169 40 117 170 40 118 171 40 101 146 34
  105773. +42 61 14 2 3 1 0 0 0 0 0 0 0 0 0 0 0 0
  105774. +0 0 0 0 0 0 0 0 0
  105775. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105776. +0 0 0 0 0 0 0 0 0 3 4 1 36 52 12 89 128 30
  105777. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105778. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105779. +118 171 41 101 146 34 14 20 5 0 0 0 0 0 0 0 0 0
  105780. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105781. +0 0 0 47 69 16 118 170 40 117 169 40 117 169 40 117 169 40
  105782. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105783. +117 169 40 117 169 40 117 170 40 111 160 38 69 100 23 19 27 6
  105784. +0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105785. +0 0 0 0 0 0 0 0 0
  105786. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105787. +0 0 0 0 0 0 0 0 0 0 0 0 11 16 4 69 100 23
  105788. +115 167 39 119 172 41 117 169 40 117 169 40 117 169 40 117 169 40
  105789. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105790. +119 172 41 75 109 26 3 4 1 0 0 0 0 0 0 0 0 0
  105791. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105792. +0 0 0 23 34 8 106 153 36 118 170 40 117 169 40 117 169 40
  105793. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105794. +117 169 40 118 170 40 119 172 41 105 151 36 42 61 14 2 3 1
  105795. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105796. +0 0 0 0 0 0 0 0 0
  105797. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105798. +0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 15 21 5
  105799. +45 64 15 80 116 27 114 165 39 118 170 40 117 169 40 117 169 40
  105800. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 119 172 41
  105801. +97 141 33 20 30 7 0 0 0 0 0 0 0 0 0 0 0 0
  105802. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105803. +0 0 0 1 1 0 53 76 18 114 165 39 118 171 40 117 169 40
  105804. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  105805. +118 171 40 104 150 35 64 92 22 31 45 11 10 15 3 0 0 0
  105806. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105807. +0 0 0 0 0 0 0 0 0
  105808. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105809. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105810. +0 0 0 36 52 12 97 141 33 109 158 37 113 163 39 116 168 40
  105811. +117 169 40 117 170 40 118 170 40 119 172 41 115 167 39 84 121 28
  105812. +23 34 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105813. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105814. +0 0 0 0 0 0 3 4 1 50 72 17 102 148 35 118 171 40
  105815. +119 171 41 118 170 40 117 169 40 117 169 40 115 166 39 111 161 38
  105816. +109 157 37 79 115 27 12 18 4 0 0 0 0 0 0 0 0 0
  105817. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105818. +0 0 0 0 0 0 0 0 0
  105819. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105820. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105821. +0 0 0 3 4 1 15 21 5 23 34 8 45 64 15 106 153 36
  105822. +116 167 40 111 160 38 101 146 34 79 115 27 42 61 14 10 15 3
  105823. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105824. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105825. +0 0 0 0 0 0 0 0 0 1 1 0 20 30 7 60 86 20
  105826. +89 128 30 106 153 36 113 163 39 117 169 40 84 121 28 29 42 10
  105827. +19 27 6 10 15 3 2 3 1 0 0 0 0 0 0 0 0 0
  105828. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105829. +0 0 0 0 0 0 0 0 0
  105830. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105831. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105832. +0 0 0 0 0 0 0 0 0 0 0 0 16 23 5 38 55 13
  105833. +36 52 12 26 37 9 12 18 4 2 3 1 0 0 0 0 0 0
  105834. +0 0 0 0 0 0 0 0 0 1 0 0 19 2 7 52 5 18
  105835. +78 7 27 88 8 31 81 7 29 56 5 19 25 2 9 3 0 1
  105836. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105837. +3 4 1 19 27 6 31 45 11 38 55 13 32 47 11 3 4 1
  105838. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105839. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105840. +0 0 0 0 0 0 0 0 0
  105841. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105842. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105843. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 1
  105844. +9 0 3 12 1 4 9 0 3 4 0 1 0 0 0 0 0 0
  105845. +0 0 0 0 0 0 28 3 10 99 9 35 156 14 55 182 16 64
  105846. +189 17 66 190 17 67 189 17 66 184 17 65 166 15 58 118 13 41
  105847. +45 4 16 3 0 1 0 0 0 0 0 0 0 0 0 0 0 0
  105848. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105849. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105850. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105851. +0 0 0 0 0 0 0 0 0
  105852. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105853. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105854. +0 0 0 0 0 0 11 1 4 52 5 18 101 9 35 134 12 47
  105855. +151 14 53 154 14 54 151 14 53 113 10 40 11 1 4 0 0 0
  105856. +3 0 1 67 6 24 159 14 56 190 17 67 190 17 67 188 17 66
  105857. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 191 17 67
  105858. +174 16 61 101 9 35 14 1 5 0 0 0 35 3 12 108 10 38
  105859. +122 11 43 122 11 43 112 10 39 87 8 30 50 5 17 13 1 5
  105860. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105861. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105862. +0 0 0 0 0 0 0 0 0
  105863. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105864. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105865. +3 0 1 56 5 19 141 13 49 182 16 64 191 17 67 191 17 67
  105866. +190 17 67 190 17 67 191 17 67 113 10 40 3 0 1 1 0 0
  105867. +79 7 28 180 16 63 190 17 67 188 17 66 188 17 66 188 17 66
  105868. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105869. +189 17 66 188 17 66 122 11 43 11 1 4 41 4 14 176 16 62
  105870. +191 17 67 191 17 67 191 17 67 190 17 67 181 16 63 146 13 51
  105871. +75 7 26 10 1 4 0 0 0 0 0 0 0 0 0 0 0 0
  105872. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105873. +0 0 0 0 0 0 0 0 0
  105874. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105875. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 1 2
  105876. +90 8 32 178 16 62 191 17 67 188 17 66 188 17 66 188 17 66
  105877. +188 17 66 190 17 67 141 13 49 22 2 8 0 0 0 41 4 14
  105878. +173 16 61 190 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  105879. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105880. +188 17 66 188 17 66 188 17 66 88 8 31 1 0 0 89 8 31
  105881. +185 17 65 189 17 66 188 17 66 188 17 66 189 17 66 191 17 67
  105882. +186 17 65 124 11 43 25 2 9 0 0 0 0 0 0 0 0 0
  105883. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105884. +0 0 0 0 0 0 0 0 0
  105885. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105886. +0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 89 8 31
  105887. +184 17 65 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105888. +190 17 67 151 14 53 34 3 12 0 0 0 0 0 0 79 7 28
  105889. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105890. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105891. +188 17 66 188 17 66 191 17 67 146 13 51 9 1 3 7 1 2
  105892. +108 10 38 187 17 66 189 17 66 188 17 66 188 17 66 188 17 66
  105893. +188 17 66 190 17 67 141 13 49 22 2 8 0 0 0 0 0 0
  105894. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105895. +0 0 0 0 0 0 0 0 0
  105896. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105897. +0 0 0 0 0 0 0 0 0 0 0 0 52 5 18 176 16 62
  105898. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  105899. +151 14 53 38 3 13 0 0 0 0 0 0 0 0 0 50 5 17
  105900. +180 16 63 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105901. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105902. +188 17 66 188 17 66 191 17 67 141 13 49 7 1 3 0 0 0
  105903. +11 1 4 112 10 39 187 17 66 189 17 66 188 17 66 188 17 66
  105904. +188 17 66 188 17 66 190 17 67 113 10 40 5 0 2 0 0 0
  105905. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105906. +0 0 0 0 0 0 0 0 0
  105907. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105908. +0 0 0 0 0 0 0 0 0 7 1 3 132 12 46 191 17 67
  105909. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 146 13 51
  105910. +35 3 12 0 0 0 0 0 0 0 0 0 0 0 0 5 0 2
  105911. +101 9 35 185 17 65 190 17 67 188 17 66 188 17 66 188 17 66
  105912. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105913. +188 17 66 190 17 67 180 16 63 67 6 24 0 0 0 0 0 0
  105914. +0 0 0 11 1 4 108 10 38 186 17 65 189 17 66 188 17 66
  105915. +188 17 66 188 17 66 189 17 66 180 16 63 56 5 19 0 0 0
  105916. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105917. +0 0 0 0 0 0 0 0 0
  105918. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105919. +0 0 0 0 0 0 0 0 0 44 4 15 177 16 62 189 17 66
  105920. +188 17 66 188 17 66 189 17 66 189 17 66 134 12 47 28 3 10
  105921. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105922. +8 1 3 79 7 28 159 14 56 188 17 66 191 17 67 190 17 67
  105923. +189 17 66 189 17 66 189 17 66 189 17 66 190 17 67 191 17 67
  105924. +188 17 66 158 14 55 72 7 25 4 0 1 0 0 0 0 0 0
  105925. +0 0 0 0 0 0 8 1 3 95 9 33 182 16 64 189 17 67
  105926. +188 17 66 188 17 66 188 17 66 191 17 67 122 11 43 3 0 1
  105927. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105928. +0 0 0 0 0 0 0 0 0
  105929. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105930. +0 0 0 0 0 0 0 0 0 88 8 31 190 17 67 188 17 66
  105931. +188 17 66 189 17 66 185 17 65 113 10 40 18 2 6 0 0 0
  105932. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105933. +0 0 0 1 0 0 24 2 8 77 7 27 124 11 43 154 14 54
  105934. +168 15 59 173 16 61 173 16 61 168 15 59 154 14 54 124 11 43
  105935. +77 7 27 22 2 8 0 0 0 0 0 0 0 0 0 0 0 0
  105936. +0 0 0 0 0 0 0 0 0 5 0 2 77 7 27 173 16 61
  105937. +190 17 67 188 17 66 188 17 66 190 17 67 164 15 57 23 2 8
  105938. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105939. +0 0 0 0 0 0 0 0 0
  105940. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105941. +0 0 0 0 0 0 1 0 0 118 13 41 191 17 67 188 17 66
  105942. +190 17 67 174 16 61 87 8 30 8 1 3 0 0 0 0 0 0
  105943. +0 0 0 0 0 0 10 1 4 29 3 10 40 4 14 36 3 13
  105944. +18 2 6 2 0 1 0 0 0 0 0 0 3 0 1 14 1 5
  105945. +26 2 9 33 3 11 32 3 11 25 2 9 13 1 5 3 0 1
  105946. +0 0 0 14 1 5 56 5 19 95 9 33 109 10 38 101 9 35
  105947. +77 7 27 35 3 12 5 0 2 0 0 0 1 0 0 56 5 19
  105948. +156 14 55 190 17 67 188 17 66 188 17 66 182 16 64 50 5 17
  105949. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105950. +0 0 0 0 0 0 0 0 0
  105951. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105952. +0 0 0 0 0 0 5 0 2 134 12 47 191 17 67 189 17 66
  105953. +151 14 53 52 5 18 2 0 1 0 0 0 0 0 0 1 0 0
  105954. +28 3 10 90 8 32 146 13 51 170 15 60 178 16 62 174 16 61
  105955. +158 14 55 112 10 39 40 4 14 1 0 0 0 0 0 0 0 0
  105956. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 1
  105957. +56 5 19 146 13 51 183 17 64 191 17 67 191 17 67 191 17 67
  105958. +188 17 66 173 16 61 122 11 43 41 4 14 1 0 0 0 0 0
  105959. +30 3 10 124 11 43 185 17 65 190 17 67 187 17 66 67 6 24
  105960. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105961. +0 0 0 0 0 0 0 0 0
  105962. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105963. +0 0 0 0 0 0 6 1 2 134 12 47 168 15 59 99 9 35
  105964. +21 2 7 0 0 0 0 0 0 0 0 0 6 1 2 77 7 27
  105965. +162 15 57 190 17 67 191 17 67 189 17 66 189 17 66 189 17 66
  105966. +190 17 67 191 17 67 169 15 59 75 7 26 3 0 1 0 0 0
  105967. +0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 79 7 28
  105968. +178 16 62 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  105969. +188 17 66 189 17 66 191 17 67 170 15 60 79 7 28 5 0 2
  105970. +0 0 0 10 1 3 78 7 27 159 14 56 188 17 66 75 7 26
  105971. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105972. +0 0 0 0 0 0 0 0 0
  105973. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105974. +0 0 0 0 0 0 1 0 0 35 3 12 29 3 10 2 0 1
  105975. +0 0 0 0 0 0 0 0 0 9 1 3 101 9 35 183 17 64
  105976. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105977. +188 17 66 188 17 66 190 17 67 178 16 63 67 6 23 0 0 0
  105978. +0 0 0 0 0 0 0 0 0 0 0 0 52 5 18 174 16 61
  105979. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105980. +188 17 66 188 17 66 188 17 66 190 17 67 182 16 64 89 8 31
  105981. +4 0 1 0 0 0 0 0 0 25 2 9 73 7 26 31 3 11
  105982. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105983. +0 0 0 0 0 0 0 0 0
  105984. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105985. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105986. +0 0 0 0 0 0 4 0 1 98 9 34 187 17 66 189 17 66
  105987. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105988. +188 17 66 188 17 66 188 17 66 190 17 67 158 14 55 25 2 9
  105989. +0 0 0 0 0 0 0 0 0 8 1 3 134 12 47 191 17 67
  105990. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105991. +188 17 66 188 17 66 188 17 66 188 17 66 189 17 66 180 16 63
  105992. +68 6 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105993. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105994. +0 0 0 0 0 0 0 0 0
  105995. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105996. +0 0 0 6 1 2 19 2 7 3 0 1 0 0 0 0 0 0
  105997. +0 0 0 0 0 0 65 6 23 180 16 63 189 17 66 188 17 66
  105998. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105999. +188 17 66 188 17 66 188 17 66 188 17 66 189 17 66 83 8 29
  106000. +0 0 0 0 0 0 0 0 0 41 4 14 177 16 62 189 17 66
  106001. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106002. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  106003. +159 14 56 28 3 10 0 0 0 0 0 0 0 0 0 23 2 8
  106004. +41 4 14 5 0 2 0 0 0 0 0 0 0 0 0 0 0 0
  106005. +0 0 0 0 0 0 0 0 0
  106006. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106007. +23 2 8 113 10 40 159 14 56 65 6 23 0 0 0 0 0 0
  106008. +0 0 0 16 1 6 146 13 51 191 17 67 188 17 66 188 17 66
  106009. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106010. +188 17 66 188 17 66 188 17 66 188 17 66 191 17 67 132 12 46
  106011. +5 0 2 0 0 0 0 0 0 77 7 27 189 17 66 188 17 66
  106012. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106013. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106014. +190 17 67 98 9 34 0 0 0 0 0 0 12 1 4 134 12 47
  106015. +178 16 63 108 10 38 16 1 6 0 0 0 0 0 0 0 0 0
  106016. +0 0 0 0 0 0 0 0 0
  106017. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30 3 10
  106018. +141 13 49 190 17 67 191 17 67 134 12 47 6 1 2 0 0 0
  106019. +0 0 0 68 6 24 186 17 65 188 17 66 188 17 66 188 17 66
  106020. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106021. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 156 14 55
  106022. +14 1 5 0 0 0 0 0 0 98 9 34 191 17 67 188 17 66
  106023. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106024. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106025. +190 17 67 156 14 55 19 2 7 0 0 0 47 4 16 181 16 63
  106026. +190 17 67 189 17 66 126 14 44 17 2 6 0 0 0 0 0 0
  106027. +0 0 0 0 0 0 0 0 0
  106028. +0 0 0 0 0 0 0 0 0 0 0 0 16 1 6 134 12 47
  106029. +191 17 67 188 17 66 190 17 67 162 15 57 19 2 7 0 0 0
  106030. +3 0 1 123 11 43 191 17 67 188 17 66 188 17 66 188 17 66
  106031. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106032. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 163 15 57
  106033. +20 2 7 0 0 0 0 0 0 101 9 35 191 17 67 188 17 66
  106034. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106035. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106036. +188 17 66 182 16 64 52 5 18 0 0 0 73 7 26 188 17 66
  106037. +188 17 66 188 17 66 189 17 66 109 10 38 5 0 2 0 0 0
  106038. +0 0 0 0 0 0 0 0 0
  106039. +0 0 0 0 0 0 0 0 0 0 0 0 95 9 33 189 17 66
  106040. +188 17 66 188 17 66 189 17 66 171 15 60 29 3 10 0 0 0
  106041. +16 1 6 156 14 55 190 17 67 188 17 66 188 17 66 188 17 66
  106042. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106043. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 158 14 55
  106044. +17 2 6 0 0 0 0 0 0 85 8 30 190 17 67 188 17 66
  106045. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106046. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106047. +188 17 66 189 17 66 81 7 29 0 0 0 85 8 30 190 17 67
  106048. +188 17 66 188 17 66 189 17 66 180 16 63 56 5 19 0 0 0
  106049. +0 0 0 0 0 0 0 0 0
  106050. +0 0 0 0 0 0 0 0 0 25 2 9 162 15 57 190 17 67
  106051. +188 17 66 188 17 66 189 17 66 173 16 61 31 3 11 0 0 0
  106052. +30 3 10 171 15 60 189 17 66 188 17 66 188 17 66 188 17 66
  106053. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106054. +188 17 66 188 17 66 188 17 66 188 17 66 191 17 67 141 13 49
  106055. +7 1 2 0 0 0 0 0 0 56 5 19 183 17 64 188 17 66
  106056. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106057. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106058. +188 17 66 191 17 67 98 9 34 0 0 0 88 8 31 190 17 67
  106059. +188 17 66 188 17 66 188 17 66 191 17 67 124 11 43 5 0 2
  106060. +0 0 0 0 0 0 0 0 0
  106061. +0 0 0 0 0 0 0 0 0 68 6 24 187 17 66 188 17 66
  106062. +188 17 66 188 17 66 189 17 66 170 15 60 28 3 10 0 0 0
  106063. +34 3 12 174 16 61 189 17 66 188 17 66 188 17 66 188 17 66
  106064. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106065. +188 17 66 188 17 66 188 17 66 188 17 66 191 17 67 101 9 35
  106066. +0 0 0 0 0 0 0 0 0 21 2 7 159 14 56 190 17 67
  106067. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106068. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106069. +188 17 66 191 17 67 98 9 34 0 0 0 81 7 29 189 17 66
  106070. +188 17 66 188 17 66 188 17 66 189 17 66 168 15 59 28 3 10
  106071. +0 0 0 0 0 0 0 0 0
  106072. +0 0 0 0 0 0 0 0 0 109 10 38 191 17 67 188 17 66
  106073. +188 17 66 188 17 66 190 17 67 163 15 57 21 2 7 0 0 0
  106074. +26 2 9 168 15 59 189 17 66 188 17 66 188 17 66 188 17 66
  106075. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106076. +188 17 66 188 17 66 188 17 66 189 17 66 180 16 63 47 4 16
  106077. +0 0 0 0 0 0 0 0 0 0 0 0 108 10 38 190 17 67
  106078. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106079. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106080. +188 17 66 189 17 66 78 7 27 0 0 0 68 6 24 187 17 66
  106081. +188 17 66 188 17 66 188 17 66 188 17 66 183 17 64 56 5 19
  106082. +0 0 0 0 0 0 0 0 0
  106083. +0 0 0 0 0 0 3 0 1 131 12 46 191 17 67 188 17 66
  106084. +188 17 66 188 17 66 190 17 67 151 14 53 12 1 4 0 0 0
  106085. +11 1 4 146 13 51 190 17 67 188 17 66 188 17 66 188 17 66
  106086. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106087. +188 17 66 188 17 66 188 17 66 191 17 67 126 14 44 7 1 2
  106088. +0 0 0 0 0 0 0 0 0 0 0 0 32 3 11 164 15 58
  106089. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106090. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106091. +189 17 66 178 16 62 44 4 15 0 0 0 50 5 17 182 16 64
  106092. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 72 7 25
  106093. +0 0 0 0 0 0 0 0 0
  106094. +0 0 0 0 0 0 5 0 2 134 12 47 191 17 67 188 17 66
  106095. +188 17 66 188 17 66 191 17 67 131 12 46 3 0 1 0 0 0
  106096. +0 0 0 101 9 35 190 17 67 188 17 66 188 17 66 188 17 66
  106097. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106098. +188 17 66 188 17 66 190 17 67 170 15 60 44 4 15 0 0 0
  106099. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 77 7 27
  106100. +183 17 64 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106101. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106102. +191 17 67 134 12 47 9 1 3 0 0 0 31 3 11 171 15 60
  106103. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 72 7 25
  106104. +0 0 0 0 0 0 0 0 0
  106105. +0 0 0 0 0 0 2 0 1 124 11 43 191 17 67 188 17 66
  106106. +188 17 66 188 17 66 191 17 67 101 9 35 0 0 0 0 0 0
  106107. +0 0 0 35 3 12 168 15 59 190 17 67 188 17 66 188 17 66
  106108. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106109. +188 17 66 189 17 66 182 16 64 77 7 27 0 0 0 0 0 0
  106110. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 1 2
  106111. +99 9 35 185 17 65 189 17 66 188 17 66 188 17 66 188 17 66
  106112. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 189 17 66
  106113. +177 16 62 56 5 19 0 0 0 0 0 0 13 1 5 151 14 53
  106114. +190 17 67 188 17 66 188 17 66 188 17 66 185 17 65 56 5 19
  106115. +0 0 0 0 0 0 0 0 0
  106116. +0 0 0 0 0 0 0 0 0 99 9 35 191 17 67 188 17 66
  106117. +188 17 66 188 17 66 186 17 65 65 6 23 0 0 0 0 0 0
  106118. +0 0 0 0 0 0 79 7 28 182 16 64 190 17 67 188 17 66
  106119. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106120. +191 17 67 177 16 62 83 8 29 4 0 1 0 0 0 0 0 0
  106121. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106122. +8 1 3 89 8 31 175 16 62 191 17 67 189 17 66 188 17 66
  106123. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 181 16 63
  106124. +85 8 30 3 0 1 0 0 0 0 0 0 1 0 0 118 13 41
  106125. +191 17 67 188 17 66 188 17 66 189 17 66 173 16 61 34 3 12
  106126. +0 0 0 0 0 0 0 0 0
  106127. +0 0 0 0 0 0 0 0 0 56 5 19 183 17 64 188 17 66
  106128. +188 17 66 189 17 66 169 15 59 30 3 10 0 0 0 0 0 0
  106129. +0 0 0 0 0 0 5 0 2 83 8 29 173 16 61 191 17 67
  106130. +190 17 67 189 17 66 189 17 66 190 17 67 191 17 67 187 17 66
  106131. +151 14 53 56 5 19 3 0 1 0 0 0 16 1 6 50 5 17
  106132. +79 7 28 95 9 33 95 9 33 75 7 26 41 4 14 10 1 4
  106133. +0 0 0 2 0 1 50 5 17 132 12 46 178 16 62 190 17 67
  106134. +191 17 67 191 17 67 191 17 67 186 17 65 154 14 54 68 6 24
  106135. +4 0 1 0 0 0 0 0 0 0 0 0 0 0 0 72 7 25
  106136. +187 17 66 188 17 66 188 17 66 191 17 67 141 13 49 9 1 3
  106137. +0 0 0 0 0 0 0 0 0
  106138. +0 0 0 0 0 0 0 0 0 14 1 5 151 14 53 190 17 67
  106139. +188 17 66 191 17 67 131 12 46 5 0 2 0 0 0 0 0 0
  106140. +0 0 0 0 0 0 0 0 0 2 0 1 44 4 15 113 10 40
  106141. +156 14 55 173 16 61 174 16 61 164 15 58 134 12 47 77 7 27
  106142. +18 2 6 0 0 0 16 1 6 85 8 30 151 14 53 182 16 64
  106143. +189 17 66 191 17 67 190 17 67 188 17 66 177 16 62 141 13 49
  106144. +68 6 24 8 1 3 0 0 0 8 1 3 44 4 15 88 8 31
  106145. +113 10 40 122 11 43 108 10 38 67 6 24 20 2 7 0 0 0
  106146. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 28 3 10
  106147. +166 15 58 190 17 67 188 17 66 187 17 66 79 7 28 0 0 0
  106148. +0 0 0 0 0 0 0 0 0
  106149. +0 0 0 0 0 0 0 0 0 0 0 0 73 7 26 185 17 65
  106150. +189 17 66 184 17 65 65 6 23 0 0 0 0 0 0 0 0 0
  106151. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 1
  106152. +17 2 6 32 3 11 34 3 12 22 2 8 6 1 2 0 0 0
  106153. +0 0 0 38 3 13 141 13 49 188 17 66 190 17 67 188 17 66
  106154. +188 17 66 188 17 66 188 17 66 188 17 66 189 17 66 191 17 67
  106155. +184 17 65 122 11 43 21 2 7 0 0 0 0 0 0 0 0 0
  106156. +0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106157. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
  106158. +108 10 38 191 17 67 191 17 67 141 13 49 16 1 6 0 0 0
  106159. +0 0 0 0 0 0 0 0 0
  106160. +0 0 0 0 0 0 0 0 0 0 0 0 8 1 3 112 10 39
  106161. +186 17 65 124 11 43 10 1 4 0 0 0 0 0 0 0 0 0
  106162. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106163. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106164. +36 3 13 156 14 55 191 17 67 188 17 66 188 17 66 188 17 66
  106165. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106166. +189 17 66 190 17 67 134 12 47 18 2 6 0 0 0 0 0 0
  106167. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106168. +0 0 0 7 1 2 41 4 14 75 7 26 66 5 23 19 2 7
  106169. +26 2 9 144 13 50 154 14 54 40 4 14 0 0 0 0 0 0
  106170. +0 0 0 0 0 0 0 0 0
  106171. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 1 5
  106172. +56 5 19 19 2 7 0 0 0 7 1 2 29 3 10 35 3 12
  106173. +19 2 7 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0
  106174. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 1 5
  106175. +134 12 47 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  106176. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106177. +188 17 66 188 17 66 189 17 67 108 10 38 3 0 1 0 0 0
  106178. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
  106179. +40 4 14 124 11 43 177 16 62 188 17 66 187 17 66 144 13 50
  106180. +24 2 8 17 2 6 22 2 8 0 0 0 0 0 0 0 0 0
  106181. +0 0 0 0 0 0 0 0 0
  106182. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106183. +0 0 0 0 0 0 19 2 7 122 11 43 171 15 60 175 16 62
  106184. +159 14 56 112 10 39 40 4 14 2 0 1 0 0 0 0 0 0
  106185. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 72 7 25
  106186. +186 17 65 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106187. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106188. +188 17 66 188 17 66 189 17 66 174 16 61 41 4 14 0 0 0
  106189. +0 0 0 0 0 0 0 0 0 0 0 0 3 0 1 72 7 25
  106190. +168 15 59 191 17 67 189 17 66 188 17 66 188 17 66 190 17 67
  106191. +95 9 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106192. +0 0 0 0 0 0 0 0 0
  106193. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106194. +0 0 0 0 0 0 95 9 33 191 17 67 189 17 66 189 17 66
  106195. +190 17 67 191 17 67 171 15 60 90 8 32 12 1 4 0 0 0
  106196. +0 0 0 0 0 0 0 0 0 0 0 0 5 0 2 132 12 46
  106197. +191 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106198. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106199. +188 17 66 188 17 66 188 17 66 190 17 67 98 9 34 0 0 0
  106200. +0 0 0 0 0 0 0 0 0 5 0 2 88 8 31 180 16 63
  106201. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 191 17 67
  106202. +146 13 51 11 1 4 0 0 0 0 0 0 0 0 0 0 0 0
  106203. +0 0 0 0 0 0 0 0 0
  106204. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106205. +0 0 0 9 1 3 144 13 50 191 17 67 188 17 66 188 17 66
  106206. +188 17 66 188 17 66 189 17 66 187 17 66 123 11 43 20 2 7
  106207. +0 0 0 0 0 0 0 0 0 0 0 0 21 2 7 163 15 57
  106208. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106209. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106210. +188 17 66 188 17 66 188 17 66 191 17 67 134 12 47 5 0 2
  106211. +0 0 0 0 0 0 3 0 1 88 8 31 182 16 64 189 17 66
  106212. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 189 17 66
  106213. +171 15 60 31 3 11 0 0 0 0 0 0 0 0 0 0 0 0
  106214. +0 0 0 0 0 0 0 0 0
  106215. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106216. +0 0 0 20 2 7 162 15 57 190 17 67 188 17 66 188 17 66
  106217. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 132 12 46
  106218. +20 2 7 0 0 0 0 0 0 0 0 0 32 3 11 173 16 61
  106219. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106220. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106221. +188 17 66 188 17 66 188 17 66 190 17 67 151 14 53 12 1 4
  106222. +0 0 0 0 0 0 72 7 25 180 16 63 189 17 66 188 17 66
  106223. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106224. +181 16 63 47 4 16 0 0 0 0 0 0 0 0 0 0 0 0
  106225. +0 0 0 0 0 0 0 0 0
  106226. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106227. +0 0 0 21 2 7 163 15 57 190 17 67 188 17 66 188 17 66
  106228. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  106229. +122 11 43 9 1 3 0 0 0 0 0 0 30 3 10 171 15 60
  106230. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106231. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106232. +188 17 66 188 17 66 188 17 66 190 17 67 146 13 51 10 1 4
  106233. +0 0 0 38 3 13 166 15 58 190 17 67 188 17 66 188 17 66
  106234. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106235. +183 17 64 52 5 18 0 0 0 0 0 0 0 0 0 0 0 0
  106236. +0 0 0 0 0 0 0 0 0
  106237. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106238. +0 0 0 13 1 5 154 14 54 190 17 67 188 17 66 188 17 66
  106239. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106240. +186 17 65 79 7 28 0 0 0 0 0 0 14 1 5 156 14 54
  106241. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106242. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106243. +188 17 66 188 17 66 188 17 66 191 17 67 124 11 43 2 0 1
  106244. +5 0 2 122 11 43 191 17 67 188 17 66 188 17 66 188 17 66
  106245. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106246. +182 16 64 47 4 16 0 0 0 0 0 0 0 0 0 0 0 0
  106247. +0 0 0 0 0 0 0 0 0
  106248. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106249. +0 0 0 3 0 1 126 14 44 191 17 67 188 17 66 188 17 66
  106250. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106251. +190 17 67 158 14 55 23 2 8 0 0 0 1 0 0 113 10 40
  106252. +191 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106253. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106254. +188 17 66 188 17 66 188 17 66 188 17 66 78 7 27 0 0 0
  106255. +47 4 16 177 16 62 189 17 66 188 17 66 188 17 66 188 17 66
  106256. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 189 17 66
  106257. +173 16 61 34 3 12 0 0 0 0 0 0 0 0 0 0 0 0
  106258. +0 0 0 0 0 0 0 0 0
  106259. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106260. +0 0 0 0 0 0 85 8 30 189 17 66 188 17 66 188 17 66
  106261. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106262. +188 17 66 188 17 66 79 7 28 0 0 0 0 0 0 47 4 16
  106263. +175 16 62 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106264. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106265. +188 17 66 188 17 66 190 17 67 156 14 55 22 2 8 0 0 0
  106266. +109 10 38 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  106267. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  106268. +151 14 53 13 1 5 0 0 0 0 0 0 0 0 0 0 0 0
  106269. +0 0 0 0 0 0 0 0 0
  106270. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106271. +0 0 0 0 0 0 35 3 12 173 16 61 189 17 66 188 17 66
  106272. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106273. +188 17 66 191 17 67 134 12 47 7 1 2 0 0 0 3 0 1
  106274. +99 9 35 188 17 66 189 17 66 188 17 66 188 17 66 188 17 66
  106275. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106276. +188 17 66 189 17 66 181 16 63 68 6 24 0 0 0 18 2 6
  106277. +156 14 55 190 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  106278. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  106279. +101 9 35 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106280. +0 0 0 0 0 0 0 0 0
  106281. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106282. +0 0 0 0 0 0 3 0 1 118 13 41 191 17 67 188 17 66
  106283. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106284. +188 17 66 189 17 66 168 15 59 28 3 10 0 0 0 0 0 0
  106285. +12 1 4 113 10 40 187 17 66 189 17 67 188 17 66 188 17 66
  106286. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106287. +190 17 67 180 16 63 88 8 31 4 0 1 0 0 0 47 4 16
  106288. +180 16 63 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106289. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 168 15 59
  106290. +36 3 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106291. +0 0 0 0 0 0 0 0 0
  106292. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106293. +0 0 0 0 0 0 0 0 0 38 3 13 164 15 58 190 17 67
  106294. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106295. +188 17 66 188 17 66 182 16 64 50 5 17 0 0 0 0 0 0
  106296. +0 0 0 11 1 4 90 8 32 169 15 59 190 17 67 190 17 67
  106297. +189 17 66 189 17 66 189 17 66 189 17 66 191 17 67 189 17 66
  106298. +158 14 55 68 6 24 4 0 1 0 0 0 0 0 0 73 7 26
  106299. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106300. +188 17 66 188 17 66 188 17 66 189 17 66 185 17 65 83 8 29
  106301. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106302. +0 0 0 0 0 0 0 0 0
  106303. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106304. +0 0 0 0 0 0 0 0 0 0 0 0 65 6 23 174 16 61
  106305. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106306. +188 17 66 188 17 66 185 17 65 56 5 19 0 0 0 0 0 0
  106307. +0 0 0 0 0 0 2 0 1 35 3 12 99 9 35 146 13 51
  106308. +170 15 60 177 16 62 177 16 62 166 15 58 141 13 49 85 8 30
  106309. +24 2 8 0 0 0 0 0 0 0 0 0 0 0 0 85 8 30
  106310. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106311. +188 17 66 188 17 66 188 17 66 189 17 66 112 10 39 8 1 3
  106312. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106313. +0 0 0 0 0 0 0 0 0
  106314. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106315. +0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 68 6 24
  106316. +170 15 60 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  106317. +188 17 66 188 17 66 182 16 64 50 5 17 0 0 0 0 0 0
  106318. +0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 11 1 4
  106319. +28 3 10 40 4 14 38 3 13 25 2 9 8 1 3 0 0 0
  106320. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 78 7 27
  106321. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106322. +188 17 66 189 17 66 187 17 66 113 10 40 14 1 5 0 0 0
  106323. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106324. +0 0 0 0 0 0 0 0 0
  106325. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106326. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
  106327. +47 4 16 141 13 49 186 17 65 191 17 67 190 17 67 189 17 66
  106328. +189 17 66 191 17 67 156 14 55 20 2 7 0 0 0 0 0 0
  106329. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106330. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106331. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 44 4 15
  106332. +178 16 62 190 17 67 188 17 66 188 17 66 188 17 66 190 17 67
  106333. +191 17 67 173 16 61 90 8 32 10 1 4 0 0 0 0 0 0
  106334. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106335. +0 0 0 0 0 0 0 0 0
  106336. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106337. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106338. +0 0 0 14 1 5 68 6 24 131 12 46 162 15 57 174 16 61
  106339. +171 15 60 146 13 51 56 5 19 0 0 0 0 0 0 0 0 0
  106340. +0 0 0 0 0 0 0 0 0 3 0 1 14 1 5 29 3 10
  106341. +41 4 14 47 4 16 50 5 17 45 4 16 34 3 12 18 2 6
  106342. +5 0 2 0 0 0 0 0 0 0 0 0 0 0 0 5 0 2
  106343. +90 8 32 169 15 59 185 17 65 187 17 66 182 16 64 163 15 57
  106344. +113 10 40 41 4 14 2 0 1 0 0 0 0 0 0 0 0 0
  106345. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106346. +0 0 0 0 0 0 0 0 0
  106347. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106348. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106349. +0 0 0 0 0 0 0 0 0 5 0 2 21 2 7 34 3 12
  106350. +29 3 10 11 1 4 0 0 0 0 0 0 0 0 0 0 0 0
  106351. +3 0 1 32 3 11 79 7 28 124 11 43 154 14 54 171 15 60
  106352. +180 16 63 182 16 64 182 16 64 180 16 63 174 16 61 159 14 56
  106353. +132 12 46 88 8 31 34 3 12 3 0 1 0 0 0 0 0 0
  106354. +3 0 1 29 3 10 56 5 19 65 6 23 50 5 17 23 2 8
  106355. +3 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106356. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106357. +0 0 0 0 0 0 0 0 0
  106358. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106359. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106360. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106361. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 25 2 9
  106362. +109 10 38 169 15 59 189 17 66 191 17 67 190 17 67 189 17 66
  106363. +189 17 66 188 17 66 188 17 66 188 17 66 189 17 66 190 17 67
  106364. +191 17 67 190 17 67 171 15 60 98 9 34 10 1 3 0 0 0
  106365. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106366. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106367. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106368. +0 0 0 0 0 0 0 0 0
  106369. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106370. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106371. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106372. +0 0 0 0 0 0 0 0 0 0 0 0 14 1 5 141 13 49
  106373. +191 17 67 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106374. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106375. +188 17 66 188 17 66 189 17 67 186 17 65 65 6 23 0 0 0
  106376. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106377. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106378. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106379. +0 0 0 0 0 0 0 0 0
  106380. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106381. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106382. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106383. +0 0 0 0 0 0 0 0 0 0 0 0 23 2 8 166 15 58
  106384. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106385. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106386. +188 17 66 188 17 66 189 17 66 176 16 62 45 4 16 0 0 0
  106387. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106388. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106389. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106390. +0 0 0 0 0 0 0 0 0
  106391. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106392. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106393. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106394. +0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 83 8 29
  106395. +183 17 64 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106396. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106397. +188 17 66 189 17 66 185 17 65 95 9 33 3 0 1 0 0 0
  106398. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106399. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106400. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106401. +0 0 0 0 0 0 0 0 0
  106402. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106403. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106404. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106405. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 2
  106406. +85 8 30 176 16 62 191 17 67 188 17 66 188 17 66 188 17 66
  106407. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  106408. +191 17 67 180 16 63 95 9 33 7 1 3 0 0 0 0 0 0
  106409. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106410. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106411. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106412. +0 0 0 0 0 0 0 0 0
  106413. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106414. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106415. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106416. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106417. +2 0 1 52 5 18 141 13 49 185 17 65 191 17 67 189 17 67
  106418. +189 17 66 188 17 66 188 17 66 189 17 66 191 17 67 187 17 66
  106419. +146 13 51 56 5 19 4 0 1 0 0 0 0 0 0 0 0 0
  106420. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106421. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106422. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106423. +0 0 0 0 0 0 0 0 0
  106424. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106425. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106426. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106427. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106428. +0 0 0 0 0 0 14 1 5 68 6 24 131 12 46 166 15 58
  106429. +180 16 63 183 17 64 180 16 63 168 15 59 134 12 47 75 7 26
  106430. +17 2 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106431. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106432. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106433. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106434. +0 0 0 0 0 0 0 0 0
  106435. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106436. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106437. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106438. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106439. +0 0 0 0 0 0 0 0 0 0 0 0 5 0 2 24 2 8
  106440. +44 4 15 52 5 18 45 4 16 26 2 9 6 1 2 0 0 0
  106441. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106442. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106443. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106444. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106445. +0 0 0 0 0 0 0 0 0
  106446. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106447. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106448. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106449. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106450. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106451. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106452. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106453. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106454. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106455. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106456. +0 0 0 0 0 0 0 0 0
  106457. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106458. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106459. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106460. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106461. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106462. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106463. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106464. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106465. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106466. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  106467. +0 0 0 0 0 0 0 0 0
  106468. diff -Nur linux-3.12.33/drivers/video/Makefile linux-3.12.33-rpi/drivers/video/Makefile
  106469. --- linux-3.12.33/drivers/video/Makefile 2014-11-15 06:28:07.000000000 -0600
  106470. +++ linux-3.12.33-rpi/drivers/video/Makefile 2014-12-03 19:13:40.336418001 -0600
  106471. @@ -100,6 +100,7 @@
  106472. obj-$(CONFIG_FB_VOODOO1) += sstfb.o
  106473. obj-$(CONFIG_FB_ARMCLCD) += amba-clcd.o
  106474. obj-$(CONFIG_FB_GOLDFISH) += goldfishfb.o
  106475. +obj-$(CONFIG_FB_BCM2708) += bcm2708_fb.o
  106476. obj-$(CONFIG_FB_68328) += 68328fb.o
  106477. obj-$(CONFIG_FB_GBE) += gbefb.o
  106478. obj-$(CONFIG_FB_CIRRUS) += cirrusfb.o
  106479. diff -Nur linux-3.12.33/drivers/w1/masters/w1-gpio.c linux-3.12.33-rpi/drivers/w1/masters/w1-gpio.c
  106480. --- linux-3.12.33/drivers/w1/masters/w1-gpio.c 2014-11-15 06:28:07.000000000 -0600
  106481. +++ linux-3.12.33-rpi/drivers/w1/masters/w1-gpio.c 2014-12-03 19:13:42.580418001 -0600
  106482. @@ -22,6 +22,15 @@
  106483. #include "../w1.h"
  106484. #include "../w1_int.h"
  106485. +static int w1_gpio_pullup = -1;
  106486. +static int w1_gpio_pullup_orig = -1;
  106487. +module_param_named(pullup, w1_gpio_pullup, int, 0);
  106488. +MODULE_PARM_DESC(pullup, "GPIO pin pullup number");
  106489. +static int w1_gpio_pin = -1;
  106490. +static int w1_gpio_pin_orig = -1;
  106491. +module_param_named(gpiopin, w1_gpio_pin, int, 0);
  106492. +MODULE_PARM_DESC(gpiopin, "GPIO pin number");
  106493. +
  106494. static void w1_gpio_write_bit_dir(void *data, u8 bit)
  106495. {
  106496. struct w1_gpio_platform_data *pdata = data;
  106497. @@ -46,6 +55,16 @@
  106498. return gpio_get_value(pdata->pin) ? 1 : 0;
  106499. }
  106500. +static void w1_gpio_bitbang_pullup(void *data, u8 on)
  106501. +{
  106502. + struct w1_gpio_platform_data *pdata = data;
  106503. +
  106504. + if (on)
  106505. + gpio_direction_output(pdata->pin, 1);
  106506. + else
  106507. + gpio_direction_input(pdata->pin);
  106508. +}
  106509. +
  106510. #if defined(CONFIG_OF)
  106511. static struct of_device_id w1_gpio_dt_ids[] = {
  106512. { .compatible = "w1-gpio" },
  106513. @@ -76,14 +95,16 @@
  106514. static int w1_gpio_probe(struct platform_device *pdev)
  106515. {
  106516. struct w1_bus_master *master;
  106517. - struct w1_gpio_platform_data *pdata;
  106518. + struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
  106519. int err;
  106520. - if (of_have_populated_dt()) {
  106521. - err = w1_gpio_probe_dt(pdev);
  106522. - if (err < 0) {
  106523. - dev_err(&pdev->dev, "Failed to parse DT\n");
  106524. - return err;
  106525. + if(pdata == NULL) {
  106526. + if (of_have_populated_dt()) {
  106527. + err = w1_gpio_probe_dt(pdev);
  106528. + if (err < 0) {
  106529. + dev_err(&pdev->dev, "Failed to parse DT\n");
  106530. + return err;
  106531. + }
  106532. }
  106533. }
  106534. @@ -100,6 +121,19 @@
  106535. return -ENOMEM;
  106536. }
  106537. + w1_gpio_pin_orig = pdata->pin;
  106538. + w1_gpio_pullup_orig = pdata->ext_pullup_enable_pin;
  106539. +
  106540. + if(gpio_is_valid(w1_gpio_pin)) {
  106541. + pdata->pin = w1_gpio_pin;
  106542. + pdata->ext_pullup_enable_pin = -1;
  106543. + }
  106544. + if(gpio_is_valid(w1_gpio_pullup)) {
  106545. + pdata->ext_pullup_enable_pin = w1_gpio_pullup;
  106546. + }
  106547. +
  106548. + dev_info(&pdev->dev, "gpio pin %d, gpio pullup pin %d\n", pdata->pin, pdata->ext_pullup_enable_pin);
  106549. +
  106550. err = gpio_request(pdata->pin, "w1");
  106551. if (err) {
  106552. dev_err(&pdev->dev, "gpio_request (pin) failed\n");
  106553. @@ -127,6 +161,14 @@
  106554. master->write_bit = w1_gpio_write_bit_dir;
  106555. }
  106556. + if (gpio_is_valid(w1_gpio_pullup)) {
  106557. + if (pdata->is_open_drain)
  106558. + printk(KERN_ERR "w1-gpio 'pullup' option "
  106559. + "doesn't work with open drain GPIO\n");
  106560. + else
  106561. + master->bitbang_pullup = w1_gpio_bitbang_pullup;
  106562. + }
  106563. +
  106564. err = w1_add_master_device(master);
  106565. if (err) {
  106566. dev_err(&pdev->dev, "w1_add_master device failed\n");
  106567. @@ -167,8 +209,14 @@
  106568. w1_remove_master_device(master);
  106569. gpio_free(pdata->pin);
  106570. + if (gpio_is_valid(pdata->ext_pullup_enable_pin))
  106571. + gpio_free(pdata->ext_pullup_enable_pin);
  106572. +
  106573. kfree(master);
  106574. + pdata->pin = w1_gpio_pin_orig;
  106575. + pdata->ext_pullup_enable_pin = w1_gpio_pullup_orig;
  106576. +
  106577. return 0;
  106578. }
  106579. diff -Nur linux-3.12.33/drivers/w1/w1.h linux-3.12.33-rpi/drivers/w1/w1.h
  106580. --- linux-3.12.33/drivers/w1/w1.h 2014-11-15 06:28:07.000000000 -0600
  106581. +++ linux-3.12.33-rpi/drivers/w1/w1.h 2014-12-03 19:13:42.584418001 -0600
  106582. @@ -148,6 +148,12 @@
  106583. */
  106584. u8 (*set_pullup)(void *, int);
  106585. + /**
  106586. + * Turns the pullup on/off in bitbanging mode, takes an on/off argument.
  106587. + * @return -1=Error, 0=completed
  106588. + */
  106589. + void (*bitbang_pullup) (void *, u8);
  106590. +
  106591. /** Really nice hardware can handles the different types of ROM search
  106592. * w1_master* is passed to the slave found callback.
  106593. */
  106594. diff -Nur linux-3.12.33/drivers/w1/w1_int.c linux-3.12.33-rpi/drivers/w1/w1_int.c
  106595. --- linux-3.12.33/drivers/w1/w1_int.c 2014-11-15 06:28:07.000000000 -0600
  106596. +++ linux-3.12.33-rpi/drivers/w1/w1_int.c 2014-12-03 19:13:42.584418001 -0600
  106597. @@ -130,6 +130,20 @@
  106598. master->set_pullup = NULL;
  106599. }
  106600. + /* bitbanging hardware uses bitbang_pullup, other hardware uses set_pullup
  106601. + * and takes care of timing itself */
  106602. + if (!master->write_byte && !master->touch_bit && master->set_pullup) {
  106603. + printk(KERN_ERR "w1_add_master_device: set_pullup requires "
  106604. + "write_byte or touch_bit, disabling\n");
  106605. + master->set_pullup = NULL;
  106606. + }
  106607. +
  106608. + if (master->set_pullup && master->bitbang_pullup) {
  106609. + printk(KERN_ERR "w1_add_master_device: set_pullup should not "
  106610. + "be set when bitbang_pullup is used, disabling\n");
  106611. + master->set_pullup = NULL;
  106612. + }
  106613. +
  106614. /* Lock until the device is added (or not) to w1_masters. */
  106615. mutex_lock(&w1_mlock);
  106616. /* Search for the first available id (starting at 1). */
  106617. diff -Nur linux-3.12.33/drivers/w1/w1_io.c linux-3.12.33-rpi/drivers/w1/w1_io.c
  106618. --- linux-3.12.33/drivers/w1/w1_io.c 2014-11-15 06:28:07.000000000 -0600
  106619. +++ linux-3.12.33-rpi/drivers/w1/w1_io.c 2014-12-03 19:13:42.584418001 -0600
  106620. @@ -127,10 +127,22 @@
  106621. static void w1_post_write(struct w1_master *dev)
  106622. {
  106623. if (dev->pullup_duration) {
  106624. - if (dev->enable_pullup && dev->bus_master->set_pullup)
  106625. - dev->bus_master->set_pullup(dev->bus_master->data, 0);
  106626. - else
  106627. + if (dev->enable_pullup) {
  106628. + if (dev->bus_master->set_pullup) {
  106629. + dev->bus_master->set_pullup(dev->
  106630. + bus_master->data,
  106631. + 0);
  106632. + } else if (dev->bus_master->bitbang_pullup) {
  106633. + dev->bus_master->
  106634. + bitbang_pullup(dev->bus_master->data, 1);
  106635. msleep(dev->pullup_duration);
  106636. + dev->bus_master->
  106637. + bitbang_pullup(dev->bus_master->data, 0);
  106638. + }
  106639. + } else {
  106640. + msleep(dev->pullup_duration);
  106641. + }
  106642. +
  106643. dev->pullup_duration = 0;
  106644. }
  106645. }
  106646. diff -Nur linux-3.12.33/drivers/watchdog/bcm2708_wdog.c linux-3.12.33-rpi/drivers/watchdog/bcm2708_wdog.c
  106647. --- linux-3.12.33/drivers/watchdog/bcm2708_wdog.c 1969-12-31 18:00:00.000000000 -0600
  106648. +++ linux-3.12.33-rpi/drivers/watchdog/bcm2708_wdog.c 2014-12-03 19:13:42.584418001 -0600
  106649. @@ -0,0 +1,384 @@
  106650. +/*
  106651. + * Broadcom BCM2708 watchdog driver.
  106652. + *
  106653. + * (c) Copyright 2010 Broadcom Europe Ltd
  106654. + *
  106655. + * This program is free software; you can redistribute it and/or
  106656. + * modify it under the terms of the GNU General Public License
  106657. + * as published by the Free Software Foundation; either version
  106658. + * 2 of the License, or (at your option) any later version.
  106659. + *
  106660. + * BCM2708 watchdog driver. Loosely based on wdt driver.
  106661. + */
  106662. +
  106663. +#include <linux/interrupt.h>
  106664. +#include <linux/module.h>
  106665. +#include <linux/moduleparam.h>
  106666. +#include <linux/types.h>
  106667. +#include <linux/miscdevice.h>
  106668. +#include <linux/watchdog.h>
  106669. +#include <linux/fs.h>
  106670. +#include <linux/ioport.h>
  106671. +#include <linux/notifier.h>
  106672. +#include <linux/reboot.h>
  106673. +#include <linux/init.h>
  106674. +#include <linux/io.h>
  106675. +#include <linux/uaccess.h>
  106676. +#include <mach/platform.h>
  106677. +
  106678. +#include <asm/system.h>
  106679. +
  106680. +#define SECS_TO_WDOG_TICKS(x) ((x) << 16)
  106681. +#define WDOG_TICKS_TO_SECS(x) ((x) >> 16)
  106682. +
  106683. +static unsigned long wdog_is_open;
  106684. +static uint32_t wdog_ticks; /* Ticks to load into wdog timer */
  106685. +static char expect_close;
  106686. +
  106687. +/*
  106688. + * Module parameters
  106689. + */
  106690. +
  106691. +#define WD_TIMO 10 /* Default heartbeat = 60 seconds */
  106692. +static int heartbeat = WD_TIMO; /* Heartbeat in seconds */
  106693. +
  106694. +module_param(heartbeat, int, 0);
  106695. +MODULE_PARM_DESC(heartbeat,
  106696. + "Watchdog heartbeat in seconds. (0 < heartbeat < 65536, default="
  106697. + __MODULE_STRING(WD_TIMO) ")");
  106698. +
  106699. +static int nowayout = WATCHDOG_NOWAYOUT;
  106700. +module_param(nowayout, int, 0);
  106701. +MODULE_PARM_DESC(nowayout,
  106702. + "Watchdog cannot be stopped once started (default="
  106703. + __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  106704. +
  106705. +static DEFINE_SPINLOCK(wdog_lock);
  106706. +
  106707. +/**
  106708. + * Start the watchdog driver.
  106709. + */
  106710. +
  106711. +static int wdog_start(unsigned long timeout)
  106712. +{
  106713. + uint32_t cur;
  106714. + unsigned long flags;
  106715. + spin_lock_irqsave(&wdog_lock, flags);
  106716. +
  106717. + /* enable the watchdog */
  106718. + iowrite32(PM_PASSWORD | (timeout & PM_WDOG_TIME_SET),
  106719. + __io_address(PM_WDOG));
  106720. + cur = ioread32(__io_address(PM_RSTC));
  106721. + iowrite32(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
  106722. + PM_RSTC_WRCFG_FULL_RESET, __io_address(PM_RSTC));
  106723. +
  106724. + spin_unlock_irqrestore(&wdog_lock, flags);
  106725. + return 0;
  106726. +}
  106727. +
  106728. +/**
  106729. + * Stop the watchdog driver.
  106730. + */
  106731. +
  106732. +static int wdog_stop(void)
  106733. +{
  106734. + iowrite32(PM_PASSWORD | PM_RSTC_RESET, __io_address(PM_RSTC));
  106735. + printk(KERN_INFO "watchdog stopped\n");
  106736. + return 0;
  106737. +}
  106738. +
  106739. +/**
  106740. + * Reload counter one with the watchdog heartbeat. We don't bother
  106741. + * reloading the cascade counter.
  106742. + */
  106743. +
  106744. +static void wdog_ping(void)
  106745. +{
  106746. + wdog_start(wdog_ticks);
  106747. +}
  106748. +
  106749. +/**
  106750. + * @t: the new heartbeat value that needs to be set.
  106751. + *
  106752. + * Set a new heartbeat value for the watchdog device. If the heartbeat
  106753. + * value is incorrect we keep the old value and return -EINVAL. If
  106754. + * successful we return 0.
  106755. + */
  106756. +
  106757. +static int wdog_set_heartbeat(int t)
  106758. +{
  106759. + if (t < 1 || t > WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET))
  106760. + return -EINVAL;
  106761. +
  106762. + heartbeat = t;
  106763. + wdog_ticks = SECS_TO_WDOG_TICKS(t);
  106764. + return 0;
  106765. +}
  106766. +
  106767. +/**
  106768. + * @file: file handle to the watchdog
  106769. + * @buf: buffer to write (unused as data does not matter here
  106770. + * @count: count of bytes
  106771. + * @ppos: pointer to the position to write. No seeks allowed
  106772. + *
  106773. + * A write to a watchdog device is defined as a keepalive signal.
  106774. + *
  106775. + * if 'nowayout' is set then normally a close() is ignored. But
  106776. + * if you write 'V' first then the close() will stop the timer.
  106777. + */
  106778. +
  106779. +static ssize_t wdog_write(struct file *file, const char __user *buf,
  106780. + size_t count, loff_t *ppos)
  106781. +{
  106782. + if (count) {
  106783. + if (!nowayout) {
  106784. + size_t i;
  106785. +
  106786. + /* In case it was set long ago */
  106787. + expect_close = 0;
  106788. +
  106789. + for (i = 0; i != count; i++) {
  106790. + char c;
  106791. + if (get_user(c, buf + i))
  106792. + return -EFAULT;
  106793. + if (c == 'V')
  106794. + expect_close = 42;
  106795. + }
  106796. + }
  106797. + wdog_ping();
  106798. + }
  106799. + return count;
  106800. +}
  106801. +
  106802. +static int wdog_get_status(void)
  106803. +{
  106804. + unsigned long flags;
  106805. + int status = 0;
  106806. + spin_lock_irqsave(&wdog_lock, flags);
  106807. + /* FIXME: readback reset reason */
  106808. + spin_unlock_irqrestore(&wdog_lock, flags);
  106809. + return status;
  106810. +}
  106811. +
  106812. +static uint32_t wdog_get_remaining(void)
  106813. +{
  106814. + uint32_t ret = ioread32(__io_address(PM_WDOG));
  106815. + return ret & PM_WDOG_TIME_SET;
  106816. +}
  106817. +
  106818. +/**
  106819. + * @file: file handle to the device
  106820. + * @cmd: watchdog command
  106821. + * @arg: argument pointer
  106822. + *
  106823. + * The watchdog API defines a common set of functions for all watchdogs
  106824. + * according to their available features. We only actually usefully support
  106825. + * querying capabilities and current status.
  106826. + */
  106827. +
  106828. +static long wdog_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  106829. +{
  106830. + void __user *argp = (void __user *)arg;
  106831. + int __user *p = argp;
  106832. + int new_heartbeat;
  106833. + int status;
  106834. + int options;
  106835. + uint32_t remaining;
  106836. +
  106837. + struct watchdog_info ident = {
  106838. + .options = WDIOF_SETTIMEOUT|
  106839. + WDIOF_MAGICCLOSE|
  106840. + WDIOF_KEEPALIVEPING,
  106841. + .firmware_version = 1,
  106842. + .identity = "BCM2708",
  106843. + };
  106844. +
  106845. + switch (cmd) {
  106846. + case WDIOC_GETSUPPORT:
  106847. + return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  106848. + case WDIOC_GETSTATUS:
  106849. + status = wdog_get_status();
  106850. + return put_user(status, p);
  106851. + case WDIOC_GETBOOTSTATUS:
  106852. + return put_user(0, p);
  106853. + case WDIOC_KEEPALIVE:
  106854. + wdog_ping();
  106855. + return 0;
  106856. + case WDIOC_SETTIMEOUT:
  106857. + if (get_user(new_heartbeat, p))
  106858. + return -EFAULT;
  106859. + if (wdog_set_heartbeat(new_heartbeat))
  106860. + return -EINVAL;
  106861. + wdog_ping();
  106862. + /* Fall */
  106863. + case WDIOC_GETTIMEOUT:
  106864. + return put_user(heartbeat, p);
  106865. + case WDIOC_GETTIMELEFT:
  106866. + remaining = WDOG_TICKS_TO_SECS(wdog_get_remaining());
  106867. + return put_user(remaining, p);
  106868. + case WDIOC_SETOPTIONS:
  106869. + if (get_user(options, p))
  106870. + return -EFAULT;
  106871. + if (options & WDIOS_DISABLECARD)
  106872. + wdog_stop();
  106873. + if (options & WDIOS_ENABLECARD)
  106874. + wdog_start(wdog_ticks);
  106875. + return 0;
  106876. + default:
  106877. + return -ENOTTY;
  106878. + }
  106879. +}
  106880. +
  106881. +/**
  106882. + * @inode: inode of device
  106883. + * @file: file handle to device
  106884. + *
  106885. + * The watchdog device has been opened. The watchdog device is single
  106886. + * open and on opening we load the counters.
  106887. + */
  106888. +
  106889. +static int wdog_open(struct inode *inode, struct file *file)
  106890. +{
  106891. + if (test_and_set_bit(0, &wdog_is_open))
  106892. + return -EBUSY;
  106893. + /*
  106894. + * Activate
  106895. + */
  106896. + wdog_start(wdog_ticks);
  106897. + return nonseekable_open(inode, file);
  106898. +}
  106899. +
  106900. +/**
  106901. + * @inode: inode to board
  106902. + * @file: file handle to board
  106903. + *
  106904. + * The watchdog has a configurable API. There is a religious dispute
  106905. + * between people who want their watchdog to be able to shut down and
  106906. + * those who want to be sure if the watchdog manager dies the machine
  106907. + * reboots. In the former case we disable the counters, in the latter
  106908. + * case you have to open it again very soon.
  106909. + */
  106910. +
  106911. +static int wdog_release(struct inode *inode, struct file *file)
  106912. +{
  106913. + if (expect_close == 42) {
  106914. + wdog_stop();
  106915. + } else {
  106916. + printk(KERN_CRIT
  106917. + "wdt: WDT device closed unexpectedly. WDT will not stop!\n");
  106918. + wdog_ping();
  106919. + }
  106920. + clear_bit(0, &wdog_is_open);
  106921. + expect_close = 0;
  106922. + return 0;
  106923. +}
  106924. +
  106925. +/**
  106926. + * @this: our notifier block
  106927. + * @code: the event being reported
  106928. + * @unused: unused
  106929. + *
  106930. + * Our notifier is called on system shutdowns. Turn the watchdog
  106931. + * off so that it does not fire during the next reboot.
  106932. + */
  106933. +
  106934. +static int wdog_notify_sys(struct notifier_block *this, unsigned long code,
  106935. + void *unused)
  106936. +{
  106937. + if (code == SYS_DOWN || code == SYS_HALT)
  106938. + wdog_stop();
  106939. + return NOTIFY_DONE;
  106940. +}
  106941. +
  106942. +/*
  106943. + * Kernel Interfaces
  106944. + */
  106945. +
  106946. +
  106947. +static const struct file_operations wdog_fops = {
  106948. + .owner = THIS_MODULE,
  106949. + .llseek = no_llseek,
  106950. + .write = wdog_write,
  106951. + .unlocked_ioctl = wdog_ioctl,
  106952. + .open = wdog_open,
  106953. + .release = wdog_release,
  106954. +};
  106955. +
  106956. +static struct miscdevice wdog_miscdev = {
  106957. + .minor = WATCHDOG_MINOR,
  106958. + .name = "watchdog",
  106959. + .fops = &wdog_fops,
  106960. +};
  106961. +
  106962. +/*
  106963. + * The WDT card needs to learn about soft shutdowns in order to
  106964. + * turn the timebomb registers off.
  106965. + */
  106966. +
  106967. +static struct notifier_block wdog_notifier = {
  106968. + .notifier_call = wdog_notify_sys,
  106969. +};
  106970. +
  106971. +/**
  106972. + * cleanup_module:
  106973. + *
  106974. + * Unload the watchdog. You cannot do this with any file handles open.
  106975. + * If your watchdog is set to continue ticking on close and you unload
  106976. + * it, well it keeps ticking. We won't get the interrupt but the board
  106977. + * will not touch PC memory so all is fine. You just have to load a new
  106978. + * module in 60 seconds or reboot.
  106979. + */
  106980. +
  106981. +static void __exit wdog_exit(void)
  106982. +{
  106983. + misc_deregister(&wdog_miscdev);
  106984. + unregister_reboot_notifier(&wdog_notifier);
  106985. +}
  106986. +
  106987. +static int __init wdog_init(void)
  106988. +{
  106989. + int ret;
  106990. +
  106991. + /* Check that the heartbeat value is within it's range;
  106992. + if not reset to the default */
  106993. + if (wdog_set_heartbeat(heartbeat)) {
  106994. + wdog_set_heartbeat(WD_TIMO);
  106995. + printk(KERN_INFO "bcm2708_wdog: heartbeat value must be "
  106996. + "0 < heartbeat < %d, using %d\n",
  106997. + WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
  106998. + WD_TIMO);
  106999. + }
  107000. +
  107001. + ret = register_reboot_notifier(&wdog_notifier);
  107002. + if (ret) {
  107003. + printk(KERN_ERR
  107004. + "wdt: cannot register reboot notifier (err=%d)\n", ret);
  107005. + goto out_reboot;
  107006. + }
  107007. +
  107008. + ret = misc_register(&wdog_miscdev);
  107009. + if (ret) {
  107010. + printk(KERN_ERR
  107011. + "wdt: cannot register miscdev on minor=%d (err=%d)\n",
  107012. + WATCHDOG_MINOR, ret);
  107013. + goto out_misc;
  107014. + }
  107015. +
  107016. + printk(KERN_INFO "bcm2708 watchdog, heartbeat=%d sec (nowayout=%d)\n",
  107017. + heartbeat, nowayout);
  107018. + return 0;
  107019. +
  107020. +out_misc:
  107021. + unregister_reboot_notifier(&wdog_notifier);
  107022. +out_reboot:
  107023. + return ret;
  107024. +}
  107025. +
  107026. +module_init(wdog_init);
  107027. +module_exit(wdog_exit);
  107028. +
  107029. +MODULE_AUTHOR("Luke Diamand");
  107030. +MODULE_DESCRIPTION("Driver for BCM2708 watchdog");
  107031. +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  107032. +MODULE_ALIAS_MISCDEV(TEMP_MINOR);
  107033. +MODULE_LICENSE("GPL");
  107034. diff -Nur linux-3.12.33/drivers/watchdog/Kconfig linux-3.12.33-rpi/drivers/watchdog/Kconfig
  107035. --- linux-3.12.33/drivers/watchdog/Kconfig 2014-11-15 06:28:07.000000000 -0600
  107036. +++ linux-3.12.33-rpi/drivers/watchdog/Kconfig 2014-12-03 19:13:42.584418001 -0600
  107037. @@ -392,6 +392,12 @@
  107038. To compile this driver as a module, choose M here: the
  107039. module will be called retu_wdt.
  107040. +config BCM2708_WDT
  107041. + tristate "BCM2708 Watchdog"
  107042. + depends on ARCH_BCM2708
  107043. + help
  107044. + Enables BCM2708 watchdog support.
  107045. +
  107046. # AVR32 Architecture
  107047. config AT32AP700X_WDT
  107048. diff -Nur linux-3.12.33/drivers/watchdog/Makefile linux-3.12.33-rpi/drivers/watchdog/Makefile
  107049. --- linux-3.12.33/drivers/watchdog/Makefile 2014-11-15 06:28:07.000000000 -0600
  107050. +++ linux-3.12.33-rpi/drivers/watchdog/Makefile 2014-12-03 19:13:42.584418001 -0600
  107051. @@ -54,6 +54,7 @@
  107052. obj-$(CONFIG_IMX2_WDT) += imx2_wdt.o
  107053. obj-$(CONFIG_UX500_WATCHDOG) += ux500_wdt.o
  107054. obj-$(CONFIG_RETU_WATCHDOG) += retu_wdt.o
  107055. +obj-$(CONFIG_BCM2708_WDT) += bcm2708_wdog.o
  107056. obj-$(CONFIG_BCM2835_WDT) += bcm2835_wdt.o
  107057. # AVR32 Architecture
  107058. diff -Nur linux-3.12.33/include/linux/broadcom/vc_cma.h linux-3.12.33-rpi/include/linux/broadcom/vc_cma.h
  107059. --- linux-3.12.33/include/linux/broadcom/vc_cma.h 1969-12-31 18:00:00.000000000 -0600
  107060. +++ linux-3.12.33-rpi/include/linux/broadcom/vc_cma.h 2014-12-03 19:13:43.040418001 -0600
  107061. @@ -0,0 +1,29 @@
  107062. +/*****************************************************************************
  107063. +* Copyright 2012 Broadcom Corporation. All rights reserved.
  107064. +*
  107065. +* Unless you and Broadcom execute a separate written software license
  107066. +* agreement governing use of this software, this software is licensed to you
  107067. +* under the terms of the GNU General Public License version 2, available at
  107068. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  107069. +*
  107070. +* Notwithstanding the above, under no circumstances may you combine this
  107071. +* software in any way with any other Broadcom software provided under a
  107072. +* license other than the GPL, without Broadcom's express prior written
  107073. +* consent.
  107074. +*****************************************************************************/
  107075. +
  107076. +#if !defined( VC_CMA_H )
  107077. +#define VC_CMA_H
  107078. +
  107079. +#include <linux/ioctl.h>
  107080. +
  107081. +#define VC_CMA_IOC_MAGIC 0xc5
  107082. +
  107083. +#define VC_CMA_IOC_RESERVE _IO(VC_CMA_IOC_MAGIC, 0)
  107084. +
  107085. +#ifdef __KERNEL__
  107086. +extern void __init vc_cma_early_init(void);
  107087. +extern void __init vc_cma_reserve(void);
  107088. +#endif
  107089. +
  107090. +#endif /* VC_CMA_H */
  107091. diff -Nur linux-3.12.33/include/linux/mmc/host.h linux-3.12.33-rpi/include/linux/mmc/host.h
  107092. --- linux-3.12.33/include/linux/mmc/host.h 2014-11-15 06:28:07.000000000 -0600
  107093. +++ linux-3.12.33-rpi/include/linux/mmc/host.h 2014-12-03 19:13:43.132418001 -0600
  107094. @@ -281,6 +281,7 @@
  107095. MMC_CAP2_PACKED_WR)
  107096. #define MMC_CAP2_NO_PRESCAN_POWERUP (1 << 14) /* Don't power up before scan */
  107097. #define MMC_CAP2_SANITIZE (1 << 15) /* Support Sanitize */
  107098. +#define MMC_CAP2_FORCE_MULTIBLOCK (1 << 31) /* Always use multiblock transfers */
  107099. mmc_pm_flag_t pm_caps; /* supported pm features */
  107100. diff -Nur linux-3.12.33/include/linux/mmc/sdhci.h linux-3.12.33-rpi/include/linux/mmc/sdhci.h
  107101. --- linux-3.12.33/include/linux/mmc/sdhci.h 2014-11-15 06:28:07.000000000 -0600
  107102. +++ linux-3.12.33-rpi/include/linux/mmc/sdhci.h 2014-12-03 19:13:43.132418001 -0600
  107103. @@ -100,6 +100,7 @@
  107104. #define SDHCI_QUIRK2_BROKEN_HOST_CONTROL (1<<5)
  107105. int irq; /* Device IRQ */
  107106. + int second_irq; /* Additional IRQ to disable/enable in low-latency mode */
  107107. void __iomem *ioaddr; /* Mapped address */
  107108. const struct sdhci_ops *ops; /* Low level hw interface */
  107109. @@ -131,6 +132,7 @@
  107110. #define SDHCI_SDIO_IRQ_ENABLED (1<<9) /* SDIO irq enabled */
  107111. #define SDHCI_SDR104_NEEDS_TUNING (1<<10) /* SDR104/HS200 needs tuning */
  107112. #define SDHCI_USING_RETUNING_TIMER (1<<11) /* Host is using a retuning timer for the card */
  107113. +#define SDHCI_USE_PLATDMA (1<<12) /* Host uses 3rd party DMA */
  107114. unsigned int version; /* SDHCI spec. version */
  107115. @@ -146,6 +148,7 @@
  107116. struct mmc_request *mrq; /* Current request */
  107117. struct mmc_command *cmd; /* Current command */
  107118. + int last_cmdop; /* Opcode of last cmd sent */
  107119. struct mmc_data *data; /* Current data request */
  107120. unsigned int data_early:1; /* Data finished before cmd */
  107121. diff -Nur linux-3.12.33/include/linux/platform_data/bcm2708.h linux-3.12.33-rpi/include/linux/platform_data/bcm2708.h
  107122. --- linux-3.12.33/include/linux/platform_data/bcm2708.h 1969-12-31 18:00:00.000000000 -0600
  107123. +++ linux-3.12.33-rpi/include/linux/platform_data/bcm2708.h 2014-12-03 19:13:43.200418001 -0600
  107124. @@ -0,0 +1,23 @@
  107125. +/*
  107126. + * include/linux/platform_data/bcm2708.h
  107127. + *
  107128. + * This program is free software; you can redistribute it and/or modify
  107129. + * it under the terms of the GNU General Public License version 2 as
  107130. + * published by the Free Software Foundation.
  107131. + *
  107132. + * (C) 2014 Julian Scheel <julian@jusst.de>
  107133. + *
  107134. + */
  107135. +#ifndef __BCM2708_H_
  107136. +#define __BCM2708_H_
  107137. +
  107138. +typedef enum {
  107139. + BCM2708_PULL_OFF,
  107140. + BCM2708_PULL_UP,
  107141. + BCM2708_PULL_DOWN
  107142. +} bcm2708_gpio_pull_t;
  107143. +
  107144. +extern int bcm2708_gpio_setpull(struct gpio_chip *gc, unsigned offset,
  107145. + bcm2708_gpio_pull_t value);
  107146. +
  107147. +#endif
  107148. diff -Nur linux-3.12.33/include/linux/vmstat.h linux-3.12.33-rpi/include/linux/vmstat.h
  107149. --- linux-3.12.33/include/linux/vmstat.h 2014-11-15 06:28:07.000000000 -0600
  107150. +++ linux-3.12.33-rpi/include/linux/vmstat.h 2014-12-03 19:13:43.252418001 -0600
  107151. @@ -239,7 +239,11 @@
  107152. static inline void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
  107153. {
  107154. atomic_long_dec(&zone->vm_stat[item]);
  107155. + if (item == NR_FILE_DIRTY && unlikely(atomic_long_read(&zone->vm_stat[item]) < 0))
  107156. + atomic_long_set(&zone->vm_stat[item], 0);
  107157. atomic_long_dec(&vm_stat[item]);
  107158. + if (item == NR_FILE_DIRTY && unlikely(atomic_long_read(&vm_stat[item]) < 0))
  107159. + atomic_long_set(&vm_stat[item], 0);
  107160. }
  107161. static inline void __dec_zone_page_state(struct page *page,
  107162. diff -Nur linux-3.12.33/include/sound/soc-dai.h linux-3.12.33-rpi/include/sound/soc-dai.h
  107163. --- linux-3.12.33/include/sound/soc-dai.h 2014-11-15 06:28:07.000000000 -0600
  107164. +++ linux-3.12.33-rpi/include/sound/soc-dai.h 2014-12-03 19:13:43.360418001 -0600
  107165. @@ -105,6 +105,8 @@
  107166. int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
  107167. int pll_id, int source, unsigned int freq_in, unsigned int freq_out);
  107168. +int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio);
  107169. +
  107170. /* Digital Audio interface formatting */
  107171. int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt);
  107172. @@ -131,6 +133,7 @@
  107173. int (*set_pll)(struct snd_soc_dai *dai, int pll_id, int source,
  107174. unsigned int freq_in, unsigned int freq_out);
  107175. int (*set_clkdiv)(struct snd_soc_dai *dai, int div_id, int div);
  107176. + int (*set_bclk_ratio)(struct snd_soc_dai *dai, unsigned int ratio);
  107177. /*
  107178. * DAI format configuration
  107179. diff -Nur linux-3.12.33/include/uapi/linux/fb.h linux-3.12.33-rpi/include/uapi/linux/fb.h
  107180. --- linux-3.12.33/include/uapi/linux/fb.h 2014-11-15 06:28:07.000000000 -0600
  107181. +++ linux-3.12.33-rpi/include/uapi/linux/fb.h 2014-12-03 19:13:43.376418001 -0600
  107182. @@ -34,6 +34,11 @@
  107183. #define FBIOPUT_MODEINFO 0x4617
  107184. #define FBIOGET_DISPINFO 0x4618
  107185. #define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
  107186. +/*
  107187. + * HACK: use 'z' in order not to clash with any other ioctl numbers which might
  107188. + * be concurrently added to the mainline kernel
  107189. + */
  107190. +#define FBIOCOPYAREA _IOW('z', 0x21, struct fb_copyarea)
  107191. #define FB_TYPE_PACKED_PIXELS 0 /* Packed Pixels */
  107192. #define FB_TYPE_PLANES 1 /* Non interleaved planes */
  107193. diff -Nur linux-3.12.33/kernel/cgroup.c linux-3.12.33-rpi/kernel/cgroup.c
  107194. --- linux-3.12.33/kernel/cgroup.c 2014-11-15 06:28:07.000000000 -0600
  107195. +++ linux-3.12.33-rpi/kernel/cgroup.c 2014-12-03 19:13:43.456418001 -0600
  107196. @@ -5558,6 +5558,33 @@
  107197. }
  107198. __setup("cgroup_disable=", cgroup_disable);
  107199. +static int __init cgroup_enable(char *str)
  107200. +{
  107201. + struct cgroup_subsys *ss;
  107202. + char *token;
  107203. + int i;
  107204. +
  107205. + while ((token = strsep(&str, ",")) != NULL) {
  107206. + if (!*token)
  107207. + continue;
  107208. +
  107209. + /*
  107210. + * cgroup_disable, being at boot time, can't know about
  107211. + * module subsystems, so we don't worry about them.
  107212. + */
  107213. + for_each_builtin_subsys(ss, i) {
  107214. + if (!strcmp(token, ss->name)) {
  107215. + ss->disabled = 0;
  107216. + printk(KERN_INFO "Disabling %s control group"
  107217. + " subsystem\n", ss->name);
  107218. + break;
  107219. + }
  107220. + }
  107221. + }
  107222. + return 1;
  107223. +}
  107224. +__setup("cgroup_enable=", cgroup_enable);
  107225. +
  107226. /*
  107227. * Functons for CSS ID.
  107228. */
  107229. diff -Nur linux-3.12.33/mm/memcontrol.c linux-3.12.33-rpi/mm/memcontrol.c
  107230. --- linux-3.12.33/mm/memcontrol.c 2014-11-15 06:28:07.000000000 -0600
  107231. +++ linux-3.12.33-rpi/mm/memcontrol.c 2014-12-03 19:13:43.680418001 -0600
  107232. @@ -7066,6 +7066,7 @@
  107233. .base_cftypes = mem_cgroup_files,
  107234. .early_init = 0,
  107235. .use_id = 1,
  107236. + .disabled = 1,
  107237. };
  107238. #ifdef CONFIG_MEMCG_SWAP
  107239. diff -Nur linux-3.12.33/sound/arm/bcm2835.c linux-3.12.33-rpi/sound/arm/bcm2835.c
  107240. --- linux-3.12.33/sound/arm/bcm2835.c 1969-12-31 18:00:00.000000000 -0600
  107241. +++ linux-3.12.33-rpi/sound/arm/bcm2835.c 2014-12-03 19:13:44.308418001 -0600
  107242. @@ -0,0 +1,420 @@
  107243. +/*****************************************************************************
  107244. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  107245. +*
  107246. +* Unless you and Broadcom execute a separate written software license
  107247. +* agreement governing use of this software, this software is licensed to you
  107248. +* under the terms of the GNU General Public License version 2, available at
  107249. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  107250. +*
  107251. +* Notwithstanding the above, under no circumstances may you combine this
  107252. +* software in any way with any other Broadcom software provided under a
  107253. +* license other than the GPL, without Broadcom's express prior written
  107254. +* consent.
  107255. +*****************************************************************************/
  107256. +
  107257. +#include <linux/platform_device.h>
  107258. +
  107259. +#include <linux/init.h>
  107260. +#include <linux/slab.h>
  107261. +#include <linux/module.h>
  107262. +
  107263. +#include "bcm2835.h"
  107264. +
  107265. +/* module parameters (see "Module Parameters") */
  107266. +/* SNDRV_CARDS: maximum number of cards supported by this module */
  107267. +static int index[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = -1 };
  107268. +static char *id[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = NULL };
  107269. +static int enable[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = 1 };
  107270. +
  107271. +/* HACKY global pointers needed for successive probes to work : ssp
  107272. + * But compared against the changes we will have to do in VC audio_ipc code
  107273. + * to export 8 audio_ipc devices as a single IPC device and then monitor all
  107274. + * four devices in a thread, this gets things done quickly and should be easier
  107275. + * to debug if we run into issues
  107276. + */
  107277. +
  107278. +static struct snd_card *g_card = NULL;
  107279. +static bcm2835_chip_t *g_chip = NULL;
  107280. +
  107281. +static int snd_bcm2835_free(bcm2835_chip_t * chip)
  107282. +{
  107283. + kfree(chip);
  107284. + return 0;
  107285. +}
  107286. +
  107287. +/* component-destructor
  107288. + * (see "Management of Cards and Components")
  107289. + */
  107290. +static int snd_bcm2835_dev_free(struct snd_device *device)
  107291. +{
  107292. + return snd_bcm2835_free(device->device_data);
  107293. +}
  107294. +
  107295. +/* chip-specific constructor
  107296. + * (see "Management of Cards and Components")
  107297. + */
  107298. +static int snd_bcm2835_create(struct snd_card *card,
  107299. + struct platform_device *pdev,
  107300. + bcm2835_chip_t ** rchip)
  107301. +{
  107302. + bcm2835_chip_t *chip;
  107303. + int err;
  107304. + static struct snd_device_ops ops = {
  107305. + .dev_free = snd_bcm2835_dev_free,
  107306. + };
  107307. +
  107308. + *rchip = NULL;
  107309. +
  107310. + chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  107311. + if (chip == NULL)
  107312. + return -ENOMEM;
  107313. +
  107314. + chip->card = card;
  107315. +
  107316. + err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  107317. + if (err < 0) {
  107318. + snd_bcm2835_free(chip);
  107319. + return err;
  107320. + }
  107321. +
  107322. + *rchip = chip;
  107323. + return 0;
  107324. +}
  107325. +
  107326. +static int snd_bcm2835_alsa_probe(struct platform_device *pdev)
  107327. +{
  107328. + static int dev;
  107329. + bcm2835_chip_t *chip;
  107330. + struct snd_card *card;
  107331. + int err;
  107332. +
  107333. + if (dev >= MAX_SUBSTREAMS)
  107334. + return -ENODEV;
  107335. +
  107336. + if (!enable[dev]) {
  107337. + dev++;
  107338. + return -ENOENT;
  107339. + }
  107340. +
  107341. + if (dev > 0)
  107342. + goto add_register_map;
  107343. +
  107344. + err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &g_card);
  107345. + if (err < 0)
  107346. + goto out;
  107347. +
  107348. + snd_card_set_dev(g_card, &pdev->dev);
  107349. + strcpy(g_card->driver, "bcm2835");
  107350. + strcpy(g_card->shortname, "bcm2835 ALSA");
  107351. + sprintf(g_card->longname, "%s", g_card->shortname);
  107352. +
  107353. + err = snd_bcm2835_create(g_card, pdev, &chip);
  107354. + if (err < 0) {
  107355. + dev_err(&pdev->dev, "Failed to create bcm2835 chip\n");
  107356. + goto out_bcm2835_create;
  107357. + }
  107358. +
  107359. + g_chip = chip;
  107360. + err = snd_bcm2835_new_pcm(chip);
  107361. + if (err < 0) {
  107362. + dev_err(&pdev->dev, "Failed to create new BCM2835 pcm device\n");
  107363. + goto out_bcm2835_new_pcm;
  107364. + }
  107365. +
  107366. + err = snd_bcm2835_new_spdif_pcm(chip);
  107367. + if (err < 0) {
  107368. + dev_err(&pdev->dev, "Failed to create new BCM2835 spdif pcm device\n");
  107369. + goto out_bcm2835_new_spdif;
  107370. + }
  107371. +
  107372. + err = snd_bcm2835_new_ctl(chip);
  107373. + if (err < 0) {
  107374. + dev_err(&pdev->dev, "Failed to create new BCM2835 ctl\n");
  107375. + goto out_bcm2835_new_ctl;
  107376. + }
  107377. +
  107378. +add_register_map:
  107379. + card = g_card;
  107380. + chip = g_chip;
  107381. +
  107382. + BUG_ON(!(card && chip));
  107383. +
  107384. + chip->avail_substreams |= (1 << dev);
  107385. + chip->pdev[dev] = pdev;
  107386. +
  107387. + if (dev == 0) {
  107388. + err = snd_card_register(card);
  107389. + if (err < 0) {
  107390. + dev_err(&pdev->dev,
  107391. + "Failed to register bcm2835 ALSA card \n");
  107392. + goto out_card_register;
  107393. + }
  107394. + platform_set_drvdata(pdev, card);
  107395. + audio_info("bcm2835 ALSA card created!\n");
  107396. + } else {
  107397. + audio_info("bcm2835 ALSA chip created!\n");
  107398. + platform_set_drvdata(pdev, (void *)dev);
  107399. + }
  107400. +
  107401. + dev++;
  107402. +
  107403. + return 0;
  107404. +
  107405. +out_card_register:
  107406. +out_bcm2835_new_ctl:
  107407. +out_bcm2835_new_spdif:
  107408. +out_bcm2835_new_pcm:
  107409. +out_bcm2835_create:
  107410. + BUG_ON(!g_card);
  107411. + if (snd_card_free(g_card))
  107412. + dev_err(&pdev->dev, "Failed to free Registered alsa card\n");
  107413. + g_card = NULL;
  107414. +out:
  107415. + dev = SNDRV_CARDS; /* stop more avail_substreams from being probed */
  107416. + dev_err(&pdev->dev, "BCM2835 ALSA Probe failed !!\n");
  107417. + return err;
  107418. +}
  107419. +
  107420. +static int snd_bcm2835_alsa_remove(struct platform_device *pdev)
  107421. +{
  107422. + uint32_t idx;
  107423. + void *drv_data;
  107424. +
  107425. + drv_data = platform_get_drvdata(pdev);
  107426. +
  107427. + if (drv_data == (void *)g_card) {
  107428. + /* This is the card device */
  107429. + snd_card_free((struct snd_card *)drv_data);
  107430. + g_card = NULL;
  107431. + g_chip = NULL;
  107432. + } else {
  107433. + idx = (uint32_t) drv_data;
  107434. + if (g_card != NULL) {
  107435. + BUG_ON(!g_chip);
  107436. + /* We pass chip device numbers in audio ipc devices
  107437. + * other than the one we registered our card with
  107438. + */
  107439. + idx = (uint32_t) drv_data;
  107440. + BUG_ON(!idx || idx > MAX_SUBSTREAMS);
  107441. + g_chip->avail_substreams &= ~(1 << idx);
  107442. + /* There should be atleast one substream registered
  107443. + * after we are done here, as it wil be removed when
  107444. + * the *remove* is called for the card device
  107445. + */
  107446. + BUG_ON(!g_chip->avail_substreams);
  107447. + }
  107448. + }
  107449. +
  107450. + platform_set_drvdata(pdev, NULL);
  107451. +
  107452. + return 0;
  107453. +}
  107454. +
  107455. +#ifdef CONFIG_PM
  107456. +static int snd_bcm2835_alsa_suspend(struct platform_device *pdev,
  107457. + pm_message_t state)
  107458. +{
  107459. + return 0;
  107460. +}
  107461. +
  107462. +static int snd_bcm2835_alsa_resume(struct platform_device *pdev)
  107463. +{
  107464. + return 0;
  107465. +}
  107466. +
  107467. +#endif
  107468. +
  107469. +static struct platform_driver bcm2835_alsa0_driver = {
  107470. + .probe = snd_bcm2835_alsa_probe,
  107471. + .remove = snd_bcm2835_alsa_remove,
  107472. +#ifdef CONFIG_PM
  107473. + .suspend = snd_bcm2835_alsa_suspend,
  107474. + .resume = snd_bcm2835_alsa_resume,
  107475. +#endif
  107476. + .driver = {
  107477. + .name = "bcm2835_AUD0",
  107478. + .owner = THIS_MODULE,
  107479. + },
  107480. +};
  107481. +
  107482. +static struct platform_driver bcm2835_alsa1_driver = {
  107483. + .probe = snd_bcm2835_alsa_probe,
  107484. + .remove = snd_bcm2835_alsa_remove,
  107485. +#ifdef CONFIG_PM
  107486. + .suspend = snd_bcm2835_alsa_suspend,
  107487. + .resume = snd_bcm2835_alsa_resume,
  107488. +#endif
  107489. + .driver = {
  107490. + .name = "bcm2835_AUD1",
  107491. + .owner = THIS_MODULE,
  107492. + },
  107493. +};
  107494. +
  107495. +static struct platform_driver bcm2835_alsa2_driver = {
  107496. + .probe = snd_bcm2835_alsa_probe,
  107497. + .remove = snd_bcm2835_alsa_remove,
  107498. +#ifdef CONFIG_PM
  107499. + .suspend = snd_bcm2835_alsa_suspend,
  107500. + .resume = snd_bcm2835_alsa_resume,
  107501. +#endif
  107502. + .driver = {
  107503. + .name = "bcm2835_AUD2",
  107504. + .owner = THIS_MODULE,
  107505. + },
  107506. +};
  107507. +
  107508. +static struct platform_driver bcm2835_alsa3_driver = {
  107509. + .probe = snd_bcm2835_alsa_probe,
  107510. + .remove = snd_bcm2835_alsa_remove,
  107511. +#ifdef CONFIG_PM
  107512. + .suspend = snd_bcm2835_alsa_suspend,
  107513. + .resume = snd_bcm2835_alsa_resume,
  107514. +#endif
  107515. + .driver = {
  107516. + .name = "bcm2835_AUD3",
  107517. + .owner = THIS_MODULE,
  107518. + },
  107519. +};
  107520. +
  107521. +static struct platform_driver bcm2835_alsa4_driver = {
  107522. + .probe = snd_bcm2835_alsa_probe,
  107523. + .remove = snd_bcm2835_alsa_remove,
  107524. +#ifdef CONFIG_PM
  107525. + .suspend = snd_bcm2835_alsa_suspend,
  107526. + .resume = snd_bcm2835_alsa_resume,
  107527. +#endif
  107528. + .driver = {
  107529. + .name = "bcm2835_AUD4",
  107530. + .owner = THIS_MODULE,
  107531. + },
  107532. +};
  107533. +
  107534. +static struct platform_driver bcm2835_alsa5_driver = {
  107535. + .probe = snd_bcm2835_alsa_probe,
  107536. + .remove = snd_bcm2835_alsa_remove,
  107537. +#ifdef CONFIG_PM
  107538. + .suspend = snd_bcm2835_alsa_suspend,
  107539. + .resume = snd_bcm2835_alsa_resume,
  107540. +#endif
  107541. + .driver = {
  107542. + .name = "bcm2835_AUD5",
  107543. + .owner = THIS_MODULE,
  107544. + },
  107545. +};
  107546. +
  107547. +static struct platform_driver bcm2835_alsa6_driver = {
  107548. + .probe = snd_bcm2835_alsa_probe,
  107549. + .remove = snd_bcm2835_alsa_remove,
  107550. +#ifdef CONFIG_PM
  107551. + .suspend = snd_bcm2835_alsa_suspend,
  107552. + .resume = snd_bcm2835_alsa_resume,
  107553. +#endif
  107554. + .driver = {
  107555. + .name = "bcm2835_AUD6",
  107556. + .owner = THIS_MODULE,
  107557. + },
  107558. +};
  107559. +
  107560. +static struct platform_driver bcm2835_alsa7_driver = {
  107561. + .probe = snd_bcm2835_alsa_probe,
  107562. + .remove = snd_bcm2835_alsa_remove,
  107563. +#ifdef CONFIG_PM
  107564. + .suspend = snd_bcm2835_alsa_suspend,
  107565. + .resume = snd_bcm2835_alsa_resume,
  107566. +#endif
  107567. + .driver = {
  107568. + .name = "bcm2835_AUD7",
  107569. + .owner = THIS_MODULE,
  107570. + },
  107571. +};
  107572. +
  107573. +static int bcm2835_alsa_device_init(void)
  107574. +{
  107575. + int err;
  107576. + err = platform_driver_register(&bcm2835_alsa0_driver);
  107577. + if (err) {
  107578. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  107579. + goto out;
  107580. + }
  107581. +
  107582. + err = platform_driver_register(&bcm2835_alsa1_driver);
  107583. + if (err) {
  107584. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  107585. + goto unregister_0;
  107586. + }
  107587. +
  107588. + err = platform_driver_register(&bcm2835_alsa2_driver);
  107589. + if (err) {
  107590. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  107591. + goto unregister_1;
  107592. + }
  107593. +
  107594. + err = platform_driver_register(&bcm2835_alsa3_driver);
  107595. + if (err) {
  107596. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  107597. + goto unregister_2;
  107598. + }
  107599. +
  107600. + err = platform_driver_register(&bcm2835_alsa4_driver);
  107601. + if (err) {
  107602. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  107603. + goto unregister_3;
  107604. + }
  107605. +
  107606. + err = platform_driver_register(&bcm2835_alsa5_driver);
  107607. + if (err) {
  107608. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  107609. + goto unregister_4;
  107610. + }
  107611. +
  107612. + err = platform_driver_register(&bcm2835_alsa6_driver);
  107613. + if (err) {
  107614. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  107615. + goto unregister_5;
  107616. + }
  107617. +
  107618. + err = platform_driver_register(&bcm2835_alsa7_driver);
  107619. + if (err) {
  107620. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  107621. + goto unregister_6;
  107622. + }
  107623. +
  107624. + return 0;
  107625. +
  107626. +unregister_6:
  107627. + platform_driver_unregister(&bcm2835_alsa6_driver);
  107628. +unregister_5:
  107629. + platform_driver_unregister(&bcm2835_alsa5_driver);
  107630. +unregister_4:
  107631. + platform_driver_unregister(&bcm2835_alsa4_driver);
  107632. +unregister_3:
  107633. + platform_driver_unregister(&bcm2835_alsa3_driver);
  107634. +unregister_2:
  107635. + platform_driver_unregister(&bcm2835_alsa2_driver);
  107636. +unregister_1:
  107637. + platform_driver_unregister(&bcm2835_alsa1_driver);
  107638. +unregister_0:
  107639. + platform_driver_unregister(&bcm2835_alsa0_driver);
  107640. +out:
  107641. + return err;
  107642. +}
  107643. +
  107644. +static void bcm2835_alsa_device_exit(void)
  107645. +{
  107646. + platform_driver_unregister(&bcm2835_alsa0_driver);
  107647. + platform_driver_unregister(&bcm2835_alsa1_driver);
  107648. + platform_driver_unregister(&bcm2835_alsa2_driver);
  107649. + platform_driver_unregister(&bcm2835_alsa3_driver);
  107650. + platform_driver_unregister(&bcm2835_alsa4_driver);
  107651. + platform_driver_unregister(&bcm2835_alsa5_driver);
  107652. + platform_driver_unregister(&bcm2835_alsa6_driver);
  107653. + platform_driver_unregister(&bcm2835_alsa7_driver);
  107654. +}
  107655. +
  107656. +late_initcall(bcm2835_alsa_device_init);
  107657. +module_exit(bcm2835_alsa_device_exit);
  107658. +
  107659. +MODULE_AUTHOR("Dom Cobley");
  107660. +MODULE_DESCRIPTION("Alsa driver for BCM2835 chip");
  107661. +MODULE_LICENSE("GPL");
  107662. +MODULE_ALIAS("platform:bcm2835_alsa");
  107663. diff -Nur linux-3.12.33/sound/arm/bcm2835-ctl.c linux-3.12.33-rpi/sound/arm/bcm2835-ctl.c
  107664. --- linux-3.12.33/sound/arm/bcm2835-ctl.c 1969-12-31 18:00:00.000000000 -0600
  107665. +++ linux-3.12.33-rpi/sound/arm/bcm2835-ctl.c 2014-12-03 19:13:44.308418001 -0600
  107666. @@ -0,0 +1,323 @@
  107667. +/*****************************************************************************
  107668. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  107669. +*
  107670. +* Unless you and Broadcom execute a separate written software license
  107671. +* agreement governing use of this software, this software is licensed to you
  107672. +* under the terms of the GNU General Public License version 2, available at
  107673. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  107674. +*
  107675. +* Notwithstanding the above, under no circumstances may you combine this
  107676. +* software in any way with any other Broadcom software provided under a
  107677. +* license other than the GPL, without Broadcom's express prior written
  107678. +* consent.
  107679. +*****************************************************************************/
  107680. +
  107681. +#include <linux/platform_device.h>
  107682. +#include <linux/init.h>
  107683. +#include <linux/io.h>
  107684. +#include <linux/jiffies.h>
  107685. +#include <linux/slab.h>
  107686. +#include <linux/time.h>
  107687. +#include <linux/wait.h>
  107688. +#include <linux/delay.h>
  107689. +#include <linux/moduleparam.h>
  107690. +#include <linux/sched.h>
  107691. +
  107692. +#include <sound/core.h>
  107693. +#include <sound/control.h>
  107694. +#include <sound/pcm.h>
  107695. +#include <sound/pcm_params.h>
  107696. +#include <sound/rawmidi.h>
  107697. +#include <sound/initval.h>
  107698. +#include <sound/tlv.h>
  107699. +#include <sound/asoundef.h>
  107700. +
  107701. +#include "bcm2835.h"
  107702. +
  107703. +/* volume maximum and minimum in terms of 0.01dB */
  107704. +#define CTRL_VOL_MAX 400
  107705. +#define CTRL_VOL_MIN -10239 /* originally -10240 */
  107706. +
  107707. +
  107708. +static int snd_bcm2835_ctl_info(struct snd_kcontrol *kcontrol,
  107709. + struct snd_ctl_elem_info *uinfo)
  107710. +{
  107711. + audio_info(" ... IN\n");
  107712. + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
  107713. + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  107714. + uinfo->count = 1;
  107715. + uinfo->value.integer.min = CTRL_VOL_MIN;
  107716. + uinfo->value.integer.max = CTRL_VOL_MAX; /* 2303 */
  107717. + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
  107718. + uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  107719. + uinfo->count = 1;
  107720. + uinfo->value.integer.min = 0;
  107721. + uinfo->value.integer.max = 1;
  107722. + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
  107723. + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  107724. + uinfo->count = 1;
  107725. + uinfo->value.integer.min = 0;
  107726. + uinfo->value.integer.max = AUDIO_DEST_MAX-1;
  107727. + }
  107728. + audio_info(" ... OUT\n");
  107729. + return 0;
  107730. +}
  107731. +
  107732. +/* toggles mute on or off depending on the value of nmute, and returns
  107733. + * 1 if the mute value was changed, otherwise 0
  107734. + */
  107735. +static int toggle_mute(struct bcm2835_chip *chip, int nmute)
  107736. +{
  107737. + /* if settings are ok, just return 0 */
  107738. + if(chip->mute == nmute)
  107739. + return 0;
  107740. +
  107741. + /* if the sound is muted then we need to unmute */
  107742. + if(chip->mute == CTRL_VOL_MUTE)
  107743. + {
  107744. + chip->volume = chip->old_volume; /* copy the old volume back */
  107745. + audio_info("Unmuting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
  107746. + }
  107747. + else /* otherwise we mute */
  107748. + {
  107749. + chip->old_volume = chip->volume;
  107750. + chip->volume = 26214; /* set volume to minimum level AKA mute */
  107751. + audio_info("Muting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
  107752. + }
  107753. +
  107754. + chip->mute = nmute;
  107755. + return 1;
  107756. +}
  107757. +
  107758. +static int snd_bcm2835_ctl_get(struct snd_kcontrol *kcontrol,
  107759. + struct snd_ctl_elem_value *ucontrol)
  107760. +{
  107761. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  107762. +
  107763. + BUG_ON(!chip && !(chip->avail_substreams & AVAIL_SUBSTREAMS_MASK));
  107764. +
  107765. + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME)
  107766. + ucontrol->value.integer.value[0] = chip2alsa(chip->volume);
  107767. + else if (kcontrol->private_value == PCM_PLAYBACK_MUTE)
  107768. + ucontrol->value.integer.value[0] = chip->mute;
  107769. + else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE)
  107770. + ucontrol->value.integer.value[0] = chip->dest;
  107771. +
  107772. + return 0;
  107773. +}
  107774. +
  107775. +static int snd_bcm2835_ctl_put(struct snd_kcontrol *kcontrol,
  107776. + struct snd_ctl_elem_value *ucontrol)
  107777. +{
  107778. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  107779. + int changed = 0;
  107780. +
  107781. + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
  107782. + audio_info("Volume change attempted.. volume = %d new_volume = %d\n", chip->volume, (int)ucontrol->value.integer.value[0]);
  107783. + if (chip->mute == CTRL_VOL_MUTE) {
  107784. + /* changed = toggle_mute(chip, CTRL_VOL_UNMUTE); */
  107785. + return 1; /* should return 0 to signify no change but the mixer takes this as the opposite sign (no idea why) */
  107786. + }
  107787. + if (changed
  107788. + || (ucontrol->value.integer.value[0] != chip2alsa(chip->volume))) {
  107789. +
  107790. + chip->volume = alsa2chip(ucontrol->value.integer.value[0]);
  107791. + changed = 1;
  107792. + }
  107793. +
  107794. + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
  107795. + /* Now implemented */
  107796. + audio_info(" Mute attempted\n");
  107797. + changed = toggle_mute(chip, ucontrol->value.integer.value[0]);
  107798. +
  107799. + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
  107800. + if (ucontrol->value.integer.value[0] != chip->dest) {
  107801. + chip->dest = ucontrol->value.integer.value[0];
  107802. + changed = 1;
  107803. + }
  107804. + }
  107805. +
  107806. + if (changed) {
  107807. + if (bcm2835_audio_set_ctls(chip))
  107808. + printk(KERN_ERR "Failed to set ALSA controls..\n");
  107809. + }
  107810. +
  107811. + return changed;
  107812. +}
  107813. +
  107814. +static DECLARE_TLV_DB_SCALE(snd_bcm2835_db_scale, CTRL_VOL_MIN, 1, 1);
  107815. +
  107816. +static struct snd_kcontrol_new snd_bcm2835_ctl[] = {
  107817. + {
  107818. + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  107819. + .name = "PCM Playback Volume",
  107820. + .index = 0,
  107821. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  107822. + .private_value = PCM_PLAYBACK_VOLUME,
  107823. + .info = snd_bcm2835_ctl_info,
  107824. + .get = snd_bcm2835_ctl_get,
  107825. + .put = snd_bcm2835_ctl_put,
  107826. + .count = 1,
  107827. + .tlv = {.p = snd_bcm2835_db_scale}
  107828. + },
  107829. + {
  107830. + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  107831. + .name = "PCM Playback Switch",
  107832. + .index = 0,
  107833. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  107834. + .private_value = PCM_PLAYBACK_MUTE,
  107835. + .info = snd_bcm2835_ctl_info,
  107836. + .get = snd_bcm2835_ctl_get,
  107837. + .put = snd_bcm2835_ctl_put,
  107838. + .count = 1,
  107839. + },
  107840. + {
  107841. + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  107842. + .name = "PCM Playback Route",
  107843. + .index = 0,
  107844. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  107845. + .private_value = PCM_PLAYBACK_DEVICE,
  107846. + .info = snd_bcm2835_ctl_info,
  107847. + .get = snd_bcm2835_ctl_get,
  107848. + .put = snd_bcm2835_ctl_put,
  107849. + .count = 1,
  107850. + },
  107851. +};
  107852. +
  107853. +static int snd_bcm2835_spdif_default_info(struct snd_kcontrol *kcontrol,
  107854. + struct snd_ctl_elem_info *uinfo)
  107855. +{
  107856. + uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  107857. + uinfo->count = 1;
  107858. + return 0;
  107859. +}
  107860. +
  107861. +static int snd_bcm2835_spdif_default_get(struct snd_kcontrol *kcontrol,
  107862. + struct snd_ctl_elem_value *ucontrol)
  107863. +{
  107864. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  107865. + int i;
  107866. +
  107867. + for (i = 0; i < 4; i++)
  107868. + ucontrol->value.iec958.status[i] =
  107869. + (chip->spdif_status >> (i * 8)) && 0xff;
  107870. +
  107871. + return 0;
  107872. +}
  107873. +
  107874. +static int snd_bcm2835_spdif_default_put(struct snd_kcontrol *kcontrol,
  107875. + struct snd_ctl_elem_value *ucontrol)
  107876. +{
  107877. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  107878. + unsigned int val = 0;
  107879. + int i, change;
  107880. +
  107881. + for (i = 0; i < 4; i++)
  107882. + val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
  107883. +
  107884. + change = val != chip->spdif_status;
  107885. + chip->spdif_status = val;
  107886. +
  107887. + return change;
  107888. +}
  107889. +
  107890. +static int snd_bcm2835_spdif_mask_info(struct snd_kcontrol *kcontrol,
  107891. + struct snd_ctl_elem_info *uinfo)
  107892. +{
  107893. + uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  107894. + uinfo->count = 1;
  107895. + return 0;
  107896. +}
  107897. +
  107898. +static int snd_bcm2835_spdif_mask_get(struct snd_kcontrol *kcontrol,
  107899. + struct snd_ctl_elem_value *ucontrol)
  107900. +{
  107901. + /* bcm2835 supports only consumer mode and sets all other format flags
  107902. + * automatically. So the only thing left is signalling non-audio
  107903. + * content */
  107904. + ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO;
  107905. + return 0;
  107906. +}
  107907. +
  107908. +static int snd_bcm2835_spdif_stream_info(struct snd_kcontrol *kcontrol,
  107909. + struct snd_ctl_elem_info *uinfo)
  107910. +{
  107911. + uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  107912. + uinfo->count = 1;
  107913. + return 0;
  107914. +}
  107915. +
  107916. +static int snd_bcm2835_spdif_stream_get(struct snd_kcontrol *kcontrol,
  107917. + struct snd_ctl_elem_value *ucontrol)
  107918. +{
  107919. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  107920. + int i;
  107921. +
  107922. + for (i = 0; i < 4; i++)
  107923. + ucontrol->value.iec958.status[i] =
  107924. + (chip->spdif_status >> (i * 8)) & 0xff;
  107925. + return 0;
  107926. +}
  107927. +
  107928. +static int snd_bcm2835_spdif_stream_put(struct snd_kcontrol *kcontrol,
  107929. + struct snd_ctl_elem_value *ucontrol)
  107930. +{
  107931. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  107932. + unsigned int val = 0;
  107933. + int i, change;
  107934. +
  107935. + for (i = 0; i < 4; i++)
  107936. + val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
  107937. + change = val != chip->spdif_status;
  107938. + chip->spdif_status = val;
  107939. +
  107940. + return change;
  107941. +}
  107942. +
  107943. +static struct snd_kcontrol_new snd_bcm2835_spdif[] = {
  107944. + {
  107945. + .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  107946. + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  107947. + .info = snd_bcm2835_spdif_default_info,
  107948. + .get = snd_bcm2835_spdif_default_get,
  107949. + .put = snd_bcm2835_spdif_default_put
  107950. + },
  107951. + {
  107952. + .access = SNDRV_CTL_ELEM_ACCESS_READ,
  107953. + .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  107954. + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
  107955. + .info = snd_bcm2835_spdif_mask_info,
  107956. + .get = snd_bcm2835_spdif_mask_get,
  107957. + },
  107958. + {
  107959. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  107960. + SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  107961. + .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  107962. + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
  107963. + .info = snd_bcm2835_spdif_stream_info,
  107964. + .get = snd_bcm2835_spdif_stream_get,
  107965. + .put = snd_bcm2835_spdif_stream_put,
  107966. + },
  107967. +};
  107968. +
  107969. +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip)
  107970. +{
  107971. + int err;
  107972. + unsigned int idx;
  107973. +
  107974. + strcpy(chip->card->mixername, "Broadcom Mixer");
  107975. + for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_ctl); idx++) {
  107976. + err =
  107977. + snd_ctl_add(chip->card,
  107978. + snd_ctl_new1(&snd_bcm2835_ctl[idx], chip));
  107979. + if (err < 0)
  107980. + return err;
  107981. + }
  107982. + for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_spdif); idx++) {
  107983. + err = snd_ctl_add(chip->card,
  107984. + snd_ctl_new1(&snd_bcm2835_spdif[idx], chip));
  107985. + if (err < 0)
  107986. + return err;
  107987. + }
  107988. + return 0;
  107989. +}
  107990. diff -Nur linux-3.12.33/sound/arm/bcm2835.h linux-3.12.33-rpi/sound/arm/bcm2835.h
  107991. --- linux-3.12.33/sound/arm/bcm2835.h 1969-12-31 18:00:00.000000000 -0600
  107992. +++ linux-3.12.33-rpi/sound/arm/bcm2835.h 2014-12-03 19:13:44.308418001 -0600
  107993. @@ -0,0 +1,167 @@
  107994. +/*****************************************************************************
  107995. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  107996. +*
  107997. +* Unless you and Broadcom execute a separate written software license
  107998. +* agreement governing use of this software, this software is licensed to you
  107999. +* under the terms of the GNU General Public License version 2, available at
  108000. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  108001. +*
  108002. +* Notwithstanding the above, under no circumstances may you combine this
  108003. +* software in any way with any other Broadcom software provided under a
  108004. +* license other than the GPL, without Broadcom's express prior written
  108005. +* consent.
  108006. +*****************************************************************************/
  108007. +
  108008. +#ifndef __SOUND_ARM_BCM2835_H
  108009. +#define __SOUND_ARM_BCM2835_H
  108010. +
  108011. +#include <linux/device.h>
  108012. +#include <linux/list.h>
  108013. +#include <linux/interrupt.h>
  108014. +#include <linux/wait.h>
  108015. +#include <sound/core.h>
  108016. +#include <sound/initval.h>
  108017. +#include <sound/pcm.h>
  108018. +#include <sound/pcm_params.h>
  108019. +#include <sound/pcm-indirect.h>
  108020. +#include <linux/workqueue.h>
  108021. +
  108022. +/*
  108023. +#define AUDIO_DEBUG_ENABLE
  108024. +#define AUDIO_VERBOSE_DEBUG_ENABLE
  108025. +*/
  108026. +
  108027. +/* Debug macros */
  108028. +
  108029. +#ifdef AUDIO_DEBUG_ENABLE
  108030. +#ifdef AUDIO_VERBOSE_DEBUG_ENABLE
  108031. +
  108032. +#define audio_debug(fmt, arg...) \
  108033. + printk(KERN_INFO"%s:%d " fmt, __func__, __LINE__, ##arg)
  108034. +
  108035. +#define audio_info(fmt, arg...) \
  108036. + printk(KERN_INFO"%s:%d " fmt, __func__, __LINE__, ##arg)
  108037. +
  108038. +#else
  108039. +
  108040. +#define audio_debug(fmt, arg...)
  108041. +
  108042. +#define audio_info(fmt, arg...)
  108043. +
  108044. +#endif /* AUDIO_VERBOSE_DEBUG_ENABLE */
  108045. +
  108046. +#else
  108047. +
  108048. +#define audio_debug(fmt, arg...)
  108049. +
  108050. +#define audio_info(fmt, arg...)
  108051. +
  108052. +#endif /* AUDIO_DEBUG_ENABLE */
  108053. +
  108054. +#define audio_error(fmt, arg...) \
  108055. + printk(KERN_ERR"%s:%d " fmt, __func__, __LINE__, ##arg)
  108056. +
  108057. +#define audio_warning(fmt, arg...) \
  108058. + printk(KERN_WARNING"%s:%d " fmt, __func__, __LINE__, ##arg)
  108059. +
  108060. +#define audio_alert(fmt, arg...) \
  108061. + printk(KERN_ALERT"%s:%d " fmt, __func__, __LINE__, ##arg)
  108062. +
  108063. +#define MAX_SUBSTREAMS (8)
  108064. +#define AVAIL_SUBSTREAMS_MASK (0xff)
  108065. +enum {
  108066. + CTRL_VOL_MUTE,
  108067. + CTRL_VOL_UNMUTE
  108068. +};
  108069. +
  108070. +/* macros for alsa2chip and chip2alsa, instead of functions */
  108071. +
  108072. +#define alsa2chip(vol) (uint)(-((vol << 8) / 100)) /* convert alsa to chip volume (defined as macro rather than function call) */
  108073. +#define chip2alsa(vol) -((vol * 100) >> 8) /* convert chip to alsa volume */
  108074. +
  108075. +/* Some constants for values .. */
  108076. +typedef enum {
  108077. + AUDIO_DEST_AUTO = 0,
  108078. + AUDIO_DEST_HEADPHONES = 1,
  108079. + AUDIO_DEST_HDMI = 2,
  108080. + AUDIO_DEST_MAX,
  108081. +} SND_BCM2835_ROUTE_T;
  108082. +
  108083. +typedef enum {
  108084. + PCM_PLAYBACK_VOLUME,
  108085. + PCM_PLAYBACK_MUTE,
  108086. + PCM_PLAYBACK_DEVICE,
  108087. +} SND_BCM2835_CTRL_T;
  108088. +
  108089. +/* definition of the chip-specific record */
  108090. +typedef struct bcm2835_chip {
  108091. + struct snd_card *card;
  108092. + struct snd_pcm *pcm;
  108093. + struct snd_pcm *pcm_spdif;
  108094. + /* Bitmat for valid reg_base and irq numbers */
  108095. + uint32_t avail_substreams;
  108096. + struct platform_device *pdev[MAX_SUBSTREAMS];
  108097. + struct bcm2835_alsa_stream *alsa_stream[MAX_SUBSTREAMS];
  108098. +
  108099. + int volume;
  108100. + int old_volume; /* stores the volume value whist muted */
  108101. + int dest;
  108102. + int mute;
  108103. +
  108104. + unsigned int opened;
  108105. + unsigned int spdif_status;
  108106. + struct mutex audio_mutex;
  108107. +} bcm2835_chip_t;
  108108. +
  108109. +typedef struct bcm2835_alsa_stream {
  108110. + bcm2835_chip_t *chip;
  108111. + struct snd_pcm_substream *substream;
  108112. + struct snd_pcm_indirect pcm_indirect;
  108113. +
  108114. + struct semaphore buffers_update_sem;
  108115. + struct semaphore control_sem;
  108116. + spinlock_t lock;
  108117. + volatile uint32_t control;
  108118. + volatile uint32_t status;
  108119. +
  108120. + int open;
  108121. + int running;
  108122. + int draining;
  108123. +
  108124. + int channels;
  108125. + int params_rate;
  108126. + int pcm_format_width;
  108127. +
  108128. + unsigned int pos;
  108129. + unsigned int buffer_size;
  108130. + unsigned int period_size;
  108131. +
  108132. + uint32_t enable_fifo_irq;
  108133. + irq_handler_t fifo_irq_handler;
  108134. +
  108135. + atomic_t retrieved;
  108136. + struct opaque_AUDIO_INSTANCE_T *instance;
  108137. + struct workqueue_struct *my_wq;
  108138. + int idx;
  108139. +} bcm2835_alsa_stream_t;
  108140. +
  108141. +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip);
  108142. +int snd_bcm2835_new_pcm(bcm2835_chip_t * chip);
  108143. +int snd_bcm2835_new_spdif_pcm(bcm2835_chip_t * chip);
  108144. +
  108145. +int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream);
  108146. +int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream);
  108147. +int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,
  108148. + uint32_t channels, uint32_t samplerate,
  108149. + uint32_t bps);
  108150. +int bcm2835_audio_setup(bcm2835_alsa_stream_t * alsa_stream);
  108151. +int bcm2835_audio_start(bcm2835_alsa_stream_t * alsa_stream);
  108152. +int bcm2835_audio_stop(bcm2835_alsa_stream_t * alsa_stream);
  108153. +int bcm2835_audio_set_ctls(bcm2835_chip_t * chip);
  108154. +int bcm2835_audio_write(bcm2835_alsa_stream_t * alsa_stream, uint32_t count,
  108155. + void *src);
  108156. +uint32_t bcm2835_audio_retrieve_buffers(bcm2835_alsa_stream_t * alsa_stream);
  108157. +void bcm2835_audio_flush_buffers(bcm2835_alsa_stream_t * alsa_stream);
  108158. +void bcm2835_audio_flush_playback_buffers(bcm2835_alsa_stream_t * alsa_stream);
  108159. +
  108160. +#endif /* __SOUND_ARM_BCM2835_H */
  108161. diff -Nur linux-3.12.33/sound/arm/bcm2835-pcm.c linux-3.12.33-rpi/sound/arm/bcm2835-pcm.c
  108162. --- linux-3.12.33/sound/arm/bcm2835-pcm.c 1969-12-31 18:00:00.000000000 -0600
  108163. +++ linux-3.12.33-rpi/sound/arm/bcm2835-pcm.c 2014-12-03 19:13:44.308418001 -0600
  108164. @@ -0,0 +1,552 @@
  108165. +/*****************************************************************************
  108166. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  108167. +*
  108168. +* Unless you and Broadcom execute a separate written software license
  108169. +* agreement governing use of this software, this software is licensed to you
  108170. +* under the terms of the GNU General Public License version 2, available at
  108171. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  108172. +*
  108173. +* Notwithstanding the above, under no circumstances may you combine this
  108174. +* software in any way with any other Broadcom software provided under a
  108175. +* license other than the GPL, without Broadcom's express prior written
  108176. +* consent.
  108177. +*****************************************************************************/
  108178. +
  108179. +#include <linux/interrupt.h>
  108180. +#include <linux/slab.h>
  108181. +
  108182. +#include <sound/asoundef.h>
  108183. +
  108184. +#include "bcm2835.h"
  108185. +
  108186. +/* hardware definition */
  108187. +static struct snd_pcm_hardware snd_bcm2835_playback_hw = {
  108188. + .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  108189. + SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
  108190. + .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  108191. + .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  108192. + .rate_min = 8000,
  108193. + .rate_max = 48000,
  108194. + .channels_min = 1,
  108195. + .channels_max = 2,
  108196. + .buffer_bytes_max = 128 * 1024,
  108197. + .period_bytes_min = 1 * 1024,
  108198. + .period_bytes_max = 128 * 1024,
  108199. + .periods_min = 1,
  108200. + .periods_max = 128,
  108201. +};
  108202. +
  108203. +static struct snd_pcm_hardware snd_bcm2835_playback_spdif_hw = {
  108204. + .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  108205. + SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
  108206. + .formats = SNDRV_PCM_FMTBIT_S16_LE,
  108207. + .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_44100 |
  108208. + SNDRV_PCM_RATE_48000,
  108209. + .rate_min = 44100,
  108210. + .rate_max = 48000,
  108211. + .channels_min = 2,
  108212. + .channels_max = 2,
  108213. + .buffer_bytes_max = 128 * 1024,
  108214. + .period_bytes_min = 1 * 1024,
  108215. + .period_bytes_max = 128 * 1024,
  108216. + .periods_min = 1,
  108217. + .periods_max = 128,
  108218. +};
  108219. +
  108220. +static void snd_bcm2835_playback_free(struct snd_pcm_runtime *runtime)
  108221. +{
  108222. + audio_info("Freeing up alsa stream here ..\n");
  108223. + if (runtime->private_data)
  108224. + kfree(runtime->private_data);
  108225. + runtime->private_data = NULL;
  108226. +}
  108227. +
  108228. +static irqreturn_t bcm2835_playback_fifo_irq(int irq, void *dev_id)
  108229. +{
  108230. + bcm2835_alsa_stream_t *alsa_stream = (bcm2835_alsa_stream_t *) dev_id;
  108231. + uint32_t consumed = 0;
  108232. + int new_period = 0;
  108233. +
  108234. + audio_info(" .. IN\n");
  108235. +
  108236. + audio_info("alsa_stream=%p substream=%p\n", alsa_stream,
  108237. + alsa_stream ? alsa_stream->substream : 0);
  108238. +
  108239. + if (alsa_stream->open)
  108240. + consumed = bcm2835_audio_retrieve_buffers(alsa_stream);
  108241. +
  108242. + /* We get called only if playback was triggered, So, the number of buffers we retrieve in
  108243. + * each iteration are the buffers that have been played out already
  108244. + */
  108245. +
  108246. + if (alsa_stream->period_size) {
  108247. + if ((alsa_stream->pos / alsa_stream->period_size) !=
  108248. + ((alsa_stream->pos + consumed) / alsa_stream->period_size))
  108249. + new_period = 1;
  108250. + }
  108251. + audio_debug("updating pos cur: %d + %d max:%d period_bytes:%d, hw_ptr: %d new_period:%d\n",
  108252. + alsa_stream->pos,
  108253. + consumed,
  108254. + alsa_stream->buffer_size,
  108255. + (int)(alsa_stream->period_size*alsa_stream->substream->runtime->periods),
  108256. + frames_to_bytes(alsa_stream->substream->runtime, alsa_stream->substream->runtime->status->hw_ptr),
  108257. + new_period);
  108258. + if (alsa_stream->buffer_size) {
  108259. + alsa_stream->pos += consumed &~ (1<<30);
  108260. + alsa_stream->pos %= alsa_stream->buffer_size;
  108261. + }
  108262. +
  108263. + if (alsa_stream->substream) {
  108264. + if (new_period)
  108265. + snd_pcm_period_elapsed(alsa_stream->substream);
  108266. + } else {
  108267. + audio_warning(" unexpected NULL substream\n");
  108268. + }
  108269. + audio_info(" .. OUT\n");
  108270. +
  108271. + return IRQ_HANDLED;
  108272. +}
  108273. +
  108274. +/* open callback */
  108275. +static int snd_bcm2835_playback_open_generic(
  108276. + struct snd_pcm_substream *substream, int spdif)
  108277. +{
  108278. + bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
  108279. + struct snd_pcm_runtime *runtime = substream->runtime;
  108280. + bcm2835_alsa_stream_t *alsa_stream;
  108281. + int idx;
  108282. + int err;
  108283. +
  108284. + audio_info(" .. IN (%d)\n", substream->number);
  108285. +
  108286. + if(mutex_lock_interruptible(&chip->audio_mutex))
  108287. + {
  108288. + audio_error("Interrupted whilst waiting for lock\n");
  108289. + return -EINTR;
  108290. + }
  108291. + audio_info("Alsa open (%d)\n", substream->number);
  108292. + idx = substream->number;
  108293. +
  108294. + if (spdif && chip->opened != 0)
  108295. + return -EBUSY;
  108296. + else if (!spdif && (chip->opened & (1 << idx)))
  108297. + return -EBUSY;
  108298. +
  108299. + if (idx > MAX_SUBSTREAMS) {
  108300. + audio_error
  108301. + ("substream(%d) device doesn't exist max(%d) substreams allowed\n",
  108302. + idx, MAX_SUBSTREAMS);
  108303. + err = -ENODEV;
  108304. + goto out;
  108305. + }
  108306. +
  108307. + /* Check if we are ready */
  108308. + if (!(chip->avail_substreams & (1 << idx))) {
  108309. + /* We are not ready yet */
  108310. + audio_error("substream(%d) device is not ready yet\n", idx);
  108311. + err = -EAGAIN;
  108312. + goto out;
  108313. + }
  108314. +
  108315. + alsa_stream = kzalloc(sizeof(bcm2835_alsa_stream_t), GFP_KERNEL);
  108316. + if (alsa_stream == NULL) {
  108317. + err = -ENOMEM;
  108318. + goto out;
  108319. + }
  108320. +
  108321. + /* Initialise alsa_stream */
  108322. + alsa_stream->chip = chip;
  108323. + alsa_stream->substream = substream;
  108324. + alsa_stream->idx = idx;
  108325. +
  108326. + sema_init(&alsa_stream->buffers_update_sem, 0);
  108327. + sema_init(&alsa_stream->control_sem, 0);
  108328. + spin_lock_init(&alsa_stream->lock);
  108329. +
  108330. + /* Enabled in start trigger, called on each "fifo irq" after that */
  108331. + alsa_stream->enable_fifo_irq = 0;
  108332. + alsa_stream->fifo_irq_handler = bcm2835_playback_fifo_irq;
  108333. +
  108334. + err = bcm2835_audio_open(alsa_stream);
  108335. + if (err != 0) {
  108336. + kfree(alsa_stream);
  108337. + return err;
  108338. + }
  108339. + runtime->private_data = alsa_stream;
  108340. + runtime->private_free = snd_bcm2835_playback_free;
  108341. + if (spdif) {
  108342. + runtime->hw = snd_bcm2835_playback_spdif_hw;
  108343. + } else {
  108344. + /* clear spdif status, as we are not in spdif mode */
  108345. + chip->spdif_status = 0;
  108346. + runtime->hw = snd_bcm2835_playback_hw;
  108347. + }
  108348. + /* minimum 16 bytes alignment (for vchiq bulk transfers) */
  108349. + snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  108350. + 16);
  108351. +
  108352. + chip->alsa_stream[idx] = alsa_stream;
  108353. +
  108354. + chip->opened |= (1 << idx);
  108355. + alsa_stream->open = 1;
  108356. + alsa_stream->draining = 1;
  108357. +
  108358. +out:
  108359. + mutex_unlock(&chip->audio_mutex);
  108360. +
  108361. + audio_info(" .. OUT =%d\n", err);
  108362. +
  108363. + return err;
  108364. +}
  108365. +
  108366. +static int snd_bcm2835_playback_open(struct snd_pcm_substream *substream)
  108367. +{
  108368. + return snd_bcm2835_playback_open_generic(substream, 0);
  108369. +}
  108370. +
  108371. +static int snd_bcm2835_playback_spdif_open(struct snd_pcm_substream *substream)
  108372. +{
  108373. + return snd_bcm2835_playback_open_generic(substream, 1);
  108374. +}
  108375. +
  108376. +/* close callback */
  108377. +static int snd_bcm2835_playback_close(struct snd_pcm_substream *substream)
  108378. +{
  108379. + /* the hardware-specific codes will be here */
  108380. +
  108381. + bcm2835_chip_t *chip;
  108382. + struct snd_pcm_runtime *runtime;
  108383. + bcm2835_alsa_stream_t *alsa_stream;
  108384. +
  108385. + audio_info(" .. IN\n");
  108386. +
  108387. + chip = snd_pcm_substream_chip(substream);
  108388. + if(mutex_lock_interruptible(&chip->audio_mutex))
  108389. + {
  108390. + audio_error("Interrupted whilst waiting for lock\n");
  108391. + return -EINTR;
  108392. + }
  108393. + runtime = substream->runtime;
  108394. + alsa_stream = runtime->private_data;
  108395. +
  108396. + audio_info("Alsa close\n");
  108397. +
  108398. + /*
  108399. + * Call stop if it's still running. This happens when app
  108400. + * is force killed and we don't get a stop trigger.
  108401. + */
  108402. + if (alsa_stream->running) {
  108403. + int err;
  108404. + err = bcm2835_audio_stop(alsa_stream);
  108405. + alsa_stream->running = 0;
  108406. + if (err != 0)
  108407. + audio_error(" Failed to STOP alsa device\n");
  108408. + }
  108409. +
  108410. + alsa_stream->period_size = 0;
  108411. + alsa_stream->buffer_size = 0;
  108412. +
  108413. + if (alsa_stream->open) {
  108414. + alsa_stream->open = 0;
  108415. + bcm2835_audio_close(alsa_stream);
  108416. + }
  108417. + if (alsa_stream->chip)
  108418. + alsa_stream->chip->alsa_stream[alsa_stream->idx] = NULL;
  108419. + /*
  108420. + * Do not free up alsa_stream here, it will be freed up by
  108421. + * runtime->private_free callback we registered in *_open above
  108422. + */
  108423. +
  108424. + chip->opened &= ~(1 << substream->number);
  108425. +
  108426. + mutex_unlock(&chip->audio_mutex);
  108427. + audio_info(" .. OUT\n");
  108428. +
  108429. + return 0;
  108430. +}
  108431. +
  108432. +/* hw_params callback */
  108433. +static int snd_bcm2835_pcm_hw_params(struct snd_pcm_substream *substream,
  108434. + struct snd_pcm_hw_params *params)
  108435. +{
  108436. + struct snd_pcm_runtime *runtime = substream->runtime;
  108437. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  108438. + int err;
  108439. +
  108440. + audio_info(" .. IN\n");
  108441. +
  108442. + err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  108443. + if (err < 0) {
  108444. + audio_error
  108445. + (" pcm_lib_malloc failed to allocated pages for buffers\n");
  108446. + return err;
  108447. + }
  108448. +
  108449. + alsa_stream->channels = params_channels(params);
  108450. + alsa_stream->params_rate = params_rate(params);
  108451. + alsa_stream->pcm_format_width = snd_pcm_format_width(params_format (params));
  108452. + audio_info(" .. OUT\n");
  108453. +
  108454. + return err;
  108455. +}
  108456. +
  108457. +/* hw_free callback */
  108458. +static int snd_bcm2835_pcm_hw_free(struct snd_pcm_substream *substream)
  108459. +{
  108460. + audio_info(" .. IN\n");
  108461. + return snd_pcm_lib_free_pages(substream);
  108462. +}
  108463. +
  108464. +/* prepare callback */
  108465. +static int snd_bcm2835_pcm_prepare(struct snd_pcm_substream *substream)
  108466. +{
  108467. + bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
  108468. + struct snd_pcm_runtime *runtime = substream->runtime;
  108469. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  108470. + int channels;
  108471. + int err;
  108472. +
  108473. + audio_info(" .. IN\n");
  108474. +
  108475. + /* notify the vchiq that it should enter spdif passthrough mode by
  108476. + * setting channels=0 (see
  108477. + * https://github.com/raspberrypi/linux/issues/528) */
  108478. + if (chip->spdif_status & IEC958_AES0_NONAUDIO)
  108479. + channels = 0;
  108480. + else
  108481. + channels = alsa_stream->channels;
  108482. +
  108483. + err = bcm2835_audio_set_params(alsa_stream, channels,
  108484. + alsa_stream->params_rate,
  108485. + alsa_stream->pcm_format_width);
  108486. + if (err < 0) {
  108487. + audio_error(" error setting hw params\n");
  108488. + }
  108489. +
  108490. + bcm2835_audio_setup(alsa_stream);
  108491. +
  108492. + /* in preparation of the stream, set the controls (volume level) of the stream */
  108493. + bcm2835_audio_set_ctls(alsa_stream->chip);
  108494. +
  108495. +
  108496. + memset(&alsa_stream->pcm_indirect, 0, sizeof(alsa_stream->pcm_indirect));
  108497. +
  108498. + alsa_stream->pcm_indirect.hw_buffer_size =
  108499. + alsa_stream->pcm_indirect.sw_buffer_size =
  108500. + snd_pcm_lib_buffer_bytes(substream);
  108501. +
  108502. + alsa_stream->buffer_size = snd_pcm_lib_buffer_bytes(substream);
  108503. + alsa_stream->period_size = snd_pcm_lib_period_bytes(substream);
  108504. + alsa_stream->pos = 0;
  108505. +
  108506. + audio_debug("buffer_size=%d, period_size=%d pos=%d frame_bits=%d\n",
  108507. + alsa_stream->buffer_size, alsa_stream->period_size,
  108508. + alsa_stream->pos, runtime->frame_bits);
  108509. +
  108510. + audio_info(" .. OUT\n");
  108511. + return 0;
  108512. +}
  108513. +
  108514. +static void snd_bcm2835_pcm_transfer(struct snd_pcm_substream *substream,
  108515. + struct snd_pcm_indirect *rec, size_t bytes)
  108516. +{
  108517. + struct snd_pcm_runtime *runtime = substream->runtime;
  108518. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  108519. + void *src = (void *)(substream->runtime->dma_area + rec->sw_data);
  108520. + int err;
  108521. +
  108522. + err = bcm2835_audio_write(alsa_stream, bytes, src);
  108523. + if (err)
  108524. + audio_error(" Failed to transfer to alsa device (%d)\n", err);
  108525. +
  108526. +}
  108527. +
  108528. +static int snd_bcm2835_pcm_ack(struct snd_pcm_substream *substream)
  108529. +{
  108530. + struct snd_pcm_runtime *runtime = substream->runtime;
  108531. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  108532. + struct snd_pcm_indirect *pcm_indirect = &alsa_stream->pcm_indirect;
  108533. +
  108534. + pcm_indirect->hw_queue_size = runtime->hw.buffer_bytes_max;
  108535. + snd_pcm_indirect_playback_transfer(substream, pcm_indirect,
  108536. + snd_bcm2835_pcm_transfer);
  108537. + return 0;
  108538. +}
  108539. +
  108540. +/* trigger callback */
  108541. +static int snd_bcm2835_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  108542. +{
  108543. + struct snd_pcm_runtime *runtime = substream->runtime;
  108544. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  108545. + int err = 0;
  108546. +
  108547. + audio_info(" .. IN\n");
  108548. +
  108549. + switch (cmd) {
  108550. + case SNDRV_PCM_TRIGGER_START:
  108551. + audio_debug("bcm2835_AUDIO_TRIGGER_START running=%d\n",
  108552. + alsa_stream->running);
  108553. + if (!alsa_stream->running) {
  108554. + err = bcm2835_audio_start(alsa_stream);
  108555. + if (err == 0) {
  108556. + alsa_stream->pcm_indirect.hw_io =
  108557. + alsa_stream->pcm_indirect.hw_data =
  108558. + bytes_to_frames(runtime,
  108559. + alsa_stream->pos);
  108560. + substream->ops->ack(substream);
  108561. + alsa_stream->running = 1;
  108562. + alsa_stream->draining = 1;
  108563. + } else {
  108564. + audio_error(" Failed to START alsa device (%d)\n", err);
  108565. + }
  108566. + }
  108567. + break;
  108568. + case SNDRV_PCM_TRIGGER_STOP:
  108569. + audio_debug
  108570. + ("bcm2835_AUDIO_TRIGGER_STOP running=%d draining=%d\n",
  108571. + alsa_stream->running, runtime->status->state == SNDRV_PCM_STATE_DRAINING);
  108572. + if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  108573. + audio_info("DRAINING\n");
  108574. + alsa_stream->draining = 1;
  108575. + } else {
  108576. + audio_info("DROPPING\n");
  108577. + alsa_stream->draining = 0;
  108578. + }
  108579. + if (alsa_stream->running) {
  108580. + err = bcm2835_audio_stop(alsa_stream);
  108581. + if (err != 0)
  108582. + audio_error(" Failed to STOP alsa device (%d)\n", err);
  108583. + alsa_stream->running = 0;
  108584. + }
  108585. + break;
  108586. + default:
  108587. + err = -EINVAL;
  108588. + }
  108589. +
  108590. + audio_info(" .. OUT\n");
  108591. + return err;
  108592. +}
  108593. +
  108594. +/* pointer callback */
  108595. +static snd_pcm_uframes_t
  108596. +snd_bcm2835_pcm_pointer(struct snd_pcm_substream *substream)
  108597. +{
  108598. + struct snd_pcm_runtime *runtime = substream->runtime;
  108599. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  108600. +
  108601. + audio_info(" .. IN\n");
  108602. +
  108603. + audio_debug("pcm_pointer... (%d) hwptr=%d appl=%d pos=%d\n", 0,
  108604. + frames_to_bytes(runtime, runtime->status->hw_ptr),
  108605. + frames_to_bytes(runtime, runtime->control->appl_ptr),
  108606. + alsa_stream->pos);
  108607. +
  108608. + audio_info(" .. OUT\n");
  108609. + return snd_pcm_indirect_playback_pointer(substream,
  108610. + &alsa_stream->pcm_indirect,
  108611. + alsa_stream->pos);
  108612. +}
  108613. +
  108614. +static int snd_bcm2835_pcm_lib_ioctl(struct snd_pcm_substream *substream,
  108615. + unsigned int cmd, void *arg)
  108616. +{
  108617. + int ret = snd_pcm_lib_ioctl(substream, cmd, arg);
  108618. + audio_info(" .. substream=%p, cmd=%d, arg=%p (%x) ret=%d\n", substream,
  108619. + cmd, arg, arg ? *(unsigned *)arg : 0, ret);
  108620. + return ret;
  108621. +}
  108622. +
  108623. +/* operators */
  108624. +static struct snd_pcm_ops snd_bcm2835_playback_ops = {
  108625. + .open = snd_bcm2835_playback_open,
  108626. + .close = snd_bcm2835_playback_close,
  108627. + .ioctl = snd_bcm2835_pcm_lib_ioctl,
  108628. + .hw_params = snd_bcm2835_pcm_hw_params,
  108629. + .hw_free = snd_bcm2835_pcm_hw_free,
  108630. + .prepare = snd_bcm2835_pcm_prepare,
  108631. + .trigger = snd_bcm2835_pcm_trigger,
  108632. + .pointer = snd_bcm2835_pcm_pointer,
  108633. + .ack = snd_bcm2835_pcm_ack,
  108634. +};
  108635. +
  108636. +static struct snd_pcm_ops snd_bcm2835_playback_spdif_ops = {
  108637. + .open = snd_bcm2835_playback_spdif_open,
  108638. + .close = snd_bcm2835_playback_close,
  108639. + .ioctl = snd_bcm2835_pcm_lib_ioctl,
  108640. + .hw_params = snd_bcm2835_pcm_hw_params,
  108641. + .hw_free = snd_bcm2835_pcm_hw_free,
  108642. + .prepare = snd_bcm2835_pcm_prepare,
  108643. + .trigger = snd_bcm2835_pcm_trigger,
  108644. + .pointer = snd_bcm2835_pcm_pointer,
  108645. + .ack = snd_bcm2835_pcm_ack,
  108646. +};
  108647. +
  108648. +/* create a pcm device */
  108649. +int snd_bcm2835_new_pcm(bcm2835_chip_t * chip)
  108650. +{
  108651. + struct snd_pcm *pcm;
  108652. + int err;
  108653. +
  108654. + audio_info(" .. IN\n");
  108655. + mutex_init(&chip->audio_mutex);
  108656. + if(mutex_lock_interruptible(&chip->audio_mutex))
  108657. + {
  108658. + audio_error("Interrupted whilst waiting for lock\n");
  108659. + return -EINTR;
  108660. + }
  108661. + err =
  108662. + snd_pcm_new(chip->card, "bcm2835 ALSA", 0, MAX_SUBSTREAMS, 0, &pcm);
  108663. + if (err < 0)
  108664. + return err;
  108665. + pcm->private_data = chip;
  108666. + strcpy(pcm->name, "bcm2835 ALSA");
  108667. + chip->pcm = pcm;
  108668. + chip->dest = AUDIO_DEST_AUTO;
  108669. + chip->volume = alsa2chip(0);
  108670. + chip->mute = CTRL_VOL_UNMUTE; /*disable mute on startup */
  108671. + /* set operators */
  108672. + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  108673. + &snd_bcm2835_playback_ops);
  108674. +
  108675. + /* pre-allocation of buffers */
  108676. + /* NOTE: this may fail */
  108677. + snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  108678. + snd_dma_continuous_data
  108679. + (GFP_KERNEL), 64 * 1024,
  108680. + 64 * 1024);
  108681. +
  108682. + mutex_unlock(&chip->audio_mutex);
  108683. + audio_info(" .. OUT\n");
  108684. +
  108685. + return 0;
  108686. +}
  108687. +
  108688. +int snd_bcm2835_new_spdif_pcm(bcm2835_chip_t * chip)
  108689. +{
  108690. + struct snd_pcm *pcm;
  108691. + int err;
  108692. +
  108693. + audio_info(" .. IN\n");
  108694. + if(mutex_lock_interruptible(&chip->audio_mutex))
  108695. + {
  108696. + audio_error("Interrupted whilst waiting for lock\n");
  108697. + return -EINTR;
  108698. + }
  108699. + err = snd_pcm_new(chip->card, "bcm2835 ALSA", 1, 1, 0, &pcm);
  108700. + if (err < 0)
  108701. + return err;
  108702. +
  108703. + pcm->private_data = chip;
  108704. + strcpy(pcm->name, "bcm2835 IEC958/HDMI");
  108705. + chip->pcm_spdif = pcm;
  108706. + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  108707. + &snd_bcm2835_playback_spdif_ops);
  108708. +
  108709. + snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  108710. + snd_dma_continuous_data (GFP_KERNEL),
  108711. + 64 * 1024, 64 * 1024);
  108712. + mutex_unlock(&chip->audio_mutex);
  108713. + audio_info(" .. OUT\n");
  108714. +
  108715. + return 0;
  108716. +}
  108717. diff -Nur linux-3.12.33/sound/arm/bcm2835-vchiq.c linux-3.12.33-rpi/sound/arm/bcm2835-vchiq.c
  108718. --- linux-3.12.33/sound/arm/bcm2835-vchiq.c 1969-12-31 18:00:00.000000000 -0600
  108719. +++ linux-3.12.33-rpi/sound/arm/bcm2835-vchiq.c 2014-12-03 19:13:44.308418001 -0600
  108720. @@ -0,0 +1,901 @@
  108721. +/*****************************************************************************
  108722. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  108723. +*
  108724. +* Unless you and Broadcom execute a separate written software license
  108725. +* agreement governing use of this software, this software is licensed to you
  108726. +* under the terms of the GNU General Public License version 2, available at
  108727. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  108728. +*
  108729. +* Notwithstanding the above, under no circumstances may you combine this
  108730. +* software in any way with any other Broadcom software provided under a
  108731. +* license other than the GPL, without Broadcom's express prior written
  108732. +* consent.
  108733. +*****************************************************************************/
  108734. +
  108735. +#include <linux/device.h>
  108736. +#include <sound/core.h>
  108737. +#include <sound/initval.h>
  108738. +#include <sound/pcm.h>
  108739. +#include <linux/io.h>
  108740. +#include <linux/interrupt.h>
  108741. +#include <linux/fs.h>
  108742. +#include <linux/file.h>
  108743. +#include <linux/mm.h>
  108744. +#include <linux/syscalls.h>
  108745. +#include <asm/uaccess.h>
  108746. +#include <linux/slab.h>
  108747. +#include <linux/delay.h>
  108748. +#include <linux/atomic.h>
  108749. +#include <linux/module.h>
  108750. +#include <linux/completion.h>
  108751. +
  108752. +#include "bcm2835.h"
  108753. +
  108754. +/* ---- Include Files -------------------------------------------------------- */
  108755. +
  108756. +#include "interface/vchi/vchi.h"
  108757. +#include "vc_vchi_audioserv_defs.h"
  108758. +
  108759. +/* ---- Private Constants and Types ------------------------------------------ */
  108760. +
  108761. +#define BCM2835_AUDIO_STOP 0
  108762. +#define BCM2835_AUDIO_START 1
  108763. +#define BCM2835_AUDIO_WRITE 2
  108764. +
  108765. +/* Logging macros (for remapping to other logging mechanisms, i.e., printf) */
  108766. +#ifdef AUDIO_DEBUG_ENABLE
  108767. + #define LOG_ERR( fmt, arg... ) pr_err( "%s:%d " fmt, __func__, __LINE__, ##arg)
  108768. + #define LOG_WARN( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
  108769. + #define LOG_INFO( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
  108770. + #define LOG_DBG( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
  108771. +#else
  108772. + #define LOG_ERR( fmt, arg... ) pr_err( "%s:%d " fmt, __func__, __LINE__, ##arg)
  108773. + #define LOG_WARN( fmt, arg... )
  108774. + #define LOG_INFO( fmt, arg... )
  108775. + #define LOG_DBG( fmt, arg... )
  108776. +#endif
  108777. +
  108778. +typedef struct opaque_AUDIO_INSTANCE_T {
  108779. + uint32_t num_connections;
  108780. + VCHI_SERVICE_HANDLE_T vchi_handle[VCHI_MAX_NUM_CONNECTIONS];
  108781. + struct completion msg_avail_comp;
  108782. + struct mutex vchi_mutex;
  108783. + bcm2835_alsa_stream_t *alsa_stream;
  108784. + int32_t result;
  108785. + short peer_version;
  108786. +} AUDIO_INSTANCE_T;
  108787. +
  108788. +bool force_bulk = false;
  108789. +
  108790. +/* ---- Private Variables ---------------------------------------------------- */
  108791. +
  108792. +/* ---- Private Function Prototypes ------------------------------------------ */
  108793. +
  108794. +/* ---- Private Functions ---------------------------------------------------- */
  108795. +
  108796. +static int bcm2835_audio_stop_worker(bcm2835_alsa_stream_t * alsa_stream);
  108797. +static int bcm2835_audio_start_worker(bcm2835_alsa_stream_t * alsa_stream);
  108798. +static int bcm2835_audio_write_worker(bcm2835_alsa_stream_t *alsa_stream,
  108799. + uint32_t count, void *src);
  108800. +
  108801. +typedef struct {
  108802. + struct work_struct my_work;
  108803. + bcm2835_alsa_stream_t *alsa_stream;
  108804. + int cmd;
  108805. + void *src;
  108806. + uint32_t count;
  108807. +} my_work_t;
  108808. +
  108809. +static void my_wq_function(struct work_struct *work)
  108810. +{
  108811. + my_work_t *w = (my_work_t *) work;
  108812. + int ret = -9;
  108813. + LOG_DBG(" .. IN %p:%d\n", w->alsa_stream, w->cmd);
  108814. + switch (w->cmd) {
  108815. + case BCM2835_AUDIO_START:
  108816. + ret = bcm2835_audio_start_worker(w->alsa_stream);
  108817. + break;
  108818. + case BCM2835_AUDIO_STOP:
  108819. + ret = bcm2835_audio_stop_worker(w->alsa_stream);
  108820. + break;
  108821. + case BCM2835_AUDIO_WRITE:
  108822. + ret = bcm2835_audio_write_worker(w->alsa_stream, w->count,
  108823. + w->src);
  108824. + break;
  108825. + default:
  108826. + LOG_ERR(" Unexpected work: %p:%d\n", w->alsa_stream, w->cmd);
  108827. + break;
  108828. + }
  108829. + kfree((void *)work);
  108830. + LOG_DBG(" .. OUT %d\n", ret);
  108831. +}
  108832. +
  108833. +int bcm2835_audio_start(bcm2835_alsa_stream_t * alsa_stream)
  108834. +{
  108835. + int ret = -1;
  108836. + LOG_DBG(" .. IN\n");
  108837. + if (alsa_stream->my_wq) {
  108838. + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
  108839. + /*--- Queue some work (item 1) ---*/
  108840. + if (work) {
  108841. + INIT_WORK((struct work_struct *)work, my_wq_function);
  108842. + work->alsa_stream = alsa_stream;
  108843. + work->cmd = BCM2835_AUDIO_START;
  108844. + if (queue_work
  108845. + (alsa_stream->my_wq, (struct work_struct *)work))
  108846. + ret = 0;
  108847. + } else
  108848. + LOG_ERR(" .. Error: NULL work kmalloc\n");
  108849. + }
  108850. + LOG_DBG(" .. OUT %d\n", ret);
  108851. + return ret;
  108852. +}
  108853. +
  108854. +int bcm2835_audio_stop(bcm2835_alsa_stream_t * alsa_stream)
  108855. +{
  108856. + int ret = -1;
  108857. + LOG_DBG(" .. IN\n");
  108858. + if (alsa_stream->my_wq) {
  108859. + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
  108860. + /*--- Queue some work (item 1) ---*/
  108861. + if (work) {
  108862. + INIT_WORK((struct work_struct *)work, my_wq_function);
  108863. + work->alsa_stream = alsa_stream;
  108864. + work->cmd = BCM2835_AUDIO_STOP;
  108865. + if (queue_work
  108866. + (alsa_stream->my_wq, (struct work_struct *)work))
  108867. + ret = 0;
  108868. + } else
  108869. + LOG_ERR(" .. Error: NULL work kmalloc\n");
  108870. + }
  108871. + LOG_DBG(" .. OUT %d\n", ret);
  108872. + return ret;
  108873. +}
  108874. +
  108875. +int bcm2835_audio_write(bcm2835_alsa_stream_t *alsa_stream,
  108876. + uint32_t count, void *src)
  108877. +{
  108878. + int ret = -1;
  108879. + LOG_DBG(" .. IN\n");
  108880. + if (alsa_stream->my_wq) {
  108881. + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
  108882. + /*--- Queue some work (item 1) ---*/
  108883. + if (work) {
  108884. + INIT_WORK((struct work_struct *)work, my_wq_function);
  108885. + work->alsa_stream = alsa_stream;
  108886. + work->cmd = BCM2835_AUDIO_WRITE;
  108887. + work->src = src;
  108888. + work->count = count;
  108889. + if (queue_work
  108890. + (alsa_stream->my_wq, (struct work_struct *)work))
  108891. + ret = 0;
  108892. + } else
  108893. + LOG_ERR(" .. Error: NULL work kmalloc\n");
  108894. + }
  108895. + LOG_DBG(" .. OUT %d\n", ret);
  108896. + return ret;
  108897. +}
  108898. +
  108899. +void my_workqueue_init(bcm2835_alsa_stream_t * alsa_stream)
  108900. +{
  108901. + alsa_stream->my_wq = alloc_workqueue("my_queue", WQ_HIGHPRI, 1);
  108902. + return;
  108903. +}
  108904. +
  108905. +void my_workqueue_quit(bcm2835_alsa_stream_t * alsa_stream)
  108906. +{
  108907. + if (alsa_stream->my_wq) {
  108908. + flush_workqueue(alsa_stream->my_wq);
  108909. + destroy_workqueue(alsa_stream->my_wq);
  108910. + alsa_stream->my_wq = NULL;
  108911. + }
  108912. + return;
  108913. +}
  108914. +
  108915. +static void audio_vchi_callback(void *param,
  108916. + const VCHI_CALLBACK_REASON_T reason,
  108917. + void *msg_handle)
  108918. +{
  108919. + AUDIO_INSTANCE_T *instance = (AUDIO_INSTANCE_T *) param;
  108920. + int32_t status;
  108921. + int32_t msg_len;
  108922. + VC_AUDIO_MSG_T m;
  108923. + LOG_DBG(" .. IN instance=%p, handle=%p, alsa=%p, reason=%d, handle=%p\n",
  108924. + instance, instance ? instance->vchi_handle[0] : NULL, instance ? instance->alsa_stream : NULL, reason, msg_handle);
  108925. +
  108926. + if (reason != VCHI_CALLBACK_MSG_AVAILABLE) {
  108927. + return;
  108928. + }
  108929. + if (!instance) {
  108930. + LOG_ERR(" .. instance is null\n");
  108931. + BUG();
  108932. + return;
  108933. + }
  108934. + if (!instance->vchi_handle[0]) {
  108935. + LOG_ERR(" .. instance->vchi_handle[0] is null\n");
  108936. + BUG();
  108937. + return;
  108938. + }
  108939. + status = vchi_msg_dequeue(instance->vchi_handle[0],
  108940. + &m, sizeof m, &msg_len, VCHI_FLAGS_NONE);
  108941. + if (m.type == VC_AUDIO_MSG_TYPE_RESULT) {
  108942. + LOG_DBG
  108943. + (" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_RESULT, success=%d\n",
  108944. + instance, m.u.result.success);
  108945. + instance->result = m.u.result.success;
  108946. + complete(&instance->msg_avail_comp);
  108947. + } else if (m.type == VC_AUDIO_MSG_TYPE_COMPLETE) {
  108948. + bcm2835_alsa_stream_t *alsa_stream = instance->alsa_stream;
  108949. + irq_handler_t callback = (irq_handler_t) m.u.complete.callback;
  108950. + LOG_DBG
  108951. + (" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_COMPLETE, complete=%d\n",
  108952. + instance, m.u.complete.count);
  108953. + if (alsa_stream && callback) {
  108954. + atomic_add(m.u.complete.count, &alsa_stream->retrieved);
  108955. + callback(0, alsa_stream);
  108956. + } else {
  108957. + LOG_ERR(" .. unexpected alsa_stream=%p, callback=%p\n",
  108958. + alsa_stream, callback);
  108959. + }
  108960. + } else {
  108961. + LOG_ERR(" .. unexpected m.type=%d\n", m.type);
  108962. + }
  108963. + LOG_DBG(" .. OUT\n");
  108964. +}
  108965. +
  108966. +static AUDIO_INSTANCE_T *vc_vchi_audio_init(VCHI_INSTANCE_T vchi_instance,
  108967. + VCHI_CONNECTION_T **
  108968. + vchi_connections,
  108969. + uint32_t num_connections)
  108970. +{
  108971. + uint32_t i;
  108972. + AUDIO_INSTANCE_T *instance;
  108973. + int status;
  108974. +
  108975. + LOG_DBG("%s: start", __func__);
  108976. +
  108977. + if (num_connections > VCHI_MAX_NUM_CONNECTIONS) {
  108978. + LOG_ERR("%s: unsupported number of connections %u (max=%u)\n",
  108979. + __func__, num_connections, VCHI_MAX_NUM_CONNECTIONS);
  108980. +
  108981. + return NULL;
  108982. + }
  108983. + /* Allocate memory for this instance */
  108984. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  108985. + if (!instance)
  108986. + return NULL;
  108987. +
  108988. + instance->num_connections = num_connections;
  108989. +
  108990. + /* Create a lock for exclusive, serialized VCHI connection access */
  108991. + mutex_init(&instance->vchi_mutex);
  108992. + /* Open the VCHI service connections */
  108993. + for (i = 0; i < num_connections; i++) {
  108994. + SERVICE_CREATION_T params = {
  108995. + VCHI_VERSION_EX(VC_AUDIOSERV_VER, VC_AUDIOSERV_MIN_VER),
  108996. + VC_AUDIO_SERVER_NAME, // 4cc service code
  108997. + vchi_connections[i], // passed in fn pointers
  108998. + 0, // rx fifo size (unused)
  108999. + 0, // tx fifo size (unused)
  109000. + audio_vchi_callback, // service callback
  109001. + instance, // service callback parameter
  109002. + 1, //TODO: remove VCOS_FALSE, // unaligned bulk recieves
  109003. + 1, //TODO: remove VCOS_FALSE, // unaligned bulk transmits
  109004. + 0 // want crc check on bulk transfers
  109005. + };
  109006. +
  109007. + LOG_DBG("%s: about to open %i\n", __func__, i);
  109008. + status = vchi_service_open(vchi_instance, &params,
  109009. + &instance->vchi_handle[i]);
  109010. + LOG_DBG("%s: opened %i: %p=%d\n", __func__, i, instance->vchi_handle[i], status);
  109011. + if (status) {
  109012. + LOG_ERR
  109013. + ("%s: failed to open VCHI service connection (status=%d)\n",
  109014. + __func__, status);
  109015. +
  109016. + goto err_close_services;
  109017. + }
  109018. + /* Finished with the service for now */
  109019. + vchi_service_release(instance->vchi_handle[i]);
  109020. + }
  109021. +
  109022. + LOG_DBG("%s: okay\n", __func__);
  109023. + return instance;
  109024. +
  109025. +err_close_services:
  109026. + for (i = 0; i < instance->num_connections; i++) {
  109027. + LOG_ERR("%s: closing %i: %p\n", __func__, i, instance->vchi_handle[i]);
  109028. + if (instance->vchi_handle[i])
  109029. + vchi_service_close(instance->vchi_handle[i]);
  109030. + }
  109031. +
  109032. + kfree(instance);
  109033. + LOG_ERR("%s: error\n", __func__);
  109034. +
  109035. + return NULL;
  109036. +}
  109037. +
  109038. +static int32_t vc_vchi_audio_deinit(AUDIO_INSTANCE_T * instance)
  109039. +{
  109040. + uint32_t i;
  109041. +
  109042. + LOG_DBG(" .. IN\n");
  109043. +
  109044. + if (instance == NULL) {
  109045. + LOG_ERR("%s: invalid handle %p\n", __func__, instance);
  109046. +
  109047. + return -1;
  109048. + }
  109049. +
  109050. + LOG_DBG(" .. about to lock (%d)\n", instance->num_connections);
  109051. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  109052. + {
  109053. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  109054. + return -EINTR;
  109055. + }
  109056. +
  109057. + /* Close all VCHI service connections */
  109058. + for (i = 0; i < instance->num_connections; i++) {
  109059. + int32_t success;
  109060. + LOG_DBG(" .. %i:closing %p\n", i, instance->vchi_handle[i]);
  109061. + vchi_service_use(instance->vchi_handle[i]);
  109062. +
  109063. + success = vchi_service_close(instance->vchi_handle[i]);
  109064. + if (success != 0) {
  109065. + LOG_ERR
  109066. + ("%s: failed to close VCHI service connection (status=%d)\n",
  109067. + __func__, success);
  109068. + }
  109069. + }
  109070. +
  109071. + mutex_unlock(&instance->vchi_mutex);
  109072. +
  109073. + kfree(instance);
  109074. +
  109075. + LOG_DBG(" .. OUT\n");
  109076. +
  109077. + return 0;
  109078. +}
  109079. +
  109080. +static int bcm2835_audio_open_connection(bcm2835_alsa_stream_t * alsa_stream)
  109081. +{
  109082. + static VCHI_INSTANCE_T vchi_instance;
  109083. + static VCHI_CONNECTION_T *vchi_connection;
  109084. + static int initted;
  109085. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  109086. + int ret;
  109087. + LOG_DBG(" .. IN\n");
  109088. +
  109089. + LOG_INFO("%s: start\n", __func__);
  109090. + BUG_ON(instance);
  109091. + if (instance) {
  109092. + LOG_ERR("%s: VCHI instance already open (%p)\n",
  109093. + __func__, instance);
  109094. + instance->alsa_stream = alsa_stream;
  109095. + alsa_stream->instance = instance;
  109096. + ret = 0; // xxx todo -1;
  109097. + goto err_free_mem;
  109098. + }
  109099. +
  109100. + /* Initialize and create a VCHI connection */
  109101. + if (!initted) {
  109102. + ret = vchi_initialise(&vchi_instance);
  109103. + if (ret != 0) {
  109104. + LOG_ERR("%s: failed to initialise VCHI instance (ret=%d)\n",
  109105. + __func__, ret);
  109106. +
  109107. + ret = -EIO;
  109108. + goto err_free_mem;
  109109. + }
  109110. + ret = vchi_connect(NULL, 0, vchi_instance);
  109111. + if (ret != 0) {
  109112. + LOG_ERR("%s: failed to connect VCHI instance (ret=%d)\n",
  109113. + __func__, ret);
  109114. +
  109115. + ret = -EIO;
  109116. + goto err_free_mem;
  109117. + }
  109118. + initted = 1;
  109119. + }
  109120. +
  109121. + /* Initialize an instance of the audio service */
  109122. + instance = vc_vchi_audio_init(vchi_instance, &vchi_connection, 1);
  109123. +
  109124. + if (instance == NULL) {
  109125. + LOG_ERR("%s: failed to initialize audio service\n", __func__);
  109126. +
  109127. + ret = -EPERM;
  109128. + goto err_free_mem;
  109129. + }
  109130. +
  109131. + instance->alsa_stream = alsa_stream;
  109132. + alsa_stream->instance = instance;
  109133. +
  109134. + LOG_DBG(" success !\n");
  109135. +err_free_mem:
  109136. + LOG_DBG(" .. OUT\n");
  109137. +
  109138. + return ret;
  109139. +}
  109140. +
  109141. +int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream)
  109142. +{
  109143. + AUDIO_INSTANCE_T *instance;
  109144. + VC_AUDIO_MSG_T m;
  109145. + int32_t success;
  109146. + int ret;
  109147. + LOG_DBG(" .. IN\n");
  109148. +
  109149. + my_workqueue_init(alsa_stream);
  109150. +
  109151. + ret = bcm2835_audio_open_connection(alsa_stream);
  109152. + if (ret != 0) {
  109153. + ret = -1;
  109154. + goto exit;
  109155. + }
  109156. + instance = alsa_stream->instance;
  109157. + LOG_DBG(" instance (%p)\n", instance);
  109158. +
  109159. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  109160. + {
  109161. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  109162. + return -EINTR;
  109163. + }
  109164. + vchi_service_use(instance->vchi_handle[0]);
  109165. +
  109166. + m.type = VC_AUDIO_MSG_TYPE_OPEN;
  109167. +
  109168. + /* Send the message to the videocore */
  109169. + success = vchi_msg_queue(instance->vchi_handle[0],
  109170. + &m, sizeof m,
  109171. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  109172. +
  109173. + if (success != 0) {
  109174. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  109175. + __func__, success);
  109176. +
  109177. + ret = -1;
  109178. + goto unlock;
  109179. + }
  109180. +
  109181. + ret = 0;
  109182. +
  109183. +unlock:
  109184. + vchi_service_release(instance->vchi_handle[0]);
  109185. + mutex_unlock(&instance->vchi_mutex);
  109186. +exit:
  109187. + LOG_DBG(" .. OUT\n");
  109188. + return ret;
  109189. +}
  109190. +
  109191. +static int bcm2835_audio_set_ctls_chan(bcm2835_alsa_stream_t * alsa_stream,
  109192. + bcm2835_chip_t * chip)
  109193. +{
  109194. + VC_AUDIO_MSG_T m;
  109195. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  109196. + int32_t success;
  109197. + int ret;
  109198. + LOG_DBG(" .. IN\n");
  109199. +
  109200. + LOG_INFO
  109201. + (" Setting ALSA dest(%d), volume(%d)\n", chip->dest, chip->volume);
  109202. +
  109203. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  109204. + {
  109205. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  109206. + return -EINTR;
  109207. + }
  109208. + vchi_service_use(instance->vchi_handle[0]);
  109209. +
  109210. + instance->result = -1;
  109211. +
  109212. + m.type = VC_AUDIO_MSG_TYPE_CONTROL;
  109213. + m.u.control.dest = chip->dest;
  109214. + m.u.control.volume = chip->volume;
  109215. +
  109216. + /* Create the message available completion */
  109217. + init_completion(&instance->msg_avail_comp);
  109218. +
  109219. + /* Send the message to the videocore */
  109220. + success = vchi_msg_queue(instance->vchi_handle[0],
  109221. + &m, sizeof m,
  109222. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  109223. +
  109224. + if (success != 0) {
  109225. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  109226. + __func__, success);
  109227. +
  109228. + ret = -1;
  109229. + goto unlock;
  109230. + }
  109231. +
  109232. + /* We are expecting a reply from the videocore */
  109233. + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
  109234. + if (ret) {
  109235. + LOG_ERR("%s: failed on waiting for event (status=%d)\n",
  109236. + __func__, success);
  109237. + goto unlock;
  109238. + }
  109239. +
  109240. + if (instance->result != 0) {
  109241. + LOG_ERR("%s: result=%d\n", __func__, instance->result);
  109242. +
  109243. + ret = -1;
  109244. + goto unlock;
  109245. + }
  109246. +
  109247. + ret = 0;
  109248. +
  109249. +unlock:
  109250. + vchi_service_release(instance->vchi_handle[0]);
  109251. + mutex_unlock(&instance->vchi_mutex);
  109252. +
  109253. + LOG_DBG(" .. OUT\n");
  109254. + return ret;
  109255. +}
  109256. +
  109257. +int bcm2835_audio_set_ctls(bcm2835_chip_t * chip)
  109258. +{
  109259. + int i;
  109260. + int ret = 0;
  109261. + LOG_DBG(" .. IN\n");
  109262. + LOG_DBG(" Setting ALSA dest(%d), volume(%d)\n", chip->dest, chip->volume);
  109263. +
  109264. + /* change ctls for all substreams */
  109265. + for (i = 0; i < MAX_SUBSTREAMS; i++) {
  109266. + if (chip->avail_substreams & (1 << i)) {
  109267. + if (!chip->alsa_stream[i])
  109268. + {
  109269. + LOG_DBG(" No ALSA stream available?! %i:%p (%x)\n", i, chip->alsa_stream[i], chip->avail_substreams);
  109270. + ret = 0;
  109271. + }
  109272. + else if (bcm2835_audio_set_ctls_chan /* returns 0 on success */
  109273. + (chip->alsa_stream[i], chip) != 0)
  109274. + {
  109275. + LOG_ERR("Couldn't set the controls for stream %d\n", i);
  109276. + ret = -1;
  109277. + }
  109278. + else LOG_DBG(" Controls set for stream %d\n", i);
  109279. + }
  109280. + }
  109281. + LOG_DBG(" .. OUT ret=%d\n", ret);
  109282. + return ret;
  109283. +}
  109284. +
  109285. +int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,
  109286. + uint32_t channels, uint32_t samplerate,
  109287. + uint32_t bps)
  109288. +{
  109289. + VC_AUDIO_MSG_T m;
  109290. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  109291. + int32_t success;
  109292. + int ret;
  109293. + LOG_DBG(" .. IN\n");
  109294. +
  109295. + LOG_INFO
  109296. + (" Setting ALSA channels(%d), samplerate(%d), bits-per-sample(%d)\n",
  109297. + channels, samplerate, bps);
  109298. +
  109299. + /* resend ctls - alsa_stream may not have been open when first send */
  109300. + ret = bcm2835_audio_set_ctls_chan(alsa_stream, alsa_stream->chip);
  109301. + if (ret != 0) {
  109302. + LOG_ERR(" Alsa controls not supported\n");
  109303. + return -EINVAL;
  109304. + }
  109305. +
  109306. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  109307. + {
  109308. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  109309. + return -EINTR;
  109310. + }
  109311. + vchi_service_use(instance->vchi_handle[0]);
  109312. +
  109313. + instance->result = -1;
  109314. +
  109315. + m.type = VC_AUDIO_MSG_TYPE_CONFIG;
  109316. + m.u.config.channels = channels;
  109317. + m.u.config.samplerate = samplerate;
  109318. + m.u.config.bps = bps;
  109319. +
  109320. + /* Create the message available completion */
  109321. + init_completion(&instance->msg_avail_comp);
  109322. +
  109323. + /* Send the message to the videocore */
  109324. + success = vchi_msg_queue(instance->vchi_handle[0],
  109325. + &m, sizeof m,
  109326. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  109327. +
  109328. + if (success != 0) {
  109329. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  109330. + __func__, success);
  109331. +
  109332. + ret = -1;
  109333. + goto unlock;
  109334. + }
  109335. +
  109336. + /* We are expecting a reply from the videocore */
  109337. + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
  109338. + if (ret) {
  109339. + LOG_ERR("%s: failed on waiting for event (status=%d)\n",
  109340. + __func__, success);
  109341. + goto unlock;
  109342. + }
  109343. +
  109344. + if (instance->result != 0) {
  109345. + LOG_ERR("%s: result=%d", __func__, instance->result);
  109346. +
  109347. + ret = -1;
  109348. + goto unlock;
  109349. + }
  109350. +
  109351. + ret = 0;
  109352. +
  109353. +unlock:
  109354. + vchi_service_release(instance->vchi_handle[0]);
  109355. + mutex_unlock(&instance->vchi_mutex);
  109356. +
  109357. + LOG_DBG(" .. OUT\n");
  109358. + return ret;
  109359. +}
  109360. +
  109361. +int bcm2835_audio_setup(bcm2835_alsa_stream_t * alsa_stream)
  109362. +{
  109363. + LOG_DBG(" .. IN\n");
  109364. +
  109365. + LOG_DBG(" .. OUT\n");
  109366. +
  109367. + return 0;
  109368. +}
  109369. +
  109370. +static int bcm2835_audio_start_worker(bcm2835_alsa_stream_t * alsa_stream)
  109371. +{
  109372. + VC_AUDIO_MSG_T m;
  109373. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  109374. + int32_t success;
  109375. + int ret;
  109376. + LOG_DBG(" .. IN\n");
  109377. +
  109378. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  109379. + {
  109380. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  109381. + return -EINTR;
  109382. + }
  109383. + vchi_service_use(instance->vchi_handle[0]);
  109384. +
  109385. + m.type = VC_AUDIO_MSG_TYPE_START;
  109386. +
  109387. + /* Send the message to the videocore */
  109388. + success = vchi_msg_queue(instance->vchi_handle[0],
  109389. + &m, sizeof m,
  109390. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  109391. +
  109392. + if (success != 0) {
  109393. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  109394. + __func__, success);
  109395. +
  109396. + ret = -1;
  109397. + goto unlock;
  109398. + }
  109399. +
  109400. + ret = 0;
  109401. +
  109402. +unlock:
  109403. + vchi_service_release(instance->vchi_handle[0]);
  109404. + mutex_unlock(&instance->vchi_mutex);
  109405. + LOG_DBG(" .. OUT\n");
  109406. + return ret;
  109407. +}
  109408. +
  109409. +static int bcm2835_audio_stop_worker(bcm2835_alsa_stream_t * alsa_stream)
  109410. +{
  109411. + VC_AUDIO_MSG_T m;
  109412. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  109413. + int32_t success;
  109414. + int ret;
  109415. + LOG_DBG(" .. IN\n");
  109416. +
  109417. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  109418. + {
  109419. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  109420. + return -EINTR;
  109421. + }
  109422. + vchi_service_use(instance->vchi_handle[0]);
  109423. +
  109424. + m.type = VC_AUDIO_MSG_TYPE_STOP;
  109425. + m.u.stop.draining = alsa_stream->draining;
  109426. +
  109427. + /* Send the message to the videocore */
  109428. + success = vchi_msg_queue(instance->vchi_handle[0],
  109429. + &m, sizeof m,
  109430. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  109431. +
  109432. + if (success != 0) {
  109433. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  109434. + __func__, success);
  109435. +
  109436. + ret = -1;
  109437. + goto unlock;
  109438. + }
  109439. +
  109440. + ret = 0;
  109441. +
  109442. +unlock:
  109443. + vchi_service_release(instance->vchi_handle[0]);
  109444. + mutex_unlock(&instance->vchi_mutex);
  109445. + LOG_DBG(" .. OUT\n");
  109446. + return ret;
  109447. +}
  109448. +
  109449. +int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream)
  109450. +{
  109451. + VC_AUDIO_MSG_T m;
  109452. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  109453. + int32_t success;
  109454. + int ret;
  109455. + LOG_DBG(" .. IN\n");
  109456. +
  109457. + my_workqueue_quit(alsa_stream);
  109458. +
  109459. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  109460. + {
  109461. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  109462. + return -EINTR;
  109463. + }
  109464. + vchi_service_use(instance->vchi_handle[0]);
  109465. +
  109466. + m.type = VC_AUDIO_MSG_TYPE_CLOSE;
  109467. +
  109468. + /* Create the message available completion */
  109469. + init_completion(&instance->msg_avail_comp);
  109470. +
  109471. + /* Send the message to the videocore */
  109472. + success = vchi_msg_queue(instance->vchi_handle[0],
  109473. + &m, sizeof m,
  109474. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  109475. +
  109476. + if (success != 0) {
  109477. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  109478. + __func__, success);
  109479. + ret = -1;
  109480. + goto unlock;
  109481. + }
  109482. +
  109483. + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
  109484. + if (ret) {
  109485. + LOG_ERR("%s: failed on waiting for event (status=%d)\n",
  109486. + __func__, success);
  109487. + goto unlock;
  109488. + }
  109489. + if (instance->result != 0) {
  109490. + LOG_ERR("%s: failed result (status=%d)\n",
  109491. + __func__, instance->result);
  109492. +
  109493. + ret = -1;
  109494. + goto unlock;
  109495. + }
  109496. +
  109497. + ret = 0;
  109498. +
  109499. +unlock:
  109500. + vchi_service_release(instance->vchi_handle[0]);
  109501. + mutex_unlock(&instance->vchi_mutex);
  109502. +
  109503. + /* Stop the audio service */
  109504. + if (instance) {
  109505. + vc_vchi_audio_deinit(instance);
  109506. + alsa_stream->instance = NULL;
  109507. + }
  109508. + LOG_DBG(" .. OUT\n");
  109509. + return ret;
  109510. +}
  109511. +
  109512. +int bcm2835_audio_write_worker(bcm2835_alsa_stream_t *alsa_stream,
  109513. + uint32_t count, void *src)
  109514. +{
  109515. + VC_AUDIO_MSG_T m;
  109516. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  109517. + int32_t success;
  109518. + int ret;
  109519. +
  109520. + LOG_DBG(" .. IN\n");
  109521. +
  109522. + LOG_INFO(" Writing %d bytes from %p\n", count, src);
  109523. +
  109524. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  109525. + {
  109526. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  109527. + return -EINTR;
  109528. + }
  109529. + vchi_service_use(instance->vchi_handle[0]);
  109530. +
  109531. + if ( instance->peer_version==0 && vchi_get_peer_version(instance->vchi_handle[0], &instance->peer_version) == 0 ) {
  109532. + LOG_DBG("%s: client version %d connected\n", __func__, instance->peer_version);
  109533. + }
  109534. + m.type = VC_AUDIO_MSG_TYPE_WRITE;
  109535. + m.u.write.count = count;
  109536. + // old version uses bulk, new version uses control
  109537. + m.u.write.max_packet = instance->peer_version < 2 || force_bulk ? 0:4000;
  109538. + m.u.write.callback = alsa_stream->fifo_irq_handler;
  109539. + m.u.write.cookie = alsa_stream;
  109540. + m.u.write.silence = src == NULL;
  109541. +
  109542. + /* Send the message to the videocore */
  109543. + success = vchi_msg_queue(instance->vchi_handle[0],
  109544. + &m, sizeof m,
  109545. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  109546. +
  109547. + if (success != 0) {
  109548. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  109549. + __func__, success);
  109550. +
  109551. + ret = -1;
  109552. + goto unlock;
  109553. + }
  109554. + if (!m.u.write.silence) {
  109555. + if (m.u.write.max_packet == 0) {
  109556. + /* Send the message to the videocore */
  109557. + success = vchi_bulk_queue_transmit(instance->vchi_handle[0],
  109558. + src, count,
  109559. + 0 *
  109560. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED
  109561. + +
  109562. + 1 *
  109563. + VCHI_FLAGS_BLOCK_UNTIL_DATA_READ,
  109564. + NULL);
  109565. + } else {
  109566. + while (count > 0) {
  109567. + int bytes = min((int)m.u.write.max_packet, (int)count);
  109568. + success = vchi_msg_queue(instance->vchi_handle[0],
  109569. + src, bytes,
  109570. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  109571. + src = (char *)src + bytes;
  109572. + count -= bytes;
  109573. + }
  109574. + }
  109575. + if (success != 0) {
  109576. + LOG_ERR
  109577. + ("%s: failed on vchi_bulk_queue_transmit (status=%d)\n",
  109578. + __func__, success);
  109579. +
  109580. + ret = -1;
  109581. + goto unlock;
  109582. + }
  109583. + }
  109584. + ret = 0;
  109585. +
  109586. +unlock:
  109587. + vchi_service_release(instance->vchi_handle[0]);
  109588. + mutex_unlock(&instance->vchi_mutex);
  109589. + LOG_DBG(" .. OUT\n");
  109590. + return ret;
  109591. +}
  109592. +
  109593. +/**
  109594. + * Returns all buffers from arm->vc
  109595. + */
  109596. +void bcm2835_audio_flush_buffers(bcm2835_alsa_stream_t * alsa_stream)
  109597. +{
  109598. + LOG_DBG(" .. IN\n");
  109599. + LOG_DBG(" .. OUT\n");
  109600. + return;
  109601. +}
  109602. +
  109603. +/**
  109604. + * Forces VC to flush(drop) its filled playback buffers and
  109605. + * return them the us. (VC->ARM)
  109606. + */
  109607. +void bcm2835_audio_flush_playback_buffers(bcm2835_alsa_stream_t * alsa_stream)
  109608. +{
  109609. + LOG_DBG(" .. IN\n");
  109610. + LOG_DBG(" .. OUT\n");
  109611. +}
  109612. +
  109613. +uint32_t bcm2835_audio_retrieve_buffers(bcm2835_alsa_stream_t * alsa_stream)
  109614. +{
  109615. + uint32_t count = atomic_read(&alsa_stream->retrieved);
  109616. + atomic_sub(count, &alsa_stream->retrieved);
  109617. + return count;
  109618. +}
  109619. +
  109620. +module_param(force_bulk, bool, 0444);
  109621. +MODULE_PARM_DESC(force_bulk, "Force use of vchiq bulk for audio");
  109622. diff -Nur linux-3.12.33/sound/arm/Kconfig linux-3.12.33-rpi/sound/arm/Kconfig
  109623. --- linux-3.12.33/sound/arm/Kconfig 2014-11-15 06:28:07.000000000 -0600
  109624. +++ linux-3.12.33-rpi/sound/arm/Kconfig 2014-12-03 19:13:44.288418001 -0600
  109625. @@ -39,5 +39,12 @@
  109626. Say Y or M if you want to support any AC97 codec attached to
  109627. the PXA2xx AC97 interface.
  109628. +config SND_BCM2835
  109629. + tristate "BCM2835 ALSA driver"
  109630. + depends on ARCH_BCM2708 && BCM2708_VCHIQ && SND
  109631. + select SND_PCM
  109632. + help
  109633. + Say Y or M if you want to support BCM2835 Alsa pcm card driver
  109634. +
  109635. endif # SND_ARM
  109636. diff -Nur linux-3.12.33/sound/arm/Makefile linux-3.12.33-rpi/sound/arm/Makefile
  109637. --- linux-3.12.33/sound/arm/Makefile 2014-11-15 06:28:07.000000000 -0600
  109638. +++ linux-3.12.33-rpi/sound/arm/Makefile 2014-12-03 19:13:44.288418001 -0600
  109639. @@ -14,3 +14,8 @@
  109640. obj-$(CONFIG_SND_PXA2XX_AC97) += snd-pxa2xx-ac97.o
  109641. snd-pxa2xx-ac97-objs := pxa2xx-ac97.o
  109642. +
  109643. +obj-$(CONFIG_SND_BCM2835) += snd-bcm2835.o
  109644. +snd-bcm2835-objs := bcm2835.o bcm2835-ctl.o bcm2835-pcm.o bcm2835-vchiq.o
  109645. +
  109646. +ccflags-y += -Idrivers/misc/vc04_services -Idrivers/misc/vc04_services/interface/vcos/linuxkernel -D__VCCOREVER__=0x04000000
  109647. diff -Nur linux-3.12.33/sound/arm/vc_vchi_audioserv_defs.h linux-3.12.33-rpi/sound/arm/vc_vchi_audioserv_defs.h
  109648. --- linux-3.12.33/sound/arm/vc_vchi_audioserv_defs.h 1969-12-31 18:00:00.000000000 -0600
  109649. +++ linux-3.12.33-rpi/sound/arm/vc_vchi_audioserv_defs.h 2014-12-03 19:13:44.312418001 -0600
  109650. @@ -0,0 +1,116 @@
  109651. +/*****************************************************************************
  109652. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  109653. +*
  109654. +* Unless you and Broadcom execute a separate written software license
  109655. +* agreement governing use of this software, this software is licensed to you
  109656. +* under the terms of the GNU General Public License version 2, available at
  109657. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  109658. +*
  109659. +* Notwithstanding the above, under no circumstances may you combine this
  109660. +* software in any way with any other Broadcom software provided under a
  109661. +* license other than the GPL, without Broadcom's express prior written
  109662. +* consent.
  109663. +*****************************************************************************/
  109664. +
  109665. +#ifndef _VC_AUDIO_DEFS_H_
  109666. +#define _VC_AUDIO_DEFS_H_
  109667. +
  109668. +#define VC_AUDIOSERV_MIN_VER 1
  109669. +#define VC_AUDIOSERV_VER 2
  109670. +
  109671. +// FourCC code used for VCHI connection
  109672. +#define VC_AUDIO_SERVER_NAME MAKE_FOURCC("AUDS")
  109673. +
  109674. +// Maximum message length
  109675. +#define VC_AUDIO_MAX_MSG_LEN (sizeof( VC_AUDIO_MSG_T ))
  109676. +
  109677. +// List of screens that are currently supported
  109678. +// All message types supported for HOST->VC direction
  109679. +typedef enum {
  109680. + VC_AUDIO_MSG_TYPE_RESULT, // Generic result
  109681. + VC_AUDIO_MSG_TYPE_COMPLETE, // Generic result
  109682. + VC_AUDIO_MSG_TYPE_CONFIG, // Configure audio
  109683. + VC_AUDIO_MSG_TYPE_CONTROL, // Configure audio
  109684. + VC_AUDIO_MSG_TYPE_OPEN, // Configure audio
  109685. + VC_AUDIO_MSG_TYPE_CLOSE, // Configure audio
  109686. + VC_AUDIO_MSG_TYPE_START, // Configure audio
  109687. + VC_AUDIO_MSG_TYPE_STOP, // Configure audio
  109688. + VC_AUDIO_MSG_TYPE_WRITE, // Configure audio
  109689. + VC_AUDIO_MSG_TYPE_MAX
  109690. +} VC_AUDIO_MSG_TYPE;
  109691. +
  109692. +// configure the audio
  109693. +typedef struct {
  109694. + uint32_t channels;
  109695. + uint32_t samplerate;
  109696. + uint32_t bps;
  109697. +
  109698. +} VC_AUDIO_CONFIG_T;
  109699. +
  109700. +typedef struct {
  109701. + uint32_t volume;
  109702. + uint32_t dest;
  109703. +
  109704. +} VC_AUDIO_CONTROL_T;
  109705. +
  109706. +// audio
  109707. +typedef struct {
  109708. + uint32_t dummy;
  109709. +
  109710. +} VC_AUDIO_OPEN_T;
  109711. +
  109712. +// audio
  109713. +typedef struct {
  109714. + uint32_t dummy;
  109715. +
  109716. +} VC_AUDIO_CLOSE_T;
  109717. +// audio
  109718. +typedef struct {
  109719. + uint32_t dummy;
  109720. +
  109721. +} VC_AUDIO_START_T;
  109722. +// audio
  109723. +typedef struct {
  109724. + uint32_t draining;
  109725. +
  109726. +} VC_AUDIO_STOP_T;
  109727. +
  109728. +// configure the write audio samples
  109729. +typedef struct {
  109730. + uint32_t count; // in bytes
  109731. + void *callback;
  109732. + void *cookie;
  109733. + uint16_t silence;
  109734. + uint16_t max_packet;
  109735. +} VC_AUDIO_WRITE_T;
  109736. +
  109737. +// Generic result for a request (VC->HOST)
  109738. +typedef struct {
  109739. + int32_t success; // Success value
  109740. +
  109741. +} VC_AUDIO_RESULT_T;
  109742. +
  109743. +// Generic result for a request (VC->HOST)
  109744. +typedef struct {
  109745. + int32_t count; // Success value
  109746. + void *callback;
  109747. + void *cookie;
  109748. +} VC_AUDIO_COMPLETE_T;
  109749. +
  109750. +// Message header for all messages in HOST->VC direction
  109751. +typedef struct {
  109752. + int32_t type; // Message type (VC_AUDIO_MSG_TYPE)
  109753. + union {
  109754. + VC_AUDIO_CONFIG_T config;
  109755. + VC_AUDIO_CONTROL_T control;
  109756. + VC_AUDIO_OPEN_T open;
  109757. + VC_AUDIO_CLOSE_T close;
  109758. + VC_AUDIO_START_T start;
  109759. + VC_AUDIO_STOP_T stop;
  109760. + VC_AUDIO_WRITE_T write;
  109761. + VC_AUDIO_RESULT_T result;
  109762. + VC_AUDIO_COMPLETE_T complete;
  109763. + } u;
  109764. +} VC_AUDIO_MSG_T;
  109765. +
  109766. +#endif // _VC_AUDIO_DEFS_H_
  109767. diff -Nur linux-3.12.33/sound/soc/bcm/bcm2708-i2s.c linux-3.12.33-rpi/sound/soc/bcm/bcm2708-i2s.c
  109768. --- linux-3.12.33/sound/soc/bcm/bcm2708-i2s.c 1969-12-31 18:00:00.000000000 -0600
  109769. +++ linux-3.12.33-rpi/sound/soc/bcm/bcm2708-i2s.c 2014-12-03 19:13:44.596418001 -0600
  109770. @@ -0,0 +1,998 @@
  109771. +/*
  109772. + * ALSA SoC I2S Audio Layer for Broadcom BCM2708 SoC
  109773. + *
  109774. + * Author: Florian Meier <florian.meier@koalo.de>
  109775. + * Copyright 2013
  109776. + *
  109777. + * Based on
  109778. + * Raspberry Pi PCM I2S ALSA Driver
  109779. + * Copyright (c) by Phil Poole 2013
  109780. + *
  109781. + * ALSA SoC I2S (McBSP) Audio Layer for TI DAVINCI processor
  109782. + * Vladimir Barinov, <vbarinov@embeddedalley.com>
  109783. + * Copyright (C) 2007 MontaVista Software, Inc., <source@mvista.com>
  109784. + *
  109785. + * OMAP ALSA SoC DAI driver using McBSP port
  109786. + * Copyright (C) 2008 Nokia Corporation
  109787. + * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
  109788. + * Peter Ujfalusi <peter.ujfalusi@ti.com>
  109789. + *
  109790. + * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
  109791. + * Author: Timur Tabi <timur@freescale.com>
  109792. + * Copyright 2007-2010 Freescale Semiconductor, Inc.
  109793. + *
  109794. + * This program is free software; you can redistribute it and/or
  109795. + * modify it under the terms of the GNU General Public License
  109796. + * version 2 as published by the Free Software Foundation.
  109797. + *
  109798. + * This program is distributed in the hope that it will be useful, but
  109799. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  109800. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  109801. + * General Public License for more details.
  109802. + */
  109803. +
  109804. +#include "bcm2708-i2s.h"
  109805. +
  109806. +#include <linux/init.h>
  109807. +#include <linux/module.h>
  109808. +#include <linux/device.h>
  109809. +#include <linux/slab.h>
  109810. +#include <linux/delay.h>
  109811. +#include <linux/io.h>
  109812. +#include <linux/clk.h>
  109813. +#include <mach/gpio.h>
  109814. +
  109815. +#include <sound/core.h>
  109816. +#include <sound/pcm.h>
  109817. +#include <sound/pcm_params.h>
  109818. +#include <sound/initval.h>
  109819. +#include <sound/soc.h>
  109820. +#include <sound/dmaengine_pcm.h>
  109821. +
  109822. +#include <asm/system_info.h>
  109823. +
  109824. +/* Clock registers */
  109825. +#define BCM2708_CLK_PCMCTL_REG 0x00
  109826. +#define BCM2708_CLK_PCMDIV_REG 0x04
  109827. +
  109828. +/* Clock register settings */
  109829. +#define BCM2708_CLK_PASSWD (0x5a000000)
  109830. +#define BCM2708_CLK_PASSWD_MASK (0xff000000)
  109831. +#define BCM2708_CLK_MASH(v) ((v) << 9)
  109832. +#define BCM2708_CLK_FLIP BIT(8)
  109833. +#define BCM2708_CLK_BUSY BIT(7)
  109834. +#define BCM2708_CLK_KILL BIT(5)
  109835. +#define BCM2708_CLK_ENAB BIT(4)
  109836. +#define BCM2708_CLK_SRC(v) (v)
  109837. +
  109838. +#define BCM2708_CLK_SHIFT (12)
  109839. +#define BCM2708_CLK_DIVI(v) ((v) << BCM2708_CLK_SHIFT)
  109840. +#define BCM2708_CLK_DIVF(v) (v)
  109841. +#define BCM2708_CLK_DIVF_MASK (0xFFF)
  109842. +
  109843. +enum {
  109844. + BCM2708_CLK_MASH_0 = 0,
  109845. + BCM2708_CLK_MASH_1,
  109846. + BCM2708_CLK_MASH_2,
  109847. + BCM2708_CLK_MASH_3,
  109848. +};
  109849. +
  109850. +enum {
  109851. + BCM2708_CLK_SRC_GND = 0,
  109852. + BCM2708_CLK_SRC_OSC,
  109853. + BCM2708_CLK_SRC_DBG0,
  109854. + BCM2708_CLK_SRC_DBG1,
  109855. + BCM2708_CLK_SRC_PLLA,
  109856. + BCM2708_CLK_SRC_PLLC,
  109857. + BCM2708_CLK_SRC_PLLD,
  109858. + BCM2708_CLK_SRC_HDMI,
  109859. +};
  109860. +
  109861. +/* Most clocks are not useable (freq = 0) */
  109862. +static const unsigned int bcm2708_clk_freq[BCM2708_CLK_SRC_HDMI+1] = {
  109863. + [BCM2708_CLK_SRC_GND] = 0,
  109864. + [BCM2708_CLK_SRC_OSC] = 19200000,
  109865. + [BCM2708_CLK_SRC_DBG0] = 0,
  109866. + [BCM2708_CLK_SRC_DBG1] = 0,
  109867. + [BCM2708_CLK_SRC_PLLA] = 0,
  109868. + [BCM2708_CLK_SRC_PLLC] = 0,
  109869. + [BCM2708_CLK_SRC_PLLD] = 500000000,
  109870. + [BCM2708_CLK_SRC_HDMI] = 0,
  109871. +};
  109872. +
  109873. +/* I2S registers */
  109874. +#define BCM2708_I2S_CS_A_REG 0x00
  109875. +#define BCM2708_I2S_FIFO_A_REG 0x04
  109876. +#define BCM2708_I2S_MODE_A_REG 0x08
  109877. +#define BCM2708_I2S_RXC_A_REG 0x0c
  109878. +#define BCM2708_I2S_TXC_A_REG 0x10
  109879. +#define BCM2708_I2S_DREQ_A_REG 0x14
  109880. +#define BCM2708_I2S_INTEN_A_REG 0x18
  109881. +#define BCM2708_I2S_INTSTC_A_REG 0x1c
  109882. +#define BCM2708_I2S_GRAY_REG 0x20
  109883. +
  109884. +/* I2S register settings */
  109885. +#define BCM2708_I2S_STBY BIT(25)
  109886. +#define BCM2708_I2S_SYNC BIT(24)
  109887. +#define BCM2708_I2S_RXSEX BIT(23)
  109888. +#define BCM2708_I2S_RXF BIT(22)
  109889. +#define BCM2708_I2S_TXE BIT(21)
  109890. +#define BCM2708_I2S_RXD BIT(20)
  109891. +#define BCM2708_I2S_TXD BIT(19)
  109892. +#define BCM2708_I2S_RXR BIT(18)
  109893. +#define BCM2708_I2S_TXW BIT(17)
  109894. +#define BCM2708_I2S_CS_RXERR BIT(16)
  109895. +#define BCM2708_I2S_CS_TXERR BIT(15)
  109896. +#define BCM2708_I2S_RXSYNC BIT(14)
  109897. +#define BCM2708_I2S_TXSYNC BIT(13)
  109898. +#define BCM2708_I2S_DMAEN BIT(9)
  109899. +#define BCM2708_I2S_RXTHR(v) ((v) << 7)
  109900. +#define BCM2708_I2S_TXTHR(v) ((v) << 5)
  109901. +#define BCM2708_I2S_RXCLR BIT(4)
  109902. +#define BCM2708_I2S_TXCLR BIT(3)
  109903. +#define BCM2708_I2S_TXON BIT(2)
  109904. +#define BCM2708_I2S_RXON BIT(1)
  109905. +#define BCM2708_I2S_EN (1)
  109906. +
  109907. +#define BCM2708_I2S_CLKDIS BIT(28)
  109908. +#define BCM2708_I2S_PDMN BIT(27)
  109909. +#define BCM2708_I2S_PDME BIT(26)
  109910. +#define BCM2708_I2S_FRXP BIT(25)
  109911. +#define BCM2708_I2S_FTXP BIT(24)
  109912. +#define BCM2708_I2S_CLKM BIT(23)
  109913. +#define BCM2708_I2S_CLKI BIT(22)
  109914. +#define BCM2708_I2S_FSM BIT(21)
  109915. +#define BCM2708_I2S_FSI BIT(20)
  109916. +#define BCM2708_I2S_FLEN(v) ((v) << 10)
  109917. +#define BCM2708_I2S_FSLEN(v) (v)
  109918. +
  109919. +#define BCM2708_I2S_CHWEX BIT(15)
  109920. +#define BCM2708_I2S_CHEN BIT(14)
  109921. +#define BCM2708_I2S_CHPOS(v) ((v) << 4)
  109922. +#define BCM2708_I2S_CHWID(v) (v)
  109923. +#define BCM2708_I2S_CH1(v) ((v) << 16)
  109924. +#define BCM2708_I2S_CH2(v) (v)
  109925. +
  109926. +#define BCM2708_I2S_TX_PANIC(v) ((v) << 24)
  109927. +#define BCM2708_I2S_RX_PANIC(v) ((v) << 16)
  109928. +#define BCM2708_I2S_TX(v) ((v) << 8)
  109929. +#define BCM2708_I2S_RX(v) (v)
  109930. +
  109931. +#define BCM2708_I2S_INT_RXERR BIT(3)
  109932. +#define BCM2708_I2S_INT_TXERR BIT(2)
  109933. +#define BCM2708_I2S_INT_RXR BIT(1)
  109934. +#define BCM2708_I2S_INT_TXW BIT(0)
  109935. +
  109936. +/* I2S DMA interface */
  109937. +#define BCM2708_I2S_FIFO_PHYSICAL_ADDR 0x7E203004
  109938. +#define BCM2708_DMA_DREQ_PCM_TX 2
  109939. +#define BCM2708_DMA_DREQ_PCM_RX 3
  109940. +
  109941. +/* I2S pin configuration */
  109942. +static int bcm2708_i2s_gpio=BCM2708_I2S_GPIO_AUTO;
  109943. +
  109944. +/* General device struct */
  109945. +struct bcm2708_i2s_dev {
  109946. + struct device *dev;
  109947. + struct snd_dmaengine_dai_dma_data dma_data[2];
  109948. + unsigned int fmt;
  109949. + unsigned int bclk_ratio;
  109950. +
  109951. + struct regmap *i2s_regmap;
  109952. + struct regmap *clk_regmap;
  109953. +};
  109954. +
  109955. +void bcm2708_i2s_set_gpio(int gpio) {
  109956. + bcm2708_i2s_gpio=gpio;
  109957. +}
  109958. +EXPORT_SYMBOL(bcm2708_i2s_set_gpio);
  109959. +
  109960. +
  109961. +static void bcm2708_i2s_start_clock(struct bcm2708_i2s_dev *dev)
  109962. +{
  109963. + /* Start the clock if in master mode */
  109964. + unsigned int master = dev->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  109965. +
  109966. + switch (master) {
  109967. + case SND_SOC_DAIFMT_CBS_CFS:
  109968. + case SND_SOC_DAIFMT_CBS_CFM:
  109969. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  109970. + BCM2708_CLK_PASSWD_MASK | BCM2708_CLK_ENAB,
  109971. + BCM2708_CLK_PASSWD | BCM2708_CLK_ENAB);
  109972. + break;
  109973. + default:
  109974. + break;
  109975. + }
  109976. +}
  109977. +
  109978. +static void bcm2708_i2s_stop_clock(struct bcm2708_i2s_dev *dev)
  109979. +{
  109980. + uint32_t clkreg;
  109981. + int timeout = 1000;
  109982. +
  109983. + /* Stop clock */
  109984. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  109985. + BCM2708_CLK_PASSWD_MASK | BCM2708_CLK_ENAB,
  109986. + BCM2708_CLK_PASSWD);
  109987. +
  109988. + /* Wait for the BUSY flag going down */
  109989. + while (--timeout) {
  109990. + regmap_read(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG, &clkreg);
  109991. + if (!(clkreg & BCM2708_CLK_BUSY))
  109992. + break;
  109993. + }
  109994. +
  109995. + if (!timeout) {
  109996. + /* KILL the clock */
  109997. + dev_err(dev->dev, "I2S clock didn't stop. Kill the clock!\n");
  109998. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  109999. + BCM2708_CLK_KILL | BCM2708_CLK_PASSWD_MASK,
  110000. + BCM2708_CLK_KILL | BCM2708_CLK_PASSWD);
  110001. + }
  110002. +}
  110003. +
  110004. +static void bcm2708_i2s_clear_fifos(struct bcm2708_i2s_dev *dev,
  110005. + bool tx, bool rx)
  110006. +{
  110007. + int timeout = 1000;
  110008. + uint32_t syncval;
  110009. + uint32_t csreg;
  110010. + uint32_t i2s_active_state;
  110011. + uint32_t clkreg;
  110012. + uint32_t clk_active_state;
  110013. + uint32_t off;
  110014. + uint32_t clr;
  110015. +
  110016. + off = tx ? BCM2708_I2S_TXON : 0;
  110017. + off |= rx ? BCM2708_I2S_RXON : 0;
  110018. +
  110019. + clr = tx ? BCM2708_I2S_TXCLR : 0;
  110020. + clr |= rx ? BCM2708_I2S_RXCLR : 0;
  110021. +
  110022. + /* Backup the current state */
  110023. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &csreg);
  110024. + i2s_active_state = csreg & (BCM2708_I2S_RXON | BCM2708_I2S_TXON);
  110025. +
  110026. + regmap_read(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG, &clkreg);
  110027. + clk_active_state = clkreg & BCM2708_CLK_ENAB;
  110028. +
  110029. + /* Start clock if not running */
  110030. + if (!clk_active_state) {
  110031. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  110032. + BCM2708_CLK_PASSWD_MASK | BCM2708_CLK_ENAB,
  110033. + BCM2708_CLK_PASSWD | BCM2708_CLK_ENAB);
  110034. + }
  110035. +
  110036. + /* Stop I2S module */
  110037. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, off, 0);
  110038. +
  110039. + /*
  110040. + * Clear the FIFOs
  110041. + * Requires at least 2 PCM clock cycles to take effect
  110042. + */
  110043. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, clr, clr);
  110044. +
  110045. + /* Wait for 2 PCM clock cycles */
  110046. +
  110047. + /*
  110048. + * Toggle the SYNC flag. After 2 PCM clock cycles it can be read back
  110049. + * FIXME: This does not seem to work for slave mode!
  110050. + */
  110051. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &syncval);
  110052. + syncval &= BCM2708_I2S_SYNC;
  110053. +
  110054. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  110055. + BCM2708_I2S_SYNC, ~syncval);
  110056. +
  110057. + /* Wait for the SYNC flag changing it's state */
  110058. + while (--timeout) {
  110059. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &csreg);
  110060. + if ((csreg & BCM2708_I2S_SYNC) != syncval)
  110061. + break;
  110062. + }
  110063. +
  110064. + if (!timeout)
  110065. + dev_err(dev->dev, "I2S SYNC error!\n");
  110066. +
  110067. + /* Stop clock if it was not running before */
  110068. + if (!clk_active_state)
  110069. + bcm2708_i2s_stop_clock(dev);
  110070. +
  110071. + /* Restore I2S state */
  110072. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  110073. + BCM2708_I2S_RXON | BCM2708_I2S_TXON, i2s_active_state);
  110074. +}
  110075. +
  110076. +static int bcm2708_i2s_set_dai_fmt(struct snd_soc_dai *dai,
  110077. + unsigned int fmt)
  110078. +{
  110079. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  110080. + dev->fmt = fmt;
  110081. + return 0;
  110082. +}
  110083. +
  110084. +static int bcm2708_i2s_set_dai_bclk_ratio(struct snd_soc_dai *dai,
  110085. + unsigned int ratio)
  110086. +{
  110087. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  110088. + dev->bclk_ratio = ratio;
  110089. + return 0;
  110090. +}
  110091. +
  110092. +
  110093. +static int bcm2708_i2s_set_function(unsigned offset, int function)
  110094. +{
  110095. + #define GPIOFSEL(x) (0x00+(x)*4)
  110096. + void __iomem *gpio = __io_address(GPIO_BASE);
  110097. + unsigned alt = function <= 3 ? function + 4: function == 4 ? 3 : 2;
  110098. + unsigned gpiodir;
  110099. + unsigned gpio_bank = offset / 10;
  110100. + unsigned gpio_field_offset = (offset - 10 * gpio_bank) * 3;
  110101. +
  110102. + if (offset >= BCM2708_NR_GPIOS)
  110103. + return -EINVAL;
  110104. +
  110105. + gpiodir = readl(gpio + GPIOFSEL(gpio_bank));
  110106. + gpiodir &= ~(7 << gpio_field_offset);
  110107. + gpiodir |= alt << gpio_field_offset;
  110108. + writel(gpiodir, gpio + GPIOFSEL(gpio_bank));
  110109. + return 0;
  110110. +}
  110111. +
  110112. +static void bcm2708_i2s_setup_gpio(void)
  110113. +{
  110114. + /*
  110115. + * This is the common way to handle the GPIO pins for
  110116. + * the Raspberry Pi.
  110117. + * TODO Better way would be to handle
  110118. + * this in the device tree!
  110119. + */
  110120. + int pin,pinconfig,startpin,alt;
  110121. +
  110122. + /* SPI is on different GPIOs on different boards */
  110123. + /* for Raspberry Pi B+, this is pin GPIO18-21, for original on 28-31 */
  110124. + if (bcm2708_i2s_gpio==BCM2708_I2S_GPIO_AUTO) {
  110125. + if ((system_rev & 0xffffff) >= 0x10) {
  110126. + /* Model B+ */
  110127. + pinconfig=BCM2708_I2S_GPIO_PIN18;
  110128. + } else {
  110129. + /* original */
  110130. + pinconfig=BCM2708_I2S_GPIO_PIN28;
  110131. + }
  110132. + } else {
  110133. + pinconfig=bcm2708_i2s_gpio;
  110134. + }
  110135. +
  110136. + if (pinconfig==BCM2708_I2S_GPIO_PIN18) {
  110137. + startpin=18;
  110138. + alt=BCM2708_I2S_GPIO_PIN18_ALT;
  110139. + } else if (pinconfig==BCM2708_I2S_GPIO_PIN28) {
  110140. + startpin=28;
  110141. + alt=BCM2708_I2S_GPIO_PIN28_ALT;
  110142. + } else {
  110143. + printk(KERN_INFO "Can't configure I2S GPIOs, unknown pin mode for I2S: %i\n",pinconfig);
  110144. + return;
  110145. + }
  110146. +
  110147. + /* configure I2S pins to correct ALT mode */
  110148. + for (pin = startpin; pin <= startpin+3; pin++) {
  110149. + bcm2708_i2s_set_function(pin, alt);
  110150. + }
  110151. +}
  110152. +
  110153. +static int bcm2708_i2s_hw_params(struct snd_pcm_substream *substream,
  110154. + struct snd_pcm_hw_params *params,
  110155. + struct snd_soc_dai *dai)
  110156. +{
  110157. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  110158. +
  110159. + unsigned int sampling_rate = params_rate(params);
  110160. + unsigned int data_length, data_delay, bclk_ratio;
  110161. + unsigned int ch1pos, ch2pos, mode, format;
  110162. + unsigned int mash = BCM2708_CLK_MASH_1;
  110163. + unsigned int divi, divf, target_frequency;
  110164. + int clk_src = -1;
  110165. + unsigned int master = dev->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  110166. + bool bit_master = (master == SND_SOC_DAIFMT_CBS_CFS
  110167. + || master == SND_SOC_DAIFMT_CBS_CFM);
  110168. +
  110169. + bool frame_master = (master == SND_SOC_DAIFMT_CBS_CFS
  110170. + || master == SND_SOC_DAIFMT_CBM_CFS);
  110171. + uint32_t csreg;
  110172. +
  110173. + /*
  110174. + * If a stream is already enabled,
  110175. + * the registers are already set properly.
  110176. + */
  110177. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &csreg);
  110178. +
  110179. + if (csreg & (BCM2708_I2S_TXON | BCM2708_I2S_RXON))
  110180. + return 0;
  110181. +
  110182. +
  110183. + bcm2708_i2s_setup_gpio();
  110184. +
  110185. + /*
  110186. + * Adjust the data length according to the format.
  110187. + * We prefill the half frame length with an integer
  110188. + * divider of 2400 as explained at the clock settings.
  110189. + * Maybe it is overwritten there, if the Integer mode
  110190. + * does not apply.
  110191. + */
  110192. + switch (params_format(params)) {
  110193. + case SNDRV_PCM_FORMAT_S16_LE:
  110194. + data_length = 16;
  110195. + bclk_ratio = 50;
  110196. + break;
  110197. + case SNDRV_PCM_FORMAT_S24_LE:
  110198. + data_length = 24;
  110199. + bclk_ratio = 50;
  110200. + break;
  110201. + case SNDRV_PCM_FORMAT_S32_LE:
  110202. + data_length = 32;
  110203. + bclk_ratio = 100;
  110204. + break;
  110205. + default:
  110206. + return -EINVAL;
  110207. + }
  110208. +
  110209. + /* If bclk_ratio already set, use that one. */
  110210. + if (dev->bclk_ratio)
  110211. + bclk_ratio = dev->bclk_ratio;
  110212. +
  110213. + /*
  110214. + * Clock Settings
  110215. + *
  110216. + * The target frequency of the bit clock is
  110217. + * sampling rate * frame length
  110218. + *
  110219. + * Integer mode:
  110220. + * Sampling rates that are multiples of 8000 kHz
  110221. + * can be driven by the oscillator of 19.2 MHz
  110222. + * with an integer divider as long as the frame length
  110223. + * is an integer divider of 19200000/8000=2400 as set up above.
  110224. + * This is no longer possible if the sampling rate
  110225. + * is too high (e.g. 192 kHz), because the oscillator is too slow.
  110226. + *
  110227. + * MASH mode:
  110228. + * For all other sampling rates, it is not possible to
  110229. + * have an integer divider. Approximate the clock
  110230. + * with the MASH module that induces a slight frequency
  110231. + * variance. To minimize that it is best to have the fastest
  110232. + * clock here. That is PLLD with 500 MHz.
  110233. + */
  110234. + target_frequency = sampling_rate * bclk_ratio;
  110235. + clk_src = BCM2708_CLK_SRC_OSC;
  110236. + mash = BCM2708_CLK_MASH_0;
  110237. +
  110238. + if (bcm2708_clk_freq[clk_src] % target_frequency == 0
  110239. + && bit_master && frame_master) {
  110240. + divi = bcm2708_clk_freq[clk_src] / target_frequency;
  110241. + divf = 0;
  110242. + } else {
  110243. + uint64_t dividend;
  110244. +
  110245. + if (!dev->bclk_ratio) {
  110246. + /*
  110247. + * Overwrite bclk_ratio, because the
  110248. + * above trick is not needed or can
  110249. + * not be used.
  110250. + */
  110251. + bclk_ratio = 2 * data_length;
  110252. + }
  110253. +
  110254. + target_frequency = sampling_rate * bclk_ratio;
  110255. +
  110256. + clk_src = BCM2708_CLK_SRC_PLLD;
  110257. + mash = BCM2708_CLK_MASH_1;
  110258. +
  110259. + dividend = bcm2708_clk_freq[clk_src];
  110260. + dividend <<= BCM2708_CLK_SHIFT;
  110261. + do_div(dividend, target_frequency);
  110262. + divi = dividend >> BCM2708_CLK_SHIFT;
  110263. + divf = dividend & BCM2708_CLK_DIVF_MASK;
  110264. + }
  110265. +
  110266. + /* Set clock divider */
  110267. + regmap_write(dev->clk_regmap, BCM2708_CLK_PCMDIV_REG, BCM2708_CLK_PASSWD
  110268. + | BCM2708_CLK_DIVI(divi)
  110269. + | BCM2708_CLK_DIVF(divf));
  110270. +
  110271. + /* Setup clock, but don't start it yet */
  110272. + regmap_write(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG, BCM2708_CLK_PASSWD
  110273. + | BCM2708_CLK_MASH(mash)
  110274. + | BCM2708_CLK_SRC(clk_src));
  110275. +
  110276. + /* Setup the frame format */
  110277. + format = BCM2708_I2S_CHEN;
  110278. +
  110279. + if (data_length >= 24)
  110280. + format |= BCM2708_I2S_CHWEX;
  110281. +
  110282. + format |= BCM2708_I2S_CHWID((data_length-8)&0xf);
  110283. +
  110284. + switch (dev->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  110285. + case SND_SOC_DAIFMT_I2S:
  110286. + data_delay = 1;
  110287. + break;
  110288. + default:
  110289. + /*
  110290. + * TODO
  110291. + * Others are possible but are not implemented at the moment.
  110292. + */
  110293. + dev_err(dev->dev, "%s:bad format\n", __func__);
  110294. + return -EINVAL;
  110295. + }
  110296. +
  110297. + ch1pos = data_delay;
  110298. + ch2pos = bclk_ratio / 2 + data_delay;
  110299. +
  110300. + switch (params_channels(params)) {
  110301. + case 2:
  110302. + format = BCM2708_I2S_CH1(format) | BCM2708_I2S_CH2(format);
  110303. + format |= BCM2708_I2S_CH1(BCM2708_I2S_CHPOS(ch1pos));
  110304. + format |= BCM2708_I2S_CH2(BCM2708_I2S_CHPOS(ch2pos));
  110305. + break;
  110306. + default:
  110307. + return -EINVAL;
  110308. + }
  110309. +
  110310. + /*
  110311. + * Set format for both streams.
  110312. + * We cannot set another frame length
  110313. + * (and therefore word length) anyway,
  110314. + * so the format will be the same.
  110315. + */
  110316. + regmap_write(dev->i2s_regmap, BCM2708_I2S_RXC_A_REG, format);
  110317. + regmap_write(dev->i2s_regmap, BCM2708_I2S_TXC_A_REG, format);
  110318. +
  110319. + /* Setup the I2S mode */
  110320. + mode = 0;
  110321. +
  110322. + if (data_length <= 16) {
  110323. + /*
  110324. + * Use frame packed mode (2 channels per 32 bit word)
  110325. + * We cannot set another frame length in the second stream
  110326. + * (and therefore word length) anyway,
  110327. + * so the format will be the same.
  110328. + */
  110329. + mode |= BCM2708_I2S_FTXP | BCM2708_I2S_FRXP;
  110330. + }
  110331. +
  110332. + mode |= BCM2708_I2S_FLEN(bclk_ratio - 1);
  110333. + mode |= BCM2708_I2S_FSLEN(bclk_ratio / 2);
  110334. +
  110335. + /* Master or slave? */
  110336. + switch (dev->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  110337. + case SND_SOC_DAIFMT_CBS_CFS:
  110338. + /* CPU is master */
  110339. + break;
  110340. + case SND_SOC_DAIFMT_CBM_CFS:
  110341. + /*
  110342. + * CODEC is bit clock master
  110343. + * CPU is frame master
  110344. + */
  110345. + mode |= BCM2708_I2S_CLKM;
  110346. + break;
  110347. + case SND_SOC_DAIFMT_CBS_CFM:
  110348. + /*
  110349. + * CODEC is frame master
  110350. + * CPU is bit clock master
  110351. + */
  110352. + mode |= BCM2708_I2S_FSM;
  110353. + break;
  110354. + case SND_SOC_DAIFMT_CBM_CFM:
  110355. + /* CODEC is master */
  110356. + mode |= BCM2708_I2S_CLKM;
  110357. + mode |= BCM2708_I2S_FSM;
  110358. + break;
  110359. + default:
  110360. + dev_err(dev->dev, "%s:bad master\n", __func__);
  110361. + return -EINVAL;
  110362. + }
  110363. +
  110364. + /*
  110365. + * Invert clocks?
  110366. + *
  110367. + * The BCM approach seems to be inverted to the classical I2S approach.
  110368. + */
  110369. + switch (dev->fmt & SND_SOC_DAIFMT_INV_MASK) {
  110370. + case SND_SOC_DAIFMT_NB_NF:
  110371. + /* None. Therefore, both for BCM */
  110372. + mode |= BCM2708_I2S_CLKI;
  110373. + mode |= BCM2708_I2S_FSI;
  110374. + break;
  110375. + case SND_SOC_DAIFMT_IB_IF:
  110376. + /* Both. Therefore, none for BCM */
  110377. + break;
  110378. + case SND_SOC_DAIFMT_NB_IF:
  110379. + /*
  110380. + * Invert only frame sync. Therefore,
  110381. + * invert only bit clock for BCM
  110382. + */
  110383. + mode |= BCM2708_I2S_CLKI;
  110384. + break;
  110385. + case SND_SOC_DAIFMT_IB_NF:
  110386. + /*
  110387. + * Invert only bit clock. Therefore,
  110388. + * invert only frame sync for BCM
  110389. + */
  110390. + mode |= BCM2708_I2S_FSI;
  110391. + break;
  110392. + default:
  110393. + return -EINVAL;
  110394. + }
  110395. +
  110396. + regmap_write(dev->i2s_regmap, BCM2708_I2S_MODE_A_REG, mode);
  110397. +
  110398. + /* Setup the DMA parameters */
  110399. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  110400. + BCM2708_I2S_RXTHR(1)
  110401. + | BCM2708_I2S_TXTHR(1)
  110402. + | BCM2708_I2S_DMAEN, 0xffffffff);
  110403. +
  110404. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_DREQ_A_REG,
  110405. + BCM2708_I2S_TX_PANIC(0x10)
  110406. + | BCM2708_I2S_RX_PANIC(0x30)
  110407. + | BCM2708_I2S_TX(0x30)
  110408. + | BCM2708_I2S_RX(0x20), 0xffffffff);
  110409. +
  110410. + /* Clear FIFOs */
  110411. + bcm2708_i2s_clear_fifos(dev, true, true);
  110412. +
  110413. + return 0;
  110414. +}
  110415. +
  110416. +static int bcm2708_i2s_prepare(struct snd_pcm_substream *substream,
  110417. + struct snd_soc_dai *dai)
  110418. +{
  110419. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  110420. + uint32_t cs_reg;
  110421. +
  110422. + bcm2708_i2s_start_clock(dev);
  110423. +
  110424. + /*
  110425. + * Clear both FIFOs if the one that should be started
  110426. + * is not empty at the moment. This should only happen
  110427. + * after overrun. Otherwise, hw_params would have cleared
  110428. + * the FIFO.
  110429. + */
  110430. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &cs_reg);
  110431. +
  110432. + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  110433. + && !(cs_reg & BCM2708_I2S_TXE))
  110434. + bcm2708_i2s_clear_fifos(dev, true, false);
  110435. + else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE
  110436. + && (cs_reg & BCM2708_I2S_RXD))
  110437. + bcm2708_i2s_clear_fifos(dev, false, true);
  110438. +
  110439. + return 0;
  110440. +}
  110441. +
  110442. +static void bcm2708_i2s_stop(struct bcm2708_i2s_dev *dev,
  110443. + struct snd_pcm_substream *substream,
  110444. + struct snd_soc_dai *dai)
  110445. +{
  110446. + uint32_t mask;
  110447. +
  110448. + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  110449. + mask = BCM2708_I2S_RXON;
  110450. + else
  110451. + mask = BCM2708_I2S_TXON;
  110452. +
  110453. + regmap_update_bits(dev->i2s_regmap,
  110454. + BCM2708_I2S_CS_A_REG, mask, 0);
  110455. +
  110456. + /* Stop also the clock when not SND_SOC_DAIFMT_CONT */
  110457. + if (!dai->active && !(dev->fmt & SND_SOC_DAIFMT_CONT))
  110458. + bcm2708_i2s_stop_clock(dev);
  110459. +}
  110460. +
  110461. +static int bcm2708_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  110462. + struct snd_soc_dai *dai)
  110463. +{
  110464. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  110465. + uint32_t mask;
  110466. +
  110467. + switch (cmd) {
  110468. + case SNDRV_PCM_TRIGGER_START:
  110469. + case SNDRV_PCM_TRIGGER_RESUME:
  110470. + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  110471. + bcm2708_i2s_start_clock(dev);
  110472. +
  110473. + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  110474. + mask = BCM2708_I2S_RXON;
  110475. + else
  110476. + mask = BCM2708_I2S_TXON;
  110477. +
  110478. + regmap_update_bits(dev->i2s_regmap,
  110479. + BCM2708_I2S_CS_A_REG, mask, mask);
  110480. + break;
  110481. +
  110482. + case SNDRV_PCM_TRIGGER_STOP:
  110483. + case SNDRV_PCM_TRIGGER_SUSPEND:
  110484. + case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  110485. + bcm2708_i2s_stop(dev, substream, dai);
  110486. + break;
  110487. + default:
  110488. + return -EINVAL;
  110489. + }
  110490. +
  110491. + return 0;
  110492. +}
  110493. +
  110494. +static int bcm2708_i2s_startup(struct snd_pcm_substream *substream,
  110495. + struct snd_soc_dai *dai)
  110496. +{
  110497. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  110498. +
  110499. + if (dai->active)
  110500. + return 0;
  110501. +
  110502. + /* Should this still be running stop it */
  110503. + bcm2708_i2s_stop_clock(dev);
  110504. +
  110505. + /* Enable PCM block */
  110506. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  110507. + BCM2708_I2S_EN, BCM2708_I2S_EN);
  110508. +
  110509. + /*
  110510. + * Disable STBY.
  110511. + * Requires at least 4 PCM clock cycles to take effect.
  110512. + */
  110513. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  110514. + BCM2708_I2S_STBY, BCM2708_I2S_STBY);
  110515. +
  110516. + return 0;
  110517. +}
  110518. +
  110519. +static void bcm2708_i2s_shutdown(struct snd_pcm_substream *substream,
  110520. + struct snd_soc_dai *dai)
  110521. +{
  110522. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  110523. +
  110524. + bcm2708_i2s_stop(dev, substream, dai);
  110525. +
  110526. + /* If both streams are stopped, disable module and clock */
  110527. + if (dai->active)
  110528. + return;
  110529. +
  110530. + /* Disable the module */
  110531. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  110532. + BCM2708_I2S_EN, 0);
  110533. +
  110534. + /*
  110535. + * Stopping clock is necessary, because stop does
  110536. + * not stop the clock when SND_SOC_DAIFMT_CONT
  110537. + */
  110538. + bcm2708_i2s_stop_clock(dev);
  110539. +}
  110540. +
  110541. +static const struct snd_soc_dai_ops bcm2708_i2s_dai_ops = {
  110542. + .startup = bcm2708_i2s_startup,
  110543. + .shutdown = bcm2708_i2s_shutdown,
  110544. + .prepare = bcm2708_i2s_prepare,
  110545. + .trigger = bcm2708_i2s_trigger,
  110546. + .hw_params = bcm2708_i2s_hw_params,
  110547. + .set_fmt = bcm2708_i2s_set_dai_fmt,
  110548. + .set_bclk_ratio = bcm2708_i2s_set_dai_bclk_ratio
  110549. +};
  110550. +
  110551. +static int bcm2708_i2s_dai_probe(struct snd_soc_dai *dai)
  110552. +{
  110553. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  110554. +
  110555. + dai->playback_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
  110556. + dai->capture_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE];
  110557. +
  110558. + return 0;
  110559. +}
  110560. +
  110561. +static struct snd_soc_dai_driver bcm2708_i2s_dai = {
  110562. + .name = "bcm2708-i2s",
  110563. + .probe = bcm2708_i2s_dai_probe,
  110564. + .playback = {
  110565. + .channels_min = 2,
  110566. + .channels_max = 2,
  110567. + .rates = SNDRV_PCM_RATE_8000_192000,
  110568. + .formats = SNDRV_PCM_FMTBIT_S16_LE
  110569. + | SNDRV_PCM_FMTBIT_S24_LE
  110570. + | SNDRV_PCM_FMTBIT_S32_LE
  110571. + },
  110572. + .capture = {
  110573. + .channels_min = 2,
  110574. + .channels_max = 2,
  110575. + .rates = SNDRV_PCM_RATE_8000_192000,
  110576. + .formats = SNDRV_PCM_FMTBIT_S16_LE
  110577. + | SNDRV_PCM_FMTBIT_S24_LE
  110578. + | SNDRV_PCM_FMTBIT_S32_LE
  110579. + },
  110580. + .ops = &bcm2708_i2s_dai_ops,
  110581. + .symmetric_rates = 1
  110582. +};
  110583. +
  110584. +static bool bcm2708_i2s_volatile_reg(struct device *dev, unsigned int reg)
  110585. +{
  110586. + switch (reg) {
  110587. + case BCM2708_I2S_CS_A_REG:
  110588. + case BCM2708_I2S_FIFO_A_REG:
  110589. + case BCM2708_I2S_INTSTC_A_REG:
  110590. + case BCM2708_I2S_GRAY_REG:
  110591. + return true;
  110592. + default:
  110593. + return false;
  110594. + };
  110595. +}
  110596. +
  110597. +static bool bcm2708_i2s_precious_reg(struct device *dev, unsigned int reg)
  110598. +{
  110599. + switch (reg) {
  110600. + case BCM2708_I2S_FIFO_A_REG:
  110601. + return true;
  110602. + default:
  110603. + return false;
  110604. + };
  110605. +}
  110606. +
  110607. +static bool bcm2708_clk_volatile_reg(struct device *dev, unsigned int reg)
  110608. +{
  110609. + switch (reg) {
  110610. + case BCM2708_CLK_PCMCTL_REG:
  110611. + return true;
  110612. + default:
  110613. + return false;
  110614. + };
  110615. +}
  110616. +
  110617. +static const struct regmap_config bcm2708_regmap_config[] = {
  110618. + {
  110619. + .reg_bits = 32,
  110620. + .reg_stride = 4,
  110621. + .val_bits = 32,
  110622. + .max_register = BCM2708_I2S_GRAY_REG,
  110623. + .precious_reg = bcm2708_i2s_precious_reg,
  110624. + .volatile_reg = bcm2708_i2s_volatile_reg,
  110625. + .cache_type = REGCACHE_RBTREE,
  110626. + .name = "i2s",
  110627. + },
  110628. + {
  110629. + .reg_bits = 32,
  110630. + .reg_stride = 4,
  110631. + .val_bits = 32,
  110632. + .max_register = BCM2708_CLK_PCMDIV_REG,
  110633. + .volatile_reg = bcm2708_clk_volatile_reg,
  110634. + .cache_type = REGCACHE_RBTREE,
  110635. + .name = "clk",
  110636. + },
  110637. +};
  110638. +
  110639. +static const struct snd_soc_component_driver bcm2708_i2s_component = {
  110640. + .name = "bcm2708-i2s-comp",
  110641. +};
  110642. +
  110643. +static const struct snd_pcm_hardware bcm2708_pcm_hardware = {
  110644. + .info = SNDRV_PCM_INFO_INTERLEAVED |
  110645. + SNDRV_PCM_INFO_JOINT_DUPLEX,
  110646. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  110647. + SNDRV_PCM_FMTBIT_S24_LE |
  110648. + SNDRV_PCM_FMTBIT_S32_LE,
  110649. + .period_bytes_min = 32,
  110650. + .period_bytes_max = 64 * PAGE_SIZE,
  110651. + .periods_min = 2,
  110652. + .periods_max = 255,
  110653. + .buffer_bytes_max = 128 * PAGE_SIZE,
  110654. +};
  110655. +
  110656. +static const struct snd_dmaengine_pcm_config bcm2708_dmaengine_pcm_config = {
  110657. + .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  110658. + .pcm_hardware = &bcm2708_pcm_hardware,
  110659. + .prealloc_buffer_size = 256 * PAGE_SIZE,
  110660. +};
  110661. +
  110662. +
  110663. +static int bcm2708_i2s_probe(struct platform_device *pdev)
  110664. +{
  110665. + struct bcm2708_i2s_dev *dev;
  110666. + int i;
  110667. + int ret;
  110668. + struct regmap *regmap[2];
  110669. + struct resource *mem[2];
  110670. +
  110671. + /* Request both ioareas */
  110672. + for (i = 0; i <= 1; i++) {
  110673. + void __iomem *base;
  110674. +
  110675. + mem[i] = platform_get_resource(pdev, IORESOURCE_MEM, i);
  110676. + base = devm_ioremap_resource(&pdev->dev, mem[i]);
  110677. + if (IS_ERR(base))
  110678. + return PTR_ERR(base);
  110679. +
  110680. + regmap[i] = devm_regmap_init_mmio(&pdev->dev, base,
  110681. + &bcm2708_regmap_config[i]);
  110682. + if (IS_ERR(regmap[i])) {
  110683. + dev_err(&pdev->dev, "I2S probe: regmap init failed\n");
  110684. + return PTR_ERR(regmap[i]);
  110685. + }
  110686. + }
  110687. +
  110688. + dev = devm_kzalloc(&pdev->dev, sizeof(*dev),
  110689. + GFP_KERNEL);
  110690. + if (IS_ERR(dev))
  110691. + return PTR_ERR(dev);
  110692. +
  110693. + dev->i2s_regmap = regmap[0];
  110694. + dev->clk_regmap = regmap[1];
  110695. +
  110696. + /* Set the DMA address */
  110697. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr =
  110698. + (dma_addr_t)BCM2708_I2S_FIFO_PHYSICAL_ADDR;
  110699. +
  110700. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr =
  110701. + (dma_addr_t)BCM2708_I2S_FIFO_PHYSICAL_ADDR;
  110702. +
  110703. + /* Set the DREQ */
  110704. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].slave_id =
  110705. + BCM2708_DMA_DREQ_PCM_TX;
  110706. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].slave_id =
  110707. + BCM2708_DMA_DREQ_PCM_RX;
  110708. +
  110709. + /* Set the bus width */
  110710. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr_width =
  110711. + DMA_SLAVE_BUSWIDTH_4_BYTES;
  110712. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr_width =
  110713. + DMA_SLAVE_BUSWIDTH_4_BYTES;
  110714. +
  110715. + /* Set burst */
  110716. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].maxburst = 2;
  110717. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].maxburst = 2;
  110718. +
  110719. + /* BCLK ratio - use default */
  110720. + dev->bclk_ratio = 0;
  110721. +
  110722. + /* Store the pdev */
  110723. + dev->dev = &pdev->dev;
  110724. + dev_set_drvdata(&pdev->dev, dev);
  110725. +
  110726. + ret = snd_soc_register_component(&pdev->dev,
  110727. + &bcm2708_i2s_component, &bcm2708_i2s_dai, 1);
  110728. +
  110729. + if (ret) {
  110730. + dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
  110731. + ret = -ENOMEM;
  110732. + return ret;
  110733. + }
  110734. +
  110735. + ret = snd_dmaengine_pcm_register(&pdev->dev,
  110736. + &bcm2708_dmaengine_pcm_config,
  110737. + SND_DMAENGINE_PCM_FLAG_COMPAT);
  110738. + if (ret) {
  110739. + dev_err(&pdev->dev, "Could not register PCM: %d\n", ret);
  110740. + snd_soc_unregister_component(&pdev->dev);
  110741. + return ret;
  110742. + }
  110743. +
  110744. + return 0;
  110745. +}
  110746. +
  110747. +static int bcm2708_i2s_remove(struct platform_device *pdev)
  110748. +{
  110749. + snd_dmaengine_pcm_unregister(&pdev->dev);
  110750. + snd_soc_unregister_component(&pdev->dev);
  110751. + return 0;
  110752. +}
  110753. +
  110754. +static struct platform_driver bcm2708_i2s_driver = {
  110755. + .probe = bcm2708_i2s_probe,
  110756. + .remove = bcm2708_i2s_remove,
  110757. + .driver = {
  110758. + .name = "bcm2708-i2s",
  110759. + .owner = THIS_MODULE,
  110760. + },
  110761. +};
  110762. +
  110763. +module_platform_driver(bcm2708_i2s_driver);
  110764. +
  110765. +MODULE_ALIAS("platform:bcm2708-i2s");
  110766. +MODULE_DESCRIPTION("BCM2708 I2S interface");
  110767. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  110768. +MODULE_LICENSE("GPL v2");
  110769. diff -Nur linux-3.12.33/sound/soc/bcm/bcm2708-i2s.h linux-3.12.33-rpi/sound/soc/bcm/bcm2708-i2s.h
  110770. --- linux-3.12.33/sound/soc/bcm/bcm2708-i2s.h 1969-12-31 18:00:00.000000000 -0600
  110771. +++ linux-3.12.33-rpi/sound/soc/bcm/bcm2708-i2s.h 2014-12-03 19:13:44.596418001 -0600
  110772. @@ -0,0 +1,35 @@
  110773. +/*
  110774. + * I2S configuration for sound cards.
  110775. + *
  110776. + * Copyright (c) 2014 Daniel Matuschek <daniel@hifiberry.com>
  110777. + *
  110778. + * This program is free software; you can redistribute it and/or modify
  110779. + * it under the terms of the GNU General Public License as published by
  110780. + * the Free Software Foundation; either version 2 of the License, or
  110781. + * (at your option) any later version.
  110782. + *
  110783. + * This program is distributed in the hope that it will be useful,
  110784. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  110785. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  110786. + * GNU General Public License for more details.
  110787. + *
  110788. + * You should have received a copy of the GNU General Public License
  110789. + * along with this program; if not, write to the Free Software
  110790. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  110791. + */
  110792. +
  110793. +#ifndef BCM2708_I2S_H
  110794. +#define BCM2708_I2S_H
  110795. +
  110796. +/* I2S pin assignment */
  110797. +#define BCM2708_I2S_GPIO_AUTO 0
  110798. +#define BCM2708_I2S_GPIO_PIN18 1
  110799. +#define BCM2708_I2S_GPIO_PIN28 2
  110800. +
  110801. +/* Alt mode to enable I2S */
  110802. +#define BCM2708_I2S_GPIO_PIN18_ALT 0
  110803. +#define BCM2708_I2S_GPIO_PIN28_ALT 2
  110804. +
  110805. +extern void bcm2708_i2s_set_gpio(int gpio);
  110806. +
  110807. +#endif
  110808. diff -Nur linux-3.12.33/sound/soc/bcm/hifiberry_amp.c linux-3.12.33-rpi/sound/soc/bcm/hifiberry_amp.c
  110809. --- linux-3.12.33/sound/soc/bcm/hifiberry_amp.c 1969-12-31 18:00:00.000000000 -0600
  110810. +++ linux-3.12.33-rpi/sound/soc/bcm/hifiberry_amp.c 2014-12-03 19:13:44.596418001 -0600
  110811. @@ -0,0 +1,106 @@
  110812. +/*
  110813. + * ASoC Driver for HifiBerry AMP
  110814. + *
  110815. + * Author: Sebastian Eickhoff <basti.eickhoff@googlemail.com>
  110816. + * Copyright 2014
  110817. + *
  110818. + * This program is free software; you can redistribute it and/or
  110819. + * modify it under the terms of the GNU General Public License
  110820. + * version 2 as published by the Free Software Foundation.
  110821. + *
  110822. + * This program is distributed in the hope that it will be useful, but
  110823. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  110824. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  110825. + * General Public License for more details.
  110826. + */
  110827. +
  110828. +#include <linux/module.h>
  110829. +#include <linux/platform_device.h>
  110830. +
  110831. +#include <sound/core.h>
  110832. +#include <sound/pcm.h>
  110833. +#include <sound/pcm_params.h>
  110834. +#include <sound/soc.h>
  110835. +#include <sound/jack.h>
  110836. +
  110837. +static int snd_rpi_hifiberry_amp_init(struct snd_soc_pcm_runtime *rtd)
  110838. +{
  110839. + // ToDo: init of the dsp-registers.
  110840. + return 0;
  110841. +}
  110842. +
  110843. +static int snd_rpi_hifiberry_amp_hw_params( struct snd_pcm_substream *substream,
  110844. + struct snd_pcm_hw_params *params )
  110845. +{
  110846. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  110847. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  110848. +
  110849. + return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
  110850. +}
  110851. +
  110852. +static struct snd_soc_ops snd_rpi_hifiberry_amp_ops = {
  110853. + .hw_params = snd_rpi_hifiberry_amp_hw_params,
  110854. +};
  110855. +
  110856. +static struct snd_soc_dai_link snd_rpi_hifiberry_amp_dai[] = {
  110857. + {
  110858. + .name = "HifiBerry AMP",
  110859. + .stream_name = "HifiBerry AMP HiFi",
  110860. + .cpu_dai_name = "bcm2708-i2s.0",
  110861. + .codec_dai_name = "tas5713-hifi",
  110862. + .platform_name = "bcm2708-i2s.0",
  110863. + .codec_name = "tas5713.1-001b",
  110864. + .dai_fmt = SND_SOC_DAIFMT_I2S |
  110865. + SND_SOC_DAIFMT_NB_NF |
  110866. + SND_SOC_DAIFMT_CBS_CFS,
  110867. + .ops = &snd_rpi_hifiberry_amp_ops,
  110868. + .init = snd_rpi_hifiberry_amp_init,
  110869. + },
  110870. +};
  110871. +
  110872. +
  110873. +static struct snd_soc_card snd_rpi_hifiberry_amp = {
  110874. + .name = "snd_rpi_hifiberry_amp",
  110875. + .dai_link = snd_rpi_hifiberry_amp_dai,
  110876. + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_amp_dai),
  110877. +};
  110878. +
  110879. +
  110880. +static int snd_rpi_hifiberry_amp_probe(struct platform_device *pdev)
  110881. +{
  110882. + int ret = 0;
  110883. +
  110884. + snd_rpi_hifiberry_amp.dev = &pdev->dev;
  110885. +
  110886. + ret = snd_soc_register_card(&snd_rpi_hifiberry_amp);
  110887. +
  110888. + if (ret != 0) {
  110889. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  110890. + }
  110891. +
  110892. + return ret;
  110893. +}
  110894. +
  110895. +
  110896. +static int snd_rpi_hifiberry_amp_remove(struct platform_device *pdev)
  110897. +{
  110898. + return snd_soc_unregister_card(&snd_rpi_hifiberry_amp);
  110899. +}
  110900. +
  110901. +
  110902. +static struct platform_driver snd_rpi_hifiberry_amp_driver = {
  110903. + .driver = {
  110904. + .name = "snd-hifiberry-amp",
  110905. + .owner = THIS_MODULE,
  110906. + },
  110907. + .probe = snd_rpi_hifiberry_amp_probe,
  110908. + .remove = snd_rpi_hifiberry_amp_remove,
  110909. +};
  110910. +
  110911. +
  110912. +module_platform_driver(snd_rpi_hifiberry_amp_driver);
  110913. +
  110914. +
  110915. +MODULE_AUTHOR("Sebastian Eickhoff <basti.eickhoff@googlemail.com>");
  110916. +MODULE_DESCRIPTION("ASoC driver for HiFiBerry-AMP");
  110917. +MODULE_LICENSE("GPL v2");
  110918. diff -Nur linux-3.12.33/sound/soc/bcm/hifiberry_dac.c linux-3.12.33-rpi/sound/soc/bcm/hifiberry_dac.c
  110919. --- linux-3.12.33/sound/soc/bcm/hifiberry_dac.c 1969-12-31 18:00:00.000000000 -0600
  110920. +++ linux-3.12.33-rpi/sound/soc/bcm/hifiberry_dac.c 2014-12-03 19:13:44.596418001 -0600
  110921. @@ -0,0 +1,100 @@
  110922. +/*
  110923. + * ASoC Driver for HifiBerry DAC
  110924. + *
  110925. + * Author: Florian Meier <florian.meier@koalo.de>
  110926. + * Copyright 2013
  110927. + *
  110928. + * This program is free software; you can redistribute it and/or
  110929. + * modify it under the terms of the GNU General Public License
  110930. + * version 2 as published by the Free Software Foundation.
  110931. + *
  110932. + * This program is distributed in the hope that it will be useful, but
  110933. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  110934. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  110935. + * General Public License for more details.
  110936. + */
  110937. +
  110938. +#include <linux/module.h>
  110939. +#include <linux/platform_device.h>
  110940. +
  110941. +#include <sound/core.h>
  110942. +#include <sound/pcm.h>
  110943. +#include <sound/pcm_params.h>
  110944. +#include <sound/soc.h>
  110945. +#include <sound/jack.h>
  110946. +
  110947. +static int snd_rpi_hifiberry_dac_init(struct snd_soc_pcm_runtime *rtd)
  110948. +{
  110949. + return 0;
  110950. +}
  110951. +
  110952. +static int snd_rpi_hifiberry_dac_hw_params(struct snd_pcm_substream *substream,
  110953. + struct snd_pcm_hw_params *params)
  110954. +{
  110955. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  110956. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  110957. +
  110958. + unsigned int sample_bits =
  110959. + snd_pcm_format_physical_width(params_format(params));
  110960. +
  110961. + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
  110962. +}
  110963. +
  110964. +/* machine stream operations */
  110965. +static struct snd_soc_ops snd_rpi_hifiberry_dac_ops = {
  110966. + .hw_params = snd_rpi_hifiberry_dac_hw_params,
  110967. +};
  110968. +
  110969. +static struct snd_soc_dai_link snd_rpi_hifiberry_dac_dai[] = {
  110970. +{
  110971. + .name = "HifiBerry DAC",
  110972. + .stream_name = "HifiBerry DAC HiFi",
  110973. + .cpu_dai_name = "bcm2708-i2s.0",
  110974. + .codec_dai_name = "pcm5102a-hifi",
  110975. + .platform_name = "bcm2708-i2s.0",
  110976. + .codec_name = "pcm5102a-codec",
  110977. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  110978. + SND_SOC_DAIFMT_CBS_CFS,
  110979. + .ops = &snd_rpi_hifiberry_dac_ops,
  110980. + .init = snd_rpi_hifiberry_dac_init,
  110981. +},
  110982. +};
  110983. +
  110984. +/* audio machine driver */
  110985. +static struct snd_soc_card snd_rpi_hifiberry_dac = {
  110986. + .name = "snd_rpi_hifiberry_dac",
  110987. + .dai_link = snd_rpi_hifiberry_dac_dai,
  110988. + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_dac_dai),
  110989. +};
  110990. +
  110991. +static int snd_rpi_hifiberry_dac_probe(struct platform_device *pdev)
  110992. +{
  110993. + int ret = 0;
  110994. +
  110995. + snd_rpi_hifiberry_dac.dev = &pdev->dev;
  110996. + ret = snd_soc_register_card(&snd_rpi_hifiberry_dac);
  110997. + if (ret)
  110998. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  110999. +
  111000. + return ret;
  111001. +}
  111002. +
  111003. +static int snd_rpi_hifiberry_dac_remove(struct platform_device *pdev)
  111004. +{
  111005. + return snd_soc_unregister_card(&snd_rpi_hifiberry_dac);
  111006. +}
  111007. +
  111008. +static struct platform_driver snd_rpi_hifiberry_dac_driver = {
  111009. + .driver = {
  111010. + .name = "snd-hifiberry-dac",
  111011. + .owner = THIS_MODULE,
  111012. + },
  111013. + .probe = snd_rpi_hifiberry_dac_probe,
  111014. + .remove = snd_rpi_hifiberry_dac_remove,
  111015. +};
  111016. +
  111017. +module_platform_driver(snd_rpi_hifiberry_dac_driver);
  111018. +
  111019. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  111020. +MODULE_DESCRIPTION("ASoC Driver for HifiBerry DAC");
  111021. +MODULE_LICENSE("GPL v2");
  111022. diff -Nur linux-3.12.33/sound/soc/bcm/hifiberry_dacplus.c linux-3.12.33-rpi/sound/soc/bcm/hifiberry_dacplus.c
  111023. --- linux-3.12.33/sound/soc/bcm/hifiberry_dacplus.c 1969-12-31 18:00:00.000000000 -0600
  111024. +++ linux-3.12.33-rpi/sound/soc/bcm/hifiberry_dacplus.c 2014-12-03 19:13:44.596418001 -0600
  111025. @@ -0,0 +1,119 @@
  111026. +/*
  111027. + * ASoC Driver for HiFiBerry DAC+
  111028. + *
  111029. + * Author: Daniel Matuschek
  111030. + * Copyright 2014
  111031. + * based on code by Florian Meier <florian.meier@koalo.de>
  111032. + *
  111033. + * This program is free software; you can redistribute it and/or
  111034. + * modify it under the terms of the GNU General Public License
  111035. + * version 2 as published by the Free Software Foundation.
  111036. + *
  111037. + * This program is distributed in the hope that it will be useful, but
  111038. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  111039. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  111040. + * General Public License for more details.
  111041. + */
  111042. +
  111043. +#include <linux/module.h>
  111044. +#include <linux/platform_device.h>
  111045. +
  111046. +#include <sound/core.h>
  111047. +#include <sound/pcm.h>
  111048. +#include <sound/pcm_params.h>
  111049. +#include <sound/soc.h>
  111050. +#include <sound/jack.h>
  111051. +
  111052. +#include "../codecs/pcm512x.h"
  111053. +
  111054. +static int snd_rpi_hifiberry_dacplus_init(struct snd_soc_pcm_runtime *rtd)
  111055. +{
  111056. + struct snd_soc_codec *codec = rtd->codec;
  111057. + snd_soc_update_bits(codec, PCM512x_GPIO_EN, 0x08, 0x08);
  111058. + snd_soc_update_bits(codec, PCM512x_GPIO_OUTPUT_4, 0xf, 0x02);
  111059. + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08,0x08);
  111060. + return 0;
  111061. +}
  111062. +
  111063. +static int snd_rpi_hifiberry_dacplus_hw_params(struct snd_pcm_substream *substream,
  111064. + struct snd_pcm_hw_params *params)
  111065. +{
  111066. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  111067. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  111068. + return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
  111069. +}
  111070. +
  111071. +static int snd_rpi_hifiberry_dacplus_startup(struct snd_pcm_substream *substream) {
  111072. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  111073. + struct snd_soc_codec *codec = rtd->codec;
  111074. + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08,0x08);
  111075. + return 0;
  111076. +}
  111077. +
  111078. +static void snd_rpi_hifiberry_dacplus_shutdown(struct snd_pcm_substream *substream) {
  111079. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  111080. + struct snd_soc_codec *codec = rtd->codec;
  111081. + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08,0x00);
  111082. +}
  111083. +
  111084. +/* machine stream operations */
  111085. +static struct snd_soc_ops snd_rpi_hifiberry_dacplus_ops = {
  111086. + .hw_params = snd_rpi_hifiberry_dacplus_hw_params,
  111087. + .startup = snd_rpi_hifiberry_dacplus_startup,
  111088. + .shutdown = snd_rpi_hifiberry_dacplus_shutdown,
  111089. +};
  111090. +
  111091. +static struct snd_soc_dai_link snd_rpi_hifiberry_dacplus_dai[] = {
  111092. +{
  111093. + .name = "HiFiBerry DAC+",
  111094. + .stream_name = "HiFiBerry DAC+ HiFi",
  111095. + .cpu_dai_name = "bcm2708-i2s.0",
  111096. + .codec_dai_name = "pcm512x-hifi",
  111097. + .platform_name = "bcm2708-i2s.0",
  111098. + .codec_name = "pcm512x.1-004d",
  111099. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  111100. + SND_SOC_DAIFMT_CBS_CFS,
  111101. + .ops = &snd_rpi_hifiberry_dacplus_ops,
  111102. + .init = snd_rpi_hifiberry_dacplus_init,
  111103. +},
  111104. +};
  111105. +
  111106. +/* audio machine driver */
  111107. +static struct snd_soc_card snd_rpi_hifiberry_dacplus = {
  111108. + .name = "snd_rpi_hifiberry_dacplus",
  111109. + .dai_link = snd_rpi_hifiberry_dacplus_dai,
  111110. + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_dacplus_dai),
  111111. +};
  111112. +
  111113. +static int snd_rpi_hifiberry_dacplus_probe(struct platform_device *pdev)
  111114. +{
  111115. + int ret = 0;
  111116. +
  111117. + snd_rpi_hifiberry_dacplus.dev = &pdev->dev;
  111118. + ret = snd_soc_register_card(&snd_rpi_hifiberry_dacplus);
  111119. + if (ret)
  111120. + dev_err(&pdev->dev,
  111121. + "snd_soc_register_card() failed: %d\n", ret);
  111122. +
  111123. + return ret;
  111124. +}
  111125. +
  111126. +static int snd_rpi_hifiberry_dacplus_remove(struct platform_device *pdev)
  111127. +{
  111128. + return snd_soc_unregister_card(&snd_rpi_hifiberry_dacplus);
  111129. +}
  111130. +
  111131. +static struct platform_driver snd_rpi_hifiberry_dacplus_driver = {
  111132. + .driver = {
  111133. + .name = "snd-rpi-hifiberry-dacplus",
  111134. + .owner = THIS_MODULE,
  111135. + },
  111136. + .probe = snd_rpi_hifiberry_dacplus_probe,
  111137. + .remove = snd_rpi_hifiberry_dacplus_remove,
  111138. +};
  111139. +
  111140. +module_platform_driver(snd_rpi_hifiberry_dacplus_driver);
  111141. +
  111142. +MODULE_AUTHOR("Daniel Matuschek <daniel@hifiberry.com>");
  111143. +MODULE_DESCRIPTION("ASoC Driver for HiFiBerry DAC+");
  111144. +MODULE_LICENSE("GPL v2");
  111145. diff -Nur linux-3.12.33/sound/soc/bcm/hifiberry_digi.c linux-3.12.33-rpi/sound/soc/bcm/hifiberry_digi.c
  111146. --- linux-3.12.33/sound/soc/bcm/hifiberry_digi.c 1969-12-31 18:00:00.000000000 -0600
  111147. +++ linux-3.12.33-rpi/sound/soc/bcm/hifiberry_digi.c 2014-12-03 19:13:44.596418001 -0600
  111148. @@ -0,0 +1,153 @@
  111149. +/*
  111150. + * ASoC Driver for HifiBerry Digi
  111151. + *
  111152. + * Author: Daniel Matuschek <info@crazy-audio.com>
  111153. + * based on the HifiBerry DAC driver by Florian Meier <florian.meier@koalo.de>
  111154. + * Copyright 2013
  111155. + *
  111156. + * This program is free software; you can redistribute it and/or
  111157. + * modify it under the terms of the GNU General Public License
  111158. + * version 2 as published by the Free Software Foundation.
  111159. + *
  111160. + * This program is distributed in the hope that it will be useful, but
  111161. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  111162. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  111163. + * General Public License for more details.
  111164. + */
  111165. +
  111166. +#include <linux/module.h>
  111167. +#include <linux/platform_device.h>
  111168. +
  111169. +#include <sound/core.h>
  111170. +#include <sound/pcm.h>
  111171. +#include <sound/pcm_params.h>
  111172. +#include <sound/soc.h>
  111173. +#include <sound/jack.h>
  111174. +
  111175. +#include "../codecs/wm8804.h"
  111176. +
  111177. +static int samplerate=44100;
  111178. +
  111179. +static int snd_rpi_hifiberry_digi_init(struct snd_soc_pcm_runtime *rtd)
  111180. +{
  111181. + struct snd_soc_codec *codec = rtd->codec;
  111182. +
  111183. + /* enable TX output */
  111184. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
  111185. +
  111186. + return 0;
  111187. +}
  111188. +
  111189. +static int snd_rpi_hifiberry_digi_hw_params(struct snd_pcm_substream *substream,
  111190. + struct snd_pcm_hw_params *params)
  111191. +{
  111192. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  111193. + struct snd_soc_dai *codec_dai = rtd->codec_dai;
  111194. + struct snd_soc_codec *codec = rtd->codec;
  111195. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  111196. +
  111197. + int sysclk = 27000000; /* This is fixed on this board */
  111198. +
  111199. + long mclk_freq=0;
  111200. + int mclk_div=1;
  111201. +
  111202. + int ret;
  111203. +
  111204. + samplerate = params_rate(params);
  111205. +
  111206. + switch (samplerate) {
  111207. + case 44100:
  111208. + case 48000:
  111209. + case 88200:
  111210. + case 96000:
  111211. + mclk_freq=samplerate*256;
  111212. + mclk_div=WM8804_MCLKDIV_256FS;
  111213. + break;
  111214. + case 176400:
  111215. + case 192000:
  111216. + mclk_freq=samplerate*128;
  111217. + mclk_div=WM8804_MCLKDIV_128FS;
  111218. + break;
  111219. + default:
  111220. + dev_err(substream->pcm->dev,
  111221. + "Failed to set WM8804 SYSCLK, unsupported samplerate\n");
  111222. + }
  111223. +
  111224. + snd_soc_dai_set_clkdiv(codec_dai, WM8804_MCLK_DIV, mclk_div);
  111225. + snd_soc_dai_set_pll(codec_dai, 0, 0, sysclk, mclk_freq);
  111226. +
  111227. + ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
  111228. + sysclk, SND_SOC_CLOCK_OUT);
  111229. + if (ret < 0) {
  111230. + dev_err(substream->pcm->dev,
  111231. + "Failed to set WM8804 SYSCLK: %d\n", ret);
  111232. + return ret;
  111233. + }
  111234. +
  111235. + /* Enable TX output */
  111236. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
  111237. +
  111238. + /* Power on */
  111239. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x9, 0);
  111240. +
  111241. + return snd_soc_dai_set_bclk_ratio(cpu_dai,64);
  111242. +}
  111243. +
  111244. +/* machine stream operations */
  111245. +static struct snd_soc_ops snd_rpi_hifiberry_digi_ops = {
  111246. + .hw_params = snd_rpi_hifiberry_digi_hw_params,
  111247. +};
  111248. +
  111249. +static struct snd_soc_dai_link snd_rpi_hifiberry_digi_dai[] = {
  111250. +{
  111251. + .name = "HifiBerry Digi",
  111252. + .stream_name = "HifiBerry Digi HiFi",
  111253. + .cpu_dai_name = "bcm2708-i2s.0",
  111254. + .codec_dai_name = "wm8804-spdif",
  111255. + .platform_name = "bcm2708-i2s.0",
  111256. + .codec_name = "wm8804.1-003b",
  111257. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  111258. + SND_SOC_DAIFMT_CBM_CFM,
  111259. + .ops = &snd_rpi_hifiberry_digi_ops,
  111260. + .init = snd_rpi_hifiberry_digi_init,
  111261. +},
  111262. +};
  111263. +
  111264. +/* audio machine driver */
  111265. +static struct snd_soc_card snd_rpi_hifiberry_digi = {
  111266. + .name = "snd_rpi_hifiberry_digi",
  111267. + .dai_link = snd_rpi_hifiberry_digi_dai,
  111268. + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_digi_dai),
  111269. +};
  111270. +
  111271. +static int snd_rpi_hifiberry_digi_probe(struct platform_device *pdev)
  111272. +{
  111273. + int ret = 0;
  111274. +
  111275. + snd_rpi_hifiberry_digi.dev = &pdev->dev;
  111276. + ret = snd_soc_register_card(&snd_rpi_hifiberry_digi);
  111277. + if (ret)
  111278. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  111279. +
  111280. + return ret;
  111281. +}
  111282. +
  111283. +static int snd_rpi_hifiberry_digi_remove(struct platform_device *pdev)
  111284. +{
  111285. + return snd_soc_unregister_card(&snd_rpi_hifiberry_digi);
  111286. +}
  111287. +
  111288. +static struct platform_driver snd_rpi_hifiberry_digi_driver = {
  111289. + .driver = {
  111290. + .name = "snd-hifiberry-digi",
  111291. + .owner = THIS_MODULE,
  111292. + },
  111293. + .probe = snd_rpi_hifiberry_digi_probe,
  111294. + .remove = snd_rpi_hifiberry_digi_remove,
  111295. +};
  111296. +
  111297. +module_platform_driver(snd_rpi_hifiberry_digi_driver);
  111298. +
  111299. +MODULE_AUTHOR("Daniel Matuschek <info@crazy-audio.com>");
  111300. +MODULE_DESCRIPTION("ASoC Driver for HifiBerry Digi");
  111301. +MODULE_LICENSE("GPL v2");
  111302. diff -Nur linux-3.12.33/sound/soc/bcm/iqaudio-dac.c linux-3.12.33-rpi/sound/soc/bcm/iqaudio-dac.c
  111303. --- linux-3.12.33/sound/soc/bcm/iqaudio-dac.c 1969-12-31 18:00:00.000000000 -0600
  111304. +++ linux-3.12.33-rpi/sound/soc/bcm/iqaudio-dac.c 2014-12-03 19:13:44.596418001 -0600
  111305. @@ -0,0 +1,111 @@
  111306. +/*
  111307. + * ASoC Driver for IQaudIO DAC
  111308. + *
  111309. + * Author: Florian Meier <florian.meier@koalo.de>
  111310. + * Copyright 2013
  111311. + *
  111312. + * This program is free software; you can redistribute it and/or
  111313. + * modify it under the terms of the GNU General Public License
  111314. + * version 2 as published by the Free Software Foundation.
  111315. + *
  111316. + * This program is distributed in the hope that it will be useful, but
  111317. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  111318. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  111319. + * General Public License for more details.
  111320. + */
  111321. +
  111322. +#include <linux/module.h>
  111323. +#include <linux/platform_device.h>
  111324. +
  111325. +#include <sound/core.h>
  111326. +#include <sound/pcm.h>
  111327. +#include <sound/pcm_params.h>
  111328. +#include <sound/soc.h>
  111329. +#include <sound/jack.h>
  111330. +
  111331. +static int snd_rpi_iqaudio_dac_init(struct snd_soc_pcm_runtime *rtd)
  111332. +{
  111333. +// NOT USED struct snd_soc_codec *codec = rtd->codec;
  111334. +
  111335. + return 0;
  111336. +}
  111337. +
  111338. +static int snd_rpi_iqaudio_dac_hw_params(struct snd_pcm_substream *substream,
  111339. + struct snd_pcm_hw_params *params)
  111340. +{
  111341. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  111342. +// NOT USED struct snd_soc_dai *codec_dai = rtd->codec_dai;
  111343. +// NOT USED struct snd_soc_codec *codec = rtd->codec;
  111344. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  111345. +
  111346. + unsigned int sample_bits =
  111347. + snd_pcm_format_physical_width(params_format(params));
  111348. +
  111349. + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
  111350. +}
  111351. +
  111352. +/* machine stream operations */
  111353. +static struct snd_soc_ops snd_rpi_iqaudio_dac_ops = {
  111354. + .hw_params = snd_rpi_iqaudio_dac_hw_params,
  111355. +};
  111356. +
  111357. +static struct snd_soc_dai_link snd_rpi_iqaudio_dac_dai[] = {
  111358. +{
  111359. + .name = "IQaudIO DAC",
  111360. + .stream_name = "IQaudIO DAC HiFi",
  111361. + .cpu_dai_name = "bcm2708-i2s.0",
  111362. + .codec_dai_name = "pcm512x-hifi",
  111363. + .platform_name = "bcm2708-i2s.0",
  111364. + .codec_name = "pcm512x.1-004c",
  111365. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  111366. + SND_SOC_DAIFMT_CBS_CFS,
  111367. + .ops = &snd_rpi_iqaudio_dac_ops,
  111368. + .init = snd_rpi_iqaudio_dac_init,
  111369. +},
  111370. +};
  111371. +
  111372. +/* audio machine driver */
  111373. +static struct snd_soc_card snd_rpi_iqaudio_dac = {
  111374. + .name = "snd_rpi_iqaudio_dac",
  111375. + .dai_link = snd_rpi_iqaudio_dac_dai,
  111376. + .num_links = ARRAY_SIZE(snd_rpi_iqaudio_dac_dai),
  111377. +};
  111378. +
  111379. +static int snd_rpi_iqaudio_dac_probe(struct platform_device *pdev)
  111380. +{
  111381. + int ret = 0;
  111382. +
  111383. + snd_rpi_iqaudio_dac.dev = &pdev->dev;
  111384. + ret = snd_soc_register_card(&snd_rpi_iqaudio_dac);
  111385. + if (ret)
  111386. + dev_err(&pdev->dev,
  111387. + "snd_soc_register_card() failed: %d\n", ret);
  111388. +
  111389. + return ret;
  111390. +}
  111391. +
  111392. +static int snd_rpi_iqaudio_dac_remove(struct platform_device *pdev)
  111393. +{
  111394. + return snd_soc_unregister_card(&snd_rpi_iqaudio_dac);
  111395. +}
  111396. +
  111397. +static const struct of_device_id iqaudio_of_match[] = {
  111398. + { .compatible = "iqaudio,iqaudio-dac", },
  111399. + {},
  111400. +};
  111401. +
  111402. +static struct platform_driver snd_rpi_iqaudio_dac_driver = {
  111403. + .driver = {
  111404. + .name = "snd-rpi-iqaudio-dac",
  111405. + .owner = THIS_MODULE,
  111406. + .of_match_table = iqaudio_of_match,
  111407. + },
  111408. + .probe = snd_rpi_iqaudio_dac_probe,
  111409. + .remove = snd_rpi_iqaudio_dac_remove,
  111410. +};
  111411. +
  111412. +module_platform_driver(snd_rpi_iqaudio_dac_driver);
  111413. +
  111414. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  111415. +MODULE_DESCRIPTION("ASoC Driver for IQAudio DAC");
  111416. +MODULE_LICENSE("GPL v2");
  111417. diff -Nur linux-3.12.33/sound/soc/bcm/Kconfig linux-3.12.33-rpi/sound/soc/bcm/Kconfig
  111418. --- linux-3.12.33/sound/soc/bcm/Kconfig 1969-12-31 18:00:00.000000000 -0600
  111419. +++ linux-3.12.33-rpi/sound/soc/bcm/Kconfig 2014-12-03 19:13:44.596418001 -0600
  111420. @@ -0,0 +1,52 @@
  111421. +config SND_BCM2708_SOC_I2S
  111422. + tristate "SoC Audio support for the Broadcom BCM2708 I2S module"
  111423. + depends on MACH_BCM2708
  111424. + select REGMAP_MMIO
  111425. + select SND_SOC_DMAENGINE_PCM
  111426. + select SND_SOC_GENERIC_DMAENGINE_PCM
  111427. + help
  111428. + Say Y or M if you want to add support for codecs attached to
  111429. + the BCM2708 I2S interface. You will also need
  111430. + to select the audio interfaces to support below.
  111431. +
  111432. +config SND_BCM2708_SOC_HIFIBERRY_DAC
  111433. + tristate "Support for HifiBerry DAC"
  111434. + depends on SND_BCM2708_SOC_I2S
  111435. + select SND_SOC_PCM5102A
  111436. + help
  111437. + Say Y or M if you want to add support for HifiBerry DAC.
  111438. +
  111439. +config SND_BCM2708_SOC_HIFIBERRY_DACPLUS
  111440. + tristate "Support for HifiBerry DAC+"
  111441. + depends on SND_BCM2708_SOC_I2S
  111442. + select SND_SOC_PCM512x
  111443. + help
  111444. + Say Y or M if you want to add support for HifiBerry DAC+.
  111445. +
  111446. +config SND_BCM2708_SOC_HIFIBERRY_DIGI
  111447. + tristate "Support for HifiBerry Digi"
  111448. + depends on SND_BCM2708_SOC_I2S
  111449. + select SND_SOC_WM8804
  111450. + help
  111451. + Say Y or M if you want to add support for HifiBerry Digi S/PDIF output board.
  111452. +
  111453. +config SND_BCM2708_SOC_HIFIBERRY_AMP
  111454. + tristate "Support for the HifiBerry Amp"
  111455. + depends on SND_BCM2708_SOC_I2S
  111456. + select SND_SOC_TAS5713
  111457. + help
  111458. + Say Y or M if you want to add support for the HifiBerry Amp amplifier board.
  111459. +
  111460. +config SND_BCM2708_SOC_RPI_DAC
  111461. + tristate "Support for RPi-DAC"
  111462. + depends on SND_BCM2708_SOC_I2S
  111463. + select SND_SOC_PCM1794A
  111464. + help
  111465. + Say Y or M if you want to add support for RPi-DAC.
  111466. +
  111467. +config SND_BCM2708_SOC_IQAUDIO_DAC
  111468. + tristate "Support for IQaudIO-DAC"
  111469. + depends on SND_BCM2708_SOC_I2S
  111470. + select SND_SOC_PCM512x
  111471. + help
  111472. + Say Y or M if you want to add support for IQaudIO-DAC.
  111473. diff -Nur linux-3.12.33/sound/soc/bcm/Makefile linux-3.12.33-rpi/sound/soc/bcm/Makefile
  111474. --- linux-3.12.33/sound/soc/bcm/Makefile 1969-12-31 18:00:00.000000000 -0600
  111475. +++ linux-3.12.33-rpi/sound/soc/bcm/Makefile 2014-12-03 19:13:44.596418001 -0600
  111476. @@ -0,0 +1,19 @@
  111477. +# BCM2708 Platform Support
  111478. +snd-soc-bcm2708-i2s-objs := bcm2708-i2s.o
  111479. +
  111480. +obj-$(CONFIG_SND_BCM2708_SOC_I2S) += snd-soc-bcm2708-i2s.o
  111481. +
  111482. +# BCM2708 Machine Support
  111483. +snd-soc-hifiberry-dac-objs := hifiberry_dac.o
  111484. +snd-soc-hifiberry-dacplus-objs := hifiberry_dacplus.o
  111485. +snd-soc-hifiberry-digi-objs := hifiberry_digi.o
  111486. +snd-soc-hifiberry-amp-objs := hifiberry_amp.o
  111487. +snd-soc-rpi-dac-objs := rpi-dac.o
  111488. +snd-soc-iqaudio-dac-objs := iqaudio-dac.o
  111489. +
  111490. +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) += snd-soc-hifiberry-dac.o
  111491. +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS) += snd-soc-hifiberry-dacplus.o
  111492. +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) += snd-soc-hifiberry-digi.o
  111493. +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP) += snd-soc-hifiberry-amp.o
  111494. +obj-$(CONFIG_SND_BCM2708_SOC_RPI_DAC) += snd-soc-rpi-dac.o
  111495. +obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) += snd-soc-iqaudio-dac.o
  111496. diff -Nur linux-3.12.33/sound/soc/bcm/rpi-dac.c linux-3.12.33-rpi/sound/soc/bcm/rpi-dac.c
  111497. --- linux-3.12.33/sound/soc/bcm/rpi-dac.c 1969-12-31 18:00:00.000000000 -0600
  111498. +++ linux-3.12.33-rpi/sound/soc/bcm/rpi-dac.c 2014-12-03 19:13:44.596418001 -0600
  111499. @@ -0,0 +1,97 @@
  111500. +/*
  111501. + * ASoC Driver for RPi-DAC.
  111502. + *
  111503. + * Author: Florian Meier <florian.meier@koalo.de>
  111504. + * Copyright 2013
  111505. + *
  111506. + * This program is free software; you can redistribute it and/or
  111507. + * modify it under the terms of the GNU General Public License
  111508. + * version 2 as published by the Free Software Foundation.
  111509. + *
  111510. + * This program is distributed in the hope that it will be useful, but
  111511. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  111512. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  111513. + * General Public License for more details.
  111514. + */
  111515. +
  111516. +#include <linux/module.h>
  111517. +#include <linux/platform_device.h>
  111518. +
  111519. +#include <sound/core.h>
  111520. +#include <sound/pcm.h>
  111521. +#include <sound/pcm_params.h>
  111522. +#include <sound/soc.h>
  111523. +#include <sound/jack.h>
  111524. +
  111525. +static int snd_rpi_rpi_dac_init(struct snd_soc_pcm_runtime *rtd)
  111526. +{
  111527. + return 0;
  111528. +}
  111529. +
  111530. +static int snd_rpi_rpi_dac_hw_params(struct snd_pcm_substream *substream,
  111531. + struct snd_pcm_hw_params *params)
  111532. +{
  111533. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  111534. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  111535. +
  111536. + return snd_soc_dai_set_bclk_ratio(cpu_dai, 32*2);
  111537. +}
  111538. +
  111539. +/* machine stream operations */
  111540. +static struct snd_soc_ops snd_rpi_rpi_dac_ops = {
  111541. + .hw_params = snd_rpi_rpi_dac_hw_params,
  111542. +};
  111543. +
  111544. +static struct snd_soc_dai_link snd_rpi_rpi_dac_dai[] = {
  111545. +{
  111546. + .name = "RPi-DAC",
  111547. + .stream_name = "RPi-DAC HiFi",
  111548. + .cpu_dai_name = "bcm2708-i2s.0",
  111549. + .codec_dai_name = "pcm1794a-hifi",
  111550. + .platform_name = "bcm2708-i2s.0",
  111551. + .codec_name = "pcm1794a-codec",
  111552. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  111553. + SND_SOC_DAIFMT_CBS_CFS,
  111554. + .ops = &snd_rpi_rpi_dac_ops,
  111555. + .init = snd_rpi_rpi_dac_init,
  111556. +},
  111557. +};
  111558. +
  111559. +/* audio machine driver */
  111560. +static struct snd_soc_card snd_rpi_rpi_dac = {
  111561. + .name = "snd_rpi_rpi_dac",
  111562. + .dai_link = snd_rpi_rpi_dac_dai,
  111563. + .num_links = ARRAY_SIZE(snd_rpi_rpi_dac_dai),
  111564. +};
  111565. +
  111566. +static int snd_rpi_rpi_dac_probe(struct platform_device *pdev)
  111567. +{
  111568. + int ret = 0;
  111569. +
  111570. + snd_rpi_rpi_dac.dev = &pdev->dev;
  111571. + ret = snd_soc_register_card(&snd_rpi_rpi_dac);
  111572. + if (ret)
  111573. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  111574. +
  111575. + return ret;
  111576. +}
  111577. +
  111578. +static int snd_rpi_rpi_dac_remove(struct platform_device *pdev)
  111579. +{
  111580. + return snd_soc_unregister_card(&snd_rpi_rpi_dac);
  111581. +}
  111582. +
  111583. +static struct platform_driver snd_rpi_rpi_dac_driver = {
  111584. + .driver = {
  111585. + .name = "snd-rpi-dac",
  111586. + .owner = THIS_MODULE,
  111587. + },
  111588. + .probe = snd_rpi_rpi_dac_probe,
  111589. + .remove = snd_rpi_rpi_dac_remove,
  111590. +};
  111591. +
  111592. +module_platform_driver(snd_rpi_rpi_dac_driver);
  111593. +
  111594. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  111595. +MODULE_DESCRIPTION("ASoC Driver for RPi-DAC");
  111596. +MODULE_LICENSE("GPL v2");
  111597. diff -Nur linux-3.12.33/sound/soc/codecs/Kconfig linux-3.12.33-rpi/sound/soc/codecs/Kconfig
  111598. --- linux-3.12.33/sound/soc/codecs/Kconfig 2014-11-15 06:28:07.000000000 -0600
  111599. +++ linux-3.12.33-rpi/sound/soc/codecs/Kconfig 2014-12-03 19:13:44.600418001 -0600
  111600. @@ -59,6 +59,9 @@
  111601. select SND_SOC_PCM1681 if I2C
  111602. select SND_SOC_PCM1792A if SPI_MASTER
  111603. select SND_SOC_PCM3008
  111604. + select SND_SOC_PCM1794A
  111605. + select SND_SOC_PCM5102A
  111606. + select SND_SOC_PCM512x if SND_SOC_I2C_AND_SPI
  111607. select SND_SOC_RT5631 if I2C
  111608. select SND_SOC_RT5640 if I2C
  111609. select SND_SOC_SGTL5000 if I2C
  111610. @@ -71,6 +74,7 @@
  111611. select SND_SOC_STA529 if I2C
  111612. select SND_SOC_STAC9766 if SND_SOC_AC97_BUS
  111613. select SND_SOC_TAS5086 if I2C
  111614. + select SND_SOC_TAS5713 if I2C
  111615. select SND_SOC_TLV320AIC23 if I2C
  111616. select SND_SOC_TLV320AIC26 if SPI_MASTER
  111617. select SND_SOC_TLV320AIC32X4 if I2C
  111618. @@ -311,6 +315,15 @@
  111619. config SND_SOC_PCM3008
  111620. tristate
  111621. +config SND_SOC_PCM1794A
  111622. + tristate
  111623. +
  111624. +config SND_SOC_PCM5102A
  111625. + tristate
  111626. +
  111627. +config SND_SOC_PCM512x
  111628. + tristate
  111629. +
  111630. config SND_SOC_RT5631
  111631. tristate
  111632. @@ -352,6 +365,9 @@
  111633. config SND_SOC_TAS5086
  111634. tristate
  111635. +config SND_SOC_TAS5713
  111636. + tristate
  111637. +
  111638. config SND_SOC_TLV320AIC23
  111639. tristate
  111640. diff -Nur linux-3.12.33/sound/soc/codecs/Makefile linux-3.12.33-rpi/sound/soc/codecs/Makefile
  111641. --- linux-3.12.33/sound/soc/codecs/Makefile 2014-11-15 06:28:07.000000000 -0600
  111642. +++ linux-3.12.33-rpi/sound/soc/codecs/Makefile 2014-12-03 19:13:44.600418001 -0600
  111643. @@ -46,6 +46,9 @@
  111644. snd-soc-pcm1681-objs := pcm1681.o
  111645. snd-soc-pcm1792a-codec-objs := pcm1792a.o
  111646. snd-soc-pcm3008-objs := pcm3008.o
  111647. +snd-soc-pcm1794a-objs := pcm1794a.o
  111648. +snd-soc-pcm5102a-objs := pcm5102a.o
  111649. +snd-soc-pcm512x-objs := pcm512x.o
  111650. snd-soc-rt5631-objs := rt5631.o
  111651. snd-soc-rt5640-objs := rt5640.o
  111652. snd-soc-sgtl5000-objs := sgtl5000.o
  111653. @@ -62,6 +65,7 @@
  111654. snd-soc-sta529-objs := sta529.o
  111655. snd-soc-stac9766-objs := stac9766.o
  111656. snd-soc-tas5086-objs := tas5086.o
  111657. +snd-soc-tas5713-objs := tas5713.o
  111658. snd-soc-tlv320aic23-objs := tlv320aic23.o
  111659. snd-soc-tlv320aic26-objs := tlv320aic26.o
  111660. snd-soc-tlv320aic3x-objs := tlv320aic3x.o
  111661. @@ -179,6 +183,9 @@
  111662. obj-$(CONFIG_SND_SOC_PCM1681) += snd-soc-pcm1681.o
  111663. obj-$(CONFIG_SND_SOC_PCM1792A) += snd-soc-pcm1792a-codec.o
  111664. obj-$(CONFIG_SND_SOC_PCM3008) += snd-soc-pcm3008.o
  111665. +obj-$(CONFIG_SND_SOC_PCM1794A) += snd-soc-pcm1794a.o
  111666. +obj-$(CONFIG_SND_SOC_PCM5102A) += snd-soc-pcm5102a.o
  111667. +obj-$(CONFIG_SND_SOC_PCM512x) += snd-soc-pcm512x.o
  111668. obj-$(CONFIG_SND_SOC_RT5631) += snd-soc-rt5631.o
  111669. obj-$(CONFIG_SND_SOC_RT5640) += snd-soc-rt5640.o
  111670. obj-$(CONFIG_SND_SOC_SGTL5000) += snd-soc-sgtl5000.o
  111671. @@ -192,6 +199,7 @@
  111672. obj-$(CONFIG_SND_SOC_STA529) += snd-soc-sta529.o
  111673. obj-$(CONFIG_SND_SOC_STAC9766) += snd-soc-stac9766.o
  111674. obj-$(CONFIG_SND_SOC_TAS5086) += snd-soc-tas5086.o
  111675. +obj-$(CONFIG_SND_SOC_TAS5713) += snd-soc-tas5713.o
  111676. obj-$(CONFIG_SND_SOC_TLV320AIC23) += snd-soc-tlv320aic23.o
  111677. obj-$(CONFIG_SND_SOC_TLV320AIC26) += snd-soc-tlv320aic26.o
  111678. obj-$(CONFIG_SND_SOC_TLV320AIC3X) += snd-soc-tlv320aic3x.o
  111679. diff -Nur linux-3.12.33/sound/soc/codecs/pcm1794a.c linux-3.12.33-rpi/sound/soc/codecs/pcm1794a.c
  111680. --- linux-3.12.33/sound/soc/codecs/pcm1794a.c 1969-12-31 18:00:00.000000000 -0600
  111681. +++ linux-3.12.33-rpi/sound/soc/codecs/pcm1794a.c 2014-12-03 19:13:44.608418001 -0600
  111682. @@ -0,0 +1,62 @@
  111683. +/*
  111684. + * Driver for the PCM1794A codec
  111685. + *
  111686. + * Author: Florian Meier <florian.meier@koalo.de>
  111687. + * Copyright 2013
  111688. + *
  111689. + * This program is free software; you can redistribute it and/or
  111690. + * modify it under the terms of the GNU General Public License
  111691. + * version 2 as published by the Free Software Foundation.
  111692. + *
  111693. + * This program is distributed in the hope that it will be useful, but
  111694. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  111695. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  111696. + * General Public License for more details.
  111697. + */
  111698. +
  111699. +
  111700. +#include <linux/init.h>
  111701. +#include <linux/module.h>
  111702. +#include <linux/platform_device.h>
  111703. +
  111704. +#include <sound/soc.h>
  111705. +
  111706. +static struct snd_soc_dai_driver pcm1794a_dai = {
  111707. + .name = "pcm1794a-hifi",
  111708. + .playback = {
  111709. + .channels_min = 2,
  111710. + .channels_max = 2,
  111711. + .rates = SNDRV_PCM_RATE_8000_192000,
  111712. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  111713. + SNDRV_PCM_FMTBIT_S24_LE
  111714. + },
  111715. +};
  111716. +
  111717. +static struct snd_soc_codec_driver soc_codec_dev_pcm1794a;
  111718. +
  111719. +static int pcm1794a_probe(struct platform_device *pdev)
  111720. +{
  111721. + return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_pcm1794a,
  111722. + &pcm1794a_dai, 1);
  111723. +}
  111724. +
  111725. +static int pcm1794a_remove(struct platform_device *pdev)
  111726. +{
  111727. + snd_soc_unregister_codec(&pdev->dev);
  111728. + return 0;
  111729. +}
  111730. +
  111731. +static struct platform_driver pcm1794a_codec_driver = {
  111732. + .probe = pcm1794a_probe,
  111733. + .remove = pcm1794a_remove,
  111734. + .driver = {
  111735. + .name = "pcm1794a-codec",
  111736. + .owner = THIS_MODULE,
  111737. + },
  111738. +};
  111739. +
  111740. +module_platform_driver(pcm1794a_codec_driver);
  111741. +
  111742. +MODULE_DESCRIPTION("ASoC PCM1794A codec driver");
  111743. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  111744. +MODULE_LICENSE("GPL v2");
  111745. diff -Nur linux-3.12.33/sound/soc/codecs/pcm5102a.c linux-3.12.33-rpi/sound/soc/codecs/pcm5102a.c
  111746. --- linux-3.12.33/sound/soc/codecs/pcm5102a.c 1969-12-31 18:00:00.000000000 -0600
  111747. +++ linux-3.12.33-rpi/sound/soc/codecs/pcm5102a.c 2014-12-03 19:13:44.608418001 -0600
  111748. @@ -0,0 +1,63 @@
  111749. +/*
  111750. + * Driver for the PCM5102A codec
  111751. + *
  111752. + * Author: Florian Meier <florian.meier@koalo.de>
  111753. + * Copyright 2013
  111754. + *
  111755. + * This program is free software; you can redistribute it and/or
  111756. + * modify it under the terms of the GNU General Public License
  111757. + * version 2 as published by the Free Software Foundation.
  111758. + *
  111759. + * This program is distributed in the hope that it will be useful, but
  111760. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  111761. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  111762. + * General Public License for more details.
  111763. + */
  111764. +
  111765. +
  111766. +#include <linux/init.h>
  111767. +#include <linux/module.h>
  111768. +#include <linux/platform_device.h>
  111769. +
  111770. +#include <sound/soc.h>
  111771. +
  111772. +static struct snd_soc_dai_driver pcm5102a_dai = {
  111773. + .name = "pcm5102a-hifi",
  111774. + .playback = {
  111775. + .channels_min = 2,
  111776. + .channels_max = 2,
  111777. + .rates = SNDRV_PCM_RATE_8000_192000,
  111778. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  111779. + SNDRV_PCM_FMTBIT_S24_LE |
  111780. + SNDRV_PCM_FMTBIT_S32_LE
  111781. + },
  111782. +};
  111783. +
  111784. +static struct snd_soc_codec_driver soc_codec_dev_pcm5102a;
  111785. +
  111786. +static int pcm5102a_probe(struct platform_device *pdev)
  111787. +{
  111788. + return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_pcm5102a,
  111789. + &pcm5102a_dai, 1);
  111790. +}
  111791. +
  111792. +static int pcm5102a_remove(struct platform_device *pdev)
  111793. +{
  111794. + snd_soc_unregister_codec(&pdev->dev);
  111795. + return 0;
  111796. +}
  111797. +
  111798. +static struct platform_driver pcm5102a_codec_driver = {
  111799. + .probe = pcm5102a_probe,
  111800. + .remove = pcm5102a_remove,
  111801. + .driver = {
  111802. + .name = "pcm5102a-codec",
  111803. + .owner = THIS_MODULE,
  111804. + },
  111805. +};
  111806. +
  111807. +module_platform_driver(pcm5102a_codec_driver);
  111808. +
  111809. +MODULE_DESCRIPTION("ASoC PCM5102A codec driver");
  111810. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  111811. +MODULE_LICENSE("GPL v2");
  111812. diff -Nur linux-3.12.33/sound/soc/codecs/pcm512x.c linux-3.12.33-rpi/sound/soc/codecs/pcm512x.c
  111813. --- linux-3.12.33/sound/soc/codecs/pcm512x.c 1969-12-31 18:00:00.000000000 -0600
  111814. +++ linux-3.12.33-rpi/sound/soc/codecs/pcm512x.c 2014-12-03 19:13:44.608418001 -0600
  111815. @@ -0,0 +1,678 @@
  111816. +/*
  111817. + * Driver for the PCM512x CODECs
  111818. + *
  111819. + * Author: Mark Brown <broonie@linaro.org>
  111820. + * Copyright 2014 Linaro Ltd
  111821. + *
  111822. + * This program is free software; you can redistribute it and/or
  111823. + * modify it under the terms of the GNU General Public License
  111824. + * version 2 as published by the Free Software Foundation.
  111825. + *
  111826. + * This program is distributed in the hope that it will be useful, but
  111827. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  111828. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  111829. + * General Public License for more details.
  111830. + */
  111831. +
  111832. +
  111833. +#include <linux/init.h>
  111834. +#include <linux/module.h>
  111835. +#include <linux/clk.h>
  111836. +#include <linux/i2c.h>
  111837. +#include <linux/pm_runtime.h>
  111838. +#include <linux/regmap.h>
  111839. +#include <linux/regulator/consumer.h>
  111840. +#include <linux/spi/spi.h>
  111841. +#include <sound/soc.h>
  111842. +#include <sound/soc-dapm.h>
  111843. +#include <sound/tlv.h>
  111844. +
  111845. +#include "pcm512x.h"
  111846. +
  111847. +#define PCM512x_NUM_SUPPLIES 3
  111848. +static const char *pcm512x_supply_names[PCM512x_NUM_SUPPLIES] = {
  111849. + "AVDD",
  111850. + "DVDD",
  111851. + "CPVDD",
  111852. +};
  111853. +
  111854. +struct pcm512x_priv {
  111855. + struct regmap *regmap;
  111856. + struct clk *sclk;
  111857. + struct regulator_bulk_data supplies[PCM512x_NUM_SUPPLIES];
  111858. + struct notifier_block supply_nb[PCM512x_NUM_SUPPLIES];
  111859. +};
  111860. +
  111861. +/*
  111862. + * We can't use the same notifier block for more than one supply and
  111863. + * there's no way I can see to get from a callback to the caller
  111864. + * except container_of().
  111865. + */
  111866. +#define PCM512x_REGULATOR_EVENT(n) \
  111867. +static int pcm512x_regulator_event_##n(struct notifier_block *nb, \
  111868. + unsigned long event, void *data) \
  111869. +{ \
  111870. + struct pcm512x_priv *pcm512x = container_of(nb, struct pcm512x_priv, \
  111871. + supply_nb[n]); \
  111872. + if (event & REGULATOR_EVENT_DISABLE) { \
  111873. + regcache_mark_dirty(pcm512x->regmap); \
  111874. + regcache_cache_only(pcm512x->regmap, true); \
  111875. + } \
  111876. + return 0; \
  111877. +}
  111878. +
  111879. +PCM512x_REGULATOR_EVENT(0)
  111880. +PCM512x_REGULATOR_EVENT(1)
  111881. +PCM512x_REGULATOR_EVENT(2)
  111882. +
  111883. +static const struct reg_default pcm512x_reg_defaults[] = {
  111884. + { PCM512x_RESET, 0x00 },
  111885. + { PCM512x_POWER, 0x00 },
  111886. + { PCM512x_MUTE, 0x00 },
  111887. + { PCM512x_DSP, 0x00 },
  111888. + { PCM512x_PLL_REF, 0x00 },
  111889. + { PCM512x_DAC_ROUTING, 0x11 },
  111890. + { PCM512x_DSP_PROGRAM, 0x01 },
  111891. + { PCM512x_CLKDET, 0x00 },
  111892. + { PCM512x_AUTO_MUTE, 0x00 },
  111893. + { PCM512x_ERROR_DETECT, 0x00 },
  111894. + { PCM512x_DIGITAL_VOLUME_1, 0x00 },
  111895. + { PCM512x_DIGITAL_VOLUME_2, 0x30 },
  111896. + { PCM512x_DIGITAL_VOLUME_3, 0x30 },
  111897. + { PCM512x_DIGITAL_MUTE_1, 0x22 },
  111898. + { PCM512x_DIGITAL_MUTE_2, 0x00 },
  111899. + { PCM512x_DIGITAL_MUTE_3, 0x07 },
  111900. +};
  111901. +
  111902. +static bool pcm512x_readable(struct device *dev, unsigned int reg)
  111903. +{
  111904. + switch (reg) {
  111905. + case PCM512x_RESET:
  111906. + case PCM512x_POWER:
  111907. + case PCM512x_MUTE:
  111908. + case PCM512x_PLL_EN:
  111909. + case PCM512x_SPI_MISO_FUNCTION:
  111910. + case PCM512x_DSP:
  111911. + case PCM512x_GPIO_EN:
  111912. + case PCM512x_BCLK_LRCLK_CFG:
  111913. + case PCM512x_DSP_GPIO_INPUT:
  111914. + case PCM512x_MASTER_MODE:
  111915. + case PCM512x_PLL_REF:
  111916. + case PCM512x_PLL_COEFF_0:
  111917. + case PCM512x_PLL_COEFF_1:
  111918. + case PCM512x_PLL_COEFF_2:
  111919. + case PCM512x_PLL_COEFF_3:
  111920. + case PCM512x_PLL_COEFF_4:
  111921. + case PCM512x_DSP_CLKDIV:
  111922. + case PCM512x_DAC_CLKDIV:
  111923. + case PCM512x_NCP_CLKDIV:
  111924. + case PCM512x_OSR_CLKDIV:
  111925. + case PCM512x_MASTER_CLKDIV_1:
  111926. + case PCM512x_MASTER_CLKDIV_2:
  111927. + case PCM512x_FS_SPEED_MODE:
  111928. + case PCM512x_IDAC_1:
  111929. + case PCM512x_IDAC_2:
  111930. + case PCM512x_ERROR_DETECT:
  111931. + case PCM512x_I2S_1:
  111932. + case PCM512x_I2S_2:
  111933. + case PCM512x_DAC_ROUTING:
  111934. + case PCM512x_DSP_PROGRAM:
  111935. + case PCM512x_CLKDET:
  111936. + case PCM512x_AUTO_MUTE:
  111937. + case PCM512x_DIGITAL_VOLUME_1:
  111938. + case PCM512x_DIGITAL_VOLUME_2:
  111939. + case PCM512x_DIGITAL_VOLUME_3:
  111940. + case PCM512x_DIGITAL_MUTE_1:
  111941. + case PCM512x_DIGITAL_MUTE_2:
  111942. + case PCM512x_DIGITAL_MUTE_3:
  111943. + case PCM512x_GPIO_OUTPUT_1:
  111944. + case PCM512x_GPIO_OUTPUT_2:
  111945. + case PCM512x_GPIO_OUTPUT_3:
  111946. + case PCM512x_GPIO_OUTPUT_4:
  111947. + case PCM512x_GPIO_OUTPUT_5:
  111948. + case PCM512x_GPIO_OUTPUT_6:
  111949. + case PCM512x_GPIO_CONTROL_1:
  111950. + case PCM512x_GPIO_CONTROL_2:
  111951. + case PCM512x_OVERFLOW:
  111952. + case PCM512x_RATE_DET_1:
  111953. + case PCM512x_RATE_DET_2:
  111954. + case PCM512x_RATE_DET_3:
  111955. + case PCM512x_RATE_DET_4:
  111956. + case PCM512x_ANALOG_MUTE_DET:
  111957. + case PCM512x_GPIN:
  111958. + case PCM512x_DIGITAL_MUTE_DET:
  111959. + return true;
  111960. + default:
  111961. + return false;
  111962. + }
  111963. +}
  111964. +
  111965. +static bool pcm512x_volatile(struct device *dev, unsigned int reg)
  111966. +{
  111967. + switch (reg) {
  111968. + case PCM512x_PLL_EN:
  111969. + case PCM512x_OVERFLOW:
  111970. + case PCM512x_RATE_DET_1:
  111971. + case PCM512x_RATE_DET_2:
  111972. + case PCM512x_RATE_DET_3:
  111973. + case PCM512x_RATE_DET_4:
  111974. + case PCM512x_ANALOG_MUTE_DET:
  111975. + case PCM512x_GPIN:
  111976. + case PCM512x_DIGITAL_MUTE_DET:
  111977. + return true;
  111978. + default:
  111979. + return false;
  111980. + }
  111981. +}
  111982. +
  111983. +static const DECLARE_TLV_DB_SCALE(digital_tlv, -10350, 50, 1);
  111984. +
  111985. +static const char *pcm512x_dsp_program_texts[] = {
  111986. + "FIR interpolation with de-emphasis",
  111987. + "Low latency IIR with de-emphasis",
  111988. + "High attenuation with de-emphasis",
  111989. + "Ringing-less low latency FIR",
  111990. +};
  111991. +
  111992. +static const unsigned int pcm512x_dsp_program_values[] = {
  111993. + 1,
  111994. + 2,
  111995. + 3,
  111996. + 5,
  111997. + 7,
  111998. +};
  111999. +
  112000. +static const SOC_VALUE_ENUM_SINGLE_DECL(pcm512x_dsp_program,
  112001. + PCM512x_DSP_PROGRAM, 0, 0x1f,
  112002. + pcm512x_dsp_program_texts,
  112003. + pcm512x_dsp_program_values);
  112004. +
  112005. +static const char *pcm512x_clk_missing_text[] = {
  112006. + "1s", "2s", "3s", "4s", "5s", "6s", "7s", "8s"
  112007. +};
  112008. +
  112009. +static const struct soc_enum pcm512x_clk_missing =
  112010. + SOC_ENUM_SINGLE(PCM512x_CLKDET, 0, 7, pcm512x_clk_missing_text);
  112011. +
  112012. +static const char *pcm512x_autom_text[] = {
  112013. + "21ms", "106ms", "213ms", "533ms", "1.07s", "2.13s", "5.33s", "10.66s"
  112014. +};
  112015. +
  112016. +static const struct soc_enum pcm512x_autom_l =
  112017. + SOC_ENUM_SINGLE(PCM512x_AUTO_MUTE, PCM512x_ATML_SHIFT, 7,
  112018. + pcm512x_autom_text);
  112019. +
  112020. +static const struct soc_enum pcm512x_autom_r =
  112021. + SOC_ENUM_SINGLE(PCM512x_AUTO_MUTE, PCM512x_ATMR_SHIFT, 7,
  112022. + pcm512x_autom_text);
  112023. +
  112024. +static const char *pcm512x_ramp_rate_text[] = {
  112025. + "1 sample/update", "2 samples/update", "4 samples/update",
  112026. + "Immediate"
  112027. +};
  112028. +
  112029. +static const struct soc_enum pcm512x_vndf =
  112030. + SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNDF_SHIFT, 4,
  112031. + pcm512x_ramp_rate_text);
  112032. +
  112033. +static const struct soc_enum pcm512x_vnuf =
  112034. + SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNUF_SHIFT, 4,
  112035. + pcm512x_ramp_rate_text);
  112036. +
  112037. +static const struct soc_enum pcm512x_vedf =
  112038. + SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_2, PCM512x_VEDF_SHIFT, 4,
  112039. + pcm512x_ramp_rate_text);
  112040. +
  112041. +static const char *pcm512x_ramp_step_text[] = {
  112042. + "4dB/step", "2dB/step", "1dB/step", "0.5dB/step"
  112043. +};
  112044. +
  112045. +static const struct soc_enum pcm512x_vnds =
  112046. + SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNDS_SHIFT, 4,
  112047. + pcm512x_ramp_step_text);
  112048. +
  112049. +static const struct soc_enum pcm512x_vnus =
  112050. + SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNUS_SHIFT, 4,
  112051. + pcm512x_ramp_step_text);
  112052. +
  112053. +static const struct soc_enum pcm512x_veds =
  112054. + SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_2, PCM512x_VEDS_SHIFT, 4,
  112055. + pcm512x_ramp_step_text);
  112056. +
  112057. +/* Don't let the DAC go into clipping by limiting the alsa volume control range */
  112058. +static const struct snd_kcontrol_new pcm512x_controls[] = {
  112059. +SOC_DOUBLE_R_RANGE_TLV("Playback Digital Volume", PCM512x_DIGITAL_VOLUME_2,
  112060. + PCM512x_DIGITAL_VOLUME_3, 0, 40, 255, 1, digital_tlv),
  112061. +SOC_DOUBLE("Playback Digital Switch", PCM512x_MUTE, PCM512x_RQML_SHIFT,
  112062. + PCM512x_RQMR_SHIFT, 1, 1),
  112063. +
  112064. +SOC_SINGLE("Deemphasis Switch", PCM512x_DSP, PCM512x_DEMP_SHIFT, 1, 1),
  112065. +SOC_VALUE_ENUM("DSP Program", pcm512x_dsp_program),
  112066. +
  112067. +SOC_ENUM("Clock Missing Period", pcm512x_clk_missing),
  112068. +SOC_ENUM("Auto Mute Time Left", pcm512x_autom_l),
  112069. +SOC_ENUM("Auto Mute Time Right", pcm512x_autom_r),
  112070. +SOC_SINGLE("Auto Mute Mono Switch", PCM512x_DIGITAL_MUTE_3,
  112071. + PCM512x_ACTL_SHIFT, 1, 0),
  112072. +SOC_DOUBLE("Auto Mute Switch", PCM512x_DIGITAL_MUTE_3, PCM512x_AMLE_SHIFT,
  112073. + PCM512x_AMLR_SHIFT, 1, 0),
  112074. +
  112075. +SOC_ENUM("Volume Ramp Down Rate", pcm512x_vndf),
  112076. +SOC_ENUM("Volume Ramp Down Step", pcm512x_vnds),
  112077. +SOC_ENUM("Volume Ramp Up Rate", pcm512x_vnuf),
  112078. +SOC_ENUM("Volume Ramp Up Step", pcm512x_vnus),
  112079. +SOC_ENUM("Volume Ramp Down Emergency Rate", pcm512x_vedf),
  112080. +SOC_ENUM("Volume Ramp Down Emergency Step", pcm512x_veds),
  112081. +};
  112082. +
  112083. +static const struct snd_soc_dapm_widget pcm512x_dapm_widgets[] = {
  112084. +SND_SOC_DAPM_DAC("DACL", NULL, SND_SOC_NOPM, 0, 0),
  112085. +SND_SOC_DAPM_DAC("DACR", NULL, SND_SOC_NOPM, 0, 0),
  112086. +
  112087. +SND_SOC_DAPM_OUTPUT("OUTL"),
  112088. +SND_SOC_DAPM_OUTPUT("OUTR"),
  112089. +};
  112090. +
  112091. +static const struct snd_soc_dapm_route pcm512x_dapm_routes[] = {
  112092. + { "DACL", NULL, "Playback" },
  112093. + { "DACR", NULL, "Playback" },
  112094. +
  112095. + { "OUTL", NULL, "DACL" },
  112096. + { "OUTR", NULL, "DACR" },
  112097. +};
  112098. +
  112099. +static int pcm512x_set_bias_level(struct snd_soc_codec *codec,
  112100. + enum snd_soc_bias_level level)
  112101. +{
  112102. + struct pcm512x_priv *pcm512x = dev_get_drvdata(codec->dev);
  112103. + int ret;
  112104. +
  112105. + switch (level) {
  112106. + case SND_SOC_BIAS_ON:
  112107. + case SND_SOC_BIAS_PREPARE:
  112108. + break;
  112109. +
  112110. + case SND_SOC_BIAS_STANDBY:
  112111. + ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
  112112. + PCM512x_RQST, 0);
  112113. + if (ret != 0) {
  112114. + dev_err(codec->dev, "Failed to remove standby: %d\n",
  112115. + ret);
  112116. + return ret;
  112117. + }
  112118. + break;
  112119. +
  112120. + case SND_SOC_BIAS_OFF:
  112121. + ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
  112122. + PCM512x_RQST, PCM512x_RQST);
  112123. + if (ret != 0) {
  112124. + dev_err(codec->dev, "Failed to request standby: %d\n",
  112125. + ret);
  112126. + return ret;
  112127. + }
  112128. + break;
  112129. + }
  112130. +
  112131. + codec->dapm.bias_level = level;
  112132. +
  112133. + return 0;
  112134. +}
  112135. +
  112136. +static struct snd_soc_dai_driver pcm512x_dai = {
  112137. + .name = "pcm512x-hifi",
  112138. + .playback = {
  112139. + .stream_name = "Playback",
  112140. + .channels_min = 2,
  112141. + .channels_max = 2,
  112142. + .rates = SNDRV_PCM_RATE_8000_192000,
  112143. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  112144. + SNDRV_PCM_FMTBIT_S24_LE |
  112145. + SNDRV_PCM_FMTBIT_S32_LE
  112146. + },
  112147. +};
  112148. +
  112149. +static struct snd_soc_codec_driver pcm512x_codec_driver = {
  112150. + .set_bias_level = pcm512x_set_bias_level,
  112151. + .idle_bias_off = true,
  112152. +
  112153. + .controls = pcm512x_controls,
  112154. + .num_controls = ARRAY_SIZE(pcm512x_controls),
  112155. + .dapm_widgets = pcm512x_dapm_widgets,
  112156. + .num_dapm_widgets = ARRAY_SIZE(pcm512x_dapm_widgets),
  112157. + .dapm_routes = pcm512x_dapm_routes,
  112158. + .num_dapm_routes = ARRAY_SIZE(pcm512x_dapm_routes),
  112159. +};
  112160. +
  112161. +static const struct regmap_config pcm512x_regmap = {
  112162. + .reg_bits = 8,
  112163. + .val_bits = 8,
  112164. +
  112165. + .readable_reg = pcm512x_readable,
  112166. + .volatile_reg = pcm512x_volatile,
  112167. +
  112168. + .max_register = PCM512x_MAX_REGISTER,
  112169. + .reg_defaults = pcm512x_reg_defaults,
  112170. + .num_reg_defaults = ARRAY_SIZE(pcm512x_reg_defaults),
  112171. + .cache_type = REGCACHE_RBTREE,
  112172. +};
  112173. +
  112174. +static const struct of_device_id pcm512x_of_match[] = {
  112175. + { .compatible = "ti,pcm5121", },
  112176. + { .compatible = "ti,pcm5122", },
  112177. + { }
  112178. +};
  112179. +MODULE_DEVICE_TABLE(of, pcm512x_of_match);
  112180. +
  112181. +static int pcm512x_probe(struct device *dev, struct regmap *regmap)
  112182. +{
  112183. + struct pcm512x_priv *pcm512x;
  112184. + int i, ret;
  112185. +
  112186. + pcm512x = devm_kzalloc(dev, sizeof(struct pcm512x_priv), GFP_KERNEL);
  112187. + if (!pcm512x)
  112188. + return -ENOMEM;
  112189. +
  112190. + dev_set_drvdata(dev, pcm512x);
  112191. + pcm512x->regmap = regmap;
  112192. +
  112193. + for (i = 0; i < ARRAY_SIZE(pcm512x->supplies); i++)
  112194. + pcm512x->supplies[i].supply = pcm512x_supply_names[i];
  112195. +
  112196. + ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(pcm512x->supplies),
  112197. + pcm512x->supplies);
  112198. + if (ret != 0) {
  112199. + dev_err(dev, "Failed to get supplies: %d\n", ret);
  112200. + return ret;
  112201. + }
  112202. +
  112203. + pcm512x->supply_nb[0].notifier_call = pcm512x_regulator_event_0;
  112204. + pcm512x->supply_nb[1].notifier_call = pcm512x_regulator_event_1;
  112205. + pcm512x->supply_nb[2].notifier_call = pcm512x_regulator_event_2;
  112206. +
  112207. + for (i = 0; i < ARRAY_SIZE(pcm512x->supplies); i++) {
  112208. + ret = regulator_register_notifier(pcm512x->supplies[i].consumer,
  112209. + &pcm512x->supply_nb[i]);
  112210. + if (ret != 0) {
  112211. + dev_err(dev,
  112212. + "Failed to register regulator notifier: %d\n",
  112213. + ret);
  112214. + }
  112215. + }
  112216. +
  112217. + ret = regulator_bulk_enable(ARRAY_SIZE(pcm512x->supplies),
  112218. + pcm512x->supplies);
  112219. + if (ret != 0) {
  112220. + dev_err(dev, "Failed to enable supplies: %d\n", ret);
  112221. + return ret;
  112222. + }
  112223. +
  112224. + /* Reset the device, verifying I/O in the process for I2C */
  112225. + ret = regmap_write(regmap, PCM512x_RESET,
  112226. + PCM512x_RSTM | PCM512x_RSTR);
  112227. + if (ret != 0) {
  112228. + dev_err(dev, "Failed to reset device: %d\n", ret);
  112229. + goto err;
  112230. + }
  112231. +
  112232. + ret = regmap_write(regmap, PCM512x_RESET, 0);
  112233. + if (ret != 0) {
  112234. + dev_err(dev, "Failed to reset device: %d\n", ret);
  112235. + goto err;
  112236. + }
  112237. +
  112238. + pcm512x->sclk = devm_clk_get(dev, NULL);
  112239. + if (IS_ERR(pcm512x->sclk)) {
  112240. + if (PTR_ERR(pcm512x->sclk) == -EPROBE_DEFER)
  112241. + return -EPROBE_DEFER;
  112242. +
  112243. + dev_info(dev, "No SCLK, using BCLK: %ld\n",
  112244. + PTR_ERR(pcm512x->sclk));
  112245. +
  112246. + /* Disable reporting of missing SCLK as an error */
  112247. + regmap_update_bits(regmap, PCM512x_ERROR_DETECT,
  112248. + PCM512x_IDCH, PCM512x_IDCH);
  112249. +
  112250. + /* Switch PLL input to BCLK */
  112251. + regmap_update_bits(regmap, PCM512x_PLL_REF,
  112252. + PCM512x_SREF, PCM512x_SREF);
  112253. + } else {
  112254. + ret = clk_prepare_enable(pcm512x->sclk);
  112255. + if (ret != 0) {
  112256. + dev_err(dev, "Failed to enable SCLK: %d\n", ret);
  112257. + return ret;
  112258. + }
  112259. + }
  112260. +
  112261. + /* Default to standby mode */
  112262. + ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
  112263. + PCM512x_RQST, PCM512x_RQST);
  112264. + if (ret != 0) {
  112265. + dev_err(dev, "Failed to request standby: %d\n",
  112266. + ret);
  112267. + goto err_clk;
  112268. + }
  112269. +
  112270. + pm_runtime_set_active(dev);
  112271. + pm_runtime_enable(dev);
  112272. + pm_runtime_idle(dev);
  112273. +
  112274. + ret = snd_soc_register_codec(dev, &pcm512x_codec_driver,
  112275. + &pcm512x_dai, 1);
  112276. + if (ret != 0) {
  112277. + dev_err(dev, "Failed to register CODEC: %d\n", ret);
  112278. + goto err_pm;
  112279. + }
  112280. +
  112281. + dev_info(dev, "Completed initialisation - pcm512x_probe");
  112282. +
  112283. + return 0;
  112284. +
  112285. +err_pm:
  112286. + pm_runtime_disable(dev);
  112287. +err_clk:
  112288. + if (!IS_ERR(pcm512x->sclk))
  112289. + clk_disable_unprepare(pcm512x->sclk);
  112290. +err:
  112291. + regulator_bulk_disable(ARRAY_SIZE(pcm512x->supplies),
  112292. + pcm512x->supplies);
  112293. + return ret;
  112294. +}
  112295. +
  112296. +static void pcm512x_remove(struct device *dev)
  112297. +{
  112298. + struct pcm512x_priv *pcm512x = dev_get_drvdata(dev);
  112299. +
  112300. + snd_soc_unregister_codec(dev);
  112301. + pm_runtime_disable(dev);
  112302. + if (!IS_ERR(pcm512x->sclk))
  112303. + clk_disable_unprepare(pcm512x->sclk);
  112304. + regulator_bulk_disable(ARRAY_SIZE(pcm512x->supplies),
  112305. + pcm512x->supplies);
  112306. +}
  112307. +
  112308. +/* TODO
  112309. +static int pcm512x_suspend(struct device *dev)
  112310. +{
  112311. + struct pcm512x_priv *pcm512x = dev_get_drvdata(dev);
  112312. + int ret;
  112313. +
  112314. + ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
  112315. + PCM512x_RQPD, PCM512x_RQPD);
  112316. + if (ret != 0) {
  112317. + dev_err(dev, "Failed to request power down: %d\n", ret);
  112318. + return ret;
  112319. + }
  112320. +
  112321. + ret = regulator_bulk_disable(ARRAY_SIZE(pcm512x->supplies),
  112322. + pcm512x->supplies);
  112323. + if (ret != 0) {
  112324. + dev_err(dev, "Failed to disable supplies: %d\n", ret);
  112325. + return ret;
  112326. + }
  112327. +
  112328. + if (!IS_ERR(pcm512x->sclk))
  112329. + clk_disable_unprepare(pcm512x->sclk);
  112330. +
  112331. + return 0;
  112332. +}
  112333. +
  112334. +static int pcm512x_resume(struct device *dev)
  112335. +{
  112336. + struct pcm512x_priv *pcm512x = dev_get_drvdata(dev);
  112337. + int ret;
  112338. +
  112339. + if (!IS_ERR(pcm512x->sclk)) {
  112340. + ret = clk_prepare_enable(pcm512x->sclk);
  112341. + if (ret != 0) {
  112342. + dev_err(dev, "Failed to enable SCLK: %d\n", ret);
  112343. + return ret;
  112344. + }
  112345. + }
  112346. +
  112347. + ret = regulator_bulk_enable(ARRAY_SIZE(pcm512x->supplies),
  112348. + pcm512x->supplies);
  112349. + if (ret != 0) {
  112350. + dev_err(dev, "Failed to enable supplies: %d\n", ret);
  112351. + return ret;
  112352. + }
  112353. +
  112354. + regcache_cache_only(pcm512x->regmap, false);
  112355. + ret = regcache_sync(pcm512x->regmap);
  112356. + if (ret != 0) {
  112357. + dev_err(dev, "Failed to sync cache: %d\n", ret);
  112358. + return ret;
  112359. + }
  112360. +
  112361. + ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
  112362. + PCM512x_RQPD, 0);
  112363. + if (ret != 0) {
  112364. + dev_err(dev, "Failed to remove power down: %d\n", ret);
  112365. + return ret;
  112366. + }
  112367. +
  112368. + return 0;
  112369. +}
  112370. +
  112371. +// END OF PCM512x_suspend and resume calls TODO
  112372. +*/
  112373. +
  112374. +static const struct dev_pm_ops pcm512x_pm_ops = {
  112375. + SET_RUNTIME_PM_OPS(pcm512x_suspend, pcm512x_resume, NULL)
  112376. +};
  112377. +
  112378. +#if IS_ENABLED(CONFIG_I2C)
  112379. +static int pcm512x_i2c_probe(struct i2c_client *i2c,
  112380. + const struct i2c_device_id *id)
  112381. +{
  112382. + struct regmap *regmap;
  112383. +
  112384. + regmap = devm_regmap_init_i2c(i2c, &pcm512x_regmap);
  112385. + if (IS_ERR(regmap))
  112386. + return PTR_ERR(regmap);
  112387. +
  112388. + return pcm512x_probe(&i2c->dev, regmap);
  112389. +}
  112390. +
  112391. +static int pcm512x_i2c_remove(struct i2c_client *i2c)
  112392. +{
  112393. + pcm512x_remove(&i2c->dev);
  112394. + return 0;
  112395. +}
  112396. +
  112397. +static const struct i2c_device_id pcm512x_i2c_id[] = {
  112398. + { "pcm5121", },
  112399. + { "pcm5122", },
  112400. + { }
  112401. +};
  112402. +MODULE_DEVICE_TABLE(i2c, pcm512x_i2c_id);
  112403. +
  112404. +static struct i2c_driver pcm512x_i2c_driver = {
  112405. + .probe = pcm512x_i2c_probe,
  112406. + .remove = pcm512x_i2c_remove,
  112407. + .id_table = pcm512x_i2c_id,
  112408. + .driver = {
  112409. + .name = "pcm512x",
  112410. + .owner = THIS_MODULE,
  112411. + .of_match_table = pcm512x_of_match,
  112412. + .pm = &pcm512x_pm_ops,
  112413. + },
  112414. +};
  112415. +#endif
  112416. +
  112417. +#if defined(CONFIG_SPI_MASTER)
  112418. +static int pcm512x_spi_probe(struct spi_device *spi)
  112419. +{
  112420. + struct regmap *regmap;
  112421. + int ret;
  112422. +
  112423. + regmap = devm_regmap_init_spi(spi, &pcm512x_regmap);
  112424. + if (IS_ERR(regmap)) {
  112425. + ret = PTR_ERR(regmap);
  112426. + return ret;
  112427. + }
  112428. +
  112429. + return pcm512x_probe(&spi->dev, regmap);
  112430. +}
  112431. +
  112432. +static int pcm512x_spi_remove(struct spi_device *spi)
  112433. +{
  112434. + pcm512x_remove(&spi->dev);
  112435. + return 0;
  112436. +}
  112437. +
  112438. +static const struct spi_device_id pcm512x_spi_id[] = {
  112439. + { "pcm5121", },
  112440. + { "pcm5122", },
  112441. + { },
  112442. +};
  112443. +MODULE_DEVICE_TABLE(spi, pcm512x_spi_id);
  112444. +
  112445. +static struct spi_driver pcm512x_spi_driver = {
  112446. + .probe = pcm512x_spi_probe,
  112447. + .remove = pcm512x_spi_remove,
  112448. + .id_table = pcm512x_spi_id,
  112449. + .driver = {
  112450. + .name = "pcm512x",
  112451. + .owner = THIS_MODULE,
  112452. + .of_match_table = pcm512x_of_match,
  112453. + .pm = &pcm512x_pm_ops,
  112454. + },
  112455. +};
  112456. +#endif
  112457. +
  112458. +static int __init pcm512x_modinit(void)
  112459. +{
  112460. + int ret = 0;
  112461. +
  112462. +#if IS_ENABLED(CONFIG_I2C)
  112463. + ret = i2c_add_driver(&pcm512x_i2c_driver);
  112464. + if (ret) {
  112465. + printk(KERN_ERR "Failed to register pcm512x I2C driver: %d\n",
  112466. + ret);
  112467. + }
  112468. +#endif
  112469. +#if defined(CONFIG_SPI_MASTER)
  112470. + ret = spi_register_driver(&pcm512x_spi_driver);
  112471. + if (ret != 0) {
  112472. + printk(KERN_ERR "Failed to register pcm512x SPI driver: %d\n",
  112473. + ret);
  112474. + }
  112475. +#endif
  112476. + return ret;
  112477. +}
  112478. +module_init(pcm512x_modinit);
  112479. +
  112480. +static void __exit pcm512x_exit(void)
  112481. +{
  112482. +#if IS_ENABLED(CONFIG_I2C)
  112483. + i2c_del_driver(&pcm512x_i2c_driver);
  112484. +#endif
  112485. +#if defined(CONFIG_SPI_MASTER)
  112486. + spi_unregister_driver(&pcm512x_spi_driver);
  112487. +#endif
  112488. +}
  112489. +module_exit(pcm512x_exit);
  112490. +
  112491. +MODULE_DESCRIPTION("ASoC PCM512x codec driver");
  112492. +MODULE_AUTHOR("Mark Brown <broonie@linaro.org>");
  112493. +MODULE_LICENSE("GPL v2");
  112494. diff -Nur linux-3.12.33/sound/soc/codecs/pcm512x.h linux-3.12.33-rpi/sound/soc/codecs/pcm512x.h
  112495. --- linux-3.12.33/sound/soc/codecs/pcm512x.h 1969-12-31 18:00:00.000000000 -0600
  112496. +++ linux-3.12.33-rpi/sound/soc/codecs/pcm512x.h 2014-12-03 19:13:44.608418001 -0600
  112497. @@ -0,0 +1,142 @@
  112498. +/*
  112499. + * Driver for the PCM512x CODECs
  112500. + *
  112501. + * Author: Mark Brown <broonie@linaro.org>
  112502. + * Copyright 2014 Linaro Ltd
  112503. + *
  112504. + * This program is free software; you can redistribute it and/or
  112505. + * modify it under the terms of the GNU General Public License
  112506. + * version 2 as published by the Free Software Foundation.
  112507. + *
  112508. + * This program is distributed in the hope that it will be useful, but
  112509. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  112510. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  112511. + * General Public License for more details.
  112512. + */
  112513. +
  112514. +#ifndef _SND_SOC_PCM512X
  112515. +#define _SND_SOC_PCM512X
  112516. +
  112517. +#define PCM512x_PAGE_0_BASE 0
  112518. +
  112519. +#define PCM512x_PAGE 0
  112520. +
  112521. +#define PCM512x_RESET (PCM512x_PAGE_0_BASE + 1)
  112522. +#define PCM512x_POWER (PCM512x_PAGE_0_BASE + 2)
  112523. +#define PCM512x_MUTE (PCM512x_PAGE_0_BASE + 3)
  112524. +#define PCM512x_PLL_EN (PCM512x_PAGE_0_BASE + 4)
  112525. +#define PCM512x_SPI_MISO_FUNCTION (PCM512x_PAGE_0_BASE + 6)
  112526. +#define PCM512x_DSP (PCM512x_PAGE_0_BASE + 7)
  112527. +#define PCM512x_GPIO_EN (PCM512x_PAGE_0_BASE + 8)
  112528. +#define PCM512x_BCLK_LRCLK_CFG (PCM512x_PAGE_0_BASE + 9)
  112529. +#define PCM512x_DSP_GPIO_INPUT (PCM512x_PAGE_0_BASE + 10)
  112530. +#define PCM512x_MASTER_MODE (PCM512x_PAGE_0_BASE + 12)
  112531. +#define PCM512x_PLL_REF (PCM512x_PAGE_0_BASE + 13)
  112532. +#define PCM512x_PLL_COEFF_0 (PCM512x_PAGE_0_BASE + 20)
  112533. +#define PCM512x_PLL_COEFF_1 (PCM512x_PAGE_0_BASE + 21)
  112534. +#define PCM512x_PLL_COEFF_2 (PCM512x_PAGE_0_BASE + 22)
  112535. +#define PCM512x_PLL_COEFF_3 (PCM512x_PAGE_0_BASE + 23)
  112536. +#define PCM512x_PLL_COEFF_4 (PCM512x_PAGE_0_BASE + 24)
  112537. +#define PCM512x_DSP_CLKDIV (PCM512x_PAGE_0_BASE + 27)
  112538. +#define PCM512x_DAC_CLKDIV (PCM512x_PAGE_0_BASE + 28)
  112539. +#define PCM512x_NCP_CLKDIV (PCM512x_PAGE_0_BASE + 29)
  112540. +#define PCM512x_OSR_CLKDIV (PCM512x_PAGE_0_BASE + 30)
  112541. +#define PCM512x_MASTER_CLKDIV_1 (PCM512x_PAGE_0_BASE + 32)
  112542. +#define PCM512x_MASTER_CLKDIV_2 (PCM512x_PAGE_0_BASE + 33)
  112543. +#define PCM512x_FS_SPEED_MODE (PCM512x_PAGE_0_BASE + 34)
  112544. +#define PCM512x_IDAC_1 (PCM512x_PAGE_0_BASE + 35)
  112545. +#define PCM512x_IDAC_2 (PCM512x_PAGE_0_BASE + 36)
  112546. +#define PCM512x_ERROR_DETECT (PCM512x_PAGE_0_BASE + 37)
  112547. +#define PCM512x_I2S_1 (PCM512x_PAGE_0_BASE + 40)
  112548. +#define PCM512x_I2S_2 (PCM512x_PAGE_0_BASE + 41)
  112549. +#define PCM512x_DAC_ROUTING (PCM512x_PAGE_0_BASE + 42)
  112550. +#define PCM512x_DSP_PROGRAM (PCM512x_PAGE_0_BASE + 43)
  112551. +#define PCM512x_CLKDET (PCM512x_PAGE_0_BASE + 44)
  112552. +#define PCM512x_AUTO_MUTE (PCM512x_PAGE_0_BASE + 59)
  112553. +#define PCM512x_DIGITAL_VOLUME_1 (PCM512x_PAGE_0_BASE + 60)
  112554. +#define PCM512x_DIGITAL_VOLUME_2 (PCM512x_PAGE_0_BASE + 61)
  112555. +#define PCM512x_DIGITAL_VOLUME_3 (PCM512x_PAGE_0_BASE + 62)
  112556. +#define PCM512x_DIGITAL_MUTE_1 (PCM512x_PAGE_0_BASE + 63)
  112557. +#define PCM512x_DIGITAL_MUTE_2 (PCM512x_PAGE_0_BASE + 64)
  112558. +#define PCM512x_DIGITAL_MUTE_3 (PCM512x_PAGE_0_BASE + 65)
  112559. +#define PCM512x_GPIO_OUTPUT_1 (PCM512x_PAGE_0_BASE + 80)
  112560. +#define PCM512x_GPIO_OUTPUT_2 (PCM512x_PAGE_0_BASE + 81)
  112561. +#define PCM512x_GPIO_OUTPUT_3 (PCM512x_PAGE_0_BASE + 82)
  112562. +#define PCM512x_GPIO_OUTPUT_4 (PCM512x_PAGE_0_BASE + 83)
  112563. +#define PCM512x_GPIO_OUTPUT_5 (PCM512x_PAGE_0_BASE + 84)
  112564. +#define PCM512x_GPIO_OUTPUT_6 (PCM512x_PAGE_0_BASE + 85)
  112565. +#define PCM512x_GPIO_CONTROL_1 (PCM512x_PAGE_0_BASE + 86)
  112566. +#define PCM512x_GPIO_CONTROL_2 (PCM512x_PAGE_0_BASE + 87)
  112567. +#define PCM512x_OVERFLOW (PCM512x_PAGE_0_BASE + 90)
  112568. +#define PCM512x_RATE_DET_1 (PCM512x_PAGE_0_BASE + 91)
  112569. +#define PCM512x_RATE_DET_2 (PCM512x_PAGE_0_BASE + 92)
  112570. +#define PCM512x_RATE_DET_3 (PCM512x_PAGE_0_BASE + 93)
  112571. +#define PCM512x_RATE_DET_4 (PCM512x_PAGE_0_BASE + 94)
  112572. +#define PCM512x_ANALOG_MUTE_DET (PCM512x_PAGE_0_BASE + 108)
  112573. +#define PCM512x_GPIN (PCM512x_PAGE_0_BASE + 119)
  112574. +#define PCM512x_DIGITAL_MUTE_DET (PCM512x_PAGE_0_BASE + 120)
  112575. +
  112576. +#define PCM512x_MAX_REGISTER (PCM512x_PAGE_0_BASE + 120)
  112577. +
  112578. +/* Page 0, Register 1 - reset */
  112579. +#define PCM512x_RSTR (1 << 0)
  112580. +#define PCM512x_RSTM (1 << 4)
  112581. +
  112582. +/* Page 0, Register 2 - power */
  112583. +#define PCM512x_RQPD (1 << 0)
  112584. +#define PCM512x_RQPD_SHIFT 0
  112585. +#define PCM512x_RQST (1 << 4)
  112586. +#define PCM512x_RQST_SHIFT 4
  112587. +
  112588. +/* Page 0, Register 3 - mute */
  112589. +#define PCM512x_RQMR_SHIFT 0
  112590. +#define PCM512x_RQML_SHIFT 4
  112591. +
  112592. +/* Page 0, Register 4 - PLL */
  112593. +#define PCM512x_PLCE (1 << 0)
  112594. +#define PCM512x_RLCE_SHIFT 0
  112595. +#define PCM512x_PLCK (1 << 4)
  112596. +#define PCM512x_PLCK_SHIFT 4
  112597. +
  112598. +/* Page 0, Register 7 - DSP */
  112599. +#define PCM512x_SDSL (1 << 0)
  112600. +#define PCM512x_SDSL_SHIFT 0
  112601. +#define PCM512x_DEMP (1 << 4)
  112602. +#define PCM512x_DEMP_SHIFT 4
  112603. +
  112604. +/* Page 0, Register 13 - PLL reference */
  112605. +#define PCM512x_SREF (1 << 4)
  112606. +
  112607. +/* Page 0, Register 37 - Error detection */
  112608. +#define PCM512x_IPLK (1 << 0)
  112609. +#define PCM512x_DCAS (1 << 1)
  112610. +#define PCM512x_IDCM (1 << 2)
  112611. +#define PCM512x_IDCH (1 << 3)
  112612. +#define PCM512x_IDSK (1 << 4)
  112613. +#define PCM512x_IDBK (1 << 5)
  112614. +#define PCM512x_IDFS (1 << 6)
  112615. +
  112616. +/* Page 0, Register 42 - DAC routing */
  112617. +#define PCM512x_AUPR_SHIFT 0
  112618. +#define PCM512x_AUPL_SHIFT 4
  112619. +
  112620. +/* Page 0, Register 59 - auto mute */
  112621. +#define PCM512x_ATMR_SHIFT 0
  112622. +#define PCM512x_ATML_SHIFT 4
  112623. +
  112624. +/* Page 0, Register 63 - ramp rates */
  112625. +#define PCM512x_VNDF_SHIFT 6
  112626. +#define PCM512x_VNDS_SHIFT 4
  112627. +#define PCM512x_VNUF_SHIFT 2
  112628. +#define PCM512x_VNUS_SHIFT 0
  112629. +
  112630. +/* Page 0, Register 64 - emergency ramp rates */
  112631. +#define PCM512x_VEDF_SHIFT 6
  112632. +#define PCM512x_VEDS_SHIFT 4
  112633. +
  112634. +/* Page 0, Register 65 - Digital mute enables */
  112635. +#define PCM512x_ACTL_SHIFT 2
  112636. +#define PCM512x_AMLE_SHIFT 1
  112637. +#define PCM512x_AMLR_SHIFT 0
  112638. +
  112639. +#endif
  112640. diff -Nur linux-3.12.33/sound/soc/codecs/tas5713.c linux-3.12.33-rpi/sound/soc/codecs/tas5713.c
  112641. --- linux-3.12.33/sound/soc/codecs/tas5713.c 1969-12-31 18:00:00.000000000 -0600
  112642. +++ linux-3.12.33-rpi/sound/soc/codecs/tas5713.c 2014-12-03 19:13:44.612418001 -0600
  112643. @@ -0,0 +1,370 @@
  112644. +/*
  112645. + * ASoC Driver for TAS5713
  112646. + *
  112647. + * Author: Sebastian Eickhoff <basti.eickhoff@googlemail.com>
  112648. + * Copyright 2014
  112649. + *
  112650. + * This program is free software; you can redistribute it and/or
  112651. + * modify it under the terms of the GNU General Public License
  112652. + * version 2 as published by the Free Software Foundation.
  112653. + *
  112654. + * This program is distributed in the hope that it will be useful, but
  112655. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  112656. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  112657. + * General Public License for more details.
  112658. + */
  112659. +
  112660. +#include <linux/module.h>
  112661. +#include <linux/moduleparam.h>
  112662. +#include <linux/init.h>
  112663. +#include <linux/delay.h>
  112664. +#include <linux/pm.h>
  112665. +#include <linux/i2c.h>
  112666. +#include <linux/of_device.h>
  112667. +#include <linux/spi/spi.h>
  112668. +#include <linux/regmap.h>
  112669. +#include <linux/regulator/consumer.h>
  112670. +#include <linux/slab.h>
  112671. +#include <sound/core.h>
  112672. +#include <sound/pcm.h>
  112673. +#include <sound/pcm_params.h>
  112674. +#include <sound/soc.h>
  112675. +#include <sound/initval.h>
  112676. +#include <sound/tlv.h>
  112677. +
  112678. +#include <linux/kernel.h>
  112679. +#include <linux/string.h>
  112680. +#include <linux/fs.h>
  112681. +#include <asm/uaccess.h>
  112682. +
  112683. +#include "tas5713.h"
  112684. +
  112685. +
  112686. +static struct i2c_client *i2c;
  112687. +
  112688. +struct tas5713_priv {
  112689. + struct regmap *regmap;
  112690. + int mclk_div;
  112691. + struct snd_soc_codec *codec;
  112692. +};
  112693. +
  112694. +static struct tas5713_priv *priv_data;
  112695. +
  112696. +
  112697. +
  112698. +
  112699. +/*
  112700. + * _ _ ___ _ ___ _ _
  112701. + * /_\ | | / __| /_\ / __|___ _ _| |_ _ _ ___| |___
  112702. + * / _ \| |__\__ \/ _ \ | (__/ _ \ ' \ _| '_/ _ \ (_-<
  112703. + * /_/ \_\____|___/_/ \_\ \___\___/_||_\__|_| \___/_/__/
  112704. + *
  112705. + */
  112706. +
  112707. +static const DECLARE_TLV_DB_SCALE(tas5713_vol_tlv, -10000, 50, 1);
  112708. +
  112709. +
  112710. +static const struct snd_kcontrol_new tas5713_snd_controls[] = {
  112711. + SOC_SINGLE_TLV ("Master" , TAS5713_VOL_MASTER, 0, 248, 1, tas5713_vol_tlv),
  112712. + SOC_DOUBLE_R_TLV("Channels" , TAS5713_VOL_CH1, TAS5713_VOL_CH2, 0, 248, 1, tas5713_vol_tlv)
  112713. +};
  112714. +
  112715. +
  112716. +
  112717. +
  112718. +/*
  112719. + * __ __ _ _ ___ _
  112720. + * | \/ |__ _ __| |_ (_)_ _ ___ | \ _ _(_)_ _____ _ _
  112721. + * | |\/| / _` / _| ' \| | ' \/ -_) | |) | '_| \ V / -_) '_|
  112722. + * |_| |_\__,_\__|_||_|_|_||_\___| |___/|_| |_|\_/\___|_|
  112723. + *
  112724. + */
  112725. +
  112726. +static int tas5713_hw_params(struct snd_pcm_substream *substream,
  112727. + struct snd_pcm_hw_params *params,
  112728. + struct snd_soc_dai *dai)
  112729. +{
  112730. + u16 blen = 0x00;
  112731. +
  112732. + struct snd_soc_codec *codec;
  112733. + codec = dai->codec;
  112734. + priv_data->codec = dai->codec;
  112735. +
  112736. + switch (params_format(params)) {
  112737. + case SNDRV_PCM_FORMAT_S16_LE:
  112738. + blen = 0x03;
  112739. + break;
  112740. + case SNDRV_PCM_FORMAT_S20_3LE:
  112741. + blen = 0x1;
  112742. + break;
  112743. + case SNDRV_PCM_FORMAT_S24_LE:
  112744. + blen = 0x04;
  112745. + break;
  112746. + case SNDRV_PCM_FORMAT_S32_LE:
  112747. + blen = 0x05;
  112748. + break;
  112749. + default:
  112750. + dev_err(dai->dev, "Unsupported word length: %u\n",
  112751. + params_format(params));
  112752. + return -EINVAL;
  112753. + }
  112754. +
  112755. + // set word length
  112756. + snd_soc_update_bits(codec, TAS5713_SERIAL_DATA_INTERFACE, 0x7, blen);
  112757. +
  112758. + return 0;
  112759. +}
  112760. +
  112761. +
  112762. +static int tas5713_mute_stream(struct snd_soc_dai *dai, int mute, int stream)
  112763. +{
  112764. + unsigned int val = 0;
  112765. +
  112766. + struct tas5713_priv *tas5713;
  112767. + struct snd_soc_codec *codec = dai->codec;
  112768. + tas5713 = snd_soc_codec_get_drvdata(codec);
  112769. +
  112770. + if (mute) {
  112771. + val = TAS5713_SOFT_MUTE_ALL;
  112772. + }
  112773. +
  112774. + return regmap_write(tas5713->regmap, TAS5713_SOFT_MUTE, val);
  112775. +}
  112776. +
  112777. +
  112778. +static const struct snd_soc_dai_ops tas5713_dai_ops = {
  112779. + .hw_params = tas5713_hw_params,
  112780. + .mute_stream = tas5713_mute_stream,
  112781. +};
  112782. +
  112783. +
  112784. +static struct snd_soc_dai_driver tas5713_dai = {
  112785. + .name = "tas5713-hifi",
  112786. + .playback = {
  112787. + .stream_name = "Playback",
  112788. + .channels_min = 2,
  112789. + .channels_max = 2,
  112790. + .rates = SNDRV_PCM_RATE_8000_48000,
  112791. + .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE ),
  112792. + },
  112793. + .ops = &tas5713_dai_ops,
  112794. +};
  112795. +
  112796. +
  112797. +
  112798. +
  112799. +/*
  112800. + * ___ _ ___ _
  112801. + * / __|___ __| |___ __ | \ _ _(_)_ _____ _ _
  112802. + * | (__/ _ \/ _` / -_) _| | |) | '_| \ V / -_) '_|
  112803. + * \___\___/\__,_\___\__| |___/|_| |_|\_/\___|_|
  112804. + *
  112805. + */
  112806. +
  112807. +static int tas5713_remove(struct snd_soc_codec *codec)
  112808. +{
  112809. + struct tas5713_priv *tas5713;
  112810. +
  112811. + tas5713 = snd_soc_codec_get_drvdata(codec);
  112812. +
  112813. + return 0;
  112814. +}
  112815. +
  112816. +
  112817. +static int tas5713_probe(struct snd_soc_codec *codec)
  112818. +{
  112819. + struct tas5713_priv *tas5713;
  112820. + int i, ret;
  112821. +
  112822. + i2c = container_of(codec->dev, struct i2c_client, dev);
  112823. +
  112824. + tas5713 = snd_soc_codec_get_drvdata(codec);
  112825. +
  112826. + codec->control_data = tas5713->regmap;
  112827. +
  112828. + ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_REGMAP);
  112829. + if (ret < 0) {
  112830. + dev_err(codec->dev, "Failed to set cache i/o: %d\n", ret);
  112831. + return ret;
  112832. + }
  112833. +
  112834. + // Reset error
  112835. + ret = snd_soc_write(codec, TAS5713_ERROR_STATUS, 0x00);
  112836. +
  112837. + // Trim oscillator
  112838. + ret = snd_soc_write(codec, TAS5713_OSC_TRIM, 0x00);
  112839. + msleep(1000);
  112840. +
  112841. + // Reset error
  112842. + ret = snd_soc_write(codec, TAS5713_ERROR_STATUS, 0x00);
  112843. +
  112844. + // Clock mode: 44/48kHz, MCLK=64xfs
  112845. + ret = snd_soc_write(codec, TAS5713_CLOCK_CTRL, 0x60);
  112846. +
  112847. + // I2S 24bit
  112848. + ret = snd_soc_write(codec, TAS5713_SERIAL_DATA_INTERFACE, 0x05);
  112849. +
  112850. + // Unmute
  112851. + ret = snd_soc_write(codec, TAS5713_SYSTEM_CTRL2, 0x00);
  112852. + ret = snd_soc_write(codec, TAS5713_SOFT_MUTE, 0x00);
  112853. +
  112854. + // Set volume to 0db
  112855. + ret = snd_soc_write(codec, TAS5713_VOL_MASTER, 0x00);
  112856. +
  112857. + // Now start programming the default initialization sequence
  112858. + for (i = 0; i < ARRAY_SIZE(tas5713_init_sequence); ++i) {
  112859. + ret = i2c_master_send(i2c,
  112860. + tas5713_init_sequence[i].data,
  112861. + tas5713_init_sequence[i].size);
  112862. +
  112863. + if (ret < 0) {
  112864. + printk(KERN_INFO "TAS5713 CODEC PROBE: InitSeq returns: %d\n", ret);
  112865. + }
  112866. + }
  112867. +
  112868. + // Unmute
  112869. + ret = snd_soc_write(codec, TAS5713_SYSTEM_CTRL2, 0x00);
  112870. +
  112871. +
  112872. + return 0;
  112873. +}
  112874. +
  112875. +
  112876. +static struct snd_soc_codec_driver soc_codec_dev_tas5713 = {
  112877. + .probe = tas5713_probe,
  112878. + .remove = tas5713_remove,
  112879. + .controls = tas5713_snd_controls,
  112880. + .num_controls = ARRAY_SIZE(tas5713_snd_controls),
  112881. +};
  112882. +
  112883. +
  112884. +
  112885. +
  112886. +/*
  112887. + * ___ ___ ___ ___ _
  112888. + * |_ _|_ ) __| | \ _ _(_)_ _____ _ _
  112889. + * | | / / (__ | |) | '_| \ V / -_) '_|
  112890. + * |___/___\___| |___/|_| |_|\_/\___|_|
  112891. + *
  112892. + */
  112893. +
  112894. +static const struct reg_default tas5713_reg_defaults[] = {
  112895. + { 0x07 ,0x80 }, // R7 - VOL_MASTER - -40dB
  112896. + { 0x08 , 30 }, // R8 - VOL_CH1 - 0dB
  112897. + { 0x09 , 30 }, // R9 - VOL_CH2 - 0dB
  112898. + { 0x0A ,0x80 }, // R10 - VOL_HEADPHONE - -40dB
  112899. +};
  112900. +
  112901. +
  112902. +static bool tas5713_reg_volatile(struct device *dev, unsigned int reg)
  112903. +{
  112904. + switch (reg) {
  112905. + case TAS5713_DEVICE_ID:
  112906. + case TAS5713_ERROR_STATUS:
  112907. + return true;
  112908. + default:
  112909. + return false;
  112910. + }
  112911. +}
  112912. +
  112913. +
  112914. +static const struct of_device_id tas5713_of_match[] = {
  112915. + { .compatible = "ti,tas5713", },
  112916. + { }
  112917. +};
  112918. +MODULE_DEVICE_TABLE(of, tas5713_of_match);
  112919. +
  112920. +
  112921. +static struct regmap_config tas5713_regmap_config = {
  112922. + .reg_bits = 8,
  112923. + .val_bits = 8,
  112924. +
  112925. + .max_register = TAS5713_MAX_REGISTER,
  112926. + .volatile_reg = tas5713_reg_volatile,
  112927. +
  112928. + .cache_type = REGCACHE_RBTREE,
  112929. + .reg_defaults = tas5713_reg_defaults,
  112930. + .num_reg_defaults = ARRAY_SIZE(tas5713_reg_defaults),
  112931. +};
  112932. +
  112933. +
  112934. +static int tas5713_i2c_probe(struct i2c_client *i2c,
  112935. + const struct i2c_device_id *id)
  112936. +{
  112937. + int ret;
  112938. +
  112939. + priv_data = devm_kzalloc(&i2c->dev, sizeof *priv_data, GFP_KERNEL);
  112940. + if (!priv_data)
  112941. + return -ENOMEM;
  112942. +
  112943. + priv_data->regmap = devm_regmap_init_i2c(i2c, &tas5713_regmap_config);
  112944. + if (IS_ERR(priv_data->regmap)) {
  112945. + ret = PTR_ERR(priv_data->regmap);
  112946. + return ret;
  112947. + }
  112948. +
  112949. + i2c_set_clientdata(i2c, priv_data);
  112950. +
  112951. + ret = snd_soc_register_codec(&i2c->dev,
  112952. + &soc_codec_dev_tas5713, &tas5713_dai, 1);
  112953. +
  112954. + return ret;
  112955. +}
  112956. +
  112957. +
  112958. +static int tas5713_i2c_remove(struct i2c_client *i2c)
  112959. +{
  112960. + snd_soc_unregister_codec(&i2c->dev);
  112961. + i2c_set_clientdata(i2c, NULL);
  112962. +
  112963. + kfree(priv_data);
  112964. +
  112965. + return 0;
  112966. +}
  112967. +
  112968. +
  112969. +static const struct i2c_device_id tas5713_i2c_id[] = {
  112970. + { "tas5713", 0 },
  112971. + { }
  112972. +};
  112973. +
  112974. +MODULE_DEVICE_TABLE(i2c, tas5713_i2c_id);
  112975. +
  112976. +
  112977. +static struct i2c_driver tas5713_i2c_driver = {
  112978. + .driver = {
  112979. + .name = "tas5713",
  112980. + .owner = THIS_MODULE,
  112981. + .of_match_table = tas5713_of_match,
  112982. + },
  112983. + .probe = tas5713_i2c_probe,
  112984. + .remove = tas5713_i2c_remove,
  112985. + .id_table = tas5713_i2c_id
  112986. +};
  112987. +
  112988. +
  112989. +static int __init tas5713_modinit(void)
  112990. +{
  112991. + int ret = 0;
  112992. +
  112993. + ret = i2c_add_driver(&tas5713_i2c_driver);
  112994. + if (ret) {
  112995. + printk(KERN_ERR "Failed to register tas5713 I2C driver: %d\n",
  112996. + ret);
  112997. + }
  112998. +
  112999. + return ret;
  113000. +}
  113001. +module_init(tas5713_modinit);
  113002. +
  113003. +
  113004. +static void __exit tas5713_exit(void)
  113005. +{
  113006. + i2c_del_driver(&tas5713_i2c_driver);
  113007. +}
  113008. +module_exit(tas5713_exit);
  113009. +
  113010. +
  113011. +MODULE_AUTHOR("Sebastian Eickhoff <basti.eickhoff@googlemail.com>");
  113012. +MODULE_DESCRIPTION("ASoC driver for TAS5713");
  113013. +MODULE_LICENSE("GPL v2");
  113014. diff -Nur linux-3.12.33/sound/soc/codecs/tas5713.h linux-3.12.33-rpi/sound/soc/codecs/tas5713.h
  113015. --- linux-3.12.33/sound/soc/codecs/tas5713.h 1969-12-31 18:00:00.000000000 -0600
  113016. +++ linux-3.12.33-rpi/sound/soc/codecs/tas5713.h 2014-12-03 19:13:44.612418001 -0600
  113017. @@ -0,0 +1,210 @@
  113018. +/*
  113019. + * ASoC Driver for TAS5713
  113020. + *
  113021. + * Author: Sebastian Eickhoff <basti.eickhoff@googlemail.com>
  113022. + * Copyright 2014
  113023. + *
  113024. + * This program is free software; you can redistribute it and/or
  113025. + * modify it under the terms of the GNU General Public License
  113026. + * version 2 as published by the Free Software Foundation.
  113027. + *
  113028. + * This program is distributed in the hope that it will be useful, but
  113029. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  113030. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  113031. + * General Public License for more details.
  113032. + */
  113033. +
  113034. +#ifndef _TAS5713_H
  113035. +#define _TAS5713_H
  113036. +
  113037. +
  113038. +// TAS5713 I2C-bus register addresses
  113039. +
  113040. +#define TAS5713_CLOCK_CTRL 0x00
  113041. +#define TAS5713_DEVICE_ID 0x01
  113042. +#define TAS5713_ERROR_STATUS 0x02
  113043. +#define TAS5713_SYSTEM_CTRL1 0x03
  113044. +#define TAS5713_SERIAL_DATA_INTERFACE 0x04
  113045. +#define TAS5713_SYSTEM_CTRL2 0x05
  113046. +#define TAS5713_SOFT_MUTE 0x06
  113047. +#define TAS5713_VOL_MASTER 0x07
  113048. +#define TAS5713_VOL_CH1 0x08
  113049. +#define TAS5713_VOL_CH2 0x09
  113050. +#define TAS5713_VOL_HEADPHONE 0x0A
  113051. +#define TAS5713_VOL_CONFIG 0x0E
  113052. +#define TAS5713_MODULATION_LIMIT 0x10
  113053. +#define TAS5713_IC_DLY_CH1 0x11
  113054. +#define TAS5713_IC_DLY_CH2 0x12
  113055. +#define TAS5713_IC_DLY_CH3 0x13
  113056. +#define TAS5713_IC_DLY_CH4 0x14
  113057. +
  113058. +#define TAS5713_START_STOP_PERIOD 0x1A
  113059. +#define TAS5713_OSC_TRIM 0x1B
  113060. +#define TAS5713_BKND_ERR 0x1C
  113061. +
  113062. +#define TAS5713_INPUT_MUX 0x20
  113063. +#define TAS5713_SRC_SELECT_CH4 0x21
  113064. +#define TAS5713_PWM_MUX 0x25
  113065. +
  113066. +#define TAS5713_CH1_BQ0 0x29
  113067. +#define TAS5713_CH1_BQ1 0x2A
  113068. +#define TAS5713_CH1_BQ2 0x2B
  113069. +#define TAS5713_CH1_BQ3 0x2C
  113070. +#define TAS5713_CH1_BQ4 0x2D
  113071. +#define TAS5713_CH1_BQ5 0x2E
  113072. +#define TAS5713_CH1_BQ6 0x2F
  113073. +#define TAS5713_CH1_BQ7 0x58
  113074. +#define TAS5713_CH1_BQ8 0x59
  113075. +
  113076. +#define TAS5713_CH2_BQ0 0x30
  113077. +#define TAS5713_CH2_BQ1 0x31
  113078. +#define TAS5713_CH2_BQ2 0x32
  113079. +#define TAS5713_CH2_BQ3 0x33
  113080. +#define TAS5713_CH2_BQ4 0x34
  113081. +#define TAS5713_CH2_BQ5 0x35
  113082. +#define TAS5713_CH2_BQ6 0x36
  113083. +#define TAS5713_CH2_BQ7 0x5C
  113084. +#define TAS5713_CH2_BQ8 0x5D
  113085. +
  113086. +#define TAS5713_CH4_BQ0 0x5A
  113087. +#define TAS5713_CH4_BQ1 0x5B
  113088. +#define TAS5713_CH3_BQ0 0x5E
  113089. +#define TAS5713_CH3_BQ1 0x5F
  113090. +
  113091. +#define TAS5713_DRC1_SOFTENING_FILTER_ALPHA_OMEGA 0x3B
  113092. +#define TAS5713_DRC1_ATTACK_RELEASE_RATE 0x3C
  113093. +#define TAS5713_DRC2_SOFTENING_FILTER_ALPHA_OMEGA 0x3E
  113094. +#define TAS5713_DRC2_ATTACK_RELEASE_RATE 0x3F
  113095. +#define TAS5713_DRC1_ATTACK_RELEASE_THRES 0x40
  113096. +#define TAS5713_DRC2_ATTACK_RELEASE_THRES 0x43
  113097. +#define TAS5713_DRC_CTRL 0x46
  113098. +
  113099. +#define TAS5713_BANK_SW_CTRL 0x50
  113100. +#define TAS5713_CH1_OUTPUT_MIXER 0x51
  113101. +#define TAS5713_CH2_OUTPUT_MIXER 0x52
  113102. +#define TAS5713_CH1_INPUT_MIXER 0x53
  113103. +#define TAS5713_CH2_INPUT_MIXER 0x54
  113104. +#define TAS5713_OUTPUT_POST_SCALE 0x56
  113105. +#define TAS5713_OUTPUT_PRESCALE 0x57
  113106. +
  113107. +#define TAS5713_IDF_POST_SCALE 0x62
  113108. +
  113109. +#define TAS5713_CH1_INLINE_MIXER 0x70
  113110. +#define TAS5713_CH1_INLINE_DRC_EN_MIXER 0x71
  113111. +#define TAS5713_CH1_R_CHANNEL_MIXER 0x72
  113112. +#define TAS5713_CH1_L_CHANNEL_MIXER 0x73
  113113. +#define TAS5713_CH2_INLINE_MIXER 0x74
  113114. +#define TAS5713_CH2_INLINE_DRC_EN_MIXER 0x75
  113115. +#define TAS5713_CH2_L_CHANNEL_MIXER 0x76
  113116. +#define TAS5713_CH2_R_CHANNEL_MIXER 0x77
  113117. +
  113118. +#define TAS5713_UPDATE_DEV_ADDR_KEY 0xF8
  113119. +#define TAS5713_UPDATE_DEV_ADDR_REG 0xF9
  113120. +
  113121. +#define TAS5713_REGISTER_COUNT 0x46
  113122. +#define TAS5713_MAX_REGISTER 0xF9
  113123. +
  113124. +
  113125. +// Bitmasks for registers
  113126. +#define TAS5713_SOFT_MUTE_ALL 0x07
  113127. +
  113128. +
  113129. +
  113130. +struct tas5713_init_command {
  113131. + const int size;
  113132. + const char *const data;
  113133. +};
  113134. +
  113135. +static const struct tas5713_init_command tas5713_init_sequence[] = {
  113136. +
  113137. + // Trim oscillator
  113138. + { .size = 2, .data = "\x1B\x00" },
  113139. + // System control register 1 (0x03): block DC
  113140. + { .size = 2, .data = "\x03\x80" },
  113141. + // Mute everything
  113142. + { .size = 2, .data = "\x05\x40" },
  113143. + // Modulation limit register (0x10): 97.7%
  113144. + { .size = 2, .data = "\x10\x02" },
  113145. + // Interchannel delay registers
  113146. + // (0x11, 0x12, 0x13, and 0x14): BD mode
  113147. + { .size = 2, .data = "\x11\xB8" },
  113148. + { .size = 2, .data = "\x12\x60" },
  113149. + { .size = 2, .data = "\x13\xA0" },
  113150. + { .size = 2, .data = "\x14\x48" },
  113151. + // PWM shutdown group register (0x19): no shutdown
  113152. + { .size = 2, .data = "\x19\x00" },
  113153. + // Input multiplexer register (0x20): BD mode
  113154. + { .size = 2, .data = "\x20\x00\x89\x77\x72" },
  113155. + // PWM output mux register (0x25)
  113156. + // Channel 1 --> OUTA, channel 1 neg --> OUTB
  113157. + // Channel 2 --> OUTC, channel 2 neg --> OUTD
  113158. + { .size = 5, .data = "\x25\x01\x02\x13\x45" },
  113159. + // DRC control (0x46): DRC off
  113160. + { .size = 5, .data = "\x46\x00\x00\x00\x00" },
  113161. + // BKND_ERR register (0x1C): 299ms reset period
  113162. + { .size = 2, .data = "\x1C\x07" },
  113163. + // Mute channel 3
  113164. + { .size = 2, .data = "\x0A\xFF" },
  113165. + // Volume configuration register (0x0E): volume slew 512 steps
  113166. + { .size = 2, .data = "\x0E\x90" },
  113167. + // Clock control register (0x00): 44/48kHz, MCLK=64xfs
  113168. + { .size = 2, .data = "\x00\x60" },
  113169. + // Bank switch and eq control (0x50): no bank switching
  113170. + { .size = 5, .data = "\x50\x00\x00\x00\x00" },
  113171. + // Volume registers (0x07, 0x08, 0x09, 0x0A)
  113172. + { .size = 2, .data = "\x07\x20" },
  113173. + { .size = 2, .data = "\x08\x30" },
  113174. + { .size = 2, .data = "\x09\x30" },
  113175. + { .size = 2, .data = "\x0A\xFF" },
  113176. + // 0x72, 0x73, 0x76, 0x77 input mixer:
  113177. + // no intermix between channels
  113178. + { .size = 5, .data = "\x72\x00\x00\x00\x00" },
  113179. + { .size = 5, .data = "\x73\x00\x80\x00\x00" },
  113180. + { .size = 5, .data = "\x76\x00\x00\x00\x00" },
  113181. + { .size = 5, .data = "\x77\x00\x80\x00\x00" },
  113182. + // 0x70, 0x71, 0x74, 0x75 inline DRC mixer:
  113183. + // no inline DRC inmix
  113184. + { .size = 5, .data = "\x70\x00\x80\x00\x00" },
  113185. + { .size = 5, .data = "\x71\x00\x00\x00\x00" },
  113186. + { .size = 5, .data = "\x74\x00\x80\x00\x00" },
  113187. + { .size = 5, .data = "\x75\x00\x00\x00\x00" },
  113188. + // 0x56, 0x57 Output scale
  113189. + { .size = 5, .data = "\x56\x00\x80\x00\x00" },
  113190. + { .size = 5, .data = "\x57\x00\x02\x00\x00" },
  113191. + // 0x3B, 0x3c
  113192. + { .size = 9, .data = "\x3B\x00\x08\x00\x00\x00\x78\x00\x00" },
  113193. + { .size = 9, .data = "\x3C\x00\x00\x01\x00\xFF\xFF\xFF\x00" },
  113194. + { .size = 9, .data = "\x3E\x00\x08\x00\x00\x00\x78\x00\x00" },
  113195. + { .size = 9, .data = "\x3F\x00\x00\x01\x00\xFF\xFF\xFF\x00" },
  113196. + { .size = 9, .data = "\x40\x00\x00\x01\x00\xFF\xFF\xFF\x00" },
  113197. + { .size = 9, .data = "\x43\x00\x00\x01\x00\xFF\xFF\xFF\x00" },
  113198. + // 0x51, 0x52: output mixer
  113199. + { .size = 9, .data = "\x51\x00\x80\x00\x00\x00\x00\x00\x00" },
  113200. + { .size = 9, .data = "\x52\x00\x80\x00\x00\x00\x00\x00\x00" },
  113201. + // PEQ defaults
  113202. + { .size = 21, .data = "\x29\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113203. + { .size = 21, .data = "\x2A\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113204. + { .size = 21, .data = "\x2B\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113205. + { .size = 21, .data = "\x2C\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113206. + { .size = 21, .data = "\x2D\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113207. + { .size = 21, .data = "\x2E\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113208. + { .size = 21, .data = "\x2F\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113209. + { .size = 21, .data = "\x30\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113210. + { .size = 21, .data = "\x31\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113211. + { .size = 21, .data = "\x32\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113212. + { .size = 21, .data = "\x33\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113213. + { .size = 21, .data = "\x34\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113214. + { .size = 21, .data = "\x35\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113215. + { .size = 21, .data = "\x36\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113216. + { .size = 21, .data = "\x58\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113217. + { .size = 21, .data = "\x59\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113218. + { .size = 21, .data = "\x5C\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113219. + { .size = 21, .data = "\x5D\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113220. + { .size = 21, .data = "\x5E\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113221. + { .size = 21, .data = "\x5F\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113222. + { .size = 21, .data = "\x5A\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113223. + { .size = 21, .data = "\x5B\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" },
  113224. +};
  113225. +
  113226. +
  113227. +#endif /* _TAS5713_H */
  113228. diff -Nur linux-3.12.33/sound/soc/codecs/wm8804.c linux-3.12.33-rpi/sound/soc/codecs/wm8804.c
  113229. --- linux-3.12.33/sound/soc/codecs/wm8804.c 2014-11-15 06:28:07.000000000 -0600
  113230. +++ linux-3.12.33-rpi/sound/soc/codecs/wm8804.c 2014-12-03 19:13:44.792418001 -0600
  113231. @@ -63,6 +63,7 @@
  113232. struct regmap *regmap;
  113233. struct regulator_bulk_data supplies[WM8804_NUM_SUPPLIES];
  113234. struct notifier_block disable_nb[WM8804_NUM_SUPPLIES];
  113235. + int mclk_div;
  113236. };
  113237. static int txsrc_get(struct snd_kcontrol *kcontrol,
  113238. @@ -277,6 +278,7 @@
  113239. blen = 0x1;
  113240. break;
  113241. case SNDRV_PCM_FORMAT_S24_LE:
  113242. + case SNDRV_PCM_FORMAT_S32_LE:
  113243. blen = 0x2;
  113244. break;
  113245. default:
  113246. @@ -318,7 +320,7 @@
  113247. #define FIXED_PLL_SIZE ((1ULL << 22) * 10)
  113248. static int pll_factors(struct pll_div *pll_div, unsigned int target,
  113249. - unsigned int source)
  113250. + unsigned int source, unsigned int mclk_div)
  113251. {
  113252. u64 Kpart;
  113253. unsigned long int K, Ndiv, Nmod, tmp;
  113254. @@ -330,7 +332,8 @@
  113255. */
  113256. for (i = 0; i < ARRAY_SIZE(post_table); i++) {
  113257. tmp = target * post_table[i].div;
  113258. - if (tmp >= 90000000 && tmp <= 100000000) {
  113259. + if ((tmp >= 90000000 && tmp <= 100000000) &&
  113260. + (mclk_div == post_table[i].mclkdiv)) {
  113261. pll_div->freqmode = post_table[i].freqmode;
  113262. pll_div->mclkdiv = post_table[i].mclkdiv;
  113263. target *= post_table[i].div;
  113264. @@ -387,8 +390,11 @@
  113265. } else {
  113266. int ret;
  113267. struct pll_div pll_div;
  113268. + struct wm8804_priv *wm8804;
  113269. - ret = pll_factors(&pll_div, freq_out, freq_in);
  113270. + wm8804 = snd_soc_codec_get_drvdata(codec);
  113271. +
  113272. + ret = pll_factors(&pll_div, freq_out, freq_in, wm8804->mclk_div);
  113273. if (ret)
  113274. return ret;
  113275. @@ -452,6 +458,7 @@
  113276. int div_id, int div)
  113277. {
  113278. struct snd_soc_codec *codec;
  113279. + struct wm8804_priv *wm8804;
  113280. codec = dai->codec;
  113281. switch (div_id) {
  113282. @@ -459,6 +466,10 @@
  113283. snd_soc_update_bits(codec, WM8804_PLL5, 0x30,
  113284. (div & 0x3) << 4);
  113285. break;
  113286. + case WM8804_MCLK_DIV:
  113287. + wm8804 = snd_soc_codec_get_drvdata(codec);
  113288. + wm8804->mclk_div = div;
  113289. + break;
  113290. default:
  113291. dev_err(dai->dev, "Unknown clock divider: %d\n", div_id);
  113292. return -EINVAL;
  113293. @@ -641,7 +652,7 @@
  113294. };
  113295. #define WM8804_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  113296. - SNDRV_PCM_FMTBIT_S24_LE)
  113297. + SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  113298. #define WM8804_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  113299. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 | \
  113300. @@ -674,7 +685,7 @@
  113301. .suspend = wm8804_suspend,
  113302. .resume = wm8804_resume,
  113303. .set_bias_level = wm8804_set_bias_level,
  113304. - .idle_bias_off = true,
  113305. + .idle_bias_off = false,
  113306. .controls = wm8804_snd_controls,
  113307. .num_controls = ARRAY_SIZE(wm8804_snd_controls),
  113308. diff -Nur linux-3.12.33/sound/soc/codecs/wm8804.h linux-3.12.33-rpi/sound/soc/codecs/wm8804.h
  113309. --- linux-3.12.33/sound/soc/codecs/wm8804.h 2014-11-15 06:28:07.000000000 -0600
  113310. +++ linux-3.12.33-rpi/sound/soc/codecs/wm8804.h 2014-12-03 19:13:44.792418001 -0600
  113311. @@ -57,5 +57,9 @@
  113312. #define WM8804_CLKOUT_SRC_OSCCLK 4
  113313. #define WM8804_CLKOUT_DIV 1
  113314. +#define WM8804_MCLK_DIV 2
  113315. +
  113316. +#define WM8804_MCLKDIV_256FS 0
  113317. +#define WM8804_MCLKDIV_128FS 1
  113318. #endif /* _WM8804_H */
  113319. diff -Nur linux-3.12.33/sound/soc/generic/simple-card.c linux-3.12.33-rpi/sound/soc/generic/simple-card.c
  113320. --- linux-3.12.33/sound/soc/generic/simple-card.c 2014-11-15 06:28:07.000000000 -0600
  113321. +++ linux-3.12.33-rpi/sound/soc/generic/simple-card.c 2014-12-03 19:13:44.812418001 -0600
  113322. @@ -27,6 +27,11 @@
  113323. if (!ret && daifmt)
  113324. ret = snd_soc_dai_set_fmt(dai, daifmt);
  113325. + if (ret == -ENOTSUPP) {
  113326. + dev_dbg(dai->dev, "ASoC: set_fmt is not supported\n");
  113327. + ret = 0;
  113328. + }
  113329. +
  113330. if (!ret && set->sysclk)
  113331. ret = snd_soc_dai_set_sysclk(dai, 0, set->sysclk, 0);
  113332. diff -Nur linux-3.12.33/sound/soc/Kconfig linux-3.12.33-rpi/sound/soc/Kconfig
  113333. --- linux-3.12.33/sound/soc/Kconfig 2014-11-15 06:28:07.000000000 -0600
  113334. +++ linux-3.12.33-rpi/sound/soc/Kconfig 2014-12-03 19:13:44.592418001 -0600
  113335. @@ -33,6 +33,7 @@
  113336. # All the supported SoCs
  113337. source "sound/soc/atmel/Kconfig"
  113338. source "sound/soc/au1x/Kconfig"
  113339. +source "sound/soc/bcm/Kconfig"
  113340. source "sound/soc/blackfin/Kconfig"
  113341. source "sound/soc/cirrus/Kconfig"
  113342. source "sound/soc/davinci/Kconfig"
  113343. diff -Nur linux-3.12.33/sound/soc/Makefile linux-3.12.33-rpi/sound/soc/Makefile
  113344. --- linux-3.12.33/sound/soc/Makefile 2014-11-15 06:28:07.000000000 -0600
  113345. +++ linux-3.12.33-rpi/sound/soc/Makefile 2014-12-03 19:13:44.592418001 -0600
  113346. @@ -10,6 +10,7 @@
  113347. obj-$(CONFIG_SND_SOC) += generic/
  113348. obj-$(CONFIG_SND_SOC) += atmel/
  113349. obj-$(CONFIG_SND_SOC) += au1x/
  113350. +obj-$(CONFIG_SND_SOC) += bcm/
  113351. obj-$(CONFIG_SND_SOC) += blackfin/
  113352. obj-$(CONFIG_SND_SOC) += cirrus/
  113353. obj-$(CONFIG_SND_SOC) += davinci/
  113354. diff -Nur linux-3.12.33/sound/soc/soc-core.c linux-3.12.33-rpi/sound/soc/soc-core.c
  113355. --- linux-3.12.33/sound/soc/soc-core.c 2014-11-15 06:28:07.000000000 -0600
  113356. +++ linux-3.12.33-rpi/sound/soc/soc-core.c 2014-12-03 19:13:44.828418001 -0600
  113357. @@ -3038,8 +3038,8 @@
  113358. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  113359. uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
  113360. - uinfo->value.integer.min = 0;
  113361. - uinfo->value.integer.max = platform_max - min;
  113362. + uinfo->value.integer.min = min;
  113363. + uinfo->value.integer.max = platform_max;
  113364. return 0;
  113365. }
  113366. @@ -3070,9 +3070,10 @@
  113367. unsigned int val, val_mask;
  113368. int ret;
  113369. - val = ((ucontrol->value.integer.value[0] + min) & mask);
  113370. if (invert)
  113371. - val = max - val;
  113372. + val = ((max - ucontrol->value.integer.value[0] + min) & mask);
  113373. + else
  113374. + val = (ucontrol->value.integer.value[0] & mask);
  113375. val_mask = mask << shift;
  113376. val = val << shift;
  113377. @@ -3081,9 +3082,10 @@
  113378. return ret;
  113379. if (snd_soc_volsw_is_stereo(mc)) {
  113380. - val = ((ucontrol->value.integer.value[1] + min) & mask);
  113381. if (invert)
  113382. - val = max - val;
  113383. + val = ((max - ucontrol->value.integer.value[1] + min) & mask);
  113384. + else
  113385. + val = (ucontrol->value.integer.value[1] & mask);
  113386. val_mask = mask << shift;
  113387. val = val << shift;
  113388. @@ -3121,18 +3123,14 @@
  113389. (snd_soc_read(codec, reg) >> shift) & mask;
  113390. if (invert)
  113391. ucontrol->value.integer.value[0] =
  113392. - max - ucontrol->value.integer.value[0];
  113393. - ucontrol->value.integer.value[0] =
  113394. - ucontrol->value.integer.value[0] - min;
  113395. + max - ucontrol->value.integer.value[0] + min;
  113396. if (snd_soc_volsw_is_stereo(mc)) {
  113397. ucontrol->value.integer.value[1] =
  113398. (snd_soc_read(codec, rreg) >> shift) & mask;
  113399. if (invert)
  113400. ucontrol->value.integer.value[1] =
  113401. - max - ucontrol->value.integer.value[1];
  113402. - ucontrol->value.integer.value[1] =
  113403. - ucontrol->value.integer.value[1] - min;
  113404. + max - ucontrol->value.integer.value[1] + min;
  113405. }
  113406. return 0;
  113407. @@ -3576,6 +3574,22 @@
  113408. EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
  113409. /**
  113410. + * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
  113411. + * @dai: DAI
  113412. + * @ratio Ratio of BCLK to Sample rate.
  113413. + *
  113414. + * Configures the DAI for a preset BCLK to sample rate ratio.
  113415. + */
  113416. +int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
  113417. +{
  113418. + if (dai->driver && dai->driver->ops->set_bclk_ratio)
  113419. + return dai->driver->ops->set_bclk_ratio(dai, ratio);
  113420. + else
  113421. + return -EINVAL;
  113422. +}
  113423. +EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
  113424. +
  113425. +/**
  113426. * snd_soc_dai_set_fmt - configure DAI hardware audio format.
  113427. * @dai: DAI
  113428. * @fmt: SND_SOC_DAIFMT_ format value.