Contributors: 19
Author Tokens Token Proportion Commits Commit Proportion
Geoffrey D. Bennett 40470 93.27% 127 74.27%
Daniel Mack 1991 4.59% 8 4.68%
Jaroslav Kysela 303 0.70% 3 1.75%
Takashi Iwai 274 0.63% 14 8.19%
Christian Colglazier 152 0.35% 1 0.58%
Clemens Ladisch 75 0.17% 4 2.34%
Gustavo A. R. Silva 24 0.06% 1 0.58%
Nathan Chancellor 22 0.05% 2 1.17%
Felix Homann 21 0.05% 1 0.58%
Mark Hills 15 0.03% 1 0.58%
zhujun2 9 0.02% 1 0.58%
Chris J Arges 8 0.02% 1 0.58%
Jussi Laako 7 0.02% 1 0.58%
Przemek Rudy 6 0.01% 1 0.58%
Eldad Zack 6 0.01% 1 0.58%
Oliver Neukum 4 0.01% 1 0.58%
Stephen Rothwell 3 0.01% 1 0.58%
Joe Perches 1 0.00% 1 0.58%
kbuild test robot 1 0.00% 1 0.58%
Total 43392 171

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651
// SPDX-License-Identifier: GPL-2.0
/*
 *   Focusrite Scarlett 2 Protocol Driver for ALSA
 *   (including Scarlett 2nd Gen, 3rd Gen, 4th Gen, Clarett USB, and
 *   Clarett+ series products)
 *
 *   Supported models:
 *   - 6i6/18i8/18i20 Gen 2
 *   - Solo/2i2/4i4/8i6/18i8/18i20 Gen 3
 *   - Solo/2i2/4i4 Gen 4
 *   - Clarett 2Pre/4Pre/8Pre USB
 *   - Clarett+ 2Pre/4Pre/8Pre
 *
 *   Copyright (c) 2018-2023 by Geoffrey D. Bennett <g at b4.vu>
 *   Copyright (c) 2020-2021 by Vladimir Sadovnikov <sadko4u@gmail.com>
 *   Copyright (c) 2022 by Christian Colglazier <christian@cacolglazier.com>
 *
 *   Based on the Scarlett (Gen 1) Driver for ALSA:
 *
 *   Copyright (c) 2013 by Tobias Hoffmann
 *   Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
 *   Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
 *   Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
 *
 *   Many codes borrowed from audio.c by
 *     Alan Cox (alan at lxorguk.ukuu.org.uk)
 *     Thomas Sailer (sailer at ife.ee.ethz.ch)
 *
 *   Code cleanup:
 *   David Henningsson <david.henningsson at canonical.com>
 */

/* The protocol was reverse engineered by looking at the communication
 * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20
 * (firmware 1083) using usbmon in July-August 2018.
 *
 * Scarlett 18i8 support added in April 2019.
 *
 * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
 * for providing usbmon output and testing).
 *
 * Scarlett 4i4/8i6 Gen 3 support added in May 2020 (thanks to Laurent
 * Debricon for donating a 4i4 and to Fredrik Unger for providing 8i6
 * usbmon output and testing).
 *
 * Scarlett 18i8/18i20 Gen 3 support added in June 2020 (thanks to
 * Darren Jaeckel, Alex Sedlack, and Clovis Lunel for providing usbmon
 * output, protocol traces and testing).
 *
 * Support for loading mixer volume and mux configuration from the
 * interface during driver initialisation added in May 2021 (thanks to
 * Vladimir Sadovnikov for figuring out how).
 *
 * Support for Solo/2i2 Gen 3 added in May 2021 (thanks to Alexander
 * Vorona for 2i2 protocol traces).
 *
 * Support for phantom power, direct monitoring, speaker switching,
 * and talkback added in May-June 2021.
 *
 * Support for Clarett+ 8Pre added in Aug 2022 by Christian
 * Colglazier.
 *
 * Support for Clarett 8Pre USB added in Sep 2023 (thanks to Philippe
 * Perrot for confirmation).
 *
 * Support for Clarett+ 4Pre and 2Pre added in Sep 2023 (thanks to
 * Gregory Rozzo for donating a 4Pre, and David Sherwood and Patrice
 * Peterson for usbmon output).
 *
 * Support for Clarett 2Pre and 4Pre USB added in Oct 2023.
 *
 * Support for firmware updates added in Dec 2023.
 *
 * Support for Scarlett Solo/2i2/4i4 Gen 4 added in Dec 2023 (thanks
 * to many LinuxMusicians people and to Focusrite for hardware
 * donations).
 *
 * This ALSA mixer gives access to (model-dependent):
 *  - input, output, mixer-matrix muxes
 *  - mixer-matrix gain stages
 *  - gain/volume/mute controls
 *  - level meters
 *  - line/inst level, pad, and air controls
 *  - phantom power, direct monitor, speaker switching, and talkback
 *    controls
 *  - disable/enable MSD mode
 *  - disable/enable standalone mode
 *  - input mute, gain, autogain, safe mode
 *  - direct monitor mixes
 *  - compressor and EQ
 *  - Bluetooth volume
 *
 * <ditaa>
 *    /--------------\    18chn            20chn     /--------------\
 *    | Hardware  in +--+------\    /-------------+--+ ALSA PCM out |
 *    \--------------/  |      |    |             |  \--------------/
 *                      |      |    |    /-----\  |
 *                      |      |    |    |     |  |
 *                      |      v    v    v     |  |
 *                      |   +---------------+  |  |
 *                      |    \ Matrix  Mux /   |  |
 *                      |     +-----+-----+    |  |
 *                      |           |          |  |
 *                      |           |18chn     |  |
 *                      |           |          |  |
 *                      |           |     10chn|  |
 *                      |           v          |  |
 *                      |     +------------+   |  |
 *                      |     | Mixer      |   |  |
 *                      |     |     Matrix |   |  |
 *                      |     |            |   |  |
 *                      |     | 18x10 Gain |   |  |
 *                      |     |   stages   |   |  |
 *                      |     +-----+------+   |  |
 *                      |           |          |  |
 *                      |18chn      |10chn     |  |20chn
 *                      |           |          |  |
 *                      |           +----------/  |
 *                      |           |             |
 *                      v           v             v
 *                      ===========================
 *               +---------------+       +--—------------+
 *                \ Output  Mux /         \ Capture Mux /
 *                 +---+---+---+           +-----+-----+
 *                     |   |                     |
 *                10chn|   |                     |18chn
 *                     |   |                     |
 *  /--------------\   |   |                     |   /--------------\
 *  | S/PDIF, ADAT |<--/   |10chn                \-->| ALSA PCM in  |
 *  | Hardware out |       |                         \--------------/
 *  \--------------/       |
 *                         v
 *                  +-------------+    Software gain per channel.
 *                  | Master Gain |<-- 18i20 only: Switch per channel
 *                  +------+------+    to select HW or SW gain control.
 *                         |
 *                         |10chn
 *  /--------------\       |
 *  | Analogue     |<------/
 *  | Hardware out |
 *  \--------------/
 * </ditaa>
 *
 * Gen 3/4 devices have a Mass Storage Device (MSD) mode where a small
 * disk with registration and driver download information is presented
 * to the host. To access the full functionality of the device without
 * proprietary software, MSD mode can be disabled by:
 * - holding down the 48V button for five seconds while powering on
 *   the device, or
 * - using this driver and alsamixer to change the "MSD Mode" setting
 *   to Off and power-cycling the device
 */

#include <linux/slab.h>
#include <linux/usb.h>
#include <linux/moduleparam.h>

#include <sound/control.h>
#include <sound/tlv.h>
#include <sound/hwdep.h>

#include <uapi/sound/scarlett2.h>

#include "usbaudio.h"
#include "mixer.h"
#include "helper.h"

#include "mixer_scarlett2.h"

/* device_setup value to allow turning MSD mode back on */
#define SCARLETT2_MSD_ENABLE 0x02

/* device_setup value to disable this mixer driver */
#define SCARLETT2_DISABLE 0x04

/* some gui mixers can't handle negative ctl values */
#define SCARLETT2_VOLUME_BIAS 127

/* maximum preamp input gain value
 * (the corresponding value in dB is per-device)
 */
#define SCARLETT2_MAX_GAIN_VALUE 70

/* maximum Bluetooth volume value */
#define SCARLETT2_MAX_BLUETOOTH_VOLUME 30

/* mixer range from -80dB to +12dB in 0.5dB steps */
#define SCARLETT2_MIXER_MIN_DB -80
#define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
#define SCARLETT2_MIXER_MAX_DB 12
#define SCARLETT2_MIXER_MAX_VALUE \
	((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
#define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1)

/* map from (dB + 80) * 2 to mixer value
 * for dB in 0 .. 184: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
 */
static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = {
	0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
	2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
	9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
	23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
	54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
	122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
	244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
	487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
	973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
	1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
	3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
	5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
	9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
	16345, 17313, 18339, 19426, 20577, 21796, 23088, 24456, 25905,
	27440, 29066, 30788, 32612
};

/* Maximum number of analogue outputs */
#define SCARLETT2_ANALOGUE_MAX 10

/* Maximum number of various input controls */
#define SCARLETT2_LEVEL_SWITCH_MAX 2
#define SCARLETT2_PAD_SWITCH_MAX 8
#define SCARLETT2_AIR_SWITCH_MAX 8
#define SCARLETT2_DSP_SWITCH_MAX 2
#define SCARLETT2_INPUT_MUTE_SWITCH_MAX 2
#define SCARLETT2_PHANTOM_SWITCH_MAX 2
#define SCARLETT2_INPUT_GAIN_MAX 2

/* Maximum number of inputs to the mixer */
#define SCARLETT2_INPUT_MIX_MAX 25

/* Maximum number of outputs from the mixer */
#define SCARLETT2_OUTPUT_MIX_MAX 12

/* Maximum number of mixer gain controls */
#define SCARLETT2_MIX_MAX (SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX)

/* Maximum number of direct monitor mixer gain controls
 * 1 (Solo) or 2 (2i2) direct monitor selections (Mono & Stereo)
 * 2 Mix outputs (A/Left & B/Right)
 * 4 Mix inputs
 */
#define SCARLETT2_MONITOR_MIX_MAX (2 * 2 * 4)

/* Maximum size of the data in the USB mux assignment message:
 * 20 inputs, 20 outputs, 25 matrix inputs, 12 spare
 */
#define SCARLETT2_MUX_MAX 77

/* Maximum number of sources (sum of input port counts) */
#define SCARLETT2_MAX_SRCS 52

/* Maximum number of meters (sum of output port counts) */
#define SCARLETT2_MAX_METERS 65

/* Compressor parameter data
 *
 * The compressor parameters are 32-bit fixed point values with 24
 * bits of fraction. Integer values are sufficient for the parameters
 * except for ratio which we can set in 0.5:1 steps.
 */
struct compressor_param {
	const char          *name;
	snd_ctl_elem_type_t  type;
	s32                  min;
	s32                  max;
	int                  scale_bits;
};

/* The available compressor parameters on the Vocaster:
 * - Enable: Off, On
 * - Threshold: -40dB to 0dB
 * - Ratio: 1:1 to 50:1 in 0.5:1 steps
 * - Knee Width: 0dB to 10dB
 * - Attack: 30ms to 127ms
 * - Release: 30ms to 127ms
 * - Makeup Gain: 0dB to 24dB
 */
static const struct compressor_param compressor_params[] = {
	{ "Enable",      SNDRV_CTL_ELEM_TYPE_BOOLEAN,   0,   1,  0 },
	{ "Threshold",   SNDRV_CTL_ELEM_TYPE_INTEGER, -40,   0, 24 },
	{ "Ratio",       SNDRV_CTL_ELEM_TYPE_INTEGER,   2, 100, 23 },
	{ "Knee Width",  SNDRV_CTL_ELEM_TYPE_INTEGER,   0,  10, 24 },
	{ "Attack",      SNDRV_CTL_ELEM_TYPE_INTEGER,  30, 127, 24 },
	{ "Release",     SNDRV_CTL_ELEM_TYPE_INTEGER,  30, 127, 24 },
	{ "Makeup Gain", SNDRV_CTL_ELEM_TYPE_INTEGER,   0,  24, 24 },
};

#define SCARLETT2_COMPRESSOR_PARAM_COUNT ARRAY_SIZE(compressor_params)
#define SCARLETT2_COMPRESSOR_CTLS_MAX \
	(SCARLETT2_COMPRESSOR_PARAM_COUNT * SCARLETT2_DSP_SWITCH_MAX)

/* Maximum number of filter controls */
#define SCARLETT2_PRECOMP_FLT_CTLS_MAX (2 * SCARLETT2_DSP_SWITCH_MAX)
#define SCARLETT2_PEQ_FLT_CTLS_MAX (3 * SCARLETT2_DSP_SWITCH_MAX)

/* Number of biquad filter coefficients */
#define SCARLETT2_BIQUAD_COEFFS 5

/* Maximum number of filter coefficient values */
#define SCARLETT2_PRECOMP_FLT_VALUES_MAX \
	(SCARLETT2_PRECOMP_FLT_CTLS_MAX * SCARLETT2_BIQUAD_COEFFS)
#define SCARLETT2_PEQ_FLT_VALUES_MAX \
	(SCARLETT2_PEQ_FLT_CTLS_MAX * SCARLETT2_BIQUAD_COEFFS)

/* Maximum number of PEQ filter slots */
#define SCARLETT2_PEQ_FLT_SLOTS_MAX 4

/* Hardware port types:
 * - None (no input to mux)
 * - Analogue I/O
 * - S/PDIF I/O
 * - ADAT I/O
 * - Mixer I/O
 * - PCM I/O
 */
enum {
	SCARLETT2_PORT_TYPE_NONE,
	SCARLETT2_PORT_TYPE_ANALOGUE,
	SCARLETT2_PORT_TYPE_SPDIF,
	SCARLETT2_PORT_TYPE_ADAT,
	SCARLETT2_PORT_TYPE_MIX,
	SCARLETT2_PORT_TYPE_PCM,
	SCARLETT2_PORT_TYPE_COUNT
};

/* I/O count of each port type kept in struct scarlett2_ports */
enum {
	SCARLETT2_PORT_IN,
	SCARLETT2_PORT_OUT,
	SCARLETT2_PORT_DIRNS
};

/* Dim/Mute buttons on the 18i20 */
enum {
	SCARLETT2_BUTTON_MUTE,
	SCARLETT2_BUTTON_DIM,
	SCARLETT2_DIM_MUTE_COUNT
};

/* Autogain target values */

#define SCARLETT2_AG_TARGET_MIN (-30)

enum {
	SCARLETT2_AG_HOT_TARGET,
	SCARLETT2_AG_MEAN_TARGET,
	SCARLETT2_AG_PEAK_TARGET,
	SCARLETT2_AG_TARGET_COUNT
};

/* Flash Write State */
enum {
	SCARLETT2_FLASH_WRITE_STATE_IDLE,
	SCARLETT2_FLASH_WRITE_STATE_SELECTED,
	SCARLETT2_FLASH_WRITE_STATE_ERASING,
	SCARLETT2_FLASH_WRITE_STATE_WRITE
};

static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
	"Mute Playback Switch", "Dim Playback Switch"
};

/* The autogain_status is set based on the autogain_switch and
 * raw_autogain_status values.
 *
 * If autogain_switch is set, autogain_status is set to 0 (Running).
 * The other status values are from the raw_autogain_status value + 1.
 */
static const char *const scarlett2_autogain_status_gen4[] = {
	"Running",
	"Success",
	"SuccessDRover",
	"WarnMinGainLimit",
	"FailDRunder",
	"FailMaxGainLimit",
	"FailClipped",
	"Cancelled",
	"Invalid",
	NULL
};

static const char *const scarlett2_autogain_status_vocaster[] = {
	"Running",
	"Success",
	"FailPG",
	"FailRange",
	"WarnMaxCap",
	"WarnMinCap",
	"Cancelled",
	"Invalid",
	NULL
};

/* Power Status Values */
enum {
	SCARLETT2_POWER_STATUS_EXT,
	SCARLETT2_POWER_STATUS_BUS,
	SCARLETT2_POWER_STATUS_FAIL,
	SCARLETT2_POWER_STATUS_COUNT
};

/* Notification callback functions */
struct scarlett2_notification {
	u32 mask;
	void (*func)(struct usb_mixer_interface *mixer);
};

static void scarlett2_notify_ack(struct usb_mixer_interface *mixer);
static void scarlett2_notify_sync(struct usb_mixer_interface *mixer);
static void scarlett2_notify_dim_mute(struct usb_mixer_interface *mixer);
static void scarlett2_notify_monitor(struct usb_mixer_interface *mixer);
static void scarlett2_notify_volume(struct usb_mixer_interface *mixer);
static void scarlett2_notify_input_level(struct usb_mixer_interface *mixer);
static void scarlett2_notify_input_pad(struct usb_mixer_interface *mixer);
static void scarlett2_notify_input_air(struct usb_mixer_interface *mixer);
static void scarlett2_notify_input_dsp(struct usb_mixer_interface *mixer);
static void scarlett2_notify_input_mute(struct usb_mixer_interface *mixer);
static void scarlett2_notify_input_phantom(struct usb_mixer_interface *mixer);
static void scarlett2_notify_input_other(struct usb_mixer_interface *mixer);
static void scarlett2_notify_input_select(struct usb_mixer_interface *mixer);
static void scarlett2_notify_input_gain(struct usb_mixer_interface *mixer);
static void scarlett2_notify_autogain(struct usb_mixer_interface *mixer);
static void scarlett2_notify_input_safe(struct usb_mixer_interface *mixer);
static void scarlett2_notify_monitor_other(struct usb_mixer_interface *mixer);
static void scarlett2_notify_direct_monitor(struct usb_mixer_interface *mixer);
static void scarlett2_notify_power_status(struct usb_mixer_interface *mixer);
static void scarlett2_notify_pcm_input_switch(
					struct usb_mixer_interface *mixer);
static void scarlett2_notify_bluetooth(struct usb_mixer_interface *mixer);

/* Arrays of notification callback functions */

static const struct scarlett2_notification scarlett2_notifications[] = {
	{ 0x00000001, scarlett2_notify_ack },
	{ 0x00000008, scarlett2_notify_sync },
	{ 0x00200000, scarlett2_notify_dim_mute },
	{ 0x00400000, scarlett2_notify_monitor },
	{ 0x00800000, scarlett2_notify_input_other },
	{ 0x01000000, scarlett2_notify_monitor_other },
	{ 0, NULL }
};

static const struct scarlett2_notification scarlett3a_notifications[] = {
	{ 0x00000001, scarlett2_notify_ack },
	{ 0x00800000, scarlett2_notify_input_other },
	{ 0x01000000, scarlett2_notify_direct_monitor },
	{ 0, NULL }
};

static const struct scarlett2_notification vocaster_notifications[] = {
	{ 0x00000001, scarlett2_notify_ack },
	{ 0x00000008, scarlett2_notify_sync },
	{ 0x00200000, scarlett2_notify_input_mute },
	{ 0x00400000, scarlett2_notify_autogain },
	{ 0x04000000, scarlett2_notify_input_dsp },
	{ 0x08000000, scarlett2_notify_input_gain },
	{ 0x10000000, scarlett2_notify_input_phantom },
	{ 0x20000000, scarlett2_notify_bluetooth },
	{ 0, NULL }
};

static const struct scarlett2_notification scarlett4_solo_notifications[] = {
	{ 0x00000001, scarlett2_notify_ack },
	{ 0x00000008, scarlett2_notify_sync },
	{ 0x00400000, scarlett2_notify_input_air },
	{ 0x00800000, scarlett2_notify_direct_monitor },
	{ 0x01000000, scarlett2_notify_input_level },
	{ 0x02000000, scarlett2_notify_input_phantom },
	{ 0x04000000, scarlett2_notify_pcm_input_switch },
	{ 0, NULL }
};

static const struct scarlett2_notification scarlett4_2i2_notifications[] = {
	{ 0x00000001, scarlett2_notify_ack },
	{ 0x00000008, scarlett2_notify_sync },
	{ 0x00200000, scarlett2_notify_input_safe },
	{ 0x00400000, scarlett2_notify_autogain },
	{ 0x00800000, scarlett2_notify_input_air },
	{ 0x01000000, scarlett2_notify_direct_monitor },
	{ 0x02000000, scarlett2_notify_input_select },
	{ 0x04000000, scarlett2_notify_input_level },
	{ 0x08000000, scarlett2_notify_input_phantom },
	{ 0x10000000, NULL }, /* power status, ignored */
	{ 0x40000000, scarlett2_notify_input_gain },
	{ 0x80000000, NULL }, /* power status, ignored */
	{ 0, NULL }
};

static const struct scarlett2_notification scarlett4_4i4_notifications[] = {
	{ 0x00000001, scarlett2_notify_ack },
	{ 0x00000008, scarlett2_notify_sync },
	{ 0x00200000, scarlett2_notify_input_safe },
	{ 0x00400000, scarlett2_notify_autogain },
	{ 0x00800000, scarlett2_notify_input_air },
	{ 0x01000000, scarlett2_notify_input_select },
	{ 0x02000000, scarlett2_notify_input_level },
	{ 0x04000000, scarlett2_notify_input_phantom },
	{ 0x08000000, scarlett2_notify_power_status }, /* power external */
	{ 0x20000000, scarlett2_notify_input_gain },
	{ 0x40000000, scarlett2_notify_power_status }, /* power status */
	{ 0x80000000, scarlett2_notify_volume },
	{ 0, NULL }
};

/* Configuration parameters that can be read and written */
enum {
	SCARLETT2_CONFIG_DIM_MUTE,
	SCARLETT2_CONFIG_LINE_OUT_VOLUME,
	SCARLETT2_CONFIG_MUTE_SWITCH,
	SCARLETT2_CONFIG_SW_HW_SWITCH,
	SCARLETT2_CONFIG_MASTER_VOLUME,
	SCARLETT2_CONFIG_HEADPHONE_VOLUME,
	SCARLETT2_CONFIG_LEVEL_SWITCH,
	SCARLETT2_CONFIG_PAD_SWITCH,
	SCARLETT2_CONFIG_MSD_SWITCH,
	SCARLETT2_CONFIG_AIR_SWITCH,
	SCARLETT2_CONFIG_DSP_SWITCH,
	SCARLETT2_CONFIG_COMPRESSOR_PARAMS,
	SCARLETT2_CONFIG_PRECOMP_FLT_SWITCH,
	SCARLETT2_CONFIG_PRECOMP_FLT_PARAMS,
	SCARLETT2_CONFIG_PEQ_FLT_SWITCH,
	SCARLETT2_CONFIG_PEQ_FLT_PARAMS,
	SCARLETT2_CONFIG_INPUT_MUTE_SWITCH,
	SCARLETT2_CONFIG_STANDALONE_SWITCH,
	SCARLETT2_CONFIG_PHANTOM_SWITCH,
	SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
	SCARLETT2_CONFIG_DIRECT_MONITOR,
	SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
	SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
	SCARLETT2_CONFIG_TALKBACK_MAP,
	SCARLETT2_CONFIG_AUTOGAIN_SWITCH,
	SCARLETT2_CONFIG_AUTOGAIN_STATUS,
	SCARLETT2_CONFIG_AG_HOT_TARGET,
	SCARLETT2_CONFIG_AG_MEAN_TARGET,
	SCARLETT2_CONFIG_AG_PEAK_TARGET,
	SCARLETT2_CONFIG_INPUT_GAIN,
	SCARLETT2_CONFIG_SAFE_SWITCH,
	SCARLETT2_CONFIG_INPUT_SELECT_SWITCH,
	SCARLETT2_CONFIG_INPUT_LINK_SWITCH,
	SCARLETT2_CONFIG_POWER_EXT,
	SCARLETT2_CONFIG_POWER_LOW,
	SCARLETT2_CONFIG_PCM_INPUT_SWITCH,
	SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN,
	SCARLETT2_CONFIG_BLUETOOTH_VOLUME,
	SCARLETT2_CONFIG_SPDIF_MODE,
	SCARLETT2_CONFIG_COUNT
};

/* Autogain target configuration parameters and names */

static const int scarlett2_ag_target_configs[] = {
	[SCARLETT2_AG_HOT_TARGET]  = SCARLETT2_CONFIG_AG_HOT_TARGET,
	[SCARLETT2_AG_MEAN_TARGET] = SCARLETT2_CONFIG_AG_MEAN_TARGET,
	[SCARLETT2_AG_PEAK_TARGET] = SCARLETT2_CONFIG_AG_PEAK_TARGET
};

static const char *const scarlett2_ag_target_names[] = {
	"Hot", "Mean", "Peak"
};

/* Location, size, and activation command number for the configuration
 * parameters. Size is in bits and may be 1, 8, 16, or 32.
 *
 * Vocaster and 4th Gen devices have a parameter buffer to set certain
 * configuration parameters. When pbuf is set, rather than writing to
 * the given offset, the channel and value are written to the
 * parameter buffer and the activate command is sent to the device.
 *
 * Some Gen 4 configuration parameters are written with 0x02 for a
 * desired value of 0x01, and 0x03 for 0x00. These are indicated with
 * mute set to 1. 0x02 and 0x03 are temporary values while the device
 * makes the change and the channel and/or corresponding DSP channel
 * output is muted.
 */
struct scarlett2_config {
	u16 offset;
	u8 size;
	u8 activate;
	u8 pbuf;
	u8 mute;
};

struct scarlett2_config_set {
	const struct scarlett2_notification *notifications;
	u16 param_buf_addr;
	const unsigned int *input_gain_tlv;
	const char *const *autogain_status_texts;
	const struct scarlett2_config items[SCARLETT2_CONFIG_COUNT];
};

/* Input gain TLV dB ranges */

static const DECLARE_TLV_DB_MINMAX(
	db_scale_vocaster_gain, 0, 70 * 100
);

static const DECLARE_TLV_DB_MINMAX(
	db_scale_gen4_gain, 0, 69 * 100
);

/* Gen 2 devices without SW/HW volume switch: 6i6, 18i8 */

static const struct scarlett2_config_set scarlett2_config_set_gen2a = {
	.notifications = scarlett2_notifications,
	.items = {
		[SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
			.offset = 0x34, .size = 16, .activate = 1 },

		[SCARLETT2_CONFIG_MUTE_SWITCH] = {
			.offset = 0x5c, .size = 8, .activate = 1 },

		[SCARLETT2_CONFIG_LEVEL_SWITCH] = {
			.offset = 0x7c, .size = 8, .activate = 7 },

		[SCARLETT2_CONFIG_PAD_SWITCH] = {
			.offset = 0x84, .size = 8, .activate = 8 },

		[SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
			.offset = 0x8d, .size = 8, .activate = 6 },
	}
};

/* Gen 2 devices with SW/HW volume switch: 18i20 */

static const struct scarlett2_config_set scarlett2_config_set_gen2b = {
	.notifications = scarlett2_notifications,
	.items = {
		[SCARLETT2_CONFIG_DIM_MUTE] = {
			.offset = 0x31, .size = 8, .activate = 2 },

		[SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
			.offset = 0x34, .size = 16, .activate = 1 },

		[SCARLETT2_CONFIG_MUTE_SWITCH] = {
			.offset = 0x5c, .size = 8, .activate = 1 },

		[SCARLETT2_CONFIG_SW_HW_SWITCH] = {
			.offset = 0x66, .size = 8, .activate = 3 },

		[SCARLETT2_CONFIG_MASTER_VOLUME] = {
			.offset = 0x76, .size = 16 },

		[SCARLETT2_CONFIG_LEVEL_SWITCH] = {
			.offset = 0x7c, .size = 8, .activate = 7 },

		[SCARLETT2_CONFIG_PAD_SWITCH] = {
			.offset = 0x84, .size = 8, .activate = 8 },

		[SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
			.offset = 0x8d, .size = 8, .activate = 6 },
	}
};

/* Gen 3 devices without a mixer (Solo and 2i2) */
static const struct scarlett2_config_set scarlett2_config_set_gen3a = {
	.notifications = scarlett3a_notifications,
	.items = {
		[SCARLETT2_CONFIG_MSD_SWITCH] = {
			.offset = 0x04, .size = 8, .activate = 6 },

		[SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
			.offset = 0x05, .size = 8, .activate = 6 },

		[SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
			.offset = 0x06, .size = 8, .activate = 3 },

		[SCARLETT2_CONFIG_DIRECT_MONITOR] = {
			.offset = 0x07, .size = 8, .activate = 4 },

		[SCARLETT2_CONFIG_LEVEL_SWITCH] = {
			.offset = 0x08, .size = 1, .activate = 7 },

		[SCARLETT2_CONFIG_AIR_SWITCH] = {
			.offset = 0x09, .size = 1, .activate = 8 },
	}
};

/* Gen 3 devices without SW/HW volume switch: 4i4, 8i6 */
static const struct scarlett2_config_set scarlett2_config_set_gen3b = {
	.notifications = scarlett2_notifications,
	.items = {
		[SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
			.offset = 0x34, .size = 16, .activate = 1 },

		[SCARLETT2_CONFIG_MUTE_SWITCH] = {
			.offset = 0x5c, .size = 8, .activate = 1 },

		[SCARLETT2_CONFIG_LEVEL_SWITCH] = {
			.offset = 0x7c, .size = 8, .activate = 7 },

		[SCARLETT2_CONFIG_PAD_SWITCH] = {
			.offset = 0x84, .size = 8, .activate = 8 },

		[SCARLETT2_CONFIG_AIR_SWITCH] = {
			.offset = 0x8c, .size = 8, .activate = 8 },

		[SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
			.offset = 0x95, .size = 8, .activate = 6 },

		[SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
			.offset = 0x9c, .size = 1, .activate = 8 },

		[SCARLETT2_CONFIG_MSD_SWITCH] = {
			.offset = 0x9d, .size = 8, .activate = 6 },

		[SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
			.offset = 0x9e, .size = 8, .activate = 6 },
	}
};

/* Gen 3 devices with SW/HW volume switch: 18i8, 18i20 */
static const struct scarlett2_config_set scarlett2_config_set_gen3c = {
	.notifications = scarlett2_notifications,
	.items = {
		[SCARLETT2_CONFIG_DIM_MUTE] = {
			.offset = 0x31, .size = 8, .activate = 2 },

		[SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
			.offset = 0x34, .size = 16, .activate = 1 },

		[SCARLETT2_CONFIG_MUTE_SWITCH] = {
			.offset = 0x5c, .size = 8, .activate = 1 },

		[SCARLETT2_CONFIG_SW_HW_SWITCH] = {
			.offset = 0x66, .size = 8, .activate = 3 },

		[SCARLETT2_CONFIG_MASTER_VOLUME] = {
			.offset = 0x76, .size = 16 },

		[SCARLETT2_CONFIG_LEVEL_SWITCH] = {
			.offset = 0x7c, .size = 8, .activate = 7 },

		[SCARLETT2_CONFIG_PAD_SWITCH] = {
			.offset = 0x84, .size = 8, .activate = 8 },

		[SCARLETT2_CONFIG_AIR_SWITCH] = {
			.offset = 0x8c, .size = 8, .activate = 8 },

		[SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
			.offset = 0x95, .size = 8, .activate = 6 },

		[SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
			.offset = 0x9c, .size = 1, .activate = 8 },

		[SCARLETT2_CONFIG_MSD_SWITCH] = {
			.offset = 0x9d, .size = 8, .activate = 6 },

		[SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
			.offset = 0x9e, .size = 8, .activate = 6 },

		[SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH] = {
			.offset = 0x9f, .size = 1, .activate = 10 },

		[SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE] = {
			.offset = 0xa0, .size = 1, .activate = 10 },

		[SCARLETT2_CONFIG_TALKBACK_MAP] = {
			.offset = 0xb0, .size = 16, .activate = 10 },

		[SCARLETT2_CONFIG_SPDIF_MODE] = {
			.offset = 0x94, .size = 8, .activate = 6 },
	}
};

/* Vocaster */
static const struct scarlett2_config_set scarlett2_config_set_vocaster = {
	.notifications = vocaster_notifications,
	.param_buf_addr = 0x1bc,
	.input_gain_tlv = db_scale_vocaster_gain,
	.autogain_status_texts = scarlett2_autogain_status_vocaster,
	.items = {
		[SCARLETT2_CONFIG_MSD_SWITCH] = {
			.offset = 0x9d, .size = 8, .activate = 6 },

		[SCARLETT2_CONFIG_AUTOGAIN_SWITCH] = {
			.offset = 0x1c0, .size = 8, .activate = 19, .pbuf = 1 },

		[SCARLETT2_CONFIG_AUTOGAIN_STATUS] = {
			.offset = 0x1c2, .size = 8, },

		[SCARLETT2_CONFIG_AG_HOT_TARGET] = {
			.offset = 0xc1, .size = 8, .activate = 29, .pbuf = 1 },

		[SCARLETT2_CONFIG_INPUT_GAIN] = {
			.offset = 0x9f, .size = 8, .activate = 21, .pbuf = 1 },

		[SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
			.offset = 0x9c, .size = 1, .activate = 20, .pbuf = 1 },

		[SCARLETT2_CONFIG_DSP_SWITCH] = {
			.offset = 0x1c4, .size = 8, .activate = 22, .pbuf = 1 },

		[SCARLETT2_CONFIG_COMPRESSOR_PARAMS] = {
			.offset = 0x1c8, .size = 32, .activate = 23 },

		[SCARLETT2_CONFIG_PRECOMP_FLT_SWITCH] = {
			.offset = 0x7c, .size = 32, .activate = 27 },

		[SCARLETT2_CONFIG_PRECOMP_FLT_PARAMS] = {
			.offset = 0x200, .size = 32, .activate = 27 },

		[SCARLETT2_CONFIG_PEQ_FLT_SWITCH] = {
			.offset = 0x84, .size = 32, .activate = 27 },

		[SCARLETT2_CONFIG_PEQ_FLT_PARAMS] = {
			.offset = 0x250, .size = 32, .activate = 27 },

		[SCARLETT2_CONFIG_INPUT_MUTE_SWITCH] = {
			.offset = 0x1be, .size = 8, .activate = 17, .pbuf = 1 },

		[SCARLETT2_CONFIG_BLUETOOTH_VOLUME] = {
			.offset = 0xbf, .size = 8, .activate = 28 },
	}
};

/* Solo Gen 4 */
static const struct scarlett2_config_set scarlett2_config_set_gen4_solo = {
	.notifications = scarlett4_solo_notifications,
	.param_buf_addr = 0xd8,
	.items = {
		[SCARLETT2_CONFIG_MSD_SWITCH] = {
			.offset = 0x47, .size = 8, .activate = 4 },

		[SCARLETT2_CONFIG_DIRECT_MONITOR] = {
			.offset = 0x108, .size = 8, .activate = 12, .pbuf = 1 },

		[SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
			.offset = 0x46, .size = 8, .activate = 9, .pbuf = 1,
			.mute = 1 },

		[SCARLETT2_CONFIG_LEVEL_SWITCH] = {
			.offset = 0x3d, .size = 8, .activate = 10, .pbuf = 1,
			.mute = 1 },

		[SCARLETT2_CONFIG_AIR_SWITCH] = {
			.offset = 0x3e, .size = 8, .activate = 11, .pbuf = 1 },

		[SCARLETT2_CONFIG_PCM_INPUT_SWITCH] = {
			.offset = 0x206, .size = 8, .activate = 25, .pbuf = 1 },

		[SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN] = {
			.offset = 0x232, .size = 16, .activate = 26 }
	}
};

/* 2i2 Gen 4 */
static const struct scarlett2_config_set scarlett2_config_set_gen4_2i2 = {
	.notifications = scarlett4_2i2_notifications,
	.param_buf_addr = 0xfc,
	.input_gain_tlv = db_scale_gen4_gain,
	.autogain_status_texts = scarlett2_autogain_status_gen4,
	.items = {
		[SCARLETT2_CONFIG_MSD_SWITCH] = {
			.offset = 0x49, .size = 8, .activate = 4 },

		[SCARLETT2_CONFIG_DIRECT_MONITOR] = {
			.offset = 0x14a, .size = 8, .activate = 16, .pbuf = 1 },

		[SCARLETT2_CONFIG_AUTOGAIN_SWITCH] = {
			.offset = 0x135, .size = 8, .activate = 10, .pbuf = 1 },

		[SCARLETT2_CONFIG_AUTOGAIN_STATUS] = {
			.offset = 0x137, .size = 8 },

		[SCARLETT2_CONFIG_AG_MEAN_TARGET] = {
			.offset = 0x131, .size = 8, .activate = 29, .pbuf = 1 },

		[SCARLETT2_CONFIG_AG_PEAK_TARGET] = {
			.offset = 0x132, .size = 8, .activate = 30, .pbuf = 1 },

		[SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
			.offset = 0x48, .size = 8, .activate = 11, .pbuf = 1,
			.mute = 1 },

		[SCARLETT2_CONFIG_INPUT_GAIN] = {
			.offset = 0x4b, .size = 8, .activate = 12, .pbuf = 1 },

		[SCARLETT2_CONFIG_LEVEL_SWITCH] = {
			.offset = 0x3c, .size = 8, .activate = 13, .pbuf = 1,
			.mute = 1 },

		[SCARLETT2_CONFIG_SAFE_SWITCH] = {
			.offset = 0x147, .size = 8, .activate = 14, .pbuf = 1 },

		[SCARLETT2_CONFIG_AIR_SWITCH] = {
			.offset = 0x3e, .size = 8, .activate = 15, .pbuf = 1 },

		[SCARLETT2_CONFIG_INPUT_SELECT_SWITCH] = {
			.offset = 0x14b, .size = 8, .activate = 17, .pbuf = 1 },

		[SCARLETT2_CONFIG_INPUT_LINK_SWITCH] = {
			.offset = 0x14e, .size = 8, .activate = 18, .pbuf = 1 },

		[SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN] = {
			.offset = 0x2a0, .size = 16, .activate = 36 }
	}
};

/* 4i4 Gen 4 */
static const struct scarlett2_config_set scarlett2_config_set_gen4_4i4 = {
	.notifications = scarlett4_4i4_notifications,
	.param_buf_addr = 0x130,
	.input_gain_tlv = db_scale_gen4_gain,
	.autogain_status_texts = scarlett2_autogain_status_gen4,
	.items = {
		[SCARLETT2_CONFIG_MSD_SWITCH] = {
			.offset = 0x5c, .size = 8, .activate = 4 },

		[SCARLETT2_CONFIG_AUTOGAIN_SWITCH] = {
			.offset = 0x13e, .size = 8, .activate = 10, .pbuf = 1 },

		[SCARLETT2_CONFIG_AUTOGAIN_STATUS] = {
			.offset = 0x140, .size = 8 },

		[SCARLETT2_CONFIG_AG_MEAN_TARGET] = {
			.offset = 0x13a, .size = 8, .activate = 23, .pbuf = 1 },

		[SCARLETT2_CONFIG_AG_PEAK_TARGET] = {
			.offset = 0x13b, .size = 8, .activate = 24, .pbuf = 1 },

		[SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
			.offset = 0x5a, .size = 8, .activate = 11, .pbuf = 1,
			.mute = 1 },

		[SCARLETT2_CONFIG_INPUT_GAIN] = {
			.offset = 0x5e, .size = 8, .activate = 12, .pbuf = 1 },

		[SCARLETT2_CONFIG_LEVEL_SWITCH] = {
			.offset = 0x4e, .size = 8, .activate = 13, .pbuf = 1,
			.mute = 1 },

		[SCARLETT2_CONFIG_SAFE_SWITCH] = {
			.offset = 0x150, .size = 8, .activate = 14, .pbuf = 1 },

		[SCARLETT2_CONFIG_AIR_SWITCH] = {
			.offset = 0x50, .size = 8, .activate = 15, .pbuf = 1 },

		[SCARLETT2_CONFIG_INPUT_SELECT_SWITCH] = {
			.offset = 0x153, .size = 8, .activate = 16, .pbuf = 1 },

		[SCARLETT2_CONFIG_INPUT_LINK_SWITCH] = {
			.offset = 0x156, .size = 8, .activate = 17, .pbuf = 1 },

		[SCARLETT2_CONFIG_MASTER_VOLUME] = {
			.offset = 0x32, .size = 16 },

		[SCARLETT2_CONFIG_HEADPHONE_VOLUME] = {
			.offset = 0x3a, .size = 16 },

		[SCARLETT2_CONFIG_POWER_EXT] = {
			.offset = 0x168, .size = 8 },

		[SCARLETT2_CONFIG_POWER_LOW] = {
			.offset = 0x16d, .size = 8 }
	}
};

/* Clarett USB and Clarett+ devices: 2Pre, 4Pre, 8Pre */
static const struct scarlett2_config_set scarlett2_config_set_clarett = {
	.notifications = scarlett2_notifications,
	.items = {
		[SCARLETT2_CONFIG_DIM_MUTE] = {
			.offset = 0x31, .size = 8, .activate = 2 },

		[SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
			.offset = 0x34, .size = 16, .activate = 1 },

		[SCARLETT2_CONFIG_MUTE_SWITCH] = {
			.offset = 0x5c, .size = 8, .activate = 1 },

		[SCARLETT2_CONFIG_SW_HW_SWITCH] = {
			.offset = 0x66, .size = 8, .activate = 3 },

		[SCARLETT2_CONFIG_MASTER_VOLUME] = {
			.offset = 0x76, .size = 16 },

		[SCARLETT2_CONFIG_LEVEL_SWITCH] = {
			.offset = 0x7c, .size = 8, .activate = 7 },

		[SCARLETT2_CONFIG_AIR_SWITCH] = {
			.offset = 0x95, .size = 8, .activate = 8 },

		[SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
			.offset = 0x8d, .size = 8, .activate = 6 },

		[SCARLETT2_CONFIG_SPDIF_MODE] = {
			.offset = 0x9e, .size = 8, .activate = 4 },
	}
};

/* Description of each hardware port type:
 * - id: hardware ID of this port type
 * - src_descr: printf format string for mux input selections
 * - src_num_offset: added to channel number for the fprintf
 * - dst_descr: printf format string for mixer controls
 */
struct scarlett2_port {
	u16 id;
	const char * const src_descr;
	int src_num_offset;
	const char * const dst_descr;
	const char * const dsp_src_descr;
	const char * const dsp_dst_descr;
};

static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
	[SCARLETT2_PORT_TYPE_NONE] = {
		.id = 0x000,
		.src_descr = "Off"
	},
	[SCARLETT2_PORT_TYPE_ANALOGUE] = {
		.id = 0x080,
		.src_descr = "Analogue %d",
		.src_num_offset = 1,
		.dst_descr = "Analogue Output %02d Playback"
	},
	[SCARLETT2_PORT_TYPE_SPDIF] = {
		.id = 0x180,
		.src_descr = "S/PDIF %d",
		.src_num_offset = 1,
		.dst_descr = "S/PDIF Output %d Playback"
	},
	[SCARLETT2_PORT_TYPE_ADAT] = {
		.id = 0x200,
		.src_descr = "ADAT %d",
		.src_num_offset = 1,
		.dst_descr = "ADAT Output %d Playback"
	},
	[SCARLETT2_PORT_TYPE_MIX] = {
		.id = 0x300,
		.src_descr = "Mix %c",
		.src_num_offset = 'A',
		.dst_descr = "Mixer Input %02d Capture",
		.dsp_src_descr = "DSP %d",
		.dsp_dst_descr = "DSP Input %d Capture"
	},
	[SCARLETT2_PORT_TYPE_PCM] = {
		.id = 0x600,
		.src_descr = "PCM %d",
		.src_num_offset = 1,
		.dst_descr = "PCM %02d Capture"
	},
};

