q6asm.c 278 KB

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