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