q6asm.c 277 KB

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