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