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