mixer_scarlett_gen2.c 108 KB
Newer Older
1
2
// SPDX-License-Identifier: GPL-2.0
/*
3
 *   Focusrite Scarlett Gen 2/3 Driver for ALSA
4
 *
5
6
 *   Supported models:
 *   - 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
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 *
 *   Based on the Scarlett (Gen 1) Driver for ALSA:
 *
 *   Copyright (c) 2013 by Tobias Hoffmann
 *   Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
 *   Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
 *   Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
 *
 *   Many codes borrowed from audio.c by
 *     Alan Cox (alan at lxorguk.ukuu.org.uk)
 *     Thomas Sailer (sailer at ife.ee.ethz.ch)
 *
 *   Code cleanup:
 *   David Henningsson <david.henningsson at canonical.com>
 */

/* The protocol was reverse engineered by looking at the communication
 * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20
 * (firmware 1083) using usbmon in July-August 2018.
 *
 * Scarlett 18i8 support added in April 2019.
 *
 * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
 * for providing usbmon output and testing).
 *
36
37
38
39
40
41
42
43
 * Scarlett 4i4/8i6 Gen 3 support added in May 2020 (thanks to Laurent
 * Debricon for donating a 4i4 and to Fredrik Unger for providing 8i6
 * usbmon output and testing).
 *
 * Scarlett 18i8/18i20 Gen 3 support added in June 2020 (thanks to
 * Darren Jaeckel, Alex Sedlack, and Clovis Lunel for providing usbmon
 * output, protocol traces and testing).
 *
44
45
46
47
 * Support for loading mixer volume and mux configuration from the
 * interface during driver initialisation added in May 2021 (thanks to
 * Vladimir Sadovnikov for figuring out how).
 *
48
49
50
 * Support for Solo/2i2 Gen 3 added in May 2021 (thanks to Alexander
 * Vorona for 2i2 protocol traces).
 *
51
52
 * Support for phantom power, direct monitoring, speaker switching,
 * 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
61
 *  - phantom power, direct monitor, speaker switching, and talkback
 *    controls
62
 *  - disable/enable MSD mode
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
 *
 * <ditaa>
 *    /--------------\    18chn            20chn     /--------------\
 *    | Hardware  in +--+------\    /-------------+--+ ALSA PCM out |
 *    \--------------/  |      |    |             |  \--------------/
 *                      |      |    |    /-----\  |
 *                      |      |    |    |     |  |
 *                      |      v    v    v     |  |
 *                      |   +---------------+  |  |
 *                      |    \ Matrix  Mux /   |  |
 *                      |     +-----+-----+    |  |
 *                      |           |          |  |
 *                      |           |18chn     |  |
 *                      |           |          |  |
 *                      |           |     10chn|  |
 *                      |           v          |  |
 *                      |     +------------+   |  |
 *                      |     | Mixer      |   |  |
 *                      |     |     Matrix |   |  |
 *                      |     |            |   |  |
 *                      |     | 18x10 Gain |   |  |
 *                      |     |   stages   |   |  |
 *                      |     +-----+------+   |  |
 *                      |           |          |  |
 *                      |18chn      |10chn     |  |20chn
 *                      |           |          |  |
 *                      |           +----------/  |
 *                      |           |             |
 *                      v           v             v
 *                      ===========================
 *               +---------------+       +--—------------+
 *                \ Output  Mux /         \ Capture Mux /
 *                 +---+---+---+           +-----+-----+
 *                     |   |                     |
 *                10chn|   |                     |18chn
 *                     |   |                     |
 *  /--------------\   |   |                     |   /--------------\
 *  | S/PDIF, ADAT |<--/   |10chn                \-->| ALSA PCM in  |
 *  | Hardware out |       |                         \--------------/
 *  \--------------/       |
 *                         v
 *                  +-------------+    Software gain per channel.
 *                  | Master Gain |<-- 18i20 only: Switch per channel
 *                  +------+------+    to select HW or SW gain control.
 *                         |
 *                         |10chn
 *  /--------------\       |
 *  | Analogue     |<------/
 *  | Hardware out |
 *  \--------------/
 * </ditaa>
 *
115
116
117
118
119
120
121
122
 * Gen 3 devices have a Mass Storage Device (MSD) mode where a small
 * disk with registration and driver download information is presented
 * to the host. To access the full functionality of the device without
 * proprietary software, MSD mode can be disabled by:
 * - holding down the 48V button for five seconds while powering on
 *   the device, or
 * - using this driver and alsamixer to change the "MSD Mode" setting
 *   to Off and power-cycling the device
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
 */

