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