q6asm.c 275 KB

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