#include <linux/slab.h>
#include <linux/usb.h>
#include <linux/moduleparam.h>

#include <sound/control.h>
#include <sound/tlv.h>

#include "usbaudio.h"
#include "mixer.h"
#include "helper.h"

#include "mixer_scarlett_gen2.h"

/* device_setup value to enable */
#define SCARLETT2_ENABLE 0x01

141
142
143
/* device_setup value to allow turning MSD mode back on */
#define SCARLETT2_MSD_ENABLE 0x02

144
145
146
147
148
149
150
151
152
/* some gui mixers can't handle negative ctl values */
#define SCARLETT2_VOLUME_BIAS 127

/* mixer range from -80dB to +6dB in 0.5dB steps */
#define SCARLETT2_MIXER_MIN_DB -80
#define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
#define SCARLETT2_MIXER_MAX_DB 6
#define SCARLETT2_MIXER_MAX_VALUE \
	((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
153
#define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1)
154
155
156
157

/* map from (dB + 80) * 2 to mixer value
 * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
 */
158
static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = {
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
	0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
	2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
	9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
	23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
	54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
	122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
	244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
	487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
	973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
	1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
	3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
	5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
	9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
	16345
};

/* Maximum number of analogue outputs */
#define SCARLETT2_ANALOGUE_MAX 10

/* Maximum number of level and pad switches */
#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
199
200
201
202
203
204
205
206

/* Hardware port types:
 * - None (no input to mux)
 * - Analogue I/O
 * - S/PDIF I/O
 * - ADAT I/O
 * - Mixer I/O
 * - PCM I/O
 */
enum {
207
	SCARLETT2_PORT_TYPE_NONE     = 0,
208
	SCARLETT2_PORT_TYPE_ANALOGUE = 1,
209
210
211
212
213
	SCARLETT2_PORT_TYPE_SPDIF    = 2,
	SCARLETT2_PORT_TYPE_ADAT     = 3,
	SCARLETT2_PORT_TYPE_MIX      = 4,
	SCARLETT2_PORT_TYPE_PCM      = 5,
	SCARLETT2_PORT_TYPE_COUNT    = 6,
214
215
};

216
/* I/O count of each port type kept in struct scarlett2_ports */
217
enum {
218
219
220
	SCARLETT2_PORT_IN    = 0,
	SCARLETT2_PORT_OUT   = 1,
	SCARLETT2_PORT_DIRNS = 2,
221
222
};

223
/* Dim/Mute buttons on the 18i20 */
224
225
226
227
228
enum {
	SCARLETT2_BUTTON_MUTE    = 0,
	SCARLETT2_BUTTON_DIM     = 1,
	SCARLETT2_DIM_MUTE_COUNT = 2,
};
229

230
static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
231
232
233
234
	"Mute", "Dim"
};

/* Description of each hardware port type:
235
 * - id: hardware ID of this port type
236
237
238
239
 * - src_descr: printf format string for mux input selections
 * - src_num_offset: added to channel number for the fprintf
 * - dst_descr: printf format string for mixer controls
 */
240
struct scarlett2_port {
241
242
243
244
245
246
	u16 id;
	const char * const src_descr;
	int src_num_offset;
	const char * const dst_descr;
};

247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
	[SCARLETT2_PORT_TYPE_NONE] = {
		.id = 0x000,
		.src_descr = "Off"
	},
	[SCARLETT2_PORT_TYPE_ANALOGUE] = {
		.id = 0x080,
		.src_descr = "Analogue %d",
		.src_num_offset = 1,
		.dst_descr = "Analogue Output %02d Playback"
	},
	[SCARLETT2_PORT_TYPE_SPDIF] = {
		.id = 0x180,
		.src_descr = "S/PDIF %d",
		.src_num_offset = 1,
		.dst_descr = "S/PDIF Output %d Playback"
	},
	[SCARLETT2_PORT_TYPE_ADAT] = {
		.id = 0x200,
		.src_descr = "ADAT %d",
		.src_num_offset = 1,
		.dst_descr = "ADAT Output %d Playback"
	},
	[SCARLETT2_PORT_TYPE_MIX] = {
		.id = 0x300,
		.src_descr = "Mix %c",
		.src_num_offset = 'A',
		.dst_descr = "Mixer Input %02d Capture"
	},
	[SCARLETT2_PORT_TYPE_PCM] = {
		.id = 0x600,
		.src_descr = "PCM %d",
		.src_num_offset = 1,
		.dst_descr = "PCM %02d Capture"
	},
};

