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