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