d742c939eed0b067163d7e59942fcf6e7b11b36b
[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  *   - 4i4/8i6/18i8/18i20 Gen 3
8  *
9  *   Copyright (c) 2018-2021 by Geoffrey D. Bennett <g at b4.vu>
10  *
11  *   Based on the Scarlett (Gen 1) Driver for ALSA:
12  *
13  *   Copyright (c) 2013 by Tobias Hoffmann
14  *   Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
15  *   Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
16  *   Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
17  *
18  *   Many codes borrowed from audio.c by
19  *     Alan Cox (alan at lxorguk.ukuu.org.uk)
20  *     Thomas Sailer (sailer at ife.ee.ethz.ch)
21  *
22  *   Code cleanup:
23  *   David Henningsson <david.henningsson at canonical.com>
24  */
25
26 /* The protocol was reverse engineered by looking at the communication
27  * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20
28  * (firmware 1083) using usbmon in July-August 2018.
29  *
30  * Scarlett 18i8 support added in April 2019.
31  *
32  * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
33  * for providing usbmon output and testing).
34  *
35  * Scarlett 4i4/8i6 Gen 3 support added in May 2020 (thanks to Laurent
36  * Debricon for donating a 4i4 and to Fredrik Unger for providing 8i6
37  * usbmon output and testing).
38  *
39  * Scarlett 18i8/18i20 Gen 3 support added in June 2020 (thanks to
40  * Darren Jaeckel, Alex Sedlack, and Clovis Lunel for providing usbmon
41  * output, protocol traces and testing).
42  *
43  * Support for loading mixer volume and mux configuration from the
44  * interface during driver initialisation added in May 2021 (thanks to
45  * Vladimir Sadovnikov for figuring out how).
46  *
47  * This ALSA mixer gives access to:
48  *  - input, output, mixer-matrix muxes
49  *  - mixer-matrix gain stages
50  *  - gain/volume/mute controls
51  *  - level meters
52  *  - line/inst level and pad controls
53  *
54  * <ditaa>
55  *    /--------------\    18chn            20chn     /--------------\
56  *    | Hardware  in +--+------\    /-------------+--+ ALSA PCM out |
57  *    \--------------/  |      |    |             |  \--------------/
58  *                      |      |    |    /-----\  |
59  *                      |      |    |    |     |  |
60  *                      |      v    v    v     |  |
61  *                      |   +---------------+  |  |
62  *                      |    \ Matrix  Mux /   |  |
63  *                      |     +-----+-----+    |  |
64  *                      |           |          |  |
65  *                      |           |18chn     |  |
66  *                      |           |          |  |
67  *                      |           |     10chn|  |
68  *                      |           v          |  |
69  *                      |     +------------+   |  |
70  *                      |     | Mixer      |   |  |
71  *                      |     |     Matrix |   |  |
72  *                      |     |            |   |  |
73  *                      |     | 18x10 Gain |   |  |
74  *                      |     |   stages   |   |  |
75  *                      |     +-----+------+   |  |
76  *                      |           |          |  |
77  *                      |18chn      |10chn     |  |20chn
78  *                      |           |          |  |
79  *                      |           +----------/  |
80  *                      |           |             |
81  *                      v           v             v
82  *                      ===========================
83  *               +---------------+       +--—------------+
84  *                \ Output  Mux /         \ Capture Mux /
85  *                 +---+---+---+           +-----+-----+
86  *                     |   |                     |
87  *                10chn|   |                     |18chn
88  *                     |   |                     |
89  *  /--------------\   |   |                     |   /--------------\
90  *  | S/PDIF, ADAT |<--/   |10chn                \-->| ALSA PCM in  |
91  *  | Hardware out |       |                         \--------------/
92  *  \--------------/       |
93  *                         v
94  *                  +-------------+    Software gain per channel.
95  *                  | Master Gain |<-- 18i20 only: Switch per channel
96  *                  +------+------+    to select HW or SW gain control.
97  *                         |
98  *                         |10chn
99  *  /--------------\       |
100  *  | Analogue     |<------/
101  *  | Hardware out |
102  *  \--------------/
103  * </ditaa>
104  *
105  */
106
107 #include <linux/slab.h>
108 #include <linux/usb.h>
109 #include <linux/moduleparam.h>
110
111 #include <sound/control.h>
112 #include <sound/tlv.h>
113
114 #include "usbaudio.h"
115 #include "mixer.h"
116 #include "helper.h"
117
118 #include "mixer_scarlett_gen2.h"
119
120 /* device_setup value to enable */
121 #define SCARLETT2_ENABLE 0x01
122
123 /* some gui mixers can't handle negative ctl values */
124 #define SCARLETT2_VOLUME_BIAS 127
125
126 /* mixer range from -80dB to +6dB in 0.5dB steps */
127 #define SCARLETT2_MIXER_MIN_DB -80
128 #define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
129 #define SCARLETT2_MIXER_MAX_DB 6
130 #define SCARLETT2_MIXER_MAX_VALUE \
131         ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
132 #define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1)
133
134 /* map from (dB + 80) * 2 to mixer value
135  * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
136  */
137 static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = {
138         0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
139         2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
140         9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
141         23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
142         54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
143         122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
144         244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
145         487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
146         973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
147         1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
148         3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
149         5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
150         9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
151         16345
152 };
153
154 /* Maximum number of analogue outputs */
155 #define SCARLETT2_ANALOGUE_MAX 10
156
157 /* Maximum number of level and pad switches */
158 #define SCARLETT2_LEVEL_SWITCH_MAX 2
159 #define SCARLETT2_PAD_SWITCH_MAX 8
160
161 /* Maximum number of inputs to the mixer */
162 #define SCARLETT2_INPUT_MIX_MAX 25
163
164 /* Maximum number of outputs from the mixer */
165 #define SCARLETT2_OUTPUT_MIX_MAX 12
166
167 /* Maximum size of the data in the USB mux assignment message:
168  * 20 inputs, 20 outputs, 25 matrix inputs, 12 spare
169  */
170 #define SCARLETT2_MUX_MAX 77
171
172 /* Maximum number of meters (sum of output port counts) */
173 #define SCARLETT2_MAX_METERS 65
174
175 /* Hardware port types:
176  * - None (no input to mux)
177  * - Analogue I/O
178  * - S/PDIF I/O
179  * - ADAT I/O
180  * - Mixer I/O
181  * - PCM I/O
182  */
183 enum {
184         SCARLETT2_PORT_TYPE_NONE     = 0,
185         SCARLETT2_PORT_TYPE_ANALOGUE = 1,
186         SCARLETT2_PORT_TYPE_SPDIF    = 2,
187         SCARLETT2_PORT_TYPE_ADAT     = 3,
188         SCARLETT2_PORT_TYPE_MIX      = 4,
189         SCARLETT2_PORT_TYPE_PCM      = 5,
190         SCARLETT2_PORT_TYPE_COUNT    = 6,
191 };
192
193 /* I/O count of each port type kept in struct scarlett2_ports */
194 enum {
195         SCARLETT2_PORT_IN    = 0,
196         SCARLETT2_PORT_OUT   = 1,
197         SCARLETT2_PORT_DIRNS = 2,
198 };
199
200 /* Dim/Mute buttons on the 18i20 */
201 enum {
202         SCARLETT2_BUTTON_MUTE    = 0,
203         SCARLETT2_BUTTON_DIM     = 1,
204         SCARLETT2_DIM_MUTE_COUNT = 2,
205 };
206
207 static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
208         "Mute", "Dim"
209 };
210
211 /* Description of each hardware port type:
212  * - id: hardware ID of this port type
213  * - src_descr: printf format string for mux input selections
214  * - src_num_offset: added to channel number for the fprintf
215  * - dst_descr: printf format string for mixer controls
216  */
217 struct scarlett2_port {
218         u16 id;
219         const char * const src_descr;
220         int src_num_offset;
221         const char * const dst_descr;
222 };
223
224 static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
225         [SCARLETT2_PORT_TYPE_NONE] = {
226                 .id = 0x000,
227                 .src_descr = "Off"
228         },
229         [SCARLETT2_PORT_TYPE_ANALOGUE] = {
230                 .id = 0x080,
231                 .src_descr = "Analogue %d",
232                 .src_num_offset = 1,
233                 .dst_descr = "Analogue Output %02d Playback"
234         },
235         [SCARLETT2_PORT_TYPE_SPDIF] = {
236                 .id = 0x180,
237                 .src_descr = "S/PDIF %d",
238                 .src_num_offset = 1,
239                 .dst_descr = "S/PDIF Output %d Playback"
240         },
241         [SCARLETT2_PORT_TYPE_ADAT] = {
242                 .id = 0x200,
243                 .src_descr = "ADAT %d",
244                 .src_num_offset = 1,
245                 .dst_descr = "ADAT Output %d Playback"
246         },
247         [SCARLETT2_PORT_TYPE_MIX] = {
248                 .id = 0x300,
249                 .src_descr = "Mix %c",
250                 .src_num_offset = 'A',
251                 .dst_descr = "Mixer Input %02d Capture"
252         },
253         [SCARLETT2_PORT_TYPE_PCM] = {
254                 .id = 0x600,
255                 .src_descr = "PCM %d",
256                 .src_num_offset = 1,
257                 .dst_descr = "PCM %02d Capture"
258         },
259 };
260
261 /* Number of mux tables: one for each band of sample rates
262  * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
263  */
264 #define SCARLETT2_MUX_TABLES 3
265
266 /* Maximum number of entries in a mux table */
267 #define SCARLETT2_MAX_MUX_ENTRIES 10
268
269 /* One entry within mux_assignment defines the port type and range of
270  * ports to add to the set_mux message. The end of the list is marked
271  * with count == 0.
272  */
273 struct scarlett2_mux_entry {
274         u8 port_type;
275         u8 start;
276         u8 count;
277 };
278
279 struct scarlett2_device_info {
280         u32 usb_id; /* USB device identifier */
281
282         /* line out hw volume is sw controlled */
283         u8 line_out_hw_vol;
284
285         /* the number of analogue inputs with a software switchable
286          * level control that can be set to line or instrument
287          */
288         u8 level_input_count;
289
290         /* the number of analogue inputs with a software switchable
291          * 10dB pad control
292          */
293         u8 pad_input_count;
294
295         /* additional description for the line out volume controls */
296         const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
297
298         /* number of sources/destinations of each port type */
299         const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];
300
301         /* layout/order of the entries in the set_mux message */
302         struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES]
303                                                  [SCARLETT2_MAX_MUX_ENTRIES];
304 };
305
306 struct scarlett2_data {
307         struct usb_mixer_interface *mixer;
308         struct mutex usb_mutex; /* prevent sending concurrent USB requests */
309         struct mutex data_mutex; /* lock access to this data */
310         struct delayed_work work;
311         const struct scarlett2_device_info *info;
312         __u8 bInterfaceNumber;
313         __u8 bEndpointAddress;
314         __u16 wMaxPacketSize;
315         __u8 bInterval;
316         int num_mux_srcs;
317         int num_mux_dsts;
318         u16 scarlett2_seq;
319         u8 sync_updated;
320         u8 vol_updated;
321         u8 sync;
322         u8 master_vol;
323         u8 vol[SCARLETT2_ANALOGUE_MAX];
324         u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
325         u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
326         u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
327         u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
328         u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
329         struct snd_kcontrol *sync_ctl;
330         struct snd_kcontrol *master_vol_ctl;
331         struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
332         struct snd_kcontrol *mute_ctls[SCARLETT2_ANALOGUE_MAX];
333         struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT];
334         u8 mux[SCARLETT2_MUX_MAX];
335         u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX];
336 };
337
338 /*** Model-specific data ***/
339
340 static const struct scarlett2_device_info s6i6_gen2_info = {
341         .usb_id = USB_ID(0x1235, 0x8203),
342
343         .level_input_count = 2,
344         .pad_input_count = 2,
345
346         .line_out_descrs = {
347                 "Headphones 1 L",
348                 "Headphones 1 R",
349                 "Headphones 2 L",
350                 "Headphones 2 R",
351         },
352
353         .port_count = {
354                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
355                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  4,  4 },
356                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
357                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
358                 [SCARLETT2_PORT_TYPE_PCM]      = {  6,  6 },
359         },
360
361         .mux_assignment = { {
362                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
363                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
364                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
365                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
366                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
367                 { 0,                            0,  0 },
368         }, {
369                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
370                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
371                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
372                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
373                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
374                 { 0,                            0,  0 },
375         }, {
376                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
377                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
378                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
379                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
380                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
381                 { 0,                            0,  0 },
382         } },
383 };
384
385 static const struct scarlett2_device_info s18i8_gen2_info = {
386         .usb_id = USB_ID(0x1235, 0x8204),
387
388         .level_input_count = 2,
389         .pad_input_count = 4,
390
391         .line_out_descrs = {
392                 "Monitor L",
393                 "Monitor R",
394                 "Headphones 1 L",
395                 "Headphones 1 R",
396                 "Headphones 2 L",
397                 "Headphones 2 R",
398         },
399
400         .port_count = {
401                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
402                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  8,  6 },
403                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
404                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  0 },
405                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
406                 [SCARLETT2_PORT_TYPE_PCM]      = {  8, 18 },
407         },
408
409         .mux_assignment = { {
410                 { SCARLETT2_PORT_TYPE_PCM,      0, 18 },
411                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
412                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
413                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
414                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
415                 { 0,                            0,  0 },
416         }, {
417                 { SCARLETT2_PORT_TYPE_PCM,      0, 14 },
418                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
419                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
420                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
421                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
422                 { 0,                            0,  0 },
423         }, {
424                 { SCARLETT2_PORT_TYPE_PCM,      0, 10 },
425                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
426                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
427                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
428                 { SCARLETT2_PORT_TYPE_NONE,     0,  4 },
429                 { 0,                            0,  0 },
430         } },
431 };
432
433 static const struct scarlett2_device_info s18i20_gen2_info = {
434         .usb_id = USB_ID(0x1235, 0x8201),
435
436         .line_out_hw_vol = 1,
437
438         .line_out_descrs = {
439                 "Monitor L",
440                 "Monitor R",
441                 NULL,
442                 NULL,
443                 NULL,
444                 NULL,
445                 "Headphones 1 L",
446                 "Headphones 1 R",
447                 "Headphones 2 L",
448                 "Headphones 2 R",
449         },
450
451         .port_count = {
452                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
453                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  8, 10 },
454                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
455                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  8 },
456                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
457                 [SCARLETT2_PORT_TYPE_PCM]      = { 20, 18 },
458         },
459
460         .mux_assignment = { {
461                 { SCARLETT2_PORT_TYPE_PCM,      0, 18 },
462                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
463                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
464                 { SCARLETT2_PORT_TYPE_ADAT,     0,  8 },
465                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
466                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
467                 { 0,                            0,  0 },
468         }, {
469                 { SCARLETT2_PORT_TYPE_PCM,      0, 14 },
470                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
471                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
472                 { SCARLETT2_PORT_TYPE_ADAT,     0,  4 },
473                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
474                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
475                 { 0,                            0,  0 },
476         }, {
477                 { SCARLETT2_PORT_TYPE_PCM,      0, 10 },
478                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
479                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
480                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
481                 { SCARLETT2_PORT_TYPE_NONE,     0,  6 },
482                 { 0,                            0,  0 },
483         } },
484 };
485
486 static const struct scarlett2_device_info s4i4_gen3_info = {
487         .usb_id = USB_ID(0x1235, 0x8212),
488
489         .level_input_count = 2,
490         .pad_input_count = 2,
491
492         .line_out_descrs = {
493                 "Monitor L",
494                 "Monitor R",
495                 "Headphones L",
496                 "Headphones R",
497         },
498
499         .port_count = {
500                 [SCARLETT2_PORT_TYPE_NONE]     = { 1, 0 },
501                 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
502                 [SCARLETT2_PORT_TYPE_MIX]      = { 6, 8 },
503                 [SCARLETT2_PORT_TYPE_PCM]      = { 4, 6 },
504         },
505
506         .mux_assignment = { {
507                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
508                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
509                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
510                 { SCARLETT2_PORT_TYPE_NONE,     0, 16 },
511                 { 0,                            0,  0 },
512         }, {
513                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
514                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
515                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
516                 { SCARLETT2_PORT_TYPE_NONE,     0, 16 },
517                 { 0,                            0,  0 },
518         }, {
519                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
520                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
521                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
522                 { SCARLETT2_PORT_TYPE_NONE,     0, 16 },
523                 { 0,                            0,  0 },
524         } },
525 };
526
527 static const struct scarlett2_device_info s8i6_gen3_info = {
528         .usb_id = USB_ID(0x1235, 0x8213),
529
530         .level_input_count = 2,
531         .pad_input_count = 2,
532
533         .line_out_descrs = {
534                 "Headphones 1 L",
535                 "Headphones 1 R",
536                 "Headphones 2 L",
537                 "Headphones 2 R",
538         },
539
540         .port_count = {
541                 [SCARLETT2_PORT_TYPE_NONE]     = { 1,  0 },
542                 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6,  4 },
543                 [SCARLETT2_PORT_TYPE_SPDIF]    = { 2,  2 },
544                 [SCARLETT2_PORT_TYPE_MIX]      = { 8,  8 },
545                 [SCARLETT2_PORT_TYPE_PCM]      = { 6, 10 },
546         },
547
548         .mux_assignment = { {
549                 { SCARLETT2_PORT_TYPE_PCM,      0,  8 },
550                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
551                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
552                 { SCARLETT2_PORT_TYPE_PCM,      8,  2 },
553                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
554                 { SCARLETT2_PORT_TYPE_NONE,     0, 18 },
555                 { 0,                            0,  0 },
556         }, {
557                 { SCARLETT2_PORT_TYPE_PCM,      0,  8 },
558                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
559                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
560                 { SCARLETT2_PORT_TYPE_PCM,      8,  2 },
561                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
562                 { SCARLETT2_PORT_TYPE_NONE,     0, 18 },
563                 { 0,                            0,  0 },
564         }, {
565                 { SCARLETT2_PORT_TYPE_PCM,      0,  8 },
566                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
567                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
568                 { SCARLETT2_PORT_TYPE_PCM,      8,  2 },
569                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
570                 { SCARLETT2_PORT_TYPE_NONE,     0, 18 },
571                 { 0,                            0,  0 },
572         } },
573 };
574
575 static const struct scarlett2_device_info s18i8_gen3_info = {
576         .usb_id = USB_ID(0x1235, 0x8214),
577
578         .line_out_hw_vol = 1,
579         .level_input_count = 2,
580         .pad_input_count = 2,
581
582         .line_out_descrs = {
583                 "Monitor L",
584                 "Monitor R",
585                 "Headphones 1 L",
586                 "Headphones 1 R",
587                 "Headphones 2 L",
588                 "Headphones 2 R",
589                 "Alt Monitor L",
590                 "Alt Monitor R",
591         },
592
593         .port_count = {
594                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
595                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  8,  8 },
596                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
597                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  0 },
598                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 20 },
599                 [SCARLETT2_PORT_TYPE_PCM]      = {  8, 20 },
600         },
601
602         .mux_assignment = { {
603                 { SCARLETT2_PORT_TYPE_PCM,       0, 10 },
604                 { SCARLETT2_PORT_TYPE_PCM,      12,  8 },
605                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
606                 { SCARLETT2_PORT_TYPE_ANALOGUE,  6,  2 },
607                 { SCARLETT2_PORT_TYPE_ANALOGUE,  2,  4 },
608                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
609                 { SCARLETT2_PORT_TYPE_PCM,      10,  2 },
610                 { SCARLETT2_PORT_TYPE_MIX,       0, 20 },
611                 { SCARLETT2_PORT_TYPE_NONE,      0, 10 },
612                 { 0,                             0,  0 },
613         }, {
614                 { SCARLETT2_PORT_TYPE_PCM,       0, 10 },
615                 { SCARLETT2_PORT_TYPE_PCM,      12,  4 },
616                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
617                 { SCARLETT2_PORT_TYPE_ANALOGUE,  6,  2 },
618                 { SCARLETT2_PORT_TYPE_ANALOGUE,  2,  4 },
619                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
620                 { SCARLETT2_PORT_TYPE_PCM,      10,  2 },
621                 { SCARLETT2_PORT_TYPE_MIX,       0, 20 },
622                 { SCARLETT2_PORT_TYPE_NONE,      0, 10 },
623                 { 0,                             0,  0 },
624         }, {
625                 { SCARLETT2_PORT_TYPE_PCM,       0, 10 },
626                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
627                 { SCARLETT2_PORT_TYPE_ANALOGUE,  6,  2 },
628                 { SCARLETT2_PORT_TYPE_ANALOGUE,  2,  4 },
629                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
630                 { SCARLETT2_PORT_TYPE_MIX,       0, 20 },
631                 { SCARLETT2_PORT_TYPE_NONE,      0, 10 },
632                 { 0,                             0,  0 },
633         } },
634 };
635
636 static const struct scarlett2_device_info s18i20_gen3_info = {
637         .usb_id = USB_ID(0x1235, 0x8215),
638
639         .line_out_hw_vol = 1,
640         .level_input_count = 2,
641         .pad_input_count = 8,
642
643         .line_out_descrs = {
644                 "Monitor 1 L",
645                 "Monitor 1 R",
646                 "Monitor 2 L",
647                 "Monitor 2 R",
648                 NULL,
649                 NULL,
650                 "Headphones 1 L",
651                 "Headphones 1 R",
652                 "Headphones 2 L",
653                 "Headphones 2 R",
654         },
655
656         .port_count = {
657                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
658                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  9, 10 },
659                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
660                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  8 },
661                 [SCARLETT2_PORT_TYPE_MIX]      = { 12, 25 },
662                 [SCARLETT2_PORT_TYPE_PCM]      = { 20, 20 },
663         },
664
665         .mux_assignment = { {
666                 { SCARLETT2_PORT_TYPE_PCM,       0,  8 },
667                 { SCARLETT2_PORT_TYPE_PCM,      10, 10 },
668                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0, 10 },
669                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
670                 { SCARLETT2_PORT_TYPE_ADAT,      0,  8 },
671                 { SCARLETT2_PORT_TYPE_PCM,       8,  2 },
672                 { SCARLETT2_PORT_TYPE_MIX,       0, 25 },
673                 { SCARLETT2_PORT_TYPE_NONE,      0, 12 },
674                 { 0,                             0,  0 },
675         }, {
676                 { SCARLETT2_PORT_TYPE_PCM,       0,  8 },
677                 { SCARLETT2_PORT_TYPE_PCM,      10,  8 },
678                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0, 10 },
679                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
680                 { SCARLETT2_PORT_TYPE_ADAT,      0,  8 },
681                 { SCARLETT2_PORT_TYPE_PCM,       8,  2 },
682                 { SCARLETT2_PORT_TYPE_MIX,       0, 25 },
683                 { SCARLETT2_PORT_TYPE_NONE,      0, 10 },
684                 { 0,                             0,  0 },
685         }, {
686                 { SCARLETT2_PORT_TYPE_PCM,       0, 10 },
687                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0, 10 },
688                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
689                 { SCARLETT2_PORT_TYPE_NONE,      0, 24 },
690                 { 0,                             0,  0 },
691         } },
692 };
693
694 static const struct scarlett2_device_info *scarlett2_devices[] = {
695         /* Supported Gen 2 devices */
696         &s6i6_gen2_info,
697         &s18i8_gen2_info,
698         &s18i20_gen2_info,
699
700         /* Supported Gen 3 devices */
701         &s4i4_gen3_info,
702         &s8i6_gen3_info,
703         &s18i8_gen3_info,
704         &s18i20_gen3_info,
705
706         /* End of list */
707         NULL
708 };
709
710 /* get the starting port index number for a given port type/direction */
711 static int scarlett2_get_port_start_num(
712         const int port_count[][SCARLETT2_PORT_DIRNS],
713         int direction, int port_type)
714 {
715         int i, num = 0;
716
717         for (i = 0; i < port_type; i++)
718                 num += port_count[i][direction];
719
720         return num;
721 }
722
723 /*** USB Interactions ***/
724
725 /* Notifications from the interface */
726 #define SCARLETT2_USB_NOTIFY_SYNC     0x00000008
727 #define SCARLETT2_USB_NOTIFY_DIM_MUTE 0x00200000
728 #define SCARLETT2_USB_NOTIFY_MONITOR  0x00400000
729
730 /* Commands for sending/receiving requests/responses */
731 #define SCARLETT2_USB_CMD_INIT 0
732 #define SCARLETT2_USB_CMD_REQ  2
733 #define SCARLETT2_USB_CMD_RESP 3
734
735 #define SCARLETT2_USB_INIT_1    0x00000000
736 #define SCARLETT2_USB_INIT_2    0x00000002
737 #define SCARLETT2_USB_GET_METER 0x00001001
738 #define SCARLETT2_USB_GET_MIX   0x00002001
739 #define SCARLETT2_USB_SET_MIX   0x00002002
740 #define SCARLETT2_USB_GET_MUX   0x00003001
741 #define SCARLETT2_USB_SET_MUX   0x00003002
742 #define SCARLETT2_USB_GET_SYNC  0x00006004
743 #define SCARLETT2_USB_GET_DATA  0x00800000
744 #define SCARLETT2_USB_SET_DATA  0x00800001
745 #define SCARLETT2_USB_DATA_CMD  0x00800002
746
747 #define SCARLETT2_USB_CONFIG_SAVE 6
748
749 #define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31
750 #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
751
752 /* volume status is read together (matches scarlett2_config_items[]) */
753 struct scarlett2_usb_volume_status {
754         /* dim/mute buttons */
755         u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
756
757         u8 pad1;
758
759         /* software volume setting */
760         s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
761
762         /* actual volume of output inc. dim (-18dB) */
763         s16 hw_vol[SCARLETT2_ANALOGUE_MAX];
764
765         /* internal mute buttons */
766         u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
767
768         /* sw (0) or hw (1) controlled */
769         u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
770
771         u8 pad3[6];
772
773         /* front panel volume knob */
774         s16 master_vol;
775 } __packed;
776
777 /* Configuration parameters that can be read and written */
778 enum {
779         SCARLETT2_CONFIG_DIM_MUTE = 0,
780         SCARLETT2_CONFIG_LINE_OUT_VOLUME = 1,
781         SCARLETT2_CONFIG_MUTE_SWITCH = 2,
782         SCARLETT2_CONFIG_SW_HW_SWITCH = 3,
783         SCARLETT2_CONFIG_LEVEL_SWITCH = 4,
784         SCARLETT2_CONFIG_PAD_SWITCH = 5,
785         SCARLETT2_CONFIG_COUNT = 6
786 };
787
788 /* Location, size, and activation command number for the configuration
789  * parameters
790  */
791 struct scarlett2_config {
792         u8 offset;
793         u8 size;
794         u8 activate;
795 };
796
797 static const struct scarlett2_config
798                 scarlett2_config_items[SCARLETT2_CONFIG_COUNT] = {
799         [SCARLETT2_CONFIG_DIM_MUTE] = {
800                 .offset = 0x31, .size = 1, .activate = 2 },
801
802         [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
803                 .offset = 0x34, .size = 2, .activate = 1 },
804
805         [SCARLETT2_CONFIG_MUTE_SWITCH] = {
806                 .offset = 0x5c, .size = 1, .activate = 1 },
807
808         [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
809                 .offset = 0x66, .size = 1, .activate = 3 },
810
811         [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
812                 .offset = 0x7c, .size = 1, .activate = 7 },
813
814         [SCARLETT2_CONFIG_PAD_SWITCH] = {
815                 .offset = 0x84, .size = 1, .activate = 8 },
816 };
817
818 /* proprietary request/response format */
819 struct scarlett2_usb_packet {
820         __le32 cmd;
821         __le16 size;
822         __le16 seq;
823         __le32 error;
824         __le32 pad;
825         u8 data[];
826 };
827
828 static void scarlett2_fill_request_header(struct scarlett2_data *private,
829                                           struct scarlett2_usb_packet *req,
830                                           u32 cmd, u16 req_size)
831 {
832         /* sequence must go up by 1 for each request */
833         u16 seq = private->scarlett2_seq++;
834
835         req->cmd = cpu_to_le32(cmd);
836         req->size = cpu_to_le16(req_size);
837         req->seq = cpu_to_le16(seq);
838         req->error = 0;
839         req->pad = 0;
840 }
841
842 static int scarlett2_usb_tx(struct usb_device *dev, int interface,
843                             void *buf, u16 size)
844 {
845         return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
846                         SCARLETT2_USB_CMD_REQ,
847                         USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
848                         0, interface, buf, size);
849 }
850
851 static int scarlett2_usb_rx(struct usb_device *dev, int interface,
852                             u32 usb_req, void *buf, u16 size)
853 {
854         return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
855                         usb_req,
856                         USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
857                         0, interface, buf, size);
858 }
859
860 /* Send a proprietary format request to the Scarlett interface */
861 static int scarlett2_usb(
862         struct usb_mixer_interface *mixer, u32 cmd,
863         void *req_data, u16 req_size, void *resp_data, u16 resp_size)
864 {
865         struct scarlett2_data *private = mixer->private_data;
866         struct usb_device *dev = mixer->chip->dev;
867         u16 req_buf_size = sizeof(struct scarlett2_usb_packet) + req_size;
868         u16 resp_buf_size = sizeof(struct scarlett2_usb_packet) + resp_size;
869         struct scarlett2_usb_packet *req, *resp = NULL;
870         int err;
871
872         req = kmalloc(req_buf_size, GFP_KERNEL);
873         if (!req) {
874                 err = -ENOMEM;
875                 goto error;
876         }
877
878         resp = kmalloc(resp_buf_size, GFP_KERNEL);
879         if (!resp) {
880                 err = -ENOMEM;
881                 goto error;
882         }
883
884         mutex_lock(&private->usb_mutex);
885
886         /* build request message and send it */
887
888         scarlett2_fill_request_header(private, req, cmd, req_size);
889
890         if (req_size)
891                 memcpy(req->data, req_data, req_size);
892
893         err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
894                                req, req_buf_size);
895
896         if (err != req_buf_size) {
897                 usb_audio_err(
898                         mixer->chip,
899                         "Scarlett Gen 2/3 USB request result cmd %x was %d\n",
900                         cmd, err);
901                 err = -EINVAL;
902                 goto unlock;
903         }
904
905         /* send a second message to get the response */
906
907         err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
908                                SCARLETT2_USB_CMD_RESP,
909                                resp, resp_buf_size);
910
911         /* validate the response */
912
913         if (err != resp_buf_size) {
914                 usb_audio_err(
915                         mixer->chip,
916                         "Scarlett Gen 2/3 USB response result cmd %x was %d "
917                         "expected %d\n",
918                         cmd, err, resp_buf_size);
919                 err = -EINVAL;
920                 goto unlock;
921         }
922
923         /* cmd/seq/size should match except when initialising
924          * seq sent = 1, response = 0
925          */
926         if (resp->cmd != req->cmd ||
927             (resp->seq != req->seq &&
928                 (le16_to_cpu(req->seq) != 1 || resp->seq != 0)) ||
929             resp_size != le16_to_cpu(resp->size) ||
930             resp->error ||
931             resp->pad) {
932                 usb_audio_err(
933                         mixer->chip,
934                         "Scarlett Gen 2/3 USB invalid response; "
935                            "cmd tx/rx %d/%d seq %d/%d size %d/%d "
936                            "error %d pad %d\n",
937                         le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
938                         le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
939                         resp_size, le16_to_cpu(resp->size),
940                         le32_to_cpu(resp->error),
941                         le32_to_cpu(resp->pad));
942                 err = -EINVAL;
943                 goto unlock;
944         }
945
946         if (resp_data && resp_size > 0)
947                 memcpy(resp_data, resp->data, resp_size);
948
949 unlock:
950         mutex_unlock(&private->usb_mutex);
951 error:
952         kfree(req);
953         kfree(resp);
954         return err;
955 }
956
957 /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
958 static void scarlett2_config_save(struct usb_mixer_interface *mixer)
959 {
960         __le32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE);
961
962         scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
963                       &req, sizeof(u32),
964                       NULL, 0);
965 }
966
967 /* Delayed work to save config */
968 static void scarlett2_config_save_work(struct work_struct *work)
969 {
970         struct scarlett2_data *private =
971                 container_of(work, struct scarlett2_data, work.work);
972
973         scarlett2_config_save(private->mixer);
974 }
975
976 /* Send a USB message to set a configuration parameter (volume level,
977  * sw/hw volume switch, line/inst level switch, or pad switch)
978  */
979 static int scarlett2_usb_set_config(
980         struct usb_mixer_interface *mixer,
981         int config_item_num, int index, int value)
982 {
983         const struct scarlett2_config *config_item =
984                &scarlett2_config_items[config_item_num];
985         struct {
986                 __le32 offset;
987                 __le32 bytes;
988                 __le32 value;
989         } __packed req;
990         __le32 req2;
991         int err;
992         struct scarlett2_data *private = mixer->private_data;
993
994         /* Cancel any pending NVRAM save */
995         cancel_delayed_work_sync(&private->work);
996
997         /* Send the configuration parameter data */
998         req.offset = cpu_to_le32(config_item->offset + index * config_item->size);
999         req.bytes = cpu_to_le32(config_item->size);
1000         req.value = cpu_to_le32(value);
1001         err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA,
1002                             &req, sizeof(u32) * 2 + config_item->size,
1003                             NULL, 0);
1004         if (err < 0)
1005                 return err;
1006
1007         /* Activate the change */
1008         req2 = cpu_to_le32(config_item->activate);
1009         err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1010                             &req2, sizeof(req2), NULL, 0);
1011         if (err < 0)
1012                 return err;
1013
1014         /* Schedule the change to be written to NVRAM */
1015         schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
1016
1017         return 0;
1018 }
1019
1020 /* Send a USB message to get data; result placed in *buf */
1021 static int scarlett2_usb_get(
1022         struct usb_mixer_interface *mixer,
1023         int offset, void *buf, int size)
1024 {
1025         struct {
1026                 __le32 offset;
1027                 __le32 size;
1028         } __packed req;
1029
1030         req.offset = cpu_to_le32(offset);
1031         req.size = cpu_to_le32(size);
1032         return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
1033                              &req, sizeof(req), buf, size);
1034 }
1035
1036 /* Send a USB message to get configuration parameters; result placed in *buf */
1037 static int scarlett2_usb_get_config(
1038         struct usb_mixer_interface *mixer,
1039         int config_item_num, int count, void *buf)
1040 {
1041         const struct scarlett2_config *config_item =
1042                 &scarlett2_config_items[config_item_num];
1043         int size = config_item->size * count;
1044
1045         return scarlett2_usb_get(mixer, config_item->offset, buf, size);
1046 }
1047
1048 /* Send a USB message to get sync status; result placed in *sync */
1049 static int scarlett2_usb_get_sync_status(
1050         struct usb_mixer_interface *mixer,
1051         u8 *sync)
1052 {
1053         __le32 data;
1054         int err;
1055
1056         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
1057                             NULL, 0, &data, sizeof(data));
1058         if (err < 0)
1059                 return err;
1060
1061         *sync = !!data;
1062         return 0;
1063 }
1064
1065 /* Send a USB message to get volume status; result placed in *buf */
1066 static int scarlett2_usb_get_volume_status(
1067         struct usb_mixer_interface *mixer,
1068         struct scarlett2_usb_volume_status *buf)
1069 {
1070         return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET,
1071                                  buf, sizeof(*buf));
1072 }
1073
1074 /* Send a USB message to get the volumes for all inputs of one mix
1075  * and put the values into private->mix[]
1076  */
1077 static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
1078                                  int mix_num)
1079 {
1080         struct scarlett2_data *private = mixer->private_data;
1081         const struct scarlett2_device_info *info = private->info;
1082
1083         int num_mixer_in =
1084                 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1085         int err, i, j, k;
1086
1087         struct {
1088                 __le16 mix_num;
1089                 __le16 count;
1090         } __packed req;
1091
1092         __le16 data[SCARLETT2_INPUT_MIX_MAX];
1093
1094         req.mix_num = cpu_to_le16(mix_num);
1095         req.count = cpu_to_le16(num_mixer_in);
1096
1097         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
1098                             &req, sizeof(req),
1099                             data, num_mixer_in * sizeof(u16));
1100         if (err < 0)
1101                 return err;
1102
1103         for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++) {
1104                 u16 mixer_value = le16_to_cpu(data[i]);
1105
1106                 for (k = 0; k < SCARLETT2_MIXER_VALUE_COUNT; k++)
1107                         if (scarlett2_mixer_values[k] >= mixer_value)
1108                                 break;
1109                 if (k == SCARLETT2_MIXER_VALUE_COUNT)
1110                         k = SCARLETT2_MIXER_MAX_VALUE;
1111                 private->mix[j] = k;
1112         }
1113
1114         return 0;
1115 }
1116
1117 /* Send a USB message to set the volumes for all inputs of one mix
1118  * (values obtained from private->mix[])
1119  */
1120 static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
1121                                  int mix_num)
1122 {
1123         struct scarlett2_data *private = mixer->private_data;
1124         const struct scarlett2_device_info *info = private->info;
1125
1126         struct {
1127                 __le16 mix_num;
1128                 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1129         } __packed req;
1130
1131         int i, j;
1132         int num_mixer_in =
1133                 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1134
1135         req.mix_num = cpu_to_le16(mix_num);
1136
1137         for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
1138                 req.data[i] = cpu_to_le16(
1139                         scarlett2_mixer_values[private->mix[j]]
1140                 );
1141
1142         return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
1143                              &req, (num_mixer_in + 1) * sizeof(u16),
1144                              NULL, 0);
1145 }
1146
1147 /* Convert a port number index (per info->port_count) to a hardware ID */
1148 static u32 scarlett2_mux_src_num_to_id(
1149         const int port_count[][SCARLETT2_PORT_DIRNS], int num)
1150 {
1151         int port_type;
1152
1153         for (port_type = 0;
1154              port_type < SCARLETT2_PORT_TYPE_COUNT;
1155              port_type++) {
1156                 if (num < port_count[port_type][SCARLETT2_PORT_IN])
1157                         return scarlett2_ports[port_type].id | num;
1158                 num -= port_count[port_type][SCARLETT2_PORT_IN];
1159         }
1160
1161         /* Oops */
1162         return 0;
1163 }
1164
1165 /* Convert a hardware ID to a port number index */
1166 static u32 scarlett2_mux_id_to_num(
1167         const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
1168 {
1169         int port_type;
1170         int port_num = 0;
1171
1172         for (port_type = 0;
1173              port_type < SCARLETT2_PORT_TYPE_COUNT;
1174              port_type++) {
1175                 int base = scarlett2_ports[port_type].id;
1176                 int count = port_count[port_type][direction];
1177
1178                 if (id >= base && id < base + count)
1179                         return port_num + id - base;
1180                 port_num += count;
1181         }
1182
1183         /* Oops */
1184         return -1;
1185 }
1186
1187 /* Convert one mux entry from the interface and load into private->mux[] */
1188 static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
1189                                        u32 mux_entry)
1190 {
1191         const struct scarlett2_device_info *info = private->info;
1192         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1193
1194         int dst_idx, src_idx;
1195
1196         dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
1197                                           mux_entry & 0xFFF);
1198         if (dst_idx < 0)
1199                 return;
1200
1201         if (dst_idx >= private->num_mux_dsts) {
1202                 usb_audio_err(private->mixer->chip,
1203                         "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
1204                         mux_entry, dst_idx, private->num_mux_dsts);
1205                 return;
1206         }
1207
1208         src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
1209                                           mux_entry >> 12);
1210         if (src_idx < 0)
1211                 return;
1212
1213         if (src_idx >= private->num_mux_srcs) {
1214                 usb_audio_err(private->mixer->chip,
1215                         "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
1216                         mux_entry, src_idx, private->num_mux_srcs);
1217                 return;
1218         }
1219
1220         private->mux[dst_idx] = src_idx;
1221 }
1222
1223 /* Send USB message to get mux inputs and then populate private->mux[] */
1224 static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
1225 {
1226         struct scarlett2_data *private = mixer->private_data;
1227         int count = private->num_mux_dsts;
1228         int err, i;
1229
1230         struct {
1231                 __le16 num;
1232                 __le16 count;
1233         } __packed req;
1234
1235         __le32 data[SCARLETT2_MUX_MAX];
1236
1237         req.num = 0;
1238         req.count = cpu_to_le16(count);
1239
1240         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
1241                             &req, sizeof(req),
1242                             data, count * sizeof(u32));
1243         if (err < 0)
1244                 return err;
1245
1246         for (i = 0; i < count; i++)
1247                 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
1248
1249         return 0;
1250 }
1251
1252 /* Send USB messages to set mux inputs */
1253 static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
1254 {
1255         struct scarlett2_data *private = mixer->private_data;
1256         const struct scarlett2_device_info *info = private->info;
1257         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1258         int table;
1259
1260         struct {
1261                 __le16 pad;
1262                 __le16 num;
1263                 __le32 data[SCARLETT2_MUX_MAX];
1264         } __packed req;
1265
1266         req.pad = 0;
1267
1268         /* set mux settings for each rate */
1269         for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
1270                 const struct scarlett2_mux_entry *entry;
1271
1272                 /* i counts over the output array */
1273                 int i = 0, err;
1274
1275                 req.num = cpu_to_le16(table);
1276
1277                 /* loop through each entry */
1278                 for (entry = info->mux_assignment[table];
1279                      entry->count;
1280                      entry++) {
1281                         int j;
1282                         int port_type = entry->port_type;
1283                         int port_idx = entry->start;
1284                         int mux_idx = scarlett2_get_port_start_num(port_count,
1285                                 SCARLETT2_PORT_OUT, port_type) + port_idx;
1286                         int dst_id = scarlett2_ports[port_type].id + port_idx;
1287
1288                         /* Empty slots */
1289                         if (!dst_id) {
1290                                 for (j = 0; j < entry->count; j++)
1291                                         req.data[i++] = 0;
1292                                 continue;
1293                         }
1294
1295                         /* Non-empty mux slots use the lower 12 bits
1296                          * for the destination and next 12 bits for
1297                          * the source
1298                          */
1299                         for (j = 0; j < entry->count; j++) {
1300                                 int src_id = scarlett2_mux_src_num_to_id(
1301                                         port_count, private->mux[mux_idx++]);
1302                                 req.data[i++] = cpu_to_le32(dst_id |
1303                                                             src_id << 12);
1304                                 dst_id++;
1305                         }
1306                 }
1307
1308                 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
1309                                     &req, (i + 1) * sizeof(u32),
1310                                     NULL, 0);
1311                 if (err < 0)
1312                         return err;
1313         }
1314
1315         return 0;
1316 }
1317
1318 /* Send USB message to get meter levels */
1319 static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
1320                                           u16 num_meters, u16 *levels)
1321 {
1322         struct {
1323                 __le16 pad;
1324                 __le16 num_meters;
1325                 __le32 magic;
1326         } __packed req;
1327         u32 resp[SCARLETT2_MAX_METERS];
1328         int i, err;
1329
1330         req.pad = 0;
1331         req.num_meters = cpu_to_le16(num_meters);
1332         req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
1333         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
1334                             &req, sizeof(req), resp, num_meters * sizeof(u32));
1335         if (err < 0)
1336                 return err;
1337
1338         /* copy, convert to u16 */
1339         for (i = 0; i < num_meters; i++)
1340                 levels[i] = resp[i];
1341
1342         return 0;
1343 }
1344
1345 /*** Control Functions ***/
1346
1347 /* helper function to create a new control */
1348 static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
1349                                  const struct snd_kcontrol_new *ncontrol,
1350                                  int index, int channels, const char *name,
1351                                  struct snd_kcontrol **kctl_return)
1352 {
1353         struct snd_kcontrol *kctl;
1354         struct usb_mixer_elem_info *elem;
1355         int err;
1356
1357         elem = kzalloc(sizeof(*elem), GFP_KERNEL);
1358         if (!elem)
1359                 return -ENOMEM;
1360
1361         /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
1362          * ignores them for resume and other operations.
1363          * Also, the head.id field is set to 0, as we don't use this field.
1364          */
1365         elem->head.mixer = mixer;
1366         elem->control = index;
1367         elem->head.id = 0;
1368         elem->channels = channels;
1369         elem->val_type = USB_MIXER_BESPOKEN;
1370
1371         kctl = snd_ctl_new1(ncontrol, elem);
1372         if (!kctl) {
1373                 kfree(elem);
1374                 return -ENOMEM;
1375         }
1376         kctl->private_free = snd_usb_mixer_elem_free;
1377
1378         strscpy(kctl->id.name, name, sizeof(kctl->id.name));
1379
1380         err = snd_usb_mixer_add_control(&elem->head, kctl);
1381         if (err < 0)
1382                 return err;
1383
1384         if (kctl_return)
1385                 *kctl_return = kctl;
1386
1387         return 0;
1388 }
1389
1390 /*** Sync Control ***/
1391
1392 /* Update sync control after receiving notification that the status
1393  * has changed
1394  */
1395 static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
1396 {
1397         struct scarlett2_data *private = mixer->private_data;
1398
1399         private->sync_updated = 0;
1400         return scarlett2_usb_get_sync_status(mixer, &private->sync);
1401 }
1402
1403 static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
1404                                    struct snd_ctl_elem_info *uinfo)
1405 {
1406         static const char *texts[2] = {
1407                 "Unlocked", "Locked"
1408         };
1409         return snd_ctl_enum_info(uinfo, 1, 2, texts);
1410 }
1411
1412 static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
1413                                   struct snd_ctl_elem_value *ucontrol)
1414 {
1415         struct usb_mixer_elem_info *elem = kctl->private_data;
1416         struct usb_mixer_interface *mixer = elem->head.mixer;
1417         struct scarlett2_data *private = mixer->private_data;
1418
1419         mutex_lock(&private->data_mutex);
1420         if (private->sync_updated)
1421                 scarlett2_update_sync(mixer);
1422         ucontrol->value.enumerated.item[0] = private->sync;
1423         mutex_unlock(&private->data_mutex);
1424
1425         return 0;
1426 }
1427
1428 static const struct snd_kcontrol_new scarlett2_sync_ctl = {
1429         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1430         .access = SNDRV_CTL_ELEM_ACCESS_READ,
1431         .name = "",
1432         .info = scarlett2_sync_ctl_info,
1433         .get  = scarlett2_sync_ctl_get
1434 };
1435
1436 static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
1437 {
1438         struct scarlett2_data *private = mixer->private_data;
1439
1440         return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
1441                                      0, 1, "Sync Status", &private->sync_ctl);
1442 }
1443
1444 /*** Analogue Line Out Volume Controls ***/
1445
1446 /* Update hardware volume controls after receiving notification that
1447  * they have changed
1448  */
1449 static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
1450 {
1451         struct scarlett2_data *private = mixer->private_data;
1452         const struct scarlett2_device_info *info = private->info;
1453         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1454         struct scarlett2_usb_volume_status volume_status;
1455         int num_line_out =
1456                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
1457         int err, i;
1458         int mute;
1459
1460         private->vol_updated = 0;
1461
1462         err = scarlett2_usb_get_volume_status(mixer, &volume_status);
1463         if (err < 0)
1464                 return err;
1465
1466         private->master_vol = clamp(
1467                 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
1468                 0, SCARLETT2_VOLUME_BIAS);
1469
1470         if (info->line_out_hw_vol)
1471                 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
1472                         private->dim_mute[i] = !!volume_status.dim_mute[i];
1473
1474         mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];
1475
1476         for (i = 0; i < num_line_out; i++)
1477                 if (private->vol_sw_hw_switch[i]) {
1478                         private->vol[i] = private->master_vol;
1479                         private->mute_switch[i] = mute;
1480                 }
1481
1482         return 0;
1483 }
1484
1485 static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
1486                                      struct snd_ctl_elem_info *uinfo)
1487 {
1488         struct usb_mixer_elem_info *elem = kctl->private_data;
1489
1490         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1491         uinfo->count = elem->channels;
1492         uinfo->value.integer.min = 0;
1493         uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
1494         uinfo->value.integer.step = 1;
1495         return 0;
1496 }
1497
1498 static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
1499                                            struct snd_ctl_elem_value *ucontrol)
1500 {
1501         struct usb_mixer_elem_info *elem = kctl->private_data;
1502         struct usb_mixer_interface *mixer = elem->head.mixer;
1503         struct scarlett2_data *private = mixer->private_data;
1504
1505         mutex_lock(&private->data_mutex);
1506         if (private->vol_updated)
1507                 scarlett2_update_volumes(mixer);
1508         mutex_unlock(&private->data_mutex);
1509
1510         ucontrol->value.integer.value[0] = private->master_vol;
1511         return 0;
1512 }
1513
1514 static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
1515                                     struct snd_ctl_elem_value *ucontrol)
1516 {
1517         struct usb_mixer_elem_info *elem = kctl->private_data;
1518         struct usb_mixer_interface *mixer = elem->head.mixer;
1519         struct scarlett2_data *private = mixer->private_data;
1520         int index = elem->control;
1521
1522         mutex_lock(&private->data_mutex);
1523         if (private->vol_updated)
1524                 scarlett2_update_volumes(mixer);
1525         mutex_unlock(&private->data_mutex);
1526
1527         ucontrol->value.integer.value[0] = private->vol[index];
1528         return 0;
1529 }
1530
1531 static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
1532                                     struct snd_ctl_elem_value *ucontrol)
1533 {
1534         struct usb_mixer_elem_info *elem = kctl->private_data;
1535         struct usb_mixer_interface *mixer = elem->head.mixer;
1536         struct scarlett2_data *private = mixer->private_data;
1537         int index = elem->control;
1538         int oval, val, err = 0;
1539
1540         mutex_lock(&private->data_mutex);
1541
1542         oval = private->vol[index];
1543         val = ucontrol->value.integer.value[0];
1544
1545         if (oval == val)
1546                 goto unlock;
1547
1548         private->vol[index] = val;
1549         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1550                                        index, val - SCARLETT2_VOLUME_BIAS);
1551         if (err == 0)
1552                 err = 1;
1553
1554 unlock:
1555         mutex_unlock(&private->data_mutex);
1556         return err;
1557 }
1558
1559 static const DECLARE_TLV_DB_MINMAX(
1560         db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0
1561 );
1562
1563 static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
1564         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1565         .access = SNDRV_CTL_ELEM_ACCESS_READ |
1566                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1567         .name = "",
1568         .info = scarlett2_volume_ctl_info,
1569         .get  = scarlett2_master_volume_ctl_get,
1570         .private_value = 0, /* max value */
1571         .tlv = { .p = db_scale_scarlett2_gain }
1572 };
1573
1574 static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
1575         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1576         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1577                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1578         .name = "",
1579         .info = scarlett2_volume_ctl_info,
1580         .get  = scarlett2_volume_ctl_get,
1581         .put  = scarlett2_volume_ctl_put,
1582         .private_value = 0, /* max value */
1583         .tlv = { .p = db_scale_scarlett2_gain }
1584 };
1585
1586 /*** Mute Switch Controls ***/
1587
1588 static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
1589                                         struct snd_ctl_elem_value *ucontrol)
1590 {
1591         struct usb_mixer_elem_info *elem = kctl->private_data;
1592         struct scarlett2_data *private = elem->head.mixer->private_data;
1593         int index = elem->control;
1594
1595         ucontrol->value.integer.value[0] = private->mute_switch[index];
1596         return 0;
1597 }
1598
1599 static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
1600                                         struct snd_ctl_elem_value *ucontrol)
1601 {
1602         struct usb_mixer_elem_info *elem = kctl->private_data;
1603         struct usb_mixer_interface *mixer = elem->head.mixer;
1604         struct scarlett2_data *private = mixer->private_data;
1605         int index = elem->control;
1606         int oval, val, err = 0;
1607
1608         mutex_lock(&private->data_mutex);
1609
1610         oval = private->mute_switch[index];
1611         val = !!ucontrol->value.integer.value[0];
1612
1613         if (oval == val)
1614                 goto unlock;
1615
1616         private->mute_switch[index] = val;
1617
1618         /* Send mute change to the device */
1619         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
1620                                        index, val);
1621
1622 unlock:
1623         mutex_unlock(&private->data_mutex);
1624         return err;
1625 }
1626
1627 static const struct snd_kcontrol_new scarlett2_mute_ctl = {
1628         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1629         .name = "",
1630         .info = snd_ctl_boolean_mono_info,
1631         .get  = scarlett2_mute_ctl_get,
1632         .put  = scarlett2_mute_ctl_put,
1633 };
1634
1635 /*** HW/SW Volume Switch Controls ***/
1636
1637 static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
1638                                          struct snd_ctl_elem_info *uinfo)
1639 {
1640         static const char *const values[2] = {
1641                 "SW", "HW"
1642         };
1643
1644         return snd_ctl_enum_info(uinfo, 1, 2, values);
1645 }
1646
1647 static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
1648                                         struct snd_ctl_elem_value *ucontrol)
1649 {
1650         struct usb_mixer_elem_info *elem = kctl->private_data;
1651         struct scarlett2_data *private = elem->head.mixer->private_data;
1652
1653         ucontrol->value.enumerated.item[0] =
1654                 private->vol_sw_hw_switch[elem->control];
1655         return 0;
1656 }
1657
1658 static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
1659                                            int index, int value)
1660 {
1661         struct scarlett2_data *private = mixer->private_data;
1662         struct snd_card *card = mixer->chip->card;
1663
1664         /* Set/Clear write bits */
1665         if (value) {
1666                 private->vol_ctls[index]->vd[0].access |=
1667                         SNDRV_CTL_ELEM_ACCESS_WRITE;
1668                 private->mute_ctls[index]->vd[0].access |=
1669                         SNDRV_CTL_ELEM_ACCESS_WRITE;
1670         } else {
1671                 private->vol_ctls[index]->vd[0].access &=
1672                         ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1673                 private->mute_ctls[index]->vd[0].access &=
1674                         ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1675         }
1676
1677         /* Notify of write bit change */
1678         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
1679                        &private->vol_ctls[index]->id);
1680         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
1681                        &private->mute_ctls[index]->id);
1682 }
1683
1684 static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
1685                                         struct snd_ctl_elem_value *ucontrol)
1686 {
1687         struct usb_mixer_elem_info *elem = kctl->private_data;
1688         struct usb_mixer_interface *mixer = elem->head.mixer;
1689         struct scarlett2_data *private = mixer->private_data;
1690
1691         int index = elem->control;
1692         int oval, val, err = 0;
1693
1694         mutex_lock(&private->data_mutex);
1695
1696         oval = private->vol_sw_hw_switch[index];
1697         val = !!ucontrol->value.enumerated.item[0];
1698
1699         if (oval == val)
1700                 goto unlock;
1701
1702         private->vol_sw_hw_switch[index] = val;
1703
1704         /* Change access mode to RO (hardware controlled volume)
1705          * or RW (software controlled volume)
1706          */
1707         scarlett2_vol_ctl_set_writable(mixer, index, !val);
1708
1709         /* Reset volume/mute to master volume/mute */
1710         private->vol[index] = private->master_vol;
1711         private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE];
1712
1713         /* Set SW volume to current HW volume */
1714         err = scarlett2_usb_set_config(
1715                 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1716                 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
1717         if (err < 0)
1718                 goto unlock;
1719
1720         /* Set SW mute to current HW mute */
1721         err = scarlett2_usb_set_config(
1722                 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
1723                 index, private->dim_mute[SCARLETT2_BUTTON_MUTE]);
1724         if (err < 0)
1725                 goto unlock;
1726
1727         /* Send SW/HW switch change to the device */
1728         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
1729                                        index, val);
1730         if (err == 0)
1731                 err = 1;
1732
1733 unlock:
1734         mutex_unlock(&private->data_mutex);
1735         return err;
1736 }
1737
1738 static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
1739         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1740         .name = "",
1741         .info = scarlett2_sw_hw_enum_ctl_info,
1742         .get  = scarlett2_sw_hw_enum_ctl_get,
1743         .put  = scarlett2_sw_hw_enum_ctl_put,
1744 };
1745
1746 /*** Line Level/Instrument Level Switch Controls ***/
1747
1748 static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
1749                                          struct snd_ctl_elem_info *uinfo)
1750 {
1751         static const char *const values[2] = {
1752                 "Line", "Inst"
1753         };
1754
1755         return snd_ctl_enum_info(uinfo, 1, 2, values);
1756 }
1757
1758 static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
1759                                         struct snd_ctl_elem_value *ucontrol)
1760 {
1761         struct usb_mixer_elem_info *elem = kctl->private_data;
1762         struct scarlett2_data *private = elem->head.mixer->private_data;
1763
1764         ucontrol->value.enumerated.item[0] =
1765                 private->level_switch[elem->control];
1766         return 0;
1767 }
1768
1769 static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
1770                                         struct snd_ctl_elem_value *ucontrol)
1771 {
1772         struct usb_mixer_elem_info *elem = kctl->private_data;
1773         struct usb_mixer_interface *mixer = elem->head.mixer;
1774         struct scarlett2_data *private = mixer->private_data;
1775
1776         int index = elem->control;
1777         int oval, val, err = 0;
1778
1779         mutex_lock(&private->data_mutex);
1780
1781         oval = private->level_switch[index];
1782         val = !!ucontrol->value.enumerated.item[0];
1783
1784         if (oval == val)
1785                 goto unlock;
1786
1787         private->level_switch[index] = val;
1788
1789         /* Send switch change to the device */
1790         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
1791                                        index, val);
1792         if (err == 0)
1793                 err = 1;
1794
1795 unlock:
1796         mutex_unlock(&private->data_mutex);
1797         return err;
1798 }
1799
1800 static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
1801         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1802         .name = "",
1803         .info = scarlett2_level_enum_ctl_info,
1804         .get  = scarlett2_level_enum_ctl_get,
1805         .put  = scarlett2_level_enum_ctl_put,
1806 };
1807
1808 /*** Pad Switch Controls ***/
1809
1810 static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
1811                                  struct snd_ctl_elem_value *ucontrol)
1812 {
1813         struct usb_mixer_elem_info *elem = kctl->private_data;
1814         struct scarlett2_data *private = elem->head.mixer->private_data;
1815
1816         ucontrol->value.integer.value[0] =
1817                 private->pad_switch[elem->control];
1818         return 0;
1819 }
1820
1821 static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
1822                                  struct snd_ctl_elem_value *ucontrol)
1823 {
1824         struct usb_mixer_elem_info *elem = kctl->private_data;
1825         struct usb_mixer_interface *mixer = elem->head.mixer;
1826         struct scarlett2_data *private = mixer->private_data;
1827
1828         int index = elem->control;
1829         int oval, val, err = 0;
1830
1831         mutex_lock(&private->data_mutex);
1832
1833         oval = private->pad_switch[index];
1834         val = !!ucontrol->value.integer.value[0];
1835
1836         if (oval == val)
1837                 goto unlock;
1838
1839         private->pad_switch[index] = val;
1840
1841         /* Send switch change to the device */
1842         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
1843                                        index, val);
1844         if (err == 0)
1845                 err = 1;
1846
1847 unlock:
1848         mutex_unlock(&private->data_mutex);
1849         return err;
1850 }
1851
1852 static const struct snd_kcontrol_new scarlett2_pad_ctl = {
1853         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1854         .name = "",
1855         .info = snd_ctl_boolean_mono_info,
1856         .get  = scarlett2_pad_ctl_get,
1857         .put  = scarlett2_pad_ctl_put,
1858 };
1859
1860 /*** Dim/Mute Controls ***/
1861
1862 static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
1863                                       struct snd_ctl_elem_value *ucontrol)
1864 {
1865         struct usb_mixer_elem_info *elem = kctl->private_data;
1866         struct usb_mixer_interface *mixer = elem->head.mixer;
1867         struct scarlett2_data *private = mixer->private_data;
1868
1869         mutex_lock(&private->data_mutex);
1870         if (private->vol_updated)
1871                 scarlett2_update_volumes(mixer);
1872         mutex_unlock(&private->data_mutex);
1873
1874         ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
1875         return 0;
1876 }
1877
1878 static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
1879                                       struct snd_ctl_elem_value *ucontrol)
1880 {
1881         struct usb_mixer_elem_info *elem = kctl->private_data;
1882         struct usb_mixer_interface *mixer = elem->head.mixer;
1883         struct scarlett2_data *private = mixer->private_data;
1884         const struct scarlett2_device_info *info = private->info;
1885         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1886         int num_line_out =
1887                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
1888
1889         int index = elem->control;
1890         int oval, val, err = 0, i;
1891
1892         mutex_lock(&private->data_mutex);
1893
1894         oval = private->dim_mute[index];
1895         val = !!ucontrol->value.integer.value[0];
1896
1897         if (oval == val)
1898                 goto unlock;
1899
1900         private->dim_mute[index] = val;
1901
1902         /* Send switch change to the device */
1903         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
1904                                        index, val);
1905         if (err == 0)
1906                 err = 1;
1907
1908         if (index == SCARLETT2_BUTTON_MUTE)
1909                 for (i = 0; i < num_line_out; i++)
1910                         if (private->vol_sw_hw_switch[i]) {
1911                                 private->mute_switch[i] = val;
1912                                 snd_ctl_notify(mixer->chip->card,
1913                                                SNDRV_CTL_EVENT_MASK_INFO,
1914                                                &private->mute_ctls[i]->id);
1915                         }
1916
1917 unlock:
1918         mutex_unlock(&private->data_mutex);
1919         return err;
1920 }
1921
1922 static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
1923         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1924         .name = "",
1925         .info = snd_ctl_boolean_mono_info,
1926         .get  = scarlett2_dim_mute_ctl_get,
1927         .put  = scarlett2_dim_mute_ctl_put
1928 };
1929
1930 /*** Create the analogue output controls ***/
1931
1932 static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
1933 {
1934         struct scarlett2_data *private = mixer->private_data;
1935         const struct scarlett2_device_info *info = private->info;
1936         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1937         int num_line_out =
1938                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
1939         int err, i;
1940         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1941
1942         /* Add R/O HW volume control */
1943         if (info->line_out_hw_vol) {
1944                 snprintf(s, sizeof(s), "Master HW Playback Volume");
1945                 err = scarlett2_add_new_ctl(mixer,
1946                                             &scarlett2_master_volume_ctl,
1947                                             0, 1, s, &private->master_vol_ctl);
1948                 if (err < 0)
1949                         return err;
1950         }
1951
1952         /* Add volume controls */
1953         for (i = 0; i < num_line_out; i++) {
1954
1955                 /* Fader */
1956                 if (info->line_out_descrs[i])
1957                         snprintf(s, sizeof(s),
1958                                  "Line %02d (%s) Playback Volume",
1959                                  i + 1, info->line_out_descrs[i]);
1960                 else
1961                         snprintf(s, sizeof(s),
1962                                  "Line %02d Playback Volume",
1963                                  i + 1);
1964                 err = scarlett2_add_new_ctl(mixer,
1965                                             &scarlett2_line_out_volume_ctl,
1966                                             i, 1, s, &private->vol_ctls[i]);
1967                 if (err < 0)
1968                         return err;
1969
1970                 /* Mute Switch */
1971                 snprintf(s, sizeof(s),
1972                          "Line %02d Mute Playback Switch",
1973                          i + 1);
1974                 err = scarlett2_add_new_ctl(mixer,
1975                                             &scarlett2_mute_ctl,
1976                                             i, 1, s,
1977                                             &private->mute_ctls[i]);
1978                 if (err < 0)
1979                         return err;
1980
1981                 /* Make the fader and mute controls read-only if the
1982                  * SW/HW switch is set to HW
1983                  */
1984                 if (private->vol_sw_hw_switch[i])
1985                         scarlett2_vol_ctl_set_writable(mixer, i, 0);
1986
1987                 /* SW/HW Switch */
1988                 if (info->line_out_hw_vol) {
1989                         snprintf(s, sizeof(s),
1990                                  "Line Out %02d Volume Control Playback Enum",
1991                                  i + 1);
1992                         err = scarlett2_add_new_ctl(mixer,
1993                                                     &scarlett2_sw_hw_enum_ctl,
1994                                                     i, 1, s, NULL);
1995                         if (err < 0)
1996                                 return err;
1997                 }
1998         }
1999
2000         /* Add dim/mute controls */
2001         if (info->line_out_hw_vol)
2002                 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
2003                         err = scarlett2_add_new_ctl(
2004                                 mixer, &scarlett2_dim_mute_ctl,
2005                                 i, 1, scarlett2_dim_mute_names[i],
2006                                 &private->dim_mute_ctls[i]);
2007                         if (err < 0)
2008                                 return err;
2009                 }
2010
2011         return 0;
2012 }
2013
2014 /*** Create the analogue input controls ***/
2015
2016 static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
2017 {
2018         struct scarlett2_data *private = mixer->private_data;
2019         const struct scarlett2_device_info *info = private->info;
2020         int err, i;
2021         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2022         const char *fmt = "Line In %d %s Capture %s";
2023
2024         /* Add input level (line/inst) controls */
2025         for (i = 0; i < info->level_input_count; i++) {
2026                 snprintf(s, sizeof(s), fmt, i + 1, "Level", "Enum");
2027                 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
2028                                             i, 1, s, NULL);
2029                 if (err < 0)
2030                         return err;
2031         }
2032
2033         /* Add input pad controls */
2034         for (i = 0; i < info->pad_input_count; i++) {
2035                 snprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
2036                 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
2037                                             i, 1, s, NULL);
2038                 if (err < 0)
2039                         return err;
2040         }
2041
2042         return 0;
2043 }
2044
2045 /*** Mixer Volume Controls ***/
2046
2047 static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
2048                                     struct snd_ctl_elem_info *uinfo)
2049 {
2050         struct usb_mixer_elem_info *elem = kctl->private_data;
2051
2052         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2053         uinfo->count = elem->channels;
2054         uinfo->value.integer.min = 0;
2055         uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
2056         uinfo->value.integer.step = 1;
2057         return 0;
2058 }
2059
2060 static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
2061                                    struct snd_ctl_elem_value *ucontrol)
2062 {
2063         struct usb_mixer_elem_info *elem = kctl->private_data;
2064         struct scarlett2_data *private = elem->head.mixer->private_data;
2065
2066         ucontrol->value.integer.value[0] = private->mix[elem->control];
2067         return 0;
2068 }
2069
2070 static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
2071                                    struct snd_ctl_elem_value *ucontrol)
2072 {
2073         struct usb_mixer_elem_info *elem = kctl->private_data;
2074         struct usb_mixer_interface *mixer = elem->head.mixer;
2075         struct scarlett2_data *private = mixer->private_data;
2076         const struct scarlett2_device_info *info = private->info;
2077         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2078         int oval, val, num_mixer_in, mix_num, err = 0;
2079         int index = elem->control;
2080
2081         mutex_lock(&private->data_mutex);
2082
2083         oval = private->mix[index];
2084         val = ucontrol->value.integer.value[0];
2085         num_mixer_in = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
2086         mix_num = index / num_mixer_in;
2087
2088         if (oval == val)
2089                 goto unlock;
2090
2091         private->mix[index] = val;
2092         err = scarlett2_usb_set_mix(mixer, mix_num);
2093         if (err == 0)
2094                 err = 1;
2095
2096 unlock:
2097         mutex_unlock(&private->data_mutex);
2098         return err;
2099 }
2100
2101 static const DECLARE_TLV_DB_MINMAX(
2102         db_scale_scarlett2_mixer,
2103         SCARLETT2_MIXER_MIN_DB * 100,
2104         SCARLETT2_MIXER_MAX_DB * 100
2105 );
2106
2107 static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
2108         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2109         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
2110                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
2111         .name = "",
2112         .info = scarlett2_mixer_ctl_info,
2113         .get  = scarlett2_mixer_ctl_get,
2114         .put  = scarlett2_mixer_ctl_put,
2115         .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
2116         .tlv = { .p = db_scale_scarlett2_mixer }
2117 };
2118
2119 static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
2120 {
2121         struct scarlett2_data *private = mixer->private_data;
2122         const struct scarlett2_device_info *info = private->info;
2123         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2124         int err, i, j;
2125         int index;
2126         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2127
2128         int num_inputs =
2129                 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
2130         int num_outputs =
2131                 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2132
2133         for (i = 0, index = 0; i < num_outputs; i++)
2134                 for (j = 0; j < num_inputs; j++, index++) {
2135                         snprintf(s, sizeof(s),
2136                                  "Mix %c Input %02d Playback Volume",
2137                                  'A' + i, j + 1);
2138                         err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
2139                                                     index, 1, s, NULL);
2140                         if (err < 0)
2141                                 return err;
2142                 }
2143
2144         return 0;
2145 }
2146
2147 /*** Mux Source Selection Controls ***/
2148
2149 static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
2150                                            struct snd_ctl_elem_info *uinfo)
2151 {
2152         struct usb_mixer_elem_info *elem = kctl->private_data;
2153         struct scarlett2_data *private = elem->head.mixer->private_data;
2154         const struct scarlett2_device_info *info = private->info;
2155         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2156         unsigned int item = uinfo->value.enumerated.item;
2157         int items = private->num_mux_srcs;
2158         int port_type;
2159
2160         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2161         uinfo->count = elem->channels;
2162         uinfo->value.enumerated.items = items;
2163
2164         if (item >= items)
2165                 item = uinfo->value.enumerated.item = items - 1;
2166
2167         for (port_type = 0;
2168              port_type < SCARLETT2_PORT_TYPE_COUNT;
2169              port_type++) {
2170                 if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
2171                         const struct scarlett2_port *port =
2172                                 &scarlett2_ports[port_type];
2173
2174                         sprintf(uinfo->value.enumerated.name,
2175                                 port->src_descr, item + port->src_num_offset);
2176                         return 0;
2177                 }
2178                 item -= port_count[port_type][SCARLETT2_PORT_IN];
2179         }
2180
2181         return -EINVAL;
2182 }
2183
2184 static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
2185                                           struct snd_ctl_elem_value *ucontrol)
2186 {
2187         struct usb_mixer_elem_info *elem = kctl->private_data;
2188         struct scarlett2_data *private = elem->head.mixer->private_data;
2189
2190         ucontrol->value.enumerated.item[0] = private->mux[elem->control];
2191         return 0;
2192 }
2193
2194 static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
2195                                           struct snd_ctl_elem_value *ucontrol)
2196 {
2197         struct usb_mixer_elem_info *elem = kctl->private_data;
2198         struct usb_mixer_interface *mixer = elem->head.mixer;
2199         struct scarlett2_data *private = mixer->private_data;
2200         int index = elem->control;
2201         int oval, val, err = 0;
2202
2203         mutex_lock(&private->data_mutex);
2204
2205         oval = private->mux[index];
2206         val = min(ucontrol->value.enumerated.item[0],
2207                   private->num_mux_srcs - 1U);
2208
2209         if (oval == val)
2210                 goto unlock;
2211
2212         private->mux[index] = val;
2213         err = scarlett2_usb_set_mux(mixer);
2214         if (err == 0)
2215                 err = 1;
2216
2217 unlock:
2218         mutex_unlock(&private->data_mutex);
2219         return err;
2220 }
2221
2222 static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
2223         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2224         .name = "",
2225         .info = scarlett2_mux_src_enum_ctl_info,
2226         .get  = scarlett2_mux_src_enum_ctl_get,
2227         .put  = scarlett2_mux_src_enum_ctl_put,
2228 };
2229
2230 static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
2231 {
2232         struct scarlett2_data *private = mixer->private_data;
2233         const struct scarlett2_device_info *info = private->info;
2234         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2235         int port_type, channel, i;
2236
2237         for (i = 0, port_type = 0;
2238              port_type < SCARLETT2_PORT_TYPE_COUNT;
2239              port_type++) {
2240                 for (channel = 0;
2241                      channel < port_count[port_type][SCARLETT2_PORT_OUT];
2242                      channel++, i++) {
2243                         int err;
2244                         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2245                         const char *const descr =
2246                                 scarlett2_ports[port_type].dst_descr;
2247
2248                         snprintf(s, sizeof(s) - 5, descr, channel + 1);
2249                         strcat(s, " Enum");
2250
2251                         err = scarlett2_add_new_ctl(mixer,
2252                                                     &scarlett2_mux_src_enum_ctl,
2253                                                     i, 1, s, NULL);
2254                         if (err < 0)
2255                                 return err;
2256                 }
2257         }
2258
2259         return 0;
2260 }
2261
2262 /*** Meter Controls ***/
2263
2264 static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
2265                                     struct snd_ctl_elem_info *uinfo)
2266 {
2267         struct usb_mixer_elem_info *elem = kctl->private_data;
2268
2269         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2270         uinfo->count = elem->channels;
2271         uinfo->value.integer.min = 0;
2272         uinfo->value.integer.max = 4095;
2273         uinfo->value.integer.step = 1;
2274         return 0;
2275 }
2276
2277 static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
2278                                    struct snd_ctl_elem_value *ucontrol)
2279 {
2280         struct usb_mixer_elem_info *elem = kctl->private_data;
2281         u16 meter_levels[SCARLETT2_MAX_METERS];
2282         int i, err;
2283
2284         err = scarlett2_usb_get_meter_levels(elem->head.mixer, elem->channels,
2285                                              meter_levels);
2286         if (err < 0)
2287                 return err;
2288
2289         for (i = 0; i < elem->channels; i++)
2290                 ucontrol->value.integer.value[i] = meter_levels[i];
2291
2292         return 0;
2293 }
2294
2295 static const struct snd_kcontrol_new scarlett2_meter_ctl = {
2296         .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2297         .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2298         .name = "",
2299         .info = scarlett2_meter_ctl_info,
2300         .get  = scarlett2_meter_ctl_get
2301 };
2302
2303 static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
2304 {
2305         struct scarlett2_data *private = mixer->private_data;
2306
2307         return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
2308                                      0, private->num_mux_dsts,
2309                                      "Level Meter", NULL);
2310 }
2311
2312 /*** Cleanup/Suspend Callbacks ***/
2313
2314 static void scarlett2_private_free(struct usb_mixer_interface *mixer)
2315 {
2316         struct scarlett2_data *private = mixer->private_data;
2317
2318         cancel_delayed_work_sync(&private->work);
2319         kfree(private);
2320         mixer->private_data = NULL;
2321 }
2322
2323 static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
2324 {
2325         struct scarlett2_data *private = mixer->private_data;
2326
2327         if (cancel_delayed_work_sync(&private->work))
2328                 scarlett2_config_save(private->mixer);
2329 }
2330
2331 /*** Initialisation ***/
2332
2333 static void scarlett2_count_mux_io(struct scarlett2_data *private)
2334 {
2335         const struct scarlett2_device_info *info = private->info;
2336         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2337         int port_type, srcs = 0, dsts = 0;
2338
2339         for (port_type = 0;
2340              port_type < SCARLETT2_PORT_TYPE_COUNT;
2341              port_type++) {
2342                 srcs += port_count[port_type][SCARLETT2_PORT_IN];
2343                 dsts += port_count[port_type][SCARLETT2_PORT_OUT];
2344         }
2345
2346         private->num_mux_srcs = srcs;
2347         private->num_mux_dsts = dsts;
2348 }
2349
2350 /* Look through the interface descriptors for the Focusrite Control
2351  * interface (bInterfaceClass = 255 Vendor Specific Class) and set
2352  * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
2353  * in private
2354  */
2355 static int scarlett2_find_fc_interface(struct usb_device *dev,
2356                                        struct scarlett2_data *private)
2357 {
2358         struct usb_host_config *config = dev->actconfig;
2359         int i;
2360
2361         for (i = 0; i < config->desc.bNumInterfaces; i++) {
2362                 struct usb_interface *intf = config->interface[i];
2363                 struct usb_interface_descriptor *desc =
2364                         &intf->altsetting[0].desc;
2365                 struct usb_endpoint_descriptor *epd;
2366
2367                 if (desc->bInterfaceClass != 255)
2368                         continue;
2369
2370                 epd = get_endpoint(intf->altsetting, 0);
2371                 private->bInterfaceNumber = desc->bInterfaceNumber;
2372                 private->bEndpointAddress = epd->bEndpointAddress &
2373                         USB_ENDPOINT_NUMBER_MASK;
2374                 private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
2375                 private->bInterval = epd->bInterval;
2376                 return 0;
2377         }
2378
2379         return -EINVAL;
2380 }
2381
2382 /* Initialise private data */
2383 static int scarlett2_init_private(struct usb_mixer_interface *mixer,
2384                                   const struct scarlett2_device_info *info)
2385 {
2386         struct scarlett2_data *private =
2387                 kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);
2388
2389         if (!private)
2390                 return -ENOMEM;
2391
2392         mutex_init(&private->usb_mutex);
2393         mutex_init(&private->data_mutex);
2394         INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
2395
2396         mixer->private_data = private;
2397         mixer->private_free = scarlett2_private_free;
2398         mixer->private_suspend = scarlett2_private_suspend;
2399
2400         private->info = info;
2401         scarlett2_count_mux_io(private);
2402         private->scarlett2_seq = 0;
2403         private->mixer = mixer;
2404
2405         return scarlett2_find_fc_interface(mixer->chip->dev, private);
2406 }
2407
2408 /* Cargo cult proprietary initialisation sequence */
2409 static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
2410 {
2411         struct usb_device *dev = mixer->chip->dev;
2412         struct scarlett2_data *private = mixer->private_data;
2413         u8 buf[24];
2414         int err;
2415
2416         if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
2417                 return -EINVAL;
2418
2419         /* step 0 */
2420         err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
2421                                SCARLETT2_USB_CMD_INIT, buf, sizeof(buf));
2422         if (err < 0)
2423                 return err;
2424
2425         /* step 1 */
2426         private->scarlett2_seq = 1;
2427         err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
2428         if (err < 0)
2429                 return err;
2430
2431         /* step 2 */
2432         private->scarlett2_seq = 1;
2433         return scarlett2_usb(mixer, SCARLETT2_USB_INIT_2, NULL, 0, NULL, 84);
2434 }
2435
2436 /* Read configuration from the interface on start */
2437 static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
2438 {
2439         struct scarlett2_data *private = mixer->private_data;
2440         const struct scarlett2_device_info *info = private->info;
2441         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2442         int num_line_out =
2443                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
2444         int num_mixer_out =
2445                 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2446         struct scarlett2_usb_volume_status volume_status;
2447         int err, i;
2448
2449         if (info->level_input_count) {
2450                 err = scarlett2_usb_get_config(
2451                         mixer,
2452                         SCARLETT2_CONFIG_LEVEL_SWITCH,
2453                         info->level_input_count,
2454                         private->level_switch);
2455                 if (err < 0)
2456                         return err;
2457         }
2458
2459         if (info->pad_input_count) {
2460                 err = scarlett2_usb_get_config(
2461                         mixer,
2462                         SCARLETT2_CONFIG_PAD_SWITCH,
2463                         info->pad_input_count,
2464                         private->pad_switch);
2465                 if (err < 0)
2466                         return err;
2467         }
2468
2469         err = scarlett2_update_sync(mixer);
2470         if (err < 0)
2471                 return err;
2472
2473         err = scarlett2_usb_get_volume_status(mixer, &volume_status);
2474         if (err < 0)
2475                 return err;
2476
2477         if (info->line_out_hw_vol)
2478                 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
2479                         private->dim_mute[i] = !!volume_status.dim_mute[i];
2480
2481         private->master_vol = clamp(
2482                 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
2483                 0, SCARLETT2_VOLUME_BIAS);
2484
2485         for (i = 0; i < num_line_out; i++) {
2486                 int volume, mute;
2487
2488                 private->vol_sw_hw_switch[i] =
2489                         info->line_out_hw_vol
2490                                 && volume_status.sw_hw_switch[i];
2491
2492                 volume = private->vol_sw_hw_switch[i]
2493                            ? volume_status.master_vol
2494                            : volume_status.sw_vol[i];
2495                 volume = clamp(volume + SCARLETT2_VOLUME_BIAS,
2496                                0, SCARLETT2_VOLUME_BIAS);
2497                 private->vol[i] = volume;
2498
2499                 mute = private->vol_sw_hw_switch[i]
2500                          ? private->dim_mute[SCARLETT2_BUTTON_MUTE]
2501                          : volume_status.mute_switch[i];
2502                 private->mute_switch[i] = mute;
2503         }
2504
2505         for (i = 0; i < num_mixer_out; i++) {
2506                 err = scarlett2_usb_get_mix(mixer, i);
2507                 if (err < 0)
2508                         return err;
2509         }
2510
2511         return scarlett2_usb_get_mux(mixer);
2512 }
2513
2514 /* Notify on sync change */
2515 static void scarlett2_notify_sync(
2516         struct usb_mixer_interface *mixer)
2517 {
2518         struct scarlett2_data *private = mixer->private_data;
2519
2520         private->sync_updated = 1;
2521
2522         snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2523                        &private->sync_ctl->id);
2524 }
2525
2526 /* Notify on monitor change */
2527 static void scarlett2_notify_monitor(
2528         struct usb_mixer_interface *mixer)
2529 {
2530         struct scarlett2_data *private = mixer->private_data;
2531         const struct scarlett2_device_info *info = private->info;
2532         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2533         int num_line_out =
2534                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
2535         int i;
2536
2537         /* if line_out_hw_vol is 0, there are no controls to update */
2538         if (!info->line_out_hw_vol)
2539                 return;
2540
2541         private->vol_updated = 1;
2542
2543         snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2544                        &private->master_vol_ctl->id);
2545
2546         for (i = 0; i < num_line_out; i++) {
2547                 if (!private->vol_sw_hw_switch[i])
2548                         continue;
2549                 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2550                                &private->vol_ctls[i]->id);
2551         }
2552 }
2553
2554 /* Notify on dim/mute change */
2555 static void scarlett2_notify_dim_mute(
2556         struct usb_mixer_interface *mixer)
2557 {
2558         struct snd_card *card = mixer->chip->card;
2559         struct scarlett2_data *private = mixer->private_data;
2560         const struct scarlett2_device_info *info = private->info;
2561         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2562         int num_line_out =
2563                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
2564         int i;
2565
2566         private->vol_updated = 1;
2567
2568         if (!info->line_out_hw_vol)
2569                 return;
2570
2571         for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
2572                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
2573                                &private->dim_mute_ctls[i]->id);
2574
2575         for (i = 0; i < num_line_out; i++)
2576                 if (private->vol_sw_hw_switch[i])
2577                         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
2578                                        &private->mute_ctls[i]->id);
2579 }
2580
2581 /* Interrupt callback */
2582 static void scarlett2_notify(struct urb *urb)
2583 {
2584         struct usb_mixer_interface *mixer = urb->context;
2585         int len = urb->actual_length;
2586         int ustatus = urb->status;
2587         u32 data;
2588
2589         if (ustatus != 0 || len != 8)
2590                 goto requeue;
2591
2592         data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
2593         if (data & SCARLETT2_USB_NOTIFY_SYNC)
2594                 scarlett2_notify_sync(mixer);
2595         if (data & SCARLETT2_USB_NOTIFY_MONITOR)
2596                 scarlett2_notify_monitor(mixer);
2597         if (data & SCARLETT2_USB_NOTIFY_DIM_MUTE)
2598                 scarlett2_notify_dim_mute(mixer);
2599
2600 requeue:
2601         if (ustatus != -ENOENT &&
2602             ustatus != -ECONNRESET &&
2603             ustatus != -ESHUTDOWN) {
2604                 urb->dev = mixer->chip->dev;
2605                 usb_submit_urb(urb, GFP_ATOMIC);
2606         }
2607 }
2608
2609 static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
2610 {
2611         struct usb_device *dev = mixer->chip->dev;
2612         struct scarlett2_data *private = mixer->private_data;
2613         unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
2614         void *transfer_buffer;
2615
2616         if (mixer->urb) {
2617                 usb_audio_err(mixer->chip,
2618                               "%s: mixer urb already in use!\n", __func__);
2619                 return 0;
2620         }
2621
2622         if (usb_pipe_type_check(dev, pipe))
2623                 return -EINVAL;
2624
2625         mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
2626         if (!mixer->urb)
2627                 return -ENOMEM;
2628
2629         transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
2630         if (!transfer_buffer)
2631                 return -ENOMEM;
2632
2633         usb_fill_int_urb(mixer->urb, dev, pipe,
2634                          transfer_buffer, private->wMaxPacketSize,
2635                          scarlett2_notify, mixer, private->bInterval);
2636
2637         return usb_submit_urb(mixer->urb, GFP_KERNEL);
2638 }
2639
2640 static int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer)
2641 {
2642         const struct scarlett2_device_info **info = scarlett2_devices;
2643         int err;
2644
2645         /* Find device in scarlett2_devices */
2646         while (*info && (*info)->usb_id != mixer->chip->usb_id)
2647                 info++;
2648         if (!*info)
2649                 return -EINVAL;
2650
2651         /* Initialise private data */
2652         err = scarlett2_init_private(mixer, *info);
2653         if (err < 0)
2654                 return err;
2655
2656         /* Send proprietary USB initialisation sequence */
2657         err = scarlett2_usb_init(mixer);
2658         if (err < 0)
2659                 return err;
2660
2661         /* Read volume levels and controls from the interface */
2662         err = scarlett2_read_configs(mixer);
2663         if (err < 0)
2664                 return err;
2665
2666         /* Create the analogue output controls */
2667         err = scarlett2_add_line_out_ctls(mixer);
2668         if (err < 0)
2669                 return err;
2670
2671         /* Create the analogue input controls */
2672         err = scarlett2_add_line_in_ctls(mixer);
2673         if (err < 0)
2674                 return err;
2675
2676         /* Create the input, output, and mixer mux input selections */
2677         err = scarlett2_add_mux_enums(mixer);
2678         if (err < 0)
2679                 return err;
2680
2681         /* Create the matrix mixer controls */
2682         err = scarlett2_add_mixer_ctls(mixer);
2683         if (err < 0)
2684                 return err;
2685
2686         /* Create the level meter controls */
2687         err = scarlett2_add_meter_ctl(mixer);
2688         if (err < 0)
2689                 return err;
2690
2691         /* Create the sync control */
2692         err = scarlett2_add_sync_ctl(mixer);
2693         if (err < 0)
2694                 return err;
2695
2696         /* Set up the interrupt polling */
2697         err = scarlett2_init_notify(mixer);
2698         if (err < 0)
2699                 return err;
2700
2701         return 0;
2702 }
2703
2704 int snd_scarlett_gen2_init(struct usb_mixer_interface *mixer)
2705 {
2706         struct snd_usb_audio *chip = mixer->chip;
2707         int err;
2708
2709         /* only use UAC_VERSION_2 */
2710         if (!mixer->protocol)
2711                 return 0;
2712
2713         if (!(chip->setup & SCARLETT2_ENABLE)) {
2714                 usb_audio_info(chip,
2715                         "Focusrite Scarlett Gen 2/3 Mixer Driver disabled; "
2716                         "use options snd_usb_audio vid=0x%04x pid=0x%04x "
2717                         "device_setup=1 to enable and report any issues "
2718                         "to g@b4.vu",
2719                         USB_ID_VENDOR(chip->usb_id),
2720                         USB_ID_PRODUCT(chip->usb_id));
2721                 return 0;
2722         }
2723
2724         usb_audio_info(chip,
2725                 "Focusrite Scarlett Gen 2/3 Mixer Driver enabled pid=0x%04x",
2726                 USB_ID_PRODUCT(chip->usb_id));
2727
2728         err = snd_scarlett_gen2_controls_create(mixer);
2729         if (err < 0)
2730                 usb_audio_err(mixer->chip,
2731                               "Error initialising Scarlett Mixer Driver: %d",
2732                               err);
2733
2734         return err;
2735 }