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