284
285
286
287
288
289
/* Number of mux tables: one for each band of sample rates
 * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
 */
#define SCARLETT2_MUX_TABLES 3

/* Maximum number of entries in a mux table */
290
#define SCARLETT2_MAX_MUX_ENTRIES 10
291
292
293
294
295
296
297
298
299
300
301

/* One entry within mux_assignment defines the port type and range of
 * ports to add to the set_mux message. The end of the list is marked
 * with count == 0.
 */
struct scarlett2_mux_entry {
	u8 port_type;
	u8 start;
	u8 count;
};

302
struct scarlett2_device_info {
303
	u32 usb_id; /* USB device identifier */
304

305
306
307
308
309
310
	/* Gen 3 devices have an internal MSD mode switch that needs
	 * to be disabled in order to access the full functionality of
	 * the device.
	 */
	u8 has_msd_mode;

311
312
313
314
315
	/* Gen 3 devices without a mixer have a different
	 * configuration set
	 */
	u8 has_mixer;

316
317
318
	/* line out hw volume is sw controlled */
	u8 line_out_hw_vol;

319
320
321
	/* support for main/alt speaker switching */
	u8 has_speaker_switching;

322
323
324
	/* support for talkback microphone */
	u8 has_talkback;

325
326
327
328
329
	/* the number of analogue inputs with a software switchable
	 * level control that can be set to line or instrument
	 */
	u8 level_input_count;

330
331
332
	/* the first input with a level control (0-based) */
	u8 level_input_first;

333
334
335
336
337
	/* the number of analogue inputs with a software switchable
	 * 10dB pad control
	 */
	u8 pad_input_count;

338
339
340
341
342
	/* the number of analogue inputs with a software switchable
	 * "air" control
	 */
	u8 air_input_count;

343
344
345
346
347
348
	/* the number of phantom (48V) software switchable controls */
	u8 phantom_count;

	/* the number of inputs each phantom switch controls */
	u8 inputs_per_phantom;

349
350
351
352
353
	/* the number of direct monitor options
	 * (0 = none, 1 = mono only, 2 = mono/stereo)
	 */
	u8 direct_monitor;

354
355
356
357
358
359
	/* remap analogue outputs; 18i8 Gen 3 has "line 3/4" connected
	 * internally to the analogue 7/8 outputs
	 */
	u8 line_out_remap_enable;
	u8 line_out_remap[SCARLETT2_ANALOGUE_MAX];

360
	/* additional description for the line out volume controls */
361
	const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
362

363
364
	/* number of sources/destinations of each port type */
	const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];
365
366
367
368

	/* layout/order of the entries in the set_mux message */
	struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES]
						 [SCARLETT2_MAX_MUX_ENTRIES];
369
370
};

371
struct scarlett2_data {
372
373
374
375
376
	struct usb_mixer_interface *mixer;
	struct mutex usb_mutex; /* prevent sending concurrent USB requests */
	struct mutex data_mutex; /* lock access to this data */
	struct delayed_work work;
	const struct scarlett2_device_info *info;
377
378
379
380
	__u8 bInterfaceNumber;
	__u8 bEndpointAddress;
	__u16 wMaxPacketSize;
	__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
392
393
	u8 master_vol;
	u8 vol[SCARLETT2_ANALOGUE_MAX];
	u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
394
	u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
395
396
	u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
	u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
397
	u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
398
	u8 air_switch[SCARLETT2_AIR_SWITCH_MAX];
399
400
	u8 phantom_switch[SCARLETT2_PHANTOM_SWITCH_MAX];
	u8 phantom_persistence;
401
	u8 direct_monitor_switch;
402
	u8 speaker_switching_switch;
403
404
	u8 talkback_switch;
	u8 talkback_map[SCARLETT2_OUTPUT_MIX_MAX];
405
	u8 msd_switch;
406
	struct snd_kcontrol *sync_ctl;
407
408
	struct snd_kcontrol *master_vol_ctl;
	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
413
	struct snd_kcontrol *level_ctls[SCARLETT2_LEVEL_SWITCH_MAX];
	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
421
422
423
424
425
426
	u8 mux[SCARLETT2_MUX_MAX];
	u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX];
};

