q6asm.c 300 KB

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