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