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