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