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