q6asm.c 275 KB

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