q6asm.c 293 KB

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