/* Number of mux tables: one for each band of sample rates
 * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
 */
#define SCARLETT2_MUX_TABLES 3

/* Maximum number of entries in a mux table */
#define SCARLETT2_MAX_MUX_ENTRIES 10

/* One entry within mux_assignment defines the port type and range of
 * ports to add to the set_mux message. The end of the list is marked
 * with count == 0.
 */
struct scarlett2_mux_entry {
	u8 port_type;
	u8 start;
	u8 count;
};

/* Maximum number of entries in a mux table */
#define SCARLETT2_MAX_METER_ENTRIES 9

/* One entry within meter_assignment defines the range of mux outputs
 * that consecutive meter entries are mapped to. The end of the list
 * is marked with count == 0.
 */
struct scarlett2_meter_entry {
	u8 start;
	u8 count;
};

struct scarlett2_device_info {
	/* which set of configuration parameters the device uses */
	const struct scarlett2_config_set *config_set;

	/* minimum firmware version required */
	u16 min_firmware_version;

	/* support for main/alt speaker switching */
	u8 has_speaker_switching;

	/* support for talkback microphone */
	u8 has_talkback;

	/* the number of analogue inputs with a software switchable
	 * level control that can be set to line or instrument
	 */
	u8 level_input_count;

	/* the first input with a level control (0-based) */
	u8 level_input_first;

	/* the number of analogue inputs with a software switchable
	 * 10dB pad control
	 */
	u8 pad_input_count;

	/* the number of analogue inputs with a software switchable
	 * "air" control
	 */
	u8 air_input_count;

	/* the first input with an air control (0-based) */
	u8 air_input_first;

	/* number of additional air options
	 * 0 for air presence only (Gen 3)
	 * 1 for air presence+drive (Gen 4)
	 */
	u8 air_option;

	/* the number of analogue inputs with DSP control */
	u8 dsp_input_count;

	/* number of pre-compressor filters */
	u8 precomp_flt_count;

	/* number of parametric EQ filters */
	u8 peq_flt_count;

	/* number of PEQ filters plus unused slots */
	u8 peq_flt_total_count;

	/* the number of analogue inputs with a software switchable
	 * mute control
	 */
	u8 mute_input_count;

	/* the number of phantom (48V) software switchable controls */
	u8 phantom_count;

	/* the first input with phantom power control (0-based) */
	u8 phantom_first;

	/* the number of inputs each phantom switch controls */
	u8 inputs_per_phantom;

	/* the number of inputs with software-controllable gain */
	u8 gain_input_count;

	/* the number of inputs with safe mode */
	u8 safe_input_count;

	/* the number of direct monitor options
	 * (0 = none, 1 = mono only, 2 = mono/stereo)
	 */
	u8 direct_monitor;

	/* the number of DSP channels */
	u8 dsp_count;

	/* has a Bluetooth module with volume control */
	u8 has_bluetooth;

	/* S/PDIF Source/Digital I/O mode control */
	const char * const spdif_mode_control_name;
	const u8 *spdif_mode_values;
	const char * const *spdif_mode_texts;

	/* remap analogue outputs; 18i8 Gen 3 has "line 3/4" connected
	 * internally to the analogue 7/8 outputs
	 */
	u8 line_out_remap_enable;
	u8 line_out_remap[SCARLETT2_ANALOGUE_MAX];
	u8 line_out_unmap[SCARLETT2_ANALOGUE_MAX];

	/* additional description for the line out volume controls */
	const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];

	/* number of sources/destinations of each port type */
	const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];

	/* layout/order of the entries in the set_mux message */
	struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES]
						 [SCARLETT2_MAX_MUX_ENTRIES];

	/* map from meter level order returned by
	 * SCARLETT2_USB_GET_METER to index into mux[] entries (same
	 * as the order returned by scarlett2_meter_ctl_get())
	 */
	struct scarlett2_meter_entry meter_map[SCARLETT2_MAX_METER_ENTRIES];
};

struct scarlett2_data {
	struct usb_mixer_interface *mixer;
	struct mutex usb_mutex; /* prevent sending concurrent USB requests */
	struct completion cmd_done;
	struct mutex data_mutex; /* lock access to this data */
	u8 running;
	u8 hwdep_in_use;
	u8 selected_flash_segment_id;
	u8 flash_write_state;
	struct delayed_work work;
	const struct scarlett2_device_info *info;
	const struct scarlett2_config_set *config_set;
	const char *series_name;
	__u8 bInterfaceNumber;
	__u8 bEndpointAddress;
	__u16 wMaxPacketSize;
	__u8 bInterval;
	u8 num_mux_srcs;
	u8 num_mux_dsts;
	u8 num_mix_in;
	u8 num_mix_out;
	u8 num_line_out;
	u8 num_monitor_mix_ctls;
	u8 num_autogain_status_texts;
	u32 firmware_version;
	u8 flash_segment_nums[SCARLETT2_SEGMENT_ID_COUNT];
	u8 flash_segment_blocks[SCARLETT2_SEGMENT_ID_COUNT];
	u16 scarlett2_seq;
	u8 sync_updated;
	u8 vol_updated;
	u8 dim_mute_updated;
	u8 input_level_updated;
	u8 input_pad_updated;
	u8 input_air_updated;
	u8 input_dsp_updated;
	u8 input_mute_updated;
	u8 input_phantom_updated;
	u8 input_select_updated;
	u8 input_gain_updated;
	u8 autogain_updated;
	u8 input_safe_updated;
	u8 pcm_input_switch_updated;
	u8 monitor_other_updated;
	u8 direct_monitor_updated;
	u8 mux_updated;
	u8 mix_updated;
	u8 speaker_switching_switched;
	u8 power_status_updated;
	u8 bluetooth_updated;
	u8 sync;
	u8 master_vol;
	u8 headphone_vol;
	u8 vol[SCARLETT2_ANALOGUE_MAX];
	u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
	u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
	u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
	u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
	u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
	u8 air_switch[SCARLETT2_AIR_SWITCH_MAX];
	u8 dsp_switch[SCARLETT2_DSP_SWITCH_MAX];
	s32 compressor_values[SCARLETT2_COMPRESSOR_CTLS_MAX];
	s32 precomp_flt_values[SCARLETT2_PRECOMP_FLT_VALUES_MAX];
	s32 peq_flt_values[SCARLETT2_PEQ_FLT_VALUES_MAX];
	u8 precomp_flt_switch[SCARLETT2_DSP_SWITCH_MAX];
	u8 peq_flt_switch[SCARLETT2_DSP_SWITCH_MAX];
	u8 input_mute_switch[SCARLETT2_INPUT_MUTE_SWITCH_MAX];
	u8 phantom_switch[SCARLETT2_PHANTOM_SWITCH_MAX];
	u8 phantom_persistence;
	u8 input_select_switch;
	u8 input_link_switch[SCARLETT2_INPUT_GAIN_MAX / 2];
	u8 gain[SCARLETT2_INPUT_GAIN_MAX];
	u8 autogain_switch[SCARLETT2_INPUT_GAIN_MAX];
	u8 autogain_status[SCARLETT2_INPUT_GAIN_MAX];
	s8 ag_targets[SCARLETT2_AG_TARGET_COUNT];
	u8 safe_switch[SCARLETT2_INPUT_GAIN_MAX];
	u8 pcm_input_switch;
	u8 direct_monitor_switch;
	u8 speaker_switching_switch;
	u8 talkback_switch;
	u8 talkback_map[SCARLETT2_OUTPUT_MIX_MAX];
	u8 msd_switch;
	u8 standalone_switch;
	u8 power_status;
	u8 bluetooth_volume;
	u8 spdif_mode;
	u8 meter_level_map[SCARLETT2_MAX_METERS];
	struct snd_kcontrol *sync_ctl;
	struct snd_kcontrol *master_vol_ctl;
	struct snd_kcontrol *headphone_vol_ctl;
	struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
	struct snd_kcontrol *sw_hw_ctls[SCARLETT2_ANALOGUE_MAX];
	struct snd_kcontrol *mute_ctls[SCARLETT2_ANALOGUE_MAX];
	struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT];
	struct snd_kcontrol *level_ctls[SCARLETT2_LEVEL_SWITCH_MAX];
	struct snd_kcontrol *pad_ctls[SCARLETT2_PAD_SWITCH_MAX];
	struct snd_kcontrol *air_ctls[SCARLETT2_AIR_SWITCH_MAX];
	struct snd_kcontrol *dsp_ctls[SCARLETT2_DSP_SWITCH_MAX];
	struct snd_kcontrol *input_mute_ctls[SCARLETT2_INPUT_MUTE_SWITCH_MAX];
	struct snd_kcontrol *phantom_ctls[SCARLETT2_PHANTOM_SWITCH_MAX];
	struct snd_kcontrol *input_select_ctl;
	struct snd_kcontrol *input_link_ctls[SCARLETT2_INPUT_GAIN_MAX / 2];
	struct snd_kcontrol *input_gain_ctls[SCARLETT2_INPUT_GAIN_MAX];
	struct snd_kcontrol *autogain_ctls[SCARLETT2_INPUT_GAIN_MAX];
	struct snd_kcontrol *autogain_status_ctls[SCARLETT2_INPUT_GAIN_MAX];
	struct snd_kcontrol *ag_target_ctls[SCARLETT2_AG_TARGET_COUNT];
	struct snd_kcontrol *safe_ctls[SCARLETT2_INPUT_GAIN_MAX];
	struct snd_kcontrol *pcm_input_switch_ctl;
	struct snd_kcontrol *mux_ctls[SCARLETT2_MUX_MAX];
	struct snd_kcontrol *mix_ctls[SCARLETT2_MIX_MAX];
	struct snd_kcontrol *compressor_ctls[SCARLETT2_COMPRESSOR_CTLS_MAX];
	struct snd_kcontrol *precomp_flt_ctls[SCARLETT2_PRECOMP_FLT_CTLS_MAX];
	struct snd_kcontrol *peq_flt_ctls[SCARLETT2_PEQ_FLT_CTLS_MAX];
	struct snd_kcontrol *precomp_flt_switch_ctls[SCARLETT2_DSP_SWITCH_MAX];
	struct snd_kcontrol *peq_flt_switch_ctls[SCARLETT2_DSP_SWITCH_MAX];
	struct snd_kcontrol *direct_monitor_ctl;
	struct snd_kcontrol *speaker_switching_ctl;
	struct snd_kcontrol *talkback_ctl;
	struct snd_kcontrol *power_status_ctl;
	struct snd_kcontrol *bluetooth_volume_ctl;
	u8 mux[SCARLETT2_MUX_MAX];
	u8 mix[SCARLETT2_MIX_MAX];
	u8 monitor_mix[SCARLETT2_MONITOR_MIX_MAX];
};

/*** Model-specific data ***/

static const struct scarlett2_device_info s6i6_gen2_info = {
	.config_set = &scarlett2_config_set_gen2a,
	.level_input_count = 2,
	.pad_input_count = 2,

	.line_out_descrs = {
		"Headphones 1 L",
		"Headphones 1 R",
		"Headphones 2 L",
		"Headphones 2 R",
	},

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = {  4,  4 },
		[SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
		[SCARLETT2_PORT_TYPE_PCM]      = {  6,  6 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  6 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  6 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  6 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	} },

	.meter_map = {
		{ 24,  6 },
		{  0, 24 },
		{  0,  0 },
	}
};

static const struct scarlett2_device_info s18i8_gen2_info = {
	.config_set = &scarlett2_config_set_gen2a,
	.level_input_count = 2,
	.pad_input_count = 4,

	.line_out_descrs = {
		"Monitor L",
		"Monitor R",
		"Headphones 1 L",
		"Headphones 1 R",
		"Headphones 2 L",
		"Headphones 2 R",
	},

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = {  8,  6 },
		[SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
		[SCARLETT2_PORT_TYPE_ADAT]     = {  8,  0 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
		[SCARLETT2_PORT_TYPE_PCM]      = {  8, 18 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 18 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 14 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 10 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  4 },
		{ 0,                            0,  0 },
	} },

	.meter_map = {
		{ 26, 18 },
		{  0, 26 },
		{  0,  0 },
	}
};

static const struct scarlett2_device_info s18i20_gen2_info = {
	.config_set = &scarlett2_config_set_gen2b,

	.line_out_descrs = {
		"Monitor L",
		"Monitor R",
		NULL,
		NULL,
		NULL,
		NULL,
		"Headphones 1 L",
		"Headphones 1 R",
		"Headphones 2 L",
		"Headphones 2 R",
	},

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = {  8, 10 },
		[SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
		[SCARLETT2_PORT_TYPE_ADAT]     = {  8,  8 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
		[SCARLETT2_PORT_TYPE_PCM]      = { 20, 18 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 18 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_ADAT,     0,  8 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 14 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_ADAT,     0,  4 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 10 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  6 },
		{ 0,                            0,  0 },
	} },

	.meter_map = {
		{ 38, 18 },
		{  0, 38 },
		{  0,  0 },
	}
};

static const struct scarlett2_device_info solo_gen3_info = {
	.config_set = &scarlett2_config_set_gen3a,
	.level_input_count = 1,
	.level_input_first = 1,
	.air_input_count = 1,
	.phantom_count = 1,
	.inputs_per_phantom = 1,
	.direct_monitor = 1,
};

static const struct scarlett2_device_info s2i2_gen3_info = {
	.config_set = &scarlett2_config_set_gen3a,
	.level_input_count = 2,
	.air_input_count = 2,
	.phantom_count = 1,
	.inputs_per_phantom = 2,
	.direct_monitor = 2,
};

static const struct scarlett2_device_info s4i4_gen3_info = {
	.config_set = &scarlett2_config_set_gen3b,
	.level_input_count = 2,
	.pad_input_count = 2,
	.air_input_count = 2,
	.phantom_count = 1,
	.inputs_per_phantom = 2,

	.line_out_descrs = {
		"Monitor L",
		"Monitor R",
		"Headphones L",
		"Headphones R",
	},

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = { 1, 0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 6, 8 },
		[SCARLETT2_PORT_TYPE_PCM]      = { 4, 6 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  6 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_MIX,      0,  8 },
		{ SCARLETT2_PORT_TYPE_NONE,     0, 16 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  6 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_MIX,      0,  8 },
		{ SCARLETT2_PORT_TYPE_NONE,     0, 16 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  6 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_MIX,      0,  8 },
		{ SCARLETT2_PORT_TYPE_NONE,     0, 16 },
		{ 0,                            0,  0 },
	} },

	.meter_map = {
		{ 12,  6 },
		{  0, 12 },
		{  0,  0 },
	}
};

