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