q6asm.c 307 KB

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