static const struct scarlett2_device_info s8i6_gen3_info = {
	.config_set = &scarlett2_config_set_gen3b,
	.level_input_count = 2,
	.pad_input_count = 2,
	.air_input_count = 2,
	.phantom_count = 1,
	.inputs_per_phantom = 2,

	.line_out_descrs = {
		"Headphones 1 L",
		"Headphones 1 R",
		"Headphones 2 L",
		"Headphones 2 R",
	},

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = { 1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = { 6,  4 },
		[SCARLETT2_PORT_TYPE_SPDIF]    = { 2,  2 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 8,  8 },
		[SCARLETT2_PORT_TYPE_PCM]      = { 6, 10 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  8 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,      8,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0,  8 },
		{ SCARLETT2_PORT_TYPE_NONE,     0, 18 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  8 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,      8,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0,  8 },
		{ SCARLETT2_PORT_TYPE_NONE,     0, 18 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  8 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,      8,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0,  8 },
		{ SCARLETT2_PORT_TYPE_NONE,     0, 18 },
		{ 0,                            0,  0 },
	} },

	.meter_map = {
		{ 14, 8 },
		{  0, 6 },
		{ 22, 2 },
		{  6, 8 },
		{  0, 0 },
	}
};

static const u8 scarlett2_spdif_s18i8_gen3_values[] = { 0, 2, 0xff };

static const char * const scarlett2_spdif_s18i8_gen3_texts[] = {
	"RCA",
	"Optical",
	NULL
};

static const struct scarlett2_device_info s18i8_gen3_info = {
	.config_set = &scarlett2_config_set_gen3c,
	.has_speaker_switching = 1,
	.level_input_count = 2,
	.pad_input_count = 4,
	.air_input_count = 4,
	.phantom_count = 2,
	.inputs_per_phantom = 2,

	.spdif_mode_control_name = "S/PDIF Mode Capture Enum",
	.spdif_mode_values = scarlett2_spdif_s18i8_gen3_values,
	.spdif_mode_texts = scarlett2_spdif_s18i8_gen3_texts,

	.line_out_remap_enable = 1,
	.line_out_remap = { 0, 1, 6, 7, 2, 3, 4, 5 },
	.line_out_unmap = { 0, 1, 4, 5, 6, 7, 2, 3 },

	.line_out_descrs = {
		"Monitor L",
		"Monitor R",
		"Alt Monitor L",
		"Alt Monitor R",
		"Headphones 1 L",
		"Headphones 1 R",
		"Headphones 2 L",
		"Headphones 2 R",
	},

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = {  8,  8 },
		[SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
		[SCARLETT2_PORT_TYPE_ADAT]     = {  8,  0 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 10, 20 },
		[SCARLETT2_PORT_TYPE_PCM]      = {  8, 20 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_PCM,       0, 10 },
		{ SCARLETT2_PORT_TYPE_PCM,      12,  8 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  6,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  2,  4 },
		{ SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,      10,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,       0, 20 },
		{ SCARLETT2_PORT_TYPE_NONE,      0, 10 },
		{ 0,                             0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,       0, 10 },
		{ SCARLETT2_PORT_TYPE_PCM,      12,  4 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  6,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  2,  4 },
		{ SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,      10,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,       0, 20 },
		{ SCARLETT2_PORT_TYPE_NONE,      0, 10 },
		{ 0,                             0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,       0, 10 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  6,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  2,  4 },
		{ SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,       0, 20 },
		{ SCARLETT2_PORT_TYPE_NONE,      0, 10 },
		{ 0,                             0,  0 },
	} },

	.meter_map = {
		{ 30, 10 },
		{ 42,  8 },
		{  0,  2 },
		{  6,  2 },
		{  2,  4 },
		{  8,  2 },
		{ 40,  2 },
		{ 10, 20 },
		{  0,  0 }
	}
};

static const u8 scarlett2_spdif_s18i20_gen3_values[] = { 0, 6, 1, 0xff };

static const char * const scarlett2_spdif_s18i20_gen3_texts[] = {
	"S/PDIF RCA",
	"S/PDIF Optical",
	"Dual ADAT",
	NULL
};

static const struct scarlett2_device_info s18i20_gen3_info = {
	.config_set = &scarlett2_config_set_gen3c,
	.has_speaker_switching = 1,
	.has_talkback = 1,
	.level_input_count = 2,
	.pad_input_count = 8,
	.air_input_count = 8,
	.phantom_count = 2,
	.inputs_per_phantom = 4,

	.spdif_mode_control_name = "Digital I/O Mode Capture Enum",
	.spdif_mode_values = scarlett2_spdif_s18i20_gen3_values,
	.spdif_mode_texts = scarlett2_spdif_s18i20_gen3_texts,

	.line_out_descrs = {
		"Monitor 1 L",
		"Monitor 1 R",
		"Monitor 2 L",
		"Monitor 2 R",
		NULL,
		NULL,
		"Headphones 1 L",
		"Headphones 1 R",
		"Headphones 2 L",
		"Headphones 2 R",
	},

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = {  9, 10 },
		[SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
		[SCARLETT2_PORT_TYPE_ADAT]     = {  8,  8 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 12, 25 },
		[SCARLETT2_PORT_TYPE_PCM]      = { 20, 20 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_PCM,       0,  8 },
		{ SCARLETT2_PORT_TYPE_PCM,      10, 10 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0, 10 },
		{ SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
		{ SCARLETT2_PORT_TYPE_ADAT,      0,  8 },
		{ SCARLETT2_PORT_TYPE_PCM,       8,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,       0, 25 },
		{ SCARLETT2_PORT_TYPE_NONE,      0, 12 },
		{ 0,                             0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,       0,  8 },
		{ SCARLETT2_PORT_TYPE_PCM,      10,  8 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0, 10 },
		{ SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
		{ SCARLETT2_PORT_TYPE_ADAT,      0,  8 },
		{ SCARLETT2_PORT_TYPE_PCM,       8,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,       0, 25 },
		{ SCARLETT2_PORT_TYPE_NONE,      0, 10 },
		{ 0,                             0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,       0, 10 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0, 10 },
		{ SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
		{ SCARLETT2_PORT_TYPE_NONE,      0, 24 },
		{ 0,                             0,  0 },
	} },

	.meter_map = {
		{ 45,  8 },
		{ 55, 10 },
		{  0, 20 },
		{ 53,  2 },
		{ 20, 25 },
		{  0,  0 },
	}
};

static const struct scarlett2_device_info vocaster_one_info = {
	.config_set = &scarlett2_config_set_vocaster,
	.min_firmware_version = 1769,

	.phantom_count = 1,
	.inputs_per_phantom = 1,
	.dsp_count = 1,
	.dsp_input_count = 1,
	.precomp_flt_count = 2,
	.peq_flt_count = 3,
	.peq_flt_total_count = 4,
	.mute_input_count = 1,
	.gain_input_count = 1,

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = { 1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = { 2,  4 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 9,  9 },
		[SCARLETT2_PORT_TYPE_PCM]      = { 4, 10 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_MIX,      8, 1 },
		{ SCARLETT2_PORT_TYPE_PCM,      5, 5 },
		{ SCARLETT2_PORT_TYPE_MIX,      6, 2 },
		{ SCARLETT2_PORT_TYPE_PCM,      0, 5 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 6 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
		{ 0, 0, 0 },
	} },

	.meter_map = {
		{ 12, 1 },
		{ 18, 5 },
		{ 10, 2 },
		{ 13, 5 },
		{  4, 6 },
		{  0, 4 },
		{  0, 0 }
	}
};

static const struct scarlett2_device_info vocaster_two_info = {
	.config_set = &scarlett2_config_set_vocaster,
	.min_firmware_version = 1769,

	.phantom_count = 2,
	.inputs_per_phantom = 1,
	.dsp_count = 2,
	.dsp_input_count = 2,
	.precomp_flt_count = 2,
	.peq_flt_count = 3,
	.peq_flt_total_count = 4,
	.mute_input_count = 2,
	.gain_input_count = 2,
	.has_bluetooth = 1,

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = {  6,  6 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 12, 14 },
		[SCARLETT2_PORT_TYPE_PCM]      = {  4, 14 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_MIX,      12,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,       6,  8 },
		{ SCARLETT2_PORT_TYPE_MIX,      10,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,       0,  6 },
		{ SCARLETT2_PORT_TYPE_MIX,       0, 10 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0,  6 },
		{ 0, 0, 0 },
	} },

	.meter_map = {
		{ 18,  2 },
		{ 26,  8 },
		{ 16,  2 },
		{ 20,  6 },
		{  6, 10 },
		{  0,  6 },
		{  0,  0 }
	}
};

static const struct scarlett2_device_info solo_gen4_info = {
	.config_set = &scarlett2_config_set_gen4_solo,
	.min_firmware_version = 2115,

	.level_input_count = 1,
	.air_input_count = 1,
	.air_input_first = 1,
	.air_option = 1,
	.phantom_count = 1,
	.phantom_first = 1,
	.inputs_per_phantom = 1,
	.direct_monitor = 1,
	.dsp_count = 2,

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = { 1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = { 2,  2 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 8,  6 },
		[SCARLETT2_PORT_TYPE_PCM]      = { 2,  4 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_MIX,       4,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,       2,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,       0,  4 },
		{ SCARLETT2_PORT_TYPE_MIX,       0,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
		{ 0,                             0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_MIX,       4,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,       2,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,       0,  4 },
		{ SCARLETT2_PORT_TYPE_MIX,       0,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
		{ 0,                             0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_MIX,       4,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,       2,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,       0,  4 },
		{ SCARLETT2_PORT_TYPE_MIX,       0,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
		{ 0,                             0,  0 },
	} },

	.meter_map = {
		{  6,  2 },
		{  4,  2 },
		{  8,  4 },
		{  2,  2 },
		{  0,  2 },
		{  0,  0 }
	}
};

static const struct scarlett2_device_info s2i2_gen4_info = {
	.config_set = &scarlett2_config_set_gen4_2i2,
	.min_firmware_version = 2115,

	.level_input_count = 2,
	.air_input_count = 2,
	.air_option = 1,
	.phantom_count = 1,
	.inputs_per_phantom = 2,
	.gain_input_count = 2,
	.safe_input_count = 2,
	.direct_monitor = 2,
	.dsp_count = 2,

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = { 1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = { 2,  2 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 6,  6 },
		[SCARLETT2_PORT_TYPE_PCM]      = { 2,  4 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_MIX,       4,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,       2,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,       0,  4 },
		{ SCARLETT2_PORT_TYPE_MIX,       0,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
		{ 0,                             0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_MIX,       4,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,       2,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,       0,  4 },
		{ SCARLETT2_PORT_TYPE_MIX,       0,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
		{ 0,                             0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_MIX,       4,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,       2,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,       0,  4 },
		{ SCARLETT2_PORT_TYPE_MIX,       0,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
		{ 0,                             0,  0 },
	} },

	.meter_map = {
		{  6,  2 },
		{  4,  2 },
		{  8,  4 },
		{  2,  2 },
		{  0,  2 },
		{  0,  0 }
	}
};

static const struct scarlett2_device_info s4i4_gen4_info = {
	.config_set = &scarlett2_config_set_gen4_4i4,
	.min_firmware_version = 2089,

	.level_input_count = 2,
	.air_input_count = 2,
	.air_option = 1,
	.phantom_count = 2,
	.inputs_per_phantom = 1,
	.gain_input_count = 2,
	.safe_input_count = 2,
	.dsp_count = 2,

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = { 1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = { 4,  6 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 8, 12 },
		[SCARLETT2_PORT_TYPE_PCM]      = { 6,  6 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_MIX,      10,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,       0,  6 },
		{ SCARLETT2_PORT_TYPE_MIX,       0, 10 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0,  6 },
		{ 0,                             0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_MIX,      10,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,       0,  6 },
		{ SCARLETT2_PORT_TYPE_MIX,       0, 10 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0,  6 },
		{ 0,                             0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_MIX,      10,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,       0,  6 },
		{ SCARLETT2_PORT_TYPE_MIX,       0, 10 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0,  6 },
		{ 0,                             0,  0 },
	} },

	.meter_map = {
		{ 16,  8 },
		{  6, 10 },
		{  0,  6 },
		{  0,  0 }
	}
};

static const struct scarlett2_device_info clarett_2pre_info = {
	.config_set = &scarlett2_config_set_clarett,
	.level_input_count = 2,
	.air_input_count = 2,

	.line_out_descrs = {
		"Monitor L",
		"Monitor R",
		"Headphones L",
		"Headphones R",
	},

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = {  2,  4 },
		[SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  0 },
		[SCARLETT2_PORT_TYPE_ADAT]     = {  8,  0 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
		[SCARLETT2_PORT_TYPE_PCM]      = {  4, 12 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 12 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  8 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_NONE,     0, 26 },
		{ 0,                            0,  0 },
	} },

	.meter_map = {
		{ 22, 12 },
		{  0, 22 },
		{  0,  0 }
	}
};

static const u8 scarlett2_spdif_clarett_values[] = { 0, 1, 2, 0xff };

static const char * const scarlett2_spdif_clarett_texts[] = {
	"None",
	"Optical",
	"RCA",
	NULL
};

static const struct scarlett2_device_info clarett_4pre_info = {
	.config_set = &scarlett2_config_set_clarett,
	.level_input_count = 2,
	.air_input_count = 4,

	.spdif_mode_control_name = "S/PDIF Source Capture Enum",
	.spdif_mode_values = scarlett2_spdif_clarett_values,
	.spdif_mode_texts = scarlett2_spdif_clarett_texts,

	.line_out_descrs = {
		"Monitor L",
		"Monitor R",
		"Headphones 1 L",
		"Headphones 1 R",
		"Headphones 2 L",
		"Headphones 2 R",
	},

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = {  8,  6 },
		[SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
		[SCARLETT2_PORT_TYPE_ADAT]     = {  8,  0 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
		[SCARLETT2_PORT_TYPE_PCM]      = {  8, 18 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 18 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 14 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 12 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_NONE,     0, 24 },
		{ 0,                            0,  0 },
	} },

	.meter_map = {
		{ 26, 18 },
		{  0, 26 },
		{  0,  0 }
	}
};

static const struct scarlett2_device_info clarett_8pre_info = {
	.config_set = &scarlett2_config_set_clarett,
	.level_input_count = 2,
	.air_input_count = 8,

	.spdif_mode_control_name = "S/PDIF Source Capture Enum",
	.spdif_mode_values = scarlett2_spdif_clarett_values,
	.spdif_mode_texts = scarlett2_spdif_clarett_texts,

	.line_out_descrs = {
		"Monitor L",
		"Monitor R",
		NULL,
		NULL,
		NULL,
		NULL,
		"Headphones 1 L",
		"Headphones 1 R",
		"Headphones 2 L",
		"Headphones 2 R",
	},

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = {  8, 10 },
		[SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
		[SCARLETT2_PORT_TYPE_ADAT]     = {  8,  8 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
		[SCARLETT2_PORT_TYPE_PCM]      = { 20, 18 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 18 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_ADAT,     0,  8 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 14 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_ADAT,     0,  4 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 12 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_NONE,     0, 22 },
		{ 0,                            0,  0 },
	} },

	.meter_map = {
		{ 38, 18 },
		{  0, 38 },
		{  0,  0 }
	}
};

struct scarlett2_device_entry {
	const u32 usb_id; /* USB device identifier */
	const struct scarlett2_device_info *info;
	const char *series_name;
};

static const struct scarlett2_device_entry scarlett2_devices[] = {
	/* Supported Gen 2 devices */
	{ USB_ID(0x1235, 0x8203), &s6i6_gen2_info, "Scarlett Gen 2" },
	{ USB_ID(0x1235, 0x8204), &s18i8_gen2_info, "Scarlett Gen 2" },
	{ USB_ID(0x1235, 0x8201), &s18i20_gen2_info, "Scarlett Gen 2" },

	/* Supported Gen 3 devices */
	{ USB_ID(0x1235, 0x8211), &solo_gen3_info, "Scarlett Gen 3" },
	{ USB_ID(0x1235, 0x8210), &s2i2_gen3_info, "Scarlett Gen 3" },
	{ USB_ID(0x1235, 0x8212), &s4i4_gen3_info, "Scarlett Gen 3" },
	{ USB_ID(0x1235, 0x8213), &s8i6_gen3_info, "Scarlett Gen 3" },
	{ USB_ID(0x1235, 0x8214), &s18i8_gen3_info, "Scarlett Gen 3" },
	{ USB_ID(0x1235, 0x8215), &s18i20_gen3_info, "Scarlett Gen 3" },

	/* Supported Vocaster devices */
	{ USB_ID(0x1235, 0x8216), &vocaster_one_info, "Vocaster" },
	{ USB_ID(0x1235, 0x8217), &vocaster_two_info, "Vocaster" },

	/* Supported Gen 4 devices */
	{ USB_ID(0x1235, 0x8218), &solo_gen4_info, "Scarlett Gen 4" },
	{ USB_ID(0x1235, 0x8219), &s2i2_gen4_info, "Scarlett Gen 4" },
	{ USB_ID(0x1235, 0x821a), &s4i4_gen4_info, "Scarlett Gen 4" },

	/* Supported Clarett USB/Clarett+ devices */
	{ USB_ID(0x1235, 0x8206), &clarett_2pre_info, "Clarett USB" },
	{ USB_ID(0x1235, 0x8207), &clarett_4pre_info, "Clarett USB" },
	{ USB_ID(0x1235, 0x8208), &clarett_8pre_info, "Clarett USB" },
	{ USB_ID(0x1235, 0x820a), &clarett_2pre_info, "Clarett+" },
	{ USB_ID(0x1235, 0x820b), &clarett_4pre_info, "Clarett+" },
	{ USB_ID(0x1235, 0x820c), &clarett_8pre_info, "Clarett+" },

	/* End of list */
	{ 0, NULL },
};

/* get the starting port index number for a given port type/direction */
static int scarlett2_get_port_start_num(
	const int port_count[][SCARLETT2_PORT_DIRNS],
	int direction, int port_type)
{
	int i, num = 0;

	for (i = 0; i < port_type; i++)
		num += port_count[i][direction];

	return num;
}

/*** USB Interactions ***/

/* Commands for sending/receiving requests/responses */
#define SCARLETT2_USB_CMD_INIT 0
#define SCARLETT2_USB_CMD_REQ  2
#define SCARLETT2_USB_CMD_RESP 3

#define SCARLETT2_USB_INIT_1        0x00000000
#define SCARLETT2_USB_INIT_2        0x00000002
#define SCARLETT2_USB_REBOOT        0x00000003
#define SCARLETT2_USB_GET_METER     0x00001001
#define SCARLETT2_USB_GET_MIX       0x00002001
#define SCARLETT2_USB_SET_MIX       0x00002002
#define SCARLETT2_USB_GET_MUX       0x00003001
#define SCARLETT2_USB_SET_MUX       0x00003002
#define SCARLETT2_USB_INFO_FLASH    0x00004000
#define SCARLETT2_USB_INFO_SEGMENT  0x00004001
#define SCARLETT2_USB_ERASE_SEGMENT 0x00004002
#define SCARLETT2_USB_GET_ERASE     0x00004003
#define SCARLETT2_USB_WRITE_SEGMENT 0x00004004
#define SCARLETT2_USB_READ_SEGMENT  0x00004005
#define SCARLETT2_USB_GET_SYNC      0x00006004
#define SCARLETT2_USB_GET_DATA      0x00800000
#define SCARLETT2_USB_SET_DATA      0x00800001
#define SCARLETT2_USB_DATA_CMD      0x00800002

#define SCARLETT2_USB_CONFIG_SAVE 6

#define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1

#define SCARLETT2_FLASH_BLOCK_SIZE 4096
#define SCARLETT2_FLASH_RW_MAX 1024
#define SCARLETT2_SEGMENT_NUM_MIN 1
#define SCARLETT2_SEGMENT_NUM_MAX 4

#define SCARLETT2_SEGMENT_SETTINGS_NAME "App_Settings"
#define SCARLETT2_SEGMENT_FIRMWARE_NAME "App_Upgrade"

/* proprietary request/response format */
struct scarlett2_usb_packet {
	__le32 cmd;
	__le16 size;
	__le16 seq;
	__le32 error;
	__le32 pad;
	u8 data[];
};

static void scarlett2_fill_request_header(struct scarlett2_data *private,
					  struct scarlett2_usb_packet *req,
					  u32 cmd, u16 req_size)
{
	/* sequence must go up by 1 for each request */
	u16 seq = private->scarlett2_seq++;

	req->cmd = cpu_to_le32(cmd);
	req->size = cpu_to_le16(req_size);
	req->seq = cpu_to_le16(seq);
	req->error = 0;
	req->pad = 0;
}

static int scarlett2_usb_tx(struct usb_device *dev, int interface,
			    void *buf, u16 size)
{
	return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
			SCARLETT2_USB_CMD_REQ,
			USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
			0, interface, buf, size);
}

static int scarlett2_usb_rx(struct usb_device *dev, int interface,
			    u32 usb_req, void *buf, u16 size)
{
	return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
			usb_req,
			USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
			0, interface, buf, size);
}

/* Send a proprietary format request to the Scarlett interface */
static int scarlett2_usb(
	struct usb_mixer_interface *mixer, u32 cmd,
	void *req_data, u16 req_size, void *resp_data, u16 resp_size)
{
	struct scarlett2_data *private = mixer->private_data;
	struct usb_device *dev = mixer->chip->dev;
	struct scarlett2_usb_packet *req, *resp = NULL;
	size_t req_buf_size = struct_size(req, data, req_size);
	size_t resp_buf_size = struct_size(resp, data, resp_size);
	int err;

	req = kmalloc(req_buf_size, GFP_KERNEL);
	if (!req) {
		err = -ENOMEM;
		goto error;
	}

	resp = kmalloc(resp_buf_size, GFP_KERNEL);
	if (!resp) {
		err = -ENOMEM;
		goto error;
	}

	mutex_lock(&private->usb_mutex);

	/* build request message and send it */

	scarlett2_fill_request_header(private, req, cmd, req_size);

	if (req_size)
		memcpy(req->data, req_data, req_size);

	err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
			       req, req_buf_size);

	if (err != req_buf_size) {
		usb_audio_err(
			mixer->chip,
			"%s USB request result cmd %x was %d\n",
			private->series_name, cmd, err);
		err = -EINVAL;
		goto unlock;
	}

	if (!wait_for_completion_timeout(&private->cmd_done,
					 msecs_to_jiffies(1000))) {
		usb_audio_err(
			mixer->chip,
			"%s USB request timed out, cmd %x\n",
			private->series_name, cmd);

		err = -ETIMEDOUT;
		goto unlock;
	}

	/* send a second message to get the response */

	err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
			       SCARLETT2_USB_CMD_RESP,
			       resp, resp_buf_size);

	/* validate the response */

	if (err != resp_buf_size) {

		/* ESHUTDOWN and EPROTO are valid responses to a
		 * reboot request
		 */
		if (cmd == SCARLETT2_USB_REBOOT &&
		    (err == -ESHUTDOWN || err == -EPROTO)) {
			err = 0;
			goto unlock;
		}

		usb_audio_err(
			mixer->chip,
			"%s USB response result cmd %x was %d expected %zu\n",
			private->series_name, cmd, err, resp_buf_size);
		err = -EINVAL;
		goto unlock;
	}

	/* cmd/seq/size should match except when initialising
	 * seq sent = 1, response = 0
	 */
	if (resp->cmd != req->cmd ||
	    (resp->seq != req->seq &&
		(le16_to_cpu(req->seq) != 1 || resp->seq != 0)) ||
	    resp_size != le16_to_cpu(resp->size) ||
	    resp->error ||
	    resp->pad) {
		usb_audio_err(
			mixer->chip,
			"%s USB invalid response; "
			   "cmd tx/rx %d/%d seq %d/%d size %d/%d "
			   "error %d pad %d\n",
			private->series_name,
			le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
			le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
			resp_size, le16_to_cpu(resp->size),
			le32_to_cpu(resp->error),
			le32_to_cpu(resp->pad));
		err = -EINVAL;
		goto unlock;
	}

	if (resp_data && resp_size > 0)
		memcpy(resp_data, resp->data, resp_size);

unlock:
	mutex_unlock(&private->usb_mutex);
error:
	kfree(req);
	kfree(resp);
	return err;
}

/* Send a USB message to get data; result placed in *buf */
static int scarlett2_usb_get(
	struct usb_mixer_interface *mixer,
	int offset, void *buf, int size)
{
	struct {
		__le32 offset;
		__le32 size;
	} __packed req;

	req.offset = cpu_to_le32(offset);
	req.size = cpu_to_le32(size);
	return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
			     &req, sizeof(req), buf, size);
}

/* Return true if the given configuration item is present in the
 * configuration set used by this device.
 */
static int scarlett2_has_config_item(
	struct scarlett2_data *private, int config_item_num)
{
	return !!private->config_set->items[config_item_num].offset;
}

/* Send a USB message to get configuration parameters; result placed in *buf */
static int scarlett2_usb_get_config(
	struct usb_mixer_interface *mixer,
	int config_item_num, int count, void *buf)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_config *config_item =
		&private->config_set->items[config_item_num];
	int size, err, i;
	u8 *buf_8;
	u8 value;

	/* Check that the configuration item is present in the
	 * configuration set used by this device
	 */
	if (!config_item->offset)
		return -EFAULT;

	/* Writes to the parameter buffer are always 1 byte */
	size = config_item->size ? config_item->size : 8;

	/* For byte-sized parameters, retrieve directly into buf */
	if (size >= 8) {
		size = size / 8 * count;
		err = scarlett2_usb_get(mixer, config_item->offset, buf, size);
		if (err < 0)
			return err;
		if (size == 2) {
			u16 *buf_16 = buf;

			for (i = 0; i < count; i++, buf_16++)
				*buf_16 = le16_to_cpu(*(__le16 *)buf_16);
		} else if (size == 4) {
			u32 *buf_32 = buf;

			for (i = 0; i < count; i++, buf_32++)
				*buf_32 = le32_to_cpu(*(__le32 *)buf_32);
		}
		return 0;
	}

	/* For bit-sized parameters, retrieve into value */
	err = scarlett2_usb_get(mixer, config_item->offset, &value, 1);
	if (err < 0)
		return err;

	/* then unpack from value into buf[] */
	buf_8 = buf;
	for (i = 0; i < 8 && i < count; i++, value >>= 1)
		*buf_8++ = value & 1;

	return 0;
}

/* Send a SCARLETT2_USB_SET_DATA command.
 * offset: location in the device's data space
 * size: size in bytes of the value (1, 2, 4)
 */
static int scarlett2_usb_set_data(
	struct usb_mixer_interface *mixer,
	int offset, int size, int value)
{
	struct scarlett2_data *private = mixer->private_data;
	struct {
		__le32 offset;
		__le32 size;
		__le32 value;
	} __packed req;

	req.offset = cpu_to_le32(offset);
	req.size = cpu_to_le32(size);
	req.value = cpu_to_le32(value);
	return scarlett2_usb(private->mixer, SCARLETT2_USB_SET_DATA,
			     &req, sizeof(u32) * 2 + size, NULL, 0);
}

/* Send a SCARLETT2_USB_SET_DATA command with multiple values.
 * offset: location in the device's data space
 * size: size in bytes of each value (1, 2, 4)
 * count: number of values
 */
static int scarlett2_usb_set_data_buf(
	struct usb_mixer_interface *mixer,
	int offset, int size, int count, void *buf)
{
	struct scarlett2_data *private = mixer->private_data;
	int bytes = size * count;
	struct {
		__le32 offset;
		__le32 size;
		u8 data[];
	} __packed *req;
	int err;
	int buf_size = struct_size(req, data, bytes);

	req = kmalloc(buf_size, GFP_KERNEL);
	if (!req)
		return -ENOMEM;

	req->offset = cpu_to_le32(offset);
	req->size = cpu_to_le32(bytes);
	if (size == 1) {
		memcpy(req->data, buf, count);
	} else if (size == 2) {
		u16 *buf_16 = buf;
		int i;

		for (i = 0; i < count; i++)
			((__le16 *)req->data)[i] = cpu_to_le16(buf_16[i]);
	} else {
		u32 *buf_32 = buf;
		int i;

		for (i = 0; i < count; i++)
			((__le32 *)req->data)[i] = cpu_to_le32(buf_32[i]);
	}

	err = scarlett2_usb(private->mixer, SCARLETT2_USB_SET_DATA,
			    req, buf_size, NULL, 0);

	kfree(req);
	return err;
}

/* Send a SCARLETT2_USB_DATA_CMD command.
 * Configuration changes require activation with this after they have
 * been uploaded by a previous SCARLETT2_USB_SET_DATA.
 * The value for activate needed is determined by the configuration
 * item.
 */
static int scarlett2_usb_activate_config(
	struct usb_mixer_interface *mixer, int activate)
{
	__le32 req;

