q6asm.c 308 KB

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