q6asm.c 307 KB

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