q6asm.c 307 KB

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