q6asm.c 135 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061
  1. /*
  2. * Copyright (c) 2012-2016, 2019 The Linux Foundation. All rights reserved.
  3. * Author: Brian Swetland <swetland@google.com>
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/fs.h>
  16. #include <linux/mutex.h>
  17. #include <linux/wait.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/sched.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/miscdevice.h>
  23. #include <linux/delay.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/slab.h>
  26. #include <linux/msm_audio.h>
  27. #include <linux/memory_alloc.h>
  28. #include <linux/debugfs.h>
  29. #include <linux/time.h>
  30. #include <linux/atomic.h>
  31. #include <linux/msm_audio_ion.h>
  32. #include <linux/mm.h>
  33. #include <asm/ioctls.h>
  34. #include <mach/memory.h>
  35. #include <mach/debug_mm.h>
  36. #include <sound/apr_audio-v2.h>
  37. #include <sound/q6asm-v2.h>
  38. #include <sound/q6audio-v2.h>
  39. #include "audio_acdb.h"
  40. #define TRUE 0x01
  41. #define FALSE 0x00
  42. #define FRAME_NUM (8)
  43. /* TODO, combine them together */
  44. static DEFINE_MUTEX(session_lock);
  45. struct asm_mmap {
  46. atomic_t ref_cnt;
  47. void *apr;
  48. };
  49. static struct asm_mmap this_mmap;
  50. /* session id: 0 reserved */
  51. static struct audio_client *session[SESSION_MAX+1];
  52. struct asm_buffer_node {
  53. struct list_head list;
  54. uint32_t buf_addr_lsw;
  55. uint32_t mmap_hdl;
  56. };
  57. static int32_t q6asm_srvc_callback(struct apr_client_data *data, void *priv);
  58. static int32_t q6asm_callback(struct apr_client_data *data, void *priv);
  59. static void q6asm_add_hdr(struct audio_client *ac, struct apr_hdr *hdr,
  60. uint32_t pkt_size, uint32_t cmd_flg);
  61. static void q6asm_add_hdr_custom_topology(struct audio_client *ac,
  62. struct apr_hdr *hdr,
  63. uint32_t pkt_size, uint32_t cmd_flg);
  64. static void q6asm_add_hdr_async(struct audio_client *ac, struct apr_hdr *hdr,
  65. uint32_t pkt_size, uint32_t cmd_flg);
  66. static int q6asm_memory_map_regions(struct audio_client *ac, int dir,
  67. uint32_t bufsz, uint32_t bufcnt,
  68. bool is_contiguous);
  69. static int q6asm_memory_unmap_regions(struct audio_client *ac, int dir);
  70. static void q6asm_reset_buf_state(struct audio_client *ac);
  71. static int q6asm_map_channels(u8 *channel_mapping, uint32_t channels);
  72. void *q6asm_mmap_apr_reg(void);
  73. static int q6asm_is_valid_session(struct apr_client_data *data, void *priv);
  74. /* for ASM custom topology */
  75. static struct audio_buffer common_buf[2];
  76. static struct audio_client common_client;
  77. static int set_custom_topology;
  78. static int topology_map_handle;
  79. #ifdef CONFIG_DEBUG_FS
  80. #define OUT_BUFFER_SIZE 56
  81. #define IN_BUFFER_SIZE 24
  82. static struct timeval out_cold_tv;
  83. static struct timeval out_warm_tv;
  84. static struct timeval out_cont_tv;
  85. static struct timeval in_cont_tv;
  86. static long out_enable_flag;
  87. static long in_enable_flag;
  88. static struct dentry *out_dentry;
  89. static struct dentry *in_dentry;
  90. static int in_cont_index;
  91. /*This var is used to keep track of first write done for cold output latency */
  92. static int out_cold_index;
  93. static char *out_buffer;
  94. static char *in_buffer;
  95. u32 score;
  96. int q6asm_mmap_apr_dereg(void)
  97. {
  98. int c;
  99. c = atomic_sub_return(1, &this_mmap.ref_cnt);
  100. if (c == 0) {
  101. apr_deregister(this_mmap.apr);
  102. pr_debug("%s: APR De-Register common port\n", __func__);
  103. } else if (c < 0) {
  104. pr_err("%s: APR Common Port Already Closed\n", __func__);
  105. atomic_set(&this_mmap.ref_cnt, 0);
  106. }
  107. return 0;
  108. }
  109. static int audio_output_latency_dbgfs_open(struct inode *inode,
  110. struct file *file)
  111. {
  112. file->private_data = inode->i_private;
  113. return 0;
  114. }
  115. static ssize_t audio_output_latency_dbgfs_read(struct file *file,
  116. char __user *buf, size_t count, loff_t *ppos)
  117. {
  118. if (out_buffer == NULL) {
  119. pr_err("%s: out_buffer is null\n", __func__);
  120. return 0;
  121. }
  122. if (count < OUT_BUFFER_SIZE) {
  123. pr_err("%s: read size %d exceeds buf size %zd\n", __func__,
  124. OUT_BUFFER_SIZE, count);
  125. return 0;
  126. }
  127. snprintf(out_buffer, OUT_BUFFER_SIZE, "%ld,%ld,%ld,%ld,%ld,%ld,",\
  128. out_cold_tv.tv_sec, out_cold_tv.tv_usec, out_warm_tv.tv_sec,\
  129. out_warm_tv.tv_usec, out_cont_tv.tv_sec, out_cont_tv.tv_usec);
  130. return simple_read_from_buffer(buf, OUT_BUFFER_SIZE, ppos,
  131. out_buffer, OUT_BUFFER_SIZE);
  132. }
  133. static ssize_t audio_output_latency_dbgfs_write(struct file *file,
  134. const char __user *buf, size_t count, loff_t *ppos)
  135. {
  136. char *temp;
  137. if (count > 2*sizeof(char))
  138. return -EINVAL;
  139. else
  140. temp = kmalloc(2*sizeof(char), GFP_KERNEL);
  141. out_cold_index = 0;
  142. if (temp) {
  143. if (copy_from_user(temp, buf, 2*sizeof(char))) {
  144. kfree(temp);
  145. return -EFAULT;
  146. }
  147. if (!kstrtol(temp, 10, &out_enable_flag)) {
  148. kfree(temp);
  149. return count;
  150. }
  151. kfree(temp);
  152. }
  153. return -EINVAL;
  154. }
  155. static const struct file_operations audio_output_latency_debug_fops = {
  156. .open = audio_output_latency_dbgfs_open,
  157. .read = audio_output_latency_dbgfs_read,
  158. .write = audio_output_latency_dbgfs_write
  159. };
  160. static int audio_input_latency_dbgfs_open(struct inode *inode,
  161. struct file *file)
  162. {
  163. file->private_data = inode->i_private;
  164. return 0;
  165. }
  166. static ssize_t audio_input_latency_dbgfs_read(struct file *file,
  167. char __user *buf, size_t count, loff_t *ppos)
  168. {
  169. if (in_buffer == NULL) {
  170. pr_err("%s: in_buffer is null\n", __func__);
  171. return 0;
  172. }
  173. if (count < IN_BUFFER_SIZE) {
  174. pr_err("%s: read size %d exceeds buf size %zd\n", __func__,
  175. IN_BUFFER_SIZE, count);
  176. return 0;
  177. }
  178. snprintf(in_buffer, IN_BUFFER_SIZE, "%ld,%ld,",\
  179. in_cont_tv.tv_sec, in_cont_tv.tv_usec);
  180. return simple_read_from_buffer(buf, IN_BUFFER_SIZE, ppos,
  181. in_buffer, IN_BUFFER_SIZE);
  182. }
  183. static ssize_t audio_input_latency_dbgfs_write(struct file *file,
  184. const char __user *buf, size_t count, loff_t *ppos)
  185. {
  186. char *temp;
  187. if (count > 2*sizeof(char))
  188. return -EINVAL;
  189. else
  190. temp = kmalloc(2*sizeof(char), GFP_KERNEL);
  191. if (temp) {
  192. if (copy_from_user(temp, buf, 2*sizeof(char))) {
  193. kfree(temp);
  194. return -EFAULT;
  195. }
  196. if (!kstrtol(temp, 10, &in_enable_flag)) {
  197. kfree(temp);
  198. return count;
  199. }
  200. kfree(temp);
  201. }
  202. return -EINVAL;
  203. }
  204. static const struct file_operations audio_input_latency_debug_fops = {
  205. .open = audio_input_latency_dbgfs_open,
  206. .read = audio_input_latency_dbgfs_read,
  207. .write = audio_input_latency_dbgfs_write
  208. };
  209. static void config_debug_fs_write_cb(void)
  210. {
  211. if (out_enable_flag) {
  212. /* For first Write done log the time and reset
  213. out_cold_index*/
  214. if (out_cold_index != 1) {
  215. do_gettimeofday(&out_cold_tv);
  216. pr_debug("COLD: apr_send_pkt at %ld sec %ld microsec\n",
  217. out_cold_tv.tv_sec,\
  218. out_cold_tv.tv_usec);
  219. out_cold_index = 1;
  220. }
  221. pr_debug("out_enable_flag %ld",\
  222. out_enable_flag);
  223. }
  224. }
  225. static void config_debug_fs_read_cb(void)
  226. {
  227. if (in_enable_flag) {
  228. /* when in_cont_index == 7, DSP would be
  229. * writing into the 8th 512 byte buffer and this
  230. * timestamp is tapped here.Once done it then writes
  231. * to 9th 512 byte buffer.These two buffers(8th, 9th)
  232. * reach the test application in 5th iteration and that
  233. * timestamp is tapped at user level. The difference
  234. * of these two timestamps gives us the time between
  235. * the time at which dsp started filling the sample
  236. * required and when it reached the test application.
  237. * Hence continuous input latency
  238. */
  239. if (in_cont_index == 7) {
  240. do_gettimeofday(&in_cont_tv);
  241. pr_err("In_CONT:previous read buffer done at %ld sec %ld microsec\n",
  242. in_cont_tv.tv_sec, in_cont_tv.tv_usec);
  243. }
  244. in_cont_index++;
  245. }
  246. }
  247. static void config_debug_fs_reset_index(void)
  248. {
  249. in_cont_index = 0;
  250. }
  251. static void config_debug_fs_run(void)
  252. {
  253. if (out_enable_flag) {
  254. do_gettimeofday(&out_cold_tv);
  255. pr_debug("COLD: apr_send_pkt at %ld sec %ld microsec\n",\
  256. out_cold_tv.tv_sec, out_cold_tv.tv_usec);
  257. }
  258. }
  259. static void config_debug_fs_write(struct audio_buffer *ab)
  260. {
  261. if (out_enable_flag) {
  262. char zero_pattern[2] = {0x00, 0x00};
  263. /* If First two byte is non zero and last two byte
  264. is zero then it is warm output pattern */
  265. if ((strncmp(((char *)ab->data), zero_pattern, 2)) &&
  266. (!strncmp(((char *)ab->data + 2), zero_pattern, 2))) {
  267. do_gettimeofday(&out_warm_tv);
  268. pr_debug("WARM:apr_send_pkt at %ld sec %ld microsec\n",
  269. out_warm_tv.tv_sec,\
  270. out_warm_tv.tv_usec);
  271. pr_debug("Warm Pattern Matched");
  272. }
  273. /* If First two byte is zero and last two byte is
  274. non zero then it is cont ouput pattern */
  275. else if ((!strncmp(((char *)ab->data), zero_pattern, 2))
  276. && (strncmp(((char *)ab->data + 2), zero_pattern, 2))) {
  277. do_gettimeofday(&out_cont_tv);
  278. pr_debug("CONT:apr_send_pkt at %ld sec %ld microsec\n",
  279. out_cont_tv.tv_sec,\
  280. out_cont_tv.tv_usec);
  281. pr_debug("Cont Pattern Matched");
  282. }
  283. }
  284. }
  285. static void config_debug_fs_init(void)
  286. {
  287. out_buffer = kmalloc(OUT_BUFFER_SIZE, GFP_KERNEL);
  288. if (out_buffer == NULL) {
  289. pr_err("%s: kmalloc() for out_buffer failed\n", __func__);
  290. goto outbuf_fail;
  291. }
  292. in_buffer = kmalloc(IN_BUFFER_SIZE, GFP_KERNEL);
  293. if (in_buffer == NULL) {
  294. pr_err("%s: kmalloc() for in_buffer failed\n", __func__);
  295. goto inbuf_fail;
  296. }
  297. out_dentry = debugfs_create_file("audio_out_latency_measurement_node",\
  298. S_IRUGO | S_IWUSR | S_IWGRP,\
  299. NULL, NULL, &audio_output_latency_debug_fops);
  300. if (IS_ERR(out_dentry)) {
  301. pr_err("%s: debugfs_create_file failed\n", __func__);
  302. goto file_fail;
  303. }
  304. in_dentry = debugfs_create_file("audio_in_latency_measurement_node",\
  305. S_IRUGO | S_IWUSR | S_IWGRP,\
  306. NULL, NULL, &audio_input_latency_debug_fops);
  307. if (IS_ERR(in_dentry)) {
  308. pr_err("%s: debugfs_create_file failed\n", __func__);
  309. goto file_fail;
  310. }
  311. return;
  312. file_fail:
  313. kfree(in_buffer);
  314. inbuf_fail:
  315. kfree(out_buffer);
  316. outbuf_fail:
  317. in_buffer = NULL;
  318. out_buffer = NULL;
  319. return;
  320. }
  321. #else
  322. static void config_debug_fs_write(struct audio_buffer *ab)
  323. {
  324. return;
  325. }
  326. static void config_debug_fs_run(void)
  327. {
  328. return;
  329. }
  330. static void config_debug_fs_reset_index(void)
  331. {
  332. return;
  333. }
  334. static void config_debug_fs_read_cb(void)
  335. {
  336. return;
  337. }
  338. static void config_debug_fs_write_cb(void)
  339. {
  340. return;
  341. }
  342. static void config_debug_fs_init(void)
  343. {
  344. return;
  345. }
  346. #endif
  347. static int q6asm_session_alloc(struct audio_client *ac)
  348. {
  349. int n;
  350. for (n = 1; n <= SESSION_MAX; n++) {
  351. if (!session[n]) {
  352. session[n] = ac;
  353. return n;
  354. }
  355. }
  356. return -ENOMEM;
  357. }
  358. static bool q6asm_is_valid_audio_client(struct audio_client *ac)
  359. {
  360. int n;
  361. for (n = 1; n <= SESSION_MAX; n++) {
  362. if (session[n] == ac)
  363. return 1;
  364. }
  365. return 0;
  366. }
  367. static void q6asm_session_free(struct audio_client *ac)
  368. {
  369. pr_debug("%s: sessionid[%d]\n", __func__, ac->session);
  370. rtac_remove_popp_from_adm_devices(ac->session);
  371. session[ac->session] = 0;
  372. ac->session = 0;
  373. ac->perf_mode = LEGACY_PCM_MODE;
  374. ac->fptr_cache_ops = NULL;
  375. return;
  376. }
  377. void send_asm_custom_topology(struct audio_client *ac)
  378. {
  379. struct acdb_cal_block cal_block;
  380. struct cmd_set_topologies asm_top;
  381. struct asm_buffer_node *buf_node = NULL;
  382. struct list_head *ptr, *next;
  383. int result;
  384. int size = 4096;
  385. if (!set_custom_topology)
  386. return;
  387. get_asm_custom_topology(&cal_block);
  388. if (cal_block.cal_size == 0) {
  389. pr_debug("%s: no cal to send addr= 0x%pa\n",
  390. __func__, &cal_block.cal_paddr);
  391. return;
  392. }
  393. common_client.mmap_apr = q6asm_mmap_apr_reg();
  394. common_client.apr = common_client.mmap_apr;
  395. if (common_client.mmap_apr == NULL) {
  396. pr_err("%s: q6asm_mmap_apr_reg failed\n",
  397. __func__);
  398. result = -EPERM;
  399. goto mmap_fail;
  400. }
  401. /* Only call this once */
  402. set_custom_topology = 0;
  403. /* Use first asm buf to map memory */
  404. if (common_client.port[IN].buf == NULL) {
  405. pr_err("%s: common buf is NULL\n",
  406. __func__);
  407. goto err_map;
  408. }
  409. common_client.port[IN].buf->phys = cal_block.cal_paddr;
  410. result = q6asm_memory_map_regions(&common_client,
  411. IN, size, 1, 1);
  412. if (result < 0) {
  413. pr_err("%s: mmap did not work! addr = 0x%pa, size = %zd\n",
  414. __func__, &cal_block.cal_paddr,
  415. cal_block.cal_size);
  416. goto err_map;
  417. }
  418. list_for_each_safe(ptr, next,
  419. &common_client.port[IN].mem_map_handle) {
  420. buf_node = list_entry(ptr, struct asm_buffer_node,
  421. list);
  422. if (buf_node->buf_addr_lsw == cal_block.cal_paddr) {
  423. topology_map_handle = buf_node->mmap_hdl;
  424. break;
  425. }
  426. }
  427. q6asm_add_hdr_custom_topology(ac, &asm_top.hdr,
  428. APR_PKT_SIZE(APR_HDR_SIZE,
  429. sizeof(asm_top)), TRUE);
  430. atomic_set(&ac->mem_state, 1);
  431. asm_top.hdr.opcode = ASM_CMD_ADD_TOPOLOGIES;
  432. asm_top.payload_addr_lsw = cal_block.cal_paddr;
  433. asm_top.payload_addr_msw = 0;
  434. asm_top.mem_map_handle = topology_map_handle;
  435. asm_top.payload_size = cal_block.cal_size;
  436. pr_debug("%s: Sending ASM_CMD_ADD_TOPOLOGIES payload = 0x%x, size = %d, map handle = 0x%x\n",
  437. __func__, asm_top.payload_addr_lsw,
  438. asm_top.payload_size, asm_top.mem_map_handle);
  439. result = apr_send_pkt(ac->apr, (uint32_t *) &asm_top);
  440. if (result < 0) {
  441. pr_err("%s: Set topologies failed payload = 0x%x\n",
  442. __func__, cal_block.cal_paddr);
  443. goto err_unmap;
  444. }
  445. result = wait_event_timeout(ac->mem_wait,
  446. (atomic_read(&ac->mem_state) == 0), 5*HZ);
  447. if (!result) {
  448. pr_err("%s: Set topologies failed after timedout payload = 0x%x\n",
  449. __func__, cal_block.cal_paddr);
  450. goto err_unmap;
  451. }
  452. return;
  453. err_unmap:
  454. q6asm_memory_unmap_regions(ac, IN);
  455. err_map:
  456. q6asm_mmap_apr_dereg();
  457. set_custom_topology = 1;
  458. mmap_fail:
  459. return;
  460. }
  461. int q6asm_map_rtac_block(struct rtac_cal_block_data *cal_block)
  462. {
  463. int result = 0;
  464. struct asm_buffer_node *buf_node = NULL;
  465. struct list_head *ptr, *next;
  466. pr_debug("%s\n", __func__);
  467. if (cal_block == NULL) {
  468. pr_err("%s: cal_block is NULL!\n",
  469. __func__);
  470. result = -EINVAL;
  471. goto done;
  472. }
  473. if (cal_block->cal_data.paddr == 0) {
  474. pr_debug("%s: No address to map!\n",
  475. __func__);
  476. result = -EINVAL;
  477. goto done;
  478. }
  479. if (common_client.mmap_apr == NULL) {
  480. common_client.mmap_apr = q6asm_mmap_apr_reg();
  481. if (common_client.mmap_apr == NULL) {
  482. pr_err("%s: q6asm_mmap_apr_reg failed\n",
  483. __func__);
  484. result = -EPERM;
  485. goto done;
  486. }
  487. }
  488. if (cal_block->map_data.map_size == 0) {
  489. pr_debug("%s: map size is 0!\n",
  490. __func__);
  491. result = -EINVAL;
  492. goto done;
  493. }
  494. /* Use second asm buf to map memory */
  495. if (common_client.port[OUT].buf == NULL) {
  496. pr_err("%s: common buf is NULL\n",
  497. __func__);
  498. result = -EINVAL;
  499. goto done;
  500. }
  501. common_client.port[OUT].buf->phys = cal_block->cal_data.paddr;
  502. result = q6asm_memory_map_regions(&common_client,
  503. OUT, cal_block->map_data.map_size, 1, 1);
  504. if (result < 0) {
  505. pr_err("%s: mmap did not work! addr = 0x%x, size = %d\n",
  506. __func__, cal_block->cal_data.paddr,
  507. cal_block->map_data.map_size);
  508. goto done;
  509. }
  510. list_for_each_safe(ptr, next,
  511. &common_client.port[OUT].mem_map_handle) {
  512. buf_node = list_entry(ptr, struct asm_buffer_node,
  513. list);
  514. if (buf_node->buf_addr_lsw == cal_block->cal_data.paddr) {
  515. cal_block->map_data.map_handle = buf_node->mmap_hdl;
  516. break;
  517. }
  518. }
  519. result = q6asm_mmap_apr_dereg();
  520. if (result < 0) {
  521. pr_err("%s: q6asm_mmap_apr_dereg failed, err %d\n",
  522. __func__, result);
  523. } else {
  524. common_client.mmap_apr = NULL;
  525. }
  526. done:
  527. return result;
  528. }
  529. int q6asm_unmap_rtac_block(uint32_t *mem_map_handle)
  530. {
  531. int result = 0;
  532. int result2 = 0;
  533. pr_debug("%s\n", __func__);
  534. if (mem_map_handle == NULL) {
  535. pr_debug("%s: Map handle is NULL, nothing to unmap\n",
  536. __func__);
  537. goto done;
  538. }
  539. if (*mem_map_handle == 0) {
  540. pr_debug("%s: Map handle is 0, nothing to unmap\n",
  541. __func__);
  542. goto done;
  543. }
  544. if (common_client.mmap_apr == NULL) {
  545. common_client.mmap_apr = q6asm_mmap_apr_reg();
  546. if (common_client.mmap_apr == NULL) {
  547. pr_err("%s: q6asm_mmap_apr_reg failed\n",
  548. __func__);
  549. result = -EPERM;
  550. goto done;
  551. }
  552. }
  553. result2 = q6asm_memory_unmap_regions(&common_client, OUT);
  554. if (result2 < 0) {
  555. pr_err("%s: unmap failed, err %d\n",
  556. __func__, result2);
  557. result = result2;
  558. } else {
  559. mem_map_handle = 0;
  560. }
  561. result2 = q6asm_mmap_apr_dereg();
  562. if (result2 < 0) {
  563. pr_err("%s: q6asm_mmap_apr_dereg failed, err %d\n",
  564. __func__, result2);
  565. result = result2;
  566. } else {
  567. common_client.mmap_apr = NULL;
  568. }
  569. done:
  570. return result;
  571. }
  572. int q6asm_unmap_cal_blocks(void)
  573. {
  574. int result = 0;
  575. int result2 = 0;
  576. pr_debug("%s\n", __func__);
  577. if (topology_map_handle == 0)
  578. goto done;
  579. if (common_client.mmap_apr == NULL) {
  580. common_client.mmap_apr = q6asm_mmap_apr_reg();
  581. if (common_client.mmap_apr == NULL) {
  582. pr_err("%s: q6asm_mmap_apr_reg failed\n",
  583. __func__);
  584. result = -EPERM;
  585. goto done;
  586. }
  587. }
  588. result2 = q6asm_memory_unmap_regions(&common_client, IN);
  589. if (result2 < 0) {
  590. pr_err("%s: unmap failed, err %d\n",
  591. __func__, result2);
  592. result = result2;
  593. } else {
  594. topology_map_handle = 0;
  595. }
  596. result2 = q6asm_mmap_apr_dereg();
  597. if (result2 < 0) {
  598. pr_err("%s: q6asm_mmap_apr_dereg failed, err %d\n",
  599. __func__, result2);
  600. result = result2;
  601. } else {
  602. common_client.mmap_apr = NULL;
  603. }
  604. set_custom_topology = 0;
  605. done:
  606. return result;
  607. }
  608. int q6asm_audio_client_buf_free(unsigned int dir,
  609. struct audio_client *ac)
  610. {
  611. struct audio_port_data *port;
  612. int cnt = 0;
  613. int rc = 0;
  614. pr_debug("%s: Session id %d\n", __func__, ac->session);
  615. mutex_lock(&ac->cmd_lock);
  616. if (ac->io_mode & SYNC_IO_MODE) {
  617. port = &ac->port[dir];
  618. if (!port->buf) {
  619. mutex_unlock(&ac->cmd_lock);
  620. return 0;
  621. }
  622. cnt = port->max_buf_cnt - 1;
  623. if (cnt >= 0) {
  624. rc = q6asm_memory_unmap_regions(ac, dir);
  625. if (rc < 0)
  626. pr_err("%s CMD Memory_unmap_regions failed\n",
  627. __func__);
  628. }
  629. while (cnt >= 0) {
  630. if (port->buf[cnt].data) {
  631. if (!rc || atomic_read(&ac->reset))
  632. msm_audio_ion_free(
  633. port->buf[cnt].client,
  634. port->buf[cnt].handle);
  635. port->buf[cnt].client = NULL;
  636. port->buf[cnt].handle = NULL;
  637. port->buf[cnt].data = NULL;
  638. port->buf[cnt].phys = 0;
  639. --(port->max_buf_cnt);
  640. }
  641. --cnt;
  642. }
  643. kfree(port->buf);
  644. port->buf = NULL;
  645. }
  646. mutex_unlock(&ac->cmd_lock);
  647. return 0;
  648. }
  649. int q6asm_audio_client_buf_free_contiguous(unsigned int dir,
  650. struct audio_client *ac)
  651. {
  652. struct audio_port_data *port;
  653. int cnt = 0;
  654. int rc = 0;
  655. pr_debug("%s: Session id %d\n", __func__, ac->session);
  656. mutex_lock(&ac->cmd_lock);
  657. port = &ac->port[dir];
  658. if (!port->buf) {
  659. mutex_unlock(&ac->cmd_lock);
  660. return 0;
  661. }
  662. cnt = port->max_buf_cnt - 1;
  663. if (cnt >= 0) {
  664. rc = q6asm_memory_unmap(ac, port->buf[0].phys, dir);
  665. if (rc < 0)
  666. pr_err("%s CMD Memory_unmap_regions failed\n",
  667. __func__);
  668. }
  669. if (port->buf[0].data) {
  670. pr_debug("%s:data[%p]phys[%p][%p] , client[%p] handle[%p]\n",
  671. __func__,
  672. (void *)port->buf[0].data,
  673. (void *)port->buf[0].phys,
  674. (void *)&port->buf[0].phys,
  675. (void *)port->buf[0].client,
  676. (void *)port->buf[0].handle);
  677. if (!rc || atomic_read(&ac->reset))
  678. msm_audio_ion_free(port->buf[0].client,
  679. port->buf[0].handle);
  680. port->buf[0].client = NULL;
  681. port->buf[0].handle = NULL;
  682. }
  683. while (cnt >= 0) {
  684. port->buf[cnt].data = NULL;
  685. port->buf[cnt].phys = 0;
  686. cnt--;
  687. }
  688. port->max_buf_cnt = 0;
  689. kfree(port->buf);
  690. port->buf = NULL;
  691. mutex_unlock(&ac->cmd_lock);
  692. return 0;
  693. }
  694. void q6asm_audio_client_free(struct audio_client *ac)
  695. {
  696. int loopcnt;
  697. struct audio_port_data *port;
  698. if (!ac || !ac->session)
  699. return;
  700. mutex_lock(&session_lock);
  701. pr_debug("%s: Session id %d\n", __func__, ac->session);
  702. if (ac->io_mode & SYNC_IO_MODE) {
  703. for (loopcnt = 0; loopcnt <= OUT; loopcnt++) {
  704. port = &ac->port[loopcnt];
  705. if (!port->buf)
  706. continue;
  707. pr_debug("%s:loopcnt = %d\n", __func__, loopcnt);
  708. q6asm_audio_client_buf_free(loopcnt, ac);
  709. }
  710. }
  711. apr_deregister(ac->apr2);
  712. ac->apr2 = NULL;
  713. apr_deregister(ac->apr);
  714. ac->apr = NULL;
  715. ac->mmap_apr = NULL;
  716. rtac_set_asm_handle(ac->session, ac->apr);
  717. q6asm_session_free(ac);
  718. q6asm_mmap_apr_dereg();
  719. pr_debug("%s: APR De-Register\n", __func__);
  720. /*done:*/
  721. kfree(ac);
  722. ac = NULL;
  723. mutex_unlock(&session_lock);
  724. return;
  725. }
  726. int q6asm_set_io_mode(struct audio_client *ac, uint32_t mode1)
  727. {
  728. uint32_t mode;
  729. if (ac == NULL) {
  730. pr_err("%s APR handle NULL\n", __func__);
  731. return -EINVAL;
  732. }
  733. ac->io_mode &= 0xFF00;
  734. mode = (mode1 & 0xF);
  735. pr_debug("%s ac->mode after anding with FF00:0x[%x],\n",
  736. __func__, ac->io_mode);
  737. if ((mode == ASYNC_IO_MODE) || (mode == SYNC_IO_MODE)) {
  738. ac->io_mode |= mode1;
  739. pr_debug("%s:Set Mode to 0x[%x]\n", __func__, ac->io_mode);
  740. return 0;
  741. } else {
  742. pr_err("%s:Not an valid IO Mode:%d\n", __func__, ac->io_mode);
  743. return -EINVAL;
  744. }
  745. }
  746. void *q6asm_mmap_apr_reg(void)
  747. {
  748. if ((atomic_read(&this_mmap.ref_cnt) == 0) ||
  749. (this_mmap.apr == NULL)) {
  750. this_mmap.apr = apr_register("ADSP", "ASM", \
  751. (apr_fn)q6asm_srvc_callback,\
  752. 0x0FFFFFFFF, &this_mmap);
  753. if (this_mmap.apr == NULL) {
  754. pr_debug("%s Unable to register APR ASM common port\n",
  755. __func__);
  756. goto fail;
  757. }
  758. }
  759. atomic_inc(&this_mmap.ref_cnt);
  760. return this_mmap.apr;
  761. fail:
  762. return NULL;
  763. }
  764. struct audio_client *q6asm_audio_client_alloc(app_cb cb, void *priv)
  765. {
  766. struct audio_client *ac;
  767. int n;
  768. int lcnt = 0;
  769. ac = kzalloc(sizeof(struct audio_client), GFP_KERNEL);
  770. if (!ac)
  771. return NULL;
  772. mutex_lock(&session_lock);
  773. n = q6asm_session_alloc(ac);
  774. if (n <= 0) {
  775. mutex_unlock(&session_lock);
  776. goto fail_session;
  777. }
  778. ac->session = n;
  779. ac->cb = cb;
  780. ac->priv = priv;
  781. ac->io_mode = SYNC_IO_MODE;
  782. ac->perf_mode = LEGACY_PCM_MODE;
  783. ac->fptr_cache_ops = NULL;
  784. /* DSP expects stream id from 1 */
  785. ac->stream_id = 1;
  786. ac->apr = apr_register("ADSP", "ASM", \
  787. (apr_fn)q6asm_callback,\
  788. ((ac->session) << 8 | 0x0001),\
  789. ac);
  790. if (ac->apr == NULL) {
  791. pr_err("%s Registration with APR failed\n", __func__);
  792. mutex_unlock(&session_lock);
  793. goto fail_apr1;
  794. }
  795. ac->apr2 = apr_register("ADSP", "ASM", \
  796. (apr_fn)q6asm_callback,\
  797. ((ac->session) << 8 | 0x0002),\
  798. ac);
  799. if (ac->apr2 == NULL) {
  800. pr_err("%s Registration with APR-2 failed\n", __func__);
  801. mutex_unlock(&session_lock);
  802. goto fail_apr2;
  803. }
  804. rtac_set_asm_handle(n, ac->apr);
  805. pr_debug("%s Registering the common port with APR\n", __func__);
  806. ac->mmap_apr = q6asm_mmap_apr_reg();
  807. if (ac->mmap_apr == NULL) {
  808. mutex_unlock(&session_lock);
  809. goto fail_mmap;
  810. }
  811. init_waitqueue_head(&ac->cmd_wait);
  812. init_waitqueue_head(&ac->time_wait);
  813. init_waitqueue_head(&ac->mem_wait);
  814. atomic_set(&ac->time_flag, 1);
  815. atomic_set(&ac->reset, 0);
  816. INIT_LIST_HEAD(&ac->port[0].mem_map_handle);
  817. INIT_LIST_HEAD(&ac->port[1].mem_map_handle);
  818. pr_debug("%s: mem_map_handle list init'ed\n", __func__);
  819. mutex_init(&ac->cmd_lock);
  820. for (lcnt = 0; lcnt <= OUT; lcnt++) {
  821. mutex_init(&ac->port[lcnt].lock);
  822. spin_lock_init(&ac->port[lcnt].dsp_lock);
  823. }
  824. atomic_set(&ac->cmd_state, 0);
  825. atomic_set(&ac->nowait_cmd_cnt, 0);
  826. atomic_set(&ac->mem_state, 0);
  827. send_asm_custom_topology(ac);
  828. pr_debug("%s: session[%d]\n", __func__, ac->session);
  829. mutex_unlock(&session_lock);
  830. return ac;
  831. fail_mmap:
  832. apr_deregister(ac->apr2);
  833. fail_apr2:
  834. apr_deregister(ac->apr);
  835. fail_apr1:
  836. q6asm_session_free(ac);
  837. fail_session:
  838. kfree(ac);
  839. return NULL;
  840. }
  841. struct audio_client *q6asm_get_audio_client(int session_id)
  842. {
  843. if (session_id == ASM_CONTROL_SESSION) {
  844. return &common_client;
  845. }
  846. if ((session_id <= 0) || (session_id > SESSION_MAX)) {
  847. pr_err("%s: invalid session: %d\n", __func__, session_id);
  848. goto err;
  849. }
  850. if (!session[session_id]) {
  851. pr_err("%s: session not active: %d\n", __func__, session_id);
  852. goto err;
  853. }
  854. return session[session_id];
  855. err:
  856. return NULL;
  857. }
  858. int q6asm_audio_client_buf_alloc(unsigned int dir,
  859. struct audio_client *ac,
  860. unsigned int bufsz,
  861. unsigned int bufcnt)
  862. {
  863. int cnt = 0;
  864. int rc = 0;
  865. struct audio_buffer *buf;
  866. size_t len;
  867. if (!(ac) || ((dir != IN) && (dir != OUT)))
  868. return -EINVAL;
  869. pr_debug("%s: session[%d]bufsz[%d]bufcnt[%d]\n", __func__, ac->session,
  870. bufsz, bufcnt);
  871. if (ac->session <= 0 || ac->session > 8)
  872. goto fail;
  873. if (ac->io_mode & SYNC_IO_MODE) {
  874. if (ac->port[dir].buf) {
  875. pr_debug("%s: buffer already allocated\n", __func__);
  876. return 0;
  877. }
  878. if (bufcnt != FRAME_NUM)
  879. goto fail;
  880. mutex_lock(&ac->cmd_lock);
  881. if (bufcnt > (LONG_MAX/sizeof(struct audio_buffer))) {
  882. pr_err("%s: Buffer size overflows", __func__);
  883. mutex_unlock(&ac->cmd_lock);
  884. goto fail;
  885. }
  886. buf = kzalloc(((sizeof(struct audio_buffer))*bufcnt),
  887. GFP_KERNEL);
  888. if (!buf) {
  889. mutex_unlock(&ac->cmd_lock);
  890. goto fail;
  891. }
  892. ac->port[dir].buf = buf;
  893. while (cnt < bufcnt) {
  894. if (bufsz > 0) {
  895. if (!buf[cnt].data) {
  896. rc = msm_audio_ion_alloc("audio_client",
  897. &buf[cnt].client, &buf[cnt].handle,
  898. bufsz,
  899. (ion_phys_addr_t *)&buf[cnt].phys,
  900. &len,
  901. &buf[cnt].data);
  902. if (rc) {
  903. pr_err("%s: ION Get Physical for AUDIO failed, rc = %d\n",
  904. __func__, rc);
  905. mutex_unlock(&ac->cmd_lock);
  906. goto fail;
  907. }
  908. buf[cnt].used = 1;
  909. buf[cnt].size = bufsz;
  910. buf[cnt].actual_size = bufsz;
  911. pr_debug("%s data[%p]phys[%p][%p]\n",
  912. __func__,
  913. (void *)buf[cnt].data,
  914. (void *)buf[cnt].phys,
  915. (void *)&buf[cnt].phys);
  916. cnt++;
  917. }
  918. }
  919. }
  920. ac->port[dir].max_buf_cnt = cnt;
  921. mutex_unlock(&ac->cmd_lock);
  922. rc = q6asm_memory_map_regions(ac, dir, bufsz, cnt, 0);
  923. if (rc < 0) {
  924. pr_err("%s:CMD Memory_map_regions failed\n", __func__);
  925. goto fail;
  926. }
  927. }
  928. return 0;
  929. fail:
  930. q6asm_audio_client_buf_free(dir, ac);
  931. return -EINVAL;
  932. }
  933. int q6asm_audio_client_buf_alloc_contiguous(unsigned int dir,
  934. struct audio_client *ac,
  935. unsigned int bufsz,
  936. unsigned int bufcnt)
  937. {
  938. int cnt = 0;
  939. int rc = 0;
  940. struct audio_buffer *buf;
  941. size_t len;
  942. int bytes_to_alloc;
  943. if (!(ac) || ((dir != IN) && (dir != OUT)))
  944. return -EINVAL;
  945. pr_info("%s: session[%d]bufsz[%d]bufcnt[%d]\n",
  946. __func__, ac->session,
  947. bufsz, bufcnt);
  948. if (ac->session <= 0 || ac->session > 8)
  949. goto fail;
  950. if (ac->port[dir].buf) {
  951. pr_info("%s: buffer already allocated\n", __func__);
  952. return 0;
  953. }
  954. if (bufcnt == 0) {
  955. pr_err("%s: invalid buffer count\n", __func__);
  956. return -EINVAL;
  957. }
  958. mutex_lock(&ac->cmd_lock);
  959. buf = kzalloc(((sizeof(struct audio_buffer))*bufcnt),
  960. GFP_KERNEL);
  961. if (!buf) {
  962. mutex_unlock(&ac->cmd_lock);
  963. goto fail;
  964. }
  965. ac->port[dir].buf = buf;
  966. /* check for integer overflow */
  967. if ((bufcnt > 0) && ((INT_MAX / bufcnt) < bufsz)) {
  968. pr_err("%s: integer overflow\n", __func__);
  969. mutex_unlock(&ac->cmd_lock);
  970. goto fail;
  971. }
  972. bytes_to_alloc = bufsz * bufcnt;
  973. /* The size to allocate should be multiple of 4K bytes */
  974. bytes_to_alloc = PAGE_ALIGN(bytes_to_alloc);
  975. rc = msm_audio_ion_alloc("audio_client", &buf[0].client, &buf[0].handle,
  976. bytes_to_alloc,
  977. (ion_phys_addr_t *)&buf[0].phys, &len,
  978. &buf[0].data);
  979. if (rc) {
  980. pr_err("%s: Audio ION alloc is failed, rc = %d\n",
  981. __func__, rc);
  982. mutex_unlock(&ac->cmd_lock);
  983. goto fail;
  984. }
  985. buf[0].used = dir ^ 1;
  986. buf[0].size = bufsz;
  987. buf[0].actual_size = bufsz;
  988. cnt = 1;
  989. while (cnt < bufcnt) {
  990. if (bufsz > 0) {
  991. buf[cnt].data = buf[0].data + (cnt * bufsz);
  992. buf[cnt].phys = buf[0].phys + (cnt * bufsz);
  993. if (!buf[cnt].data) {
  994. pr_err("%s Buf alloc failed\n",
  995. __func__);
  996. mutex_unlock(&ac->cmd_lock);
  997. goto fail;
  998. }
  999. buf[cnt].used = dir ^ 1;
  1000. buf[cnt].size = bufsz;
  1001. buf[cnt].actual_size = bufsz;
  1002. pr_debug("%s data[%p]phys[%p][%p]\n", __func__,
  1003. (void *)buf[cnt].data,
  1004. (void *)buf[cnt].phys,
  1005. (void *)&buf[cnt].phys);
  1006. }
  1007. cnt++;
  1008. }
  1009. ac->port[dir].max_buf_cnt = cnt;
  1010. mutex_unlock(&ac->cmd_lock);
  1011. rc = q6asm_memory_map_regions(ac, dir, bufsz, cnt, 1);
  1012. if (rc < 0) {
  1013. pr_err("%s:CMD Memory_map_regions failed\n", __func__);
  1014. goto fail;
  1015. }
  1016. return 0;
  1017. fail:
  1018. q6asm_audio_client_buf_free_contiguous(dir, ac);
  1019. return -EINVAL;
  1020. }
  1021. static int32_t q6asm_srvc_callback(struct apr_client_data *data, void *priv)
  1022. {
  1023. uint32_t sid = 0;
  1024. uint32_t dir = 0;
  1025. uint32_t i = IN;
  1026. uint32_t *payload;
  1027. unsigned long dsp_flags;
  1028. struct asm_buffer_node *buf_node = NULL;
  1029. struct list_head *ptr, *next;
  1030. struct audio_client *ac = NULL;
  1031. struct audio_port_data *port;
  1032. if (!data) {
  1033. pr_err("%s: Invalid CB\n", __func__);
  1034. return 0;
  1035. }
  1036. payload = data->payload;
  1037. if (data->opcode == RESET_EVENTS) {
  1038. pr_debug("%s: Reset event is received: %d %d apr[%p]\n",
  1039. __func__,
  1040. data->reset_event,
  1041. data->reset_proc,
  1042. this_mmap.apr);
  1043. atomic_set(&this_mmap.ref_cnt, 0);
  1044. apr_reset(this_mmap.apr);
  1045. this_mmap.apr = NULL;
  1046. for (; i <= OUT; i++) {
  1047. list_for_each_safe(ptr, next,
  1048. &common_client.port[i].mem_map_handle) {
  1049. buf_node = list_entry(ptr,
  1050. struct asm_buffer_node,
  1051. list);
  1052. if (buf_node->buf_addr_lsw ==
  1053. common_client.port[i].buf->phys) {
  1054. list_del(&buf_node->list);
  1055. kfree(buf_node);
  1056. }
  1057. }
  1058. pr_debug("%s:Clearing custom topology\n", __func__);
  1059. }
  1060. reset_custom_topology_flags();
  1061. set_custom_topology = 1;
  1062. topology_map_handle = 0;
  1063. rtac_clear_mapping(ASM_RTAC_CAL);
  1064. return 0;
  1065. }
  1066. sid = (data->token >> 8) & 0x0F;
  1067. ac = q6asm_get_audio_client(sid);
  1068. if (!ac) {
  1069. pr_debug("%s: session[%d] already freed\n", __func__, sid);
  1070. return 0;
  1071. }
  1072. pr_debug("%s:ptr0[0x%x]ptr1[0x%x]opcode[0x%x] token[0x%x]payload_s[%d] src[%d] dest[%d]sid[%d]dir[%d]\n",
  1073. __func__, payload[0], payload[1], data->opcode, data->token,
  1074. data->payload_size, data->src_port, data->dest_port, sid, dir);
  1075. pr_debug("%s:Payload = [0x%x] status[0x%x]\n",
  1076. __func__, payload[0], payload[1]);
  1077. if (data->opcode == APR_BASIC_RSP_RESULT) {
  1078. switch (payload[0]) {
  1079. case ASM_CMD_SHARED_MEM_MAP_REGIONS:
  1080. case ASM_CMD_SHARED_MEM_UNMAP_REGIONS:
  1081. case ASM_CMD_ADD_TOPOLOGIES:
  1082. if (payload[1] != 0) {
  1083. pr_err("%s: cmd = 0x%x returned error = 0x%x sid:%d\n",
  1084. __func__, payload[0], payload[1], sid);
  1085. if (payload[0] ==
  1086. ASM_CMD_SHARED_MEM_UNMAP_REGIONS)
  1087. atomic_set(&ac->unmap_cb_success, 0);
  1088. } else {
  1089. if (payload[0] ==
  1090. ASM_CMD_SHARED_MEM_UNMAP_REGIONS)
  1091. atomic_set(&ac->unmap_cb_success, 1);
  1092. }
  1093. if (atomic_cmpxchg(&ac->mem_state, 1, 0))
  1094. wake_up(&ac->mem_wait);
  1095. pr_debug("%s:Payload = [0x%x] status[0x%x]\n",
  1096. __func__, payload[0], payload[1]);
  1097. break;
  1098. default:
  1099. pr_debug("%s:command[0x%x] not expecting rsp\n",
  1100. __func__, payload[0]);
  1101. break;
  1102. }
  1103. return 0;
  1104. }
  1105. dir = (data->token & 0x0F);
  1106. if (dir != IN && dir != OUT) {
  1107. pr_err("%s: Invalid audio port index: %d\n", __func__, dir);
  1108. return 0;
  1109. }
  1110. port = &ac->port[dir];
  1111. switch (data->opcode) {
  1112. case ASM_CMDRSP_SHARED_MEM_MAP_REGIONS:{
  1113. pr_debug("%s:PL#0[0x%x]PL#1 [0x%x] dir=%x s_id=%x\n",
  1114. __func__, payload[0], payload[1], dir, sid);
  1115. spin_lock_irqsave(&port->dsp_lock, dsp_flags);
  1116. if (atomic_cmpxchg(&ac->mem_state, 1, 0)) {
  1117. ac->port[dir].tmp_hdl = payload[0];
  1118. wake_up(&ac->mem_wait);
  1119. }
  1120. spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
  1121. break;
  1122. }
  1123. case ASM_CMD_SHARED_MEM_UNMAP_REGIONS:{
  1124. pr_debug("%s:PL#0[0x%x]PL#1 [0x%x]\n",
  1125. __func__, payload[0], payload[1]);
  1126. spin_lock_irqsave(&port->dsp_lock, dsp_flags);
  1127. if (atomic_cmpxchg(&ac->mem_state, 1, 0))
  1128. wake_up(&ac->mem_wait);
  1129. spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
  1130. break;
  1131. }
  1132. default:
  1133. pr_debug("%s:command[0x%x]success [0x%x]\n",
  1134. __func__, payload[0], payload[1]);
  1135. }
  1136. if (ac->cb)
  1137. ac->cb(data->opcode, data->token,
  1138. data->payload, ac->priv);
  1139. return 0;
  1140. }
  1141. static int32_t is_no_wait_cmd_rsp(uint32_t opcode, uint32_t *cmd_type)
  1142. {
  1143. if (opcode == APR_BASIC_RSP_RESULT) {
  1144. if (cmd_type != NULL) {
  1145. switch (cmd_type[0]) {
  1146. case ASM_SESSION_CMD_RUN_V2:
  1147. case ASM_SESSION_CMD_PAUSE:
  1148. case ASM_DATA_CMD_EOS:
  1149. return 1;
  1150. default:
  1151. break;
  1152. }
  1153. } else
  1154. pr_err("%s: null pointer!", __func__);
  1155. } else if (opcode == ASM_DATA_EVENT_RENDERED_EOS)
  1156. return 1;
  1157. return 0;
  1158. }
  1159. static int32_t q6asm_callback(struct apr_client_data *data, void *priv)
  1160. {
  1161. int i = 0;
  1162. struct audio_client *ac = (struct audio_client *)priv;
  1163. uint32_t token;
  1164. unsigned long dsp_flags;
  1165. uint32_t *payload;
  1166. uint32_t wakeup_flag = 1;
  1167. int32_t ret = 0;
  1168. if ((ac == NULL) || (data == NULL)) {
  1169. pr_err("ac or priv NULL\n");
  1170. return -EINVAL;
  1171. }
  1172. if (!q6asm_is_valid_audio_client(ac)) {
  1173. pr_err("%s: audio client pointer is invalid, ac = %p\n",
  1174. __func__, ac);
  1175. return -EINVAL;
  1176. }
  1177. if (ac->session <= 0 || ac->session > 8) {
  1178. pr_err("%s:Session ID is invalid, session = %d\n", __func__,
  1179. ac->session);
  1180. return -EINVAL;
  1181. }
  1182. payload = data->payload;
  1183. if ((atomic_read(&ac->nowait_cmd_cnt) > 0) &&
  1184. is_no_wait_cmd_rsp(data->opcode, payload)) {
  1185. pr_debug("%s: nowait_cmd_cnt %d\n",
  1186. __func__,
  1187. atomic_read(&ac->nowait_cmd_cnt));
  1188. atomic_dec(&ac->nowait_cmd_cnt);
  1189. wakeup_flag = 0;
  1190. }
  1191. if (data->opcode == RESET_EVENTS) {
  1192. atomic_set(&ac->reset, 1);
  1193. if (ac->apr == NULL)
  1194. ac->apr = ac->apr2;
  1195. pr_debug("q6asm_callback: Reset event is received: %d %d apr[%p]\n",
  1196. data->reset_event, data->reset_proc, ac->apr);
  1197. if (ac->cb)
  1198. ac->cb(data->opcode, data->token,
  1199. (uint32_t *)data->payload, ac->priv);
  1200. apr_reset(ac->apr);
  1201. ac->apr = NULL;
  1202. atomic_set(&ac->time_flag, 0);
  1203. atomic_set(&ac->cmd_state, 0);
  1204. wake_up(&ac->time_wait);
  1205. wake_up(&ac->cmd_wait);
  1206. return 0;
  1207. }
  1208. pr_debug("%s: session[%d]opcode[0x%x] token[0x%x]payload_s[%d] src[%d] dest[%d]\n",
  1209. __func__,
  1210. ac->session, data->opcode,
  1211. data->token, data->payload_size, data->src_port,
  1212. data->dest_port);
  1213. if ((data->opcode != ASM_DATA_EVENT_RENDERED_EOS) &&
  1214. (data->opcode != ASM_DATA_EVENT_EOS) &&
  1215. (data->opcode != ASM_SESSION_EVENT_RX_UNDERFLOW))
  1216. pr_debug("%s:Payload = [0x%x] status[0x%x]\n",
  1217. __func__, payload[0], payload[1]);
  1218. if (data->opcode == APR_BASIC_RSP_RESULT) {
  1219. token = data->token;
  1220. if (payload[1] != 0) {
  1221. pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
  1222. __func__, payload[0], payload[1]);
  1223. }
  1224. switch (payload[0]) {
  1225. case ASM_STREAM_CMD_SET_PP_PARAMS_V2:
  1226. if (rtac_make_asm_callback(ac->session, payload,
  1227. data->payload_size))
  1228. break;
  1229. case ASM_SESSION_CMD_PAUSE:
  1230. case ASM_SESSION_CMD_SUSPEND:
  1231. case ASM_DATA_CMD_EOS:
  1232. case ASM_STREAM_CMD_CLOSE:
  1233. case ASM_STREAM_CMD_FLUSH:
  1234. case ASM_SESSION_CMD_RUN_V2:
  1235. case ASM_SESSION_CMD_REGISTER_FORX_OVERFLOW_EVENTS:
  1236. case ASM_STREAM_CMD_FLUSH_READBUFS:
  1237. pr_debug("%s:Payload = [0x%x]\n", __func__, payload[0]);
  1238. ret = q6asm_is_valid_session(data, priv);
  1239. if (ret != 0)
  1240. return ret;
  1241. case ASM_STREAM_CMD_OPEN_READ_V3:
  1242. case ASM_STREAM_CMD_OPEN_WRITE_V3:
  1243. case ASM_STREAM_CMD_OPEN_READWRITE_V2:
  1244. case ASM_STREAM_CMD_OPEN_LOOPBACK_V2:
  1245. case ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2:
  1246. case ASM_STREAM_CMD_SET_ENCDEC_PARAM:
  1247. case ASM_DATA_CMD_REMOVE_INITIAL_SILENCE:
  1248. case ASM_DATA_CMD_REMOVE_TRAILING_SILENCE:
  1249. case ASM_SESSION_CMD_REGISTER_FOR_RX_UNDERFLOW_EVENTS:
  1250. pr_debug("%s:Payload = [0x%x]stat[0x%x]\n",
  1251. __func__, payload[0], payload[1]);
  1252. if (atomic_read(&ac->cmd_state) && wakeup_flag) {
  1253. atomic_set(&ac->cmd_state, 0);
  1254. wake_up(&ac->cmd_wait);
  1255. }
  1256. if (ac->cb)
  1257. ac->cb(data->opcode, data->token,
  1258. (uint32_t *)data->payload, ac->priv);
  1259. break;
  1260. case ASM_CMD_ADD_TOPOLOGIES:
  1261. pr_debug("%s:Payload = [0x%x]stat[0x%x]\n",
  1262. __func__, payload[0], payload[1]);
  1263. if (atomic_read(&ac->mem_state) && wakeup_flag) {
  1264. atomic_set(&ac->mem_state, 0);
  1265. wake_up(&ac->mem_wait);
  1266. }
  1267. if (ac->cb)
  1268. ac->cb(data->opcode, data->token,
  1269. (uint32_t *)data->payload, ac->priv);
  1270. break;
  1271. case ASM_STREAM_CMD_GET_PP_PARAMS_V2:
  1272. pr_debug("%s: ASM_STREAM_CMD_GET_PP_PARAMS_V2\n",
  1273. __func__);
  1274. /* Should only come here if there is an APR */
  1275. /* error or malformed APR packet. Otherwise */
  1276. /* response will be returned as */
  1277. /* ASM_STREAM_CMDRSP_GET_PP_PARAMS_V2 */
  1278. if (payload[1] != 0) {
  1279. pr_err("%s: ASM get param error = %d, resuming\n",
  1280. __func__, payload[1]);
  1281. rtac_make_asm_callback(ac->session, payload,
  1282. data->payload_size);
  1283. }
  1284. break;
  1285. default:
  1286. pr_debug("%s:command[0x%x] not expecting rsp\n",
  1287. __func__, payload[0]);
  1288. break;
  1289. }
  1290. return 0;
  1291. }
  1292. switch (data->opcode) {
  1293. case ASM_DATA_EVENT_WRITE_DONE_V2:{
  1294. struct audio_port_data *port = &ac->port[IN];
  1295. pr_debug("%s: Rxed opcode[0x%x] status[0x%x] token[%d]",
  1296. __func__, payload[0], payload[1],
  1297. data->token);
  1298. if (ac->io_mode & SYNC_IO_MODE) {
  1299. if (port->buf == NULL) {
  1300. pr_err("%s: Unexpected Write Done\n",
  1301. __func__);
  1302. return -EINVAL;
  1303. }
  1304. spin_lock_irqsave(&port->dsp_lock, dsp_flags);
  1305. if (port->buf[data->token].phys !=
  1306. payload[0]) {
  1307. pr_err("Buf expected[%p]rxed[%p]\n",\
  1308. (void *)port->buf[data->token].phys,\
  1309. (void *)payload[0]);
  1310. spin_unlock_irqrestore(&port->dsp_lock,
  1311. dsp_flags);
  1312. return -EINVAL;
  1313. }
  1314. token = data->token;
  1315. port->buf[token].used = 1;
  1316. spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
  1317. config_debug_fs_write_cb();
  1318. for (i = 0; i < port->max_buf_cnt; i++)
  1319. pr_debug("%d ", port->buf[i].used);
  1320. }
  1321. break;
  1322. }
  1323. case ASM_STREAM_CMDRSP_GET_PP_PARAMS_V2:
  1324. pr_debug("%s: ASM_STREAM_CMDRSP_GET_PP_PARAMS_V2\n", __func__);
  1325. if (payload[0] != 0)
  1326. pr_err("%s: ASM_STREAM_CMDRSP_GET_PP_PARAMS_V2 returned error = 0x%x\n",
  1327. __func__, payload[0]);
  1328. rtac_make_asm_callback(ac->session, payload,
  1329. data->payload_size);
  1330. break;
  1331. case ASM_DATA_EVENT_READ_DONE_V2:{
  1332. struct audio_port_data *port = &ac->port[OUT];
  1333. config_debug_fs_read_cb();
  1334. pr_debug("%s:R-D: status=%d buff_add=%x act_size=%d offset=%d\n",
  1335. __func__, payload[READDONE_IDX_STATUS],
  1336. payload[READDONE_IDX_BUFADD_LSW],
  1337. payload[READDONE_IDX_SIZE],
  1338. payload[READDONE_IDX_OFFSET]);
  1339. pr_debug("%s:R-D:msw_ts=%d lsw_ts=%d memmap_hdl=%x flags=%d id=%d num=%d\n",
  1340. __func__, payload[READDONE_IDX_MSW_TS],
  1341. payload[READDONE_IDX_LSW_TS],
  1342. payload[READDONE_IDX_MEMMAP_HDL],
  1343. payload[READDONE_IDX_FLAGS],
  1344. payload[READDONE_IDX_SEQ_ID],
  1345. payload[READDONE_IDX_NUMFRAMES]);
  1346. if (ac->io_mode & SYNC_IO_MODE) {
  1347. if (port->buf == NULL) {
  1348. pr_err("%s: Unexpected Write Done\n", __func__);
  1349. return -EINVAL;
  1350. }
  1351. spin_lock_irqsave(&port->dsp_lock, dsp_flags);
  1352. token = data->token;
  1353. port->buf[token].used = 0;
  1354. if (port->buf[token].phys !=
  1355. payload[READDONE_IDX_BUFADD_LSW]) {
  1356. pr_err("Buf expected[%p]rxed[%p]\n",\
  1357. (void *)port->buf[token].phys,\
  1358. (void *)payload[READDONE_IDX_BUFADD_LSW]);
  1359. spin_unlock_irqrestore(&port->dsp_lock,
  1360. dsp_flags);
  1361. break;
  1362. }
  1363. port->buf[token].actual_size =
  1364. payload[READDONE_IDX_SIZE];
  1365. spin_unlock_irqrestore(&port->dsp_lock, dsp_flags);
  1366. }
  1367. break;
  1368. }
  1369. case ASM_DATA_EVENT_EOS:
  1370. case ASM_DATA_EVENT_RENDERED_EOS:
  1371. pr_debug("%s:EOS ACK received: rxed opcode[0x%x]\n",
  1372. __func__, data->opcode);
  1373. break;
  1374. case ASM_SESSION_EVENTX_OVERFLOW:
  1375. pr_debug("ASM_SESSION_EVENTX_OVERFLOW\n");
  1376. break;
  1377. case ASM_SESSION_EVENT_RX_UNDERFLOW:
  1378. pr_debug("ASM_SESSION_EVENT_RX_UNDERFLOW\n");
  1379. break;
  1380. case ASM_SESSION_CMDRSP_GET_SESSIONTIME_V3:
  1381. pr_debug("%s: ASM_SESSION_CMDRSP_GET_SESSIONTIME_V3, payload[0] = %d, payload[1] = %d, payload[2] = %d\n",
  1382. __func__,
  1383. payload[0], payload[1], payload[2]);
  1384. ac->time_stamp = (uint64_t)(((uint64_t)payload[2] << 32) |
  1385. payload[1]);
  1386. if (atomic_cmpxchg(&ac->time_flag, 1, 0))
  1387. wake_up(&ac->time_wait);
  1388. break;
  1389. case ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY:
  1390. case ASM_DATA_EVENT_ENC_SR_CM_CHANGE_NOTIFY:
  1391. pr_debug("%s: ASM_DATA_EVENT_SR_CM_CHANGE_NOTIFY, payload[0] = %d, payload[1] = %d, payload[2] = %d, payload[3] = %d\n",
  1392. __func__,
  1393. payload[0], payload[1], payload[2],
  1394. payload[3]);
  1395. break;
  1396. }
  1397. if (ac->cb)
  1398. ac->cb(data->opcode, data->token,
  1399. data->payload, ac->priv);
  1400. return 0;
  1401. }
  1402. void *q6asm_is_cpu_buf_avail(int dir, struct audio_client *ac, uint32_t *size,
  1403. uint32_t *index)
  1404. {
  1405. void *data;
  1406. unsigned char idx;
  1407. struct audio_port_data *port;
  1408. if (!ac || ((dir != IN) && (dir != OUT)))
  1409. return NULL;
  1410. if (ac->io_mode & SYNC_IO_MODE) {
  1411. port = &ac->port[dir];
  1412. mutex_lock(&port->lock);
  1413. idx = port->cpu_buf;
  1414. if (port->buf == NULL) {
  1415. pr_debug("%s:Buffer pointer null\n", __func__);
  1416. mutex_unlock(&port->lock);
  1417. return NULL;
  1418. }
  1419. /* dir 0: used = 0 means buf in use
  1420. dir 1: used = 1 means buf in use */
  1421. if (port->buf[idx].used == dir) {
  1422. /* To make it more robust, we could loop and get the
  1423. next avail buf, its risky though */
  1424. pr_debug("%s:Next buf idx[0x%x] not available, dir[%d]\n",
  1425. __func__, idx, dir);
  1426. mutex_unlock(&port->lock);
  1427. return NULL;
  1428. }
  1429. *size = port->buf[idx].actual_size;
  1430. *index = port->cpu_buf;
  1431. data = port->buf[idx].data;
  1432. pr_debug("%s:session[%d]index[%d] data[%p]size[%d]\n",
  1433. __func__,
  1434. ac->session,
  1435. port->cpu_buf,
  1436. data, *size);
  1437. /* By default increase the cpu_buf cnt
  1438. user accesses this function,increase cpu
  1439. buf(to avoid another api)*/
  1440. port->buf[idx].used = dir;
  1441. port->cpu_buf = ((port->cpu_buf + 1) & (port->max_buf_cnt - 1));
  1442. mutex_unlock(&port->lock);
  1443. return data;
  1444. }
  1445. return NULL;
  1446. }
  1447. void *q6asm_is_cpu_buf_avail_nolock(int dir, struct audio_client *ac,
  1448. uint32_t *size, uint32_t *index)
  1449. {
  1450. void *data;
  1451. unsigned char idx;
  1452. struct audio_port_data *port;
  1453. if (!ac || ((dir != IN) && (dir != OUT)))
  1454. return NULL;
  1455. port = &ac->port[dir];
  1456. idx = port->cpu_buf;
  1457. if (port->buf == NULL) {
  1458. pr_debug("%s:Buffer pointer null\n", __func__);
  1459. return NULL;
  1460. }
  1461. /*
  1462. * dir 0: used = 0 means buf in use
  1463. * dir 1: used = 1 means buf in use
  1464. */
  1465. if (port->buf[idx].used == dir) {
  1466. /*
  1467. * To make it more robust, we could loop and get the
  1468. * next avail buf, its risky though
  1469. */
  1470. pr_debug("%s:Next buf idx[0x%x] not available, dir[%d]\n",
  1471. __func__, idx, dir);
  1472. return NULL;
  1473. }
  1474. *size = port->buf[idx].actual_size;
  1475. *index = port->cpu_buf;
  1476. data = port->buf[idx].data;
  1477. pr_debug("%s:session[%d]index[%d] data[%p]size[%d]\n",
  1478. __func__, ac->session, port->cpu_buf,
  1479. data, *size);
  1480. /*
  1481. * By default increase the cpu_buf cnt
  1482. * user accesses this function,increase cpu
  1483. * buf(to avoid another api)
  1484. */
  1485. port->buf[idx].used = dir;
  1486. port->cpu_buf = ((port->cpu_buf + 1) & (port->max_buf_cnt - 1));
  1487. return data;
  1488. }
  1489. int q6asm_is_dsp_buf_avail(int dir, struct audio_client *ac)
  1490. {
  1491. int ret = -1;
  1492. struct audio_port_data *port;
  1493. uint32_t idx;
  1494. if (!ac || (dir != OUT))
  1495. return ret;
  1496. if (ac->io_mode & SYNC_IO_MODE) {
  1497. port = &ac->port[dir];
  1498. mutex_lock(&port->lock);
  1499. idx = port->dsp_buf;
  1500. if (port->buf[idx].used == (dir ^ 1)) {
  1501. /* To make it more robust, we could loop and get the
  1502. next avail buf, its risky though */
  1503. pr_err("Next buf idx[0x%x] not available, dir[%d]\n",
  1504. idx, dir);
  1505. mutex_unlock(&port->lock);
  1506. return ret;
  1507. }
  1508. pr_debug("%s: session[%d]dsp_buf=%d cpu_buf=%d\n", __func__,
  1509. ac->session, port->dsp_buf, port->cpu_buf);
  1510. ret = ((port->dsp_buf != port->cpu_buf) ? 0 : -1);
  1511. mutex_unlock(&port->lock);
  1512. }
  1513. return ret;
  1514. }
  1515. static void __q6asm_add_hdr(struct audio_client *ac, struct apr_hdr *hdr,
  1516. uint32_t pkt_size, uint32_t cmd_flg, uint32_t stream_id)
  1517. {
  1518. pr_debug("%s:pkt_size=%d cmd_flg=%d session=%d stream_id=%d\n",
  1519. __func__, pkt_size, cmd_flg, ac->session, stream_id);
  1520. if (ac->apr == NULL) {
  1521. pr_err("%s: ac->apr is NULL", __func__);
  1522. return;
  1523. }
  1524. mutex_lock(&ac->cmd_lock);
  1525. hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, \
  1526. APR_HDR_LEN(sizeof(struct apr_hdr)),\
  1527. APR_PKT_VER);
  1528. hdr->src_svc = ((struct apr_svc *)ac->apr)->id;
  1529. hdr->src_domain = APR_DOMAIN_APPS;
  1530. hdr->dest_svc = APR_SVC_ASM;
  1531. hdr->dest_domain = APR_DOMAIN_ADSP;
  1532. hdr->src_port = ((ac->session << 8) & 0xFF00) | (stream_id);
  1533. hdr->dest_port = ((ac->session << 8) & 0xFF00) | (stream_id);
  1534. if (cmd_flg) {
  1535. hdr->token = ac->session;
  1536. }
  1537. hdr->pkt_size = pkt_size;
  1538. mutex_unlock(&ac->cmd_lock);
  1539. return;
  1540. }
  1541. static void q6asm_add_hdr(struct audio_client *ac, struct apr_hdr *hdr,
  1542. uint32_t pkt_size, uint32_t cmd_flg)
  1543. {
  1544. __q6asm_add_hdr(ac, hdr, pkt_size, cmd_flg, ac->stream_id);
  1545. return;
  1546. }
  1547. static void q6asm_stream_add_hdr(struct audio_client *ac, struct apr_hdr *hdr,
  1548. uint32_t pkt_size, uint32_t cmd_flg, int32_t stream_id)
  1549. {
  1550. __q6asm_add_hdr(ac, hdr, pkt_size, cmd_flg, stream_id);
  1551. return;
  1552. }
  1553. static void __q6asm_add_hdr_async(struct audio_client *ac, struct apr_hdr *hdr,
  1554. uint32_t pkt_size, uint32_t cmd_flg, uint32_t stream_id)
  1555. {
  1556. pr_debug("%s pkt_size = %d, cmd_flg = %d, session = %d stream_id=%d\n",
  1557. __func__, pkt_size, cmd_flg, ac->session, stream_id);
  1558. hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, \
  1559. APR_HDR_LEN(sizeof(struct apr_hdr)),\
  1560. APR_PKT_VER);
  1561. if (ac->apr == NULL) {
  1562. pr_err("%s: ac->apr is NULL", __func__);
  1563. return;
  1564. }
  1565. hdr->src_svc = ((struct apr_svc *)ac->apr)->id;
  1566. hdr->src_domain = APR_DOMAIN_APPS;
  1567. hdr->dest_svc = APR_SVC_ASM;
  1568. hdr->dest_domain = APR_DOMAIN_ADSP;
  1569. hdr->src_port = ((ac->session << 8) & 0xFF00) | (stream_id);
  1570. hdr->dest_port = ((ac->session << 8) & 0xFF00) | (stream_id);
  1571. if (cmd_flg) {
  1572. hdr->token = ac->session;
  1573. }
  1574. hdr->pkt_size = pkt_size;
  1575. return;
  1576. }
  1577. static void q6asm_add_hdr_async(struct audio_client *ac, struct apr_hdr *hdr,
  1578. uint32_t pkt_size, uint32_t cmd_flg)
  1579. {
  1580. __q6asm_add_hdr_async(ac, hdr, pkt_size, cmd_flg, ac->stream_id);
  1581. return;
  1582. }
  1583. static void q6asm_stream_add_hdr_async(struct audio_client *ac,
  1584. struct apr_hdr *hdr, uint32_t pkt_size,
  1585. uint32_t cmd_flg, int32_t stream_id)
  1586. {
  1587. __q6asm_add_hdr_async(ac, hdr, pkt_size, cmd_flg, stream_id);
  1588. return;
  1589. }
  1590. static void q6asm_add_hdr_custom_topology(struct audio_client *ac,
  1591. struct apr_hdr *hdr,
  1592. uint32_t pkt_size, uint32_t cmd_flg)
  1593. {
  1594. pr_debug("%s:pkt_size=%d cmd_flg=%d session=%d\n", __func__, pkt_size,
  1595. cmd_flg, ac->session);
  1596. if (ac->apr == NULL) {
  1597. pr_err("%s: ac->apr is NULL", __func__);
  1598. return;
  1599. }
  1600. mutex_lock(&ac->cmd_lock);
  1601. hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, \
  1602. APR_HDR_LEN(sizeof(struct apr_hdr)),\
  1603. APR_PKT_VER);
  1604. hdr->src_svc = ((struct apr_svc *)ac->apr)->id;
  1605. hdr->src_domain = APR_DOMAIN_APPS;
  1606. hdr->dest_svc = APR_SVC_ASM;
  1607. hdr->dest_domain = APR_DOMAIN_ADSP;
  1608. hdr->src_port = ((ac->session << 8) & 0xFF00) | 0x01;
  1609. hdr->dest_port = 0;
  1610. if (cmd_flg) {
  1611. hdr->token = ((ac->session << 8) | 0x0001) ;
  1612. }
  1613. hdr->pkt_size = pkt_size;
  1614. mutex_unlock(&ac->cmd_lock);
  1615. return;
  1616. }
  1617. static void q6asm_add_mmaphdr(struct audio_client *ac, struct apr_hdr *hdr,
  1618. u32 pkt_size, u32 cmd_flg, u32 token)
  1619. {
  1620. pr_debug("%s:pkt size=%d cmd_flg=%d\n", __func__, pkt_size, cmd_flg);
  1621. hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, \
  1622. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1623. hdr->src_port = 0;
  1624. hdr->dest_port = 0;
  1625. if (cmd_flg) {
  1626. hdr->token = token;
  1627. }
  1628. hdr->pkt_size = pkt_size;
  1629. return;
  1630. }
  1631. static int __q6asm_open_read(struct audio_client *ac,
  1632. uint32_t format, uint16_t bits_per_sample)
  1633. {
  1634. int rc = 0x00;
  1635. struct asm_stream_cmd_open_read_v3 open;
  1636. config_debug_fs_reset_index();
  1637. if ((ac == NULL) || (ac->apr == NULL)) {
  1638. pr_err("%s: APR handle NULL\n", __func__);
  1639. return -EINVAL;
  1640. }
  1641. pr_debug("%s:session[%d]", __func__, ac->session);
  1642. q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
  1643. atomic_set(&ac->cmd_state, 1);
  1644. open.hdr.opcode = ASM_STREAM_CMD_OPEN_READ_V3;
  1645. /* Stream prio : High, provide meta info with encoded frames */
  1646. open.src_endpointype = ASM_END_POINT_DEVICE_MATRIX;
  1647. open.preprocopo_id = get_asm_topology();
  1648. if (open.preprocopo_id == 0)
  1649. open.preprocopo_id = ASM_STREAM_POSTPROC_TOPO_ID_NONE;
  1650. open.bits_per_sample = bits_per_sample;
  1651. open.mode_flags = 0x0;
  1652. //if (ac->perf_mode)
  1653. if (ac->perf_mode == LOW_LATENCY_PCM_MODE)
  1654. open.mode_flags |= ASM_LOW_LATENCY_TX_STREAM_SESSION << ASM_SHIFT_STREAM_PERF_MODE_FLAG_IN_OPEN_READ;
  1655. else
  1656. open.mode_flags |= ASM_LEGACY_STREAM_SESSION << ASM_SHIFT_STREAM_PERF_MODE_FLAG_IN_OPEN_READ;
  1657. switch (format) {
  1658. case FORMAT_LINEAR_PCM:
  1659. open.mode_flags |= 0x00;
  1660. open.enc_cfg_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V2;
  1661. break;
  1662. case FORMAT_MPEG4_AAC:
  1663. open.mode_flags |= BUFFER_META_ENABLE;
  1664. open.enc_cfg_id = ASM_MEDIA_FMT_AAC_V2;
  1665. break;
  1666. case FORMAT_V13K:
  1667. open.mode_flags |= BUFFER_META_ENABLE;
  1668. open.enc_cfg_id = ASM_MEDIA_FMT_V13K_FS;
  1669. break;
  1670. case FORMAT_EVRC:
  1671. open.mode_flags |= BUFFER_META_ENABLE;
  1672. open.enc_cfg_id = ASM_MEDIA_FMT_EVRC_FS;
  1673. break;
  1674. case FORMAT_AMRNB:
  1675. open.mode_flags |= BUFFER_META_ENABLE ;
  1676. open.enc_cfg_id = ASM_MEDIA_FMT_AMRNB_FS;
  1677. break;
  1678. case FORMAT_AMRWB:
  1679. open.mode_flags |= BUFFER_META_ENABLE ;
  1680. open.enc_cfg_id = ASM_MEDIA_FMT_AMRWB_FS;
  1681. break;
  1682. default:
  1683. pr_err("Invalid format[%d]\n", format);
  1684. goto fail_cmd;
  1685. }
  1686. rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
  1687. if (rc < 0) {
  1688. pr_err("open failed op[0x%x]rc[%d]\n", \
  1689. open.hdr.opcode, rc);
  1690. goto fail_cmd;
  1691. }
  1692. rc = wait_event_timeout(ac->cmd_wait,
  1693. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  1694. if (!rc) {
  1695. pr_err("%s: timeout. waited for open read rc[%d]\n", __func__,
  1696. rc);
  1697. goto fail_cmd;
  1698. }
  1699. ac->io_mode |= TUN_READ_IO_MODE;
  1700. return 0;
  1701. fail_cmd:
  1702. return -EINVAL;
  1703. }
  1704. int q6asm_open_read(struct audio_client *ac,
  1705. uint32_t format)
  1706. {
  1707. return __q6asm_open_read(ac, format, 16);
  1708. }
  1709. int q6asm_open_read_v2(struct audio_client *ac, uint32_t format,
  1710. uint16_t bits_per_sample)
  1711. {
  1712. return __q6asm_open_read(ac, format, bits_per_sample);
  1713. }
  1714. static int __q6asm_open_write(struct audio_client *ac, uint32_t format,
  1715. uint16_t bits_per_sample, uint32_t stream_id,
  1716. bool is_gapless_mode)
  1717. {
  1718. int rc = 0x00;
  1719. struct asm_stream_cmd_open_write_v3 open;
  1720. if ((ac == NULL) || (ac->apr == NULL)) {
  1721. pr_err("%s: APR handle NULL\n", __func__);
  1722. return -EINVAL;
  1723. }
  1724. pr_info("%s: session[%d] wr_format[0x%x]", __func__, ac->session,
  1725. format);
  1726. q6asm_stream_add_hdr(ac, &open.hdr, sizeof(open), TRUE, stream_id);
  1727. atomic_set(&ac->cmd_state, 1);
  1728. /*
  1729. * Updated the token field with stream/session for compressed playback
  1730. * Platform driver must know the the stream with which the command is
  1731. * associated
  1732. */
  1733. if (ac->io_mode & COMPRESSED_STREAM_IO)
  1734. open.hdr.token = ((ac->session << 8) & 0xFFFF00) |
  1735. (stream_id & 0xFF);
  1736. pr_debug("%s: token = 0x%x, stream_id %d, session 0x%x\n",
  1737. __func__, open.hdr.token, stream_id, ac->session);
  1738. open.hdr.opcode = ASM_STREAM_CMD_OPEN_WRITE_V3;
  1739. open.mode_flags = 0x00;
  1740. if (ac->perf_mode == ULTRA_LOW_LATENCY_PCM_MODE)
  1741. open.mode_flags |= ASM_ULTRA_LOW_LATENCY_STREAM_SESSION;
  1742. else if (ac->perf_mode == LOW_LATENCY_PCM_MODE)
  1743. open.mode_flags |= ASM_LOW_LATENCY_STREAM_SESSION;
  1744. else {
  1745. open.mode_flags |= ASM_LEGACY_STREAM_SESSION;
  1746. if (is_gapless_mode)
  1747. open.mode_flags |= 1 << ASM_SHIFT_GAPLESS_MODE_FLAG;
  1748. }
  1749. /* source endpoint : matrix */
  1750. open.sink_endpointype = ASM_END_POINT_DEVICE_MATRIX;
  1751. open.bits_per_sample = bits_per_sample;
  1752. pr_info("%s: session[%d] open.mode_flags:%x", __func__, ac->session,
  1753. open.mode_flags);
  1754. open.postprocopo_id = get_asm_topology();
  1755. if (open.postprocopo_id == 0)
  1756. open.postprocopo_id = ASM_STREAM_POSTPROC_TOPO_ID_NONE;
  1757. switch (format) {
  1758. case FORMAT_LINEAR_PCM:
  1759. open.dec_fmt_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V2;
  1760. break;
  1761. case FORMAT_MPEG4_AAC:
  1762. open.dec_fmt_id = ASM_MEDIA_FMT_AAC_V2;
  1763. break;
  1764. case FORMAT_MPEG4_MULTI_AAC:
  1765. open.dec_fmt_id = ASM_MEDIA_FMT_DOLBY_AAC;
  1766. break;
  1767. case FORMAT_WMA_V9:
  1768. open.dec_fmt_id = ASM_MEDIA_FMT_WMA_V9_V2;
  1769. break;
  1770. case FORMAT_WMA_V10PRO:
  1771. open.dec_fmt_id = ASM_MEDIA_FMT_WMA_V10PRO_V2;
  1772. break;
  1773. case FORMAT_MP3:
  1774. open.dec_fmt_id = ASM_MEDIA_FMT_MP3;
  1775. break;
  1776. case FORMAT_AC3:
  1777. open.dec_fmt_id = ASM_MEDIA_FMT_EAC3_DEC;
  1778. break;
  1779. case FORMAT_EAC3:
  1780. open.dec_fmt_id = ASM_MEDIA_FMT_EAC3_DEC;
  1781. break;
  1782. default:
  1783. pr_err("%s: Invalid format[%d]\n", __func__, format);
  1784. goto fail_cmd;
  1785. }
  1786. rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
  1787. if (rc < 0) {
  1788. pr_err("%s: open failed op[0x%x]rc[%d]\n", \
  1789. __func__, open.hdr.opcode, rc);
  1790. goto fail_cmd;
  1791. }
  1792. rc = wait_event_timeout(ac->cmd_wait,
  1793. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  1794. if (!rc) {
  1795. pr_err("%s: timeout. waited for open write rc[%d]\n", __func__,
  1796. rc);
  1797. goto fail_cmd;
  1798. }
  1799. ac->io_mode |= TUN_WRITE_IO_MODE;
  1800. return 0;
  1801. fail_cmd:
  1802. return -EINVAL;
  1803. }
  1804. int q6asm_open_write(struct audio_client *ac, uint32_t format)
  1805. {
  1806. return __q6asm_open_write(ac, format, 16, ac->stream_id,
  1807. false /*gapless*/);
  1808. }
  1809. int q6asm_open_write_v2(struct audio_client *ac, uint32_t format,
  1810. uint16_t bits_per_sample)
  1811. {
  1812. return __q6asm_open_write(ac, format, bits_per_sample,
  1813. ac->stream_id, false /*gapless*/);
  1814. }
  1815. int q6asm_stream_open_write_v2(struct audio_client *ac, uint32_t format,
  1816. uint16_t bits_per_sample, int32_t stream_id,
  1817. bool is_gapless_mode)
  1818. {
  1819. return __q6asm_open_write(ac, format, bits_per_sample,
  1820. stream_id, is_gapless_mode);
  1821. }
  1822. int q6asm_open_read_write(struct audio_client *ac,
  1823. uint32_t rd_format,
  1824. uint32_t wr_format)
  1825. {
  1826. int rc = 0x00;
  1827. struct asm_stream_cmd_open_readwrite_v2 open;
  1828. if ((ac == NULL) || (ac->apr == NULL)) {
  1829. pr_err("%s: APR handle NULL\n", __func__);
  1830. return -EINVAL;
  1831. }
  1832. pr_debug("%s: session[%d]", __func__, ac->session);
  1833. pr_debug("wr_format[0x%x]rd_format[0x%x]",
  1834. wr_format, rd_format);
  1835. ac->io_mode |= NT_MODE;
  1836. q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
  1837. atomic_set(&ac->cmd_state, 1);
  1838. open.hdr.opcode = ASM_STREAM_CMD_OPEN_READWRITE_V2;
  1839. open.mode_flags = BUFFER_META_ENABLE;
  1840. open.bits_per_sample = 16;
  1841. /* source endpoint : matrix */
  1842. open.postprocopo_id = get_asm_topology();
  1843. if (open.postprocopo_id == 0)
  1844. open.postprocopo_id = ASM_STREAM_POSTPROC_TOPO_ID_NONE;
  1845. switch (wr_format) {
  1846. case FORMAT_LINEAR_PCM:
  1847. open.dec_fmt_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V2;
  1848. break;
  1849. case FORMAT_MPEG4_AAC:
  1850. open.dec_fmt_id = ASM_MEDIA_FMT_AAC_V2;
  1851. break;
  1852. case FORMAT_MPEG4_MULTI_AAC:
  1853. open.dec_fmt_id = ASM_MEDIA_FMT_DOLBY_AAC;
  1854. break;
  1855. case FORMAT_WMA_V9:
  1856. open.dec_fmt_id = ASM_MEDIA_FMT_WMA_V9_V2;
  1857. break;
  1858. case FORMAT_WMA_V10PRO:
  1859. open.dec_fmt_id = ASM_MEDIA_FMT_WMA_V10PRO_V2;
  1860. break;
  1861. case FORMAT_AMRNB:
  1862. open.dec_fmt_id = ASM_MEDIA_FMT_AMRNB_FS;
  1863. break;
  1864. case FORMAT_AMRWB:
  1865. open.dec_fmt_id = ASM_MEDIA_FMT_AMRWB_FS;
  1866. break;
  1867. case FORMAT_AMR_WB_PLUS:
  1868. open.dec_fmt_id = ASM_MEDIA_FMT_AMR_WB_PLUS_V2;
  1869. break;
  1870. case FORMAT_V13K:
  1871. open.dec_fmt_id = ASM_MEDIA_FMT_V13K_FS;
  1872. break;
  1873. case FORMAT_EVRC:
  1874. open.dec_fmt_id = ASM_MEDIA_FMT_EVRC_FS;
  1875. break;
  1876. case FORMAT_EVRCB:
  1877. open.dec_fmt_id = ASM_MEDIA_FMT_EVRCB_FS;
  1878. break;
  1879. case FORMAT_EVRCWB:
  1880. open.dec_fmt_id = ASM_MEDIA_FMT_EVRCWB_FS;
  1881. break;
  1882. case FORMAT_MP3:
  1883. open.dec_fmt_id = ASM_MEDIA_FMT_MP3;
  1884. break;
  1885. default:
  1886. pr_err("Invalid format[%d]\n", wr_format);
  1887. goto fail_cmd;
  1888. }
  1889. switch (rd_format) {
  1890. case FORMAT_LINEAR_PCM:
  1891. open.enc_cfg_id = ASM_MEDIA_FMT_MULTI_CHANNEL_PCM_V2;
  1892. break;
  1893. case FORMAT_MPEG4_AAC:
  1894. open.enc_cfg_id = ASM_MEDIA_FMT_AAC_V2;
  1895. break;
  1896. case FORMAT_V13K:
  1897. open.enc_cfg_id = ASM_MEDIA_FMT_V13K_FS;
  1898. break;
  1899. case FORMAT_EVRC:
  1900. open.enc_cfg_id = ASM_MEDIA_FMT_EVRC_FS;
  1901. break;
  1902. case FORMAT_AMRNB:
  1903. open.enc_cfg_id = ASM_MEDIA_FMT_AMRNB_FS;
  1904. break;
  1905. case FORMAT_AMRWB:
  1906. open.enc_cfg_id = ASM_MEDIA_FMT_AMRWB_FS;
  1907. break;
  1908. default:
  1909. pr_err("Invalid format[%d]\n", rd_format);
  1910. goto fail_cmd;
  1911. }
  1912. pr_debug("%s:rdformat[0x%x]wrformat[0x%x]\n", __func__,
  1913. open.enc_cfg_id, open.dec_fmt_id);
  1914. rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
  1915. if (rc < 0) {
  1916. pr_err("open failed op[0x%x]rc[%d]\n", \
  1917. open.hdr.opcode, rc);
  1918. goto fail_cmd;
  1919. }
  1920. rc = wait_event_timeout(ac->cmd_wait,
  1921. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  1922. if (!rc) {
  1923. pr_err("timeout. waited for open read-write rc[%d]\n", rc);
  1924. goto fail_cmd;
  1925. }
  1926. return 0;
  1927. fail_cmd:
  1928. return -EINVAL;
  1929. }
  1930. int q6asm_open_loopback_v2(struct audio_client *ac, uint16_t bits_per_sample)
  1931. {
  1932. int rc = 0x00;
  1933. struct asm_stream_cmd_open_loopback_v2 open;
  1934. if ((ac == NULL) || (ac->apr == NULL)) {
  1935. pr_err("%s APR handle NULL\n", __func__);
  1936. return -EINVAL;
  1937. }
  1938. pr_debug("%s: session[%d]", __func__, ac->session);
  1939. q6asm_add_hdr(ac, &open.hdr, sizeof(open), TRUE);
  1940. atomic_set(&ac->cmd_state, 1);
  1941. open.hdr.opcode = ASM_STREAM_CMD_OPEN_LOOPBACK_V2;
  1942. open.mode_flags = 0;
  1943. open.src_endpointype = 0;
  1944. open.sink_endpointype = 0;
  1945. /* source endpoint : matrix */
  1946. open.postprocopo_id = get_asm_topology();
  1947. if (open.postprocopo_id == 0)
  1948. open.postprocopo_id = DEFAULT_POPP_TOPOLOGY;
  1949. open.bits_per_sample = bits_per_sample;
  1950. open.reserved = 0;
  1951. rc = apr_send_pkt(ac->apr, (uint32_t *) &open);
  1952. if (rc < 0) {
  1953. pr_err("%s open failed op[0x%x]rc[%d]\n", __func__,
  1954. open.hdr.opcode, rc);
  1955. goto fail_cmd;
  1956. }
  1957. rc = wait_event_timeout(ac->cmd_wait,
  1958. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  1959. if (!rc) {
  1960. pr_err("%s timeout. waited for open_loopback rc[%d]\n",
  1961. __func__, rc);
  1962. goto fail_cmd;
  1963. }
  1964. return 0;
  1965. fail_cmd:
  1966. return -EINVAL;
  1967. }
  1968. int q6asm_run(struct audio_client *ac, uint32_t flags,
  1969. uint32_t msw_ts, uint32_t lsw_ts)
  1970. {
  1971. struct asm_session_cmd_run_v2 run;
  1972. int rc;
  1973. if (!ac || ac->apr == NULL) {
  1974. pr_err("%s: APR handle NULL\n", __func__);
  1975. return -EINVAL;
  1976. }
  1977. pr_debug("%s session[%d]", __func__, ac->session);
  1978. q6asm_add_hdr(ac, &run.hdr, sizeof(run), TRUE);
  1979. atomic_set(&ac->cmd_state, 1);
  1980. run.hdr.opcode = ASM_SESSION_CMD_RUN_V2;
  1981. run.flags = flags;
  1982. run.time_lsw = lsw_ts;
  1983. run.time_msw = msw_ts;
  1984. config_debug_fs_run();
  1985. rc = apr_send_pkt(ac->apr, (uint32_t *) &run);
  1986. if (rc < 0) {
  1987. pr_err("Commmand run failed[%d]", rc);
  1988. goto fail_cmd;
  1989. }
  1990. rc = wait_event_timeout(ac->cmd_wait,
  1991. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  1992. if (!rc) {
  1993. pr_err("timeout. waited for run success rc[%d]", rc);
  1994. goto fail_cmd;
  1995. }
  1996. return 0;
  1997. fail_cmd:
  1998. return -EINVAL;
  1999. }
  2000. static int __q6asm_run_nowait(struct audio_client *ac, uint32_t flags,
  2001. uint32_t msw_ts, uint32_t lsw_ts, uint32_t stream_id)
  2002. {
  2003. struct asm_session_cmd_run_v2 run;
  2004. int rc;
  2005. if (!ac || ac->apr == NULL) {
  2006. pr_err("%s:APR handle NULL\n", __func__);
  2007. return -EINVAL;
  2008. }
  2009. pr_debug("session[%d]", ac->session);
  2010. q6asm_stream_add_hdr_async(ac, &run.hdr, sizeof(run), TRUE, stream_id);
  2011. atomic_set(&ac->cmd_state, 1);
  2012. run.hdr.opcode = ASM_SESSION_CMD_RUN_V2;
  2013. run.flags = flags;
  2014. run.time_lsw = lsw_ts;
  2015. run.time_msw = msw_ts;
  2016. /* have to increase first avoid race */
  2017. atomic_inc(&ac->nowait_cmd_cnt);
  2018. rc = apr_send_pkt(ac->apr, (uint32_t *) &run);
  2019. if (rc < 0) {
  2020. atomic_dec(&ac->nowait_cmd_cnt);
  2021. pr_err("%s:Commmand run failed[%d]", __func__, rc);
  2022. return -EINVAL;
  2023. }
  2024. return 0;
  2025. }
  2026. int q6asm_run_nowait(struct audio_client *ac, uint32_t flags,
  2027. uint32_t msw_ts, uint32_t lsw_ts)
  2028. {
  2029. return __q6asm_run_nowait(ac, flags, msw_ts, lsw_ts, ac->stream_id);
  2030. }
  2031. int q6asm_stream_run_nowait(struct audio_client *ac, uint32_t flags,
  2032. uint32_t msw_ts, uint32_t lsw_ts, uint32_t stream_id)
  2033. {
  2034. return __q6asm_run_nowait(ac, flags, msw_ts, lsw_ts, stream_id);
  2035. }
  2036. int q6asm_enc_cfg_blk_aac(struct audio_client *ac,
  2037. uint32_t frames_per_buf,
  2038. uint32_t sample_rate, uint32_t channels,
  2039. uint32_t bit_rate, uint32_t mode, uint32_t format)
  2040. {
  2041. struct asm_aac_enc_cfg_v2 enc_cfg;
  2042. int rc = 0;
  2043. pr_debug("%s:session[%d]frames[%d]SR[%d]ch[%d]bitrate[%d]mode[%d] format[%d]",
  2044. __func__, ac->session, frames_per_buf,
  2045. sample_rate, channels, bit_rate, mode, format);
  2046. q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
  2047. atomic_set(&ac->cmd_state, 1);
  2048. enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  2049. enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
  2050. enc_cfg.encdec.param_size = sizeof(struct asm_aac_enc_cfg_v2) -
  2051. sizeof(struct asm_stream_cmd_set_encdec_param);
  2052. enc_cfg.encblk.frames_per_buf = frames_per_buf;
  2053. enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
  2054. sizeof(struct asm_enc_cfg_blk_param_v2);
  2055. enc_cfg.bit_rate = bit_rate;
  2056. enc_cfg.enc_mode = mode;
  2057. enc_cfg.aac_fmt_flag = format;
  2058. enc_cfg.channel_cfg = channels;
  2059. enc_cfg.sample_rate = sample_rate;
  2060. rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
  2061. if (rc < 0) {
  2062. pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
  2063. rc = -EINVAL;
  2064. goto fail_cmd;
  2065. }
  2066. rc = wait_event_timeout(ac->cmd_wait,
  2067. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  2068. if (!rc) {
  2069. pr_err("timeout. waited for FORMAT_UPDATE\n");
  2070. goto fail_cmd;
  2071. }
  2072. return 0;
  2073. fail_cmd:
  2074. return -EINVAL;
  2075. }
  2076. int q6asm_set_encdec_chan_map(struct audio_client *ac,
  2077. uint32_t num_channels)
  2078. {
  2079. struct asm_dec_out_chan_map_param chan_map;
  2080. u8 *channel_mapping;
  2081. int rc = 0;
  2082. pr_debug("%s: Session %d, num_channels = %d\n",
  2083. __func__, ac->session, num_channels);
  2084. if (num_channels > MAX_CHAN_MAP_CHANNELS) {
  2085. pr_err("%s: Invalid channel count %d\n", __func__,
  2086. num_channels);
  2087. return -EINVAL;
  2088. }
  2089. q6asm_add_hdr(ac, &chan_map.hdr, sizeof(chan_map), TRUE);
  2090. atomic_set(&ac->cmd_state, 1);
  2091. chan_map.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  2092. chan_map.encdec.param_id = ASM_PARAM_ID_DEC_OUTPUT_CHAN_MAP;
  2093. chan_map.encdec.param_size = sizeof(struct asm_dec_out_chan_map_param) -
  2094. (sizeof(struct apr_hdr) +
  2095. sizeof(struct asm_stream_cmd_set_encdec_param));
  2096. chan_map.num_channels = num_channels;
  2097. channel_mapping = chan_map.channel_mapping;
  2098. memset(channel_mapping, PCM_CHANNEL_NULL, MAX_CHAN_MAP_CHANNELS);
  2099. if (q6asm_map_channels(channel_mapping, num_channels))
  2100. return -EINVAL;
  2101. rc = apr_send_pkt(ac->apr, (uint32_t *) &chan_map);
  2102. if (rc < 0) {
  2103. pr_err("%s:Command opcode[0x%x]paramid[0x%x] failed\n",
  2104. __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM,
  2105. ASM_PARAM_ID_DEC_OUTPUT_CHAN_MAP);
  2106. goto fail_cmd;
  2107. }
  2108. rc = wait_event_timeout(ac->cmd_wait,
  2109. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  2110. if (!rc) {
  2111. pr_err("%s:timeout opcode[0x%x]\n", __func__,
  2112. chan_map.hdr.opcode);
  2113. rc = -ETIMEDOUT;
  2114. goto fail_cmd;
  2115. }
  2116. return 0;
  2117. fail_cmd:
  2118. return rc;
  2119. }
  2120. static int __q6asm_enc_cfg_blk_pcm(struct audio_client *ac,
  2121. uint32_t rate, uint32_t channels, uint16_t bits_per_sample)
  2122. {
  2123. struct asm_multi_channel_pcm_enc_cfg_v2 enc_cfg;
  2124. u8 *channel_mapping;
  2125. u32 frames_per_buf = 0;
  2126. int rc = 0;
  2127. if (channels > PCM_FORMAT_MAX_NUM_CHANNEL) {
  2128. pr_err("%s: Invalid channel count %d\n", __func__, channels);
  2129. return -EINVAL;
  2130. }
  2131. pr_debug("%s: Session %d, rate = %d, channels = %d\n", __func__,
  2132. ac->session, rate, channels);
  2133. q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
  2134. atomic_set(&ac->cmd_state, 1);
  2135. enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  2136. enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
  2137. enc_cfg.encdec.param_size = sizeof(enc_cfg) - sizeof(enc_cfg.hdr) -
  2138. sizeof(enc_cfg.encdec);
  2139. enc_cfg.encblk.frames_per_buf = frames_per_buf;
  2140. enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
  2141. sizeof(struct asm_enc_cfg_blk_param_v2);
  2142. enc_cfg.num_channels = channels;
  2143. enc_cfg.bits_per_sample = bits_per_sample;
  2144. enc_cfg.sample_rate = rate;
  2145. enc_cfg.is_signed = 1;
  2146. channel_mapping = enc_cfg.channel_mapping;
  2147. memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  2148. if (q6asm_map_channels(channel_mapping, channels))
  2149. return -EINVAL;
  2150. rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
  2151. if (rc < 0) {
  2152. pr_err("Comamnd open failed\n");
  2153. rc = -EINVAL;
  2154. goto fail_cmd;
  2155. }
  2156. rc = wait_event_timeout(ac->cmd_wait,
  2157. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  2158. if (!rc) {
  2159. pr_err("timeout opcode[0x%x] ", enc_cfg.hdr.opcode);
  2160. goto fail_cmd;
  2161. }
  2162. return 0;
  2163. fail_cmd:
  2164. return -EINVAL;
  2165. }
  2166. int q6asm_enc_cfg_blk_pcm(struct audio_client *ac,
  2167. uint32_t rate, uint32_t channels)
  2168. {
  2169. return __q6asm_enc_cfg_blk_pcm(ac, rate, channels, 16);
  2170. }
  2171. int q6asm_enc_cfg_blk_pcm_format_support(struct audio_client *ac,
  2172. uint32_t rate, uint32_t channels, uint16_t bits_per_sample)
  2173. {
  2174. return __q6asm_enc_cfg_blk_pcm(ac, rate, channels, bits_per_sample);
  2175. }
  2176. int q6asm_enc_cfg_blk_pcm_native(struct audio_client *ac,
  2177. uint32_t rate, uint32_t channels)
  2178. {
  2179. struct asm_multi_channel_pcm_enc_cfg_v2 enc_cfg;
  2180. u8 *channel_mapping;
  2181. u32 frames_per_buf = 0;
  2182. int rc = 0;
  2183. if (channels > PCM_FORMAT_MAX_NUM_CHANNEL) {
  2184. pr_err("%s: Invalid channel count %d\n", __func__, channels);
  2185. return -EINVAL;
  2186. }
  2187. pr_debug("%s: Session %d, rate = %d, channels = %d\n", __func__,
  2188. ac->session, rate, channels);
  2189. q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
  2190. atomic_set(&ac->cmd_state, 1);
  2191. enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  2192. enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
  2193. enc_cfg.encdec.param_size = sizeof(enc_cfg) - sizeof(enc_cfg.hdr) -
  2194. sizeof(enc_cfg.encdec);
  2195. enc_cfg.encblk.frames_per_buf = frames_per_buf;
  2196. enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
  2197. sizeof(struct asm_enc_cfg_blk_param_v2);
  2198. enc_cfg.num_channels = 0;/*channels;*/
  2199. enc_cfg.bits_per_sample = 16;
  2200. enc_cfg.sample_rate = 0;/*rate;*/
  2201. enc_cfg.is_signed = 1;
  2202. channel_mapping = enc_cfg.channel_mapping;
  2203. memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  2204. if (q6asm_map_channels(channel_mapping, channels))
  2205. return -EINVAL;
  2206. rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
  2207. if (rc < 0) {
  2208. pr_err("Comamnd open failed\n");
  2209. rc = -EINVAL;
  2210. goto fail_cmd;
  2211. }
  2212. rc = wait_event_timeout(ac->cmd_wait,
  2213. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  2214. if (!rc) {
  2215. pr_err("timeout opcode[0x%x] ", enc_cfg.hdr.opcode);
  2216. goto fail_cmd;
  2217. }
  2218. return 0;
  2219. fail_cmd:
  2220. return -EINVAL;
  2221. }
  2222. static int q6asm_map_channels(u8 *channel_mapping, uint32_t channels)
  2223. {
  2224. u8 *lchannel_mapping;
  2225. lchannel_mapping = channel_mapping;
  2226. pr_debug("%s channels passed: %d\n", __func__, channels);
  2227. if (channels == 1) {
  2228. lchannel_mapping[0] = PCM_CHANNEL_FC;
  2229. } else if (channels == 2) {
  2230. lchannel_mapping[0] = PCM_CHANNEL_FL;
  2231. lchannel_mapping[1] = PCM_CHANNEL_FR;
  2232. } else if (channels == 3) {
  2233. lchannel_mapping[0] = PCM_CHANNEL_FL;
  2234. lchannel_mapping[1] = PCM_CHANNEL_FR;
  2235. lchannel_mapping[2] = PCM_CHANNEL_FC;
  2236. } else if (channels == 4) {
  2237. lchannel_mapping[0] = PCM_CHANNEL_FL;
  2238. lchannel_mapping[1] = PCM_CHANNEL_FR;
  2239. lchannel_mapping[2] = PCM_CHANNEL_RB;
  2240. lchannel_mapping[3] = PCM_CHANNEL_LB;
  2241. } else if (channels == 5) {
  2242. lchannel_mapping[0] = PCM_CHANNEL_FL;
  2243. lchannel_mapping[1] = PCM_CHANNEL_FR;
  2244. lchannel_mapping[2] = PCM_CHANNEL_FC;
  2245. lchannel_mapping[3] = PCM_CHANNEL_LB;
  2246. lchannel_mapping[4] = PCM_CHANNEL_RB;
  2247. } else if (channels == 6) {
  2248. lchannel_mapping[0] = PCM_CHANNEL_FC;
  2249. lchannel_mapping[1] = PCM_CHANNEL_FL;
  2250. lchannel_mapping[2] = PCM_CHANNEL_FR;
  2251. lchannel_mapping[3] = PCM_CHANNEL_LB;
  2252. lchannel_mapping[4] = PCM_CHANNEL_RB;
  2253. lchannel_mapping[5] = PCM_CHANNEL_LFE;
  2254. } else if (channels == 8) {
  2255. lchannel_mapping[0] = PCM_CHANNEL_FL;
  2256. lchannel_mapping[1] = PCM_CHANNEL_FR;
  2257. lchannel_mapping[2] = PCM_CHANNEL_LFE;
  2258. lchannel_mapping[3] = PCM_CHANNEL_FC;
  2259. lchannel_mapping[4] = PCM_CHANNEL_LB;
  2260. lchannel_mapping[5] = PCM_CHANNEL_RB;
  2261. lchannel_mapping[6] = PCM_CHANNEL_RLC;
  2262. lchannel_mapping[7] = PCM_CHANNEL_RRC;
  2263. } else {
  2264. pr_err("%s: ERROR.unsupported num_ch = %u\n",
  2265. __func__, channels);
  2266. return -EINVAL;
  2267. }
  2268. return 0;
  2269. }
  2270. int q6asm_enable_sbrps(struct audio_client *ac,
  2271. uint32_t sbr_ps_enable)
  2272. {
  2273. struct asm_aac_sbr_ps_flag_param sbrps;
  2274. u32 frames_per_buf = 0;
  2275. int rc = 0;
  2276. pr_debug("%s: Session %d\n", __func__, ac->session);
  2277. q6asm_add_hdr(ac, &sbrps.hdr, sizeof(sbrps), TRUE);
  2278. atomic_set(&ac->cmd_state, 1);
  2279. sbrps.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  2280. sbrps.encdec.param_id = ASM_PARAM_ID_AAC_SBR_PS_FLAG;
  2281. sbrps.encdec.param_size = sizeof(struct asm_aac_sbr_ps_flag_param) -
  2282. sizeof(struct asm_stream_cmd_set_encdec_param);
  2283. sbrps.encblk.frames_per_buf = frames_per_buf;
  2284. sbrps.encblk.enc_cfg_blk_size = sbrps.encdec.param_size -
  2285. sizeof(struct asm_enc_cfg_blk_param_v2);
  2286. sbrps.sbr_ps_flag = sbr_ps_enable;
  2287. rc = apr_send_pkt(ac->apr, (uint32_t *) &sbrps);
  2288. if (rc < 0) {
  2289. pr_err("Command opcode[0x%x]paramid[0x%x] failed\n",
  2290. ASM_STREAM_CMD_SET_ENCDEC_PARAM,
  2291. ASM_PARAM_ID_AAC_SBR_PS_FLAG);
  2292. rc = -EINVAL;
  2293. goto fail_cmd;
  2294. }
  2295. rc = wait_event_timeout(ac->cmd_wait,
  2296. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  2297. if (!rc) {
  2298. pr_err("timeout opcode[0x%x] ", sbrps.hdr.opcode);
  2299. goto fail_cmd;
  2300. }
  2301. return 0;
  2302. fail_cmd:
  2303. return -EINVAL;
  2304. }
  2305. int q6asm_cfg_dual_mono_aac(struct audio_client *ac,
  2306. uint16_t sce_left, uint16_t sce_right)
  2307. {
  2308. struct asm_aac_dual_mono_mapping_param dual_mono;
  2309. int rc = 0;
  2310. pr_debug("%s: Session %d, sce_left = %d, sce_right = %d\n",
  2311. __func__, ac->session, sce_left, sce_right);
  2312. q6asm_add_hdr(ac, &dual_mono.hdr, sizeof(dual_mono), TRUE);
  2313. atomic_set(&ac->cmd_state, 1);
  2314. dual_mono.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  2315. dual_mono.encdec.param_id = ASM_PARAM_ID_AAC_DUAL_MONO_MAPPING;
  2316. dual_mono.encdec.param_size = sizeof(dual_mono.left_channel_sce) +
  2317. sizeof(dual_mono.right_channel_sce);
  2318. dual_mono.left_channel_sce = sce_left;
  2319. dual_mono.right_channel_sce = sce_right;
  2320. rc = apr_send_pkt(ac->apr, (uint32_t *) &dual_mono);
  2321. if (rc < 0) {
  2322. pr_err("%s:Command opcode[0x%x]paramid[0x%x] failed\n",
  2323. __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM,
  2324. ASM_PARAM_ID_AAC_DUAL_MONO_MAPPING);
  2325. rc = -EINVAL;
  2326. goto fail_cmd;
  2327. }
  2328. rc = wait_event_timeout(ac->cmd_wait,
  2329. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  2330. if (!rc) {
  2331. pr_err("%s:timeout opcode[0x%x]\n", __func__,
  2332. dual_mono.hdr.opcode);
  2333. goto fail_cmd;
  2334. }
  2335. return 0;
  2336. fail_cmd:
  2337. return -EINVAL;
  2338. }
  2339. /* Support for selecting stereo mixing coefficients for B family not done */
  2340. int q6asm_cfg_aac_sel_mix_coef(struct audio_client *ac, uint32_t mix_coeff)
  2341. {
  2342. struct asm_aac_stereo_mix_coeff_selection_param_v2 aac_mix_coeff;
  2343. int rc = 0;
  2344. q6asm_add_hdr(ac, &aac_mix_coeff.hdr, sizeof(aac_mix_coeff), TRUE);
  2345. atomic_set(&ac->cmd_state, 1);
  2346. aac_mix_coeff.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  2347. aac_mix_coeff.param_id =
  2348. ASM_PARAM_ID_AAC_STEREO_MIX_COEFF_SELECTION_FLAG_V2;
  2349. aac_mix_coeff.param_size =
  2350. sizeof(struct asm_aac_stereo_mix_coeff_selection_param_v2);
  2351. aac_mix_coeff.aac_stereo_mix_coeff_flag = mix_coeff;
  2352. pr_debug("%s, mix_coeff = %u", __func__, mix_coeff);
  2353. rc = apr_send_pkt(ac->apr, (uint32_t *) &aac_mix_coeff);
  2354. if (rc < 0) {
  2355. pr_err("%s:Command opcode[0x%x]paramid[0x%x] failed\n",
  2356. __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM,
  2357. ASM_PARAM_ID_AAC_STEREO_MIX_COEFF_SELECTION_FLAG_V2);
  2358. goto fail_cmd;
  2359. }
  2360. rc = wait_event_timeout(ac->cmd_wait,
  2361. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  2362. if (!rc) {
  2363. pr_err("%s:timeout opcode[0x%x]\n",
  2364. __func__, aac_mix_coeff.hdr.opcode);
  2365. rc = -ETIMEDOUT;
  2366. goto fail_cmd;
  2367. }
  2368. return 0;
  2369. fail_cmd:
  2370. return rc;
  2371. }
  2372. int q6asm_enc_cfg_blk_qcelp(struct audio_client *ac, uint32_t frames_per_buf,
  2373. uint16_t min_rate, uint16_t max_rate,
  2374. uint16_t reduced_rate_level, uint16_t rate_modulation_cmd)
  2375. {
  2376. struct asm_v13k_enc_cfg enc_cfg;
  2377. int rc = 0;
  2378. pr_debug("%s:session[%d]frames[%d]min_rate[0x%4x]max_rate[0x%4x] reduced_rate_level[0x%4x]rate_modulation_cmd[0x%4x]",
  2379. __func__,
  2380. ac->session, frames_per_buf, min_rate, max_rate,
  2381. reduced_rate_level, rate_modulation_cmd);
  2382. q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
  2383. atomic_set(&ac->cmd_state, 1);
  2384. enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  2385. enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
  2386. enc_cfg.encdec.param_size = sizeof(struct asm_v13k_enc_cfg) -
  2387. sizeof(struct asm_stream_cmd_set_encdec_param);
  2388. enc_cfg.encblk.frames_per_buf = frames_per_buf;
  2389. enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
  2390. sizeof(struct asm_enc_cfg_blk_param_v2);
  2391. enc_cfg.min_rate = min_rate;
  2392. enc_cfg.max_rate = max_rate;
  2393. enc_cfg.reduced_rate_cmd = reduced_rate_level;
  2394. enc_cfg.rate_mod_cmd = rate_modulation_cmd;
  2395. rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
  2396. if (rc < 0) {
  2397. pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
  2398. goto fail_cmd;
  2399. }
  2400. rc = wait_event_timeout(ac->cmd_wait,
  2401. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  2402. if (!rc) {
  2403. pr_err("timeout. waited for setencdec v13k resp\n");
  2404. goto fail_cmd;
  2405. }
  2406. return 0;
  2407. fail_cmd:
  2408. return -EINVAL;
  2409. }
  2410. int q6asm_enc_cfg_blk_evrc(struct audio_client *ac, uint32_t frames_per_buf,
  2411. uint16_t min_rate, uint16_t max_rate,
  2412. uint16_t rate_modulation_cmd)
  2413. {
  2414. struct asm_evrc_enc_cfg enc_cfg;
  2415. int rc = 0;
  2416. pr_debug("%s:session[%d]frames[%d]min_rate[0x%4x]max_rate[0x%4x] rate_modulation_cmd[0x%4x]",
  2417. __func__, ac->session,
  2418. frames_per_buf, min_rate, max_rate, rate_modulation_cmd);
  2419. q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
  2420. atomic_set(&ac->cmd_state, 1);
  2421. enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  2422. enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
  2423. enc_cfg.encdec.param_size = sizeof(struct asm_evrc_enc_cfg) -
  2424. sizeof(struct asm_stream_cmd_set_encdec_param);
  2425. enc_cfg.encblk.frames_per_buf = frames_per_buf;
  2426. enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
  2427. sizeof(struct asm_enc_cfg_blk_param_v2);
  2428. enc_cfg.min_rate = min_rate;
  2429. enc_cfg.max_rate = max_rate;
  2430. enc_cfg.rate_mod_cmd = rate_modulation_cmd;
  2431. enc_cfg.reserved = 0;
  2432. rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
  2433. if (rc < 0) {
  2434. pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
  2435. goto fail_cmd;
  2436. }
  2437. rc = wait_event_timeout(ac->cmd_wait,
  2438. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  2439. if (!rc) {
  2440. pr_err("timeout. waited for encdec evrc\n");
  2441. goto fail_cmd;
  2442. }
  2443. return 0;
  2444. fail_cmd:
  2445. return -EINVAL;
  2446. }
  2447. int q6asm_enc_cfg_blk_amrnb(struct audio_client *ac, uint32_t frames_per_buf,
  2448. uint16_t band_mode, uint16_t dtx_enable)
  2449. {
  2450. struct asm_amrnb_enc_cfg enc_cfg;
  2451. int rc = 0;
  2452. pr_debug("%s:session[%d]frames[%d]band_mode[0x%4x]dtx_enable[0x%4x]",
  2453. __func__, ac->session, frames_per_buf, band_mode, dtx_enable);
  2454. q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
  2455. atomic_set(&ac->cmd_state, 1);
  2456. enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  2457. enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
  2458. enc_cfg.encdec.param_size = sizeof(struct asm_amrnb_enc_cfg) -
  2459. sizeof(struct asm_stream_cmd_set_encdec_param);
  2460. enc_cfg.encblk.frames_per_buf = frames_per_buf;
  2461. enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
  2462. sizeof(struct asm_enc_cfg_blk_param_v2);
  2463. enc_cfg.enc_mode = band_mode;
  2464. enc_cfg.dtx_mode = dtx_enable;
  2465. rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
  2466. if (rc < 0) {
  2467. pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
  2468. goto fail_cmd;
  2469. }
  2470. rc = wait_event_timeout(ac->cmd_wait,
  2471. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  2472. if (!rc) {
  2473. pr_err("timeout. waited for set encdec amrnb\n");
  2474. goto fail_cmd;
  2475. }
  2476. return 0;
  2477. fail_cmd:
  2478. return -EINVAL;
  2479. }
  2480. int q6asm_enc_cfg_blk_amrwb(struct audio_client *ac, uint32_t frames_per_buf,
  2481. uint16_t band_mode, uint16_t dtx_enable)
  2482. {
  2483. struct asm_amrwb_enc_cfg enc_cfg;
  2484. int rc = 0;
  2485. pr_debug("%s:session[%d]frames[%d]band_mode[0x%4x]dtx_enable[0x%4x]",
  2486. __func__, ac->session, frames_per_buf, band_mode, dtx_enable);
  2487. q6asm_add_hdr(ac, &enc_cfg.hdr, sizeof(enc_cfg), TRUE);
  2488. atomic_set(&ac->cmd_state, 1);
  2489. enc_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  2490. enc_cfg.encdec.param_id = ASM_PARAM_ID_ENCDEC_ENC_CFG_BLK_V2;
  2491. enc_cfg.encdec.param_size = sizeof(struct asm_amrwb_enc_cfg) -
  2492. sizeof(struct asm_stream_cmd_set_encdec_param);
  2493. enc_cfg.encblk.frames_per_buf = frames_per_buf;
  2494. enc_cfg.encblk.enc_cfg_blk_size = enc_cfg.encdec.param_size -
  2495. sizeof(struct asm_enc_cfg_blk_param_v2);
  2496. enc_cfg.enc_mode = band_mode;
  2497. enc_cfg.dtx_mode = dtx_enable;
  2498. rc = apr_send_pkt(ac->apr, (uint32_t *) &enc_cfg);
  2499. if (rc < 0) {
  2500. pr_err("Comamnd %d failed\n", ASM_STREAM_CMD_SET_ENCDEC_PARAM);
  2501. goto fail_cmd;
  2502. }
  2503. rc = wait_event_timeout(ac->cmd_wait,
  2504. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  2505. if (!rc) {
  2506. pr_err("timeout. waited for FORMAT_UPDATE\n");
  2507. goto fail_cmd;
  2508. }
  2509. return 0;
  2510. fail_cmd:
  2511. return -EINVAL;
  2512. }
  2513. static int __q6asm_media_format_block_pcm(struct audio_client *ac,
  2514. uint32_t rate, uint32_t channels,
  2515. uint16_t bits_per_sample)
  2516. {
  2517. struct asm_multi_channel_pcm_fmt_blk_v2 fmt;
  2518. u8 *channel_mapping;
  2519. int rc = 0;
  2520. if (channels > PCM_FORMAT_MAX_NUM_CHANNEL) {
  2521. pr_err("%s: Invalid channel count %d\n", __func__, channels);
  2522. return -EINVAL;
  2523. }
  2524. pr_debug("%s:session[%d]rate[%d]ch[%d]\n", __func__, ac->session, rate,
  2525. channels);
  2526. q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
  2527. atomic_set(&ac->cmd_state, 1);
  2528. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  2529. fmt.fmt_blk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  2530. sizeof(fmt.fmt_blk);
  2531. fmt.num_channels = channels;
  2532. fmt.bits_per_sample = bits_per_sample;
  2533. fmt.sample_rate = rate;
  2534. fmt.is_signed = 1;
  2535. channel_mapping = fmt.channel_mapping;
  2536. memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  2537. if (q6asm_map_channels(channel_mapping, channels))
  2538. return -EINVAL;
  2539. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  2540. if (rc < 0) {
  2541. pr_err("%s:Comamnd open failed\n", __func__);
  2542. goto fail_cmd;
  2543. }
  2544. rc = wait_event_timeout(ac->cmd_wait,
  2545. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  2546. if (!rc) {
  2547. pr_err("%s:timeout. waited for format update\n", __func__);
  2548. goto fail_cmd;
  2549. }
  2550. return 0;
  2551. fail_cmd:
  2552. return -EINVAL;
  2553. }
  2554. int q6asm_media_format_block_pcm(struct audio_client *ac,
  2555. uint32_t rate, uint32_t channels)
  2556. {
  2557. return __q6asm_media_format_block_pcm(ac, rate,
  2558. channels, 16);
  2559. }
  2560. int q6asm_media_format_block_pcm_format_support(struct audio_client *ac,
  2561. uint32_t rate, uint32_t channels,
  2562. uint16_t bits_per_sample)
  2563. {
  2564. return __q6asm_media_format_block_pcm(ac, rate,
  2565. channels, bits_per_sample);
  2566. }
  2567. static int __q6asm_media_format_block_multi_ch_pcm(struct audio_client *ac,
  2568. uint32_t rate, uint32_t channels,
  2569. bool use_default_chmap, char *channel_map,
  2570. uint16_t bits_per_sample)
  2571. {
  2572. struct asm_multi_channel_pcm_fmt_blk_v2 fmt;
  2573. u8 *channel_mapping;
  2574. int rc = 0;
  2575. if (channels > PCM_FORMAT_MAX_NUM_CHANNEL) {
  2576. pr_err("%s: Invalid channel count %d\n", __func__, channels);
  2577. return -EINVAL;
  2578. }
  2579. pr_debug("%s:session[%d]rate[%d]ch[%d]\n", __func__, ac->session, rate,
  2580. channels);
  2581. q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
  2582. atomic_set(&ac->cmd_state, 1);
  2583. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  2584. fmt.fmt_blk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  2585. sizeof(fmt.fmt_blk);
  2586. fmt.num_channels = channels;
  2587. fmt.bits_per_sample = bits_per_sample;
  2588. fmt.sample_rate = rate;
  2589. fmt.is_signed = 1;
  2590. channel_mapping = fmt.channel_mapping;
  2591. memset(channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  2592. if (use_default_chmap) {
  2593. if (q6asm_map_channels(channel_mapping, channels)) {
  2594. pr_err("%s: map channels failed", __func__);
  2595. return -EINVAL;
  2596. }
  2597. } else {
  2598. memcpy(channel_mapping, channel_map,
  2599. PCM_FORMAT_MAX_NUM_CHANNEL);
  2600. }
  2601. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  2602. if (rc < 0) {
  2603. pr_err("%s:Comamnd open failed\n", __func__);
  2604. goto fail_cmd;
  2605. }
  2606. rc = wait_event_timeout(ac->cmd_wait,
  2607. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  2608. if (!rc) {
  2609. pr_err("%s:timeout. waited for format update\n", __func__);
  2610. goto fail_cmd;
  2611. }
  2612. return 0;
  2613. fail_cmd:
  2614. return -EINVAL;
  2615. }
  2616. int q6asm_media_format_block_multi_ch_pcm(struct audio_client *ac,
  2617. uint32_t rate, uint32_t channels,
  2618. bool use_default_chmap, char *channel_map)
  2619. {
  2620. return __q6asm_media_format_block_multi_ch_pcm(ac, rate,
  2621. channels, use_default_chmap, channel_map, 16);
  2622. }
  2623. int q6asm_media_format_block_multi_ch_pcm_v2(
  2624. struct audio_client *ac,
  2625. uint32_t rate, uint32_t channels,
  2626. bool use_default_chmap, char *channel_map,
  2627. uint16_t bits_per_sample)
  2628. {
  2629. return __q6asm_media_format_block_multi_ch_pcm(ac, rate,
  2630. channels, use_default_chmap, channel_map,
  2631. bits_per_sample);
  2632. }
  2633. static int __q6asm_media_format_block_multi_aac(struct audio_client *ac,
  2634. struct asm_aac_cfg *cfg, int stream_id)
  2635. {
  2636. struct asm_aac_fmt_blk_v2 fmt;
  2637. int rc = 0;
  2638. pr_debug("%s:session[%d]rate[%d]ch[%d]\n", __func__, ac->session,
  2639. cfg->sample_rate, cfg->ch_cfg);
  2640. q6asm_stream_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE, stream_id);
  2641. atomic_set(&ac->cmd_state, 1);
  2642. /*
  2643. * Updated the token field with stream/session for compressed playback
  2644. * Platform driver must know the the stream with which the command is
  2645. * associated
  2646. */
  2647. if (ac->io_mode & COMPRESSED_STREAM_IO)
  2648. fmt.hdr.token = ((ac->session << 8) & 0xFFFF00) |
  2649. (stream_id & 0xFF);
  2650. pr_debug("%s: token = 0x%x, stream_id %d, session 0x%x\n",
  2651. __func__, fmt.hdr.token, stream_id, ac->session);
  2652. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  2653. fmt.fmt_blk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  2654. sizeof(fmt.fmt_blk);
  2655. fmt.aac_fmt_flag = cfg->format;
  2656. fmt.audio_objype = cfg->aot;
  2657. /* If zero, PCE is assumed to be available in bitstream*/
  2658. fmt.total_size_of_PCE_bits = 0;
  2659. fmt.channel_config = cfg->ch_cfg;
  2660. fmt.sample_rate = cfg->sample_rate;
  2661. pr_info("%s:format=%x cfg_size=%d aac-cfg=%x aot=%d ch=%d sr=%d\n",
  2662. __func__, fmt.aac_fmt_flag, fmt.fmt_blk.fmt_blk_size,
  2663. fmt.aac_fmt_flag,
  2664. fmt.audio_objype,
  2665. fmt.channel_config,
  2666. fmt.sample_rate);
  2667. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  2668. if (rc < 0) {
  2669. pr_err("%s:Comamnd open failed\n", __func__);
  2670. goto fail_cmd;
  2671. }
  2672. rc = wait_event_timeout(ac->cmd_wait,
  2673. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  2674. if (!rc) {
  2675. pr_err("%s:timeout. waited for FORMAT_UPDATE\n", __func__);
  2676. goto fail_cmd;
  2677. }
  2678. return 0;
  2679. fail_cmd:
  2680. return -EINVAL;
  2681. }
  2682. int q6asm_media_format_block_multi_aac(struct audio_client *ac,
  2683. struct asm_aac_cfg *cfg)
  2684. {
  2685. return __q6asm_media_format_block_multi_aac(ac, cfg, ac->stream_id);
  2686. }
  2687. int q6asm_media_format_block_aac(struct audio_client *ac,
  2688. struct asm_aac_cfg *cfg)
  2689. {
  2690. return __q6asm_media_format_block_multi_aac(ac, cfg, ac->stream_id);
  2691. }
  2692. int q6asm_stream_media_format_block_aac(struct audio_client *ac,
  2693. struct asm_aac_cfg *cfg, int stream_id)
  2694. {
  2695. return __q6asm_media_format_block_multi_aac(ac, cfg, stream_id);
  2696. }
  2697. int q6asm_media_format_block_wma(struct audio_client *ac,
  2698. void *cfg)
  2699. {
  2700. struct asm_wmastdv9_fmt_blk_v2 fmt;
  2701. struct asm_wma_cfg *wma_cfg = (struct asm_wma_cfg *)cfg;
  2702. int rc = 0;
  2703. pr_debug("session[%d]format_tag[0x%4x] rate[%d] ch[0x%4x] bps[%d], balign[0x%4x], bit_sample[0x%4x], ch_msk[%d], enc_opt[0x%4x]\n",
  2704. ac->session, wma_cfg->format_tag, wma_cfg->sample_rate,
  2705. wma_cfg->ch_cfg, wma_cfg->avg_bytes_per_sec,
  2706. wma_cfg->block_align, wma_cfg->valid_bits_per_sample,
  2707. wma_cfg->ch_mask, wma_cfg->encode_opt);
  2708. q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
  2709. atomic_set(&ac->cmd_state, 1);
  2710. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  2711. fmt.fmtblk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  2712. sizeof(fmt.fmtblk);
  2713. fmt.fmtag = wma_cfg->format_tag;
  2714. fmt.num_channels = wma_cfg->ch_cfg;
  2715. fmt.sample_rate = wma_cfg->sample_rate;
  2716. fmt.avg_bytes_per_sec = wma_cfg->avg_bytes_per_sec;
  2717. fmt.blk_align = wma_cfg->block_align;
  2718. fmt.bits_per_sample =
  2719. wma_cfg->valid_bits_per_sample;
  2720. fmt.channel_mask = wma_cfg->ch_mask;
  2721. fmt.enc_options = wma_cfg->encode_opt;
  2722. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  2723. if (rc < 0) {
  2724. pr_err("%s:Comamnd open failed\n", __func__);
  2725. goto fail_cmd;
  2726. }
  2727. rc = wait_event_timeout(ac->cmd_wait,
  2728. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  2729. if (!rc) {
  2730. pr_err("%s:timeout. waited for FORMAT_UPDATE\n", __func__);
  2731. goto fail_cmd;
  2732. }
  2733. return 0;
  2734. fail_cmd:
  2735. return -EINVAL;
  2736. }
  2737. int q6asm_media_format_block_wmapro(struct audio_client *ac,
  2738. void *cfg)
  2739. {
  2740. struct asm_wmaprov10_fmt_blk_v2 fmt;
  2741. struct asm_wmapro_cfg *wmapro_cfg = (struct asm_wmapro_cfg *)cfg;
  2742. int rc = 0;
  2743. pr_debug("session[%d]format_tag[0x%4x] rate[%d] ch[0x%4x] bps[%d], balign[0x%4x], bit_sample[0x%4x], ch_msk[%d], enc_opt[0x%4x], adv_enc_opt[0x%4x], adv_enc_opt2[0x%8x]\n",
  2744. ac->session, wmapro_cfg->format_tag, wmapro_cfg->sample_rate,
  2745. wmapro_cfg->ch_cfg, wmapro_cfg->avg_bytes_per_sec,
  2746. wmapro_cfg->block_align, wmapro_cfg->valid_bits_per_sample,
  2747. wmapro_cfg->ch_mask, wmapro_cfg->encode_opt,
  2748. wmapro_cfg->adv_encode_opt, wmapro_cfg->adv_encode_opt2);
  2749. q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
  2750. atomic_set(&ac->cmd_state, 1);
  2751. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  2752. fmt.fmtblk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  2753. sizeof(fmt.fmtblk);
  2754. fmt.fmtag = wmapro_cfg->format_tag;
  2755. fmt.num_channels = wmapro_cfg->ch_cfg;
  2756. fmt.sample_rate = wmapro_cfg->sample_rate;
  2757. fmt.avg_bytes_per_sec =
  2758. wmapro_cfg->avg_bytes_per_sec;
  2759. fmt.blk_align = wmapro_cfg->block_align;
  2760. fmt.bits_per_sample = wmapro_cfg->valid_bits_per_sample;
  2761. fmt.channel_mask = wmapro_cfg->ch_mask;
  2762. fmt.enc_options = wmapro_cfg->encode_opt;
  2763. fmt.usAdvancedEncodeOpt = wmapro_cfg->adv_encode_opt;
  2764. fmt.advanced_enc_options2 = wmapro_cfg->adv_encode_opt2;
  2765. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  2766. if (rc < 0) {
  2767. pr_err("%s:Comamnd open failed\n", __func__);
  2768. goto fail_cmd;
  2769. }
  2770. rc = wait_event_timeout(ac->cmd_wait,
  2771. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  2772. if (!rc) {
  2773. pr_err("%s:timeout. waited for FORMAT_UPDATE\n", __func__);
  2774. goto fail_cmd;
  2775. }
  2776. return 0;
  2777. fail_cmd:
  2778. return -EINVAL;
  2779. }
  2780. int q6asm_media_format_block_amrwbplus(struct audio_client *ac,
  2781. struct asm_amrwbplus_cfg *cfg)
  2782. {
  2783. struct asm_amrwbplus_fmt_blk_v2 fmt;
  2784. int rc = 0;
  2785. pr_debug("%s:session[%d]band-mode[%d]frame-fmt[%d]ch[%d]\n",
  2786. __func__,
  2787. ac->session,
  2788. cfg->amr_band_mode,
  2789. cfg->amr_frame_fmt,
  2790. cfg->num_channels);
  2791. q6asm_add_hdr(ac, &fmt.hdr, sizeof(fmt), TRUE);
  2792. atomic_set(&ac->cmd_state, 1);
  2793. fmt.hdr.opcode = ASM_DATA_CMD_MEDIA_FMT_UPDATE_V2;
  2794. fmt.fmtblk.fmt_blk_size = sizeof(fmt) - sizeof(fmt.hdr) -
  2795. sizeof(fmt.fmtblk);
  2796. fmt.amr_frame_fmt = cfg->amr_frame_fmt;
  2797. rc = apr_send_pkt(ac->apr, (uint32_t *) &fmt);
  2798. if (rc < 0) {
  2799. pr_err("%s:Comamnd media format update failed..\n", __func__);
  2800. goto fail_cmd;
  2801. }
  2802. rc = wait_event_timeout(ac->cmd_wait,
  2803. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  2804. if (!rc) {
  2805. pr_err("%s:timeout. waited for FORMAT_UPDATE\n", __func__);
  2806. goto fail_cmd;
  2807. }
  2808. return 0;
  2809. fail_cmd:
  2810. return -EINVAL;
  2811. }
  2812. int q6asm_ds1_set_endp_params(struct audio_client *ac,
  2813. int param_id, int param_value)
  2814. {
  2815. struct asm_dec_ddp_endp_param_v2 ddp_cfg;
  2816. int rc = 0;
  2817. pr_debug("%s: session[%d]param_id[%d]param_value[%d]", __func__,
  2818. ac->session, param_id, param_value);
  2819. q6asm_add_hdr(ac, &ddp_cfg.hdr, sizeof(ddp_cfg), TRUE);
  2820. atomic_set(&ac->cmd_state, 1);
  2821. ddp_cfg.hdr.opcode = ASM_STREAM_CMD_SET_ENCDEC_PARAM;
  2822. ddp_cfg.encdec.param_id = param_id;
  2823. ddp_cfg.encdec.param_size = sizeof(struct asm_dec_ddp_endp_param_v2) -
  2824. (sizeof(struct apr_hdr) +
  2825. sizeof(struct asm_stream_cmd_set_encdec_param));
  2826. ddp_cfg.endp_param_value = param_value;
  2827. rc = apr_send_pkt(ac->apr, (uint32_t *) &ddp_cfg);
  2828. if (rc < 0) {
  2829. pr_err("%s:Command opcode[0x%x] failed\n",
  2830. __func__, ASM_STREAM_CMD_SET_ENCDEC_PARAM);
  2831. goto fail_cmd;
  2832. }
  2833. rc = wait_event_timeout(ac->cmd_wait,
  2834. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  2835. if (!rc) {
  2836. pr_err("%s:timeout opcode[0x%x]\n", __func__,
  2837. ddp_cfg.hdr.opcode);
  2838. rc = -ETIMEDOUT;
  2839. goto fail_cmd;
  2840. }
  2841. return 0;
  2842. fail_cmd:
  2843. return rc;
  2844. }
  2845. int q6asm_memory_map(struct audio_client *ac, uint32_t buf_add, int dir,
  2846. uint32_t bufsz, uint32_t bufcnt)
  2847. {
  2848. struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
  2849. struct avs_shared_map_region_payload *mregions = NULL;
  2850. struct audio_port_data *port = NULL;
  2851. void *mmap_region_cmd = NULL;
  2852. void *payload = NULL;
  2853. struct asm_buffer_node *buffer_node = NULL;
  2854. int rc = 0;
  2855. int cmd_size = 0;
  2856. if (!ac || ac->mmap_apr == NULL) {
  2857. pr_err("%s: APR handle NULL\n", __func__);
  2858. return -EINVAL;
  2859. }
  2860. pr_debug("%s: Session[%d]\n", __func__, ac->session);
  2861. buffer_node = kmalloc(sizeof(struct asm_buffer_node), GFP_KERNEL);
  2862. if (!buffer_node)
  2863. return -ENOMEM;
  2864. cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
  2865. + sizeof(struct avs_shared_map_region_payload) * bufcnt;
  2866. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  2867. if (mmap_region_cmd == NULL) {
  2868. pr_err("%s: Mem alloc failed\n", __func__);
  2869. rc = -EINVAL;
  2870. kfree(buffer_node);
  2871. return rc;
  2872. }
  2873. mmap_regions = (struct avs_cmd_shared_mem_map_regions *)
  2874. mmap_region_cmd;
  2875. q6asm_add_mmaphdr(ac, &mmap_regions->hdr, cmd_size,
  2876. TRUE, ((ac->session << 8) | dir));
  2877. atomic_set(&ac->mem_state, 1);
  2878. mmap_regions->hdr.opcode = ASM_CMD_SHARED_MEM_MAP_REGIONS;
  2879. mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
  2880. mmap_regions->num_regions = bufcnt & 0x00ff;
  2881. mmap_regions->property_flag = 0x00;
  2882. payload = ((u8 *) mmap_region_cmd +
  2883. sizeof(struct avs_cmd_shared_mem_map_regions));
  2884. mregions = (struct avs_shared_map_region_payload *)payload;
  2885. ac->port[dir].tmp_hdl = 0;
  2886. port = &ac->port[dir];
  2887. pr_debug("%s, buf_add 0x%x, bufsz: %d\n", __func__, buf_add, bufsz);
  2888. mregions->shm_addr_lsw = buf_add;
  2889. /* Using only 32 bit address */
  2890. mregions->shm_addr_msw = 0;
  2891. mregions->mem_size_bytes = bufsz;
  2892. ++mregions;
  2893. rc = apr_send_pkt(ac->mmap_apr, (uint32_t *) mmap_region_cmd);
  2894. if (rc < 0) {
  2895. pr_err("mmap op[0x%x]rc[%d]\n",
  2896. mmap_regions->hdr.opcode, rc);
  2897. rc = -EINVAL;
  2898. kfree(buffer_node);
  2899. goto fail_cmd;
  2900. }
  2901. rc = wait_event_timeout(ac->mem_wait,
  2902. (atomic_read(&ac->mem_state) == 0 &&
  2903. ac->port[dir].tmp_hdl), 5*HZ);
  2904. if (!rc) {
  2905. pr_err("timeout. waited for memory_map\n");
  2906. rc = -EINVAL;
  2907. kfree(buffer_node);
  2908. goto fail_cmd;
  2909. }
  2910. buffer_node->buf_addr_lsw = buf_add;
  2911. buffer_node->mmap_hdl = ac->port[dir].tmp_hdl;
  2912. list_add_tail(&buffer_node->list, &ac->port[dir].mem_map_handle);
  2913. ac->port[dir].tmp_hdl = 0;
  2914. rc = 0;
  2915. fail_cmd:
  2916. kfree(mmap_region_cmd);
  2917. return rc;
  2918. }
  2919. int q6asm_memory_unmap(struct audio_client *ac, uint32_t buf_add, int dir)
  2920. {
  2921. struct avs_cmd_shared_mem_unmap_regions mem_unmap;
  2922. struct asm_buffer_node *buf_node = NULL;
  2923. struct list_head *ptr, *next;
  2924. int rc = 0;
  2925. if (!ac || this_mmap.apr == NULL) {
  2926. pr_err("%s: APR handle NULL\n", __func__);
  2927. return -EINVAL;
  2928. }
  2929. pr_debug("%s: Session[%d]\n", __func__, ac->session);
  2930. q6asm_add_mmaphdr(ac, &mem_unmap.hdr,
  2931. sizeof(struct avs_cmd_shared_mem_unmap_regions),
  2932. TRUE, ((ac->session << 8) | dir));
  2933. atomic_set(&ac->mem_state, 1);
  2934. mem_unmap.hdr.opcode = ASM_CMD_SHARED_MEM_UNMAP_REGIONS;
  2935. mem_unmap.mem_map_handle = 0;
  2936. list_for_each_safe(ptr, next, &ac->port[dir].mem_map_handle) {
  2937. buf_node = list_entry(ptr, struct asm_buffer_node,
  2938. list);
  2939. if (buf_node->buf_addr_lsw == buf_add) {
  2940. pr_debug("%s: Found the element\n", __func__);
  2941. mem_unmap.mem_map_handle = buf_node->mmap_hdl;
  2942. break;
  2943. }
  2944. }
  2945. pr_debug("%s: mem_unmap-mem_map_handle: 0x%x",
  2946. __func__, mem_unmap.mem_map_handle);
  2947. if (mem_unmap.mem_map_handle == 0) {
  2948. pr_err("%s Do not send null mem handle to DSP\n", __func__);
  2949. rc = 0;
  2950. goto fail_cmd;
  2951. }
  2952. rc = apr_send_pkt(ac->mmap_apr, (uint32_t *) &mem_unmap);
  2953. if (rc < 0) {
  2954. pr_err("mem_unmap op[0x%x]rc[%d]\n",
  2955. mem_unmap.hdr.opcode, rc);
  2956. rc = -EINVAL;
  2957. goto fail_cmd;
  2958. }
  2959. rc = wait_event_timeout(ac->mem_wait,
  2960. (atomic_read(&ac->mem_state) == 0), 5 * HZ);
  2961. if (!rc) {
  2962. pr_err("%s timeout. waited for memory_unmap of handle 0x%x\n",
  2963. __func__, mem_unmap.mem_map_handle);
  2964. rc = -ETIMEDOUT;
  2965. goto fail_cmd;
  2966. } else if (atomic_read(&ac->unmap_cb_success) == 0) {
  2967. pr_err("%s Error in mem unmap callback of handle 0x%x\n",
  2968. __func__, mem_unmap.mem_map_handle);
  2969. rc = -EINVAL;
  2970. goto fail_cmd;
  2971. }
  2972. rc = 0;
  2973. fail_cmd:
  2974. list_for_each_safe(ptr, next, &ac->port[dir].mem_map_handle) {
  2975. buf_node = list_entry(ptr, struct asm_buffer_node,
  2976. list);
  2977. if (buf_node->buf_addr_lsw == buf_add) {
  2978. list_del(&buf_node->list);
  2979. kfree(buf_node);
  2980. break;
  2981. }
  2982. }
  2983. return rc;
  2984. }
  2985. static int q6asm_memory_map_regions(struct audio_client *ac, int dir,
  2986. uint32_t bufsz, uint32_t bufcnt,
  2987. bool is_contiguous)
  2988. {
  2989. struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
  2990. struct avs_shared_map_region_payload *mregions = NULL;
  2991. struct audio_port_data *port = NULL;
  2992. struct audio_buffer *ab = NULL;
  2993. void *mmap_region_cmd = NULL;
  2994. void *payload = NULL;
  2995. struct asm_buffer_node *buffer_node = NULL;
  2996. int rc = 0;
  2997. int i = 0;
  2998. uint32_t cmd_size = 0;
  2999. uint32_t bufcnt_t;
  3000. uint32_t bufsz_t;
  3001. if (!ac || ac->mmap_apr == NULL) {
  3002. pr_err("%s: APR handle NULL\n", __func__);
  3003. return -EINVAL;
  3004. }
  3005. pr_debug("%s: Session[%d]\n", __func__, ac->session);
  3006. bufcnt_t = (is_contiguous) ? 1 : bufcnt;
  3007. bufsz_t = (is_contiguous) ? (bufsz * bufcnt) : bufsz;
  3008. if (is_contiguous) {
  3009. /* The size to memory map should be multiple of 4K bytes */
  3010. bufsz_t = PAGE_ALIGN(bufsz_t);
  3011. }
  3012. if (bufcnt_t > (UINT_MAX
  3013. - sizeof(struct avs_cmd_shared_mem_map_regions))
  3014. / sizeof(struct avs_shared_map_region_payload)) {
  3015. pr_err("%s: Unsigned Integer Overflow. bufcnt_t = %u\n",
  3016. __func__, bufcnt_t);
  3017. return -EINVAL;
  3018. }
  3019. cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
  3020. + (sizeof(struct avs_shared_map_region_payload)
  3021. * bufcnt_t);
  3022. if (bufcnt > (UINT_MAX / sizeof(struct asm_buffer_node))) {
  3023. pr_err("%s: Unsigned Integer Overflow. bufcnt = %u\n",
  3024. __func__, bufcnt);
  3025. return -EINVAL;
  3026. }
  3027. buffer_node = kzalloc(sizeof(struct asm_buffer_node) * bufcnt,
  3028. GFP_KERNEL);
  3029. if (!buffer_node) {
  3030. pr_err("%s: Mem alloc failed for asm_buffer_node\n",
  3031. __func__);
  3032. return -ENOMEM;
  3033. }
  3034. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  3035. if (mmap_region_cmd == NULL) {
  3036. pr_err("%s: Mem alloc failed\n", __func__);
  3037. rc = -EINVAL;
  3038. kfree(buffer_node);
  3039. return rc;
  3040. }
  3041. mmap_regions = (struct avs_cmd_shared_mem_map_regions *)
  3042. mmap_region_cmd;
  3043. q6asm_add_mmaphdr(ac, &mmap_regions->hdr, cmd_size, TRUE,
  3044. ((ac->session << 8) | dir));
  3045. atomic_set(&ac->mem_state, 1);
  3046. pr_debug("mmap_region=0x%p token=0x%x\n",
  3047. mmap_regions, ((ac->session << 8) | dir));
  3048. mmap_regions->hdr.opcode = ASM_CMD_SHARED_MEM_MAP_REGIONS;
  3049. mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
  3050. mmap_regions->num_regions = bufcnt_t; /*bufcnt & 0x00ff; */
  3051. mmap_regions->property_flag = 0x00;
  3052. pr_debug("map_regions->nregions = %d\n", mmap_regions->num_regions);
  3053. payload = ((u8 *) mmap_region_cmd +
  3054. sizeof(struct avs_cmd_shared_mem_map_regions));
  3055. mregions = (struct avs_shared_map_region_payload *)payload;
  3056. ac->port[dir].tmp_hdl = 0;
  3057. port = &ac->port[dir];
  3058. for (i = 0; i < bufcnt_t; i++) {
  3059. ab = &port->buf[i];
  3060. mregions->shm_addr_lsw = ab->phys;
  3061. /* Using only 32 bit address */
  3062. mregions->shm_addr_msw = 0;
  3063. mregions->mem_size_bytes = bufsz_t;
  3064. ++mregions;
  3065. }
  3066. rc = apr_send_pkt(ac->mmap_apr, (uint32_t *) mmap_region_cmd);
  3067. if (rc < 0) {
  3068. pr_err("mmap_regions op[0x%x]rc[%d]\n",
  3069. mmap_regions->hdr.opcode, rc);
  3070. rc = -EINVAL;
  3071. kfree(buffer_node);
  3072. goto fail_cmd;
  3073. }
  3074. rc = wait_event_timeout(ac->mem_wait,
  3075. (atomic_read(&ac->mem_state) == 0)
  3076. , 5*HZ);
  3077. if (!rc) {
  3078. pr_err("timeout. waited for memory_map\n");
  3079. rc = -EINVAL;
  3080. kfree(buffer_node);
  3081. goto fail_cmd;
  3082. }
  3083. mutex_lock(&ac->cmd_lock);
  3084. for (i = 0; i < bufcnt; i++) {
  3085. ab = &port->buf[i];
  3086. buffer_node[i].buf_addr_lsw = ab->phys;
  3087. buffer_node[i].mmap_hdl = ac->port[dir].tmp_hdl;
  3088. list_add_tail(&buffer_node[i].list,
  3089. &ac->port[dir].mem_map_handle);
  3090. pr_debug("%s: i=%d, bufadd[i] = 0x%x, maphdl[i] = 0x%x\n",
  3091. __func__, i, buffer_node[i].buf_addr_lsw,
  3092. buffer_node[i].mmap_hdl);
  3093. }
  3094. ac->port[dir].tmp_hdl = 0;
  3095. mutex_unlock(&ac->cmd_lock);
  3096. rc = 0;
  3097. pr_debug("%s: exit\n", __func__);
  3098. fail_cmd:
  3099. kfree(mmap_region_cmd);
  3100. return rc;
  3101. }
  3102. static int q6asm_memory_unmap_regions(struct audio_client *ac, int dir)
  3103. {
  3104. struct avs_cmd_shared_mem_unmap_regions mem_unmap;
  3105. struct audio_port_data *port = NULL;
  3106. struct asm_buffer_node *buf_node = NULL;
  3107. struct list_head *ptr, *next;
  3108. uint32_t buf_add;
  3109. int rc = 0;
  3110. int cmd_size = 0;
  3111. if (!ac || ac->mmap_apr == NULL) {
  3112. pr_err("%s: APR handle NULL\n", __func__);
  3113. return -EINVAL;
  3114. }
  3115. pr_debug("%s: Session[%d]\n", __func__, ac->session);
  3116. cmd_size = sizeof(struct avs_cmd_shared_mem_unmap_regions);
  3117. q6asm_add_mmaphdr(ac, &mem_unmap.hdr, cmd_size,
  3118. TRUE, ((ac->session << 8) | dir));
  3119. atomic_set(&ac->mem_state, 1);
  3120. port = &ac->port[dir];
  3121. buf_add = (uint32_t)port->buf->phys;
  3122. mem_unmap.hdr.opcode = ASM_CMD_SHARED_MEM_UNMAP_REGIONS;
  3123. mem_unmap.mem_map_handle = 0;
  3124. list_for_each_safe(ptr, next, &ac->port[dir].mem_map_handle) {
  3125. buf_node = list_entry(ptr, struct asm_buffer_node,
  3126. list);
  3127. if (buf_node->buf_addr_lsw == buf_add) {
  3128. pr_debug("%s: Found the element\n", __func__);
  3129. mem_unmap.mem_map_handle = buf_node->mmap_hdl;
  3130. break;
  3131. }
  3132. }
  3133. pr_debug("%s: mem_unmap-mem_map_handle: 0x%x",
  3134. __func__, mem_unmap.mem_map_handle);
  3135. if (mem_unmap.mem_map_handle == 0) {
  3136. pr_err("%s Do not send null mem handle to DSP\n", __func__);
  3137. rc = 0;
  3138. goto fail_cmd;
  3139. }
  3140. rc = apr_send_pkt(ac->mmap_apr, (uint32_t *) &mem_unmap);
  3141. if (rc < 0) {
  3142. pr_err("mmap_regions op[0x%x]rc[%d]\n",
  3143. mem_unmap.hdr.opcode, rc);
  3144. goto fail_cmd;
  3145. }
  3146. rc = wait_event_timeout(ac->mem_wait,
  3147. (atomic_read(&ac->mem_state) == 0), 5*HZ);
  3148. if (!rc) {
  3149. pr_err("%s timeout. waited for memory_unmap of handle 0x%x\n",
  3150. __func__, mem_unmap.mem_map_handle);
  3151. rc = -ETIMEDOUT;
  3152. goto fail_cmd;
  3153. } else if (atomic_read(&ac->unmap_cb_success) == 0) {
  3154. pr_err("%s Error in mem unmap callback of handle 0x%x\n",
  3155. __func__, mem_unmap.mem_map_handle);
  3156. rc = -EINVAL;
  3157. goto fail_cmd;
  3158. }
  3159. rc = 0;
  3160. fail_cmd:
  3161. list_for_each_safe(ptr, next, &ac->port[dir].mem_map_handle) {
  3162. buf_node = list_entry(ptr, struct asm_buffer_node,
  3163. list);
  3164. if (buf_node->buf_addr_lsw == buf_add) {
  3165. list_del(&buf_node->list);
  3166. kfree(buf_node);
  3167. break;
  3168. }
  3169. }
  3170. return rc;
  3171. }
  3172. int q6asm_set_lrgain(struct audio_client *ac, int left_gain, int right_gain)
  3173. {
  3174. struct asm_volume_ctrl_lr_chan_gain lrgain;
  3175. int sz = 0;
  3176. int rc = 0;
  3177. if (!ac || ac->apr == NULL) {
  3178. pr_err("%s: APR handle NULL\n", __func__);
  3179. rc = -EINVAL;
  3180. goto fail_cmd;
  3181. }
  3182. sz = sizeof(struct asm_volume_ctrl_lr_chan_gain);
  3183. q6asm_add_hdr_async(ac, &lrgain.hdr, sz, TRUE);
  3184. atomic_set(&ac->cmd_state, 1);
  3185. lrgain.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  3186. lrgain.param.data_payload_addr_lsw = 0;
  3187. lrgain.param.data_payload_addr_msw = 0;
  3188. lrgain.param.mem_map_handle = 0;
  3189. lrgain.param.data_payload_size = sizeof(lrgain) -
  3190. sizeof(lrgain.hdr) - sizeof(lrgain.param);
  3191. lrgain.data.module_id = ASM_MODULE_ID_VOL_CTRL;
  3192. lrgain.data.param_id = ASM_PARAM_ID_VOL_CTRL_LR_CHANNEL_GAIN;
  3193. lrgain.data.param_size = lrgain.param.data_payload_size -
  3194. sizeof(lrgain.data);
  3195. lrgain.data.reserved = 0;
  3196. lrgain.l_chan_gain = left_gain;
  3197. lrgain.r_chan_gain = right_gain;
  3198. rc = apr_send_pkt(ac->apr, (uint32_t *) &lrgain);
  3199. if (rc < 0) {
  3200. pr_err("%s: set-params send failed paramid[0x%x]\n", __func__,
  3201. lrgain.data.param_id);
  3202. rc = -EINVAL;
  3203. goto fail_cmd;
  3204. }
  3205. rc = wait_event_timeout(ac->cmd_wait,
  3206. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  3207. if (!rc) {
  3208. pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
  3209. lrgain.data.param_id);
  3210. rc = -EINVAL;
  3211. goto fail_cmd;
  3212. }
  3213. rc = 0;
  3214. fail_cmd:
  3215. return rc;
  3216. }
  3217. int q6asm_set_mute(struct audio_client *ac, int muteflag)
  3218. {
  3219. struct asm_volume_ctrl_mute_config mute;
  3220. int sz = 0;
  3221. int rc = 0;
  3222. if (!ac || ac->apr == NULL) {
  3223. pr_err("%s: APR handle NULL\n", __func__);
  3224. rc = -EINVAL;
  3225. goto fail_cmd;
  3226. }
  3227. sz = sizeof(struct asm_volume_ctrl_mute_config);
  3228. q6asm_add_hdr_async(ac, &mute.hdr, sz, TRUE);
  3229. atomic_set(&ac->cmd_state, 1);
  3230. mute.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  3231. mute.param.data_payload_addr_lsw = 0;
  3232. mute.param.data_payload_addr_msw = 0;
  3233. mute.param.mem_map_handle = 0;
  3234. mute.param.data_payload_size = sizeof(mute) -
  3235. sizeof(mute.hdr) - sizeof(mute.param);
  3236. mute.data.module_id = ASM_MODULE_ID_VOL_CTRL;
  3237. mute.data.param_id = ASM_PARAM_ID_VOL_CTRL_MUTE_CONFIG;
  3238. mute.data.param_size = mute.param.data_payload_size - sizeof(mute.data);
  3239. mute.data.reserved = 0;
  3240. mute.mute_flag = muteflag;
  3241. rc = apr_send_pkt(ac->apr, (uint32_t *) &mute);
  3242. if (rc < 0) {
  3243. pr_err("%s: set-params send failed paramid[0x%x]\n", __func__,
  3244. mute.data.param_id);
  3245. rc = -EINVAL;
  3246. goto fail_cmd;
  3247. }
  3248. rc = wait_event_timeout(ac->cmd_wait,
  3249. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  3250. if (!rc) {
  3251. pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
  3252. mute.data.param_id);
  3253. rc = -EINVAL;
  3254. goto fail_cmd;
  3255. }
  3256. rc = 0;
  3257. fail_cmd:
  3258. return rc;
  3259. }
  3260. int q6asm_set_volume(struct audio_client *ac, int volume)
  3261. {
  3262. struct asm_volume_ctrl_master_gain vol;
  3263. int sz = 0;
  3264. int rc = 0;
  3265. if (!ac || ac->apr == NULL) {
  3266. pr_err("%s: APR handle NULL\n", __func__);
  3267. rc = -EINVAL;
  3268. goto fail_cmd;
  3269. }
  3270. sz = sizeof(struct asm_volume_ctrl_master_gain);
  3271. q6asm_add_hdr_async(ac, &vol.hdr, sz, TRUE);
  3272. atomic_set(&ac->cmd_state, 1);
  3273. vol.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  3274. vol.param.data_payload_addr_lsw = 0;
  3275. vol.param.data_payload_addr_msw = 0;
  3276. vol.param.mem_map_handle = 0;
  3277. vol.param.data_payload_size = sizeof(vol) -
  3278. sizeof(vol.hdr) - sizeof(vol.param);
  3279. vol.data.module_id = ASM_MODULE_ID_VOL_CTRL;
  3280. vol.data.param_id = ASM_PARAM_ID_VOL_CTRL_MASTER_GAIN;
  3281. vol.data.param_size = vol.param.data_payload_size - sizeof(vol.data);
  3282. vol.data.reserved = 0;
  3283. vol.master_gain = volume;
  3284. rc = apr_send_pkt(ac->apr, (uint32_t *) &vol);
  3285. if (rc < 0) {
  3286. pr_err("%s: set-params send failed paramid[0x%x]\n", __func__,
  3287. vol.data.param_id);
  3288. rc = -EINVAL;
  3289. goto fail_cmd;
  3290. }
  3291. rc = wait_event_timeout(ac->cmd_wait,
  3292. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  3293. if (!rc) {
  3294. pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
  3295. vol.data.param_id);
  3296. rc = -EINVAL;
  3297. goto fail_cmd;
  3298. }
  3299. rc = 0;
  3300. fail_cmd:
  3301. return rc;
  3302. }
  3303. int q6asm_set_softpause(struct audio_client *ac,
  3304. struct asm_softpause_params *pause_param)
  3305. {
  3306. struct asm_soft_pause_params softpause;
  3307. int sz = 0;
  3308. int rc = 0;
  3309. if (!ac || ac->apr == NULL) {
  3310. pr_err("%s: APR handle NULL\n", __func__);
  3311. rc = -EINVAL;
  3312. goto fail_cmd;
  3313. }
  3314. sz = sizeof(struct asm_soft_pause_params);
  3315. q6asm_add_hdr_async(ac, &softpause.hdr, sz, TRUE);
  3316. atomic_set(&ac->cmd_state, 1);
  3317. softpause.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  3318. softpause.param.data_payload_addr_lsw = 0;
  3319. softpause.param.data_payload_addr_msw = 0;
  3320. softpause.param.mem_map_handle = 0;
  3321. softpause.param.data_payload_size = sizeof(softpause) -
  3322. sizeof(softpause.hdr) - sizeof(softpause.param);
  3323. softpause.data.module_id = ASM_MODULE_ID_VOL_CTRL;
  3324. softpause.data.param_id = ASM_PARAM_ID_SOFT_PAUSE_PARAMETERS;
  3325. softpause.data.param_size = softpause.param.data_payload_size -
  3326. sizeof(softpause.data);
  3327. softpause.data.reserved = 0;
  3328. softpause.enable_flag = pause_param->enable;
  3329. softpause.period = pause_param->period;
  3330. softpause.step = pause_param->step;
  3331. softpause.ramping_curve = pause_param->rampingcurve;
  3332. rc = apr_send_pkt(ac->apr, (uint32_t *) &softpause);
  3333. if (rc < 0) {
  3334. pr_err("%s: set-params send failed paramid[0x%x]\n", __func__,
  3335. softpause.data.param_id);
  3336. rc = -EINVAL;
  3337. goto fail_cmd;
  3338. }
  3339. rc = wait_event_timeout(ac->cmd_wait,
  3340. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  3341. if (!rc) {
  3342. pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
  3343. softpause.data.param_id);
  3344. rc = -EINVAL;
  3345. goto fail_cmd;
  3346. }
  3347. rc = 0;
  3348. fail_cmd:
  3349. return rc;
  3350. }
  3351. int q6asm_set_softvolume(struct audio_client *ac,
  3352. struct asm_softvolume_params *softvol_param)
  3353. {
  3354. struct asm_soft_step_volume_params softvol;
  3355. int sz = 0;
  3356. int rc = 0;
  3357. if (!ac || ac->apr == NULL) {
  3358. pr_err("%s: APR handle NULL\n", __func__);
  3359. rc = -EINVAL;
  3360. goto fail_cmd;
  3361. }
  3362. sz = sizeof(struct asm_soft_step_volume_params);
  3363. q6asm_add_hdr_async(ac, &softvol.hdr, sz, TRUE);
  3364. atomic_set(&ac->cmd_state, 1);
  3365. softvol.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  3366. softvol.param.data_payload_addr_lsw = 0;
  3367. softvol.param.data_payload_addr_msw = 0;
  3368. softvol.param.mem_map_handle = 0;
  3369. softvol.param.data_payload_size = sizeof(softvol) -
  3370. sizeof(softvol.hdr) - sizeof(softvol.param);
  3371. softvol.data.module_id = ASM_MODULE_ID_VOL_CTRL;
  3372. softvol.data.param_id = ASM_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS;
  3373. softvol.data.param_size = softvol.param.data_payload_size -
  3374. sizeof(softvol.data);
  3375. softvol.data.reserved = 0;
  3376. softvol.period = softvol_param->period;
  3377. softvol.step = softvol_param->step;
  3378. softvol.ramping_curve = softvol_param->rampingcurve;
  3379. rc = apr_send_pkt(ac->apr, (uint32_t *) &softvol);
  3380. if (rc < 0) {
  3381. pr_err("%s: set-params send failed paramid[0x%x]\n", __func__,
  3382. softvol.data.param_id);
  3383. rc = -EINVAL;
  3384. goto fail_cmd;
  3385. }
  3386. rc = wait_event_timeout(ac->cmd_wait,
  3387. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  3388. if (!rc) {
  3389. pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
  3390. softvol.data.param_id);
  3391. rc = -EINVAL;
  3392. goto fail_cmd;
  3393. }
  3394. rc = 0;
  3395. fail_cmd:
  3396. return rc;
  3397. }
  3398. int q6asm_set_sa(struct audio_client *ac, int *param)
  3399. {
  3400. int sz = 0;
  3401. int rc = 0;
  3402. int i = 0;
  3403. struct asm_stream_cmd_set_pp_params_sa cmd ;
  3404. if(ac == NULL){
  3405. printk("%s: audio client is null\n", __func__);
  3406. return -1;
  3407. }
  3408. sz = sizeof(struct asm_stream_cmd_set_pp_params_sa);
  3409. q6asm_add_hdr_async(ac, &cmd.hdr, sz, TRUE);
  3410. cmd.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  3411. cmd.param.data_payload_addr_lsw = 0;
  3412. cmd.param.data_payload_addr_msw = 0;
  3413. cmd.param.mem_map_handle = 0;
  3414. cmd.param.data_payload_size =
  3415. sizeof(cmd) - sizeof(cmd.hdr) - sizeof(cmd.param);
  3416. cmd.data.module_id = ASM_MODULE_ID_PP_SA;
  3417. cmd.data.param_id = ASM_PARAM_ID_PP_SA_PARAMS;
  3418. cmd.data.param_size = cmd.param.data_payload_size - sizeof(cmd.data);
  3419. cmd.data.reserved = 0;
  3420. /* SA paramerters */
  3421. cmd.sa_param.OutDevice = param[0];
  3422. cmd.sa_param.Preset = param[1];
  3423. for( i=0; i<7; i++)
  3424. cmd.sa_param.EqLev[i] = param[i+2];
  3425. cmd.sa_param.m3Dlevel = param[9];
  3426. cmd.sa_param.BElevel = param[10];
  3427. cmd.sa_param.CHlevel = param[11];
  3428. cmd.sa_param.CHRoomSize = param[12];
  3429. cmd.sa_param.Clalevel = param[13];
  3430. cmd.sa_param.volume = param[14];
  3431. cmd.sa_param.Sqrow = param[15];
  3432. cmd.sa_param.Sqcol = param[16];
  3433. cmd.sa_param.TabInfo = param[17];
  3434. cmd.sa_param.NewUI = param[18];
  3435. printk("S ");
  3436. for(i = 0 ; i < 19; i++)
  3437. printk("%d ", param[i]);
  3438. printk("\n");
  3439. rc = apr_send_pkt(ac->apr, (uint32_t *)&cmd);
  3440. if (rc < 0) {
  3441. pr_err("%s: send_pkt failed. paramid[0x%x]\n", __func__,
  3442. cmd.data.param_id);
  3443. rc = -EINVAL;
  3444. goto fail_cmd;
  3445. }
  3446. fail_cmd:
  3447. return rc;
  3448. }
  3449. int q6asm_set_vsp(struct audio_client *ac, int *param)
  3450. {
  3451. int sz = 0;
  3452. int rc = 0;
  3453. struct asm_stream_cmd_set_pp_params_vsp cmd;
  3454. if(ac == NULL) {
  3455. printk("%s: audio client is null\n", __func__);
  3456. return -1;
  3457. }
  3458. sz = sizeof(struct asm_stream_cmd_set_pp_params_vsp);
  3459. q6asm_add_hdr_async(ac, &cmd.hdr, sz, TRUE);
  3460. cmd.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  3461. cmd.param.data_payload_addr_lsw = 0;
  3462. cmd.param.data_payload_addr_msw = 0;
  3463. cmd.param.mem_map_handle = 0;
  3464. cmd.param.data_payload_size =
  3465. sizeof(cmd) - sizeof(cmd.hdr) - sizeof(cmd.param);
  3466. cmd.data.module_id = ASM_MODULE_ID_PP_SA_VSP;
  3467. cmd.data.param_id = ASM_PARAM_ID_PP_SA_VSP_PARAMS;
  3468. cmd.data.param_size = cmd.param.data_payload_size - sizeof(cmd.data);
  3469. cmd.data.reserved = 0;
  3470. /* VSP paramerters */
  3471. cmd.speed_int = param[0];
  3472. printk("%d ", param[0]);
  3473. printk("\n");
  3474. rc = apr_send_pkt(ac->apr, (uint32_t *)&cmd);
  3475. if (rc < 0) {
  3476. rc = -EINVAL;
  3477. goto fail_cmd;
  3478. }
  3479. fail_cmd:
  3480. return rc;
  3481. }
  3482. int q6asm_set_dha(struct audio_client *ac,int *param)
  3483. {
  3484. int sz = 0;
  3485. int rc = 0;
  3486. int i = 0;
  3487. struct asm_stream_cmd_set_pp_params_dha cmd;
  3488. if(ac == NULL) {
  3489. printk("%s: audio client is null\n", __func__);
  3490. return -1;
  3491. }
  3492. sz = sizeof(struct asm_stream_cmd_set_pp_params_dha);
  3493. q6asm_add_hdr_async(ac, &cmd.hdr, sz, TRUE);
  3494. cmd.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  3495. cmd.param.data_payload_addr_lsw = 0;
  3496. cmd.param.data_payload_addr_msw = 0;
  3497. cmd.param.mem_map_handle = 0;
  3498. cmd.param.data_payload_size =
  3499. sizeof(cmd) - sizeof(cmd.hdr) - sizeof(cmd.param);
  3500. cmd.data.module_id = ASM_MODULE_ID_PP_DHA;
  3501. cmd.data.param_id = ASM_PARAM_ID_PP_DHA_PARAMS;
  3502. cmd.data.param_size = cmd.param.data_payload_size - sizeof(cmd.data);
  3503. cmd.data.reserved = 0;
  3504. /* DHA paramerters */
  3505. cmd.enable = param[0];
  3506. for(i = 0; i < 12; i++)
  3507. cmd.gain[i/6][i%6] = param[i+1];
  3508. rc = apr_send_pkt(ac->apr, (uint32_t *)&cmd);
  3509. if (rc < 0) {
  3510. rc = -EINVAL;
  3511. goto fail_cmd;
  3512. }
  3513. fail_cmd:
  3514. return rc;
  3515. }
  3516. int q6asm_set_lrsm(struct audio_client *ac,int *param)
  3517. {
  3518. int sz = 0;
  3519. int rc = 0;
  3520. struct asm_stream_cmd_set_pp_params_lrsm cmd;
  3521. if(ac == NULL) {
  3522. printk("%s: audio client is null\n", __func__);
  3523. return -1;
  3524. }
  3525. sz = sizeof(struct asm_stream_cmd_set_pp_params_lrsm);
  3526. q6asm_add_hdr_async(ac, &cmd.hdr, sz, TRUE);
  3527. cmd.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  3528. cmd.param.data_payload_addr_lsw = 0;
  3529. cmd.param.data_payload_addr_msw = 0;
  3530. cmd.param.mem_map_handle = 0;
  3531. cmd.param.data_payload_size =
  3532. sizeof(cmd) - sizeof(cmd.hdr) - sizeof(cmd.param);
  3533. cmd.data.module_id = ASM_MODULE_ID_PP_LRSM;
  3534. cmd.data.param_id = ASM_PARAM_ID_PP_LRSM_PARAMS;
  3535. cmd.data.param_size = cmd.param.data_payload_size - sizeof(cmd.data);
  3536. cmd.data.reserved = 0;
  3537. /* LRSM paramerters */
  3538. cmd.sm = param[0];
  3539. cmd.lr = param[1];
  3540. rc = apr_send_pkt(ac->apr, (uint32_t *)&cmd);
  3541. if (rc < 0) {
  3542. rc = -EINVAL;
  3543. goto fail_cmd;
  3544. }
  3545. fail_cmd:
  3546. return rc;
  3547. }
  3548. int q6asm_set_msp(struct audio_client *ac, long *param)
  3549. {
  3550. int sz = 0;
  3551. int rc = 0;
  3552. struct asm_stream_cmd_set_pp_params_msp cmd;
  3553. if(ac == NULL) {
  3554. pr_err("%s: audio client is null\n", __func__);
  3555. return -1;
  3556. }
  3557. sz = sizeof(struct asm_stream_cmd_set_pp_params_msp);
  3558. q6asm_add_hdr_async(ac, &cmd.hdr, sz, TRUE);
  3559. cmd.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  3560. cmd.param.data_payload_addr_lsw = 0;
  3561. cmd.param.data_payload_addr_msw = 0;
  3562. cmd.param.mem_map_handle = 0;
  3563. cmd.param.data_payload_size =
  3564. sizeof(cmd) - sizeof(cmd.hdr) - sizeof(cmd.param);
  3565. cmd.data.module_id = ASM_MODULE_ID_PP_SA_MSP;
  3566. cmd.data.param_id = ASM_MODULE_ID_PP_SA_MSP_PARAM;
  3567. cmd.data.param_size = cmd.param.data_payload_size - sizeof(cmd.data);
  3568. cmd.data.reserved = 0;
  3569. /* VSP paramerters */
  3570. cmd.msp_int = param[0];
  3571. pr_info("%s: %d\n", __func__, cmd.msp_int);
  3572. rc = apr_send_pkt(ac->apr, (uint32_t *)&cmd);
  3573. if (rc < 0) {
  3574. rc = -EINVAL;
  3575. goto fail_cmd;
  3576. }
  3577. fail_cmd:
  3578. return rc;
  3579. }
  3580. int q6asm_set_sa_ep(struct audio_client *ac,int *param)
  3581. {
  3582. int sz = 0;
  3583. int rc = 0;
  3584. struct asm_stream_cmd_set_pp_params_sa_ep cmd;
  3585. if(ac == NULL) {
  3586. printk("%s: audio client is null\n", __func__);
  3587. return -1;
  3588. }
  3589. sz = sizeof(struct asm_stream_cmd_set_pp_params_sa_ep);
  3590. q6asm_add_hdr_async(ac, &cmd.hdr, sz, TRUE);
  3591. cmd.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  3592. cmd.param.data_payload_addr_lsw = 0;
  3593. cmd.param.data_payload_addr_msw = 0;
  3594. cmd.param.mem_map_handle = 0;
  3595. cmd.param.data_payload_size =
  3596. sizeof(cmd) - sizeof(cmd.hdr) - sizeof(cmd.param);
  3597. cmd.data.module_id = ASM_MODULE_ID_PP_SA_EP;
  3598. cmd.data.param_id = ASM_PARAM_ID_PP_SA_EP_PARAMS;
  3599. cmd.data.param_size = cmd.param.data_payload_size - sizeof(cmd.data);
  3600. cmd.data.reserved = 0;
  3601. /* SA EP paramerters */
  3602. cmd.enable = param[0];
  3603. cmd.score = param[1];
  3604. printk("%s: %d %d\n", __func__, cmd.enable, cmd.score);
  3605. rc = apr_send_pkt(ac->apr, (uint32_t *)&cmd);
  3606. if (rc < 0) {
  3607. rc = -EINVAL;
  3608. goto fail_cmd;
  3609. }
  3610. fail_cmd:
  3611. return rc;
  3612. }
  3613. int q6asm_get_sa_ep(struct audio_client *ac)
  3614. {
  3615. #if 0
  3616. int sz = 0;
  3617. int rc = 0;
  3618. struct asm_stream_cmd_get_pp_params_v2 cmd;
  3619. if(ac == NULL) {
  3620. printk("%s: audio client is null\n", __func__);
  3621. return -1;
  3622. }
  3623. sz = sizeof(struct asm_stream_cmd_get_pp_params_v2);
  3624. q6asm_add_hdr_async(ac, &cmd.hdr, sz, true);
  3625. cmd.hdr.opcode = ASM_STREAM_CMD_GET_PP_PARAMS_V2;
  3626. cmd.param.data_payload_addr_lsw = 0;
  3627. cmd.param.data_payload_addr_msw = 0;
  3628. cmd.param.mem_map_handle = 0;
  3629. cmd.data.module_id = ASM_MODULE_ID_PP_SA_EP;
  3630. cmd.data.param_id = ASM_PARAM_ID_PP_SA_EP_GET_PARAMS;
  3631. cmd.data.param_max_size = 20;
  3632. cmd.data.reserved = 0;
  3633. rc = apr_send_pkt(ac->apr, (uint32_t *)&cmd);
  3634. if (rc < 0) {
  3635. rc = -EINVAL;
  3636. goto fail_cmd;
  3637. }
  3638. fail_cmd:
  3639. return rc;
  3640. #else
  3641. return 0;
  3642. #endif
  3643. }
  3644. int q6asm_equalizer(struct audio_client *ac, void *eq_p)
  3645. {
  3646. struct asm_eq_params eq;
  3647. struct msm_audio_eq_stream_config *eq_params = NULL;
  3648. int i = 0;
  3649. int sz = 0;
  3650. int rc = 0;
  3651. if (!ac || ac->apr == NULL) {
  3652. pr_err("%s: APR handle NULL\n", __func__);
  3653. rc = -EINVAL;
  3654. goto fail_cmd;
  3655. }
  3656. if (eq_p == NULL) {
  3657. pr_err("%s[%d]: Invalid Eq param\n", __func__, ac->session);
  3658. rc = -EINVAL;
  3659. goto fail_cmd;
  3660. }
  3661. sz = sizeof(struct asm_eq_params);
  3662. eq_params = (struct msm_audio_eq_stream_config *) eq_p;
  3663. q6asm_add_hdr(ac, &eq.hdr, sz, TRUE);
  3664. atomic_set(&ac->cmd_state, 1);
  3665. eq.hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  3666. eq.param.data_payload_addr_lsw = 0;
  3667. eq.param.data_payload_addr_msw = 0;
  3668. eq.param.mem_map_handle = 0;
  3669. eq.param.data_payload_size = sizeof(eq) -
  3670. sizeof(eq.hdr) - sizeof(eq.param);
  3671. eq.data.module_id = ASM_MODULE_ID_EQUALIZER;
  3672. eq.data.param_id = ASM_PARAM_ID_EQUALIZER_PARAMETERS;
  3673. eq.data.param_size = eq.param.data_payload_size - sizeof(eq.data);
  3674. eq.enable_flag = eq_params->enable;
  3675. eq.num_bands = eq_params->num_bands;
  3676. pr_debug("%s: enable:%d numbands:%d\n", __func__, eq_params->enable,
  3677. eq_params->num_bands);
  3678. for (i = 0; i < eq_params->num_bands; i++) {
  3679. eq.eq_bands[i].band_idx =
  3680. eq_params->eq_bands[i].band_idx;
  3681. eq.eq_bands[i].filterype =
  3682. eq_params->eq_bands[i].filter_type;
  3683. eq.eq_bands[i].center_freq_hz =
  3684. eq_params->eq_bands[i].center_freq_hz;
  3685. eq.eq_bands[i].filter_gain =
  3686. eq_params->eq_bands[i].filter_gain;
  3687. eq.eq_bands[i].q_factor =
  3688. eq_params->eq_bands[i].q_factor;
  3689. pr_debug("%s: filter_type:%u bandnum:%d\n", __func__,
  3690. eq_params->eq_bands[i].filter_type, i);
  3691. pr_debug("%s: center_freq_hz:%u bandnum:%d\n", __func__,
  3692. eq_params->eq_bands[i].center_freq_hz, i);
  3693. pr_debug("%s: filter_gain:%d bandnum:%d\n", __func__,
  3694. eq_params->eq_bands[i].filter_gain, i);
  3695. pr_debug("%s: q_factor:%d bandnum:%d\n", __func__,
  3696. eq_params->eq_bands[i].q_factor, i);
  3697. }
  3698. rc = apr_send_pkt(ac->apr, (uint32_t *)&eq);
  3699. if (rc < 0) {
  3700. pr_err("%s: set-params send failed paramid[0x%x]\n", __func__,
  3701. eq.data.param_id);
  3702. rc = -EINVAL;
  3703. goto fail_cmd;
  3704. }
  3705. rc = wait_event_timeout(ac->cmd_wait,
  3706. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  3707. if (!rc) {
  3708. pr_err("%s: timeout, set-params paramid[0x%x]\n", __func__,
  3709. eq.data.param_id);
  3710. rc = -EINVAL;
  3711. goto fail_cmd;
  3712. }
  3713. rc = 0;
  3714. fail_cmd:
  3715. return rc;
  3716. }
  3717. int q6asm_read(struct audio_client *ac)
  3718. {
  3719. struct asm_data_cmd_read_v2 read;
  3720. struct asm_buffer_node *buf_node = NULL;
  3721. struct list_head *ptr, *next;
  3722. struct audio_buffer *ab;
  3723. int dsp_buf;
  3724. struct audio_port_data *port;
  3725. int rc;
  3726. if (!ac || ac->apr == NULL) {
  3727. pr_err("%s: APR handle NULL\n", __func__);
  3728. return -EINVAL;
  3729. }
  3730. if (ac->io_mode & SYNC_IO_MODE) {
  3731. port = &ac->port[OUT];
  3732. q6asm_add_hdr(ac, &read.hdr, sizeof(read), FALSE);
  3733. mutex_lock(&port->lock);
  3734. dsp_buf = port->dsp_buf;
  3735. if (port->buf == NULL) {
  3736. pr_err("%s buf is NULL\n", __func__);
  3737. mutex_unlock(&port->lock);
  3738. return -EINVAL;
  3739. }
  3740. ab = &port->buf[dsp_buf];
  3741. pr_debug("%s:session[%d]dsp-buf[%d][%p]cpu_buf[%d][%p]\n",
  3742. __func__,
  3743. ac->session,
  3744. dsp_buf,
  3745. (void *)port->buf[dsp_buf].data,
  3746. port->cpu_buf,
  3747. (void *)port->buf[port->cpu_buf].phys);
  3748. read.hdr.opcode = ASM_DATA_CMD_READ_V2;
  3749. read.buf_addr_lsw = ab->phys;
  3750. read.buf_addr_msw = 0;
  3751. list_for_each_safe(ptr, next, &ac->port[OUT].mem_map_handle) {
  3752. buf_node = list_entry(ptr, struct asm_buffer_node,
  3753. list);
  3754. if (buf_node->buf_addr_lsw == (uint32_t) ab->phys)
  3755. read.mem_map_handle = buf_node->mmap_hdl;
  3756. }
  3757. pr_debug("memory_map handle in q6asm_read: [%0x]:",
  3758. read.mem_map_handle);
  3759. read.buf_size = ab->size;
  3760. read.seq_id = port->dsp_buf;
  3761. read.hdr.token = port->dsp_buf;
  3762. port->dsp_buf = (port->dsp_buf + 1) & (port->max_buf_cnt - 1);
  3763. mutex_unlock(&port->lock);
  3764. pr_debug("%s:buf add[0x%x] token[%d] uid[%d]\n", __func__,
  3765. read.buf_addr_lsw,
  3766. read.hdr.token,
  3767. read.seq_id);
  3768. rc = apr_send_pkt(ac->apr, (uint32_t *) &read);
  3769. if (rc < 0) {
  3770. pr_err("read op[0x%x]rc[%d]\n", read.hdr.opcode, rc);
  3771. goto fail_cmd;
  3772. }
  3773. return 0;
  3774. }
  3775. fail_cmd:
  3776. return -EINVAL;
  3777. }
  3778. int q6asm_read_nolock(struct audio_client *ac)
  3779. {
  3780. struct asm_data_cmd_read_v2 read;
  3781. struct asm_buffer_node *buf_node = NULL;
  3782. struct list_head *ptr, *next;
  3783. struct audio_buffer *ab;
  3784. int dsp_buf;
  3785. struct audio_port_data *port;
  3786. int rc;
  3787. if (!ac || ac->apr == NULL) {
  3788. pr_err("%s: APR handle NULL\n", __func__);
  3789. return -EINVAL;
  3790. }
  3791. if (ac->io_mode & SYNC_IO_MODE) {
  3792. port = &ac->port[OUT];
  3793. q6asm_add_hdr_async(ac, &read.hdr, sizeof(read), FALSE);
  3794. dsp_buf = port->dsp_buf;
  3795. ab = &port->buf[dsp_buf];
  3796. pr_debug("%s:session[%d]dsp-buf[%d][%p]cpu_buf[%d][%p]\n",
  3797. __func__,
  3798. ac->session,
  3799. dsp_buf,
  3800. (void *)port->buf[dsp_buf].data,
  3801. port->cpu_buf,
  3802. (void *)port->buf[port->cpu_buf].phys);
  3803. read.hdr.opcode = ASM_DATA_CMD_READ_V2;
  3804. read.buf_addr_lsw = ab->phys;
  3805. read.buf_addr_msw = 0;
  3806. read.buf_size = ab->size;
  3807. read.seq_id = port->dsp_buf;
  3808. read.hdr.token = port->dsp_buf;
  3809. list_for_each_safe(ptr, next, &ac->port[OUT].mem_map_handle) {
  3810. buf_node = list_entry(ptr, struct asm_buffer_node,
  3811. list);
  3812. if (buf_node->buf_addr_lsw == (uint32_t)ab->phys) {
  3813. read.mem_map_handle = buf_node->mmap_hdl;
  3814. break;
  3815. }
  3816. }
  3817. port->dsp_buf = (port->dsp_buf + 1) & (port->max_buf_cnt - 1);
  3818. pr_debug("%s:buf add[0x%x] token[%d] uid[%d]\n", __func__,
  3819. read.buf_addr_lsw,
  3820. read.hdr.token,
  3821. read.seq_id);
  3822. rc = apr_send_pkt(ac->apr, (uint32_t *) &read);
  3823. if (rc < 0) {
  3824. pr_err("read op[0x%x]rc[%d]\n", read.hdr.opcode, rc);
  3825. goto fail_cmd;
  3826. }
  3827. return 0;
  3828. }
  3829. fail_cmd:
  3830. return -EINVAL;
  3831. }
  3832. int q6asm_async_write(struct audio_client *ac,
  3833. struct audio_aio_write_param *param)
  3834. {
  3835. int rc = 0;
  3836. struct asm_data_cmd_write_v2 write;
  3837. struct asm_buffer_node *buf_node = NULL;
  3838. struct list_head *ptr, *next;
  3839. struct audio_buffer *ab;
  3840. struct audio_port_data *port;
  3841. u32 lbuf_addr_lsw;
  3842. u32 liomode;
  3843. u32 io_compressed;
  3844. u32 io_compressed_stream;
  3845. if (!ac || ac->apr == NULL) {
  3846. pr_err("%s: APR handle NULL\n", __func__);
  3847. return -EINVAL;
  3848. }
  3849. q6asm_stream_add_hdr_async(
  3850. ac, &write.hdr, sizeof(write), FALSE, ac->stream_id);
  3851. port = &ac->port[IN];
  3852. ab = &port->buf[port->dsp_buf];
  3853. /* Pass physical address as token for AIO scheme */
  3854. write.hdr.token = param->uid;
  3855. write.hdr.opcode = ASM_DATA_CMD_WRITE_V2;
  3856. write.buf_addr_lsw = param->paddr;
  3857. write.buf_addr_msw = 0x00;
  3858. write.buf_size = param->len;
  3859. write.timestamp_msw = param->msw_ts;
  3860. write.timestamp_lsw = param->lsw_ts;
  3861. liomode = (ASYNC_IO_MODE | NT_MODE);
  3862. io_compressed = (ASYNC_IO_MODE | COMPRESSED_IO);
  3863. io_compressed_stream = (ASYNC_IO_MODE | COMPRESSED_STREAM_IO);
  3864. if (ac->io_mode == liomode)
  3865. lbuf_addr_lsw = (write.buf_addr_lsw - 32);
  3866. else if (ac->io_mode == io_compressed ||
  3867. ac->io_mode == io_compressed_stream)
  3868. lbuf_addr_lsw = (write.buf_addr_lsw - param->metadata_len);
  3869. else
  3870. lbuf_addr_lsw = write.buf_addr_lsw;
  3871. pr_debug("%s: token[0x%x], buf_addr_lsw[0x%x], buf_size[0x%x], ts_msw[0x%x], ts_lsw[0x%x], lbuf_addr_lsw: 0x[%x]\n",
  3872. __func__,
  3873. write.hdr.token, write.buf_addr_lsw,
  3874. write.buf_size, write.timestamp_msw,
  3875. write.timestamp_lsw, lbuf_addr_lsw);
  3876. /* Use 0xFF00 for disabling timestamps */
  3877. if (param->flags == 0xFF00)
  3878. write.flags = (0x00000000 | (param->flags & 0x800000FF));
  3879. else
  3880. write.flags = (0x80000000 | param->flags);
  3881. write.flags |= param->last_buffer << ASM_SHIFT_LAST_BUFFER_FLAG;
  3882. write.seq_id = param->uid;
  3883. list_for_each_safe(ptr, next, &ac->port[IN].mem_map_handle) {
  3884. buf_node = list_entry(ptr, struct asm_buffer_node,
  3885. list);
  3886. if (buf_node->buf_addr_lsw == lbuf_addr_lsw) {
  3887. write.mem_map_handle = buf_node->mmap_hdl;
  3888. break;
  3889. }
  3890. }
  3891. rc = apr_send_pkt(ac->apr, (uint32_t *) &write);
  3892. if (rc < 0) {
  3893. pr_debug("[%s] write op[0x%x]rc[%d]\n", __func__,
  3894. write.hdr.opcode, rc);
  3895. goto fail_cmd;
  3896. }
  3897. return 0;
  3898. fail_cmd:
  3899. return -EINVAL;
  3900. }
  3901. int q6asm_async_read(struct audio_client *ac,
  3902. struct audio_aio_read_param *param)
  3903. {
  3904. int rc = 0;
  3905. struct asm_data_cmd_read_v2 read;
  3906. struct asm_buffer_node *buf_node = NULL;
  3907. struct list_head *ptr, *next;
  3908. u32 lbuf_addr_lsw;
  3909. u32 liomode;
  3910. u32 io_compressed;
  3911. int dir = 0;
  3912. if (!ac || ac->apr == NULL) {
  3913. pr_err("%s: APR handle NULL\n", __func__);
  3914. return -EINVAL;
  3915. }
  3916. q6asm_add_hdr_async(ac, &read.hdr, sizeof(read), FALSE);
  3917. /* Pass physical address as token for AIO scheme */
  3918. read.hdr.token = param->paddr;
  3919. read.hdr.opcode = ASM_DATA_CMD_READ_V2;
  3920. read.buf_addr_lsw = param->paddr;
  3921. read.buf_addr_msw = 0;
  3922. read.buf_size = param->len;
  3923. read.seq_id = param->uid;
  3924. liomode = (NT_MODE | ASYNC_IO_MODE);
  3925. io_compressed = (ASYNC_IO_MODE | COMPRESSED_IO);
  3926. if (ac->io_mode == liomode) {
  3927. lbuf_addr_lsw = (read.buf_addr_lsw - 32);
  3928. /*legacy wma driver case*/
  3929. dir = IN;
  3930. } else if (ac->io_mode == io_compressed) {
  3931. lbuf_addr_lsw = (read.buf_addr_lsw - 64);
  3932. dir = OUT;
  3933. } else {
  3934. lbuf_addr_lsw = read.buf_addr_lsw;
  3935. dir = OUT;
  3936. }
  3937. list_for_each_safe(ptr, next, &ac->port[dir].mem_map_handle) {
  3938. buf_node = list_entry(ptr, struct asm_buffer_node,
  3939. list);
  3940. if (buf_node->buf_addr_lsw == lbuf_addr_lsw) {
  3941. read.mem_map_handle = buf_node->mmap_hdl;
  3942. break;
  3943. }
  3944. }
  3945. rc = apr_send_pkt(ac->apr, (uint32_t *) &read);
  3946. if (rc < 0) {
  3947. pr_debug("[%s] read op[0x%x]rc[%d]\n", __func__,
  3948. read.hdr.opcode, rc);
  3949. goto fail_cmd;
  3950. }
  3951. return 0;
  3952. fail_cmd:
  3953. return -EINVAL;
  3954. }
  3955. int q6asm_write(struct audio_client *ac, uint32_t len, uint32_t msw_ts,
  3956. uint32_t lsw_ts, uint32_t flags)
  3957. {
  3958. int rc = 0;
  3959. struct asm_data_cmd_write_v2 write;
  3960. struct asm_buffer_node *buf_node = NULL;
  3961. struct audio_port_data *port;
  3962. struct audio_buffer *ab;
  3963. int dsp_buf = 0;
  3964. if (!ac || ac->apr == NULL) {
  3965. pr_err("%s: APR handle NULL\n", __func__);
  3966. return -EINVAL;
  3967. }
  3968. pr_debug("%s: session[%d] len=%d", __func__, ac->session, len);
  3969. if (ac->io_mode & SYNC_IO_MODE) {
  3970. port = &ac->port[IN];
  3971. q6asm_add_hdr(ac, &write.hdr, sizeof(write),
  3972. FALSE);
  3973. mutex_lock(&port->lock);
  3974. dsp_buf = port->dsp_buf;
  3975. ab = &port->buf[dsp_buf];
  3976. write.hdr.token = port->dsp_buf;
  3977. write.hdr.opcode = ASM_DATA_CMD_WRITE_V2;
  3978. write.buf_addr_lsw = ab->phys;
  3979. write.buf_addr_msw = 0;
  3980. write.buf_size = len;
  3981. write.seq_id = port->dsp_buf;
  3982. write.timestamp_lsw = lsw_ts;
  3983. write.timestamp_msw = msw_ts;
  3984. /* Use 0xFF00 for disabling timestamps */
  3985. if (flags == 0xFF00)
  3986. write.flags = (0x00000000 | (flags & 0x800000FF));
  3987. else
  3988. write.flags = (0x80000000 | flags);
  3989. port->dsp_buf = (port->dsp_buf + 1) & (port->max_buf_cnt - 1);
  3990. buf_node = list_first_entry(&ac->port[IN].mem_map_handle,
  3991. struct asm_buffer_node,
  3992. list);
  3993. write.mem_map_handle = buf_node->mmap_hdl;
  3994. pr_debug("%s:ab->phys[0x%x]bufadd[0x%x] token[0x%x]buf_id[0x%x]buf_size[0x%x]mmaphdl[0x%x]"
  3995. , __func__,
  3996. ab->phys,
  3997. write.buf_addr_lsw,
  3998. write.hdr.token,
  3999. write.seq_id,
  4000. write.buf_size,
  4001. write.mem_map_handle);
  4002. mutex_unlock(&port->lock);
  4003. config_debug_fs_write(ab);
  4004. rc = apr_send_pkt(ac->apr, (uint32_t *) &write);
  4005. if (rc < 0) {
  4006. pr_err("write op[0x%x]rc[%d]\n", write.hdr.opcode, rc);
  4007. goto fail_cmd;
  4008. }
  4009. pr_debug("%s: WRITE SUCCESS\n", __func__);
  4010. return 0;
  4011. }
  4012. fail_cmd:
  4013. return -EINVAL;
  4014. }
  4015. int q6asm_write_nolock(struct audio_client *ac, uint32_t len, uint32_t msw_ts,
  4016. uint32_t lsw_ts, uint32_t flags)
  4017. {
  4018. int rc = 0;
  4019. struct asm_data_cmd_write_v2 write;
  4020. struct asm_buffer_node *buf_node = NULL;
  4021. struct audio_port_data *port;
  4022. struct audio_buffer *ab;
  4023. int dsp_buf = 0;
  4024. if (!ac || ac->apr == NULL) {
  4025. pr_err("%s: APR handle NULL\n", __func__);
  4026. return -EINVAL;
  4027. }
  4028. pr_debug("%s: session[%d] len=%d", __func__, ac->session, len);
  4029. if (ac->io_mode & SYNC_IO_MODE) {
  4030. port = &ac->port[IN];
  4031. q6asm_add_hdr_async(ac, &write.hdr, sizeof(write),
  4032. FALSE);
  4033. dsp_buf = port->dsp_buf;
  4034. ab = &port->buf[dsp_buf];
  4035. write.hdr.token = port->dsp_buf;
  4036. write.hdr.opcode = ASM_DATA_CMD_WRITE_V2;
  4037. write.buf_addr_lsw = ab->phys;
  4038. write.buf_addr_msw = 0;
  4039. write.buf_size = len;
  4040. write.seq_id = port->dsp_buf;
  4041. write.timestamp_lsw = lsw_ts;
  4042. write.timestamp_msw = msw_ts;
  4043. buf_node = list_first_entry(&ac->port[IN].mem_map_handle,
  4044. struct asm_buffer_node,
  4045. list);
  4046. write.mem_map_handle = buf_node->mmap_hdl;
  4047. /* Use 0xFF00 for disabling timestamps */
  4048. if (flags == 0xFF00)
  4049. write.flags = (0x00000000 | (flags & 0x800000FF));
  4050. else
  4051. write.flags = (0x80000000 | flags);
  4052. port->dsp_buf = (port->dsp_buf + 1) & (port->max_buf_cnt - 1);
  4053. pr_debug("%s:ab->phys[0x%x]bufadd[0x%x]token[0x%x] buf_id[0x%x]buf_size[0x%x]mmaphdl[0x%x]"
  4054. , __func__,
  4055. ab->phys,
  4056. write.buf_addr_lsw,
  4057. write.hdr.token,
  4058. write.seq_id,
  4059. write.buf_size,
  4060. write.mem_map_handle);
  4061. rc = apr_send_pkt(ac->apr, (uint32_t *) &write);
  4062. if (rc < 0) {
  4063. pr_err("write op[0x%x]rc[%d]\n", write.hdr.opcode, rc);
  4064. goto fail_cmd;
  4065. }
  4066. pr_debug("%s: WRITE SUCCESS\n", __func__);
  4067. return 0;
  4068. }
  4069. fail_cmd:
  4070. return -EINVAL;
  4071. }
  4072. int q6asm_get_session_time(struct audio_client *ac, uint64_t *tstamp)
  4073. {
  4074. struct apr_hdr hdr;
  4075. int rc;
  4076. if (!ac || ac->apr == NULL || tstamp == NULL) {
  4077. pr_err("%s: APR handle NULL or tstamp NULL\n", __func__);
  4078. return -EINVAL;
  4079. }
  4080. q6asm_add_hdr(ac, &hdr, sizeof(hdr), TRUE);
  4081. hdr.opcode = ASM_SESSION_CMD_GET_SESSIONTIME_V3;
  4082. atomic_set(&ac->time_flag, 1);
  4083. pr_debug("%s: session[%d]opcode[0x%x]\n", __func__,
  4084. ac->session,
  4085. hdr.opcode);
  4086. rc = apr_send_pkt(ac->apr, (uint32_t *) &hdr);
  4087. if (rc < 0) {
  4088. pr_err("Commmand 0x%x failed\n", hdr.opcode);
  4089. goto fail_cmd;
  4090. }
  4091. rc = wait_event_timeout(ac->time_wait,
  4092. (atomic_read(&ac->time_flag) == 0), 5*HZ);
  4093. if (!rc) {
  4094. pr_err("%s: timeout in getting session time from DSP\n",
  4095. __func__);
  4096. goto fail_cmd;
  4097. }
  4098. *tstamp = ac->time_stamp;
  4099. return 0;
  4100. fail_cmd:
  4101. return -EINVAL;
  4102. }
  4103. int q6asm_send_audio_effects_params(struct audio_client *ac, char *params,
  4104. uint32_t params_length)
  4105. {
  4106. char *asm_params = NULL;
  4107. struct apr_hdr hdr;
  4108. struct asm_stream_cmd_set_pp_params_v2 payload_params;
  4109. int sz, rc;
  4110. pr_debug("%s\n", __func__);
  4111. if (!ac || ac->apr == NULL || params == NULL) {
  4112. pr_err("APR handle NULL or params NULL\n");
  4113. return -EINVAL;
  4114. }
  4115. sz = sizeof(struct apr_hdr) +
  4116. sizeof(struct asm_stream_cmd_set_pp_params_v2) +
  4117. params_length;
  4118. asm_params = kzalloc(sz, GFP_KERNEL);
  4119. if (!asm_params) {
  4120. pr_err("%s, adm params memory alloc failed", __func__);
  4121. return -ENOMEM;
  4122. }
  4123. q6asm_add_hdr_async(ac, &hdr, (sizeof(struct apr_hdr) +
  4124. sizeof(struct asm_stream_cmd_set_pp_params_v2) +
  4125. params_length), TRUE);
  4126. atomic_set(&ac->cmd_state, 1);
  4127. hdr.opcode = ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  4128. payload_params.data_payload_addr_lsw = 0;
  4129. payload_params.data_payload_addr_msw = 0;
  4130. payload_params.mem_map_handle = 0;
  4131. payload_params.data_payload_size = params_length;
  4132. memcpy(((u8 *)asm_params), &hdr, sizeof(struct apr_hdr));
  4133. memcpy(((u8 *)asm_params + sizeof(struct apr_hdr)), &payload_params,
  4134. sizeof(struct asm_stream_cmd_set_pp_params_v2));
  4135. memcpy(((u8 *)asm_params + sizeof(struct apr_hdr) +
  4136. sizeof(struct asm_stream_cmd_set_pp_params_v2)),
  4137. params, params_length);
  4138. rc = apr_send_pkt(ac->apr, (uint32_t *) asm_params);
  4139. if (rc < 0) {
  4140. pr_err("%s: audio effects set-params send failed\n", __func__);
  4141. goto fail_send_param;
  4142. }
  4143. rc = wait_event_timeout(ac->cmd_wait,
  4144. (atomic_read(&ac->cmd_state) == 0), 1*HZ);
  4145. if (!rc) {
  4146. pr_err("%s: timeout, audio effects set-params\n", __func__);
  4147. goto fail_send_param;
  4148. }
  4149. rc = 0;
  4150. fail_send_param:
  4151. kfree(asm_params);
  4152. return rc;
  4153. }
  4154. static int __q6asm_cmd(struct audio_client *ac, int cmd, uint32_t stream_id)
  4155. {
  4156. struct apr_hdr hdr;
  4157. int rc;
  4158. atomic_t *state;
  4159. int cnt = 0;
  4160. if (!ac || ac->apr == NULL) {
  4161. pr_err("%s: APR handle NULL\n", __func__);
  4162. return -EINVAL;
  4163. }
  4164. q6asm_stream_add_hdr(ac, &hdr, sizeof(hdr), TRUE, stream_id);
  4165. atomic_set(&ac->cmd_state, 1);
  4166. /*
  4167. * Updated the token field with stream/session for compressed playback
  4168. * Platform driver must know the the stream with which the command is
  4169. * associated
  4170. */
  4171. if (ac->io_mode & COMPRESSED_STREAM_IO)
  4172. hdr.token = ((ac->session << 8) & 0xFFFF00) |
  4173. (stream_id & 0xFF);
  4174. pr_debug("%s: token = 0x%x, stream_id %d, session 0x%x\n",
  4175. __func__, hdr.token, stream_id, ac->session);
  4176. switch (cmd) {
  4177. case CMD_PAUSE:
  4178. pr_debug("%s:CMD_PAUSE\n", __func__);
  4179. hdr.opcode = ASM_SESSION_CMD_PAUSE;
  4180. state = &ac->cmd_state;
  4181. break;
  4182. case CMD_SUSPEND:
  4183. pr_debug("%s:CMD_SUSPEND\n", __func__);
  4184. hdr.opcode = ASM_SESSION_CMD_SUSPEND;
  4185. state = &ac->cmd_state;
  4186. break;
  4187. case CMD_FLUSH:
  4188. pr_debug("%s:CMD_FLUSH\n", __func__);
  4189. hdr.opcode = ASM_STREAM_CMD_FLUSH;
  4190. state = &ac->cmd_state;
  4191. break;
  4192. case CMD_OUT_FLUSH:
  4193. pr_debug("%s:CMD_OUT_FLUSH\n", __func__);
  4194. hdr.opcode = ASM_STREAM_CMD_FLUSH_READBUFS;
  4195. state = &ac->cmd_state;
  4196. break;
  4197. case CMD_EOS:
  4198. pr_debug("%s:CMD_EOS\n", __func__);
  4199. hdr.opcode = ASM_DATA_CMD_EOS;
  4200. atomic_set(&ac->cmd_state, 0);
  4201. state = &ac->cmd_state;
  4202. break;
  4203. case CMD_CLOSE:
  4204. pr_debug("%s:CMD_CLOSE\n", __func__);
  4205. hdr.opcode = ASM_STREAM_CMD_CLOSE;
  4206. state = &ac->cmd_state;
  4207. break;
  4208. default:
  4209. pr_err("Invalid format[%d]\n", cmd);
  4210. goto fail_cmd;
  4211. }
  4212. pr_debug("%s:session[%d]opcode[0x%x] ", __func__,
  4213. ac->session,
  4214. hdr.opcode);
  4215. rc = apr_send_pkt(ac->apr, (uint32_t *) &hdr);
  4216. if (rc < 0) {
  4217. pr_err("Commmand 0x%x failed\n", hdr.opcode);
  4218. goto fail_cmd;
  4219. }
  4220. rc = wait_event_timeout(ac->cmd_wait, (atomic_read(state) == 0), 5*HZ);
  4221. if (!rc) {
  4222. pr_err("timeout. waited for response opcode[0x%x]\n",
  4223. hdr.opcode);
  4224. goto fail_cmd;
  4225. }
  4226. if (cmd == CMD_FLUSH)
  4227. q6asm_reset_buf_state(ac);
  4228. if (cmd == CMD_CLOSE) {
  4229. /* check if DSP return all buffers */
  4230. if (ac->port[IN].buf) {
  4231. for (cnt = 0; cnt < ac->port[IN].max_buf_cnt;
  4232. cnt++) {
  4233. if (ac->port[IN].buf[cnt].used == IN) {
  4234. pr_debug("Write Buf[%d] not returned\n",
  4235. cnt);
  4236. }
  4237. }
  4238. }
  4239. if (ac->port[OUT].buf) {
  4240. for (cnt = 0; cnt < ac->port[OUT].max_buf_cnt; cnt++) {
  4241. if (ac->port[OUT].buf[cnt].used == OUT) {
  4242. pr_debug("Read Buf[%d] not returned\n",
  4243. cnt);
  4244. }
  4245. }
  4246. }
  4247. }
  4248. return 0;
  4249. fail_cmd:
  4250. return -EINVAL;
  4251. }
  4252. int q6asm_cmd(struct audio_client *ac, int cmd)
  4253. {
  4254. return __q6asm_cmd(ac, cmd, ac->stream_id);
  4255. }
  4256. int q6asm_stream_cmd(struct audio_client *ac, int cmd, uint32_t stream_id)
  4257. {
  4258. return __q6asm_cmd(ac, cmd, stream_id);
  4259. }
  4260. static int __q6asm_cmd_nowait(struct audio_client *ac, int cmd,
  4261. uint32_t stream_id)
  4262. {
  4263. struct apr_hdr hdr;
  4264. int rc;
  4265. if (!ac || ac->apr == NULL) {
  4266. pr_err("%s:APR handle NULL\n", __func__);
  4267. return -EINVAL;
  4268. }
  4269. q6asm_stream_add_hdr_async(ac, &hdr, sizeof(hdr), TRUE, stream_id);
  4270. atomic_set(&ac->cmd_state, 1);
  4271. /*
  4272. * Updated the token field with stream/session for compressed playback
  4273. * Platform driver must know the the stream with which the command is
  4274. * associated
  4275. */
  4276. if (ac->io_mode & COMPRESSED_STREAM_IO)
  4277. hdr.token = ((ac->session << 8) & 0xFFFF00) |
  4278. (stream_id & 0xFF);
  4279. pr_debug("%s: token = 0x%x, stream_id %d, session 0x%x\n",
  4280. __func__, hdr.token, stream_id, ac->session);
  4281. switch (cmd) {
  4282. case CMD_PAUSE:
  4283. pr_debug("%s:CMD_PAUSE\n", __func__);
  4284. hdr.opcode = ASM_SESSION_CMD_PAUSE;
  4285. break;
  4286. case CMD_EOS:
  4287. pr_debug("%s:CMD_EOS\n", __func__);
  4288. hdr.opcode = ASM_DATA_CMD_EOS;
  4289. break;
  4290. case CMD_CLOSE:
  4291. pr_debug("%s:CMD_CLOSE\n", __func__);
  4292. hdr.opcode = ASM_STREAM_CMD_CLOSE;
  4293. break;
  4294. default:
  4295. pr_err("%s:Invalid format[%d]\n", __func__, cmd);
  4296. goto fail_cmd;
  4297. }
  4298. pr_debug("%s:session[%d]opcode[0x%x] ", __func__,
  4299. ac->session,
  4300. hdr.opcode);
  4301. /* have to increase first avoid race */
  4302. atomic_inc(&ac->nowait_cmd_cnt);
  4303. rc = apr_send_pkt(ac->apr, (uint32_t *) &hdr);
  4304. if (rc < 0) {
  4305. atomic_dec(&ac->nowait_cmd_cnt);
  4306. pr_err("%s:Commmand 0x%x failed\n", __func__, hdr.opcode);
  4307. goto fail_cmd;
  4308. }
  4309. return 0;
  4310. fail_cmd:
  4311. return -EINVAL;
  4312. }
  4313. int q6asm_cmd_nowait(struct audio_client *ac, int cmd)
  4314. {
  4315. pr_debug("%s: stream_id: %d\n", __func__, ac->stream_id);
  4316. return __q6asm_cmd_nowait(ac, cmd, ac->stream_id);
  4317. }
  4318. int q6asm_stream_cmd_nowait(struct audio_client *ac, int cmd,
  4319. uint32_t stream_id)
  4320. {
  4321. pr_debug("%s: stream_id: %d\n", __func__, stream_id);
  4322. return __q6asm_cmd_nowait(ac, cmd, stream_id);
  4323. }
  4324. int __q6asm_send_meta_data(struct audio_client *ac, uint32_t stream_id,
  4325. uint32_t initial_samples, uint32_t trailing_samples)
  4326. {
  4327. struct asm_data_cmd_remove_silence silence;
  4328. int rc = 0;
  4329. if (!ac || ac->apr == NULL) {
  4330. pr_err("APR handle NULL\n");
  4331. return -EINVAL;
  4332. }
  4333. pr_debug("%s session[%d]", __func__, ac->session);
  4334. q6asm_stream_add_hdr_async(ac, &silence.hdr, sizeof(silence), FALSE,
  4335. stream_id);
  4336. /*
  4337. * Updated the token field with stream/session for compressed playback
  4338. * Platform driver must know the the stream with which the command is
  4339. * associated
  4340. */
  4341. if (ac->io_mode & COMPRESSED_STREAM_IO)
  4342. silence.hdr.token = ((ac->session << 8) & 0xFFFF00) |
  4343. (stream_id & 0xFF);
  4344. pr_debug("%s: token = 0x%x, stream_id %d, session 0x%x\n",
  4345. __func__, silence.hdr.token, stream_id, ac->session);
  4346. silence.hdr.opcode = ASM_DATA_CMD_REMOVE_INITIAL_SILENCE;
  4347. silence.num_samples_to_remove = initial_samples;
  4348. rc = apr_send_pkt(ac->apr, (uint32_t *) &silence);
  4349. if (rc < 0) {
  4350. pr_err("Commmand silence failed[%d]", rc);
  4351. goto fail_cmd;
  4352. }
  4353. silence.hdr.opcode = ASM_DATA_CMD_REMOVE_TRAILING_SILENCE;
  4354. silence.num_samples_to_remove = trailing_samples;
  4355. rc = apr_send_pkt(ac->apr, (uint32_t *) &silence);
  4356. if (rc < 0) {
  4357. pr_err("Commmand silence failed[%d]", rc);
  4358. goto fail_cmd;
  4359. }
  4360. return 0;
  4361. fail_cmd:
  4362. return -EINVAL;
  4363. }
  4364. int q6asm_stream_send_meta_data(struct audio_client *ac, uint32_t stream_id,
  4365. uint32_t initial_samples, uint32_t trailing_samples)
  4366. {
  4367. return __q6asm_send_meta_data(ac, stream_id, initial_samples,
  4368. trailing_samples);
  4369. }
  4370. int q6asm_send_meta_data(struct audio_client *ac, uint32_t initial_samples,
  4371. uint32_t trailing_samples)
  4372. {
  4373. return __q6asm_send_meta_data(ac, ac->stream_id, initial_samples,
  4374. trailing_samples);
  4375. }
  4376. static void q6asm_reset_buf_state(struct audio_client *ac)
  4377. {
  4378. int cnt = 0;
  4379. int loopcnt = 0;
  4380. int used;
  4381. struct audio_port_data *port = NULL;
  4382. if (ac->io_mode & SYNC_IO_MODE) {
  4383. used = (ac->io_mode & TUN_WRITE_IO_MODE ? 1 : 0);
  4384. mutex_lock(&ac->cmd_lock);
  4385. for (loopcnt = 0; loopcnt <= OUT; loopcnt++) {
  4386. port = &ac->port[loopcnt];
  4387. cnt = port->max_buf_cnt - 1;
  4388. port->dsp_buf = 0;
  4389. port->cpu_buf = 0;
  4390. while (cnt >= 0) {
  4391. if (!port->buf)
  4392. continue;
  4393. port->buf[cnt].used = used;
  4394. cnt--;
  4395. }
  4396. }
  4397. mutex_unlock(&ac->cmd_lock);
  4398. }
  4399. }
  4400. int q6asm_reg_tx_overflow(struct audio_client *ac, uint16_t enable)
  4401. {
  4402. struct asm_session_cmd_regx_overflow tx_overflow;
  4403. int rc;
  4404. if (!ac || ac->apr == NULL) {
  4405. pr_err("%s: APR handle NULL\n", __func__);
  4406. return -EINVAL;
  4407. }
  4408. pr_debug("%s:session[%d]enable[%d]\n", __func__,
  4409. ac->session, enable);
  4410. q6asm_add_hdr(ac, &tx_overflow.hdr, sizeof(tx_overflow), TRUE);
  4411. atomic_set(&ac->cmd_state, 1);
  4412. tx_overflow.hdr.opcode = \
  4413. ASM_SESSION_CMD_REGISTER_FORX_OVERFLOW_EVENTS;
  4414. /* tx overflow event: enable */
  4415. tx_overflow.enable_flag = enable;
  4416. rc = apr_send_pkt(ac->apr, (uint32_t *) &tx_overflow);
  4417. if (rc < 0) {
  4418. pr_err("tx overflow op[0x%x]rc[%d]\n", \
  4419. tx_overflow.hdr.opcode, rc);
  4420. goto fail_cmd;
  4421. }
  4422. rc = wait_event_timeout(ac->cmd_wait,
  4423. (atomic_read(&ac->cmd_state) == 0), 5*HZ);
  4424. if (!rc) {
  4425. pr_err("timeout. waited for tx overflow\n");
  4426. goto fail_cmd;
  4427. }
  4428. return 0;
  4429. fail_cmd:
  4430. return -EINVAL;
  4431. }
  4432. int q6asm_reg_rx_underflow(struct audio_client *ac, uint16_t enable)
  4433. {
  4434. struct asm_session_cmd_rgstr_rx_underflow rx_underflow;
  4435. int rc;
  4436. if (!ac || ac->apr == NULL) {
  4437. pr_err("APR handle NULL\n");
  4438. return -EINVAL;
  4439. }
  4440. pr_debug("%s:session[%d]enable[%d]\n", __func__,
  4441. ac->session, enable);
  4442. q6asm_add_hdr_async(ac, &rx_underflow.hdr, sizeof(rx_underflow), FALSE);
  4443. rx_underflow.hdr.opcode = \
  4444. ASM_SESSION_CMD_REGISTER_FOR_RX_UNDERFLOW_EVENTS;
  4445. /* tx overflow event: enable */
  4446. rx_underflow.enable_flag = enable;
  4447. rc = apr_send_pkt(ac->apr, (uint32_t *) &rx_underflow);
  4448. if (rc < 0) {
  4449. pr_err("tx overflow op[0x%x]rc[%d]\n", \
  4450. rx_underflow.hdr.opcode, rc);
  4451. goto fail_cmd;
  4452. }
  4453. return 0;
  4454. fail_cmd:
  4455. return -EINVAL;
  4456. }
  4457. int q6asm_get_apr_service_id(int session_id)
  4458. {
  4459. pr_debug("%s\n", __func__);
  4460. if (session_id <= 0 || session_id > SESSION_MAX) {
  4461. pr_err("%s: invalid session_id = %d\n", __func__, session_id);
  4462. return -EINVAL;
  4463. }
  4464. return ((struct apr_svc *)session[session_id]->apr)->id;
  4465. }
  4466. static int q6asm_is_valid_session(struct apr_client_data *data, void *priv)
  4467. {
  4468. struct audio_client *ac = (struct audio_client *)priv;
  4469. uint32_t token = data->token;
  4470. /*
  4471. * Some commands for compressed playback has token as session and
  4472. * other commands has session|stream. Check for both conditions
  4473. * before deciding if the callback was for a invalud session.
  4474. */
  4475. if (ac->io_mode & COMPRESSED_STREAM_IO) {
  4476. if ((token & 0xFFFFFF00) != ((ac->session << 8) & 0xFFFFFF00)
  4477. && (token != ac->session)) {
  4478. pr_err("%s:Invalid compr session[%d] rxed expected[%d]",
  4479. __func__, token, ac->session);
  4480. return -EINVAL;
  4481. }
  4482. } else {
  4483. if (token != ac->session) {
  4484. pr_err("%s:Invalid session[%d] rxed expected[%d]",
  4485. __func__, token, ac->session);
  4486. return -EINVAL;
  4487. }
  4488. }
  4489. return 0;
  4490. }
  4491. static int __init q6asm_init(void)
  4492. {
  4493. int lcnt;
  4494. pr_debug("%s\n", __func__);
  4495. memset(session, 0, sizeof(session));
  4496. set_custom_topology = 1;
  4497. /*setup common client used for cal mem map */
  4498. common_client.session = ASM_CONTROL_SESSION;
  4499. common_client.port[0].buf = &common_buf[0];
  4500. common_client.port[1].buf = &common_buf[1];
  4501. init_waitqueue_head(&common_client.cmd_wait);
  4502. init_waitqueue_head(&common_client.time_wait);
  4503. init_waitqueue_head(&common_client.mem_wait);
  4504. atomic_set(&common_client.time_flag, 1);
  4505. INIT_LIST_HEAD(&common_client.port[0].mem_map_handle);
  4506. INIT_LIST_HEAD(&common_client.port[1].mem_map_handle);
  4507. mutex_init(&common_client.cmd_lock);
  4508. for (lcnt = 0; lcnt <= OUT; lcnt++) {
  4509. mutex_init(&common_client.port[lcnt].lock);
  4510. spin_lock_init(&common_client.port[lcnt].dsp_lock);
  4511. }
  4512. atomic_set(&common_client.cmd_state, 0);
  4513. atomic_set(&common_client.nowait_cmd_cnt, 0);
  4514. atomic_set(&common_client.mem_state, 0);
  4515. config_debug_fs_init();
  4516. return 0;
  4517. }
  4518. device_initcall(q6asm_init);