/*** Model-specific data ***/

static const struct scarlett2_device_info s6i6_gen2_info = {
427
428
	.usb_id = USB_ID(0x1235, 0x8203),

429
	.has_mixer = 1,
430
431
432
433
	.level_input_count = 2,
	.pad_input_count = 2,

	.line_out_descrs = {
434
435
436
437
		"Headphones 1 L",
		"Headphones 1 R",
		"Headphones 2 L",
		"Headphones 2 R",
438
439
	},

440
441
442
443
444
445
	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = {  4,  4 },
		[SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
		[SCARLETT2_PORT_TYPE_PCM]      = {  6,  6 },
446
	},
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  6 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  6 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  6 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	} },
470
471
472
};

static const struct scarlett2_device_info s18i8_gen2_info = {
473
474
	.usb_id = USB_ID(0x1235, 0x8204),

475
	.has_mixer = 1,
476
477
478
479
480
481
482
483
484
485
486
487
	.level_input_count = 2,
	.pad_input_count = 4,

	.line_out_descrs = {
		"Monitor L",
		"Monitor R",
		"Headphones 1 L",
		"Headphones 1 R",
		"Headphones 2 L",
		"Headphones 2 R",
	},

488
489
490
491
492
493
494
	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = {  8,  6 },
		[SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
		[SCARLETT2_PORT_TYPE_ADAT]     = {  8,  0 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
		[SCARLETT2_PORT_TYPE_PCM]      = {  8, 18 },
495
	},
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 18 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 14 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 10 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  6 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  4 },
		{ 0,                            0,  0 },
	} },
519
520
521
};

static const struct scarlett2_device_info s18i20_gen2_info = {
522
523
	.usb_id = USB_ID(0x1235, 0x8201),

524
	.has_mixer = 1,
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
	.line_out_hw_vol = 1,

	.line_out_descrs = {
		"Monitor L",
		"Monitor R",
		NULL,
		NULL,
		NULL,
		NULL,
		"Headphones 1 L",
		"Headphones 1 R",
		"Headphones 2 L",
		"Headphones 2 R",
	},

540
541
542
543
544
545
546
	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = {  8, 10 },
		[SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
		[SCARLETT2_PORT_TYPE_ADAT]     = {  8,  8 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 10, 18 },
		[SCARLETT2_PORT_TYPE_PCM]      = { 20, 18 },
547
	},
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 18 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_ADAT,     0,  8 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 14 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_ADAT,     0,  4 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  8 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0, 10 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0, 18 },
		{ SCARLETT2_PORT_TYPE_NONE,     0,  6 },
		{ 0,                            0,  0 },
	} },
573
574
};

575
576
577
578
579
580
static const struct scarlett2_device_info solo_gen3_info = {
	.usb_id = USB_ID(0x1235, 0x8211),

	.has_msd_mode = 1,
	.level_input_count = 1,
	.level_input_first = 1,
581
	.air_input_count = 1,
582
583
	.phantom_count = 1,
	.inputs_per_phantom = 1,
584
	.direct_monitor = 1,
585
586
587
588
589
590
591
};

static const struct scarlett2_device_info s2i2_gen3_info = {
	.usb_id = USB_ID(0x1235, 0x8210),

	.has_msd_mode = 1,
	.level_input_count = 2,
592
	.air_input_count = 2,
593
594
	.phantom_count = 1,
	.inputs_per_phantom = 2,
595
	.direct_monitor = 2,
596
597
};