	req = cpu_to_le32(activate);
	return scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
			     &req, sizeof(req), NULL, 0);
}

/* Send USB messages to set a SCARLETT2_CONFIG_* parameter */
static int scarlett2_usb_set_config(
	struct usb_mixer_interface *mixer,
	int config_item_num, int index, int value)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_config_set *config_set = private->config_set;
	const struct scarlett2_config *config_item =
		&config_set->items[config_item_num];
	int offset, size;
	int err;

	/* Check that the configuration item is present in the
	 * configuration set used by this device
	 */
	if (!config_item->offset)
		return -EFAULT;

	/* Write via the parameter buffer? */
	if (config_item->pbuf) {
		if (!config_set->param_buf_addr)
			return -EFAULT;

		/* Place index in param_buf_addr + 1 */
		err = scarlett2_usb_set_data(
			mixer, config_set->param_buf_addr + 1, 1, index);
		if (err < 0)
			return err;

		/* Place value in param_buf_addr */
		err = scarlett2_usb_set_data(
			mixer, config_set->param_buf_addr, 1, value);
		if (err < 0)
			return err;

		/* Activate the write through the parameter buffer */
		return scarlett2_usb_activate_config(
			mixer, config_item->activate);
	}

	/* Direct writes (not via the parameter buffer) need NVRAM
	 * save and support bit-modification
	 */

	/* Cancel any pending NVRAM save */
	cancel_delayed_work_sync(&private->work);

	/* Convert config_item->size in bits to size in bytes and
	 * calculate offset
	 */
	if (config_item->size >= 8) {
		size = config_item->size / 8;
		offset = config_item->offset + index * size;

	/* If updating a bit, retrieve the old value, set/clear the
	 * bit as needed, and update value
	 */
	} else {
		u8 tmp;

		size = 1;
		offset = config_item->offset;

		err = scarlett2_usb_get(mixer, offset, &tmp, 1);
		if (err < 0)
			return err;

		if (value)
			tmp |= (1 << index);
		else
			tmp &= ~(1 << index);

		value = tmp;
	}

	/* Write the new value */
	err = scarlett2_usb_set_data(mixer, offset, size, value);
	if (err < 0)
		return err;

	/* Activate the change */
	err = scarlett2_usb_activate_config(mixer, config_item->activate);
	if (err < 0)
		return err;

	/* Interfaces with parameter buffer writes don't need a
	 * separate save step
	 */
	if (config_set->param_buf_addr)
		return 0;

	/* Schedule the change to be written to NVRAM */
	if (config_item->activate != SCARLETT2_USB_CONFIG_SAVE)
		schedule_delayed_work(&private->work, msecs_to_jiffies(2000));

	return 0;
}

/* Send USB messages to set a SCARLETT2_CONFIG_* parameter with
 * multiple values
 */
static int scarlett2_usb_set_config_buf(
	struct usb_mixer_interface *mixer,
	int config_item_num, int index, int count, void *buf)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_config_set *config_set = private->config_set;
	const struct scarlett2_config *config_item =
		&config_set->items[config_item_num];
	int offset, size;
	int err;

	/* Check that the configuration item is present in the
	 * configuration set used by this device
	 */
	if (!config_item->offset)
		return -EFAULT;

	/* Convert config_item->size in bits to size in bytes and
	 * calculate offset
	 */
	if (config_item->size >= 8) {
		size = config_item->size / 8;
		offset = config_item->offset + index * size;

	/* Bit updates not supported */
	} else {
		return -EFAULT;
	}

	/* Write the new values */
	err = scarlett2_usb_set_data_buf(mixer, offset, size, count, buf);
	if (err < 0)
		return err;

	/* Activate the change */
	return scarlett2_usb_activate_config(mixer, config_item->activate);
}

/* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
static void scarlett2_config_save(struct usb_mixer_interface *mixer)
{
	int err;

	err = scarlett2_usb_activate_config(mixer, SCARLETT2_USB_CONFIG_SAVE);
	if (err < 0)
		usb_audio_err(mixer->chip, "config save failed: %d\n", err);
}

/* Delayed work to save config */
static void scarlett2_config_save_work(struct work_struct *work)
{
	struct scarlett2_data *private =
		container_of(work, struct scarlett2_data, work.work);

	scarlett2_config_save(private->mixer);
}

/* Send a USB message to get sync status; result placed in *sync */
static int scarlett2_usb_get_sync_status(
	struct usb_mixer_interface *mixer,
	u8 *sync)
{
	__le32 data;
	int err;

	err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
			    NULL, 0, &data, sizeof(data));
	if (err < 0)
		return err;

	*sync = !!data;
	return 0;
}

/* Return true if the device has a mixer that we can control */
static int scarlett2_has_mixer(struct scarlett2_data *private)
{
	return !!private->info->mux_assignment[0][0].count;
}

/* Map from mixer value to (db + 80) * 2
 * (reverse of scarlett2_mixer_values[])
 */
static int scarlett2_mixer_value_to_db(int value)
{
	int i;

	for (i = 0; i < SCARLETT2_MIXER_VALUE_COUNT; i++)
		if (scarlett2_mixer_values[i] >= value)
			return i;
	return SCARLETT2_MIXER_MAX_VALUE;
}

/* Send a USB message to get the volumes for all inputs of one mix
 * and put the values into private->mix[]
 */
static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
				 int mix_num)
{
	struct scarlett2_data *private = mixer->private_data;

	int num_mixer_in = private->num_mix_in;
	int err, i, j;

	struct {
		__le16 mix_num;
		__le16 count;
	} __packed req;

	__le16 data[SCARLETT2_INPUT_MIX_MAX];

	req.mix_num = cpu_to_le16(mix_num);
	req.count = cpu_to_le16(num_mixer_in);

	err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
			    &req, sizeof(req),
			    data, num_mixer_in * sizeof(u16));
	if (err < 0)
		return err;

	for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
		private->mix[j] = scarlett2_mixer_value_to_db(
			le16_to_cpu(data[i]));

	return 0;
}

/* Send a USB message to set the volumes for all inputs of one mix
 * (values obtained from private->mix[])
 */
static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
				 int mix_num)
{
	struct scarlett2_data *private = mixer->private_data;

	struct {
		__le16 mix_num;
		__le16 data[SCARLETT2_INPUT_MIX_MAX];
	} __packed req;

	int i, j;
	int num_mixer_in = private->num_mix_in;

	req.mix_num = cpu_to_le16(mix_num);

	for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
		req.data[i] = cpu_to_le16(
			scarlett2_mixer_values[private->mix[j]]
		);

	return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
			     &req, (num_mixer_in + 1) * sizeof(u16),
			     NULL, 0);
}

/* Convert a port number index (per info->port_count) to a hardware ID */
static u32 scarlett2_mux_src_num_to_id(
	const int port_count[][SCARLETT2_PORT_DIRNS], int num)
{
	int port_type;

	for (port_type = 0;
	     port_type < SCARLETT2_PORT_TYPE_COUNT;
	     port_type++) {
		if (num < port_count[port_type][SCARLETT2_PORT_IN])
			return scarlett2_ports[port_type].id | num;
		num -= port_count[port_type][SCARLETT2_PORT_IN];
	}

	/* Oops */
	return 0;
}

/* Convert a hardware ID to a port number index */
static u32 scarlett2_mux_id_to_num(
	const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
{
	int port_type;
	int port_num = 0;

	for (port_type = 0;
	     port_type < SCARLETT2_PORT_TYPE_COUNT;
	     port_type++) {
		int base = scarlett2_ports[port_type].id;
		int count = port_count[port_type][direction];

		if (id >= base && id < base + count)
			return port_num + id - base;
		port_num += count;
	}

	/* Oops */
	return -1;
}

/* Convert one mux entry from the interface and load into private->mux[] */
static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
				       u32 mux_entry)
{
	const struct scarlett2_device_info *info = private->info;
	const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;

	int dst_idx, src_idx;

	dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
					  mux_entry & 0xFFF);
	if (dst_idx < 0)
		return;

	if (dst_idx >= private->num_mux_dsts) {
		usb_audio_err(private->mixer->chip,
			"BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
			mux_entry, dst_idx, private->num_mux_dsts);
		return;
	}

	src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
					  mux_entry >> 12);
	if (src_idx < 0)
		return;

	if (src_idx >= private->num_mux_srcs) {
		usb_audio_err(private->mixer->chip,
			"BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
			mux_entry, src_idx, private->num_mux_srcs);
		return;
	}

	private->mux[dst_idx] = src_idx;
}

/* Update the meter level map
 *
 * The meter level data from the interface (SCARLETT2_USB_GET_METER
 * request) is returned in mux_assignment order, but to avoid exposing
 * that to userspace, scarlett2_meter_ctl_get() rearranges the data
 * into scarlett2_ports order using the meter_level_map[] array which
 * is set up by this function.
 *
 * In addition, the meter level data values returned from the
 * interface are invalid for destinations where:
 *
 * - the source is "Off"; therefore we set those values to zero (map
 *   value of 255)
 *
 * - the source is assigned to a previous (with respect to the
 *   mux_assignment order) destination; therefore we set those values
 *   to the value previously reported for that source
 */
static void scarlett2_update_meter_level_map(struct scarlett2_data *private)
{
	const struct scarlett2_device_info *info = private->info;
	const struct scarlett2_meter_entry *entry;

	/* sources already assigned to a destination
	 * value is 255 for None, otherwise the value of i
	 * (index into array returned by
	 * scarlett2_usb_get_meter_levels())
	 */
	u8 seen_src[SCARLETT2_MAX_SRCS] = { 1 };
	u8 seen_src_value[SCARLETT2_MAX_SRCS] = { 255 };

	/* index in meter_map[] order */
	int i = 0;

	/* go through the meter_map[] entries */
	for (entry = info->meter_map;
	     entry->count;
	     entry++) {

		/* fill in each meter_level_map[] entry */
		int j, mux_idx;

		for (j = 0, mux_idx = entry->start;
		     j < entry->count;
		     i++, j++, mux_idx++) {

			/* convert mux_idx using line_out_unmap[] */
			int map_mux_idx = (
			    info->line_out_remap_enable &&
			    mux_idx < private->num_line_out
			) ? info->line_out_unmap[mux_idx]
			  : mux_idx;

			/* check which source is connected, and if
			 * that source is already connected elsewhere,
			 * use that existing connection's destination
			 * for this meter entry instead
			 */
			int mux_src = private->mux[mux_idx];

			if (!seen_src[mux_src]) {
				seen_src[mux_src] = 1;
				seen_src_value[mux_src] = i;
			}
			private->meter_level_map[map_mux_idx] =
				seen_src_value[mux_src];
		}
	}
}

/* Send USB message to get mux inputs and then populate private->mux[] */
static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	int count = private->num_mux_dsts;
	int err, i;

	struct {
		__le16 num;
		__le16 count;
	} __packed req;

	__le32 data[SCARLETT2_MUX_MAX];

	private->mux_updated = 0;

	req.num = 0;
	req.count = cpu_to_le16(count);

	err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
			    &req, sizeof(req),
			    data, count * sizeof(u32));
	if (err < 0)
		return err;

	for (i = 0; i < count; i++)
		scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));

	scarlett2_update_meter_level_map(private);

	return 0;
}

/* Send USB messages to set mux inputs */
static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
	int table;

	struct {
		__le16 pad;
		__le16 num;
		__le32 data[SCARLETT2_MUX_MAX];
	} __packed req;

	req.pad = 0;

	/* set mux settings for each rate */
	for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
		const struct scarlett2_mux_entry *entry;

		/* i counts over the output array */
		int i = 0, err;

		req.num = cpu_to_le16(table);

		/* loop through each entry */
		for (entry = info->mux_assignment[table];
		     entry->count;
		     entry++) {
			int j;
			int port_type = entry->port_type;
			int port_idx = entry->start;
			int mux_idx = scarlett2_get_port_start_num(port_count,
				SCARLETT2_PORT_OUT, port_type) + port_idx;
			int dst_id = scarlett2_ports[port_type].id + port_idx;

			/* Empty slots */
			if (!dst_id) {
				for (j = 0; j < entry->count; j++)
					req.data[i++] = 0;
				continue;
			}

			/* Non-empty mux slots use the lower 12 bits
			 * for the destination and next 12 bits for
			 * the source
			 */
			for (j = 0; j < entry->count; j++) {
				int src_id = scarlett2_mux_src_num_to_id(
					port_count, private->mux[mux_idx++]);
				req.data[i++] = cpu_to_le32(dst_id |
							    src_id << 12);
				dst_id++;
			}
		}

		err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
				    &req, (i + 1) * sizeof(u32),
				    NULL, 0);
		if (err < 0)
			return err;
	}

	scarlett2_update_meter_level_map(private);

	return 0;
}

/* Send USB message to get meter levels */
static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
					  u16 num_meters, u16 *levels)
{
	struct {
		__le16 pad;
		__le16 num_meters;
		__le32 magic;
	} __packed req;
	__le32 resp[SCARLETT2_MAX_METERS];
	int i, err;

	req.pad = 0;
	req.num_meters = cpu_to_le16(num_meters);
	req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
	err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
			    &req, sizeof(req), resp, num_meters * sizeof(u32));
	if (err < 0)
		return err;

	/* copy, convert to u16 */
	for (i = 0; i < num_meters; i++)
		levels[i] = le32_to_cpu(resp[i]);

	return 0;
}

/* For config items with mute=1, xor bits 0 & 1 together to get the
 * current/next state. This won't have any effect on values which are
 * only ever 0/1.
 */
static uint8_t scarlett2_decode_muteable(uint8_t v)
{
	return (v ^ (v >> 1)) & 1;
}

/*** Control Functions ***/

/* helper function to create a new control */
static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
				 const struct snd_kcontrol_new *ncontrol,
				 int index, int channels, const char *name,
				 struct snd_kcontrol **kctl_return)
{
	struct snd_kcontrol *kctl;
	struct usb_mixer_elem_info *elem;
	int err;

	elem = kzalloc(sizeof(*elem), GFP_KERNEL);
	if (!elem)
		return -ENOMEM;

	/* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
	 * ignores them for resume and other operations.
	 * Also, the head.id field is set to 0, as we don't use this field.
	 */
	elem->head.mixer = mixer;
	elem->control = index;
	elem->head.id = 0;
	elem->channels = channels;
	elem->val_type = USB_MIXER_BESPOKEN;

	kctl = snd_ctl_new1(ncontrol, elem);
	if (!kctl) {
		kfree(elem);
		return -ENOMEM;
	}
	kctl->private_free = snd_usb_mixer_elem_free;

	strscpy(kctl->id.name, name, sizeof(kctl->id.name));

	err = snd_usb_mixer_add_control(&elem->head, kctl);
	if (err < 0)
		return err;

	if (kctl_return)
		*kctl_return = kctl;

	return 0;
}

/*** Firmware Version Control ***/

static int scarlett2_firmware_version_ctl_get(
	struct snd_kcontrol *kctl,
	struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct scarlett2_data *private = elem->head.mixer->private_data;

	ucontrol->value.integer.value[0] = private->firmware_version;

	return 0;
}

static int scarlett2_firmware_version_ctl_info(
	struct snd_kcontrol *kctl,
	struct snd_ctl_elem_info *uinfo)
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;

	return 0;
}

static const struct snd_kcontrol_new scarlett2_firmware_version_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
	.access = SNDRV_CTL_ELEM_ACCESS_READ,
	.name = "",
	.info = scarlett2_firmware_version_ctl_info,
	.get  = scarlett2_firmware_version_ctl_get
};

static int scarlett2_add_firmware_version_ctl(
	struct usb_mixer_interface *mixer)
{
	return scarlett2_add_new_ctl(mixer, &scarlett2_firmware_version_ctl,
				     0, 0, "Firmware Version", NULL);
}

/*** Minimum Firmware Version Control ***/

static int scarlett2_min_firmware_version_ctl_get(
	struct snd_kcontrol *kctl,
	struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct scarlett2_data *private = elem->head.mixer->private_data;

	ucontrol->value.integer.value[0] = private->info->min_firmware_version;

	return 0;
}

static int scarlett2_min_firmware_version_ctl_info(
	struct snd_kcontrol *kctl,
	struct snd_ctl_elem_info *uinfo)
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;

	return 0;
}

static const struct snd_kcontrol_new scarlett2_min_firmware_version_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
	.access = SNDRV_CTL_ELEM_ACCESS_READ,
	.name = "",
	.info = scarlett2_min_firmware_version_ctl_info,
	.get  = scarlett2_min_firmware_version_ctl_get
};

static int scarlett2_add_min_firmware_version_ctl(
	struct usb_mixer_interface *mixer)
{
	return scarlett2_add_new_ctl(mixer, &scarlett2_min_firmware_version_ctl,
				     0, 0, "Minimum Firmware Version", NULL);
}

/*** Sync Control ***/

/* Update sync control after receiving notification that the status
 * has changed
 */
static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;

	private->sync_updated = 0;
	return scarlett2_usb_get_sync_status(mixer, &private->sync);
}

static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
				   struct snd_ctl_elem_info *uinfo)
{
	static const char *texts[2] = {
		"Unlocked", "Locked"
	};
	return snd_ctl_enum_info(uinfo, 1, 2, texts);
}

static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
				  struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->sync_updated) {
		err = scarlett2_update_sync(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.enumerated.item[0] = private->sync;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_sync_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.access = SNDRV_CTL_ELEM_ACCESS_READ,
	.name = "",
	.info = scarlett2_sync_ctl_info,
	.get  = scarlett2_sync_ctl_get
};

static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;

	/* devices without a mixer also don't support reporting sync status */
	if (!scarlett2_has_mixer(private))
		return 0;

	return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
				     0, 1, "Sync Status", &private->sync_ctl);
}

/*** Autogain Switch and Status Controls ***/

/* Forward declarations as phantom power and autogain can disable each other */
static int scarlett2_check_input_phantom_updated(struct usb_mixer_interface *);
static int scarlett2_phantom_is_switching(struct scarlett2_data *, int);

/* Set the access mode of a control to read-only (val = 0) or
 * read-write (val = 1).
 */
static void scarlett2_set_ctl_access(struct snd_kcontrol *kctl, int val)
{
	if (val)
		kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
	else
		kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_WRITE;
}

/* Check if autogain is running on any input */
static int scarlett2_autogain_is_running(struct scarlett2_data *private)
{
	int i;

	/* autogain_status[] is 0 if autogain is running */
	for (i = 0; i < private->info->gain_input_count; i++)
		if (!private->autogain_status[i])
			return 1;

	return 0;
}

static int scarlett2_update_autogain(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int err, i;
	u8 raw_autogain_status[SCARLETT2_INPUT_GAIN_MAX];
	s8 ag_target_values[SCARLETT2_AG_TARGET_COUNT];

	private->autogain_updated = 0;

	if (!info->gain_input_count)
		return 0;

	err = scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_AUTOGAIN_SWITCH,
		info->gain_input_count, private->autogain_switch);
	if (err < 0)
		return err;
	err = scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_AUTOGAIN_STATUS,
		info->gain_input_count, raw_autogain_status);
	if (err < 0)
		return err;

	/* Translate autogain_switch and raw_autogain_status into
	 * autogain_status.
	 *
	 * When autogain_switch[] is set, the status is the first
	 * element in scarlett2_autogain_status_texts[] (Running). The
	 * subsequent elements correspond to the status value from the
	 * device (raw_autogain_status[]) + 1. The last element is
	 * "Invalid", in case the device reports a status outside the
	 * range of scarlett2_autogain_status_texts[].
	 */
	for (i = 0; i < info->gain_input_count; i++)
		if (private->autogain_switch[i])
			private->autogain_status[i] = 0;
		else if (raw_autogain_status[i] <
				private->num_autogain_status_texts - 1)
			private->autogain_status[i] =
				raw_autogain_status[i] + 1;
		else
			private->autogain_status[i] =
				private->num_autogain_status_texts - 1;


	for (int i = 0; i < SCARLETT2_AG_TARGET_COUNT; i++)
		if (scarlett2_has_config_item(private,
					      scarlett2_ag_target_configs[i])) {
			err = scarlett2_usb_get_config(
				mixer, scarlett2_ag_target_configs[i],
				1, &ag_target_values[i]);
			if (err < 0)
				return err;
		}

	/* convert from negative dBFS as used by the device */
	for (int i = 0; i < SCARLETT2_AG_TARGET_COUNT; i++)
		private->ag_targets[i] = -ag_target_values[i];

	return 0;
}

/* Update access mode for controls affected by autogain */
static void scarlett2_autogain_update_access(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int val = !scarlett2_autogain_is_running(private);
	int i;

	if (scarlett2_has_config_item(private,
				      SCARLETT2_CONFIG_INPUT_SELECT_SWITCH))
		scarlett2_set_ctl_access(private->input_select_ctl, val);
	if (scarlett2_has_config_item(private,
				      SCARLETT2_CONFIG_INPUT_LINK_SWITCH))
		for (i = 0; i < info->gain_input_count / 2; i++)
			scarlett2_set_ctl_access(private->input_link_ctls[i],
						 val);
	for (i = 0; i < info->gain_input_count; i++)
		scarlett2_set_ctl_access(private->input_gain_ctls[i], val);
	for (i = 0; i < info->safe_input_count; i++)
		scarlett2_set_ctl_access(private->safe_ctls[i], val);
	for (i = 0; i < info->level_input_count; i++)
		scarlett2_set_ctl_access(private->level_ctls[i], val);
	for (i = 0; i < info->air_input_count; i++)
		scarlett2_set_ctl_access(private->air_ctls[i], val);
	for (i = 0; i < info->mute_input_count; i++)
		scarlett2_set_ctl_access(private->input_mute_ctls[i], val);
	for (i = 0; i < info->phantom_count; i++)
		scarlett2_set_ctl_access(private->phantom_ctls[i], val);
	for (i = 0; i < info->dsp_input_count; i++)
		scarlett2_set_ctl_access(private->dsp_ctls[i], val);

	for (i = 0; i < SCARLETT2_AG_TARGET_COUNT; i++)
		if (scarlett2_has_config_item(private,
					      scarlett2_ag_target_configs[i]))
			scarlett2_set_ctl_access(
				private->ag_target_ctls[i], val);
}

/* Notify of access mode change for all controls read-only while
 * autogain runs.
 */
static void scarlett2_autogain_notify_access(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int i;

	if (scarlett2_has_config_item(private,
				      SCARLETT2_CONFIG_INPUT_SELECT_SWITCH))
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
			       &private->input_select_ctl->id);
	if (scarlett2_has_config_item(private,
				      SCARLETT2_CONFIG_INPUT_LINK_SWITCH))
		for (i = 0; i < info->gain_input_count / 2; i++)
			snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
				       &private->input_link_ctls[i]->id);
	for (i = 0; i < info->gain_input_count; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
			       &private->input_gain_ctls[i]->id);
	for (i = 0; i < info->safe_input_count; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
			       &private->safe_ctls[i]->id);
	for (i = 0; i < info->level_input_count; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
			       &private->level_ctls[i]->id);
	for (i = 0; i < info->air_input_count; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
			       &private->air_ctls[i]->id);
	for (i = 0; i < info->dsp_input_count; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
			       &private->dsp_ctls[i]->id);
	for (i = 0; i < info->mute_input_count; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
			       &private->input_mute_ctls[i]->id);
	for (i = 0; i < info->phantom_count; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
			       &private->phantom_ctls[i]->id);

	for (i = 0; i < SCARLETT2_AG_TARGET_COUNT; i++)
		if (scarlett2_has_config_item(private,
					      scarlett2_ag_target_configs[i]))
			snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
				       &private->ag_target_ctls[i]->id);
}

/* Call scarlett2_update_autogain() and
 * scarlett2_autogain_update_access() if autogain_updated is set.
 */
static int scarlett2_check_autogain_updated(
	struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	int err;

	if (!private->autogain_updated)
		return 0;

	err = scarlett2_update_autogain(mixer);
	if (err < 0)
		return err;

	scarlett2_autogain_update_access(mixer);

	return 0;
}

/* If autogain_updated is set when a *_ctl_put() function for a
 * control that is meant to be read-only while autogain is running,
 * update the autogain status and access mode of affected controls.
 * Return -EPERM if autogain is running.
 */
static int scarlett2_check_put_during_autogain(
	struct usb_mixer_interface *mixer)
{
	int err = scarlett2_check_autogain_updated(mixer);

	if (err < 0)
		return err;

	if (scarlett2_autogain_is_running(mixer->private_data))
		return -EPERM;

	return 0;
}

