q6voice.c 264 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/kthread.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/wait.h>
  11. #include <linux/mutex.h>
  12. #include <soc/qcom/socinfo.h>
  13. #include <dsp/msm_audio_ion.h>
  14. #include <dsp/q6audio-v2.h>
  15. #include <dsp/apr_audio-v2.h>
  16. #include <dsp/q6afe-v2.h>
  17. #include <dsp/q6common.h>
  18. #include <dsp/audio_cal_utils.h>
  19. #include <dsp/q6core.h>
  20. #include <dsp/q6voice.h>
  21. #include <ipc/apr_tal.h>
  22. #include "adsp_err.h"
  23. #include <dsp/voice_mhi.h>
  24. #include <soc/qcom/secure_buffer.h>
  25. #define TIMEOUT_MS 300
  26. #define CMD_STATUS_SUCCESS 0
  27. #define CMD_STATUS_FAIL 1
  28. #define NUM_CHANNELS_MONO 1
  29. #define NUM_CHANNELS_STEREO 2
  30. #define NUM_CHANNELS_THREE 3
  31. #define NUM_CHANNELS_QUAD 4
  32. #define CVP_VERSION_2 2
  33. #define GAIN_Q14_FORMAT(a) (a << 14)
  34. enum {
  35. VOC_TOKEN_NONE,
  36. VOIP_MEM_MAP_TOKEN,
  37. VOC_CAL_MEM_MAP_TOKEN,
  38. VOC_VOICE_HOST_PCM_MAP_TOKEN,
  39. VOC_RTAC_MEM_MAP_TOKEN,
  40. VOC_SOURCE_TRACKING_MEM_MAP_TOKEN
  41. };
  42. struct cvd_version_table cvd_version_table_mapping[CVD_INT_VERSION_MAX] = {
  43. {CVD_VERSION_DEFAULT, CVD_INT_VERSION_DEFAULT},
  44. {CVD_VERSION_0_0, CVD_INT_VERSION_0_0},
  45. {CVD_VERSION_2_1, CVD_INT_VERSION_2_1},
  46. {CVD_VERSION_2_2, CVD_INT_VERSION_2_2},
  47. {CVD_VERSION_2_3, CVD_INT_VERSION_2_3},
  48. {CVD_VERSION_2_4, CVD_INT_VERSION_2_4},
  49. };
  50. static struct common_data common;
  51. static bool module_initialized;
  52. static bool hyp_assigned;
  53. static int voice_send_enable_vocproc_cmd(struct voice_data *v);
  54. static int voice_send_netid_timing_cmd(struct voice_data *v);
  55. static int voice_send_attach_vocproc_cmd(struct voice_data *v);
  56. static int voice_send_set_device_cmd(struct voice_data *v);
  57. static int voice_send_vol_step_cmd(struct voice_data *v);
  58. static int voice_send_mvm_unmap_memory_physical_cmd(struct voice_data *v,
  59. uint32_t mem_handle);
  60. static int voice_send_mvm_cal_network_cmd(struct voice_data *v);
  61. static int voice_send_mvm_media_type_cmd(struct voice_data *v);
  62. static int voice_send_mvm_cvd_version_cmd(struct voice_data *v);
  63. static int voice_send_mvm_event_class_cmd(struct voice_data *v,
  64. uint32_t event_id,
  65. uint32_t class_id);
  66. static int voice_send_cvs_data_exchange_mode_cmd(struct voice_data *v);
  67. static int voice_send_cvs_packet_exchange_config_cmd(struct voice_data *v);
  68. static int voice_set_packet_exchange_mode_and_config(uint32_t session_id,
  69. uint32_t mode);
  70. static int voice_send_cvs_register_cal_cmd(struct voice_data *v);
  71. static int voice_send_cvs_deregister_cal_cmd(struct voice_data *v);
  72. static int voice_send_cvp_create_cmd(struct voice_data *v);
  73. static int voice_send_cvp_register_dev_cfg_cmd(struct voice_data *v);
  74. static int voice_send_cvp_deregister_dev_cfg_cmd(struct voice_data *v);
  75. static int voice_send_cvp_register_cal_cmd(struct voice_data *v);
  76. static int voice_send_cvp_deregister_cal_cmd(struct voice_data *v);
  77. static int voice_send_cvp_register_vol_cal_cmd(struct voice_data *v);
  78. static int voice_send_cvp_deregister_vol_cal_cmd(struct voice_data *v);
  79. static int voice_send_cvp_media_fmt_info_cmd(struct voice_data *v);
  80. static int voice_send_cvp_device_channels_cmd(struct voice_data *v);
  81. static int voice_send_cvp_media_format_cmd(struct voice_data *v,
  82. uint32_t param_type);
  83. static int voice_send_cvp_topology_commit_cmd(struct voice_data *v);
  84. static int voice_send_cvp_channel_info_cmd(struct voice_data *v);
  85. static int voice_send_cvp_channel_info_v2(struct voice_data *v,
  86. uint32_t param_type);
  87. static int voice_get_avcs_version_per_service(uint32_t service_id);
  88. static void voice_load_topo_modules(int cal_index);
  89. static void voice_unload_topo_modules(void);
  90. static int voice_cvs_stop_playback(struct voice_data *v);
  91. static int voice_cvs_start_playback(struct voice_data *v);
  92. static int voice_cvs_start_record(struct voice_data *v, uint32_t rec_mode,
  93. uint32_t port_id);
  94. static int voice_cvs_stop_record(struct voice_data *v);
  95. static int32_t qdsp_mvm_callback(struct apr_client_data *data, void *priv);
  96. static int32_t qdsp_cvs_callback(struct apr_client_data *data, void *priv);
  97. static int32_t qdsp_cvp_callback(struct apr_client_data *data, void *priv);
  98. static int voice_send_set_pp_enable_cmd(
  99. struct voice_data *v, struct module_instance_info mod_inst_info,
  100. int enable);
  101. static int voice_send_cvp_ecns_enable_cmd(struct voice_data *v,
  102. uint32_t module_id, int enable);
  103. static int is_cal_memory_allocated(void);
  104. static bool is_cvd_version_queried(void);
  105. static int is_voip_memory_allocated(void);
  106. static int voice_get_cvd_int_version(char *cvd_ver_string);
  107. static int voice_alloc_cal_mem_map_table(void);
  108. static int voice_alloc_rtac_mem_map_table(void);
  109. static int voice_alloc_oob_shared_mem(void);
  110. static int voice_free_oob_shared_mem(void);
  111. static int voice_alloc_oob_mem_table(void);
  112. static int voice_alloc_and_map_oob_mem(struct voice_data *v);
  113. static struct voice_data *voice_get_session_by_idx(int idx);
  114. static int remap_cal_data(struct cal_block_data *cal_block,
  115. uint32_t session_id);
  116. static int voice_unmap_cal_memory(int32_t cal_type,
  117. struct cal_block_data *cal_block);
  118. static int is_source_tracking_shared_memomry_allocated(void);
  119. static int voice_alloc_source_tracking_shared_memory(void);
  120. static int voice_alloc_and_map_source_tracking_shared_memory(
  121. struct voice_data *v);
  122. static int voice_unmap_and_free_source_tracking_shared_memory(
  123. struct voice_data *v);
  124. static int voice_send_set_sound_focus_cmd(struct voice_data *v,
  125. struct sound_focus_param soundFocusData);
  126. static int voice_send_get_sound_focus_cmd(struct voice_data *v,
  127. struct sound_focus_param *soundFocusData);
  128. static int voice_send_get_source_tracking_cmd(struct voice_data *v,
  129. struct source_tracking_param *sourceTrackingData);
  130. static int voice_pack_and_set_cvp_param(struct voice_data *v,
  131. struct param_hdr_v3 param_hdr,
  132. u8 *param_data);
  133. static int voice_pack_and_set_cvs_ui_property(struct voice_data *v,
  134. struct param_hdr_v3 param_hdr,
  135. u8 *param_data);
  136. static void voice_itr_init(struct voice_session_itr *itr,
  137. u32 session_id)
  138. {
  139. if (itr == NULL)
  140. return;
  141. itr->session_idx = voice_get_idx_for_session(session_id);
  142. if (session_id == ALL_SESSION_VSID)
  143. itr->cur_idx = 0;
  144. else
  145. itr->cur_idx = itr->session_idx;
  146. }
  147. static bool voice_itr_get_next_session(struct voice_session_itr *itr,
  148. struct voice_data **voice)
  149. {
  150. bool ret = false;
  151. if (itr == NULL)
  152. return false;
  153. pr_debug("%s : cur idx = %d session idx = %d\n",
  154. __func__, itr->cur_idx, itr->session_idx);
  155. if (itr->cur_idx <= itr->session_idx) {
  156. ret = true;
  157. *voice = voice_get_session_by_idx(itr->cur_idx);
  158. itr->cur_idx++;
  159. } else {
  160. *voice = NULL;
  161. }
  162. return ret;
  163. }
  164. static bool voice_is_valid_session_id(uint32_t session_id)
  165. {
  166. bool ret = false;
  167. switch (session_id) {
  168. case VOICE_SESSION_VSID:
  169. case VOICE2_SESSION_VSID:
  170. case VOLTE_SESSION_VSID:
  171. case VOIP_SESSION_VSID:
  172. case QCHAT_SESSION_VSID:
  173. case VOWLAN_SESSION_VSID:
  174. case VOICEMMODE1_VSID:
  175. case VOICEMMODE2_VSID:
  176. case ALL_SESSION_VSID:
  177. ret = true;
  178. break;
  179. default:
  180. pr_err("%s: Invalid session_id : %x\n", __func__, session_id);
  181. break;
  182. }
  183. return ret;
  184. }
  185. static u16 voice_get_mvm_handle(struct voice_data *v)
  186. {
  187. if (v == NULL) {
  188. pr_err("%s: v is NULL\n", __func__);
  189. return 0;
  190. }
  191. pr_debug("%s: mvm_handle %d\n", __func__, v->mvm_handle);
  192. return v->mvm_handle;
  193. }
  194. static void voice_set_mvm_handle(struct voice_data *v, u16 mvm_handle)
  195. {
  196. pr_debug("%s: mvm_handle %d\n", __func__, mvm_handle);
  197. if (v == NULL) {
  198. pr_err("%s: v is NULL\n", __func__);
  199. return;
  200. }
  201. v->mvm_handle = mvm_handle;
  202. }
  203. static u16 voice_get_cvs_handle(struct voice_data *v)
  204. {
  205. if (v == NULL) {
  206. pr_err("%s: v is NULL\n", __func__);
  207. return 0;
  208. }
  209. pr_debug("%s: cvs_handle %d\n", __func__, v->cvs_handle);
  210. return v->cvs_handle;
  211. }
  212. static void voice_set_cvs_handle(struct voice_data *v, u16 cvs_handle)
  213. {
  214. pr_debug("%s: cvs_handle %d\n", __func__, cvs_handle);
  215. if (v == NULL) {
  216. pr_err("%s: v is NULL\n", __func__);
  217. return;
  218. }
  219. v->cvs_handle = cvs_handle;
  220. }
  221. static u16 voice_get_cvp_handle(struct voice_data *v)
  222. {
  223. if (v == NULL) {
  224. pr_err("%s: v is NULL\n", __func__);
  225. return 0;
  226. }
  227. pr_debug("%s: cvp_handle %d\n", __func__, v->cvp_handle);
  228. return v->cvp_handle;
  229. }
  230. static void voice_set_cvp_handle(struct voice_data *v, u16 cvp_handle)
  231. {
  232. pr_debug("%s: cvp_handle %d\n", __func__, cvp_handle);
  233. if (v == NULL) {
  234. pr_err("%s: v is NULL\n", __func__);
  235. return;
  236. }
  237. v->cvp_handle = cvp_handle;
  238. }
  239. char *voc_get_session_name(u32 session_id)
  240. {
  241. char *session_name = NULL;
  242. if (session_id == common.voice[VOC_PATH_PASSIVE].session_id) {
  243. session_name = VOICE_SESSION_NAME;
  244. } else if (session_id ==
  245. common.voice[VOC_PATH_VOLTE_PASSIVE].session_id) {
  246. session_name = VOLTE_SESSION_NAME;
  247. } else if (session_id ==
  248. common.voice[VOC_PATH_QCHAT_PASSIVE].session_id) {
  249. session_name = QCHAT_SESSION_NAME;
  250. } else if (session_id ==
  251. common.voice[VOC_PATH_VOWLAN_PASSIVE].session_id) {
  252. session_name = VOWLAN_SESSION_NAME;
  253. } else if (session_id ==
  254. common.voice[VOC_PATH_VOICEMMODE1_PASSIVE].session_id) {
  255. session_name = VOICEMMODE1_NAME;
  256. } else if (session_id ==
  257. common.voice[VOC_PATH_VOICEMMODE2_PASSIVE].session_id) {
  258. session_name = VOICEMMODE2_NAME;
  259. } else if (session_id == common.voice[VOC_PATH_FULL].session_id) {
  260. session_name = VOIP_SESSION_NAME;
  261. }
  262. return session_name;
  263. }
  264. /**
  265. * voc_get_session_id -
  266. * Get session ID of given voice session name
  267. *
  268. * @name: voice session name
  269. *
  270. * Returns session id for valid session or 0 if invalid.
  271. */
  272. uint32_t voc_get_session_id(char *name)
  273. {
  274. u32 session_id = 0;
  275. if (name != NULL) {
  276. if (!strcmp(name, "Voice session"))
  277. session_id = common.voice[VOC_PATH_PASSIVE].session_id;
  278. else if (!strcmp(name, "Voice2 session"))
  279. session_id =
  280. common.voice[VOC_PATH_VOICE2_PASSIVE].session_id;
  281. else if (!strcmp(name, "VoLTE session"))
  282. session_id =
  283. common.voice[VOC_PATH_VOLTE_PASSIVE].session_id;
  284. else if (!strcmp(name, "QCHAT session"))
  285. session_id =
  286. common.voice[VOC_PATH_QCHAT_PASSIVE].session_id;
  287. else if (!strcmp(name, "VoWLAN session"))
  288. session_id =
  289. common.voice[VOC_PATH_VOWLAN_PASSIVE].session_id;
  290. else if (!strcmp(name, "VoiceMMode1"))
  291. session_id =
  292. common.voice[VOC_PATH_VOICEMMODE1_PASSIVE].session_id;
  293. else if (!strcmp(name, "VoiceMMode2"))
  294. session_id =
  295. common.voice[VOC_PATH_VOICEMMODE2_PASSIVE].session_id;
  296. else
  297. session_id = common.voice[VOC_PATH_FULL].session_id;
  298. pr_debug("%s: %s has session id 0x%x\n", __func__, name,
  299. session_id);
  300. }
  301. return session_id;
  302. }
  303. EXPORT_SYMBOL(voc_get_session_id);
  304. static struct voice_data *voice_get_session(u32 session_id)
  305. {
  306. struct voice_data *v = NULL;
  307. switch (session_id) {
  308. case VOICE_SESSION_VSID:
  309. v = &common.voice[VOC_PATH_PASSIVE];
  310. break;
  311. case VOICE2_SESSION_VSID:
  312. v = &common.voice[VOC_PATH_VOICE2_PASSIVE];
  313. break;
  314. case VOLTE_SESSION_VSID:
  315. v = &common.voice[VOC_PATH_VOLTE_PASSIVE];
  316. break;
  317. case VOIP_SESSION_VSID:
  318. v = &common.voice[VOC_PATH_FULL];
  319. break;
  320. case QCHAT_SESSION_VSID:
  321. v = &common.voice[VOC_PATH_QCHAT_PASSIVE];
  322. break;
  323. case VOWLAN_SESSION_VSID:
  324. v = &common.voice[VOC_PATH_VOWLAN_PASSIVE];
  325. break;
  326. case VOICEMMODE1_VSID:
  327. v = &common.voice[VOC_PATH_VOICEMMODE1_PASSIVE];
  328. break;
  329. case VOICEMMODE2_VSID:
  330. v = &common.voice[VOC_PATH_VOICEMMODE2_PASSIVE];
  331. break;
  332. case ALL_SESSION_VSID:
  333. break;
  334. default:
  335. pr_err("%s: Invalid session_id : %x\n", __func__, session_id);
  336. break;
  337. }
  338. pr_debug("%s:session_id 0x%x session handle %pK\n",
  339. __func__, session_id, v);
  340. return v;
  341. }
  342. int voice_get_idx_for_session(u32 session_id)
  343. {
  344. int idx = 0;
  345. switch (session_id) {
  346. case VOICE_SESSION_VSID:
  347. idx = VOC_PATH_PASSIVE;
  348. break;
  349. case VOICE2_SESSION_VSID:
  350. idx = VOC_PATH_VOICE2_PASSIVE;
  351. break;
  352. case VOLTE_SESSION_VSID:
  353. idx = VOC_PATH_VOLTE_PASSIVE;
  354. break;
  355. case VOIP_SESSION_VSID:
  356. idx = VOC_PATH_FULL;
  357. break;
  358. case QCHAT_SESSION_VSID:
  359. idx = VOC_PATH_QCHAT_PASSIVE;
  360. break;
  361. case VOWLAN_SESSION_VSID:
  362. idx = VOC_PATH_VOWLAN_PASSIVE;
  363. break;
  364. case VOICEMMODE1_VSID:
  365. idx = VOC_PATH_VOICEMMODE1_PASSIVE;
  366. break;
  367. case VOICEMMODE2_VSID:
  368. idx = VOC_PATH_VOICEMMODE2_PASSIVE;
  369. break;
  370. case ALL_SESSION_VSID:
  371. idx = MAX_VOC_SESSIONS - 1;
  372. break;
  373. default:
  374. pr_err("%s: Invalid session_id : %x\n", __func__, session_id);
  375. break;
  376. }
  377. return idx;
  378. }
  379. static struct voice_data *voice_get_session_by_idx(int idx)
  380. {
  381. return ((idx < 0 || idx >= MAX_VOC_SESSIONS) ?
  382. NULL : &common.voice[idx]);
  383. }
  384. static bool is_voip_session(u32 session_id)
  385. {
  386. return (session_id == common.voice[VOC_PATH_FULL].session_id);
  387. }
  388. static bool is_volte_session(u32 session_id)
  389. {
  390. return (session_id == common.voice[VOC_PATH_VOLTE_PASSIVE].session_id);
  391. }
  392. static bool is_voice2_session(u32 session_id)
  393. {
  394. return (session_id == common.voice[VOC_PATH_VOICE2_PASSIVE].session_id);
  395. }
  396. static bool is_qchat_session(u32 session_id)
  397. {
  398. return (session_id == common.voice[VOC_PATH_QCHAT_PASSIVE].session_id);
  399. }
  400. static bool is_vowlan_session(u32 session_id)
  401. {
  402. return (session_id == common.voice[VOC_PATH_VOWLAN_PASSIVE].session_id);
  403. }
  404. static bool is_voicemmode1(u32 session_id)
  405. {
  406. return session_id ==
  407. common.voice[VOC_PATH_VOICEMMODE1_PASSIVE].session_id;
  408. }
  409. static bool is_voicemmode2(u32 session_id)
  410. {
  411. return session_id ==
  412. common.voice[VOC_PATH_VOICEMMODE2_PASSIVE].session_id;
  413. }
  414. static bool is_voc_state_active(int voc_state)
  415. {
  416. if ((voc_state == VOC_RUN) ||
  417. (voc_state == VOC_CHANGE) ||
  418. (voc_state == VOC_STANDBY))
  419. return true;
  420. return false;
  421. }
  422. static void voc_set_error_state(uint16_t reset_proc)
  423. {
  424. struct voice_data *v = NULL;
  425. int i;
  426. for (i = 0; i < MAX_VOC_SESSIONS; i++) {
  427. v = &common.voice[i];
  428. if (v != NULL) {
  429. v->voc_state = VOC_ERROR;
  430. v->rec_info.recording = 0;
  431. v->music_info.playing = 0;
  432. v->music_info.force = 0;
  433. }
  434. }
  435. }
  436. static bool is_other_session_active(u32 session_id)
  437. {
  438. int i;
  439. bool ret = false;
  440. /* Check if there is other active session except the input one */
  441. for (i = 0; i < MAX_VOC_SESSIONS; i++) {
  442. if (common.voice[i].session_id == session_id)
  443. continue;
  444. if (is_voc_state_active(common.voice[i].voc_state)) {
  445. ret = true;
  446. break;
  447. }
  448. }
  449. pr_debug("%s: ret %d\n", __func__, ret);
  450. return ret;
  451. }
  452. static bool is_sub1_vsid(u32 session_id)
  453. {
  454. bool ret;
  455. switch (session_id) {
  456. case VOICE_SESSION_VSID:
  457. case VOLTE_SESSION_VSID:
  458. case VOWLAN_SESSION_VSID:
  459. case VOICEMMODE1_VSID:
  460. ret = true;
  461. break;
  462. default:
  463. ret = false;
  464. }
  465. return ret;
  466. }
  467. static bool is_sub2_vsid(u32 session_id)
  468. {
  469. bool ret;
  470. switch (session_id) {
  471. case VOICE2_SESSION_VSID:
  472. case VOICEMMODE2_VSID:
  473. ret = true;
  474. break;
  475. default:
  476. ret = false;
  477. }
  478. return ret;
  479. }
  480. static bool is_voice_app_id(u32 session_id)
  481. {
  482. return is_sub1_vsid(session_id) || is_sub2_vsid(session_id);
  483. }
  484. static void init_session_id(void)
  485. {
  486. common.voice[VOC_PATH_PASSIVE].session_id = VOICE_SESSION_VSID;
  487. common.voice[VOC_PATH_VOLTE_PASSIVE].session_id = VOLTE_SESSION_VSID;
  488. common.voice[VOC_PATH_VOICE2_PASSIVE].session_id = VOICE2_SESSION_VSID;
  489. common.voice[VOC_PATH_FULL].session_id = VOIP_SESSION_VSID;
  490. common.voice[VOC_PATH_QCHAT_PASSIVE].session_id = QCHAT_SESSION_VSID;
  491. common.voice[VOC_PATH_VOWLAN_PASSIVE].session_id = VOWLAN_SESSION_VSID;
  492. common.voice[VOC_PATH_VOICEMMODE1_PASSIVE].session_id =
  493. VOICEMMODE1_VSID;
  494. common.voice[VOC_PATH_VOICEMMODE2_PASSIVE].session_id =
  495. VOICEMMODE2_VSID;
  496. }
  497. static bool is_cvd_version_queried(void)
  498. {
  499. bool ret = 0;
  500. if (!strcmp(common.cvd_version, CVD_VERSION_DEFAULT))
  501. ret = false;
  502. else
  503. ret = true;
  504. return ret;
  505. }
  506. static int voice_get_cvd_int_version(char *cvd_ver_string)
  507. {
  508. unsigned int idx;
  509. int cvd_int_ver = CVD_INT_VERSION_DEFAULT;
  510. for (idx = 0; idx < CVD_INT_VERSION_MAX; idx++) {
  511. if (strcmp((char *)cvd_ver_string,
  512. cvd_version_table_mapping[idx].cvd_ver) == 0) {
  513. cvd_int_ver =
  514. cvd_version_table_mapping[idx].cvd_ver_int;
  515. break;
  516. }
  517. }
  518. return cvd_int_ver;
  519. }
  520. static int voice_apr_register(uint32_t session_id)
  521. {
  522. pr_debug("%s\n", __func__);
  523. mutex_lock(&common.common_lock);
  524. /* register callback to APR */
  525. if (common.apr_q6_mvm == NULL) {
  526. pr_debug("%s: Start to register MVM callback\n", __func__);
  527. common.apr_q6_mvm = apr_register("ADSP", "MVM",
  528. qdsp_mvm_callback,
  529. 0xFFFFFFFF, &common);
  530. if (common.apr_q6_mvm == NULL) {
  531. pr_err("%s: Unable to register MVM\n", __func__);
  532. goto err;
  533. }
  534. }
  535. if (common.apr_q6_cvs == NULL) {
  536. pr_debug("%s: Start to register CVS callback\n", __func__);
  537. common.apr_q6_cvs = apr_register("ADSP", "CVS",
  538. qdsp_cvs_callback,
  539. 0xFFFFFFFF, &common);
  540. if (common.apr_q6_cvs == NULL) {
  541. pr_err("%s: Unable to register CVS\n", __func__);
  542. goto err;
  543. }
  544. rtac_set_voice_handle(RTAC_CVS, common.apr_q6_cvs);
  545. }
  546. if (common.apr_q6_cvp == NULL) {
  547. pr_debug("%s: Start to register CVP callback\n", __func__);
  548. common.apr_q6_cvp = apr_register("ADSP", "CVP",
  549. qdsp_cvp_callback,
  550. 0xFFFFFFFF, &common);
  551. if (common.apr_q6_cvp == NULL) {
  552. pr_err("%s: Unable to register CVP\n", __func__);
  553. goto err;
  554. }
  555. rtac_set_voice_handle(RTAC_CVP, common.apr_q6_cvp);
  556. }
  557. mutex_unlock(&common.common_lock);
  558. return 0;
  559. err:
  560. if (common.apr_q6_cvs != NULL) {
  561. apr_deregister(common.apr_q6_cvs);
  562. common.apr_q6_cvs = NULL;
  563. rtac_set_voice_handle(RTAC_CVS, NULL);
  564. }
  565. if (common.apr_q6_mvm != NULL) {
  566. apr_deregister(common.apr_q6_mvm);
  567. common.apr_q6_mvm = NULL;
  568. }
  569. mutex_unlock(&common.common_lock);
  570. return -ENODEV;
  571. }
  572. static int voice_send_mvm_cvd_version_cmd(struct voice_data *v)
  573. {
  574. int ret;
  575. struct apr_hdr cvd_version_get_cmd;
  576. void *apr_mvm;
  577. if (v == NULL) {
  578. pr_err("%s: v is NULL\n", __func__);
  579. ret = -EINVAL;
  580. goto done;
  581. }
  582. apr_mvm = common.apr_q6_mvm;
  583. if (!apr_mvm) {
  584. pr_err("%s: apr_mvm is NULL.\n", __func__);
  585. ret = -EINVAL;
  586. goto done;
  587. }
  588. /* Send command to CVD to retrieve Version */
  589. cvd_version_get_cmd.hdr_field = APR_HDR_FIELD(
  590. APR_MSG_TYPE_SEQ_CMD,
  591. APR_HDR_LEN(APR_HDR_SIZE),
  592. APR_PKT_VER);
  593. cvd_version_get_cmd.pkt_size = APR_PKT_SIZE(
  594. APR_HDR_SIZE,
  595. sizeof(cvd_version_get_cmd) -
  596. APR_HDR_SIZE);
  597. cvd_version_get_cmd.src_port =
  598. voice_get_idx_for_session(v->session_id);
  599. cvd_version_get_cmd.dest_port = 0;
  600. cvd_version_get_cmd.token = 0;
  601. cvd_version_get_cmd.opcode = VSS_IVERSION_CMD_GET;
  602. pr_debug("%s: send CVD version get cmd, pkt size = %d\n",
  603. __func__, cvd_version_get_cmd.pkt_size);
  604. v->mvm_state = CMD_STATUS_FAIL;
  605. v->async_err = 0;
  606. ret = apr_send_pkt(apr_mvm,
  607. (uint32_t *) &cvd_version_get_cmd);
  608. if (ret < 0) {
  609. pr_err("%s: Error sending command\n", __func__);
  610. ret = -EINVAL;
  611. goto done;
  612. }
  613. ret = wait_event_timeout(v->mvm_wait,
  614. (v->mvm_state == CMD_STATUS_SUCCESS),
  615. msecs_to_jiffies(TIMEOUT_MS));
  616. if (!ret) {
  617. pr_err("%s: wait_event timeout, fall back to default\n",
  618. __func__);
  619. ret = -EINVAL;
  620. goto done;
  621. }
  622. if (v->async_err > 0) {
  623. pr_err("%s: DSP returned error[%s]\n",
  624. __func__, adsp_err_get_err_str(
  625. v->async_err));
  626. ret = adsp_err_get_lnx_err_code(
  627. v->async_err);
  628. goto done;
  629. }
  630. ret = 0;
  631. done:
  632. if (ret) {
  633. strlcpy(common.cvd_version, CVD_VERSION_0_0,
  634. sizeof(common.cvd_version));
  635. }
  636. pr_debug("%s: CVD Version retrieved=%s\n",
  637. __func__, common.cvd_version);
  638. return ret;
  639. }
  640. static int voice_send_mvm_event_class_cmd(struct voice_data *v,
  641. uint32_t event_id,
  642. uint32_t class_id)
  643. {
  644. struct vss_inotify_cmd_event_class_t mvm_event;
  645. int ret = 0;
  646. void *apr_mvm = NULL;
  647. u16 mvm_handle = 0;
  648. if (v == NULL) {
  649. pr_err("%s: v is NULL\n", __func__);
  650. return -EINVAL;
  651. }
  652. apr_mvm = common.apr_q6_mvm;
  653. if (!apr_mvm) {
  654. pr_err("%s: apr_mvm is NULL.\n", __func__);
  655. return -EINVAL;
  656. }
  657. memset(&mvm_event, 0, sizeof(mvm_event));
  658. mvm_handle = voice_get_mvm_handle(v);
  659. mvm_event.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  660. APR_HDR_LEN(APR_HDR_SIZE),
  661. APR_PKT_VER);
  662. mvm_event.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  663. sizeof(mvm_event) - APR_HDR_SIZE);
  664. mvm_event.hdr.src_port =
  665. voice_get_idx_for_session(v->session_id);
  666. mvm_event.hdr.dest_port = mvm_handle;
  667. mvm_event.hdr.token = 0;
  668. mvm_event.hdr.opcode = event_id;
  669. mvm_event.class_id = class_id;
  670. v->mvm_state = CMD_STATUS_FAIL;
  671. v->async_err = 0;
  672. ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_event);
  673. if (ret < 0) {
  674. pr_err("%s: Error %d sending %x event\n", __func__, ret,
  675. event_id);
  676. goto fail;
  677. }
  678. ret = wait_event_timeout(v->mvm_wait,
  679. (v->mvm_state == CMD_STATUS_SUCCESS),
  680. msecs_to_jiffies(TIMEOUT_MS));
  681. if (!ret) {
  682. pr_err("%s: wait_event timeout %d\n", __func__, ret);
  683. ret = -ETIMEDOUT;
  684. goto fail;
  685. }
  686. if (v->async_err > 0) {
  687. pr_err("%s: DSP returned error[%s]\n",
  688. __func__, adsp_err_get_err_str(
  689. v->async_err));
  690. ret = adsp_err_get_lnx_err_code(
  691. v->async_err);
  692. goto fail;
  693. }
  694. return 0;
  695. fail:
  696. return ret;
  697. }
  698. static int voice_send_dual_control_cmd(struct voice_data *v)
  699. {
  700. int ret = 0;
  701. struct mvm_modem_dual_control_session_cmd mvm_voice_ctl_cmd;
  702. void *apr_mvm;
  703. u16 mvm_handle;
  704. if (v == NULL) {
  705. pr_err("%s: v is NULL\n", __func__);
  706. return -EINVAL;
  707. }
  708. apr_mvm = common.apr_q6_mvm;
  709. if (!apr_mvm) {
  710. pr_err("%s: apr_mvm is NULL.\n", __func__);
  711. return -EINVAL;
  712. }
  713. pr_debug("%s: Send Dual Control command to MVM\n", __func__);
  714. if (!is_voip_session(v->session_id)) {
  715. mvm_handle = voice_get_mvm_handle(v);
  716. mvm_voice_ctl_cmd.hdr.hdr_field = APR_HDR_FIELD(
  717. APR_MSG_TYPE_SEQ_CMD,
  718. APR_HDR_LEN(APR_HDR_SIZE),
  719. APR_PKT_VER);
  720. mvm_voice_ctl_cmd.hdr.pkt_size = APR_PKT_SIZE(
  721. APR_HDR_SIZE,
  722. sizeof(mvm_voice_ctl_cmd) -
  723. APR_HDR_SIZE);
  724. pr_debug("%s: send mvm Voice Ctl pkt size = %d\n",
  725. __func__, mvm_voice_ctl_cmd.hdr.pkt_size);
  726. mvm_voice_ctl_cmd.hdr.src_port =
  727. voice_get_idx_for_session(v->session_id);
  728. mvm_voice_ctl_cmd.hdr.dest_port = mvm_handle;
  729. mvm_voice_ctl_cmd.hdr.token = 0;
  730. mvm_voice_ctl_cmd.hdr.opcode =
  731. VSS_IMVM_CMD_SET_POLICY_DUAL_CONTROL;
  732. mvm_voice_ctl_cmd.voice_ctl.enable_flag = true;
  733. v->mvm_state = CMD_STATUS_FAIL;
  734. v->async_err = 0;
  735. ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_voice_ctl_cmd);
  736. if (ret < 0) {
  737. pr_err("%s: Error sending MVM Voice CTL CMD\n",
  738. __func__);
  739. ret = -EINVAL;
  740. goto fail;
  741. }
  742. ret = wait_event_timeout(v->mvm_wait,
  743. (v->mvm_state == CMD_STATUS_SUCCESS),
  744. msecs_to_jiffies(TIMEOUT_MS));
  745. if (!ret) {
  746. pr_err("%s: wait_event timeout\n", __func__);
  747. ret = -EINVAL;
  748. goto fail;
  749. }
  750. if (v->async_err > 0) {
  751. pr_err("%s: DSP returned error[%s]\n",
  752. __func__, adsp_err_get_err_str(
  753. v->async_err));
  754. ret = adsp_err_get_lnx_err_code(
  755. v->async_err);
  756. goto fail;
  757. }
  758. }
  759. ret = 0;
  760. fail:
  761. return ret;
  762. }
  763. static int voice_create_mvm_cvs_session(struct voice_data *v)
  764. {
  765. int ret = 0;
  766. struct mvm_create_ctl_session_cmd mvm_session_cmd;
  767. struct cvs_create_passive_ctl_session_cmd cvs_session_cmd;
  768. struct cvs_create_full_ctl_session_cmd cvs_full_ctl_cmd;
  769. struct mvm_attach_stream_cmd attach_stream_cmd;
  770. void *apr_mvm, *apr_cvs, *apr_cvp;
  771. u16 mvm_handle, cvs_handle, cvp_handle;
  772. if (v == NULL) {
  773. pr_err("%s: v is NULL\n", __func__);
  774. return -EINVAL;
  775. }
  776. apr_mvm = common.apr_q6_mvm;
  777. apr_cvs = common.apr_q6_cvs;
  778. apr_cvp = common.apr_q6_cvp;
  779. if (!apr_mvm || !apr_cvs || !apr_cvp) {
  780. pr_err("%s: apr_mvm or apr_cvs or apr_cvp is NULL\n", __func__);
  781. return -EINVAL;
  782. }
  783. mvm_handle = voice_get_mvm_handle(v);
  784. cvs_handle = voice_get_cvs_handle(v);
  785. cvp_handle = voice_get_cvp_handle(v);
  786. pr_debug("%s: mvm_hdl=%d, cvs_hdl=%d\n", __func__,
  787. mvm_handle, cvs_handle);
  788. /* send cmd to create mvm session and wait for response */
  789. if (!mvm_handle) {
  790. memset(mvm_session_cmd.mvm_session.name, 0,
  791. sizeof(mvm_session_cmd.mvm_session.name));
  792. if (!is_voip_session(v->session_id)) {
  793. mvm_session_cmd.hdr.hdr_field = APR_HDR_FIELD(
  794. APR_MSG_TYPE_SEQ_CMD,
  795. APR_HDR_LEN(APR_HDR_SIZE),
  796. APR_PKT_VER);
  797. mvm_session_cmd.hdr.pkt_size = APR_PKT_SIZE(
  798. APR_HDR_SIZE,
  799. sizeof(mvm_session_cmd) -
  800. APR_HDR_SIZE);
  801. pr_debug("%s: send mvm create session pkt size = %d\n",
  802. __func__, mvm_session_cmd.hdr.pkt_size);
  803. mvm_session_cmd.hdr.src_port =
  804. voice_get_idx_for_session(v->session_id);
  805. mvm_session_cmd.hdr.dest_port = 0;
  806. mvm_session_cmd.hdr.token = 0;
  807. mvm_session_cmd.hdr.opcode =
  808. VSS_IMVM_CMD_CREATE_PASSIVE_CONTROL_SESSION;
  809. if (is_volte_session(v->session_id)) {
  810. strlcpy(mvm_session_cmd.mvm_session.name,
  811. "default volte voice",
  812. strlen("default volte voice")+1);
  813. } else if (is_voice2_session(v->session_id)) {
  814. strlcpy(mvm_session_cmd.mvm_session.name,
  815. VOICE2_SESSION_VSID_STR,
  816. strlen(VOICE2_SESSION_VSID_STR)+1);
  817. } else if (is_qchat_session(v->session_id)) {
  818. strlcpy(mvm_session_cmd.mvm_session.name,
  819. QCHAT_SESSION_VSID_STR,
  820. strlen(QCHAT_SESSION_VSID_STR)+1);
  821. } else if (is_vowlan_session(v->session_id)) {
  822. strlcpy(mvm_session_cmd.mvm_session.name,
  823. VOWLAN_SESSION_VSID_STR,
  824. strlen(VOWLAN_SESSION_VSID_STR)+1);
  825. } else if (is_voicemmode1(v->session_id)) {
  826. strlcpy(mvm_session_cmd.mvm_session.name,
  827. VOICEMMODE1_VSID_STR,
  828. strlen(VOICEMMODE1_VSID_STR) + 1);
  829. } else if (is_voicemmode2(v->session_id)) {
  830. strlcpy(mvm_session_cmd.mvm_session.name,
  831. VOICEMMODE2_VSID_STR,
  832. strlen(VOICEMMODE2_VSID_STR) + 1);
  833. } else {
  834. strlcpy(mvm_session_cmd.mvm_session.name,
  835. "default modem voice",
  836. strlen("default modem voice")+1);
  837. }
  838. v->mvm_state = CMD_STATUS_FAIL;
  839. v->async_err = 0;
  840. ret = apr_send_pkt(apr_mvm,
  841. (uint32_t *) &mvm_session_cmd);
  842. if (ret < 0) {
  843. pr_err("%s: Error sending MVM_CONTROL_SESSION\n",
  844. __func__);
  845. goto fail;
  846. }
  847. ret = wait_event_timeout(v->mvm_wait,
  848. (v->mvm_state == CMD_STATUS_SUCCESS),
  849. msecs_to_jiffies(TIMEOUT_MS));
  850. if (!ret) {
  851. pr_err("%s: wait_event timeout\n", __func__);
  852. goto fail;
  853. }
  854. if (v->async_err > 0) {
  855. pr_err("%s: DSP returned error[%s]\n",
  856. __func__, adsp_err_get_err_str(
  857. v->async_err));
  858. ret = adsp_err_get_lnx_err_code(
  859. v->async_err);
  860. goto fail;
  861. }
  862. } else {
  863. pr_debug("%s: creating MVM full ctrl\n", __func__);
  864. mvm_session_cmd.hdr.hdr_field =
  865. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  866. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  867. mvm_session_cmd.hdr.pkt_size =
  868. APR_PKT_SIZE(APR_HDR_SIZE,
  869. sizeof(mvm_session_cmd) -
  870. APR_HDR_SIZE);
  871. mvm_session_cmd.hdr.src_port =
  872. voice_get_idx_for_session(v->session_id);
  873. mvm_session_cmd.hdr.dest_port = 0;
  874. mvm_session_cmd.hdr.token = 0;
  875. mvm_session_cmd.hdr.opcode =
  876. VSS_IMVM_CMD_CREATE_FULL_CONTROL_SESSION;
  877. strlcpy(mvm_session_cmd.mvm_session.name,
  878. "default voip",
  879. strlen("default voip")+1);
  880. v->mvm_state = CMD_STATUS_FAIL;
  881. v->async_err = 0;
  882. ret = apr_send_pkt(apr_mvm,
  883. (uint32_t *) &mvm_session_cmd);
  884. if (ret < 0) {
  885. pr_err("Fail in sending MVM_CONTROL_SESSION\n");
  886. goto fail;
  887. }
  888. ret = wait_event_timeout(v->mvm_wait,
  889. (v->mvm_state == CMD_STATUS_SUCCESS),
  890. msecs_to_jiffies(TIMEOUT_MS));
  891. if (!ret) {
  892. pr_err("%s: wait_event timeout\n", __func__);
  893. goto fail;
  894. }
  895. if (v->async_err > 0) {
  896. pr_err("%s: DSP returned error[%s]\n",
  897. __func__, adsp_err_get_err_str(
  898. v->async_err));
  899. ret = adsp_err_get_lnx_err_code(
  900. v->async_err);
  901. goto fail;
  902. }
  903. }
  904. /* Get the created MVM handle. */
  905. mvm_handle = voice_get_mvm_handle(v);
  906. }
  907. /* send cmd to create cvs session */
  908. if (!cvs_handle) {
  909. memset(cvs_session_cmd.cvs_session.name, 0,
  910. sizeof(cvs_session_cmd.cvs_session.name));
  911. if (!is_voip_session(v->session_id)) {
  912. pr_debug("%s: creating CVS passive session\n",
  913. __func__);
  914. cvs_session_cmd.hdr.hdr_field = APR_HDR_FIELD(
  915. APR_MSG_TYPE_SEQ_CMD,
  916. APR_HDR_LEN(APR_HDR_SIZE),
  917. APR_PKT_VER);
  918. cvs_session_cmd.hdr.pkt_size =
  919. APR_PKT_SIZE(APR_HDR_SIZE,
  920. sizeof(cvs_session_cmd) -
  921. APR_HDR_SIZE);
  922. cvs_session_cmd.hdr.src_port =
  923. voice_get_idx_for_session(v->session_id);
  924. cvs_session_cmd.hdr.dest_port = 0;
  925. cvs_session_cmd.hdr.token = 0;
  926. cvs_session_cmd.hdr.opcode =
  927. VSS_ISTREAM_CMD_CREATE_PASSIVE_CONTROL_SESSION;
  928. if (is_volte_session(v->session_id)) {
  929. strlcpy(cvs_session_cmd.cvs_session.name,
  930. "default volte voice",
  931. strlen("default volte voice")+1);
  932. } else if (is_voice2_session(v->session_id)) {
  933. strlcpy(cvs_session_cmd.cvs_session.name,
  934. VOICE2_SESSION_VSID_STR,
  935. strlen(VOICE2_SESSION_VSID_STR)+1);
  936. } else if (is_qchat_session(v->session_id)) {
  937. strlcpy(cvs_session_cmd.cvs_session.name,
  938. QCHAT_SESSION_VSID_STR,
  939. strlen(QCHAT_SESSION_VSID_STR)+1);
  940. } else if (is_vowlan_session(v->session_id)) {
  941. strlcpy(cvs_session_cmd.cvs_session.name,
  942. VOWLAN_SESSION_VSID_STR,
  943. strlen(VOWLAN_SESSION_VSID_STR)+1);
  944. } else if (is_voicemmode1(v->session_id)) {
  945. strlcpy(cvs_session_cmd.cvs_session.name,
  946. VOICEMMODE1_VSID_STR,
  947. strlen(VOICEMMODE1_VSID_STR) + 1);
  948. } else if (is_voicemmode2(v->session_id)) {
  949. strlcpy(cvs_session_cmd.cvs_session.name,
  950. VOICEMMODE2_VSID_STR,
  951. strlen(VOICEMMODE2_VSID_STR) + 1);
  952. } else {
  953. strlcpy(cvs_session_cmd.cvs_session.name,
  954. "default modem voice",
  955. strlen("default modem voice")+1);
  956. }
  957. v->cvs_state = CMD_STATUS_FAIL;
  958. v->async_err = 0;
  959. ret = apr_send_pkt(apr_cvs,
  960. (uint32_t *) &cvs_session_cmd);
  961. if (ret < 0) {
  962. pr_err("Fail in sending STREAM_CONTROL_SESSION\n");
  963. goto fail;
  964. }
  965. ret = wait_event_timeout(v->cvs_wait,
  966. (v->cvs_state == CMD_STATUS_SUCCESS),
  967. msecs_to_jiffies(TIMEOUT_MS));
  968. if (!ret) {
  969. pr_err("%s: wait_event timeout\n", __func__);
  970. goto fail;
  971. }
  972. if (v->async_err > 0) {
  973. pr_err("%s: DSP returned error[%s]\n",
  974. __func__, adsp_err_get_err_str(
  975. v->async_err));
  976. ret = adsp_err_get_lnx_err_code(
  977. v->async_err);
  978. goto fail;
  979. }
  980. /* Get the created CVS handle. */
  981. cvs_handle = voice_get_cvs_handle(v);
  982. } else {
  983. pr_debug("%s: creating CVS full session\n", __func__);
  984. cvs_full_ctl_cmd.hdr.hdr_field =
  985. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  986. APR_HDR_LEN(APR_HDR_SIZE),
  987. APR_PKT_VER);
  988. cvs_full_ctl_cmd.hdr.pkt_size =
  989. APR_PKT_SIZE(APR_HDR_SIZE,
  990. sizeof(cvs_full_ctl_cmd) -
  991. APR_HDR_SIZE);
  992. cvs_full_ctl_cmd.hdr.src_port =
  993. voice_get_idx_for_session(v->session_id);
  994. cvs_full_ctl_cmd.hdr.dest_port = 0;
  995. cvs_full_ctl_cmd.hdr.token = 0;
  996. cvs_full_ctl_cmd.hdr.opcode =
  997. VSS_ISTREAM_CMD_CREATE_FULL_CONTROL_SESSION;
  998. cvs_full_ctl_cmd.cvs_session.direction = 2;
  999. cvs_full_ctl_cmd.cvs_session.enc_media_type =
  1000. common.mvs_info.media_type;
  1001. cvs_full_ctl_cmd.cvs_session.dec_media_type =
  1002. common.mvs_info.media_type;
  1003. cvs_full_ctl_cmd.cvs_session.network_id =
  1004. common.mvs_info.network_type;
  1005. strlcpy(cvs_full_ctl_cmd.cvs_session.name,
  1006. "default q6 voice",
  1007. strlen("default q6 voice")+1);
  1008. v->cvs_state = CMD_STATUS_FAIL;
  1009. v->async_err = 0;
  1010. ret = apr_send_pkt(apr_cvs,
  1011. (uint32_t *) &cvs_full_ctl_cmd);
  1012. if (ret < 0) {
  1013. pr_err("%s: Err %d sending CREATE_FULL_CTRL\n",
  1014. __func__, ret);
  1015. goto fail;
  1016. }
  1017. ret = wait_event_timeout(v->cvs_wait,
  1018. (v->cvs_state == CMD_STATUS_SUCCESS),
  1019. msecs_to_jiffies(TIMEOUT_MS));
  1020. if (!ret) {
  1021. pr_err("%s: wait_event timeout\n", __func__);
  1022. goto fail;
  1023. }
  1024. if (v->async_err > 0) {
  1025. pr_err("%s: DSP returned error[%s]\n",
  1026. __func__, adsp_err_get_err_str(
  1027. v->async_err));
  1028. ret = adsp_err_get_lnx_err_code(
  1029. v->async_err);
  1030. goto fail;
  1031. }
  1032. /* Get the created CVS handle. */
  1033. cvs_handle = voice_get_cvs_handle(v);
  1034. /* Attach MVM to CVS. */
  1035. pr_debug("%s: Attach MVM to stream\n", __func__);
  1036. attach_stream_cmd.hdr.hdr_field =
  1037. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1038. APR_HDR_LEN(APR_HDR_SIZE),
  1039. APR_PKT_VER);
  1040. attach_stream_cmd.hdr.pkt_size =
  1041. APR_PKT_SIZE(APR_HDR_SIZE,
  1042. sizeof(attach_stream_cmd) -
  1043. APR_HDR_SIZE);
  1044. attach_stream_cmd.hdr.src_port =
  1045. voice_get_idx_for_session(v->session_id);
  1046. attach_stream_cmd.hdr.dest_port = mvm_handle;
  1047. attach_stream_cmd.hdr.token = 0;
  1048. attach_stream_cmd.hdr.opcode =
  1049. VSS_IMVM_CMD_ATTACH_STREAM;
  1050. attach_stream_cmd.attach_stream.handle = cvs_handle;
  1051. v->mvm_state = CMD_STATUS_FAIL;
  1052. v->async_err = 0;
  1053. ret = apr_send_pkt(apr_mvm,
  1054. (uint32_t *) &attach_stream_cmd);
  1055. if (ret < 0) {
  1056. pr_err("%s: Error %d sending ATTACH_STREAM\n",
  1057. __func__, ret);
  1058. goto fail;
  1059. }
  1060. ret = wait_event_timeout(v->mvm_wait,
  1061. (v->mvm_state == CMD_STATUS_SUCCESS),
  1062. msecs_to_jiffies(TIMEOUT_MS));
  1063. if (!ret) {
  1064. pr_err("%s: wait_event timeout\n", __func__);
  1065. goto fail;
  1066. }
  1067. if (v->async_err > 0) {
  1068. pr_err("%s: DSP returned error[%s]\n",
  1069. __func__, adsp_err_get_err_str(
  1070. v->async_err));
  1071. ret = adsp_err_get_lnx_err_code(
  1072. v->async_err);
  1073. goto fail;
  1074. }
  1075. }
  1076. }
  1077. return 0;
  1078. fail:
  1079. return ret;
  1080. }
  1081. static int voice_unmap_cal_block(struct voice_data *v, int cal_index)
  1082. {
  1083. int result = 0;
  1084. struct cal_block_data *cal_block;
  1085. int dest_perms[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
  1086. int source_vm[2] = {VMID_LPASS, VMID_ADSP_HEAP};
  1087. int dest_vm[1] = {VMID_HLOS};
  1088. if (common.cal_data[cal_index] == NULL) {
  1089. pr_err("%s: Cal type is NULL, index %d!\n",
  1090. __func__, cal_index);
  1091. goto done;
  1092. }
  1093. mutex_lock(&common.cal_data[cal_index]->lock);
  1094. cal_block = cal_utils_get_only_cal_block(
  1095. common.cal_data[cal_index]);
  1096. if (cal_block == NULL) {
  1097. pr_err("%s: Cal block is NULL, index %d!\n",
  1098. __func__, cal_index);
  1099. result = -EINVAL;
  1100. goto unlock;
  1101. }
  1102. if (cal_block->map_data.q6map_handle == 0) {
  1103. pr_debug("%s: Q6 handle is not set!\n", __func__);
  1104. result = -EINVAL;
  1105. goto unlock;
  1106. }
  1107. result = voice_send_mvm_unmap_memory_physical_cmd(
  1108. v, cal_block->map_data.q6map_handle);
  1109. if (result)
  1110. pr_err("%s: Voice_send_mvm_unmap_memory_physical_cmd failed for session 0x%x, err %d!\n",
  1111. __func__, v->session_id, result);
  1112. pr_debug("%s: use hyp assigned %d\n",__func__, hyp_assigned);
  1113. if (cal_block->cma_mem && hyp_assigned) {
  1114. if (cal_block->cal_data.paddr == 0 ||
  1115. cal_block->map_data.map_size <= 0) {
  1116. pr_err("%s: No address to map!\n", __func__);
  1117. result = -EINVAL;
  1118. goto unlock;
  1119. }
  1120. result = hyp_assign_phys(cal_block->cal_data.paddr,
  1121. cal_block->map_data.map_size,
  1122. source_vm, 2, dest_vm, dest_perms, 1);
  1123. if (result < 0) {
  1124. pr_err("%s: hyp_assign_phys failed result = %d addr = 0x%pK size = %d\n",
  1125. __func__, result, cal_block->cal_data.paddr,
  1126. cal_block->map_data.map_size);
  1127. result = -EINVAL;
  1128. goto unlock;
  1129. }
  1130. hyp_assigned = false;
  1131. pr_debug("%s: hyp_assign_phys success\n", __func__);
  1132. }
  1133. cal_block->map_data.q6map_handle = 0;
  1134. unlock:
  1135. mutex_unlock(&common.cal_data[cal_index]->lock);
  1136. done:
  1137. return result;
  1138. }
  1139. static int voice_destroy_mvm_cvs_session(struct voice_data *v)
  1140. {
  1141. int ret = 0, result = 0;
  1142. struct mvm_detach_stream_cmd detach_stream;
  1143. struct apr_hdr mvm_destroy;
  1144. struct apr_hdr cvs_destroy;
  1145. void *apr_mvm, *apr_cvs;
  1146. u16 mvm_handle, cvs_handle;
  1147. struct cal_block_data *cal_block;
  1148. int dest_perms[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
  1149. int source_vm[2] = {VMID_LPASS, VMID_ADSP_HEAP};
  1150. int dest_vm[1] = {VMID_HLOS};
  1151. if (v == NULL) {
  1152. pr_err("%s: v is NULL\n", __func__);
  1153. return -EINVAL;
  1154. }
  1155. apr_mvm = common.apr_q6_mvm;
  1156. apr_cvs = common.apr_q6_cvs;
  1157. if (!apr_mvm || !apr_cvs) {
  1158. pr_err("%s: apr_mvm or apr_cvs is NULL\n", __func__);
  1159. ret = -EINVAL;
  1160. pr_debug("%s: use hyp assigned %d\n",__func__, hyp_assigned);
  1161. if (hyp_assigned) {
  1162. mutex_lock(&common.cal_data[CVP_VOCPROC_CAL]->lock);
  1163. cal_block = cal_utils_get_only_cal_block(
  1164. common.cal_data[CVP_VOCPROC_CAL]);
  1165. if (cal_block == NULL) {
  1166. pr_err("%s: Cal block NULL, CVP_VOCPROC_CAL!\n",
  1167. __func__);
  1168. mutex_unlock(&common.cal_data[CVP_VOCPROC_CAL]->lock);
  1169. goto fail;
  1170. }
  1171. if (cal_block->cma_mem) {
  1172. if (cal_block->cal_data.paddr == 0 ||
  1173. cal_block->map_data.map_size <= 0) {
  1174. pr_err("%s: No address to map!\n", __func__);
  1175. mutex_unlock(&common.cal_data[CVP_VOCPROC_CAL]->lock);
  1176. goto fail;
  1177. }
  1178. result = hyp_assign_phys(
  1179. cal_block->cal_data.paddr,
  1180. cal_block->map_data.map_size,
  1181. source_vm, 2, dest_vm, dest_perms, 1);
  1182. if (result < 0) {
  1183. pr_err("%s: hyp_assign_phys failed result = %d addr = 0x%pK size = %d\n",
  1184. __func__, result,
  1185. cal_block->cal_data.paddr,
  1186. cal_block->map_data.map_size);
  1187. mutex_unlock(&common.cal_data[CVP_VOCPROC_CAL]->lock);
  1188. goto fail;
  1189. }
  1190. hyp_assigned = false;
  1191. pr_debug("%s: hyp_assign_phys success\n", __func__);
  1192. mutex_unlock(&common.cal_data[CVP_VOCPROC_CAL]->lock);
  1193. }
  1194. }
  1195. goto fail;
  1196. }
  1197. mvm_handle = voice_get_mvm_handle(v);
  1198. cvs_handle = voice_get_cvs_handle(v);
  1199. /* MVM, CVS sessions are destroyed only for Full control sessions. */
  1200. if (is_voip_session(v->session_id)) {
  1201. pr_debug("%s: MVM detach stream, VOC_STATE: %d\n", __func__,
  1202. v->voc_state);
  1203. /* Detach voice stream. */
  1204. detach_stream.hdr.hdr_field =
  1205. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1206. APR_HDR_LEN(APR_HDR_SIZE),
  1207. APR_PKT_VER);
  1208. detach_stream.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  1209. sizeof(detach_stream) - APR_HDR_SIZE);
  1210. detach_stream.hdr.src_port =
  1211. voice_get_idx_for_session(v->session_id);
  1212. detach_stream.hdr.dest_port = mvm_handle;
  1213. detach_stream.hdr.token = 0;
  1214. detach_stream.hdr.opcode = VSS_IMVM_CMD_DETACH_STREAM;
  1215. detach_stream.detach_stream.handle = cvs_handle;
  1216. v->mvm_state = CMD_STATUS_FAIL;
  1217. v->async_err = 0;
  1218. ret = apr_send_pkt(apr_mvm, (uint32_t *) &detach_stream);
  1219. if (ret < 0) {
  1220. pr_err("%s: Error %d sending DETACH_STREAM\n",
  1221. __func__, ret);
  1222. goto fail;
  1223. }
  1224. ret = wait_event_timeout(v->mvm_wait,
  1225. (v->mvm_state == CMD_STATUS_SUCCESS),
  1226. msecs_to_jiffies(TIMEOUT_MS));
  1227. if (!ret) {
  1228. pr_err("%s: wait event timeout\n", __func__);
  1229. goto fail;
  1230. }
  1231. if (v->async_err > 0) {
  1232. pr_err("%s: DSP returned error[%s]\n",
  1233. __func__, adsp_err_get_err_str(
  1234. v->async_err));
  1235. ret = adsp_err_get_lnx_err_code(
  1236. v->async_err);
  1237. goto fail;
  1238. }
  1239. /* Unmap memory */
  1240. if (v->shmem_info.mem_handle != 0) {
  1241. ret = voice_send_mvm_unmap_memory_physical_cmd(v,
  1242. v->shmem_info.mem_handle);
  1243. if (ret < 0) {
  1244. pr_err("%s Memory_unmap for voip failed %d\n",
  1245. __func__, ret);
  1246. goto fail;
  1247. }
  1248. v->shmem_info.mem_handle = 0;
  1249. }
  1250. }
  1251. /* Unmap Source Tracking shared memory if mapped earlier */
  1252. voice_unmap_and_free_source_tracking_shared_memory(v);
  1253. if (is_voip_session(v->session_id) ||
  1254. is_qchat_session(v->session_id) ||
  1255. is_volte_session(v->session_id) ||
  1256. is_vowlan_session(v->session_id) ||
  1257. v->voc_state == VOC_ERROR || common.is_destroy_cvd) {
  1258. /* Destroy CVS. */
  1259. pr_debug("%s: CVS destroy session\n", __func__);
  1260. cvs_destroy.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1261. APR_HDR_LEN(APR_HDR_SIZE),
  1262. APR_PKT_VER);
  1263. cvs_destroy.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  1264. sizeof(cvs_destroy) - APR_HDR_SIZE);
  1265. cvs_destroy.src_port =
  1266. voice_get_idx_for_session(v->session_id);
  1267. cvs_destroy.dest_port = cvs_handle;
  1268. cvs_destroy.token = 0;
  1269. cvs_destroy.opcode = APRV2_IBASIC_CMD_DESTROY_SESSION;
  1270. v->cvs_state = CMD_STATUS_FAIL;
  1271. v->async_err = 0;
  1272. ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_destroy);
  1273. if (ret < 0) {
  1274. pr_err("%s: Error %d sending CVS DESTROY\n",
  1275. __func__, ret);
  1276. goto fail;
  1277. }
  1278. ret = wait_event_timeout(v->cvs_wait,
  1279. (v->cvs_state == CMD_STATUS_SUCCESS),
  1280. msecs_to_jiffies(TIMEOUT_MS));
  1281. if (!ret) {
  1282. pr_err("%s: wait event timeout\n", __func__);
  1283. goto fail;
  1284. }
  1285. if (v->async_err > 0) {
  1286. pr_err("%s: DSP returned error[%s]\n",
  1287. __func__, adsp_err_get_err_str(
  1288. v->async_err));
  1289. ret = adsp_err_get_lnx_err_code(
  1290. v->async_err);
  1291. goto fail;
  1292. }
  1293. cvs_handle = 0;
  1294. voice_set_cvs_handle(v, cvs_handle);
  1295. /* Unmap physical memory for all calibration buffers */
  1296. if (!is_other_session_active(v->session_id)) {
  1297. if (voice_unmap_cal_block(v, CVP_VOCPROC_CAL))
  1298. pr_err("%s: Unmap VOCPROC cal failed\n",
  1299. __func__);
  1300. if (voice_unmap_cal_block(v, CVP_VOCVOL_CAL))
  1301. pr_err("%s: Unmap VOCVOL cal failed\n",
  1302. __func__);
  1303. if (voice_unmap_cal_block(v, CVP_VOCDEV_CFG_CAL))
  1304. pr_err("%s: Unmap VOCDEV_CFG cal failed\n",
  1305. __func__);
  1306. if (voice_unmap_cal_block(v, CVS_VOCSTRM_CAL))
  1307. pr_err("%s: Unmap VOCSTRM cal failed\n",
  1308. __func__);
  1309. }
  1310. /* Destroy MVM. */
  1311. pr_debug("%s: MVM destroy session\n", __func__);
  1312. mvm_destroy.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1313. APR_HDR_LEN(APR_HDR_SIZE),
  1314. APR_PKT_VER);
  1315. mvm_destroy.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  1316. sizeof(mvm_destroy) - APR_HDR_SIZE);
  1317. mvm_destroy.src_port =
  1318. voice_get_idx_for_session(v->session_id);
  1319. mvm_destroy.dest_port = mvm_handle;
  1320. mvm_destroy.token = 0;
  1321. mvm_destroy.opcode = APRV2_IBASIC_CMD_DESTROY_SESSION;
  1322. v->mvm_state = CMD_STATUS_FAIL;
  1323. v->async_err = 0;
  1324. ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_destroy);
  1325. if (ret < 0) {
  1326. pr_err("%s: Error %d sending MVM DESTROY\n",
  1327. __func__, ret);
  1328. goto fail;
  1329. }
  1330. ret = wait_event_timeout(v->mvm_wait,
  1331. (v->mvm_state == CMD_STATUS_SUCCESS),
  1332. msecs_to_jiffies(TIMEOUT_MS));
  1333. if (!ret) {
  1334. pr_err("%s: wait event timeout\n", __func__);
  1335. goto fail;
  1336. }
  1337. if (v->async_err > 0) {
  1338. pr_err("%s: DSP returned error[%s]\n",
  1339. __func__, adsp_err_get_err_str(
  1340. v->async_err));
  1341. ret = adsp_err_get_lnx_err_code(
  1342. v->async_err);
  1343. goto fail;
  1344. }
  1345. mvm_handle = 0;
  1346. voice_set_mvm_handle(v, mvm_handle);
  1347. }
  1348. return 0;
  1349. fail:
  1350. return ret;
  1351. }
  1352. static int voice_send_tty_mode_cmd(struct voice_data *v)
  1353. {
  1354. int ret = 0;
  1355. struct mvm_set_tty_mode_cmd mvm_tty_mode_cmd;
  1356. void *apr_mvm;
  1357. u16 mvm_handle;
  1358. if (v == NULL) {
  1359. pr_err("%s: v is NULL\n", __func__);
  1360. return -EINVAL;
  1361. }
  1362. apr_mvm = common.apr_q6_mvm;
  1363. if (!apr_mvm) {
  1364. pr_err("%s: apr_mvm is NULL.\n", __func__);
  1365. return -EINVAL;
  1366. }
  1367. mvm_handle = voice_get_mvm_handle(v);
  1368. /* send tty mode cmd to mvm */
  1369. mvm_tty_mode_cmd.hdr.hdr_field = APR_HDR_FIELD(
  1370. APR_MSG_TYPE_SEQ_CMD,
  1371. APR_HDR_LEN(APR_HDR_SIZE),
  1372. APR_PKT_VER);
  1373. mvm_tty_mode_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  1374. sizeof(mvm_tty_mode_cmd) -
  1375. APR_HDR_SIZE);
  1376. pr_debug("%s: pkt size = %d\n",
  1377. __func__, mvm_tty_mode_cmd.hdr.pkt_size);
  1378. mvm_tty_mode_cmd.hdr.src_port =
  1379. voice_get_idx_for_session(v->session_id);
  1380. mvm_tty_mode_cmd.hdr.dest_port = mvm_handle;
  1381. mvm_tty_mode_cmd.hdr.token = 0;
  1382. mvm_tty_mode_cmd.hdr.opcode = VSS_ISTREAM_CMD_SET_TTY_MODE;
  1383. mvm_tty_mode_cmd.tty_mode.mode = v->tty_mode;
  1384. pr_debug("tty mode =%d\n", mvm_tty_mode_cmd.tty_mode.mode);
  1385. v->mvm_state = CMD_STATUS_FAIL;
  1386. v->async_err = 0;
  1387. ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_tty_mode_cmd);
  1388. if (ret < 0) {
  1389. pr_err("%s: Error %d sending SET_TTY_MODE\n",
  1390. __func__, ret);
  1391. goto fail;
  1392. }
  1393. ret = wait_event_timeout(v->mvm_wait,
  1394. (v->mvm_state == CMD_STATUS_SUCCESS),
  1395. msecs_to_jiffies(TIMEOUT_MS));
  1396. if (!ret) {
  1397. pr_err("%s: wait_event timeout\n", __func__);
  1398. goto fail;
  1399. }
  1400. if (v->async_err > 0) {
  1401. pr_err("%s: DSP returned error[%s]\n",
  1402. __func__, adsp_err_get_err_str(
  1403. v->async_err));
  1404. ret = adsp_err_get_lnx_err_code(
  1405. v->async_err);
  1406. goto fail;
  1407. }
  1408. return 0;
  1409. fail:
  1410. return ret;
  1411. }
  1412. static int voice_send_cvp_ecns_enable_cmd(struct voice_data *v,
  1413. uint32_t module_id, int enable)
  1414. {
  1415. int ret;
  1416. struct cvp_set_channel_ecns_cmd_v2 cvp_set_ch_ecns_cmd;
  1417. void *apr_cvp;
  1418. u16 cvp_handle;
  1419. struct vss_icommon_param_data_ecns_t *cvp_config_param_data =
  1420. &cvp_set_ch_ecns_cmd.
  1421. cvp_set_ecns.param_data;
  1422. if (v == NULL) {
  1423. pr_err("%s: v is NULL\n", __func__);
  1424. ret = -EINVAL;
  1425. goto done;
  1426. }
  1427. apr_cvp = common.apr_q6_cvp;
  1428. if (!apr_cvp) {
  1429. pr_err("%s: apr_cvp is NULL\n", __func__);
  1430. ret = -EINVAL;
  1431. goto done;
  1432. }
  1433. cvp_handle = voice_get_cvp_handle(v);
  1434. memset(&cvp_set_ch_ecns_cmd, 0,
  1435. sizeof(cvp_set_ch_ecns_cmd));
  1436. cvp_set_ch_ecns_cmd.hdr.hdr_field =
  1437. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1438. APR_HDR_LEN(APR_HDR_SIZE),
  1439. APR_PKT_VER);
  1440. cvp_set_ch_ecns_cmd.hdr.pkt_size =
  1441. APR_PKT_SIZE(APR_HDR_SIZE,
  1442. sizeof(cvp_set_ch_ecns_cmd) - APR_HDR_SIZE);
  1443. cvp_set_ch_ecns_cmd.hdr.src_svc = 0;
  1444. cvp_set_ch_ecns_cmd.hdr.src_domain = APR_DOMAIN_APPS;
  1445. cvp_set_ch_ecns_cmd.hdr.src_port =
  1446. voice_get_idx_for_session(v->session_id);
  1447. cvp_set_ch_ecns_cmd.hdr.dest_svc = 0;
  1448. cvp_set_ch_ecns_cmd.hdr.dest_domain = APR_DOMAIN_ADSP;
  1449. cvp_set_ch_ecns_cmd.hdr.dest_port = cvp_handle;
  1450. cvp_set_ch_ecns_cmd.hdr.token = 0;
  1451. cvp_set_ch_ecns_cmd.hdr.opcode = VSS_ICOMMON_CMD_SET_PARAM_V2;
  1452. cvp_set_ch_ecns_cmd.cvp_set_ecns.mem_size =
  1453. sizeof(struct vss_icommon_param_data_ecns_t);
  1454. cvp_config_param_data->module_id = module_id;
  1455. cvp_config_param_data->param_id = VOICE_PARAM_MOD_ENABLE;
  1456. cvp_config_param_data->param_size = MOD_ENABLE_PARAM_LEN;
  1457. cvp_config_param_data->reserved = 0;
  1458. cvp_config_param_data->enable = enable;
  1459. v->cvp_state = CMD_STATUS_FAIL;
  1460. v->async_err = 0;
  1461. ret = apr_send_pkt(apr_cvp, (uint32_t *)&cvp_set_ch_ecns_cmd);
  1462. if (ret < 0) {
  1463. pr_err("%s: Failed to send VSS_ICOMMON_CMD_SET_PARAM_V2 %d\n",
  1464. __func__, ret);
  1465. goto done;
  1466. }
  1467. ret = wait_event_timeout(v->cvp_wait,
  1468. (v->cvp_state == CMD_STATUS_SUCCESS),
  1469. msecs_to_jiffies(TIMEOUT_MS));
  1470. if (!ret) {
  1471. pr_err("%s: wait_event timeout\n", __func__);
  1472. ret = -ETIMEDOUT;
  1473. goto done;
  1474. }
  1475. if (v->async_err > 0) {
  1476. pr_err("%s: DSP returned error[%s] handle = %d\n", __func__,
  1477. adsp_err_get_err_str(v->async_err), cvp_handle);
  1478. ret = adsp_err_get_lnx_err_code(v->async_err);
  1479. goto done;
  1480. }
  1481. ret = 0;
  1482. done:
  1483. return ret;
  1484. }
  1485. /**
  1486. * voc_set_ecns_enable -
  1487. * Command to set ECNS for voice module
  1488. *
  1489. * @session_id: voice session ID to send this command
  1490. * @module_id: voice module id
  1491. * @enable: enable/disable flag
  1492. *
  1493. * Returns 0 on success or error on failure
  1494. */
  1495. int voc_set_ecns_enable(uint32_t session_id, uint32_t module_id,
  1496. uint32_t enable)
  1497. {
  1498. struct voice_data *v = voice_get_session(session_id);
  1499. int ret = 0;
  1500. if (v == NULL) {
  1501. pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
  1502. return -EINVAL;
  1503. }
  1504. mutex_lock(&v->lock);
  1505. v->ecns_enable = enable;
  1506. v->ecns_module_id = module_id;
  1507. if (is_voc_state_active(v->voc_state))
  1508. ret = voice_send_cvp_ecns_enable_cmd(v,
  1509. v->ecns_module_id, v->ecns_enable);
  1510. mutex_unlock(&v->lock);
  1511. return ret;
  1512. }
  1513. EXPORT_SYMBOL(voc_set_ecns_enable);
  1514. static int voice_send_set_pp_enable_cmd(
  1515. struct voice_data *v, struct module_instance_info mod_inst_info,
  1516. int enable)
  1517. {
  1518. struct enable_param enable_param;
  1519. struct param_hdr_v3 param_hdr;
  1520. int ret = 0;
  1521. memset(&enable_param, 0, sizeof(enable_param));
  1522. memset(&param_hdr, 0, sizeof(param_hdr));
  1523. param_hdr.module_id = mod_inst_info.module_id;
  1524. param_hdr.instance_id = mod_inst_info.instance_id;
  1525. param_hdr.param_id = VOICE_PARAM_MOD_ENABLE;
  1526. param_hdr.param_size = sizeof(enable_param);
  1527. enable_param.enable = enable ? 1 : 0;
  1528. pr_debug("%s: module_id=%d, instance_id=%d, enable=%d\n",
  1529. __func__, mod_inst_info.module_id, mod_inst_info.instance_id,
  1530. enable);
  1531. ret = voice_pack_and_set_cvs_ui_property(v, param_hdr,
  1532. (uint8_t *) &enable_param);
  1533. if (ret < 0)
  1534. pr_err("Fail: sending cvs set pp enable\n");
  1535. return ret;
  1536. }
  1537. static int voice_send_hd_cmd(struct voice_data *v, int enable)
  1538. {
  1539. struct mvm_set_hd_enable_cmd mvm_set_hd_cmd;
  1540. int ret = 0;
  1541. void *apr_mvm;
  1542. u16 mvm_handle;
  1543. if (v == NULL) {
  1544. pr_err("%s: v is NULL\n", __func__);
  1545. ret = -EINVAL;
  1546. goto done;
  1547. }
  1548. apr_mvm = common.apr_q6_mvm;
  1549. if (!apr_mvm) {
  1550. pr_err("%s: apr_mvm is NULL.\n", __func__);
  1551. ret = -EINVAL;
  1552. goto done;
  1553. }
  1554. mvm_handle = voice_get_mvm_handle(v);
  1555. if (!mvm_handle) {
  1556. pr_err("%s: mvm_handle is NULL\n", __func__);
  1557. ret = -EINVAL;
  1558. goto done;
  1559. }
  1560. mvm_set_hd_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1561. APR_HDR_LEN(APR_HDR_SIZE),
  1562. APR_PKT_VER);
  1563. mvm_set_hd_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  1564. sizeof(mvm_set_hd_cmd) -
  1565. APR_HDR_SIZE);
  1566. mvm_set_hd_cmd.hdr.src_port = voice_get_idx_for_session(v->session_id);
  1567. mvm_set_hd_cmd.hdr.dest_port = mvm_handle;
  1568. mvm_set_hd_cmd.hdr.token = 0;
  1569. if (enable)
  1570. mvm_set_hd_cmd.hdr.opcode = VSS_IHDVOICE_CMD_ENABLE;
  1571. else
  1572. mvm_set_hd_cmd.hdr.opcode = VSS_IHDVOICE_CMD_DISABLE;
  1573. pr_debug("%s: enable=%d\n", __func__, enable);
  1574. v->mvm_state = CMD_STATUS_FAIL;
  1575. v->async_err = 0;
  1576. ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_set_hd_cmd);
  1577. if (ret < 0) {
  1578. pr_err("%s: Failed to sending mvm set HD Voice enable %d\n",
  1579. __func__, ret);
  1580. ret = -EINVAL;
  1581. goto done;
  1582. }
  1583. ret = wait_event_timeout(v->mvm_wait,
  1584. (v->mvm_state == CMD_STATUS_SUCCESS),
  1585. msecs_to_jiffies(TIMEOUT_MS));
  1586. if (!ret) {
  1587. pr_err("%s: wait_event timeout\n", __func__);
  1588. ret = -EINVAL;
  1589. goto done;
  1590. }
  1591. if (v->async_err > 0) {
  1592. pr_err("%s: DSP returned error[%s]\n",
  1593. __func__, adsp_err_get_err_str(
  1594. v->async_err));
  1595. ret = adsp_err_get_lnx_err_code(
  1596. v->async_err);
  1597. goto done;
  1598. }
  1599. done:
  1600. return ret;
  1601. }
  1602. static int voice_set_dtx(struct voice_data *v)
  1603. {
  1604. int ret = 0;
  1605. void *apr_cvs;
  1606. u16 cvs_handle;
  1607. struct cvs_set_enc_dtx_mode_cmd cvs_set_dtx;
  1608. if (v == NULL) {
  1609. pr_err("%s: v is NULL\n", __func__);
  1610. return -EINVAL;
  1611. }
  1612. apr_cvs = common.apr_q6_cvs;
  1613. if (!apr_cvs) {
  1614. pr_err("%s: apr_cvs is NULL.\n", __func__);
  1615. return -EINVAL;
  1616. }
  1617. cvs_handle = voice_get_cvs_handle(v);
  1618. /* Set DTX */
  1619. cvs_set_dtx.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1620. APR_HDR_LEN(APR_HDR_SIZE),
  1621. APR_PKT_VER);
  1622. cvs_set_dtx.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  1623. sizeof(cvs_set_dtx) - APR_HDR_SIZE);
  1624. cvs_set_dtx.hdr.src_port =
  1625. voice_get_idx_for_session(v->session_id);
  1626. cvs_set_dtx.hdr.dest_port = cvs_handle;
  1627. cvs_set_dtx.hdr.token = 0;
  1628. cvs_set_dtx.hdr.opcode = VSS_ISTREAM_CMD_SET_ENC_DTX_MODE;
  1629. cvs_set_dtx.dtx_mode.enable = common.mvs_info.dtx_mode;
  1630. pr_debug("%s: Setting DTX %d\n", __func__, common.mvs_info.dtx_mode);
  1631. v->cvs_state = CMD_STATUS_FAIL;
  1632. v->async_err = 0;
  1633. ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_set_dtx);
  1634. if (ret < 0) {
  1635. pr_err("%s: Error %d sending SET_DTX\n", __func__, ret);
  1636. return -EINVAL;
  1637. }
  1638. ret = wait_event_timeout(v->cvs_wait,
  1639. (v->cvs_state == CMD_STATUS_SUCCESS),
  1640. msecs_to_jiffies(TIMEOUT_MS));
  1641. if (!ret) {
  1642. pr_err("%s: wait_event timeout\n", __func__);
  1643. return -EINVAL;
  1644. }
  1645. if (v->async_err > 0) {
  1646. pr_err("%s: DSP returned error[%s]\n",
  1647. __func__, adsp_err_get_err_str(
  1648. v->async_err));
  1649. ret = adsp_err_get_lnx_err_code(
  1650. v->async_err);
  1651. return ret;
  1652. }
  1653. return 0;
  1654. }
  1655. static int voice_send_mvm_media_type_cmd(struct voice_data *v)
  1656. {
  1657. struct vss_imvm_cmd_set_cal_media_type_t mvm_set_cal_media_type;
  1658. int ret = 0;
  1659. void *apr_mvm;
  1660. u16 mvm_handle;
  1661. if (v == NULL) {
  1662. pr_err("%s: v is NULL\n", __func__);
  1663. return -EINVAL;
  1664. }
  1665. apr_mvm = common.apr_q6_mvm;
  1666. if (!apr_mvm) {
  1667. pr_err("%s: apr_mvm is NULL.\n", __func__);
  1668. return -EINVAL;
  1669. }
  1670. mvm_handle = voice_get_mvm_handle(v);
  1671. mvm_set_cal_media_type.hdr.hdr_field =
  1672. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1673. APR_HDR_LEN(APR_HDR_SIZE),
  1674. APR_PKT_VER);
  1675. mvm_set_cal_media_type.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  1676. sizeof(mvm_set_cal_media_type) -
  1677. APR_HDR_SIZE);
  1678. mvm_set_cal_media_type.hdr.src_port =
  1679. voice_get_idx_for_session(v->session_id);
  1680. mvm_set_cal_media_type.hdr.dest_port = mvm_handle;
  1681. mvm_set_cal_media_type.hdr.token = 0;
  1682. mvm_set_cal_media_type.hdr.opcode = VSS_IMVM_CMD_SET_CAL_MEDIA_TYPE;
  1683. mvm_set_cal_media_type.media_id = common.mvs_info.media_type;
  1684. pr_debug("%s: setting media_id as %x\n",
  1685. __func__, mvm_set_cal_media_type.media_id);
  1686. v->mvm_state = CMD_STATUS_FAIL;
  1687. v->async_err = 0;
  1688. ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_set_cal_media_type);
  1689. if (ret < 0) {
  1690. pr_err("%s: Error %d sending media type\n", __func__, ret);
  1691. goto fail;
  1692. }
  1693. ret = wait_event_timeout(v->mvm_wait,
  1694. (v->mvm_state == CMD_STATUS_SUCCESS),
  1695. msecs_to_jiffies(TIMEOUT_MS));
  1696. if (!ret) {
  1697. pr_err("%s: wait_event timeout %d\n", __func__, ret);
  1698. goto fail;
  1699. }
  1700. if (v->async_err > 0) {
  1701. pr_err("%s: DSP returned error[%s]\n",
  1702. __func__, adsp_err_get_err_str(
  1703. v->async_err));
  1704. ret = adsp_err_get_lnx_err_code(
  1705. v->async_err);
  1706. goto fail;
  1707. }
  1708. return 0;
  1709. fail:
  1710. return ret;
  1711. }
  1712. static int voice_send_dtmf_rx_detection_cmd(struct voice_data *v,
  1713. uint32_t enable)
  1714. {
  1715. int ret = 0;
  1716. void *apr_cvs;
  1717. u16 cvs_handle;
  1718. struct cvs_set_rx_dtmf_detection_cmd cvs_dtmf_rx_detection;
  1719. if (v == NULL) {
  1720. pr_err("%s: v is NULL\n", __func__);
  1721. return -EINVAL;
  1722. }
  1723. apr_cvs = common.apr_q6_cvs;
  1724. if (!apr_cvs) {
  1725. pr_err("%s: apr_cvs is NULL.\n", __func__);
  1726. return -EINVAL;
  1727. }
  1728. cvs_handle = voice_get_cvs_handle(v);
  1729. /* Set SET_DTMF_RX_DETECTION */
  1730. cvs_dtmf_rx_detection.hdr.hdr_field =
  1731. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1732. APR_HDR_LEN(APR_HDR_SIZE),
  1733. APR_PKT_VER);
  1734. cvs_dtmf_rx_detection.hdr.pkt_size =
  1735. APR_PKT_SIZE(APR_HDR_SIZE,
  1736. sizeof(cvs_dtmf_rx_detection) - APR_HDR_SIZE);
  1737. cvs_dtmf_rx_detection.hdr.src_port =
  1738. voice_get_idx_for_session(v->session_id);
  1739. cvs_dtmf_rx_detection.hdr.dest_port = cvs_handle;
  1740. cvs_dtmf_rx_detection.hdr.token = 0;
  1741. cvs_dtmf_rx_detection.hdr.opcode =
  1742. VSS_ISTREAM_CMD_SET_RX_DTMF_DETECTION;
  1743. cvs_dtmf_rx_detection.cvs_dtmf_det.enable = enable;
  1744. v->cvs_state = CMD_STATUS_FAIL;
  1745. v->async_err = 0;
  1746. ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_dtmf_rx_detection);
  1747. if (ret < 0) {
  1748. pr_err("%s: Error %d sending SET_DTMF_RX_DETECTION\n",
  1749. __func__,
  1750. ret);
  1751. return -EINVAL;
  1752. }
  1753. ret = wait_event_timeout(v->cvs_wait,
  1754. (v->cvs_state == CMD_STATUS_SUCCESS),
  1755. msecs_to_jiffies(TIMEOUT_MS));
  1756. if (!ret) {
  1757. pr_err("%s: wait_event timeout\n", __func__);
  1758. return -EINVAL;
  1759. }
  1760. if (v->async_err > 0) {
  1761. pr_err("%s: DSP returned error[%s]\n",
  1762. __func__, adsp_err_get_err_str(
  1763. v->async_err));
  1764. ret = adsp_err_get_lnx_err_code(
  1765. v->async_err);
  1766. return ret;
  1767. }
  1768. return ret;
  1769. }
  1770. /**
  1771. * voc_disable_dtmf_det_on_active_sessions -
  1772. * command to disable DTMF detection for voice sessions
  1773. *
  1774. */
  1775. void voc_disable_dtmf_det_on_active_sessions(void)
  1776. {
  1777. struct voice_data *v = NULL;
  1778. int i;
  1779. for (i = 0; i < MAX_VOC_SESSIONS; i++) {
  1780. v = &common.voice[i];
  1781. if ((v->dtmf_rx_detect_en) &&
  1782. is_voc_state_active(v->voc_state)) {
  1783. pr_debug("disable dtmf det on ses_id=%d\n",
  1784. v->session_id);
  1785. voice_send_dtmf_rx_detection_cmd(v, 0);
  1786. }
  1787. }
  1788. }
  1789. EXPORT_SYMBOL(voc_disable_dtmf_det_on_active_sessions);
  1790. /**
  1791. * voc_enable_dtmf_rx_detection -
  1792. * command to set DTMF RX detection
  1793. *
  1794. * @session_id: voice session ID to send this command
  1795. * @enable: Enable or Disable detection
  1796. *
  1797. * Returns 0 on success or error on failure
  1798. */
  1799. int voc_enable_dtmf_rx_detection(uint32_t session_id, uint32_t enable)
  1800. {
  1801. struct voice_data *v = voice_get_session(session_id);
  1802. int ret = 0;
  1803. if (v == NULL) {
  1804. pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
  1805. return -EINVAL;
  1806. }
  1807. mutex_lock(&v->lock);
  1808. v->dtmf_rx_detect_en = enable;
  1809. if (is_voc_state_active(v->voc_state))
  1810. ret = voice_send_dtmf_rx_detection_cmd(v,
  1811. v->dtmf_rx_detect_en);
  1812. mutex_unlock(&v->lock);
  1813. return ret;
  1814. }
  1815. EXPORT_SYMBOL(voc_enable_dtmf_rx_detection);
  1816. /**
  1817. * voc_set_destroy_cvd_flag -
  1818. * set flag for destroy CVD session
  1819. *
  1820. * @is_destroy_cvd: bool value used to indicate
  1821. * destroy CVD session or not.
  1822. *
  1823. */
  1824. void voc_set_destroy_cvd_flag(bool is_destroy_cvd)
  1825. {
  1826. pr_debug("%s: %d\n", __func__, is_destroy_cvd);
  1827. common.is_destroy_cvd = is_destroy_cvd;
  1828. }
  1829. EXPORT_SYMBOL(voc_set_destroy_cvd_flag);
  1830. /**
  1831. * voc_alloc_cal_shared_memory -
  1832. * Alloc mem map table for calibration
  1833. *
  1834. * Returns 0 on success or error on failure
  1835. */
  1836. int voc_alloc_cal_shared_memory(void)
  1837. {
  1838. int rc = 0;
  1839. mutex_lock(&common.common_lock);
  1840. if (is_cal_memory_allocated()) {
  1841. pr_debug("%s: Calibration shared buffer already allocated",
  1842. __func__);
  1843. } else {
  1844. /* Allocate memory for calibration memory map table. */
  1845. rc = voice_alloc_cal_mem_map_table();
  1846. if ((rc < 0) && (rc != -EPROBE_DEFER)) {
  1847. pr_err("%s: Failed to allocate cal memory, err=%d",
  1848. __func__, rc);
  1849. }
  1850. }
  1851. mutex_unlock(&common.common_lock);
  1852. return rc;
  1853. }
  1854. EXPORT_SYMBOL(voc_alloc_cal_shared_memory);
  1855. /**
  1856. * voc_alloc_voip_shared_memory -
  1857. * Alloc mem map table for OOB
  1858. *
  1859. * Returns 0 on success or error on failure
  1860. */
  1861. int voc_alloc_voip_shared_memory(void)
  1862. {
  1863. int rc = 0;
  1864. /* Allocate shared memory for OOB Voip */
  1865. rc = voice_alloc_oob_shared_mem();
  1866. if (rc < 0) {
  1867. pr_err("%s: Failed to alloc shared memory for OOB rc:%d\n",
  1868. __func__, rc);
  1869. } else {
  1870. /* Allocate mem map table for OOB */
  1871. rc = voice_alloc_oob_mem_table();
  1872. if (rc < 0) {
  1873. pr_err("%s: Failed to alloc mem map talbe rc:%d\n",
  1874. __func__, rc);
  1875. voice_free_oob_shared_mem();
  1876. }
  1877. }
  1878. return rc;
  1879. }
  1880. EXPORT_SYMBOL(voc_alloc_voip_shared_memory);
  1881. static int is_cal_memory_allocated(void)
  1882. {
  1883. bool ret;
  1884. if (common.cal_mem_map_table.dma_buf != NULL)
  1885. ret = true;
  1886. else
  1887. ret = false;
  1888. return ret;
  1889. }
  1890. static int free_cal_map_table(void)
  1891. {
  1892. int ret = 0;
  1893. if (common.cal_mem_map_table.dma_buf == NULL)
  1894. goto done;
  1895. ret = msm_audio_ion_free(common.cal_mem_map_table.dma_buf);
  1896. if (ret < 0)
  1897. pr_err("%s: msm_audio_ion_free failed:\n", __func__);
  1898. done:
  1899. common.cal_mem_map_table.dma_buf = NULL;
  1900. return ret;
  1901. }
  1902. static int is_rtac_memory_allocated(void)
  1903. {
  1904. bool ret;
  1905. if (common.rtac_mem_map_table.dma_buf != NULL)
  1906. ret = true;
  1907. else
  1908. ret = false;
  1909. return ret;
  1910. }
  1911. static int free_rtac_map_table(void)
  1912. {
  1913. int ret = 0;
  1914. if (common.rtac_mem_map_table.dma_buf == NULL)
  1915. goto done;
  1916. ret = msm_audio_ion_free(common.rtac_mem_map_table.dma_buf);
  1917. if (ret < 0)
  1918. pr_err("%s: msm_audio_ion_free failed:\n", __func__);
  1919. done:
  1920. common.rtac_mem_map_table.dma_buf = NULL;
  1921. return ret;
  1922. }
  1923. static int is_voip_memory_allocated(void)
  1924. {
  1925. bool ret;
  1926. struct voice_data *v = voice_get_session(
  1927. common.voice[VOC_PATH_FULL].session_id);
  1928. if (v == NULL) {
  1929. pr_err("%s: v is NULL, session_id:%d\n", __func__,
  1930. common.voice[VOC_PATH_FULL].session_id);
  1931. ret = false;
  1932. goto done;
  1933. }
  1934. mutex_lock(&common.common_lock);
  1935. if (v->shmem_info.sh_buf.dma_buf != NULL)
  1936. ret = true;
  1937. else
  1938. ret = false;
  1939. mutex_unlock(&common.common_lock);
  1940. done:
  1941. return ret;
  1942. }
  1943. static int voice_config_cvs_vocoder_amr_rate(struct voice_data *v)
  1944. {
  1945. int ret = 0;
  1946. void *apr_cvs;
  1947. u16 cvs_handle;
  1948. struct cvs_set_amr_enc_rate_cmd cvs_set_amr_rate;
  1949. if (v == NULL) {
  1950. pr_err("%s: v is NULL\n", __func__);
  1951. ret = -EINVAL;
  1952. goto done;
  1953. }
  1954. apr_cvs = common.apr_q6_cvs;
  1955. if (!apr_cvs) {
  1956. pr_err("%s: apr_cvs is NULL.\n", __func__);
  1957. ret = -EINVAL;
  1958. goto done;
  1959. }
  1960. cvs_handle = voice_get_cvs_handle(v);
  1961. pr_debug("%s: Setting AMR rate. Media Type: %d\n", __func__,
  1962. common.mvs_info.media_type);
  1963. cvs_set_amr_rate.hdr.hdr_field =
  1964. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1965. APR_HDR_LEN(APR_HDR_SIZE),
  1966. APR_PKT_VER);
  1967. cvs_set_amr_rate.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  1968. sizeof(cvs_set_amr_rate) - APR_HDR_SIZE);
  1969. cvs_set_amr_rate.hdr.src_port =
  1970. voice_get_idx_for_session(v->session_id);
  1971. cvs_set_amr_rate.hdr.dest_port = cvs_handle;
  1972. cvs_set_amr_rate.hdr.token = 0;
  1973. if (common.mvs_info.media_type == VSS_MEDIA_ID_AMR_NB_MODEM)
  1974. cvs_set_amr_rate.hdr.opcode =
  1975. VSS_ISTREAM_CMD_VOC_AMR_SET_ENC_RATE;
  1976. else if (common.mvs_info.media_type == VSS_MEDIA_ID_AMR_WB_MODEM)
  1977. cvs_set_amr_rate.hdr.opcode =
  1978. VSS_ISTREAM_CMD_VOC_AMRWB_SET_ENC_RATE;
  1979. cvs_set_amr_rate.amr_rate.mode = common.mvs_info.rate;
  1980. v->cvs_state = CMD_STATUS_FAIL;
  1981. v->async_err = 0;
  1982. ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_set_amr_rate);
  1983. if (ret < 0) {
  1984. pr_err("%s: Error %d sending SET_AMR_RATE\n",
  1985. __func__, ret);
  1986. goto done;
  1987. }
  1988. ret = wait_event_timeout(v->cvs_wait,
  1989. (v->cvs_state == CMD_STATUS_SUCCESS),
  1990. msecs_to_jiffies(TIMEOUT_MS));
  1991. if (!ret) {
  1992. pr_err("%s: wait_event timeout\n", __func__);
  1993. ret = -EINVAL;
  1994. goto done;
  1995. }
  1996. if (v->async_err > 0) {
  1997. pr_err("%s: DSP returned error[%s]\n",
  1998. __func__, adsp_err_get_err_str(
  1999. v->async_err));
  2000. ret = adsp_err_get_lnx_err_code(
  2001. v->async_err);
  2002. goto done;
  2003. }
  2004. return 0;
  2005. done:
  2006. return ret;
  2007. }
  2008. static int voice_config_cvs_vocoder(struct voice_data *v)
  2009. {
  2010. int ret = 0;
  2011. void *apr_cvs;
  2012. u16 cvs_handle;
  2013. /* Set media type. */
  2014. struct cvs_set_media_type_cmd cvs_set_media_cmd;
  2015. if (v == NULL) {
  2016. pr_err("%s: v is NULL\n", __func__);
  2017. return -EINVAL;
  2018. }
  2019. apr_cvs = common.apr_q6_cvs;
  2020. if (!apr_cvs) {
  2021. pr_err("%s: apr_cvs is NULL.\n", __func__);
  2022. return -EINVAL;
  2023. }
  2024. cvs_handle = voice_get_cvs_handle(v);
  2025. cvs_set_media_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2026. APR_HDR_LEN(APR_HDR_SIZE),
  2027. APR_PKT_VER);
  2028. cvs_set_media_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  2029. sizeof(cvs_set_media_cmd) -
  2030. APR_HDR_SIZE);
  2031. cvs_set_media_cmd.hdr.src_port =
  2032. voice_get_idx_for_session(v->session_id);
  2033. cvs_set_media_cmd.hdr.dest_port = cvs_handle;
  2034. cvs_set_media_cmd.hdr.token = 0;
  2035. cvs_set_media_cmd.hdr.opcode = VSS_ISTREAM_CMD_SET_MEDIA_TYPE;
  2036. cvs_set_media_cmd.media_type.tx_media_id = common.mvs_info.media_type;
  2037. cvs_set_media_cmd.media_type.rx_media_id = common.mvs_info.media_type;
  2038. v->cvs_state = CMD_STATUS_FAIL;
  2039. v->async_err = 0;
  2040. ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_set_media_cmd);
  2041. if (ret < 0) {
  2042. pr_err("%s: Error %d sending SET_MEDIA_TYPE\n",
  2043. __func__, ret);
  2044. goto fail;
  2045. }
  2046. ret = wait_event_timeout(v->cvs_wait,
  2047. (v->cvs_state == CMD_STATUS_SUCCESS),
  2048. msecs_to_jiffies(TIMEOUT_MS));
  2049. if (!ret) {
  2050. pr_err("%s: wait_event timeout\n", __func__);
  2051. goto fail;
  2052. }
  2053. if (v->async_err > 0) {
  2054. pr_err("%s: DSP returned error[%s]\n",
  2055. __func__, adsp_err_get_err_str(
  2056. v->async_err));
  2057. ret = adsp_err_get_lnx_err_code(
  2058. v->async_err);
  2059. goto fail;
  2060. }
  2061. /* Set encoder properties. */
  2062. switch (common.mvs_info.media_type) {
  2063. case VSS_MEDIA_ID_EVRC_MODEM:
  2064. case VSS_MEDIA_ID_4GV_NB_MODEM:
  2065. case VSS_MEDIA_ID_4GV_WB_MODEM:
  2066. case VSS_MEDIA_ID_4GV_NW_MODEM: {
  2067. struct cvs_set_cdma_enc_minmax_rate_cmd cvs_set_cdma_rate;
  2068. pr_debug("Setting EVRC min-max rate\n");
  2069. cvs_set_cdma_rate.hdr.hdr_field =
  2070. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2071. APR_HDR_LEN(APR_HDR_SIZE),
  2072. APR_PKT_VER);
  2073. cvs_set_cdma_rate.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  2074. sizeof(cvs_set_cdma_rate) - APR_HDR_SIZE);
  2075. cvs_set_cdma_rate.hdr.src_port =
  2076. voice_get_idx_for_session(v->session_id);
  2077. cvs_set_cdma_rate.hdr.dest_port = cvs_handle;
  2078. cvs_set_cdma_rate.hdr.token = 0;
  2079. cvs_set_cdma_rate.hdr.opcode =
  2080. VSS_ISTREAM_CMD_CDMA_SET_ENC_MINMAX_RATE;
  2081. cvs_set_cdma_rate.cdma_rate.min_rate =
  2082. common.mvs_info.evrc_min_rate;
  2083. cvs_set_cdma_rate.cdma_rate.max_rate =
  2084. common.mvs_info.evrc_max_rate;
  2085. v->cvs_state = CMD_STATUS_FAIL;
  2086. v->async_err = 0;
  2087. ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_set_cdma_rate);
  2088. if (ret < 0) {
  2089. pr_err("%s: Error %d sending SET_EVRC_MINMAX_RATE\n",
  2090. __func__, ret);
  2091. goto fail;
  2092. }
  2093. ret = wait_event_timeout(v->cvs_wait,
  2094. (v->cvs_state == CMD_STATUS_SUCCESS),
  2095. msecs_to_jiffies(TIMEOUT_MS));
  2096. if (!ret) {
  2097. pr_err("%s: wait_event timeout\n", __func__);
  2098. goto fail;
  2099. }
  2100. if (v->async_err > 0) {
  2101. pr_err("%s: DSP returned error[%s]\n",
  2102. __func__, adsp_err_get_err_str(
  2103. v->async_err));
  2104. ret = adsp_err_get_lnx_err_code(
  2105. v->async_err);
  2106. goto fail;
  2107. }
  2108. if (common.mvs_info.media_type != VSS_MEDIA_ID_EVRC_MODEM) {
  2109. ret = voice_set_dtx(v);
  2110. if (ret < 0)
  2111. goto fail;
  2112. }
  2113. break;
  2114. }
  2115. case VSS_MEDIA_ID_AMR_NB_MODEM:
  2116. case VSS_MEDIA_ID_AMR_WB_MODEM: {
  2117. ret = voice_config_cvs_vocoder_amr_rate(v);
  2118. if (ret) {
  2119. pr_err("%s: Failed to update vocoder rate. %d\n",
  2120. __func__, ret);
  2121. goto fail;
  2122. }
  2123. ret = voice_set_dtx(v);
  2124. if (ret < 0)
  2125. goto fail;
  2126. break;
  2127. }
  2128. case VSS_MEDIA_ID_G729:
  2129. case VSS_MEDIA_ID_G711_ALAW:
  2130. case VSS_MEDIA_ID_G711_MULAW: {
  2131. ret = voice_set_dtx(v);
  2132. break;
  2133. }
  2134. default:
  2135. /* Do nothing. */
  2136. break;
  2137. }
  2138. return 0;
  2139. fail:
  2140. return ret;
  2141. }
  2142. /**
  2143. * voc_update_amr_vocoder_rate -
  2144. * command to update AMR rate for voice session
  2145. *
  2146. * @session_id: voice session ID to send this command
  2147. *
  2148. * Returns 0 on success or error on failure
  2149. */
  2150. int voc_update_amr_vocoder_rate(uint32_t session_id)
  2151. {
  2152. int ret = 0;
  2153. struct voice_data *v;
  2154. pr_debug("%s: session_id:%d", __func__, session_id);
  2155. v = voice_get_session(session_id);
  2156. if (v == NULL) {
  2157. pr_err("%s: v is NULL, session_id:%d\n", __func__,
  2158. session_id);
  2159. ret = -EINVAL;
  2160. goto done;
  2161. }
  2162. mutex_lock(&v->lock);
  2163. ret = voice_config_cvs_vocoder_amr_rate(v);
  2164. mutex_unlock(&v->lock);
  2165. done:
  2166. return ret;
  2167. }
  2168. EXPORT_SYMBOL(voc_update_amr_vocoder_rate);
  2169. static int voice_send_start_voice_cmd(struct voice_data *v)
  2170. {
  2171. struct apr_hdr mvm_start_voice_cmd;
  2172. int ret = 0;
  2173. void *apr_mvm;
  2174. u16 mvm_handle;
  2175. if (v == NULL) {
  2176. pr_err("%s: v is NULL\n", __func__);
  2177. return -EINVAL;
  2178. }
  2179. apr_mvm = common.apr_q6_mvm;
  2180. if (!apr_mvm) {
  2181. pr_err("%s: apr_mvm is NULL.\n", __func__);
  2182. return -EINVAL;
  2183. }
  2184. mvm_handle = voice_get_mvm_handle(v);
  2185. mvm_start_voice_cmd.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2186. APR_HDR_LEN(APR_HDR_SIZE),
  2187. APR_PKT_VER);
  2188. mvm_start_voice_cmd.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  2189. sizeof(mvm_start_voice_cmd) - APR_HDR_SIZE);
  2190. pr_debug("send mvm_start_voice_cmd pkt size = %d\n",
  2191. mvm_start_voice_cmd.pkt_size);
  2192. mvm_start_voice_cmd.src_port =
  2193. voice_get_idx_for_session(v->session_id);
  2194. mvm_start_voice_cmd.dest_port = mvm_handle;
  2195. mvm_start_voice_cmd.token = 0;
  2196. mvm_start_voice_cmd.opcode = VSS_IMVM_CMD_START_VOICE;
  2197. v->mvm_state = CMD_STATUS_FAIL;
  2198. v->async_err = 0;
  2199. ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_start_voice_cmd);
  2200. if (ret < 0) {
  2201. pr_err("Fail in sending VSS_IMVM_CMD_START_VOICE\n");
  2202. goto fail;
  2203. }
  2204. ret = wait_event_timeout(v->mvm_wait,
  2205. (v->mvm_state == CMD_STATUS_SUCCESS),
  2206. msecs_to_jiffies(TIMEOUT_MS));
  2207. if (!ret) {
  2208. pr_err("%s: wait_event timeout\n", __func__);
  2209. goto fail;
  2210. }
  2211. if (v->async_err > 0) {
  2212. pr_err("%s: DSP returned error[%s]\n",
  2213. __func__, adsp_err_get_err_str(
  2214. v->async_err));
  2215. ret = adsp_err_get_lnx_err_code(
  2216. v->async_err);
  2217. goto fail;
  2218. }
  2219. return 0;
  2220. fail:
  2221. return ret;
  2222. }
  2223. static void voc_get_tx_rx_topology(struct voice_data *v,
  2224. uint32_t *tx_topology_id,
  2225. uint32_t *rx_topology_id)
  2226. {
  2227. uint32_t tx_id = 0;
  2228. uint32_t rx_id = 0;
  2229. if (v->lch_mode == VOICE_LCH_START || v->disable_topology) {
  2230. pr_debug("%s: Setting TX and RX topology to NONE for LCH\n",
  2231. __func__);
  2232. tx_id = VSS_IVOCPROC_TOPOLOGY_ID_NONE;
  2233. rx_id = VSS_IVOCPROC_TOPOLOGY_ID_NONE;
  2234. } else {
  2235. tx_id = voice_get_topology(CVP_VOC_TX_TOPOLOGY_CAL);
  2236. rx_id = voice_get_topology(CVP_VOC_RX_TOPOLOGY_CAL);
  2237. }
  2238. *tx_topology_id = tx_id;
  2239. *rx_topology_id = rx_id;
  2240. }
  2241. static int voice_send_set_device_cmd(struct voice_data *v)
  2242. {
  2243. struct cvp_set_device_cmd cvp_setdev_cmd;
  2244. int ret = 0;
  2245. void *apr_cvp;
  2246. u16 cvp_handle;
  2247. if (v == NULL) {
  2248. pr_err("%s: v is NULL\n", __func__);
  2249. return -EINVAL;
  2250. }
  2251. apr_cvp = common.apr_q6_cvp;
  2252. if (!apr_cvp) {
  2253. pr_err("%s: apr_cvp is NULL.\n", __func__);
  2254. return -EINVAL;
  2255. }
  2256. cvp_handle = voice_get_cvp_handle(v);
  2257. /* set device and wait for response */
  2258. cvp_setdev_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2259. APR_HDR_LEN(APR_HDR_SIZE),
  2260. APR_PKT_VER);
  2261. cvp_setdev_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  2262. sizeof(cvp_setdev_cmd) - APR_HDR_SIZE);
  2263. pr_debug(" send create cvp setdev, pkt size = %d\n",
  2264. cvp_setdev_cmd.hdr.pkt_size);
  2265. cvp_setdev_cmd.hdr.src_port =
  2266. voice_get_idx_for_session(v->session_id);
  2267. cvp_setdev_cmd.hdr.dest_port = cvp_handle;
  2268. cvp_setdev_cmd.hdr.token = 0;
  2269. if (voice_get_cvd_int_version(common.cvd_version) >=
  2270. CVD_INT_VERSION_2_2)
  2271. cvp_setdev_cmd.hdr.opcode =
  2272. VSS_IVOCPROC_CMD_SET_DEVICE_V3;
  2273. else
  2274. cvp_setdev_cmd.hdr.opcode =
  2275. VSS_IVOCPROC_CMD_SET_DEVICE_V2;
  2276. voc_get_tx_rx_topology(v,
  2277. &cvp_setdev_cmd.cvp_set_device_v2.tx_topology_id,
  2278. &cvp_setdev_cmd.cvp_set_device_v2.rx_topology_id);
  2279. voice_set_topology_specific_info(v, CVP_VOC_RX_TOPOLOGY_CAL);
  2280. voice_set_topology_specific_info(v, CVP_VOC_TX_TOPOLOGY_CAL);
  2281. cvp_setdev_cmd.cvp_set_device_v2.tx_port_id = v->dev_tx.port_id;
  2282. cvp_setdev_cmd.cvp_set_device_v2.rx_port_id = v->dev_rx.port_id;
  2283. if (common.ec_ref_ext) {
  2284. cvp_setdev_cmd.cvp_set_device_v2.vocproc_mode =
  2285. VSS_IVOCPROC_VOCPROC_MODE_EC_EXT_MIXING;
  2286. cvp_setdev_cmd.cvp_set_device_v2.ec_ref_port_id =
  2287. common.ec_media_fmt_info.port_id;
  2288. } else {
  2289. cvp_setdev_cmd.cvp_set_device_v2.vocproc_mode =
  2290. VSS_IVOCPROC_VOCPROC_MODE_EC_INT_MIXING;
  2291. cvp_setdev_cmd.cvp_set_device_v2.ec_ref_port_id =
  2292. VSS_IVOCPROC_PORT_ID_NONE;
  2293. }
  2294. pr_debug("topology=%d , tx_port_id=%d, rx_port_id=%d\n",
  2295. cvp_setdev_cmd.cvp_set_device_v2.tx_topology_id,
  2296. cvp_setdev_cmd.cvp_set_device_v2.tx_port_id,
  2297. cvp_setdev_cmd.cvp_set_device_v2.rx_port_id);
  2298. v->cvp_state = CMD_STATUS_FAIL;
  2299. v->async_err = 0;
  2300. ret = apr_send_pkt(apr_cvp, (uint32_t *) &cvp_setdev_cmd);
  2301. if (ret < 0) {
  2302. pr_err("Fail in sending VSS_IVOCPROC_CMD_SET_DEVICE\n");
  2303. goto fail;
  2304. }
  2305. ret = wait_event_timeout(v->cvp_wait,
  2306. (v->cvp_state == CMD_STATUS_SUCCESS),
  2307. msecs_to_jiffies(TIMEOUT_MS));
  2308. if (!ret) {
  2309. pr_err("%s: wait_event timeout\n", __func__);
  2310. goto fail;
  2311. }
  2312. if (v->async_err > 0) {
  2313. pr_err("%s: DSP returned error[%s]\n",
  2314. __func__, adsp_err_get_err_str(
  2315. v->async_err));
  2316. ret = adsp_err_get_lnx_err_code(
  2317. v->async_err);
  2318. goto fail;
  2319. }
  2320. return 0;
  2321. fail:
  2322. return ret;
  2323. }
  2324. static int voice_send_stop_voice_cmd(struct voice_data *v)
  2325. {
  2326. struct apr_hdr mvm_stop_voice_cmd;
  2327. int ret = 0;
  2328. void *apr_mvm;
  2329. u16 mvm_handle;
  2330. if (v == NULL) {
  2331. pr_err("%s: v is NULL\n", __func__);
  2332. return -EINVAL;
  2333. }
  2334. apr_mvm = common.apr_q6_mvm;
  2335. if (!apr_mvm) {
  2336. pr_err("%s: apr_mvm is NULL.\n", __func__);
  2337. return -EINVAL;
  2338. }
  2339. mvm_handle = voice_get_mvm_handle(v);
  2340. mvm_stop_voice_cmd.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2341. APR_HDR_LEN(APR_HDR_SIZE),
  2342. APR_PKT_VER);
  2343. mvm_stop_voice_cmd.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  2344. sizeof(mvm_stop_voice_cmd) - APR_HDR_SIZE);
  2345. pr_debug("send mvm_stop_voice_cmd pkt size = %d\n",
  2346. mvm_stop_voice_cmd.pkt_size);
  2347. mvm_stop_voice_cmd.src_port =
  2348. voice_get_idx_for_session(v->session_id);
  2349. mvm_stop_voice_cmd.dest_port = mvm_handle;
  2350. mvm_stop_voice_cmd.token = 0;
  2351. mvm_stop_voice_cmd.opcode = VSS_IMVM_CMD_STOP_VOICE;
  2352. v->mvm_state = CMD_STATUS_FAIL;
  2353. v->async_err = 0;
  2354. ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_stop_voice_cmd);
  2355. if (ret < 0) {
  2356. pr_err("Fail in sending VSS_IMVM_CMD_STOP_VOICE\n");
  2357. goto fail;
  2358. }
  2359. ret = wait_event_timeout(v->mvm_wait,
  2360. (v->mvm_state == CMD_STATUS_SUCCESS),
  2361. msecs_to_jiffies(TIMEOUT_MS));
  2362. if (!ret) {
  2363. pr_err("%s: wait_event timeout\n", __func__);
  2364. goto fail;
  2365. }
  2366. if (v->async_err > 0) {
  2367. pr_err("%s: DSP returned error[%s]\n",
  2368. __func__, adsp_err_get_err_str(
  2369. v->async_err));
  2370. ret = adsp_err_get_lnx_err_code(
  2371. v->async_err);
  2372. goto fail;
  2373. }
  2374. return 0;
  2375. fail:
  2376. return ret;
  2377. }
  2378. static int voice_get_cal(struct cal_block_data **cal_block,
  2379. int cal_block_idx,
  2380. struct cal_block_data **col_data,
  2381. int col_data_idx, int session_id)
  2382. {
  2383. int ret = 0;
  2384. int dest_perms[2] = {PERM_READ | PERM_WRITE, PERM_READ | PERM_WRITE};
  2385. int source_vm[1] = {VMID_HLOS};
  2386. int dest_vm[2] = {VMID_LPASS, VMID_ADSP_HEAP};
  2387. *cal_block = cal_utils_get_only_cal_block(
  2388. common.cal_data[cal_block_idx]);
  2389. if (*cal_block == NULL) {
  2390. pr_err("%s: No cal data for cal %d!\n",
  2391. __func__, cal_block_idx);
  2392. ret = -ENODEV;
  2393. goto done;
  2394. }
  2395. if ((*cal_block)->cma_mem) {
  2396. if ((*cal_block)->cal_data.paddr == 0 ||
  2397. (*cal_block)->map_data.map_size <= 0) {
  2398. pr_err("%s: No address to map!\n", __func__);
  2399. ret = -EINVAL;
  2400. goto done;
  2401. }
  2402. ret = hyp_assign_phys((*cal_block)->cal_data.paddr,
  2403. (*cal_block)->map_data.map_size,
  2404. source_vm, 1, dest_vm, dest_perms, 2);
  2405. if (ret < 0) {
  2406. pr_err("%s: hyp_assign_phys failed ret = %d addr = 0x%pK size = %d\n",
  2407. __func__, ret, (*cal_block)->cal_data.paddr,
  2408. (*cal_block)->map_data.map_size);
  2409. ret = -EINVAL;
  2410. goto done;
  2411. }
  2412. hyp_assigned = true;
  2413. pr_debug("%s: hyp_assign_phys success\n", __func__);
  2414. }
  2415. ret = remap_cal_data(*cal_block, session_id);
  2416. if (ret < 0) {
  2417. pr_err("%s: Remap_cal_data failed for cal %d!\n",
  2418. __func__, cal_block_idx);
  2419. ret = -ENODEV;
  2420. goto done;
  2421. }
  2422. if (col_data == NULL)
  2423. goto done;
  2424. *col_data = cal_utils_get_only_cal_block(
  2425. common.cal_data[col_data_idx]);
  2426. if (*col_data == NULL) {
  2427. pr_err("%s: No cal data for cal %d!\n",
  2428. __func__, col_data_idx);
  2429. ret = -ENODEV;
  2430. goto done;
  2431. }
  2432. done:
  2433. return ret;
  2434. }
  2435. static int voice_send_cvs_register_cal_cmd(struct voice_data *v)
  2436. {
  2437. struct cvs_register_cal_data_cmd cvs_reg_cal_cmd;
  2438. struct cal_block_data *cal_block = NULL;
  2439. struct cal_block_data *col_data = NULL;
  2440. int ret = 0;
  2441. memset(&cvs_reg_cal_cmd, 0, sizeof(cvs_reg_cal_cmd));
  2442. if (v == NULL) {
  2443. pr_err("%s: v is NULL\n", __func__);
  2444. ret = -EINVAL;
  2445. goto done;
  2446. }
  2447. if (!common.apr_q6_cvs) {
  2448. pr_err("%s: apr_cvs is NULL\n", __func__);
  2449. ret = -EINVAL;
  2450. goto done;
  2451. }
  2452. mutex_lock(&common.cal_data[CVS_VOCSTRM_CAL]->lock);
  2453. mutex_lock(&common.cal_data[CVS_VOCSTRM_COL_CAL]->lock);
  2454. ret = voice_get_cal(&cal_block, CVS_VOCSTRM_CAL, &col_data,
  2455. CVS_VOCSTRM_COL_CAL, v->session_id);
  2456. if (ret < 0) {
  2457. pr_err("%s: Voice_get_cal failed for cal %d!\n",
  2458. __func__, CVS_VOCSTRM_CAL);
  2459. goto unlock;
  2460. }
  2461. memcpy(&cvs_reg_cal_cmd.cvs_cal_data.column_info[0],
  2462. (void *) &((struct audio_cal_info_voc_col *)
  2463. col_data->cal_info)->data,
  2464. col_data->cal_data.size);
  2465. cvs_reg_cal_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2466. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2467. cvs_reg_cal_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  2468. sizeof(cvs_reg_cal_cmd) - APR_HDR_SIZE);
  2469. cvs_reg_cal_cmd.hdr.src_port =
  2470. voice_get_idx_for_session(v->session_id);
  2471. cvs_reg_cal_cmd.hdr.dest_port = voice_get_cvs_handle(v);
  2472. cvs_reg_cal_cmd.hdr.token = 0;
  2473. if (common.is_per_vocoder_cal_enabled)
  2474. cvs_reg_cal_cmd.hdr.opcode =
  2475. VSS_ISTREAM_CMD_REGISTER_STATIC_CALIBRATION_DATA;
  2476. else
  2477. cvs_reg_cal_cmd.hdr.opcode =
  2478. VSS_ISTREAM_CMD_REGISTER_CALIBRATION_DATA_V2;
  2479. cvs_reg_cal_cmd.cvs_cal_data.cal_mem_handle =
  2480. cal_block->map_data.q6map_handle;
  2481. cvs_reg_cal_cmd.cvs_cal_data.cal_mem_address_lsw =
  2482. lower_32_bits(cal_block->cal_data.paddr);
  2483. cvs_reg_cal_cmd.cvs_cal_data.cal_mem_address_msw =
  2484. msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
  2485. cvs_reg_cal_cmd.cvs_cal_data.cal_mem_size =
  2486. cal_block->cal_data.size;
  2487. v->cvs_state = CMD_STATUS_FAIL;
  2488. v->async_err = 0;
  2489. ret = apr_send_pkt(common.apr_q6_cvs, (uint32_t *) &cvs_reg_cal_cmd);
  2490. if (ret < 0) {
  2491. pr_err("%s: Error %d registering CVS cal\n", __func__, ret);
  2492. ret = -EINVAL;
  2493. goto unlock;
  2494. }
  2495. ret = wait_event_timeout(v->cvs_wait,
  2496. (v->cvs_state == CMD_STATUS_SUCCESS),
  2497. msecs_to_jiffies(TIMEOUT_MS));
  2498. if (!ret) {
  2499. pr_err("%s: Command timeout\n", __func__);
  2500. ret = -EINVAL;
  2501. goto unlock;
  2502. }
  2503. if (v->async_err > 0) {
  2504. pr_err("%s: DSP returned error[%s]\n",
  2505. __func__, adsp_err_get_err_str(
  2506. v->async_err));
  2507. ret = adsp_err_get_lnx_err_code(
  2508. v->async_err);
  2509. goto unlock;
  2510. }
  2511. unlock:
  2512. mutex_unlock(&common.cal_data[CVS_VOCSTRM_COL_CAL]->lock);
  2513. mutex_unlock(&common.cal_data[CVS_VOCSTRM_CAL]->lock);
  2514. done:
  2515. return ret;
  2516. }
  2517. static int voice_send_cvs_deregister_cal_cmd(struct voice_data *v)
  2518. {
  2519. struct cvs_deregister_cal_data_cmd cvs_dereg_cal_cmd;
  2520. int ret = 0;
  2521. memset(&cvs_dereg_cal_cmd, 0, sizeof(cvs_dereg_cal_cmd));
  2522. if (v == NULL) {
  2523. pr_err("%s: v is NULL\n", __func__);
  2524. ret = -EINVAL;
  2525. goto done;
  2526. }
  2527. if (!common.apr_q6_cvs) {
  2528. pr_err("%s: apr_cvs is NULL\n", __func__);
  2529. ret = -EPERM;
  2530. goto done;
  2531. }
  2532. cvs_dereg_cal_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2533. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2534. cvs_dereg_cal_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  2535. sizeof(cvs_dereg_cal_cmd) - APR_HDR_SIZE);
  2536. cvs_dereg_cal_cmd.hdr.src_port =
  2537. voice_get_idx_for_session(v->session_id);
  2538. cvs_dereg_cal_cmd.hdr.dest_port = voice_get_cvs_handle(v);
  2539. cvs_dereg_cal_cmd.hdr.token = 0;
  2540. if (common.is_per_vocoder_cal_enabled)
  2541. cvs_dereg_cal_cmd.hdr.opcode =
  2542. VSS_ISTREAM_CMD_DEREGISTER_STATIC_CALIBRATION_DATA;
  2543. else
  2544. cvs_dereg_cal_cmd.hdr.opcode =
  2545. VSS_ISTREAM_CMD_DEREGISTER_CALIBRATION_DATA;
  2546. v->cvs_state = CMD_STATUS_FAIL;
  2547. v->async_err = 0;
  2548. ret = apr_send_pkt(common.apr_q6_cvs, (uint32_t *) &cvs_dereg_cal_cmd);
  2549. if (ret < 0) {
  2550. pr_err("%s: Error %d de-registering CVS cal\n", __func__, ret);
  2551. goto done;
  2552. }
  2553. ret = wait_event_timeout(v->cvs_wait,
  2554. (v->cvs_state == CMD_STATUS_SUCCESS),
  2555. msecs_to_jiffies(TIMEOUT_MS));
  2556. if (!ret) {
  2557. pr_err("%s: Command timeout\n", __func__);
  2558. goto done;
  2559. }
  2560. if (v->async_err > 0) {
  2561. pr_err("%s: DSP returned error[%s]\n",
  2562. __func__, adsp_err_get_err_str(
  2563. v->async_err));
  2564. ret = adsp_err_get_lnx_err_code(
  2565. v->async_err);
  2566. goto done;
  2567. }
  2568. done:
  2569. return ret;
  2570. }
  2571. static void voice_load_topo_modules(int cal_index)
  2572. {
  2573. uint32_t topology_id;
  2574. int ret;
  2575. topology_id = voice_get_topology(cal_index);
  2576. ret = q6core_load_unload_topo_modules(topology_id, CORE_LOAD_TOPOLOGY);
  2577. if (ret < 0)
  2578. pr_debug("%s ret:%d load topo modules %d failed\n",
  2579. __func__, ret, topology_id);
  2580. }
  2581. static void voice_unload_topo_modules(void)
  2582. {
  2583. uint32_t topology_id;
  2584. int i, ret;
  2585. for (i = CVP_VOC_RX_TOPOLOGY_CAL; i <= CVP_VOC_TX_TOPOLOGY_CAL; i++) {
  2586. topology_id = voice_get_topology(i);
  2587. ret = q6core_load_unload_topo_modules(topology_id,
  2588. CORE_UNLOAD_TOPOLOGY);
  2589. if (ret < 0) {
  2590. pr_debug("%s ret:%d unload topo modules %d failed\n",
  2591. __func__, ret, topology_id);
  2592. }
  2593. }
  2594. }
  2595. static int voice_send_cvp_create_cmd(struct voice_data *v)
  2596. {
  2597. struct cvp_create_full_ctl_session_cmd cvp_session_cmd;
  2598. void *apr_cvp;
  2599. int ret = 0;
  2600. if (v == NULL) {
  2601. pr_err("%s: v is NULL\n", __func__);
  2602. ret = -EINVAL;
  2603. goto done;
  2604. }
  2605. apr_cvp = common.apr_q6_cvp;
  2606. if (!apr_cvp) {
  2607. pr_err("%s: apr_cvp is NULL.\n", __func__);
  2608. ret = -EINVAL;
  2609. goto done;
  2610. }
  2611. /* create cvp session and wait for response */
  2612. cvp_session_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2613. APR_HDR_LEN(APR_HDR_SIZE),
  2614. APR_PKT_VER);
  2615. cvp_session_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  2616. sizeof(cvp_session_cmd) - APR_HDR_SIZE);
  2617. pr_debug("%s: send create cvp session, pkt size = %d\n",
  2618. __func__, cvp_session_cmd.hdr.pkt_size);
  2619. cvp_session_cmd.hdr.src_port =
  2620. voice_get_idx_for_session(v->session_id);
  2621. cvp_session_cmd.hdr.dest_port = 0;
  2622. cvp_session_cmd.hdr.token = 0;
  2623. if (voice_get_cvd_int_version(common.cvd_version) >=
  2624. CVD_INT_VERSION_2_2)
  2625. cvp_session_cmd.hdr.opcode =
  2626. VSS_IVOCPROC_CMD_CREATE_FULL_CONTROL_SESSION_V3;
  2627. else
  2628. cvp_session_cmd.hdr.opcode =
  2629. VSS_IVOCPROC_CMD_CREATE_FULL_CONTROL_SESSION_V2;
  2630. voc_get_tx_rx_topology(v,
  2631. &cvp_session_cmd.cvp_session.tx_topology_id,
  2632. &cvp_session_cmd.cvp_session.rx_topology_id);
  2633. voice_set_topology_specific_info(v, CVP_VOC_RX_TOPOLOGY_CAL);
  2634. voice_set_topology_specific_info(v, CVP_VOC_TX_TOPOLOGY_CAL);
  2635. cvp_session_cmd.cvp_session.direction = 2; /*tx and rx*/
  2636. cvp_session_cmd.cvp_session.tx_port_id = v->dev_tx.port_id;
  2637. cvp_session_cmd.cvp_session.rx_port_id = v->dev_rx.port_id;
  2638. cvp_session_cmd.cvp_session.profile_id =
  2639. VSS_ICOMMON_CAL_NETWORK_ID_NONE;
  2640. if (common.ec_ref_ext) {
  2641. cvp_session_cmd.cvp_session.vocproc_mode =
  2642. VSS_IVOCPROC_VOCPROC_MODE_EC_EXT_MIXING;
  2643. cvp_session_cmd.cvp_session.ec_ref_port_id =
  2644. common.ec_media_fmt_info.port_id;
  2645. } else {
  2646. cvp_session_cmd.cvp_session.vocproc_mode =
  2647. VSS_IVOCPROC_VOCPROC_MODE_EC_INT_MIXING;
  2648. cvp_session_cmd.cvp_session.ec_ref_port_id =
  2649. VSS_IVOCPROC_PORT_ID_NONE;
  2650. }
  2651. pr_debug("tx_topology: %d tx_port_id=%d, rx_port_id=%d, mode: 0x%x\n",
  2652. cvp_session_cmd.cvp_session.tx_topology_id,
  2653. cvp_session_cmd.cvp_session.tx_port_id,
  2654. cvp_session_cmd.cvp_session.rx_port_id,
  2655. cvp_session_cmd.cvp_session.vocproc_mode);
  2656. pr_debug("rx_topology: %d, profile_id: 0x%x, pkt_size: %d\n",
  2657. cvp_session_cmd.cvp_session.rx_topology_id,
  2658. cvp_session_cmd.cvp_session.profile_id,
  2659. cvp_session_cmd.hdr.pkt_size);
  2660. v->cvp_state = CMD_STATUS_FAIL;
  2661. v->async_err = 0;
  2662. ret = apr_send_pkt(apr_cvp, (uint32_t *) &cvp_session_cmd);
  2663. if (ret < 0) {
  2664. pr_err("Fail in sending VOCPROC_FULL_CONTROL_SESSION\n");
  2665. ret = -EINVAL;
  2666. goto done;
  2667. }
  2668. ret = wait_event_timeout(v->cvp_wait,
  2669. (v->cvp_state == CMD_STATUS_SUCCESS),
  2670. msecs_to_jiffies(TIMEOUT_MS));
  2671. if (!ret) {
  2672. pr_err("%s: wait_event timeout\n", __func__);
  2673. ret = -EINVAL;
  2674. goto done;
  2675. }
  2676. if (v->async_err > 0) {
  2677. pr_err("%s: DSP returned error[%s]\n",
  2678. __func__, adsp_err_get_err_str(
  2679. v->async_err));
  2680. ret = adsp_err_get_lnx_err_code(
  2681. v->async_err);
  2682. goto done;
  2683. }
  2684. done:
  2685. return ret;
  2686. }
  2687. static int voice_send_cvp_register_dev_cfg_cmd(struct voice_data *v)
  2688. {
  2689. struct cvp_register_dev_cfg_cmd cvp_reg_dev_cfg_cmd;
  2690. struct cal_block_data *cal_block = NULL;
  2691. int ret = 0;
  2692. memset(&cvp_reg_dev_cfg_cmd, 0, sizeof(cvp_reg_dev_cfg_cmd));
  2693. if (v == NULL) {
  2694. pr_err("%s: v is NULL\n", __func__);
  2695. ret = -EINVAL;
  2696. goto done;
  2697. }
  2698. if (!common.apr_q6_cvp) {
  2699. pr_err("%s: apr_cvp is NULL\n", __func__);
  2700. ret = -EPERM;
  2701. goto done;
  2702. }
  2703. mutex_lock(&common.cal_data[CVP_VOCDEV_CFG_CAL]->lock);
  2704. ret = voice_get_cal(&cal_block, CVP_VOCDEV_CFG_CAL, NULL,
  2705. 0, v->session_id);
  2706. if (ret < 0) {
  2707. pr_err("%s: Voice_get_cal failed for cal %d!\n",
  2708. __func__, CVP_VOCDEV_CFG_CAL);
  2709. goto unlock;
  2710. }
  2711. cvp_reg_dev_cfg_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2712. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2713. cvp_reg_dev_cfg_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  2714. sizeof(cvp_reg_dev_cfg_cmd) - APR_HDR_SIZE);
  2715. cvp_reg_dev_cfg_cmd.hdr.src_port =
  2716. voice_get_idx_for_session(v->session_id);
  2717. cvp_reg_dev_cfg_cmd.hdr.dest_port = voice_get_cvp_handle(v);
  2718. cvp_reg_dev_cfg_cmd.hdr.token = 0;
  2719. cvp_reg_dev_cfg_cmd.hdr.opcode =
  2720. VSS_IVOCPROC_CMD_REGISTER_DEVICE_CONFIG;
  2721. cvp_reg_dev_cfg_cmd.cvp_dev_cfg_data.mem_handle =
  2722. cal_block->map_data.q6map_handle;
  2723. cvp_reg_dev_cfg_cmd.cvp_dev_cfg_data.mem_address_lsw =
  2724. lower_32_bits(cal_block->cal_data.paddr);
  2725. cvp_reg_dev_cfg_cmd.cvp_dev_cfg_data.mem_address_msw =
  2726. msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
  2727. cvp_reg_dev_cfg_cmd.cvp_dev_cfg_data.mem_size =
  2728. cal_block->cal_data.size;
  2729. v->cvp_state = CMD_STATUS_FAIL;
  2730. v->async_err = 0;
  2731. ret = apr_send_pkt(common.apr_q6_cvp,
  2732. (uint32_t *) &cvp_reg_dev_cfg_cmd);
  2733. if (ret < 0) {
  2734. pr_err("%s: Error %d registering CVP dev cfg cal\n",
  2735. __func__, ret);
  2736. ret = -EINVAL;
  2737. goto unlock;
  2738. }
  2739. ret = wait_event_timeout(v->cvp_wait,
  2740. (v->cvp_state == CMD_STATUS_SUCCESS),
  2741. msecs_to_jiffies(TIMEOUT_MS));
  2742. if (!ret) {
  2743. pr_err("%s: Command timeout\n", __func__);
  2744. ret = -EINVAL;
  2745. goto unlock;
  2746. }
  2747. if (v->async_err > 0) {
  2748. pr_err("%s: DSP returned error[%s]\n",
  2749. __func__, adsp_err_get_err_str(
  2750. v->async_err));
  2751. ret = adsp_err_get_lnx_err_code(
  2752. v->async_err);
  2753. goto unlock;
  2754. }
  2755. unlock:
  2756. mutex_unlock(&common.cal_data[CVP_VOCDEV_CFG_CAL]->lock);
  2757. done:
  2758. return ret;
  2759. }
  2760. static int voice_send_cvp_deregister_dev_cfg_cmd(struct voice_data *v)
  2761. {
  2762. struct cvp_deregister_dev_cfg_cmd cvp_dereg_dev_cfg_cmd;
  2763. int ret = 0;
  2764. memset(&cvp_dereg_dev_cfg_cmd, 0, sizeof(cvp_dereg_dev_cfg_cmd));
  2765. if (v == NULL) {
  2766. pr_err("%s: v is NULL\n", __func__);
  2767. ret = -EINVAL;
  2768. goto done;
  2769. }
  2770. if (!common.apr_q6_cvp) {
  2771. pr_err("%s: apr_cvp is NULL\n", __func__);
  2772. ret = -EPERM;
  2773. goto done;
  2774. }
  2775. cvp_dereg_dev_cfg_cmd.hdr.hdr_field =
  2776. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2777. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2778. cvp_dereg_dev_cfg_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  2779. sizeof(cvp_dereg_dev_cfg_cmd) - APR_HDR_SIZE);
  2780. cvp_dereg_dev_cfg_cmd.hdr.src_port =
  2781. voice_get_idx_for_session(v->session_id);
  2782. cvp_dereg_dev_cfg_cmd.hdr.dest_port = voice_get_cvp_handle(v);
  2783. cvp_dereg_dev_cfg_cmd.hdr.token = 0;
  2784. cvp_dereg_dev_cfg_cmd.hdr.opcode =
  2785. VSS_IVOCPROC_CMD_DEREGISTER_DEVICE_CONFIG;
  2786. v->cvp_state = CMD_STATUS_FAIL;
  2787. v->async_err = 0;
  2788. ret = apr_send_pkt(common.apr_q6_cvp,
  2789. (uint32_t *) &cvp_dereg_dev_cfg_cmd);
  2790. if (ret < 0) {
  2791. pr_err("%s: Error %d de-registering CVP dev cfg cal\n",
  2792. __func__, ret);
  2793. goto done;
  2794. }
  2795. ret = wait_event_timeout(v->cvp_wait,
  2796. (v->cvp_state == CMD_STATUS_SUCCESS),
  2797. msecs_to_jiffies(TIMEOUT_MS));
  2798. if (!ret) {
  2799. pr_err("%s: Command timeout\n", __func__);
  2800. goto done;
  2801. }
  2802. if (v->async_err > 0) {
  2803. pr_err("%s: DSP returned error[%s]\n",
  2804. __func__, adsp_err_get_err_str(
  2805. v->async_err));
  2806. ret = adsp_err_get_lnx_err_code(
  2807. v->async_err);
  2808. goto done;
  2809. }
  2810. done:
  2811. return ret;
  2812. }
  2813. static int voice_send_cvp_register_cal_cmd(struct voice_data *v)
  2814. {
  2815. struct cvp_register_cal_data_cmd cvp_reg_cal_cmd;
  2816. struct cal_block_data *cal_block = NULL;
  2817. struct cal_block_data *col_data = NULL;
  2818. int ret = 0;
  2819. memset(&cvp_reg_cal_cmd, 0, sizeof(cvp_reg_cal_cmd));
  2820. if (v == NULL) {
  2821. pr_err("%s: v is NULL\n", __func__);
  2822. ret = -EINVAL;
  2823. goto done;
  2824. }
  2825. if (!common.apr_q6_cvp) {
  2826. pr_err("%s: apr_cvp is NULL\n", __func__);
  2827. ret = -EINVAL;
  2828. goto done;
  2829. }
  2830. mutex_lock(&common.cal_data[CVP_VOCPROC_CAL]->lock);
  2831. mutex_lock(&common.cal_data[CVP_VOCPROC_COL_CAL]->lock);
  2832. ret = voice_get_cal(&cal_block, CVP_VOCPROC_CAL, &col_data,
  2833. CVP_VOCPROC_COL_CAL, v->session_id);
  2834. if (ret < 0) {
  2835. pr_err("%s: Voice_get_cal failed for cal %d!\n",
  2836. __func__, CVP_VOCPROC_CAL);
  2837. goto unlock;
  2838. }
  2839. v->dev_tx.dev_id = ((struct audio_cal_info_vocproc *)
  2840. cal_block->cal_info)->tx_acdb_id;
  2841. v->dev_rx.dev_id = ((struct audio_cal_info_vocproc *)
  2842. cal_block->cal_info)->rx_acdb_id;
  2843. pr_debug("%s: %s: Tx acdb id = %d and Rx acdb id = %d", __func__,
  2844. voc_get_session_name(v->session_id), v->dev_tx.dev_id,
  2845. v->dev_rx.dev_id);
  2846. memcpy(&cvp_reg_cal_cmd.cvp_cal_data.column_info[0],
  2847. (void *) &((struct audio_cal_info_voc_col *)
  2848. col_data->cal_info)->data,
  2849. col_data->cal_data.size);
  2850. cvp_reg_cal_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2851. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2852. cvp_reg_cal_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  2853. sizeof(cvp_reg_cal_cmd) - APR_HDR_SIZE);
  2854. cvp_reg_cal_cmd.hdr.src_port =
  2855. voice_get_idx_for_session(v->session_id);
  2856. cvp_reg_cal_cmd.hdr.dest_port = voice_get_cvp_handle(v);
  2857. cvp_reg_cal_cmd.hdr.token = 0;
  2858. if (common.is_per_vocoder_cal_enabled)
  2859. cvp_reg_cal_cmd.hdr.opcode =
  2860. VSS_IVOCPROC_CMD_REGISTER_STATIC_CALIBRATION_DATA;
  2861. else
  2862. cvp_reg_cal_cmd.hdr.opcode =
  2863. VSS_IVOCPROC_CMD_REGISTER_CALIBRATION_DATA_V2;
  2864. cvp_reg_cal_cmd.cvp_cal_data.cal_mem_handle =
  2865. cal_block->map_data.q6map_handle;
  2866. cvp_reg_cal_cmd.cvp_cal_data.cal_mem_address_lsw =
  2867. lower_32_bits(cal_block->cal_data.paddr);
  2868. cvp_reg_cal_cmd.cvp_cal_data.cal_mem_address_msw =
  2869. msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
  2870. cvp_reg_cal_cmd.cvp_cal_data.cal_mem_size =
  2871. cal_block->cal_data.size;
  2872. v->cvp_state = CMD_STATUS_FAIL;
  2873. v->async_err = 0;
  2874. ret = apr_send_pkt(common.apr_q6_cvp, (uint32_t *) &cvp_reg_cal_cmd);
  2875. if (ret < 0) {
  2876. pr_err("%s: Error %d registering CVP cal\n", __func__, ret);
  2877. ret = -EINVAL;
  2878. goto unlock;
  2879. }
  2880. ret = wait_event_timeout(v->cvp_wait,
  2881. (v->cvp_state == CMD_STATUS_SUCCESS),
  2882. msecs_to_jiffies(TIMEOUT_MS));
  2883. if (!ret) {
  2884. pr_err("%s: Command timeout\n", __func__);
  2885. ret = -EINVAL;
  2886. goto unlock;
  2887. }
  2888. if (v->async_err > 0) {
  2889. pr_err("%s: DSP returned error[%s]\n",
  2890. __func__, adsp_err_get_err_str(
  2891. v->async_err));
  2892. ret = adsp_err_get_lnx_err_code(
  2893. v->async_err);
  2894. goto unlock;
  2895. }
  2896. unlock:
  2897. mutex_unlock(&common.cal_data[CVP_VOCPROC_COL_CAL]->lock);
  2898. mutex_unlock(&common.cal_data[CVP_VOCPROC_CAL]->lock);
  2899. done:
  2900. return ret;
  2901. }
  2902. static int voice_send_cvp_deregister_cal_cmd(struct voice_data *v)
  2903. {
  2904. struct cvp_deregister_cal_data_cmd cvp_dereg_cal_cmd;
  2905. int ret = 0;
  2906. int cal_index = CVP_VOCPROC_CAL;
  2907. struct cal_block_data *cal_block;
  2908. int dest_perms[1] = {PERM_READ | PERM_WRITE | PERM_EXEC};
  2909. int source_vm[2] = {VMID_LPASS, VMID_ADSP_HEAP};
  2910. int dest_vm[1] = {VMID_HLOS};
  2911. memset(&cvp_dereg_cal_cmd, 0, sizeof(cvp_dereg_cal_cmd));
  2912. if (v == NULL) {
  2913. pr_err("%s: v is NULL\n", __func__);
  2914. ret = -EINVAL;
  2915. goto done;
  2916. }
  2917. if (!common.apr_q6_cvp) {
  2918. pr_err("%s: apr_cvp is NULL.\n", __func__);
  2919. ret = -EPERM;
  2920. goto done;
  2921. }
  2922. cvp_dereg_cal_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2923. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2924. cvp_dereg_cal_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  2925. sizeof(cvp_dereg_cal_cmd) - APR_HDR_SIZE);
  2926. cvp_dereg_cal_cmd.hdr.src_port =
  2927. voice_get_idx_for_session(v->session_id);
  2928. cvp_dereg_cal_cmd.hdr.dest_port = voice_get_cvp_handle(v);
  2929. cvp_dereg_cal_cmd.hdr.token = 0;
  2930. if (common.is_per_vocoder_cal_enabled)
  2931. cvp_dereg_cal_cmd.hdr.opcode =
  2932. VSS_IVOCPROC_CMD_DEREGISTER_STATIC_CALIBRATION_DATA;
  2933. else
  2934. cvp_dereg_cal_cmd.hdr.opcode =
  2935. VSS_IVOCPROC_CMD_DEREGISTER_CALIBRATION_DATA;
  2936. v->cvp_state = CMD_STATUS_FAIL;
  2937. v->async_err = 0;
  2938. ret = apr_send_pkt(common.apr_q6_cvp, (uint32_t *) &cvp_dereg_cal_cmd);
  2939. if (ret < 0) {
  2940. pr_err("%s: Error %d de-registering CVP cal\n", __func__, ret);
  2941. goto done;
  2942. }
  2943. ret = wait_event_timeout(v->cvp_wait,
  2944. (v->cvp_state == CMD_STATUS_SUCCESS),
  2945. msecs_to_jiffies(TIMEOUT_MS));
  2946. if (!ret) {
  2947. pr_err("%s: Command timeout\n", __func__);
  2948. goto done;
  2949. }
  2950. mutex_lock(&common.cal_data[cal_index]->lock);
  2951. cal_block = cal_utils_get_only_cal_block(
  2952. common.cal_data[cal_index]);
  2953. if (cal_block == NULL) {
  2954. pr_err("%s: Cal block is NULL, index %d!\n",
  2955. __func__, cal_index);
  2956. ret = -EINVAL;
  2957. goto unlock;
  2958. }
  2959. pr_debug("%s: use hyp assigned %d\n",__func__, hyp_assigned);
  2960. if (cal_block->cma_mem && hyp_assigned) {
  2961. if (cal_block->cal_data.paddr == 0 ||
  2962. cal_block->map_data.map_size <= 0) {
  2963. pr_err("%s: No address to map!\n", __func__);
  2964. ret = -EINVAL;
  2965. goto unlock;
  2966. }
  2967. ret = hyp_assign_phys(cal_block->cal_data.paddr,
  2968. cal_block->map_data.map_size,
  2969. source_vm, 2, dest_vm, dest_perms, 1);
  2970. if (ret < 0) {
  2971. pr_err("%s: hyp_assign_phys failed result = %d addr = 0x%pK size = %d\n",
  2972. __func__, ret, cal_block->cal_data.paddr,
  2973. cal_block->map_data.map_size);
  2974. ret = -EINVAL;
  2975. goto unlock;
  2976. } else {
  2977. hyp_assigned = false;
  2978. pr_debug("%s: hyp_assign_phys success\n", __func__);
  2979. }
  2980. }
  2981. if (v->async_err > 0) {
  2982. pr_err("%s: DSP returned error[%s]\n",
  2983. __func__, adsp_err_get_err_str(
  2984. v->async_err));
  2985. ret = adsp_err_get_lnx_err_code(
  2986. v->async_err);
  2987. goto unlock;
  2988. }
  2989. unlock:
  2990. mutex_unlock(&common.cal_data[cal_index]->lock);
  2991. done:
  2992. return ret;
  2993. }
  2994. static int voice_send_cvp_register_vol_cal_cmd(struct voice_data *v)
  2995. {
  2996. struct cvp_register_vol_cal_data_cmd cvp_reg_vol_cal_cmd;
  2997. struct cal_block_data *cal_block = NULL;
  2998. struct cal_block_data *col_data = NULL;
  2999. int ret = 0;
  3000. memset(&cvp_reg_vol_cal_cmd, 0, sizeof(cvp_reg_vol_cal_cmd));
  3001. if (v == NULL) {
  3002. pr_err("%s: v is NULL\n", __func__);
  3003. ret = -EINVAL;
  3004. goto done;
  3005. }
  3006. if (!common.apr_q6_cvp) {
  3007. pr_err("%s: apr_cvp is NULL\n", __func__);
  3008. ret = -EINVAL;
  3009. goto done;
  3010. }
  3011. mutex_lock(&common.cal_data[CVP_VOCVOL_CAL]->lock);
  3012. mutex_lock(&common.cal_data[CVP_VOCVOL_COL_CAL]->lock);
  3013. ret = voice_get_cal(&cal_block, CVP_VOCVOL_CAL, &col_data,
  3014. CVP_VOCVOL_COL_CAL, v->session_id);
  3015. if (ret < 0) {
  3016. pr_err("%s: Voice_get_cal failed for cal %d!\n",
  3017. __func__, CVP_VOCVOL_CAL);
  3018. goto unlock;
  3019. }
  3020. memcpy(&cvp_reg_vol_cal_cmd.cvp_vol_cal_data.column_info[0],
  3021. (void *) &((struct audio_cal_info_voc_col *)
  3022. col_data->cal_info)->data,
  3023. col_data->cal_data.size);
  3024. cvp_reg_vol_cal_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3025. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3026. cvp_reg_vol_cal_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  3027. sizeof(cvp_reg_vol_cal_cmd) - APR_HDR_SIZE);
  3028. cvp_reg_vol_cal_cmd.hdr.src_port =
  3029. voice_get_idx_for_session(v->session_id);
  3030. cvp_reg_vol_cal_cmd.hdr.dest_port = voice_get_cvp_handle(v);
  3031. cvp_reg_vol_cal_cmd.hdr.token = 0;
  3032. if (common.is_per_vocoder_cal_enabled)
  3033. cvp_reg_vol_cal_cmd.hdr.opcode =
  3034. VSS_IVOCPROC_CMD_REGISTER_DYNAMIC_CALIBRATION_DATA;
  3035. else
  3036. cvp_reg_vol_cal_cmd.hdr.opcode =
  3037. VSS_IVOCPROC_CMD_REGISTER_VOL_CALIBRATION_DATA;
  3038. cvp_reg_vol_cal_cmd.cvp_vol_cal_data.cal_mem_handle =
  3039. cal_block->map_data.q6map_handle;
  3040. cvp_reg_vol_cal_cmd.cvp_vol_cal_data.cal_mem_address_lsw =
  3041. lower_32_bits(cal_block->cal_data.paddr);
  3042. cvp_reg_vol_cal_cmd.cvp_vol_cal_data.cal_mem_address_msw =
  3043. msm_audio_populate_upper_32_bits(cal_block->cal_data.paddr);
  3044. cvp_reg_vol_cal_cmd.cvp_vol_cal_data.cal_mem_size =
  3045. cal_block->cal_data.size;
  3046. v->cvp_state = CMD_STATUS_FAIL;
  3047. v->async_err = 0;
  3048. ret = apr_send_pkt(common.apr_q6_cvp,
  3049. (uint32_t *) &cvp_reg_vol_cal_cmd);
  3050. if (ret < 0) {
  3051. pr_err("%s: Error %d registering CVP vol cal\n", __func__, ret);
  3052. ret = -EINVAL;
  3053. goto unlock;
  3054. }
  3055. ret = wait_event_timeout(v->cvp_wait,
  3056. (v->cvp_state == CMD_STATUS_SUCCESS),
  3057. msecs_to_jiffies(TIMEOUT_MS));
  3058. if (!ret) {
  3059. pr_err("%s: Command timeout\n", __func__);
  3060. ret = -EINVAL;
  3061. goto unlock;
  3062. }
  3063. if (v->async_err > 0) {
  3064. pr_err("%s: DSP returned error[%s]\n",
  3065. __func__, adsp_err_get_err_str(
  3066. v->async_err));
  3067. ret = adsp_err_get_lnx_err_code(
  3068. v->async_err);
  3069. goto unlock;
  3070. }
  3071. unlock:
  3072. mutex_unlock(&common.cal_data[CVP_VOCVOL_COL_CAL]->lock);
  3073. mutex_unlock(&common.cal_data[CVP_VOCVOL_CAL]->lock);
  3074. done:
  3075. return ret;
  3076. }
  3077. static int voice_send_cvp_deregister_vol_cal_cmd(struct voice_data *v)
  3078. {
  3079. struct cvp_deregister_vol_cal_data_cmd cvp_dereg_vol_cal_cmd;
  3080. int ret = 0;
  3081. memset(&cvp_dereg_vol_cal_cmd, 0, sizeof(cvp_dereg_vol_cal_cmd));
  3082. if (v == NULL) {
  3083. pr_err("%s: v is NULL\n", __func__);
  3084. ret = -EINVAL;
  3085. goto done;
  3086. }
  3087. if (!common.apr_q6_cvp) {
  3088. pr_err("%s: apr_cvp is NULL\n", __func__);
  3089. ret = -EPERM;
  3090. goto done;
  3091. }
  3092. cvp_dereg_vol_cal_cmd.hdr.hdr_field =
  3093. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3094. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3095. cvp_dereg_vol_cal_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  3096. sizeof(cvp_dereg_vol_cal_cmd) - APR_HDR_SIZE);
  3097. cvp_dereg_vol_cal_cmd.hdr.src_port =
  3098. voice_get_idx_for_session(v->session_id);
  3099. cvp_dereg_vol_cal_cmd.hdr.dest_port = voice_get_cvp_handle(v);
  3100. cvp_dereg_vol_cal_cmd.hdr.token = 0;
  3101. if (common.is_per_vocoder_cal_enabled)
  3102. cvp_dereg_vol_cal_cmd.hdr.opcode =
  3103. VSS_IVOCPROC_CMD_DEREGISTER_DYNAMIC_CALIBRATION_DATA;
  3104. else
  3105. cvp_dereg_vol_cal_cmd.hdr.opcode =
  3106. VSS_IVOCPROC_CMD_DEREGISTER_VOL_CALIBRATION_DATA;
  3107. v->cvp_state = CMD_STATUS_FAIL;
  3108. v->async_err = 0;
  3109. ret = apr_send_pkt(common.apr_q6_cvp,
  3110. (uint32_t *) &cvp_dereg_vol_cal_cmd);
  3111. if (ret < 0) {
  3112. pr_err("%s: Error %d de-registering CVP vol cal\n",
  3113. __func__, ret);
  3114. goto done;
  3115. }
  3116. ret = wait_event_timeout(v->cvp_wait,
  3117. (v->cvp_state == CMD_STATUS_SUCCESS),
  3118. msecs_to_jiffies(TIMEOUT_MS));
  3119. if (!ret) {
  3120. pr_err("%s: Command timeout\n", __func__);
  3121. goto done;
  3122. }
  3123. if (v->async_err > 0) {
  3124. pr_err("%s: DSP returned error[%s]\n",
  3125. __func__, adsp_err_get_err_str(
  3126. v->async_err));
  3127. ret = adsp_err_get_lnx_err_code(
  3128. v->async_err);
  3129. goto done;
  3130. }
  3131. done:
  3132. return ret;
  3133. }
  3134. static int voice_map_memory_physical_cmd(struct voice_data *v,
  3135. struct mem_map_table *table_info,
  3136. dma_addr_t phys,
  3137. uint32_t size,
  3138. uint32_t token)
  3139. {
  3140. struct vss_imemory_cmd_map_physical_t mvm_map_phys_cmd;
  3141. uint32_t *memtable;
  3142. int ret = 0;
  3143. if (v == NULL) {
  3144. pr_err("%s: v is NULL\n", __func__);
  3145. ret = -EINVAL;
  3146. goto fail;
  3147. }
  3148. if (!common.apr_q6_mvm) {
  3149. pr_err("%s: apr_mvm is NULL.\n", __func__);
  3150. ret = -EINVAL;
  3151. goto fail;
  3152. }
  3153. if (!table_info->data) {
  3154. pr_err("%s: memory table is NULL.\n", __func__);
  3155. ret = -EINVAL;
  3156. goto fail;
  3157. }
  3158. memtable = (uint32_t *) table_info->data;
  3159. /*
  3160. * Store next table descriptor's address(64 bit) as NULL as there
  3161. * is only one memory block
  3162. */
  3163. memtable[0] = 0;
  3164. memtable[1] = 0;
  3165. /* Store next table descriptor's size */
  3166. memtable[2] = 0;
  3167. /* Store shared mem adddress (64 bit) */
  3168. memtable[3] = lower_32_bits(phys);
  3169. memtable[4] = msm_audio_populate_upper_32_bits(phys);
  3170. /* Store shared memory size */
  3171. memtable[5] = size;
  3172. mvm_map_phys_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3173. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3174. mvm_map_phys_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  3175. sizeof(mvm_map_phys_cmd) - APR_HDR_SIZE);
  3176. mvm_map_phys_cmd.hdr.src_port =
  3177. voice_get_idx_for_session(v->session_id);
  3178. mvm_map_phys_cmd.hdr.dest_port = voice_get_mvm_handle(v);
  3179. mvm_map_phys_cmd.hdr.token = token;
  3180. mvm_map_phys_cmd.hdr.opcode = VSS_IMEMORY_CMD_MAP_PHYSICAL;
  3181. mvm_map_phys_cmd.table_descriptor.mem_address_lsw =
  3182. lower_32_bits(table_info->phys);
  3183. mvm_map_phys_cmd.table_descriptor.mem_address_msw =
  3184. msm_audio_populate_upper_32_bits(table_info->phys);
  3185. mvm_map_phys_cmd.table_descriptor.mem_size =
  3186. sizeof(struct vss_imemory_block_t) +
  3187. sizeof(struct vss_imemory_table_descriptor_t);
  3188. mvm_map_phys_cmd.is_cached = true;
  3189. mvm_map_phys_cmd.cache_line_size = 128;
  3190. mvm_map_phys_cmd.access_mask = 3;
  3191. mvm_map_phys_cmd.page_align = 4096;
  3192. mvm_map_phys_cmd.min_data_width = 8;
  3193. mvm_map_phys_cmd.max_data_width = 64;
  3194. pr_debug("%s: next table desc: add: %lld, size: %d\n",
  3195. __func__, *((uint64_t *) memtable),
  3196. *(((uint32_t *) memtable) + 2));
  3197. pr_debug("%s: phy add of of mem being mapped LSW:0x%x, MSW:0x%x size: %d\n",
  3198. __func__, *(((uint32_t *) memtable) + 3),
  3199. *(((uint32_t *) memtable) + 4), *(((uint32_t *) memtable) + 5));
  3200. v->mvm_state = CMD_STATUS_FAIL;
  3201. v->async_err = 0;
  3202. ret = apr_send_pkt(common.apr_q6_mvm, (uint32_t *) &mvm_map_phys_cmd);
  3203. if (ret < 0) {
  3204. pr_err("%s: Error %d sending mvm map phy cmd\n", __func__, ret);
  3205. goto fail;
  3206. }
  3207. ret = wait_event_timeout(v->mvm_wait,
  3208. (v->mvm_state == CMD_STATUS_SUCCESS),
  3209. msecs_to_jiffies(TIMEOUT_MS));
  3210. if (!ret) {
  3211. pr_err("%s: Command timeout\n", __func__);
  3212. goto fail;
  3213. }
  3214. if (v->async_err > 0) {
  3215. pr_err("%s: DSP returned error[%s]\n",
  3216. __func__, adsp_err_get_err_str(
  3217. v->async_err));
  3218. ret = adsp_err_get_lnx_err_code(
  3219. v->async_err);
  3220. goto fail;
  3221. }
  3222. return 0;
  3223. fail:
  3224. return ret;
  3225. }
  3226. static int voice_pause_voice_call(struct voice_data *v)
  3227. {
  3228. struct apr_hdr mvm_pause_voice_cmd;
  3229. void *apr_mvm;
  3230. int ret = 0;
  3231. pr_debug("%s\n", __func__);
  3232. if (v == NULL) {
  3233. pr_err("%s: Voice data is NULL\n", __func__);
  3234. ret = -EINVAL;
  3235. goto done;
  3236. }
  3237. apr_mvm = common.apr_q6_mvm;
  3238. if (!apr_mvm) {
  3239. pr_err("%s: apr_mvm is NULL.\n", __func__);
  3240. ret = -EINVAL;
  3241. goto done;
  3242. }
  3243. mvm_pause_voice_cmd.hdr_field =
  3244. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3245. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3246. mvm_pause_voice_cmd.pkt_size =
  3247. APR_PKT_SIZE(APR_HDR_SIZE,
  3248. sizeof(mvm_pause_voice_cmd) - APR_HDR_SIZE);
  3249. mvm_pause_voice_cmd.src_port =
  3250. voice_get_idx_for_session(v->session_id);
  3251. mvm_pause_voice_cmd.dest_port = voice_get_mvm_handle(v);
  3252. mvm_pause_voice_cmd.token = 0;
  3253. mvm_pause_voice_cmd.opcode = VSS_IMVM_CMD_PAUSE_VOICE;
  3254. v->mvm_state = CMD_STATUS_FAIL;
  3255. v->async_err = 0;
  3256. pr_debug("%s: send mvm_pause_voice_cmd pkt size = %d\n",
  3257. __func__, mvm_pause_voice_cmd.pkt_size);
  3258. ret = apr_send_pkt(apr_mvm,
  3259. (uint32_t *)&mvm_pause_voice_cmd);
  3260. if (ret < 0) {
  3261. pr_err("Fail in sending VSS_IMVM_CMD_PAUSE_VOICE\n");
  3262. ret = -EINVAL;
  3263. goto done;
  3264. }
  3265. ret = wait_event_timeout(v->mvm_wait,
  3266. (v->mvm_state == CMD_STATUS_SUCCESS),
  3267. msecs_to_jiffies(TIMEOUT_MS));
  3268. if (!ret) {
  3269. pr_err("%s: Command timeout\n", __func__);
  3270. ret = -EINVAL;
  3271. goto done;
  3272. }
  3273. if (v->async_err > 0) {
  3274. pr_err("%s: DSP returned error[%s]\n",
  3275. __func__, adsp_err_get_err_str(
  3276. v->async_err));
  3277. ret = adsp_err_get_lnx_err_code(
  3278. v->async_err);
  3279. goto done;
  3280. }
  3281. done:
  3282. return ret;
  3283. }
  3284. static int voice_map_cal_memory(struct cal_block_data *cal_block,
  3285. uint32_t session_id)
  3286. {
  3287. int result = 0;
  3288. int voc_index;
  3289. struct voice_data *v = NULL;
  3290. pr_debug("%s\n", __func__);
  3291. if (cal_block == NULL) {
  3292. pr_err("%s: Cal block is NULL!\n", __func__);
  3293. result = -EINVAL;
  3294. goto done;
  3295. }
  3296. if (cal_block->cal_data.paddr == 0) {
  3297. pr_debug("%s: No address to map!\n", __func__);
  3298. result = -EINVAL;
  3299. goto done;
  3300. }
  3301. if (cal_block->map_data.map_size == 0) {
  3302. pr_debug("%s: Map size is 0!\n", __func__);
  3303. result = -EINVAL;
  3304. goto done;
  3305. }
  3306. voc_index = voice_get_idx_for_session(session_id);
  3307. if (voc_index < 0) {
  3308. pr_err("%s: Invalid session ID %d\n", __func__, session_id);
  3309. goto done;
  3310. }
  3311. mutex_lock(&common.common_lock);
  3312. v = &common.voice[voc_index];
  3313. result = voice_map_memory_physical_cmd(v,
  3314. &common.cal_mem_map_table,
  3315. (dma_addr_t)cal_block->cal_data.paddr,
  3316. cal_block->map_data.map_size,
  3317. VOC_CAL_MEM_MAP_TOKEN);
  3318. if (result < 0) {
  3319. pr_err("%s: Mmap did not work! addr = 0x%pK, size = %zd\n",
  3320. __func__,
  3321. &cal_block->cal_data.paddr,
  3322. cal_block->map_data.map_size);
  3323. goto done_unlock;
  3324. }
  3325. cal_block->map_data.q6map_handle = common.cal_mem_handle;
  3326. done_unlock:
  3327. mutex_unlock(&common.common_lock);
  3328. done:
  3329. return result;
  3330. }
  3331. static int remap_cal_data(struct cal_block_data *cal_block,
  3332. uint32_t session_id)
  3333. {
  3334. int ret = 0;
  3335. pr_debug("%s\n", __func__);
  3336. if (cal_block->map_data.dma_buf == NULL) {
  3337. pr_err("%s: No ION allocation for session_id %d!\n",
  3338. __func__, session_id);
  3339. ret = -EINVAL;
  3340. goto done;
  3341. }
  3342. if ((cal_block->map_data.map_size > 0) &&
  3343. (cal_block->map_data.q6map_handle == 0)) {
  3344. /* cal type not used */
  3345. ret = voice_map_cal_memory(cal_block, session_id);
  3346. if (ret < 0) {
  3347. pr_err("%s: Mmap did not work! size = %zd\n",
  3348. __func__, cal_block->map_data.map_size);
  3349. goto done;
  3350. }
  3351. } else {
  3352. pr_debug("%s: Cal block 0x%pK, size %zd already mapped. Q6 map handle = %d\n",
  3353. __func__, &cal_block->cal_data.paddr,
  3354. cal_block->map_data.map_size,
  3355. cal_block->map_data.q6map_handle);
  3356. }
  3357. done:
  3358. return ret;
  3359. }
  3360. static int voice_unmap_cal_memory(int32_t cal_type,
  3361. struct cal_block_data *cal_block)
  3362. {
  3363. int result = 0;
  3364. int result2 = 0;
  3365. int i;
  3366. struct voice_data *v = NULL;
  3367. pr_debug("%s\n", __func__);
  3368. if (cal_block == NULL) {
  3369. pr_err("%s: Cal block is NULL!\n", __func__);
  3370. result = -EINVAL;
  3371. goto done;
  3372. }
  3373. if (cal_block->map_data.q6map_handle == 0) {
  3374. pr_debug("%s: Q6 handle is not set!\n", __func__);
  3375. result = -EINVAL;
  3376. goto done;
  3377. }
  3378. mutex_lock(&common.common_lock);
  3379. for (i = 0; i < MAX_VOC_SESSIONS; i++) {
  3380. v = &common.voice[i];
  3381. mutex_lock(&v->lock);
  3382. if (is_voc_state_active(v->voc_state)) {
  3383. result2 = voice_pause_voice_call(v);
  3384. if (result2 < 0) {
  3385. pr_err("%s: Voice_pause_voice_call failed for session 0x%x, err %d!\n",
  3386. __func__, v->session_id, result2);
  3387. result = result2;
  3388. }
  3389. if (cal_type == CVP_VOCPROC_DYNAMIC_CAL_TYPE)
  3390. voice_send_cvp_deregister_vol_cal_cmd(v);
  3391. else if (cal_type == CVP_VOCPROC_STATIC_CAL_TYPE)
  3392. voice_send_cvp_deregister_cal_cmd(v);
  3393. else if (cal_type == CVP_VOCDEV_CFG_CAL_TYPE)
  3394. voice_send_cvp_deregister_dev_cfg_cmd(v);
  3395. else if (cal_type == CVS_VOCSTRM_STATIC_CAL_TYPE)
  3396. voice_send_cvs_deregister_cal_cmd(v);
  3397. else
  3398. pr_err("%s: Invalid cal type %d!\n",
  3399. __func__, cal_type);
  3400. result2 = voice_send_start_voice_cmd(v);
  3401. if (result2) {
  3402. pr_err("%s: Voice_send_start_voice_cmd failed for session 0x%x, err %d!\n",
  3403. __func__, v->session_id, result2);
  3404. result = result2;
  3405. }
  3406. }
  3407. if ((cal_block->map_data.q6map_handle != 0) &&
  3408. (!is_other_session_active(v->session_id))) {
  3409. result2 = voice_send_mvm_unmap_memory_physical_cmd(
  3410. v, cal_block->map_data.q6map_handle);
  3411. if (result2) {
  3412. pr_err("%s: Voice_send_mvm_unmap_memory_physical_cmd failed for session 0x%x, err %d!\n",
  3413. __func__, v->session_id, result2);
  3414. result = result2;
  3415. }
  3416. cal_block->map_data.q6map_handle = 0;
  3417. }
  3418. mutex_unlock(&v->lock);
  3419. }
  3420. mutex_unlock(&common.common_lock);
  3421. done:
  3422. return result;
  3423. }
  3424. int voc_register_vocproc_vol_table(void)
  3425. {
  3426. int result = 0;
  3427. int result2 = 0;
  3428. int i;
  3429. struct voice_data *v = NULL;
  3430. pr_debug("%s\n", __func__);
  3431. mutex_lock(&common.common_lock);
  3432. for (i = 0; i < MAX_VOC_SESSIONS; i++) {
  3433. v = &common.voice[i];
  3434. mutex_lock(&v->lock);
  3435. if (is_voc_state_active(v->voc_state)) {
  3436. result2 = voice_send_cvp_register_vol_cal_cmd(v);
  3437. if (result2 < 0) {
  3438. pr_err("%s: Failed to register vocvol table for session 0x%x!\n",
  3439. __func__, v->session_id);
  3440. result = result2;
  3441. /* Still try to register other sessions */
  3442. }
  3443. }
  3444. mutex_unlock(&v->lock);
  3445. }
  3446. mutex_unlock(&common.common_lock);
  3447. return result;
  3448. }
  3449. int voc_deregister_vocproc_vol_table(void)
  3450. {
  3451. int result = 0;
  3452. int success = 0;
  3453. int i;
  3454. struct voice_data *v = NULL;
  3455. pr_debug("%s\n", __func__);
  3456. mutex_lock(&common.common_lock);
  3457. for (i = 0; i < MAX_VOC_SESSIONS; i++) {
  3458. v = &common.voice[i];
  3459. mutex_lock(&v->lock);
  3460. if (is_voc_state_active(v->voc_state)) {
  3461. result = voice_send_cvp_deregister_vol_cal_cmd(v);
  3462. if (result < 0) {
  3463. pr_err("%s: Failed to deregister vocvol table for session 0x%x!\n",
  3464. __func__, v->session_id);
  3465. mutex_unlock(&v->lock);
  3466. mutex_unlock(&common.common_lock);
  3467. if (success) {
  3468. pr_err("%s: Try to re-register all deregistered sessions!\n",
  3469. __func__);
  3470. voc_register_vocproc_vol_table();
  3471. }
  3472. goto done;
  3473. } else {
  3474. success = 1;
  3475. }
  3476. }
  3477. mutex_unlock(&v->lock);
  3478. }
  3479. mutex_unlock(&common.common_lock);
  3480. done:
  3481. return result;
  3482. }
  3483. int voc_map_rtac_block(struct rtac_cal_block_data *cal_block)
  3484. {
  3485. int result = 0;
  3486. struct voice_data *v = NULL;
  3487. pr_debug("%s\n", __func__);
  3488. if (cal_block == NULL) {
  3489. pr_err("%s: cal_block is NULL!\n",
  3490. __func__);
  3491. result = -EINVAL;
  3492. goto done;
  3493. }
  3494. if (cal_block->cal_data.paddr == 0) {
  3495. pr_debug("%s: No address to map!\n",
  3496. __func__);
  3497. result = -EINVAL;
  3498. goto done;
  3499. }
  3500. if (cal_block->map_data.map_size == 0) {
  3501. pr_debug("%s: map size is 0!\n",
  3502. __func__);
  3503. result = -EINVAL;
  3504. goto done;
  3505. }
  3506. mutex_lock(&common.common_lock);
  3507. /* use first session */
  3508. v = &common.voice[0];
  3509. mutex_lock(&v->lock);
  3510. if (!is_rtac_memory_allocated()) {
  3511. result = voice_alloc_rtac_mem_map_table();
  3512. if (result < 0) {
  3513. pr_err("%s: RTAC alloc mem map table did not work! addr = 0x%pK, size = %d\n",
  3514. __func__,
  3515. &cal_block->cal_data.paddr,
  3516. cal_block->map_data.map_size);
  3517. common.rtac_mem_map_table.dma_buf = NULL;
  3518. goto done_unlock;
  3519. }
  3520. }
  3521. result = voice_map_memory_physical_cmd(v,
  3522. &common.rtac_mem_map_table,
  3523. (dma_addr_t)cal_block->cal_data.paddr,
  3524. cal_block->map_data.map_size,
  3525. VOC_RTAC_MEM_MAP_TOKEN);
  3526. if (result < 0) {
  3527. pr_err("%s: RTAC mmap did not work! addr = 0x%pK, size = %d\n",
  3528. __func__,
  3529. &cal_block->cal_data.paddr,
  3530. cal_block->map_data.map_size);
  3531. free_rtac_map_table();
  3532. goto done_unlock;
  3533. }
  3534. cal_block->map_data.map_handle = common.rtac_mem_handle;
  3535. done_unlock:
  3536. mutex_unlock(&v->lock);
  3537. mutex_unlock(&common.common_lock);
  3538. done:
  3539. return result;
  3540. }
  3541. int voc_unmap_rtac_block(uint32_t *mem_map_handle)
  3542. {
  3543. int result = 0;
  3544. struct voice_data *v = NULL;
  3545. pr_debug("%s\n", __func__);
  3546. if (mem_map_handle == NULL) {
  3547. pr_debug("%s: Map handle is NULL, nothing to unmap\n",
  3548. __func__);
  3549. goto done;
  3550. }
  3551. if (*mem_map_handle == 0) {
  3552. pr_debug("%s: Map handle is 0, nothing to unmap\n",
  3553. __func__);
  3554. goto done;
  3555. }
  3556. mutex_lock(&common.common_lock);
  3557. /* Use first session */
  3558. /* Only used for apr wait lock */
  3559. v = &common.voice[0];
  3560. mutex_lock(&v->lock);
  3561. result = voice_send_mvm_unmap_memory_physical_cmd(
  3562. v, *mem_map_handle);
  3563. if (result) {
  3564. pr_err("%s: voice_send_mvm_unmap_memory_physical_cmd Failed for session 0x%x!\n",
  3565. __func__, v->session_id);
  3566. } else {
  3567. *mem_map_handle = 0;
  3568. common.rtac_mem_handle = 0;
  3569. free_rtac_map_table();
  3570. }
  3571. mutex_unlock(&v->lock);
  3572. mutex_unlock(&common.common_lock);
  3573. done:
  3574. return result;
  3575. }
  3576. static int voice_send_cvp_channel_info_v2(struct voice_data *v,
  3577. uint32_t param_type)
  3578. {
  3579. int ret;
  3580. struct cvp_set_channel_info_cmd_v2 cvp_set_channel_info_cmd;
  3581. void *apr_cvp;
  3582. u16 cvp_handle;
  3583. struct vss_icommon_param_data_channel_info_v2_t
  3584. *channel_info_param_data =
  3585. &cvp_set_channel_info_cmd.
  3586. cvp_set_ch_info_param_v2.param_data;
  3587. struct vss_param_vocproc_dev_channel_info_t *channel_info =
  3588. &channel_info_param_data->channel_info;
  3589. if (v == NULL) {
  3590. pr_err("%s: v is NULL\n", __func__);
  3591. ret = -EINVAL;
  3592. goto done;
  3593. }
  3594. apr_cvp = common.apr_q6_cvp;
  3595. if (!apr_cvp) {
  3596. pr_err("%s: apr_cvp is NULL\n", __func__);
  3597. ret = -EINVAL;
  3598. goto done;
  3599. }
  3600. cvp_handle = voice_get_cvp_handle(v);
  3601. memset(&cvp_set_channel_info_cmd, 0, sizeof(cvp_set_channel_info_cmd));
  3602. cvp_set_channel_info_cmd.hdr.hdr_field =
  3603. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  3604. APR_PKT_VER);
  3605. cvp_set_channel_info_cmd.hdr.pkt_size =
  3606. APR_PKT_SIZE(APR_HDR_SIZE,
  3607. sizeof(cvp_set_channel_info_cmd) - APR_HDR_SIZE);
  3608. cvp_set_channel_info_cmd.hdr.src_svc = 0;
  3609. cvp_set_channel_info_cmd.hdr.src_domain = APR_DOMAIN_APPS;
  3610. cvp_set_channel_info_cmd.hdr.src_port =
  3611. voice_get_idx_for_session(v->session_id);
  3612. cvp_set_channel_info_cmd.hdr.dest_svc = 0;
  3613. cvp_set_channel_info_cmd.hdr.dest_domain = APR_DOMAIN_ADSP;
  3614. cvp_set_channel_info_cmd.hdr.dest_port = cvp_handle;
  3615. cvp_set_channel_info_cmd.hdr.token = 0;
  3616. cvp_set_channel_info_cmd.hdr.opcode = VSS_ICOMMON_CMD_SET_PARAM_V2;
  3617. cvp_set_channel_info_cmd.cvp_set_ch_info_param_v2.mem_size =
  3618. sizeof(struct vss_icommon_param_data_channel_info_v2_t);
  3619. channel_info_param_data->module_id = VSS_MODULE_CVD_GENERIC;
  3620. channel_info_param_data->param_size =
  3621. sizeof(struct vss_param_vocproc_dev_channel_info_t);
  3622. /* Device specific data */
  3623. switch (param_type) {
  3624. case RX_PATH:
  3625. channel_info_param_data->param_id =
  3626. VSS_PARAM_VOCPROC_RX_CHANNEL_INFO;
  3627. channel_info->num_channels = v->dev_rx.no_of_channels;
  3628. channel_info->bits_per_sample = v->dev_rx.bits_per_sample;
  3629. memcpy(&channel_info->channel_mapping,
  3630. v->dev_rx.channel_mapping,
  3631. VSS_NUM_CHANNELS_MAX * sizeof(uint8_t));
  3632. break;
  3633. case TX_PATH:
  3634. channel_info_param_data->param_id =
  3635. VSS_PARAM_VOCPROC_TX_CHANNEL_INFO;
  3636. channel_info->num_channels = v->dev_tx.no_of_channels;
  3637. channel_info->bits_per_sample = v->dev_tx.bits_per_sample;
  3638. memcpy(&channel_info->channel_mapping,
  3639. v->dev_tx.channel_mapping,
  3640. VSS_NUM_CHANNELS_MAX * sizeof(uint8_t));
  3641. break;
  3642. case EC_REF_PATH:
  3643. channel_info_param_data->param_id =
  3644. VSS_PARAM_VOCPROC_EC_REF_CHANNEL_INFO;
  3645. channel_info->num_channels = v->dev_rx.no_of_channels;
  3646. channel_info->bits_per_sample = v->dev_rx.bits_per_sample;
  3647. memcpy(&channel_info->channel_mapping,
  3648. v->dev_rx.channel_mapping,
  3649. VSS_NUM_CHANNELS_MAX * sizeof(uint8_t));
  3650. break;
  3651. default:
  3652. pr_err("%s: Invalid param type\n",
  3653. __func__);
  3654. ret = -EINVAL;
  3655. goto done;
  3656. }
  3657. v->cvp_state = CMD_STATUS_FAIL;
  3658. v->async_err = 0;
  3659. ret = apr_send_pkt(apr_cvp, (uint32_t *) &cvp_set_channel_info_cmd);
  3660. if (ret < 0) {
  3661. pr_err("%s: Failed to send VSS_ICOMMON_CMD_SET_PARAM_V2\n",
  3662. __func__);
  3663. goto done;
  3664. }
  3665. ret = wait_event_timeout(v->cvp_wait,
  3666. (v->cvp_state == CMD_STATUS_SUCCESS),
  3667. msecs_to_jiffies(TIMEOUT_MS));
  3668. if (!ret) {
  3669. pr_err("%s: wait_event timeout\n", __func__);
  3670. ret = -ETIMEDOUT;
  3671. goto done;
  3672. }
  3673. if (v->async_err > 0) {
  3674. pr_err("%s: DSP returned error[%s] handle = %d\n", __func__,
  3675. adsp_err_get_err_str(v->async_err), cvp_handle);
  3676. ret = adsp_err_get_lnx_err_code(v->async_err);
  3677. goto done;
  3678. }
  3679. ret = 0;
  3680. done:
  3681. return ret;
  3682. }
  3683. static int voice_send_cvp_channel_info_cmd(struct voice_data *v)
  3684. {
  3685. int ret = 0;
  3686. ret = voice_send_cvp_channel_info_v2(v, RX_PATH);
  3687. if (ret < 0) {
  3688. pr_err("%s: Error in sending cvp_channel_info RX: %d\n",
  3689. __func__, ret);
  3690. goto done;
  3691. }
  3692. ret = voice_send_cvp_channel_info_v2(v, TX_PATH);
  3693. if (ret < 0) {
  3694. pr_err("%s: Error in sending cvp_channel_info TX: %d\n",
  3695. __func__, ret);
  3696. goto done;
  3697. }
  3698. ret = voice_send_cvp_channel_info_v2(v, EC_REF_PATH);
  3699. if (ret < 0) {
  3700. pr_err("%s: Error in sending cvp_channel_info EC Ref: %d\n",
  3701. __func__, ret);
  3702. goto done;
  3703. }
  3704. done:
  3705. return ret;
  3706. }
  3707. static int voice_send_cvp_ch_mixer_info_v2(struct voice_data *v)
  3708. {
  3709. int ret;
  3710. struct cvp_set_channel_mixer_info_cmd_v2 cvp_set_ch_mixer_info_cmd;
  3711. void *apr_cvp;
  3712. u16 cvp_handle;
  3713. struct vss_icommon_param_data_ch_mixer_v2_t *cvp_config_param_data =
  3714. &cvp_set_ch_mixer_info_cmd.
  3715. cvp_set_ch_mixer_param_v2.param_data;
  3716. struct vss_param_channel_mixer_info_t *ch_mixer_info =
  3717. &cvp_config_param_data->ch_mixer_info;
  3718. if (v == NULL) {
  3719. pr_err("%s: v is NULL\n", __func__);
  3720. ret = -EINVAL;
  3721. goto done;
  3722. }
  3723. apr_cvp = common.apr_q6_cvp;
  3724. if (!apr_cvp) {
  3725. pr_err("%s: apr_cvp is NULL\n", __func__);
  3726. ret = -EINVAL;
  3727. goto done;
  3728. }
  3729. cvp_handle = voice_get_cvp_handle(v);
  3730. memset(&cvp_set_ch_mixer_info_cmd, 0,
  3731. sizeof(cvp_set_ch_mixer_info_cmd));
  3732. cvp_set_ch_mixer_info_cmd.hdr.hdr_field =
  3733. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3734. APR_HDR_LEN(APR_HDR_SIZE),
  3735. APR_PKT_VER);
  3736. cvp_set_ch_mixer_info_cmd.hdr.pkt_size =
  3737. APR_PKT_SIZE(APR_HDR_SIZE,
  3738. sizeof(cvp_set_ch_mixer_info_cmd) - APR_HDR_SIZE);
  3739. cvp_set_ch_mixer_info_cmd.hdr.src_svc = 0;
  3740. cvp_set_ch_mixer_info_cmd.hdr.src_domain = APR_DOMAIN_APPS;
  3741. cvp_set_ch_mixer_info_cmd.hdr.src_port =
  3742. voice_get_idx_for_session(v->session_id);
  3743. cvp_set_ch_mixer_info_cmd.hdr.dest_svc = 0;
  3744. cvp_set_ch_mixer_info_cmd.hdr.dest_domain = APR_DOMAIN_ADSP;
  3745. cvp_set_ch_mixer_info_cmd.hdr.dest_port = cvp_handle;
  3746. cvp_set_ch_mixer_info_cmd.hdr.token = VOC_GENERIC_SET_PARAM_TOKEN;
  3747. cvp_set_ch_mixer_info_cmd.hdr.opcode = VSS_ICOMMON_CMD_SET_PARAM_V2;
  3748. cvp_set_ch_mixer_info_cmd.cvp_set_ch_mixer_param_v2.mem_size =
  3749. sizeof(struct vss_icommon_param_data_ch_mixer_v2_t);
  3750. cvp_config_param_data->module_id = AUDPROC_MODULE_ID_MFC;
  3751. cvp_config_param_data->param_id =
  3752. AUDPROC_CHMIXER_PARAM_ID_COEFF;
  3753. cvp_config_param_data->param_size =
  3754. sizeof(struct vss_param_channel_mixer_info_t);
  3755. ch_mixer_info->index = 0;
  3756. ch_mixer_info->num_output_channels = v->dev_rx.no_of_channels;
  3757. /*
  3758. * Configure Rx input to be mono for channel mixer as the DSP
  3759. * configures vocproc input as mono.
  3760. */
  3761. ch_mixer_info->num_input_channels = NUM_CHANNELS_MONO;
  3762. ch_mixer_info->out_channel_map[0] = PCM_CHANNEL_L;
  3763. ch_mixer_info->out_channel_map[1] = PCM_CHANNEL_R;
  3764. ch_mixer_info->in_channel_map[0] = PCM_CHANNEL_L;
  3765. ch_mixer_info->channel_weight_coeff[0][0] = GAIN_Q14_FORMAT(1);
  3766. ch_mixer_info->channel_weight_coeff[1][0] = GAIN_Q14_FORMAT(1);
  3767. ch_mixer_info->reserved = 0;
  3768. v->cvp_state = CMD_STATUS_FAIL;
  3769. v->async_err = 0;
  3770. ret = apr_send_pkt(apr_cvp, (uint32_t *)&cvp_set_ch_mixer_info_cmd);
  3771. if (ret < 0) {
  3772. pr_err("%s: Failed to send VSS_ICOMMON_CMD_SET_PARAM_V2 %d\n",
  3773. __func__, ret);
  3774. goto done;
  3775. }
  3776. ret = wait_event_timeout(v->cvp_wait,
  3777. (v->cvp_state == CMD_STATUS_SUCCESS),
  3778. msecs_to_jiffies(TIMEOUT_MS));
  3779. if (!ret) {
  3780. pr_err("%s: wait_event timeout\n", __func__);
  3781. ret = -ETIMEDOUT;
  3782. goto done;
  3783. }
  3784. if (v->async_err > 0) {
  3785. pr_err("%s: DSP returned error[%s] handle = %d\n", __func__,
  3786. adsp_err_get_err_str(v->async_err), cvp_handle);
  3787. ret = adsp_err_get_lnx_err_code(v->async_err);
  3788. goto done;
  3789. }
  3790. ret = 0;
  3791. done:
  3792. return ret;
  3793. }
  3794. static int voice_send_cvp_mfc_config_v2(struct voice_data *v)
  3795. {
  3796. int ret;
  3797. struct cvp_set_mfc_config_cmd_v2 cvp_set_mfc_config_cmd;
  3798. void *apr_cvp;
  3799. u16 cvp_handle;
  3800. uint8_t ch_idx;
  3801. struct vss_icommon_param_data_mfc_config_v2_t *cvp_config_param_data =
  3802. &cvp_set_mfc_config_cmd.cvp_set_mfc_param_v2.param_data;
  3803. struct vss_param_mfc_config_info_t *mfc_config_info =
  3804. &cvp_config_param_data->mfc_config_info;
  3805. if (v == NULL) {
  3806. pr_err("%s: v is NULL\n", __func__);
  3807. ret = -EINVAL;
  3808. goto done;
  3809. }
  3810. apr_cvp = common.apr_q6_cvp;
  3811. if (!apr_cvp) {
  3812. pr_err("%s: apr_cvp is NULL\n", __func__);
  3813. ret = -EINVAL;
  3814. goto done;
  3815. }
  3816. cvp_handle = voice_get_cvp_handle(v);
  3817. memset(&cvp_set_mfc_config_cmd, 0, sizeof(cvp_set_mfc_config_cmd));
  3818. cvp_set_mfc_config_cmd.hdr.hdr_field =
  3819. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  3820. APR_PKT_VER);
  3821. cvp_set_mfc_config_cmd.hdr.pkt_size =
  3822. APR_PKT_SIZE(APR_HDR_SIZE,
  3823. sizeof(cvp_set_mfc_config_cmd) - APR_HDR_SIZE);
  3824. cvp_set_mfc_config_cmd.hdr.src_svc = 0;
  3825. cvp_set_mfc_config_cmd.hdr.src_domain = APR_DOMAIN_APPS;
  3826. cvp_set_mfc_config_cmd.hdr.src_port =
  3827. voice_get_idx_for_session(v->session_id);
  3828. cvp_set_mfc_config_cmd.hdr.dest_svc = 0;
  3829. cvp_set_mfc_config_cmd.hdr.dest_domain = APR_DOMAIN_ADSP;
  3830. cvp_set_mfc_config_cmd.hdr.dest_port = cvp_handle;
  3831. cvp_set_mfc_config_cmd.hdr.token = 0;
  3832. cvp_set_mfc_config_cmd.hdr.opcode = VSS_ICOMMON_CMD_SET_PARAM_V2;
  3833. cvp_set_mfc_config_cmd.cvp_set_mfc_param_v2.mem_size =
  3834. sizeof(struct vss_icommon_param_data_mfc_config_v2_t);
  3835. cvp_config_param_data->module_id = AUDPROC_MODULE_ID_MFC;
  3836. cvp_config_param_data->param_id =
  3837. AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
  3838. cvp_config_param_data->param_size =
  3839. sizeof(struct vss_param_mfc_config_info_t);
  3840. mfc_config_info->num_channels = v->dev_rx.no_of_channels;
  3841. mfc_config_info->bits_per_sample = 16;
  3842. mfc_config_info->sample_rate = v->dev_rx.sample_rate;
  3843. /*
  3844. * Do not use memcpy here as channel_type in mfc_config structure is a
  3845. * uint16_t array while channel_mapping array of device is of uint8_t
  3846. */
  3847. for (ch_idx = 0; ch_idx < VSS_NUM_CHANNELS_MAX; ch_idx++) {
  3848. mfc_config_info->channel_type[ch_idx] =
  3849. v->dev_rx.channel_mapping[ch_idx];
  3850. }
  3851. v->cvp_state = CMD_STATUS_FAIL;
  3852. v->async_err = 0;
  3853. ret = apr_send_pkt(apr_cvp, (uint32_t *)&cvp_set_mfc_config_cmd);
  3854. if (ret < 0) {
  3855. pr_err("%s: Failed to send VSS_ICOMMON_CMD_SET_PARAM_V2 %d\n",
  3856. __func__, ret);
  3857. goto done;
  3858. }
  3859. ret = wait_event_timeout(v->cvp_wait,
  3860. (v->cvp_state == CMD_STATUS_SUCCESS),
  3861. msecs_to_jiffies(TIMEOUT_MS));
  3862. if (!ret) {
  3863. pr_err("%s: wait_event timeout\n", __func__);
  3864. ret = -ETIMEDOUT;
  3865. goto done;
  3866. }
  3867. if (v->async_err > 0) {
  3868. pr_err("%s: DSP returned error[%s] handle = %d\n", __func__,
  3869. adsp_err_get_err_str(v->async_err), cvp_handle);
  3870. ret = adsp_err_get_lnx_err_code(v->async_err);
  3871. goto done;
  3872. }
  3873. ret = 0;
  3874. done:
  3875. return ret;
  3876. }
  3877. static int voice_send_cvp_mfc_config_cmd(struct voice_data *v)
  3878. {
  3879. int ret = 0;
  3880. if (common.cvp_version >= CVP_VERSION_2) {
  3881. ret = voice_send_cvp_ch_mixer_info_v2(v);
  3882. if (ret < 0)
  3883. pr_warn("%s: Set channel mixer config failed err:%d",
  3884. __func__, ret);
  3885. ret = voice_send_cvp_mfc_config_v2(v);
  3886. if (ret < 0)
  3887. pr_warn("%s: Set MFC config failed err:%d",
  3888. __func__, ret);
  3889. } else {
  3890. pr_warn("%s: CVP Version not supported\n", __func__);
  3891. ret = -EINVAL;
  3892. }
  3893. return ret;
  3894. }
  3895. static int voice_get_avcs_version_per_service(uint32_t service_id)
  3896. {
  3897. int ret = 0;
  3898. size_t ver_size;
  3899. struct avcs_fwk_ver_info *ver_info = NULL;
  3900. if (service_id == AVCS_SERVICE_ID_ALL) {
  3901. pr_err("%s: Invalid service id: %d", __func__,
  3902. AVCS_SERVICE_ID_ALL);
  3903. return -EINVAL;
  3904. }
  3905. ver_size = sizeof(struct avcs_get_fwk_version) +
  3906. sizeof(struct avs_svc_api_info);
  3907. ver_info = kzalloc(ver_size, GFP_KERNEL);
  3908. if (ver_info == NULL)
  3909. return -ENOMEM;
  3910. ret = q6core_get_service_version(service_id, ver_info, ver_size);
  3911. if (ret < 0)
  3912. goto done;
  3913. ret = ver_info->services[0].api_version;
  3914. common.is_avcs_version_queried = true;
  3915. done:
  3916. kfree(ver_info);
  3917. return ret;
  3918. }
  3919. static void voice_mic_break_work_fn(struct work_struct *work)
  3920. {
  3921. int ret = 0;
  3922. char event[25] = "";
  3923. struct voice_data *v = container_of(work, struct voice_data,
  3924. voice_mic_break_work);
  3925. snprintf(event, sizeof(event), "MIC_BREAK_STATUS=%s",
  3926. v->mic_break_status ? "TRUE" : "FALSE");
  3927. mutex_lock(&common.common_lock);
  3928. ret = q6core_send_uevent(common.uevent_data, event);
  3929. if (ret)
  3930. pr_err("%s: Send UEvent %s failed :%d\n", __func__, event, ret);
  3931. mutex_unlock(&common.common_lock);
  3932. }
  3933. static int voice_setup_vocproc(struct voice_data *v)
  3934. {
  3935. struct module_instance_info mod_inst_info;
  3936. int ret = 0;
  3937. memset(&mod_inst_info, 0, sizeof(mod_inst_info));
  3938. ret = voice_send_cvp_create_cmd(v);
  3939. if (ret < 0) {
  3940. pr_err("%s: CVP create failed err:%d\n", __func__, ret);
  3941. goto fail;
  3942. }
  3943. if (common.is_avcs_version_queried == false)
  3944. common.cvp_version = voice_get_avcs_version_per_service(
  3945. APRV2_IDS_SERVICE_ID_ADSP_CVP_V);
  3946. if (common.cvp_version < 0) {
  3947. pr_err("%s: Invalid CVP version %d\n",
  3948. __func__, common.cvp_version);
  3949. ret = -EINVAL;
  3950. goto fail;
  3951. }
  3952. pr_debug("%s: CVP Version %d\n", __func__, common.cvp_version);
  3953. ret = voice_send_cvp_media_fmt_info_cmd(v);
  3954. if (ret < 0) {
  3955. pr_err("%s: Set media format info failed err:%d\n", __func__,
  3956. ret);
  3957. goto fail;
  3958. }
  3959. ret = voice_send_cvp_topology_commit_cmd(v);
  3960. if (ret < 0) {
  3961. pr_err("%s: Set topology commit failed err:%d\n",
  3962. __func__, ret);
  3963. goto fail;
  3964. }
  3965. /* Send MFC config only when the no of channels are more than 1 */
  3966. if (v->dev_rx.no_of_channels > NUM_CHANNELS_MONO) {
  3967. ret = voice_send_cvp_mfc_config_cmd(v);
  3968. if (ret < 0) {
  3969. pr_warn("%s: Set mfc config failed err:%d\n",
  3970. __func__, ret);
  3971. }
  3972. }
  3973. mod_inst_info.module_id = MODULE_ID_VOICE_MODULE_ST;
  3974. mod_inst_info.instance_id = INSTANCE_ID_0;
  3975. voice_send_cvs_register_cal_cmd(v);
  3976. voice_send_cvp_register_dev_cfg_cmd(v);
  3977. voice_send_cvp_register_cal_cmd(v);
  3978. voice_send_cvp_register_vol_cal_cmd(v);
  3979. /* enable vocproc */
  3980. ret = voice_send_enable_vocproc_cmd(v);
  3981. if (ret < 0)
  3982. goto fail;
  3983. /* attach vocproc */
  3984. ret = voice_send_attach_vocproc_cmd(v);
  3985. if (ret < 0)
  3986. goto fail;
  3987. /* send tty mode if tty device is used */
  3988. voice_send_tty_mode_cmd(v);
  3989. if (is_voip_session(v->session_id)) {
  3990. ret = voice_send_mvm_cal_network_cmd(v);
  3991. if (ret < 0)
  3992. pr_err("%s: voice_send_mvm_cal_network_cmd: %d\n",
  3993. __func__, ret);
  3994. ret = voice_send_mvm_media_type_cmd(v);
  3995. if (ret < 0)
  3996. pr_err("%s: voice_send_mvm_media_type_cmd: %d\n",
  3997. __func__, ret);
  3998. voice_send_netid_timing_cmd(v);
  3999. }
  4000. if (v->st_enable && !v->tty_mode)
  4001. voice_send_set_pp_enable_cmd(v, mod_inst_info, v->st_enable);
  4002. /* Start in-call music delivery if this feature is enabled */
  4003. if (v->music_info.play_enable)
  4004. voice_cvs_start_playback(v);
  4005. /* Start in-call recording if this feature is enabled */
  4006. if (v->rec_info.rec_enable)
  4007. voice_cvs_start_record(v, v->rec_info.rec_mode,
  4008. v->rec_info.port_id);
  4009. if (v->dtmf_rx_detect_en)
  4010. voice_send_dtmf_rx_detection_cmd(v, v->dtmf_rx_detect_en);
  4011. if (v->ecns_enable)
  4012. voice_send_cvp_ecns_enable_cmd(v, v->ecns_module_id,
  4013. v->ecns_enable);
  4014. if (v->hd_enable)
  4015. voice_send_hd_cmd(v, v->hd_enable);
  4016. if (common.mic_break_enable)
  4017. voice_send_mvm_event_class_cmd(v,
  4018. VSS_INOTIFY_CMD_LISTEN_FOR_EVENT_CLASS,
  4019. VSS_ICOMMON_EVENT_CLASS_VOICE_ACTIVITY_UPDATE);
  4020. rtac_add_voice(voice_get_cvs_handle(v),
  4021. voice_get_cvp_handle(v),
  4022. v->dev_rx.port_id, v->dev_tx.port_id,
  4023. v->dev_rx.dev_id, v->dev_tx.dev_id,
  4024. v->session_id);
  4025. return 0;
  4026. fail:
  4027. return ret;
  4028. }
  4029. static int voice_send_cvp_device_channels_cmd(struct voice_data *v)
  4030. {
  4031. int ret = 0;
  4032. struct cvp_set_dev_channels_cmd cvp_set_dev_channels_cmd;
  4033. void *apr_cvp;
  4034. u16 cvp_handle;
  4035. if (!(voice_get_cvd_int_version(common.cvd_version) >=
  4036. CVD_INT_VERSION_2_2)) {
  4037. pr_debug("%s CVD ver %s doesn't support send_device_channels cmd\n",
  4038. __func__, common.cvd_version);
  4039. goto done;
  4040. }
  4041. if (v == NULL) {
  4042. pr_err("%s: v is NULL\n", __func__);
  4043. ret = -EINVAL;
  4044. goto done;
  4045. }
  4046. apr_cvp = common.apr_q6_cvp;
  4047. if (!apr_cvp) {
  4048. pr_err("%s: apr_cvp is NULL.\n", __func__);
  4049. ret = -EINVAL;
  4050. goto done;
  4051. }
  4052. cvp_handle = voice_get_cvp_handle(v);
  4053. cvp_set_dev_channels_cmd.hdr.hdr_field =
  4054. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4055. APR_HDR_LEN(APR_HDR_SIZE),
  4056. APR_PKT_VER);
  4057. cvp_set_dev_channels_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  4058. sizeof(cvp_set_dev_channels_cmd) - APR_HDR_SIZE);
  4059. cvp_set_dev_channels_cmd.hdr.src_port =
  4060. voice_get_idx_for_session(v->session_id);
  4061. cvp_set_dev_channels_cmd.hdr.dest_port = cvp_handle;
  4062. cvp_set_dev_channels_cmd.hdr.token = 0;
  4063. cvp_set_dev_channels_cmd.hdr.opcode =
  4064. VSS_IVOCPROC_CMD_TOPOLOGY_SET_DEV_CHANNELS;
  4065. cvp_set_dev_channels_cmd.cvp_set_channels.rx_num_channels =
  4066. VSS_NUM_DEV_CHANNELS_1;
  4067. cvp_set_dev_channels_cmd.cvp_set_channels.tx_num_channels =
  4068. v->dev_tx.no_of_channels;
  4069. v->cvp_state = CMD_STATUS_FAIL;
  4070. v->async_err = 0;
  4071. ret = apr_send_pkt(apr_cvp, (uint32_t *) &cvp_set_dev_channels_cmd);
  4072. if (ret < 0) {
  4073. pr_err("%s: Fail in sending VSS_IVOCPROC_CMD_TOPOLOGY_SET_DEV_CHANNELS\n",
  4074. __func__);
  4075. ret = -EINVAL;
  4076. goto done;
  4077. }
  4078. ret = wait_event_timeout(v->cvp_wait,
  4079. (v->cvp_state == CMD_STATUS_SUCCESS),
  4080. msecs_to_jiffies(TIMEOUT_MS));
  4081. if (!ret) {
  4082. pr_err("%s: wait_event timeout\n", __func__);
  4083. ret = -EINVAL;
  4084. goto done;
  4085. }
  4086. if (v->async_err > 0) {
  4087. pr_err("%s: DSP returned error[%s]\n",
  4088. __func__, adsp_err_get_err_str(
  4089. v->async_err));
  4090. ret = adsp_err_get_lnx_err_code(
  4091. v->async_err);
  4092. goto done;
  4093. }
  4094. done:
  4095. return ret;
  4096. }
  4097. static int voice_send_cvp_media_fmt_info_cmd(struct voice_data *v)
  4098. {
  4099. int ret = 0;
  4100. if (common.cvp_version < CVP_VERSION_2)
  4101. ret = voice_send_cvp_device_channels_cmd(v);
  4102. else
  4103. ret = voice_send_cvp_channel_info_cmd(v);
  4104. if (ret < 0) {
  4105. pr_err("%s: Set channel info failed err: %d\n", __func__,
  4106. ret);
  4107. goto done;
  4108. }
  4109. if (voice_get_cvd_int_version(common.cvd_version) >=
  4110. CVD_INT_VERSION_2_3) {
  4111. ret = voice_send_cvp_media_format_cmd(v, RX_PATH);
  4112. if (ret < 0)
  4113. goto done;
  4114. ret = voice_send_cvp_media_format_cmd(v, TX_PATH);
  4115. if (ret < 0)
  4116. goto done;
  4117. if (common.ec_ref_ext)
  4118. ret = voice_send_cvp_media_format_cmd(v, EC_REF_PATH);
  4119. }
  4120. done:
  4121. return ret;
  4122. }
  4123. static int voice_send_cvp_media_format_cmd(struct voice_data *v,
  4124. uint32_t param_type)
  4125. {
  4126. struct vss_param_endpoint_media_format_info media_fmt_info;
  4127. struct param_hdr_v3 param_hdr;
  4128. int ret = 0;
  4129. memset(&media_fmt_info, 0, sizeof(media_fmt_info));
  4130. memset(&param_hdr, 0, sizeof(param_hdr));
  4131. if (v == NULL) {
  4132. pr_err("%s: v is NULL\n", __func__);
  4133. ret = -EINVAL;
  4134. goto done;
  4135. }
  4136. param_hdr.module_id = VSS_MODULE_CVD_GENERIC;
  4137. param_hdr.instance_id = INSTANCE_ID_0;
  4138. param_hdr.param_size = sizeof(media_fmt_info);
  4139. switch (param_type) {
  4140. case RX_PATH:
  4141. param_hdr.param_id = VSS_PARAM_RX_PORT_ENDPOINT_MEDIA_INFO;
  4142. media_fmt_info.port_id = v->dev_rx.port_id;
  4143. media_fmt_info.num_channels = v->dev_rx.no_of_channels;
  4144. media_fmt_info.bits_per_sample = v->dev_rx.bits_per_sample;
  4145. media_fmt_info.sample_rate = v->dev_rx.sample_rate;
  4146. memcpy(&media_fmt_info.channel_mapping,
  4147. &v->dev_rx.channel_mapping, VSS_CHANNEL_MAPPING_SIZE);
  4148. break;
  4149. case TX_PATH:
  4150. param_hdr.param_id = VSS_PARAM_TX_PORT_ENDPOINT_MEDIA_INFO;
  4151. media_fmt_info.port_id = v->dev_tx.port_id;
  4152. media_fmt_info.num_channels = v->dev_tx.no_of_channels;
  4153. media_fmt_info.bits_per_sample = v->dev_tx.bits_per_sample;
  4154. media_fmt_info.sample_rate = v->dev_tx.sample_rate;
  4155. memcpy(&media_fmt_info.channel_mapping,
  4156. &v->dev_tx.channel_mapping, VSS_CHANNEL_MAPPING_SIZE);
  4157. break;
  4158. case EC_REF_PATH:
  4159. param_hdr.param_id = VSS_PARAM_EC_REF_PORT_ENDPOINT_MEDIA_INFO;
  4160. media_fmt_info.port_id = common.ec_media_fmt_info.port_id;
  4161. media_fmt_info.num_channels =
  4162. common.ec_media_fmt_info.num_channels;
  4163. media_fmt_info.bits_per_sample =
  4164. common.ec_media_fmt_info.bits_per_sample;
  4165. media_fmt_info.sample_rate =
  4166. common.ec_media_fmt_info.sample_rate;
  4167. memcpy(&media_fmt_info.channel_mapping,
  4168. &common.ec_media_fmt_info.channel_mapping,
  4169. VSS_CHANNEL_MAPPING_SIZE);
  4170. break;
  4171. default:
  4172. pr_err("%s: Invalid param type %d\n", __func__, param_type);
  4173. ret = -EINVAL;
  4174. goto done;
  4175. }
  4176. ret = voice_pack_and_set_cvp_param(v, param_hdr,
  4177. (u8 *) &media_fmt_info);
  4178. if (ret)
  4179. pr_err("%s: Failed to set media format params on CVP, err %d\n",
  4180. __func__, ret);
  4181. done:
  4182. return ret;
  4183. }
  4184. static int voice_send_cvp_topology_commit_cmd(struct voice_data *v)
  4185. {
  4186. int ret = 0;
  4187. struct apr_hdr cvp_topology_commit_cmd;
  4188. void *apr_cvp;
  4189. u16 cvp_handle;
  4190. if (!(voice_get_cvd_int_version(common.cvd_version) >=
  4191. CVD_INT_VERSION_2_2)) {
  4192. pr_debug("%s CVD version string %s doesn't support this command\n",
  4193. __func__, common.cvd_version);
  4194. goto done;
  4195. }
  4196. if (v == NULL) {
  4197. pr_err("%s: v is NULL\n", __func__);
  4198. ret = -EINVAL;
  4199. goto done;
  4200. }
  4201. apr_cvp = common.apr_q6_cvp;
  4202. if (!apr_cvp) {
  4203. pr_err("%s: apr_cvp is NULL.\n", __func__);
  4204. ret = -EINVAL;
  4205. goto done;
  4206. }
  4207. cvp_handle = voice_get_cvp_handle(v);
  4208. cvp_topology_commit_cmd.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4209. APR_HDR_LEN(APR_HDR_SIZE),
  4210. APR_PKT_VER);
  4211. cvp_topology_commit_cmd.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  4212. sizeof(cvp_topology_commit_cmd) - APR_HDR_SIZE);
  4213. cvp_topology_commit_cmd.src_port =
  4214. voice_get_idx_for_session(v->session_id);
  4215. cvp_topology_commit_cmd.dest_port = cvp_handle;
  4216. cvp_topology_commit_cmd.token = 0;
  4217. cvp_topology_commit_cmd.opcode = VSS_IVOCPROC_CMD_TOPOLOGY_COMMIT;
  4218. v->cvp_state = CMD_STATUS_FAIL;
  4219. v->async_err = 0;
  4220. ret = apr_send_pkt(apr_cvp, (uint32_t *) &cvp_topology_commit_cmd);
  4221. if (ret < 0) {
  4222. pr_err("%s: Fail in sending VSS_IVOCPROC_CMD_TOPOLOGY_COMMIT\n",
  4223. __func__);
  4224. ret = -EINVAL;
  4225. goto done;
  4226. }
  4227. ret = wait_event_timeout(v->cvp_wait,
  4228. (v->cvp_state == CMD_STATUS_SUCCESS),
  4229. msecs_to_jiffies(TIMEOUT_MS));
  4230. if (!ret) {
  4231. pr_err("%s: wait_event timeout\n", __func__);
  4232. ret = -EINVAL;
  4233. goto done;
  4234. }
  4235. if (v->async_err > 0) {
  4236. pr_err("%s: DSP returned error[%s]\n",
  4237. __func__, adsp_err_get_err_str(
  4238. v->async_err));
  4239. ret = adsp_err_get_lnx_err_code(
  4240. v->async_err);
  4241. goto done;
  4242. }
  4243. done:
  4244. return ret;
  4245. }
  4246. static int voice_send_enable_vocproc_cmd(struct voice_data *v)
  4247. {
  4248. int ret = 0;
  4249. struct apr_hdr cvp_enable_cmd;
  4250. void *apr_cvp;
  4251. u16 cvp_handle;
  4252. if (v == NULL) {
  4253. pr_err("%s: v is NULL\n", __func__);
  4254. return -EINVAL;
  4255. }
  4256. apr_cvp = common.apr_q6_cvp;
  4257. if (!apr_cvp) {
  4258. pr_err("%s: apr_cvp is NULL.\n", __func__);
  4259. return -EINVAL;
  4260. }
  4261. cvp_handle = voice_get_cvp_handle(v);
  4262. /* enable vocproc and wait for respose */
  4263. cvp_enable_cmd.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4264. APR_HDR_LEN(APR_HDR_SIZE),
  4265. APR_PKT_VER);
  4266. cvp_enable_cmd.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  4267. sizeof(cvp_enable_cmd) - APR_HDR_SIZE);
  4268. pr_debug("cvp_enable_cmd pkt size = %d, cvp_handle=%d\n",
  4269. cvp_enable_cmd.pkt_size, cvp_handle);
  4270. cvp_enable_cmd.src_port =
  4271. voice_get_idx_for_session(v->session_id);
  4272. cvp_enable_cmd.dest_port = cvp_handle;
  4273. cvp_enable_cmd.token = 0;
  4274. cvp_enable_cmd.opcode = VSS_IVOCPROC_CMD_ENABLE;
  4275. v->cvp_state = CMD_STATUS_FAIL;
  4276. v->async_err = 0;
  4277. ret = apr_send_pkt(apr_cvp, (uint32_t *) &cvp_enable_cmd);
  4278. if (ret < 0) {
  4279. pr_err("Fail in sending VSS_IVOCPROC_CMD_ENABLE\n");
  4280. goto fail;
  4281. }
  4282. ret = wait_event_timeout(v->cvp_wait,
  4283. (v->cvp_state == CMD_STATUS_SUCCESS),
  4284. msecs_to_jiffies(TIMEOUT_MS));
  4285. if (!ret) {
  4286. pr_err("%s: wait_event timeout\n", __func__);
  4287. goto fail;
  4288. }
  4289. if (v->async_err > 0) {
  4290. pr_err("%s: DSP returned error[%s]\n",
  4291. __func__, adsp_err_get_err_str(
  4292. v->async_err));
  4293. ret = adsp_err_get_lnx_err_code(
  4294. v->async_err);
  4295. goto fail;
  4296. }
  4297. return 0;
  4298. fail:
  4299. return ret;
  4300. }
  4301. static int voice_send_mvm_cal_network_cmd(struct voice_data *v)
  4302. {
  4303. struct vss_imvm_cmd_set_cal_network_t mvm_set_cal_network;
  4304. int ret = 0;
  4305. void *apr_mvm;
  4306. u16 mvm_handle;
  4307. if (v == NULL) {
  4308. pr_err("%s: v is NULL\n", __func__);
  4309. return -EINVAL;
  4310. }
  4311. apr_mvm = common.apr_q6_mvm;
  4312. if (!apr_mvm) {
  4313. pr_err("%s: apr_mvm is NULL.\n", __func__);
  4314. return -EINVAL;
  4315. }
  4316. mvm_handle = voice_get_mvm_handle(v);
  4317. mvm_set_cal_network.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4318. APR_HDR_LEN(APR_HDR_SIZE),
  4319. APR_PKT_VER);
  4320. mvm_set_cal_network.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  4321. sizeof(mvm_set_cal_network) - APR_HDR_SIZE);
  4322. mvm_set_cal_network.hdr.src_port =
  4323. voice_get_idx_for_session(v->session_id);
  4324. mvm_set_cal_network.hdr.dest_port = mvm_handle;
  4325. mvm_set_cal_network.hdr.token = 0;
  4326. mvm_set_cal_network.hdr.opcode = VSS_IMVM_CMD_SET_CAL_NETWORK;
  4327. mvm_set_cal_network.network_id = VSS_ICOMMON_CAL_NETWORK_ID_NONE;
  4328. v->mvm_state = CMD_STATUS_FAIL;
  4329. v->async_err = 0;
  4330. ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_set_cal_network);
  4331. if (ret < 0) {
  4332. pr_err("%s: Error %d sending SET_NETWORK\n", __func__, ret);
  4333. goto fail;
  4334. }
  4335. ret = wait_event_timeout(v->mvm_wait,
  4336. (v->mvm_state == CMD_STATUS_SUCCESS),
  4337. msecs_to_jiffies(TIMEOUT_MS));
  4338. if (!ret) {
  4339. pr_err("%s: wait_event timeout %d\n", __func__, ret);
  4340. goto fail;
  4341. }
  4342. if (v->async_err > 0) {
  4343. pr_err("%s: DSP returned error[%s]\n",
  4344. __func__, adsp_err_get_err_str(
  4345. v->async_err));
  4346. ret = adsp_err_get_lnx_err_code(
  4347. v->async_err);
  4348. goto fail;
  4349. }
  4350. return 0;
  4351. fail:
  4352. return ret;
  4353. }
  4354. static int voice_send_netid_timing_cmd(struct voice_data *v)
  4355. {
  4356. int ret = 0;
  4357. void *apr_mvm;
  4358. u16 mvm_handle;
  4359. struct mvm_set_network_cmd mvm_set_network;
  4360. struct mvm_set_voice_timing_cmd mvm_set_voice_timing;
  4361. if (v == NULL) {
  4362. pr_err("%s: v is NULL\n", __func__);
  4363. return -EINVAL;
  4364. }
  4365. apr_mvm = common.apr_q6_mvm;
  4366. if (!apr_mvm) {
  4367. pr_err("%s: apr_mvm is NULL.\n", __func__);
  4368. return -EINVAL;
  4369. }
  4370. mvm_handle = voice_get_mvm_handle(v);
  4371. ret = voice_config_cvs_vocoder(v);
  4372. if (ret < 0) {
  4373. pr_err("%s: Error %d configuring CVS voc",
  4374. __func__, ret);
  4375. goto fail;
  4376. }
  4377. /* Set network ID. */
  4378. pr_debug("Setting network ID %x\n", common.mvs_info.network_type);
  4379. mvm_set_network.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4380. APR_HDR_LEN(APR_HDR_SIZE),
  4381. APR_PKT_VER);
  4382. mvm_set_network.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  4383. sizeof(mvm_set_network) - APR_HDR_SIZE);
  4384. mvm_set_network.hdr.src_port =
  4385. voice_get_idx_for_session(v->session_id);
  4386. mvm_set_network.hdr.dest_port = mvm_handle;
  4387. mvm_set_network.hdr.token = 0;
  4388. mvm_set_network.hdr.opcode = VSS_IMVM_CMD_SET_CAL_NETWORK;
  4389. mvm_set_network.network.network_id = common.mvs_info.network_type;
  4390. v->mvm_state = CMD_STATUS_FAIL;
  4391. v->async_err = 0;
  4392. ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_set_network);
  4393. if (ret < 0) {
  4394. pr_err("%s: Error %d sending SET_NETWORK\n", __func__, ret);
  4395. goto fail;
  4396. }
  4397. ret = wait_event_timeout(v->mvm_wait,
  4398. (v->mvm_state == CMD_STATUS_SUCCESS),
  4399. msecs_to_jiffies(TIMEOUT_MS));
  4400. if (!ret) {
  4401. pr_err("%s: wait_event timeout\n", __func__);
  4402. goto fail;
  4403. }
  4404. if (v->async_err > 0) {
  4405. pr_err("%s: DSP returned error[%s]\n",
  4406. __func__, adsp_err_get_err_str(
  4407. v->async_err));
  4408. ret = adsp_err_get_lnx_err_code(
  4409. v->async_err);
  4410. goto fail;
  4411. }
  4412. /* Set voice timing. */
  4413. pr_debug("Setting voice timing\n");
  4414. mvm_set_voice_timing.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4415. APR_HDR_LEN(APR_HDR_SIZE),
  4416. APR_PKT_VER);
  4417. mvm_set_voice_timing.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  4418. sizeof(mvm_set_voice_timing) -
  4419. APR_HDR_SIZE);
  4420. mvm_set_voice_timing.hdr.src_port =
  4421. voice_get_idx_for_session(v->session_id);
  4422. mvm_set_voice_timing.hdr.dest_port = mvm_handle;
  4423. mvm_set_voice_timing.hdr.token = 0;
  4424. mvm_set_voice_timing.hdr.opcode = VSS_ICOMMON_CMD_SET_VOICE_TIMING;
  4425. mvm_set_voice_timing.timing.mode = 0;
  4426. mvm_set_voice_timing.timing.enc_offset = 8000;
  4427. mvm_set_voice_timing.timing.dec_req_offset = 3300;
  4428. mvm_set_voice_timing.timing.dec_offset = 8300;
  4429. v->mvm_state = CMD_STATUS_FAIL;
  4430. v->async_err = 0;
  4431. ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_set_voice_timing);
  4432. if (ret < 0) {
  4433. pr_err("%s: Error %d sending SET_TIMING\n", __func__, ret);
  4434. goto fail;
  4435. }
  4436. ret = wait_event_timeout(v->mvm_wait,
  4437. (v->mvm_state == CMD_STATUS_SUCCESS),
  4438. msecs_to_jiffies(TIMEOUT_MS));
  4439. if (!ret) {
  4440. pr_err("%s: wait_event timeout\n", __func__);
  4441. goto fail;
  4442. }
  4443. if (v->async_err > 0) {
  4444. pr_err("%s: DSP returned error[%s]\n",
  4445. __func__, adsp_err_get_err_str(
  4446. v->async_err));
  4447. ret = adsp_err_get_lnx_err_code(
  4448. v->async_err);
  4449. goto fail;
  4450. }
  4451. return 0;
  4452. fail:
  4453. return ret;
  4454. }
  4455. static int voice_send_attach_vocproc_cmd(struct voice_data *v)
  4456. {
  4457. int ret = 0;
  4458. struct mvm_attach_vocproc_cmd mvm_a_vocproc_cmd;
  4459. void *apr_mvm;
  4460. u16 mvm_handle, cvp_handle;
  4461. if (v == NULL) {
  4462. pr_err("%s: v is NULL\n", __func__);
  4463. return -EINVAL;
  4464. }
  4465. apr_mvm = common.apr_q6_mvm;
  4466. if (!apr_mvm) {
  4467. pr_err("%s: apr_mvm is NULL.\n", __func__);
  4468. return -EINVAL;
  4469. }
  4470. mvm_handle = voice_get_mvm_handle(v);
  4471. cvp_handle = voice_get_cvp_handle(v);
  4472. /* attach vocproc and wait for response */
  4473. mvm_a_vocproc_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4474. APR_HDR_LEN(APR_HDR_SIZE),
  4475. APR_PKT_VER);
  4476. mvm_a_vocproc_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  4477. sizeof(mvm_a_vocproc_cmd) - APR_HDR_SIZE);
  4478. pr_debug("send mvm_a_vocproc_cmd pkt size = %d\n",
  4479. mvm_a_vocproc_cmd.hdr.pkt_size);
  4480. mvm_a_vocproc_cmd.hdr.src_port =
  4481. voice_get_idx_for_session(v->session_id);
  4482. mvm_a_vocproc_cmd.hdr.dest_port = mvm_handle;
  4483. mvm_a_vocproc_cmd.hdr.token = 0;
  4484. mvm_a_vocproc_cmd.hdr.opcode = VSS_IMVM_CMD_ATTACH_VOCPROC;
  4485. mvm_a_vocproc_cmd.mvm_attach_cvp_handle.handle = cvp_handle;
  4486. v->mvm_state = CMD_STATUS_FAIL;
  4487. v->async_err = 0;
  4488. ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_a_vocproc_cmd);
  4489. if (ret < 0) {
  4490. pr_err("Fail in sending VSS_IMVM_CMD_ATTACH_VOCPROC\n");
  4491. goto fail;
  4492. }
  4493. ret = wait_event_timeout(v->mvm_wait,
  4494. (v->mvm_state == CMD_STATUS_SUCCESS),
  4495. msecs_to_jiffies(TIMEOUT_MS));
  4496. if (!ret) {
  4497. pr_err("%s: wait_event timeout\n", __func__);
  4498. goto fail;
  4499. }
  4500. if (v->async_err > 0) {
  4501. pr_err("%s: DSP returned error[%s]\n",
  4502. __func__, adsp_err_get_err_str(
  4503. v->async_err));
  4504. ret = adsp_err_get_lnx_err_code(
  4505. v->async_err);
  4506. goto fail;
  4507. }
  4508. return 0;
  4509. fail:
  4510. return ret;
  4511. }
  4512. static void voc_update_session_params(struct voice_data *v)
  4513. {
  4514. /* reset LCH mode */
  4515. v->lch_mode = 0;
  4516. /* clear disable topology setting */
  4517. v->disable_topology = false;
  4518. /* clear mute setting */
  4519. v->dev_rx.dev_mute = common.default_mute_val;
  4520. v->dev_tx.dev_mute = common.default_mute_val;
  4521. v->stream_rx.stream_mute = common.default_mute_val;
  4522. v->stream_tx.stream_mute = common.default_mute_val;
  4523. }
  4524. static int voice_destroy_vocproc(struct voice_data *v)
  4525. {
  4526. struct mvm_detach_vocproc_cmd mvm_d_vocproc_cmd;
  4527. struct apr_hdr cvp_destroy_session_cmd;
  4528. struct module_instance_info mod_inst_info;
  4529. int ret = 0;
  4530. void *apr_mvm, *apr_cvp;
  4531. u16 mvm_handle, cvp_handle;
  4532. memset(&mod_inst_info, 0, sizeof(mod_inst_info));
  4533. if (v == NULL) {
  4534. pr_err("%s: v is NULL\n", __func__);
  4535. return -EINVAL;
  4536. }
  4537. apr_mvm = common.apr_q6_mvm;
  4538. apr_cvp = common.apr_q6_cvp;
  4539. if (!apr_mvm || !apr_cvp) {
  4540. pr_err("%s: apr_mvm or apr_cvp is NULL.\n", __func__);
  4541. return -EINVAL;
  4542. }
  4543. mvm_handle = voice_get_mvm_handle(v);
  4544. cvp_handle = voice_get_cvp_handle(v);
  4545. mod_inst_info.module_id = MODULE_ID_VOICE_MODULE_ST;
  4546. mod_inst_info.instance_id = INSTANCE_ID_0;
  4547. /* disable slowtalk if st_enable is set */
  4548. if (v->st_enable)
  4549. voice_send_set_pp_enable_cmd(v, mod_inst_info, 0);
  4550. /* Disable HD Voice if hd_enable is set */
  4551. if (v->hd_enable)
  4552. voice_send_hd_cmd(v, 0);
  4553. /* stop playback or recording */
  4554. v->music_info.force = 1;
  4555. voice_cvs_stop_playback(v);
  4556. voice_cvs_stop_record(v);
  4557. /* If voice call is active during VoLTE, SRVCC happens.
  4558. * Start recording on voice session if recording started during VoLTE.
  4559. */
  4560. if (is_volte_session(v->session_id) &&
  4561. ((common.voice[VOC_PATH_PASSIVE].voc_state == VOC_RUN) ||
  4562. (common.voice[VOC_PATH_PASSIVE].voc_state == VOC_CHANGE))) {
  4563. if (v->rec_info.rec_enable) {
  4564. voice_cvs_start_record(
  4565. &common.voice[VOC_PATH_PASSIVE],
  4566. v->rec_info.rec_mode,
  4567. v->rec_info.port_id);
  4568. common.srvcc_rec_flag = true;
  4569. pr_debug("%s: switch recording, srvcc_rec_flag %d\n",
  4570. __func__, common.srvcc_rec_flag);
  4571. }
  4572. }
  4573. /* send stop voice cmd */
  4574. voice_send_stop_voice_cmd(v);
  4575. /* send stop dtmf detecton cmd */
  4576. if (v->dtmf_rx_detect_en)
  4577. voice_send_dtmf_rx_detection_cmd(v, 0);
  4578. if (v->ecns_enable)
  4579. voice_send_cvp_ecns_enable_cmd(v, v->ecns_module_id, 0);
  4580. /* detach VOCPROC and wait for response from mvm */
  4581. mvm_d_vocproc_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4582. APR_HDR_LEN(APR_HDR_SIZE),
  4583. APR_PKT_VER);
  4584. mvm_d_vocproc_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  4585. sizeof(mvm_d_vocproc_cmd) - APR_HDR_SIZE);
  4586. pr_debug("mvm_d_vocproc_cmd pkt size = %d\n",
  4587. mvm_d_vocproc_cmd.hdr.pkt_size);
  4588. mvm_d_vocproc_cmd.hdr.src_port =
  4589. voice_get_idx_for_session(v->session_id);
  4590. mvm_d_vocproc_cmd.hdr.dest_port = mvm_handle;
  4591. mvm_d_vocproc_cmd.hdr.token = 0;
  4592. mvm_d_vocproc_cmd.hdr.opcode = VSS_IMVM_CMD_DETACH_VOCPROC;
  4593. mvm_d_vocproc_cmd.mvm_detach_cvp_handle.handle = cvp_handle;
  4594. v->mvm_state = CMD_STATUS_FAIL;
  4595. v->async_err = 0;
  4596. ret = apr_send_pkt(apr_mvm, (uint32_t *) &mvm_d_vocproc_cmd);
  4597. if (ret < 0) {
  4598. pr_err("Fail in sending VSS_IMVM_CMD_DETACH_VOCPROC\n");
  4599. goto fail;
  4600. }
  4601. ret = wait_event_timeout(v->mvm_wait,
  4602. (v->mvm_state == CMD_STATUS_SUCCESS),
  4603. msecs_to_jiffies(TIMEOUT_MS));
  4604. if (!ret) {
  4605. pr_err("%s: wait_event timeout\n", __func__);
  4606. goto fail;
  4607. }
  4608. if (v->async_err > 0) {
  4609. pr_err("%s: DSP returned error[%s]\n",
  4610. __func__, adsp_err_get_err_str(
  4611. v->async_err));
  4612. ret = adsp_err_get_lnx_err_code(
  4613. v->async_err);
  4614. goto fail;
  4615. }
  4616. voice_send_cvp_deregister_vol_cal_cmd(v);
  4617. voice_send_cvp_deregister_cal_cmd(v);
  4618. voice_send_cvp_deregister_dev_cfg_cmd(v);
  4619. voice_send_cvs_deregister_cal_cmd(v);
  4620. /* Unload topology modules */
  4621. voice_unload_topo_modules();
  4622. if (common.mic_break_enable)
  4623. voice_send_mvm_event_class_cmd(v,
  4624. VSS_INOTIFY_CMD_CANCEL_EVENT_CLASS,
  4625. VSS_ICOMMON_EVENT_CLASS_VOICE_ACTIVITY_UPDATE);
  4626. /* destrop cvp session */
  4627. cvp_destroy_session_cmd.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4628. APR_HDR_LEN(APR_HDR_SIZE),
  4629. APR_PKT_VER);
  4630. cvp_destroy_session_cmd.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  4631. sizeof(cvp_destroy_session_cmd) - APR_HDR_SIZE);
  4632. pr_debug("cvp_destroy_session_cmd pkt size = %d\n",
  4633. cvp_destroy_session_cmd.pkt_size);
  4634. cvp_destroy_session_cmd.src_port =
  4635. voice_get_idx_for_session(v->session_id);
  4636. cvp_destroy_session_cmd.dest_port = cvp_handle;
  4637. cvp_destroy_session_cmd.token = 0;
  4638. cvp_destroy_session_cmd.opcode = APRV2_IBASIC_CMD_DESTROY_SESSION;
  4639. v->cvp_state = CMD_STATUS_FAIL;
  4640. v->async_err = 0;
  4641. ret = apr_send_pkt(apr_cvp, (uint32_t *) &cvp_destroy_session_cmd);
  4642. if (ret < 0) {
  4643. pr_err("Fail in sending APRV2_IBASIC_CMD_DESTROY_SESSION\n");
  4644. goto fail;
  4645. }
  4646. ret = wait_event_timeout(v->cvp_wait,
  4647. (v->cvp_state == CMD_STATUS_SUCCESS),
  4648. msecs_to_jiffies(TIMEOUT_MS));
  4649. if (!ret) {
  4650. pr_err("%s: wait_event timeout\n", __func__);
  4651. goto fail;
  4652. }
  4653. if (v->async_err > 0) {
  4654. pr_err("%s: DSP returned error[%s]\n",
  4655. __func__, adsp_err_get_err_str(
  4656. v->async_err));
  4657. ret = adsp_err_get_lnx_err_code(
  4658. v->async_err);
  4659. goto fail;
  4660. }
  4661. rtac_remove_voice(voice_get_cvs_handle(v));
  4662. cvp_handle = 0;
  4663. voice_set_cvp_handle(v, cvp_handle);
  4664. return 0;
  4665. fail:
  4666. return ret;
  4667. }
  4668. static int voice_send_mvm_unmap_memory_physical_cmd(struct voice_data *v,
  4669. uint32_t mem_handle)
  4670. {
  4671. struct vss_imemory_cmd_unmap_t mem_unmap;
  4672. int ret = 0;
  4673. void *apr_mvm;
  4674. u16 mvm_handle;
  4675. if (v == NULL) {
  4676. pr_err("%s: v is NULL\n", __func__);
  4677. return -EINVAL;
  4678. }
  4679. apr_mvm = common.apr_q6_mvm;
  4680. if (!apr_mvm) {
  4681. pr_err("%s: apr_mvm is NULL.\n", __func__);
  4682. return -EINVAL;
  4683. }
  4684. mvm_handle = voice_get_mvm_handle(v);
  4685. mem_unmap.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4686. APR_HDR_LEN(APR_HDR_SIZE),
  4687. APR_PKT_VER);
  4688. mem_unmap.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  4689. sizeof(mem_unmap) - APR_HDR_SIZE);
  4690. mem_unmap.hdr.src_port =
  4691. voice_get_idx_for_session(v->session_id);
  4692. mem_unmap.hdr.dest_port = mvm_handle;
  4693. mem_unmap.hdr.token = 0;
  4694. mem_unmap.hdr.opcode = VSS_IMEMORY_CMD_UNMAP;
  4695. mem_unmap.mem_handle = mem_handle;
  4696. pr_debug("%s: mem_handle: 0x%x\n", __func__, mem_unmap.mem_handle);
  4697. v->mvm_state = CMD_STATUS_FAIL;
  4698. v->async_err = 0;
  4699. ret = apr_send_pkt(apr_mvm, (uint32_t *) &mem_unmap);
  4700. if (ret < 0) {
  4701. pr_err("mem_unmap op[0x%x]ret[%d]\n",
  4702. mem_unmap.hdr.opcode, ret);
  4703. goto fail;
  4704. }
  4705. ret = wait_event_timeout(v->mvm_wait,
  4706. (v->mvm_state == CMD_STATUS_SUCCESS),
  4707. msecs_to_jiffies(TIMEOUT_MS));
  4708. if (!ret) {
  4709. pr_err("%s: wait_event timeout %d\n", __func__, ret);
  4710. goto fail;
  4711. }
  4712. if (v->async_err > 0) {
  4713. pr_err("%s: DSP returned error[%s]\n",
  4714. __func__, adsp_err_get_err_str(
  4715. v->async_err));
  4716. ret = adsp_err_get_lnx_err_code(
  4717. v->async_err);
  4718. goto fail;
  4719. }
  4720. return 0;
  4721. fail:
  4722. return ret;
  4723. }
  4724. static int voice_send_cvs_packet_exchange_config_cmd(struct voice_data *v)
  4725. {
  4726. struct vss_istream_cmd_set_oob_packet_exchange_config_t
  4727. packet_exchange_config_pkt;
  4728. int ret = 0;
  4729. void *apr_cvs;
  4730. u16 cvs_handle;
  4731. if (v == NULL) {
  4732. pr_err("%s: v is NULL\n", __func__);
  4733. return -EINVAL;
  4734. }
  4735. apr_cvs = common.apr_q6_cvs;
  4736. if (!apr_cvs) {
  4737. pr_err("%s: apr_cvs is NULL.\n", __func__);
  4738. return -EINVAL;
  4739. }
  4740. cvs_handle = voice_get_cvs_handle(v);
  4741. packet_exchange_config_pkt.hdr.hdr_field = APR_HDR_FIELD(
  4742. APR_MSG_TYPE_SEQ_CMD,
  4743. APR_HDR_LEN(APR_HDR_SIZE),
  4744. APR_PKT_VER);
  4745. packet_exchange_config_pkt.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  4746. sizeof(packet_exchange_config_pkt) -
  4747. APR_HDR_SIZE);
  4748. packet_exchange_config_pkt.hdr.src_port =
  4749. voice_get_idx_for_session(v->session_id);
  4750. packet_exchange_config_pkt.hdr.dest_port = cvs_handle;
  4751. packet_exchange_config_pkt.hdr.token = 0;
  4752. packet_exchange_config_pkt.hdr.opcode =
  4753. VSS_ISTREAM_CMD_SET_OOB_PACKET_EXCHANGE_CONFIG;
  4754. packet_exchange_config_pkt.mem_handle = v->shmem_info.mem_handle;
  4755. /* dec buffer address */
  4756. packet_exchange_config_pkt.dec_buf_addr_lsw =
  4757. lower_32_bits(v->shmem_info.sh_buf.buf[0].phys);
  4758. packet_exchange_config_pkt.dec_buf_addr_msw =
  4759. msm_audio_populate_upper_32_bits(
  4760. v->shmem_info.sh_buf.buf[0].phys);
  4761. packet_exchange_config_pkt.dec_buf_size = 4096;
  4762. /* enc buffer address */
  4763. packet_exchange_config_pkt.enc_buf_addr_lsw =
  4764. lower_32_bits(v->shmem_info.sh_buf.buf[1].phys);
  4765. packet_exchange_config_pkt.enc_buf_addr_msw =
  4766. msm_audio_populate_upper_32_bits(
  4767. v->shmem_info.sh_buf.buf[1].phys);
  4768. packet_exchange_config_pkt.enc_buf_size = 4096;
  4769. pr_debug("%s: dec buf add: lsw %0x msw %0x, size %d, enc buf add: lsw %0x msw %0x, size %d\n",
  4770. __func__,
  4771. packet_exchange_config_pkt.dec_buf_addr_lsw,
  4772. packet_exchange_config_pkt.dec_buf_addr_msw,
  4773. packet_exchange_config_pkt.dec_buf_size,
  4774. packet_exchange_config_pkt.enc_buf_addr_lsw,
  4775. packet_exchange_config_pkt.enc_buf_addr_msw,
  4776. packet_exchange_config_pkt.enc_buf_size);
  4777. v->cvs_state = CMD_STATUS_FAIL;
  4778. v->async_err = 0;
  4779. ret = apr_send_pkt(apr_cvs, (uint32_t *) &packet_exchange_config_pkt);
  4780. if (ret < 0) {
  4781. pr_err("Failed to send packet exchange config cmd %d\n", ret);
  4782. goto fail;
  4783. }
  4784. ret = wait_event_timeout(v->cvs_wait,
  4785. (v->cvs_state == CMD_STATUS_SUCCESS),
  4786. msecs_to_jiffies(TIMEOUT_MS));
  4787. if (!ret)
  4788. pr_err("%s: wait_event timeout %d\n", __func__, ret);
  4789. if (v->async_err > 0) {
  4790. pr_err("%s: DSP returned error[%s]\n",
  4791. __func__, adsp_err_get_err_str(
  4792. v->async_err));
  4793. ret = adsp_err_get_lnx_err_code(
  4794. v->async_err);
  4795. goto fail;
  4796. }
  4797. return 0;
  4798. fail:
  4799. return ret;
  4800. }
  4801. static int voice_send_cvs_data_exchange_mode_cmd(struct voice_data *v)
  4802. {
  4803. struct vss_istream_cmd_set_packet_exchange_mode_t data_exchange_pkt;
  4804. int ret = 0;
  4805. void *apr_cvs;
  4806. u16 cvs_handle;
  4807. if (v == NULL) {
  4808. pr_err("%s: v is NULL\n", __func__);
  4809. return -EINVAL;
  4810. }
  4811. apr_cvs = common.apr_q6_cvs;
  4812. if (!apr_cvs) {
  4813. pr_err("%s: apr_cvs is NULL.\n", __func__);
  4814. return -EINVAL;
  4815. }
  4816. cvs_handle = voice_get_cvs_handle(v);
  4817. data_exchange_pkt.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4818. APR_HDR_LEN(APR_HDR_SIZE),
  4819. APR_PKT_VER);
  4820. data_exchange_pkt.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  4821. sizeof(data_exchange_pkt) - APR_HDR_SIZE);
  4822. data_exchange_pkt.hdr.src_port =
  4823. voice_get_idx_for_session(v->session_id);
  4824. data_exchange_pkt.hdr.dest_port = cvs_handle;
  4825. data_exchange_pkt.hdr.token = 0;
  4826. data_exchange_pkt.hdr.opcode = VSS_ISTREAM_CMD_SET_PACKET_EXCHANGE_MODE;
  4827. data_exchange_pkt.mode = VSS_ISTREAM_PACKET_EXCHANGE_MODE_OUT_OF_BAND;
  4828. v->cvs_state = CMD_STATUS_FAIL;
  4829. v->async_err = 0;
  4830. ret = apr_send_pkt(apr_cvs, (uint32_t *) &data_exchange_pkt);
  4831. if (ret < 0) {
  4832. pr_err("Failed to send data exchange mode %d\n", ret);
  4833. goto fail;
  4834. }
  4835. ret = wait_event_timeout(v->cvs_wait,
  4836. (v->cvs_state == CMD_STATUS_SUCCESS),
  4837. msecs_to_jiffies(TIMEOUT_MS));
  4838. if (!ret)
  4839. pr_err("%s: wait_event timeout %d\n", __func__, ret);
  4840. if (v->async_err > 0) {
  4841. pr_err("%s: DSP returned error[%s]\n",
  4842. __func__, adsp_err_get_err_str(
  4843. v->async_err));
  4844. ret = adsp_err_get_lnx_err_code(
  4845. v->async_err);
  4846. goto fail;
  4847. }
  4848. return 0;
  4849. fail:
  4850. return ret;
  4851. }
  4852. static int voice_send_stream_mute_cmd(struct voice_data *v, uint16_t direction,
  4853. uint16_t mute_flag, uint32_t ramp_duration)
  4854. {
  4855. struct cvs_set_mute_cmd cvs_mute_cmd;
  4856. int ret = 0;
  4857. if (v == NULL) {
  4858. pr_err("%s: v is NULL\n", __func__);
  4859. ret = -EINVAL;
  4860. goto fail;
  4861. }
  4862. if (!common.apr_q6_cvs) {
  4863. pr_err("%s: apr_cvs is NULL.\n", __func__);
  4864. ret = -EINVAL;
  4865. goto fail;
  4866. }
  4867. /* send mute/unmute to cvs */
  4868. cvs_mute_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4869. APR_HDR_LEN(APR_HDR_SIZE),
  4870. APR_PKT_VER);
  4871. cvs_mute_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  4872. sizeof(cvs_mute_cmd) - APR_HDR_SIZE);
  4873. cvs_mute_cmd.hdr.src_port =
  4874. voice_get_idx_for_session(v->session_id);
  4875. cvs_mute_cmd.hdr.dest_port = voice_get_cvs_handle(v);
  4876. cvs_mute_cmd.hdr.token = 0;
  4877. cvs_mute_cmd.hdr.opcode = VSS_IVOLUME_CMD_MUTE_V2;
  4878. cvs_mute_cmd.cvs_set_mute.direction = direction;
  4879. cvs_mute_cmd.cvs_set_mute.mute_flag = mute_flag;
  4880. cvs_mute_cmd.cvs_set_mute.ramp_duration_ms = ramp_duration;
  4881. v->cvs_state = CMD_STATUS_FAIL;
  4882. v->async_err = 0;
  4883. ret = apr_send_pkt(common.apr_q6_cvs, (uint32_t *) &cvs_mute_cmd);
  4884. if (ret < 0) {
  4885. pr_err("%s: Error %d sending stream mute\n", __func__, ret);
  4886. goto fail;
  4887. }
  4888. ret = wait_event_timeout(v->cvs_wait,
  4889. (v->cvs_state == CMD_STATUS_SUCCESS),
  4890. msecs_to_jiffies(TIMEOUT_MS));
  4891. if (!ret) {
  4892. pr_err("%s: Command timeout\n", __func__);
  4893. goto fail;
  4894. }
  4895. if (v->async_err > 0) {
  4896. pr_err("%s: DSP returned error[%s]\n",
  4897. __func__, adsp_err_get_err_str(
  4898. v->async_err));
  4899. ret = adsp_err_get_lnx_err_code(
  4900. v->async_err);
  4901. goto fail;
  4902. }
  4903. return 0;
  4904. fail:
  4905. return ret;
  4906. }
  4907. static int voice_send_device_mute_cmd(struct voice_data *v, uint16_t direction,
  4908. uint16_t mute_flag, uint32_t ramp_duration)
  4909. {
  4910. struct cvp_set_mute_cmd cvp_mute_cmd;
  4911. int ret = 0;
  4912. if (v == NULL) {
  4913. pr_err("%s: v is NULL\n", __func__);
  4914. ret = -EINVAL;
  4915. goto fail;
  4916. }
  4917. if (!common.apr_q6_cvp) {
  4918. pr_err("%s: apr_cvp is NULL.\n", __func__);
  4919. ret = -EINVAL;
  4920. goto fail;
  4921. }
  4922. cvp_mute_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4923. APR_HDR_LEN(APR_HDR_SIZE),
  4924. APR_PKT_VER);
  4925. cvp_mute_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  4926. sizeof(cvp_mute_cmd) - APR_HDR_SIZE);
  4927. cvp_mute_cmd.hdr.src_port =
  4928. voice_get_idx_for_session(v->session_id);
  4929. cvp_mute_cmd.hdr.dest_port = voice_get_cvp_handle(v);
  4930. cvp_mute_cmd.hdr.token = 0;
  4931. cvp_mute_cmd.hdr.opcode = VSS_IVOLUME_CMD_MUTE_V2;
  4932. cvp_mute_cmd.cvp_set_mute.direction = direction;
  4933. cvp_mute_cmd.cvp_set_mute.mute_flag = mute_flag;
  4934. cvp_mute_cmd.cvp_set_mute.ramp_duration_ms = ramp_duration;
  4935. v->cvp_state = CMD_STATUS_FAIL;
  4936. v->async_err = 0;
  4937. ret = apr_send_pkt(common.apr_q6_cvp, (uint32_t *) &cvp_mute_cmd);
  4938. if (ret < 0) {
  4939. pr_err("%s: Error %d sending rx device cmd\n", __func__, ret);
  4940. goto fail;
  4941. }
  4942. ret = wait_event_timeout(v->cvp_wait,
  4943. (v->cvp_state == CMD_STATUS_SUCCESS),
  4944. msecs_to_jiffies(TIMEOUT_MS));
  4945. if (!ret) {
  4946. pr_err("%s: Command timeout\n", __func__);
  4947. goto fail;
  4948. }
  4949. if (v->async_err > 0) {
  4950. pr_err("%s: DSP returned error[%s]\n",
  4951. __func__, adsp_err_get_err_str(
  4952. v->async_err));
  4953. ret = adsp_err_get_lnx_err_code(
  4954. v->async_err);
  4955. goto fail;
  4956. }
  4957. return 0;
  4958. fail:
  4959. return ret;
  4960. }
  4961. static int voice_send_vol_step_cmd(struct voice_data *v)
  4962. {
  4963. struct cvp_set_rx_volume_step_cmd cvp_vol_step_cmd;
  4964. int ret = 0;
  4965. void *apr_cvp;
  4966. u16 cvp_handle;
  4967. if (v == NULL) {
  4968. pr_err("%s: v is NULL\n", __func__);
  4969. return -EINVAL;
  4970. }
  4971. apr_cvp = common.apr_q6_cvp;
  4972. if (!apr_cvp) {
  4973. pr_err("%s: apr_cvp is NULL.\n", __func__);
  4974. return -EINVAL;
  4975. }
  4976. cvp_handle = voice_get_cvp_handle(v);
  4977. /* send volume index to cvp */
  4978. cvp_vol_step_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4979. APR_HDR_LEN(APR_HDR_SIZE),
  4980. APR_PKT_VER);
  4981. cvp_vol_step_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  4982. sizeof(cvp_vol_step_cmd) - APR_HDR_SIZE);
  4983. cvp_vol_step_cmd.hdr.src_port =
  4984. voice_get_idx_for_session(v->session_id);
  4985. cvp_vol_step_cmd.hdr.dest_port = cvp_handle;
  4986. cvp_vol_step_cmd.hdr.token = 0;
  4987. cvp_vol_step_cmd.hdr.opcode = VSS_IVOLUME_CMD_SET_STEP;
  4988. cvp_vol_step_cmd.cvp_set_vol_step.direction = VSS_IVOLUME_DIRECTION_RX;
  4989. cvp_vol_step_cmd.cvp_set_vol_step.value = v->dev_rx.volume_step_value;
  4990. cvp_vol_step_cmd.cvp_set_vol_step.ramp_duration_ms =
  4991. v->dev_rx.volume_ramp_duration_ms;
  4992. pr_debug("%s step_value:%d, ramp_duration_ms:%d",
  4993. __func__,
  4994. cvp_vol_step_cmd.cvp_set_vol_step.value,
  4995. cvp_vol_step_cmd.cvp_set_vol_step.ramp_duration_ms);
  4996. v->cvp_state = CMD_STATUS_FAIL;
  4997. v->async_err = 0;
  4998. ret = apr_send_pkt(apr_cvp, (uint32_t *) &cvp_vol_step_cmd);
  4999. if (ret < 0) {
  5000. pr_err("Fail in sending RX VOL step\n");
  5001. return -EINVAL;
  5002. }
  5003. ret = wait_event_timeout(v->cvp_wait,
  5004. (v->cvp_state == CMD_STATUS_SUCCESS),
  5005. msecs_to_jiffies(TIMEOUT_MS));
  5006. if (!ret) {
  5007. pr_err("%s: wait_event timeout\n", __func__);
  5008. return -EINVAL;
  5009. }
  5010. if (v->async_err > 0) {
  5011. pr_err("%s: DSP returned error[%s]\n",
  5012. __func__, adsp_err_get_err_str(
  5013. v->async_err));
  5014. ret = adsp_err_get_lnx_err_code(
  5015. v->async_err);
  5016. return ret;
  5017. }
  5018. return 0;
  5019. }
  5020. static int voice_cvs_start_record(struct voice_data *v, uint32_t rec_mode,
  5021. uint32_t port_id)
  5022. {
  5023. int ret = 0;
  5024. void *apr_cvs;
  5025. u16 cvs_handle;
  5026. struct cvs_start_record_cmd cvs_start_record;
  5027. if (v == NULL) {
  5028. pr_err("%s: v is NULL\n", __func__);
  5029. return -EINVAL;
  5030. }
  5031. apr_cvs = common.apr_q6_cvs;
  5032. if (!apr_cvs) {
  5033. pr_err("%s: apr_cvs is NULL.\n", __func__);
  5034. return -EINVAL;
  5035. }
  5036. cvs_handle = voice_get_cvs_handle(v);
  5037. if (!v->rec_info.recording) {
  5038. cvs_start_record.hdr.hdr_field = APR_HDR_FIELD(
  5039. APR_MSG_TYPE_SEQ_CMD,
  5040. APR_HDR_LEN(APR_HDR_SIZE),
  5041. APR_PKT_VER);
  5042. cvs_start_record.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  5043. sizeof(cvs_start_record) - APR_HDR_SIZE);
  5044. cvs_start_record.hdr.src_port =
  5045. voice_get_idx_for_session(v->session_id);
  5046. cvs_start_record.hdr.dest_port = cvs_handle;
  5047. cvs_start_record.hdr.token = 0;
  5048. cvs_start_record.hdr.opcode = VSS_IRECORD_CMD_START;
  5049. cvs_start_record.rec_mode.port_id =
  5050. VSS_IRECORD_PORT_ID_DEFAULT;
  5051. if (rec_mode == VOC_REC_UPLINK) {
  5052. cvs_start_record.rec_mode.rx_tap_point =
  5053. VSS_IRECORD_TAP_POINT_NONE;
  5054. cvs_start_record.rec_mode.tx_tap_point =
  5055. VSS_IRECORD_TAP_POINT_STREAM_END;
  5056. } else if (rec_mode == VOC_REC_DOWNLINK) {
  5057. cvs_start_record.rec_mode.rx_tap_point =
  5058. VSS_IRECORD_TAP_POINT_STREAM_END;
  5059. cvs_start_record.rec_mode.tx_tap_point =
  5060. VSS_IRECORD_TAP_POINT_NONE;
  5061. } else if (rec_mode == VOC_REC_BOTH) {
  5062. cvs_start_record.rec_mode.rx_tap_point =
  5063. VSS_IRECORD_TAP_POINT_STREAM_END;
  5064. cvs_start_record.rec_mode.tx_tap_point =
  5065. VSS_IRECORD_TAP_POINT_STREAM_END;
  5066. if (common.rec_channel_count ==
  5067. NUM_CHANNELS_STEREO) {
  5068. /*
  5069. * if channel count is not stereo,
  5070. * then default port_id and mode
  5071. * (mono) will be used
  5072. */
  5073. cvs_start_record.rec_mode.mode =
  5074. VSS_IRECORD_MODE_TX_RX_STEREO;
  5075. cvs_start_record.rec_mode.port_id =
  5076. port_id;
  5077. }
  5078. } else {
  5079. pr_err("%s: Invalid in-call rec_mode %d\n", __func__,
  5080. rec_mode);
  5081. ret = -EINVAL;
  5082. goto fail;
  5083. }
  5084. v->cvs_state = CMD_STATUS_FAIL;
  5085. v->async_err = 0;
  5086. ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_start_record);
  5087. if (ret < 0) {
  5088. pr_err("%s: Error %d sending START_RECORD\n", __func__,
  5089. ret);
  5090. goto fail;
  5091. }
  5092. ret = wait_event_timeout(v->cvs_wait,
  5093. (v->cvs_state == CMD_STATUS_SUCCESS),
  5094. msecs_to_jiffies(TIMEOUT_MS));
  5095. if (!ret) {
  5096. pr_err("%s: wait_event timeout\n", __func__);
  5097. goto fail;
  5098. }
  5099. if (v->async_err > 0) {
  5100. pr_err("%s: DSP returned error[%s]\n",
  5101. __func__, adsp_err_get_err_str(
  5102. v->async_err));
  5103. ret = adsp_err_get_lnx_err_code(
  5104. v->async_err);
  5105. goto fail;
  5106. }
  5107. v->rec_info.recording = 1;
  5108. } else {
  5109. pr_debug("%s: Start record already sent\n", __func__);
  5110. }
  5111. return 0;
  5112. fail:
  5113. return ret;
  5114. }
  5115. static int voice_cvs_stop_record(struct voice_data *v)
  5116. {
  5117. int ret = 0;
  5118. void *apr_cvs;
  5119. u16 cvs_handle;
  5120. struct apr_hdr cvs_stop_record;
  5121. if (v == NULL) {
  5122. pr_err("%s: v is NULL\n", __func__);
  5123. return -EINVAL;
  5124. }
  5125. apr_cvs = common.apr_q6_cvs;
  5126. if (!apr_cvs) {
  5127. pr_err("%s: apr_cvs is NULL.\n", __func__);
  5128. return -EINVAL;
  5129. }
  5130. cvs_handle = voice_get_cvs_handle(v);
  5131. if (v->rec_info.recording) {
  5132. cvs_stop_record.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5133. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5134. cvs_stop_record.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  5135. sizeof(cvs_stop_record) - APR_HDR_SIZE);
  5136. cvs_stop_record.src_port =
  5137. voice_get_idx_for_session(v->session_id);
  5138. cvs_stop_record.dest_port = cvs_handle;
  5139. cvs_stop_record.token = 0;
  5140. cvs_stop_record.opcode = VSS_IRECORD_CMD_STOP;
  5141. v->cvs_state = CMD_STATUS_FAIL;
  5142. v->async_err = 0;
  5143. ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_stop_record);
  5144. if (ret < 0) {
  5145. pr_err("%s: Error %d sending STOP_RECORD\n",
  5146. __func__, ret);
  5147. goto fail;
  5148. }
  5149. ret = wait_event_timeout(v->cvs_wait,
  5150. (v->cvs_state == CMD_STATUS_SUCCESS),
  5151. msecs_to_jiffies(TIMEOUT_MS));
  5152. if (!ret) {
  5153. pr_err("%s: wait_event timeout\n", __func__);
  5154. goto fail;
  5155. }
  5156. if (v->async_err > 0) {
  5157. pr_err("%s: DSP returned error[%s]\n",
  5158. __func__, adsp_err_get_err_str(
  5159. v->async_err));
  5160. ret = adsp_err_get_lnx_err_code(
  5161. v->async_err);
  5162. goto fail;
  5163. }
  5164. v->rec_info.recording = 0;
  5165. } else {
  5166. pr_debug("%s: Stop record already sent\n", __func__);
  5167. }
  5168. return 0;
  5169. fail:
  5170. return ret;
  5171. }
  5172. /**
  5173. * voc_start_record -
  5174. * command to set record for voice session
  5175. *
  5176. * @port_id: Pseudo Port ID for record data
  5177. * @set: Enable or Disable for record start/stop
  5178. * @session_id: voice session ID to send this command
  5179. *
  5180. * Returns 0 on success or error on failure
  5181. */
  5182. int voc_start_record(uint32_t port_id, uint32_t set, uint32_t session_id)
  5183. {
  5184. int ret = 0;
  5185. int rec_mode = 0;
  5186. u16 cvs_handle;
  5187. int rec_set = 0;
  5188. struct voice_session_itr itr;
  5189. struct voice_data *v = NULL;
  5190. /* check if session_id is valid */
  5191. if (!voice_is_valid_session_id(session_id)) {
  5192. pr_err("%s: Invalid session id:%u\n", __func__,
  5193. session_id);
  5194. return -EINVAL;
  5195. }
  5196. voice_itr_init(&itr, session_id);
  5197. pr_debug("%s: session_id:%u\n", __func__, session_id);
  5198. while (voice_itr_get_next_session(&itr, &v)) {
  5199. if (v == NULL) {
  5200. pr_err("%s: v is NULL, sessionid:%u\n", __func__,
  5201. session_id);
  5202. break;
  5203. }
  5204. pr_debug("%s: port_id: %d, set: %d, v: %pK\n",
  5205. __func__, port_id, set, v);
  5206. mutex_lock(&v->lock);
  5207. rec_mode = v->rec_info.rec_mode;
  5208. v->rec_info.port_id = port_id;
  5209. rec_set = set;
  5210. if (set) {
  5211. if ((v->rec_route_state.ul_flag != 0) &&
  5212. (v->rec_route_state.dl_flag != 0)) {
  5213. pr_debug("%s: rec mode already set.\n",
  5214. __func__);
  5215. mutex_unlock(&v->lock);
  5216. continue;
  5217. }
  5218. if (port_id == VOICE_RECORD_TX) {
  5219. if ((v->rec_route_state.ul_flag == 0)
  5220. && (v->rec_route_state.dl_flag == 0)) {
  5221. rec_mode = VOC_REC_UPLINK;
  5222. v->rec_route_state.ul_flag = 1;
  5223. } else if ((v->rec_route_state.ul_flag == 0)
  5224. && (v->rec_route_state.dl_flag != 0)) {
  5225. voice_cvs_stop_record(v);
  5226. rec_mode = VOC_REC_BOTH;
  5227. v->rec_route_state.ul_flag = 1;
  5228. }
  5229. } else if (port_id == VOICE_RECORD_RX) {
  5230. if ((v->rec_route_state.ul_flag == 0)
  5231. && (v->rec_route_state.dl_flag == 0)) {
  5232. rec_mode = VOC_REC_DOWNLINK;
  5233. v->rec_route_state.dl_flag = 1;
  5234. } else if ((v->rec_route_state.ul_flag != 0)
  5235. && (v->rec_route_state.dl_flag == 0)) {
  5236. voice_cvs_stop_record(v);
  5237. rec_mode = VOC_REC_BOTH;
  5238. v->rec_route_state.dl_flag = 1;
  5239. }
  5240. }
  5241. rec_set = 1;
  5242. } else {
  5243. if ((v->rec_route_state.ul_flag == 0) &&
  5244. (v->rec_route_state.dl_flag == 0)) {
  5245. pr_debug("%s: rec already stops.\n",
  5246. __func__);
  5247. mutex_unlock(&v->lock);
  5248. continue;
  5249. }
  5250. if (port_id == VOICE_RECORD_TX) {
  5251. if ((v->rec_route_state.ul_flag != 0)
  5252. && (v->rec_route_state.dl_flag == 0)) {
  5253. v->rec_route_state.ul_flag = 0;
  5254. rec_set = 0;
  5255. } else if ((v->rec_route_state.ul_flag != 0)
  5256. && (v->rec_route_state.dl_flag != 0)) {
  5257. voice_cvs_stop_record(v);
  5258. v->rec_route_state.ul_flag = 0;
  5259. rec_mode = VOC_REC_DOWNLINK;
  5260. rec_set = 1;
  5261. }
  5262. } else if (port_id == VOICE_RECORD_RX) {
  5263. if ((v->rec_route_state.ul_flag == 0)
  5264. && (v->rec_route_state.dl_flag != 0)) {
  5265. v->rec_route_state.dl_flag = 0;
  5266. rec_set = 0;
  5267. } else if ((v->rec_route_state.ul_flag != 0)
  5268. && (v->rec_route_state.dl_flag != 0)) {
  5269. voice_cvs_stop_record(v);
  5270. v->rec_route_state.dl_flag = 0;
  5271. rec_mode = VOC_REC_UPLINK;
  5272. rec_set = 1;
  5273. }
  5274. }
  5275. }
  5276. pr_debug("%s: mode =%d, set =%d\n", __func__,
  5277. rec_mode, rec_set);
  5278. cvs_handle = voice_get_cvs_handle(v);
  5279. if (cvs_handle != 0) {
  5280. if (rec_set)
  5281. ret = voice_cvs_start_record(v, rec_mode,
  5282. port_id);
  5283. else
  5284. ret = voice_cvs_stop_record(v);
  5285. }
  5286. /* During SRVCC, recording will switch from VoLTE session to
  5287. * voice session.
  5288. * Then stop recording, need to stop recording on voice session.
  5289. */
  5290. if ((!rec_set) && common.srvcc_rec_flag) {
  5291. pr_debug("%s, srvcc_rec_flag:%d\n", __func__,
  5292. common.srvcc_rec_flag);
  5293. voice_cvs_stop_record(&common.voice[VOC_PATH_PASSIVE]);
  5294. common.srvcc_rec_flag = false;
  5295. }
  5296. /* Cache the value */
  5297. v->rec_info.rec_enable = rec_set;
  5298. v->rec_info.rec_mode = rec_mode;
  5299. mutex_unlock(&v->lock);
  5300. }
  5301. return ret;
  5302. }
  5303. EXPORT_SYMBOL(voc_start_record);
  5304. static int voice_cvs_start_playback(struct voice_data *v)
  5305. {
  5306. int ret = 0;
  5307. struct cvs_start_playback_cmd cvs_start_playback;
  5308. void *apr_cvs;
  5309. u16 cvs_handle;
  5310. if (v == NULL) {
  5311. pr_err("%s: v is NULL\n", __func__);
  5312. return -EINVAL;
  5313. }
  5314. apr_cvs = common.apr_q6_cvs;
  5315. if (!apr_cvs) {
  5316. pr_err("%s: apr_cvs is NULL.\n", __func__);
  5317. return -EINVAL;
  5318. }
  5319. cvs_handle = voice_get_cvs_handle(v);
  5320. if (!v->music_info.playing && v->music_info.count) {
  5321. cvs_start_playback.hdr.hdr_field = APR_HDR_FIELD(
  5322. APR_MSG_TYPE_SEQ_CMD,
  5323. APR_HDR_LEN(APR_HDR_SIZE),
  5324. APR_PKT_VER);
  5325. cvs_start_playback.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  5326. sizeof(cvs_start_playback) - APR_HDR_SIZE);
  5327. cvs_start_playback.hdr.src_port =
  5328. voice_get_idx_for_session(v->session_id);
  5329. cvs_start_playback.hdr.dest_port = cvs_handle;
  5330. cvs_start_playback.hdr.token = 0;
  5331. cvs_start_playback.hdr.opcode = VSS_IPLAYBACK_CMD_START;
  5332. cvs_start_playback.playback_mode.port_id =
  5333. v->music_info.port_id;
  5334. v->cvs_state = CMD_STATUS_FAIL;
  5335. v->async_err = 0;
  5336. ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_start_playback);
  5337. if (ret < 0) {
  5338. pr_err("%s: Error %d sending START_PLAYBACK\n",
  5339. __func__, ret);
  5340. goto fail;
  5341. }
  5342. ret = wait_event_timeout(v->cvs_wait,
  5343. (v->cvs_state == CMD_STATUS_SUCCESS),
  5344. msecs_to_jiffies(TIMEOUT_MS));
  5345. if (!ret) {
  5346. pr_err("%s: wait_event timeout\n", __func__);
  5347. goto fail;
  5348. }
  5349. if (v->async_err > 0) {
  5350. pr_err("%s: DSP returned error[%s]\n",
  5351. __func__, adsp_err_get_err_str(
  5352. v->async_err));
  5353. ret = adsp_err_get_lnx_err_code(
  5354. v->async_err);
  5355. goto fail;
  5356. }
  5357. v->music_info.playing = 1;
  5358. } else {
  5359. pr_debug("%s: Start playback already sent\n", __func__);
  5360. }
  5361. return 0;
  5362. fail:
  5363. return ret;
  5364. }
  5365. static int voice_cvs_stop_playback(struct voice_data *v)
  5366. {
  5367. int ret = 0;
  5368. struct apr_hdr cvs_stop_playback;
  5369. void *apr_cvs;
  5370. u16 cvs_handle;
  5371. if (v == NULL) {
  5372. pr_err("%s: v is NULL\n", __func__);
  5373. return -EINVAL;
  5374. }
  5375. apr_cvs = common.apr_q6_cvs;
  5376. if (!apr_cvs) {
  5377. pr_err("%s: apr_cvs is NULL.\n", __func__);
  5378. return -EINVAL;
  5379. }
  5380. cvs_handle = voice_get_cvs_handle(v);
  5381. if (v->music_info.playing && ((!v->music_info.count) ||
  5382. (v->music_info.force))) {
  5383. cvs_stop_playback.hdr_field =
  5384. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  5385. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  5386. cvs_stop_playback.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  5387. sizeof(cvs_stop_playback) - APR_HDR_SIZE);
  5388. cvs_stop_playback.src_port =
  5389. voice_get_idx_for_session(v->session_id);
  5390. cvs_stop_playback.dest_port = cvs_handle;
  5391. cvs_stop_playback.token = 0;
  5392. cvs_stop_playback.opcode = VSS_IPLAYBACK_CMD_STOP;
  5393. v->cvs_state = CMD_STATUS_FAIL;
  5394. v->async_err = 0;
  5395. ret = apr_send_pkt(apr_cvs, (uint32_t *) &cvs_stop_playback);
  5396. if (ret < 0) {
  5397. pr_err("%s: Error %d sending STOP_PLAYBACK\n",
  5398. __func__, ret);
  5399. goto fail;
  5400. }
  5401. ret = wait_event_timeout(v->cvs_wait,
  5402. (v->cvs_state == CMD_STATUS_SUCCESS),
  5403. msecs_to_jiffies(TIMEOUT_MS));
  5404. if (!ret) {
  5405. pr_err("%s: wait_event timeout\n", __func__);
  5406. goto fail;
  5407. }
  5408. if (v->async_err > 0) {
  5409. pr_err("%s: DSP returned error[%s]\n",
  5410. __func__, adsp_err_get_err_str(
  5411. v->async_err));
  5412. ret = adsp_err_get_lnx_err_code(
  5413. v->async_err);
  5414. goto fail;
  5415. }
  5416. v->music_info.playing = 0;
  5417. v->music_info.force = 0;
  5418. } else {
  5419. pr_debug("%s: Stop playback already sent\n", __func__);
  5420. }
  5421. return 0;
  5422. fail:
  5423. return ret;
  5424. }
  5425. static int voc_lch_ops(struct voice_data *v, enum voice_lch_mode lch_mode)
  5426. {
  5427. int ret = 0;
  5428. if (v == NULL) {
  5429. pr_err("%s: v is NULL\n", __func__);
  5430. ret = -EINVAL;
  5431. goto done;
  5432. }
  5433. switch (lch_mode) {
  5434. case VOICE_LCH_START:
  5435. ret = voc_end_voice_call(v->session_id);
  5436. if (ret < 0)
  5437. pr_err("%s: voice call end failed %d\n",
  5438. __func__, ret);
  5439. break;
  5440. case VOICE_LCH_STOP:
  5441. ret = voc_start_voice_call(v->session_id);
  5442. if (ret < 0) {
  5443. pr_err("%s: voice call start failed %d\n",
  5444. __func__, ret);
  5445. goto done;
  5446. }
  5447. break;
  5448. default:
  5449. pr_err("%s: Invalid LCH mode: %d\n",
  5450. __func__, v->lch_mode);
  5451. break;
  5452. }
  5453. done:
  5454. return ret;
  5455. }
  5456. /**
  5457. * voc_start_playback -
  5458. * command to set playback for voice session
  5459. *
  5460. * @set: Enable or Disable for playback start/stop
  5461. * @port_id: Pseudo Port ID for playback data
  5462. *
  5463. * Returns 0 on success or error on failure
  5464. */
  5465. int voc_start_playback(uint32_t set, uint16_t port_id)
  5466. {
  5467. struct voice_data *v = NULL;
  5468. int ret = 0;
  5469. struct voice_session_itr itr;
  5470. u16 cvs_handle;
  5471. pr_debug("%s port_id = %#x set = %d", __func__, port_id, set);
  5472. voice_itr_init(&itr, ALL_SESSION_VSID);
  5473. while (voice_itr_get_next_session(&itr, &v)) {
  5474. if ((v != NULL) &&
  5475. (((port_id == VOICE_PLAYBACK_TX) &&
  5476. is_sub1_vsid(v->session_id)) ||
  5477. ((port_id == VOICE2_PLAYBACK_TX) &&
  5478. is_sub2_vsid(v->session_id)))) {
  5479. mutex_lock(&v->lock);
  5480. v->music_info.port_id = port_id;
  5481. v->music_info.play_enable = set;
  5482. if (set)
  5483. v->music_info.count++;
  5484. else
  5485. v->music_info.count--;
  5486. pr_debug("%s: music_info count=%d\n", __func__,
  5487. v->music_info.count);
  5488. cvs_handle = voice_get_cvs_handle(v);
  5489. if (cvs_handle != 0) {
  5490. if (set)
  5491. ret = voice_cvs_start_playback(v);
  5492. else
  5493. ret = voice_cvs_stop_playback(v);
  5494. }
  5495. mutex_unlock(&v->lock);
  5496. } else {
  5497. pr_err("%s: Invalid session\n", __func__);
  5498. }
  5499. }
  5500. return ret;
  5501. }
  5502. EXPORT_SYMBOL(voc_start_playback);
  5503. /**
  5504. * voc_disable_topology -
  5505. * disable topology for voice session
  5506. *
  5507. * @session_id: voice session ID to send this command
  5508. * @disable: disable value
  5509. *
  5510. * Returns 0 on success or error on failure
  5511. */
  5512. int voc_disable_topology(uint32_t session_id, uint32_t disable)
  5513. {
  5514. struct voice_data *v = voice_get_session(session_id);
  5515. int ret = 0;
  5516. if (v == NULL) {
  5517. pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
  5518. return -EINVAL;
  5519. }
  5520. mutex_lock(&v->lock);
  5521. v->disable_topology = disable;
  5522. mutex_unlock(&v->lock);
  5523. return ret;
  5524. }
  5525. EXPORT_SYMBOL(voc_disable_topology);
  5526. /**
  5527. * voc_set_incall_capture_channel_config -
  5528. * command to set channel count for record
  5529. *
  5530. * @channel_count: number of channels
  5531. *
  5532. */
  5533. void voc_set_incall_capture_channel_config(int channel_count)
  5534. {
  5535. common.rec_channel_count = channel_count;
  5536. }
  5537. EXPORT_SYMBOL(voc_set_incall_capture_channel_config);
  5538. /**
  5539. * voc_get_incall_capture_channel_config -
  5540. * command to get channel count for record
  5541. *
  5542. * Returns number of channels configured for record
  5543. */
  5544. int voc_get_incall_capture_channel_config(void)
  5545. {
  5546. return common.rec_channel_count;
  5547. }
  5548. EXPORT_SYMBOL(voc_get_incall_capture_channel_config);
  5549. static int voice_set_packet_exchange_mode_and_config(uint32_t session_id,
  5550. uint32_t mode)
  5551. {
  5552. struct voice_data *v = voice_get_session(session_id);
  5553. int ret = 0;
  5554. if (v == NULL) {
  5555. pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
  5556. return -EINVAL;
  5557. }
  5558. if (v->voc_state != VOC_RUN)
  5559. ret = voice_send_cvs_data_exchange_mode_cmd(v);
  5560. if (ret) {
  5561. pr_err("%s: Error voice_send_data_exchange_mode_cmd %d\n",
  5562. __func__, ret);
  5563. goto fail;
  5564. }
  5565. ret = voice_send_cvs_packet_exchange_config_cmd(v);
  5566. if (ret) {
  5567. pr_err("%s: Error: voice_send_packet_exchange_config_cmd %d\n",
  5568. __func__, ret);
  5569. goto fail;
  5570. }
  5571. return ret;
  5572. fail:
  5573. return -EINVAL;
  5574. }
  5575. /**
  5576. * voc_set_tx_mute -
  5577. * command to send TX mute for voice session
  5578. *
  5579. * @session_id: voice session ID to send this command
  5580. * @dir: RX or TX
  5581. * @mute: TX mute value
  5582. * @ramp_duration: Ramp duration in ms
  5583. *
  5584. * Returns 0 on success or error on failure
  5585. */
  5586. int voc_set_tx_mute(uint32_t session_id, uint32_t dir, uint32_t mute,
  5587. uint32_t ramp_duration)
  5588. {
  5589. struct voice_data *v = NULL;
  5590. int ret = 0;
  5591. struct voice_session_itr itr;
  5592. voice_itr_init(&itr, session_id);
  5593. while (voice_itr_get_next_session(&itr, &v)) {
  5594. if (v != NULL) {
  5595. mutex_lock(&v->lock);
  5596. v->stream_tx.stream_mute = mute;
  5597. v->stream_tx.stream_mute_ramp_duration_ms =
  5598. ramp_duration;
  5599. if (is_voc_state_active(v->voc_state) &&
  5600. (v->lch_mode == 0))
  5601. ret = voice_send_stream_mute_cmd(v,
  5602. VSS_IVOLUME_DIRECTION_TX,
  5603. v->stream_tx.stream_mute,
  5604. v->stream_tx.stream_mute_ramp_duration_ms);
  5605. mutex_unlock(&v->lock);
  5606. } else {
  5607. pr_err("%s: invalid session_id 0x%x\n", __func__,
  5608. session_id);
  5609. ret = -EINVAL;
  5610. break;
  5611. }
  5612. }
  5613. return ret;
  5614. }
  5615. EXPORT_SYMBOL(voc_set_tx_mute);
  5616. /**
  5617. * voc_set_device_mute -
  5618. * command to set device mute for voice session
  5619. *
  5620. * @session_id: voice session ID to send this command
  5621. * @dir: RX or TX
  5622. * @mute: mute value
  5623. * @ramp_duration: Ramp duration in ms
  5624. *
  5625. * Returns 0 on success or error on failure
  5626. */
  5627. int voc_set_device_mute(uint32_t session_id, uint32_t dir, uint32_t mute,
  5628. uint32_t ramp_duration)
  5629. {
  5630. struct voice_data *v = NULL;
  5631. int ret = 0;
  5632. struct voice_session_itr itr;
  5633. voice_itr_init(&itr, session_id);
  5634. while (voice_itr_get_next_session(&itr, &v)) {
  5635. if (v != NULL) {
  5636. mutex_lock(&v->lock);
  5637. if (dir == VSS_IVOLUME_DIRECTION_TX) {
  5638. v->dev_tx.dev_mute = mute;
  5639. v->dev_tx.dev_mute_ramp_duration_ms =
  5640. ramp_duration;
  5641. } else {
  5642. v->dev_rx.dev_mute = mute;
  5643. v->dev_rx.dev_mute_ramp_duration_ms =
  5644. ramp_duration;
  5645. }
  5646. if (((v->voc_state == VOC_RUN) ||
  5647. (v->voc_state == VOC_STANDBY)) &&
  5648. (v->lch_mode == 0))
  5649. ret = voice_send_device_mute_cmd(v,
  5650. dir,
  5651. mute,
  5652. ramp_duration);
  5653. mutex_unlock(&v->lock);
  5654. } else {
  5655. pr_err("%s: invalid session_id 0x%x\n", __func__,
  5656. session_id);
  5657. ret = -EINVAL;
  5658. break;
  5659. }
  5660. }
  5661. return ret;
  5662. }
  5663. EXPORT_SYMBOL(voc_set_device_mute);
  5664. int voc_get_rx_device_mute(uint32_t session_id)
  5665. {
  5666. struct voice_data *v = voice_get_session(session_id);
  5667. int ret = 0;
  5668. if (v == NULL) {
  5669. pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
  5670. return -EINVAL;
  5671. }
  5672. mutex_lock(&v->lock);
  5673. ret = v->dev_rx.dev_mute;
  5674. mutex_unlock(&v->lock);
  5675. return ret;
  5676. }
  5677. /**
  5678. * voc_set_tty_mode -
  5679. * Update tty mode for voice session
  5680. *
  5681. * @session_id: voice session ID
  5682. * @tty_mode: TTY mode value
  5683. *
  5684. * Returns 0 on success or error on failure
  5685. */
  5686. int voc_set_tty_mode(uint32_t session_id, uint8_t tty_mode)
  5687. {
  5688. struct voice_data *v = voice_get_session(session_id);
  5689. int ret = 0;
  5690. if (v == NULL) {
  5691. pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
  5692. return -EINVAL;
  5693. }
  5694. mutex_lock(&v->lock);
  5695. v->tty_mode = tty_mode;
  5696. mutex_unlock(&v->lock);
  5697. return ret;
  5698. }
  5699. EXPORT_SYMBOL(voc_set_tty_mode);
  5700. /**
  5701. * voc_get_tty_mode -
  5702. * Retrieve tty mode for voice session
  5703. *
  5704. * @session_id: voice session ID
  5705. *
  5706. * Returns 0 on success or error on failure
  5707. */
  5708. uint8_t voc_get_tty_mode(uint32_t session_id)
  5709. {
  5710. struct voice_data *v = voice_get_session(session_id);
  5711. int ret = 0;
  5712. if (v == NULL) {
  5713. pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
  5714. return -EINVAL;
  5715. }
  5716. mutex_lock(&v->lock);
  5717. ret = v->tty_mode;
  5718. mutex_unlock(&v->lock);
  5719. return ret;
  5720. }
  5721. EXPORT_SYMBOL(voc_get_tty_mode);
  5722. /**
  5723. * voc_set_pp_enable -
  5724. * Command to set PP for voice module
  5725. *
  5726. * @session_id: voice session ID to send this command
  5727. * @module_id: voice module id
  5728. * @enable: enable/disable flag
  5729. *
  5730. * Returns 0 on success or error on failure
  5731. */
  5732. int voc_set_pp_enable(uint32_t session_id,
  5733. struct module_instance_info mod_inst_info,
  5734. uint32_t enable)
  5735. {
  5736. struct voice_data *v = NULL;
  5737. int ret = 0;
  5738. struct voice_session_itr itr;
  5739. int mid = mod_inst_info.module_id;
  5740. int iid = mod_inst_info.instance_id;
  5741. voice_itr_init(&itr, session_id);
  5742. while (voice_itr_get_next_session(&itr, &v)) {
  5743. if (v != NULL) {
  5744. if (!(is_voice_app_id(v->session_id)))
  5745. continue;
  5746. mutex_lock(&v->lock);
  5747. if (mid == MODULE_ID_VOICE_MODULE_ST &&
  5748. iid == INSTANCE_ID_0)
  5749. v->st_enable = enable;
  5750. if (v->voc_state == VOC_RUN) {
  5751. if ((mid == MODULE_ID_VOICE_MODULE_ST) &&
  5752. iid == INSTANCE_ID_0 && (!v->tty_mode))
  5753. ret = voice_send_set_pp_enable_cmd(
  5754. v, mod_inst_info, enable);
  5755. }
  5756. mutex_unlock(&v->lock);
  5757. } else {
  5758. pr_err("%s: invalid session_id 0x%x\n", __func__,
  5759. session_id);
  5760. ret = -EINVAL;
  5761. break;
  5762. }
  5763. }
  5764. return ret;
  5765. }
  5766. EXPORT_SYMBOL(voc_set_pp_enable);
  5767. /**
  5768. * voc_set_hd_enable -
  5769. * Command to set HD for voice session
  5770. *
  5771. * @session_id: voice session ID to send this command
  5772. * @enable: enable/disable flag
  5773. *
  5774. * Returns 0 on success or error on failure
  5775. */
  5776. int voc_set_hd_enable(uint32_t session_id, uint32_t enable)
  5777. {
  5778. struct voice_data *v = NULL;
  5779. int ret = 0;
  5780. struct voice_session_itr itr;
  5781. voice_itr_init(&itr, session_id);
  5782. while (voice_itr_get_next_session(&itr, &v)) {
  5783. if (v != NULL) {
  5784. mutex_lock(&v->lock);
  5785. v->hd_enable = enable;
  5786. if (v->voc_state == VOC_RUN)
  5787. ret = voice_send_hd_cmd(v, enable);
  5788. mutex_unlock(&v->lock);
  5789. } else {
  5790. pr_err("%s: invalid session_id 0x%x\n", __func__,
  5791. session_id);
  5792. ret = -EINVAL;
  5793. break;
  5794. }
  5795. }
  5796. return ret;
  5797. }
  5798. EXPORT_SYMBOL(voc_set_hd_enable);
  5799. /**
  5800. * voc_set_afe_sidetone -
  5801. * Command to set sidetone at AFE
  5802. *
  5803. * @session_id: voice session ID to send this command
  5804. * @sidetone_enable: enable/disable flag for sidetone
  5805. *
  5806. * Returns 0 on success or error on failure
  5807. */
  5808. int voc_set_afe_sidetone(uint32_t session_id, bool sidetone_enable)
  5809. {
  5810. struct voice_data *v = NULL;
  5811. int ret = -EINVAL;
  5812. struct voice_session_itr itr;
  5813. u16 rx_port, tx_port;
  5814. common.sidetone_enable = sidetone_enable;
  5815. voice_itr_init(&itr, session_id);
  5816. while (voice_itr_get_next_session(&itr, &v)) {
  5817. if (v == NULL) {
  5818. pr_err("%s: invalid session_id 0x%x\n", __func__,
  5819. session_id);
  5820. ret = -EINVAL;
  5821. break;
  5822. }
  5823. mutex_lock(&v->lock);
  5824. if (v->voc_state != VOC_RUN) {
  5825. mutex_unlock(&v->lock);
  5826. continue;
  5827. }
  5828. rx_port = v->dev_rx.port_id;
  5829. tx_port = v->dev_tx.port_id;
  5830. ret = afe_sidetone_enable(tx_port, rx_port,
  5831. sidetone_enable);
  5832. if (!ret) {
  5833. mutex_unlock(&v->lock);
  5834. break;
  5835. }
  5836. mutex_unlock(&v->lock);
  5837. }
  5838. return ret;
  5839. }
  5840. EXPORT_SYMBOL(voc_set_afe_sidetone);
  5841. /**
  5842. * voc_get_afe_sidetone -
  5843. * Retrieve sidetone status at AFE
  5844. *
  5845. * Returns sidetone enable status
  5846. */
  5847. bool voc_get_afe_sidetone(void)
  5848. {
  5849. bool ret;
  5850. ret = common.sidetone_enable;
  5851. return ret;
  5852. }
  5853. EXPORT_SYMBOL(voc_get_afe_sidetone);
  5854. int voc_get_pp_enable(uint32_t session_id,
  5855. struct module_instance_info mod_inst_info)
  5856. {
  5857. struct voice_data *v = voice_get_session(session_id);
  5858. int ret = 0;
  5859. if (v == NULL) {
  5860. pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
  5861. return -EINVAL;
  5862. }
  5863. mutex_lock(&v->lock);
  5864. if (mod_inst_info.module_id == MODULE_ID_VOICE_MODULE_ST &&
  5865. mod_inst_info.instance_id == INSTANCE_ID_0)
  5866. ret = v->st_enable;
  5867. mutex_unlock(&v->lock);
  5868. return ret;
  5869. }
  5870. /**
  5871. * voc_set_rx_vol_step -
  5872. * command to send voice RX volume in step value
  5873. *
  5874. * @session_id: voice session ID
  5875. * @dir: direction RX or TX
  5876. * @vol_step: Volume step value
  5877. * @ramp_duration: Ramp duration in ms
  5878. *
  5879. * Returns 0 on success or -EINVAL on failure
  5880. */
  5881. int voc_set_rx_vol_step(uint32_t session_id, uint32_t dir, uint32_t vol_step,
  5882. uint32_t ramp_duration)
  5883. {
  5884. struct voice_data *v = NULL;
  5885. int ret = 0;
  5886. struct voice_session_itr itr;
  5887. pr_debug("%s session id = %#x vol = %u", __func__, session_id,
  5888. vol_step);
  5889. voice_itr_init(&itr, session_id);
  5890. while (voice_itr_get_next_session(&itr, &v)) {
  5891. if (v != NULL) {
  5892. mutex_lock(&v->lock);
  5893. v->dev_rx.volume_step_value = vol_step;
  5894. v->dev_rx.volume_ramp_duration_ms = ramp_duration;
  5895. if (is_voc_state_active(v->voc_state))
  5896. ret = voice_send_vol_step_cmd(v);
  5897. mutex_unlock(&v->lock);
  5898. } else {
  5899. pr_err("%s: invalid session_id 0x%x\n", __func__,
  5900. session_id);
  5901. ret = -EINVAL;
  5902. break;
  5903. }
  5904. }
  5905. return ret;
  5906. }
  5907. EXPORT_SYMBOL(voc_set_rx_vol_step);
  5908. /**
  5909. * voc_set_device_config -
  5910. * Set voice path config for RX or TX
  5911. *
  5912. * @session_id: voice session ID
  5913. * @path_dir: direction RX or TX
  5914. * @finfo: format config info
  5915. *
  5916. * Returns 0 on success or -EINVAL on failure
  5917. */
  5918. int voc_set_device_config(uint32_t session_id, uint8_t path_dir,
  5919. struct media_format_info *finfo)
  5920. {
  5921. struct voice_data *v = voice_get_session(session_id);
  5922. if (v == NULL) {
  5923. pr_err("%s: Invalid session_id 0x%x\n", __func__, session_id);
  5924. return -EINVAL;
  5925. }
  5926. pr_debug("%s: path_dir=%d port_id=%x, channels=%d, sample_rate=%d, bits_per_sample=%d\n",
  5927. __func__, path_dir, finfo->port_id, finfo->num_channels,
  5928. finfo->sample_rate, finfo->bits_per_sample);
  5929. mutex_lock(&v->lock);
  5930. switch (path_dir) {
  5931. case RX_PATH:
  5932. v->dev_rx.port_id = q6audio_get_port_id(finfo->port_id);
  5933. v->dev_rx.no_of_channels = finfo->num_channels;
  5934. v->dev_rx.sample_rate = finfo->sample_rate;
  5935. v->dev_rx.bits_per_sample = finfo->bits_per_sample;
  5936. memcpy(&v->dev_rx.channel_mapping, &finfo->channel_mapping,
  5937. VSS_CHANNEL_MAPPING_SIZE);
  5938. break;
  5939. case TX_PATH:
  5940. v->dev_tx.port_id = q6audio_get_port_id(finfo->port_id);
  5941. v->dev_tx.no_of_channels = finfo->num_channels;
  5942. v->dev_tx.sample_rate = finfo->sample_rate;
  5943. v->dev_tx.bits_per_sample = finfo->bits_per_sample;
  5944. memcpy(&v->dev_tx.channel_mapping, &finfo->channel_mapping,
  5945. VSS_CHANNEL_MAPPING_SIZE);
  5946. break;
  5947. default:
  5948. pr_err("%s: Invalid path_dir %d\n", __func__, path_dir);
  5949. return -EINVAL;
  5950. }
  5951. mutex_unlock(&v->lock);
  5952. return 0;
  5953. }
  5954. EXPORT_SYMBOL(voc_set_device_config);
  5955. /**
  5956. * voc_set_ext_ec_ref_media_fmt_info -
  5957. * Update voice EC media format info
  5958. *
  5959. * @finfo: media format info
  5960. *
  5961. */
  5962. int voc_set_ext_ec_ref_media_fmt_info(struct media_format_info *finfo)
  5963. {
  5964. mutex_lock(&common.common_lock);
  5965. if (common.ec_ref_ext) {
  5966. common.ec_media_fmt_info.num_channels = finfo->num_channels;
  5967. common.ec_media_fmt_info.bits_per_sample =
  5968. finfo->bits_per_sample;
  5969. common.ec_media_fmt_info.sample_rate = finfo->sample_rate;
  5970. memcpy(&common.ec_media_fmt_info.channel_mapping,
  5971. &finfo->channel_mapping, VSS_CHANNEL_MAPPING_SIZE);
  5972. } else {
  5973. pr_debug("%s: Ext Ec Ref not active, returning", __func__);
  5974. }
  5975. mutex_unlock(&common.common_lock);
  5976. return 0;
  5977. }
  5978. EXPORT_SYMBOL(voc_set_ext_ec_ref_media_fmt_info);
  5979. /**
  5980. * voc_set_route_flag -
  5981. * Set voice route state for RX or TX
  5982. *
  5983. * @session_id: voice session ID
  5984. * @path_dir: direction RX or TX
  5985. * @set: Value of route state to set
  5986. *
  5987. * Returns 0 on success or -EINVAL on failure
  5988. */
  5989. int voc_set_route_flag(uint32_t session_id, uint8_t path_dir, uint8_t set)
  5990. {
  5991. struct voice_data *v = voice_get_session(session_id);
  5992. if (v == NULL) {
  5993. pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
  5994. return -EINVAL;
  5995. }
  5996. pr_debug("%s: path_dir=%d, set=%d\n", __func__, path_dir, set);
  5997. mutex_lock(&v->lock);
  5998. if (path_dir == RX_PATH)
  5999. v->voc_route_state.rx_route_flag = set;
  6000. else
  6001. v->voc_route_state.tx_route_flag = set;
  6002. mutex_unlock(&v->lock);
  6003. return 0;
  6004. }
  6005. EXPORT_SYMBOL(voc_set_route_flag);
  6006. /**
  6007. * voc_get_route_flag -
  6008. * Retrieve voice route state for RX or TX
  6009. *
  6010. * @session_id: voice session ID
  6011. * @path_dir: direction RX or TX
  6012. *
  6013. * Returns route state on success or 0 on failure
  6014. */
  6015. uint8_t voc_get_route_flag(uint32_t session_id, uint8_t path_dir)
  6016. {
  6017. struct voice_data *v = voice_get_session(session_id);
  6018. int ret = 0;
  6019. if (v == NULL) {
  6020. pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
  6021. return 0;
  6022. }
  6023. mutex_lock(&v->lock);
  6024. if (path_dir == RX_PATH)
  6025. ret = v->voc_route_state.rx_route_flag;
  6026. else
  6027. ret = v->voc_route_state.tx_route_flag;
  6028. mutex_unlock(&v->lock);
  6029. return ret;
  6030. }
  6031. EXPORT_SYMBOL(voc_get_route_flag);
  6032. /**
  6033. * voc_get_mbd_enable -
  6034. * Retrieve mic break detection enable state
  6035. *
  6036. * Returns true if mic break detection is enabled or false if disabled
  6037. */
  6038. bool voc_get_mbd_enable(void)
  6039. {
  6040. bool enable = false;
  6041. mutex_lock(&common.common_lock);
  6042. enable = common.mic_break_enable;
  6043. mutex_unlock(&common.common_lock);
  6044. return enable;
  6045. }
  6046. EXPORT_SYMBOL(voc_get_mbd_enable);
  6047. /**
  6048. * voc_set_mbd_enable -
  6049. * Set mic break detection enable state
  6050. *
  6051. * @enable: mic break detection state to set
  6052. *
  6053. * Returns 0
  6054. */
  6055. uint8_t voc_set_mbd_enable(bool enable)
  6056. {
  6057. struct voice_data *v = NULL;
  6058. struct voice_session_itr itr;
  6059. bool check_and_send_event = false;
  6060. uint32_t event_id = VSS_INOTIFY_CMD_LISTEN_FOR_EVENT_CLASS;
  6061. uint32_t class_id = VSS_ICOMMON_EVENT_CLASS_VOICE_ACTIVITY_UPDATE;
  6062. mutex_lock(&common.common_lock);
  6063. if (common.mic_break_enable != enable)
  6064. check_and_send_event = true;
  6065. common.mic_break_enable = enable;
  6066. mutex_unlock(&common.common_lock);
  6067. if (!check_and_send_event)
  6068. return 0;
  6069. if (!enable)
  6070. event_id = VSS_INOTIFY_CMD_CANCEL_EVENT_CLASS;
  6071. memset(&itr, 0, sizeof(itr));
  6072. voice_itr_init(&itr, ALL_SESSION_VSID);
  6073. while (voice_itr_get_next_session(&itr, &v)) {
  6074. if (v != NULL) {
  6075. mutex_lock(&v->lock);
  6076. if (is_voc_state_active(v->voc_state)) {
  6077. voice_send_mvm_event_class_cmd(v, event_id,
  6078. class_id);
  6079. }
  6080. mutex_unlock(&v->lock);
  6081. }
  6082. }
  6083. return 0;
  6084. }
  6085. EXPORT_SYMBOL(voc_set_mbd_enable);
  6086. /**
  6087. * voc_end_voice_call -
  6088. * command to end voice call
  6089. *
  6090. * @session_id: voice session ID to send this command
  6091. *
  6092. * Returns 0 on success or error on failure
  6093. */
  6094. int voc_end_voice_call(uint32_t session_id)
  6095. {
  6096. struct voice_data *v = voice_get_session(session_id);
  6097. int ret = 0;
  6098. if (v == NULL) {
  6099. pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
  6100. return -EINVAL;
  6101. }
  6102. mutex_lock(&v->lock);
  6103. if (v->voc_state == VOC_RUN || v->voc_state == VOC_ERROR ||
  6104. v->voc_state == VOC_CHANGE || v->voc_state == VOC_STANDBY) {
  6105. pr_debug("%s: VOC_STATE: %d\n", __func__, v->voc_state);
  6106. ret = voice_destroy_vocproc(v);
  6107. if (ret < 0)
  6108. pr_err("%s: destroy voice failed\n", __func__);
  6109. voc_update_session_params(v);
  6110. voice_destroy_mvm_cvs_session(v);
  6111. ret = voice_mhi_end();
  6112. if (ret < 0)
  6113. pr_debug("%s: voice_mhi_end failed! %d\n",
  6114. __func__, ret);
  6115. v->voc_state = VOC_RELEASE;
  6116. } else {
  6117. pr_err("%s: Error: End voice called in state %d\n",
  6118. __func__, v->voc_state);
  6119. ret = -EINVAL;
  6120. }
  6121. mutex_unlock(&v->lock);
  6122. return ret;
  6123. }
  6124. EXPORT_SYMBOL(voc_end_voice_call);
  6125. /**
  6126. * voc_standby_voice_call -
  6127. * command to standy voice call
  6128. *
  6129. * @session_id: voice session ID to send this command
  6130. *
  6131. * Returns 0 on success or error on failure
  6132. */
  6133. int voc_standby_voice_call(uint32_t session_id)
  6134. {
  6135. struct voice_data *v = voice_get_session(session_id);
  6136. struct apr_hdr mvm_standby_voice_cmd;
  6137. void *apr_mvm;
  6138. u16 mvm_handle;
  6139. int ret = 0;
  6140. if (v == NULL) {
  6141. pr_err("%s: v is NULL\n", __func__);
  6142. return -EINVAL;
  6143. }
  6144. pr_debug("%s: voc state=%d", __func__, v->voc_state);
  6145. if (v->voc_state == VOC_RUN) {
  6146. apr_mvm = common.apr_q6_mvm;
  6147. if (!apr_mvm) {
  6148. pr_err("%s: apr_mvm is NULL.\n", __func__);
  6149. ret = -EINVAL;
  6150. goto fail;
  6151. }
  6152. mvm_handle = voice_get_mvm_handle(v);
  6153. mvm_standby_voice_cmd.hdr_field =
  6154. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  6155. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  6156. mvm_standby_voice_cmd.pkt_size =
  6157. APR_PKT_SIZE(APR_HDR_SIZE,
  6158. sizeof(mvm_standby_voice_cmd) - APR_HDR_SIZE);
  6159. pr_debug("send mvm_standby_voice_cmd pkt size = %d\n",
  6160. mvm_standby_voice_cmd.pkt_size);
  6161. mvm_standby_voice_cmd.src_port =
  6162. voice_get_idx_for_session(v->session_id);
  6163. mvm_standby_voice_cmd.dest_port = mvm_handle;
  6164. mvm_standby_voice_cmd.token = 0;
  6165. mvm_standby_voice_cmd.opcode = VSS_IMVM_CMD_STANDBY_VOICE;
  6166. v->mvm_state = CMD_STATUS_FAIL;
  6167. ret = apr_send_pkt(apr_mvm,
  6168. (uint32_t *)&mvm_standby_voice_cmd);
  6169. if (ret < 0) {
  6170. pr_err("Fail in sending VSS_IMVM_CMD_STANDBY_VOICE\n");
  6171. ret = -EINVAL;
  6172. goto fail;
  6173. }
  6174. v->voc_state = VOC_STANDBY;
  6175. }
  6176. fail:
  6177. return ret;
  6178. }
  6179. EXPORT_SYMBOL(voc_standby_voice_call);
  6180. /**
  6181. * voc_disable_device -
  6182. * command to pause call and disable voice path
  6183. *
  6184. * @session_id: voice session ID to send this command
  6185. *
  6186. * Returns 0 on success or error on failure
  6187. */
  6188. int voc_disable_device(uint32_t session_id)
  6189. {
  6190. struct voice_data *v = voice_get_session(session_id);
  6191. int ret = 0;
  6192. if (v == NULL) {
  6193. pr_err("%s: v is NULL\n", __func__);
  6194. return -EINVAL;
  6195. }
  6196. pr_debug("%s: voc state=%d\n", __func__, v->voc_state);
  6197. mutex_lock(&v->lock);
  6198. if (v->voc_state == VOC_RUN) {
  6199. ret = voice_pause_voice_call(v);
  6200. if (ret < 0) {
  6201. pr_err("%s: Pause Voice Call failed for session 0x%x, err %d!\n",
  6202. __func__, v->session_id, ret);
  6203. goto done;
  6204. }
  6205. rtac_remove_voice(voice_get_cvs_handle(v));
  6206. voice_send_cvp_deregister_vol_cal_cmd(v);
  6207. voice_send_cvp_deregister_cal_cmd(v);
  6208. voice_send_cvp_deregister_dev_cfg_cmd(v);
  6209. /* Unload topology modules */
  6210. voice_unload_topo_modules();
  6211. v->voc_state = VOC_CHANGE;
  6212. } else {
  6213. pr_debug("%s: called in voc state=%d, No_OP\n",
  6214. __func__, v->voc_state);
  6215. }
  6216. done:
  6217. mutex_unlock(&v->lock);
  6218. return ret;
  6219. }
  6220. EXPORT_SYMBOL(voc_disable_device);
  6221. /**
  6222. * voc_enable_device -
  6223. * command to enable voice path and start call
  6224. *
  6225. * @session_id: voice session ID to send this command
  6226. *
  6227. * Returns 0 on success or error on failure
  6228. */
  6229. int voc_enable_device(uint32_t session_id)
  6230. {
  6231. struct voice_data *v = voice_get_session(session_id);
  6232. struct module_instance_info mod_inst_info;
  6233. int ret = 0;
  6234. memset(&mod_inst_info, 0, sizeof(mod_inst_info));
  6235. if (v == NULL) {
  6236. pr_err("%s: v is NULL\n", __func__);
  6237. return -EINVAL;
  6238. }
  6239. pr_debug("%s: voc state=%d\n", __func__, v->voc_state);
  6240. mutex_lock(&v->lock);
  6241. if (v->voc_state == VOC_CHANGE) {
  6242. ret = voice_send_tty_mode_cmd(v);
  6243. if (ret < 0) {
  6244. pr_err("%s: Sending TTY mode failed, ret=%d\n",
  6245. __func__, ret);
  6246. /* Not a critical error, allow voice call to continue */
  6247. }
  6248. mod_inst_info.module_id = MODULE_ID_VOICE_MODULE_ST;
  6249. mod_inst_info.instance_id = INSTANCE_ID_0;
  6250. if (v->tty_mode) {
  6251. /* disable slowtalk */
  6252. voice_send_set_pp_enable_cmd(v, mod_inst_info, 0);
  6253. } else {
  6254. /* restore slowtalk */
  6255. voice_send_set_pp_enable_cmd(v, mod_inst_info,
  6256. v->st_enable);
  6257. }
  6258. ret = voice_send_set_device_cmd(v);
  6259. if (ret < 0) {
  6260. pr_err("%s: Set device failed, ret=%d\n",
  6261. __func__, ret);
  6262. goto done;
  6263. }
  6264. ret = voice_send_cvp_media_fmt_info_cmd(v);
  6265. if (ret < 0) {
  6266. pr_err("%s: Set format failed err:%d\n", __func__, ret);
  6267. goto done;
  6268. }
  6269. ret = voice_send_cvp_topology_commit_cmd(v);
  6270. if (ret < 0) {
  6271. pr_err("%s: Set topology commit failed\n", __func__);
  6272. goto done;
  6273. }
  6274. /* Send MFC config only when the no of channels are > 1 */
  6275. if (v->dev_rx.no_of_channels > NUM_CHANNELS_MONO) {
  6276. ret = voice_send_cvp_mfc_config_cmd(v);
  6277. if (ret < 0) {
  6278. pr_warn("%s: Set mfc config failed err: %d\n",
  6279. __func__, ret);
  6280. }
  6281. }
  6282. voice_send_cvp_register_dev_cfg_cmd(v);
  6283. voice_send_cvp_register_cal_cmd(v);
  6284. voice_send_cvp_register_vol_cal_cmd(v);
  6285. rtac_add_voice(voice_get_cvs_handle(v),
  6286. voice_get_cvp_handle(v),
  6287. v->dev_rx.port_id, v->dev_tx.port_id,
  6288. v->dev_rx.dev_id, v->dev_tx.dev_id,
  6289. v->session_id);
  6290. ret = voice_send_start_voice_cmd(v);
  6291. if (ret < 0) {
  6292. pr_err("%s: Fail in sending START_VOICE, ret=%d\n",
  6293. __func__, ret);
  6294. goto done;
  6295. }
  6296. v->voc_state = VOC_RUN;
  6297. if (v->lch_mode == 0) {
  6298. pr_debug("%s: dev_mute = %d, ramp_duration = %d ms\n",
  6299. __func__, v->dev_rx.dev_mute,
  6300. v->dev_rx.dev_mute_ramp_duration_ms);
  6301. ret = voice_send_device_mute_cmd(v,
  6302. VSS_IVOLUME_DIRECTION_RX,
  6303. v->dev_rx.dev_mute,
  6304. v->dev_rx.dev_mute_ramp_duration_ms);
  6305. }
  6306. } else {
  6307. pr_debug("%s: called in voc state=%d, No_OP\n",
  6308. __func__, v->voc_state);
  6309. }
  6310. done:
  6311. mutex_unlock(&v->lock);
  6312. return ret;
  6313. }
  6314. EXPORT_SYMBOL(voc_enable_device);
  6315. /**
  6316. * voc_set_lch -
  6317. * command to set hold/unhold call state
  6318. *
  6319. * @session_id: voice session ID to send this command
  6320. * @lch_mode: LCH mode to set
  6321. *
  6322. * Returns 0 on success or error on failure
  6323. */
  6324. int voc_set_lch(uint32_t session_id, enum voice_lch_mode lch_mode)
  6325. {
  6326. struct voice_data *v = voice_get_session(session_id);
  6327. int ret = 0;
  6328. if (v == NULL) {
  6329. pr_err("%s: Invalid session_id 0x%x\n", __func__, session_id);
  6330. ret = -EINVAL;
  6331. goto done;
  6332. }
  6333. mutex_lock(&v->lock);
  6334. if (v->lch_mode == lch_mode) {
  6335. pr_debug("%s: Session %d already in LCH mode %d\n",
  6336. __func__, session_id, lch_mode);
  6337. mutex_unlock(&v->lock);
  6338. goto done;
  6339. }
  6340. v->lch_mode = lch_mode;
  6341. mutex_unlock(&v->lock);
  6342. ret = voc_lch_ops(v, v->lch_mode);
  6343. if (ret < 0) {
  6344. pr_err("%s: lch ops failed %d\n", __func__, ret);
  6345. goto done;
  6346. }
  6347. done:
  6348. return ret;
  6349. }
  6350. EXPORT_SYMBOL(voc_set_lch);
  6351. /**
  6352. * voc_resume_voice_call -
  6353. * command to resume voice call
  6354. *
  6355. * @session_id: voice session ID to send this command
  6356. *
  6357. * Returns 0 on success or error on failure
  6358. */
  6359. int voc_resume_voice_call(uint32_t session_id)
  6360. {
  6361. struct voice_data *v = voice_get_session(session_id);
  6362. int ret = 0;
  6363. ret = voice_send_start_voice_cmd(v);
  6364. if (ret < 0) {
  6365. pr_err("Fail in sending START_VOICE\n");
  6366. goto fail;
  6367. }
  6368. v->voc_state = VOC_RUN;
  6369. return 0;
  6370. fail:
  6371. return -EINVAL;
  6372. }
  6373. EXPORT_SYMBOL(voc_resume_voice_call);
  6374. /**
  6375. * voc_start_voice_call -
  6376. * command to start voice call
  6377. *
  6378. * @session_id: voice session ID to send this command
  6379. *
  6380. * Returns 0 on success or error on failure
  6381. */
  6382. int voc_start_voice_call(uint32_t session_id)
  6383. {
  6384. struct voice_data *v = voice_get_session(session_id);
  6385. int ret = 0;
  6386. if (v == NULL) {
  6387. pr_err("%s: invalid session_id 0x%x\n", __func__, session_id);
  6388. return -EINVAL;
  6389. }
  6390. mutex_lock(&v->lock);
  6391. if (v->voc_state == VOC_ERROR) {
  6392. pr_debug("%s: VOC in ERR state\n", __func__);
  6393. voice_destroy_mvm_cvs_session(v);
  6394. v->voc_state = VOC_INIT;
  6395. }
  6396. if ((v->voc_state == VOC_INIT) ||
  6397. (v->voc_state == VOC_RELEASE)) {
  6398. ret = voice_apr_register(session_id);
  6399. if (ret < 0) {
  6400. pr_err("%s: apr register failed\n", __func__);
  6401. goto fail;
  6402. }
  6403. if (is_cvd_version_queried()) {
  6404. pr_debug("%s: Returning the cached value %s\n",
  6405. __func__, common.cvd_version);
  6406. } else {
  6407. ret = voice_send_mvm_cvd_version_cmd(v);
  6408. if (ret < 0)
  6409. pr_debug("%s: Error retrieving CVD version %d\n",
  6410. __func__, ret);
  6411. }
  6412. ret = voice_mhi_start();
  6413. if (ret < 0) {
  6414. pr_debug("%s: voice_mhi_start failed! %d\n",
  6415. __func__, ret);
  6416. goto fail;
  6417. }
  6418. ret = voice_create_mvm_cvs_session(v);
  6419. if (ret < 0) {
  6420. pr_err("create mvm and cvs failed\n");
  6421. goto fail;
  6422. }
  6423. if (is_voip_session(session_id)) {
  6424. /* Allocate oob mem if not already allocated and
  6425. * memory map the oob memory block.
  6426. */
  6427. ret = voice_alloc_and_map_oob_mem(v);
  6428. if (ret < 0) {
  6429. pr_err("%s: voice_alloc_and_map_oob_mem() failed, ret:%d\n",
  6430. __func__, ret);
  6431. goto fail;
  6432. }
  6433. ret = voice_set_packet_exchange_mode_and_config(
  6434. session_id,
  6435. VSS_ISTREAM_PACKET_EXCHANGE_MODE_OUT_OF_BAND);
  6436. if (ret) {
  6437. pr_err("%s: Err: exchange_mode_and_config %d\n",
  6438. __func__, ret);
  6439. goto fail;
  6440. }
  6441. }
  6442. ret = voice_send_dual_control_cmd(v);
  6443. if (ret < 0) {
  6444. pr_err("Err Dual command failed\n");
  6445. goto fail;
  6446. }
  6447. ret = voice_setup_vocproc(v);
  6448. if (ret < 0) {
  6449. pr_err("setup voice failed\n");
  6450. goto fail;
  6451. }
  6452. ret = voice_send_vol_step_cmd(v);
  6453. if (ret < 0)
  6454. pr_err("voice volume failed\n");
  6455. ret = voice_send_stream_mute_cmd(v,
  6456. VSS_IVOLUME_DIRECTION_TX,
  6457. v->stream_tx.stream_mute,
  6458. v->stream_tx.stream_mute_ramp_duration_ms);
  6459. if (ret < 0)
  6460. pr_err("voice mute failed\n");
  6461. ret = voice_send_start_voice_cmd(v);
  6462. if (ret < 0) {
  6463. pr_err("start voice failed\n");
  6464. goto fail;
  6465. }
  6466. v->voc_state = VOC_RUN;
  6467. } else {
  6468. pr_err("%s: Error: Start voice called in state %d\n",
  6469. __func__, v->voc_state);
  6470. ret = -EINVAL;
  6471. goto fail;
  6472. }
  6473. fail:
  6474. mutex_unlock(&v->lock);
  6475. return ret;
  6476. }
  6477. EXPORT_SYMBOL(voc_start_voice_call);
  6478. /**
  6479. * voc_set_ext_ec_ref_port_id -
  6480. * Set EC ref port id
  6481. *
  6482. * Returns 0 on success or -EINVAL on failure
  6483. */
  6484. int voc_set_ext_ec_ref_port_id(uint16_t port_id, bool state)
  6485. {
  6486. int ret = 0;
  6487. mutex_lock(&common.common_lock);
  6488. if (state == true) {
  6489. if (port_id == AFE_PORT_INVALID) {
  6490. pr_err("%s: Invalid port id", __func__);
  6491. ret = -EINVAL;
  6492. goto exit;
  6493. }
  6494. common.ec_ref_ext = true;
  6495. } else {
  6496. common.ec_ref_ext = false;
  6497. }
  6498. /* Cache EC Fromat Info in common */
  6499. common.ec_media_fmt_info.port_id = port_id;
  6500. exit:
  6501. mutex_unlock(&common.common_lock);
  6502. return ret;
  6503. }
  6504. EXPORT_SYMBOL(voc_set_ext_ec_ref_port_id);
  6505. /**
  6506. * voc_get_ext_ec_ref_port_id -
  6507. * Retrieve EC ref port id
  6508. *
  6509. * Returns EC Ref port id if present
  6510. * otherwise AFE_PORT_INVALID
  6511. */
  6512. int voc_get_ext_ec_ref_port_id(void)
  6513. {
  6514. if (common.ec_ref_ext)
  6515. return common.ec_media_fmt_info.port_id;
  6516. else
  6517. return AFE_PORT_INVALID;
  6518. }
  6519. EXPORT_SYMBOL(voc_get_ext_ec_ref_port_id);
  6520. /**
  6521. * voc_register_mvs_cb -
  6522. * Update callback info for mvs
  6523. *
  6524. * @ul_cb: Uplink callback fn
  6525. * @dl_cb: downlink callback fn
  6526. * ssr_cb: SSR callback fn
  6527. * @private_data: private data of mvs
  6528. *
  6529. */
  6530. void voc_register_mvs_cb(ul_cb_fn ul_cb,
  6531. dl_cb_fn dl_cb,
  6532. voip_ssr_cb ssr_cb,
  6533. void *private_data)
  6534. {
  6535. common.mvs_info.ul_cb = ul_cb;
  6536. common.mvs_info.dl_cb = dl_cb;
  6537. common.mvs_info.ssr_cb = ssr_cb;
  6538. common.mvs_info.private_data = private_data;
  6539. }
  6540. EXPORT_SYMBOL(voc_register_mvs_cb);
  6541. /**
  6542. * voc_register_dtmf_rx_detection_cb -
  6543. * Update callback info for dtmf
  6544. *
  6545. * @dtmf_rx_ul_cb: DTMF uplink RX callback fn
  6546. * @private_data: private data of dtmf info
  6547. *
  6548. */
  6549. void voc_register_dtmf_rx_detection_cb(dtmf_rx_det_cb_fn dtmf_rx_ul_cb,
  6550. void *private_data)
  6551. {
  6552. common.dtmf_info.dtmf_rx_ul_cb = dtmf_rx_ul_cb;
  6553. common.dtmf_info.private_data = private_data;
  6554. }
  6555. EXPORT_SYMBOL(voc_register_dtmf_rx_detection_cb);
  6556. /**
  6557. * voc_config_vocoder -
  6558. * Update config for mvs params.
  6559. */
  6560. void voc_config_vocoder(uint32_t media_type,
  6561. uint32_t rate,
  6562. uint32_t network_type,
  6563. uint32_t dtx_mode,
  6564. uint32_t evrc_min_rate,
  6565. uint32_t evrc_max_rate)
  6566. {
  6567. common.mvs_info.media_type = media_type;
  6568. common.mvs_info.rate = rate;
  6569. common.mvs_info.network_type = network_type;
  6570. common.mvs_info.dtx_mode = dtx_mode;
  6571. common.mvs_info.evrc_min_rate = evrc_min_rate;
  6572. common.mvs_info.evrc_max_rate = evrc_max_rate;
  6573. }
  6574. EXPORT_SYMBOL(voc_config_vocoder);
  6575. static int32_t qdsp_mvm_callback(struct apr_client_data *data, void *priv)
  6576. {
  6577. uint32_t *ptr = NULL, min_payload_size = 0;
  6578. struct common_data *c = NULL;
  6579. struct voice_data *v = NULL;
  6580. struct vss_evt_voice_activity *voice_act_update = NULL;
  6581. int i = 0;
  6582. struct vss_iversion_rsp_get_t *version_rsp = NULL;
  6583. if ((data == NULL) || (priv == NULL)) {
  6584. pr_err("%s: data or priv is NULL\n", __func__);
  6585. return -EINVAL;
  6586. }
  6587. c = priv;
  6588. pr_debug("%s: Payload Length = %d, opcode=%x\n", __func__,
  6589. data->payload_size, data->opcode);
  6590. if (data->opcode == RESET_EVENTS) {
  6591. pr_debug("%s: Reset event received in Voice service\n",
  6592. __func__);
  6593. if (common.mvs_info.ssr_cb) {
  6594. pr_debug("%s: Informing reset event to VoIP\n",
  6595. __func__);
  6596. common.mvs_info.ssr_cb(data->opcode,
  6597. common.mvs_info.private_data);
  6598. }
  6599. apr_reset(c->apr_q6_mvm);
  6600. c->apr_q6_mvm = NULL;
  6601. /* clean up memory handle */
  6602. c->cal_mem_handle = 0;
  6603. c->rtac_mem_handle = 0;
  6604. cal_utils_clear_cal_block_q6maps(MAX_VOICE_CAL_TYPES,
  6605. common.cal_data);
  6606. rtac_clear_mapping(VOICE_RTAC_CAL);
  6607. /* Sub-system restart is applicable to all sessions. */
  6608. for (i = 0; i < MAX_VOC_SESSIONS; i++) {
  6609. c->voice[i].mvm_handle = 0;
  6610. c->voice[i].shmem_info.mem_handle = 0;
  6611. }
  6612. /* Free the ION memory and clear handles for Source Tracking */
  6613. if (is_source_tracking_shared_memomry_allocated()) {
  6614. msm_audio_ion_free(
  6615. common.source_tracking_sh_mem.sh_mem_block.dma_buf);
  6616. common.source_tracking_sh_mem.mem_handle = 0;
  6617. common.source_tracking_sh_mem.sh_mem_block.dma_buf =
  6618. NULL;
  6619. }
  6620. /* clean up srvcc rec flag */
  6621. c->srvcc_rec_flag = false;
  6622. voc_set_error_state(data->reset_proc);
  6623. return 0;
  6624. }
  6625. pr_debug("%s: session_idx 0x%x\n", __func__, data->dest_port);
  6626. v = voice_get_session_by_idx(data->dest_port);
  6627. if (v == NULL) {
  6628. pr_err("%s: v is NULL\n", __func__);
  6629. return -EINVAL;
  6630. }
  6631. if (data->opcode == APR_BASIC_RSP_RESULT) {
  6632. if (data->payload_size >= sizeof(ptr[0]) * 2) {
  6633. ptr = data->payload;
  6634. pr_debug("%x %x\n", ptr[0], ptr[1]);
  6635. /* ping mvm service ACK */
  6636. switch (ptr[0]) {
  6637. case VSS_IMVM_CMD_CREATE_PASSIVE_CONTROL_SESSION:
  6638. case VSS_IMVM_CMD_CREATE_FULL_CONTROL_SESSION:
  6639. /* Passive session is used for CS call
  6640. * Full session is used for VoIP call.
  6641. */
  6642. pr_debug("%s: cmd = 0x%x\n", __func__, ptr[0]);
  6643. if (!ptr[1]) {
  6644. pr_debug("%s: MVM handle is %d\n",
  6645. __func__, data->src_port);
  6646. voice_set_mvm_handle(v, data->src_port);
  6647. } else
  6648. pr_err("got NACK for sending MVM create session\n");
  6649. v->mvm_state = CMD_STATUS_SUCCESS;
  6650. v->async_err = ptr[1];
  6651. wake_up(&v->mvm_wait);
  6652. break;
  6653. case VSS_IMVM_CMD_START_VOICE:
  6654. case VSS_IMVM_CMD_ATTACH_VOCPROC:
  6655. case VSS_IMVM_CMD_STOP_VOICE:
  6656. case VSS_IMVM_CMD_DETACH_VOCPROC:
  6657. case VSS_ISTREAM_CMD_SET_TTY_MODE:
  6658. case APRV2_IBASIC_CMD_DESTROY_SESSION:
  6659. case VSS_IMVM_CMD_ATTACH_STREAM:
  6660. case VSS_IMVM_CMD_DETACH_STREAM:
  6661. case VSS_ICOMMON_CMD_SET_NETWORK:
  6662. case VSS_ICOMMON_CMD_SET_VOICE_TIMING:
  6663. case VSS_IMVM_CMD_SET_POLICY_DUAL_CONTROL:
  6664. case VSS_IMVM_CMD_SET_CAL_NETWORK:
  6665. case VSS_IMVM_CMD_SET_CAL_MEDIA_TYPE:
  6666. case VSS_IMEMORY_CMD_MAP_PHYSICAL:
  6667. case VSS_IMEMORY_CMD_UNMAP:
  6668. case VSS_IMVM_CMD_PAUSE_VOICE:
  6669. case VSS_IMVM_CMD_STANDBY_VOICE:
  6670. case VSS_IHDVOICE_CMD_ENABLE:
  6671. case VSS_IHDVOICE_CMD_DISABLE:
  6672. case VSS_INOTIFY_CMD_LISTEN_FOR_EVENT_CLASS:
  6673. case VSS_INOTIFY_CMD_CANCEL_EVENT_CLASS:
  6674. pr_debug("%s: cmd = 0x%x\n", __func__, ptr[0]);
  6675. v->mvm_state = CMD_STATUS_SUCCESS;
  6676. v->async_err = ptr[1];
  6677. wake_up(&v->mvm_wait);
  6678. break;
  6679. case VSS_IVERSION_CMD_GET:
  6680. pr_debug("%s: Error retrieving CVD Version, error:%d\n",
  6681. __func__, ptr[1]);
  6682. strlcpy(common.cvd_version, CVD_VERSION_0_0,
  6683. sizeof(common.cvd_version));
  6684. pr_debug("%s: Fall back to default value, CVD Version = %s\n",
  6685. __func__, common.cvd_version);
  6686. v->mvm_state = CMD_STATUS_SUCCESS;
  6687. v->async_err = ptr[1];
  6688. wake_up(&v->mvm_wait);
  6689. break;
  6690. default:
  6691. pr_debug("%s: not match cmd = 0x%x\n",
  6692. __func__, ptr[0]);
  6693. break;
  6694. }
  6695. }
  6696. } else if (data->opcode == VSS_IMEMORY_RSP_MAP) {
  6697. pr_debug("%s, Revd VSS_IMEMORY_RSP_MAP response\n", __func__);
  6698. if (data->payload_size < sizeof(ptr[0])) {
  6699. pr_err("%s: payload has invalid size[%d]\n", __func__,
  6700. data->payload_size);
  6701. return -EINVAL;
  6702. }
  6703. if (data->token == VOIP_MEM_MAP_TOKEN) {
  6704. ptr = data->payload;
  6705. if (ptr[0]) {
  6706. v->shmem_info.mem_handle = ptr[0];
  6707. pr_debug("%s: shared mem_handle: 0x[%x]\n",
  6708. __func__, v->shmem_info.mem_handle);
  6709. v->mvm_state = CMD_STATUS_SUCCESS;
  6710. wake_up(&v->mvm_wait);
  6711. }
  6712. } else if (data->payload_size &&
  6713. data->token == VOC_CAL_MEM_MAP_TOKEN) {
  6714. ptr = data->payload;
  6715. if (ptr[0]) {
  6716. c->cal_mem_handle = ptr[0];
  6717. pr_debug("%s: cal mem handle 0x%x\n",
  6718. __func__, c->cal_mem_handle);
  6719. v->mvm_state = CMD_STATUS_SUCCESS;
  6720. wake_up(&v->mvm_wait);
  6721. }
  6722. } else if (data->payload_size &&
  6723. data->token == VOC_VOICE_HOST_PCM_MAP_TOKEN) {
  6724. ptr = data->payload;
  6725. if (ptr[0]) {
  6726. common.voice_host_pcm_mem_handle = ptr[0];
  6727. pr_debug("%s: vhpcm mem handle 0x%x\n",
  6728. __func__,
  6729. common.voice_host_pcm_mem_handle);
  6730. v->mvm_state = CMD_STATUS_SUCCESS;
  6731. wake_up(&v->mvm_wait);
  6732. }
  6733. } else if (data->payload_size &&
  6734. data->token == VOC_RTAC_MEM_MAP_TOKEN) {
  6735. ptr = data->payload;
  6736. if (ptr[0]) {
  6737. c->rtac_mem_handle = ptr[0];
  6738. pr_debug("%s: cal mem handle 0x%x\n",
  6739. __func__, c->rtac_mem_handle);
  6740. v->mvm_state = CMD_STATUS_SUCCESS;
  6741. wake_up(&v->mvm_wait);
  6742. }
  6743. } else if (data->payload_size &&
  6744. data->token == VOC_SOURCE_TRACKING_MEM_MAP_TOKEN) {
  6745. ptr = data->payload;
  6746. if (ptr[0]) {
  6747. common.source_tracking_sh_mem.mem_handle =
  6748. ptr[0];
  6749. pr_debug("%s: Source Tracking shared mem handle 0x%x\n",
  6750. __func__,
  6751. common.source_tracking_sh_mem.mem_handle);
  6752. v->mvm_state = CMD_STATUS_SUCCESS;
  6753. wake_up(&v->mvm_wait);
  6754. }
  6755. } else {
  6756. pr_err("%s: Unknown mem map token %d\n",
  6757. __func__, data->token);
  6758. }
  6759. } else if (data->opcode == VSS_IVERSION_RSP_GET) {
  6760. pr_debug("%s: Received VSS_IVERSION_RSP_GET\n", __func__);
  6761. if (data->payload_size) {
  6762. min_payload_size = min_t(u32, (int)data->payload_size,
  6763. CVD_VERSION_STRING_MAX_SIZE);
  6764. version_rsp =
  6765. (struct vss_iversion_rsp_get_t *)data->payload;
  6766. memcpy(common.cvd_version, version_rsp->version,
  6767. min_payload_size);
  6768. common.cvd_version[min_payload_size - 1] = '\0';
  6769. pr_debug("%s: CVD Version = %s\n",
  6770. __func__, common.cvd_version);
  6771. v->mvm_state = CMD_STATUS_SUCCESS;
  6772. wake_up(&v->mvm_wait);
  6773. }
  6774. } else if (data->opcode == VSS_ICOMMON_EVT_VOICE_ACTIVITY_UPDATE) {
  6775. if (data->payload_size == sizeof(struct vss_evt_voice_activity)) {
  6776. voice_act_update =
  6777. (struct vss_evt_voice_activity *)
  6778. data->payload;
  6779. /* Drop notifications other than Mic Break */
  6780. if ((voice_act_update->activity
  6781. != VSS_ICOMMON_VOICE_ACTIVITY_MIC_BREAK)
  6782. && (voice_act_update->activity
  6783. != VSS_ICOMMON_VOICE_ACITIVTY_MIC_UNBREAK))
  6784. return 0;
  6785. switch (voice_act_update->activity) {
  6786. case VSS_ICOMMON_VOICE_ACTIVITY_MIC_BREAK:
  6787. v->mic_break_status = true;
  6788. break;
  6789. case VSS_ICOMMON_VOICE_ACITIVTY_MIC_UNBREAK:
  6790. v->mic_break_status = false;
  6791. break;
  6792. }
  6793. if (c->mic_break_enable)
  6794. schedule_work(&(v->voice_mic_break_work));
  6795. }
  6796. }
  6797. return 0;
  6798. }
  6799. static int32_t qdsp_cvs_callback(struct apr_client_data *data, void *priv)
  6800. {
  6801. uint32_t *ptr = NULL;
  6802. struct common_data *c = NULL;
  6803. struct voice_data *v = NULL;
  6804. int i = 0;
  6805. if ((data == NULL) || (priv == NULL)) {
  6806. pr_err("%s: data or priv is NULL\n", __func__);
  6807. return -EINVAL;
  6808. }
  6809. c = priv;
  6810. pr_debug("%s: session_id 0x%x\n", __func__, data->dest_port);
  6811. pr_debug("%s: Payload Length = %d, opcode=%x\n", __func__,
  6812. data->payload_size, data->opcode);
  6813. if (data->opcode == RESET_EVENTS) {
  6814. pr_debug("%s: Reset event received in Voice service\n",
  6815. __func__);
  6816. apr_reset(c->apr_q6_cvs);
  6817. c->apr_q6_cvs = NULL;
  6818. /* Sub-system restart is applicable to all sessions. */
  6819. for (i = 0; i < MAX_VOC_SESSIONS; i++)
  6820. c->voice[i].cvs_handle = 0;
  6821. cal_utils_clear_cal_block_q6maps(MAX_VOICE_CAL_TYPES,
  6822. common.cal_data);
  6823. /* Free the ION memory and clear handles for Source Tracking */
  6824. if (is_source_tracking_shared_memomry_allocated()) {
  6825. msm_audio_ion_free(
  6826. common.source_tracking_sh_mem.sh_mem_block.dma_buf);
  6827. common.source_tracking_sh_mem.mem_handle = 0;
  6828. common.source_tracking_sh_mem.sh_mem_block.dma_buf =
  6829. NULL;
  6830. }
  6831. voc_set_error_state(data->reset_proc);
  6832. return 0;
  6833. }
  6834. v = voice_get_session_by_idx(data->dest_port);
  6835. if (v == NULL) {
  6836. pr_err("%s: v is NULL\n", __func__);
  6837. return -EINVAL;
  6838. }
  6839. if (data->opcode == APR_BASIC_RSP_RESULT) {
  6840. if (data->payload_size) {
  6841. ptr = data->payload;
  6842. pr_debug("%x %x\n", ptr[0], ptr[1]);
  6843. if (ptr[1] != 0) {
  6844. pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
  6845. __func__, ptr[0], ptr[1]);
  6846. }
  6847. /*response from CVS */
  6848. switch (ptr[0]) {
  6849. case VSS_ISTREAM_CMD_CREATE_PASSIVE_CONTROL_SESSION:
  6850. case VSS_ISTREAM_CMD_CREATE_FULL_CONTROL_SESSION:
  6851. if (!ptr[1]) {
  6852. pr_debug("%s: CVS handle is %d\n",
  6853. __func__, data->src_port);
  6854. voice_set_cvs_handle(v, data->src_port);
  6855. } else
  6856. pr_err("got NACK for sending CVS create session\n");
  6857. v->cvs_state = CMD_STATUS_SUCCESS;
  6858. v->async_err = ptr[1];
  6859. wake_up(&v->cvs_wait);
  6860. break;
  6861. case VSS_IVOLUME_CMD_MUTE_V2:
  6862. case VSS_ISTREAM_CMD_SET_MEDIA_TYPE:
  6863. case VSS_ISTREAM_CMD_VOC_AMR_SET_ENC_RATE:
  6864. case VSS_ISTREAM_CMD_VOC_AMRWB_SET_ENC_RATE:
  6865. case VSS_ISTREAM_CMD_SET_ENC_DTX_MODE:
  6866. case VSS_ISTREAM_CMD_CDMA_SET_ENC_MINMAX_RATE:
  6867. case APRV2_IBASIC_CMD_DESTROY_SESSION:
  6868. case VSS_ISTREAM_CMD_REGISTER_CALIBRATION_DATA_V2:
  6869. case VSS_ISTREAM_CMD_DEREGISTER_CALIBRATION_DATA:
  6870. case VSS_ISTREAM_CMD_REGISTER_STATIC_CALIBRATION_DATA:
  6871. case VSS_ISTREAM_CMD_DEREGISTER_STATIC_CALIBRATION_DATA:
  6872. case VSS_ICOMMON_CMD_MAP_MEMORY:
  6873. case VSS_ICOMMON_CMD_UNMAP_MEMORY:
  6874. case VSS_ICOMMON_CMD_SET_UI_PROPERTY:
  6875. case VSS_ICOMMON_CMD_SET_UI_PROPERTY_V2:
  6876. case VSS_IPLAYBACK_CMD_START:
  6877. case VSS_IPLAYBACK_CMD_STOP:
  6878. case VSS_IRECORD_CMD_START:
  6879. case VSS_IRECORD_CMD_STOP:
  6880. case VSS_ISTREAM_CMD_SET_PACKET_EXCHANGE_MODE:
  6881. case VSS_ISTREAM_CMD_SET_OOB_PACKET_EXCHANGE_CONFIG:
  6882. case VSS_ISTREAM_CMD_SET_RX_DTMF_DETECTION:
  6883. pr_debug("%s: cmd = 0x%x\n", __func__, ptr[0]);
  6884. v->cvs_state = CMD_STATUS_SUCCESS;
  6885. v->async_err = ptr[1];
  6886. wake_up(&v->cvs_wait);
  6887. break;
  6888. case VSS_ICOMMON_CMD_SET_PARAM_V2:
  6889. case VSS_ICOMMON_CMD_SET_PARAM_V3:
  6890. pr_debug("%s: VSS_ICOMMON_CMD_SET_PARAM\n",
  6891. __func__);
  6892. rtac_make_voice_callback(RTAC_CVS, ptr,
  6893. data->payload_size);
  6894. break;
  6895. case VSS_ICOMMON_CMD_GET_PARAM_V2:
  6896. case VSS_ICOMMON_CMD_GET_PARAM_V3:
  6897. pr_debug("%s: VSS_ICOMMON_CMD_GET_PARAM_V2\n",
  6898. __func__);
  6899. /* Should only come here if there is an APR */
  6900. /* error or malformed APR packet. Otherwise */
  6901. /* response will be returned as */
  6902. /* VSS_ICOMMON_RSP_GET_PARAM */
  6903. if (ptr[1] != 0) {
  6904. pr_err("%s: CVP get param error = %d, resuming\n",
  6905. __func__, ptr[1]);
  6906. rtac_make_voice_callback(RTAC_CVP,
  6907. data->payload,
  6908. data->payload_size);
  6909. }
  6910. break;
  6911. default:
  6912. pr_debug("%s: cmd = 0x%x\n", __func__, ptr[0]);
  6913. break;
  6914. }
  6915. }
  6916. } else if (data->opcode ==
  6917. VSS_ISTREAM_EVT_OOB_NOTIFY_ENC_BUFFER_READY) {
  6918. int ret = 0;
  6919. u16 cvs_handle;
  6920. uint32_t *cvs_voc_pkt;
  6921. struct cvs_enc_buffer_consumed_cmd send_enc_buf_consumed_cmd;
  6922. void *apr_cvs;
  6923. pr_debug("Encoder buffer is ready\n");
  6924. apr_cvs = common.apr_q6_cvs;
  6925. if (!apr_cvs) {
  6926. pr_err("%s: apr_cvs is NULL\n", __func__);
  6927. return -EINVAL;
  6928. }
  6929. cvs_handle = voice_get_cvs_handle(v);
  6930. send_enc_buf_consumed_cmd.hdr.hdr_field =
  6931. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  6932. APR_HDR_LEN(APR_HDR_SIZE),
  6933. APR_PKT_VER);
  6934. send_enc_buf_consumed_cmd.hdr.pkt_size =
  6935. APR_PKT_SIZE(APR_HDR_SIZE,
  6936. sizeof(send_enc_buf_consumed_cmd) - APR_HDR_SIZE);
  6937. send_enc_buf_consumed_cmd.hdr.src_port =
  6938. voice_get_idx_for_session(v->session_id);
  6939. send_enc_buf_consumed_cmd.hdr.dest_port = cvs_handle;
  6940. send_enc_buf_consumed_cmd.hdr.token = 0;
  6941. send_enc_buf_consumed_cmd.hdr.opcode =
  6942. VSS_ISTREAM_EVT_OOB_NOTIFY_ENC_BUFFER_CONSUMED;
  6943. cvs_voc_pkt = v->shmem_info.sh_buf.buf[1].data;
  6944. if (cvs_voc_pkt != NULL && common.mvs_info.ul_cb != NULL) {
  6945. if (v->shmem_info.sh_buf.buf[1].size <
  6946. ((3 * sizeof(uint32_t)) + cvs_voc_pkt[2])) {
  6947. pr_err("%s: invalid voc pkt size\n", __func__);
  6948. return -EINVAL;
  6949. }
  6950. /* cvs_voc_pkt[0] contains tx timestamp */
  6951. common.mvs_info.ul_cb((uint8_t *)&cvs_voc_pkt[3],
  6952. cvs_voc_pkt[2],
  6953. cvs_voc_pkt[0],
  6954. common.mvs_info.private_data);
  6955. } else
  6956. pr_err("%s: cvs_voc_pkt or ul_cb is NULL\n", __func__);
  6957. ret = apr_send_pkt(apr_cvs,
  6958. (uint32_t *) &send_enc_buf_consumed_cmd);
  6959. if (ret < 0) {
  6960. pr_err("%s: Err send ENC_BUF_CONSUMED_NOTIFY %d\n",
  6961. __func__, ret);
  6962. goto fail;
  6963. }
  6964. } else if (data->opcode == VSS_ISTREAM_EVT_SEND_ENC_BUFFER) {
  6965. pr_debug("Recd VSS_ISTREAM_EVT_SEND_ENC_BUFFER\n");
  6966. } else if (data->opcode ==
  6967. VSS_ISTREAM_EVT_OOB_NOTIFY_DEC_BUFFER_REQUEST) {
  6968. int ret = 0;
  6969. u16 cvs_handle;
  6970. uint32_t *cvs_voc_pkt;
  6971. struct cvs_dec_buffer_ready_cmd send_dec_buf;
  6972. void *apr_cvs;
  6973. apr_cvs = common.apr_q6_cvs;
  6974. if (!apr_cvs) {
  6975. pr_err("%s: apr_cvs is NULL\n", __func__);
  6976. return -EINVAL;
  6977. }
  6978. cvs_handle = voice_get_cvs_handle(v);
  6979. send_dec_buf.hdr.hdr_field =
  6980. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  6981. APR_HDR_LEN(APR_HDR_SIZE),
  6982. APR_PKT_VER);
  6983. send_dec_buf.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  6984. sizeof(send_dec_buf) - APR_HDR_SIZE);
  6985. send_dec_buf.hdr.src_port =
  6986. voice_get_idx_for_session(v->session_id);
  6987. send_dec_buf.hdr.dest_port = cvs_handle;
  6988. send_dec_buf.hdr.token = 0;
  6989. send_dec_buf.hdr.opcode =
  6990. VSS_ISTREAM_EVT_OOB_NOTIFY_DEC_BUFFER_READY;
  6991. cvs_voc_pkt = (uint32_t *)(v->shmem_info.sh_buf.buf[0].data);
  6992. if (cvs_voc_pkt != NULL && common.mvs_info.dl_cb != NULL) {
  6993. /* Set timestamp to 0 and advance the pointer */
  6994. cvs_voc_pkt[0] = 0;
  6995. /* Set media_type and advance the pointer */
  6996. cvs_voc_pkt[1] = common.mvs_info.media_type;
  6997. common.mvs_info.dl_cb(
  6998. (uint8_t *)&cvs_voc_pkt[2],
  6999. common.mvs_info.private_data);
  7000. ret = apr_send_pkt(apr_cvs, (uint32_t *) &send_dec_buf);
  7001. if (ret < 0) {
  7002. pr_err("%s: Err send DEC_BUF_READY_NOTIFI %d\n",
  7003. __func__, ret);
  7004. goto fail;
  7005. }
  7006. } else {
  7007. pr_debug("%s: voc_pkt or dl_cb is NULL\n", __func__);
  7008. goto fail;
  7009. }
  7010. } else if (data->opcode == VSS_ISTREAM_EVT_REQUEST_DEC_BUFFER) {
  7011. pr_debug("Recd VSS_ISTREAM_EVT_REQUEST_DEC_BUFFER\n");
  7012. } else if (data->opcode == VSS_ISTREAM_EVT_SEND_DEC_BUFFER) {
  7013. pr_debug("Send dec buf resp\n");
  7014. } else if (data->opcode == APR_RSP_ACCEPTED) {
  7015. ptr = data->payload;
  7016. if (ptr[0])
  7017. pr_debug("%s: APR_RSP_ACCEPTED for 0x%x:\n",
  7018. __func__, ptr[0]);
  7019. } else if (data->opcode == VSS_ISTREAM_EVT_NOT_READY) {
  7020. pr_debug("Recd VSS_ISTREAM_EVT_NOT_READY\n");
  7021. } else if (data->opcode == VSS_ISTREAM_EVT_READY) {
  7022. pr_debug("Recd VSS_ISTREAM_EVT_READY\n");
  7023. } else if (data->opcode == VSS_ICOMMON_RSP_GET_PARAM ||
  7024. data->opcode == VSS_ICOMMON_RSP_GET_PARAM_V3) {
  7025. pr_debug("%s: VSS_ICOMMON_RSP_GET_PARAM\n", __func__);
  7026. ptr = data->payload;
  7027. if (ptr[0] != 0) {
  7028. pr_err("%s: VSS_ICOMMON_RSP_GET_PARAM returned error = 0x%x\n",
  7029. __func__, ptr[0]);
  7030. }
  7031. rtac_make_voice_callback(RTAC_CVS, data->payload,
  7032. data->payload_size);
  7033. } else if (data->opcode == VSS_ISTREAM_EVT_RX_DTMF_DETECTED) {
  7034. struct vss_istream_evt_rx_dtmf_detected *dtmf_rx_detected;
  7035. uint32_t *voc_pkt = data->payload;
  7036. uint32_t pkt_len = data->payload_size;
  7037. if ((voc_pkt != NULL) &&
  7038. (pkt_len ==
  7039. sizeof(struct vss_istream_evt_rx_dtmf_detected))) {
  7040. dtmf_rx_detected =
  7041. (struct vss_istream_evt_rx_dtmf_detected *) voc_pkt;
  7042. pr_debug("RX_DTMF_DETECTED low_freq=%d high_freq=%d\n",
  7043. dtmf_rx_detected->low_freq,
  7044. dtmf_rx_detected->high_freq);
  7045. if (c->dtmf_info.dtmf_rx_ul_cb)
  7046. c->dtmf_info.dtmf_rx_ul_cb((uint8_t *)voc_pkt,
  7047. voc_get_session_name(v->session_id),
  7048. c->dtmf_info.private_data);
  7049. } else {
  7050. pr_err("Invalid packet\n");
  7051. }
  7052. } else
  7053. pr_debug("Unknown opcode 0x%x\n", data->opcode);
  7054. fail:
  7055. return 0;
  7056. }
  7057. static int32_t qdsp_cvp_callback(struct apr_client_data *data, void *priv)
  7058. {
  7059. uint32_t *ptr = NULL;
  7060. struct common_data *c = NULL;
  7061. struct voice_data *v = NULL;
  7062. int i = 0;
  7063. if ((data == NULL) || (priv == NULL)) {
  7064. pr_err("%s: data or priv is NULL\n", __func__);
  7065. return -EINVAL;
  7066. }
  7067. c = priv;
  7068. if (data->opcode == RESET_EVENTS) {
  7069. pr_debug("%s: Reset event received in Voice service\n",
  7070. __func__);
  7071. apr_reset(c->apr_q6_cvp);
  7072. c->apr_q6_cvp = NULL;
  7073. cal_utils_clear_cal_block_q6maps(MAX_VOICE_CAL_TYPES,
  7074. common.cal_data);
  7075. /* Sub-system restart is applicable to all sessions. */
  7076. for (i = 0; i < MAX_VOC_SESSIONS; i++)
  7077. c->voice[i].cvp_handle = 0;
  7078. /*
  7079. * Free the ION memory and clear handles for
  7080. * Source Tracking
  7081. */
  7082. if (is_source_tracking_shared_memomry_allocated()) {
  7083. msm_audio_ion_free(
  7084. common.source_tracking_sh_mem.sh_mem_block.dma_buf);
  7085. common.source_tracking_sh_mem.mem_handle = 0;
  7086. common.source_tracking_sh_mem.sh_mem_block.dma_buf =
  7087. NULL;
  7088. }
  7089. voc_set_error_state(data->reset_proc);
  7090. return 0;
  7091. }
  7092. v = voice_get_session_by_idx(data->dest_port);
  7093. if (v == NULL) {
  7094. pr_err("%s: v is NULL\n", __func__);
  7095. return -EINVAL;
  7096. }
  7097. if (data->opcode == APR_BASIC_RSP_RESULT) {
  7098. if (data->payload_size >= (2 * sizeof(uint32_t))) {
  7099. ptr = data->payload;
  7100. pr_debug("%x %x\n", ptr[0], ptr[1]);
  7101. if (ptr[1] != 0) {
  7102. pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
  7103. __func__, ptr[0], ptr[1]);
  7104. }
  7105. switch (ptr[0]) {
  7106. case VSS_IVOCPROC_CMD_CREATE_FULL_CONTROL_SESSION_V2:
  7107. case VSS_IVOCPROC_CMD_CREATE_FULL_CONTROL_SESSION_V3:
  7108. /*response from CVP */
  7109. pr_debug("%s: cmd = 0x%x\n", __func__, ptr[0]);
  7110. if (!ptr[1]) {
  7111. voice_set_cvp_handle(v, data->src_port);
  7112. pr_debug("status: %d, cvphdl=%d\n",
  7113. ptr[1], data->src_port);
  7114. } else
  7115. pr_err("got NACK from CVP create session response\n");
  7116. v->cvp_state = CMD_STATUS_SUCCESS;
  7117. v->async_err = ptr[1];
  7118. wake_up(&v->cvp_wait);
  7119. break;
  7120. case VSS_IVOCPROC_CMD_SET_DEVICE_V2:
  7121. case VSS_IVOCPROC_CMD_SET_DEVICE_V3:
  7122. case VSS_IVOLUME_CMD_SET_STEP:
  7123. case VSS_IVOCPROC_CMD_ENABLE:
  7124. case VSS_IVOCPROC_CMD_DISABLE:
  7125. case APRV2_IBASIC_CMD_DESTROY_SESSION:
  7126. case VSS_IVOCPROC_CMD_REGISTER_VOL_CALIBRATION_DATA:
  7127. case VSS_IVOCPROC_CMD_DEREGISTER_VOL_CALIBRATION_DATA:
  7128. case VSS_IVOCPROC_CMD_REGISTER_CALIBRATION_DATA_V2:
  7129. case VSS_IVOCPROC_CMD_DEREGISTER_CALIBRATION_DATA:
  7130. case VSS_IVOCPROC_CMD_REGISTER_DYNAMIC_CALIBRATION_DATA:
  7131. case VSS_IVOCPROC_CMD_DEREGISTER_DYNAMIC_CALIBRATION_DATA:
  7132. case VSS_IVOCPROC_CMD_REGISTER_STATIC_CALIBRATION_DATA:
  7133. case VSS_IVOCPROC_CMD_DEREGISTER_STATIC_CALIBRATION_DATA:
  7134. case VSS_IVOCPROC_CMD_REGISTER_DEVICE_CONFIG:
  7135. case VSS_IVOCPROC_CMD_DEREGISTER_DEVICE_CONFIG:
  7136. case VSS_ICOMMON_CMD_MAP_MEMORY:
  7137. case VSS_ICOMMON_CMD_UNMAP_MEMORY:
  7138. case VSS_IVOLUME_CMD_MUTE_V2:
  7139. case VSS_IVPCM_CMD_START_V2:
  7140. case VSS_IVPCM_CMD_STOP:
  7141. case VSS_IVOCPROC_CMD_TOPOLOGY_SET_DEV_CHANNELS:
  7142. case VSS_IVOCPROC_CMD_TOPOLOGY_COMMIT:
  7143. v->cvp_state = CMD_STATUS_SUCCESS;
  7144. v->async_err = ptr[1];
  7145. wake_up(&v->cvp_wait);
  7146. break;
  7147. case VSS_IVPCM_EVT_PUSH_BUFFER_V2:
  7148. break;
  7149. case VSS_ICOMMON_CMD_SET_PARAM_V2:
  7150. case VSS_ICOMMON_CMD_SET_PARAM_V3:
  7151. switch (data->token) {
  7152. case VOC_SET_MEDIA_FORMAT_PARAM_TOKEN:
  7153. case VOC_GENERIC_SET_PARAM_TOKEN:
  7154. pr_debug("%s: VSS_ICOMMON_CMD_SET_PARAM called by voice_send_cvp_media_format_cmd\n",
  7155. __func__);
  7156. v->cvp_state = CMD_STATUS_SUCCESS;
  7157. v->async_err = ptr[1];
  7158. wake_up(&v->cvp_wait);
  7159. break;
  7160. case VOC_RTAC_SET_PARAM_TOKEN:
  7161. pr_debug("%s: VSS_ICOMMON_CMD_SET_PARAM called by rtac\n",
  7162. __func__);
  7163. rtac_make_voice_callback(
  7164. RTAC_CVP, ptr,
  7165. data->payload_size);
  7166. break;
  7167. default:
  7168. pr_debug("%s: invalid token for command VSS_ICOMMON_CMD_SET_PARAM: %d\n",
  7169. __func__, data->token);
  7170. break;
  7171. }
  7172. break;
  7173. case VSS_ICOMMON_CMD_GET_PARAM_V2:
  7174. case VSS_ICOMMON_CMD_GET_PARAM_V3:
  7175. pr_debug("%s: VSS_ICOMMON_CMD_GET_PARAM_V2\n",
  7176. __func__);
  7177. /* Should only come here if there is an APR */
  7178. /* error or malformed APR packet. Otherwise */
  7179. /* response will be returned as */
  7180. /* VSS_ICOMMON_RSP_GET_PARAM */
  7181. if (ptr[1] != 0) {
  7182. pr_err("%s: CVP get param error = %d, resuming\n",
  7183. __func__, ptr[1]);
  7184. rtac_make_voice_callback(RTAC_CVP,
  7185. data->payload,
  7186. data->payload_size);
  7187. }
  7188. break;
  7189. case VSS_ISOUNDFOCUS_CMD_SET_SECTORS:
  7190. if (!ptr[1])
  7191. common.is_sound_focus_resp_success =
  7192. true;
  7193. else
  7194. common.is_sound_focus_resp_success =
  7195. false;
  7196. v->cvp_state = CMD_STATUS_SUCCESS;
  7197. v->async_err = ptr[1];
  7198. wake_up(&v->cvp_wait);
  7199. break;
  7200. case VSS_ISOUNDFOCUS_CMD_GET_SECTORS:
  7201. /*
  7202. * Should only come here if there is an error
  7203. * response received from ADSP. Otherwise
  7204. * response will be returned as
  7205. * VSS_ISOUNDFOCUS_RSP_GET_SECTORS
  7206. */
  7207. pr_err("%s: VSS_ISOUNDFOCUS_CMD_GET_SECTORS failed\n",
  7208. __func__);
  7209. common.is_sound_focus_resp_success = false;
  7210. v->cvp_state = CMD_STATUS_SUCCESS;
  7211. v->async_err = ptr[1];
  7212. wake_up(&v->cvp_wait);
  7213. break;
  7214. case VSS_ISOURCETRACK_CMD_GET_ACTIVITY:
  7215. if (!ptr[1]) {
  7216. /* Read data from shared memory */
  7217. memcpy(&common.sourceTrackingResponse,
  7218. common.source_tracking_sh_mem.
  7219. sh_mem_block.data,
  7220. sizeof(struct
  7221. vss_isourcetrack_activity_data_t));
  7222. common.is_source_tracking_resp_success =
  7223. true;
  7224. } else {
  7225. common.is_source_tracking_resp_success =
  7226. false;
  7227. pr_err("%s: Error received for source tracking params\n",
  7228. __func__);
  7229. }
  7230. v->cvp_state = CMD_STATUS_SUCCESS;
  7231. v->async_err = ptr[1];
  7232. wake_up(&v->cvp_wait);
  7233. break;
  7234. default:
  7235. pr_debug("%s: not match cmd = 0x%x\n",
  7236. __func__, ptr[0]);
  7237. break;
  7238. }
  7239. }
  7240. } else if (data->opcode == VSS_ICOMMON_RSP_GET_PARAM ||
  7241. data->opcode == VSS_ICOMMON_RSP_GET_PARAM_V3) {
  7242. pr_debug("%s: VSS_ICOMMON_RSP_GET_PARAM\n", __func__);
  7243. ptr = data->payload;
  7244. if (ptr[0] != 0) {
  7245. pr_err("%s: VSS_ICOMMON_RSP_GET_PARAM returned error = 0x%x\n",
  7246. __func__, ptr[0]);
  7247. }
  7248. rtac_make_voice_callback(RTAC_CVP, data->payload,
  7249. data->payload_size);
  7250. } else if (data->opcode == VSS_IVPCM_EVT_NOTIFY_V2) {
  7251. if ((data->payload != NULL) && data->payload_size ==
  7252. sizeof(struct vss_ivpcm_evt_notify_v2_t) &&
  7253. common.hostpcm_info.hostpcm_evt_cb != NULL) {
  7254. common.hostpcm_info.hostpcm_evt_cb(data->payload,
  7255. voc_get_session_name(v->session_id),
  7256. common.hostpcm_info.private_data);
  7257. }
  7258. } else if (data->opcode == VSS_ISOUNDFOCUS_RSP_GET_SECTORS) {
  7259. if (data->payload && (data->payload_size ==
  7260. sizeof(struct vss_isoundfocus_rsp_get_sectors_t))) {
  7261. common.is_sound_focus_resp_success = true;
  7262. memcpy(&common.soundFocusResponse,
  7263. (struct vss_isoundfocus_rsp_get_sectors_t *)
  7264. data->payload,
  7265. sizeof(struct
  7266. vss_isoundfocus_rsp_get_sectors_t));
  7267. } else {
  7268. common.is_sound_focus_resp_success = false;
  7269. pr_debug("%s: Invalid payload received from CVD\n",
  7270. __func__);
  7271. }
  7272. v->cvp_state = CMD_STATUS_SUCCESS;
  7273. wake_up(&v->cvp_wait);
  7274. }
  7275. return 0;
  7276. }
  7277. static int voice_free_oob_shared_mem(void)
  7278. {
  7279. int rc = 0;
  7280. int cnt = 0;
  7281. int bufcnt = NUM_OF_BUFFERS;
  7282. struct voice_data *v = voice_get_session(
  7283. common.voice[VOC_PATH_FULL].session_id);
  7284. mutex_lock(&common.common_lock);
  7285. if (v == NULL) {
  7286. pr_err("%s: v is NULL\n", __func__);
  7287. rc = -EINVAL;
  7288. goto done;
  7289. }
  7290. rc = msm_audio_ion_free(v->shmem_info.sh_buf.dma_buf);
  7291. v->shmem_info.sh_buf.dma_buf = NULL;
  7292. if (rc < 0) {
  7293. pr_err("%s: Error:%d freeing memory\n", __func__, rc);
  7294. goto done;
  7295. }
  7296. while (cnt < bufcnt) {
  7297. v->shmem_info.sh_buf.buf[cnt].data = NULL;
  7298. v->shmem_info.sh_buf.buf[cnt].phys = 0;
  7299. cnt++;
  7300. }
  7301. v->shmem_info.sh_buf.dma_buf = NULL;
  7302. done:
  7303. mutex_unlock(&common.common_lock);
  7304. return rc;
  7305. }
  7306. static int voice_alloc_oob_shared_mem(void)
  7307. {
  7308. int cnt = 0;
  7309. int rc = 0;
  7310. size_t len;
  7311. void *mem_addr = NULL;
  7312. dma_addr_t phys;
  7313. int bufsz = BUFFER_BLOCK_SIZE;
  7314. int bufcnt = NUM_OF_BUFFERS;
  7315. struct voice_data *v = voice_get_session(
  7316. common.voice[VOC_PATH_FULL].session_id);
  7317. mutex_lock(&common.common_lock);
  7318. if (v == NULL) {
  7319. pr_err("%s: v is NULL\n", __func__);
  7320. rc = -EINVAL;
  7321. goto done;
  7322. }
  7323. rc = msm_audio_ion_alloc(&(v->shmem_info.sh_buf.dma_buf),
  7324. bufsz * bufcnt,
  7325. &phys, &len,
  7326. &mem_addr);
  7327. if (rc < 0) {
  7328. pr_err("%s: audio ION alloc failed, rc = %d\n",
  7329. __func__, rc);
  7330. goto done;
  7331. }
  7332. while (cnt < bufcnt) {
  7333. v->shmem_info.sh_buf.buf[cnt].data = mem_addr + (cnt * bufsz);
  7334. v->shmem_info.sh_buf.buf[cnt].phys = phys + (cnt * bufsz);
  7335. v->shmem_info.sh_buf.buf[cnt].size = bufsz;
  7336. cnt++;
  7337. }
  7338. pr_debug("%s buf[0].data:[%pK], buf[0].phys:[%pK], &buf[0].phys:[%pK],\n",
  7339. __func__,
  7340. (void *)v->shmem_info.sh_buf.buf[0].data,
  7341. &v->shmem_info.sh_buf.buf[0].phys,
  7342. (void *)&v->shmem_info.sh_buf.buf[0].phys);
  7343. pr_debug("%s: buf[1].data:[%pK], buf[1].phys[%pK], &buf[1].phys[%pK]\n",
  7344. __func__,
  7345. (void *)v->shmem_info.sh_buf.buf[1].data,
  7346. &v->shmem_info.sh_buf.buf[1].phys,
  7347. (void *)&v->shmem_info.sh_buf.buf[1].phys);
  7348. memset((void *)v->shmem_info.sh_buf.buf[0].data, 0, (bufsz * bufcnt));
  7349. done:
  7350. mutex_unlock(&common.common_lock);
  7351. return rc;
  7352. }
  7353. static int voice_alloc_oob_mem_table(void)
  7354. {
  7355. int rc = 0;
  7356. size_t len;
  7357. struct voice_data *v = voice_get_session(
  7358. common.voice[VOC_PATH_FULL].session_id);
  7359. mutex_lock(&common.common_lock);
  7360. if (v == NULL) {
  7361. pr_err("%s: v is NULL\n", __func__);
  7362. rc = -EINVAL;
  7363. goto done;
  7364. }
  7365. rc = msm_audio_ion_alloc(&(v->shmem_info.memtbl.dma_buf),
  7366. sizeof(struct vss_imemory_table_t),
  7367. &v->shmem_info.memtbl.phys,
  7368. &len,
  7369. &(v->shmem_info.memtbl.data));
  7370. if (rc < 0) {
  7371. pr_err("%s: audio ION alloc failed, rc = %d\n",
  7372. __func__, rc);
  7373. goto done;
  7374. }
  7375. v->shmem_info.memtbl.size = sizeof(struct vss_imemory_table_t);
  7376. pr_debug("%s data[%pK]phys[%pK][%pK]\n", __func__,
  7377. (void *)v->shmem_info.memtbl.data,
  7378. &v->shmem_info.memtbl.phys,
  7379. (void *)&v->shmem_info.memtbl.phys);
  7380. done:
  7381. mutex_unlock(&common.common_lock);
  7382. return rc;
  7383. }
  7384. /**
  7385. * voc_send_cvp_start_vocpcm -
  7386. * command to start voice hpcm
  7387. *
  7388. * @session_id: voice session ID to send this command
  7389. *
  7390. * Returns 0 on success or error on failure
  7391. */
  7392. int voc_send_cvp_start_vocpcm(uint32_t session_id,
  7393. struct vss_ivpcm_tap_point *vpcm_tp,
  7394. uint32_t no_of_tp)
  7395. {
  7396. struct cvp_start_cmd cvp_start_cmd;
  7397. int ret = 0;
  7398. void *apr_cvp;
  7399. u16 cvp_handle;
  7400. struct voice_data *v = voice_get_session(session_id);
  7401. int i = 0;
  7402. if (v == NULL) {
  7403. pr_err("%s: v is NULL\n", __func__);
  7404. ret = -EINVAL;
  7405. goto done;
  7406. }
  7407. apr_cvp = common.apr_q6_cvp;
  7408. if (!apr_cvp) {
  7409. pr_err("%s: apr_cvp is NULL.\n", __func__);
  7410. ret = -EINVAL;
  7411. goto done;
  7412. }
  7413. cvp_handle = voice_get_cvp_handle(v);
  7414. /* Fill the header */
  7415. cvp_start_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  7416. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  7417. cvp_start_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  7418. sizeof(struct vss_ivpcm_tap_point) * no_of_tp) +
  7419. sizeof(cvp_start_cmd.vpcm_start_cmd.num_tap_points) +
  7420. sizeof(cvp_start_cmd.vpcm_start_cmd.mem_handle);
  7421. cvp_start_cmd.hdr.src_port = voice_get_idx_for_session(v->session_id);
  7422. cvp_start_cmd.hdr.dest_port = cvp_handle;
  7423. cvp_start_cmd.hdr.token = 0;
  7424. cvp_start_cmd.hdr.opcode = VSS_IVPCM_CMD_START_V2;
  7425. for (i = 0; i < no_of_tp; i++) {
  7426. cvp_start_cmd.vpcm_start_cmd.tap_points[i].tap_point =
  7427. vpcm_tp[i].tap_point;
  7428. cvp_start_cmd.vpcm_start_cmd.tap_points[i].direction =
  7429. vpcm_tp[i].direction;
  7430. cvp_start_cmd.vpcm_start_cmd.tap_points[i].sampling_rate =
  7431. vpcm_tp[i].sampling_rate;
  7432. cvp_start_cmd.vpcm_start_cmd.tap_points[i].duration = 0;
  7433. }
  7434. cvp_start_cmd.vpcm_start_cmd.mem_handle =
  7435. common.voice_host_pcm_mem_handle;
  7436. cvp_start_cmd.vpcm_start_cmd.num_tap_points = no_of_tp;
  7437. v->cvp_state = CMD_STATUS_FAIL;
  7438. v->async_err = 0;
  7439. ret = apr_send_pkt(apr_cvp, (uint32_t *) &cvp_start_cmd);
  7440. if (ret < 0) {
  7441. pr_err("%s: Fail: sending vocpcm map memory,\n", __func__);
  7442. goto done;
  7443. }
  7444. ret = wait_event_timeout(v->cvp_wait,
  7445. (v->cvp_state == CMD_STATUS_SUCCESS),
  7446. msecs_to_jiffies(TIMEOUT_MS));
  7447. if (!ret) {
  7448. pr_err("%s: wait_event timeout\n", __func__);
  7449. goto done;
  7450. }
  7451. if (v->async_err > 0) {
  7452. pr_err("%s: DSP returned error[%s]\n",
  7453. __func__, adsp_err_get_err_str(
  7454. v->async_err));
  7455. ret = adsp_err_get_lnx_err_code(
  7456. v->async_err);
  7457. goto done;
  7458. }
  7459. done:
  7460. return ret;
  7461. }
  7462. EXPORT_SYMBOL(voc_send_cvp_start_vocpcm);
  7463. /**
  7464. * voc_send_cvp_stop_vocpcm -
  7465. * command to stop voice hpcm
  7466. *
  7467. * @session_id: voice session ID to send this command
  7468. *
  7469. * Returns 0 on success or error on failure
  7470. */
  7471. int voc_send_cvp_stop_vocpcm(uint32_t session_id)
  7472. {
  7473. struct cvp_command vpcm_stop_cmd;
  7474. int ret = 0;
  7475. void *apr_cvp;
  7476. u16 cvp_handle;
  7477. struct voice_data *v = voice_get_session(session_id);
  7478. if (v == NULL) {
  7479. pr_err("%s: v is NULL\n", __func__);
  7480. ret = -EINVAL;
  7481. goto done;
  7482. }
  7483. apr_cvp = common.apr_q6_cvp;
  7484. if (!apr_cvp) {
  7485. pr_err("%s: apr_cvp is NULL.\n", __func__);
  7486. ret = -EINVAL;
  7487. goto done;
  7488. }
  7489. cvp_handle = voice_get_cvp_handle(v);
  7490. /* fill in the header */
  7491. vpcm_stop_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  7492. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  7493. vpcm_stop_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  7494. sizeof(vpcm_stop_cmd) - APR_HDR_SIZE);
  7495. vpcm_stop_cmd.hdr.src_port = voice_get_idx_for_session(v->session_id);
  7496. vpcm_stop_cmd.hdr.dest_port = cvp_handle;
  7497. vpcm_stop_cmd.hdr.token = 0;
  7498. vpcm_stop_cmd.hdr.opcode = VSS_IVPCM_CMD_STOP;
  7499. v->cvp_state = CMD_STATUS_FAIL;
  7500. v->async_err = 0;
  7501. ret = apr_send_pkt(apr_cvp, (uint32_t *) &vpcm_stop_cmd);
  7502. if (ret < 0) {
  7503. pr_err("Fail: sending vocpcm stop,\n");
  7504. goto done;
  7505. }
  7506. ret = wait_event_timeout(v->cvp_wait,
  7507. (v->cvp_state == CMD_STATUS_SUCCESS),
  7508. msecs_to_jiffies(TIMEOUT_MS));
  7509. if (!ret) {
  7510. pr_err("%s: wait_event timeout\n", __func__);
  7511. goto done;
  7512. }
  7513. if (v->async_err > 0) {
  7514. pr_err("%s: DSP returned error[%s]\n",
  7515. __func__, adsp_err_get_err_str(
  7516. v->async_err));
  7517. ret = adsp_err_get_lnx_err_code(
  7518. v->async_err);
  7519. goto done;
  7520. }
  7521. done:
  7522. return ret;
  7523. }
  7524. EXPORT_SYMBOL(voc_send_cvp_stop_vocpcm);
  7525. /**
  7526. * voc_send_cvp_map_vocpcm_memory -
  7527. * command to map memory for voice hpcm
  7528. *
  7529. * @session_id: voice session ID to send this command
  7530. * @tp_mem_table: tap point memory table of hpcm
  7531. * paddr: Physical address of hpcm memory mapped area.
  7532. * bufsize: Buffer size of memory mapped area
  7533. *
  7534. * Returns 0 on success or error on failure
  7535. */
  7536. int voc_send_cvp_map_vocpcm_memory(uint32_t session_id,
  7537. struct mem_map_table *tp_mem_table,
  7538. phys_addr_t paddr, uint32_t bufsize)
  7539. {
  7540. return voice_map_memory_physical_cmd(voice_get_session(session_id),
  7541. tp_mem_table,
  7542. (dma_addr_t) paddr, bufsize,
  7543. VOC_VOICE_HOST_PCM_MAP_TOKEN);
  7544. }
  7545. EXPORT_SYMBOL(voc_send_cvp_map_vocpcm_memory);
  7546. /**
  7547. * voc_send_cvp_unmap_vocpcm_memory -
  7548. * command to unmap memory for voice hpcm
  7549. *
  7550. * @session_id: voice session ID to send this command
  7551. *
  7552. * Returns 0 on success or error on failure
  7553. */
  7554. int voc_send_cvp_unmap_vocpcm_memory(uint32_t session_id)
  7555. {
  7556. int ret = 0;
  7557. ret = voice_send_mvm_unmap_memory_physical_cmd(
  7558. voice_get_session(session_id),
  7559. common.voice_host_pcm_mem_handle);
  7560. if (ret == 0)
  7561. common.voice_host_pcm_mem_handle = 0;
  7562. return ret;
  7563. }
  7564. EXPORT_SYMBOL(voc_send_cvp_unmap_vocpcm_memory);
  7565. /**
  7566. * voc_send_cvp_vocpcm_push_buf_evt - Send buf event command
  7567. *
  7568. * @session_id: voice session ID to send this command
  7569. * @push_buff_evt: pointer with buffer event details
  7570. *
  7571. * Returns 0 on success or error on failure
  7572. */
  7573. int voc_send_cvp_vocpcm_push_buf_evt(uint32_t session_id,
  7574. struct vss_ivpcm_evt_push_buffer_v2_t *push_buff_evt)
  7575. {
  7576. struct cvp_push_buf_cmd vpcm_push_buf_cmd;
  7577. int ret = 0;
  7578. void *apr_cvp;
  7579. u16 cvp_handle;
  7580. struct voice_data *v = voice_get_session(session_id);
  7581. if (v == NULL) {
  7582. pr_err("%s: v is NULL\n", __func__);
  7583. ret = -EINVAL;
  7584. goto done;
  7585. }
  7586. apr_cvp = common.apr_q6_cvp;
  7587. if (!apr_cvp) {
  7588. pr_err("%s: apr_cvp is NULL.\n", __func__);
  7589. ret = -EINVAL;
  7590. goto done;
  7591. }
  7592. memset(&vpcm_push_buf_cmd, 0, sizeof(vpcm_push_buf_cmd));
  7593. cvp_handle = voice_get_cvp_handle(v);
  7594. /* fill in the header */
  7595. vpcm_push_buf_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  7596. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  7597. vpcm_push_buf_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  7598. sizeof(vpcm_push_buf_cmd) - APR_HDR_SIZE);
  7599. vpcm_push_buf_cmd.hdr.src_port =
  7600. voice_get_idx_for_session(v->session_id);
  7601. vpcm_push_buf_cmd.hdr.dest_port = cvp_handle;
  7602. vpcm_push_buf_cmd.hdr.token = 0;
  7603. vpcm_push_buf_cmd.hdr.opcode = VSS_IVPCM_EVT_PUSH_BUFFER_V2;
  7604. vpcm_push_buf_cmd.vpcm_evt_push_buffer.tap_point =
  7605. push_buff_evt->tap_point;
  7606. vpcm_push_buf_cmd.vpcm_evt_push_buffer.push_buf_mask =
  7607. push_buff_evt->push_buf_mask;
  7608. vpcm_push_buf_cmd.vpcm_evt_push_buffer.out_buf_mem_address =
  7609. push_buff_evt->out_buf_mem_address;
  7610. vpcm_push_buf_cmd.vpcm_evt_push_buffer.in_buf_mem_address =
  7611. push_buff_evt->in_buf_mem_address;
  7612. vpcm_push_buf_cmd.vpcm_evt_push_buffer.out_buf_mem_size =
  7613. push_buff_evt->out_buf_mem_size;
  7614. vpcm_push_buf_cmd.vpcm_evt_push_buffer.in_buf_mem_size =
  7615. push_buff_evt->in_buf_mem_size;
  7616. vpcm_push_buf_cmd.vpcm_evt_push_buffer.sampling_rate =
  7617. push_buff_evt->sampling_rate;
  7618. vpcm_push_buf_cmd.vpcm_evt_push_buffer.num_in_channels =
  7619. push_buff_evt->num_in_channels;
  7620. ret = apr_send_pkt(apr_cvp, (uint32_t *) &vpcm_push_buf_cmd);
  7621. if (ret < 0) {
  7622. pr_err("Fail: sending vocpcm map memory,\n");
  7623. goto done;
  7624. }
  7625. done:
  7626. return ret;
  7627. }
  7628. EXPORT_SYMBOL(voc_send_cvp_vocpcm_push_buf_evt);
  7629. /**
  7630. * voc_register_hpcm_evt_cb - Updates hostpcm info.
  7631. *
  7632. * @hostpcm_cb: callback function for hostpcm event
  7633. * @private_data: private data for hostpcm
  7634. *
  7635. */
  7636. void voc_register_hpcm_evt_cb(hostpcm_cb_fn hostpcm_cb,
  7637. void *private_data)
  7638. {
  7639. common.hostpcm_info.hostpcm_evt_cb = hostpcm_cb;
  7640. common.hostpcm_info.private_data = private_data;
  7641. }
  7642. EXPORT_SYMBOL(voc_register_hpcm_evt_cb);
  7643. /**
  7644. * voc_deregister_hpcm_evt_cb - resets hostpcm info.
  7645. *
  7646. */
  7647. void voc_deregister_hpcm_evt_cb(void)
  7648. {
  7649. common.hostpcm_info.hostpcm_evt_cb = NULL;
  7650. common.hostpcm_info.private_data = NULL;
  7651. }
  7652. EXPORT_SYMBOL(voc_deregister_hpcm_evt_cb);
  7653. /**
  7654. * voc_get_cvd_version - retrieve CVD version.
  7655. *
  7656. * @cvd_version: pointer to be updated with CVD version info.
  7657. *
  7658. * Returns 0 on success or error on failure
  7659. */
  7660. int voc_get_cvd_version(char *cvd_version)
  7661. {
  7662. int ret = 0;
  7663. struct voice_data *v = voice_get_session(VOICE_SESSION_VSID);
  7664. if (v == NULL) {
  7665. pr_err("%s: invalid session_id 0x%x\n",
  7666. __func__, VOICE_SESSION_VSID);
  7667. ret = -EINVAL;
  7668. goto done;
  7669. }
  7670. if (is_cvd_version_queried()) {
  7671. pr_debug("%s: Returning the cached value %s\n",
  7672. __func__, common.cvd_version);
  7673. goto done;
  7674. }
  7675. /* Register callback to APR */
  7676. ret = voice_apr_register(VOICE_SESSION_VSID);
  7677. if (ret < 0) {
  7678. pr_err("%s: apr register failed\n", __func__);
  7679. goto done;
  7680. }
  7681. mutex_lock(&common.common_lock);
  7682. mutex_lock(&v->lock);
  7683. ret = voice_send_mvm_cvd_version_cmd(v);
  7684. if (ret < 0) {
  7685. pr_err("%s: voice_send_mvm_cvd_version_cmd failed\n", __func__);
  7686. goto unlock;
  7687. }
  7688. ret = 0;
  7689. unlock:
  7690. mutex_unlock(&v->lock);
  7691. mutex_unlock(&common.common_lock);
  7692. done:
  7693. if (cvd_version)
  7694. memcpy(cvd_version, common.cvd_version,
  7695. CVD_VERSION_STRING_MAX_SIZE);
  7696. return ret;
  7697. }
  7698. EXPORT_SYMBOL(voc_get_cvd_version);
  7699. static int voice_alloc_cal_mem_map_table(void)
  7700. {
  7701. int ret = 0;
  7702. size_t len;
  7703. ret = msm_audio_ion_alloc(&(common.cal_mem_map_table.dma_buf),
  7704. sizeof(struct vss_imemory_table_t),
  7705. &common.cal_mem_map_table.phys,
  7706. &len,
  7707. &(common.cal_mem_map_table.data));
  7708. if ((ret < 0) && (ret != -EPROBE_DEFER)) {
  7709. pr_err("%s: audio ION alloc failed, rc = %d\n",
  7710. __func__, ret);
  7711. goto done;
  7712. }
  7713. common.cal_mem_map_table.size = sizeof(struct vss_imemory_table_t);
  7714. pr_debug("%s: data %pK phys %pK\n", __func__,
  7715. common.cal_mem_map_table.data,
  7716. &common.cal_mem_map_table.phys);
  7717. done:
  7718. return ret;
  7719. }
  7720. static int voice_alloc_rtac_mem_map_table(void)
  7721. {
  7722. int ret = 0;
  7723. size_t len;
  7724. ret = msm_audio_ion_alloc(
  7725. &(common.rtac_mem_map_table.dma_buf),
  7726. sizeof(struct vss_imemory_table_t),
  7727. &common.rtac_mem_map_table.phys,
  7728. &len,
  7729. &(common.rtac_mem_map_table.data));
  7730. if (ret < 0) {
  7731. pr_err("%s: audio ION alloc failed, rc = %d\n",
  7732. __func__, ret);
  7733. goto done;
  7734. }
  7735. common.rtac_mem_map_table.size = sizeof(struct vss_imemory_table_t);
  7736. pr_debug("%s: data %pK phys %pK\n", __func__,
  7737. common.rtac_mem_map_table.data,
  7738. &common.rtac_mem_map_table.phys);
  7739. done:
  7740. return ret;
  7741. }
  7742. static int voice_alloc_and_map_oob_mem(struct voice_data *v)
  7743. {
  7744. int ret = 0;
  7745. if (v == NULL) {
  7746. pr_err("%s: v is NULL\n", __func__);
  7747. return -EINVAL;
  7748. }
  7749. if (!is_voip_memory_allocated()) {
  7750. ret = voc_alloc_voip_shared_memory();
  7751. if (ret < 0) {
  7752. pr_err("%s: Failed to create voip oob memory %d\n",
  7753. __func__, ret);
  7754. goto done;
  7755. }
  7756. }
  7757. ret = voice_map_memory_physical_cmd(v,
  7758. &v->shmem_info.memtbl,
  7759. v->shmem_info.sh_buf.buf[0].phys,
  7760. v->shmem_info.sh_buf.buf[0].size * NUM_OF_BUFFERS,
  7761. VOIP_MEM_MAP_TOKEN);
  7762. if (ret) {
  7763. pr_err("%s: mvm_map_memory_phy failed %d\n",
  7764. __func__, ret);
  7765. goto done;
  7766. }
  7767. done:
  7768. return ret;
  7769. }
  7770. uint32_t voice_get_topology(uint32_t topology_idx)
  7771. {
  7772. uint32_t topology = VSS_IVOCPROC_TOPOLOGY_ID_RX_DEFAULT;
  7773. struct cal_block_data *cal_block = NULL;
  7774. /* initialize as default topology */
  7775. if (topology_idx == CVP_VOC_RX_TOPOLOGY_CAL) {
  7776. topology = VSS_IVOCPROC_TOPOLOGY_ID_RX_DEFAULT;
  7777. } else if (topology_idx == CVP_VOC_TX_TOPOLOGY_CAL) {
  7778. topology = VSS_IVOCPROC_TOPOLOGY_ID_NONE;
  7779. } else {
  7780. pr_err("%s: cal index %x is invalid!\n",
  7781. __func__, topology_idx);
  7782. goto done;
  7783. }
  7784. if (common.cal_data[topology_idx] == NULL) {
  7785. pr_err("%s: cal type is NULL for cal index %x\n",
  7786. __func__, topology_idx);
  7787. goto done;
  7788. }
  7789. mutex_lock(&common.cal_data[topology_idx]->lock);
  7790. cal_block = cal_utils_get_only_cal_block(
  7791. common.cal_data[topology_idx]);
  7792. if (cal_block == NULL) {
  7793. pr_debug("%s: cal_block not found for cal index %x\n",
  7794. __func__, topology_idx);
  7795. goto unlock;
  7796. }
  7797. topology = ((struct audio_cal_info_voc_top *)
  7798. cal_block->cal_info)->topology;
  7799. unlock:
  7800. mutex_unlock(&common.cal_data[topology_idx]->lock);
  7801. done:
  7802. pr_debug("%s: Using topology %d\n", __func__, topology);
  7803. return topology;
  7804. }
  7805. int voice_set_topology_specific_info(struct voice_data *v,
  7806. uint32_t topology_idx)
  7807. {
  7808. struct cal_block_data *cal_block = NULL;
  7809. int ret = 0;
  7810. uint32_t topo_channels;
  7811. if (common.cal_data[topology_idx] == NULL) {
  7812. pr_err("%s: cal type is NULL for cal index %x\n",
  7813. __func__, topology_idx);
  7814. ret = -EINVAL;
  7815. goto done;
  7816. }
  7817. mutex_lock(&common.cal_data[topology_idx]->lock);
  7818. cal_block = cal_utils_get_only_cal_block(
  7819. common.cal_data[topology_idx]);
  7820. if (cal_block == NULL) {
  7821. pr_debug("%s: cal_block not found for cal index %x\n",
  7822. __func__, topology_idx);
  7823. ret = -EINVAL;
  7824. goto unlock;
  7825. }
  7826. if (topology_idx == CVP_VOC_RX_TOPOLOGY_CAL) {
  7827. topo_channels = ((struct audio_cal_info_voc_top *)
  7828. cal_block->cal_info)->num_channels;
  7829. if (topo_channels > 0) {
  7830. v->dev_rx.no_of_channels = topo_channels;
  7831. pr_debug("%s: Topology Rx no of channels: %d",
  7832. __func__, v->dev_rx.no_of_channels);
  7833. memcpy(&v->dev_rx.channel_mapping,
  7834. &((struct audio_cal_info_voc_top *)
  7835. cal_block->cal_info)->channel_mapping,
  7836. VSS_CHANNEL_MAPPING_SIZE);
  7837. } else {
  7838. pr_debug("%s: cal data is zero, default to Rx backend config\n",
  7839. __func__);
  7840. if (v->dev_rx.no_of_channels == NUM_CHANNELS_MONO) {
  7841. v->dev_rx.channel_mapping[0] = PCM_CHANNEL_FC;
  7842. } else if (v->dev_rx.no_of_channels ==
  7843. NUM_CHANNELS_STEREO) {
  7844. v->dev_rx.channel_mapping[0] = PCM_CHANNEL_FL;
  7845. v->dev_rx.channel_mapping[1] = PCM_CHANNEL_FR;
  7846. } else {
  7847. pr_warn("%s: Unsupported Rx num channels: %d\n",
  7848. __func__, v->dev_rx.no_of_channels);
  7849. }
  7850. }
  7851. } else if (topology_idx == CVP_VOC_TX_TOPOLOGY_CAL) {
  7852. topo_channels = ((struct audio_cal_info_voc_top *)
  7853. cal_block->cal_info)->num_channels;
  7854. if (topo_channels > 0) {
  7855. v->dev_tx.no_of_channels = topo_channels;
  7856. pr_debug("%s: Topology Tx no of channels: %d",
  7857. __func__, v->dev_tx.no_of_channels);
  7858. memcpy(&v->dev_tx.channel_mapping,
  7859. &((struct audio_cal_info_voc_top *)
  7860. cal_block->cal_info)->channel_mapping,
  7861. VSS_CHANNEL_MAPPING_SIZE);
  7862. } else {
  7863. pr_debug("%s: cal data is zero, default to Tx backend config\n",
  7864. __func__);
  7865. if (v->dev_tx.no_of_channels == NUM_CHANNELS_MONO) {
  7866. v->dev_tx.channel_mapping[0] = PCM_CHANNEL_FC;
  7867. } else if (v->dev_tx.no_of_channels ==
  7868. NUM_CHANNELS_STEREO) {
  7869. v->dev_tx.channel_mapping[0] = PCM_CHANNEL_FL;
  7870. v->dev_tx.channel_mapping[1] = PCM_CHANNEL_FR;
  7871. } else if (v->dev_tx.no_of_channels ==
  7872. NUM_CHANNELS_THREE) {
  7873. v->dev_tx.channel_mapping[0] = PCM_CHANNEL_FL;
  7874. v->dev_tx.channel_mapping[1] = PCM_CHANNEL_FR;
  7875. v->dev_tx.channel_mapping[2] = PCM_CHANNEL_FC;
  7876. } else if (v->dev_tx.no_of_channels ==
  7877. NUM_CHANNELS_QUAD) {
  7878. v->dev_tx.channel_mapping[0] = PCM_CHANNEL_FL;
  7879. v->dev_tx.channel_mapping[1] = PCM_CHANNEL_FR;
  7880. v->dev_tx.channel_mapping[2] = PCM_CHANNEL_LS;
  7881. v->dev_tx.channel_mapping[3] = PCM_CHANNEL_RS;
  7882. } else {
  7883. pr_warn("%s: Unsupported Tx num channels: %d\n",
  7884. __func__, v->dev_tx.no_of_channels);
  7885. }
  7886. }
  7887. } else {
  7888. pr_err("%s: topology index %x is invalid\n",
  7889. __func__, topology_idx);
  7890. }
  7891. unlock:
  7892. mutex_unlock(&common.cal_data[topology_idx]->lock);
  7893. done:
  7894. return ret;
  7895. }
  7896. static int get_cal_type_index(int32_t cal_type)
  7897. {
  7898. int ret = -EINVAL;
  7899. switch (cal_type) {
  7900. case CVP_VOC_RX_TOPOLOGY_CAL_TYPE:
  7901. ret = CVP_VOC_RX_TOPOLOGY_CAL;
  7902. break;
  7903. case CVP_VOC_TX_TOPOLOGY_CAL_TYPE:
  7904. ret = CVP_VOC_TX_TOPOLOGY_CAL;
  7905. break;
  7906. case CVP_VOCPROC_STATIC_CAL_TYPE:
  7907. ret = CVP_VOCPROC_CAL;
  7908. break;
  7909. case CVP_VOCPROC_DYNAMIC_CAL_TYPE:
  7910. ret = CVP_VOCVOL_CAL;
  7911. break;
  7912. case CVS_VOCSTRM_STATIC_CAL_TYPE:
  7913. ret = CVS_VOCSTRM_CAL;
  7914. break;
  7915. case CVP_VOCDEV_CFG_CAL_TYPE:
  7916. ret = CVP_VOCDEV_CFG_CAL;
  7917. break;
  7918. case CVP_VOCPROC_STATIC_COL_CAL_TYPE:
  7919. ret = CVP_VOCPROC_COL_CAL;
  7920. break;
  7921. case CVP_VOCPROC_DYNAMIC_COL_CAL_TYPE:
  7922. ret = CVP_VOCVOL_COL_CAL;
  7923. break;
  7924. case CVS_VOCSTRM_STATIC_COL_CAL_TYPE:
  7925. ret = CVS_VOCSTRM_COL_CAL;
  7926. break;
  7927. case VOICE_RTAC_INFO_CAL_TYPE:
  7928. ret = VOICE_RTAC_INFO_CAL;
  7929. break;
  7930. case VOICE_RTAC_APR_CAL_TYPE:
  7931. ret = VOICE_RTAC_APR_CAL;
  7932. break;
  7933. default:
  7934. pr_err("%s: Invalid cal type %d!\n", __func__, cal_type);
  7935. }
  7936. return ret;
  7937. }
  7938. static int voice_prepare_volume_boost(int32_t cal_type,
  7939. size_t data_size, void *data)
  7940. {
  7941. return voc_deregister_vocproc_vol_table();
  7942. }
  7943. static int voice_enable_volume_boost(int32_t cal_type,
  7944. size_t data_size, void *data)
  7945. {
  7946. return voc_register_vocproc_vol_table();
  7947. }
  7948. static int voice_alloc_cal(int32_t cal_type,
  7949. size_t data_size, void *data)
  7950. {
  7951. int ret = 0;
  7952. int cal_index;
  7953. int cal_version;
  7954. pr_debug("%s\n", __func__);
  7955. cal_version = cal_utils_get_cal_type_version(data);
  7956. common.is_per_vocoder_cal_enabled =
  7957. !!(cal_version & PER_VOCODER_CAL_BIT_MASK);
  7958. cal_index = get_cal_type_index(cal_type);
  7959. if (cal_index < 0) {
  7960. pr_err("%s: Could not get cal index %d!\n",
  7961. __func__, cal_index);
  7962. ret = -EINVAL;
  7963. goto done;
  7964. }
  7965. ret = cal_utils_alloc_cal(data_size, data,
  7966. common.cal_data[cal_index], 0, NULL);
  7967. if (ret < 0) {
  7968. pr_err("%s: Cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
  7969. __func__, ret, cal_type);
  7970. ret = -EINVAL;
  7971. goto done;
  7972. }
  7973. done:
  7974. return ret;
  7975. }
  7976. static int voice_dealloc_cal(int32_t cal_type,
  7977. size_t data_size, void *data)
  7978. {
  7979. int ret = 0;
  7980. int cal_index;
  7981. pr_debug("%s\n", __func__);
  7982. cal_index = get_cal_type_index(cal_type);
  7983. if (cal_index < 0) {
  7984. pr_err("%s: Could not get cal index %d!\n",
  7985. __func__, cal_index);
  7986. ret = -EINVAL;
  7987. goto done;
  7988. }
  7989. ret = cal_utils_dealloc_cal(data_size, data,
  7990. common.cal_data[cal_index]);
  7991. if (ret < 0) {
  7992. pr_err("%s: Cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
  7993. __func__, ret, cal_type);
  7994. ret = -EINVAL;
  7995. goto done;
  7996. }
  7997. done:
  7998. return ret;
  7999. }
  8000. static int voice_set_cal(int32_t cal_type,
  8001. size_t data_size, void *data)
  8002. {
  8003. int ret = 0;
  8004. int cal_index;
  8005. pr_debug("%s\n", __func__);
  8006. cal_index = get_cal_type_index(cal_type);
  8007. if (cal_index < 0) {
  8008. pr_err("%s: Could not get cal index %d!\n",
  8009. __func__, cal_index);
  8010. ret = -EINVAL;
  8011. goto done;
  8012. }
  8013. ret = cal_utils_set_cal(data_size, data,
  8014. common.cal_data[cal_index], 0, NULL);
  8015. if (ret < 0) {
  8016. pr_err("%s: Cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
  8017. __func__, ret, cal_type);
  8018. ret = -EINVAL;
  8019. goto done;
  8020. }
  8021. /* Pre-load if it is voice Rx or Tx topology */
  8022. if ((cal_index == CVP_VOC_RX_TOPOLOGY_CAL) ||
  8023. (cal_index == CVP_VOC_TX_TOPOLOGY_CAL)) {
  8024. voice_load_topo_modules(cal_index);
  8025. }
  8026. done:
  8027. return ret;
  8028. }
  8029. static void voice_delete_cal_data(void)
  8030. {
  8031. pr_debug("%s\n", __func__);
  8032. cal_utils_destroy_cal_types(MAX_VOICE_CAL_TYPES, common.cal_data);
  8033. }
  8034. static int voice_init_cal_data(void)
  8035. {
  8036. int ret = 0;
  8037. struct cal_type_info cal_type_info[] = {
  8038. {{CVP_VOC_RX_TOPOLOGY_CAL_TYPE,
  8039. {NULL, NULL, NULL, voice_set_cal, NULL, NULL} },
  8040. {NULL, NULL, cal_utils_match_buf_num} },
  8041. {{CVP_VOC_TX_TOPOLOGY_CAL_TYPE,
  8042. {NULL, NULL, NULL, voice_set_cal, NULL, NULL} },
  8043. {NULL, NULL, cal_utils_match_buf_num} },
  8044. {{CVP_VOCPROC_STATIC_CAL_TYPE,
  8045. {voice_alloc_cal, voice_dealloc_cal, NULL,
  8046. voice_set_cal, NULL, NULL} },
  8047. {NULL, voice_unmap_cal_memory,
  8048. cal_utils_match_buf_num} },
  8049. {{CVP_VOCPROC_DYNAMIC_CAL_TYPE,
  8050. {voice_alloc_cal, voice_dealloc_cal,
  8051. voice_prepare_volume_boost,
  8052. voice_set_cal, NULL,
  8053. voice_enable_volume_boost} },
  8054. {NULL, voice_unmap_cal_memory,
  8055. cal_utils_match_buf_num} },
  8056. {{CVP_VOCDEV_CFG_CAL_TYPE,
  8057. {voice_alloc_cal, voice_dealloc_cal, NULL,
  8058. voice_set_cal, NULL, NULL} },
  8059. {NULL, voice_unmap_cal_memory,
  8060. cal_utils_match_buf_num} },
  8061. {{CVP_VOCPROC_STATIC_COL_CAL_TYPE,
  8062. {NULL, NULL, NULL, voice_set_cal, NULL, NULL} },
  8063. {NULL, NULL, cal_utils_match_buf_num} },
  8064. {{CVP_VOCPROC_DYNAMIC_COL_CAL_TYPE,
  8065. {NULL, NULL, NULL, voice_set_cal, NULL, NULL} },
  8066. {NULL, NULL, cal_utils_match_buf_num} },
  8067. {{CVS_VOCSTRM_STATIC_CAL_TYPE,
  8068. {voice_alloc_cal, voice_dealloc_cal, NULL,
  8069. voice_set_cal, NULL, NULL} },
  8070. {NULL, voice_unmap_cal_memory,
  8071. cal_utils_match_buf_num} },
  8072. {{CVS_VOCSTRM_STATIC_COL_CAL_TYPE,
  8073. {NULL, NULL, NULL, voice_set_cal, NULL, NULL} },
  8074. {NULL, NULL, cal_utils_match_buf_num} },
  8075. {{VOICE_RTAC_INFO_CAL_TYPE,
  8076. {NULL, NULL, NULL, NULL, NULL, NULL} },
  8077. {NULL, NULL, cal_utils_match_buf_num} },
  8078. {{VOICE_RTAC_APR_CAL_TYPE,
  8079. {NULL, NULL, NULL, NULL, NULL, NULL} },
  8080. {NULL, NULL, cal_utils_match_buf_num} },
  8081. };
  8082. ret = cal_utils_create_cal_types(MAX_VOICE_CAL_TYPES, common.cal_data,
  8083. cal_type_info);
  8084. if (ret < 0) {
  8085. pr_err("%s: Could not create cal type!\n",
  8086. __func__);
  8087. ret = -EINVAL;
  8088. goto err;
  8089. }
  8090. return ret;
  8091. err:
  8092. voice_delete_cal_data();
  8093. memset(&common, 0, sizeof(struct common_data));
  8094. return ret;
  8095. }
  8096. static int voice_send_set_sound_focus_cmd(struct voice_data *v,
  8097. struct sound_focus_param soundFocusData)
  8098. {
  8099. struct cvp_set_sound_focus_param_cmd_t cvp_set_sound_focus_param_cmd;
  8100. int ret = 0;
  8101. void *apr_cvp;
  8102. u16 cvp_handle;
  8103. int i;
  8104. pr_debug("%s: Enter\n", __func__);
  8105. if (v == NULL) {
  8106. pr_err("%s: v is NULL\n", __func__);
  8107. ret = -EINVAL;
  8108. goto done;
  8109. }
  8110. apr_cvp = common.apr_q6_cvp;
  8111. if (!apr_cvp) {
  8112. pr_err("%s: apr_cvp is NULL.\n", __func__);
  8113. ret = -EINVAL;
  8114. goto done;
  8115. }
  8116. cvp_handle = voice_get_cvp_handle(v);
  8117. /* send Sound Focus Params to cvp */
  8118. cvp_set_sound_focus_param_cmd.hdr.hdr_field =
  8119. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  8120. APR_HDR_LEN(APR_HDR_SIZE),
  8121. APR_PKT_VER);
  8122. cvp_set_sound_focus_param_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  8123. sizeof(cvp_set_sound_focus_param_cmd) - APR_HDR_SIZE);
  8124. cvp_set_sound_focus_param_cmd.hdr.src_port =
  8125. voice_get_idx_for_session(v->session_id);
  8126. cvp_set_sound_focus_param_cmd.hdr.dest_port = cvp_handle;
  8127. cvp_set_sound_focus_param_cmd.hdr.token = 0;
  8128. cvp_set_sound_focus_param_cmd.hdr.opcode =
  8129. VSS_ISOUNDFOCUS_CMD_SET_SECTORS;
  8130. memset(&(cvp_set_sound_focus_param_cmd.cvp_set_sound_focus_param), 0xFF,
  8131. sizeof(struct vss_isoundfocus_cmd_set_sectors_t));
  8132. for (i = 0; i < MAX_SECTORS; i++) {
  8133. cvp_set_sound_focus_param_cmd.cvp_set_sound_focus_param.
  8134. start_angles[i] = soundFocusData.start_angle[i];
  8135. cvp_set_sound_focus_param_cmd.cvp_set_sound_focus_param.
  8136. enables[i] = soundFocusData.enable[i];
  8137. pr_debug("%s: start_angle[%d] = %d\n",
  8138. __func__, i, soundFocusData.start_angle[i]);
  8139. pr_debug("%s: enable[%d] = %d\n",
  8140. __func__, i, soundFocusData.enable[i]);
  8141. }
  8142. cvp_set_sound_focus_param_cmd.cvp_set_sound_focus_param.gain_step =
  8143. soundFocusData.gain_step;
  8144. pr_debug("%s: gain_step = %d\n", __func__, soundFocusData.gain_step);
  8145. v->cvp_state = CMD_STATUS_FAIL;
  8146. v->async_err = 0;
  8147. ret = apr_send_pkt(apr_cvp, (uint32_t *)&cvp_set_sound_focus_param_cmd);
  8148. if (ret < 0) {
  8149. pr_err("%s: Error in sending APR command\n", __func__);
  8150. ret = -EINVAL;
  8151. goto done;
  8152. }
  8153. ret = wait_event_timeout(v->cvp_wait,
  8154. (v->cvp_state == CMD_STATUS_SUCCESS),
  8155. msecs_to_jiffies(TIMEOUT_MS));
  8156. if (!ret) {
  8157. pr_err("%s: wait_event timeout\n", __func__);
  8158. ret = -EINVAL;
  8159. goto done;
  8160. }
  8161. if (v->async_err > 0) {
  8162. pr_err("%s: DSP returned error[%s]\n",
  8163. __func__, adsp_err_get_err_str(
  8164. v->async_err));
  8165. ret = adsp_err_get_lnx_err_code(
  8166. v->async_err);
  8167. goto done;
  8168. }
  8169. if (common.is_sound_focus_resp_success) {
  8170. ret = 0;
  8171. } else {
  8172. pr_err("%s: Error in setting sound focus params\n", __func__);
  8173. ret = -EINVAL;
  8174. }
  8175. done:
  8176. pr_debug("%s: Exit, ret=%d\n", __func__, ret);
  8177. return ret;
  8178. }
  8179. /**
  8180. * voc_set_sound_focus - sends sound focus data.
  8181. *
  8182. * @soundFocusData: sound focus data.
  8183. *
  8184. * Returns 0 on success or error on failure
  8185. */
  8186. int voc_set_sound_focus(struct sound_focus_param soundFocusData)
  8187. {
  8188. struct voice_data *v = NULL;
  8189. int ret = -EINVAL;
  8190. struct voice_session_itr itr;
  8191. pr_debug("%s: Enter\n", __func__);
  8192. mutex_lock(&common.common_lock);
  8193. voice_itr_init(&itr, ALL_SESSION_VSID);
  8194. while (voice_itr_get_next_session(&itr, &v)) {
  8195. if (v != NULL) {
  8196. mutex_lock(&v->lock);
  8197. if (is_voc_state_active(v->voc_state) &&
  8198. (v->lch_mode != VOICE_LCH_START) &&
  8199. !v->disable_topology)
  8200. ret = voice_send_set_sound_focus_cmd(v,
  8201. soundFocusData);
  8202. mutex_unlock(&v->lock);
  8203. } else {
  8204. pr_err("%s: invalid session\n", __func__);
  8205. ret = -EINVAL;
  8206. break;
  8207. }
  8208. }
  8209. mutex_unlock(&common.common_lock);
  8210. pr_debug("%s: Exit, ret=%d\n", __func__, ret);
  8211. return ret;
  8212. }
  8213. EXPORT_SYMBOL(voc_set_sound_focus);
  8214. static int voice_send_get_sound_focus_cmd(struct voice_data *v,
  8215. struct sound_focus_param *soundFocusData)
  8216. {
  8217. struct apr_hdr cvp_get_sound_focus_param_cmd;
  8218. int ret = 0;
  8219. void *apr_cvp;
  8220. u16 cvp_handle;
  8221. int i;
  8222. pr_debug("%s: Enter\n", __func__);
  8223. if (!v) {
  8224. pr_err("%s: v is NULL\n", __func__);
  8225. ret = -EINVAL;
  8226. goto done;
  8227. }
  8228. apr_cvp = common.apr_q6_cvp;
  8229. if (!apr_cvp) {
  8230. pr_err("%s: apr_cvp is NULL\n", __func__);
  8231. ret = -EINVAL;
  8232. goto done;
  8233. }
  8234. cvp_handle = voice_get_cvp_handle(v);
  8235. /* send APR command to retrieve Sound Focus Params */
  8236. cvp_get_sound_focus_param_cmd.hdr_field =
  8237. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  8238. APR_HDR_LEN(APR_HDR_SIZE),
  8239. APR_PKT_VER);
  8240. cvp_get_sound_focus_param_cmd.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  8241. sizeof(cvp_get_sound_focus_param_cmd) - APR_HDR_SIZE);
  8242. cvp_get_sound_focus_param_cmd.src_port =
  8243. voice_get_idx_for_session(v->session_id);
  8244. cvp_get_sound_focus_param_cmd.dest_port = cvp_handle;
  8245. cvp_get_sound_focus_param_cmd.token = 0;
  8246. cvp_get_sound_focus_param_cmd.opcode = VSS_ISOUNDFOCUS_CMD_GET_SECTORS;
  8247. v->cvp_state = CMD_STATUS_FAIL;
  8248. v->async_err = 0;
  8249. ret = apr_send_pkt(apr_cvp, (uint32_t *)&cvp_get_sound_focus_param_cmd);
  8250. if (ret < 0) {
  8251. pr_err("%s: Error in sending APR command\n", __func__);
  8252. ret = -EINVAL;
  8253. goto done;
  8254. }
  8255. ret = wait_event_timeout(v->cvp_wait,
  8256. (v->cvp_state == CMD_STATUS_SUCCESS),
  8257. msecs_to_jiffies(TIMEOUT_MS));
  8258. if (!ret) {
  8259. pr_err("%s: wait_event timeout\n", __func__);
  8260. ret = -EINVAL;
  8261. goto done;
  8262. }
  8263. if (v->async_err > 0) {
  8264. pr_err("%s: DSP returned error[%s]\n",
  8265. __func__, adsp_err_get_err_str(
  8266. v->async_err));
  8267. ret = adsp_err_get_lnx_err_code(
  8268. v->async_err);
  8269. goto done;
  8270. }
  8271. if (common.is_sound_focus_resp_success) {
  8272. for (i = 0; i < MAX_SECTORS; i++) {
  8273. soundFocusData->start_angle[i] =
  8274. common.soundFocusResponse.start_angles[i];
  8275. soundFocusData->enable[i] =
  8276. common.soundFocusResponse.enables[i];
  8277. pr_debug("%s: start_angle[%d] = %d\n",
  8278. __func__, i, soundFocusData->start_angle[i]);
  8279. pr_debug("%s: enable[%d] = %d\n",
  8280. __func__, i, soundFocusData->enable[i]);
  8281. }
  8282. soundFocusData->gain_step = common.soundFocusResponse.gain_step;
  8283. pr_debug("%s: gain_step = %d\n", __func__,
  8284. soundFocusData->gain_step);
  8285. common.is_sound_focus_resp_success = false;
  8286. ret = 0;
  8287. } else {
  8288. pr_err("%s: Invalid payload received from CVD\n", __func__);
  8289. ret = -EINVAL;
  8290. }
  8291. done:
  8292. pr_debug("%s: Exit, ret=%d\n", __func__, ret);
  8293. return ret;
  8294. }
  8295. /**
  8296. * voc_get_sound_focus - retrieves sound focus data.
  8297. *
  8298. * @soundFocusData: pointer to be updated with sound focus data.
  8299. *
  8300. * Returns 0 on success or error on failure
  8301. */
  8302. int voc_get_sound_focus(struct sound_focus_param *soundFocusData)
  8303. {
  8304. struct voice_data *v = NULL;
  8305. int ret = -EINVAL;
  8306. struct voice_session_itr itr;
  8307. pr_debug("%s: Enter\n", __func__);
  8308. mutex_lock(&common.common_lock);
  8309. voice_itr_init(&itr, ALL_SESSION_VSID);
  8310. while (voice_itr_get_next_session(&itr, &v)) {
  8311. if (v) {
  8312. mutex_lock(&v->lock);
  8313. if (is_voc_state_active(v->voc_state) &&
  8314. (v->lch_mode != VOICE_LCH_START) &&
  8315. !v->disable_topology)
  8316. ret = voice_send_get_sound_focus_cmd(v,
  8317. soundFocusData);
  8318. mutex_unlock(&v->lock);
  8319. } else {
  8320. pr_err("%s: invalid session\n", __func__);
  8321. ret = -EINVAL;
  8322. break;
  8323. }
  8324. }
  8325. mutex_unlock(&common.common_lock);
  8326. pr_debug("%s: Exit, ret=%d\n", __func__, ret);
  8327. return ret;
  8328. }
  8329. EXPORT_SYMBOL(voc_get_sound_focus);
  8330. static int is_source_tracking_shared_memomry_allocated(void)
  8331. {
  8332. bool ret;
  8333. pr_debug("%s: Enter\n", __func__);
  8334. if (common.source_tracking_sh_mem.sh_mem_block.dma_buf != NULL)
  8335. ret = true;
  8336. else
  8337. ret = false;
  8338. pr_debug("%s: Exit\n", __func__);
  8339. return ret;
  8340. }
  8341. static int voice_alloc_source_tracking_shared_memory(void)
  8342. {
  8343. int ret = 0;
  8344. pr_debug("%s: Enter\n", __func__);
  8345. ret = msm_audio_ion_alloc(
  8346. &(common.source_tracking_sh_mem.sh_mem_block.dma_buf),
  8347. BUFFER_BLOCK_SIZE,
  8348. &(common.source_tracking_sh_mem.sh_mem_block.phys),
  8349. (size_t *)&(common.source_tracking_sh_mem.sh_mem_block.size),
  8350. &(common.source_tracking_sh_mem.sh_mem_block.data));
  8351. if (ret < 0) {
  8352. pr_err("%s: audio ION alloc failed for sh_mem block, ret = %d\n",
  8353. __func__, ret);
  8354. ret = -EINVAL;
  8355. goto done;
  8356. }
  8357. memset((void *)(common.source_tracking_sh_mem.sh_mem_block.data), 0,
  8358. common.source_tracking_sh_mem.sh_mem_block.size);
  8359. pr_debug("%s: sh_mem_block: phys:[%pK], data:[0x%pK], size:[%zd]\n",
  8360. __func__,
  8361. &(common.source_tracking_sh_mem.sh_mem_block.phys),
  8362. (void *)(common.source_tracking_sh_mem.sh_mem_block.data),
  8363. (size_t)(common.source_tracking_sh_mem.sh_mem_block.size));
  8364. ret = msm_audio_ion_alloc(
  8365. &(common.source_tracking_sh_mem.sh_mem_table.dma_buf),
  8366. sizeof(struct vss_imemory_table_t),
  8367. &(common.source_tracking_sh_mem.sh_mem_table.phys),
  8368. (size_t *)&(common.source_tracking_sh_mem.sh_mem_table.size),
  8369. &(common.source_tracking_sh_mem.sh_mem_table.data));
  8370. if (ret < 0) {
  8371. pr_err("%s: audio ION alloc failed for sh_mem table, ret = %d\n",
  8372. __func__, ret);
  8373. ret = msm_audio_ion_free(
  8374. common.source_tracking_sh_mem.sh_mem_block.dma_buf);
  8375. common.source_tracking_sh_mem.sh_mem_block.dma_buf = NULL;
  8376. if (ret < 0)
  8377. pr_err("%s: Error:%d freeing memory\n", __func__, ret);
  8378. ret = -EINVAL;
  8379. goto done;
  8380. }
  8381. memset((void *)(common.source_tracking_sh_mem.sh_mem_table.data), 0,
  8382. common.source_tracking_sh_mem.sh_mem_table.size);
  8383. pr_debug("%s sh_mem_table: phys:[%pK], data:[0x%pK], size:[%zd],\n",
  8384. __func__,
  8385. &(common.source_tracking_sh_mem.sh_mem_table.phys),
  8386. (void *)(common.source_tracking_sh_mem.sh_mem_table.data),
  8387. (size_t)(common.source_tracking_sh_mem.sh_mem_table.size));
  8388. done:
  8389. pr_debug("%s: Exit, ret=%d\n", __func__, ret);
  8390. return ret;
  8391. }
  8392. static int voice_alloc_and_map_source_tracking_shared_memory(
  8393. struct voice_data *v)
  8394. {
  8395. int ret = 0;
  8396. pr_debug("%s: Enter\n", __func__);
  8397. ret = voice_alloc_source_tracking_shared_memory();
  8398. if (ret) {
  8399. pr_err("%s: Failed to allocate shared memory %d\n",
  8400. __func__, ret);
  8401. ret = -EINVAL;
  8402. goto done;
  8403. }
  8404. ret = voice_map_memory_physical_cmd(v,
  8405. &(common.source_tracking_sh_mem.sh_mem_table),
  8406. common.source_tracking_sh_mem.sh_mem_block.phys,
  8407. common.source_tracking_sh_mem.sh_mem_block.size,
  8408. VOC_SOURCE_TRACKING_MEM_MAP_TOKEN);
  8409. if (ret) {
  8410. pr_err("%s: memory mapping failed %d\n",
  8411. __func__, ret);
  8412. ret = -EINVAL;
  8413. goto done;
  8414. }
  8415. done:
  8416. pr_debug("%s: Exit, ret=%d\n", __func__, ret);
  8417. return ret;
  8418. }
  8419. static int voice_unmap_and_free_source_tracking_shared_memory(
  8420. struct voice_data *v)
  8421. {
  8422. int ret = 0;
  8423. pr_debug("%s: Enter\n", __func__);
  8424. if (common.source_tracking_sh_mem.mem_handle != 0) {
  8425. ret = voice_send_mvm_unmap_memory_physical_cmd(v,
  8426. common.source_tracking_sh_mem.mem_handle);
  8427. if (ret < 0) {
  8428. pr_err("%s: Memory_unmap failed err %d\n",
  8429. __func__, ret);
  8430. ret = -EINVAL;
  8431. goto done;
  8432. }
  8433. }
  8434. if (common.source_tracking_sh_mem.sh_mem_block.dma_buf == NULL)
  8435. goto done;
  8436. ret = msm_audio_ion_free(
  8437. common.source_tracking_sh_mem.sh_mem_block.dma_buf);
  8438. if (ret < 0) {
  8439. pr_err("%s: Error:%d freeing memory\n", __func__, ret);
  8440. ret = -EINVAL;
  8441. goto done;
  8442. }
  8443. done:
  8444. common.source_tracking_sh_mem.mem_handle = 0;
  8445. common.source_tracking_sh_mem.sh_mem_block.dma_buf = NULL;
  8446. pr_debug("%s: Exit, ret=%d\n", __func__, ret);
  8447. return ret;
  8448. }
  8449. static int voice_send_get_source_tracking_cmd(struct voice_data *v,
  8450. struct source_tracking_param *sourceTrackingData)
  8451. {
  8452. struct cvp_get_source_tracking_param_cmd_t st_cmd;
  8453. int ret = 0;
  8454. void *apr_cvp;
  8455. u16 cvp_handle;
  8456. int i;
  8457. pr_debug("%s: Enter\n", __func__);
  8458. if (!v) {
  8459. pr_err("%s: v is NULL\n", __func__);
  8460. return -EINVAL;
  8461. }
  8462. apr_cvp = common.apr_q6_cvp;
  8463. if (!apr_cvp) {
  8464. pr_err("%s: apr_cvp is NULL.\n", __func__);
  8465. return -EINVAL;
  8466. }
  8467. cvp_handle = voice_get_cvp_handle(v);
  8468. if (!is_source_tracking_shared_memomry_allocated()) {
  8469. ret = voice_alloc_and_map_source_tracking_shared_memory(v);
  8470. if (ret) {
  8471. pr_err("%s: Fail in allocating/mapping shared memory\n",
  8472. __func__);
  8473. ret = -EINVAL;
  8474. goto done;
  8475. }
  8476. }
  8477. st_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  8478. APR_HDR_LEN(APR_HDR_SIZE),
  8479. APR_PKT_VER);
  8480. st_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  8481. sizeof(st_cmd) - APR_HDR_SIZE);
  8482. st_cmd.hdr.src_port = voice_get_idx_for_session(v->session_id);
  8483. st_cmd.hdr.dest_port = cvp_handle;
  8484. st_cmd.hdr.token = 0;
  8485. st_cmd.hdr.opcode = VSS_ISOURCETRACK_CMD_GET_ACTIVITY;
  8486. st_cmd.cvp_get_source_tracking_param.mem_handle =
  8487. common.source_tracking_sh_mem.mem_handle;
  8488. st_cmd.cvp_get_source_tracking_param.mem_address_lsw =
  8489. lower_32_bits(common.source_tracking_sh_mem.sh_mem_block.phys);
  8490. st_cmd.cvp_get_source_tracking_param.mem_address_msw =
  8491. msm_audio_populate_upper_32_bits(common.source_tracking_sh_mem.
  8492. sh_mem_block.phys);
  8493. st_cmd.cvp_get_source_tracking_param.mem_size =
  8494. (uint32_t)common.source_tracking_sh_mem.sh_mem_block.size;
  8495. pr_debug("%s: mem_handle=0x%x, mem_address_lsw=0x%x, msw=0x%x, mem_size=%d\n",
  8496. __func__,
  8497. st_cmd.cvp_get_source_tracking_param.mem_handle,
  8498. st_cmd.cvp_get_source_tracking_param.mem_address_lsw,
  8499. st_cmd.cvp_get_source_tracking_param.mem_address_msw,
  8500. (uint32_t)st_cmd.cvp_get_source_tracking_param.mem_size);
  8501. v->cvp_state = CMD_STATUS_FAIL;
  8502. v->async_err = 0;
  8503. ret = apr_send_pkt(apr_cvp,
  8504. (uint32_t *) &st_cmd);
  8505. if (ret < 0) {
  8506. pr_err("%s: Error in sending APR command\n", __func__);
  8507. ret = -EINVAL;
  8508. goto done;
  8509. }
  8510. ret = wait_event_timeout(v->cvp_wait,
  8511. (v->cvp_state == CMD_STATUS_SUCCESS),
  8512. msecs_to_jiffies(TIMEOUT_MS));
  8513. if (!ret) {
  8514. pr_err("%s: wait_event timeout\n", __func__);
  8515. ret = -EINVAL;
  8516. goto done;
  8517. }
  8518. if (v->async_err > 0) {
  8519. pr_err("%s: DSP returned error[%s]\n",
  8520. __func__, adsp_err_get_err_str(
  8521. v->async_err));
  8522. ret = adsp_err_get_lnx_err_code(
  8523. v->async_err);
  8524. goto done;
  8525. }
  8526. if (common.is_source_tracking_resp_success) {
  8527. for (i = 0; i < MAX_SECTORS; i++) {
  8528. sourceTrackingData->vad[i] =
  8529. common.sourceTrackingResponse.voice_active[i];
  8530. pr_debug("%s: vad[%d] = %d\n",
  8531. __func__, i, sourceTrackingData->vad[i]);
  8532. }
  8533. sourceTrackingData->doa_speech =
  8534. common.sourceTrackingResponse.talker_doa;
  8535. pr_debug("%s: doa_speech = %d\n",
  8536. __func__, sourceTrackingData->doa_speech);
  8537. for (i = 0; i < MAX_NOISE_SOURCE_INDICATORS; i++) {
  8538. sourceTrackingData->doa_noise[i] =
  8539. common.sourceTrackingResponse.interferer_doa[i];
  8540. pr_debug("%s: doa_noise[%d] = %d\n",
  8541. __func__, i, sourceTrackingData->doa_noise[i]);
  8542. }
  8543. for (i = 0; i < MAX_POLAR_ACTIVITY_INDICATORS; i++) {
  8544. sourceTrackingData->polar_activity[i] =
  8545. common.sourceTrackingResponse.sound_strength[i];
  8546. pr_debug("%s: polar_activity[%d] = %d\n",
  8547. __func__, i, sourceTrackingData->polar_activity[i]);
  8548. }
  8549. common.is_source_tracking_resp_success = false;
  8550. ret = 0;
  8551. } else {
  8552. pr_err("%s: Error response received from CVD\n", __func__);
  8553. ret = -EINVAL;
  8554. }
  8555. done:
  8556. pr_debug("%s: Exit, ret=%d\n", __func__, ret);
  8557. return ret;
  8558. }
  8559. /**
  8560. * voc_get_source_tracking - retrieves source track data.
  8561. *
  8562. * @sourceTrackingData: pointer to be updated with source track data.
  8563. *
  8564. * Returns 0 on success or error on failure
  8565. */
  8566. int voc_get_source_tracking(struct source_tracking_param *sourceTrackingData)
  8567. {
  8568. struct voice_data *v = NULL;
  8569. int ret = -EINVAL;
  8570. struct voice_session_itr itr;
  8571. pr_debug("%s: Enter\n", __func__);
  8572. mutex_lock(&common.common_lock);
  8573. voice_itr_init(&itr, ALL_SESSION_VSID);
  8574. while (voice_itr_get_next_session(&itr, &v)) {
  8575. if (v != NULL) {
  8576. mutex_lock(&v->lock);
  8577. if (is_voc_state_active(v->voc_state) &&
  8578. (v->lch_mode != VOICE_LCH_START) &&
  8579. !v->disable_topology)
  8580. ret = voice_send_get_source_tracking_cmd(v,
  8581. sourceTrackingData);
  8582. mutex_unlock(&v->lock);
  8583. } else {
  8584. pr_err("%s: invalid session\n", __func__);
  8585. break;
  8586. }
  8587. }
  8588. mutex_unlock(&common.common_lock);
  8589. pr_debug("%s: Exit, ret=%d\n", __func__, ret);
  8590. return ret;
  8591. }
  8592. EXPORT_SYMBOL(voc_get_source_tracking);
  8593. static int voice_set_cvp_param(struct voice_data *v,
  8594. struct vss_icommon_mem_mapping_hdr *mem_hdr,
  8595. u32 *param_data, u32 param_size)
  8596. {
  8597. struct vss_icommon_cmd_set_param *set_param = NULL;
  8598. uint32_t pkt_size = sizeof(struct vss_icommon_cmd_set_param);
  8599. void *apr_cvp;
  8600. int ret = 0;
  8601. apr_cvp = common.apr_q6_cvp;
  8602. if (!apr_cvp) {
  8603. pr_err("%s: apr_cvp is NULL\n", __func__);
  8604. return -EINVAL;
  8605. }
  8606. if (param_data != NULL)
  8607. pkt_size += param_size;
  8608. set_param = kzalloc(pkt_size, GFP_KERNEL);
  8609. if (!set_param)
  8610. return -ENOMEM;
  8611. set_param->apr_hdr.hdr_field =
  8612. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  8613. APR_PKT_VER);
  8614. set_param->apr_hdr.pkt_size =
  8615. APR_PKT_SIZE(APR_HDR_SIZE, pkt_size - APR_HDR_SIZE);
  8616. set_param->apr_hdr.src_svc = 0;
  8617. set_param->apr_hdr.src_domain = APR_DOMAIN_APPS;
  8618. set_param->apr_hdr.src_port = voice_get_idx_for_session(v->session_id);
  8619. set_param->apr_hdr.dest_svc = 0;
  8620. set_param->apr_hdr.dest_domain = APR_DOMAIN_ADSP;
  8621. set_param->apr_hdr.dest_port = voice_get_cvp_handle(v);
  8622. set_param->apr_hdr.token = VOC_SET_MEDIA_FORMAT_PARAM_TOKEN;
  8623. set_param->apr_hdr.opcode = q6common_is_instance_id_supported() ?
  8624. VSS_ICOMMON_CMD_SET_PARAM_V3 :
  8625. VSS_ICOMMON_CMD_SET_PARAM_V2;
  8626. set_param->payload_size = param_size;
  8627. if (mem_hdr != NULL) {
  8628. set_param->mem_hdr = *mem_hdr;
  8629. } else if (param_data != NULL) {
  8630. memcpy(set_param->param_data, param_data, param_size);
  8631. } else {
  8632. pr_err("%s: Both memory header and param data are NULL\n",
  8633. __func__);
  8634. ret = -EINVAL;
  8635. goto done;
  8636. }
  8637. v->cvp_state = CMD_STATUS_FAIL;
  8638. v->async_err = 0;
  8639. ret = apr_send_pkt(apr_cvp, (u32 *) set_param);
  8640. if (ret < 0) {
  8641. pr_err("%s: Failed to send apr packet, error %d\n", __func__,
  8642. ret);
  8643. goto done;
  8644. }
  8645. ret = wait_event_timeout(v->cvp_wait,
  8646. v->cvp_state == CMD_STATUS_SUCCESS,
  8647. msecs_to_jiffies(TIMEOUT_MS));
  8648. if (!ret) {
  8649. pr_err("%s: wait_event timeout\n", __func__);
  8650. ret = -ETIMEDOUT;
  8651. goto done;
  8652. }
  8653. if (v->async_err > 0) {
  8654. pr_err("%s: DSP returned error[%s]\n", __func__,
  8655. adsp_err_get_err_str(v->async_err));
  8656. ret = adsp_err_get_lnx_err_code(v->async_err);
  8657. goto done;
  8658. }
  8659. ret = 0;
  8660. done:
  8661. kfree(set_param);
  8662. return ret;
  8663. }
  8664. static int voice_pack_and_set_cvp_param(struct voice_data *v,
  8665. struct param_hdr_v3 param_hdr,
  8666. u8 *param_data)
  8667. {
  8668. u8 *packed_data = NULL;
  8669. u32 total_size = 0;
  8670. int ret = 0;
  8671. total_size = sizeof(union param_hdrs) + param_hdr.param_size;
  8672. packed_data = kzalloc(total_size, GFP_KERNEL);
  8673. if (!packed_data)
  8674. return -ENOMEM;
  8675. ret = q6common_pack_pp_params(packed_data, &param_hdr, param_data,
  8676. &total_size);
  8677. if (ret) {
  8678. pr_err("%s: Failed to pack params, error %d", __func__, ret);
  8679. goto done;
  8680. }
  8681. ret = voice_set_cvp_param(v, NULL, (u32 *) packed_data, total_size);
  8682. done:
  8683. kfree(packed_data);
  8684. return ret;
  8685. }
  8686. /*
  8687. * Out of band is not supported and there are currently no pre-packed cases,
  8688. * so pack and set in the same function. When needed, split up.
  8689. */
  8690. static int voice_pack_and_set_cvs_ui_property(struct voice_data *v,
  8691. struct param_hdr_v3 param_hdr,
  8692. u8 *param_data)
  8693. {
  8694. struct vss_icommon_cmd_set_ui_property *set_ui_property = NULL;
  8695. u32 total_size = 0;
  8696. u32 pkt_size = 0;
  8697. u32 param_size = 0;
  8698. bool iid_supported = q6common_is_instance_id_supported();
  8699. void *apr_cvs;
  8700. int ret = 0;
  8701. apr_cvs = common.apr_q6_cvs;
  8702. if (!apr_cvs) {
  8703. pr_err("%s: apr_cvs is NULL\n", __func__);
  8704. return -EINVAL;
  8705. }
  8706. pkt_size = sizeof(struct vss_icommon_cmd_set_ui_property);
  8707. param_size = sizeof(union param_hdrs) + param_hdr.param_size;
  8708. total_size = pkt_size + param_size;
  8709. set_ui_property = kzalloc(total_size, GFP_KERNEL);
  8710. if (!set_ui_property)
  8711. return -ENOMEM;
  8712. ret = q6common_pack_pp_params(set_ui_property->param_data, &param_hdr,
  8713. param_data, &param_size);
  8714. if (ret) {
  8715. pr_err("%s: Failed to pack params, error %d", __func__, ret);
  8716. goto done;
  8717. }
  8718. /*
  8719. * Pack the APR header after packing the data so we have the actual
  8720. * total size of the payload
  8721. */
  8722. total_size = pkt_size + param_size;
  8723. set_ui_property->apr_hdr.hdr_field =
  8724. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  8725. APR_PKT_VER);
  8726. set_ui_property->apr_hdr.pkt_size = total_size;
  8727. set_ui_property->apr_hdr.src_svc = 0;
  8728. set_ui_property->apr_hdr.src_domain = APR_DOMAIN_APPS;
  8729. set_ui_property->apr_hdr.src_port =
  8730. voice_get_idx_for_session(v->session_id);
  8731. set_ui_property->apr_hdr.dest_svc = 0;
  8732. set_ui_property->apr_hdr.dest_domain = APR_DOMAIN_ADSP;
  8733. set_ui_property->apr_hdr.dest_port = voice_get_cvs_handle(v);
  8734. set_ui_property->apr_hdr.token = 0;
  8735. set_ui_property->apr_hdr.opcode =
  8736. iid_supported ? VSS_ICOMMON_CMD_SET_UI_PROPERTY_V2 :
  8737. VSS_ICOMMON_CMD_SET_UI_PROPERTY;
  8738. v->cvs_state = CMD_STATUS_FAIL;
  8739. v->async_err = 0;
  8740. ret = apr_send_pkt(apr_cvs, (u32 *) set_ui_property);
  8741. if (ret < 0) {
  8742. pr_err("%s: Failed to send apr packet, error %d\n", __func__,
  8743. ret);
  8744. goto done;
  8745. }
  8746. ret = wait_event_timeout(v->cvs_wait,
  8747. v->cvs_state == CMD_STATUS_SUCCESS,
  8748. msecs_to_jiffies(TIMEOUT_MS));
  8749. if (!ret) {
  8750. pr_err("%s: wait_event timeout\n", __func__);
  8751. ret = -ETIMEDOUT;
  8752. goto done;
  8753. }
  8754. if (v->async_err > 0) {
  8755. pr_err("%s: DSP returned error[%s]\n", __func__,
  8756. adsp_err_get_err_str(v->async_err));
  8757. ret = adsp_err_get_lnx_err_code(v->async_err);
  8758. goto done;
  8759. }
  8760. ret = 0;
  8761. done:
  8762. kfree(set_ui_property);
  8763. return ret;
  8764. }
  8765. static void voc_release_uevent_data(struct kobject *kobj)
  8766. {
  8767. struct audio_uevent_data *data = container_of(kobj,
  8768. struct audio_uevent_data,
  8769. kobj);
  8770. kfree(data);
  8771. }
  8772. /**
  8773. * is_voc_initialized:
  8774. *
  8775. * Returns voice module init status
  8776. *
  8777. */
  8778. int is_voc_initialized(void)
  8779. {
  8780. return module_initialized;
  8781. }
  8782. EXPORT_SYMBOL(is_voc_initialized);
  8783. int __init voice_init(void)
  8784. {
  8785. int rc = 0, i = 0;
  8786. memset(&common, 0, sizeof(struct common_data));
  8787. /* set default value */
  8788. common.default_mute_val = 0; /* default is un-mute */
  8789. common.default_sample_val = 8000;
  8790. common.default_vol_step_val = 0;
  8791. common.default_vol_ramp_duration_ms = DEFAULT_VOLUME_RAMP_DURATION;
  8792. common.default_mute_ramp_duration_ms = DEFAULT_MUTE_RAMP_DURATION;
  8793. common.cvp_version = 0;
  8794. common.is_avcs_version_queried = false;
  8795. /* Initialize EC Ref media format info */
  8796. common.ec_ref_ext = false;
  8797. common.ec_media_fmt_info.port_id = AFE_PORT_INVALID;
  8798. common.ec_media_fmt_info.num_channels = 0;
  8799. common.ec_media_fmt_info.bits_per_sample = 16;
  8800. common.ec_media_fmt_info.sample_rate = 8000;
  8801. memset(&common.ec_media_fmt_info.channel_mapping, 0,
  8802. VSS_CHANNEL_MAPPING_SIZE);
  8803. /* Initialize AFE Sidetone Enable */
  8804. common.sidetone_enable = false;
  8805. /* Initialize MVS info. */
  8806. common.mvs_info.network_type = VSS_NETWORK_ID_DEFAULT;
  8807. /* Initialize is low memory flag */
  8808. common.is_destroy_cvd = false;
  8809. /* Initialize CVD version */
  8810. strlcpy(common.cvd_version, CVD_VERSION_DEFAULT,
  8811. sizeof(common.cvd_version));
  8812. /* Initialize Per-Vocoder Calibration flag */
  8813. common.is_per_vocoder_cal_enabled = false;
  8814. /*
  8815. * Initialize in call record channel config
  8816. * to mono
  8817. */
  8818. common.rec_channel_count = NUM_CHANNELS_MONO;
  8819. mutex_init(&common.common_lock);
  8820. common.uevent_data = kzalloc(sizeof(*(common.uevent_data)), GFP_KERNEL);
  8821. if (!common.uevent_data)
  8822. return -ENOMEM;
  8823. /*
  8824. * Set release function to cleanup memory related to kobject
  8825. * before initializing the kobject.
  8826. */
  8827. common.uevent_data->ktype.release = voc_release_uevent_data;
  8828. q6core_init_uevent_data(common.uevent_data, "q6voice_uevent");
  8829. common.mic_break_enable = false;
  8830. /* Initialize session id with vsid */
  8831. init_session_id();
  8832. for (i = 0; i < MAX_VOC_SESSIONS; i++) {
  8833. /* initialize dev_rx and dev_tx */
  8834. common.voice[i].dev_rx.dev_mute = common.default_mute_val;
  8835. common.voice[i].dev_tx.dev_mute = common.default_mute_val;
  8836. common.voice[i].dev_rx.volume_step_value =
  8837. common.default_vol_step_val;
  8838. common.voice[i].dev_rx.volume_ramp_duration_ms =
  8839. common.default_vol_ramp_duration_ms;
  8840. common.voice[i].dev_rx.dev_mute_ramp_duration_ms =
  8841. common.default_mute_ramp_duration_ms;
  8842. common.voice[i].dev_tx.dev_mute_ramp_duration_ms =
  8843. common.default_mute_ramp_duration_ms;
  8844. common.voice[i].stream_rx.stream_mute = common.default_mute_val;
  8845. common.voice[i].stream_tx.stream_mute = common.default_mute_val;
  8846. common.voice[i].dev_tx.port_id = 0x100B;
  8847. common.voice[i].dev_rx.port_id = 0x100A;
  8848. common.voice[i].dev_tx.dev_id = 0;
  8849. common.voice[i].dev_rx.dev_id = 0;
  8850. common.voice[i].dev_tx.no_of_channels = 0;
  8851. common.voice[i].dev_rx.no_of_channels = 0;
  8852. common.voice[i].dev_tx.sample_rate = 8000;
  8853. common.voice[i].dev_rx.sample_rate = 8000;
  8854. common.voice[i].dev_tx.bits_per_sample = 16;
  8855. common.voice[i].dev_rx.bits_per_sample = 16;
  8856. memset(&common.voice[i].dev_tx.channel_mapping, 0,
  8857. VSS_CHANNEL_MAPPING_SIZE);
  8858. memset(&common.voice[i].dev_rx.channel_mapping, 0,
  8859. VSS_CHANNEL_MAPPING_SIZE);
  8860. common.voice[i].sidetone_gain = 0x512;
  8861. common.voice[i].dtmf_rx_detect_en = 0;
  8862. common.voice[i].lch_mode = 0;
  8863. common.voice[i].disable_topology = false;
  8864. common.voice[i].voc_state = VOC_INIT;
  8865. INIT_WORK(&common.voice[i].voice_mic_break_work,
  8866. voice_mic_break_work_fn);
  8867. init_waitqueue_head(&common.voice[i].mvm_wait);
  8868. init_waitqueue_head(&common.voice[i].cvs_wait);
  8869. init_waitqueue_head(&common.voice[i].cvp_wait);
  8870. mutex_init(&common.voice[i].lock);
  8871. }
  8872. if (voice_init_cal_data())
  8873. pr_err("%s: Could not init cal data!\n", __func__);
  8874. if (rc == 0) {
  8875. module_initialized = true;
  8876. hyp_assigned = false;
  8877. }
  8878. pr_debug("%s: rc=%d\n", __func__, rc);
  8879. return rc;
  8880. }
  8881. void voice_exit(void)
  8882. {
  8883. q6core_destroy_uevent_data(common.uevent_data);
  8884. voice_delete_cal_data();
  8885. free_cal_map_table();
  8886. }