q6asm.c 254 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396
  1. /*
  2. * Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
  3. * Author: Brian Swetland <[email protected]>
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/fs.h>
  16. #include <linux/mutex.h>
  17. #include <linux/wait.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/sched.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/miscdevice.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/time.h>
  27. #include <linux/atomic.h>
  28. #include <linux/mm.h>
  29. #include <asm/ioctls.h>
  30. #include <linux/memory.h>
  31. #include <sound/compress_params.h>
  32. #include <dsp/msm_audio_ion.h>
  33. #include <dsp/apr_audio-v2.h>
  34. #include <dsp/audio_cal_utils.h>
  35. #include <dsp/q6asm-v2.h>
  36. #include <dsp/q6audio-v2.h>
  37. #include "adsp_err.h"
  38. #define TRUE 0x01
  39. #define FALSE 0x00
  40. #define SESSION_MAX 8
  41. enum {
  42. ASM_TOPOLOGY_CAL = 0,
  43. ASM_CUSTOM_TOP_CAL,
  44. ASM_AUDSTRM_CAL,
  45. ASM_RTAC_APR_CAL,
  46. ASM_MAX_CAL_TYPES
  47. };
  48. union asm_token_struct {
  49. struct {
  50. u8 stream_id;
  51. u8 session_id;
  52. u8 buf_index;
  53. u8 flags;
  54. } _token;
  55. u32 token;
  56. } __packed;
  57. enum {
  58. ASM_DIRECTION_OFFSET,
  59. ASM_CMD_NO_WAIT_OFFSET,
  60. /*
  61. * Offset is limited to 7 because flags is stored in u8
  62. * field in asm_token_structure defined above. The offset
  63. * starts from 0.
  64. */
  65. ASM_MAX_OFFSET = 7,
  66. };
  67. enum {
  68. WAIT_CMD,
  69. NO_WAIT_CMD
  70. };
  71. #define ASM_SET_BIT(n, x) (n |= 1 << x)
  72. #define ASM_TEST_BIT(n, x) ((n >> x) & 1)
  73. /* TODO, combine them together */
  74. static DEFINE_MUTEX(session_lock);
  75. struct asm_mmap {
  76. atomic_t ref_cnt;
  77. void *apr;
  78. };
  79. static struct asm_mmap this_mmap;
  80. /* session id: 0 reserved */
  81. static struct audio_client *session[ASM_ACTIVE_STREAMS_ALLOWED + 1];
  82. struct asm_buffer_node {
  83. struct list_head list;
  84. phys_addr_t buf_phys_addr;
  85. uint32_t mmap_hdl;
  86. };
  87. static int32_t q6asm_srvc_callback(struct apr_client_data *data, void *priv);
  88. static int32_t q6asm_callback(struct apr_client_data *data, void *priv);
  89. static void q6asm_add_hdr(struct audio_client *ac, struct apr_hdr *hdr,
  90. uint32_t pkt_size, uint32_t cmd_flg);
  91. static void q6asm_add_hdr_custom_topology(struct audio_client *ac,
  92. struct apr_hdr *hdr,
  93. uint32_t pkt_size);
  94. static void q6asm_add_hdr_async(struct audio_client *ac, struct apr_hdr *hdr,
  95. uint32_t pkt_size, uint32_t cmd_flg);
  96. static int q6asm_memory_map_regions(struct audio_client *ac, int dir,
  97. uint32_t bufsz, uint32_t bufcnt,
  98. bool is_contiguous);
  99. static int q6asm_memory_unmap_regions(struct audio_client *ac, int dir);
  100. static void q6asm_reset_buf_state(struct audio_client *ac);
  101. static int q6asm_map_channels(u8 *channel_mapping, uint32_t channels,
  102. bool use_back_flavor);
  103. void *q6asm_mmap_apr_reg(void);
  104. static int q6asm_is_valid_session(struct apr_client_data *data, void *priv);
  105. static int q6asm_get_asm_topology_cal(void);
  106. static int q6asm_get_asm_app_type_cal(void);
  107. /* for ASM custom topology */
  108. static struct cal_type_data *cal_data[ASM_MAX_CAL_TYPES];
  109. static struct audio_buffer common_buf[2];
  110. static struct audio_client common_client;
  111. static int set_custom_topology;
  112. static int topology_map_handle;
  113. struct generic_get_data_ {
  114. int valid;
  115. int is_inband;
  116. int size_in_ints;
  117. int ints[];
  118. };
  119. static struct generic_get_data_ *generic_get_data;
  120. #ifdef CONFIG_DEBUG_FS
  121. #define OUT_BUFFER_SIZE 56
  122. #define IN_BUFFER_SIZE 24
  123. static struct timeval out_cold_tv;
  124. static struct timeval out_warm_tv;
  125. static struct timeval out_cont_tv;
  126. static struct timeval in_cont_tv;
  127. static long out_enable_flag;
  128. static long in_enable_flag;
  129. static struct dentry *out_dentry;
  130. static struct dentry *in_dentry;
  131. static int in_cont_index;
  132. /*This var is used to keep track of first write done for cold output latency */
  133. static int out_cold_index;
  134. static char *out_buffer;
  135. static char *in_buffer;
  136. static uint32_t adsp_reg_event_opcode[] = {
  137. ASM_STREAM_CMD_REGISTER_PP_EVENTS,
  138. ASM_STREAM_CMD_REGISTER_ENCDEC_EVENTS,
  139. ASM_STREAM_CMD_REGISTER_IEC_61937_FMT_UPDATE };
  140. static uint32_t adsp_raise_event_opcode[] = {
  141. ASM_STREAM_PP_EVENT,
  142. ASM_STREAM_CMD_ENCDEC_EVENTS,
  143. ASM_IEC_61937_MEDIA_FMT_EVENT };
  144. static int is_adsp_reg_event(uint32_t cmd)
  145. {
  146. int i;
  147. for (i = 0; i < ARRAY_SIZE(adsp_reg_event_opcode); i++) {
  148. if (cmd == adsp_reg_event_opcode[i])
  149. return i;
  150. }
  151. return -EINVAL;
  152. }
  153. static int is_adsp_raise_event(uint32_t cmd)
  154. {
  155. int i;
  156. for (i = 0; i < ARRAY_SIZE(adsp_raise_event_opcode); i++) {
  157. if (cmd == adsp_raise_event_opcode[i])
  158. return i;
  159. }
  160. return -EINVAL;
  161. }
  162. static inline void q6asm_set_flag_in_token(union asm_token_struct *asm_token,
  163. int flag, int flag_offset)
  164. {
  165. if (flag)
  166. ASM_SET_BIT(asm_token->_token.flags, flag_offset);
  167. }
  168. static inline int q6asm_get_flag_from_token(union asm_token_struct *asm_token,
  169. int flag_offset)
  170. {
  171. return ASM_TEST_BIT(asm_token->_token.flags, flag_offset);
  172. }
  173. static inline void q6asm_update_token(u32 *token, u8 session_id, u8 stream_id,
  174. u8 buf_index, u8 dir, u8 nowait_flag)
  175. {
  176. union asm_token_struct asm_token;
  177. asm_token.token = 0;
  178. asm_token._token.session_id = session_id;
  179. asm_token._token.stream_id = stream_id;
  180. asm_token._token.buf_index = buf_index;
  181. q6asm_set_flag_in_token(&asm_token, dir, ASM_DIRECTION_OFFSET);
  182. q6asm_set_flag_in_token(&asm_token, nowait_flag,
  183. ASM_CMD_NO_WAIT_OFFSET);
  184. *token = asm_token.token;
  185. }
  186. static inline uint32_t q6asm_get_pcm_format_id(uint32_t media_format_block_ver)
  187. {
  188. uint32_t pcm_format_id;
  189. switch (media_format_block_ver) {
  190. case PCM_MEDIA_FORMAT_V4:
  191. pcm_format_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V4;
  192. break;
  193. case PCM_MEDIA_FORMAT_V3:
  194. pcm_format_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V3;
  195. break;
  196. case PCM_MEDIA_FORMAT_V2:
  197. default:
  198. pcm_format_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V2;
  199. break;
  200. }
  201. return pcm_format_id;
  202. }
  203. /*
  204. * q6asm_get_buf_index_from_token:
  205. * Retrieve buffer index from token.
  206. *
  207. * @token: token value sent to ASM service on q6.
  208. * Returns buffer index in the read/write commands.
  209. */
  210. uint8_t q6asm_get_buf_index_from_token(uint32_t token)
  211. {
  212. union asm_token_struct asm_token;
  213. asm_token.token = token;
  214. return asm_token._token.buf_index;
  215. }
  216. EXPORT_SYMBOL(q6asm_get_buf_index_from_token);
  217. /*
  218. * q6asm_get_stream_id_from_token:
  219. * Retrieve stream id from token.
  220. *
  221. * @token: token value sent to ASM service on q6.
  222. * Returns stream id.
  223. */
  224. uint8_t q6asm_get_stream_id_from_token(uint32_t token)
  225. {
  226. union asm_token_struct asm_token;
  227. asm_token.token = token;
  228. return asm_token._token.stream_id;
  229. }
  230. EXPORT_SYMBOL(q6asm_get_stream_id_from_token);
  231. static int audio_output_latency_dbgfs_open(struct inode *inode,
  232. struct file *file)
  233. {
  234. file->private_data = inode->i_private;
  235. return 0;
  236. }
  237. static ssize_t audio_output_latency_dbgfs_read(struct file *file,
  238. char __user *buf, size_t count, loff_t *ppos)
  239. {
  240. if (out_buffer == NULL) {
  241. pr_err("%s: out_buffer is null\n", __func__);
  242. return 0;
  243. }
  244. snprintf(out_buffer, OUT_BUFFER_SIZE, "%ld,%ld,%ld,%ld,%ld,%ld,",
  245. out_cold_tv.tv_sec, out_cold_tv.tv_usec, out_warm_tv.tv_sec,
  246. out_warm_tv.tv_usec, out_cont_tv.tv_sec, out_cont_tv.tv_usec);
  247. return simple_read_from_buffer(buf, OUT_BUFFER_SIZE, ppos,
  248. out_buffer, OUT_BUFFER_SIZE);
  249. }
  250. static ssize_t audio_output_latency_dbgfs_write(struct file *file,
  251. const char __user *buf, size_t count, loff_t *ppos)
  252. {
  253. char *temp;
  254. if (count > 2*sizeof(char)) {
  255. pr_err("%s: err count is more %zd\n", __func__, count);
  256. return -EINVAL;
  257. }
  258. temp = kmalloc(2*sizeof(char), GFP_KERNEL);
  259. out_cold_index = 0;
  260. if (temp) {
  261. if (copy_from_user(temp, buf, 2*sizeof(char))) {
  262. pr_err("%s: copy from user failed for size %zd\n",
  263. __func__, 2*sizeof(char));
  264. kfree(temp);
  265. return -EFAULT;
  266. }
  267. if (!kstrtol(temp, 10, &out_enable_flag)) {
  268. kfree(temp);
  269. return count;
  270. }
  271. kfree(temp);
  272. }
  273. return -EINVAL;
  274. }
  275. static const struct file_operations audio_output_latency_debug_fops = {
  276. .open = audio_output_latency_dbgfs_open,
  277. .read = audio_output_latency_dbgfs_read,
  278. .write = audio_output_latency_dbgfs_write
  279. };
  280. static int audio_input_latency_dbgfs_open(struct inode *inode,
  281. struct file *file)
  282. {
  283. file->private_data = inode->i_private;
  284. return 0;
  285. }
  286. static ssize_t audio_input_latency_dbgfs_read(struct file *file,
  287. char __user *buf, size_t count, loff_t *ppos)
  288. {
  289. if (in_buffer == NULL) {
  290. pr_err("%s: in_buffer is null\n", __func__);
  291. return 0;
  292. }
  293. snprintf(in_buffer, IN_BUFFER_SIZE, "%ld,%ld,",
  294. in_cont_tv.tv_sec, in_cont_tv.tv_usec);
  295. return simple_read_from_buffer(buf, IN_BUFFER_SIZE, ppos,
  296. in_buffer, IN_BUFFER_SIZE);
  297. }
  298. static ssize_t audio_input_latency_dbgfs_write(struct file *file,
  299. const char __user *buf, size_t count, loff_t *ppos)
  300. {
  301. char *temp;
  302. if (count > 2*sizeof(char)) {
  303. pr_err("%s: err count is more %zd\n", __func__, count);
  304. return -EINVAL;
  305. }
  306. temp = kmalloc(2*sizeof(char), GFP_KERNEL);
  307. if (temp) {
  308. if (copy_from_user(temp, buf, 2*sizeof(char))) {
  309. pr_err("%s: copy from user failed for size %zd\n",
  310. __func__, 2*sizeof(char));
  311. kfree(temp);
  312. return -EFAULT;
  313. }
  314. if (!kstrtol(temp, 10, &in_enable_flag)) {
  315. kfree(temp);
  316. return count;
  317. }
  318. kfree(temp);
  319. }
  320. return -EINVAL;
  321. }
  322. static const struct file_operations audio_input_latency_debug_fops = {
  323. .open = audio_input_latency_dbgfs_open,
  324. .read = audio_input_latency_dbgfs_read,
  325. .write = audio_input_latency_dbgfs_write
  326. };
  327. static void config_debug_fs_write_cb(void)
  328. {
  329. if (out_enable_flag) {
  330. /* For first Write done log the time and reset
  331. * out_cold_index
  332. */
  333. if (out_cold_index != 1) {
  334. do_gettimeofday(&out_cold_tv);
  335. pr_debug("COLD: apr_send_pkt at %ld sec %ld microsec\n",
  336. out_cold_tv.tv_sec,
  337. out_cold_tv.tv_usec);
  338. out_cold_index = 1;
  339. }
  340. pr_debug("%s: out_enable_flag %ld\n",
  341. __func__, out_enable_flag);
  342. }
  343. }
  344. static void config_debug_fs_read_cb(void)
  345. {
  346. if (in_enable_flag) {
  347. /* when in_cont_index == 7, DSP would be
  348. * writing into the 8th 512 byte buffer and this
  349. * timestamp is tapped here.Once done it then writes
  350. * to 9th 512 byte buffer.These two buffers(8th, 9th)
  351. * reach the test application in 5th iteration and that
  352. * timestamp is tapped at user level. The difference
  353. * of these two timestamps gives us the time between
  354. * the time at which dsp started filling the sample
  355. * required and when it reached the test application.
  356. * Hence continuous input latency
  357. */
  358. if (in_cont_index == 7) {
  359. do_gettimeofday(&in_cont_tv);
  360. pr_info("%s: read buffer at %ld sec %ld microsec\n",
  361. __func__,
  362. in_cont_tv.tv_sec, in_cont_tv.tv_usec);
  363. }
  364. in_cont_index++;
  365. }
  366. }
  367. static void config_debug_fs_reset_index(void)
  368. {
  369. in_cont_index = 0;
  370. }
  371. static void config_debug_fs_run(void)
  372. {
  373. if (out_enable_flag) {
  374. do_gettimeofday(&out_cold_tv);
  375. pr_debug("%s: COLD apr_send_pkt at %ld sec %ld microsec\n",
  376. __func__, out_cold_tv.tv_sec, out_cold_tv.tv_usec);
  377. }
  378. }
  379. static void config_debug_fs_write(struct audio_buffer *ab)
  380. {
  381. if (out_enable_flag) {
  382. char zero_pattern[2] = {0x00, 0x00};
  383. /* If First two byte is non zero and last two byte
  384. * is zero then it is warm output pattern
  385. */
  386. if ((strcmp(((char *)ab->data), zero_pattern)) &&
  387. (!strcmp(((char *)ab->data + 2), zero_pattern))) {
  388. do_gettimeofday(&out_warm_tv);
  389. pr_debug("%s: WARM:apr_send_pkt at %ld sec %ld microsec\n",
  390. __func__,
  391. out_warm_tv.tv_sec,
  392. out_warm_tv.tv_usec);
  393. pr_debug("%s: Warm Pattern Matched\n", __func__);
  394. }
  395. /* If First two byte is zero and last two byte is
  396. * non zero then it is cont output pattern
  397. */
  398. else if ((!strcmp(((char *)ab->data), zero_pattern))
  399. && (strcmp(((char *)ab->data + 2), zero_pattern))) {
  400. do_gettimeofday(&out_cont_tv);
  401. pr_debug("%s: CONT:apr_send_pkt at %ld sec %ld microsec\n",
  402. __func__,
  403. out_cont_tv.tv_sec,
  404. out_cont_tv.tv_usec);
  405. pr_debug("%s: Cont Pattern Matched\n", __func__);
  406. }
  407. }
  408. }
  409. static void config_debug_fs_init(void)
  410. {
  411. out_buffer = kzalloc(OUT_BUFFER_SIZE, GFP_KERNEL);
  412. if (out_buffer == NULL)
  413. goto outbuf_fail;
  414. in_buffer = kzalloc(IN_BUFFER_SIZE, GFP_KERNEL);
  415. if (in_buffer == NULL)
  416. goto inbuf_fail;
  417. out_dentry = debugfs_create_file("audio_out_latency_measurement_node",
  418. 0664,
  419. NULL, NULL, &audio_output_latency_debug_fops);
  420. if (IS_ERR(out_dentry)) {
  421. pr_err("%s: debugfs_create_file failed\n", __func__);
  422. goto file_fail;
  423. }
  424. in_dentry = debugfs_create_file("audio_in_latency_measurement_node",
  425. 0664,
  426. NULL, NULL, &audio_input_latency_debug_fops);
  427. if (IS_ERR(in_dentry)) {
  428. pr_err("%s: debugfs_create_file failed\n", __func__);
  429. goto file_fail;
  430. }
  431. return;
  432. file_fail:
  433. kfree(in_buffer);
  434. inbuf_fail:
  435. kfree(out_buffer);
  436. outbuf_fail:
  437. in_buffer = NULL;
  438. out_buffer = NULL;
  439. }
  440. #else
  441. static void config_debug_fs_write(struct audio_buffer *ab)
  442. {
  443. }
  444. static void config_debug_fs_run(void)
  445. {
  446. }
  447. static void config_debug_fs_reset_index(void)
  448. {
  449. }
  450. static void config_debug_fs_read_cb(void)
  451. {
  452. }
  453. static void config_debug_fs_write_cb(void)
  454. {
  455. }
  456. static void config_debug_fs_init(void)
  457. {
  458. }
  459. #endif
  460. int q6asm_mmap_apr_dereg(void)
  461. {
  462. int c;
  463. c = atomic_sub_return(1, &this_mmap.ref_cnt);
  464. if (c == 0) {
  465. apr_deregister(this_mmap.apr);
  466. common_client.mmap_apr = NULL;
  467. pr_debug("%s: APR De-Register common port\n", __func__);
  468. } else if (c < 0) {
  469. pr_err("%s: APR Common Port Already Closed %d\n",
  470. __func__, c);
  471. atomic_set(&this_mmap.ref_cnt, 0);
  472. }
  473. return 0;
  474. }
  475. static int q6asm_session_alloc(struct audio_client *ac)
  476. {
  477. int n;
  478. for (n = 1; n <= ASM_ACTIVE_STREAMS_ALLOWED; n++) {
  479. if (!session[n]) {
  480. session[n] = ac;
  481. return n;
  482. }
  483. }
  484. pr_err("%s: session not available\n", __func__);
  485. return -ENOMEM;
  486. }
  487. static bool q6asm_is_valid_audio_client(struct audio_client *ac)
  488. {
  489. int n;
  490. for (n = 1; n <= ASM_ACTIVE_STREAMS_ALLOWED; n++) {
  491. if (session[n] == ac)
  492. return 1;
  493. }
  494. return 0;
  495. }
  496. static void q6asm_session_free(struct audio_client *ac)
  497. {
  498. pr_debug("%s: sessionid[%d]\n", __func__, ac->session);
  499. rtac_remove_popp_from_adm_devices(ac->session);
  500. session[ac->session] = 0;
  501. ac->session = 0;
  502. ac->perf_mode = LEGACY_PCM_MODE;
  503. ac->fptr_cache_ops = NULL;
  504. }
  505. static uint32_t q6asm_get_next_buf(struct audio_client *ac,
  506. uint32_t curr_buf, uint32_t max_buf_cnt)
  507. {
  508. dev_vdbg(ac->dev, "%s: curr_buf = %d, max_buf_cnt = %d\n",
  509. __func__, curr_buf, max_buf_cnt);
  510. curr_buf += 1;
  511. return (curr_buf >= max_buf_cnt) ? 0 : curr_buf;
  512. }
  513. static int q6asm_map_cal_memory(int32_t cal_type,
  514. struct cal_block_data *cal_block)
  515. {
  516. int result = 0;
  517. struct asm_buffer_node *buf_node = NULL;
  518. struct list_head *ptr, *next;
  519. if (cal_block == NULL) {
  520. pr_err("%s: cal_block is NULL!\n",
  521. __func__);
  522. goto done;
  523. }
  524. if (cal_block->cal_data.paddr == 0) {
  525. pr_debug("%s: No address to map!\n",
  526. __func__);
  527. goto done;
  528. }
  529. common_client.mmap_apr = q6asm_mmap_apr_reg();
  530. if (common_client.mmap_apr == NULL) {
  531. pr_err("%s: q6asm_mmap_apr_reg failed\n",
  532. __func__);
  533. result = -EPERM;
  534. goto done;
  535. }
  536. common_client.apr = common_client.mmap_apr;
  537. if (cal_block->map_data.map_size == 0) {
  538. pr_debug("%s: map size is 0!\n",
  539. __func__);
  540. goto done;
  541. }
  542. /* Use second asm buf to map memory */
  543. if (common_client.port[IN].buf == NULL) {
  544. pr_err("%s: common buf is NULL\n",
  545. __func__);
  546. result = -EINVAL;
  547. goto done;
  548. }
  549. common_client.port[IN].buf->phys = cal_block->cal_data.paddr;
  550. result = q6asm_memory_map_regions(&common_client,
  551. IN, cal_block->map_data.map_size, 1, 1);
  552. if (result < 0) {
  553. pr_err("%s: mmap did not work! size = %zd result %d\n",
  554. __func__,
  555. cal_block->map_data.map_size, result);
  556. pr_debug("%s: mmap did not work! addr = 0x%pK, size = %zd\n",
  557. __func__,
  558. &cal_block->cal_data.paddr,
  559. cal_block->map_data.map_size);
  560. goto done;
  561. }
  562. list_for_each_safe(ptr, next,
  563. &common_client.port[IN].mem_map_handle) {
  564. buf_node = list_entry(ptr, struct asm_buffer_node,
  565. list);
  566. if (buf_node->buf_phys_addr == cal_block->cal_data.paddr) {
  567. cal_block->map_data.q6map_handle = buf_node->mmap_hdl;
  568. break;
  569. }
  570. }
  571. done:
  572. return result;
  573. }
  574. static int remap_cal_data(int32_t cal_type, struct cal_block_data *cal_block)
  575. {
  576. int ret = 0;
  577. if (cal_block->map_data.ion_client == NULL) {
  578. pr_err("%s: No ION allocation for cal type %d!\n",
  579. __func__, cal_type);
  580. ret = -EINVAL;
  581. goto done;
  582. }
  583. if ((cal_block->map_data.map_size > 0) &&
  584. (cal_block->map_data.q6map_handle == 0)) {
  585. ret = q6asm_map_cal_memory(cal_type, cal_block);
  586. if (ret < 0) {
  587. pr_err("%s: mmap did not work! size = %zd ret %d\n",
  588. __func__, cal_block->map_data.map_size, ret);
  589. goto done;
  590. }
  591. }
  592. done:
  593. return ret;
  594. }
  595. static int q6asm_unmap_cal_memory(int32_t cal_type,
  596. struct cal_block_data *cal_block)
  597. {
  598. int result = 0;
  599. int result2 = 0;
  600. if (cal_block == NULL) {
  601. pr_err("%s: cal_block is NULL!\n",
  602. __func__);
  603. result = -EINVAL;
  604. goto done;
  605. }
  606. if (cal_block->map_data.q6map_handle == 0) {
  607. pr_debug("%s: No address to unmap!\n",
  608. __func__);
  609. result = -EINVAL;
  610. goto done;
  611. }
  612. if (common_client.mmap_apr == NULL) {
  613. common_client.mmap_apr = q6asm_mmap_apr_reg();
  614. if (common_client.mmap_apr == NULL) {
  615. pr_err("%s: q6asm_mmap_apr_reg failed\n",
  616. __func__);
  617. result = -EPERM;
  618. goto done;
  619. }
  620. }
  621. result2 = q6asm_memory_unmap_regions(&common_client, IN);
  622. if (result2 < 0) {
  623. pr_err("%s: unmap failed, err %d\n",
  624. __func__, result2);
  625. result = result2;
  626. }
  627. cal_block->map_data.q6map_handle = 0;
  628. done:
  629. return result;
  630. }
  631. int q6asm_unmap_cal_data(int cal_type, struct cal_block_data *cal_block)
  632. {
  633. int ret = 0;
  634. if ((cal_block->map_data.map_size > 0) &&
  635. (cal_block->map_data.q6map_handle != 0)) {
  636. ret = q6asm_unmap_cal_memory(cal_type, cal_block);
  637. if (ret < 0) {
  638. pr_err("%s: unmap did not work! size = %zd ret %d\n",
  639. __func__, cal_block->map_data.map_size, ret);
  640. goto done;
  641. }
  642. }
  643. done:
  644. return ret;
  645. }
  646. int send_asm_custom_topology(struct audio_client *ac)
  647. {
  648. struct cal_block_data *cal_block = NULL;
  649. struct cmd_set_topologies asm_top;
  650. int result = 0;
  651. int result1 = 0;
  652. if (cal_data[ASM_CUSTOM_TOP_CAL] == NULL)
  653. goto done;
  654. mutex_lock(&cal_data[ASM_CUSTOM_TOP_CAL]->lock);
  655. if (!set_custom_topology)
  656. goto unlock;
  657. set_custom_topology = 0;
  658. cal_block = cal_utils_get_only_cal_block(cal_data[ASM_CUSTOM_TOP_CAL]);
  659. if (cal_block == NULL)
  660. goto unlock;
  661. if (cal_block->cal_data.size == 0) {
  662. pr_debug("%s: No cal to send!\n", __func__);
  663. goto unlock;
  664. }
  665. pr_debug("%s: Sending cal_index %d\n", __func__, ASM_CUSTOM_TOP_CAL);
  666. result = remap_cal_data(ASM_CUST_TOPOLOGY_CAL_TYPE, cal_block);
  667. if (result) {
  668. pr_err("%s: Remap_cal_data failed for cal %d!\n",
  669. __func__, ASM_CUSTOM_TOP_CAL);
  670. goto unlock;
  671. }
  672. q6asm_add_hdr_custom_topology(ac, &asm_top.hdr, sizeof(asm_top));
  673. atomic_set(&ac->mem_state, -1);
  674. asm_top.hdr.opcode = ASM_CMD_ADD_TOPOLOGIES;
  675. asm_top.payload_addr_lsw = lower_32_bits(cal_block->cal_data.paddr);
  676. asm_top.payload_addr_msw = msm_audio_populate_upper_32_bits(
  677. cal_block->cal_data.paddr);
  678. asm_top.mem_map_handle = cal_block->map_data.q6map_handle;
  679. asm_top.payload_size = cal_block->cal_data.size;
  680. pr_debug("%s: Sending ASM_CMD_ADD_TOPOLOGIES payload = %pK, size = %d, map handle = 0x%x\n",
  681. __func__, &cal_block->cal_data.paddr,
  682. asm_top.payload_size, asm_top.mem_map_handle);
  683. result = apr_send_pkt(ac->apr, (uint32_t *) &asm_top);
  684. if (result < 0) {
  685. pr_err("%s: Set topologies failed result %d\n",
  686. __func__, result);
  687. pr_debug("%s: Set topologies failed payload = 0x%pK\n",
  688. __func__, &cal_block->cal_data.paddr);
  689. goto unmap;
  690. }
  691. result = wait_event_timeout(ac->mem_wait,
  692. (atomic_read(&ac->mem_state) >= 0), 5*HZ);
  693. if (!result) {
  694. pr_err("%s: Set topologies failed timeout\n", __func__);
  695. pr_debug("%s: Set topologies failed after timedout payload = 0x%pK\n",
  696. __func__, &cal_block->cal_data.paddr);
  697. result = -ETIMEDOUT;
  698. goto unmap;
  699. }
  700. if (atomic_read(&ac->mem_state) > 0) {
  701. pr_err("%s: DSP returned error[%s]\n",
  702. __func__, adsp_err_get_err_str(
  703. atomic_read(&ac->mem_state)));
  704. result = adsp_err_get_lnx_err_code(
  705. atomic_read(&ac->mem_state));
  706. goto unmap;
  707. }
  708. unmap:
  709. result1 = q6asm_unmap_cal_memory(ASM_CUST_TOPOLOGY_CAL_TYPE,
  710. cal_block);
  711. if (result1 < 0) {
  712. result = result1;
  713. pr_debug("%s: unmap cal failed! %d\n", __func__, result);
  714. }
  715. unlock:
  716. mutex_unlock(&cal_data[ASM_CUSTOM_TOP_CAL]->lock);
  717. done:
  718. return result;
  719. }
  720. int q6asm_map_rtac_block(struct rtac_cal_block_data *cal_block)
  721. {
  722. int result = 0;
  723. struct asm_buffer_node *buf_node = NULL;
  724. struct list_head *ptr, *next;
  725. pr_debug("%s:\n", __func__);
  726. if (cal_block == NULL) {
  727. pr_err("%s: cal_block is NULL!\n",
  728. __func__);
  729. result = -EINVAL;
  730. goto done;
  731. }
  732. if (cal_block->cal_data.paddr == 0) {
  733. pr_debug("%s: No address to map!\n",
  734. __func__);
  735. result = -EINVAL;
  736. goto done;
  737. }
  738. if (common_client.mmap_apr == NULL) {
  739. common_client.mmap_apr = q6asm_mmap_apr_reg();
  740. if (common_client.mmap_apr == NULL) {
  741. pr_err("%s: q6asm_mmap_apr_reg failed\n",
  742. __func__);
  743. result = -EPERM;
  744. goto done;
  745. }
  746. }
  747. if (cal_block->map_data.map_size == 0) {
  748. pr_debug("%s: map size is 0!\n",
  749. __func__);
  750. result = -EINVAL;
  751. goto done;
  752. }
  753. /* Use second asm buf to map memory */
  754. if (common_client.port[OUT].buf == NULL) {
  755. pr_err("%s: common buf is NULL\n",
  756. __func__);
  757. result = -EINVAL;
  758. goto done;
  759. }
  760. common_client.port[OUT].buf->phys = cal_block->cal_data.paddr;
  761. result = q6asm_memory_map_regions(&common_client,
  762. OUT, cal_block->map_data.map_size, 1, 1);
  763. if (result < 0) {
  764. pr_err("%s: mmap did not work! size = %d result %d\n",
  765. __func__,
  766. cal_block->map_data.map_size, result);
  767. pr_debug("%s: mmap did not work! addr = 0x%pK, size = %d\n",
  768. __func__,
  769. &cal_block->cal_data.paddr,
  770. cal_block->map_data.map_size);
  771. goto done;
  772. }
  773. list_for_each_safe(ptr, next,
  774. &common_client.port[OUT].mem_map_handle) {
  775. buf_node = list_entry(ptr, struct asm_buffer_node,
  776. list);
  777. if (buf_node->buf_phys_addr == cal_block->cal_data.paddr) {
  778. cal_block->map_data.map_handle = buf_node->mmap_hdl;
  779. break;
  780. }
  781. }
  782. done:
  783. return result;
  784. }
  785. int q6asm_unmap_rtac_block(uint32_t *mem_map_handle)
  786. {
  787. int result = 0;
  788. int result2 = 0;
  789. pr_debug("%s:\n", __func__);
  790. if (mem_map_handle == NULL) {
  791. pr_debug("%s: Map handle is NULL, nothing to unmap\n",
  792. __func__);
  793. goto done;
  794. }
  795. if (*mem_map_handle == 0) {
  796. pr_debug("%s: Map handle is 0, nothing to unmap\n",
  797. __func__);
  798. goto done;
  799. }
  800. if (common_client.mmap_apr == NULL) {
  801. common_client.mmap_apr = q6asm_mmap_apr_reg();
  802. if (common_client.mmap_apr == NULL) {
  803. pr_err("%s: q6asm_mmap_apr_reg failed\n",
  804. __func__);
  805. result = -EPERM;
  806. goto done;
  807. }
  808. }
  809. result2 = q6asm_memory_unmap_regions(&common_client, OUT);
  810. if (result2 < 0) {
  811. pr_err("%s: unmap failed, err %d\n",
  812. __func__, result2);
  813. result = result2;
  814. } else {
  815. mem_map_handle = 0;
  816. }
  817. result2 = q6asm_mmap_apr_dereg();
  818. if (result2 < 0) {
  819. pr_err("%s: q6asm_mmap_apr_dereg failed, err %d\n",
  820. __func__, result2);
  821. result = result2;
  822. }
  823. done:
  824. return result;
  825. }
  826. int q6asm_audio_client_buf_free(unsigned int dir,
  827. struct audio_client *ac)
  828. {
  829. struct audio_port_data *port;
  830. int cnt = 0;
  831. int rc = 0;
  832. pr_debug("%s: Session id %d\n", __func__, ac->session);
  833. mutex_lock(&ac->cmd_lock);
  834. if (ac->io_mode & SYNC_IO_MODE) {
  835. port = &ac->port[dir];
  836. if (!port->buf) {
  837. pr_err("%s: buf NULL\n", __func__);
  838. mutex_unlock(&ac->cmd_lock);
  839. return 0;
  840. }
  841. cnt = port->max_buf_cnt - 1;
  842. if (cnt >= 0) {
  843. rc = q6asm_memory_unmap_regions(ac, dir);
  844. if (rc < 0)
  845. pr_err("%s: Memory_unmap_regions failed %d\n",
  846. __func__, rc);
  847. }
  848. while (cnt >= 0) {
  849. if (port->buf[cnt].data) {
  850. if (!rc || atomic_read(&ac->reset))
  851. msm_audio_ion_free(
  852. port->buf[cnt].client,
  853. port->buf[cnt].handle);
  854. port->buf[cnt].client = NULL;
  855. port->buf[cnt].handle = NULL;
  856. port->buf[cnt].data = NULL;
  857. port->buf[cnt].phys = 0;
  858. --(port->max_buf_cnt);
  859. }
  860. --cnt;
  861. }
  862. kfree(port->buf);
  863. port->buf = NULL;
  864. }
  865. mutex_unlock(&ac->cmd_lock);
  866. return 0;
  867. }
  868. int q6asm_audio_client_buf_free_contiguous(unsigned int dir,
  869. struct audio_client *ac)
  870. {
  871. struct audio_port_data *port;
  872. int cnt = 0;
  873. int rc = 0;
  874. pr_debug("%s: Session id %d\n", __func__, ac->session);
  875. mutex_lock(&ac->cmd_lock);
  876. port = &ac->port[dir];
  877. if (!port->buf) {
  878. mutex_unlock(&ac->cmd_lock);
  879. return 0;
  880. }
  881. cnt = port->max_buf_cnt - 1;
  882. if (cnt >= 0) {
  883. rc = q6asm_memory_unmap(ac, port->buf[0].phys, dir);
  884. if (rc < 0)
  885. pr_err("%s: Memory_unmap_regions failed %d\n",
  886. __func__, rc);
  887. }
  888. if (port->buf[0].data) {
  889. pr_debug("%s: data[%pK]phys[%pK][%pK] , client[%pK] handle[%pK]\n",
  890. __func__,
  891. port->buf[0].data,
  892. &port->buf[0].phys,
  893. &port->buf[0].phys,
  894. port->buf[0].client,
  895. port->buf[0].handle);
  896. if (!rc || atomic_read(&ac->reset))
  897. msm_audio_ion_free(port->buf[0].client,
  898. port->buf[0].handle);
  899. port->buf[0].client = NULL;
  900. port->buf[0].handle = NULL;
  901. }
  902. while (cnt >= 0) {
  903. port->buf[cnt].data = NULL;
  904. port->buf[cnt].phys = 0;
  905. cnt--;
  906. }
  907. port->max_buf_cnt = 0;
  908. kfree(port->buf);
  909. port->buf = NULL;
  910. mutex_unlock(&ac->cmd_lock);
  911. return 0;
  912. }
  913. void q6asm_audio_client_free(struct audio_client *ac)
  914. {
  915. int loopcnt;
  916. struct audio_port_data *port;
  917. if (!ac) {
  918. pr_err("%s: ac %pK\n", __func__, ac);
  919. return;
  920. }
  921. if (!ac->session) {
  922. pr_err("%s: ac session invalid\n", __func__);
  923. return;
  924. }
  925. mutex_lock(&session_lock);
  926. pr_debug("%s: Session id %d\n", __func__, ac->session);
  927. if (ac->io_mode & SYNC_IO_MODE) {
  928. for (loopcnt = 0; loopcnt <= OUT; loopcnt++) {
  929. port = &ac->port[loopcnt];
  930. if (!port->buf)
  931. continue;
  932. pr_debug("%s: loopcnt = %d\n",
  933. __func__, loopcnt);
  934. q6asm_audio_client_buf_free(loopcnt, ac);
  935. }
  936. }
  937. rtac_set_asm_handle(ac->session, NULL);
  938. apr_deregister(ac->apr2);
  939. apr_deregister(ac->apr);
  940. q6asm_mmap_apr_dereg();
  941. ac->apr2 = NULL;
  942. ac->apr = NULL;
  943. ac->mmap_apr = NULL;
  944. q6asm_session_free(ac);
  945. pr_debug("%s: APR De-Register\n", __func__);
  946. /*done:*/
  947. kfree(ac);
  948. ac = NULL;
  949. mutex_unlock(&session_lock);
  950. }
  951. int q6asm_set_io_mode(struct audio_client *ac, uint32_t mode1)
  952. {
  953. uint32_t mode;
  954. int ret = 0;
  955. if (ac == NULL) {
  956. pr_err("%s: APR handle NULL\n", __func__);
  957. return -EINVAL;
  958. }
  959. ac->io_mode &= 0xFF00;
  960. mode = (mode1 & 0xF);
  961. pr_debug("%s: ac->mode after anding with FF00:0x%x,\n",
  962. __func__, ac->io_mode);
  963. if ((mode == ASYNC_IO_MODE) || (mode == SYNC_IO_MODE)) {
  964. ac->io_mode |= mode1;
  965. pr_debug("%s: Set Mode to 0x%x\n", __func__, ac->io_mode);
  966. } else {
  967. pr_err("%s: Not an valid IO Mode:%d\n", __func__, ac->io_mode);
  968. ret = -EINVAL;
  969. }
  970. return ret;
  971. }
  972. void *q6asm_mmap_apr_reg(void)
  973. {
  974. if ((atomic_read(&this_mmap.ref_cnt) == 0) ||
  975. (this_mmap.apr == NULL)) {
  976. this_mmap.apr = apr_register("ADSP", "ASM",
  977. (apr_fn)q6asm_srvc_callback,
  978. 0x0FFFFFFFF, &this_mmap);
  979. if (this_mmap.apr == NULL) {
  980. pr_debug("%s: Unable to register APR ASM common port\n",
  981. __func__);
  982. goto fail;
  983. }
  984. }
  985. atomic_inc(&this_mmap.ref_cnt);
  986. return this_mmap.apr;
  987. fail:
  988. return NULL;
  989. }
  990. int q6asm_send_stream_cmd(struct audio_client *ac,
  991. struct msm_adsp_event_data *data)
  992. {
  993. char *asm_params = NULL;
  994. struct apr_hdr hdr;
  995. int sz, rc;
  996. if (!data || !ac) {
  997. pr_err("%s: %s is NULL\n", __func__,
  998. (!data) ? "data" : "ac");
  999. rc = -EINVAL;
  1000. goto done;
  1001. }
  1002. if (data->event_type >= ARRAY_SIZE(adsp_reg_event_opcode)) {
  1003. pr_err("%s: event %u out of boundary of array size of (%lu)\n",
  1004. __func__, data->event_type,
  1005. (long)ARRAY_SIZE(adsp_reg_event_opcode));
  1006. rc = -EINVAL;
  1007. goto done;
  1008. }
  1009. sz = sizeof(struct apr_hdr) + data->payload_len;
  1010. asm_params = kzalloc(sz, GFP_KERNEL);
  1011. if (!asm_params) {
  1012. rc = -ENOMEM;
  1013. goto done;
  1014. }
  1015. q6asm_add_hdr_async(ac, &hdr, sz, TRUE);
  1016. atomic_set(&ac->cmd_state_pp, -1);
  1017. hdr.opcode = adsp_reg_event_opcode[data->event_type];
  1018. memcpy(asm_params, &hdr, sizeof(struct apr_hdr));
  1019. memcpy(asm_params + sizeof(struct apr_hdr),
  1020. data->payload, data->payload_len);
  1021. rc = apr_send_pkt(ac->apr, (uint32_t *) asm_params);
  1022. if (rc < 0) {
  1023. pr_err("%s: stream event cmd apr pkt failed\n", __func__);
  1024. rc = -EINVAL;
  1025. goto fail_send_param;
  1026. }
  1027. rc = wait_event_timeout(ac->cmd_wait,
  1028. (atomic_read(&ac->cmd_state_pp) >= 0), 1 * HZ);
  1029. if (!rc) {
  1030. pr_err("%s: timeout for stream event cmd resp\n", __func__);
  1031. rc = -ETIMEDOUT;
  1032. goto fail_send_param;
  1033. }
  1034. if (atomic_read(&ac->cmd_state_pp) > 0) {
  1035. pr_err("%s: DSP returned error[%s] for stream event cmd\n",
  1036. __func__, adsp_err_get_err_str(
  1037. atomic_read(&ac->cmd_state_pp)));
  1038. rc = adsp_err_get_lnx_err_code(
  1039. atomic_read(&ac->cmd_state_pp));
  1040. goto fail_send_param;
  1041. }
  1042. rc = 0;
  1043. fail_send_param:
  1044. kfree(asm_params);
  1045. done:
  1046. return rc;
  1047. }
  1048. struct audio_client *q6asm_audio_client_alloc(app_cb cb, void *priv)
  1049. {
  1050. struct audio_client *ac;
  1051. int n;
  1052. int lcnt = 0;
  1053. int rc = 0;
  1054. ac = kzalloc(sizeof(struct audio_client), GFP_KERNEL);
  1055. if (!ac)
  1056. return NULL;
  1057. mutex_lock(&session_lock);
  1058. n = q6asm_session_alloc(ac);
  1059. if (n <= 0) {
  1060. pr_err("%s: ASM Session alloc fail n=%d\n", __func__, n);
  1061. mutex_unlock(&session_lock);
  1062. goto fail_session;
  1063. }
  1064. ac->session = n;
  1065. ac->cb = cb;
  1066. ac->path_delay = UINT_MAX;
  1067. ac->priv = priv;
  1068. ac->io_mode = SYNC_IO_MODE;
  1069. ac->perf_mode = LEGACY_PCM_MODE;
  1070. ac->fptr_cache_ops = NULL;
  1071. /* DSP expects stream id from 1 */
  1072. ac->stream_id = 1;
  1073. ac->apr = apr_register("ADSP", "ASM",
  1074. (apr_fn)q6asm_callback,
  1075. ((ac->session) << 8 | 0x0001),
  1076. ac);
  1077. if (ac->apr == NULL) {
  1078. pr_err("%s: Registration with APR failed\n", __func__);
  1079. mutex_unlock(&session_lock);
  1080. goto fail_apr1;
  1081. }
  1082. ac->apr2 = apr_register("ADSP", "ASM",
  1083. (apr_fn)q6asm_callback,
  1084. ((ac->session) << 8 | 0x0002),
  1085. ac);
  1086. if (ac->apr2 == NULL) {
  1087. pr_err("%s: Registration with APR-2 failed\n", __func__);
  1088. mutex_unlock(&session_lock);
  1089. goto fail_apr2;
  1090. }
  1091. rtac_set_asm_handle(n, ac->apr);
  1092. pr_debug("%s: Registering the common port with APR\n", __func__);
  1093. ac->mmap_apr = q6asm_mmap_apr_reg();
  1094. if (ac->mmap_apr == NULL) {
  1095. mutex_unlock(&session_lock);
  1096. goto fail_mmap;
  1097. }
  1098. init_waitqueue_head(&ac->cmd_wait);
  1099. init_waitqueue_head(&ac->time_wait);
  1100. init_waitqueue_head(&ac->mem_wait);
  1101. atomic_set(&ac->time_flag, 1);
  1102. atomic_set(&ac->reset, 0);
  1103. INIT_LIST_HEAD(&ac->port[0].mem_map_handle);
  1104. INIT_LIST_HEAD(&ac->port[1].mem_map_handle);
  1105. pr_debug("%s: mem_map_handle list init'ed\n", __func__);
  1106. mutex_init(&ac->cmd_lock);
  1107. for (lcnt = 0; lcnt <= OUT; lcnt++) {
  1108. mutex_init(&ac->port[lcnt].lock);
  1109. spin_lock_init(&ac->port[lcnt].dsp_lock);
  1110. }
  1111. atomic_set(&ac->cmd_state, 0);
  1112. atomic_set(&ac->cmd_state_pp, 0);
  1113. atomic_set(&ac->mem_state, 0);
  1114. rc = send_asm_custom_topology(ac);
  1115. if (rc < 0) {
  1116. mutex_unlock(&session_lock);
  1117. goto fail_mmap;
  1118. }
  1119. pr_debug("%s: session[%d]\n", __func__, ac->session);
  1120. mutex_unlock(&session_lock);
  1121. return ac;
  1122. fail_mmap:
  1123. apr_deregister(ac->apr2);
  1124. fail_apr2:
  1125. apr_deregister(ac->apr);
  1126. fail_apr1:
  1127. q6asm_session_free(ac);
  1128. fail_session:
  1129. kfree(ac);
  1130. return NULL;
  1131. }
  1132. struct audio_client *q6asm_get_audio_client(int session_id)
  1133. {
  1134. if (session_id == ASM_CONTROL_SESSION)
  1135. return &common_client;
  1136. if ((session_id <= 0) || (session_id > ASM_ACTIVE_STREAMS_ALLOWED)) {
  1137. pr_err("%s: invalid session: %d\n", __func__, session_id);
  1138. goto err;
  1139. }
  1140. if (!session[session_id]) {
  1141. pr_err("%s: session not active: %d\n", __func__, session_id);
  1142. goto err;
  1143. }
  1144. return session[session_id];
  1145. err:
  1146. return NULL;
  1147. }
  1148. int q6asm_audio_client_buf_alloc(unsigned int dir,
  1149. struct audio_client *ac,
  1150. unsigned int bufsz,
  1151. uint32_t bufcnt)
  1152. {
  1153. int cnt = 0;
  1154. int rc = 0;
  1155. struct audio_buffer *buf;
  1156. size_t len;
  1157. if (!(ac) || !(bufsz) || ((dir != IN) && (dir != OUT))) {
  1158. pr_err("%s: ac %pK bufsz %d dir %d\n", __func__, ac, bufsz,
  1159. dir);
  1160. return -EINVAL;
  1161. }
  1162. pr_debug("%s: session[%d]bufsz[%d]bufcnt[%d]\n", __func__, ac->session,
  1163. bufsz, bufcnt);
  1164. if (ac->session <= 0 || ac->session > 8) {
  1165. pr_err("%s: Session ID is invalid, session = %d\n", __func__,
  1166. ac->session);
  1167. goto fail;
  1168. }
  1169. if (ac->io_mode & SYNC_IO_MODE) {
  1170. if (ac->port[dir].buf) {
  1171. pr_debug("%s: buffer already allocated\n", __func__);
  1172. return 0;
  1173. }
  1174. mutex_lock(&ac->cmd_lock);
  1175. if (bufcnt > (U32_MAX/sizeof(struct audio_buffer))) {
  1176. pr_err("%s: Buffer size overflows", __func__);
  1177. mutex_unlock(&ac->cmd_lock);
  1178. goto fail;
  1179. }
  1180. buf = kzalloc(((sizeof(struct audio_buffer))*bufcnt),
  1181. GFP_KERNEL);
  1182. if (!buf) {
  1183. mutex_unlock(&ac->cmd_lock);
  1184. goto fail;
  1185. }
  1186. ac->port[dir].buf = buf;
  1187. while (cnt < bufcnt) {
  1188. if (bufsz > 0) {
  1189. if (!buf[cnt].data) {
  1190. rc = msm_audio_ion_alloc("asm_client",
  1191. &buf[cnt].client, &buf[cnt].handle,
  1192. bufsz,
  1193. (ion_phys_addr_t *)&buf[cnt].phys,
  1194. &len,
  1195. &buf[cnt].data);
  1196. if (rc) {
  1197. pr_err("%s: ION Get Physical for AUDIO failed, rc = %d\n",
  1198. __func__, rc);
  1199. mutex_unlock(&ac->cmd_lock);
  1200. goto fail;
  1201. }
  1202. buf[cnt].used = 1;
  1203. buf[cnt].size = bufsz;
  1204. buf[cnt].actual_size = bufsz;
  1205. pr_debug("%s: data[%pK]phys[%pK][%pK]\n",
  1206. __func__,
  1207. buf[cnt].data,
  1208. &buf[cnt].phys,
  1209. &buf[cnt].phys);
  1210. cnt++;
  1211. }
  1212. }
  1213. }
  1214. ac->port[dir].max_buf_cnt = cnt;
  1215. mutex_unlock(&ac->cmd_lock);
  1216. rc = q6asm_memory_map_regions(ac, dir, bufsz, cnt, 0);
  1217. if (rc < 0) {
  1218. pr_err("%s: CMD Memory_map_regions failed %d for size %d\n",
  1219. __func__, rc, bufsz);
  1220. goto fail;
  1221. }
  1222. }
  1223. return 0;
  1224. fail:
  1225. q6asm_audio_client_buf_free(dir, ac);
  1226. return -EINVAL;
  1227. }
  1228. int q6asm_audio_client_buf_alloc_contiguous(unsigned int dir,
  1229. struct audio_client *ac,
  1230. unsigned int bufsz,
  1231. unsigned int bufcnt)
  1232. {
  1233. int cnt = 0;
  1234. int rc = 0;
  1235. struct audio_buffer *buf;
  1236. size_t len;
  1237. int bytes_to_alloc;
  1238. if (!(ac) || ((dir != IN) && (dir != OUT))) {
  1239. pr_err("%s: ac %pK dir %d\n", __func__, ac, dir);
  1240. return -EINVAL;
  1241. }
  1242. pr_debug("%s: session[%d]bufsz[%d]bufcnt[%d]\n",
  1243. __func__, ac->session,
  1244. bufsz, bufcnt);
  1245. if (ac->session <= 0 || ac->session > 8) {
  1246. pr_err("%s: Session ID is invalid, session = %d\n", __func__,
  1247. ac->session);
  1248. goto fail;
  1249. }
  1250. if (ac->port[dir].buf) {
  1251. pr_err("%s: buffer already allocated\n", __func__);
  1252. return 0;
  1253. }
  1254. mutex_lock(&ac->cmd_lock);
  1255. buf = kzalloc(((sizeof(struct audio_buffer))*bufcnt),
  1256. GFP_KERNEL);
  1257. if (!buf) {
  1258. pr_err("%s: buffer allocation failed\n", __func__);
  1259. mutex_unlock(&ac->cmd_lock);
  1260. goto fail;
  1261. }
  1262. ac->port[dir].buf = buf;
  1263. /* check for integer overflow */
  1264. if ((bufcnt > 0) && ((INT_MAX / bufcnt) < bufsz)) {
  1265. pr_err("%s: integer overflow\n", __func__);
  1266. mutex_unlock(&ac->cmd_lock);
  1267. goto fail;
  1268. }
  1269. bytes_to_alloc = bufsz * bufcnt;
  1270. /* The size to allocate should be multiple of 4K bytes */
  1271. bytes_to_alloc = PAGE_ALIGN(bytes_to_alloc);
  1272. rc = msm_audio_ion_alloc("asm_client", &buf[0].client, &buf[0].handle,
  1273. bytes_to_alloc,
  1274. (ion_phys_addr_t *)&buf[0].phys, &len,
  1275. &buf[0].data);
  1276. if (rc) {
  1277. pr_err("%s: Audio ION alloc is failed, rc = %d\n",
  1278. __func__, rc);
  1279. mutex_unlock(&ac->cmd_lock);
  1280. goto fail;
  1281. }
  1282. buf[0].used = dir ^ 1;
  1283. buf[0].size = bufsz;
  1284. buf[0].actual_size = bufsz;
  1285. cnt = 1;
  1286. while (cnt < bufcnt) {
  1287. if (bufsz > 0) {
  1288. buf[cnt].data = buf[0].data + (cnt * bufsz);
  1289. buf[cnt].phys = buf[0].phys + (cnt * bufsz);
  1290. if (!buf[cnt].data) {
  1291. pr_err("%s: Buf alloc failed\n",
  1292. __func__);
  1293. mutex_unlock(&ac->cmd_lock);
  1294. goto fail;
  1295. }
  1296. buf[cnt].used = dir ^ 1;
  1297. buf[cnt].size = bufsz;
  1298. buf[cnt].actual_size = bufsz;
  1299. pr_debug("%s: data[%pK]phys[%pK][%pK]\n",
  1300. __func__,
  1301. buf[cnt].data,
  1302. &buf[cnt].phys,
  1303. &buf[cnt].phys);
  1304. }
  1305. cnt++;
  1306. }
  1307. ac->port[dir].max_buf_cnt = cnt;
  1308. mutex_unlock(&ac->cmd_lock);
  1309. rc = q6asm_memory_map_regions(ac, dir, bufsz, cnt, 1);
  1310. if (rc < 0) {
  1311. pr_err("%s: CMD Memory_map_regions failed %d for size %d\n",
  1312. __func__, rc, bufsz);
  1313. goto fail;
  1314. }
  1315. return 0;
  1316. fail:
  1317. q6asm_audio_client_buf_free_contiguous(dir, ac);
  1318. return -EINVAL;
  1319. }
  1320. static int32_t q6asm_srvc_callback(struct apr_client_data *data, void *priv)
  1321. {
  1322. uint32_t dir = 0;
  1323. uint32_t i = IN;
  1324. uint32_t *payload;
  1325. unsigned long dsp_flags;
  1326. struct asm_buffer_node *buf_node = NULL;
  1327. struct list_head *ptr, *next;
  1328. union asm_token_struct asm_token;
  1329. struct audio_client *ac = NULL;
  1330. struct audio_port_data *port;
  1331. if (!data) {
  1332. pr_err("%s: Invalid CB\n", __func__);
  1333. return 0;
  1334. }
  1335. payload = data->payload;
  1336. if (data->opcode == RESET_EVENTS) {
  1337. pr_debug("%s: Reset event is received: %d %d apr[%pK]\n",
  1338. __func__,
  1339. data->reset_event,
  1340. data->reset_proc,
  1341. this_mmap.apr);
  1342. atomic_set(&this_mmap.ref_cnt, 0);
  1343. apr_reset(this_mmap.apr);
  1344. this_mmap.apr = NULL;
  1345. for (; i <= OUT; i++) {
  1346. list_for_each_safe(ptr, next,
  1347. &common_client.port[i].mem_map_handle) {
  1348. buf_node = list_entry(ptr,
  1349. struct asm_buffer_node,
  1350. list);
  1351. if (buf_node->buf_phys_addr ==
  1352. common_client.port[i].buf->phys) {
  1353. list_del(&buf_node->list);
  1354. kfree(buf_node);
  1355. }
  1356. }
  1357. pr_debug("%s: Clearing custom topology\n", __func__);
  1358. }
  1359. cal_utils_clear_cal_block_q6maps(ASM_MAX_CAL_TYPES, cal_data);
  1360. common_client.mmap_apr = NULL;
  1361. mutex_lock(&cal_data[ASM_CUSTOM_TOP_CAL]->lock);
  1362. set_custom_topology = 1;
  1363. mutex_unlock(&cal_data[ASM_CUSTOM_TOP_CAL]->lock);
  1364. topology_map_handle = 0;
  1365. rtac_clear_mapping(ASM_RTAC_CAL);
  1366. return 0;
  1367. }
  1368. asm_token.token = data->token;
  1369. ac = q6asm_get_audio_client(asm_token._token.session_id);
  1370. dir = q6asm_get_flag_from_token(&asm_token, ASM_DIRECTION_OFFSET);
  1371. if (!ac) {
  1372. pr_debug("%s: session[%d] already freed\n",
  1373. __func__, asm_token._token.session_id);
  1374. return 0;
  1375. }
  1376. if (data->payload_size > sizeof(int)) {
  1377. pr_debug("%s:ptr0[0x%x]ptr1[0x%x]opcode[0x%x] token[0x%x]payload_s[%d] src[%d] dest[%d]sid[%d]dir[%d]\n",
  1378. __func__, payload[0], payload[1], data->opcode,
  1379. data->token, data->payload_size, data->src_port,
  1380. data->dest_port, asm_token._token.session_id, dir);
  1381. pr_debug("%s:Payload = [0x%x] status[0x%x]\n",
  1382. __func__, payload[0], payload[1]);
  1383. } else if (data->payload_size == sizeof(int)) {
  1384. pr_debug("%s:ptr0[0x%x]opcode[0x%x] token[0x%x]payload_s[%d] src[%d] dest[%d]sid[%d]dir[%d]\n",
  1385. __func__, payload[0], data->opcode,
  1386. data->token, data->payload_size, data->src_port,
  1387. data->dest_port, asm_token._token.session_id, dir);
  1388. pr_debug("%s:Payload = [0x%x]\n",
  1389. __func__, payload[0]);
  1390. }
  1391. if (data->opcode == APR_BASIC_RSP_RESULT) {
  1392. switch (payload[0]) {
  1393. case ASM_CMD_SHARED_MEM_MAP_REGIONS:
  1394. case ASM_CMD_SHARED_MEM_UNMAP_REGIONS:
  1395. case ASM_CMD_ADD_TOPOLOGIES:
  1396. if (payload[1] != 0) {
  1397. pr_err("%s: cmd = 0x%x returned error = 0x%x sid:%d\n",
  1398. __func__, payload[0], payload[1],
  1399. asm_token._token.session_id);
  1400. if (payload[0] ==
  1401. ASM_CMD_SHARED_MEM_UNMAP_REGIONS)
  1402. atomic_set(&ac->unmap_cb_success, 0);
  1403. atomic_set(&ac->mem_state, payload[1]);
  1404. wake_up(&ac->mem_wait);
  1405. } else {
  1406. if (payload[0] ==
  1407. ASM_CMD_SHARED_MEM_UNMAP_REGIONS)
  1408. atomic_set(&ac->unmap_cb_success, 1);
  1409. }
  1410. if (atomic_cmpxchg(&ac->mem_state, -1, 0) == -1)
  1411. wake_up(&ac->mem_wait);
  1412. dev_vdbg(ac->dev, "%s: Payload = [0x%x] status[0x%x]\n",
  1413. __func__, payload[0], payload[1]);
  1414. break;
  1415. default:
  1416. pr_debug("%s: command[0x%x] not expecting rsp\n",
  1417. __func__, payload[0]);
  1418. break;
  1419. }
  1420. return 0;
  1421. }
  1422. port = &ac->port[dir];
  1423. switch (data->opcode) {
  1424. case ASM_CMDRSP_SHARED_MEM_MAP_REGIONS:{
  1425. pr_debug("%s:PL#0[0x%x] dir=0x%x s_id=0x%x\n",
  1426. __func__, payload[0], dir, asm_token._token.session_id);
  1427. spin_lock_irqsave(&port->dsp_lock, dsp_flags);
  1428. if (atomic_cmpxchg(&ac->mem_state, -1, 0) == -1) {
  1429. ac->port[dir].tmp_hdl = payload[0];
  1430. wake_up(&ac->mem_wait);
  1431. }
  1432. spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
  1433. break;
  1434. }
  1435. case ASM_CMD_SHARED_MEM_UNMAP_REGIONS:{
  1436. pr_debug("%s: PL#0[0x%x]PL#1 [0x%x]\n",
  1437. __func__, payload[0], payload[1]);
  1438. spin_lock_irqsave(&port->dsp_lock, dsp_flags);
  1439. if (atomic_cmpxchg(&ac->mem_state, -1, 0) == -1)
  1440. wake_up(&ac->mem_wait);
  1441. spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
  1442. break;
  1443. }
  1444. default:
  1445. pr_debug("%s: command[0x%x]success [0x%x]\n",
  1446. __func__, payload[0], payload[1]);
  1447. }
  1448. if (ac->cb)
  1449. ac->cb(data->opcode, data->token,
  1450. data->payload, ac->priv);
  1451. return 0;
  1452. }
  1453. static void q6asm_process_mtmx_get_param_rsp(struct audio_client *ac,
  1454. struct asm_mtmx_strtr_get_params_cmdrsp *cmdrsp)
  1455. {
  1456. struct asm_session_mtmx_strtr_param_session_time_v3_t *time;
  1457. if (cmdrsp->err_code) {
  1458. dev_err_ratelimited(ac->dev,
  1459. "%s: err=%x, mod_id=%x, param_id=%x\n",
  1460. __func__, cmdrsp->err_code,
  1461. cmdrsp->param_info.module_id,
  1462. cmdrsp->param_info.param_id);
  1463. return;
  1464. }
  1465. dev_dbg_ratelimited(ac->dev,
  1466. "%s: mod_id=%x, param_id=%x\n", __func__,
  1467. cmdrsp->param_info.module_id,
  1468. cmdrsp->param_info.param_id);
  1469. switch (cmdrsp->param_info.module_id) {
  1470. case ASM_SESSION_MTMX_STRTR_MODULE_ID_AVSYNC:
  1471. switch (cmdrsp->param_info.param_id) {
  1472. case ASM_SESSION_MTMX_STRTR_PARAM_SESSION_TIME_V3:
  1473. time = &cmdrsp->param_data.session_time;
  1474. dev_vdbg(ac->dev, "%s: GET_TIME_V3, time_lsw=%x, time_msw=%x\n",
  1475. __func__, time->session_time_lsw,
  1476. time->session_time_msw);
  1477. ac->time_stamp = (uint64_t)(((uint64_t)
  1478. time->session_time_msw << 32) |
  1479. time->session_time_lsw);
  1480. if (time->flags &
  1481. ASM_SESSION_MTMX_STRTR_PARAM_STIME_TSTMP_FLG_BMASK)
  1482. dev_warn_ratelimited(ac->dev,
  1483. "%s: recv inval tstmp\n",
  1484. __func__);
  1485. if (atomic_cmpxchg(&ac->time_flag, 1, 0))
  1486. wake_up(&ac->time_wait);
  1487. break;
  1488. default:
  1489. dev_err(ac->dev, "%s: unexpected param_id %x\n",
  1490. __func__, cmdrsp->param_info.param_id);
  1491. break;
  1492. }
  1493. break;
  1494. default:
  1495. dev_err(ac->dev, "%s: unexpected mod_id %x\n", __func__,
  1496. cmdrsp->param_info.module_id);
  1497. break;
  1498. }
  1499. }
  1500. static int32_t q6asm_callback(struct apr_client_data *data, void *priv)
  1501. {
  1502. int i = 0;
  1503. struct audio_client *ac = (struct audio_client *)priv;
  1504. unsigned long dsp_flags;
  1505. uint32_t *payload;
  1506. uint32_t wakeup_flag = 1;
  1507. int32_t ret = 0;
  1508. union asm_token_struct asm_token;
  1509. uint8_t buf_index;
  1510. struct msm_adsp_event_data *pp_event_package = NULL;
  1511. uint32_t payload_size = 0;
  1512. if (ac == NULL) {
  1513. pr_err("%s: ac NULL\n", __func__);
  1514. return -EINVAL;
  1515. }
  1516. if (data == NULL) {
  1517. pr_err("%s: data NULL\n", __func__);
  1518. return -EINVAL;
  1519. }
  1520. if (!q6asm_is_valid_audio_client(ac)) {
  1521. pr_err("%s: audio client pointer is invalid, ac = %pK\n",
  1522. __func__, ac);
  1523. return -EINVAL;
  1524. }
  1525. if (ac->session <= 0 || ac->session > 8) {
  1526. pr_err("%s: Session ID is invalid, session = %d\n", __func__,
  1527. ac->session);
  1528. return -EINVAL;
  1529. }
  1530. payload = data->payload;
  1531. asm_token.token = data->token;
  1532. if (q6asm_get_flag_from_token(&asm_token, ASM_CMD_NO_WAIT_OFFSET)) {
  1533. pr_debug("%s: No wait command opcode[0x%x] cmd_opcode:%x\n",
  1534. __func__, data->opcode, payload ? payload[0] : 0);
  1535. wakeup_flag = 0;
  1536. }
  1537. if (data->opcode == RESET_EVENTS) {
  1538. mutex_lock(&ac->cmd_lock);
  1539. atomic_set(&ac->reset, 1);
  1540. if (ac->apr == NULL) {
  1541. ac->apr = ac->apr2;
  1542. ac->apr2 = NULL;
  1543. }
  1544. pr_debug("%s: Reset event is received: %d %d apr[%pK]\n",
  1545. __func__,
  1546. data->reset_event, data->reset_proc, ac->apr);
  1547. if (ac->cb)
  1548. ac->cb(data->opcode, data->token,
  1549. (uint32_t *)data->payload, ac->priv);
  1550. apr_reset(ac->apr);
  1551. ac->apr = NULL;
  1552. atomic_set(&ac->time_flag, 0);
  1553. atomic_set(&ac->cmd_state, 0);
  1554. atomic_set(&ac->mem_state, 0);
  1555. atomic_set(&ac->cmd_state_pp, 0);
  1556. wake_up(&ac->time_wait);
  1557. wake_up(&ac->cmd_wait);
  1558. wake_up(&ac->mem_wait);
  1559. mutex_unlock(&ac->cmd_lock);
  1560. return 0;
  1561. }
  1562. dev_vdbg(ac->dev, "%s: session[%d]opcode[0x%x] token[0x%x]payload_size[%d] src[%d] dest[%d]\n",
  1563. __func__,
  1564. ac->session, data->opcode,
  1565. data->token, data->payload_size, data->src_port,
  1566. data->dest_port);
  1567. if ((data->opcode != ASM_DATA_EVENT_RENDERED_EOS) &&
  1568. (data->opcode != ASM_DATA_EVENT_EOS) &&
  1569. (data->opcode != ASM_SESSION_EVENT_RX_UNDERFLOW)) {
  1570. if (payload == NULL) {
  1571. pr_err("%s: payload is null\n", __func__);
  1572. return -EINVAL;
  1573. }
  1574. dev_vdbg(ac->dev, "%s: Payload = [0x%x] status[0x%x] opcode 0x%x\n",
  1575. __func__, payload[0], payload[1], data->opcode);
  1576. }
  1577. if (data->opcode == APR_BASIC_RSP_RESULT) {
  1578. switch (payload[0]) {
  1579. case ASM_STREAM_CMD_SET_PP_PARAMS_V2:
  1580. if (rtac_make_asm_callback(ac->session, payload,
  1581. data->payload_size))
  1582. break;
  1583. case ASM_SESSION_CMD_PAUSE:
  1584. case ASM_SESSION_CMD_SUSPEND:
  1585. case ASM_DATA_CMD_EOS:
  1586. case ASM_STREAM_CMD_CLOSE:
  1587. case ASM_STREAM_CMD_FLUSH:
  1588. case ASM_SESSION_CMD_RUN_V2:
  1589. case ASM_SESSION_CMD_REGISTER_FORX_OVERFLOW_EVENTS:
  1590. case ASM_STREAM_CMD_FLUSH_READBUFS:
  1591. pr_debug("%s: session %d opcode 0x%x token 0x%x Payload = [0x%x] src %d dest %d\n",
  1592. __func__, ac->session, data->opcode, data->token,
  1593. payload[0], data->src_port, data->dest_port);
  1594. ret = q6asm_is_valid_session(data, priv);
  1595. if (ret != 0) {
  1596. pr_err("%s: session invalid %d\n", __func__, ret);
  1597. return ret;
  1598. }
  1599. case ASM_SESSION_CMD_SET_MTMX_STRTR_PARAMS_V2:
  1600. case ASM_STREAM_CMD_OPEN_READ_V3:
  1601. case ASM_STREAM_CMD_OPEN_WRITE_V3:
  1602. case ASM_STREAM_CMD_OPEN_PULL_MODE_WRITE:
  1603. case ASM_STREAM_CMD_OPEN_PUSH_MODE_READ:
  1604. case ASM_STREAM_CMD_OPEN_READWRITE_V2:
  1605. case ASM_STREAM_CMD_OPEN_LOOPBACK_V2:
  1606. case ASM_STREAM_CMD_OPEN_TRANSCODE_LOOPBACK:
  1607. case ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2:
  1608. case ASM_DATA_CMD_IEC_60958_MEDIA_FMT:
  1609. case ASM_STREAM_CMD_SET_ENCDEC_PARAM:
  1610. case ASM_STREAM_CMD_SET_ENCDEC_PARAM_V2:
  1611. case ASM_STREAM_CMD_REGISTER_ENCDEC_EVENTS:
  1612. case ASM_STREAM_CMD_REGISTER_IEC_61937_FMT_UPDATE:
  1613. case ASM_DATA_CMD_REMOVE_INITIAL_SILENCE:
  1614. case ASM_DATA_CMD_REMOVE_TRAILING_SILENCE:
  1615. case ASM_SESSION_CMD_REGISTER_FOR_RX_UNDERFLOW_EVENTS:
  1616. case ASM_STREAM_CMD_OPEN_WRITE_COMPRESSED:
  1617. pr_debug("%s: session %d opcode 0x%x token 0x%x Payload = [0x%x] stat 0x%x src %d dest %d\n",
  1618. __func__, ac->session,
  1619. data->opcode, data->token,
  1620. payload[0], payload[1],
  1621. data->src_port, data->dest_port);
  1622. if (payload[1] != 0) {
  1623. pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
  1624. __func__, payload[0], payload[1]);
  1625. if (wakeup_flag) {
  1626. if ((is_adsp_reg_event(payload[0]) >= 0)
  1627. || (payload[0] ==
  1628. ASM_STREAM_CMD_SET_PP_PARAMS_V2))
  1629. atomic_set(&ac->cmd_state_pp,
  1630. payload[1]);
  1631. else
  1632. atomic_set(&ac->cmd_state,
  1633. payload[1]);
  1634. wake_up(&ac->cmd_wait);
  1635. }
  1636. return 0;
  1637. }
  1638. if ((is_adsp_reg_event(payload[0]) >= 0) ||
  1639. (payload[0] == ASM_STREAM_CMD_SET_PP_PARAMS_V2)) {
  1640. if (atomic_read(&ac->cmd_state_pp) &&
  1641. wakeup_flag) {
  1642. atomic_set(&ac->cmd_state_pp, 0);
  1643. wake_up(&ac->cmd_wait);
  1644. }
  1645. } else {
  1646. if (atomic_read(&ac->cmd_state) &&
  1647. wakeup_flag) {
  1648. atomic_set(&ac->cmd_state, 0);
  1649. wake_up(&ac->cmd_wait);
  1650. }
  1651. }
  1652. if (ac->cb)
  1653. ac->cb(data->opcode, data->token,
  1654. (uint32_t *)data->payload, ac->priv);
  1655. break;
  1656. case ASM_CMD_ADD_TOPOLOGIES:
  1657. pr_debug("%s:Payload = [0x%x]stat[0x%x]\n",
  1658. __func__, payload[0], payload[1]);
  1659. if (payload[1] != 0) {
  1660. pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
  1661. __func__, payload[0], payload[1]);
  1662. if (wakeup_flag) {
  1663. atomic_set(&ac->mem_state, payload[1]);
  1664. wake_up(&ac->mem_wait);
  1665. }
  1666. return 0;
  1667. }
  1668. if (atomic_read(&ac->mem_state) && wakeup_flag) {
  1669. atomic_set(&ac->mem_state, 0);
  1670. wake_up(&ac->mem_wait);
  1671. }
  1672. if (ac->cb)
  1673. ac->cb(data->opcode, data->token,
  1674. (uint32_t *)data->payload, ac->priv);
  1675. break;
  1676. case ASM_DATA_EVENT_WATERMARK: {
  1677. pr_debug("%s: Watermark opcode[0x%x] status[0x%x]",
  1678. __func__, payload[0], payload[1]);
  1679. break;
  1680. }
  1681. case ASM_STREAM_CMD_GET_PP_PARAMS_V2:
  1682. pr_debug("%s: ASM_STREAM_CMD_GET_PP_PARAMS_V2 session %d opcode 0x%x token 0x%x src %d dest %d\n",
  1683. __func__, ac->session,
  1684. data->opcode, data->token,
  1685. data->src_port, data->dest_port);
  1686. /* Should only come here if there is an APR */
  1687. /* error or malformed APR packet. Otherwise */
  1688. /* response will be returned as */
  1689. /* ASM_STREAM_CMDRSP_GET_PP_PARAMS_V2 */
  1690. if (payload[1] != 0) {
  1691. pr_err("%s: ASM get param error = %d, resuming\n",
  1692. __func__, payload[1]);
  1693. rtac_make_asm_callback(ac->session, payload,
  1694. data->payload_size);
  1695. }
  1696. break;
  1697. case ASM_STREAM_CMD_REGISTER_PP_EVENTS:
  1698. pr_debug("%s: ASM_STREAM_CMD_REGISTER_PP_EVENTS session %d opcode 0x%x token 0x%x src %d dest %d\n",
  1699. __func__, ac->session,
  1700. data->opcode, data->token,
  1701. data->src_port, data->dest_port);
  1702. if (payload[1] != 0)
  1703. pr_err("%s: ASM get param error = %d, resuming\n",
  1704. __func__, payload[1]);
  1705. atomic_set(&ac->cmd_state_pp, payload[1]);
  1706. wake_up(&ac->cmd_wait);
  1707. break;
  1708. default:
  1709. pr_debug("%s: command[0x%x] not expecting rsp\n",
  1710. __func__, payload[0]);
  1711. break;
  1712. }
  1713. return 0;
  1714. }
  1715. switch (data->opcode) {
  1716. case ASM_DATA_EVENT_WRITE_DONE_V2:{
  1717. struct audio_port_data *port = &ac->port[IN];
  1718. dev_vdbg(ac->dev, "%s: Rxed opcode[0x%x] status[0x%x] token[%d]",
  1719. __func__, payload[0], payload[1],
  1720. data->token);
  1721. if (ac->io_mode & SYNC_IO_MODE) {
  1722. if (port->buf == NULL) {
  1723. pr_err("%s: Unexpected Write Done\n",
  1724. __func__);
  1725. return -EINVAL;
  1726. }
  1727. spin_lock_irqsave(&port->dsp_lock, dsp_flags);
  1728. buf_index = asm_token._token.buf_index;
  1729. if (lower_32_bits(port->buf[buf_index].phys) !=
  1730. payload[0] ||
  1731. msm_audio_populate_upper_32_bits(
  1732. port->buf[buf_index].phys) != payload[1]) {
  1733. pr_debug("%s: Expected addr %pK\n",
  1734. __func__, &port->buf[buf_index].phys);
  1735. pr_err("%s: rxedl[0x%x] rxedu [0x%x]\n",
  1736. __func__, payload[0], payload[1]);
  1737. spin_unlock_irqrestore(&port->dsp_lock,
  1738. dsp_flags);
  1739. return -EINVAL;
  1740. }
  1741. port->buf[buf_index].used = 1;
  1742. spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
  1743. config_debug_fs_write_cb();
  1744. for (i = 0; i < port->max_buf_cnt; i++)
  1745. dev_vdbg(ac->dev, "%s %d\n",
  1746. __func__, port->buf[i].used);
  1747. }
  1748. break;
  1749. }
  1750. case ASM_STREAM_CMDRSP_GET_PP_PARAMS_V2:
  1751. pr_debug("%s: ASM_STREAM_CMDRSP_GET_PP_PARAMS_V2 session %d opcode 0x%x token 0x%x src %d dest %d\n",
  1752. __func__, ac->session, data->opcode,
  1753. data->token,
  1754. data->src_port, data->dest_port);
  1755. if (payload[0] != 0) {
  1756. pr_err("%s: ASM_STREAM_CMDRSP_GET_PP_PARAMS_V2 returned error = 0x%x\n",
  1757. __func__, payload[0]);
  1758. } else if (generic_get_data) {
  1759. generic_get_data->valid = 1;
  1760. if (generic_get_data->is_inband) {
  1761. pr_debug("%s: payload[1] = 0x%x, payload[2]=0x%x, payload[3]=0x%x\n",
  1762. __func__, payload[1], payload[2], payload[3]);
  1763. generic_get_data->size_in_ints = payload[3]>>2;
  1764. for (i = 0; i < payload[3]>>2; i++) {
  1765. generic_get_data->ints[i] =
  1766. payload[4+i];
  1767. pr_debug("%s: ASM callback val %i = %i\n",
  1768. __func__, i, payload[4+i]);
  1769. }
  1770. pr_debug("%s: callback size in ints = %i\n",
  1771. __func__,
  1772. generic_get_data->size_in_ints);
  1773. }
  1774. if (atomic_read(&ac->cmd_state) && wakeup_flag) {
  1775. atomic_set(&ac->cmd_state, 0);
  1776. wake_up(&ac->cmd_wait);
  1777. }
  1778. break;
  1779. }
  1780. rtac_make_asm_callback(ac->session, payload,
  1781. data->payload_size);
  1782. break;
  1783. case ASM_DATA_EVENT_READ_DONE_V2:{
  1784. struct audio_port_data *port = &ac->port[OUT];
  1785. config_debug_fs_read_cb();
  1786. dev_vdbg(ac->dev, "%s: ReadDone: status=%d buff_add=0x%x act_size=%d offset=%d\n",
  1787. __func__, payload[READDONE_IDX_STATUS],
  1788. payload[READDONE_IDX_BUFADD_LSW],
  1789. payload[READDONE_IDX_SIZE],
  1790. payload[READDONE_IDX_OFFSET]);
  1791. dev_vdbg(ac->dev, "%s: ReadDone:msw_ts=%d lsw_ts=%d memmap_hdl=0x%x flags=%d id=%d num=%d\n",
  1792. __func__, payload[READDONE_IDX_MSW_TS],
  1793. payload[READDONE_IDX_LSW_TS],
  1794. payload[READDONE_IDX_MEMMAP_HDL],
  1795. payload[READDONE_IDX_FLAGS],
  1796. payload[READDONE_IDX_SEQ_ID],
  1797. payload[READDONE_IDX_NUMFRAMES]);
  1798. if (ac->io_mode & SYNC_IO_MODE) {
  1799. if (port->buf == NULL) {
  1800. pr_err("%s: Unexpected Write Done\n", __func__);
  1801. return -EINVAL;
  1802. }
  1803. spin_lock_irqsave(&port->dsp_lock, dsp_flags);
  1804. buf_index = asm_token._token.buf_index;
  1805. port->buf[buf_index].used = 0;
  1806. if (lower_32_bits(port->buf[buf_index].phys) !=
  1807. payload[READDONE_IDX_BUFADD_LSW] ||
  1808. msm_audio_populate_upper_32_bits(
  1809. port->buf[buf_index].phys) !=
  1810. payload[READDONE_IDX_BUFADD_MSW]) {
  1811. dev_vdbg(ac->dev, "%s: Expected addr %pK\n",
  1812. __func__, &port->buf[buf_index].phys);
  1813. pr_err("%s: rxedl[0x%x] rxedu[0x%x]\n",
  1814. __func__,
  1815. payload[READDONE_IDX_BUFADD_LSW],
  1816. payload[READDONE_IDX_BUFADD_MSW]);
  1817. spin_unlock_irqrestore(&port->dsp_lock,
  1818. dsp_flags);
  1819. break;
  1820. }
  1821. port->buf[buf_index].actual_size =
  1822. payload[READDONE_IDX_SIZE];
  1823. spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
  1824. }
  1825. break;
  1826. }
  1827. case ASM_DATA_EVENT_EOS:
  1828. case ASM_DATA_EVENT_RENDERED_EOS:
  1829. pr_debug("%s: EOS ACK received: rxed session %d opcode 0x%x token 0x%x src %d dest %d\n",
  1830. __func__, ac->session,
  1831. data->opcode, data->token,
  1832. data->src_port, data->dest_port);
  1833. break;
  1834. case ASM_SESSION_EVENTX_OVERFLOW:
  1835. pr_debug("%s: ASM_SESSION_EVENTX_OVERFLOW session %d opcode 0x%x token 0x%x src %d dest %d\n",
  1836. __func__, ac->session,
  1837. data->opcode, data->token,
  1838. data->src_port, data->dest_port);
  1839. break;
  1840. case ASM_SESSION_EVENT_RX_UNDERFLOW:
  1841. pr_debug("%s: ASM_SESSION_EVENT_RX_UNDERFLOW session %d opcode 0x%x token 0x%x src %d dest %d\n",
  1842. __func__, ac->session,
  1843. data->opcode, data->token,
  1844. data->src_port, data->dest_port);
  1845. break;
  1846. case ASM_SESSION_CMDRSP_GET_SESSIONTIME_V3:
  1847. dev_vdbg(ac->dev, "%s: ASM_SESSION_CMDRSP_GET_SESSIONTIME_V3, payload[0] = %d, payload[1] = %d, payload[2] = %d\n",
  1848. __func__,
  1849. payload[0], payload[1], payload[2]);
  1850. ac->time_stamp = (uint64_t)(((uint64_t)payload[2] << 32) |
  1851. payload[1]);
  1852. if (atomic_cmpxchg(&ac->time_flag, 1, 0))
  1853. wake_up(&ac->time_wait);
  1854. break;
  1855. case ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY:
  1856. case ASM_DATA_EVENT_ENC_SR_CM_CHANGE_NOTIFY:
  1857. pr_debug("%s: ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY session %d opcode 0x%x token 0x%x src %d dest %d\n",
  1858. __func__, ac->session,
  1859. data->opcode, data->token,
  1860. data->src_port, data->dest_port);
  1861. pr_debug("%s: ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY, payload[0] = %d, payload[1] = %d, payload[2] = %d, payload[3] = %d\n",
  1862. __func__,
  1863. payload[0], payload[1], payload[2],
  1864. payload[3]);
  1865. break;
  1866. case ASM_SESSION_CMDRSP_GET_MTMX_STRTR_PARAMS_V2:
  1867. q6asm_process_mtmx_get_param_rsp(ac, (void *) payload);
  1868. break;
  1869. case ASM_STREAM_PP_EVENT:
  1870. case ASM_STREAM_CMD_ENCDEC_EVENTS:
  1871. case ASM_STREAM_CMD_REGISTER_IEC_61937_FMT_UPDATE:
  1872. pr_debug("%s: ASM_STREAM_EVENT payload[0][0x%x] payload[1][0x%x]",
  1873. __func__, payload[0], payload[1]);
  1874. i = is_adsp_raise_event(data->opcode);
  1875. if (i < 0)
  1876. return 0;
  1877. /* repack payload for asm_stream_pp_event
  1878. * package is composed of event type + size + actual payload
  1879. */
  1880. payload_size = data->payload_size;
  1881. pp_event_package = kzalloc(payload_size
  1882. + sizeof(struct msm_adsp_event_data),
  1883. GFP_ATOMIC);
  1884. if (!pp_event_package)
  1885. return -ENOMEM;
  1886. pp_event_package->event_type = i;
  1887. pp_event_package->payload_len = payload_size;
  1888. memcpy((void *)pp_event_package->payload,
  1889. data->payload, payload_size);
  1890. ac->cb(data->opcode, data->token,
  1891. (void *)pp_event_package, ac->priv);
  1892. kfree(pp_event_package);
  1893. return 0;
  1894. case ASM_SESSION_CMDRSP_ADJUST_SESSION_CLOCK_V2:
  1895. pr_debug("%s: ASM_SESSION_CMDRSP_ADJUST_SESSION_CLOCK_V2 sesion %d status 0x%x msw %u lsw %u\n",
  1896. __func__, ac->session, payload[0], payload[2],
  1897. payload[1]);
  1898. wake_up(&ac->cmd_wait);
  1899. break;
  1900. case ASM_SESSION_CMDRSP_GET_PATH_DELAY_V2:
  1901. pr_debug("%s: ASM_SESSION_CMDRSP_GET_PATH_DELAY_V2 session %d status 0x%x msw %u lsw %u\n",
  1902. __func__, ac->session, payload[0], payload[2],
  1903. payload[1]);
  1904. if (payload[0] == 0) {
  1905. atomic_set(&ac->cmd_state, 0);
  1906. /* ignore msw, as a delay that large shouldn't happen */
  1907. ac->path_delay = payload[1];
  1908. } else {
  1909. atomic_set(&ac->cmd_state, payload[0]);
  1910. ac->path_delay = UINT_MAX;
  1911. }
  1912. wake_up(&ac->cmd_wait);
  1913. break;
  1914. }
  1915. if (ac->cb)
  1916. ac->cb(data->opcode, data->token,
  1917. data->payload, ac->priv);
  1918. return 0;
  1919. }
  1920. void *q6asm_is_cpu_buf_avail(int dir, struct audio_client *ac, uint32_t *size,
  1921. uint32_t *index)
  1922. {
  1923. void *data;
  1924. unsigned char idx;
  1925. struct audio_port_data *port;
  1926. if (!ac || ((dir != IN) && (dir != OUT))) {
  1927. pr_err("%s: ac %pK dir %d\n", __func__, ac, dir);
  1928. return NULL;
  1929. }
  1930. if (ac->io_mode & SYNC_IO_MODE) {
  1931. port = &ac->port[dir];
  1932. mutex_lock(&port->lock);
  1933. idx = port->cpu_buf;
  1934. if (port->buf == NULL) {
  1935. pr_err("%s: Buffer pointer null\n", __func__);
  1936. mutex_unlock(&port->lock);
  1937. return NULL;
  1938. }
  1939. /* dir 0: used = 0 means buf in use
  1940. * dir 1: used = 1 means buf in use
  1941. */
  1942. if (port->buf[idx].used == dir) {
  1943. /* To make it more robust, we could loop and get the
  1944. * next avail buf, its risky though
  1945. */
  1946. pr_err("%s: Next buf idx[0x%x] not available, dir[%d]\n",
  1947. __func__, idx, dir);
  1948. mutex_unlock(&port->lock);
  1949. return NULL;
  1950. }
  1951. *size = port->buf[idx].actual_size;
  1952. *index = port->cpu_buf;
  1953. data = port->buf[idx].data;
  1954. dev_vdbg(ac->dev, "%s: session[%d]index[%d] data[%pK]size[%d]\n",
  1955. __func__,
  1956. ac->session,
  1957. port->cpu_buf,
  1958. data, *size);
  1959. /* By default increase the cpu_buf cnt
  1960. * user accesses this function,increase cpu
  1961. * buf(to avoid another api)
  1962. */
  1963. port->buf[idx].used = dir;
  1964. port->cpu_buf = q6asm_get_next_buf(ac, port->cpu_buf,
  1965. port->max_buf_cnt);
  1966. mutex_unlock(&port->lock);
  1967. return data;
  1968. }
  1969. return NULL;
  1970. }
  1971. int q6asm_cpu_buf_release(int dir, struct audio_client *ac)
  1972. {
  1973. struct audio_port_data *port;
  1974. int ret = 0;
  1975. int idx;
  1976. if (!ac || ((dir != IN) && (dir != OUT))) {
  1977. pr_err("%s: ac %pK dir %d\n", __func__, ac, dir);
  1978. ret = -EINVAL;
  1979. goto exit;
  1980. }
  1981. if (ac->io_mode & SYNC_IO_MODE) {
  1982. port = &ac->port[dir];
  1983. mutex_lock(&port->lock);
  1984. idx = port->cpu_buf;
  1985. if (port->cpu_buf == 0) {
  1986. port->cpu_buf = port->max_buf_cnt - 1;
  1987. } else if (port->cpu_buf < port->max_buf_cnt) {
  1988. port->cpu_buf = port->cpu_buf - 1;
  1989. } else {
  1990. pr_err("%s: buffer index(%d) out of range\n",
  1991. __func__, port->cpu_buf);
  1992. ret = -EINVAL;
  1993. mutex_unlock(&port->lock);
  1994. goto exit;
  1995. }
  1996. port->buf[port->cpu_buf].used = dir ^ 1;
  1997. mutex_unlock(&port->lock);
  1998. }
  1999. exit:
  2000. return ret;
  2001. }
  2002. void *q6asm_is_cpu_buf_avail_nolock(int dir, struct audio_client *ac,
  2003. uint32_t *size, uint32_t *index)
  2004. {
  2005. void *data;
  2006. unsigned char idx;
  2007. struct audio_port_data *port;
  2008. if (!ac || ((dir != IN) && (dir != OUT))) {
  2009. pr_err("%s: ac %pK dir %d\n", __func__, ac, dir);
  2010. return NULL;
  2011. }
  2012. port = &ac->port[dir];
  2013. idx = port->cpu_buf;
  2014. if (port->buf == NULL) {
  2015. pr_err("%s: Buffer pointer null\n", __func__);
  2016. return NULL;
  2017. }
  2018. /*
  2019. * dir 0: used = 0 means buf in use
  2020. * dir 1: used = 1 means buf in use
  2021. */
  2022. if (port->buf[idx].used == dir) {
  2023. /*
  2024. * To make it more robust, we could loop and get the
  2025. * next avail buf, its risky though
  2026. */
  2027. pr_err("%s: Next buf idx[0x%x] not available, dir[%d]\n",
  2028. __func__, idx, dir);
  2029. return NULL;
  2030. }
  2031. *size = port->buf[idx].actual_size;
  2032. *index = port->cpu_buf;
  2033. data = port->buf[idx].data;
  2034. dev_vdbg(ac->dev, "%s: session[%d]index[%d] data[%pK]size[%d]\n",
  2035. __func__, ac->session, port->cpu_buf,
  2036. data, *size);
  2037. /*
  2038. * By default increase the cpu_buf cnt
  2039. * user accesses this function,increase cpu
  2040. * buf(to avoid another api)
  2041. */
  2042. port->buf[idx].used = dir;
  2043. port->cpu_buf = q6asm_get_next_buf(ac, port->cpu_buf,
  2044. port->max_buf_cnt);
  2045. return data;
  2046. }
  2047. int q6asm_is_dsp_buf_avail(int dir, struct audio_client *ac)
  2048. {
  2049. int ret = -1;
  2050. struct audio_port_data *port;
  2051. uint32_t idx;
  2052. if (!ac || (dir != OUT)) {
  2053. pr_err("%s: ac %pK dir %d\n", __func__, ac, dir);
  2054. return ret;
  2055. }
  2056. if (ac->io_mode & SYNC_IO_MODE) {
  2057. port = &ac->port[dir];
  2058. mutex_lock(&port->lock);
  2059. idx = port->dsp_buf;
  2060. if (port->buf[idx].used == (dir ^ 1)) {
  2061. /* To make it more robust, we could loop and get the
  2062. * next avail buf, its risky though
  2063. */
  2064. pr_err("%s: Next buf idx[0x%x] not available, dir[%d]\n",
  2065. __func__, idx, dir);
  2066. mutex_unlock(&port->lock);
  2067. return ret;
  2068. }
  2069. dev_vdbg(ac->dev, "%s: session[%d]dsp_buf=%d cpu_buf=%d\n",
  2070. __func__,
  2071. ac->session, port->dsp_buf, port->cpu_buf);
  2072. ret = ((port->dsp_buf != port->cpu_buf) ? 0 : -1);
  2073. mutex_unlock(&port->lock);
  2074. }
  2075. return ret;
  2076. }
  2077. static void __q6asm_add_hdr(struct audio_client *ac, struct apr_hdr *hdr,
  2078. uint32_t pkt_size, uint32_t cmd_flg, uint32_t stream_id)
  2079. {
  2080. dev_vdbg(ac->dev, "%s: pkt_size=%d cmd_flg=%d session=%d stream_id=%d\n",
  2081. __func__, pkt_size, cmd_flg, ac->session, stream_id);
  2082. mutex_lock(&ac->cmd_lock);
  2083. if (ac->apr == NULL) {
  2084. pr_err("%s: AC APR handle NULL", __func__);
  2085. mutex_unlock(&ac->cmd_lock);
  2086. return;
  2087. }
  2088. hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2089. APR_HDR_LEN(sizeof(struct apr_hdr)),
  2090. APR_PKT_VER);
  2091. hdr->src_svc = ((struct apr_svc *)ac->apr)->id;
  2092. hdr->src_domain = APR_DOMAIN_APPS;
  2093. hdr->dest_svc = APR_SVC_ASM;
  2094. hdr->dest_domain = APR_DOMAIN_ADSP;
  2095. hdr->src_port = ((ac->session << 8) & 0xFF00) | (stream_id);
  2096. hdr->dest_port = ((ac->session << 8) & 0xFF00) | (stream_id);
  2097. if (cmd_flg)
  2098. q6asm_update_token(&hdr->token,
  2099. ac->session,
  2100. 0, /* Stream ID is NA */
  2101. 0, /* Buffer index is NA */
  2102. 0, /* Direction flag is NA */
  2103. WAIT_CMD);
  2104. hdr->pkt_size = pkt_size;
  2105. mutex_unlock(&ac->cmd_lock);
  2106. }
  2107. static void q6asm_add_hdr(struct audio_client *ac, struct apr_hdr *hdr,
  2108. uint32_t pkt_size, uint32_t cmd_flg)
  2109. {
  2110. __q6asm_add_hdr(ac, hdr, pkt_size, cmd_flg, ac->stream_id);
  2111. }
  2112. static void q6asm_stream_add_hdr(struct audio_client *ac, struct apr_hdr *hdr,
  2113. uint32_t pkt_size, uint32_t cmd_flg, int32_t stream_id)
  2114. {
  2115. __q6asm_add_hdr(ac, hdr, pkt_size, cmd_flg, stream_id);
  2116. }
  2117. static void __q6asm_add_hdr_async(struct audio_client *ac, struct apr_hdr *hdr,
  2118. uint32_t pkt_size, uint32_t cmd_flg,
  2119. uint32_t stream_id, u8 no_wait_flag)
  2120. {
  2121. dev_vdbg(ac->dev, "%s: pkt_size = %d, cmd_flg = %d, session = %d stream_id=%d\n",
  2122. __func__, pkt_size, cmd_flg, ac->session, stream_id);
  2123. hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2124. APR_HDR_LEN(sizeof(struct apr_hdr)),
  2125. APR_PKT_VER);
  2126. if (ac->apr == NULL) {
  2127. pr_err("%s: AC APR is NULL", __func__);
  2128. return;
  2129. }
  2130. hdr->src_svc = ((struct apr_svc *)ac->apr)->id;
  2131. hdr->src_domain = APR_DOMAIN_APPS;
  2132. hdr->dest_svc = APR_SVC_ASM;
  2133. hdr->dest_domain = APR_DOMAIN_ADSP;
  2134. hdr->src_port = ((ac->session << 8) & 0xFF00) | (stream_id);
  2135. hdr->dest_port = ((ac->session << 8) & 0xFF00) | (stream_id);
  2136. if (cmd_flg) {
  2137. q6asm_update_token(&hdr->token,
  2138. ac->session,
  2139. 0, /* Stream ID is NA */
  2140. 0, /* Buffer index is NA */
  2141. 0, /* Direction flag is NA */
  2142. no_wait_flag);
  2143. }
  2144. hdr->pkt_size = pkt_size;
  2145. }
  2146. static void q6asm_add_hdr_async(struct audio_client *ac, struct apr_hdr *hdr,
  2147. uint32_t pkt_size, uint32_t cmd_flg)
  2148. {
  2149. __q6asm_add_hdr_async(ac, hdr, pkt_size, cmd_flg,
  2150. ac->stream_id, WAIT_CMD);
  2151. }
  2152. static void q6asm_stream_add_hdr_async(struct audio_client *ac,
  2153. struct apr_hdr *hdr, uint32_t pkt_size,
  2154. uint32_t cmd_flg, int32_t stream_id)
  2155. {
  2156. __q6asm_add_hdr_async(ac, hdr, pkt_size, cmd_flg,
  2157. stream_id, NO_WAIT_CMD);
  2158. }
  2159. static void q6asm_add_hdr_custom_topology(struct audio_client *ac,
  2160. struct apr_hdr *hdr,
  2161. uint32_t pkt_size)
  2162. {
  2163. pr_debug("%s: pkt_size=%d session=%d\n",
  2164. __func__, pkt_size, ac->session);
  2165. if (ac->apr == NULL) {
  2166. pr_err("%s: AC APR handle NULL\n", __func__);
  2167. return;
  2168. }
  2169. mutex_lock(&ac->cmd_lock);
  2170. hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2171. APR_HDR_LEN(sizeof(struct apr_hdr)),
  2172. APR_PKT_VER);
  2173. hdr->src_svc = ((struct apr_svc *)ac->apr)->id;
  2174. hdr->src_domain = APR_DOMAIN_APPS;
  2175. hdr->dest_svc = APR_SVC_ASM;
  2176. hdr->dest_domain = APR_DOMAIN_ADSP;
  2177. hdr->src_port = ((ac->session << 8) & 0xFF00) | 0x01;
  2178. hdr->dest_port = 0;
  2179. q6asm_update_token(&hdr->token,
  2180. ac->session,
  2181. 0, /* Stream ID is NA */
  2182. 0, /* Buffer index is NA */
  2183. 0, /* Direction flag is NA */
  2184. WAIT_CMD);
  2185. hdr->pkt_size = pkt_size;
  2186. mutex_unlock(&ac->cmd_lock);
  2187. }
  2188. static void q6asm_add_mmaphdr(struct audio_client *ac, struct apr_hdr *hdr,
  2189. u32 pkt_size, int dir)
  2190. {
  2191. pr_debug("%s: pkt size=%d\n",
  2192. __func__, pkt_size);
  2193. hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2194. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2195. hdr->src_port = 0;
  2196. hdr->dest_port = 0;
  2197. q6asm_update_token(&hdr->token,
  2198. ac->session,
  2199. 0, /* Stream ID is NA */
  2200. 0, /* Buffer index is NA */
  2201. dir,
  2202. WAIT_CMD);
  2203. hdr->pkt_size = pkt_size;
  2204. }
  2205. static int __q6asm_open_read(struct audio_client *ac,
  2206. uint32_t format, uint16_t bits_per_sample,
  2207. uint32_t pcm_format_block_ver,
  2208. bool ts_mode)
  2209. {
  2210. int rc = 0x00;
  2211. struct asm_stream_cmd_open_read_v3 open;
  2212. config_debug_fs_reset_index();
  2213. if (ac == NULL) {
  2214. pr_err("%s: APR handle NULL\n", __func__);
  2215. return -EINVAL;
  2216. }
  2217. if (ac->apr == NULL) {
  2218. pr_err("%s: AC APR handle NULL\n", __func__);
  2219. return -EINVAL;
  2220. }
  2221. pr_debug("%s: session[%d]\n", __func__, ac->session);
  2222. q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
  2223. atomic_set(&ac->cmd_state, -1);
  2224. open.hdr.opcode = ASM_STREAM_CMD_OPEN_READ_V3;
  2225. /* Stream prio : High, provide meta info with encoded frames */
  2226. open.src_endpointype = ASM_END_POINT_DEVICE_MATRIX;
  2227. open.preprocopo_id = q6asm_get_asm_topology_cal();
  2228. open.bits_per_sample = bits_per_sample;
  2229. open.mode_flags = 0x0;
  2230. ac->topology = open.preprocopo_id;
  2231. ac->app_type = q6asm_get_asm_app_type_cal();
  2232. if (ac->perf_mode == LOW_LATENCY_PCM_MODE) {
  2233. open.mode_flags |= ASM_LOW_LATENCY_TX_STREAM_SESSION <<
  2234. ASM_SHIFT_STREAM_PERF_MODE_FLAG_IN_OPEN_READ;
  2235. } else {
  2236. open.mode_flags |= ASM_LEGACY_STREAM_SESSION <<
  2237. ASM_SHIFT_STREAM_PERF_MODE_FLAG_IN_OPEN_READ;
  2238. }
  2239. switch (format) {
  2240. case FORMAT_LINEAR_PCM:
  2241. open.mode_flags |= 0x00;
  2242. open.enc_cfg_id = q6asm_get_pcm_format_id(pcm_format_block_ver);
  2243. if (ts_mode)
  2244. open.mode_flags |= ABSOLUTE_TIMESTAMP_ENABLE;
  2245. break;
  2246. case FORMAT_MPEG4_AAC:
  2247. open.mode_flags |= BUFFER_META_ENABLE;
  2248. open.enc_cfg_id = ASM_MEDIA_FMT_AAC_V2;
  2249. break;
  2250. case FORMAT_G711_ALAW_FS:
  2251. open.mode_flags |= BUFFER_META_ENABLE;
  2252. open.enc_cfg_id = ASM_MEDIA_FMT_G711_ALAW_FS;
  2253. break;
  2254. case FORMAT_G711_MLAW_FS:
  2255. open.mode_flags |= BUFFER_META_ENABLE;
  2256. open.enc_cfg_id = ASM_MEDIA_FMT_G711_MLAW_FS;
  2257. break;
  2258. case FORMAT_V13K:
  2259. open.mode_flags |= BUFFER_META_ENABLE;
  2260. open.enc_cfg_id = ASM_MEDIA_FMT_V13K_FS;
  2261. break;
  2262. case FORMAT_EVRC:
  2263. open.mode_flags |= BUFFER_META_ENABLE;
  2264. open.enc_cfg_id = ASM_MEDIA_FMT_EVRC_FS;
  2265. break;
  2266. case FORMAT_AMRNB:
  2267. open.mode_flags |= BUFFER_META_ENABLE;
  2268. open.enc_cfg_id = ASM_MEDIA_FMT_AMRNB_FS;
  2269. break;
  2270. case FORMAT_AMRWB:
  2271. open.mode_flags |= BUFFER_META_ENABLE;
  2272. open.enc_cfg_id = ASM_MEDIA_FMT_AMRWB_FS;
  2273. break;
  2274. default:
  2275. pr_err("%s: Invalid format 0x%x\n",
  2276. __func__, format);
  2277. rc = -EINVAL;
  2278. goto fail_cmd;
  2279. }
  2280. rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
  2281. if (rc < 0) {
  2282. pr_err("%s: open failed op[0x%x]rc[%d]\n",
  2283. __func__, open.hdr.opcode, rc);
  2284. rc = -EINVAL;
  2285. goto fail_cmd;
  2286. }
  2287. rc = wait_event_timeout(ac->cmd_wait,
  2288. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  2289. if (!rc) {
  2290. pr_err("%s: timeout. waited for open read\n",
  2291. __func__);
  2292. rc = -ETIMEDOUT;
  2293. goto fail_cmd;
  2294. }
  2295. if (atomic_read(&ac->cmd_state) > 0) {
  2296. pr_err("%s: DSP returned error[%s]\n",
  2297. __func__, adsp_err_get_err_str(
  2298. atomic_read(&ac->cmd_state)));
  2299. rc = adsp_err_get_lnx_err_code(
  2300. atomic_read(&ac->cmd_state));
  2301. goto fail_cmd;
  2302. }
  2303. ac->io_mode |= TUN_READ_IO_MODE;
  2304. return 0;
  2305. fail_cmd:
  2306. return rc;
  2307. }
  2308. int q6asm_open_read(struct audio_client *ac,
  2309. uint32_t format)
  2310. {
  2311. return __q6asm_open_read(ac, format, 16,
  2312. PCM_MEDIA_FORMAT_V2 /*media fmt block ver*/,
  2313. false/*ts_mode*/);
  2314. }
  2315. int q6asm_open_read_v2(struct audio_client *ac, uint32_t format,
  2316. uint16_t bits_per_sample)
  2317. {
  2318. return __q6asm_open_read(ac, format, bits_per_sample,
  2319. PCM_MEDIA_FORMAT_V2 /*media fmt block ver*/,
  2320. false/*ts_mode*/);
  2321. }
  2322. /*
  2323. * asm_open_read_v3 - Opens audio capture session
  2324. *
  2325. * @ac: Client session handle
  2326. * @format: encoder format
  2327. * @bits_per_sample: bit width of capture session
  2328. */
  2329. int q6asm_open_read_v3(struct audio_client *ac, uint32_t format,
  2330. uint16_t bits_per_sample)
  2331. {
  2332. return __q6asm_open_read(ac, format, bits_per_sample,
  2333. PCM_MEDIA_FORMAT_V3/*media fmt block ver*/,
  2334. false/*ts_mode*/);
  2335. }
  2336. EXPORT_SYMBOL(q6asm_open_read_v3);
  2337. /*
  2338. * asm_open_read_v4 - Opens audio capture session
  2339. *
  2340. * @ac: Client session handle
  2341. * @format: encoder format
  2342. * @bits_per_sample: bit width of capture session
  2343. * @ts_mode: timestamp mode
  2344. */
  2345. int q6asm_open_read_v4(struct audio_client *ac, uint32_t format,
  2346. uint16_t bits_per_sample, bool ts_mode)
  2347. {
  2348. return __q6asm_open_read(ac, format, bits_per_sample,
  2349. PCM_MEDIA_FORMAT_V4 /*media fmt block ver*/,
  2350. ts_mode);
  2351. }
  2352. EXPORT_SYMBOL(q6asm_open_read_v4);
  2353. int q6asm_open_write_compressed(struct audio_client *ac, uint32_t format,
  2354. uint32_t passthrough_flag)
  2355. {
  2356. int rc = 0;
  2357. struct asm_stream_cmd_open_write_compressed open;
  2358. if (ac == NULL) {
  2359. pr_err("%s: ac[%pK] NULL\n", __func__, ac);
  2360. rc = -EINVAL;
  2361. goto fail_cmd;
  2362. }
  2363. if (ac->apr == NULL) {
  2364. pr_err("%s: APR handle[%pK] NULL\n", __func__, ac->apr);
  2365. rc = -EINVAL;
  2366. goto fail_cmd;
  2367. }
  2368. pr_debug("%s: session[%d] wr_format[0x%x]", __func__, ac->session,
  2369. format);
  2370. q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
  2371. open.hdr.opcode = ASM_STREAM_CMD_OPEN_WRITE_COMPRESSED;
  2372. atomic_set(&ac->cmd_state, -1);
  2373. switch (format) {
  2374. case FORMAT_AC3:
  2375. open.fmt_id = ASM_MEDIA_FMT_AC3;
  2376. break;
  2377. case FORMAT_EAC3:
  2378. open.fmt_id = ASM_MEDIA_FMT_EAC3;
  2379. break;
  2380. case FORMAT_DTS:
  2381. open.fmt_id = ASM_MEDIA_FMT_DTS;
  2382. break;
  2383. case FORMAT_DSD:
  2384. open.fmt_id = ASM_MEDIA_FMT_DSD;
  2385. break;
  2386. case FORMAT_GEN_COMPR:
  2387. open.fmt_id = ASM_MEDIA_FMT_GENERIC_COMPRESSED;
  2388. break;
  2389. case FORMAT_TRUEHD:
  2390. open.fmt_id = ASM_MEDIA_FMT_TRUEHD;
  2391. break;
  2392. case FORMAT_IEC61937:
  2393. open.fmt_id = ASM_MEDIA_FMT_IEC;
  2394. break;
  2395. default:
  2396. pr_err("%s: Invalid format[%d]\n", __func__, format);
  2397. rc = -EINVAL;
  2398. goto fail_cmd;
  2399. }
  2400. /* Below flag indicates the DSP that Compressed audio input
  2401. * stream is not IEC 61937 or IEC 60958 packetizied
  2402. */
  2403. if (passthrough_flag == COMPRESSED_PASSTHROUGH ||
  2404. passthrough_flag == COMPRESSED_PASSTHROUGH_DSD ||
  2405. passthrough_flag == COMPRESSED_PASSTHROUGH_GEN) {
  2406. open.flags = 0x0;
  2407. pr_debug("%s: Flag 0 COMPRESSED_PASSTHROUGH\n", __func__);
  2408. } else if (passthrough_flag == COMPRESSED_PASSTHROUGH_CONVERT) {
  2409. open.flags = 0x8;
  2410. pr_debug("%s: Flag 8 - COMPRESSED_PASSTHROUGH_CONVERT\n",
  2411. __func__);
  2412. } else if (passthrough_flag == COMPRESSED_PASSTHROUGH_IEC61937) {
  2413. open.flags = 0x1;
  2414. pr_debug("%s: Flag 1 - COMPRESSED_PASSTHROUGH_IEC61937\n",
  2415. __func__);
  2416. } else {
  2417. pr_err("%s: Invalid passthrough type[%d]\n",
  2418. __func__, passthrough_flag);
  2419. rc = -EINVAL;
  2420. goto fail_cmd;
  2421. }
  2422. rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
  2423. if (rc < 0) {
  2424. pr_err("%s: open failed op[0x%x]rc[%d]\n",
  2425. __func__, open.hdr.opcode, rc);
  2426. rc = -EINVAL;
  2427. goto fail_cmd;
  2428. }
  2429. rc = wait_event_timeout(ac->cmd_wait,
  2430. (atomic_read(&ac->cmd_state) >= 0), 1*HZ);
  2431. if (!rc) {
  2432. pr_err("%s: timeout. waited for OPEN_WRITE_COMPR rc[%d]\n",
  2433. __func__, rc);
  2434. rc = -ETIMEDOUT;
  2435. goto fail_cmd;
  2436. }
  2437. if (atomic_read(&ac->cmd_state) > 0) {
  2438. pr_err("%s: DSP returned error[%s]\n",
  2439. __func__, adsp_err_get_err_str(
  2440. atomic_read(&ac->cmd_state)));
  2441. rc = adsp_err_get_lnx_err_code(
  2442. atomic_read(&ac->cmd_state));
  2443. goto fail_cmd;
  2444. }
  2445. return 0;
  2446. fail_cmd:
  2447. return rc;
  2448. }
  2449. static int __q6asm_open_write(struct audio_client *ac, uint32_t format,
  2450. uint16_t bits_per_sample, uint32_t stream_id,
  2451. bool is_gapless_mode,
  2452. uint32_t pcm_format_block_ver)
  2453. {
  2454. int rc = 0x00;
  2455. struct asm_stream_cmd_open_write_v3 open;
  2456. if (ac == NULL) {
  2457. pr_err("%s: APR handle NULL\n", __func__);
  2458. return -EINVAL;
  2459. }
  2460. if (ac->apr == NULL) {
  2461. pr_err("%s: AC APR handle NULL\n", __func__);
  2462. return -EINVAL;
  2463. }
  2464. dev_vdbg(ac->dev, "%s: session[%d] wr_format[0x%x]\n",
  2465. __func__, ac->session, format);
  2466. q6asm_stream_add_hdr(ac, &open.hdr, sizeof(open), TRUE, stream_id);
  2467. atomic_set(&ac->cmd_state, -1);
  2468. /*
  2469. * Updated the token field with stream/session for compressed playback
  2470. * Platform driver must know the the stream with which the command is
  2471. * associated
  2472. */
  2473. if (ac->io_mode & COMPRESSED_STREAM_IO)
  2474. q6asm_update_token(&open.hdr.token,
  2475. ac->session,
  2476. stream_id,
  2477. 0, /* Buffer index is NA */
  2478. 0, /* Direction flag is NA */
  2479. WAIT_CMD);
  2480. dev_vdbg(ac->dev, "%s: token = 0x%x, stream_id %d, session 0x%x\n",
  2481. __func__, open.hdr.token, stream_id, ac->session);
  2482. open.hdr.opcode = ASM_STREAM_CMD_OPEN_WRITE_V3;
  2483. open.mode_flags = 0x00;
  2484. if (ac->perf_mode == ULL_POST_PROCESSING_PCM_MODE)
  2485. open.mode_flags |= ASM_ULL_POST_PROCESSING_STREAM_SESSION;
  2486. else if (ac->perf_mode == ULTRA_LOW_LATENCY_PCM_MODE)
  2487. open.mode_flags |= ASM_ULTRA_LOW_LATENCY_STREAM_SESSION;
  2488. else if (ac->perf_mode == LOW_LATENCY_PCM_MODE)
  2489. open.mode_flags |= ASM_LOW_LATENCY_STREAM_SESSION;
  2490. else {
  2491. open.mode_flags |= ASM_LEGACY_STREAM_SESSION;
  2492. if (is_gapless_mode)
  2493. open.mode_flags |= 1 << ASM_SHIFT_GAPLESS_MODE_FLAG;
  2494. }
  2495. /* source endpoint : matrix */
  2496. open.sink_endpointype = ASM_END_POINT_DEVICE_MATRIX;
  2497. open.bits_per_sample = bits_per_sample;
  2498. open.postprocopo_id = q6asm_get_asm_topology_cal();
  2499. if (ac->perf_mode != LEGACY_PCM_MODE)
  2500. open.postprocopo_id = ASM_STREAM_POSTPROCOPO_ID_NONE;
  2501. pr_debug("%s: perf_mode %d asm_topology 0x%x bps %d\n", __func__,
  2502. ac->perf_mode, open.postprocopo_id, open.bits_per_sample);
  2503. /*
  2504. * For Gapless playback it will use the same session for next stream,
  2505. * So use the same topology
  2506. */
  2507. if (!ac->topology) {
  2508. ac->topology = open.postprocopo_id;
  2509. ac->app_type = q6asm_get_asm_app_type_cal();
  2510. }
  2511. switch (format) {
  2512. case FORMAT_LINEAR_PCM:
  2513. open.dec_fmt_id = q6asm_get_pcm_format_id(pcm_format_block_ver);
  2514. break;
  2515. case FORMAT_MPEG4_AAC:
  2516. open.dec_fmt_id = ASM_MEDIA_FMT_AAC_V2;
  2517. break;
  2518. case FORMAT_MPEG4_MULTI_AAC:
  2519. open.dec_fmt_id = ASM_MEDIA_FMT_AAC_V2;
  2520. break;
  2521. case FORMAT_WMA_V9:
  2522. open.dec_fmt_id = ASM_MEDIA_FMT_WMA_V9_V2;
  2523. break;
  2524. case FORMAT_WMA_V10PRO:
  2525. open.dec_fmt_id = ASM_MEDIA_FMT_WMA_V10PRO_V2;
  2526. break;
  2527. case FORMAT_MP3:
  2528. open.dec_fmt_id = ASM_MEDIA_FMT_MP3;
  2529. break;
  2530. case FORMAT_AC3:
  2531. open.dec_fmt_id = ASM_MEDIA_FMT_AC3;
  2532. break;
  2533. case FORMAT_EAC3:
  2534. open.dec_fmt_id = ASM_MEDIA_FMT_EAC3;
  2535. break;
  2536. case FORMAT_MP2:
  2537. open.dec_fmt_id = ASM_MEDIA_FMT_MP2;
  2538. break;
  2539. case FORMAT_FLAC:
  2540. open.dec_fmt_id = ASM_MEDIA_FMT_FLAC;
  2541. break;
  2542. case FORMAT_ALAC:
  2543. open.dec_fmt_id = ASM_MEDIA_FMT_ALAC;
  2544. break;
  2545. case FORMAT_VORBIS:
  2546. open.dec_fmt_id = ASM_MEDIA_FMT_VORBIS;
  2547. break;
  2548. case FORMAT_APE:
  2549. open.dec_fmt_id = ASM_MEDIA_FMT_APE;
  2550. break;
  2551. case FORMAT_DSD:
  2552. open.dec_fmt_id = ASM_MEDIA_FMT_DSD;
  2553. break;
  2554. case FORMAT_APTX:
  2555. open.dec_fmt_id = ASM_MEDIA_FMT_APTX;
  2556. break;
  2557. case FORMAT_GEN_COMPR:
  2558. open.dec_fmt_id = ASM_MEDIA_FMT_GENERIC_COMPRESSED;
  2559. break;
  2560. default:
  2561. pr_err("%s: Invalid format 0x%x\n", __func__, format);
  2562. rc = -EINVAL;
  2563. goto fail_cmd;
  2564. }
  2565. rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
  2566. if (rc < 0) {
  2567. pr_err("%s: open failed op[0x%x]rc[%d]\n",
  2568. __func__, open.hdr.opcode, rc);
  2569. rc = -EINVAL;
  2570. goto fail_cmd;
  2571. }
  2572. rc = wait_event_timeout(ac->cmd_wait,
  2573. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  2574. if (!rc) {
  2575. pr_err("%s: timeout. waited for open write\n", __func__);
  2576. rc = -ETIMEDOUT;
  2577. goto fail_cmd;
  2578. }
  2579. if (atomic_read(&ac->cmd_state) > 0) {
  2580. pr_err("%s: DSP returned error[%s]\n",
  2581. __func__, adsp_err_get_err_str(
  2582. atomic_read(&ac->cmd_state)));
  2583. rc = adsp_err_get_lnx_err_code(
  2584. atomic_read(&ac->cmd_state));
  2585. goto fail_cmd;
  2586. }
  2587. ac->io_mode |= TUN_WRITE_IO_MODE;
  2588. return 0;
  2589. fail_cmd:
  2590. return rc;
  2591. }
  2592. int q6asm_open_write(struct audio_client *ac, uint32_t format)
  2593. {
  2594. return __q6asm_open_write(ac, format, 16, ac->stream_id,
  2595. false /*gapless*/,
  2596. PCM_MEDIA_FORMAT_V2 /*pcm_format_block_ver*/);
  2597. }
  2598. int q6asm_open_write_v2(struct audio_client *ac, uint32_t format,
  2599. uint16_t bits_per_sample)
  2600. {
  2601. return __q6asm_open_write(ac, format, bits_per_sample,
  2602. ac->stream_id, false /*gapless*/,
  2603. PCM_MEDIA_FORMAT_V2 /*pcm_format_block_ver*/);
  2604. }
  2605. /*
  2606. * q6asm_open_write_v3 - Opens audio playback session
  2607. *
  2608. * @ac: Client session handle
  2609. * @format: decoder format
  2610. * @bits_per_sample: bit width of playback session
  2611. */
  2612. int q6asm_open_write_v3(struct audio_client *ac, uint32_t format,
  2613. uint16_t bits_per_sample)
  2614. {
  2615. return __q6asm_open_write(ac, format, bits_per_sample,
  2616. ac->stream_id, false /*gapless*/,
  2617. PCM_MEDIA_FORMAT_V3 /*pcm_format_block_ver*/);
  2618. }
  2619. EXPORT_SYMBOL(q6asm_open_write_v3);
  2620. /*
  2621. * q6asm_open_write_v4 - Opens audio playback session
  2622. *
  2623. * @ac: Client session handle
  2624. * @format: decoder format
  2625. * @bits_per_sample: bit width of playback session
  2626. */
  2627. int q6asm_open_write_v4(struct audio_client *ac, uint32_t format,
  2628. uint16_t bits_per_sample)
  2629. {
  2630. return __q6asm_open_write(ac, format, bits_per_sample,
  2631. ac->stream_id, false /*gapless*/,
  2632. PCM_MEDIA_FORMAT_V4 /*pcm_format_block_ver*/);
  2633. }
  2634. EXPORT_SYMBOL(q6asm_open_write_v4);
  2635. int q6asm_stream_open_write_v2(struct audio_client *ac, uint32_t format,
  2636. uint16_t bits_per_sample, int32_t stream_id,
  2637. bool is_gapless_mode)
  2638. {
  2639. return __q6asm_open_write(ac, format, bits_per_sample,
  2640. stream_id, is_gapless_mode,
  2641. PCM_MEDIA_FORMAT_V2 /*pcm_format_block_ver*/);
  2642. }
  2643. /*
  2644. * q6asm_stream_open_write_v3 - Creates audio stream for playback
  2645. *
  2646. * @ac: Client session handle
  2647. * @format: asm playback format
  2648. * @bits_per_sample: bit width of requested stream
  2649. * @stream_id: stream id of stream to be associated with this session
  2650. * @is_gapless_mode: true if gapless mode needs to be enabled
  2651. */
  2652. int q6asm_stream_open_write_v3(struct audio_client *ac, uint32_t format,
  2653. uint16_t bits_per_sample, int32_t stream_id,
  2654. bool is_gapless_mode)
  2655. {
  2656. return __q6asm_open_write(ac, format, bits_per_sample,
  2657. stream_id, is_gapless_mode,
  2658. PCM_MEDIA_FORMAT_V3 /*pcm_format_block_ver*/);
  2659. }
  2660. EXPORT_SYMBOL(q6asm_stream_open_write_v3);
  2661. /*
  2662. * q6asm_stream_open_write_v4 - Creates audio stream for playback
  2663. *
  2664. * @ac: Client session handle
  2665. * @format: asm playback format
  2666. * @bits_per_sample: bit width of requested stream
  2667. * @stream_id: stream id of stream to be associated with this session
  2668. * @is_gapless_mode: true if gapless mode needs to be enabled
  2669. */
  2670. int q6asm_stream_open_write_v4(struct audio_client *ac, uint32_t format,
  2671. uint16_t bits_per_sample, int32_t stream_id,
  2672. bool is_gapless_mode)
  2673. {
  2674. return __q6asm_open_write(ac, format, bits_per_sample,
  2675. stream_id, is_gapless_mode,
  2676. PCM_MEDIA_FORMAT_V4 /*pcm_format_block_ver*/);
  2677. }
  2678. EXPORT_SYMBOL(q6asm_stream_open_write_v4);
  2679. static int __q6asm_open_read_write(struct audio_client *ac, uint32_t rd_format,
  2680. uint32_t wr_format, bool is_meta_data_mode,
  2681. uint32_t bits_per_sample,
  2682. bool overwrite_topology, int topology)
  2683. {
  2684. int rc = 0x00;
  2685. struct asm_stream_cmd_open_readwrite_v2 open;
  2686. if (ac == NULL) {
  2687. pr_err("%s: APR handle NULL\n", __func__);
  2688. return -EINVAL;
  2689. }
  2690. if (ac->apr == NULL) {
  2691. pr_err("%s: AC APR handle NULL\n", __func__);
  2692. return -EINVAL;
  2693. }
  2694. pr_debug("%s: session[%d]\n", __func__, ac->session);
  2695. pr_debug("%s: wr_format[0x%x]rd_format[0x%x]\n",
  2696. __func__, wr_format, rd_format);
  2697. ac->io_mode |= NT_MODE;
  2698. q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
  2699. atomic_set(&ac->cmd_state, -1);
  2700. open.hdr.opcode = ASM_STREAM_CMD_OPEN_READWRITE_V2;
  2701. open.mode_flags = is_meta_data_mode ? BUFFER_META_ENABLE : 0;
  2702. open.bits_per_sample = bits_per_sample;
  2703. /* source endpoint : matrix */
  2704. open.postprocopo_id = q6asm_get_asm_topology_cal();
  2705. open.postprocopo_id = overwrite_topology ?
  2706. topology : open.postprocopo_id;
  2707. ac->topology = open.postprocopo_id;
  2708. ac->app_type = q6asm_get_asm_app_type_cal();
  2709. switch (wr_format) {
  2710. case FORMAT_LINEAR_PCM:
  2711. case FORMAT_MULTI_CHANNEL_LINEAR_PCM:
  2712. open.dec_fmt_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V2;
  2713. break;
  2714. case FORMAT_MPEG4_AAC:
  2715. open.dec_fmt_id = ASM_MEDIA_FMT_AAC_V2;
  2716. break;
  2717. case FORMAT_MPEG4_MULTI_AAC:
  2718. open.dec_fmt_id = ASM_MEDIA_FMT_AAC_V2;
  2719. break;
  2720. case FORMAT_WMA_V9:
  2721. open.dec_fmt_id = ASM_MEDIA_FMT_WMA_V9_V2;
  2722. break;
  2723. case FORMAT_WMA_V10PRO:
  2724. open.dec_fmt_id = ASM_MEDIA_FMT_WMA_V10PRO_V2;
  2725. break;
  2726. case FORMAT_AMRNB:
  2727. open.dec_fmt_id = ASM_MEDIA_FMT_AMRNB_FS;
  2728. break;
  2729. case FORMAT_AMRWB:
  2730. open.dec_fmt_id = ASM_MEDIA_FMT_AMRWB_FS;
  2731. break;
  2732. case FORMAT_AMR_WB_PLUS:
  2733. open.dec_fmt_id = ASM_MEDIA_FMT_AMR_WB_PLUS_V2;
  2734. break;
  2735. case FORMAT_V13K:
  2736. open.dec_fmt_id = ASM_MEDIA_FMT_V13K_FS;
  2737. break;
  2738. case FORMAT_EVRC:
  2739. open.dec_fmt_id = ASM_MEDIA_FMT_EVRC_FS;
  2740. break;
  2741. case FORMAT_EVRCB:
  2742. open.dec_fmt_id = ASM_MEDIA_FMT_EVRCB_FS;
  2743. break;
  2744. case FORMAT_EVRCWB:
  2745. open.dec_fmt_id = ASM_MEDIA_FMT_EVRCWB_FS;
  2746. break;
  2747. case FORMAT_MP3:
  2748. open.dec_fmt_id = ASM_MEDIA_FMT_MP3;
  2749. break;
  2750. case FORMAT_ALAC:
  2751. open.dec_fmt_id = ASM_MEDIA_FMT_ALAC;
  2752. break;
  2753. case FORMAT_APE:
  2754. open.dec_fmt_id = ASM_MEDIA_FMT_APE;
  2755. break;
  2756. case FORMAT_DSD:
  2757. open.dec_fmt_id = ASM_MEDIA_FMT_DSD;
  2758. break;
  2759. case FORMAT_G711_ALAW_FS:
  2760. open.dec_fmt_id = ASM_MEDIA_FMT_G711_ALAW_FS;
  2761. break;
  2762. case FORMAT_G711_MLAW_FS:
  2763. open.dec_fmt_id = ASM_MEDIA_FMT_G711_MLAW_FS;
  2764. break;
  2765. default:
  2766. pr_err("%s: Invalid format 0x%x\n",
  2767. __func__, wr_format);
  2768. rc = -EINVAL;
  2769. goto fail_cmd;
  2770. }
  2771. switch (rd_format) {
  2772. case FORMAT_LINEAR_PCM:
  2773. case FORMAT_MULTI_CHANNEL_LINEAR_PCM:
  2774. open.enc_cfg_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V2;
  2775. break;
  2776. case FORMAT_MPEG4_AAC:
  2777. open.enc_cfg_id = ASM_MEDIA_FMT_AAC_V2;
  2778. break;
  2779. case FORMAT_G711_ALAW_FS:
  2780. open.enc_cfg_id = ASM_MEDIA_FMT_G711_ALAW_FS;
  2781. break;
  2782. case FORMAT_G711_MLAW_FS:
  2783. open.enc_cfg_id = ASM_MEDIA_FMT_G711_MLAW_FS;
  2784. break;
  2785. case FORMAT_V13K:
  2786. open.enc_cfg_id = ASM_MEDIA_FMT_V13K_FS;
  2787. break;
  2788. case FORMAT_EVRC:
  2789. open.enc_cfg_id = ASM_MEDIA_FMT_EVRC_FS;
  2790. break;
  2791. case FORMAT_AMRNB:
  2792. open.enc_cfg_id = ASM_MEDIA_FMT_AMRNB_FS;
  2793. break;
  2794. case FORMAT_AMRWB:
  2795. open.enc_cfg_id = ASM_MEDIA_FMT_AMRWB_FS;
  2796. break;
  2797. case FORMAT_ALAC:
  2798. open.enc_cfg_id = ASM_MEDIA_FMT_ALAC;
  2799. break;
  2800. case FORMAT_APE:
  2801. open.enc_cfg_id = ASM_MEDIA_FMT_APE;
  2802. break;
  2803. default:
  2804. pr_err("%s: Invalid format 0x%x\n",
  2805. __func__, rd_format);
  2806. rc = -EINVAL;
  2807. goto fail_cmd;
  2808. }
  2809. dev_vdbg(ac->dev, "%s: rdformat[0x%x]wrformat[0x%x]\n", __func__,
  2810. open.enc_cfg_id, open.dec_fmt_id);
  2811. rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
  2812. if (rc < 0) {
  2813. pr_err("%s: open failed op[0x%x]rc[%d]\n",
  2814. __func__, open.hdr.opcode, rc);
  2815. rc = -EINVAL;
  2816. goto fail_cmd;
  2817. }
  2818. rc = wait_event_timeout(ac->cmd_wait,
  2819. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  2820. if (!rc) {
  2821. pr_err("%s: timeout. waited for open read-write\n",
  2822. __func__);
  2823. rc = -ETIMEDOUT;
  2824. goto fail_cmd;
  2825. }
  2826. if (atomic_read(&ac->cmd_state) > 0) {
  2827. pr_err("%s: DSP returned error[%s]\n",
  2828. __func__, adsp_err_get_err_str(
  2829. atomic_read(&ac->cmd_state)));
  2830. rc = adsp_err_get_lnx_err_code(
  2831. atomic_read(&ac->cmd_state));
  2832. goto fail_cmd;
  2833. }
  2834. return 0;
  2835. fail_cmd:
  2836. return rc;
  2837. }
  2838. int q6asm_open_read_write(struct audio_client *ac, uint32_t rd_format,
  2839. uint32_t wr_format)
  2840. {
  2841. return __q6asm_open_read_write(ac, rd_format, wr_format,
  2842. true/*meta data mode*/,
  2843. 16 /*bits_per_sample*/,
  2844. false /*overwrite_topology*/, 0);
  2845. }
  2846. int q6asm_open_read_write_v2(struct audio_client *ac, uint32_t rd_format,
  2847. uint32_t wr_format, bool is_meta_data_mode,
  2848. uint32_t bits_per_sample, bool overwrite_topology,
  2849. int topology)
  2850. {
  2851. return __q6asm_open_read_write(ac, rd_format, wr_format,
  2852. is_meta_data_mode, bits_per_sample,
  2853. overwrite_topology, topology);
  2854. }
  2855. int q6asm_open_loopback_v2(struct audio_client *ac, uint16_t bits_per_sample)
  2856. {
  2857. int rc = 0x00;
  2858. if (ac == NULL) {
  2859. pr_err("%s: APR handle NULL\n", __func__);
  2860. return -EINVAL;
  2861. }
  2862. if (ac->apr == NULL) {
  2863. pr_err("%s: AC APR handle NULL\n", __func__);
  2864. return -EINVAL;
  2865. }
  2866. pr_debug("%s: session[%d]\n", __func__, ac->session);
  2867. if (ac->perf_mode == LOW_LATENCY_PCM_MODE) {
  2868. struct asm_stream_cmd_open_transcode_loopback_t open;
  2869. q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
  2870. atomic_set(&ac->cmd_state, -1);
  2871. open.hdr.opcode = ASM_STREAM_CMD_OPEN_TRANSCODE_LOOPBACK;
  2872. open.mode_flags = 0;
  2873. open.src_endpoint_type = 0;
  2874. open.sink_endpoint_type = 0;
  2875. open.src_format_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V2;
  2876. open.sink_format_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V2;
  2877. /* source endpoint : matrix */
  2878. open.audproc_topo_id = q6asm_get_asm_topology_cal();
  2879. ac->app_type = q6asm_get_asm_app_type_cal();
  2880. if (ac->perf_mode == LOW_LATENCY_PCM_MODE)
  2881. open.mode_flags |= ASM_LOW_LATENCY_STREAM_SESSION;
  2882. else
  2883. open.mode_flags |= ASM_LEGACY_STREAM_SESSION;
  2884. ac->topology = open.audproc_topo_id;
  2885. open.bits_per_sample = bits_per_sample;
  2886. open.reserved = 0;
  2887. pr_debug("%s: opening a transcode_loopback with mode_flags =[%d] session[%d]\n",
  2888. __func__, open.mode_flags, ac->session);
  2889. rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
  2890. if (rc < 0) {
  2891. pr_err("%s: open failed op[0x%x]rc[%d]\n",
  2892. __func__, open.hdr.opcode, rc);
  2893. rc = -EINVAL;
  2894. goto fail_cmd;
  2895. }
  2896. } else {/*if(ac->perf_mode == LEGACY_PCM_MODE)*/
  2897. struct asm_stream_cmd_open_loopback_v2 open;
  2898. q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
  2899. atomic_set(&ac->cmd_state, -1);
  2900. open.hdr.opcode = ASM_STREAM_CMD_OPEN_LOOPBACK_V2;
  2901. open.mode_flags = 0;
  2902. open.src_endpointype = 0;
  2903. open.sink_endpointype = 0;
  2904. /* source endpoint : matrix */
  2905. open.postprocopo_id = q6asm_get_asm_topology_cal();
  2906. ac->app_type = q6asm_get_asm_app_type_cal();
  2907. ac->topology = open.postprocopo_id;
  2908. open.bits_per_sample = bits_per_sample;
  2909. open.reserved = 0;
  2910. pr_debug("%s: opening a loopback_v2 with mode_flags =[%d] session[%d]\n",
  2911. __func__, open.mode_flags, ac->session);
  2912. rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
  2913. if (rc < 0) {
  2914. pr_err("%s: open failed op[0x%x]rc[%d]\n",
  2915. __func__, open.hdr.opcode, rc);
  2916. rc = -EINVAL;
  2917. goto fail_cmd;
  2918. }
  2919. }
  2920. rc = wait_event_timeout(ac->cmd_wait,
  2921. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  2922. if (!rc) {
  2923. pr_err("%s: timeout. waited for open_loopback\n",
  2924. __func__);
  2925. rc = -ETIMEDOUT;
  2926. goto fail_cmd;
  2927. }
  2928. if (atomic_read(&ac->cmd_state) > 0) {
  2929. pr_err("%s: DSP returned error[%s]\n",
  2930. __func__, adsp_err_get_err_str(
  2931. atomic_read(&ac->cmd_state)));
  2932. rc = adsp_err_get_lnx_err_code(
  2933. atomic_read(&ac->cmd_state));
  2934. goto fail_cmd;
  2935. }
  2936. return 0;
  2937. fail_cmd:
  2938. return rc;
  2939. }
  2940. int q6asm_open_transcode_loopback(struct audio_client *ac,
  2941. uint16_t bits_per_sample,
  2942. uint32_t source_format, uint32_t sink_format)
  2943. {
  2944. int rc = 0x00;
  2945. struct asm_stream_cmd_open_transcode_loopback_t open;
  2946. if (ac == NULL) {
  2947. pr_err("%s: APR handle NULL\n", __func__);
  2948. return -EINVAL;
  2949. }
  2950. if (ac->apr == NULL) {
  2951. pr_err("%s: AC APR handle NULL\n", __func__);
  2952. return -EINVAL;
  2953. }
  2954. pr_debug("%s: session[%d]\n", __func__, ac->session);
  2955. q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
  2956. atomic_set(&ac->cmd_state, -1);
  2957. open.hdr.opcode = ASM_STREAM_CMD_OPEN_TRANSCODE_LOOPBACK;
  2958. open.mode_flags = 0;
  2959. open.src_endpoint_type = 0;
  2960. open.sink_endpoint_type = 0;
  2961. switch (source_format) {
  2962. case FORMAT_LINEAR_PCM:
  2963. case FORMAT_MULTI_CHANNEL_LINEAR_PCM:
  2964. open.src_format_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V3;
  2965. break;
  2966. case FORMAT_AC3:
  2967. open.src_format_id = ASM_MEDIA_FMT_AC3;
  2968. break;
  2969. case FORMAT_EAC3:
  2970. open.src_format_id = ASM_MEDIA_FMT_EAC3;
  2971. break;
  2972. default:
  2973. pr_err("%s: Unsupported src fmt [%d]\n",
  2974. __func__, source_format);
  2975. return -EINVAL;
  2976. }
  2977. switch (sink_format) {
  2978. case FORMAT_LINEAR_PCM:
  2979. case FORMAT_MULTI_CHANNEL_LINEAR_PCM:
  2980. open.sink_format_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V3;
  2981. break;
  2982. default:
  2983. pr_err("%s: Unsupported sink fmt [%d]\n",
  2984. __func__, sink_format);
  2985. return -EINVAL;
  2986. }
  2987. /* source endpoint : matrix */
  2988. open.audproc_topo_id = q6asm_get_asm_topology_cal();
  2989. ac->app_type = q6asm_get_asm_app_type_cal();
  2990. if (ac->perf_mode == LOW_LATENCY_PCM_MODE)
  2991. open.mode_flags |= ASM_LOW_LATENCY_STREAM_SESSION;
  2992. else
  2993. open.mode_flags |= ASM_LEGACY_STREAM_SESSION;
  2994. ac->topology = open.audproc_topo_id;
  2995. open.bits_per_sample = bits_per_sample;
  2996. open.reserved = 0;
  2997. pr_debug("%s: opening a transcode_loopback with mode_flags =[%d] session[%d]\n",
  2998. __func__, open.mode_flags, ac->session);
  2999. rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
  3000. if (rc < 0) {
  3001. pr_err("%s: open failed op[0x%x]rc[%d]\n",
  3002. __func__, open.hdr.opcode, rc);
  3003. rc = -EINVAL;
  3004. goto fail_cmd;
  3005. }
  3006. rc = wait_event_timeout(ac->cmd_wait,
  3007. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  3008. if (!rc) {
  3009. pr_err("%s: timeout. waited for open_transcode_loopback\n",
  3010. __func__);
  3011. rc = -ETIMEDOUT;
  3012. goto fail_cmd;
  3013. }
  3014. if (atomic_read(&ac->cmd_state) > 0) {
  3015. pr_err("%s: DSP returned error[%s]\n",
  3016. __func__, adsp_err_get_err_str(
  3017. atomic_read(&ac->cmd_state)));
  3018. rc = adsp_err_get_lnx_err_code(
  3019. atomic_read(&ac->cmd_state));
  3020. goto fail_cmd;
  3021. }
  3022. return 0;
  3023. fail_cmd:
  3024. return rc;
  3025. }
  3026. static
  3027. int q6asm_set_shared_circ_buff(struct audio_client *ac,
  3028. struct asm_stream_cmd_open_shared_io *open,
  3029. int bufsz, int bufcnt,
  3030. int dir)
  3031. {
  3032. struct audio_buffer *buf_circ;
  3033. int bytes_to_alloc, rc;
  3034. size_t len;
  3035. buf_circ = kzalloc(sizeof(struct audio_buffer), GFP_KERNEL);
  3036. if (!buf_circ) {
  3037. rc = -ENOMEM;
  3038. goto done;
  3039. }
  3040. mutex_lock(&ac->cmd_lock);
  3041. ac->port[dir].buf = buf_circ;
  3042. bytes_to_alloc = bufsz * bufcnt;
  3043. bytes_to_alloc = PAGE_ALIGN(bytes_to_alloc);
  3044. rc = msm_audio_ion_alloc("audio_client", &buf_circ->client,
  3045. &buf_circ->handle, bytes_to_alloc,
  3046. (ion_phys_addr_t *)&buf_circ->phys,
  3047. &len, &buf_circ->data);
  3048. if (rc) {
  3049. pr_err("%s: Audio ION alloc is failed, rc = %d\n", __func__,
  3050. rc);
  3051. mutex_unlock(&ac->cmd_lock);
  3052. kfree(buf_circ);
  3053. goto done;
  3054. }
  3055. buf_circ->used = dir ^ 1;
  3056. buf_circ->size = bytes_to_alloc;
  3057. buf_circ->actual_size = bytes_to_alloc;
  3058. memset(buf_circ->data, 0, buf_circ->actual_size);
  3059. ac->port[dir].max_buf_cnt = 1;
  3060. open->shared_circ_buf_mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
  3061. open->shared_circ_buf_num_regions = 1;
  3062. open->shared_circ_buf_property_flag = 0x00;
  3063. open->shared_circ_buf_start_phy_addr_lsw =
  3064. lower_32_bits(buf_circ->phys);
  3065. open->shared_circ_buf_start_phy_addr_msw =
  3066. msm_audio_populate_upper_32_bits(buf_circ->phys);
  3067. open->shared_circ_buf_size = bufsz * bufcnt;
  3068. open->map_region_circ_buf.shm_addr_lsw = lower_32_bits(buf_circ->phys);
  3069. open->map_region_circ_buf.shm_addr_msw =
  3070. msm_audio_populate_upper_32_bits(buf_circ->phys);
  3071. open->map_region_circ_buf.mem_size_bytes = bytes_to_alloc;
  3072. mutex_unlock(&ac->cmd_lock);
  3073. done:
  3074. return rc;
  3075. }
  3076. static
  3077. int q6asm_set_shared_pos_buff(struct audio_client *ac,
  3078. struct asm_stream_cmd_open_shared_io *open,
  3079. int dir)
  3080. {
  3081. struct audio_buffer *buf_pos = &ac->shared_pos_buf;
  3082. int rc;
  3083. size_t len;
  3084. int bytes_to_alloc = sizeof(struct asm_shared_position_buffer);
  3085. mutex_lock(&ac->cmd_lock);
  3086. bytes_to_alloc = PAGE_ALIGN(bytes_to_alloc);
  3087. rc = msm_audio_ion_alloc("audio_client", &buf_pos->client,
  3088. &buf_pos->handle, bytes_to_alloc,
  3089. (ion_phys_addr_t *)&buf_pos->phys, &len,
  3090. &buf_pos->data);
  3091. if (rc) {
  3092. pr_err("%s: Audio pos buf ION alloc is failed, rc = %d\n",
  3093. __func__, rc);
  3094. goto done;
  3095. }
  3096. buf_pos->used = dir ^ 1;
  3097. buf_pos->size = bytes_to_alloc;
  3098. buf_pos->actual_size = bytes_to_alloc;
  3099. open->shared_pos_buf_mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
  3100. open->shared_pos_buf_num_regions = 1;
  3101. open->shared_pos_buf_property_flag = 0x00;
  3102. open->shared_pos_buf_phy_addr_lsw = lower_32_bits(buf_pos->phys);
  3103. open->shared_pos_buf_phy_addr_msw =
  3104. msm_audio_populate_upper_32_bits(buf_pos->phys);
  3105. open->map_region_pos_buf.shm_addr_lsw = lower_32_bits(buf_pos->phys);
  3106. open->map_region_pos_buf.shm_addr_msw =
  3107. msm_audio_populate_upper_32_bits(buf_pos->phys);
  3108. open->map_region_pos_buf.mem_size_bytes = bytes_to_alloc;
  3109. done:
  3110. mutex_unlock(&ac->cmd_lock);
  3111. return rc;
  3112. }
  3113. /*
  3114. * q6asm_open_shared_io: Open an ASM session for pull mode (playback)
  3115. * or push mode (capture).
  3116. * parameters
  3117. * config - session parameters (channels, bits_per_sample, sr)
  3118. * dir - stream direction (IN for playback, OUT for capture)
  3119. * returns 0 if successful, error code otherwise
  3120. */
  3121. int q6asm_open_shared_io(struct audio_client *ac,
  3122. struct shared_io_config *config,
  3123. int dir)
  3124. {
  3125. struct asm_stream_cmd_open_shared_io *open;
  3126. u8 *channel_mapping;
  3127. int i, size_of_open, num_watermarks, bufsz, bufcnt, rc, flags = 0;
  3128. if (!ac || !config)
  3129. return -EINVAL;
  3130. bufsz = config->bufsz;
  3131. bufcnt = config->bufcnt;
  3132. num_watermarks = 0;
  3133. ac->config = *config;
  3134. if (ac->session <= 0 || ac->session > SESSION_MAX) {
  3135. pr_err("%s: Session %d is out of bounds\n",
  3136. __func__, ac->session);
  3137. return -EINVAL;
  3138. }
  3139. size_of_open = sizeof(struct asm_stream_cmd_open_shared_io) +
  3140. (sizeof(struct asm_shared_watermark_level) * num_watermarks);
  3141. open = kzalloc(PAGE_ALIGN(size_of_open), GFP_KERNEL);
  3142. if (!open)
  3143. return -ENOMEM;
  3144. q6asm_stream_add_hdr(ac, &open->hdr, size_of_open, TRUE,
  3145. ac->stream_id);
  3146. atomic_set(&ac->cmd_state, 1);
  3147. pr_debug("%s: token = 0x%x, stream_id %d, session 0x%x, perf %d\n",
  3148. __func__, open->hdr.token, ac->stream_id, ac->session,
  3149. ac->perf_mode);
  3150. open->hdr.opcode =
  3151. dir == IN ? ASM_STREAM_CMD_OPEN_PULL_MODE_WRITE :
  3152. ASM_STREAM_CMD_OPEN_PUSH_MODE_READ;
  3153. pr_debug("%s perf_mode %d\n", __func__, ac->perf_mode);
  3154. if (dir == IN)
  3155. if (ac->perf_mode == ULL_POST_PROCESSING_PCM_MODE)
  3156. flags = 4 << ASM_SHIFT_STREAM_PERF_FLAG_PULL_MODE_WRITE;
  3157. else if (ac->perf_mode == ULTRA_LOW_LATENCY_PCM_MODE)
  3158. flags = 2 << ASM_SHIFT_STREAM_PERF_FLAG_PULL_MODE_WRITE;
  3159. else if (ac->perf_mode == LOW_LATENCY_PCM_MODE)
  3160. flags = 1 << ASM_SHIFT_STREAM_PERF_FLAG_PULL_MODE_WRITE;
  3161. else
  3162. pr_err("Invalid perf mode for pull write\n");
  3163. else
  3164. if (ac->perf_mode == LOW_LATENCY_PCM_MODE)
  3165. flags = ASM_LOW_LATENCY_TX_STREAM_SESSION <<
  3166. ASM_SHIFT_STREAM_PERF_FLAG_PUSH_MODE_READ;
  3167. else
  3168. pr_err("Invalid perf mode for push read\n");
  3169. if (flags == 0) {
  3170. pr_err("%s: Invalid mode[%d]\n", __func__,
  3171. ac->perf_mode);
  3172. kfree(open);
  3173. return -EINVAL;
  3174. }
  3175. pr_debug("open.mode_flags = 0x%x\n", flags);
  3176. open->mode_flags = flags;
  3177. open->endpoint_type = ASM_END_POINT_DEVICE_MATRIX;
  3178. open->topo_bits_per_sample = config->bits_per_sample;
  3179. open->topo_id = q6asm_get_asm_topology_cal();
  3180. if (config->format == FORMAT_LINEAR_PCM)
  3181. open->fmt_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V3;
  3182. else {
  3183. pr_err("%s: Invalid format[%d]\n", __func__, config->format);
  3184. rc = -EINVAL;
  3185. goto done;
  3186. }
  3187. if (ac->port[dir].buf) {
  3188. pr_err("%s: Buffer already allocated\n", __func__);
  3189. rc = -EINVAL;
  3190. goto done;
  3191. }
  3192. rc = q6asm_set_shared_circ_buff(ac, open, bufsz, bufcnt, dir);
  3193. if (rc)
  3194. goto done;
  3195. ac->port[dir].tmp_hdl = 0;
  3196. rc = q6asm_set_shared_pos_buff(ac, open, dir);
  3197. if (rc)
  3198. goto done;
  3199. /* asm_multi_channel_pcm_fmt_blk_v3 */
  3200. open->fmt.num_channels = config->channels;
  3201. open->fmt.bits_per_sample = config->bits_per_sample;
  3202. open->fmt.sample_rate = config->rate;
  3203. open->fmt.is_signed = 1;
  3204. open->fmt.sample_word_size = config->sample_word_size;
  3205. channel_mapping = open->fmt.channel_mapping;
  3206. memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  3207. rc = q6asm_map_channels(channel_mapping, config->channels, false);
  3208. if (rc) {
  3209. pr_err("%s: Map channels failed, ret: %d\n", __func__, rc);
  3210. goto done;
  3211. }
  3212. open->num_watermark_levels = num_watermarks;
  3213. for (i = 0; i < num_watermarks; i++) {
  3214. open->watermark[i].watermark_level_bytes = i *
  3215. ((bufsz * bufcnt) / num_watermarks);
  3216. pr_debug("%s: Watermark level set for %i\n",
  3217. __func__,
  3218. open->watermark[i].watermark_level_bytes);
  3219. }
  3220. rc = apr_send_pkt(ac->apr, (uint32_t *) open);
  3221. if (rc < 0) {
  3222. pr_err("%s: Open failed op[0x%x]rc[%d]\n",
  3223. __func__, open->hdr.opcode, rc);
  3224. goto done;
  3225. }
  3226. pr_debug("%s: sent open apr pkt\n", __func__);
  3227. rc = wait_event_timeout(ac->cmd_wait,
  3228. (atomic_read(&ac->cmd_state) <= 0), 5*HZ);
  3229. if (!rc) {
  3230. pr_err("%s: Timeout. Waited for open write apr pkt rc[%d]\n",
  3231. __func__, rc);
  3232. rc = -ETIMEDOUT;
  3233. goto done;
  3234. }
  3235. if (atomic_read(&ac->cmd_state) < 0) {
  3236. pr_err("%s: DSP returned error [%d]\n", __func__,
  3237. atomic_read(&ac->cmd_state));
  3238. rc = -EINVAL;
  3239. goto done;
  3240. }
  3241. ac->io_mode |= TUN_WRITE_IO_MODE;
  3242. rc = 0;
  3243. done:
  3244. kfree(open);
  3245. return rc;
  3246. }
  3247. EXPORT_SYMBOL(q6asm_open_shared_io);
  3248. /*
  3249. * q6asm_shared_io_buf: Returns handle to the shared circular buffer being
  3250. * used for pull/push mode.
  3251. * parameters
  3252. * dir - used to identify input/output port
  3253. * returns buffer handle
  3254. */
  3255. struct audio_buffer *q6asm_shared_io_buf(struct audio_client *ac,
  3256. int dir)
  3257. {
  3258. struct audio_port_data *port;
  3259. if (!ac) {
  3260. pr_err("%s: ac is null\n", __func__);
  3261. return NULL;
  3262. }
  3263. port = &ac->port[dir];
  3264. return port->buf;
  3265. }
  3266. EXPORT_SYMBOL(q6asm_shared_io_buf);
  3267. /*
  3268. * q6asm_shared_io_free: Frees memory allocated for a pull/push session
  3269. * parameters
  3270. * dir - port direction
  3271. * returns 0 if successful, error otherwise
  3272. */
  3273. int q6asm_shared_io_free(struct audio_client *ac, int dir)
  3274. {
  3275. struct audio_port_data *port;
  3276. if (!ac) {
  3277. pr_err("%s: audio client is null\n", __func__);
  3278. return -EINVAL;
  3279. }
  3280. port = &ac->port[dir];
  3281. mutex_lock(&ac->cmd_lock);
  3282. if (port->buf && port->buf->data) {
  3283. msm_audio_ion_free(port->buf->client, port->buf->handle);
  3284. port->buf->client = NULL;
  3285. port->buf->handle = NULL;
  3286. port->max_buf_cnt = 0;
  3287. kfree(port->buf);
  3288. port->buf = NULL;
  3289. }
  3290. if (ac->shared_pos_buf.data) {
  3291. msm_audio_ion_free(ac->shared_pos_buf.client,
  3292. ac->shared_pos_buf.handle);
  3293. ac->shared_pos_buf.client = NULL;
  3294. ac->shared_pos_buf.handle = NULL;
  3295. }
  3296. mutex_unlock(&ac->cmd_lock);
  3297. return 0;
  3298. }
  3299. EXPORT_SYMBOL(q6asm_shared_io_free);
  3300. /*
  3301. * q6asm_get_shared_pos: Returns current read index/write index as observed
  3302. * by the DSP. Note that this is an offset and iterates from [0,BUF_SIZE - 1]
  3303. * parameters - (all output)
  3304. * read_index - offset
  3305. * wall_clk_msw1 - ADSP wallclock msw
  3306. * wall_clk_lsw1 - ADSP wallclock lsw
  3307. * returns 0 if successful, -EAGAIN if DSP failed to update after some
  3308. * retries
  3309. */
  3310. int q6asm_get_shared_pos(struct audio_client *ac, uint32_t *read_index,
  3311. uint32_t *wall_clk_msw1, uint32_t *wall_clk_lsw1)
  3312. {
  3313. struct asm_shared_position_buffer *pos_buf;
  3314. uint32_t frame_cnt1, frame_cnt2;
  3315. int i, j;
  3316. if (!ac) {
  3317. pr_err("%s: audio client is null\n", __func__);
  3318. return -EINVAL;
  3319. }
  3320. pos_buf = ac->shared_pos_buf.data;
  3321. /* always try to get the latest update in the shared pos buffer */
  3322. for (i = 0; i < 2; i++) {
  3323. /* retry until there is an update from DSP */
  3324. for (j = 0; j < 5; j++) {
  3325. frame_cnt1 = pos_buf->frame_counter;
  3326. if (frame_cnt1 != 0)
  3327. break;
  3328. }
  3329. *wall_clk_msw1 = pos_buf->wall_clock_us_msw;
  3330. *wall_clk_lsw1 = pos_buf->wall_clock_us_lsw;
  3331. *read_index = pos_buf->index;
  3332. frame_cnt2 = pos_buf->frame_counter;
  3333. if (frame_cnt1 != frame_cnt2)
  3334. continue;
  3335. return 0;
  3336. }
  3337. pr_err("%s out of tries trying to get a good read, try again\n",
  3338. __func__);
  3339. return -EAGAIN;
  3340. }
  3341. int q6asm_run(struct audio_client *ac, uint32_t flags,
  3342. uint32_t msw_ts, uint32_t lsw_ts)
  3343. {
  3344. struct asm_session_cmd_run_v2 run;
  3345. int rc;
  3346. if (ac == NULL) {
  3347. pr_err("%s: APR handle NULL\n", __func__);
  3348. return -EINVAL;
  3349. }
  3350. if (ac->apr == NULL) {
  3351. pr_err("%s: AC APR handle NULL\n", __func__);
  3352. return -EINVAL;
  3353. }
  3354. pr_debug("%s: session[%d]\n", __func__, ac->session);
  3355. q6asm_add_hdr(ac, &run.hdr, sizeof(run), TRUE);
  3356. atomic_set(&ac->cmd_state, -1);
  3357. run.hdr.opcode = ASM_SESSION_CMD_RUN_V2;
  3358. run.flags = flags;
  3359. run.time_lsw = lsw_ts;
  3360. run.time_msw = msw_ts;
  3361. config_debug_fs_run();
  3362. rc = apr_send_pkt(ac->apr, (uint32_t *) &run);
  3363. if (rc < 0) {
  3364. pr_err("%s: Commmand run failed[%d]",
  3365. __func__, rc);
  3366. rc = -EINVAL;
  3367. goto fail_cmd;
  3368. }
  3369. rc = wait_event_timeout(ac->cmd_wait,
  3370. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  3371. if (!rc) {
  3372. pr_err("%s: timeout. waited for run success",
  3373. __func__);
  3374. rc = -ETIMEDOUT;
  3375. goto fail_cmd;
  3376. }
  3377. if (atomic_read(&ac->cmd_state) > 0) {
  3378. pr_err("%s: DSP returned error[%s]\n",
  3379. __func__, adsp_err_get_err_str(
  3380. atomic_read(&ac->cmd_state)));
  3381. rc = adsp_err_get_lnx_err_code(
  3382. atomic_read(&ac->cmd_state));
  3383. goto fail_cmd;
  3384. }
  3385. return 0;
  3386. fail_cmd:
  3387. return rc;
  3388. }
  3389. static int __q6asm_run_nowait(struct audio_client *ac, uint32_t flags,
  3390. uint32_t msw_ts, uint32_t lsw_ts, uint32_t stream_id)
  3391. {
  3392. struct asm_session_cmd_run_v2 run;
  3393. int rc;
  3394. if (ac == NULL) {
  3395. pr_err("%s: APR handle NULL\n", __func__);
  3396. return -EINVAL;
  3397. }
  3398. if (ac->apr == NULL) {
  3399. pr_err("%s: AC APR handle NULL\n", __func__);
  3400. return -EINVAL;
  3401. }
  3402. pr_debug("%s: session[%d]\n", __func__, ac->session);
  3403. q6asm_stream_add_hdr_async(ac, &run.hdr, sizeof(run), TRUE, stream_id);
  3404. atomic_set(&ac->cmd_state, 1);
  3405. run.hdr.opcode = ASM_SESSION_CMD_RUN_V2;
  3406. run.flags = flags;
  3407. run.time_lsw = lsw_ts;
  3408. run.time_msw = msw_ts;
  3409. rc = apr_send_pkt(ac->apr, (uint32_t *) &run);
  3410. if (rc < 0) {
  3411. pr_err("%s: Commmand run failed[%d]", __func__, rc);
  3412. return -EINVAL;
  3413. }
  3414. return 0;
  3415. }
  3416. int q6asm_run_nowait(struct audio_client *ac, uint32_t flags,
  3417. uint32_t msw_ts, uint32_t lsw_ts)
  3418. {
  3419. return __q6asm_run_nowait(ac, flags, msw_ts, lsw_ts, ac->stream_id);
  3420. }
  3421. int q6asm_stream_run_nowait(struct audio_client *ac, uint32_t flags,
  3422. uint32_t msw_ts, uint32_t lsw_ts, uint32_t stream_id)
  3423. {
  3424. return __q6asm_run_nowait(ac, flags, msw_ts, lsw_ts, stream_id);
  3425. }
  3426. int q6asm_enc_cfg_blk_aac(struct audio_client *ac,
  3427. uint32_t frames_per_buf,
  3428. uint32_t sample_rate, uint32_t channels,
  3429. uint32_t bit_rate, uint32_t mode, uint32_t format)
  3430. {
  3431. struct asm_aac_enc_cfg_v2 enc_cfg;
  3432. int rc = 0;
  3433. pr_debug("%s: session[%d]frames[%d]SR[%d]ch[%d]bitrate[%d]mode[%d] format[%d]\n",
  3434. __func__, ac->session, frames_per_buf,
  3435. sample_rate, channels, bit_rate, mode, format);
  3436. q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
  3437. atomic_set(&ac->cmd_state, -1);
  3438. enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  3439. enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
  3440. enc_cfg.encdec.param_size = sizeof(struct asm_aac_enc_cfg_v2) -
  3441. sizeof(struct asm_stream_cmd_set_encdec_param);
  3442. enc_cfg.encblk.frames_per_buf = frames_per_buf;
  3443. enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
  3444. sizeof(struct asm_enc_cfg_blk_param_v2);
  3445. enc_cfg.bit_rate = bit_rate;
  3446. enc_cfg.enc_mode = mode;
  3447. enc_cfg.aac_fmt_flag = format;
  3448. enc_cfg.channel_cfg = channels;
  3449. enc_cfg.sample_rate = sample_rate;
  3450. rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
  3451. if (rc < 0) {
  3452. pr_err("%s: Comamnd %d failed %d\n",
  3453. __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM, rc);
  3454. rc = -EINVAL;
  3455. goto fail_cmd;
  3456. }
  3457. rc = wait_event_timeout(ac->cmd_wait,
  3458. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  3459. if (!rc) {
  3460. pr_err("%s: timeout. waited for FORMAT_UPDATE\n",
  3461. __func__);
  3462. rc = -ETIMEDOUT;
  3463. goto fail_cmd;
  3464. }
  3465. if (atomic_read(&ac->cmd_state) > 0) {
  3466. pr_err("%s: DSP returned error[%s]\n",
  3467. __func__, adsp_err_get_err_str(
  3468. atomic_read(&ac->cmd_state)));
  3469. rc = adsp_err_get_lnx_err_code(
  3470. atomic_read(&ac->cmd_state));
  3471. goto fail_cmd;
  3472. }
  3473. return 0;
  3474. fail_cmd:
  3475. return rc;
  3476. }
  3477. int q6asm_enc_cfg_blk_g711(struct audio_client *ac,
  3478. uint32_t frames_per_buf,
  3479. uint32_t sample_rate)
  3480. {
  3481. struct asm_g711_enc_cfg_v2 enc_cfg;
  3482. int rc = 0;
  3483. pr_debug("%s: session[%d]frames[%d]SR[%d]\n",
  3484. __func__, ac->session, frames_per_buf,
  3485. sample_rate);
  3486. q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
  3487. atomic_set(&ac->cmd_state, -1);
  3488. enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  3489. enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
  3490. enc_cfg.encdec.param_size = sizeof(struct asm_g711_enc_cfg_v2) -
  3491. sizeof(struct asm_stream_cmd_set_encdec_param);
  3492. enc_cfg.encblk.frames_per_buf = frames_per_buf;
  3493. enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
  3494. sizeof(struct asm_enc_cfg_blk_param_v2);
  3495. enc_cfg.sample_rate = sample_rate;
  3496. rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
  3497. if (rc < 0) {
  3498. pr_err("%s: Comamnd %d failed %d\n",
  3499. __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM, rc);
  3500. rc = -EINVAL;
  3501. goto fail_cmd;
  3502. }
  3503. rc = wait_event_timeout(ac->cmd_wait,
  3504. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  3505. if (!rc) {
  3506. pr_err("%s: timeout. waited for FORMAT_UPDATE\n",
  3507. __func__);
  3508. rc = -ETIMEDOUT;
  3509. goto fail_cmd;
  3510. }
  3511. if (atomic_read(&ac->cmd_state) > 0) {
  3512. pr_err("%s: DSP returned error[%s]\n",
  3513. __func__, adsp_err_get_err_str(
  3514. atomic_read(&ac->cmd_state)));
  3515. rc = adsp_err_get_lnx_err_code(
  3516. atomic_read(&ac->cmd_state));
  3517. goto fail_cmd;
  3518. }
  3519. return 0;
  3520. fail_cmd:
  3521. return rc;
  3522. }
  3523. int q6asm_set_encdec_chan_map(struct audio_client *ac,
  3524. uint32_t num_channels)
  3525. {
  3526. struct asm_dec_out_chan_map_param chan_map;
  3527. u8 *channel_mapping;
  3528. int rc = 0;
  3529. pr_debug("%s: Session %d, num_channels = %d\n",
  3530. __func__, ac->session, num_channels);
  3531. q6asm_add_hdr(ac, &chan_map.hdr, sizeof(chan_map), TRUE);
  3532. atomic_set(&ac->cmd_state, -1);
  3533. chan_map.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  3534. chan_map.encdec.param_id = ASM_PARAM_ID_DEC_OUTPUT_CHAN_MAP;
  3535. chan_map.encdec.param_size = sizeof(struct asm_dec_out_chan_map_param) -
  3536. (sizeof(struct apr_hdr) +
  3537. sizeof(struct asm_stream_cmd_set_encdec_param));
  3538. chan_map.num_channels = num_channels;
  3539. channel_mapping = chan_map.channel_mapping;
  3540. memset(channel_mapping, PCM_CHANNEL_NULL, MAX_CHAN_MAP_CHANNELS);
  3541. if (q6asm_map_channels(channel_mapping, num_channels, false)) {
  3542. pr_err("%s: map channels failed %d\n", __func__, num_channels);
  3543. return -EINVAL;
  3544. }
  3545. rc = apr_send_pkt(ac->apr, (uint32_t *) &chan_map);
  3546. if (rc < 0) {
  3547. pr_err("%s: Command opcode[0x%x]paramid[0x%x] failed %d\n",
  3548. __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM,
  3549. ASM_PARAM_ID_DEC_OUTPUT_CHAN_MAP, rc);
  3550. goto fail_cmd;
  3551. }
  3552. rc = wait_event_timeout(ac->cmd_wait,
  3553. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  3554. if (!rc) {
  3555. pr_err("%s: timeout opcode[0x%x]\n", __func__,
  3556. chan_map.hdr.opcode);
  3557. rc = -ETIMEDOUT;
  3558. goto fail_cmd;
  3559. }
  3560. if (atomic_read(&ac->cmd_state) > 0) {
  3561. pr_err("%s: DSP returned error[%s]\n",
  3562. __func__, adsp_err_get_err_str(
  3563. atomic_read(&ac->cmd_state)));
  3564. rc = adsp_err_get_lnx_err_code(
  3565. atomic_read(&ac->cmd_state));
  3566. goto fail_cmd;
  3567. }
  3568. return 0;
  3569. fail_cmd:
  3570. return rc;
  3571. }
  3572. /*
  3573. * q6asm_enc_cfg_blk_pcm_v4 - sends encoder configuration parameters
  3574. *
  3575. * @ac: Client session handle
  3576. * @rate: sample rate
  3577. * @channels: number of channels
  3578. * @bits_per_sample: bit width of encoder session
  3579. * @use_default_chmap: true if default channel map to be used
  3580. * @use_back_flavor: to configure back left and right channel
  3581. * @channel_map: input channel map
  3582. * @sample_word_size: Size in bits of the word that holds a sample of a channel
  3583. * @endianness: endianness of the pcm data
  3584. * @mode: Mode to provide additional info about the pcm input data
  3585. */
  3586. int q6asm_enc_cfg_blk_pcm_v4(struct audio_client *ac,
  3587. uint32_t rate, uint32_t channels,
  3588. uint16_t bits_per_sample, bool use_default_chmap,
  3589. bool use_back_flavor, u8 *channel_map,
  3590. uint16_t sample_word_size, uint16_t endianness,
  3591. uint16_t mode)
  3592. {
  3593. struct asm_multi_channel_pcm_enc_cfg_v4 enc_cfg;
  3594. struct asm_enc_cfg_blk_param_v2 enc_fg_blk;
  3595. u8 *channel_mapping;
  3596. u32 frames_per_buf = 0;
  3597. int rc;
  3598. if (!use_default_chmap && (channel_map == NULL)) {
  3599. pr_err("%s: No valid chan map and can't use default\n",
  3600. __func__);
  3601. rc = -EINVAL;
  3602. goto fail_cmd;
  3603. }
  3604. pr_debug("%s: session[%d]rate[%d]ch[%d]bps[%d]wordsize[%d]\n", __func__,
  3605. ac->session, rate, channels,
  3606. bits_per_sample, sample_word_size);
  3607. memset(&enc_cfg, 0, sizeof(enc_cfg));
  3608. q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
  3609. atomic_set(&ac->cmd_state, -1);
  3610. enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  3611. enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
  3612. enc_cfg.encdec.param_size = sizeof(enc_cfg) - sizeof(enc_cfg.hdr) -
  3613. sizeof(enc_cfg.encdec);
  3614. enc_cfg.encblk.frames_per_buf = frames_per_buf;
  3615. enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
  3616. sizeof(enc_fg_blk);
  3617. enc_cfg.num_channels = channels;
  3618. enc_cfg.bits_per_sample = bits_per_sample;
  3619. enc_cfg.sample_rate = rate;
  3620. enc_cfg.is_signed = 1;
  3621. enc_cfg.sample_word_size = sample_word_size;
  3622. enc_cfg.endianness = endianness;
  3623. enc_cfg.mode = mode;
  3624. channel_mapping = enc_cfg.channel_mapping;
  3625. memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  3626. if (use_default_chmap) {
  3627. pr_debug("%s: setting default channel map for %d channels",
  3628. __func__, channels);
  3629. if (q6asm_map_channels(channel_mapping, channels,
  3630. use_back_flavor)) {
  3631. pr_err("%s: map channels failed %d\n",
  3632. __func__, channels);
  3633. rc = -EINVAL;
  3634. goto fail_cmd;
  3635. }
  3636. } else {
  3637. pr_debug("%s: Using pre-defined channel map", __func__);
  3638. memcpy(channel_mapping, channel_map,
  3639. PCM_FORMAT_MAX_NUM_CHANNEL);
  3640. }
  3641. rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
  3642. if (rc < 0) {
  3643. pr_err("%s: Command open failed %d\n", __func__, rc);
  3644. goto fail_cmd;
  3645. }
  3646. rc = wait_event_timeout(ac->cmd_wait,
  3647. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  3648. if (!rc) {
  3649. pr_err("%s: timeout opcode[0x%x]\n",
  3650. __func__, enc_cfg.hdr.opcode);
  3651. rc = -ETIMEDOUT;
  3652. goto fail_cmd;
  3653. }
  3654. if (atomic_read(&ac->cmd_state) > 0) {
  3655. pr_err("%s: DSP returned error[%s]\n",
  3656. __func__, adsp_err_get_err_str(
  3657. atomic_read(&ac->cmd_state)));
  3658. rc = adsp_err_get_lnx_err_code(
  3659. atomic_read(&ac->cmd_state));
  3660. goto fail_cmd;
  3661. }
  3662. return 0;
  3663. fail_cmd:
  3664. return rc;
  3665. }
  3666. EXPORT_SYMBOL(q6asm_enc_cfg_blk_pcm_v4);
  3667. /*
  3668. * q6asm_enc_cfg_blk_pcm_v3 - sends encoder configuration parameters
  3669. *
  3670. * @ac: Client session handle
  3671. * @rate: sample rate
  3672. * @channels: number of channels
  3673. * @bits_per_sample: bit width of encoder session
  3674. * @use_default_chmap: true if default channel map to be used
  3675. * @use_back_flavor: to configure back left and right channel
  3676. * @channel_map: input channel map
  3677. * @sample_word_size: Size in bits of the word that holds a sample of a channel
  3678. */
  3679. int q6asm_enc_cfg_blk_pcm_v3(struct audio_client *ac,
  3680. uint32_t rate, uint32_t channels,
  3681. uint16_t bits_per_sample, bool use_default_chmap,
  3682. bool use_back_flavor, u8 *channel_map,
  3683. uint16_t sample_word_size)
  3684. {
  3685. struct asm_multi_channel_pcm_enc_cfg_v3 enc_cfg;
  3686. struct asm_enc_cfg_blk_param_v2 enc_fg_blk;
  3687. u8 *channel_mapping;
  3688. u32 frames_per_buf = 0;
  3689. int rc;
  3690. if (!use_default_chmap && (channel_map == NULL)) {
  3691. pr_err("%s: No valid chan map and can't use default\n",
  3692. __func__);
  3693. rc = -EINVAL;
  3694. goto fail_cmd;
  3695. }
  3696. pr_debug("%s: session[%d]rate[%d]ch[%d]bps[%d]wordsize[%d]\n", __func__,
  3697. ac->session, rate, channels,
  3698. bits_per_sample, sample_word_size);
  3699. memset(&enc_cfg, 0, sizeof(enc_cfg));
  3700. q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
  3701. atomic_set(&ac->cmd_state, -1);
  3702. enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  3703. enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
  3704. enc_cfg.encdec.param_size = sizeof(enc_cfg) - sizeof(enc_cfg.hdr) -
  3705. sizeof(enc_cfg.encdec);
  3706. enc_cfg.encblk.frames_per_buf = frames_per_buf;
  3707. enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
  3708. sizeof(enc_fg_blk);
  3709. enc_cfg.num_channels = channels;
  3710. enc_cfg.bits_per_sample = bits_per_sample;
  3711. enc_cfg.sample_rate = rate;
  3712. enc_cfg.is_signed = 1;
  3713. enc_cfg.sample_word_size = sample_word_size;
  3714. channel_mapping = enc_cfg.channel_mapping;
  3715. memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  3716. if (use_default_chmap) {
  3717. pr_debug("%s: setting default channel map for %d channels",
  3718. __func__, channels);
  3719. if (q6asm_map_channels(channel_mapping, channels,
  3720. use_back_flavor)) {
  3721. pr_err("%s: map channels failed %d\n",
  3722. __func__, channels);
  3723. rc = -EINVAL;
  3724. goto fail_cmd;
  3725. }
  3726. } else {
  3727. pr_debug("%s: Using pre-defined channel map", __func__);
  3728. memcpy(channel_mapping, channel_map,
  3729. PCM_FORMAT_MAX_NUM_CHANNEL);
  3730. }
  3731. rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
  3732. if (rc < 0) {
  3733. pr_err("%s: Comamnd open failed %d\n", __func__, rc);
  3734. goto fail_cmd;
  3735. }
  3736. rc = wait_event_timeout(ac->cmd_wait,
  3737. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  3738. if (!rc) {
  3739. pr_err("%s: timeout opcode[0x%x]\n",
  3740. __func__, enc_cfg.hdr.opcode);
  3741. rc = -ETIMEDOUT;
  3742. goto fail_cmd;
  3743. }
  3744. if (atomic_read(&ac->cmd_state) > 0) {
  3745. pr_err("%s: DSP returned error[%s]\n",
  3746. __func__, adsp_err_get_err_str(
  3747. atomic_read(&ac->cmd_state)));
  3748. rc = adsp_err_get_lnx_err_code(
  3749. atomic_read(&ac->cmd_state));
  3750. goto fail_cmd;
  3751. }
  3752. return 0;
  3753. fail_cmd:
  3754. return rc;
  3755. }
  3756. EXPORT_SYMBOL(q6asm_enc_cfg_blk_pcm_v3);
  3757. int q6asm_enc_cfg_blk_pcm_v2(struct audio_client *ac,
  3758. uint32_t rate, uint32_t channels, uint16_t bits_per_sample,
  3759. bool use_default_chmap, bool use_back_flavor, u8 *channel_map)
  3760. {
  3761. struct asm_multi_channel_pcm_enc_cfg_v2 enc_cfg;
  3762. u8 *channel_mapping;
  3763. u32 frames_per_buf = 0;
  3764. int rc = 0;
  3765. if (!use_default_chmap && (channel_map == NULL)) {
  3766. pr_err("%s: No valid chan map and can't use default\n",
  3767. __func__);
  3768. return -EINVAL;
  3769. }
  3770. pr_debug("%s: Session %d, rate = %d, channels = %d\n", __func__,
  3771. ac->session, rate, channels);
  3772. q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
  3773. atomic_set(&ac->cmd_state, -1);
  3774. enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  3775. enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
  3776. enc_cfg.encdec.param_size = sizeof(enc_cfg) - sizeof(enc_cfg.hdr) -
  3777. sizeof(enc_cfg.encdec);
  3778. enc_cfg.encblk.frames_per_buf = frames_per_buf;
  3779. enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
  3780. sizeof(struct asm_enc_cfg_blk_param_v2);
  3781. enc_cfg.num_channels = channels;
  3782. enc_cfg.bits_per_sample = bits_per_sample;
  3783. enc_cfg.sample_rate = rate;
  3784. enc_cfg.is_signed = 1;
  3785. channel_mapping = enc_cfg.channel_mapping;
  3786. memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  3787. if (use_default_chmap) {
  3788. pr_debug("%s: setting default channel map for %d channels",
  3789. __func__, channels);
  3790. if (q6asm_map_channels(channel_mapping, channels,
  3791. use_back_flavor)) {
  3792. pr_err("%s: map channels failed %d\n",
  3793. __func__, channels);
  3794. return -EINVAL;
  3795. }
  3796. } else {
  3797. pr_debug("%s: Using pre-defined channel map", __func__);
  3798. memcpy(channel_mapping, channel_map,
  3799. PCM_FORMAT_MAX_NUM_CHANNEL);
  3800. }
  3801. rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
  3802. if (rc < 0) {
  3803. pr_err("%s: Comamnd open failed %d\n", __func__, rc);
  3804. rc = -EINVAL;
  3805. goto fail_cmd;
  3806. }
  3807. rc = wait_event_timeout(ac->cmd_wait,
  3808. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  3809. if (!rc) {
  3810. pr_err("%s: timeout opcode[0x%x]\n",
  3811. __func__, enc_cfg.hdr.opcode);
  3812. rc = -ETIMEDOUT;
  3813. goto fail_cmd;
  3814. }
  3815. if (atomic_read(&ac->cmd_state) > 0) {
  3816. pr_err("%s: DSP returned error[%s]\n",
  3817. __func__, adsp_err_get_err_str(
  3818. atomic_read(&ac->cmd_state)));
  3819. rc = adsp_err_get_lnx_err_code(
  3820. atomic_read(&ac->cmd_state));
  3821. goto fail_cmd;
  3822. }
  3823. return 0;
  3824. fail_cmd:
  3825. return rc;
  3826. }
  3827. static int __q6asm_enc_cfg_blk_pcm_v4(struct audio_client *ac,
  3828. uint32_t rate, uint32_t channels,
  3829. uint16_t bits_per_sample,
  3830. uint16_t sample_word_size,
  3831. uint16_t endianness,
  3832. uint16_t mode)
  3833. {
  3834. return q6asm_enc_cfg_blk_pcm_v4(ac, rate, channels,
  3835. bits_per_sample, true, false, NULL,
  3836. sample_word_size, endianness, mode);
  3837. }
  3838. static int __q6asm_enc_cfg_blk_pcm_v3(struct audio_client *ac,
  3839. uint32_t rate, uint32_t channels,
  3840. uint16_t bits_per_sample,
  3841. uint16_t sample_word_size)
  3842. {
  3843. return q6asm_enc_cfg_blk_pcm_v3(ac, rate, channels,
  3844. bits_per_sample, true, false, NULL,
  3845. sample_word_size);
  3846. }
  3847. static int __q6asm_enc_cfg_blk_pcm(struct audio_client *ac,
  3848. uint32_t rate, uint32_t channels, uint16_t bits_per_sample)
  3849. {
  3850. return q6asm_enc_cfg_blk_pcm_v2(ac, rate, channels,
  3851. bits_per_sample, true, false, NULL);
  3852. }
  3853. int q6asm_enc_cfg_blk_pcm(struct audio_client *ac,
  3854. uint32_t rate, uint32_t channels)
  3855. {
  3856. return __q6asm_enc_cfg_blk_pcm(ac, rate, channels, 16);
  3857. }
  3858. int q6asm_enc_cfg_blk_pcm_format_support(struct audio_client *ac,
  3859. uint32_t rate, uint32_t channels, uint16_t bits_per_sample)
  3860. {
  3861. return __q6asm_enc_cfg_blk_pcm(ac, rate, channels, bits_per_sample);
  3862. }
  3863. /*
  3864. * q6asm_enc_cfg_blk_pcm_format_support_v3 - sends encoder configuration
  3865. * parameters
  3866. *
  3867. * @ac: Client session handle
  3868. * @rate: sample rate
  3869. * @channels: number of channels
  3870. * @bits_per_sample: bit width of encoder session
  3871. * @sample_word_size: Size in bits of the word that holds a sample of a channel
  3872. */
  3873. int q6asm_enc_cfg_blk_pcm_format_support_v3(struct audio_client *ac,
  3874. uint32_t rate, uint32_t channels,
  3875. uint16_t bits_per_sample,
  3876. uint16_t sample_word_size)
  3877. {
  3878. return __q6asm_enc_cfg_blk_pcm_v3(ac, rate, channels,
  3879. bits_per_sample, sample_word_size);
  3880. }
  3881. EXPORT_SYMBOL(q6asm_enc_cfg_blk_pcm_format_support_v3);
  3882. /*
  3883. * q6asm_enc_cfg_blk_pcm_format_support_v4 - sends encoder configuration
  3884. * parameters
  3885. *
  3886. * @ac: Client session handle
  3887. * @rate: sample rate
  3888. * @channels: number of channels
  3889. * @bits_per_sample: bit width of encoder session
  3890. * @sample_word_size: Size in bits of the word that holds a sample of a channel
  3891. * @endianness: endianness of the pcm data
  3892. * @mode: Mode to provide additional info about the pcm input data
  3893. */
  3894. int q6asm_enc_cfg_blk_pcm_format_support_v4(struct audio_client *ac,
  3895. uint32_t rate, uint32_t channels,
  3896. uint16_t bits_per_sample,
  3897. uint16_t sample_word_size,
  3898. uint16_t endianness,
  3899. uint16_t mode)
  3900. {
  3901. return __q6asm_enc_cfg_blk_pcm_v4(ac, rate, channels,
  3902. bits_per_sample, sample_word_size,
  3903. endianness, mode);
  3904. }
  3905. EXPORT_SYMBOL(q6asm_enc_cfg_blk_pcm_format_support_v4);
  3906. int q6asm_enc_cfg_blk_pcm_native(struct audio_client *ac,
  3907. uint32_t rate, uint32_t channels)
  3908. {
  3909. struct asm_multi_channel_pcm_enc_cfg_v2 enc_cfg;
  3910. u8 *channel_mapping;
  3911. u32 frames_per_buf = 0;
  3912. int rc = 0;
  3913. pr_debug("%s: Session %d, rate = %d, channels = %d\n", __func__,
  3914. ac->session, rate, channels);
  3915. q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
  3916. atomic_set(&ac->cmd_state, -1);
  3917. enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  3918. enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
  3919. enc_cfg.encdec.param_size = sizeof(enc_cfg) - sizeof(enc_cfg.hdr) -
  3920. sizeof(enc_cfg.encdec);
  3921. enc_cfg.encblk.frames_per_buf = frames_per_buf;
  3922. enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
  3923. sizeof(struct asm_enc_cfg_blk_param_v2);
  3924. enc_cfg.num_channels = 0;/*channels;*/
  3925. enc_cfg.bits_per_sample = 16;
  3926. enc_cfg.sample_rate = 0;/*rate;*/
  3927. enc_cfg.is_signed = 1;
  3928. channel_mapping = enc_cfg.channel_mapping;
  3929. memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  3930. if (q6asm_map_channels(channel_mapping, channels, false)) {
  3931. pr_err("%s: map channels failed %d\n", __func__, channels);
  3932. return -EINVAL;
  3933. }
  3934. rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
  3935. if (rc < 0) {
  3936. pr_err("%s: Comamnd open failed %d\n", __func__, rc);
  3937. rc = -EINVAL;
  3938. goto fail_cmd;
  3939. }
  3940. rc = wait_event_timeout(ac->cmd_wait,
  3941. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  3942. if (!rc) {
  3943. pr_err("%s: timeout opcode[0x%x]\n",
  3944. __func__, enc_cfg.hdr.opcode);
  3945. rc = -ETIMEDOUT;
  3946. goto fail_cmd;
  3947. }
  3948. if (atomic_read(&ac->cmd_state) > 0) {
  3949. pr_err("%s: DSP returned error[%s]\n",
  3950. __func__, adsp_err_get_err_str(
  3951. atomic_read(&ac->cmd_state)));
  3952. rc = adsp_err_get_lnx_err_code(
  3953. atomic_read(&ac->cmd_state));
  3954. goto fail_cmd;
  3955. }
  3956. return 0;
  3957. fail_cmd:
  3958. return rc;
  3959. }
  3960. static int q6asm_map_channels(u8 *channel_mapping, uint32_t channels,
  3961. bool use_back_flavor)
  3962. {
  3963. u8 *lchannel_mapping;
  3964. lchannel_mapping = channel_mapping;
  3965. pr_debug("%s: channels passed: %d\n", __func__, channels);
  3966. if (channels == 1) {
  3967. lchannel_mapping[0] = PCM_CHANNEL_FC;
  3968. } else if (channels == 2) {
  3969. lchannel_mapping[0] = PCM_CHANNEL_FL;
  3970. lchannel_mapping[1] = PCM_CHANNEL_FR;
  3971. } else if (channels == 3) {
  3972. lchannel_mapping[0] = PCM_CHANNEL_FL;
  3973. lchannel_mapping[1] = PCM_CHANNEL_FR;
  3974. lchannel_mapping[2] = PCM_CHANNEL_FC;
  3975. } else if (channels == 4) {
  3976. lchannel_mapping[0] = PCM_CHANNEL_FL;
  3977. lchannel_mapping[1] = PCM_CHANNEL_FR;
  3978. lchannel_mapping[2] = use_back_flavor ?
  3979. PCM_CHANNEL_LB : PCM_CHANNEL_LS;
  3980. lchannel_mapping[3] = use_back_flavor ?
  3981. PCM_CHANNEL_RB : PCM_CHANNEL_RS;
  3982. } else if (channels == 5) {
  3983. lchannel_mapping[0] = PCM_CHANNEL_FL;
  3984. lchannel_mapping[1] = PCM_CHANNEL_FR;
  3985. lchannel_mapping[2] = PCM_CHANNEL_FC;
  3986. lchannel_mapping[3] = use_back_flavor ?
  3987. PCM_CHANNEL_LB : PCM_CHANNEL_LS;
  3988. lchannel_mapping[4] = use_back_flavor ?
  3989. PCM_CHANNEL_RB : PCM_CHANNEL_RS;
  3990. } else if (channels == 6) {
  3991. lchannel_mapping[0] = PCM_CHANNEL_FL;
  3992. lchannel_mapping[1] = PCM_CHANNEL_FR;
  3993. lchannel_mapping[2] = PCM_CHANNEL_FC;
  3994. lchannel_mapping[3] = PCM_CHANNEL_LFE;
  3995. lchannel_mapping[4] = use_back_flavor ?
  3996. PCM_CHANNEL_LB : PCM_CHANNEL_LS;
  3997. lchannel_mapping[5] = use_back_flavor ?
  3998. PCM_CHANNEL_RB : PCM_CHANNEL_RS;
  3999. } else if (channels == 7) {
  4000. /*
  4001. * Configured for 5.1 channel mapping + 1 channel for debug
  4002. * Can be customized based on DSP.
  4003. */
  4004. lchannel_mapping[0] = PCM_CHANNEL_FL;
  4005. lchannel_mapping[1] = PCM_CHANNEL_FR;
  4006. lchannel_mapping[2] = PCM_CHANNEL_FC;
  4007. lchannel_mapping[3] = PCM_CHANNEL_LFE;
  4008. lchannel_mapping[4] = use_back_flavor ?
  4009. PCM_CHANNEL_LB : PCM_CHANNEL_LS;
  4010. lchannel_mapping[5] = use_back_flavor ?
  4011. PCM_CHANNEL_RB : PCM_CHANNEL_RS;
  4012. lchannel_mapping[6] = PCM_CHANNEL_CS;
  4013. } else if (channels == 8) {
  4014. lchannel_mapping[0] = PCM_CHANNEL_FL;
  4015. lchannel_mapping[1] = PCM_CHANNEL_FR;
  4016. lchannel_mapping[2] = PCM_CHANNEL_FC;
  4017. lchannel_mapping[3] = PCM_CHANNEL_LFE;
  4018. lchannel_mapping[4] = PCM_CHANNEL_LB;
  4019. lchannel_mapping[5] = PCM_CHANNEL_RB;
  4020. lchannel_mapping[6] = PCM_CHANNEL_LS;
  4021. lchannel_mapping[7] = PCM_CHANNEL_RS;
  4022. } else {
  4023. pr_err("%s: ERROR.unsupported num_ch = %u\n",
  4024. __func__, channels);
  4025. return -EINVAL;
  4026. }
  4027. return 0;
  4028. }
  4029. int q6asm_enable_sbrps(struct audio_client *ac,
  4030. uint32_t sbr_ps_enable)
  4031. {
  4032. struct asm_aac_sbr_ps_flag_param sbrps;
  4033. u32 frames_per_buf = 0;
  4034. int rc = 0;
  4035. pr_debug("%s: Session %d\n", __func__, ac->session);
  4036. q6asm_add_hdr(ac, &sbrps.hdr, sizeof(sbrps), TRUE);
  4037. atomic_set(&ac->cmd_state, -1);
  4038. sbrps.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  4039. sbrps.encdec.param_id = ASM_PARAM_ID_AAC_SBR_PS_FLAG;
  4040. sbrps.encdec.param_size = sizeof(struct asm_aac_sbr_ps_flag_param) -
  4041. sizeof(struct asm_stream_cmd_set_encdec_param);
  4042. sbrps.encblk.frames_per_buf = frames_per_buf;
  4043. sbrps.encblk.enc_cfg_blk_size = sbrps.encdec.param_size -
  4044. sizeof(struct asm_enc_cfg_blk_param_v2);
  4045. sbrps.sbr_ps_flag = sbr_ps_enable;
  4046. rc = apr_send_pkt(ac->apr, (uint32_t *) &sbrps);
  4047. if (rc < 0) {
  4048. pr_err("%s: Command opcode[0x%x]paramid[0x%x] failed %d\n",
  4049. __func__,
  4050. ASM_STREAM_CMD_SET_ENCDEC_PARAM,
  4051. ASM_PARAM_ID_AAC_SBR_PS_FLAG, rc);
  4052. rc = -EINVAL;
  4053. goto fail_cmd;
  4054. }
  4055. rc = wait_event_timeout(ac->cmd_wait,
  4056. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  4057. if (!rc) {
  4058. pr_err("%s: timeout opcode[0x%x] ", __func__, sbrps.hdr.opcode);
  4059. rc = -ETIMEDOUT;
  4060. goto fail_cmd;
  4061. }
  4062. if (atomic_read(&ac->cmd_state) > 0) {
  4063. pr_err("%s: DSP returned error[%s]\n",
  4064. __func__, adsp_err_get_err_str(
  4065. atomic_read(&ac->cmd_state)));
  4066. rc = adsp_err_get_lnx_err_code(
  4067. atomic_read(&ac->cmd_state));
  4068. goto fail_cmd;
  4069. }
  4070. return 0;
  4071. fail_cmd:
  4072. return rc;
  4073. }
  4074. int q6asm_cfg_dual_mono_aac(struct audio_client *ac,
  4075. uint16_t sce_left, uint16_t sce_right)
  4076. {
  4077. struct asm_aac_dual_mono_mapping_param dual_mono;
  4078. int rc = 0;
  4079. pr_debug("%s: Session %d, sce_left = %d, sce_right = %d\n",
  4080. __func__, ac->session, sce_left, sce_right);
  4081. q6asm_add_hdr(ac, &dual_mono.hdr, sizeof(dual_mono), TRUE);
  4082. atomic_set(&ac->cmd_state, -1);
  4083. dual_mono.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  4084. dual_mono.encdec.param_id = ASM_PARAM_ID_AAC_DUAL_MONO_MAPPING;
  4085. dual_mono.encdec.param_size = sizeof(dual_mono.left_channel_sce) +
  4086. sizeof(dual_mono.right_channel_sce);
  4087. dual_mono.left_channel_sce = sce_left;
  4088. dual_mono.right_channel_sce = sce_right;
  4089. rc = apr_send_pkt(ac->apr, (uint32_t *) &dual_mono);
  4090. if (rc < 0) {
  4091. pr_err("%s: Command opcode[0x%x]paramid[0x%x] failed %d\n",
  4092. __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM,
  4093. ASM_PARAM_ID_AAC_DUAL_MONO_MAPPING, rc);
  4094. rc = -EINVAL;
  4095. goto fail_cmd;
  4096. }
  4097. rc = wait_event_timeout(ac->cmd_wait,
  4098. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  4099. if (!rc) {
  4100. pr_err("%s: timeout opcode[0x%x]\n", __func__,
  4101. dual_mono.hdr.opcode);
  4102. rc = -ETIMEDOUT;
  4103. goto fail_cmd;
  4104. }
  4105. if (atomic_read(&ac->cmd_state) > 0) {
  4106. pr_err("%s: DSP returned error[%s]\n",
  4107. __func__, adsp_err_get_err_str(
  4108. atomic_read(&ac->cmd_state)));
  4109. rc = adsp_err_get_lnx_err_code(
  4110. atomic_read(&ac->cmd_state));
  4111. goto fail_cmd;
  4112. }
  4113. return 0;
  4114. fail_cmd:
  4115. return rc;
  4116. }
  4117. /* Support for selecting stereo mixing coefficients for B family not done */
  4118. int q6asm_cfg_aac_sel_mix_coef(struct audio_client *ac, uint32_t mix_coeff)
  4119. {
  4120. struct asm_aac_stereo_mix_coeff_selection_param_v2 aac_mix_coeff;
  4121. int rc = 0;
  4122. q6asm_add_hdr(ac, &aac_mix_coeff.hdr, sizeof(aac_mix_coeff), TRUE);
  4123. atomic_set(&ac->cmd_state, -1);
  4124. aac_mix_coeff.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  4125. aac_mix_coeff.param_id =
  4126. ASM_PARAM_ID_AAC_STEREO_MIX_COEFF_SELECTION_FLAG_V2;
  4127. aac_mix_coeff.param_size =
  4128. sizeof(struct asm_aac_stereo_mix_coeff_selection_param_v2);
  4129. aac_mix_coeff.aac_stereo_mix_coeff_flag = mix_coeff;
  4130. pr_debug("%s: mix_coeff = %u\n", __func__, mix_coeff);
  4131. rc = apr_send_pkt(ac->apr, (uint32_t *) &aac_mix_coeff);
  4132. if (rc < 0) {
  4133. pr_err("%s: Command opcode[0x%x]paramid[0x%x] failed %d\n",
  4134. __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM,
  4135. ASM_PARAM_ID_AAC_STEREO_MIX_COEFF_SELECTION_FLAG_V2,
  4136. rc);
  4137. rc = -EINVAL;
  4138. goto fail_cmd;
  4139. }
  4140. rc = wait_event_timeout(ac->cmd_wait,
  4141. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  4142. if (!rc) {
  4143. pr_err("%s: timeout opcode[0x%x]\n",
  4144. __func__, aac_mix_coeff.hdr.opcode);
  4145. rc = -ETIMEDOUT;
  4146. goto fail_cmd;
  4147. }
  4148. if (atomic_read(&ac->cmd_state) > 0) {
  4149. pr_err("%s: DSP returned error[%s]\n",
  4150. __func__, adsp_err_get_err_str(
  4151. atomic_read(&ac->cmd_state)));
  4152. rc = adsp_err_get_lnx_err_code(
  4153. atomic_read(&ac->cmd_state));
  4154. goto fail_cmd;
  4155. }
  4156. return 0;
  4157. fail_cmd:
  4158. return rc;
  4159. }
  4160. int q6asm_enc_cfg_blk_qcelp(struct audio_client *ac, uint32_t frames_per_buf,
  4161. uint16_t min_rate, uint16_t max_rate,
  4162. uint16_t reduced_rate_level, uint16_t rate_modulation_cmd)
  4163. {
  4164. struct asm_v13k_enc_cfg enc_cfg;
  4165. int rc = 0;
  4166. pr_debug("%s: session[%d]frames[%d]min_rate[0x%4x]max_rate[0x%4x] reduced_rate_level[0x%4x]rate_modulation_cmd[0x%4x]\n",
  4167. __func__,
  4168. ac->session, frames_per_buf, min_rate, max_rate,
  4169. reduced_rate_level, rate_modulation_cmd);
  4170. q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
  4171. atomic_set(&ac->cmd_state, -1);
  4172. enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  4173. enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
  4174. enc_cfg.encdec.param_size = sizeof(struct asm_v13k_enc_cfg) -
  4175. sizeof(struct asm_stream_cmd_set_encdec_param);
  4176. enc_cfg.encblk.frames_per_buf = frames_per_buf;
  4177. enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
  4178. sizeof(struct asm_enc_cfg_blk_param_v2);
  4179. enc_cfg.min_rate = min_rate;
  4180. enc_cfg.max_rate = max_rate;
  4181. enc_cfg.reduced_rate_cmd = reduced_rate_level;
  4182. enc_cfg.rate_mod_cmd = rate_modulation_cmd;
  4183. rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
  4184. if (rc < 0) {
  4185. pr_err("%s: Comamnd %d failed %d\n",
  4186. __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM, rc);
  4187. rc = -EINVAL;
  4188. goto fail_cmd;
  4189. }
  4190. rc = wait_event_timeout(ac->cmd_wait,
  4191. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  4192. if (!rc) {
  4193. pr_err("%s: timeout. waited for setencdec v13k resp\n",
  4194. __func__);
  4195. rc = -ETIMEDOUT;
  4196. goto fail_cmd;
  4197. }
  4198. if (atomic_read(&ac->cmd_state) > 0) {
  4199. pr_err("%s: DSP returned error[%s]\n",
  4200. __func__, adsp_err_get_err_str(
  4201. atomic_read(&ac->cmd_state)));
  4202. rc = adsp_err_get_lnx_err_code(
  4203. atomic_read(&ac->cmd_state));
  4204. goto fail_cmd;
  4205. }
  4206. return 0;
  4207. fail_cmd:
  4208. return rc;
  4209. }
  4210. int q6asm_enc_cfg_blk_evrc(struct audio_client *ac, uint32_t frames_per_buf,
  4211. uint16_t min_rate, uint16_t max_rate,
  4212. uint16_t rate_modulation_cmd)
  4213. {
  4214. struct asm_evrc_enc_cfg enc_cfg;
  4215. int rc = 0;
  4216. pr_debug("%s: session[%d]frames[%d]min_rate[0x%4x]max_rate[0x%4x] rate_modulation_cmd[0x%4x]\n",
  4217. __func__, ac->session,
  4218. frames_per_buf, min_rate, max_rate, rate_modulation_cmd);
  4219. q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
  4220. atomic_set(&ac->cmd_state, -1);
  4221. enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  4222. enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
  4223. enc_cfg.encdec.param_size = sizeof(struct asm_evrc_enc_cfg) -
  4224. sizeof(struct asm_stream_cmd_set_encdec_param);
  4225. enc_cfg.encblk.frames_per_buf = frames_per_buf;
  4226. enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
  4227. sizeof(struct asm_enc_cfg_blk_param_v2);
  4228. enc_cfg.min_rate = min_rate;
  4229. enc_cfg.max_rate = max_rate;
  4230. enc_cfg.rate_mod_cmd = rate_modulation_cmd;
  4231. enc_cfg.reserved = 0;
  4232. rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
  4233. if (rc < 0) {
  4234. pr_err("%s: Comamnd %d failed %d\n",
  4235. __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM, rc);
  4236. rc = -EINVAL;
  4237. goto fail_cmd;
  4238. }
  4239. rc = wait_event_timeout(ac->cmd_wait,
  4240. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  4241. if (!rc) {
  4242. pr_err("%s: timeout. waited for encdec evrc\n", __func__);
  4243. rc = -ETIMEDOUT;
  4244. goto fail_cmd;
  4245. }
  4246. if (atomic_read(&ac->cmd_state) > 0) {
  4247. pr_err("%s: DSP returned error[%s]\n",
  4248. __func__, adsp_err_get_err_str(
  4249. atomic_read(&ac->cmd_state)));
  4250. rc = adsp_err_get_lnx_err_code(
  4251. atomic_read(&ac->cmd_state));
  4252. goto fail_cmd;
  4253. }
  4254. return 0;
  4255. fail_cmd:
  4256. return rc;
  4257. }
  4258. int q6asm_enc_cfg_blk_amrnb(struct audio_client *ac, uint32_t frames_per_buf,
  4259. uint16_t band_mode, uint16_t dtx_enable)
  4260. {
  4261. struct asm_amrnb_enc_cfg enc_cfg;
  4262. int rc = 0;
  4263. pr_debug("%s: session[%d]frames[%d]band_mode[0x%4x]dtx_enable[0x%4x]\n",
  4264. __func__, ac->session, frames_per_buf, band_mode, dtx_enable);
  4265. q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
  4266. atomic_set(&ac->cmd_state, -1);
  4267. enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  4268. enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
  4269. enc_cfg.encdec.param_size = sizeof(struct asm_amrnb_enc_cfg) -
  4270. sizeof(struct asm_stream_cmd_set_encdec_param);
  4271. enc_cfg.encblk.frames_per_buf = frames_per_buf;
  4272. enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
  4273. sizeof(struct asm_enc_cfg_blk_param_v2);
  4274. enc_cfg.enc_mode = band_mode;
  4275. enc_cfg.dtx_mode = dtx_enable;
  4276. rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
  4277. if (rc < 0) {
  4278. pr_err("%s: Comamnd %d failed %d\n",
  4279. __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM, rc);
  4280. rc = -EINVAL;
  4281. goto fail_cmd;
  4282. }
  4283. rc = wait_event_timeout(ac->cmd_wait,
  4284. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  4285. if (!rc) {
  4286. pr_err("%s: timeout. waited for set encdec amrnb\n", __func__);
  4287. rc = -ETIMEDOUT;
  4288. goto fail_cmd;
  4289. }
  4290. if (atomic_read(&ac->cmd_state) > 0) {
  4291. pr_err("%s: DSP returned error[%s]\n",
  4292. __func__, adsp_err_get_err_str(
  4293. atomic_read(&ac->cmd_state)));
  4294. rc = adsp_err_get_lnx_err_code(
  4295. atomic_read(&ac->cmd_state));
  4296. goto fail_cmd;
  4297. }
  4298. return 0;
  4299. fail_cmd:
  4300. return rc;
  4301. }
  4302. int q6asm_enc_cfg_blk_amrwb(struct audio_client *ac, uint32_t frames_per_buf,
  4303. uint16_t band_mode, uint16_t dtx_enable)
  4304. {
  4305. struct asm_amrwb_enc_cfg enc_cfg;
  4306. int rc = 0;
  4307. pr_debug("%s: session[%d]frames[%d]band_mode[0x%4x]dtx_enable[0x%4x]\n",
  4308. __func__, ac->session, frames_per_buf, band_mode, dtx_enable);
  4309. q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
  4310. atomic_set(&ac->cmd_state, -1);
  4311. enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  4312. enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
  4313. enc_cfg.encdec.param_size = sizeof(struct asm_amrwb_enc_cfg) -
  4314. sizeof(struct asm_stream_cmd_set_encdec_param);
  4315. enc_cfg.encblk.frames_per_buf = frames_per_buf;
  4316. enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
  4317. sizeof(struct asm_enc_cfg_blk_param_v2);
  4318. enc_cfg.enc_mode = band_mode;
  4319. enc_cfg.dtx_mode = dtx_enable;
  4320. rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
  4321. if (rc < 0) {
  4322. pr_err("%s: Comamnd %d failed %d\n",
  4323. __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM, rc);
  4324. rc = -EINVAL;
  4325. goto fail_cmd;
  4326. }
  4327. rc = wait_event_timeout(ac->cmd_wait,
  4328. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  4329. if (!rc) {
  4330. pr_err("%s: timeout. waited for FORMAT_UPDATE\n", __func__);
  4331. rc = -ETIMEDOUT;
  4332. goto fail_cmd;
  4333. }
  4334. if (atomic_read(&ac->cmd_state) > 0) {
  4335. pr_err("%s: DSP returned error[%s]\n",
  4336. __func__, adsp_err_get_err_str(
  4337. atomic_read(&ac->cmd_state)));
  4338. rc = adsp_err_get_lnx_err_code(
  4339. atomic_read(&ac->cmd_state));
  4340. goto fail_cmd;
  4341. }
  4342. return 0;
  4343. fail_cmd:
  4344. return rc;
  4345. }
  4346. static int __q6asm_media_format_block_pcm(struct audio_client *ac,
  4347. uint32_t rate, uint32_t channels,
  4348. uint16_t bits_per_sample, int stream_id,
  4349. bool use_default_chmap, char *channel_map)
  4350. {
  4351. struct asm_multi_channel_pcm_fmt_blk_v2 fmt;
  4352. u8 *channel_mapping;
  4353. int rc = 0;
  4354. pr_debug("%s: session[%d]rate[%d]ch[%d]\n", __func__, ac->session, rate,
  4355. channels);
  4356. q6asm_stream_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE, stream_id);
  4357. atomic_set(&ac->cmd_state, -1);
  4358. /*
  4359. * Updated the token field with stream/session for compressed playback
  4360. * Platform driver must know the the stream with which the command is
  4361. * associated
  4362. */
  4363. if (ac->io_mode & COMPRESSED_STREAM_IO)
  4364. q6asm_update_token(&fmt.hdr.token,
  4365. ac->session,
  4366. stream_id,
  4367. 0, /* Buffer index is NA */
  4368. 0, /* Direction flag is NA */
  4369. WAIT_CMD);
  4370. pr_debug("%s: token = 0x%x, stream_id %d, session 0x%x\n",
  4371. __func__, fmt.hdr.token, stream_id, ac->session);
  4372. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  4373. fmt.fmt_blk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  4374. sizeof(fmt.fmt_blk);
  4375. fmt.num_channels = channels;
  4376. fmt.bits_per_sample = bits_per_sample;
  4377. fmt.sample_rate = rate;
  4378. fmt.is_signed = 1;
  4379. channel_mapping = fmt.channel_mapping;
  4380. memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  4381. if (use_default_chmap) {
  4382. if (q6asm_map_channels(channel_mapping, channels, false)) {
  4383. pr_err("%s: map channels failed %d\n",
  4384. __func__, channels);
  4385. return -EINVAL;
  4386. }
  4387. } else {
  4388. memcpy(channel_mapping, channel_map,
  4389. PCM_FORMAT_MAX_NUM_CHANNEL);
  4390. }
  4391. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  4392. if (rc < 0) {
  4393. pr_err("%s: Comamnd open failed %d\n", __func__, rc);
  4394. rc = -EINVAL;
  4395. goto fail_cmd;
  4396. }
  4397. rc = wait_event_timeout(ac->cmd_wait,
  4398. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  4399. if (!rc) {
  4400. pr_err("%s: timeout. waited for format update\n", __func__);
  4401. rc = -ETIMEDOUT;
  4402. goto fail_cmd;
  4403. }
  4404. if (atomic_read(&ac->cmd_state) > 0) {
  4405. pr_err("%s: DSP returned error[%s]\n",
  4406. __func__, adsp_err_get_err_str(
  4407. atomic_read(&ac->cmd_state)));
  4408. rc = adsp_err_get_lnx_err_code(
  4409. atomic_read(&ac->cmd_state));
  4410. goto fail_cmd;
  4411. }
  4412. return 0;
  4413. fail_cmd:
  4414. return rc;
  4415. }
  4416. static int __q6asm_media_format_block_pcm_v3(struct audio_client *ac,
  4417. uint32_t rate, uint32_t channels,
  4418. uint16_t bits_per_sample,
  4419. int stream_id,
  4420. bool use_default_chmap,
  4421. char *channel_map,
  4422. uint16_t sample_word_size)
  4423. {
  4424. struct asm_multi_channel_pcm_fmt_blk_param_v3 fmt;
  4425. u8 *channel_mapping;
  4426. int rc;
  4427. pr_debug("%s: session[%d]rate[%d]ch[%d]bps[%d]wordsize[%d]\n", __func__,
  4428. ac->session, rate, channels,
  4429. bits_per_sample, sample_word_size);
  4430. memset(&fmt, 0, sizeof(fmt));
  4431. q6asm_stream_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE, stream_id);
  4432. atomic_set(&ac->cmd_state, -1);
  4433. /*
  4434. * Updated the token field with stream/session for compressed playback
  4435. * Platform driver must know the the stream with which the command is
  4436. * associated
  4437. */
  4438. if (ac->io_mode & COMPRESSED_STREAM_IO)
  4439. fmt.hdr.token = ((ac->session << 8) & 0xFFFF00) |
  4440. (stream_id & 0xFF);
  4441. pr_debug("%s: token = 0x%x, stream_id %d, session 0x%x\n",
  4442. __func__, fmt.hdr.token, stream_id, ac->session);
  4443. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  4444. fmt.fmt_blk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  4445. sizeof(fmt.fmt_blk);
  4446. fmt.param.num_channels = channels;
  4447. fmt.param.bits_per_sample = bits_per_sample;
  4448. fmt.param.sample_rate = rate;
  4449. fmt.param.is_signed = 1;
  4450. fmt.param.sample_word_size = sample_word_size;
  4451. channel_mapping = fmt.param.channel_mapping;
  4452. memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  4453. if (use_default_chmap) {
  4454. if (q6asm_map_channels(channel_mapping, channels, false)) {
  4455. pr_err("%s: map channels failed %d\n",
  4456. __func__, channels);
  4457. rc = -EINVAL;
  4458. goto fail_cmd;
  4459. }
  4460. } else {
  4461. memcpy(channel_mapping, channel_map,
  4462. PCM_FORMAT_MAX_NUM_CHANNEL);
  4463. }
  4464. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  4465. if (rc < 0) {
  4466. pr_err("%s: Comamnd open failed %d\n", __func__, rc);
  4467. rc = -EINVAL;
  4468. goto fail_cmd;
  4469. }
  4470. rc = wait_event_timeout(ac->cmd_wait,
  4471. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  4472. if (!rc) {
  4473. pr_err("%s: timeout. waited for format update\n", __func__);
  4474. rc = -ETIMEDOUT;
  4475. goto fail_cmd;
  4476. }
  4477. if (atomic_read(&ac->cmd_state) > 0) {
  4478. pr_err("%s: DSP returned error[%s]\n",
  4479. __func__, adsp_err_get_err_str(
  4480. atomic_read(&ac->cmd_state)));
  4481. rc = adsp_err_get_lnx_err_code(
  4482. atomic_read(&ac->cmd_state));
  4483. goto fail_cmd;
  4484. }
  4485. return 0;
  4486. fail_cmd:
  4487. return rc;
  4488. }
  4489. static int __q6asm_media_format_block_pcm_v4(struct audio_client *ac,
  4490. uint32_t rate, uint32_t channels,
  4491. uint16_t bits_per_sample,
  4492. int stream_id,
  4493. bool use_default_chmap,
  4494. char *channel_map,
  4495. uint16_t sample_word_size,
  4496. uint16_t endianness,
  4497. uint16_t mode)
  4498. {
  4499. struct asm_multi_channel_pcm_fmt_blk_param_v4 fmt;
  4500. u8 *channel_mapping;
  4501. int rc;
  4502. pr_debug("%s: session[%d]rate[%d]ch[%d]bps[%d]wordsize[%d]\n", __func__,
  4503. ac->session, rate, channels,
  4504. bits_per_sample, sample_word_size);
  4505. memset(&fmt, 0, sizeof(fmt));
  4506. q6asm_stream_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE, stream_id);
  4507. atomic_set(&ac->cmd_state, -1);
  4508. /*
  4509. * Updated the token field with stream/session for compressed playback
  4510. * Platform driver must know the the stream with which the command is
  4511. * associated
  4512. */
  4513. if (ac->io_mode & COMPRESSED_STREAM_IO)
  4514. fmt.hdr.token = ((ac->session << 8) & 0xFFFF00) |
  4515. (stream_id & 0xFF);
  4516. pr_debug("%s: token = 0x%x, stream_id %d, session 0x%x\n",
  4517. __func__, fmt.hdr.token, stream_id, ac->session);
  4518. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  4519. fmt.fmt_blk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  4520. sizeof(fmt.fmt_blk);
  4521. fmt.param.num_channels = channels;
  4522. fmt.param.bits_per_sample = bits_per_sample;
  4523. fmt.param.sample_rate = rate;
  4524. fmt.param.is_signed = 1;
  4525. fmt.param.sample_word_size = sample_word_size;
  4526. fmt.param.endianness = endianness;
  4527. fmt.param.mode = mode;
  4528. channel_mapping = fmt.param.channel_mapping;
  4529. memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  4530. if (use_default_chmap) {
  4531. if (q6asm_map_channels(channel_mapping, channels, false)) {
  4532. pr_err("%s: map channels failed %d\n",
  4533. __func__, channels);
  4534. rc = -EINVAL;
  4535. goto fail_cmd;
  4536. }
  4537. } else {
  4538. memcpy(channel_mapping, channel_map,
  4539. PCM_FORMAT_MAX_NUM_CHANNEL);
  4540. }
  4541. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  4542. if (rc < 0) {
  4543. pr_err("%s: Comamnd open failed %d\n", __func__, rc);
  4544. rc = -EINVAL;
  4545. goto fail_cmd;
  4546. }
  4547. rc = wait_event_timeout(ac->cmd_wait,
  4548. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  4549. if (!rc) {
  4550. pr_err("%s: timeout. waited for format update\n", __func__);
  4551. rc = -ETIMEDOUT;
  4552. goto fail_cmd;
  4553. }
  4554. if (atomic_read(&ac->cmd_state) > 0) {
  4555. pr_err("%s: DSP returned error[%s]\n",
  4556. __func__, adsp_err_get_err_str(
  4557. atomic_read(&ac->cmd_state)));
  4558. rc = adsp_err_get_lnx_err_code(
  4559. atomic_read(&ac->cmd_state));
  4560. goto fail_cmd;
  4561. }
  4562. return 0;
  4563. fail_cmd:
  4564. return rc;
  4565. }
  4566. int q6asm_media_format_block_pcm(struct audio_client *ac,
  4567. uint32_t rate, uint32_t channels)
  4568. {
  4569. return __q6asm_media_format_block_pcm(ac, rate,
  4570. channels, 16, ac->stream_id,
  4571. true, NULL);
  4572. }
  4573. int q6asm_media_format_block_pcm_format_support(struct audio_client *ac,
  4574. uint32_t rate, uint32_t channels,
  4575. uint16_t bits_per_sample)
  4576. {
  4577. return __q6asm_media_format_block_pcm(ac, rate,
  4578. channels, bits_per_sample, ac->stream_id,
  4579. true, NULL);
  4580. }
  4581. int q6asm_media_format_block_pcm_format_support_v2(struct audio_client *ac,
  4582. uint32_t rate, uint32_t channels,
  4583. uint16_t bits_per_sample, int stream_id,
  4584. bool use_default_chmap, char *channel_map)
  4585. {
  4586. if (!use_default_chmap && (channel_map == NULL)) {
  4587. pr_err("%s: No valid chan map and can't use default\n",
  4588. __func__);
  4589. return -EINVAL;
  4590. }
  4591. return __q6asm_media_format_block_pcm(ac, rate,
  4592. channels, bits_per_sample, stream_id,
  4593. use_default_chmap, channel_map);
  4594. }
  4595. /*
  4596. * q6asm_media_format_block_pcm_format_support_v3- sends pcm decoder
  4597. * configuration parameters
  4598. *
  4599. * @ac: Client session handle
  4600. * @rate: sample rate
  4601. * @channels: number of channels
  4602. * @bits_per_sample: bit width of encoder session
  4603. * @stream_id: stream id of stream to be associated with this session
  4604. * @use_default_chmap: true if default channel map to be used
  4605. * @channel_map: input channel map
  4606. * @sample_word_size: Size in bits of the word that holds a sample of a channel
  4607. */
  4608. int q6asm_media_format_block_pcm_format_support_v3(struct audio_client *ac,
  4609. uint32_t rate,
  4610. uint32_t channels,
  4611. uint16_t bits_per_sample,
  4612. int stream_id,
  4613. bool use_default_chmap,
  4614. char *channel_map,
  4615. uint16_t sample_word_size)
  4616. {
  4617. if (!use_default_chmap && (channel_map == NULL)) {
  4618. pr_err("%s: No valid chan map and can't use default\n",
  4619. __func__);
  4620. return -EINVAL;
  4621. }
  4622. return __q6asm_media_format_block_pcm_v3(ac, rate,
  4623. channels, bits_per_sample, stream_id,
  4624. use_default_chmap, channel_map,
  4625. sample_word_size);
  4626. }
  4627. EXPORT_SYMBOL(q6asm_media_format_block_pcm_format_support_v3);
  4628. /*
  4629. * q6asm_media_format_block_pcm_format_support_v4- sends pcm decoder
  4630. * configuration parameters
  4631. *
  4632. * @ac: Client session handle
  4633. * @rate: sample rate
  4634. * @channels: number of channels
  4635. * @bits_per_sample: bit width of encoder session
  4636. * @stream_id: stream id of stream to be associated with this session
  4637. * @use_default_chmap: true if default channel map to be used
  4638. * @channel_map: input channel map
  4639. * @sample_word_size: Size in bits of the word that holds a sample of a channel
  4640. * @endianness: endianness of the pcm data
  4641. * @mode: Mode to provide additional info about the pcm input data
  4642. */
  4643. int q6asm_media_format_block_pcm_format_support_v4(struct audio_client *ac,
  4644. uint32_t rate,
  4645. uint32_t channels,
  4646. uint16_t bits_per_sample,
  4647. int stream_id,
  4648. bool use_default_chmap,
  4649. char *channel_map,
  4650. uint16_t sample_word_size,
  4651. uint16_t endianness,
  4652. uint16_t mode)
  4653. {
  4654. if (!use_default_chmap && (channel_map == NULL)) {
  4655. pr_err("%s: No valid chan map and can't use default\n",
  4656. __func__);
  4657. return -EINVAL;
  4658. }
  4659. return __q6asm_media_format_block_pcm_v4(ac, rate,
  4660. channels, bits_per_sample, stream_id,
  4661. use_default_chmap, channel_map,
  4662. sample_word_size, endianness,
  4663. mode);
  4664. }
  4665. EXPORT_SYMBOL(q6asm_media_format_block_pcm_format_support_v4);
  4666. static int __q6asm_media_format_block_multi_ch_pcm(struct audio_client *ac,
  4667. uint32_t rate, uint32_t channels,
  4668. bool use_default_chmap, char *channel_map,
  4669. uint16_t bits_per_sample)
  4670. {
  4671. struct asm_multi_channel_pcm_fmt_blk_v2 fmt;
  4672. u8 *channel_mapping;
  4673. int rc = 0;
  4674. pr_debug("%s: session[%d]rate[%d]ch[%d]\n", __func__, ac->session, rate,
  4675. channels);
  4676. q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
  4677. atomic_set(&ac->cmd_state, -1);
  4678. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  4679. fmt.fmt_blk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  4680. sizeof(fmt.fmt_blk);
  4681. fmt.num_channels = channels;
  4682. fmt.bits_per_sample = bits_per_sample;
  4683. fmt.sample_rate = rate;
  4684. fmt.is_signed = 1;
  4685. channel_mapping = fmt.channel_mapping;
  4686. memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  4687. if (use_default_chmap) {
  4688. if (q6asm_map_channels(channel_mapping, channels, false)) {
  4689. pr_err("%s: map channels failed %d\n",
  4690. __func__, channels);
  4691. return -EINVAL;
  4692. }
  4693. } else {
  4694. memcpy(channel_mapping, channel_map,
  4695. PCM_FORMAT_MAX_NUM_CHANNEL);
  4696. }
  4697. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  4698. if (rc < 0) {
  4699. pr_err("%s: Comamnd open failed %d\n", __func__, rc);
  4700. rc = -EINVAL;
  4701. goto fail_cmd;
  4702. }
  4703. rc = wait_event_timeout(ac->cmd_wait,
  4704. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  4705. if (!rc) {
  4706. pr_err("%s: timeout. waited for format update\n", __func__);
  4707. rc = -ETIMEDOUT;
  4708. goto fail_cmd;
  4709. }
  4710. if (atomic_read(&ac->cmd_state) > 0) {
  4711. pr_err("%s: DSP returned error[%s]\n",
  4712. __func__, adsp_err_get_err_str(
  4713. atomic_read(&ac->cmd_state)));
  4714. rc = adsp_err_get_lnx_err_code(
  4715. atomic_read(&ac->cmd_state));
  4716. goto fail_cmd;
  4717. }
  4718. return 0;
  4719. fail_cmd:
  4720. return rc;
  4721. }
  4722. static int __q6asm_media_format_block_multi_ch_pcm_v3(struct audio_client *ac,
  4723. uint32_t rate,
  4724. uint32_t channels,
  4725. bool use_default_chmap,
  4726. char *channel_map,
  4727. uint16_t bits_per_sample,
  4728. uint16_t sample_word_size)
  4729. {
  4730. struct asm_multi_channel_pcm_fmt_blk_param_v3 fmt;
  4731. u8 *channel_mapping;
  4732. int rc;
  4733. pr_debug("%s: session[%d]rate[%d]ch[%d]bps[%d]wordsize[%d]\n", __func__,
  4734. ac->session, rate, channels,
  4735. bits_per_sample, sample_word_size);
  4736. memset(&fmt, 0, sizeof(fmt));
  4737. q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
  4738. atomic_set(&ac->cmd_state, -1);
  4739. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  4740. fmt.fmt_blk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  4741. sizeof(fmt.fmt_blk);
  4742. fmt.param.num_channels = channels;
  4743. fmt.param.bits_per_sample = bits_per_sample;
  4744. fmt.param.sample_rate = rate;
  4745. fmt.param.is_signed = 1;
  4746. fmt.param.sample_word_size = sample_word_size;
  4747. channel_mapping = fmt.param.channel_mapping;
  4748. memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  4749. if (use_default_chmap) {
  4750. if (q6asm_map_channels(channel_mapping, channels, false)) {
  4751. pr_err("%s: map channels failed %d\n",
  4752. __func__, channels);
  4753. rc = -EINVAL;
  4754. goto fail_cmd;
  4755. }
  4756. } else {
  4757. memcpy(channel_mapping, channel_map,
  4758. PCM_FORMAT_MAX_NUM_CHANNEL);
  4759. }
  4760. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  4761. if (rc < 0) {
  4762. pr_err("%s: Comamnd open failed %d\n", __func__, rc);
  4763. goto fail_cmd;
  4764. }
  4765. rc = wait_event_timeout(ac->cmd_wait,
  4766. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  4767. if (!rc) {
  4768. pr_err("%s: timeout. waited for format update\n", __func__);
  4769. rc = -ETIMEDOUT;
  4770. goto fail_cmd;
  4771. }
  4772. if (atomic_read(&ac->cmd_state) > 0) {
  4773. pr_err("%s: DSP returned error[%s]\n",
  4774. __func__, adsp_err_get_err_str(
  4775. atomic_read(&ac->cmd_state)));
  4776. rc = adsp_err_get_lnx_err_code(
  4777. atomic_read(&ac->cmd_state));
  4778. goto fail_cmd;
  4779. }
  4780. return 0;
  4781. fail_cmd:
  4782. return rc;
  4783. }
  4784. static int __q6asm_media_format_block_multi_ch_pcm_v4(struct audio_client *ac,
  4785. uint32_t rate,
  4786. uint32_t channels,
  4787. bool use_default_chmap,
  4788. char *channel_map,
  4789. uint16_t bits_per_sample,
  4790. uint16_t sample_word_size,
  4791. uint16_t endianness,
  4792. uint16_t mode)
  4793. {
  4794. struct asm_multi_channel_pcm_fmt_blk_param_v4 fmt;
  4795. u8 *channel_mapping;
  4796. int rc;
  4797. pr_debug("%s: session[%d]rate[%d]ch[%d]bps[%d]wordsize[%d]\n", __func__,
  4798. ac->session, rate, channels,
  4799. bits_per_sample, sample_word_size);
  4800. memset(&fmt, 0, sizeof(fmt));
  4801. q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
  4802. atomic_set(&ac->cmd_state, -1);
  4803. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  4804. fmt.fmt_blk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  4805. sizeof(fmt.fmt_blk);
  4806. fmt.param.num_channels = channels;
  4807. fmt.param.bits_per_sample = bits_per_sample;
  4808. fmt.param.sample_rate = rate;
  4809. fmt.param.is_signed = 1;
  4810. fmt.param.sample_word_size = sample_word_size;
  4811. fmt.param.endianness = endianness;
  4812. fmt.param.mode = mode;
  4813. channel_mapping = fmt.param.channel_mapping;
  4814. memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  4815. if (use_default_chmap) {
  4816. if (q6asm_map_channels(channel_mapping, channels, false)) {
  4817. pr_err("%s: map channels failed %d\n",
  4818. __func__, channels);
  4819. rc = -EINVAL;
  4820. goto fail_cmd;
  4821. }
  4822. } else {
  4823. memcpy(channel_mapping, channel_map,
  4824. PCM_FORMAT_MAX_NUM_CHANNEL);
  4825. }
  4826. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  4827. if (rc < 0) {
  4828. pr_err("%s: Comamnd open failed %d\n", __func__, rc);
  4829. goto fail_cmd;
  4830. }
  4831. rc = wait_event_timeout(ac->cmd_wait,
  4832. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  4833. if (!rc) {
  4834. pr_err("%s: timeout. waited for format update\n", __func__);
  4835. rc = -ETIMEDOUT;
  4836. goto fail_cmd;
  4837. }
  4838. if (atomic_read(&ac->cmd_state) > 0) {
  4839. pr_err("%s: DSP returned error[%s]\n",
  4840. __func__, adsp_err_get_err_str(
  4841. atomic_read(&ac->cmd_state)));
  4842. rc = adsp_err_get_lnx_err_code(
  4843. atomic_read(&ac->cmd_state));
  4844. goto fail_cmd;
  4845. }
  4846. return 0;
  4847. fail_cmd:
  4848. return rc;
  4849. }
  4850. int q6asm_media_format_block_multi_ch_pcm(struct audio_client *ac,
  4851. uint32_t rate, uint32_t channels,
  4852. bool use_default_chmap, char *channel_map)
  4853. {
  4854. return __q6asm_media_format_block_multi_ch_pcm(ac, rate,
  4855. channels, use_default_chmap, channel_map, 16);
  4856. }
  4857. int q6asm_media_format_block_multi_ch_pcm_v2(
  4858. struct audio_client *ac,
  4859. uint32_t rate, uint32_t channels,
  4860. bool use_default_chmap, char *channel_map,
  4861. uint16_t bits_per_sample)
  4862. {
  4863. return __q6asm_media_format_block_multi_ch_pcm(ac, rate,
  4864. channels, use_default_chmap, channel_map,
  4865. bits_per_sample);
  4866. }
  4867. /*
  4868. * q6asm_media_format_block_multi_ch_pcm_v3 - sends pcm decoder configuration
  4869. * parameters
  4870. *
  4871. * @ac: Client session handle
  4872. * @rate: sample rate
  4873. * @channels: number of channels
  4874. * @bits_per_sample: bit width of encoder session
  4875. * @use_default_chmap: true if default channel map to be used
  4876. * @channel_map: input channel map
  4877. * @sample_word_size: Size in bits of the word that holds a sample of a channel
  4878. */
  4879. int q6asm_media_format_block_multi_ch_pcm_v3(struct audio_client *ac,
  4880. uint32_t rate, uint32_t channels,
  4881. bool use_default_chmap,
  4882. char *channel_map,
  4883. uint16_t bits_per_sample,
  4884. uint16_t sample_word_size)
  4885. {
  4886. return __q6asm_media_format_block_multi_ch_pcm_v3(ac, rate, channels,
  4887. use_default_chmap,
  4888. channel_map,
  4889. bits_per_sample,
  4890. sample_word_size);
  4891. }
  4892. EXPORT_SYMBOL(q6asm_media_format_block_multi_ch_pcm_v3);
  4893. /*
  4894. * q6asm_media_format_block_multi_ch_pcm_v4 - sends pcm decoder configuration
  4895. * parameters
  4896. *
  4897. * @ac: Client session handle
  4898. * @rate: sample rate
  4899. * @channels: number of channels
  4900. * @bits_per_sample: bit width of encoder session
  4901. * @use_default_chmap: true if default channel map to be used
  4902. * @channel_map: input channel map
  4903. * @sample_word_size: Size in bits of the word that holds a sample of a channel
  4904. * @endianness: endianness of the pcm data
  4905. * @mode: Mode to provide additional info about the pcm input data
  4906. */
  4907. int q6asm_media_format_block_multi_ch_pcm_v4(struct audio_client *ac,
  4908. uint32_t rate, uint32_t channels,
  4909. bool use_default_chmap,
  4910. char *channel_map,
  4911. uint16_t bits_per_sample,
  4912. uint16_t sample_word_size,
  4913. uint16_t endianness,
  4914. uint16_t mode)
  4915. {
  4916. return __q6asm_media_format_block_multi_ch_pcm_v4(ac, rate, channels,
  4917. use_default_chmap,
  4918. channel_map,
  4919. bits_per_sample,
  4920. sample_word_size,
  4921. endianness,
  4922. mode);
  4923. }
  4924. EXPORT_SYMBOL(q6asm_media_format_block_multi_ch_pcm_v4);
  4925. /*
  4926. * q6asm_media_format_block_gen_compr - set up generic compress format params
  4927. *
  4928. * @ac: Client session handle
  4929. * @rate: sample rate
  4930. * @channels: number of channels
  4931. * @use_default_chmap: true if default channel map to be used
  4932. * @channel_map: input channel map
  4933. * @bits_per_sample: bit width of gen compress stream
  4934. */
  4935. int q6asm_media_format_block_gen_compr(struct audio_client *ac,
  4936. uint32_t rate, uint32_t channels,
  4937. bool use_default_chmap, char *channel_map,
  4938. uint16_t bits_per_sample)
  4939. {
  4940. struct asm_generic_compressed_fmt_blk_t fmt;
  4941. u8 *channel_mapping;
  4942. int rc = 0;
  4943. pr_debug("%s: session[%d]rate[%d]ch[%d]bps[%d]\n",
  4944. __func__, ac->session, rate,
  4945. channels, bits_per_sample);
  4946. memset(&fmt, 0, sizeof(fmt));
  4947. q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
  4948. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  4949. fmt.fmt_blk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  4950. sizeof(fmt.fmt_blk);
  4951. fmt.num_channels = channels;
  4952. fmt.bits_per_sample = bits_per_sample;
  4953. fmt.sampling_rate = rate;
  4954. channel_mapping = fmt.channel_mapping;
  4955. memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  4956. if (use_default_chmap) {
  4957. if (q6asm_map_channels(channel_mapping, channels, false)) {
  4958. pr_err("%s: map channels failed %d\n",
  4959. __func__, channels);
  4960. return -EINVAL;
  4961. }
  4962. } else {
  4963. memcpy(channel_mapping, channel_map,
  4964. PCM_FORMAT_MAX_NUM_CHANNEL);
  4965. }
  4966. atomic_set(&ac->cmd_state, -1);
  4967. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  4968. if (rc < 0) {
  4969. pr_err("%s: Comamnd open failed %d\n", __func__, rc);
  4970. rc = -EINVAL;
  4971. goto fail_cmd;
  4972. }
  4973. rc = wait_event_timeout(ac->cmd_wait,
  4974. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  4975. if (!rc) {
  4976. pr_err("%s: timeout. waited for format update\n", __func__);
  4977. rc = -ETIMEDOUT;
  4978. goto fail_cmd;
  4979. }
  4980. if (atomic_read(&ac->cmd_state) > 0) {
  4981. pr_err("%s: DSP returned error[%s]\n",
  4982. __func__, adsp_err_get_err_str(
  4983. atomic_read(&ac->cmd_state)));
  4984. rc = adsp_err_get_lnx_err_code(
  4985. atomic_read(&ac->cmd_state));
  4986. }
  4987. return 0;
  4988. fail_cmd:
  4989. return rc;
  4990. }
  4991. EXPORT_SYMBOL(q6asm_media_format_block_gen_compr);
  4992. /*
  4993. * q6asm_media_format_block_iec - set up IEC61937 (compressed) or IEC60958
  4994. * (pcm) format params. Both audio standards
  4995. * use the same format and are used for
  4996. * HDMI or SPDIF.
  4997. *
  4998. * @ac: Client session handle
  4999. * @rate: sample rate
  5000. * @channels: number of channels
  5001. */
  5002. int q6asm_media_format_block_iec(struct audio_client *ac,
  5003. uint32_t rate, uint32_t channels)
  5004. {
  5005. struct asm_iec_compressed_fmt_blk_t fmt;
  5006. int rc = 0;
  5007. pr_debug("%s: session[%d]rate[%d]ch[%d]\n",
  5008. __func__, ac->session, rate,
  5009. channels);
  5010. memset(&fmt, 0, sizeof(fmt));
  5011. q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
  5012. fmt.hdr.opcode = ASM_DATA_CMD_IEC_60958_MEDIA_FMT;
  5013. fmt.num_channels = channels;
  5014. fmt.sampling_rate = rate;
  5015. atomic_set(&ac->cmd_state, -1);
  5016. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  5017. if (rc < 0) {
  5018. pr_err("%s: Comamnd open failed %d\n", __func__, rc);
  5019. rc = -EINVAL;
  5020. goto fail_cmd;
  5021. }
  5022. rc = wait_event_timeout(ac->cmd_wait,
  5023. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  5024. if (!rc) {
  5025. pr_err("%s: timeout. waited for format update\n", __func__);
  5026. rc = -ETIMEDOUT;
  5027. goto fail_cmd;
  5028. }
  5029. if (atomic_read(&ac->cmd_state) > 0) {
  5030. pr_err("%s: DSP returned error[%s]\n",
  5031. __func__, adsp_err_get_err_str(
  5032. atomic_read(&ac->cmd_state)));
  5033. rc = adsp_err_get_lnx_err_code(
  5034. atomic_read(&ac->cmd_state));
  5035. }
  5036. return 0;
  5037. fail_cmd:
  5038. return rc;
  5039. }
  5040. EXPORT_SYMBOL(q6asm_media_format_block_iec);
  5041. static int __q6asm_media_format_block_multi_aac(struct audio_client *ac,
  5042. struct asm_aac_cfg *cfg, int stream_id)
  5043. {
  5044. struct asm_aac_fmt_blk_v2 fmt;
  5045. int rc = 0;
  5046. pr_debug("%s: session[%d]rate[%d]ch[%d]\n", __func__, ac->session,
  5047. cfg->sample_rate, cfg->ch_cfg);
  5048. q6asm_stream_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE, stream_id);
  5049. atomic_set(&ac->cmd_state, -1);
  5050. /*
  5051. * Updated the token field with stream/session for compressed playback
  5052. * Platform driver must know the the stream with which the command is
  5053. * associated
  5054. */
  5055. if (ac->io_mode & COMPRESSED_STREAM_IO)
  5056. q6asm_update_token(&fmt.hdr.token,
  5057. ac->session,
  5058. stream_id,
  5059. 0, /* Buffer index is NA */
  5060. 0, /* Direction flag is NA */
  5061. WAIT_CMD);
  5062. pr_debug("%s: token = 0x%x, stream_id %d, session 0x%x\n",
  5063. __func__, fmt.hdr.token, stream_id, ac->session);
  5064. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  5065. fmt.fmt_blk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  5066. sizeof(fmt.fmt_blk);
  5067. fmt.aac_fmt_flag = cfg->format;
  5068. fmt.audio_objype = cfg->aot;
  5069. /* If zero, PCE is assumed to be available in bitstream*/
  5070. fmt.total_size_of_PCE_bits = 0;
  5071. fmt.channel_config = cfg->ch_cfg;
  5072. fmt.sample_rate = cfg->sample_rate;
  5073. pr_debug("%s: format=0x%x cfg_size=%d aac-cfg=0x%x aot=%d ch=%d sr=%d\n",
  5074. __func__, fmt.aac_fmt_flag, fmt.fmt_blk.fmt_blk_size,
  5075. fmt.aac_fmt_flag,
  5076. fmt.audio_objype,
  5077. fmt.channel_config,
  5078. fmt.sample_rate);
  5079. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  5080. if (rc < 0) {
  5081. pr_err("%s: Comamnd open failed %d\n", __func__, rc);
  5082. rc = -EINVAL;
  5083. goto fail_cmd;
  5084. }
  5085. rc = wait_event_timeout(ac->cmd_wait,
  5086. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  5087. if (!rc) {
  5088. pr_err("%s: timeout. waited for FORMAT_UPDATE\n", __func__);
  5089. rc = -ETIMEDOUT;
  5090. goto fail_cmd;
  5091. }
  5092. if (atomic_read(&ac->cmd_state) > 0) {
  5093. pr_err("%s: DSP returned error[%s]\n",
  5094. __func__, adsp_err_get_err_str(
  5095. atomic_read(&ac->cmd_state)));
  5096. rc = adsp_err_get_lnx_err_code(
  5097. atomic_read(&ac->cmd_state));
  5098. goto fail_cmd;
  5099. }
  5100. return 0;
  5101. fail_cmd:
  5102. return rc;
  5103. }
  5104. int q6asm_media_format_block_multi_aac(struct audio_client *ac,
  5105. struct asm_aac_cfg *cfg)
  5106. {
  5107. return __q6asm_media_format_block_multi_aac(ac, cfg, ac->stream_id);
  5108. }
  5109. int q6asm_media_format_block_aac(struct audio_client *ac,
  5110. struct asm_aac_cfg *cfg)
  5111. {
  5112. return __q6asm_media_format_block_multi_aac(ac, cfg, ac->stream_id);
  5113. }
  5114. int q6asm_stream_media_format_block_aac(struct audio_client *ac,
  5115. struct asm_aac_cfg *cfg, int stream_id)
  5116. {
  5117. return __q6asm_media_format_block_multi_aac(ac, cfg, stream_id);
  5118. }
  5119. int q6asm_media_format_block_wma(struct audio_client *ac,
  5120. void *cfg, int stream_id)
  5121. {
  5122. struct asm_wmastdv9_fmt_blk_v2 fmt;
  5123. struct asm_wma_cfg *wma_cfg = (struct asm_wma_cfg *)cfg;
  5124. int rc = 0;
  5125. pr_debug("session[%d]format_tag[0x%4x] rate[%d] ch[0x%4x] bps[%d], balign[0x%4x], bit_sample[0x%4x], ch_msk[%d], enc_opt[0x%4x]\n",
  5126. ac->session, wma_cfg->format_tag, wma_cfg->sample_rate,
  5127. wma_cfg->ch_cfg, wma_cfg->avg_bytes_per_sec,
  5128. wma_cfg->block_align, wma_cfg->valid_bits_per_sample,
  5129. wma_cfg->ch_mask, wma_cfg->encode_opt);
  5130. q6asm_stream_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE, stream_id);
  5131. atomic_set(&ac->cmd_state, -1);
  5132. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  5133. fmt.fmtblk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  5134. sizeof(fmt.fmtblk);
  5135. fmt.fmtag = wma_cfg->format_tag;
  5136. fmt.num_channels = wma_cfg->ch_cfg;
  5137. fmt.sample_rate = wma_cfg->sample_rate;
  5138. fmt.avg_bytes_per_sec = wma_cfg->avg_bytes_per_sec;
  5139. fmt.blk_align = wma_cfg->block_align;
  5140. fmt.bits_per_sample =
  5141. wma_cfg->valid_bits_per_sample;
  5142. fmt.channel_mask = wma_cfg->ch_mask;
  5143. fmt.enc_options = wma_cfg->encode_opt;
  5144. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  5145. if (rc < 0) {
  5146. pr_err("%s: Comamnd open failed %d\n", __func__, rc);
  5147. rc = -EINVAL;
  5148. goto fail_cmd;
  5149. }
  5150. rc = wait_event_timeout(ac->cmd_wait,
  5151. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  5152. if (!rc) {
  5153. pr_err("%s: timeout. waited for FORMAT_UPDATE\n", __func__);
  5154. rc = -ETIMEDOUT;
  5155. goto fail_cmd;
  5156. }
  5157. if (atomic_read(&ac->cmd_state) > 0) {
  5158. pr_err("%s: DSP returned error[%s]\n",
  5159. __func__, adsp_err_get_err_str(
  5160. atomic_read(&ac->cmd_state)));
  5161. rc = adsp_err_get_lnx_err_code(
  5162. atomic_read(&ac->cmd_state));
  5163. goto fail_cmd;
  5164. }
  5165. return 0;
  5166. fail_cmd:
  5167. return rc;
  5168. }
  5169. int q6asm_media_format_block_wmapro(struct audio_client *ac,
  5170. void *cfg, int stream_id)
  5171. {
  5172. struct asm_wmaprov10_fmt_blk_v2 fmt;
  5173. struct asm_wmapro_cfg *wmapro_cfg = (struct asm_wmapro_cfg *)cfg;
  5174. int rc = 0;
  5175. pr_debug("%s: session[%d]format_tag[0x%4x] rate[%d] ch[0x%4x] bps[%d], balign[0x%4x], bit_sample[0x%4x], ch_msk[%d], enc_opt[0x%4x], adv_enc_opt[0x%4x], adv_enc_opt2[0x%8x]\n",
  5176. __func__,
  5177. ac->session, wmapro_cfg->format_tag, wmapro_cfg->sample_rate,
  5178. wmapro_cfg->ch_cfg, wmapro_cfg->avg_bytes_per_sec,
  5179. wmapro_cfg->block_align, wmapro_cfg->valid_bits_per_sample,
  5180. wmapro_cfg->ch_mask, wmapro_cfg->encode_opt,
  5181. wmapro_cfg->adv_encode_opt, wmapro_cfg->adv_encode_opt2);
  5182. q6asm_stream_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE, stream_id);
  5183. atomic_set(&ac->cmd_state, -1);
  5184. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  5185. fmt.fmtblk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  5186. sizeof(fmt.fmtblk);
  5187. fmt.fmtag = wmapro_cfg->format_tag;
  5188. fmt.num_channels = wmapro_cfg->ch_cfg;
  5189. fmt.sample_rate = wmapro_cfg->sample_rate;
  5190. fmt.avg_bytes_per_sec =
  5191. wmapro_cfg->avg_bytes_per_sec;
  5192. fmt.blk_align = wmapro_cfg->block_align;
  5193. fmt.bits_per_sample = wmapro_cfg->valid_bits_per_sample;
  5194. fmt.channel_mask = wmapro_cfg->ch_mask;
  5195. fmt.enc_options = wmapro_cfg->encode_opt;
  5196. fmt.usAdvancedEncodeOpt = wmapro_cfg->adv_encode_opt;
  5197. fmt.advanced_enc_options2 = wmapro_cfg->adv_encode_opt2;
  5198. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  5199. if (rc < 0) {
  5200. pr_err("%s: Comamnd open failed %d\n", __func__, rc);
  5201. rc = -EINVAL;
  5202. goto fail_cmd;
  5203. }
  5204. rc = wait_event_timeout(ac->cmd_wait,
  5205. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  5206. if (!rc) {
  5207. pr_err("%s: timeout. waited for FORMAT_UPDATE\n", __func__);
  5208. rc = -ETIMEDOUT;
  5209. goto fail_cmd;
  5210. }
  5211. if (atomic_read(&ac->cmd_state) > 0) {
  5212. pr_err("%s: DSP returned error[%s]\n",
  5213. __func__, adsp_err_get_err_str(
  5214. atomic_read(&ac->cmd_state)));
  5215. rc = adsp_err_get_lnx_err_code(
  5216. atomic_read(&ac->cmd_state));
  5217. goto fail_cmd;
  5218. }
  5219. return 0;
  5220. fail_cmd:
  5221. return rc;
  5222. }
  5223. int q6asm_media_format_block_amrwbplus(struct audio_client *ac,
  5224. struct asm_amrwbplus_cfg *cfg)
  5225. {
  5226. struct asm_amrwbplus_fmt_blk_v2 fmt;
  5227. int rc = 0;
  5228. pr_debug("%s: session[%d]band-mode[%d]frame-fmt[%d]ch[%d]\n",
  5229. __func__,
  5230. ac->session,
  5231. cfg->amr_band_mode,
  5232. cfg->amr_frame_fmt,
  5233. cfg->num_channels);
  5234. q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
  5235. atomic_set(&ac->cmd_state, -1);
  5236. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  5237. fmt.fmtblk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  5238. sizeof(fmt.fmtblk);
  5239. fmt.amr_frame_fmt = cfg->amr_frame_fmt;
  5240. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  5241. if (rc < 0) {
  5242. pr_err("%s: Comamnd media format update failed.. %d\n",
  5243. __func__, rc);
  5244. rc = -EINVAL;
  5245. goto fail_cmd;
  5246. }
  5247. rc = wait_event_timeout(ac->cmd_wait,
  5248. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  5249. if (!rc) {
  5250. pr_err("%s: timeout. waited for FORMAT_UPDATE\n", __func__);
  5251. rc = -ETIMEDOUT;
  5252. goto fail_cmd;
  5253. }
  5254. if (atomic_read(&ac->cmd_state) > 0) {
  5255. pr_err("%s: DSP returned error[%s]\n",
  5256. __func__, adsp_err_get_err_str(
  5257. atomic_read(&ac->cmd_state)));
  5258. rc = adsp_err_get_lnx_err_code(
  5259. atomic_read(&ac->cmd_state));
  5260. goto fail_cmd;
  5261. }
  5262. return 0;
  5263. fail_cmd:
  5264. return rc;
  5265. }
  5266. int q6asm_stream_media_format_block_flac(struct audio_client *ac,
  5267. struct asm_flac_cfg *cfg, int stream_id)
  5268. {
  5269. struct asm_flac_fmt_blk_v2 fmt;
  5270. int rc = 0;
  5271. pr_debug("%s :session[%d] rate[%d] ch[%d] size[%d] stream_id[%d]\n",
  5272. __func__, ac->session, cfg->sample_rate, cfg->ch_cfg,
  5273. cfg->sample_size, stream_id);
  5274. q6asm_stream_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE, stream_id);
  5275. atomic_set(&ac->cmd_state, -1);
  5276. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  5277. fmt.fmtblk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  5278. sizeof(fmt.fmtblk);
  5279. fmt.is_stream_info_present = cfg->stream_info_present;
  5280. fmt.num_channels = cfg->ch_cfg;
  5281. fmt.min_blk_size = cfg->min_blk_size;
  5282. fmt.max_blk_size = cfg->max_blk_size;
  5283. fmt.sample_rate = cfg->sample_rate;
  5284. fmt.min_frame_size = cfg->min_frame_size;
  5285. fmt.max_frame_size = cfg->max_frame_size;
  5286. fmt.sample_size = cfg->sample_size;
  5287. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  5288. if (rc < 0) {
  5289. pr_err("%s :Comamnd media format update failed %d\n",
  5290. __func__, rc);
  5291. goto fail_cmd;
  5292. }
  5293. rc = wait_event_timeout(ac->cmd_wait,
  5294. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  5295. if (!rc) {
  5296. pr_err("%s :timeout. waited for FORMAT_UPDATE\n", __func__);
  5297. rc = -ETIMEDOUT;
  5298. goto fail_cmd;
  5299. }
  5300. if (atomic_read(&ac->cmd_state) > 0) {
  5301. pr_err("%s: DSP returned error[%s]\n",
  5302. __func__, adsp_err_get_err_str(
  5303. atomic_read(&ac->cmd_state)));
  5304. rc = adsp_err_get_lnx_err_code(
  5305. atomic_read(&ac->cmd_state));
  5306. goto fail_cmd;
  5307. }
  5308. return 0;
  5309. fail_cmd:
  5310. return rc;
  5311. }
  5312. int q6asm_media_format_block_alac(struct audio_client *ac,
  5313. struct asm_alac_cfg *cfg, int stream_id)
  5314. {
  5315. struct asm_alac_fmt_blk_v2 fmt;
  5316. int rc = 0;
  5317. pr_debug("%s :session[%d]rate[%d]ch[%d]\n", __func__,
  5318. ac->session, cfg->sample_rate, cfg->num_channels);
  5319. q6asm_stream_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE, stream_id);
  5320. atomic_set(&ac->cmd_state, -1);
  5321. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  5322. fmt.fmtblk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  5323. sizeof(fmt.fmtblk);
  5324. fmt.frame_length = cfg->frame_length;
  5325. fmt.compatible_version = cfg->compatible_version;
  5326. fmt.bit_depth = cfg->bit_depth;
  5327. fmt.pb = cfg->pb;
  5328. fmt.mb = cfg->mb;
  5329. fmt.kb = cfg->kb;
  5330. fmt.num_channels = cfg->num_channels;
  5331. fmt.max_run = cfg->max_run;
  5332. fmt.max_frame_bytes = cfg->max_frame_bytes;
  5333. fmt.avg_bit_rate = cfg->avg_bit_rate;
  5334. fmt.sample_rate = cfg->sample_rate;
  5335. fmt.channel_layout_tag = cfg->channel_layout_tag;
  5336. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  5337. if (rc < 0) {
  5338. pr_err("%s :Comamnd media format update failed %d\n",
  5339. __func__, rc);
  5340. goto fail_cmd;
  5341. }
  5342. rc = wait_event_timeout(ac->cmd_wait,
  5343. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  5344. if (!rc) {
  5345. pr_err("%s :timeout. waited for FORMAT_UPDATE\n", __func__);
  5346. rc = -ETIMEDOUT;
  5347. goto fail_cmd;
  5348. }
  5349. if (atomic_read(&ac->cmd_state) > 0) {
  5350. pr_err("%s: DSP returned error[%s]\n",
  5351. __func__, adsp_err_get_err_str(
  5352. atomic_read(&ac->cmd_state)));
  5353. rc = adsp_err_get_lnx_err_code(
  5354. atomic_read(&ac->cmd_state));
  5355. goto fail_cmd;
  5356. }
  5357. return 0;
  5358. fail_cmd:
  5359. return rc;
  5360. }
  5361. /*
  5362. * q6asm_media_format_block_g711 - sends g711 decoder configuration
  5363. * parameters
  5364. * @ac: Client session handle
  5365. * @cfg: Audio stream manager configuration parameters
  5366. * @stream_id: Stream id
  5367. */
  5368. int q6asm_media_format_block_g711(struct audio_client *ac,
  5369. struct asm_g711_dec_cfg *cfg, int stream_id)
  5370. {
  5371. struct asm_g711_dec_fmt_blk_v2 fmt;
  5372. int rc = 0;
  5373. if (!ac) {
  5374. pr_err("%s: audio client is null\n", __func__);
  5375. return -EINVAL;
  5376. }
  5377. if (!cfg) {
  5378. pr_err("%s: Invalid ASM config\n", __func__);
  5379. return -EINVAL;
  5380. }
  5381. if (stream_id <= 0) {
  5382. pr_err("%s: Invalid stream id\n", __func__);
  5383. return -EINVAL;
  5384. }
  5385. pr_debug("%s :session[%d]rate[%d]\n", __func__,
  5386. ac->session, cfg->sample_rate);
  5387. memset(&fmt, 0, sizeof(struct asm_g711_dec_fmt_blk_v2));
  5388. q6asm_stream_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE, stream_id);
  5389. atomic_set(&ac->cmd_state, -1);
  5390. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  5391. fmt.fmtblk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  5392. sizeof(fmt.fmtblk);
  5393. fmt.sample_rate = cfg->sample_rate;
  5394. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  5395. if (rc < 0) {
  5396. pr_err("%s :Command media format update failed %d\n",
  5397. __func__, rc);
  5398. goto fail_cmd;
  5399. }
  5400. rc = wait_event_timeout(ac->cmd_wait,
  5401. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  5402. if (!rc) {
  5403. pr_err("%s :timeout. waited for FORMAT_UPDATE\n", __func__);
  5404. rc = -ETIMEDOUT;
  5405. goto fail_cmd;
  5406. }
  5407. if (atomic_read(&ac->cmd_state) > 0) {
  5408. pr_err("%s: DSP returned error[%s]\n",
  5409. __func__, adsp_err_get_err_str(
  5410. atomic_read(&ac->cmd_state)));
  5411. rc = adsp_err_get_lnx_err_code(
  5412. atomic_read(&ac->cmd_state));
  5413. goto fail_cmd;
  5414. }
  5415. return 0;
  5416. fail_cmd:
  5417. return rc;
  5418. }
  5419. EXPORT_SYMBOL(q6asm_media_format_block_g711);
  5420. int q6asm_stream_media_format_block_vorbis(struct audio_client *ac,
  5421. struct asm_vorbis_cfg *cfg, int stream_id)
  5422. {
  5423. struct asm_vorbis_fmt_blk_v2 fmt;
  5424. int rc = 0;
  5425. pr_debug("%s :session[%d] bit_stream_fmt[%d] stream_id[%d]\n",
  5426. __func__, ac->session, cfg->bit_stream_fmt, stream_id);
  5427. q6asm_stream_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE, stream_id);
  5428. atomic_set(&ac->cmd_state, -1);
  5429. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  5430. fmt.fmtblk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  5431. sizeof(fmt.fmtblk);
  5432. fmt.bit_stream_fmt = cfg->bit_stream_fmt;
  5433. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  5434. if (rc < 0) {
  5435. pr_err("%s :Comamnd media format update failed %d\n",
  5436. __func__, rc);
  5437. goto fail_cmd;
  5438. }
  5439. rc = wait_event_timeout(ac->cmd_wait,
  5440. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  5441. if (!rc) {
  5442. pr_err("%s :timeout. waited for FORMAT_UPDATE\n", __func__);
  5443. rc = -ETIMEDOUT;
  5444. goto fail_cmd;
  5445. }
  5446. if (atomic_read(&ac->cmd_state) > 0) {
  5447. pr_err("%s: DSP returned error[%s]\n",
  5448. __func__, adsp_err_get_err_str(
  5449. atomic_read(&ac->cmd_state)));
  5450. rc = adsp_err_get_lnx_err_code(
  5451. atomic_read(&ac->cmd_state));
  5452. goto fail_cmd;
  5453. }
  5454. return 0;
  5455. fail_cmd:
  5456. return rc;
  5457. }
  5458. int q6asm_media_format_block_ape(struct audio_client *ac,
  5459. struct asm_ape_cfg *cfg, int stream_id)
  5460. {
  5461. struct asm_ape_fmt_blk_v2 fmt;
  5462. int rc = 0;
  5463. pr_debug("%s :session[%d]rate[%d]ch[%d]\n", __func__,
  5464. ac->session, cfg->sample_rate, cfg->num_channels);
  5465. q6asm_stream_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE, stream_id);
  5466. atomic_set(&ac->cmd_state, -1);
  5467. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  5468. fmt.fmtblk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  5469. sizeof(fmt.fmtblk);
  5470. fmt.compatible_version = cfg->compatible_version;
  5471. fmt.compression_level = cfg->compression_level;
  5472. fmt.format_flags = cfg->format_flags;
  5473. fmt.blocks_per_frame = cfg->blocks_per_frame;
  5474. fmt.final_frame_blocks = cfg->final_frame_blocks;
  5475. fmt.total_frames = cfg->total_frames;
  5476. fmt.bits_per_sample = cfg->bits_per_sample;
  5477. fmt.num_channels = cfg->num_channels;
  5478. fmt.sample_rate = cfg->sample_rate;
  5479. fmt.seek_table_present = cfg->seek_table_present;
  5480. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  5481. if (rc < 0) {
  5482. pr_err("%s :Comamnd media format update failed %d\n",
  5483. __func__, rc);
  5484. goto fail_cmd;
  5485. }
  5486. rc = wait_event_timeout(ac->cmd_wait,
  5487. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  5488. if (!rc) {
  5489. pr_err("%s :timeout. waited for FORMAT_UPDATE\n", __func__);
  5490. rc = -ETIMEDOUT;
  5491. goto fail_cmd;
  5492. }
  5493. if (atomic_read(&ac->cmd_state) > 0) {
  5494. pr_err("%s: DSP returned error[%s]\n",
  5495. __func__, adsp_err_get_err_str(
  5496. atomic_read(&ac->cmd_state)));
  5497. rc = adsp_err_get_lnx_err_code(
  5498. atomic_read(&ac->cmd_state));
  5499. goto fail_cmd;
  5500. }
  5501. return 0;
  5502. fail_cmd:
  5503. return rc;
  5504. }
  5505. /*
  5506. * q6asm_media_format_block_dsd- Sends DSD Decoder
  5507. * configuration parameters
  5508. *
  5509. * @ac: Client session handle
  5510. * @cfg: DSD Media Format Configuration.
  5511. * @stream_id: stream id of stream to be associated with this session
  5512. *
  5513. * Return 0 on success or negative error code on failure
  5514. */
  5515. int q6asm_media_format_block_dsd(struct audio_client *ac,
  5516. struct asm_dsd_cfg *cfg, int stream_id)
  5517. {
  5518. struct asm_dsd_fmt_blk_v2 fmt;
  5519. int rc;
  5520. pr_debug("%s: session[%d] data_rate[%d] ch[%d]\n", __func__,
  5521. ac->session, cfg->dsd_data_rate, cfg->num_channels);
  5522. memset(&fmt, 0, sizeof(fmt));
  5523. q6asm_stream_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE, stream_id);
  5524. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  5525. fmt.fmtblk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  5526. sizeof(fmt.fmtblk);
  5527. fmt.num_version = cfg->num_version;
  5528. fmt.is_bitwise_big_endian = cfg->is_bitwise_big_endian;
  5529. fmt.dsd_channel_block_size = cfg->dsd_channel_block_size;
  5530. fmt.num_channels = cfg->num_channels;
  5531. fmt.dsd_data_rate = cfg->dsd_data_rate;
  5532. atomic_set(&ac->cmd_state, -1);
  5533. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  5534. if (rc < 0) {
  5535. pr_err("%s: Command DSD media format update failed, err: %d\n",
  5536. __func__, rc);
  5537. goto done;
  5538. }
  5539. rc = wait_event_timeout(ac->cmd_wait,
  5540. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  5541. if (!rc) {
  5542. pr_err("%s: timeout. waited for DSD FORMAT_UPDATE\n", __func__);
  5543. rc = -ETIMEDOUT;
  5544. goto done;
  5545. }
  5546. if (atomic_read(&ac->cmd_state) > 0) {
  5547. pr_err("%s: DSP returned error[%s]\n",
  5548. __func__, adsp_err_get_err_str(
  5549. atomic_read(&ac->cmd_state)));
  5550. rc = adsp_err_get_lnx_err_code(
  5551. atomic_read(&ac->cmd_state));
  5552. goto done;
  5553. }
  5554. return 0;
  5555. done:
  5556. return rc;
  5557. }
  5558. EXPORT_SYMBOL(q6asm_media_format_block_dsd);
  5559. int q6asm_stream_media_format_block_aptx_dec(struct audio_client *ac,
  5560. uint32_t srate, int stream_id)
  5561. {
  5562. struct asm_aptx_dec_fmt_blk_v2 aptx_fmt;
  5563. int rc = 0;
  5564. if (!ac->session) {
  5565. pr_err("%s: ac session invalid\n", __func__);
  5566. rc = -EINVAL;
  5567. goto fail_cmd;
  5568. }
  5569. pr_debug("%s :session[%d] rate[%d] stream_id[%d]\n",
  5570. __func__, ac->session, srate, stream_id);
  5571. q6asm_stream_add_hdr(ac, &aptx_fmt.hdr, sizeof(aptx_fmt), TRUE,
  5572. stream_id);
  5573. atomic_set(&ac->cmd_state, -1);
  5574. aptx_fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  5575. aptx_fmt.fmtblk.fmt_blk_size = sizeof(aptx_fmt) - sizeof(aptx_fmt.hdr) -
  5576. sizeof(aptx_fmt.fmtblk);
  5577. aptx_fmt.sample_rate = srate;
  5578. rc = apr_send_pkt(ac->apr, (uint32_t *) &aptx_fmt);
  5579. if (rc < 0) {
  5580. pr_err("%s :Comamnd media format update failed %d\n",
  5581. __func__, rc);
  5582. goto fail_cmd;
  5583. }
  5584. rc = wait_event_timeout(ac->cmd_wait,
  5585. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  5586. if (!rc) {
  5587. pr_err("%s :timeout. waited for FORMAT_UPDATE\n", __func__);
  5588. rc = -ETIMEDOUT;
  5589. goto fail_cmd;
  5590. }
  5591. if (atomic_read(&ac->cmd_state) > 0) {
  5592. pr_err("%s: DSP returned error[%s]\n",
  5593. __func__, adsp_err_get_err_str(
  5594. atomic_read(&ac->cmd_state)));
  5595. rc = adsp_err_get_lnx_err_code(
  5596. atomic_read(&ac->cmd_state));
  5597. goto fail_cmd;
  5598. }
  5599. rc = 0;
  5600. fail_cmd:
  5601. return rc;
  5602. }
  5603. static int __q6asm_ds1_set_endp_params(struct audio_client *ac, int param_id,
  5604. int param_value, int stream_id)
  5605. {
  5606. struct asm_dec_ddp_endp_param_v2 ddp_cfg;
  5607. int rc = 0;
  5608. pr_debug("%s: session[%d] stream[%d],param_id[%d]param_value[%d]",
  5609. __func__, ac->session, stream_id, param_id, param_value);
  5610. q6asm_stream_add_hdr(ac, &ddp_cfg.hdr, sizeof(ddp_cfg), TRUE,
  5611. stream_id);
  5612. atomic_set(&ac->cmd_state, -1);
  5613. /*
  5614. * Updated the token field with stream/session for compressed playback
  5615. * Platform driver must know the stream with which the command is
  5616. * associated
  5617. */
  5618. if (ac->io_mode & COMPRESSED_STREAM_IO)
  5619. q6asm_update_token(&ddp_cfg.hdr.token,
  5620. ac->session,
  5621. stream_id,
  5622. 0, /* Buffer index is NA */
  5623. 0, /* Direction flag is NA */
  5624. WAIT_CMD);
  5625. ddp_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  5626. ddp_cfg.encdec.param_id = param_id;
  5627. ddp_cfg.encdec.param_size = sizeof(struct asm_dec_ddp_endp_param_v2) -
  5628. (sizeof(struct apr_hdr) +
  5629. sizeof(struct asm_stream_cmd_set_encdec_param));
  5630. ddp_cfg.endp_param_value = param_value;
  5631. rc = apr_send_pkt(ac->apr, (uint32_t *) &ddp_cfg);
  5632. if (rc < 0) {
  5633. pr_err("%s: Command opcode[0x%x] failed %d\n",
  5634. __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM, rc);
  5635. goto fail_cmd;
  5636. }
  5637. rc = wait_event_timeout(ac->cmd_wait,
  5638. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  5639. if (!rc) {
  5640. pr_err("%s: timeout opcode[0x%x]\n", __func__,
  5641. ddp_cfg.hdr.opcode);
  5642. rc = -ETIMEDOUT;
  5643. goto fail_cmd;
  5644. }
  5645. if (atomic_read(&ac->cmd_state) > 0) {
  5646. pr_err("%s: DSP returned error[%s]\n",
  5647. __func__, adsp_err_get_err_str(
  5648. atomic_read(&ac->cmd_state)));
  5649. rc = adsp_err_get_lnx_err_code(
  5650. atomic_read(&ac->cmd_state));
  5651. goto fail_cmd;
  5652. }
  5653. return 0;
  5654. fail_cmd:
  5655. return rc;
  5656. }
  5657. int q6asm_ds1_set_endp_params(struct audio_client *ac,
  5658. int param_id, int param_value)
  5659. {
  5660. return __q6asm_ds1_set_endp_params(ac, param_id, param_value,
  5661. ac->stream_id);
  5662. }
  5663. int q6asm_ds1_set_stream_endp_params(struct audio_client *ac,
  5664. int param_id, int param_value,
  5665. int stream_id)
  5666. {
  5667. return __q6asm_ds1_set_endp_params(ac, param_id, param_value,
  5668. stream_id);
  5669. }
  5670. int q6asm_memory_map(struct audio_client *ac, phys_addr_t buf_add, int dir,
  5671. uint32_t bufsz, uint32_t bufcnt)
  5672. {
  5673. struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
  5674. struct avs_shared_map_region_payload *mregions = NULL;
  5675. struct audio_port_data *port = NULL;
  5676. void *mmap_region_cmd = NULL;
  5677. void *payload = NULL;
  5678. struct asm_buffer_node *buffer_node = NULL;
  5679. int rc = 0;
  5680. int cmd_size = 0;
  5681. if (!ac) {
  5682. pr_err("%s: APR handle NULL\n", __func__);
  5683. return -EINVAL;
  5684. }
  5685. if (ac->mmap_apr == NULL) {
  5686. pr_err("%s: mmap APR handle NULL\n", __func__);
  5687. return -EINVAL;
  5688. }
  5689. pr_debug("%s: Session[%d]\n", __func__, ac->session);
  5690. buffer_node = kmalloc(sizeof(struct asm_buffer_node), GFP_KERNEL);
  5691. if (!buffer_node)
  5692. return -ENOMEM;
  5693. cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
  5694. + sizeof(struct avs_shared_map_region_payload) * bufcnt;
  5695. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  5696. if (mmap_region_cmd == NULL) {
  5697. rc = -EINVAL;
  5698. kfree(buffer_node);
  5699. return rc;
  5700. }
  5701. mmap_regions = (struct avs_cmd_shared_mem_map_regions *)
  5702. mmap_region_cmd;
  5703. q6asm_add_mmaphdr(ac, &mmap_regions->hdr, cmd_size, dir);
  5704. atomic_set(&ac->mem_state, -1);
  5705. mmap_regions->hdr.opcode = ASM_CMD_SHARED_MEM_MAP_REGIONS;
  5706. mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
  5707. mmap_regions->num_regions = bufcnt & 0x00ff;
  5708. mmap_regions->property_flag = 0x00;
  5709. payload = ((u8 *) mmap_region_cmd +
  5710. sizeof(struct avs_cmd_shared_mem_map_regions));
  5711. mregions = (struct avs_shared_map_region_payload *)payload;
  5712. ac->port[dir].tmp_hdl = 0;
  5713. port = &ac->port[dir];
  5714. pr_debug("%s: buf_add 0x%pK, bufsz: %d\n", __func__,
  5715. &buf_add, bufsz);
  5716. mregions->shm_addr_lsw = lower_32_bits(buf_add);
  5717. mregions->shm_addr_msw = msm_audio_populate_upper_32_bits(buf_add);
  5718. mregions->mem_size_bytes = bufsz;
  5719. ++mregions;
  5720. rc = apr_send_pkt(ac->mmap_apr, (uint32_t *) mmap_region_cmd);
  5721. if (rc < 0) {
  5722. pr_err("%s: mmap op[0x%x]rc[%d]\n", __func__,
  5723. mmap_regions->hdr.opcode, rc);
  5724. rc = -EINVAL;
  5725. kfree(buffer_node);
  5726. goto fail_cmd;
  5727. }
  5728. rc = wait_event_timeout(ac->mem_wait,
  5729. (atomic_read(&ac->mem_state) >= 0 &&
  5730. ac->port[dir].tmp_hdl), 5*HZ);
  5731. if (!rc) {
  5732. pr_err("%s: timeout. waited for memory_map\n", __func__);
  5733. rc = -ETIMEDOUT;
  5734. kfree(buffer_node);
  5735. goto fail_cmd;
  5736. }
  5737. if (atomic_read(&ac->mem_state) > 0) {
  5738. pr_err("%s: DSP returned error[%s] for memory_map\n",
  5739. __func__, adsp_err_get_err_str(
  5740. atomic_read(&ac->mem_state)));
  5741. rc = adsp_err_get_lnx_err_code(
  5742. atomic_read(&ac->mem_state));
  5743. kfree(buffer_node);
  5744. goto fail_cmd;
  5745. }
  5746. buffer_node->buf_phys_addr = buf_add;
  5747. buffer_node->mmap_hdl = ac->port[dir].tmp_hdl;
  5748. list_add_tail(&buffer_node->list, &ac->port[dir].mem_map_handle);
  5749. ac->port[dir].tmp_hdl = 0;
  5750. rc = 0;
  5751. fail_cmd:
  5752. kfree(mmap_region_cmd);
  5753. return rc;
  5754. }
  5755. int q6asm_memory_unmap(struct audio_client *ac, phys_addr_t buf_add, int dir)
  5756. {
  5757. struct avs_cmd_shared_mem_unmap_regions mem_unmap;
  5758. struct asm_buffer_node *buf_node = NULL;
  5759. struct list_head *ptr, *next;
  5760. int rc = 0;
  5761. if (!ac) {
  5762. pr_err("%s: APR handle NULL\n", __func__);
  5763. return -EINVAL;
  5764. }
  5765. if (this_mmap.apr == NULL) {
  5766. pr_err("%s: APR handle NULL\n", __func__);
  5767. return -EINVAL;
  5768. }
  5769. pr_debug("%s: Session[%d]\n", __func__, ac->session);
  5770. q6asm_add_mmaphdr(ac, &mem_unmap.hdr,
  5771. sizeof(struct avs_cmd_shared_mem_unmap_regions),
  5772. dir);
  5773. atomic_set(&ac->mem_state, -1);
  5774. mem_unmap.hdr.opcode = ASM_CMD_SHARED_MEM_UNMAP_REGIONS;
  5775. mem_unmap.mem_map_handle = 0;
  5776. list_for_each_safe(ptr, next, &ac->port[dir].mem_map_handle) {
  5777. buf_node = list_entry(ptr, struct asm_buffer_node,
  5778. list);
  5779. if (buf_node->buf_phys_addr == buf_add) {
  5780. pr_debug("%s: Found the element\n", __func__);
  5781. mem_unmap.mem_map_handle = buf_node->mmap_hdl;
  5782. break;
  5783. }
  5784. }
  5785. pr_debug("%s: mem_unmap-mem_map_handle: 0x%x\n",
  5786. __func__, mem_unmap.mem_map_handle);
  5787. if (mem_unmap.mem_map_handle == 0) {
  5788. pr_err("%s: Do not send null mem handle to DSP\n", __func__);
  5789. rc = 0;
  5790. goto fail_cmd;
  5791. }
  5792. rc = apr_send_pkt(ac->mmap_apr, (uint32_t *) &mem_unmap);
  5793. if (rc < 0) {
  5794. pr_err("%s: mem_unmap op[0x%x]rc[%d]\n", __func__,
  5795. mem_unmap.hdr.opcode, rc);
  5796. rc = -EINVAL;
  5797. goto fail_cmd;
  5798. }
  5799. rc = wait_event_timeout(ac->mem_wait,
  5800. (atomic_read(&ac->mem_state) >= 0), 5 * HZ);
  5801. if (!rc) {
  5802. pr_err("%s: timeout. waited for memory_unmap of handle 0x%x\n",
  5803. __func__, mem_unmap.mem_map_handle);
  5804. rc = -ETIMEDOUT;
  5805. goto fail_cmd;
  5806. } else if (atomic_read(&ac->mem_state) > 0) {
  5807. pr_err("%s DSP returned error [%s] map handle 0x%x\n",
  5808. __func__, adsp_err_get_err_str(
  5809. atomic_read(&ac->mem_state)),
  5810. mem_unmap.mem_map_handle);
  5811. rc = adsp_err_get_lnx_err_code(
  5812. atomic_read(&ac->mem_state));
  5813. goto fail_cmd;
  5814. } else if (atomic_read(&ac->unmap_cb_success) == 0) {
  5815. pr_err("%s: Error in mem unmap callback of handle 0x%x\n",
  5816. __func__, mem_unmap.mem_map_handle);
  5817. rc = -EINVAL;
  5818. goto fail_cmd;
  5819. }
  5820. rc = 0;
  5821. fail_cmd:
  5822. list_for_each_safe(ptr, next, &ac->port[dir].mem_map_handle) {
  5823. buf_node = list_entry(ptr, struct asm_buffer_node,
  5824. list);
  5825. if (buf_node->buf_phys_addr == buf_add) {
  5826. list_del(&buf_node->list);
  5827. kfree(buf_node);
  5828. break;
  5829. }
  5830. }
  5831. return rc;
  5832. }
  5833. static int q6asm_memory_map_regions(struct audio_client *ac, int dir,
  5834. uint32_t bufsz, uint32_t bufcnt,
  5835. bool is_contiguous)
  5836. {
  5837. struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
  5838. struct avs_shared_map_region_payload *mregions = NULL;
  5839. struct audio_port_data *port = NULL;
  5840. struct audio_buffer *ab = NULL;
  5841. void *mmap_region_cmd = NULL;
  5842. void *payload = NULL;
  5843. struct asm_buffer_node *buffer_node = NULL;
  5844. int rc = 0;
  5845. int i = 0;
  5846. uint32_t cmd_size = 0;
  5847. uint32_t bufcnt_t;
  5848. uint32_t bufsz_t;
  5849. if (!ac) {
  5850. pr_err("%s: APR handle NULL\n", __func__);
  5851. return -EINVAL;
  5852. }
  5853. if (ac->mmap_apr == NULL) {
  5854. pr_err("%s: mmap APR handle NULL\n", __func__);
  5855. return -EINVAL;
  5856. }
  5857. pr_debug("%s: Session[%d]\n", __func__, ac->session);
  5858. bufcnt_t = (is_contiguous) ? 1 : bufcnt;
  5859. bufsz_t = (is_contiguous) ? (bufsz * bufcnt) : bufsz;
  5860. if (is_contiguous) {
  5861. /* The size to memory map should be multiple of 4K bytes */
  5862. bufsz_t = PAGE_ALIGN(bufsz_t);
  5863. }
  5864. if (bufcnt_t > (UINT_MAX
  5865. - sizeof(struct avs_cmd_shared_mem_map_regions))
  5866. / sizeof(struct avs_shared_map_region_payload)) {
  5867. pr_err("%s: Unsigned Integer Overflow. bufcnt_t = %u\n",
  5868. __func__, bufcnt_t);
  5869. return -EINVAL;
  5870. }
  5871. cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
  5872. + (sizeof(struct avs_shared_map_region_payload)
  5873. * bufcnt_t);
  5874. if (bufcnt > (UINT_MAX / sizeof(struct asm_buffer_node))) {
  5875. pr_err("%s: Unsigned Integer Overflow. bufcnt = %u\n",
  5876. __func__, bufcnt);
  5877. return -EINVAL;
  5878. }
  5879. buffer_node = kzalloc(sizeof(struct asm_buffer_node) * bufcnt,
  5880. GFP_KERNEL);
  5881. if (!buffer_node)
  5882. return -ENOMEM;
  5883. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  5884. if (mmap_region_cmd == NULL) {
  5885. rc = -EINVAL;
  5886. kfree(buffer_node);
  5887. return rc;
  5888. }
  5889. mmap_regions = (struct avs_cmd_shared_mem_map_regions *)
  5890. mmap_region_cmd;
  5891. q6asm_add_mmaphdr(ac, &mmap_regions->hdr, cmd_size, dir);
  5892. atomic_set(&ac->mem_state, -1);
  5893. pr_debug("%s: mmap_region=0x%pK token=0x%x\n", __func__,
  5894. mmap_regions, ((ac->session << 8) | dir));
  5895. mmap_regions->hdr.opcode = ASM_CMD_SHARED_MEM_MAP_REGIONS;
  5896. mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
  5897. mmap_regions->num_regions = bufcnt_t; /*bufcnt & 0x00ff; */
  5898. mmap_regions->property_flag = 0x00;
  5899. pr_debug("%s: map_regions->nregions = %d\n", __func__,
  5900. mmap_regions->num_regions);
  5901. payload = ((u8 *) mmap_region_cmd +
  5902. sizeof(struct avs_cmd_shared_mem_map_regions));
  5903. mregions = (struct avs_shared_map_region_payload *)payload;
  5904. ac->port[dir].tmp_hdl = 0;
  5905. port = &ac->port[dir];
  5906. for (i = 0; i < bufcnt_t; i++) {
  5907. ab = &port->buf[i];
  5908. mregions->shm_addr_lsw = lower_32_bits(ab->phys);
  5909. mregions->shm_addr_msw =
  5910. msm_audio_populate_upper_32_bits(ab->phys);
  5911. mregions->mem_size_bytes = bufsz_t;
  5912. ++mregions;
  5913. }
  5914. rc = apr_send_pkt(ac->mmap_apr, (uint32_t *) mmap_region_cmd);
  5915. if (rc < 0) {
  5916. pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__,
  5917. mmap_regions->hdr.opcode, rc);
  5918. rc = -EINVAL;
  5919. kfree(buffer_node);
  5920. goto fail_cmd;
  5921. }
  5922. rc = wait_event_timeout(ac->mem_wait,
  5923. (atomic_read(&ac->mem_state) >= 0)
  5924. , 5*HZ);
  5925. if (!rc) {
  5926. pr_err("%s: timeout. waited for memory_map\n", __func__);
  5927. rc = -ETIMEDOUT;
  5928. kfree(buffer_node);
  5929. goto fail_cmd;
  5930. }
  5931. if (atomic_read(&ac->mem_state) > 0) {
  5932. pr_err("%s DSP returned error for memory_map [%s]\n",
  5933. __func__, adsp_err_get_err_str(
  5934. atomic_read(&ac->mem_state)));
  5935. rc = adsp_err_get_lnx_err_code(
  5936. atomic_read(&ac->mem_state));
  5937. kfree(buffer_node);
  5938. goto fail_cmd;
  5939. }
  5940. mutex_lock(&ac->cmd_lock);
  5941. for (i = 0; i < bufcnt; i++) {
  5942. ab = &port->buf[i];
  5943. buffer_node[i].buf_phys_addr = ab->phys;
  5944. buffer_node[i].mmap_hdl = ac->port[dir].tmp_hdl;
  5945. list_add_tail(&buffer_node[i].list,
  5946. &ac->port[dir].mem_map_handle);
  5947. pr_debug("%s: i=%d, bufadd[i] = 0x%pK, maphdl[i] = 0x%x\n",
  5948. __func__, i, &buffer_node[i].buf_phys_addr,
  5949. buffer_node[i].mmap_hdl);
  5950. }
  5951. ac->port[dir].tmp_hdl = 0;
  5952. mutex_unlock(&ac->cmd_lock);
  5953. rc = 0;
  5954. fail_cmd:
  5955. kfree(mmap_region_cmd);
  5956. return rc;
  5957. }
  5958. static int q6asm_memory_unmap_regions(struct audio_client *ac, int dir)
  5959. {
  5960. struct avs_cmd_shared_mem_unmap_regions mem_unmap;
  5961. struct audio_port_data *port = NULL;
  5962. struct asm_buffer_node *buf_node = NULL;
  5963. struct list_head *ptr, *next;
  5964. phys_addr_t buf_add;
  5965. int rc = 0;
  5966. int cmd_size = 0;
  5967. if (!ac) {
  5968. pr_err("%s: APR handle NULL\n", __func__);
  5969. return -EINVAL;
  5970. }
  5971. if (ac->mmap_apr == NULL) {
  5972. pr_err("%s: mmap APR handle NULL\n", __func__);
  5973. return -EINVAL;
  5974. }
  5975. pr_debug("%s: Session[%d]\n", __func__, ac->session);
  5976. cmd_size = sizeof(struct avs_cmd_shared_mem_unmap_regions);
  5977. q6asm_add_mmaphdr(ac, &mem_unmap.hdr, cmd_size, dir);
  5978. atomic_set(&ac->mem_state, -1);
  5979. port = &ac->port[dir];
  5980. buf_add = port->buf->phys;
  5981. mem_unmap.hdr.opcode = ASM_CMD_SHARED_MEM_UNMAP_REGIONS;
  5982. mem_unmap.mem_map_handle = 0;
  5983. list_for_each_safe(ptr, next, &ac->port[dir].mem_map_handle) {
  5984. buf_node = list_entry(ptr, struct asm_buffer_node,
  5985. list);
  5986. if (buf_node->buf_phys_addr == buf_add) {
  5987. pr_debug("%s: Found the element\n", __func__);
  5988. mem_unmap.mem_map_handle = buf_node->mmap_hdl;
  5989. break;
  5990. }
  5991. }
  5992. pr_debug("%s: mem_unmap-mem_map_handle: 0x%x\n",
  5993. __func__, mem_unmap.mem_map_handle);
  5994. if (mem_unmap.mem_map_handle == 0) {
  5995. pr_err("%s: Do not send null mem handle to DSP\n", __func__);
  5996. rc = 0;
  5997. goto fail_cmd;
  5998. }
  5999. rc = apr_send_pkt(ac->mmap_apr, (uint32_t *) &mem_unmap);
  6000. if (rc < 0) {
  6001. pr_err("mmap_regions op[0x%x]rc[%d]\n",
  6002. mem_unmap.hdr.opcode, rc);
  6003. goto fail_cmd;
  6004. }
  6005. rc = wait_event_timeout(ac->mem_wait,
  6006. (atomic_read(&ac->mem_state) >= 0), 5*HZ);
  6007. if (!rc) {
  6008. pr_err("%s: timeout. waited for memory_unmap of handle 0x%x\n",
  6009. __func__, mem_unmap.mem_map_handle);
  6010. rc = -ETIMEDOUT;
  6011. goto fail_cmd;
  6012. } else if (atomic_read(&ac->mem_state) > 0) {
  6013. pr_err("%s: DSP returned error[%s]\n",
  6014. __func__, adsp_err_get_err_str(
  6015. atomic_read(&ac->mem_state)));
  6016. rc = adsp_err_get_lnx_err_code(
  6017. atomic_read(&ac->mem_state));
  6018. goto fail_cmd;
  6019. } else if (atomic_read(&ac->unmap_cb_success) == 0) {
  6020. pr_err("%s: Error in mem unmap callback of handle 0x%x\n",
  6021. __func__, mem_unmap.mem_map_handle);
  6022. rc = -EINVAL;
  6023. goto fail_cmd;
  6024. }
  6025. rc = 0;
  6026. fail_cmd:
  6027. list_for_each_safe(ptr, next, &ac->port[dir].mem_map_handle) {
  6028. buf_node = list_entry(ptr, struct asm_buffer_node,
  6029. list);
  6030. if (buf_node->buf_phys_addr == buf_add) {
  6031. list_del(&buf_node->list);
  6032. kfree(buf_node);
  6033. break;
  6034. }
  6035. }
  6036. return rc;
  6037. }
  6038. int q6asm_set_lrgain(struct audio_client *ac, int left_gain, int right_gain)
  6039. {
  6040. struct asm_volume_ctrl_multichannel_gain multi_ch_gain;
  6041. int sz = 0;
  6042. int rc = 0;
  6043. if (ac == NULL) {
  6044. pr_err("%s: APR handle NULL\n", __func__);
  6045. rc = -EINVAL;
  6046. goto fail_cmd;
  6047. }
  6048. if (ac->apr == NULL) {
  6049. pr_err("%s: AC APR handle NULL\n", __func__);
  6050. rc = -EINVAL;
  6051. goto fail_cmd;
  6052. }
  6053. memset(&multi_ch_gain, 0, sizeof(multi_ch_gain));
  6054. sz = sizeof(struct asm_volume_ctrl_multichannel_gain);
  6055. q6asm_add_hdr_async(ac, &multi_ch_gain.hdr, sz, TRUE);
  6056. atomic_set(&ac->cmd_state_pp, -1);
  6057. multi_ch_gain.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  6058. multi_ch_gain.param.data_payload_addr_lsw = 0;
  6059. multi_ch_gain.param.data_payload_addr_msw = 0;
  6060. multi_ch_gain.param.mem_map_handle = 0;
  6061. multi_ch_gain.param.data_payload_size = sizeof(multi_ch_gain) -
  6062. sizeof(multi_ch_gain.hdr) - sizeof(multi_ch_gain.param);
  6063. multi_ch_gain.data.module_id = ASM_MODULE_ID_VOL_CTRL;
  6064. multi_ch_gain.data.param_id = ASM_PARAM_ID_MULTICHANNEL_GAIN;
  6065. multi_ch_gain.data.param_size = multi_ch_gain.param.data_payload_size -
  6066. sizeof(multi_ch_gain.data);
  6067. multi_ch_gain.data.reserved = 0;
  6068. multi_ch_gain.gain_data[0].channeltype = PCM_CHANNEL_FL;
  6069. multi_ch_gain.gain_data[0].gain = left_gain << 15;
  6070. multi_ch_gain.gain_data[1].channeltype = PCM_CHANNEL_FR;
  6071. multi_ch_gain.gain_data[1].gain = right_gain << 15;
  6072. multi_ch_gain.num_channels = 2;
  6073. rc = apr_send_pkt(ac->apr, (uint32_t *) &multi_ch_gain);
  6074. if (rc < 0) {
  6075. pr_err("%s: set-params send failed paramid[0x%x] rc %d\n",
  6076. __func__, multi_ch_gain.data.param_id, rc);
  6077. rc = -EINVAL;
  6078. goto fail_cmd;
  6079. }
  6080. rc = wait_event_timeout(ac->cmd_wait,
  6081. (atomic_read(&ac->cmd_state_pp) >= 0), 5*HZ);
  6082. if (!rc) {
  6083. pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
  6084. multi_ch_gain.data.param_id);
  6085. rc = -ETIMEDOUT;
  6086. goto fail_cmd;
  6087. }
  6088. if (atomic_read(&ac->cmd_state_pp) > 0) {
  6089. pr_err("%s: DSP returned error[%s] , set-params paramid[0x%x]\n",
  6090. __func__, adsp_err_get_err_str(
  6091. atomic_read(&ac->cmd_state_pp)),
  6092. multi_ch_gain.data.param_id);
  6093. rc = adsp_err_get_lnx_err_code(
  6094. atomic_read(&ac->cmd_state_pp));
  6095. goto fail_cmd;
  6096. }
  6097. rc = 0;
  6098. fail_cmd:
  6099. return rc;
  6100. }
  6101. /*
  6102. * q6asm_set_multich_gain: set multiple channel gains on an ASM session
  6103. * @ac: audio client handle
  6104. * @channels: number of channels caller intends to set gains
  6105. * @gains: list of gains of audio channels
  6106. * @ch_map: list of channel mapping. Only valid if use_default is false
  6107. * @use_default: flag to indicate whether to use default mapping
  6108. */
  6109. int q6asm_set_multich_gain(struct audio_client *ac, uint32_t channels,
  6110. uint32_t *gains, uint8_t *ch_map, bool use_default)
  6111. {
  6112. struct asm_volume_ctrl_multichannel_gain multich_gain;
  6113. int sz = 0;
  6114. int rc = 0;
  6115. int i;
  6116. u8 default_chmap[VOLUME_CONTROL_MAX_CHANNELS];
  6117. if (ac == NULL) {
  6118. pr_err("%s: ac is NULL\n", __func__);
  6119. rc = -EINVAL;
  6120. goto done;
  6121. }
  6122. if (ac->apr == NULL) {
  6123. dev_err(ac->dev, "%s: AC APR handle NULL\n", __func__);
  6124. rc = -EINVAL;
  6125. goto done;
  6126. }
  6127. if (gains == NULL) {
  6128. dev_err(ac->dev, "%s: gain_list is NULL\n", __func__);
  6129. rc = -EINVAL;
  6130. goto done;
  6131. }
  6132. if (channels > VOLUME_CONTROL_MAX_CHANNELS) {
  6133. dev_err(ac->dev, "%s: Invalid channel count %d\n",
  6134. __func__, channels);
  6135. rc = -EINVAL;
  6136. goto done;
  6137. }
  6138. if (!use_default && ch_map == NULL) {
  6139. dev_err(ac->dev, "%s: NULL channel map\n", __func__);
  6140. rc = -EINVAL;
  6141. goto done;
  6142. }
  6143. memset(&multich_gain, 0, sizeof(multich_gain));
  6144. sz = sizeof(struct asm_volume_ctrl_multichannel_gain);
  6145. q6asm_add_hdr_async(ac, &multich_gain.hdr, sz, TRUE);
  6146. atomic_set(&ac->cmd_state_pp, -1);
  6147. multich_gain.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  6148. multich_gain.param.data_payload_addr_lsw = 0;
  6149. multich_gain.param.data_payload_addr_msw = 0;
  6150. multich_gain.param.mem_map_handle = 0;
  6151. multich_gain.param.data_payload_size = sizeof(multich_gain) -
  6152. sizeof(multich_gain.hdr) - sizeof(multich_gain.param);
  6153. multich_gain.data.module_id = ASM_MODULE_ID_VOL_CTRL;
  6154. multich_gain.data.param_id = ASM_PARAM_ID_MULTICHANNEL_GAIN;
  6155. multich_gain.data.param_size = multich_gain.param.data_payload_size -
  6156. sizeof(multich_gain.data);
  6157. multich_gain.data.reserved = 0;
  6158. if (use_default) {
  6159. rc = q6asm_map_channels(default_chmap, channels, false);
  6160. if (rc < 0)
  6161. goto done;
  6162. for (i = 0; i < channels; i++) {
  6163. multich_gain.gain_data[i].channeltype =
  6164. default_chmap[i];
  6165. multich_gain.gain_data[i].gain = gains[i] << 15;
  6166. }
  6167. } else {
  6168. for (i = 0; i < channels; i++) {
  6169. multich_gain.gain_data[i].channeltype = ch_map[i];
  6170. multich_gain.gain_data[i].gain = gains[i] << 15;
  6171. }
  6172. }
  6173. multich_gain.num_channels = channels;
  6174. rc = apr_send_pkt(ac->apr, (uint32_t *) &multich_gain);
  6175. if (rc < 0) {
  6176. pr_err("%s: set-params send failed paramid[0x%x] rc %d\n",
  6177. __func__, multich_gain.data.param_id, rc);
  6178. goto done;
  6179. }
  6180. rc = wait_event_timeout(ac->cmd_wait,
  6181. (atomic_read(&ac->cmd_state_pp) >= 0), 5*HZ);
  6182. if (!rc) {
  6183. pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
  6184. multich_gain.data.param_id);
  6185. rc = -EINVAL;
  6186. goto done;
  6187. }
  6188. if (atomic_read(&ac->cmd_state_pp) > 0) {
  6189. pr_err("%s: DSP returned error[%d] , set-params paramid[0x%x]\n",
  6190. __func__, atomic_read(&ac->cmd_state_pp),
  6191. multich_gain.data.param_id);
  6192. rc = -EINVAL;
  6193. goto done;
  6194. }
  6195. rc = 0;
  6196. done:
  6197. return rc;
  6198. }
  6199. int q6asm_set_mute(struct audio_client *ac, int muteflag)
  6200. {
  6201. struct asm_volume_ctrl_mute_config mute;
  6202. int sz = 0;
  6203. int rc = 0;
  6204. if (ac == NULL) {
  6205. pr_err("%s: APR handle NULL\n", __func__);
  6206. rc = -EINVAL;
  6207. goto fail_cmd;
  6208. }
  6209. if (ac->apr == NULL) {
  6210. pr_err("%s: AC APR handle NULL\n", __func__);
  6211. rc = -EINVAL;
  6212. goto fail_cmd;
  6213. }
  6214. sz = sizeof(struct asm_volume_ctrl_mute_config);
  6215. q6asm_add_hdr_async(ac, &mute.hdr, sz, TRUE);
  6216. atomic_set(&ac->cmd_state_pp, -1);
  6217. mute.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  6218. mute.param.data_payload_addr_lsw = 0;
  6219. mute.param.data_payload_addr_msw = 0;
  6220. mute.param.mem_map_handle = 0;
  6221. mute.param.data_payload_size = sizeof(mute) -
  6222. sizeof(mute.hdr) - sizeof(mute.param);
  6223. mute.data.module_id = ASM_MODULE_ID_VOL_CTRL;
  6224. mute.data.param_id = ASM_PARAM_ID_VOL_CTRL_MUTE_CONFIG;
  6225. mute.data.param_size = mute.param.data_payload_size - sizeof(mute.data);
  6226. mute.data.reserved = 0;
  6227. mute.mute_flag = muteflag;
  6228. rc = apr_send_pkt(ac->apr, (uint32_t *) &mute);
  6229. if (rc < 0) {
  6230. pr_err("%s: set-params send failed paramid[0x%x] rc %d\n",
  6231. __func__, mute.data.param_id, rc);
  6232. rc = -EINVAL;
  6233. goto fail_cmd;
  6234. }
  6235. rc = wait_event_timeout(ac->cmd_wait,
  6236. (atomic_read(&ac->cmd_state_pp) >= 0), 5*HZ);
  6237. if (!rc) {
  6238. pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
  6239. mute.data.param_id);
  6240. rc = -ETIMEDOUT;
  6241. goto fail_cmd;
  6242. }
  6243. if (atomic_read(&ac->cmd_state_pp) > 0) {
  6244. pr_err("%s: DSP returned error[%s] set-params paramid[0x%x]\n",
  6245. __func__, adsp_err_get_err_str(
  6246. atomic_read(&ac->cmd_state_pp)),
  6247. mute.data.param_id);
  6248. rc = adsp_err_get_lnx_err_code(
  6249. atomic_read(&ac->cmd_state_pp));
  6250. goto fail_cmd;
  6251. }
  6252. rc = 0;
  6253. fail_cmd:
  6254. return rc;
  6255. }
  6256. static int __q6asm_set_volume(struct audio_client *ac, int volume, int instance)
  6257. {
  6258. struct asm_volume_ctrl_master_gain vol;
  6259. int sz = 0;
  6260. int rc = 0;
  6261. int module_id;
  6262. if (ac == NULL) {
  6263. pr_err("%s: APR handle NULL\n", __func__);
  6264. rc = -EINVAL;
  6265. goto fail_cmd;
  6266. }
  6267. if (ac->apr == NULL) {
  6268. pr_err("%s: AC APR handle NULL\n", __func__);
  6269. rc = -EINVAL;
  6270. goto fail_cmd;
  6271. }
  6272. switch (instance) {
  6273. case SOFT_VOLUME_INSTANCE_2:
  6274. module_id = ASM_MODULE_ID_VOL_CTRL2;
  6275. break;
  6276. case SOFT_VOLUME_INSTANCE_1:
  6277. default:
  6278. module_id = ASM_MODULE_ID_VOL_CTRL;
  6279. break;
  6280. }
  6281. sz = sizeof(struct asm_volume_ctrl_master_gain);
  6282. q6asm_add_hdr_async(ac, &vol.hdr, sz, TRUE);
  6283. atomic_set(&ac->cmd_state_pp, -1);
  6284. vol.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  6285. vol.param.data_payload_addr_lsw = 0;
  6286. vol.param.data_payload_addr_msw = 0;
  6287. vol.param.mem_map_handle = 0;
  6288. vol.param.data_payload_size = sizeof(vol) -
  6289. sizeof(vol.hdr) - sizeof(vol.param);
  6290. vol.data.module_id = module_id;
  6291. vol.data.param_id = ASM_PARAM_ID_VOL_CTRL_MASTER_GAIN;
  6292. vol.data.param_size = vol.param.data_payload_size - sizeof(vol.data);
  6293. vol.data.reserved = 0;
  6294. vol.master_gain = volume;
  6295. rc = apr_send_pkt(ac->apr, (uint32_t *) &vol);
  6296. if (rc < 0) {
  6297. pr_err("%s: set-params send failed paramid[0x%x] rc %d\n",
  6298. __func__, vol.data.param_id, rc);
  6299. rc = -EINVAL;
  6300. goto fail_cmd;
  6301. }
  6302. rc = wait_event_timeout(ac->cmd_wait,
  6303. (atomic_read(&ac->cmd_state_pp) >= 0), 5*HZ);
  6304. if (!rc) {
  6305. pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
  6306. vol.data.param_id);
  6307. rc = -ETIMEDOUT;
  6308. goto fail_cmd;
  6309. }
  6310. if (atomic_read(&ac->cmd_state_pp) > 0) {
  6311. pr_err("%s: DSP returned error[%s] set-params paramid[0x%x]\n",
  6312. __func__, adsp_err_get_err_str(
  6313. atomic_read(&ac->cmd_state_pp)),
  6314. vol.data.param_id);
  6315. rc = adsp_err_get_lnx_err_code(
  6316. atomic_read(&ac->cmd_state_pp));
  6317. goto fail_cmd;
  6318. }
  6319. rc = 0;
  6320. fail_cmd:
  6321. return rc;
  6322. }
  6323. int q6asm_set_volume(struct audio_client *ac, int volume)
  6324. {
  6325. return __q6asm_set_volume(ac, volume, SOFT_VOLUME_INSTANCE_1);
  6326. }
  6327. int q6asm_set_volume_v2(struct audio_client *ac, int volume, int instance)
  6328. {
  6329. return __q6asm_set_volume(ac, volume, instance);
  6330. }
  6331. int q6asm_set_aptx_dec_bt_addr(struct audio_client *ac,
  6332. struct aptx_dec_bt_addr_cfg *cfg)
  6333. {
  6334. struct aptx_dec_bt_dev_addr paylod;
  6335. int sz = 0;
  6336. int rc = 0;
  6337. pr_debug("%s: BT addr nap %d, uap %d, lap %d\n", __func__, cfg->nap,
  6338. cfg->uap, cfg->lap);
  6339. if (ac == NULL) {
  6340. pr_err("%s: AC handle NULL\n", __func__);
  6341. rc = -EINVAL;
  6342. goto fail_cmd;
  6343. }
  6344. if (ac->apr == NULL) {
  6345. pr_err("%s: AC APR handle NULL\n", __func__);
  6346. rc = -EINVAL;
  6347. goto fail_cmd;
  6348. }
  6349. sz = sizeof(struct aptx_dec_bt_dev_addr);
  6350. q6asm_add_hdr_async(ac, &paylod.hdr, sz, TRUE);
  6351. atomic_set(&ac->cmd_state, -1);
  6352. paylod.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  6353. paylod.encdec.param_id = APTX_DECODER_BT_ADDRESS;
  6354. paylod.encdec.param_size = sz - sizeof(paylod.hdr)
  6355. - sizeof(paylod.encdec);
  6356. paylod.bt_addr_cfg.lap = cfg->lap;
  6357. paylod.bt_addr_cfg.uap = cfg->uap;
  6358. paylod.bt_addr_cfg.nap = cfg->nap;
  6359. rc = apr_send_pkt(ac->apr, (uint32_t *) &paylod);
  6360. if (rc < 0) {
  6361. pr_err("%s: set-params send failed paramid[0x%x] rc %d\n",
  6362. __func__, paylod.encdec.param_id, rc);
  6363. rc = -EINVAL;
  6364. goto fail_cmd;
  6365. }
  6366. rc = wait_event_timeout(ac->cmd_wait,
  6367. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  6368. if (!rc) {
  6369. pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
  6370. paylod.encdec.param_id);
  6371. rc = -ETIMEDOUT;
  6372. goto fail_cmd;
  6373. }
  6374. if (atomic_read(&ac->cmd_state) > 0) {
  6375. pr_err("%s: DSP returned error[%s] set-params paramid[0x%x]\n",
  6376. __func__, adsp_err_get_err_str(
  6377. atomic_read(&ac->cmd_state)),
  6378. paylod.encdec.param_id);
  6379. rc = adsp_err_get_lnx_err_code(
  6380. atomic_read(&ac->cmd_state));
  6381. goto fail_cmd;
  6382. }
  6383. pr_debug("%s: set BT addr is success\n", __func__);
  6384. rc = 0;
  6385. fail_cmd:
  6386. return rc;
  6387. }
  6388. int q6asm_send_ion_fd(struct audio_client *ac, int fd)
  6389. {
  6390. struct ion_client *client;
  6391. struct ion_handle *handle;
  6392. ion_phys_addr_t paddr;
  6393. size_t pa_len = 0;
  6394. void *vaddr;
  6395. int ret;
  6396. int sz = 0;
  6397. struct avs_rtic_shared_mem_addr shm;
  6398. if (ac == NULL) {
  6399. pr_err("%s: APR handle NULL\n", __func__);
  6400. ret = -EINVAL;
  6401. goto fail_cmd;
  6402. }
  6403. if (ac->apr == NULL) {
  6404. pr_err("%s: AC APR handle NULL\n", __func__);
  6405. ret = -EINVAL;
  6406. goto fail_cmd;
  6407. }
  6408. ret = msm_audio_ion_import("audio_mem_client",
  6409. &client,
  6410. &handle,
  6411. fd,
  6412. NULL,
  6413. 0,
  6414. &paddr,
  6415. &pa_len,
  6416. &vaddr);
  6417. if (ret) {
  6418. pr_err("%s: audio ION import failed, rc = %d\n",
  6419. __func__, ret);
  6420. ret = -ENOMEM;
  6421. goto fail_cmd;
  6422. }
  6423. /* get payload length */
  6424. sz = sizeof(struct avs_rtic_shared_mem_addr);
  6425. q6asm_add_hdr_async(ac, &shm.hdr, sz, TRUE);
  6426. atomic_set(&ac->cmd_state, -1);
  6427. shm.shm_buf_addr_lsw = lower_32_bits(paddr);
  6428. shm.shm_buf_addr_msw = msm_audio_populate_upper_32_bits(paddr);
  6429. shm.buf_size = pa_len;
  6430. shm.shm_buf_num_regions = 1;
  6431. shm.shm_buf_mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
  6432. shm.shm_buf_flag = 0x00;
  6433. shm.encdec.param_id = AVS_PARAM_ID_RTIC_SHARED_MEMORY_ADDR;
  6434. shm.encdec.param_size = sizeof(struct avs_rtic_shared_mem_addr) -
  6435. sizeof(struct apr_hdr) -
  6436. sizeof(struct asm_stream_cmd_set_encdec_param_v2);
  6437. shm.encdec.service_id = OUT;
  6438. shm.encdec.reserved = 0;
  6439. shm.map_region.shm_addr_lsw = shm.shm_buf_addr_lsw;
  6440. shm.map_region.shm_addr_msw = shm.shm_buf_addr_msw;
  6441. shm.map_region.mem_size_bytes = pa_len;
  6442. shm.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM_V2;
  6443. ret = apr_send_pkt(ac->apr, (uint32_t *) &shm);
  6444. if (ret < 0) {
  6445. pr_err("%s: set-params send failed paramid[0x%x] rc %d\n",
  6446. __func__, shm.encdec.param_id, ret);
  6447. ret = -EINVAL;
  6448. goto fail_cmd;
  6449. }
  6450. ret = wait_event_timeout(ac->cmd_wait,
  6451. (atomic_read(&ac->cmd_state) >= 0), 1*HZ);
  6452. if (!ret) {
  6453. pr_err("%s: timeout, shm.encdec paramid[0x%x]\n", __func__,
  6454. shm.encdec.param_id);
  6455. ret = -ETIMEDOUT;
  6456. goto fail_cmd;
  6457. }
  6458. if (atomic_read(&ac->cmd_state) > 0) {
  6459. pr_err("%s: DSP returned error[%s] shm.encdec paramid[0x%x]\n",
  6460. __func__,
  6461. adsp_err_get_err_str(atomic_read(&ac->cmd_state)),
  6462. shm.encdec.param_id);
  6463. ret = adsp_err_get_lnx_err_code(atomic_read(&ac->cmd_state));
  6464. goto fail_cmd;
  6465. }
  6466. ret = 0;
  6467. fail_cmd:
  6468. return ret;
  6469. }
  6470. int q6asm_send_rtic_event_ack(struct audio_client *ac,
  6471. void *param, uint32_t params_length)
  6472. {
  6473. char *asm_params = NULL;
  6474. int sz, rc;
  6475. struct avs_param_rtic_event_ack ack;
  6476. if (!param || !ac) {
  6477. pr_err("%s: %s is NULL\n", __func__,
  6478. (!param) ? "param" : "ac");
  6479. rc = -EINVAL;
  6480. goto done;
  6481. }
  6482. sz = sizeof(struct avs_param_rtic_event_ack) + params_length;
  6483. asm_params = kzalloc(sz, GFP_KERNEL);
  6484. if (!asm_params) {
  6485. rc = -ENOMEM;
  6486. goto done;
  6487. }
  6488. q6asm_add_hdr_async(ac, &ack.hdr,
  6489. sizeof(struct avs_param_rtic_event_ack) +
  6490. params_length, TRUE);
  6491. atomic_set(&ac->cmd_state, -1);
  6492. ack.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM_V2;
  6493. ack.encdec.param_id = AVS_PARAM_ID_RTIC_EVENT_ACK;
  6494. ack.encdec.param_size = params_length;
  6495. ack.encdec.reserved = 0;
  6496. ack.encdec.service_id = OUT;
  6497. memcpy(asm_params, &ack, sizeof(struct avs_param_rtic_event_ack));
  6498. memcpy(asm_params + sizeof(struct avs_param_rtic_event_ack),
  6499. param, params_length);
  6500. rc = apr_send_pkt(ac->apr, (uint32_t *) asm_params);
  6501. if (rc < 0) {
  6502. pr_err("%s: apr pkt failed for rtic event ack\n", __func__);
  6503. rc = -EINVAL;
  6504. goto fail_send_param;
  6505. }
  6506. rc = wait_event_timeout(ac->cmd_wait,
  6507. (atomic_read(&ac->cmd_state) >= 0), 1 * HZ);
  6508. if (!rc) {
  6509. pr_err("%s: timeout for rtic event ack cmd\n", __func__);
  6510. rc = -ETIMEDOUT;
  6511. goto fail_send_param;
  6512. }
  6513. if (atomic_read(&ac->cmd_state) > 0) {
  6514. pr_err("%s: DSP returned error[%s] for rtic event ack cmd\n",
  6515. __func__, adsp_err_get_err_str(
  6516. atomic_read(&ac->cmd_state)));
  6517. rc = adsp_err_get_lnx_err_code(
  6518. atomic_read(&ac->cmd_state));
  6519. goto fail_send_param;
  6520. }
  6521. rc = 0;
  6522. fail_send_param:
  6523. kfree(asm_params);
  6524. done:
  6525. return rc;
  6526. }
  6527. int q6asm_set_softpause(struct audio_client *ac,
  6528. struct asm_softpause_params *pause_param)
  6529. {
  6530. struct asm_soft_pause_params softpause;
  6531. int sz = 0;
  6532. int rc = 0;
  6533. if (ac == NULL) {
  6534. pr_err("%s: APR handle NULL\n", __func__);
  6535. rc = -EINVAL;
  6536. goto fail_cmd;
  6537. }
  6538. if (ac->apr == NULL) {
  6539. pr_err("%s: AC APR handle NULL\n", __func__);
  6540. rc = -EINVAL;
  6541. goto fail_cmd;
  6542. }
  6543. sz = sizeof(struct asm_soft_pause_params);
  6544. q6asm_add_hdr_async(ac, &softpause.hdr, sz, TRUE);
  6545. atomic_set(&ac->cmd_state_pp, -1);
  6546. softpause.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  6547. softpause.param.data_payload_addr_lsw = 0;
  6548. softpause.param.data_payload_addr_msw = 0;
  6549. softpause.param.mem_map_handle = 0;
  6550. softpause.param.data_payload_size = sizeof(softpause) -
  6551. sizeof(softpause.hdr) - sizeof(softpause.param);
  6552. softpause.data.module_id = ASM_MODULE_ID_VOL_CTRL;
  6553. softpause.data.param_id = ASM_PARAM_ID_SOFT_PAUSE_PARAMETERS;
  6554. softpause.data.param_size = softpause.param.data_payload_size -
  6555. sizeof(softpause.data);
  6556. softpause.data.reserved = 0;
  6557. softpause.enable_flag = pause_param->enable;
  6558. softpause.period = pause_param->period;
  6559. softpause.step = pause_param->step;
  6560. softpause.ramping_curve = pause_param->rampingcurve;
  6561. rc = apr_send_pkt(ac->apr, (uint32_t *) &softpause);
  6562. if (rc < 0) {
  6563. pr_err("%s: set-params send failed paramid[0x%x] rc %d\n",
  6564. __func__, softpause.data.param_id, rc);
  6565. rc = -EINVAL;
  6566. goto fail_cmd;
  6567. }
  6568. rc = wait_event_timeout(ac->cmd_wait,
  6569. (atomic_read(&ac->cmd_state_pp) >= 0), 5*HZ);
  6570. if (!rc) {
  6571. pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
  6572. softpause.data.param_id);
  6573. rc = -ETIMEDOUT;
  6574. goto fail_cmd;
  6575. }
  6576. if (atomic_read(&ac->cmd_state_pp) > 0) {
  6577. pr_err("%s: DSP returned error[%s] set-params paramid[0x%x]\n",
  6578. __func__, adsp_err_get_err_str(
  6579. atomic_read(&ac->cmd_state_pp)),
  6580. softpause.data.param_id);
  6581. rc = adsp_err_get_lnx_err_code(
  6582. atomic_read(&ac->cmd_state_pp));
  6583. goto fail_cmd;
  6584. }
  6585. rc = 0;
  6586. fail_cmd:
  6587. return rc;
  6588. }
  6589. static int __q6asm_set_softvolume(struct audio_client *ac,
  6590. struct asm_softvolume_params *softvol_param,
  6591. int instance)
  6592. {
  6593. struct asm_soft_step_volume_params softvol;
  6594. int sz = 0;
  6595. int rc = 0;
  6596. int module_id;
  6597. if (ac == NULL) {
  6598. pr_err("%s: APR handle NULL\n", __func__);
  6599. rc = -EINVAL;
  6600. goto fail_cmd;
  6601. }
  6602. if (ac->apr == NULL) {
  6603. pr_err("%s: AC APR handle NULL\n", __func__);
  6604. rc = -EINVAL;
  6605. goto fail_cmd;
  6606. }
  6607. switch (instance) {
  6608. case SOFT_VOLUME_INSTANCE_2:
  6609. module_id = ASM_MODULE_ID_VOL_CTRL2;
  6610. break;
  6611. case SOFT_VOLUME_INSTANCE_1:
  6612. default:
  6613. module_id = ASM_MODULE_ID_VOL_CTRL;
  6614. break;
  6615. }
  6616. sz = sizeof(struct asm_soft_step_volume_params);
  6617. q6asm_add_hdr_async(ac, &softvol.hdr, sz, TRUE);
  6618. atomic_set(&ac->cmd_state_pp, -1);
  6619. softvol.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  6620. softvol.param.data_payload_addr_lsw = 0;
  6621. softvol.param.data_payload_addr_msw = 0;
  6622. softvol.param.mem_map_handle = 0;
  6623. softvol.param.data_payload_size = sizeof(softvol) -
  6624. sizeof(softvol.hdr) - sizeof(softvol.param);
  6625. softvol.data.module_id = module_id;
  6626. softvol.data.param_id = ASM_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS;
  6627. softvol.data.param_size = softvol.param.data_payload_size -
  6628. sizeof(softvol.data);
  6629. softvol.data.reserved = 0;
  6630. softvol.period = softvol_param->period;
  6631. softvol.step = softvol_param->step;
  6632. softvol.ramping_curve = softvol_param->rampingcurve;
  6633. rc = apr_send_pkt(ac->apr, (uint32_t *) &softvol);
  6634. if (rc < 0) {
  6635. pr_err("%s: set-params send failed paramid[0x%x] rc %d\n",
  6636. __func__, softvol.data.param_id, rc);
  6637. rc = -EINVAL;
  6638. goto fail_cmd;
  6639. }
  6640. rc = wait_event_timeout(ac->cmd_wait,
  6641. (atomic_read(&ac->cmd_state_pp) >= 0), 5*HZ);
  6642. if (!rc) {
  6643. pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
  6644. softvol.data.param_id);
  6645. rc = -ETIMEDOUT;
  6646. goto fail_cmd;
  6647. }
  6648. if (atomic_read(&ac->cmd_state_pp) > 0) {
  6649. pr_err("%s: DSP returned error[%s] set-params paramid[0x%x]\n",
  6650. __func__, adsp_err_get_err_str(
  6651. atomic_read(&ac->cmd_state_pp)),
  6652. softvol.data.param_id);
  6653. rc = adsp_err_get_lnx_err_code(
  6654. atomic_read(&ac->cmd_state_pp));
  6655. goto fail_cmd;
  6656. }
  6657. rc = 0;
  6658. fail_cmd:
  6659. return rc;
  6660. }
  6661. int q6asm_set_softvolume(struct audio_client *ac,
  6662. struct asm_softvolume_params *softvol_param)
  6663. {
  6664. return __q6asm_set_softvolume(ac, softvol_param,
  6665. SOFT_VOLUME_INSTANCE_1);
  6666. }
  6667. int q6asm_set_softvolume_v2(struct audio_client *ac,
  6668. struct asm_softvolume_params *softvol_param,
  6669. int instance)
  6670. {
  6671. return __q6asm_set_softvolume(ac, softvol_param, instance);
  6672. }
  6673. int q6asm_equalizer(struct audio_client *ac, void *eq_p)
  6674. {
  6675. struct asm_eq_params eq;
  6676. struct msm_audio_eq_stream_config *eq_params = NULL;
  6677. int i = 0;
  6678. int sz = 0;
  6679. int rc = 0;
  6680. if (ac == NULL) {
  6681. pr_err("%s: APR handle NULL\n", __func__);
  6682. rc = -EINVAL;
  6683. goto fail_cmd;
  6684. }
  6685. if (ac->apr == NULL) {
  6686. pr_err("%s: AC APR handle NULL\n", __func__);
  6687. rc = -EINVAL;
  6688. goto fail_cmd;
  6689. }
  6690. if (eq_p == NULL) {
  6691. pr_err("%s: [%d]: Invalid Eq param\n", __func__, ac->session);
  6692. rc = -EINVAL;
  6693. goto fail_cmd;
  6694. }
  6695. sz = sizeof(struct asm_eq_params);
  6696. eq_params = (struct msm_audio_eq_stream_config *) eq_p;
  6697. q6asm_add_hdr(ac, &eq.hdr, sz, TRUE);
  6698. atomic_set(&ac->cmd_state_pp, -1);
  6699. eq.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  6700. eq.param.data_payload_addr_lsw = 0;
  6701. eq.param.data_payload_addr_msw = 0;
  6702. eq.param.mem_map_handle = 0;
  6703. eq.param.data_payload_size = sizeof(eq) -
  6704. sizeof(eq.hdr) - sizeof(eq.param);
  6705. eq.data.module_id = ASM_MODULE_ID_EQUALIZER;
  6706. eq.data.param_id = ASM_PARAM_ID_EQUALIZER_PARAMETERS;
  6707. eq.data.param_size = eq.param.data_payload_size - sizeof(eq.data);
  6708. eq.enable_flag = eq_params->enable;
  6709. eq.num_bands = eq_params->num_bands;
  6710. pr_debug("%s: enable:%d numbands:%d\n", __func__, eq_params->enable,
  6711. eq_params->num_bands);
  6712. for (i = 0; i < eq_params->num_bands; i++) {
  6713. eq.eq_bands[i].band_idx =
  6714. eq_params->eq_bands[i].band_idx;
  6715. eq.eq_bands[i].filterype =
  6716. eq_params->eq_bands[i].filter_type;
  6717. eq.eq_bands[i].center_freq_hz =
  6718. eq_params->eq_bands[i].center_freq_hz;
  6719. eq.eq_bands[i].filter_gain =
  6720. eq_params->eq_bands[i].filter_gain;
  6721. eq.eq_bands[i].q_factor =
  6722. eq_params->eq_bands[i].q_factor;
  6723. pr_debug("%s: filter_type:%u bandnum:%d\n", __func__,
  6724. eq_params->eq_bands[i].filter_type, i);
  6725. pr_debug("%s: center_freq_hz:%u bandnum:%d\n", __func__,
  6726. eq_params->eq_bands[i].center_freq_hz, i);
  6727. pr_debug("%s: filter_gain:%d bandnum:%d\n", __func__,
  6728. eq_params->eq_bands[i].filter_gain, i);
  6729. pr_debug("%s: q_factor:%d bandnum:%d\n", __func__,
  6730. eq_params->eq_bands[i].q_factor, i);
  6731. }
  6732. rc = apr_send_pkt(ac->apr, (uint32_t *)&eq);
  6733. if (rc < 0) {
  6734. pr_err("%s: set-params send failed paramid[0x%x] rc %d\n",
  6735. __func__, eq.data.param_id, rc);
  6736. rc = -EINVAL;
  6737. goto fail_cmd;
  6738. }
  6739. rc = wait_event_timeout(ac->cmd_wait,
  6740. (atomic_read(&ac->cmd_state_pp) >= 0), 5*HZ);
  6741. if (!rc) {
  6742. pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
  6743. eq.data.param_id);
  6744. rc = -ETIMEDOUT;
  6745. goto fail_cmd;
  6746. }
  6747. if (atomic_read(&ac->cmd_state_pp) > 0) {
  6748. pr_err("%s: DSP returned error[%s] set-params paramid[0x%x]\n",
  6749. __func__, adsp_err_get_err_str(
  6750. atomic_read(&ac->cmd_state_pp)),
  6751. eq.data.param_id);
  6752. rc = adsp_err_get_lnx_err_code(
  6753. atomic_read(&ac->cmd_state_pp));
  6754. goto fail_cmd;
  6755. }
  6756. rc = 0;
  6757. fail_cmd:
  6758. return rc;
  6759. }
  6760. static int __q6asm_read(struct audio_client *ac, bool is_custom_len_reqd,
  6761. int len)
  6762. {
  6763. struct asm_data_cmd_read_v2 read;
  6764. struct asm_buffer_node *buf_node = NULL;
  6765. struct list_head *ptr, *next;
  6766. struct audio_buffer *ab;
  6767. int dsp_buf;
  6768. struct audio_port_data *port;
  6769. int rc;
  6770. if (ac == NULL) {
  6771. pr_err("%s: APR handle NULL\n", __func__);
  6772. return -EINVAL;
  6773. }
  6774. if (ac->apr == NULL) {
  6775. pr_err("%s: AC APR handle NULL\n", __func__);
  6776. return -EINVAL;
  6777. }
  6778. if (ac->io_mode & SYNC_IO_MODE) {
  6779. port = &ac->port[OUT];
  6780. q6asm_add_hdr(ac, &read.hdr, sizeof(read), FALSE);
  6781. mutex_lock(&port->lock);
  6782. dsp_buf = port->dsp_buf;
  6783. if (port->buf == NULL) {
  6784. pr_err("%s: buf is NULL\n", __func__);
  6785. mutex_unlock(&port->lock);
  6786. return -EINVAL;
  6787. }
  6788. ab = &port->buf[dsp_buf];
  6789. dev_vdbg(ac->dev, "%s: session[%d]dsp-buf[%d][%pK]cpu_buf[%d][%pK]\n",
  6790. __func__,
  6791. ac->session,
  6792. dsp_buf,
  6793. port->buf[dsp_buf].data,
  6794. port->cpu_buf,
  6795. &port->buf[port->cpu_buf].phys);
  6796. read.hdr.opcode = ASM_DATA_CMD_READ_V2;
  6797. read.buf_addr_lsw = lower_32_bits(ab->phys);
  6798. read.buf_addr_msw = msm_audio_populate_upper_32_bits(ab->phys);
  6799. list_for_each_safe(ptr, next, &ac->port[OUT].mem_map_handle) {
  6800. buf_node = list_entry(ptr, struct asm_buffer_node,
  6801. list);
  6802. if (buf_node->buf_phys_addr == ab->phys)
  6803. read.mem_map_handle = buf_node->mmap_hdl;
  6804. }
  6805. dev_vdbg(ac->dev, "memory_map handle in q6asm_read: [%0x]:",
  6806. read.mem_map_handle);
  6807. read.buf_size = is_custom_len_reqd ? len : ab->size;
  6808. read.seq_id = port->dsp_buf;
  6809. q6asm_update_token(&read.hdr.token,
  6810. 0, /* Session ID is NA */
  6811. 0, /* Stream ID is NA */
  6812. port->dsp_buf,
  6813. 0, /* Direction flag is NA */
  6814. WAIT_CMD);
  6815. port->dsp_buf = q6asm_get_next_buf(ac, port->dsp_buf,
  6816. port->max_buf_cnt);
  6817. mutex_unlock(&port->lock);
  6818. dev_vdbg(ac->dev, "%s: buf add[%pK] token[0x%x] uid[%d]\n",
  6819. __func__, &ab->phys, read.hdr.token,
  6820. read.seq_id);
  6821. rc = apr_send_pkt(ac->apr, (uint32_t *) &read);
  6822. if (rc < 0) {
  6823. pr_err("%s: read op[0x%x]rc[%d]\n",
  6824. __func__, read.hdr.opcode, rc);
  6825. goto fail_cmd;
  6826. }
  6827. return 0;
  6828. }
  6829. fail_cmd:
  6830. return -EINVAL;
  6831. }
  6832. int q6asm_read(struct audio_client *ac)
  6833. {
  6834. return __q6asm_read(ac, false/*is_custom_len_reqd*/, 0);
  6835. }
  6836. int q6asm_read_v2(struct audio_client *ac, uint32_t len)
  6837. {
  6838. return __q6asm_read(ac, true /*is_custom_len_reqd*/, len);
  6839. }
  6840. int q6asm_read_nolock(struct audio_client *ac)
  6841. {
  6842. struct asm_data_cmd_read_v2 read;
  6843. struct asm_buffer_node *buf_node = NULL;
  6844. struct list_head *ptr, *next;
  6845. struct audio_buffer *ab;
  6846. int dsp_buf;
  6847. struct audio_port_data *port;
  6848. int rc;
  6849. if (ac == NULL) {
  6850. pr_err("%s: APR handle NULL\n", __func__);
  6851. return -EINVAL;
  6852. }
  6853. if (ac->apr == NULL) {
  6854. pr_err("%s: AC APR handle NULL\n", __func__);
  6855. return -EINVAL;
  6856. }
  6857. if (ac->io_mode & SYNC_IO_MODE) {
  6858. port = &ac->port[OUT];
  6859. q6asm_add_hdr_async(ac, &read.hdr, sizeof(read), FALSE);
  6860. dsp_buf = port->dsp_buf;
  6861. ab = &port->buf[dsp_buf];
  6862. dev_vdbg(ac->dev, "%s: session[%d]dsp-buf[%d][%pK]cpu_buf[%d][%pK]\n",
  6863. __func__,
  6864. ac->session,
  6865. dsp_buf,
  6866. port->buf[dsp_buf].data,
  6867. port->cpu_buf,
  6868. &port->buf[port->cpu_buf].phys);
  6869. read.hdr.opcode = ASM_DATA_CMD_READ_V2;
  6870. read.buf_addr_lsw = lower_32_bits(ab->phys);
  6871. read.buf_addr_msw = msm_audio_populate_upper_32_bits(ab->phys);
  6872. read.buf_size = ab->size;
  6873. read.seq_id = port->dsp_buf;
  6874. q6asm_update_token(&read.hdr.token,
  6875. 0, /* Session ID is NA */
  6876. 0, /* Stream ID is NA */
  6877. port->dsp_buf,
  6878. 0, /* Direction flag is NA */
  6879. WAIT_CMD);
  6880. list_for_each_safe(ptr, next, &ac->port[OUT].mem_map_handle) {
  6881. buf_node = list_entry(ptr, struct asm_buffer_node,
  6882. list);
  6883. if (buf_node->buf_phys_addr == ab->phys) {
  6884. read.mem_map_handle = buf_node->mmap_hdl;
  6885. break;
  6886. }
  6887. }
  6888. port->dsp_buf = q6asm_get_next_buf(ac, port->dsp_buf,
  6889. port->max_buf_cnt);
  6890. dev_vdbg(ac->dev, "%s: buf add[%pK] token[0x%x] uid[%d]\n",
  6891. __func__, &ab->phys, read.hdr.token,
  6892. read.seq_id);
  6893. rc = apr_send_pkt(ac->apr, (uint32_t *) &read);
  6894. if (rc < 0) {
  6895. pr_err("%s: read op[0x%x]rc[%d]\n",
  6896. __func__, read.hdr.opcode, rc);
  6897. goto fail_cmd;
  6898. }
  6899. return 0;
  6900. }
  6901. fail_cmd:
  6902. return -EINVAL;
  6903. }
  6904. int q6asm_async_write(struct audio_client *ac,
  6905. struct audio_aio_write_param *param)
  6906. {
  6907. int rc = 0;
  6908. struct asm_data_cmd_write_v2 write;
  6909. struct asm_buffer_node *buf_node = NULL;
  6910. struct list_head *ptr, *next;
  6911. struct audio_buffer *ab;
  6912. struct audio_port_data *port;
  6913. phys_addr_t lbuf_phys_addr;
  6914. u32 liomode;
  6915. u32 io_compressed;
  6916. u32 io_compressed_stream;
  6917. if (ac == NULL) {
  6918. pr_err("%s: APR handle NULL\n", __func__);
  6919. return -EINVAL;
  6920. }
  6921. if (ac->apr == NULL) {
  6922. pr_err("%s: AC APR handle NULL\n", __func__);
  6923. return -EINVAL;
  6924. }
  6925. q6asm_stream_add_hdr_async(
  6926. ac, &write.hdr, sizeof(write), TRUE, ac->stream_id);
  6927. port = &ac->port[IN];
  6928. ab = &port->buf[port->dsp_buf];
  6929. /* Pass session id as token for AIO scheme */
  6930. write.hdr.token = param->uid;
  6931. write.hdr.opcode = ASM_DATA_CMD_WRITE_V2;
  6932. write.buf_addr_lsw = lower_32_bits(param->paddr);
  6933. write.buf_addr_msw = msm_audio_populate_upper_32_bits(param->paddr);
  6934. write.buf_size = param->len;
  6935. write.timestamp_msw = param->msw_ts;
  6936. write.timestamp_lsw = param->lsw_ts;
  6937. liomode = (ASYNC_IO_MODE | NT_MODE);
  6938. io_compressed = (ASYNC_IO_MODE | COMPRESSED_IO);
  6939. io_compressed_stream = (ASYNC_IO_MODE | COMPRESSED_STREAM_IO);
  6940. if (ac->io_mode == liomode)
  6941. lbuf_phys_addr = (param->paddr - 32);
  6942. else if (ac->io_mode == io_compressed ||
  6943. ac->io_mode == io_compressed_stream)
  6944. lbuf_phys_addr = (param->paddr - param->metadata_len);
  6945. else {
  6946. if (param->flags & SET_TIMESTAMP)
  6947. lbuf_phys_addr = param->paddr -
  6948. sizeof(struct snd_codec_metadata);
  6949. else
  6950. lbuf_phys_addr = param->paddr;
  6951. }
  6952. dev_vdbg(ac->dev, "%s: token[0x%x], buf_addr[%pK], buf_size[0x%x], ts_msw[0x%x], ts_lsw[0x%x], lbuf_phys_addr: 0x[%pK]\n",
  6953. __func__,
  6954. write.hdr.token, &param->paddr,
  6955. write.buf_size, write.timestamp_msw,
  6956. write.timestamp_lsw, &lbuf_phys_addr);
  6957. /* Use 0xFF00 for disabling timestamps */
  6958. if (param->flags == 0xFF00)
  6959. write.flags = (0x00000000 | (param->flags & 0x800000FF));
  6960. else
  6961. write.flags = (0x80000000 | param->flags);
  6962. write.flags |= param->last_buffer << ASM_SHIFT_LAST_BUFFER_FLAG;
  6963. write.seq_id = param->uid;
  6964. list_for_each_safe(ptr, next, &ac->port[IN].mem_map_handle) {
  6965. buf_node = list_entry(ptr, struct asm_buffer_node,
  6966. list);
  6967. if (buf_node->buf_phys_addr == lbuf_phys_addr) {
  6968. write.mem_map_handle = buf_node->mmap_hdl;
  6969. break;
  6970. }
  6971. }
  6972. rc = apr_send_pkt(ac->apr, (uint32_t *) &write);
  6973. if (rc < 0) {
  6974. pr_err("%s: write op[0x%x]rc[%d]\n", __func__,
  6975. write.hdr.opcode, rc);
  6976. goto fail_cmd;
  6977. }
  6978. return 0;
  6979. fail_cmd:
  6980. return -EINVAL;
  6981. }
  6982. int q6asm_async_read(struct audio_client *ac,
  6983. struct audio_aio_read_param *param)
  6984. {
  6985. int rc = 0;
  6986. struct asm_data_cmd_read_v2 read;
  6987. struct asm_buffer_node *buf_node = NULL;
  6988. struct list_head *ptr, *next;
  6989. phys_addr_t lbuf_phys_addr;
  6990. u32 liomode;
  6991. u32 io_compressed;
  6992. int dir = 0;
  6993. if (ac == NULL) {
  6994. pr_err("%s: APR handle NULL\n", __func__);
  6995. return -EINVAL;
  6996. }
  6997. if (ac->apr == NULL) {
  6998. pr_err("%s: AC APR handle NULL\n", __func__);
  6999. return -EINVAL;
  7000. }
  7001. q6asm_add_hdr_async(ac, &read.hdr, sizeof(read), FALSE);
  7002. /* Pass session id as token for AIO scheme */
  7003. read.hdr.token = param->uid;
  7004. read.hdr.opcode = ASM_DATA_CMD_READ_V2;
  7005. read.buf_addr_lsw = lower_32_bits(param->paddr);
  7006. read.buf_addr_msw = msm_audio_populate_upper_32_bits(param->paddr);
  7007. read.buf_size = param->len;
  7008. read.seq_id = param->uid;
  7009. liomode = (NT_MODE | ASYNC_IO_MODE);
  7010. io_compressed = (ASYNC_IO_MODE | COMPRESSED_IO);
  7011. if (ac->io_mode == liomode) {
  7012. lbuf_phys_addr = (param->paddr - 32);
  7013. /*legacy wma driver case*/
  7014. dir = IN;
  7015. } else if (ac->io_mode == io_compressed) {
  7016. lbuf_phys_addr = (param->paddr - 64);
  7017. dir = OUT;
  7018. } else {
  7019. if (param->flags & COMPRESSED_TIMESTAMP_FLAG)
  7020. lbuf_phys_addr = param->paddr -
  7021. sizeof(struct snd_codec_metadata);
  7022. else
  7023. lbuf_phys_addr = param->paddr;
  7024. dir = OUT;
  7025. }
  7026. list_for_each_safe(ptr, next, &ac->port[dir].mem_map_handle) {
  7027. buf_node = list_entry(ptr, struct asm_buffer_node,
  7028. list);
  7029. if (buf_node->buf_phys_addr == lbuf_phys_addr) {
  7030. read.mem_map_handle = buf_node->mmap_hdl;
  7031. break;
  7032. }
  7033. }
  7034. rc = apr_send_pkt(ac->apr, (uint32_t *) &read);
  7035. if (rc < 0) {
  7036. pr_err("%s: read op[0x%x]rc[%d]\n", __func__,
  7037. read.hdr.opcode, rc);
  7038. goto fail_cmd;
  7039. }
  7040. return 0;
  7041. fail_cmd:
  7042. return -EINVAL;
  7043. }
  7044. int q6asm_write(struct audio_client *ac, uint32_t len, uint32_t msw_ts,
  7045. uint32_t lsw_ts, uint32_t flags)
  7046. {
  7047. int rc = 0;
  7048. struct asm_data_cmd_write_v2 write;
  7049. struct asm_buffer_node *buf_node = NULL;
  7050. struct audio_port_data *port;
  7051. struct audio_buffer *ab;
  7052. int dsp_buf = 0;
  7053. if (ac == NULL) {
  7054. pr_err("%s: APR handle NULL\n", __func__);
  7055. return -EINVAL;
  7056. }
  7057. if (ac->apr == NULL) {
  7058. pr_err("%s: AC APR handle NULL\n", __func__);
  7059. return -EINVAL;
  7060. }
  7061. dev_vdbg(ac->dev, "%s: session[%d] len=%d\n",
  7062. __func__, ac->session, len);
  7063. if (ac->io_mode & SYNC_IO_MODE) {
  7064. port = &ac->port[IN];
  7065. q6asm_add_hdr(ac, &write.hdr, sizeof(write),
  7066. FALSE);
  7067. mutex_lock(&port->lock);
  7068. dsp_buf = port->dsp_buf;
  7069. ab = &port->buf[dsp_buf];
  7070. q6asm_update_token(&write.hdr.token,
  7071. 0, /* Session ID is NA */
  7072. 0, /* Stream ID is NA */
  7073. port->dsp_buf,
  7074. 0, /* Direction flag is NA */
  7075. NO_WAIT_CMD);
  7076. write.hdr.opcode = ASM_DATA_CMD_WRITE_V2;
  7077. write.buf_addr_lsw = lower_32_bits(ab->phys);
  7078. write.buf_addr_msw = msm_audio_populate_upper_32_bits(ab->phys);
  7079. write.buf_size = len;
  7080. write.seq_id = port->dsp_buf;
  7081. write.timestamp_lsw = lsw_ts;
  7082. write.timestamp_msw = msw_ts;
  7083. /* Use 0xFF00 for disabling timestamps */
  7084. if (flags == 0xFF00)
  7085. write.flags = (0x00000000 | (flags & 0x800000FF));
  7086. else
  7087. write.flags = (0x80000000 | flags);
  7088. port->dsp_buf = q6asm_get_next_buf(ac, port->dsp_buf,
  7089. port->max_buf_cnt);
  7090. buf_node = list_first_entry(&ac->port[IN].mem_map_handle,
  7091. struct asm_buffer_node,
  7092. list);
  7093. write.mem_map_handle = buf_node->mmap_hdl;
  7094. dev_vdbg(ac->dev, "%s: ab->phys[%pK]bufadd[0x%x] token[0x%x]buf_id[0x%x]buf_size[0x%x]mmaphdl[0x%x]"
  7095. , __func__,
  7096. &ab->phys,
  7097. write.buf_addr_lsw,
  7098. write.hdr.token,
  7099. write.seq_id,
  7100. write.buf_size,
  7101. write.mem_map_handle);
  7102. mutex_unlock(&port->lock);
  7103. config_debug_fs_write(ab);
  7104. rc = apr_send_pkt(ac->apr, (uint32_t *) &write);
  7105. if (rc < 0) {
  7106. pr_err("%s: write op[0x%x]rc[%d]\n",
  7107. __func__, write.hdr.opcode, rc);
  7108. goto fail_cmd;
  7109. }
  7110. return 0;
  7111. }
  7112. fail_cmd:
  7113. return -EINVAL;
  7114. }
  7115. int q6asm_write_nolock(struct audio_client *ac, uint32_t len, uint32_t msw_ts,
  7116. uint32_t lsw_ts, uint32_t flags)
  7117. {
  7118. int rc = 0;
  7119. struct asm_data_cmd_write_v2 write;
  7120. struct asm_buffer_node *buf_node = NULL;
  7121. struct audio_port_data *port;
  7122. struct audio_buffer *ab;
  7123. int dsp_buf = 0;
  7124. if (ac == NULL) {
  7125. pr_err("%s: APR handle NULL\n", __func__);
  7126. return -EINVAL;
  7127. }
  7128. if (ac->apr == NULL) {
  7129. pr_err("%s: AC APR handle NULL\n", __func__);
  7130. return -EINVAL;
  7131. }
  7132. dev_vdbg(ac->dev, "%s: session[%d] len=%d\n",
  7133. __func__, ac->session, len);
  7134. if (ac->io_mode & SYNC_IO_MODE) {
  7135. port = &ac->port[IN];
  7136. q6asm_add_hdr_async(ac, &write.hdr, sizeof(write),
  7137. FALSE);
  7138. dsp_buf = port->dsp_buf;
  7139. ab = &port->buf[dsp_buf];
  7140. q6asm_update_token(&write.hdr.token,
  7141. 0, /* Session ID is NA */
  7142. 0, /* Stream ID is NA */
  7143. port->dsp_buf,
  7144. 0, /* Direction flag is NA */
  7145. NO_WAIT_CMD);
  7146. write.hdr.opcode = ASM_DATA_CMD_WRITE_V2;
  7147. write.buf_addr_lsw = lower_32_bits(ab->phys);
  7148. write.buf_addr_msw = msm_audio_populate_upper_32_bits(ab->phys);
  7149. write.buf_size = len;
  7150. write.seq_id = port->dsp_buf;
  7151. write.timestamp_lsw = lsw_ts;
  7152. write.timestamp_msw = msw_ts;
  7153. buf_node = list_first_entry(&ac->port[IN].mem_map_handle,
  7154. struct asm_buffer_node,
  7155. list);
  7156. write.mem_map_handle = buf_node->mmap_hdl;
  7157. /* Use 0xFF00 for disabling timestamps */
  7158. if (flags == 0xFF00)
  7159. write.flags = (0x00000000 | (flags & 0x800000FF));
  7160. else
  7161. write.flags = (0x80000000 | flags);
  7162. port->dsp_buf = q6asm_get_next_buf(ac, port->dsp_buf,
  7163. port->max_buf_cnt);
  7164. dev_vdbg(ac->dev, "%s: ab->phys[%pK]bufadd[0x%x]token[0x%x] buf_id[0x%x]buf_size[0x%x]mmaphdl[0x%x]"
  7165. , __func__,
  7166. &ab->phys,
  7167. write.buf_addr_lsw,
  7168. write.hdr.token,
  7169. write.seq_id,
  7170. write.buf_size,
  7171. write.mem_map_handle);
  7172. rc = apr_send_pkt(ac->apr, (uint32_t *) &write);
  7173. if (rc < 0) {
  7174. pr_err("%s: write op[0x%x]rc[%d]\n",
  7175. __func__, write.hdr.opcode, rc);
  7176. goto fail_cmd;
  7177. }
  7178. return 0;
  7179. }
  7180. fail_cmd:
  7181. return -EINVAL;
  7182. }
  7183. int q6asm_get_session_time(struct audio_client *ac, uint64_t *tstamp)
  7184. {
  7185. struct asm_mtmx_strtr_get_params mtmx_params;
  7186. int rc;
  7187. if (ac == NULL) {
  7188. pr_err("%s: APR handle NULL\n", __func__);
  7189. return -EINVAL;
  7190. }
  7191. if (ac->apr == NULL) {
  7192. pr_err("%s: AC APR handle NULL\n", __func__);
  7193. return -EINVAL;
  7194. }
  7195. if (tstamp == NULL) {
  7196. pr_err("%s: tstamp NULL\n", __func__);
  7197. return -EINVAL;
  7198. }
  7199. q6asm_add_hdr(ac, &mtmx_params.hdr, sizeof(mtmx_params), TRUE);
  7200. mtmx_params.hdr.opcode = ASM_SESSION_CMD_GET_MTMX_STRTR_PARAMS_V2;
  7201. mtmx_params.param_info.data_payload_addr_lsw = 0;
  7202. mtmx_params.param_info.data_payload_addr_msw = 0;
  7203. mtmx_params.param_info.mem_map_handle = 0;
  7204. mtmx_params.param_info.direction = (ac->io_mode & TUN_READ_IO_MODE
  7205. ? 1 : 0);
  7206. mtmx_params.param_info.module_id =
  7207. ASM_SESSION_MTMX_STRTR_MODULE_ID_AVSYNC;
  7208. mtmx_params.param_info.param_id =
  7209. ASM_SESSION_MTMX_STRTR_PARAM_SESSION_TIME_V3;
  7210. mtmx_params.param_info.param_max_size =
  7211. sizeof(struct asm_stream_param_data_v2) +
  7212. sizeof(struct asm_session_mtmx_strtr_param_session_time_v3_t);
  7213. atomic_set(&ac->time_flag, 1);
  7214. dev_vdbg(ac->dev, "%s: session[%d]opcode[0x%x]\n", __func__,
  7215. ac->session, mtmx_params.hdr.opcode);
  7216. rc = apr_send_pkt(ac->apr, (uint32_t *) &mtmx_params);
  7217. if (rc < 0) {
  7218. pr_err("%s: Commmand 0x%x failed %d\n", __func__,
  7219. mtmx_params.hdr.opcode, rc);
  7220. goto fail_cmd;
  7221. }
  7222. rc = wait_event_timeout(ac->time_wait,
  7223. (atomic_read(&ac->time_flag) == 0), 5*HZ);
  7224. if (!rc) {
  7225. pr_err("%s: timeout in getting session time from DSP\n",
  7226. __func__);
  7227. goto fail_cmd;
  7228. }
  7229. *tstamp = ac->time_stamp;
  7230. return 0;
  7231. fail_cmd:
  7232. return -EINVAL;
  7233. }
  7234. int q6asm_get_session_time_legacy(struct audio_client *ac, uint64_t *tstamp)
  7235. {
  7236. struct apr_hdr hdr;
  7237. int rc;
  7238. if (ac == NULL) {
  7239. pr_err("%s: APR handle NULL\n", __func__);
  7240. return -EINVAL;
  7241. }
  7242. if (ac->apr == NULL) {
  7243. pr_err("%s: AC APR handle NULL\n", __func__);
  7244. return -EINVAL;
  7245. }
  7246. if (tstamp == NULL) {
  7247. pr_err("%s: tstamp NULL\n", __func__);
  7248. return -EINVAL;
  7249. }
  7250. q6asm_add_hdr(ac, &hdr, sizeof(hdr), TRUE);
  7251. hdr.opcode = ASM_SESSION_CMD_GET_SESSIONTIME_V3;
  7252. atomic_set(&ac->time_flag, 1);
  7253. dev_vdbg(ac->dev, "%s: session[%d]opcode[0x%x]\n", __func__,
  7254. ac->session,
  7255. hdr.opcode);
  7256. rc = apr_send_pkt(ac->apr, (uint32_t *) &hdr);
  7257. if (rc < 0) {
  7258. pr_err("%s: Commmand 0x%x failed %d\n",
  7259. __func__, hdr.opcode, rc);
  7260. goto fail_cmd;
  7261. }
  7262. rc = wait_event_timeout(ac->time_wait,
  7263. (atomic_read(&ac->time_flag) == 0), 5*HZ);
  7264. if (!rc) {
  7265. pr_err("%s: timeout in getting session time from DSP\n",
  7266. __func__);
  7267. goto fail_cmd;
  7268. }
  7269. *tstamp = ac->time_stamp;
  7270. return 0;
  7271. fail_cmd:
  7272. return -EINVAL;
  7273. }
  7274. int q6asm_send_audio_effects_params(struct audio_client *ac, char *params,
  7275. uint32_t params_length)
  7276. {
  7277. char *asm_params = NULL;
  7278. struct apr_hdr hdr;
  7279. struct asm_stream_cmd_set_pp_params_v2 payload_params;
  7280. int sz, rc;
  7281. pr_debug("%s:\n", __func__);
  7282. if (!ac) {
  7283. pr_err("%s: APR handle NULL\n", __func__);
  7284. return -EINVAL;
  7285. }
  7286. if (ac->apr == NULL) {
  7287. pr_err("%s: AC APR handle NULL\n", __func__);
  7288. return -EINVAL;
  7289. }
  7290. if (params == NULL) {
  7291. pr_err("%s: params NULL\n", __func__);
  7292. return -EINVAL;
  7293. }
  7294. sz = sizeof(struct apr_hdr) +
  7295. sizeof(struct asm_stream_cmd_set_pp_params_v2) +
  7296. params_length;
  7297. asm_params = kzalloc(sz, GFP_KERNEL);
  7298. if (!asm_params) {
  7299. pr_err("%s, asm params memory alloc failed", __func__);
  7300. return -ENOMEM;
  7301. }
  7302. q6asm_add_hdr_async(ac, &hdr, (sizeof(struct apr_hdr) +
  7303. sizeof(struct asm_stream_cmd_set_pp_params_v2) +
  7304. params_length), TRUE);
  7305. atomic_set(&ac->cmd_state_pp, -1);
  7306. hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  7307. payload_params.data_payload_addr_lsw = 0;
  7308. payload_params.data_payload_addr_msw = 0;
  7309. payload_params.mem_map_handle = 0;
  7310. payload_params.data_payload_size = params_length;
  7311. memcpy(((u8 *)asm_params), &hdr, sizeof(struct apr_hdr));
  7312. memcpy(((u8 *)asm_params + sizeof(struct apr_hdr)), &payload_params,
  7313. sizeof(struct asm_stream_cmd_set_pp_params_v2));
  7314. memcpy(((u8 *)asm_params + sizeof(struct apr_hdr) +
  7315. sizeof(struct asm_stream_cmd_set_pp_params_v2)),
  7316. params, params_length);
  7317. rc = apr_send_pkt(ac->apr, (uint32_t *) asm_params);
  7318. if (rc < 0) {
  7319. pr_err("%s: audio effects set-params send failed\n", __func__);
  7320. rc = -EINVAL;
  7321. goto fail_send_param;
  7322. }
  7323. rc = wait_event_timeout(ac->cmd_wait,
  7324. (atomic_read(&ac->cmd_state_pp) >= 0), 1*HZ);
  7325. if (!rc) {
  7326. pr_err("%s: timeout, audio effects set-params\n", __func__);
  7327. rc = -ETIMEDOUT;
  7328. goto fail_send_param;
  7329. }
  7330. if (atomic_read(&ac->cmd_state_pp) > 0) {
  7331. pr_err("%s: DSP returned error[%s] set-params\n",
  7332. __func__, adsp_err_get_err_str(
  7333. atomic_read(&ac->cmd_state_pp)));
  7334. rc = adsp_err_get_lnx_err_code(
  7335. atomic_read(&ac->cmd_state_pp));
  7336. goto fail_send_param;
  7337. }
  7338. rc = 0;
  7339. fail_send_param:
  7340. kfree(asm_params);
  7341. return rc;
  7342. }
  7343. int q6asm_send_mtmx_strtr_window(struct audio_client *ac,
  7344. struct asm_session_mtmx_strtr_param_window_v2_t *window_param,
  7345. uint32_t param_id)
  7346. {
  7347. struct asm_mtmx_strtr_params matrix;
  7348. int sz = 0;
  7349. int rc = 0;
  7350. pr_debug("%s: Window lsw is %d, window msw is %d\n", __func__,
  7351. window_param->window_lsw, window_param->window_msw);
  7352. if (!ac) {
  7353. pr_err("%s: audio client handle is NULL\n", __func__);
  7354. rc = -EINVAL;
  7355. goto fail_cmd;
  7356. }
  7357. if (ac->apr == NULL) {
  7358. pr_err("%s: ac->apr is NULL", __func__);
  7359. rc = -EINVAL;
  7360. goto fail_cmd;
  7361. }
  7362. sz = sizeof(struct asm_mtmx_strtr_params);
  7363. q6asm_add_hdr(ac, &matrix.hdr, sz, TRUE);
  7364. atomic_set(&ac->cmd_state, -1);
  7365. matrix.hdr.opcode = ASM_SESSION_CMD_SET_MTMX_STRTR_PARAMS_V2;
  7366. matrix.param.data_payload_addr_lsw = 0;
  7367. matrix.param.data_payload_addr_msw = 0;
  7368. matrix.param.mem_map_handle = 0;
  7369. matrix.param.data_payload_size =
  7370. sizeof(struct asm_stream_param_data_v2) +
  7371. sizeof(struct asm_session_mtmx_strtr_param_window_v2_t);
  7372. matrix.param.direction = 0; /* RX */
  7373. matrix.data.module_id = ASM_SESSION_MTMX_STRTR_MODULE_ID_AVSYNC;
  7374. matrix.data.param_id = param_id;
  7375. matrix.data.param_size =
  7376. sizeof(struct asm_session_mtmx_strtr_param_window_v2_t);
  7377. matrix.data.reserved = 0;
  7378. memcpy(&(matrix.config.window_param),
  7379. window_param,
  7380. sizeof(struct asm_session_mtmx_strtr_param_window_v2_t));
  7381. rc = apr_send_pkt(ac->apr, (uint32_t *) &matrix);
  7382. if (rc < 0) {
  7383. pr_err("%s: Render window start send failed paramid [0x%x]\n",
  7384. __func__, matrix.data.param_id);
  7385. rc = -EINVAL;
  7386. goto fail_cmd;
  7387. }
  7388. rc = wait_event_timeout(ac->cmd_wait,
  7389. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  7390. if (!rc) {
  7391. pr_err("%s: timeout, Render window start paramid[0x%x]\n",
  7392. __func__, matrix.data.param_id);
  7393. rc = -ETIMEDOUT;
  7394. goto fail_cmd;
  7395. }
  7396. if (atomic_read(&ac->cmd_state) > 0) {
  7397. pr_err("%s: DSP returned error[%s]\n",
  7398. __func__, adsp_err_get_err_str(
  7399. atomic_read(&ac->cmd_state)));
  7400. rc = adsp_err_get_lnx_err_code(
  7401. atomic_read(&ac->cmd_state));
  7402. goto fail_cmd;
  7403. }
  7404. rc = 0;
  7405. fail_cmd:
  7406. return rc;
  7407. }
  7408. int q6asm_send_mtmx_strtr_render_mode(struct audio_client *ac,
  7409. uint32_t render_mode)
  7410. {
  7411. struct asm_mtmx_strtr_params matrix;
  7412. struct asm_session_mtmx_strtr_param_render_mode_t render_param;
  7413. int sz = 0;
  7414. int rc = 0;
  7415. pr_debug("%s: render mode is %d\n", __func__, render_mode);
  7416. if (!ac) {
  7417. pr_err("%s: audio client handle is NULL\n", __func__);
  7418. rc = -EINVAL;
  7419. goto exit;
  7420. }
  7421. if (ac->apr == NULL) {
  7422. pr_err("%s: ac->apr is NULL\n", __func__);
  7423. rc = -EINVAL;
  7424. goto exit;
  7425. }
  7426. if ((render_mode != ASM_SESSION_MTMX_STRTR_PARAM_RENDER_DEFAULT) &&
  7427. (render_mode != ASM_SESSION_MTMX_STRTR_PARAM_RENDER_LOCAL_STC)) {
  7428. pr_err("%s: Invalid render mode %d\n", __func__, render_mode);
  7429. rc = -EINVAL;
  7430. goto exit;
  7431. }
  7432. memset(&render_param, 0,
  7433. sizeof(struct asm_session_mtmx_strtr_param_render_mode_t));
  7434. render_param.flags = render_mode;
  7435. memset(&matrix, 0, sizeof(struct asm_mtmx_strtr_params));
  7436. sz = sizeof(struct asm_mtmx_strtr_params);
  7437. q6asm_add_hdr(ac, &matrix.hdr, sz, TRUE);
  7438. atomic_set(&ac->cmd_state, -1);
  7439. matrix.hdr.opcode = ASM_SESSION_CMD_SET_MTMX_STRTR_PARAMS_V2;
  7440. matrix.param.data_payload_addr_lsw = 0;
  7441. matrix.param.data_payload_addr_msw = 0;
  7442. matrix.param.mem_map_handle = 0;
  7443. matrix.param.data_payload_size =
  7444. sizeof(struct asm_stream_param_data_v2) +
  7445. sizeof(struct asm_session_mtmx_strtr_param_render_mode_t);
  7446. matrix.param.direction = 0; /* RX */
  7447. matrix.data.module_id = ASM_SESSION_MTMX_STRTR_MODULE_ID_AVSYNC;
  7448. matrix.data.param_id = ASM_SESSION_MTMX_STRTR_PARAM_RENDER_MODE_CMD;
  7449. matrix.data.param_size =
  7450. sizeof(struct asm_session_mtmx_strtr_param_render_mode_t);
  7451. matrix.data.reserved = 0;
  7452. memcpy(&(matrix.config.render_param),
  7453. &render_param,
  7454. sizeof(struct asm_session_mtmx_strtr_param_render_mode_t));
  7455. rc = apr_send_pkt(ac->apr, (uint32_t *) &matrix);
  7456. if (rc < 0) {
  7457. pr_err("%s: Render mode send failed paramid [0x%x]\n",
  7458. __func__, matrix.data.param_id);
  7459. rc = -EINVAL;
  7460. goto exit;
  7461. }
  7462. rc = wait_event_timeout(ac->cmd_wait,
  7463. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  7464. if (!rc) {
  7465. pr_err("%s: timeout, Render mode send paramid [0x%x]\n",
  7466. __func__, matrix.data.param_id);
  7467. rc = -ETIMEDOUT;
  7468. goto exit;
  7469. }
  7470. if (atomic_read(&ac->cmd_state) > 0) {
  7471. pr_err("%s: DSP returned error[%s]\n",
  7472. __func__, adsp_err_get_err_str(
  7473. atomic_read(&ac->cmd_state)));
  7474. rc = adsp_err_get_lnx_err_code(
  7475. atomic_read(&ac->cmd_state));
  7476. goto exit;
  7477. }
  7478. rc = 0;
  7479. exit:
  7480. return rc;
  7481. }
  7482. int q6asm_send_mtmx_strtr_clk_rec_mode(struct audio_client *ac,
  7483. uint32_t clk_rec_mode)
  7484. {
  7485. struct asm_mtmx_strtr_params matrix;
  7486. struct asm_session_mtmx_strtr_param_clk_rec_t clk_rec_param;
  7487. int sz = 0;
  7488. int rc = 0;
  7489. pr_debug("%s: clk rec mode is %d\n", __func__, clk_rec_mode);
  7490. if (!ac) {
  7491. pr_err("%s: audio client handle is NULL\n", __func__);
  7492. rc = -EINVAL;
  7493. goto exit;
  7494. }
  7495. if (ac->apr == NULL) {
  7496. pr_err("%s: ac->apr is NULL\n", __func__);
  7497. rc = -EINVAL;
  7498. goto exit;
  7499. }
  7500. if ((clk_rec_mode != ASM_SESSION_MTMX_STRTR_PARAM_CLK_REC_NONE) &&
  7501. (clk_rec_mode != ASM_SESSION_MTMX_STRTR_PARAM_CLK_REC_AUTO)) {
  7502. pr_err("%s: Invalid clk rec mode %d\n", __func__, clk_rec_mode);
  7503. rc = -EINVAL;
  7504. goto exit;
  7505. }
  7506. memset(&clk_rec_param, 0,
  7507. sizeof(struct asm_session_mtmx_strtr_param_clk_rec_t));
  7508. clk_rec_param.flags = clk_rec_mode;
  7509. memset(&matrix, 0, sizeof(struct asm_mtmx_strtr_params));
  7510. sz = sizeof(struct asm_mtmx_strtr_params);
  7511. q6asm_add_hdr(ac, &matrix.hdr, sz, TRUE);
  7512. atomic_set(&ac->cmd_state, -1);
  7513. matrix.hdr.opcode = ASM_SESSION_CMD_SET_MTMX_STRTR_PARAMS_V2;
  7514. matrix.param.data_payload_addr_lsw = 0;
  7515. matrix.param.data_payload_addr_msw = 0;
  7516. matrix.param.mem_map_handle = 0;
  7517. matrix.param.data_payload_size =
  7518. sizeof(struct asm_stream_param_data_v2) +
  7519. sizeof(struct asm_session_mtmx_strtr_param_clk_rec_t);
  7520. matrix.param.direction = 0; /* RX */
  7521. matrix.data.module_id = ASM_SESSION_MTMX_STRTR_MODULE_ID_AVSYNC;
  7522. matrix.data.param_id = ASM_SESSION_MTMX_STRTR_PARAM_CLK_REC_CMD;
  7523. matrix.data.param_size =
  7524. sizeof(struct asm_session_mtmx_strtr_param_clk_rec_t);
  7525. matrix.data.reserved = 0;
  7526. memcpy(&(matrix.config.clk_rec_param),
  7527. &clk_rec_param,
  7528. sizeof(struct asm_session_mtmx_strtr_param_clk_rec_t));
  7529. rc = apr_send_pkt(ac->apr, (uint32_t *) &matrix);
  7530. if (rc < 0) {
  7531. pr_err("%s: clk rec mode send failed paramid [0x%x]\n",
  7532. __func__, matrix.data.param_id);
  7533. rc = -EINVAL;
  7534. goto exit;
  7535. }
  7536. rc = wait_event_timeout(ac->cmd_wait,
  7537. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  7538. if (!rc) {
  7539. pr_err("%s: timeout, clk rec mode send paramid [0x%x]\n",
  7540. __func__, matrix.data.param_id);
  7541. rc = -ETIMEDOUT;
  7542. goto exit;
  7543. }
  7544. if (atomic_read(&ac->cmd_state) > 0) {
  7545. pr_err("%s: DSP returned error[%s]\n",
  7546. __func__, adsp_err_get_err_str(
  7547. atomic_read(&ac->cmd_state)));
  7548. rc = adsp_err_get_lnx_err_code(
  7549. atomic_read(&ac->cmd_state));
  7550. goto exit;
  7551. }
  7552. rc = 0;
  7553. exit:
  7554. return rc;
  7555. }
  7556. int q6asm_send_mtmx_strtr_enable_adjust_session_clock(struct audio_client *ac,
  7557. bool enable)
  7558. {
  7559. struct asm_mtmx_strtr_params matrix;
  7560. struct asm_session_mtmx_param_adjust_session_time_ctl_t adjust_time;
  7561. int sz = 0;
  7562. int rc = 0;
  7563. pr_debug("%s: adjust session enable %d\n", __func__, enable);
  7564. if (!ac) {
  7565. pr_err("%s: audio client handle is NULL\n", __func__);
  7566. rc = -EINVAL;
  7567. goto exit;
  7568. }
  7569. if (ac->apr == NULL) {
  7570. pr_err("%s: ac->apr is NULL\n", __func__);
  7571. rc = -EINVAL;
  7572. goto exit;
  7573. }
  7574. adjust_time.enable = enable;
  7575. memset(&matrix, 0, sizeof(struct asm_mtmx_strtr_params));
  7576. sz = sizeof(struct asm_mtmx_strtr_params);
  7577. q6asm_add_hdr(ac, &matrix.hdr, sz, TRUE);
  7578. atomic_set(&ac->cmd_state, -1);
  7579. matrix.hdr.opcode = ASM_SESSION_CMD_SET_MTMX_STRTR_PARAMS_V2;
  7580. matrix.param.data_payload_addr_lsw = 0;
  7581. matrix.param.data_payload_addr_msw = 0;
  7582. matrix.param.mem_map_handle = 0;
  7583. matrix.param.data_payload_size =
  7584. sizeof(struct asm_stream_param_data_v2) +
  7585. sizeof(struct asm_session_mtmx_param_adjust_session_time_ctl_t);
  7586. matrix.param.direction = 0; /* RX */
  7587. matrix.data.module_id = ASM_SESSION_MTMX_STRTR_MODULE_ID_AVSYNC;
  7588. matrix.data.param_id = ASM_SESSION_MTMX_PARAM_ADJUST_SESSION_TIME_CTL;
  7589. matrix.data.param_size =
  7590. sizeof(struct asm_session_mtmx_param_adjust_session_time_ctl_t);
  7591. matrix.data.reserved = 0;
  7592. matrix.config.adj_time_param.enable = adjust_time.enable;
  7593. rc = apr_send_pkt(ac->apr, (uint32_t *) &matrix);
  7594. if (rc < 0) {
  7595. pr_err("%s: enable adjust session failed failed paramid [0x%x]\n",
  7596. __func__, matrix.data.param_id);
  7597. rc = -EINVAL;
  7598. goto exit;
  7599. }
  7600. rc = wait_event_timeout(ac->cmd_wait,
  7601. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  7602. if (!rc) {
  7603. pr_err("%s: enable adjust session failed failed paramid [0x%x]\n",
  7604. __func__, matrix.data.param_id);
  7605. rc = -ETIMEDOUT;
  7606. goto exit;
  7607. }
  7608. if (atomic_read(&ac->cmd_state) > 0) {
  7609. pr_err("%s: DSP returned error[%s]\n",
  7610. __func__, adsp_err_get_err_str(
  7611. atomic_read(&ac->cmd_state)));
  7612. rc = adsp_err_get_lnx_err_code(
  7613. atomic_read(&ac->cmd_state));
  7614. goto exit;
  7615. }
  7616. rc = 0;
  7617. exit:
  7618. return rc;
  7619. }
  7620. static int __q6asm_cmd(struct audio_client *ac, int cmd, uint32_t stream_id)
  7621. {
  7622. struct apr_hdr hdr;
  7623. int rc;
  7624. atomic_t *state;
  7625. int cnt = 0;
  7626. if (!ac) {
  7627. pr_err("%s: APR handle NULL\n", __func__);
  7628. return -EINVAL;
  7629. }
  7630. if (ac->apr == NULL) {
  7631. pr_err("%s: AC APR handle NULL\n", __func__);
  7632. return -EINVAL;
  7633. }
  7634. q6asm_stream_add_hdr(ac, &hdr, sizeof(hdr), TRUE, stream_id);
  7635. atomic_set(&ac->cmd_state, -1);
  7636. /*
  7637. * Updated the token field with stream/session for compressed playback
  7638. * Platform driver must know the the stream with which the command is
  7639. * associated
  7640. */
  7641. if (ac->io_mode & COMPRESSED_STREAM_IO)
  7642. q6asm_update_token(&hdr.token,
  7643. ac->session,
  7644. stream_id,
  7645. 0, /* Buffer index is NA */
  7646. 0, /* Direction flag is NA */
  7647. WAIT_CMD);
  7648. pr_debug("%s: token = 0x%x, stream_id %d, session 0x%x\n",
  7649. __func__, hdr.token, stream_id, ac->session);
  7650. switch (cmd) {
  7651. case CMD_PAUSE:
  7652. pr_debug("%s: CMD_PAUSE\n", __func__);
  7653. hdr.opcode = ASM_SESSION_CMD_PAUSE;
  7654. state = &ac->cmd_state;
  7655. break;
  7656. case CMD_SUSPEND:
  7657. pr_debug("%s: CMD_SUSPEND\n", __func__);
  7658. hdr.opcode = ASM_SESSION_CMD_SUSPEND;
  7659. state = &ac->cmd_state;
  7660. break;
  7661. case CMD_FLUSH:
  7662. pr_debug("%s: CMD_FLUSH\n", __func__);
  7663. hdr.opcode = ASM_STREAM_CMD_FLUSH;
  7664. state = &ac->cmd_state;
  7665. break;
  7666. case CMD_OUT_FLUSH:
  7667. pr_debug("%s: CMD_OUT_FLUSH\n", __func__);
  7668. hdr.opcode = ASM_STREAM_CMD_FLUSH_READBUFS;
  7669. state = &ac->cmd_state;
  7670. break;
  7671. case CMD_EOS:
  7672. pr_debug("%s: CMD_EOS\n", __func__);
  7673. hdr.opcode = ASM_DATA_CMD_EOS;
  7674. atomic_set(&ac->cmd_state, 0);
  7675. state = &ac->cmd_state;
  7676. break;
  7677. case CMD_CLOSE:
  7678. pr_debug("%s: CMD_CLOSE\n", __func__);
  7679. hdr.opcode = ASM_STREAM_CMD_CLOSE;
  7680. state = &ac->cmd_state;
  7681. break;
  7682. default:
  7683. pr_err("%s: Invalid format[%d]\n", __func__, cmd);
  7684. rc = -EINVAL;
  7685. goto fail_cmd;
  7686. }
  7687. pr_debug("%s: session[%d]opcode[0x%x]\n", __func__,
  7688. ac->session,
  7689. hdr.opcode);
  7690. rc = apr_send_pkt(ac->apr, (uint32_t *) &hdr);
  7691. if (rc < 0) {
  7692. pr_err("%s: Commmand 0x%x failed %d\n",
  7693. __func__, hdr.opcode, rc);
  7694. rc = -EINVAL;
  7695. goto fail_cmd;
  7696. }
  7697. rc = wait_event_timeout(ac->cmd_wait, (atomic_read(state) >= 0), 5*HZ);
  7698. if (!rc) {
  7699. pr_err("%s: timeout. waited for response opcode[0x%x]\n",
  7700. __func__, hdr.opcode);
  7701. rc = -ETIMEDOUT;
  7702. goto fail_cmd;
  7703. }
  7704. if (atomic_read(state) > 0) {
  7705. pr_err("%s: DSP returned error[%s] opcode %d\n",
  7706. __func__, adsp_err_get_err_str(
  7707. atomic_read(state)),
  7708. hdr.opcode);
  7709. rc = adsp_err_get_lnx_err_code(atomic_read(state));
  7710. goto fail_cmd;
  7711. }
  7712. if (cmd == CMD_FLUSH)
  7713. q6asm_reset_buf_state(ac);
  7714. if (cmd == CMD_CLOSE) {
  7715. /* check if DSP return all buffers */
  7716. if (ac->port[IN].buf) {
  7717. for (cnt = 0; cnt < ac->port[IN].max_buf_cnt;
  7718. cnt++) {
  7719. if (ac->port[IN].buf[cnt].used == IN) {
  7720. dev_vdbg(ac->dev, "Write Buf[%d] not returned\n",
  7721. cnt);
  7722. }
  7723. }
  7724. }
  7725. if (ac->port[OUT].buf) {
  7726. for (cnt = 0; cnt < ac->port[OUT].max_buf_cnt; cnt++) {
  7727. if (ac->port[OUT].buf[cnt].used == OUT) {
  7728. dev_vdbg(ac->dev, "Read Buf[%d] not returned\n",
  7729. cnt);
  7730. }
  7731. }
  7732. }
  7733. }
  7734. return 0;
  7735. fail_cmd:
  7736. return rc;
  7737. }
  7738. int q6asm_cmd(struct audio_client *ac, int cmd)
  7739. {
  7740. return __q6asm_cmd(ac, cmd, ac->stream_id);
  7741. }
  7742. int q6asm_stream_cmd(struct audio_client *ac, int cmd, uint32_t stream_id)
  7743. {
  7744. return __q6asm_cmd(ac, cmd, stream_id);
  7745. }
  7746. static int __q6asm_cmd_nowait(struct audio_client *ac, int cmd,
  7747. uint32_t stream_id)
  7748. {
  7749. struct apr_hdr hdr;
  7750. int rc;
  7751. if (!ac) {
  7752. pr_err("%s: APR handle NULL\n", __func__);
  7753. return -EINVAL;
  7754. }
  7755. if (ac->apr == NULL) {
  7756. pr_err("%s: AC APR handle NULL\n", __func__);
  7757. return -EINVAL;
  7758. }
  7759. q6asm_stream_add_hdr_async(ac, &hdr, sizeof(hdr), TRUE, stream_id);
  7760. atomic_set(&ac->cmd_state, 1);
  7761. /*
  7762. * Updated the token field with stream/session for compressed playback
  7763. * Platform driver must know the the stream with which the command is
  7764. * associated
  7765. */
  7766. if (ac->io_mode & COMPRESSED_STREAM_IO)
  7767. q6asm_update_token(&hdr.token,
  7768. ac->session,
  7769. stream_id,
  7770. 0, /* Buffer index is NA */
  7771. 0, /* Direction flag is NA */
  7772. NO_WAIT_CMD);
  7773. pr_debug("%s: token = 0x%x, stream_id %d, session 0x%x\n",
  7774. __func__, hdr.token, stream_id, ac->session);
  7775. switch (cmd) {
  7776. case CMD_PAUSE:
  7777. pr_debug("%s: CMD_PAUSE\n", __func__);
  7778. hdr.opcode = ASM_SESSION_CMD_PAUSE;
  7779. break;
  7780. case CMD_EOS:
  7781. pr_debug("%s: CMD_EOS\n", __func__);
  7782. hdr.opcode = ASM_DATA_CMD_EOS;
  7783. break;
  7784. case CMD_CLOSE:
  7785. pr_debug("%s: CMD_CLOSE\n", __func__);
  7786. hdr.opcode = ASM_STREAM_CMD_CLOSE;
  7787. break;
  7788. default:
  7789. pr_err("%s: Invalid format[%d]\n", __func__, cmd);
  7790. goto fail_cmd;
  7791. }
  7792. pr_debug("%s: session[%d]opcode[0x%x]\n", __func__,
  7793. ac->session,
  7794. hdr.opcode);
  7795. rc = apr_send_pkt(ac->apr, (uint32_t *) &hdr);
  7796. if (rc < 0) {
  7797. pr_err("%s: Commmand 0x%x failed %d\n",
  7798. __func__, hdr.opcode, rc);
  7799. goto fail_cmd;
  7800. }
  7801. return 0;
  7802. fail_cmd:
  7803. return -EINVAL;
  7804. }
  7805. int q6asm_cmd_nowait(struct audio_client *ac, int cmd)
  7806. {
  7807. pr_debug("%s: stream_id: %d\n", __func__, ac->stream_id);
  7808. return __q6asm_cmd_nowait(ac, cmd, ac->stream_id);
  7809. }
  7810. int q6asm_stream_cmd_nowait(struct audio_client *ac, int cmd,
  7811. uint32_t stream_id)
  7812. {
  7813. pr_debug("%s: stream_id: %d\n", __func__, stream_id);
  7814. return __q6asm_cmd_nowait(ac, cmd, stream_id);
  7815. }
  7816. int __q6asm_send_meta_data(struct audio_client *ac, uint32_t stream_id,
  7817. uint32_t initial_samples, uint32_t trailing_samples)
  7818. {
  7819. struct asm_data_cmd_remove_silence silence;
  7820. int rc = 0;
  7821. if (!ac) {
  7822. pr_err("%s: APR handle NULL\n", __func__);
  7823. return -EINVAL;
  7824. }
  7825. if (ac->apr == NULL) {
  7826. pr_err("%s: AC APR handle NULL\n", __func__);
  7827. return -EINVAL;
  7828. }
  7829. pr_debug("%s: session[%d]\n", __func__, ac->session);
  7830. q6asm_stream_add_hdr_async(ac, &silence.hdr, sizeof(silence), TRUE,
  7831. stream_id);
  7832. /*
  7833. * Updated the token field with stream/session for compressed playback
  7834. * Platform driver must know the the stream with which the command is
  7835. * associated
  7836. */
  7837. if (ac->io_mode & COMPRESSED_STREAM_IO)
  7838. q6asm_update_token(&silence.hdr.token,
  7839. ac->session,
  7840. stream_id,
  7841. 0, /* Buffer index is NA */
  7842. 0, /* Direction flag is NA */
  7843. NO_WAIT_CMD);
  7844. pr_debug("%s: token = 0x%x, stream_id %d, session 0x%x\n",
  7845. __func__, silence.hdr.token, stream_id, ac->session);
  7846. silence.hdr.opcode = ASM_DATA_CMD_REMOVE_INITIAL_SILENCE;
  7847. silence.num_samples_to_remove = initial_samples;
  7848. rc = apr_send_pkt(ac->apr, (uint32_t *) &silence);
  7849. if (rc < 0) {
  7850. pr_err("%s: Commmand silence failed[%d]", __func__, rc);
  7851. goto fail_cmd;
  7852. }
  7853. silence.hdr.opcode = ASM_DATA_CMD_REMOVE_TRAILING_SILENCE;
  7854. silence.num_samples_to_remove = trailing_samples;
  7855. rc = apr_send_pkt(ac->apr, (uint32_t *) &silence);
  7856. if (rc < 0) {
  7857. pr_err("%s: Commmand silence failed[%d]", __func__, rc);
  7858. goto fail_cmd;
  7859. }
  7860. return 0;
  7861. fail_cmd:
  7862. return -EINVAL;
  7863. }
  7864. int q6asm_stream_send_meta_data(struct audio_client *ac, uint32_t stream_id,
  7865. uint32_t initial_samples, uint32_t trailing_samples)
  7866. {
  7867. return __q6asm_send_meta_data(ac, stream_id, initial_samples,
  7868. trailing_samples);
  7869. }
  7870. int q6asm_send_meta_data(struct audio_client *ac, uint32_t initial_samples,
  7871. uint32_t trailing_samples)
  7872. {
  7873. return __q6asm_send_meta_data(ac, ac->stream_id, initial_samples,
  7874. trailing_samples);
  7875. }
  7876. static void q6asm_reset_buf_state(struct audio_client *ac)
  7877. {
  7878. int cnt = 0;
  7879. int loopcnt = 0;
  7880. int used;
  7881. struct audio_port_data *port = NULL;
  7882. if (ac->io_mode & SYNC_IO_MODE) {
  7883. used = (ac->io_mode & TUN_WRITE_IO_MODE ? 1 : 0);
  7884. mutex_lock(&ac->cmd_lock);
  7885. for (loopcnt = 0; loopcnt <= OUT; loopcnt++) {
  7886. port = &ac->port[loopcnt];
  7887. cnt = port->max_buf_cnt - 1;
  7888. port->dsp_buf = 0;
  7889. port->cpu_buf = 0;
  7890. while (cnt >= 0) {
  7891. if (!port->buf)
  7892. continue;
  7893. port->buf[cnt].used = used;
  7894. cnt--;
  7895. }
  7896. }
  7897. mutex_unlock(&ac->cmd_lock);
  7898. }
  7899. }
  7900. int q6asm_reg_tx_overflow(struct audio_client *ac, uint16_t enable)
  7901. {
  7902. struct asm_session_cmd_regx_overflow tx_overflow;
  7903. int rc;
  7904. if (!ac) {
  7905. pr_err("%s: APR handle NULL\n", __func__);
  7906. return -EINVAL;
  7907. }
  7908. if (ac->apr == NULL) {
  7909. pr_err("%s: AC APR handle NULL\n", __func__);
  7910. return -EINVAL;
  7911. }
  7912. pr_debug("%s: session[%d]enable[%d]\n", __func__,
  7913. ac->session, enable);
  7914. q6asm_add_hdr(ac, &tx_overflow.hdr, sizeof(tx_overflow), TRUE);
  7915. atomic_set(&ac->cmd_state, -1);
  7916. tx_overflow.hdr.opcode =
  7917. ASM_SESSION_CMD_REGISTER_FORX_OVERFLOW_EVENTS;
  7918. /* tx overflow event: enable */
  7919. tx_overflow.enable_flag = enable;
  7920. rc = apr_send_pkt(ac->apr, (uint32_t *) &tx_overflow);
  7921. if (rc < 0) {
  7922. pr_err("%s: tx overflow op[0x%x]rc[%d]\n",
  7923. __func__, tx_overflow.hdr.opcode, rc);
  7924. rc = -EINVAL;
  7925. goto fail_cmd;
  7926. }
  7927. rc = wait_event_timeout(ac->cmd_wait,
  7928. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  7929. if (!rc) {
  7930. pr_err("%s: timeout. waited for tx overflow\n", __func__);
  7931. rc = -ETIMEDOUT;
  7932. goto fail_cmd;
  7933. }
  7934. if (atomic_read(&ac->cmd_state) > 0) {
  7935. pr_err("%s: DSP returned error[%s]\n",
  7936. __func__, adsp_err_get_err_str(
  7937. atomic_read(&ac->cmd_state)));
  7938. rc = adsp_err_get_lnx_err_code(
  7939. atomic_read(&ac->cmd_state));
  7940. goto fail_cmd;
  7941. }
  7942. return 0;
  7943. fail_cmd:
  7944. return rc;
  7945. }
  7946. int q6asm_reg_rx_underflow(struct audio_client *ac, uint16_t enable)
  7947. {
  7948. struct asm_session_cmd_rgstr_rx_underflow rx_underflow;
  7949. int rc;
  7950. if (!ac) {
  7951. pr_err("%s: AC APR handle NULL\n", __func__);
  7952. return -EINVAL;
  7953. }
  7954. if (ac->apr == NULL) {
  7955. pr_err("%s: APR handle NULL\n", __func__);
  7956. return -EINVAL;
  7957. }
  7958. pr_debug("%s: session[%d]enable[%d]\n", __func__,
  7959. ac->session, enable);
  7960. q6asm_add_hdr_async(ac, &rx_underflow.hdr, sizeof(rx_underflow), FALSE);
  7961. rx_underflow.hdr.opcode =
  7962. ASM_SESSION_CMD_REGISTER_FOR_RX_UNDERFLOW_EVENTS;
  7963. /* tx overflow event: enable */
  7964. rx_underflow.enable_flag = enable;
  7965. rc = apr_send_pkt(ac->apr, (uint32_t *) &rx_underflow);
  7966. if (rc < 0) {
  7967. pr_err("%s: tx overflow op[0x%x]rc[%d]\n",
  7968. __func__, rx_underflow.hdr.opcode, rc);
  7969. goto fail_cmd;
  7970. }
  7971. return 0;
  7972. fail_cmd:
  7973. return -EINVAL;
  7974. }
  7975. int q6asm_adjust_session_clock(struct audio_client *ac,
  7976. uint32_t adjust_time_lsw,
  7977. uint32_t adjust_time_msw)
  7978. {
  7979. int rc = 0;
  7980. int sz = 0;
  7981. struct asm_session_cmd_adjust_session_clock_v2 adjust_clock;
  7982. pr_debug("%s: adjust_time_lsw is %x, adjust_time_msw is %x\n", __func__,
  7983. adjust_time_lsw, adjust_time_msw);
  7984. if (!ac) {
  7985. pr_err("%s: audio client handle is NULL\n", __func__);
  7986. rc = -EINVAL;
  7987. goto fail_cmd;
  7988. }
  7989. if (ac->apr == NULL) {
  7990. pr_err("%s: ac->apr is NULL", __func__);
  7991. rc = -EINVAL;
  7992. goto fail_cmd;
  7993. }
  7994. sz = sizeof(struct asm_session_cmd_adjust_session_clock_v2);
  7995. q6asm_add_hdr(ac, &adjust_clock.hdr, sz, TRUE);
  7996. atomic_set(&ac->cmd_state, -1);
  7997. adjust_clock.hdr.opcode = ASM_SESSION_CMD_ADJUST_SESSION_CLOCK_V2;
  7998. adjust_clock.adjustime_lsw = adjust_time_lsw;
  7999. adjust_clock.adjustime_msw = adjust_time_msw;
  8000. rc = apr_send_pkt(ac->apr, (uint32_t *) &adjust_clock);
  8001. if (rc < 0) {
  8002. pr_err("%s: adjust_clock send failed paramid [0x%x]\n",
  8003. __func__, adjust_clock.hdr.opcode);
  8004. rc = -EINVAL;
  8005. goto fail_cmd;
  8006. }
  8007. rc = wait_event_timeout(ac->cmd_wait,
  8008. (atomic_read(&ac->cmd_state) >= 0), 5*HZ);
  8009. if (!rc) {
  8010. pr_err("%s: timeout, adjust_clock paramid[0x%x]\n",
  8011. __func__, adjust_clock.hdr.opcode);
  8012. rc = -ETIMEDOUT;
  8013. goto fail_cmd;
  8014. }
  8015. if (atomic_read(&ac->cmd_state) > 0) {
  8016. pr_err("%s: DSP returned error[%s]\n",
  8017. __func__, adsp_err_get_err_str(
  8018. atomic_read(&ac->cmd_state)));
  8019. rc = adsp_err_get_lnx_err_code(
  8020. atomic_read(&ac->cmd_state));
  8021. goto fail_cmd;
  8022. }
  8023. rc = 0;
  8024. fail_cmd:
  8025. return rc;
  8026. }
  8027. /*
  8028. * q6asm_get_path_delay() - get the path delay for an audio session
  8029. * @ac: audio client handle
  8030. *
  8031. * Retrieves the current audio DSP path delay for the given audio session.
  8032. *
  8033. * Return: 0 on success, error code otherwise
  8034. */
  8035. int q6asm_get_path_delay(struct audio_client *ac)
  8036. {
  8037. int rc = 0;
  8038. struct apr_hdr hdr;
  8039. if (!ac || ac->apr == NULL) {
  8040. pr_err("%s: invalid audio client\n", __func__);
  8041. return -EINVAL;
  8042. }
  8043. hdr.opcode = ASM_SESSION_CMD_GET_PATH_DELAY_V2;
  8044. q6asm_add_hdr(ac, &hdr, sizeof(hdr), TRUE);
  8045. atomic_set(&ac->cmd_state, -1);
  8046. rc = apr_send_pkt(ac->apr, (uint32_t *) &hdr);
  8047. if (rc < 0) {
  8048. pr_err("%s: Commmand 0x%x failed %d\n", __func__,
  8049. hdr.opcode, rc);
  8050. return rc;
  8051. }
  8052. rc = wait_event_timeout(ac->cmd_wait,
  8053. (atomic_read(&ac->cmd_state) >= 0), 5 * HZ);
  8054. if (!rc) {
  8055. pr_err("%s: timeout. waited for response opcode[0x%x]\n",
  8056. __func__, hdr.opcode);
  8057. return -ETIMEDOUT;
  8058. }
  8059. if (atomic_read(&ac->cmd_state) > 0) {
  8060. pr_err("%s: DSP returned error[%s]\n",
  8061. __func__, adsp_err_get_err_str(
  8062. atomic_read(&ac->cmd_state)));
  8063. rc = adsp_err_get_lnx_err_code(
  8064. atomic_read(&ac->cmd_state));
  8065. return rc;
  8066. }
  8067. return 0;
  8068. }
  8069. int q6asm_get_apr_service_id(int session_id)
  8070. {
  8071. pr_debug("%s:\n", __func__);
  8072. if (session_id <= 0 || session_id > ASM_ACTIVE_STREAMS_ALLOWED) {
  8073. pr_err("%s: invalid session_id = %d\n", __func__, session_id);
  8074. return -EINVAL;
  8075. }
  8076. return ((struct apr_svc *)session[session_id]->apr)->id;
  8077. }
  8078. int q6asm_get_asm_topology(int session_id)
  8079. {
  8080. int topology = -EINVAL;
  8081. if (session_id <= 0 || session_id > ASM_ACTIVE_STREAMS_ALLOWED) {
  8082. pr_err("%s: invalid session_id = %d\n", __func__, session_id);
  8083. goto done;
  8084. }
  8085. if (session[session_id] == NULL) {
  8086. pr_err("%s: session not created for session id = %d\n",
  8087. __func__, session_id);
  8088. goto done;
  8089. }
  8090. topology = session[session_id]->topology;
  8091. done:
  8092. return topology;
  8093. }
  8094. int q6asm_get_asm_app_type(int session_id)
  8095. {
  8096. int app_type = -EINVAL;
  8097. if (session_id <= 0 || session_id > ASM_ACTIVE_STREAMS_ALLOWED) {
  8098. pr_err("%s: invalid session_id = %d\n", __func__, session_id);
  8099. goto done;
  8100. }
  8101. if (session[session_id] == NULL) {
  8102. pr_err("%s: session not created for session id = %d\n",
  8103. __func__, session_id);
  8104. goto done;
  8105. }
  8106. app_type = session[session_id]->app_type;
  8107. done:
  8108. return app_type;
  8109. }
  8110. static int q6asm_get_asm_topology_cal(void)
  8111. {
  8112. int topology = DEFAULT_POPP_TOPOLOGY;
  8113. struct cal_block_data *cal_block = NULL;
  8114. if (cal_data[ASM_TOPOLOGY_CAL] == NULL)
  8115. goto done;
  8116. mutex_lock(&cal_data[ASM_TOPOLOGY_CAL]->lock);
  8117. cal_block = cal_utils_get_only_cal_block(cal_data[ASM_TOPOLOGY_CAL]);
  8118. if (cal_block == NULL)
  8119. goto unlock;
  8120. topology = ((struct audio_cal_info_asm_top *)
  8121. cal_block->cal_info)->topology;
  8122. unlock:
  8123. mutex_unlock(&cal_data[ASM_TOPOLOGY_CAL]->lock);
  8124. done:
  8125. pr_debug("%s: Using topology %d\n", __func__, topology);
  8126. return topology;
  8127. }
  8128. static int q6asm_get_asm_app_type_cal(void)
  8129. {
  8130. int app_type = DEFAULT_APP_TYPE;
  8131. struct cal_block_data *cal_block = NULL;
  8132. if (cal_data[ASM_TOPOLOGY_CAL] == NULL)
  8133. goto done;
  8134. mutex_lock(&cal_data[ASM_TOPOLOGY_CAL]->lock);
  8135. cal_block = cal_utils_get_only_cal_block(cal_data[ASM_TOPOLOGY_CAL]);
  8136. if (cal_block == NULL)
  8137. goto unlock;
  8138. app_type = ((struct audio_cal_info_asm_top *)
  8139. cal_block->cal_info)->app_type;
  8140. if (app_type == 0)
  8141. app_type = DEFAULT_APP_TYPE;
  8142. unlock:
  8143. mutex_unlock(&cal_data[ASM_TOPOLOGY_CAL]->lock);
  8144. done:
  8145. pr_debug("%s: Using app_type %d\n", __func__, app_type);
  8146. return app_type;
  8147. }
  8148. int q6asm_send_cal(struct audio_client *ac)
  8149. {
  8150. struct cal_block_data *cal_block = NULL;
  8151. struct apr_hdr hdr;
  8152. char *asm_params = NULL;
  8153. struct asm_stream_cmd_set_pp_params_v2 payload_params;
  8154. int sz, rc = -EINVAL;
  8155. pr_debug("%s:\n", __func__);
  8156. if (!ac) {
  8157. pr_err("%s: APR handle NULL\n", __func__);
  8158. goto done;
  8159. }
  8160. if (ac->apr == NULL) {
  8161. pr_err("%s: AC APR handle NULL\n", __func__);
  8162. goto done;
  8163. }
  8164. if (ac->io_mode & NT_MODE) {
  8165. pr_debug("%s: called for NT MODE, exiting\n", __func__);
  8166. goto done;
  8167. }
  8168. if (cal_data[ASM_AUDSTRM_CAL] == NULL)
  8169. goto done;
  8170. if (ac->perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) {
  8171. rc = 0; /* no cal is required, not error case */
  8172. goto done;
  8173. }
  8174. mutex_lock(&cal_data[ASM_AUDSTRM_CAL]->lock);
  8175. cal_block = cal_utils_get_only_cal_block(cal_data[ASM_AUDSTRM_CAL]);
  8176. if (cal_block == NULL) {
  8177. pr_err("%s: cal_block is NULL\n",
  8178. __func__);
  8179. goto unlock;
  8180. }
  8181. if (cal_block->cal_data.size == 0) {
  8182. rc = 0; /* not error case */
  8183. pr_debug("%s: cal_data.size is 0, don't send cal data\n",
  8184. __func__);
  8185. goto unlock;
  8186. }
  8187. rc = remap_cal_data(ASM_AUDSTRM_CAL_TYPE, cal_block);
  8188. if (rc) {
  8189. pr_err("%s: Remap_cal_data failed for cal %d!\n",
  8190. __func__, ASM_AUDSTRM_CAL);
  8191. goto unlock;
  8192. }
  8193. sz = sizeof(struct apr_hdr) +
  8194. sizeof(struct asm_stream_cmd_set_pp_params_v2);
  8195. asm_params = kzalloc(sz, GFP_KERNEL);
  8196. if (!asm_params) {
  8197. pr_err("%s, asm params memory alloc failed", __func__);
  8198. rc = -ENOMEM;
  8199. goto unlock;
  8200. }
  8201. /* asm_stream_cmd_set_pp_params_v2 has no APR header in it */
  8202. q6asm_add_hdr_async(ac, &hdr, (sizeof(struct apr_hdr) +
  8203. sizeof(struct asm_stream_cmd_set_pp_params_v2)), TRUE);
  8204. atomic_set(&ac->cmd_state_pp, -1);
  8205. hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  8206. payload_params.data_payload_addr_lsw =
  8207. lower_32_bits(cal_block->cal_data.paddr);
  8208. payload_params.data_payload_addr_msw =
  8209. msm_audio_populate_upper_32_bits(
  8210. cal_block->cal_data.paddr);
  8211. payload_params.mem_map_handle = cal_block->map_data.q6map_handle;
  8212. payload_params.data_payload_size = cal_block->cal_data.size;
  8213. memcpy(((u8 *)asm_params), &hdr, sizeof(struct apr_hdr));
  8214. memcpy(((u8 *)asm_params + sizeof(struct apr_hdr)), &payload_params,
  8215. sizeof(struct asm_stream_cmd_set_pp_params_v2));
  8216. pr_debug("%s: phyaddr lsw = %x msw = %x, maphdl = %x calsize = %d\n",
  8217. __func__, payload_params.data_payload_addr_lsw,
  8218. payload_params.data_payload_addr_msw,
  8219. payload_params.mem_map_handle,
  8220. payload_params.data_payload_size);
  8221. rc = apr_send_pkt(ac->apr, (uint32_t *) asm_params);
  8222. if (rc < 0) {
  8223. pr_err("%s: audio audstrm cal send failed\n", __func__);
  8224. rc = -EINVAL;
  8225. goto free;
  8226. }
  8227. rc = wait_event_timeout(ac->cmd_wait,
  8228. (atomic_read(&ac->cmd_state_pp) >= 0), 5 * HZ);
  8229. if (!rc) {
  8230. pr_err("%s: timeout, audio audstrm cal send\n", __func__);
  8231. rc = -ETIMEDOUT;
  8232. goto free;
  8233. }
  8234. if (atomic_read(&ac->cmd_state_pp) > 0) {
  8235. pr_err("%s: DSP returned error[%d] audio audstrm cal send\n",
  8236. __func__, atomic_read(&ac->cmd_state_pp));
  8237. rc = -EINVAL;
  8238. goto free;
  8239. }
  8240. rc = 0;
  8241. free:
  8242. kfree(asm_params);
  8243. unlock:
  8244. mutex_unlock(&cal_data[ASM_AUDSTRM_CAL]->lock);
  8245. done:
  8246. return rc;
  8247. }
  8248. static int get_cal_type_index(int32_t cal_type)
  8249. {
  8250. int ret = -EINVAL;
  8251. switch (cal_type) {
  8252. case ASM_TOPOLOGY_CAL_TYPE:
  8253. ret = ASM_TOPOLOGY_CAL;
  8254. break;
  8255. case ASM_CUST_TOPOLOGY_CAL_TYPE:
  8256. ret = ASM_CUSTOM_TOP_CAL;
  8257. break;
  8258. case ASM_AUDSTRM_CAL_TYPE:
  8259. ret = ASM_AUDSTRM_CAL;
  8260. break;
  8261. case ASM_RTAC_APR_CAL_TYPE:
  8262. ret = ASM_RTAC_APR_CAL;
  8263. break;
  8264. default:
  8265. pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
  8266. }
  8267. return ret;
  8268. }
  8269. static int q6asm_alloc_cal(int32_t cal_type,
  8270. size_t data_size, void *data)
  8271. {
  8272. int ret = 0;
  8273. int cal_index;
  8274. pr_debug("%s:\n", __func__);
  8275. cal_index = get_cal_type_index(cal_type);
  8276. if (cal_index < 0) {
  8277. pr_err("%s: could not get cal index %d!\n",
  8278. __func__, cal_index);
  8279. ret = -EINVAL;
  8280. goto done;
  8281. }
  8282. ret = cal_utils_alloc_cal(data_size, data,
  8283. cal_data[cal_index], 0, NULL);
  8284. if (ret < 0) {
  8285. pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
  8286. __func__, ret, cal_type);
  8287. ret = -EINVAL;
  8288. goto done;
  8289. }
  8290. done:
  8291. return ret;
  8292. }
  8293. static int q6asm_dealloc_cal(int32_t cal_type,
  8294. size_t data_size, void *data)
  8295. {
  8296. int ret = 0;
  8297. int cal_index;
  8298. pr_debug("%s:\n", __func__);
  8299. cal_index = get_cal_type_index(cal_type);
  8300. if (cal_index < 0) {
  8301. pr_err("%s: could not get cal index %d!\n",
  8302. __func__, cal_index);
  8303. ret = -EINVAL;
  8304. goto done;
  8305. }
  8306. ret = cal_utils_dealloc_cal(data_size, data,
  8307. cal_data[cal_index]);
  8308. if (ret < 0) {
  8309. pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
  8310. __func__, ret, cal_type);
  8311. ret = -EINVAL;
  8312. goto done;
  8313. }
  8314. done:
  8315. return ret;
  8316. }
  8317. static int q6asm_set_cal(int32_t cal_type,
  8318. size_t data_size, void *data)
  8319. {
  8320. int ret = 0;
  8321. int cal_index;
  8322. pr_debug("%s:\n", __func__);
  8323. cal_index = get_cal_type_index(cal_type);
  8324. if (cal_index < 0) {
  8325. pr_err("%s: could not get cal index %d!\n",
  8326. __func__, cal_index);
  8327. ret = -EINVAL;
  8328. goto done;
  8329. }
  8330. ret = cal_utils_set_cal(data_size, data,
  8331. cal_data[cal_index], 0, NULL);
  8332. if (ret < 0) {
  8333. pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
  8334. __func__, ret, cal_type);
  8335. ret = -EINVAL;
  8336. goto done;
  8337. }
  8338. if (cal_index == ASM_CUSTOM_TOP_CAL) {
  8339. mutex_lock(&cal_data[ASM_CUSTOM_TOP_CAL]->lock);
  8340. set_custom_topology = 1;
  8341. mutex_unlock(&cal_data[ASM_CUSTOM_TOP_CAL]->lock);
  8342. }
  8343. done:
  8344. return ret;
  8345. }
  8346. static void q6asm_delete_cal_data(void)
  8347. {
  8348. pr_debug("%s:\n", __func__);
  8349. cal_utils_destroy_cal_types(ASM_MAX_CAL_TYPES, cal_data);
  8350. }
  8351. static int q6asm_init_cal_data(void)
  8352. {
  8353. int ret = 0;
  8354. struct cal_type_info cal_type_info[] = {
  8355. {{ASM_TOPOLOGY_CAL_TYPE,
  8356. {NULL, NULL, NULL,
  8357. q6asm_set_cal, NULL, NULL} },
  8358. {NULL, NULL, cal_utils_match_buf_num} },
  8359. {{ASM_CUST_TOPOLOGY_CAL_TYPE,
  8360. {q6asm_alloc_cal, q6asm_dealloc_cal, NULL,
  8361. q6asm_set_cal, NULL, NULL} },
  8362. {NULL, q6asm_unmap_cal_memory, cal_utils_match_buf_num} },
  8363. {{ASM_AUDSTRM_CAL_TYPE,
  8364. {q6asm_alloc_cal, q6asm_dealloc_cal, NULL,
  8365. q6asm_set_cal, NULL, NULL} },
  8366. {NULL, q6asm_unmap_cal_memory, cal_utils_match_buf_num} },
  8367. {{ASM_RTAC_APR_CAL_TYPE,
  8368. {NULL, NULL, NULL, NULL, NULL, NULL} },
  8369. {NULL, NULL, cal_utils_match_buf_num} }
  8370. };
  8371. pr_debug("%s\n", __func__);
  8372. ret = cal_utils_create_cal_types(ASM_MAX_CAL_TYPES, cal_data,
  8373. cal_type_info);
  8374. if (ret < 0) {
  8375. pr_err("%s: could not create cal type! %d\n",
  8376. __func__, ret);
  8377. ret = -EINVAL;
  8378. goto err;
  8379. }
  8380. return ret;
  8381. err:
  8382. q6asm_delete_cal_data();
  8383. return ret;
  8384. }
  8385. static int q6asm_is_valid_session(struct apr_client_data *data, void *priv)
  8386. {
  8387. struct audio_client *ac = (struct audio_client *)priv;
  8388. union asm_token_struct asm_token;
  8389. asm_token.token = data->token;
  8390. if (asm_token._token.session_id != ac->session) {
  8391. pr_err("%s: Invalid session[%d] rxed expected[%d]",
  8392. __func__, asm_token._token.session_id, ac->session);
  8393. return -EINVAL;
  8394. }
  8395. return 0;
  8396. }
  8397. static int __init q6asm_init(void)
  8398. {
  8399. int lcnt, ret;
  8400. pr_debug("%s:\n", __func__);
  8401. memset(session, 0, sizeof(session));
  8402. set_custom_topology = 1;
  8403. /*setup common client used for cal mem map */
  8404. common_client.session = ASM_CONTROL_SESSION;
  8405. common_client.port[0].buf = &common_buf[0];
  8406. common_client.port[1].buf = &common_buf[1];
  8407. init_waitqueue_head(&common_client.cmd_wait);
  8408. init_waitqueue_head(&common_client.time_wait);
  8409. init_waitqueue_head(&common_client.mem_wait);
  8410. atomic_set(&common_client.time_flag, 1);
  8411. INIT_LIST_HEAD(&common_client.port[0].mem_map_handle);
  8412. INIT_LIST_HEAD(&common_client.port[1].mem_map_handle);
  8413. mutex_init(&common_client.cmd_lock);
  8414. for (lcnt = 0; lcnt <= OUT; lcnt++) {
  8415. mutex_init(&common_client.port[lcnt].lock);
  8416. spin_lock_init(&common_client.port[lcnt].dsp_lock);
  8417. }
  8418. atomic_set(&common_client.cmd_state, 0);
  8419. atomic_set(&common_client.mem_state, 0);
  8420. ret = q6asm_init_cal_data();
  8421. if (ret)
  8422. pr_err("%s: could not init cal data! ret %d\n",
  8423. __func__, ret);
  8424. config_debug_fs_init();
  8425. return 0;
  8426. }
  8427. static void __exit q6asm_exit(void)
  8428. {
  8429. q6asm_delete_cal_data();
  8430. }
  8431. device_initcall(q6asm_init);
  8432. __exitcall(q6asm_exit);