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