ALSA: scarlett2: Split direct_monitor out from monitor_other
[linux-2.6-block.git] / sound / usb / mixer_scarlett2.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *   Focusrite Scarlett 2 Protocol Driver for ALSA
4  *   (including Scarlett 2nd Gen, 3rd Gen, Clarett USB, and Clarett+
5  *   series products)
6  *
7  *   Supported models:
8  *   - 6i6/18i8/18i20 Gen 2
9  *   - Solo/2i2/4i4/8i6/18i8/18i20 Gen 3
10  *   - Clarett 2Pre/4Pre/8Pre USB
11  *   - Clarett+ 2Pre/4Pre/8Pre
12  *
13  *   Copyright (c) 2018-2023 by Geoffrey D. Bennett <g at b4.vu>
14  *   Copyright (c) 2020-2021 by Vladimir Sadovnikov <sadko4u@gmail.com>
15  *   Copyright (c) 2022 by Christian Colglazier <christian@cacolglazier.com>
16  *
17  *   Based on the Scarlett (Gen 1) Driver for ALSA:
18  *
19  *   Copyright (c) 2013 by Tobias Hoffmann
20  *   Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
21  *   Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
22  *   Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
23  *
24  *   Many codes borrowed from audio.c by
25  *     Alan Cox (alan at lxorguk.ukuu.org.uk)
26  *     Thomas Sailer (sailer at ife.ee.ethz.ch)
27  *
28  *   Code cleanup:
29  *   David Henningsson <david.henningsson at canonical.com>
30  */
31
32 /* The protocol was reverse engineered by looking at the communication
33  * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20
34  * (firmware 1083) using usbmon in July-August 2018.
35  *
36  * Scarlett 18i8 support added in April 2019.
37  *
38  * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
39  * for providing usbmon output and testing).
40  *
41  * Scarlett 4i4/8i6 Gen 3 support added in May 2020 (thanks to Laurent
42  * Debricon for donating a 4i4 and to Fredrik Unger for providing 8i6
43  * usbmon output and testing).
44  *
45  * Scarlett 18i8/18i20 Gen 3 support added in June 2020 (thanks to
46  * Darren Jaeckel, Alex Sedlack, and Clovis Lunel for providing usbmon
47  * output, protocol traces and testing).
48  *
49  * Support for loading mixer volume and mux configuration from the
50  * interface during driver initialisation added in May 2021 (thanks to
51  * Vladimir Sadovnikov for figuring out how).
52  *
53  * Support for Solo/2i2 Gen 3 added in May 2021 (thanks to Alexander
54  * Vorona for 2i2 protocol traces).
55  *
56  * Support for phantom power, direct monitoring, speaker switching,
57  * and talkback added in May-June 2021.
58  *
59  * Support for Clarett+ 8Pre added in Aug 2022 by Christian
60  * Colglazier.
61  *
62  * Support for Clarett 8Pre USB added in Sep 2023 (thanks to Philippe
63  * Perrot for confirmation).
64  *
65  * Support for Clarett+ 4Pre and 2Pre added in Sep 2023 (thanks to
66  * Gregory Rozzo for donating a 4Pre, and David Sherwood and Patrice
67  * Peterson for usbmon output).
68  *
69  * Support for Clarett 2Pre and 4Pre USB added in Oct 2023.
70  *
71  * This ALSA mixer gives access to (model-dependent):
72  *  - input, output, mixer-matrix muxes
73  *  - mixer-matrix gain stages
74  *  - gain/volume/mute controls
75  *  - level meters
76  *  - line/inst level, pad, and air controls
77  *  - phantom power, direct monitor, speaker switching, and talkback
78  *    controls
79  *  - disable/enable MSD mode
80  *  - disable/enable standalone mode
81  *
82  * <ditaa>
83  *    /--------------\    18chn            20chn     /--------------\
84  *    | Hardware  in +--+------\    /-------------+--+ ALSA PCM out |
85  *    \--------------/  |      |    |             |  \--------------/
86  *                      |      |    |    /-----\  |
87  *                      |      |    |    |     |  |
88  *                      |      v    v    v     |  |
89  *                      |   +---------------+  |  |
90  *                      |    \ Matrix  Mux /   |  |
91  *                      |     +-----+-----+    |  |
92  *                      |           |          |  |
93  *                      |           |18chn     |  |
94  *                      |           |          |  |
95  *                      |           |     10chn|  |
96  *                      |           v          |  |
97  *                      |     +------------+   |  |
98  *                      |     | Mixer      |   |  |
99  *                      |     |     Matrix |   |  |
100  *                      |     |            |   |  |
101  *                      |     | 18x10 Gain |   |  |
102  *                      |     |   stages   |   |  |
103  *                      |     +-----+------+   |  |
104  *                      |           |          |  |
105  *                      |18chn      |10chn     |  |20chn
106  *                      |           |          |  |
107  *                      |           +----------/  |
108  *                      |           |             |
109  *                      v           v             v
110  *                      ===========================
111  *               +---------------+       +--—------------+
112  *                \ Output  Mux /         \ Capture Mux /
113  *                 +---+---+---+           +-----+-----+
114  *                     |   |                     |
115  *                10chn|   |                     |18chn
116  *                     |   |                     |
117  *  /--------------\   |   |                     |   /--------------\
118  *  | S/PDIF, ADAT |<--/   |10chn                \-->| ALSA PCM in  |
119  *  | Hardware out |       |                         \--------------/
120  *  \--------------/       |
121  *                         v
122  *                  +-------------+    Software gain per channel.
123  *                  | Master Gain |<-- 18i20 only: Switch per channel
124  *                  +------+------+    to select HW or SW gain control.
125  *                         |
126  *                         |10chn
127  *  /--------------\       |
128  *  | Analogue     |<------/
129  *  | Hardware out |
130  *  \--------------/
131  * </ditaa>
132  *
133  * Gen 3 devices have a Mass Storage Device (MSD) mode where a small
134  * disk with registration and driver download information is presented
135  * to the host. To access the full functionality of the device without
136  * proprietary software, MSD mode can be disabled by:
137  * - holding down the 48V button for five seconds while powering on
138  *   the device, or
139  * - using this driver and alsamixer to change the "MSD Mode" setting
140  *   to Off and power-cycling the device
141  */
142
143 #include <linux/slab.h>
144 #include <linux/usb.h>
145 #include <linux/moduleparam.h>
146
147 #include <sound/control.h>
148 #include <sound/tlv.h>
149 #include <sound/hwdep.h>
150
151 #include <uapi/sound/scarlett2.h>
152
153 #include "usbaudio.h"
154 #include "mixer.h"
155 #include "helper.h"
156
157 #include "mixer_scarlett2.h"
158
159 /* device_setup value to allow turning MSD mode back on */
160 #define SCARLETT2_MSD_ENABLE 0x02
161
162 /* device_setup value to disable this mixer driver */
163 #define SCARLETT2_DISABLE 0x04
164
165 /* some gui mixers can't handle negative ctl values */
166 #define SCARLETT2_VOLUME_BIAS 127
167
168 /* mixer range from -80dB to +6dB in 0.5dB steps */
169 #define SCARLETT2_MIXER_MIN_DB -80
170 #define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
171 #define SCARLETT2_MIXER_MAX_DB 6
172 #define SCARLETT2_MIXER_MAX_VALUE \
173         ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
174 #define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1)
175
176 /* map from (dB + 80) * 2 to mixer value
177  * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
178  */
179 static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = {
180         0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
181         2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
182         9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
183         23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
184         54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
185         122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
186         244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
187         487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
188         973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
189         1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
190         3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
191         5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
192         9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
193         16345
194 };
195
196 /* Maximum number of analogue outputs */
197 #define SCARLETT2_ANALOGUE_MAX 10
198
199 /* Maximum number of level and pad switches */
200 #define SCARLETT2_LEVEL_SWITCH_MAX 2
201 #define SCARLETT2_PAD_SWITCH_MAX 8
202 #define SCARLETT2_AIR_SWITCH_MAX 8
203 #define SCARLETT2_PHANTOM_SWITCH_MAX 2
204
205 /* Maximum number of inputs to the mixer */
206 #define SCARLETT2_INPUT_MIX_MAX 25
207
208 /* Maximum number of outputs from the mixer */
209 #define SCARLETT2_OUTPUT_MIX_MAX 12
210
211 /* Maximum number of mixer gain controls */
212 #define SCARLETT2_MIX_MAX (SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX)
213
214 /* Maximum size of the data in the USB mux assignment message:
215  * 20 inputs, 20 outputs, 25 matrix inputs, 12 spare
216  */
217 #define SCARLETT2_MUX_MAX 77
218
219 /* Maximum number of sources (sum of input port counts) */
220 #define SCARLETT2_MAX_SRCS 52
221
222 /* Maximum number of meters (sum of output port counts) */
223 #define SCARLETT2_MAX_METERS 65
224
225 /* Hardware port types:
226  * - None (no input to mux)
227  * - Analogue I/O
228  * - S/PDIF I/O
229  * - ADAT I/O
230  * - Mixer I/O
231  * - PCM I/O
232  */
233 enum {
234         SCARLETT2_PORT_TYPE_NONE,
235         SCARLETT2_PORT_TYPE_ANALOGUE,
236         SCARLETT2_PORT_TYPE_SPDIF,
237         SCARLETT2_PORT_TYPE_ADAT,
238         SCARLETT2_PORT_TYPE_MIX,
239         SCARLETT2_PORT_TYPE_PCM,
240         SCARLETT2_PORT_TYPE_COUNT
241 };
242
243 /* I/O count of each port type kept in struct scarlett2_ports */
244 enum {
245         SCARLETT2_PORT_IN,
246         SCARLETT2_PORT_OUT,
247         SCARLETT2_PORT_DIRNS
248 };
249
250 /* Dim/Mute buttons on the 18i20 */
251 enum {
252         SCARLETT2_BUTTON_MUTE,
253         SCARLETT2_BUTTON_DIM,
254         SCARLETT2_DIM_MUTE_COUNT
255 };
256
257 /* Flash Write State */
258 enum {
259         SCARLETT2_FLASH_WRITE_STATE_IDLE,
260         SCARLETT2_FLASH_WRITE_STATE_SELECTED,
261         SCARLETT2_FLASH_WRITE_STATE_ERASING,
262         SCARLETT2_FLASH_WRITE_STATE_WRITE
263 };
264
265 static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
266         "Mute Playback Switch", "Dim Playback Switch"
267 };
268
269 /* Notification callback functions */
270 struct scarlett2_notification {
271         u32 mask;
272         void (*func)(struct usb_mixer_interface *mixer);
273 };
274
275 static void scarlett2_notify_sync(struct usb_mixer_interface *mixer);
276 static void scarlett2_notify_dim_mute(struct usb_mixer_interface *mixer);
277 static void scarlett2_notify_monitor(struct usb_mixer_interface *mixer);
278 static void scarlett2_notify_input_other(struct usb_mixer_interface *mixer);
279 static void scarlett2_notify_monitor_other(struct usb_mixer_interface *mixer);
280 static void scarlett2_notify_direct_monitor(struct usb_mixer_interface *mixer);
281
282 /* Arrays of notification callback functions */
283
284 static const struct scarlett2_notification scarlett2_notifications[] = {
285         { 0x00000001, NULL }, /* ack, gets ignored */
286         { 0x00000008, scarlett2_notify_sync },
287         { 0x00200000, scarlett2_notify_dim_mute },
288         { 0x00400000, scarlett2_notify_monitor },
289         { 0x00800000, scarlett2_notify_input_other },
290         { 0x01000000, scarlett2_notify_monitor_other },
291         { 0, NULL }
292 };
293
294 static const struct scarlett2_notification scarlett3a_notifications[] = {
295         { 0x00000001, NULL }, /* ack, gets ignored */
296         { 0x00800000, scarlett2_notify_input_other },
297         { 0x01000000, scarlett2_notify_direct_monitor },
298         { 0, NULL }
299 };
300
301 /* Configuration parameters that can be read and written */
302 enum {
303         SCARLETT2_CONFIG_DIM_MUTE,
304         SCARLETT2_CONFIG_LINE_OUT_VOLUME,
305         SCARLETT2_CONFIG_MUTE_SWITCH,
306         SCARLETT2_CONFIG_SW_HW_SWITCH,
307         SCARLETT2_CONFIG_MASTER_VOLUME,
308         SCARLETT2_CONFIG_LEVEL_SWITCH,
309         SCARLETT2_CONFIG_PAD_SWITCH,
310         SCARLETT2_CONFIG_MSD_SWITCH,
311         SCARLETT2_CONFIG_AIR_SWITCH,
312         SCARLETT2_CONFIG_STANDALONE_SWITCH,
313         SCARLETT2_CONFIG_PHANTOM_SWITCH,
314         SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
315         SCARLETT2_CONFIG_DIRECT_MONITOR,
316         SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
317         SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
318         SCARLETT2_CONFIG_TALKBACK_MAP,
319         SCARLETT2_CONFIG_COUNT
320 };
321
322 /* Location, size, and activation command number for the configuration
323  * parameters. Size is in bits and may be 1, 8, or 16.
324  */
325 struct scarlett2_config {
326         u8 offset;
327         u8 size;
328         u8 activate;
329 };
330
331 struct scarlett2_config_set {
332         const struct scarlett2_notification *notifications;
333         const struct scarlett2_config items[SCARLETT2_CONFIG_COUNT];
334 };
335
336 /* Gen 2 devices without SW/HW volume switch: 6i6, 18i8 */
337
338 static const struct scarlett2_config_set scarlett2_config_set_gen2a = {
339         .notifications = scarlett2_notifications,
340         .items = {
341                 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
342                         .offset = 0x34, .size = 16, .activate = 1 },
343
344                 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
345                         .offset = 0x5c, .size = 8, .activate = 1 },
346
347                 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
348                         .offset = 0x7c, .size = 8, .activate = 7 },
349
350                 [SCARLETT2_CONFIG_PAD_SWITCH] = {
351                         .offset = 0x84, .size = 8, .activate = 8 },
352
353                 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
354                         .offset = 0x8d, .size = 8, .activate = 6 },
355         }
356 };
357
358 /* Gen 2 devices with SW/HW volume switch: 18i20 */
359
360 static const struct scarlett2_config_set scarlett2_config_set_gen2b = {
361         .notifications = scarlett2_notifications,
362         .items = {
363                 [SCARLETT2_CONFIG_DIM_MUTE] = {
364                         .offset = 0x31, .size = 8, .activate = 2 },
365
366                 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
367                         .offset = 0x34, .size = 16, .activate = 1 },
368
369                 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
370                         .offset = 0x5c, .size = 8, .activate = 1 },
371
372                 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
373                         .offset = 0x66, .size = 8, .activate = 3 },
374
375                 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
376                         .offset = 0x76, .size = 16 },
377
378                 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
379                         .offset = 0x7c, .size = 8, .activate = 7 },
380
381                 [SCARLETT2_CONFIG_PAD_SWITCH] = {
382                         .offset = 0x84, .size = 8, .activate = 8 },
383
384                 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
385                         .offset = 0x8d, .size = 8, .activate = 6 },
386         }
387 };
388
389 /* Gen 3 devices without a mixer (Solo and 2i2) */
390 static const struct scarlett2_config_set scarlett2_config_set_gen3a = {
391         .notifications = scarlett3a_notifications,
392         .items = {
393                 [SCARLETT2_CONFIG_MSD_SWITCH] = {
394                         .offset = 0x04, .size = 8, .activate = 6 },
395
396                 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
397                         .offset = 0x05, .size = 8, .activate = 6 },
398
399                 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
400                         .offset = 0x06, .size = 8, .activate = 3 },
401
402                 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
403                         .offset = 0x07, .size = 8, .activate = 4 },
404
405                 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
406                         .offset = 0x08, .size = 1, .activate = 7 },
407
408                 [SCARLETT2_CONFIG_AIR_SWITCH] = {
409                         .offset = 0x09, .size = 1, .activate = 8 },
410         }
411 };
412
413 /* Gen 3 devices without SW/HW volume switch: 4i4, 8i6 */
414 static const struct scarlett2_config_set scarlett2_config_set_gen3b = {
415         .notifications = scarlett2_notifications,
416         .items = {
417                 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
418                         .offset = 0x34, .size = 16, .activate = 1 },
419
420                 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
421                         .offset = 0x5c, .size = 8, .activate = 1 },
422
423                 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
424                         .offset = 0x7c, .size = 8, .activate = 7 },
425
426                 [SCARLETT2_CONFIG_PAD_SWITCH] = {
427                         .offset = 0x84, .size = 8, .activate = 8 },
428
429                 [SCARLETT2_CONFIG_AIR_SWITCH] = {
430                         .offset = 0x8c, .size = 8, .activate = 8 },
431
432                 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
433                         .offset = 0x95, .size = 8, .activate = 6 },
434
435                 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
436                         .offset = 0x9c, .size = 1, .activate = 8 },
437
438                 [SCARLETT2_CONFIG_MSD_SWITCH] = {
439                         .offset = 0x9d, .size = 8, .activate = 6 },
440
441                 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
442                         .offset = 0x9e, .size = 8, .activate = 6 },
443         }
444 };
445
446 /* Gen 3 devices with SW/HW volume switch: 18i8, 18i20 */
447 static const struct scarlett2_config_set scarlett2_config_set_gen3c = {
448         .notifications = scarlett2_notifications,
449         .items = {
450                 [SCARLETT2_CONFIG_DIM_MUTE] = {
451                         .offset = 0x31, .size = 8, .activate = 2 },
452
453                 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
454                         .offset = 0x34, .size = 16, .activate = 1 },
455
456                 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
457                         .offset = 0x5c, .size = 8, .activate = 1 },
458
459                 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
460                         .offset = 0x66, .size = 8, .activate = 3 },
461
462                 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
463                         .offset = 0x76, .size = 16 },
464
465                 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
466                         .offset = 0x7c, .size = 8, .activate = 7 },
467
468                 [SCARLETT2_CONFIG_PAD_SWITCH] = {
469                         .offset = 0x84, .size = 8, .activate = 8 },
470
471                 [SCARLETT2_CONFIG_AIR_SWITCH] = {
472                         .offset = 0x8c, .size = 8, .activate = 8 },
473
474                 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
475                         .offset = 0x95, .size = 8, .activate = 6 },
476
477                 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
478                         .offset = 0x9c, .size = 1, .activate = 8 },
479
480                 [SCARLETT2_CONFIG_MSD_SWITCH] = {
481                         .offset = 0x9d, .size = 8, .activate = 6 },
482
483                 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
484                         .offset = 0x9e, .size = 8, .activate = 6 },
485
486                 [SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH] = {
487                         .offset = 0x9f, .size = 1, .activate = 10 },
488
489                 [SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE] = {
490                         .offset = 0xa0, .size = 1, .activate = 10 },
491
492                 [SCARLETT2_CONFIG_TALKBACK_MAP] = {
493                         .offset = 0xb0, .size = 16, .activate = 10 },
494         }
495 };
496
497 /* Clarett USB and Clarett+ devices: 2Pre, 4Pre, 8Pre */
498 static const struct scarlett2_config_set scarlett2_config_set_clarett = {
499         .notifications = scarlett2_notifications,
500         .items = {
501                 [SCARLETT2_CONFIG_DIM_MUTE] = {
502                         .offset = 0x31, .size = 8, .activate = 2 },
503
504                 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
505                         .offset = 0x34, .size = 16, .activate = 1 },
506
507                 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
508                         .offset = 0x5c, .size = 8, .activate = 1 },
509
510                 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
511                         .offset = 0x66, .size = 8, .activate = 3 },
512
513                 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
514                         .offset = 0x76, .size = 16 },
515
516                 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
517                         .offset = 0x7c, .size = 8, .activate = 7 },
518
519                 [SCARLETT2_CONFIG_AIR_SWITCH] = {
520                         .offset = 0x95, .size = 8, .activate = 8 },
521
522                 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
523                         .offset = 0x8d, .size = 8, .activate = 6 },
524         }
525 };
526
527 /* Description of each hardware port type:
528  * - id: hardware ID of this port type
529  * - src_descr: printf format string for mux input selections
530  * - src_num_offset: added to channel number for the fprintf
531  * - dst_descr: printf format string for mixer controls
532  */
533 struct scarlett2_port {
534         u16 id;
535         const char * const src_descr;
536         int src_num_offset;
537         const char * const dst_descr;
538 };
539
540 static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
541         [SCARLETT2_PORT_TYPE_NONE] = {
542                 .id = 0x000,
543                 .src_descr = "Off"
544         },
545         [SCARLETT2_PORT_TYPE_ANALOGUE] = {
546                 .id = 0x080,
547                 .src_descr = "Analogue %d",
548                 .src_num_offset = 1,
549                 .dst_descr = "Analogue Output %02d Playback"
550         },
551         [SCARLETT2_PORT_TYPE_SPDIF] = {
552                 .id = 0x180,
553                 .src_descr = "S/PDIF %d",
554                 .src_num_offset = 1,
555                 .dst_descr = "S/PDIF Output %d Playback"
556         },
557         [SCARLETT2_PORT_TYPE_ADAT] = {
558                 .id = 0x200,
559                 .src_descr = "ADAT %d",
560                 .src_num_offset = 1,
561                 .dst_descr = "ADAT Output %d Playback"
562         },
563         [SCARLETT2_PORT_TYPE_MIX] = {
564                 .id = 0x300,
565                 .src_descr = "Mix %c",
566                 .src_num_offset = 'A',
567                 .dst_descr = "Mixer Input %02d Capture"
568         },
569         [SCARLETT2_PORT_TYPE_PCM] = {
570                 .id = 0x600,
571                 .src_descr = "PCM %d",
572                 .src_num_offset = 1,
573                 .dst_descr = "PCM %02d Capture"
574         },
575 };
576
577 /* Number of mux tables: one for each band of sample rates
578  * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
579  */
580 #define SCARLETT2_MUX_TABLES 3
581
582 /* Maximum number of entries in a mux table */
583 #define SCARLETT2_MAX_MUX_ENTRIES 10
584
585 /* One entry within mux_assignment defines the port type and range of
586  * ports to add to the set_mux message. The end of the list is marked
587  * with count == 0.
588  */
589 struct scarlett2_mux_entry {
590         u8 port_type;
591         u8 start;
592         u8 count;
593 };
594
595 /* Maximum number of entries in a mux table */
596 #define SCARLETT2_MAX_METER_ENTRIES 9
597
598 /* One entry within meter_assignment defines the range of mux outputs
599  * that consecutive meter entries are mapped to. The end of the list
600  * is marked with count == 0.
601  */
602 struct scarlett2_meter_entry {
603         u8 start;
604         u8 count;
605 };
606
607 struct scarlett2_device_info {
608         /* which set of configuration parameters the device uses */
609         const struct scarlett2_config_set *config_set;
610
611         /* support for main/alt speaker switching */
612         u8 has_speaker_switching;
613
614         /* support for talkback microphone */
615         u8 has_talkback;
616
617         /* the number of analogue inputs with a software switchable
618          * level control that can be set to line or instrument
619          */
620         u8 level_input_count;
621
622         /* the first input with a level control (0-based) */
623         u8 level_input_first;
624
625         /* the number of analogue inputs with a software switchable
626          * 10dB pad control
627          */
628         u8 pad_input_count;
629
630         /* the number of analogue inputs with a software switchable
631          * "air" control
632          */
633         u8 air_input_count;
634
635         /* the number of phantom (48V) software switchable controls */
636         u8 phantom_count;
637
638         /* the number of inputs each phantom switch controls */
639         u8 inputs_per_phantom;
640
641         /* the number of direct monitor options
642          * (0 = none, 1 = mono only, 2 = mono/stereo)
643          */
644         u8 direct_monitor;
645
646         /* remap analogue outputs; 18i8 Gen 3 has "line 3/4" connected
647          * internally to the analogue 7/8 outputs
648          */
649         u8 line_out_remap_enable;
650         u8 line_out_remap[SCARLETT2_ANALOGUE_MAX];
651         u8 line_out_unmap[SCARLETT2_ANALOGUE_MAX];
652
653         /* additional description for the line out volume controls */
654         const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
655
656         /* number of sources/destinations of each port type */
657         const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];
658
659         /* layout/order of the entries in the set_mux message */
660         struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES]
661                                                  [SCARLETT2_MAX_MUX_ENTRIES];
662
663         /* map from meter level order returned by
664          * SCARLETT2_USB_GET_METER to index into mux[] entries (same
665          * as the order returned by scarlett2_meter_ctl_get())
666          */
667         struct scarlett2_meter_entry meter_map[SCARLETT2_MAX_METER_ENTRIES];
668 };
669
670 struct scarlett2_data {
671         struct usb_mixer_interface *mixer;
672         struct mutex usb_mutex; /* prevent sending concurrent USB requests */
673         struct mutex data_mutex; /* lock access to this data */
674         u8 hwdep_in_use;
675         u8 selected_flash_segment_id;
676         u8 flash_write_state;
677         struct delayed_work work;
678         const struct scarlett2_device_info *info;
679         const struct scarlett2_config_set *config_set;
680         const char *series_name;
681         __u8 bInterfaceNumber;
682         __u8 bEndpointAddress;
683         __u16 wMaxPacketSize;
684         __u8 bInterval;
685         u8 num_mux_srcs;
686         u8 num_mux_dsts;
687         u8 num_mix_in;
688         u8 num_mix_out;
689         u8 num_line_out;
690         u32 firmware_version;
691         u8 flash_segment_nums[SCARLETT2_SEGMENT_ID_COUNT];
692         u8 flash_segment_blocks[SCARLETT2_SEGMENT_ID_COUNT];
693         u16 scarlett2_seq;
694         u8 sync_updated;
695         u8 vol_updated;
696         u8 dim_mute_updated;
697         u8 input_level_updated;
698         u8 input_pad_updated;
699         u8 input_air_updated;
700         u8 input_phantom_updated;
701         u8 monitor_other_updated;
702         u8 direct_monitor_updated;
703         u8 mux_updated;
704         u8 speaker_switching_switched;
705         u8 sync;
706         u8 master_vol;
707         u8 vol[SCARLETT2_ANALOGUE_MAX];
708         u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
709         u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
710         u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
711         u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
712         u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
713         u8 air_switch[SCARLETT2_AIR_SWITCH_MAX];
714         u8 phantom_switch[SCARLETT2_PHANTOM_SWITCH_MAX];
715         u8 phantom_persistence;
716         u8 direct_monitor_switch;
717         u8 speaker_switching_switch;
718         u8 talkback_switch;
719         u8 talkback_map[SCARLETT2_OUTPUT_MIX_MAX];
720         u8 msd_switch;
721         u8 standalone_switch;
722         u8 meter_level_map[SCARLETT2_MAX_METERS];
723         struct snd_kcontrol *sync_ctl;
724         struct snd_kcontrol *master_vol_ctl;
725         struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
726         struct snd_kcontrol *sw_hw_ctls[SCARLETT2_ANALOGUE_MAX];
727         struct snd_kcontrol *mute_ctls[SCARLETT2_ANALOGUE_MAX];
728         struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT];
729         struct snd_kcontrol *level_ctls[SCARLETT2_LEVEL_SWITCH_MAX];
730         struct snd_kcontrol *pad_ctls[SCARLETT2_PAD_SWITCH_MAX];
731         struct snd_kcontrol *air_ctls[SCARLETT2_AIR_SWITCH_MAX];
732         struct snd_kcontrol *phantom_ctls[SCARLETT2_PHANTOM_SWITCH_MAX];
733         struct snd_kcontrol *mux_ctls[SCARLETT2_MUX_MAX];
734         struct snd_kcontrol *direct_monitor_ctl;
735         struct snd_kcontrol *speaker_switching_ctl;
736         struct snd_kcontrol *talkback_ctl;
737         u8 mux[SCARLETT2_MUX_MAX];
738         u8 mix[SCARLETT2_MIX_MAX];
739 };
740
741 /*** Model-specific data ***/
742
743 static const struct scarlett2_device_info s6i6_gen2_info = {
744         .config_set = &scarlett2_config_set_gen2a,
745         .level_input_count = 2,
746         .pad_input_count = 2,
747
748         .line_out_descrs = {
749                 "Headphones 1 L",
750                 "Headphones 1 R",
751                 "Headphones 2 L",
752                 "Headphones 2 R",
753         },
754
755         .port_count = {
756                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
757                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  4,  4 },
758                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
759                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
760                 [SCARLETT2_PORT_TYPE_PCM]      = {  6,  6 },
761         },
762
763         .mux_assignment = { {
764                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
765                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
766                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
767                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
768                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
769                 { 0,                            0,  0 },
770         }, {
771                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
772                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
773                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
774                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
775                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
776                 { 0,                            0,  0 },
777         }, {
778                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
779                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
780                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
781                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
782                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
783                 { 0,                            0,  0 },
784         } },
785
786         .meter_map = {
787                 { 24,  6 },
788                 {  0, 24 },
789                 {  0,  0 },
790         }
791 };
792
793 static const struct scarlett2_device_info s18i8_gen2_info = {
794         .config_set = &scarlett2_config_set_gen2a,
795         .level_input_count = 2,
796         .pad_input_count = 4,
797
798         .line_out_descrs = {
799                 "Monitor L",
800                 "Monitor R",
801                 "Headphones 1 L",
802                 "Headphones 1 R",
803                 "Headphones 2 L",
804                 "Headphones 2 R",
805         },
806
807         .port_count = {
808                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
809                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  8,  6 },
810                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
811                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  0 },
812                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
813                 [SCARLETT2_PORT_TYPE_PCM]      = {  8, 18 },
814         },
815
816         .mux_assignment = { {
817                 { SCARLETT2_PORT_TYPE_PCM,      0, 18 },
818                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
819                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
820                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
821                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
822                 { 0,                            0,  0 },
823         }, {
824                 { SCARLETT2_PORT_TYPE_PCM,      0, 14 },
825                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
826                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
827                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
828                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
829                 { 0,                            0,  0 },
830         }, {
831                 { SCARLETT2_PORT_TYPE_PCM,      0, 10 },
832                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
833                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
834                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
835                 { SCARLETT2_PORT_TYPE_NONE,     0,  4 },
836                 { 0,                            0,  0 },
837         } },
838
839         .meter_map = {
840                 { 26, 18 },
841                 {  0, 26 },
842                 {  0,  0 },
843         }
844 };
845
846 static const struct scarlett2_device_info s18i20_gen2_info = {
847         .config_set = &scarlett2_config_set_gen2b,
848
849         .line_out_descrs = {
850                 "Monitor L",
851                 "Monitor R",
852                 NULL,
853                 NULL,
854                 NULL,
855                 NULL,
856                 "Headphones 1 L",
857                 "Headphones 1 R",
858                 "Headphones 2 L",
859                 "Headphones 2 R",
860         },
861
862         .port_count = {
863                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
864                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  8, 10 },
865                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
866                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  8 },
867                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
868                 [SCARLETT2_PORT_TYPE_PCM]      = { 20, 18 },
869         },
870
871         .mux_assignment = { {
872                 { SCARLETT2_PORT_TYPE_PCM,      0, 18 },
873                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
874                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
875                 { SCARLETT2_PORT_TYPE_ADAT,     0,  8 },
876                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
877                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
878                 { 0,                            0,  0 },
879         }, {
880                 { SCARLETT2_PORT_TYPE_PCM,      0, 14 },
881                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
882                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
883                 { SCARLETT2_PORT_TYPE_ADAT,     0,  4 },
884                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
885                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
886                 { 0,                            0,  0 },
887         }, {
888                 { SCARLETT2_PORT_TYPE_PCM,      0, 10 },
889                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
890                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
891                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
892                 { SCARLETT2_PORT_TYPE_NONE,     0,  6 },
893                 { 0,                            0,  0 },
894         } },
895
896         .meter_map = {
897                 { 38, 18 },
898                 {  0, 38 },
899                 {  0,  0 },
900         }
901 };
902
903 static const struct scarlett2_device_info solo_gen3_info = {
904         .config_set = &scarlett2_config_set_gen3a,
905         .level_input_count = 1,
906         .level_input_first = 1,
907         .air_input_count = 1,
908         .phantom_count = 1,
909         .inputs_per_phantom = 1,
910         .direct_monitor = 1,
911 };
912
913 static const struct scarlett2_device_info s2i2_gen3_info = {
914         .config_set = &scarlett2_config_set_gen3a,
915         .level_input_count = 2,
916         .air_input_count = 2,
917         .phantom_count = 1,
918         .inputs_per_phantom = 2,
919         .direct_monitor = 2,
920 };
921
922 static const struct scarlett2_device_info s4i4_gen3_info = {
923         .config_set = &scarlett2_config_set_gen3b,
924         .level_input_count = 2,
925         .pad_input_count = 2,
926         .air_input_count = 2,
927         .phantom_count = 1,
928         .inputs_per_phantom = 2,
929
930         .line_out_descrs = {
931                 "Monitor L",
932                 "Monitor R",
933                 "Headphones L",
934                 "Headphones R",
935         },
936
937         .port_count = {
938                 [SCARLETT2_PORT_TYPE_NONE]     = { 1, 0 },
939                 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
940                 [SCARLETT2_PORT_TYPE_MIX]      = { 6, 8 },
941                 [SCARLETT2_PORT_TYPE_PCM]      = { 4, 6 },
942         },
943
944         .mux_assignment = { {
945                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
946                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
947                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
948                 { SCARLETT2_PORT_TYPE_NONE,     0, 16 },
949                 { 0,                            0,  0 },
950         }, {
951                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
952                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
953                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
954                 { SCARLETT2_PORT_TYPE_NONE,     0, 16 },
955                 { 0,                            0,  0 },
956         }, {
957                 { SCARLETT2_PORT_TYPE_PCM,      0,  6 },
958                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
959                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
960                 { SCARLETT2_PORT_TYPE_NONE,     0, 16 },
961                 { 0,                            0,  0 },
962         } },
963
964         .meter_map = {
965                 { 12,  6 },
966                 {  0, 12 },
967                 {  0,  0 },
968         }
969 };
970
971 static const struct scarlett2_device_info s8i6_gen3_info = {
972         .config_set = &scarlett2_config_set_gen3b,
973         .level_input_count = 2,
974         .pad_input_count = 2,
975         .air_input_count = 2,
976         .phantom_count = 1,
977         .inputs_per_phantom = 2,
978
979         .line_out_descrs = {
980                 "Headphones 1 L",
981                 "Headphones 1 R",
982                 "Headphones 2 L",
983                 "Headphones 2 R",
984         },
985
986         .port_count = {
987                 [SCARLETT2_PORT_TYPE_NONE]     = { 1,  0 },
988                 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6,  4 },
989                 [SCARLETT2_PORT_TYPE_SPDIF]    = { 2,  2 },
990                 [SCARLETT2_PORT_TYPE_MIX]      = { 8,  8 },
991                 [SCARLETT2_PORT_TYPE_PCM]      = { 6, 10 },
992         },
993
994         .mux_assignment = { {
995                 { SCARLETT2_PORT_TYPE_PCM,      0,  8 },
996                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
997                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
998                 { SCARLETT2_PORT_TYPE_PCM,      8,  2 },
999                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
1000                 { SCARLETT2_PORT_TYPE_NONE,     0, 18 },
1001                 { 0,                            0,  0 },
1002         }, {
1003                 { SCARLETT2_PORT_TYPE_PCM,      0,  8 },
1004                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
1005                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
1006                 { SCARLETT2_PORT_TYPE_PCM,      8,  2 },
1007                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
1008                 { SCARLETT2_PORT_TYPE_NONE,     0, 18 },
1009                 { 0,                            0,  0 },
1010         }, {
1011                 { SCARLETT2_PORT_TYPE_PCM,      0,  8 },
1012                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
1013                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
1014                 { SCARLETT2_PORT_TYPE_PCM,      8,  2 },
1015                 { SCARLETT2_PORT_TYPE_MIX,      0,  8 },
1016                 { SCARLETT2_PORT_TYPE_NONE,     0, 18 },
1017                 { 0,                            0,  0 },
1018         } },
1019
1020         .meter_map = {
1021                 { 14, 8 },
1022                 {  0, 6 },
1023                 { 22, 2 },
1024                 {  6, 8 },
1025                 {  0, 0 },
1026         }
1027 };
1028
1029 static const struct scarlett2_device_info s18i8_gen3_info = {
1030         .config_set = &scarlett2_config_set_gen3c,
1031         .has_speaker_switching = 1,
1032         .level_input_count = 2,
1033         .pad_input_count = 4,
1034         .air_input_count = 4,
1035         .phantom_count = 2,
1036         .inputs_per_phantom = 2,
1037
1038         .line_out_remap_enable = 1,
1039         .line_out_remap = { 0, 1, 6, 7, 2, 3, 4, 5 },
1040         .line_out_unmap = { 0, 1, 4, 5, 6, 7, 2, 3 },
1041
1042         .line_out_descrs = {
1043                 "Monitor L",
1044                 "Monitor R",
1045                 "Alt Monitor L",
1046                 "Alt Monitor R",
1047                 "Headphones 1 L",
1048                 "Headphones 1 R",
1049                 "Headphones 2 L",
1050                 "Headphones 2 R",
1051         },
1052
1053         .port_count = {
1054                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
1055                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  8,  8 },
1056                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
1057                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  0 },
1058                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 20 },
1059                 [SCARLETT2_PORT_TYPE_PCM]      = {  8, 20 },
1060         },
1061
1062         .mux_assignment = { {
1063                 { SCARLETT2_PORT_TYPE_PCM,       0, 10 },
1064                 { SCARLETT2_PORT_TYPE_PCM,      12,  8 },
1065                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
1066                 { SCARLETT2_PORT_TYPE_ANALOGUE,  6,  2 },
1067                 { SCARLETT2_PORT_TYPE_ANALOGUE,  2,  4 },
1068                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
1069                 { SCARLETT2_PORT_TYPE_PCM,      10,  2 },
1070                 { SCARLETT2_PORT_TYPE_MIX,       0, 20 },
1071                 { SCARLETT2_PORT_TYPE_NONE,      0, 10 },
1072                 { 0,                             0,  0 },
1073         }, {
1074                 { SCARLETT2_PORT_TYPE_PCM,       0, 10 },
1075                 { SCARLETT2_PORT_TYPE_PCM,      12,  4 },
1076                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
1077                 { SCARLETT2_PORT_TYPE_ANALOGUE,  6,  2 },
1078                 { SCARLETT2_PORT_TYPE_ANALOGUE,  2,  4 },
1079                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
1080                 { SCARLETT2_PORT_TYPE_PCM,      10,  2 },
1081                 { SCARLETT2_PORT_TYPE_MIX,       0, 20 },
1082                 { SCARLETT2_PORT_TYPE_NONE,      0, 10 },
1083                 { 0,                             0,  0 },
1084         }, {
1085                 { SCARLETT2_PORT_TYPE_PCM,       0, 10 },
1086                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
1087                 { SCARLETT2_PORT_TYPE_ANALOGUE,  6,  2 },
1088                 { SCARLETT2_PORT_TYPE_ANALOGUE,  2,  4 },
1089                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
1090                 { SCARLETT2_PORT_TYPE_MIX,       0, 20 },
1091                 { SCARLETT2_PORT_TYPE_NONE,      0, 10 },
1092                 { 0,                             0,  0 },
1093         } },
1094
1095         .meter_map = {
1096                 { 30, 10 },
1097                 { 42,  8 },
1098                 {  0,  2 },
1099                 {  6,  2 },
1100                 {  2,  4 },
1101                 {  8,  2 },
1102                 { 40,  2 },
1103                 { 10, 20 },
1104                 {  0,  0 }
1105         }
1106 };
1107
1108 static const struct scarlett2_device_info s18i20_gen3_info = {
1109         .config_set = &scarlett2_config_set_gen3c,
1110         .has_speaker_switching = 1,
1111         .has_talkback = 1,
1112         .level_input_count = 2,
1113         .pad_input_count = 8,
1114         .air_input_count = 8,
1115         .phantom_count = 2,
1116         .inputs_per_phantom = 4,
1117
1118         .line_out_descrs = {
1119                 "Monitor 1 L",
1120                 "Monitor 1 R",
1121                 "Monitor 2 L",
1122                 "Monitor 2 R",
1123                 NULL,
1124                 NULL,
1125                 "Headphones 1 L",
1126                 "Headphones 1 R",
1127                 "Headphones 2 L",
1128                 "Headphones 2 R",
1129         },
1130
1131         .port_count = {
1132                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
1133                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  9, 10 },
1134                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
1135                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  8 },
1136                 [SCARLETT2_PORT_TYPE_MIX]      = { 12, 25 },
1137                 [SCARLETT2_PORT_TYPE_PCM]      = { 20, 20 },
1138         },
1139
1140         .mux_assignment = { {
1141                 { SCARLETT2_PORT_TYPE_PCM,       0,  8 },
1142                 { SCARLETT2_PORT_TYPE_PCM,      10, 10 },
1143                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0, 10 },
1144                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
1145                 { SCARLETT2_PORT_TYPE_ADAT,      0,  8 },
1146                 { SCARLETT2_PORT_TYPE_PCM,       8,  2 },
1147                 { SCARLETT2_PORT_TYPE_MIX,       0, 25 },
1148                 { SCARLETT2_PORT_TYPE_NONE,      0, 12 },
1149                 { 0,                             0,  0 },
1150         }, {
1151                 { SCARLETT2_PORT_TYPE_PCM,       0,  8 },
1152                 { SCARLETT2_PORT_TYPE_PCM,      10,  8 },
1153                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0, 10 },
1154                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
1155                 { SCARLETT2_PORT_TYPE_ADAT,      0,  8 },
1156                 { SCARLETT2_PORT_TYPE_PCM,       8,  2 },
1157                 { SCARLETT2_PORT_TYPE_MIX,       0, 25 },
1158                 { SCARLETT2_PORT_TYPE_NONE,      0, 10 },
1159                 { 0,                             0,  0 },
1160         }, {
1161                 { SCARLETT2_PORT_TYPE_PCM,       0, 10 },
1162                 { SCARLETT2_PORT_TYPE_ANALOGUE,  0, 10 },
1163                 { SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
1164                 { SCARLETT2_PORT_TYPE_NONE,      0, 24 },
1165                 { 0,                             0,  0 },
1166         } },
1167
1168         .meter_map = {
1169                 { 45,  8 },
1170                 { 55, 10 },
1171                 {  0, 20 },
1172                 { 53,  2 },
1173                 { 20, 25 },
1174                 {  0,  0 },
1175         }
1176 };
1177
1178 static const struct scarlett2_device_info clarett_2pre_info = {
1179         .config_set = &scarlett2_config_set_clarett,
1180         .level_input_count = 2,
1181         .air_input_count = 2,
1182
1183         .line_out_descrs = {
1184                 "Monitor L",
1185                 "Monitor R",
1186                 "Headphones L",
1187                 "Headphones R",
1188         },
1189
1190         .port_count = {
1191                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
1192                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  2,  4 },
1193                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  0 },
1194                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  0 },
1195                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
1196                 [SCARLETT2_PORT_TYPE_PCM]      = {  4, 12 },
1197         },
1198
1199         .mux_assignment = { {
1200                 { SCARLETT2_PORT_TYPE_PCM,      0, 12 },
1201                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
1202                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
1203                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
1204                 { 0,                            0,  0 },
1205         }, {
1206                 { SCARLETT2_PORT_TYPE_PCM,      0,  8 },
1207                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
1208                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
1209                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
1210                 { 0,                            0,  0 },
1211         }, {
1212                 { SCARLETT2_PORT_TYPE_PCM,      0,  2 },
1213                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
1214                 { SCARLETT2_PORT_TYPE_NONE,     0, 26 },
1215                 { 0,                            0,  0 },
1216         } },
1217
1218         .meter_map = {
1219                 { 22, 12 },
1220                 {  0, 22 },
1221                 {  0,  0 }
1222         }
1223 };
1224
1225 static const struct scarlett2_device_info clarett_4pre_info = {
1226         .config_set = &scarlett2_config_set_clarett,
1227         .level_input_count = 2,
1228         .air_input_count = 4,
1229
1230         .line_out_descrs = {
1231                 "Monitor L",
1232                 "Monitor R",
1233                 "Headphones 1 L",
1234                 "Headphones 1 R",
1235                 "Headphones 2 L",
1236                 "Headphones 2 R",
1237         },
1238
1239         .port_count = {
1240                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
1241                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  8,  6 },
1242                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
1243                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  0 },
1244                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
1245                 [SCARLETT2_PORT_TYPE_PCM]      = {  8, 18 },
1246         },
1247
1248         .mux_assignment = { {
1249                 { SCARLETT2_PORT_TYPE_PCM,      0, 18 },
1250                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
1251                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
1252                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
1253                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
1254                 { 0,                            0,  0 },
1255         }, {
1256                 { SCARLETT2_PORT_TYPE_PCM,      0, 14 },
1257                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
1258                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
1259                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
1260                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
1261                 { 0,                            0,  0 },
1262         }, {
1263                 { SCARLETT2_PORT_TYPE_PCM,      0, 12 },
1264                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
1265                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
1266                 { SCARLETT2_PORT_TYPE_NONE,     0, 24 },
1267                 { 0,                            0,  0 },
1268         } },
1269
1270         .meter_map = {
1271                 { 26, 18 },
1272                 {  0, 26 },
1273                 {  0,  0 }
1274         }
1275 };
1276
1277 static const struct scarlett2_device_info clarett_8pre_info = {
1278         .config_set = &scarlett2_config_set_clarett,
1279         .level_input_count = 2,
1280         .air_input_count = 8,
1281
1282         .line_out_descrs = {
1283                 "Monitor L",
1284                 "Monitor R",
1285                 NULL,
1286                 NULL,
1287                 NULL,
1288                 NULL,
1289                 "Headphones 1 L",
1290                 "Headphones 1 R",
1291                 "Headphones 2 L",
1292                 "Headphones 2 R",
1293         },
1294
1295         .port_count = {
1296                 [SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
1297                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {  8, 10 },
1298                 [SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
1299                 [SCARLETT2_PORT_TYPE_ADAT]     = {  8,  8 },
1300                 [SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
1301                 [SCARLETT2_PORT_TYPE_PCM]      = { 20, 18 },
1302         },
1303
1304         .mux_assignment = { {
1305                 { SCARLETT2_PORT_TYPE_PCM,      0, 18 },
1306                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1307                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
1308                 { SCARLETT2_PORT_TYPE_ADAT,     0,  8 },
1309                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
1310                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
1311                 { 0,                            0,  0 },
1312         }, {
1313                 { SCARLETT2_PORT_TYPE_PCM,      0, 14 },
1314                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1315                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
1316                 { SCARLETT2_PORT_TYPE_ADAT,     0,  4 },
1317                 { SCARLETT2_PORT_TYPE_MIX,      0, 18 },
1318                 { SCARLETT2_PORT_TYPE_NONE,     0,  8 },
1319                 { 0,                            0,  0 },
1320         }, {
1321                 { SCARLETT2_PORT_TYPE_PCM,      0, 12 },
1322                 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1323                 { SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
1324                 { SCARLETT2_PORT_TYPE_NONE,     0, 22 },
1325                 { 0,                            0,  0 },
1326         } },
1327
1328         .meter_map = {
1329                 { 38, 18 },
1330                 {  0, 38 },
1331                 {  0,  0 }
1332         }
1333 };
1334
1335 struct scarlett2_device_entry {
1336         const u32 usb_id; /* USB device identifier */
1337         const struct scarlett2_device_info *info;
1338         const char *series_name;
1339 };
1340
1341 static const struct scarlett2_device_entry scarlett2_devices[] = {
1342         /* Supported Gen 2 devices */
1343         { USB_ID(0x1235, 0x8203), &s6i6_gen2_info, "Scarlett Gen 2" },
1344         { USB_ID(0x1235, 0x8204), &s18i8_gen2_info, "Scarlett Gen 2" },
1345         { USB_ID(0x1235, 0x8201), &s18i20_gen2_info, "Scarlett Gen 2" },
1346
1347         /* Supported Gen 3 devices */
1348         { USB_ID(0x1235, 0x8211), &solo_gen3_info, "Scarlett Gen 3" },
1349         { USB_ID(0x1235, 0x8210), &s2i2_gen3_info, "Scarlett Gen 3" },
1350         { USB_ID(0x1235, 0x8212), &s4i4_gen3_info, "Scarlett Gen 3" },
1351         { USB_ID(0x1235, 0x8213), &s8i6_gen3_info, "Scarlett Gen 3" },
1352         { USB_ID(0x1235, 0x8214), &s18i8_gen3_info, "Scarlett Gen 3" },
1353         { USB_ID(0x1235, 0x8215), &s18i20_gen3_info, "Scarlett Gen 3" },
1354
1355         /* Supported Clarett USB/Clarett+ devices */
1356         { USB_ID(0x1235, 0x8206), &clarett_2pre_info, "Clarett USB" },
1357         { USB_ID(0x1235, 0x8207), &clarett_4pre_info, "Clarett USB" },
1358         { USB_ID(0x1235, 0x8208), &clarett_8pre_info, "Clarett USB" },
1359         { USB_ID(0x1235, 0x820a), &clarett_2pre_info, "Clarett+" },
1360         { USB_ID(0x1235, 0x820b), &clarett_4pre_info, "Clarett+" },
1361         { USB_ID(0x1235, 0x820c), &clarett_8pre_info, "Clarett+" },
1362
1363         /* End of list */
1364         { 0, NULL },
1365 };
1366
1367 /* get the starting port index number for a given port type/direction */
1368 static int scarlett2_get_port_start_num(
1369         const int port_count[][SCARLETT2_PORT_DIRNS],
1370         int direction, int port_type)
1371 {
1372         int i, num = 0;
1373
1374         for (i = 0; i < port_type; i++)
1375                 num += port_count[i][direction];
1376
1377         return num;
1378 }
1379
1380 /*** USB Interactions ***/
1381
1382 /* Commands for sending/receiving requests/responses */
1383 #define SCARLETT2_USB_CMD_INIT 0
1384 #define SCARLETT2_USB_CMD_REQ  2
1385 #define SCARLETT2_USB_CMD_RESP 3
1386
1387 #define SCARLETT2_USB_INIT_1        0x00000000
1388 #define SCARLETT2_USB_INIT_2        0x00000002
1389 #define SCARLETT2_USB_REBOOT        0x00000003
1390 #define SCARLETT2_USB_GET_METER     0x00001001
1391 #define SCARLETT2_USB_GET_MIX       0x00002001
1392 #define SCARLETT2_USB_SET_MIX       0x00002002
1393 #define SCARLETT2_USB_GET_MUX       0x00003001
1394 #define SCARLETT2_USB_SET_MUX       0x00003002
1395 #define SCARLETT2_USB_INFO_FLASH    0x00004000
1396 #define SCARLETT2_USB_INFO_SEGMENT  0x00004001
1397 #define SCARLETT2_USB_ERASE_SEGMENT 0x00004002
1398 #define SCARLETT2_USB_GET_ERASE     0x00004003
1399 #define SCARLETT2_USB_WRITE_SEGMENT 0x00004004
1400 #define SCARLETT2_USB_GET_SYNC      0x00006004
1401 #define SCARLETT2_USB_GET_DATA      0x00800000
1402 #define SCARLETT2_USB_SET_DATA      0x00800001
1403 #define SCARLETT2_USB_DATA_CMD      0x00800002
1404
1405 #define SCARLETT2_USB_CONFIG_SAVE 6
1406
1407 #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
1408
1409 #define SCARLETT2_FLASH_BLOCK_SIZE 4096
1410 #define SCARLETT2_FLASH_WRITE_MAX 1024
1411 #define SCARLETT2_SEGMENT_NUM_MIN 1
1412 #define SCARLETT2_SEGMENT_NUM_MAX 4
1413
1414 #define SCARLETT2_SEGMENT_SETTINGS_NAME "App_Settings"
1415 #define SCARLETT2_SEGMENT_FIRMWARE_NAME "App_Upgrade"
1416
1417 /* proprietary request/response format */
1418 struct scarlett2_usb_packet {
1419         __le32 cmd;
1420         __le16 size;
1421         __le16 seq;
1422         __le32 error;
1423         __le32 pad;
1424         u8 data[];
1425 };
1426
1427 static void scarlett2_fill_request_header(struct scarlett2_data *private,
1428                                           struct scarlett2_usb_packet *req,
1429                                           u32 cmd, u16 req_size)
1430 {
1431         /* sequence must go up by 1 for each request */
1432         u16 seq = private->scarlett2_seq++;
1433
1434         req->cmd = cpu_to_le32(cmd);
1435         req->size = cpu_to_le16(req_size);
1436         req->seq = cpu_to_le16(seq);
1437         req->error = 0;
1438         req->pad = 0;
1439 }
1440
1441 static int scarlett2_usb_tx(struct usb_device *dev, int interface,
1442                             void *buf, u16 size)
1443 {
1444         return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1445                         SCARLETT2_USB_CMD_REQ,
1446                         USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1447                         0, interface, buf, size);
1448 }
1449
1450 static int scarlett2_usb_rx(struct usb_device *dev, int interface,
1451                             u32 usb_req, void *buf, u16 size)
1452 {
1453         return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
1454                         usb_req,
1455                         USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1456                         0, interface, buf, size);
1457 }
1458
1459 /* Send a proprietary format request to the Scarlett interface */
1460 static int scarlett2_usb(
1461         struct usb_mixer_interface *mixer, u32 cmd,
1462         void *req_data, u16 req_size, void *resp_data, u16 resp_size)
1463 {
1464         struct scarlett2_data *private = mixer->private_data;
1465         struct usb_device *dev = mixer->chip->dev;
1466         struct scarlett2_usb_packet *req, *resp = NULL;
1467         size_t req_buf_size = struct_size(req, data, req_size);
1468         size_t resp_buf_size = struct_size(resp, data, resp_size);
1469         int err;
1470
1471         req = kmalloc(req_buf_size, GFP_KERNEL);
1472         if (!req) {
1473                 err = -ENOMEM;
1474                 goto error;
1475         }
1476
1477         resp = kmalloc(resp_buf_size, GFP_KERNEL);
1478         if (!resp) {
1479                 err = -ENOMEM;
1480                 goto error;
1481         }
1482
1483         mutex_lock(&private->usb_mutex);
1484
1485         /* build request message and send it */
1486
1487         scarlett2_fill_request_header(private, req, cmd, req_size);
1488
1489         if (req_size)
1490                 memcpy(req->data, req_data, req_size);
1491
1492         err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
1493                                req, req_buf_size);
1494
1495         if (err != req_buf_size) {
1496                 usb_audio_err(
1497                         mixer->chip,
1498                         "%s USB request result cmd %x was %d\n",
1499                         private->series_name, cmd, err);
1500                 err = -EINVAL;
1501                 goto unlock;
1502         }
1503
1504         /* send a second message to get the response */
1505
1506         err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
1507                                SCARLETT2_USB_CMD_RESP,
1508                                resp, resp_buf_size);
1509
1510         /* validate the response */
1511
1512         if (err != resp_buf_size) {
1513
1514                 /* ESHUTDOWN and EPROTO are valid responses to a
1515                  * reboot request
1516                  */
1517                 if (cmd == SCARLETT2_USB_REBOOT &&
1518                     (err == -ESHUTDOWN || err == -EPROTO)) {
1519                         err = 0;
1520                         goto unlock;
1521                 }
1522
1523                 usb_audio_err(
1524                         mixer->chip,
1525                         "%s USB response result cmd %x was %d expected %zu\n",
1526                         private->series_name, cmd, err, resp_buf_size);
1527                 err = -EINVAL;
1528                 goto unlock;
1529         }
1530
1531         /* cmd/seq/size should match except when initialising
1532          * seq sent = 1, response = 0
1533          */
1534         if (resp->cmd != req->cmd ||
1535             (resp->seq != req->seq &&
1536                 (le16_to_cpu(req->seq) != 1 || resp->seq != 0)) ||
1537             resp_size != le16_to_cpu(resp->size) ||
1538             resp->error ||
1539             resp->pad) {
1540                 usb_audio_err(
1541                         mixer->chip,
1542                         "%s USB invalid response; "
1543                            "cmd tx/rx %d/%d seq %d/%d size %d/%d "
1544                            "error %d pad %d\n",
1545                         private->series_name,
1546                         le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
1547                         le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
1548                         resp_size, le16_to_cpu(resp->size),
1549                         le32_to_cpu(resp->error),
1550                         le32_to_cpu(resp->pad));
1551                 err = -EINVAL;
1552                 goto unlock;
1553         }
1554
1555         if (resp_data && resp_size > 0)
1556                 memcpy(resp_data, resp->data, resp_size);
1557
1558 unlock:
1559         mutex_unlock(&private->usb_mutex);
1560 error:
1561         kfree(req);
1562         kfree(resp);
1563         return err;
1564 }
1565
1566 /* Send a USB message to get data; result placed in *buf */
1567 static int scarlett2_usb_get(
1568         struct usb_mixer_interface *mixer,
1569         int offset, void *buf, int size)
1570 {
1571         struct {
1572                 __le32 offset;
1573                 __le32 size;
1574         } __packed req;
1575
1576         req.offset = cpu_to_le32(offset);
1577         req.size = cpu_to_le32(size);
1578         return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
1579                              &req, sizeof(req), buf, size);
1580 }
1581
1582 /* Return true if the given configuration item is present in the
1583  * configuration set used by this device.
1584  */
1585 static int scarlett2_has_config_item(
1586         struct scarlett2_data *private, int config_item_num)
1587 {
1588         return !!private->config_set->items[config_item_num].offset;
1589 }
1590
1591 /* Send a USB message to get configuration parameters; result placed in *buf */
1592 static int scarlett2_usb_get_config(
1593         struct usb_mixer_interface *mixer,
1594         int config_item_num, int count, void *buf)
1595 {
1596         struct scarlett2_data *private = mixer->private_data;
1597         const struct scarlett2_config *config_item =
1598                 &private->config_set->items[config_item_num];
1599         int size, err, i;
1600         u8 *buf_8;
1601         u8 value;
1602
1603         /* Check that the configuration item is present in the
1604          * configuration set used by this device
1605          */
1606         if (!config_item->offset)
1607                 return -EFAULT;
1608
1609         /* For byte-sized parameters, retrieve directly into buf */
1610         if (config_item->size >= 8) {
1611                 size = config_item->size / 8 * count;
1612                 err = scarlett2_usb_get(mixer, config_item->offset, buf, size);
1613                 if (err < 0)
1614                         return err;
1615                 if (size == 2) {
1616                         u16 *buf_16 = buf;
1617
1618                         for (i = 0; i < count; i++, buf_16++)
1619                                 *buf_16 = le16_to_cpu(*(__le16 *)buf_16);
1620                 }
1621                 return 0;
1622         }
1623
1624         /* For bit-sized parameters, retrieve into value */
1625         err = scarlett2_usb_get(mixer, config_item->offset, &value, 1);
1626         if (err < 0)
1627                 return err;
1628
1629         /* then unpack from value into buf[] */
1630         buf_8 = buf;
1631         for (i = 0; i < 8 && i < count; i++, value >>= 1)
1632                 *buf_8++ = value & 1;
1633
1634         return 0;
1635 }
1636
1637 /* Send a SCARLETT2_USB_SET_DATA command.
1638  * offset: location in the device's data space
1639  * size: size in bytes of the value (1, 2, 4)
1640  */
1641 static int scarlett2_usb_set_data(
1642         struct usb_mixer_interface *mixer,
1643         int offset, int size, int value)
1644 {
1645         struct scarlett2_data *private = mixer->private_data;
1646         struct {
1647                 __le32 offset;
1648                 __le32 size;
1649                 __le32 value;
1650         } __packed req;
1651
1652         req.offset = cpu_to_le32(offset);
1653         req.size = cpu_to_le32(size);
1654         req.value = cpu_to_le32(value);
1655         return scarlett2_usb(private->mixer, SCARLETT2_USB_SET_DATA,
1656                              &req, sizeof(u32) * 2 + size, NULL, 0);
1657 }
1658
1659 /* Send a SCARLETT2_USB_DATA_CMD command.
1660  * Configuration changes require activation with this after they have
1661  * been uploaded by a previous SCARLETT2_USB_SET_DATA.
1662  * The value for activate needed is determined by the configuration
1663  * item.
1664  */
1665 static int scarlett2_usb_activate_config(
1666         struct usb_mixer_interface *mixer, int activate)
1667 {
1668         __le32 req;
1669
1670         req = cpu_to_le32(activate);
1671         return scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1672                              &req, sizeof(req), NULL, 0);
1673 }
1674
1675 /* Send USB messages to set a SCARLETT2_CONFIG_* parameter */
1676 static int scarlett2_usb_set_config(
1677         struct usb_mixer_interface *mixer,
1678         int config_item_num, int index, int value)
1679 {
1680         struct scarlett2_data *private = mixer->private_data;
1681         const struct scarlett2_config *config_item =
1682                 &private->config_set->items[config_item_num];
1683         int offset, size;
1684         int err;
1685
1686         /* Check that the configuration item is present in the
1687          * configuration set used by this device
1688          */
1689         if (!config_item->offset)
1690                 return -EFAULT;
1691
1692         /* Cancel any pending NVRAM save */
1693         cancel_delayed_work_sync(&private->work);
1694
1695         /* Convert config_item->size in bits to size in bytes and
1696          * calculate offset
1697          */
1698         if (config_item->size >= 8) {
1699                 size = config_item->size / 8;
1700                 offset = config_item->offset + index * size;
1701
1702         /* If updating a bit, retrieve the old value, set/clear the
1703          * bit as needed, and update value
1704          */
1705         } else {
1706                 u8 tmp;
1707
1708                 size = 1;
1709                 offset = config_item->offset;
1710
1711                 err = scarlett2_usb_get(mixer, offset, &tmp, 1);
1712                 if (err < 0)
1713                         return err;
1714
1715                 if (value)
1716                         tmp |= (1 << index);
1717                 else
1718                         tmp &= ~(1 << index);
1719
1720                 value = tmp;
1721         }
1722
1723         /* Send the configuration parameter data */
1724         err = scarlett2_usb_set_data(mixer, offset, size, value);
1725         if (err < 0)
1726                 return err;
1727
1728         /* Activate the change */
1729         err = scarlett2_usb_activate_config(mixer, config_item->activate);
1730         if (err < 0)
1731                 return err;
1732
1733         /* Schedule the change to be written to NVRAM */
1734         if (config_item->activate != SCARLETT2_USB_CONFIG_SAVE)
1735                 schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
1736
1737         return 0;
1738 }
1739
1740 /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
1741 static void scarlett2_config_save(struct usb_mixer_interface *mixer)
1742 {
1743         int err;
1744
1745         err = scarlett2_usb_activate_config(mixer, SCARLETT2_USB_CONFIG_SAVE);
1746         if (err < 0)
1747                 usb_audio_err(mixer->chip, "config save failed: %d\n", err);
1748 }
1749
1750 /* Delayed work to save config */
1751 static void scarlett2_config_save_work(struct work_struct *work)
1752 {
1753         struct scarlett2_data *private =
1754                 container_of(work, struct scarlett2_data, work.work);
1755
1756         scarlett2_config_save(private->mixer);
1757 }
1758
1759 /* Send a USB message to get sync status; result placed in *sync */
1760 static int scarlett2_usb_get_sync_status(
1761         struct usb_mixer_interface *mixer,
1762         u8 *sync)
1763 {
1764         __le32 data;
1765         int err;
1766
1767         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
1768                             NULL, 0, &data, sizeof(data));
1769         if (err < 0)
1770                 return err;
1771
1772         *sync = !!data;
1773         return 0;
1774 }
1775
1776 /* Return true if the device has a mixer that we can control */
1777 static int scarlett2_has_mixer(struct scarlett2_data *private)
1778 {
1779         return !!private->info->mux_assignment[0][0].count;
1780 }
1781
1782 /* Map from mixer value to (db + 80) * 2
1783  * (reverse of scarlett2_mixer_values[])
1784  */
1785 static int scarlett2_mixer_value_to_db(int value)
1786 {
1787         int i;
1788
1789         for (i = 0; i < SCARLETT2_MIXER_VALUE_COUNT; i++)
1790                 if (scarlett2_mixer_values[i] >= value)
1791                         return i;
1792         return SCARLETT2_MIXER_MAX_VALUE;
1793 }
1794
1795 /* Send a USB message to get the volumes for all inputs of one mix
1796  * and put the values into private->mix[]
1797  */
1798 static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
1799                                  int mix_num)
1800 {
1801         struct scarlett2_data *private = mixer->private_data;
1802
1803         int num_mixer_in = private->num_mix_in;
1804         int err, i, j;
1805
1806         struct {
1807                 __le16 mix_num;
1808                 __le16 count;
1809         } __packed req;
1810
1811         __le16 data[SCARLETT2_INPUT_MIX_MAX];
1812
1813         req.mix_num = cpu_to_le16(mix_num);
1814         req.count = cpu_to_le16(num_mixer_in);
1815
1816         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
1817                             &req, sizeof(req),
1818                             data, num_mixer_in * sizeof(u16));
1819         if (err < 0)
1820                 return err;
1821
1822         for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
1823                 private->mix[j] = scarlett2_mixer_value_to_db(
1824                         le16_to_cpu(data[i]));
1825
1826         return 0;
1827 }
1828
1829 /* Send a USB message to set the volumes for all inputs of one mix
1830  * (values obtained from private->mix[])
1831  */
1832 static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
1833                                  int mix_num)
1834 {
1835         struct scarlett2_data *private = mixer->private_data;
1836
1837         struct {
1838                 __le16 mix_num;
1839                 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1840         } __packed req;
1841
1842         int i, j;
1843         int num_mixer_in = private->num_mix_in;
1844
1845         req.mix_num = cpu_to_le16(mix_num);
1846
1847         for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
1848                 req.data[i] = cpu_to_le16(
1849                         scarlett2_mixer_values[private->mix[j]]
1850                 );
1851
1852         return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
1853                              &req, (num_mixer_in + 1) * sizeof(u16),
1854                              NULL, 0);
1855 }
1856
1857 /* Convert a port number index (per info->port_count) to a hardware ID */
1858 static u32 scarlett2_mux_src_num_to_id(
1859         const int port_count[][SCARLETT2_PORT_DIRNS], int num)
1860 {
1861         int port_type;
1862
1863         for (port_type = 0;
1864              port_type < SCARLETT2_PORT_TYPE_COUNT;
1865              port_type++) {
1866                 if (num < port_count[port_type][SCARLETT2_PORT_IN])
1867                         return scarlett2_ports[port_type].id | num;
1868                 num -= port_count[port_type][SCARLETT2_PORT_IN];
1869         }
1870
1871         /* Oops */
1872         return 0;
1873 }
1874
1875 /* Convert a hardware ID to a port number index */
1876 static u32 scarlett2_mux_id_to_num(
1877         const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
1878 {
1879         int port_type;
1880         int port_num = 0;
1881
1882         for (port_type = 0;
1883              port_type < SCARLETT2_PORT_TYPE_COUNT;
1884              port_type++) {
1885                 int base = scarlett2_ports[port_type].id;
1886                 int count = port_count[port_type][direction];
1887
1888                 if (id >= base && id < base + count)
1889                         return port_num + id - base;
1890                 port_num += count;
1891         }
1892
1893         /* Oops */
1894         return -1;
1895 }
1896
1897 /* Convert one mux entry from the interface and load into private->mux[] */
1898 static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
1899                                        u32 mux_entry)
1900 {
1901         const struct scarlett2_device_info *info = private->info;
1902         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1903
1904         int dst_idx, src_idx;
1905
1906         dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
1907                                           mux_entry & 0xFFF);
1908         if (dst_idx < 0)
1909                 return;
1910
1911         if (dst_idx >= private->num_mux_dsts) {
1912                 usb_audio_err(private->mixer->chip,
1913                         "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
1914                         mux_entry, dst_idx, private->num_mux_dsts);
1915                 return;
1916         }
1917
1918         src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
1919                                           mux_entry >> 12);
1920         if (src_idx < 0)
1921                 return;
1922
1923         if (src_idx >= private->num_mux_srcs) {
1924                 usb_audio_err(private->mixer->chip,
1925                         "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
1926                         mux_entry, src_idx, private->num_mux_srcs);
1927                 return;
1928         }
1929
1930         private->mux[dst_idx] = src_idx;
1931 }
1932
1933 /* Update the meter level map
1934  *
1935  * The meter level data from the interface (SCARLETT2_USB_GET_METER
1936  * request) is returned in mux_assignment order, but to avoid exposing
1937  * that to userspace, scarlett2_meter_ctl_get() rearranges the data
1938  * into scarlett2_ports order using the meter_level_map[] array which
1939  * is set up by this function.
1940  *
1941  * In addition, the meter level data values returned from the
1942  * interface are invalid for destinations where:
1943  *
1944  * - the source is "Off"; therefore we set those values to zero (map
1945  *   value of 255)
1946  *
1947  * - the source is assigned to a previous (with respect to the
1948  *   mux_assignment order) destination; therefore we set those values
1949  *   to the value previously reported for that source
1950  */
1951 static void scarlett2_update_meter_level_map(struct scarlett2_data *private)
1952 {
1953         const struct scarlett2_device_info *info = private->info;
1954         const struct scarlett2_meter_entry *entry;
1955
1956         /* sources already assigned to a destination
1957          * value is 255 for None, otherwise the value of i
1958          * (index into array returned by
1959          * scarlett2_usb_get_meter_levels())
1960          */
1961         u8 seen_src[SCARLETT2_MAX_SRCS] = { 1 };
1962         u8 seen_src_value[SCARLETT2_MAX_SRCS] = { 255 };
1963
1964         /* index in meter_map[] order */
1965         int i = 0;
1966
1967         /* go through the meter_map[] entries */
1968         for (entry = info->meter_map;
1969              entry->count;
1970              entry++) {
1971
1972                 /* fill in each meter_level_map[] entry */
1973                 int j, mux_idx;
1974
1975                 for (j = 0, mux_idx = entry->start;
1976                      j < entry->count;
1977                      i++, j++, mux_idx++) {
1978
1979                         /* convert mux_idx using line_out_unmap[] */
1980                         int map_mux_idx = (
1981                             info->line_out_remap_enable &&
1982                             mux_idx < private->num_line_out
1983                         ) ? info->line_out_unmap[mux_idx]
1984                           : mux_idx;
1985
1986                         /* check which source is connected, and if
1987                          * that source is already connected elsewhere,
1988                          * use that existing connection's destination
1989                          * for this meter entry instead
1990                          */
1991                         int mux_src = private->mux[mux_idx];
1992
1993                         if (!seen_src[mux_src]) {
1994                                 seen_src[mux_src] = 1;
1995                                 seen_src_value[mux_src] = i;
1996                         }
1997                         private->meter_level_map[map_mux_idx] =
1998                                 seen_src_value[mux_src];
1999                 }
2000         }
2001 }
2002
2003 /* Send USB message to get mux inputs and then populate private->mux[] */
2004 static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
2005 {
2006         struct scarlett2_data *private = mixer->private_data;
2007         int count = private->num_mux_dsts;
2008         int err, i;
2009
2010         struct {
2011                 __le16 num;
2012                 __le16 count;
2013         } __packed req;
2014
2015         __le32 data[SCARLETT2_MUX_MAX];
2016
2017         private->mux_updated = 0;
2018
2019         req.num = 0;
2020         req.count = cpu_to_le16(count);
2021
2022         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
2023                             &req, sizeof(req),
2024                             data, count * sizeof(u32));
2025         if (err < 0)
2026                 return err;
2027
2028         for (i = 0; i < count; i++)
2029                 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
2030
2031         scarlett2_update_meter_level_map(private);
2032
2033         return 0;
2034 }
2035
2036 /* Send USB messages to set mux inputs */
2037 static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
2038 {
2039         struct scarlett2_data *private = mixer->private_data;
2040         const struct scarlett2_device_info *info = private->info;
2041         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2042         int table;
2043
2044         struct {
2045                 __le16 pad;
2046                 __le16 num;
2047                 __le32 data[SCARLETT2_MUX_MAX];
2048         } __packed req;
2049
2050         req.pad = 0;
2051
2052         /* set mux settings for each rate */
2053         for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
2054                 const struct scarlett2_mux_entry *entry;
2055
2056                 /* i counts over the output array */
2057                 int i = 0, err;
2058
2059                 req.num = cpu_to_le16(table);
2060
2061                 /* loop through each entry */
2062                 for (entry = info->mux_assignment[table];
2063                      entry->count;
2064                      entry++) {
2065                         int j;
2066                         int port_type = entry->port_type;
2067                         int port_idx = entry->start;
2068                         int mux_idx = scarlett2_get_port_start_num(port_count,
2069                                 SCARLETT2_PORT_OUT, port_type) + port_idx;
2070                         int dst_id = scarlett2_ports[port_type].id + port_idx;
2071
2072                         /* Empty slots */
2073                         if (!dst_id) {
2074                                 for (j = 0; j < entry->count; j++)
2075                                         req.data[i++] = 0;
2076                                 continue;
2077                         }
2078
2079                         /* Non-empty mux slots use the lower 12 bits
2080                          * for the destination and next 12 bits for
2081                          * the source
2082                          */
2083                         for (j = 0; j < entry->count; j++) {
2084                                 int src_id = scarlett2_mux_src_num_to_id(
2085                                         port_count, private->mux[mux_idx++]);
2086                                 req.data[i++] = cpu_to_le32(dst_id |
2087                                                             src_id << 12);
2088                                 dst_id++;
2089                         }
2090                 }
2091
2092                 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
2093                                     &req, (i + 1) * sizeof(u32),
2094                                     NULL, 0);
2095                 if (err < 0)
2096                         return err;
2097         }
2098
2099         scarlett2_update_meter_level_map(private);
2100
2101         return 0;
2102 }
2103
2104 /* Send USB message to get meter levels */
2105 static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
2106                                           u16 num_meters, u16 *levels)
2107 {
2108         struct {
2109                 __le16 pad;
2110                 __le16 num_meters;
2111                 __le32 magic;
2112         } __packed req;
2113         __le32 resp[SCARLETT2_MAX_METERS];
2114         int i, err;
2115
2116         req.pad = 0;
2117         req.num_meters = cpu_to_le16(num_meters);
2118         req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
2119         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
2120                             &req, sizeof(req), resp, num_meters * sizeof(u32));
2121         if (err < 0)
2122                 return err;
2123
2124         /* copy, convert to u16 */
2125         for (i = 0; i < num_meters; i++)
2126                 levels[i] = le32_to_cpu(resp[i]);
2127
2128         return 0;
2129 }
2130
2131 /*** Control Functions ***/
2132
2133 /* helper function to create a new control */
2134 static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
2135                                  const struct snd_kcontrol_new *ncontrol,
2136                                  int index, int channels, const char *name,
2137                                  struct snd_kcontrol **kctl_return)
2138 {
2139         struct snd_kcontrol *kctl;
2140         struct usb_mixer_elem_info *elem;
2141         int err;
2142
2143         elem = kzalloc(sizeof(*elem), GFP_KERNEL);
2144         if (!elem)
2145                 return -ENOMEM;
2146
2147         /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
2148          * ignores them for resume and other operations.
2149          * Also, the head.id field is set to 0, as we don't use this field.
2150          */
2151         elem->head.mixer = mixer;
2152         elem->control = index;
2153         elem->head.id = 0;
2154         elem->channels = channels;
2155         elem->val_type = USB_MIXER_BESPOKEN;
2156
2157         kctl = snd_ctl_new1(ncontrol, elem);
2158         if (!kctl) {
2159                 kfree(elem);
2160                 return -ENOMEM;
2161         }
2162         kctl->private_free = snd_usb_mixer_elem_free;
2163
2164         strscpy(kctl->id.name, name, sizeof(kctl->id.name));
2165
2166         err = snd_usb_mixer_add_control(&elem->head, kctl);
2167         if (err < 0)
2168                 return err;
2169
2170         if (kctl_return)
2171                 *kctl_return = kctl;
2172
2173         return 0;
2174 }
2175
2176 /*** Firmware Version Control ***/
2177
2178 static int scarlett2_firmware_version_ctl_get(
2179         struct snd_kcontrol *kctl,
2180         struct snd_ctl_elem_value *ucontrol)
2181 {
2182         struct usb_mixer_elem_info *elem = kctl->private_data;
2183         struct scarlett2_data *private = elem->head.mixer->private_data;
2184
2185         ucontrol->value.integer.value[0] = private->firmware_version;
2186
2187         return 0;
2188 }
2189
2190 static int scarlett2_firmware_version_ctl_info(
2191         struct snd_kcontrol *kctl,
2192         struct snd_ctl_elem_info *uinfo)
2193 {
2194         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2195         uinfo->count = 1;
2196
2197         return 0;
2198 }
2199
2200 static const struct snd_kcontrol_new scarlett2_firmware_version_ctl = {
2201         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
2202         .access = SNDRV_CTL_ELEM_ACCESS_READ,
2203         .name = "",
2204         .info = scarlett2_firmware_version_ctl_info,
2205         .get  = scarlett2_firmware_version_ctl_get
2206 };
2207
2208 static int scarlett2_add_firmware_version_ctl(
2209         struct usb_mixer_interface *mixer)
2210 {
2211         return scarlett2_add_new_ctl(mixer, &scarlett2_firmware_version_ctl,
2212                                      0, 0, "Firmware Version", NULL);
2213 }
2214
2215 /*** Sync Control ***/
2216
2217 /* Update sync control after receiving notification that the status
2218  * has changed
2219  */
2220 static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
2221 {
2222         struct scarlett2_data *private = mixer->private_data;
2223
2224         private->sync_updated = 0;
2225         return scarlett2_usb_get_sync_status(mixer, &private->sync);
2226 }
2227
2228 static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
2229                                    struct snd_ctl_elem_info *uinfo)
2230 {
2231         static const char *texts[2] = {
2232                 "Unlocked", "Locked"
2233         };
2234         return snd_ctl_enum_info(uinfo, 1, 2, texts);
2235 }
2236
2237 static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
2238                                   struct snd_ctl_elem_value *ucontrol)
2239 {
2240         struct usb_mixer_elem_info *elem = kctl->private_data;
2241         struct usb_mixer_interface *mixer = elem->head.mixer;
2242         struct scarlett2_data *private = mixer->private_data;
2243         int err = 0;
2244
2245         mutex_lock(&private->data_mutex);
2246
2247         if (private->hwdep_in_use) {
2248                 err = -EBUSY;
2249                 goto unlock;
2250         }
2251
2252         if (private->sync_updated) {
2253                 err = scarlett2_update_sync(mixer);
2254                 if (err < 0)
2255                         goto unlock;
2256         }
2257         ucontrol->value.enumerated.item[0] = private->sync;
2258
2259 unlock:
2260         mutex_unlock(&private->data_mutex);
2261         return err;
2262 }
2263
2264 static const struct snd_kcontrol_new scarlett2_sync_ctl = {
2265         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2266         .access = SNDRV_CTL_ELEM_ACCESS_READ,
2267         .name = "",
2268         .info = scarlett2_sync_ctl_info,
2269         .get  = scarlett2_sync_ctl_get
2270 };
2271
2272 static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
2273 {
2274         struct scarlett2_data *private = mixer->private_data;
2275
2276         /* devices without a mixer also don't support reporting sync status */
2277         if (!scarlett2_has_mixer(private))
2278                 return 0;
2279
2280         return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
2281                                      0, 1, "Sync Status", &private->sync_ctl);
2282 }
2283
2284 /*** Analogue Line Out Volume Controls ***/
2285
2286 /* Update hardware volume controls after receiving notification that
2287  * they have changed
2288  */
2289 static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
2290 {
2291         struct scarlett2_data *private = mixer->private_data;
2292         s16 vol;
2293         int err, i;
2294
2295         private->vol_updated = 0;
2296
2297         if (scarlett2_has_config_item(private,
2298                                       SCARLETT2_CONFIG_MASTER_VOLUME)) {
2299                 err = scarlett2_usb_get_config(
2300                         mixer, SCARLETT2_CONFIG_MASTER_VOLUME,
2301                         1, &vol);
2302                 if (err < 0)
2303                         return err;
2304
2305                 private->master_vol = clamp(vol + SCARLETT2_VOLUME_BIAS,
2306                                             0, SCARLETT2_VOLUME_BIAS);
2307
2308                 if (scarlett2_has_config_item(private,
2309                                               SCARLETT2_CONFIG_SW_HW_SWITCH))
2310                         for (i = 0; i < private->num_line_out; i++)
2311                                 if (private->vol_sw_hw_switch[i])
2312                                         private->vol[i] = private->master_vol;
2313         }
2314
2315         return 0;
2316 }
2317
2318 static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
2319                                      struct snd_ctl_elem_info *uinfo)
2320 {
2321         struct usb_mixer_elem_info *elem = kctl->private_data;
2322
2323         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2324         uinfo->count = elem->channels;
2325         uinfo->value.integer.min = 0;
2326         uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
2327         uinfo->value.integer.step = 1;
2328         return 0;
2329 }
2330
2331 static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
2332                                            struct snd_ctl_elem_value *ucontrol)
2333 {
2334         struct usb_mixer_elem_info *elem = kctl->private_data;
2335         struct usb_mixer_interface *mixer = elem->head.mixer;
2336         struct scarlett2_data *private = mixer->private_data;
2337         int err = 0;
2338
2339         mutex_lock(&private->data_mutex);
2340
2341         if (private->hwdep_in_use) {
2342                 err = -EBUSY;
2343                 goto unlock;
2344         }
2345
2346         if (private->vol_updated) {
2347                 err = scarlett2_update_volumes(mixer);
2348                 if (err < 0)
2349                         goto unlock;
2350         }
2351         ucontrol->value.integer.value[0] = private->master_vol;
2352
2353 unlock:
2354         mutex_unlock(&private->data_mutex);
2355         return err;
2356 }
2357
2358 static int line_out_remap(struct scarlett2_data *private, int index)
2359 {
2360         const struct scarlett2_device_info *info = private->info;
2361
2362         if (!info->line_out_remap_enable)
2363                 return index;
2364
2365         if (index >= private->num_line_out)
2366                 return index;
2367
2368         return info->line_out_remap[index];
2369 }
2370
2371 static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
2372                                     struct snd_ctl_elem_value *ucontrol)
2373 {
2374         struct usb_mixer_elem_info *elem = kctl->private_data;
2375         struct usb_mixer_interface *mixer = elem->head.mixer;
2376         struct scarlett2_data *private = mixer->private_data;
2377         int index = line_out_remap(private, elem->control);
2378         int err = 0;
2379
2380         mutex_lock(&private->data_mutex);
2381
2382         if (private->hwdep_in_use) {
2383                 err = -EBUSY;
2384                 goto unlock;
2385         }
2386
2387         if (private->vol_updated) {
2388                 err = scarlett2_update_volumes(mixer);
2389                 if (err < 0)
2390                         goto unlock;
2391         }
2392         ucontrol->value.integer.value[0] = private->vol[index];
2393
2394 unlock:
2395         mutex_unlock(&private->data_mutex);
2396         return err;
2397 }
2398
2399 static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
2400                                     struct snd_ctl_elem_value *ucontrol)
2401 {
2402         struct usb_mixer_elem_info *elem = kctl->private_data;
2403         struct usb_mixer_interface *mixer = elem->head.mixer;
2404         struct scarlett2_data *private = mixer->private_data;
2405         int index = line_out_remap(private, elem->control);
2406         int oval, val, err = 0;
2407
2408         mutex_lock(&private->data_mutex);
2409
2410         if (private->hwdep_in_use) {
2411                 err = -EBUSY;
2412                 goto unlock;
2413         }
2414
2415         oval = private->vol[index];
2416         val = ucontrol->value.integer.value[0];
2417
2418         if (oval == val)
2419                 goto unlock;
2420
2421         private->vol[index] = val;
2422         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
2423                                        index, val - SCARLETT2_VOLUME_BIAS);
2424         if (err == 0)
2425                 err = 1;
2426
2427 unlock:
2428         mutex_unlock(&private->data_mutex);
2429         return err;
2430 }
2431
2432 static const DECLARE_TLV_DB_MINMAX(
2433         db_scale_scarlett2_volume, -SCARLETT2_VOLUME_BIAS * 100, 0
2434 );
2435
2436 static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
2437         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2438         .access = SNDRV_CTL_ELEM_ACCESS_READ |
2439                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
2440         .name = "",
2441         .info = scarlett2_volume_ctl_info,
2442         .get  = scarlett2_master_volume_ctl_get,
2443         .private_value = 0, /* max value */
2444         .tlv = { .p = db_scale_scarlett2_volume }
2445 };
2446
2447 static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
2448         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2449         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
2450                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
2451         .name = "",
2452         .info = scarlett2_volume_ctl_info,
2453         .get  = scarlett2_volume_ctl_get,
2454         .put  = scarlett2_volume_ctl_put,
2455         .private_value = 0, /* max value */
2456         .tlv = { .p = db_scale_scarlett2_volume }
2457 };
2458
2459 /*** Mute Switch Controls ***/
2460
2461 static int scarlett2_update_dim_mute(struct usb_mixer_interface *mixer)
2462 {
2463         struct scarlett2_data *private = mixer->private_data;
2464         int err, i;
2465         u8 mute;
2466
2467         private->dim_mute_updated = 0;
2468
2469         if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
2470                 return 0;
2471
2472         err = scarlett2_usb_get_config(
2473                 mixer, SCARLETT2_CONFIG_DIM_MUTE,
2474                 SCARLETT2_DIM_MUTE_COUNT, private->dim_mute);
2475         if (err < 0)
2476                 return err;
2477
2478         for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
2479                 private->dim_mute[i] = !!private->dim_mute[i];
2480
2481         mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];
2482
2483         for (i = 0; i < private->num_line_out; i++)
2484                 if (private->vol_sw_hw_switch[i])
2485                         private->mute_switch[i] = mute;
2486
2487         return 0;
2488 }
2489
2490 static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
2491                                         struct snd_ctl_elem_value *ucontrol)
2492 {
2493         struct usb_mixer_elem_info *elem = kctl->private_data;
2494         struct usb_mixer_interface *mixer = elem->head.mixer;
2495         struct scarlett2_data *private = mixer->private_data;
2496         int index = line_out_remap(private, elem->control);
2497         int err = 0;
2498
2499         mutex_lock(&private->data_mutex);
2500
2501         if (private->hwdep_in_use) {
2502                 err = -EBUSY;
2503                 goto unlock;
2504         }
2505
2506         if (private->dim_mute_updated) {
2507                 err = scarlett2_update_dim_mute(mixer);
2508                 if (err < 0)
2509                         goto unlock;
2510         }
2511         ucontrol->value.integer.value[0] = private->mute_switch[index];
2512
2513 unlock:
2514         mutex_unlock(&private->data_mutex);
2515         return err;
2516 }
2517
2518 static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
2519                                         struct snd_ctl_elem_value *ucontrol)
2520 {
2521         struct usb_mixer_elem_info *elem = kctl->private_data;
2522         struct usb_mixer_interface *mixer = elem->head.mixer;
2523         struct scarlett2_data *private = mixer->private_data;
2524         int index = line_out_remap(private, elem->control);
2525         int oval, val, err = 0;
2526
2527         mutex_lock(&private->data_mutex);
2528
2529         if (private->hwdep_in_use) {
2530                 err = -EBUSY;
2531                 goto unlock;
2532         }
2533
2534         oval = private->mute_switch[index];
2535         val = !!ucontrol->value.integer.value[0];
2536
2537         if (oval == val)
2538                 goto unlock;
2539
2540         private->mute_switch[index] = val;
2541
2542         /* Send mute change to the device */
2543         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
2544                                        index, val);
2545         if (err == 0)
2546                 err = 1;
2547
2548 unlock:
2549         mutex_unlock(&private->data_mutex);
2550         return err;
2551 }
2552
2553 static const struct snd_kcontrol_new scarlett2_mute_ctl = {
2554         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2555         .name = "",
2556         .info = snd_ctl_boolean_mono_info,
2557         .get  = scarlett2_mute_ctl_get,
2558         .put  = scarlett2_mute_ctl_put,
2559 };
2560
2561 /*** HW/SW Volume Switch Controls ***/
2562
2563 static void scarlett2_sw_hw_ctl_ro(struct scarlett2_data *private, int index)
2564 {
2565         private->sw_hw_ctls[index]->vd[0].access &=
2566                 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2567 }
2568
2569 static void scarlett2_sw_hw_ctl_rw(struct scarlett2_data *private, int index)
2570 {
2571         private->sw_hw_ctls[index]->vd[0].access |=
2572                 SNDRV_CTL_ELEM_ACCESS_WRITE;
2573 }
2574
2575 static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
2576                                          struct snd_ctl_elem_info *uinfo)
2577 {
2578         static const char *const values[2] = {
2579                 "SW", "HW"
2580         };
2581
2582         return snd_ctl_enum_info(uinfo, 1, 2, values);
2583 }
2584
2585 static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
2586                                         struct snd_ctl_elem_value *ucontrol)
2587 {
2588         struct usb_mixer_elem_info *elem = kctl->private_data;
2589         struct scarlett2_data *private = elem->head.mixer->private_data;
2590         int index = line_out_remap(private, elem->control);
2591
2592         ucontrol->value.enumerated.item[0] = private->vol_sw_hw_switch[index];
2593         return 0;
2594 }
2595
2596 static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
2597                                            int index, int value)
2598 {
2599         struct scarlett2_data *private = mixer->private_data;
2600         struct snd_card *card = mixer->chip->card;
2601
2602         /* Set/Clear write bits */
2603         if (value) {
2604                 private->vol_ctls[index]->vd[0].access |=
2605                         SNDRV_CTL_ELEM_ACCESS_WRITE;
2606                 private->mute_ctls[index]->vd[0].access |=
2607                         SNDRV_CTL_ELEM_ACCESS_WRITE;
2608         } else {
2609                 private->vol_ctls[index]->vd[0].access &=
2610                         ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2611                 private->mute_ctls[index]->vd[0].access &=
2612                         ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2613         }
2614
2615         /* Notify of write bit and possible value change */
2616         snd_ctl_notify(card,
2617                        SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
2618                        &private->vol_ctls[index]->id);
2619         snd_ctl_notify(card,
2620                        SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
2621                        &private->mute_ctls[index]->id);
2622 }
2623
2624 static int scarlett2_sw_hw_change(struct usb_mixer_interface *mixer,
2625                                   int ctl_index, int val)
2626 {
2627         struct scarlett2_data *private = mixer->private_data;
2628         int index = line_out_remap(private, ctl_index);
2629         int err;
2630
2631         private->vol_sw_hw_switch[index] = val;
2632
2633         /* Change access mode to RO (hardware controlled volume)
2634          * or RW (software controlled volume)
2635          */
2636         scarlett2_vol_ctl_set_writable(mixer, ctl_index, !val);
2637
2638         /* Reset volume/mute to master volume/mute */
2639         private->vol[index] = private->master_vol;
2640         private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE];
2641
2642         /* Set SW volume to current HW volume */
2643         err = scarlett2_usb_set_config(
2644                 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
2645                 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
2646         if (err < 0)
2647                 return err;
2648
2649         /* Set SW mute to current HW mute */
2650         err = scarlett2_usb_set_config(
2651                 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
2652                 index, private->dim_mute[SCARLETT2_BUTTON_MUTE]);
2653         if (err < 0)
2654                 return err;
2655
2656         /* Send SW/HW switch change to the device */
2657         return scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
2658                                         index, val);
2659 }
2660
2661 static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
2662                                         struct snd_ctl_elem_value *ucontrol)
2663 {
2664         struct usb_mixer_elem_info *elem = kctl->private_data;
2665         struct usb_mixer_interface *mixer = elem->head.mixer;
2666         struct scarlett2_data *private = mixer->private_data;
2667         int ctl_index = elem->control;
2668         int index = line_out_remap(private, ctl_index);
2669         int oval, val, err = 0;
2670
2671         mutex_lock(&private->data_mutex);
2672
2673         if (private->hwdep_in_use) {
2674                 err = -EBUSY;
2675                 goto unlock;
2676         }
2677
2678         oval = private->vol_sw_hw_switch[index];
2679         val = !!ucontrol->value.enumerated.item[0];
2680
2681         if (oval == val)
2682                 goto unlock;
2683
2684         err = scarlett2_sw_hw_change(mixer, ctl_index, val);
2685         if (err == 0)
2686                 err = 1;
2687
2688 unlock:
2689         mutex_unlock(&private->data_mutex);
2690         return err;
2691 }
2692
2693 static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
2694         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2695         .name = "",
2696         .info = scarlett2_sw_hw_enum_ctl_info,
2697         .get  = scarlett2_sw_hw_enum_ctl_get,
2698         .put  = scarlett2_sw_hw_enum_ctl_put,
2699 };
2700
2701 /*** Line Level/Instrument Level Switch Controls ***/
2702
2703 static int scarlett2_update_input_level(struct usb_mixer_interface *mixer)
2704 {
2705         struct scarlett2_data *private = mixer->private_data;
2706         const struct scarlett2_device_info *info = private->info;
2707
2708         private->input_level_updated = 0;
2709
2710         if (!info->level_input_count)
2711                 return 0;
2712
2713         return scarlett2_usb_get_config(
2714                 mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2715                 info->level_input_count + info->level_input_first,
2716                 private->level_switch);
2717 }
2718
2719 static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
2720                                          struct snd_ctl_elem_info *uinfo)
2721 {
2722         static const char *const values[2] = {
2723                 "Line", "Inst"
2724         };
2725
2726         return snd_ctl_enum_info(uinfo, 1, 2, values);
2727 }
2728
2729 static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
2730                                         struct snd_ctl_elem_value *ucontrol)
2731 {
2732         struct usb_mixer_elem_info *elem = kctl->private_data;
2733         struct usb_mixer_interface *mixer = elem->head.mixer;
2734         struct scarlett2_data *private = mixer->private_data;
2735         const struct scarlett2_device_info *info = private->info;
2736
2737         int index = elem->control + info->level_input_first;
2738         int err = 0;
2739
2740         mutex_lock(&private->data_mutex);
2741
2742         if (private->hwdep_in_use) {
2743                 err = -EBUSY;
2744                 goto unlock;
2745         }
2746
2747         if (private->input_level_updated) {
2748                 err = scarlett2_update_input_level(mixer);
2749                 if (err < 0)
2750                         goto unlock;
2751         }
2752         ucontrol->value.enumerated.item[0] = private->level_switch[index];
2753
2754 unlock:
2755         mutex_unlock(&private->data_mutex);
2756         return err;
2757 }
2758
2759 static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
2760                                         struct snd_ctl_elem_value *ucontrol)
2761 {
2762         struct usb_mixer_elem_info *elem = kctl->private_data;
2763         struct usb_mixer_interface *mixer = elem->head.mixer;
2764         struct scarlett2_data *private = mixer->private_data;
2765         const struct scarlett2_device_info *info = private->info;
2766
2767         int index = elem->control + info->level_input_first;
2768         int oval, val, err = 0;
2769
2770         mutex_lock(&private->data_mutex);
2771
2772         if (private->hwdep_in_use) {
2773                 err = -EBUSY;
2774                 goto unlock;
2775         }
2776
2777         oval = private->level_switch[index];
2778         val = !!ucontrol->value.enumerated.item[0];
2779
2780         if (oval == val)
2781                 goto unlock;
2782
2783         private->level_switch[index] = val;
2784
2785         /* Send switch change to the device */
2786         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2787                                        index, val);
2788         if (err == 0)
2789                 err = 1;
2790
2791 unlock:
2792         mutex_unlock(&private->data_mutex);
2793         return err;
2794 }
2795
2796 static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
2797         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2798         .name = "",
2799         .info = scarlett2_level_enum_ctl_info,
2800         .get  = scarlett2_level_enum_ctl_get,
2801         .put  = scarlett2_level_enum_ctl_put,
2802 };
2803
2804 /*** Pad Switch Controls ***/
2805
2806 static int scarlett2_update_input_pad(struct usb_mixer_interface *mixer)
2807 {
2808         struct scarlett2_data *private = mixer->private_data;
2809         const struct scarlett2_device_info *info = private->info;
2810
2811         private->input_pad_updated = 0;
2812
2813         if (!info->pad_input_count)
2814                 return 0;
2815
2816         return scarlett2_usb_get_config(
2817                 mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2818                 info->pad_input_count, private->pad_switch);
2819 }
2820
2821 static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
2822                                  struct snd_ctl_elem_value *ucontrol)
2823 {
2824         struct usb_mixer_elem_info *elem = kctl->private_data;
2825         struct usb_mixer_interface *mixer = elem->head.mixer;
2826         struct scarlett2_data *private = mixer->private_data;
2827         int err = 0;
2828
2829         mutex_lock(&private->data_mutex);
2830
2831         if (private->hwdep_in_use) {
2832                 err = -EBUSY;
2833                 goto unlock;
2834         }
2835
2836         if (private->input_pad_updated) {
2837                 err = scarlett2_update_input_pad(mixer);
2838                 if (err < 0)
2839                         goto unlock;
2840         }
2841         ucontrol->value.integer.value[0] =
2842                 private->pad_switch[elem->control];
2843
2844 unlock:
2845         mutex_unlock(&private->data_mutex);
2846         return err;
2847 }
2848
2849 static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
2850                                  struct snd_ctl_elem_value *ucontrol)
2851 {
2852         struct usb_mixer_elem_info *elem = kctl->private_data;
2853         struct usb_mixer_interface *mixer = elem->head.mixer;
2854         struct scarlett2_data *private = mixer->private_data;
2855
2856         int index = elem->control;
2857         int oval, val, err = 0;
2858
2859         mutex_lock(&private->data_mutex);
2860
2861         if (private->hwdep_in_use) {
2862                 err = -EBUSY;
2863                 goto unlock;
2864         }
2865
2866         oval = private->pad_switch[index];
2867         val = !!ucontrol->value.integer.value[0];
2868
2869         if (oval == val)
2870                 goto unlock;
2871
2872         private->pad_switch[index] = val;
2873
2874         /* Send switch change to the device */
2875         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2876                                        index, val);
2877         if (err == 0)
2878                 err = 1;
2879
2880 unlock:
2881         mutex_unlock(&private->data_mutex);
2882         return err;
2883 }
2884
2885 static const struct snd_kcontrol_new scarlett2_pad_ctl = {
2886         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2887         .name = "",
2888         .info = snd_ctl_boolean_mono_info,
2889         .get  = scarlett2_pad_ctl_get,
2890         .put  = scarlett2_pad_ctl_put,
2891 };
2892
2893 /*** Air Switch Controls ***/
2894
2895 static int scarlett2_update_input_air(struct usb_mixer_interface *mixer)
2896 {
2897         struct scarlett2_data *private = mixer->private_data;
2898         const struct scarlett2_device_info *info = private->info;
2899
2900         private->input_air_updated = 0;
2901
2902         if (!info->air_input_count)
2903                 return 0;
2904
2905         return scarlett2_usb_get_config(
2906                 mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2907                 info->air_input_count, private->air_switch);
2908 }
2909
2910 static int scarlett2_air_ctl_get(struct snd_kcontrol *kctl,
2911                                  struct snd_ctl_elem_value *ucontrol)
2912 {
2913         struct usb_mixer_elem_info *elem = kctl->private_data;
2914         struct usb_mixer_interface *mixer = elem->head.mixer;
2915         struct scarlett2_data *private = mixer->private_data;
2916         int err = 0;
2917
2918         mutex_lock(&private->data_mutex);
2919
2920         if (private->hwdep_in_use) {
2921                 err = -EBUSY;
2922                 goto unlock;
2923         }
2924
2925         if (private->input_air_updated) {
2926                 err = scarlett2_update_input_air(mixer);
2927                 if (err < 0)
2928                         goto unlock;
2929         }
2930         ucontrol->value.integer.value[0] = private->air_switch[elem->control];
2931
2932 unlock:
2933         mutex_unlock(&private->data_mutex);
2934         return err;
2935 }
2936
2937 static int scarlett2_air_ctl_put(struct snd_kcontrol *kctl,
2938                                  struct snd_ctl_elem_value *ucontrol)
2939 {
2940         struct usb_mixer_elem_info *elem = kctl->private_data;
2941         struct usb_mixer_interface *mixer = elem->head.mixer;
2942         struct scarlett2_data *private = mixer->private_data;
2943
2944         int index = elem->control;
2945         int oval, val, err = 0;
2946
2947         mutex_lock(&private->data_mutex);
2948
2949         if (private->hwdep_in_use) {
2950                 err = -EBUSY;
2951                 goto unlock;
2952         }
2953
2954         oval = private->air_switch[index];
2955         val = !!ucontrol->value.integer.value[0];
2956
2957         if (oval == val)
2958                 goto unlock;
2959
2960         private->air_switch[index] = val;
2961
2962         /* Send switch change to the device */
2963         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2964                                        index, val);
2965         if (err == 0)
2966                 err = 1;
2967
2968 unlock:
2969         mutex_unlock(&private->data_mutex);
2970         return err;
2971 }
2972
2973 static const struct snd_kcontrol_new scarlett2_air_ctl = {
2974         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2975         .name = "",
2976         .info = snd_ctl_boolean_mono_info,
2977         .get  = scarlett2_air_ctl_get,
2978         .put  = scarlett2_air_ctl_put,
2979 };
2980
2981 /*** Phantom Switch Controls ***/
2982
2983 static int scarlett2_update_input_phantom(struct usb_mixer_interface *mixer)
2984 {
2985         struct scarlett2_data *private = mixer->private_data;
2986         const struct scarlett2_device_info *info = private->info;
2987         int err;
2988
2989         private->input_phantom_updated = 0;
2990
2991         if (!info->phantom_count)
2992                 return 0;
2993
2994         err = scarlett2_usb_get_config(
2995                 mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
2996                 info->phantom_count, private->phantom_switch);
2997         if (err < 0)
2998                 return err;
2999
3000         if (scarlett2_has_config_item(private,
3001                                       SCARLETT2_CONFIG_PHANTOM_PERSISTENCE)) {
3002                 err = scarlett2_usb_get_config(
3003                         mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
3004                         1, &private->phantom_persistence);
3005                 if (err < 0)
3006                         return err;
3007         }
3008
3009         return 0;
3010 }
3011
3012 static int scarlett2_phantom_ctl_get(struct snd_kcontrol *kctl,
3013                                      struct snd_ctl_elem_value *ucontrol)
3014 {
3015         struct usb_mixer_elem_info *elem = kctl->private_data;
3016         struct usb_mixer_interface *mixer = elem->head.mixer;
3017         struct scarlett2_data *private = mixer->private_data;
3018         int err = 0;
3019
3020         mutex_lock(&private->data_mutex);
3021
3022         if (private->hwdep_in_use) {
3023                 err = -EBUSY;
3024                 goto unlock;
3025         }
3026
3027         if (private->input_phantom_updated) {
3028                 err = scarlett2_update_input_phantom(mixer);
3029                 if (err < 0)
3030                         goto unlock;
3031         }
3032         ucontrol->value.integer.value[0] =
3033                 private->phantom_switch[elem->control];
3034
3035 unlock:
3036         mutex_unlock(&private->data_mutex);
3037         return err;
3038 }
3039
3040 static int scarlett2_phantom_ctl_put(struct snd_kcontrol *kctl,
3041                                      struct snd_ctl_elem_value *ucontrol)
3042 {
3043         struct usb_mixer_elem_info *elem = kctl->private_data;
3044         struct usb_mixer_interface *mixer = elem->head.mixer;
3045         struct scarlett2_data *private = mixer->private_data;
3046
3047         int index = elem->control;
3048         int oval, val, err = 0;
3049
3050         mutex_lock(&private->data_mutex);
3051
3052         if (private->hwdep_in_use) {
3053                 err = -EBUSY;
3054                 goto unlock;
3055         }
3056
3057         oval = private->phantom_switch[index];
3058         val = !!ucontrol->value.integer.value[0];
3059
3060         if (oval == val)
3061                 goto unlock;
3062
3063         private->phantom_switch[index] = val;
3064
3065         /* Send switch change to the device */
3066         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
3067                                        index, val);
3068         if (err == 0)
3069                 err = 1;
3070
3071 unlock:
3072         mutex_unlock(&private->data_mutex);
3073         return err;
3074 }
3075
3076 static const struct snd_kcontrol_new scarlett2_phantom_ctl = {
3077         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3078         .name = "",
3079         .info = snd_ctl_boolean_mono_info,
3080         .get  = scarlett2_phantom_ctl_get,
3081         .put  = scarlett2_phantom_ctl_put,
3082 };
3083
3084 /*** Phantom Persistence Control ***/
3085
3086 static int scarlett2_phantom_persistence_ctl_get(
3087         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3088 {
3089         struct usb_mixer_elem_info *elem = kctl->private_data;
3090         struct scarlett2_data *private = elem->head.mixer->private_data;
3091
3092         ucontrol->value.integer.value[0] = private->phantom_persistence;
3093         return 0;
3094 }
3095
3096 static int scarlett2_phantom_persistence_ctl_put(
3097         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3098 {
3099         struct usb_mixer_elem_info *elem = kctl->private_data;
3100         struct usb_mixer_interface *mixer = elem->head.mixer;
3101         struct scarlett2_data *private = mixer->private_data;
3102
3103         int index = elem->control;
3104         int oval, val, err = 0;
3105
3106         mutex_lock(&private->data_mutex);
3107
3108         if (private->hwdep_in_use) {
3109                 err = -EBUSY;
3110                 goto unlock;
3111         }
3112
3113         oval = private->phantom_persistence;
3114         val = !!ucontrol->value.integer.value[0];
3115
3116         if (oval == val)
3117                 goto unlock;
3118
3119         private->phantom_persistence = val;
3120
3121         /* Send switch change to the device */
3122         err = scarlett2_usb_set_config(
3123                 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, index, val);
3124         if (err == 0)
3125                 err = 1;
3126
3127 unlock:
3128         mutex_unlock(&private->data_mutex);
3129         return err;
3130 }
3131
3132 static const struct snd_kcontrol_new scarlett2_phantom_persistence_ctl = {
3133         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3134         .name = "",
3135         .info = snd_ctl_boolean_mono_info,
3136         .get  = scarlett2_phantom_persistence_ctl_get,
3137         .put  = scarlett2_phantom_persistence_ctl_put,
3138 };
3139
3140 /*** Speaker Switching Control ***/
3141
3142 static int scarlett2_update_monitor_other(struct usb_mixer_interface *mixer)
3143 {
3144         struct scarlett2_data *private = mixer->private_data;
3145         const struct scarlett2_device_info *info = private->info;
3146         int err;
3147
3148         /* monitor_other_enable[0] enables speaker switching
3149          * monitor_other_enable[1] enables talkback
3150          */
3151         u8 monitor_other_enable[2];
3152
3153         /* monitor_other_switch[0] activates the alternate speakers
3154          * monitor_other_switch[1] activates talkback
3155          */
3156         u8 monitor_other_switch[2];
3157
3158         private->monitor_other_updated = 0;
3159
3160         /* if it doesn't do speaker switching then it also doesn't do
3161          * talkback
3162          */
3163         if (!info->has_speaker_switching)
3164                 return 0;
3165
3166         err = scarlett2_usb_get_config(
3167                 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
3168                 2, monitor_other_enable);
3169         if (err < 0)
3170                 return err;
3171
3172         err = scarlett2_usb_get_config(
3173                 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
3174                 2, monitor_other_switch);
3175         if (err < 0)
3176                 return err;
3177
3178         if (!monitor_other_enable[0])
3179                 private->speaker_switching_switch = 0;
3180         else
3181                 private->speaker_switching_switch = monitor_other_switch[0] + 1;
3182
3183         if (info->has_talkback) {
3184                 u16 bitmap;
3185                 int i;
3186
3187                 if (!monitor_other_enable[1])
3188                         private->talkback_switch = 0;
3189                 else
3190                         private->talkback_switch = monitor_other_switch[1] + 1;
3191
3192                 err = scarlett2_usb_get_config(mixer,
3193                                                SCARLETT2_CONFIG_TALKBACK_MAP,
3194                                                1, &bitmap);
3195                 if (err < 0)
3196                         return err;
3197                 for (i = 0; i < private->num_mix_out; i++, bitmap >>= 1)
3198                         private->talkback_map[i] = bitmap & 1;
3199         }
3200
3201         return 0;
3202 }
3203
3204 static int scarlett2_speaker_switch_enum_ctl_info(
3205         struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3206 {
3207         static const char *const values[3] = {
3208                 "Off", "Main", "Alt"
3209         };
3210
3211         return snd_ctl_enum_info(uinfo, 1, 3, values);
3212 }
3213
3214 static int scarlett2_speaker_switch_enum_ctl_get(
3215         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3216 {
3217         struct usb_mixer_elem_info *elem = kctl->private_data;
3218         struct usb_mixer_interface *mixer = elem->head.mixer;
3219         struct scarlett2_data *private = mixer->private_data;
3220         int err = 0;
3221
3222         mutex_lock(&private->data_mutex);
3223
3224         if (private->hwdep_in_use) {
3225                 err = -EBUSY;
3226                 goto unlock;
3227         }
3228
3229         if (private->monitor_other_updated) {
3230                 err = scarlett2_update_monitor_other(mixer);
3231                 if (err < 0)
3232                         goto unlock;
3233         }
3234         ucontrol->value.enumerated.item[0] = private->speaker_switching_switch;
3235
3236 unlock:
3237         mutex_unlock(&private->data_mutex);
3238         return err;
3239 }
3240
3241 /* when speaker switching gets enabled, switch the main/alt speakers
3242  * to HW volume and disable those controls
3243  */
3244 static int scarlett2_speaker_switch_enable(struct usb_mixer_interface *mixer)
3245 {
3246         struct snd_card *card = mixer->chip->card;
3247         struct scarlett2_data *private = mixer->private_data;
3248         int i, err;
3249
3250         for (i = 0; i < 4; i++) {
3251                 int index = line_out_remap(private, i);
3252
3253                 /* switch the main/alt speakers to HW volume */
3254                 if (!private->vol_sw_hw_switch[index]) {
3255                         err = scarlett2_sw_hw_change(private->mixer, i, 1);
3256                         if (err < 0)
3257                                 return err;
3258                 }
3259
3260                 /* disable the line out SW/HW switch */
3261                 scarlett2_sw_hw_ctl_ro(private, i);
3262                 snd_ctl_notify(card,
3263                                SNDRV_CTL_EVENT_MASK_VALUE |
3264                                  SNDRV_CTL_EVENT_MASK_INFO,
3265                                &private->sw_hw_ctls[i]->id);
3266         }
3267
3268         /* when the next monitor-other notify comes in, update the mux
3269          * configuration
3270          */
3271         private->speaker_switching_switched = 1;
3272
3273         return 0;
3274 }
3275
3276 /* when speaker switching gets disabled, reenable the hw/sw controls
3277  * and invalidate the routing
3278  */
3279 static void scarlett2_speaker_switch_disable(struct usb_mixer_interface *mixer)
3280 {
3281         struct snd_card *card = mixer->chip->card;
3282         struct scarlett2_data *private = mixer->private_data;
3283         int i;
3284
3285         /* enable the line out SW/HW switch */
3286         for (i = 0; i < 4; i++) {
3287                 scarlett2_sw_hw_ctl_rw(private, i);
3288                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
3289                                &private->sw_hw_ctls[i]->id);
3290         }
3291
3292         /* when the next monitor-other notify comes in, update the mux
3293          * configuration
3294          */
3295         private->speaker_switching_switched = 1;
3296 }
3297
3298 static int scarlett2_speaker_switch_enum_ctl_put(
3299         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3300 {
3301         struct usb_mixer_elem_info *elem = kctl->private_data;
3302         struct usb_mixer_interface *mixer = elem->head.mixer;
3303         struct scarlett2_data *private = mixer->private_data;
3304
3305         int oval, val, err = 0;
3306
3307         mutex_lock(&private->data_mutex);
3308
3309         if (private->hwdep_in_use) {
3310                 err = -EBUSY;
3311                 goto unlock;
3312         }
3313
3314         oval = private->speaker_switching_switch;
3315         val = min(ucontrol->value.enumerated.item[0], 2U);
3316
3317         if (oval == val)
3318                 goto unlock;
3319
3320         private->speaker_switching_switch = val;
3321
3322         /* enable/disable speaker switching */
3323         err = scarlett2_usb_set_config(
3324                 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
3325                 0, !!val);
3326         if (err < 0)
3327                 goto unlock;
3328
3329         /* if speaker switching is enabled, select main or alt */
3330         err = scarlett2_usb_set_config(
3331                 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
3332                 0, val == 2);
3333         if (err < 0)
3334                 goto unlock;
3335
3336         /* update controls if speaker switching gets enabled or disabled */
3337         if (!oval && val)
3338                 err = scarlett2_speaker_switch_enable(mixer);
3339         else if (oval && !val)
3340                 scarlett2_speaker_switch_disable(mixer);
3341
3342         if (err == 0)
3343                 err = 1;
3344
3345 unlock:
3346         mutex_unlock(&private->data_mutex);
3347         return err;
3348 }
3349
3350 static const struct snd_kcontrol_new scarlett2_speaker_switch_enum_ctl = {
3351         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3352         .name = "",
3353         .info = scarlett2_speaker_switch_enum_ctl_info,
3354         .get  = scarlett2_speaker_switch_enum_ctl_get,
3355         .put  = scarlett2_speaker_switch_enum_ctl_put,
3356 };
3357
3358 static int scarlett2_add_speaker_switch_ctl(struct usb_mixer_interface *mixer)
3359 {
3360         struct scarlett2_data *private = mixer->private_data;
3361         const struct scarlett2_device_info *info = private->info;
3362
3363         if (!info->has_speaker_switching)
3364                 return 0;
3365
3366         return scarlett2_add_new_ctl(
3367                 mixer, &scarlett2_speaker_switch_enum_ctl,
3368                 0, 1, "Speaker Switching Playback Enum",
3369                 &private->speaker_switching_ctl);
3370 }
3371
3372 /*** Talkback and Talkback Map Controls ***/
3373
3374 static int scarlett2_talkback_enum_ctl_info(
3375         struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3376 {
3377         static const char *const values[3] = {
3378                 "Disabled", "Off", "On"
3379         };
3380
3381         return snd_ctl_enum_info(uinfo, 1, 3, values);
3382 }
3383
3384 static int scarlett2_talkback_enum_ctl_get(
3385         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3386 {
3387         struct usb_mixer_elem_info *elem = kctl->private_data;
3388         struct usb_mixer_interface *mixer = elem->head.mixer;
3389         struct scarlett2_data *private = mixer->private_data;
3390         int err = 0;
3391
3392         mutex_lock(&private->data_mutex);
3393
3394         if (private->hwdep_in_use) {
3395                 err = -EBUSY;
3396                 goto unlock;
3397         }
3398
3399         if (private->monitor_other_updated) {
3400                 err = scarlett2_update_monitor_other(mixer);
3401                 if (err < 0)
3402                         goto unlock;
3403         }
3404         ucontrol->value.enumerated.item[0] = private->talkback_switch;
3405
3406 unlock:
3407         mutex_unlock(&private->data_mutex);
3408         return err;
3409 }
3410
3411 static int scarlett2_talkback_enum_ctl_put(
3412         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3413 {
3414         struct usb_mixer_elem_info *elem = kctl->private_data;
3415         struct usb_mixer_interface *mixer = elem->head.mixer;
3416         struct scarlett2_data *private = mixer->private_data;
3417
3418         int oval, val, err = 0;
3419
3420         mutex_lock(&private->data_mutex);
3421
3422         if (private->hwdep_in_use) {
3423                 err = -EBUSY;
3424                 goto unlock;
3425         }
3426
3427         oval = private->talkback_switch;
3428         val = min(ucontrol->value.enumerated.item[0], 2U);
3429
3430         if (oval == val)
3431                 goto unlock;
3432
3433         private->talkback_switch = val;
3434
3435         /* enable/disable talkback */
3436         err = scarlett2_usb_set_config(
3437                 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
3438                 1, !!val);
3439         if (err < 0)
3440                 goto unlock;
3441
3442         /* if talkback is enabled, select main or alt */
3443         err = scarlett2_usb_set_config(
3444                 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
3445                 1, val == 2);
3446         if (err == 0)
3447                 err = 1;
3448
3449 unlock:
3450         mutex_unlock(&private->data_mutex);
3451         return err;
3452 }
3453
3454 static const struct snd_kcontrol_new scarlett2_talkback_enum_ctl = {
3455         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3456         .name = "",
3457         .info = scarlett2_talkback_enum_ctl_info,
3458         .get  = scarlett2_talkback_enum_ctl_get,
3459         .put  = scarlett2_talkback_enum_ctl_put,
3460 };
3461
3462 static int scarlett2_talkback_map_ctl_get(
3463         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3464 {
3465         struct usb_mixer_elem_info *elem = kctl->private_data;
3466         struct usb_mixer_interface *mixer = elem->head.mixer;
3467         struct scarlett2_data *private = mixer->private_data;
3468         int index = elem->control;
3469
3470         ucontrol->value.integer.value[0] = private->talkback_map[index];
3471
3472         return 0;
3473 }
3474
3475 static int scarlett2_talkback_map_ctl_put(
3476         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3477 {
3478         struct usb_mixer_elem_info *elem = kctl->private_data;
3479         struct usb_mixer_interface *mixer = elem->head.mixer;
3480         struct scarlett2_data *private = mixer->private_data;
3481         int index = elem->control;
3482         int oval, val, err = 0, i;
3483         u16 bitmap = 0;
3484
3485         mutex_lock(&private->data_mutex);
3486
3487         if (private->hwdep_in_use) {
3488                 err = -EBUSY;
3489                 goto unlock;
3490         }
3491
3492         oval = private->talkback_map[index];
3493         val = !!ucontrol->value.integer.value[0];
3494
3495         if (oval == val)
3496                 goto unlock;
3497
3498         private->talkback_map[index] = val;
3499
3500         for (i = 0; i < private->num_mix_out; i++)
3501                 bitmap |= private->talkback_map[i] << i;
3502
3503         /* Send updated bitmap to the device */
3504         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_TALKBACK_MAP,
3505                                        0, bitmap);
3506         if (err == 0)
3507                 err = 1;
3508
3509 unlock:
3510         mutex_unlock(&private->data_mutex);
3511         return err;
3512 }
3513
3514 static const struct snd_kcontrol_new scarlett2_talkback_map_ctl = {
3515         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3516         .name = "",
3517         .info = snd_ctl_boolean_mono_info,
3518         .get  = scarlett2_talkback_map_ctl_get,
3519         .put  = scarlett2_talkback_map_ctl_put,
3520 };
3521
3522 static int scarlett2_add_talkback_ctls(struct usb_mixer_interface *mixer)
3523 {
3524         struct scarlett2_data *private = mixer->private_data;
3525         const struct scarlett2_device_info *info = private->info;
3526         int err, i;
3527         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3528
3529         if (!info->has_talkback)
3530                 return 0;
3531
3532         err = scarlett2_add_new_ctl(
3533                 mixer, &scarlett2_talkback_enum_ctl,
3534                 0, 1, "Talkback Playback Enum",
3535                 &private->talkback_ctl);
3536         if (err < 0)
3537                 return err;
3538
3539         for (i = 0; i < private->num_mix_out; i++) {
3540                 snprintf(s, sizeof(s),
3541                          "Talkback Mix %c Playback Switch", i + 'A');
3542                 err = scarlett2_add_new_ctl(mixer, &scarlett2_talkback_map_ctl,
3543                                             i, 1, s, NULL);
3544                 if (err < 0)
3545                         return err;
3546         }
3547
3548         return 0;
3549 }
3550
3551 /*** Dim/Mute Controls ***/
3552
3553 static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
3554                                       struct snd_ctl_elem_value *ucontrol)
3555 {
3556         struct usb_mixer_elem_info *elem = kctl->private_data;
3557         struct usb_mixer_interface *mixer = elem->head.mixer;
3558         struct scarlett2_data *private = mixer->private_data;
3559         int err = 0;
3560
3561         mutex_lock(&private->data_mutex);
3562
3563         if (private->hwdep_in_use) {
3564                 err = -EBUSY;
3565                 goto unlock;
3566         }
3567
3568         if (private->dim_mute_updated) {
3569                 err = scarlett2_update_dim_mute(mixer);
3570                 if (err < 0)
3571                         goto unlock;
3572         }
3573         ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
3574
3575 unlock:
3576         mutex_unlock(&private->data_mutex);
3577         return err;
3578 }
3579
3580 static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
3581                                       struct snd_ctl_elem_value *ucontrol)
3582 {
3583         struct usb_mixer_elem_info *elem = kctl->private_data;
3584         struct usb_mixer_interface *mixer = elem->head.mixer;
3585         struct scarlett2_data *private = mixer->private_data;
3586         int index = elem->control;
3587         int oval, val, err = 0, i;
3588
3589         mutex_lock(&private->data_mutex);
3590
3591         if (private->hwdep_in_use) {
3592                 err = -EBUSY;
3593                 goto unlock;
3594         }
3595
3596         oval = private->dim_mute[index];
3597         val = !!ucontrol->value.integer.value[0];
3598
3599         if (oval == val)
3600                 goto unlock;
3601
3602         private->dim_mute[index] = val;
3603
3604         /* Send switch change to the device */
3605         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
3606                                        index, val);
3607         if (err == 0)
3608                 err = 1;
3609
3610         if (index == SCARLETT2_BUTTON_MUTE)
3611                 for (i = 0; i < private->num_line_out; i++) {
3612                         int line_index = line_out_remap(private, i);
3613
3614                         if (private->vol_sw_hw_switch[line_index]) {
3615                                 private->mute_switch[line_index] = val;
3616                                 snd_ctl_notify(mixer->chip->card,
3617                                                SNDRV_CTL_EVENT_MASK_VALUE,
3618                                                &private->mute_ctls[i]->id);
3619                         }
3620                 }
3621
3622 unlock:
3623         mutex_unlock(&private->data_mutex);
3624         return err;
3625 }
3626
3627 static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
3628         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3629         .name = "",
3630         .info = snd_ctl_boolean_mono_info,
3631         .get  = scarlett2_dim_mute_ctl_get,
3632         .put  = scarlett2_dim_mute_ctl_put
3633 };
3634
3635 /*** Create the analogue output controls ***/
3636
3637 static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
3638 {
3639         struct scarlett2_data *private = mixer->private_data;
3640         const struct scarlett2_device_info *info = private->info;
3641         int err, i;
3642         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3643
3644         /* Add R/O HW volume control */
3645         if (scarlett2_has_config_item(private,
3646                                       SCARLETT2_CONFIG_MASTER_VOLUME)) {
3647                 snprintf(s, sizeof(s), "Master HW Playback Volume");
3648                 err = scarlett2_add_new_ctl(mixer,
3649                                             &scarlett2_master_volume_ctl,
3650                                             0, 1, s, &private->master_vol_ctl);
3651                 if (err < 0)
3652                         return err;
3653         }
3654
3655         /* Remaining controls are only applicable if the device
3656          * has per-channel line-out volume controls.
3657          */
3658         if (!scarlett2_has_config_item(private,
3659                                        SCARLETT2_CONFIG_LINE_OUT_VOLUME))
3660                 return 0;
3661
3662         /* Add volume controls */
3663         for (i = 0; i < private->num_line_out; i++) {
3664                 int index = line_out_remap(private, i);
3665
3666                 /* Fader */
3667                 if (info->line_out_descrs[i])
3668                         snprintf(s, sizeof(s),
3669                                  "Line %02d (%s) Playback Volume",
3670                                  i + 1, info->line_out_descrs[i]);
3671                 else
3672                         snprintf(s, sizeof(s),
3673                                  "Line %02d Playback Volume",
3674                                  i + 1);
3675                 err = scarlett2_add_new_ctl(mixer,
3676                                             &scarlett2_line_out_volume_ctl,
3677                                             i, 1, s, &private->vol_ctls[i]);
3678                 if (err < 0)
3679                         return err;
3680
3681                 /* Mute Switch */
3682                 snprintf(s, sizeof(s),
3683                          "Line %02d Mute Playback Switch",
3684                          i + 1);
3685                 err = scarlett2_add_new_ctl(mixer,
3686                                             &scarlett2_mute_ctl,
3687                                             i, 1, s,
3688                                             &private->mute_ctls[i]);
3689                 if (err < 0)
3690                         return err;
3691
3692                 /* SW/HW Switch */
3693                 if (scarlett2_has_config_item(private,
3694                                               SCARLETT2_CONFIG_SW_HW_SWITCH)) {
3695
3696                         /* Make the fader and mute controls read-only if the
3697                          * SW/HW switch is set to HW
3698                          */
3699                         if (private->vol_sw_hw_switch[index])
3700                                 scarlett2_vol_ctl_set_writable(mixer, i, 0);
3701
3702                         snprintf(s, sizeof(s),
3703                                  "Line Out %02d Volume Control Playback Enum",
3704                                  i + 1);
3705                         err = scarlett2_add_new_ctl(mixer,
3706                                                     &scarlett2_sw_hw_enum_ctl,
3707                                                     i, 1, s,
3708                                                     &private->sw_hw_ctls[i]);
3709                         if (err < 0)
3710                                 return err;
3711
3712                         /* Make the switch read-only if the line is
3713                          * involved in speaker switching
3714                          */
3715                         if (private->speaker_switching_switch && i < 4)
3716                                 scarlett2_sw_hw_ctl_ro(private, i);
3717                 }
3718         }
3719
3720         /* Add dim/mute controls */
3721         if (scarlett2_has_config_item(private, SCARLETT2_CONFIG_DIM_MUTE))
3722                 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
3723                         err = scarlett2_add_new_ctl(
3724                                 mixer, &scarlett2_dim_mute_ctl,
3725                                 i, 1, scarlett2_dim_mute_names[i],
3726                                 &private->dim_mute_ctls[i]);
3727                         if (err < 0)
3728                                 return err;
3729                 }
3730
3731         return 0;
3732 }
3733
3734 /*** Create the analogue input controls ***/
3735
3736 static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
3737 {
3738         struct scarlett2_data *private = mixer->private_data;
3739         const struct scarlett2_device_info *info = private->info;
3740         int err, i;
3741         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3742         const char *fmt = "Line In %d %s Capture %s";
3743         const char *fmt2 = "Line In %d-%d %s Capture %s";
3744
3745         /* Add input level (line/inst) controls */
3746         for (i = 0; i < info->level_input_count; i++) {
3747                 snprintf(s, sizeof(s), fmt, i + 1 + info->level_input_first,
3748                          "Level", "Enum");
3749                 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
3750                                             i, 1, s, &private->level_ctls[i]);
3751                 if (err < 0)
3752                         return err;
3753         }
3754
3755         /* Add input pad controls */
3756         for (i = 0; i < info->pad_input_count; i++) {
3757                 snprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
3758                 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
3759                                             i, 1, s, &private->pad_ctls[i]);
3760                 if (err < 0)
3761                         return err;
3762         }
3763
3764         /* Add input air controls */
3765         for (i = 0; i < info->air_input_count; i++) {
3766                 snprintf(s, sizeof(s), fmt, i + 1, "Air", "Switch");
3767                 err = scarlett2_add_new_ctl(mixer, &scarlett2_air_ctl,
3768                                             i, 1, s, &private->air_ctls[i]);
3769                 if (err < 0)
3770                         return err;
3771         }
3772
3773         /* Add input phantom controls */
3774         if (info->inputs_per_phantom == 1) {
3775                 for (i = 0; i < info->phantom_count; i++) {
3776                         scnprintf(s, sizeof(s), fmt, i + 1,
3777                                   "Phantom Power", "Switch");
3778                         err = scarlett2_add_new_ctl(
3779                                 mixer, &scarlett2_phantom_ctl,
3780                                 i, 1, s, &private->phantom_ctls[i]);
3781                         if (err < 0)
3782                                 return err;
3783                 }
3784         } else if (info->inputs_per_phantom > 1) {
3785                 for (i = 0; i < info->phantom_count; i++) {
3786                         int from = i * info->inputs_per_phantom + 1;
3787                         int to = (i + 1) * info->inputs_per_phantom;
3788
3789                         scnprintf(s, sizeof(s), fmt2, from, to,
3790                                   "Phantom Power", "Switch");
3791                         err = scarlett2_add_new_ctl(
3792                                 mixer, &scarlett2_phantom_ctl,
3793                                 i, 1, s, &private->phantom_ctls[i]);
3794                         if (err < 0)
3795                                 return err;
3796                 }
3797         }
3798         if (info->phantom_count &&
3799             scarlett2_has_config_item(private,
3800                                       SCARLETT2_CONFIG_PHANTOM_PERSISTENCE)) {
3801                 err = scarlett2_add_new_ctl(
3802                         mixer, &scarlett2_phantom_persistence_ctl, 0, 1,
3803                         "Phantom Power Persistence Capture Switch", NULL);
3804                 if (err < 0)
3805                         return err;
3806         }
3807
3808         return 0;
3809 }
3810
3811 /*** Mixer Volume Controls ***/
3812
3813 static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
3814                                     struct snd_ctl_elem_info *uinfo)
3815 {
3816         struct usb_mixer_elem_info *elem = kctl->private_data;
3817
3818         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3819         uinfo->count = elem->channels;
3820         uinfo->value.integer.min = 0;
3821         uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
3822         uinfo->value.integer.step = 1;
3823         return 0;
3824 }
3825
3826 static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
3827                                    struct snd_ctl_elem_value *ucontrol)
3828 {
3829         struct usb_mixer_elem_info *elem = kctl->private_data;
3830         struct scarlett2_data *private = elem->head.mixer->private_data;
3831
3832         ucontrol->value.integer.value[0] = private->mix[elem->control];
3833         return 0;
3834 }
3835
3836 static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
3837                                    struct snd_ctl_elem_value *ucontrol)
3838 {
3839         struct usb_mixer_elem_info *elem = kctl->private_data;
3840         struct usb_mixer_interface *mixer = elem->head.mixer;
3841         struct scarlett2_data *private = mixer->private_data;
3842         int oval, val, mix_num, err = 0;
3843         int index = elem->control;
3844
3845         mutex_lock(&private->data_mutex);
3846
3847         if (private->hwdep_in_use) {
3848                 err = -EBUSY;
3849                 goto unlock;
3850         }
3851
3852         oval = private->mix[index];
3853         val = clamp(ucontrol->value.integer.value[0],
3854                     0L, (long)SCARLETT2_MIXER_MAX_VALUE);
3855         mix_num = index / private->num_mix_in;
3856
3857         if (oval == val)
3858                 goto unlock;
3859
3860         private->mix[index] = val;
3861         err = scarlett2_usb_set_mix(mixer, mix_num);
3862         if (err == 0)
3863                 err = 1;
3864
3865 unlock:
3866         mutex_unlock(&private->data_mutex);
3867         return err;
3868 }
3869
3870 static const DECLARE_TLV_DB_MINMAX(
3871         db_scale_scarlett2_mixer,
3872         SCARLETT2_MIXER_MIN_DB * 100,
3873         SCARLETT2_MIXER_MAX_DB * 100
3874 );
3875
3876 static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
3877         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3878         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3879                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3880         .name = "",
3881         .info = scarlett2_mixer_ctl_info,
3882         .get  = scarlett2_mixer_ctl_get,
3883         .put  = scarlett2_mixer_ctl_put,
3884         .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
3885         .tlv = { .p = db_scale_scarlett2_mixer }
3886 };
3887
3888 static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
3889 {
3890         struct scarlett2_data *private = mixer->private_data;
3891         int err, i, j;
3892         int index;
3893         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3894
3895         for (i = 0, index = 0; i < private->num_mix_out; i++)
3896                 for (j = 0; j < private->num_mix_in; j++, index++) {
3897                         snprintf(s, sizeof(s),
3898                                  "Mix %c Input %02d Playback Volume",
3899                                  'A' + i, j + 1);
3900                         err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
3901                                                     index, 1, s, NULL);
3902                         if (err < 0)
3903                                 return err;
3904                 }
3905
3906         return 0;
3907 }
3908
3909 /*** Direct Monitor Control ***/
3910
3911 static int scarlett2_update_direct_monitor(struct usb_mixer_interface *mixer)
3912 {
3913         struct scarlett2_data *private = mixer->private_data;
3914
3915         private->direct_monitor_updated = 0;
3916
3917         if (!private->info->direct_monitor)
3918                 return 0;
3919
3920         return scarlett2_usb_get_config(
3921                 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR,
3922                 1, &private->direct_monitor_switch);
3923 }
3924
3925 static int scarlett2_direct_monitor_ctl_get(
3926         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3927 {
3928         struct usb_mixer_elem_info *elem = kctl->private_data;
3929         struct usb_mixer_interface *mixer = elem->head.mixer;
3930         struct scarlett2_data *private = elem->head.mixer->private_data;
3931         int err = 0;
3932
3933         mutex_lock(&private->data_mutex);
3934
3935         if (private->hwdep_in_use) {
3936                 err = -EBUSY;
3937                 goto unlock;
3938         }
3939
3940         if (private->direct_monitor_updated) {
3941                 err = scarlett2_update_direct_monitor(mixer);
3942                 if (err < 0)
3943                         goto unlock;
3944         }
3945         ucontrol->value.enumerated.item[0] = private->direct_monitor_switch;
3946
3947 unlock:
3948         mutex_unlock(&private->data_mutex);
3949         return err;
3950 }
3951
3952 static int scarlett2_direct_monitor_ctl_put(
3953         struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3954 {
3955         struct usb_mixer_elem_info *elem = kctl->private_data;
3956         struct usb_mixer_interface *mixer = elem->head.mixer;
3957         struct scarlett2_data *private = mixer->private_data;
3958
3959         int index = elem->control;
3960         int oval, val, err = 0;
3961
3962         mutex_lock(&private->data_mutex);
3963
3964         if (private->hwdep_in_use) {
3965                 err = -EBUSY;
3966                 goto unlock;
3967         }
3968
3969         oval = private->direct_monitor_switch;
3970         val = min(ucontrol->value.enumerated.item[0], 2U);
3971
3972         if (oval == val)
3973                 goto unlock;
3974
3975         private->direct_monitor_switch = val;
3976
3977         /* Send switch change to the device */
3978         err = scarlett2_usb_set_config(
3979                 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, index, val);
3980         if (err == 0)
3981                 err = 1;
3982
3983 unlock:
3984         mutex_unlock(&private->data_mutex);
3985         return err;
3986 }
3987
3988 static int scarlett2_direct_monitor_stereo_enum_ctl_info(
3989         struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3990 {
3991         static const char *const values[3] = {
3992                 "Off", "Mono", "Stereo"
3993         };
3994
3995         return snd_ctl_enum_info(uinfo, 1, 3, values);
3996 }
3997
3998 /* Direct Monitor for Solo is mono-only and only needs a boolean control
3999  * Direct Monitor for 2i2 is selectable between Off/Mono/Stereo
4000  */
4001 static const struct snd_kcontrol_new scarlett2_direct_monitor_ctl[2] = {
4002         {
4003                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4004                 .name = "",
4005                 .info = snd_ctl_boolean_mono_info,
4006                 .get  = scarlett2_direct_monitor_ctl_get,
4007                 .put  = scarlett2_direct_monitor_ctl_put,
4008         },
4009         {
4010                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4011                 .name = "",
4012                 .info = scarlett2_direct_monitor_stereo_enum_ctl_info,
4013                 .get  = scarlett2_direct_monitor_ctl_get,
4014                 .put  = scarlett2_direct_monitor_ctl_put,
4015         }
4016 };
4017
4018 static int scarlett2_add_direct_monitor_ctl(struct usb_mixer_interface *mixer)
4019 {
4020         struct scarlett2_data *private = mixer->private_data;
4021         const struct scarlett2_device_info *info = private->info;
4022         const char *s;
4023
4024         if (!info->direct_monitor)
4025                 return 0;
4026
4027         s = info->direct_monitor == 1
4028               ? "Direct Monitor Playback Switch"
4029               : "Direct Monitor Playback Enum";
4030
4031         return scarlett2_add_new_ctl(
4032                 mixer, &scarlett2_direct_monitor_ctl[info->direct_monitor - 1],
4033                 0, 1, s, &private->direct_monitor_ctl);
4034 }
4035
4036 /*** Mux Source Selection Controls ***/
4037
4038 static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
4039                                            struct snd_ctl_elem_info *uinfo)
4040 {
4041         struct usb_mixer_elem_info *elem = kctl->private_data;
4042         struct scarlett2_data *private = elem->head.mixer->private_data;
4043         const struct scarlett2_device_info *info = private->info;
4044         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
4045         unsigned int item = uinfo->value.enumerated.item;
4046         int items = private->num_mux_srcs;
4047         int port_type;
4048
4049         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4050         uinfo->count = elem->channels;
4051         uinfo->value.enumerated.items = items;
4052
4053         if (item >= items)
4054                 item = uinfo->value.enumerated.item = items - 1;
4055
4056         for (port_type = 0;
4057              port_type < SCARLETT2_PORT_TYPE_COUNT;
4058              port_type++) {
4059                 if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
4060                         const struct scarlett2_port *port =
4061                                 &scarlett2_ports[port_type];
4062
4063                         sprintf(uinfo->value.enumerated.name,
4064                                 port->src_descr, item + port->src_num_offset);
4065                         return 0;
4066                 }
4067                 item -= port_count[port_type][SCARLETT2_PORT_IN];
4068         }
4069
4070         return -EINVAL;
4071 }
4072
4073 static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
4074                                           struct snd_ctl_elem_value *ucontrol)
4075 {
4076         struct usb_mixer_elem_info *elem = kctl->private_data;
4077         struct usb_mixer_interface *mixer = elem->head.mixer;
4078         struct scarlett2_data *private = mixer->private_data;
4079         int index = line_out_remap(private, elem->control);
4080         int err = 0;
4081
4082         mutex_lock(&private->data_mutex);
4083
4084         if (private->hwdep_in_use) {
4085                 err = -EBUSY;
4086                 goto unlock;
4087         }
4088
4089         if (private->mux_updated) {
4090                 err = scarlett2_usb_get_mux(mixer);
4091                 if (err < 0)
4092                         goto unlock;
4093         }
4094         ucontrol->value.enumerated.item[0] = private->mux[index];
4095
4096 unlock:
4097         mutex_unlock(&private->data_mutex);
4098         return err;
4099 }
4100
4101 static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
4102                                           struct snd_ctl_elem_value *ucontrol)
4103 {
4104         struct usb_mixer_elem_info *elem = kctl->private_data;
4105         struct usb_mixer_interface *mixer = elem->head.mixer;
4106         struct scarlett2_data *private = mixer->private_data;
4107         int index = line_out_remap(private, elem->control);
4108         int oval, val, err = 0;
4109
4110         mutex_lock(&private->data_mutex);
4111
4112         if (private->hwdep_in_use) {
4113                 err = -EBUSY;
4114                 goto unlock;
4115         }
4116
4117         oval = private->mux[index];
4118         val = min(ucontrol->value.enumerated.item[0],
4119                   private->num_mux_srcs - 1U);
4120
4121         if (oval == val)
4122                 goto unlock;
4123
4124         private->mux[index] = val;
4125         err = scarlett2_usb_set_mux(mixer);
4126         if (err == 0)
4127                 err = 1;
4128
4129 unlock:
4130         mutex_unlock(&private->data_mutex);
4131         return err;
4132 }
4133
4134 static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
4135         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4136         .name = "",
4137         .info = scarlett2_mux_src_enum_ctl_info,
4138         .get  = scarlett2_mux_src_enum_ctl_get,
4139         .put  = scarlett2_mux_src_enum_ctl_put,
4140 };
4141
4142 static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
4143 {
4144         struct scarlett2_data *private = mixer->private_data;
4145         const struct scarlett2_device_info *info = private->info;
4146         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
4147         int port_type, channel, i;
4148
4149         for (i = 0, port_type = 0;
4150              port_type < SCARLETT2_PORT_TYPE_COUNT;
4151              port_type++) {
4152                 for (channel = 0;
4153                      channel < port_count[port_type][SCARLETT2_PORT_OUT];
4154                      channel++, i++) {
4155                         int err;
4156                         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
4157                         const char *const descr =
4158                                 scarlett2_ports[port_type].dst_descr;
4159
4160                         snprintf(s, sizeof(s) - 5, descr, channel + 1);
4161                         strcat(s, " Enum");
4162
4163                         err = scarlett2_add_new_ctl(mixer,
4164                                                     &scarlett2_mux_src_enum_ctl,
4165                                                     i, 1, s,
4166                                                     &private->mux_ctls[i]);
4167                         if (err < 0)
4168                                 return err;
4169                 }
4170         }
4171
4172         return 0;
4173 }
4174
4175 /*** Meter Controls ***/
4176
4177 static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
4178                                     struct snd_ctl_elem_info *uinfo)
4179 {
4180         struct usb_mixer_elem_info *elem = kctl->private_data;
4181
4182         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
4183         uinfo->count = elem->channels;
4184         uinfo->value.integer.min = 0;
4185         uinfo->value.integer.max = 4095;
4186         uinfo->value.integer.step = 1;
4187         return 0;
4188 }
4189
4190 static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
4191                                    struct snd_ctl_elem_value *ucontrol)
4192 {
4193         struct usb_mixer_elem_info *elem = kctl->private_data;
4194         struct scarlett2_data *private = elem->head.mixer->private_data;
4195         u8 *meter_level_map = private->meter_level_map;
4196         u16 meter_levels[SCARLETT2_MAX_METERS];
4197         int i, err;
4198
4199         mutex_lock(&private->data_mutex);
4200
4201         if (private->hwdep_in_use) {
4202                 err = -EBUSY;
4203                 goto unlock;
4204         }
4205
4206         err = scarlett2_usb_get_meter_levels(elem->head.mixer, elem->channels,
4207                                              meter_levels);
4208         if (err < 0)
4209                 goto unlock;
4210
4211         /* copy & translate from meter_levels[] using meter_level_map[] */
4212         for (i = 0; i < elem->channels; i++) {
4213                 int idx = meter_level_map[i];
4214                 int value;
4215
4216                 if (idx == 255)
4217                         value = 0;
4218                 else
4219                         value = meter_levels[idx];
4220
4221                 ucontrol->value.integer.value[i] = value;
4222         }
4223
4224 unlock:
4225         mutex_unlock(&private->data_mutex);
4226
4227         return err;
4228 }
4229
4230 static const struct snd_kcontrol_new scarlett2_meter_ctl = {
4231         .iface = SNDRV_CTL_ELEM_IFACE_PCM,
4232         .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
4233         .name = "",
4234         .info = scarlett2_meter_ctl_info,
4235         .get  = scarlett2_meter_ctl_get
4236 };
4237
4238 static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
4239 {
4240         struct scarlett2_data *private = mixer->private_data;
4241
4242         /* devices without a mixer also don't support reporting levels */
4243         if (!scarlett2_has_mixer(private))
4244                 return 0;
4245
4246         return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
4247                                      0, private->num_mux_dsts,
4248                                      "Level Meter", NULL);
4249 }
4250
4251 /*** MSD Controls ***/
4252
4253 static int scarlett2_msd_ctl_get(struct snd_kcontrol *kctl,
4254                                  struct snd_ctl_elem_value *ucontrol)
4255 {
4256         struct usb_mixer_elem_info *elem = kctl->private_data;
4257         struct scarlett2_data *private = elem->head.mixer->private_data;
4258
4259         ucontrol->value.integer.value[0] = private->msd_switch;
4260         return 0;
4261 }
4262
4263 static int scarlett2_msd_ctl_put(struct snd_kcontrol *kctl,
4264                                  struct snd_ctl_elem_value *ucontrol)
4265 {
4266         struct usb_mixer_elem_info *elem = kctl->private_data;
4267         struct usb_mixer_interface *mixer = elem->head.mixer;
4268         struct scarlett2_data *private = mixer->private_data;
4269
4270         int oval, val, err = 0;
4271
4272         mutex_lock(&private->data_mutex);
4273
4274         if (private->hwdep_in_use) {
4275                 err = -EBUSY;
4276                 goto unlock;
4277         }
4278
4279         oval = private->msd_switch;
4280         val = !!ucontrol->value.integer.value[0];
4281
4282         if (oval == val)
4283                 goto unlock;
4284
4285         private->msd_switch = val;
4286
4287         /* Send switch change to the device */
4288         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MSD_SWITCH,
4289                                        0, val);
4290         if (err == 0)
4291                 err = 1;
4292
4293 unlock:
4294         mutex_unlock(&private->data_mutex);
4295         return err;
4296 }
4297
4298 static const struct snd_kcontrol_new scarlett2_msd_ctl = {
4299         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4300         .name = "",
4301         .info = snd_ctl_boolean_mono_info,
4302         .get  = scarlett2_msd_ctl_get,
4303         .put  = scarlett2_msd_ctl_put,
4304 };
4305
4306 static int scarlett2_add_msd_ctl(struct usb_mixer_interface *mixer)
4307 {
4308         struct scarlett2_data *private = mixer->private_data;
4309
4310         if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_MSD_SWITCH))
4311                 return 0;
4312
4313         /* If MSD mode is off, hide the switch by default */
4314         if (!private->msd_switch && !(mixer->chip->setup & SCARLETT2_MSD_ENABLE))
4315                 return 0;
4316
4317         /* Add MSD control */
4318         return scarlett2_add_new_ctl(mixer, &scarlett2_msd_ctl,
4319                                      0, 1, "MSD Mode Switch", NULL);
4320 }
4321
4322 /*** Standalone Control ***/
4323
4324 static int scarlett2_standalone_ctl_get(struct snd_kcontrol *kctl,
4325                                         struct snd_ctl_elem_value *ucontrol)
4326 {
4327         struct usb_mixer_elem_info *elem = kctl->private_data;
4328         struct scarlett2_data *private = elem->head.mixer->private_data;
4329
4330         ucontrol->value.integer.value[0] = private->standalone_switch;
4331         return 0;
4332 }
4333
4334 static int scarlett2_standalone_ctl_put(struct snd_kcontrol *kctl,
4335                                         struct snd_ctl_elem_value *ucontrol)
4336 {
4337         struct usb_mixer_elem_info *elem = kctl->private_data;
4338         struct usb_mixer_interface *mixer = elem->head.mixer;
4339         struct scarlett2_data *private = mixer->private_data;
4340
4341         int oval, val, err = 0;
4342
4343         mutex_lock(&private->data_mutex);
4344
4345         if (private->hwdep_in_use) {
4346                 err = -EBUSY;
4347                 goto unlock;
4348         }
4349
4350         oval = private->standalone_switch;
4351         val = !!ucontrol->value.integer.value[0];
4352
4353         if (oval == val)
4354                 goto unlock;
4355
4356         private->standalone_switch = val;
4357
4358         /* Send switch change to the device */
4359         err = scarlett2_usb_set_config(mixer,
4360                                        SCARLETT2_CONFIG_STANDALONE_SWITCH,
4361                                        0, val);
4362         if (err == 0)
4363                 err = 1;
4364
4365 unlock:
4366         mutex_unlock(&private->data_mutex);
4367         return err;
4368 }
4369
4370 static const struct snd_kcontrol_new scarlett2_standalone_ctl = {
4371         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4372         .name = "",
4373         .info = snd_ctl_boolean_mono_info,
4374         .get  = scarlett2_standalone_ctl_get,
4375         .put  = scarlett2_standalone_ctl_put,
4376 };
4377
4378 static int scarlett2_add_standalone_ctl(struct usb_mixer_interface *mixer)
4379 {
4380         struct scarlett2_data *private = mixer->private_data;
4381
4382         if (!scarlett2_has_config_item(private,
4383                                        SCARLETT2_CONFIG_STANDALONE_SWITCH))
4384                 return 0;
4385
4386         /* Add standalone control */
4387         return scarlett2_add_new_ctl(mixer, &scarlett2_standalone_ctl,
4388                                      0, 1, "Standalone Switch", NULL);
4389 }
4390
4391 /*** Cleanup/Suspend Callbacks ***/
4392
4393 static void scarlett2_private_free(struct usb_mixer_interface *mixer)
4394 {
4395         struct scarlett2_data *private = mixer->private_data;
4396
4397         cancel_delayed_work_sync(&private->work);
4398         kfree(private);
4399         mixer->private_data = NULL;
4400 }
4401
4402 static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
4403 {
4404         struct scarlett2_data *private = mixer->private_data;
4405
4406         if (cancel_delayed_work_sync(&private->work))
4407                 scarlett2_config_save(private->mixer);
4408 }
4409
4410 /*** Initialisation ***/
4411
4412 static void scarlett2_count_io(struct scarlett2_data *private)
4413 {
4414         const struct scarlett2_device_info *info = private->info;
4415         const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
4416         int port_type, srcs = 0, dsts = 0;
4417
4418         /* Count the number of mux sources and destinations */
4419         for (port_type = 0;
4420              port_type < SCARLETT2_PORT_TYPE_COUNT;
4421              port_type++) {
4422                 srcs += port_count[port_type][SCARLETT2_PORT_IN];
4423                 dsts += port_count[port_type][SCARLETT2_PORT_OUT];
4424         }
4425
4426         private->num_mux_srcs = srcs;
4427         private->num_mux_dsts = dsts;
4428
4429         /* Mixer inputs are mux outputs and vice versa */
4430         private->num_mix_in =
4431                 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
4432
4433         private->num_mix_out =
4434                 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
4435
4436         /* Number of analogue line outputs */
4437         private->num_line_out =
4438                 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
4439 }
4440
4441 /* Look through the interface descriptors for the Focusrite Control
4442  * interface (bInterfaceClass = 255 Vendor Specific Class) and set
4443  * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
4444  * in private
4445  */
4446 static int scarlett2_find_fc_interface(struct usb_device *dev,
4447                                        struct scarlett2_data *private)
4448 {
4449         struct usb_host_config *config = dev->actconfig;
4450         int i;
4451
4452         for (i = 0; i < config->desc.bNumInterfaces; i++) {
4453                 struct usb_interface *intf = config->interface[i];
4454                 struct usb_interface_descriptor *desc =
4455                         &intf->altsetting[0].desc;
4456                 struct usb_endpoint_descriptor *epd;
4457
4458                 if (desc->bInterfaceClass != 255)
4459                         continue;
4460
4461                 epd = get_endpoint(intf->altsetting, 0);
4462                 private->bInterfaceNumber = desc->bInterfaceNumber;
4463                 private->bEndpointAddress = epd->bEndpointAddress &
4464                         USB_ENDPOINT_NUMBER_MASK;
4465                 private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
4466                 private->bInterval = epd->bInterval;
4467                 return 0;
4468         }
4469
4470         return -EINVAL;
4471 }
4472
4473 /* Initialise private data */
4474 static int scarlett2_init_private(struct usb_mixer_interface *mixer,
4475                                   const struct scarlett2_device_entry *entry)
4476 {
4477         struct scarlett2_data *private =
4478                 kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);
4479
4480         if (!private)
4481                 return -ENOMEM;
4482
4483         mutex_init(&private->usb_mutex);
4484         mutex_init(&private->data_mutex);
4485         INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
4486
4487         mixer->private_data = private;
4488         mixer->private_free = scarlett2_private_free;
4489         mixer->private_suspend = scarlett2_private_suspend;
4490
4491         private->info = entry->info;
4492         private->config_set = entry->info->config_set;
4493         private->series_name = entry->series_name;
4494         scarlett2_count_io(private);
4495         private->scarlett2_seq = 0;
4496         private->mixer = mixer;
4497
4498         return scarlett2_find_fc_interface(mixer->chip->dev, private);
4499 }
4500
4501 /* Cargo cult proprietary initialisation sequence */
4502 static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
4503 {
4504         struct usb_device *dev = mixer->chip->dev;
4505         struct scarlett2_data *private = mixer->private_data;
4506         u8 step0_buf[24];
4507         u8 step2_buf[84];
4508         int err;
4509
4510         if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
4511                 return -EINVAL;
4512
4513         /* step 0 */
4514         err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
4515                                SCARLETT2_USB_CMD_INIT,
4516                                step0_buf, sizeof(step0_buf));
4517         if (err < 0)
4518                 return err;
4519
4520         /* step 1 */
4521         private->scarlett2_seq = 1;
4522         err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
4523         if (err < 0)
4524                 return err;
4525
4526         /* step 2 */
4527         private->scarlett2_seq = 1;
4528         err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_2,
4529                             NULL, 0,
4530                             step2_buf, sizeof(step2_buf));
4531         if (err < 0)
4532                 return err;
4533
4534         /* extract 4-byte firmware version from step2_buf[8] */
4535         private->firmware_version = le32_to_cpu(*(__le32 *)(step2_buf + 8));
4536         usb_audio_info(mixer->chip,
4537                        "Firmware version %d\n",
4538                        private->firmware_version);
4539
4540         return 0;
4541 }
4542
4543 /* Get the flash segment numbers for the App_Settings and App_Upgrade
4544  * segments and put them in the private data
4545  */
4546 static int scarlett2_get_flash_segment_nums(struct usb_mixer_interface *mixer)
4547 {
4548         struct scarlett2_data *private = mixer->private_data;
4549         int err, count, i;
4550
4551         struct {
4552                 __le32 size;
4553                 __le32 count;
4554                 u8 unknown[8];
4555         } __packed flash_info;
4556
4557         struct {
4558                 __le32 size;
4559                 __le32 flags;
4560                 char name[16];
4561         } __packed segment_info;
4562
4563         err = scarlett2_usb(mixer, SCARLETT2_USB_INFO_FLASH,
4564                             NULL, 0,
4565                             &flash_info, sizeof(flash_info));
4566         if (err < 0)
4567                 return err;
4568
4569         count = le32_to_cpu(flash_info.count);
4570
4571         /* sanity check count */
4572         if (count < SCARLETT2_SEGMENT_NUM_MIN ||
4573             count > SCARLETT2_SEGMENT_NUM_MAX + 1) {
4574                 usb_audio_err(mixer->chip,
4575                               "invalid flash segment count: %d\n", count);
4576                 return -EINVAL;
4577         }
4578
4579         for (i = 0; i < count; i++) {
4580                 __le32 segment_num_req = cpu_to_le32(i);
4581                 int flash_segment_id;
4582
4583                 err = scarlett2_usb(mixer, SCARLETT2_USB_INFO_SEGMENT,
4584                                     &segment_num_req, sizeof(segment_num_req),
4585                                     &segment_info, sizeof(segment_info));
4586                 if (err < 0) {
4587                         usb_audio_err(mixer->chip,
4588                                 "failed to get flash segment info %d: %d\n",
4589                                 i, err);
4590                         return err;
4591                 }
4592
4593                 if (!strncmp(segment_info.name,
4594                              SCARLETT2_SEGMENT_SETTINGS_NAME, 16))
4595                         flash_segment_id = SCARLETT2_SEGMENT_ID_SETTINGS;
4596                 else if (!strncmp(segment_info.name,
4597                                   SCARLETT2_SEGMENT_FIRMWARE_NAME, 16))
4598                         flash_segment_id = SCARLETT2_SEGMENT_ID_FIRMWARE;
4599                 else
4600                         continue;
4601
4602                 private->flash_segment_nums[flash_segment_id] = i;
4603                 private->flash_segment_blocks[flash_segment_id] =
4604                         le32_to_cpu(segment_info.size) /
4605                                 SCARLETT2_FLASH_BLOCK_SIZE;
4606         }
4607
4608         /* segment 0 is App_Gold and we never want to touch that, so
4609          * use 0 as the "not-found" value
4610          */
4611         if (!private->flash_segment_nums[SCARLETT2_SEGMENT_ID_SETTINGS]) {
4612                 usb_audio_err(mixer->chip,
4613                               "failed to find flash segment %s\n",
4614                               SCARLETT2_SEGMENT_SETTINGS_NAME);
4615                 return -EINVAL;
4616         }
4617         if (!private->flash_segment_nums[SCARLETT2_SEGMENT_ID_FIRMWARE]) {
4618                 usb_audio_err(mixer->chip,
4619                               "failed to find flash segment %s\n",
4620                               SCARLETT2_SEGMENT_FIRMWARE_NAME);
4621                 return -EINVAL;
4622         }
4623
4624         return 0;
4625 }
4626
4627 /* Read configuration from the interface on start */
4628 static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
4629 {
4630         struct scarlett2_data *private = mixer->private_data;
4631         int err, i;
4632
4633         if (scarlett2_has_config_item(private, SCARLETT2_CONFIG_MSD_SWITCH)) {
4634                 err = scarlett2_usb_get_config(
4635                         mixer, SCARLETT2_CONFIG_MSD_SWITCH,
4636                         1, &private->msd_switch);
4637                 if (err < 0)
4638                         return err;
4639
4640                 /* no other controls are created if MSD mode is on */
4641                 if (private->msd_switch)
4642                         return 0;
4643         }
4644
4645         err = scarlett2_update_input_level(mixer);
4646         if (err < 0)
4647                 return err;
4648
4649         err = scarlett2_update_input_pad(mixer);
4650         if (err < 0)
4651                 return err;
4652
4653         err = scarlett2_update_input_air(mixer);
4654         if (err < 0)
4655                 return err;
4656
4657         err = scarlett2_update_input_phantom(mixer);
4658         if (err < 0)
4659                 return err;
4660
4661         err = scarlett2_update_direct_monitor(mixer);
4662         if (err < 0)
4663                 return err;
4664
4665         /* the rest of the configuration is for devices with a mixer */
4666         if (!scarlett2_has_mixer(private))
4667                 return 0;
4668
4669         err = scarlett2_update_monitor_other(mixer);
4670         if (err < 0)
4671                 return err;
4672
4673         if (scarlett2_has_config_item(private,
4674                                       SCARLETT2_CONFIG_STANDALONE_SWITCH)) {
4675                 err = scarlett2_usb_get_config(
4676                         mixer, SCARLETT2_CONFIG_STANDALONE_SWITCH,
4677                         1, &private->standalone_switch);
4678                 if (err < 0)
4679                         return err;
4680         }
4681
4682         err = scarlett2_update_sync(mixer);
4683         if (err < 0)
4684                 return err;
4685
4686         if (scarlett2_has_config_item(private,
4687                                       SCARLETT2_CONFIG_LINE_OUT_VOLUME)) {
4688                 s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
4689
4690                 /* read SW line out volume */
4691                 err = scarlett2_usb_get_config(
4692                         mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
4693                         private->num_line_out, &sw_vol);
4694                 if (err < 0)
4695                         return err;
4696
4697                 for (i = 0; i < private->num_line_out; i++)
4698                         private->vol[i] = clamp(
4699                                 sw_vol[i] + SCARLETT2_VOLUME_BIAS,
4700                                 0, SCARLETT2_VOLUME_BIAS);
4701
4702                 /* read SW mute */
4703                 err = scarlett2_usb_get_config(
4704                         mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
4705                         private->num_line_out, &private->mute_switch);
4706                 if (err < 0)
4707                         return err;
4708
4709                 for (i = 0; i < private->num_line_out; i++)
4710                         private->mute_switch[i] =
4711                                 !!private->mute_switch[i];
4712
4713                 /* read SW/HW switches */
4714                 if (scarlett2_has_config_item(private,
4715                                               SCARLETT2_CONFIG_SW_HW_SWITCH)) {
4716                         err = scarlett2_usb_get_config(
4717                                 mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
4718                                 private->num_line_out,
4719                                 &private->vol_sw_hw_switch);
4720                         if (err < 0)
4721                                 return err;
4722
4723                         for (i = 0; i < private->num_line_out; i++)
4724                                 private->vol_sw_hw_switch[i] =
4725                                         !!private->vol_sw_hw_switch[i];
4726                 }
4727         }
4728
4729         err = scarlett2_update_volumes(mixer);
4730         if (err < 0)
4731                 return err;
4732
4733         err = scarlett2_update_dim_mute(mixer);
4734         if (err < 0)
4735                 return err;
4736
4737         for (i = 0; i < private->num_mix_out; i++) {
4738                 err = scarlett2_usb_get_mix(mixer, i);
4739                 if (err < 0)
4740                         return err;
4741         }
4742
4743         return scarlett2_usb_get_mux(mixer);
4744 }
4745
4746 /* Notify on sync change */
4747 static void scarlett2_notify_sync(struct usb_mixer_interface *mixer)
4748 {
4749         struct scarlett2_data *private = mixer->private_data;
4750
4751         private->sync_updated = 1;
4752
4753         snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
4754                        &private->sync_ctl->id);
4755 }
4756
4757 /* Notify on monitor change */
4758 static void scarlett2_notify_monitor(struct usb_mixer_interface *mixer)
4759 {
4760         struct snd_card *card = mixer->chip->card;
4761         struct scarlett2_data *private = mixer->private_data;
4762         int i;
4763
4764         if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
4765                 return;
4766
4767         private->vol_updated = 1;
4768
4769         snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
4770                        &private->master_vol_ctl->id);
4771
4772         for (i = 0; i < private->num_line_out; i++)
4773                 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
4774                         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4775                                        &private->vol_ctls[i]->id);
4776 }
4777
4778 /* Notify on dim/mute change */
4779 static void scarlett2_notify_dim_mute(struct usb_mixer_interface *mixer)
4780 {
4781         struct snd_card *card = mixer->chip->card;
4782         struct scarlett2_data *private = mixer->private_data;
4783         int i;
4784
4785         if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
4786                 return;
4787
4788         private->dim_mute_updated = 1;
4789
4790         for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
4791                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4792                                &private->dim_mute_ctls[i]->id);
4793
4794         for (i = 0; i < private->num_line_out; i++)
4795                 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
4796                         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4797                                        &private->mute_ctls[i]->id);
4798 }
4799
4800 /* Notify on input level switch change */
4801 static void scarlett2_notify_input_level(struct usb_mixer_interface *mixer)
4802 {
4803         struct snd_card *card = mixer->chip->card;
4804         struct scarlett2_data *private = mixer->private_data;
4805         const struct scarlett2_device_info *info = private->info;
4806         int i;
4807
4808         private->input_level_updated = 1;
4809
4810         for (i = 0; i < info->level_input_count; i++)
4811                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4812                                &private->level_ctls[i]->id);
4813 }
4814
4815 /* Notify on input pad switch change */
4816 static void scarlett2_notify_input_pad(struct usb_mixer_interface *mixer)
4817 {
4818         struct snd_card *card = mixer->chip->card;
4819         struct scarlett2_data *private = mixer->private_data;
4820         const struct scarlett2_device_info *info = private->info;
4821         int i;
4822
4823         private->input_pad_updated = 1;
4824
4825         for (i = 0; i < info->pad_input_count; i++)
4826                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4827                                &private->pad_ctls[i]->id);
4828 }
4829
4830 /* Notify on input air switch change */
4831 static void scarlett2_notify_input_air(struct usb_mixer_interface *mixer)
4832 {
4833         struct snd_card *card = mixer->chip->card;
4834         struct scarlett2_data *private = mixer->private_data;
4835         const struct scarlett2_device_info *info = private->info;
4836         int i;
4837
4838         private->input_air_updated = 1;
4839
4840         for (i = 0; i < info->air_input_count; i++)
4841                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4842                                &private->air_ctls[i]->id);
4843 }
4844
4845 /* Notify on input phantom switch change */
4846 static void scarlett2_notify_input_phantom(struct usb_mixer_interface *mixer)
4847 {
4848         struct snd_card *card = mixer->chip->card;
4849         struct scarlett2_data *private = mixer->private_data;
4850         const struct scarlett2_device_info *info = private->info;
4851         int i;
4852
4853         private->input_phantom_updated = 1;
4854
4855         for (i = 0; i < info->phantom_count; i++)
4856                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4857                                &private->phantom_ctls[i]->id);
4858 }
4859
4860 /* Notify on "input other" change (level/pad/air/phantom) */
4861 static void scarlett2_notify_input_other(struct usb_mixer_interface *mixer)
4862 {
4863         scarlett2_notify_input_level(mixer);
4864         scarlett2_notify_input_pad(mixer);
4865         scarlett2_notify_input_air(mixer);
4866         scarlett2_notify_input_phantom(mixer);
4867 }
4868
4869 /* Notify on "monitor other" change (speaker switching, talkback) */
4870 static void scarlett2_notify_monitor_other(struct usb_mixer_interface *mixer)
4871 {
4872         struct snd_card *card = mixer->chip->card;
4873         struct scarlett2_data *private = mixer->private_data;
4874         const struct scarlett2_device_info *info = private->info;
4875
4876         private->monitor_other_updated = 1;
4877
4878         if (info->has_speaker_switching)
4879                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4880                                &private->speaker_switching_ctl->id);
4881
4882         if (info->has_talkback)
4883                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4884                                &private->talkback_ctl->id);
4885
4886         /* if speaker switching was recently enabled or disabled,
4887          * invalidate the dim/mute and mux enum controls
4888          */
4889         if (private->speaker_switching_switched) {
4890                 int i;
4891
4892                 scarlett2_notify_dim_mute(mixer);
4893
4894                 private->speaker_switching_switched = 0;
4895                 private->mux_updated = 1;
4896
4897                 for (i = 0; i < private->num_mux_dsts; i++)
4898                         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4899                                        &private->mux_ctls[i]->id);
4900         }
4901 }
4902
4903 /* Notify on direct monitor switch change */
4904 static void scarlett2_notify_direct_monitor(struct usb_mixer_interface *mixer)
4905 {
4906         struct snd_card *card = mixer->chip->card;
4907         struct scarlett2_data *private = mixer->private_data;
4908
4909         private->direct_monitor_updated = 1;
4910
4911         snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4912                        &private->direct_monitor_ctl->id);
4913 }
4914
4915 /* Interrupt callback */
4916 static void scarlett2_notify(struct urb *urb)
4917 {
4918         struct usb_mixer_interface *mixer = urb->context;
4919         int len = urb->actual_length;
4920         int ustatus = urb->status;
4921         u32 data;
4922         struct scarlett2_data *private = mixer->private_data;
4923         const struct scarlett2_notification *notifications =
4924                 private->config_set->notifications;
4925
4926         if (ustatus != 0 || len != 8)
4927                 goto requeue;
4928
4929         data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
4930
4931         while (data && notifications->mask) {
4932                 if (data & notifications->mask) {
4933                         data &= ~notifications->mask;
4934                         if (notifications->func)
4935                                 notifications->func(mixer);
4936                 }
4937                 notifications++;
4938         }
4939
4940         if (data)
4941                 usb_audio_warn(mixer->chip,
4942                                "%s: Unhandled notification: 0x%08x\n",
4943                                __func__, data);
4944
4945 requeue:
4946         if (ustatus != -ENOENT &&
4947             ustatus != -ECONNRESET &&
4948             ustatus != -ESHUTDOWN) {
4949                 urb->dev = mixer->chip->dev;
4950                 usb_submit_urb(urb, GFP_ATOMIC);
4951         }
4952 }
4953
4954 static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
4955 {
4956         struct usb_device *dev = mixer->chip->dev;
4957         struct scarlett2_data *private = mixer->private_data;
4958         unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
4959         void *transfer_buffer;
4960
4961         if (mixer->urb) {
4962                 usb_audio_err(mixer->chip,
4963                               "%s: mixer urb already in use!\n", __func__);
4964                 return 0;
4965         }
4966
4967         if (usb_pipe_type_check(dev, pipe))
4968                 return -EINVAL;
4969
4970         mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
4971         if (!mixer->urb)
4972                 return -ENOMEM;
4973
4974         transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
4975         if (!transfer_buffer)
4976                 return -ENOMEM;
4977
4978         usb_fill_int_urb(mixer->urb, dev, pipe,
4979                          transfer_buffer, private->wMaxPacketSize,
4980                          scarlett2_notify, mixer, private->bInterval);
4981
4982         return usb_submit_urb(mixer->urb, GFP_KERNEL);
4983 }
4984
4985 static const struct scarlett2_device_entry *get_scarlett2_device_entry(
4986         struct usb_mixer_interface *mixer)
4987 {
4988         const struct scarlett2_device_entry *entry = scarlett2_devices;
4989
4990         /* Find entry in scarlett2_devices */
4991         while (entry->usb_id && entry->usb_id != mixer->chip->usb_id)
4992                 entry++;
4993         if (!entry->usb_id)
4994                 return NULL;
4995
4996         return entry;
4997 }
4998
4999 static int snd_scarlett2_controls_create(
5000         struct usb_mixer_interface *mixer,
5001         const struct scarlett2_device_entry *entry)
5002 {
5003         int err;
5004
5005         /* Initialise private data */
5006         err = scarlett2_init_private(mixer, entry);
5007         if (err < 0)
5008                 return err;
5009
5010         /* Send proprietary USB initialisation sequence */
5011         err = scarlett2_usb_init(mixer);
5012         if (err < 0)
5013                 return err;
5014
5015         /* Get the upgrade & settings flash segment numbers */
5016         err = scarlett2_get_flash_segment_nums(mixer);
5017         if (err < 0)
5018                 return err;
5019
5020         /* Add firmware version control */
5021         err = scarlett2_add_firmware_version_ctl(mixer);
5022         if (err < 0)
5023                 return err;
5024
5025         /* Read volume levels and controls from the interface */
5026         err = scarlett2_read_configs(mixer);
5027         if (err < 0)
5028                 return err;
5029
5030         /* Create the MSD control */
5031         err = scarlett2_add_msd_ctl(mixer);
5032         if (err < 0)
5033                 return err;
5034
5035         /* If MSD mode is enabled, don't create any other controls */
5036         if (((struct scarlett2_data *)mixer->private_data)->msd_switch)
5037                 return 0;
5038
5039         /* Create the analogue output controls */
5040         err = scarlett2_add_line_out_ctls(mixer);
5041         if (err < 0)
5042                 return err;
5043
5044         /* Create the analogue input controls */
5045         err = scarlett2_add_line_in_ctls(mixer);
5046         if (err < 0)
5047                 return err;
5048
5049         /* Create the input, output, and mixer mux input selections */
5050         err = scarlett2_add_mux_enums(mixer);
5051         if (err < 0)
5052                 return err;
5053
5054         /* Create the matrix mixer controls */
5055         err = scarlett2_add_mixer_ctls(mixer);
5056         if (err < 0)
5057                 return err;
5058
5059         /* Create the level meter controls */
5060         err = scarlett2_add_meter_ctl(mixer);
5061         if (err < 0)
5062                 return err;
5063
5064         /* Create the sync control */
5065         err = scarlett2_add_sync_ctl(mixer);
5066         if (err < 0)
5067                 return err;
5068
5069         /* Create the direct monitor control */
5070         err = scarlett2_add_direct_monitor_ctl(mixer);
5071         if (err < 0)
5072                 return err;
5073
5074         /* Create the speaker switching control */
5075         err = scarlett2_add_speaker_switch_ctl(mixer);
5076         if (err < 0)
5077                 return err;
5078
5079         /* Create the talkback controls */
5080         err = scarlett2_add_talkback_ctls(mixer);
5081         if (err < 0)
5082                 return err;
5083
5084         /* Create the standalone control */
5085         err = scarlett2_add_standalone_ctl(mixer);
5086         if (err < 0)
5087                 return err;
5088
5089         /* Set up the interrupt polling */
5090         err = scarlett2_init_notify(mixer);
5091         if (err < 0)
5092                 return err;
5093
5094         return 0;
5095 }
5096
5097 /*** hwdep interface ***/
5098
5099 /* Set private->hwdep_in_use; prevents access to the ALSA controls
5100  * while doing a config erase/firmware upgrade.
5101  */
5102 static void scarlett2_lock(struct scarlett2_data *private)
5103 {
5104         mutex_lock(&private->data_mutex);
5105         private->hwdep_in_use = 1;
5106         mutex_unlock(&private->data_mutex);
5107 }
5108
5109 /* Call SCARLETT2_USB_GET_ERASE to get the erase progress */
5110 static int scarlett2_get_erase_progress(struct usb_mixer_interface *mixer)
5111 {
5112         struct scarlett2_data *private = mixer->private_data;
5113         int segment_id, segment_num, err;
5114         u8 erase_resp;
5115
5116         struct {
5117                 __le32 segment_num;
5118                 __le32 pad;
5119         } __packed erase_req;
5120
5121         segment_id = private->selected_flash_segment_id;
5122         segment_num = private->flash_segment_nums[segment_id];
5123
5124         if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
5125             segment_num > SCARLETT2_SEGMENT_NUM_MAX)
5126                 return -EFAULT;
5127
5128         /* Send the erase progress request */
5129         erase_req.segment_num = cpu_to_le32(segment_num);
5130         erase_req.pad = 0;
5131
5132         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_ERASE,
5133                             &erase_req, sizeof(erase_req),
5134                             &erase_resp, sizeof(erase_resp));
5135         if (err < 0)
5136                 return err;
5137
5138         return erase_resp;
5139 }
5140
5141 /* Repeatedly call scarlett2_get_erase_progress() until it returns
5142  * 0xff (erase complete) or we've waited 10 seconds (it usually takes
5143  * <3 seconds).
5144  */
5145 static int scarlett2_wait_for_erase(struct usb_mixer_interface *mixer)
5146 {
5147         int i, err;
5148
5149         for (i = 0; i < 100; i++) {
5150                 err = scarlett2_get_erase_progress(mixer);
5151                 if (err < 0)
5152                         return err;
5153
5154                 if (err == 0xff)
5155                         return 0;
5156
5157                 msleep(100);
5158         }
5159
5160         return -ETIMEDOUT;
5161 }
5162
5163 /* Reboot the device; wait for the erase to complete if one is in
5164  * progress.
5165  */
5166 static int scarlett2_reboot(struct usb_mixer_interface *mixer)
5167 {
5168         struct scarlett2_data *private = mixer->private_data;
5169
5170         if (private->flash_write_state ==
5171               SCARLETT2_FLASH_WRITE_STATE_ERASING) {
5172                 int err = scarlett2_wait_for_erase(mixer);
5173
5174                 if (err < 0)
5175                         return err;
5176         }
5177
5178         return scarlett2_usb(mixer, SCARLETT2_USB_REBOOT, NULL, 0, NULL, 0);
5179 }
5180
5181 /* Select a flash segment for erasing (and possibly writing to) */
5182 static int scarlett2_ioctl_select_flash_segment(
5183         struct usb_mixer_interface *mixer,
5184         unsigned long arg)
5185 {
5186         struct scarlett2_data *private = mixer->private_data;
5187         int segment_id, segment_num;
5188
5189         if (get_user(segment_id, (int __user *)arg))
5190                 return -EFAULT;
5191
5192         /* Check the segment ID and segment number */
5193         if (segment_id < 0 || segment_id >= SCARLETT2_SEGMENT_ID_COUNT)
5194                 return -EINVAL;
5195
5196         segment_num = private->flash_segment_nums[segment_id];
5197         if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
5198             segment_num > SCARLETT2_SEGMENT_NUM_MAX) {
5199                 usb_audio_err(mixer->chip,
5200                               "%s: invalid segment number %d\n",
5201                               __func__, segment_id);
5202                 return -EFAULT;
5203         }
5204
5205         /* If erasing, wait for it to complete */
5206         if (private->flash_write_state == SCARLETT2_FLASH_WRITE_STATE_ERASING) {
5207                 int err = scarlett2_wait_for_erase(mixer);
5208
5209                 if (err < 0)
5210                         return err;
5211         }
5212
5213         /* Save the selected segment ID and set the state to SELECTED */
5214         private->selected_flash_segment_id = segment_id;
5215         private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_SELECTED;
5216
5217         return 0;
5218 }
5219
5220 /* Erase the previously-selected flash segment */
5221 static int scarlett2_ioctl_erase_flash_segment(
5222         struct usb_mixer_interface *mixer)
5223 {
5224         struct scarlett2_data *private = mixer->private_data;
5225         int segment_id, segment_num, err;
5226
5227         struct {
5228                 __le32 segment_num;
5229                 __le32 pad;
5230         } __packed erase_req;
5231
5232         if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_SELECTED)
5233                 return -EINVAL;
5234
5235         segment_id = private->selected_flash_segment_id;
5236         segment_num = private->flash_segment_nums[segment_id];
5237
5238         if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
5239             segment_num > SCARLETT2_SEGMENT_NUM_MAX)
5240                 return -EFAULT;
5241
5242         /* Prevent access to ALSA controls that access the device from
5243          * here on
5244          */
5245         scarlett2_lock(private);
5246
5247         /* Send the erase request */
5248         erase_req.segment_num = cpu_to_le32(segment_num);
5249         erase_req.pad = 0;
5250
5251         err = scarlett2_usb(mixer, SCARLETT2_USB_ERASE_SEGMENT,
5252                             &erase_req, sizeof(erase_req),
5253                             NULL, 0);
5254         if (err < 0)
5255                 return err;
5256
5257         /* On success, change the state from SELECTED to ERASING */
5258         private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_ERASING;
5259
5260         return 0;
5261 }
5262
5263 /* Get the erase progress from the device */
5264 static int scarlett2_ioctl_get_erase_progress(
5265         struct usb_mixer_interface *mixer,
5266         unsigned long arg)
5267 {
5268         struct scarlett2_data *private = mixer->private_data;
5269         struct scarlett2_flash_segment_erase_progress progress;
5270         int segment_id, segment_num, err;
5271         u8 erase_resp;
5272
5273         struct {
5274                 __le32 segment_num;
5275                 __le32 pad;
5276         } __packed erase_req;
5277
5278         /* Check that we're erasing */
5279         if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_ERASING)
5280                 return -EINVAL;
5281
5282         segment_id = private->selected_flash_segment_id;
5283         segment_num = private->flash_segment_nums[segment_id];
5284
5285         if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
5286             segment_num > SCARLETT2_SEGMENT_NUM_MAX)
5287                 return -EFAULT;
5288
5289         /* Send the erase progress request */
5290         erase_req.segment_num = cpu_to_le32(segment_num);
5291         erase_req.pad = 0;
5292
5293         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_ERASE,
5294                             &erase_req, sizeof(erase_req),
5295                             &erase_resp, sizeof(erase_resp));
5296         if (err < 0)
5297                 return err;
5298
5299         progress.progress = erase_resp;
5300         progress.num_blocks = private->flash_segment_blocks[segment_id];
5301
5302         if (copy_to_user((void __user *)arg, &progress, sizeof(progress)))
5303                 return -EFAULT;
5304
5305         /* If the erase is complete, change the state from ERASING to
5306          * WRITE.
5307          */
5308         if (progress.progress == 0xff)
5309                 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_WRITE;
5310
5311         return 0;
5312 }
5313
5314 static int scarlett2_hwdep_open(struct snd_hwdep *hw, struct file *file)
5315 {
5316         struct usb_mixer_interface *mixer = hw->private_data;
5317         struct scarlett2_data *private = mixer->private_data;
5318
5319         /* If erasing, wait for it to complete */
5320         if (private->flash_write_state ==
5321               SCARLETT2_FLASH_WRITE_STATE_ERASING) {
5322                 int err = scarlett2_wait_for_erase(mixer);
5323
5324                 if (err < 0)
5325                         return err;
5326         }
5327
5328         /* Set the state to IDLE */
5329         private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_IDLE;
5330
5331         return 0;
5332 }
5333
5334 static int scarlett2_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
5335                                  unsigned int cmd, unsigned long arg)
5336 {
5337         struct usb_mixer_interface *mixer = hw->private_data;
5338
5339         switch (cmd) {
5340
5341         case SCARLETT2_IOCTL_PVERSION:
5342                 return put_user(SCARLETT2_HWDEP_VERSION,
5343                                 (int __user *)arg) ? -EFAULT : 0;
5344
5345         case SCARLETT2_IOCTL_REBOOT:
5346                 return scarlett2_reboot(mixer);
5347
5348         case SCARLETT2_IOCTL_SELECT_FLASH_SEGMENT:
5349                 return scarlett2_ioctl_select_flash_segment(mixer, arg);
5350
5351         case SCARLETT2_IOCTL_ERASE_FLASH_SEGMENT:
5352                 return scarlett2_ioctl_erase_flash_segment(mixer);
5353
5354         case SCARLETT2_IOCTL_GET_ERASE_PROGRESS:
5355                 return scarlett2_ioctl_get_erase_progress(mixer, arg);
5356
5357         default:
5358                 return -ENOIOCTLCMD;
5359         }
5360 }
5361
5362 static long scarlett2_hwdep_write(struct snd_hwdep *hw,
5363                                   const char __user *buf,
5364                                   long count, loff_t *offset)
5365 {
5366         struct usb_mixer_interface *mixer = hw->private_data;
5367         struct scarlett2_data *private = mixer->private_data;
5368         int segment_id, segment_num, err, len;
5369         int flash_size;
5370
5371         /* SCARLETT2_USB_WRITE_SEGMENT request data */
5372         struct {
5373                 __le32 segment_num;
5374                 __le32 offset;
5375                 __le32 pad;
5376                 u8 data[];
5377         } __packed *req;
5378
5379         /* Calculate the maximum permitted in data[] */
5380         const size_t max_data_size = SCARLETT2_FLASH_WRITE_MAX -
5381                                      offsetof(typeof(*req), data);
5382
5383         /* If erasing, wait for it to complete */
5384         if (private->flash_write_state ==
5385               SCARLETT2_FLASH_WRITE_STATE_ERASING) {
5386                 err = scarlett2_wait_for_erase(mixer);
5387                 if (err < 0)
5388                         return err;
5389                 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_WRITE;
5390
5391         /* Check that an erase has been done & completed */
5392         } else if (private->flash_write_state !=
5393                      SCARLETT2_FLASH_WRITE_STATE_WRITE) {
5394                 return -EINVAL;
5395         }
5396
5397         /* Check that we're writing to the upgrade firmware */
5398         segment_id = private->selected_flash_segment_id;
5399         if (segment_id != SCARLETT2_SEGMENT_ID_FIRMWARE)
5400                 return -EINVAL;
5401
5402         segment_num = private->flash_segment_nums[segment_id];
5403         if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
5404             segment_num > SCARLETT2_SEGMENT_NUM_MAX)
5405                 return -EFAULT;
5406
5407         /* Validate the offset and count */
5408         flash_size = private->flash_segment_blocks[segment_id] *
5409                      SCARLETT2_FLASH_BLOCK_SIZE;
5410
5411         if (count < 0 || *offset < 0 || *offset + count >= flash_size)
5412                 return -EINVAL;
5413
5414         if (!count)
5415                 return 0;
5416
5417         /* Limit the *req size to SCARLETT2_FLASH_WRITE_MAX */
5418         if (count > max_data_size)
5419                 count = max_data_size;
5420
5421         /* Create and send the request */
5422         len = struct_size(req, data, count);
5423         req = kzalloc(len, GFP_KERNEL);
5424         if (!req)
5425                 return -ENOMEM;
5426
5427         req->segment_num = cpu_to_le32(segment_num);
5428         req->offset = cpu_to_le32(*offset);
5429         req->pad = 0;
5430
5431         if (copy_from_user(req->data, buf, count)) {
5432                 err = -EFAULT;
5433                 goto error;
5434         }
5435
5436         err = scarlett2_usb(mixer, SCARLETT2_USB_WRITE_SEGMENT,
5437                             req, len, NULL, 0);
5438         if (err < 0)
5439                 goto error;
5440
5441         *offset += count;
5442         err = count;
5443
5444 error:
5445         kfree(req);
5446         return err;
5447 }
5448
5449 static int scarlett2_hwdep_release(struct snd_hwdep *hw, struct file *file)
5450 {
5451         struct usb_mixer_interface *mixer = hw->private_data;
5452         struct scarlett2_data *private = mixer->private_data;
5453
5454         /* Return from the SELECTED or WRITE state to IDLE.
5455          * The ERASING state is left as-is, and checked on next open.
5456          */
5457         if (private &&
5458             private->hwdep_in_use &&
5459             private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_ERASING)
5460                 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_IDLE;
5461
5462         return 0;
5463 }
5464
5465 static int scarlett2_hwdep_init(struct usb_mixer_interface *mixer)
5466 {
5467         struct snd_hwdep *hw;
5468         int err;
5469
5470         err = snd_hwdep_new(mixer->chip->card, "Focusrite Control", 0, &hw);
5471         if (err < 0)
5472                 return err;
5473
5474         hw->private_data = mixer;
5475         hw->exclusive = 1;
5476         hw->ops.open = scarlett2_hwdep_open;
5477         hw->ops.ioctl = scarlett2_hwdep_ioctl;
5478         hw->ops.write = scarlett2_hwdep_write;
5479         hw->ops.release = scarlett2_hwdep_release;
5480
5481         return 0;
5482 }
5483
5484 int snd_scarlett2_init(struct usb_mixer_interface *mixer)
5485 {
5486         struct snd_usb_audio *chip = mixer->chip;
5487         const struct scarlett2_device_entry *entry;
5488         int err;
5489
5490         /* only use UAC_VERSION_2 */
5491         if (!mixer->protocol)
5492                 return 0;
5493
5494         /* find entry in scarlett2_devices */
5495         entry = get_scarlett2_device_entry(mixer);
5496         if (!entry) {
5497                 usb_audio_err(mixer->chip,
5498                               "%s: missing device entry for %04x:%04x\n",
5499                               __func__,
5500                               USB_ID_VENDOR(chip->usb_id),
5501                               USB_ID_PRODUCT(chip->usb_id));
5502                 return 0;
5503         }
5504
5505         if (chip->setup & SCARLETT2_DISABLE) {
5506                 usb_audio_info(chip,
5507                         "Focusrite %s Mixer Driver disabled "
5508                         "by modprobe options (snd_usb_audio "
5509                         "vid=0x%04x pid=0x%04x device_setup=%d)\n",
5510                         entry->series_name,
5511                         USB_ID_VENDOR(chip->usb_id),
5512                         USB_ID_PRODUCT(chip->usb_id),
5513                         SCARLETT2_DISABLE);
5514                 return 0;
5515         }
5516
5517         usb_audio_info(chip,
5518                 "Focusrite %s Mixer Driver enabled (pid=0x%04x); "
5519                 "report any issues to "
5520                 "https://github.com/geoffreybennett/scarlett-gen2/issues",
5521                 entry->series_name,
5522                 USB_ID_PRODUCT(chip->usb_id));
5523
5524         err = snd_scarlett2_controls_create(mixer, entry);
5525         if (err < 0) {
5526                 usb_audio_err(mixer->chip,
5527                               "Error initialising %s Mixer Driver: %d",
5528                               entry->series_name,
5529                               err);
5530                 return err;
5531         }
5532
5533         err = scarlett2_hwdep_init(mixer);
5534         if (err < 0)
5535                 usb_audio_err(mixer->chip,
5536                               "Error creating %s hwdep device: %d",
5537                               entry->series_name,
5538                               err);
5539
5540         return err;
5541 }