static int scarlett2_autogain_switch_ctl_info(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err;

	mutex_lock(&private->data_mutex);

	err = scarlett2_check_input_phantom_updated(mixer);
	if (err < 0)
		goto unlock;

	err = snd_ctl_boolean_mono_info(kctl, uinfo);

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_autogain_switch_ctl_get(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_autogain_updated(mixer);
	if (err < 0)
		goto unlock;

	ucontrol->value.enumerated.item[0] =
		private->autogain_switch[elem->control];

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_autogain_status_ctl_get(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_autogain_updated(mixer);
	if (err < 0)
		goto unlock;

	ucontrol->value.enumerated.item[0] =
		private->autogain_status[elem->control];

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_autogain_switch_ctl_put(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int index = elem->control;
	int oval, val, err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_input_phantom_updated(mixer);
	if (err < 0)
		goto unlock;

	if (scarlett2_phantom_is_switching(private, index)) {
		err = -EPERM;
		goto unlock;
	}

	oval = private->autogain_switch[index];
	val = !!ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->autogain_switch[index] = val;

	/* Send switch change to the device */
	err = scarlett2_usb_set_config(
		mixer, SCARLETT2_CONFIG_AUTOGAIN_SWITCH, index, val);
	if (err == 0)
		err = 1;

	scarlett2_autogain_update_access(mixer);
	scarlett2_autogain_notify_access(mixer);

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_autogain_status_ctl_info(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct scarlett2_data *private = elem->head.mixer->private_data;

	return snd_ctl_enum_info(
		uinfo, 1,
		private->num_autogain_status_texts,
		private->config_set->autogain_status_texts);
}

static const struct snd_kcontrol_new scarlett2_autogain_switch_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = scarlett2_autogain_switch_ctl_info,
	.get  = scarlett2_autogain_switch_ctl_get,
	.put  = scarlett2_autogain_switch_ctl_put
};

static const struct snd_kcontrol_new scarlett2_autogain_status_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.access = SNDRV_CTL_ELEM_ACCESS_READ,
	.name = "",
	.info = scarlett2_autogain_status_ctl_info,
	.get  = scarlett2_autogain_status_ctl_get,
};

/*** Autogain Target Controls ***/

static int scarlett2_ag_target_ctl_info(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_autogain_updated(mixer);
	if (err < 0)
		goto unlock;

	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = SCARLETT2_AG_TARGET_MIN;
	uinfo->value.integer.max = 0;
	uinfo->value.integer.step = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_ag_target_ctl_get(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->autogain_updated) {
		err = scarlett2_update_autogain(mixer);
		if (err < 0)
			goto unlock;
	}

	ucontrol->value.integer.value[0] = private->ag_targets[elem->control];

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_ag_target_ctl_put(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int index = elem->control;
	int oval, val, err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_put_during_autogain(mixer);
	if (err < 0)
		goto unlock;

	oval = private->ag_targets[index];
	val = clamp(ucontrol->value.integer.value[0],
		    (long)SCARLETT2_AG_TARGET_MIN, 0L);

	if (oval == val)
		goto unlock;

	private->ag_targets[index] = val;

	/* Send new value to the device */
	err = scarlett2_usb_set_config(
		mixer, scarlett2_ag_target_configs[index], 1, -val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const DECLARE_TLV_DB_MINMAX(
	db_scale_ag_target, SCARLETT2_AG_TARGET_MIN * 100, 0
);

static const struct snd_kcontrol_new scarlett2_ag_target_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
	.name = "",
	.info = scarlett2_ag_target_ctl_info,
	.get  = scarlett2_ag_target_ctl_get,
	.put  = scarlett2_ag_target_ctl_put,
	.tlv = { .p = db_scale_ag_target }
};

/*** Input Select Control ***/

static int scarlett2_update_input_select(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int link_count = info->gain_input_count / 2;
	int err;

	private->input_select_updated = 0;

	if (!scarlett2_has_config_item(private,
				       SCARLETT2_CONFIG_INPUT_SELECT_SWITCH) ||
	    !link_count)
		return 0;

	err = scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_INPUT_SELECT_SWITCH,
		1, &private->input_select_switch);
	if (err < 0)
		return err;

	err = scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_INPUT_LINK_SWITCH,
		link_count, private->input_link_switch);
	if (err < 0)
		return err;

	/* simplified because no model yet has link_count > 1 */
	if (private->input_link_switch[0])
		private->input_select_switch = 0;

	return 0;
}

static int scarlett2_input_select_ctl_get(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->input_select_updated) {
		err = scarlett2_update_input_select(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.enumerated.item[0] = private->input_select_switch;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_input_select_ctl_put(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int oval, val, err;
	int max_val = private->input_link_switch[0] ? 0 : 1;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_put_during_autogain(mixer);
	if (err < 0)
		goto unlock;

	oval = private->input_select_switch;
	val = ucontrol->value.integer.value[0];

	if (val < 0)
		val = 0;
	else if (val > max_val)
		val = max_val;

	if (oval == val)
		goto unlock;

	private->input_select_switch = val;

	/* Send switch change to the device if inputs not linked */
	if (!private->input_link_switch[0])
		err = scarlett2_usb_set_config(
			mixer, SCARLETT2_CONFIG_INPUT_SELECT_SWITCH,
			1, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_input_select_ctl_info(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int inputs = private->info->gain_input_count;
	int i, j;
	int err;
	char **values = kcalloc(inputs, sizeof(char *), GFP_KERNEL);

	if (!values)
		return -ENOMEM;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_autogain_updated(mixer);
	if (err < 0)
		goto unlock;

	/* Loop through each input
	 * Linked inputs have one value for the pair
	 */
	for (i = 0, j = 0; i < inputs; i++) {
		if (private->input_link_switch[i / 2]) {
			values[j++] = kasprintf(
				GFP_KERNEL, "Input %d-%d", i + 1, i + 2);
			i++;
		} else {
			values[j++] = kasprintf(
				GFP_KERNEL, "Input %d", i + 1);
		}
	}

	err = snd_ctl_enum_info(uinfo, 1, j,
				(const char * const *)values);

unlock:
	mutex_unlock(&private->data_mutex);

	for (i = 0; i < inputs; i++)
		kfree(values[i]);
	kfree(values);

	return err;
}

static const struct snd_kcontrol_new scarlett2_input_select_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = scarlett2_input_select_ctl_info,
	.get  = scarlett2_input_select_ctl_get,
	.put  = scarlett2_input_select_ctl_put,
};

/*** Input Link Switch Controls ***/

/* snd_ctl_boolean_mono_info() with autogain-updated check
 * (for controls that are read-only while autogain is running)
 */
static int scarlett2_autogain_disables_ctl_info(struct snd_kcontrol *kctl,
						struct snd_ctl_elem_info *uinfo)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_autogain_updated(mixer);
	if (err < 0)
		goto unlock;

	err = snd_ctl_boolean_mono_info(kctl, uinfo);

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_input_link_ctl_get(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->input_select_updated) {
		err = scarlett2_update_input_select(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.enumerated.item[0] =
		private->input_link_switch[elem->control];

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_input_link_ctl_put(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int index = elem->control;
	int oval, val, err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_put_during_autogain(mixer);
	if (err < 0)
		goto unlock;

	oval = private->input_link_switch[index];
	val = !!ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->input_link_switch[index] = val;

	/* Notify of change in input select options available */
	snd_ctl_notify(mixer->chip->card,
		       SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
		       &private->input_select_ctl->id);
	private->input_select_updated = 1;

	/* Send switch change to the device
	 * Link for channels 1-2 is at index 1
	 * No device yet has more than 2 channels linked
	 */
	err = scarlett2_usb_set_config(
		mixer, SCARLETT2_CONFIG_INPUT_LINK_SWITCH, index + 1, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_input_link_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = scarlett2_autogain_disables_ctl_info,
	.get  = scarlett2_input_link_ctl_get,
	.put  = scarlett2_input_link_ctl_put
};

/*** Input Gain Controls ***/

static int scarlett2_update_input_gain(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;

	private->input_gain_updated = 0;

	if (!info->gain_input_count)
		return 0;

	return scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_INPUT_GAIN,
		info->gain_input_count, private->gain);
}

static int scarlett2_input_gain_ctl_info(struct snd_kcontrol *kctl,
					 struct snd_ctl_elem_info *uinfo)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_autogain_updated(mixer);
	if (err < 0)
		goto unlock;

	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = elem->channels;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = SCARLETT2_MAX_GAIN_VALUE;
	uinfo->value.integer.step = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_input_gain_ctl_get(struct snd_kcontrol *kctl,
					struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->input_gain_updated) {
		err = scarlett2_update_input_gain(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.integer.value[0] =
		private->gain[elem->control];

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_input_gain_ctl_put(struct snd_kcontrol *kctl,
					struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int index = elem->control;
	int oval, val, err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_put_during_autogain(mixer);
	if (err < 0)
		goto unlock;

	oval = private->gain[index];
	val = ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->gain[index] = val;

	/* Send gain change to the device */
	err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_INPUT_GAIN,
				       index, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_input_gain_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
	.name = "",
	.info = scarlett2_input_gain_ctl_info,
	.get  = scarlett2_input_gain_ctl_get,
	.put  = scarlett2_input_gain_ctl_put,
	.private_value = 0, /* max value */
};

/*** Safe Controls ***/

static int scarlett2_update_input_safe(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;

	private->input_safe_updated = 0;

	if (!info->safe_input_count)
		return 0;

	return scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_SAFE_SWITCH,
		info->safe_input_count, private->safe_switch);
}

static int scarlett2_safe_ctl_get(struct snd_kcontrol *kctl,
				  struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->input_safe_updated) {
		err = scarlett2_update_input_safe(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.integer.value[0] =
		private->safe_switch[elem->control];

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_safe_ctl_put(struct snd_kcontrol *kctl,
				  struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int index = elem->control;
	int oval, val, err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_put_during_autogain(mixer);
	if (err < 0)
		goto unlock;

	oval = private->safe_switch[index];
	val = !!ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->safe_switch[index] = val;

	/* Send switch change to the device */
	err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SAFE_SWITCH,
				       index, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_safe_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = scarlett2_autogain_disables_ctl_info,
	.get  = scarlett2_safe_ctl_get,
	.put  = scarlett2_safe_ctl_put,
};

/*** PCM Input Control ***/

static int scarlett2_update_pcm_input_switch(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	int err;

	private->pcm_input_switch_updated = 0;

	err = scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_PCM_INPUT_SWITCH,
		1, &private->pcm_input_switch);
	if (err < 0)
		return err;

	return 0;
}

static int scarlett2_pcm_input_switch_ctl_get(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = elem->head.mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->pcm_input_switch_updated) {
		err = scarlett2_update_pcm_input_switch(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.enumerated.item[0] = private->pcm_input_switch;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_pcm_input_switch_ctl_put(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int oval, val, err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->pcm_input_switch;
	val = !!ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->pcm_input_switch = val;

	/* Send switch change to the device */
	err = scarlett2_usb_set_config(
		mixer, SCARLETT2_CONFIG_PCM_INPUT_SWITCH,
		0, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_pcm_input_switch_ctl_info(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
{
	static const char *const values[2] = {
		"Direct", "Mixer"
	};

	return snd_ctl_enum_info(
		uinfo, 1, 2, values);
}

static const struct snd_kcontrol_new scarlett2_pcm_input_switch_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = scarlett2_pcm_input_switch_ctl_info,
	.get  = scarlett2_pcm_input_switch_ctl_get,
	.put  = scarlett2_pcm_input_switch_ctl_put
};

/*** Analogue Line Out Volume Controls ***/

/* Update hardware volume controls after receiving notification that
 * they have changed
 */
static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	s16 vol;
	int err, i;

	private->vol_updated = 0;

	if (scarlett2_has_config_item(private,
				      SCARLETT2_CONFIG_MASTER_VOLUME)) {
		err = scarlett2_usb_get_config(
			mixer, SCARLETT2_CONFIG_MASTER_VOLUME,
			1, &vol);
		if (err < 0)
			return err;

		private->master_vol = clamp(vol + SCARLETT2_VOLUME_BIAS,
					    0, SCARLETT2_VOLUME_BIAS);

		if (scarlett2_has_config_item(private,
					      SCARLETT2_CONFIG_SW_HW_SWITCH))
			for (i = 0; i < private->num_line_out; i++)
				if (private->vol_sw_hw_switch[i])
					private->vol[i] = private->master_vol;
	}

	if (scarlett2_has_config_item(private,
				      SCARLETT2_CONFIG_HEADPHONE_VOLUME)) {
		err = scarlett2_usb_get_config(
			mixer, SCARLETT2_CONFIG_HEADPHONE_VOLUME,
			1, &vol);
		if (err < 0)
			return err;

		private->headphone_vol = clamp(vol + SCARLETT2_VOLUME_BIAS,
					       0, SCARLETT2_VOLUME_BIAS);
	}

	return 0;
}

static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
				     struct snd_ctl_elem_info *uinfo)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;

	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = elem->channels;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
	uinfo->value.integer.step = 1;
	return 0;
}

static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
					   struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->vol_updated) {
		err = scarlett2_update_volumes(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.integer.value[0] = private->master_vol;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_headphone_volume_ctl_get(
	struct snd_kcontrol *kctl,
	struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->vol_updated) {
		err = scarlett2_update_volumes(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.integer.value[0] = private->headphone_vol;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int line_out_remap(struct scarlett2_data *private, int index)
{
	const struct scarlett2_device_info *info = private->info;

	if (!info->line_out_remap_enable)
		return index;

	if (index >= private->num_line_out)
		return index;

	return info->line_out_remap[index];
}

static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
				    struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int index = line_out_remap(private, elem->control);
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->vol_updated) {
		err = scarlett2_update_volumes(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.integer.value[0] = private->vol[index];

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
				    struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int index = line_out_remap(private, elem->control);
	int oval, val, err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->vol[index];
	val = ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->vol[index] = val;
	err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
				       index, val - SCARLETT2_VOLUME_BIAS);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const DECLARE_TLV_DB_MINMAX(
	db_scale_scarlett2_volume, -SCARLETT2_VOLUME_BIAS * 100, 0
);

static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.access = SNDRV_CTL_ELEM_ACCESS_READ |
		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
	.name = "",
	.info = scarlett2_volume_ctl_info,
	.get  = scarlett2_master_volume_ctl_get,
	.private_value = 0, /* max value */
	.tlv = { .p = db_scale_scarlett2_volume }
};

static const struct snd_kcontrol_new scarlett2_headphone_volume_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.access = SNDRV_CTL_ELEM_ACCESS_READ |
		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
	.name = "",
	.info = scarlett2_volume_ctl_info,
	.get  = scarlett2_headphone_volume_ctl_get,
	.private_value = 0, /* max value */
	.tlv = { .p = db_scale_scarlett2_volume }
};

static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
	.name = "",
	.info = scarlett2_volume_ctl_info,
	.get  = scarlett2_volume_ctl_get,
	.put  = scarlett2_volume_ctl_put,
	.private_value = 0, /* max value */
	.tlv = { .p = db_scale_scarlett2_volume }
};

/*** Mute Switch Controls ***/

static int scarlett2_update_dim_mute(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	int err, i;
	u8 mute;

	private->dim_mute_updated = 0;

	if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
		return 0;

	err = scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_DIM_MUTE,
		SCARLETT2_DIM_MUTE_COUNT, private->dim_mute);
	if (err < 0)
		return err;

	for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
		private->dim_mute[i] = !!private->dim_mute[i];

	mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];

	for (i = 0; i < private->num_line_out; i++)
		if (private->vol_sw_hw_switch[i])
			private->mute_switch[i] = mute;

	return 0;
}

static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
					struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int index = line_out_remap(private, elem->control);
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->dim_mute_updated) {
		err = scarlett2_update_dim_mute(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.integer.value[0] = private->mute_switch[index];

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
					struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int index = line_out_remap(private, elem->control);
	int oval, val, err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->mute_switch[index];
	val = !!ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->mute_switch[index] = val;

	/* Send mute change to the device */
	err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
				       index, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_mute_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = snd_ctl_boolean_mono_info,
	.get  = scarlett2_mute_ctl_get,
	.put  = scarlett2_mute_ctl_put,
};

/*** HW/SW Volume Switch Controls ***/

static void scarlett2_sw_hw_ctl_ro(struct scarlett2_data *private, int index)
{
	private->sw_hw_ctls[index]->vd[0].access &=
		~SNDRV_CTL_ELEM_ACCESS_WRITE;
}

static void scarlett2_sw_hw_ctl_rw(struct scarlett2_data *private, int index)
{
	private->sw_hw_ctls[index]->vd[0].access |=
		SNDRV_CTL_ELEM_ACCESS_WRITE;
}

static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
					 struct snd_ctl_elem_info *uinfo)
{
	static const char *const values[2] = {
		"SW", "HW"
	};

	return snd_ctl_enum_info(uinfo, 1, 2, values);
}

static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
					struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct scarlett2_data *private = elem->head.mixer->private_data;
	int index = line_out_remap(private, elem->control);

	ucontrol->value.enumerated.item[0] = private->vol_sw_hw_switch[index];
	return 0;
}

static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
					   int index, int value)
{
	struct scarlett2_data *private = mixer->private_data;
	struct snd_card *card = mixer->chip->card;

	/* Set/Clear write bits */
	if (value) {
		private->vol_ctls[index]->vd[0].access |=
			SNDRV_CTL_ELEM_ACCESS_WRITE;
		private->mute_ctls[index]->vd[0].access |=
			SNDRV_CTL_ELEM_ACCESS_WRITE;
	} else {
		private->vol_ctls[index]->vd[0].access &=
			~SNDRV_CTL_ELEM_ACCESS_WRITE;
		private->mute_ctls[index]->vd[0].access &=
			~SNDRV_CTL_ELEM_ACCESS_WRITE;
	}

	/* Notify of write bit and possible value change */
	snd_ctl_notify(card,
		       SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
		       &private->vol_ctls[index]->id);
	snd_ctl_notify(card,
		       SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
		       &private->mute_ctls[index]->id);
}

static int scarlett2_sw_hw_change(struct usb_mixer_interface *mixer,
				  int ctl_index, int val)
{
	struct scarlett2_data *private = mixer->private_data;
	int index = line_out_remap(private, ctl_index);
	int err;

	private->vol_sw_hw_switch[index] = val;

	/* Change access mode to RO (hardware controlled volume)
	 * or RW (software controlled volume)
	 */
	scarlett2_vol_ctl_set_writable(mixer, ctl_index, !val);

	/* Reset volume/mute to master volume/mute */
	private->vol[index] = private->master_vol;
	private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE];

	/* Set SW volume to current HW volume */
	err = scarlett2_usb_set_config(
		mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
		index, private->master_vol - SCARLETT2_VOLUME_BIAS);
	if (err < 0)
		return err;

	/* Set SW mute to current HW mute */
	err = scarlett2_usb_set_config(
		mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
		index, private->dim_mute[SCARLETT2_BUTTON_MUTE]);
	if (err < 0)
		return err;

	/* Send SW/HW switch change to the device */
	return scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
					index, val);
}

static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
					struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int ctl_index = elem->control;
	int index = line_out_remap(private, ctl_index);
	int oval, val, err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->vol_sw_hw_switch[index];
	val = !!ucontrol->value.enumerated.item[0];

	if (oval == val)
		goto unlock;

	err = scarlett2_sw_hw_change(mixer, ctl_index, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = scarlett2_sw_hw_enum_ctl_info,
	.get  = scarlett2_sw_hw_enum_ctl_get,
	.put  = scarlett2_sw_hw_enum_ctl_put,
};

/*** Line Level/Instrument Level Switch Controls ***/

static int scarlett2_update_input_level(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;

	private->input_level_updated = 0;

	if (!info->level_input_count)
		return 0;

	return scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
		info->level_input_count + info->level_input_first,
		private->level_switch);
}

static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
					 struct snd_ctl_elem_info *uinfo)
{
	static const char *const values[2] = {
		"Line", "Inst"
	};
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_autogain_updated(mixer);
	if (err < 0)
		goto unlock;

	err = snd_ctl_enum_info(uinfo, 1, 2, values);

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
					struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;

	int index = elem->control + info->level_input_first;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->input_level_updated) {
		err = scarlett2_update_input_level(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.enumerated.item[0] = scarlett2_decode_muteable(
		private->level_switch[index]);

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
					struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;

	int index = elem->control + info->level_input_first;
	int oval, val, err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_put_during_autogain(mixer);
	if (err < 0)
		goto unlock;

	oval = private->level_switch[index];
	val = !!ucontrol->value.enumerated.item[0];

	if (oval == val)
		goto unlock;

	private->level_switch[index] = val;

	/* To set the Gen 4 muteable controls, bit 1 gets set instead */
	if (private->config_set->items[SCARLETT2_CONFIG_LEVEL_SWITCH].mute)
		val = (!val) | 0x02;

	/* Send switch change to the device */
	err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
				       index, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = scarlett2_level_enum_ctl_info,
	.get  = scarlett2_level_enum_ctl_get,
	.put  = scarlett2_level_enum_ctl_put,
};

/*** Pad Switch Controls ***/

static int scarlett2_update_input_pad(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;

	private->input_pad_updated = 0;

	if (!info->pad_input_count)
		return 0;

	return scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_PAD_SWITCH,
		info->pad_input_count, private->pad_switch);
}

static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->input_pad_updated) {
		err = scarlett2_update_input_pad(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.integer.value[0] =
		private->pad_switch[elem->control];

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int index = elem->control;
	int oval, val, err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->pad_switch[index];
	val = !!ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->pad_switch[index] = val;

	/* Send switch change to the device */
	err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
				       index, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_pad_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = snd_ctl_boolean_mono_info,
	.get  = scarlett2_pad_ctl_get,
	.put  = scarlett2_pad_ctl_put,
};

/*** Air Switch Controls ***/

static int scarlett2_update_input_air(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;

	private->input_air_updated = 0;

	if (!info->air_input_count)
		return 0;

	return scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_AIR_SWITCH,
		info->air_input_count, private->air_switch);
}

static int scarlett2_air_ctl_get(struct snd_kcontrol *kctl,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->input_air_updated) {
		err = scarlett2_update_input_air(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.integer.value[0] = private->air_switch[elem->control];

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_air_ctl_put(struct snd_kcontrol *kctl,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int index = elem->control;
	int oval, val, err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_put_during_autogain(mixer);
	if (err < 0)
		goto unlock;

	oval = private->air_switch[index];
	val = ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->air_switch[index] = val;

	/* Send switch change to the device */
	err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_AIR_SWITCH,
				       index, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_air_with_drive_ctl_info(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
{
	static const char *const values[3] = {
		"Off", "Presence", "Presence + Drive"
	};
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_autogain_updated(mixer);
	if (err < 0)
		goto unlock;

	err = snd_ctl_enum_info(uinfo, 1, 3, values);

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_air_ctl[2] = {
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "",
		.info = snd_ctl_boolean_mono_info,
		.get  = scarlett2_air_ctl_get,
		.put  = scarlett2_air_ctl_put,
	},
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "",
		.info = scarlett2_air_with_drive_ctl_info,
		.get  = scarlett2_air_ctl_get,
		.put  = scarlett2_air_ctl_put,
	}
};

/*** DSP Switch Control ***/

static int scarlett2_update_input_dsp(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;

	private->input_dsp_updated = 0;

	if (!info->dsp_input_count)
		return 0;

	return scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_DSP_SWITCH,
		info->dsp_input_count, private->dsp_switch);
}

static int scarlett2_dsp_ctl_get(struct snd_kcontrol *kctl,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->input_dsp_updated) {
		err = scarlett2_update_input_dsp(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.integer.value[0] = private->dsp_switch[elem->control];

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_dsp_ctl_put(struct snd_kcontrol *kctl,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int index = elem->control;
	int oval, val, err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_put_during_autogain(mixer);
	if (err < 0)
		goto unlock;

	oval = private->dsp_switch[index];
	val = ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->dsp_switch[index] = val;

	/* Send switch change to the device */
	err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DSP_SWITCH,
				       index, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_dsp_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = scarlett2_autogain_disables_ctl_info,
	.get  = scarlett2_dsp_ctl_get,
	.put  = scarlett2_dsp_ctl_put,
};

/*** DSP Compressor Parameter Controls ***/

static int scarlett2_update_compressor_values(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int err, i, j;

	if (!info->dsp_input_count)
		return 0;

	err = scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_COMPRESSOR_PARAMS,
		SCARLETT2_COMPRESSOR_PARAM_COUNT * info->dsp_input_count,
		private->compressor_values);

	if (err < 0)
		return err;

	for (i = 0; i < SCARLETT2_COMPRESSOR_PARAM_COUNT; i++) {
		const struct compressor_param *param = &compressor_params[i];

		for (j = 0; j < info->dsp_input_count; j++) {
			int idx = i + j * SCARLETT2_COMPRESSOR_PARAM_COUNT;
			int val = private->compressor_values[idx];

			val >>= param->scale_bits;
			val = clamp(val, param->min, param->max);
			private->compressor_values[idx] = val;
		}
	}

	return 0;
}

static int scarlett2_compressor_ctl_get(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct scarlett2_data *private = elem->head.mixer->private_data;

	ucontrol->value.integer.value[0] =
		private->compressor_values[elem->control];
	return 0;
}

static int scarlett2_compressor_ctl_put(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int index = elem->control;
	int channel = index / SCARLETT2_COMPRESSOR_PARAM_COUNT;
	int param_index = index % SCARLETT2_COMPRESSOR_PARAM_COUNT;
	int oval, val, err;
	s32 scaled_val;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_put_during_autogain(mixer);
	if (err < 0)
		goto unlock;

	oval = private->compressor_values[index];
	val = ucontrol->value.integer.value[0];
	if (oval == val)
		goto unlock;

	private->compressor_values[index] = val;

	const struct compressor_param *param = &compressor_params[param_index];

	scaled_val = val << param->scale_bits;

	/* Send change to the device */

	/* The channel needs to be put in the parameter buffer index
	 * field (param_buf_addr + 1); the value field isn't used in
	 * this case.
	 */
	err = scarlett2_usb_set_data(
		mixer, private->config_set->param_buf_addr + 1, 1, channel);
	if (err < 0)
		goto unlock;

	err = scarlett2_usb_set_config(
		mixer, SCARLETT2_CONFIG_COMPRESSOR_PARAMS, index, scaled_val);
	if (err < 0)
		goto unlock;

	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_compressor_ctl_info(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	int control = elem->control % SCARLETT2_COMPRESSOR_PARAM_COUNT;

	uinfo->type = compressor_params[control].type;
	uinfo->count = 1;
	uinfo->value.integer.min = compressor_params[control].min;
	uinfo->value.integer.max = compressor_params[control].max;
	uinfo->value.integer.step = 1;
	return 0;
}

static const struct snd_kcontrol_new scarlett2_compressor_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
	.name = "",
	.info = scarlett2_compressor_ctl_info,
	.get  = scarlett2_compressor_ctl_get,
	.put  = scarlett2_compressor_ctl_put,
};

/*** DSP Pre-Compressor and PEQ Filter Controls ***/

static int scarlett2_precomp_flt_switch_ctl_get(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct scarlett2_data *private = elem->head.mixer->private_data;

	ucontrol->value.integer.value[0] = private->precomp_flt_switch[elem->control];

	return 0;
}

static int scarlett2_peq_flt_switch_ctl_get(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct scarlett2_data *private = elem->head.mixer->private_data;

	ucontrol->value.integer.value[0] =
		private->peq_flt_switch[elem->control];

	return 0;
}

static int scarlett2_precomp_flt_switch_ctl_put(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int oval, val, err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->precomp_flt_switch[elem->control];
	val = ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->precomp_flt_switch[elem->control] = val;

	/* Send change to the device */
	err = scarlett2_usb_set_config(
		mixer, SCARLETT2_CONFIG_PRECOMP_FLT_SWITCH,
		elem->control, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_peq_flt_switch_ctl_put(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int oval, val, err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->peq_flt_switch[elem->control];
	val = ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->peq_flt_switch[elem->control] = val;

	/* Send change to the device */
	err = scarlett2_usb_set_config(
		mixer, SCARLETT2_CONFIG_PEQ_FLT_SWITCH,
		elem->control, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_precomp_flt_switch_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
	.name = "",
	.info = snd_ctl_boolean_mono_info,
	.get  = scarlett2_precomp_flt_switch_ctl_get,
	.put  = scarlett2_precomp_flt_switch_ctl_put,
};

static const struct snd_kcontrol_new scarlett2_peq_flt_switch_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
	.name = "",
	.info = snd_ctl_boolean_mono_info,
	.get  = scarlett2_peq_flt_switch_ctl_get,
	.put  = scarlett2_peq_flt_switch_ctl_put,
};

static int scarlett2_update_filter_values(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int err, i, j, k, src_idx, dst_idx;
	s32 peq_flt_values[SCARLETT2_DSP_SWITCH_MAX *
			   SCARLETT2_PEQ_FLT_SLOTS_MAX *
			   SCARLETT2_BIQUAD_COEFFS];

	if (!info->dsp_input_count)
		return 0;

	/* Get filter switch values */
	err = scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_PRECOMP_FLT_SWITCH,
		info->dsp_input_count, private->precomp_flt_switch);
	if (err < 0)
		return err;

	err = scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_PEQ_FLT_SWITCH,
		info->dsp_input_count * info->peq_flt_count,
		private->peq_flt_switch);
	if (err < 0)
		return err;

	/* Get pre-compressor filter values directly */
	err = scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_PRECOMP_FLT_PARAMS,
		info->dsp_input_count *
			info->precomp_flt_count *
			SCARLETT2_BIQUAD_COEFFS,
		private->precomp_flt_values);

	if (err < 0)
		return err;

	/* PEQ filter values need to be copied via buffer because of
	 * padding after peq_flt_count up to peq_flt_total_count
	 */
	err = scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_PEQ_FLT_PARAMS,
		info->dsp_input_count *
			info->peq_flt_total_count *
			SCARLETT2_BIQUAD_COEFFS,
		peq_flt_values);
	if (err < 0)
		return err;

	for (i = 0, dst_idx = 0; i < info->dsp_input_count; i++) {
		src_idx = i *
			  info->peq_flt_total_count *
			  SCARLETT2_BIQUAD_COEFFS;
		for (j = 0; j < info->peq_flt_count; j++)
			for (k = 0;
			     k < SCARLETT2_BIQUAD_COEFFS;
			     k++, src_idx++, dst_idx++)
				private->peq_flt_values[dst_idx] =
					peq_flt_values[src_idx];
	}

	return 0;
}

static int scarlett2_precomp_flt_ctl_get(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct scarlett2_data *private = elem->head.mixer->private_data;
	int i, idx;

	for (i = 0, idx = elem->control * SCARLETT2_BIQUAD_COEFFS;
	     i < SCARLETT2_BIQUAD_COEFFS;
	     i++, idx++)
		ucontrol->value.integer.value[i] =
			private->precomp_flt_values[idx];

	return 0;
}

static int scarlett2_peq_flt_ctl_get(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct scarlett2_data *private = elem->head.mixer->private_data;
	int i, idx;

	for (i = 0, idx = elem->control * SCARLETT2_BIQUAD_COEFFS;
	     i < SCARLETT2_BIQUAD_COEFFS;
	     i++, idx++)
		ucontrol->value.integer.value[i] =
			private->peq_flt_values[idx];

	return 0;
}

static int scarlett2_precomp_flt_ctl_put(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int index = elem->control * SCARLETT2_BIQUAD_COEFFS;
	int i, oval, val, err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_put_during_autogain(mixer);
	if (err < 0)
		goto unlock;

	/* Check if any of the values have changed; if not, return */
	for (i = 0; i < SCARLETT2_BIQUAD_COEFFS; i++) {
		oval = private->precomp_flt_values[index + i];
		val = ucontrol->value.integer.value[i];
		if (oval != val)
			break;
	}

	if (i == SCARLETT2_BIQUAD_COEFFS)
		goto unlock;

	/* Update the values */
	for (i = 0; i < SCARLETT2_BIQUAD_COEFFS; i++)
		private->precomp_flt_values[index + i] =
			ucontrol->value.integer.value[i];

	/* Send change to the device */
	err = scarlett2_usb_set_data(
		mixer, private->config_set->param_buf_addr, 1, index);
	if (err < 0)
		goto unlock;

	err = scarlett2_usb_set_config_buf(
		mixer, SCARLETT2_CONFIG_PRECOMP_FLT_PARAMS,
		index, SCARLETT2_BIQUAD_COEFFS,
		&private->precomp_flt_values[index]);

	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_peq_flt_ctl_put(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;

	int src_index = elem->control * SCARLETT2_BIQUAD_COEFFS;
	int dst_index = (
		elem->control /
		info->peq_flt_count *
		info->peq_flt_total_count +
		elem->control % info->peq_flt_count
	) * SCARLETT2_BIQUAD_COEFFS;
	int i, oval, val, err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_put_during_autogain(mixer);
	if (err < 0)
		goto unlock;

	/* Check if any of the values have changed; if not, return */
	for (i = 0; i < SCARLETT2_BIQUAD_COEFFS; i++) {
		oval = private->peq_flt_values[src_index + i];
		val = ucontrol->value.integer.value[i];
		if (oval != val)
			break;
	}

	if (i == SCARLETT2_BIQUAD_COEFFS)
		goto unlock;

	/* Update the values */
	for (i = 0; i < SCARLETT2_BIQUAD_COEFFS; i++)
		private->peq_flt_values[src_index + i] =
			ucontrol->value.integer.value[i];

	/* Send change to the device */
	err = scarlett2_usb_set_data(
		mixer, private->config_set->param_buf_addr, 1, dst_index);
	if (err < 0)
		goto unlock;

	err = scarlett2_usb_set_config_buf(
		mixer, SCARLETT2_CONFIG_PEQ_FLT_PARAMS,
		dst_index, SCARLETT2_BIQUAD_COEFFS,
		&private->peq_flt_values[src_index]);

	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_flt_ctl_info(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = SCARLETT2_BIQUAD_COEFFS;
	uinfo->value.integer.min = INT_MIN;
	uinfo->value.integer.max = INT_MAX;
	uinfo->value.integer.step = 1;
	return 0;
}

static const struct snd_kcontrol_new scarlett2_precomp_flt_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
	.name = "",
	.info = scarlett2_flt_ctl_info,
	.get  = scarlett2_precomp_flt_ctl_get,
	.put  = scarlett2_precomp_flt_ctl_put,
};

static const struct snd_kcontrol_new scarlett2_peq_flt_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
	.name = "",
	.info = scarlett2_flt_ctl_info,
	.get  = scarlett2_peq_flt_ctl_get,
	.put  = scarlett2_peq_flt_ctl_put,
};

/*** Input Mute Switch Controls ***/

static int scarlett2_update_input_mute(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;

	private->input_mute_updated = 0;

	if (!info->mute_input_count)
		return 0;

	return scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_INPUT_MUTE_SWITCH,
		info->mute_input_count, private->input_mute_switch);
}

