q6asm.c 277 KB

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