q6asm.c 276 KB

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