static int scarlett2_input_mute_ctl_get(struct snd_kcontrol *kctl,
					struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->input_mute_updated) {
		err = scarlett2_update_input_mute(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.integer.value[0] =
		private->input_mute_switch[elem->control];

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_input_mute_ctl_put(struct snd_kcontrol *kctl,
					struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int index = elem->control;
	int oval, val, err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_put_during_autogain(mixer);
	if (err < 0)
		goto unlock;

	oval = private->input_mute_switch[index];
	val = ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->input_mute_switch[index] = val;

	/* Send switch change to the device */
	err = scarlett2_usb_set_config(
		mixer, SCARLETT2_CONFIG_INPUT_MUTE_SWITCH,
			index, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_input_mute_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = scarlett2_autogain_disables_ctl_info,
	.get  = scarlett2_input_mute_ctl_get,
	.put  = scarlett2_input_mute_ctl_put,
};

/*** Phantom Switch Controls ***/

static int scarlett2_update_input_phantom(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int err;

	private->input_phantom_updated = 0;

	if (!info->phantom_count)
		return 0;

	err = scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
		info->phantom_count, private->phantom_switch);
	if (err < 0)
		return err;

	if (scarlett2_has_config_item(private,
				      SCARLETT2_CONFIG_PHANTOM_PERSISTENCE)) {
		err = scarlett2_usb_get_config(
			mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
			1, &private->phantom_persistence);
		if (err < 0)
			return err;
	}

	return 0;
}

/* Check if phantom power on the given input is currently changing state */
static int scarlett2_phantom_is_switching(
	struct scarlett2_data *private, int line_num)
{
	const struct scarlett2_device_info *info = private->info;
	int index = line_num / info->inputs_per_phantom;

	return !!(private->phantom_switch[index] & 0x02);
}

/* Update autogain controls' access mode when phantom power changes state */
static void scarlett2_phantom_update_access(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int i;

	/* Disable autogain controls if phantom power is changing state */
	for (i = 0; i < info->gain_input_count; i++) {
		int val = !scarlett2_phantom_is_switching(private, i);

		scarlett2_set_ctl_access(private->autogain_ctls[i], val);
	}
}

/* Notify of access mode change for autogain which can't be enabled
 * while phantom power is changing.
 */
static void scarlett2_phantom_notify_access(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int i;

	for (i = 0; i < info->gain_input_count; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
			       &private->autogain_ctls[i]->id);
}

/* Call scarlett2_update_input_phantom() and
 * scarlett2_phantom_update_access() if input_phantom_updated is set.
 */
static int scarlett2_check_input_phantom_updated(
	struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	int err;

	if (!private->input_phantom_updated)
		return 0;

	err = scarlett2_update_input_phantom(mixer);
	if (err < 0)
		return err;

	scarlett2_phantom_update_access(mixer);

	return 0;
}

static int scarlett2_phantom_ctl_get(struct snd_kcontrol *kctl,
				     struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_input_phantom_updated(mixer);
	if (err < 0)
		goto unlock;

	ucontrol->value.integer.value[0] = scarlett2_decode_muteable(
		private->phantom_switch[elem->control]);

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_phantom_ctl_put(struct snd_kcontrol *kctl,
				     struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;

	int index = elem->control;
	int oval, val, err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_check_put_during_autogain(mixer);
	if (err < 0)
		goto unlock;

	oval = private->phantom_switch[index];
	val = !!ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->phantom_switch[index] = val;

	/* To set the Gen 4 muteable controls, bit 1 gets set */
	if (private->config_set->items[SCARLETT2_CONFIG_PHANTOM_SWITCH].mute)
		val = (!val) | 0x02;

	/* Send switch change to the device */
	err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
				       index + info->phantom_first, val);
	if (err == 0)
		err = 1;

	scarlett2_phantom_update_access(mixer);
	scarlett2_phantom_notify_access(mixer);

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_phantom_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = scarlett2_autogain_disables_ctl_info,
	.get  = scarlett2_phantom_ctl_get,
	.put  = scarlett2_phantom_ctl_put,
};

/*** Phantom Persistence Control ***/

static int scarlett2_phantom_persistence_ctl_get(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct scarlett2_data *private = elem->head.mixer->private_data;

	ucontrol->value.integer.value[0] = private->phantom_persistence;
	return 0;
}

static int scarlett2_phantom_persistence_ctl_put(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int index = elem->control;
	int oval, val, err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->phantom_persistence;
	val = !!ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->phantom_persistence = val;

	/* Send switch change to the device */
	err = scarlett2_usb_set_config(
		mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, index, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_phantom_persistence_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = snd_ctl_boolean_mono_info,
	.get  = scarlett2_phantom_persistence_ctl_get,
	.put  = scarlett2_phantom_persistence_ctl_put,
};

/*** Speaker Switching Control ***/

static int scarlett2_update_monitor_other(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int err;

	/* monitor_other_enable[0] enables speaker switching
	 * monitor_other_enable[1] enables talkback
	 */
	u8 monitor_other_enable[2];

	/* monitor_other_switch[0] activates the alternate speakers
	 * monitor_other_switch[1] activates talkback
	 */
	u8 monitor_other_switch[2];

	private->monitor_other_updated = 0;

	/* if it doesn't do speaker switching then it also doesn't do
	 * talkback
	 */
	if (!info->has_speaker_switching)
		return 0;

	err = scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
		2, monitor_other_enable);
	if (err < 0)
		return err;

	err = scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
		2, monitor_other_switch);
	if (err < 0)
		return err;

	if (!monitor_other_enable[0])
		private->speaker_switching_switch = 0;
	else
		private->speaker_switching_switch = monitor_other_switch[0] + 1;

	if (info->has_talkback) {
		u16 bitmap;
		int i;

		if (!monitor_other_enable[1])
			private->talkback_switch = 0;
		else
			private->talkback_switch = monitor_other_switch[1] + 1;

		err = scarlett2_usb_get_config(mixer,
					       SCARLETT2_CONFIG_TALKBACK_MAP,
					       1, &bitmap);
		if (err < 0)
			return err;
		for (i = 0; i < private->num_mix_out; i++, bitmap >>= 1)
			private->talkback_map[i] = bitmap & 1;
	}

	return 0;
}

static int scarlett2_speaker_switch_enum_ctl_info(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
{
	static const char *const values[3] = {
		"Off", "Main", "Alt"
	};

	return snd_ctl_enum_info(uinfo, 1, 3, values);
}

static int scarlett2_speaker_switch_enum_ctl_get(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->monitor_other_updated) {
		err = scarlett2_update_monitor_other(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.enumerated.item[0] = private->speaker_switching_switch;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

/* when speaker switching gets enabled, switch the main/alt speakers
 * to HW volume and disable those controls
 */
static int scarlett2_speaker_switch_enable(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	int i, err;

	for (i = 0; i < 4; i++) {
		int index = line_out_remap(private, i);

		/* switch the main/alt speakers to HW volume */
		if (!private->vol_sw_hw_switch[index]) {
			err = scarlett2_sw_hw_change(private->mixer, i, 1);
			if (err < 0)
				return err;
		}

		/* disable the line out SW/HW switch */
		scarlett2_sw_hw_ctl_ro(private, i);
		snd_ctl_notify(card,
			       SNDRV_CTL_EVENT_MASK_VALUE |
				 SNDRV_CTL_EVENT_MASK_INFO,
			       &private->sw_hw_ctls[i]->id);
	}

	/* when the next monitor-other notify comes in, update the mux
	 * configuration
	 */
	private->speaker_switching_switched = 1;

	return 0;
}

/* when speaker switching gets disabled, reenable the hw/sw controls
 * and invalidate the routing
 */
static void scarlett2_speaker_switch_disable(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	int i;

	/* enable the line out SW/HW switch */
	for (i = 0; i < 4; i++) {
		scarlett2_sw_hw_ctl_rw(private, i);
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
			       &private->sw_hw_ctls[i]->id);
	}

	/* when the next monitor-other notify comes in, update the mux
	 * configuration
	 */
	private->speaker_switching_switched = 1;
}

static int scarlett2_speaker_switch_enum_ctl_put(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int oval, val, err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->speaker_switching_switch;
	val = min(ucontrol->value.enumerated.item[0], 2U);

	if (oval == val)
		goto unlock;

	private->speaker_switching_switch = val;

	/* enable/disable speaker switching */
	err = scarlett2_usb_set_config(
		mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
		0, !!val);
	if (err < 0)
		goto unlock;

	/* if speaker switching is enabled, select main or alt */
	err = scarlett2_usb_set_config(
		mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
		0, val == 2);
	if (err < 0)
		goto unlock;

	/* update controls if speaker switching gets enabled or disabled */
	if (!oval && val)
		err = scarlett2_speaker_switch_enable(mixer);
	else if (oval && !val)
		scarlett2_speaker_switch_disable(mixer);

	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_speaker_switch_enum_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = scarlett2_speaker_switch_enum_ctl_info,
	.get  = scarlett2_speaker_switch_enum_ctl_get,
	.put  = scarlett2_speaker_switch_enum_ctl_put,
};

static int scarlett2_add_speaker_switch_ctl(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;

	if (!info->has_speaker_switching)
		return 0;

	return scarlett2_add_new_ctl(
		mixer, &scarlett2_speaker_switch_enum_ctl,
		0, 1, "Speaker Switching Playback Enum",
		&private->speaker_switching_ctl);
}

/*** Talkback and Talkback Map Controls ***/

static int scarlett2_talkback_enum_ctl_info(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
{
	static const char *const values[3] = {
		"Disabled", "Off", "On"
	};

	return snd_ctl_enum_info(uinfo, 1, 3, values);
}

static int scarlett2_talkback_enum_ctl_get(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->monitor_other_updated) {
		err = scarlett2_update_monitor_other(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.enumerated.item[0] = private->talkback_switch;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_talkback_enum_ctl_put(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int oval, val, err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->talkback_switch;
	val = min(ucontrol->value.enumerated.item[0], 2U);

	if (oval == val)
		goto unlock;

	private->talkback_switch = val;

	/* enable/disable talkback */
	err = scarlett2_usb_set_config(
		mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
		1, !!val);
	if (err < 0)
		goto unlock;

	/* if talkback is enabled, select main or alt */
	err = scarlett2_usb_set_config(
		mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
		1, val == 2);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_talkback_enum_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = scarlett2_talkback_enum_ctl_info,
	.get  = scarlett2_talkback_enum_ctl_get,
	.put  = scarlett2_talkback_enum_ctl_put,
};

static int scarlett2_talkback_map_ctl_get(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int index = elem->control;

	ucontrol->value.integer.value[0] = private->talkback_map[index];

	return 0;
}

static int scarlett2_talkback_map_ctl_put(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int index = elem->control;
	int oval, val, err = 0, i;
	u16 bitmap = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->talkback_map[index];
	val = !!ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->talkback_map[index] = val;

	for (i = 0; i < private->num_mix_out; i++)
		bitmap |= private->talkback_map[i] << i;

	/* Send updated bitmap to the device */
	err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_TALKBACK_MAP,
				       0, bitmap);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_talkback_map_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = snd_ctl_boolean_mono_info,
	.get  = scarlett2_talkback_map_ctl_get,
	.put  = scarlett2_talkback_map_ctl_put,
};

static int scarlett2_add_talkback_ctls(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int err, i;
	char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];

	if (!info->has_talkback)
		return 0;

	err = scarlett2_add_new_ctl(
		mixer, &scarlett2_talkback_enum_ctl,
		0, 1, "Talkback Playback Enum",
		&private->talkback_ctl);
	if (err < 0)
		return err;

	for (i = 0; i < private->num_mix_out; i++) {
		snprintf(s, sizeof(s),
			 "Talkback Mix %c Playback Switch", i + 'A');
		err = scarlett2_add_new_ctl(mixer, &scarlett2_talkback_map_ctl,
					    i, 1, s, NULL);
		if (err < 0)
			return err;
	}

	return 0;
}

/*** Dim/Mute Controls ***/

static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
				      struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->dim_mute_updated) {
		err = scarlett2_update_dim_mute(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.integer.value[0] = private->dim_mute[elem->control];

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
				      struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int index = elem->control;
	int oval, val, err = 0, i;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->dim_mute[index];
	val = !!ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->dim_mute[index] = val;

	/* Send switch change to the device */
	err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
				       index, val);
	if (err == 0)
		err = 1;

	if (index == SCARLETT2_BUTTON_MUTE)
		for (i = 0; i < private->num_line_out; i++) {
			int line_index = line_out_remap(private, i);

			if (private->vol_sw_hw_switch[line_index]) {
				private->mute_switch[line_index] = val;
				snd_ctl_notify(mixer->chip->card,
					       SNDRV_CTL_EVENT_MASK_VALUE,
					       &private->mute_ctls[i]->id);
			}
		}

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = snd_ctl_boolean_mono_info,
	.get  = scarlett2_dim_mute_ctl_get,
	.put  = scarlett2_dim_mute_ctl_put
};

/*** Create the analogue output controls ***/

static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int err, i;
	char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];

	/* Add R/O HW volume control */
	if (scarlett2_has_config_item(private,
				      SCARLETT2_CONFIG_MASTER_VOLUME)) {
		snprintf(s, sizeof(s), "Master HW Playback Volume");
		err = scarlett2_add_new_ctl(mixer,
					    &scarlett2_master_volume_ctl,
					    0, 1, s, &private->master_vol_ctl);
		if (err < 0)
			return err;
	}

	/* Add R/O headphone volume control */
	if (scarlett2_has_config_item(private,
				      SCARLETT2_CONFIG_HEADPHONE_VOLUME)) {
		snprintf(s, sizeof(s), "Headphone Playback Volume");
		err = scarlett2_add_new_ctl(mixer,
					    &scarlett2_headphone_volume_ctl,
					    0, 1, s,
					    &private->headphone_vol_ctl);
		if (err < 0)
			return err;
	}

	/* Remaining controls are only applicable if the device
	 * has per-channel line-out volume controls.
	 */
	if (!scarlett2_has_config_item(private,
				       SCARLETT2_CONFIG_LINE_OUT_VOLUME))
		return 0;

	/* Add volume controls */
	for (i = 0; i < private->num_line_out; i++) {
		int index = line_out_remap(private, i);

		/* Fader */
		if (info->line_out_descrs[i])
			snprintf(s, sizeof(s),
				 "Line %02d (%s) Playback Volume",
				 i + 1, info->line_out_descrs[i]);
		else
			snprintf(s, sizeof(s),
				 "Line %02d Playback Volume",
				 i + 1);
		err = scarlett2_add_new_ctl(mixer,
					    &scarlett2_line_out_volume_ctl,
					    i, 1, s, &private->vol_ctls[i]);
		if (err < 0)
			return err;

		/* Mute Switch */
		snprintf(s, sizeof(s),
			 "Line %02d Mute Playback Switch",
			 i + 1);
		err = scarlett2_add_new_ctl(mixer,
					    &scarlett2_mute_ctl,
					    i, 1, s,
					    &private->mute_ctls[i]);
		if (err < 0)
			return err;

		/* SW/HW Switch */
		if (scarlett2_has_config_item(private,
					      SCARLETT2_CONFIG_SW_HW_SWITCH)) {

			/* Make the fader and mute controls read-only if the
			 * SW/HW switch is set to HW
			 */
			if (private->vol_sw_hw_switch[index])
				scarlett2_vol_ctl_set_writable(mixer, i, 0);

			scnprintf(s, sizeof(s),
				  "Line Out %02d Volume Control Playback Enum",
				  i + 1);
			err = scarlett2_add_new_ctl(mixer,
						    &scarlett2_sw_hw_enum_ctl,
						    i, 1, s,
						    &private->sw_hw_ctls[i]);
			if (err < 0)
				return err;

			/* Make the switch read-only if the line is
			 * involved in speaker switching
			 */
			if (private->speaker_switching_switch && i < 4)
				scarlett2_sw_hw_ctl_ro(private, i);
		}
	}

	/* Add dim/mute controls */
	if (scarlett2_has_config_item(private, SCARLETT2_CONFIG_DIM_MUTE))
		for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
			err = scarlett2_add_new_ctl(
				mixer, &scarlett2_dim_mute_ctl,
				i, 1, scarlett2_dim_mute_names[i],
				&private->dim_mute_ctls[i]);
			if (err < 0)
				return err;
		}

	return 0;
}

/*** Create the analogue input controls ***/

static int scarlett2_add_dsp_ctls(struct usb_mixer_interface *mixer, int i)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int j, err;
	char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
	const char *compr_fmt = "Line In %d Compressor %s";
	const char *flt_switch_fmt = "Line In %d %s Filter Enable";
	const char *flt_fmt = "Line In %d %s Coefficients %d";

	/* Add compressor controls */
	for (j = 0; j < SCARLETT2_COMPRESSOR_PARAM_COUNT; j++) {
		const struct compressor_param *param = &compressor_params[j];
		int idx = i * SCARLETT2_COMPRESSOR_PARAM_COUNT + j;

		scnprintf(s, sizeof(s), compr_fmt, i + 1, param->name);
		err = scarlett2_add_new_ctl(
			mixer, &scarlett2_compressor_ctl,
			i * SCARLETT2_COMPRESSOR_PARAM_COUNT + j,
			1, s, &private->compressor_ctls[idx]);
		if (err < 0)
			return err;
	}

	/* Add filter enable controls */
	scnprintf(s, sizeof(s), flt_switch_fmt, i + 1, "Pre-Comp");
	err = scarlett2_add_new_ctl(
		mixer, &scarlett2_precomp_flt_switch_ctl,
		i, 1, s, &private->precomp_flt_switch_ctls[i]);
	if (err < 0)
		return err;

	scnprintf(s, sizeof(s), flt_switch_fmt, i + 1, "PEQ");
	err = scarlett2_add_new_ctl(
		mixer, &scarlett2_peq_flt_switch_ctl,
		i, 1, s, &private->peq_flt_switch_ctls[i]);
	if (err < 0)
		return err;

	/* Add filter coefficient controls */
	for (j = 0; j < info->precomp_flt_count; j++) {
		scnprintf(s, sizeof(s), flt_fmt, i + 1, "Pre-Comp", j + 1);
		err = scarlett2_add_new_ctl(
			mixer, &scarlett2_precomp_flt_ctl,
			i * info->precomp_flt_count + j,
			1, s, &private->precomp_flt_switch_ctls[j]);
		if (err < 0)
			return err;
	}

	for (j = 0; j < info->peq_flt_count; j++) {
		scnprintf(s, sizeof(s), flt_fmt, i + 1, "PEQ", j + 1);
		err = scarlett2_add_new_ctl(
			mixer, &scarlett2_peq_flt_ctl,
			i * info->peq_flt_count + j,
			1, s, &private->peq_flt_switch_ctls[j]);
		if (err < 0)
			return err;
	}

	return 0;
}

static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int err, i;
	char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
	const char *fmt = "Line In %d %s Capture %s";
	const char *fmt2 = "Line In %d-%d %s Capture %s";

	/* Add input level (line/inst) controls */
	for (i = 0; i < info->level_input_count; i++) {
		scnprintf(s, sizeof(s), fmt, i + 1 + info->level_input_first,
			  "Level", "Enum");
		err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
					    i, 1, s, &private->level_ctls[i]);
		if (err < 0)
			return err;
	}

	/* Add input pad controls */
	for (i = 0; i < info->pad_input_count; i++) {
		scnprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
		err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
					    i, 1, s, &private->pad_ctls[i]);
		if (err < 0)
			return err;
	}

	/* Add input air controls */
	for (i = 0; i < info->air_input_count; i++) {
		scnprintf(s, sizeof(s), fmt, i + 1 + info->air_input_first,
			  "Air", info->air_option ? "Enum" : "Switch");
		err = scarlett2_add_new_ctl(
			mixer, &scarlett2_air_ctl[info->air_option],
			i, 1, s, &private->air_ctls[i]);
		if (err < 0)
			return err;
	}

	/* Add input DSP controls */
	for (i = 0; i < info->dsp_input_count; i++) {
		scnprintf(s, sizeof(s), fmt, i + 1, "DSP", "Switch");
		err = scarlett2_add_new_ctl(mixer, &scarlett2_dsp_ctl,
					    i, 1, s, &private->dsp_ctls[i]);
		if (err < 0)
			return err;

		err = scarlett2_add_dsp_ctls(mixer, i);
		if (err < 0)
			return err;
	}

	/* Add input mute controls */
	for (i = 0; i < info->mute_input_count; i++) {
		scnprintf(s, sizeof(s), fmt, i + 1, "Mute", "Switch");
		err = scarlett2_add_new_ctl(
			mixer, &scarlett2_input_mute_ctl,
			i, 1, s, &private->input_mute_ctls[i]);
		if (err < 0)
			return err;
	}

	/* Add input phantom controls */
	if (info->inputs_per_phantom == 1) {
		for (i = 0; i < info->phantom_count; i++) {
			scnprintf(s, sizeof(s), fmt,
				  i + 1 + info->phantom_first,
				  "Phantom Power", "Switch");
			err = scarlett2_add_new_ctl(
				mixer, &scarlett2_phantom_ctl,
				i, 1, s, &private->phantom_ctls[i]);
			if (err < 0)
				return err;
		}
	} else if (info->inputs_per_phantom > 1) {
		for (i = 0; i < info->phantom_count; i++) {
			int from = i * info->inputs_per_phantom + 1;
			int to = (i + 1) * info->inputs_per_phantom;

			scnprintf(s, sizeof(s), fmt2, from, to,
				  "Phantom Power", "Switch");
			err = scarlett2_add_new_ctl(
				mixer, &scarlett2_phantom_ctl,
				i, 1, s, &private->phantom_ctls[i]);
			if (err < 0)
				return err;
		}
	}
	if (info->phantom_count &&
	    scarlett2_has_config_item(private,
				      SCARLETT2_CONFIG_PHANTOM_PERSISTENCE)) {
		err = scarlett2_add_new_ctl(
			mixer, &scarlett2_phantom_persistence_ctl, 0, 1,
			"Phantom Power Persistence Capture Switch", NULL);
		if (err < 0)
			return err;
	}

	/* Add input select/link controls */
	if (scarlett2_has_config_item(private,
				      SCARLETT2_CONFIG_INPUT_SELECT_SWITCH)) {
		err = scarlett2_add_new_ctl(
			mixer, &scarlett2_input_select_ctl, 0, 1,
			"Input Select Capture Enum",
			&private->input_select_ctl);
		if (err < 0)
			return err;
	}

	if (scarlett2_has_config_item(private,
				      SCARLETT2_CONFIG_INPUT_LINK_SWITCH)) {
		for (i = 0; i < info->gain_input_count / 2; i++) {
			scnprintf(s, sizeof(s),
				  "Line In %d-%d Link Capture Switch",
				  (i * 2) + 1, (i * 2) + 2);
			err = scarlett2_add_new_ctl(
				mixer, &scarlett2_input_link_ctl,
				i, 1, s, &private->input_link_ctls[i]);
			if (err < 0)
				return err;
		}
	}

	/* Add software-controllable input gain controls */
	for (i = 0; i < info->gain_input_count; i++) {
		scnprintf(s, sizeof(s), fmt, i + 1,
			  "Gain", "Volume");
		err = scarlett2_add_new_ctl(
			mixer, &scarlett2_input_gain_ctl,
			i, 1, s, &private->input_gain_ctls[i]);
		if (err < 0)
			return err;
		private->input_gain_ctls[i]->tlv.p =
			private->config_set->input_gain_tlv;

		scnprintf(s, sizeof(s), fmt, i + 1,
			  "Autogain", "Switch");
		err = scarlett2_add_new_ctl(
			mixer, &scarlett2_autogain_switch_ctl,
			i, 1, s, &private->autogain_ctls[i]);
		if (err < 0)
			return err;

		scnprintf(s, sizeof(s), fmt, i + 1,
			  "Autogain Status", "Enum");
		err = scarlett2_add_new_ctl(
			mixer, &scarlett2_autogain_status_ctl,
			i, 1, s, &private->autogain_status_ctls[i]);
	}

	/* Add autogain target controls */
	for (i = 0; i < SCARLETT2_AG_TARGET_COUNT; i++)
		if (scarlett2_has_config_item(private,
					      scarlett2_ag_target_configs[i])) {

			scnprintf(s, sizeof(s), "Autogain %s Target",
				  scarlett2_ag_target_names[i]);
			err = scarlett2_add_new_ctl(
				mixer, &scarlett2_ag_target_ctl,
				i, 1, s, &private->ag_target_ctls[i]);
			if (err < 0)
				return err;
		}

	/* Add safe-mode input switch controls */
	for (i = 0; i < info->safe_input_count; i++) {
		scnprintf(s, sizeof(s), fmt, i + 1,
			  "Safe", "Switch");
		err = scarlett2_add_new_ctl(
			mixer, &scarlett2_safe_ctl,
			i, 1, s, &private->safe_ctls[i]);
		if (err < 0)
			return err;
	}

	/* Add PCM Input Switch control */
	if (scarlett2_has_config_item(private,
				      SCARLETT2_CONFIG_PCM_INPUT_SWITCH)) {
		err = scarlett2_add_new_ctl(
			mixer, &scarlett2_pcm_input_switch_ctl, 0, 1,
			"PCM Input Capture Switch",
			&private->pcm_input_switch_ctl);
		if (err < 0)
			return err;
	}

	return 0;
}

/*** Mixer Volume Controls ***/

static int scarlett2_update_mix(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	int i, err;

	private->mix_updated = 0;

	for (i = 0; i < private->num_mix_out; i++) {
		err = scarlett2_usb_get_mix(mixer, i);
		if (err < 0)
			return err;
	}

	return 1;
}

static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
				    struct snd_ctl_elem_info *uinfo)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;

	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = elem->channels;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
	uinfo->value.integer.step = 1;
	return 0;
}

static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
				   struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->mix_updated) {
		err = scarlett2_update_mix(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.integer.value[0] = private->mix[elem->control];

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
				   struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int oval, val, mix_num, err = 0;
	int index = elem->control;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->mix[index];
	val = clamp(ucontrol->value.integer.value[0],
		    0L, (long)SCARLETT2_MIXER_MAX_VALUE);
	mix_num = index / private->num_mix_in;

	if (oval == val)
		goto unlock;

	private->mix[index] = val;
	err = scarlett2_usb_set_mix(mixer, mix_num);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const DECLARE_TLV_DB_MINMAX(
	db_scale_scarlett2_mixer,
	SCARLETT2_MIXER_MIN_DB * 100,
	SCARLETT2_MIXER_MAX_DB * 100
);

static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
	.name = "",
	.info = scarlett2_mixer_ctl_info,
	.get  = scarlett2_mixer_ctl_get,
	.put  = scarlett2_mixer_ctl_put,
	.private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
	.tlv = { .p = db_scale_scarlett2_mixer }
};

static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	int err, i, j;
	int index;
	char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];

	for (i = 0, index = 0; i < private->num_mix_out; i++)
		for (j = 0; j < private->num_mix_in; j++, index++) {
			snprintf(s, sizeof(s),
				 "Mix %c Input %02d Playback Volume",
				 'A' + i, j + 1);
			err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
						    index, 1, s,
						    &private->mix_ctls[index]);
			if (err < 0)
				return err;
		}

	return 0;
}

/*** Direct Monitor Control ***/

static int scarlett2_update_direct_monitor(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;

	private->direct_monitor_updated = 0;

	if (!private->info->direct_monitor)
		return 0;

	return scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_DIRECT_MONITOR,
		1, &private->direct_monitor_switch);
}

