ALSA: usb-audio: scarlett2: Coding style improvements
[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 /* Hardware buttons on the 18i20 */
198 #define SCARLETT2_BUTTON_MAX 2
199
200 static const char *const scarlett2_button_names[SCARLETT2_BUTTON_MAX] = {
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 button_count; /* number of buttons */
222         u8 level_input_count; /* inputs with level selectable */
223         u8 pad_input_count; /* inputs with pad selectable */
224         const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
225         struct scarlett2_ports ports[SCARLETT2_PORT_TYPE_COUNT];
226 };
227
228 struct scarlett2_mixer_data {
229         struct usb_mixer_interface *mixer;
230         struct mutex usb_mutex; /* prevent sending concurrent USB requests */
231         struct mutex data_mutex; /* lock access to this data */
232         struct delayed_work work;
233         const struct scarlett2_device_info *info;
234         int num_mux_srcs;
235         int num_mux_dsts;
236         u16 scarlett2_seq;
237         u8 vol_updated;
238         u8 master_vol;
239         u8 vol[SCARLETT2_ANALOGUE_MAX];
240         u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
241         u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
242         u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
243         u8 buttons[SCARLETT2_BUTTON_MAX];
244         struct snd_kcontrol *master_vol_ctl;
245         struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
246         struct snd_kcontrol *button_ctls[SCARLETT2_BUTTON_MAX];
247         u8 mux[SCARLETT2_MUX_MAX];
248         u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX];
249 };
250
251 /*** Model-specific data ***/
252
253 static const struct scarlett2_device_info s6i6_gen2_info = {
254         /* The first two analogue inputs can be switched between line
255          * and instrument levels.
256          */
257         .level_input_count = 2,
258
259         /* The first two analogue inputs have an optional pad. */
260         .pad_input_count = 2,
261
262         .line_out_descrs = {
263                 "Monitor L",
264                 "Monitor R",
265                 "Headphones L",
266                 "Headphones R",
267         },
268
269         .ports = {
270                 [SCARLETT2_PORT_TYPE_NONE] = {
271                         .id = 0x000,
272                         .num = { 1, 0, 8, 8, 8 },
273                         .src_descr = "Off",
274                         .src_num_offset = 0,
275                 },
276                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
277                         .id = 0x080,
278                         .num = { 4, 4, 4, 4, 4 },
279                         .src_descr = "Analogue %d",
280                         .src_num_offset = 1,
281                         .dst_descr = "Analogue Output %02d Playback"
282                 },
283                 [SCARLETT2_PORT_TYPE_SPDIF] = {
284                         .id = 0x180,
285                         .num = { 2, 2, 2, 2, 2 },
286                         .src_descr = "S/PDIF %d",
287                         .src_num_offset = 1,
288                         .dst_descr = "S/PDIF Output %d Playback"
289                 },
290                 [SCARLETT2_PORT_TYPE_MIX] = {
291                         .id = 0x300,
292                         .num = { 10, 18, 18, 18, 18 },
293                         .src_descr = "Mix %c",
294                         .src_num_offset = 65,
295                         .dst_descr = "Mixer Input %02d Capture"
296                 },
297                 [SCARLETT2_PORT_TYPE_PCM] = {
298                         .id = 0x600,
299                         .num = { 6, 6, 6, 6, 6 },
300                         .src_descr = "PCM %d",
301                         .src_num_offset = 1,
302                         .dst_descr = "PCM %02d Capture"
303                 },
304         },
305 };
306
307 static const struct scarlett2_device_info s18i8_gen2_info = {
308         /* The first two analogue inputs can be switched between line
309          * and instrument levels.
310          */
311         .level_input_count = 2,
312
313         /* The first four analogue inputs have an optional pad. */
314         .pad_input_count = 4,
315
316         .line_out_descrs = {
317                 "Monitor L",
318                 "Monitor R",
319                 "Headphones 1 L",
320                 "Headphones 1 R",
321                 "Headphones 2 L",
322                 "Headphones 2 R",
323         },
324
325         .ports = {
326                 [SCARLETT2_PORT_TYPE_NONE] = {
327                         .id = 0x000,
328                         .num = { 1, 0, 8, 8, 4 },
329                         .src_descr = "Off",
330                         .src_num_offset = 0,
331                 },
332                 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
333                         .id = 0x080,
334                         .num = { 8, 6, 6, 6, 6 },
335                         .src_descr = "Analogue %d",
336                         .src_num_offset = 1,
337                         .dst_descr = "Analogue Output %02d Playback"
338                 },
339                 [SCARLETT2_PORT_TYPE_SPDIF] = {
340                         .id = 0x180,
341                         .num = { 2, 2, 2, 2, 2 },
342                         .src_descr = "S/PDIF %d",
343                         .src_num_offset = 1,
344                         .dst_descr = "S/PDIF Output %d Playback"
345                 },
346                 [SCARLETT2_PORT_TYPE_ADAT] = {
347                         .id = 0x200,
348                         .num = { 8, 0, 0, 0, 0 },
349                         .src_descr = "ADAT %d",
350                         .src_num_offset = 1,
351                 },
352                 [SCARLETT2_PORT_TYPE_MIX] = {
353                         .id = 0x300,
354                         .num = { 10, 18, 18, 18, 18 },
355                         .src_descr = "Mix %c",
356                         .src_num_offset = 65,
357                         .dst_descr = "Mixer Input %02d Capture"
358                 },
359                 [SCARLETT2_PORT_TYPE_PCM] = {
360                         .id = 0x600,
361                         .num = { 8, 18, 18, 14, 10 },
362                         .src_descr = "PCM %d",
363                         .src_num_offset = 1,
364                         .dst_descr = "PCM %02d Capture"
365                 },
366         },
367 };
368
369 static const struct scarlett2_device_info s18i20_gen2_info = {
370         /* The analogue line outputs on the 18i20 can be switched
371          * between software and hardware volume control
372          */
373         .line_out_hw_vol = 1,
374
375         /* Mute and dim buttons */
376         .button_count = 2,
377
378         .line_out_descrs = {
379                 "Monitor L",
380                 "Monitor R",
381                 NULL,
382                 NULL,
383                 NULL,
384                 NULL,
385                 "Headphones 1 L",
386                 "Headphones 1 R",
387                 "Headphones 2 L",
388                 "Headphones 2 R",
389         },
390
391         .ports = {
392                 [SCARLETT2_PORT_TYPE_NONE] = {
393                         .id = 0x000,
394                         .num = { 1, 0, 8, 8, 6 },
395                         .src_descr = "Off",
396                         .src_num_offset = 0,
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 /* Vendor-Specific Interface, Endpoint, MaxPacketSize, Interval */
455 #define SCARLETT2_USB_VENDOR_SPECIFIC_INTERFACE 5
456 #define SCARLETT2_USB_INTERRUPT_ENDPOINT 4
457 #define SCARLETT2_USB_INTERRUPT_MAX_DATA 64
458 #define SCARLETT2_USB_INTERRUPT_INTERVAL 3
459
460 /* Interrupt flags for volume and mute/dim button changes */
461 #define SCARLETT2_USB_INTERRUPT_VOL_CHANGE    0x00400000
462 #define SCARLETT2_USB_INTERRUPT_BUTTON_CHANGE 0x00200000
463
464 /* Commands for sending/receiving requests/responses */
465 #define SCARLETT2_USB_VENDOR_SPECIFIC_CMD_REQ  2
466 #define SCARLETT2_USB_VENDOR_SPECIFIC_CMD_RESP 3
467
468 #define SCARLETT2_USB_INIT_SEQ  0x00000000
469 #define SCARLETT2_USB_GET_METER 0x00001001
470 #define SCARLETT2_USB_GET_MIX   0x00002001
471 #define SCARLETT2_USB_SET_MIX   0x00002002
472 #define SCARLETT2_USB_GET_MUX   0x00003001
473 #define SCARLETT2_USB_SET_MUX   0x00003002
474 #define SCARLETT2_USB_GET_DATA  0x00800000
475 #define SCARLETT2_USB_SET_DATA  0x00800001
476 #define SCARLETT2_USB_DATA_CMD  0x00800002
477
478 #define SCARLETT2_USB_CONFIG_SAVE 6
479
480 #define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31
481 #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
482
483 /* volume status is read together (matches scarlett2_config_items[]) */
484 struct scarlett2_usb_volume_status {
485         /* mute & dim buttons */
486         u8 buttons[SCARLETT2_BUTTON_MAX];
487
488         u8 pad1;
489
490         /* software volume setting */
491         s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
492
493         /* actual volume of output inc. dim (-18dB) */
494         s16 hw_vol[SCARLETT2_ANALOGUE_MAX];
495
496         u8 pad2[SCARLETT2_ANALOGUE_MAX];
497
498         /* sw (0) or hw (1) controlled */
499         u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
500
501         u8 pad3[6];
502
503         /* front panel volume knob */
504         s16 master_vol;
505 } __packed;
506
507 /* Configuration parameters that can be read and written */
508 enum {
509         SCARLETT2_CONFIG_BUTTONS = 0,
510         SCARLETT2_CONFIG_LINE_OUT_VOLUME = 1,
511         SCARLETT2_CONFIG_SW_HW_SWITCH = 2,
512         SCARLETT2_CONFIG_LEVEL_SWITCH = 3,
513         SCARLETT2_CONFIG_PAD_SWITCH = 4,
514         SCARLETT2_CONFIG_COUNT = 5
515 };
516
517 /* Location, size, and activation command number for the configuration
518  * parameters
519  */
520 struct scarlett2_config {
521         u8 offset;
522         u8 size;
523         u8 activate;
524 };
525
526 static const struct scarlett2_config
527                 scarlett2_config_items[SCARLETT2_CONFIG_COUNT] = {
528         /* Mute/Dim Buttons */
529         {
530                 .offset = 0x31,
531                 .size = 1,
532                 .activate = 2
533         },
534
535         /* Line Out Volume */
536         {
537                 .offset = 0x34,
538                 .size = 2,
539                 .activate = 1
540         },
541
542         /* SW/HW Volume Switch */
543         {
544                 .offset = 0x66,
545                 .size = 1,
546                 .activate = 3
547         },
548
549         /* Level Switch */
550         {
551                 .offset = 0x7c,
552                 .size = 1,
553                 .activate = 7
554         },
555
556         /* Pad Switch */
557         {
558                 .offset = 0x84,
559                 .size = 1,
560                 .activate = 8
561         }
562 };
563
564 /* proprietary request/response format */
565 struct scarlett2_usb_packet {
566         __le32 cmd;
567         __le16 size;
568         __le16 seq;
569         __le32 error;
570         __le32 pad;
571         u8 data[];
572 };
573
574 #define SCARLETT2_USB_PACKET_LEN (sizeof(struct scarlett2_usb_packet))
575
576 static void scarlett2_fill_request_header(struct scarlett2_mixer_data *private,
577                                           struct scarlett2_usb_packet *req,
578                                           u32 cmd, u16 req_size)
579 {
580         /* sequence must go up by 1 for each request */
581         u16 seq = private->scarlett2_seq++;
582
583         req->cmd = cpu_to_le32(cmd);
584         req->size = cpu_to_le16(req_size);
585         req->seq = cpu_to_le16(seq);
586         req->error = 0;
587         req->pad = 0;
588 }
589
590 /* Send a proprietary format request to the Scarlett interface */
591 static int scarlett2_usb(
592         struct usb_mixer_interface *mixer, u32 cmd,
593         void *req_data, u16 req_size, void *resp_data, u16 resp_size)
594 {
595         struct scarlett2_mixer_data *private = mixer->private_data;
596         u16 req_buf_size = sizeof(struct scarlett2_usb_packet) + req_size;
597         u16 resp_buf_size = sizeof(struct scarlett2_usb_packet) + resp_size;
598         struct scarlett2_usb_packet *req = NULL, *resp = NULL;
599         int err = 0;
600
601         req = kmalloc(req_buf_size, GFP_KERNEL);
602         if (!req) {
603                 err = -ENOMEM;
604                 goto error;
605         }
606
607         resp = kmalloc(resp_buf_size, GFP_KERNEL);
608         if (!resp) {
609                 err = -ENOMEM;
610                 goto error;
611         }
612
613         mutex_lock(&private->usb_mutex);
614
615         /* build request message and send it */
616
617         scarlett2_fill_request_header(private, req, cmd, req_size);
618
619         if (req_size)
620                 memcpy(req->data, req_data, req_size);
621
622         err = snd_usb_ctl_msg(mixer->chip->dev,
623                         usb_sndctrlpipe(mixer->chip->dev, 0),
624                         SCARLETT2_USB_VENDOR_SPECIFIC_CMD_REQ,
625                         USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
626                         0,
627                         SCARLETT2_USB_VENDOR_SPECIFIC_INTERFACE,
628                         req,
629                         req_buf_size);
630
631         if (err != req_buf_size) {
632                 usb_audio_err(
633                         mixer->chip,
634                         "Scarlett Gen 2 USB request result cmd %x was %d\n",
635                         cmd, err);
636                 err = -EINVAL;
637                 goto unlock;
638         }
639
640         /* send a second message to get the response */
641
642         err = snd_usb_ctl_msg(mixer->chip->dev,
643                         usb_rcvctrlpipe(mixer->chip->dev, 0),
644                         SCARLETT2_USB_VENDOR_SPECIFIC_CMD_RESP,
645                         USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
646                         0,
647                         SCARLETT2_USB_VENDOR_SPECIFIC_INTERFACE,
648                         resp,
649                         resp_buf_size);
650
651         /* validate the response */
652
653         if (err != resp_buf_size) {
654                 usb_audio_err(
655                         mixer->chip,
656                         "Scarlett Gen 2 USB response result cmd %x was %d\n",
657                         cmd, err);
658                 err = -EINVAL;
659                 goto unlock;
660         }
661
662         if (resp->cmd != req->cmd ||
663             resp->seq != req->seq ||
664             resp_size != le16_to_cpu(resp->size) ||
665             resp->error ||
666             resp->pad) {
667                 usb_audio_err(
668                         mixer->chip,
669                         "Scarlett Gen 2 USB invalid response; "
670                            "cmd tx/rx %d/%d seq %d/%d size %d/%d "
671                            "error %d pad %d\n",
672                         le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
673                         le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
674                         resp_size, le16_to_cpu(resp->size),
675                         le32_to_cpu(resp->error),
676                         le32_to_cpu(resp->pad));
677                 err = -EINVAL;
678                 goto unlock;
679         }
680
681         if (resp_size > 0)
682                 memcpy(resp_data, resp->data, resp_size);
683
684 unlock:
685         mutex_unlock(&private->usb_mutex);
686 error:
687         kfree(req);
688         kfree(resp);
689         return err;
690 }
691
692 /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
693 static void scarlett2_config_save(struct usb_mixer_interface *mixer)
694 {
695         __le32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE);
696
697         scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
698                       &req, sizeof(u32),
699                       NULL, 0);
700 }
701
702 /* Delayed work to save config */
703 static void scarlett2_config_save_work(struct work_struct *work)
704 {
705         struct scarlett2_mixer_data *private =
706                 container_of(work, struct scarlett2_mixer_data, work.work);
707
708         scarlett2_config_save(private->mixer);
709 }
710
711 /* Send a USB message to set a configuration parameter (volume level,
712  * sw/hw volume switch, line/inst level switch, or pad switch)
713  */
714 static int scarlett2_usb_set_config(
715         struct usb_mixer_interface *mixer,
716         int config_item_num, int index, int value)
717 {
718         const struct scarlett2_config config_item =
719                scarlett2_config_items[config_item_num];
720         struct {
721                 __le32 offset;
722                 __le32 bytes;
723                 __le32 value;
724         } __packed req;
725         __le32 req2;
726         int err;
727         struct scarlett2_mixer_data *private = mixer->private_data;
728
729         /* Cancel any pending NVRAM save */
730         cancel_delayed_work_sync(&private->work);
731
732         /* Send the configuration parameter data */
733         req.offset = cpu_to_le32(config_item.offset + index * config_item.size);
734         req.bytes = cpu_to_le32(config_item.size);
735         req.value = cpu_to_le32(value);
736         err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA,
737                             &req, sizeof(u32) * 2 + config_item.size,
738                             NULL, 0);
739         if (err < 0)
740                 return err;
741
742         /* Activate the change */
743         req2 = cpu_to_le32(config_item.activate);
744         err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
745                             &req2, sizeof(req2), NULL, 0);
746         if (err < 0)
747                 return err;
748
749         /* Schedule the change to be written to NVRAM */
750         schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
751
752         return 0;
753 }
754
755 /* Send a USB message to get data; result placed in *buf */
756 static int scarlett2_usb_get(
757         struct usb_mixer_interface *mixer,
758         int offset, void *buf, int size)
759 {
760         struct {
761                 __le32 offset;
762                 __le32 size;
763         } __packed req;
764
765         req.offset = cpu_to_le32(offset);
766         req.size = cpu_to_le32(size);
767         return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
768                              &req, sizeof(req), buf, size);
769 }
770
771 /* Send a USB message to get configuration parameters; result placed in *buf */
772 static int scarlett2_usb_get_config(
773         struct usb_mixer_interface *mixer,
774         int config_item_num, int count, void *buf)
775 {
776         const struct scarlett2_config config_item =
777                scarlett2_config_items[config_item_num];
778         int size = config_item.size * count;
779
780         return scarlett2_usb_get(mixer, config_item.offset, buf, size);
781 }
782
783 /* Send a USB message to get volume status; result placed in *buf */
784 static int scarlett2_usb_get_volume_status(
785         struct usb_mixer_interface *mixer,
786         struct scarlett2_usb_volume_status *buf)
787 {
788         return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET,
789                                  buf, sizeof(*buf));
790 }
791
792 /* Send a USB message to get the volumes for all inputs of one mix
793  * and put the values into private->mix[]
794  */
795 static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
796                                  int mix_num)
797 {
798         struct scarlett2_mixer_data *private = mixer->private_data;
799         const struct scarlett2_device_info *info = private->info;
800
801         int num_mixer_in =
802                 info->ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
803         int err, i, j, k;
804
805         struct {
806                 __le16 mix_num;
807                 __le16 count;
808         } __packed req;
809
810         __le16 data[SCARLETT2_INPUT_MIX_MAX];
811
812         req.mix_num = cpu_to_le16(mix_num);
813         req.count = cpu_to_le16(num_mixer_in);
814
815         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
816                             &req, sizeof(req),
817                             data, num_mixer_in * sizeof(u16));
818         if (err < 0)
819                 return err;
820
821         for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++) {
822                 u16 mixer_value = le16_to_cpu(data[i]);
823
824                 for (k = 0; k < SCARLETT2_MIXER_VALUE_COUNT; k++)
825                         if (scarlett2_mixer_values[k] >= mixer_value)
826                                 break;
827                 if (k == SCARLETT2_MIXER_VALUE_COUNT)
828                         k = SCARLETT2_MIXER_MAX_VALUE;
829                 private->mix[j] = k;
830         }
831
832         return 0;
833 }
834
835 /* Send a USB message to set the volumes for all inputs of one mix
836  * (values obtained from private->mix[])
837  */
838 static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
839                                  int mix_num)
840 {
841         struct scarlett2_mixer_data *private = mixer->private_data;
842         const struct scarlett2_device_info *info = private->info;
843
844         struct {
845                 __le16 mix_num;
846                 __le16 data[SCARLETT2_INPUT_MIX_MAX];
847         } __packed req;
848
849         int i, j;
850         int num_mixer_in =
851                 info->ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
852
853         req.mix_num = cpu_to_le16(mix_num);
854
855         for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
856                 req.data[i] = cpu_to_le16(
857                         scarlett2_mixer_values[private->mix[j]]
858                 );
859
860         return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
861                              &req, (num_mixer_in + 1) * sizeof(u16),
862                              NULL, 0);
863 }
864
865 /* Convert a port number index (per info->ports) to a hardware ID */
866 static u32 scarlett2_mux_src_num_to_id(const struct scarlett2_ports *ports,
867                                        int num)
868 {
869         int port_type;
870
871         for (port_type = 0;
872              port_type < SCARLETT2_PORT_TYPE_COUNT;
873              port_type++) {
874                 if (num < ports[port_type].num[SCARLETT2_PORT_IN])
875                         return ports[port_type].id | num;
876                 num -= ports[port_type].num[SCARLETT2_PORT_IN];
877         }
878
879         /* Oops */
880         return 0;
881 }
882
883 /* Convert a hardware ID to a port number index */
884 static u32 scarlett2_mux_id_to_num(const struct scarlett2_ports *ports,
885                                    int direction,
886                                    u32 id)
887 {
888         int port_type;
889         int port_num = 0;
890
891         for (port_type = 0;
892              port_type < SCARLETT2_PORT_TYPE_COUNT;
893              port_type++) {
894                 struct scarlett2_ports port = ports[port_type];
895                 int count = port.num[direction];
896
897                 if (id >= port.id && id < port.id + count)
898                         return port_num + id - port.id;
899                 port_num += count;
900         }
901
902         /* Oops */
903         return -1;
904 }
905
906 /* Convert one mux entry from the interface and load into private->mux[] */
907 static void scarlett2_usb_populate_mux(struct scarlett2_mixer_data *private,
908                                        u32 mux_entry)
909 {
910         const struct scarlett2_device_info *info = private->info;
911         const struct scarlett2_ports *ports = info->ports;
912
913         int dst_idx, src_idx;
914
915         dst_idx = scarlett2_mux_id_to_num(ports, SCARLETT2_PORT_OUT,
916                                           mux_entry & 0xFFF);
917         if (dst_idx < 0)
918                 return;
919
920         if (dst_idx >= private->num_mux_dsts) {
921                 usb_audio_err(private->mixer->chip,
922                         "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
923                         mux_entry, dst_idx, private->num_mux_dsts);
924                 return;
925         }
926
927         src_idx = scarlett2_mux_id_to_num(ports, SCARLETT2_PORT_IN,
928                                           mux_entry >> 12);
929         if (src_idx < 0)
930                 return;
931
932         if (src_idx >= private->num_mux_srcs) {
933                 usb_audio_err(private->mixer->chip,
934                         "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
935                         mux_entry, src_idx, private->num_mux_srcs);
936                 return;
937         }
938
939         private->mux[dst_idx] = src_idx;
940 }
941
942 /* Send USB message to get mux inputs and then populate private->mux[] */
943 static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
944 {
945         struct scarlett2_mixer_data *private = mixer->private_data;
946         int count = private->num_mux_dsts;
947         int err, i;
948
949         struct {
950                 __le16 num;
951                 __le16 count;
952         } __packed req;
953
954         __le32 data[SCARLETT2_MUX_MAX];
955
956         req.num = 0;
957         req.count = cpu_to_le16(count);
958
959         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
960                             &req, sizeof(req),
961                             data, count * sizeof(u32));
962         if (err < 0)
963                 return err;
964
965         for (i = 0; i < count; i++)
966                 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
967
968         return 0;
969 }
970
971 /* Send USB messages to set mux inputs */
972 static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
973 {
974         struct scarlett2_mixer_data *private = mixer->private_data;
975         const struct scarlett2_device_info *info = private->info;
976         const struct scarlett2_ports *ports = info->ports;
977         int rate, port_dir_rate;
978
979         static const int assignment_order[SCARLETT2_PORT_TYPE_COUNT] = {
980                 SCARLETT2_PORT_TYPE_PCM,
981                 SCARLETT2_PORT_TYPE_ANALOGUE,
982                 SCARLETT2_PORT_TYPE_SPDIF,
983                 SCARLETT2_PORT_TYPE_ADAT,
984                 SCARLETT2_PORT_TYPE_MIX,
985                 SCARLETT2_PORT_TYPE_NONE,
986         };
987
988         struct {
989                 __le16 pad;
990                 __le16 num;
991                 __le32 data[SCARLETT2_MUX_MAX];
992         } __packed req;
993
994         req.pad = 0;
995
996         /* mux settings for each rate */
997         for (rate = 0, port_dir_rate = SCARLETT2_PORT_OUT_44;
998              port_dir_rate <= SCARLETT2_PORT_OUT_176;
999              rate++, port_dir_rate++) {
1000                 int order_num, i, err;
1001
1002                 req.num = cpu_to_le16(rate);
1003
1004                 for (order_num = 0, i = 0;
1005                      order_num < SCARLETT2_PORT_TYPE_COUNT;
1006                      order_num++) {
1007                         int port_type = assignment_order[order_num];
1008                         int j = scarlett2_get_port_start_num(ports,
1009                                                              SCARLETT2_PORT_OUT,
1010                                                              port_type);
1011                         int port_id = ports[port_type].id;
1012                         int channel;
1013
1014                         for (channel = 0;
1015                              channel < ports[port_type].num[port_dir_rate];
1016                              channel++, i++, j++)
1017                                 /* lower 12 bits for the destination and
1018                                  * next 12 bits for the source
1019                                  */
1020                                 req.data[i] = !port_id
1021                                         ? 0
1022                                         : cpu_to_le32(
1023                                                 port_id |
1024                                                 channel |
1025                                                 scarlett2_mux_src_num_to_id(
1026                                                         ports, private->mux[j]
1027                                                 ) << 12
1028                                           );
1029
1030                         /* skip private->mux[j] entries not output */
1031                         j += ports[port_type].num[SCARLETT2_PORT_OUT] -
1032                              ports[port_type].num[port_dir_rate];
1033                 }
1034
1035                 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
1036                                     &req, (i + 1) * sizeof(u32),
1037                                     NULL, 0);
1038                 if (err < 0)
1039                         return err;
1040         }
1041
1042         return 0;
1043 }
1044
1045 /* Send USB message to get meter levels */
1046 static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
1047                                           u16 *levels)
1048 {
1049         struct {
1050                 __le16 pad;
1051                 __le16 num_meters;
1052                 __le32 magic;
1053         } __packed req;
1054         u32 resp[SCARLETT2_NUM_METERS];
1055         int i, err;
1056
1057         req.pad = 0;
1058         req.num_meters = cpu_to_le16(SCARLETT2_NUM_METERS);
1059         req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
1060         err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
1061                             &req, sizeof(req), resp, sizeof(resp));
1062         if (err < 0)
1063                 return err;
1064
1065         /* copy, convert to u16 */
1066         for (i = 0; i < SCARLETT2_NUM_METERS; i++)
1067                 levels[i] = resp[i];
1068
1069         return 0;
1070 }
1071
1072 /*** Control Functions ***/
1073
1074 /* helper function to create a new control */
1075 static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
1076                                  const struct snd_kcontrol_new *ncontrol,
1077                                  int index, int channels, const char *name,
1078                                  struct snd_kcontrol **kctl_return)
1079 {
1080         struct snd_kcontrol *kctl;
1081         struct usb_mixer_elem_info *elem;
1082         int err;
1083
1084         elem = kzalloc(sizeof(*elem), GFP_KERNEL);
1085         if (!elem)
1086                 return -ENOMEM;
1087
1088         elem->head.mixer = mixer;
1089         elem->control = index;
1090         elem->head.id = index;
1091         elem->channels = channels;
1092
1093         kctl = snd_ctl_new1(ncontrol, elem);
1094         if (!kctl) {
1095                 kfree(elem);
1096                 return -ENOMEM;
1097         }
1098         kctl->private_free = snd_usb_mixer_elem_free;
1099
1100         strscpy(kctl->id.name, name, sizeof(kctl->id.name));
1101
1102         err = snd_usb_mixer_add_control(&elem->head, kctl);
1103         if (err < 0)
1104                 return err;
1105
1106         if (kctl_return)
1107                 *kctl_return = kctl;
1108
1109         return 0;
1110 }
1111
1112 /*** Analogue Line Out Volume Controls ***/
1113
1114 /* Update hardware volume controls after receiving notification that
1115  * they have changed
1116  */
1117 static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
1118 {
1119         struct scarlett2_mixer_data *private = mixer->private_data;
1120         const struct scarlett2_ports *ports = private->info->ports;
1121         struct scarlett2_usb_volume_status volume_status;
1122         int num_line_out =
1123                 ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
1124         int err, i;
1125
1126         private->vol_updated = 0;
1127
1128         err = scarlett2_usb_get_volume_status(mixer, &volume_status);
1129         if (err < 0)
1130                 return err;
1131
1132         private->master_vol = clamp(
1133                 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
1134                 0, SCARLETT2_VOLUME_BIAS);
1135
1136         for (i = 0; i < num_line_out; i++) {
1137                 if (private->vol_sw_hw_switch[i])
1138                         private->vol[i] = private->master_vol;
1139         }
1140
1141         for (i = 0; i < private->info->button_count; i++)
1142                 private->buttons[i] = !!volume_status.buttons[i];
1143
1144         return 0;
1145 }
1146
1147 static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
1148                                      struct snd_ctl_elem_info *uinfo)
1149 {
1150         struct usb_mixer_elem_info *elem = kctl->private_data;
1151
1152         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1153         uinfo->count = elem->channels;
1154         uinfo->value.integer.min = 0;
1155         uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
1156         uinfo->value.integer.step = 1;
1157         return 0;
1158 }
1159
1160 static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
1161                                            struct snd_ctl_elem_value *ucontrol)
1162 {
1163         struct usb_mixer_elem_info *elem = kctl->private_data;
1164         struct usb_mixer_interface *mixer = elem->head.mixer;
1165         struct scarlett2_mixer_data *private = mixer->private_data;
1166
1167         if (private->vol_updated) {
1168                 mutex_lock(&private->data_mutex);
1169                 scarlett2_update_volumes(mixer);
1170                 mutex_unlock(&private->data_mutex);
1171         }
1172
1173         ucontrol->value.integer.value[0] = private->master_vol;
1174         return 0;
1175 }
1176
1177 static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
1178                                     struct snd_ctl_elem_value *ucontrol)
1179 {
1180         struct usb_mixer_elem_info *elem = kctl->private_data;
1181         struct usb_mixer_interface *mixer = elem->head.mixer;
1182         struct scarlett2_mixer_data *private = mixer->private_data;
1183         int index = elem->control;
1184
1185         if (private->vol_updated) {
1186                 mutex_lock(&private->data_mutex);
1187                 scarlett2_update_volumes(mixer);
1188                 mutex_unlock(&private->data_mutex);
1189         }
1190
1191         ucontrol->value.integer.value[0] = private->vol[index];
1192         return 0;
1193 }
1194
1195 static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
1196                                     struct snd_ctl_elem_value *ucontrol)
1197 {
1198         struct usb_mixer_elem_info *elem = kctl->private_data;
1199         struct usb_mixer_interface *mixer = elem->head.mixer;
1200         struct scarlett2_mixer_data *private = mixer->private_data;
1201         int index = elem->control;
1202         int oval, val, err = 0;
1203
1204         mutex_lock(&private->data_mutex);
1205
1206         oval = private->vol[index];
1207         val = ucontrol->value.integer.value[0];
1208
1209         if (oval == val)
1210                 goto unlock;
1211
1212         private->vol[index] = val;
1213         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1214                                        index, val - SCARLETT2_VOLUME_BIAS);
1215         if (err == 0)
1216                 err = 1;
1217
1218 unlock:
1219         mutex_unlock(&private->data_mutex);
1220         return err;
1221 }
1222
1223 static const DECLARE_TLV_DB_MINMAX(
1224         db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0
1225 );
1226
1227 static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
1228         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1229         .access = SNDRV_CTL_ELEM_ACCESS_READ |
1230                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1231         .name = "",
1232         .info = scarlett2_volume_ctl_info,
1233         .get  = scarlett2_master_volume_ctl_get,
1234         .private_value = 0, /* max value */
1235         .tlv = { .p = db_scale_scarlett2_gain }
1236 };
1237
1238 static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
1239         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1240         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1241                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1242         .name = "",
1243         .info = scarlett2_volume_ctl_info,
1244         .get  = scarlett2_volume_ctl_get,
1245         .put  = scarlett2_volume_ctl_put,
1246         .private_value = 0, /* max value */
1247         .tlv = { .p = db_scale_scarlett2_gain }
1248 };
1249
1250 /*** HW/SW Volume Switch Controls ***/
1251
1252 static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
1253                                          struct snd_ctl_elem_info *uinfo)
1254 {
1255         static const char *const values[2] = {
1256                 "SW", "HW"
1257         };
1258
1259         return snd_ctl_enum_info(uinfo, 1, 2, values);
1260 }
1261
1262 static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
1263                                         struct snd_ctl_elem_value *ucontrol)
1264 {
1265         struct usb_mixer_elem_info *elem = kctl->private_data;
1266         struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1267
1268         ucontrol->value.enumerated.item[0] =
1269                 private->vol_sw_hw_switch[elem->control];
1270         return 0;
1271 }
1272
1273 static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
1274                                         struct snd_ctl_elem_value *ucontrol)
1275 {
1276         struct usb_mixer_elem_info *elem = kctl->private_data;
1277         struct usb_mixer_interface *mixer = elem->head.mixer;
1278         struct scarlett2_mixer_data *private = mixer->private_data;
1279
1280         int index = elem->control;
1281         int oval, val, err = 0;
1282
1283         mutex_lock(&private->data_mutex);
1284
1285         oval = private->vol_sw_hw_switch[index];
1286         val = !!ucontrol->value.integer.value[0];
1287
1288         if (oval == val)
1289                 goto unlock;
1290
1291         private->vol_sw_hw_switch[index] = val;
1292
1293         /* Change access mode to RO (hardware controlled volume)
1294          * or RW (software controlled volume)
1295          */
1296         if (val)
1297                 private->vol_ctls[index]->vd[0].access &=
1298                         ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1299         else
1300                 private->vol_ctls[index]->vd[0].access |=
1301                         SNDRV_CTL_ELEM_ACCESS_WRITE;
1302
1303         /* Reset volume to master volume */
1304         private->vol[index] = private->master_vol;
1305
1306         /* Set SW volume to current HW volume */
1307         err = scarlett2_usb_set_config(
1308                 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1309                 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
1310         if (err < 0)
1311                 goto unlock;
1312
1313         /* Notify of RO/RW change */
1314         snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_INFO,
1315                        &private->vol_ctls[index]->id);
1316
1317         /* Send SW/HW switch change to the device */
1318         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
1319                                        index, val);
1320
1321 unlock:
1322         mutex_unlock(&private->data_mutex);
1323         return err;
1324 }
1325
1326 static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
1327         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1328         .name = "",
1329         .info = scarlett2_sw_hw_enum_ctl_info,
1330         .get  = scarlett2_sw_hw_enum_ctl_get,
1331         .put  = scarlett2_sw_hw_enum_ctl_put,
1332 };
1333
1334 /*** Line Level/Instrument Level Switch Controls ***/
1335
1336 static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
1337                                          struct snd_ctl_elem_info *uinfo)
1338 {
1339         static const char *const values[2] = {
1340                 "Line", "Inst"
1341         };
1342
1343         return snd_ctl_enum_info(uinfo, 1, 2, values);
1344 }
1345
1346 static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
1347                                         struct snd_ctl_elem_value *ucontrol)
1348 {
1349         struct usb_mixer_elem_info *elem = kctl->private_data;
1350         struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1351
1352         ucontrol->value.enumerated.item[0] =
1353                 private->level_switch[elem->control];
1354         return 0;
1355 }
1356
1357 static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
1358                                         struct snd_ctl_elem_value *ucontrol)
1359 {
1360         struct usb_mixer_elem_info *elem = kctl->private_data;
1361         struct usb_mixer_interface *mixer = elem->head.mixer;
1362         struct scarlett2_mixer_data *private = mixer->private_data;
1363
1364         int index = elem->control;
1365         int oval, val, err = 0;
1366
1367         mutex_lock(&private->data_mutex);
1368
1369         oval = private->level_switch[index];
1370         val = !!ucontrol->value.integer.value[0];
1371
1372         if (oval == val)
1373                 goto unlock;
1374
1375         private->level_switch[index] = val;
1376
1377         /* Send switch change to the device */
1378         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
1379                                        index, val);
1380
1381 unlock:
1382         mutex_unlock(&private->data_mutex);
1383         return err;
1384 }
1385
1386 static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
1387         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1388         .name = "",
1389         .info = scarlett2_level_enum_ctl_info,
1390         .get  = scarlett2_level_enum_ctl_get,
1391         .put  = scarlett2_level_enum_ctl_put,
1392 };
1393
1394 /*** Pad Switch Controls ***/
1395
1396 static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
1397                                  struct snd_ctl_elem_value *ucontrol)
1398 {
1399         struct usb_mixer_elem_info *elem = kctl->private_data;
1400         struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1401
1402         ucontrol->value.enumerated.item[0] =
1403                 private->pad_switch[elem->control];
1404         return 0;
1405 }
1406
1407 static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
1408                                  struct snd_ctl_elem_value *ucontrol)
1409 {
1410         struct usb_mixer_elem_info *elem = kctl->private_data;
1411         struct usb_mixer_interface *mixer = elem->head.mixer;
1412         struct scarlett2_mixer_data *private = mixer->private_data;
1413
1414         int index = elem->control;
1415         int oval, val, err = 0;
1416
1417         mutex_lock(&private->data_mutex);
1418
1419         oval = private->pad_switch[index];
1420         val = !!ucontrol->value.integer.value[0];
1421
1422         if (oval == val)
1423                 goto unlock;
1424
1425         private->pad_switch[index] = val;
1426
1427         /* Send switch change to the device */
1428         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
1429                                        index, val);
1430
1431 unlock:
1432         mutex_unlock(&private->data_mutex);
1433         return err;
1434 }
1435
1436 static const struct snd_kcontrol_new scarlett2_pad_ctl = {
1437         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1438         .name = "",
1439         .info = snd_ctl_boolean_mono_info,
1440         .get  = scarlett2_pad_ctl_get,
1441         .put  = scarlett2_pad_ctl_put,
1442 };
1443
1444 /*** Mute/Dim Controls ***/
1445
1446 static int scarlett2_button_ctl_get(struct snd_kcontrol *kctl,
1447                                     struct snd_ctl_elem_value *ucontrol)
1448 {
1449         struct usb_mixer_elem_info *elem = kctl->private_data;
1450         struct usb_mixer_interface *mixer = elem->head.mixer;
1451         struct scarlett2_mixer_data *private = mixer->private_data;
1452
1453         if (private->vol_updated) {
1454                 mutex_lock(&private->data_mutex);
1455                 scarlett2_update_volumes(mixer);
1456                 mutex_unlock(&private->data_mutex);
1457         }
1458
1459         ucontrol->value.enumerated.item[0] = private->buttons[elem->control];
1460         return 0;
1461 }
1462
1463 static int scarlett2_button_ctl_put(struct snd_kcontrol *kctl,
1464                                     struct snd_ctl_elem_value *ucontrol)
1465 {
1466         struct usb_mixer_elem_info *elem = kctl->private_data;
1467         struct usb_mixer_interface *mixer = elem->head.mixer;
1468         struct scarlett2_mixer_data *private = mixer->private_data;
1469
1470         int index = elem->control;
1471         int oval, val, err = 0;
1472
1473         mutex_lock(&private->data_mutex);
1474
1475         oval = private->buttons[index];
1476         val = !!ucontrol->value.integer.value[0];
1477
1478         if (oval == val)
1479                 goto unlock;
1480
1481         private->buttons[index] = val;
1482
1483         /* Send switch change to the device */
1484         err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_BUTTONS,
1485                                        index, val);
1486
1487 unlock:
1488         mutex_unlock(&private->data_mutex);
1489         return err;
1490 }
1491
1492 static const struct snd_kcontrol_new scarlett2_button_ctl = {
1493         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1494         .name = "",
1495         .info = snd_ctl_boolean_mono_info,
1496         .get  = scarlett2_button_ctl_get,
1497         .put  = scarlett2_button_ctl_put
1498 };
1499
1500 /*** Create the analogue output controls ***/
1501
1502 static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
1503 {
1504         struct scarlett2_mixer_data *private = mixer->private_data;
1505         const struct scarlett2_device_info *info = private->info;
1506         const struct scarlett2_ports *ports = info->ports;
1507         int num_line_out =
1508                 ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
1509         int err, i;
1510         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1511
1512         /* Add R/O HW volume control */
1513         if (info->line_out_hw_vol) {
1514                 snprintf(s, sizeof(s), "Master HW Playback Volume");
1515                 err = scarlett2_add_new_ctl(mixer,
1516                                             &scarlett2_master_volume_ctl,
1517                                             0, 1, s, &private->master_vol_ctl);
1518                 if (err < 0)
1519                         return err;
1520         }
1521
1522         /* Add volume controls */
1523         for (i = 0; i < num_line_out; i++) {
1524
1525                 /* Fader */
1526                 if (info->line_out_descrs[i])
1527                         snprintf(s, sizeof(s),
1528                                  "Line %02d (%s) Playback Volume",
1529                                  i + 1, info->line_out_descrs[i]);
1530                 else
1531                         snprintf(s, sizeof(s),
1532                                  "Line %02d Playback Volume",
1533                                  i + 1);
1534                 err = scarlett2_add_new_ctl(mixer,
1535                                             &scarlett2_line_out_volume_ctl,
1536                                             i, 1, s, &private->vol_ctls[i]);
1537                 if (err < 0)
1538                         return err;
1539
1540                 /* Make the fader read-only if the SW/HW switch is set to HW */
1541                 if (private->vol_sw_hw_switch[i])
1542                         private->vol_ctls[i]->vd[0].access &=
1543                                 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1544
1545                 /* SW/HW Switch */
1546                 if (info->line_out_hw_vol) {
1547                         snprintf(s, sizeof(s),
1548                                  "Line Out %02d Volume Control Playback Enum",
1549                                  i + 1);
1550                         err = scarlett2_add_new_ctl(mixer,
1551                                                     &scarlett2_sw_hw_enum_ctl,
1552                                                     i, 1, s, NULL);
1553                         if (err < 0)
1554                                 return err;
1555                 }
1556         }
1557
1558         /* Add HW button controls */
1559         for (i = 0; i < private->info->button_count; i++) {
1560                 err = scarlett2_add_new_ctl(mixer, &scarlett2_button_ctl,
1561                                             i, 1, scarlett2_button_names[i],
1562                                             &private->button_ctls[i]);
1563                 if (err < 0)
1564                         return err;
1565         }
1566
1567         return 0;
1568 }
1569
1570 /*** Create the analogue input controls ***/
1571
1572 static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
1573 {
1574         struct scarlett2_mixer_data *private = mixer->private_data;
1575         const struct scarlett2_device_info *info = private->info;
1576         int err, i;
1577         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1578
1579         /* Add input level (line/inst) controls */
1580         for (i = 0; i < info->level_input_count; i++) {
1581                 snprintf(s, sizeof(s), "Line In %d Level Capture Enum", i + 1);
1582                 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
1583                                             i, 1, s, NULL);
1584                 if (err < 0)
1585                         return err;
1586         }
1587
1588         /* Add input pad controls */
1589         for (i = 0; i < info->pad_input_count; i++) {
1590                 snprintf(s, sizeof(s), "Line In %d Pad Capture Switch", i + 1);
1591                 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
1592                                             i, 1, s, NULL);
1593                 if (err < 0)
1594                         return err;
1595         }
1596
1597         return 0;
1598 }
1599
1600 /*** Mixer Volume Controls ***/
1601
1602 static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
1603                                     struct snd_ctl_elem_info *uinfo)
1604 {
1605         struct usb_mixer_elem_info *elem = kctl->private_data;
1606
1607         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1608         uinfo->count = elem->channels;
1609         uinfo->value.integer.min = 0;
1610         uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
1611         uinfo->value.integer.step = 1;
1612         return 0;
1613 }
1614
1615 static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
1616                                    struct snd_ctl_elem_value *ucontrol)
1617 {
1618         struct usb_mixer_elem_info *elem = kctl->private_data;
1619         struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1620
1621         ucontrol->value.integer.value[0] = private->mix[elem->control];
1622         return 0;
1623 }
1624
1625 static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
1626                                    struct snd_ctl_elem_value *ucontrol)
1627 {
1628         struct usb_mixer_elem_info *elem = kctl->private_data;
1629         struct usb_mixer_interface *mixer = elem->head.mixer;
1630         struct scarlett2_mixer_data *private = mixer->private_data;
1631         const struct scarlett2_device_info *info = private->info;
1632         const struct scarlett2_ports *ports = info->ports;
1633         int oval, val, num_mixer_in, mix_num, err = 0;
1634
1635         mutex_lock(&private->data_mutex);
1636
1637         oval = private->mix[elem->control];
1638         val = ucontrol->value.integer.value[0];
1639         num_mixer_in = ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
1640         mix_num = elem->control / num_mixer_in;
1641
1642         if (oval == val)
1643                 goto unlock;
1644
1645         private->mix[elem->control] = val;
1646         err = scarlett2_usb_set_mix(mixer, mix_num);
1647         if (err == 0)
1648                 err = 1;
1649
1650 unlock:
1651         mutex_unlock(&private->data_mutex);
1652         return err;
1653 }
1654
1655 static const DECLARE_TLV_DB_MINMAX(
1656         db_scale_scarlett2_mixer,
1657         SCARLETT2_MIXER_MIN_DB * 100,
1658         SCARLETT2_MIXER_MAX_DB * 100
1659 );
1660
1661 static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
1662         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1663         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1664                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1665         .name = "",
1666         .info = scarlett2_mixer_ctl_info,
1667         .get  = scarlett2_mixer_ctl_get,
1668         .put  = scarlett2_mixer_ctl_put,
1669         .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
1670         .tlv = { .p = db_scale_scarlett2_mixer }
1671 };
1672
1673 static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
1674 {
1675         struct scarlett2_mixer_data *private = mixer->private_data;
1676         const struct scarlett2_ports *ports = private->info->ports;
1677         int err, i, j;
1678         int index;
1679         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1680
1681         int num_inputs = ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
1682         int num_outputs = ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_IN];
1683
1684         for (i = 0, index = 0; i < num_outputs; i++) {
1685                 for (j = 0; j < num_inputs; j++, index++) {
1686                         snprintf(s, sizeof(s),
1687                                  "Mix %c Input %02d Playback Volume",
1688                                  'A' + i, j + 1);
1689                         err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
1690                                                     index, 1, s, NULL);
1691                         if (err < 0)
1692                                 return err;
1693                 }
1694         }
1695
1696         return 0;
1697 }
1698
1699 /*** Mux Source Selection Controls ***/
1700
1701 static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
1702                                            struct snd_ctl_elem_info *uinfo)
1703 {
1704         struct usb_mixer_elem_info *elem = kctl->private_data;
1705         struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1706         const struct scarlett2_ports *ports = private->info->ports;
1707         unsigned int item = uinfo->value.enumerated.item;
1708         int items = private->num_mux_srcs;
1709         int port_type;
1710
1711         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1712         uinfo->count = elem->channels;
1713         uinfo->value.enumerated.items = items;
1714
1715         if (item >= items)
1716                 item = uinfo->value.enumerated.item = items - 1;
1717
1718         for (port_type = 0;
1719              port_type < SCARLETT2_PORT_TYPE_COUNT;
1720              port_type++) {
1721                 if (item < ports[port_type].num[SCARLETT2_PORT_IN]) {
1722                         sprintf(uinfo->value.enumerated.name,
1723                                 ports[port_type].src_descr,
1724                                 item + ports[port_type].src_num_offset);
1725                         return 0;
1726                 }
1727                 item -= ports[port_type].num[SCARLETT2_PORT_IN];
1728         }
1729
1730         return -EINVAL;
1731 }
1732
1733 static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
1734                                           struct snd_ctl_elem_value *ucontrol)
1735 {
1736         struct usb_mixer_elem_info *elem = kctl->private_data;
1737         struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1738
1739         ucontrol->value.enumerated.item[0] = private->mux[elem->control];
1740         return 0;
1741 }
1742
1743 static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
1744                                           struct snd_ctl_elem_value *ucontrol)
1745 {
1746         struct usb_mixer_elem_info *elem = kctl->private_data;
1747         struct usb_mixer_interface *mixer = elem->head.mixer;
1748         struct scarlett2_mixer_data *private = mixer->private_data;
1749         int index = elem->control;
1750         int oval, val, err = 0;
1751
1752         mutex_lock(&private->data_mutex);
1753
1754         oval = private->mux[index];
1755         val = clamp(ucontrol->value.integer.value[0],
1756                     0L, private->num_mux_srcs - 1L);
1757
1758         if (oval == val)
1759                 goto unlock;
1760
1761         private->mux[index] = val;
1762         err = scarlett2_usb_set_mux(mixer);
1763         if (err == 0)
1764                 err = 1;
1765
1766 unlock:
1767         mutex_unlock(&private->data_mutex);
1768         return err;
1769 }
1770
1771 static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
1772         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1773         .name = "",
1774         .info = scarlett2_mux_src_enum_ctl_info,
1775         .get  = scarlett2_mux_src_enum_ctl_get,
1776         .put  = scarlett2_mux_src_enum_ctl_put,
1777 };
1778
1779 static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
1780 {
1781         struct scarlett2_mixer_data *private = mixer->private_data;
1782         const struct scarlett2_ports *ports = private->info->ports;
1783         int port_type, channel, i;
1784
1785         for (i = 0, port_type = 0;
1786              port_type < SCARLETT2_PORT_TYPE_COUNT;
1787              port_type++) {
1788                 for (channel = 0;
1789                      channel < ports[port_type].num[SCARLETT2_PORT_OUT];
1790                      channel++, i++) {
1791                         int err;
1792                         char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1793                         const char *const descr = ports[port_type].dst_descr;
1794
1795                         snprintf(s, sizeof(s) - 5, descr, channel + 1);
1796                         strcat(s, " Enum");
1797
1798                         err = scarlett2_add_new_ctl(mixer,
1799                                                     &scarlett2_mux_src_enum_ctl,
1800                                                     i, 1, s, NULL);
1801                         if (err < 0)
1802                                 return err;
1803                 }
1804         }
1805
1806         return 0;
1807 }
1808
1809 /*** Meter Controls ***/
1810
1811 static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
1812                                     struct snd_ctl_elem_info *uinfo)
1813 {
1814         struct usb_mixer_elem_info *elem = kctl->private_data;
1815
1816         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1817         uinfo->count = elem->channels;
1818         uinfo->value.integer.min = 0;
1819         uinfo->value.integer.max = 4095;
1820         uinfo->value.integer.step = 1;
1821         return 0;
1822 }
1823
1824 static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
1825                                    struct snd_ctl_elem_value *ucontrol)
1826 {
1827         struct usb_mixer_elem_info *elem = kctl->private_data;
1828         u16 meter_levels[SCARLETT2_NUM_METERS];
1829         int i, err;
1830
1831         err = scarlett2_usb_get_meter_levels(elem->head.mixer, meter_levels);
1832         if (err < 0)
1833                 return err;
1834
1835         for (i = 0; i < elem->channels; i++)
1836                 ucontrol->value.integer.value[i] = meter_levels[i];
1837
1838         return 0;
1839 }
1840
1841 static const struct snd_kcontrol_new scarlett2_meter_ctl = {
1842         .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1843         .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1844         .name = "",
1845         .info = scarlett2_meter_ctl_info,
1846         .get  = scarlett2_meter_ctl_get
1847 };
1848
1849 static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
1850 {
1851         return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
1852                                      0, SCARLETT2_NUM_METERS,
1853                                      "Level Meter", NULL);
1854 }
1855
1856 /*** Cleanup/Suspend Callbacks ***/
1857
1858 static void scarlett2_private_free(struct usb_mixer_interface *mixer)
1859 {
1860         struct scarlett2_mixer_data *private = mixer->private_data;
1861
1862         cancel_delayed_work_sync(&private->work);
1863         kfree(private);
1864         mixer->private_data = NULL;
1865 }
1866
1867 static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
1868 {
1869         struct scarlett2_mixer_data *private = mixer->private_data;
1870
1871         if (cancel_delayed_work_sync(&private->work))
1872                 scarlett2_config_save(private->mixer);
1873 }
1874
1875 /*** Initialisation ***/
1876
1877 static void scarlett2_count_mux_io(struct scarlett2_mixer_data *private)
1878 {
1879         const struct scarlett2_ports *ports = private->info->ports;
1880         int port_type, srcs = 0, dsts = 0;
1881
1882         for (port_type = 0;
1883              port_type < SCARLETT2_PORT_TYPE_COUNT;
1884              port_type++) {
1885                 srcs += ports[port_type].num[SCARLETT2_PORT_IN];
1886                 dsts += ports[port_type].num[SCARLETT2_PORT_OUT_44];
1887         }
1888
1889         private->num_mux_srcs = srcs;
1890         private->num_mux_dsts = dsts;
1891 }
1892
1893 /* Initialise private data and sequence number */
1894 static int scarlett2_init_private(struct usb_mixer_interface *mixer,
1895                                   const struct scarlett2_device_info *info)
1896 {
1897         struct scarlett2_mixer_data *private =
1898                 kzalloc(sizeof(struct scarlett2_mixer_data), GFP_KERNEL);
1899
1900         if (!private)
1901                 return -ENOMEM;
1902
1903         mutex_init(&private->usb_mutex);
1904         mutex_init(&private->data_mutex);
1905         INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
1906         private->info = info;
1907         scarlett2_count_mux_io(private);
1908         private->scarlett2_seq = 0;
1909         private->mixer = mixer;
1910         mixer->private_data = private;
1911         mixer->private_free = scarlett2_private_free;
1912         mixer->private_suspend = scarlett2_private_suspend;
1913
1914         /* Initialise the sequence number used for the proprietary commands */
1915         return scarlett2_usb(mixer, SCARLETT2_USB_INIT_SEQ, NULL, 0, NULL, 0);
1916 }
1917
1918 /* Read configuration from the interface on start */
1919 static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
1920 {
1921         struct scarlett2_mixer_data *private = mixer->private_data;
1922         const struct scarlett2_device_info *info = private->info;
1923         const struct scarlett2_ports *ports = info->ports;
1924         int num_line_out =
1925                 ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
1926         int num_mixer_out =
1927                 ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_IN];
1928         u8 level_switches[SCARLETT2_LEVEL_SWITCH_MAX];
1929         u8 pad_switches[SCARLETT2_PAD_SWITCH_MAX];
1930         struct scarlett2_usb_volume_status volume_status;
1931         int err, i;
1932
1933         if (info->level_input_count) {
1934                 err = scarlett2_usb_get_config(
1935                         mixer,
1936                         SCARLETT2_CONFIG_LEVEL_SWITCH,
1937                         info->level_input_count,
1938                         level_switches);
1939                 if (err < 0)
1940                         return err;
1941                 for (i = 0; i < info->level_input_count; i++)
1942                         private->level_switch[i] = level_switches[i];
1943         }
1944
1945         if (info->pad_input_count) {
1946                 err = scarlett2_usb_get_config(
1947                         mixer,
1948                         SCARLETT2_CONFIG_PAD_SWITCH,
1949                         info->pad_input_count,
1950                         pad_switches);
1951                 if (err < 0)
1952                         return err;
1953                 for (i = 0; i < info->pad_input_count; i++)
1954                         private->pad_switch[i] = pad_switches[i];
1955         }
1956
1957         err = scarlett2_usb_get_volume_status(mixer, &volume_status);
1958         if (err < 0)
1959                 return err;
1960
1961         private->master_vol = clamp(
1962                 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
1963                 0, SCARLETT2_VOLUME_BIAS);
1964
1965         for (i = 0; i < num_line_out; i++) {
1966                 int volume;
1967
1968                 private->vol_sw_hw_switch[i] =
1969                         info->line_out_hw_vol
1970                                 && volume_status.sw_hw_switch[i];
1971
1972                 volume = private->vol_sw_hw_switch[i]
1973                            ? volume_status.master_vol
1974                            : volume_status.sw_vol[i];
1975                 volume = clamp(volume + SCARLETT2_VOLUME_BIAS,
1976                                0, SCARLETT2_VOLUME_BIAS);
1977                 private->vol[i] = volume;
1978         }
1979
1980         for (i = 0; i < info->button_count; i++)
1981                 private->buttons[i] = !!volume_status.buttons[i];
1982
1983         for (i = 0; i < num_mixer_out; i++) {
1984                 err = scarlett2_usb_get_mix(mixer, i);
1985                 if (err < 0)
1986                         return err;
1987         }
1988
1989         return scarlett2_usb_get_mux(mixer);
1990 }
1991
1992 /* Notify on volume change */
1993 static void scarlett2_mixer_interrupt_vol_change(
1994         struct usb_mixer_interface *mixer)
1995 {
1996         struct scarlett2_mixer_data *private = mixer->private_data;
1997         const struct scarlett2_ports *ports = private->info->ports;
1998         int num_line_out =
1999                 ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
2000         int i;
2001
2002         private->vol_updated = 1;
2003
2004         snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2005                        &private->master_vol_ctl->id);
2006
2007         for (i = 0; i < num_line_out; i++) {
2008                 if (!private->vol_sw_hw_switch[i])
2009                         continue;
2010                 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2011                                &private->vol_ctls[i]->id);
2012         }
2013 }
2014
2015 /* Notify on button change */
2016 static void scarlett2_mixer_interrupt_button_change(
2017         struct usb_mixer_interface *mixer)
2018 {
2019         struct scarlett2_mixer_data *private = mixer->private_data;
2020         int i;
2021
2022         private->vol_updated = 1;
2023
2024         for (i = 0; i < private->info->button_count; i++)
2025                 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2026                                &private->button_ctls[i]->id);
2027 }
2028
2029 /* Interrupt callback */
2030 static void scarlett2_mixer_interrupt(struct urb *urb)
2031 {
2032         struct usb_mixer_interface *mixer = urb->context;
2033         int len = urb->actual_length;
2034         int ustatus = urb->status;
2035         u32 data;
2036
2037         if (ustatus != 0)
2038                 goto requeue;
2039
2040         if (len == 8) {
2041                 data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
2042                 if (data & SCARLETT2_USB_INTERRUPT_VOL_CHANGE)
2043                         scarlett2_mixer_interrupt_vol_change(mixer);
2044                 if (data & SCARLETT2_USB_INTERRUPT_BUTTON_CHANGE)
2045                         scarlett2_mixer_interrupt_button_change(mixer);
2046         } else {
2047                 usb_audio_err(mixer->chip,
2048                               "scarlett mixer interrupt length %d\n", len);
2049         }
2050
2051 requeue:
2052         if (ustatus != -ENOENT &&
2053             ustatus != -ECONNRESET &&
2054             ustatus != -ESHUTDOWN) {
2055                 urb->dev = mixer->chip->dev;
2056                 usb_submit_urb(urb, GFP_ATOMIC);
2057         }
2058 }
2059
2060 static int scarlett2_mixer_status_create(struct usb_mixer_interface *mixer)
2061 {
2062         struct usb_device *dev = mixer->chip->dev;
2063         unsigned int pipe = usb_rcvintpipe(dev,
2064                                            SCARLETT2_USB_INTERRUPT_ENDPOINT);
2065         void *transfer_buffer;
2066
2067         if (mixer->urb) {
2068                 usb_audio_err(mixer->chip,
2069                               "%s: mixer urb already in use!\n", __func__);
2070                 return 0;
2071         }
2072
2073         if (usb_pipe_type_check(dev, pipe))
2074                 return -EINVAL;
2075
2076         mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
2077         if (!mixer->urb)
2078                 return -ENOMEM;
2079
2080         transfer_buffer = kmalloc(SCARLETT2_USB_INTERRUPT_MAX_DATA, GFP_KERNEL);
2081         if (!transfer_buffer)
2082                 return -ENOMEM;
2083
2084         usb_fill_int_urb(mixer->urb, dev, pipe,
2085                          transfer_buffer, SCARLETT2_USB_INTERRUPT_MAX_DATA,
2086                          scarlett2_mixer_interrupt, mixer,
2087                          SCARLETT2_USB_INTERRUPT_INTERVAL);
2088
2089         return usb_submit_urb(mixer->urb, GFP_KERNEL);
2090 }
2091
2092 static int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer,
2093                                              const struct scarlett2_device_info *info)
2094 {
2095         int err;
2096
2097         /* Initialise private data and sequence number */
2098         err = scarlett2_init_private(mixer, info);
2099         if (err < 0)
2100                 return err;
2101
2102         /* Read volume levels and controls from the interface */
2103         err = scarlett2_read_configs(mixer);
2104         if (err < 0)
2105                 return err;
2106
2107         /* Create the analogue output controls */
2108         err = scarlett2_add_line_out_ctls(mixer);
2109         if (err < 0)
2110                 return err;
2111
2112         /* Create the analogue input controls */
2113         err = scarlett2_add_line_in_ctls(mixer);
2114         if (err < 0)
2115                 return err;
2116
2117         /* Create the input, output, and mixer mux input selections */
2118         err = scarlett2_add_mux_enums(mixer);
2119         if (err < 0)
2120                 return err;
2121
2122         /* Create the matrix mixer controls */
2123         err = scarlett2_add_mixer_ctls(mixer);
2124         if (err < 0)
2125                 return err;
2126
2127         /* Create the level meter controls */
2128         err = scarlett2_add_meter_ctl(mixer);
2129         if (err < 0)
2130                 return err;
2131
2132         /* Set up the interrupt polling if there are hardware buttons */
2133         if (info->button_count) {
2134                 err = scarlett2_mixer_status_create(mixer);
2135                 if (err < 0)
2136                         return err;
2137         }
2138
2139         return 0;
2140 }
2141
2142 int snd_scarlett_gen2_init(struct usb_mixer_interface *mixer)
2143 {
2144         struct snd_usb_audio *chip = mixer->chip;
2145         const struct scarlett2_device_info *info;
2146         int err;
2147
2148         /* only use UAC_VERSION_2 */
2149         if (!mixer->protocol)
2150                 return 0;
2151
2152         switch (chip->usb_id) {
2153         case USB_ID(0x1235, 0x8203):
2154                 info = &s6i6_gen2_info;
2155                 break;
2156         case USB_ID(0x1235, 0x8204):
2157                 info = &s18i8_gen2_info;
2158                 break;
2159         case USB_ID(0x1235, 0x8201):
2160                 info = &s18i20_gen2_info;
2161                 break;
2162         default: /* device not (yet) supported */
2163                 return -EINVAL;
2164         }
2165
2166         if (!(chip->setup & SCARLETT2_ENABLE)) {
2167                 usb_audio_info(chip,
2168                         "Focusrite Scarlett Gen 2 Mixer Driver disabled; "
2169                         "use options snd_usb_audio vid=0x%04x pid=0x%04x "
2170                         "device_setup=1 to enable and report any issues "
2171                         "to g@b4.vu",
2172                         USB_ID_VENDOR(chip->usb_id),
2173                         USB_ID_PRODUCT(chip->usb_id));
2174                 return 0;
2175         }
2176
2177         usb_audio_info(chip,
2178                 "Focusrite Scarlett Gen 2 Mixer Driver enabled pid=0x%04x",
2179                 USB_ID_PRODUCT(chip->usb_id));
2180
2181         err = snd_scarlett_gen2_controls_create(mixer, info);
2182         if (err < 0)
2183                 usb_audio_err(mixer->chip,
2184                               "Error initialising Scarlett Mixer Driver: %d",
2185                               err);
2186
2187         return err;
2188 }