598
599
600
static const struct scarlett2_device_info s4i4_gen3_info = {
	.usb_id = USB_ID(0x1235, 0x8212),

601
	.has_msd_mode = 1,
602
	.has_mixer = 1,
603
604
	.level_input_count = 2,
	.pad_input_count = 2,
605
	.air_input_count = 2,
606
607
	.phantom_count = 1,
	.inputs_per_phantom = 2,
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646

	.line_out_descrs = {
		"Monitor L",
		"Monitor R",
		"Headphones L",
		"Headphones R",
	},

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = { 1, 0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 6, 8 },
		[SCARLETT2_PORT_TYPE_PCM]      = { 4, 6 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  6 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_MIX,      0,  8 },
		{ SCARLETT2_PORT_TYPE_NONE,     0, 16 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  6 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_MIX,      0,  8 },
		{ SCARLETT2_PORT_TYPE_NONE,     0, 16 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  6 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_MIX,      0,  8 },
		{ SCARLETT2_PORT_TYPE_NONE,     0, 16 },
		{ 0,                            0,  0 },
	} },
};

static const struct scarlett2_device_info s8i6_gen3_info = {
	.usb_id = USB_ID(0x1235, 0x8213),

647
	.has_msd_mode = 1,
648
	.has_mixer = 1,
649
650
	.level_input_count = 2,
	.pad_input_count = 2,
651
	.air_input_count = 2,
652
653
	.phantom_count = 1,
	.inputs_per_phantom = 2,
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699

	.line_out_descrs = {
		"Headphones 1 L",
		"Headphones 1 R",
		"Headphones 2 L",
		"Headphones 2 R",
	},

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = { 1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = { 6,  4 },
		[SCARLETT2_PORT_TYPE_SPDIF]    = { 2,  2 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 8,  8 },
		[SCARLETT2_PORT_TYPE_PCM]      = { 6, 10 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  8 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,      8,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0,  8 },
		{ SCARLETT2_PORT_TYPE_NONE,     0, 18 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  8 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,      8,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0,  8 },
		{ SCARLETT2_PORT_TYPE_NONE,     0, 18 },
		{ 0,                            0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,      0,  8 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE, 0,  4 },
		{ SCARLETT2_PORT_TYPE_SPDIF,    0,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,      8,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,      0,  8 },
		{ SCARLETT2_PORT_TYPE_NONE,     0, 18 },
		{ 0,                            0,  0 },
	} },
};

static const struct scarlett2_device_info s18i8_gen3_info = {
	.usb_id = USB_ID(0x1235, 0x8214),

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
708
	.phantom_count = 2,
	.inputs_per_phantom = 2,
709

710
711
712
	.line_out_remap_enable = 1,
	.line_out_remap = { 0, 1, 6, 7, 2, 3, 4, 5 },

713
714
715
	.line_out_descrs = {
		"Monitor L",
		"Monitor R",
716
717
		"Alt Monitor L",
		"Alt Monitor R",
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
		"Headphones 1 L",
		"Headphones 1 R",
		"Headphones 2 L",
		"Headphones 2 R",
	},

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = {  8,  8 },
		[SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
		[SCARLETT2_PORT_TYPE_ADAT]     = {  8,  0 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 10, 20 },
		[SCARLETT2_PORT_TYPE_PCM]      = {  8, 20 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_PCM,       0, 10 },
		{ SCARLETT2_PORT_TYPE_PCM,      12,  8 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  6,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  2,  4 },
		{ SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,      10,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,       0, 20 },
		{ SCARLETT2_PORT_TYPE_NONE,      0, 10 },
		{ 0,                             0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,       0, 10 },
		{ SCARLETT2_PORT_TYPE_PCM,      12,  4 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  6,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  2,  4 },
		{ SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
		{ SCARLETT2_PORT_TYPE_PCM,      10,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,       0, 20 },
		{ SCARLETT2_PORT_TYPE_NONE,      0, 10 },
		{ 0,                             0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,       0, 10 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  6,  2 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  2,  4 },
		{ SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,       0, 20 },
		{ SCARLETT2_PORT_TYPE_NONE,      0, 10 },
		{ 0,                             0,  0 },
	} },
};

