arm64: dts: qcom: sm8550: add TRNG node
[linux-modified.git] / sound / usb / mixer_scarlett2.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *   Focusrite Scarlett 2 Protocol Driver for ALSA
4  *   (including Scarlett 2nd Gen, 3rd Gen, Clarett USB, and Clarett+
5  *   series products)
6  *
7  *   Supported models:
8  *   - 6i6/18i8/18i20 Gen 2
9  *   - Solo/2i2/4i4/8i6/18i8/18i20 Gen 3
10  *   - Clarett 2Pre/4Pre/8Pre USB
11  *   - Clarett+ 2Pre/4Pre/8Pre
12  *
13  *   Copyright (c) 2018-2023 by Geoffrey D. Bennett <g at b4.vu>
14  *   Copyright (c) 2020-2021 by Vladimir Sadovnikov <sadko4u@gmail.com>
15  *   Copyright (c) 2022 by Christian Colglazier <christian@cacolglazier.com>
16  *
17  *   Based on the Scarlett (Gen 1) Driver for ALSA:
18  *
19  *   Copyright (c) 2013 by Tobias Hoffmann
20  *   Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
21  *   Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
22  *   Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
23  *
24  *   Many codes borrowed from audio.c by
25  *     Alan Cox (alan at lxorguk.ukuu.org.uk)
26  *     Thomas Sailer (sailer at ife.ee.ethz.ch)
27  *
28  *   Code cleanup:
29  *   David Henningsson <david.henningsson at canonical.com>
30  */
31
32 /* The protocol was reverse engineered by looking at the communication
33  * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20
34  * (firmware 1083) using usbmon in July-August 2018.
35  *
36  * Scarlett 18i8 support added in April 2019.
37  *
38  * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
39  * for providing usbmon output and testing).
40  *
41  * Scarlett 4i4/8i6 Gen 3 support added in May 2020 (thanks to Laurent
42  * Debricon for donating a 4i4 and to Fredrik Unger for providing 8i6
43  * usbmon output and testing).
44  *
45  * Scarlett 18i8/18i20 Gen 3 support added in June 2020 (thanks to
46  * Darren Jaeckel, Alex Sedlack, and Clovis Lunel for providing usbmon
47  * output, protocol traces and testing).
48  *
49  * Support for loading mixer volume and mux configuration from the
50  * interface during driver initialisation added in May 2021 (thanks to
51  * Vladimir Sadovnikov for figuring out how).
52  *
53  * Support for Solo/2i2 Gen 3 added in May 2021 (thanks to Alexander
54  * Vorona for 2i2 protocol traces).
55  *
56  * Support for phantom power, direct monitoring, speaker switching,
57  * and talkback added in May-June 2021.
58  *
59  * Support for Clarett+ 8Pre added in Aug 2022 by Christian
60  * Colglazier.
61  *
62  * Support for Clarett 8Pre USB added in Sep 2023 (thanks to Philippe
63  * Perrot for confirmation).
64  *
65  * Support for Clarett+ 4Pre and 2Pre added in Sep 2023 (thanks to
66  * Gregory Rozzo for donating a 4Pre, and David Sherwood and Patrice
67  * Peterson for usbmon output).
68  *
69  * Support for Clarett 2Pre and 4Pre USB added in Oct 2023.
70  *
71  * This ALSA mixer gives access to (model-dependent):
72  *  - input, output, mixer-matrix muxes
73  *  - mixer-matrix gain stages
74  *  - gain/volume/mute controls
75  *  - level meters
76  *  - line/inst level, pad, and air controls
77  *  - phantom power, direct monitor, speaker switching, and talkback
78  *    controls
79  *  - disable/enable MSD mode
80  *  - disable/enable standalone mode
81  *
82  * <ditaa>
83  *    /--------------\    18chn            20chn     /--------------\
84  *    | Hardware  in +--+------\    /-------------+--+ ALSA PCM out |
85  *    \--------------/  |      |    |             |  \--------------/
86  *                      |      |    |    /-----\  |
87  *                      |      |    |    |     |  |
88  *                      |      v    v    v     |  |
89  *                      |   +---------------+  |  |
90  *                      |    \ Matrix  Mux /   |  |
91  *                      |     +-----+-----+    |  |
92  *                      |           |          |  |
93  *                      |           |18chn     |  |
94  *                      |           |          |  |
95  *                      |           |     10chn|  |
96  *                      |           v          |  |
97  *                      |     +------------+   |  |
98  *                      |     | Mixer      |   |  |
99  *                      |     |     Matrix |   |  |
100  *                      |     |            |   |  |
101  *                      |     | 18x10 Gain |   |  |
102  *                      |     |   stages   |   |  |
103  *                      |     +-----+------+   |  |
104  *                      |           |          |  |
105  *                      |18chn      |10chn     |  |20chn
106  *                      |           |          |  |
107  *                      |           +----------/  |
108  *                      |           |             |
109  *                      v           v             v
110  *                      ===========================
111  *               +---------------+       +--—------------+
112  *                \ Output  Mux /         \ Capture Mux /
113  *                 +---+---+---+           +-----+-----+
114  *                     |   |                     |
115  *                10chn|   |                     |18chn
116  *                     |   |                     |
117  *  /--------------\   |   |                     |   /--------------\
118  *  | S/PDIF, ADAT |<--/   |10chn                \-->| ALSA PCM in  |
119  *  | Hardware out |       |                         \--------------/
120  *  \--------------/       |
121  *                         v
122  *                  +-------------+    Software gain per channel.
123  *                  | Master Gain |<-- 18i20 only: Switch per channel
124  *                  +------+------+    to select HW or SW gain control.
125  *                         |
126  *                         |10chn
127  *  /--------------\       |
128  *  | Analogue     |<------/
129  *  | Hardware out |
130  *  \--------------/
131  * </ditaa>
132  *
133  * Gen 3 devices have a Mass Storage Device (MSD) mode where a small
134  * disk with registration and driver download information is presented
135  * to the host. To access the full functionality of the device without
136  * proprietary software, MSD mode can be disabled by:
137  * - holding down the 48V button for five seconds while powering on
138  *   the device, or
139  * - using this driver and alsamixer to change the "MSD Mode" setting
140  *   to Off and power-cycling the device
141  */
142
143 #include <linux/slab.h>
144 #include <linux/usb.h>
145 #include <linux/moduleparam.h>
146
147 #include <sound/control.h>
148 #include <sound/tlv.h>
149
150 #include "usbaudio.h"
151 #include "mixer.h"
152 #include "helper.h"
153
154 #include "mixer_scarlett2.h"
155
156 /* device_setup value to allow turning MSD mode back on */
157 #define SCARLETT2_MSD_ENABLE 0x02
158
159 /* device_setup value to disable this mixer driver */
160 #define SCARLETT2_DISABLE 0x04
161
162 /* some gui mixers can't handle negative ctl values */
163 #define SCARLETT2_VOLUME_BIAS 127
164
165 /* mixer range from -80dB to +6dB in 0.5dB steps */
166 #define SCARLETT2_MIXER_MIN_DB -80
167 #define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
168 #define SCARLETT2_MIXER_MAX_DB 6
169 #define SCARLETT2_MIXER_MAX_VALUE \
170         ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
171 #define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1)
172
173 /* map from (dB + 80) * 2 to mixer value
174  * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
175  */
176 static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = {
177         0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
178         2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
179         9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
180         23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
181         54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
182         122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
183         244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
184         487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
185         973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
186         1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
187         3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
188         5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
189         9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
190         16345
191 };
192
193 /* Maximum number of analogue outputs */
194 #define SCARLETT2_ANALOGUE_MAX 10
195
196 /* Maximum number of level and pad switches */
197 #define SCARLETT2_LEVEL_SWITCH_MAX 2
198 #define SCARLETT2_PAD_SWITCH_MAX 8
199 #define SCARLETT2_AIR_SWITCH_MAX 8
200 #define SCARLETT2_PHANTOM_SWITCH_MAX 2
201
202 /* Maximum number of inputs to the mixer */
203 #define SCARLETT2_INPUT_MIX_MAX 25
204
205 /* Maximum number of outputs from the mixer */
206 #define SCARLETT2_OUTPUT_MIX_MAX 12
207
208 /* Maximum size of the data in the USB mux assignment message:
209  * 20 inputs, 20 outputs, 25 matrix inputs, 12 spare
210  */
211 #define SCARLETT2_MUX_MAX 77
212
213 /* Maximum number of sources (sum of input port counts) */
214 #define SCARLETT2_MAX_SRCS 52
215
216 /* Maximum number of meters (sum of output port counts) */
217 #define SCARLETT2_MAX_METERS 65
218
219 /* There are different sets of configuration parameters across the
220  * devices, dependent on series and model.
221  */
222 enum {
223         SCARLETT2_CONFIG_SET_GEN_2   = 0,
224         SCARLETT2_CONFIG_SET_GEN_3A  = 1,
225         SCARLETT2_CONFIG_SET_GEN_3B  = 2,
226         SCARLETT2_CONFIG_SET_CLARETT = 3,
227         SCARLETT2_CONFIG_SET_COUNT   = 4
228 };
229
230 /* Hardware port types:
231  * - None (no input to mux)
232  * - Analogue I/O
233  * - S/PDIF I/O
234  * - ADAT I/O
235  * - Mixer I/O
236  * - PCM I/O
237  */
238 enum {
239         SCARLETT2_PORT_TYPE_NONE     = 0,
240         SCARLETT2_PORT_TYPE_ANALOGUE = 1,
241         SCARLETT2_PORT_TYPE_SPDIF    = 2,
242         SCARLETT2_PORT_TYPE_ADAT     = 3,
243         SCARLETT2_PORT_TYPE_MIX      = 4,
244         SCARLETT2_PORT_TYPE_PCM      = 5,
245         SCARLETT2_PORT_TYPE_COUNT    = 6,
246 };
247
248 /* I/O count of each port type kept in struct scarlett2_ports */
249 enum {
250         SCARLETT2_PORT_IN    = 0,
251         SCARLETT2_PORT_OUT   = 1,
252         SCARLETT2_PORT_DIRNS = 2,
253 };
254
255 /* Dim/Mute buttons on the 18i20 */
256 enum {
257         SCARLETT2_BUTTON_MUTE    = 0,
258         SCARLETT2_BUTTON_DIM     = 1,
259         SCARLETT2_DIM_MUTE_COUNT = 2,
260 };
261
262 static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
263         "Mute Playback Switch", "Dim Playback Switch"
264 };
265
266 /* Description of each hardware port type:
267  * - id: hardware ID of this port type
268  * - src_descr: printf format string for mux input selections
269  * - src_num_offset: added to channel number for the fprintf
270  * - dst_descr: printf format string for mixer controls
271  */
272 struct scarlett2_port {
273         u16 id;
274         const char * const src_descr;
275         int src_num_offset;
276         const char * const dst_descr;
277 };
278
279 static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
280         [SCARLETT2_PORT_TYPE_NONE] = {
281                 .id = 0x000,
282                 .src_descr = "Off"
283         },
284         [SCARLETT2_PORT_TYPE_ANALOGUE] = {
285                 .id = 0x080,
286                 .src_descr = "Analogue %d",
287                 .src_num_offset = 1,
288                 .dst_descr = "Analogue Output %02d Playback"
289         },
290         [SCARLETT2_PORT_TYPE_SPDIF] = {
291                 .id = 0x180,
292                 .src_descr = "S/PDIF %d",
293                 .src_num_offset = 1,
294                 .dst_descr = "S/PDIF Output %d Playback"
295         },
296         [SCARLETT2_PORT_TYPE_ADAT] = {
297                 .id = 0x200,
298                 .src_descr = "ADAT %d",
299                 .src_num_offset = 1,
300                 .dst_descr = "ADAT Output %d Playback"
301         },
302         [SCARLETT2_PORT_TYPE_MIX] = {
303                 .id = 0x300,
304                 .src_descr = "Mix %c",
305                 .src_num_offset = 'A',
306                 .dst_descr = "Mixer Input %02d Capture"
307         },
308         [SCARLETT2_PORT_TYPE_PCM] = {
309                 .id = 0x600,
310                 .src_descr = "PCM %d",
311                 .src_num_offset = 1,
312                 .dst_descr = "PCM %02d Capture"
313         },
314 };
315
316 /* Number of mux tables: one for each band of sample rates
317  * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
318  */
319 #define SCARLETT2_MUX_TABLES 3
320
321 /* Maximum number of entries in a mux table */
322 #define SCARLETT2_MAX_MUX_ENTRIES 10
323
324 /* One entry within mux_assignment defines the port type and range of
325  * ports to add to the set_mux message. The end of the list is marked
326  * with count == 0.
327  */
328 struct scarlett2_mux_entry {
329         u8 port_type;
330         u8 start;
331         u8 count;
332 };
333
334 /* Maximum number of entries in a mux table */
335 #define SCARLETT2_MAX_METER_ENTRIES 9
336
337 /* One entry within meter_assignment defines the range of mux outputs
338  * that consecutive meter entries are mapped to. The end of the list
339  * is marked with count == 0.
340  */
341 struct scarlett2_meter_entry {
342         u8 start;
343         u8 count;
344 };
345
346 struct scarlett2_device_info {
347         /* Gen 3 devices have an internal MSD mode switch that needs
348          * to be disabled in order to access the full functionality of
349          * the device.
350          */
351         u8 has_msd_mode;
352
353         /* which set of configuration parameters the device uses */
354         u8 config_set;
355
356         /* line out hw volume is sw controlled */
357         u8 line_out_hw_vol;
358
359         /* support for main/alt speaker switching */
360         u8 has_speaker_switching;
361
362         /* support for talkback microphone */
363         u8 has_talkback;
364
365         /* the number of analogue inputs with a software switchable
366          * level control that can be set to line or instrument
367          */
368         u8 level_input_count;
369
370         /* the first input with a level control (0-based) */
371         u8 level_input_first;
372
373         /* the number of analogue inputs with a software switchable
374          * 10dB pad control
375          */
376         u8 pad_input_count;
377
378         /* the number of analogue inputs with a software switchable
379          * "air" control
380          */
381         u8 air_input_count;
382
383         /* the number of phantom (48V) software switchable controls */
384         u8 phantom_count;
385
386         /* the number of inputs each phantom switch controls */
387         u8 inputs_per_phantom;
388
389         /* the number of direct monitor options
390          * (0 = none, 1 = mono only, 2 = mono/stereo)
391          */
392         u8 direct_monitor;
393
394         /* remap analogue outputs; 18i8 Gen 3 has "line 3/4" connected
395          * internally to the analogue 7/8 outputs
396          */
397         u8 line_out_remap_enable;
398         u8 line_out_remap[SCARLETT2_ANALOGUE_MAX];
399         u8 line_out_unmap[SCARLETT2_ANALOGUE_MAX];
400
401         /* additional description for the line out volume controls */
402         const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
403
404         /* number of sources/destinations of each port type */
405         const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];
406
407         /* layout/order of the entries in the set_mux message */
408         struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES]
409                                                  [SCARLETT2_MAX_MUX_ENTRIES];
410
411         /* map from meter level order returned by
412          * SCARLETT2_USB_GET_METER to index into mux[] entries (same
413          * as the order returned by scarlett2_meter_ctl_get())
414          */
415         struct scarlett2_meter_entry meter_map[SCARLETT2_MAX_METER_ENTRIES];
416 };
417
418 struct scarlett2_data {
419         struct usb_mixer_interface *mixer;
420         struct mutex usb_mutex; /* prevent sending concurrent USB requests */
421         struct mutex data_mutex; /* lock access to this data */
422         struct delayed_work work;
423         const struct scarlett2_device_info *info;
424         const char *series_name;
425         __u8 bInterfaceNumber;
426         __u8 bEndpointAddress;
427         __u16 wMaxPacketSize;
428         __u8 bInterval;
429         int num_mux_srcs;
430         int num_mux_dsts;
431         u32 firmware_version;
432         u16 scarlett2_seq;
433         u8 sync_updated;
434         u8 vol_updated;
435         u8 input_other_updated;
436         u8 monitor_other_updated;
437         u8 mux_updated;
438         u8 speaker_switching_switched;
439         u8 sync;
440         u8 master_vol;
441         u8 vol[SCARLETT2_ANALOGUE_MAX];
442         u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
443         u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
444         u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
445         u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
446         u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
447         u8 air_switch[SCARLETT2_AIR_SWITCH_MAX];
448         u8 phantom_switch[SCARLETT2_PHANTOM_SWITCH_MAX];
449         u8 phantom_persistence;
450         u8 direct_monitor_switch;
451         u8 speaker_switching_switch;
452         u8 talkback_switch;
453         u8 talkback_map[SCARLETT2_OUTPUT_MIX_MAX];
454         u8 msd_switch;
455         u8 standalone_switch;
456         u8 meter_level_map[SCARLETT2_MAX_METERS];
457         struct snd_kcontrol *sync_ctl;
458         struct snd_kcontrol *master_vol_ctl;
459         struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
460         struct snd_kcontrol *sw_hw_ctls[SCARLETT2_ANALOGUE_MAX];
461         struct snd_kcontrol *mute_ctls[SCARLETT2_ANALOGUE_MAX];
462         struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT];
463         struct snd_kcontrol *level_ctls[SCARLETT2_LEVEL_SWITCH_MAX];
464         struct snd_kcontrol *pad_ctls[SCARLETT2_PAD_SWITCH_MAX];
465         struct snd_kcontrol *air_ctls[SCARLETT2_AIR_SWITCH_MAX];
466         struct snd_kcontrol *phantom_ctls[SCARLETT2_PHANTOM_SWITCH_MAX];
467         struct snd_kcontrol *mux_ctls[SCARLETT2_MUX_MAX];
468         struct snd_kcontrol *direct_monitor_ctl;
469         struct snd_kcontrol *speaker_switching_ctl;
470         struct snd_kcontrol *talkback_ctl;
471         u8 mux[SCARLETT2_MUX_MAX];
472         u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX];
473 };
474
475 /*** Model-specific data ***/
476
477 static const struct scarlett2_device_info s6i6_gen2_info = {
478         .config_set = SCARLETT2_CONFIG_SET_GEN_2,
479         .level_input_count = 2,
480         .pad_input_count = 2,
481
482         .line_out_descrs = {
483                 "Headphones 1 L",
484                 "Headphones 1 R",
485                 "Headphones 2 L",
486                 "Headphones 2 R",
487         },
488
489         .port_count = {
490                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
491                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  4,  4 },
492                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
493                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
494                 [SCARLETT2_PORT_TYPE_PCM]      = {  6,  6 },
495         },
496
497         .mux_assignment = { {
498                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
499                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
500                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
501                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
502                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
503                 { 0,                            0,  0 },
504         }, {
505                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
506                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
507                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
508                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
509                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
510                 { 0,                            0,  0 },
511         }, {
512                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
513                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
514                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
515                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
516                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
517                 { 0,                            0,  0 },
518         } },
519
520         .meter_map = {
521                 { 24,  6 },
522                 {  0, 24 },
523                 {  0,  0 },
524         }
525 };
526
527 static const struct scarlett2_device_info s18i8_gen2_info = {
528         .config_set = SCARLETT2_CONFIG_SET_GEN_2,
529         .level_input_count = 2,
530         .pad_input_count = 4,
531
532         .line_out_descrs = {
533                 "Monitor L",
534                 "Monitor R",
535                 "Headphones 1 L",
536                 "Headphones 1 R",
537                 "Headphones 2 L",
538                 "Headphones 2 R",
539         },
540
541         .port_count = {
542                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
543                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  8,  6 },
544                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
545                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  0 },
546                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
547                 [SCARLETT2_PORT_TYPE_PCM]      = {  8, 18 },
548         },
549
550         .mux_assignment = { {
551                 { SCARLETT2_PORT_TYPE_PCM,      0, 18 },
552                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
553                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
554                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
555                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
556                 { 0,                            0,  0 },
557         }, {
558                 { SCARLETT2_PORT_TYPE_PCM,      0, 14 },
559                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
560                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
561                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
562                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
563                 { 0,                            0,  0 },
564         }, {
565                 { SCARLETT2_PORT_TYPE_PCM,      0, 10 },
566                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
567                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
568                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
569                 { SCARLETT2_PORT_TYPE_NONE,     0,  4 },
570                 { 0,                            0,  0 },
571         } },
572
573         .meter_map = {
574                 { 26, 18 },
575                 {  0, 26 },
576                 {  0,  0 },
577         }
578 };
579
580 static const struct scarlett2_device_info s18i20_gen2_info = {
581         .config_set = SCARLETT2_CONFIG_SET_GEN_2,
582         .line_out_hw_vol = 1,
583
584         .line_out_descrs = {
585                 "Monitor L",
586                 "Monitor R",
587                 NULL,
588                 NULL,
589                 NULL,
590                 NULL,
591                 "Headphones 1 L",
592                 "Headphones 1 R",
593                 "Headphones 2 L",
594                 "Headphones 2 R",
595         },
596
597         .port_count = {
598                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
599                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  8, 10 },
600                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
601                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  8 },
602                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
603                 [SCARLETT2_PORT_TYPE_PCM]      = { 20, 18 },
604         },
605
606         .mux_assignment = { {
607                 { SCARLETT2_PORT_TYPE_PCM,      0, 18 },
608                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
609                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
610                 { SCARLETT2_PORT_TYPE_ADAT,     0,  8 },
611                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
612                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
613                 { 0,                            0,  0 },
614         }, {
615                 { SCARLETT2_PORT_TYPE_PCM,      0, 14 },
616                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
617                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
618                 { SCARLETT2_PORT_TYPE_ADAT,     0,  4 },
619                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
620                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
621                 { 0,                            0,  0 },
622         }, {
623                 { SCARLETT2_PORT_TYPE_PCM,      0, 10 },
624                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
625                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
626                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
627                 { SCARLETT2_PORT_TYPE_NONE,     0,  6 },
628                 { 0,                            0,  0 },
629         } },
630
631         .meter_map = {
632                 { 38, 18 },
633                 {  0, 38 },
634                 {  0,  0 },
635         }
636 };
637
638 static const struct scarlett2_device_info solo_gen3_info = {
639         .has_msd_mode = 1,
640         .config_set = SCARLETT2_CONFIG_SET_GEN_3A,
641         .level_input_count = 1,
642         .level_input_first = 1,
643         .air_input_count = 1,
644         .phantom_count = 1,
645         .inputs_per_phantom = 1,
646         .direct_monitor = 1,
647 };
648
649 static const struct scarlett2_device_info s2i2_gen3_info = {
650         .has_msd_mode = 1,
651         .config_set = SCARLETT2_CONFIG_SET_GEN_3A,
652         .level_input_count = 2,
653         .air_input_count = 2,
654         .phantom_count = 1,
655         .inputs_per_phantom = 2,
656         .direct_monitor = 2,
657 };
658
659 static const struct scarlett2_device_info s4i4_gen3_info = {
660         .has_msd_mode = 1,
661         .config_set = SCARLETT2_CONFIG_SET_GEN_3B,
662         .level_input_count = 2,
663         .pad_input_count = 2,
664         .air_input_count = 2,
665         .phantom_count = 1,
666         .inputs_per_phantom = 2,
667
668         .line_out_descrs = {
669                 "Monitor L",
670                 "Monitor R",
671                 "Headphones L",
672                 "Headphones R",
673         },
674
675         .port_count = {
676                 [SCARLETT2_PORT_TYPE_NONE]     = { 1, 0 },
677                 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
678                 [SCARLETT2_PORT_TYPE_MIX]      = { 6, 8 },
679                 [SCARLETT2_PORT_TYPE_PCM]      = { 4, 6 },
680         },
681
682         .mux_assignment = { {
683                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
684                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
685                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
686                 { SCARLETT2_PORT_TYPE_NONE,     0, 16 },
687                 { 0,                            0,  0 },
688         }, {
689                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
690                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
691                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
692                 { SCARLETT2_PORT_TYPE_NONE,     0, 16 },
693                 { 0,                            0,  0 },
694         }, {
695                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
696                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
697                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
698                 { SCARLETT2_PORT_TYPE_NONE,     0, 16 },
699                 { 0,                            0,  0 },
700         } },
701
702         .meter_map = {
703                 { 12,  6 },
704                 {  0, 12 },
705                 {  0,  0 },
706         }
707 };
708
709 static const struct scarlett2_device_info s8i6_gen3_info = {
710         .has_msd_mode = 1,
711         .config_set = SCARLETT2_CONFIG_SET_GEN_3B,
712         .level_input_count = 2,
713         .pad_input_count = 2,
714         .air_input_count = 2,
715         .phantom_count = 1,
716         .inputs_per_phantom = 2,
717
718         .line_out_descrs = {
719                 "Headphones 1 L",
720                 "Headphones 1 R",
721                 "Headphones 2 L",
722                 "Headphones 2 R",
723         },
724
725         .port_count = {
726                 [SCARLETT2_PORT_TYPE_NONE]     = { 1,  0 },
727                 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6,  4 },
728                 [SCARLETT2_PORT_TYPE_SPDIF]    = { 2,  2 },
729                 [SCARLETT2_PORT_TYPE_MIX]      = { 8,  8 },
730                 [SCARLETT2_PORT_TYPE_PCM]      = { 6, 10 },
731         },
732
733         .mux_assignment = { {
734                 { SCARLETT2_PORT_TYPE_PCM,      0,  8 },
735                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
736                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
737                 { SCARLETT2_PORT_TYPE_PCM,      8,  2 },
738                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
739                 { SCARLETT2_PORT_TYPE_NONE,     0, 18 },
740                 { 0,                            0,  0 },
741         }, {
742                 { SCARLETT2_PORT_TYPE_PCM,      0,  8 },
743                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
744                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
745                 { SCARLETT2_PORT_TYPE_PCM,      8,  2 },
746                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
747                 { SCARLETT2_PORT_TYPE_NONE,     0, 18 },
748                 { 0,                            0,  0 },
749         }, {
750                 { SCARLETT2_PORT_TYPE_PCM,      0,  8 },
751                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
752                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
753                 { SCARLETT2_PORT_TYPE_PCM,      8,  2 },
754                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
755                 { SCARLETT2_PORT_TYPE_NONE,     0, 18 },
756                 { 0,                            0,  0 },
757         } },
758
759         .meter_map = {
760                 { 14, 8 },
761                 {  0, 6 },
762                 { 22, 2 },
763                 {  6, 8 },
764                 {  0, 0 },
765         }
766 };
767
768 static const struct scarlett2_device_info s18i8_gen3_info = {
769         .has_msd_mode = 1,
770         .config_set = SCARLETT2_CONFIG_SET_GEN_3B,
771         .line_out_hw_vol = 1,
772         .has_speaker_switching = 1,
773         .level_input_count = 2,
774         .pad_input_count = 4,
775         .air_input_count = 4,
776         .phantom_count = 2,
777         .inputs_per_phantom = 2,
778
779         .line_out_remap_enable = 1,
780         .line_out_remap = { 0, 1, 6, 7, 2, 3, 4, 5 },
781         .line_out_unmap = { 0, 1, 4, 5, 6, 7, 2, 3 },
782
783         .line_out_descrs = {
784                 "Monitor L",
785                 "Monitor R",
786                 "Alt Monitor L",
787                 "Alt Monitor R",
788                 "Headphones 1 L",
789                 "Headphones 1 R",
790                 "Headphones 2 L",
791                 "Headphones 2 R",
792         },
793
794         .port_count = {
795                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
796                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  8,  8 },
797                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
798                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  0 },
799                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 20 },
800                 [SCARLETT2_PORT_TYPE_PCM]      = {  8, 20 },
801         },
802
803         .mux_assignment = { {
804                 { SCARLETT2_PORT_TYPE_PCM,       0, 10 },
805                 { SCARLETT2_PORT_TYPE_PCM,      12,  8 },
806                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
807                 { SCARLETT2_PORT_TYPE_ANALOGUE,  6,  2 },
808                 { SCARLETT2_PORT_TYPE_ANALOGUE,  2,  4 },
809                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
810                 { SCARLETT2_PORT_TYPE_PCM,      10,  2 },
811                 { SCARLETT2_PORT_TYPE_MIX,       0, 20 },
812                 { SCARLETT2_PORT_TYPE_NONE,      0, 10 },
813                 { 0,                             0,  0 },
814         }, {
815                 { SCARLETT2_PORT_TYPE_PCM,       0, 10 },
816                 { SCARLETT2_PORT_TYPE_PCM,      12,  4 },
817                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
818                 { SCARLETT2_PORT_TYPE_ANALOGUE,  6,  2 },
819                 { SCARLETT2_PORT_TYPE_ANALOGUE,  2,  4 },
820                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
821                 { SCARLETT2_PORT_TYPE_PCM,      10,  2 },
822                 { SCARLETT2_PORT_TYPE_MIX,       0, 20 },
823                 { SCARLETT2_PORT_TYPE_NONE,      0, 10 },
824                 { 0,                             0,  0 },
825         }, {
826                 { SCARLETT2_PORT_TYPE_PCM,       0, 10 },
827                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
828                 { SCARLETT2_PORT_TYPE_ANALOGUE,  6,  2 },
829                 { SCARLETT2_PORT_TYPE_ANALOGUE,  2,  4 },
830                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
831                 { SCARLETT2_PORT_TYPE_MIX,       0, 20 },
832                 { SCARLETT2_PORT_TYPE_NONE,      0, 10 },
833                 { 0,                             0,  0 },
834         } },
835
836         .meter_map = {
837                 { 30, 10 },
838                 { 42,  8 },
839                 {  0,  2 },
840                 {  6,  2 },
841                 {  2,  4 },
842                 {  8,  2 },
843                 { 40,  2 },
844                 { 10, 20 },
845                 {  0,  0 }
846         }
847 };
848
849 static const struct scarlett2_device_info s18i20_gen3_info = {
850         .has_msd_mode = 1,
851         .config_set = SCARLETT2_CONFIG_SET_GEN_3B,
852         .line_out_hw_vol = 1,
853         .has_speaker_switching = 1,
854         .has_talkback = 1,
855         .level_input_count = 2,
856         .pad_input_count = 8,
857         .air_input_count = 8,
858         .phantom_count = 2,
859         .inputs_per_phantom = 4,
860
861         .line_out_descrs = {
862                 "Monitor 1 L",
863                 "Monitor 1 R",
864                 "Monitor 2 L",
865                 "Monitor 2 R",
866                 NULL,
867                 NULL,
868                 "Headphones 1 L",
869                 "Headphones 1 R",
870                 "Headphones 2 L",
871                 "Headphones 2 R",
872         },
873
874         .port_count = {
875                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
876                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  9, 10 },
877                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
878                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  8 },
879                 [SCARLETT2_PORT_TYPE_MIX]      = { 12, 25 },
880                 [SCARLETT2_PORT_TYPE_PCM]      = { 20, 20 },
881         },
882
883         .mux_assignment = { {
884                 { SCARLETT2_PORT_TYPE_PCM,       0,  8 },
885                 { SCARLETT2_PORT_TYPE_PCM,      10, 10 },
886                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0, 10 },
887                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
888                 { SCARLETT2_PORT_TYPE_ADAT,      0,  8 },
889                 { SCARLETT2_PORT_TYPE_PCM,       8,  2 },
890                 { SCARLETT2_PORT_TYPE_MIX,       0, 25 },
891                 { SCARLETT2_PORT_TYPE_NONE,      0, 12 },
892                 { 0,                             0,  0 },
893         }, {
894                 { SCARLETT2_PORT_TYPE_PCM,       0,  8 },
895                 { SCARLETT2_PORT_TYPE_PCM,      10,  8 },
896                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0, 10 },
897                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
898                 { SCARLETT2_PORT_TYPE_ADAT,      0,  8 },
899                 { SCARLETT2_PORT_TYPE_PCM,       8,  2 },
900                 { SCARLETT2_PORT_TYPE_MIX,       0, 25 },
901                 { SCARLETT2_PORT_TYPE_NONE,      0, 10 },
902                 { 0,                             0,  0 },
903         }, {
904                 { SCARLETT2_PORT_TYPE_PCM,       0, 10 },
905                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0, 10 },
906                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
907                 { SCARLETT2_PORT_TYPE_NONE,      0, 24 },
908                 { 0,                             0,  0 },
909         } },
910
911         .meter_map = {
912                 { 45,  8 },
913                 { 55, 10 },
914                 {  0, 20 },
915                 { 53,  2 },
916                 { 20, 25 },
917                 {  0,  0 },
918         }
919 };
920
921 static const struct scarlett2_device_info clarett_2pre_info = {
922         .config_set = SCARLETT2_CONFIG_SET_CLARETT,
923         .line_out_hw_vol = 1,
924         .level_input_count = 2,
925         .air_input_count = 2,
926
927         .line_out_descrs = {
928                 "Monitor L",
929                 "Monitor R",
930                 "Headphones L",
931                 "Headphones R",
932         },
933
934         .port_count = {
935                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
936                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  2,  4 },
937                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  0 },
938                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  0 },
939                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
940                 [SCARLETT2_PORT_TYPE_PCM]      = {  4, 12 },
941         },
942
943         .mux_assignment = { {
944                 { SCARLETT2_PORT_TYPE_PCM,      0, 12 },
945                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
946                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
947                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
948                 { 0,                            0,  0 },
949         }, {
950                 { SCARLETT2_PORT_TYPE_PCM,      0,  8 },
951                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
952                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
953                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
954                 { 0,                            0,  0 },
955         }, {
956                 { SCARLETT2_PORT_TYPE_PCM,      0,  2 },
957                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
958                 { SCARLETT2_PORT_TYPE_NONE,     0, 26 },
959                 { 0,                            0,  0 },
960         } },
961
962         .meter_map = {
963                 { 22, 12 },
964                 {  0, 22 },
965                 {  0,  0 }
966         }
967 };
968
969 static const struct scarlett2_device_info clarett_4pre_info = {
970         .config_set = SCARLETT2_CONFIG_SET_CLARETT,
971         .line_out_hw_vol = 1,
972         .level_input_count = 2,
973         .air_input_count = 4,
974
975         .line_out_descrs = {
976                 "Monitor L",
977                 "Monitor R",
978                 "Headphones 1 L",
979                 "Headphones 1 R",
980                 "Headphones 2 L",
981                 "Headphones 2 R",
982         },
983
984         .port_count = {
985                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
986                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  8,  6 },
987                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
988                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  0 },
989                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
990                 [SCARLETT2_PORT_TYPE_PCM]      = {  8, 18 },
991         },
992
993         .mux_assignment = { {
994                 { SCARLETT2_PORT_TYPE_PCM,      0, 18 },
995                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
996                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
997                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
998                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
999                 { 0,                            0,  0 },
1000         }, {
1001                 { SCARLETT2_PORT_TYPE_PCM,      0, 14 },
1002                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
1003                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
1004                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
1005                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
1006                 { 0,                            0,  0 },
1007         }, {
1008                 { SCARLETT2_PORT_TYPE_PCM,      0, 12 },
1009                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
1010                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
1011                 { SCARLETT2_PORT_TYPE_NONE,     0, 24 },
1012                 { 0,                            0,  0 },
1013         } },
1014
1015         .meter_map = {
1016                 { 26, 18 },
1017                 {  0, 26 },
1018                 {  0,  0 }
1019         }
1020 };
1021
1022 static const struct scarlett2_device_info clarett_8pre_info = {
1023         .config_set = SCARLETT2_CONFIG_SET_CLARETT,
1024         .line_out_hw_vol = 1,
1025         .level_input_count = 2,
1026         .air_input_count = 8,
1027
1028         .line_out_descrs = {
1029                 "Monitor L",
1030                 "Monitor R",
1031                 NULL,
1032                 NULL,
1033                 NULL,
1034                 NULL,
1035                 "Headphones 1 L",
1036                 "Headphones 1 R",
1037                 "Headphones 2 L",
1038                 "Headphones 2 R",
1039         },
1040
1041         .port_count = {
1042                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
1043                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  8, 10 },
1044                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
1045                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  8 },
1046                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
1047                 [SCARLETT2_PORT_TYPE_PCM]      = { 20, 18 },
1048         },
1049
1050         .mux_assignment = { {
1051                 { SCARLETT2_PORT_TYPE_PCM,      0, 18 },
1052                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1053                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
1054                 { SCARLETT2_PORT_TYPE_ADAT,     0,  8 },
1055                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
1056                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
1057                 { 0,                            0,  0 },
1058         }, {
1059                 { SCARLETT2_PORT_TYPE_PCM,      0, 14 },
1060                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1061                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
1062                 { SCARLETT2_PORT_TYPE_ADAT,     0,  4 },
1063                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
1064                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
1065                 { 0,                            0,  0 },
1066         }, {
1067                 { SCARLETT2_PORT_TYPE_PCM,      0, 12 },
1068                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1069                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
1070                 { SCARLETT2_PORT_TYPE_NONE,     0, 22 },
1071                 { 0,                            0,  0 },
1072         } },
1073
1074         .meter_map = {
1075                 { 38, 18 },
1076                 {  0, 38 },
1077                 {  0,  0 }
1078         }
1079 };
1080
1081 struct scarlett2_device_entry {
1082         const u32 usb_id; /* USB device identifier */
1083         const struct scarlett2_device_info *info;
1084         const char *series_name;
1085 };
1086
1087 static const struct scarlett2_device_entry scarlett2_devices[] = {
1088         /* Supported Gen 2 devices */
1089         { USB_ID(0x1235, 0x8203), &s6i6_gen2_info, "Scarlett Gen 2" },
1090         { USB_ID(0x1235, 0x8204), &s18i8_gen2_info, "Scarlett Gen 2" },
1091         { USB_ID(0x1235, 0x8201), &s18i20_gen2_info, "Scarlett Gen 2" },
1092
1093         /* Supported Gen 3 devices */
1094         { USB_ID(0x1235, 0x8211), &solo_gen3_info, "Scarlett Gen 3" },
1095         { USB_ID(0x1235, 0x8210), &s2i2_gen3_info, "Scarlett Gen 3" },
1096         { USB_ID(0x1235, 0x8212), &s4i4_gen3_info, "Scarlett Gen 3" },
1097         { USB_ID(0x1235, 0x8213), &s8i6_gen3_info, "Scarlett Gen 3" },
1098         { USB_ID(0x1235, 0x8214), &s18i8_gen3_info, "Scarlett Gen 3" },
1099         { USB_ID(0x1235, 0x8215), &s18i20_gen3_info, "Scarlett Gen 3" },
1100
1101         /* Supported Clarett USB/Clarett+ devices */
1102         { USB_ID(0x1235, 0x8206), &clarett_2pre_info, "Clarett USB" },
1103         { USB_ID(0x1235, 0x8207), &clarett_4pre_info, "Clarett USB" },
1104         { USB_ID(0x1235, 0x8208), &clarett_8pre_info, "Clarett USB" },
1105         { USB_ID(0x1235, 0x820a), &clarett_2pre_info, "Clarett+" },
1106         { USB_ID(0x1235, 0x820b), &clarett_4pre_info, "Clarett+" },
1107         { USB_ID(0x1235, 0x820c), &clarett_8pre_info, "Clarett+" },
1108
1109         /* End of list */
1110         { 0, NULL },
1111 };
1112
1113 /* get the starting port index number for a given port type/direction */
1114 static int scarlett2_get_port_start_num(
1115         const int port_count[][SCARLETT2_PORT_DIRNS],
1116         int direction, int port_type)
1117 {
1118         int i, num = 0;
1119
1120         for (i = 0; i < port_type; i++)
1121                 num += port_count[i][direction];
1122
1123         return num;
1124 }
1125
1126 /*** USB Interactions ***/
1127
1128 /* Notifications from the interface */
1129 #define SCARLETT2_USB_NOTIFY_SYNC          0x00000008
1130 #define SCARLETT2_USB_NOTIFY_DIM_MUTE      0x00200000
1131 #define SCARLETT2_USB_NOTIFY_MONITOR       0x00400000
1132 #define SCARLETT2_USB_NOTIFY_INPUT_OTHER   0x00800000
1133 #define SCARLETT2_USB_NOTIFY_MONITOR_OTHER 0x01000000
1134
1135 /* Commands for sending/receiving requests/responses */
1136 #define SCARLETT2_USB_CMD_INIT 0
1137 #define SCARLETT2_USB_CMD_REQ  2
1138 #define SCARLETT2_USB_CMD_RESP 3
1139
1140 #define SCARLETT2_USB_INIT_1    0x00000000
1141 #define SCARLETT2_USB_INIT_2    0x00000002
1142 #define SCARLETT2_USB_GET_METER 0x00001001
1143 #define SCARLETT2_USB_GET_MIX   0x00002001
1144 #define SCARLETT2_USB_SET_MIX   0x00002002
1145 #define SCARLETT2_USB_GET_MUX   0x00003001
1146 #define SCARLETT2_USB_SET_MUX   0x00003002
1147 #define SCARLETT2_USB_GET_SYNC  0x00006004
1148 #define SCARLETT2_USB_GET_DATA  0x00800000
1149 #define SCARLETT2_USB_SET_DATA  0x00800001
1150 #define SCARLETT2_USB_DATA_CMD  0x00800002
1151
1152 #define SCARLETT2_USB_CONFIG_SAVE 6
1153
1154 #define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31
1155 #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
1156
1157 /* volume status is read together (matches scarlett2_config_items[1]) */
1158 struct scarlett2_usb_volume_status {
1159         /* dim/mute buttons */
1160         u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
1161
1162         u8 pad1;
1163
1164         /* software volume setting */
1165         s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
1166
1167         /* actual volume of output inc. dim (-18dB) */
1168         s16 hw_vol[SCARLETT2_ANALOGUE_MAX];
1169
1170         /* internal mute buttons */
1171         u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
1172
1173         /* sw (0) or hw (1) controlled */
1174         u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
1175
1176         u8 pad3[6];
1177
1178         /* front panel volume knob */
1179         s16 master_vol;
1180 } __packed;
1181
1182 /* Configuration parameters that can be read and written */
1183 enum {
1184         SCARLETT2_CONFIG_DIM_MUTE = 0,
1185         SCARLETT2_CONFIG_LINE_OUT_VOLUME = 1,
1186         SCARLETT2_CONFIG_MUTE_SWITCH = 2,
1187         SCARLETT2_CONFIG_SW_HW_SWITCH = 3,
1188         SCARLETT2_CONFIG_LEVEL_SWITCH = 4,
1189         SCARLETT2_CONFIG_PAD_SWITCH = 5,
1190         SCARLETT2_CONFIG_MSD_SWITCH = 6,
1191         SCARLETT2_CONFIG_AIR_SWITCH = 7,
1192         SCARLETT2_CONFIG_STANDALONE_SWITCH = 8,
1193         SCARLETT2_CONFIG_PHANTOM_SWITCH = 9,
1194         SCARLETT2_CONFIG_PHANTOM_PERSISTENCE = 10,
1195         SCARLETT2_CONFIG_DIRECT_MONITOR = 11,
1196         SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH = 12,
1197         SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE = 13,
1198         SCARLETT2_CONFIG_TALKBACK_MAP = 14,
1199         SCARLETT2_CONFIG_COUNT = 15
1200 };
1201
1202 /* Location, size, and activation command number for the configuration
1203  * parameters. Size is in bits and may be 1, 8, or 16.
1204  */
1205 struct scarlett2_config {
1206         u8 offset;
1207         u8 size;
1208         u8 activate;
1209 };
1210
1211 static const struct scarlett2_config
1212         scarlett2_config_items[SCARLETT2_CONFIG_SET_COUNT]
1213                               [SCARLETT2_CONFIG_COUNT] =
1214
1215 /* Gen 2 devices: 6i6, 18i8, 18i20 */
1216 { {
1217         [SCARLETT2_CONFIG_DIM_MUTE] = {
1218                 .offset = 0x31, .size = 8, .activate = 2 },
1219
1220         [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
1221                 .offset = 0x34, .size = 16, .activate = 1 },
1222
1223         [SCARLETT2_CONFIG_MUTE_SWITCH] = {
1224                 .offset = 0x5c, .size = 8, .activate = 1 },
1225
1226         [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
1227                 .offset = 0x66, .size = 8, .activate = 3 },
1228
1229         [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
1230                 .offset = 0x7c, .size = 8, .activate = 7 },
1231
1232         [SCARLETT2_CONFIG_PAD_SWITCH] = {
1233                 .offset = 0x84, .size = 8, .activate = 8 },
1234
1235         [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
1236                 .offset = 0x8d, .size = 8, .activate = 6 },
1237
1238 /* Gen 3 devices without a mixer (Solo and 2i2) */
1239 }, {
1240         [SCARLETT2_CONFIG_MSD_SWITCH] = {
1241                 .offset = 0x04, .size = 8, .activate = 6 },
1242
1243         [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
1244                 .offset = 0x05, .size = 8, .activate = 6 },
1245
1246         [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
1247                 .offset = 0x06, .size = 8, .activate = 3 },
1248
1249         [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
1250                 .offset = 0x07, .size = 8, .activate = 4 },
1251
1252         [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
1253                 .offset = 0x08, .size = 1, .activate = 7 },
1254
1255         [SCARLETT2_CONFIG_AIR_SWITCH] = {
1256                 .offset = 0x09, .size = 1, .activate = 8 },
1257
1258 /* Gen 3 devices: 4i4, 8i6, 18i8, 18i20 */
1259 }, {
1260         [SCARLETT2_CONFIG_DIM_MUTE] = {
1261                 .offset = 0x31, .size = 8, .activate = 2 },
1262
1263         [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
1264                 .offset = 0x34, .size = 16, .activate = 1 },
1265
1266         [SCARLETT2_CONFIG_MUTE_SWITCH] = {
1267                 .offset = 0x5c, .size = 8, .activate = 1 },
1268
1269         [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
1270                 .offset = 0x66, .size = 8, .activate = 3 },
1271
1272         [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
1273                 .offset = 0x7c, .size = 8, .activate = 7 },
1274
1275         [SCARLETT2_CONFIG_PAD_SWITCH] = {
1276                 .offset = 0x84, .size = 8, .activate = 8 },
1277
1278         [SCARLETT2_CONFIG_AIR_SWITCH] = {
1279                 .offset = 0x8c, .size = 8, .activate = 8 },
1280
1281         [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
1282                 .offset = 0x95, .size = 8, .activate = 6 },
1283
1284         [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
1285                 .offset = 0x9c, .size = 1, .activate = 8 },
1286
1287         [SCARLETT2_CONFIG_MSD_SWITCH] = {
1288                 .offset = 0x9d, .size = 8, .activate = 6 },
1289
1290         [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
1291                 .offset = 0x9e, .size = 8, .activate = 6 },
1292
1293         [SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH] = {
1294                 .offset = 0x9f, .size = 1, .activate = 10 },
1295
1296         [SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE] = {
1297                 .offset = 0xa0, .size = 1, .activate = 10 },
1298
1299         [SCARLETT2_CONFIG_TALKBACK_MAP] = {
1300                 .offset = 0xb0, .size = 16, .activate = 10 },
1301
1302 /* Clarett USB and Clarett+ devices: 2Pre, 4Pre, 8Pre */
1303 }, {
1304         [SCARLETT2_CONFIG_DIM_MUTE] = {
1305                 .offset = 0x31, .size = 8, .activate = 2 },
1306
1307         [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
1308                 .offset = 0x34, .size = 16, .activate = 1 },
1309
1310         [SCARLETT2_CONFIG_MUTE_SWITCH] = {
1311                 .offset = 0x5c, .size = 8, .activate = 1 },
1312
1313         [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
1314                 .offset = 0x66, .size = 8, .activate = 3 },
1315
1316         [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
1317                 .offset = 0x7c, .size = 8, .activate = 7 },
1318
1319         [SCARLETT2_CONFIG_AIR_SWITCH] = {
1320                 .offset = 0x95, .size = 8, .activate = 8 },
1321
1322         [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
1323                 .offset = 0x8d, .size = 8, .activate = 6 },
1324 } };
1325
1326 /* proprietary request/response format */
1327 struct scarlett2_usb_packet {
1328         __le32 cmd;
1329         __le16 size;
1330         __le16 seq;
1331         __le32 error;
1332         __le32 pad;
1333         u8 data[];
1334 };
1335
1336 static void scarlett2_fill_request_header(struct scarlett2_data *private,
1337                                           struct scarlett2_usb_packet *req,
1338                                           u32 cmd, u16 req_size)
1339 {
1340         /* sequence must go up by 1 for each request */
1341         u16 seq = private->scarlett2_seq++;
1342
1343         req->cmd = cpu_to_le32(cmd);
1344         req->size = cpu_to_le16(req_size);
1345         req->seq = cpu_to_le16(seq);
1346         req->error = 0;
1347         req->pad = 0;
1348 }
1349
1350 static int scarlett2_usb_tx(struct usb_device *dev, int interface,
1351                             void *buf, u16 size)
1352 {
1353         return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1354                         SCARLETT2_USB_CMD_REQ,
1355                         USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1356                         0, interface, buf, size);
1357 }
1358
1359 static int scarlett2_usb_rx(struct usb_device *dev, int interface,
1360                             u32 usb_req, void *buf, u16 size)
1361 {
1362         return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
1363                         usb_req,
1364                         USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1365                         0, interface, buf, size);
1366 }
1367
1368 /* Send a proprietary format request to the Scarlett interface */
1369 static int scarlett2_usb(
1370         struct usb_mixer_interface *mixer, u32 cmd,
1371         void *req_data, u16 req_size, void *resp_data, u16 resp_size)
1372 {
1373         struct scarlett2_data *private = mixer->private_data;
1374         struct usb_device *dev = mixer->chip->dev;
1375         struct scarlett2_usb_packet *req, *resp = NULL;
1376         size_t req_buf_size = struct_size(req, data, req_size);
1377         size_t resp_buf_size = struct_size(resp, data, resp_size);
1378         int err;
1379
1380         req = kmalloc(req_buf_size, GFP_KERNEL);
1381         if (!req) {
1382                 err = -ENOMEM;
1383                 goto error;
1384         }
1385
1386         resp = kmalloc(resp_buf_size, GFP_KERNEL);
1387         if (!resp) {
1388                 err = -ENOMEM;
1389                 goto error;
1390         }
1391
1392         mutex_lock(&private->usb_mutex);
1393
1394         /* build request message and send it */
1395
1396         scarlett2_fill_request_header(private, req, cmd, req_size);
1397
1398         if (req_size)
1399                 memcpy(req->data, req_data, req_size);
1400
1401         err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
1402                                req, req_buf_size);
1403
1404         if (err != req_buf_size) {
1405                 usb_audio_err(
1406                         mixer->chip,
1407                         "%s USB request result cmd %x was %d\n",
1408                         private->series_name, cmd, err);
1409                 err = -EINVAL;
1410                 goto unlock;
1411         }
1412
1413         /* send a second message to get the response */
1414
1415         err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
1416                                SCARLETT2_USB_CMD_RESP,
1417                                resp, resp_buf_size);
1418
1419         /* validate the response */
1420
1421         if (err != resp_buf_size) {
1422                 usb_audio_err(
1423                         mixer->chip,
1424                         "%s USB response result cmd %x was %d expected %zu\n",
1425                         private->series_name, cmd, err, resp_buf_size);
1426                 err = -EINVAL;
1427                 goto unlock;
1428         }
1429
1430         /* cmd/seq/size should match except when initialising
1431          * seq sent = 1, response = 0
1432          */
1433         if (resp->cmd != req->cmd ||
1434             (resp->seq != req->seq &&
1435                 (le16_to_cpu(req->seq) != 1 || resp->seq != 0)) ||
1436             resp_size != le16_to_cpu(resp->size) ||
1437             resp->error ||
1438             resp->pad) {
1439                 usb_audio_err(
1440                         mixer->chip,
1441                         "%s USB invalid response; "
1442                            "cmd tx/rx %d/%d seq %d/%d size %d/%d "
1443                            "error %d pad %d\n",
1444                         private->series_name,
1445                         le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
1446                         le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
1447                         resp_size, le16_to_cpu(resp->size),
1448                         le32_to_cpu(resp->error),
1449                         le32_to_cpu(resp->pad));
1450                 err = -EINVAL;
1451                 goto unlock;
1452         }
1453
1454         if (resp_data && resp_size > 0)
1455                 memcpy(resp_data, resp->data, resp_size);
1456
1457 unlock:
1458         mutex_unlock(&private->usb_mutex);
1459 error:
1460         kfree(req);
1461         kfree(resp);
1462         return err;
1463 }
1464
1465 /* Send a USB message to get data; result placed in *buf */
1466 static int scarlett2_usb_get(
1467         struct usb_mixer_interface *mixer,
1468         int offset, void *buf, int size)
1469 {
1470         struct {
1471                 __le32 offset;
1472                 __le32 size;
1473         } __packed req;
1474
1475         req.offset = cpu_to_le32(offset);
1476         req.size = cpu_to_le32(size);
1477         return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
1478                              &req, sizeof(req), buf, size);
1479 }
1480
1481 /* Send a USB message to get configuration parameters; result placed in *buf */
1482 static int scarlett2_usb_get_config(
1483         struct usb_mixer_interface *mixer,
1484         int config_item_num, int count, void *buf)
1485 {
1486         struct scarlett2_data *private = mixer->private_data;
1487         const struct scarlett2_device_info *info = private->info;
1488         const struct scarlett2_config *config_item =
1489                 &scarlett2_config_items[info->config_set][config_item_num];
1490         int size, err, i;
1491         u8 *buf_8;
1492         u8 value;
1493
1494         /* For byte-sized parameters, retrieve directly into buf */
1495         if (config_item->size >= 8) {
1496                 size = config_item->size / 8 * count;
1497                 err = scarlett2_usb_get(mixer, config_item->offset, buf, size);
1498                 if (err < 0)
1499                         return err;
1500                 if (size == 2) {
1501                         u16 *buf_16 = buf;
1502
1503                         for (i = 0; i < count; i++, buf_16++)
1504                                 *buf_16 = le16_to_cpu(*(__le16 *)buf_16);
1505                 }
1506                 return 0;
1507         }
1508
1509         /* For bit-sized parameters, retrieve into value */
1510         err = scarlett2_usb_get(mixer, config_item->offset, &value, 1);
1511         if (err < 0)
1512                 return err;
1513
1514         /* then unpack from value into buf[] */
1515         buf_8 = buf;
1516         for (i = 0; i < 8 && i < count; i++, value >>= 1)
1517                 *buf_8++ = value & 1;
1518
1519         return 0;
1520 }
1521
1522 /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
1523 static void scarlett2_config_save(struct usb_mixer_interface *mixer)
1524 {
1525         __le32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE);
1526
1527         scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1528                       &req, sizeof(u32),
1529                       NULL, 0);
1530 }
1531
1532 /* Delayed work to save config */
1533 static void scarlett2_config_save_work(struct work_struct *work)
1534 {
1535         struct scarlett2_data *private =
1536                 container_of(work, struct scarlett2_data, work.work);
1537
1538         scarlett2_config_save(private->mixer);
1539 }
1540
1541 /* Send a USB message to set a SCARLETT2_CONFIG_* parameter */
1542 static int scarlett2_usb_set_config(
1543         struct usb_mixer_interface *mixer,
1544         int config_item_num, int index, int value)
1545 {
1546         struct scarlett2_data *private = mixer->private_data;
1547         const struct scarlett2_device_info *info = private->info;
1548         const struct scarlett2_config *config_item =
1549                &scarlett2_config_items[info->config_set][config_item_num];
1550         struct {
1551                 __le32 offset;
1552                 __le32 bytes;
1553                 __le32 value;
1554         } __packed req;
1555         __le32 req2;
1556         int offset, size;
1557         int err;
1558
1559         /* Cancel any pending NVRAM save */
1560         cancel_delayed_work_sync(&private->work);
1561
1562         /* Convert config_item->size in bits to size in bytes and
1563          * calculate offset
1564          */
1565         if (config_item->size >= 8) {
1566                 size = config_item->size / 8;
1567                 offset = config_item->offset + index * size;
1568
1569         /* If updating a bit, retrieve the old value, set/clear the
1570          * bit as needed, and update value
1571          */
1572         } else {
1573                 u8 tmp;
1574
1575                 size = 1;
1576                 offset = config_item->offset;
1577
1578                 scarlett2_usb_get(mixer, offset, &tmp, 1);
1579                 if (value)
1580                         tmp |= (1 << index);
1581                 else
1582                         tmp &= ~(1 << index);
1583
1584                 value = tmp;
1585         }
1586
1587         /* Send the configuration parameter data */
1588         req.offset = cpu_to_le32(offset);
1589         req.bytes = cpu_to_le32(size);
1590         req.value = cpu_to_le32(value);
1591         err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA,
1592                             &req, sizeof(u32) * 2 + size,
1593                             NULL, 0);
1594         if (err < 0)
1595                 return err;
1596
1597         /* Activate the change */
1598         req2 = cpu_to_le32(config_item->activate);
1599         err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1600                             &req2, sizeof(req2), NULL, 0);
1601         if (err < 0)
1602                 return err;
1603
1604         /* Schedule the change to be written to NVRAM */
1605         if (config_item->activate != SCARLETT2_USB_CONFIG_SAVE)
1606                 schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
1607
1608         return 0;
1609 }
1610
1611 /* Send a USB message to get sync status; result placed in *sync */
1612 static int scarlett2_usb_get_sync_status(
1613         struct usb_mixer_interface *mixer,
1614         u8 *sync)
1615 {
1616         __le32 data;
1617         int err;
1618
1619         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
1620                             NULL, 0, &data, sizeof(data));
1621         if (err < 0)
1622                 return err;
1623
1624         *sync = !!data;
1625         return 0;
1626 }
1627
1628 /* Send a USB message to get volume status; result placed in *buf */
1629 static int scarlett2_usb_get_volume_status(
1630         struct usb_mixer_interface *mixer,
1631         struct scarlett2_usb_volume_status *buf)
1632 {
1633         return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET,
1634                                  buf, sizeof(*buf));
1635 }
1636
1637 /* Send a USB message to get the volumes for all inputs of one mix
1638  * and put the values into private->mix[]
1639  */
1640 static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
1641                                  int mix_num)
1642 {
1643         struct scarlett2_data *private = mixer->private_data;
1644         const struct scarlett2_device_info *info = private->info;
1645
1646         int num_mixer_in =
1647                 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1648         int err, i, j, k;
1649
1650         struct {
1651                 __le16 mix_num;
1652                 __le16 count;
1653         } __packed req;
1654
1655         __le16 data[SCARLETT2_INPUT_MIX_MAX];
1656
1657         req.mix_num = cpu_to_le16(mix_num);
1658         req.count = cpu_to_le16(num_mixer_in);
1659
1660         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
1661                             &req, sizeof(req),
1662                             data, num_mixer_in * sizeof(u16));
1663         if (err < 0)
1664                 return err;
1665
1666         for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++) {
1667                 u16 mixer_value = le16_to_cpu(data[i]);
1668
1669                 for (k = 0; k < SCARLETT2_MIXER_VALUE_COUNT; k++)
1670                         if (scarlett2_mixer_values[k] >= mixer_value)
1671                                 break;
1672                 if (k == SCARLETT2_MIXER_VALUE_COUNT)
1673                         k = SCARLETT2_MIXER_MAX_VALUE;
1674                 private->mix[j] = k;
1675         }
1676
1677         return 0;
1678 }
1679
1680 /* Send a USB message to set the volumes for all inputs of one mix
1681  * (values obtained from private->mix[])
1682  */
1683 static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
1684                                  int mix_num)
1685 {
1686         struct scarlett2_data *private = mixer->private_data;
1687         const struct scarlett2_device_info *info = private->info;
1688
1689         struct {
1690                 __le16 mix_num;
1691                 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1692         } __packed req;
1693
1694         int i, j;
1695         int num_mixer_in =
1696                 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1697
1698         req.mix_num = cpu_to_le16(mix_num);
1699
1700         for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
1701                 req.data[i] = cpu_to_le16(
1702                         scarlett2_mixer_values[private->mix[j]]
1703                 );
1704
1705         return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
1706                              &req, (num_mixer_in + 1) * sizeof(u16),
1707                              NULL, 0);
1708 }
1709
1710 /* Convert a port number index (per info->port_count) to a hardware ID */
1711 static u32 scarlett2_mux_src_num_to_id(
1712         const int port_count[][SCARLETT2_PORT_DIRNS], int num)
1713 {
1714         int port_type;
1715
1716         for (port_type = 0;
1717              port_type < SCARLETT2_PORT_TYPE_COUNT;
1718              port_type++) {
1719                 if (num < port_count[port_type][SCARLETT2_PORT_IN])
1720                         return scarlett2_ports[port_type].id | num;
1721                 num -= port_count[port_type][SCARLETT2_PORT_IN];
1722         }
1723
1724         /* Oops */
1725         return 0;
1726 }
1727
1728 /* Convert a hardware ID to a port number index */
1729 static u32 scarlett2_mux_id_to_num(
1730         const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
1731 {
1732         int port_type;
1733         int port_num = 0;
1734
1735         for (port_type = 0;
1736              port_type < SCARLETT2_PORT_TYPE_COUNT;
1737              port_type++) {
1738                 int base = scarlett2_ports[port_type].id;
1739                 int count = port_count[port_type][direction];
1740
1741                 if (id >= base && id < base + count)
1742                         return port_num + id - base;
1743                 port_num += count;
1744         }
1745
1746         /* Oops */
1747         return -1;
1748 }
1749
1750 /* Convert one mux entry from the interface and load into private->mux[] */
1751 static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
1752                                        u32 mux_entry)
1753 {
1754         const struct scarlett2_device_info *info = private->info;
1755         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1756
1757         int dst_idx, src_idx;
1758
1759         dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
1760                                           mux_entry & 0xFFF);
1761         if (dst_idx < 0)
1762                 return;
1763
1764         if (dst_idx >= private->num_mux_dsts) {
1765                 usb_audio_err(private->mixer->chip,
1766                         "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
1767                         mux_entry, dst_idx, private->num_mux_dsts);
1768                 return;
1769         }
1770
1771         src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
1772                                           mux_entry >> 12);
1773         if (src_idx < 0)
1774                 return;
1775
1776         if (src_idx >= private->num_mux_srcs) {
1777                 usb_audio_err(private->mixer->chip,
1778                         "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
1779                         mux_entry, src_idx, private->num_mux_srcs);
1780                 return;
1781         }
1782
1783         private->mux[dst_idx] = src_idx;
1784 }
1785
1786 /* Update the meter level map
1787  *
1788  * The meter level data from the interface (SCARLETT2_USB_GET_METER
1789  * request) is returned in mux_assignment order, but to avoid exposing
1790  * that to userspace, scarlett2_meter_ctl_get() rearranges the data
1791  * into scarlett2_ports order using the meter_level_map[] array which
1792  * is set up by this function.
1793  *
1794  * In addition, the meter level data values returned from the
1795  * interface are invalid for destinations where:
1796  *
1797  * - the source is "Off"; therefore we set those values to zero (map
1798  *   value of 255)
1799  *
1800  * - the source is assigned to a previous (with respect to the
1801  *   mux_assignment order) destination; therefore we set those values
1802  *   to the value previously reported for that source
1803  */
1804 static void scarlett2_update_meter_level_map(struct scarlett2_data *private)
1805 {
1806         const struct scarlett2_device_info *info = private->info;
1807         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1808         int line_out_count =
1809                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
1810         const struct scarlett2_meter_entry *entry;
1811
1812         /* sources already assigned to a destination
1813          * value is 255 for None, otherwise the value of i
1814          * (index into array returned by
1815          * scarlett2_usb_get_meter_levels())
1816          */
1817         u8 seen_src[SCARLETT2_MAX_SRCS] = { 1 };
1818         u8 seen_src_value[SCARLETT2_MAX_SRCS] = { 255 };
1819
1820         /* index in meter_map[] order */
1821         int i = 0;
1822
1823         /* go through the meter_map[] entries */
1824         for (entry = info->meter_map;
1825              entry->count;
1826              entry++) {
1827
1828                 /* fill in each meter_level_map[] entry */
1829                 int j, mux_idx;
1830
1831                 for (j = 0, mux_idx = entry->start;
1832                      j < entry->count;
1833                      i++, j++, mux_idx++) {
1834
1835                         /* convert mux_idx using line_out_unmap[] */
1836                         int map_mux_idx = (
1837                             info->line_out_remap_enable &&
1838                             mux_idx < line_out_count
1839                         ) ? info->line_out_unmap[mux_idx]
1840                           : mux_idx;
1841
1842                         /* check which source is connected, and if
1843                          * that source is already connected elsewhere,
1844                          * use that existing connection's destination
1845                          * for this meter entry instead
1846                          */
1847                         int mux_src = private->mux[mux_idx];
1848
1849                         if (!seen_src[mux_src]) {
1850                                 seen_src[mux_src] = 1;
1851                                 seen_src_value[mux_src] = i;
1852                         }
1853                         private->meter_level_map[map_mux_idx] =
1854                                 seen_src_value[mux_src];
1855                 }
1856         }
1857 }
1858
1859 /* Send USB message to get mux inputs and then populate private->mux[] */
1860 static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
1861 {
1862         struct scarlett2_data *private = mixer->private_data;
1863         int count = private->num_mux_dsts;
1864         int err, i;
1865
1866         struct {
1867                 __le16 num;
1868                 __le16 count;
1869         } __packed req;
1870
1871         __le32 data[SCARLETT2_MUX_MAX];
1872
1873         private->mux_updated = 0;
1874
1875         req.num = 0;
1876         req.count = cpu_to_le16(count);
1877
1878         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
1879                             &req, sizeof(req),
1880                             data, count * sizeof(u32));
1881         if (err < 0)
1882                 return err;
1883
1884         for (i = 0; i < count; i++)
1885                 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
1886
1887         scarlett2_update_meter_level_map(private);
1888
1889         return 0;
1890 }
1891
1892 /* Send USB messages to set mux inputs */
1893 static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
1894 {
1895         struct scarlett2_data *private = mixer->private_data;
1896         const struct scarlett2_device_info *info = private->info;
1897         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1898         int table;
1899
1900         struct {
1901                 __le16 pad;
1902                 __le16 num;
1903                 __le32 data[SCARLETT2_MUX_MAX];
1904         } __packed req;
1905
1906         req.pad = 0;
1907
1908         /* set mux settings for each rate */
1909         for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
1910                 const struct scarlett2_mux_entry *entry;
1911
1912                 /* i counts over the output array */
1913                 int i = 0, err;
1914
1915                 req.num = cpu_to_le16(table);
1916
1917                 /* loop through each entry */
1918                 for (entry = info->mux_assignment[table];
1919                      entry->count;
1920                      entry++) {
1921                         int j;
1922                         int port_type = entry->port_type;
1923                         int port_idx = entry->start;
1924                         int mux_idx = scarlett2_get_port_start_num(port_count,
1925                                 SCARLETT2_PORT_OUT, port_type) + port_idx;
1926                         int dst_id = scarlett2_ports[port_type].id + port_idx;
1927
1928                         /* Empty slots */
1929                         if (!dst_id) {
1930                                 for (j = 0; j < entry->count; j++)
1931                                         req.data[i++] = 0;
1932                                 continue;
1933                         }
1934
1935                         /* Non-empty mux slots use the lower 12 bits
1936                          * for the destination and next 12 bits for
1937                          * the source
1938                          */
1939                         for (j = 0; j < entry->count; j++) {
1940                                 int src_id = scarlett2_mux_src_num_to_id(
1941                                         port_count, private->mux[mux_idx++]);
1942                                 req.data[i++] = cpu_to_le32(dst_id |
1943                                                             src_id << 12);
1944                                 dst_id++;
1945                         }
1946                 }
1947
1948                 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
1949                                     &req, (i + 1) * sizeof(u32),
1950                                     NULL, 0);
1951                 if (err < 0)
1952                         return err;
1953         }
1954
1955         scarlett2_update_meter_level_map(private);
1956
1957         return 0;
1958 }
1959
1960 /* Send USB message to get meter levels */
1961 static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
1962                                           u16 num_meters, u16 *levels)
1963 {
1964         struct {
1965                 __le16 pad;
1966                 __le16 num_meters;
1967                 __le32 magic;
1968         } __packed req;
1969         u32 resp[SCARLETT2_MAX_METERS];
1970         int i, err;
1971
1972         req.pad = 0;
1973         req.num_meters = cpu_to_le16(num_meters);
1974         req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
1975         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
1976                             &req, sizeof(req), resp, num_meters * sizeof(u32));
1977         if (err < 0)
1978                 return err;
1979
1980         /* copy, convert to u16 */
1981         for (i = 0; i < num_meters; i++)
1982                 levels[i] = resp[i];
1983
1984         return 0;
1985 }
1986
1987 /*** Control Functions ***/
1988
1989 /* helper function to create a new control */
1990 static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
1991                                  const struct snd_kcontrol_new *ncontrol,
1992                                  int index, int channels, const char *name,
1993                                  struct snd_kcontrol **kctl_return)
1994 {
1995         struct snd_kcontrol *kctl;
1996         struct usb_mixer_elem_info *elem;
1997         int err;
1998
1999         elem = kzalloc(sizeof(*elem), GFP_KERNEL);
2000         if (!elem)
2001                 return -ENOMEM;
2002
2003         /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
2004          * ignores them for resume and other operations.
2005          * Also, the head.id field is set to 0, as we don't use this field.
2006          */
2007         elem->head.mixer = mixer;
2008         elem->control = index;
2009         elem->head.id = 0;
2010         elem->channels = channels;
2011         elem->val_type = USB_MIXER_BESPOKEN;
2012
2013         kctl = snd_ctl_new1(ncontrol, elem);
2014         if (!kctl) {
2015                 kfree(elem);
2016                 return -ENOMEM;
2017         }
2018         kctl->private_free = snd_usb_mixer_elem_free;
2019
2020         strscpy(kctl->id.name, name, sizeof(kctl->id.name));
2021
2022         err = snd_usb_mixer_add_control(&elem->head, kctl);
2023         if (err < 0)
2024                 return err;
2025
2026         if (kctl_return)
2027                 *kctl_return = kctl;
2028
2029         return 0;
2030 }
2031
2032 /*** Firmware Version Control ***/
2033
2034 static int scarlett2_firmware_version_ctl_get(
2035         struct snd_kcontrol *kctl,
2036         struct snd_ctl_elem_value *ucontrol)
2037 {
2038         struct usb_mixer_elem_info *elem = kctl->private_data;
2039         struct scarlett2_data *private = elem->head.mixer->private_data;
2040
2041         ucontrol->value.integer.value[0] = private->firmware_version;
2042
2043         return 0;
2044 }
2045
2046 static int scarlett2_firmware_version_ctl_info(
2047         struct snd_kcontrol *kctl,
2048         struct snd_ctl_elem_info *uinfo)
2049 {
2050         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2051         uinfo->count = 1;
2052
2053         return 0;
2054 }
2055
2056 static const struct snd_kcontrol_new scarlett2_firmware_version_ctl = {
2057         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
2058         .access = SNDRV_CTL_ELEM_ACCESS_READ,
2059         .name = "",
2060         .info = scarlett2_firmware_version_ctl_info,
2061         .get  = scarlett2_firmware_version_ctl_get
2062 };
2063
2064 static int scarlett2_add_firmware_version_ctl(
2065         struct usb_mixer_interface *mixer)
2066 {
2067         return scarlett2_add_new_ctl(mixer, &scarlett2_firmware_version_ctl,
2068                                      0, 0, "Firmware Version", NULL);
2069 }
2070 /*** Sync Control ***/
2071
2072 /* Update sync control after receiving notification that the status
2073  * has changed
2074  */
2075 static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
2076 {
2077         struct scarlett2_data *private = mixer->private_data;
2078
2079         private->sync_updated = 0;
2080         return scarlett2_usb_get_sync_status(mixer, &private->sync);
2081 }
2082
2083 static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
2084                                    struct snd_ctl_elem_info *uinfo)
2085 {
2086         static const char *texts[2] = {
2087                 "Unlocked", "Locked"
2088         };
2089         return snd_ctl_enum_info(uinfo, 1, 2, texts);
2090 }
2091
2092 static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
2093                                   struct snd_ctl_elem_value *ucontrol)
2094 {
2095         struct usb_mixer_elem_info *elem = kctl->private_data;
2096         struct usb_mixer_interface *mixer = elem->head.mixer;
2097         struct scarlett2_data *private = mixer->private_data;
2098
2099         mutex_lock(&private->data_mutex);
2100         if (private->sync_updated)
2101                 scarlett2_update_sync(mixer);
2102         ucontrol->value.enumerated.item[0] = private->sync;
2103         mutex_unlock(&private->data_mutex);
2104
2105         return 0;
2106 }
2107
2108 static const struct snd_kcontrol_new scarlett2_sync_ctl = {
2109         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2110         .access = SNDRV_CTL_ELEM_ACCESS_READ,
2111         .name = "",
2112         .info = scarlett2_sync_ctl_info,
2113         .get  = scarlett2_sync_ctl_get
2114 };
2115
2116 static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
2117 {
2118         struct scarlett2_data *private = mixer->private_data;
2119
2120         /* devices without a mixer also don't support reporting sync status */
2121         if (private->info->config_set == SCARLETT2_CONFIG_SET_GEN_3A)
2122                 return 0;
2123
2124         return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
2125                                      0, 1, "Sync Status", &private->sync_ctl);
2126 }
2127
2128 /*** Analogue Line Out Volume Controls ***/
2129
2130 /* Update hardware volume controls after receiving notification that
2131  * they have changed
2132  */
2133 static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
2134 {
2135         struct scarlett2_data *private = mixer->private_data;
2136         const struct scarlett2_device_info *info = private->info;
2137         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2138         struct scarlett2_usb_volume_status volume_status;
2139         int num_line_out =
2140                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
2141         int err, i;
2142         int mute;
2143
2144         private->vol_updated = 0;
2145
2146         err = scarlett2_usb_get_volume_status(mixer, &volume_status);
2147         if (err < 0)
2148                 return err;
2149
2150         private->master_vol = clamp(
2151                 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
2152                 0, SCARLETT2_VOLUME_BIAS);
2153
2154         if (info->line_out_hw_vol)
2155                 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
2156                         private->dim_mute[i] = !!volume_status.dim_mute[i];
2157
2158         mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];
2159
2160         for (i = 0; i < num_line_out; i++)
2161                 if (private->vol_sw_hw_switch[i]) {
2162                         private->vol[i] = private->master_vol;
2163                         private->mute_switch[i] = mute;
2164                 }
2165
2166         return 0;
2167 }
2168
2169 static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
2170                                      struct snd_ctl_elem_info *uinfo)
2171 {
2172         struct usb_mixer_elem_info *elem = kctl->private_data;
2173
2174         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2175         uinfo->count = elem->channels;
2176         uinfo->value.integer.min = 0;
2177         uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
2178         uinfo->value.integer.step = 1;
2179         return 0;
2180 }
2181
2182 static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
2183                                            struct snd_ctl_elem_value *ucontrol)
2184 {
2185         struct usb_mixer_elem_info *elem = kctl->private_data;
2186         struct usb_mixer_interface *mixer = elem->head.mixer;
2187         struct scarlett2_data *private = mixer->private_data;
2188
2189         mutex_lock(&private->data_mutex);
2190         if (private->vol_updated)
2191                 scarlett2_update_volumes(mixer);
2192         mutex_unlock(&private->data_mutex);
2193
2194         ucontrol->value.integer.value[0] = private->master_vol;
2195         return 0;
2196 }
2197
2198 static int line_out_remap(struct scarlett2_data *private, int index)
2199 {
2200         const struct scarlett2_device_info *info = private->info;
2201         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2202         int line_out_count =
2203                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
2204
2205         if (!info->line_out_remap_enable)
2206                 return index;
2207
2208         if (index >= line_out_count)
2209                 return index;
2210
2211         return info->line_out_remap[index];
2212 }
2213
2214 static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
2215                                     struct snd_ctl_elem_value *ucontrol)
2216 {
2217         struct usb_mixer_elem_info *elem = kctl->private_data;
2218         struct usb_mixer_interface *mixer = elem->head.mixer;
2219         struct scarlett2_data *private = mixer->private_data;
2220         int index = line_out_remap(private, elem->control);
2221
2222         mutex_lock(&private->data_mutex);
2223         if (private->vol_updated)
2224                 scarlett2_update_volumes(mixer);
2225         mutex_unlock(&private->data_mutex);
2226
2227         ucontrol->value.integer.value[0] = private->vol[index];
2228         return 0;
2229 }
2230
2231 static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
2232                                     struct snd_ctl_elem_value *ucontrol)
2233 {
2234         struct usb_mixer_elem_info *elem = kctl->private_data;
2235         struct usb_mixer_interface *mixer = elem->head.mixer;
2236         struct scarlett2_data *private = mixer->private_data;
2237         int index = line_out_remap(private, elem->control);
2238         int oval, val, err = 0;
2239
2240         mutex_lock(&private->data_mutex);
2241
2242         oval = private->vol[index];
2243         val = ucontrol->value.integer.value[0];
2244
2245         if (oval == val)
2246                 goto unlock;
2247
2248         private->vol[index] = val;
2249         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
2250                                        index, val - SCARLETT2_VOLUME_BIAS);
2251         if (err == 0)
2252                 err = 1;
2253
2254 unlock:
2255         mutex_unlock(&private->data_mutex);
2256         return err;
2257 }
2258
2259 static const DECLARE_TLV_DB_MINMAX(
2260         db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0
2261 );
2262
2263 static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
2264         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2265         .access = SNDRV_CTL_ELEM_ACCESS_READ |
2266                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
2267         .name = "",
2268         .info = scarlett2_volume_ctl_info,
2269         .get  = scarlett2_master_volume_ctl_get,
2270         .private_value = 0, /* max value */
2271         .tlv = { .p = db_scale_scarlett2_gain }
2272 };
2273
2274 static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
2275         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2276         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
2277                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
2278         .name = "",
2279         .info = scarlett2_volume_ctl_info,
2280         .get  = scarlett2_volume_ctl_get,
2281         .put  = scarlett2_volume_ctl_put,
2282         .private_value = 0, /* max value */
2283         .tlv = { .p = db_scale_scarlett2_gain }
2284 };
2285
2286 /*** Mute Switch Controls ***/
2287
2288 static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
2289                                         struct snd_ctl_elem_value *ucontrol)
2290 {
2291         struct usb_mixer_elem_info *elem = kctl->private_data;
2292         struct usb_mixer_interface *mixer = elem->head.mixer;
2293         struct scarlett2_data *private = mixer->private_data;
2294         int index = line_out_remap(private, elem->control);
2295
2296         mutex_lock(&private->data_mutex);
2297         if (private->vol_updated)
2298                 scarlett2_update_volumes(mixer);
2299         mutex_unlock(&private->data_mutex);
2300
2301         ucontrol->value.integer.value[0] = private->mute_switch[index];
2302         return 0;
2303 }
2304
2305 static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
2306                                         struct snd_ctl_elem_value *ucontrol)
2307 {
2308         struct usb_mixer_elem_info *elem = kctl->private_data;
2309         struct usb_mixer_interface *mixer = elem->head.mixer;
2310         struct scarlett2_data *private = mixer->private_data;
2311         int index = line_out_remap(private, elem->control);
2312         int oval, val, err = 0;
2313
2314         mutex_lock(&private->data_mutex);
2315
2316         oval = private->mute_switch[index];
2317         val = !!ucontrol->value.integer.value[0];
2318
2319         if (oval == val)
2320                 goto unlock;
2321
2322         private->mute_switch[index] = val;
2323
2324         /* Send mute change to the device */
2325         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
2326                                        index, val);
2327         if (err == 0)
2328                 err = 1;
2329
2330 unlock:
2331         mutex_unlock(&private->data_mutex);
2332         return err;
2333 }
2334
2335 static const struct snd_kcontrol_new scarlett2_mute_ctl = {
2336         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2337         .name = "",
2338         .info = snd_ctl_boolean_mono_info,
2339         .get  = scarlett2_mute_ctl_get,
2340         .put  = scarlett2_mute_ctl_put,
2341 };
2342
2343 /*** HW/SW Volume Switch Controls ***/
2344
2345 static void scarlett2_sw_hw_ctl_ro(struct scarlett2_data *private, int index)
2346 {
2347         private->sw_hw_ctls[index]->vd[0].access &=
2348                 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2349 }
2350
2351 static void scarlett2_sw_hw_ctl_rw(struct scarlett2_data *private, int index)
2352 {
2353         private->sw_hw_ctls[index]->vd[0].access |=
2354                 SNDRV_CTL_ELEM_ACCESS_WRITE;
2355 }
2356
2357 static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
2358                                          struct snd_ctl_elem_info *uinfo)
2359 {
2360         static const char *const values[2] = {
2361                 "SW", "HW"
2362         };
2363
2364         return snd_ctl_enum_info(uinfo, 1, 2, values);
2365 }
2366
2367 static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
2368                                         struct snd_ctl_elem_value *ucontrol)
2369 {
2370         struct usb_mixer_elem_info *elem = kctl->private_data;
2371         struct scarlett2_data *private = elem->head.mixer->private_data;
2372         int index = line_out_remap(private, elem->control);
2373
2374         ucontrol->value.enumerated.item[0] = private->vol_sw_hw_switch[index];
2375         return 0;
2376 }
2377
2378 static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
2379                                            int index, int value)
2380 {
2381         struct scarlett2_data *private = mixer->private_data;
2382         struct snd_card *card = mixer->chip->card;
2383
2384         /* Set/Clear write bits */
2385         if (value) {
2386                 private->vol_ctls[index]->vd[0].access |=
2387                         SNDRV_CTL_ELEM_ACCESS_WRITE;
2388                 private->mute_ctls[index]->vd[0].access |=
2389                         SNDRV_CTL_ELEM_ACCESS_WRITE;
2390         } else {
2391                 private->vol_ctls[index]->vd[0].access &=
2392                         ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2393                 private->mute_ctls[index]->vd[0].access &=
2394                         ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2395         }
2396
2397         /* Notify of write bit and possible value change */
2398         snd_ctl_notify(card,
2399                        SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
2400                        &private->vol_ctls[index]->id);
2401         snd_ctl_notify(card,
2402                        SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
2403                        &private->mute_ctls[index]->id);
2404 }
2405
2406 static int scarlett2_sw_hw_change(struct usb_mixer_interface *mixer,
2407                                   int ctl_index, int val)
2408 {
2409         struct scarlett2_data *private = mixer->private_data;
2410         int index = line_out_remap(private, ctl_index);
2411         int err;
2412
2413         private->vol_sw_hw_switch[index] = val;
2414
2415         /* Change access mode to RO (hardware controlled volume)
2416          * or RW (software controlled volume)
2417          */
2418         scarlett2_vol_ctl_set_writable(mixer, ctl_index, !val);
2419
2420         /* Reset volume/mute to master volume/mute */
2421         private->vol[index] = private->master_vol;
2422         private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE];
2423
2424         /* Set SW volume to current HW volume */
2425         err = scarlett2_usb_set_config(
2426                 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
2427                 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
2428         if (err < 0)
2429                 return err;
2430
2431         /* Set SW mute to current HW mute */
2432         err = scarlett2_usb_set_config(
2433                 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
2434                 index, private->dim_mute[SCARLETT2_BUTTON_MUTE]);
2435         if (err < 0)
2436                 return err;
2437
2438         /* Send SW/HW switch change to the device */
2439         return scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
2440                                         index, val);
2441 }
2442
2443 static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
2444                                         struct snd_ctl_elem_value *ucontrol)
2445 {
2446         struct usb_mixer_elem_info *elem = kctl->private_data;
2447         struct usb_mixer_interface *mixer = elem->head.mixer;
2448         struct scarlett2_data *private = mixer->private_data;
2449         int ctl_index = elem->control;
2450         int index = line_out_remap(private, ctl_index);
2451         int oval, val, err = 0;
2452
2453         mutex_lock(&private->data_mutex);
2454
2455         oval = private->vol_sw_hw_switch[index];
2456         val = !!ucontrol->value.enumerated.item[0];
2457
2458         if (oval == val)
2459                 goto unlock;
2460
2461         err = scarlett2_sw_hw_change(mixer, ctl_index, val);
2462         if (err == 0)
2463                 err = 1;
2464
2465 unlock:
2466         mutex_unlock(&private->data_mutex);
2467         return err;
2468 }
2469
2470 static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
2471         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2472         .name = "",
2473         .info = scarlett2_sw_hw_enum_ctl_info,
2474         .get  = scarlett2_sw_hw_enum_ctl_get,
2475         .put  = scarlett2_sw_hw_enum_ctl_put,
2476 };
2477
2478 /*** Line Level/Instrument Level Switch Controls ***/
2479
2480 static int scarlett2_update_input_other(struct usb_mixer_interface *mixer)
2481 {
2482         struct scarlett2_data *private = mixer->private_data;
2483         const struct scarlett2_device_info *info = private->info;
2484
2485         private->input_other_updated = 0;
2486
2487         if (info->level_input_count) {
2488                 int err = scarlett2_usb_get_config(
2489                         mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2490                         info->level_input_count + info->level_input_first,
2491                         private->level_switch);
2492                 if (err < 0)
2493                         return err;
2494         }
2495
2496         if (info->pad_input_count) {
2497                 int err = scarlett2_usb_get_config(
2498                         mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2499                         info->pad_input_count, private->pad_switch);
2500                 if (err < 0)
2501                         return err;
2502         }
2503
2504         if (info->air_input_count) {
2505                 int err = scarlett2_usb_get_config(
2506                         mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2507                         info->air_input_count, private->air_switch);
2508                 if (err < 0)
2509                         return err;
2510         }
2511
2512         if (info->phantom_count) {
2513                 int err = scarlett2_usb_get_config(
2514                         mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
2515                         info->phantom_count, private->phantom_switch);
2516                 if (err < 0)
2517                         return err;
2518
2519                 err = scarlett2_usb_get_config(
2520                         mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
2521                         1, &private->phantom_persistence);
2522                 if (err < 0)
2523                         return err;
2524         }
2525
2526         return 0;
2527 }
2528
2529 static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
2530                                          struct snd_ctl_elem_info *uinfo)
2531 {
2532         static const char *const values[2] = {
2533                 "Line", "Inst"
2534         };
2535
2536         return snd_ctl_enum_info(uinfo, 1, 2, values);
2537 }
2538
2539 static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
2540                                         struct snd_ctl_elem_value *ucontrol)
2541 {
2542         struct usb_mixer_elem_info *elem = kctl->private_data;
2543         struct usb_mixer_interface *mixer = elem->head.mixer;
2544         struct scarlett2_data *private = mixer->private_data;
2545         const struct scarlett2_device_info *info = private->info;
2546
2547         int index = elem->control + info->level_input_first;
2548
2549         mutex_lock(&private->data_mutex);
2550         if (private->input_other_updated)
2551                 scarlett2_update_input_other(mixer);
2552         ucontrol->value.enumerated.item[0] = private->level_switch[index];
2553         mutex_unlock(&private->data_mutex);
2554
2555         return 0;
2556 }
2557
2558 static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
2559                                         struct snd_ctl_elem_value *ucontrol)
2560 {
2561         struct usb_mixer_elem_info *elem = kctl->private_data;
2562         struct usb_mixer_interface *mixer = elem->head.mixer;
2563         struct scarlett2_data *private = mixer->private_data;
2564         const struct scarlett2_device_info *info = private->info;
2565
2566         int index = elem->control + info->level_input_first;
2567         int oval, val, err = 0;
2568
2569         mutex_lock(&private->data_mutex);
2570
2571         oval = private->level_switch[index];
2572         val = !!ucontrol->value.enumerated.item[0];
2573
2574         if (oval == val)
2575                 goto unlock;
2576
2577         private->level_switch[index] = val;
2578
2579         /* Send switch change to the device */
2580         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2581                                        index, val);
2582         if (err == 0)
2583                 err = 1;
2584
2585 unlock:
2586         mutex_unlock(&private->data_mutex);
2587         return err;
2588 }
2589
2590 static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
2591         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2592         .name = "",
2593         .info = scarlett2_level_enum_ctl_info,
2594         .get  = scarlett2_level_enum_ctl_get,
2595         .put  = scarlett2_level_enum_ctl_put,
2596 };
2597
2598 /*** Pad Switch Controls ***/
2599
2600 static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
2601                                  struct snd_ctl_elem_value *ucontrol)
2602 {
2603         struct usb_mixer_elem_info *elem = kctl->private_data;
2604         struct usb_mixer_interface *mixer = elem->head.mixer;
2605         struct scarlett2_data *private = mixer->private_data;
2606
2607         mutex_lock(&private->data_mutex);
2608         if (private->input_other_updated)
2609                 scarlett2_update_input_other(mixer);
2610         ucontrol->value.integer.value[0] =
2611                 private->pad_switch[elem->control];
2612         mutex_unlock(&private->data_mutex);
2613
2614         return 0;
2615 }
2616
2617 static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
2618                                  struct snd_ctl_elem_value *ucontrol)
2619 {
2620         struct usb_mixer_elem_info *elem = kctl->private_data;
2621         struct usb_mixer_interface *mixer = elem->head.mixer;
2622         struct scarlett2_data *private = mixer->private_data;
2623
2624         int index = elem->control;
2625         int oval, val, err = 0;
2626
2627         mutex_lock(&private->data_mutex);
2628
2629         oval = private->pad_switch[index];
2630         val = !!ucontrol->value.integer.value[0];
2631
2632         if (oval == val)
2633                 goto unlock;
2634
2635         private->pad_switch[index] = val;
2636
2637         /* Send switch change to the device */
2638         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2639                                        index, val);
2640         if (err == 0)
2641                 err = 1;
2642
2643 unlock:
2644         mutex_unlock(&private->data_mutex);
2645         return err;
2646 }
2647
2648 static const struct snd_kcontrol_new scarlett2_pad_ctl = {
2649         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2650         .name = "",
2651         .info = snd_ctl_boolean_mono_info,
2652         .get  = scarlett2_pad_ctl_get,
2653         .put  = scarlett2_pad_ctl_put,
2654 };
2655
2656 /*** Air Switch Controls ***/
2657
2658 static int scarlett2_air_ctl_get(struct snd_kcontrol *kctl,
2659                                  struct snd_ctl_elem_value *ucontrol)
2660 {
2661         struct usb_mixer_elem_info *elem = kctl->private_data;
2662         struct usb_mixer_interface *mixer = elem->head.mixer;
2663         struct scarlett2_data *private = mixer->private_data;
2664
2665         mutex_lock(&private->data_mutex);
2666         if (private->input_other_updated)
2667                 scarlett2_update_input_other(mixer);
2668         ucontrol->value.integer.value[0] = private->air_switch[elem->control];
2669         mutex_unlock(&private->data_mutex);
2670
2671         return 0;
2672 }
2673
2674 static int scarlett2_air_ctl_put(struct snd_kcontrol *kctl,
2675                                  struct snd_ctl_elem_value *ucontrol)
2676 {
2677         struct usb_mixer_elem_info *elem = kctl->private_data;
2678         struct usb_mixer_interface *mixer = elem->head.mixer;
2679         struct scarlett2_data *private = mixer->private_data;
2680
2681         int index = elem->control;
2682         int oval, val, err = 0;
2683
2684         mutex_lock(&private->data_mutex);
2685
2686         oval = private->air_switch[index];
2687         val = !!ucontrol->value.integer.value[0];
2688
2689         if (oval == val)
2690                 goto unlock;
2691
2692         private->air_switch[index] = val;
2693
2694         /* Send switch change to the device */
2695         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2696                                        index, val);
2697         if (err == 0)
2698                 err = 1;
2699
2700 unlock:
2701         mutex_unlock(&private->data_mutex);
2702         return err;
2703 }
2704
2705 static const struct snd_kcontrol_new scarlett2_air_ctl = {
2706         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2707         .name = "",
2708         .info = snd_ctl_boolean_mono_info,
2709         .get  = scarlett2_air_ctl_get,
2710         .put  = scarlett2_air_ctl_put,
2711 };
2712
2713 /*** Phantom Switch Controls ***/
2714
2715 static int scarlett2_phantom_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 usb_mixer_interface *mixer = elem->head.mixer;
2720         struct scarlett2_data *private = mixer->private_data;
2721
2722         mutex_lock(&private->data_mutex);
2723         if (private->input_other_updated)
2724                 scarlett2_update_input_other(mixer);
2725         ucontrol->value.integer.value[0] =
2726                 private->phantom_switch[elem->control];
2727         mutex_unlock(&private->data_mutex);
2728
2729         return 0;
2730 }
2731
2732 static int scarlett2_phantom_ctl_put(struct snd_kcontrol *kctl,
2733                                      struct snd_ctl_elem_value *ucontrol)
2734 {
2735         struct usb_mixer_elem_info *elem = kctl->private_data;
2736         struct usb_mixer_interface *mixer = elem->head.mixer;
2737         struct scarlett2_data *private = mixer->private_data;
2738
2739         int index = elem->control;
2740         int oval, val, err = 0;
2741
2742         mutex_lock(&private->data_mutex);
2743
2744         oval = private->phantom_switch[index];
2745         val = !!ucontrol->value.integer.value[0];
2746
2747         if (oval == val)
2748                 goto unlock;
2749
2750         private->phantom_switch[index] = val;
2751
2752         /* Send switch change to the device */
2753         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
2754                                        index, val);
2755         if (err == 0)
2756                 err = 1;
2757
2758 unlock:
2759         mutex_unlock(&private->data_mutex);
2760         return err;
2761 }
2762
2763 static const struct snd_kcontrol_new scarlett2_phantom_ctl = {
2764         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2765         .name = "",
2766         .info = snd_ctl_boolean_mono_info,
2767         .get  = scarlett2_phantom_ctl_get,
2768         .put  = scarlett2_phantom_ctl_put,
2769 };
2770
2771 /*** Phantom Persistence Control ***/
2772
2773 static int scarlett2_phantom_persistence_ctl_get(
2774         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2775 {
2776         struct usb_mixer_elem_info *elem = kctl->private_data;
2777         struct scarlett2_data *private = elem->head.mixer->private_data;
2778
2779         ucontrol->value.integer.value[0] = private->phantom_persistence;
2780         return 0;
2781 }
2782
2783 static int scarlett2_phantom_persistence_ctl_put(
2784         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2785 {
2786         struct usb_mixer_elem_info *elem = kctl->private_data;
2787         struct usb_mixer_interface *mixer = elem->head.mixer;
2788         struct scarlett2_data *private = mixer->private_data;
2789
2790         int index = elem->control;
2791         int oval, val, err = 0;
2792
2793         mutex_lock(&private->data_mutex);
2794
2795         oval = private->phantom_persistence;
2796         val = !!ucontrol->value.integer.value[0];
2797
2798         if (oval == val)
2799                 goto unlock;
2800
2801         private->phantom_persistence = val;
2802
2803         /* Send switch change to the device */
2804         err = scarlett2_usb_set_config(
2805                 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, index, val);
2806         if (err == 0)
2807                 err = 1;
2808
2809 unlock:
2810         mutex_unlock(&private->data_mutex);
2811         return err;
2812 }
2813
2814 static const struct snd_kcontrol_new scarlett2_phantom_persistence_ctl = {
2815         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2816         .name = "",
2817         .info = snd_ctl_boolean_mono_info,
2818         .get  = scarlett2_phantom_persistence_ctl_get,
2819         .put  = scarlett2_phantom_persistence_ctl_put,
2820 };
2821
2822 /*** Direct Monitor Control ***/
2823
2824 static int scarlett2_update_monitor_other(struct usb_mixer_interface *mixer)
2825 {
2826         struct scarlett2_data *private = mixer->private_data;
2827         const struct scarlett2_device_info *info = private->info;
2828         int err;
2829
2830         /* monitor_other_enable[0] enables speaker switching
2831          * monitor_other_enable[1] enables talkback
2832          */
2833         u8 monitor_other_enable[2];
2834
2835         /* monitor_other_switch[0] activates the alternate speakers
2836          * monitor_other_switch[1] activates talkback
2837          */
2838         u8 monitor_other_switch[2];
2839
2840         private->monitor_other_updated = 0;
2841
2842         if (info->direct_monitor)
2843                 return scarlett2_usb_get_config(
2844                         mixer, SCARLETT2_CONFIG_DIRECT_MONITOR,
2845                         1, &private->direct_monitor_switch);
2846
2847         /* if it doesn't do speaker switching then it also doesn't do
2848          * talkback
2849          */
2850         if (!info->has_speaker_switching)
2851                 return 0;
2852
2853         err = scarlett2_usb_get_config(
2854                 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2855                 2, monitor_other_enable);
2856         if (err < 0)
2857                 return err;
2858
2859         err = scarlett2_usb_get_config(
2860                 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2861                 2, monitor_other_switch);
2862         if (err < 0)
2863                 return err;
2864
2865         if (!monitor_other_enable[0])
2866                 private->speaker_switching_switch = 0;
2867         else
2868                 private->speaker_switching_switch = monitor_other_switch[0] + 1;
2869
2870         if (info->has_talkback) {
2871                 const int (*port_count)[SCARLETT2_PORT_DIRNS] =
2872                         info->port_count;
2873                 int num_mixes =
2874                         port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2875                 u16 bitmap;
2876                 int i;
2877
2878                 if (!monitor_other_enable[1])
2879                         private->talkback_switch = 0;
2880                 else
2881                         private->talkback_switch = monitor_other_switch[1] + 1;
2882
2883                 err = scarlett2_usb_get_config(mixer,
2884                                                SCARLETT2_CONFIG_TALKBACK_MAP,
2885                                                1, &bitmap);
2886                 if (err < 0)
2887                         return err;
2888                 for (i = 0; i < num_mixes; i++, bitmap >>= 1)
2889                         private->talkback_map[i] = bitmap & 1;
2890         }
2891
2892         return 0;
2893 }
2894
2895 static int scarlett2_direct_monitor_ctl_get(
2896         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2897 {
2898         struct usb_mixer_elem_info *elem = kctl->private_data;
2899         struct usb_mixer_interface *mixer = elem->head.mixer;
2900         struct scarlett2_data *private = elem->head.mixer->private_data;
2901
2902         mutex_lock(&private->data_mutex);
2903         if (private->monitor_other_updated)
2904                 scarlett2_update_monitor_other(mixer);
2905         ucontrol->value.enumerated.item[0] = private->direct_monitor_switch;
2906         mutex_unlock(&private->data_mutex);
2907
2908         return 0;
2909 }
2910
2911 static int scarlett2_direct_monitor_ctl_put(
2912         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2913 {
2914         struct usb_mixer_elem_info *elem = kctl->private_data;
2915         struct usb_mixer_interface *mixer = elem->head.mixer;
2916         struct scarlett2_data *private = mixer->private_data;
2917
2918         int index = elem->control;
2919         int oval, val, err = 0;
2920
2921         mutex_lock(&private->data_mutex);
2922
2923         oval = private->direct_monitor_switch;
2924         val = min(ucontrol->value.enumerated.item[0], 2U);
2925
2926         if (oval == val)
2927                 goto unlock;
2928
2929         private->direct_monitor_switch = val;
2930
2931         /* Send switch change to the device */
2932         err = scarlett2_usb_set_config(
2933                 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, index, val);
2934         if (err == 0)
2935                 err = 1;
2936
2937 unlock:
2938         mutex_unlock(&private->data_mutex);
2939         return err;
2940 }
2941
2942 static int scarlett2_direct_monitor_stereo_enum_ctl_info(
2943         struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2944 {
2945         static const char *const values[3] = {
2946                 "Off", "Mono", "Stereo"
2947         };
2948
2949         return snd_ctl_enum_info(uinfo, 1, 3, values);
2950 }
2951
2952 /* Direct Monitor for Solo is mono-only and only needs a boolean control
2953  * Direct Monitor for 2i2 is selectable between Off/Mono/Stereo
2954  */
2955 static const struct snd_kcontrol_new scarlett2_direct_monitor_ctl[2] = {
2956         {
2957                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2958                 .name = "",
2959                 .info = snd_ctl_boolean_mono_info,
2960                 .get  = scarlett2_direct_monitor_ctl_get,
2961                 .put  = scarlett2_direct_monitor_ctl_put,
2962         },
2963         {
2964                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2965                 .name = "",
2966                 .info = scarlett2_direct_monitor_stereo_enum_ctl_info,
2967                 .get  = scarlett2_direct_monitor_ctl_get,
2968                 .put  = scarlett2_direct_monitor_ctl_put,
2969         }
2970 };
2971
2972 static int scarlett2_add_direct_monitor_ctl(struct usb_mixer_interface *mixer)
2973 {
2974         struct scarlett2_data *private = mixer->private_data;
2975         const struct scarlett2_device_info *info = private->info;
2976         const char *s;
2977
2978         if (!info->direct_monitor)
2979                 return 0;
2980
2981         s = info->direct_monitor == 1
2982               ? "Direct Monitor Playback Switch"
2983               : "Direct Monitor Playback Enum";
2984
2985         return scarlett2_add_new_ctl(
2986                 mixer, &scarlett2_direct_monitor_ctl[info->direct_monitor - 1],
2987                 0, 1, s, &private->direct_monitor_ctl);
2988 }
2989
2990 /*** Speaker Switching Control ***/
2991
2992 static int scarlett2_speaker_switch_enum_ctl_info(
2993         struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2994 {
2995         static const char *const values[3] = {
2996                 "Off", "Main", "Alt"
2997         };
2998
2999         return snd_ctl_enum_info(uinfo, 1, 3, values);
3000 }
3001
3002 static int scarlett2_speaker_switch_enum_ctl_get(
3003         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3004 {
3005         struct usb_mixer_elem_info *elem = kctl->private_data;
3006         struct usb_mixer_interface *mixer = elem->head.mixer;
3007         struct scarlett2_data *private = mixer->private_data;
3008
3009         mutex_lock(&private->data_mutex);
3010         if (private->monitor_other_updated)
3011                 scarlett2_update_monitor_other(mixer);
3012         ucontrol->value.enumerated.item[0] = private->speaker_switching_switch;
3013         mutex_unlock(&private->data_mutex);
3014
3015         return 0;
3016 }
3017
3018 /* when speaker switching gets enabled, switch the main/alt speakers
3019  * to HW volume and disable those controls
3020  */
3021 static int scarlett2_speaker_switch_enable(struct usb_mixer_interface *mixer)
3022 {
3023         struct snd_card *card = mixer->chip->card;
3024         struct scarlett2_data *private = mixer->private_data;
3025         int i, err;
3026
3027         for (i = 0; i < 4; i++) {
3028                 int index = line_out_remap(private, i);
3029
3030                 /* switch the main/alt speakers to HW volume */
3031                 if (!private->vol_sw_hw_switch[index]) {
3032                         err = scarlett2_sw_hw_change(private->mixer, i, 1);
3033                         if (err < 0)
3034                                 return err;
3035                 }
3036
3037                 /* disable the line out SW/HW switch */
3038                 scarlett2_sw_hw_ctl_ro(private, i);
3039                 snd_ctl_notify(card,
3040                                SNDRV_CTL_EVENT_MASK_VALUE |
3041                                  SNDRV_CTL_EVENT_MASK_INFO,
3042                                &private->sw_hw_ctls[i]->id);
3043         }
3044
3045         /* when the next monitor-other notify comes in, update the mux
3046          * configuration
3047          */
3048         private->speaker_switching_switched = 1;
3049
3050         return 0;
3051 }
3052
3053 /* when speaker switching gets disabled, reenable the hw/sw controls
3054  * and invalidate the routing
3055  */
3056 static void scarlett2_speaker_switch_disable(struct usb_mixer_interface *mixer)
3057 {
3058         struct snd_card *card = mixer->chip->card;
3059         struct scarlett2_data *private = mixer->private_data;
3060         int i;
3061
3062         /* enable the line out SW/HW switch */
3063         for (i = 0; i < 4; i++) {
3064                 scarlett2_sw_hw_ctl_rw(private, i);
3065                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
3066                                &private->sw_hw_ctls[i]->id);
3067         }
3068
3069         /* when the next monitor-other notify comes in, update the mux
3070          * configuration
3071          */
3072         private->speaker_switching_switched = 1;
3073 }
3074
3075 static int scarlett2_speaker_switch_enum_ctl_put(
3076         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3077 {
3078         struct usb_mixer_elem_info *elem = kctl->private_data;
3079         struct usb_mixer_interface *mixer = elem->head.mixer;
3080         struct scarlett2_data *private = mixer->private_data;
3081
3082         int oval, val, err = 0;
3083
3084         mutex_lock(&private->data_mutex);
3085
3086         oval = private->speaker_switching_switch;
3087         val = min(ucontrol->value.enumerated.item[0], 2U);
3088
3089         if (oval == val)
3090                 goto unlock;
3091
3092         private->speaker_switching_switch = val;
3093
3094         /* enable/disable speaker switching */
3095         err = scarlett2_usb_set_config(
3096                 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
3097                 0, !!val);
3098         if (err < 0)
3099                 goto unlock;
3100
3101         /* if speaker switching is enabled, select main or alt */
3102         err = scarlett2_usb_set_config(
3103                 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
3104                 0, val == 2);
3105         if (err < 0)
3106                 goto unlock;
3107
3108         /* update controls if speaker switching gets enabled or disabled */
3109         if (!oval && val)
3110                 err = scarlett2_speaker_switch_enable(mixer);
3111         else if (oval && !val)
3112                 scarlett2_speaker_switch_disable(mixer);
3113
3114         if (err == 0)
3115                 err = 1;
3116
3117 unlock:
3118         mutex_unlock(&private->data_mutex);
3119         return err;
3120 }
3121
3122 static const struct snd_kcontrol_new scarlett2_speaker_switch_enum_ctl = {
3123         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3124         .name = "",
3125         .info = scarlett2_speaker_switch_enum_ctl_info,
3126         .get  = scarlett2_speaker_switch_enum_ctl_get,
3127         .put  = scarlett2_speaker_switch_enum_ctl_put,
3128 };
3129
3130 static int scarlett2_add_speaker_switch_ctl(
3131         struct usb_mixer_interface *mixer)
3132 {
3133         struct scarlett2_data *private = mixer->private_data;
3134         const struct scarlett2_device_info *info = private->info;
3135
3136         if (!info->has_speaker_switching)
3137                 return 0;
3138
3139         return scarlett2_add_new_ctl(
3140                 mixer, &scarlett2_speaker_switch_enum_ctl,
3141                 0, 1, "Speaker Switching Playback Enum",
3142                 &private->speaker_switching_ctl);
3143 }
3144
3145 /*** Talkback and Talkback Map Controls ***/
3146
3147 static int scarlett2_talkback_enum_ctl_info(
3148         struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3149 {
3150         static const char *const values[3] = {
3151                 "Disabled", "Off", "On"
3152         };
3153
3154         return snd_ctl_enum_info(uinfo, 1, 3, values);
3155 }
3156
3157 static int scarlett2_talkback_enum_ctl_get(
3158         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3159 {
3160         struct usb_mixer_elem_info *elem = kctl->private_data;
3161         struct usb_mixer_interface *mixer = elem->head.mixer;
3162         struct scarlett2_data *private = mixer->private_data;
3163
3164         mutex_lock(&private->data_mutex);
3165         if (private->monitor_other_updated)
3166                 scarlett2_update_monitor_other(mixer);
3167         ucontrol->value.enumerated.item[0] = private->talkback_switch;
3168         mutex_unlock(&private->data_mutex);
3169
3170         return 0;
3171 }
3172
3173 static int scarlett2_talkback_enum_ctl_put(
3174         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3175 {
3176         struct usb_mixer_elem_info *elem = kctl->private_data;
3177         struct usb_mixer_interface *mixer = elem->head.mixer;
3178         struct scarlett2_data *private = mixer->private_data;
3179
3180         int oval, val, err = 0;
3181
3182         mutex_lock(&private->data_mutex);
3183
3184         oval = private->talkback_switch;
3185         val = min(ucontrol->value.enumerated.item[0], 2U);
3186
3187         if (oval == val)
3188                 goto unlock;
3189
3190         private->talkback_switch = val;
3191
3192         /* enable/disable talkback */
3193         err = scarlett2_usb_set_config(
3194                 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
3195                 1, !!val);
3196         if (err < 0)
3197                 goto unlock;
3198
3199         /* if talkback is enabled, select main or alt */
3200         err = scarlett2_usb_set_config(
3201                 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
3202                 1, val == 2);
3203         if (err == 0)
3204                 err = 1;
3205
3206 unlock:
3207         mutex_unlock(&private->data_mutex);
3208         return err;
3209 }
3210
3211 static const struct snd_kcontrol_new scarlett2_talkback_enum_ctl = {
3212         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3213         .name = "",
3214         .info = scarlett2_talkback_enum_ctl_info,
3215         .get  = scarlett2_talkback_enum_ctl_get,
3216         .put  = scarlett2_talkback_enum_ctl_put,
3217 };
3218
3219 static int scarlett2_talkback_map_ctl_get(
3220         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3221 {
3222         struct usb_mixer_elem_info *elem = kctl->private_data;
3223         struct usb_mixer_interface *mixer = elem->head.mixer;
3224         struct scarlett2_data *private = mixer->private_data;
3225         int index = elem->control;
3226
3227         ucontrol->value.integer.value[0] = private->talkback_map[index];
3228
3229         return 0;
3230 }
3231
3232 static int scarlett2_talkback_map_ctl_put(
3233         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3234 {
3235         struct usb_mixer_elem_info *elem = kctl->private_data;
3236         struct usb_mixer_interface *mixer = elem->head.mixer;
3237         struct scarlett2_data *private = mixer->private_data;
3238         const int (*port_count)[SCARLETT2_PORT_DIRNS] =
3239                 private->info->port_count;
3240         int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3241
3242         int index = elem->control;
3243         int oval, val, err = 0, i;
3244         u16 bitmap = 0;
3245
3246         mutex_lock(&private->data_mutex);
3247
3248         oval = private->talkback_map[index];
3249         val = !!ucontrol->value.integer.value[0];
3250
3251         if (oval == val)
3252                 goto unlock;
3253
3254         private->talkback_map[index] = val;
3255
3256         for (i = 0; i < num_mixes; i++)
3257                 bitmap |= private->talkback_map[i] << i;
3258
3259         /* Send updated bitmap to the device */
3260         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_TALKBACK_MAP,
3261                                        0, bitmap);
3262         if (err == 0)
3263                 err = 1;
3264
3265 unlock:
3266         mutex_unlock(&private->data_mutex);
3267         return err;
3268 }
3269
3270 static const struct snd_kcontrol_new scarlett2_talkback_map_ctl = {
3271         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3272         .name = "",
3273         .info = snd_ctl_boolean_mono_info,
3274         .get  = scarlett2_talkback_map_ctl_get,
3275         .put  = scarlett2_talkback_map_ctl_put,
3276 };
3277
3278 static int scarlett2_add_talkback_ctls(
3279         struct usb_mixer_interface *mixer)
3280 {
3281         struct scarlett2_data *private = mixer->private_data;
3282         const struct scarlett2_device_info *info = private->info;
3283         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3284         int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3285         int err, i;
3286         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3287
3288         if (!info->has_talkback)
3289                 return 0;
3290
3291         err = scarlett2_add_new_ctl(
3292                 mixer, &scarlett2_talkback_enum_ctl,
3293                 0, 1, "Talkback Playback Enum",
3294                 &private->talkback_ctl);
3295         if (err < 0)
3296                 return err;
3297
3298         for (i = 0; i < num_mixes; i++) {
3299                 snprintf(s, sizeof(s),
3300                          "Talkback Mix %c Playback Switch", i + 'A');
3301                 err = scarlett2_add_new_ctl(mixer, &scarlett2_talkback_map_ctl,
3302                                             i, 1, s, NULL);
3303                 if (err < 0)
3304                         return err;
3305         }
3306
3307         return 0;
3308 }
3309
3310 /*** Dim/Mute Controls ***/
3311
3312 static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
3313                                       struct snd_ctl_elem_value *ucontrol)
3314 {
3315         struct usb_mixer_elem_info *elem = kctl->private_data;
3316         struct usb_mixer_interface *mixer = elem->head.mixer;
3317         struct scarlett2_data *private = mixer->private_data;
3318
3319         mutex_lock(&private->data_mutex);
3320         if (private->vol_updated)
3321                 scarlett2_update_volumes(mixer);
3322         mutex_unlock(&private->data_mutex);
3323
3324         ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
3325         return 0;
3326 }
3327
3328 static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
3329                                       struct snd_ctl_elem_value *ucontrol)
3330 {
3331         struct usb_mixer_elem_info *elem = kctl->private_data;
3332         struct usb_mixer_interface *mixer = elem->head.mixer;
3333         struct scarlett2_data *private = mixer->private_data;
3334         const struct scarlett2_device_info *info = private->info;
3335         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3336         int num_line_out =
3337                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3338
3339         int index = elem->control;
3340         int oval, val, err = 0, i;
3341
3342         mutex_lock(&private->data_mutex);
3343
3344         oval = private->dim_mute[index];
3345         val = !!ucontrol->value.integer.value[0];
3346
3347         if (oval == val)
3348                 goto unlock;
3349
3350         private->dim_mute[index] = val;
3351
3352         /* Send switch change to the device */
3353         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
3354                                        index, val);
3355         if (err == 0)
3356                 err = 1;
3357
3358         if (index == SCARLETT2_BUTTON_MUTE)
3359                 for (i = 0; i < num_line_out; i++) {
3360                         int line_index = line_out_remap(private, i);
3361
3362                         if (private->vol_sw_hw_switch[line_index]) {
3363                                 private->mute_switch[line_index] = val;
3364                                 snd_ctl_notify(mixer->chip->card,
3365                                                SNDRV_CTL_EVENT_MASK_VALUE,
3366                                                &private->mute_ctls[i]->id);
3367                         }
3368                 }
3369
3370 unlock:
3371         mutex_unlock(&private->data_mutex);
3372         return err;
3373 }
3374
3375 static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
3376         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3377         .name = "",
3378         .info = snd_ctl_boolean_mono_info,
3379         .get  = scarlett2_dim_mute_ctl_get,
3380         .put  = scarlett2_dim_mute_ctl_put
3381 };
3382
3383 /*** Create the analogue output controls ***/
3384
3385 static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
3386 {
3387         struct scarlett2_data *private = mixer->private_data;
3388         const struct scarlett2_device_info *info = private->info;
3389         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3390         int num_line_out =
3391                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3392         int err, i;
3393         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3394
3395         /* Add R/O HW volume control */
3396         if (info->line_out_hw_vol) {
3397                 snprintf(s, sizeof(s), "Master HW Playback Volume");
3398                 err = scarlett2_add_new_ctl(mixer,
3399                                             &scarlett2_master_volume_ctl,
3400                                             0, 1, s, &private->master_vol_ctl);
3401                 if (err < 0)
3402                         return err;
3403         }
3404
3405         /* Add volume controls */
3406         for (i = 0; i < num_line_out; i++) {
3407                 int index = line_out_remap(private, i);
3408
3409                 /* Fader */
3410                 if (info->line_out_descrs[i])
3411                         snprintf(s, sizeof(s),
3412                                  "Line %02d (%s) Playback Volume",
3413                                  i + 1, info->line_out_descrs[i]);
3414                 else
3415                         snprintf(s, sizeof(s),
3416                                  "Line %02d Playback Volume",
3417                                  i + 1);
3418                 err = scarlett2_add_new_ctl(mixer,
3419                                             &scarlett2_line_out_volume_ctl,
3420                                             i, 1, s, &private->vol_ctls[i]);
3421                 if (err < 0)
3422                         return err;
3423
3424                 /* Mute Switch */
3425                 snprintf(s, sizeof(s),
3426                          "Line %02d Mute Playback Switch",
3427                          i + 1);
3428                 err = scarlett2_add_new_ctl(mixer,
3429                                             &scarlett2_mute_ctl,
3430                                             i, 1, s,
3431                                             &private->mute_ctls[i]);
3432                 if (err < 0)
3433                         return err;
3434
3435                 /* Make the fader and mute controls read-only if the
3436                  * SW/HW switch is set to HW
3437                  */
3438                 if (private->vol_sw_hw_switch[index])
3439                         scarlett2_vol_ctl_set_writable(mixer, i, 0);
3440
3441                 /* SW/HW Switch */
3442                 if (info->line_out_hw_vol) {
3443                         snprintf(s, sizeof(s),
3444                                  "Line Out %02d Volume Control Playback Enum",
3445                                  i + 1);
3446                         err = scarlett2_add_new_ctl(mixer,
3447                                                     &scarlett2_sw_hw_enum_ctl,
3448                                                     i, 1, s,
3449                                                     &private->sw_hw_ctls[i]);
3450                         if (err < 0)
3451                                 return err;
3452
3453                         /* Make the switch read-only if the line is
3454                          * involved in speaker switching
3455                          */
3456                         if (private->speaker_switching_switch && i < 4)
3457                                 scarlett2_sw_hw_ctl_ro(private, i);
3458                 }
3459         }
3460
3461         /* Add dim/mute controls */
3462         if (info->line_out_hw_vol)
3463                 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
3464                         err = scarlett2_add_new_ctl(
3465                                 mixer, &scarlett2_dim_mute_ctl,
3466                                 i, 1, scarlett2_dim_mute_names[i],
3467                                 &private->dim_mute_ctls[i]);
3468                         if (err < 0)
3469                                 return err;
3470                 }
3471
3472         return 0;
3473 }
3474
3475 /*** Create the analogue input controls ***/
3476
3477 static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
3478 {
3479         struct scarlett2_data *private = mixer->private_data;
3480         const struct scarlett2_device_info *info = private->info;
3481         int err, i;
3482         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3483         const char *fmt = "Line In %d %s Capture %s";
3484         const char *fmt2 = "Line In %d-%d %s Capture %s";
3485
3486         /* Add input level (line/inst) controls */
3487         for (i = 0; i < info->level_input_count; i++) {
3488                 snprintf(s, sizeof(s), fmt, i + 1 + info->level_input_first,
3489                          "Level", "Enum");
3490                 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
3491                                             i, 1, s, &private->level_ctls[i]);
3492                 if (err < 0)
3493                         return err;
3494         }
3495
3496         /* Add input pad controls */
3497         for (i = 0; i < info->pad_input_count; i++) {
3498                 snprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
3499                 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
3500                                             i, 1, s, &private->pad_ctls[i]);
3501                 if (err < 0)
3502                         return err;
3503         }
3504
3505         /* Add input air controls */
3506         for (i = 0; i < info->air_input_count; i++) {
3507                 snprintf(s, sizeof(s), fmt, i + 1, "Air", "Switch");
3508                 err = scarlett2_add_new_ctl(mixer, &scarlett2_air_ctl,
3509                                             i, 1, s, &private->air_ctls[i]);
3510                 if (err < 0)
3511                         return err;
3512         }
3513
3514         /* Add input phantom controls */
3515         if (info->inputs_per_phantom == 1) {
3516                 for (i = 0; i < info->phantom_count; i++) {
3517                         scnprintf(s, sizeof(s), fmt, i + 1,
3518                                   "Phantom Power", "Switch");
3519                         err = scarlett2_add_new_ctl(
3520                                 mixer, &scarlett2_phantom_ctl,
3521                                 i, 1, s, &private->phantom_ctls[i]);
3522                         if (err < 0)
3523                                 return err;
3524                 }
3525         } else if (info->inputs_per_phantom > 1) {
3526                 for (i = 0; i < info->phantom_count; i++) {
3527                         int from = i * info->inputs_per_phantom + 1;
3528                         int to = (i + 1) * info->inputs_per_phantom;
3529
3530                         scnprintf(s, sizeof(s), fmt2, from, to,
3531                                   "Phantom Power", "Switch");
3532                         err = scarlett2_add_new_ctl(
3533                                 mixer, &scarlett2_phantom_ctl,
3534                                 i, 1, s, &private->phantom_ctls[i]);
3535                         if (err < 0)
3536                                 return err;
3537                 }
3538         }
3539         if (info->phantom_count) {
3540                 err = scarlett2_add_new_ctl(
3541                         mixer, &scarlett2_phantom_persistence_ctl, 0, 1,
3542                         "Phantom Power Persistence Capture Switch", NULL);
3543                 if (err < 0)
3544                         return err;
3545         }
3546
3547         return 0;
3548 }
3549
3550 /*** Mixer Volume Controls ***/
3551
3552 static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
3553                                     struct snd_ctl_elem_info *uinfo)
3554 {
3555         struct usb_mixer_elem_info *elem = kctl->private_data;
3556
3557         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3558         uinfo->count = elem->channels;
3559         uinfo->value.integer.min = 0;
3560         uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
3561         uinfo->value.integer.step = 1;
3562         return 0;
3563 }
3564
3565 static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
3566                                    struct snd_ctl_elem_value *ucontrol)
3567 {
3568         struct usb_mixer_elem_info *elem = kctl->private_data;
3569         struct scarlett2_data *private = elem->head.mixer->private_data;
3570
3571         ucontrol->value.integer.value[0] = private->mix[elem->control];
3572         return 0;
3573 }
3574
3575 static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
3576                                    struct snd_ctl_elem_value *ucontrol)
3577 {
3578         struct usb_mixer_elem_info *elem = kctl->private_data;
3579         struct usb_mixer_interface *mixer = elem->head.mixer;
3580         struct scarlett2_data *private = mixer->private_data;
3581         const struct scarlett2_device_info *info = private->info;
3582         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3583         int oval, val, num_mixer_in, mix_num, err = 0;
3584         int index = elem->control;
3585
3586         mutex_lock(&private->data_mutex);
3587
3588         oval = private->mix[index];
3589         val = ucontrol->value.integer.value[0];
3590         num_mixer_in = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
3591         mix_num = index / num_mixer_in;
3592
3593         if (oval == val)
3594                 goto unlock;
3595
3596         private->mix[index] = val;
3597         err = scarlett2_usb_set_mix(mixer, mix_num);
3598         if (err == 0)
3599                 err = 1;
3600
3601 unlock:
3602         mutex_unlock(&private->data_mutex);
3603         return err;
3604 }
3605
3606 static const DECLARE_TLV_DB_MINMAX(
3607         db_scale_scarlett2_mixer,
3608         SCARLETT2_MIXER_MIN_DB * 100,
3609         SCARLETT2_MIXER_MAX_DB * 100
3610 );
3611
3612 static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
3613         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3614         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3615                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3616         .name = "",
3617         .info = scarlett2_mixer_ctl_info,
3618         .get  = scarlett2_mixer_ctl_get,
3619         .put  = scarlett2_mixer_ctl_put,
3620         .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
3621         .tlv = { .p = db_scale_scarlett2_mixer }
3622 };
3623
3624 static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
3625 {
3626         struct scarlett2_data *private = mixer->private_data;
3627         const struct scarlett2_device_info *info = private->info;
3628         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3629         int err, i, j;
3630         int index;
3631         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3632
3633         int num_inputs =
3634                 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
3635         int num_outputs =
3636                 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3637
3638         for (i = 0, index = 0; i < num_outputs; i++)
3639                 for (j = 0; j < num_inputs; j++, index++) {
3640                         snprintf(s, sizeof(s),
3641                                  "Mix %c Input %02d Playback Volume",
3642                                  'A' + i, j + 1);
3643                         err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
3644                                                     index, 1, s, NULL);
3645                         if (err < 0)
3646                                 return err;
3647                 }
3648
3649         return 0;
3650 }
3651
3652 /*** Mux Source Selection Controls ***/
3653
3654 static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
3655                                            struct snd_ctl_elem_info *uinfo)
3656 {
3657         struct usb_mixer_elem_info *elem = kctl->private_data;
3658         struct scarlett2_data *private = elem->head.mixer->private_data;
3659         const struct scarlett2_device_info *info = private->info;
3660         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3661         unsigned int item = uinfo->value.enumerated.item;
3662         int items = private->num_mux_srcs;
3663         int port_type;
3664
3665         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3666         uinfo->count = elem->channels;
3667         uinfo->value.enumerated.items = items;
3668
3669         if (item >= items)
3670                 item = uinfo->value.enumerated.item = items - 1;
3671
3672         for (port_type = 0;
3673              port_type < SCARLETT2_PORT_TYPE_COUNT;
3674              port_type++) {
3675                 if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
3676                         const struct scarlett2_port *port =
3677                                 &scarlett2_ports[port_type];
3678
3679                         sprintf(uinfo->value.enumerated.name,
3680                                 port->src_descr, item + port->src_num_offset);
3681                         return 0;
3682                 }
3683                 item -= port_count[port_type][SCARLETT2_PORT_IN];
3684         }
3685
3686         return -EINVAL;
3687 }
3688
3689 static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
3690                                           struct snd_ctl_elem_value *ucontrol)
3691 {
3692         struct usb_mixer_elem_info *elem = kctl->private_data;
3693         struct usb_mixer_interface *mixer = elem->head.mixer;
3694         struct scarlett2_data *private = mixer->private_data;
3695         int index = line_out_remap(private, elem->control);
3696
3697         mutex_lock(&private->data_mutex);
3698         if (private->mux_updated)
3699                 scarlett2_usb_get_mux(mixer);
3700         ucontrol->value.enumerated.item[0] = private->mux[index];
3701         mutex_unlock(&private->data_mutex);
3702
3703         return 0;
3704 }
3705
3706 static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
3707                                           struct snd_ctl_elem_value *ucontrol)
3708 {
3709         struct usb_mixer_elem_info *elem = kctl->private_data;
3710         struct usb_mixer_interface *mixer = elem->head.mixer;
3711         struct scarlett2_data *private = mixer->private_data;
3712         int index = line_out_remap(private, elem->control);
3713         int oval, val, err = 0;
3714
3715         mutex_lock(&private->data_mutex);
3716
3717         oval = private->mux[index];
3718         val = min(ucontrol->value.enumerated.item[0],
3719                   private->num_mux_srcs - 1U);
3720
3721         if (oval == val)
3722                 goto unlock;
3723
3724         private->mux[index] = val;
3725         err = scarlett2_usb_set_mux(mixer);
3726         if (err == 0)
3727                 err = 1;
3728
3729 unlock:
3730         mutex_unlock(&private->data_mutex);
3731         return err;
3732 }
3733
3734 static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
3735         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3736         .name = "",
3737         .info = scarlett2_mux_src_enum_ctl_info,
3738         .get  = scarlett2_mux_src_enum_ctl_get,
3739         .put  = scarlett2_mux_src_enum_ctl_put,
3740 };
3741
3742 static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
3743 {
3744         struct scarlett2_data *private = mixer->private_data;
3745         const struct scarlett2_device_info *info = private->info;
3746         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3747         int port_type, channel, i;
3748
3749         for (i = 0, port_type = 0;
3750              port_type < SCARLETT2_PORT_TYPE_COUNT;
3751              port_type++) {
3752                 for (channel = 0;
3753                      channel < port_count[port_type][SCARLETT2_PORT_OUT];
3754                      channel++, i++) {
3755                         int err;
3756                         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3757                         const char *const descr =
3758                                 scarlett2_ports[port_type].dst_descr;
3759
3760                         snprintf(s, sizeof(s) - 5, descr, channel + 1);
3761                         strcat(s, " Enum");
3762
3763                         err = scarlett2_add_new_ctl(mixer,
3764                                                     &scarlett2_mux_src_enum_ctl,
3765                                                     i, 1, s,
3766                                                     &private->mux_ctls[i]);
3767                         if (err < 0)
3768                                 return err;
3769                 }
3770         }
3771
3772         return 0;
3773 }
3774
3775 /*** Meter Controls ***/
3776
3777 static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
3778                                     struct snd_ctl_elem_info *uinfo)
3779 {
3780         struct usb_mixer_elem_info *elem = kctl->private_data;
3781
3782         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3783         uinfo->count = elem->channels;
3784         uinfo->value.integer.min = 0;
3785         uinfo->value.integer.max = 4095;
3786         uinfo->value.integer.step = 1;
3787         return 0;
3788 }
3789
3790 static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
3791                                    struct snd_ctl_elem_value *ucontrol)
3792 {
3793         struct usb_mixer_elem_info *elem = kctl->private_data;
3794         struct scarlett2_data *private = elem->head.mixer->private_data;
3795         u8 *meter_level_map = private->meter_level_map;
3796         u16 meter_levels[SCARLETT2_MAX_METERS];
3797         int i, err;
3798
3799         err = scarlett2_usb_get_meter_levels(elem->head.mixer, elem->channels,
3800                                              meter_levels);
3801         if (err < 0)
3802                 return err;
3803
3804         /* copy & translate from meter_levels[] using meter_level_map[] */
3805         for (i = 0; i < elem->channels; i++) {
3806                 int idx = meter_level_map[i];
3807                 int value;
3808
3809                 if (idx == 255)
3810                         value = 0;
3811                 else
3812                         value = meter_levels[idx];
3813
3814                 ucontrol->value.integer.value[i] = value;
3815         }
3816
3817         return 0;
3818 }
3819
3820 static const struct snd_kcontrol_new scarlett2_meter_ctl = {
3821         .iface = SNDRV_CTL_ELEM_IFACE_PCM,
3822         .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3823         .name = "",
3824         .info = scarlett2_meter_ctl_info,
3825         .get  = scarlett2_meter_ctl_get
3826 };
3827
3828 static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
3829 {
3830         struct scarlett2_data *private = mixer->private_data;
3831
3832         /* devices without a mixer also don't support reporting levels */
3833         if (private->info->config_set == SCARLETT2_CONFIG_SET_GEN_3A)
3834                 return 0;
3835
3836         return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
3837                                      0, private->num_mux_dsts,
3838                                      "Level Meter", NULL);
3839 }
3840
3841 /*** MSD Controls ***/
3842
3843 static int scarlett2_msd_ctl_get(struct snd_kcontrol *kctl,
3844                                  struct snd_ctl_elem_value *ucontrol)
3845 {
3846         struct usb_mixer_elem_info *elem = kctl->private_data;
3847         struct scarlett2_data *private = elem->head.mixer->private_data;
3848
3849         ucontrol->value.integer.value[0] = private->msd_switch;
3850         return 0;
3851 }
3852
3853 static int scarlett2_msd_ctl_put(struct snd_kcontrol *kctl,
3854                                  struct snd_ctl_elem_value *ucontrol)
3855 {
3856         struct usb_mixer_elem_info *elem = kctl->private_data;
3857         struct usb_mixer_interface *mixer = elem->head.mixer;
3858         struct scarlett2_data *private = mixer->private_data;
3859
3860         int oval, val, err = 0;
3861
3862         mutex_lock(&private->data_mutex);
3863
3864         oval = private->msd_switch;
3865         val = !!ucontrol->value.integer.value[0];
3866
3867         if (oval == val)
3868                 goto unlock;
3869
3870         private->msd_switch = val;
3871
3872         /* Send switch change to the device */
3873         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MSD_SWITCH,
3874                                        0, val);
3875         if (err == 0)
3876                 err = 1;
3877
3878 unlock:
3879         mutex_unlock(&private->data_mutex);
3880         return err;
3881 }
3882
3883 static const struct snd_kcontrol_new scarlett2_msd_ctl = {
3884         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3885         .name = "",
3886         .info = snd_ctl_boolean_mono_info,
3887         .get  = scarlett2_msd_ctl_get,
3888         .put  = scarlett2_msd_ctl_put,
3889 };
3890
3891 static int scarlett2_add_msd_ctl(struct usb_mixer_interface *mixer)
3892 {
3893         struct scarlett2_data *private = mixer->private_data;
3894         const struct scarlett2_device_info *info = private->info;
3895
3896         if (!info->has_msd_mode)
3897                 return 0;
3898
3899         /* If MSD mode is off, hide the switch by default */
3900         if (!private->msd_switch && !(mixer->chip->setup & SCARLETT2_MSD_ENABLE))
3901                 return 0;
3902
3903         /* Add MSD control */
3904         return scarlett2_add_new_ctl(mixer, &scarlett2_msd_ctl,
3905                                      0, 1, "MSD Mode Switch", NULL);
3906 }
3907
3908 /*** Standalone Control ***/
3909
3910 static int scarlett2_standalone_ctl_get(struct snd_kcontrol *kctl,
3911                                         struct snd_ctl_elem_value *ucontrol)
3912 {
3913         struct usb_mixer_elem_info *elem = kctl->private_data;
3914         struct scarlett2_data *private = elem->head.mixer->private_data;
3915
3916         ucontrol->value.integer.value[0] = private->standalone_switch;
3917         return 0;
3918 }
3919
3920 static int scarlett2_standalone_ctl_put(struct snd_kcontrol *kctl,
3921                                         struct snd_ctl_elem_value *ucontrol)
3922 {
3923         struct usb_mixer_elem_info *elem = kctl->private_data;
3924         struct usb_mixer_interface *mixer = elem->head.mixer;
3925         struct scarlett2_data *private = mixer->private_data;
3926
3927         int oval, val, err = 0;
3928
3929         mutex_lock(&private->data_mutex);
3930
3931         oval = private->standalone_switch;
3932         val = !!ucontrol->value.integer.value[0];
3933
3934         if (oval == val)
3935                 goto unlock;
3936
3937         private->standalone_switch = val;
3938
3939         /* Send switch change to the device */
3940         err = scarlett2_usb_set_config(mixer,
3941                                        SCARLETT2_CONFIG_STANDALONE_SWITCH,
3942                                        0, val);
3943         if (err == 0)
3944                 err = 1;
3945
3946 unlock:
3947         mutex_unlock(&private->data_mutex);
3948         return err;
3949 }
3950
3951 static const struct snd_kcontrol_new scarlett2_standalone_ctl = {
3952         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3953         .name = "",
3954         .info = snd_ctl_boolean_mono_info,
3955         .get  = scarlett2_standalone_ctl_get,
3956         .put  = scarlett2_standalone_ctl_put,
3957 };
3958
3959 static int scarlett2_add_standalone_ctl(struct usb_mixer_interface *mixer)
3960 {
3961         struct scarlett2_data *private = mixer->private_data;
3962
3963         if (private->info->config_set == SCARLETT2_CONFIG_SET_GEN_3A)
3964                 return 0;
3965
3966         /* Add standalone control */
3967         return scarlett2_add_new_ctl(mixer, &scarlett2_standalone_ctl,
3968                                      0, 1, "Standalone Switch", NULL);
3969 }
3970
3971 /*** Cleanup/Suspend Callbacks ***/
3972
3973 static void scarlett2_private_free(struct usb_mixer_interface *mixer)
3974 {
3975         struct scarlett2_data *private = mixer->private_data;
3976
3977         cancel_delayed_work_sync(&private->work);
3978         kfree(private);
3979         mixer->private_data = NULL;
3980 }
3981
3982 static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
3983 {
3984         struct scarlett2_data *private = mixer->private_data;
3985
3986         if (cancel_delayed_work_sync(&private->work))
3987                 scarlett2_config_save(private->mixer);
3988 }
3989
3990 /*** Initialisation ***/
3991
3992 static void scarlett2_count_mux_io(struct scarlett2_data *private)
3993 {
3994         const struct scarlett2_device_info *info = private->info;
3995         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3996         int port_type, srcs = 0, dsts = 0;
3997
3998         for (port_type = 0;
3999              port_type < SCARLETT2_PORT_TYPE_COUNT;
4000              port_type++) {
4001                 srcs += port_count[port_type][SCARLETT2_PORT_IN];
4002                 dsts += port_count[port_type][SCARLETT2_PORT_OUT];
4003         }
4004
4005         private->num_mux_srcs = srcs;
4006         private->num_mux_dsts = dsts;
4007 }
4008
4009 /* Look through the interface descriptors for the Focusrite Control
4010  * interface (bInterfaceClass = 255 Vendor Specific Class) and set
4011  * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
4012  * in private
4013  */
4014 static int scarlett2_find_fc_interface(struct usb_device *dev,
4015                                        struct scarlett2_data *private)
4016 {
4017         struct usb_host_config *config = dev->actconfig;
4018         int i;
4019
4020         for (i = 0; i < config->desc.bNumInterfaces; i++) {
4021                 struct usb_interface *intf = config->interface[i];
4022                 struct usb_interface_descriptor *desc =
4023                         &intf->altsetting[0].desc;
4024                 struct usb_endpoint_descriptor *epd;
4025
4026                 if (desc->bInterfaceClass != 255)
4027                         continue;
4028
4029                 epd = get_endpoint(intf->altsetting, 0);
4030                 private->bInterfaceNumber = desc->bInterfaceNumber;
4031                 private->bEndpointAddress = epd->bEndpointAddress &
4032                         USB_ENDPOINT_NUMBER_MASK;
4033                 private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
4034                 private->bInterval = epd->bInterval;
4035                 return 0;
4036         }
4037
4038         return -EINVAL;
4039 }
4040
4041 /* Initialise private data */
4042 static int scarlett2_init_private(struct usb_mixer_interface *mixer,
4043                                   const struct scarlett2_device_entry *entry)
4044 {
4045         struct scarlett2_data *private =
4046                 kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);
4047
4048         if (!private)
4049                 return -ENOMEM;
4050
4051         mutex_init(&private->usb_mutex);
4052         mutex_init(&private->data_mutex);
4053         INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
4054
4055         mixer->private_data = private;
4056         mixer->private_free = scarlett2_private_free;
4057         mixer->private_suspend = scarlett2_private_suspend;
4058
4059         private->info = entry->info;
4060         private->series_name = entry->series_name;
4061         scarlett2_count_mux_io(private);
4062         private->scarlett2_seq = 0;
4063         private->mixer = mixer;
4064
4065         return scarlett2_find_fc_interface(mixer->chip->dev, private);
4066 }
4067
4068 /* Cargo cult proprietary initialisation sequence */
4069 static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
4070 {
4071         struct usb_device *dev = mixer->chip->dev;
4072         struct scarlett2_data *private = mixer->private_data;
4073         u8 step0_buf[24];
4074         u8 step2_buf[84];
4075         int err;
4076
4077         if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
4078                 return -EINVAL;
4079
4080         /* step 0 */
4081         err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
4082                                SCARLETT2_USB_CMD_INIT,
4083                                step0_buf, sizeof(step0_buf));
4084         if (err < 0)
4085                 return err;
4086
4087         /* step 1 */
4088         private->scarlett2_seq = 1;
4089         err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
4090         if (err < 0)
4091                 return err;
4092
4093         /* step 2 */
4094         private->scarlett2_seq = 1;
4095         err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_2,
4096                             NULL, 0,
4097                             step2_buf, sizeof(step2_buf));
4098         if (err < 0)
4099                 return err;
4100
4101         /* extract 4-byte firmware version from step2_buf[8] */
4102         private->firmware_version = le32_to_cpu(*(__le32 *)(step2_buf + 8));
4103         usb_audio_info(mixer->chip,
4104                        "Firmware version %d\n",
4105                        private->firmware_version);
4106
4107         return 0;
4108 }
4109
4110 /* Read configuration from the interface on start */
4111 static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
4112 {
4113         struct scarlett2_data *private = mixer->private_data;
4114         const struct scarlett2_device_info *info = private->info;
4115         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
4116         int num_line_out =
4117                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
4118         int num_mixer_out =
4119                 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
4120         struct scarlett2_usb_volume_status volume_status;
4121         int err, i;
4122
4123         if (info->has_msd_mode) {
4124                 err = scarlett2_usb_get_config(
4125                         mixer, SCARLETT2_CONFIG_MSD_SWITCH,
4126                         1, &private->msd_switch);
4127                 if (err < 0)
4128                         return err;
4129
4130                 /* no other controls are created if MSD mode is on */
4131                 if (private->msd_switch)
4132                         return 0;
4133         }
4134
4135         err = scarlett2_update_input_other(mixer);
4136         if (err < 0)
4137                 return err;
4138
4139         err = scarlett2_update_monitor_other(mixer);
4140         if (err < 0)
4141                 return err;
4142
4143         /* the rest of the configuration is for devices with a mixer */
4144         if (info->config_set == SCARLETT2_CONFIG_SET_GEN_3A)
4145                 return 0;
4146
4147         err = scarlett2_usb_get_config(
4148                 mixer, SCARLETT2_CONFIG_STANDALONE_SWITCH,
4149                 1, &private->standalone_switch);
4150         if (err < 0)
4151                 return err;
4152
4153         err = scarlett2_update_sync(mixer);
4154         if (err < 0)
4155                 return err;
4156
4157         err = scarlett2_usb_get_volume_status(mixer, &volume_status);
4158         if (err < 0)
4159                 return err;
4160
4161         if (info->line_out_hw_vol)
4162                 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
4163                         private->dim_mute[i] = !!volume_status.dim_mute[i];
4164
4165         private->master_vol = clamp(
4166                 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
4167                 0, SCARLETT2_VOLUME_BIAS);
4168
4169         for (i = 0; i < num_line_out; i++) {
4170                 int volume, mute;
4171
4172                 private->vol_sw_hw_switch[i] =
4173                         info->line_out_hw_vol
4174                                 && volume_status.sw_hw_switch[i];
4175
4176                 volume = private->vol_sw_hw_switch[i]
4177                            ? volume_status.master_vol
4178                            : volume_status.sw_vol[i];
4179                 volume = clamp(volume + SCARLETT2_VOLUME_BIAS,
4180                                0, SCARLETT2_VOLUME_BIAS);
4181                 private->vol[i] = volume;
4182
4183                 mute = private->vol_sw_hw_switch[i]
4184                          ? private->dim_mute[SCARLETT2_BUTTON_MUTE]
4185                          : volume_status.mute_switch[i];
4186                 private->mute_switch[i] = mute;
4187         }
4188
4189         for (i = 0; i < num_mixer_out; i++) {
4190                 err = scarlett2_usb_get_mix(mixer, i);
4191                 if (err < 0)
4192                         return err;
4193         }
4194
4195         return scarlett2_usb_get_mux(mixer);
4196 }
4197
4198 /* Notify on sync change */
4199 static void scarlett2_notify_sync(
4200         struct usb_mixer_interface *mixer)
4201 {
4202         struct scarlett2_data *private = mixer->private_data;
4203
4204         private->sync_updated = 1;
4205
4206         snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
4207                        &private->sync_ctl->id);
4208 }
4209
4210 /* Notify on monitor change */
4211 static void scarlett2_notify_monitor(
4212         struct usb_mixer_interface *mixer)
4213 {
4214         struct snd_card *card = mixer->chip->card;
4215         struct scarlett2_data *private = mixer->private_data;
4216         const struct scarlett2_device_info *info = private->info;
4217         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
4218         int num_line_out =
4219                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
4220         int i;
4221
4222         /* if line_out_hw_vol is 0, there are no controls to update */
4223         if (!info->line_out_hw_vol)
4224                 return;
4225
4226         private->vol_updated = 1;
4227
4228         snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
4229                        &private->master_vol_ctl->id);
4230
4231         for (i = 0; i < num_line_out; i++)
4232                 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
4233                         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4234                                        &private->vol_ctls[i]->id);
4235 }
4236
4237 /* Notify on dim/mute change */
4238 static void scarlett2_notify_dim_mute(
4239         struct usb_mixer_interface *mixer)
4240 {
4241         struct snd_card *card = mixer->chip->card;
4242         struct scarlett2_data *private = mixer->private_data;
4243         const struct scarlett2_device_info *info = private->info;
4244         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
4245         int num_line_out =
4246                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
4247         int i;
4248
4249         private->vol_updated = 1;
4250
4251         if (!info->line_out_hw_vol)
4252                 return;
4253
4254         for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
4255                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4256                                &private->dim_mute_ctls[i]->id);
4257
4258         for (i = 0; i < num_line_out; i++)
4259                 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
4260                         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4261                                        &private->mute_ctls[i]->id);
4262 }
4263
4264 /* Notify on "input other" change (level/pad/air) */
4265 static void scarlett2_notify_input_other(
4266         struct usb_mixer_interface *mixer)
4267 {
4268         struct snd_card *card = mixer->chip->card;
4269         struct scarlett2_data *private = mixer->private_data;
4270         const struct scarlett2_device_info *info = private->info;
4271         int i;
4272
4273         private->input_other_updated = 1;
4274
4275         for (i = 0; i < info->level_input_count; i++)
4276                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4277                                &private->level_ctls[i]->id);
4278         for (i = 0; i < info->pad_input_count; i++)
4279                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4280                                &private->pad_ctls[i]->id);
4281         for (i = 0; i < info->air_input_count; i++)
4282                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4283                                &private->air_ctls[i]->id);
4284         for (i = 0; i < info->phantom_count; i++)
4285                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4286                                &private->phantom_ctls[i]->id);
4287 }
4288
4289 /* Notify on "monitor other" change (direct monitor, speaker
4290  * switching, talkback)
4291  */
4292 static void scarlett2_notify_monitor_other(
4293         struct usb_mixer_interface *mixer)
4294 {
4295         struct snd_card *card = mixer->chip->card;
4296         struct scarlett2_data *private = mixer->private_data;
4297         const struct scarlett2_device_info *info = private->info;
4298
4299         private->monitor_other_updated = 1;
4300
4301         if (info->direct_monitor) {
4302                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4303                                &private->direct_monitor_ctl->id);
4304                 return;
4305         }
4306
4307         if (info->has_speaker_switching)
4308                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4309                                &private->speaker_switching_ctl->id);
4310
4311         if (info->has_talkback)
4312                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4313                                &private->talkback_ctl->id);
4314
4315         /* if speaker switching was recently enabled or disabled,
4316          * invalidate the dim/mute and mux enum controls
4317          */
4318         if (private->speaker_switching_switched) {
4319                 int i;
4320
4321                 scarlett2_notify_dim_mute(mixer);
4322
4323                 private->speaker_switching_switched = 0;
4324                 private->mux_updated = 1;
4325
4326                 for (i = 0; i < private->num_mux_dsts; i++)
4327                         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4328                                        &private->mux_ctls[i]->id);
4329         }
4330 }
4331
4332 /* Interrupt callback */
4333 static void scarlett2_notify(struct urb *urb)
4334 {
4335         struct usb_mixer_interface *mixer = urb->context;
4336         int len = urb->actual_length;
4337         int ustatus = urb->status;
4338         u32 data;
4339
4340         if (ustatus != 0 || len != 8)
4341                 goto requeue;
4342
4343         data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
4344         if (data & SCARLETT2_USB_NOTIFY_SYNC)
4345                 scarlett2_notify_sync(mixer);
4346         if (data & SCARLETT2_USB_NOTIFY_MONITOR)
4347                 scarlett2_notify_monitor(mixer);
4348         if (data & SCARLETT2_USB_NOTIFY_DIM_MUTE)
4349                 scarlett2_notify_dim_mute(mixer);
4350         if (data & SCARLETT2_USB_NOTIFY_INPUT_OTHER)
4351                 scarlett2_notify_input_other(mixer);
4352         if (data & SCARLETT2_USB_NOTIFY_MONITOR_OTHER)
4353                 scarlett2_notify_monitor_other(mixer);
4354
4355 requeue:
4356         if (ustatus != -ENOENT &&
4357             ustatus != -ECONNRESET &&
4358             ustatus != -ESHUTDOWN) {
4359                 urb->dev = mixer->chip->dev;
4360                 usb_submit_urb(urb, GFP_ATOMIC);
4361         }
4362 }
4363
4364 static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
4365 {
4366         struct usb_device *dev = mixer->chip->dev;
4367         struct scarlett2_data *private = mixer->private_data;
4368         unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
4369         void *transfer_buffer;
4370
4371         if (mixer->urb) {
4372                 usb_audio_err(mixer->chip,
4373                               "%s: mixer urb already in use!\n", __func__);
4374                 return 0;
4375         }
4376
4377         if (usb_pipe_type_check(dev, pipe))
4378                 return -EINVAL;
4379
4380         mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
4381         if (!mixer->urb)
4382                 return -ENOMEM;
4383
4384         transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
4385         if (!transfer_buffer)
4386                 return -ENOMEM;
4387
4388         usb_fill_int_urb(mixer->urb, dev, pipe,
4389                          transfer_buffer, private->wMaxPacketSize,
4390                          scarlett2_notify, mixer, private->bInterval);
4391
4392         return usb_submit_urb(mixer->urb, GFP_KERNEL);
4393 }
4394
4395 static const struct scarlett2_device_entry *get_scarlett2_device_entry(
4396         struct usb_mixer_interface *mixer)
4397 {
4398         const struct scarlett2_device_entry *entry = scarlett2_devices;
4399
4400         /* Find entry in scarlett2_devices */
4401         while (entry->usb_id && entry->usb_id != mixer->chip->usb_id)
4402                 entry++;
4403         if (!entry->usb_id)
4404                 return NULL;
4405
4406         return entry;
4407 }
4408
4409 static int snd_scarlett2_controls_create(
4410         struct usb_mixer_interface *mixer,
4411         const struct scarlett2_device_entry *entry)
4412 {
4413         int err;
4414
4415         /* Initialise private data */
4416         err = scarlett2_init_private(mixer, entry);
4417         if (err < 0)
4418                 return err;
4419
4420         /* Send proprietary USB initialisation sequence */
4421         err = scarlett2_usb_init(mixer);
4422         if (err < 0)
4423                 return err;
4424
4425         /* Add firmware version control */
4426         err = scarlett2_add_firmware_version_ctl(mixer);
4427         if (err < 0)
4428                 return err;
4429
4430         /* Read volume levels and controls from the interface */
4431         err = scarlett2_read_configs(mixer);
4432         if (err < 0)
4433                 return err;
4434
4435         /* Create the MSD control */
4436         err = scarlett2_add_msd_ctl(mixer);
4437         if (err < 0)
4438                 return err;
4439
4440         /* If MSD mode is enabled, don't create any other controls */
4441         if (((struct scarlett2_data *)mixer->private_data)->msd_switch)
4442                 return 0;
4443
4444         /* Create the analogue output controls */
4445         err = scarlett2_add_line_out_ctls(mixer);
4446         if (err < 0)
4447                 return err;
4448
4449         /* Create the analogue input controls */
4450         err = scarlett2_add_line_in_ctls(mixer);
4451         if (err < 0)
4452                 return err;
4453
4454         /* Create the input, output, and mixer mux input selections */
4455         err = scarlett2_add_mux_enums(mixer);
4456         if (err < 0)
4457                 return err;
4458
4459         /* Create the matrix mixer controls */
4460         err = scarlett2_add_mixer_ctls(mixer);
4461         if (err < 0)
4462                 return err;
4463
4464         /* Create the level meter controls */
4465         err = scarlett2_add_meter_ctl(mixer);
4466         if (err < 0)
4467                 return err;
4468
4469         /* Create the sync control */
4470         err = scarlett2_add_sync_ctl(mixer);
4471         if (err < 0)
4472                 return err;
4473
4474         /* Create the direct monitor control */
4475         err = scarlett2_add_direct_monitor_ctl(mixer);
4476         if (err < 0)
4477                 return err;
4478
4479         /* Create the speaker switching control */
4480         err = scarlett2_add_speaker_switch_ctl(mixer);
4481         if (err < 0)
4482                 return err;
4483
4484         /* Create the talkback controls */
4485         err = scarlett2_add_talkback_ctls(mixer);
4486         if (err < 0)
4487                 return err;
4488
4489         /* Create the standalone control */
4490         err = scarlett2_add_standalone_ctl(mixer);
4491         if (err < 0)
4492                 return err;
4493
4494         /* Set up the interrupt polling */
4495         err = scarlett2_init_notify(mixer);
4496         if (err < 0)
4497                 return err;
4498
4499         return 0;
4500 }
4501
4502 int snd_scarlett2_init(struct usb_mixer_interface *mixer)
4503 {
4504         struct snd_usb_audio *chip = mixer->chip;
4505         const struct scarlett2_device_entry *entry;
4506         int err;
4507
4508         /* only use UAC_VERSION_2 */
4509         if (!mixer->protocol)
4510                 return 0;
4511
4512         /* find entry in scarlett2_devices */
4513         entry = get_scarlett2_device_entry(mixer);
4514         if (!entry) {
4515                 usb_audio_err(mixer->chip,
4516                               "%s: missing device entry for %04x:%04x\n",
4517                               __func__,
4518                               USB_ID_VENDOR(chip->usb_id),
4519                               USB_ID_PRODUCT(chip->usb_id));
4520                 return 0;
4521         }
4522
4523         if (chip->setup & SCARLETT2_DISABLE) {
4524                 usb_audio_info(chip,
4525                         "Focusrite %s Mixer Driver disabled "
4526                         "by modprobe options (snd_usb_audio "
4527                         "vid=0x%04x pid=0x%04x device_setup=%d)\n",
4528                         entry->series_name,
4529                         USB_ID_VENDOR(chip->usb_id),
4530                         USB_ID_PRODUCT(chip->usb_id),
4531                         SCARLETT2_DISABLE);
4532                 return 0;
4533         }
4534
4535         usb_audio_info(chip,
4536                 "Focusrite %s Mixer Driver enabled (pid=0x%04x); "
4537                 "report any issues to g@b4.vu",
4538                 entry->series_name,
4539                 USB_ID_PRODUCT(chip->usb_id));
4540
4541         err = snd_scarlett2_controls_create(mixer, entry);
4542         if (err < 0)
4543                 usb_audio_err(mixer->chip,
4544                               "Error initialising %s Mixer Driver: %d",
4545                               entry->series_name,
4546                               err);
4547
4548         return err;
4549 }