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