q6asm.c 305 KB

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