mixer_scarlett2.c 253 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Focusrite Scarlett 2 Protocol Driver for ALSA
  4. * (including Scarlett 2nd Gen, 3rd Gen, 4th Gen, Clarett USB, and
  5. * Clarett+ series products)
  6. *
  7. * Supported models:
  8. * - 6i6/18i8/18i20 Gen 2
  9. * - Solo/2i2/4i4/8i6/18i8/18i20 Gen 3
  10. * - Solo/2i2/4i4 Gen 4
  11. * - Clarett 2Pre/4Pre/8Pre USB
  12. * - Clarett+ 2Pre/4Pre/8Pre
  13. *
  14. * Copyright (c) 2018-2023 by Geoffrey D. Bennett <g at b4.vu>
  15. * Copyright (c) 2020-2021 by Vladimir Sadovnikov <sadko4u@gmail.com>
  16. * Copyright (c) 2022 by Christian Colglazier <christian@cacolglazier.com>
  17. *
  18. * Based on the Scarlett (Gen 1) Driver for ALSA:
  19. *
  20. * Copyright (c) 2013 by Tobias Hoffmann
  21. * Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
  22. * Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
  23. * Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
  24. *
  25. * Many codes borrowed from audio.c by
  26. * Alan Cox (alan at lxorguk.ukuu.org.uk)
  27. * Thomas Sailer (sailer at ife.ee.ethz.ch)
  28. *
  29. * Code cleanup:
  30. * David Henningsson <david.henningsson at canonical.com>
  31. */
  32. /* The protocol was reverse engineered by looking at the communication
  33. * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20
  34. * (firmware 1083) using usbmon in July-August 2018.
  35. *
  36. * Scarlett 18i8 support added in April 2019.
  37. *
  38. * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
  39. * for providing usbmon output and testing).
  40. *
  41. * Scarlett 4i4/8i6 Gen 3 support added in May 2020 (thanks to Laurent
  42. * Debricon for donating a 4i4 and to Fredrik Unger for providing 8i6
  43. * usbmon output and testing).
  44. *
  45. * Scarlett 18i8/18i20 Gen 3 support added in June 2020 (thanks to
  46. * Darren Jaeckel, Alex Sedlack, and Clovis Lunel for providing usbmon
  47. * output, protocol traces and testing).
  48. *
  49. * Support for loading mixer volume and mux configuration from the
  50. * interface during driver initialisation added in May 2021 (thanks to
  51. * Vladimir Sadovnikov for figuring out how).
  52. *
  53. * Support for Solo/2i2 Gen 3 added in May 2021 (thanks to Alexander
  54. * Vorona for 2i2 protocol traces).
  55. *
  56. * Support for phantom power, direct monitoring, speaker switching,
  57. * and talkback added in May-June 2021.
  58. *
  59. * Support for Clarett+ 8Pre added in Aug 2022 by Christian
  60. * Colglazier.
  61. *
  62. * Support for Clarett 8Pre USB added in Sep 2023 (thanks to Philippe
  63. * Perrot for confirmation).
  64. *
  65. * Support for Clarett+ 4Pre and 2Pre added in Sep 2023 (thanks to
  66. * Gregory Rozzo for donating a 4Pre, and David Sherwood and Patrice
  67. * Peterson for usbmon output).
  68. *
  69. * Support for Clarett 2Pre and 4Pre USB added in Oct 2023.
  70. *
  71. * Support for firmware updates added in Dec 2023.
  72. *
  73. * Support for Scarlett Solo/2i2/4i4 Gen 4 added in Dec 2023 (thanks
  74. * to many LinuxMusicians people and to Focusrite for hardware
  75. * donations).
  76. *
  77. * This ALSA mixer gives access to (model-dependent):
  78. * - input, output, mixer-matrix muxes
  79. * - mixer-matrix gain stages
  80. * - gain/volume/mute controls
  81. * - level meters
  82. * - line/inst level, pad, and air controls
  83. * - phantom power, direct monitor, speaker switching, and talkback
  84. * controls
  85. * - disable/enable MSD mode
  86. * - disable/enable standalone mode
  87. * - input mute, gain, autogain, safe mode
  88. * - direct monitor mixes
  89. * - compressor and EQ
  90. * - Bluetooth volume
  91. *
  92. * <ditaa>
  93. * /--------------\ 18chn 20chn /--------------\
  94. * | Hardware in +--+------\ /-------------+--+ ALSA PCM out |
  95. * \--------------/ | | | | \--------------/
  96. * | | | /-----\ |
  97. * | | | | | |
  98. * | v v v | |
  99. * | +---------------+ | |
  100. * | \ Matrix Mux / | |
  101. * | +-----+-----+ | |
  102. * | | | |
  103. * | |18chn | |
  104. * | | | |
  105. * | | 10chn| |
  106. * | v | |
  107. * | +------------+ | |
  108. * | | Mixer | | |
  109. * | | Matrix | | |
  110. * | | | | |
  111. * | | 18x10 Gain | | |
  112. * | | stages | | |
  113. * | +-----+------+ | |
  114. * | | | |
  115. * |18chn |10chn | |20chn
  116. * | | | |
  117. * | +----------/ |
  118. * | | |
  119. * v v v
  120. * ===========================
  121. * +---------------+ +--—------------+
  122. * \ Output Mux / \ Capture Mux /
  123. * +---+---+---+ +-----+-----+
  124. * | | |
  125. * 10chn| | |18chn
  126. * | | |
  127. * /--------------\ | | | /--------------\
  128. * | S/PDIF, ADAT |<--/ |10chn \-->| ALSA PCM in |
  129. * | Hardware out | | \--------------/
  130. * \--------------/ |
  131. * v
  132. * +-------------+ Software gain per channel.
  133. * | Master Gain |<-- 18i20 only: Switch per channel
  134. * +------+------+ to select HW or SW gain control.
  135. * |
  136. * |10chn
  137. * /--------------\ |
  138. * | Analogue |<------/
  139. * | Hardware out |
  140. * \--------------/
  141. * </ditaa>
  142. *
  143. * Gen 3/4 devices have a Mass Storage Device (MSD) mode where a small
  144. * disk with registration and driver download information is presented
  145. * to the host. To access the full functionality of the device without
  146. * proprietary software, MSD mode can be disabled by:
  147. * - holding down the 48V button for five seconds while powering on
  148. * the device, or
  149. * - using this driver and alsamixer to change the "MSD Mode" setting
  150. * to Off and power-cycling the device
  151. */
  152. #include <linux/slab.h>
  153. #include <linux/usb.h>
  154. #include <linux/moduleparam.h>
  155. #include <sound/control.h>
  156. #include <sound/tlv.h>
  157. #include <sound/hwdep.h>
  158. #include <uapi/sound/scarlett2.h>
  159. #include "usbaudio.h"
  160. #include "mixer.h"
  161. #include "helper.h"
  162. #include "mixer_scarlett2.h"
  163. /* device_setup value to allow turning MSD mode back on */
  164. #define SCARLETT2_MSD_ENABLE 0x02
  165. /* device_setup value to disable this mixer driver */
  166. #define SCARLETT2_DISABLE 0x04
  167. /* some gui mixers can't handle negative ctl values */
  168. #define SCARLETT2_VOLUME_BIAS 127
  169. /* maximum preamp input gain value
  170. * (the corresponding value in dB is per-device)
  171. */
  172. #define SCARLETT2_MAX_GAIN_VALUE 70
  173. /* maximum Bluetooth volume value */
  174. #define SCARLETT2_MAX_BLUETOOTH_VOLUME 30
  175. /* mixer range from -80dB to +12dB in 0.5dB steps */
  176. #define SCARLETT2_MIXER_MIN_DB -80
  177. #define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
  178. #define SCARLETT2_MIXER_MAX_DB 12
  179. #define SCARLETT2_MIXER_MAX_VALUE \
  180. ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
  181. #define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1)
  182. /* map from (dB + 80) * 2 to mixer value
  183. * for dB in 0 .. 184: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
  184. */
  185. static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = {
  186. 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
  187. 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
  188. 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
  189. 23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
  190. 54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
  191. 122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
  192. 244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
  193. 487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
  194. 973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
  195. 1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
  196. 3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
  197. 5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
  198. 9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
  199. 16345, 17313, 18339, 19426, 20577, 21796, 23088, 24456, 25905,
  200. 27440, 29066, 30788, 32612
  201. };
  202. /* Maximum number of analogue outputs */
  203. #define SCARLETT2_ANALOGUE_MAX 10
  204. /* Maximum number of various input controls */
  205. #define SCARLETT2_LEVEL_SWITCH_MAX 2
  206. #define SCARLETT2_PAD_SWITCH_MAX 8
  207. #define SCARLETT2_AIR_SWITCH_MAX 8
  208. #define SCARLETT2_DSP_SWITCH_MAX 2
  209. #define SCARLETT2_INPUT_MUTE_SWITCH_MAX 2
  210. #define SCARLETT2_PHANTOM_SWITCH_MAX 2
  211. #define SCARLETT2_INPUT_GAIN_MAX 2
  212. /* Maximum number of inputs to the mixer */
  213. #define SCARLETT2_INPUT_MIX_MAX 25
  214. /* Maximum number of outputs from the mixer */
  215. #define SCARLETT2_OUTPUT_MIX_MAX 12
  216. /* Maximum number of mixer gain controls */
  217. #define SCARLETT2_MIX_MAX (SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX)
  218. /* Maximum number of direct monitor mixer gain controls
  219. * 1 (Solo) or 2 (2i2) direct monitor selections (Mono & Stereo)
  220. * 2 Mix outputs (A/Left & B/Right)
  221. * 4 Mix inputs
  222. */
  223. #define SCARLETT2_MONITOR_MIX_MAX (2 * 2 * 4)
  224. /* Maximum size of the data in the USB mux assignment message:
  225. * 20 inputs, 20 outputs, 25 matrix inputs, 12 spare
  226. */
  227. #define SCARLETT2_MUX_MAX 77
  228. /* Maximum number of sources (sum of input port counts) */
  229. #define SCARLETT2_MAX_SRCS 52
  230. /* Maximum number of meters (sum of output port counts) */
  231. #define SCARLETT2_MAX_METERS 65
  232. /* Compressor parameter data
  233. *
  234. * The compressor parameters are 32-bit fixed point values with 24
  235. * bits of fraction. Integer values are sufficient for the parameters
  236. * except for ratio which we can set in 0.5:1 steps.
  237. */
  238. struct compressor_param {
  239. const char *name;
  240. snd_ctl_elem_type_t type;
  241. s32 min;
  242. s32 max;
  243. int scale_bits;
  244. };
  245. /* The available compressor parameters on the Vocaster:
  246. * - Enable: Off, On
  247. * - Threshold: -40dB to 0dB
  248. * - Ratio: 1:1 to 50:1 in 0.5:1 steps
  249. * - Knee Width: 0dB to 10dB
  250. * - Attack: 30ms to 127ms
  251. * - Release: 30ms to 127ms
  252. * - Makeup Gain: 0dB to 24dB
  253. */
  254. static const struct compressor_param compressor_params[] = {
  255. { "Enable", SNDRV_CTL_ELEM_TYPE_BOOLEAN, 0, 1, 0 },
  256. { "Threshold", SNDRV_CTL_ELEM_TYPE_INTEGER, -40, 0, 24 },
  257. { "Ratio", SNDRV_CTL_ELEM_TYPE_INTEGER, 2, 100, 23 },
  258. { "Knee Width", SNDRV_CTL_ELEM_TYPE_INTEGER, 0, 10, 24 },
  259. { "Attack", SNDRV_CTL_ELEM_TYPE_INTEGER, 30, 127, 24 },
  260. { "Release", SNDRV_CTL_ELEM_TYPE_INTEGER, 30, 127, 24 },
  261. { "Makeup Gain", SNDRV_CTL_ELEM_TYPE_INTEGER, 0, 24, 24 },
  262. };
  263. #define SCARLETT2_COMPRESSOR_PARAM_COUNT ARRAY_SIZE(compressor_params)
  264. #define SCARLETT2_COMPRESSOR_CTLS_MAX \
  265. (SCARLETT2_COMPRESSOR_PARAM_COUNT * SCARLETT2_DSP_SWITCH_MAX)
  266. /* Maximum number of filter controls */
  267. #define SCARLETT2_PRECOMP_FLT_CTLS_MAX (2 * SCARLETT2_DSP_SWITCH_MAX)
  268. #define SCARLETT2_PEQ_FLT_CTLS_MAX (3 * SCARLETT2_DSP_SWITCH_MAX)
  269. /* Number of biquad filter coefficients */
  270. #define SCARLETT2_BIQUAD_COEFFS 5
  271. /* Maximum number of filter coefficient values */
  272. #define SCARLETT2_PRECOMP_FLT_VALUES_MAX \
  273. (SCARLETT2_PRECOMP_FLT_CTLS_MAX * SCARLETT2_BIQUAD_COEFFS)
  274. #define SCARLETT2_PEQ_FLT_VALUES_MAX \
  275. (SCARLETT2_PEQ_FLT_CTLS_MAX * SCARLETT2_BIQUAD_COEFFS)
  276. /* Maximum number of PEQ filter slots */
  277. #define SCARLETT2_PEQ_FLT_SLOTS_MAX 4
  278. /* Hardware port types:
  279. * - None (no input to mux)
  280. * - Analogue I/O
  281. * - S/PDIF I/O
  282. * - ADAT I/O
  283. * - Mixer I/O
  284. * - PCM I/O
  285. */
  286. enum {
  287. SCARLETT2_PORT_TYPE_NONE,
  288. SCARLETT2_PORT_TYPE_ANALOGUE,
  289. SCARLETT2_PORT_TYPE_SPDIF,
  290. SCARLETT2_PORT_TYPE_ADAT,
  291. SCARLETT2_PORT_TYPE_MIX,
  292. SCARLETT2_PORT_TYPE_PCM,
  293. SCARLETT2_PORT_TYPE_COUNT
  294. };
  295. /* I/O count of each port type kept in struct scarlett2_ports */
  296. enum {
  297. SCARLETT2_PORT_IN,
  298. SCARLETT2_PORT_OUT,
  299. SCARLETT2_PORT_DIRNS
  300. };
  301. /* Dim/Mute buttons on the 18i20 */
  302. enum {
  303. SCARLETT2_BUTTON_MUTE,
  304. SCARLETT2_BUTTON_DIM,
  305. SCARLETT2_DIM_MUTE_COUNT
  306. };
  307. /* Autogain target values */
  308. #define SCARLETT2_AG_TARGET_MIN (-30)
  309. enum {
  310. SCARLETT2_AG_HOT_TARGET,
  311. SCARLETT2_AG_MEAN_TARGET,
  312. SCARLETT2_AG_PEAK_TARGET,
  313. SCARLETT2_AG_TARGET_COUNT
  314. };
  315. /* Flash Write State */
  316. enum {
  317. SCARLETT2_FLASH_WRITE_STATE_IDLE,
  318. SCARLETT2_FLASH_WRITE_STATE_SELECTED,
  319. SCARLETT2_FLASH_WRITE_STATE_ERASING,
  320. SCARLETT2_FLASH_WRITE_STATE_WRITE
  321. };
  322. static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
  323. "Mute Playback Switch", "Dim Playback Switch"
  324. };
  325. /* The autogain_status is set based on the autogain_switch and
  326. * raw_autogain_status values.
  327. *
  328. * If autogain_switch is set, autogain_status is set to 0 (Running).
  329. * The other status values are from the raw_autogain_status value + 1.
  330. */
  331. static const char *const scarlett2_autogain_status_gen4[] = {
  332. "Running",
  333. "Success",
  334. "SuccessDRover",
  335. "WarnMinGainLimit",
  336. "FailDRunder",
  337. "FailMaxGainLimit",
  338. "FailClipped",
  339. "Cancelled",
  340. "Invalid",
  341. NULL
  342. };
  343. static const char *const scarlett2_autogain_status_vocaster[] = {
  344. "Running",
  345. "Success",
  346. "FailPG",
  347. "FailRange",
  348. "WarnMaxCap",
  349. "WarnMinCap",
  350. "Cancelled",
  351. "Invalid",
  352. NULL
  353. };
  354. /* Power Status Values */
  355. enum {
  356. SCARLETT2_POWER_STATUS_EXT,
  357. SCARLETT2_POWER_STATUS_BUS,
  358. SCARLETT2_POWER_STATUS_FAIL,
  359. SCARLETT2_POWER_STATUS_COUNT
  360. };
  361. /* Notification callback functions */
  362. struct scarlett2_notification {
  363. u32 mask;
  364. void (*func)(struct usb_mixer_interface *mixer);
  365. };
  366. static void scarlett2_notify_ack(struct usb_mixer_interface *mixer);
  367. static void scarlett2_notify_sync(struct usb_mixer_interface *mixer);
  368. static void scarlett2_notify_dim_mute(struct usb_mixer_interface *mixer);
  369. static void scarlett2_notify_monitor(struct usb_mixer_interface *mixer);
  370. static void scarlett2_notify_volume(struct usb_mixer_interface *mixer);
  371. static void scarlett2_notify_input_level(struct usb_mixer_interface *mixer);
  372. static void scarlett2_notify_input_pad(struct usb_mixer_interface *mixer);
  373. static void scarlett2_notify_input_air(struct usb_mixer_interface *mixer);
  374. static void scarlett2_notify_input_dsp(struct usb_mixer_interface *mixer);
  375. static void scarlett2_notify_input_mute(struct usb_mixer_interface *mixer);
  376. static void scarlett2_notify_input_phantom(struct usb_mixer_interface *mixer);
  377. static void scarlett2_notify_input_other(struct usb_mixer_interface *mixer);
  378. static void scarlett2_notify_input_select(struct usb_mixer_interface *mixer);
  379. static void scarlett2_notify_input_gain(struct usb_mixer_interface *mixer);
  380. static void scarlett2_notify_autogain(struct usb_mixer_interface *mixer);
  381. static void scarlett2_notify_input_safe(struct usb_mixer_interface *mixer);
  382. static void scarlett2_notify_monitor_other(struct usb_mixer_interface *mixer);
  383. static void scarlett2_notify_direct_monitor(struct usb_mixer_interface *mixer);
  384. static void scarlett2_notify_power_status(struct usb_mixer_interface *mixer);
  385. static void scarlett2_notify_pcm_input_switch(
  386. struct usb_mixer_interface *mixer);
  387. static void scarlett2_notify_bluetooth(struct usb_mixer_interface *mixer);
  388. /* Arrays of notification callback functions */
  389. static const struct scarlett2_notification scarlett2_notifications[] = {
  390. { 0x00000001, scarlett2_notify_ack },
  391. { 0x00000008, scarlett2_notify_sync },
  392. { 0x00200000, scarlett2_notify_dim_mute },
  393. { 0x00400000, scarlett2_notify_monitor },
  394. { 0x00800000, scarlett2_notify_input_other },
  395. { 0x01000000, scarlett2_notify_monitor_other },
  396. { 0, NULL }
  397. };
  398. static const struct scarlett2_notification scarlett3a_notifications[] = {
  399. { 0x00000001, scarlett2_notify_ack },
  400. { 0x00800000, scarlett2_notify_input_other },
  401. { 0x01000000, scarlett2_notify_direct_monitor },
  402. { 0, NULL }
  403. };
  404. static const struct scarlett2_notification vocaster_notifications[] = {
  405. { 0x00000001, scarlett2_notify_ack },
  406. { 0x00000008, scarlett2_notify_sync },
  407. { 0x00200000, scarlett2_notify_input_mute },
  408. { 0x00400000, scarlett2_notify_autogain },
  409. { 0x04000000, scarlett2_notify_input_dsp },
  410. { 0x08000000, scarlett2_notify_input_gain },
  411. { 0x10000000, scarlett2_notify_input_phantom },
  412. { 0x20000000, scarlett2_notify_bluetooth },
  413. { 0, NULL }
  414. };
  415. static const struct scarlett2_notification scarlett4_solo_notifications[] = {
  416. { 0x00000001, scarlett2_notify_ack },
  417. { 0x00000008, scarlett2_notify_sync },
  418. { 0x00400000, scarlett2_notify_input_air },
  419. { 0x00800000, scarlett2_notify_direct_monitor },
  420. { 0x01000000, scarlett2_notify_input_level },
  421. { 0x02000000, scarlett2_notify_input_phantom },
  422. { 0x04000000, scarlett2_notify_pcm_input_switch },
  423. { 0, NULL }
  424. };
  425. static const struct scarlett2_notification scarlett4_2i2_notifications[] = {
  426. { 0x00000001, scarlett2_notify_ack },
  427. { 0x00000008, scarlett2_notify_sync },
  428. { 0x00200000, scarlett2_notify_input_safe },
  429. { 0x00400000, scarlett2_notify_autogain },
  430. { 0x00800000, scarlett2_notify_input_air },
  431. { 0x01000000, scarlett2_notify_direct_monitor },
  432. { 0x02000000, scarlett2_notify_input_select },
  433. { 0x04000000, scarlett2_notify_input_level },
  434. { 0x08000000, scarlett2_notify_input_phantom },
  435. { 0x10000000, NULL }, /* power status, ignored */
  436. { 0x40000000, scarlett2_notify_input_gain },
  437. { 0x80000000, NULL }, /* power status, ignored */
  438. { 0, NULL }
  439. };
  440. static const struct scarlett2_notification scarlett4_4i4_notifications[] = {
  441. { 0x00000001, scarlett2_notify_ack },
  442. { 0x00000008, scarlett2_notify_sync },
  443. { 0x00200000, scarlett2_notify_input_safe },
  444. { 0x00400000, scarlett2_notify_autogain },
  445. { 0x00800000, scarlett2_notify_input_air },
  446. { 0x01000000, scarlett2_notify_input_select },
  447. { 0x02000000, scarlett2_notify_input_level },
  448. { 0x04000000, scarlett2_notify_input_phantom },
  449. { 0x08000000, scarlett2_notify_power_status }, /* power external */
  450. { 0x20000000, scarlett2_notify_input_gain },
  451. { 0x40000000, scarlett2_notify_power_status }, /* power status */
  452. { 0x80000000, scarlett2_notify_volume },
  453. { 0, NULL }
  454. };
  455. /* Configuration parameters that can be read and written */
  456. enum {
  457. SCARLETT2_CONFIG_DIM_MUTE,
  458. SCARLETT2_CONFIG_LINE_OUT_VOLUME,
  459. SCARLETT2_CONFIG_MUTE_SWITCH,
  460. SCARLETT2_CONFIG_SW_HW_SWITCH,
  461. SCARLETT2_CONFIG_MASTER_VOLUME,
  462. SCARLETT2_CONFIG_HEADPHONE_VOLUME,
  463. SCARLETT2_CONFIG_LEVEL_SWITCH,
  464. SCARLETT2_CONFIG_PAD_SWITCH,
  465. SCARLETT2_CONFIG_MSD_SWITCH,
  466. SCARLETT2_CONFIG_AIR_SWITCH,
  467. SCARLETT2_CONFIG_DSP_SWITCH,
  468. SCARLETT2_CONFIG_COMPRESSOR_PARAMS,
  469. SCARLETT2_CONFIG_PRECOMP_FLT_SWITCH,
  470. SCARLETT2_CONFIG_PRECOMP_FLT_PARAMS,
  471. SCARLETT2_CONFIG_PEQ_FLT_SWITCH,
  472. SCARLETT2_CONFIG_PEQ_FLT_PARAMS,
  473. SCARLETT2_CONFIG_INPUT_MUTE_SWITCH,
  474. SCARLETT2_CONFIG_STANDALONE_SWITCH,
  475. SCARLETT2_CONFIG_PHANTOM_SWITCH,
  476. SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
  477. SCARLETT2_CONFIG_DIRECT_MONITOR,
  478. SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
  479. SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
  480. SCARLETT2_CONFIG_TALKBACK_MAP,
  481. SCARLETT2_CONFIG_AUTOGAIN_SWITCH,
  482. SCARLETT2_CONFIG_AUTOGAIN_STATUS,
  483. SCARLETT2_CONFIG_AG_HOT_TARGET,
  484. SCARLETT2_CONFIG_AG_MEAN_TARGET,
  485. SCARLETT2_CONFIG_AG_PEAK_TARGET,
  486. SCARLETT2_CONFIG_INPUT_GAIN,
  487. SCARLETT2_CONFIG_SAFE_SWITCH,
  488. SCARLETT2_CONFIG_INPUT_SELECT_SWITCH,
  489. SCARLETT2_CONFIG_INPUT_LINK_SWITCH,
  490. SCARLETT2_CONFIG_POWER_EXT,
  491. SCARLETT2_CONFIG_POWER_LOW,
  492. SCARLETT2_CONFIG_PCM_INPUT_SWITCH,
  493. SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN,
  494. SCARLETT2_CONFIG_BLUETOOTH_VOLUME,
  495. SCARLETT2_CONFIG_SPDIF_MODE,
  496. SCARLETT2_CONFIG_COUNT
  497. };
  498. /* Autogain target configuration parameters and names */
  499. static const int scarlett2_ag_target_configs[] = {
  500. [SCARLETT2_AG_HOT_TARGET] = SCARLETT2_CONFIG_AG_HOT_TARGET,
  501. [SCARLETT2_AG_MEAN_TARGET] = SCARLETT2_CONFIG_AG_MEAN_TARGET,
  502. [SCARLETT2_AG_PEAK_TARGET] = SCARLETT2_CONFIG_AG_PEAK_TARGET
  503. };
  504. static const char *const scarlett2_ag_target_names[] = {
  505. "Hot", "Mean", "Peak"
  506. };
  507. /* Location, size, and activation command number for the configuration
  508. * parameters. Size is in bits and may be 1, 8, 16, or 32.
  509. *
  510. * Vocaster and 4th Gen devices have a parameter buffer to set certain
  511. * configuration parameters. When pbuf is set, rather than writing to
  512. * the given offset, the channel and value are written to the
  513. * parameter buffer and the activate command is sent to the device.
  514. *
  515. * Some Gen 4 configuration parameters are written with 0x02 for a
  516. * desired value of 0x01, and 0x03 for 0x00. These are indicated with
  517. * mute set to 1. 0x02 and 0x03 are temporary values while the device
  518. * makes the change and the channel and/or corresponding DSP channel
  519. * output is muted.
  520. */
  521. struct scarlett2_config {
  522. u16 offset;
  523. u8 size;
  524. u8 activate;
  525. u8 pbuf;
  526. u8 mute;
  527. };
  528. struct scarlett2_config_set {
  529. const struct scarlett2_notification *notifications;
  530. u16 param_buf_addr;
  531. const unsigned int *input_gain_tlv;
  532. const char *const *autogain_status_texts;
  533. const struct scarlett2_config items[SCARLETT2_CONFIG_COUNT];
  534. };
  535. /* Input gain TLV dB ranges */
  536. static const DECLARE_TLV_DB_MINMAX(
  537. db_scale_vocaster_gain, 0, 70 * 100
  538. );
  539. static const DECLARE_TLV_DB_MINMAX(
  540. db_scale_gen4_gain, 0, 69 * 100
  541. );
  542. /* Gen 2 devices without SW/HW volume switch: 6i6, 18i8 */
  543. static const struct scarlett2_config_set scarlett2_config_set_gen2a = {
  544. .notifications = scarlett2_notifications,
  545. .items = {
  546. [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
  547. .offset = 0x34, .size = 16, .activate = 1 },
  548. [SCARLETT2_CONFIG_MUTE_SWITCH] = {
  549. .offset = 0x5c, .size = 8, .activate = 1 },
  550. [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
  551. .offset = 0x7c, .size = 8, .activate = 7 },
  552. [SCARLETT2_CONFIG_PAD_SWITCH] = {
  553. .offset = 0x84, .size = 8, .activate = 8 },
  554. [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
  555. .offset = 0x8d, .size = 8, .activate = 6 },
  556. }
  557. };
  558. /* Gen 2 devices with SW/HW volume switch: 18i20 */
  559. static const struct scarlett2_config_set scarlett2_config_set_gen2b = {
  560. .notifications = scarlett2_notifications,
  561. .items = {
  562. [SCARLETT2_CONFIG_DIM_MUTE] = {
  563. .offset = 0x31, .size = 8, .activate = 2 },
  564. [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
  565. .offset = 0x34, .size = 16, .activate = 1 },
  566. [SCARLETT2_CONFIG_MUTE_SWITCH] = {
  567. .offset = 0x5c, .size = 8, .activate = 1 },
  568. [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
  569. .offset = 0x66, .size = 8, .activate = 3 },
  570. [SCARLETT2_CONFIG_MASTER_VOLUME] = {
  571. .offset = 0x76, .size = 16 },
  572. [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
  573. .offset = 0x7c, .size = 8, .activate = 7 },
  574. [SCARLETT2_CONFIG_PAD_SWITCH] = {
  575. .offset = 0x84, .size = 8, .activate = 8 },
  576. [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
  577. .offset = 0x8d, .size = 8, .activate = 6 },
  578. }
  579. };
  580. /* Gen 3 devices without a mixer (Solo and 2i2) */
  581. static const struct scarlett2_config_set scarlett2_config_set_gen3a = {
  582. .notifications = scarlett3a_notifications,
  583. .items = {
  584. [SCARLETT2_CONFIG_MSD_SWITCH] = {
  585. .offset = 0x04, .size = 8, .activate = 6 },
  586. [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
  587. .offset = 0x05, .size = 8, .activate = 6 },
  588. [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
  589. .offset = 0x06, .size = 8, .activate = 3 },
  590. [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
  591. .offset = 0x07, .size = 8, .activate = 4 },
  592. [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
  593. .offset = 0x08, .size = 1, .activate = 7 },
  594. [SCARLETT2_CONFIG_AIR_SWITCH] = {
  595. .offset = 0x09, .size = 1, .activate = 8 },
  596. }
  597. };
  598. /* Gen 3 devices without SW/HW volume switch: 4i4, 8i6 */
  599. static const struct scarlett2_config_set scarlett2_config_set_gen3b = {
  600. .notifications = scarlett2_notifications,
  601. .items = {
  602. [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
  603. .offset = 0x34, .size = 16, .activate = 1 },
  604. [SCARLETT2_CONFIG_MUTE_SWITCH] = {
  605. .offset = 0x5c, .size = 8, .activate = 1 },
  606. [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
  607. .offset = 0x7c, .size = 8, .activate = 7 },
  608. [SCARLETT2_CONFIG_PAD_SWITCH] = {
  609. .offset = 0x84, .size = 8, .activate = 8 },
  610. [SCARLETT2_CONFIG_AIR_SWITCH] = {
  611. .offset = 0x8c, .size = 8, .activate = 8 },
  612. [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
  613. .offset = 0x95, .size = 8, .activate = 6 },
  614. [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
  615. .offset = 0x9c, .size = 1, .activate = 8 },
  616. [SCARLETT2_CONFIG_MSD_SWITCH] = {
  617. .offset = 0x9d, .size = 8, .activate = 6 },
  618. [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
  619. .offset = 0x9e, .size = 8, .activate = 6 },
  620. }
  621. };
  622. /* Gen 3 devices with SW/HW volume switch: 18i8, 18i20 */
  623. static const struct scarlett2_config_set scarlett2_config_set_gen3c = {
  624. .notifications = scarlett2_notifications,
  625. .items = {
  626. [SCARLETT2_CONFIG_DIM_MUTE] = {
  627. .offset = 0x31, .size = 8, .activate = 2 },
  628. [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
  629. .offset = 0x34, .size = 16, .activate = 1 },
  630. [SCARLETT2_CONFIG_MUTE_SWITCH] = {
  631. .offset = 0x5c, .size = 8, .activate = 1 },
  632. [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
  633. .offset = 0x66, .size = 8, .activate = 3 },
  634. [SCARLETT2_CONFIG_MASTER_VOLUME] = {
  635. .offset = 0x76, .size = 16 },
  636. [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
  637. .offset = 0x7c, .size = 8, .activate = 7 },
  638. [SCARLETT2_CONFIG_PAD_SWITCH] = {
  639. .offset = 0x84, .size = 8, .activate = 8 },
  640. [SCARLETT2_CONFIG_AIR_SWITCH] = {
  641. .offset = 0x8c, .size = 8, .activate = 8 },
  642. [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
  643. .offset = 0x95, .size = 8, .activate = 6 },
  644. [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
  645. .offset = 0x9c, .size = 1, .activate = 8 },
  646. [SCARLETT2_CONFIG_MSD_SWITCH] = {
  647. .offset = 0x9d, .size = 8, .activate = 6 },
  648. [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
  649. .offset = 0x9e, .size = 8, .activate = 6 },
  650. [SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH] = {
  651. .offset = 0x9f, .size = 1, .activate = 10 },
  652. [SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE] = {
  653. .offset = 0xa0, .size = 1, .activate = 10 },
  654. [SCARLETT2_CONFIG_TALKBACK_MAP] = {
  655. .offset = 0xb0, .size = 16, .activate = 10 },
  656. [SCARLETT2_CONFIG_SPDIF_MODE] = {
  657. .offset = 0x94, .size = 8, .activate = 6 },
  658. }
  659. };
  660. /* Vocaster */
  661. static const struct scarlett2_config_set scarlett2_config_set_vocaster = {
  662. .notifications = vocaster_notifications,
  663. .param_buf_addr = 0x1bc,
  664. .input_gain_tlv = db_scale_vocaster_gain,
  665. .autogain_status_texts = scarlett2_autogain_status_vocaster,
  666. .items = {
  667. [SCARLETT2_CONFIG_MSD_SWITCH] = {
  668. .offset = 0x9d, .size = 8, .activate = 6 },
  669. [SCARLETT2_CONFIG_AUTOGAIN_SWITCH] = {
  670. .offset = 0x1c0, .size = 8, .activate = 19, .pbuf = 1 },
  671. [SCARLETT2_CONFIG_AUTOGAIN_STATUS] = {
  672. .offset = 0x1c2, .size = 8, },
  673. [SCARLETT2_CONFIG_AG_HOT_TARGET] = {
  674. .offset = 0xc1, .size = 8, .activate = 29, .pbuf = 1 },
  675. [SCARLETT2_CONFIG_INPUT_GAIN] = {
  676. .offset = 0x9f, .size = 8, .activate = 21, .pbuf = 1 },
  677. [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
  678. .offset = 0x9c, .size = 1, .activate = 20, .pbuf = 1 },
  679. [SCARLETT2_CONFIG_DSP_SWITCH] = {
  680. .offset = 0x1c4, .size = 8, .activate = 22, .pbuf = 1 },
  681. [SCARLETT2_CONFIG_COMPRESSOR_PARAMS] = {
  682. .offset = 0x1c8, .size = 32, .activate = 23 },
  683. [SCARLETT2_CONFIG_PRECOMP_FLT_SWITCH] = {
  684. .offset = 0x7c, .size = 32, .activate = 27 },
  685. [SCARLETT2_CONFIG_PRECOMP_FLT_PARAMS] = {
  686. .offset = 0x200, .size = 32, .activate = 27 },
  687. [SCARLETT2_CONFIG_PEQ_FLT_SWITCH] = {
  688. .offset = 0x84, .size = 32, .activate = 27 },
  689. [SCARLETT2_CONFIG_PEQ_FLT_PARAMS] = {
  690. .offset = 0x250, .size = 32, .activate = 27 },
  691. [SCARLETT2_CONFIG_INPUT_MUTE_SWITCH] = {
  692. .offset = 0x1be, .size = 8, .activate = 17, .pbuf = 1 },
  693. [SCARLETT2_CONFIG_BLUETOOTH_VOLUME] = {
  694. .offset = 0xbf, .size = 8, .activate = 28 },
  695. }
  696. };
  697. /* Solo Gen 4 */
  698. static const struct scarlett2_config_set scarlett2_config_set_gen4_solo = {
  699. .notifications = scarlett4_solo_notifications,
  700. .param_buf_addr = 0xd8,
  701. .items = {
  702. [SCARLETT2_CONFIG_MSD_SWITCH] = {
  703. .offset = 0x47, .size = 8, .activate = 4 },
  704. [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
  705. .offset = 0x108, .size = 8, .activate = 12, .pbuf = 1 },
  706. [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
  707. .offset = 0x46, .size = 8, .activate = 9, .pbuf = 1,
  708. .mute = 1 },
  709. [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
  710. .offset = 0x3d, .size = 8, .activate = 10, .pbuf = 1,
  711. .mute = 1 },
  712. [SCARLETT2_CONFIG_AIR_SWITCH] = {
  713. .offset = 0x3e, .size = 8, .activate = 11, .pbuf = 1 },
  714. [SCARLETT2_CONFIG_PCM_INPUT_SWITCH] = {
  715. .offset = 0x206, .size = 8, .activate = 25, .pbuf = 1 },
  716. [SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN] = {
  717. .offset = 0x232, .size = 16, .activate = 26 }
  718. }
  719. };
  720. /* 2i2 Gen 4 */
  721. static const struct scarlett2_config_set scarlett2_config_set_gen4_2i2 = {
  722. .notifications = scarlett4_2i2_notifications,
  723. .param_buf_addr = 0xfc,
  724. .input_gain_tlv = db_scale_gen4_gain,
  725. .autogain_status_texts = scarlett2_autogain_status_gen4,
  726. .items = {
  727. [SCARLETT2_CONFIG_MSD_SWITCH] = {
  728. .offset = 0x49, .size = 8, .activate = 4 },
  729. [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
  730. .offset = 0x14a, .size = 8, .activate = 16, .pbuf = 1 },
  731. [SCARLETT2_CONFIG_AUTOGAIN_SWITCH] = {
  732. .offset = 0x135, .size = 8, .activate = 10, .pbuf = 1 },
  733. [SCARLETT2_CONFIG_AUTOGAIN_STATUS] = {
  734. .offset = 0x137, .size = 8 },
  735. [SCARLETT2_CONFIG_AG_MEAN_TARGET] = {
  736. .offset = 0x131, .size = 8, .activate = 29, .pbuf = 1 },
  737. [SCARLETT2_CONFIG_AG_PEAK_TARGET] = {
  738. .offset = 0x132, .size = 8, .activate = 30, .pbuf = 1 },
  739. [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
  740. .offset = 0x48, .size = 8, .activate = 11, .pbuf = 1,
  741. .mute = 1 },
  742. [SCARLETT2_CONFIG_INPUT_GAIN] = {
  743. .offset = 0x4b, .size = 8, .activate = 12, .pbuf = 1 },
  744. [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
  745. .offset = 0x3c, .size = 8, .activate = 13, .pbuf = 1,
  746. .mute = 1 },
  747. [SCARLETT2_CONFIG_SAFE_SWITCH] = {
  748. .offset = 0x147, .size = 8, .activate = 14, .pbuf = 1 },
  749. [SCARLETT2_CONFIG_AIR_SWITCH] = {
  750. .offset = 0x3e, .size = 8, .activate = 15, .pbuf = 1 },
  751. [SCARLETT2_CONFIG_INPUT_SELECT_SWITCH] = {
  752. .offset = 0x14b, .size = 8, .activate = 17, .pbuf = 1 },
  753. [SCARLETT2_CONFIG_INPUT_LINK_SWITCH] = {
  754. .offset = 0x14e, .size = 8, .activate = 18, .pbuf = 1 },
  755. [SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN] = {
  756. .offset = 0x2a0, .size = 16, .activate = 36 }
  757. }
  758. };
  759. /* 4i4 Gen 4 */
  760. static const struct scarlett2_config_set scarlett2_config_set_gen4_4i4 = {
  761. .notifications = scarlett4_4i4_notifications,
  762. .param_buf_addr = 0x130,
  763. .input_gain_tlv = db_scale_gen4_gain,
  764. .autogain_status_texts = scarlett2_autogain_status_gen4,
  765. .items = {
  766. [SCARLETT2_CONFIG_MSD_SWITCH] = {
  767. .offset = 0x5c, .size = 8, .activate = 4 },
  768. [SCARLETT2_CONFIG_AUTOGAIN_SWITCH] = {
  769. .offset = 0x13e, .size = 8, .activate = 10, .pbuf = 1 },
  770. [SCARLETT2_CONFIG_AUTOGAIN_STATUS] = {
  771. .offset = 0x140, .size = 8 },
  772. [SCARLETT2_CONFIG_AG_MEAN_TARGET] = {
  773. .offset = 0x13a, .size = 8, .activate = 23, .pbuf = 1 },
  774. [SCARLETT2_CONFIG_AG_PEAK_TARGET] = {
  775. .offset = 0x13b, .size = 8, .activate = 24, .pbuf = 1 },
  776. [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
  777. .offset = 0x5a, .size = 8, .activate = 11, .pbuf = 1,
  778. .mute = 1 },
  779. [SCARLETT2_CONFIG_INPUT_GAIN] = {
  780. .offset = 0x5e, .size = 8, .activate = 12, .pbuf = 1 },
  781. [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
  782. .offset = 0x4e, .size = 8, .activate = 13, .pbuf = 1,
  783. .mute = 1 },
  784. [SCARLETT2_CONFIG_SAFE_SWITCH] = {
  785. .offset = 0x150, .size = 8, .activate = 14, .pbuf = 1 },
  786. [SCARLETT2_CONFIG_AIR_SWITCH] = {
  787. .offset = 0x50, .size = 8, .activate = 15, .pbuf = 1 },
  788. [SCARLETT2_CONFIG_INPUT_SELECT_SWITCH] = {
  789. .offset = 0x153, .size = 8, .activate = 16, .pbuf = 1 },
  790. [SCARLETT2_CONFIG_INPUT_LINK_SWITCH] = {
  791. .offset = 0x156, .size = 8, .activate = 17, .pbuf = 1 },
  792. [SCARLETT2_CONFIG_MASTER_VOLUME] = {
  793. .offset = 0x32, .size = 16 },
  794. [SCARLETT2_CONFIG_HEADPHONE_VOLUME] = {
  795. .offset = 0x3a, .size = 16 },
  796. [SCARLETT2_CONFIG_POWER_EXT] = {
  797. .offset = 0x168, .size = 8 },
  798. [SCARLETT2_CONFIG_POWER_LOW] = {
  799. .offset = 0x16d, .size = 8 }
  800. }
  801. };
  802. /* Clarett USB and Clarett+ devices: 2Pre, 4Pre, 8Pre */
  803. static const struct scarlett2_config_set scarlett2_config_set_clarett = {
  804. .notifications = scarlett2_notifications,
  805. .items = {
  806. [SCARLETT2_CONFIG_DIM_MUTE] = {
  807. .offset = 0x31, .size = 8, .activate = 2 },
  808. [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
  809. .offset = 0x34, .size = 16, .activate = 1 },
  810. [SCARLETT2_CONFIG_MUTE_SWITCH] = {
  811. .offset = 0x5c, .size = 8, .activate = 1 },
  812. [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
  813. .offset = 0x66, .size = 8, .activate = 3 },
  814. [SCARLETT2_CONFIG_MASTER_VOLUME] = {
  815. .offset = 0x76, .size = 16 },
  816. [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
  817. .offset = 0x7c, .size = 8, .activate = 7 },
  818. [SCARLETT2_CONFIG_AIR_SWITCH] = {
  819. .offset = 0x95, .size = 8, .activate = 8 },
  820. [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
  821. .offset = 0x8d, .size = 8, .activate = 6 },
  822. [SCARLETT2_CONFIG_SPDIF_MODE] = {
  823. .offset = 0x9e, .size = 8, .activate = 4 },
  824. }
  825. };
  826. /* Description of each hardware port type:
  827. * - id: hardware ID of this port type
  828. * - src_descr: printf format string for mux input selections
  829. * - src_num_offset: added to channel number for the fprintf
  830. * - dst_descr: printf format string for mixer controls
  831. */
  832. struct scarlett2_port {
  833. u16 id;
  834. const char * const src_descr;
  835. int src_num_offset;
  836. const char * const dst_descr;
  837. const char * const dsp_src_descr;
  838. const char * const dsp_dst_descr;
  839. };
  840. static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
  841. [SCARLETT2_PORT_TYPE_NONE] = {
  842. .id = 0x000,
  843. .src_descr = "Off"
  844. },
  845. [SCARLETT2_PORT_TYPE_ANALOGUE] = {
  846. .id = 0x080,
  847. .src_descr = "Analogue %d",
  848. .src_num_offset = 1,
  849. .dst_descr = "Analogue Output %02d Playback"
  850. },
  851. [SCARLETT2_PORT_TYPE_SPDIF] = {
  852. .id = 0x180,
  853. .src_descr = "S/PDIF %d",
  854. .src_num_offset = 1,
  855. .dst_descr = "S/PDIF Output %d Playback"
  856. },
  857. [SCARLETT2_PORT_TYPE_ADAT] = {
  858. .id = 0x200,
  859. .src_descr = "ADAT %d",
  860. .src_num_offset = 1,
  861. .dst_descr = "ADAT Output %d Playback"
  862. },
  863. [SCARLETT2_PORT_TYPE_MIX] = {
  864. .id = 0x300,
  865. .src_descr = "Mix %c",
  866. .src_num_offset = 'A',
  867. .dst_descr = "Mixer Input %02d Capture",
  868. .dsp_src_descr = "DSP %d",
  869. .dsp_dst_descr = "DSP Input %d Capture"
  870. },
  871. [SCARLETT2_PORT_TYPE_PCM] = {
  872. .id = 0x600,
  873. .src_descr = "PCM %d",
  874. .src_num_offset = 1,
  875. .dst_descr = "PCM %02d Capture"
  876. },
  877. };
  878. /* Number of mux tables: one for each band of sample rates
  879. * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
  880. */
  881. #define SCARLETT2_MUX_TABLES 3
  882. /* Maximum number of entries in a mux table */
  883. #define SCARLETT2_MAX_MUX_ENTRIES 10
  884. /* One entry within mux_assignment defines the port type and range of
  885. * ports to add to the set_mux message. The end of the list is marked
  886. * with count == 0.
  887. */
  888. struct scarlett2_mux_entry {
  889. u8 port_type;
  890. u8 start;
  891. u8 count;
  892. };
  893. /* Maximum number of entries in a mux table */
  894. #define SCARLETT2_MAX_METER_ENTRIES 9
  895. /* One entry within meter_assignment defines the range of mux outputs
  896. * that consecutive meter entries are mapped to. The end of the list
  897. * is marked with count == 0.
  898. */
  899. struct scarlett2_meter_entry {
  900. u8 start;
  901. u8 count;
  902. };
  903. struct scarlett2_device_info {
  904. /* which set of configuration parameters the device uses */
  905. const struct scarlett2_config_set *config_set;
  906. /* minimum firmware version required */
  907. u16 min_firmware_version;
  908. /* support for main/alt speaker switching */
  909. u8 has_speaker_switching;
  910. /* support for talkback microphone */
  911. u8 has_talkback;
  912. /* the number of analogue inputs with a software switchable
  913. * level control that can be set to line or instrument
  914. */
  915. u8 level_input_count;
  916. /* the first input with a level control (0-based) */
  917. u8 level_input_first;
  918. /* the number of analogue inputs with a software switchable
  919. * 10dB pad control
  920. */
  921. u8 pad_input_count;
  922. /* the number of analogue inputs with a software switchable
  923. * "air" control
  924. */
  925. u8 air_input_count;
  926. /* the first input with an air control (0-based) */
  927. u8 air_input_first;
  928. /* number of additional air options
  929. * 0 for air presence only (Gen 3)
  930. * 1 for air presence+drive (Gen 4)
  931. */
  932. u8 air_option;
  933. /* the number of analogue inputs with DSP control */
  934. u8 dsp_input_count;
  935. /* number of pre-compressor filters */
  936. u8 precomp_flt_count;
  937. /* number of parametric EQ filters */
  938. u8 peq_flt_count;
  939. /* number of PEQ filters plus unused slots */
  940. u8 peq_flt_total_count;
  941. /* the number of analogue inputs with a software switchable
  942. * mute control
  943. */
  944. u8 mute_input_count;
  945. /* the number of phantom (48V) software switchable controls */
  946. u8 phantom_count;
  947. /* the first input with phantom power control (0-based) */
  948. u8 phantom_first;
  949. /* the number of inputs each phantom switch controls */
  950. u8 inputs_per_phantom;
  951. /* the number of inputs with software-controllable gain */
  952. u8 gain_input_count;
  953. /* the number of inputs with safe mode */
  954. u8 safe_input_count;
  955. /* the number of direct monitor options
  956. * (0 = none, 1 = mono only, 2 = mono/stereo)
  957. */
  958. u8 direct_monitor;
  959. /* the number of DSP channels */
  960. u8 dsp_count;
  961. /* has a Bluetooth module with volume control */
  962. u8 has_bluetooth;
  963. /* S/PDIF Source/Digital I/O mode control */
  964. const char * const spdif_mode_control_name;
  965. const u8 *spdif_mode_values;
  966. const char * const *spdif_mode_texts;
  967. /* remap analogue outputs; 18i8 Gen 3 has "line 3/4" connected
  968. * internally to the analogue 7/8 outputs
  969. */
  970. u8 line_out_remap_enable;
  971. u8 line_out_remap[SCARLETT2_ANALOGUE_MAX];
  972. u8 line_out_unmap[SCARLETT2_ANALOGUE_MAX];
  973. /* additional description for the line out volume controls */
  974. const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
  975. /* number of sources/destinations of each port type */
  976. const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];
  977. /* layout/order of the entries in the set_mux message */
  978. struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES]
  979. [SCARLETT2_MAX_MUX_ENTRIES];
  980. /* map from meter level order returned by
  981. * SCARLETT2_USB_GET_METER to index into mux[] entries (same
  982. * as the order returned by scarlett2_meter_ctl_get())
  983. */
  984. struct scarlett2_meter_entry meter_map[SCARLETT2_MAX_METER_ENTRIES];
  985. };
  986. struct scarlett2_data {
  987. struct usb_mixer_interface *mixer;
  988. struct mutex usb_mutex; /* prevent sending concurrent USB requests */
  989. struct completion cmd_done;
  990. struct mutex data_mutex; /* lock access to this data */
  991. u8 running;
  992. u8 hwdep_in_use;
  993. u8 selected_flash_segment_id;
  994. u8 flash_write_state;
  995. struct delayed_work work;
  996. const struct scarlett2_device_info *info;
  997. const struct scarlett2_config_set *config_set;
  998. const char *series_name;
  999. __u8 bInterfaceNumber;
  1000. __u8 bEndpointAddress;
  1001. __u16 wMaxPacketSize;
  1002. __u8 bInterval;
  1003. u8 num_mux_srcs;
  1004. u8 num_mux_dsts;
  1005. u8 num_mix_in;
  1006. u8 num_mix_out;
  1007. u8 num_line_out;
  1008. u8 num_monitor_mix_ctls;
  1009. u8 num_autogain_status_texts;
  1010. u32 firmware_version;
  1011. u8 flash_segment_nums[SCARLETT2_SEGMENT_ID_COUNT];
  1012. u8 flash_segment_blocks[SCARLETT2_SEGMENT_ID_COUNT];
  1013. u16 scarlett2_seq;
  1014. u8 sync_updated;
  1015. u8 vol_updated;
  1016. u8 dim_mute_updated;
  1017. u8 input_level_updated;
  1018. u8 input_pad_updated;
  1019. u8 input_air_updated;
  1020. u8 input_dsp_updated;
  1021. u8 input_mute_updated;
  1022. u8 input_phantom_updated;
  1023. u8 input_select_updated;
  1024. u8 input_gain_updated;
  1025. u8 autogain_updated;
  1026. u8 input_safe_updated;
  1027. u8 pcm_input_switch_updated;
  1028. u8 monitor_other_updated;
  1029. u8 direct_monitor_updated;
  1030. u8 mux_updated;
  1031. u8 mix_updated;
  1032. u8 speaker_switching_switched;
  1033. u8 power_status_updated;
  1034. u8 bluetooth_updated;
  1035. u8 sync;
  1036. u8 master_vol;
  1037. u8 headphone_vol;
  1038. u8 vol[SCARLETT2_ANALOGUE_MAX];
  1039. u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
  1040. u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
  1041. u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
  1042. u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
  1043. u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
  1044. u8 air_switch[SCARLETT2_AIR_SWITCH_MAX];
  1045. u8 dsp_switch[SCARLETT2_DSP_SWITCH_MAX];
  1046. s32 compressor_values[SCARLETT2_COMPRESSOR_CTLS_MAX];
  1047. s32 precomp_flt_values[SCARLETT2_PRECOMP_FLT_VALUES_MAX];
  1048. s32 peq_flt_values[SCARLETT2_PEQ_FLT_VALUES_MAX];
  1049. u8 precomp_flt_switch[SCARLETT2_DSP_SWITCH_MAX];
  1050. u8 peq_flt_switch[SCARLETT2_DSP_SWITCH_MAX];
  1051. u8 input_mute_switch[SCARLETT2_INPUT_MUTE_SWITCH_MAX];
  1052. u8 phantom_switch[SCARLETT2_PHANTOM_SWITCH_MAX];
  1053. u8 phantom_persistence;
  1054. u8 input_select_switch;
  1055. u8 input_link_switch[SCARLETT2_INPUT_GAIN_MAX / 2];
  1056. u8 gain[SCARLETT2_INPUT_GAIN_MAX];
  1057. u8 autogain_switch[SCARLETT2_INPUT_GAIN_MAX];
  1058. u8 autogain_status[SCARLETT2_INPUT_GAIN_MAX];
  1059. s8 ag_targets[SCARLETT2_AG_TARGET_COUNT];
  1060. u8 safe_switch[SCARLETT2_INPUT_GAIN_MAX];
  1061. u8 pcm_input_switch;
  1062. u8 direct_monitor_switch;
  1063. u8 speaker_switching_switch;
  1064. u8 talkback_switch;
  1065. u8 talkback_map[SCARLETT2_OUTPUT_MIX_MAX];
  1066. u8 msd_switch;
  1067. u8 standalone_switch;
  1068. u8 power_status;
  1069. u8 bluetooth_volume;
  1070. u8 spdif_mode;
  1071. u8 meter_level_map[SCARLETT2_MAX_METERS];
  1072. struct snd_kcontrol *sync_ctl;
  1073. struct snd_kcontrol *master_vol_ctl;
  1074. struct snd_kcontrol *headphone_vol_ctl;
  1075. struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
  1076. struct snd_kcontrol *sw_hw_ctls[SCARLETT2_ANALOGUE_MAX];
  1077. struct snd_kcontrol *mute_ctls[SCARLETT2_ANALOGUE_MAX];
  1078. struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT];
  1079. struct snd_kcontrol *level_ctls[SCARLETT2_LEVEL_SWITCH_MAX];
  1080. struct snd_kcontrol *pad_ctls[SCARLETT2_PAD_SWITCH_MAX];
  1081. struct snd_kcontrol *air_ctls[SCARLETT2_AIR_SWITCH_MAX];
  1082. struct snd_kcontrol *dsp_ctls[SCARLETT2_DSP_SWITCH_MAX];
  1083. struct snd_kcontrol *input_mute_ctls[SCARLETT2_INPUT_MUTE_SWITCH_MAX];
  1084. struct snd_kcontrol *phantom_ctls[SCARLETT2_PHANTOM_SWITCH_MAX];
  1085. struct snd_kcontrol *input_select_ctl;
  1086. struct snd_kcontrol *input_link_ctls[SCARLETT2_INPUT_GAIN_MAX / 2];
  1087. struct snd_kcontrol *input_gain_ctls[SCARLETT2_INPUT_GAIN_MAX];
  1088. struct snd_kcontrol *autogain_ctls[SCARLETT2_INPUT_GAIN_MAX];
  1089. struct snd_kcontrol *autogain_status_ctls[SCARLETT2_INPUT_GAIN_MAX];
  1090. struct snd_kcontrol *ag_target_ctls[SCARLETT2_AG_TARGET_COUNT];
  1091. struct snd_kcontrol *safe_ctls[SCARLETT2_INPUT_GAIN_MAX];
  1092. struct snd_kcontrol *pcm_input_switch_ctl;
  1093. struct snd_kcontrol *mux_ctls[SCARLETT2_MUX_MAX];
  1094. struct snd_kcontrol *mix_ctls[SCARLETT2_MIX_MAX];
  1095. struct snd_kcontrol *compressor_ctls[SCARLETT2_COMPRESSOR_CTLS_MAX];
  1096. struct snd_kcontrol *precomp_flt_ctls[SCARLETT2_PRECOMP_FLT_CTLS_MAX];
  1097. struct snd_kcontrol *peq_flt_ctls[SCARLETT2_PEQ_FLT_CTLS_MAX];
  1098. struct snd_kcontrol *precomp_flt_switch_ctls[SCARLETT2_DSP_SWITCH_MAX];
  1099. struct snd_kcontrol *peq_flt_switch_ctls[SCARLETT2_DSP_SWITCH_MAX];
  1100. struct snd_kcontrol *direct_monitor_ctl;
  1101. struct snd_kcontrol *speaker_switching_ctl;
  1102. struct snd_kcontrol *talkback_ctl;
  1103. struct snd_kcontrol *power_status_ctl;
  1104. struct snd_kcontrol *bluetooth_volume_ctl;
  1105. u8 mux[SCARLETT2_MUX_MAX];
  1106. u8 mix[SCARLETT2_MIX_MAX];
  1107. u8 monitor_mix[SCARLETT2_MONITOR_MIX_MAX];
  1108. };
  1109. /*** Model-specific data ***/
  1110. static const struct scarlett2_device_info s6i6_gen2_info = {
  1111. .config_set = &scarlett2_config_set_gen2a,
  1112. .level_input_count = 2,
  1113. .pad_input_count = 2,
  1114. .line_out_descrs = {
  1115. "Headphones 1 L",
  1116. "Headphones 1 R",
  1117. "Headphones 2 L",
  1118. "Headphones 2 R",
  1119. },
  1120. .port_count = {
  1121. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  1122. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
  1123. [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
  1124. [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
  1125. [SCARLETT2_PORT_TYPE_PCM] = { 6, 6 },
  1126. },
  1127. .mux_assignment = { {
  1128. { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
  1129. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  1130. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1131. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  1132. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  1133. { 0, 0, 0 },
  1134. }, {
  1135. { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
  1136. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  1137. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1138. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  1139. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  1140. { 0, 0, 0 },
  1141. }, {
  1142. { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
  1143. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  1144. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1145. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  1146. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  1147. { 0, 0, 0 },
  1148. } },
  1149. .meter_map = {
  1150. { 24, 6 },
  1151. { 0, 24 },
  1152. { 0, 0 },
  1153. }
  1154. };
  1155. static const struct scarlett2_device_info s18i8_gen2_info = {
  1156. .config_set = &scarlett2_config_set_gen2a,
  1157. .level_input_count = 2,
  1158. .pad_input_count = 4,
  1159. .line_out_descrs = {
  1160. "Monitor L",
  1161. "Monitor R",
  1162. "Headphones 1 L",
  1163. "Headphones 1 R",
  1164. "Headphones 2 L",
  1165. "Headphones 2 R",
  1166. },
  1167. .port_count = {
  1168. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  1169. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 },
  1170. [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
  1171. [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
  1172. [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
  1173. [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 },
  1174. },
  1175. .mux_assignment = { {
  1176. { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
  1177. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
  1178. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1179. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  1180. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  1181. { 0, 0, 0 },
  1182. }, {
  1183. { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
  1184. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
  1185. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1186. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  1187. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  1188. { 0, 0, 0 },
  1189. }, {
  1190. { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
  1191. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
  1192. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1193. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  1194. { SCARLETT2_PORT_TYPE_NONE, 0, 4 },
  1195. { 0, 0, 0 },
  1196. } },
  1197. .meter_map = {
  1198. { 26, 18 },
  1199. { 0, 26 },
  1200. { 0, 0 },
  1201. }
  1202. };
  1203. static const struct scarlett2_device_info s18i20_gen2_info = {
  1204. .config_set = &scarlett2_config_set_gen2b,
  1205. .line_out_descrs = {
  1206. "Monitor L",
  1207. "Monitor R",
  1208. NULL,
  1209. NULL,
  1210. NULL,
  1211. NULL,
  1212. "Headphones 1 L",
  1213. "Headphones 1 R",
  1214. "Headphones 2 L",
  1215. "Headphones 2 R",
  1216. },
  1217. .port_count = {
  1218. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  1219. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
  1220. [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
  1221. [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
  1222. [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
  1223. [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
  1224. },
  1225. .mux_assignment = { {
  1226. { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
  1227. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
  1228. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1229. { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
  1230. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  1231. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  1232. { 0, 0, 0 },
  1233. }, {
  1234. { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
  1235. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
  1236. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1237. { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
  1238. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  1239. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  1240. { 0, 0, 0 },
  1241. }, {
  1242. { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
  1243. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
  1244. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1245. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  1246. { SCARLETT2_PORT_TYPE_NONE, 0, 6 },
  1247. { 0, 0, 0 },
  1248. } },
  1249. .meter_map = {
  1250. { 38, 18 },
  1251. { 0, 38 },
  1252. { 0, 0 },
  1253. }
  1254. };
  1255. static const struct scarlett2_device_info solo_gen3_info = {
  1256. .config_set = &scarlett2_config_set_gen3a,
  1257. .level_input_count = 1,
  1258. .level_input_first = 1,
  1259. .air_input_count = 1,
  1260. .phantom_count = 1,
  1261. .inputs_per_phantom = 1,
  1262. .direct_monitor = 1,
  1263. };
  1264. static const struct scarlett2_device_info s2i2_gen3_info = {
  1265. .config_set = &scarlett2_config_set_gen3a,
  1266. .level_input_count = 2,
  1267. .air_input_count = 2,
  1268. .phantom_count = 1,
  1269. .inputs_per_phantom = 2,
  1270. .direct_monitor = 2,
  1271. };
  1272. static const struct scarlett2_device_info s4i4_gen3_info = {
  1273. .config_set = &scarlett2_config_set_gen3b,
  1274. .level_input_count = 2,
  1275. .pad_input_count = 2,
  1276. .air_input_count = 2,
  1277. .phantom_count = 1,
  1278. .inputs_per_phantom = 2,
  1279. .line_out_descrs = {
  1280. "Monitor L",
  1281. "Monitor R",
  1282. "Headphones L",
  1283. "Headphones R",
  1284. },
  1285. .port_count = {
  1286. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  1287. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
  1288. [SCARLETT2_PORT_TYPE_MIX] = { 6, 8 },
  1289. [SCARLETT2_PORT_TYPE_PCM] = { 4, 6 },
  1290. },
  1291. .mux_assignment = { {
  1292. { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
  1293. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  1294. { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
  1295. { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
  1296. { 0, 0, 0 },
  1297. }, {
  1298. { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
  1299. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  1300. { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
  1301. { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
  1302. { 0, 0, 0 },
  1303. }, {
  1304. { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
  1305. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  1306. { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
  1307. { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
  1308. { 0, 0, 0 },
  1309. } },
  1310. .meter_map = {
  1311. { 12, 6 },
  1312. { 0, 12 },
  1313. { 0, 0 },
  1314. }
  1315. };
  1316. static const struct scarlett2_device_info s8i6_gen3_info = {
  1317. .config_set = &scarlett2_config_set_gen3b,
  1318. .level_input_count = 2,
  1319. .pad_input_count = 2,
  1320. .air_input_count = 2,
  1321. .phantom_count = 1,
  1322. .inputs_per_phantom = 2,
  1323. .line_out_descrs = {
  1324. "Headphones 1 L",
  1325. "Headphones 1 R",
  1326. "Headphones 2 L",
  1327. "Headphones 2 R",
  1328. },
  1329. .port_count = {
  1330. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  1331. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6, 4 },
  1332. [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
  1333. [SCARLETT2_PORT_TYPE_MIX] = { 8, 8 },
  1334. [SCARLETT2_PORT_TYPE_PCM] = { 6, 10 },
  1335. },
  1336. .mux_assignment = { {
  1337. { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
  1338. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  1339. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1340. { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
  1341. { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
  1342. { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
  1343. { 0, 0, 0 },
  1344. }, {
  1345. { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
  1346. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  1347. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1348. { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
  1349. { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
  1350. { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
  1351. { 0, 0, 0 },
  1352. }, {
  1353. { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
  1354. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  1355. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1356. { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
  1357. { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
  1358. { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
  1359. { 0, 0, 0 },
  1360. } },
  1361. .meter_map = {
  1362. { 14, 8 },
  1363. { 0, 6 },
  1364. { 22, 2 },
  1365. { 6, 8 },
  1366. { 0, 0 },
  1367. }
  1368. };
  1369. static const u8 scarlett2_spdif_s18i8_gen3_values[] = { 0, 2, 0xff };
  1370. static const char * const scarlett2_spdif_s18i8_gen3_texts[] = {
  1371. "RCA",
  1372. "Optical",
  1373. NULL
  1374. };
  1375. static const struct scarlett2_device_info s18i8_gen3_info = {
  1376. .config_set = &scarlett2_config_set_gen3c,
  1377. .has_speaker_switching = 1,
  1378. .level_input_count = 2,
  1379. .pad_input_count = 4,
  1380. .air_input_count = 4,
  1381. .phantom_count = 2,
  1382. .inputs_per_phantom = 2,
  1383. .spdif_mode_control_name = "S/PDIF Mode Capture Enum",
  1384. .spdif_mode_values = scarlett2_spdif_s18i8_gen3_values,
  1385. .spdif_mode_texts = scarlett2_spdif_s18i8_gen3_texts,
  1386. .line_out_remap_enable = 1,
  1387. .line_out_remap = { 0, 1, 6, 7, 2, 3, 4, 5 },
  1388. .line_out_unmap = { 0, 1, 4, 5, 6, 7, 2, 3 },
  1389. .line_out_descrs = {
  1390. "Monitor L",
  1391. "Monitor R",
  1392. "Alt Monitor L",
  1393. "Alt Monitor R",
  1394. "Headphones 1 L",
  1395. "Headphones 1 R",
  1396. "Headphones 2 L",
  1397. "Headphones 2 R",
  1398. },
  1399. .port_count = {
  1400. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  1401. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 8 },
  1402. [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
  1403. [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
  1404. [SCARLETT2_PORT_TYPE_MIX] = { 10, 20 },
  1405. [SCARLETT2_PORT_TYPE_PCM] = { 8, 20 },
  1406. },
  1407. .mux_assignment = { {
  1408. { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
  1409. { SCARLETT2_PORT_TYPE_PCM, 12, 8 },
  1410. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
  1411. { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
  1412. { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
  1413. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1414. { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
  1415. { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
  1416. { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
  1417. { 0, 0, 0 },
  1418. }, {
  1419. { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
  1420. { SCARLETT2_PORT_TYPE_PCM, 12, 4 },
  1421. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
  1422. { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
  1423. { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
  1424. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1425. { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
  1426. { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
  1427. { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
  1428. { 0, 0, 0 },
  1429. }, {
  1430. { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
  1431. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
  1432. { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
  1433. { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
  1434. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1435. { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
  1436. { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
  1437. { 0, 0, 0 },
  1438. } },
  1439. .meter_map = {
  1440. { 30, 10 },
  1441. { 42, 8 },
  1442. { 0, 2 },
  1443. { 6, 2 },
  1444. { 2, 4 },
  1445. { 8, 2 },
  1446. { 40, 2 },
  1447. { 10, 20 },
  1448. { 0, 0 }
  1449. }
  1450. };
  1451. static const u8 scarlett2_spdif_s18i20_gen3_values[] = { 0, 6, 1, 0xff };
  1452. static const char * const scarlett2_spdif_s18i20_gen3_texts[] = {
  1453. "S/PDIF RCA",
  1454. "S/PDIF Optical",
  1455. "Dual ADAT",
  1456. NULL
  1457. };
  1458. static const struct scarlett2_device_info s18i20_gen3_info = {
  1459. .config_set = &scarlett2_config_set_gen3c,
  1460. .has_speaker_switching = 1,
  1461. .has_talkback = 1,
  1462. .level_input_count = 2,
  1463. .pad_input_count = 8,
  1464. .air_input_count = 8,
  1465. .phantom_count = 2,
  1466. .inputs_per_phantom = 4,
  1467. .spdif_mode_control_name = "Digital I/O Mode Capture Enum",
  1468. .spdif_mode_values = scarlett2_spdif_s18i20_gen3_values,
  1469. .spdif_mode_texts = scarlett2_spdif_s18i20_gen3_texts,
  1470. .line_out_descrs = {
  1471. "Monitor 1 L",
  1472. "Monitor 1 R",
  1473. "Monitor 2 L",
  1474. "Monitor 2 R",
  1475. NULL,
  1476. NULL,
  1477. "Headphones 1 L",
  1478. "Headphones 1 R",
  1479. "Headphones 2 L",
  1480. "Headphones 2 R",
  1481. },
  1482. .port_count = {
  1483. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  1484. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 9, 10 },
  1485. [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
  1486. [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
  1487. [SCARLETT2_PORT_TYPE_MIX] = { 12, 25 },
  1488. [SCARLETT2_PORT_TYPE_PCM] = { 20, 20 },
  1489. },
  1490. .mux_assignment = { {
  1491. { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
  1492. { SCARLETT2_PORT_TYPE_PCM, 10, 10 },
  1493. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
  1494. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1495. { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
  1496. { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
  1497. { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
  1498. { SCARLETT2_PORT_TYPE_NONE, 0, 12 },
  1499. { 0, 0, 0 },
  1500. }, {
  1501. { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
  1502. { SCARLETT2_PORT_TYPE_PCM, 10, 8 },
  1503. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
  1504. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1505. { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
  1506. { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
  1507. { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
  1508. { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
  1509. { 0, 0, 0 },
  1510. }, {
  1511. { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
  1512. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
  1513. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1514. { SCARLETT2_PORT_TYPE_NONE, 0, 24 },
  1515. { 0, 0, 0 },
  1516. } },
  1517. .meter_map = {
  1518. { 45, 8 },
  1519. { 55, 10 },
  1520. { 0, 20 },
  1521. { 53, 2 },
  1522. { 20, 25 },
  1523. { 0, 0 },
  1524. }
  1525. };
  1526. static const struct scarlett2_device_info vocaster_one_info = {
  1527. .config_set = &scarlett2_config_set_vocaster,
  1528. .min_firmware_version = 1769,
  1529. .phantom_count = 1,
  1530. .inputs_per_phantom = 1,
  1531. .dsp_count = 1,
  1532. .dsp_input_count = 1,
  1533. .precomp_flt_count = 2,
  1534. .peq_flt_count = 3,
  1535. .peq_flt_total_count = 4,
  1536. .mute_input_count = 1,
  1537. .gain_input_count = 1,
  1538. .port_count = {
  1539. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  1540. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 2, 4 },
  1541. [SCARLETT2_PORT_TYPE_MIX] = { 9, 9 },
  1542. [SCARLETT2_PORT_TYPE_PCM] = { 4, 10 },
  1543. },
  1544. .mux_assignment = { {
  1545. { SCARLETT2_PORT_TYPE_MIX, 8, 1 },
  1546. { SCARLETT2_PORT_TYPE_PCM, 5, 5 },
  1547. { SCARLETT2_PORT_TYPE_MIX, 6, 2 },
  1548. { SCARLETT2_PORT_TYPE_PCM, 0, 5 },
  1549. { SCARLETT2_PORT_TYPE_MIX, 0, 6 },
  1550. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  1551. { 0, 0, 0 },
  1552. } },
  1553. .meter_map = {
  1554. { 12, 1 },
  1555. { 18, 5 },
  1556. { 10, 2 },
  1557. { 13, 5 },
  1558. { 4, 6 },
  1559. { 0, 4 },
  1560. { 0, 0 }
  1561. }
  1562. };
  1563. static const struct scarlett2_device_info vocaster_two_info = {
  1564. .config_set = &scarlett2_config_set_vocaster,
  1565. .min_firmware_version = 1769,
  1566. .phantom_count = 2,
  1567. .inputs_per_phantom = 1,
  1568. .dsp_count = 2,
  1569. .dsp_input_count = 2,
  1570. .precomp_flt_count = 2,
  1571. .peq_flt_count = 3,
  1572. .peq_flt_total_count = 4,
  1573. .mute_input_count = 2,
  1574. .gain_input_count = 2,
  1575. .has_bluetooth = 1,
  1576. .port_count = {
  1577. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  1578. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6, 6 },
  1579. [SCARLETT2_PORT_TYPE_MIX] = { 12, 14 },
  1580. [SCARLETT2_PORT_TYPE_PCM] = { 4, 14 },
  1581. },
  1582. .mux_assignment = { {
  1583. { SCARLETT2_PORT_TYPE_MIX, 12, 2 },
  1584. { SCARLETT2_PORT_TYPE_PCM, 6, 8 },
  1585. { SCARLETT2_PORT_TYPE_MIX, 10, 2 },
  1586. { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
  1587. { SCARLETT2_PORT_TYPE_MIX, 0, 10 },
  1588. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
  1589. { 0, 0, 0 },
  1590. } },
  1591. .meter_map = {
  1592. { 18, 2 },
  1593. { 26, 8 },
  1594. { 16, 2 },
  1595. { 20, 6 },
  1596. { 6, 10 },
  1597. { 0, 6 },
  1598. { 0, 0 }
  1599. }
  1600. };
  1601. static const struct scarlett2_device_info solo_gen4_info = {
  1602. .config_set = &scarlett2_config_set_gen4_solo,
  1603. .min_firmware_version = 2115,
  1604. .level_input_count = 1,
  1605. .air_input_count = 1,
  1606. .air_input_first = 1,
  1607. .air_option = 1,
  1608. .phantom_count = 1,
  1609. .phantom_first = 1,
  1610. .inputs_per_phantom = 1,
  1611. .direct_monitor = 1,
  1612. .dsp_count = 2,
  1613. .port_count = {
  1614. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  1615. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 2, 2 },
  1616. [SCARLETT2_PORT_TYPE_MIX] = { 8, 6 },
  1617. [SCARLETT2_PORT_TYPE_PCM] = { 2, 4 },
  1618. },
  1619. .mux_assignment = { {
  1620. { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
  1621. { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
  1622. { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
  1623. { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
  1624. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
  1625. { 0, 0, 0 },
  1626. }, {
  1627. { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
  1628. { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
  1629. { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
  1630. { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
  1631. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
  1632. { 0, 0, 0 },
  1633. }, {
  1634. { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
  1635. { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
  1636. { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
  1637. { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
  1638. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
  1639. { 0, 0, 0 },
  1640. } },
  1641. .meter_map = {
  1642. { 6, 2 },
  1643. { 4, 2 },
  1644. { 8, 4 },
  1645. { 2, 2 },
  1646. { 0, 2 },
  1647. { 0, 0 }
  1648. }
  1649. };
  1650. static const struct scarlett2_device_info s2i2_gen4_info = {
  1651. .config_set = &scarlett2_config_set_gen4_2i2,
  1652. .min_firmware_version = 2115,
  1653. .level_input_count = 2,
  1654. .air_input_count = 2,
  1655. .air_option = 1,
  1656. .phantom_count = 1,
  1657. .inputs_per_phantom = 2,
  1658. .gain_input_count = 2,
  1659. .safe_input_count = 2,
  1660. .direct_monitor = 2,
  1661. .dsp_count = 2,
  1662. .port_count = {
  1663. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  1664. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 2, 2 },
  1665. [SCARLETT2_PORT_TYPE_MIX] = { 6, 6 },
  1666. [SCARLETT2_PORT_TYPE_PCM] = { 2, 4 },
  1667. },
  1668. .mux_assignment = { {
  1669. { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
  1670. { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
  1671. { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
  1672. { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
  1673. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
  1674. { 0, 0, 0 },
  1675. }, {
  1676. { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
  1677. { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
  1678. { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
  1679. { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
  1680. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
  1681. { 0, 0, 0 },
  1682. }, {
  1683. { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
  1684. { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
  1685. { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
  1686. { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
  1687. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
  1688. { 0, 0, 0 },
  1689. } },
  1690. .meter_map = {
  1691. { 6, 2 },
  1692. { 4, 2 },
  1693. { 8, 4 },
  1694. { 2, 2 },
  1695. { 0, 2 },
  1696. { 0, 0 }
  1697. }
  1698. };
  1699. static const struct scarlett2_device_info s4i4_gen4_info = {
  1700. .config_set = &scarlett2_config_set_gen4_4i4,
  1701. .min_firmware_version = 2089,
  1702. .level_input_count = 2,
  1703. .air_input_count = 2,
  1704. .air_option = 1,
  1705. .phantom_count = 2,
  1706. .inputs_per_phantom = 1,
  1707. .gain_input_count = 2,
  1708. .safe_input_count = 2,
  1709. .dsp_count = 2,
  1710. .port_count = {
  1711. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  1712. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 6 },
  1713. [SCARLETT2_PORT_TYPE_MIX] = { 8, 12 },
  1714. [SCARLETT2_PORT_TYPE_PCM] = { 6, 6 },
  1715. },
  1716. .mux_assignment = { {
  1717. { SCARLETT2_PORT_TYPE_MIX, 10, 2 },
  1718. { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
  1719. { SCARLETT2_PORT_TYPE_MIX, 0, 10 },
  1720. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
  1721. { 0, 0, 0 },
  1722. }, {
  1723. { SCARLETT2_PORT_TYPE_MIX, 10, 2 },
  1724. { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
  1725. { SCARLETT2_PORT_TYPE_MIX, 0, 10 },
  1726. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
  1727. { 0, 0, 0 },
  1728. }, {
  1729. { SCARLETT2_PORT_TYPE_MIX, 10, 2 },
  1730. { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
  1731. { SCARLETT2_PORT_TYPE_MIX, 0, 10 },
  1732. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
  1733. { 0, 0, 0 },
  1734. } },
  1735. .meter_map = {
  1736. { 16, 8 },
  1737. { 6, 10 },
  1738. { 0, 6 },
  1739. { 0, 0 }
  1740. }
  1741. };
  1742. static const struct scarlett2_device_info clarett_2pre_info = {
  1743. .config_set = &scarlett2_config_set_clarett,
  1744. .level_input_count = 2,
  1745. .air_input_count = 2,
  1746. .line_out_descrs = {
  1747. "Monitor L",
  1748. "Monitor R",
  1749. "Headphones L",
  1750. "Headphones R",
  1751. },
  1752. .port_count = {
  1753. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  1754. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 2, 4 },
  1755. [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 0 },
  1756. [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
  1757. [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
  1758. [SCARLETT2_PORT_TYPE_PCM] = { 4, 12 },
  1759. },
  1760. .mux_assignment = { {
  1761. { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
  1762. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  1763. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  1764. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  1765. { 0, 0, 0 },
  1766. }, {
  1767. { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
  1768. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  1769. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  1770. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  1771. { 0, 0, 0 },
  1772. }, {
  1773. { SCARLETT2_PORT_TYPE_PCM, 0, 2 },
  1774. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  1775. { SCARLETT2_PORT_TYPE_NONE, 0, 26 },
  1776. { 0, 0, 0 },
  1777. } },
  1778. .meter_map = {
  1779. { 22, 12 },
  1780. { 0, 22 },
  1781. { 0, 0 }
  1782. }
  1783. };
  1784. static const u8 scarlett2_spdif_clarett_values[] = { 0, 1, 2, 0xff };
  1785. static const char * const scarlett2_spdif_clarett_texts[] = {
  1786. "None",
  1787. "Optical",
  1788. "RCA",
  1789. NULL
  1790. };
  1791. static const struct scarlett2_device_info clarett_4pre_info = {
  1792. .config_set = &scarlett2_config_set_clarett,
  1793. .level_input_count = 2,
  1794. .air_input_count = 4,
  1795. .spdif_mode_control_name = "S/PDIF Source Capture Enum",
  1796. .spdif_mode_values = scarlett2_spdif_clarett_values,
  1797. .spdif_mode_texts = scarlett2_spdif_clarett_texts,
  1798. .line_out_descrs = {
  1799. "Monitor L",
  1800. "Monitor R",
  1801. "Headphones 1 L",
  1802. "Headphones 1 R",
  1803. "Headphones 2 L",
  1804. "Headphones 2 R",
  1805. },
  1806. .port_count = {
  1807. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  1808. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 },
  1809. [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
  1810. [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
  1811. [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
  1812. [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 },
  1813. },
  1814. .mux_assignment = { {
  1815. { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
  1816. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
  1817. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1818. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  1819. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  1820. { 0, 0, 0 },
  1821. }, {
  1822. { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
  1823. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
  1824. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1825. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  1826. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  1827. { 0, 0, 0 },
  1828. }, {
  1829. { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
  1830. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
  1831. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1832. { SCARLETT2_PORT_TYPE_NONE, 0, 24 },
  1833. { 0, 0, 0 },
  1834. } },
  1835. .meter_map = {
  1836. { 26, 18 },
  1837. { 0, 26 },
  1838. { 0, 0 }
  1839. }
  1840. };
  1841. static const struct scarlett2_device_info clarett_8pre_info = {
  1842. .config_set = &scarlett2_config_set_clarett,
  1843. .level_input_count = 2,
  1844. .air_input_count = 8,
  1845. .spdif_mode_control_name = "S/PDIF Source Capture Enum",
  1846. .spdif_mode_values = scarlett2_spdif_clarett_values,
  1847. .spdif_mode_texts = scarlett2_spdif_clarett_texts,
  1848. .line_out_descrs = {
  1849. "Monitor L",
  1850. "Monitor R",
  1851. NULL,
  1852. NULL,
  1853. NULL,
  1854. NULL,
  1855. "Headphones 1 L",
  1856. "Headphones 1 R",
  1857. "Headphones 2 L",
  1858. "Headphones 2 R",
  1859. },
  1860. .port_count = {
  1861. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  1862. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
  1863. [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
  1864. [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
  1865. [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
  1866. [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
  1867. },
  1868. .mux_assignment = { {
  1869. { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
  1870. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
  1871. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1872. { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
  1873. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  1874. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  1875. { 0, 0, 0 },
  1876. }, {
  1877. { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
  1878. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
  1879. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1880. { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
  1881. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  1882. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  1883. { 0, 0, 0 },
  1884. }, {
  1885. { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
  1886. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
  1887. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  1888. { SCARLETT2_PORT_TYPE_NONE, 0, 22 },
  1889. { 0, 0, 0 },
  1890. } },
  1891. .meter_map = {
  1892. { 38, 18 },
  1893. { 0, 38 },
  1894. { 0, 0 }
  1895. }
  1896. };
  1897. struct scarlett2_device_entry {
  1898. const u32 usb_id; /* USB device identifier */
  1899. const struct scarlett2_device_info *info;
  1900. const char *series_name;
  1901. };
  1902. static const struct scarlett2_device_entry scarlett2_devices[] = {
  1903. /* Supported Gen 2 devices */
  1904. { USB_ID(0x1235, 0x8203), &s6i6_gen2_info, "Scarlett Gen 2" },
  1905. { USB_ID(0x1235, 0x8204), &s18i8_gen2_info, "Scarlett Gen 2" },
  1906. { USB_ID(0x1235, 0x8201), &s18i20_gen2_info, "Scarlett Gen 2" },
  1907. /* Supported Gen 3 devices */
  1908. { USB_ID(0x1235, 0x8211), &solo_gen3_info, "Scarlett Gen 3" },
  1909. { USB_ID(0x1235, 0x8210), &s2i2_gen3_info, "Scarlett Gen 3" },
  1910. { USB_ID(0x1235, 0x8212), &s4i4_gen3_info, "Scarlett Gen 3" },
  1911. { USB_ID(0x1235, 0x8213), &s8i6_gen3_info, "Scarlett Gen 3" },
  1912. { USB_ID(0x1235, 0x8214), &s18i8_gen3_info, "Scarlett Gen 3" },
  1913. { USB_ID(0x1235, 0x8215), &s18i20_gen3_info, "Scarlett Gen 3" },
  1914. /* Supported Vocaster devices */
  1915. { USB_ID(0x1235, 0x8216), &vocaster_one_info, "Vocaster" },
  1916. { USB_ID(0x1235, 0x8217), &vocaster_two_info, "Vocaster" },
  1917. /* Supported Gen 4 devices */
  1918. { USB_ID(0x1235, 0x8218), &solo_gen4_info, "Scarlett Gen 4" },
  1919. { USB_ID(0x1235, 0x8219), &s2i2_gen4_info, "Scarlett Gen 4" },
  1920. { USB_ID(0x1235, 0x821a), &s4i4_gen4_info, "Scarlett Gen 4" },
  1921. /* Supported Clarett USB/Clarett+ devices */
  1922. { USB_ID(0x1235, 0x8206), &clarett_2pre_info, "Clarett USB" },
  1923. { USB_ID(0x1235, 0x8207), &clarett_4pre_info, "Clarett USB" },
  1924. { USB_ID(0x1235, 0x8208), &clarett_8pre_info, "Clarett USB" },
  1925. { USB_ID(0x1235, 0x820a), &clarett_2pre_info, "Clarett+" },
  1926. { USB_ID(0x1235, 0x820b), &clarett_4pre_info, "Clarett+" },
  1927. { USB_ID(0x1235, 0x820c), &clarett_8pre_info, "Clarett+" },
  1928. /* End of list */
  1929. { 0, NULL },
  1930. };
  1931. /* get the starting port index number for a given port type/direction */
  1932. static int scarlett2_get_port_start_num(
  1933. const int port_count[][SCARLETT2_PORT_DIRNS],
  1934. int direction, int port_type)
  1935. {
  1936. int i, num = 0;
  1937. for (i = 0; i < port_type; i++)
  1938. num += port_count[i][direction];
  1939. return num;
  1940. }
  1941. /*** USB Interactions ***/
  1942. /* Commands for sending/receiving requests/responses */
  1943. #define SCARLETT2_USB_CMD_INIT 0
  1944. #define SCARLETT2_USB_CMD_REQ 2
  1945. #define SCARLETT2_USB_CMD_RESP 3
  1946. #define SCARLETT2_USB_INIT_1 0x00000000
  1947. #define SCARLETT2_USB_INIT_2 0x00000002
  1948. #define SCARLETT2_USB_REBOOT 0x00000003
  1949. #define SCARLETT2_USB_GET_METER 0x00001001
  1950. #define SCARLETT2_USB_GET_MIX 0x00002001
  1951. #define SCARLETT2_USB_SET_MIX 0x00002002
  1952. #define SCARLETT2_USB_GET_MUX 0x00003001
  1953. #define SCARLETT2_USB_SET_MUX 0x00003002
  1954. #define SCARLETT2_USB_INFO_FLASH 0x00004000
  1955. #define SCARLETT2_USB_INFO_SEGMENT 0x00004001
  1956. #define SCARLETT2_USB_ERASE_SEGMENT 0x00004002
  1957. #define SCARLETT2_USB_GET_ERASE 0x00004003
  1958. #define SCARLETT2_USB_WRITE_SEGMENT 0x00004004
  1959. #define SCARLETT2_USB_READ_SEGMENT 0x00004005
  1960. #define SCARLETT2_USB_GET_SYNC 0x00006004
  1961. #define SCARLETT2_USB_GET_DATA 0x00800000
  1962. #define SCARLETT2_USB_SET_DATA 0x00800001
  1963. #define SCARLETT2_USB_DATA_CMD 0x00800002
  1964. #define SCARLETT2_USB_CONFIG_SAVE 6
  1965. #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
  1966. #define SCARLETT2_FLASH_BLOCK_SIZE 4096
  1967. #define SCARLETT2_FLASH_RW_MAX 1024
  1968. #define SCARLETT2_SEGMENT_NUM_MIN 1
  1969. #define SCARLETT2_SEGMENT_NUM_MAX 4
  1970. #define SCARLETT2_SEGMENT_SETTINGS_NAME "App_Settings"
  1971. #define SCARLETT2_SEGMENT_FIRMWARE_NAME "App_Upgrade"
  1972. /* proprietary request/response format */
  1973. struct scarlett2_usb_packet {
  1974. __le32 cmd;
  1975. __le16 size;
  1976. __le16 seq;
  1977. __le32 error;
  1978. __le32 pad;
  1979. u8 data[];
  1980. };
  1981. static void scarlett2_fill_request_header(struct scarlett2_data *private,
  1982. struct scarlett2_usb_packet *req,
  1983. u32 cmd, u16 req_size)
  1984. {
  1985. /* sequence must go up by 1 for each request */
  1986. u16 seq = private->scarlett2_seq++;
  1987. req->cmd = cpu_to_le32(cmd);
  1988. req->size = cpu_to_le16(req_size);
  1989. req->seq = cpu_to_le16(seq);
  1990. req->error = 0;
  1991. req->pad = 0;
  1992. }
  1993. static int scarlett2_usb_tx(struct usb_device *dev, int interface,
  1994. void *buf, u16 size)
  1995. {
  1996. return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
  1997. SCARLETT2_USB_CMD_REQ,
  1998. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
  1999. 0, interface, buf, size);
  2000. }
  2001. static int scarlett2_usb_rx(struct usb_device *dev, int interface,
  2002. u32 usb_req, void *buf, u16 size)
  2003. {
  2004. return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
  2005. usb_req,
  2006. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  2007. 0, interface, buf, size);
  2008. }
  2009. /* Send a proprietary format request to the Scarlett interface */
  2010. static int scarlett2_usb(
  2011. struct usb_mixer_interface *mixer, u32 cmd,
  2012. void *req_data, u16 req_size, void *resp_data, u16 resp_size)
  2013. {
  2014. struct scarlett2_data *private = mixer->private_data;
  2015. struct usb_device *dev = mixer->chip->dev;
  2016. struct scarlett2_usb_packet *req, *resp = NULL;
  2017. size_t req_buf_size = struct_size(req, data, req_size);
  2018. size_t resp_buf_size = struct_size(resp, data, resp_size);
  2019. int err;
  2020. req = kmalloc(req_buf_size, GFP_KERNEL);
  2021. if (!req) {
  2022. err = -ENOMEM;
  2023. goto error;
  2024. }
  2025. resp = kmalloc(resp_buf_size, GFP_KERNEL);
  2026. if (!resp) {
  2027. err = -ENOMEM;
  2028. goto error;
  2029. }
  2030. mutex_lock(&private->usb_mutex);
  2031. /* build request message and send it */
  2032. scarlett2_fill_request_header(private, req, cmd, req_size);
  2033. if (req_size)
  2034. memcpy(req->data, req_data, req_size);
  2035. err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
  2036. req, req_buf_size);
  2037. if (err != req_buf_size) {
  2038. usb_audio_err(
  2039. mixer->chip,
  2040. "%s USB request result cmd %x was %d\n",
  2041. private->series_name, cmd, err);
  2042. err = -EINVAL;
  2043. goto unlock;
  2044. }
  2045. if (!wait_for_completion_timeout(&private->cmd_done,
  2046. msecs_to_jiffies(1000))) {
  2047. usb_audio_err(
  2048. mixer->chip,
  2049. "%s USB request timed out, cmd %x\n",
  2050. private->series_name, cmd);
  2051. err = -ETIMEDOUT;
  2052. goto unlock;
  2053. }
  2054. /* send a second message to get the response */
  2055. err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
  2056. SCARLETT2_USB_CMD_RESP,
  2057. resp, resp_buf_size);
  2058. /* validate the response */
  2059. if (err != resp_buf_size) {
  2060. /* ESHUTDOWN and EPROTO are valid responses to a
  2061. * reboot request
  2062. */
  2063. if (cmd == SCARLETT2_USB_REBOOT &&
  2064. (err == -ESHUTDOWN || err == -EPROTO)) {
  2065. err = 0;
  2066. goto unlock;
  2067. }
  2068. usb_audio_err(
  2069. mixer->chip,
  2070. "%s USB response result cmd %x was %d expected %zu\n",
  2071. private->series_name, cmd, err, resp_buf_size);
  2072. err = -EINVAL;
  2073. goto unlock;
  2074. }
  2075. /* cmd/seq/size should match except when initialising
  2076. * seq sent = 1, response = 0
  2077. */
  2078. if (resp->cmd != req->cmd ||
  2079. (resp->seq != req->seq &&
  2080. (le16_to_cpu(req->seq) != 1 || resp->seq != 0)) ||
  2081. resp_size != le16_to_cpu(resp->size) ||
  2082. resp->error ||
  2083. resp->pad) {
  2084. usb_audio_err(
  2085. mixer->chip,
  2086. "%s USB invalid response; "
  2087. "cmd tx/rx %d/%d seq %d/%d size %d/%d "
  2088. "error %d pad %d\n",
  2089. private->series_name,
  2090. le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
  2091. le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
  2092. resp_size, le16_to_cpu(resp->size),
  2093. le32_to_cpu(resp->error),
  2094. le32_to_cpu(resp->pad));
  2095. err = -EINVAL;
  2096. goto unlock;
  2097. }
  2098. if (resp_data && resp_size > 0)
  2099. memcpy(resp_data, resp->data, resp_size);
  2100. unlock:
  2101. mutex_unlock(&private->usb_mutex);
  2102. error:
  2103. kfree(req);
  2104. kfree(resp);
  2105. return err;
  2106. }
  2107. /* Send a USB message to get data; result placed in *buf */
  2108. static int scarlett2_usb_get(
  2109. struct usb_mixer_interface *mixer,
  2110. int offset, void *buf, int size)
  2111. {
  2112. struct {
  2113. __le32 offset;
  2114. __le32 size;
  2115. } __packed req;
  2116. req.offset = cpu_to_le32(offset);
  2117. req.size = cpu_to_le32(size);
  2118. return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
  2119. &req, sizeof(req), buf, size);
  2120. }
  2121. /* Return true if the given configuration item is present in the
  2122. * configuration set used by this device.
  2123. */
  2124. static int scarlett2_has_config_item(
  2125. struct scarlett2_data *private, int config_item_num)
  2126. {
  2127. return !!private->config_set->items[config_item_num].offset;
  2128. }
  2129. /* Send a USB message to get configuration parameters; result placed in *buf */
  2130. static int scarlett2_usb_get_config(
  2131. struct usb_mixer_interface *mixer,
  2132. int config_item_num, int count, void *buf)
  2133. {
  2134. struct scarlett2_data *private = mixer->private_data;
  2135. const struct scarlett2_config *config_item =
  2136. &private->config_set->items[config_item_num];
  2137. int size, err, i;
  2138. u8 *buf_8;
  2139. u8 value;
  2140. /* Check that the configuration item is present in the
  2141. * configuration set used by this device
  2142. */
  2143. if (!config_item->offset)
  2144. return -EFAULT;
  2145. /* Writes to the parameter buffer are always 1 byte */
  2146. size = config_item->size ? config_item->size : 8;
  2147. /* For byte-sized parameters, retrieve directly into buf */
  2148. if (size >= 8) {
  2149. size = size / 8 * count;
  2150. err = scarlett2_usb_get(mixer, config_item->offset, buf, size);
  2151. if (err < 0)
  2152. return err;
  2153. if (size == 2) {
  2154. u16 *buf_16 = buf;
  2155. for (i = 0; i < count; i++, buf_16++)
  2156. *buf_16 = le16_to_cpu(*(__le16 *)buf_16);
  2157. } else if (size == 4) {
  2158. u32 *buf_32 = buf;
  2159. for (i = 0; i < count; i++, buf_32++)
  2160. *buf_32 = le32_to_cpu(*(__le32 *)buf_32);
  2161. }
  2162. return 0;
  2163. }
  2164. /* For bit-sized parameters, retrieve into value */
  2165. err = scarlett2_usb_get(mixer, config_item->offset, &value, 1);
  2166. if (err < 0)
  2167. return err;
  2168. /* then unpack from value into buf[] */
  2169. buf_8 = buf;
  2170. for (i = 0; i < 8 && i < count; i++, value >>= 1)
  2171. *buf_8++ = value & 1;
  2172. return 0;
  2173. }
  2174. /* Send a SCARLETT2_USB_SET_DATA command.
  2175. * offset: location in the device's data space
  2176. * size: size in bytes of the value (1, 2, 4)
  2177. */
  2178. static int scarlett2_usb_set_data(
  2179. struct usb_mixer_interface *mixer,
  2180. int offset, int size, int value)
  2181. {
  2182. struct scarlett2_data *private = mixer->private_data;
  2183. struct {
  2184. __le32 offset;
  2185. __le32 size;
  2186. __le32 value;
  2187. } __packed req;
  2188. req.offset = cpu_to_le32(offset);
  2189. req.size = cpu_to_le32(size);
  2190. req.value = cpu_to_le32(value);
  2191. return scarlett2_usb(private->mixer, SCARLETT2_USB_SET_DATA,
  2192. &req, sizeof(u32) * 2 + size, NULL, 0);
  2193. }
  2194. /* Send a SCARLETT2_USB_SET_DATA command with multiple values.
  2195. * offset: location in the device's data space
  2196. * size: size in bytes of each value (1, 2, 4)
  2197. * count: number of values
  2198. */
  2199. static int scarlett2_usb_set_data_buf(
  2200. struct usb_mixer_interface *mixer,
  2201. int offset, int size, int count, void *buf)
  2202. {
  2203. struct scarlett2_data *private = mixer->private_data;
  2204. int bytes = size * count;
  2205. struct {
  2206. __le32 offset;
  2207. __le32 size;
  2208. u8 data[];
  2209. } __packed *req;
  2210. int err;
  2211. int buf_size = struct_size(req, data, bytes);
  2212. req = kmalloc(buf_size, GFP_KERNEL);
  2213. if (!req)
  2214. return -ENOMEM;
  2215. req->offset = cpu_to_le32(offset);
  2216. req->size = cpu_to_le32(bytes);
  2217. if (size == 1) {
  2218. memcpy(req->data, buf, count);
  2219. } else if (size == 2) {
  2220. u16 *buf_16 = buf;
  2221. int i;
  2222. for (i = 0; i < count; i++)
  2223. ((__le16 *)req->data)[i] = cpu_to_le16(buf_16[i]);
  2224. } else {
  2225. u32 *buf_32 = buf;
  2226. int i;
  2227. for (i = 0; i < count; i++)
  2228. ((__le32 *)req->data)[i] = cpu_to_le32(buf_32[i]);
  2229. }
  2230. err = scarlett2_usb(private->mixer, SCARLETT2_USB_SET_DATA,
  2231. req, buf_size, NULL, 0);
  2232. kfree(req);
  2233. return err;
  2234. }
  2235. /* Send a SCARLETT2_USB_DATA_CMD command.
  2236. * Configuration changes require activation with this after they have
  2237. * been uploaded by a previous SCARLETT2_USB_SET_DATA.
  2238. * The value for activate needed is determined by the configuration
  2239. * item.
  2240. */
  2241. static int scarlett2_usb_activate_config(
  2242. struct usb_mixer_interface *mixer, int activate)
  2243. {
  2244. __le32 req;
  2245. req = cpu_to_le32(activate);
  2246. return scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
  2247. &req, sizeof(req), NULL, 0);
  2248. }
  2249. /* Send USB messages to set a SCARLETT2_CONFIG_* parameter */
  2250. static int scarlett2_usb_set_config(
  2251. struct usb_mixer_interface *mixer,
  2252. int config_item_num, int index, int value)
  2253. {
  2254. struct scarlett2_data *private = mixer->private_data;
  2255. const struct scarlett2_config_set *config_set = private->config_set;
  2256. const struct scarlett2_config *config_item =
  2257. &config_set->items[config_item_num];
  2258. int offset, size;
  2259. int err;
  2260. /* Check that the configuration item is present in the
  2261. * configuration set used by this device
  2262. */
  2263. if (!config_item->offset)
  2264. return -EFAULT;
  2265. /* Write via the parameter buffer? */
  2266. if (config_item->pbuf) {
  2267. if (!config_set->param_buf_addr)
  2268. return -EFAULT;
  2269. /* Place index in param_buf_addr + 1 */
  2270. err = scarlett2_usb_set_data(
  2271. mixer, config_set->param_buf_addr + 1, 1, index);
  2272. if (err < 0)
  2273. return err;
  2274. /* Place value in param_buf_addr */
  2275. err = scarlett2_usb_set_data(
  2276. mixer, config_set->param_buf_addr, 1, value);
  2277. if (err < 0)
  2278. return err;
  2279. /* Activate the write through the parameter buffer */
  2280. return scarlett2_usb_activate_config(
  2281. mixer, config_item->activate);
  2282. }
  2283. /* Direct writes (not via the parameter buffer) need NVRAM
  2284. * save and support bit-modification
  2285. */
  2286. /* Cancel any pending NVRAM save */
  2287. cancel_delayed_work_sync(&private->work);
  2288. /* Convert config_item->size in bits to size in bytes and
  2289. * calculate offset
  2290. */
  2291. if (config_item->size >= 8) {
  2292. size = config_item->size / 8;
  2293. offset = config_item->offset + index * size;
  2294. /* If updating a bit, retrieve the old value, set/clear the
  2295. * bit as needed, and update value
  2296. */
  2297. } else {
  2298. u8 tmp;
  2299. size = 1;
  2300. offset = config_item->offset;
  2301. err = scarlett2_usb_get(mixer, offset, &tmp, 1);
  2302. if (err < 0)
  2303. return err;
  2304. if (value)
  2305. tmp |= (1 << index);
  2306. else
  2307. tmp &= ~(1 << index);
  2308. value = tmp;
  2309. }
  2310. /* Write the new value */
  2311. err = scarlett2_usb_set_data(mixer, offset, size, value);
  2312. if (err < 0)
  2313. return err;
  2314. /* Activate the change */
  2315. err = scarlett2_usb_activate_config(mixer, config_item->activate);
  2316. if (err < 0)
  2317. return err;
  2318. /* Interfaces with parameter buffer writes don't need a
  2319. * separate save step
  2320. */
  2321. if (config_set->param_buf_addr)
  2322. return 0;
  2323. /* Schedule the change to be written to NVRAM */
  2324. if (config_item->activate != SCARLETT2_USB_CONFIG_SAVE)
  2325. schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
  2326. return 0;
  2327. }
  2328. /* Send USB messages to set a SCARLETT2_CONFIG_* parameter with
  2329. * multiple values
  2330. */
  2331. static int scarlett2_usb_set_config_buf(
  2332. struct usb_mixer_interface *mixer,
  2333. int config_item_num, int index, int count, void *buf)
  2334. {
  2335. struct scarlett2_data *private = mixer->private_data;
  2336. const struct scarlett2_config_set *config_set = private->config_set;
  2337. const struct scarlett2_config *config_item =
  2338. &config_set->items[config_item_num];
  2339. int offset, size;
  2340. int err;
  2341. /* Check that the configuration item is present in the
  2342. * configuration set used by this device
  2343. */
  2344. if (!config_item->offset)
  2345. return -EFAULT;
  2346. /* Convert config_item->size in bits to size in bytes and
  2347. * calculate offset
  2348. */
  2349. if (config_item->size >= 8) {
  2350. size = config_item->size / 8;
  2351. offset = config_item->offset + index * size;
  2352. /* Bit updates not supported */
  2353. } else {
  2354. return -EFAULT;
  2355. }
  2356. /* Write the new values */
  2357. err = scarlett2_usb_set_data_buf(mixer, offset, size, count, buf);
  2358. if (err < 0)
  2359. return err;
  2360. /* Activate the change */
  2361. return scarlett2_usb_activate_config(mixer, config_item->activate);
  2362. }
  2363. /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
  2364. static void scarlett2_config_save(struct usb_mixer_interface *mixer)
  2365. {
  2366. int err;
  2367. err = scarlett2_usb_activate_config(mixer, SCARLETT2_USB_CONFIG_SAVE);
  2368. if (err < 0)
  2369. usb_audio_err(mixer->chip, "config save failed: %d\n", err);
  2370. }
  2371. /* Delayed work to save config */
  2372. static void scarlett2_config_save_work(struct work_struct *work)
  2373. {
  2374. struct scarlett2_data *private =
  2375. container_of(work, struct scarlett2_data, work.work);
  2376. scarlett2_config_save(private->mixer);
  2377. }
  2378. /* Send a USB message to get sync status; result placed in *sync */
  2379. static int scarlett2_usb_get_sync_status(
  2380. struct usb_mixer_interface *mixer,
  2381. u8 *sync)
  2382. {
  2383. __le32 data;
  2384. int err;
  2385. err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
  2386. NULL, 0, &data, sizeof(data));
  2387. if (err < 0)
  2388. return err;
  2389. *sync = !!data;
  2390. return 0;
  2391. }
  2392. /* Return true if the device has a mixer that we can control */
  2393. static int scarlett2_has_mixer(struct scarlett2_data *private)
  2394. {
  2395. return !!private->info->mux_assignment[0][0].count;
  2396. }
  2397. /* Map from mixer value to (db + 80) * 2
  2398. * (reverse of scarlett2_mixer_values[])
  2399. */
  2400. static int scarlett2_mixer_value_to_db(int value)
  2401. {
  2402. int i;
  2403. for (i = 0; i < SCARLETT2_MIXER_VALUE_COUNT; i++)
  2404. if (scarlett2_mixer_values[i] >= value)
  2405. return i;
  2406. return SCARLETT2_MIXER_MAX_VALUE;
  2407. }
  2408. /* Send a USB message to get the volumes for all inputs of one mix
  2409. * and put the values into private->mix[]
  2410. */
  2411. static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
  2412. int mix_num)
  2413. {
  2414. struct scarlett2_data *private = mixer->private_data;
  2415. int num_mixer_in = private->num_mix_in;
  2416. int err, i, j;
  2417. struct {
  2418. __le16 mix_num;
  2419. __le16 count;
  2420. } __packed req;
  2421. __le16 data[SCARLETT2_INPUT_MIX_MAX];
  2422. req.mix_num = cpu_to_le16(mix_num);
  2423. req.count = cpu_to_le16(num_mixer_in);
  2424. err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
  2425. &req, sizeof(req),
  2426. data, num_mixer_in * sizeof(u16));
  2427. if (err < 0)
  2428. return err;
  2429. for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
  2430. private->mix[j] = scarlett2_mixer_value_to_db(
  2431. le16_to_cpu(data[i]));
  2432. return 0;
  2433. }
  2434. /* Send a USB message to set the volumes for all inputs of one mix
  2435. * (values obtained from private->mix[])
  2436. */
  2437. static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
  2438. int mix_num)
  2439. {
  2440. struct scarlett2_data *private = mixer->private_data;
  2441. struct {
  2442. __le16 mix_num;
  2443. __le16 data[SCARLETT2_INPUT_MIX_MAX];
  2444. } __packed req;
  2445. int i, j;
  2446. int num_mixer_in = private->num_mix_in;
  2447. req.mix_num = cpu_to_le16(mix_num);
  2448. for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
  2449. req.data[i] = cpu_to_le16(
  2450. scarlett2_mixer_values[private->mix[j]]
  2451. );
  2452. return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
  2453. &req, (num_mixer_in + 1) * sizeof(u16),
  2454. NULL, 0);
  2455. }
  2456. /* Convert a port number index (per info->port_count) to a hardware ID */
  2457. static u32 scarlett2_mux_src_num_to_id(
  2458. const int port_count[][SCARLETT2_PORT_DIRNS], int num)
  2459. {
  2460. int port_type;
  2461. for (port_type = 0;
  2462. port_type < SCARLETT2_PORT_TYPE_COUNT;
  2463. port_type++) {
  2464. if (num < port_count[port_type][SCARLETT2_PORT_IN])
  2465. return scarlett2_ports[port_type].id | num;
  2466. num -= port_count[port_type][SCARLETT2_PORT_IN];
  2467. }
  2468. /* Oops */
  2469. return 0;
  2470. }
  2471. /* Convert a hardware ID to a port number index */
  2472. static u32 scarlett2_mux_id_to_num(
  2473. const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
  2474. {
  2475. int port_type;
  2476. int port_num = 0;
  2477. for (port_type = 0;
  2478. port_type < SCARLETT2_PORT_TYPE_COUNT;
  2479. port_type++) {
  2480. int base = scarlett2_ports[port_type].id;
  2481. int count = port_count[port_type][direction];
  2482. if (id >= base && id < base + count)
  2483. return port_num + id - base;
  2484. port_num += count;
  2485. }
  2486. /* Oops */
  2487. return -1;
  2488. }
  2489. /* Convert one mux entry from the interface and load into private->mux[] */
  2490. static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
  2491. u32 mux_entry)
  2492. {
  2493. const struct scarlett2_device_info *info = private->info;
  2494. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  2495. int dst_idx, src_idx;
  2496. dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
  2497. mux_entry & 0xFFF);
  2498. if (dst_idx < 0)
  2499. return;
  2500. if (dst_idx >= private->num_mux_dsts) {
  2501. usb_audio_err(private->mixer->chip,
  2502. "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
  2503. mux_entry, dst_idx, private->num_mux_dsts);
  2504. return;
  2505. }
  2506. src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
  2507. mux_entry >> 12);
  2508. if (src_idx < 0)
  2509. return;
  2510. if (src_idx >= private->num_mux_srcs) {
  2511. usb_audio_err(private->mixer->chip,
  2512. "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
  2513. mux_entry, src_idx, private->num_mux_srcs);
  2514. return;
  2515. }
  2516. private->mux[dst_idx] = src_idx;
  2517. }
  2518. /* Update the meter level map
  2519. *
  2520. * The meter level data from the interface (SCARLETT2_USB_GET_METER
  2521. * request) is returned in mux_assignment order, but to avoid exposing
  2522. * that to userspace, scarlett2_meter_ctl_get() rearranges the data
  2523. * into scarlett2_ports order using the meter_level_map[] array which
  2524. * is set up by this function.
  2525. *
  2526. * In addition, the meter level data values returned from the
  2527. * interface are invalid for destinations where:
  2528. *
  2529. * - the source is "Off"; therefore we set those values to zero (map
  2530. * value of 255)
  2531. *
  2532. * - the source is assigned to a previous (with respect to the
  2533. * mux_assignment order) destination; therefore we set those values
  2534. * to the value previously reported for that source
  2535. */
  2536. static void scarlett2_update_meter_level_map(struct scarlett2_data *private)
  2537. {
  2538. const struct scarlett2_device_info *info = private->info;
  2539. const struct scarlett2_meter_entry *entry;
  2540. /* sources already assigned to a destination
  2541. * value is 255 for None, otherwise the value of i
  2542. * (index into array returned by
  2543. * scarlett2_usb_get_meter_levels())
  2544. */
  2545. u8 seen_src[SCARLETT2_MAX_SRCS] = { 1 };
  2546. u8 seen_src_value[SCARLETT2_MAX_SRCS] = { 255 };
  2547. /* index in meter_map[] order */
  2548. int i = 0;
  2549. /* go through the meter_map[] entries */
  2550. for (entry = info->meter_map;
  2551. entry->count;
  2552. entry++) {
  2553. /* fill in each meter_level_map[] entry */
  2554. int j, mux_idx;
  2555. for (j = 0, mux_idx = entry->start;
  2556. j < entry->count;
  2557. i++, j++, mux_idx++) {
  2558. /* convert mux_idx using line_out_unmap[] */
  2559. int map_mux_idx = (
  2560. info->line_out_remap_enable &&
  2561. mux_idx < private->num_line_out
  2562. ) ? info->line_out_unmap[mux_idx]
  2563. : mux_idx;
  2564. /* check which source is connected, and if
  2565. * that source is already connected elsewhere,
  2566. * use that existing connection's destination
  2567. * for this meter entry instead
  2568. */
  2569. int mux_src = private->mux[mux_idx];
  2570. if (!seen_src[mux_src]) {
  2571. seen_src[mux_src] = 1;
  2572. seen_src_value[mux_src] = i;
  2573. }
  2574. private->meter_level_map[map_mux_idx] =
  2575. seen_src_value[mux_src];
  2576. }
  2577. }
  2578. }
  2579. /* Send USB message to get mux inputs and then populate private->mux[] */
  2580. static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
  2581. {
  2582. struct scarlett2_data *private = mixer->private_data;
  2583. int count = private->num_mux_dsts;
  2584. int err, i;
  2585. struct {
  2586. __le16 num;
  2587. __le16 count;
  2588. } __packed req;
  2589. __le32 data[SCARLETT2_MUX_MAX];
  2590. private->mux_updated = 0;
  2591. req.num = 0;
  2592. req.count = cpu_to_le16(count);
  2593. err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
  2594. &req, sizeof(req),
  2595. data, count * sizeof(u32));
  2596. if (err < 0)
  2597. return err;
  2598. for (i = 0; i < count; i++)
  2599. scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
  2600. scarlett2_update_meter_level_map(private);
  2601. return 0;
  2602. }
  2603. /* Send USB messages to set mux inputs */
  2604. static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
  2605. {
  2606. struct scarlett2_data *private = mixer->private_data;
  2607. const struct scarlett2_device_info *info = private->info;
  2608. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  2609. int table;
  2610. struct {
  2611. __le16 pad;
  2612. __le16 num;
  2613. __le32 data[SCARLETT2_MUX_MAX];
  2614. } __packed req;
  2615. req.pad = 0;
  2616. /* set mux settings for each rate */
  2617. for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
  2618. const struct scarlett2_mux_entry *entry;
  2619. /* i counts over the output array */
  2620. int i = 0, err;
  2621. req.num = cpu_to_le16(table);
  2622. /* loop through each entry */
  2623. for (entry = info->mux_assignment[table];
  2624. entry->count;
  2625. entry++) {
  2626. int j;
  2627. int port_type = entry->port_type;
  2628. int port_idx = entry->start;
  2629. int mux_idx = scarlett2_get_port_start_num(port_count,
  2630. SCARLETT2_PORT_OUT, port_type) + port_idx;
  2631. int dst_id = scarlett2_ports[port_type].id + port_idx;
  2632. /* Empty slots */
  2633. if (!dst_id) {
  2634. for (j = 0; j < entry->count; j++)
  2635. req.data[i++] = 0;
  2636. continue;
  2637. }
  2638. /* Non-empty mux slots use the lower 12 bits
  2639. * for the destination and next 12 bits for
  2640. * the source
  2641. */
  2642. for (j = 0; j < entry->count; j++) {
  2643. int src_id = scarlett2_mux_src_num_to_id(
  2644. port_count, private->mux[mux_idx++]);
  2645. req.data[i++] = cpu_to_le32(dst_id |
  2646. src_id << 12);
  2647. dst_id++;
  2648. }
  2649. }
  2650. err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
  2651. &req, (i + 1) * sizeof(u32),
  2652. NULL, 0);
  2653. if (err < 0)
  2654. return err;
  2655. }
  2656. scarlett2_update_meter_level_map(private);
  2657. return 0;
  2658. }
  2659. /* Send USB message to get meter levels */
  2660. static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
  2661. u16 num_meters, u16 *levels)
  2662. {
  2663. struct {
  2664. __le16 pad;
  2665. __le16 num_meters;
  2666. __le32 magic;
  2667. } __packed req;
  2668. __le32 resp[SCARLETT2_MAX_METERS];
  2669. int i, err;
  2670. req.pad = 0;
  2671. req.num_meters = cpu_to_le16(num_meters);
  2672. req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
  2673. err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
  2674. &req, sizeof(req), resp, num_meters * sizeof(u32));
  2675. if (err < 0)
  2676. return err;
  2677. /* copy, convert to u16 */
  2678. for (i = 0; i < num_meters; i++)
  2679. levels[i] = le32_to_cpu(resp[i]);
  2680. return 0;
  2681. }
  2682. /* For config items with mute=1, xor bits 0 & 1 together to get the
  2683. * current/next state. This won't have any effect on values which are
  2684. * only ever 0/1.
  2685. */
  2686. static uint8_t scarlett2_decode_muteable(uint8_t v)
  2687. {
  2688. return (v ^ (v >> 1)) & 1;
  2689. }
  2690. /*** Control Functions ***/
  2691. /* helper function to create a new control */
  2692. static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
  2693. const struct snd_kcontrol_new *ncontrol,
  2694. int index, int channels, const char *name,
  2695. struct snd_kcontrol **kctl_return)
  2696. {
  2697. struct snd_kcontrol *kctl;
  2698. struct usb_mixer_elem_info *elem;
  2699. int err;
  2700. elem = kzalloc(sizeof(*elem), GFP_KERNEL);
  2701. if (!elem)
  2702. return -ENOMEM;
  2703. /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
  2704. * ignores them for resume and other operations.
  2705. * Also, the head.id field is set to 0, as we don't use this field.
  2706. */
  2707. elem->head.mixer = mixer;
  2708. elem->control = index;
  2709. elem->head.id = 0;
  2710. elem->channels = channels;
  2711. elem->val_type = USB_MIXER_BESPOKEN;
  2712. kctl = snd_ctl_new1(ncontrol, elem);
  2713. if (!kctl) {
  2714. kfree(elem);
  2715. return -ENOMEM;
  2716. }
  2717. kctl->private_free = snd_usb_mixer_elem_free;
  2718. strscpy(kctl->id.name, name, sizeof(kctl->id.name));
  2719. err = snd_usb_mixer_add_control(&elem->head, kctl);
  2720. if (err < 0)
  2721. return err;
  2722. if (kctl_return)
  2723. *kctl_return = kctl;
  2724. return 0;
  2725. }
  2726. /*** Firmware Version Control ***/
  2727. static int scarlett2_firmware_version_ctl_get(
  2728. struct snd_kcontrol *kctl,
  2729. struct snd_ctl_elem_value *ucontrol)
  2730. {
  2731. struct usb_mixer_elem_info *elem = kctl->private_data;
  2732. struct scarlett2_data *private = elem->head.mixer->private_data;
  2733. ucontrol->value.integer.value[0] = private->firmware_version;
  2734. return 0;
  2735. }
  2736. static int scarlett2_firmware_version_ctl_info(
  2737. struct snd_kcontrol *kctl,
  2738. struct snd_ctl_elem_info *uinfo)
  2739. {
  2740. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2741. uinfo->count = 1;
  2742. return 0;
  2743. }
  2744. static const struct snd_kcontrol_new scarlett2_firmware_version_ctl = {
  2745. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  2746. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2747. .name = "",
  2748. .info = scarlett2_firmware_version_ctl_info,
  2749. .get = scarlett2_firmware_version_ctl_get
  2750. };
  2751. static int scarlett2_add_firmware_version_ctl(
  2752. struct usb_mixer_interface *mixer)
  2753. {
  2754. return scarlett2_add_new_ctl(mixer, &scarlett2_firmware_version_ctl,
  2755. 0, 0, "Firmware Version", NULL);
  2756. }
  2757. /*** Minimum Firmware Version Control ***/
  2758. static int scarlett2_min_firmware_version_ctl_get(
  2759. struct snd_kcontrol *kctl,
  2760. struct snd_ctl_elem_value *ucontrol)
  2761. {
  2762. struct usb_mixer_elem_info *elem = kctl->private_data;
  2763. struct scarlett2_data *private = elem->head.mixer->private_data;
  2764. ucontrol->value.integer.value[0] = private->info->min_firmware_version;
  2765. return 0;
  2766. }
  2767. static int scarlett2_min_firmware_version_ctl_info(
  2768. struct snd_kcontrol *kctl,
  2769. struct snd_ctl_elem_info *uinfo)
  2770. {
  2771. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2772. uinfo->count = 1;
  2773. return 0;
  2774. }
  2775. static const struct snd_kcontrol_new scarlett2_min_firmware_version_ctl = {
  2776. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  2777. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2778. .name = "",
  2779. .info = scarlett2_min_firmware_version_ctl_info,
  2780. .get = scarlett2_min_firmware_version_ctl_get
  2781. };
  2782. static int scarlett2_add_min_firmware_version_ctl(
  2783. struct usb_mixer_interface *mixer)
  2784. {
  2785. return scarlett2_add_new_ctl(mixer, &scarlett2_min_firmware_version_ctl,
  2786. 0, 0, "Minimum Firmware Version", NULL);
  2787. }
  2788. /*** Sync Control ***/
  2789. /* Update sync control after receiving notification that the status
  2790. * has changed
  2791. */
  2792. static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
  2793. {
  2794. struct scarlett2_data *private = mixer->private_data;
  2795. private->sync_updated = 0;
  2796. return scarlett2_usb_get_sync_status(mixer, &private->sync);
  2797. }
  2798. static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
  2799. struct snd_ctl_elem_info *uinfo)
  2800. {
  2801. static const char *texts[2] = {
  2802. "Unlocked", "Locked"
  2803. };
  2804. return snd_ctl_enum_info(uinfo, 1, 2, texts);
  2805. }
  2806. static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
  2807. struct snd_ctl_elem_value *ucontrol)
  2808. {
  2809. struct usb_mixer_elem_info *elem = kctl->private_data;
  2810. struct usb_mixer_interface *mixer = elem->head.mixer;
  2811. struct scarlett2_data *private = mixer->private_data;
  2812. int err = 0;
  2813. mutex_lock(&private->data_mutex);
  2814. if (private->hwdep_in_use) {
  2815. err = -EBUSY;
  2816. goto unlock;
  2817. }
  2818. if (private->sync_updated) {
  2819. err = scarlett2_update_sync(mixer);
  2820. if (err < 0)
  2821. goto unlock;
  2822. }
  2823. ucontrol->value.enumerated.item[0] = private->sync;
  2824. unlock:
  2825. mutex_unlock(&private->data_mutex);
  2826. return err;
  2827. }
  2828. static const struct snd_kcontrol_new scarlett2_sync_ctl = {
  2829. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2830. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2831. .name = "",
  2832. .info = scarlett2_sync_ctl_info,
  2833. .get = scarlett2_sync_ctl_get
  2834. };
  2835. static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
  2836. {
  2837. struct scarlett2_data *private = mixer->private_data;
  2838. /* devices without a mixer also don't support reporting sync status */
  2839. if (!scarlett2_has_mixer(private))
  2840. return 0;
  2841. return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
  2842. 0, 1, "Sync Status", &private->sync_ctl);
  2843. }
  2844. /*** Autogain Switch and Status Controls ***/
  2845. /* Forward declarations as phantom power and autogain can disable each other */
  2846. static int scarlett2_check_input_phantom_updated(struct usb_mixer_interface *);
  2847. static int scarlett2_phantom_is_switching(struct scarlett2_data *, int);
  2848. /* Set the access mode of a control to read-only (val = 0) or
  2849. * read-write (val = 1).
  2850. */
  2851. static void scarlett2_set_ctl_access(struct snd_kcontrol *kctl, int val)
  2852. {
  2853. if (val)
  2854. kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
  2855. else
  2856. kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_WRITE;
  2857. }
  2858. /* Check if autogain is running on any input */
  2859. static int scarlett2_autogain_is_running(struct scarlett2_data *private)
  2860. {
  2861. int i;
  2862. /* autogain_status[] is 0 if autogain is running */
  2863. for (i = 0; i < private->info->gain_input_count; i++)
  2864. if (!private->autogain_status[i])
  2865. return 1;
  2866. return 0;
  2867. }
  2868. static int scarlett2_update_autogain(struct usb_mixer_interface *mixer)
  2869. {
  2870. struct scarlett2_data *private = mixer->private_data;
  2871. const struct scarlett2_device_info *info = private->info;
  2872. int err, i;
  2873. u8 raw_autogain_status[SCARLETT2_INPUT_GAIN_MAX];
  2874. s8 ag_target_values[SCARLETT2_AG_TARGET_COUNT];
  2875. private->autogain_updated = 0;
  2876. if (!info->gain_input_count)
  2877. return 0;
  2878. err = scarlett2_usb_get_config(
  2879. mixer, SCARLETT2_CONFIG_AUTOGAIN_SWITCH,
  2880. info->gain_input_count, private->autogain_switch);
  2881. if (err < 0)
  2882. return err;
  2883. err = scarlett2_usb_get_config(
  2884. mixer, SCARLETT2_CONFIG_AUTOGAIN_STATUS,
  2885. info->gain_input_count, raw_autogain_status);
  2886. if (err < 0)
  2887. return err;
  2888. /* Translate autogain_switch and raw_autogain_status into
  2889. * autogain_status.
  2890. *
  2891. * When autogain_switch[] is set, the status is the first
  2892. * element in scarlett2_autogain_status_texts[] (Running). The
  2893. * subsequent elements correspond to the status value from the
  2894. * device (raw_autogain_status[]) + 1. The last element is
  2895. * "Invalid", in case the device reports a status outside the
  2896. * range of scarlett2_autogain_status_texts[].
  2897. */
  2898. for (i = 0; i < info->gain_input_count; i++)
  2899. if (private->autogain_switch[i])
  2900. private->autogain_status[i] = 0;
  2901. else if (raw_autogain_status[i] <
  2902. private->num_autogain_status_texts - 1)
  2903. private->autogain_status[i] =
  2904. raw_autogain_status[i] + 1;
  2905. else
  2906. private->autogain_status[i] =
  2907. private->num_autogain_status_texts - 1;
  2908. for (int i = 0; i < SCARLETT2_AG_TARGET_COUNT; i++)
  2909. if (scarlett2_has_config_item(private,
  2910. scarlett2_ag_target_configs[i])) {
  2911. err = scarlett2_usb_get_config(
  2912. mixer, scarlett2_ag_target_configs[i],
  2913. 1, &ag_target_values[i]);
  2914. if (err < 0)
  2915. return err;
  2916. }
  2917. /* convert from negative dBFS as used by the device */
  2918. for (int i = 0; i < SCARLETT2_AG_TARGET_COUNT; i++)
  2919. private->ag_targets[i] = -ag_target_values[i];
  2920. return 0;
  2921. }
  2922. /* Update access mode for controls affected by autogain */
  2923. static void scarlett2_autogain_update_access(struct usb_mixer_interface *mixer)
  2924. {
  2925. struct scarlett2_data *private = mixer->private_data;
  2926. const struct scarlett2_device_info *info = private->info;
  2927. int val = !scarlett2_autogain_is_running(private);
  2928. int i;
  2929. if (scarlett2_has_config_item(private,
  2930. SCARLETT2_CONFIG_INPUT_SELECT_SWITCH))
  2931. scarlett2_set_ctl_access(private->input_select_ctl, val);
  2932. if (scarlett2_has_config_item(private,
  2933. SCARLETT2_CONFIG_INPUT_LINK_SWITCH))
  2934. for (i = 0; i < info->gain_input_count / 2; i++)
  2935. scarlett2_set_ctl_access(private->input_link_ctls[i],
  2936. val);
  2937. for (i = 0; i < info->gain_input_count; i++)
  2938. scarlett2_set_ctl_access(private->input_gain_ctls[i], val);
  2939. for (i = 0; i < info->safe_input_count; i++)
  2940. scarlett2_set_ctl_access(private->safe_ctls[i], val);
  2941. for (i = 0; i < info->level_input_count; i++)
  2942. scarlett2_set_ctl_access(private->level_ctls[i], val);
  2943. for (i = 0; i < info->air_input_count; i++)
  2944. scarlett2_set_ctl_access(private->air_ctls[i], val);
  2945. for (i = 0; i < info->mute_input_count; i++)
  2946. scarlett2_set_ctl_access(private->input_mute_ctls[i], val);
  2947. for (i = 0; i < info->phantom_count; i++)
  2948. scarlett2_set_ctl_access(private->phantom_ctls[i], val);
  2949. for (i = 0; i < info->dsp_input_count; i++)
  2950. scarlett2_set_ctl_access(private->dsp_ctls[i], val);
  2951. for (i = 0; i < SCARLETT2_AG_TARGET_COUNT; i++)
  2952. if (scarlett2_has_config_item(private,
  2953. scarlett2_ag_target_configs[i]))
  2954. scarlett2_set_ctl_access(
  2955. private->ag_target_ctls[i], val);
  2956. }
  2957. /* Notify of access mode change for all controls read-only while
  2958. * autogain runs.
  2959. */
  2960. static void scarlett2_autogain_notify_access(struct usb_mixer_interface *mixer)
  2961. {
  2962. struct snd_card *card = mixer->chip->card;
  2963. struct scarlett2_data *private = mixer->private_data;
  2964. const struct scarlett2_device_info *info = private->info;
  2965. int i;
  2966. if (scarlett2_has_config_item(private,
  2967. SCARLETT2_CONFIG_INPUT_SELECT_SWITCH))
  2968. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
  2969. &private->input_select_ctl->id);
  2970. if (scarlett2_has_config_item(private,
  2971. SCARLETT2_CONFIG_INPUT_LINK_SWITCH))
  2972. for (i = 0; i < info->gain_input_count / 2; i++)
  2973. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
  2974. &private->input_link_ctls[i]->id);
  2975. for (i = 0; i < info->gain_input_count; i++)
  2976. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
  2977. &private->input_gain_ctls[i]->id);
  2978. for (i = 0; i < info->safe_input_count; i++)
  2979. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
  2980. &private->safe_ctls[i]->id);
  2981. for (i = 0; i < info->level_input_count; i++)
  2982. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
  2983. &private->level_ctls[i]->id);
  2984. for (i = 0; i < info->air_input_count; i++)
  2985. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
  2986. &private->air_ctls[i]->id);
  2987. for (i = 0; i < info->dsp_input_count; i++)
  2988. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
  2989. &private->dsp_ctls[i]->id);
  2990. for (i = 0; i < info->mute_input_count; i++)
  2991. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
  2992. &private->input_mute_ctls[i]->id);
  2993. for (i = 0; i < info->phantom_count; i++)
  2994. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
  2995. &private->phantom_ctls[i]->id);
  2996. for (i = 0; i < SCARLETT2_AG_TARGET_COUNT; i++)
  2997. if (scarlett2_has_config_item(private,
  2998. scarlett2_ag_target_configs[i]))
  2999. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
  3000. &private->ag_target_ctls[i]->id);
  3001. }
  3002. /* Call scarlett2_update_autogain() and
  3003. * scarlett2_autogain_update_access() if autogain_updated is set.
  3004. */
  3005. static int scarlett2_check_autogain_updated(
  3006. struct usb_mixer_interface *mixer)
  3007. {
  3008. struct scarlett2_data *private = mixer->private_data;
  3009. int err;
  3010. if (!private->autogain_updated)
  3011. return 0;
  3012. err = scarlett2_update_autogain(mixer);
  3013. if (err < 0)
  3014. return err;
  3015. scarlett2_autogain_update_access(mixer);
  3016. return 0;
  3017. }
  3018. /* If autogain_updated is set when a *_ctl_put() function for a
  3019. * control that is meant to be read-only while autogain is running,
  3020. * update the autogain status and access mode of affected controls.
  3021. * Return -EPERM if autogain is running.
  3022. */
  3023. static int scarlett2_check_put_during_autogain(
  3024. struct usb_mixer_interface *mixer)
  3025. {
  3026. int err = scarlett2_check_autogain_updated(mixer);
  3027. if (err < 0)
  3028. return err;
  3029. if (scarlett2_autogain_is_running(mixer->private_data))
  3030. return -EPERM;
  3031. return 0;
  3032. }
  3033. static int scarlett2_autogain_switch_ctl_info(
  3034. struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
  3035. {
  3036. struct usb_mixer_elem_info *elem = kctl->private_data;
  3037. struct usb_mixer_interface *mixer = elem->head.mixer;
  3038. struct scarlett2_data *private = mixer->private_data;
  3039. int err;
  3040. mutex_lock(&private->data_mutex);
  3041. err = scarlett2_check_input_phantom_updated(mixer);
  3042. if (err < 0)
  3043. goto unlock;
  3044. err = snd_ctl_boolean_mono_info(kctl, uinfo);
  3045. unlock:
  3046. mutex_unlock(&private->data_mutex);
  3047. return err;
  3048. }
  3049. static int scarlett2_autogain_switch_ctl_get(
  3050. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  3051. {
  3052. struct usb_mixer_elem_info *elem = kctl->private_data;
  3053. struct usb_mixer_interface *mixer = elem->head.mixer;
  3054. struct scarlett2_data *private = mixer->private_data;
  3055. int err;
  3056. mutex_lock(&private->data_mutex);
  3057. if (private->hwdep_in_use) {
  3058. err = -EBUSY;
  3059. goto unlock;
  3060. }
  3061. err = scarlett2_check_autogain_updated(mixer);
  3062. if (err < 0)
  3063. goto unlock;
  3064. ucontrol->value.enumerated.item[0] =
  3065. private->autogain_switch[elem->control];
  3066. unlock:
  3067. mutex_unlock(&private->data_mutex);
  3068. return err;
  3069. }
  3070. static int scarlett2_autogain_status_ctl_get(
  3071. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  3072. {
  3073. struct usb_mixer_elem_info *elem = kctl->private_data;
  3074. struct usb_mixer_interface *mixer = elem->head.mixer;
  3075. struct scarlett2_data *private = mixer->private_data;
  3076. int err;
  3077. mutex_lock(&private->data_mutex);
  3078. if (private->hwdep_in_use) {
  3079. err = -EBUSY;
  3080. goto unlock;
  3081. }
  3082. err = scarlett2_check_autogain_updated(mixer);
  3083. if (err < 0)
  3084. goto unlock;
  3085. ucontrol->value.enumerated.item[0] =
  3086. private->autogain_status[elem->control];
  3087. unlock:
  3088. mutex_unlock(&private->data_mutex);
  3089. return err;
  3090. }
  3091. static int scarlett2_autogain_switch_ctl_put(
  3092. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  3093. {
  3094. struct usb_mixer_elem_info *elem = kctl->private_data;
  3095. struct usb_mixer_interface *mixer = elem->head.mixer;
  3096. struct scarlett2_data *private = mixer->private_data;
  3097. int index = elem->control;
  3098. int oval, val, err;
  3099. mutex_lock(&private->data_mutex);
  3100. if (private->hwdep_in_use) {
  3101. err = -EBUSY;
  3102. goto unlock;
  3103. }
  3104. err = scarlett2_check_input_phantom_updated(mixer);
  3105. if (err < 0)
  3106. goto unlock;
  3107. if (scarlett2_phantom_is_switching(private, index)) {
  3108. err = -EPERM;
  3109. goto unlock;
  3110. }
  3111. oval = private->autogain_switch[index];
  3112. val = !!ucontrol->value.integer.value[0];
  3113. if (oval == val)
  3114. goto unlock;
  3115. private->autogain_switch[index] = val;
  3116. /* Send switch change to the device */
  3117. err = scarlett2_usb_set_config(
  3118. mixer, SCARLETT2_CONFIG_AUTOGAIN_SWITCH, index, val);
  3119. if (err == 0)
  3120. err = 1;
  3121. scarlett2_autogain_update_access(mixer);
  3122. scarlett2_autogain_notify_access(mixer);
  3123. unlock:
  3124. mutex_unlock(&private->data_mutex);
  3125. return err;
  3126. }
  3127. static int scarlett2_autogain_status_ctl_info(
  3128. struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
  3129. {
  3130. struct usb_mixer_elem_info *elem = kctl->private_data;
  3131. struct scarlett2_data *private = elem->head.mixer->private_data;
  3132. return snd_ctl_enum_info(
  3133. uinfo, 1,
  3134. private->num_autogain_status_texts,
  3135. private->config_set->autogain_status_texts);
  3136. }
  3137. static const struct snd_kcontrol_new scarlett2_autogain_switch_ctl = {
  3138. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3139. .name = "",
  3140. .info = scarlett2_autogain_switch_ctl_info,
  3141. .get = scarlett2_autogain_switch_ctl_get,
  3142. .put = scarlett2_autogain_switch_ctl_put
  3143. };
  3144. static const struct snd_kcontrol_new scarlett2_autogain_status_ctl = {
  3145. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3146. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  3147. .name = "",
  3148. .info = scarlett2_autogain_status_ctl_info,
  3149. .get = scarlett2_autogain_status_ctl_get,
  3150. };
  3151. /*** Autogain Target Controls ***/
  3152. static int scarlett2_ag_target_ctl_info(
  3153. struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
  3154. {
  3155. struct usb_mixer_elem_info *elem = kctl->private_data;
  3156. struct usb_mixer_interface *mixer = elem->head.mixer;
  3157. struct scarlett2_data *private = mixer->private_data;
  3158. int err;
  3159. mutex_lock(&private->data_mutex);
  3160. if (private->hwdep_in_use) {
  3161. err = -EBUSY;
  3162. goto unlock;
  3163. }
  3164. err = scarlett2_check_autogain_updated(mixer);
  3165. if (err < 0)
  3166. goto unlock;
  3167. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  3168. uinfo->count = 1;
  3169. uinfo->value.integer.min = SCARLETT2_AG_TARGET_MIN;
  3170. uinfo->value.integer.max = 0;
  3171. uinfo->value.integer.step = 1;
  3172. unlock:
  3173. mutex_unlock(&private->data_mutex);
  3174. return err;
  3175. }
  3176. static int scarlett2_ag_target_ctl_get(
  3177. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  3178. {
  3179. struct usb_mixer_elem_info *elem = kctl->private_data;
  3180. struct usb_mixer_interface *mixer = elem->head.mixer;
  3181. struct scarlett2_data *private = mixer->private_data;
  3182. int err = 0;
  3183. mutex_lock(&private->data_mutex);
  3184. if (private->hwdep_in_use) {
  3185. err = -EBUSY;
  3186. goto unlock;
  3187. }
  3188. if (private->autogain_updated) {
  3189. err = scarlett2_update_autogain(mixer);
  3190. if (err < 0)
  3191. goto unlock;
  3192. }
  3193. ucontrol->value.integer.value[0] = private->ag_targets[elem->control];
  3194. unlock:
  3195. mutex_unlock(&private->data_mutex);
  3196. return err;
  3197. }
  3198. static int scarlett2_ag_target_ctl_put(
  3199. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  3200. {
  3201. struct usb_mixer_elem_info *elem = kctl->private_data;
  3202. struct usb_mixer_interface *mixer = elem->head.mixer;
  3203. struct scarlett2_data *private = mixer->private_data;
  3204. int index = elem->control;
  3205. int oval, val, err;
  3206. mutex_lock(&private->data_mutex);
  3207. if (private->hwdep_in_use) {
  3208. err = -EBUSY;
  3209. goto unlock;
  3210. }
  3211. err = scarlett2_check_put_during_autogain(mixer);
  3212. if (err < 0)
  3213. goto unlock;
  3214. oval = private->ag_targets[index];
  3215. val = clamp(ucontrol->value.integer.value[0],
  3216. (long)SCARLETT2_AG_TARGET_MIN, 0L);
  3217. if (oval == val)
  3218. goto unlock;
  3219. private->ag_targets[index] = val;
  3220. /* Send new value to the device */
  3221. err = scarlett2_usb_set_config(
  3222. mixer, scarlett2_ag_target_configs[index], 1, -val);
  3223. if (err == 0)
  3224. err = 1;
  3225. unlock:
  3226. mutex_unlock(&private->data_mutex);
  3227. return err;
  3228. }
  3229. static const DECLARE_TLV_DB_MINMAX(
  3230. db_scale_ag_target, SCARLETT2_AG_TARGET_MIN * 100, 0
  3231. );
  3232. static const struct snd_kcontrol_new scarlett2_ag_target_ctl = {
  3233. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  3234. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  3235. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  3236. .name = "",
  3237. .info = scarlett2_ag_target_ctl_info,
  3238. .get = scarlett2_ag_target_ctl_get,
  3239. .put = scarlett2_ag_target_ctl_put,
  3240. .tlv = { .p = db_scale_ag_target }
  3241. };
  3242. /*** Input Select Control ***/
  3243. static int scarlett2_update_input_select(struct usb_mixer_interface *mixer)
  3244. {
  3245. struct scarlett2_data *private = mixer->private_data;
  3246. const struct scarlett2_device_info *info = private->info;
  3247. int link_count = info->gain_input_count / 2;
  3248. int err;
  3249. private->input_select_updated = 0;
  3250. if (!scarlett2_has_config_item(private,
  3251. SCARLETT2_CONFIG_INPUT_SELECT_SWITCH) ||
  3252. !link_count)
  3253. return 0;
  3254. err = scarlett2_usb_get_config(
  3255. mixer, SCARLETT2_CONFIG_INPUT_SELECT_SWITCH,
  3256. 1, &private->input_select_switch);
  3257. if (err < 0)
  3258. return err;
  3259. err = scarlett2_usb_get_config(
  3260. mixer, SCARLETT2_CONFIG_INPUT_LINK_SWITCH,
  3261. link_count, private->input_link_switch);
  3262. if (err < 0)
  3263. return err;
  3264. /* simplified because no model yet has link_count > 1 */
  3265. if (private->input_link_switch[0])
  3266. private->input_select_switch = 0;
  3267. return 0;
  3268. }
  3269. static int scarlett2_input_select_ctl_get(
  3270. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  3271. {
  3272. struct usb_mixer_elem_info *elem = kctl->private_data;
  3273. struct usb_mixer_interface *mixer = elem->head.mixer;
  3274. struct scarlett2_data *private = mixer->private_data;
  3275. int err = 0;
  3276. mutex_lock(&private->data_mutex);
  3277. if (private->hwdep_in_use) {
  3278. err = -EBUSY;
  3279. goto unlock;
  3280. }
  3281. if (private->input_select_updated) {
  3282. err = scarlett2_update_input_select(mixer);
  3283. if (err < 0)
  3284. goto unlock;
  3285. }
  3286. ucontrol->value.enumerated.item[0] = private->input_select_switch;
  3287. unlock:
  3288. mutex_unlock(&private->data_mutex);
  3289. return err;
  3290. }
  3291. static int scarlett2_input_select_ctl_put(
  3292. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  3293. {
  3294. struct usb_mixer_elem_info *elem = kctl->private_data;
  3295. struct usb_mixer_interface *mixer = elem->head.mixer;
  3296. struct scarlett2_data *private = mixer->private_data;
  3297. int oval, val, err;
  3298. int max_val = private->input_link_switch[0] ? 0 : 1;
  3299. mutex_lock(&private->data_mutex);
  3300. if (private->hwdep_in_use) {
  3301. err = -EBUSY;
  3302. goto unlock;
  3303. }
  3304. err = scarlett2_check_put_during_autogain(mixer);
  3305. if (err < 0)
  3306. goto unlock;
  3307. oval = private->input_select_switch;
  3308. val = ucontrol->value.integer.value[0];
  3309. if (val < 0)
  3310. val = 0;
  3311. else if (val > max_val)
  3312. val = max_val;
  3313. if (oval == val)
  3314. goto unlock;
  3315. private->input_select_switch = val;
  3316. /* Send switch change to the device if inputs not linked */
  3317. if (!private->input_link_switch[0])
  3318. err = scarlett2_usb_set_config(
  3319. mixer, SCARLETT2_CONFIG_INPUT_SELECT_SWITCH,
  3320. 1, val);
  3321. if (err == 0)
  3322. err = 1;
  3323. unlock:
  3324. mutex_unlock(&private->data_mutex);
  3325. return err;
  3326. }
  3327. static int scarlett2_input_select_ctl_info(
  3328. struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
  3329. {
  3330. struct usb_mixer_elem_info *elem = kctl->private_data;
  3331. struct usb_mixer_interface *mixer = elem->head.mixer;
  3332. struct scarlett2_data *private = mixer->private_data;
  3333. int inputs = private->info->gain_input_count;
  3334. int i, j;
  3335. int err;
  3336. char **values = kcalloc(inputs, sizeof(char *), GFP_KERNEL);
  3337. if (!values)
  3338. return -ENOMEM;
  3339. mutex_lock(&private->data_mutex);
  3340. if (private->hwdep_in_use) {
  3341. err = -EBUSY;
  3342. goto unlock;
  3343. }
  3344. err = scarlett2_check_autogain_updated(mixer);
  3345. if (err < 0)
  3346. goto unlock;
  3347. /* Loop through each input
  3348. * Linked inputs have one value for the pair
  3349. */
  3350. for (i = 0, j = 0; i < inputs; i++) {
  3351. if (private->input_link_switch[i / 2]) {
  3352. values[j++] = kasprintf(
  3353. GFP_KERNEL, "Input %d-%d", i + 1, i + 2);
  3354. i++;
  3355. } else {
  3356. values[j++] = kasprintf(
  3357. GFP_KERNEL, "Input %d", i + 1);
  3358. }
  3359. }
  3360. err = snd_ctl_enum_info(uinfo, 1, j,
  3361. (const char * const *)values);
  3362. unlock:
  3363. mutex_unlock(&private->data_mutex);
  3364. for (i = 0; i < inputs; i++)
  3365. kfree(values[i]);
  3366. kfree(values);
  3367. return err;
  3368. }
  3369. static const struct snd_kcontrol_new scarlett2_input_select_ctl = {
  3370. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3371. .name = "",
  3372. .info = scarlett2_input_select_ctl_info,
  3373. .get = scarlett2_input_select_ctl_get,
  3374. .put = scarlett2_input_select_ctl_put,
  3375. };
  3376. /*** Input Link Switch Controls ***/
  3377. /* snd_ctl_boolean_mono_info() with autogain-updated check
  3378. * (for controls that are read-only while autogain is running)
  3379. */
  3380. static int scarlett2_autogain_disables_ctl_info(struct snd_kcontrol *kctl,
  3381. struct snd_ctl_elem_info *uinfo)
  3382. {
  3383. struct usb_mixer_elem_info *elem = kctl->private_data;
  3384. struct usb_mixer_interface *mixer = elem->head.mixer;
  3385. struct scarlett2_data *private = mixer->private_data;
  3386. int err;
  3387. mutex_lock(&private->data_mutex);
  3388. if (private->hwdep_in_use) {
  3389. err = -EBUSY;
  3390. goto unlock;
  3391. }
  3392. err = scarlett2_check_autogain_updated(mixer);
  3393. if (err < 0)
  3394. goto unlock;
  3395. err = snd_ctl_boolean_mono_info(kctl, uinfo);
  3396. unlock:
  3397. mutex_unlock(&private->data_mutex);
  3398. return err;
  3399. }
  3400. static int scarlett2_input_link_ctl_get(
  3401. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  3402. {
  3403. struct usb_mixer_elem_info *elem = kctl->private_data;
  3404. struct usb_mixer_interface *mixer = elem->head.mixer;
  3405. struct scarlett2_data *private = mixer->private_data;
  3406. int err = 0;
  3407. mutex_lock(&private->data_mutex);
  3408. if (private->hwdep_in_use) {
  3409. err = -EBUSY;
  3410. goto unlock;
  3411. }
  3412. if (private->input_select_updated) {
  3413. err = scarlett2_update_input_select(mixer);
  3414. if (err < 0)
  3415. goto unlock;
  3416. }
  3417. ucontrol->value.enumerated.item[0] =
  3418. private->input_link_switch[elem->control];
  3419. unlock:
  3420. mutex_unlock(&private->data_mutex);
  3421. return err;
  3422. }
  3423. static int scarlett2_input_link_ctl_put(
  3424. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  3425. {
  3426. struct usb_mixer_elem_info *elem = kctl->private_data;
  3427. struct usb_mixer_interface *mixer = elem->head.mixer;
  3428. struct scarlett2_data *private = mixer->private_data;
  3429. int index = elem->control;
  3430. int oval, val, err;
  3431. mutex_lock(&private->data_mutex);
  3432. if (private->hwdep_in_use) {
  3433. err = -EBUSY;
  3434. goto unlock;
  3435. }
  3436. err = scarlett2_check_put_during_autogain(mixer);
  3437. if (err < 0)
  3438. goto unlock;
  3439. oval = private->input_link_switch[index];
  3440. val = !!ucontrol->value.integer.value[0];
  3441. if (oval == val)
  3442. goto unlock;
  3443. private->input_link_switch[index] = val;
  3444. /* Notify of change in input select options available */
  3445. snd_ctl_notify(mixer->chip->card,
  3446. SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
  3447. &private->input_select_ctl->id);
  3448. private->input_select_updated = 1;
  3449. /* Send switch change to the device
  3450. * Link for channels 1-2 is at index 1
  3451. * No device yet has more than 2 channels linked
  3452. */
  3453. err = scarlett2_usb_set_config(
  3454. mixer, SCARLETT2_CONFIG_INPUT_LINK_SWITCH, index + 1, val);
  3455. if (err == 0)
  3456. err = 1;
  3457. unlock:
  3458. mutex_unlock(&private->data_mutex);
  3459. return err;
  3460. }
  3461. static const struct snd_kcontrol_new scarlett2_input_link_ctl = {
  3462. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3463. .name = "",
  3464. .info = scarlett2_autogain_disables_ctl_info,
  3465. .get = scarlett2_input_link_ctl_get,
  3466. .put = scarlett2_input_link_ctl_put
  3467. };
  3468. /*** Input Gain Controls ***/
  3469. static int scarlett2_update_input_gain(struct usb_mixer_interface *mixer)
  3470. {
  3471. struct scarlett2_data *private = mixer->private_data;
  3472. const struct scarlett2_device_info *info = private->info;
  3473. private->input_gain_updated = 0;
  3474. if (!info->gain_input_count)
  3475. return 0;
  3476. return scarlett2_usb_get_config(
  3477. mixer, SCARLETT2_CONFIG_INPUT_GAIN,
  3478. info->gain_input_count, private->gain);
  3479. }
  3480. static int scarlett2_input_gain_ctl_info(struct snd_kcontrol *kctl,
  3481. struct snd_ctl_elem_info *uinfo)
  3482. {
  3483. struct usb_mixer_elem_info *elem = kctl->private_data;
  3484. struct usb_mixer_interface *mixer = elem->head.mixer;
  3485. struct scarlett2_data *private = mixer->private_data;
  3486. int err;
  3487. mutex_lock(&private->data_mutex);
  3488. if (private->hwdep_in_use) {
  3489. err = -EBUSY;
  3490. goto unlock;
  3491. }
  3492. err = scarlett2_check_autogain_updated(mixer);
  3493. if (err < 0)
  3494. goto unlock;
  3495. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  3496. uinfo->count = elem->channels;
  3497. uinfo->value.integer.min = 0;
  3498. uinfo->value.integer.max = SCARLETT2_MAX_GAIN_VALUE;
  3499. uinfo->value.integer.step = 1;
  3500. unlock:
  3501. mutex_unlock(&private->data_mutex);
  3502. return err;
  3503. }
  3504. static int scarlett2_input_gain_ctl_get(struct snd_kcontrol *kctl,
  3505. struct snd_ctl_elem_value *ucontrol)
  3506. {
  3507. struct usb_mixer_elem_info *elem = kctl->private_data;
  3508. struct usb_mixer_interface *mixer = elem->head.mixer;
  3509. struct scarlett2_data *private = mixer->private_data;
  3510. int err = 0;
  3511. mutex_lock(&private->data_mutex);
  3512. if (private->hwdep_in_use) {
  3513. err = -EBUSY;
  3514. goto unlock;
  3515. }
  3516. if (private->input_gain_updated) {
  3517. err = scarlett2_update_input_gain(mixer);
  3518. if (err < 0)
  3519. goto unlock;
  3520. }
  3521. ucontrol->value.integer.value[0] =
  3522. private->gain[elem->control];
  3523. unlock:
  3524. mutex_unlock(&private->data_mutex);
  3525. return err;
  3526. }
  3527. static int scarlett2_input_gain_ctl_put(struct snd_kcontrol *kctl,
  3528. struct snd_ctl_elem_value *ucontrol)
  3529. {
  3530. struct usb_mixer_elem_info *elem = kctl->private_data;
  3531. struct usb_mixer_interface *mixer = elem->head.mixer;
  3532. struct scarlett2_data *private = mixer->private_data;
  3533. int index = elem->control;
  3534. int oval, val, err;
  3535. mutex_lock(&private->data_mutex);
  3536. if (private->hwdep_in_use) {
  3537. err = -EBUSY;
  3538. goto unlock;
  3539. }
  3540. err = scarlett2_check_put_during_autogain(mixer);
  3541. if (err < 0)
  3542. goto unlock;
  3543. oval = private->gain[index];
  3544. val = ucontrol->value.integer.value[0];
  3545. if (oval == val)
  3546. goto unlock;
  3547. private->gain[index] = val;
  3548. /* Send gain change to the device */
  3549. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_INPUT_GAIN,
  3550. index, val);
  3551. if (err == 0)
  3552. err = 1;
  3553. unlock:
  3554. mutex_unlock(&private->data_mutex);
  3555. return err;
  3556. }
  3557. static const struct snd_kcontrol_new scarlett2_input_gain_ctl = {
  3558. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3559. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  3560. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  3561. .name = "",
  3562. .info = scarlett2_input_gain_ctl_info,
  3563. .get = scarlett2_input_gain_ctl_get,
  3564. .put = scarlett2_input_gain_ctl_put,
  3565. .private_value = 0, /* max value */
  3566. };
  3567. /*** Safe Controls ***/
  3568. static int scarlett2_update_input_safe(struct usb_mixer_interface *mixer)
  3569. {
  3570. struct scarlett2_data *private = mixer->private_data;
  3571. const struct scarlett2_device_info *info = private->info;
  3572. private->input_safe_updated = 0;
  3573. if (!info->safe_input_count)
  3574. return 0;
  3575. return scarlett2_usb_get_config(
  3576. mixer, SCARLETT2_CONFIG_SAFE_SWITCH,
  3577. info->safe_input_count, private->safe_switch);
  3578. }
  3579. static int scarlett2_safe_ctl_get(struct snd_kcontrol *kctl,
  3580. struct snd_ctl_elem_value *ucontrol)
  3581. {
  3582. struct usb_mixer_elem_info *elem = kctl->private_data;
  3583. struct usb_mixer_interface *mixer = elem->head.mixer;
  3584. struct scarlett2_data *private = mixer->private_data;
  3585. int err = 0;
  3586. mutex_lock(&private->data_mutex);
  3587. if (private->hwdep_in_use) {
  3588. err = -EBUSY;
  3589. goto unlock;
  3590. }
  3591. if (private->input_safe_updated) {
  3592. err = scarlett2_update_input_safe(mixer);
  3593. if (err < 0)
  3594. goto unlock;
  3595. }
  3596. ucontrol->value.integer.value[0] =
  3597. private->safe_switch[elem->control];
  3598. unlock:
  3599. mutex_unlock(&private->data_mutex);
  3600. return err;
  3601. }
  3602. static int scarlett2_safe_ctl_put(struct snd_kcontrol *kctl,
  3603. struct snd_ctl_elem_value *ucontrol)
  3604. {
  3605. struct usb_mixer_elem_info *elem = kctl->private_data;
  3606. struct usb_mixer_interface *mixer = elem->head.mixer;
  3607. struct scarlett2_data *private = mixer->private_data;
  3608. int index = elem->control;
  3609. int oval, val, err;
  3610. mutex_lock(&private->data_mutex);
  3611. if (private->hwdep_in_use) {
  3612. err = -EBUSY;
  3613. goto unlock;
  3614. }
  3615. err = scarlett2_check_put_during_autogain(mixer);
  3616. if (err < 0)
  3617. goto unlock;
  3618. oval = private->safe_switch[index];
  3619. val = !!ucontrol->value.integer.value[0];
  3620. if (oval == val)
  3621. goto unlock;
  3622. private->safe_switch[index] = val;
  3623. /* Send switch change to the device */
  3624. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SAFE_SWITCH,
  3625. index, val);
  3626. if (err == 0)
  3627. err = 1;
  3628. unlock:
  3629. mutex_unlock(&private->data_mutex);
  3630. return err;
  3631. }
  3632. static const struct snd_kcontrol_new scarlett2_safe_ctl = {
  3633. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3634. .name = "",
  3635. .info = scarlett2_autogain_disables_ctl_info,
  3636. .get = scarlett2_safe_ctl_get,
  3637. .put = scarlett2_safe_ctl_put,
  3638. };
  3639. /*** PCM Input Control ***/
  3640. static int scarlett2_update_pcm_input_switch(struct usb_mixer_interface *mixer)
  3641. {
  3642. struct scarlett2_data *private = mixer->private_data;
  3643. int err;
  3644. private->pcm_input_switch_updated = 0;
  3645. err = scarlett2_usb_get_config(
  3646. mixer, SCARLETT2_CONFIG_PCM_INPUT_SWITCH,
  3647. 1, &private->pcm_input_switch);
  3648. if (err < 0)
  3649. return err;
  3650. return 0;
  3651. }
  3652. static int scarlett2_pcm_input_switch_ctl_get(
  3653. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  3654. {
  3655. struct usb_mixer_elem_info *elem = kctl->private_data;
  3656. struct usb_mixer_interface *mixer = elem->head.mixer;
  3657. struct scarlett2_data *private = elem->head.mixer->private_data;
  3658. int err = 0;
  3659. mutex_lock(&private->data_mutex);
  3660. if (private->pcm_input_switch_updated) {
  3661. err = scarlett2_update_pcm_input_switch(mixer);
  3662. if (err < 0)
  3663. goto unlock;
  3664. }
  3665. ucontrol->value.enumerated.item[0] = private->pcm_input_switch;
  3666. unlock:
  3667. mutex_unlock(&private->data_mutex);
  3668. return err;
  3669. }
  3670. static int scarlett2_pcm_input_switch_ctl_put(
  3671. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  3672. {
  3673. struct usb_mixer_elem_info *elem = kctl->private_data;
  3674. struct usb_mixer_interface *mixer = elem->head.mixer;
  3675. struct scarlett2_data *private = mixer->private_data;
  3676. int oval, val, err = 0;
  3677. mutex_lock(&private->data_mutex);
  3678. if (private->hwdep_in_use) {
  3679. err = -EBUSY;
  3680. goto unlock;
  3681. }
  3682. oval = private->pcm_input_switch;
  3683. val = !!ucontrol->value.integer.value[0];
  3684. if (oval == val)
  3685. goto unlock;
  3686. private->pcm_input_switch = val;
  3687. /* Send switch change to the device */
  3688. err = scarlett2_usb_set_config(
  3689. mixer, SCARLETT2_CONFIG_PCM_INPUT_SWITCH,
  3690. 0, val);
  3691. if (err == 0)
  3692. err = 1;
  3693. unlock:
  3694. mutex_unlock(&private->data_mutex);
  3695. return err;
  3696. }
  3697. static int scarlett2_pcm_input_switch_ctl_info(
  3698. struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
  3699. {
  3700. static const char *const values[2] = {
  3701. "Direct", "Mixer"
  3702. };
  3703. return snd_ctl_enum_info(
  3704. uinfo, 1, 2, values);
  3705. }
  3706. static const struct snd_kcontrol_new scarlett2_pcm_input_switch_ctl = {
  3707. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3708. .name = "",
  3709. .info = scarlett2_pcm_input_switch_ctl_info,
  3710. .get = scarlett2_pcm_input_switch_ctl_get,
  3711. .put = scarlett2_pcm_input_switch_ctl_put
  3712. };
  3713. /*** Analogue Line Out Volume Controls ***/
  3714. /* Update hardware volume controls after receiving notification that
  3715. * they have changed
  3716. */
  3717. static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
  3718. {
  3719. struct scarlett2_data *private = mixer->private_data;
  3720. s16 vol;
  3721. int err, i;
  3722. private->vol_updated = 0;
  3723. if (scarlett2_has_config_item(private,
  3724. SCARLETT2_CONFIG_MASTER_VOLUME)) {
  3725. err = scarlett2_usb_get_config(
  3726. mixer, SCARLETT2_CONFIG_MASTER_VOLUME,
  3727. 1, &vol);
  3728. if (err < 0)
  3729. return err;
  3730. private->master_vol = clamp(vol + SCARLETT2_VOLUME_BIAS,
  3731. 0, SCARLETT2_VOLUME_BIAS);
  3732. if (scarlett2_has_config_item(private,
  3733. SCARLETT2_CONFIG_SW_HW_SWITCH))
  3734. for (i = 0; i < private->num_line_out; i++)
  3735. if (private->vol_sw_hw_switch[i])
  3736. private->vol[i] = private->master_vol;
  3737. }
  3738. if (scarlett2_has_config_item(private,
  3739. SCARLETT2_CONFIG_HEADPHONE_VOLUME)) {
  3740. err = scarlett2_usb_get_config(
  3741. mixer, SCARLETT2_CONFIG_HEADPHONE_VOLUME,
  3742. 1, &vol);
  3743. if (err < 0)
  3744. return err;
  3745. private->headphone_vol = clamp(vol + SCARLETT2_VOLUME_BIAS,
  3746. 0, SCARLETT2_VOLUME_BIAS);
  3747. }
  3748. return 0;
  3749. }
  3750. static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
  3751. struct snd_ctl_elem_info *uinfo)
  3752. {
  3753. struct usb_mixer_elem_info *elem = kctl->private_data;
  3754. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  3755. uinfo->count = elem->channels;
  3756. uinfo->value.integer.min = 0;
  3757. uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
  3758. uinfo->value.integer.step = 1;
  3759. return 0;
  3760. }
  3761. static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
  3762. struct snd_ctl_elem_value *ucontrol)
  3763. {
  3764. struct usb_mixer_elem_info *elem = kctl->private_data;
  3765. struct usb_mixer_interface *mixer = elem->head.mixer;
  3766. struct scarlett2_data *private = mixer->private_data;
  3767. int err = 0;
  3768. mutex_lock(&private->data_mutex);
  3769. if (private->hwdep_in_use) {
  3770. err = -EBUSY;
  3771. goto unlock;
  3772. }
  3773. if (private->vol_updated) {
  3774. err = scarlett2_update_volumes(mixer);
  3775. if (err < 0)
  3776. goto unlock;
  3777. }
  3778. ucontrol->value.integer.value[0] = private->master_vol;
  3779. unlock:
  3780. mutex_unlock(&private->data_mutex);
  3781. return err;
  3782. }
  3783. static int scarlett2_headphone_volume_ctl_get(
  3784. struct snd_kcontrol *kctl,
  3785. struct snd_ctl_elem_value *ucontrol)
  3786. {
  3787. struct usb_mixer_elem_info *elem = kctl->private_data;
  3788. struct usb_mixer_interface *mixer = elem->head.mixer;
  3789. struct scarlett2_data *private = mixer->private_data;
  3790. int err = 0;
  3791. mutex_lock(&private->data_mutex);
  3792. if (private->hwdep_in_use) {
  3793. err = -EBUSY;
  3794. goto unlock;
  3795. }
  3796. if (private->vol_updated) {
  3797. err = scarlett2_update_volumes(mixer);
  3798. if (err < 0)
  3799. goto unlock;
  3800. }
  3801. ucontrol->value.integer.value[0] = private->headphone_vol;
  3802. unlock:
  3803. mutex_unlock(&private->data_mutex);
  3804. return err;
  3805. }
  3806. static int line_out_remap(struct scarlett2_data *private, int index)
  3807. {
  3808. const struct scarlett2_device_info *info = private->info;
  3809. if (!info->line_out_remap_enable)
  3810. return index;
  3811. if (index >= private->num_line_out)
  3812. return index;
  3813. return info->line_out_remap[index];
  3814. }
  3815. static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
  3816. struct snd_ctl_elem_value *ucontrol)
  3817. {
  3818. struct usb_mixer_elem_info *elem = kctl->private_data;
  3819. struct usb_mixer_interface *mixer = elem->head.mixer;
  3820. struct scarlett2_data *private = mixer->private_data;
  3821. int index = line_out_remap(private, elem->control);
  3822. int err = 0;
  3823. mutex_lock(&private->data_mutex);
  3824. if (private->hwdep_in_use) {
  3825. err = -EBUSY;
  3826. goto unlock;
  3827. }
  3828. if (private->vol_updated) {
  3829. err = scarlett2_update_volumes(mixer);
  3830. if (err < 0)
  3831. goto unlock;
  3832. }
  3833. ucontrol->value.integer.value[0] = private->vol[index];
  3834. unlock:
  3835. mutex_unlock(&private->data_mutex);
  3836. return err;
  3837. }
  3838. static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
  3839. struct snd_ctl_elem_value *ucontrol)
  3840. {
  3841. struct usb_mixer_elem_info *elem = kctl->private_data;
  3842. struct usb_mixer_interface *mixer = elem->head.mixer;
  3843. struct scarlett2_data *private = mixer->private_data;
  3844. int index = line_out_remap(private, elem->control);
  3845. int oval, val, err = 0;
  3846. mutex_lock(&private->data_mutex);
  3847. if (private->hwdep_in_use) {
  3848. err = -EBUSY;
  3849. goto unlock;
  3850. }
  3851. oval = private->vol[index];
  3852. val = ucontrol->value.integer.value[0];
  3853. if (oval == val)
  3854. goto unlock;
  3855. private->vol[index] = val;
  3856. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
  3857. index, val - SCARLETT2_VOLUME_BIAS);
  3858. if (err == 0)
  3859. err = 1;
  3860. unlock:
  3861. mutex_unlock(&private->data_mutex);
  3862. return err;
  3863. }
  3864. static const DECLARE_TLV_DB_MINMAX(
  3865. db_scale_scarlett2_volume, -SCARLETT2_VOLUME_BIAS * 100, 0
  3866. );
  3867. static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
  3868. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3869. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  3870. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  3871. .name = "",
  3872. .info = scarlett2_volume_ctl_info,
  3873. .get = scarlett2_master_volume_ctl_get,
  3874. .private_value = 0, /* max value */
  3875. .tlv = { .p = db_scale_scarlett2_volume }
  3876. };
  3877. static const struct snd_kcontrol_new scarlett2_headphone_volume_ctl = {
  3878. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3879. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  3880. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  3881. .name = "",
  3882. .info = scarlett2_volume_ctl_info,
  3883. .get = scarlett2_headphone_volume_ctl_get,
  3884. .private_value = 0, /* max value */
  3885. .tlv = { .p = db_scale_scarlett2_volume }
  3886. };
  3887. static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
  3888. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3889. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  3890. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  3891. .name = "",
  3892. .info = scarlett2_volume_ctl_info,
  3893. .get = scarlett2_volume_ctl_get,
  3894. .put = scarlett2_volume_ctl_put,
  3895. .private_value = 0, /* max value */
  3896. .tlv = { .p = db_scale_scarlett2_volume }
  3897. };
  3898. /*** Mute Switch Controls ***/
  3899. static int scarlett2_update_dim_mute(struct usb_mixer_interface *mixer)
  3900. {
  3901. struct scarlett2_data *private = mixer->private_data;
  3902. int err, i;
  3903. u8 mute;
  3904. private->dim_mute_updated = 0;
  3905. if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
  3906. return 0;
  3907. err = scarlett2_usb_get_config(
  3908. mixer, SCARLETT2_CONFIG_DIM_MUTE,
  3909. SCARLETT2_DIM_MUTE_COUNT, private->dim_mute);
  3910. if (err < 0)
  3911. return err;
  3912. for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
  3913. private->dim_mute[i] = !!private->dim_mute[i];
  3914. mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];
  3915. for (i = 0; i < private->num_line_out; i++)
  3916. if (private->vol_sw_hw_switch[i])
  3917. private->mute_switch[i] = mute;
  3918. return 0;
  3919. }
  3920. static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
  3921. struct snd_ctl_elem_value *ucontrol)
  3922. {
  3923. struct usb_mixer_elem_info *elem = kctl->private_data;
  3924. struct usb_mixer_interface *mixer = elem->head.mixer;
  3925. struct scarlett2_data *private = mixer->private_data;
  3926. int index = line_out_remap(private, elem->control);
  3927. int err = 0;
  3928. mutex_lock(&private->data_mutex);
  3929. if (private->hwdep_in_use) {
  3930. err = -EBUSY;
  3931. goto unlock;
  3932. }
  3933. if (private->dim_mute_updated) {
  3934. err = scarlett2_update_dim_mute(mixer);
  3935. if (err < 0)
  3936. goto unlock;
  3937. }
  3938. ucontrol->value.integer.value[0] = private->mute_switch[index];
  3939. unlock:
  3940. mutex_unlock(&private->data_mutex);
  3941. return err;
  3942. }
  3943. static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
  3944. struct snd_ctl_elem_value *ucontrol)
  3945. {
  3946. struct usb_mixer_elem_info *elem = kctl->private_data;
  3947. struct usb_mixer_interface *mixer = elem->head.mixer;
  3948. struct scarlett2_data *private = mixer->private_data;
  3949. int index = line_out_remap(private, elem->control);
  3950. int oval, val, err = 0;
  3951. mutex_lock(&private->data_mutex);
  3952. if (private->hwdep_in_use) {
  3953. err = -EBUSY;
  3954. goto unlock;
  3955. }
  3956. oval = private->mute_switch[index];
  3957. val = !!ucontrol->value.integer.value[0];
  3958. if (oval == val)
  3959. goto unlock;
  3960. private->mute_switch[index] = val;
  3961. /* Send mute change to the device */
  3962. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
  3963. index, val);
  3964. if (err == 0)
  3965. err = 1;
  3966. unlock:
  3967. mutex_unlock(&private->data_mutex);
  3968. return err;
  3969. }
  3970. static const struct snd_kcontrol_new scarlett2_mute_ctl = {
  3971. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3972. .name = "",
  3973. .info = snd_ctl_boolean_mono_info,
  3974. .get = scarlett2_mute_ctl_get,
  3975. .put = scarlett2_mute_ctl_put,
  3976. };
  3977. /*** HW/SW Volume Switch Controls ***/
  3978. static void scarlett2_sw_hw_ctl_ro(struct scarlett2_data *private, int index)
  3979. {
  3980. private->sw_hw_ctls[index]->vd[0].access &=
  3981. ~SNDRV_CTL_ELEM_ACCESS_WRITE;
  3982. }
  3983. static void scarlett2_sw_hw_ctl_rw(struct scarlett2_data *private, int index)
  3984. {
  3985. private->sw_hw_ctls[index]->vd[0].access |=
  3986. SNDRV_CTL_ELEM_ACCESS_WRITE;
  3987. }
  3988. static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
  3989. struct snd_ctl_elem_info *uinfo)
  3990. {
  3991. static const char *const values[2] = {
  3992. "SW", "HW"
  3993. };
  3994. return snd_ctl_enum_info(uinfo, 1, 2, values);
  3995. }
  3996. static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
  3997. struct snd_ctl_elem_value *ucontrol)
  3998. {
  3999. struct usb_mixer_elem_info *elem = kctl->private_data;
  4000. struct scarlett2_data *private = elem->head.mixer->private_data;
  4001. int index = line_out_remap(private, elem->control);
  4002. ucontrol->value.enumerated.item[0] = private->vol_sw_hw_switch[index];
  4003. return 0;
  4004. }
  4005. static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
  4006. int index, int value)
  4007. {
  4008. struct scarlett2_data *private = mixer->private_data;
  4009. struct snd_card *card = mixer->chip->card;
  4010. /* Set/Clear write bits */
  4011. if (value) {
  4012. private->vol_ctls[index]->vd[0].access |=
  4013. SNDRV_CTL_ELEM_ACCESS_WRITE;
  4014. private->mute_ctls[index]->vd[0].access |=
  4015. SNDRV_CTL_ELEM_ACCESS_WRITE;
  4016. } else {
  4017. private->vol_ctls[index]->vd[0].access &=
  4018. ~SNDRV_CTL_ELEM_ACCESS_WRITE;
  4019. private->mute_ctls[index]->vd[0].access &=
  4020. ~SNDRV_CTL_ELEM_ACCESS_WRITE;
  4021. }
  4022. /* Notify of write bit and possible value change */
  4023. snd_ctl_notify(card,
  4024. SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
  4025. &private->vol_ctls[index]->id);
  4026. snd_ctl_notify(card,
  4027. SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
  4028. &private->mute_ctls[index]->id);
  4029. }
  4030. static int scarlett2_sw_hw_change(struct usb_mixer_interface *mixer,
  4031. int ctl_index, int val)
  4032. {
  4033. struct scarlett2_data *private = mixer->private_data;
  4034. int index = line_out_remap(private, ctl_index);
  4035. int err;
  4036. private->vol_sw_hw_switch[index] = val;
  4037. /* Change access mode to RO (hardware controlled volume)
  4038. * or RW (software controlled volume)
  4039. */
  4040. scarlett2_vol_ctl_set_writable(mixer, ctl_index, !val);
  4041. /* Reset volume/mute to master volume/mute */
  4042. private->vol[index] = private->master_vol;
  4043. private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE];
  4044. /* Set SW volume to current HW volume */
  4045. err = scarlett2_usb_set_config(
  4046. mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
  4047. index, private->master_vol - SCARLETT2_VOLUME_BIAS);
  4048. if (err < 0)
  4049. return err;
  4050. /* Set SW mute to current HW mute */
  4051. err = scarlett2_usb_set_config(
  4052. mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
  4053. index, private->dim_mute[SCARLETT2_BUTTON_MUTE]);
  4054. if (err < 0)
  4055. return err;
  4056. /* Send SW/HW switch change to the device */
  4057. return scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
  4058. index, val);
  4059. }
  4060. static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
  4061. struct snd_ctl_elem_value *ucontrol)
  4062. {
  4063. struct usb_mixer_elem_info *elem = kctl->private_data;
  4064. struct usb_mixer_interface *mixer = elem->head.mixer;
  4065. struct scarlett2_data *private = mixer->private_data;
  4066. int ctl_index = elem->control;
  4067. int index = line_out_remap(private, ctl_index);
  4068. int oval, val, err = 0;
  4069. mutex_lock(&private->data_mutex);
  4070. if (private->hwdep_in_use) {
  4071. err = -EBUSY;
  4072. goto unlock;
  4073. }
  4074. oval = private->vol_sw_hw_switch[index];
  4075. val = !!ucontrol->value.enumerated.item[0];
  4076. if (oval == val)
  4077. goto unlock;
  4078. err = scarlett2_sw_hw_change(mixer, ctl_index, val);
  4079. if (err == 0)
  4080. err = 1;
  4081. unlock:
  4082. mutex_unlock(&private->data_mutex);
  4083. return err;
  4084. }
  4085. static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
  4086. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4087. .name = "",
  4088. .info = scarlett2_sw_hw_enum_ctl_info,
  4089. .get = scarlett2_sw_hw_enum_ctl_get,
  4090. .put = scarlett2_sw_hw_enum_ctl_put,
  4091. };
  4092. /*** Line Level/Instrument Level Switch Controls ***/
  4093. static int scarlett2_update_input_level(struct usb_mixer_interface *mixer)
  4094. {
  4095. struct scarlett2_data *private = mixer->private_data;
  4096. const struct scarlett2_device_info *info = private->info;
  4097. private->input_level_updated = 0;
  4098. if (!info->level_input_count)
  4099. return 0;
  4100. return scarlett2_usb_get_config(
  4101. mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
  4102. info->level_input_count + info->level_input_first,
  4103. private->level_switch);
  4104. }
  4105. static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
  4106. struct snd_ctl_elem_info *uinfo)
  4107. {
  4108. static const char *const values[2] = {
  4109. "Line", "Inst"
  4110. };
  4111. struct usb_mixer_elem_info *elem = kctl->private_data;
  4112. struct usb_mixer_interface *mixer = elem->head.mixer;
  4113. struct scarlett2_data *private = mixer->private_data;
  4114. int err;
  4115. mutex_lock(&private->data_mutex);
  4116. if (private->hwdep_in_use) {
  4117. err = -EBUSY;
  4118. goto unlock;
  4119. }
  4120. err = scarlett2_check_autogain_updated(mixer);
  4121. if (err < 0)
  4122. goto unlock;
  4123. err = snd_ctl_enum_info(uinfo, 1, 2, values);
  4124. unlock:
  4125. mutex_unlock(&private->data_mutex);
  4126. return err;
  4127. }
  4128. static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
  4129. struct snd_ctl_elem_value *ucontrol)
  4130. {
  4131. struct usb_mixer_elem_info *elem = kctl->private_data;
  4132. struct usb_mixer_interface *mixer = elem->head.mixer;
  4133. struct scarlett2_data *private = mixer->private_data;
  4134. const struct scarlett2_device_info *info = private->info;
  4135. int index = elem->control + info->level_input_first;
  4136. int err = 0;
  4137. mutex_lock(&private->data_mutex);
  4138. if (private->hwdep_in_use) {
  4139. err = -EBUSY;
  4140. goto unlock;
  4141. }
  4142. if (private->input_level_updated) {
  4143. err = scarlett2_update_input_level(mixer);
  4144. if (err < 0)
  4145. goto unlock;
  4146. }
  4147. ucontrol->value.enumerated.item[0] = scarlett2_decode_muteable(
  4148. private->level_switch[index]);
  4149. unlock:
  4150. mutex_unlock(&private->data_mutex);
  4151. return err;
  4152. }
  4153. static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
  4154. struct snd_ctl_elem_value *ucontrol)
  4155. {
  4156. struct usb_mixer_elem_info *elem = kctl->private_data;
  4157. struct usb_mixer_interface *mixer = elem->head.mixer;
  4158. struct scarlett2_data *private = mixer->private_data;
  4159. const struct scarlett2_device_info *info = private->info;
  4160. int index = elem->control + info->level_input_first;
  4161. int oval, val, err;
  4162. mutex_lock(&private->data_mutex);
  4163. if (private->hwdep_in_use) {
  4164. err = -EBUSY;
  4165. goto unlock;
  4166. }
  4167. err = scarlett2_check_put_during_autogain(mixer);
  4168. if (err < 0)
  4169. goto unlock;
  4170. oval = private->level_switch[index];
  4171. val = !!ucontrol->value.enumerated.item[0];
  4172. if (oval == val)
  4173. goto unlock;
  4174. private->level_switch[index] = val;
  4175. /* To set the Gen 4 muteable controls, bit 1 gets set instead */
  4176. if (private->config_set->items[SCARLETT2_CONFIG_LEVEL_SWITCH].mute)
  4177. val = (!val) | 0x02;
  4178. /* Send switch change to the device */
  4179. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
  4180. index, val);
  4181. if (err == 0)
  4182. err = 1;
  4183. unlock:
  4184. mutex_unlock(&private->data_mutex);
  4185. return err;
  4186. }
  4187. static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
  4188. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4189. .name = "",
  4190. .info = scarlett2_level_enum_ctl_info,
  4191. .get = scarlett2_level_enum_ctl_get,
  4192. .put = scarlett2_level_enum_ctl_put,
  4193. };
  4194. /*** Pad Switch Controls ***/
  4195. static int scarlett2_update_input_pad(struct usb_mixer_interface *mixer)
  4196. {
  4197. struct scarlett2_data *private = mixer->private_data;
  4198. const struct scarlett2_device_info *info = private->info;
  4199. private->input_pad_updated = 0;
  4200. if (!info->pad_input_count)
  4201. return 0;
  4202. return scarlett2_usb_get_config(
  4203. mixer, SCARLETT2_CONFIG_PAD_SWITCH,
  4204. info->pad_input_count, private->pad_switch);
  4205. }
  4206. static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
  4207. struct snd_ctl_elem_value *ucontrol)
  4208. {
  4209. struct usb_mixer_elem_info *elem = kctl->private_data;
  4210. struct usb_mixer_interface *mixer = elem->head.mixer;
  4211. struct scarlett2_data *private = mixer->private_data;
  4212. int err = 0;
  4213. mutex_lock(&private->data_mutex);
  4214. if (private->hwdep_in_use) {
  4215. err = -EBUSY;
  4216. goto unlock;
  4217. }
  4218. if (private->input_pad_updated) {
  4219. err = scarlett2_update_input_pad(mixer);
  4220. if (err < 0)
  4221. goto unlock;
  4222. }
  4223. ucontrol->value.integer.value[0] =
  4224. private->pad_switch[elem->control];
  4225. unlock:
  4226. mutex_unlock(&private->data_mutex);
  4227. return err;
  4228. }
  4229. static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
  4230. struct snd_ctl_elem_value *ucontrol)
  4231. {
  4232. struct usb_mixer_elem_info *elem = kctl->private_data;
  4233. struct usb_mixer_interface *mixer = elem->head.mixer;
  4234. struct scarlett2_data *private = mixer->private_data;
  4235. int index = elem->control;
  4236. int oval, val, err = 0;
  4237. mutex_lock(&private->data_mutex);
  4238. if (private->hwdep_in_use) {
  4239. err = -EBUSY;
  4240. goto unlock;
  4241. }
  4242. oval = private->pad_switch[index];
  4243. val = !!ucontrol->value.integer.value[0];
  4244. if (oval == val)
  4245. goto unlock;
  4246. private->pad_switch[index] = val;
  4247. /* Send switch change to the device */
  4248. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
  4249. index, val);
  4250. if (err == 0)
  4251. err = 1;
  4252. unlock:
  4253. mutex_unlock(&private->data_mutex);
  4254. return err;
  4255. }
  4256. static const struct snd_kcontrol_new scarlett2_pad_ctl = {
  4257. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4258. .name = "",
  4259. .info = snd_ctl_boolean_mono_info,
  4260. .get = scarlett2_pad_ctl_get,
  4261. .put = scarlett2_pad_ctl_put,
  4262. };
  4263. /*** Air Switch Controls ***/
  4264. static int scarlett2_update_input_air(struct usb_mixer_interface *mixer)
  4265. {
  4266. struct scarlett2_data *private = mixer->private_data;
  4267. const struct scarlett2_device_info *info = private->info;
  4268. private->input_air_updated = 0;
  4269. if (!info->air_input_count)
  4270. return 0;
  4271. return scarlett2_usb_get_config(
  4272. mixer, SCARLETT2_CONFIG_AIR_SWITCH,
  4273. info->air_input_count, private->air_switch);
  4274. }
  4275. static int scarlett2_air_ctl_get(struct snd_kcontrol *kctl,
  4276. struct snd_ctl_elem_value *ucontrol)
  4277. {
  4278. struct usb_mixer_elem_info *elem = kctl->private_data;
  4279. struct usb_mixer_interface *mixer = elem->head.mixer;
  4280. struct scarlett2_data *private = mixer->private_data;
  4281. int err = 0;
  4282. mutex_lock(&private->data_mutex);
  4283. if (private->hwdep_in_use) {
  4284. err = -EBUSY;
  4285. goto unlock;
  4286. }
  4287. if (private->input_air_updated) {
  4288. err = scarlett2_update_input_air(mixer);
  4289. if (err < 0)
  4290. goto unlock;
  4291. }
  4292. ucontrol->value.integer.value[0] = private->air_switch[elem->control];
  4293. unlock:
  4294. mutex_unlock(&private->data_mutex);
  4295. return err;
  4296. }
  4297. static int scarlett2_air_ctl_put(struct snd_kcontrol *kctl,
  4298. struct snd_ctl_elem_value *ucontrol)
  4299. {
  4300. struct usb_mixer_elem_info *elem = kctl->private_data;
  4301. struct usb_mixer_interface *mixer = elem->head.mixer;
  4302. struct scarlett2_data *private = mixer->private_data;
  4303. int index = elem->control;
  4304. int oval, val, err;
  4305. mutex_lock(&private->data_mutex);
  4306. if (private->hwdep_in_use) {
  4307. err = -EBUSY;
  4308. goto unlock;
  4309. }
  4310. err = scarlett2_check_put_during_autogain(mixer);
  4311. if (err < 0)
  4312. goto unlock;
  4313. oval = private->air_switch[index];
  4314. val = ucontrol->value.integer.value[0];
  4315. if (oval == val)
  4316. goto unlock;
  4317. private->air_switch[index] = val;
  4318. /* Send switch change to the device */
  4319. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_AIR_SWITCH,
  4320. index, val);
  4321. if (err == 0)
  4322. err = 1;
  4323. unlock:
  4324. mutex_unlock(&private->data_mutex);
  4325. return err;
  4326. }
  4327. static int scarlett2_air_with_drive_ctl_info(
  4328. struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
  4329. {
  4330. static const char *const values[3] = {
  4331. "Off", "Presence", "Presence + Drive"
  4332. };
  4333. struct usb_mixer_elem_info *elem = kctl->private_data;
  4334. struct usb_mixer_interface *mixer = elem->head.mixer;
  4335. struct scarlett2_data *private = mixer->private_data;
  4336. int err;
  4337. mutex_lock(&private->data_mutex);
  4338. if (private->hwdep_in_use) {
  4339. err = -EBUSY;
  4340. goto unlock;
  4341. }
  4342. err = scarlett2_check_autogain_updated(mixer);
  4343. if (err < 0)
  4344. goto unlock;
  4345. err = snd_ctl_enum_info(uinfo, 1, 3, values);
  4346. unlock:
  4347. mutex_unlock(&private->data_mutex);
  4348. return err;
  4349. }
  4350. static const struct snd_kcontrol_new scarlett2_air_ctl[2] = {
  4351. {
  4352. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4353. .name = "",
  4354. .info = snd_ctl_boolean_mono_info,
  4355. .get = scarlett2_air_ctl_get,
  4356. .put = scarlett2_air_ctl_put,
  4357. },
  4358. {
  4359. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4360. .name = "",
  4361. .info = scarlett2_air_with_drive_ctl_info,
  4362. .get = scarlett2_air_ctl_get,
  4363. .put = scarlett2_air_ctl_put,
  4364. }
  4365. };
  4366. /*** DSP Switch Control ***/
  4367. static int scarlett2_update_input_dsp(struct usb_mixer_interface *mixer)
  4368. {
  4369. struct scarlett2_data *private = mixer->private_data;
  4370. const struct scarlett2_device_info *info = private->info;
  4371. private->input_dsp_updated = 0;
  4372. if (!info->dsp_input_count)
  4373. return 0;
  4374. return scarlett2_usb_get_config(
  4375. mixer, SCARLETT2_CONFIG_DSP_SWITCH,
  4376. info->dsp_input_count, private->dsp_switch);
  4377. }
  4378. static int scarlett2_dsp_ctl_get(struct snd_kcontrol *kctl,
  4379. struct snd_ctl_elem_value *ucontrol)
  4380. {
  4381. struct usb_mixer_elem_info *elem = kctl->private_data;
  4382. struct usb_mixer_interface *mixer = elem->head.mixer;
  4383. struct scarlett2_data *private = mixer->private_data;
  4384. int err = 0;
  4385. mutex_lock(&private->data_mutex);
  4386. if (private->hwdep_in_use) {
  4387. err = -EBUSY;
  4388. goto unlock;
  4389. }
  4390. if (private->input_dsp_updated) {
  4391. err = scarlett2_update_input_dsp(mixer);
  4392. if (err < 0)
  4393. goto unlock;
  4394. }
  4395. ucontrol->value.integer.value[0] = private->dsp_switch[elem->control];
  4396. unlock:
  4397. mutex_unlock(&private->data_mutex);
  4398. return err;
  4399. }
  4400. static int scarlett2_dsp_ctl_put(struct snd_kcontrol *kctl,
  4401. struct snd_ctl_elem_value *ucontrol)
  4402. {
  4403. struct usb_mixer_elem_info *elem = kctl->private_data;
  4404. struct usb_mixer_interface *mixer = elem->head.mixer;
  4405. struct scarlett2_data *private = mixer->private_data;
  4406. int index = elem->control;
  4407. int oval, val, err;
  4408. mutex_lock(&private->data_mutex);
  4409. if (private->hwdep_in_use) {
  4410. err = -EBUSY;
  4411. goto unlock;
  4412. }
  4413. err = scarlett2_check_put_during_autogain(mixer);
  4414. if (err < 0)
  4415. goto unlock;
  4416. oval = private->dsp_switch[index];
  4417. val = ucontrol->value.integer.value[0];
  4418. if (oval == val)
  4419. goto unlock;
  4420. private->dsp_switch[index] = val;
  4421. /* Send switch change to the device */
  4422. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DSP_SWITCH,
  4423. index, val);
  4424. if (err == 0)
  4425. err = 1;
  4426. unlock:
  4427. mutex_unlock(&private->data_mutex);
  4428. return err;
  4429. }
  4430. static const struct snd_kcontrol_new scarlett2_dsp_ctl = {
  4431. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4432. .name = "",
  4433. .info = scarlett2_autogain_disables_ctl_info,
  4434. .get = scarlett2_dsp_ctl_get,
  4435. .put = scarlett2_dsp_ctl_put,
  4436. };
  4437. /*** DSP Compressor Parameter Controls ***/
  4438. static int scarlett2_update_compressor_values(struct usb_mixer_interface *mixer)
  4439. {
  4440. struct scarlett2_data *private = mixer->private_data;
  4441. const struct scarlett2_device_info *info = private->info;
  4442. int err, i, j;
  4443. if (!info->dsp_input_count)
  4444. return 0;
  4445. err = scarlett2_usb_get_config(
  4446. mixer, SCARLETT2_CONFIG_COMPRESSOR_PARAMS,
  4447. SCARLETT2_COMPRESSOR_PARAM_COUNT * info->dsp_input_count,
  4448. private->compressor_values);
  4449. if (err < 0)
  4450. return err;
  4451. for (i = 0; i < SCARLETT2_COMPRESSOR_PARAM_COUNT; i++) {
  4452. const struct compressor_param *param = &compressor_params[i];
  4453. for (j = 0; j < info->dsp_input_count; j++) {
  4454. int idx = i + j * SCARLETT2_COMPRESSOR_PARAM_COUNT;
  4455. int val = private->compressor_values[idx];
  4456. val >>= param->scale_bits;
  4457. val = clamp(val, param->min, param->max);
  4458. private->compressor_values[idx] = val;
  4459. }
  4460. }
  4461. return 0;
  4462. }
  4463. static int scarlett2_compressor_ctl_get(
  4464. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  4465. {
  4466. struct usb_mixer_elem_info *elem = kctl->private_data;
  4467. struct scarlett2_data *private = elem->head.mixer->private_data;
  4468. ucontrol->value.integer.value[0] =
  4469. private->compressor_values[elem->control];
  4470. return 0;
  4471. }
  4472. static int scarlett2_compressor_ctl_put(
  4473. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  4474. {
  4475. struct usb_mixer_elem_info *elem = kctl->private_data;
  4476. struct usb_mixer_interface *mixer = elem->head.mixer;
  4477. struct scarlett2_data *private = mixer->private_data;
  4478. int index = elem->control;
  4479. int channel = index / SCARLETT2_COMPRESSOR_PARAM_COUNT;
  4480. int param_index = index % SCARLETT2_COMPRESSOR_PARAM_COUNT;
  4481. int oval, val, err;
  4482. s32 scaled_val;
  4483. mutex_lock(&private->data_mutex);
  4484. if (private->hwdep_in_use) {
  4485. err = -EBUSY;
  4486. goto unlock;
  4487. }
  4488. err = scarlett2_check_put_during_autogain(mixer);
  4489. if (err < 0)
  4490. goto unlock;
  4491. oval = private->compressor_values[index];
  4492. val = ucontrol->value.integer.value[0];
  4493. if (oval == val)
  4494. goto unlock;
  4495. private->compressor_values[index] = val;
  4496. const struct compressor_param *param = &compressor_params[param_index];
  4497. scaled_val = val << param->scale_bits;
  4498. /* Send change to the device */
  4499. /* The channel needs to be put in the parameter buffer index
  4500. * field (param_buf_addr + 1); the value field isn't used in
  4501. * this case.
  4502. */
  4503. err = scarlett2_usb_set_data(
  4504. mixer, private->config_set->param_buf_addr + 1, 1, channel);
  4505. if (err < 0)
  4506. goto unlock;
  4507. err = scarlett2_usb_set_config(
  4508. mixer, SCARLETT2_CONFIG_COMPRESSOR_PARAMS, index, scaled_val);
  4509. if (err < 0)
  4510. goto unlock;
  4511. if (err == 0)
  4512. err = 1;
  4513. unlock:
  4514. mutex_unlock(&private->data_mutex);
  4515. return err;
  4516. }
  4517. static int scarlett2_compressor_ctl_info(
  4518. struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
  4519. {
  4520. struct usb_mixer_elem_info *elem = kctl->private_data;
  4521. int control = elem->control % SCARLETT2_COMPRESSOR_PARAM_COUNT;
  4522. uinfo->type = compressor_params[control].type;
  4523. uinfo->count = 1;
  4524. uinfo->value.integer.min = compressor_params[control].min;
  4525. uinfo->value.integer.max = compressor_params[control].max;
  4526. uinfo->value.integer.step = 1;
  4527. return 0;
  4528. }
  4529. static const struct snd_kcontrol_new scarlett2_compressor_ctl = {
  4530. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  4531. .name = "",
  4532. .info = scarlett2_compressor_ctl_info,
  4533. .get = scarlett2_compressor_ctl_get,
  4534. .put = scarlett2_compressor_ctl_put,
  4535. };
  4536. /*** DSP Pre-Compressor and PEQ Filter Controls ***/
  4537. static int scarlett2_precomp_flt_switch_ctl_get(
  4538. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  4539. {
  4540. struct usb_mixer_elem_info *elem = kctl->private_data;
  4541. struct scarlett2_data *private = elem->head.mixer->private_data;
  4542. ucontrol->value.integer.value[0] = private->precomp_flt_switch[elem->control];
  4543. return 0;
  4544. }
  4545. static int scarlett2_peq_flt_switch_ctl_get(
  4546. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  4547. {
  4548. struct usb_mixer_elem_info *elem = kctl->private_data;
  4549. struct scarlett2_data *private = elem->head.mixer->private_data;
  4550. ucontrol->value.integer.value[0] =
  4551. private->peq_flt_switch[elem->control];
  4552. return 0;
  4553. }
  4554. static int scarlett2_precomp_flt_switch_ctl_put(
  4555. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  4556. {
  4557. struct usb_mixer_elem_info *elem = kctl->private_data;
  4558. struct usb_mixer_interface *mixer = elem->head.mixer;
  4559. struct scarlett2_data *private = mixer->private_data;
  4560. int oval, val, err = 0;
  4561. mutex_lock(&private->data_mutex);
  4562. if (private->hwdep_in_use) {
  4563. err = -EBUSY;
  4564. goto unlock;
  4565. }
  4566. oval = private->precomp_flt_switch[elem->control];
  4567. val = ucontrol->value.integer.value[0];
  4568. if (oval == val)
  4569. goto unlock;
  4570. private->precomp_flt_switch[elem->control] = val;
  4571. /* Send change to the device */
  4572. err = scarlett2_usb_set_config(
  4573. mixer, SCARLETT2_CONFIG_PRECOMP_FLT_SWITCH,
  4574. elem->control, val);
  4575. if (err == 0)
  4576. err = 1;
  4577. unlock:
  4578. mutex_unlock(&private->data_mutex);
  4579. return err;
  4580. }
  4581. static int scarlett2_peq_flt_switch_ctl_put(
  4582. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  4583. {
  4584. struct usb_mixer_elem_info *elem = kctl->private_data;
  4585. struct usb_mixer_interface *mixer = elem->head.mixer;
  4586. struct scarlett2_data *private = mixer->private_data;
  4587. int oval, val, err = 0;
  4588. mutex_lock(&private->data_mutex);
  4589. if (private->hwdep_in_use) {
  4590. err = -EBUSY;
  4591. goto unlock;
  4592. }
  4593. oval = private->peq_flt_switch[elem->control];
  4594. val = ucontrol->value.integer.value[0];
  4595. if (oval == val)
  4596. goto unlock;
  4597. private->peq_flt_switch[elem->control] = val;
  4598. /* Send change to the device */
  4599. err = scarlett2_usb_set_config(
  4600. mixer, SCARLETT2_CONFIG_PEQ_FLT_SWITCH,
  4601. elem->control, val);
  4602. if (err == 0)
  4603. err = 1;
  4604. unlock:
  4605. mutex_unlock(&private->data_mutex);
  4606. return err;
  4607. }
  4608. static const struct snd_kcontrol_new scarlett2_precomp_flt_switch_ctl = {
  4609. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  4610. .name = "",
  4611. .info = snd_ctl_boolean_mono_info,
  4612. .get = scarlett2_precomp_flt_switch_ctl_get,
  4613. .put = scarlett2_precomp_flt_switch_ctl_put,
  4614. };
  4615. static const struct snd_kcontrol_new scarlett2_peq_flt_switch_ctl = {
  4616. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  4617. .name = "",
  4618. .info = snd_ctl_boolean_mono_info,
  4619. .get = scarlett2_peq_flt_switch_ctl_get,
  4620. .put = scarlett2_peq_flt_switch_ctl_put,
  4621. };
  4622. static int scarlett2_update_filter_values(struct usb_mixer_interface *mixer)
  4623. {
  4624. struct scarlett2_data *private = mixer->private_data;
  4625. const struct scarlett2_device_info *info = private->info;
  4626. int err, i, j, k, src_idx, dst_idx;
  4627. s32 peq_flt_values[SCARLETT2_DSP_SWITCH_MAX *
  4628. SCARLETT2_PEQ_FLT_SLOTS_MAX *
  4629. SCARLETT2_BIQUAD_COEFFS];
  4630. if (!info->dsp_input_count)
  4631. return 0;
  4632. /* Get filter switch values */
  4633. err = scarlett2_usb_get_config(
  4634. mixer, SCARLETT2_CONFIG_PRECOMP_FLT_SWITCH,
  4635. info->dsp_input_count, private->precomp_flt_switch);
  4636. if (err < 0)
  4637. return err;
  4638. err = scarlett2_usb_get_config(
  4639. mixer, SCARLETT2_CONFIG_PEQ_FLT_SWITCH,
  4640. info->dsp_input_count * info->peq_flt_count,
  4641. private->peq_flt_switch);
  4642. if (err < 0)
  4643. return err;
  4644. /* Get pre-compressor filter values directly */
  4645. err = scarlett2_usb_get_config(
  4646. mixer, SCARLETT2_CONFIG_PRECOMP_FLT_PARAMS,
  4647. info->dsp_input_count *
  4648. info->precomp_flt_count *
  4649. SCARLETT2_BIQUAD_COEFFS,
  4650. private->precomp_flt_values);
  4651. if (err < 0)
  4652. return err;
  4653. /* PEQ filter values need to be copied via buffer because of
  4654. * padding after peq_flt_count up to peq_flt_total_count
  4655. */
  4656. err = scarlett2_usb_get_config(
  4657. mixer, SCARLETT2_CONFIG_PEQ_FLT_PARAMS,
  4658. info->dsp_input_count *
  4659. info->peq_flt_total_count *
  4660. SCARLETT2_BIQUAD_COEFFS,
  4661. peq_flt_values);
  4662. if (err < 0)
  4663. return err;
  4664. for (i = 0, dst_idx = 0; i < info->dsp_input_count; i++) {
  4665. src_idx = i *
  4666. info->peq_flt_total_count *
  4667. SCARLETT2_BIQUAD_COEFFS;
  4668. for (j = 0; j < info->peq_flt_count; j++)
  4669. for (k = 0;
  4670. k < SCARLETT2_BIQUAD_COEFFS;
  4671. k++, src_idx++, dst_idx++)
  4672. private->peq_flt_values[dst_idx] =
  4673. peq_flt_values[src_idx];
  4674. }
  4675. return 0;
  4676. }
  4677. static int scarlett2_precomp_flt_ctl_get(
  4678. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  4679. {
  4680. struct usb_mixer_elem_info *elem = kctl->private_data;
  4681. struct scarlett2_data *private = elem->head.mixer->private_data;
  4682. int i, idx;
  4683. for (i = 0, idx = elem->control * SCARLETT2_BIQUAD_COEFFS;
  4684. i < SCARLETT2_BIQUAD_COEFFS;
  4685. i++, idx++)
  4686. ucontrol->value.integer.value[i] =
  4687. private->precomp_flt_values[idx];
  4688. return 0;
  4689. }
  4690. static int scarlett2_peq_flt_ctl_get(
  4691. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  4692. {
  4693. struct usb_mixer_elem_info *elem = kctl->private_data;
  4694. struct scarlett2_data *private = elem->head.mixer->private_data;
  4695. int i, idx;
  4696. for (i = 0, idx = elem->control * SCARLETT2_BIQUAD_COEFFS;
  4697. i < SCARLETT2_BIQUAD_COEFFS;
  4698. i++, idx++)
  4699. ucontrol->value.integer.value[i] =
  4700. private->peq_flt_values[idx];
  4701. return 0;
  4702. }
  4703. static int scarlett2_precomp_flt_ctl_put(
  4704. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  4705. {
  4706. struct usb_mixer_elem_info *elem = kctl->private_data;
  4707. struct usb_mixer_interface *mixer = elem->head.mixer;
  4708. struct scarlett2_data *private = mixer->private_data;
  4709. int index = elem->control * SCARLETT2_BIQUAD_COEFFS;
  4710. int i, oval, val, err;
  4711. mutex_lock(&private->data_mutex);
  4712. if (private->hwdep_in_use) {
  4713. err = -EBUSY;
  4714. goto unlock;
  4715. }
  4716. err = scarlett2_check_put_during_autogain(mixer);
  4717. if (err < 0)
  4718. goto unlock;
  4719. /* Check if any of the values have changed; if not, return */
  4720. for (i = 0; i < SCARLETT2_BIQUAD_COEFFS; i++) {
  4721. oval = private->precomp_flt_values[index + i];
  4722. val = ucontrol->value.integer.value[i];
  4723. if (oval != val)
  4724. break;
  4725. }
  4726. if (i == SCARLETT2_BIQUAD_COEFFS)
  4727. goto unlock;
  4728. /* Update the values */
  4729. for (i = 0; i < SCARLETT2_BIQUAD_COEFFS; i++)
  4730. private->precomp_flt_values[index + i] =
  4731. ucontrol->value.integer.value[i];
  4732. /* Send change to the device */
  4733. err = scarlett2_usb_set_data(
  4734. mixer, private->config_set->param_buf_addr, 1, index);
  4735. if (err < 0)
  4736. goto unlock;
  4737. err = scarlett2_usb_set_config_buf(
  4738. mixer, SCARLETT2_CONFIG_PRECOMP_FLT_PARAMS,
  4739. index, SCARLETT2_BIQUAD_COEFFS,
  4740. &private->precomp_flt_values[index]);
  4741. if (err == 0)
  4742. err = 1;
  4743. unlock:
  4744. mutex_unlock(&private->data_mutex);
  4745. return err;
  4746. }
  4747. static int scarlett2_peq_flt_ctl_put(
  4748. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  4749. {
  4750. struct usb_mixer_elem_info *elem = kctl->private_data;
  4751. struct usb_mixer_interface *mixer = elem->head.mixer;
  4752. struct scarlett2_data *private = mixer->private_data;
  4753. const struct scarlett2_device_info *info = private->info;
  4754. int src_index = elem->control * SCARLETT2_BIQUAD_COEFFS;
  4755. int dst_index = (
  4756. elem->control /
  4757. info->peq_flt_count *
  4758. info->peq_flt_total_count +
  4759. elem->control % info->peq_flt_count
  4760. ) * SCARLETT2_BIQUAD_COEFFS;
  4761. int i, oval, val, err;
  4762. mutex_lock(&private->data_mutex);
  4763. if (private->hwdep_in_use) {
  4764. err = -EBUSY;
  4765. goto unlock;
  4766. }
  4767. err = scarlett2_check_put_during_autogain(mixer);
  4768. if (err < 0)
  4769. goto unlock;
  4770. /* Check if any of the values have changed; if not, return */
  4771. for (i = 0; i < SCARLETT2_BIQUAD_COEFFS; i++) {
  4772. oval = private->peq_flt_values[src_index + i];
  4773. val = ucontrol->value.integer.value[i];
  4774. if (oval != val)
  4775. break;
  4776. }
  4777. if (i == SCARLETT2_BIQUAD_COEFFS)
  4778. goto unlock;
  4779. /* Update the values */
  4780. for (i = 0; i < SCARLETT2_BIQUAD_COEFFS; i++)
  4781. private->peq_flt_values[src_index + i] =
  4782. ucontrol->value.integer.value[i];
  4783. /* Send change to the device */
  4784. err = scarlett2_usb_set_data(
  4785. mixer, private->config_set->param_buf_addr, 1, dst_index);
  4786. if (err < 0)
  4787. goto unlock;
  4788. err = scarlett2_usb_set_config_buf(
  4789. mixer, SCARLETT2_CONFIG_PEQ_FLT_PARAMS,
  4790. dst_index, SCARLETT2_BIQUAD_COEFFS,
  4791. &private->peq_flt_values[src_index]);
  4792. if (err == 0)
  4793. err = 1;
  4794. unlock:
  4795. mutex_unlock(&private->data_mutex);
  4796. return err;
  4797. }
  4798. static int scarlett2_flt_ctl_info(
  4799. struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
  4800. {
  4801. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4802. uinfo->count = SCARLETT2_BIQUAD_COEFFS;
  4803. uinfo->value.integer.min = INT_MIN;
  4804. uinfo->value.integer.max = INT_MAX;
  4805. uinfo->value.integer.step = 1;
  4806. return 0;
  4807. }
  4808. static const struct snd_kcontrol_new scarlett2_precomp_flt_ctl = {
  4809. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  4810. .name = "",
  4811. .info = scarlett2_flt_ctl_info,
  4812. .get = scarlett2_precomp_flt_ctl_get,
  4813. .put = scarlett2_precomp_flt_ctl_put,
  4814. };
  4815. static const struct snd_kcontrol_new scarlett2_peq_flt_ctl = {
  4816. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  4817. .name = "",
  4818. .info = scarlett2_flt_ctl_info,
  4819. .get = scarlett2_peq_flt_ctl_get,
  4820. .put = scarlett2_peq_flt_ctl_put,
  4821. };
  4822. /*** Input Mute Switch Controls ***/
  4823. static int scarlett2_update_input_mute(struct usb_mixer_interface *mixer)
  4824. {
  4825. struct scarlett2_data *private = mixer->private_data;
  4826. const struct scarlett2_device_info *info = private->info;
  4827. private->input_mute_updated = 0;
  4828. if (!info->mute_input_count)
  4829. return 0;
  4830. return scarlett2_usb_get_config(
  4831. mixer, SCARLETT2_CONFIG_INPUT_MUTE_SWITCH,
  4832. info->mute_input_count, private->input_mute_switch);
  4833. }
  4834. static int scarlett2_input_mute_ctl_get(struct snd_kcontrol *kctl,
  4835. struct snd_ctl_elem_value *ucontrol)
  4836. {
  4837. struct usb_mixer_elem_info *elem = kctl->private_data;
  4838. struct usb_mixer_interface *mixer = elem->head.mixer;
  4839. struct scarlett2_data *private = mixer->private_data;
  4840. int err = 0;
  4841. mutex_lock(&private->data_mutex);
  4842. if (private->hwdep_in_use) {
  4843. err = -EBUSY;
  4844. goto unlock;
  4845. }
  4846. if (private->input_mute_updated) {
  4847. err = scarlett2_update_input_mute(mixer);
  4848. if (err < 0)
  4849. goto unlock;
  4850. }
  4851. ucontrol->value.integer.value[0] =
  4852. private->input_mute_switch[elem->control];
  4853. unlock:
  4854. mutex_unlock(&private->data_mutex);
  4855. return err;
  4856. }
  4857. static int scarlett2_input_mute_ctl_put(struct snd_kcontrol *kctl,
  4858. struct snd_ctl_elem_value *ucontrol)
  4859. {
  4860. struct usb_mixer_elem_info *elem = kctl->private_data;
  4861. struct usb_mixer_interface *mixer = elem->head.mixer;
  4862. struct scarlett2_data *private = mixer->private_data;
  4863. int index = elem->control;
  4864. int oval, val, err;
  4865. mutex_lock(&private->data_mutex);
  4866. if (private->hwdep_in_use) {
  4867. err = -EBUSY;
  4868. goto unlock;
  4869. }
  4870. err = scarlett2_check_put_during_autogain(mixer);
  4871. if (err < 0)
  4872. goto unlock;
  4873. oval = private->input_mute_switch[index];
  4874. val = ucontrol->value.integer.value[0];
  4875. if (oval == val)
  4876. goto unlock;
  4877. private->input_mute_switch[index] = val;
  4878. /* Send switch change to the device */
  4879. err = scarlett2_usb_set_config(
  4880. mixer, SCARLETT2_CONFIG_INPUT_MUTE_SWITCH,
  4881. index, val);
  4882. if (err == 0)
  4883. err = 1;
  4884. unlock:
  4885. mutex_unlock(&private->data_mutex);
  4886. return err;
  4887. }
  4888. static const struct snd_kcontrol_new scarlett2_input_mute_ctl = {
  4889. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  4890. .name = "",
  4891. .info = scarlett2_autogain_disables_ctl_info,
  4892. .get = scarlett2_input_mute_ctl_get,
  4893. .put = scarlett2_input_mute_ctl_put,
  4894. };
  4895. /*** Phantom Switch Controls ***/
  4896. static int scarlett2_update_input_phantom(struct usb_mixer_interface *mixer)
  4897. {
  4898. struct scarlett2_data *private = mixer->private_data;
  4899. const struct scarlett2_device_info *info = private->info;
  4900. int err;
  4901. private->input_phantom_updated = 0;
  4902. if (!info->phantom_count)
  4903. return 0;
  4904. err = scarlett2_usb_get_config(
  4905. mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
  4906. info->phantom_count, private->phantom_switch);
  4907. if (err < 0)
  4908. return err;
  4909. if (scarlett2_has_config_item(private,
  4910. SCARLETT2_CONFIG_PHANTOM_PERSISTENCE)) {
  4911. err = scarlett2_usb_get_config(
  4912. mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
  4913. 1, &private->phantom_persistence);
  4914. if (err < 0)
  4915. return err;
  4916. }
  4917. return 0;
  4918. }
  4919. /* Check if phantom power on the given input is currently changing state */
  4920. static int scarlett2_phantom_is_switching(
  4921. struct scarlett2_data *private, int line_num)
  4922. {
  4923. const struct scarlett2_device_info *info = private->info;
  4924. int index = line_num / info->inputs_per_phantom;
  4925. return !!(private->phantom_switch[index] & 0x02);
  4926. }
  4927. /* Update autogain controls' access mode when phantom power changes state */
  4928. static void scarlett2_phantom_update_access(struct usb_mixer_interface *mixer)
  4929. {
  4930. struct scarlett2_data *private = mixer->private_data;
  4931. const struct scarlett2_device_info *info = private->info;
  4932. int i;
  4933. /* Disable autogain controls if phantom power is changing state */
  4934. for (i = 0; i < info->gain_input_count; i++) {
  4935. int val = !scarlett2_phantom_is_switching(private, i);
  4936. scarlett2_set_ctl_access(private->autogain_ctls[i], val);
  4937. }
  4938. }
  4939. /* Notify of access mode change for autogain which can't be enabled
  4940. * while phantom power is changing.
  4941. */
  4942. static void scarlett2_phantom_notify_access(struct usb_mixer_interface *mixer)
  4943. {
  4944. struct snd_card *card = mixer->chip->card;
  4945. struct scarlett2_data *private = mixer->private_data;
  4946. const struct scarlett2_device_info *info = private->info;
  4947. int i;
  4948. for (i = 0; i < info->gain_input_count; i++)
  4949. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
  4950. &private->autogain_ctls[i]->id);
  4951. }
  4952. /* Call scarlett2_update_input_phantom() and
  4953. * scarlett2_phantom_update_access() if input_phantom_updated is set.
  4954. */
  4955. static int scarlett2_check_input_phantom_updated(
  4956. struct usb_mixer_interface *mixer)
  4957. {
  4958. struct scarlett2_data *private = mixer->private_data;
  4959. int err;
  4960. if (!private->input_phantom_updated)
  4961. return 0;
  4962. err = scarlett2_update_input_phantom(mixer);
  4963. if (err < 0)
  4964. return err;
  4965. scarlett2_phantom_update_access(mixer);
  4966. return 0;
  4967. }
  4968. static int scarlett2_phantom_ctl_get(struct snd_kcontrol *kctl,
  4969. struct snd_ctl_elem_value *ucontrol)
  4970. {
  4971. struct usb_mixer_elem_info *elem = kctl->private_data;
  4972. struct usb_mixer_interface *mixer = elem->head.mixer;
  4973. struct scarlett2_data *private = mixer->private_data;
  4974. int err;
  4975. mutex_lock(&private->data_mutex);
  4976. if (private->hwdep_in_use) {
  4977. err = -EBUSY;
  4978. goto unlock;
  4979. }
  4980. err = scarlett2_check_input_phantom_updated(mixer);
  4981. if (err < 0)
  4982. goto unlock;
  4983. ucontrol->value.integer.value[0] = scarlett2_decode_muteable(
  4984. private->phantom_switch[elem->control]);
  4985. unlock:
  4986. mutex_unlock(&private->data_mutex);
  4987. return err;
  4988. }
  4989. static int scarlett2_phantom_ctl_put(struct snd_kcontrol *kctl,
  4990. struct snd_ctl_elem_value *ucontrol)
  4991. {
  4992. struct usb_mixer_elem_info *elem = kctl->private_data;
  4993. struct usb_mixer_interface *mixer = elem->head.mixer;
  4994. struct scarlett2_data *private = mixer->private_data;
  4995. const struct scarlett2_device_info *info = private->info;
  4996. int index = elem->control;
  4997. int oval, val, err;
  4998. mutex_lock(&private->data_mutex);
  4999. if (private->hwdep_in_use) {
  5000. err = -EBUSY;
  5001. goto unlock;
  5002. }
  5003. err = scarlett2_check_put_during_autogain(mixer);
  5004. if (err < 0)
  5005. goto unlock;
  5006. oval = private->phantom_switch[index];
  5007. val = !!ucontrol->value.integer.value[0];
  5008. if (oval == val)
  5009. goto unlock;
  5010. private->phantom_switch[index] = val;
  5011. /* To set the Gen 4 muteable controls, bit 1 gets set */
  5012. if (private->config_set->items[SCARLETT2_CONFIG_PHANTOM_SWITCH].mute)
  5013. val = (!val) | 0x02;
  5014. /* Send switch change to the device */
  5015. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
  5016. index + info->phantom_first, val);
  5017. if (err == 0)
  5018. err = 1;
  5019. scarlett2_phantom_update_access(mixer);
  5020. scarlett2_phantom_notify_access(mixer);
  5021. unlock:
  5022. mutex_unlock(&private->data_mutex);
  5023. return err;
  5024. }
  5025. static const struct snd_kcontrol_new scarlett2_phantom_ctl = {
  5026. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  5027. .name = "",
  5028. .info = scarlett2_autogain_disables_ctl_info,
  5029. .get = scarlett2_phantom_ctl_get,
  5030. .put = scarlett2_phantom_ctl_put,
  5031. };
  5032. /*** Phantom Persistence Control ***/
  5033. static int scarlett2_phantom_persistence_ctl_get(
  5034. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  5035. {
  5036. struct usb_mixer_elem_info *elem = kctl->private_data;
  5037. struct scarlett2_data *private = elem->head.mixer->private_data;
  5038. ucontrol->value.integer.value[0] = private->phantom_persistence;
  5039. return 0;
  5040. }
  5041. static int scarlett2_phantom_persistence_ctl_put(
  5042. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  5043. {
  5044. struct usb_mixer_elem_info *elem = kctl->private_data;
  5045. struct usb_mixer_interface *mixer = elem->head.mixer;
  5046. struct scarlett2_data *private = mixer->private_data;
  5047. int index = elem->control;
  5048. int oval, val, err = 0;
  5049. mutex_lock(&private->data_mutex);
  5050. if (private->hwdep_in_use) {
  5051. err = -EBUSY;
  5052. goto unlock;
  5053. }
  5054. oval = private->phantom_persistence;
  5055. val = !!ucontrol->value.integer.value[0];
  5056. if (oval == val)
  5057. goto unlock;
  5058. private->phantom_persistence = val;
  5059. /* Send switch change to the device */
  5060. err = scarlett2_usb_set_config(
  5061. mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, index, val);
  5062. if (err == 0)
  5063. err = 1;
  5064. unlock:
  5065. mutex_unlock(&private->data_mutex);
  5066. return err;
  5067. }
  5068. static const struct snd_kcontrol_new scarlett2_phantom_persistence_ctl = {
  5069. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  5070. .name = "",
  5071. .info = snd_ctl_boolean_mono_info,
  5072. .get = scarlett2_phantom_persistence_ctl_get,
  5073. .put = scarlett2_phantom_persistence_ctl_put,
  5074. };
  5075. /*** Speaker Switching Control ***/
  5076. static int scarlett2_update_monitor_other(struct usb_mixer_interface *mixer)
  5077. {
  5078. struct scarlett2_data *private = mixer->private_data;
  5079. const struct scarlett2_device_info *info = private->info;
  5080. int err;
  5081. /* monitor_other_enable[0] enables speaker switching
  5082. * monitor_other_enable[1] enables talkback
  5083. */
  5084. u8 monitor_other_enable[2];
  5085. /* monitor_other_switch[0] activates the alternate speakers
  5086. * monitor_other_switch[1] activates talkback
  5087. */
  5088. u8 monitor_other_switch[2];
  5089. private->monitor_other_updated = 0;
  5090. /* if it doesn't do speaker switching then it also doesn't do
  5091. * talkback
  5092. */
  5093. if (!info->has_speaker_switching)
  5094. return 0;
  5095. err = scarlett2_usb_get_config(
  5096. mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
  5097. 2, monitor_other_enable);
  5098. if (err < 0)
  5099. return err;
  5100. err = scarlett2_usb_get_config(
  5101. mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
  5102. 2, monitor_other_switch);
  5103. if (err < 0)
  5104. return err;
  5105. if (!monitor_other_enable[0])
  5106. private->speaker_switching_switch = 0;
  5107. else
  5108. private->speaker_switching_switch = monitor_other_switch[0] + 1;
  5109. if (info->has_talkback) {
  5110. u16 bitmap;
  5111. int i;
  5112. if (!monitor_other_enable[1])
  5113. private->talkback_switch = 0;
  5114. else
  5115. private->talkback_switch = monitor_other_switch[1] + 1;
  5116. err = scarlett2_usb_get_config(mixer,
  5117. SCARLETT2_CONFIG_TALKBACK_MAP,
  5118. 1, &bitmap);
  5119. if (err < 0)
  5120. return err;
  5121. for (i = 0; i < private->num_mix_out; i++, bitmap >>= 1)
  5122. private->talkback_map[i] = bitmap & 1;
  5123. }
  5124. return 0;
  5125. }
  5126. static int scarlett2_speaker_switch_enum_ctl_info(
  5127. struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
  5128. {
  5129. static const char *const values[3] = {
  5130. "Off", "Main", "Alt"
  5131. };
  5132. return snd_ctl_enum_info(uinfo, 1, 3, values);
  5133. }
  5134. static int scarlett2_speaker_switch_enum_ctl_get(
  5135. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  5136. {
  5137. struct usb_mixer_elem_info *elem = kctl->private_data;
  5138. struct usb_mixer_interface *mixer = elem->head.mixer;
  5139. struct scarlett2_data *private = mixer->private_data;
  5140. int err = 0;
  5141. mutex_lock(&private->data_mutex);
  5142. if (private->hwdep_in_use) {
  5143. err = -EBUSY;
  5144. goto unlock;
  5145. }
  5146. if (private->monitor_other_updated) {
  5147. err = scarlett2_update_monitor_other(mixer);
  5148. if (err < 0)
  5149. goto unlock;
  5150. }
  5151. ucontrol->value.enumerated.item[0] = private->speaker_switching_switch;
  5152. unlock:
  5153. mutex_unlock(&private->data_mutex);
  5154. return err;
  5155. }
  5156. /* when speaker switching gets enabled, switch the main/alt speakers
  5157. * to HW volume and disable those controls
  5158. */
  5159. static int scarlett2_speaker_switch_enable(struct usb_mixer_interface *mixer)
  5160. {
  5161. struct snd_card *card = mixer->chip->card;
  5162. struct scarlett2_data *private = mixer->private_data;
  5163. int i, err;
  5164. for (i = 0; i < 4; i++) {
  5165. int index = line_out_remap(private, i);
  5166. /* switch the main/alt speakers to HW volume */
  5167. if (!private->vol_sw_hw_switch[index]) {
  5168. err = scarlett2_sw_hw_change(private->mixer, i, 1);
  5169. if (err < 0)
  5170. return err;
  5171. }
  5172. /* disable the line out SW/HW switch */
  5173. scarlett2_sw_hw_ctl_ro(private, i);
  5174. snd_ctl_notify(card,
  5175. SNDRV_CTL_EVENT_MASK_VALUE |
  5176. SNDRV_CTL_EVENT_MASK_INFO,
  5177. &private->sw_hw_ctls[i]->id);
  5178. }
  5179. /* when the next monitor-other notify comes in, update the mux
  5180. * configuration
  5181. */
  5182. private->speaker_switching_switched = 1;
  5183. return 0;
  5184. }
  5185. /* when speaker switching gets disabled, reenable the hw/sw controls
  5186. * and invalidate the routing
  5187. */
  5188. static void scarlett2_speaker_switch_disable(struct usb_mixer_interface *mixer)
  5189. {
  5190. struct snd_card *card = mixer->chip->card;
  5191. struct scarlett2_data *private = mixer->private_data;
  5192. int i;
  5193. /* enable the line out SW/HW switch */
  5194. for (i = 0; i < 4; i++) {
  5195. scarlett2_sw_hw_ctl_rw(private, i);
  5196. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
  5197. &private->sw_hw_ctls[i]->id);
  5198. }
  5199. /* when the next monitor-other notify comes in, update the mux
  5200. * configuration
  5201. */
  5202. private->speaker_switching_switched = 1;
  5203. }
  5204. static int scarlett2_speaker_switch_enum_ctl_put(
  5205. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  5206. {
  5207. struct usb_mixer_elem_info *elem = kctl->private_data;
  5208. struct usb_mixer_interface *mixer = elem->head.mixer;
  5209. struct scarlett2_data *private = mixer->private_data;
  5210. int oval, val, err = 0;
  5211. mutex_lock(&private->data_mutex);
  5212. if (private->hwdep_in_use) {
  5213. err = -EBUSY;
  5214. goto unlock;
  5215. }
  5216. oval = private->speaker_switching_switch;
  5217. val = min(ucontrol->value.enumerated.item[0], 2U);
  5218. if (oval == val)
  5219. goto unlock;
  5220. private->speaker_switching_switch = val;
  5221. /* enable/disable speaker switching */
  5222. err = scarlett2_usb_set_config(
  5223. mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
  5224. 0, !!val);
  5225. if (err < 0)
  5226. goto unlock;
  5227. /* if speaker switching is enabled, select main or alt */
  5228. err = scarlett2_usb_set_config(
  5229. mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
  5230. 0, val == 2);
  5231. if (err < 0)
  5232. goto unlock;
  5233. /* update controls if speaker switching gets enabled or disabled */
  5234. if (!oval && val)
  5235. err = scarlett2_speaker_switch_enable(mixer);
  5236. else if (oval && !val)
  5237. scarlett2_speaker_switch_disable(mixer);
  5238. if (err == 0)
  5239. err = 1;
  5240. unlock:
  5241. mutex_unlock(&private->data_mutex);
  5242. return err;
  5243. }
  5244. static const struct snd_kcontrol_new scarlett2_speaker_switch_enum_ctl = {
  5245. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  5246. .name = "",
  5247. .info = scarlett2_speaker_switch_enum_ctl_info,
  5248. .get = scarlett2_speaker_switch_enum_ctl_get,
  5249. .put = scarlett2_speaker_switch_enum_ctl_put,
  5250. };
  5251. static int scarlett2_add_speaker_switch_ctl(struct usb_mixer_interface *mixer)
  5252. {
  5253. struct scarlett2_data *private = mixer->private_data;
  5254. const struct scarlett2_device_info *info = private->info;
  5255. if (!info->has_speaker_switching)
  5256. return 0;
  5257. return scarlett2_add_new_ctl(
  5258. mixer, &scarlett2_speaker_switch_enum_ctl,
  5259. 0, 1, "Speaker Switching Playback Enum",
  5260. &private->speaker_switching_ctl);
  5261. }
  5262. /*** Talkback and Talkback Map Controls ***/
  5263. static int scarlett2_talkback_enum_ctl_info(
  5264. struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
  5265. {
  5266. static const char *const values[3] = {
  5267. "Disabled", "Off", "On"
  5268. };
  5269. return snd_ctl_enum_info(uinfo, 1, 3, values);
  5270. }
  5271. static int scarlett2_talkback_enum_ctl_get(
  5272. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  5273. {
  5274. struct usb_mixer_elem_info *elem = kctl->private_data;
  5275. struct usb_mixer_interface *mixer = elem->head.mixer;
  5276. struct scarlett2_data *private = mixer->private_data;
  5277. int err = 0;
  5278. mutex_lock(&private->data_mutex);
  5279. if (private->hwdep_in_use) {
  5280. err = -EBUSY;
  5281. goto unlock;
  5282. }
  5283. if (private->monitor_other_updated) {
  5284. err = scarlett2_update_monitor_other(mixer);
  5285. if (err < 0)
  5286. goto unlock;
  5287. }
  5288. ucontrol->value.enumerated.item[0] = private->talkback_switch;
  5289. unlock:
  5290. mutex_unlock(&private->data_mutex);
  5291. return err;
  5292. }
  5293. static int scarlett2_talkback_enum_ctl_put(
  5294. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  5295. {
  5296. struct usb_mixer_elem_info *elem = kctl->private_data;
  5297. struct usb_mixer_interface *mixer = elem->head.mixer;
  5298. struct scarlett2_data *private = mixer->private_data;
  5299. int oval, val, err = 0;
  5300. mutex_lock(&private->data_mutex);
  5301. if (private->hwdep_in_use) {
  5302. err = -EBUSY;
  5303. goto unlock;
  5304. }
  5305. oval = private->talkback_switch;
  5306. val = min(ucontrol->value.enumerated.item[0], 2U);
  5307. if (oval == val)
  5308. goto unlock;
  5309. private->talkback_switch = val;
  5310. /* enable/disable talkback */
  5311. err = scarlett2_usb_set_config(
  5312. mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
  5313. 1, !!val);
  5314. if (err < 0)
  5315. goto unlock;
  5316. /* if talkback is enabled, select main or alt */
  5317. err = scarlett2_usb_set_config(
  5318. mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
  5319. 1, val == 2);
  5320. if (err == 0)
  5321. err = 1;
  5322. unlock:
  5323. mutex_unlock(&private->data_mutex);
  5324. return err;
  5325. }
  5326. static const struct snd_kcontrol_new scarlett2_talkback_enum_ctl = {
  5327. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  5328. .name = "",
  5329. .info = scarlett2_talkback_enum_ctl_info,
  5330. .get = scarlett2_talkback_enum_ctl_get,
  5331. .put = scarlett2_talkback_enum_ctl_put,
  5332. };
  5333. static int scarlett2_talkback_map_ctl_get(
  5334. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  5335. {
  5336. struct usb_mixer_elem_info *elem = kctl->private_data;
  5337. struct usb_mixer_interface *mixer = elem->head.mixer;
  5338. struct scarlett2_data *private = mixer->private_data;
  5339. int index = elem->control;
  5340. ucontrol->value.integer.value[0] = private->talkback_map[index];
  5341. return 0;
  5342. }
  5343. static int scarlett2_talkback_map_ctl_put(
  5344. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  5345. {
  5346. struct usb_mixer_elem_info *elem = kctl->private_data;
  5347. struct usb_mixer_interface *mixer = elem->head.mixer;
  5348. struct scarlett2_data *private = mixer->private_data;
  5349. int index = elem->control;
  5350. int oval, val, err = 0, i;
  5351. u16 bitmap = 0;
  5352. mutex_lock(&private->data_mutex);
  5353. if (private->hwdep_in_use) {
  5354. err = -EBUSY;
  5355. goto unlock;
  5356. }
  5357. oval = private->talkback_map[index];
  5358. val = !!ucontrol->value.integer.value[0];
  5359. if (oval == val)
  5360. goto unlock;
  5361. private->talkback_map[index] = val;
  5362. for (i = 0; i < private->num_mix_out; i++)
  5363. bitmap |= private->talkback_map[i] << i;
  5364. /* Send updated bitmap to the device */
  5365. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_TALKBACK_MAP,
  5366. 0, bitmap);
  5367. if (err == 0)
  5368. err = 1;
  5369. unlock:
  5370. mutex_unlock(&private->data_mutex);
  5371. return err;
  5372. }
  5373. static const struct snd_kcontrol_new scarlett2_talkback_map_ctl = {
  5374. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  5375. .name = "",
  5376. .info = snd_ctl_boolean_mono_info,
  5377. .get = scarlett2_talkback_map_ctl_get,
  5378. .put = scarlett2_talkback_map_ctl_put,
  5379. };
  5380. static int scarlett2_add_talkback_ctls(struct usb_mixer_interface *mixer)
  5381. {
  5382. struct scarlett2_data *private = mixer->private_data;
  5383. const struct scarlett2_device_info *info = private->info;
  5384. int err, i;
  5385. char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  5386. if (!info->has_talkback)
  5387. return 0;
  5388. err = scarlett2_add_new_ctl(
  5389. mixer, &scarlett2_talkback_enum_ctl,
  5390. 0, 1, "Talkback Playback Enum",
  5391. &private->talkback_ctl);
  5392. if (err < 0)
  5393. return err;
  5394. for (i = 0; i < private->num_mix_out; i++) {
  5395. snprintf(s, sizeof(s),
  5396. "Talkback Mix %c Playback Switch", i + 'A');
  5397. err = scarlett2_add_new_ctl(mixer, &scarlett2_talkback_map_ctl,
  5398. i, 1, s, NULL);
  5399. if (err < 0)
  5400. return err;
  5401. }
  5402. return 0;
  5403. }
  5404. /*** Dim/Mute Controls ***/
  5405. static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
  5406. struct snd_ctl_elem_value *ucontrol)
  5407. {
  5408. struct usb_mixer_elem_info *elem = kctl->private_data;
  5409. struct usb_mixer_interface *mixer = elem->head.mixer;
  5410. struct scarlett2_data *private = mixer->private_data;
  5411. int err = 0;
  5412. mutex_lock(&private->data_mutex);
  5413. if (private->hwdep_in_use) {
  5414. err = -EBUSY;
  5415. goto unlock;
  5416. }
  5417. if (private->dim_mute_updated) {
  5418. err = scarlett2_update_dim_mute(mixer);
  5419. if (err < 0)
  5420. goto unlock;
  5421. }
  5422. ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
  5423. unlock:
  5424. mutex_unlock(&private->data_mutex);
  5425. return err;
  5426. }
  5427. static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
  5428. struct snd_ctl_elem_value *ucontrol)
  5429. {
  5430. struct usb_mixer_elem_info *elem = kctl->private_data;
  5431. struct usb_mixer_interface *mixer = elem->head.mixer;
  5432. struct scarlett2_data *private = mixer->private_data;
  5433. int index = elem->control;
  5434. int oval, val, err = 0, i;
  5435. mutex_lock(&private->data_mutex);
  5436. if (private->hwdep_in_use) {
  5437. err = -EBUSY;
  5438. goto unlock;
  5439. }
  5440. oval = private->dim_mute[index];
  5441. val = !!ucontrol->value.integer.value[0];
  5442. if (oval == val)
  5443. goto unlock;
  5444. private->dim_mute[index] = val;
  5445. /* Send switch change to the device */
  5446. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
  5447. index, val);
  5448. if (err == 0)
  5449. err = 1;
  5450. if (index == SCARLETT2_BUTTON_MUTE)
  5451. for (i = 0; i < private->num_line_out; i++) {
  5452. int line_index = line_out_remap(private, i);
  5453. if (private->vol_sw_hw_switch[line_index]) {
  5454. private->mute_switch[line_index] = val;
  5455. snd_ctl_notify(mixer->chip->card,
  5456. SNDRV_CTL_EVENT_MASK_VALUE,
  5457. &private->mute_ctls[i]->id);
  5458. }
  5459. }
  5460. unlock:
  5461. mutex_unlock(&private->data_mutex);
  5462. return err;
  5463. }
  5464. static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
  5465. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  5466. .name = "",
  5467. .info = snd_ctl_boolean_mono_info,
  5468. .get = scarlett2_dim_mute_ctl_get,
  5469. .put = scarlett2_dim_mute_ctl_put
  5470. };
  5471. /*** Create the analogue output controls ***/
  5472. static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
  5473. {
  5474. struct scarlett2_data *private = mixer->private_data;
  5475. const struct scarlett2_device_info *info = private->info;
  5476. int err, i;
  5477. char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  5478. /* Add R/O HW volume control */
  5479. if (scarlett2_has_config_item(private,
  5480. SCARLETT2_CONFIG_MASTER_VOLUME)) {
  5481. snprintf(s, sizeof(s), "Master HW Playback Volume");
  5482. err = scarlett2_add_new_ctl(mixer,
  5483. &scarlett2_master_volume_ctl,
  5484. 0, 1, s, &private->master_vol_ctl);
  5485. if (err < 0)
  5486. return err;
  5487. }
  5488. /* Add R/O headphone volume control */
  5489. if (scarlett2_has_config_item(private,
  5490. SCARLETT2_CONFIG_HEADPHONE_VOLUME)) {
  5491. snprintf(s, sizeof(s), "Headphone Playback Volume");
  5492. err = scarlett2_add_new_ctl(mixer,
  5493. &scarlett2_headphone_volume_ctl,
  5494. 0, 1, s,
  5495. &private->headphone_vol_ctl);
  5496. if (err < 0)
  5497. return err;
  5498. }
  5499. /* Remaining controls are only applicable if the device
  5500. * has per-channel line-out volume controls.
  5501. */
  5502. if (!scarlett2_has_config_item(private,
  5503. SCARLETT2_CONFIG_LINE_OUT_VOLUME))
  5504. return 0;
  5505. /* Add volume controls */
  5506. for (i = 0; i < private->num_line_out; i++) {
  5507. int index = line_out_remap(private, i);
  5508. /* Fader */
  5509. if (info->line_out_descrs[i])
  5510. snprintf(s, sizeof(s),
  5511. "Line %02d (%s) Playback Volume",
  5512. i + 1, info->line_out_descrs[i]);
  5513. else
  5514. snprintf(s, sizeof(s),
  5515. "Line %02d Playback Volume",
  5516. i + 1);
  5517. err = scarlett2_add_new_ctl(mixer,
  5518. &scarlett2_line_out_volume_ctl,
  5519. i, 1, s, &private->vol_ctls[i]);
  5520. if (err < 0)
  5521. return err;
  5522. /* Mute Switch */
  5523. snprintf(s, sizeof(s),
  5524. "Line %02d Mute Playback Switch",
  5525. i + 1);
  5526. err = scarlett2_add_new_ctl(mixer,
  5527. &scarlett2_mute_ctl,
  5528. i, 1, s,
  5529. &private->mute_ctls[i]);
  5530. if (err < 0)
  5531. return err;
  5532. /* SW/HW Switch */
  5533. if (scarlett2_has_config_item(private,
  5534. SCARLETT2_CONFIG_SW_HW_SWITCH)) {
  5535. /* Make the fader and mute controls read-only if the
  5536. * SW/HW switch is set to HW
  5537. */
  5538. if (private->vol_sw_hw_switch[index])
  5539. scarlett2_vol_ctl_set_writable(mixer, i, 0);
  5540. scnprintf(s, sizeof(s),
  5541. "Line Out %02d Volume Control Playback Enum",
  5542. i + 1);
  5543. err = scarlett2_add_new_ctl(mixer,
  5544. &scarlett2_sw_hw_enum_ctl,
  5545. i, 1, s,
  5546. &private->sw_hw_ctls[i]);
  5547. if (err < 0)
  5548. return err;
  5549. /* Make the switch read-only if the line is
  5550. * involved in speaker switching
  5551. */
  5552. if (private->speaker_switching_switch && i < 4)
  5553. scarlett2_sw_hw_ctl_ro(private, i);
  5554. }
  5555. }
  5556. /* Add dim/mute controls */
  5557. if (scarlett2_has_config_item(private, SCARLETT2_CONFIG_DIM_MUTE))
  5558. for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
  5559. err = scarlett2_add_new_ctl(
  5560. mixer, &scarlett2_dim_mute_ctl,
  5561. i, 1, scarlett2_dim_mute_names[i],
  5562. &private->dim_mute_ctls[i]);
  5563. if (err < 0)
  5564. return err;
  5565. }
  5566. return 0;
  5567. }
  5568. /*** Create the analogue input controls ***/
  5569. static int scarlett2_add_dsp_ctls(struct usb_mixer_interface *mixer, int i)
  5570. {
  5571. struct scarlett2_data *private = mixer->private_data;
  5572. const struct scarlett2_device_info *info = private->info;
  5573. int j, err;
  5574. char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  5575. const char *compr_fmt = "Line In %d Compressor %s";
  5576. const char *flt_switch_fmt = "Line In %d %s Filter Enable";
  5577. const char *flt_fmt = "Line In %d %s Coefficients %d";
  5578. /* Add compressor controls */
  5579. for (j = 0; j < SCARLETT2_COMPRESSOR_PARAM_COUNT; j++) {
  5580. const struct compressor_param *param = &compressor_params[j];
  5581. int idx = i * SCARLETT2_COMPRESSOR_PARAM_COUNT + j;
  5582. scnprintf(s, sizeof(s), compr_fmt, i + 1, param->name);
  5583. err = scarlett2_add_new_ctl(
  5584. mixer, &scarlett2_compressor_ctl,
  5585. i * SCARLETT2_COMPRESSOR_PARAM_COUNT + j,
  5586. 1, s, &private->compressor_ctls[idx]);
  5587. if (err < 0)
  5588. return err;
  5589. }
  5590. /* Add filter enable controls */
  5591. scnprintf(s, sizeof(s), flt_switch_fmt, i + 1, "Pre-Comp");
  5592. err = scarlett2_add_new_ctl(
  5593. mixer, &scarlett2_precomp_flt_switch_ctl,
  5594. i, 1, s, &private->precomp_flt_switch_ctls[i]);
  5595. if (err < 0)
  5596. return err;
  5597. scnprintf(s, sizeof(s), flt_switch_fmt, i + 1, "PEQ");
  5598. err = scarlett2_add_new_ctl(
  5599. mixer, &scarlett2_peq_flt_switch_ctl,
  5600. i, 1, s, &private->peq_flt_switch_ctls[i]);
  5601. if (err < 0)
  5602. return err;
  5603. /* Add filter coefficient controls */
  5604. for (j = 0; j < info->precomp_flt_count; j++) {
  5605. scnprintf(s, sizeof(s), flt_fmt, i + 1, "Pre-Comp", j + 1);
  5606. err = scarlett2_add_new_ctl(
  5607. mixer, &scarlett2_precomp_flt_ctl,
  5608. i * info->precomp_flt_count + j,
  5609. 1, s, &private->precomp_flt_switch_ctls[j]);
  5610. if (err < 0)
  5611. return err;
  5612. }
  5613. for (j = 0; j < info->peq_flt_count; j++) {
  5614. scnprintf(s, sizeof(s), flt_fmt, i + 1, "PEQ", j + 1);
  5615. err = scarlett2_add_new_ctl(
  5616. mixer, &scarlett2_peq_flt_ctl,
  5617. i * info->peq_flt_count + j,
  5618. 1, s, &private->peq_flt_switch_ctls[j]);
  5619. if (err < 0)
  5620. return err;
  5621. }
  5622. return 0;
  5623. }
  5624. static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
  5625. {
  5626. struct scarlett2_data *private = mixer->private_data;
  5627. const struct scarlett2_device_info *info = private->info;
  5628. int err, i;
  5629. char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  5630. const char *fmt = "Line In %d %s Capture %s";
  5631. const char *fmt2 = "Line In %d-%d %s Capture %s";
  5632. /* Add input level (line/inst) controls */
  5633. for (i = 0; i < info->level_input_count; i++) {
  5634. scnprintf(s, sizeof(s), fmt, i + 1 + info->level_input_first,
  5635. "Level", "Enum");
  5636. err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
  5637. i, 1, s, &private->level_ctls[i]);
  5638. if (err < 0)
  5639. return err;
  5640. }
  5641. /* Add input pad controls */
  5642. for (i = 0; i < info->pad_input_count; i++) {
  5643. scnprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
  5644. err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
  5645. i, 1, s, &private->pad_ctls[i]);
  5646. if (err < 0)
  5647. return err;
  5648. }
  5649. /* Add input air controls */
  5650. for (i = 0; i < info->air_input_count; i++) {
  5651. scnprintf(s, sizeof(s), fmt, i + 1 + info->air_input_first,
  5652. "Air", info->air_option ? "Enum" : "Switch");
  5653. err = scarlett2_add_new_ctl(
  5654. mixer, &scarlett2_air_ctl[info->air_option],
  5655. i, 1, s, &private->air_ctls[i]);
  5656. if (err < 0)
  5657. return err;
  5658. }
  5659. /* Add input DSP controls */
  5660. for (i = 0; i < info->dsp_input_count; i++) {
  5661. scnprintf(s, sizeof(s), fmt, i + 1, "DSP", "Switch");
  5662. err = scarlett2_add_new_ctl(mixer, &scarlett2_dsp_ctl,
  5663. i, 1, s, &private->dsp_ctls[i]);
  5664. if (err < 0)
  5665. return err;
  5666. err = scarlett2_add_dsp_ctls(mixer, i);
  5667. if (err < 0)
  5668. return err;
  5669. }
  5670. /* Add input mute controls */
  5671. for (i = 0; i < info->mute_input_count; i++) {
  5672. scnprintf(s, sizeof(s), fmt, i + 1, "Mute", "Switch");
  5673. err = scarlett2_add_new_ctl(
  5674. mixer, &scarlett2_input_mute_ctl,
  5675. i, 1, s, &private->input_mute_ctls[i]);
  5676. if (err < 0)
  5677. return err;
  5678. }
  5679. /* Add input phantom controls */
  5680. if (info->inputs_per_phantom == 1) {
  5681. for (i = 0; i < info->phantom_count; i++) {
  5682. scnprintf(s, sizeof(s), fmt,
  5683. i + 1 + info->phantom_first,
  5684. "Phantom Power", "Switch");
  5685. err = scarlett2_add_new_ctl(
  5686. mixer, &scarlett2_phantom_ctl,
  5687. i, 1, s, &private->phantom_ctls[i]);
  5688. if (err < 0)
  5689. return err;
  5690. }
  5691. } else if (info->inputs_per_phantom > 1) {
  5692. for (i = 0; i < info->phantom_count; i++) {
  5693. int from = i * info->inputs_per_phantom + 1;
  5694. int to = (i + 1) * info->inputs_per_phantom;
  5695. scnprintf(s, sizeof(s), fmt2, from, to,
  5696. "Phantom Power", "Switch");
  5697. err = scarlett2_add_new_ctl(
  5698. mixer, &scarlett2_phantom_ctl,
  5699. i, 1, s, &private->phantom_ctls[i]);
  5700. if (err < 0)
  5701. return err;
  5702. }
  5703. }
  5704. if (info->phantom_count &&
  5705. scarlett2_has_config_item(private,
  5706. SCARLETT2_CONFIG_PHANTOM_PERSISTENCE)) {
  5707. err = scarlett2_add_new_ctl(
  5708. mixer, &scarlett2_phantom_persistence_ctl, 0, 1,
  5709. "Phantom Power Persistence Capture Switch", NULL);
  5710. if (err < 0)
  5711. return err;
  5712. }
  5713. /* Add input select/link controls */
  5714. if (scarlett2_has_config_item(private,
  5715. SCARLETT2_CONFIG_INPUT_SELECT_SWITCH)) {
  5716. err = scarlett2_add_new_ctl(
  5717. mixer, &scarlett2_input_select_ctl, 0, 1,
  5718. "Input Select Capture Enum",
  5719. &private->input_select_ctl);
  5720. if (err < 0)
  5721. return err;
  5722. }
  5723. if (scarlett2_has_config_item(private,
  5724. SCARLETT2_CONFIG_INPUT_LINK_SWITCH)) {
  5725. for (i = 0; i < info->gain_input_count / 2; i++) {
  5726. scnprintf(s, sizeof(s),
  5727. "Line In %d-%d Link Capture Switch",
  5728. (i * 2) + 1, (i * 2) + 2);
  5729. err = scarlett2_add_new_ctl(
  5730. mixer, &scarlett2_input_link_ctl,
  5731. i, 1, s, &private->input_link_ctls[i]);
  5732. if (err < 0)
  5733. return err;
  5734. }
  5735. }
  5736. /* Add software-controllable input gain controls */
  5737. for (i = 0; i < info->gain_input_count; i++) {
  5738. scnprintf(s, sizeof(s), fmt, i + 1,
  5739. "Gain", "Volume");
  5740. err = scarlett2_add_new_ctl(
  5741. mixer, &scarlett2_input_gain_ctl,
  5742. i, 1, s, &private->input_gain_ctls[i]);
  5743. if (err < 0)
  5744. return err;
  5745. private->input_gain_ctls[i]->tlv.p =
  5746. private->config_set->input_gain_tlv;
  5747. scnprintf(s, sizeof(s), fmt, i + 1,
  5748. "Autogain", "Switch");
  5749. err = scarlett2_add_new_ctl(
  5750. mixer, &scarlett2_autogain_switch_ctl,
  5751. i, 1, s, &private->autogain_ctls[i]);
  5752. if (err < 0)
  5753. return err;
  5754. scnprintf(s, sizeof(s), fmt, i + 1,
  5755. "Autogain Status", "Enum");
  5756. err = scarlett2_add_new_ctl(
  5757. mixer, &scarlett2_autogain_status_ctl,
  5758. i, 1, s, &private->autogain_status_ctls[i]);
  5759. }
  5760. /* Add autogain target controls */
  5761. for (i = 0; i < SCARLETT2_AG_TARGET_COUNT; i++)
  5762. if (scarlett2_has_config_item(private,
  5763. scarlett2_ag_target_configs[i])) {
  5764. scnprintf(s, sizeof(s), "Autogain %s Target",
  5765. scarlett2_ag_target_names[i]);
  5766. err = scarlett2_add_new_ctl(
  5767. mixer, &scarlett2_ag_target_ctl,
  5768. i, 1, s, &private->ag_target_ctls[i]);
  5769. if (err < 0)
  5770. return err;
  5771. }
  5772. /* Add safe-mode input switch controls */
  5773. for (i = 0; i < info->safe_input_count; i++) {
  5774. scnprintf(s, sizeof(s), fmt, i + 1,
  5775. "Safe", "Switch");
  5776. err = scarlett2_add_new_ctl(
  5777. mixer, &scarlett2_safe_ctl,
  5778. i, 1, s, &private->safe_ctls[i]);
  5779. if (err < 0)
  5780. return err;
  5781. }
  5782. /* Add PCM Input Switch control */
  5783. if (scarlett2_has_config_item(private,
  5784. SCARLETT2_CONFIG_PCM_INPUT_SWITCH)) {
  5785. err = scarlett2_add_new_ctl(
  5786. mixer, &scarlett2_pcm_input_switch_ctl, 0, 1,
  5787. "PCM Input Capture Switch",
  5788. &private->pcm_input_switch_ctl);
  5789. if (err < 0)
  5790. return err;
  5791. }
  5792. return 0;
  5793. }
  5794. /*** Mixer Volume Controls ***/
  5795. static int scarlett2_update_mix(struct usb_mixer_interface *mixer)
  5796. {
  5797. struct scarlett2_data *private = mixer->private_data;
  5798. int i, err;
  5799. private->mix_updated = 0;
  5800. for (i = 0; i < private->num_mix_out; i++) {
  5801. err = scarlett2_usb_get_mix(mixer, i);
  5802. if (err < 0)
  5803. return err;
  5804. }
  5805. return 1;
  5806. }
  5807. static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
  5808. struct snd_ctl_elem_info *uinfo)
  5809. {
  5810. struct usb_mixer_elem_info *elem = kctl->private_data;
  5811. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  5812. uinfo->count = elem->channels;
  5813. uinfo->value.integer.min = 0;
  5814. uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
  5815. uinfo->value.integer.step = 1;
  5816. return 0;
  5817. }
  5818. static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
  5819. struct snd_ctl_elem_value *ucontrol)
  5820. {
  5821. struct usb_mixer_elem_info *elem = kctl->private_data;
  5822. struct usb_mixer_interface *mixer = elem->head.mixer;
  5823. struct scarlett2_data *private = mixer->private_data;
  5824. int err = 0;
  5825. mutex_lock(&private->data_mutex);
  5826. if (private->hwdep_in_use) {
  5827. err = -EBUSY;
  5828. goto unlock;
  5829. }
  5830. if (private->mix_updated) {
  5831. err = scarlett2_update_mix(mixer);
  5832. if (err < 0)
  5833. goto unlock;
  5834. }
  5835. ucontrol->value.integer.value[0] = private->mix[elem->control];
  5836. unlock:
  5837. mutex_unlock(&private->data_mutex);
  5838. return err;
  5839. }
  5840. static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
  5841. struct snd_ctl_elem_value *ucontrol)
  5842. {
  5843. struct usb_mixer_elem_info *elem = kctl->private_data;
  5844. struct usb_mixer_interface *mixer = elem->head.mixer;
  5845. struct scarlett2_data *private = mixer->private_data;
  5846. int oval, val, mix_num, err = 0;
  5847. int index = elem->control;
  5848. mutex_lock(&private->data_mutex);
  5849. if (private->hwdep_in_use) {
  5850. err = -EBUSY;
  5851. goto unlock;
  5852. }
  5853. oval = private->mix[index];
  5854. val = clamp(ucontrol->value.integer.value[0],
  5855. 0L, (long)SCARLETT2_MIXER_MAX_VALUE);
  5856. mix_num = index / private->num_mix_in;
  5857. if (oval == val)
  5858. goto unlock;
  5859. private->mix[index] = val;
  5860. err = scarlett2_usb_set_mix(mixer, mix_num);
  5861. if (err == 0)
  5862. err = 1;
  5863. unlock:
  5864. mutex_unlock(&private->data_mutex);
  5865. return err;
  5866. }
  5867. static const DECLARE_TLV_DB_MINMAX(
  5868. db_scale_scarlett2_mixer,
  5869. SCARLETT2_MIXER_MIN_DB * 100,
  5870. SCARLETT2_MIXER_MAX_DB * 100
  5871. );
  5872. static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
  5873. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  5874. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  5875. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  5876. .name = "",
  5877. .info = scarlett2_mixer_ctl_info,
  5878. .get = scarlett2_mixer_ctl_get,
  5879. .put = scarlett2_mixer_ctl_put,
  5880. .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
  5881. .tlv = { .p = db_scale_scarlett2_mixer }
  5882. };
  5883. static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
  5884. {
  5885. struct scarlett2_data *private = mixer->private_data;
  5886. int err, i, j;
  5887. int index;
  5888. char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  5889. for (i = 0, index = 0; i < private->num_mix_out; i++)
  5890. for (j = 0; j < private->num_mix_in; j++, index++) {
  5891. snprintf(s, sizeof(s),
  5892. "Mix %c Input %02d Playback Volume",
  5893. 'A' + i, j + 1);
  5894. err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
  5895. index, 1, s,
  5896. &private->mix_ctls[index]);
  5897. if (err < 0)
  5898. return err;
  5899. }
  5900. return 0;
  5901. }
  5902. /*** Direct Monitor Control ***/
  5903. static int scarlett2_update_direct_monitor(struct usb_mixer_interface *mixer)
  5904. {
  5905. struct scarlett2_data *private = mixer->private_data;
  5906. private->direct_monitor_updated = 0;
  5907. if (!private->info->direct_monitor)
  5908. return 0;
  5909. return scarlett2_usb_get_config(
  5910. mixer, SCARLETT2_CONFIG_DIRECT_MONITOR,
  5911. 1, &private->direct_monitor_switch);
  5912. }
  5913. static int scarlett2_update_monitor_mix(struct usb_mixer_interface *mixer)
  5914. {
  5915. struct scarlett2_data *private = mixer->private_data;
  5916. int err, i;
  5917. u16 mix_values[SCARLETT2_MONITOR_MIX_MAX];
  5918. if (!private->num_monitor_mix_ctls)
  5919. return 0;
  5920. err = scarlett2_usb_get_config(
  5921. mixer, SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN,
  5922. private->num_monitor_mix_ctls, mix_values);
  5923. if (err < 0)
  5924. return err;
  5925. for (i = 0; i < private->num_monitor_mix_ctls; i++)
  5926. private->monitor_mix[i] = scarlett2_mixer_value_to_db(
  5927. mix_values[i]);
  5928. return 0;
  5929. }
  5930. static int scarlett2_direct_monitor_ctl_get(
  5931. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  5932. {
  5933. struct usb_mixer_elem_info *elem = kctl->private_data;
  5934. struct usb_mixer_interface *mixer = elem->head.mixer;
  5935. struct scarlett2_data *private = mixer->private_data;
  5936. int err = 0;
  5937. mutex_lock(&private->data_mutex);
  5938. if (private->hwdep_in_use) {
  5939. err = -EBUSY;
  5940. goto unlock;
  5941. }
  5942. if (private->direct_monitor_updated) {
  5943. err = scarlett2_update_direct_monitor(mixer);
  5944. if (err < 0)
  5945. goto unlock;
  5946. }
  5947. ucontrol->value.enumerated.item[0] = private->direct_monitor_switch;
  5948. unlock:
  5949. mutex_unlock(&private->data_mutex);
  5950. return err;
  5951. }
  5952. static int scarlett2_direct_monitor_ctl_put(
  5953. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  5954. {
  5955. struct usb_mixer_elem_info *elem = kctl->private_data;
  5956. struct usb_mixer_interface *mixer = elem->head.mixer;
  5957. struct scarlett2_data *private = mixer->private_data;
  5958. int index = elem->control;
  5959. int oval, val, err = 0;
  5960. mutex_lock(&private->data_mutex);
  5961. if (private->hwdep_in_use) {
  5962. err = -EBUSY;
  5963. goto unlock;
  5964. }
  5965. oval = private->direct_monitor_switch;
  5966. val = min(ucontrol->value.enumerated.item[0], 2U);
  5967. if (oval == val)
  5968. goto unlock;
  5969. private->direct_monitor_switch = val;
  5970. /* Send switch change to the device */
  5971. err = scarlett2_usb_set_config(
  5972. mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, index, val);
  5973. if (err == 0)
  5974. err = 1;
  5975. unlock:
  5976. mutex_unlock(&private->data_mutex);
  5977. return err;
  5978. }
  5979. static int scarlett2_direct_monitor_stereo_enum_ctl_info(
  5980. struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
  5981. {
  5982. static const char *const values[3] = {
  5983. "Off", "Mono", "Stereo"
  5984. };
  5985. return snd_ctl_enum_info(uinfo, 1, 3, values);
  5986. }
  5987. /* Direct Monitor for Solo is mono-only and only needs a boolean control
  5988. * Direct Monitor for 2i2 is selectable between Off/Mono/Stereo
  5989. */
  5990. static const struct snd_kcontrol_new scarlett2_direct_monitor_ctl[2] = {
  5991. {
  5992. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  5993. .name = "",
  5994. .info = snd_ctl_boolean_mono_info,
  5995. .get = scarlett2_direct_monitor_ctl_get,
  5996. .put = scarlett2_direct_monitor_ctl_put,
  5997. },
  5998. {
  5999. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  6000. .name = "",
  6001. .info = scarlett2_direct_monitor_stereo_enum_ctl_info,
  6002. .get = scarlett2_direct_monitor_ctl_get,
  6003. .put = scarlett2_direct_monitor_ctl_put,
  6004. }
  6005. };
  6006. static int scarlett2_monitor_mix_ctl_get(struct snd_kcontrol *kctl,
  6007. struct snd_ctl_elem_value *ucontrol)
  6008. {
  6009. struct usb_mixer_elem_info *elem = kctl->private_data;
  6010. struct scarlett2_data *private = elem->head.mixer->private_data;
  6011. ucontrol->value.integer.value[0] = private->monitor_mix[elem->control];
  6012. return 0;
  6013. }
  6014. static int scarlett2_monitor_mix_ctl_put(struct snd_kcontrol *kctl,
  6015. struct snd_ctl_elem_value *ucontrol)
  6016. {
  6017. struct usb_mixer_elem_info *elem = kctl->private_data;
  6018. struct usb_mixer_interface *mixer = elem->head.mixer;
  6019. struct scarlett2_data *private = mixer->private_data;
  6020. int oval, val, err = 0;
  6021. int index = elem->control;
  6022. mutex_lock(&private->data_mutex);
  6023. if (private->hwdep_in_use) {
  6024. err = -EBUSY;
  6025. goto unlock;
  6026. }
  6027. oval = private->monitor_mix[index];
  6028. val = clamp(ucontrol->value.integer.value[0],
  6029. 0L, (long)SCARLETT2_MIXER_MAX_VALUE);
  6030. if (oval == val)
  6031. goto unlock;
  6032. private->monitor_mix[index] = val;
  6033. err = scarlett2_usb_set_config(
  6034. mixer, SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN,
  6035. index, scarlett2_mixer_values[val]);
  6036. if (err == 0)
  6037. err = 1;
  6038. unlock:
  6039. mutex_unlock(&private->data_mutex);
  6040. return err;
  6041. }
  6042. static const struct snd_kcontrol_new scarlett2_monitor_mix_ctl = {
  6043. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  6044. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  6045. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  6046. .name = "",
  6047. .info = scarlett2_mixer_ctl_info,
  6048. .get = scarlett2_monitor_mix_ctl_get,
  6049. .put = scarlett2_monitor_mix_ctl_put,
  6050. .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
  6051. .tlv = { .p = db_scale_scarlett2_mixer }
  6052. };
  6053. static int scarlett2_add_direct_monitor_ctls(struct usb_mixer_interface *mixer)
  6054. {
  6055. struct scarlett2_data *private = mixer->private_data;
  6056. const struct scarlett2_device_info *info = private->info;
  6057. const char *s;
  6058. int err, i, j, k, index;
  6059. if (!info->direct_monitor)
  6060. return 0;
  6061. s = info->direct_monitor == 1
  6062. ? "Direct Monitor Playback Switch"
  6063. : "Direct Monitor Playback Enum";
  6064. err = scarlett2_add_new_ctl(
  6065. mixer, &scarlett2_direct_monitor_ctl[info->direct_monitor - 1],
  6066. 0, 1, s, &private->direct_monitor_ctl);
  6067. if (err < 0)
  6068. return err;
  6069. if (!private->num_monitor_mix_ctls)
  6070. return 0;
  6071. /* 1 or 2 direct monitor selections (Mono & Stereo) */
  6072. for (i = 0, index = 0; i < info->direct_monitor; i++) {
  6073. const char * const format =
  6074. "Monitor %sMix %c Input %02d Playback Volume";
  6075. const char *mix_type;
  6076. if (info->direct_monitor == 1)
  6077. mix_type = "";
  6078. else if (i == 0)
  6079. mix_type = "1 ";
  6080. else
  6081. mix_type = "2 ";
  6082. /* 2 Mix outputs, A/Left & B/Right */
  6083. for (j = 0; j < 2; j++)
  6084. /* Mix inputs */
  6085. for (k = 0; k < private->num_mix_in; k++, index++) {
  6086. char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  6087. scnprintf(name, sizeof(name), format,
  6088. mix_type, 'A' + j, k + 1);
  6089. err = scarlett2_add_new_ctl(
  6090. mixer, &scarlett2_monitor_mix_ctl,
  6091. index, 1, name, NULL);
  6092. if (err < 0)
  6093. return err;
  6094. }
  6095. }
  6096. return 0;
  6097. }
  6098. /*** Mux Source Selection Controls ***/
  6099. static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
  6100. struct snd_ctl_elem_info *uinfo)
  6101. {
  6102. struct usb_mixer_elem_info *elem = kctl->private_data;
  6103. struct scarlett2_data *private = elem->head.mixer->private_data;
  6104. const struct scarlett2_device_info *info = private->info;
  6105. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  6106. unsigned int item = uinfo->value.enumerated.item;
  6107. int items = private->num_mux_srcs;
  6108. int port_type;
  6109. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  6110. uinfo->count = elem->channels;
  6111. uinfo->value.enumerated.items = items;
  6112. if (item >= items)
  6113. item = uinfo->value.enumerated.item = items - 1;
  6114. for (port_type = 0;
  6115. port_type < SCARLETT2_PORT_TYPE_COUNT;
  6116. port_type++) {
  6117. if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
  6118. const struct scarlett2_port *port =
  6119. &scarlett2_ports[port_type];
  6120. if (port_type == SCARLETT2_PORT_TYPE_MIX &&
  6121. item >= private->num_mix_out)
  6122. sprintf(uinfo->value.enumerated.name,
  6123. port->dsp_src_descr,
  6124. item - private->num_mix_out + 1);
  6125. else
  6126. sprintf(uinfo->value.enumerated.name,
  6127. port->src_descr,
  6128. item + port->src_num_offset);
  6129. return 0;
  6130. }
  6131. item -= port_count[port_type][SCARLETT2_PORT_IN];
  6132. }
  6133. return -EINVAL;
  6134. }
  6135. static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
  6136. struct snd_ctl_elem_value *ucontrol)
  6137. {
  6138. struct usb_mixer_elem_info *elem = kctl->private_data;
  6139. struct usb_mixer_interface *mixer = elem->head.mixer;
  6140. struct scarlett2_data *private = mixer->private_data;
  6141. int index = line_out_remap(private, elem->control);
  6142. int err = 0;
  6143. mutex_lock(&private->data_mutex);
  6144. if (private->hwdep_in_use) {
  6145. err = -EBUSY;
  6146. goto unlock;
  6147. }
  6148. if (private->mux_updated) {
  6149. err = scarlett2_usb_get_mux(mixer);
  6150. if (err < 0)
  6151. goto unlock;
  6152. }
  6153. ucontrol->value.enumerated.item[0] = private->mux[index];
  6154. unlock:
  6155. mutex_unlock(&private->data_mutex);
  6156. return err;
  6157. }
  6158. static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
  6159. struct snd_ctl_elem_value *ucontrol)
  6160. {
  6161. struct usb_mixer_elem_info *elem = kctl->private_data;
  6162. struct usb_mixer_interface *mixer = elem->head.mixer;
  6163. struct scarlett2_data *private = mixer->private_data;
  6164. int index = line_out_remap(private, elem->control);
  6165. int oval, val, err = 0;
  6166. mutex_lock(&private->data_mutex);
  6167. if (private->hwdep_in_use) {
  6168. err = -EBUSY;
  6169. goto unlock;
  6170. }
  6171. oval = private->mux[index];
  6172. val = min(ucontrol->value.enumerated.item[0],
  6173. private->num_mux_srcs - 1U);
  6174. if (oval == val)
  6175. goto unlock;
  6176. private->mux[index] = val;
  6177. err = scarlett2_usb_set_mux(mixer);
  6178. if (err == 0)
  6179. err = 1;
  6180. unlock:
  6181. mutex_unlock(&private->data_mutex);
  6182. return err;
  6183. }
  6184. static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
  6185. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  6186. .name = "",
  6187. .info = scarlett2_mux_src_enum_ctl_info,
  6188. .get = scarlett2_mux_src_enum_ctl_get,
  6189. .put = scarlett2_mux_src_enum_ctl_put,
  6190. };
  6191. static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
  6192. {
  6193. struct scarlett2_data *private = mixer->private_data;
  6194. const struct scarlett2_device_info *info = private->info;
  6195. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  6196. int port_type, channel, i;
  6197. for (i = 0, port_type = 0;
  6198. port_type < SCARLETT2_PORT_TYPE_COUNT;
  6199. port_type++) {
  6200. for (channel = 0;
  6201. channel < port_count[port_type][SCARLETT2_PORT_OUT];
  6202. channel++, i++) {
  6203. int err;
  6204. char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  6205. int channel_num = channel + 1;
  6206. const struct scarlett2_port *port =
  6207. &scarlett2_ports[port_type];
  6208. const char *descr = port->dst_descr;
  6209. if (port_type == SCARLETT2_PORT_TYPE_MIX &&
  6210. channel >= private->num_mix_in) {
  6211. channel_num -= private->num_mix_in;
  6212. descr = port->dsp_dst_descr;
  6213. }
  6214. snprintf(s, sizeof(s) - 5, descr, channel_num);
  6215. strcat(s, " Enum");
  6216. err = scarlett2_add_new_ctl(mixer,
  6217. &scarlett2_mux_src_enum_ctl,
  6218. i, 1, s,
  6219. &private->mux_ctls[i]);
  6220. if (err < 0)
  6221. return err;
  6222. }
  6223. }
  6224. return 0;
  6225. }
  6226. /*** Meter Controls ***/
  6227. static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
  6228. struct snd_ctl_elem_info *uinfo)
  6229. {
  6230. struct usb_mixer_elem_info *elem = kctl->private_data;
  6231. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  6232. uinfo->count = elem->channels;
  6233. uinfo->value.integer.min = 0;
  6234. uinfo->value.integer.max = 4095;
  6235. uinfo->value.integer.step = 1;
  6236. return 0;
  6237. }
  6238. static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
  6239. struct snd_ctl_elem_value *ucontrol)
  6240. {
  6241. struct usb_mixer_elem_info *elem = kctl->private_data;
  6242. struct usb_mixer_interface *mixer = elem->head.mixer;
  6243. struct scarlett2_data *private = mixer->private_data;
  6244. u8 *meter_level_map = private->meter_level_map;
  6245. u16 meter_levels[SCARLETT2_MAX_METERS];
  6246. int i, err;
  6247. mutex_lock(&private->data_mutex);
  6248. if (private->hwdep_in_use) {
  6249. err = -EBUSY;
  6250. goto unlock;
  6251. }
  6252. err = scarlett2_usb_get_meter_levels(mixer, elem->channels,
  6253. meter_levels);
  6254. if (err < 0)
  6255. goto unlock;
  6256. /* copy & translate from meter_levels[] using meter_level_map[] */
  6257. for (i = 0; i < elem->channels; i++) {
  6258. int idx = meter_level_map[i];
  6259. int value;
  6260. if (idx == 255)
  6261. value = 0;
  6262. else
  6263. value = meter_levels[idx];
  6264. ucontrol->value.integer.value[i] = value;
  6265. }
  6266. unlock:
  6267. mutex_unlock(&private->data_mutex);
  6268. return err;
  6269. }
  6270. static const struct snd_kcontrol_new scarlett2_meter_ctl = {
  6271. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  6272. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  6273. .name = "",
  6274. .info = scarlett2_meter_ctl_info,
  6275. .get = scarlett2_meter_ctl_get
  6276. };
  6277. static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
  6278. {
  6279. struct scarlett2_data *private = mixer->private_data;
  6280. /* devices without a mixer also don't support reporting levels */
  6281. if (!scarlett2_has_mixer(private))
  6282. return 0;
  6283. return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
  6284. 0, private->num_mux_dsts,
  6285. "Level Meter", NULL);
  6286. }
  6287. /*** MSD Controls ***/
  6288. static int scarlett2_msd_ctl_get(struct snd_kcontrol *kctl,
  6289. struct snd_ctl_elem_value *ucontrol)
  6290. {
  6291. struct usb_mixer_elem_info *elem = kctl->private_data;
  6292. struct scarlett2_data *private = elem->head.mixer->private_data;
  6293. ucontrol->value.integer.value[0] = private->msd_switch;
  6294. return 0;
  6295. }
  6296. static int scarlett2_msd_ctl_put(struct snd_kcontrol *kctl,
  6297. struct snd_ctl_elem_value *ucontrol)
  6298. {
  6299. struct usb_mixer_elem_info *elem = kctl->private_data;
  6300. struct usb_mixer_interface *mixer = elem->head.mixer;
  6301. struct scarlett2_data *private = mixer->private_data;
  6302. int oval, val, err = 0;
  6303. mutex_lock(&private->data_mutex);
  6304. if (private->hwdep_in_use) {
  6305. err = -EBUSY;
  6306. goto unlock;
  6307. }
  6308. oval = private->msd_switch;
  6309. val = !!ucontrol->value.integer.value[0];
  6310. if (oval == val)
  6311. goto unlock;
  6312. private->msd_switch = val;
  6313. /* Send switch change to the device */
  6314. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MSD_SWITCH,
  6315. 0, val);
  6316. if (err == 0)
  6317. err = 1;
  6318. unlock:
  6319. mutex_unlock(&private->data_mutex);
  6320. return err;
  6321. }
  6322. static const struct snd_kcontrol_new scarlett2_msd_ctl = {
  6323. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  6324. .name = "",
  6325. .info = snd_ctl_boolean_mono_info,
  6326. .get = scarlett2_msd_ctl_get,
  6327. .put = scarlett2_msd_ctl_put,
  6328. };
  6329. static int scarlett2_add_msd_ctl(struct usb_mixer_interface *mixer)
  6330. {
  6331. struct scarlett2_data *private = mixer->private_data;
  6332. if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_MSD_SWITCH))
  6333. return 0;
  6334. /* If MSD mode is off, hide the switch by default */
  6335. if (!private->msd_switch && !(mixer->chip->setup & SCARLETT2_MSD_ENABLE))
  6336. return 0;
  6337. /* Add MSD control */
  6338. return scarlett2_add_new_ctl(mixer, &scarlett2_msd_ctl,
  6339. 0, 1, "MSD Mode Switch", NULL);
  6340. }
  6341. /*** Standalone Control ***/
  6342. static int scarlett2_standalone_ctl_get(struct snd_kcontrol *kctl,
  6343. struct snd_ctl_elem_value *ucontrol)
  6344. {
  6345. struct usb_mixer_elem_info *elem = kctl->private_data;
  6346. struct scarlett2_data *private = elem->head.mixer->private_data;
  6347. ucontrol->value.integer.value[0] = private->standalone_switch;
  6348. return 0;
  6349. }
  6350. static int scarlett2_standalone_ctl_put(struct snd_kcontrol *kctl,
  6351. struct snd_ctl_elem_value *ucontrol)
  6352. {
  6353. struct usb_mixer_elem_info *elem = kctl->private_data;
  6354. struct usb_mixer_interface *mixer = elem->head.mixer;
  6355. struct scarlett2_data *private = mixer->private_data;
  6356. int oval, val, err = 0;
  6357. mutex_lock(&private->data_mutex);
  6358. if (private->hwdep_in_use) {
  6359. err = -EBUSY;
  6360. goto unlock;
  6361. }
  6362. oval = private->standalone_switch;
  6363. val = !!ucontrol->value.integer.value[0];
  6364. if (oval == val)
  6365. goto unlock;
  6366. private->standalone_switch = val;
  6367. /* Send switch change to the device */
  6368. err = scarlett2_usb_set_config(mixer,
  6369. SCARLETT2_CONFIG_STANDALONE_SWITCH,
  6370. 0, val);
  6371. if (err == 0)
  6372. err = 1;
  6373. unlock:
  6374. mutex_unlock(&private->data_mutex);
  6375. return err;
  6376. }
  6377. static const struct snd_kcontrol_new scarlett2_standalone_ctl = {
  6378. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  6379. .name = "",
  6380. .info = snd_ctl_boolean_mono_info,
  6381. .get = scarlett2_standalone_ctl_get,
  6382. .put = scarlett2_standalone_ctl_put,
  6383. };
  6384. static int scarlett2_add_standalone_ctl(struct usb_mixer_interface *mixer)
  6385. {
  6386. struct scarlett2_data *private = mixer->private_data;
  6387. if (!scarlett2_has_config_item(private,
  6388. SCARLETT2_CONFIG_STANDALONE_SWITCH))
  6389. return 0;
  6390. /* Add standalone control */
  6391. return scarlett2_add_new_ctl(mixer, &scarlett2_standalone_ctl,
  6392. 0, 1, "Standalone Switch", NULL);
  6393. }
  6394. /*** Power Status ***/
  6395. static int scarlett2_update_power_status(struct usb_mixer_interface *mixer)
  6396. {
  6397. struct scarlett2_data *private = mixer->private_data;
  6398. int err;
  6399. u8 power_ext, power_low;
  6400. private->power_status_updated = 0;
  6401. err = scarlett2_usb_get_config(mixer, SCARLETT2_CONFIG_POWER_EXT,
  6402. 1, &power_ext);
  6403. if (err < 0)
  6404. return err;
  6405. err = scarlett2_usb_get_config(mixer, SCARLETT2_CONFIG_POWER_LOW,
  6406. 1, &power_low);
  6407. if (err < 0)
  6408. return err;
  6409. if (power_low)
  6410. private->power_status = SCARLETT2_POWER_STATUS_FAIL;
  6411. else if (power_ext)
  6412. private->power_status = SCARLETT2_POWER_STATUS_EXT;
  6413. else
  6414. private->power_status = SCARLETT2_POWER_STATUS_BUS;
  6415. return 0;
  6416. }
  6417. static int scarlett2_power_status_ctl_get(struct snd_kcontrol *kctl,
  6418. struct snd_ctl_elem_value *ucontrol)
  6419. {
  6420. struct usb_mixer_elem_info *elem = kctl->private_data;
  6421. struct usb_mixer_interface *mixer = elem->head.mixer;
  6422. struct scarlett2_data *private = mixer->private_data;
  6423. int err = 0;
  6424. mutex_lock(&private->data_mutex);
  6425. if (private->power_status_updated) {
  6426. err = scarlett2_update_power_status(mixer);
  6427. if (err < 0)
  6428. goto unlock;
  6429. }
  6430. ucontrol->value.integer.value[0] = private->power_status;
  6431. unlock:
  6432. mutex_unlock(&private->data_mutex);
  6433. return err;
  6434. }
  6435. static int scarlett2_power_status_ctl_info(
  6436. struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
  6437. {
  6438. static const char *const values[3] = {
  6439. "External", "Bus", "Fail"
  6440. };
  6441. return snd_ctl_enum_info(uinfo, 1, 3, values);
  6442. }
  6443. static const struct snd_kcontrol_new scarlett2_power_status_ctl = {
  6444. .iface = SNDRV_CTL_ELEM_IFACE_CARD,
  6445. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  6446. .name = "",
  6447. .info = scarlett2_power_status_ctl_info,
  6448. .get = scarlett2_power_status_ctl_get,
  6449. };
  6450. static int scarlett2_add_power_status_ctl(struct usb_mixer_interface *mixer)
  6451. {
  6452. struct scarlett2_data *private = mixer->private_data;
  6453. if (!scarlett2_has_config_item(private,
  6454. SCARLETT2_CONFIG_POWER_EXT))
  6455. return 0;
  6456. /* Add power status control */
  6457. return scarlett2_add_new_ctl(mixer, &scarlett2_power_status_ctl,
  6458. 0, 1, "Power Status Card Enum",
  6459. &private->power_status_ctl);
  6460. }
  6461. /*** Bluetooth Volume ***/
  6462. static int scarlett2_update_bluetooth_volume(struct usb_mixer_interface *mixer)
  6463. {
  6464. struct scarlett2_data *private = mixer->private_data;
  6465. int err;
  6466. private->bluetooth_updated = 0;
  6467. if (!private->info->has_bluetooth)
  6468. return 0;
  6469. err = scarlett2_usb_get_config(mixer,
  6470. SCARLETT2_CONFIG_BLUETOOTH_VOLUME,
  6471. 1, &private->bluetooth_volume);
  6472. if (err < 0)
  6473. return err;
  6474. return 0;
  6475. }
  6476. static int scarlett2_bluetooth_volume_ctl_get(struct snd_kcontrol *kctl,
  6477. struct snd_ctl_elem_value *ucontrol)
  6478. {
  6479. struct usb_mixer_elem_info *elem = kctl->private_data;
  6480. struct usb_mixer_interface *mixer = elem->head.mixer;
  6481. struct scarlett2_data *private = mixer->private_data;
  6482. int err = 0;
  6483. mutex_lock(&private->data_mutex);
  6484. if (private->hwdep_in_use) {
  6485. err = -EBUSY;
  6486. goto unlock;
  6487. }
  6488. if (private->bluetooth_updated) {
  6489. err = scarlett2_update_bluetooth_volume(mixer);
  6490. if (err < 0)
  6491. goto unlock;
  6492. }
  6493. ucontrol->value.integer.value[0] = private->bluetooth_volume;
  6494. unlock:
  6495. mutex_unlock(&private->data_mutex);
  6496. return err;
  6497. }
  6498. static int scarlett2_bluetooth_volume_ctl_put(struct snd_kcontrol *kctl,
  6499. struct snd_ctl_elem_value *ucontrol)
  6500. {
  6501. struct usb_mixer_elem_info *elem = kctl->private_data;
  6502. struct usb_mixer_interface *mixer = elem->head.mixer;
  6503. struct scarlett2_data *private = mixer->private_data;
  6504. int oval, val, err = 0;
  6505. mutex_lock(&private->data_mutex);
  6506. if (private->hwdep_in_use) {
  6507. err = -EBUSY;
  6508. goto unlock;
  6509. }
  6510. oval = private->bluetooth_volume;
  6511. val = clamp(ucontrol->value.integer.value[0],
  6512. 0L, (long)SCARLETT2_MAX_BLUETOOTH_VOLUME);
  6513. if (oval == val)
  6514. goto unlock;
  6515. private->bluetooth_volume = val;
  6516. err = scarlett2_usb_set_config(mixer,
  6517. SCARLETT2_CONFIG_BLUETOOTH_VOLUME,
  6518. 0, val);
  6519. if (err == 0)
  6520. err = 1;
  6521. unlock:
  6522. mutex_unlock(&private->data_mutex);
  6523. return err;
  6524. }
  6525. static int scarlett2_bluetooth_volume_ctl_info(
  6526. struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
  6527. {
  6528. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  6529. uinfo->count = 1;
  6530. uinfo->value.integer.min = 0;
  6531. uinfo->value.integer.max = SCARLETT2_MAX_BLUETOOTH_VOLUME;
  6532. uinfo->value.integer.step = 1;
  6533. return 0;
  6534. }
  6535. static const struct snd_kcontrol_new scarlett2_bluetooth_volume_ctl = {
  6536. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  6537. .name = "",
  6538. .info = scarlett2_bluetooth_volume_ctl_info,
  6539. .get = scarlett2_bluetooth_volume_ctl_get,
  6540. .put = scarlett2_bluetooth_volume_ctl_put,
  6541. };
  6542. static int scarlett2_add_bluetooth_volume_ctl(
  6543. struct usb_mixer_interface *mixer)
  6544. {
  6545. struct scarlett2_data *private = mixer->private_data;
  6546. if (!private->info->has_bluetooth)
  6547. return 0;
  6548. /* Add Bluetooth volume control */
  6549. return scarlett2_add_new_ctl(mixer, &scarlett2_bluetooth_volume_ctl,
  6550. 0, 1, "Bluetooth Capture Volume",
  6551. &private->bluetooth_volume_ctl);
  6552. }
  6553. /*** S/PDIF Mode Controls ***/
  6554. static int scarlett2_update_spdif_mode(struct usb_mixer_interface *mixer)
  6555. {
  6556. struct scarlett2_data *private = mixer->private_data;
  6557. int err, i;
  6558. u8 mode;
  6559. const u8 *mode_values = private->info->spdif_mode_values;
  6560. if (!private->info->spdif_mode_control_name)
  6561. return 0;
  6562. err = scarlett2_usb_get_config(mixer, SCARLETT2_CONFIG_SPDIF_MODE,
  6563. 1, &mode);
  6564. if (err < 0)
  6565. return err;
  6566. private->spdif_mode = 0;
  6567. for (i = 0; *mode_values != 0xff; i++, mode_values++)
  6568. if (*mode_values == mode) {
  6569. private->spdif_mode = i;
  6570. break;
  6571. }
  6572. return 0;
  6573. }
  6574. static int scarlett2_spdif_mode_ctl_info(struct snd_kcontrol *kctl,
  6575. struct snd_ctl_elem_info *uinfo)
  6576. {
  6577. struct usb_mixer_elem_info *elem = kctl->private_data;
  6578. struct scarlett2_data *private = elem->head.mixer->private_data;
  6579. const char * const *mode_texts = private->info->spdif_mode_texts;
  6580. int count = 0;
  6581. while (*mode_texts++)
  6582. count++;
  6583. return snd_ctl_enum_info(uinfo, 1, count,
  6584. private->info->spdif_mode_texts);
  6585. }
  6586. static int scarlett2_spdif_mode_ctl_get(struct snd_kcontrol *kctl,
  6587. struct snd_ctl_elem_value *ucontrol)
  6588. {
  6589. struct usb_mixer_elem_info *elem = kctl->private_data;
  6590. struct scarlett2_data *private = elem->head.mixer->private_data;
  6591. ucontrol->value.enumerated.item[0] = private->spdif_mode;
  6592. return 0;
  6593. }
  6594. static int scarlett2_spdif_mode_ctl_put(struct snd_kcontrol *kctl,
  6595. struct snd_ctl_elem_value *ucontrol)
  6596. {
  6597. struct usb_mixer_elem_info *elem = kctl->private_data;
  6598. struct usb_mixer_interface *mixer = elem->head.mixer;
  6599. struct scarlett2_data *private = mixer->private_data;
  6600. int oval, val, err = 0;
  6601. int i;
  6602. mutex_lock(&private->data_mutex);
  6603. oval = private->spdif_mode;
  6604. val = ucontrol->value.enumerated.item[0];
  6605. if (val < 0) {
  6606. err = -EINVAL;
  6607. goto unlock;
  6608. }
  6609. for (i = 0; i <= val; i++)
  6610. if (private->info->spdif_mode_values[i] == 0xff) {
  6611. err = -EINVAL;
  6612. goto unlock;
  6613. }
  6614. if (oval == val)
  6615. goto unlock;
  6616. private->spdif_mode = val;
  6617. err = scarlett2_usb_set_config(
  6618. mixer, SCARLETT2_CONFIG_SPDIF_MODE, 0,
  6619. private->info->spdif_mode_values[val]);
  6620. if (!err)
  6621. err = 1;
  6622. unlock:
  6623. mutex_unlock(&private->data_mutex);
  6624. return err;
  6625. }
  6626. static const struct snd_kcontrol_new scarlett2_spdif_mode_ctl = {
  6627. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  6628. .name = "",
  6629. .info = scarlett2_spdif_mode_ctl_info,
  6630. .get = scarlett2_spdif_mode_ctl_get,
  6631. .put = scarlett2_spdif_mode_ctl_put,
  6632. };
  6633. static int scarlett2_add_spdif_mode_ctl(struct usb_mixer_interface *mixer)
  6634. {
  6635. struct scarlett2_data *private = mixer->private_data;
  6636. if (!private->info->spdif_mode_control_name)
  6637. return 0;
  6638. return scarlett2_add_new_ctl(mixer, &scarlett2_spdif_mode_ctl,
  6639. 0, 1,
  6640. private->info->spdif_mode_control_name,
  6641. NULL);
  6642. }
  6643. /*** Notification Handlers ***/
  6644. /* Notify on sync change */
  6645. static void scarlett2_notify_sync(struct usb_mixer_interface *mixer)
  6646. {
  6647. struct scarlett2_data *private = mixer->private_data;
  6648. private->sync_updated = 1;
  6649. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  6650. &private->sync_ctl->id);
  6651. }
  6652. /* Notify on monitor change (Gen 2/3) */
  6653. static void scarlett2_notify_monitor(struct usb_mixer_interface *mixer)
  6654. {
  6655. struct snd_card *card = mixer->chip->card;
  6656. struct scarlett2_data *private = mixer->private_data;
  6657. int i;
  6658. if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
  6659. return;
  6660. private->vol_updated = 1;
  6661. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  6662. &private->master_vol_ctl->id);
  6663. for (i = 0; i < private->num_line_out; i++)
  6664. if (private->vol_sw_hw_switch[line_out_remap(private, i)])
  6665. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6666. &private->vol_ctls[i]->id);
  6667. }
  6668. /* Notify on volume change (Gen 4) */
  6669. static void scarlett2_notify_volume(struct usb_mixer_interface *mixer)
  6670. {
  6671. struct scarlett2_data *private = mixer->private_data;
  6672. private->vol_updated = 1;
  6673. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  6674. &private->master_vol_ctl->id);
  6675. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  6676. &private->headphone_vol_ctl->id);
  6677. }
  6678. /* Notify on dim/mute change */
  6679. static void scarlett2_notify_dim_mute(struct usb_mixer_interface *mixer)
  6680. {
  6681. struct snd_card *card = mixer->chip->card;
  6682. struct scarlett2_data *private = mixer->private_data;
  6683. int i;
  6684. if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
  6685. return;
  6686. private->dim_mute_updated = 1;
  6687. for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
  6688. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6689. &private->dim_mute_ctls[i]->id);
  6690. for (i = 0; i < private->num_line_out; i++)
  6691. if (private->vol_sw_hw_switch[line_out_remap(private, i)])
  6692. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6693. &private->mute_ctls[i]->id);
  6694. }
  6695. /* Notify on input level switch change */
  6696. static void scarlett2_notify_input_level(struct usb_mixer_interface *mixer)
  6697. {
  6698. struct snd_card *card = mixer->chip->card;
  6699. struct scarlett2_data *private = mixer->private_data;
  6700. const struct scarlett2_device_info *info = private->info;
  6701. int i;
  6702. private->input_level_updated = 1;
  6703. for (i = 0; i < info->level_input_count; i++)
  6704. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6705. &private->level_ctls[i]->id);
  6706. }
  6707. /* Notify on input pad switch change */
  6708. static void scarlett2_notify_input_pad(struct usb_mixer_interface *mixer)
  6709. {
  6710. struct snd_card *card = mixer->chip->card;
  6711. struct scarlett2_data *private = mixer->private_data;
  6712. const struct scarlett2_device_info *info = private->info;
  6713. int i;
  6714. private->input_pad_updated = 1;
  6715. for (i = 0; i < info->pad_input_count; i++)
  6716. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6717. &private->pad_ctls[i]->id);
  6718. }
  6719. /* Notify on input air switch change */
  6720. static void scarlett2_notify_input_air(struct usb_mixer_interface *mixer)
  6721. {
  6722. struct snd_card *card = mixer->chip->card;
  6723. struct scarlett2_data *private = mixer->private_data;
  6724. const struct scarlett2_device_info *info = private->info;
  6725. int i;
  6726. private->input_air_updated = 1;
  6727. for (i = 0; i < info->air_input_count; i++)
  6728. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6729. &private->air_ctls[i]->id);
  6730. }
  6731. /* Notify on input DSP switch change */
  6732. static void scarlett2_notify_input_dsp(struct usb_mixer_interface *mixer)
  6733. {
  6734. struct snd_card *card = mixer->chip->card;
  6735. struct scarlett2_data *private = mixer->private_data;
  6736. const struct scarlett2_device_info *info = private->info;
  6737. int i;
  6738. private->input_dsp_updated = 1;
  6739. for (i = 0; i < info->dsp_input_count; i++)
  6740. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6741. &private->dsp_ctls[i]->id);
  6742. }
  6743. /* Notify on input mute switch change */
  6744. static void scarlett2_notify_input_mute(struct usb_mixer_interface *mixer)
  6745. {
  6746. struct snd_card *card = mixer->chip->card;
  6747. struct scarlett2_data *private = mixer->private_data;
  6748. const struct scarlett2_device_info *info = private->info;
  6749. int i;
  6750. private->input_mute_updated = 1;
  6751. for (i = 0; i < info->mute_input_count; i++)
  6752. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6753. &private->input_mute_ctls[i]->id);
  6754. }
  6755. /* Notify on input phantom switch change */
  6756. static void scarlett2_notify_input_phantom(struct usb_mixer_interface *mixer)
  6757. {
  6758. struct snd_card *card = mixer->chip->card;
  6759. struct scarlett2_data *private = mixer->private_data;
  6760. const struct scarlett2_device_info *info = private->info;
  6761. int i;
  6762. private->input_phantom_updated = 1;
  6763. for (i = 0; i < info->phantom_count; i++)
  6764. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6765. &private->phantom_ctls[i]->id);
  6766. scarlett2_phantom_notify_access(mixer);
  6767. }
  6768. /* Notify on "input other" change (level/pad/air/phantom) */
  6769. static void scarlett2_notify_input_other(struct usb_mixer_interface *mixer)
  6770. {
  6771. scarlett2_notify_input_level(mixer);
  6772. scarlett2_notify_input_pad(mixer);
  6773. scarlett2_notify_input_air(mixer);
  6774. scarlett2_notify_input_phantom(mixer);
  6775. }
  6776. /* Notify on input select change */
  6777. static void scarlett2_notify_input_select(struct usb_mixer_interface *mixer)
  6778. {
  6779. struct snd_card *card = mixer->chip->card;
  6780. struct scarlett2_data *private = mixer->private_data;
  6781. const struct scarlett2_device_info *info = private->info;
  6782. int i;
  6783. if (!scarlett2_has_config_item(private,
  6784. SCARLETT2_CONFIG_INPUT_SELECT_SWITCH))
  6785. return;
  6786. private->input_select_updated = 1;
  6787. snd_ctl_notify(card,
  6788. SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
  6789. &private->input_select_ctl->id);
  6790. for (i = 0; i < info->gain_input_count / 2; i++)
  6791. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6792. &private->input_link_ctls[i]->id);
  6793. }
  6794. /* Notify on input gain change */
  6795. static void scarlett2_notify_input_gain(struct usb_mixer_interface *mixer)
  6796. {
  6797. struct snd_card *card = mixer->chip->card;
  6798. struct scarlett2_data *private = mixer->private_data;
  6799. const struct scarlett2_device_info *info = private->info;
  6800. int i;
  6801. if (!info->gain_input_count)
  6802. return;
  6803. private->input_gain_updated = 1;
  6804. for (i = 0; i < info->gain_input_count; i++)
  6805. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6806. &private->input_gain_ctls[i]->id);
  6807. }
  6808. /* Notify on autogain change */
  6809. static void scarlett2_notify_autogain(struct usb_mixer_interface *mixer)
  6810. {
  6811. struct snd_card *card = mixer->chip->card;
  6812. struct scarlett2_data *private = mixer->private_data;
  6813. const struct scarlett2_device_info *info = private->info;
  6814. int i;
  6815. if (!info->gain_input_count)
  6816. return;
  6817. private->autogain_updated = 1;
  6818. for (i = 0; i < info->gain_input_count; i++) {
  6819. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6820. &private->autogain_ctls[i]->id);
  6821. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6822. &private->autogain_status_ctls[i]->id);
  6823. }
  6824. for (i = 0; i < SCARLETT2_AG_TARGET_COUNT; i++)
  6825. if (scarlett2_has_config_item(private,
  6826. scarlett2_ag_target_configs[i]))
  6827. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
  6828. &private->ag_target_ctls[i]->id);
  6829. scarlett2_autogain_notify_access(mixer);
  6830. }
  6831. /* Notify on input safe switch change */
  6832. static void scarlett2_notify_input_safe(struct usb_mixer_interface *mixer)
  6833. {
  6834. struct snd_card *card = mixer->chip->card;
  6835. struct scarlett2_data *private = mixer->private_data;
  6836. const struct scarlett2_device_info *info = private->info;
  6837. int i;
  6838. if (!info->safe_input_count)
  6839. return;
  6840. private->input_safe_updated = 1;
  6841. for (i = 0; i < info->safe_input_count; i++)
  6842. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6843. &private->safe_ctls[i]->id);
  6844. }
  6845. /* Notify on "monitor other" change (speaker switching, talkback) */
  6846. static void scarlett2_notify_monitor_other(struct usb_mixer_interface *mixer)
  6847. {
  6848. struct snd_card *card = mixer->chip->card;
  6849. struct scarlett2_data *private = mixer->private_data;
  6850. const struct scarlett2_device_info *info = private->info;
  6851. private->monitor_other_updated = 1;
  6852. if (info->has_speaker_switching)
  6853. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6854. &private->speaker_switching_ctl->id);
  6855. if (info->has_talkback)
  6856. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6857. &private->talkback_ctl->id);
  6858. /* if speaker switching was recently enabled or disabled,
  6859. * invalidate the dim/mute and mux enum controls
  6860. */
  6861. if (private->speaker_switching_switched) {
  6862. int i;
  6863. scarlett2_notify_dim_mute(mixer);
  6864. private->speaker_switching_switched = 0;
  6865. private->mux_updated = 1;
  6866. for (i = 0; i < private->num_mux_dsts; i++)
  6867. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6868. &private->mux_ctls[i]->id);
  6869. }
  6870. }
  6871. /* Notify on direct monitor switch change */
  6872. static void scarlett2_notify_direct_monitor(struct usb_mixer_interface *mixer)
  6873. {
  6874. struct snd_card *card = mixer->chip->card;
  6875. struct scarlett2_data *private = mixer->private_data;
  6876. int count = private->num_mix_in * private->num_mix_out;
  6877. int i;
  6878. private->direct_monitor_updated = 1;
  6879. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6880. &private->direct_monitor_ctl->id);
  6881. if (!scarlett2_has_mixer(private))
  6882. return;
  6883. private->mix_updated = 1;
  6884. /* Notify of change to the mix controls */
  6885. for (i = 0; i < count; i++)
  6886. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6887. &private->mix_ctls[i]->id);
  6888. }
  6889. /* Notify on power change */
  6890. static void scarlett2_notify_power_status(struct usb_mixer_interface *mixer)
  6891. {
  6892. struct snd_card *card = mixer->chip->card;
  6893. struct scarlett2_data *private = mixer->private_data;
  6894. private->power_status_updated = 1;
  6895. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6896. &private->power_status_ctl->id);
  6897. }
  6898. /* Notify on mux change */
  6899. static void scarlett2_notify_mux(struct usb_mixer_interface *mixer)
  6900. {
  6901. struct snd_card *card = mixer->chip->card;
  6902. struct scarlett2_data *private = mixer->private_data;
  6903. int i;
  6904. private->mux_updated = 1;
  6905. for (i = 0; i < private->num_mux_dsts; i++)
  6906. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6907. &private->mux_ctls[i]->id);
  6908. }
  6909. /* Notify on PCM input switch change */
  6910. static void scarlett2_notify_pcm_input_switch(struct usb_mixer_interface *mixer)
  6911. {
  6912. struct snd_card *card = mixer->chip->card;
  6913. struct scarlett2_data *private = mixer->private_data;
  6914. private->pcm_input_switch_updated = 1;
  6915. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6916. &private->pcm_input_switch_ctl->id);
  6917. scarlett2_notify_mux(mixer);
  6918. }
  6919. /* Notify on Bluetooth change */
  6920. static void scarlett2_notify_bluetooth(struct usb_mixer_interface *mixer)
  6921. {
  6922. struct snd_card *card = mixer->chip->card;
  6923. struct scarlett2_data *private = mixer->private_data;
  6924. if (!private->info->has_bluetooth)
  6925. return;
  6926. private->bluetooth_updated = 1;
  6927. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  6928. &private->bluetooth_volume_ctl->id);
  6929. }
  6930. /* Handle acknowledgement that a command was received; let
  6931. * scarlett2_usb() know that it can proceed
  6932. */
  6933. static void scarlett2_notify_ack(struct usb_mixer_interface *mixer)
  6934. {
  6935. struct scarlett2_data *private = mixer->private_data;
  6936. /* if running == 0, ignore ACKs */
  6937. if (private->running)
  6938. complete(&private->cmd_done);
  6939. }
  6940. /* Interrupt callback */
  6941. static void scarlett2_notify(struct urb *urb)
  6942. {
  6943. struct usb_mixer_interface *mixer = urb->context;
  6944. int len = urb->actual_length;
  6945. int ustatus = urb->status;
  6946. u32 data;
  6947. struct scarlett2_data *private = mixer->private_data;
  6948. const struct scarlett2_notification *notifications =
  6949. private->config_set->notifications;
  6950. if (ustatus != 0 || len != 8)
  6951. goto requeue;
  6952. data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
  6953. /* Ignore notifications except ACK during initialisation.
  6954. * ACK is 0x00000001 on every device.
  6955. */
  6956. if (private->running < 2)
  6957. data &= 1;
  6958. while (data && notifications->mask) {
  6959. if (data & notifications->mask) {
  6960. data &= ~notifications->mask;
  6961. if (notifications->func)
  6962. notifications->func(mixer);
  6963. }
  6964. notifications++;
  6965. }
  6966. if (data)
  6967. usb_audio_warn(mixer->chip,
  6968. "%s: Unhandled notification: 0x%08x\n",
  6969. __func__, data);
  6970. requeue:
  6971. if (ustatus != -ENOENT &&
  6972. ustatus != -ECONNRESET &&
  6973. ustatus != -ESHUTDOWN) {
  6974. urb->dev = mixer->chip->dev;
  6975. usb_submit_urb(urb, GFP_ATOMIC);
  6976. } else {
  6977. complete(&private->cmd_done);
  6978. }
  6979. }
  6980. /*** Cleanup/Suspend Callbacks ***/
  6981. static void scarlett2_private_free(struct usb_mixer_interface *mixer)
  6982. {
  6983. struct scarlett2_data *private = mixer->private_data;
  6984. cancel_delayed_work_sync(&private->work);
  6985. kfree(private);
  6986. mixer->private_data = NULL;
  6987. }
  6988. static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
  6989. {
  6990. struct scarlett2_data *private = mixer->private_data;
  6991. if (cancel_delayed_work_sync(&private->work))
  6992. scarlett2_config_save(private->mixer);
  6993. }
  6994. /*** Initialisation ***/
  6995. static void scarlett2_count_io(struct scarlett2_data *private)
  6996. {
  6997. const struct scarlett2_device_info *info = private->info;
  6998. const struct scarlett2_config_set *config_set = info->config_set;
  6999. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  7000. int port_type, srcs = 0, dsts = 0, i;
  7001. /* Count the number of mux sources and destinations */
  7002. for (port_type = 0;
  7003. port_type < SCARLETT2_PORT_TYPE_COUNT;
  7004. port_type++) {
  7005. srcs += port_count[port_type][SCARLETT2_PORT_IN];
  7006. dsts += port_count[port_type][SCARLETT2_PORT_OUT];
  7007. }
  7008. private->num_mux_srcs = srcs;
  7009. private->num_mux_dsts = dsts;
  7010. /* Mixer inputs are mux outputs and vice versa.
  7011. * Scarlett Gen 4 DSP I/O uses SCARLETT2_PORT_TYPE_MIX but
  7012. * doesn't have mixer controls.
  7013. */
  7014. private->num_mix_in =
  7015. port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT] -
  7016. info->dsp_count;
  7017. private->num_mix_out =
  7018. port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN] -
  7019. info->dsp_count;
  7020. /* Number of analogue line outputs */
  7021. private->num_line_out =
  7022. port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
  7023. /* Number of monitor mix controls */
  7024. private->num_monitor_mix_ctls =
  7025. info->direct_monitor * 2 * private->num_mix_in;
  7026. /* Number of autogain status texts */
  7027. if (config_set->autogain_status_texts) {
  7028. const char * const *texts = config_set->autogain_status_texts;
  7029. for (i = 0; texts[i]; i++)
  7030. ;
  7031. private->num_autogain_status_texts = i;
  7032. }
  7033. }
  7034. /* Look through the interface descriptors for the Focusrite Control
  7035. * interface (bInterfaceClass = 255 Vendor Specific Class) and set
  7036. * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
  7037. * in private
  7038. */
  7039. static int scarlett2_find_fc_interface(struct usb_device *dev,
  7040. struct scarlett2_data *private)
  7041. {
  7042. struct usb_host_config *config = dev->actconfig;
  7043. int i;
  7044. for (i = 0; i < config->desc.bNumInterfaces; i++) {
  7045. struct usb_interface *intf = config->interface[i];
  7046. struct usb_interface_descriptor *desc =
  7047. &intf->altsetting[0].desc;
  7048. struct usb_endpoint_descriptor *epd;
  7049. if (desc->bInterfaceClass != 255)
  7050. continue;
  7051. epd = get_endpoint(intf->altsetting, 0);
  7052. private->bInterfaceNumber = desc->bInterfaceNumber;
  7053. private->bEndpointAddress = epd->bEndpointAddress &
  7054. USB_ENDPOINT_NUMBER_MASK;
  7055. private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
  7056. private->bInterval = epd->bInterval;
  7057. return 0;
  7058. }
  7059. return -EINVAL;
  7060. }
  7061. /* Initialise private data */
  7062. static int scarlett2_init_private(struct usb_mixer_interface *mixer,
  7063. const struct scarlett2_device_entry *entry)
  7064. {
  7065. struct scarlett2_data *private =
  7066. kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);
  7067. if (!private)
  7068. return -ENOMEM;
  7069. mutex_init(&private->usb_mutex);
  7070. mutex_init(&private->data_mutex);
  7071. INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
  7072. mixer->private_data = private;
  7073. mixer->private_free = scarlett2_private_free;
  7074. mixer->private_suspend = scarlett2_private_suspend;
  7075. private->info = entry->info;
  7076. private->config_set = entry->info->config_set;
  7077. private->series_name = entry->series_name;
  7078. scarlett2_count_io(private);
  7079. private->scarlett2_seq = 0;
  7080. private->mixer = mixer;
  7081. return scarlett2_find_fc_interface(mixer->chip->dev, private);
  7082. }
  7083. /* Submit a URB to receive notifications from the device */
  7084. static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
  7085. {
  7086. struct usb_device *dev = mixer->chip->dev;
  7087. struct scarlett2_data *private = mixer->private_data;
  7088. unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
  7089. void *transfer_buffer;
  7090. if (mixer->urb) {
  7091. usb_audio_err(mixer->chip,
  7092. "%s: mixer urb already in use!\n", __func__);
  7093. return 0;
  7094. }
  7095. if (usb_pipe_type_check(dev, pipe))
  7096. return -EINVAL;
  7097. mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
  7098. if (!mixer->urb)
  7099. return -ENOMEM;
  7100. transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
  7101. if (!transfer_buffer)
  7102. return -ENOMEM;
  7103. usb_fill_int_urb(mixer->urb, dev, pipe,
  7104. transfer_buffer, private->wMaxPacketSize,
  7105. scarlett2_notify, mixer, private->bInterval);
  7106. init_completion(&private->cmd_done);
  7107. return usb_submit_urb(mixer->urb, GFP_KERNEL);
  7108. }
  7109. /* Cargo cult proprietary initialisation sequence */
  7110. static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
  7111. {
  7112. struct usb_device *dev = mixer->chip->dev;
  7113. struct scarlett2_data *private = mixer->private_data;
  7114. u8 step0_buf[24];
  7115. u8 step2_buf[84];
  7116. int err;
  7117. if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
  7118. return -EINVAL;
  7119. /* step 0 */
  7120. err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
  7121. SCARLETT2_USB_CMD_INIT,
  7122. step0_buf, sizeof(step0_buf));
  7123. if (err < 0)
  7124. return err;
  7125. /* Set up the interrupt polling for notifications.
  7126. * When running is:
  7127. * 0: all notifications are ignored
  7128. * 1: only ACKs are handled
  7129. * 2: all notifications are handled
  7130. */
  7131. err = scarlett2_init_notify(mixer);
  7132. if (err < 0)
  7133. return err;
  7134. /* sleep for a moment in case of an outstanding ACK */
  7135. msleep(20);
  7136. /* start handling ACKs, but no other notifications until the
  7137. * ALSA controls have been created
  7138. */
  7139. private->running = 1;
  7140. /* step 1 */
  7141. private->scarlett2_seq = 1;
  7142. err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
  7143. if (err < 0)
  7144. return err;
  7145. /* step 2 */
  7146. private->scarlett2_seq = 1;
  7147. err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_2,
  7148. NULL, 0,
  7149. step2_buf, sizeof(step2_buf));
  7150. if (err < 0)
  7151. return err;
  7152. /* extract 4-byte firmware version from step2_buf[8] */
  7153. private->firmware_version = le32_to_cpu(*(__le32 *)(step2_buf + 8));
  7154. usb_audio_info(mixer->chip,
  7155. "Firmware version %d\n",
  7156. private->firmware_version);
  7157. return 0;
  7158. }
  7159. /* Get the flash segment numbers for the App_Settings and App_Upgrade
  7160. * segments and put them in the private data
  7161. */
  7162. static int scarlett2_get_flash_segment_nums(struct usb_mixer_interface *mixer)
  7163. {
  7164. struct scarlett2_data *private = mixer->private_data;
  7165. int err, count, i;
  7166. struct {
  7167. __le32 size;
  7168. __le32 count;
  7169. u8 unknown[8];
  7170. } __packed flash_info;
  7171. struct {
  7172. __le32 size;
  7173. __le32 flags;
  7174. char name[16];
  7175. } __packed segment_info;
  7176. err = scarlett2_usb(mixer, SCARLETT2_USB_INFO_FLASH,
  7177. NULL, 0,
  7178. &flash_info, sizeof(flash_info));
  7179. if (err < 0)
  7180. return err;
  7181. count = le32_to_cpu(flash_info.count);
  7182. /* sanity check count */
  7183. if (count < SCARLETT2_SEGMENT_NUM_MIN ||
  7184. count > SCARLETT2_SEGMENT_NUM_MAX + 1) {
  7185. usb_audio_err(mixer->chip,
  7186. "invalid flash segment count: %d\n", count);
  7187. return -EINVAL;
  7188. }
  7189. for (i = 0; i < count; i++) {
  7190. __le32 segment_num_req = cpu_to_le32(i);
  7191. int flash_segment_id;
  7192. err = scarlett2_usb(mixer, SCARLETT2_USB_INFO_SEGMENT,
  7193. &segment_num_req, sizeof(segment_num_req),
  7194. &segment_info, sizeof(segment_info));
  7195. if (err < 0) {
  7196. usb_audio_err(mixer->chip,
  7197. "failed to get flash segment info %d: %d\n",
  7198. i, err);
  7199. return err;
  7200. }
  7201. if (!strncmp(segment_info.name,
  7202. SCARLETT2_SEGMENT_SETTINGS_NAME, 16))
  7203. flash_segment_id = SCARLETT2_SEGMENT_ID_SETTINGS;
  7204. else if (!strncmp(segment_info.name,
  7205. SCARLETT2_SEGMENT_FIRMWARE_NAME, 16))
  7206. flash_segment_id = SCARLETT2_SEGMENT_ID_FIRMWARE;
  7207. else
  7208. continue;
  7209. private->flash_segment_nums[flash_segment_id] = i;
  7210. private->flash_segment_blocks[flash_segment_id] =
  7211. le32_to_cpu(segment_info.size) /
  7212. SCARLETT2_FLASH_BLOCK_SIZE;
  7213. }
  7214. /* segment 0 is App_Gold and we never want to touch that, so
  7215. * use 0 as the "not-found" value
  7216. */
  7217. if (!private->flash_segment_nums[SCARLETT2_SEGMENT_ID_SETTINGS]) {
  7218. usb_audio_err(mixer->chip,
  7219. "failed to find flash segment %s\n",
  7220. SCARLETT2_SEGMENT_SETTINGS_NAME);
  7221. return -EINVAL;
  7222. }
  7223. if (!private->flash_segment_nums[SCARLETT2_SEGMENT_ID_FIRMWARE]) {
  7224. usb_audio_err(mixer->chip,
  7225. "failed to find flash segment %s\n",
  7226. SCARLETT2_SEGMENT_FIRMWARE_NAME);
  7227. return -EINVAL;
  7228. }
  7229. return 0;
  7230. }
  7231. /* Read configuration from the interface on start */
  7232. static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
  7233. {
  7234. struct scarlett2_data *private = mixer->private_data;
  7235. const struct scarlett2_device_info *info = private->info;
  7236. int err, i;
  7237. if (scarlett2_has_config_item(private, SCARLETT2_CONFIG_MSD_SWITCH)) {
  7238. err = scarlett2_usb_get_config(
  7239. mixer, SCARLETT2_CONFIG_MSD_SWITCH,
  7240. 1, &private->msd_switch);
  7241. if (err < 0)
  7242. return err;
  7243. }
  7244. if (private->firmware_version < info->min_firmware_version) {
  7245. usb_audio_err(mixer->chip,
  7246. "Focusrite %s firmware version %d is too old; "
  7247. "need %d",
  7248. private->series_name,
  7249. private->firmware_version,
  7250. info->min_firmware_version);
  7251. return 0;
  7252. }
  7253. /* no other controls are created if MSD mode is on */
  7254. if (private->msd_switch)
  7255. return 0;
  7256. err = scarlett2_update_input_level(mixer);
  7257. if (err < 0)
  7258. return err;
  7259. err = scarlett2_update_input_pad(mixer);
  7260. if (err < 0)
  7261. return err;
  7262. err = scarlett2_update_input_air(mixer);
  7263. if (err < 0)
  7264. return err;
  7265. err = scarlett2_update_input_dsp(mixer);
  7266. if (err < 0)
  7267. return err;
  7268. err = scarlett2_update_compressor_values(mixer);
  7269. if (err < 0)
  7270. return err;
  7271. err = scarlett2_update_filter_values(mixer);
  7272. if (err < 0)
  7273. return err;
  7274. err = scarlett2_update_input_mute(mixer);
  7275. if (err < 0)
  7276. return err;
  7277. err = scarlett2_update_input_phantom(mixer);
  7278. if (err < 0)
  7279. return err;
  7280. err = scarlett2_update_direct_monitor(mixer);
  7281. if (err < 0)
  7282. return err;
  7283. /* the rest of the configuration is for devices with a mixer */
  7284. if (!scarlett2_has_mixer(private))
  7285. return 0;
  7286. err = scarlett2_update_monitor_mix(mixer);
  7287. if (err < 0)
  7288. return err;
  7289. err = scarlett2_update_monitor_other(mixer);
  7290. if (err < 0)
  7291. return err;
  7292. if (scarlett2_has_config_item(private,
  7293. SCARLETT2_CONFIG_STANDALONE_SWITCH)) {
  7294. err = scarlett2_usb_get_config(
  7295. mixer, SCARLETT2_CONFIG_STANDALONE_SWITCH,
  7296. 1, &private->standalone_switch);
  7297. if (err < 0)
  7298. return err;
  7299. }
  7300. if (scarlett2_has_config_item(private,
  7301. SCARLETT2_CONFIG_POWER_EXT)) {
  7302. err = scarlett2_update_power_status(mixer);
  7303. if (err < 0)
  7304. return err;
  7305. }
  7306. err = scarlett2_update_sync(mixer);
  7307. if (err < 0)
  7308. return err;
  7309. if (scarlett2_has_config_item(private,
  7310. SCARLETT2_CONFIG_LINE_OUT_VOLUME)) {
  7311. s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
  7312. /* read SW line out volume */
  7313. err = scarlett2_usb_get_config(
  7314. mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
  7315. private->num_line_out, &sw_vol);
  7316. if (err < 0)
  7317. return err;
  7318. for (i = 0; i < private->num_line_out; i++)
  7319. private->vol[i] = clamp(
  7320. sw_vol[i] + SCARLETT2_VOLUME_BIAS,
  7321. 0, SCARLETT2_VOLUME_BIAS);
  7322. /* read SW mute */
  7323. err = scarlett2_usb_get_config(
  7324. mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
  7325. private->num_line_out, &private->mute_switch);
  7326. if (err < 0)
  7327. return err;
  7328. for (i = 0; i < private->num_line_out; i++)
  7329. private->mute_switch[i] =
  7330. !!private->mute_switch[i];
  7331. /* read SW/HW switches */
  7332. if (scarlett2_has_config_item(private,
  7333. SCARLETT2_CONFIG_SW_HW_SWITCH)) {
  7334. err = scarlett2_usb_get_config(
  7335. mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
  7336. private->num_line_out,
  7337. &private->vol_sw_hw_switch);
  7338. if (err < 0)
  7339. return err;
  7340. for (i = 0; i < private->num_line_out; i++)
  7341. private->vol_sw_hw_switch[i] =
  7342. !!private->vol_sw_hw_switch[i];
  7343. }
  7344. }
  7345. err = scarlett2_update_volumes(mixer);
  7346. if (err < 0)
  7347. return err;
  7348. err = scarlett2_update_dim_mute(mixer);
  7349. if (err < 0)
  7350. return err;
  7351. err = scarlett2_update_input_select(mixer);
  7352. if (err < 0)
  7353. return err;
  7354. err = scarlett2_update_input_gain(mixer);
  7355. if (err < 0)
  7356. return err;
  7357. err = scarlett2_update_autogain(mixer);
  7358. if (err < 0)
  7359. return err;
  7360. err = scarlett2_update_input_safe(mixer);
  7361. if (err < 0)
  7362. return err;
  7363. if (scarlett2_has_config_item(private,
  7364. SCARLETT2_CONFIG_PCM_INPUT_SWITCH)) {
  7365. err = scarlett2_update_pcm_input_switch(mixer);
  7366. if (err < 0)
  7367. return err;
  7368. }
  7369. err = scarlett2_update_bluetooth_volume(mixer);
  7370. if (err < 0)
  7371. return err;
  7372. err = scarlett2_update_spdif_mode(mixer);
  7373. if (err < 0)
  7374. return err;
  7375. err = scarlett2_update_mix(mixer);
  7376. if (err < 0)
  7377. return err;
  7378. return scarlett2_usb_get_mux(mixer);
  7379. }
  7380. static const struct scarlett2_device_entry *get_scarlett2_device_entry(
  7381. struct usb_mixer_interface *mixer)
  7382. {
  7383. const struct scarlett2_device_entry *entry = scarlett2_devices;
  7384. /* Find entry in scarlett2_devices */
  7385. while (entry->usb_id && entry->usb_id != mixer->chip->usb_id)
  7386. entry++;
  7387. if (!entry->usb_id)
  7388. return NULL;
  7389. return entry;
  7390. }
  7391. static int snd_scarlett2_controls_create(
  7392. struct usb_mixer_interface *mixer,
  7393. const struct scarlett2_device_entry *entry)
  7394. {
  7395. struct scarlett2_data *private;
  7396. int err;
  7397. /* Initialise private data */
  7398. err = scarlett2_init_private(mixer, entry);
  7399. if (err < 0)
  7400. return err;
  7401. private = mixer->private_data;
  7402. /* Send proprietary USB initialisation sequence */
  7403. err = scarlett2_usb_init(mixer);
  7404. if (err < 0)
  7405. return err;
  7406. /* Get the upgrade & settings flash segment numbers */
  7407. err = scarlett2_get_flash_segment_nums(mixer);
  7408. if (err < 0)
  7409. return err;
  7410. /* Add firmware version control */
  7411. err = scarlett2_add_firmware_version_ctl(mixer);
  7412. if (err < 0)
  7413. return err;
  7414. /* Add minimum firmware version control */
  7415. err = scarlett2_add_min_firmware_version_ctl(mixer);
  7416. if (err < 0)
  7417. return err;
  7418. /* Read volume levels and controls from the interface */
  7419. err = scarlett2_read_configs(mixer);
  7420. if (err < 0)
  7421. return err;
  7422. /* Create the MSD control */
  7423. err = scarlett2_add_msd_ctl(mixer);
  7424. if (err < 0)
  7425. return err;
  7426. /* If MSD mode is enabled, or if the firmware version is too
  7427. * old, don't create any other controls
  7428. */
  7429. if (private->msd_switch ||
  7430. private->firmware_version < private->info->min_firmware_version)
  7431. return 0;
  7432. /* Create the analogue output controls */
  7433. err = scarlett2_add_line_out_ctls(mixer);
  7434. if (err < 0)
  7435. return err;
  7436. /* Create the analogue input controls */
  7437. err = scarlett2_add_line_in_ctls(mixer);
  7438. if (err < 0)
  7439. return err;
  7440. /* Create the input, output, and mixer mux input selections */
  7441. err = scarlett2_add_mux_enums(mixer);
  7442. if (err < 0)
  7443. return err;
  7444. /* Create the matrix mixer controls */
  7445. err = scarlett2_add_mixer_ctls(mixer);
  7446. if (err < 0)
  7447. return err;
  7448. /* Create the level meter controls */
  7449. err = scarlett2_add_meter_ctl(mixer);
  7450. if (err < 0)
  7451. return err;
  7452. /* Create the sync control */
  7453. err = scarlett2_add_sync_ctl(mixer);
  7454. if (err < 0)
  7455. return err;
  7456. /* Create the direct monitor control(s) */
  7457. err = scarlett2_add_direct_monitor_ctls(mixer);
  7458. if (err < 0)
  7459. return err;
  7460. /* Create the speaker switching control */
  7461. err = scarlett2_add_speaker_switch_ctl(mixer);
  7462. if (err < 0)
  7463. return err;
  7464. /* Create the talkback controls */
  7465. err = scarlett2_add_talkback_ctls(mixer);
  7466. if (err < 0)
  7467. return err;
  7468. /* Create the standalone control */
  7469. err = scarlett2_add_standalone_ctl(mixer);
  7470. if (err < 0)
  7471. return err;
  7472. /* Create the power status control */
  7473. err = scarlett2_add_power_status_ctl(mixer);
  7474. if (err < 0)
  7475. return err;
  7476. /* Create the Bluetooth volume control */
  7477. err = scarlett2_add_bluetooth_volume_ctl(mixer);
  7478. if (err < 0)
  7479. return err;
  7480. /* Create the S/PDIF mode control */
  7481. err = scarlett2_add_spdif_mode_ctl(mixer);
  7482. if (err < 0)
  7483. return err;
  7484. /* Set the access mode of controls disabled during
  7485. * autogain/phantom power switching.
  7486. */
  7487. if (private->info->gain_input_count) {
  7488. scarlett2_autogain_update_access(mixer);
  7489. scarlett2_phantom_update_access(mixer);
  7490. }
  7491. /* Start handling all notifications */
  7492. private->running = 2;
  7493. return 0;
  7494. }
  7495. /*** hwdep interface ***/
  7496. /* Set private->hwdep_in_use; prevents access to the ALSA controls
  7497. * while doing a config erase/firmware upgrade.
  7498. */
  7499. static void scarlett2_lock(struct scarlett2_data *private)
  7500. {
  7501. mutex_lock(&private->data_mutex);
  7502. private->hwdep_in_use = 1;
  7503. mutex_unlock(&private->data_mutex);
  7504. }
  7505. /* Call SCARLETT2_USB_GET_ERASE to get the erase progress */
  7506. static int scarlett2_get_erase_progress(struct usb_mixer_interface *mixer)
  7507. {
  7508. struct scarlett2_data *private = mixer->private_data;
  7509. int segment_id, segment_num, err;
  7510. u8 erase_resp;
  7511. struct {
  7512. __le32 segment_num;
  7513. __le32 pad;
  7514. } __packed erase_req;
  7515. segment_id = private->selected_flash_segment_id;
  7516. segment_num = private->flash_segment_nums[segment_id];
  7517. if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
  7518. segment_num > SCARLETT2_SEGMENT_NUM_MAX)
  7519. return -EFAULT;
  7520. /* Send the erase progress request */
  7521. erase_req.segment_num = cpu_to_le32(segment_num);
  7522. erase_req.pad = 0;
  7523. err = scarlett2_usb(mixer, SCARLETT2_USB_GET_ERASE,
  7524. &erase_req, sizeof(erase_req),
  7525. &erase_resp, sizeof(erase_resp));
  7526. if (err < 0)
  7527. return err;
  7528. return erase_resp;
  7529. }
  7530. /* Repeatedly call scarlett2_get_erase_progress() until it returns
  7531. * 0xff (erase complete) or we've waited 10 seconds (it usually takes
  7532. * <3 seconds).
  7533. */
  7534. static int scarlett2_wait_for_erase(struct usb_mixer_interface *mixer)
  7535. {
  7536. int i, err;
  7537. for (i = 0; i < 100; i++) {
  7538. err = scarlett2_get_erase_progress(mixer);
  7539. if (err < 0)
  7540. return err;
  7541. if (err == 0xff)
  7542. return 0;
  7543. msleep(100);
  7544. }
  7545. return -ETIMEDOUT;
  7546. }
  7547. /* Reboot the device; wait for the erase to complete if one is in
  7548. * progress.
  7549. */
  7550. static int scarlett2_reboot(struct usb_mixer_interface *mixer)
  7551. {
  7552. struct scarlett2_data *private = mixer->private_data;
  7553. if (private->flash_write_state ==
  7554. SCARLETT2_FLASH_WRITE_STATE_ERASING) {
  7555. int err = scarlett2_wait_for_erase(mixer);
  7556. if (err < 0)
  7557. return err;
  7558. }
  7559. return scarlett2_usb(mixer, SCARLETT2_USB_REBOOT, NULL, 0, NULL, 0);
  7560. }
  7561. /* Select a flash segment for reading/erasing/writing */
  7562. static int scarlett2_ioctl_select_flash_segment(
  7563. struct usb_mixer_interface *mixer,
  7564. unsigned long arg)
  7565. {
  7566. struct scarlett2_data *private = mixer->private_data;
  7567. int segment_id, segment_num;
  7568. if (get_user(segment_id, (int __user *)arg))
  7569. return -EFAULT;
  7570. /* Check the segment ID and segment number */
  7571. if (segment_id < 0 || segment_id >= SCARLETT2_SEGMENT_ID_COUNT)
  7572. return -EINVAL;
  7573. segment_num = private->flash_segment_nums[segment_id];
  7574. if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
  7575. segment_num > SCARLETT2_SEGMENT_NUM_MAX) {
  7576. usb_audio_err(mixer->chip,
  7577. "%s: invalid segment number %d\n",
  7578. __func__, segment_id);
  7579. return -EFAULT;
  7580. }
  7581. /* If erasing, wait for it to complete */
  7582. if (private->flash_write_state == SCARLETT2_FLASH_WRITE_STATE_ERASING) {
  7583. int err = scarlett2_wait_for_erase(mixer);
  7584. if (err < 0)
  7585. return err;
  7586. }
  7587. /* Save the selected segment ID and set the state to SELECTED */
  7588. private->selected_flash_segment_id = segment_id;
  7589. private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_SELECTED;
  7590. return 0;
  7591. }
  7592. /* Erase the previously-selected flash segment */
  7593. static int scarlett2_ioctl_erase_flash_segment(
  7594. struct usb_mixer_interface *mixer)
  7595. {
  7596. struct scarlett2_data *private = mixer->private_data;
  7597. int segment_id, segment_num, err;
  7598. struct {
  7599. __le32 segment_num;
  7600. __le32 pad;
  7601. } __packed erase_req;
  7602. if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_SELECTED)
  7603. return -EINVAL;
  7604. segment_id = private->selected_flash_segment_id;
  7605. segment_num = private->flash_segment_nums[segment_id];
  7606. if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
  7607. segment_num > SCARLETT2_SEGMENT_NUM_MAX)
  7608. return -EFAULT;
  7609. /* Prevent access to ALSA controls that access the device from
  7610. * here on
  7611. */
  7612. scarlett2_lock(private);
  7613. /* Send the erase request */
  7614. erase_req.segment_num = cpu_to_le32(segment_num);
  7615. erase_req.pad = 0;
  7616. err = scarlett2_usb(mixer, SCARLETT2_USB_ERASE_SEGMENT,
  7617. &erase_req, sizeof(erase_req),
  7618. NULL, 0);
  7619. if (err < 0)
  7620. return err;
  7621. /* On success, change the state from SELECTED to ERASING */
  7622. private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_ERASING;
  7623. return 0;
  7624. }
  7625. /* Get the erase progress from the device */
  7626. static int scarlett2_ioctl_get_erase_progress(
  7627. struct usb_mixer_interface *mixer,
  7628. unsigned long arg)
  7629. {
  7630. struct scarlett2_data *private = mixer->private_data;
  7631. struct scarlett2_flash_segment_erase_progress progress;
  7632. int segment_id, segment_num, err;
  7633. u8 erase_resp;
  7634. struct {
  7635. __le32 segment_num;
  7636. __le32 pad;
  7637. } __packed erase_req;
  7638. /* Check that we're erasing */
  7639. if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_ERASING)
  7640. return -EINVAL;
  7641. segment_id = private->selected_flash_segment_id;
  7642. segment_num = private->flash_segment_nums[segment_id];
  7643. if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
  7644. segment_num > SCARLETT2_SEGMENT_NUM_MAX)
  7645. return -EFAULT;
  7646. /* Send the erase progress request */
  7647. erase_req.segment_num = cpu_to_le32(segment_num);
  7648. erase_req.pad = 0;
  7649. err = scarlett2_usb(mixer, SCARLETT2_USB_GET_ERASE,
  7650. &erase_req, sizeof(erase_req),
  7651. &erase_resp, sizeof(erase_resp));
  7652. if (err < 0)
  7653. return err;
  7654. progress.progress = erase_resp;
  7655. progress.num_blocks = private->flash_segment_blocks[segment_id];
  7656. if (copy_to_user((void __user *)arg, &progress, sizeof(progress)))
  7657. return -EFAULT;
  7658. /* If the erase is complete, change the state from ERASING to
  7659. * WRITE.
  7660. */
  7661. if (progress.progress == 0xff)
  7662. private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_WRITE;
  7663. return 0;
  7664. }
  7665. static int scarlett2_hwdep_open(struct snd_hwdep *hw, struct file *file)
  7666. {
  7667. struct usb_mixer_interface *mixer = hw->private_data;
  7668. struct scarlett2_data *private = mixer->private_data;
  7669. /* If erasing, wait for it to complete */
  7670. if (private->flash_write_state ==
  7671. SCARLETT2_FLASH_WRITE_STATE_ERASING) {
  7672. int err = scarlett2_wait_for_erase(mixer);
  7673. if (err < 0)
  7674. return err;
  7675. }
  7676. /* Set the state to IDLE */
  7677. private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_IDLE;
  7678. return 0;
  7679. }
  7680. static int scarlett2_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
  7681. unsigned int cmd, unsigned long arg)
  7682. {
  7683. struct usb_mixer_interface *mixer = hw->private_data;
  7684. switch (cmd) {
  7685. case SCARLETT2_IOCTL_PVERSION:
  7686. return put_user(SCARLETT2_HWDEP_VERSION,
  7687. (int __user *)arg) ? -EFAULT : 0;
  7688. case SCARLETT2_IOCTL_REBOOT:
  7689. return scarlett2_reboot(mixer);
  7690. case SCARLETT2_IOCTL_SELECT_FLASH_SEGMENT:
  7691. return scarlett2_ioctl_select_flash_segment(mixer, arg);
  7692. case SCARLETT2_IOCTL_ERASE_FLASH_SEGMENT:
  7693. return scarlett2_ioctl_erase_flash_segment(mixer);
  7694. case SCARLETT2_IOCTL_GET_ERASE_PROGRESS:
  7695. return scarlett2_ioctl_get_erase_progress(mixer, arg);
  7696. default:
  7697. return -ENOIOCTLCMD;
  7698. }
  7699. }
  7700. static long scarlett2_hwdep_read(struct snd_hwdep *hw,
  7701. char __user *buf,
  7702. long count, loff_t *offset)
  7703. {
  7704. struct usb_mixer_interface *mixer = hw->private_data;
  7705. struct scarlett2_data *private = mixer->private_data;
  7706. int segment_id, segment_num, err;
  7707. int flash_size;
  7708. /* SCARLETT2_USB_READ_SEGMENT request data */
  7709. struct {
  7710. __le32 segment_num;
  7711. __le32 offset;
  7712. __le32 len;
  7713. } __packed req;
  7714. u8 *resp;
  7715. /* Flash segment must first be selected */
  7716. if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_SELECTED)
  7717. return -EINVAL;
  7718. /* Get the selected flash segment number */
  7719. segment_id = private->selected_flash_segment_id;
  7720. if (segment_id < 0 || segment_id >= SCARLETT2_SEGMENT_ID_COUNT)
  7721. return -EINVAL;
  7722. segment_num = private->flash_segment_nums[segment_id];
  7723. if (segment_num < 0 ||
  7724. segment_num > SCARLETT2_SEGMENT_NUM_MAX)
  7725. return -EFAULT;
  7726. /* Validate the offset and count */
  7727. if (count < 0 || *offset < 0)
  7728. return -EINVAL;
  7729. /* Reached EOF? */
  7730. flash_size = private->flash_segment_blocks[segment_id] *
  7731. SCARLETT2_FLASH_BLOCK_SIZE;
  7732. if (!count || *offset >= flash_size)
  7733. return 0;
  7734. /* Limit the numbers of bytes read to SCARLETT2_FLASH_RW_MAX */
  7735. if (count > SCARLETT2_FLASH_RW_MAX)
  7736. count = SCARLETT2_FLASH_RW_MAX;
  7737. /* Limit read to EOF */
  7738. if (*offset + count >= flash_size)
  7739. count = flash_size - *offset;
  7740. /* Create and send the request */
  7741. req.segment_num = cpu_to_le32(segment_num);
  7742. req.offset = cpu_to_le32(*offset);
  7743. req.len = cpu_to_le32(count);
  7744. resp = kzalloc(count, GFP_KERNEL);
  7745. if (!resp)
  7746. return -ENOMEM;
  7747. err = scarlett2_usb(mixer, SCARLETT2_USB_READ_SEGMENT,
  7748. &req, sizeof(req), resp, count);
  7749. if (err < 0)
  7750. goto error;
  7751. /* Copy the response to userspace */
  7752. if (copy_to_user(buf, resp, count)) {
  7753. err = -EFAULT;
  7754. goto error;
  7755. }
  7756. *offset += count;
  7757. err = count;
  7758. error:
  7759. kfree(resp);
  7760. return err;
  7761. }
  7762. static long scarlett2_hwdep_write(struct snd_hwdep *hw,
  7763. const char __user *buf,
  7764. long count, loff_t *offset)
  7765. {
  7766. struct usb_mixer_interface *mixer = hw->private_data;
  7767. struct scarlett2_data *private = mixer->private_data;
  7768. int segment_id, segment_num, err, len;
  7769. int flash_size;
  7770. /* SCARLETT2_USB_WRITE_SEGMENT request data */
  7771. struct {
  7772. __le32 segment_num;
  7773. __le32 offset;
  7774. __le32 pad;
  7775. u8 data[];
  7776. } __packed *req;
  7777. /* Calculate the maximum permitted in data[] */
  7778. const size_t max_data_size = SCARLETT2_FLASH_RW_MAX -
  7779. offsetof(typeof(*req), data);
  7780. /* If erasing, wait for it to complete */
  7781. if (private->flash_write_state ==
  7782. SCARLETT2_FLASH_WRITE_STATE_ERASING) {
  7783. err = scarlett2_wait_for_erase(mixer);
  7784. if (err < 0)
  7785. return err;
  7786. private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_WRITE;
  7787. /* Check that an erase has been done & completed */
  7788. } else if (private->flash_write_state !=
  7789. SCARLETT2_FLASH_WRITE_STATE_WRITE) {
  7790. return -EINVAL;
  7791. }
  7792. /* Check that we're writing to the upgrade firmware */
  7793. segment_id = private->selected_flash_segment_id;
  7794. if (segment_id != SCARLETT2_SEGMENT_ID_FIRMWARE)
  7795. return -EINVAL;
  7796. segment_num = private->flash_segment_nums[segment_id];
  7797. if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
  7798. segment_num > SCARLETT2_SEGMENT_NUM_MAX)
  7799. return -EFAULT;
  7800. /* Validate the offset and count */
  7801. flash_size = private->flash_segment_blocks[segment_id] *
  7802. SCARLETT2_FLASH_BLOCK_SIZE;
  7803. if (count < 0 || *offset < 0 || *offset + count >= flash_size)
  7804. return -EINVAL;
  7805. if (!count)
  7806. return 0;
  7807. /* Limit the *req size to SCARLETT2_FLASH_RW_MAX */
  7808. if (count > max_data_size)
  7809. count = max_data_size;
  7810. /* Create and send the request */
  7811. len = struct_size(req, data, count);
  7812. req = kzalloc(len, GFP_KERNEL);
  7813. if (!req)
  7814. return -ENOMEM;
  7815. req->segment_num = cpu_to_le32(segment_num);
  7816. req->offset = cpu_to_le32(*offset);
  7817. req->pad = 0;
  7818. if (copy_from_user(req->data, buf, count)) {
  7819. err = -EFAULT;
  7820. goto error;
  7821. }
  7822. err = scarlett2_usb(mixer, SCARLETT2_USB_WRITE_SEGMENT,
  7823. req, len, NULL, 0);
  7824. if (err < 0)
  7825. goto error;
  7826. *offset += count;
  7827. err = count;
  7828. error:
  7829. kfree(req);
  7830. return err;
  7831. }
  7832. static int scarlett2_hwdep_release(struct snd_hwdep *hw, struct file *file)
  7833. {
  7834. struct usb_mixer_interface *mixer = hw->private_data;
  7835. struct scarlett2_data *private = mixer->private_data;
  7836. /* Return from the SELECTED or WRITE state to IDLE.
  7837. * The ERASING state is left as-is, and checked on next open.
  7838. */
  7839. if (private &&
  7840. private->hwdep_in_use &&
  7841. private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_ERASING)
  7842. private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_IDLE;
  7843. return 0;
  7844. }
  7845. static int scarlett2_hwdep_init(struct usb_mixer_interface *mixer)
  7846. {
  7847. struct snd_hwdep *hw;
  7848. int err;
  7849. err = snd_hwdep_new(mixer->chip->card, "Focusrite Control", 0, &hw);
  7850. if (err < 0)
  7851. return err;
  7852. hw->private_data = mixer;
  7853. hw->exclusive = 1;
  7854. hw->ops.open = scarlett2_hwdep_open;
  7855. hw->ops.ioctl = scarlett2_hwdep_ioctl;
  7856. hw->ops.read = scarlett2_hwdep_read;
  7857. hw->ops.write = scarlett2_hwdep_write;
  7858. hw->ops.release = scarlett2_hwdep_release;
  7859. return 0;
  7860. }
  7861. int snd_scarlett2_init(struct usb_mixer_interface *mixer)
  7862. {
  7863. struct snd_usb_audio *chip = mixer->chip;
  7864. const struct scarlett2_device_entry *entry;
  7865. int err;
  7866. /* only use UAC_VERSION_2 */
  7867. if (!mixer->protocol)
  7868. return 0;
  7869. /* find entry in scarlett2_devices */
  7870. entry = get_scarlett2_device_entry(mixer);
  7871. if (!entry) {
  7872. usb_audio_err(mixer->chip,
  7873. "%s: missing device entry for %04x:%04x\n",
  7874. __func__,
  7875. USB_ID_VENDOR(chip->usb_id),
  7876. USB_ID_PRODUCT(chip->usb_id));
  7877. return 0;
  7878. }
  7879. if (chip->setup & SCARLETT2_DISABLE) {
  7880. usb_audio_info(chip,
  7881. "Focusrite %s Mixer Driver disabled "
  7882. "by modprobe options (snd_usb_audio "
  7883. "vid=0x%04x pid=0x%04x device_setup=%d)\n",
  7884. entry->series_name,
  7885. USB_ID_VENDOR(chip->usb_id),
  7886. USB_ID_PRODUCT(chip->usb_id),
  7887. SCARLETT2_DISABLE);
  7888. return 0;
  7889. }
  7890. usb_audio_info(chip,
  7891. "Focusrite %s Mixer Driver enabled (pid=0x%04x); "
  7892. "report any issues to "
  7893. "https://github.com/geoffreybennett/scarlett-gen2/issues",
  7894. entry->series_name,
  7895. USB_ID_PRODUCT(chip->usb_id));
  7896. err = snd_scarlett2_controls_create(mixer, entry);
  7897. if (err < 0) {
  7898. usb_audio_err(mixer->chip,
  7899. "Error initialising %s Mixer Driver: %d",
  7900. entry->series_name,
  7901. err);
  7902. return err;
  7903. }
  7904. err = scarlett2_hwdep_init(mixer);
  7905. if (err < 0)
  7906. usb_audio_err(mixer->chip,
  7907. "Error creating %s hwdep device: %d",
  7908. entry->series_name,
  7909. err);
  7910. return err;
  7911. }