static int scarlett2_update_monitor_mix(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	int err, i;
	u16 mix_values[SCARLETT2_MONITOR_MIX_MAX];

	if (!private->num_monitor_mix_ctls)
		return 0;

	err = scarlett2_usb_get_config(
		mixer, SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN,
		private->num_monitor_mix_ctls, mix_values);
	if (err < 0)
		return err;

	for (i = 0; i < private->num_monitor_mix_ctls; i++)
		private->monitor_mix[i] = scarlett2_mixer_value_to_db(
			mix_values[i]);

	return 0;
}

static int scarlett2_direct_monitor_ctl_get(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->direct_monitor_updated) {
		err = scarlett2_update_direct_monitor(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.enumerated.item[0] = private->direct_monitor_switch;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_direct_monitor_ctl_put(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int index = elem->control;
	int oval, val, err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->direct_monitor_switch;
	val = min(ucontrol->value.enumerated.item[0], 2U);

	if (oval == val)
		goto unlock;

	private->direct_monitor_switch = val;

	/* Send switch change to the device */
	err = scarlett2_usb_set_config(
		mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, index, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_direct_monitor_stereo_enum_ctl_info(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
{
	static const char *const values[3] = {
		"Off", "Mono", "Stereo"
	};

	return snd_ctl_enum_info(uinfo, 1, 3, values);
}

/* Direct Monitor for Solo is mono-only and only needs a boolean control
 * Direct Monitor for 2i2 is selectable between Off/Mono/Stereo
 */
static const struct snd_kcontrol_new scarlett2_direct_monitor_ctl[2] = {
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "",
		.info = snd_ctl_boolean_mono_info,
		.get  = scarlett2_direct_monitor_ctl_get,
		.put  = scarlett2_direct_monitor_ctl_put,
	},
	{
		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
		.name = "",
		.info = scarlett2_direct_monitor_stereo_enum_ctl_info,
		.get  = scarlett2_direct_monitor_ctl_get,
		.put  = scarlett2_direct_monitor_ctl_put,
	}
};

static int scarlett2_monitor_mix_ctl_get(struct snd_kcontrol *kctl,
					 struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct scarlett2_data *private = elem->head.mixer->private_data;

	ucontrol->value.integer.value[0] = private->monitor_mix[elem->control];

	return 0;
}

static int scarlett2_monitor_mix_ctl_put(struct snd_kcontrol *kctl,
					 struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int oval, val, err = 0;
	int index = elem->control;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->monitor_mix[index];
	val = clamp(ucontrol->value.integer.value[0],
		    0L, (long)SCARLETT2_MIXER_MAX_VALUE);

	if (oval == val)
		goto unlock;

	private->monitor_mix[index] = val;
	err = scarlett2_usb_set_config(
		mixer, SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN,
		index, scarlett2_mixer_values[val]);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_monitor_mix_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
	.name = "",
	.info = scarlett2_mixer_ctl_info,
	.get  = scarlett2_monitor_mix_ctl_get,
	.put  = scarlett2_monitor_mix_ctl_put,
	.private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
	.tlv = { .p = db_scale_scarlett2_mixer }
};

static int scarlett2_add_direct_monitor_ctls(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	const char *s;
	int err, i, j, k, index;

	if (!info->direct_monitor)
		return 0;

	s = info->direct_monitor == 1
	      ? "Direct Monitor Playback Switch"
	      : "Direct Monitor Playback Enum";

	err = scarlett2_add_new_ctl(
		mixer, &scarlett2_direct_monitor_ctl[info->direct_monitor - 1],
		0, 1, s, &private->direct_monitor_ctl);
	if (err < 0)
		return err;

	if (!private->num_monitor_mix_ctls)
		return 0;

	/* 1 or 2 direct monitor selections (Mono & Stereo) */
	for (i = 0, index = 0; i < info->direct_monitor; i++) {
		const char * const format =
			"Monitor %sMix %c Input %02d Playback Volume";
		const char *mix_type;

		if (info->direct_monitor == 1)
			mix_type = "";
		else if (i == 0)
			mix_type = "1 ";
		else
			mix_type = "2 ";

		/* 2 Mix outputs, A/Left & B/Right */
		for (j = 0; j < 2; j++)

			/* Mix inputs */
			for (k = 0; k < private->num_mix_in; k++, index++) {
				char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];

				scnprintf(name, sizeof(name), format,
					  mix_type, 'A' + j, k + 1);

				err = scarlett2_add_new_ctl(
					mixer, &scarlett2_monitor_mix_ctl,
					index, 1, name, NULL);
				if (err < 0)
					return err;
			}
	}

	return 0;
}

/*** Mux Source Selection Controls ***/

static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
					   struct snd_ctl_elem_info *uinfo)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct scarlett2_data *private = elem->head.mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
	unsigned int item = uinfo->value.enumerated.item;
	int items = private->num_mux_srcs;
	int port_type;

	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = elem->channels;
	uinfo->value.enumerated.items = items;

	if (item >= items)
		item = uinfo->value.enumerated.item = items - 1;

	for (port_type = 0;
	     port_type < SCARLETT2_PORT_TYPE_COUNT;
	     port_type++) {
		if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
			const struct scarlett2_port *port =
				&scarlett2_ports[port_type];

			if (port_type == SCARLETT2_PORT_TYPE_MIX &&
			    item >= private->num_mix_out)
				sprintf(uinfo->value.enumerated.name,
					port->dsp_src_descr,
					item - private->num_mix_out + 1);
			else
				sprintf(uinfo->value.enumerated.name,
					port->src_descr,
					item + port->src_num_offset);

			return 0;
		}
		item -= port_count[port_type][SCARLETT2_PORT_IN];
	}

	return -EINVAL;
}

static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
					  struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int index = line_out_remap(private, elem->control);
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->mux_updated) {
		err = scarlett2_usb_get_mux(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.enumerated.item[0] = private->mux[index];

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
					  struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int index = line_out_remap(private, elem->control);
	int oval, val, err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->mux[index];
	val = min(ucontrol->value.enumerated.item[0],
		  private->num_mux_srcs - 1U);

	if (oval == val)
		goto unlock;

	private->mux[index] = val;
	err = scarlett2_usb_set_mux(mixer);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = scarlett2_mux_src_enum_ctl_info,
	.get  = scarlett2_mux_src_enum_ctl_get,
	.put  = scarlett2_mux_src_enum_ctl_put,
};

static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
	int port_type, channel, i;

	for (i = 0, port_type = 0;
	     port_type < SCARLETT2_PORT_TYPE_COUNT;
	     port_type++) {
		for (channel = 0;
		     channel < port_count[port_type][SCARLETT2_PORT_OUT];
		     channel++, i++) {
			int err;
			char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
			int channel_num = channel + 1;
			const struct scarlett2_port *port =
				&scarlett2_ports[port_type];
			const char *descr = port->dst_descr;

			if (port_type == SCARLETT2_PORT_TYPE_MIX &&
			    channel >= private->num_mix_in) {
				channel_num -= private->num_mix_in;
				descr = port->dsp_dst_descr;
			}

			snprintf(s, sizeof(s) - 5, descr, channel_num);
			strcat(s, " Enum");

			err = scarlett2_add_new_ctl(mixer,
						    &scarlett2_mux_src_enum_ctl,
						    i, 1, s,
						    &private->mux_ctls[i]);
			if (err < 0)
				return err;
		}
	}

	return 0;
}

/*** Meter Controls ***/

static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
				    struct snd_ctl_elem_info *uinfo)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;

	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = elem->channels;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 4095;
	uinfo->value.integer.step = 1;
	return 0;
}

static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
				   struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	u8 *meter_level_map = private->meter_level_map;
	u16 meter_levels[SCARLETT2_MAX_METERS];
	int i, err;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	err = scarlett2_usb_get_meter_levels(mixer, elem->channels,
					     meter_levels);
	if (err < 0)
		goto unlock;

	/* copy & translate from meter_levels[] using meter_level_map[] */
	for (i = 0; i < elem->channels; i++) {
		int idx = meter_level_map[i];
		int value;

		if (idx == 255)
			value = 0;
		else
			value = meter_levels[idx];

		ucontrol->value.integer.value[i] = value;
	}

unlock:
	mutex_unlock(&private->data_mutex);

	return err;
}

static const struct snd_kcontrol_new scarlett2_meter_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
	.name = "",
	.info = scarlett2_meter_ctl_info,
	.get  = scarlett2_meter_ctl_get
};

static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;

	/* devices without a mixer also don't support reporting levels */
	if (!scarlett2_has_mixer(private))
		return 0;

	return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
				     0, private->num_mux_dsts,
				     "Level Meter", NULL);
}

/*** MSD Controls ***/

static int scarlett2_msd_ctl_get(struct snd_kcontrol *kctl,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct scarlett2_data *private = elem->head.mixer->private_data;

	ucontrol->value.integer.value[0] = private->msd_switch;
	return 0;
}

static int scarlett2_msd_ctl_put(struct snd_kcontrol *kctl,
				 struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int oval, val, err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->msd_switch;
	val = !!ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->msd_switch = val;

	/* Send switch change to the device */
	err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MSD_SWITCH,
				       0, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_msd_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = snd_ctl_boolean_mono_info,
	.get  = scarlett2_msd_ctl_get,
	.put  = scarlett2_msd_ctl_put,
};

static int scarlett2_add_msd_ctl(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;

	if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_MSD_SWITCH))
		return 0;

	/* If MSD mode is off, hide the switch by default */
	if (!private->msd_switch && !(mixer->chip->setup & SCARLETT2_MSD_ENABLE))
		return 0;

	/* Add MSD control */
	return scarlett2_add_new_ctl(mixer, &scarlett2_msd_ctl,
				     0, 1, "MSD Mode Switch", NULL);
}

/*** Standalone Control ***/

static int scarlett2_standalone_ctl_get(struct snd_kcontrol *kctl,
					struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct scarlett2_data *private = elem->head.mixer->private_data;

	ucontrol->value.integer.value[0] = private->standalone_switch;
	return 0;
}

static int scarlett2_standalone_ctl_put(struct snd_kcontrol *kctl,
					struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;

	int oval, val, err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->standalone_switch;
	val = !!ucontrol->value.integer.value[0];

	if (oval == val)
		goto unlock;

	private->standalone_switch = val;

	/* Send switch change to the device */
	err = scarlett2_usb_set_config(mixer,
				       SCARLETT2_CONFIG_STANDALONE_SWITCH,
				       0, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_standalone_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = snd_ctl_boolean_mono_info,
	.get  = scarlett2_standalone_ctl_get,
	.put  = scarlett2_standalone_ctl_put,
};

static int scarlett2_add_standalone_ctl(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;

	if (!scarlett2_has_config_item(private,
				       SCARLETT2_CONFIG_STANDALONE_SWITCH))
		return 0;

	/* Add standalone control */
	return scarlett2_add_new_ctl(mixer, &scarlett2_standalone_ctl,
				     0, 1, "Standalone Switch", NULL);
}

/*** Power Status ***/

static int scarlett2_update_power_status(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	int err;
	u8 power_ext, power_low;

	private->power_status_updated = 0;

	err = scarlett2_usb_get_config(mixer, SCARLETT2_CONFIG_POWER_EXT,
				       1, &power_ext);
	if (err < 0)
		return err;

	err = scarlett2_usb_get_config(mixer, SCARLETT2_CONFIG_POWER_LOW,
				       1, &power_low);
	if (err < 0)
		return err;

	if (power_low)
		private->power_status = SCARLETT2_POWER_STATUS_FAIL;
	else if (power_ext)
		private->power_status = SCARLETT2_POWER_STATUS_EXT;
	else
		private->power_status = SCARLETT2_POWER_STATUS_BUS;

	return 0;
}

static int scarlett2_power_status_ctl_get(struct snd_kcontrol *kctl,
					  struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->power_status_updated) {
		err = scarlett2_update_power_status(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.integer.value[0] = private->power_status;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_power_status_ctl_info(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
{
	static const char *const values[3] = {
		"External", "Bus", "Fail"
	};

	return snd_ctl_enum_info(uinfo, 1, 3, values);
}

static const struct snd_kcontrol_new scarlett2_power_status_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
	.access = SNDRV_CTL_ELEM_ACCESS_READ,
	.name = "",
	.info = scarlett2_power_status_ctl_info,
	.get  = scarlett2_power_status_ctl_get,
};

static int scarlett2_add_power_status_ctl(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;

	if (!scarlett2_has_config_item(private,
				       SCARLETT2_CONFIG_POWER_EXT))
		return 0;

	/* Add power status control */
	return scarlett2_add_new_ctl(mixer, &scarlett2_power_status_ctl,
				     0, 1, "Power Status Card Enum",
				     &private->power_status_ctl);
}

/*** Bluetooth Volume ***/

static int scarlett2_update_bluetooth_volume(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	int err;

	private->bluetooth_updated = 0;

	if (!private->info->has_bluetooth)
		return 0;

	err = scarlett2_usb_get_config(mixer,
				       SCARLETT2_CONFIG_BLUETOOTH_VOLUME,
				       1, &private->bluetooth_volume);
	if (err < 0)
		return err;

	return 0;
}

static int scarlett2_bluetooth_volume_ctl_get(struct snd_kcontrol *kctl,
					     struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	if (private->bluetooth_updated) {
		err = scarlett2_update_bluetooth_volume(mixer);
		if (err < 0)
			goto unlock;
	}
	ucontrol->value.integer.value[0] = private->bluetooth_volume;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_bluetooth_volume_ctl_put(struct snd_kcontrol *kctl,
					     struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int oval, val, err = 0;

	mutex_lock(&private->data_mutex);

	if (private->hwdep_in_use) {
		err = -EBUSY;
		goto unlock;
	}

	oval = private->bluetooth_volume;
	val = clamp(ucontrol->value.integer.value[0],
		    0L, (long)SCARLETT2_MAX_BLUETOOTH_VOLUME);

	if (oval == val)
		goto unlock;

	private->bluetooth_volume = val;
	err = scarlett2_usb_set_config(mixer,
				       SCARLETT2_CONFIG_BLUETOOTH_VOLUME,
				       0, val);
	if (err == 0)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static int scarlett2_bluetooth_volume_ctl_info(
	struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = SCARLETT2_MAX_BLUETOOTH_VOLUME;
	uinfo->value.integer.step = 1;
	return 0;
}

static const struct snd_kcontrol_new scarlett2_bluetooth_volume_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = scarlett2_bluetooth_volume_ctl_info,
	.get  = scarlett2_bluetooth_volume_ctl_get,
	.put  = scarlett2_bluetooth_volume_ctl_put,
};

static int scarlett2_add_bluetooth_volume_ctl(
	struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;

	if (!private->info->has_bluetooth)
		return 0;

	/* Add Bluetooth volume control */
	return scarlett2_add_new_ctl(mixer, &scarlett2_bluetooth_volume_ctl,
				     0, 1, "Bluetooth Capture Volume",
				     &private->bluetooth_volume_ctl);
}

/*** S/PDIF Mode Controls ***/

static int scarlett2_update_spdif_mode(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	int err, i;
	u8 mode;
	const u8 *mode_values = private->info->spdif_mode_values;

	if (!private->info->spdif_mode_control_name)
		return 0;

	err = scarlett2_usb_get_config(mixer, SCARLETT2_CONFIG_SPDIF_MODE,
				       1, &mode);
	if (err < 0)
		return err;

	private->spdif_mode = 0;

	for (i = 0; *mode_values != 0xff; i++, mode_values++)
		if (*mode_values == mode) {
			private->spdif_mode = i;
			break;
		}

	return 0;
}

static int scarlett2_spdif_mode_ctl_info(struct snd_kcontrol *kctl,
					   struct snd_ctl_elem_info *uinfo)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct scarlett2_data *private = elem->head.mixer->private_data;
	const char * const *mode_texts = private->info->spdif_mode_texts;
	int count = 0;

	while (*mode_texts++)
		count++;

	return snd_ctl_enum_info(uinfo, 1, count,
				 private->info->spdif_mode_texts);
}

static int scarlett2_spdif_mode_ctl_get(struct snd_kcontrol *kctl,
					  struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct scarlett2_data *private = elem->head.mixer->private_data;

	ucontrol->value.enumerated.item[0] = private->spdif_mode;
	return 0;
}

static int scarlett2_spdif_mode_ctl_put(struct snd_kcontrol *kctl,
					  struct snd_ctl_elem_value *ucontrol)
{
	struct usb_mixer_elem_info *elem = kctl->private_data;
	struct usb_mixer_interface *mixer = elem->head.mixer;
	struct scarlett2_data *private = mixer->private_data;
	int oval, val, err = 0;
	int i;

	mutex_lock(&private->data_mutex);

	oval = private->spdif_mode;
	val = ucontrol->value.enumerated.item[0];

	if (val < 0) {
		err = -EINVAL;
		goto unlock;
	}

	for (i = 0; i <= val; i++)
		if (private->info->spdif_mode_values[i] == 0xff) {
			err = -EINVAL;
			goto unlock;
		}

	if (oval == val)
		goto unlock;

	private->spdif_mode = val;

	err = scarlett2_usb_set_config(
		mixer, SCARLETT2_CONFIG_SPDIF_MODE, 0,
		private->info->spdif_mode_values[val]);
	if (!err)
		err = 1;

unlock:
	mutex_unlock(&private->data_mutex);
	return err;
}

static const struct snd_kcontrol_new scarlett2_spdif_mode_ctl = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "",
	.info = scarlett2_spdif_mode_ctl_info,
	.get  = scarlett2_spdif_mode_ctl_get,
	.put  = scarlett2_spdif_mode_ctl_put,
};

static int scarlett2_add_spdif_mode_ctl(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;

	if (!private->info->spdif_mode_control_name)
		return 0;

	return scarlett2_add_new_ctl(mixer, &scarlett2_spdif_mode_ctl,
				     0, 1,
				     private->info->spdif_mode_control_name,
				     NULL);
}

/*** Notification Handlers ***/

/* Notify on sync change */
static void scarlett2_notify_sync(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;

	private->sync_updated = 1;

	snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
		       &private->sync_ctl->id);
}

/* Notify on monitor change (Gen 2/3) */
static void scarlett2_notify_monitor(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	int i;

	if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
		return;

	private->vol_updated = 1;

	snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
		       &private->master_vol_ctl->id);

	for (i = 0; i < private->num_line_out; i++)
		if (private->vol_sw_hw_switch[line_out_remap(private, i)])
			snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
				       &private->vol_ctls[i]->id);
}

/* Notify on volume change (Gen 4) */
static void scarlett2_notify_volume(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;

	private->vol_updated = 1;

	snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
		       &private->master_vol_ctl->id);
	snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
		       &private->headphone_vol_ctl->id);
}

/* Notify on dim/mute change */
static void scarlett2_notify_dim_mute(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	int i;

	if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
		return;

	private->dim_mute_updated = 1;

	for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
			       &private->dim_mute_ctls[i]->id);

	for (i = 0; i < private->num_line_out; i++)
		if (private->vol_sw_hw_switch[line_out_remap(private, i)])
			snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
				       &private->mute_ctls[i]->id);
}

/* Notify on input level switch change */
static void scarlett2_notify_input_level(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int i;

	private->input_level_updated = 1;

	for (i = 0; i < info->level_input_count; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
			       &private->level_ctls[i]->id);
}

/* Notify on input pad switch change */
static void scarlett2_notify_input_pad(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int i;

	private->input_pad_updated = 1;

	for (i = 0; i < info->pad_input_count; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
			       &private->pad_ctls[i]->id);
}

/* Notify on input air switch change */
static void scarlett2_notify_input_air(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int i;

	private->input_air_updated = 1;

	for (i = 0; i < info->air_input_count; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
			       &private->air_ctls[i]->id);
}

/* Notify on input DSP switch change */
static void scarlett2_notify_input_dsp(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int i;

	private->input_dsp_updated = 1;

	for (i = 0; i < info->dsp_input_count; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
			       &private->dsp_ctls[i]->id);
}

/* Notify on input mute switch change */
static void scarlett2_notify_input_mute(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int i;

	private->input_mute_updated = 1;

	for (i = 0; i < info->mute_input_count; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
			       &private->input_mute_ctls[i]->id);
}

/* Notify on input phantom switch change */
static void scarlett2_notify_input_phantom(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int i;

	private->input_phantom_updated = 1;

	for (i = 0; i < info->phantom_count; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
			       &private->phantom_ctls[i]->id);

	scarlett2_phantom_notify_access(mixer);
}

/* Notify on "input other" change (level/pad/air/phantom) */
static void scarlett2_notify_input_other(struct usb_mixer_interface *mixer)
{
	scarlett2_notify_input_level(mixer);
	scarlett2_notify_input_pad(mixer);
	scarlett2_notify_input_air(mixer);
	scarlett2_notify_input_phantom(mixer);
}

/* Notify on input select change */
static void scarlett2_notify_input_select(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int i;

	if (!scarlett2_has_config_item(private,
				       SCARLETT2_CONFIG_INPUT_SELECT_SWITCH))
		return;

	private->input_select_updated = 1;

	snd_ctl_notify(card,
		       SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
		       &private->input_select_ctl->id);

	for (i = 0; i < info->gain_input_count / 2; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
			       &private->input_link_ctls[i]->id);
}

/* Notify on input gain change */
static void scarlett2_notify_input_gain(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int i;

	if (!info->gain_input_count)
		return;

	private->input_gain_updated = 1;

	for (i = 0; i < info->gain_input_count; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
			       &private->input_gain_ctls[i]->id);
}

/* Notify on autogain change */
static void scarlett2_notify_autogain(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int i;

	if (!info->gain_input_count)
		return;

	private->autogain_updated = 1;

	for (i = 0; i < info->gain_input_count; i++) {
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
			       &private->autogain_ctls[i]->id);
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
			       &private->autogain_status_ctls[i]->id);
	}

	for (i = 0; i < SCARLETT2_AG_TARGET_COUNT; i++)
		if (scarlett2_has_config_item(private,
					      scarlett2_ag_target_configs[i]))
			snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
				       &private->ag_target_ctls[i]->id);

	scarlett2_autogain_notify_access(mixer);
}

/* Notify on input safe switch change */
static void scarlett2_notify_input_safe(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int i;

	if (!info->safe_input_count)
		return;

	private->input_safe_updated = 1;

	for (i = 0; i < info->safe_input_count; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
			       &private->safe_ctls[i]->id);
}

/* Notify on "monitor other" change (speaker switching, talkback) */
static void scarlett2_notify_monitor_other(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;

	private->monitor_other_updated = 1;

	if (info->has_speaker_switching)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
			       &private->speaker_switching_ctl->id);

	if (info->has_talkback)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
			       &private->talkback_ctl->id);

	/* if speaker switching was recently enabled or disabled,
	 * invalidate the dim/mute and mux enum controls
	 */
	if (private->speaker_switching_switched) {
		int i;

		scarlett2_notify_dim_mute(mixer);

		private->speaker_switching_switched = 0;
		private->mux_updated = 1;

		for (i = 0; i < private->num_mux_dsts; i++)
			snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
				       &private->mux_ctls[i]->id);
	}
}

/* Notify on direct monitor switch change */
static void scarlett2_notify_direct_monitor(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	int count = private->num_mix_in * private->num_mix_out;
	int i;

	private->direct_monitor_updated = 1;

	snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
		       &private->direct_monitor_ctl->id);

	if (!scarlett2_has_mixer(private))
		return;

	private->mix_updated = 1;

	/* Notify of change to the mix controls */
	for (i = 0; i < count; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
			       &private->mix_ctls[i]->id);
}

/* Notify on power change */
static void scarlett2_notify_power_status(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;

	private->power_status_updated = 1;

	snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
		       &private->power_status_ctl->id);
}

/* Notify on mux change */
static void scarlett2_notify_mux(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;
	int i;

	private->mux_updated = 1;

	for (i = 0; i < private->num_mux_dsts; i++)
		snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
			       &private->mux_ctls[i]->id);
}

/* Notify on PCM input switch change */
static void scarlett2_notify_pcm_input_switch(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;

	private->pcm_input_switch_updated = 1;

	snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
		       &private->pcm_input_switch_ctl->id);

	scarlett2_notify_mux(mixer);
}

/* Notify on Bluetooth change */
static void scarlett2_notify_bluetooth(struct usb_mixer_interface *mixer)
{
	struct snd_card *card = mixer->chip->card;
	struct scarlett2_data *private = mixer->private_data;

	if (!private->info->has_bluetooth)
		return;

	private->bluetooth_updated = 1;

	snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
		       &private->bluetooth_volume_ctl->id);
}

/* Handle acknowledgement that a command was received; let
 * scarlett2_usb() know that it can proceed
 */
static void scarlett2_notify_ack(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;

	/* if running == 0, ignore ACKs */
	if (private->running)
		complete(&private->cmd_done);
}

/* Interrupt callback */
static void scarlett2_notify(struct urb *urb)
{
	struct usb_mixer_interface *mixer = urb->context;
	int len = urb->actual_length;
	int ustatus = urb->status;
	u32 data;
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_notification *notifications =
		private->config_set->notifications;

	if (ustatus != 0 || len != 8)
		goto requeue;

	data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);

	/* Ignore notifications except ACK during initialisation.
	 * ACK is 0x00000001 on every device.
	 */
	if (private->running < 2)
		data &= 1;

	while (data && notifications->mask) {
		if (data & notifications->mask) {
			data &= ~notifications->mask;
			if (notifications->func)
				notifications->func(mixer);
		}
		notifications++;
	}

	if (data)
		usb_audio_warn(mixer->chip,
			       "%s: Unhandled notification: 0x%08x\n",
			       __func__, data);

requeue:
	if (ustatus != -ENOENT &&
	    ustatus != -ECONNRESET &&
	    ustatus != -ESHUTDOWN) {
		urb->dev = mixer->chip->dev;
		usb_submit_urb(urb, GFP_ATOMIC);
	} else {
		complete(&private->cmd_done);
	}
}

/*** Cleanup/Suspend Callbacks ***/

static void scarlett2_private_free(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;

	cancel_delayed_work_sync(&private->work);
	kfree(private);
	mixer->private_data = NULL;
}

static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;

	if (cancel_delayed_work_sync(&private->work))
		scarlett2_config_save(private->mixer);
}

/*** Initialisation ***/

static void scarlett2_count_io(struct scarlett2_data *private)
{
	const struct scarlett2_device_info *info = private->info;
	const struct scarlett2_config_set *config_set = info->config_set;
	const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
	int port_type, srcs = 0, dsts = 0, i;

	/* Count the number of mux sources and destinations */
	for (port_type = 0;
	     port_type < SCARLETT2_PORT_TYPE_COUNT;
	     port_type++) {
		srcs += port_count[port_type][SCARLETT2_PORT_IN];
		dsts += port_count[port_type][SCARLETT2_PORT_OUT];
	}

	private->num_mux_srcs = srcs;
	private->num_mux_dsts = dsts;

	/* Mixer inputs are mux outputs and vice versa.
	 * Scarlett Gen 4 DSP I/O uses SCARLETT2_PORT_TYPE_MIX but
	 * doesn't have mixer controls.
	 */
	private->num_mix_in =
		port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT] -
			info->dsp_count;

	private->num_mix_out =
		port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN] -
			info->dsp_count;

	/* Number of analogue line outputs */
	private->num_line_out =
		port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];

	/* Number of monitor mix controls */
	private->num_monitor_mix_ctls =
		info->direct_monitor * 2 * private->num_mix_in;

	/* Number of autogain status texts */
	if (config_set->autogain_status_texts) {
		const char * const *texts = config_set->autogain_status_texts;

		for (i = 0; texts[i]; i++)
			;
		private->num_autogain_status_texts = i;
	}
}

/* Look through the interface descriptors for the Focusrite Control
 * interface (bInterfaceClass = 255 Vendor Specific Class) and set
 * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
 * in private
 */
static int scarlett2_find_fc_interface(struct usb_device *dev,
				       struct scarlett2_data *private)
{
	struct usb_host_config *config = dev->actconfig;
	int i;

	for (i = 0; i < config->desc.bNumInterfaces; i++) {
		struct usb_interface *intf = config->interface[i];
		struct usb_interface_descriptor *desc =
			&intf->altsetting[0].desc;
		struct usb_endpoint_descriptor *epd;

		if (desc->bInterfaceClass != 255)
			continue;

		epd = get_endpoint(intf->altsetting, 0);
		private->bInterfaceNumber = desc->bInterfaceNumber;
		private->bEndpointAddress = epd->bEndpointAddress &
			USB_ENDPOINT_NUMBER_MASK;
		private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
		private->bInterval = epd->bInterval;
		return 0;
	}

	return -EINVAL;
}

