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