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