/* Initialise private data */
static int scarlett2_init_private(struct usb_mixer_interface *mixer,
				  const struct scarlett2_device_entry *entry)
{
	struct scarlett2_data *private =
		kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);

	if (!private)
		return -ENOMEM;

	mutex_init(&private->usb_mutex);
	mutex_init(&private->data_mutex);
	INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);

	mixer->private_data = private;
	mixer->private_free = scarlett2_private_free;
	mixer->private_suspend = scarlett2_private_suspend;

	private->info = entry->info;
	private->config_set = entry->info->config_set;
	private->series_name = entry->series_name;
	scarlett2_count_io(private);
	private->scarlett2_seq = 0;
	private->mixer = mixer;

	return scarlett2_find_fc_interface(mixer->chip->dev, private);
}

/* Submit a URB to receive notifications from the device */
static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
{
	struct usb_device *dev = mixer->chip->dev;
	struct scarlett2_data *private = mixer->private_data;
	unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
	void *transfer_buffer;

	if (mixer->urb) {
		usb_audio_err(mixer->chip,
			      "%s: mixer urb already in use!\n", __func__);
		return 0;
	}

	if (usb_pipe_type_check(dev, pipe))
		return -EINVAL;

	mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!mixer->urb)
		return -ENOMEM;

	transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
	if (!transfer_buffer)
		return -ENOMEM;

	usb_fill_int_urb(mixer->urb, dev, pipe,
			 transfer_buffer, private->wMaxPacketSize,
			 scarlett2_notify, mixer, private->bInterval);

	init_completion(&private->cmd_done);

	return usb_submit_urb(mixer->urb, GFP_KERNEL);
}

/* Cargo cult proprietary initialisation sequence */
static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
{
	struct usb_device *dev = mixer->chip->dev;
	struct scarlett2_data *private = mixer->private_data;
	u8 step0_buf[24];
	u8 step2_buf[84];
	int err;

	if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
		return -EINVAL;

	/* step 0 */
	err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
			       SCARLETT2_USB_CMD_INIT,
			       step0_buf, sizeof(step0_buf));
	if (err < 0)
		return err;

	/* Set up the interrupt polling for notifications.
	 * When running is:
	 * 0: all notifications are ignored
	 * 1: only ACKs are handled
	 * 2: all notifications are handled
	 */
	err = scarlett2_init_notify(mixer);
	if (err < 0)
		return err;

	/* sleep for a moment in case of an outstanding ACK */
	msleep(20);

	/* start handling ACKs, but no other notifications until the
	 * ALSA controls have been created
	 */
	private->running = 1;

	/* step 1 */
	private->scarlett2_seq = 1;
	err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
	if (err < 0)
		return err;

	/* step 2 */
	private->scarlett2_seq = 1;
	err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_2,
			    NULL, 0,
			    step2_buf, sizeof(step2_buf));
	if (err < 0)
		return err;

	/* extract 4-byte firmware version from step2_buf[8] */
	private->firmware_version = le32_to_cpu(*(__le32 *)(step2_buf + 8));
	usb_audio_info(mixer->chip,
		       "Firmware version %d\n",
		       private->firmware_version);

	return 0;
}

/* Get the flash segment numbers for the App_Settings and App_Upgrade
 * segments and put them in the private data
 */
static int scarlett2_get_flash_segment_nums(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	int err, count, i;

	struct {
		__le32 size;
		__le32 count;
		u8 unknown[8];
	} __packed flash_info;

	struct {
		__le32 size;
		__le32 flags;
		char name[16];
	} __packed segment_info;

	err = scarlett2_usb(mixer, SCARLETT2_USB_INFO_FLASH,
			    NULL, 0,
			    &flash_info, sizeof(flash_info));
	if (err < 0)
		return err;

	count = le32_to_cpu(flash_info.count);

	/* sanity check count */
	if (count < SCARLETT2_SEGMENT_NUM_MIN ||
	    count > SCARLETT2_SEGMENT_NUM_MAX + 1) {
		usb_audio_err(mixer->chip,
			      "invalid flash segment count: %d\n", count);
		return -EINVAL;
	}

	for (i = 0; i < count; i++) {
		__le32 segment_num_req = cpu_to_le32(i);
		int flash_segment_id;

		err = scarlett2_usb(mixer, SCARLETT2_USB_INFO_SEGMENT,
				    &segment_num_req, sizeof(segment_num_req),
				    &segment_info, sizeof(segment_info));
		if (err < 0) {
			usb_audio_err(mixer->chip,
				"failed to get flash segment info %d: %d\n",
				i, err);
			return err;
		}

		if (!strncmp(segment_info.name,
			     SCARLETT2_SEGMENT_SETTINGS_NAME, 16))
			flash_segment_id = SCARLETT2_SEGMENT_ID_SETTINGS;
		else if (!strncmp(segment_info.name,
				  SCARLETT2_SEGMENT_FIRMWARE_NAME, 16))
			flash_segment_id = SCARLETT2_SEGMENT_ID_FIRMWARE;
		else
			continue;

		private->flash_segment_nums[flash_segment_id] = i;
		private->flash_segment_blocks[flash_segment_id] =
			le32_to_cpu(segment_info.size) /
				SCARLETT2_FLASH_BLOCK_SIZE;
	}

	/* segment 0 is App_Gold and we never want to touch that, so
	 * use 0 as the "not-found" value
	 */
	if (!private->flash_segment_nums[SCARLETT2_SEGMENT_ID_SETTINGS]) {
		usb_audio_err(mixer->chip,
			      "failed to find flash segment %s\n",
			      SCARLETT2_SEGMENT_SETTINGS_NAME);
		return -EINVAL;
	}
	if (!private->flash_segment_nums[SCARLETT2_SEGMENT_ID_FIRMWARE]) {
		usb_audio_err(mixer->chip,
			      "failed to find flash segment %s\n",
			      SCARLETT2_SEGMENT_FIRMWARE_NAME);
		return -EINVAL;
	}

	return 0;
}

/* Read configuration from the interface on start */
static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	const struct scarlett2_device_info *info = private->info;
	int err, i;

	if (scarlett2_has_config_item(private, SCARLETT2_CONFIG_MSD_SWITCH)) {
		err = scarlett2_usb_get_config(
			mixer, SCARLETT2_CONFIG_MSD_SWITCH,
			1, &private->msd_switch);
		if (err < 0)
			return err;
	}

	if (private->firmware_version < info->min_firmware_version) {
		usb_audio_err(mixer->chip,
			      "Focusrite %s firmware version %d is too old; "
			      "need %d",
			      private->series_name,
			      private->firmware_version,
			      info->min_firmware_version);
		return 0;
	}

	/* no other controls are created if MSD mode is on */
	if (private->msd_switch)
		return 0;

	err = scarlett2_update_input_level(mixer);
	if (err < 0)
		return err;

	err = scarlett2_update_input_pad(mixer);
	if (err < 0)
		return err;

	err = scarlett2_update_input_air(mixer);
	if (err < 0)
		return err;

	err = scarlett2_update_input_dsp(mixer);
	if (err < 0)
		return err;

	err = scarlett2_update_compressor_values(mixer);
	if (err < 0)
		return err;

	err = scarlett2_update_filter_values(mixer);
	if (err < 0)
		return err;

	err = scarlett2_update_input_mute(mixer);
	if (err < 0)
		return err;

	err = scarlett2_update_input_phantom(mixer);
	if (err < 0)
		return err;

	err = scarlett2_update_direct_monitor(mixer);
	if (err < 0)
		return err;

	/* the rest of the configuration is for devices with a mixer */
	if (!scarlett2_has_mixer(private))
		return 0;

	err = scarlett2_update_monitor_mix(mixer);
	if (err < 0)
		return err;

	err = scarlett2_update_monitor_other(mixer);
	if (err < 0)
		return err;

	if (scarlett2_has_config_item(private,
				      SCARLETT2_CONFIG_STANDALONE_SWITCH)) {
		err = scarlett2_usb_get_config(
			mixer, SCARLETT2_CONFIG_STANDALONE_SWITCH,
			1, &private->standalone_switch);
		if (err < 0)
			return err;
	}

	if (scarlett2_has_config_item(private,
				      SCARLETT2_CONFIG_POWER_EXT)) {
		err = scarlett2_update_power_status(mixer);
		if (err < 0)
			return err;
	}

	err = scarlett2_update_sync(mixer);
	if (err < 0)
		return err;

	if (scarlett2_has_config_item(private,
				      SCARLETT2_CONFIG_LINE_OUT_VOLUME)) {
		s16 sw_vol[SCARLETT2_ANALOGUE_MAX];

		/* read SW line out volume */
		err = scarlett2_usb_get_config(
			mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
			private->num_line_out, &sw_vol);
		if (err < 0)
			return err;

		for (i = 0; i < private->num_line_out; i++)
			private->vol[i] = clamp(
				sw_vol[i] + SCARLETT2_VOLUME_BIAS,
				0, SCARLETT2_VOLUME_BIAS);

		/* read SW mute */
		err = scarlett2_usb_get_config(
			mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
			private->num_line_out, &private->mute_switch);
		if (err < 0)
			return err;

		for (i = 0; i < private->num_line_out; i++)
			private->mute_switch[i] =
				!!private->mute_switch[i];

		/* read SW/HW switches */
		if (scarlett2_has_config_item(private,
					      SCARLETT2_CONFIG_SW_HW_SWITCH)) {
			err = scarlett2_usb_get_config(
				mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
				private->num_line_out,
				&private->vol_sw_hw_switch);
			if (err < 0)
				return err;

			for (i = 0; i < private->num_line_out; i++)
				private->vol_sw_hw_switch[i] =
					!!private->vol_sw_hw_switch[i];
		}
	}

	err = scarlett2_update_volumes(mixer);
	if (err < 0)
		return err;

	err = scarlett2_update_dim_mute(mixer);
	if (err < 0)
		return err;

	err = scarlett2_update_input_select(mixer);
	if (err < 0)
		return err;

	err = scarlett2_update_input_gain(mixer);
	if (err < 0)
		return err;

	err = scarlett2_update_autogain(mixer);
	if (err < 0)
		return err;

	err = scarlett2_update_input_safe(mixer);
	if (err < 0)
		return err;

	if (scarlett2_has_config_item(private,
				      SCARLETT2_CONFIG_PCM_INPUT_SWITCH)) {
		err = scarlett2_update_pcm_input_switch(mixer);
		if (err < 0)
			return err;
	}

	err = scarlett2_update_bluetooth_volume(mixer);
	if (err < 0)
		return err;

	err = scarlett2_update_spdif_mode(mixer);
	if (err < 0)
		return err;

	err = scarlett2_update_mix(mixer);
	if (err < 0)
		return err;

	return scarlett2_usb_get_mux(mixer);
}

static const struct scarlett2_device_entry *get_scarlett2_device_entry(
	struct usb_mixer_interface *mixer)
{
	const struct scarlett2_device_entry *entry = scarlett2_devices;

	/* Find entry in scarlett2_devices */
	while (entry->usb_id && entry->usb_id != mixer->chip->usb_id)
		entry++;
	if (!entry->usb_id)
		return NULL;

	return entry;
}

static int snd_scarlett2_controls_create(
	struct usb_mixer_interface *mixer,
	const struct scarlett2_device_entry *entry)
{
	struct scarlett2_data *private;
	int err;

	/* Initialise private data */
	err = scarlett2_init_private(mixer, entry);
	if (err < 0)
		return err;

	private = mixer->private_data;

	/* Send proprietary USB initialisation sequence */
	err = scarlett2_usb_init(mixer);
	if (err < 0)
		return err;

	/* Get the upgrade & settings flash segment numbers */
	err = scarlett2_get_flash_segment_nums(mixer);
	if (err < 0)
		return err;

	/* Add firmware version control */
	err = scarlett2_add_firmware_version_ctl(mixer);
	if (err < 0)
		return err;

	/* Add minimum firmware version control */
	err = scarlett2_add_min_firmware_version_ctl(mixer);
	if (err < 0)
		return err;

	/* Read volume levels and controls from the interface */
	err = scarlett2_read_configs(mixer);
	if (err < 0)
		return err;

	/* Create the MSD control */
	err = scarlett2_add_msd_ctl(mixer);
	if (err < 0)
		return err;

	/* If MSD mode is enabled, or if the firmware version is too
	 * old, don't create any other controls
	 */
	if (private->msd_switch ||
	    private->firmware_version < private->info->min_firmware_version)
		return 0;

	/* Create the analogue output controls */
	err = scarlett2_add_line_out_ctls(mixer);
	if (err < 0)
		return err;

	/* Create the analogue input controls */
	err = scarlett2_add_line_in_ctls(mixer);
	if (err < 0)
		return err;

	/* Create the input, output, and mixer mux input selections */
	err = scarlett2_add_mux_enums(mixer);
	if (err < 0)
		return err;

	/* Create the matrix mixer controls */
	err = scarlett2_add_mixer_ctls(mixer);
	if (err < 0)
		return err;

	/* Create the level meter controls */
	err = scarlett2_add_meter_ctl(mixer);
	if (err < 0)
		return err;

	/* Create the sync control */
	err = scarlett2_add_sync_ctl(mixer);
	if (err < 0)
		return err;

	/* Create the direct monitor control(s) */
	err = scarlett2_add_direct_monitor_ctls(mixer);
	if (err < 0)
		return err;

	/* Create the speaker switching control */
	err = scarlett2_add_speaker_switch_ctl(mixer);
	if (err < 0)
		return err;

	/* Create the talkback controls */
	err = scarlett2_add_talkback_ctls(mixer);
	if (err < 0)
		return err;

	/* Create the standalone control */
	err = scarlett2_add_standalone_ctl(mixer);
	if (err < 0)
		return err;

	/* Create the power status control */
	err = scarlett2_add_power_status_ctl(mixer);
	if (err < 0)
		return err;

	/* Create the Bluetooth volume control */
	err = scarlett2_add_bluetooth_volume_ctl(mixer);
	if (err < 0)
		return err;

	/* Create the S/PDIF mode control */
	err = scarlett2_add_spdif_mode_ctl(mixer);
	if (err < 0)
		return err;

	/* Set the access mode of controls disabled during
	 * autogain/phantom power switching.
	 */
	if (private->info->gain_input_count) {
		scarlett2_autogain_update_access(mixer);
		scarlett2_phantom_update_access(mixer);
	}

	/* Start handling all notifications */
	private->running = 2;

	return 0;
}

/*** hwdep interface ***/

/* Set private->hwdep_in_use; prevents access to the ALSA controls
 * while doing a config erase/firmware upgrade.
 */
static void scarlett2_lock(struct scarlett2_data *private)
{
	mutex_lock(&private->data_mutex);
	private->hwdep_in_use = 1;
	mutex_unlock(&private->data_mutex);
}

/* Call SCARLETT2_USB_GET_ERASE to get the erase progress */
static int scarlett2_get_erase_progress(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	int segment_id, segment_num, err;
	u8 erase_resp;

	struct {
		__le32 segment_num;
		__le32 pad;
	} __packed erase_req;

	segment_id = private->selected_flash_segment_id;
	segment_num = private->flash_segment_nums[segment_id];

	if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
	    segment_num > SCARLETT2_SEGMENT_NUM_MAX)
		return -EFAULT;

	/* Send the erase progress request */
	erase_req.segment_num = cpu_to_le32(segment_num);
	erase_req.pad = 0;

	err = scarlett2_usb(mixer, SCARLETT2_USB_GET_ERASE,
			    &erase_req, sizeof(erase_req),
			    &erase_resp, sizeof(erase_resp));
	if (err < 0)
		return err;

	return erase_resp;
}

/* Repeatedly call scarlett2_get_erase_progress() until it returns
 * 0xff (erase complete) or we've waited 10 seconds (it usually takes
 * <3 seconds).
 */
static int scarlett2_wait_for_erase(struct usb_mixer_interface *mixer)
{
	int i, err;

	for (i = 0; i < 100; i++) {
		err = scarlett2_get_erase_progress(mixer);
		if (err < 0)
			return err;

		if (err == 0xff)
			return 0;

		msleep(100);
	}

	return -ETIMEDOUT;
}

/* Reboot the device; wait for the erase to complete if one is in
 * progress.
 */
static int scarlett2_reboot(struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;

	if (private->flash_write_state ==
	      SCARLETT2_FLASH_WRITE_STATE_ERASING) {
		int err = scarlett2_wait_for_erase(mixer);

		if (err < 0)
			return err;
	}

	return scarlett2_usb(mixer, SCARLETT2_USB_REBOOT, NULL, 0, NULL, 0);
}

/* Select a flash segment for reading/erasing/writing */
static int scarlett2_ioctl_select_flash_segment(
	struct usb_mixer_interface *mixer,
	unsigned long arg)
{
	struct scarlett2_data *private = mixer->private_data;
	int segment_id, segment_num;

	if (get_user(segment_id, (int __user *)arg))
		return -EFAULT;

	/* Check the segment ID and segment number */
	if (segment_id < 0 || segment_id >= SCARLETT2_SEGMENT_ID_COUNT)
		return -EINVAL;

	segment_num = private->flash_segment_nums[segment_id];
	if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
	    segment_num > SCARLETT2_SEGMENT_NUM_MAX) {
		usb_audio_err(mixer->chip,
			      "%s: invalid segment number %d\n",
			      __func__, segment_id);
		return -EFAULT;
	}

	/* If erasing, wait for it to complete */
	if (private->flash_write_state == SCARLETT2_FLASH_WRITE_STATE_ERASING) {
		int err = scarlett2_wait_for_erase(mixer);

		if (err < 0)
			return err;
	}

	/* Save the selected segment ID and set the state to SELECTED */
	private->selected_flash_segment_id = segment_id;
	private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_SELECTED;

	return 0;
}

/* Erase the previously-selected flash segment */
static int scarlett2_ioctl_erase_flash_segment(
	struct usb_mixer_interface *mixer)
{
	struct scarlett2_data *private = mixer->private_data;
	int segment_id, segment_num, err;

	struct {
		__le32 segment_num;
		__le32 pad;
	} __packed erase_req;

	if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_SELECTED)
		return -EINVAL;

	segment_id = private->selected_flash_segment_id;
	segment_num = private->flash_segment_nums[segment_id];

	if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
	    segment_num > SCARLETT2_SEGMENT_NUM_MAX)
		return -EFAULT;

	/* Prevent access to ALSA controls that access the device from
	 * here on
	 */
	scarlett2_lock(private);

	/* Send the erase request */
	erase_req.segment_num = cpu_to_le32(segment_num);
	erase_req.pad = 0;

	err = scarlett2_usb(mixer, SCARLETT2_USB_ERASE_SEGMENT,
			    &erase_req, sizeof(erase_req),
			    NULL, 0);
	if (err < 0)
		return err;

	/* On success, change the state from SELECTED to ERASING */
	private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_ERASING;

	return 0;
}

/* Get the erase progress from the device */
static int scarlett2_ioctl_get_erase_progress(
	struct usb_mixer_interface *mixer,
	unsigned long arg)
{
	struct scarlett2_data *private = mixer->private_data;
	struct scarlett2_flash_segment_erase_progress progress;
	int segment_id, segment_num, err;
	u8 erase_resp;

	struct {
		__le32 segment_num;
		__le32 pad;
	} __packed erase_req;

	/* Check that we're erasing */
	if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_ERASING)
		return -EINVAL;

	segment_id = private->selected_flash_segment_id;
	segment_num = private->flash_segment_nums[segment_id];

	if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
	    segment_num > SCARLETT2_SEGMENT_NUM_MAX)
		return -EFAULT;

	/* Send the erase progress request */
	erase_req.segment_num = cpu_to_le32(segment_num);
	erase_req.pad = 0;

	err = scarlett2_usb(mixer, SCARLETT2_USB_GET_ERASE,
			    &erase_req, sizeof(erase_req),
			    &erase_resp, sizeof(erase_resp));
	if (err < 0)
		return err;

	progress.progress = erase_resp;
	progress.num_blocks = private->flash_segment_blocks[segment_id];

	if (copy_to_user((void __user *)arg, &progress, sizeof(progress)))
		return -EFAULT;

	/* If the erase is complete, change the state from ERASING to
	 * WRITE.
	 */
	if (progress.progress == 0xff)
		private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_WRITE;

	return 0;
}

static int scarlett2_hwdep_open(struct snd_hwdep *hw, struct file *file)
{
	struct usb_mixer_interface *mixer = hw->private_data;
	struct scarlett2_data *private = mixer->private_data;

	/* If erasing, wait for it to complete */
	if (private->flash_write_state ==
	      SCARLETT2_FLASH_WRITE_STATE_ERASING) {
		int err = scarlett2_wait_for_erase(mixer);

		if (err < 0)
			return err;
	}

	/* Set the state to IDLE */
	private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_IDLE;

	return 0;
}

static int scarlett2_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
				 unsigned int cmd, unsigned long arg)
{
	struct usb_mixer_interface *mixer = hw->private_data;

	switch (cmd) {

	case SCARLETT2_IOCTL_PVERSION:
		return put_user(SCARLETT2_HWDEP_VERSION,
				(int __user *)arg) ? -EFAULT : 0;

	case SCARLETT2_IOCTL_REBOOT:
		return scarlett2_reboot(mixer);

	case SCARLETT2_IOCTL_SELECT_FLASH_SEGMENT:
		return scarlett2_ioctl_select_flash_segment(mixer, arg);

	case SCARLETT2_IOCTL_ERASE_FLASH_SEGMENT:
		return scarlett2_ioctl_erase_flash_segment(mixer);

	case SCARLETT2_IOCTL_GET_ERASE_PROGRESS:
		return scarlett2_ioctl_get_erase_progress(mixer, arg);

	default:
		return -ENOIOCTLCMD;
	}
}

static long scarlett2_hwdep_read(struct snd_hwdep *hw,
				 char __user *buf,
				 long count, loff_t *offset)
{
	struct usb_mixer_interface *mixer = hw->private_data;
	struct scarlett2_data *private = mixer->private_data;
	int segment_id, segment_num, err;
	int flash_size;

	/* SCARLETT2_USB_READ_SEGMENT request data */
	struct {
		__le32 segment_num;
		__le32 offset;
		__le32 len;
	} __packed req;

	u8 *resp;

	/* Flash segment must first be selected */
	if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_SELECTED)
		return -EINVAL;

	/* Get the selected flash segment number */
	segment_id = private->selected_flash_segment_id;
	if (segment_id < 0 || segment_id >= SCARLETT2_SEGMENT_ID_COUNT)
		return -EINVAL;

	segment_num = private->flash_segment_nums[segment_id];
	if (segment_num < 0 ||
	    segment_num > SCARLETT2_SEGMENT_NUM_MAX)
		return -EFAULT;

	/* Validate the offset and count */
	if (count < 0 || *offset < 0)
		return -EINVAL;

	/* Reached EOF? */
	flash_size = private->flash_segment_blocks[segment_id] *
		     SCARLETT2_FLASH_BLOCK_SIZE;
	if (!count || *offset >= flash_size)
		return 0;

	/* Limit the numbers of bytes read to SCARLETT2_FLASH_RW_MAX */
	if (count > SCARLETT2_FLASH_RW_MAX)
		count = SCARLETT2_FLASH_RW_MAX;

	/* Limit read to EOF */
	if (*offset + count >= flash_size)
		count = flash_size - *offset;

	/* Create and send the request */
	req.segment_num = cpu_to_le32(segment_num);
	req.offset = cpu_to_le32(*offset);
	req.len = cpu_to_le32(count);

	resp = kzalloc(count, GFP_KERNEL);
	if (!resp)
		return -ENOMEM;

	err = scarlett2_usb(mixer, SCARLETT2_USB_READ_SEGMENT,
			    &req, sizeof(req), resp, count);
	if (err < 0)
		goto error;

	/* Copy the response to userspace */
	if (copy_to_user(buf, resp, count)) {
		err = -EFAULT;
		goto error;
	}

	*offset += count;
	err = count;

error:
	kfree(resp);
	return err;
}

static long scarlett2_hwdep_write(struct snd_hwdep *hw,
				  const char __user *buf,
				  long count, loff_t *offset)
{
	struct usb_mixer_interface *mixer = hw->private_data;
	struct scarlett2_data *private = mixer->private_data;
	int segment_id, segment_num, err, len;
	int flash_size;

	/* SCARLETT2_USB_WRITE_SEGMENT request data */
	struct {
		__le32 segment_num;
		__le32 offset;
		__le32 pad;
		u8 data[];
	} __packed *req;

	/* Calculate the maximum permitted in data[] */
	const size_t max_data_size = SCARLETT2_FLASH_RW_MAX -
				     offsetof(typeof(*req), data);

	/* If erasing, wait for it to complete */
	if (private->flash_write_state ==
	      SCARLETT2_FLASH_WRITE_STATE_ERASING) {
		err = scarlett2_wait_for_erase(mixer);
		if (err < 0)
			return err;
		private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_WRITE;

	/* Check that an erase has been done & completed */
	} else if (private->flash_write_state !=
		     SCARLETT2_FLASH_WRITE_STATE_WRITE) {
		return -EINVAL;
	}

	/* Check that we're writing to the upgrade firmware */
	segment_id = private->selected_flash_segment_id;
	if (segment_id != SCARLETT2_SEGMENT_ID_FIRMWARE)
		return -EINVAL;

	segment_num = private->flash_segment_nums[segment_id];
	if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
	    segment_num > SCARLETT2_SEGMENT_NUM_MAX)
		return -EFAULT;

	/* Validate the offset and count */
	flash_size = private->flash_segment_blocks[segment_id] *
		     SCARLETT2_FLASH_BLOCK_SIZE;

	if (count < 0 || *offset < 0 || *offset + count >= flash_size)
		return -EINVAL;

	if (!count)
		return 0;

	/* Limit the *req size to SCARLETT2_FLASH_RW_MAX */
	if (count > max_data_size)
		count = max_data_size;

	/* Create and send the request */
	len = struct_size(req, data, count);
	req = kzalloc(len, GFP_KERNEL);
	if (!req)
		return -ENOMEM;

	req->segment_num = cpu_to_le32(segment_num);
	req->offset = cpu_to_le32(*offset);
	req->pad = 0;

	if (copy_from_user(req->data, buf, count)) {
		err = -EFAULT;
		goto error;
	}

	err = scarlett2_usb(mixer, SCARLETT2_USB_WRITE_SEGMENT,
			    req, len, NULL, 0);
	if (err < 0)
		goto error;

	*offset += count;
	err = count;

error:
	kfree(req);
	return err;
}

static int scarlett2_hwdep_release(struct snd_hwdep *hw, struct file *file)
{
	struct usb_mixer_interface *mixer = hw->private_data;
	struct scarlett2_data *private = mixer->private_data;

	/* Return from the SELECTED or WRITE state to IDLE.
	 * The ERASING state is left as-is, and checked on next open.
	 */
	if (private &&
	    private->hwdep_in_use &&
	    private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_ERASING)
		private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_IDLE;

	return 0;
}

static int scarlett2_hwdep_init(struct usb_mixer_interface *mixer)
{
	struct snd_hwdep *hw;
	int err;

	err = snd_hwdep_new(mixer->chip->card, "Focusrite Control", 0, &hw);
	if (err < 0)
		return err;

	hw->private_data = mixer;
	hw->exclusive = 1;
	hw->ops.open = scarlett2_hwdep_open;
	hw->ops.ioctl = scarlett2_hwdep_ioctl;
	hw->ops.read = scarlett2_hwdep_read;
	hw->ops.write = scarlett2_hwdep_write;
	hw->ops.release = scarlett2_hwdep_release;

	return 0;
}

int snd_scarlett2_init(struct usb_mixer_interface *mixer)
{
	struct snd_usb_audio *chip = mixer->chip;
	const struct scarlett2_device_entry *entry;
	int err;

	/* only use UAC_VERSION_2 */
	if (!mixer->protocol)
		return 0;

	/* find entry in scarlett2_devices */
	entry = get_scarlett2_device_entry(mixer);
	if (!entry) {
		usb_audio_err(mixer->chip,
			      "%s: missing device entry for %04x:%04x\n",
			      __func__,
			      USB_ID_VENDOR(chip->usb_id),
			      USB_ID_PRODUCT(chip->usb_id));
		return 0;
	}

	if (chip->setup & SCARLETT2_DISABLE) {
		usb_audio_info(chip,
			"Focusrite %s Mixer Driver disabled "
			"by modprobe options (snd_usb_audio "
			"vid=0x%04x pid=0x%04x device_setup=%d)\n",
			entry->series_name,
			USB_ID_VENDOR(chip->usb_id),
			USB_ID_PRODUCT(chip->usb_id),
			SCARLETT2_DISABLE);
		return 0;
	}

	usb_audio_info(chip,
		"Focusrite %s Mixer Driver enabled (pid=0x%04x); "
		"report any issues to "
		"https://github.com/geoffreybennett/scarlett-gen2/issues",
		entry->series_name,
		USB_ID_PRODUCT(chip->usb_id));

	err = snd_scarlett2_controls_create(mixer, entry);
	if (err < 0) {
		usb_audio_err(mixer->chip,
			      "Error initialising %s Mixer Driver: %d",
			      entry->series_name,
			      err);
		return err;
	}

	err = scarlett2_hwdep_init(mixer);
	if (err < 0)
		usb_audio_err(mixer->chip,
			      "Error creating %s hwdep device: %d",
			      entry->series_name,
			      err);

	return err;
}