mixer_scarlett_gen2.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Focusrite Scarlett Gen 2/3 and Clarett+ Driver for ALSA
  4. *
  5. * Supported models:
  6. * - 6i6/18i8/18i20 Gen 2
  7. * - Solo/2i2/4i4/8i6/18i8/18i20 Gen 3
  8. * - Clarett+ 8Pre
  9. *
  10. * Copyright (c) 2018-2022 by Geoffrey D. Bennett <g at b4.vu>
  11. * Copyright (c) 2020-2021 by Vladimir Sadovnikov <[email protected]>
  12. * Copyright (c) 2022 by Christian Colglazier <[email protected]>
  13. *
  14. * Based on the Scarlett (Gen 1) Driver for ALSA:
  15. *
  16. * Copyright (c) 2013 by Tobias Hoffmann
  17. * Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
  18. * Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
  19. * Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
  20. *
  21. * Many codes borrowed from audio.c by
  22. * Alan Cox (alan at lxorguk.ukuu.org.uk)
  23. * Thomas Sailer (sailer at ife.ee.ethz.ch)
  24. *
  25. * Code cleanup:
  26. * David Henningsson <david.henningsson at canonical.com>
  27. */
  28. /* The protocol was reverse engineered by looking at the communication
  29. * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20
  30. * (firmware 1083) using usbmon in July-August 2018.
  31. *
  32. * Scarlett 18i8 support added in April 2019.
  33. *
  34. * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
  35. * for providing usbmon output and testing).
  36. *
  37. * Scarlett 4i4/8i6 Gen 3 support added in May 2020 (thanks to Laurent
  38. * Debricon for donating a 4i4 and to Fredrik Unger for providing 8i6
  39. * usbmon output and testing).
  40. *
  41. * Scarlett 18i8/18i20 Gen 3 support added in June 2020 (thanks to
  42. * Darren Jaeckel, Alex Sedlack, and Clovis Lunel for providing usbmon
  43. * output, protocol traces and testing).
  44. *
  45. * Support for loading mixer volume and mux configuration from the
  46. * interface during driver initialisation added in May 2021 (thanks to
  47. * Vladimir Sadovnikov for figuring out how).
  48. *
  49. * Support for Solo/2i2 Gen 3 added in May 2021 (thanks to Alexander
  50. * Vorona for 2i2 protocol traces).
  51. *
  52. * Support for phantom power, direct monitoring, speaker switching,
  53. * and talkback added in May-June 2021.
  54. *
  55. * Support for Clarett+ 8Pre added in Aug 2022 by Christian
  56. * Colglazier.
  57. *
  58. * This ALSA mixer gives access to (model-dependent):
  59. * - input, output, mixer-matrix muxes
  60. * - mixer-matrix gain stages
  61. * - gain/volume/mute controls
  62. * - level meters
  63. * - line/inst level, pad, and air controls
  64. * - phantom power, direct monitor, speaker switching, and talkback
  65. * controls
  66. * - disable/enable MSD mode
  67. * - disable/enable standalone mode
  68. *
  69. * <ditaa>
  70. * /--------------\ 18chn 20chn /--------------\
  71. * | Hardware in +--+------\ /-------------+--+ ALSA PCM out |
  72. * \--------------/ | | | | \--------------/
  73. * | | | /-----\ |
  74. * | | | | | |
  75. * | v v v | |
  76. * | +---------------+ | |
  77. * | \ Matrix Mux / | |
  78. * | +-----+-----+ | |
  79. * | | | |
  80. * | |18chn | |
  81. * | | | |
  82. * | | 10chn| |
  83. * | v | |
  84. * | +------------+ | |
  85. * | | Mixer | | |
  86. * | | Matrix | | |
  87. * | | | | |
  88. * | | 18x10 Gain | | |
  89. * | | stages | | |
  90. * | +-----+------+ | |
  91. * | | | |
  92. * |18chn |10chn | |20chn
  93. * | | | |
  94. * | +----------/ |
  95. * | | |
  96. * v v v
  97. * ===========================
  98. * +---------------+ +--—------------+
  99. * \ Output Mux / \ Capture Mux /
  100. * +---+---+---+ +-----+-----+
  101. * | | |
  102. * 10chn| | |18chn
  103. * | | |
  104. * /--------------\ | | | /--------------\
  105. * | S/PDIF, ADAT |<--/ |10chn \-->| ALSA PCM in |
  106. * | Hardware out | | \--------------/
  107. * \--------------/ |
  108. * v
  109. * +-------------+ Software gain per channel.
  110. * | Master Gain |<-- 18i20 only: Switch per channel
  111. * +------+------+ to select HW or SW gain control.
  112. * |
  113. * |10chn
  114. * /--------------\ |
  115. * | Analogue |<------/
  116. * | Hardware out |
  117. * \--------------/
  118. * </ditaa>
  119. *
  120. * Gen 3 devices have a Mass Storage Device (MSD) mode where a small
  121. * disk with registration and driver download information is presented
  122. * to the host. To access the full functionality of the device without
  123. * proprietary software, MSD mode can be disabled by:
  124. * - holding down the 48V button for five seconds while powering on
  125. * the device, or
  126. * - using this driver and alsamixer to change the "MSD Mode" setting
  127. * to Off and power-cycling the device
  128. */
  129. #include <linux/slab.h>
  130. #include <linux/usb.h>
  131. #include <linux/moduleparam.h>
  132. #include <sound/control.h>
  133. #include <sound/tlv.h>
  134. #include "usbaudio.h"
  135. #include "mixer.h"
  136. #include "helper.h"
  137. #include "mixer_scarlett_gen2.h"
  138. /* device_setup value to enable */
  139. #define SCARLETT2_ENABLE 0x01
  140. /* device_setup value to allow turning MSD mode back on */
  141. #define SCARLETT2_MSD_ENABLE 0x02
  142. /* some gui mixers can't handle negative ctl values */
  143. #define SCARLETT2_VOLUME_BIAS 127
  144. /* mixer range from -80dB to +6dB in 0.5dB steps */
  145. #define SCARLETT2_MIXER_MIN_DB -80
  146. #define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
  147. #define SCARLETT2_MIXER_MAX_DB 6
  148. #define SCARLETT2_MIXER_MAX_VALUE \
  149. ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
  150. #define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1)
  151. /* map from (dB + 80) * 2 to mixer value
  152. * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
  153. */
  154. static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = {
  155. 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
  156. 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
  157. 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
  158. 23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
  159. 54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
  160. 122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
  161. 244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
  162. 487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
  163. 973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
  164. 1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
  165. 3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
  166. 5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
  167. 9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
  168. 16345
  169. };
  170. /* Maximum number of analogue outputs */
  171. #define SCARLETT2_ANALOGUE_MAX 10
  172. /* Maximum number of level and pad switches */
  173. #define SCARLETT2_LEVEL_SWITCH_MAX 2
  174. #define SCARLETT2_PAD_SWITCH_MAX 8
  175. #define SCARLETT2_AIR_SWITCH_MAX 8
  176. #define SCARLETT2_PHANTOM_SWITCH_MAX 2
  177. /* Maximum number of inputs to the mixer */
  178. #define SCARLETT2_INPUT_MIX_MAX 25
  179. /* Maximum number of outputs from the mixer */
  180. #define SCARLETT2_OUTPUT_MIX_MAX 12
  181. /* Maximum size of the data in the USB mux assignment message:
  182. * 20 inputs, 20 outputs, 25 matrix inputs, 12 spare
  183. */
  184. #define SCARLETT2_MUX_MAX 77
  185. /* Maximum number of meters (sum of output port counts) */
  186. #define SCARLETT2_MAX_METERS 65
  187. /* There are three different sets of configuration parameters across
  188. * the devices
  189. */
  190. enum {
  191. SCARLETT2_CONFIG_SET_NO_MIXER = 0,
  192. SCARLETT2_CONFIG_SET_GEN_2 = 1,
  193. SCARLETT2_CONFIG_SET_GEN_3 = 2,
  194. SCARLETT2_CONFIG_SET_CLARETT = 3,
  195. SCARLETT2_CONFIG_SET_COUNT = 4
  196. };
  197. /* Hardware port types:
  198. * - None (no input to mux)
  199. * - Analogue I/O
  200. * - S/PDIF I/O
  201. * - ADAT I/O
  202. * - Mixer I/O
  203. * - PCM I/O
  204. */
  205. enum {
  206. SCARLETT2_PORT_TYPE_NONE = 0,
  207. SCARLETT2_PORT_TYPE_ANALOGUE = 1,
  208. SCARLETT2_PORT_TYPE_SPDIF = 2,
  209. SCARLETT2_PORT_TYPE_ADAT = 3,
  210. SCARLETT2_PORT_TYPE_MIX = 4,
  211. SCARLETT2_PORT_TYPE_PCM = 5,
  212. SCARLETT2_PORT_TYPE_COUNT = 6,
  213. };
  214. /* I/O count of each port type kept in struct scarlett2_ports */
  215. enum {
  216. SCARLETT2_PORT_IN = 0,
  217. SCARLETT2_PORT_OUT = 1,
  218. SCARLETT2_PORT_DIRNS = 2,
  219. };
  220. /* Dim/Mute buttons on the 18i20 */
  221. enum {
  222. SCARLETT2_BUTTON_MUTE = 0,
  223. SCARLETT2_BUTTON_DIM = 1,
  224. SCARLETT2_DIM_MUTE_COUNT = 2,
  225. };
  226. static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
  227. "Mute Playback Switch", "Dim Playback Switch"
  228. };
  229. /* Description of each hardware port type:
  230. * - id: hardware ID of this port type
  231. * - src_descr: printf format string for mux input selections
  232. * - src_num_offset: added to channel number for the fprintf
  233. * - dst_descr: printf format string for mixer controls
  234. */
  235. struct scarlett2_port {
  236. u16 id;
  237. const char * const src_descr;
  238. int src_num_offset;
  239. const char * const dst_descr;
  240. };
  241. static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
  242. [SCARLETT2_PORT_TYPE_NONE] = {
  243. .id = 0x000,
  244. .src_descr = "Off"
  245. },
  246. [SCARLETT2_PORT_TYPE_ANALOGUE] = {
  247. .id = 0x080,
  248. .src_descr = "Analogue %d",
  249. .src_num_offset = 1,
  250. .dst_descr = "Analogue Output %02d Playback"
  251. },
  252. [SCARLETT2_PORT_TYPE_SPDIF] = {
  253. .id = 0x180,
  254. .src_descr = "S/PDIF %d",
  255. .src_num_offset = 1,
  256. .dst_descr = "S/PDIF Output %d Playback"
  257. },
  258. [SCARLETT2_PORT_TYPE_ADAT] = {
  259. .id = 0x200,
  260. .src_descr = "ADAT %d",
  261. .src_num_offset = 1,
  262. .dst_descr = "ADAT Output %d Playback"
  263. },
  264. [SCARLETT2_PORT_TYPE_MIX] = {
  265. .id = 0x300,
  266. .src_descr = "Mix %c",
  267. .src_num_offset = 'A',
  268. .dst_descr = "Mixer Input %02d Capture"
  269. },
  270. [SCARLETT2_PORT_TYPE_PCM] = {
  271. .id = 0x600,
  272. .src_descr = "PCM %d",
  273. .src_num_offset = 1,
  274. .dst_descr = "PCM %02d Capture"
  275. },
  276. };
  277. /* Number of mux tables: one for each band of sample rates
  278. * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
  279. */
  280. #define SCARLETT2_MUX_TABLES 3
  281. /* Maximum number of entries in a mux table */
  282. #define SCARLETT2_MAX_MUX_ENTRIES 10
  283. /* One entry within mux_assignment defines the port type and range of
  284. * ports to add to the set_mux message. The end of the list is marked
  285. * with count == 0.
  286. */
  287. struct scarlett2_mux_entry {
  288. u8 port_type;
  289. u8 start;
  290. u8 count;
  291. };
  292. struct scarlett2_device_info {
  293. u32 usb_id; /* USB device identifier */
  294. /* Gen 3 devices have an internal MSD mode switch that needs
  295. * to be disabled in order to access the full functionality of
  296. * the device.
  297. */
  298. u8 has_msd_mode;
  299. /* which set of configuration parameters the device uses */
  300. u8 config_set;
  301. /* line out hw volume is sw controlled */
  302. u8 line_out_hw_vol;
  303. /* support for main/alt speaker switching */
  304. u8 has_speaker_switching;
  305. /* support for talkback microphone */
  306. u8 has_talkback;
  307. /* the number of analogue inputs with a software switchable
  308. * level control that can be set to line or instrument
  309. */
  310. u8 level_input_count;
  311. /* the first input with a level control (0-based) */
  312. u8 level_input_first;
  313. /* the number of analogue inputs with a software switchable
  314. * 10dB pad control
  315. */
  316. u8 pad_input_count;
  317. /* the number of analogue inputs with a software switchable
  318. * "air" control
  319. */
  320. u8 air_input_count;
  321. /* the number of phantom (48V) software switchable controls */
  322. u8 phantom_count;
  323. /* the number of inputs each phantom switch controls */
  324. u8 inputs_per_phantom;
  325. /* the number of direct monitor options
  326. * (0 = none, 1 = mono only, 2 = mono/stereo)
  327. */
  328. u8 direct_monitor;
  329. /* remap analogue outputs; 18i8 Gen 3 has "line 3/4" connected
  330. * internally to the analogue 7/8 outputs
  331. */
  332. u8 line_out_remap_enable;
  333. u8 line_out_remap[SCARLETT2_ANALOGUE_MAX];
  334. /* additional description for the line out volume controls */
  335. const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
  336. /* number of sources/destinations of each port type */
  337. const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];
  338. /* layout/order of the entries in the set_mux message */
  339. struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES]
  340. [SCARLETT2_MAX_MUX_ENTRIES];
  341. };
  342. struct scarlett2_data {
  343. struct usb_mixer_interface *mixer;
  344. struct mutex usb_mutex; /* prevent sending concurrent USB requests */
  345. struct mutex data_mutex; /* lock access to this data */
  346. struct delayed_work work;
  347. const struct scarlett2_device_info *info;
  348. __u8 bInterfaceNumber;
  349. __u8 bEndpointAddress;
  350. __u16 wMaxPacketSize;
  351. __u8 bInterval;
  352. int num_mux_srcs;
  353. int num_mux_dsts;
  354. u16 scarlett2_seq;
  355. u8 sync_updated;
  356. u8 vol_updated;
  357. u8 input_other_updated;
  358. u8 monitor_other_updated;
  359. u8 mux_updated;
  360. u8 speaker_switching_switched;
  361. u8 sync;
  362. u8 master_vol;
  363. u8 vol[SCARLETT2_ANALOGUE_MAX];
  364. u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
  365. u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
  366. u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
  367. u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
  368. u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
  369. u8 air_switch[SCARLETT2_AIR_SWITCH_MAX];
  370. u8 phantom_switch[SCARLETT2_PHANTOM_SWITCH_MAX];
  371. u8 phantom_persistence;
  372. u8 direct_monitor_switch;
  373. u8 speaker_switching_switch;
  374. u8 talkback_switch;
  375. u8 talkback_map[SCARLETT2_OUTPUT_MIX_MAX];
  376. u8 msd_switch;
  377. u8 standalone_switch;
  378. struct snd_kcontrol *sync_ctl;
  379. struct snd_kcontrol *master_vol_ctl;
  380. struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
  381. struct snd_kcontrol *sw_hw_ctls[SCARLETT2_ANALOGUE_MAX];
  382. struct snd_kcontrol *mute_ctls[SCARLETT2_ANALOGUE_MAX];
  383. struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT];
  384. struct snd_kcontrol *level_ctls[SCARLETT2_LEVEL_SWITCH_MAX];
  385. struct snd_kcontrol *pad_ctls[SCARLETT2_PAD_SWITCH_MAX];
  386. struct snd_kcontrol *air_ctls[SCARLETT2_AIR_SWITCH_MAX];
  387. struct snd_kcontrol *phantom_ctls[SCARLETT2_PHANTOM_SWITCH_MAX];
  388. struct snd_kcontrol *mux_ctls[SCARLETT2_MUX_MAX];
  389. struct snd_kcontrol *direct_monitor_ctl;
  390. struct snd_kcontrol *speaker_switching_ctl;
  391. struct snd_kcontrol *talkback_ctl;
  392. u8 mux[SCARLETT2_MUX_MAX];
  393. u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX];
  394. };
  395. /*** Model-specific data ***/
  396. static const struct scarlett2_device_info s6i6_gen2_info = {
  397. .usb_id = USB_ID(0x1235, 0x8203),
  398. .config_set = SCARLETT2_CONFIG_SET_GEN_2,
  399. .level_input_count = 2,
  400. .pad_input_count = 2,
  401. .line_out_descrs = {
  402. "Headphones 1 L",
  403. "Headphones 1 R",
  404. "Headphones 2 L",
  405. "Headphones 2 R",
  406. },
  407. .port_count = {
  408. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  409. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
  410. [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
  411. [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
  412. [SCARLETT2_PORT_TYPE_PCM] = { 6, 6 },
  413. },
  414. .mux_assignment = { {
  415. { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
  416. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  417. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  418. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  419. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  420. { 0, 0, 0 },
  421. }, {
  422. { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
  423. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  424. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  425. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  426. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  427. { 0, 0, 0 },
  428. }, {
  429. { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
  430. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  431. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  432. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  433. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  434. { 0, 0, 0 },
  435. } },
  436. };
  437. static const struct scarlett2_device_info s18i8_gen2_info = {
  438. .usb_id = USB_ID(0x1235, 0x8204),
  439. .config_set = SCARLETT2_CONFIG_SET_GEN_2,
  440. .level_input_count = 2,
  441. .pad_input_count = 4,
  442. .line_out_descrs = {
  443. "Monitor L",
  444. "Monitor R",
  445. "Headphones 1 L",
  446. "Headphones 1 R",
  447. "Headphones 2 L",
  448. "Headphones 2 R",
  449. },
  450. .port_count = {
  451. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  452. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 },
  453. [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
  454. [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
  455. [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
  456. [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 },
  457. },
  458. .mux_assignment = { {
  459. { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
  460. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
  461. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  462. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  463. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  464. { 0, 0, 0 },
  465. }, {
  466. { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
  467. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
  468. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  469. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  470. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  471. { 0, 0, 0 },
  472. }, {
  473. { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
  474. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
  475. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  476. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  477. { SCARLETT2_PORT_TYPE_NONE, 0, 4 },
  478. { 0, 0, 0 },
  479. } },
  480. };
  481. static const struct scarlett2_device_info s18i20_gen2_info = {
  482. .usb_id = USB_ID(0x1235, 0x8201),
  483. .config_set = SCARLETT2_CONFIG_SET_GEN_2,
  484. .line_out_hw_vol = 1,
  485. .line_out_descrs = {
  486. "Monitor L",
  487. "Monitor R",
  488. NULL,
  489. NULL,
  490. NULL,
  491. NULL,
  492. "Headphones 1 L",
  493. "Headphones 1 R",
  494. "Headphones 2 L",
  495. "Headphones 2 R",
  496. },
  497. .port_count = {
  498. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  499. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
  500. [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
  501. [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
  502. [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
  503. [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
  504. },
  505. .mux_assignment = { {
  506. { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
  507. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
  508. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  509. { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
  510. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  511. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  512. { 0, 0, 0 },
  513. }, {
  514. { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
  515. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
  516. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  517. { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
  518. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  519. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  520. { 0, 0, 0 },
  521. }, {
  522. { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
  523. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
  524. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  525. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  526. { SCARLETT2_PORT_TYPE_NONE, 0, 6 },
  527. { 0, 0, 0 },
  528. } },
  529. };
  530. static const struct scarlett2_device_info solo_gen3_info = {
  531. .usb_id = USB_ID(0x1235, 0x8211),
  532. .has_msd_mode = 1,
  533. .config_set = SCARLETT2_CONFIG_SET_NO_MIXER,
  534. .level_input_count = 1,
  535. .level_input_first = 1,
  536. .air_input_count = 1,
  537. .phantom_count = 1,
  538. .inputs_per_phantom = 1,
  539. .direct_monitor = 1,
  540. };
  541. static const struct scarlett2_device_info s2i2_gen3_info = {
  542. .usb_id = USB_ID(0x1235, 0x8210),
  543. .has_msd_mode = 1,
  544. .config_set = SCARLETT2_CONFIG_SET_NO_MIXER,
  545. .level_input_count = 2,
  546. .air_input_count = 2,
  547. .phantom_count = 1,
  548. .inputs_per_phantom = 2,
  549. .direct_monitor = 2,
  550. };
  551. static const struct scarlett2_device_info s4i4_gen3_info = {
  552. .usb_id = USB_ID(0x1235, 0x8212),
  553. .has_msd_mode = 1,
  554. .config_set = SCARLETT2_CONFIG_SET_GEN_3,
  555. .level_input_count = 2,
  556. .pad_input_count = 2,
  557. .air_input_count = 2,
  558. .phantom_count = 1,
  559. .inputs_per_phantom = 2,
  560. .line_out_descrs = {
  561. "Monitor L",
  562. "Monitor R",
  563. "Headphones L",
  564. "Headphones R",
  565. },
  566. .port_count = {
  567. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  568. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
  569. [SCARLETT2_PORT_TYPE_MIX] = { 6, 8 },
  570. [SCARLETT2_PORT_TYPE_PCM] = { 4, 6 },
  571. },
  572. .mux_assignment = { {
  573. { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
  574. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  575. { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
  576. { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
  577. { 0, 0, 0 },
  578. }, {
  579. { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
  580. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  581. { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
  582. { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
  583. { 0, 0, 0 },
  584. }, {
  585. { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
  586. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  587. { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
  588. { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
  589. { 0, 0, 0 },
  590. } },
  591. };
  592. static const struct scarlett2_device_info s8i6_gen3_info = {
  593. .usb_id = USB_ID(0x1235, 0x8213),
  594. .has_msd_mode = 1,
  595. .config_set = SCARLETT2_CONFIG_SET_GEN_3,
  596. .level_input_count = 2,
  597. .pad_input_count = 2,
  598. .air_input_count = 2,
  599. .phantom_count = 1,
  600. .inputs_per_phantom = 2,
  601. .line_out_descrs = {
  602. "Headphones 1 L",
  603. "Headphones 1 R",
  604. "Headphones 2 L",
  605. "Headphones 2 R",
  606. },
  607. .port_count = {
  608. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  609. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6, 4 },
  610. [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
  611. [SCARLETT2_PORT_TYPE_MIX] = { 8, 8 },
  612. [SCARLETT2_PORT_TYPE_PCM] = { 6, 10 },
  613. },
  614. .mux_assignment = { {
  615. { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
  616. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  617. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  618. { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
  619. { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
  620. { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
  621. { 0, 0, 0 },
  622. }, {
  623. { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
  624. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  625. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  626. { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
  627. { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
  628. { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
  629. { 0, 0, 0 },
  630. }, {
  631. { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
  632. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
  633. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  634. { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
  635. { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
  636. { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
  637. { 0, 0, 0 },
  638. } },
  639. };
  640. static const struct scarlett2_device_info s18i8_gen3_info = {
  641. .usb_id = USB_ID(0x1235, 0x8214),
  642. .has_msd_mode = 1,
  643. .config_set = SCARLETT2_CONFIG_SET_GEN_3,
  644. .line_out_hw_vol = 1,
  645. .has_speaker_switching = 1,
  646. .level_input_count = 2,
  647. .pad_input_count = 4,
  648. .air_input_count = 4,
  649. .phantom_count = 2,
  650. .inputs_per_phantom = 2,
  651. .line_out_remap_enable = 1,
  652. .line_out_remap = { 0, 1, 6, 7, 2, 3, 4, 5 },
  653. .line_out_descrs = {
  654. "Monitor L",
  655. "Monitor R",
  656. "Alt Monitor L",
  657. "Alt Monitor R",
  658. "Headphones 1 L",
  659. "Headphones 1 R",
  660. "Headphones 2 L",
  661. "Headphones 2 R",
  662. },
  663. .port_count = {
  664. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  665. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 8 },
  666. [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
  667. [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
  668. [SCARLETT2_PORT_TYPE_MIX] = { 10, 20 },
  669. [SCARLETT2_PORT_TYPE_PCM] = { 8, 20 },
  670. },
  671. .mux_assignment = { {
  672. { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
  673. { SCARLETT2_PORT_TYPE_PCM, 12, 8 },
  674. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
  675. { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
  676. { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
  677. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  678. { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
  679. { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
  680. { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
  681. { 0, 0, 0 },
  682. }, {
  683. { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
  684. { SCARLETT2_PORT_TYPE_PCM, 12, 4 },
  685. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
  686. { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
  687. { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
  688. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  689. { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
  690. { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
  691. { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
  692. { 0, 0, 0 },
  693. }, {
  694. { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
  695. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
  696. { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
  697. { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
  698. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  699. { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
  700. { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
  701. { 0, 0, 0 },
  702. } },
  703. };
  704. static const struct scarlett2_device_info s18i20_gen3_info = {
  705. .usb_id = USB_ID(0x1235, 0x8215),
  706. .has_msd_mode = 1,
  707. .config_set = SCARLETT2_CONFIG_SET_GEN_3,
  708. .line_out_hw_vol = 1,
  709. .has_speaker_switching = 1,
  710. .has_talkback = 1,
  711. .level_input_count = 2,
  712. .pad_input_count = 8,
  713. .air_input_count = 8,
  714. .phantom_count = 2,
  715. .inputs_per_phantom = 4,
  716. .line_out_descrs = {
  717. "Monitor 1 L",
  718. "Monitor 1 R",
  719. "Monitor 2 L",
  720. "Monitor 2 R",
  721. NULL,
  722. NULL,
  723. "Headphones 1 L",
  724. "Headphones 1 R",
  725. "Headphones 2 L",
  726. "Headphones 2 R",
  727. },
  728. .port_count = {
  729. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  730. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 9, 10 },
  731. [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
  732. [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
  733. [SCARLETT2_PORT_TYPE_MIX] = { 12, 25 },
  734. [SCARLETT2_PORT_TYPE_PCM] = { 20, 20 },
  735. },
  736. .mux_assignment = { {
  737. { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
  738. { SCARLETT2_PORT_TYPE_PCM, 10, 10 },
  739. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
  740. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  741. { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
  742. { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
  743. { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
  744. { SCARLETT2_PORT_TYPE_NONE, 0, 12 },
  745. { 0, 0, 0 },
  746. }, {
  747. { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
  748. { SCARLETT2_PORT_TYPE_PCM, 10, 8 },
  749. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
  750. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  751. { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
  752. { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
  753. { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
  754. { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
  755. { 0, 0, 0 },
  756. }, {
  757. { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
  758. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
  759. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  760. { SCARLETT2_PORT_TYPE_NONE, 0, 24 },
  761. { 0, 0, 0 },
  762. } },
  763. };
  764. static const struct scarlett2_device_info clarett_8pre_info = {
  765. .usb_id = USB_ID(0x1235, 0x820c),
  766. .config_set = SCARLETT2_CONFIG_SET_CLARETT,
  767. .line_out_hw_vol = 1,
  768. .level_input_count = 2,
  769. .air_input_count = 8,
  770. .line_out_descrs = {
  771. "Monitor L",
  772. "Monitor R",
  773. NULL,
  774. NULL,
  775. NULL,
  776. NULL,
  777. "Headphones 1 L",
  778. "Headphones 1 R",
  779. "Headphones 2 L",
  780. "Headphones 2 R",
  781. },
  782. .port_count = {
  783. [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
  784. [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
  785. [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
  786. [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
  787. [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
  788. [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
  789. },
  790. .mux_assignment = { {
  791. { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
  792. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
  793. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  794. { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
  795. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  796. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  797. { 0, 0, 0 },
  798. }, {
  799. { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
  800. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
  801. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  802. { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
  803. { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
  804. { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
  805. { 0, 0, 0 },
  806. }, {
  807. { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
  808. { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
  809. { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
  810. { SCARLETT2_PORT_TYPE_NONE, 0, 22 },
  811. { 0, 0, 0 },
  812. } },
  813. };
  814. static const struct scarlett2_device_info *scarlett2_devices[] = {
  815. /* Supported Gen 2 devices */
  816. &s6i6_gen2_info,
  817. &s18i8_gen2_info,
  818. &s18i20_gen2_info,
  819. /* Supported Gen 3 devices */
  820. &solo_gen3_info,
  821. &s2i2_gen3_info,
  822. &s4i4_gen3_info,
  823. &s8i6_gen3_info,
  824. &s18i8_gen3_info,
  825. &s18i20_gen3_info,
  826. /* Supported Clarett+ devices */
  827. &clarett_8pre_info,
  828. /* End of list */
  829. NULL
  830. };
  831. /* get the starting port index number for a given port type/direction */
  832. static int scarlett2_get_port_start_num(
  833. const int port_count[][SCARLETT2_PORT_DIRNS],
  834. int direction, int port_type)
  835. {
  836. int i, num = 0;
  837. for (i = 0; i < port_type; i++)
  838. num += port_count[i][direction];
  839. return num;
  840. }
  841. /*** USB Interactions ***/
  842. /* Notifications from the interface */
  843. #define SCARLETT2_USB_NOTIFY_SYNC 0x00000008
  844. #define SCARLETT2_USB_NOTIFY_DIM_MUTE 0x00200000
  845. #define SCARLETT2_USB_NOTIFY_MONITOR 0x00400000
  846. #define SCARLETT2_USB_NOTIFY_INPUT_OTHER 0x00800000
  847. #define SCARLETT2_USB_NOTIFY_MONITOR_OTHER 0x01000000
  848. /* Commands for sending/receiving requests/responses */
  849. #define SCARLETT2_USB_CMD_INIT 0
  850. #define SCARLETT2_USB_CMD_REQ 2
  851. #define SCARLETT2_USB_CMD_RESP 3
  852. #define SCARLETT2_USB_INIT_1 0x00000000
  853. #define SCARLETT2_USB_INIT_2 0x00000002
  854. #define SCARLETT2_USB_GET_METER 0x00001001
  855. #define SCARLETT2_USB_GET_MIX 0x00002001
  856. #define SCARLETT2_USB_SET_MIX 0x00002002
  857. #define SCARLETT2_USB_GET_MUX 0x00003001
  858. #define SCARLETT2_USB_SET_MUX 0x00003002
  859. #define SCARLETT2_USB_GET_SYNC 0x00006004
  860. #define SCARLETT2_USB_GET_DATA 0x00800000
  861. #define SCARLETT2_USB_SET_DATA 0x00800001
  862. #define SCARLETT2_USB_DATA_CMD 0x00800002
  863. #define SCARLETT2_USB_CONFIG_SAVE 6
  864. #define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31
  865. #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
  866. /* volume status is read together (matches scarlett2_config_items[1]) */
  867. struct scarlett2_usb_volume_status {
  868. /* dim/mute buttons */
  869. u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
  870. u8 pad1;
  871. /* software volume setting */
  872. s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
  873. /* actual volume of output inc. dim (-18dB) */
  874. s16 hw_vol[SCARLETT2_ANALOGUE_MAX];
  875. /* internal mute buttons */
  876. u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
  877. /* sw (0) or hw (1) controlled */
  878. u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
  879. u8 pad3[6];
  880. /* front panel volume knob */
  881. s16 master_vol;
  882. } __packed;
  883. /* Configuration parameters that can be read and written */
  884. enum {
  885. SCARLETT2_CONFIG_DIM_MUTE = 0,
  886. SCARLETT2_CONFIG_LINE_OUT_VOLUME = 1,
  887. SCARLETT2_CONFIG_MUTE_SWITCH = 2,
  888. SCARLETT2_CONFIG_SW_HW_SWITCH = 3,
  889. SCARLETT2_CONFIG_LEVEL_SWITCH = 4,
  890. SCARLETT2_CONFIG_PAD_SWITCH = 5,
  891. SCARLETT2_CONFIG_MSD_SWITCH = 6,
  892. SCARLETT2_CONFIG_AIR_SWITCH = 7,
  893. SCARLETT2_CONFIG_STANDALONE_SWITCH = 8,
  894. SCARLETT2_CONFIG_PHANTOM_SWITCH = 9,
  895. SCARLETT2_CONFIG_PHANTOM_PERSISTENCE = 10,
  896. SCARLETT2_CONFIG_DIRECT_MONITOR = 11,
  897. SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH = 12,
  898. SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE = 13,
  899. SCARLETT2_CONFIG_TALKBACK_MAP = 14,
  900. SCARLETT2_CONFIG_COUNT = 15
  901. };
  902. /* Location, size, and activation command number for the configuration
  903. * parameters. Size is in bits and may be 1, 8, or 16.
  904. */
  905. struct scarlett2_config {
  906. u8 offset;
  907. u8 size;
  908. u8 activate;
  909. };
  910. static const struct scarlett2_config
  911. scarlett2_config_items[SCARLETT2_CONFIG_SET_COUNT]
  912. [SCARLETT2_CONFIG_COUNT] =
  913. /* Devices without a mixer (Gen 3 Solo and 2i2) */
  914. { {
  915. [SCARLETT2_CONFIG_MSD_SWITCH] = {
  916. .offset = 0x04, .size = 8, .activate = 6 },
  917. [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
  918. .offset = 0x05, .size = 8, .activate = 6 },
  919. [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
  920. .offset = 0x06, .size = 8, .activate = 3 },
  921. [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
  922. .offset = 0x07, .size = 8, .activate = 4 },
  923. [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
  924. .offset = 0x08, .size = 1, .activate = 7 },
  925. [SCARLETT2_CONFIG_AIR_SWITCH] = {
  926. .offset = 0x09, .size = 1, .activate = 8 },
  927. /* Gen 2 devices: 6i6, 18i8, 18i20 */
  928. }, {
  929. [SCARLETT2_CONFIG_DIM_MUTE] = {
  930. .offset = 0x31, .size = 8, .activate = 2 },
  931. [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
  932. .offset = 0x34, .size = 16, .activate = 1 },
  933. [SCARLETT2_CONFIG_MUTE_SWITCH] = {
  934. .offset = 0x5c, .size = 8, .activate = 1 },
  935. [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
  936. .offset = 0x66, .size = 8, .activate = 3 },
  937. [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
  938. .offset = 0x7c, .size = 8, .activate = 7 },
  939. [SCARLETT2_CONFIG_PAD_SWITCH] = {
  940. .offset = 0x84, .size = 8, .activate = 8 },
  941. [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
  942. .offset = 0x8d, .size = 8, .activate = 6 },
  943. /* Gen 3 devices: 4i4, 8i6, 18i8, 18i20 */
  944. }, {
  945. [SCARLETT2_CONFIG_DIM_MUTE] = {
  946. .offset = 0x31, .size = 8, .activate = 2 },
  947. [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
  948. .offset = 0x34, .size = 16, .activate = 1 },
  949. [SCARLETT2_CONFIG_MUTE_SWITCH] = {
  950. .offset = 0x5c, .size = 8, .activate = 1 },
  951. [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
  952. .offset = 0x66, .size = 8, .activate = 3 },
  953. [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
  954. .offset = 0x7c, .size = 8, .activate = 7 },
  955. [SCARLETT2_CONFIG_PAD_SWITCH] = {
  956. .offset = 0x84, .size = 8, .activate = 8 },
  957. [SCARLETT2_CONFIG_AIR_SWITCH] = {
  958. .offset = 0x8c, .size = 8, .activate = 8 },
  959. [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
  960. .offset = 0x95, .size = 8, .activate = 6 },
  961. [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
  962. .offset = 0x9c, .size = 1, .activate = 8 },
  963. [SCARLETT2_CONFIG_MSD_SWITCH] = {
  964. .offset = 0x9d, .size = 8, .activate = 6 },
  965. [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
  966. .offset = 0x9e, .size = 8, .activate = 6 },
  967. [SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH] = {
  968. .offset = 0x9f, .size = 1, .activate = 10 },
  969. [SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE] = {
  970. .offset = 0xa0, .size = 1, .activate = 10 },
  971. [SCARLETT2_CONFIG_TALKBACK_MAP] = {
  972. .offset = 0xb0, .size = 16, .activate = 10 },
  973. /* Clarett+ 8Pre */
  974. }, {
  975. [SCARLETT2_CONFIG_DIM_MUTE] = {
  976. .offset = 0x31, .size = 8, .activate = 2 },
  977. [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
  978. .offset = 0x34, .size = 16, .activate = 1 },
  979. [SCARLETT2_CONFIG_MUTE_SWITCH] = {
  980. .offset = 0x5c, .size = 8, .activate = 1 },
  981. [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
  982. .offset = 0x66, .size = 8, .activate = 3 },
  983. [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
  984. .offset = 0x7c, .size = 8, .activate = 7 },
  985. [SCARLETT2_CONFIG_AIR_SWITCH] = {
  986. .offset = 0x95, .size = 8, .activate = 8 },
  987. [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
  988. .offset = 0x8d, .size = 8, .activate = 6 },
  989. } };
  990. /* proprietary request/response format */
  991. struct scarlett2_usb_packet {
  992. __le32 cmd;
  993. __le16 size;
  994. __le16 seq;
  995. __le32 error;
  996. __le32 pad;
  997. u8 data[];
  998. };
  999. static void scarlett2_fill_request_header(struct scarlett2_data *private,
  1000. struct scarlett2_usb_packet *req,
  1001. u32 cmd, u16 req_size)
  1002. {
  1003. /* sequence must go up by 1 for each request */
  1004. u16 seq = private->scarlett2_seq++;
  1005. req->cmd = cpu_to_le32(cmd);
  1006. req->size = cpu_to_le16(req_size);
  1007. req->seq = cpu_to_le16(seq);
  1008. req->error = 0;
  1009. req->pad = 0;
  1010. }
  1011. static int scarlett2_usb_tx(struct usb_device *dev, int interface,
  1012. void *buf, u16 size)
  1013. {
  1014. return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
  1015. SCARLETT2_USB_CMD_REQ,
  1016. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
  1017. 0, interface, buf, size);
  1018. }
  1019. static int scarlett2_usb_rx(struct usb_device *dev, int interface,
  1020. u32 usb_req, void *buf, u16 size)
  1021. {
  1022. return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
  1023. usb_req,
  1024. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  1025. 0, interface, buf, size);
  1026. }
  1027. /* Send a proprietary format request to the Scarlett interface */
  1028. static int scarlett2_usb(
  1029. struct usb_mixer_interface *mixer, u32 cmd,
  1030. void *req_data, u16 req_size, void *resp_data, u16 resp_size)
  1031. {
  1032. struct scarlett2_data *private = mixer->private_data;
  1033. struct usb_device *dev = mixer->chip->dev;
  1034. struct scarlett2_usb_packet *req, *resp = NULL;
  1035. size_t req_buf_size = struct_size(req, data, req_size);
  1036. size_t resp_buf_size = struct_size(resp, data, resp_size);
  1037. int err;
  1038. req = kmalloc(req_buf_size, GFP_KERNEL);
  1039. if (!req) {
  1040. err = -ENOMEM;
  1041. goto error;
  1042. }
  1043. resp = kmalloc(resp_buf_size, GFP_KERNEL);
  1044. if (!resp) {
  1045. err = -ENOMEM;
  1046. goto error;
  1047. }
  1048. mutex_lock(&private->usb_mutex);
  1049. /* build request message and send it */
  1050. scarlett2_fill_request_header(private, req, cmd, req_size);
  1051. if (req_size)
  1052. memcpy(req->data, req_data, req_size);
  1053. err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
  1054. req, req_buf_size);
  1055. if (err != req_buf_size) {
  1056. usb_audio_err(
  1057. mixer->chip,
  1058. "Scarlett Gen 2/3 USB request result cmd %x was %d\n",
  1059. cmd, err);
  1060. err = -EINVAL;
  1061. goto unlock;
  1062. }
  1063. /* send a second message to get the response */
  1064. err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
  1065. SCARLETT2_USB_CMD_RESP,
  1066. resp, resp_buf_size);
  1067. /* validate the response */
  1068. if (err != resp_buf_size) {
  1069. usb_audio_err(
  1070. mixer->chip,
  1071. "Scarlett Gen 2/3 USB response result cmd %x was %d "
  1072. "expected %zu\n",
  1073. cmd, err, resp_buf_size);
  1074. err = -EINVAL;
  1075. goto unlock;
  1076. }
  1077. /* cmd/seq/size should match except when initialising
  1078. * seq sent = 1, response = 0
  1079. */
  1080. if (resp->cmd != req->cmd ||
  1081. (resp->seq != req->seq &&
  1082. (le16_to_cpu(req->seq) != 1 || resp->seq != 0)) ||
  1083. resp_size != le16_to_cpu(resp->size) ||
  1084. resp->error ||
  1085. resp->pad) {
  1086. usb_audio_err(
  1087. mixer->chip,
  1088. "Scarlett Gen 2/3 USB invalid response; "
  1089. "cmd tx/rx %d/%d seq %d/%d size %d/%d "
  1090. "error %d pad %d\n",
  1091. le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
  1092. le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
  1093. resp_size, le16_to_cpu(resp->size),
  1094. le32_to_cpu(resp->error),
  1095. le32_to_cpu(resp->pad));
  1096. err = -EINVAL;
  1097. goto unlock;
  1098. }
  1099. if (resp_data && resp_size > 0)
  1100. memcpy(resp_data, resp->data, resp_size);
  1101. unlock:
  1102. mutex_unlock(&private->usb_mutex);
  1103. error:
  1104. kfree(req);
  1105. kfree(resp);
  1106. return err;
  1107. }
  1108. /* Send a USB message to get data; result placed in *buf */
  1109. static int scarlett2_usb_get(
  1110. struct usb_mixer_interface *mixer,
  1111. int offset, void *buf, int size)
  1112. {
  1113. struct {
  1114. __le32 offset;
  1115. __le32 size;
  1116. } __packed req;
  1117. req.offset = cpu_to_le32(offset);
  1118. req.size = cpu_to_le32(size);
  1119. return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
  1120. &req, sizeof(req), buf, size);
  1121. }
  1122. /* Send a USB message to get configuration parameters; result placed in *buf */
  1123. static int scarlett2_usb_get_config(
  1124. struct usb_mixer_interface *mixer,
  1125. int config_item_num, int count, void *buf)
  1126. {
  1127. struct scarlett2_data *private = mixer->private_data;
  1128. const struct scarlett2_device_info *info = private->info;
  1129. const struct scarlett2_config *config_item =
  1130. &scarlett2_config_items[info->config_set][config_item_num];
  1131. int size, err, i;
  1132. u8 *buf_8;
  1133. u8 value;
  1134. /* For byte-sized parameters, retrieve directly into buf */
  1135. if (config_item->size >= 8) {
  1136. size = config_item->size / 8 * count;
  1137. err = scarlett2_usb_get(mixer, config_item->offset, buf, size);
  1138. if (err < 0)
  1139. return err;
  1140. if (size == 2) {
  1141. u16 *buf_16 = buf;
  1142. for (i = 0; i < count; i++, buf_16++)
  1143. *buf_16 = le16_to_cpu(*(__le16 *)buf_16);
  1144. }
  1145. return 0;
  1146. }
  1147. /* For bit-sized parameters, retrieve into value */
  1148. err = scarlett2_usb_get(mixer, config_item->offset, &value, 1);
  1149. if (err < 0)
  1150. return err;
  1151. /* then unpack from value into buf[] */
  1152. buf_8 = buf;
  1153. for (i = 0; i < 8 && i < count; i++, value >>= 1)
  1154. *buf_8++ = value & 1;
  1155. return 0;
  1156. }
  1157. /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
  1158. static void scarlett2_config_save(struct usb_mixer_interface *mixer)
  1159. {
  1160. __le32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE);
  1161. scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
  1162. &req, sizeof(u32),
  1163. NULL, 0);
  1164. }
  1165. /* Delayed work to save config */
  1166. static void scarlett2_config_save_work(struct work_struct *work)
  1167. {
  1168. struct scarlett2_data *private =
  1169. container_of(work, struct scarlett2_data, work.work);
  1170. scarlett2_config_save(private->mixer);
  1171. }
  1172. /* Send a USB message to set a SCARLETT2_CONFIG_* parameter */
  1173. static int scarlett2_usb_set_config(
  1174. struct usb_mixer_interface *mixer,
  1175. int config_item_num, int index, int value)
  1176. {
  1177. struct scarlett2_data *private = mixer->private_data;
  1178. const struct scarlett2_device_info *info = private->info;
  1179. const struct scarlett2_config *config_item =
  1180. &scarlett2_config_items[info->config_set][config_item_num];
  1181. struct {
  1182. __le32 offset;
  1183. __le32 bytes;
  1184. __le32 value;
  1185. } __packed req;
  1186. __le32 req2;
  1187. int offset, size;
  1188. int err;
  1189. /* Cancel any pending NVRAM save */
  1190. cancel_delayed_work_sync(&private->work);
  1191. /* Convert config_item->size in bits to size in bytes and
  1192. * calculate offset
  1193. */
  1194. if (config_item->size >= 8) {
  1195. size = config_item->size / 8;
  1196. offset = config_item->offset + index * size;
  1197. /* If updating a bit, retrieve the old value, set/clear the
  1198. * bit as needed, and update value
  1199. */
  1200. } else {
  1201. u8 tmp;
  1202. size = 1;
  1203. offset = config_item->offset;
  1204. scarlett2_usb_get(mixer, offset, &tmp, 1);
  1205. if (value)
  1206. tmp |= (1 << index);
  1207. else
  1208. tmp &= ~(1 << index);
  1209. value = tmp;
  1210. }
  1211. /* Send the configuration parameter data */
  1212. req.offset = cpu_to_le32(offset);
  1213. req.bytes = cpu_to_le32(size);
  1214. req.value = cpu_to_le32(value);
  1215. err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA,
  1216. &req, sizeof(u32) * 2 + size,
  1217. NULL, 0);
  1218. if (err < 0)
  1219. return err;
  1220. /* Activate the change */
  1221. req2 = cpu_to_le32(config_item->activate);
  1222. err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
  1223. &req2, sizeof(req2), NULL, 0);
  1224. if (err < 0)
  1225. return err;
  1226. /* Schedule the change to be written to NVRAM */
  1227. if (config_item->activate != SCARLETT2_USB_CONFIG_SAVE)
  1228. schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
  1229. return 0;
  1230. }
  1231. /* Send a USB message to get sync status; result placed in *sync */
  1232. static int scarlett2_usb_get_sync_status(
  1233. struct usb_mixer_interface *mixer,
  1234. u8 *sync)
  1235. {
  1236. __le32 data;
  1237. int err;
  1238. err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
  1239. NULL, 0, &data, sizeof(data));
  1240. if (err < 0)
  1241. return err;
  1242. *sync = !!data;
  1243. return 0;
  1244. }
  1245. /* Send a USB message to get volume status; result placed in *buf */
  1246. static int scarlett2_usb_get_volume_status(
  1247. struct usb_mixer_interface *mixer,
  1248. struct scarlett2_usb_volume_status *buf)
  1249. {
  1250. return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET,
  1251. buf, sizeof(*buf));
  1252. }
  1253. /* Send a USB message to get the volumes for all inputs of one mix
  1254. * and put the values into private->mix[]
  1255. */
  1256. static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
  1257. int mix_num)
  1258. {
  1259. struct scarlett2_data *private = mixer->private_data;
  1260. const struct scarlett2_device_info *info = private->info;
  1261. int num_mixer_in =
  1262. info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
  1263. int err, i, j, k;
  1264. struct {
  1265. __le16 mix_num;
  1266. __le16 count;
  1267. } __packed req;
  1268. __le16 data[SCARLETT2_INPUT_MIX_MAX];
  1269. req.mix_num = cpu_to_le16(mix_num);
  1270. req.count = cpu_to_le16(num_mixer_in);
  1271. err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
  1272. &req, sizeof(req),
  1273. data, num_mixer_in * sizeof(u16));
  1274. if (err < 0)
  1275. return err;
  1276. for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++) {
  1277. u16 mixer_value = le16_to_cpu(data[i]);
  1278. for (k = 0; k < SCARLETT2_MIXER_VALUE_COUNT; k++)
  1279. if (scarlett2_mixer_values[k] >= mixer_value)
  1280. break;
  1281. if (k == SCARLETT2_MIXER_VALUE_COUNT)
  1282. k = SCARLETT2_MIXER_MAX_VALUE;
  1283. private->mix[j] = k;
  1284. }
  1285. return 0;
  1286. }
  1287. /* Send a USB message to set the volumes for all inputs of one mix
  1288. * (values obtained from private->mix[])
  1289. */
  1290. static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
  1291. int mix_num)
  1292. {
  1293. struct scarlett2_data *private = mixer->private_data;
  1294. const struct scarlett2_device_info *info = private->info;
  1295. struct {
  1296. __le16 mix_num;
  1297. __le16 data[SCARLETT2_INPUT_MIX_MAX];
  1298. } __packed req;
  1299. int i, j;
  1300. int num_mixer_in =
  1301. info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
  1302. req.mix_num = cpu_to_le16(mix_num);
  1303. for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
  1304. req.data[i] = cpu_to_le16(
  1305. scarlett2_mixer_values[private->mix[j]]
  1306. );
  1307. return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
  1308. &req, (num_mixer_in + 1) * sizeof(u16),
  1309. NULL, 0);
  1310. }
  1311. /* Convert a port number index (per info->port_count) to a hardware ID */
  1312. static u32 scarlett2_mux_src_num_to_id(
  1313. const int port_count[][SCARLETT2_PORT_DIRNS], int num)
  1314. {
  1315. int port_type;
  1316. for (port_type = 0;
  1317. port_type < SCARLETT2_PORT_TYPE_COUNT;
  1318. port_type++) {
  1319. if (num < port_count[port_type][SCARLETT2_PORT_IN])
  1320. return scarlett2_ports[port_type].id | num;
  1321. num -= port_count[port_type][SCARLETT2_PORT_IN];
  1322. }
  1323. /* Oops */
  1324. return 0;
  1325. }
  1326. /* Convert a hardware ID to a port number index */
  1327. static u32 scarlett2_mux_id_to_num(
  1328. const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
  1329. {
  1330. int port_type;
  1331. int port_num = 0;
  1332. for (port_type = 0;
  1333. port_type < SCARLETT2_PORT_TYPE_COUNT;
  1334. port_type++) {
  1335. int base = scarlett2_ports[port_type].id;
  1336. int count = port_count[port_type][direction];
  1337. if (id >= base && id < base + count)
  1338. return port_num + id - base;
  1339. port_num += count;
  1340. }
  1341. /* Oops */
  1342. return -1;
  1343. }
  1344. /* Convert one mux entry from the interface and load into private->mux[] */
  1345. static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
  1346. u32 mux_entry)
  1347. {
  1348. const struct scarlett2_device_info *info = private->info;
  1349. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  1350. int dst_idx, src_idx;
  1351. dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
  1352. mux_entry & 0xFFF);
  1353. if (dst_idx < 0)
  1354. return;
  1355. if (dst_idx >= private->num_mux_dsts) {
  1356. usb_audio_err(private->mixer->chip,
  1357. "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
  1358. mux_entry, dst_idx, private->num_mux_dsts);
  1359. return;
  1360. }
  1361. src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
  1362. mux_entry >> 12);
  1363. if (src_idx < 0)
  1364. return;
  1365. if (src_idx >= private->num_mux_srcs) {
  1366. usb_audio_err(private->mixer->chip,
  1367. "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
  1368. mux_entry, src_idx, private->num_mux_srcs);
  1369. return;
  1370. }
  1371. private->mux[dst_idx] = src_idx;
  1372. }
  1373. /* Send USB message to get mux inputs and then populate private->mux[] */
  1374. static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
  1375. {
  1376. struct scarlett2_data *private = mixer->private_data;
  1377. int count = private->num_mux_dsts;
  1378. int err, i;
  1379. struct {
  1380. __le16 num;
  1381. __le16 count;
  1382. } __packed req;
  1383. __le32 data[SCARLETT2_MUX_MAX];
  1384. private->mux_updated = 0;
  1385. req.num = 0;
  1386. req.count = cpu_to_le16(count);
  1387. err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
  1388. &req, sizeof(req),
  1389. data, count * sizeof(u32));
  1390. if (err < 0)
  1391. return err;
  1392. for (i = 0; i < count; i++)
  1393. scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
  1394. return 0;
  1395. }
  1396. /* Send USB messages to set mux inputs */
  1397. static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
  1398. {
  1399. struct scarlett2_data *private = mixer->private_data;
  1400. const struct scarlett2_device_info *info = private->info;
  1401. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  1402. int table;
  1403. struct {
  1404. __le16 pad;
  1405. __le16 num;
  1406. __le32 data[SCARLETT2_MUX_MAX];
  1407. } __packed req;
  1408. req.pad = 0;
  1409. /* set mux settings for each rate */
  1410. for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
  1411. const struct scarlett2_mux_entry *entry;
  1412. /* i counts over the output array */
  1413. int i = 0, err;
  1414. req.num = cpu_to_le16(table);
  1415. /* loop through each entry */
  1416. for (entry = info->mux_assignment[table];
  1417. entry->count;
  1418. entry++) {
  1419. int j;
  1420. int port_type = entry->port_type;
  1421. int port_idx = entry->start;
  1422. int mux_idx = scarlett2_get_port_start_num(port_count,
  1423. SCARLETT2_PORT_OUT, port_type) + port_idx;
  1424. int dst_id = scarlett2_ports[port_type].id + port_idx;
  1425. /* Empty slots */
  1426. if (!dst_id) {
  1427. for (j = 0; j < entry->count; j++)
  1428. req.data[i++] = 0;
  1429. continue;
  1430. }
  1431. /* Non-empty mux slots use the lower 12 bits
  1432. * for the destination and next 12 bits for
  1433. * the source
  1434. */
  1435. for (j = 0; j < entry->count; j++) {
  1436. int src_id = scarlett2_mux_src_num_to_id(
  1437. port_count, private->mux[mux_idx++]);
  1438. req.data[i++] = cpu_to_le32(dst_id |
  1439. src_id << 12);
  1440. dst_id++;
  1441. }
  1442. }
  1443. err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
  1444. &req, (i + 1) * sizeof(u32),
  1445. NULL, 0);
  1446. if (err < 0)
  1447. return err;
  1448. }
  1449. return 0;
  1450. }
  1451. /* Send USB message to get meter levels */
  1452. static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
  1453. u16 num_meters, u16 *levels)
  1454. {
  1455. struct {
  1456. __le16 pad;
  1457. __le16 num_meters;
  1458. __le32 magic;
  1459. } __packed req;
  1460. u32 resp[SCARLETT2_MAX_METERS];
  1461. int i, err;
  1462. req.pad = 0;
  1463. req.num_meters = cpu_to_le16(num_meters);
  1464. req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
  1465. err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
  1466. &req, sizeof(req), resp, num_meters * sizeof(u32));
  1467. if (err < 0)
  1468. return err;
  1469. /* copy, convert to u16 */
  1470. for (i = 0; i < num_meters; i++)
  1471. levels[i] = resp[i];
  1472. return 0;
  1473. }
  1474. /*** Control Functions ***/
  1475. /* helper function to create a new control */
  1476. static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
  1477. const struct snd_kcontrol_new *ncontrol,
  1478. int index, int channels, const char *name,
  1479. struct snd_kcontrol **kctl_return)
  1480. {
  1481. struct snd_kcontrol *kctl;
  1482. struct usb_mixer_elem_info *elem;
  1483. int err;
  1484. elem = kzalloc(sizeof(*elem), GFP_KERNEL);
  1485. if (!elem)
  1486. return -ENOMEM;
  1487. /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
  1488. * ignores them for resume and other operations.
  1489. * Also, the head.id field is set to 0, as we don't use this field.
  1490. */
  1491. elem->head.mixer = mixer;
  1492. elem->control = index;
  1493. elem->head.id = 0;
  1494. elem->channels = channels;
  1495. elem->val_type = USB_MIXER_BESPOKEN;
  1496. kctl = snd_ctl_new1(ncontrol, elem);
  1497. if (!kctl) {
  1498. kfree(elem);
  1499. return -ENOMEM;
  1500. }
  1501. kctl->private_free = snd_usb_mixer_elem_free;
  1502. strscpy(kctl->id.name, name, sizeof(kctl->id.name));
  1503. err = snd_usb_mixer_add_control(&elem->head, kctl);
  1504. if (err < 0)
  1505. return err;
  1506. if (kctl_return)
  1507. *kctl_return = kctl;
  1508. return 0;
  1509. }
  1510. /*** Sync Control ***/
  1511. /* Update sync control after receiving notification that the status
  1512. * has changed
  1513. */
  1514. static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
  1515. {
  1516. struct scarlett2_data *private = mixer->private_data;
  1517. private->sync_updated = 0;
  1518. return scarlett2_usb_get_sync_status(mixer, &private->sync);
  1519. }
  1520. static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
  1521. struct snd_ctl_elem_info *uinfo)
  1522. {
  1523. static const char *texts[2] = {
  1524. "Unlocked", "Locked"
  1525. };
  1526. return snd_ctl_enum_info(uinfo, 1, 2, texts);
  1527. }
  1528. static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
  1529. struct snd_ctl_elem_value *ucontrol)
  1530. {
  1531. struct usb_mixer_elem_info *elem = kctl->private_data;
  1532. struct usb_mixer_interface *mixer = elem->head.mixer;
  1533. struct scarlett2_data *private = mixer->private_data;
  1534. mutex_lock(&private->data_mutex);
  1535. if (private->sync_updated)
  1536. scarlett2_update_sync(mixer);
  1537. ucontrol->value.enumerated.item[0] = private->sync;
  1538. mutex_unlock(&private->data_mutex);
  1539. return 0;
  1540. }
  1541. static const struct snd_kcontrol_new scarlett2_sync_ctl = {
  1542. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1543. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1544. .name = "",
  1545. .info = scarlett2_sync_ctl_info,
  1546. .get = scarlett2_sync_ctl_get
  1547. };
  1548. static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
  1549. {
  1550. struct scarlett2_data *private = mixer->private_data;
  1551. /* devices without a mixer also don't support reporting sync status */
  1552. if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
  1553. return 0;
  1554. return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
  1555. 0, 1, "Sync Status", &private->sync_ctl);
  1556. }
  1557. /*** Analogue Line Out Volume Controls ***/
  1558. /* Update hardware volume controls after receiving notification that
  1559. * they have changed
  1560. */
  1561. static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
  1562. {
  1563. struct scarlett2_data *private = mixer->private_data;
  1564. const struct scarlett2_device_info *info = private->info;
  1565. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  1566. struct scarlett2_usb_volume_status volume_status;
  1567. int num_line_out =
  1568. port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
  1569. int err, i;
  1570. int mute;
  1571. private->vol_updated = 0;
  1572. err = scarlett2_usb_get_volume_status(mixer, &volume_status);
  1573. if (err < 0)
  1574. return err;
  1575. private->master_vol = clamp(
  1576. volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
  1577. 0, SCARLETT2_VOLUME_BIAS);
  1578. if (info->line_out_hw_vol)
  1579. for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
  1580. private->dim_mute[i] = !!volume_status.dim_mute[i];
  1581. mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];
  1582. for (i = 0; i < num_line_out; i++)
  1583. if (private->vol_sw_hw_switch[i]) {
  1584. private->vol[i] = private->master_vol;
  1585. private->mute_switch[i] = mute;
  1586. }
  1587. return 0;
  1588. }
  1589. static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
  1590. struct snd_ctl_elem_info *uinfo)
  1591. {
  1592. struct usb_mixer_elem_info *elem = kctl->private_data;
  1593. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1594. uinfo->count = elem->channels;
  1595. uinfo->value.integer.min = 0;
  1596. uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
  1597. uinfo->value.integer.step = 1;
  1598. return 0;
  1599. }
  1600. static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
  1601. struct snd_ctl_elem_value *ucontrol)
  1602. {
  1603. struct usb_mixer_elem_info *elem = kctl->private_data;
  1604. struct usb_mixer_interface *mixer = elem->head.mixer;
  1605. struct scarlett2_data *private = mixer->private_data;
  1606. mutex_lock(&private->data_mutex);
  1607. if (private->vol_updated)
  1608. scarlett2_update_volumes(mixer);
  1609. mutex_unlock(&private->data_mutex);
  1610. ucontrol->value.integer.value[0] = private->master_vol;
  1611. return 0;
  1612. }
  1613. static int line_out_remap(struct scarlett2_data *private, int index)
  1614. {
  1615. const struct scarlett2_device_info *info = private->info;
  1616. if (!info->line_out_remap_enable)
  1617. return index;
  1618. return info->line_out_remap[index];
  1619. }
  1620. static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
  1621. struct snd_ctl_elem_value *ucontrol)
  1622. {
  1623. struct usb_mixer_elem_info *elem = kctl->private_data;
  1624. struct usb_mixer_interface *mixer = elem->head.mixer;
  1625. struct scarlett2_data *private = mixer->private_data;
  1626. int index = line_out_remap(private, elem->control);
  1627. mutex_lock(&private->data_mutex);
  1628. if (private->vol_updated)
  1629. scarlett2_update_volumes(mixer);
  1630. mutex_unlock(&private->data_mutex);
  1631. ucontrol->value.integer.value[0] = private->vol[index];
  1632. return 0;
  1633. }
  1634. static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
  1635. struct snd_ctl_elem_value *ucontrol)
  1636. {
  1637. struct usb_mixer_elem_info *elem = kctl->private_data;
  1638. struct usb_mixer_interface *mixer = elem->head.mixer;
  1639. struct scarlett2_data *private = mixer->private_data;
  1640. int index = line_out_remap(private, elem->control);
  1641. int oval, val, err = 0;
  1642. mutex_lock(&private->data_mutex);
  1643. oval = private->vol[index];
  1644. val = ucontrol->value.integer.value[0];
  1645. if (oval == val)
  1646. goto unlock;
  1647. private->vol[index] = val;
  1648. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
  1649. index, val - SCARLETT2_VOLUME_BIAS);
  1650. if (err == 0)
  1651. err = 1;
  1652. unlock:
  1653. mutex_unlock(&private->data_mutex);
  1654. return err;
  1655. }
  1656. static const DECLARE_TLV_DB_MINMAX(
  1657. db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0
  1658. );
  1659. static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
  1660. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1661. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  1662. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  1663. .name = "",
  1664. .info = scarlett2_volume_ctl_info,
  1665. .get = scarlett2_master_volume_ctl_get,
  1666. .private_value = 0, /* max value */
  1667. .tlv = { .p = db_scale_scarlett2_gain }
  1668. };
  1669. static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
  1670. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1671. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  1672. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  1673. .name = "",
  1674. .info = scarlett2_volume_ctl_info,
  1675. .get = scarlett2_volume_ctl_get,
  1676. .put = scarlett2_volume_ctl_put,
  1677. .private_value = 0, /* max value */
  1678. .tlv = { .p = db_scale_scarlett2_gain }
  1679. };
  1680. /*** Mute Switch Controls ***/
  1681. static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
  1682. struct snd_ctl_elem_value *ucontrol)
  1683. {
  1684. struct usb_mixer_elem_info *elem = kctl->private_data;
  1685. struct usb_mixer_interface *mixer = elem->head.mixer;
  1686. struct scarlett2_data *private = mixer->private_data;
  1687. int index = line_out_remap(private, elem->control);
  1688. mutex_lock(&private->data_mutex);
  1689. if (private->vol_updated)
  1690. scarlett2_update_volumes(mixer);
  1691. mutex_unlock(&private->data_mutex);
  1692. ucontrol->value.integer.value[0] = private->mute_switch[index];
  1693. return 0;
  1694. }
  1695. static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
  1696. struct snd_ctl_elem_value *ucontrol)
  1697. {
  1698. struct usb_mixer_elem_info *elem = kctl->private_data;
  1699. struct usb_mixer_interface *mixer = elem->head.mixer;
  1700. struct scarlett2_data *private = mixer->private_data;
  1701. int index = line_out_remap(private, elem->control);
  1702. int oval, val, err = 0;
  1703. mutex_lock(&private->data_mutex);
  1704. oval = private->mute_switch[index];
  1705. val = !!ucontrol->value.integer.value[0];
  1706. if (oval == val)
  1707. goto unlock;
  1708. private->mute_switch[index] = val;
  1709. /* Send mute change to the device */
  1710. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
  1711. index, val);
  1712. if (err == 0)
  1713. err = 1;
  1714. unlock:
  1715. mutex_unlock(&private->data_mutex);
  1716. return err;
  1717. }
  1718. static const struct snd_kcontrol_new scarlett2_mute_ctl = {
  1719. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1720. .name = "",
  1721. .info = snd_ctl_boolean_mono_info,
  1722. .get = scarlett2_mute_ctl_get,
  1723. .put = scarlett2_mute_ctl_put,
  1724. };
  1725. /*** HW/SW Volume Switch Controls ***/
  1726. static void scarlett2_sw_hw_ctl_ro(struct scarlett2_data *private, int index)
  1727. {
  1728. private->sw_hw_ctls[index]->vd[0].access &=
  1729. ~SNDRV_CTL_ELEM_ACCESS_WRITE;
  1730. }
  1731. static void scarlett2_sw_hw_ctl_rw(struct scarlett2_data *private, int index)
  1732. {
  1733. private->sw_hw_ctls[index]->vd[0].access |=
  1734. SNDRV_CTL_ELEM_ACCESS_WRITE;
  1735. }
  1736. static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
  1737. struct snd_ctl_elem_info *uinfo)
  1738. {
  1739. static const char *const values[2] = {
  1740. "SW", "HW"
  1741. };
  1742. return snd_ctl_enum_info(uinfo, 1, 2, values);
  1743. }
  1744. static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
  1745. struct snd_ctl_elem_value *ucontrol)
  1746. {
  1747. struct usb_mixer_elem_info *elem = kctl->private_data;
  1748. struct scarlett2_data *private = elem->head.mixer->private_data;
  1749. int index = line_out_remap(private, elem->control);
  1750. ucontrol->value.enumerated.item[0] = private->vol_sw_hw_switch[index];
  1751. return 0;
  1752. }
  1753. static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
  1754. int index, int value)
  1755. {
  1756. struct scarlett2_data *private = mixer->private_data;
  1757. struct snd_card *card = mixer->chip->card;
  1758. /* Set/Clear write bits */
  1759. if (value) {
  1760. private->vol_ctls[index]->vd[0].access |=
  1761. SNDRV_CTL_ELEM_ACCESS_WRITE;
  1762. private->mute_ctls[index]->vd[0].access |=
  1763. SNDRV_CTL_ELEM_ACCESS_WRITE;
  1764. } else {
  1765. private->vol_ctls[index]->vd[0].access &=
  1766. ~SNDRV_CTL_ELEM_ACCESS_WRITE;
  1767. private->mute_ctls[index]->vd[0].access &=
  1768. ~SNDRV_CTL_ELEM_ACCESS_WRITE;
  1769. }
  1770. /* Notify of write bit and possible value change */
  1771. snd_ctl_notify(card,
  1772. SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
  1773. &private->vol_ctls[index]->id);
  1774. snd_ctl_notify(card,
  1775. SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
  1776. &private->mute_ctls[index]->id);
  1777. }
  1778. static int scarlett2_sw_hw_change(struct usb_mixer_interface *mixer,
  1779. int ctl_index, int val)
  1780. {
  1781. struct scarlett2_data *private = mixer->private_data;
  1782. int index = line_out_remap(private, ctl_index);
  1783. int err;
  1784. private->vol_sw_hw_switch[index] = val;
  1785. /* Change access mode to RO (hardware controlled volume)
  1786. * or RW (software controlled volume)
  1787. */
  1788. scarlett2_vol_ctl_set_writable(mixer, ctl_index, !val);
  1789. /* Reset volume/mute to master volume/mute */
  1790. private->vol[index] = private->master_vol;
  1791. private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE];
  1792. /* Set SW volume to current HW volume */
  1793. err = scarlett2_usb_set_config(
  1794. mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
  1795. index, private->master_vol - SCARLETT2_VOLUME_BIAS);
  1796. if (err < 0)
  1797. return err;
  1798. /* Set SW mute to current HW mute */
  1799. err = scarlett2_usb_set_config(
  1800. mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
  1801. index, private->dim_mute[SCARLETT2_BUTTON_MUTE]);
  1802. if (err < 0)
  1803. return err;
  1804. /* Send SW/HW switch change to the device */
  1805. return scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
  1806. index, val);
  1807. }
  1808. static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
  1809. struct snd_ctl_elem_value *ucontrol)
  1810. {
  1811. struct usb_mixer_elem_info *elem = kctl->private_data;
  1812. struct usb_mixer_interface *mixer = elem->head.mixer;
  1813. struct scarlett2_data *private = mixer->private_data;
  1814. int ctl_index = elem->control;
  1815. int index = line_out_remap(private, ctl_index);
  1816. int oval, val, err = 0;
  1817. mutex_lock(&private->data_mutex);
  1818. oval = private->vol_sw_hw_switch[index];
  1819. val = !!ucontrol->value.enumerated.item[0];
  1820. if (oval == val)
  1821. goto unlock;
  1822. err = scarlett2_sw_hw_change(mixer, ctl_index, val);
  1823. if (err == 0)
  1824. err = 1;
  1825. unlock:
  1826. mutex_unlock(&private->data_mutex);
  1827. return err;
  1828. }
  1829. static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
  1830. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1831. .name = "",
  1832. .info = scarlett2_sw_hw_enum_ctl_info,
  1833. .get = scarlett2_sw_hw_enum_ctl_get,
  1834. .put = scarlett2_sw_hw_enum_ctl_put,
  1835. };
  1836. /*** Line Level/Instrument Level Switch Controls ***/
  1837. static int scarlett2_update_input_other(struct usb_mixer_interface *mixer)
  1838. {
  1839. struct scarlett2_data *private = mixer->private_data;
  1840. const struct scarlett2_device_info *info = private->info;
  1841. private->input_other_updated = 0;
  1842. if (info->level_input_count) {
  1843. int err = scarlett2_usb_get_config(
  1844. mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
  1845. info->level_input_count + info->level_input_first,
  1846. private->level_switch);
  1847. if (err < 0)
  1848. return err;
  1849. }
  1850. if (info->pad_input_count) {
  1851. int err = scarlett2_usb_get_config(
  1852. mixer, SCARLETT2_CONFIG_PAD_SWITCH,
  1853. info->pad_input_count, private->pad_switch);
  1854. if (err < 0)
  1855. return err;
  1856. }
  1857. if (info->air_input_count) {
  1858. int err = scarlett2_usb_get_config(
  1859. mixer, SCARLETT2_CONFIG_AIR_SWITCH,
  1860. info->air_input_count, private->air_switch);
  1861. if (err < 0)
  1862. return err;
  1863. }
  1864. if (info->phantom_count) {
  1865. int err = scarlett2_usb_get_config(
  1866. mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
  1867. info->phantom_count, private->phantom_switch);
  1868. if (err < 0)
  1869. return err;
  1870. err = scarlett2_usb_get_config(
  1871. mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
  1872. 1, &private->phantom_persistence);
  1873. if (err < 0)
  1874. return err;
  1875. }
  1876. return 0;
  1877. }
  1878. static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
  1879. struct snd_ctl_elem_info *uinfo)
  1880. {
  1881. static const char *const values[2] = {
  1882. "Line", "Inst"
  1883. };
  1884. return snd_ctl_enum_info(uinfo, 1, 2, values);
  1885. }
  1886. static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
  1887. struct snd_ctl_elem_value *ucontrol)
  1888. {
  1889. struct usb_mixer_elem_info *elem = kctl->private_data;
  1890. struct usb_mixer_interface *mixer = elem->head.mixer;
  1891. struct scarlett2_data *private = mixer->private_data;
  1892. const struct scarlett2_device_info *info = private->info;
  1893. int index = elem->control + info->level_input_first;
  1894. mutex_lock(&private->data_mutex);
  1895. if (private->input_other_updated)
  1896. scarlett2_update_input_other(mixer);
  1897. ucontrol->value.enumerated.item[0] = private->level_switch[index];
  1898. mutex_unlock(&private->data_mutex);
  1899. return 0;
  1900. }
  1901. static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
  1902. struct snd_ctl_elem_value *ucontrol)
  1903. {
  1904. struct usb_mixer_elem_info *elem = kctl->private_data;
  1905. struct usb_mixer_interface *mixer = elem->head.mixer;
  1906. struct scarlett2_data *private = mixer->private_data;
  1907. const struct scarlett2_device_info *info = private->info;
  1908. int index = elem->control + info->level_input_first;
  1909. int oval, val, err = 0;
  1910. mutex_lock(&private->data_mutex);
  1911. oval = private->level_switch[index];
  1912. val = !!ucontrol->value.enumerated.item[0];
  1913. if (oval == val)
  1914. goto unlock;
  1915. private->level_switch[index] = val;
  1916. /* Send switch change to the device */
  1917. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
  1918. index, val);
  1919. if (err == 0)
  1920. err = 1;
  1921. unlock:
  1922. mutex_unlock(&private->data_mutex);
  1923. return err;
  1924. }
  1925. static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
  1926. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1927. .name = "",
  1928. .info = scarlett2_level_enum_ctl_info,
  1929. .get = scarlett2_level_enum_ctl_get,
  1930. .put = scarlett2_level_enum_ctl_put,
  1931. };
  1932. /*** Pad Switch Controls ***/
  1933. static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
  1934. struct snd_ctl_elem_value *ucontrol)
  1935. {
  1936. struct usb_mixer_elem_info *elem = kctl->private_data;
  1937. struct usb_mixer_interface *mixer = elem->head.mixer;
  1938. struct scarlett2_data *private = mixer->private_data;
  1939. mutex_lock(&private->data_mutex);
  1940. if (private->input_other_updated)
  1941. scarlett2_update_input_other(mixer);
  1942. ucontrol->value.integer.value[0] =
  1943. private->pad_switch[elem->control];
  1944. mutex_unlock(&private->data_mutex);
  1945. return 0;
  1946. }
  1947. static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
  1948. struct snd_ctl_elem_value *ucontrol)
  1949. {
  1950. struct usb_mixer_elem_info *elem = kctl->private_data;
  1951. struct usb_mixer_interface *mixer = elem->head.mixer;
  1952. struct scarlett2_data *private = mixer->private_data;
  1953. int index = elem->control;
  1954. int oval, val, err = 0;
  1955. mutex_lock(&private->data_mutex);
  1956. oval = private->pad_switch[index];
  1957. val = !!ucontrol->value.integer.value[0];
  1958. if (oval == val)
  1959. goto unlock;
  1960. private->pad_switch[index] = val;
  1961. /* Send switch change to the device */
  1962. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
  1963. index, val);
  1964. if (err == 0)
  1965. err = 1;
  1966. unlock:
  1967. mutex_unlock(&private->data_mutex);
  1968. return err;
  1969. }
  1970. static const struct snd_kcontrol_new scarlett2_pad_ctl = {
  1971. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1972. .name = "",
  1973. .info = snd_ctl_boolean_mono_info,
  1974. .get = scarlett2_pad_ctl_get,
  1975. .put = scarlett2_pad_ctl_put,
  1976. };
  1977. /*** Air Switch Controls ***/
  1978. static int scarlett2_air_ctl_get(struct snd_kcontrol *kctl,
  1979. struct snd_ctl_elem_value *ucontrol)
  1980. {
  1981. struct usb_mixer_elem_info *elem = kctl->private_data;
  1982. struct usb_mixer_interface *mixer = elem->head.mixer;
  1983. struct scarlett2_data *private = mixer->private_data;
  1984. mutex_lock(&private->data_mutex);
  1985. if (private->input_other_updated)
  1986. scarlett2_update_input_other(mixer);
  1987. ucontrol->value.integer.value[0] = private->air_switch[elem->control];
  1988. mutex_unlock(&private->data_mutex);
  1989. return 0;
  1990. }
  1991. static int scarlett2_air_ctl_put(struct snd_kcontrol *kctl,
  1992. struct snd_ctl_elem_value *ucontrol)
  1993. {
  1994. struct usb_mixer_elem_info *elem = kctl->private_data;
  1995. struct usb_mixer_interface *mixer = elem->head.mixer;
  1996. struct scarlett2_data *private = mixer->private_data;
  1997. int index = elem->control;
  1998. int oval, val, err = 0;
  1999. mutex_lock(&private->data_mutex);
  2000. oval = private->air_switch[index];
  2001. val = !!ucontrol->value.integer.value[0];
  2002. if (oval == val)
  2003. goto unlock;
  2004. private->air_switch[index] = val;
  2005. /* Send switch change to the device */
  2006. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_AIR_SWITCH,
  2007. index, val);
  2008. if (err == 0)
  2009. err = 1;
  2010. unlock:
  2011. mutex_unlock(&private->data_mutex);
  2012. return err;
  2013. }
  2014. static const struct snd_kcontrol_new scarlett2_air_ctl = {
  2015. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2016. .name = "",
  2017. .info = snd_ctl_boolean_mono_info,
  2018. .get = scarlett2_air_ctl_get,
  2019. .put = scarlett2_air_ctl_put,
  2020. };
  2021. /*** Phantom Switch Controls ***/
  2022. static int scarlett2_phantom_ctl_get(struct snd_kcontrol *kctl,
  2023. struct snd_ctl_elem_value *ucontrol)
  2024. {
  2025. struct usb_mixer_elem_info *elem = kctl->private_data;
  2026. struct usb_mixer_interface *mixer = elem->head.mixer;
  2027. struct scarlett2_data *private = mixer->private_data;
  2028. mutex_lock(&private->data_mutex);
  2029. if (private->input_other_updated)
  2030. scarlett2_update_input_other(mixer);
  2031. ucontrol->value.integer.value[0] =
  2032. private->phantom_switch[elem->control];
  2033. mutex_unlock(&private->data_mutex);
  2034. return 0;
  2035. }
  2036. static int scarlett2_phantom_ctl_put(struct snd_kcontrol *kctl,
  2037. struct snd_ctl_elem_value *ucontrol)
  2038. {
  2039. struct usb_mixer_elem_info *elem = kctl->private_data;
  2040. struct usb_mixer_interface *mixer = elem->head.mixer;
  2041. struct scarlett2_data *private = mixer->private_data;
  2042. int index = elem->control;
  2043. int oval, val, err = 0;
  2044. mutex_lock(&private->data_mutex);
  2045. oval = private->phantom_switch[index];
  2046. val = !!ucontrol->value.integer.value[0];
  2047. if (oval == val)
  2048. goto unlock;
  2049. private->phantom_switch[index] = val;
  2050. /* Send switch change to the device */
  2051. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
  2052. index, val);
  2053. if (err == 0)
  2054. err = 1;
  2055. unlock:
  2056. mutex_unlock(&private->data_mutex);
  2057. return err;
  2058. }
  2059. static const struct snd_kcontrol_new scarlett2_phantom_ctl = {
  2060. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2061. .name = "",
  2062. .info = snd_ctl_boolean_mono_info,
  2063. .get = scarlett2_phantom_ctl_get,
  2064. .put = scarlett2_phantom_ctl_put,
  2065. };
  2066. /*** Phantom Persistence Control ***/
  2067. static int scarlett2_phantom_persistence_ctl_get(
  2068. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  2069. {
  2070. struct usb_mixer_elem_info *elem = kctl->private_data;
  2071. struct scarlett2_data *private = elem->head.mixer->private_data;
  2072. ucontrol->value.integer.value[0] = private->phantom_persistence;
  2073. return 0;
  2074. }
  2075. static int scarlett2_phantom_persistence_ctl_put(
  2076. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  2077. {
  2078. struct usb_mixer_elem_info *elem = kctl->private_data;
  2079. struct usb_mixer_interface *mixer = elem->head.mixer;
  2080. struct scarlett2_data *private = mixer->private_data;
  2081. int index = elem->control;
  2082. int oval, val, err = 0;
  2083. mutex_lock(&private->data_mutex);
  2084. oval = private->phantom_persistence;
  2085. val = !!ucontrol->value.integer.value[0];
  2086. if (oval == val)
  2087. goto unlock;
  2088. private->phantom_persistence = val;
  2089. /* Send switch change to the device */
  2090. err = scarlett2_usb_set_config(
  2091. mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, index, val);
  2092. if (err == 0)
  2093. err = 1;
  2094. unlock:
  2095. mutex_unlock(&private->data_mutex);
  2096. return err;
  2097. }
  2098. static const struct snd_kcontrol_new scarlett2_phantom_persistence_ctl = {
  2099. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2100. .name = "",
  2101. .info = snd_ctl_boolean_mono_info,
  2102. .get = scarlett2_phantom_persistence_ctl_get,
  2103. .put = scarlett2_phantom_persistence_ctl_put,
  2104. };
  2105. /*** Direct Monitor Control ***/
  2106. static int scarlett2_update_monitor_other(struct usb_mixer_interface *mixer)
  2107. {
  2108. struct scarlett2_data *private = mixer->private_data;
  2109. const struct scarlett2_device_info *info = private->info;
  2110. int err;
  2111. /* monitor_other_enable[0] enables speaker switching
  2112. * monitor_other_enable[1] enables talkback
  2113. */
  2114. u8 monitor_other_enable[2];
  2115. /* monitor_other_switch[0] activates the alternate speakers
  2116. * monitor_other_switch[1] activates talkback
  2117. */
  2118. u8 monitor_other_switch[2];
  2119. private->monitor_other_updated = 0;
  2120. if (info->direct_monitor)
  2121. return scarlett2_usb_get_config(
  2122. mixer, SCARLETT2_CONFIG_DIRECT_MONITOR,
  2123. 1, &private->direct_monitor_switch);
  2124. /* if it doesn't do speaker switching then it also doesn't do
  2125. * talkback
  2126. */
  2127. if (!info->has_speaker_switching)
  2128. return 0;
  2129. err = scarlett2_usb_get_config(
  2130. mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
  2131. 2, monitor_other_enable);
  2132. if (err < 0)
  2133. return err;
  2134. err = scarlett2_usb_get_config(
  2135. mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
  2136. 2, monitor_other_switch);
  2137. if (err < 0)
  2138. return err;
  2139. if (!monitor_other_enable[0])
  2140. private->speaker_switching_switch = 0;
  2141. else
  2142. private->speaker_switching_switch = monitor_other_switch[0] + 1;
  2143. if (info->has_talkback) {
  2144. const int (*port_count)[SCARLETT2_PORT_DIRNS] =
  2145. info->port_count;
  2146. int num_mixes =
  2147. port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
  2148. u16 bitmap;
  2149. int i;
  2150. if (!monitor_other_enable[1])
  2151. private->talkback_switch = 0;
  2152. else
  2153. private->talkback_switch = monitor_other_switch[1] + 1;
  2154. err = scarlett2_usb_get_config(mixer,
  2155. SCARLETT2_CONFIG_TALKBACK_MAP,
  2156. 1, &bitmap);
  2157. if (err < 0)
  2158. return err;
  2159. for (i = 0; i < num_mixes; i++, bitmap >>= 1)
  2160. private->talkback_map[i] = bitmap & 1;
  2161. }
  2162. return 0;
  2163. }
  2164. static int scarlett2_direct_monitor_ctl_get(
  2165. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  2166. {
  2167. struct usb_mixer_elem_info *elem = kctl->private_data;
  2168. struct usb_mixer_interface *mixer = elem->head.mixer;
  2169. struct scarlett2_data *private = elem->head.mixer->private_data;
  2170. mutex_lock(&private->data_mutex);
  2171. if (private->monitor_other_updated)
  2172. scarlett2_update_monitor_other(mixer);
  2173. ucontrol->value.enumerated.item[0] = private->direct_monitor_switch;
  2174. mutex_unlock(&private->data_mutex);
  2175. return 0;
  2176. }
  2177. static int scarlett2_direct_monitor_ctl_put(
  2178. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  2179. {
  2180. struct usb_mixer_elem_info *elem = kctl->private_data;
  2181. struct usb_mixer_interface *mixer = elem->head.mixer;
  2182. struct scarlett2_data *private = mixer->private_data;
  2183. int index = elem->control;
  2184. int oval, val, err = 0;
  2185. mutex_lock(&private->data_mutex);
  2186. oval = private->direct_monitor_switch;
  2187. val = min(ucontrol->value.enumerated.item[0], 2U);
  2188. if (oval == val)
  2189. goto unlock;
  2190. private->direct_monitor_switch = val;
  2191. /* Send switch change to the device */
  2192. err = scarlett2_usb_set_config(
  2193. mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, index, val);
  2194. if (err == 0)
  2195. err = 1;
  2196. unlock:
  2197. mutex_unlock(&private->data_mutex);
  2198. return err;
  2199. }
  2200. static int scarlett2_direct_monitor_stereo_enum_ctl_info(
  2201. struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
  2202. {
  2203. static const char *const values[3] = {
  2204. "Off", "Mono", "Stereo"
  2205. };
  2206. return snd_ctl_enum_info(uinfo, 1, 3, values);
  2207. }
  2208. /* Direct Monitor for Solo is mono-only and only needs a boolean control
  2209. * Direct Monitor for 2i2 is selectable between Off/Mono/Stereo
  2210. */
  2211. static const struct snd_kcontrol_new scarlett2_direct_monitor_ctl[2] = {
  2212. {
  2213. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2214. .name = "",
  2215. .info = snd_ctl_boolean_mono_info,
  2216. .get = scarlett2_direct_monitor_ctl_get,
  2217. .put = scarlett2_direct_monitor_ctl_put,
  2218. },
  2219. {
  2220. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2221. .name = "",
  2222. .info = scarlett2_direct_monitor_stereo_enum_ctl_info,
  2223. .get = scarlett2_direct_monitor_ctl_get,
  2224. .put = scarlett2_direct_monitor_ctl_put,
  2225. }
  2226. };
  2227. static int scarlett2_add_direct_monitor_ctl(struct usb_mixer_interface *mixer)
  2228. {
  2229. struct scarlett2_data *private = mixer->private_data;
  2230. const struct scarlett2_device_info *info = private->info;
  2231. const char *s;
  2232. if (!info->direct_monitor)
  2233. return 0;
  2234. s = info->direct_monitor == 1
  2235. ? "Direct Monitor Playback Switch"
  2236. : "Direct Monitor Playback Enum";
  2237. return scarlett2_add_new_ctl(
  2238. mixer, &scarlett2_direct_monitor_ctl[info->direct_monitor - 1],
  2239. 0, 1, s, &private->direct_monitor_ctl);
  2240. }
  2241. /*** Speaker Switching Control ***/
  2242. static int scarlett2_speaker_switch_enum_ctl_info(
  2243. struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
  2244. {
  2245. static const char *const values[3] = {
  2246. "Off", "Main", "Alt"
  2247. };
  2248. return snd_ctl_enum_info(uinfo, 1, 3, values);
  2249. }
  2250. static int scarlett2_speaker_switch_enum_ctl_get(
  2251. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  2252. {
  2253. struct usb_mixer_elem_info *elem = kctl->private_data;
  2254. struct usb_mixer_interface *mixer = elem->head.mixer;
  2255. struct scarlett2_data *private = mixer->private_data;
  2256. mutex_lock(&private->data_mutex);
  2257. if (private->monitor_other_updated)
  2258. scarlett2_update_monitor_other(mixer);
  2259. ucontrol->value.enumerated.item[0] = private->speaker_switching_switch;
  2260. mutex_unlock(&private->data_mutex);
  2261. return 0;
  2262. }
  2263. /* when speaker switching gets enabled, switch the main/alt speakers
  2264. * to HW volume and disable those controls
  2265. */
  2266. static int scarlett2_speaker_switch_enable(struct usb_mixer_interface *mixer)
  2267. {
  2268. struct snd_card *card = mixer->chip->card;
  2269. struct scarlett2_data *private = mixer->private_data;
  2270. int i, err;
  2271. for (i = 0; i < 4; i++) {
  2272. int index = line_out_remap(private, i);
  2273. /* switch the main/alt speakers to HW volume */
  2274. if (!private->vol_sw_hw_switch[index]) {
  2275. err = scarlett2_sw_hw_change(private->mixer, i, 1);
  2276. if (err < 0)
  2277. return err;
  2278. }
  2279. /* disable the line out SW/HW switch */
  2280. scarlett2_sw_hw_ctl_ro(private, i);
  2281. snd_ctl_notify(card,
  2282. SNDRV_CTL_EVENT_MASK_VALUE |
  2283. SNDRV_CTL_EVENT_MASK_INFO,
  2284. &private->sw_hw_ctls[i]->id);
  2285. }
  2286. /* when the next monitor-other notify comes in, update the mux
  2287. * configuration
  2288. */
  2289. private->speaker_switching_switched = 1;
  2290. return 0;
  2291. }
  2292. /* when speaker switching gets disabled, reenable the hw/sw controls
  2293. * and invalidate the routing
  2294. */
  2295. static void scarlett2_speaker_switch_disable(struct usb_mixer_interface *mixer)
  2296. {
  2297. struct snd_card *card = mixer->chip->card;
  2298. struct scarlett2_data *private = mixer->private_data;
  2299. int i;
  2300. /* enable the line out SW/HW switch */
  2301. for (i = 0; i < 4; i++) {
  2302. scarlett2_sw_hw_ctl_rw(private, i);
  2303. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
  2304. &private->sw_hw_ctls[i]->id);
  2305. }
  2306. /* when the next monitor-other notify comes in, update the mux
  2307. * configuration
  2308. */
  2309. private->speaker_switching_switched = 1;
  2310. }
  2311. static int scarlett2_speaker_switch_enum_ctl_put(
  2312. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  2313. {
  2314. struct usb_mixer_elem_info *elem = kctl->private_data;
  2315. struct usb_mixer_interface *mixer = elem->head.mixer;
  2316. struct scarlett2_data *private = mixer->private_data;
  2317. int oval, val, err = 0;
  2318. mutex_lock(&private->data_mutex);
  2319. oval = private->speaker_switching_switch;
  2320. val = min(ucontrol->value.enumerated.item[0], 2U);
  2321. if (oval == val)
  2322. goto unlock;
  2323. private->speaker_switching_switch = val;
  2324. /* enable/disable speaker switching */
  2325. err = scarlett2_usb_set_config(
  2326. mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
  2327. 0, !!val);
  2328. if (err < 0)
  2329. goto unlock;
  2330. /* if speaker switching is enabled, select main or alt */
  2331. err = scarlett2_usb_set_config(
  2332. mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
  2333. 0, val == 2);
  2334. if (err < 0)
  2335. goto unlock;
  2336. /* update controls if speaker switching gets enabled or disabled */
  2337. if (!oval && val)
  2338. err = scarlett2_speaker_switch_enable(mixer);
  2339. else if (oval && !val)
  2340. scarlett2_speaker_switch_disable(mixer);
  2341. if (err == 0)
  2342. err = 1;
  2343. unlock:
  2344. mutex_unlock(&private->data_mutex);
  2345. return err;
  2346. }
  2347. static const struct snd_kcontrol_new scarlett2_speaker_switch_enum_ctl = {
  2348. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2349. .name = "",
  2350. .info = scarlett2_speaker_switch_enum_ctl_info,
  2351. .get = scarlett2_speaker_switch_enum_ctl_get,
  2352. .put = scarlett2_speaker_switch_enum_ctl_put,
  2353. };
  2354. static int scarlett2_add_speaker_switch_ctl(
  2355. struct usb_mixer_interface *mixer)
  2356. {
  2357. struct scarlett2_data *private = mixer->private_data;
  2358. const struct scarlett2_device_info *info = private->info;
  2359. if (!info->has_speaker_switching)
  2360. return 0;
  2361. return scarlett2_add_new_ctl(
  2362. mixer, &scarlett2_speaker_switch_enum_ctl,
  2363. 0, 1, "Speaker Switching Playback Enum",
  2364. &private->speaker_switching_ctl);
  2365. }
  2366. /*** Talkback and Talkback Map Controls ***/
  2367. static int scarlett2_talkback_enum_ctl_info(
  2368. struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
  2369. {
  2370. static const char *const values[3] = {
  2371. "Disabled", "Off", "On"
  2372. };
  2373. return snd_ctl_enum_info(uinfo, 1, 3, values);
  2374. }
  2375. static int scarlett2_talkback_enum_ctl_get(
  2376. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  2377. {
  2378. struct usb_mixer_elem_info *elem = kctl->private_data;
  2379. struct usb_mixer_interface *mixer = elem->head.mixer;
  2380. struct scarlett2_data *private = mixer->private_data;
  2381. mutex_lock(&private->data_mutex);
  2382. if (private->monitor_other_updated)
  2383. scarlett2_update_monitor_other(mixer);
  2384. ucontrol->value.enumerated.item[0] = private->talkback_switch;
  2385. mutex_unlock(&private->data_mutex);
  2386. return 0;
  2387. }
  2388. static int scarlett2_talkback_enum_ctl_put(
  2389. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  2390. {
  2391. struct usb_mixer_elem_info *elem = kctl->private_data;
  2392. struct usb_mixer_interface *mixer = elem->head.mixer;
  2393. struct scarlett2_data *private = mixer->private_data;
  2394. int oval, val, err = 0;
  2395. mutex_lock(&private->data_mutex);
  2396. oval = private->talkback_switch;
  2397. val = min(ucontrol->value.enumerated.item[0], 2U);
  2398. if (oval == val)
  2399. goto unlock;
  2400. private->talkback_switch = val;
  2401. /* enable/disable talkback */
  2402. err = scarlett2_usb_set_config(
  2403. mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
  2404. 1, !!val);
  2405. if (err < 0)
  2406. goto unlock;
  2407. /* if talkback is enabled, select main or alt */
  2408. err = scarlett2_usb_set_config(
  2409. mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
  2410. 1, val == 2);
  2411. if (err == 0)
  2412. err = 1;
  2413. unlock:
  2414. mutex_unlock(&private->data_mutex);
  2415. return err;
  2416. }
  2417. static const struct snd_kcontrol_new scarlett2_talkback_enum_ctl = {
  2418. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2419. .name = "",
  2420. .info = scarlett2_talkback_enum_ctl_info,
  2421. .get = scarlett2_talkback_enum_ctl_get,
  2422. .put = scarlett2_talkback_enum_ctl_put,
  2423. };
  2424. static int scarlett2_talkback_map_ctl_get(
  2425. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  2426. {
  2427. struct usb_mixer_elem_info *elem = kctl->private_data;
  2428. struct usb_mixer_interface *mixer = elem->head.mixer;
  2429. struct scarlett2_data *private = mixer->private_data;
  2430. int index = elem->control;
  2431. ucontrol->value.integer.value[0] = private->talkback_map[index];
  2432. return 0;
  2433. }
  2434. static int scarlett2_talkback_map_ctl_put(
  2435. struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
  2436. {
  2437. struct usb_mixer_elem_info *elem = kctl->private_data;
  2438. struct usb_mixer_interface *mixer = elem->head.mixer;
  2439. struct scarlett2_data *private = mixer->private_data;
  2440. const int (*port_count)[SCARLETT2_PORT_DIRNS] =
  2441. private->info->port_count;
  2442. int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
  2443. int index = elem->control;
  2444. int oval, val, err = 0, i;
  2445. u16 bitmap = 0;
  2446. mutex_lock(&private->data_mutex);
  2447. oval = private->talkback_map[index];
  2448. val = !!ucontrol->value.integer.value[0];
  2449. if (oval == val)
  2450. goto unlock;
  2451. private->talkback_map[index] = val;
  2452. for (i = 0; i < num_mixes; i++)
  2453. bitmap |= private->talkback_map[i] << i;
  2454. /* Send updated bitmap to the device */
  2455. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_TALKBACK_MAP,
  2456. 0, bitmap);
  2457. if (err == 0)
  2458. err = 1;
  2459. unlock:
  2460. mutex_unlock(&private->data_mutex);
  2461. return err;
  2462. }
  2463. static const struct snd_kcontrol_new scarlett2_talkback_map_ctl = {
  2464. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2465. .name = "",
  2466. .info = snd_ctl_boolean_mono_info,
  2467. .get = scarlett2_talkback_map_ctl_get,
  2468. .put = scarlett2_talkback_map_ctl_put,
  2469. };
  2470. static int scarlett2_add_talkback_ctls(
  2471. struct usb_mixer_interface *mixer)
  2472. {
  2473. struct scarlett2_data *private = mixer->private_data;
  2474. const struct scarlett2_device_info *info = private->info;
  2475. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  2476. int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
  2477. int err, i;
  2478. char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  2479. if (!info->has_talkback)
  2480. return 0;
  2481. err = scarlett2_add_new_ctl(
  2482. mixer, &scarlett2_talkback_enum_ctl,
  2483. 0, 1, "Talkback Playback Enum",
  2484. &private->talkback_ctl);
  2485. if (err < 0)
  2486. return err;
  2487. for (i = 0; i < num_mixes; i++) {
  2488. snprintf(s, sizeof(s),
  2489. "Talkback Mix %c Playback Switch", i + 'A');
  2490. err = scarlett2_add_new_ctl(mixer, &scarlett2_talkback_map_ctl,
  2491. i, 1, s, NULL);
  2492. if (err < 0)
  2493. return err;
  2494. }
  2495. return 0;
  2496. }
  2497. /*** Dim/Mute Controls ***/
  2498. static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
  2499. struct snd_ctl_elem_value *ucontrol)
  2500. {
  2501. struct usb_mixer_elem_info *elem = kctl->private_data;
  2502. struct usb_mixer_interface *mixer = elem->head.mixer;
  2503. struct scarlett2_data *private = mixer->private_data;
  2504. mutex_lock(&private->data_mutex);
  2505. if (private->vol_updated)
  2506. scarlett2_update_volumes(mixer);
  2507. mutex_unlock(&private->data_mutex);
  2508. ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
  2509. return 0;
  2510. }
  2511. static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
  2512. struct snd_ctl_elem_value *ucontrol)
  2513. {
  2514. struct usb_mixer_elem_info *elem = kctl->private_data;
  2515. struct usb_mixer_interface *mixer = elem->head.mixer;
  2516. struct scarlett2_data *private = mixer->private_data;
  2517. const struct scarlett2_device_info *info = private->info;
  2518. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  2519. int num_line_out =
  2520. port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
  2521. int index = elem->control;
  2522. int oval, val, err = 0, i;
  2523. mutex_lock(&private->data_mutex);
  2524. oval = private->dim_mute[index];
  2525. val = !!ucontrol->value.integer.value[0];
  2526. if (oval == val)
  2527. goto unlock;
  2528. private->dim_mute[index] = val;
  2529. /* Send switch change to the device */
  2530. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
  2531. index, val);
  2532. if (err == 0)
  2533. err = 1;
  2534. if (index == SCARLETT2_BUTTON_MUTE)
  2535. for (i = 0; i < num_line_out; i++) {
  2536. int line_index = line_out_remap(private, i);
  2537. if (private->vol_sw_hw_switch[line_index]) {
  2538. private->mute_switch[line_index] = val;
  2539. snd_ctl_notify(mixer->chip->card,
  2540. SNDRV_CTL_EVENT_MASK_VALUE,
  2541. &private->mute_ctls[i]->id);
  2542. }
  2543. }
  2544. unlock:
  2545. mutex_unlock(&private->data_mutex);
  2546. return err;
  2547. }
  2548. static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
  2549. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2550. .name = "",
  2551. .info = snd_ctl_boolean_mono_info,
  2552. .get = scarlett2_dim_mute_ctl_get,
  2553. .put = scarlett2_dim_mute_ctl_put
  2554. };
  2555. /*** Create the analogue output controls ***/
  2556. static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
  2557. {
  2558. struct scarlett2_data *private = mixer->private_data;
  2559. const struct scarlett2_device_info *info = private->info;
  2560. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  2561. int num_line_out =
  2562. port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
  2563. int err, i;
  2564. char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  2565. /* Add R/O HW volume control */
  2566. if (info->line_out_hw_vol) {
  2567. snprintf(s, sizeof(s), "Master HW Playback Volume");
  2568. err = scarlett2_add_new_ctl(mixer,
  2569. &scarlett2_master_volume_ctl,
  2570. 0, 1, s, &private->master_vol_ctl);
  2571. if (err < 0)
  2572. return err;
  2573. }
  2574. /* Add volume controls */
  2575. for (i = 0; i < num_line_out; i++) {
  2576. int index = line_out_remap(private, i);
  2577. /* Fader */
  2578. if (info->line_out_descrs[i])
  2579. snprintf(s, sizeof(s),
  2580. "Line %02d (%s) Playback Volume",
  2581. i + 1, info->line_out_descrs[i]);
  2582. else
  2583. snprintf(s, sizeof(s),
  2584. "Line %02d Playback Volume",
  2585. i + 1);
  2586. err = scarlett2_add_new_ctl(mixer,
  2587. &scarlett2_line_out_volume_ctl,
  2588. i, 1, s, &private->vol_ctls[i]);
  2589. if (err < 0)
  2590. return err;
  2591. /* Mute Switch */
  2592. snprintf(s, sizeof(s),
  2593. "Line %02d Mute Playback Switch",
  2594. i + 1);
  2595. err = scarlett2_add_new_ctl(mixer,
  2596. &scarlett2_mute_ctl,
  2597. i, 1, s,
  2598. &private->mute_ctls[i]);
  2599. if (err < 0)
  2600. return err;
  2601. /* Make the fader and mute controls read-only if the
  2602. * SW/HW switch is set to HW
  2603. */
  2604. if (private->vol_sw_hw_switch[index])
  2605. scarlett2_vol_ctl_set_writable(mixer, i, 0);
  2606. /* SW/HW Switch */
  2607. if (info->line_out_hw_vol) {
  2608. snprintf(s, sizeof(s),
  2609. "Line Out %02d Volume Control Playback Enum",
  2610. i + 1);
  2611. err = scarlett2_add_new_ctl(mixer,
  2612. &scarlett2_sw_hw_enum_ctl,
  2613. i, 1, s,
  2614. &private->sw_hw_ctls[i]);
  2615. if (err < 0)
  2616. return err;
  2617. /* Make the switch read-only if the line is
  2618. * involved in speaker switching
  2619. */
  2620. if (private->speaker_switching_switch && i < 4)
  2621. scarlett2_sw_hw_ctl_ro(private, i);
  2622. }
  2623. }
  2624. /* Add dim/mute controls */
  2625. if (info->line_out_hw_vol)
  2626. for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
  2627. err = scarlett2_add_new_ctl(
  2628. mixer, &scarlett2_dim_mute_ctl,
  2629. i, 1, scarlett2_dim_mute_names[i],
  2630. &private->dim_mute_ctls[i]);
  2631. if (err < 0)
  2632. return err;
  2633. }
  2634. return 0;
  2635. }
  2636. /*** Create the analogue input controls ***/
  2637. static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
  2638. {
  2639. struct scarlett2_data *private = mixer->private_data;
  2640. const struct scarlett2_device_info *info = private->info;
  2641. int err, i;
  2642. char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  2643. const char *fmt = "Line In %d %s Capture %s";
  2644. const char *fmt2 = "Line In %d-%d %s Capture %s";
  2645. /* Add input level (line/inst) controls */
  2646. for (i = 0; i < info->level_input_count; i++) {
  2647. snprintf(s, sizeof(s), fmt, i + 1 + info->level_input_first,
  2648. "Level", "Enum");
  2649. err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
  2650. i, 1, s, &private->level_ctls[i]);
  2651. if (err < 0)
  2652. return err;
  2653. }
  2654. /* Add input pad controls */
  2655. for (i = 0; i < info->pad_input_count; i++) {
  2656. snprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
  2657. err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
  2658. i, 1, s, &private->pad_ctls[i]);
  2659. if (err < 0)
  2660. return err;
  2661. }
  2662. /* Add input air controls */
  2663. for (i = 0; i < info->air_input_count; i++) {
  2664. snprintf(s, sizeof(s), fmt, i + 1, "Air", "Switch");
  2665. err = scarlett2_add_new_ctl(mixer, &scarlett2_air_ctl,
  2666. i, 1, s, &private->air_ctls[i]);
  2667. if (err < 0)
  2668. return err;
  2669. }
  2670. /* Add input phantom controls */
  2671. if (info->inputs_per_phantom == 1) {
  2672. for (i = 0; i < info->phantom_count; i++) {
  2673. snprintf(s, sizeof(s), fmt, i + 1,
  2674. "Phantom Power", "Switch");
  2675. err = scarlett2_add_new_ctl(
  2676. mixer, &scarlett2_phantom_ctl,
  2677. i, 1, s, &private->phantom_ctls[i]);
  2678. if (err < 0)
  2679. return err;
  2680. }
  2681. } else if (info->inputs_per_phantom > 1) {
  2682. for (i = 0; i < info->phantom_count; i++) {
  2683. int from = i * info->inputs_per_phantom + 1;
  2684. int to = (i + 1) * info->inputs_per_phantom;
  2685. snprintf(s, sizeof(s), fmt2, from, to,
  2686. "Phantom Power", "Switch");
  2687. err = scarlett2_add_new_ctl(
  2688. mixer, &scarlett2_phantom_ctl,
  2689. i, 1, s, &private->phantom_ctls[i]);
  2690. if (err < 0)
  2691. return err;
  2692. }
  2693. }
  2694. if (info->phantom_count) {
  2695. err = scarlett2_add_new_ctl(
  2696. mixer, &scarlett2_phantom_persistence_ctl, 0, 1,
  2697. "Phantom Power Persistence Capture Switch", NULL);
  2698. if (err < 0)
  2699. return err;
  2700. }
  2701. return 0;
  2702. }
  2703. /*** Mixer Volume Controls ***/
  2704. static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
  2705. struct snd_ctl_elem_info *uinfo)
  2706. {
  2707. struct usb_mixer_elem_info *elem = kctl->private_data;
  2708. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2709. uinfo->count = elem->channels;
  2710. uinfo->value.integer.min = 0;
  2711. uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
  2712. uinfo->value.integer.step = 1;
  2713. return 0;
  2714. }
  2715. static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
  2716. struct snd_ctl_elem_value *ucontrol)
  2717. {
  2718. struct usb_mixer_elem_info *elem = kctl->private_data;
  2719. struct scarlett2_data *private = elem->head.mixer->private_data;
  2720. ucontrol->value.integer.value[0] = private->mix[elem->control];
  2721. return 0;
  2722. }
  2723. static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
  2724. struct snd_ctl_elem_value *ucontrol)
  2725. {
  2726. struct usb_mixer_elem_info *elem = kctl->private_data;
  2727. struct usb_mixer_interface *mixer = elem->head.mixer;
  2728. struct scarlett2_data *private = mixer->private_data;
  2729. const struct scarlett2_device_info *info = private->info;
  2730. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  2731. int oval, val, num_mixer_in, mix_num, err = 0;
  2732. int index = elem->control;
  2733. mutex_lock(&private->data_mutex);
  2734. oval = private->mix[index];
  2735. val = ucontrol->value.integer.value[0];
  2736. num_mixer_in = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
  2737. mix_num = index / num_mixer_in;
  2738. if (oval == val)
  2739. goto unlock;
  2740. private->mix[index] = val;
  2741. err = scarlett2_usb_set_mix(mixer, mix_num);
  2742. if (err == 0)
  2743. err = 1;
  2744. unlock:
  2745. mutex_unlock(&private->data_mutex);
  2746. return err;
  2747. }
  2748. static const DECLARE_TLV_DB_MINMAX(
  2749. db_scale_scarlett2_mixer,
  2750. SCARLETT2_MIXER_MIN_DB * 100,
  2751. SCARLETT2_MIXER_MAX_DB * 100
  2752. );
  2753. static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
  2754. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2755. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  2756. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  2757. .name = "",
  2758. .info = scarlett2_mixer_ctl_info,
  2759. .get = scarlett2_mixer_ctl_get,
  2760. .put = scarlett2_mixer_ctl_put,
  2761. .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
  2762. .tlv = { .p = db_scale_scarlett2_mixer }
  2763. };
  2764. static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
  2765. {
  2766. struct scarlett2_data *private = mixer->private_data;
  2767. const struct scarlett2_device_info *info = private->info;
  2768. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  2769. int err, i, j;
  2770. int index;
  2771. char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  2772. int num_inputs =
  2773. port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
  2774. int num_outputs =
  2775. port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
  2776. for (i = 0, index = 0; i < num_outputs; i++)
  2777. for (j = 0; j < num_inputs; j++, index++) {
  2778. snprintf(s, sizeof(s),
  2779. "Mix %c Input %02d Playback Volume",
  2780. 'A' + i, j + 1);
  2781. err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
  2782. index, 1, s, NULL);
  2783. if (err < 0)
  2784. return err;
  2785. }
  2786. return 0;
  2787. }
  2788. /*** Mux Source Selection Controls ***/
  2789. static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
  2790. struct snd_ctl_elem_info *uinfo)
  2791. {
  2792. struct usb_mixer_elem_info *elem = kctl->private_data;
  2793. struct scarlett2_data *private = elem->head.mixer->private_data;
  2794. const struct scarlett2_device_info *info = private->info;
  2795. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  2796. unsigned int item = uinfo->value.enumerated.item;
  2797. int items = private->num_mux_srcs;
  2798. int port_type;
  2799. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2800. uinfo->count = elem->channels;
  2801. uinfo->value.enumerated.items = items;
  2802. if (item >= items)
  2803. item = uinfo->value.enumerated.item = items - 1;
  2804. for (port_type = 0;
  2805. port_type < SCARLETT2_PORT_TYPE_COUNT;
  2806. port_type++) {
  2807. if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
  2808. const struct scarlett2_port *port =
  2809. &scarlett2_ports[port_type];
  2810. sprintf(uinfo->value.enumerated.name,
  2811. port->src_descr, item + port->src_num_offset);
  2812. return 0;
  2813. }
  2814. item -= port_count[port_type][SCARLETT2_PORT_IN];
  2815. }
  2816. return -EINVAL;
  2817. }
  2818. static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
  2819. struct snd_ctl_elem_value *ucontrol)
  2820. {
  2821. struct usb_mixer_elem_info *elem = kctl->private_data;
  2822. struct usb_mixer_interface *mixer = elem->head.mixer;
  2823. struct scarlett2_data *private = mixer->private_data;
  2824. const struct scarlett2_device_info *info = private->info;
  2825. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  2826. int line_out_count =
  2827. port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
  2828. int index = elem->control;
  2829. if (index < line_out_count)
  2830. index = line_out_remap(private, index);
  2831. mutex_lock(&private->data_mutex);
  2832. if (private->mux_updated)
  2833. scarlett2_usb_get_mux(mixer);
  2834. ucontrol->value.enumerated.item[0] = private->mux[index];
  2835. mutex_unlock(&private->data_mutex);
  2836. return 0;
  2837. }
  2838. static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
  2839. struct snd_ctl_elem_value *ucontrol)
  2840. {
  2841. struct usb_mixer_elem_info *elem = kctl->private_data;
  2842. struct usb_mixer_interface *mixer = elem->head.mixer;
  2843. struct scarlett2_data *private = mixer->private_data;
  2844. const struct scarlett2_device_info *info = private->info;
  2845. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  2846. int line_out_count =
  2847. port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
  2848. int index = elem->control;
  2849. int oval, val, err = 0;
  2850. if (index < line_out_count)
  2851. index = line_out_remap(private, index);
  2852. mutex_lock(&private->data_mutex);
  2853. oval = private->mux[index];
  2854. val = min(ucontrol->value.enumerated.item[0],
  2855. private->num_mux_srcs - 1U);
  2856. if (oval == val)
  2857. goto unlock;
  2858. private->mux[index] = val;
  2859. err = scarlett2_usb_set_mux(mixer);
  2860. if (err == 0)
  2861. err = 1;
  2862. unlock:
  2863. mutex_unlock(&private->data_mutex);
  2864. return err;
  2865. }
  2866. static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
  2867. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2868. .name = "",
  2869. .info = scarlett2_mux_src_enum_ctl_info,
  2870. .get = scarlett2_mux_src_enum_ctl_get,
  2871. .put = scarlett2_mux_src_enum_ctl_put,
  2872. };
  2873. static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
  2874. {
  2875. struct scarlett2_data *private = mixer->private_data;
  2876. const struct scarlett2_device_info *info = private->info;
  2877. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  2878. int port_type, channel, i;
  2879. for (i = 0, port_type = 0;
  2880. port_type < SCARLETT2_PORT_TYPE_COUNT;
  2881. port_type++) {
  2882. for (channel = 0;
  2883. channel < port_count[port_type][SCARLETT2_PORT_OUT];
  2884. channel++, i++) {
  2885. int err;
  2886. char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  2887. const char *const descr =
  2888. scarlett2_ports[port_type].dst_descr;
  2889. snprintf(s, sizeof(s) - 5, descr, channel + 1);
  2890. strcat(s, " Enum");
  2891. err = scarlett2_add_new_ctl(mixer,
  2892. &scarlett2_mux_src_enum_ctl,
  2893. i, 1, s,
  2894. &private->mux_ctls[i]);
  2895. if (err < 0)
  2896. return err;
  2897. }
  2898. }
  2899. return 0;
  2900. }
  2901. /*** Meter Controls ***/
  2902. static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
  2903. struct snd_ctl_elem_info *uinfo)
  2904. {
  2905. struct usb_mixer_elem_info *elem = kctl->private_data;
  2906. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2907. uinfo->count = elem->channels;
  2908. uinfo->value.integer.min = 0;
  2909. uinfo->value.integer.max = 4095;
  2910. uinfo->value.integer.step = 1;
  2911. return 0;
  2912. }
  2913. static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
  2914. struct snd_ctl_elem_value *ucontrol)
  2915. {
  2916. struct usb_mixer_elem_info *elem = kctl->private_data;
  2917. u16 meter_levels[SCARLETT2_MAX_METERS];
  2918. int i, err;
  2919. err = scarlett2_usb_get_meter_levels(elem->head.mixer, elem->channels,
  2920. meter_levels);
  2921. if (err < 0)
  2922. return err;
  2923. for (i = 0; i < elem->channels; i++)
  2924. ucontrol->value.integer.value[i] = meter_levels[i];
  2925. return 0;
  2926. }
  2927. static const struct snd_kcontrol_new scarlett2_meter_ctl = {
  2928. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2929. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  2930. .name = "",
  2931. .info = scarlett2_meter_ctl_info,
  2932. .get = scarlett2_meter_ctl_get
  2933. };
  2934. static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
  2935. {
  2936. struct scarlett2_data *private = mixer->private_data;
  2937. /* devices without a mixer also don't support reporting levels */
  2938. if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
  2939. return 0;
  2940. return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
  2941. 0, private->num_mux_dsts,
  2942. "Level Meter", NULL);
  2943. }
  2944. /*** MSD Controls ***/
  2945. static int scarlett2_msd_ctl_get(struct snd_kcontrol *kctl,
  2946. struct snd_ctl_elem_value *ucontrol)
  2947. {
  2948. struct usb_mixer_elem_info *elem = kctl->private_data;
  2949. struct scarlett2_data *private = elem->head.mixer->private_data;
  2950. ucontrol->value.integer.value[0] = private->msd_switch;
  2951. return 0;
  2952. }
  2953. static int scarlett2_msd_ctl_put(struct snd_kcontrol *kctl,
  2954. struct snd_ctl_elem_value *ucontrol)
  2955. {
  2956. struct usb_mixer_elem_info *elem = kctl->private_data;
  2957. struct usb_mixer_interface *mixer = elem->head.mixer;
  2958. struct scarlett2_data *private = mixer->private_data;
  2959. int oval, val, err = 0;
  2960. mutex_lock(&private->data_mutex);
  2961. oval = private->msd_switch;
  2962. val = !!ucontrol->value.integer.value[0];
  2963. if (oval == val)
  2964. goto unlock;
  2965. private->msd_switch = val;
  2966. /* Send switch change to the device */
  2967. err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MSD_SWITCH,
  2968. 0, val);
  2969. if (err == 0)
  2970. err = 1;
  2971. unlock:
  2972. mutex_unlock(&private->data_mutex);
  2973. return err;
  2974. }
  2975. static const struct snd_kcontrol_new scarlett2_msd_ctl = {
  2976. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2977. .name = "",
  2978. .info = snd_ctl_boolean_mono_info,
  2979. .get = scarlett2_msd_ctl_get,
  2980. .put = scarlett2_msd_ctl_put,
  2981. };
  2982. static int scarlett2_add_msd_ctl(struct usb_mixer_interface *mixer)
  2983. {
  2984. struct scarlett2_data *private = mixer->private_data;
  2985. const struct scarlett2_device_info *info = private->info;
  2986. if (!info->has_msd_mode)
  2987. return 0;
  2988. /* If MSD mode is off, hide the switch by default */
  2989. if (!private->msd_switch && !(mixer->chip->setup & SCARLETT2_MSD_ENABLE))
  2990. return 0;
  2991. /* Add MSD control */
  2992. return scarlett2_add_new_ctl(mixer, &scarlett2_msd_ctl,
  2993. 0, 1, "MSD Mode Switch", NULL);
  2994. }
  2995. /*** Standalone Control ***/
  2996. static int scarlett2_standalone_ctl_get(struct snd_kcontrol *kctl,
  2997. struct snd_ctl_elem_value *ucontrol)
  2998. {
  2999. struct usb_mixer_elem_info *elem = kctl->private_data;
  3000. struct scarlett2_data *private = elem->head.mixer->private_data;
  3001. ucontrol->value.integer.value[0] = private->standalone_switch;
  3002. return 0;
  3003. }
  3004. static int scarlett2_standalone_ctl_put(struct snd_kcontrol *kctl,
  3005. struct snd_ctl_elem_value *ucontrol)
  3006. {
  3007. struct usb_mixer_elem_info *elem = kctl->private_data;
  3008. struct usb_mixer_interface *mixer = elem->head.mixer;
  3009. struct scarlett2_data *private = mixer->private_data;
  3010. int oval, val, err = 0;
  3011. mutex_lock(&private->data_mutex);
  3012. oval = private->standalone_switch;
  3013. val = !!ucontrol->value.integer.value[0];
  3014. if (oval == val)
  3015. goto unlock;
  3016. private->standalone_switch = val;
  3017. /* Send switch change to the device */
  3018. err = scarlett2_usb_set_config(mixer,
  3019. SCARLETT2_CONFIG_STANDALONE_SWITCH,
  3020. 0, val);
  3021. if (err == 0)
  3022. err = 1;
  3023. unlock:
  3024. mutex_unlock(&private->data_mutex);
  3025. return err;
  3026. }
  3027. static const struct snd_kcontrol_new scarlett2_standalone_ctl = {
  3028. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3029. .name = "",
  3030. .info = snd_ctl_boolean_mono_info,
  3031. .get = scarlett2_standalone_ctl_get,
  3032. .put = scarlett2_standalone_ctl_put,
  3033. };
  3034. static int scarlett2_add_standalone_ctl(struct usb_mixer_interface *mixer)
  3035. {
  3036. struct scarlett2_data *private = mixer->private_data;
  3037. if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
  3038. return 0;
  3039. /* Add standalone control */
  3040. return scarlett2_add_new_ctl(mixer, &scarlett2_standalone_ctl,
  3041. 0, 1, "Standalone Switch", NULL);
  3042. }
  3043. /*** Cleanup/Suspend Callbacks ***/
  3044. static void scarlett2_private_free(struct usb_mixer_interface *mixer)
  3045. {
  3046. struct scarlett2_data *private = mixer->private_data;
  3047. cancel_delayed_work_sync(&private->work);
  3048. kfree(private);
  3049. mixer->private_data = NULL;
  3050. }
  3051. static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
  3052. {
  3053. struct scarlett2_data *private = mixer->private_data;
  3054. if (cancel_delayed_work_sync(&private->work))
  3055. scarlett2_config_save(private->mixer);
  3056. }
  3057. /*** Initialisation ***/
  3058. static void scarlett2_count_mux_io(struct scarlett2_data *private)
  3059. {
  3060. const struct scarlett2_device_info *info = private->info;
  3061. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  3062. int port_type, srcs = 0, dsts = 0;
  3063. for (port_type = 0;
  3064. port_type < SCARLETT2_PORT_TYPE_COUNT;
  3065. port_type++) {
  3066. srcs += port_count[port_type][SCARLETT2_PORT_IN];
  3067. dsts += port_count[port_type][SCARLETT2_PORT_OUT];
  3068. }
  3069. private->num_mux_srcs = srcs;
  3070. private->num_mux_dsts = dsts;
  3071. }
  3072. /* Look through the interface descriptors for the Focusrite Control
  3073. * interface (bInterfaceClass = 255 Vendor Specific Class) and set
  3074. * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
  3075. * in private
  3076. */
  3077. static int scarlett2_find_fc_interface(struct usb_device *dev,
  3078. struct scarlett2_data *private)
  3079. {
  3080. struct usb_host_config *config = dev->actconfig;
  3081. int i;
  3082. for (i = 0; i < config->desc.bNumInterfaces; i++) {
  3083. struct usb_interface *intf = config->interface[i];
  3084. struct usb_interface_descriptor *desc =
  3085. &intf->altsetting[0].desc;
  3086. struct usb_endpoint_descriptor *epd;
  3087. if (desc->bInterfaceClass != 255)
  3088. continue;
  3089. epd = get_endpoint(intf->altsetting, 0);
  3090. private->bInterfaceNumber = desc->bInterfaceNumber;
  3091. private->bEndpointAddress = epd->bEndpointAddress &
  3092. USB_ENDPOINT_NUMBER_MASK;
  3093. private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
  3094. private->bInterval = epd->bInterval;
  3095. return 0;
  3096. }
  3097. return -EINVAL;
  3098. }
  3099. /* Initialise private data */
  3100. static int scarlett2_init_private(struct usb_mixer_interface *mixer,
  3101. const struct scarlett2_device_info *info)
  3102. {
  3103. struct scarlett2_data *private =
  3104. kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);
  3105. if (!private)
  3106. return -ENOMEM;
  3107. mutex_init(&private->usb_mutex);
  3108. mutex_init(&private->data_mutex);
  3109. INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
  3110. mixer->private_data = private;
  3111. mixer->private_free = scarlett2_private_free;
  3112. mixer->private_suspend = scarlett2_private_suspend;
  3113. private->info = info;
  3114. scarlett2_count_mux_io(private);
  3115. private->scarlett2_seq = 0;
  3116. private->mixer = mixer;
  3117. return scarlett2_find_fc_interface(mixer->chip->dev, private);
  3118. }
  3119. /* Cargo cult proprietary initialisation sequence */
  3120. static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
  3121. {
  3122. struct usb_device *dev = mixer->chip->dev;
  3123. struct scarlett2_data *private = mixer->private_data;
  3124. u8 buf[24];
  3125. int err;
  3126. if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
  3127. return -EINVAL;
  3128. /* step 0 */
  3129. err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
  3130. SCARLETT2_USB_CMD_INIT, buf, sizeof(buf));
  3131. if (err < 0)
  3132. return err;
  3133. /* step 1 */
  3134. private->scarlett2_seq = 1;
  3135. err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
  3136. if (err < 0)
  3137. return err;
  3138. /* step 2 */
  3139. private->scarlett2_seq = 1;
  3140. return scarlett2_usb(mixer, SCARLETT2_USB_INIT_2, NULL, 0, NULL, 84);
  3141. }
  3142. /* Read configuration from the interface on start */
  3143. static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
  3144. {
  3145. struct scarlett2_data *private = mixer->private_data;
  3146. const struct scarlett2_device_info *info = private->info;
  3147. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  3148. int num_line_out =
  3149. port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
  3150. int num_mixer_out =
  3151. port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
  3152. struct scarlett2_usb_volume_status volume_status;
  3153. int err, i;
  3154. if (info->has_msd_mode) {
  3155. err = scarlett2_usb_get_config(
  3156. mixer, SCARLETT2_CONFIG_MSD_SWITCH,
  3157. 1, &private->msd_switch);
  3158. if (err < 0)
  3159. return err;
  3160. /* no other controls are created if MSD mode is on */
  3161. if (private->msd_switch)
  3162. return 0;
  3163. }
  3164. err = scarlett2_update_input_other(mixer);
  3165. if (err < 0)
  3166. return err;
  3167. err = scarlett2_update_monitor_other(mixer);
  3168. if (err < 0)
  3169. return err;
  3170. /* the rest of the configuration is for devices with a mixer */
  3171. if (info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
  3172. return 0;
  3173. err = scarlett2_usb_get_config(
  3174. mixer, SCARLETT2_CONFIG_STANDALONE_SWITCH,
  3175. 1, &private->standalone_switch);
  3176. if (err < 0)
  3177. return err;
  3178. err = scarlett2_update_sync(mixer);
  3179. if (err < 0)
  3180. return err;
  3181. err = scarlett2_usb_get_volume_status(mixer, &volume_status);
  3182. if (err < 0)
  3183. return err;
  3184. if (info->line_out_hw_vol)
  3185. for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
  3186. private->dim_mute[i] = !!volume_status.dim_mute[i];
  3187. private->master_vol = clamp(
  3188. volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
  3189. 0, SCARLETT2_VOLUME_BIAS);
  3190. for (i = 0; i < num_line_out; i++) {
  3191. int volume, mute;
  3192. private->vol_sw_hw_switch[i] =
  3193. info->line_out_hw_vol
  3194. && volume_status.sw_hw_switch[i];
  3195. volume = private->vol_sw_hw_switch[i]
  3196. ? volume_status.master_vol
  3197. : volume_status.sw_vol[i];
  3198. volume = clamp(volume + SCARLETT2_VOLUME_BIAS,
  3199. 0, SCARLETT2_VOLUME_BIAS);
  3200. private->vol[i] = volume;
  3201. mute = private->vol_sw_hw_switch[i]
  3202. ? private->dim_mute[SCARLETT2_BUTTON_MUTE]
  3203. : volume_status.mute_switch[i];
  3204. private->mute_switch[i] = mute;
  3205. }
  3206. for (i = 0; i < num_mixer_out; i++) {
  3207. err = scarlett2_usb_get_mix(mixer, i);
  3208. if (err < 0)
  3209. return err;
  3210. }
  3211. return scarlett2_usb_get_mux(mixer);
  3212. }
  3213. /* Notify on sync change */
  3214. static void scarlett2_notify_sync(
  3215. struct usb_mixer_interface *mixer)
  3216. {
  3217. struct scarlett2_data *private = mixer->private_data;
  3218. private->sync_updated = 1;
  3219. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  3220. &private->sync_ctl->id);
  3221. }
  3222. /* Notify on monitor change */
  3223. static void scarlett2_notify_monitor(
  3224. struct usb_mixer_interface *mixer)
  3225. {
  3226. struct snd_card *card = mixer->chip->card;
  3227. struct scarlett2_data *private = mixer->private_data;
  3228. const struct scarlett2_device_info *info = private->info;
  3229. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  3230. int num_line_out =
  3231. port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
  3232. int i;
  3233. /* if line_out_hw_vol is 0, there are no controls to update */
  3234. if (!info->line_out_hw_vol)
  3235. return;
  3236. private->vol_updated = 1;
  3237. snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  3238. &private->master_vol_ctl->id);
  3239. for (i = 0; i < num_line_out; i++)
  3240. if (private->vol_sw_hw_switch[line_out_remap(private, i)])
  3241. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  3242. &private->vol_ctls[i]->id);
  3243. }
  3244. /* Notify on dim/mute change */
  3245. static void scarlett2_notify_dim_mute(
  3246. struct usb_mixer_interface *mixer)
  3247. {
  3248. struct snd_card *card = mixer->chip->card;
  3249. struct scarlett2_data *private = mixer->private_data;
  3250. const struct scarlett2_device_info *info = private->info;
  3251. const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
  3252. int num_line_out =
  3253. port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
  3254. int i;
  3255. private->vol_updated = 1;
  3256. if (!info->line_out_hw_vol)
  3257. return;
  3258. for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
  3259. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  3260. &private->dim_mute_ctls[i]->id);
  3261. for (i = 0; i < num_line_out; i++)
  3262. if (private->vol_sw_hw_switch[line_out_remap(private, i)])
  3263. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  3264. &private->mute_ctls[i]->id);
  3265. }
  3266. /* Notify on "input other" change (level/pad/air) */
  3267. static void scarlett2_notify_input_other(
  3268. struct usb_mixer_interface *mixer)
  3269. {
  3270. struct snd_card *card = mixer->chip->card;
  3271. struct scarlett2_data *private = mixer->private_data;
  3272. const struct scarlett2_device_info *info = private->info;
  3273. int i;
  3274. private->input_other_updated = 1;
  3275. for (i = 0; i < info->level_input_count; i++)
  3276. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  3277. &private->level_ctls[i]->id);
  3278. for (i = 0; i < info->pad_input_count; i++)
  3279. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  3280. &private->pad_ctls[i]->id);
  3281. for (i = 0; i < info->air_input_count; i++)
  3282. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  3283. &private->air_ctls[i]->id);
  3284. for (i = 0; i < info->phantom_count; i++)
  3285. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  3286. &private->phantom_ctls[i]->id);
  3287. }
  3288. /* Notify on "monitor other" change (direct monitor, speaker
  3289. * switching, talkback)
  3290. */
  3291. static void scarlett2_notify_monitor_other(
  3292. struct usb_mixer_interface *mixer)
  3293. {
  3294. struct snd_card *card = mixer->chip->card;
  3295. struct scarlett2_data *private = mixer->private_data;
  3296. const struct scarlett2_device_info *info = private->info;
  3297. private->monitor_other_updated = 1;
  3298. if (info->direct_monitor) {
  3299. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  3300. &private->direct_monitor_ctl->id);
  3301. return;
  3302. }
  3303. if (info->has_speaker_switching)
  3304. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  3305. &private->speaker_switching_ctl->id);
  3306. if (info->has_talkback)
  3307. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  3308. &private->talkback_ctl->id);
  3309. /* if speaker switching was recently enabled or disabled,
  3310. * invalidate the dim/mute and mux enum controls
  3311. */
  3312. if (private->speaker_switching_switched) {
  3313. int i;
  3314. scarlett2_notify_dim_mute(mixer);
  3315. private->speaker_switching_switched = 0;
  3316. private->mux_updated = 1;
  3317. for (i = 0; i < private->num_mux_dsts; i++)
  3318. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  3319. &private->mux_ctls[i]->id);
  3320. }
  3321. }
  3322. /* Interrupt callback */
  3323. static void scarlett2_notify(struct urb *urb)
  3324. {
  3325. struct usb_mixer_interface *mixer = urb->context;
  3326. int len = urb->actual_length;
  3327. int ustatus = urb->status;
  3328. u32 data;
  3329. if (ustatus != 0 || len != 8)
  3330. goto requeue;
  3331. data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
  3332. if (data & SCARLETT2_USB_NOTIFY_SYNC)
  3333. scarlett2_notify_sync(mixer);
  3334. if (data & SCARLETT2_USB_NOTIFY_MONITOR)
  3335. scarlett2_notify_monitor(mixer);
  3336. if (data & SCARLETT2_USB_NOTIFY_DIM_MUTE)
  3337. scarlett2_notify_dim_mute(mixer);
  3338. if (data & SCARLETT2_USB_NOTIFY_INPUT_OTHER)
  3339. scarlett2_notify_input_other(mixer);
  3340. if (data & SCARLETT2_USB_NOTIFY_MONITOR_OTHER)
  3341. scarlett2_notify_monitor_other(mixer);
  3342. requeue:
  3343. if (ustatus != -ENOENT &&
  3344. ustatus != -ECONNRESET &&
  3345. ustatus != -ESHUTDOWN) {
  3346. urb->dev = mixer->chip->dev;
  3347. usb_submit_urb(urb, GFP_ATOMIC);
  3348. }
  3349. }
  3350. static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
  3351. {
  3352. struct usb_device *dev = mixer->chip->dev;
  3353. struct scarlett2_data *private = mixer->private_data;
  3354. unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
  3355. void *transfer_buffer;
  3356. if (mixer->urb) {
  3357. usb_audio_err(mixer->chip,
  3358. "%s: mixer urb already in use!\n", __func__);
  3359. return 0;
  3360. }
  3361. if (usb_pipe_type_check(dev, pipe))
  3362. return -EINVAL;
  3363. mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
  3364. if (!mixer->urb)
  3365. return -ENOMEM;
  3366. transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
  3367. if (!transfer_buffer)
  3368. return -ENOMEM;
  3369. usb_fill_int_urb(mixer->urb, dev, pipe,
  3370. transfer_buffer, private->wMaxPacketSize,
  3371. scarlett2_notify, mixer, private->bInterval);
  3372. return usb_submit_urb(mixer->urb, GFP_KERNEL);
  3373. }
  3374. static int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer)
  3375. {
  3376. const struct scarlett2_device_info **info = scarlett2_devices;
  3377. int err;
  3378. /* Find device in scarlett2_devices */
  3379. while (*info && (*info)->usb_id != mixer->chip->usb_id)
  3380. info++;
  3381. if (!*info)
  3382. return -EINVAL;
  3383. /* Initialise private data */
  3384. err = scarlett2_init_private(mixer, *info);
  3385. if (err < 0)
  3386. return err;
  3387. /* Send proprietary USB initialisation sequence */
  3388. err = scarlett2_usb_init(mixer);
  3389. if (err < 0)
  3390. return err;
  3391. /* Read volume levels and controls from the interface */
  3392. err = scarlett2_read_configs(mixer);
  3393. if (err < 0)
  3394. return err;
  3395. /* Create the MSD control */
  3396. err = scarlett2_add_msd_ctl(mixer);
  3397. if (err < 0)
  3398. return err;
  3399. /* If MSD mode is enabled, don't create any other controls */
  3400. if (((struct scarlett2_data *)mixer->private_data)->msd_switch)
  3401. return 0;
  3402. /* Create the analogue output controls */
  3403. err = scarlett2_add_line_out_ctls(mixer);
  3404. if (err < 0)
  3405. return err;
  3406. /* Create the analogue input controls */
  3407. err = scarlett2_add_line_in_ctls(mixer);
  3408. if (err < 0)
  3409. return err;
  3410. /* Create the input, output, and mixer mux input selections */
  3411. err = scarlett2_add_mux_enums(mixer);
  3412. if (err < 0)
  3413. return err;
  3414. /* Create the matrix mixer controls */
  3415. err = scarlett2_add_mixer_ctls(mixer);
  3416. if (err < 0)
  3417. return err;
  3418. /* Create the level meter controls */
  3419. err = scarlett2_add_meter_ctl(mixer);
  3420. if (err < 0)
  3421. return err;
  3422. /* Create the sync control */
  3423. err = scarlett2_add_sync_ctl(mixer);
  3424. if (err < 0)
  3425. return err;
  3426. /* Create the direct monitor control */
  3427. err = scarlett2_add_direct_monitor_ctl(mixer);
  3428. if (err < 0)
  3429. return err;
  3430. /* Create the speaker switching control */
  3431. err = scarlett2_add_speaker_switch_ctl(mixer);
  3432. if (err < 0)
  3433. return err;
  3434. /* Create the talkback controls */
  3435. err = scarlett2_add_talkback_ctls(mixer);
  3436. if (err < 0)
  3437. return err;
  3438. /* Create the standalone control */
  3439. err = scarlett2_add_standalone_ctl(mixer);
  3440. if (err < 0)
  3441. return err;
  3442. /* Set up the interrupt polling */
  3443. err = scarlett2_init_notify(mixer);
  3444. if (err < 0)
  3445. return err;
  3446. return 0;
  3447. }
  3448. int snd_scarlett_gen2_init(struct usb_mixer_interface *mixer)
  3449. {
  3450. struct snd_usb_audio *chip = mixer->chip;
  3451. int err;
  3452. /* only use UAC_VERSION_2 */
  3453. if (!mixer->protocol)
  3454. return 0;
  3455. if (!(chip->setup & SCARLETT2_ENABLE)) {
  3456. usb_audio_info(chip,
  3457. "Focusrite Scarlett Gen 2/3 Mixer Driver disabled; "
  3458. "use options snd_usb_audio vid=0x%04x pid=0x%04x "
  3459. "device_setup=1 to enable and report any issues "
  3460. "to [email protected]",
  3461. USB_ID_VENDOR(chip->usb_id),
  3462. USB_ID_PRODUCT(chip->usb_id));
  3463. return 0;
  3464. }
  3465. usb_audio_info(chip,
  3466. "Focusrite Scarlett Gen 2/3 Mixer Driver enabled pid=0x%04x",
  3467. USB_ID_PRODUCT(chip->usb_id));
  3468. err = snd_scarlett_gen2_controls_create(mixer);
  3469. if (err < 0)
  3470. usb_audio_err(mixer->chip,
  3471. "Error initialising Scarlett Mixer Driver: %d",
  3472. err);
  3473. return err;
  3474. }