ALSA: scarlett2: Add missing error check to scarlett2_config_save()
[platform/kernel/linux-rpi.git] / sound / usb / mixer_scarlett_gen2.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *   Focusrite Scarlett Gen 2/3 and Clarett+ Driver for ALSA
4  *
5  *   Supported models:
6  *   - 6i6/18i8/18i20 Gen 2
7  *   - Solo/2i2/4i4/8i6/18i8/18i20 Gen 3
8  *   - Clarett+ 8Pre
9  *
10  *   Copyright (c) 2018-2022 by Geoffrey D. Bennett <g at b4.vu>
11  *   Copyright (c) 2020-2021 by Vladimir Sadovnikov <sadko4u@gmail.com>
12  *   Copyright (c) 2022 by Christian Colglazier <christian@cacolglazier.com>
13  *
14  *   Based on the Scarlett (Gen 1) Driver for ALSA:
15  *
16  *   Copyright (c) 2013 by Tobias Hoffmann
17  *   Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
18  *   Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
19  *   Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
20  *
21  *   Many codes borrowed from audio.c by
22  *     Alan Cox (alan at lxorguk.ukuu.org.uk)
23  *     Thomas Sailer (sailer at ife.ee.ethz.ch)
24  *
25  *   Code cleanup:
26  *   David Henningsson <david.henningsson at canonical.com>
27  */
28
29 /* The protocol was reverse engineered by looking at the communication
30  * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20
31  * (firmware 1083) using usbmon in July-August 2018.
32  *
33  * Scarlett 18i8 support added in April 2019.
34  *
35  * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
36  * for providing usbmon output and testing).
37  *
38  * Scarlett 4i4/8i6 Gen 3 support added in May 2020 (thanks to Laurent
39  * Debricon for donating a 4i4 and to Fredrik Unger for providing 8i6
40  * usbmon output and testing).
41  *
42  * Scarlett 18i8/18i20 Gen 3 support added in June 2020 (thanks to
43  * Darren Jaeckel, Alex Sedlack, and Clovis Lunel for providing usbmon
44  * output, protocol traces and testing).
45  *
46  * Support for loading mixer volume and mux configuration from the
47  * interface during driver initialisation added in May 2021 (thanks to
48  * Vladimir Sadovnikov for figuring out how).
49  *
50  * Support for Solo/2i2 Gen 3 added in May 2021 (thanks to Alexander
51  * Vorona for 2i2 protocol traces).
52  *
53  * Support for phantom power, direct monitoring, speaker switching,
54  * and talkback added in May-June 2021.
55  *
56  * Support for Clarett+ 8Pre added in Aug 2022 by Christian
57  * Colglazier.
58  *
59  * This ALSA mixer gives access to (model-dependent):
60  *  - input, output, mixer-matrix muxes
61  *  - mixer-matrix gain stages
62  *  - gain/volume/mute controls
63  *  - level meters
64  *  - line/inst level, pad, and air controls
65  *  - phantom power, direct monitor, speaker switching, and talkback
66  *    controls
67  *  - disable/enable MSD mode
68  *  - disable/enable standalone mode
69  *
70  * <ditaa>
71  *    /--------------\    18chn            20chn     /--------------\
72  *    | Hardware  in +--+------\    /-------------+--+ ALSA PCM out |
73  *    \--------------/  |      |    |             |  \--------------/
74  *                      |      |    |    /-----\  |
75  *                      |      |    |    |     |  |
76  *                      |      v    v    v     |  |
77  *                      |   +---------------+  |  |
78  *                      |    \ Matrix  Mux /   |  |
79  *                      |     +-----+-----+    |  |
80  *                      |           |          |  |
81  *                      |           |18chn     |  |
82  *                      |           |          |  |
83  *                      |           |     10chn|  |
84  *                      |           v          |  |
85  *                      |     +------------+   |  |
86  *                      |     | Mixer      |   |  |
87  *                      |     |     Matrix |   |  |
88  *                      |     |            |   |  |
89  *                      |     | 18x10 Gain |   |  |
90  *                      |     |   stages   |   |  |
91  *                      |     +-----+------+   |  |
92  *                      |           |          |  |
93  *                      |18chn      |10chn     |  |20chn
94  *                      |           |          |  |
95  *                      |           +----------/  |
96  *                      |           |             |
97  *                      v           v             v
98  *                      ===========================
99  *               +---------------+       +--—------------+
100  *                \ Output  Mux /         \ Capture Mux /
101  *                 +---+---+---+           +-----+-----+
102  *                     |   |                     |
103  *                10chn|   |                     |18chn
104  *                     |   |                     |
105  *  /--------------\   |   |                     |   /--------------\
106  *  | S/PDIF, ADAT |<--/   |10chn                \-->| ALSA PCM in  |
107  *  | Hardware out |       |                         \--------------/
108  *  \--------------/       |
109  *                         v
110  *                  +-------------+    Software gain per channel.
111  *                  | Master Gain |<-- 18i20 only: Switch per channel
112  *                  +------+------+    to select HW or SW gain control.
113  *                         |
114  *                         |10chn
115  *  /--------------\       |
116  *  | Analogue     |<------/
117  *  | Hardware out |
118  *  \--------------/
119  * </ditaa>
120  *
121  * Gen 3 devices have a Mass Storage Device (MSD) mode where a small
122  * disk with registration and driver download information is presented
123  * to the host. To access the full functionality of the device without
124  * proprietary software, MSD mode can be disabled by:
125  * - holding down the 48V button for five seconds while powering on
126  *   the device, or
127  * - using this driver and alsamixer to change the "MSD Mode" setting
128  *   to Off and power-cycling the device
129  */
130
131 #include <linux/slab.h>
132 #include <linux/usb.h>
133 #include <linux/moduleparam.h>
134
135 #include <sound/control.h>
136 #include <sound/tlv.h>
137
138 #include "usbaudio.h"
139 #include "mixer.h"
140 #include "helper.h"
141
142 #include "mixer_scarlett_gen2.h"
143
144 /* device_setup value to enable */
145 #define SCARLETT2_ENABLE 0x01
146
147 /* device_setup value to allow turning MSD mode back on */
148 #define SCARLETT2_MSD_ENABLE 0x02
149
150 /* some gui mixers can't handle negative ctl values */
151 #define SCARLETT2_VOLUME_BIAS 127
152
153 /* mixer range from -80dB to +6dB in 0.5dB steps */
154 #define SCARLETT2_MIXER_MIN_DB -80
155 #define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
156 #define SCARLETT2_MIXER_MAX_DB 6
157 #define SCARLETT2_MIXER_MAX_VALUE \
158         ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
159 #define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1)
160
161 /* map from (dB + 80) * 2 to mixer value
162  * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
163  */
164 static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = {
165         0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
166         2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
167         9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
168         23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
169         54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
170         122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
171         244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
172         487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
173         973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
174         1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
175         3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
176         5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
177         9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
178         16345
179 };
180
181 /* Maximum number of analogue outputs */
182 #define SCARLETT2_ANALOGUE_MAX 10
183
184 /* Maximum number of level and pad switches */
185 #define SCARLETT2_LEVEL_SWITCH_MAX 2
186 #define SCARLETT2_PAD_SWITCH_MAX 8
187 #define SCARLETT2_AIR_SWITCH_MAX 8
188 #define SCARLETT2_PHANTOM_SWITCH_MAX 2
189
190 /* Maximum number of inputs to the mixer */
191 #define SCARLETT2_INPUT_MIX_MAX 25
192
193 /* Maximum number of outputs from the mixer */
194 #define SCARLETT2_OUTPUT_MIX_MAX 12
195
196 /* Maximum size of the data in the USB mux assignment message:
197  * 20 inputs, 20 outputs, 25 matrix inputs, 12 spare
198  */
199 #define SCARLETT2_MUX_MAX 77
200
201 /* Maximum number of meters (sum of output port counts) */
202 #define SCARLETT2_MAX_METERS 65
203
204 /* There are three different sets of configuration parameters across
205  * the devices
206  */
207 enum {
208         SCARLETT2_CONFIG_SET_NO_MIXER = 0,
209         SCARLETT2_CONFIG_SET_GEN_2 = 1,
210         SCARLETT2_CONFIG_SET_GEN_3 = 2,
211         SCARLETT2_CONFIG_SET_CLARETT = 3,
212         SCARLETT2_CONFIG_SET_COUNT = 4
213 };
214
215 /* Hardware port types:
216  * - None (no input to mux)
217  * - Analogue I/O
218  * - S/PDIF I/O
219  * - ADAT I/O
220  * - Mixer I/O
221  * - PCM I/O
222  */
223 enum {
224         SCARLETT2_PORT_TYPE_NONE     = 0,
225         SCARLETT2_PORT_TYPE_ANALOGUE = 1,
226         SCARLETT2_PORT_TYPE_SPDIF    = 2,
227         SCARLETT2_PORT_TYPE_ADAT     = 3,
228         SCARLETT2_PORT_TYPE_MIX      = 4,
229         SCARLETT2_PORT_TYPE_PCM      = 5,
230         SCARLETT2_PORT_TYPE_COUNT    = 6,
231 };
232
233 /* I/O count of each port type kept in struct scarlett2_ports */
234 enum {
235         SCARLETT2_PORT_IN    = 0,
236         SCARLETT2_PORT_OUT   = 1,
237         SCARLETT2_PORT_DIRNS = 2,
238 };
239
240 /* Dim/Mute buttons on the 18i20 */
241 enum {
242         SCARLETT2_BUTTON_MUTE    = 0,
243         SCARLETT2_BUTTON_DIM     = 1,
244         SCARLETT2_DIM_MUTE_COUNT = 2,
245 };
246
247 static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
248         "Mute Playback Switch", "Dim Playback Switch"
249 };
250
251 /* Description of each hardware port type:
252  * - id: hardware ID of this port type
253  * - src_descr: printf format string for mux input selections
254  * - src_num_offset: added to channel number for the fprintf
255  * - dst_descr: printf format string for mixer controls
256  */
257 struct scarlett2_port {
258         u16 id;
259         const char * const src_descr;
260         int src_num_offset;
261         const char * const dst_descr;
262 };
263
264 static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
265         [SCARLETT2_PORT_TYPE_NONE] = {
266                 .id = 0x000,
267                 .src_descr = "Off"
268         },
269         [SCARLETT2_PORT_TYPE_ANALOGUE] = {
270                 .id = 0x080,
271                 .src_descr = "Analogue %d",
272                 .src_num_offset = 1,
273                 .dst_descr = "Analogue Output %02d Playback"
274         },
275         [SCARLETT2_PORT_TYPE_SPDIF] = {
276                 .id = 0x180,
277                 .src_descr = "S/PDIF %d",
278                 .src_num_offset = 1,
279                 .dst_descr = "S/PDIF Output %d Playback"
280         },
281         [SCARLETT2_PORT_TYPE_ADAT] = {
282                 .id = 0x200,
283                 .src_descr = "ADAT %d",
284                 .src_num_offset = 1,
285                 .dst_descr = "ADAT Output %d Playback"
286         },
287         [SCARLETT2_PORT_TYPE_MIX] = {
288                 .id = 0x300,
289                 .src_descr = "Mix %c",
290                 .src_num_offset = 'A',
291                 .dst_descr = "Mixer Input %02d Capture"
292         },
293         [SCARLETT2_PORT_TYPE_PCM] = {
294                 .id = 0x600,
295                 .src_descr = "PCM %d",
296                 .src_num_offset = 1,
297                 .dst_descr = "PCM %02d Capture"
298         },
299 };
300
301 /* Number of mux tables: one for each band of sample rates
302  * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
303  */
304 #define SCARLETT2_MUX_TABLES 3
305
306 /* Maximum number of entries in a mux table */
307 #define SCARLETT2_MAX_MUX_ENTRIES 10
308
309 /* One entry within mux_assignment defines the port type and range of
310  * ports to add to the set_mux message. The end of the list is marked
311  * with count == 0.
312  */
313 struct scarlett2_mux_entry {
314         u8 port_type;
315         u8 start;
316         u8 count;
317 };
318
319 struct scarlett2_device_info {
320         u32 usb_id; /* USB device identifier */
321
322         /* Gen 3 devices have an internal MSD mode switch that needs
323          * to be disabled in order to access the full functionality of
324          * the device.
325          */
326         u8 has_msd_mode;
327
328         /* which set of configuration parameters the device uses */
329         u8 config_set;
330
331         /* line out hw volume is sw controlled */
332         u8 line_out_hw_vol;
333
334         /* support for main/alt speaker switching */
335         u8 has_speaker_switching;
336
337         /* support for talkback microphone */
338         u8 has_talkback;
339
340         /* the number of analogue inputs with a software switchable
341          * level control that can be set to line or instrument
342          */
343         u8 level_input_count;
344
345         /* the first input with a level control (0-based) */
346         u8 level_input_first;
347
348         /* the number of analogue inputs with a software switchable
349          * 10dB pad control
350          */
351         u8 pad_input_count;
352
353         /* the number of analogue inputs with a software switchable
354          * "air" control
355          */
356         u8 air_input_count;
357
358         /* the number of phantom (48V) software switchable controls */
359         u8 phantom_count;
360
361         /* the number of inputs each phantom switch controls */
362         u8 inputs_per_phantom;
363
364         /* the number of direct monitor options
365          * (0 = none, 1 = mono only, 2 = mono/stereo)
366          */
367         u8 direct_monitor;
368
369         /* remap analogue outputs; 18i8 Gen 3 has "line 3/4" connected
370          * internally to the analogue 7/8 outputs
371          */
372         u8 line_out_remap_enable;
373         u8 line_out_remap[SCARLETT2_ANALOGUE_MAX];
374
375         /* additional description for the line out volume controls */
376         const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
377
378         /* number of sources/destinations of each port type */
379         const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];
380
381         /* layout/order of the entries in the set_mux message */
382         struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES]
383                                                  [SCARLETT2_MAX_MUX_ENTRIES];
384 };
385
386 struct scarlett2_data {
387         struct usb_mixer_interface *mixer;
388         struct mutex usb_mutex; /* prevent sending concurrent USB requests */
389         struct mutex data_mutex; /* lock access to this data */
390         struct delayed_work work;
391         const struct scarlett2_device_info *info;
392         __u8 bInterfaceNumber;
393         __u8 bEndpointAddress;
394         __u16 wMaxPacketSize;
395         __u8 bInterval;
396         int num_mux_srcs;
397         int num_mux_dsts;
398         u16 scarlett2_seq;
399         u8 sync_updated;
400         u8 vol_updated;
401         u8 input_other_updated;
402         u8 monitor_other_updated;
403         u8 mux_updated;
404         u8 speaker_switching_switched;
405         u8 sync;
406         u8 master_vol;
407         u8 vol[SCARLETT2_ANALOGUE_MAX];
408         u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
409         u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
410         u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
411         u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
412         u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
413         u8 air_switch[SCARLETT2_AIR_SWITCH_MAX];
414         u8 phantom_switch[SCARLETT2_PHANTOM_SWITCH_MAX];
415         u8 phantom_persistence;
416         u8 direct_monitor_switch;
417         u8 speaker_switching_switch;
418         u8 talkback_switch;
419         u8 talkback_map[SCARLETT2_OUTPUT_MIX_MAX];
420         u8 msd_switch;
421         u8 standalone_switch;
422         struct snd_kcontrol *sync_ctl;
423         struct snd_kcontrol *master_vol_ctl;
424         struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
425         struct snd_kcontrol *sw_hw_ctls[SCARLETT2_ANALOGUE_MAX];
426         struct snd_kcontrol *mute_ctls[SCARLETT2_ANALOGUE_MAX];
427         struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT];
428         struct snd_kcontrol *level_ctls[SCARLETT2_LEVEL_SWITCH_MAX];
429         struct snd_kcontrol *pad_ctls[SCARLETT2_PAD_SWITCH_MAX];
430         struct snd_kcontrol *air_ctls[SCARLETT2_AIR_SWITCH_MAX];
431         struct snd_kcontrol *phantom_ctls[SCARLETT2_PHANTOM_SWITCH_MAX];
432         struct snd_kcontrol *mux_ctls[SCARLETT2_MUX_MAX];
433         struct snd_kcontrol *direct_monitor_ctl;
434         struct snd_kcontrol *speaker_switching_ctl;
435         struct snd_kcontrol *talkback_ctl;
436         u8 mux[SCARLETT2_MUX_MAX];
437         u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX];
438 };
439
440 /*** Model-specific data ***/
441
442 static const struct scarlett2_device_info s6i6_gen2_info = {
443         .usb_id = USB_ID(0x1235, 0x8203),
444
445         .config_set = SCARLETT2_CONFIG_SET_GEN_2,
446         .level_input_count = 2,
447         .pad_input_count = 2,
448
449         .line_out_descrs = {
450                 "Headphones 1 L",
451                 "Headphones 1 R",
452                 "Headphones 2 L",
453                 "Headphones 2 R",
454         },
455
456         .port_count = {
457                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
458                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  4,  4 },
459                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
460                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
461                 [SCARLETT2_PORT_TYPE_PCM]      = {  6,  6 },
462         },
463
464         .mux_assignment = { {
465                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
466                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
467                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
468                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
469                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
470                 { 0,                            0,  0 },
471         }, {
472                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
473                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
474                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
475                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
476                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
477                 { 0,                            0,  0 },
478         }, {
479                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
480                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
481                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
482                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
483                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
484                 { 0,                            0,  0 },
485         } },
486 };
487
488 static const struct scarlett2_device_info s18i8_gen2_info = {
489         .usb_id = USB_ID(0x1235, 0x8204),
490
491         .config_set = SCARLETT2_CONFIG_SET_GEN_2,
492         .level_input_count = 2,
493         .pad_input_count = 4,
494
495         .line_out_descrs = {
496                 "Monitor L",
497                 "Monitor R",
498                 "Headphones 1 L",
499                 "Headphones 1 R",
500                 "Headphones 2 L",
501                 "Headphones 2 R",
502         },
503
504         .port_count = {
505                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
506                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  8,  6 },
507                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
508                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  0 },
509                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
510                 [SCARLETT2_PORT_TYPE_PCM]      = {  8, 18 },
511         },
512
513         .mux_assignment = { {
514                 { SCARLETT2_PORT_TYPE_PCM,      0, 18 },
515                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
516                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
517                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
518                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
519                 { 0,                            0,  0 },
520         }, {
521                 { SCARLETT2_PORT_TYPE_PCM,      0, 14 },
522                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
523                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
524                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
525                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
526                 { 0,                            0,  0 },
527         }, {
528                 { SCARLETT2_PORT_TYPE_PCM,      0, 10 },
529                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
530                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
531                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
532                 { SCARLETT2_PORT_TYPE_NONE,     0,  4 },
533                 { 0,                            0,  0 },
534         } },
535 };
536
537 static const struct scarlett2_device_info s18i20_gen2_info = {
538         .usb_id = USB_ID(0x1235, 0x8201),
539
540         .config_set = SCARLETT2_CONFIG_SET_GEN_2,
541         .line_out_hw_vol = 1,
542
543         .line_out_descrs = {
544                 "Monitor L",
545                 "Monitor R",
546                 NULL,
547                 NULL,
548                 NULL,
549                 NULL,
550                 "Headphones 1 L",
551                 "Headphones 1 R",
552                 "Headphones 2 L",
553                 "Headphones 2 R",
554         },
555
556         .port_count = {
557                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
558                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  8, 10 },
559                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
560                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  8 },
561                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
562                 [SCARLETT2_PORT_TYPE_PCM]      = { 20, 18 },
563         },
564
565         .mux_assignment = { {
566                 { SCARLETT2_PORT_TYPE_PCM,      0, 18 },
567                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
568                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
569                 { SCARLETT2_PORT_TYPE_ADAT,     0,  8 },
570                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
571                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
572                 { 0,                            0,  0 },
573         }, {
574                 { SCARLETT2_PORT_TYPE_PCM,      0, 14 },
575                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
576                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
577                 { SCARLETT2_PORT_TYPE_ADAT,     0,  4 },
578                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
579                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
580                 { 0,                            0,  0 },
581         }, {
582                 { SCARLETT2_PORT_TYPE_PCM,      0, 10 },
583                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
584                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
585                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
586                 { SCARLETT2_PORT_TYPE_NONE,     0,  6 },
587                 { 0,                            0,  0 },
588         } },
589 };
590
591 static const struct scarlett2_device_info solo_gen3_info = {
592         .usb_id = USB_ID(0x1235, 0x8211),
593
594         .has_msd_mode = 1,
595         .config_set = SCARLETT2_CONFIG_SET_NO_MIXER,
596         .level_input_count = 1,
597         .level_input_first = 1,
598         .air_input_count = 1,
599         .phantom_count = 1,
600         .inputs_per_phantom = 1,
601         .direct_monitor = 1,
602 };
603
604 static const struct scarlett2_device_info s2i2_gen3_info = {
605         .usb_id = USB_ID(0x1235, 0x8210),
606
607         .has_msd_mode = 1,
608         .config_set = SCARLETT2_CONFIG_SET_NO_MIXER,
609         .level_input_count = 2,
610         .air_input_count = 2,
611         .phantom_count = 1,
612         .inputs_per_phantom = 2,
613         .direct_monitor = 2,
614 };
615
616 static const struct scarlett2_device_info s4i4_gen3_info = {
617         .usb_id = USB_ID(0x1235, 0x8212),
618
619         .has_msd_mode = 1,
620         .config_set = SCARLETT2_CONFIG_SET_GEN_3,
621         .level_input_count = 2,
622         .pad_input_count = 2,
623         .air_input_count = 2,
624         .phantom_count = 1,
625         .inputs_per_phantom = 2,
626
627         .line_out_descrs = {
628                 "Monitor L",
629                 "Monitor R",
630                 "Headphones L",
631                 "Headphones R",
632         },
633
634         .port_count = {
635                 [SCARLETT2_PORT_TYPE_NONE]     = { 1, 0 },
636                 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
637                 [SCARLETT2_PORT_TYPE_MIX]      = { 6, 8 },
638                 [SCARLETT2_PORT_TYPE_PCM]      = { 4, 6 },
639         },
640
641         .mux_assignment = { {
642                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
643                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
644                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
645                 { SCARLETT2_PORT_TYPE_NONE,     0, 16 },
646                 { 0,                            0,  0 },
647         }, {
648                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
649                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
650                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
651                 { SCARLETT2_PORT_TYPE_NONE,     0, 16 },
652                 { 0,                            0,  0 },
653         }, {
654                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
655                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
656                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
657                 { SCARLETT2_PORT_TYPE_NONE,     0, 16 },
658                 { 0,                            0,  0 },
659         } },
660 };
661
662 static const struct scarlett2_device_info s8i6_gen3_info = {
663         .usb_id = USB_ID(0x1235, 0x8213),
664
665         .has_msd_mode = 1,
666         .config_set = SCARLETT2_CONFIG_SET_GEN_3,
667         .level_input_count = 2,
668         .pad_input_count = 2,
669         .air_input_count = 2,
670         .phantom_count = 1,
671         .inputs_per_phantom = 2,
672
673         .line_out_descrs = {
674                 "Headphones 1 L",
675                 "Headphones 1 R",
676                 "Headphones 2 L",
677                 "Headphones 2 R",
678         },
679
680         .port_count = {
681                 [SCARLETT2_PORT_TYPE_NONE]     = { 1,  0 },
682                 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6,  4 },
683                 [SCARLETT2_PORT_TYPE_SPDIF]    = { 2,  2 },
684                 [SCARLETT2_PORT_TYPE_MIX]      = { 8,  8 },
685                 [SCARLETT2_PORT_TYPE_PCM]      = { 6, 10 },
686         },
687
688         .mux_assignment = { {
689                 { SCARLETT2_PORT_TYPE_PCM,      0,  8 },
690                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
691                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
692                 { SCARLETT2_PORT_TYPE_PCM,      8,  2 },
693                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
694                 { SCARLETT2_PORT_TYPE_NONE,     0, 18 },
695                 { 0,                            0,  0 },
696         }, {
697                 { SCARLETT2_PORT_TYPE_PCM,      0,  8 },
698                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
699                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
700                 { SCARLETT2_PORT_TYPE_PCM,      8,  2 },
701                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
702                 { SCARLETT2_PORT_TYPE_NONE,     0, 18 },
703                 { 0,                            0,  0 },
704         }, {
705                 { SCARLETT2_PORT_TYPE_PCM,      0,  8 },
706                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
707                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
708                 { SCARLETT2_PORT_TYPE_PCM,      8,  2 },
709                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
710                 { SCARLETT2_PORT_TYPE_NONE,     0, 18 },
711                 { 0,                            0,  0 },
712         } },
713 };
714
715 static const struct scarlett2_device_info s18i8_gen3_info = {
716         .usb_id = USB_ID(0x1235, 0x8214),
717
718         .has_msd_mode = 1,
719         .config_set = SCARLETT2_CONFIG_SET_GEN_3,
720         .line_out_hw_vol = 1,
721         .has_speaker_switching = 1,
722         .level_input_count = 2,
723         .pad_input_count = 4,
724         .air_input_count = 4,
725         .phantom_count = 2,
726         .inputs_per_phantom = 2,
727
728         .line_out_remap_enable = 1,
729         .line_out_remap = { 0, 1, 6, 7, 2, 3, 4, 5 },
730
731         .line_out_descrs = {
732                 "Monitor L",
733                 "Monitor R",
734                 "Alt Monitor L",
735                 "Alt Monitor R",
736                 "Headphones 1 L",
737                 "Headphones 1 R",
738                 "Headphones 2 L",
739                 "Headphones 2 R",
740         },
741
742         .port_count = {
743                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
744                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  8,  8 },
745                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
746                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  0 },
747                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 20 },
748                 [SCARLETT2_PORT_TYPE_PCM]      = {  8, 20 },
749         },
750
751         .mux_assignment = { {
752                 { SCARLETT2_PORT_TYPE_PCM,       0, 10 },
753                 { SCARLETT2_PORT_TYPE_PCM,      12,  8 },
754                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
755                 { SCARLETT2_PORT_TYPE_ANALOGUE,  6,  2 },
756                 { SCARLETT2_PORT_TYPE_ANALOGUE,  2,  4 },
757                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
758                 { SCARLETT2_PORT_TYPE_PCM,      10,  2 },
759                 { SCARLETT2_PORT_TYPE_MIX,       0, 20 },
760                 { SCARLETT2_PORT_TYPE_NONE,      0, 10 },
761                 { 0,                             0,  0 },
762         }, {
763                 { SCARLETT2_PORT_TYPE_PCM,       0, 10 },
764                 { SCARLETT2_PORT_TYPE_PCM,      12,  4 },
765                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
766                 { SCARLETT2_PORT_TYPE_ANALOGUE,  6,  2 },
767                 { SCARLETT2_PORT_TYPE_ANALOGUE,  2,  4 },
768                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
769                 { SCARLETT2_PORT_TYPE_PCM,      10,  2 },
770                 { SCARLETT2_PORT_TYPE_MIX,       0, 20 },
771                 { SCARLETT2_PORT_TYPE_NONE,      0, 10 },
772                 { 0,                             0,  0 },
773         }, {
774                 { SCARLETT2_PORT_TYPE_PCM,       0, 10 },
775                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
776                 { SCARLETT2_PORT_TYPE_ANALOGUE,  6,  2 },
777                 { SCARLETT2_PORT_TYPE_ANALOGUE,  2,  4 },
778                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
779                 { SCARLETT2_PORT_TYPE_MIX,       0, 20 },
780                 { SCARLETT2_PORT_TYPE_NONE,      0, 10 },
781                 { 0,                             0,  0 },
782         } },
783 };
784
785 static const struct scarlett2_device_info s18i20_gen3_info = {
786         .usb_id = USB_ID(0x1235, 0x8215),
787
788         .has_msd_mode = 1,
789         .config_set = SCARLETT2_CONFIG_SET_GEN_3,
790         .line_out_hw_vol = 1,
791         .has_speaker_switching = 1,
792         .has_talkback = 1,
793         .level_input_count = 2,
794         .pad_input_count = 8,
795         .air_input_count = 8,
796         .phantom_count = 2,
797         .inputs_per_phantom = 4,
798
799         .line_out_descrs = {
800                 "Monitor 1 L",
801                 "Monitor 1 R",
802                 "Monitor 2 L",
803                 "Monitor 2 R",
804                 NULL,
805                 NULL,
806                 "Headphones 1 L",
807                 "Headphones 1 R",
808                 "Headphones 2 L",
809                 "Headphones 2 R",
810         },
811
812         .port_count = {
813                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
814                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  9, 10 },
815                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
816                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  8 },
817                 [SCARLETT2_PORT_TYPE_MIX]      = { 12, 25 },
818                 [SCARLETT2_PORT_TYPE_PCM]      = { 20, 20 },
819         },
820
821         .mux_assignment = { {
822                 { SCARLETT2_PORT_TYPE_PCM,       0,  8 },
823                 { SCARLETT2_PORT_TYPE_PCM,      10, 10 },
824                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0, 10 },
825                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
826                 { SCARLETT2_PORT_TYPE_ADAT,      0,  8 },
827                 { SCARLETT2_PORT_TYPE_PCM,       8,  2 },
828                 { SCARLETT2_PORT_TYPE_MIX,       0, 25 },
829                 { SCARLETT2_PORT_TYPE_NONE,      0, 12 },
830                 { 0,                             0,  0 },
831         }, {
832                 { SCARLETT2_PORT_TYPE_PCM,       0,  8 },
833                 { SCARLETT2_PORT_TYPE_PCM,      10,  8 },
834                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0, 10 },
835                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
836                 { SCARLETT2_PORT_TYPE_ADAT,      0,  8 },
837                 { SCARLETT2_PORT_TYPE_PCM,       8,  2 },
838                 { SCARLETT2_PORT_TYPE_MIX,       0, 25 },
839                 { SCARLETT2_PORT_TYPE_NONE,      0, 10 },
840                 { 0,                             0,  0 },
841         }, {
842                 { SCARLETT2_PORT_TYPE_PCM,       0, 10 },
843                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0, 10 },
844                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
845                 { SCARLETT2_PORT_TYPE_NONE,      0, 24 },
846                 { 0,                             0,  0 },
847         } },
848 };
849
850 static const struct scarlett2_device_info clarett_8pre_info = {
851         .usb_id = USB_ID(0x1235, 0x820c),
852
853         .config_set = SCARLETT2_CONFIG_SET_CLARETT,
854         .line_out_hw_vol = 1,
855         .level_input_count = 2,
856         .air_input_count = 8,
857
858         .line_out_descrs = {
859                 "Monitor L",
860                 "Monitor R",
861                 NULL,
862                 NULL,
863                 NULL,
864                 NULL,
865                 "Headphones 1 L",
866                 "Headphones 1 R",
867                 "Headphones 2 L",
868                 "Headphones 2 R",
869         },
870
871         .port_count = {
872                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
873                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  8, 10 },
874                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
875                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  8 },
876                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
877                 [SCARLETT2_PORT_TYPE_PCM]      = { 20, 18 },
878         },
879
880         .mux_assignment = { {
881                 { SCARLETT2_PORT_TYPE_PCM,      0, 18 },
882                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
883                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
884                 { SCARLETT2_PORT_TYPE_ADAT,     0,  8 },
885                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
886                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
887                 { 0,                            0,  0 },
888         }, {
889                 { SCARLETT2_PORT_TYPE_PCM,      0, 14 },
890                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
891                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
892                 { SCARLETT2_PORT_TYPE_ADAT,     0,  4 },
893                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
894                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
895                 { 0,                            0,  0 },
896         }, {
897                 { SCARLETT2_PORT_TYPE_PCM,      0, 12 },
898                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
899                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
900                 { SCARLETT2_PORT_TYPE_NONE,     0, 22 },
901                 { 0,                            0,  0 },
902         } },
903 };
904
905 static const struct scarlett2_device_info *scarlett2_devices[] = {
906         /* Supported Gen 2 devices */
907         &s6i6_gen2_info,
908         &s18i8_gen2_info,
909         &s18i20_gen2_info,
910
911         /* Supported Gen 3 devices */
912         &solo_gen3_info,
913         &s2i2_gen3_info,
914         &s4i4_gen3_info,
915         &s8i6_gen3_info,
916         &s18i8_gen3_info,
917         &s18i20_gen3_info,
918
919         /* Supported Clarett+ devices */
920         &clarett_8pre_info,
921
922         /* End of list */
923         NULL
924 };
925
926 /* get the starting port index number for a given port type/direction */
927 static int scarlett2_get_port_start_num(
928         const int port_count[][SCARLETT2_PORT_DIRNS],
929         int direction, int port_type)
930 {
931         int i, num = 0;
932
933         for (i = 0; i < port_type; i++)
934                 num += port_count[i][direction];
935
936         return num;
937 }
938
939 /*** USB Interactions ***/
940
941 /* Notifications from the interface */
942 #define SCARLETT2_USB_NOTIFY_SYNC          0x00000008
943 #define SCARLETT2_USB_NOTIFY_DIM_MUTE      0x00200000
944 #define SCARLETT2_USB_NOTIFY_MONITOR       0x00400000
945 #define SCARLETT2_USB_NOTIFY_INPUT_OTHER   0x00800000
946 #define SCARLETT2_USB_NOTIFY_MONITOR_OTHER 0x01000000
947
948 /* Commands for sending/receiving requests/responses */
949 #define SCARLETT2_USB_CMD_INIT 0
950 #define SCARLETT2_USB_CMD_REQ  2
951 #define SCARLETT2_USB_CMD_RESP 3
952
953 #define SCARLETT2_USB_INIT_1    0x00000000
954 #define SCARLETT2_USB_INIT_2    0x00000002
955 #define SCARLETT2_USB_GET_METER 0x00001001
956 #define SCARLETT2_USB_GET_MIX   0x00002001
957 #define SCARLETT2_USB_SET_MIX   0x00002002
958 #define SCARLETT2_USB_GET_MUX   0x00003001
959 #define SCARLETT2_USB_SET_MUX   0x00003002
960 #define SCARLETT2_USB_GET_SYNC  0x00006004
961 #define SCARLETT2_USB_GET_DATA  0x00800000
962 #define SCARLETT2_USB_SET_DATA  0x00800001
963 #define SCARLETT2_USB_DATA_CMD  0x00800002
964
965 #define SCARLETT2_USB_CONFIG_SAVE 6
966
967 #define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31
968 #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
969
970 /* volume status is read together (matches scarlett2_config_items[1]) */
971 struct scarlett2_usb_volume_status {
972         /* dim/mute buttons */
973         u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
974
975         u8 pad1;
976
977         /* software volume setting */
978         s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
979
980         /* actual volume of output inc. dim (-18dB) */
981         s16 hw_vol[SCARLETT2_ANALOGUE_MAX];
982
983         /* internal mute buttons */
984         u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
985
986         /* sw (0) or hw (1) controlled */
987         u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
988
989         u8 pad3[6];
990
991         /* front panel volume knob */
992         s16 master_vol;
993 } __packed;
994
995 /* Configuration parameters that can be read and written */
996 enum {
997         SCARLETT2_CONFIG_DIM_MUTE = 0,
998         SCARLETT2_CONFIG_LINE_OUT_VOLUME = 1,
999         SCARLETT2_CONFIG_MUTE_SWITCH = 2,
1000         SCARLETT2_CONFIG_SW_HW_SWITCH = 3,
1001         SCARLETT2_CONFIG_LEVEL_SWITCH = 4,
1002         SCARLETT2_CONFIG_PAD_SWITCH = 5,
1003         SCARLETT2_CONFIG_MSD_SWITCH = 6,
1004         SCARLETT2_CONFIG_AIR_SWITCH = 7,
1005         SCARLETT2_CONFIG_STANDALONE_SWITCH = 8,
1006         SCARLETT2_CONFIG_PHANTOM_SWITCH = 9,
1007         SCARLETT2_CONFIG_PHANTOM_PERSISTENCE = 10,
1008         SCARLETT2_CONFIG_DIRECT_MONITOR = 11,
1009         SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH = 12,
1010         SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE = 13,
1011         SCARLETT2_CONFIG_TALKBACK_MAP = 14,
1012         SCARLETT2_CONFIG_COUNT = 15
1013 };
1014
1015 /* Location, size, and activation command number for the configuration
1016  * parameters. Size is in bits and may be 1, 8, or 16.
1017  */
1018 struct scarlett2_config {
1019         u8 offset;
1020         u8 size;
1021         u8 activate;
1022 };
1023
1024 static const struct scarlett2_config
1025         scarlett2_config_items[SCARLETT2_CONFIG_SET_COUNT]
1026                               [SCARLETT2_CONFIG_COUNT] =
1027
1028 /* Devices without a mixer (Gen 3 Solo and 2i2) */
1029 { {
1030         [SCARLETT2_CONFIG_MSD_SWITCH] = {
1031                 .offset = 0x04, .size = 8, .activate = 6 },
1032
1033         [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
1034                 .offset = 0x05, .size = 8, .activate = 6 },
1035
1036         [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
1037                 .offset = 0x06, .size = 8, .activate = 3 },
1038
1039         [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
1040                 .offset = 0x07, .size = 8, .activate = 4 },
1041
1042         [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
1043                 .offset = 0x08, .size = 1, .activate = 7 },
1044
1045         [SCARLETT2_CONFIG_AIR_SWITCH] = {
1046                 .offset = 0x09, .size = 1, .activate = 8 },
1047
1048 /* Gen 2 devices: 6i6, 18i8, 18i20 */
1049 }, {
1050         [SCARLETT2_CONFIG_DIM_MUTE] = {
1051                 .offset = 0x31, .size = 8, .activate = 2 },
1052
1053         [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
1054                 .offset = 0x34, .size = 16, .activate = 1 },
1055
1056         [SCARLETT2_CONFIG_MUTE_SWITCH] = {
1057                 .offset = 0x5c, .size = 8, .activate = 1 },
1058
1059         [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
1060                 .offset = 0x66, .size = 8, .activate = 3 },
1061
1062         [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
1063                 .offset = 0x7c, .size = 8, .activate = 7 },
1064
1065         [SCARLETT2_CONFIG_PAD_SWITCH] = {
1066                 .offset = 0x84, .size = 8, .activate = 8 },
1067
1068         [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
1069                 .offset = 0x8d, .size = 8, .activate = 6 },
1070
1071 /* Gen 3 devices: 4i4, 8i6, 18i8, 18i20 */
1072 }, {
1073         [SCARLETT2_CONFIG_DIM_MUTE] = {
1074                 .offset = 0x31, .size = 8, .activate = 2 },
1075
1076         [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
1077                 .offset = 0x34, .size = 16, .activate = 1 },
1078
1079         [SCARLETT2_CONFIG_MUTE_SWITCH] = {
1080                 .offset = 0x5c, .size = 8, .activate = 1 },
1081
1082         [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
1083                 .offset = 0x66, .size = 8, .activate = 3 },
1084
1085         [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
1086                 .offset = 0x7c, .size = 8, .activate = 7 },
1087
1088         [SCARLETT2_CONFIG_PAD_SWITCH] = {
1089                 .offset = 0x84, .size = 8, .activate = 8 },
1090
1091         [SCARLETT2_CONFIG_AIR_SWITCH] = {
1092                 .offset = 0x8c, .size = 8, .activate = 8 },
1093
1094         [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
1095                 .offset = 0x95, .size = 8, .activate = 6 },
1096
1097         [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
1098                 .offset = 0x9c, .size = 1, .activate = 8 },
1099
1100         [SCARLETT2_CONFIG_MSD_SWITCH] = {
1101                 .offset = 0x9d, .size = 8, .activate = 6 },
1102
1103         [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
1104                 .offset = 0x9e, .size = 8, .activate = 6 },
1105
1106         [SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH] = {
1107                 .offset = 0x9f, .size = 1, .activate = 10 },
1108
1109         [SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE] = {
1110                 .offset = 0xa0, .size = 1, .activate = 10 },
1111
1112         [SCARLETT2_CONFIG_TALKBACK_MAP] = {
1113                 .offset = 0xb0, .size = 16, .activate = 10 },
1114
1115 /* Clarett+ 8Pre */
1116 }, {
1117         [SCARLETT2_CONFIG_DIM_MUTE] = {
1118                 .offset = 0x31, .size = 8, .activate = 2 },
1119
1120         [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
1121                 .offset = 0x34, .size = 16, .activate = 1 },
1122
1123         [SCARLETT2_CONFIG_MUTE_SWITCH] = {
1124                 .offset = 0x5c, .size = 8, .activate = 1 },
1125
1126         [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
1127                 .offset = 0x66, .size = 8, .activate = 3 },
1128
1129         [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
1130                 .offset = 0x7c, .size = 8, .activate = 7 },
1131
1132         [SCARLETT2_CONFIG_AIR_SWITCH] = {
1133                 .offset = 0x95, .size = 8, .activate = 8 },
1134
1135         [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
1136                 .offset = 0x8d, .size = 8, .activate = 6 },
1137 } };
1138
1139 /* proprietary request/response format */
1140 struct scarlett2_usb_packet {
1141         __le32 cmd;
1142         __le16 size;
1143         __le16 seq;
1144         __le32 error;
1145         __le32 pad;
1146         u8 data[];
1147 };
1148
1149 static void scarlett2_fill_request_header(struct scarlett2_data *private,
1150                                           struct scarlett2_usb_packet *req,
1151                                           u32 cmd, u16 req_size)
1152 {
1153         /* sequence must go up by 1 for each request */
1154         u16 seq = private->scarlett2_seq++;
1155
1156         req->cmd = cpu_to_le32(cmd);
1157         req->size = cpu_to_le16(req_size);
1158         req->seq = cpu_to_le16(seq);
1159         req->error = 0;
1160         req->pad = 0;
1161 }
1162
1163 static int scarlett2_usb_tx(struct usb_device *dev, int interface,
1164                             void *buf, u16 size)
1165 {
1166         return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1167                         SCARLETT2_USB_CMD_REQ,
1168                         USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1169                         0, interface, buf, size);
1170 }
1171
1172 static int scarlett2_usb_rx(struct usb_device *dev, int interface,
1173                             u32 usb_req, void *buf, u16 size)
1174 {
1175         return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
1176                         usb_req,
1177                         USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1178                         0, interface, buf, size);
1179 }
1180
1181 /* Send a proprietary format request to the Scarlett interface */
1182 static int scarlett2_usb(
1183         struct usb_mixer_interface *mixer, u32 cmd,
1184         void *req_data, u16 req_size, void *resp_data, u16 resp_size)
1185 {
1186         struct scarlett2_data *private = mixer->private_data;
1187         struct usb_device *dev = mixer->chip->dev;
1188         struct scarlett2_usb_packet *req, *resp = NULL;
1189         size_t req_buf_size = struct_size(req, data, req_size);
1190         size_t resp_buf_size = struct_size(resp, data, resp_size);
1191         int err;
1192
1193         req = kmalloc(req_buf_size, GFP_KERNEL);
1194         if (!req) {
1195                 err = -ENOMEM;
1196                 goto error;
1197         }
1198
1199         resp = kmalloc(resp_buf_size, GFP_KERNEL);
1200         if (!resp) {
1201                 err = -ENOMEM;
1202                 goto error;
1203         }
1204
1205         mutex_lock(&private->usb_mutex);
1206
1207         /* build request message and send it */
1208
1209         scarlett2_fill_request_header(private, req, cmd, req_size);
1210
1211         if (req_size)
1212                 memcpy(req->data, req_data, req_size);
1213
1214         err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
1215                                req, req_buf_size);
1216
1217         if (err != req_buf_size) {
1218                 usb_audio_err(
1219                         mixer->chip,
1220                         "Scarlett Gen 2/3 USB request result cmd %x was %d\n",
1221                         cmd, err);
1222                 err = -EINVAL;
1223                 goto unlock;
1224         }
1225
1226         /* send a second message to get the response */
1227
1228         err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
1229                                SCARLETT2_USB_CMD_RESP,
1230                                resp, resp_buf_size);
1231
1232         /* validate the response */
1233
1234         if (err != resp_buf_size) {
1235                 usb_audio_err(
1236                         mixer->chip,
1237                         "Scarlett Gen 2/3 USB response result cmd %x was %d "
1238                         "expected %zu\n",
1239                         cmd, err, resp_buf_size);
1240                 err = -EINVAL;
1241                 goto unlock;
1242         }
1243
1244         /* cmd/seq/size should match except when initialising
1245          * seq sent = 1, response = 0
1246          */
1247         if (resp->cmd != req->cmd ||
1248             (resp->seq != req->seq &&
1249                 (le16_to_cpu(req->seq) != 1 || resp->seq != 0)) ||
1250             resp_size != le16_to_cpu(resp->size) ||
1251             resp->error ||
1252             resp->pad) {
1253                 usb_audio_err(
1254                         mixer->chip,
1255                         "Scarlett Gen 2/3 USB invalid response; "
1256                            "cmd tx/rx %d/%d seq %d/%d size %d/%d "
1257                            "error %d pad %d\n",
1258                         le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
1259                         le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
1260                         resp_size, le16_to_cpu(resp->size),
1261                         le32_to_cpu(resp->error),
1262                         le32_to_cpu(resp->pad));
1263                 err = -EINVAL;
1264                 goto unlock;
1265         }
1266
1267         if (resp_data && resp_size > 0)
1268                 memcpy(resp_data, resp->data, resp_size);
1269
1270 unlock:
1271         mutex_unlock(&private->usb_mutex);
1272 error:
1273         kfree(req);
1274         kfree(resp);
1275         return err;
1276 }
1277
1278 /* Send a USB message to get data; result placed in *buf */
1279 static int scarlett2_usb_get(
1280         struct usb_mixer_interface *mixer,
1281         int offset, void *buf, int size)
1282 {
1283         struct {
1284                 __le32 offset;
1285                 __le32 size;
1286         } __packed req;
1287
1288         req.offset = cpu_to_le32(offset);
1289         req.size = cpu_to_le32(size);
1290         return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
1291                              &req, sizeof(req), buf, size);
1292 }
1293
1294 /* Send a USB message to get configuration parameters; result placed in *buf */
1295 static int scarlett2_usb_get_config(
1296         struct usb_mixer_interface *mixer,
1297         int config_item_num, int count, void *buf)
1298 {
1299         struct scarlett2_data *private = mixer->private_data;
1300         const struct scarlett2_device_info *info = private->info;
1301         const struct scarlett2_config *config_item =
1302                 &scarlett2_config_items[info->config_set][config_item_num];
1303         int size, err, i;
1304         u8 *buf_8;
1305         u8 value;
1306
1307         /* For byte-sized parameters, retrieve directly into buf */
1308         if (config_item->size >= 8) {
1309                 size = config_item->size / 8 * count;
1310                 err = scarlett2_usb_get(mixer, config_item->offset, buf, size);
1311                 if (err < 0)
1312                         return err;
1313                 if (size == 2) {
1314                         u16 *buf_16 = buf;
1315
1316                         for (i = 0; i < count; i++, buf_16++)
1317                                 *buf_16 = le16_to_cpu(*(__le16 *)buf_16);
1318                 }
1319                 return 0;
1320         }
1321
1322         /* For bit-sized parameters, retrieve into value */
1323         err = scarlett2_usb_get(mixer, config_item->offset, &value, 1);
1324         if (err < 0)
1325                 return err;
1326
1327         /* then unpack from value into buf[] */
1328         buf_8 = buf;
1329         for (i = 0; i < 8 && i < count; i++, value >>= 1)
1330                 *buf_8++ = value & 1;
1331
1332         return 0;
1333 }
1334
1335 /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
1336 static void scarlett2_config_save(struct usb_mixer_interface *mixer)
1337 {
1338         __le32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE);
1339
1340         int err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1341                                 &req, sizeof(u32),
1342                                 NULL, 0);
1343         if (err < 0)
1344                 usb_audio_err(mixer->chip, "config save failed: %d\n", err);
1345 }
1346
1347 /* Delayed work to save config */
1348 static void scarlett2_config_save_work(struct work_struct *work)
1349 {
1350         struct scarlett2_data *private =
1351                 container_of(work, struct scarlett2_data, work.work);
1352
1353         scarlett2_config_save(private->mixer);
1354 }
1355
1356 /* Send a USB message to set a SCARLETT2_CONFIG_* parameter */
1357 static int scarlett2_usb_set_config(
1358         struct usb_mixer_interface *mixer,
1359         int config_item_num, int index, int value)
1360 {
1361         struct scarlett2_data *private = mixer->private_data;
1362         const struct scarlett2_device_info *info = private->info;
1363         const struct scarlett2_config *config_item =
1364                &scarlett2_config_items[info->config_set][config_item_num];
1365         struct {
1366                 __le32 offset;
1367                 __le32 bytes;
1368                 __le32 value;
1369         } __packed req;
1370         __le32 req2;
1371         int offset, size;
1372         int err;
1373
1374         /* Cancel any pending NVRAM save */
1375         cancel_delayed_work_sync(&private->work);
1376
1377         /* Convert config_item->size in bits to size in bytes and
1378          * calculate offset
1379          */
1380         if (config_item->size >= 8) {
1381                 size = config_item->size / 8;
1382                 offset = config_item->offset + index * size;
1383
1384         /* If updating a bit, retrieve the old value, set/clear the
1385          * bit as needed, and update value
1386          */
1387         } else {
1388                 u8 tmp;
1389
1390                 size = 1;
1391                 offset = config_item->offset;
1392
1393                 scarlett2_usb_get(mixer, offset, &tmp, 1);
1394                 if (value)
1395                         tmp |= (1 << index);
1396                 else
1397                         tmp &= ~(1 << index);
1398
1399                 value = tmp;
1400         }
1401
1402         /* Send the configuration parameter data */
1403         req.offset = cpu_to_le32(offset);
1404         req.bytes = cpu_to_le32(size);
1405         req.value = cpu_to_le32(value);
1406         err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA,
1407                             &req, sizeof(u32) * 2 + size,
1408                             NULL, 0);
1409         if (err < 0)
1410                 return err;
1411
1412         /* Activate the change */
1413         req2 = cpu_to_le32(config_item->activate);
1414         err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1415                             &req2, sizeof(req2), NULL, 0);
1416         if (err < 0)
1417                 return err;
1418
1419         /* Schedule the change to be written to NVRAM */
1420         if (config_item->activate != SCARLETT2_USB_CONFIG_SAVE)
1421                 schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
1422
1423         return 0;
1424 }
1425
1426 /* Send a USB message to get sync status; result placed in *sync */
1427 static int scarlett2_usb_get_sync_status(
1428         struct usb_mixer_interface *mixer,
1429         u8 *sync)
1430 {
1431         __le32 data;
1432         int err;
1433
1434         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
1435                             NULL, 0, &data, sizeof(data));
1436         if (err < 0)
1437                 return err;
1438
1439         *sync = !!data;
1440         return 0;
1441 }
1442
1443 /* Send a USB message to get volume status; result placed in *buf */
1444 static int scarlett2_usb_get_volume_status(
1445         struct usb_mixer_interface *mixer,
1446         struct scarlett2_usb_volume_status *buf)
1447 {
1448         return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET,
1449                                  buf, sizeof(*buf));
1450 }
1451
1452 /* Send a USB message to get the volumes for all inputs of one mix
1453  * and put the values into private->mix[]
1454  */
1455 static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
1456                                  int mix_num)
1457 {
1458         struct scarlett2_data *private = mixer->private_data;
1459         const struct scarlett2_device_info *info = private->info;
1460
1461         int num_mixer_in =
1462                 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1463         int err, i, j, k;
1464
1465         struct {
1466                 __le16 mix_num;
1467                 __le16 count;
1468         } __packed req;
1469
1470         __le16 data[SCARLETT2_INPUT_MIX_MAX];
1471
1472         req.mix_num = cpu_to_le16(mix_num);
1473         req.count = cpu_to_le16(num_mixer_in);
1474
1475         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
1476                             &req, sizeof(req),
1477                             data, num_mixer_in * sizeof(u16));
1478         if (err < 0)
1479                 return err;
1480
1481         for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++) {
1482                 u16 mixer_value = le16_to_cpu(data[i]);
1483
1484                 for (k = 0; k < SCARLETT2_MIXER_VALUE_COUNT; k++)
1485                         if (scarlett2_mixer_values[k] >= mixer_value)
1486                                 break;
1487                 if (k == SCARLETT2_MIXER_VALUE_COUNT)
1488                         k = SCARLETT2_MIXER_MAX_VALUE;
1489                 private->mix[j] = k;
1490         }
1491
1492         return 0;
1493 }
1494
1495 /* Send a USB message to set the volumes for all inputs of one mix
1496  * (values obtained from private->mix[])
1497  */
1498 static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
1499                                  int mix_num)
1500 {
1501         struct scarlett2_data *private = mixer->private_data;
1502         const struct scarlett2_device_info *info = private->info;
1503
1504         struct {
1505                 __le16 mix_num;
1506                 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1507         } __packed req;
1508
1509         int i, j;
1510         int num_mixer_in =
1511                 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1512
1513         req.mix_num = cpu_to_le16(mix_num);
1514
1515         for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
1516                 req.data[i] = cpu_to_le16(
1517                         scarlett2_mixer_values[private->mix[j]]
1518                 );
1519
1520         return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
1521                              &req, (num_mixer_in + 1) * sizeof(u16),
1522                              NULL, 0);
1523 }
1524
1525 /* Convert a port number index (per info->port_count) to a hardware ID */
1526 static u32 scarlett2_mux_src_num_to_id(
1527         const int port_count[][SCARLETT2_PORT_DIRNS], int num)
1528 {
1529         int port_type;
1530
1531         for (port_type = 0;
1532              port_type < SCARLETT2_PORT_TYPE_COUNT;
1533              port_type++) {
1534                 if (num < port_count[port_type][SCARLETT2_PORT_IN])
1535                         return scarlett2_ports[port_type].id | num;
1536                 num -= port_count[port_type][SCARLETT2_PORT_IN];
1537         }
1538
1539         /* Oops */
1540         return 0;
1541 }
1542
1543 /* Convert a hardware ID to a port number index */
1544 static u32 scarlett2_mux_id_to_num(
1545         const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
1546 {
1547         int port_type;
1548         int port_num = 0;
1549
1550         for (port_type = 0;
1551              port_type < SCARLETT2_PORT_TYPE_COUNT;
1552              port_type++) {
1553                 int base = scarlett2_ports[port_type].id;
1554                 int count = port_count[port_type][direction];
1555
1556                 if (id >= base && id < base + count)
1557                         return port_num + id - base;
1558                 port_num += count;
1559         }
1560
1561         /* Oops */
1562         return -1;
1563 }
1564
1565 /* Convert one mux entry from the interface and load into private->mux[] */
1566 static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
1567                                        u32 mux_entry)
1568 {
1569         const struct scarlett2_device_info *info = private->info;
1570         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1571
1572         int dst_idx, src_idx;
1573
1574         dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
1575                                           mux_entry & 0xFFF);
1576         if (dst_idx < 0)
1577                 return;
1578
1579         if (dst_idx >= private->num_mux_dsts) {
1580                 usb_audio_err(private->mixer->chip,
1581                         "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
1582                         mux_entry, dst_idx, private->num_mux_dsts);
1583                 return;
1584         }
1585
1586         src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
1587                                           mux_entry >> 12);
1588         if (src_idx < 0)
1589                 return;
1590
1591         if (src_idx >= private->num_mux_srcs) {
1592                 usb_audio_err(private->mixer->chip,
1593                         "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
1594                         mux_entry, src_idx, private->num_mux_srcs);
1595                 return;
1596         }
1597
1598         private->mux[dst_idx] = src_idx;
1599 }
1600
1601 /* Send USB message to get mux inputs and then populate private->mux[] */
1602 static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
1603 {
1604         struct scarlett2_data *private = mixer->private_data;
1605         int count = private->num_mux_dsts;
1606         int err, i;
1607
1608         struct {
1609                 __le16 num;
1610                 __le16 count;
1611         } __packed req;
1612
1613         __le32 data[SCARLETT2_MUX_MAX];
1614
1615         private->mux_updated = 0;
1616
1617         req.num = 0;
1618         req.count = cpu_to_le16(count);
1619
1620         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
1621                             &req, sizeof(req),
1622                             data, count * sizeof(u32));
1623         if (err < 0)
1624                 return err;
1625
1626         for (i = 0; i < count; i++)
1627                 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
1628
1629         return 0;
1630 }
1631
1632 /* Send USB messages to set mux inputs */
1633 static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
1634 {
1635         struct scarlett2_data *private = mixer->private_data;
1636         const struct scarlett2_device_info *info = private->info;
1637         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1638         int table;
1639
1640         struct {
1641                 __le16 pad;
1642                 __le16 num;
1643                 __le32 data[SCARLETT2_MUX_MAX];
1644         } __packed req;
1645
1646         req.pad = 0;
1647
1648         /* set mux settings for each rate */
1649         for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
1650                 const struct scarlett2_mux_entry *entry;
1651
1652                 /* i counts over the output array */
1653                 int i = 0, err;
1654
1655                 req.num = cpu_to_le16(table);
1656
1657                 /* loop through each entry */
1658                 for (entry = info->mux_assignment[table];
1659                      entry->count;
1660                      entry++) {
1661                         int j;
1662                         int port_type = entry->port_type;
1663                         int port_idx = entry->start;
1664                         int mux_idx = scarlett2_get_port_start_num(port_count,
1665                                 SCARLETT2_PORT_OUT, port_type) + port_idx;
1666                         int dst_id = scarlett2_ports[port_type].id + port_idx;
1667
1668                         /* Empty slots */
1669                         if (!dst_id) {
1670                                 for (j = 0; j < entry->count; j++)
1671                                         req.data[i++] = 0;
1672                                 continue;
1673                         }
1674
1675                         /* Non-empty mux slots use the lower 12 bits
1676                          * for the destination and next 12 bits for
1677                          * the source
1678                          */
1679                         for (j = 0; j < entry->count; j++) {
1680                                 int src_id = scarlett2_mux_src_num_to_id(
1681                                         port_count, private->mux[mux_idx++]);
1682                                 req.data[i++] = cpu_to_le32(dst_id |
1683                                                             src_id << 12);
1684                                 dst_id++;
1685                         }
1686                 }
1687
1688                 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
1689                                     &req, (i + 1) * sizeof(u32),
1690                                     NULL, 0);
1691                 if (err < 0)
1692                         return err;
1693         }
1694
1695         return 0;
1696 }
1697
1698 /* Send USB message to get meter levels */
1699 static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
1700                                           u16 num_meters, u16 *levels)
1701 {
1702         struct {
1703                 __le16 pad;
1704                 __le16 num_meters;
1705                 __le32 magic;
1706         } __packed req;
1707         u32 resp[SCARLETT2_MAX_METERS];
1708         int i, err;
1709
1710         req.pad = 0;
1711         req.num_meters = cpu_to_le16(num_meters);
1712         req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
1713         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
1714                             &req, sizeof(req), resp, num_meters * sizeof(u32));
1715         if (err < 0)
1716                 return err;
1717
1718         /* copy, convert to u16 */
1719         for (i = 0; i < num_meters; i++)
1720                 levels[i] = resp[i];
1721
1722         return 0;
1723 }
1724
1725 /*** Control Functions ***/
1726
1727 /* helper function to create a new control */
1728 static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
1729                                  const struct snd_kcontrol_new *ncontrol,
1730                                  int index, int channels, const char *name,
1731                                  struct snd_kcontrol **kctl_return)
1732 {
1733         struct snd_kcontrol *kctl;
1734         struct usb_mixer_elem_info *elem;
1735         int err;
1736
1737         elem = kzalloc(sizeof(*elem), GFP_KERNEL);
1738         if (!elem)
1739                 return -ENOMEM;
1740
1741         /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
1742          * ignores them for resume and other operations.
1743          * Also, the head.id field is set to 0, as we don't use this field.
1744          */
1745         elem->head.mixer = mixer;
1746         elem->control = index;
1747         elem->head.id = 0;
1748         elem->channels = channels;
1749         elem->val_type = USB_MIXER_BESPOKEN;
1750
1751         kctl = snd_ctl_new1(ncontrol, elem);
1752         if (!kctl) {
1753                 kfree(elem);
1754                 return -ENOMEM;
1755         }
1756         kctl->private_free = snd_usb_mixer_elem_free;
1757
1758         strscpy(kctl->id.name, name, sizeof(kctl->id.name));
1759
1760         err = snd_usb_mixer_add_control(&elem->head, kctl);
1761         if (err < 0)
1762                 return err;
1763
1764         if (kctl_return)
1765                 *kctl_return = kctl;
1766
1767         return 0;
1768 }
1769
1770 /*** Sync Control ***/
1771
1772 /* Update sync control after receiving notification that the status
1773  * has changed
1774  */
1775 static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
1776 {
1777         struct scarlett2_data *private = mixer->private_data;
1778
1779         private->sync_updated = 0;
1780         return scarlett2_usb_get_sync_status(mixer, &private->sync);
1781 }
1782
1783 static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
1784                                    struct snd_ctl_elem_info *uinfo)
1785 {
1786         static const char *texts[2] = {
1787                 "Unlocked", "Locked"
1788         };
1789         return snd_ctl_enum_info(uinfo, 1, 2, texts);
1790 }
1791
1792 static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
1793                                   struct snd_ctl_elem_value *ucontrol)
1794 {
1795         struct usb_mixer_elem_info *elem = kctl->private_data;
1796         struct usb_mixer_interface *mixer = elem->head.mixer;
1797         struct scarlett2_data *private = mixer->private_data;
1798
1799         mutex_lock(&private->data_mutex);
1800         if (private->sync_updated)
1801                 scarlett2_update_sync(mixer);
1802         ucontrol->value.enumerated.item[0] = private->sync;
1803         mutex_unlock(&private->data_mutex);
1804
1805         return 0;
1806 }
1807
1808 static const struct snd_kcontrol_new scarlett2_sync_ctl = {
1809         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1810         .access = SNDRV_CTL_ELEM_ACCESS_READ,
1811         .name = "",
1812         .info = scarlett2_sync_ctl_info,
1813         .get  = scarlett2_sync_ctl_get
1814 };
1815
1816 static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
1817 {
1818         struct scarlett2_data *private = mixer->private_data;
1819
1820         /* devices without a mixer also don't support reporting sync status */
1821         if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
1822                 return 0;
1823
1824         return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
1825                                      0, 1, "Sync Status", &private->sync_ctl);
1826 }
1827
1828 /*** Analogue Line Out Volume Controls ***/
1829
1830 /* Update hardware volume controls after receiving notification that
1831  * they have changed
1832  */
1833 static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
1834 {
1835         struct scarlett2_data *private = mixer->private_data;
1836         const struct scarlett2_device_info *info = private->info;
1837         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1838         struct scarlett2_usb_volume_status volume_status;
1839         int num_line_out =
1840                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
1841         int err, i;
1842         int mute;
1843
1844         private->vol_updated = 0;
1845
1846         err = scarlett2_usb_get_volume_status(mixer, &volume_status);
1847         if (err < 0)
1848                 return err;
1849
1850         private->master_vol = clamp(
1851                 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
1852                 0, SCARLETT2_VOLUME_BIAS);
1853
1854         if (info->line_out_hw_vol)
1855                 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
1856                         private->dim_mute[i] = !!volume_status.dim_mute[i];
1857
1858         mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];
1859
1860         for (i = 0; i < num_line_out; i++)
1861                 if (private->vol_sw_hw_switch[i]) {
1862                         private->vol[i] = private->master_vol;
1863                         private->mute_switch[i] = mute;
1864                 }
1865
1866         return 0;
1867 }
1868
1869 static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
1870                                      struct snd_ctl_elem_info *uinfo)
1871 {
1872         struct usb_mixer_elem_info *elem = kctl->private_data;
1873
1874         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1875         uinfo->count = elem->channels;
1876         uinfo->value.integer.min = 0;
1877         uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
1878         uinfo->value.integer.step = 1;
1879         return 0;
1880 }
1881
1882 static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
1883                                            struct snd_ctl_elem_value *ucontrol)
1884 {
1885         struct usb_mixer_elem_info *elem = kctl->private_data;
1886         struct usb_mixer_interface *mixer = elem->head.mixer;
1887         struct scarlett2_data *private = mixer->private_data;
1888
1889         mutex_lock(&private->data_mutex);
1890         if (private->vol_updated)
1891                 scarlett2_update_volumes(mixer);
1892         mutex_unlock(&private->data_mutex);
1893
1894         ucontrol->value.integer.value[0] = private->master_vol;
1895         return 0;
1896 }
1897
1898 static int line_out_remap(struct scarlett2_data *private, int index)
1899 {
1900         const struct scarlett2_device_info *info = private->info;
1901
1902         if (!info->line_out_remap_enable)
1903                 return index;
1904         return info->line_out_remap[index];
1905 }
1906
1907 static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
1908                                     struct snd_ctl_elem_value *ucontrol)
1909 {
1910         struct usb_mixer_elem_info *elem = kctl->private_data;
1911         struct usb_mixer_interface *mixer = elem->head.mixer;
1912         struct scarlett2_data *private = mixer->private_data;
1913         int index = line_out_remap(private, elem->control);
1914
1915         mutex_lock(&private->data_mutex);
1916         if (private->vol_updated)
1917                 scarlett2_update_volumes(mixer);
1918         mutex_unlock(&private->data_mutex);
1919
1920         ucontrol->value.integer.value[0] = private->vol[index];
1921         return 0;
1922 }
1923
1924 static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
1925                                     struct snd_ctl_elem_value *ucontrol)
1926 {
1927         struct usb_mixer_elem_info *elem = kctl->private_data;
1928         struct usb_mixer_interface *mixer = elem->head.mixer;
1929         struct scarlett2_data *private = mixer->private_data;
1930         int index = line_out_remap(private, elem->control);
1931         int oval, val, err = 0;
1932
1933         mutex_lock(&private->data_mutex);
1934
1935         oval = private->vol[index];
1936         val = ucontrol->value.integer.value[0];
1937
1938         if (oval == val)
1939                 goto unlock;
1940
1941         private->vol[index] = val;
1942         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1943                                        index, val - SCARLETT2_VOLUME_BIAS);
1944         if (err == 0)
1945                 err = 1;
1946
1947 unlock:
1948         mutex_unlock(&private->data_mutex);
1949         return err;
1950 }
1951
1952 static const DECLARE_TLV_DB_MINMAX(
1953         db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0
1954 );
1955
1956 static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
1957         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1958         .access = SNDRV_CTL_ELEM_ACCESS_READ |
1959                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1960         .name = "",
1961         .info = scarlett2_volume_ctl_info,
1962         .get  = scarlett2_master_volume_ctl_get,
1963         .private_value = 0, /* max value */
1964         .tlv = { .p = db_scale_scarlett2_gain }
1965 };
1966
1967 static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
1968         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1969         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1970                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1971         .name = "",
1972         .info = scarlett2_volume_ctl_info,
1973         .get  = scarlett2_volume_ctl_get,
1974         .put  = scarlett2_volume_ctl_put,
1975         .private_value = 0, /* max value */
1976         .tlv = { .p = db_scale_scarlett2_gain }
1977 };
1978
1979 /*** Mute Switch Controls ***/
1980
1981 static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
1982                                         struct snd_ctl_elem_value *ucontrol)
1983 {
1984         struct usb_mixer_elem_info *elem = kctl->private_data;
1985         struct usb_mixer_interface *mixer = elem->head.mixer;
1986         struct scarlett2_data *private = mixer->private_data;
1987         int index = line_out_remap(private, elem->control);
1988
1989         mutex_lock(&private->data_mutex);
1990         if (private->vol_updated)
1991                 scarlett2_update_volumes(mixer);
1992         mutex_unlock(&private->data_mutex);
1993
1994         ucontrol->value.integer.value[0] = private->mute_switch[index];
1995         return 0;
1996 }
1997
1998 static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
1999                                         struct snd_ctl_elem_value *ucontrol)
2000 {
2001         struct usb_mixer_elem_info *elem = kctl->private_data;
2002         struct usb_mixer_interface *mixer = elem->head.mixer;
2003         struct scarlett2_data *private = mixer->private_data;
2004         int index = line_out_remap(private, elem->control);
2005         int oval, val, err = 0;
2006
2007         mutex_lock(&private->data_mutex);
2008
2009         oval = private->mute_switch[index];
2010         val = !!ucontrol->value.integer.value[0];
2011
2012         if (oval == val)
2013                 goto unlock;
2014
2015         private->mute_switch[index] = val;
2016
2017         /* Send mute change to the device */
2018         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
2019                                        index, val);
2020         if (err == 0)
2021                 err = 1;
2022
2023 unlock:
2024         mutex_unlock(&private->data_mutex);
2025         return err;
2026 }
2027
2028 static const struct snd_kcontrol_new scarlett2_mute_ctl = {
2029         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2030         .name = "",
2031         .info = snd_ctl_boolean_mono_info,
2032         .get  = scarlett2_mute_ctl_get,
2033         .put  = scarlett2_mute_ctl_put,
2034 };
2035
2036 /*** HW/SW Volume Switch Controls ***/
2037
2038 static void scarlett2_sw_hw_ctl_ro(struct scarlett2_data *private, int index)
2039 {
2040         private->sw_hw_ctls[index]->vd[0].access &=
2041                 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2042 }
2043
2044 static void scarlett2_sw_hw_ctl_rw(struct scarlett2_data *private, int index)
2045 {
2046         private->sw_hw_ctls[index]->vd[0].access |=
2047                 SNDRV_CTL_ELEM_ACCESS_WRITE;
2048 }
2049
2050 static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
2051                                          struct snd_ctl_elem_info *uinfo)
2052 {
2053         static const char *const values[2] = {
2054                 "SW", "HW"
2055         };
2056
2057         return snd_ctl_enum_info(uinfo, 1, 2, values);
2058 }
2059
2060 static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
2061                                         struct snd_ctl_elem_value *ucontrol)
2062 {
2063         struct usb_mixer_elem_info *elem = kctl->private_data;
2064         struct scarlett2_data *private = elem->head.mixer->private_data;
2065         int index = line_out_remap(private, elem->control);
2066
2067         ucontrol->value.enumerated.item[0] = private->vol_sw_hw_switch[index];
2068         return 0;
2069 }
2070
2071 static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
2072                                            int index, int value)
2073 {
2074         struct scarlett2_data *private = mixer->private_data;
2075         struct snd_card *card = mixer->chip->card;
2076
2077         /* Set/Clear write bits */
2078         if (value) {
2079                 private->vol_ctls[index]->vd[0].access |=
2080                         SNDRV_CTL_ELEM_ACCESS_WRITE;
2081                 private->mute_ctls[index]->vd[0].access |=
2082                         SNDRV_CTL_ELEM_ACCESS_WRITE;
2083         } else {
2084                 private->vol_ctls[index]->vd[0].access &=
2085                         ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2086                 private->mute_ctls[index]->vd[0].access &=
2087                         ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2088         }
2089
2090         /* Notify of write bit and possible value change */
2091         snd_ctl_notify(card,
2092                        SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
2093                        &private->vol_ctls[index]->id);
2094         snd_ctl_notify(card,
2095                        SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
2096                        &private->mute_ctls[index]->id);
2097 }
2098
2099 static int scarlett2_sw_hw_change(struct usb_mixer_interface *mixer,
2100                                   int ctl_index, int val)
2101 {
2102         struct scarlett2_data *private = mixer->private_data;
2103         int index = line_out_remap(private, ctl_index);
2104         int err;
2105
2106         private->vol_sw_hw_switch[index] = val;
2107
2108         /* Change access mode to RO (hardware controlled volume)
2109          * or RW (software controlled volume)
2110          */
2111         scarlett2_vol_ctl_set_writable(mixer, ctl_index, !val);
2112
2113         /* Reset volume/mute to master volume/mute */
2114         private->vol[index] = private->master_vol;
2115         private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE];
2116
2117         /* Set SW volume to current HW volume */
2118         err = scarlett2_usb_set_config(
2119                 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
2120                 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
2121         if (err < 0)
2122                 return err;
2123
2124         /* Set SW mute to current HW mute */
2125         err = scarlett2_usb_set_config(
2126                 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
2127                 index, private->dim_mute[SCARLETT2_BUTTON_MUTE]);
2128         if (err < 0)
2129                 return err;
2130
2131         /* Send SW/HW switch change to the device */
2132         return scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
2133                                         index, val);
2134 }
2135
2136 static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
2137                                         struct snd_ctl_elem_value *ucontrol)
2138 {
2139         struct usb_mixer_elem_info *elem = kctl->private_data;
2140         struct usb_mixer_interface *mixer = elem->head.mixer;
2141         struct scarlett2_data *private = mixer->private_data;
2142         int ctl_index = elem->control;
2143         int index = line_out_remap(private, ctl_index);
2144         int oval, val, err = 0;
2145
2146         mutex_lock(&private->data_mutex);
2147
2148         oval = private->vol_sw_hw_switch[index];
2149         val = !!ucontrol->value.enumerated.item[0];
2150
2151         if (oval == val)
2152                 goto unlock;
2153
2154         err = scarlett2_sw_hw_change(mixer, ctl_index, val);
2155         if (err == 0)
2156                 err = 1;
2157
2158 unlock:
2159         mutex_unlock(&private->data_mutex);
2160         return err;
2161 }
2162
2163 static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
2164         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2165         .name = "",
2166         .info = scarlett2_sw_hw_enum_ctl_info,
2167         .get  = scarlett2_sw_hw_enum_ctl_get,
2168         .put  = scarlett2_sw_hw_enum_ctl_put,
2169 };
2170
2171 /*** Line Level/Instrument Level Switch Controls ***/
2172
2173 static int scarlett2_update_input_other(struct usb_mixer_interface *mixer)
2174 {
2175         struct scarlett2_data *private = mixer->private_data;
2176         const struct scarlett2_device_info *info = private->info;
2177
2178         private->input_other_updated = 0;
2179
2180         if (info->level_input_count) {
2181                 int err = scarlett2_usb_get_config(
2182                         mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2183                         info->level_input_count + info->level_input_first,
2184                         private->level_switch);
2185                 if (err < 0)
2186                         return err;
2187         }
2188
2189         if (info->pad_input_count) {
2190                 int err = scarlett2_usb_get_config(
2191                         mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2192                         info->pad_input_count, private->pad_switch);
2193                 if (err < 0)
2194                         return err;
2195         }
2196
2197         if (info->air_input_count) {
2198                 int err = scarlett2_usb_get_config(
2199                         mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2200                         info->air_input_count, private->air_switch);
2201                 if (err < 0)
2202                         return err;
2203         }
2204
2205         if (info->phantom_count) {
2206                 int err = scarlett2_usb_get_config(
2207                         mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
2208                         info->phantom_count, private->phantom_switch);
2209                 if (err < 0)
2210                         return err;
2211
2212                 err = scarlett2_usb_get_config(
2213                         mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
2214                         1, &private->phantom_persistence);
2215                 if (err < 0)
2216                         return err;
2217         }
2218
2219         return 0;
2220 }
2221
2222 static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
2223                                          struct snd_ctl_elem_info *uinfo)
2224 {
2225         static const char *const values[2] = {
2226                 "Line", "Inst"
2227         };
2228
2229         return snd_ctl_enum_info(uinfo, 1, 2, values);
2230 }
2231
2232 static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
2233                                         struct snd_ctl_elem_value *ucontrol)
2234 {
2235         struct usb_mixer_elem_info *elem = kctl->private_data;
2236         struct usb_mixer_interface *mixer = elem->head.mixer;
2237         struct scarlett2_data *private = mixer->private_data;
2238         const struct scarlett2_device_info *info = private->info;
2239
2240         int index = elem->control + info->level_input_first;
2241
2242         mutex_lock(&private->data_mutex);
2243         if (private->input_other_updated)
2244                 scarlett2_update_input_other(mixer);
2245         ucontrol->value.enumerated.item[0] = private->level_switch[index];
2246         mutex_unlock(&private->data_mutex);
2247
2248         return 0;
2249 }
2250
2251 static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
2252                                         struct snd_ctl_elem_value *ucontrol)
2253 {
2254         struct usb_mixer_elem_info *elem = kctl->private_data;
2255         struct usb_mixer_interface *mixer = elem->head.mixer;
2256         struct scarlett2_data *private = mixer->private_data;
2257         const struct scarlett2_device_info *info = private->info;
2258
2259         int index = elem->control + info->level_input_first;
2260         int oval, val, err = 0;
2261
2262         mutex_lock(&private->data_mutex);
2263
2264         oval = private->level_switch[index];
2265         val = !!ucontrol->value.enumerated.item[0];
2266
2267         if (oval == val)
2268                 goto unlock;
2269
2270         private->level_switch[index] = val;
2271
2272         /* Send switch change to the device */
2273         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2274                                        index, val);
2275         if (err == 0)
2276                 err = 1;
2277
2278 unlock:
2279         mutex_unlock(&private->data_mutex);
2280         return err;
2281 }
2282
2283 static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
2284         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2285         .name = "",
2286         .info = scarlett2_level_enum_ctl_info,
2287         .get  = scarlett2_level_enum_ctl_get,
2288         .put  = scarlett2_level_enum_ctl_put,
2289 };
2290
2291 /*** Pad Switch Controls ***/
2292
2293 static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
2294                                  struct snd_ctl_elem_value *ucontrol)
2295 {
2296         struct usb_mixer_elem_info *elem = kctl->private_data;
2297         struct usb_mixer_interface *mixer = elem->head.mixer;
2298         struct scarlett2_data *private = mixer->private_data;
2299
2300         mutex_lock(&private->data_mutex);
2301         if (private->input_other_updated)
2302                 scarlett2_update_input_other(mixer);
2303         ucontrol->value.integer.value[0] =
2304                 private->pad_switch[elem->control];
2305         mutex_unlock(&private->data_mutex);
2306
2307         return 0;
2308 }
2309
2310 static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
2311                                  struct snd_ctl_elem_value *ucontrol)
2312 {
2313         struct usb_mixer_elem_info *elem = kctl->private_data;
2314         struct usb_mixer_interface *mixer = elem->head.mixer;
2315         struct scarlett2_data *private = mixer->private_data;
2316
2317         int index = elem->control;
2318         int oval, val, err = 0;
2319
2320         mutex_lock(&private->data_mutex);
2321
2322         oval = private->pad_switch[index];
2323         val = !!ucontrol->value.integer.value[0];
2324
2325         if (oval == val)
2326                 goto unlock;
2327
2328         private->pad_switch[index] = val;
2329
2330         /* Send switch change to the device */
2331         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2332                                        index, val);
2333         if (err == 0)
2334                 err = 1;
2335
2336 unlock:
2337         mutex_unlock(&private->data_mutex);
2338         return err;
2339 }
2340
2341 static const struct snd_kcontrol_new scarlett2_pad_ctl = {
2342         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2343         .name = "",
2344         .info = snd_ctl_boolean_mono_info,
2345         .get  = scarlett2_pad_ctl_get,
2346         .put  = scarlett2_pad_ctl_put,
2347 };
2348
2349 /*** Air Switch Controls ***/
2350
2351 static int scarlett2_air_ctl_get(struct snd_kcontrol *kctl,
2352                                  struct snd_ctl_elem_value *ucontrol)
2353 {
2354         struct usb_mixer_elem_info *elem = kctl->private_data;
2355         struct usb_mixer_interface *mixer = elem->head.mixer;
2356         struct scarlett2_data *private = mixer->private_data;
2357
2358         mutex_lock(&private->data_mutex);
2359         if (private->input_other_updated)
2360                 scarlett2_update_input_other(mixer);
2361         ucontrol->value.integer.value[0] = private->air_switch[elem->control];
2362         mutex_unlock(&private->data_mutex);
2363
2364         return 0;
2365 }
2366
2367 static int scarlett2_air_ctl_put(struct snd_kcontrol *kctl,
2368                                  struct snd_ctl_elem_value *ucontrol)
2369 {
2370         struct usb_mixer_elem_info *elem = kctl->private_data;
2371         struct usb_mixer_interface *mixer = elem->head.mixer;
2372         struct scarlett2_data *private = mixer->private_data;
2373
2374         int index = elem->control;
2375         int oval, val, err = 0;
2376
2377         mutex_lock(&private->data_mutex);
2378
2379         oval = private->air_switch[index];
2380         val = !!ucontrol->value.integer.value[0];
2381
2382         if (oval == val)
2383                 goto unlock;
2384
2385         private->air_switch[index] = val;
2386
2387         /* Send switch change to the device */
2388         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2389                                        index, val);
2390         if (err == 0)
2391                 err = 1;
2392
2393 unlock:
2394         mutex_unlock(&private->data_mutex);
2395         return err;
2396 }
2397
2398 static const struct snd_kcontrol_new scarlett2_air_ctl = {
2399         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2400         .name = "",
2401         .info = snd_ctl_boolean_mono_info,
2402         .get  = scarlett2_air_ctl_get,
2403         .put  = scarlett2_air_ctl_put,
2404 };
2405
2406 /*** Phantom Switch Controls ***/
2407
2408 static int scarlett2_phantom_ctl_get(struct snd_kcontrol *kctl,
2409                                      struct snd_ctl_elem_value *ucontrol)
2410 {
2411         struct usb_mixer_elem_info *elem = kctl->private_data;
2412         struct usb_mixer_interface *mixer = elem->head.mixer;
2413         struct scarlett2_data *private = mixer->private_data;
2414
2415         mutex_lock(&private->data_mutex);
2416         if (private->input_other_updated)
2417                 scarlett2_update_input_other(mixer);
2418         ucontrol->value.integer.value[0] =
2419                 private->phantom_switch[elem->control];
2420         mutex_unlock(&private->data_mutex);
2421
2422         return 0;
2423 }
2424
2425 static int scarlett2_phantom_ctl_put(struct snd_kcontrol *kctl,
2426                                      struct snd_ctl_elem_value *ucontrol)
2427 {
2428         struct usb_mixer_elem_info *elem = kctl->private_data;
2429         struct usb_mixer_interface *mixer = elem->head.mixer;
2430         struct scarlett2_data *private = mixer->private_data;
2431
2432         int index = elem->control;
2433         int oval, val, err = 0;
2434
2435         mutex_lock(&private->data_mutex);
2436
2437         oval = private->phantom_switch[index];
2438         val = !!ucontrol->value.integer.value[0];
2439
2440         if (oval == val)
2441                 goto unlock;
2442
2443         private->phantom_switch[index] = val;
2444
2445         /* Send switch change to the device */
2446         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
2447                                        index, val);
2448         if (err == 0)
2449                 err = 1;
2450
2451 unlock:
2452         mutex_unlock(&private->data_mutex);
2453         return err;
2454 }
2455
2456 static const struct snd_kcontrol_new scarlett2_phantom_ctl = {
2457         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2458         .name = "",
2459         .info = snd_ctl_boolean_mono_info,
2460         .get  = scarlett2_phantom_ctl_get,
2461         .put  = scarlett2_phantom_ctl_put,
2462 };
2463
2464 /*** Phantom Persistence Control ***/
2465
2466 static int scarlett2_phantom_persistence_ctl_get(
2467         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2468 {
2469         struct usb_mixer_elem_info *elem = kctl->private_data;
2470         struct scarlett2_data *private = elem->head.mixer->private_data;
2471
2472         ucontrol->value.integer.value[0] = private->phantom_persistence;
2473         return 0;
2474 }
2475
2476 static int scarlett2_phantom_persistence_ctl_put(
2477         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2478 {
2479         struct usb_mixer_elem_info *elem = kctl->private_data;
2480         struct usb_mixer_interface *mixer = elem->head.mixer;
2481         struct scarlett2_data *private = mixer->private_data;
2482
2483         int index = elem->control;
2484         int oval, val, err = 0;
2485
2486         mutex_lock(&private->data_mutex);
2487
2488         oval = private->phantom_persistence;
2489         val = !!ucontrol->value.integer.value[0];
2490
2491         if (oval == val)
2492                 goto unlock;
2493
2494         private->phantom_persistence = val;
2495
2496         /* Send switch change to the device */
2497         err = scarlett2_usb_set_config(
2498                 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, index, val);
2499         if (err == 0)
2500                 err = 1;
2501
2502 unlock:
2503         mutex_unlock(&private->data_mutex);
2504         return err;
2505 }
2506
2507 static const struct snd_kcontrol_new scarlett2_phantom_persistence_ctl = {
2508         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2509         .name = "",
2510         .info = snd_ctl_boolean_mono_info,
2511         .get  = scarlett2_phantom_persistence_ctl_get,
2512         .put  = scarlett2_phantom_persistence_ctl_put,
2513 };
2514
2515 /*** Direct Monitor Control ***/
2516
2517 static int scarlett2_update_monitor_other(struct usb_mixer_interface *mixer)
2518 {
2519         struct scarlett2_data *private = mixer->private_data;
2520         const struct scarlett2_device_info *info = private->info;
2521         int err;
2522
2523         /* monitor_other_enable[0] enables speaker switching
2524          * monitor_other_enable[1] enables talkback
2525          */
2526         u8 monitor_other_enable[2];
2527
2528         /* monitor_other_switch[0] activates the alternate speakers
2529          * monitor_other_switch[1] activates talkback
2530          */
2531         u8 monitor_other_switch[2];
2532
2533         private->monitor_other_updated = 0;
2534
2535         if (info->direct_monitor)
2536                 return scarlett2_usb_get_config(
2537                         mixer, SCARLETT2_CONFIG_DIRECT_MONITOR,
2538                         1, &private->direct_monitor_switch);
2539
2540         /* if it doesn't do speaker switching then it also doesn't do
2541          * talkback
2542          */
2543         if (!info->has_speaker_switching)
2544                 return 0;
2545
2546         err = scarlett2_usb_get_config(
2547                 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2548                 2, monitor_other_enable);
2549         if (err < 0)
2550                 return err;
2551
2552         err = scarlett2_usb_get_config(
2553                 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2554                 2, monitor_other_switch);
2555         if (err < 0)
2556                 return err;
2557
2558         if (!monitor_other_enable[0])
2559                 private->speaker_switching_switch = 0;
2560         else
2561                 private->speaker_switching_switch = monitor_other_switch[0] + 1;
2562
2563         if (info->has_talkback) {
2564                 const int (*port_count)[SCARLETT2_PORT_DIRNS] =
2565                         info->port_count;
2566                 int num_mixes =
2567                         port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2568                 u16 bitmap;
2569                 int i;
2570
2571                 if (!monitor_other_enable[1])
2572                         private->talkback_switch = 0;
2573                 else
2574                         private->talkback_switch = monitor_other_switch[1] + 1;
2575
2576                 err = scarlett2_usb_get_config(mixer,
2577                                                SCARLETT2_CONFIG_TALKBACK_MAP,
2578                                                1, &bitmap);
2579                 if (err < 0)
2580                         return err;
2581                 for (i = 0; i < num_mixes; i++, bitmap >>= 1)
2582                         private->talkback_map[i] = bitmap & 1;
2583         }
2584
2585         return 0;
2586 }
2587
2588 static int scarlett2_direct_monitor_ctl_get(
2589         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2590 {
2591         struct usb_mixer_elem_info *elem = kctl->private_data;
2592         struct usb_mixer_interface *mixer = elem->head.mixer;
2593         struct scarlett2_data *private = elem->head.mixer->private_data;
2594
2595         mutex_lock(&private->data_mutex);
2596         if (private->monitor_other_updated)
2597                 scarlett2_update_monitor_other(mixer);
2598         ucontrol->value.enumerated.item[0] = private->direct_monitor_switch;
2599         mutex_unlock(&private->data_mutex);
2600
2601         return 0;
2602 }
2603
2604 static int scarlett2_direct_monitor_ctl_put(
2605         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2606 {
2607         struct usb_mixer_elem_info *elem = kctl->private_data;
2608         struct usb_mixer_interface *mixer = elem->head.mixer;
2609         struct scarlett2_data *private = mixer->private_data;
2610
2611         int index = elem->control;
2612         int oval, val, err = 0;
2613
2614         mutex_lock(&private->data_mutex);
2615
2616         oval = private->direct_monitor_switch;
2617         val = min(ucontrol->value.enumerated.item[0], 2U);
2618
2619         if (oval == val)
2620                 goto unlock;
2621
2622         private->direct_monitor_switch = val;
2623
2624         /* Send switch change to the device */
2625         err = scarlett2_usb_set_config(
2626                 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, index, val);
2627         if (err == 0)
2628                 err = 1;
2629
2630 unlock:
2631         mutex_unlock(&private->data_mutex);
2632         return err;
2633 }
2634
2635 static int scarlett2_direct_monitor_stereo_enum_ctl_info(
2636         struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2637 {
2638         static const char *const values[3] = {
2639                 "Off", "Mono", "Stereo"
2640         };
2641
2642         return snd_ctl_enum_info(uinfo, 1, 3, values);
2643 }
2644
2645 /* Direct Monitor for Solo is mono-only and only needs a boolean control
2646  * Direct Monitor for 2i2 is selectable between Off/Mono/Stereo
2647  */
2648 static const struct snd_kcontrol_new scarlett2_direct_monitor_ctl[2] = {
2649         {
2650                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2651                 .name = "",
2652                 .info = snd_ctl_boolean_mono_info,
2653                 .get  = scarlett2_direct_monitor_ctl_get,
2654                 .put  = scarlett2_direct_monitor_ctl_put,
2655         },
2656         {
2657                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2658                 .name = "",
2659                 .info = scarlett2_direct_monitor_stereo_enum_ctl_info,
2660                 .get  = scarlett2_direct_monitor_ctl_get,
2661                 .put  = scarlett2_direct_monitor_ctl_put,
2662         }
2663 };
2664
2665 static int scarlett2_add_direct_monitor_ctl(struct usb_mixer_interface *mixer)
2666 {
2667         struct scarlett2_data *private = mixer->private_data;
2668         const struct scarlett2_device_info *info = private->info;
2669         const char *s;
2670
2671         if (!info->direct_monitor)
2672                 return 0;
2673
2674         s = info->direct_monitor == 1
2675               ? "Direct Monitor Playback Switch"
2676               : "Direct Monitor Playback Enum";
2677
2678         return scarlett2_add_new_ctl(
2679                 mixer, &scarlett2_direct_monitor_ctl[info->direct_monitor - 1],
2680                 0, 1, s, &private->direct_monitor_ctl);
2681 }
2682
2683 /*** Speaker Switching Control ***/
2684
2685 static int scarlett2_speaker_switch_enum_ctl_info(
2686         struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2687 {
2688         static const char *const values[3] = {
2689                 "Off", "Main", "Alt"
2690         };
2691
2692         return snd_ctl_enum_info(uinfo, 1, 3, values);
2693 }
2694
2695 static int scarlett2_speaker_switch_enum_ctl_get(
2696         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2697 {
2698         struct usb_mixer_elem_info *elem = kctl->private_data;
2699         struct usb_mixer_interface *mixer = elem->head.mixer;
2700         struct scarlett2_data *private = mixer->private_data;
2701
2702         mutex_lock(&private->data_mutex);
2703         if (private->monitor_other_updated)
2704                 scarlett2_update_monitor_other(mixer);
2705         ucontrol->value.enumerated.item[0] = private->speaker_switching_switch;
2706         mutex_unlock(&private->data_mutex);
2707
2708         return 0;
2709 }
2710
2711 /* when speaker switching gets enabled, switch the main/alt speakers
2712  * to HW volume and disable those controls
2713  */
2714 static int scarlett2_speaker_switch_enable(struct usb_mixer_interface *mixer)
2715 {
2716         struct snd_card *card = mixer->chip->card;
2717         struct scarlett2_data *private = mixer->private_data;
2718         int i, err;
2719
2720         for (i = 0; i < 4; i++) {
2721                 int index = line_out_remap(private, i);
2722
2723                 /* switch the main/alt speakers to HW volume */
2724                 if (!private->vol_sw_hw_switch[index]) {
2725                         err = scarlett2_sw_hw_change(private->mixer, i, 1);
2726                         if (err < 0)
2727                                 return err;
2728                 }
2729
2730                 /* disable the line out SW/HW switch */
2731                 scarlett2_sw_hw_ctl_ro(private, i);
2732                 snd_ctl_notify(card,
2733                                SNDRV_CTL_EVENT_MASK_VALUE |
2734                                  SNDRV_CTL_EVENT_MASK_INFO,
2735                                &private->sw_hw_ctls[i]->id);
2736         }
2737
2738         /* when the next monitor-other notify comes in, update the mux
2739          * configuration
2740          */
2741         private->speaker_switching_switched = 1;
2742
2743         return 0;
2744 }
2745
2746 /* when speaker switching gets disabled, reenable the hw/sw controls
2747  * and invalidate the routing
2748  */
2749 static void scarlett2_speaker_switch_disable(struct usb_mixer_interface *mixer)
2750 {
2751         struct snd_card *card = mixer->chip->card;
2752         struct scarlett2_data *private = mixer->private_data;
2753         int i;
2754
2755         /* enable the line out SW/HW switch */
2756         for (i = 0; i < 4; i++) {
2757                 scarlett2_sw_hw_ctl_rw(private, i);
2758                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2759                                &private->sw_hw_ctls[i]->id);
2760         }
2761
2762         /* when the next monitor-other notify comes in, update the mux
2763          * configuration
2764          */
2765         private->speaker_switching_switched = 1;
2766 }
2767
2768 static int scarlett2_speaker_switch_enum_ctl_put(
2769         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2770 {
2771         struct usb_mixer_elem_info *elem = kctl->private_data;
2772         struct usb_mixer_interface *mixer = elem->head.mixer;
2773         struct scarlett2_data *private = mixer->private_data;
2774
2775         int oval, val, err = 0;
2776
2777         mutex_lock(&private->data_mutex);
2778
2779         oval = private->speaker_switching_switch;
2780         val = min(ucontrol->value.enumerated.item[0], 2U);
2781
2782         if (oval == val)
2783                 goto unlock;
2784
2785         private->speaker_switching_switch = val;
2786
2787         /* enable/disable speaker switching */
2788         err = scarlett2_usb_set_config(
2789                 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2790                 0, !!val);
2791         if (err < 0)
2792                 goto unlock;
2793
2794         /* if speaker switching is enabled, select main or alt */
2795         err = scarlett2_usb_set_config(
2796                 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2797                 0, val == 2);
2798         if (err < 0)
2799                 goto unlock;
2800
2801         /* update controls if speaker switching gets enabled or disabled */
2802         if (!oval && val)
2803                 err = scarlett2_speaker_switch_enable(mixer);
2804         else if (oval && !val)
2805                 scarlett2_speaker_switch_disable(mixer);
2806
2807         if (err == 0)
2808                 err = 1;
2809
2810 unlock:
2811         mutex_unlock(&private->data_mutex);
2812         return err;
2813 }
2814
2815 static const struct snd_kcontrol_new scarlett2_speaker_switch_enum_ctl = {
2816         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2817         .name = "",
2818         .info = scarlett2_speaker_switch_enum_ctl_info,
2819         .get  = scarlett2_speaker_switch_enum_ctl_get,
2820         .put  = scarlett2_speaker_switch_enum_ctl_put,
2821 };
2822
2823 static int scarlett2_add_speaker_switch_ctl(
2824         struct usb_mixer_interface *mixer)
2825 {
2826         struct scarlett2_data *private = mixer->private_data;
2827         const struct scarlett2_device_info *info = private->info;
2828
2829         if (!info->has_speaker_switching)
2830                 return 0;
2831
2832         return scarlett2_add_new_ctl(
2833                 mixer, &scarlett2_speaker_switch_enum_ctl,
2834                 0, 1, "Speaker Switching Playback Enum",
2835                 &private->speaker_switching_ctl);
2836 }
2837
2838 /*** Talkback and Talkback Map Controls ***/
2839
2840 static int scarlett2_talkback_enum_ctl_info(
2841         struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2842 {
2843         static const char *const values[3] = {
2844                 "Disabled", "Off", "On"
2845         };
2846
2847         return snd_ctl_enum_info(uinfo, 1, 3, values);
2848 }
2849
2850 static int scarlett2_talkback_enum_ctl_get(
2851         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2852 {
2853         struct usb_mixer_elem_info *elem = kctl->private_data;
2854         struct usb_mixer_interface *mixer = elem->head.mixer;
2855         struct scarlett2_data *private = mixer->private_data;
2856
2857         mutex_lock(&private->data_mutex);
2858         if (private->monitor_other_updated)
2859                 scarlett2_update_monitor_other(mixer);
2860         ucontrol->value.enumerated.item[0] = private->talkback_switch;
2861         mutex_unlock(&private->data_mutex);
2862
2863         return 0;
2864 }
2865
2866 static int scarlett2_talkback_enum_ctl_put(
2867         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2868 {
2869         struct usb_mixer_elem_info *elem = kctl->private_data;
2870         struct usb_mixer_interface *mixer = elem->head.mixer;
2871         struct scarlett2_data *private = mixer->private_data;
2872
2873         int oval, val, err = 0;
2874
2875         mutex_lock(&private->data_mutex);
2876
2877         oval = private->talkback_switch;
2878         val = min(ucontrol->value.enumerated.item[0], 2U);
2879
2880         if (oval == val)
2881                 goto unlock;
2882
2883         private->talkback_switch = val;
2884
2885         /* enable/disable talkback */
2886         err = scarlett2_usb_set_config(
2887                 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2888                 1, !!val);
2889         if (err < 0)
2890                 goto unlock;
2891
2892         /* if talkback is enabled, select main or alt */
2893         err = scarlett2_usb_set_config(
2894                 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2895                 1, val == 2);
2896         if (err == 0)
2897                 err = 1;
2898
2899 unlock:
2900         mutex_unlock(&private->data_mutex);
2901         return err;
2902 }
2903
2904 static const struct snd_kcontrol_new scarlett2_talkback_enum_ctl = {
2905         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2906         .name = "",
2907         .info = scarlett2_talkback_enum_ctl_info,
2908         .get  = scarlett2_talkback_enum_ctl_get,
2909         .put  = scarlett2_talkback_enum_ctl_put,
2910 };
2911
2912 static int scarlett2_talkback_map_ctl_get(
2913         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2914 {
2915         struct usb_mixer_elem_info *elem = kctl->private_data;
2916         struct usb_mixer_interface *mixer = elem->head.mixer;
2917         struct scarlett2_data *private = mixer->private_data;
2918         int index = elem->control;
2919
2920         ucontrol->value.integer.value[0] = private->talkback_map[index];
2921
2922         return 0;
2923 }
2924
2925 static int scarlett2_talkback_map_ctl_put(
2926         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2927 {
2928         struct usb_mixer_elem_info *elem = kctl->private_data;
2929         struct usb_mixer_interface *mixer = elem->head.mixer;
2930         struct scarlett2_data *private = mixer->private_data;
2931         const int (*port_count)[SCARLETT2_PORT_DIRNS] =
2932                 private->info->port_count;
2933         int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2934
2935         int index = elem->control;
2936         int oval, val, err = 0, i;
2937         u16 bitmap = 0;
2938
2939         mutex_lock(&private->data_mutex);
2940
2941         oval = private->talkback_map[index];
2942         val = !!ucontrol->value.integer.value[0];
2943
2944         if (oval == val)
2945                 goto unlock;
2946
2947         private->talkback_map[index] = val;
2948
2949         for (i = 0; i < num_mixes; i++)
2950                 bitmap |= private->talkback_map[i] << i;
2951
2952         /* Send updated bitmap to the device */
2953         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_TALKBACK_MAP,
2954                                        0, bitmap);
2955         if (err == 0)
2956                 err = 1;
2957
2958 unlock:
2959         mutex_unlock(&private->data_mutex);
2960         return err;
2961 }
2962
2963 static const struct snd_kcontrol_new scarlett2_talkback_map_ctl = {
2964         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2965         .name = "",
2966         .info = snd_ctl_boolean_mono_info,
2967         .get  = scarlett2_talkback_map_ctl_get,
2968         .put  = scarlett2_talkback_map_ctl_put,
2969 };
2970
2971 static int scarlett2_add_talkback_ctls(
2972         struct usb_mixer_interface *mixer)
2973 {
2974         struct scarlett2_data *private = mixer->private_data;
2975         const struct scarlett2_device_info *info = private->info;
2976         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2977         int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2978         int err, i;
2979         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2980
2981         if (!info->has_talkback)
2982                 return 0;
2983
2984         err = scarlett2_add_new_ctl(
2985                 mixer, &scarlett2_talkback_enum_ctl,
2986                 0, 1, "Talkback Playback Enum",
2987                 &private->talkback_ctl);
2988         if (err < 0)
2989                 return err;
2990
2991         for (i = 0; i < num_mixes; i++) {
2992                 snprintf(s, sizeof(s),
2993                          "Talkback Mix %c Playback Switch", i + 'A');
2994                 err = scarlett2_add_new_ctl(mixer, &scarlett2_talkback_map_ctl,
2995                                             i, 1, s, NULL);
2996                 if (err < 0)
2997                         return err;
2998         }
2999
3000         return 0;
3001 }
3002
3003 /*** Dim/Mute Controls ***/
3004
3005 static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
3006                                       struct snd_ctl_elem_value *ucontrol)
3007 {
3008         struct usb_mixer_elem_info *elem = kctl->private_data;
3009         struct usb_mixer_interface *mixer = elem->head.mixer;
3010         struct scarlett2_data *private = mixer->private_data;
3011
3012         mutex_lock(&private->data_mutex);
3013         if (private->vol_updated)
3014                 scarlett2_update_volumes(mixer);
3015         mutex_unlock(&private->data_mutex);
3016
3017         ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
3018         return 0;
3019 }
3020
3021 static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
3022                                       struct snd_ctl_elem_value *ucontrol)
3023 {
3024         struct usb_mixer_elem_info *elem = kctl->private_data;
3025         struct usb_mixer_interface *mixer = elem->head.mixer;
3026         struct scarlett2_data *private = mixer->private_data;
3027         const struct scarlett2_device_info *info = private->info;
3028         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3029         int num_line_out =
3030                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3031
3032         int index = elem->control;
3033         int oval, val, err = 0, i;
3034
3035         mutex_lock(&private->data_mutex);
3036
3037         oval = private->dim_mute[index];
3038         val = !!ucontrol->value.integer.value[0];
3039
3040         if (oval == val)
3041                 goto unlock;
3042
3043         private->dim_mute[index] = val;
3044
3045         /* Send switch change to the device */
3046         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
3047                                        index, val);
3048         if (err == 0)
3049                 err = 1;
3050
3051         if (index == SCARLETT2_BUTTON_MUTE)
3052                 for (i = 0; i < num_line_out; i++) {
3053                         int line_index = line_out_remap(private, i);
3054
3055                         if (private->vol_sw_hw_switch[line_index]) {
3056                                 private->mute_switch[line_index] = val;
3057                                 snd_ctl_notify(mixer->chip->card,
3058                                                SNDRV_CTL_EVENT_MASK_VALUE,
3059                                                &private->mute_ctls[i]->id);
3060                         }
3061                 }
3062
3063 unlock:
3064         mutex_unlock(&private->data_mutex);
3065         return err;
3066 }
3067
3068 static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
3069         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3070         .name = "",
3071         .info = snd_ctl_boolean_mono_info,
3072         .get  = scarlett2_dim_mute_ctl_get,
3073         .put  = scarlett2_dim_mute_ctl_put
3074 };
3075
3076 /*** Create the analogue output controls ***/
3077
3078 static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
3079 {
3080         struct scarlett2_data *private = mixer->private_data;
3081         const struct scarlett2_device_info *info = private->info;
3082         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3083         int num_line_out =
3084                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3085         int err, i;
3086         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3087
3088         /* Add R/O HW volume control */
3089         if (info->line_out_hw_vol) {
3090                 snprintf(s, sizeof(s), "Master HW Playback Volume");
3091                 err = scarlett2_add_new_ctl(mixer,
3092                                             &scarlett2_master_volume_ctl,
3093                                             0, 1, s, &private->master_vol_ctl);
3094                 if (err < 0)
3095                         return err;
3096         }
3097
3098         /* Add volume controls */
3099         for (i = 0; i < num_line_out; i++) {
3100                 int index = line_out_remap(private, i);
3101
3102                 /* Fader */
3103                 if (info->line_out_descrs[i])
3104                         snprintf(s, sizeof(s),
3105                                  "Line %02d (%s) Playback Volume",
3106                                  i + 1, info->line_out_descrs[i]);
3107                 else
3108                         snprintf(s, sizeof(s),
3109                                  "Line %02d Playback Volume",
3110                                  i + 1);
3111                 err = scarlett2_add_new_ctl(mixer,
3112                                             &scarlett2_line_out_volume_ctl,
3113                                             i, 1, s, &private->vol_ctls[i]);
3114                 if (err < 0)
3115                         return err;
3116
3117                 /* Mute Switch */
3118                 snprintf(s, sizeof(s),
3119                          "Line %02d Mute Playback Switch",
3120                          i + 1);
3121                 err = scarlett2_add_new_ctl(mixer,
3122                                             &scarlett2_mute_ctl,
3123                                             i, 1, s,
3124                                             &private->mute_ctls[i]);
3125                 if (err < 0)
3126                         return err;
3127
3128                 /* Make the fader and mute controls read-only if the
3129                  * SW/HW switch is set to HW
3130                  */
3131                 if (private->vol_sw_hw_switch[index])
3132                         scarlett2_vol_ctl_set_writable(mixer, i, 0);
3133
3134                 /* SW/HW Switch */
3135                 if (info->line_out_hw_vol) {
3136                         snprintf(s, sizeof(s),
3137                                  "Line Out %02d Volume Control Playback Enum",
3138                                  i + 1);
3139                         err = scarlett2_add_new_ctl(mixer,
3140                                                     &scarlett2_sw_hw_enum_ctl,
3141                                                     i, 1, s,
3142                                                     &private->sw_hw_ctls[i]);
3143                         if (err < 0)
3144                                 return err;
3145
3146                         /* Make the switch read-only if the line is
3147                          * involved in speaker switching
3148                          */
3149                         if (private->speaker_switching_switch && i < 4)
3150                                 scarlett2_sw_hw_ctl_ro(private, i);
3151                 }
3152         }
3153
3154         /* Add dim/mute controls */
3155         if (info->line_out_hw_vol)
3156                 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
3157                         err = scarlett2_add_new_ctl(
3158                                 mixer, &scarlett2_dim_mute_ctl,
3159                                 i, 1, scarlett2_dim_mute_names[i],
3160                                 &private->dim_mute_ctls[i]);
3161                         if (err < 0)
3162                                 return err;
3163                 }
3164
3165         return 0;
3166 }
3167
3168 /*** Create the analogue input controls ***/
3169
3170 static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
3171 {
3172         struct scarlett2_data *private = mixer->private_data;
3173         const struct scarlett2_device_info *info = private->info;
3174         int err, i;
3175         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3176         const char *fmt = "Line In %d %s Capture %s";
3177         const char *fmt2 = "Line In %d-%d %s Capture %s";
3178
3179         /* Add input level (line/inst) controls */
3180         for (i = 0; i < info->level_input_count; i++) {
3181                 snprintf(s, sizeof(s), fmt, i + 1 + info->level_input_first,
3182                          "Level", "Enum");
3183                 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
3184                                             i, 1, s, &private->level_ctls[i]);
3185                 if (err < 0)
3186                         return err;
3187         }
3188
3189         /* Add input pad controls */
3190         for (i = 0; i < info->pad_input_count; i++) {
3191                 snprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
3192                 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
3193                                             i, 1, s, &private->pad_ctls[i]);
3194                 if (err < 0)
3195                         return err;
3196         }
3197
3198         /* Add input air controls */
3199         for (i = 0; i < info->air_input_count; i++) {
3200                 snprintf(s, sizeof(s), fmt, i + 1, "Air", "Switch");
3201                 err = scarlett2_add_new_ctl(mixer, &scarlett2_air_ctl,
3202                                             i, 1, s, &private->air_ctls[i]);
3203                 if (err < 0)
3204                         return err;
3205         }
3206
3207         /* Add input phantom controls */
3208         if (info->inputs_per_phantom == 1) {
3209                 for (i = 0; i < info->phantom_count; i++) {
3210                         scnprintf(s, sizeof(s), fmt, i + 1,
3211                                   "Phantom Power", "Switch");
3212                         err = scarlett2_add_new_ctl(
3213                                 mixer, &scarlett2_phantom_ctl,
3214                                 i, 1, s, &private->phantom_ctls[i]);
3215                         if (err < 0)
3216                                 return err;
3217                 }
3218         } else if (info->inputs_per_phantom > 1) {
3219                 for (i = 0; i < info->phantom_count; i++) {
3220                         int from = i * info->inputs_per_phantom + 1;
3221                         int to = (i + 1) * info->inputs_per_phantom;
3222
3223                         scnprintf(s, sizeof(s), fmt2, from, to,
3224                                   "Phantom Power", "Switch");
3225                         err = scarlett2_add_new_ctl(
3226                                 mixer, &scarlett2_phantom_ctl,
3227                                 i, 1, s, &private->phantom_ctls[i]);
3228                         if (err < 0)
3229                                 return err;
3230                 }
3231         }
3232         if (info->phantom_count) {
3233                 err = scarlett2_add_new_ctl(
3234                         mixer, &scarlett2_phantom_persistence_ctl, 0, 1,
3235                         "Phantom Power Persistence Capture Switch", NULL);
3236                 if (err < 0)
3237                         return err;
3238         }
3239
3240         return 0;
3241 }
3242
3243 /*** Mixer Volume Controls ***/
3244
3245 static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
3246                                     struct snd_ctl_elem_info *uinfo)
3247 {
3248         struct usb_mixer_elem_info *elem = kctl->private_data;
3249
3250         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3251         uinfo->count = elem->channels;
3252         uinfo->value.integer.min = 0;
3253         uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
3254         uinfo->value.integer.step = 1;
3255         return 0;
3256 }
3257
3258 static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
3259                                    struct snd_ctl_elem_value *ucontrol)
3260 {
3261         struct usb_mixer_elem_info *elem = kctl->private_data;
3262         struct scarlett2_data *private = elem->head.mixer->private_data;
3263
3264         ucontrol->value.integer.value[0] = private->mix[elem->control];
3265         return 0;
3266 }
3267
3268 static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
3269                                    struct snd_ctl_elem_value *ucontrol)
3270 {
3271         struct usb_mixer_elem_info *elem = kctl->private_data;
3272         struct usb_mixer_interface *mixer = elem->head.mixer;
3273         struct scarlett2_data *private = mixer->private_data;
3274         const struct scarlett2_device_info *info = private->info;
3275         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3276         int oval, val, num_mixer_in, mix_num, err = 0;
3277         int index = elem->control;
3278
3279         mutex_lock(&private->data_mutex);
3280
3281         oval = private->mix[index];
3282         val = ucontrol->value.integer.value[0];
3283         num_mixer_in = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
3284         mix_num = index / num_mixer_in;
3285
3286         if (oval == val)
3287                 goto unlock;
3288
3289         private->mix[index] = val;
3290         err = scarlett2_usb_set_mix(mixer, mix_num);
3291         if (err == 0)
3292                 err = 1;
3293
3294 unlock:
3295         mutex_unlock(&private->data_mutex);
3296         return err;
3297 }
3298
3299 static const DECLARE_TLV_DB_MINMAX(
3300         db_scale_scarlett2_mixer,
3301         SCARLETT2_MIXER_MIN_DB * 100,
3302         SCARLETT2_MIXER_MAX_DB * 100
3303 );
3304
3305 static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
3306         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3307         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3308                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3309         .name = "",
3310         .info = scarlett2_mixer_ctl_info,
3311         .get  = scarlett2_mixer_ctl_get,
3312         .put  = scarlett2_mixer_ctl_put,
3313         .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
3314         .tlv = { .p = db_scale_scarlett2_mixer }
3315 };
3316
3317 static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
3318 {
3319         struct scarlett2_data *private = mixer->private_data;
3320         const struct scarlett2_device_info *info = private->info;
3321         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3322         int err, i, j;
3323         int index;
3324         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3325
3326         int num_inputs =
3327                 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
3328         int num_outputs =
3329                 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3330
3331         for (i = 0, index = 0; i < num_outputs; i++)
3332                 for (j = 0; j < num_inputs; j++, index++) {
3333                         snprintf(s, sizeof(s),
3334                                  "Mix %c Input %02d Playback Volume",
3335                                  'A' + i, j + 1);
3336                         err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
3337                                                     index, 1, s, NULL);
3338                         if (err < 0)
3339                                 return err;
3340                 }
3341
3342         return 0;
3343 }
3344
3345 /*** Mux Source Selection Controls ***/
3346
3347 static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
3348                                            struct snd_ctl_elem_info *uinfo)
3349 {
3350         struct usb_mixer_elem_info *elem = kctl->private_data;
3351         struct scarlett2_data *private = elem->head.mixer->private_data;
3352         const struct scarlett2_device_info *info = private->info;
3353         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3354         unsigned int item = uinfo->value.enumerated.item;
3355         int items = private->num_mux_srcs;
3356         int port_type;
3357
3358         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3359         uinfo->count = elem->channels;
3360         uinfo->value.enumerated.items = items;
3361
3362         if (item >= items)
3363                 item = uinfo->value.enumerated.item = items - 1;
3364
3365         for (port_type = 0;
3366              port_type < SCARLETT2_PORT_TYPE_COUNT;
3367              port_type++) {
3368                 if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
3369                         const struct scarlett2_port *port =
3370                                 &scarlett2_ports[port_type];
3371
3372                         sprintf(uinfo->value.enumerated.name,
3373                                 port->src_descr, item + port->src_num_offset);
3374                         return 0;
3375                 }
3376                 item -= port_count[port_type][SCARLETT2_PORT_IN];
3377         }
3378
3379         return -EINVAL;
3380 }
3381
3382 static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
3383                                           struct snd_ctl_elem_value *ucontrol)
3384 {
3385         struct usb_mixer_elem_info *elem = kctl->private_data;
3386         struct usb_mixer_interface *mixer = elem->head.mixer;
3387         struct scarlett2_data *private = mixer->private_data;
3388         const struct scarlett2_device_info *info = private->info;
3389         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3390         int line_out_count =
3391                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3392         int index = elem->control;
3393
3394         if (index < line_out_count)
3395                 index = line_out_remap(private, index);
3396
3397         mutex_lock(&private->data_mutex);
3398         if (private->mux_updated)
3399                 scarlett2_usb_get_mux(mixer);
3400         ucontrol->value.enumerated.item[0] = private->mux[index];
3401         mutex_unlock(&private->data_mutex);
3402
3403         return 0;
3404 }
3405
3406 static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
3407                                           struct snd_ctl_elem_value *ucontrol)
3408 {
3409         struct usb_mixer_elem_info *elem = kctl->private_data;
3410         struct usb_mixer_interface *mixer = elem->head.mixer;
3411         struct scarlett2_data *private = mixer->private_data;
3412         const struct scarlett2_device_info *info = private->info;
3413         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3414         int line_out_count =
3415                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3416         int index = elem->control;
3417         int oval, val, err = 0;
3418
3419         if (index < line_out_count)
3420                 index = line_out_remap(private, index);
3421
3422         mutex_lock(&private->data_mutex);
3423
3424         oval = private->mux[index];
3425         val = min(ucontrol->value.enumerated.item[0],
3426                   private->num_mux_srcs - 1U);
3427
3428         if (oval == val)
3429                 goto unlock;
3430
3431         private->mux[index] = val;
3432         err = scarlett2_usb_set_mux(mixer);
3433         if (err == 0)
3434                 err = 1;
3435
3436 unlock:
3437         mutex_unlock(&private->data_mutex);
3438         return err;
3439 }
3440
3441 static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
3442         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3443         .name = "",
3444         .info = scarlett2_mux_src_enum_ctl_info,
3445         .get  = scarlett2_mux_src_enum_ctl_get,
3446         .put  = scarlett2_mux_src_enum_ctl_put,
3447 };
3448
3449 static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
3450 {
3451         struct scarlett2_data *private = mixer->private_data;
3452         const struct scarlett2_device_info *info = private->info;
3453         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3454         int port_type, channel, i;
3455
3456         for (i = 0, port_type = 0;
3457              port_type < SCARLETT2_PORT_TYPE_COUNT;
3458              port_type++) {
3459                 for (channel = 0;
3460                      channel < port_count[port_type][SCARLETT2_PORT_OUT];
3461                      channel++, i++) {
3462                         int err;
3463                         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3464                         const char *const descr =
3465                                 scarlett2_ports[port_type].dst_descr;
3466
3467                         snprintf(s, sizeof(s) - 5, descr, channel + 1);
3468                         strcat(s, " Enum");
3469
3470                         err = scarlett2_add_new_ctl(mixer,
3471                                                     &scarlett2_mux_src_enum_ctl,
3472                                                     i, 1, s,
3473                                                     &private->mux_ctls[i]);
3474                         if (err < 0)
3475                                 return err;
3476                 }
3477         }
3478
3479         return 0;
3480 }
3481
3482 /*** Meter Controls ***/
3483
3484 static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
3485                                     struct snd_ctl_elem_info *uinfo)
3486 {
3487         struct usb_mixer_elem_info *elem = kctl->private_data;
3488
3489         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3490         uinfo->count = elem->channels;
3491         uinfo->value.integer.min = 0;
3492         uinfo->value.integer.max = 4095;
3493         uinfo->value.integer.step = 1;
3494         return 0;
3495 }
3496
3497 static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
3498                                    struct snd_ctl_elem_value *ucontrol)
3499 {
3500         struct usb_mixer_elem_info *elem = kctl->private_data;
3501         u16 meter_levels[SCARLETT2_MAX_METERS];
3502         int i, err;
3503
3504         err = scarlett2_usb_get_meter_levels(elem->head.mixer, elem->channels,
3505                                              meter_levels);
3506         if (err < 0)
3507                 return err;
3508
3509         for (i = 0; i < elem->channels; i++)
3510                 ucontrol->value.integer.value[i] = meter_levels[i];
3511
3512         return 0;
3513 }
3514
3515 static const struct snd_kcontrol_new scarlett2_meter_ctl = {
3516         .iface = SNDRV_CTL_ELEM_IFACE_PCM,
3517         .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3518         .name = "",
3519         .info = scarlett2_meter_ctl_info,
3520         .get  = scarlett2_meter_ctl_get
3521 };
3522
3523 static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
3524 {
3525         struct scarlett2_data *private = mixer->private_data;
3526
3527         /* devices without a mixer also don't support reporting levels */
3528         if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
3529                 return 0;
3530
3531         return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
3532                                      0, private->num_mux_dsts,
3533                                      "Level Meter", NULL);
3534 }
3535
3536 /*** MSD Controls ***/
3537
3538 static int scarlett2_msd_ctl_get(struct snd_kcontrol *kctl,
3539                                  struct snd_ctl_elem_value *ucontrol)
3540 {
3541         struct usb_mixer_elem_info *elem = kctl->private_data;
3542         struct scarlett2_data *private = elem->head.mixer->private_data;
3543
3544         ucontrol->value.integer.value[0] = private->msd_switch;
3545         return 0;
3546 }
3547
3548 static int scarlett2_msd_ctl_put(struct snd_kcontrol *kctl,
3549                                  struct snd_ctl_elem_value *ucontrol)
3550 {
3551         struct usb_mixer_elem_info *elem = kctl->private_data;
3552         struct usb_mixer_interface *mixer = elem->head.mixer;
3553         struct scarlett2_data *private = mixer->private_data;
3554
3555         int oval, val, err = 0;
3556
3557         mutex_lock(&private->data_mutex);
3558
3559         oval = private->msd_switch;
3560         val = !!ucontrol->value.integer.value[0];
3561
3562         if (oval == val)
3563                 goto unlock;
3564
3565         private->msd_switch = val;
3566
3567         /* Send switch change to the device */
3568         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MSD_SWITCH,
3569                                        0, val);
3570         if (err == 0)
3571                 err = 1;
3572
3573 unlock:
3574         mutex_unlock(&private->data_mutex);
3575         return err;
3576 }
3577
3578 static const struct snd_kcontrol_new scarlett2_msd_ctl = {
3579         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3580         .name = "",
3581         .info = snd_ctl_boolean_mono_info,
3582         .get  = scarlett2_msd_ctl_get,
3583         .put  = scarlett2_msd_ctl_put,
3584 };
3585
3586 static int scarlett2_add_msd_ctl(struct usb_mixer_interface *mixer)
3587 {
3588         struct scarlett2_data *private = mixer->private_data;
3589         const struct scarlett2_device_info *info = private->info;
3590
3591         if (!info->has_msd_mode)
3592                 return 0;
3593
3594         /* If MSD mode is off, hide the switch by default */
3595         if (!private->msd_switch && !(mixer->chip->setup & SCARLETT2_MSD_ENABLE))
3596                 return 0;
3597
3598         /* Add MSD control */
3599         return scarlett2_add_new_ctl(mixer, &scarlett2_msd_ctl,
3600                                      0, 1, "MSD Mode Switch", NULL);
3601 }
3602
3603 /*** Standalone Control ***/
3604
3605 static int scarlett2_standalone_ctl_get(struct snd_kcontrol *kctl,
3606                                         struct snd_ctl_elem_value *ucontrol)
3607 {
3608         struct usb_mixer_elem_info *elem = kctl->private_data;
3609         struct scarlett2_data *private = elem->head.mixer->private_data;
3610
3611         ucontrol->value.integer.value[0] = private->standalone_switch;
3612         return 0;
3613 }
3614
3615 static int scarlett2_standalone_ctl_put(struct snd_kcontrol *kctl,
3616                                         struct snd_ctl_elem_value *ucontrol)
3617 {
3618         struct usb_mixer_elem_info *elem = kctl->private_data;
3619         struct usb_mixer_interface *mixer = elem->head.mixer;
3620         struct scarlett2_data *private = mixer->private_data;
3621
3622         int oval, val, err = 0;
3623
3624         mutex_lock(&private->data_mutex);
3625
3626         oval = private->standalone_switch;
3627         val = !!ucontrol->value.integer.value[0];
3628
3629         if (oval == val)
3630                 goto unlock;
3631
3632         private->standalone_switch = val;
3633
3634         /* Send switch change to the device */
3635         err = scarlett2_usb_set_config(mixer,
3636                                        SCARLETT2_CONFIG_STANDALONE_SWITCH,
3637                                        0, val);
3638         if (err == 0)
3639                 err = 1;
3640
3641 unlock:
3642         mutex_unlock(&private->data_mutex);
3643         return err;
3644 }
3645
3646 static const struct snd_kcontrol_new scarlett2_standalone_ctl = {
3647         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3648         .name = "",
3649         .info = snd_ctl_boolean_mono_info,
3650         .get  = scarlett2_standalone_ctl_get,
3651         .put  = scarlett2_standalone_ctl_put,
3652 };
3653
3654 static int scarlett2_add_standalone_ctl(struct usb_mixer_interface *mixer)
3655 {
3656         struct scarlett2_data *private = mixer->private_data;
3657
3658         if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
3659                 return 0;
3660
3661         /* Add standalone control */
3662         return scarlett2_add_new_ctl(mixer, &scarlett2_standalone_ctl,
3663                                      0, 1, "Standalone Switch", NULL);
3664 }
3665
3666 /*** Cleanup/Suspend Callbacks ***/
3667
3668 static void scarlett2_private_free(struct usb_mixer_interface *mixer)
3669 {
3670         struct scarlett2_data *private = mixer->private_data;
3671
3672         cancel_delayed_work_sync(&private->work);
3673         kfree(private);
3674         mixer->private_data = NULL;
3675 }
3676
3677 static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
3678 {
3679         struct scarlett2_data *private = mixer->private_data;
3680
3681         if (cancel_delayed_work_sync(&private->work))
3682                 scarlett2_config_save(private->mixer);
3683 }
3684
3685 /*** Initialisation ***/
3686
3687 static void scarlett2_count_mux_io(struct scarlett2_data *private)
3688 {
3689         const struct scarlett2_device_info *info = private->info;
3690         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3691         int port_type, srcs = 0, dsts = 0;
3692
3693         for (port_type = 0;
3694              port_type < SCARLETT2_PORT_TYPE_COUNT;
3695              port_type++) {
3696                 srcs += port_count[port_type][SCARLETT2_PORT_IN];
3697                 dsts += port_count[port_type][SCARLETT2_PORT_OUT];
3698         }
3699
3700         private->num_mux_srcs = srcs;
3701         private->num_mux_dsts = dsts;
3702 }
3703
3704 /* Look through the interface descriptors for the Focusrite Control
3705  * interface (bInterfaceClass = 255 Vendor Specific Class) and set
3706  * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
3707  * in private
3708  */
3709 static int scarlett2_find_fc_interface(struct usb_device *dev,
3710                                        struct scarlett2_data *private)
3711 {
3712         struct usb_host_config *config = dev->actconfig;
3713         int i;
3714
3715         for (i = 0; i < config->desc.bNumInterfaces; i++) {
3716                 struct usb_interface *intf = config->interface[i];
3717                 struct usb_interface_descriptor *desc =
3718                         &intf->altsetting[0].desc;
3719                 struct usb_endpoint_descriptor *epd;
3720
3721                 if (desc->bInterfaceClass != 255)
3722                         continue;
3723
3724                 epd = get_endpoint(intf->altsetting, 0);
3725                 private->bInterfaceNumber = desc->bInterfaceNumber;
3726                 private->bEndpointAddress = epd->bEndpointAddress &
3727                         USB_ENDPOINT_NUMBER_MASK;
3728                 private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
3729                 private->bInterval = epd->bInterval;
3730                 return 0;
3731         }
3732
3733         return -EINVAL;
3734 }
3735
3736 /* Initialise private data */
3737 static int scarlett2_init_private(struct usb_mixer_interface *mixer,
3738                                   const struct scarlett2_device_info *info)
3739 {
3740         struct scarlett2_data *private =
3741                 kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);
3742
3743         if (!private)
3744                 return -ENOMEM;
3745
3746         mutex_init(&private->usb_mutex);
3747         mutex_init(&private->data_mutex);
3748         INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
3749
3750         mixer->private_data = private;
3751         mixer->private_free = scarlett2_private_free;
3752         mixer->private_suspend = scarlett2_private_suspend;
3753
3754         private->info = info;
3755         scarlett2_count_mux_io(private);
3756         private->scarlett2_seq = 0;
3757         private->mixer = mixer;
3758
3759         return scarlett2_find_fc_interface(mixer->chip->dev, private);
3760 }
3761
3762 /* Cargo cult proprietary initialisation sequence */
3763 static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
3764 {
3765         struct usb_device *dev = mixer->chip->dev;
3766         struct scarlett2_data *private = mixer->private_data;
3767         u8 buf[24];
3768         int err;
3769
3770         if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
3771                 return -EINVAL;
3772
3773         /* step 0 */
3774         err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
3775                                SCARLETT2_USB_CMD_INIT, buf, sizeof(buf));
3776         if (err < 0)
3777                 return err;
3778
3779         /* step 1 */
3780         private->scarlett2_seq = 1;
3781         err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
3782         if (err < 0)
3783                 return err;
3784
3785         /* step 2 */
3786         private->scarlett2_seq = 1;
3787         return scarlett2_usb(mixer, SCARLETT2_USB_INIT_2, NULL, 0, NULL, 84);
3788 }
3789
3790 /* Read configuration from the interface on start */
3791 static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
3792 {
3793         struct scarlett2_data *private = mixer->private_data;
3794         const struct scarlett2_device_info *info = private->info;
3795         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3796         int num_line_out =
3797                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3798         int num_mixer_out =
3799                 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3800         struct scarlett2_usb_volume_status volume_status;
3801         int err, i;
3802
3803         if (info->has_msd_mode) {
3804                 err = scarlett2_usb_get_config(
3805                         mixer, SCARLETT2_CONFIG_MSD_SWITCH,
3806                         1, &private->msd_switch);
3807                 if (err < 0)
3808                         return err;
3809
3810                 /* no other controls are created if MSD mode is on */
3811                 if (private->msd_switch)
3812                         return 0;
3813         }
3814
3815         err = scarlett2_update_input_other(mixer);
3816         if (err < 0)
3817                 return err;
3818
3819         err = scarlett2_update_monitor_other(mixer);
3820         if (err < 0)
3821                 return err;
3822
3823         /* the rest of the configuration is for devices with a mixer */
3824         if (info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
3825                 return 0;
3826
3827         err = scarlett2_usb_get_config(
3828                 mixer, SCARLETT2_CONFIG_STANDALONE_SWITCH,
3829                 1, &private->standalone_switch);
3830         if (err < 0)
3831                 return err;
3832
3833         err = scarlett2_update_sync(mixer);
3834         if (err < 0)
3835                 return err;
3836
3837         err = scarlett2_usb_get_volume_status(mixer, &volume_status);
3838         if (err < 0)
3839                 return err;
3840
3841         if (info->line_out_hw_vol)
3842                 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
3843                         private->dim_mute[i] = !!volume_status.dim_mute[i];
3844
3845         private->master_vol = clamp(
3846                 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
3847                 0, SCARLETT2_VOLUME_BIAS);
3848
3849         for (i = 0; i < num_line_out; i++) {
3850                 int volume, mute;
3851
3852                 private->vol_sw_hw_switch[i] =
3853                         info->line_out_hw_vol
3854                                 && volume_status.sw_hw_switch[i];
3855
3856                 volume = private->vol_sw_hw_switch[i]
3857                            ? volume_status.master_vol
3858                            : volume_status.sw_vol[i];
3859                 volume = clamp(volume + SCARLETT2_VOLUME_BIAS,
3860                                0, SCARLETT2_VOLUME_BIAS);
3861                 private->vol[i] = volume;
3862
3863                 mute = private->vol_sw_hw_switch[i]
3864                          ? private->dim_mute[SCARLETT2_BUTTON_MUTE]
3865                          : volume_status.mute_switch[i];
3866                 private->mute_switch[i] = mute;
3867         }
3868
3869         for (i = 0; i < num_mixer_out; i++) {
3870                 err = scarlett2_usb_get_mix(mixer, i);
3871                 if (err < 0)
3872                         return err;
3873         }
3874
3875         return scarlett2_usb_get_mux(mixer);
3876 }
3877
3878 /* Notify on sync change */
3879 static void scarlett2_notify_sync(
3880         struct usb_mixer_interface *mixer)
3881 {
3882         struct scarlett2_data *private = mixer->private_data;
3883
3884         private->sync_updated = 1;
3885
3886         snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3887                        &private->sync_ctl->id);
3888 }
3889
3890 /* Notify on monitor change */
3891 static void scarlett2_notify_monitor(
3892         struct usb_mixer_interface *mixer)
3893 {
3894         struct snd_card *card = mixer->chip->card;
3895         struct scarlett2_data *private = mixer->private_data;
3896         const struct scarlett2_device_info *info = private->info;
3897         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3898         int num_line_out =
3899                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3900         int i;
3901
3902         /* if line_out_hw_vol is 0, there are no controls to update */
3903         if (!info->line_out_hw_vol)
3904                 return;
3905
3906         private->vol_updated = 1;
3907
3908         snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3909                        &private->master_vol_ctl->id);
3910
3911         for (i = 0; i < num_line_out; i++)
3912                 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
3913                         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3914                                        &private->vol_ctls[i]->id);
3915 }
3916
3917 /* Notify on dim/mute change */
3918 static void scarlett2_notify_dim_mute(
3919         struct usb_mixer_interface *mixer)
3920 {
3921         struct snd_card *card = mixer->chip->card;
3922         struct scarlett2_data *private = mixer->private_data;
3923         const struct scarlett2_device_info *info = private->info;
3924         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3925         int num_line_out =
3926                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3927         int i;
3928
3929         private->vol_updated = 1;
3930
3931         if (!info->line_out_hw_vol)
3932                 return;
3933
3934         for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
3935                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3936                                &private->dim_mute_ctls[i]->id);
3937
3938         for (i = 0; i < num_line_out; i++)
3939                 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
3940                         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3941                                        &private->mute_ctls[i]->id);
3942 }
3943
3944 /* Notify on "input other" change (level/pad/air) */
3945 static void scarlett2_notify_input_other(
3946         struct usb_mixer_interface *mixer)
3947 {
3948         struct snd_card *card = mixer->chip->card;
3949         struct scarlett2_data *private = mixer->private_data;
3950         const struct scarlett2_device_info *info = private->info;
3951         int i;
3952
3953         private->input_other_updated = 1;
3954
3955         for (i = 0; i < info->level_input_count; i++)
3956                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3957                                &private->level_ctls[i]->id);
3958         for (i = 0; i < info->pad_input_count; i++)
3959                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3960                                &private->pad_ctls[i]->id);
3961         for (i = 0; i < info->air_input_count; i++)
3962                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3963                                &private->air_ctls[i]->id);
3964         for (i = 0; i < info->phantom_count; i++)
3965                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3966                                &private->phantom_ctls[i]->id);
3967 }
3968
3969 /* Notify on "monitor other" change (direct monitor, speaker
3970  * switching, talkback)
3971  */
3972 static void scarlett2_notify_monitor_other(
3973         struct usb_mixer_interface *mixer)
3974 {
3975         struct snd_card *card = mixer->chip->card;
3976         struct scarlett2_data *private = mixer->private_data;
3977         const struct scarlett2_device_info *info = private->info;
3978
3979         private->monitor_other_updated = 1;
3980
3981         if (info->direct_monitor) {
3982                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3983                                &private->direct_monitor_ctl->id);
3984                 return;
3985         }
3986
3987         if (info->has_speaker_switching)
3988                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3989                                &private->speaker_switching_ctl->id);
3990
3991         if (info->has_talkback)
3992                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3993                                &private->talkback_ctl->id);
3994
3995         /* if speaker switching was recently enabled or disabled,
3996          * invalidate the dim/mute and mux enum controls
3997          */
3998         if (private->speaker_switching_switched) {
3999                 int i;
4000
4001                 scarlett2_notify_dim_mute(mixer);
4002
4003                 private->speaker_switching_switched = 0;
4004                 private->mux_updated = 1;
4005
4006                 for (i = 0; i < private->num_mux_dsts; i++)
4007                         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4008                                        &private->mux_ctls[i]->id);
4009         }
4010 }
4011
4012 /* Interrupt callback */
4013 static void scarlett2_notify(struct urb *urb)
4014 {
4015         struct usb_mixer_interface *mixer = urb->context;
4016         int len = urb->actual_length;
4017         int ustatus = urb->status;
4018         u32 data;
4019
4020         if (ustatus != 0 || len != 8)
4021                 goto requeue;
4022
4023         data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
4024         if (data & SCARLETT2_USB_NOTIFY_SYNC)
4025                 scarlett2_notify_sync(mixer);
4026         if (data & SCARLETT2_USB_NOTIFY_MONITOR)
4027                 scarlett2_notify_monitor(mixer);
4028         if (data & SCARLETT2_USB_NOTIFY_DIM_MUTE)
4029                 scarlett2_notify_dim_mute(mixer);
4030         if (data & SCARLETT2_USB_NOTIFY_INPUT_OTHER)
4031                 scarlett2_notify_input_other(mixer);
4032         if (data & SCARLETT2_USB_NOTIFY_MONITOR_OTHER)
4033                 scarlett2_notify_monitor_other(mixer);
4034
4035 requeue:
4036         if (ustatus != -ENOENT &&
4037             ustatus != -ECONNRESET &&
4038             ustatus != -ESHUTDOWN) {
4039                 urb->dev = mixer->chip->dev;
4040                 usb_submit_urb(urb, GFP_ATOMIC);
4041         }
4042 }
4043
4044 static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
4045 {
4046         struct usb_device *dev = mixer->chip->dev;
4047         struct scarlett2_data *private = mixer->private_data;
4048         unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
4049         void *transfer_buffer;
4050
4051         if (mixer->urb) {
4052                 usb_audio_err(mixer->chip,
4053                               "%s: mixer urb already in use!\n", __func__);
4054                 return 0;
4055         }
4056
4057         if (usb_pipe_type_check(dev, pipe))
4058                 return -EINVAL;
4059
4060         mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
4061         if (!mixer->urb)
4062                 return -ENOMEM;
4063
4064         transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
4065         if (!transfer_buffer)
4066                 return -ENOMEM;
4067
4068         usb_fill_int_urb(mixer->urb, dev, pipe,
4069                          transfer_buffer, private->wMaxPacketSize,
4070                          scarlett2_notify, mixer, private->bInterval);
4071
4072         return usb_submit_urb(mixer->urb, GFP_KERNEL);
4073 }
4074
4075 static int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer)
4076 {
4077         const struct scarlett2_device_info **info = scarlett2_devices;
4078         int err;
4079
4080         /* Find device in scarlett2_devices */
4081         while (*info && (*info)->usb_id != mixer->chip->usb_id)
4082                 info++;
4083         if (!*info)
4084                 return -EINVAL;
4085
4086         /* Initialise private data */
4087         err = scarlett2_init_private(mixer, *info);
4088         if (err < 0)
4089                 return err;
4090
4091         /* Send proprietary USB initialisation sequence */
4092         err = scarlett2_usb_init(mixer);
4093         if (err < 0)
4094                 return err;
4095
4096         /* Read volume levels and controls from the interface */
4097         err = scarlett2_read_configs(mixer);
4098         if (err < 0)
4099                 return err;
4100
4101         /* Create the MSD control */
4102         err = scarlett2_add_msd_ctl(mixer);
4103         if (err < 0)
4104                 return err;
4105
4106         /* If MSD mode is enabled, don't create any other controls */
4107         if (((struct scarlett2_data *)mixer->private_data)->msd_switch)
4108                 return 0;
4109
4110         /* Create the analogue output controls */
4111         err = scarlett2_add_line_out_ctls(mixer);
4112         if (err < 0)
4113                 return err;
4114
4115         /* Create the analogue input controls */
4116         err = scarlett2_add_line_in_ctls(mixer);
4117         if (err < 0)
4118                 return err;
4119
4120         /* Create the input, output, and mixer mux input selections */
4121         err = scarlett2_add_mux_enums(mixer);
4122         if (err < 0)
4123                 return err;
4124
4125         /* Create the matrix mixer controls */
4126         err = scarlett2_add_mixer_ctls(mixer);
4127         if (err < 0)
4128                 return err;
4129
4130         /* Create the level meter controls */
4131         err = scarlett2_add_meter_ctl(mixer);
4132         if (err < 0)
4133                 return err;
4134
4135         /* Create the sync control */
4136         err = scarlett2_add_sync_ctl(mixer);
4137         if (err < 0)
4138                 return err;
4139
4140         /* Create the direct monitor control */
4141         err = scarlett2_add_direct_monitor_ctl(mixer);
4142         if (err < 0)
4143                 return err;
4144
4145         /* Create the speaker switching control */
4146         err = scarlett2_add_speaker_switch_ctl(mixer);
4147         if (err < 0)
4148                 return err;
4149
4150         /* Create the talkback controls */
4151         err = scarlett2_add_talkback_ctls(mixer);
4152         if (err < 0)
4153                 return err;
4154
4155         /* Create the standalone control */
4156         err = scarlett2_add_standalone_ctl(mixer);
4157         if (err < 0)
4158                 return err;
4159
4160         /* Set up the interrupt polling */
4161         err = scarlett2_init_notify(mixer);
4162         if (err < 0)
4163                 return err;
4164
4165         return 0;
4166 }
4167
4168 int snd_scarlett_gen2_init(struct usb_mixer_interface *mixer)
4169 {
4170         struct snd_usb_audio *chip = mixer->chip;
4171         int err;
4172
4173         /* only use UAC_VERSION_2 */
4174         if (!mixer->protocol)
4175                 return 0;
4176
4177         if (!(chip->setup & SCARLETT2_ENABLE)) {
4178                 usb_audio_info(chip,
4179                         "Focusrite Scarlett Gen 2/3 Mixer Driver disabled; "
4180                         "use options snd_usb_audio vid=0x%04x pid=0x%04x "
4181                         "device_setup=1 to enable and report any issues "
4182                         "to g@b4.vu",
4183                         USB_ID_VENDOR(chip->usb_id),
4184                         USB_ID_PRODUCT(chip->usb_id));
4185                 return 0;
4186         }
4187
4188         usb_audio_info(chip,
4189                 "Focusrite Scarlett Gen 2/3 Mixer Driver enabled pid=0x%04x",
4190                 USB_ID_PRODUCT(chip->usb_id));
4191
4192         err = snd_scarlett_gen2_controls_create(mixer);
4193         if (err < 0)
4194                 usb_audio_err(mixer->chip,
4195                               "Error initialising Scarlett Mixer Driver: %d",
4196                               err);
4197
4198         return err;
4199 }