q6asm.c 275 KB

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