static const struct scarlett2_device_info s18i20_gen3_info = {
	.usb_id = USB_ID(0x1235, 0x8215),

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
776
	.level_input_count = 2,
	.pad_input_count = 8,
777
	.air_input_count = 8,
778
779
	.phantom_count = 2,
	.inputs_per_phantom = 4,
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831

	.line_out_descrs = {
		"Monitor 1 L",
		"Monitor 1 R",
		"Monitor 2 L",
		"Monitor 2 R",
		NULL,
		NULL,
		"Headphones 1 L",
		"Headphones 1 R",
		"Headphones 2 L",
		"Headphones 2 R",
	},

	.port_count = {
		[SCARLETT2_PORT_TYPE_NONE]     = {  1,  0 },
		[SCARLETT2_PORT_TYPE_ANALOGUE] = {  9, 10 },
		[SCARLETT2_PORT_TYPE_SPDIF]    = {  2,  2 },
		[SCARLETT2_PORT_TYPE_ADAT]     = {  8,  8 },
		[SCARLETT2_PORT_TYPE_MIX]      = { 12, 25 },
		[SCARLETT2_PORT_TYPE_PCM]      = { 20, 20 },
	},

	.mux_assignment = { {
		{ SCARLETT2_PORT_TYPE_PCM,       0,  8 },
		{ SCARLETT2_PORT_TYPE_PCM,      10, 10 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0, 10 },
		{ SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
		{ SCARLETT2_PORT_TYPE_ADAT,      0,  8 },
		{ SCARLETT2_PORT_TYPE_PCM,       8,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,       0, 25 },
		{ SCARLETT2_PORT_TYPE_NONE,      0, 12 },
		{ 0,                             0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,       0,  8 },
		{ SCARLETT2_PORT_TYPE_PCM,      10,  8 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0, 10 },
		{ SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
		{ SCARLETT2_PORT_TYPE_ADAT,      0,  8 },
		{ SCARLETT2_PORT_TYPE_PCM,       8,  2 },
		{ SCARLETT2_PORT_TYPE_MIX,       0, 25 },
		{ SCARLETT2_PORT_TYPE_NONE,      0, 10 },
		{ 0,                             0,  0 },
	}, {
		{ SCARLETT2_PORT_TYPE_PCM,       0, 10 },
		{ SCARLETT2_PORT_TYPE_ANALOGUE,  0, 10 },
		{ SCARLETT2_PORT_TYPE_SPDIF,     0,  2 },
		{ SCARLETT2_PORT_TYPE_NONE,      0, 24 },
		{ 0,                             0,  0 },
	} },
};

832
833
834
835
836
837
static const struct scarlett2_device_info *scarlett2_devices[] = {
	/* Supported Gen 2 devices */
	&s6i6_gen2_info,
	&s18i8_gen2_info,
	&s18i20_gen2_info,

838
	/* Supported Gen 3 devices */
839
840
	&solo_gen3_info,
	&s2i2_gen3_info,
841
842
843
844
845
	&s4i4_gen3_info,
	&s8i6_gen3_info,
	&s18i8_gen3_info,
	&s18i20_gen3_info,

846
847
848
849
	/* End of list */
	NULL
};

850
/* get the starting port index number for a given port type/direction */
851
852
853
static int scarlett2_get_port_start_num(
	const int port_count[][SCARLETT2_PORT_DIRNS],
	int direction, int port_type)
854
855
856
857
{
	int i, num = 0;

	for (i = 0; i < port_type; i++)
858
		num += port_count[i][direction];
859
860
861
862
863
864

	return num;
}

/*** USB Interactions ***/

865
/* Notifications from the interface */
866
867
868
869
870
#define SCARLETT2_USB_NOTIFY_SYNC          0x00000008
#define SCARLETT2_USB_NOTIFY_DIM_MUTE      0x00200000
#define SCARLETT2_USB_NOTIFY_MONITOR       0x00400000
#define SCARLETT2_USB_NOTIFY_INPUT_OTHER   0x00800000
#define SCARLETT2_USB_NOTIFY_MONITOR_OTHER 0x01000000
871
872

/* Commands for sending/receiving requests/responses */
873
#define SCARLETT2_USB_CMD_INIT 0
874
875
#define SCARLETT2_USB_CMD_REQ  2
#define SCARLETT2_USB_CMD_RESP 3
876

877
878
#define SCARLETT2_USB_INIT_1    0x00000000
#define SCARLETT2_USB_INIT_2    0x00000002
879
880
881
882
883
#define SCARLETT2_USB_GET_METER 0x00001001
#define SCARLETT2_USB_GET_MIX   0x00002001
#define SCARLETT2_USB_SET_MIX   0x00002002
#define SCARLETT2_USB_GET_MUX   0x00003001
#define SCARLETT2_USB_SET_MUX   0x00003002
884
#define SCARLETT2_USB_GET_SYNC  0x00006004
885
886
887
888
#define SCARLETT2_USB_GET_DATA  0x00800000
#define SCARLETT2_USB_SET_DATA  0x00800001
#define SCARLETT2_USB_DATA_CMD  0x00800002

889
890
891
892
893
#define SCARLETT2_USB_CONFIG_SAVE 6

#define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31
#define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1

894
/* volume status is read together (matches scarlett2_config_items[1]) */
895
struct scarlett2_usb_volume_status {
896
897
	/* dim/mute buttons */
	u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
898
899
900
901
902
903
904
905
906

	u8 pad1;

	/* software volume setting */
	s16 sw_vol[SCARLETT2_ANALOGUE_MAX];

	/* actual volume of output inc. dim (-18dB) */
	s16 hw_vol[SCARLETT2_ANALOGUE_MAX];

907
908
	/* internal mute buttons */
	u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
909
910
911
912
913
914
915
916
917
918
919
920

	/* sw (0) or hw (1) controlled */
	u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX];

	u8 pad3[6];

	/* front panel volume knob */
	s16 master_vol;
} __packed;

/* Configuration parameters that can be read and written */
enum {
921
	SCARLETT2_CONFIG_DIM_MUTE = 0,
922
	SCARLETT2_CONFIG_LINE_OUT_VOLUME = 1,
923
924
925
926
	SCARLETT2_CONFIG_MUTE_SWITCH = 2,
	SCARLETT2_CONFIG_SW_HW_SWITCH = 3,
	SCARLETT2_CONFIG_LEVEL_SWITCH = 4,
	SCARLETT2_CONFIG_PAD_SWITCH = 5,
927
	SCARLETT2_CONFIG_MSD_SWITCH = 6,
928
	SCARLETT2_CONFIG_AIR_SWITCH = 7,
929
930
	SCARLETT2_CONFIG_PHANTOM_SWITCH = 8,
	SCARLETT2_CONFIG_PHANTOM_PERSISTENCE = 9,
931
	SCARLETT2_CONFIG_DIRECT_MONITOR = 10,
932
933
	SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH = 11,
	SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE = 12,
934
935
	SCARLETT2_CONFIG_TALKBACK_MAP = 13,
	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
942
943
944
945
946
 */
struct scarlett2_config {
	u8 offset;
	u8 size;
	u8 activate;
};

947
948
949
/* scarlett2_config_items[0] is for devices without a mixer
 * scarlett2_config_items[1] is for devices with a mixer
 */
950
static const struct scarlett2_config
951
952
953
954
955
956
957
	scarlett2_config_items[2][SCARLETT2_CONFIG_COUNT] =

/* Devices without a mixer (Solo and 2i2 Gen 3) */
{ {
	[SCARLETT2_CONFIG_MSD_SWITCH] = {
		.offset = 0x04, .size = 8, .activate = 6 },

958
959
960
961
962
963
	[SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
		.offset = 0x05, .size = 8, .activate = 6 },

	[SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
		.offset = 0x06, .size = 8, .activate = 3 },

964
965
966
	[SCARLETT2_CONFIG_DIRECT_MONITOR] = {
		.offset = 0x07, .size = 8, .activate = 4 },

967
968
969
	[SCARLETT2_CONFIG_LEVEL_SWITCH] = {
		.offset = 0x08, .size = 1, .activate = 7 },

970
971
972
	[SCARLETT2_CONFIG_AIR_SWITCH] = {
		.offset = 0x09, .size = 1, .activate = 8 },

973
974
/* Devices with a mixer (Gen 2 and all other Gen 3) */
}, {
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
994
995
	[SCARLETT2_CONFIG_AIR_SWITCH] = {
		.offset = 0x8c, .size = 8, .activate = 8 },

996
997
998
	[SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
		.offset = 0x9c, .size = 1, .activate = 8 },

999
	[SCARLETT2_CONFIG_MSD_SWITCH] = {
1000
		.offset = 0x9d, .size = 8, .activate = 6 },
For faster browsing, not all history is shown. View entire blame