q6asm.c 134 KB

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