1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Focusrite Scarlett 2 Protocol Driver for ALSA
4 * (including Scarlett 2nd Gen, 3rd Gen, 4th Gen, Clarett USB, and
5 * Clarett+ series products)
6 *
7 * Supported models:
8 * - 6i6/18i8/18i20 Gen 2
9 * - Solo/2i2/4i4/8i6/18i8/18i20 Gen 3
10 * - Solo/2i2/4i4 Gen 4
11 * - Clarett 2Pre/4Pre/8Pre USB
12 * - Clarett+ 2Pre/4Pre/8Pre
13 * - Vocaster One/Two
14 *
15 * Copyright (c) 2018-2025 by Geoffrey D. Bennett <g at b4.vu>
16 * Copyright (c) 2020-2021 by Vladimir Sadovnikov <sadko4u@gmail.com>
17 * Copyright (c) 2022 by Christian Colglazier <christian@cacolglazier.com>
18 *
19 * Based on the Scarlett (Gen 1) Driver for ALSA:
20 *
21 * Copyright (c) 2013 by Tobias Hoffmann
22 * Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
23 * Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
24 * Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
25 *
26 * Many codes borrowed from audio.c by
27 * Alan Cox (alan at lxorguk.ukuu.org.uk)
28 * Thomas Sailer (sailer at ife.ee.ethz.ch)
29 *
30 * Code cleanup:
31 * David Henningsson <david.henningsson at canonical.com>
32 */
33
34 /* The protocol was reverse engineered by looking at the communication
35 * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20
36 * (firmware 1083) using usbmon in July-August 2018.
37 *
38 * Scarlett 18i8 support added in April 2019.
39 *
40 * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
41 * for providing usbmon output and testing).
42 *
43 * Scarlett 4i4/8i6 Gen 3 support added in May 2020 (thanks to Laurent
44 * Debricon for donating a 4i4 and to Fredrik Unger for providing 8i6
45 * usbmon output and testing).
46 *
47 * Scarlett 18i8/18i20 Gen 3 support added in June 2020 (thanks to
48 * Darren Jaeckel, Alex Sedlack, and Clovis Lunel for providing usbmon
49 * output, protocol traces and testing).
50 *
51 * Support for loading mixer volume and mux configuration from the
52 * interface during driver initialisation added in May 2021 (thanks to
53 * Vladimir Sadovnikov for figuring out how).
54 *
55 * Support for Solo/2i2 Gen 3 added in May 2021 (thanks to Alexander
56 * Vorona for 2i2 protocol traces).
57 *
58 * Support for phantom power, direct monitoring, speaker switching,
59 * and talkback added in May-June 2021.
60 *
61 * Support for Clarett+ 8Pre added in Aug 2022 by Christian
62 * Colglazier.
63 *
64 * Support for Clarett 8Pre USB added in Sep 2023 (thanks to Philippe
65 * Perrot for confirmation).
66 *
67 * Support for Clarett+ 4Pre and 2Pre added in Sep 2023 (thanks to
68 * Gregory Rozzo for donating a 4Pre, and David Sherwood and Patrice
69 * Peterson for usbmon output).
70 *
71 * Support for Clarett 2Pre and 4Pre USB added in Oct 2023.
72 *
73 * Support for firmware updates added in Dec 2023.
74 *
75 * Support for Scarlett Solo/2i2/4i4 Gen 4 added in Dec 2023 (thanks
76 * to many LinuxMusicians people and to Focusrite for hardware
77 * donations).
78 *
79 * Support for Vocaster One and Two added in Mar 2024 (thanks to many
80 * LinuxMusicians people and to Focusrite for hardware donations).
81 *
82 * This ALSA mixer gives access to (model-dependent):
83 * - input, output, mixer-matrix muxes
84 * - mixer-matrix gain stages
85 * - gain/volume/mute controls
86 * - level meters
87 * - line/inst level, pad, and air controls
88 * - phantom power, direct monitor, speaker switching, and talkback
89 * controls
90 * - disable/enable MSD mode
91 * - disable/enable standalone mode
92 * - input mute, gain, autogain, safe mode
93 * - direct monitor mixes
94 * - compressor and EQ
95 * - Bluetooth volume
96 *
97 * <ditaa>
98 * /--------------\ 18chn 20chn /--------------\
99 * | Hardware in +--+------\ /-------------+--+ ALSA PCM out |
100 * \--------------/ | | | | \--------------/
101 * | | | /-----\ |
102 * | | | | | |
103 * | v v v | |
104 * | +---------------+ | |
105 * | \ Matrix Mux / | |
106 * | +-----+-----+ | |
107 * | | | |
108 * | |18chn | |
109 * | | | |
110 * | | 10chn| |
111 * | v | |
112 * | +------------+ | |
113 * | | Mixer | | |
114 * | | Matrix | | |
115 * | | | | |
116 * | | 18x10 Gain | | |
117 * | | stages | | |
118 * | +-----+------+ | |
119 * | | | |
120 * |18chn |10chn | |20chn
121 * | | | |
122 * | +----------/ |
123 * | | |
124 * v v v
125 * ===========================
126 * +---------------+ +--—------------+
127 * \ Output Mux / \ Capture Mux /
128 * +---+---+---+ +-----+-----+
129 * | | |
130 * 10chn| | |18chn
131 * | | |
132 * /--------------\ | | | /--------------\
133 * | S/PDIF, ADAT |<--/ |10chn \-->| ALSA PCM in |
134 * | Hardware out | | \--------------/
135 * \--------------/ |
136 * v
137 * +-------------+ Software gain per channel.
138 * | Master Gain |<-- 18i20 only: Switch per channel
139 * +------+------+ to select HW or SW gain control.
140 * |
141 * |10chn
142 * /--------------\ |
143 * | Analogue |<------/
144 * | Hardware out |
145 * \--------------/
146 * </ditaa>
147 *
148 * Gen 3/4 devices have a Mass Storage Device (MSD) mode where a small
149 * disk with registration and driver download information is presented
150 * to the host. To access the full functionality of the device without
151 * proprietary software, MSD mode can be disabled by:
152 * - holding down the 48V button for five seconds while powering on
153 * the device, or
154 * - using this driver and alsamixer to change the "MSD Mode" setting
155 * to Off and power-cycling the device
156 */
157
158 #include <linux/slab.h>
159 #include <linux/usb.h>
160 #include <linux/moduleparam.h>
161
162 #include <sound/control.h>
163 #include <sound/tlv.h>
164 #include <sound/hwdep.h>
165
166 #include <uapi/sound/scarlett2.h>
167
168 #include "usbaudio.h"
169 #include "mixer.h"
170 #include "helper.h"
171
172 #include "mixer_scarlett2.h"
173 #include "fcp.h"
174
175 /* device_setup value to allow turning MSD mode back on */
176 #define SCARLETT2_MSD_ENABLE 0x02
177
178 /* device_setup value to disable this mixer driver */
179 #define SCARLETT2_DISABLE 0x04
180
181 /* device_setup value to use the FCP driver instead */
182 #define SCARLETT2_USE_FCP_DRIVER 0x08
183
184 /* some gui mixers can't handle negative ctl values */
185 #define SCARLETT2_VOLUME_BIAS 127
186
187 /* maximum preamp input gain value
188 * (the corresponding value in dB is per-device)
189 */
190 #define SCARLETT2_MAX_GAIN_VALUE 70
191
192 /* maximum Bluetooth volume value */
193 #define SCARLETT2_MAX_BLUETOOTH_VOLUME 30
194
195 /* mixer range from -80dB to +12dB in 0.5dB steps */
196 #define SCARLETT2_MIXER_MIN_DB -80
197 #define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
198 #define SCARLETT2_MIXER_MAX_DB 12
199 #define SCARLETT2_MIXER_MAX_VALUE \
200 ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
201 #define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1)
202
203 /* map from (dB + 80) * 2 to mixer value
204 * for dB in 0 .. 184: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
205 */
206 static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = {
207 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
208 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
209 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
210 23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
211 54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
212 122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
213 244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
214 487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
215 973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
216 1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
217 3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
218 5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
219 9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
220 16345, 17313, 18339, 19426, 20577, 21796, 23088, 24456, 25905,
221 27440, 29066, 30788, 32612
222 };
223
224 /* Maximum number of analogue outputs */
225 #define SCARLETT2_ANALOGUE_MAX 10
226
227 /* Maximum number of various input controls */
228 #define SCARLETT2_LEVEL_SWITCH_MAX 2
229 #define SCARLETT2_PAD_SWITCH_MAX 8
230 #define SCARLETT2_AIR_SWITCH_MAX 8
231 #define SCARLETT2_DSP_SWITCH_MAX 2
232 #define SCARLETT2_INPUT_MUTE_SWITCH_MAX 2
233 #define SCARLETT2_PHANTOM_SWITCH_MAX 2
234 #define SCARLETT2_INPUT_GAIN_MAX 2
235
236 /* Maximum number of inputs to the mixer */
237 #define SCARLETT2_INPUT_MIX_MAX 25
238
239 /* Maximum number of outputs from the mixer */
240 #define SCARLETT2_OUTPUT_MIX_MAX 12
241
242 /* Maximum number of mixer gain controls */
243 #define SCARLETT2_MIX_MAX (SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX)
244
245 /* Maximum number of direct monitor mixer gain controls
246 * 1 (Solo) or 2 (2i2) direct monitor selections (Mono & Stereo)
247 * 2 Mix outputs (A/Left & B/Right)
248 * 4 Mix inputs
249 */
250 #define SCARLETT2_MONITOR_MIX_MAX (2 * 2 * 4)
251
252 /* Maximum size of the data in the USB mux assignment message:
253 * 20 inputs, 20 outputs, 25 matrix inputs, 12 spare
254 */
255 #define SCARLETT2_MUX_MAX 77
256
257 /* Maximum number of sources (sum of input port counts) */
258 #define SCARLETT2_MAX_SRCS 52
259
260 /* Maximum number of meters (sum of output port counts) */
261 #define SCARLETT2_MAX_METERS 65
262
263 /* Compressor parameter data
264 *
265 * The compressor parameters are 32-bit fixed point values with 24
266 * bits of fraction. Integer values are sufficient for the parameters
267 * except for ratio which we can set in 0.5:1 steps.
268 */
269 struct compressor_param {
270 const char *name;
271 snd_ctl_elem_type_t type;
272 s32 min;
273 s32 max;
274 int scale_bits;
275 };
276
277 /* The available compressor parameters on the Vocaster:
278 * - Enable: Off, On
279 * - Threshold: -40dB to 0dB
280 * - Ratio: 1:1 to 50:1 in 0.5:1 steps
281 * - Knee Width: 0dB to 10dB
282 * - Attack: 30ms to 127ms
283 * - Release: 30ms to 127ms
284 * - Makeup Gain: 0dB to 24dB
285 */
286 static const struct compressor_param compressor_params[] = {
287 { "Enable", SNDRV_CTL_ELEM_TYPE_BOOLEAN, 0, 1, 0 },
288 { "Threshold", SNDRV_CTL_ELEM_TYPE_INTEGER, -40, 0, 24 },
289 { "Ratio", SNDRV_CTL_ELEM_TYPE_INTEGER, 2, 100, 23 },
290 { "Knee Width", SNDRV_CTL_ELEM_TYPE_INTEGER, 0, 10, 24 },
291 { "Attack", SNDRV_CTL_ELEM_TYPE_INTEGER, 30, 127, 24 },
292 { "Release", SNDRV_CTL_ELEM_TYPE_INTEGER, 30, 127, 24 },
293 { "Makeup Gain", SNDRV_CTL_ELEM_TYPE_INTEGER, 0, 24, 24 },
294 };
295
296 #define SCARLETT2_COMPRESSOR_PARAM_COUNT ARRAY_SIZE(compressor_params)
297 #define SCARLETT2_COMPRESSOR_CTLS_MAX \
298 (SCARLETT2_COMPRESSOR_PARAM_COUNT * SCARLETT2_DSP_SWITCH_MAX)
299
300 /* Maximum number of filter controls */
301 #define SCARLETT2_PRECOMP_FLT_CTLS_MAX (2 * SCARLETT2_DSP_SWITCH_MAX)
302 #define SCARLETT2_PEQ_FLT_CTLS_MAX (3 * SCARLETT2_DSP_SWITCH_MAX)
303
304 /* Number of biquad filter coefficients */
305 #define SCARLETT2_BIQUAD_COEFFS 5
306
307 /* Maximum number of filter coefficient values */
308 #define SCARLETT2_PRECOMP_FLT_VALUES_MAX \
309 (SCARLETT2_PRECOMP_FLT_CTLS_MAX * SCARLETT2_BIQUAD_COEFFS)
310 #define SCARLETT2_PEQ_FLT_VALUES_MAX \
311 (SCARLETT2_PEQ_FLT_CTLS_MAX * SCARLETT2_BIQUAD_COEFFS)
312
313 /* Maximum number of PEQ filter slots */
314 #define SCARLETT2_PEQ_FLT_SLOTS_MAX 4
315
316 /* Hardware port types:
317 * - None (no input to mux)
318 * - Analogue I/O
319 * - S/PDIF I/O
320 * - ADAT I/O
321 * - Mixer I/O
322 * - PCM I/O
323 */
324 enum {
325 SCARLETT2_PORT_TYPE_NONE,
326 SCARLETT2_PORT_TYPE_ANALOGUE,
327 SCARLETT2_PORT_TYPE_SPDIF,
328 SCARLETT2_PORT_TYPE_ADAT,
329 SCARLETT2_PORT_TYPE_MIX,
330 SCARLETT2_PORT_TYPE_PCM,
331 SCARLETT2_PORT_TYPE_COUNT
332 };
333
334 /* I/O count of each port type kept in struct scarlett2_ports */
335 enum {
336 SCARLETT2_PORT_IN,
337 SCARLETT2_PORT_OUT,
338 SCARLETT2_PORT_DIRNS
339 };
340
341 /* Dim/Mute buttons on the 18i20 */
342 enum {
343 SCARLETT2_BUTTON_MUTE,
344 SCARLETT2_BUTTON_DIM,
345 SCARLETT2_DIM_MUTE_COUNT
346 };
347
348 /* Autogain target values */
349
350 #define SCARLETT2_AG_TARGET_MIN (-30)
351
352 enum {
353 SCARLETT2_AG_HOT_TARGET,
354 SCARLETT2_AG_MEAN_TARGET,
355 SCARLETT2_AG_PEAK_TARGET,
356 SCARLETT2_AG_TARGET_COUNT
357 };
358
359 /* Flash Write State */
360 enum {
361 SCARLETT2_FLASH_WRITE_STATE_IDLE,
362 SCARLETT2_FLASH_WRITE_STATE_SELECTED,
363 SCARLETT2_FLASH_WRITE_STATE_ERASING,
364 SCARLETT2_FLASH_WRITE_STATE_WRITE
365 };
366
367 static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
368 "Mute Playback Switch", "Dim Playback Switch"
369 };
370
371 /* Vocaster One speaker/headphone mute names */
372 static const char *const vocaster_one_sp_hp_mute_names[] = {
373 "Speaker Mute Playback Switch",
374 "Headphones Mute Playback Switch",
375 NULL
376 };
377
378 /* Vocaster Two speaker/headphone mute names */
379 static const char *const vocaster_two_sp_hp_mute_names[] = {
380 "Speaker Mute Playback Switch",
381 "Headphones 1 Mute Playback Switch",
382 "Headphones 2 Mute Playback Switch",
383 NULL
384 };
385
386 /* The autogain_status is set based on the autogain_switch and
387 * raw_autogain_status values.
388 *
389 * If autogain_switch is set, autogain_status is set to 0 (Running).
390 * The other status values are from the raw_autogain_status value + 1.
391 */
392 static const char *const scarlett2_autogain_status_gen4[] = {
393 "Running",
394 "Success",
395 "SuccessDRover",
396 "WarnMinGainLimit",
397 "FailDRunder",
398 "FailMaxGainLimit",
399 "FailClipped",
400 "Cancelled",
401 "Invalid",
402 NULL
403 };
404
405 static const char *const scarlett2_autogain_status_vocaster[] = {
406 "Running",
407 "Success",
408 "FailPG",
409 "FailRange",
410 "WarnMaxCap",
411 "WarnMinCap",
412 "Cancelled",
413 "Invalid",
414 NULL
415 };
416
417 /* Power Status Values */
418 enum {
419 SCARLETT2_POWER_STATUS_EXT,
420 SCARLETT2_POWER_STATUS_BUS,
421 SCARLETT2_POWER_STATUS_FAIL,
422 SCARLETT2_POWER_STATUS_COUNT
423 };
424
425 /* Notification callback functions */
426 struct scarlett2_notification {
427 u32 mask;
428 void (*func)(struct usb_mixer_interface *mixer);
429 };
430
431 static void scarlett2_notify_ack(struct usb_mixer_interface *mixer);
432 static void scarlett2_notify_sync(struct usb_mixer_interface *mixer);
433 static void scarlett2_notify_dim_mute(struct usb_mixer_interface *mixer);
434 static void scarlett2_notify_monitor(struct usb_mixer_interface *mixer);
435 static void scarlett2_notify_volume(struct usb_mixer_interface *mixer);
436 static void scarlett2_notify_input_level(struct usb_mixer_interface *mixer);
437 static void scarlett2_notify_input_pad(struct usb_mixer_interface *mixer);
438 static void scarlett2_notify_input_air(struct usb_mixer_interface *mixer);
439 static void scarlett2_notify_input_dsp(struct usb_mixer_interface *mixer);
440 static void scarlett2_notify_input_mute(struct usb_mixer_interface *mixer);
441 static void scarlett2_notify_input_phantom(struct usb_mixer_interface *mixer);
442 static void scarlett2_notify_input_other(struct usb_mixer_interface *mixer);
443 static void scarlett2_notify_input_select(struct usb_mixer_interface *mixer);
444 static void scarlett2_notify_input_gain(struct usb_mixer_interface *mixer);
445 static void scarlett2_notify_autogain(struct usb_mixer_interface *mixer);
446 static void scarlett2_notify_input_safe(struct usb_mixer_interface *mixer);
447 static void scarlett2_notify_monitor_other(struct usb_mixer_interface *mixer);
448 static void scarlett2_notify_direct_monitor(struct usb_mixer_interface *mixer);
449 static void scarlett2_notify_power_status(struct usb_mixer_interface *mixer);
450 static void scarlett2_notify_pcm_input_switch(
451 struct usb_mixer_interface *mixer);
452 static void scarlett2_notify_bluetooth(struct usb_mixer_interface *mixer);
453
454 /* Arrays of notification callback functions */
455
456 static const struct scarlett2_notification scarlett2_notifications[] = {
457 { 0x00000001, scarlett2_notify_ack },
458 { 0x00000008, scarlett2_notify_sync },
459 { 0x00200000, scarlett2_notify_dim_mute },
460 { 0x00400000, scarlett2_notify_monitor },
461 { 0x00800000, scarlett2_notify_input_other },
462 { 0x01000000, scarlett2_notify_monitor_other },
463 { 0, NULL }
464 };
465
466 static const struct scarlett2_notification scarlett3a_notifications[] = {
467 { 0x00000001, scarlett2_notify_ack },
468 { 0x00800000, scarlett2_notify_input_other },
469 { 0x01000000, scarlett2_notify_direct_monitor },
470 { 0, NULL }
471 };
472
473 static const struct scarlett2_notification vocaster_notifications[] = {
474 { 0x00000001, scarlett2_notify_ack },
475 { 0x00000008, scarlett2_notify_sync },
476 { 0x00200000, scarlett2_notify_input_mute },
477 { 0x00400000, scarlett2_notify_autogain },
478 { 0x04000000, scarlett2_notify_input_dsp },
479 { 0x08000000, scarlett2_notify_input_gain },
480 { 0x10000000, scarlett2_notify_input_phantom },
481 { 0x20000000, scarlett2_notify_bluetooth },
482 { 0, NULL }
483 };
484
485 static const struct scarlett2_notification scarlett4_solo_notifications[] = {
486 { 0x00000001, scarlett2_notify_ack },
487 { 0x00000008, scarlett2_notify_sync },
488 { 0x00400000, scarlett2_notify_input_air },
489 { 0x00800000, scarlett2_notify_direct_monitor },
490 { 0x01000000, scarlett2_notify_input_level },
491 { 0x02000000, scarlett2_notify_input_phantom },
492 { 0x04000000, scarlett2_notify_pcm_input_switch },
493 { 0, NULL }
494 };
495
496 static const struct scarlett2_notification scarlett4_2i2_notifications[] = {
497 { 0x00000001, scarlett2_notify_ack },
498 { 0x00000008, scarlett2_notify_sync },
499 { 0x00200000, scarlett2_notify_input_safe },
500 { 0x00400000, scarlett2_notify_autogain },
501 { 0x00800000, scarlett2_notify_input_air },
502 { 0x01000000, scarlett2_notify_direct_monitor },
503 { 0x02000000, scarlett2_notify_input_select },
504 { 0x04000000, scarlett2_notify_input_level },
505 { 0x08000000, scarlett2_notify_input_phantom },
506 { 0x10000000, NULL }, /* power status, ignored */
507 { 0x40000000, scarlett2_notify_input_gain },
508 { 0x80000000, NULL }, /* power status, ignored */
509 { 0, NULL }
510 };
511
512 static const struct scarlett2_notification scarlett4_4i4_notifications[] = {
513 { 0x00000001, scarlett2_notify_ack },
514 { 0x00000008, scarlett2_notify_sync },
515 { 0x00200000, scarlett2_notify_input_safe },
516 { 0x00400000, scarlett2_notify_autogain },
517 { 0x00800000, scarlett2_notify_input_air },
518 { 0x01000000, scarlett2_notify_input_select },
519 { 0x02000000, scarlett2_notify_input_level },
520 { 0x04000000, scarlett2_notify_input_phantom },
521 { 0x08000000, scarlett2_notify_power_status }, /* power external */
522 { 0x20000000, scarlett2_notify_input_gain },
523 { 0x40000000, scarlett2_notify_power_status }, /* power status */
524 { 0x80000000, scarlett2_notify_volume },
525 { 0, NULL }
526 };
527
528 /* Configuration parameters that can be read and written */
529 enum {
530 SCARLETT2_CONFIG_DIM_MUTE,
531 SCARLETT2_CONFIG_LINE_OUT_VOLUME,
532 SCARLETT2_CONFIG_MUTE_SWITCH,
533 SCARLETT2_CONFIG_SW_HW_SWITCH,
534 SCARLETT2_CONFIG_MASTER_VOLUME,
535 SCARLETT2_CONFIG_HEADPHONE_VOLUME,
536 SCARLETT2_CONFIG_LEVEL_SWITCH,
537 SCARLETT2_CONFIG_PAD_SWITCH,
538 SCARLETT2_CONFIG_MSD_SWITCH,
539 SCARLETT2_CONFIG_AIR_SWITCH,
540 SCARLETT2_CONFIG_DSP_SWITCH,
541 SCARLETT2_CONFIG_COMPRESSOR_PARAMS,
542 SCARLETT2_CONFIG_PRECOMP_FLT_SWITCH,
543 SCARLETT2_CONFIG_PRECOMP_FLT_PARAMS,
544 SCARLETT2_CONFIG_PEQ_FLT_SWITCH,
545 SCARLETT2_CONFIG_PEQ_FLT_PARAMS,
546 SCARLETT2_CONFIG_INPUT_MUTE_SWITCH,
547 SCARLETT2_CONFIG_STANDALONE_SWITCH,
548 SCARLETT2_CONFIG_PHANTOM_SWITCH,
549 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
550 SCARLETT2_CONFIG_DIRECT_MONITOR,
551 SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
552 SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
553 SCARLETT2_CONFIG_TALKBACK_MAP,
554 SCARLETT2_CONFIG_AUTOGAIN_SWITCH,
555 SCARLETT2_CONFIG_AUTOGAIN_STATUS,
556 SCARLETT2_CONFIG_AG_HOT_TARGET,
557 SCARLETT2_CONFIG_AG_MEAN_TARGET,
558 SCARLETT2_CONFIG_AG_PEAK_TARGET,
559 SCARLETT2_CONFIG_INPUT_GAIN,
560 SCARLETT2_CONFIG_SAFE_SWITCH,
561 SCARLETT2_CONFIG_INPUT_SELECT_SWITCH,
562 SCARLETT2_CONFIG_INPUT_LINK_SWITCH,
563 SCARLETT2_CONFIG_POWER_EXT,
564 SCARLETT2_CONFIG_POWER_LOW,
565 SCARLETT2_CONFIG_PCM_INPUT_SWITCH,
566 SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN,
567 SCARLETT2_CONFIG_BLUETOOTH_VOLUME,
568 SCARLETT2_CONFIG_SPDIF_MODE,
569 SCARLETT2_CONFIG_SP_HP_MUTE,
570 SCARLETT2_CONFIG_COUNT
571 };
572
573 /* Autogain target configuration parameters and names */
574
575 static const int scarlett2_ag_target_configs[] = {
576 [SCARLETT2_AG_HOT_TARGET] = SCARLETT2_CONFIG_AG_HOT_TARGET,
577 [SCARLETT2_AG_MEAN_TARGET] = SCARLETT2_CONFIG_AG_MEAN_TARGET,
578 [SCARLETT2_AG_PEAK_TARGET] = SCARLETT2_CONFIG_AG_PEAK_TARGET
579 };
580
581 static const char *const scarlett2_ag_target_names[] = {
582 "Hot", "Mean", "Peak"
583 };
584
585 /* Location, size, and activation command number for the configuration
586 * parameters. Size is in bits and may be 1, 8, 16, or 32.
587 *
588 * Vocaster and 4th Gen devices have a parameter buffer to set certain
589 * configuration parameters. When pbuf is set, rather than writing to
590 * the given offset, the channel and value are written to the
591 * parameter buffer and the activate command is sent to the device.
592 *
593 * Some Gen 4 configuration parameters are written with 0x02 for a
594 * desired value of 0x01, and 0x03 for 0x00. These are indicated with
595 * mute set to 1. 0x02 and 0x03 are temporary values while the device
596 * makes the change and the channel and/or corresponding DSP channel
597 * output is muted.
598 */
599 struct scarlett2_config {
600 u16 offset;
601 u8 size;
602 u8 activate;
603 u8 pbuf;
604 u8 mute;
605 };
606
607 struct scarlett2_config_set {
608 const struct scarlett2_notification *notifications;
609 u16 param_buf_addr;
610 const unsigned int *input_gain_tlv;
611 const char *const *autogain_status_texts;
612 const struct scarlett2_config items[SCARLETT2_CONFIG_COUNT];
613 };
614
615 /* Input gain TLV dB ranges */
616
617 static const DECLARE_TLV_DB_MINMAX(
618 db_scale_vocaster_gain, 0, 70 * 100
619 );
620
621 static const DECLARE_TLV_DB_MINMAX(
622 db_scale_gen4_gain, 0, 69 * 100
623 );
624
625 /* Gen 2 devices without SW/HW volume switch: 6i6, 18i8 */
626
627 static const struct scarlett2_config_set scarlett2_config_set_gen2a = {
628 .notifications = scarlett2_notifications,
629 .items = {
630 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
631 .offset = 0x34, .size = 16, .activate = 1 },
632
633 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
634 .offset = 0x5c, .size = 8, .activate = 1 },
635
636 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
637 .offset = 0x7c, .size = 8, .activate = 7 },
638
639 [SCARLETT2_CONFIG_PAD_SWITCH] = {
640 .offset = 0x84, .size = 8, .activate = 8 },
641
642 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
643 .offset = 0x8d, .size = 8, .activate = 6 },
644 }
645 };
646
647 /* Gen 2 devices with SW/HW volume switch: 18i20 */
648
649 static const struct scarlett2_config_set scarlett2_config_set_gen2b = {
650 .notifications = scarlett2_notifications,
651 .items = {
652 [SCARLETT2_CONFIG_DIM_MUTE] = {
653 .offset = 0x31, .size = 8, .activate = 2 },
654
655 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
656 .offset = 0x34, .size = 16, .activate = 1 },
657
658 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
659 .offset = 0x5c, .size = 8, .activate = 1 },
660
661 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
662 .offset = 0x66, .size = 8, .activate = 3 },
663
664 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
665 .offset = 0x76, .size = 16 },
666
667 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
668 .offset = 0x7c, .size = 8, .activate = 7 },
669
670 [SCARLETT2_CONFIG_PAD_SWITCH] = {
671 .offset = 0x84, .size = 8, .activate = 8 },
672
673 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
674 .offset = 0x8d, .size = 8, .activate = 6 },
675 }
676 };
677
678 /* Gen 3 devices without a mixer (Solo and 2i2) */
679 static const struct scarlett2_config_set scarlett2_config_set_gen3a = {
680 .notifications = scarlett3a_notifications,
681 .items = {
682 [SCARLETT2_CONFIG_MSD_SWITCH] = {
683 .offset = 0x04, .size = 8, .activate = 6 },
684
685 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
686 .offset = 0x05, .size = 8, .activate = 6 },
687
688 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
689 .offset = 0x06, .size = 8, .activate = 3 },
690
691 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
692 .offset = 0x07, .size = 8, .activate = 4 },
693
694 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
695 .offset = 0x08, .size = 1, .activate = 7 },
696
697 [SCARLETT2_CONFIG_AIR_SWITCH] = {
698 .offset = 0x09, .size = 1, .activate = 8 },
699 }
700 };
701
702 /* Gen 3 devices without SW/HW volume switch: 4i4, 8i6 */
703 static const struct scarlett2_config_set scarlett2_config_set_gen3b = {
704 .notifications = scarlett2_notifications,
705 .items = {
706 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
707 .offset = 0x34, .size = 16, .activate = 1 },
708
709 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
710 .offset = 0x5c, .size = 8, .activate = 1 },
711
712 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
713 .offset = 0x7c, .size = 8, .activate = 7 },
714
715 [SCARLETT2_CONFIG_PAD_SWITCH] = {
716 .offset = 0x84, .size = 8, .activate = 8 },
717
718 [SCARLETT2_CONFIG_AIR_SWITCH] = {
719 .offset = 0x8c, .size = 8, .activate = 8 },
720
721 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
722 .offset = 0x95, .size = 8, .activate = 6 },
723
724 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
725 .offset = 0x9c, .size = 1, .activate = 8 },
726
727 [SCARLETT2_CONFIG_MSD_SWITCH] = {
728 .offset = 0x9d, .size = 8, .activate = 6 },
729
730 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
731 .offset = 0x9e, .size = 8, .activate = 6 },
732 }
733 };
734
735 /* Gen 3 devices with SW/HW volume switch: 18i8, 18i20 */
736 static const struct scarlett2_config_set scarlett2_config_set_gen3c = {
737 .notifications = scarlett2_notifications,
738 .items = {
739 [SCARLETT2_CONFIG_DIM_MUTE] = {
740 .offset = 0x31, .size = 8, .activate = 2 },
741
742 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
743 .offset = 0x34, .size = 16, .activate = 1 },
744
745 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
746 .offset = 0x5c, .size = 8, .activate = 1 },
747
748 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
749 .offset = 0x66, .size = 8, .activate = 3 },
750
751 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
752 .offset = 0x76, .size = 16 },
753
754 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
755 .offset = 0x7c, .size = 8, .activate = 7 },
756
757 [SCARLETT2_CONFIG_PAD_SWITCH] = {
758 .offset = 0x84, .size = 8, .activate = 8 },
759
760 [SCARLETT2_CONFIG_AIR_SWITCH] = {
761 .offset = 0x8c, .size = 8, .activate = 8 },
762
763 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
764 .offset = 0x95, .size = 8, .activate = 6 },
765
766 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
767 .offset = 0x9c, .size = 1, .activate = 8 },
768
769 [SCARLETT2_CONFIG_MSD_SWITCH] = {
770 .offset = 0x9d, .size = 8, .activate = 6 },
771
772 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
773 .offset = 0x9e, .size = 8, .activate = 6 },
774
775 [SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH] = {
776 .offset = 0x9f, .size = 1, .activate = 10 },
777
778 [SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE] = {
779 .offset = 0xa0, .size = 1, .activate = 10 },
780
781 [SCARLETT2_CONFIG_TALKBACK_MAP] = {
782 .offset = 0xb0, .size = 16, .activate = 10 },
783
784 [SCARLETT2_CONFIG_SPDIF_MODE] = {
785 .offset = 0x94, .size = 8, .activate = 6 },
786 }
787 };
788
789 /* Vocaster */
790 static const struct scarlett2_config_set scarlett2_config_set_vocaster = {
791 .notifications = vocaster_notifications,
792 .param_buf_addr = 0x1bc,
793 .input_gain_tlv = db_scale_vocaster_gain,
794 .autogain_status_texts = scarlett2_autogain_status_vocaster,
795 .items = {
796 [SCARLETT2_CONFIG_MSD_SWITCH] = {
797 .offset = 0x9d, .size = 8, .activate = 6 },
798
799 [SCARLETT2_CONFIG_AUTOGAIN_SWITCH] = {
800 .offset = 0x1c0, .size = 8, .activate = 19, .pbuf = 1 },
801
802 [SCARLETT2_CONFIG_AUTOGAIN_STATUS] = {
803 .offset = 0x1c2, .size = 8, },
804
805 [SCARLETT2_CONFIG_AG_HOT_TARGET] = {
806 .offset = 0xc1, .size = 8, .activate = 29, .pbuf = 1 },
807
808 [SCARLETT2_CONFIG_INPUT_GAIN] = {
809 .offset = 0x9f, .size = 8, .activate = 21, .pbuf = 1 },
810
811 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
812 .offset = 0x9c, .size = 1, .activate = 20, .pbuf = 1 },
813
814 [SCARLETT2_CONFIG_DSP_SWITCH] = {
815 .offset = 0x1c4, .size = 8, .activate = 22, .pbuf = 1 },
816
817 [SCARLETT2_CONFIG_COMPRESSOR_PARAMS] = {
818 .offset = 0x1c8, .size = 32, .activate = 23 },
819
820 [SCARLETT2_CONFIG_PRECOMP_FLT_SWITCH] = {
821 .offset = 0x7c, .size = 32, .activate = 27 },
822
823 [SCARLETT2_CONFIG_PRECOMP_FLT_PARAMS] = {
824 .offset = 0x200, .size = 32, .activate = 27 },
825
826 [SCARLETT2_CONFIG_PEQ_FLT_SWITCH] = {
827 .offset = 0x84, .size = 32, .activate = 27 },
828
829 [SCARLETT2_CONFIG_PEQ_FLT_PARAMS] = {
830 .offset = 0x250, .size = 32, .activate = 27 },
831
832 [SCARLETT2_CONFIG_INPUT_MUTE_SWITCH] = {
833 .offset = 0x1be, .size = 8, .activate = 17, .pbuf = 1 },
834
835 [SCARLETT2_CONFIG_BLUETOOTH_VOLUME] = {
836 .offset = 0xbf, .size = 8, .activate = 28 },
837
838 [SCARLETT2_CONFIG_SP_HP_MUTE] = {
839 .offset = 0xab, .size = 8, .activate = 10 },
840 }
841 };
842
843 /* Solo Gen 4 */
844 static const struct scarlett2_config_set scarlett2_config_set_gen4_solo = {
845 .notifications = scarlett4_solo_notifications,
846 .param_buf_addr = 0xd8,
847 .items = {
848 [SCARLETT2_CONFIG_MSD_SWITCH] = {
849 .offset = 0x47, .size = 8, .activate = 4 },
850
851 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
852 .offset = 0x108, .size = 8, .activate = 12, .pbuf = 1 },
853
854 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
855 .offset = 0x46, .size = 8, .activate = 9, .pbuf = 1,
856 .mute = 1 },
857
858 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
859 .offset = 0x3d, .size = 8, .activate = 10, .pbuf = 1,
860 .mute = 1 },
861
862 [SCARLETT2_CONFIG_AIR_SWITCH] = {
863 .offset = 0x3e, .size = 8, .activate = 11, .pbuf = 1 },
864
865 [SCARLETT2_CONFIG_PCM_INPUT_SWITCH] = {
866 .offset = 0x206, .size = 8, .activate = 25, .pbuf = 1 },
867
868 [SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN] = {
869 .offset = 0x232, .size = 16, .activate = 26 }
870 }
871 };
872
873 /* 2i2 Gen 4 */
874 static const struct scarlett2_config_set scarlett2_config_set_gen4_2i2 = {
875 .notifications = scarlett4_2i2_notifications,
876 .param_buf_addr = 0xfc,
877 .input_gain_tlv = db_scale_gen4_gain,
878 .autogain_status_texts = scarlett2_autogain_status_gen4,
879 .items = {
880 [SCARLETT2_CONFIG_MSD_SWITCH] = {
881 .offset = 0x49, .size = 8, .activate = 4 },
882
883 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
884 .offset = 0x14a, .size = 8, .activate = 16, .pbuf = 1 },
885
886 [SCARLETT2_CONFIG_AUTOGAIN_SWITCH] = {
887 .offset = 0x135, .size = 8, .activate = 10, .pbuf = 1 },
888
889 [SCARLETT2_CONFIG_AUTOGAIN_STATUS] = {
890 .offset = 0x137, .size = 8 },
891
892 [SCARLETT2_CONFIG_AG_MEAN_TARGET] = {
893 .offset = 0x131, .size = 8, .activate = 29, .pbuf = 1 },
894
895 [SCARLETT2_CONFIG_AG_PEAK_TARGET] = {
896 .offset = 0x132, .size = 8, .activate = 30, .pbuf = 1 },
897
898 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
899 .offset = 0x48, .size = 8, .activate = 11, .pbuf = 1,
900 .mute = 1 },
901
902 [SCARLETT2_CONFIG_INPUT_GAIN] = {
903 .offset = 0x4b, .size = 8, .activate = 12, .pbuf = 1 },
904
905 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
906 .offset = 0x3c, .size = 8, .activate = 13, .pbuf = 1,
907 .mute = 1 },
908
909 [SCARLETT2_CONFIG_SAFE_SWITCH] = {
910 .offset = 0x147, .size = 8, .activate = 14, .pbuf = 1 },
911
912 [SCARLETT2_CONFIG_AIR_SWITCH] = {
913 .offset = 0x3e, .size = 8, .activate = 15, .pbuf = 1 },
914
915 [SCARLETT2_CONFIG_INPUT_SELECT_SWITCH] = {
916 .offset = 0x14b, .size = 8, .activate = 17, .pbuf = 1 },
917
918 [SCARLETT2_CONFIG_INPUT_LINK_SWITCH] = {
919 .offset = 0x14e, .size = 8, .activate = 18, .pbuf = 1 },
920
921 [SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN] = {
922 .offset = 0x2a0, .size = 16, .activate = 36 }
923 }
924 };
925
926 /* 4i4 Gen 4 */
927 static const struct scarlett2_config_set scarlett2_config_set_gen4_4i4 = {
928 .notifications = scarlett4_4i4_notifications,
929 .param_buf_addr = 0x130,
930 .input_gain_tlv = db_scale_gen4_gain,
931 .autogain_status_texts = scarlett2_autogain_status_gen4,
932 .items = {
933 [SCARLETT2_CONFIG_MSD_SWITCH] = {
934 .offset = 0x5c, .size = 8, .activate = 4 },
935
936 [SCARLETT2_CONFIG_AUTOGAIN_SWITCH] = {
937 .offset = 0x13e, .size = 8, .activate = 10, .pbuf = 1 },
938
939 [SCARLETT2_CONFIG_AUTOGAIN_STATUS] = {
940 .offset = 0x140, .size = 8 },
941
942 [SCARLETT2_CONFIG_AG_MEAN_TARGET] = {
943 .offset = 0x13a, .size = 8, .activate = 23, .pbuf = 1 },
944
945 [SCARLETT2_CONFIG_AG_PEAK_TARGET] = {
946 .offset = 0x13b, .size = 8, .activate = 24, .pbuf = 1 },
947
948 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
949 .offset = 0x5a, .size = 8, .activate = 11, .pbuf = 1,
950 .mute = 1 },
951
952 [SCARLETT2_CONFIG_INPUT_GAIN] = {
953 .offset = 0x5e, .size = 8, .activate = 12, .pbuf = 1 },
954
955 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
956 .offset = 0x4e, .size = 8, .activate = 13, .pbuf = 1,
957 .mute = 1 },
958
959 [SCARLETT2_CONFIG_SAFE_SWITCH] = {
960 .offset = 0x150, .size = 8, .activate = 14, .pbuf = 1 },
961
962 [SCARLETT2_CONFIG_AIR_SWITCH] = {
963 .offset = 0x50, .size = 8, .activate = 15, .pbuf = 1 },
964
965 [SCARLETT2_CONFIG_INPUT_SELECT_SWITCH] = {
966 .offset = 0x153, .size = 8, .activate = 16, .pbuf = 1 },
967
968 [SCARLETT2_CONFIG_INPUT_LINK_SWITCH] = {
969 .offset = 0x156, .size = 8, .activate = 17, .pbuf = 1 },
970
971 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
972 .offset = 0x32, .size = 16 },
973
974 [SCARLETT2_CONFIG_HEADPHONE_VOLUME] = {
975 .offset = 0x3a, .size = 16 },
976
977 [SCARLETT2_CONFIG_POWER_EXT] = {
978 .offset = 0x168, .size = 8 },
979
980 [SCARLETT2_CONFIG_POWER_LOW] = {
981 .offset = 0x16d, .size = 8 }
982 }
983 };
984
985 /* Clarett USB and Clarett+ devices: 2Pre, 4Pre, 8Pre */
986 static const struct scarlett2_config_set scarlett2_config_set_clarett = {
987 .notifications = scarlett2_notifications,
988 .items = {
989 [SCARLETT2_CONFIG_DIM_MUTE] = {
990 .offset = 0x31, .size = 8, .activate = 2 },
991
992 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
993 .offset = 0x34, .size = 16, .activate = 1 },
994
995 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
996 .offset = 0x5c, .size = 8, .activate = 1 },
997
998 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
999 .offset = 0x66, .size = 8, .activate = 3 },
1000
1001 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
1002 .offset = 0x76, .size = 16 },
1003
1004 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
1005 .offset = 0x7c, .size = 8, .activate = 7 },
1006
1007 [SCARLETT2_CONFIG_AIR_SWITCH] = {
1008 .offset = 0x95, .size = 8, .activate = 8 },
1009
1010 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
1011 .offset = 0x8d, .size = 8, .activate = 6 },
1012
1013 [SCARLETT2_CONFIG_SPDIF_MODE] = {
1014 .offset = 0x9e, .size = 8, .activate = 4 },
1015 }
1016 };
1017
1018 /* Description of each hardware port type:
1019 * - id: hardware ID of this port type
1020 * - src_descr: printf format string for mux input selections
1021 * - src_num_offset: added to channel number for the fprintf
1022 * - dst_descr: printf format string for mixer controls
1023 */
1024 struct scarlett2_port {
1025 u16 id;
1026 const char * const src_descr;
1027 int src_num_offset;
1028 const char * const dst_descr;
1029 const char * const dsp_src_descr;
1030 const char * const dsp_dst_descr;
1031 };
1032
1033 static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
1034 [SCARLETT2_PORT_TYPE_NONE] = {
1035 .id = 0x000,
1036 .src_descr = "Off"
1037 },
1038 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
1039 .id = 0x080,
1040 .src_descr = "Analogue %d",
1041 .src_num_offset = 1,
1042 .dst_descr = "Analogue Output %02d Playback"
1043 },
1044 [SCARLETT2_PORT_TYPE_SPDIF] = {
1045 .id = 0x180,
1046 .src_descr = "S/PDIF %d",
1047 .src_num_offset = 1,
1048 .dst_descr = "S/PDIF Output %d Playback"
1049 },
1050 [SCARLETT2_PORT_TYPE_ADAT] = {
1051 .id = 0x200,
1052 .src_descr = "ADAT %d",
1053 .src_num_offset = 1,
1054 .dst_descr = "ADAT Output %d Playback"
1055 },
1056 [SCARLETT2_PORT_TYPE_MIX] = {
1057 .id = 0x300,
1058 .src_descr = "Mix %c",
1059 .src_num_offset = 'A',
1060 .dst_descr = "Mixer Input %02d Capture",
1061 .dsp_src_descr = "DSP %d",
1062 .dsp_dst_descr = "DSP Input %d Capture"
1063 },
1064 [SCARLETT2_PORT_TYPE_PCM] = {
1065 .id = 0x600,
1066 .src_descr = "PCM %d",
1067 .src_num_offset = 1,
1068 .dst_descr = "PCM %02d Capture"
1069 },
1070 };
1071
1072 /* Number of mux tables: one for each band of sample rates
1073 * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
1074 */
1075 #define SCARLETT2_MUX_TABLES 3
1076
1077 /* Maximum number of entries in a mux table */
1078 #define SCARLETT2_MAX_MUX_ENTRIES 10
1079
1080 /* One entry within mux_assignment defines the port type and range of
1081 * ports to add to the set_mux message. The end of the list is marked
1082 * with count == 0.
1083 */
1084 struct scarlett2_mux_entry {
1085 u8 port_type;
1086 u8 start;
1087 u8 count;
1088 };
1089
1090 /* Maximum number of entries in a mux table */
1091 #define SCARLETT2_MAX_METER_ENTRIES 9
1092
1093 /* One entry within meter_assignment defines the range of mux outputs
1094 * that consecutive meter entries are mapped to. The end of the list
1095 * is marked with count == 0.
1096 */
1097 struct scarlett2_meter_entry {
1098 u8 start;
1099 u8 count;
1100 };
1101
1102 struct scarlett2_device_info {
1103 /* which set of configuration parameters the device uses */
1104 const struct scarlett2_config_set *config_set;
1105
1106 /* minimum firmware version required */
1107 u16 min_firmware_version;
1108
1109 /* has a downloadable device map */
1110 u8 has_devmap;
1111
1112 /* support for main/alt speaker switching */
1113 u8 has_speaker_switching;
1114
1115 /* support for talkback microphone */
1116 u8 has_talkback;
1117
1118 /* the number of analogue inputs with a software switchable
1119 * level control that can be set to line or instrument
1120 */
1121 u8 level_input_count;
1122
1123 /* the first input with a level control (0-based) */
1124 u8 level_input_first;
1125
1126 /* the number of analogue inputs with a software switchable
1127 * 10dB pad control
1128 */
1129 u8 pad_input_count;
1130
1131 /* the number of analogue inputs with a software switchable
1132 * "air" control
1133 */
1134 u8 air_input_count;
1135
1136 /* the first input with an air control (0-based) */
1137 u8 air_input_first;
1138
1139 /* number of additional air options
1140 * 0 for air presence only (Gen 3)
1141 * 1 for air presence+drive (Gen 4)
1142 */
1143 u8 air_option;
1144
1145 /* the number of analogue inputs with DSP control */
1146 u8 dsp_input_count;
1147
1148 /* number of pre-compressor filters */
1149 u8 precomp_flt_count;
1150
1151 /* number of parametric EQ filters */
1152 u8 peq_flt_count;
1153
1154 /* number of PEQ filters plus unused slots */
1155 u8 peq_flt_total_count;
1156
1157 /* the number of analogue inputs with a software switchable
1158 * mute control
1159 */
1160 u8 mute_input_count;
1161
1162 /* the number of phantom (48V) software switchable controls */
1163 u8 phantom_count;
1164
1165 /* the first input with phantom power control (0-based) */
1166 u8 phantom_first;
1167
1168 /* the number of inputs each phantom switch controls */
1169 u8 inputs_per_phantom;
1170
1171 /* the number of inputs with software-controllable gain */
1172 u8 gain_input_count;
1173
1174 /* the number of inputs with safe mode */
1175 u8 safe_input_count;
1176
1177 /* the number of direct monitor options
1178 * (0 = none, 1 = mono only, 2 = mono/stereo)
1179 */
1180 u8 direct_monitor;
1181
1182 /* the number of DSP channels */
1183 u8 dsp_count;
1184
1185 /* has a Bluetooth module with volume control */
1186 u8 has_bluetooth;
1187
1188 /* S/PDIF Source/Digital I/O mode control */
1189 const char * const spdif_mode_control_name;
1190 const u8 *spdif_mode_values;
1191 const char * const *spdif_mode_texts;
1192
1193 /* remap analogue outputs; 18i8 Gen 3 has "line 3/4" connected
1194 * internally to the analogue 7/8 outputs
1195 */
1196 u8 line_out_remap_enable;
1197 u8 line_out_remap[SCARLETT2_ANALOGUE_MAX];
1198 u8 line_out_unmap[SCARLETT2_ANALOGUE_MAX];
1199
1200 /* additional description for the line out volume controls */
1201 const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
1202
1203 /* Vocaster speaker/headphone mute control names */
1204 const char * const *sp_hp_mute_names;
1205
1206 /* number of sources/destinations of each port type */
1207 const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];
1208
1209 /* layout/order of the entries in the set_mux message */
1210 struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES]
1211 [SCARLETT2_MAX_MUX_ENTRIES];
1212
1213 /* map from meter level order returned by
1214 * SCARLETT2_USB_GET_METER to index into mux[] entries (same
1215 * as the order returned by scarlett2_meter_ctl_get())
1216 */
1217 struct scarlett2_meter_entry meter_map[SCARLETT2_MAX_METER_ENTRIES];
1218 };
1219
1220 struct scarlett2_data {
1221 struct usb_mixer_interface *mixer;
1222 struct mutex usb_mutex; /* prevent sending concurrent USB requests */
1223 struct completion cmd_done;
1224 struct mutex data_mutex; /* lock access to this data */
1225 u8 running;
1226 u8 hwdep_in_use;
1227 u8 selected_flash_segment_id;
1228 u8 flash_write_state;
1229 struct delayed_work work;
1230 const struct scarlett2_device_info *info;
1231 const struct scarlett2_config_set *config_set;
1232 const char *series_name;
1233 __u8 bInterfaceNumber;
1234 __u8 bEndpointAddress;
1235 __u16 wMaxPacketSize;
1236 __u8 bInterval;
1237 u8 num_mux_srcs;
1238 u8 num_mux_dsts;
1239 u8 num_mix_in;
1240 u8 num_mix_out;
1241 u8 num_line_out;
1242 u8 num_monitor_mix_ctls;
1243 u8 num_autogain_status_texts;
1244 u32 firmware_version;
1245 u8 flash_segment_nums[SCARLETT2_SEGMENT_ID_COUNT];
1246 u8 flash_segment_blocks[SCARLETT2_SEGMENT_ID_COUNT];
1247 u16 scarlett2_seq;
1248 u8 sync_updated;
1249 u8 vol_updated;
1250 u8 dim_mute_updated;
1251 u8 input_level_updated;
1252 u8 input_pad_updated;
1253 u8 input_air_updated;
1254 u8 input_dsp_updated;
1255 u8 input_mute_updated;
1256 u8 input_phantom_updated;
1257 u8 input_select_updated;
1258 u8 input_gain_updated;
1259 u8 autogain_updated;
1260 u8 input_safe_updated;
1261 u8 pcm_input_switch_updated;
1262 u8 monitor_other_updated;
1263 u8 direct_monitor_updated;
1264 u8 mux_updated;
1265 u8 mix_updated;
1266 u8 speaker_switching_switched;
1267 u8 power_status_updated;
1268 u8 bluetooth_updated;
1269 u8 sync;
1270 u8 master_vol;
1271 u8 headphone_vol;
1272 u8 vol[SCARLETT2_ANALOGUE_MAX];
1273 u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
1274 u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
1275 u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
1276 u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
1277 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
1278 u8 sp_hp_mute;
1279 u8 air_switch[SCARLETT2_AIR_SWITCH_MAX];
1280 u8 dsp_switch[SCARLETT2_DSP_SWITCH_MAX];
1281 s32 compressor_values[SCARLETT2_COMPRESSOR_CTLS_MAX];
1282 s32 precomp_flt_values[SCARLETT2_PRECOMP_FLT_VALUES_MAX];
1283 s32 peq_flt_values[SCARLETT2_PEQ_FLT_VALUES_MAX];
1284 u8 precomp_flt_switch[SCARLETT2_DSP_SWITCH_MAX];
1285 u8 peq_flt_switch[SCARLETT2_DSP_SWITCH_MAX];
1286 u8 input_mute_switch[SCARLETT2_INPUT_MUTE_SWITCH_MAX];
1287 u8 phantom_switch[SCARLETT2_PHANTOM_SWITCH_MAX];
1288 u8 phantom_persistence;
1289 u8 input_select_switch;
1290 u8 input_link_switch[SCARLETT2_INPUT_GAIN_MAX];
1291 u8 gain[SCARLETT2_INPUT_GAIN_MAX];
1292 u8 autogain_switch[SCARLETT2_INPUT_GAIN_MAX];
1293 u8 autogain_status[SCARLETT2_INPUT_GAIN_MAX];
1294 s8 ag_targets[SCARLETT2_AG_TARGET_COUNT];
1295 u8 safe_switch[SCARLETT2_INPUT_GAIN_MAX];
1296 u8 pcm_input_switch;
1297 u8 direct_monitor_switch;
1298 u8 speaker_switching_switch;
1299 u8 talkback_switch;
1300 u8 talkback_map[SCARLETT2_OUTPUT_MIX_MAX];
1301 u8 msd_switch;
1302 u8 standalone_switch;
1303 u8 power_status;
1304 u8 bluetooth_volume;
1305 u8 spdif_mode;
1306 u8 meter_level_map[SCARLETT2_MAX_METERS];
1307 struct snd_kcontrol *sync_ctl;
1308 struct snd_kcontrol *master_vol_ctl;
1309 struct snd_kcontrol *headphone_vol_ctl;
1310 struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
1311 struct snd_kcontrol *sw_hw_ctls[SCARLETT2_ANALOGUE_MAX];
1312 struct snd_kcontrol *mute_ctls[SCARLETT2_ANALOGUE_MAX];
1313 struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT];
1314 struct snd_kcontrol *level_ctls[SCARLETT2_LEVEL_SWITCH_MAX];
1315 struct snd_kcontrol *pad_ctls[SCARLETT2_PAD_SWITCH_MAX];
1316 struct snd_kcontrol *air_ctls[SCARLETT2_AIR_SWITCH_MAX];
1317 struct snd_kcontrol *dsp_ctls[SCARLETT2_DSP_SWITCH_MAX];
1318 struct snd_kcontrol *input_mute_ctls[SCARLETT2_INPUT_MUTE_SWITCH_MAX];
1319 struct snd_kcontrol *phantom_ctls[SCARLETT2_PHANTOM_SWITCH_MAX];
1320 struct snd_kcontrol *input_select_ctl;
1321 struct snd_kcontrol *input_link_ctls[SCARLETT2_INPUT_GAIN_MAX];
1322 struct snd_kcontrol *input_gain_ctls[SCARLETT2_INPUT_GAIN_MAX];
1323 struct snd_kcontrol *autogain_ctls[SCARLETT2_INPUT_GAIN_MAX];
1324 struct snd_kcontrol *autogain_status_ctls[SCARLETT2_INPUT_GAIN_MAX];
1325 struct snd_kcontrol *ag_target_ctls[SCARLETT2_AG_TARGET_COUNT];
1326 struct snd_kcontrol *safe_ctls[SCARLETT2_INPUT_GAIN_MAX];
1327 struct snd_kcontrol *pcm_input_switch_ctl;
1328 struct snd_kcontrol *mux_ctls[SCARLETT2_MUX_MAX];
1329 struct snd_kcontrol *mix_ctls[SCARLETT2_MIX_MAX];
1330 struct snd_kcontrol *compressor_ctls[SCARLETT2_COMPRESSOR_CTLS_MAX];
1331 struct snd_kcontrol *precomp_flt_switch_ctls[SCARLETT2_DSP_SWITCH_MAX];
1332 struct snd_kcontrol *peq_flt_switch_ctls[SCARLETT2_DSP_SWITCH_MAX];
1333 struct snd_kcontrol *direct_monitor_ctl;
1334 struct snd_kcontrol *speaker_switching_ctl;
1335 struct snd_kcontrol *talkback_ctl;
1336 struct snd_kcontrol *power_status_ctl;
1337 struct snd_kcontrol *bluetooth_volume_ctl;
1338 u8 mux[SCARLETT2_MUX_MAX];
1339 u8 mix[SCARLETT2_MIX_MAX];
1340 u8 monitor_mix[SCARLETT2_MONITOR_MIX_MAX];
1341 };
1342
1343 /*** Model-specific data ***/
1344
1345 static const struct scarlett2_device_info s6i6_gen2_info = {
1346 .config_set = &scarlett2_config_set_gen2a,
1347 .level_input_count = 2,
1348 .pad_input_count = 2,
1349
1350 .line_out_descrs = {
1351 "Headphones 1 L",
1352 "Headphones 1 R",
1353 "Headphones 2 L",
1354 "Headphones 2 R",
1355 },
1356
1357 .port_count = {
1358 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1359 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
1360 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1361 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1362 [SCARLETT2_PORT_TYPE_PCM] = { 6, 6 },
1363 },
1364
1365 .mux_assignment = { {
1366 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1367 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1368 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1369 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1370 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1371 { 0, 0, 0 },
1372 }, {
1373 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1374 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1375 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1376 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1377 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1378 { 0, 0, 0 },
1379 }, {
1380 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1381 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1382 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1383 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1384 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1385 { 0, 0, 0 },
1386 } },
1387
1388 .meter_map = {
1389 { 24, 6 },
1390 { 0, 24 },
1391 { 0, 0 },
1392 }
1393 };
1394
1395 static const struct scarlett2_device_info s18i8_gen2_info = {
1396 .config_set = &scarlett2_config_set_gen2a,
1397 .level_input_count = 2,
1398 .pad_input_count = 4,
1399
1400 .line_out_descrs = {
1401 "Monitor L",
1402 "Monitor R",
1403 "Headphones 1 L",
1404 "Headphones 1 R",
1405 "Headphones 2 L",
1406 "Headphones 2 R",
1407 },
1408
1409 .port_count = {
1410 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1411 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 },
1412 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1413 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
1414 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1415 [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 },
1416 },
1417
1418 .mux_assignment = { {
1419 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
1420 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1421 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1422 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1423 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1424 { 0, 0, 0 },
1425 }, {
1426 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
1427 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1428 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1429 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1430 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1431 { 0, 0, 0 },
1432 }, {
1433 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1434 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1435 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1436 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1437 { SCARLETT2_PORT_TYPE_NONE, 0, 4 },
1438 { 0, 0, 0 },
1439 } },
1440
1441 .meter_map = {
1442 { 26, 18 },
1443 { 0, 26 },
1444 { 0, 0 },
1445 }
1446 };
1447
1448 static const struct scarlett2_device_info s18i20_gen2_info = {
1449 .config_set = &scarlett2_config_set_gen2b,
1450
1451 .line_out_descrs = {
1452 "Monitor L",
1453 "Monitor R",
1454 NULL,
1455 NULL,
1456 NULL,
1457 NULL,
1458 "Headphones 1 L",
1459 "Headphones 1 R",
1460 "Headphones 2 L",
1461 "Headphones 2 R",
1462 },
1463
1464 .port_count = {
1465 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1466 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
1467 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1468 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
1469 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1470 [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
1471 },
1472
1473 .mux_assignment = { {
1474 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
1475 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1476 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1477 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
1478 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1479 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1480 { 0, 0, 0 },
1481 }, {
1482 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
1483 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1484 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1485 { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
1486 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1487 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1488 { 0, 0, 0 },
1489 }, {
1490 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1491 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1492 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1493 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1494 { SCARLETT2_PORT_TYPE_NONE, 0, 6 },
1495 { 0, 0, 0 },
1496 } },
1497
1498 .meter_map = {
1499 { 38, 18 },
1500 { 0, 38 },
1501 { 0, 0 },
1502 }
1503 };
1504
1505 static const struct scarlett2_device_info solo_gen3_info = {
1506 .config_set = &scarlett2_config_set_gen3a,
1507 .level_input_count = 1,
1508 .level_input_first = 1,
1509 .air_input_count = 1,
1510 .phantom_count = 1,
1511 .inputs_per_phantom = 1,
1512 .direct_monitor = 1,
1513 };
1514
1515 static const struct scarlett2_device_info s2i2_gen3_info = {
1516 .config_set = &scarlett2_config_set_gen3a,
1517 .level_input_count = 2,
1518 .air_input_count = 2,
1519 .phantom_count = 1,
1520 .inputs_per_phantom = 2,
1521 .direct_monitor = 2,
1522 };
1523
1524 static const struct scarlett2_device_info s4i4_gen3_info = {
1525 .config_set = &scarlett2_config_set_gen3b,
1526 .level_input_count = 2,
1527 .pad_input_count = 2,
1528 .air_input_count = 2,
1529 .phantom_count = 1,
1530 .inputs_per_phantom = 2,
1531
1532 .line_out_descrs = {
1533 "Monitor L",
1534 "Monitor R",
1535 "Headphones L",
1536 "Headphones R",
1537 },
1538
1539 .port_count = {
1540 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1541 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
1542 [SCARLETT2_PORT_TYPE_MIX] = { 6, 8 },
1543 [SCARLETT2_PORT_TYPE_PCM] = { 4, 6 },
1544 },
1545
1546 .mux_assignment = { {
1547 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1548 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1549 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1550 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
1551 { 0, 0, 0 },
1552 }, {
1553 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1554 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1555 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1556 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
1557 { 0, 0, 0 },
1558 }, {
1559 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1560 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1561 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1562 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
1563 { 0, 0, 0 },
1564 } },
1565
1566 .meter_map = {
1567 { 12, 6 },
1568 { 0, 12 },
1569 { 0, 0 },
1570 }
1571 };
1572
1573 static const struct scarlett2_device_info s8i6_gen3_info = {
1574 .config_set = &scarlett2_config_set_gen3b,
1575 .level_input_count = 2,
1576 .pad_input_count = 2,
1577 .air_input_count = 2,
1578 .phantom_count = 1,
1579 .inputs_per_phantom = 2,
1580
1581 .line_out_descrs = {
1582 "Headphones 1 L",
1583 "Headphones 1 R",
1584 "Headphones 2 L",
1585 "Headphones 2 R",
1586 },
1587
1588 .port_count = {
1589 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1590 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6, 4 },
1591 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1592 [SCARLETT2_PORT_TYPE_MIX] = { 8, 8 },
1593 [SCARLETT2_PORT_TYPE_PCM] = { 6, 10 },
1594 },
1595
1596 .mux_assignment = { {
1597 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1598 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1599 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1600 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1601 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1602 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
1603 { 0, 0, 0 },
1604 }, {
1605 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1606 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1607 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1608 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1609 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1610 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
1611 { 0, 0, 0 },
1612 }, {
1613 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1614 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1615 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1616 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1617 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1618 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
1619 { 0, 0, 0 },
1620 } },
1621
1622 .meter_map = {
1623 { 14, 8 },
1624 { 0, 6 },
1625 { 22, 2 },
1626 { 6, 8 },
1627 { 0, 0 },
1628 }
1629 };
1630
1631 static const u8 scarlett2_spdif_s18i8_gen3_values[] = { 0, 2, 0xff };
1632
1633 static const char * const scarlett2_spdif_s18i8_gen3_texts[] = {
1634 "RCA",
1635 "Optical",
1636 NULL
1637 };
1638
1639 static const struct scarlett2_device_info s18i8_gen3_info = {
1640 .config_set = &scarlett2_config_set_gen3c,
1641 .has_speaker_switching = 1,
1642 .level_input_count = 2,
1643 .pad_input_count = 4,
1644 .air_input_count = 4,
1645 .phantom_count = 2,
1646 .inputs_per_phantom = 2,
1647
1648 .spdif_mode_control_name = "S/PDIF Mode Capture Enum",
1649 .spdif_mode_values = scarlett2_spdif_s18i8_gen3_values,
1650 .spdif_mode_texts = scarlett2_spdif_s18i8_gen3_texts,
1651
1652 .line_out_remap_enable = 1,
1653 .line_out_remap = { 0, 1, 6, 7, 2, 3, 4, 5 },
1654 .line_out_unmap = { 0, 1, 4, 5, 6, 7, 2, 3 },
1655
1656 .line_out_descrs = {
1657 "Monitor L",
1658 "Monitor R",
1659 "Alt Monitor L",
1660 "Alt Monitor R",
1661 "Headphones 1 L",
1662 "Headphones 1 R",
1663 "Headphones 2 L",
1664 "Headphones 2 R",
1665 },
1666
1667 .port_count = {
1668 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1669 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 8 },
1670 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1671 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
1672 [SCARLETT2_PORT_TYPE_MIX] = { 10, 20 },
1673 [SCARLETT2_PORT_TYPE_PCM] = { 8, 20 },
1674 },
1675
1676 .mux_assignment = { {
1677 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1678 { SCARLETT2_PORT_TYPE_PCM, 12, 8 },
1679 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1680 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
1681 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
1682 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1683 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
1684 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
1685 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
1686 { 0, 0, 0 },
1687 }, {
1688 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1689 { SCARLETT2_PORT_TYPE_PCM, 12, 4 },
1690 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1691 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
1692 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
1693 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1694 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
1695 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
1696 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
1697 { 0, 0, 0 },
1698 }, {
1699 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1700 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1701 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
1702 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
1703 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1704 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
1705 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
1706 { 0, 0, 0 },
1707 } },
1708
1709 .meter_map = {
1710 { 30, 10 },
1711 { 42, 8 },
1712 { 0, 2 },
1713 { 6, 2 },
1714 { 2, 4 },
1715 { 8, 2 },
1716 { 40, 2 },
1717 { 10, 20 },
1718 { 0, 0 }
1719 }
1720 };
1721
1722 static const u8 scarlett2_spdif_s18i20_gen3_values[] = { 0, 6, 1, 0xff };
1723
1724 static const char * const scarlett2_spdif_s18i20_gen3_texts[] = {
1725 "S/PDIF RCA",
1726 "S/PDIF Optical",
1727 "Dual ADAT",
1728 NULL
1729 };
1730
1731 static const struct scarlett2_device_info s18i20_gen3_info = {
1732 .config_set = &scarlett2_config_set_gen3c,
1733 .has_speaker_switching = 1,
1734 .has_talkback = 1,
1735 .level_input_count = 2,
1736 .pad_input_count = 8,
1737 .air_input_count = 8,
1738 .phantom_count = 2,
1739 .inputs_per_phantom = 4,
1740
1741 .spdif_mode_control_name = "Digital I/O Mode Capture Enum",
1742 .spdif_mode_values = scarlett2_spdif_s18i20_gen3_values,
1743 .spdif_mode_texts = scarlett2_spdif_s18i20_gen3_texts,
1744
1745 .line_out_descrs = {
1746 "Monitor 1 L",
1747 "Monitor 1 R",
1748 "Monitor 2 L",
1749 "Monitor 2 R",
1750 NULL,
1751 NULL,
1752 "Headphones 1 L",
1753 "Headphones 1 R",
1754 "Headphones 2 L",
1755 "Headphones 2 R",
1756 },
1757
1758 .port_count = {
1759 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1760 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 9, 10 },
1761 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1762 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
1763 [SCARLETT2_PORT_TYPE_MIX] = { 12, 25 },
1764 [SCARLETT2_PORT_TYPE_PCM] = { 20, 20 },
1765 },
1766
1767 .mux_assignment = { {
1768 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1769 { SCARLETT2_PORT_TYPE_PCM, 10, 10 },
1770 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1771 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1772 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
1773 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1774 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
1775 { SCARLETT2_PORT_TYPE_NONE, 0, 12 },
1776 { 0, 0, 0 },
1777 }, {
1778 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1779 { SCARLETT2_PORT_TYPE_PCM, 10, 8 },
1780 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1781 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1782 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
1783 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1784 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
1785 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
1786 { 0, 0, 0 },
1787 }, {
1788 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1789 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1790 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1791 { SCARLETT2_PORT_TYPE_NONE, 0, 24 },
1792 { 0, 0, 0 },
1793 } },
1794
1795 .meter_map = {
1796 { 45, 8 },
1797 { 55, 10 },
1798 { 0, 20 },
1799 { 53, 2 },
1800 { 20, 25 },
1801 { 0, 0 },
1802 }
1803 };
1804
1805 static const struct scarlett2_device_info vocaster_one_info = {
1806 .config_set = &scarlett2_config_set_vocaster,
1807 .min_firmware_version = 1769,
1808 .has_devmap = 1,
1809
1810 .phantom_count = 1,
1811 .inputs_per_phantom = 1,
1812 .dsp_count = 1,
1813 .dsp_input_count = 1,
1814 .precomp_flt_count = 2,
1815 .peq_flt_count = 3,
1816 .peq_flt_total_count = 4,
1817 .mute_input_count = 1,
1818 .gain_input_count = 1,
1819 .sp_hp_mute_names = vocaster_one_sp_hp_mute_names,
1820
1821 .port_count = {
1822 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1823 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 2, 4 },
1824 [SCARLETT2_PORT_TYPE_MIX] = { 9, 9 },
1825 [SCARLETT2_PORT_TYPE_PCM] = { 4, 10 },
1826 },
1827
1828 .mux_assignment = { {
1829 { SCARLETT2_PORT_TYPE_MIX, 8, 1 },
1830 { SCARLETT2_PORT_TYPE_PCM, 5, 5 },
1831 { SCARLETT2_PORT_TYPE_MIX, 6, 2 },
1832 { SCARLETT2_PORT_TYPE_PCM, 0, 5 },
1833 { SCARLETT2_PORT_TYPE_MIX, 0, 6 },
1834 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1835 { 0, 0, 0 },
1836 } },
1837
1838 .meter_map = {
1839 { 12, 1 },
1840 { 18, 5 },
1841 { 10, 2 },
1842 { 13, 5 },
1843 { 4, 6 },
1844 { 0, 4 },
1845 { 0, 0 }
1846 }
1847 };
1848
1849 static const struct scarlett2_device_info vocaster_two_info = {
1850 .config_set = &scarlett2_config_set_vocaster,
1851 .min_firmware_version = 1769,
1852 .has_devmap = 1,
1853
1854 .phantom_count = 2,
1855 .inputs_per_phantom = 1,
1856 .dsp_count = 2,
1857 .dsp_input_count = 2,
1858 .precomp_flt_count = 2,
1859 .peq_flt_count = 3,
1860 .peq_flt_total_count = 4,
1861 .mute_input_count = 2,
1862 .gain_input_count = 2,
1863 .has_bluetooth = 1,
1864 .sp_hp_mute_names = vocaster_two_sp_hp_mute_names,
1865
1866 .port_count = {
1867 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1868 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6, 6 },
1869 [SCARLETT2_PORT_TYPE_MIX] = { 12, 14 },
1870 [SCARLETT2_PORT_TYPE_PCM] = { 4, 14 },
1871 },
1872
1873 .mux_assignment = { {
1874 { SCARLETT2_PORT_TYPE_MIX, 12, 2 },
1875 { SCARLETT2_PORT_TYPE_PCM, 6, 8 },
1876 { SCARLETT2_PORT_TYPE_MIX, 10, 2 },
1877 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1878 { SCARLETT2_PORT_TYPE_MIX, 0, 10 },
1879 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1880 { 0, 0, 0 },
1881 } },
1882
1883 .meter_map = {
1884 { 18, 2 },
1885 { 26, 8 },
1886 { 16, 2 },
1887 { 20, 6 },
1888 { 6, 10 },
1889 { 0, 6 },
1890 { 0, 0 }
1891 }
1892 };
1893
1894 static const struct scarlett2_device_info solo_gen4_info = {
1895 .config_set = &scarlett2_config_set_gen4_solo,
1896 .min_firmware_version = 2115,
1897 .has_devmap = 1,
1898
1899 .level_input_count = 1,
1900 .air_input_count = 1,
1901 .air_input_first = 1,
1902 .air_option = 1,
1903 .phantom_count = 1,
1904 .phantom_first = 1,
1905 .inputs_per_phantom = 1,
1906 .direct_monitor = 1,
1907 .dsp_count = 2,
1908
1909 .port_count = {
1910 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1911 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 2, 2 },
1912 [SCARLETT2_PORT_TYPE_MIX] = { 8, 6 },
1913 [SCARLETT2_PORT_TYPE_PCM] = { 2, 4 },
1914 },
1915
1916 .mux_assignment = { {
1917 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1918 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1919 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1920 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1921 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1922 { 0, 0, 0 },
1923 }, {
1924 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1925 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1926 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1927 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1928 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1929 { 0, 0, 0 },
1930 }, {
1931 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1932 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1933 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1934 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1935 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1936 { 0, 0, 0 },
1937 } },
1938
1939 .meter_map = {
1940 { 6, 2 },
1941 { 4, 2 },
1942 { 8, 4 },
1943 { 2, 2 },
1944 { 0, 2 },
1945 { 0, 0 }
1946 }
1947 };
1948
1949 static const struct scarlett2_device_info s2i2_gen4_info = {
1950 .config_set = &scarlett2_config_set_gen4_2i2,
1951 .min_firmware_version = 2115,
1952 .has_devmap = 1,
1953
1954 .level_input_count = 2,
1955 .air_input_count = 2,
1956 .air_option = 1,
1957 .phantom_count = 1,
1958 .inputs_per_phantom = 2,
1959 .gain_input_count = 2,
1960 .safe_input_count = 2,
1961 .direct_monitor = 2,
1962 .dsp_count = 2,
1963
1964 .port_count = {
1965 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1966 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 2, 2 },
1967 [SCARLETT2_PORT_TYPE_MIX] = { 6, 6 },
1968 [SCARLETT2_PORT_TYPE_PCM] = { 2, 4 },
1969 },
1970
1971 .mux_assignment = { {
1972 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1973 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1974 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1975 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1976 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1977 { 0, 0, 0 },
1978 }, {
1979 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1980 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1981 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1982 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1983 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1984 { 0, 0, 0 },
1985 }, {
1986 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1987 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1988 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1989 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1990 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1991 { 0, 0, 0 },
1992 } },
1993
1994 .meter_map = {
1995 { 6, 2 },
1996 { 4, 2 },
1997 { 8, 4 },
1998 { 2, 2 },
1999 { 0, 2 },
2000 { 0, 0 }
2001 }
2002 };
2003
2004 static const struct scarlett2_device_info s4i4_gen4_info = {
2005 .config_set = &scarlett2_config_set_gen4_4i4,
2006 .min_firmware_version = 2089,
2007 .has_devmap = 1,
2008
2009 .level_input_count = 2,
2010 .air_input_count = 2,
2011 .air_option = 1,
2012 .phantom_count = 2,
2013 .inputs_per_phantom = 1,
2014 .gain_input_count = 2,
2015 .safe_input_count = 2,
2016 .dsp_count = 2,
2017
2018 .port_count = {
2019 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
2020 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 6 },
2021 [SCARLETT2_PORT_TYPE_MIX] = { 8, 12 },
2022 [SCARLETT2_PORT_TYPE_PCM] = { 6, 6 },
2023 },
2024
2025 .mux_assignment = { {
2026 { SCARLETT2_PORT_TYPE_MIX, 10, 2 },
2027 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
2028 { SCARLETT2_PORT_TYPE_MIX, 0, 10 },
2029 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
2030 { 0, 0, 0 },
2031 }, {
2032 { SCARLETT2_PORT_TYPE_MIX, 10, 2 },
2033 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
2034 { SCARLETT2_PORT_TYPE_MIX, 0, 10 },
2035 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
2036 { 0, 0, 0 },
2037 }, {
2038 { SCARLETT2_PORT_TYPE_MIX, 10, 2 },
2039 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
2040 { SCARLETT2_PORT_TYPE_MIX, 0, 10 },
2041 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
2042 { 0, 0, 0 },
2043 } },
2044
2045 .meter_map = {
2046 { 16, 8 },
2047 { 6, 10 },
2048 { 0, 6 },
2049 { 0, 0 }
2050 }
2051 };
2052
2053 static const struct scarlett2_device_info clarett_2pre_info = {
2054 .config_set = &scarlett2_config_set_clarett,
2055 .level_input_count = 2,
2056 .air_input_count = 2,
2057
2058 .line_out_descrs = {
2059 "Monitor L",
2060 "Monitor R",
2061 "Headphones L",
2062 "Headphones R",
2063 },
2064
2065 .port_count = {
2066 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
2067 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 2, 4 },
2068 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 0 },
2069 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
2070 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
2071 [SCARLETT2_PORT_TYPE_PCM] = { 4, 12 },
2072 },
2073
2074 .mux_assignment = { {
2075 { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
2076 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
2077 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
2078 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
2079 { 0, 0, 0 },
2080 }, {
2081 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
2082 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
2083 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
2084 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
2085 { 0, 0, 0 },
2086 }, {
2087 { SCARLETT2_PORT_TYPE_PCM, 0, 2 },
2088 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
2089 { SCARLETT2_PORT_TYPE_NONE, 0, 26 },
2090 { 0, 0, 0 },
2091 } },
2092
2093 .meter_map = {
2094 { 22, 12 },
2095 { 0, 22 },
2096 { 0, 0 }
2097 }
2098 };
2099
2100 static const u8 scarlett2_spdif_clarett_values[] = { 0, 1, 2, 0xff };
2101
2102 static const char * const scarlett2_spdif_clarett_texts[] = {
2103 "None",
2104 "Optical",
2105 "RCA",
2106 NULL
2107 };
2108
2109 static const struct scarlett2_device_info clarett_4pre_info = {
2110 .config_set = &scarlett2_config_set_clarett,
2111 .level_input_count = 2,
2112 .air_input_count = 4,
2113
2114 .spdif_mode_control_name = "S/PDIF Source Capture Enum",
2115 .spdif_mode_values = scarlett2_spdif_clarett_values,
2116 .spdif_mode_texts = scarlett2_spdif_clarett_texts,
2117
2118 .line_out_descrs = {
2119 "Monitor L",
2120 "Monitor R",
2121 "Headphones 1 L",
2122 "Headphones 1 R",
2123 "Headphones 2 L",
2124 "Headphones 2 R",
2125 },
2126
2127 .port_count = {
2128 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
2129 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 },
2130 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
2131 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
2132 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
2133 [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 },
2134 },
2135
2136 .mux_assignment = { {
2137 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
2138 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
2139 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
2140 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
2141 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
2142 { 0, 0, 0 },
2143 }, {
2144 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
2145 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
2146 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
2147 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
2148 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
2149 { 0, 0, 0 },
2150 }, {
2151 { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
2152 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
2153 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
2154 { SCARLETT2_PORT_TYPE_NONE, 0, 24 },
2155 { 0, 0, 0 },
2156 } },
2157
2158 .meter_map = {
2159 { 26, 18 },
2160 { 0, 26 },
2161 { 0, 0 }
2162 }
2163 };
2164
2165 static const struct scarlett2_device_info clarett_8pre_info = {
2166 .config_set = &scarlett2_config_set_clarett,
2167 .level_input_count = 2,
2168 .air_input_count = 8,
2169
2170 .spdif_mode_control_name = "S/PDIF Source Capture Enum",
2171 .spdif_mode_values = scarlett2_spdif_clarett_values,
2172 .spdif_mode_texts = scarlett2_spdif_clarett_texts,
2173
2174 .line_out_descrs = {
2175 "Monitor L",
2176 "Monitor R",
2177 NULL,
2178 NULL,
2179 NULL,
2180 NULL,
2181 "Headphones 1 L",
2182 "Headphones 1 R",
2183 "Headphones 2 L",
2184 "Headphones 2 R",
2185 },
2186
2187 .port_count = {
2188 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
2189 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
2190 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
2191 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
2192 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
2193 [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
2194 },
2195
2196 .mux_assignment = { {
2197 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
2198 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
2199 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
2200 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
2201 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
2202 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
2203 { 0, 0, 0 },
2204 }, {
2205 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
2206 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
2207 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
2208 { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
2209 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
2210 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
2211 { 0, 0, 0 },
2212 }, {
2213 { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
2214 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
2215 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
2216 { SCARLETT2_PORT_TYPE_NONE, 0, 22 },
2217 { 0, 0, 0 },
2218 } },
2219
2220 .meter_map = {
2221 { 38, 18 },
2222 { 0, 38 },
2223 { 0, 0 }
2224 }
2225 };
2226
2227 struct scarlett2_device_entry {
2228 const u32 usb_id; /* USB device identifier */
2229 const struct scarlett2_device_info *info;
2230 const char *series_name;
2231 };
2232
2233 static const struct scarlett2_device_entry scarlett2_devices[] = {
2234 /* Supported Gen 2 devices */
2235 { USB_ID(0x1235, 0x8203), &s6i6_gen2_info, "Scarlett Gen 2" },
2236 { USB_ID(0x1235, 0x8204), &s18i8_gen2_info, "Scarlett Gen 2" },
2237 { USB_ID(0x1235, 0x8201), &s18i20_gen2_info, "Scarlett Gen 2" },
2238
2239 /* Supported Gen 3 devices */
2240 { USB_ID(0x1235, 0x8211), &solo_gen3_info, "Scarlett Gen 3" },
2241 { USB_ID(0x1235, 0x8210), &s2i2_gen3_info, "Scarlett Gen 3" },
2242 { USB_ID(0x1235, 0x8212), &s4i4_gen3_info, "Scarlett Gen 3" },
2243 { USB_ID(0x1235, 0x8213), &s8i6_gen3_info, "Scarlett Gen 3" },
2244 { USB_ID(0x1235, 0x8214), &s18i8_gen3_info, "Scarlett Gen 3" },
2245 { USB_ID(0x1235, 0x8215), &s18i20_gen3_info, "Scarlett Gen 3" },
2246
2247 /* Supported Vocaster devices */
2248 { USB_ID(0x1235, 0x8216), &vocaster_one_info, "Vocaster" },
2249 { USB_ID(0x1235, 0x8217), &vocaster_two_info, "Vocaster" },
2250
2251 /* Supported Gen 4 devices */
2252 { USB_ID(0x1235, 0x8218), &solo_gen4_info, "Scarlett Gen 4" },
2253 { USB_ID(0x1235, 0x8219), &s2i2_gen4_info, "Scarlett Gen 4" },
2254 { USB_ID(0x1235, 0x821a), &s4i4_gen4_info, "Scarlett Gen 4" },
2255
2256 /* Supported Clarett USB/Clarett+ devices */
2257 { USB_ID(0x1235, 0x8206), &clarett_2pre_info, "Clarett USB" },
2258 { USB_ID(0x1235, 0x8207), &clarett_4pre_info, "Clarett USB" },
2259 { USB_ID(0x1235, 0x8208), &clarett_8pre_info, "Clarett USB" },
2260 { USB_ID(0x1235, 0x820a), &clarett_2pre_info, "Clarett+" },
2261 { USB_ID(0x1235, 0x820b), &clarett_4pre_info, "Clarett+" },
2262 { USB_ID(0x1235, 0x820c), &clarett_8pre_info, "Clarett+" },
2263
2264 /* End of list */
2265 { 0, NULL, NULL },
2266 };
2267
2268 /* 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)2269 static int scarlett2_get_port_start_num(
2270 const int port_count[][SCARLETT2_PORT_DIRNS],
2271 int direction, int port_type)
2272 {
2273 int i, num = 0;
2274
2275 for (i = 0; i < port_type; i++)
2276 num += port_count[i][direction];
2277
2278 return num;
2279 }
2280
2281 /*** USB Interactions ***/
2282
2283 /* Commands for sending/receiving requests/responses */
2284 #define SCARLETT2_USB_CMD_INIT 0
2285 #define SCARLETT2_USB_CMD_REQ 2
2286 #define SCARLETT2_USB_CMD_RESP 3
2287
2288 #define SCARLETT2_USB_INIT_1 0x00000000
2289 #define SCARLETT2_USB_INIT_2 0x00000002
2290 #define SCARLETT2_USB_REBOOT 0x00000003
2291 #define SCARLETT2_USB_GET_METER 0x00001001
2292 #define SCARLETT2_USB_GET_MIX 0x00002001
2293 #define SCARLETT2_USB_SET_MIX 0x00002002
2294 #define SCARLETT2_USB_GET_MUX 0x00003001
2295 #define SCARLETT2_USB_SET_MUX 0x00003002
2296 #define SCARLETT2_USB_INFO_FLASH 0x00004000
2297 #define SCARLETT2_USB_INFO_SEGMENT 0x00004001
2298 #define SCARLETT2_USB_ERASE_SEGMENT 0x00004002
2299 #define SCARLETT2_USB_GET_ERASE 0x00004003
2300 #define SCARLETT2_USB_WRITE_SEGMENT 0x00004004
2301 #define SCARLETT2_USB_READ_SEGMENT 0x00004005
2302 #define SCARLETT2_USB_GET_SYNC 0x00006004
2303 #define SCARLETT2_USB_GET_DATA 0x00800000
2304 #define SCARLETT2_USB_SET_DATA 0x00800001
2305 #define SCARLETT2_USB_DATA_CMD 0x00800002
2306 #define SCARLETT2_USB_INFO_DEVMAP 0x0080000c
2307 #define SCARLETT2_USB_GET_DEVMAP 0x0080000d
2308
2309 #define SCARLETT2_USB_CONFIG_SAVE 6
2310
2311 #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
2312
2313 #define SCARLETT2_FLASH_BLOCK_SIZE 4096
2314 #define SCARLETT2_FLASH_RW_MAX 1024
2315 #define SCARLETT2_SEGMENT_NUM_MIN 1
2316 #define SCARLETT2_SEGMENT_NUM_MAX 4
2317
2318 #define SCARLETT2_SEGMENT_SETTINGS_NAME "App_Settings"
2319 #define SCARLETT2_SEGMENT_FIRMWARE_NAME "App_Upgrade"
2320
2321 /* Gen 4 device firmware provides access to a base64-encoded
2322 * zlib-compressed JSON description of the device's capabilities and
2323 * configuration. This device map is made available in
2324 * /proc/asound/cardX/device-map.json.zz.b64
2325 */
2326 #define SCARLETT2_DEVMAP_BLOCK_SIZE 1024
2327 #define SCARLETT2_DEVMAP_FILENAME "device-map.json.zz.b64"
2328
2329 /* proprietary request/response format */
2330 struct scarlett2_usb_packet {
2331 __le32 cmd;
2332 __le16 size;
2333 __le16 seq;
2334 __le32 error;
2335 __le32 pad;
2336 u8 data[];
2337 };
2338
scarlett2_fill_request_header(struct scarlett2_data * private,struct scarlett2_usb_packet * req,u32 cmd,u16 req_size)2339 static void scarlett2_fill_request_header(struct scarlett2_data *private,
2340 struct scarlett2_usb_packet *req,
2341 u32 cmd, u16 req_size)
2342 {
2343 /* sequence must go up by 1 for each request */
2344 u16 seq = private->scarlett2_seq++;
2345
2346 req->cmd = cpu_to_le32(cmd);
2347 req->size = cpu_to_le16(req_size);
2348 req->seq = cpu_to_le16(seq);
2349 req->error = 0;
2350 req->pad = 0;
2351 }
2352
scarlett2_usb_tx(struct usb_device * dev,int interface,void * buf,u16 size)2353 static int scarlett2_usb_tx(struct usb_device *dev, int interface,
2354 void *buf, u16 size)
2355 {
2356 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
2357 SCARLETT2_USB_CMD_REQ,
2358 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
2359 0, interface, buf, size);
2360 }
2361
scarlett2_usb_rx(struct usb_device * dev,int interface,u32 usb_req,void * buf,u16 size)2362 static int scarlett2_usb_rx(struct usb_device *dev, int interface,
2363 u32 usb_req, void *buf, u16 size)
2364 {
2365 return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
2366 usb_req,
2367 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
2368 0, interface, buf, size);
2369 }
2370
2371 /* 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)2372 static int scarlett2_usb(
2373 struct usb_mixer_interface *mixer, u32 cmd,
2374 void *req_data, u16 req_size, void *resp_data, u16 resp_size)
2375 {
2376 struct scarlett2_data *private = mixer->private_data;
2377 struct usb_device *dev = mixer->chip->dev;
2378 int retries = 0;
2379 const int max_retries = 5;
2380 int err;
2381
2382 struct scarlett2_usb_packet *req __free(kfree) = NULL;
2383 size_t req_buf_size = struct_size(req, data, req_size);
2384 req = kmalloc(req_buf_size, GFP_KERNEL);
2385 if (!req)
2386 return -ENOMEM;
2387
2388 struct scarlett2_usb_packet *resp __free(kfree) = NULL;
2389 size_t resp_buf_size = struct_size(resp, data, resp_size);
2390 resp = kmalloc(resp_buf_size, GFP_KERNEL);
2391 if (!resp)
2392 return -ENOMEM;
2393
2394 guard(mutex)(&private->usb_mutex);
2395
2396 /* build request message and send it */
2397
2398 scarlett2_fill_request_header(private, req, cmd, req_size);
2399
2400 if (req_size)
2401 memcpy(req->data, req_data, req_size);
2402
2403 retry:
2404 err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
2405 req, req_buf_size);
2406
2407 if (err != req_buf_size) {
2408 if (err == -EPROTO && ++retries <= max_retries) {
2409 msleep(5 * (1 << (retries - 1)));
2410 goto retry;
2411 }
2412 usb_audio_err(
2413 mixer->chip,
2414 "%s USB request result cmd %x was %d\n",
2415 private->series_name, cmd, err);
2416 return -EINVAL;
2417 }
2418
2419 if (!wait_for_completion_timeout(&private->cmd_done,
2420 msecs_to_jiffies(1000))) {
2421 usb_audio_err(
2422 mixer->chip,
2423 "%s USB request timed out, cmd %x\n",
2424 private->series_name, cmd);
2425
2426 return -ETIMEDOUT;
2427 }
2428
2429 /* send a second message to get the response */
2430
2431 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
2432 SCARLETT2_USB_CMD_RESP,
2433 resp, resp_buf_size);
2434
2435 /* validate the response */
2436
2437 if (err != resp_buf_size) {
2438
2439 /* ESHUTDOWN and EPROTO are valid responses to a
2440 * reboot request
2441 */
2442 if (cmd == SCARLETT2_USB_REBOOT &&
2443 (err == -ESHUTDOWN || err == -EPROTO))
2444 return 0;
2445
2446 usb_audio_err(
2447 mixer->chip,
2448 "%s USB response result cmd %x was %d expected %zu\n",
2449 private->series_name, cmd, err, resp_buf_size);
2450 return -EINVAL;
2451 }
2452
2453 /* cmd/seq/size should match except when initialising
2454 * seq sent = 1, response = 0
2455 */
2456 if (resp->cmd != req->cmd ||
2457 (resp->seq != req->seq &&
2458 (le16_to_cpu(req->seq) != 1 || resp->seq != 0)) ||
2459 resp_size != le16_to_cpu(resp->size) ||
2460 resp->error ||
2461 resp->pad) {
2462 usb_audio_err(
2463 mixer->chip,
2464 "%s USB invalid response; "
2465 "cmd tx/rx %d/%d seq %d/%d size %d/%d "
2466 "error %d pad %d\n",
2467 private->series_name,
2468 le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
2469 le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
2470 resp_size, le16_to_cpu(resp->size),
2471 le32_to_cpu(resp->error),
2472 le32_to_cpu(resp->pad));
2473 return -EINVAL;
2474 }
2475
2476 if (resp_data && resp_size > 0)
2477 memcpy(resp_data, resp->data, resp_size);
2478
2479 return err;
2480 }
2481
2482 /* 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)2483 static int scarlett2_usb_get(
2484 struct usb_mixer_interface *mixer,
2485 int offset, void *buf, int size)
2486 {
2487 struct {
2488 __le32 offset;
2489 __le32 size;
2490 } __packed req;
2491
2492 req.offset = cpu_to_le32(offset);
2493 req.size = cpu_to_le32(size);
2494 return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
2495 &req, sizeof(req), buf, size);
2496 }
2497
2498 /* Return true if the given configuration item is present in the
2499 * configuration set used by this device.
2500 */
scarlett2_has_config_item(struct scarlett2_data * private,int config_item_num)2501 static int scarlett2_has_config_item(
2502 struct scarlett2_data *private, int config_item_num)
2503 {
2504 return !!private->config_set->items[config_item_num].offset;
2505 }
2506
2507 /* Return the configuration item's offset, applying any per-firmware
2508 * overrides.
2509 *
2510 * Firmware 2417 for the 2i2 Gen 4 moved DIRECT_MONITOR_GAIN by 4
2511 * bytes. Apply that shift here so that the rest of the driver can
2512 * keep using the single config set. This override can be removed
2513 * once the multi-config-set framework lands.
2514 */
scarlett2_config_item_offset(struct scarlett2_data * private,int config_item_num)2515 static int scarlett2_config_item_offset(
2516 struct scarlett2_data *private, int config_item_num)
2517 {
2518 int offset = private->config_set->items[config_item_num].offset;
2519
2520 if (config_item_num == SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN &&
2521 private->info == &s2i2_gen4_info &&
2522 private->firmware_version >= 2417)
2523 offset = 0x2a4;
2524
2525 return offset;
2526 }
2527
2528 /* 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)2529 static int scarlett2_usb_get_config(
2530 struct usb_mixer_interface *mixer,
2531 int config_item_num, int count, void *buf)
2532 {
2533 struct scarlett2_data *private = mixer->private_data;
2534 const struct scarlett2_config *config_item =
2535 &private->config_set->items[config_item_num];
2536 int size, err, i;
2537 int item_offset;
2538 u8 *buf_8;
2539 u8 value;
2540
2541 /* Check that the configuration item is present in the
2542 * configuration set used by this device
2543 */
2544 if (!config_item->offset)
2545 return -EFAULT;
2546
2547 item_offset = scarlett2_config_item_offset(private, config_item_num);
2548
2549 /* Writes to the parameter buffer are always 1 byte */
2550 size = config_item->size ? config_item->size : 8;
2551
2552 /* For byte-sized parameters, retrieve directly into buf */
2553 if (size >= 8) {
2554 size = size / 8 * count;
2555 err = scarlett2_usb_get(mixer, item_offset, buf, size);
2556 if (err < 0)
2557 return err;
2558 if (config_item->size == 16) {
2559 u16 *buf_16 = buf;
2560
2561 for (i = 0; i < count; i++, buf_16++)
2562 *buf_16 = le16_to_cpu(*(__le16 *)buf_16);
2563 } else if (config_item->size == 32) {
2564 u32 *buf_32 = (u32 *)buf;
2565
2566 for (i = 0; i < count; i++, buf_32++)
2567 *buf_32 = le32_to_cpu(*(__le32 *)buf_32);
2568 }
2569 return 0;
2570 }
2571
2572 /* For bit-sized parameters, retrieve into value */
2573 err = scarlett2_usb_get(mixer, item_offset, &value, 1);
2574 if (err < 0)
2575 return err;
2576
2577 /* then unpack from value into buf[] */
2578 buf_8 = buf;
2579 for (i = 0; i < 8 && i < count; i++, value >>= 1)
2580 *buf_8++ = value & 1;
2581
2582 return 0;
2583 }
2584
2585 /* Send a SCARLETT2_USB_SET_DATA command.
2586 * offset: location in the device's data space
2587 * size: size in bytes of the value (1, 2, 4)
2588 */
scarlett2_usb_set_data(struct usb_mixer_interface * mixer,int offset,int size,int value)2589 static int scarlett2_usb_set_data(
2590 struct usb_mixer_interface *mixer,
2591 int offset, int size, int value)
2592 {
2593 struct scarlett2_data *private = mixer->private_data;
2594 struct {
2595 __le32 offset;
2596 __le32 size;
2597 __le32 value;
2598 } __packed req;
2599
2600 req.offset = cpu_to_le32(offset);
2601 req.size = cpu_to_le32(size);
2602 req.value = cpu_to_le32(value);
2603 return scarlett2_usb(private->mixer, SCARLETT2_USB_SET_DATA,
2604 &req, sizeof(u32) * 2 + size, NULL, 0);
2605 }
2606
2607 /* Send a SCARLETT2_USB_SET_DATA command with multiple values.
2608 * offset: location in the device's data space
2609 * size: size in bytes of each value (1, 2, 4)
2610 * count: number of values
2611 */
scarlett2_usb_set_data_buf(struct usb_mixer_interface * mixer,int offset,int size,int count,void * buf)2612 static int scarlett2_usb_set_data_buf(
2613 struct usb_mixer_interface *mixer,
2614 int offset, int size, int count, void *buf)
2615 {
2616 struct scarlett2_data *private = mixer->private_data;
2617 int bytes = size * count;
2618 struct {
2619 __le32 offset;
2620 __le32 size;
2621 u8 data[];
2622 } __packed *req;
2623 int err;
2624 int buf_size = struct_size(req, data, bytes);
2625
2626 req = kmalloc(buf_size, GFP_KERNEL);
2627 if (!req)
2628 return -ENOMEM;
2629
2630 req->offset = cpu_to_le32(offset);
2631 req->size = cpu_to_le32(bytes);
2632 if (size == 1) {
2633 memcpy(req->data, buf, count);
2634 } else if (size == 2) {
2635 u16 *buf_16 = buf;
2636 int i;
2637
2638 for (i = 0; i < count; i++)
2639 ((__le16 *)req->data)[i] = cpu_to_le16(buf_16[i]);
2640 } else {
2641 u32 *buf_32 = buf;
2642 int i;
2643
2644 for (i = 0; i < count; i++)
2645 ((__le32 *)req->data)[i] = cpu_to_le32(buf_32[i]);
2646 }
2647
2648 err = scarlett2_usb(private->mixer, SCARLETT2_USB_SET_DATA,
2649 req, buf_size, NULL, 0);
2650
2651 kfree(req);
2652 return err;
2653 }
2654
2655 /* Send a SCARLETT2_USB_DATA_CMD command.
2656 * Configuration changes require activation with this after they have
2657 * been uploaded by a previous SCARLETT2_USB_SET_DATA.
2658 * The value for activate needed is determined by the configuration
2659 * item.
2660 */
scarlett2_usb_activate_config(struct usb_mixer_interface * mixer,int activate)2661 static int scarlett2_usb_activate_config(
2662 struct usb_mixer_interface *mixer, int activate)
2663 {
2664 __le32 req;
2665
2666 req = cpu_to_le32(activate);
2667 return scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
2668 &req, sizeof(req), NULL, 0);
2669 }
2670
2671 /* Send USB messages to set a SCARLETT2_CONFIG_* parameter */
scarlett2_usb_set_config(struct usb_mixer_interface * mixer,int config_item_num,int index,int value)2672 static int scarlett2_usb_set_config(
2673 struct usb_mixer_interface *mixer,
2674 int config_item_num, int index, int value)
2675 {
2676 struct scarlett2_data *private = mixer->private_data;
2677 const struct scarlett2_config_set *config_set = private->config_set;
2678 const struct scarlett2_config *config_item =
2679 &config_set->items[config_item_num];
2680 int offset, size;
2681 int err;
2682
2683 /* Check that the configuration item is present in the
2684 * configuration set used by this device
2685 */
2686 if (!config_item->offset)
2687 return -EFAULT;
2688
2689 /* Write via the parameter buffer? */
2690 if (config_item->pbuf) {
2691 if (!config_set->param_buf_addr)
2692 return -EFAULT;
2693
2694 /* Place index in param_buf_addr + 1 */
2695 err = scarlett2_usb_set_data(
2696 mixer, config_set->param_buf_addr + 1, 1, index);
2697 if (err < 0)
2698 return err;
2699
2700 /* Place value in param_buf_addr */
2701 err = scarlett2_usb_set_data(
2702 mixer, config_set->param_buf_addr, 1, value);
2703 if (err < 0)
2704 return err;
2705
2706 /* Activate the write through the parameter buffer */
2707 return scarlett2_usb_activate_config(
2708 mixer, config_item->activate);
2709 }
2710
2711 /* Direct writes (not via the parameter buffer) need NVRAM
2712 * save and support bit-modification
2713 */
2714
2715 /* Cancel any pending NVRAM save */
2716 cancel_delayed_work_sync(&private->work);
2717
2718 /* Convert config_item->size in bits to size in bytes and
2719 * calculate offset
2720 */
2721 if (config_item->size >= 8) {
2722 size = config_item->size / 8;
2723 offset = scarlett2_config_item_offset(private, config_item_num) +
2724 index * size;
2725
2726 /* If updating a bit, retrieve the old value, set/clear the
2727 * bit as needed, and update value
2728 */
2729 } else {
2730 u8 tmp;
2731
2732 size = 1;
2733 offset = scarlett2_config_item_offset(private, config_item_num);
2734
2735 err = scarlett2_usb_get(mixer, offset, &tmp, 1);
2736 if (err < 0)
2737 return err;
2738
2739 if (value)
2740 tmp |= (1 << index);
2741 else
2742 tmp &= ~(1 << index);
2743
2744 value = tmp;
2745 }
2746
2747 /* Write the new value */
2748 err = scarlett2_usb_set_data(mixer, offset, size, value);
2749 if (err < 0)
2750 return err;
2751
2752 /* Activate the change */
2753 err = scarlett2_usb_activate_config(mixer, config_item->activate);
2754 if (err < 0)
2755 return err;
2756
2757 /* Interfaces with parameter buffer writes don't need a
2758 * separate save step
2759 */
2760 if (config_set->param_buf_addr)
2761 return 0;
2762
2763 /* Schedule the change to be written to NVRAM */
2764 if (config_item->activate != SCARLETT2_USB_CONFIG_SAVE)
2765 schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
2766
2767 return 0;
2768 }
2769
2770 /* Send USB messages to set a SCARLETT2_CONFIG_* parameter with
2771 * multiple values
2772 */
scarlett2_usb_set_config_buf(struct usb_mixer_interface * mixer,int config_item_num,int index,int count,void * buf)2773 static int scarlett2_usb_set_config_buf(
2774 struct usb_mixer_interface *mixer,
2775 int config_item_num, int index, int count, void *buf)
2776 {
2777 struct scarlett2_data *private = mixer->private_data;
2778 const struct scarlett2_config_set *config_set = private->config_set;
2779 const struct scarlett2_config *config_item =
2780 &config_set->items[config_item_num];
2781 int offset, size;
2782 int err;
2783
2784 /* Check that the configuration item is present in the
2785 * configuration set used by this device
2786 */
2787 if (!config_item->offset)
2788 return -EFAULT;
2789
2790 /* Convert config_item->size in bits to size in bytes and
2791 * calculate offset
2792 */
2793 if (config_item->size >= 8) {
2794 size = config_item->size / 8;
2795 offset = config_item->offset + index * size;
2796
2797 /* Bit updates not supported */
2798 } else {
2799 return -EFAULT;
2800 }
2801
2802 /* Write the new values */
2803 err = scarlett2_usb_set_data_buf(mixer, offset, size, count, buf);
2804 if (err < 0)
2805 return err;
2806
2807 /* Activate the change */
2808 return scarlett2_usb_activate_config(mixer, config_item->activate);
2809 }
2810
2811 /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
scarlett2_config_save(struct usb_mixer_interface * mixer)2812 static void scarlett2_config_save(struct usb_mixer_interface *mixer)
2813 {
2814 int err;
2815
2816 err = scarlett2_usb_activate_config(mixer, SCARLETT2_USB_CONFIG_SAVE);
2817 if (err < 0)
2818 usb_audio_err(mixer->chip, "config save failed: %d\n", err);
2819 }
2820
2821 /* Delayed work to save config */
scarlett2_config_save_work(struct work_struct * work)2822 static void scarlett2_config_save_work(struct work_struct *work)
2823 {
2824 struct scarlett2_data *private =
2825 container_of(work, struct scarlett2_data, work.work);
2826
2827 scarlett2_config_save(private->mixer);
2828 }
2829
2830 /* Send a USB message to get sync status; result placed in *sync */
scarlett2_usb_get_sync_status(struct usb_mixer_interface * mixer,u8 * sync)2831 static int scarlett2_usb_get_sync_status(
2832 struct usb_mixer_interface *mixer,
2833 u8 *sync)
2834 {
2835 __le32 data;
2836 int err;
2837
2838 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
2839 NULL, 0, &data, sizeof(data));
2840 if (err < 0)
2841 return err;
2842
2843 *sync = !!data;
2844 return 0;
2845 }
2846
2847 /* Return true if the device has a mixer that we can control */
scarlett2_has_mixer(struct scarlett2_data * private)2848 static int scarlett2_has_mixer(struct scarlett2_data *private)
2849 {
2850 return !!private->info->mux_assignment[0][0].count;
2851 }
2852
2853 /* Map from mixer value to (db + 80) * 2
2854 * (reverse of scarlett2_mixer_values[])
2855 */
scarlett2_mixer_value_to_db(int value)2856 static int scarlett2_mixer_value_to_db(int value)
2857 {
2858 int i;
2859
2860 for (i = 0; i < SCARLETT2_MIXER_VALUE_COUNT; i++)
2861 if (scarlett2_mixer_values[i] >= value)
2862 return i;
2863 return SCARLETT2_MIXER_MAX_VALUE;
2864 }
2865
2866 /* Send a USB message to get the volumes for all inputs of one mix
2867 * and put the values into private->mix[]
2868 */
scarlett2_usb_get_mix(struct usb_mixer_interface * mixer,int mix_num)2869 static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
2870 int mix_num)
2871 {
2872 struct scarlett2_data *private = mixer->private_data;
2873
2874 int num_mixer_in = private->num_mix_in;
2875 int err, i, j;
2876
2877 struct {
2878 __le16 mix_num;
2879 __le16 count;
2880 } __packed req;
2881
2882 __le16 data[SCARLETT2_INPUT_MIX_MAX];
2883
2884 req.mix_num = cpu_to_le16(mix_num);
2885 req.count = cpu_to_le16(num_mixer_in);
2886
2887 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
2888 &req, sizeof(req),
2889 data, num_mixer_in * sizeof(u16));
2890 if (err < 0)
2891 return err;
2892
2893 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
2894 private->mix[j] = scarlett2_mixer_value_to_db(
2895 le16_to_cpu(data[i]));
2896
2897 return 0;
2898 }
2899
2900 /* Send a USB message to set the volumes for all inputs of one mix
2901 * (values obtained from private->mix[])
2902 */
scarlett2_usb_set_mix(struct usb_mixer_interface * mixer,int mix_num)2903 static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
2904 int mix_num)
2905 {
2906 struct scarlett2_data *private = mixer->private_data;
2907
2908 struct {
2909 __le16 mix_num;
2910 __le16 data[SCARLETT2_INPUT_MIX_MAX];
2911 } __packed req;
2912
2913 int i, j;
2914 int num_mixer_in = private->num_mix_in;
2915
2916 req.mix_num = cpu_to_le16(mix_num);
2917
2918 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
2919 req.data[i] = cpu_to_le16(
2920 scarlett2_mixer_values[private->mix[j]]
2921 );
2922
2923 return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
2924 &req, (num_mixer_in + 1) * sizeof(u16),
2925 NULL, 0);
2926 }
2927
2928 /* 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)2929 static u32 scarlett2_mux_src_num_to_id(
2930 const int port_count[][SCARLETT2_PORT_DIRNS], int num)
2931 {
2932 int port_type;
2933
2934 for (port_type = 0;
2935 port_type < SCARLETT2_PORT_TYPE_COUNT;
2936 port_type++) {
2937 if (num < port_count[port_type][SCARLETT2_PORT_IN])
2938 return scarlett2_ports[port_type].id | num;
2939 num -= port_count[port_type][SCARLETT2_PORT_IN];
2940 }
2941
2942 /* Oops */
2943 return 0;
2944 }
2945
2946 /* 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)2947 static u32 scarlett2_mux_id_to_num(
2948 const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
2949 {
2950 int port_type;
2951 int port_num = 0;
2952
2953 for (port_type = 0;
2954 port_type < SCARLETT2_PORT_TYPE_COUNT;
2955 port_type++) {
2956 int base = scarlett2_ports[port_type].id;
2957 int count = port_count[port_type][direction];
2958
2959 if (id >= base && id < base + count)
2960 return port_num + id - base;
2961 port_num += count;
2962 }
2963
2964 /* Oops */
2965 return -1;
2966 }
2967
2968 /* Convert one mux entry from the interface and load into private->mux[] */
scarlett2_usb_populate_mux(struct scarlett2_data * private,u32 mux_entry)2969 static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
2970 u32 mux_entry)
2971 {
2972 const struct scarlett2_device_info *info = private->info;
2973 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2974
2975 int dst_idx, src_idx;
2976
2977 dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
2978 mux_entry & 0xFFF);
2979 if (dst_idx < 0)
2980 return;
2981
2982 if (dst_idx >= private->num_mux_dsts) {
2983 usb_audio_err(private->mixer->chip,
2984 "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
2985 mux_entry, dst_idx, private->num_mux_dsts);
2986 return;
2987 }
2988
2989 src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
2990 mux_entry >> 12);
2991 if (src_idx < 0)
2992 return;
2993
2994 if (src_idx >= private->num_mux_srcs) {
2995 usb_audio_err(private->mixer->chip,
2996 "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
2997 mux_entry, src_idx, private->num_mux_srcs);
2998 return;
2999 }
3000
3001 private->mux[dst_idx] = src_idx;
3002 }
3003
3004 /* Update the meter level map
3005 *
3006 * The meter level data from the interface (SCARLETT2_USB_GET_METER
3007 * request) is returned in mux_assignment order, but to avoid exposing
3008 * that to userspace, scarlett2_meter_ctl_get() rearranges the data
3009 * into scarlett2_ports order using the meter_level_map[] array which
3010 * is set up by this function.
3011 *
3012 * In addition, the meter level data values returned from the
3013 * interface are invalid for destinations where:
3014 *
3015 * - the source is "Off"; therefore we set those values to zero (map
3016 * value of 255)
3017 *
3018 * - the source is assigned to a previous (with respect to the
3019 * mux_assignment order) destination; therefore we set those values
3020 * to the value previously reported for that source
3021 */
scarlett2_update_meter_level_map(struct scarlett2_data * private)3022 static void scarlett2_update_meter_level_map(struct scarlett2_data *private)
3023 {
3024 const struct scarlett2_device_info *info = private->info;
3025 const struct scarlett2_meter_entry *entry;
3026
3027 /* sources already assigned to a destination
3028 * value is 255 for None, otherwise the value of i
3029 * (index into array returned by
3030 * scarlett2_usb_get_meter_levels())
3031 */
3032 u8 seen_src[SCARLETT2_MAX_SRCS] = { 1 };
3033 u8 seen_src_value[SCARLETT2_MAX_SRCS] = { 255 };
3034
3035 /* index in meter_map[] order */
3036 int i = 0;
3037
3038 /* go through the meter_map[] entries */
3039 for (entry = info->meter_map;
3040 entry->count;
3041 entry++) {
3042
3043 /* fill in each meter_level_map[] entry */
3044 int j, mux_idx;
3045
3046 for (j = 0, mux_idx = entry->start;
3047 j < entry->count;
3048 i++, j++, mux_idx++) {
3049
3050 /* convert mux_idx using line_out_unmap[] */
3051 int map_mux_idx = (
3052 info->line_out_remap_enable &&
3053 mux_idx < private->num_line_out
3054 ) ? info->line_out_unmap[mux_idx]
3055 : mux_idx;
3056
3057 /* check which source is connected, and if
3058 * that source is already connected elsewhere,
3059 * use that existing connection's destination
3060 * for this meter entry instead
3061 */
3062 int mux_src = private->mux[mux_idx];
3063
3064 if (!seen_src[mux_src]) {
3065 seen_src[mux_src] = 1;
3066 seen_src_value[mux_src] = i;
3067 }
3068 private->meter_level_map[map_mux_idx] =
3069 seen_src_value[mux_src];
3070 }
3071 }
3072 }
3073
3074 /* Send USB message to get mux inputs and then populate private->mux[] */
scarlett2_usb_get_mux(struct usb_mixer_interface * mixer)3075 static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
3076 {
3077 struct scarlett2_data *private = mixer->private_data;
3078 int count = private->num_mux_dsts;
3079 int err, i;
3080
3081 struct {
3082 __le16 num;
3083 __le16 count;
3084 } __packed req;
3085
3086 __le32 data[SCARLETT2_MUX_MAX];
3087
3088 private->mux_updated = 0;
3089
3090 req.num = 0;
3091 req.count = cpu_to_le16(count);
3092
3093 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
3094 &req, sizeof(req),
3095 data, count * sizeof(u32));
3096 if (err < 0)
3097 return err;
3098
3099 for (i = 0; i < count; i++)
3100 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
3101
3102 scarlett2_update_meter_level_map(private);
3103
3104 return 0;
3105 }
3106
3107 /* Send USB messages to set mux inputs */
scarlett2_usb_set_mux(struct usb_mixer_interface * mixer)3108 static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
3109 {
3110 struct scarlett2_data *private = mixer->private_data;
3111 const struct scarlett2_device_info *info = private->info;
3112 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3113 int table;
3114
3115 struct {
3116 __le16 pad;
3117 __le16 num;
3118 __le32 data[SCARLETT2_MUX_MAX];
3119 } __packed req;
3120
3121 req.pad = 0;
3122
3123 /* set mux settings for each rate */
3124 for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
3125 const struct scarlett2_mux_entry *entry;
3126
3127 /* i counts over the output array */
3128 int i = 0, err;
3129
3130 req.num = cpu_to_le16(table);
3131
3132 /* loop through each entry */
3133 for (entry = info->mux_assignment[table];
3134 entry->count;
3135 entry++) {
3136 int j;
3137 int port_type = entry->port_type;
3138 int port_idx = entry->start;
3139 int mux_idx = scarlett2_get_port_start_num(port_count,
3140 SCARLETT2_PORT_OUT, port_type) + port_idx;
3141 int dst_id = scarlett2_ports[port_type].id + port_idx;
3142
3143 /* Empty slots */
3144 if (!dst_id) {
3145 for (j = 0; j < entry->count; j++)
3146 req.data[i++] = 0;
3147 continue;
3148 }
3149
3150 /* Non-empty mux slots use the lower 12 bits
3151 * for the destination and next 12 bits for
3152 * the source
3153 */
3154 for (j = 0; j < entry->count; j++) {
3155 int src_id = scarlett2_mux_src_num_to_id(
3156 port_count, private->mux[mux_idx++]);
3157 req.data[i++] = cpu_to_le32(dst_id |
3158 src_id << 12);
3159 dst_id++;
3160 }
3161 }
3162
3163 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
3164 &req, (i + 1) * sizeof(u32),
3165 NULL, 0);
3166 if (err < 0)
3167 return err;
3168 }
3169
3170 scarlett2_update_meter_level_map(private);
3171
3172 return 0;
3173 }
3174
3175 /* Send USB message to get meter levels */
scarlett2_usb_get_meter_levels(struct usb_mixer_interface * mixer,u16 num_meters,u16 * levels)3176 static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
3177 u16 num_meters, u16 *levels)
3178 {
3179 struct {
3180 __le16 pad;
3181 __le16 num_meters;
3182 __le32 magic;
3183 } __packed req;
3184 __le32 resp[SCARLETT2_MAX_METERS];
3185 int i, err;
3186
3187 req.pad = 0;
3188 req.num_meters = cpu_to_le16(num_meters);
3189 req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
3190 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
3191 &req, sizeof(req), resp, num_meters * sizeof(u32));
3192 if (err < 0)
3193 return err;
3194
3195 /* copy, convert to u16 */
3196 for (i = 0; i < num_meters; i++)
3197 levels[i] = le32_to_cpu(resp[i]);
3198
3199 return 0;
3200 }
3201
3202 /* For config items with mute=1, xor bits 0 & 1 together to get the
3203 * current/next state. This won't have any effect on values which are
3204 * only ever 0/1.
3205 */
scarlett2_decode_muteable(uint8_t v)3206 static uint8_t scarlett2_decode_muteable(uint8_t v)
3207 {
3208 return (v ^ (v >> 1)) & 1;
3209 }
3210
3211 /*** Control Functions ***/
3212
3213 /* 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)3214 static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
3215 const struct snd_kcontrol_new *ncontrol,
3216 int index, int channels, const char *name,
3217 struct snd_kcontrol **kctl_return)
3218 {
3219 struct snd_kcontrol *kctl;
3220 struct usb_mixer_elem_info *elem;
3221 int err;
3222
3223 elem = kzalloc_obj(*elem);
3224 if (!elem)
3225 return -ENOMEM;
3226
3227 /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
3228 * ignores them for resume and other operations.
3229 * Also, the head.id field is set to 0, as we don't use this field.
3230 */
3231 elem->head.mixer = mixer;
3232 elem->control = index;
3233 elem->head.id = 0;
3234 elem->channels = channels;
3235 elem->val_type = USB_MIXER_BESPOKEN;
3236
3237 kctl = snd_ctl_new1(ncontrol, elem);
3238 if (!kctl) {
3239 kfree(elem);
3240 return -ENOMEM;
3241 }
3242 kctl->private_free = snd_usb_mixer_elem_free;
3243
3244 strscpy(kctl->id.name, name, sizeof(kctl->id.name));
3245
3246 err = snd_usb_mixer_add_control(&elem->head, kctl);
3247 if (err < 0)
3248 return err;
3249
3250 if (kctl_return)
3251 *kctl_return = kctl;
3252
3253 return 0;
3254 }
3255
3256 /*** Firmware Version Control ***/
3257
scarlett2_firmware_version_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)3258 static int scarlett2_firmware_version_ctl_get(
3259 struct snd_kcontrol *kctl,
3260 struct snd_ctl_elem_value *ucontrol)
3261 {
3262 struct usb_mixer_elem_info *elem = kctl->private_data;
3263 struct scarlett2_data *private = elem->head.mixer->private_data;
3264
3265 ucontrol->value.integer.value[0] = private->firmware_version;
3266
3267 return 0;
3268 }
3269
scarlett2_firmware_version_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)3270 static int scarlett2_firmware_version_ctl_info(
3271 struct snd_kcontrol *kctl,
3272 struct snd_ctl_elem_info *uinfo)
3273 {
3274 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3275 uinfo->count = 1;
3276
3277 return 0;
3278 }
3279
3280 static const struct snd_kcontrol_new scarlett2_firmware_version_ctl = {
3281 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
3282 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3283 .name = "",
3284 .info = scarlett2_firmware_version_ctl_info,
3285 .get = scarlett2_firmware_version_ctl_get
3286 };
3287
scarlett2_add_firmware_version_ctl(struct usb_mixer_interface * mixer)3288 static int scarlett2_add_firmware_version_ctl(
3289 struct usb_mixer_interface *mixer)
3290 {
3291 return scarlett2_add_new_ctl(mixer, &scarlett2_firmware_version_ctl,
3292 0, 0, "Firmware Version", NULL);
3293 }
3294
3295 /*** Minimum Firmware Version Control ***/
3296
scarlett2_min_firmware_version_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)3297 static int scarlett2_min_firmware_version_ctl_get(
3298 struct snd_kcontrol *kctl,
3299 struct snd_ctl_elem_value *ucontrol)
3300 {
3301 struct usb_mixer_elem_info *elem = kctl->private_data;
3302 struct scarlett2_data *private = elem->head.mixer->private_data;
3303
3304 ucontrol->value.integer.value[0] = private->info->min_firmware_version;
3305
3306 return 0;
3307 }
3308
scarlett2_min_firmware_version_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)3309 static int scarlett2_min_firmware_version_ctl_info(
3310 struct snd_kcontrol *kctl,
3311 struct snd_ctl_elem_info *uinfo)
3312 {
3313 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3314 uinfo->count = 1;
3315
3316 return 0;
3317 }
3318
3319 static const struct snd_kcontrol_new scarlett2_min_firmware_version_ctl = {
3320 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
3321 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3322 .name = "",
3323 .info = scarlett2_min_firmware_version_ctl_info,
3324 .get = scarlett2_min_firmware_version_ctl_get
3325 };
3326
scarlett2_add_min_firmware_version_ctl(struct usb_mixer_interface * mixer)3327 static int scarlett2_add_min_firmware_version_ctl(
3328 struct usb_mixer_interface *mixer)
3329 {
3330 return scarlett2_add_new_ctl(mixer, &scarlett2_min_firmware_version_ctl,
3331 0, 0, "Minimum Firmware Version", NULL);
3332 }
3333
3334 /*** Sync Control ***/
3335
3336 /* Update sync control after receiving notification that the status
3337 * has changed
3338 */
scarlett2_update_sync(struct usb_mixer_interface * mixer)3339 static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
3340 {
3341 struct scarlett2_data *private = mixer->private_data;
3342
3343 private->sync_updated = 0;
3344 return scarlett2_usb_get_sync_status(mixer, &private->sync);
3345 }
3346
scarlett2_sync_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)3347 static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
3348 struct snd_ctl_elem_info *uinfo)
3349 {
3350 static const char *texts[2] = {
3351 "Unlocked", "Locked"
3352 };
3353 return snd_ctl_enum_info(uinfo, 1, 2, texts);
3354 }
3355
scarlett2_sync_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)3356 static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
3357 struct snd_ctl_elem_value *ucontrol)
3358 {
3359 struct usb_mixer_elem_info *elem = kctl->private_data;
3360 struct usb_mixer_interface *mixer = elem->head.mixer;
3361 struct scarlett2_data *private = mixer->private_data;
3362 int err;
3363
3364 guard(mutex)(&private->data_mutex);
3365
3366 if (private->hwdep_in_use)
3367 return -EBUSY;
3368
3369 if (private->sync_updated) {
3370 err = scarlett2_update_sync(mixer);
3371 if (err < 0)
3372 return err;
3373 }
3374 ucontrol->value.enumerated.item[0] = private->sync;
3375 return 0;
3376 }
3377
3378 static const struct snd_kcontrol_new scarlett2_sync_ctl = {
3379 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3380 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3381 .name = "",
3382 .info = scarlett2_sync_ctl_info,
3383 .get = scarlett2_sync_ctl_get
3384 };
3385
scarlett2_add_sync_ctl(struct usb_mixer_interface * mixer)3386 static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
3387 {
3388 struct scarlett2_data *private = mixer->private_data;
3389
3390 /* devices without a mixer also don't support reporting sync status */
3391 if (!scarlett2_has_mixer(private))
3392 return 0;
3393
3394 return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
3395 0, 1, "Sync Status", &private->sync_ctl);
3396 }
3397
3398 /*** Autogain Switch and Status Controls ***/
3399
3400 /* Forward declarations as phantom power and autogain can disable each other */
3401 static int scarlett2_check_input_phantom_updated(struct usb_mixer_interface *);
3402 static int scarlett2_phantom_is_switching(struct scarlett2_data *, int);
3403
3404 /* Set the access mode of a control to read-only (val = 0) or
3405 * read-write (val = 1).
3406 */
scarlett2_set_ctl_access(struct snd_kcontrol * kctl,int val)3407 static void scarlett2_set_ctl_access(struct snd_kcontrol *kctl, int val)
3408 {
3409 if (val)
3410 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
3411 else
3412 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_WRITE;
3413 }
3414
3415 /* Check if autogain is running on any input */
scarlett2_autogain_is_running(struct scarlett2_data * private)3416 static int scarlett2_autogain_is_running(struct scarlett2_data *private)
3417 {
3418 int i;
3419
3420 /* autogain_status[] is 0 if autogain is running */
3421 for (i = 0; i < private->info->gain_input_count; i++)
3422 if (!private->autogain_status[i])
3423 return 1;
3424
3425 return 0;
3426 }
3427
scarlett2_update_autogain(struct usb_mixer_interface * mixer)3428 static int scarlett2_update_autogain(struct usb_mixer_interface *mixer)
3429 {
3430 struct scarlett2_data *private = mixer->private_data;
3431 const struct scarlett2_device_info *info = private->info;
3432 int err, i;
3433 u8 raw_autogain_status[SCARLETT2_INPUT_GAIN_MAX];
3434 s8 ag_target_values[SCARLETT2_AG_TARGET_COUNT];
3435
3436 private->autogain_updated = 0;
3437
3438 if (!info->gain_input_count)
3439 return 0;
3440
3441 err = scarlett2_usb_get_config(
3442 mixer, SCARLETT2_CONFIG_AUTOGAIN_SWITCH,
3443 info->gain_input_count, private->autogain_switch);
3444 if (err < 0)
3445 return err;
3446 err = scarlett2_usb_get_config(
3447 mixer, SCARLETT2_CONFIG_AUTOGAIN_STATUS,
3448 info->gain_input_count, raw_autogain_status);
3449 if (err < 0)
3450 return err;
3451
3452 /* Translate autogain_switch and raw_autogain_status into
3453 * autogain_status.
3454 *
3455 * When autogain_switch[] is set, the status is the first
3456 * element in scarlett2_autogain_status_texts[] (Running). The
3457 * subsequent elements correspond to the status value from the
3458 * device (raw_autogain_status[]) + 1. The last element is
3459 * "Invalid", in case the device reports a status outside the
3460 * range of scarlett2_autogain_status_texts[].
3461 */
3462 for (i = 0; i < info->gain_input_count; i++)
3463 if (private->autogain_switch[i])
3464 private->autogain_status[i] = 0;
3465 else if (raw_autogain_status[i] <
3466 private->num_autogain_status_texts - 1)
3467 private->autogain_status[i] =
3468 raw_autogain_status[i] + 1;
3469 else
3470 private->autogain_status[i] =
3471 private->num_autogain_status_texts - 1;
3472
3473 for (i = 0; i < SCARLETT2_AG_TARGET_COUNT; i++)
3474 if (scarlett2_has_config_item(private,
3475 scarlett2_ag_target_configs[i])) {
3476 err = scarlett2_usb_get_config(
3477 mixer, scarlett2_ag_target_configs[i],
3478 1, &ag_target_values[i]);
3479 if (err < 0)
3480 return err;
3481 }
3482
3483 /* convert from negative dBFS as used by the device */
3484 for (i = 0; i < SCARLETT2_AG_TARGET_COUNT; i++)
3485 private->ag_targets[i] = -ag_target_values[i];
3486
3487 return 0;
3488 }
3489
3490 /* Update access mode for controls affected by autogain */
scarlett2_autogain_update_access(struct usb_mixer_interface * mixer)3491 static void scarlett2_autogain_update_access(struct usb_mixer_interface *mixer)
3492 {
3493 struct scarlett2_data *private = mixer->private_data;
3494 const struct scarlett2_device_info *info = private->info;
3495 int val = !scarlett2_autogain_is_running(private);
3496 int i;
3497
3498 if (scarlett2_has_config_item(private,
3499 SCARLETT2_CONFIG_INPUT_SELECT_SWITCH))
3500 scarlett2_set_ctl_access(private->input_select_ctl, val);
3501 if (scarlett2_has_config_item(private,
3502 SCARLETT2_CONFIG_INPUT_LINK_SWITCH))
3503 for (i = 0; i < info->gain_input_count; i++)
3504 scarlett2_set_ctl_access(private->input_link_ctls[i],
3505 val);
3506 for (i = 0; i < info->gain_input_count; i++)
3507 scarlett2_set_ctl_access(private->input_gain_ctls[i], val);
3508 for (i = 0; i < info->safe_input_count; i++)
3509 scarlett2_set_ctl_access(private->safe_ctls[i], val);
3510 for (i = 0; i < info->level_input_count; i++)
3511 scarlett2_set_ctl_access(private->level_ctls[i], val);
3512 for (i = 0; i < info->air_input_count; i++)
3513 scarlett2_set_ctl_access(private->air_ctls[i], val);
3514 for (i = 0; i < info->mute_input_count; i++)
3515 scarlett2_set_ctl_access(private->input_mute_ctls[i], val);
3516 for (i = 0; i < info->phantom_count; i++)
3517 scarlett2_set_ctl_access(private->phantom_ctls[i], val);
3518 for (i = 0; i < info->dsp_input_count; i++)
3519 scarlett2_set_ctl_access(private->dsp_ctls[i], val);
3520
3521 for (i = 0; i < SCARLETT2_AG_TARGET_COUNT; i++)
3522 if (scarlett2_has_config_item(private,
3523 scarlett2_ag_target_configs[i]))
3524 scarlett2_set_ctl_access(
3525 private->ag_target_ctls[i], val);
3526 }
3527
3528 /* Notify of access mode change for all controls read-only while
3529 * autogain runs.
3530 */
scarlett2_autogain_notify_access(struct usb_mixer_interface * mixer)3531 static void scarlett2_autogain_notify_access(struct usb_mixer_interface *mixer)
3532 {
3533 struct snd_card *card = mixer->chip->card;
3534 struct scarlett2_data *private = mixer->private_data;
3535 const struct scarlett2_device_info *info = private->info;
3536 int i;
3537
3538 if (scarlett2_has_config_item(private,
3539 SCARLETT2_CONFIG_INPUT_SELECT_SWITCH))
3540 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
3541 &private->input_select_ctl->id);
3542 if (scarlett2_has_config_item(private,
3543 SCARLETT2_CONFIG_INPUT_LINK_SWITCH))
3544 for (i = 0; i < info->gain_input_count; i++)
3545 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
3546 &private->input_link_ctls[i]->id);
3547 for (i = 0; i < info->gain_input_count; i++)
3548 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
3549 &private->input_gain_ctls[i]->id);
3550 for (i = 0; i < info->safe_input_count; i++)
3551 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
3552 &private->safe_ctls[i]->id);
3553 for (i = 0; i < info->level_input_count; i++)
3554 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
3555 &private->level_ctls[i]->id);
3556 for (i = 0; i < info->air_input_count; i++)
3557 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
3558 &private->air_ctls[i]->id);
3559 for (i = 0; i < info->dsp_input_count; i++)
3560 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
3561 &private->dsp_ctls[i]->id);
3562 for (i = 0; i < info->mute_input_count; i++)
3563 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
3564 &private->input_mute_ctls[i]->id);
3565 for (i = 0; i < info->phantom_count; i++)
3566 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
3567 &private->phantom_ctls[i]->id);
3568
3569 for (i = 0; i < SCARLETT2_AG_TARGET_COUNT; i++)
3570 if (scarlett2_has_config_item(private,
3571 scarlett2_ag_target_configs[i]))
3572 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
3573 &private->ag_target_ctls[i]->id);
3574 }
3575
3576 /* Call scarlett2_update_autogain() and
3577 * scarlett2_autogain_update_access() if autogain_updated is set.
3578 */
scarlett2_check_autogain_updated(struct usb_mixer_interface * mixer)3579 static int scarlett2_check_autogain_updated(
3580 struct usb_mixer_interface *mixer)
3581 {
3582 struct scarlett2_data *private = mixer->private_data;
3583 int err;
3584
3585 if (!private->autogain_updated)
3586 return 0;
3587
3588 err = scarlett2_update_autogain(mixer);
3589 if (err < 0)
3590 return err;
3591
3592 scarlett2_autogain_update_access(mixer);
3593
3594 return 0;
3595 }
3596
3597 /* If autogain_updated is set when a *_ctl_put() function for a
3598 * control that is meant to be read-only while autogain is running,
3599 * update the autogain status and access mode of affected controls.
3600 * Return -EPERM if autogain is running.
3601 */
scarlett2_check_put_during_autogain(struct usb_mixer_interface * mixer)3602 static int scarlett2_check_put_during_autogain(
3603 struct usb_mixer_interface *mixer)
3604 {
3605 int err = scarlett2_check_autogain_updated(mixer);
3606
3607 if (err < 0)
3608 return err;
3609
3610 if (scarlett2_autogain_is_running(mixer->private_data))
3611 return -EPERM;
3612
3613 return 0;
3614 }
3615
scarlett2_autogain_switch_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)3616 static int scarlett2_autogain_switch_ctl_info(
3617 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3618 {
3619 struct usb_mixer_elem_info *elem = kctl->private_data;
3620 struct usb_mixer_interface *mixer = elem->head.mixer;
3621 struct scarlett2_data *private = mixer->private_data;
3622 int err;
3623
3624 guard(mutex)(&private->data_mutex);
3625
3626 err = scarlett2_check_input_phantom_updated(mixer);
3627 if (err < 0)
3628 return err;
3629
3630 return snd_ctl_boolean_mono_info(kctl, uinfo);
3631 }
3632
scarlett2_autogain_switch_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)3633 static int scarlett2_autogain_switch_ctl_get(
3634 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3635 {
3636 struct usb_mixer_elem_info *elem = kctl->private_data;
3637 struct usb_mixer_interface *mixer = elem->head.mixer;
3638 struct scarlett2_data *private = mixer->private_data;
3639 int err;
3640
3641 guard(mutex)(&private->data_mutex);
3642
3643 if (private->hwdep_in_use)
3644 return -EBUSY;
3645
3646 err = scarlett2_check_autogain_updated(mixer);
3647 if (err < 0)
3648 return err;
3649
3650 ucontrol->value.enumerated.item[0] =
3651 private->autogain_switch[elem->control];
3652 return 0;
3653 }
3654
scarlett2_autogain_status_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)3655 static int scarlett2_autogain_status_ctl_get(
3656 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3657 {
3658 struct usb_mixer_elem_info *elem = kctl->private_data;
3659 struct usb_mixer_interface *mixer = elem->head.mixer;
3660 struct scarlett2_data *private = mixer->private_data;
3661 int err;
3662
3663 guard(mutex)(&private->data_mutex);
3664
3665 if (private->hwdep_in_use)
3666 return -EBUSY;
3667
3668 err = scarlett2_check_autogain_updated(mixer);
3669 if (err < 0)
3670 return err;
3671
3672 ucontrol->value.enumerated.item[0] =
3673 private->autogain_status[elem->control];
3674 return 0;
3675 }
3676
scarlett2_autogain_switch_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)3677 static int scarlett2_autogain_switch_ctl_put(
3678 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3679 {
3680 struct usb_mixer_elem_info *elem = kctl->private_data;
3681 struct usb_mixer_interface *mixer = elem->head.mixer;
3682 struct scarlett2_data *private = mixer->private_data;
3683 int index = elem->control;
3684 int oval, val, err;
3685
3686 guard(mutex)(&private->data_mutex);
3687
3688 if (private->hwdep_in_use)
3689 return -EBUSY;
3690
3691 err = scarlett2_check_input_phantom_updated(mixer);
3692 if (err < 0)
3693 return err;
3694
3695 if (scarlett2_phantom_is_switching(private, index))
3696 return -EPERM;
3697
3698 oval = private->autogain_switch[index];
3699 val = !!ucontrol->value.integer.value[0];
3700
3701 if (oval == val)
3702 return 0;
3703
3704 private->autogain_switch[index] = val;
3705
3706 /* Send switch change to the device */
3707 err = scarlett2_usb_set_config(
3708 mixer, SCARLETT2_CONFIG_AUTOGAIN_SWITCH, index, val);
3709
3710 scarlett2_autogain_update_access(mixer);
3711 scarlett2_autogain_notify_access(mixer);
3712
3713 return err < 0 ? err : 1;
3714 }
3715
scarlett2_autogain_status_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)3716 static int scarlett2_autogain_status_ctl_info(
3717 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3718 {
3719 struct usb_mixer_elem_info *elem = kctl->private_data;
3720 struct scarlett2_data *private = elem->head.mixer->private_data;
3721
3722 return snd_ctl_enum_info(
3723 uinfo, 1,
3724 private->num_autogain_status_texts,
3725 private->config_set->autogain_status_texts);
3726 }
3727
3728 static const struct snd_kcontrol_new scarlett2_autogain_switch_ctl = {
3729 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3730 .name = "",
3731 .info = scarlett2_autogain_switch_ctl_info,
3732 .get = scarlett2_autogain_switch_ctl_get,
3733 .put = scarlett2_autogain_switch_ctl_put
3734 };
3735
3736 static const struct snd_kcontrol_new scarlett2_autogain_status_ctl = {
3737 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3738 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3739 .name = "",
3740 .info = scarlett2_autogain_status_ctl_info,
3741 .get = scarlett2_autogain_status_ctl_get,
3742 };
3743
3744 /*** Autogain Target Controls ***/
3745
scarlett2_ag_target_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)3746 static int scarlett2_ag_target_ctl_info(
3747 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3748 {
3749 struct usb_mixer_elem_info *elem = kctl->private_data;
3750 struct usb_mixer_interface *mixer = elem->head.mixer;
3751 struct scarlett2_data *private = mixer->private_data;
3752 int err;
3753
3754 guard(mutex)(&private->data_mutex);
3755
3756 if (private->hwdep_in_use)
3757 return -EBUSY;
3758
3759 err = scarlett2_check_autogain_updated(mixer);
3760 if (err < 0)
3761 return err;
3762
3763 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3764 uinfo->count = 1;
3765 uinfo->value.integer.min = SCARLETT2_AG_TARGET_MIN;
3766 uinfo->value.integer.max = 0;
3767 uinfo->value.integer.step = 1;
3768
3769 return 0;
3770 }
3771
scarlett2_ag_target_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)3772 static int scarlett2_ag_target_ctl_get(
3773 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3774 {
3775 struct usb_mixer_elem_info *elem = kctl->private_data;
3776 struct usb_mixer_interface *mixer = elem->head.mixer;
3777 struct scarlett2_data *private = mixer->private_data;
3778 int err;
3779
3780 guard(mutex)(&private->data_mutex);
3781
3782 if (private->hwdep_in_use)
3783 return -EBUSY;
3784
3785 if (private->autogain_updated) {
3786 err = scarlett2_update_autogain(mixer);
3787 if (err < 0)
3788 return err;
3789 }
3790
3791 ucontrol->value.integer.value[0] = private->ag_targets[elem->control];
3792 return 0;
3793 }
3794
scarlett2_ag_target_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)3795 static int scarlett2_ag_target_ctl_put(
3796 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3797 {
3798 struct usb_mixer_elem_info *elem = kctl->private_data;
3799 struct usb_mixer_interface *mixer = elem->head.mixer;
3800 struct scarlett2_data *private = mixer->private_data;
3801 int index = elem->control;
3802 int oval, val, err;
3803
3804 guard(mutex)(&private->data_mutex);
3805
3806 if (private->hwdep_in_use)
3807 return -EBUSY;
3808
3809 err = scarlett2_check_put_during_autogain(mixer);
3810 if (err < 0)
3811 return err;
3812
3813 oval = private->ag_targets[index];
3814 val = clamp(ucontrol->value.integer.value[0],
3815 (long)SCARLETT2_AG_TARGET_MIN, 0L);
3816
3817 if (oval == val)
3818 return 0;
3819
3820 private->ag_targets[index] = val;
3821
3822 /* Send new value to the device */
3823 err = scarlett2_usb_set_config(
3824 mixer, scarlett2_ag_target_configs[index], 1, -val);
3825
3826 return err < 0 ? err : 1;
3827 }
3828
3829 static const DECLARE_TLV_DB_MINMAX(
3830 db_scale_ag_target, SCARLETT2_AG_TARGET_MIN * 100, 0
3831 );
3832
3833 static const struct snd_kcontrol_new scarlett2_ag_target_ctl = {
3834 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
3835 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3836 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3837 .name = "",
3838 .info = scarlett2_ag_target_ctl_info,
3839 .get = scarlett2_ag_target_ctl_get,
3840 .put = scarlett2_ag_target_ctl_put,
3841 .tlv = { .p = db_scale_ag_target }
3842 };
3843
3844 /*** Input Select Control ***/
3845
scarlett2_update_input_select(struct usb_mixer_interface * mixer)3846 static int scarlett2_update_input_select(struct usb_mixer_interface *mixer)
3847 {
3848 struct scarlett2_data *private = mixer->private_data;
3849 const struct scarlett2_device_info *info = private->info;
3850 int link_count = info->gain_input_count;
3851 int err;
3852
3853 private->input_select_updated = 0;
3854
3855 if (!scarlett2_has_config_item(private,
3856 SCARLETT2_CONFIG_INPUT_SELECT_SWITCH) ||
3857 !link_count)
3858 return 0;
3859
3860 err = scarlett2_usb_get_config(
3861 mixer, SCARLETT2_CONFIG_INPUT_SELECT_SWITCH,
3862 1, &private->input_select_switch);
3863 if (err < 0)
3864 return err;
3865
3866 err = scarlett2_usb_get_config(
3867 mixer, SCARLETT2_CONFIG_INPUT_LINK_SWITCH,
3868 link_count, private->input_link_switch);
3869 if (err < 0)
3870 return err;
3871
3872 return 0;
3873 }
3874
scarlett2_input_select_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)3875 static int scarlett2_input_select_ctl_get(
3876 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3877 {
3878 struct usb_mixer_elem_info *elem = kctl->private_data;
3879 struct usb_mixer_interface *mixer = elem->head.mixer;
3880 struct scarlett2_data *private = mixer->private_data;
3881 int err;
3882
3883 guard(mutex)(&private->data_mutex);
3884
3885 if (private->hwdep_in_use)
3886 return -EBUSY;
3887
3888 if (private->input_select_updated) {
3889 err = scarlett2_update_input_select(mixer);
3890 if (err < 0)
3891 return err;
3892 }
3893 ucontrol->value.enumerated.item[0] = private->input_select_switch;
3894 return 0;
3895 }
3896
scarlett2_input_select_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)3897 static int scarlett2_input_select_ctl_put(
3898 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3899 {
3900 struct usb_mixer_elem_info *elem = kctl->private_data;
3901 struct usb_mixer_interface *mixer = elem->head.mixer;
3902 struct scarlett2_data *private = mixer->private_data;
3903 const struct scarlett2_device_info *info = private->info;
3904 int oval, val, err;
3905
3906 guard(mutex)(&private->data_mutex);
3907
3908 if (private->hwdep_in_use)
3909 return -EBUSY;
3910
3911 err = scarlett2_check_put_during_autogain(mixer);
3912 if (err < 0)
3913 return err;
3914
3915 oval = private->input_select_switch;
3916 val = ucontrol->value.integer.value[0];
3917
3918 if (val < 0)
3919 val = 0;
3920 else if (val >= info->gain_input_count)
3921 val = info->gain_input_count - 1;
3922
3923 if (oval == val)
3924 return 0;
3925
3926 private->input_select_switch = val;
3927
3928 /* Send new value to the device */
3929 err = scarlett2_usb_set_config(
3930 mixer, SCARLETT2_CONFIG_INPUT_SELECT_SWITCH,
3931 0, val);
3932
3933 return err < 0 ? err : 1;
3934 }
3935
scarlett2_input_select_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)3936 static int scarlett2_input_select_ctl_info(
3937 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3938 {
3939 struct usb_mixer_elem_info *elem = kctl->private_data;
3940 struct usb_mixer_interface *mixer = elem->head.mixer;
3941 struct scarlett2_data *private = mixer->private_data;
3942 int inputs = private->info->gain_input_count;
3943 int i, err;
3944 char **values __free(kfree) =
3945 kcalloc(inputs, sizeof(char *), GFP_KERNEL);
3946
3947 if (!values)
3948 return -ENOMEM;
3949
3950 guard(mutex)(&private->data_mutex);
3951
3952 if (private->hwdep_in_use)
3953 return -EBUSY;
3954
3955 err = scarlett2_check_autogain_updated(mixer);
3956 if (err < 0)
3957 return err;
3958
3959 /* Loop through each input */
3960 for (i = 0; i < inputs; i++) {
3961 values[i] = kasprintf(GFP_KERNEL, "Input %d", i + 1);
3962 if (!values[i]) {
3963 err = -ENOMEM;
3964 goto clear;
3965 }
3966 }
3967
3968 err = snd_ctl_enum_info(uinfo, 1, i,
3969 (const char * const *)values);
3970
3971 clear:
3972 for (i = 0; i < inputs; i++)
3973 kfree(values[i]);
3974
3975 return err;
3976 }
3977
3978 static const struct snd_kcontrol_new scarlett2_input_select_ctl = {
3979 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3980 .name = "",
3981 .info = scarlett2_input_select_ctl_info,
3982 .get = scarlett2_input_select_ctl_get,
3983 .put = scarlett2_input_select_ctl_put,
3984 };
3985
3986 /*** Input Link Switch Controls ***/
3987
3988 /* snd_ctl_boolean_mono_info() with autogain-updated check
3989 * (for controls that are read-only while autogain is running)
3990 */
scarlett2_autogain_disables_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)3991 static int scarlett2_autogain_disables_ctl_info(struct snd_kcontrol *kctl,
3992 struct snd_ctl_elem_info *uinfo)
3993 {
3994 struct usb_mixer_elem_info *elem = kctl->private_data;
3995 struct usb_mixer_interface *mixer = elem->head.mixer;
3996 struct scarlett2_data *private = mixer->private_data;
3997 int err;
3998
3999 guard(mutex)(&private->data_mutex);
4000
4001 if (private->hwdep_in_use)
4002 return -EBUSY;
4003
4004 err = scarlett2_check_autogain_updated(mixer);
4005 if (err < 0)
4006 return err;
4007
4008 return snd_ctl_boolean_mono_info(kctl, uinfo);
4009 }
4010
scarlett2_input_link_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4011 static int scarlett2_input_link_ctl_get(
4012 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
4013 {
4014 struct usb_mixer_elem_info *elem = kctl->private_data;
4015 struct usb_mixer_interface *mixer = elem->head.mixer;
4016 struct scarlett2_data *private = mixer->private_data;
4017 int err;
4018
4019 guard(mutex)(&private->data_mutex);
4020
4021 if (private->hwdep_in_use)
4022 return -EBUSY;
4023
4024 if (private->input_select_updated) {
4025 err = scarlett2_update_input_select(mixer);
4026 if (err < 0)
4027 return err;
4028 }
4029 ucontrol->value.enumerated.item[0] =
4030 private->input_link_switch[elem->control];
4031 return 0;
4032 }
4033
scarlett2_input_link_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4034 static int scarlett2_input_link_ctl_put(
4035 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
4036 {
4037 struct usb_mixer_elem_info *elem = kctl->private_data;
4038 struct usb_mixer_interface *mixer = elem->head.mixer;
4039 struct scarlett2_data *private = mixer->private_data;
4040 int index = elem->control;
4041 int oval, val, err;
4042
4043 guard(mutex)(&private->data_mutex);
4044
4045 if (private->hwdep_in_use)
4046 return -EBUSY;
4047
4048 err = scarlett2_check_put_during_autogain(mixer);
4049 if (err < 0)
4050 return err;
4051
4052 oval = private->input_link_switch[index];
4053 val = !!ucontrol->value.integer.value[0];
4054
4055 if (oval == val)
4056 return 0;
4057
4058 private->input_link_switch[index] = val;
4059
4060 err = scarlett2_usb_set_config(
4061 mixer, SCARLETT2_CONFIG_INPUT_LINK_SWITCH, index, val);
4062
4063 return err < 0 ? err : 1;
4064 }
4065
4066 static const struct snd_kcontrol_new scarlett2_input_link_ctl = {
4067 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4068 .name = "",
4069 .info = scarlett2_autogain_disables_ctl_info,
4070 .get = scarlett2_input_link_ctl_get,
4071 .put = scarlett2_input_link_ctl_put
4072 };
4073
4074 /*** Input Gain Controls ***/
4075
scarlett2_update_input_gain(struct usb_mixer_interface * mixer)4076 static int scarlett2_update_input_gain(struct usb_mixer_interface *mixer)
4077 {
4078 struct scarlett2_data *private = mixer->private_data;
4079 const struct scarlett2_device_info *info = private->info;
4080
4081 private->input_gain_updated = 0;
4082
4083 if (!info->gain_input_count)
4084 return 0;
4085
4086 return scarlett2_usb_get_config(
4087 mixer, SCARLETT2_CONFIG_INPUT_GAIN,
4088 info->gain_input_count, private->gain);
4089 }
4090
scarlett2_input_gain_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)4091 static int scarlett2_input_gain_ctl_info(struct snd_kcontrol *kctl,
4092 struct snd_ctl_elem_info *uinfo)
4093 {
4094 struct usb_mixer_elem_info *elem = kctl->private_data;
4095 struct usb_mixer_interface *mixer = elem->head.mixer;
4096 struct scarlett2_data *private = mixer->private_data;
4097 int err;
4098
4099 guard(mutex)(&private->data_mutex);
4100
4101 if (private->hwdep_in_use)
4102 return -EBUSY;
4103
4104 err = scarlett2_check_autogain_updated(mixer);
4105 if (err < 0)
4106 return err;
4107
4108 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
4109 uinfo->count = elem->channels;
4110 uinfo->value.integer.min = 0;
4111 uinfo->value.integer.max = SCARLETT2_MAX_GAIN_VALUE;
4112 uinfo->value.integer.step = 1;
4113
4114 return 0;
4115 }
4116
scarlett2_input_gain_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4117 static int scarlett2_input_gain_ctl_get(struct snd_kcontrol *kctl,
4118 struct snd_ctl_elem_value *ucontrol)
4119 {
4120 struct usb_mixer_elem_info *elem = kctl->private_data;
4121 struct usb_mixer_interface *mixer = elem->head.mixer;
4122 struct scarlett2_data *private = mixer->private_data;
4123 int err;
4124
4125 guard(mutex)(&private->data_mutex);
4126
4127 if (private->hwdep_in_use)
4128 return -EBUSY;
4129
4130 if (private->input_gain_updated) {
4131 err = scarlett2_update_input_gain(mixer);
4132 if (err < 0)
4133 return err;
4134 }
4135 ucontrol->value.integer.value[0] =
4136 private->gain[elem->control];
4137 return 0;
4138 }
4139
scarlett2_input_gain_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4140 static int scarlett2_input_gain_ctl_put(struct snd_kcontrol *kctl,
4141 struct snd_ctl_elem_value *ucontrol)
4142 {
4143 struct usb_mixer_elem_info *elem = kctl->private_data;
4144 struct usb_mixer_interface *mixer = elem->head.mixer;
4145 struct scarlett2_data *private = mixer->private_data;
4146 int index = elem->control;
4147 int oval, val, err;
4148
4149 guard(mutex)(&private->data_mutex);
4150
4151 if (private->hwdep_in_use)
4152 return -EBUSY;
4153
4154 err = scarlett2_check_put_during_autogain(mixer);
4155 if (err < 0)
4156 return err;
4157
4158 oval = private->gain[index];
4159 val = ucontrol->value.integer.value[0];
4160
4161 if (oval == val)
4162 return 0;
4163
4164 private->gain[index] = val;
4165
4166 /* Send gain change to the device */
4167 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_INPUT_GAIN,
4168 index, val);
4169
4170 return err < 0 ? err : 1;
4171 }
4172
4173 static const struct snd_kcontrol_new scarlett2_input_gain_ctl = {
4174 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4175 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
4176 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
4177 .name = "",
4178 .info = scarlett2_input_gain_ctl_info,
4179 .get = scarlett2_input_gain_ctl_get,
4180 .put = scarlett2_input_gain_ctl_put,
4181 .private_value = 0, /* max value */
4182 };
4183
4184 /*** Safe Controls ***/
4185
scarlett2_update_input_safe(struct usb_mixer_interface * mixer)4186 static int scarlett2_update_input_safe(struct usb_mixer_interface *mixer)
4187 {
4188 struct scarlett2_data *private = mixer->private_data;
4189 const struct scarlett2_device_info *info = private->info;
4190
4191 private->input_safe_updated = 0;
4192
4193 if (!info->safe_input_count)
4194 return 0;
4195
4196 return scarlett2_usb_get_config(
4197 mixer, SCARLETT2_CONFIG_SAFE_SWITCH,
4198 info->safe_input_count, private->safe_switch);
4199 }
4200
scarlett2_safe_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4201 static int scarlett2_safe_ctl_get(struct snd_kcontrol *kctl,
4202 struct snd_ctl_elem_value *ucontrol)
4203 {
4204 struct usb_mixer_elem_info *elem = kctl->private_data;
4205 struct usb_mixer_interface *mixer = elem->head.mixer;
4206 struct scarlett2_data *private = mixer->private_data;
4207 int err;
4208
4209 guard(mutex)(&private->data_mutex);
4210
4211 if (private->hwdep_in_use)
4212 return -EBUSY;
4213
4214 if (private->input_safe_updated) {
4215 err = scarlett2_update_input_safe(mixer);
4216 if (err < 0)
4217 return err;
4218 }
4219 ucontrol->value.integer.value[0] =
4220 private->safe_switch[elem->control];
4221 return 0;
4222 }
4223
scarlett2_safe_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4224 static int scarlett2_safe_ctl_put(struct snd_kcontrol *kctl,
4225 struct snd_ctl_elem_value *ucontrol)
4226 {
4227 struct usb_mixer_elem_info *elem = kctl->private_data;
4228 struct usb_mixer_interface *mixer = elem->head.mixer;
4229 struct scarlett2_data *private = mixer->private_data;
4230 int index = elem->control;
4231 int oval, val, err;
4232
4233 guard(mutex)(&private->data_mutex);
4234
4235 if (private->hwdep_in_use)
4236 return -EBUSY;
4237
4238 err = scarlett2_check_put_during_autogain(mixer);
4239 if (err < 0)
4240 return err;
4241
4242 oval = private->safe_switch[index];
4243 val = !!ucontrol->value.integer.value[0];
4244
4245 if (oval == val)
4246 return 0;
4247
4248 private->safe_switch[index] = val;
4249
4250 /* Send switch change to the device */
4251 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SAFE_SWITCH,
4252 index, val);
4253
4254 return err < 0 ? err : 1;
4255 }
4256
4257 static const struct snd_kcontrol_new scarlett2_safe_ctl = {
4258 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4259 .name = "",
4260 .info = scarlett2_autogain_disables_ctl_info,
4261 .get = scarlett2_safe_ctl_get,
4262 .put = scarlett2_safe_ctl_put,
4263 };
4264
4265 /*** PCM Input Control ***/
4266
scarlett2_update_pcm_input_switch(struct usb_mixer_interface * mixer)4267 static int scarlett2_update_pcm_input_switch(struct usb_mixer_interface *mixer)
4268 {
4269 struct scarlett2_data *private = mixer->private_data;
4270 int err;
4271
4272 private->pcm_input_switch_updated = 0;
4273
4274 err = scarlett2_usb_get_config(
4275 mixer, SCARLETT2_CONFIG_PCM_INPUT_SWITCH,
4276 1, &private->pcm_input_switch);
4277 if (err < 0)
4278 return err;
4279
4280 return 0;
4281 }
4282
scarlett2_pcm_input_switch_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4283 static int scarlett2_pcm_input_switch_ctl_get(
4284 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
4285 {
4286 struct usb_mixer_elem_info *elem = kctl->private_data;
4287 struct usb_mixer_interface *mixer = elem->head.mixer;
4288 struct scarlett2_data *private = elem->head.mixer->private_data;
4289 int err;
4290
4291 guard(mutex)(&private->data_mutex);
4292
4293 if (private->pcm_input_switch_updated) {
4294 err = scarlett2_update_pcm_input_switch(mixer);
4295 if (err < 0)
4296 return err;
4297 }
4298 ucontrol->value.enumerated.item[0] = private->pcm_input_switch;
4299 return 0;
4300 }
4301
scarlett2_pcm_input_switch_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4302 static int scarlett2_pcm_input_switch_ctl_put(
4303 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
4304 {
4305 struct usb_mixer_elem_info *elem = kctl->private_data;
4306 struct usb_mixer_interface *mixer = elem->head.mixer;
4307 struct scarlett2_data *private = mixer->private_data;
4308 int oval, val, err;
4309
4310 guard(mutex)(&private->data_mutex);
4311
4312 if (private->hwdep_in_use)
4313 return -EBUSY;
4314
4315 oval = private->pcm_input_switch;
4316 val = !!ucontrol->value.integer.value[0];
4317
4318 if (oval == val)
4319 return 0;
4320
4321 private->pcm_input_switch = val;
4322
4323 /* Send switch change to the device */
4324 err = scarlett2_usb_set_config(
4325 mixer, SCARLETT2_CONFIG_PCM_INPUT_SWITCH,
4326 0, val);
4327
4328 return err < 0 ? err : 1;
4329 }
4330
scarlett2_pcm_input_switch_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)4331 static int scarlett2_pcm_input_switch_ctl_info(
4332 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
4333 {
4334 static const char *const values[2] = {
4335 "Direct", "Mixer"
4336 };
4337
4338 return snd_ctl_enum_info(
4339 uinfo, 1, 2, values);
4340 }
4341
4342 static const struct snd_kcontrol_new scarlett2_pcm_input_switch_ctl = {
4343 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4344 .name = "",
4345 .info = scarlett2_pcm_input_switch_ctl_info,
4346 .get = scarlett2_pcm_input_switch_ctl_get,
4347 .put = scarlett2_pcm_input_switch_ctl_put
4348 };
4349
4350 /*** Analogue Line Out Volume Controls ***/
4351
4352 /* Update hardware volume controls after receiving notification that
4353 * they have changed
4354 */
scarlett2_update_volumes(struct usb_mixer_interface * mixer)4355 static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
4356 {
4357 struct scarlett2_data *private = mixer->private_data;
4358 s16 vol;
4359 int err, i;
4360
4361 private->vol_updated = 0;
4362
4363 if (scarlett2_has_config_item(private,
4364 SCARLETT2_CONFIG_MASTER_VOLUME)) {
4365 err = scarlett2_usb_get_config(
4366 mixer, SCARLETT2_CONFIG_MASTER_VOLUME,
4367 1, &vol);
4368 if (err < 0)
4369 return err;
4370
4371 private->master_vol = clamp(vol + SCARLETT2_VOLUME_BIAS,
4372 0, SCARLETT2_VOLUME_BIAS);
4373
4374 if (scarlett2_has_config_item(private,
4375 SCARLETT2_CONFIG_SW_HW_SWITCH))
4376 for (i = 0; i < private->num_line_out; i++)
4377 if (private->vol_sw_hw_switch[i])
4378 private->vol[i] = private->master_vol;
4379 }
4380
4381 if (scarlett2_has_config_item(private,
4382 SCARLETT2_CONFIG_HEADPHONE_VOLUME)) {
4383 err = scarlett2_usb_get_config(
4384 mixer, SCARLETT2_CONFIG_HEADPHONE_VOLUME,
4385 1, &vol);
4386 if (err < 0)
4387 return err;
4388
4389 private->headphone_vol = clamp(vol + SCARLETT2_VOLUME_BIAS,
4390 0, SCARLETT2_VOLUME_BIAS);
4391 }
4392
4393 return 0;
4394 }
4395
scarlett2_volume_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)4396 static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
4397 struct snd_ctl_elem_info *uinfo)
4398 {
4399 struct usb_mixer_elem_info *elem = kctl->private_data;
4400
4401 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
4402 uinfo->count = elem->channels;
4403 uinfo->value.integer.min = 0;
4404 uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
4405 uinfo->value.integer.step = 1;
4406 return 0;
4407 }
4408
scarlett2_master_volume_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4409 static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
4410 struct snd_ctl_elem_value *ucontrol)
4411 {
4412 struct usb_mixer_elem_info *elem = kctl->private_data;
4413 struct usb_mixer_interface *mixer = elem->head.mixer;
4414 struct scarlett2_data *private = mixer->private_data;
4415 int err;
4416
4417 guard(mutex)(&private->data_mutex);
4418
4419 if (private->hwdep_in_use)
4420 return -EBUSY;
4421
4422 if (private->vol_updated) {
4423 err = scarlett2_update_volumes(mixer);
4424 if (err < 0)
4425 return err;
4426 }
4427 ucontrol->value.integer.value[0] = private->master_vol;
4428 return 0;
4429 }
4430
scarlett2_headphone_volume_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4431 static int scarlett2_headphone_volume_ctl_get(
4432 struct snd_kcontrol *kctl,
4433 struct snd_ctl_elem_value *ucontrol)
4434 {
4435 struct usb_mixer_elem_info *elem = kctl->private_data;
4436 struct usb_mixer_interface *mixer = elem->head.mixer;
4437 struct scarlett2_data *private = mixer->private_data;
4438 int err;
4439
4440 guard(mutex)(&private->data_mutex);
4441
4442 if (private->hwdep_in_use)
4443 return -EBUSY;
4444
4445 if (private->vol_updated) {
4446 err = scarlett2_update_volumes(mixer);
4447 if (err < 0)
4448 return err;
4449 }
4450 ucontrol->value.integer.value[0] = private->headphone_vol;
4451 return 0;
4452 }
4453
line_out_remap(struct scarlett2_data * private,int index)4454 static int line_out_remap(struct scarlett2_data *private, int index)
4455 {
4456 const struct scarlett2_device_info *info = private->info;
4457
4458 if (!info->line_out_remap_enable)
4459 return index;
4460
4461 if (index >= private->num_line_out)
4462 return index;
4463
4464 return info->line_out_remap[index];
4465 }
4466
scarlett2_volume_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4467 static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
4468 struct snd_ctl_elem_value *ucontrol)
4469 {
4470 struct usb_mixer_elem_info *elem = kctl->private_data;
4471 struct usb_mixer_interface *mixer = elem->head.mixer;
4472 struct scarlett2_data *private = mixer->private_data;
4473 int index = line_out_remap(private, elem->control);
4474 int err;
4475
4476 guard(mutex)(&private->data_mutex);
4477
4478 if (private->hwdep_in_use)
4479 return -EBUSY;
4480
4481 if (private->vol_updated) {
4482 err = scarlett2_update_volumes(mixer);
4483 if (err < 0)
4484 return err;
4485 }
4486 ucontrol->value.integer.value[0] = private->vol[index];
4487 return 0;
4488 }
4489
scarlett2_volume_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4490 static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
4491 struct snd_ctl_elem_value *ucontrol)
4492 {
4493 struct usb_mixer_elem_info *elem = kctl->private_data;
4494 struct usb_mixer_interface *mixer = elem->head.mixer;
4495 struct scarlett2_data *private = mixer->private_data;
4496 int index = line_out_remap(private, elem->control);
4497 int oval, val, err;
4498
4499 guard(mutex)(&private->data_mutex);
4500
4501 if (private->hwdep_in_use)
4502 return -EBUSY;
4503
4504 oval = private->vol[index];
4505 val = ucontrol->value.integer.value[0];
4506
4507 if (oval == val)
4508 return 0;
4509
4510 private->vol[index] = val;
4511 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
4512 index, val - SCARLETT2_VOLUME_BIAS);
4513
4514 return err < 0 ? err : 1;
4515 }
4516
4517 static const DECLARE_TLV_DB_MINMAX(
4518 db_scale_scarlett2_volume, -SCARLETT2_VOLUME_BIAS * 100, 0
4519 );
4520
4521 static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
4522 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4523 .access = SNDRV_CTL_ELEM_ACCESS_READ |
4524 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
4525 .name = "",
4526 .info = scarlett2_volume_ctl_info,
4527 .get = scarlett2_master_volume_ctl_get,
4528 .private_value = 0, /* max value */
4529 .tlv = { .p = db_scale_scarlett2_volume }
4530 };
4531
4532 static const struct snd_kcontrol_new scarlett2_headphone_volume_ctl = {
4533 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4534 .access = SNDRV_CTL_ELEM_ACCESS_READ |
4535 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
4536 .name = "",
4537 .info = scarlett2_volume_ctl_info,
4538 .get = scarlett2_headphone_volume_ctl_get,
4539 .private_value = 0, /* max value */
4540 .tlv = { .p = db_scale_scarlett2_volume }
4541 };
4542
4543 static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
4544 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4545 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
4546 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
4547 .name = "",
4548 .info = scarlett2_volume_ctl_info,
4549 .get = scarlett2_volume_ctl_get,
4550 .put = scarlett2_volume_ctl_put,
4551 .private_value = 0, /* max value */
4552 .tlv = { .p = db_scale_scarlett2_volume }
4553 };
4554
4555 /*** Mute Switch Controls ***/
4556
scarlett2_update_dim_mute(struct usb_mixer_interface * mixer)4557 static int scarlett2_update_dim_mute(struct usb_mixer_interface *mixer)
4558 {
4559 struct scarlett2_data *private = mixer->private_data;
4560 int err, i;
4561 u8 mute;
4562
4563 private->dim_mute_updated = 0;
4564
4565 if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
4566 return 0;
4567
4568 err = scarlett2_usb_get_config(
4569 mixer, SCARLETT2_CONFIG_DIM_MUTE,
4570 SCARLETT2_DIM_MUTE_COUNT, private->dim_mute);
4571 if (err < 0)
4572 return err;
4573
4574 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
4575 private->dim_mute[i] = !!private->dim_mute[i];
4576
4577 mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];
4578
4579 for (i = 0; i < private->num_line_out; i++)
4580 if (private->vol_sw_hw_switch[i])
4581 private->mute_switch[i] = mute;
4582
4583 return 0;
4584 }
4585
scarlett2_mute_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4586 static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
4587 struct snd_ctl_elem_value *ucontrol)
4588 {
4589 struct usb_mixer_elem_info *elem = kctl->private_data;
4590 struct usb_mixer_interface *mixer = elem->head.mixer;
4591 struct scarlett2_data *private = mixer->private_data;
4592 int index = line_out_remap(private, elem->control);
4593 int err;
4594
4595 guard(mutex)(&private->data_mutex);
4596
4597 if (private->hwdep_in_use)
4598 return -EBUSY;
4599
4600 if (private->dim_mute_updated) {
4601 err = scarlett2_update_dim_mute(mixer);
4602 if (err < 0)
4603 return err;
4604 }
4605 ucontrol->value.integer.value[0] = private->mute_switch[index];
4606 return 0;
4607 }
4608
scarlett2_mute_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4609 static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
4610 struct snd_ctl_elem_value *ucontrol)
4611 {
4612 struct usb_mixer_elem_info *elem = kctl->private_data;
4613 struct usb_mixer_interface *mixer = elem->head.mixer;
4614 struct scarlett2_data *private = mixer->private_data;
4615 int index = line_out_remap(private, elem->control);
4616 int oval, val, err;
4617
4618 guard(mutex)(&private->data_mutex);
4619
4620 if (private->hwdep_in_use)
4621 return -EBUSY;
4622
4623 oval = private->mute_switch[index];
4624 val = !!ucontrol->value.integer.value[0];
4625
4626 if (oval == val)
4627 return 0;
4628
4629 private->mute_switch[index] = val;
4630
4631 /* Send mute change to the device */
4632 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
4633 index, val);
4634
4635 return err < 0 ? err : 1;
4636 }
4637
4638 static const struct snd_kcontrol_new scarlett2_mute_ctl = {
4639 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4640 .name = "",
4641 .info = snd_ctl_boolean_mono_info,
4642 .get = scarlett2_mute_ctl_get,
4643 .put = scarlett2_mute_ctl_put,
4644 };
4645
4646 /*** HW/SW Volume Switch Controls ***/
4647
scarlett2_sw_hw_ctl_ro(struct scarlett2_data * private,int index)4648 static void scarlett2_sw_hw_ctl_ro(struct scarlett2_data *private, int index)
4649 {
4650 private->sw_hw_ctls[index]->vd[0].access &=
4651 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
4652 }
4653
scarlett2_sw_hw_ctl_rw(struct scarlett2_data * private,int index)4654 static void scarlett2_sw_hw_ctl_rw(struct scarlett2_data *private, int index)
4655 {
4656 private->sw_hw_ctls[index]->vd[0].access |=
4657 SNDRV_CTL_ELEM_ACCESS_WRITE;
4658 }
4659
scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)4660 static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
4661 struct snd_ctl_elem_info *uinfo)
4662 {
4663 static const char *const values[2] = {
4664 "SW", "HW"
4665 };
4666
4667 return snd_ctl_enum_info(uinfo, 1, 2, values);
4668 }
4669
scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4670 static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
4671 struct snd_ctl_elem_value *ucontrol)
4672 {
4673 struct usb_mixer_elem_info *elem = kctl->private_data;
4674 struct scarlett2_data *private = elem->head.mixer->private_data;
4675 int index = line_out_remap(private, elem->control);
4676
4677 ucontrol->value.enumerated.item[0] = private->vol_sw_hw_switch[index];
4678 return 0;
4679 }
4680
scarlett2_vol_ctl_set_writable(struct usb_mixer_interface * mixer,int index,int value)4681 static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
4682 int index, int value)
4683 {
4684 struct scarlett2_data *private = mixer->private_data;
4685 struct snd_card *card = mixer->chip->card;
4686
4687 /* Set/Clear write bits */
4688 if (value) {
4689 private->vol_ctls[index]->vd[0].access |=
4690 SNDRV_CTL_ELEM_ACCESS_WRITE;
4691 private->mute_ctls[index]->vd[0].access |=
4692 SNDRV_CTL_ELEM_ACCESS_WRITE;
4693 } else {
4694 private->vol_ctls[index]->vd[0].access &=
4695 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
4696 private->mute_ctls[index]->vd[0].access &=
4697 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
4698 }
4699
4700 /* Notify of write bit and possible value change */
4701 snd_ctl_notify(card,
4702 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
4703 &private->vol_ctls[index]->id);
4704 snd_ctl_notify(card,
4705 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
4706 &private->mute_ctls[index]->id);
4707 }
4708
scarlett2_sw_hw_change(struct usb_mixer_interface * mixer,int ctl_index,int val)4709 static int scarlett2_sw_hw_change(struct usb_mixer_interface *mixer,
4710 int ctl_index, int val)
4711 {
4712 struct scarlett2_data *private = mixer->private_data;
4713 int index = line_out_remap(private, ctl_index);
4714 int err;
4715
4716 private->vol_sw_hw_switch[index] = val;
4717
4718 /* Change access mode to RO (hardware controlled volume)
4719 * or RW (software controlled volume)
4720 */
4721 scarlett2_vol_ctl_set_writable(mixer, ctl_index, !val);
4722
4723 /* Reset volume/mute to master volume/mute */
4724 private->vol[index] = private->master_vol;
4725 private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE];
4726
4727 /* Set SW volume to current HW volume */
4728 err = scarlett2_usb_set_config(
4729 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
4730 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
4731 if (err < 0)
4732 return err;
4733
4734 /* Set SW mute to current HW mute */
4735 err = scarlett2_usb_set_config(
4736 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
4737 index, private->dim_mute[SCARLETT2_BUTTON_MUTE]);
4738 if (err < 0)
4739 return err;
4740
4741 /* Send SW/HW switch change to the device */
4742 return scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
4743 index, val);
4744 }
4745
scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4746 static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
4747 struct snd_ctl_elem_value *ucontrol)
4748 {
4749 struct usb_mixer_elem_info *elem = kctl->private_data;
4750 struct usb_mixer_interface *mixer = elem->head.mixer;
4751 struct scarlett2_data *private = mixer->private_data;
4752 int ctl_index = elem->control;
4753 int index = line_out_remap(private, ctl_index);
4754 int oval, val, err;
4755
4756 guard(mutex)(&private->data_mutex);
4757
4758 if (private->hwdep_in_use)
4759 return -EBUSY;
4760
4761 oval = private->vol_sw_hw_switch[index];
4762 val = !!ucontrol->value.enumerated.item[0];
4763
4764 if (oval == val)
4765 return 0;
4766
4767 err = scarlett2_sw_hw_change(mixer, ctl_index, val);
4768
4769 return err < 0 ? err : 1;
4770 }
4771
4772 static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
4773 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4774 .name = "",
4775 .info = scarlett2_sw_hw_enum_ctl_info,
4776 .get = scarlett2_sw_hw_enum_ctl_get,
4777 .put = scarlett2_sw_hw_enum_ctl_put,
4778 };
4779
4780 /*** Line Level/Instrument Level Switch Controls ***/
4781
scarlett2_update_input_level(struct usb_mixer_interface * mixer)4782 static int scarlett2_update_input_level(struct usb_mixer_interface *mixer)
4783 {
4784 struct scarlett2_data *private = mixer->private_data;
4785 const struct scarlett2_device_info *info = private->info;
4786
4787 private->input_level_updated = 0;
4788
4789 if (!info->level_input_count)
4790 return 0;
4791
4792 return scarlett2_usb_get_config(
4793 mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
4794 info->level_input_count + info->level_input_first,
4795 private->level_switch);
4796 }
4797
scarlett2_level_enum_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)4798 static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
4799 struct snd_ctl_elem_info *uinfo)
4800 {
4801 static const char *const values[2] = {
4802 "Line", "Inst"
4803 };
4804 struct usb_mixer_elem_info *elem = kctl->private_data;
4805 struct usb_mixer_interface *mixer = elem->head.mixer;
4806 struct scarlett2_data *private = mixer->private_data;
4807 int err;
4808
4809 guard(mutex)(&private->data_mutex);
4810
4811 if (private->hwdep_in_use)
4812 return -EBUSY;
4813
4814 err = scarlett2_check_autogain_updated(mixer);
4815 if (err < 0)
4816 return err;
4817
4818 return snd_ctl_enum_info(uinfo, 1, 2, values);
4819 }
4820
scarlett2_level_enum_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4821 static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
4822 struct snd_ctl_elem_value *ucontrol)
4823 {
4824 struct usb_mixer_elem_info *elem = kctl->private_data;
4825 struct usb_mixer_interface *mixer = elem->head.mixer;
4826 struct scarlett2_data *private = mixer->private_data;
4827 const struct scarlett2_device_info *info = private->info;
4828 int index = elem->control + info->level_input_first;
4829 int err;
4830
4831 guard(mutex)(&private->data_mutex);
4832
4833 if (private->hwdep_in_use)
4834 return -EBUSY;
4835
4836 if (private->input_level_updated) {
4837 err = scarlett2_update_input_level(mixer);
4838 if (err < 0)
4839 return err;
4840 }
4841 ucontrol->value.enumerated.item[0] = scarlett2_decode_muteable(
4842 private->level_switch[index]);
4843 return 0;
4844 }
4845
scarlett2_level_enum_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4846 static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
4847 struct snd_ctl_elem_value *ucontrol)
4848 {
4849 struct usb_mixer_elem_info *elem = kctl->private_data;
4850 struct usb_mixer_interface *mixer = elem->head.mixer;
4851 struct scarlett2_data *private = mixer->private_data;
4852 const struct scarlett2_device_info *info = private->info;
4853 int index = elem->control + info->level_input_first;
4854 int oval, val, err;
4855
4856 guard(mutex)(&private->data_mutex);
4857
4858 if (private->hwdep_in_use)
4859 return -EBUSY;
4860
4861 err = scarlett2_check_put_during_autogain(mixer);
4862 if (err < 0)
4863 return err;
4864
4865 oval = private->level_switch[index];
4866 val = !!ucontrol->value.enumerated.item[0];
4867
4868 if (oval == val)
4869 return 0;
4870
4871 private->level_switch[index] = val;
4872
4873 /* To set the Gen 4 muteable controls, bit 1 gets set instead */
4874 if (private->config_set->items[SCARLETT2_CONFIG_LEVEL_SWITCH].mute)
4875 val = (!val) | 0x02;
4876
4877 /* Send switch change to the device */
4878 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
4879 index, val);
4880
4881 return err < 0 ? err : 1;
4882 }
4883
4884 static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
4885 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4886 .name = "",
4887 .info = scarlett2_level_enum_ctl_info,
4888 .get = scarlett2_level_enum_ctl_get,
4889 .put = scarlett2_level_enum_ctl_put,
4890 };
4891
4892 /*** Pad Switch Controls ***/
4893
scarlett2_update_input_pad(struct usb_mixer_interface * mixer)4894 static int scarlett2_update_input_pad(struct usb_mixer_interface *mixer)
4895 {
4896 struct scarlett2_data *private = mixer->private_data;
4897 const struct scarlett2_device_info *info = private->info;
4898
4899 private->input_pad_updated = 0;
4900
4901 if (!info->pad_input_count)
4902 return 0;
4903
4904 return scarlett2_usb_get_config(
4905 mixer, SCARLETT2_CONFIG_PAD_SWITCH,
4906 info->pad_input_count, private->pad_switch);
4907 }
4908
scarlett2_pad_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4909 static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
4910 struct snd_ctl_elem_value *ucontrol)
4911 {
4912 struct usb_mixer_elem_info *elem = kctl->private_data;
4913 struct usb_mixer_interface *mixer = elem->head.mixer;
4914 struct scarlett2_data *private = mixer->private_data;
4915 int err;
4916
4917 guard(mutex)(&private->data_mutex);
4918
4919 if (private->hwdep_in_use)
4920 return -EBUSY;
4921
4922 if (private->input_pad_updated) {
4923 err = scarlett2_update_input_pad(mixer);
4924 if (err < 0)
4925 return err;
4926 }
4927 ucontrol->value.integer.value[0] =
4928 private->pad_switch[elem->control];
4929 return 0;
4930 }
4931
scarlett2_pad_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4932 static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
4933 struct snd_ctl_elem_value *ucontrol)
4934 {
4935 struct usb_mixer_elem_info *elem = kctl->private_data;
4936 struct usb_mixer_interface *mixer = elem->head.mixer;
4937 struct scarlett2_data *private = mixer->private_data;
4938 int index = elem->control;
4939 int oval, val, err;
4940
4941 guard(mutex)(&private->data_mutex);
4942
4943 if (private->hwdep_in_use)
4944 return -EBUSY;
4945
4946 oval = private->pad_switch[index];
4947 val = !!ucontrol->value.integer.value[0];
4948
4949 if (oval == val)
4950 return 0;
4951
4952 private->pad_switch[index] = val;
4953
4954 /* Send switch change to the device */
4955 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
4956 index, val);
4957
4958 return err < 0 ? err : 1;
4959 }
4960
4961 static const struct snd_kcontrol_new scarlett2_pad_ctl = {
4962 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4963 .name = "",
4964 .info = snd_ctl_boolean_mono_info,
4965 .get = scarlett2_pad_ctl_get,
4966 .put = scarlett2_pad_ctl_put,
4967 };
4968
4969 /*** Air Switch Controls ***/
4970
scarlett2_update_input_air(struct usb_mixer_interface * mixer)4971 static int scarlett2_update_input_air(struct usb_mixer_interface *mixer)
4972 {
4973 struct scarlett2_data *private = mixer->private_data;
4974 const struct scarlett2_device_info *info = private->info;
4975
4976 private->input_air_updated = 0;
4977
4978 if (!info->air_input_count)
4979 return 0;
4980
4981 return scarlett2_usb_get_config(
4982 mixer, SCARLETT2_CONFIG_AIR_SWITCH,
4983 info->air_input_count, private->air_switch);
4984 }
4985
scarlett2_air_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)4986 static int scarlett2_air_ctl_get(struct snd_kcontrol *kctl,
4987 struct snd_ctl_elem_value *ucontrol)
4988 {
4989 struct usb_mixer_elem_info *elem = kctl->private_data;
4990 struct usb_mixer_interface *mixer = elem->head.mixer;
4991 struct scarlett2_data *private = mixer->private_data;
4992 int err;
4993
4994 guard(mutex)(&private->data_mutex);
4995
4996 if (private->hwdep_in_use)
4997 return -EBUSY;
4998
4999 if (private->input_air_updated) {
5000 err = scarlett2_update_input_air(mixer);
5001 if (err < 0)
5002 return err;
5003 }
5004 ucontrol->value.integer.value[0] = private->air_switch[elem->control];
5005 return 0;
5006 }
5007
scarlett2_air_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5008 static int scarlett2_air_ctl_put(struct snd_kcontrol *kctl,
5009 struct snd_ctl_elem_value *ucontrol)
5010 {
5011 struct usb_mixer_elem_info *elem = kctl->private_data;
5012 struct usb_mixer_interface *mixer = elem->head.mixer;
5013 struct scarlett2_data *private = mixer->private_data;
5014 int index = elem->control;
5015 int oval, val, err;
5016
5017 guard(mutex)(&private->data_mutex);
5018
5019 if (private->hwdep_in_use)
5020 return -EBUSY;
5021
5022 err = scarlett2_check_put_during_autogain(mixer);
5023 if (err < 0)
5024 return err;
5025
5026 oval = private->air_switch[index];
5027 val = ucontrol->value.integer.value[0];
5028
5029 if (oval == val)
5030 return 0;
5031
5032 private->air_switch[index] = val;
5033
5034 /* Send switch change to the device */
5035 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_AIR_SWITCH,
5036 index, val);
5037
5038 return err < 0 ? err : 1;
5039 }
5040
scarlett2_air_with_drive_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)5041 static int scarlett2_air_with_drive_ctl_info(
5042 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
5043 {
5044 static const char *const values[3] = {
5045 "Off", "Presence", "Presence + Drive"
5046 };
5047 struct usb_mixer_elem_info *elem = kctl->private_data;
5048 struct usb_mixer_interface *mixer = elem->head.mixer;
5049 struct scarlett2_data *private = mixer->private_data;
5050 int err;
5051
5052 guard(mutex)(&private->data_mutex);
5053
5054 if (private->hwdep_in_use)
5055 return -EBUSY;
5056
5057 err = scarlett2_check_autogain_updated(mixer);
5058 if (err < 0)
5059 return err;
5060
5061 return snd_ctl_enum_info(uinfo, 1, 3, values);
5062 }
5063
5064 static const struct snd_kcontrol_new scarlett2_air_ctl[2] = {
5065 {
5066 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5067 .name = "",
5068 .info = snd_ctl_boolean_mono_info,
5069 .get = scarlett2_air_ctl_get,
5070 .put = scarlett2_air_ctl_put,
5071 },
5072 {
5073 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5074 .name = "",
5075 .info = scarlett2_air_with_drive_ctl_info,
5076 .get = scarlett2_air_ctl_get,
5077 .put = scarlett2_air_ctl_put,
5078 }
5079 };
5080
5081 /*** DSP Switch Control ***/
5082
scarlett2_update_input_dsp(struct usb_mixer_interface * mixer)5083 static int scarlett2_update_input_dsp(struct usb_mixer_interface *mixer)
5084 {
5085 struct scarlett2_data *private = mixer->private_data;
5086 const struct scarlett2_device_info *info = private->info;
5087
5088 private->input_dsp_updated = 0;
5089
5090 if (!info->dsp_input_count)
5091 return 0;
5092
5093 return scarlett2_usb_get_config(
5094 mixer, SCARLETT2_CONFIG_DSP_SWITCH,
5095 info->dsp_input_count, private->dsp_switch);
5096 }
5097
scarlett2_dsp_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5098 static int scarlett2_dsp_ctl_get(struct snd_kcontrol *kctl,
5099 struct snd_ctl_elem_value *ucontrol)
5100 {
5101 struct usb_mixer_elem_info *elem = kctl->private_data;
5102 struct usb_mixer_interface *mixer = elem->head.mixer;
5103 struct scarlett2_data *private = mixer->private_data;
5104 int err;
5105
5106 guard(mutex)(&private->data_mutex);
5107
5108 if (private->hwdep_in_use)
5109 return -EBUSY;
5110
5111 if (private->input_dsp_updated) {
5112 err = scarlett2_update_input_dsp(mixer);
5113 if (err < 0)
5114 return err;
5115 }
5116 ucontrol->value.integer.value[0] = private->dsp_switch[elem->control];
5117 return 0;
5118 }
5119
scarlett2_dsp_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5120 static int scarlett2_dsp_ctl_put(struct snd_kcontrol *kctl,
5121 struct snd_ctl_elem_value *ucontrol)
5122 {
5123 struct usb_mixer_elem_info *elem = kctl->private_data;
5124 struct usb_mixer_interface *mixer = elem->head.mixer;
5125 struct scarlett2_data *private = mixer->private_data;
5126 int index = elem->control;
5127 int oval, val, err;
5128
5129 guard(mutex)(&private->data_mutex);
5130
5131 if (private->hwdep_in_use)
5132 return -EBUSY;
5133
5134 err = scarlett2_check_put_during_autogain(mixer);
5135 if (err < 0)
5136 return err;
5137
5138 oval = private->dsp_switch[index];
5139 val = ucontrol->value.integer.value[0];
5140
5141 if (oval == val)
5142 return 0;
5143
5144 private->dsp_switch[index] = val;
5145
5146 /* Send switch change to the device */
5147 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DSP_SWITCH,
5148 index, val);
5149
5150 return err < 0 ? err : 1;
5151 }
5152
5153 static const struct snd_kcontrol_new scarlett2_dsp_ctl = {
5154 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5155 .name = "",
5156 .info = scarlett2_autogain_disables_ctl_info,
5157 .get = scarlett2_dsp_ctl_get,
5158 .put = scarlett2_dsp_ctl_put,
5159 };
5160
5161 /*** DSP Compressor Parameter Controls ***/
5162
scarlett2_update_compressor_values(struct usb_mixer_interface * mixer)5163 static int scarlett2_update_compressor_values(struct usb_mixer_interface *mixer)
5164 {
5165 struct scarlett2_data *private = mixer->private_data;
5166 const struct scarlett2_device_info *info = private->info;
5167 int err, i, j;
5168
5169 if (!info->dsp_input_count)
5170 return 0;
5171
5172 err = scarlett2_usb_get_config(
5173 mixer, SCARLETT2_CONFIG_COMPRESSOR_PARAMS,
5174 SCARLETT2_COMPRESSOR_PARAM_COUNT * info->dsp_input_count,
5175 private->compressor_values);
5176
5177 if (err < 0)
5178 return err;
5179
5180 for (i = 0; i < SCARLETT2_COMPRESSOR_PARAM_COUNT; i++) {
5181 const struct compressor_param *param = &compressor_params[i];
5182
5183 for (j = 0; j < info->dsp_input_count; j++) {
5184 int idx = i + j * SCARLETT2_COMPRESSOR_PARAM_COUNT;
5185 int val = private->compressor_values[idx];
5186
5187 val >>= param->scale_bits;
5188 val = clamp(val, param->min, param->max);
5189 private->compressor_values[idx] = val;
5190 }
5191 }
5192
5193 return 0;
5194 }
5195
scarlett2_compressor_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5196 static int scarlett2_compressor_ctl_get(
5197 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5198 {
5199 struct usb_mixer_elem_info *elem = kctl->private_data;
5200 struct scarlett2_data *private = elem->head.mixer->private_data;
5201
5202 ucontrol->value.integer.value[0] =
5203 private->compressor_values[elem->control];
5204 return 0;
5205 }
5206
scarlett2_compressor_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5207 static int scarlett2_compressor_ctl_put(
5208 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5209 {
5210 struct usb_mixer_elem_info *elem = kctl->private_data;
5211 struct usb_mixer_interface *mixer = elem->head.mixer;
5212 struct scarlett2_data *private = mixer->private_data;
5213 int index = elem->control;
5214 int channel = index / SCARLETT2_COMPRESSOR_PARAM_COUNT;
5215 int param_index = index % SCARLETT2_COMPRESSOR_PARAM_COUNT;
5216 const struct compressor_param *param = &compressor_params[param_index];
5217 int oval, val, err;
5218 s32 scaled_val;
5219
5220 guard(mutex)(&private->data_mutex);
5221
5222 if (private->hwdep_in_use)
5223 return -EBUSY;
5224
5225 err = scarlett2_check_put_during_autogain(mixer);
5226 if (err < 0)
5227 return err;
5228
5229 oval = private->compressor_values[index];
5230 val = ucontrol->value.integer.value[0];
5231 if (oval == val)
5232 return 0;
5233
5234 private->compressor_values[index] = val;
5235
5236 scaled_val = val << param->scale_bits;
5237
5238 /* Send change to the device */
5239
5240 /* The channel needs to be put in the parameter buffer index
5241 * field (param_buf_addr + 1); the value field isn't used in
5242 * this case.
5243 */
5244 err = scarlett2_usb_set_data(
5245 mixer, private->config_set->param_buf_addr + 1, 1, channel);
5246 if (err < 0)
5247 return err;
5248
5249 err = scarlett2_usb_set_config(
5250 mixer, SCARLETT2_CONFIG_COMPRESSOR_PARAMS, index, scaled_val);
5251
5252 return err < 0 ? err : 1;
5253 }
5254
scarlett2_compressor_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)5255 static int scarlett2_compressor_ctl_info(
5256 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
5257 {
5258 struct usb_mixer_elem_info *elem = kctl->private_data;
5259 int control = elem->control % SCARLETT2_COMPRESSOR_PARAM_COUNT;
5260
5261 uinfo->type = compressor_params[control].type;
5262 uinfo->count = 1;
5263 uinfo->value.integer.min = compressor_params[control].min;
5264 uinfo->value.integer.max = compressor_params[control].max;
5265 uinfo->value.integer.step = 1;
5266 return 0;
5267 }
5268
5269 static const struct snd_kcontrol_new scarlett2_compressor_ctl = {
5270 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
5271 .name = "",
5272 .info = scarlett2_compressor_ctl_info,
5273 .get = scarlett2_compressor_ctl_get,
5274 .put = scarlett2_compressor_ctl_put,
5275 };
5276
5277 /*** DSP Pre-Compressor and PEQ Filter Controls ***/
5278
scarlett2_precomp_flt_switch_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5279 static int scarlett2_precomp_flt_switch_ctl_get(
5280 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5281 {
5282 struct usb_mixer_elem_info *elem = kctl->private_data;
5283 struct scarlett2_data *private = elem->head.mixer->private_data;
5284
5285 ucontrol->value.integer.value[0] = private->precomp_flt_switch[elem->control];
5286
5287 return 0;
5288 }
5289
scarlett2_peq_flt_switch_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5290 static int scarlett2_peq_flt_switch_ctl_get(
5291 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5292 {
5293 struct usb_mixer_elem_info *elem = kctl->private_data;
5294 struct scarlett2_data *private = elem->head.mixer->private_data;
5295
5296 ucontrol->value.integer.value[0] =
5297 private->peq_flt_switch[elem->control];
5298
5299 return 0;
5300 }
5301
scarlett2_precomp_flt_switch_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5302 static int scarlett2_precomp_flt_switch_ctl_put(
5303 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5304 {
5305 struct usb_mixer_elem_info *elem = kctl->private_data;
5306 struct usb_mixer_interface *mixer = elem->head.mixer;
5307 struct scarlett2_data *private = mixer->private_data;
5308 int oval, val, err;
5309
5310 guard(mutex)(&private->data_mutex);
5311
5312 if (private->hwdep_in_use)
5313 return -EBUSY;
5314
5315 oval = private->precomp_flt_switch[elem->control];
5316 val = ucontrol->value.integer.value[0];
5317
5318 if (oval == val)
5319 return 0;
5320
5321 private->precomp_flt_switch[elem->control] = val;
5322
5323 /* Send change to the device */
5324 err = scarlett2_usb_set_config(
5325 mixer, SCARLETT2_CONFIG_PRECOMP_FLT_SWITCH,
5326 elem->control, val);
5327
5328 return err < 0 ? err : 1;
5329 }
5330
scarlett2_peq_flt_switch_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5331 static int scarlett2_peq_flt_switch_ctl_put(
5332 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5333 {
5334 struct usb_mixer_elem_info *elem = kctl->private_data;
5335 struct usb_mixer_interface *mixer = elem->head.mixer;
5336 struct scarlett2_data *private = mixer->private_data;
5337 int oval, val, err;
5338
5339 guard(mutex)(&private->data_mutex);
5340
5341 if (private->hwdep_in_use)
5342 return -EBUSY;
5343
5344 oval = private->peq_flt_switch[elem->control];
5345 val = ucontrol->value.integer.value[0];
5346
5347 if (oval == val)
5348 return 0;
5349
5350 private->peq_flt_switch[elem->control] = val;
5351
5352 /* Send change to the device */
5353 err = scarlett2_usb_set_config(
5354 mixer, SCARLETT2_CONFIG_PEQ_FLT_SWITCH,
5355 elem->control, val);
5356
5357 return err < 0 ? err : 1;
5358 }
5359
5360 static const struct snd_kcontrol_new scarlett2_precomp_flt_switch_ctl = {
5361 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
5362 .name = "",
5363 .info = snd_ctl_boolean_mono_info,
5364 .get = scarlett2_precomp_flt_switch_ctl_get,
5365 .put = scarlett2_precomp_flt_switch_ctl_put,
5366 };
5367
5368 static const struct snd_kcontrol_new scarlett2_peq_flt_switch_ctl = {
5369 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
5370 .name = "",
5371 .info = snd_ctl_boolean_mono_info,
5372 .get = scarlett2_peq_flt_switch_ctl_get,
5373 .put = scarlett2_peq_flt_switch_ctl_put,
5374 };
5375
scarlett2_update_filter_values(struct usb_mixer_interface * mixer)5376 static int scarlett2_update_filter_values(struct usb_mixer_interface *mixer)
5377 {
5378 struct scarlett2_data *private = mixer->private_data;
5379 const struct scarlett2_device_info *info = private->info;
5380 int err, i, j, k, src_idx, dst_idx;
5381 s32 peq_flt_values[SCARLETT2_DSP_SWITCH_MAX *
5382 SCARLETT2_PEQ_FLT_SLOTS_MAX *
5383 SCARLETT2_BIQUAD_COEFFS];
5384
5385 if (!info->dsp_input_count)
5386 return 0;
5387
5388 /* Get filter switch values */
5389 err = scarlett2_usb_get_config(
5390 mixer, SCARLETT2_CONFIG_PRECOMP_FLT_SWITCH,
5391 info->dsp_input_count, private->precomp_flt_switch);
5392 if (err < 0)
5393 return err;
5394
5395 err = scarlett2_usb_get_config(
5396 mixer, SCARLETT2_CONFIG_PEQ_FLT_SWITCH,
5397 info->dsp_input_count, private->peq_flt_switch);
5398 if (err < 0)
5399 return err;
5400
5401 /* Get pre-compressor filter values directly */
5402 err = scarlett2_usb_get_config(
5403 mixer, SCARLETT2_CONFIG_PRECOMP_FLT_PARAMS,
5404 info->dsp_input_count *
5405 info->precomp_flt_count *
5406 SCARLETT2_BIQUAD_COEFFS,
5407 private->precomp_flt_values);
5408
5409 if (err < 0)
5410 return err;
5411
5412 /* PEQ filter values need to be copied via buffer because of
5413 * padding after peq_flt_count up to peq_flt_total_count
5414 */
5415 err = scarlett2_usb_get_config(
5416 mixer, SCARLETT2_CONFIG_PEQ_FLT_PARAMS,
5417 info->dsp_input_count *
5418 info->peq_flt_total_count *
5419 SCARLETT2_BIQUAD_COEFFS,
5420 peq_flt_values);
5421 if (err < 0)
5422 return err;
5423
5424 for (i = 0, dst_idx = 0; i < info->dsp_input_count; i++) {
5425 src_idx = i *
5426 info->peq_flt_total_count *
5427 SCARLETT2_BIQUAD_COEFFS;
5428 for (j = 0; j < info->peq_flt_count; j++)
5429 for (k = 0;
5430 k < SCARLETT2_BIQUAD_COEFFS;
5431 k++, src_idx++, dst_idx++)
5432 private->peq_flt_values[dst_idx] =
5433 peq_flt_values[src_idx];
5434 }
5435
5436 return 0;
5437 }
5438
scarlett2_precomp_flt_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5439 static int scarlett2_precomp_flt_ctl_get(
5440 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5441 {
5442 struct usb_mixer_elem_info *elem = kctl->private_data;
5443 struct scarlett2_data *private = elem->head.mixer->private_data;
5444 int i, idx;
5445
5446 for (i = 0, idx = elem->control * SCARLETT2_BIQUAD_COEFFS;
5447 i < SCARLETT2_BIQUAD_COEFFS;
5448 i++, idx++)
5449 ucontrol->value.integer.value[i] =
5450 private->precomp_flt_values[idx];
5451
5452 return 0;
5453 }
5454
scarlett2_peq_flt_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5455 static int scarlett2_peq_flt_ctl_get(
5456 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5457 {
5458 struct usb_mixer_elem_info *elem = kctl->private_data;
5459 struct scarlett2_data *private = elem->head.mixer->private_data;
5460 int i, idx;
5461
5462 for (i = 0, idx = elem->control * SCARLETT2_BIQUAD_COEFFS;
5463 i < SCARLETT2_BIQUAD_COEFFS;
5464 i++, idx++)
5465 ucontrol->value.integer.value[i] =
5466 private->peq_flt_values[idx];
5467
5468 return 0;
5469 }
5470
scarlett2_precomp_flt_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5471 static int scarlett2_precomp_flt_ctl_put(
5472 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5473 {
5474 struct usb_mixer_elem_info *elem = kctl->private_data;
5475 struct usb_mixer_interface *mixer = elem->head.mixer;
5476 struct scarlett2_data *private = mixer->private_data;
5477 int index = elem->control * SCARLETT2_BIQUAD_COEFFS;
5478 int i, oval, val, err;
5479
5480 guard(mutex)(&private->data_mutex);
5481
5482 if (private->hwdep_in_use)
5483 return -EBUSY;
5484
5485 err = scarlett2_check_put_during_autogain(mixer);
5486 if (err < 0)
5487 return err;
5488
5489 /* Check if any of the values have changed; if not, return */
5490 for (i = 0; i < SCARLETT2_BIQUAD_COEFFS; i++) {
5491 oval = private->precomp_flt_values[index + i];
5492 val = ucontrol->value.integer.value[i];
5493 if (oval != val)
5494 break;
5495 }
5496
5497 if (i == SCARLETT2_BIQUAD_COEFFS)
5498 return 0;
5499
5500 /* Update the values */
5501 for (i = 0; i < SCARLETT2_BIQUAD_COEFFS; i++)
5502 private->precomp_flt_values[index + i] =
5503 ucontrol->value.integer.value[i];
5504
5505 /* Send change to the device */
5506 err = scarlett2_usb_set_data(
5507 mixer, private->config_set->param_buf_addr, 1, index);
5508 if (err < 0)
5509 return err;
5510
5511 err = scarlett2_usb_set_config_buf(
5512 mixer, SCARLETT2_CONFIG_PRECOMP_FLT_PARAMS,
5513 index, SCARLETT2_BIQUAD_COEFFS,
5514 &private->precomp_flt_values[index]);
5515
5516 return err < 0 ? err : 1;
5517 }
5518
scarlett2_peq_flt_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5519 static int scarlett2_peq_flt_ctl_put(
5520 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5521 {
5522 struct usb_mixer_elem_info *elem = kctl->private_data;
5523 struct usb_mixer_interface *mixer = elem->head.mixer;
5524 struct scarlett2_data *private = mixer->private_data;
5525 const struct scarlett2_device_info *info = private->info;
5526 int src_index = elem->control * SCARLETT2_BIQUAD_COEFFS;
5527 int dst_index = (
5528 elem->control /
5529 info->peq_flt_count *
5530 info->peq_flt_total_count +
5531 elem->control % info->peq_flt_count
5532 ) * SCARLETT2_BIQUAD_COEFFS;
5533 int i, oval, val, err;
5534
5535 guard(mutex)(&private->data_mutex);
5536
5537 if (private->hwdep_in_use)
5538 return -EBUSY;
5539
5540 err = scarlett2_check_put_during_autogain(mixer);
5541 if (err < 0)
5542 return err;
5543
5544 /* Check if any of the values have changed; if not, return */
5545 for (i = 0; i < SCARLETT2_BIQUAD_COEFFS; i++) {
5546 oval = private->peq_flt_values[src_index + i];
5547 val = ucontrol->value.integer.value[i];
5548 if (oval != val)
5549 break;
5550 }
5551
5552 if (i == SCARLETT2_BIQUAD_COEFFS)
5553 return 0;
5554
5555 /* Update the values */
5556 for (i = 0; i < SCARLETT2_BIQUAD_COEFFS; i++)
5557 private->peq_flt_values[src_index + i] =
5558 ucontrol->value.integer.value[i];
5559
5560 /* Send change to the device */
5561 err = scarlett2_usb_set_data(
5562 mixer, private->config_set->param_buf_addr, 1, dst_index);
5563 if (err < 0)
5564 return err;
5565
5566 err = scarlett2_usb_set_config_buf(
5567 mixer, SCARLETT2_CONFIG_PEQ_FLT_PARAMS,
5568 dst_index, SCARLETT2_BIQUAD_COEFFS,
5569 &private->peq_flt_values[src_index]);
5570
5571 return err < 0 ? err : 1;
5572 }
5573
scarlett2_flt_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)5574 static int scarlett2_flt_ctl_info(
5575 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
5576 {
5577 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
5578 uinfo->count = SCARLETT2_BIQUAD_COEFFS;
5579 uinfo->value.integer.min = INT_MIN;
5580 uinfo->value.integer.max = INT_MAX;
5581 uinfo->value.integer.step = 1;
5582 return 0;
5583 }
5584
5585 static const struct snd_kcontrol_new scarlett2_precomp_flt_ctl = {
5586 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
5587 .name = "",
5588 .info = scarlett2_flt_ctl_info,
5589 .get = scarlett2_precomp_flt_ctl_get,
5590 .put = scarlett2_precomp_flt_ctl_put,
5591 };
5592
5593 static const struct snd_kcontrol_new scarlett2_peq_flt_ctl = {
5594 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
5595 .name = "",
5596 .info = scarlett2_flt_ctl_info,
5597 .get = scarlett2_peq_flt_ctl_get,
5598 .put = scarlett2_peq_flt_ctl_put,
5599 };
5600
5601 /*** Input Mute Switch Controls ***/
5602
scarlett2_update_input_mute(struct usb_mixer_interface * mixer)5603 static int scarlett2_update_input_mute(struct usb_mixer_interface *mixer)
5604 {
5605 struct scarlett2_data *private = mixer->private_data;
5606 const struct scarlett2_device_info *info = private->info;
5607
5608 private->input_mute_updated = 0;
5609
5610 if (!info->mute_input_count)
5611 return 0;
5612
5613 return scarlett2_usb_get_config(
5614 mixer, SCARLETT2_CONFIG_INPUT_MUTE_SWITCH,
5615 info->mute_input_count, private->input_mute_switch);
5616 }
5617
scarlett2_input_mute_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5618 static int scarlett2_input_mute_ctl_get(struct snd_kcontrol *kctl,
5619 struct snd_ctl_elem_value *ucontrol)
5620 {
5621 struct usb_mixer_elem_info *elem = kctl->private_data;
5622 struct usb_mixer_interface *mixer = elem->head.mixer;
5623 struct scarlett2_data *private = mixer->private_data;
5624 int err;
5625
5626 guard(mutex)(&private->data_mutex);
5627
5628 if (private->hwdep_in_use)
5629 return -EBUSY;
5630
5631 if (private->input_mute_updated) {
5632 err = scarlett2_update_input_mute(mixer);
5633 if (err < 0)
5634 return err;
5635 }
5636 ucontrol->value.integer.value[0] =
5637 private->input_mute_switch[elem->control];
5638 return 0;
5639 }
5640
scarlett2_input_mute_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5641 static int scarlett2_input_mute_ctl_put(struct snd_kcontrol *kctl,
5642 struct snd_ctl_elem_value *ucontrol)
5643 {
5644 struct usb_mixer_elem_info *elem = kctl->private_data;
5645 struct usb_mixer_interface *mixer = elem->head.mixer;
5646 struct scarlett2_data *private = mixer->private_data;
5647 int index = elem->control;
5648 int oval, val, err;
5649
5650 guard(mutex)(&private->data_mutex);
5651
5652 if (private->hwdep_in_use)
5653 return -EBUSY;
5654
5655 err = scarlett2_check_put_during_autogain(mixer);
5656 if (err < 0)
5657 return err;
5658
5659 oval = private->input_mute_switch[index];
5660 val = ucontrol->value.integer.value[0];
5661
5662 if (oval == val)
5663 return 0;
5664
5665 private->input_mute_switch[index] = val;
5666
5667 /* Send switch change to the device */
5668 err = scarlett2_usb_set_config(
5669 mixer, SCARLETT2_CONFIG_INPUT_MUTE_SWITCH,
5670 index, val);
5671
5672 return err < 0 ? err : 1;
5673 }
5674
5675 static const struct snd_kcontrol_new scarlett2_input_mute_ctl = {
5676 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5677 .name = "",
5678 .info = scarlett2_autogain_disables_ctl_info,
5679 .get = scarlett2_input_mute_ctl_get,
5680 .put = scarlett2_input_mute_ctl_put,
5681 };
5682
5683 /*** Phantom Switch Controls ***/
5684
scarlett2_update_input_phantom(struct usb_mixer_interface * mixer)5685 static int scarlett2_update_input_phantom(struct usb_mixer_interface *mixer)
5686 {
5687 struct scarlett2_data *private = mixer->private_data;
5688 const struct scarlett2_device_info *info = private->info;
5689 int err;
5690
5691 private->input_phantom_updated = 0;
5692
5693 if (!info->phantom_count)
5694 return 0;
5695
5696 err = scarlett2_usb_get_config(
5697 mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
5698 info->phantom_count, private->phantom_switch);
5699 if (err < 0)
5700 return err;
5701
5702 if (scarlett2_has_config_item(private,
5703 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE)) {
5704 err = scarlett2_usb_get_config(
5705 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
5706 1, &private->phantom_persistence);
5707 if (err < 0)
5708 return err;
5709 }
5710
5711 return 0;
5712 }
5713
5714 /* Check if phantom power on the given input is currently changing state */
scarlett2_phantom_is_switching(struct scarlett2_data * private,int line_num)5715 static int scarlett2_phantom_is_switching(
5716 struct scarlett2_data *private, int line_num)
5717 {
5718 const struct scarlett2_device_info *info = private->info;
5719 int index = line_num / info->inputs_per_phantom;
5720
5721 return !!(private->phantom_switch[index] & 0x02);
5722 }
5723
5724 /* Update autogain controls' access mode when phantom power changes state */
scarlett2_phantom_update_access(struct usb_mixer_interface * mixer)5725 static void scarlett2_phantom_update_access(struct usb_mixer_interface *mixer)
5726 {
5727 struct scarlett2_data *private = mixer->private_data;
5728 const struct scarlett2_device_info *info = private->info;
5729 int i;
5730
5731 /* Disable autogain controls if phantom power is changing state */
5732 for (i = 0; i < info->gain_input_count; i++) {
5733 int val = !scarlett2_phantom_is_switching(private, i);
5734
5735 scarlett2_set_ctl_access(private->autogain_ctls[i], val);
5736 }
5737 }
5738
5739 /* Notify of access mode change for autogain which can't be enabled
5740 * while phantom power is changing.
5741 */
scarlett2_phantom_notify_access(struct usb_mixer_interface * mixer)5742 static void scarlett2_phantom_notify_access(struct usb_mixer_interface *mixer)
5743 {
5744 struct snd_card *card = mixer->chip->card;
5745 struct scarlett2_data *private = mixer->private_data;
5746 const struct scarlett2_device_info *info = private->info;
5747 int i;
5748
5749 for (i = 0; i < info->gain_input_count; i++)
5750 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
5751 &private->autogain_ctls[i]->id);
5752 }
5753
5754 /* Call scarlett2_update_input_phantom() and
5755 * scarlett2_phantom_update_access() if input_phantom_updated is set.
5756 */
scarlett2_check_input_phantom_updated(struct usb_mixer_interface * mixer)5757 static int scarlett2_check_input_phantom_updated(
5758 struct usb_mixer_interface *mixer)
5759 {
5760 struct scarlett2_data *private = mixer->private_data;
5761 int err;
5762
5763 if (!private->input_phantom_updated)
5764 return 0;
5765
5766 err = scarlett2_update_input_phantom(mixer);
5767 if (err < 0)
5768 return err;
5769
5770 scarlett2_phantom_update_access(mixer);
5771
5772 return 0;
5773 }
5774
scarlett2_phantom_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5775 static int scarlett2_phantom_ctl_get(struct snd_kcontrol *kctl,
5776 struct snd_ctl_elem_value *ucontrol)
5777 {
5778 struct usb_mixer_elem_info *elem = kctl->private_data;
5779 struct usb_mixer_interface *mixer = elem->head.mixer;
5780 struct scarlett2_data *private = mixer->private_data;
5781 int err;
5782
5783 guard(mutex)(&private->data_mutex);
5784
5785 if (private->hwdep_in_use)
5786 return -EBUSY;
5787
5788 err = scarlett2_check_input_phantom_updated(mixer);
5789 if (err < 0)
5790 return err;
5791
5792 ucontrol->value.integer.value[0] = scarlett2_decode_muteable(
5793 private->phantom_switch[elem->control]);
5794 return 0;
5795 }
5796
scarlett2_phantom_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5797 static int scarlett2_phantom_ctl_put(struct snd_kcontrol *kctl,
5798 struct snd_ctl_elem_value *ucontrol)
5799 {
5800 struct usb_mixer_elem_info *elem = kctl->private_data;
5801 struct usb_mixer_interface *mixer = elem->head.mixer;
5802 struct scarlett2_data *private = mixer->private_data;
5803 const struct scarlett2_device_info *info = private->info;
5804 int index = elem->control;
5805 int oval, val, err;
5806
5807 guard(mutex)(&private->data_mutex);
5808
5809 if (private->hwdep_in_use)
5810 return -EBUSY;
5811
5812 err = scarlett2_check_put_during_autogain(mixer);
5813 if (err < 0)
5814 return err;
5815
5816 oval = private->phantom_switch[index];
5817 val = !!ucontrol->value.integer.value[0];
5818
5819 if (oval == val)
5820 return 0;
5821
5822 private->phantom_switch[index] = val;
5823
5824 /* To set the Gen 4 muteable controls, bit 1 gets set */
5825 if (private->config_set->items[SCARLETT2_CONFIG_PHANTOM_SWITCH].mute)
5826 val = (!val) | 0x02;
5827
5828 /* Send switch change to the device */
5829 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
5830 index + info->phantom_first, val);
5831 scarlett2_phantom_update_access(mixer);
5832 scarlett2_phantom_notify_access(mixer);
5833
5834 return err < 0 ? err : 1;
5835 }
5836
5837 static const struct snd_kcontrol_new scarlett2_phantom_ctl = {
5838 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5839 .name = "",
5840 .info = scarlett2_autogain_disables_ctl_info,
5841 .get = scarlett2_phantom_ctl_get,
5842 .put = scarlett2_phantom_ctl_put,
5843 };
5844
5845 /*** Phantom Persistence Control ***/
5846
scarlett2_phantom_persistence_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5847 static int scarlett2_phantom_persistence_ctl_get(
5848 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5849 {
5850 struct usb_mixer_elem_info *elem = kctl->private_data;
5851 struct scarlett2_data *private = elem->head.mixer->private_data;
5852
5853 ucontrol->value.integer.value[0] = private->phantom_persistence;
5854 return 0;
5855 }
5856
scarlett2_phantom_persistence_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5857 static int scarlett2_phantom_persistence_ctl_put(
5858 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5859 {
5860 struct usb_mixer_elem_info *elem = kctl->private_data;
5861 struct usb_mixer_interface *mixer = elem->head.mixer;
5862 struct scarlett2_data *private = mixer->private_data;
5863 int index = elem->control;
5864 int oval, val, err;
5865
5866 guard(mutex)(&private->data_mutex);
5867
5868 if (private->hwdep_in_use)
5869 return -EBUSY;
5870
5871 oval = private->phantom_persistence;
5872 val = !!ucontrol->value.integer.value[0];
5873
5874 if (oval == val)
5875 return 0;
5876
5877 private->phantom_persistence = val;
5878
5879 /* Send switch change to the device */
5880 err = scarlett2_usb_set_config(
5881 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, index, val);
5882
5883 return err < 0 ? err : 1;
5884 }
5885
5886 static const struct snd_kcontrol_new scarlett2_phantom_persistence_ctl = {
5887 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5888 .name = "",
5889 .info = snd_ctl_boolean_mono_info,
5890 .get = scarlett2_phantom_persistence_ctl_get,
5891 .put = scarlett2_phantom_persistence_ctl_put,
5892 };
5893
5894 /*** Speaker Switching Control ***/
5895
scarlett2_update_monitor_other(struct usb_mixer_interface * mixer)5896 static int scarlett2_update_monitor_other(struct usb_mixer_interface *mixer)
5897 {
5898 struct scarlett2_data *private = mixer->private_data;
5899 const struct scarlett2_device_info *info = private->info;
5900 int err;
5901
5902 /* monitor_other_enable[0] enables speaker switching
5903 * monitor_other_enable[1] enables talkback
5904 */
5905 u8 monitor_other_enable[2];
5906
5907 /* monitor_other_switch[0] activates the alternate speakers
5908 * monitor_other_switch[1] activates talkback
5909 */
5910 u8 monitor_other_switch[2];
5911
5912 private->monitor_other_updated = 0;
5913
5914 /* if it doesn't do speaker switching then it also doesn't do
5915 * talkback
5916 */
5917 if (!info->has_speaker_switching)
5918 return 0;
5919
5920 err = scarlett2_usb_get_config(
5921 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
5922 2, monitor_other_enable);
5923 if (err < 0)
5924 return err;
5925
5926 err = scarlett2_usb_get_config(
5927 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
5928 2, monitor_other_switch);
5929 if (err < 0)
5930 return err;
5931
5932 if (!monitor_other_enable[0])
5933 private->speaker_switching_switch = 0;
5934 else
5935 private->speaker_switching_switch = monitor_other_switch[0] + 1;
5936
5937 if (info->has_talkback) {
5938 u16 bitmap;
5939 int i;
5940
5941 if (!monitor_other_enable[1])
5942 private->talkback_switch = 0;
5943 else
5944 private->talkback_switch = monitor_other_switch[1] + 1;
5945
5946 err = scarlett2_usb_get_config(mixer,
5947 SCARLETT2_CONFIG_TALKBACK_MAP,
5948 1, &bitmap);
5949 if (err < 0)
5950 return err;
5951 for (i = 0; i < private->num_mix_out; i++, bitmap >>= 1)
5952 private->talkback_map[i] = bitmap & 1;
5953 }
5954
5955 return 0;
5956 }
5957
scarlett2_speaker_switch_enum_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)5958 static int scarlett2_speaker_switch_enum_ctl_info(
5959 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
5960 {
5961 static const char *const values[3] = {
5962 "Off", "Main", "Alt"
5963 };
5964
5965 return snd_ctl_enum_info(uinfo, 1, 3, values);
5966 }
5967
scarlett2_speaker_switch_enum_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)5968 static int scarlett2_speaker_switch_enum_ctl_get(
5969 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5970 {
5971 struct usb_mixer_elem_info *elem = kctl->private_data;
5972 struct usb_mixer_interface *mixer = elem->head.mixer;
5973 struct scarlett2_data *private = mixer->private_data;
5974 int err;
5975
5976 guard(mutex)(&private->data_mutex);
5977
5978 if (private->hwdep_in_use)
5979 return -EBUSY;
5980
5981 if (private->monitor_other_updated) {
5982 err = scarlett2_update_monitor_other(mixer);
5983 if (err < 0)
5984 return err;
5985 }
5986 ucontrol->value.enumerated.item[0] = private->speaker_switching_switch;
5987 return 0;
5988 }
5989
5990 /* when speaker switching gets enabled, switch the main/alt speakers
5991 * to HW volume and disable those controls
5992 */
scarlett2_speaker_switch_enable(struct usb_mixer_interface * mixer)5993 static int scarlett2_speaker_switch_enable(struct usb_mixer_interface *mixer)
5994 {
5995 struct snd_card *card = mixer->chip->card;
5996 struct scarlett2_data *private = mixer->private_data;
5997 int i, err;
5998
5999 for (i = 0; i < 4; i++) {
6000 int index = line_out_remap(private, i);
6001
6002 /* switch the main/alt speakers to HW volume */
6003 if (!private->vol_sw_hw_switch[index]) {
6004 err = scarlett2_sw_hw_change(private->mixer, i, 1);
6005 if (err < 0)
6006 return err;
6007 }
6008
6009 /* disable the line out SW/HW switch */
6010 scarlett2_sw_hw_ctl_ro(private, i);
6011 snd_ctl_notify(card,
6012 SNDRV_CTL_EVENT_MASK_VALUE |
6013 SNDRV_CTL_EVENT_MASK_INFO,
6014 &private->sw_hw_ctls[i]->id);
6015 }
6016
6017 /* when the next monitor-other notify comes in, update the mux
6018 * configuration
6019 */
6020 private->speaker_switching_switched = 1;
6021
6022 return 0;
6023 }
6024
6025 /* when speaker switching gets disabled, reenable the hw/sw controls
6026 * and invalidate the routing
6027 */
scarlett2_speaker_switch_disable(struct usb_mixer_interface * mixer)6028 static void scarlett2_speaker_switch_disable(struct usb_mixer_interface *mixer)
6029 {
6030 struct snd_card *card = mixer->chip->card;
6031 struct scarlett2_data *private = mixer->private_data;
6032 int i;
6033
6034 /* enable the line out SW/HW switch */
6035 for (i = 0; i < 4; i++) {
6036 scarlett2_sw_hw_ctl_rw(private, i);
6037 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
6038 &private->sw_hw_ctls[i]->id);
6039 }
6040
6041 /* when the next monitor-other notify comes in, update the mux
6042 * configuration
6043 */
6044 private->speaker_switching_switched = 1;
6045 }
6046
scarlett2_speaker_switch_enum_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)6047 static int scarlett2_speaker_switch_enum_ctl_put(
6048 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
6049 {
6050 struct usb_mixer_elem_info *elem = kctl->private_data;
6051 struct usb_mixer_interface *mixer = elem->head.mixer;
6052 struct scarlett2_data *private = mixer->private_data;
6053 int oval, val, err;
6054
6055 guard(mutex)(&private->data_mutex);
6056
6057 if (private->hwdep_in_use)
6058 return -EBUSY;
6059
6060 oval = private->speaker_switching_switch;
6061 val = min(ucontrol->value.enumerated.item[0], 2U);
6062
6063 if (oval == val)
6064 return 0;
6065
6066 private->speaker_switching_switch = val;
6067
6068 /* enable/disable speaker switching */
6069 err = scarlett2_usb_set_config(
6070 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
6071 0, !!val);
6072 if (err < 0)
6073 return err;
6074
6075 /* if speaker switching is enabled, select main or alt */
6076 err = scarlett2_usb_set_config(
6077 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
6078 0, val == 2);
6079 if (err < 0)
6080 return err;
6081
6082 /* update controls if speaker switching gets enabled or disabled */
6083 if (!oval && val)
6084 err = scarlett2_speaker_switch_enable(mixer);
6085 else if (oval && !val)
6086 scarlett2_speaker_switch_disable(mixer);
6087
6088 return err < 0 ? err : 1;
6089 }
6090
6091 static const struct snd_kcontrol_new scarlett2_speaker_switch_enum_ctl = {
6092 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6093 .name = "",
6094 .info = scarlett2_speaker_switch_enum_ctl_info,
6095 .get = scarlett2_speaker_switch_enum_ctl_get,
6096 .put = scarlett2_speaker_switch_enum_ctl_put,
6097 };
6098
scarlett2_add_speaker_switch_ctl(struct usb_mixer_interface * mixer)6099 static int scarlett2_add_speaker_switch_ctl(struct usb_mixer_interface *mixer)
6100 {
6101 struct scarlett2_data *private = mixer->private_data;
6102 const struct scarlett2_device_info *info = private->info;
6103
6104 if (!info->has_speaker_switching)
6105 return 0;
6106
6107 return scarlett2_add_new_ctl(
6108 mixer, &scarlett2_speaker_switch_enum_ctl,
6109 0, 1, "Speaker Switching Playback Enum",
6110 &private->speaker_switching_ctl);
6111 }
6112
6113 /*** Talkback and Talkback Map Controls ***/
6114
scarlett2_talkback_enum_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)6115 static int scarlett2_talkback_enum_ctl_info(
6116 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
6117 {
6118 static const char *const values[3] = {
6119 "Disabled", "Off", "On"
6120 };
6121
6122 return snd_ctl_enum_info(uinfo, 1, 3, values);
6123 }
6124
scarlett2_talkback_enum_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)6125 static int scarlett2_talkback_enum_ctl_get(
6126 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
6127 {
6128 struct usb_mixer_elem_info *elem = kctl->private_data;
6129 struct usb_mixer_interface *mixer = elem->head.mixer;
6130 struct scarlett2_data *private = mixer->private_data;
6131 int err;
6132
6133 guard(mutex)(&private->data_mutex);
6134
6135 if (private->hwdep_in_use)
6136 return -EBUSY;
6137
6138 if (private->monitor_other_updated) {
6139 err = scarlett2_update_monitor_other(mixer);
6140 if (err < 0)
6141 return err;
6142 }
6143 ucontrol->value.enumerated.item[0] = private->talkback_switch;
6144 return 0;
6145 }
6146
scarlett2_talkback_enum_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)6147 static int scarlett2_talkback_enum_ctl_put(
6148 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
6149 {
6150 struct usb_mixer_elem_info *elem = kctl->private_data;
6151 struct usb_mixer_interface *mixer = elem->head.mixer;
6152 struct scarlett2_data *private = mixer->private_data;
6153 int oval, val, err;
6154
6155 guard(mutex)(&private->data_mutex);
6156
6157 if (private->hwdep_in_use)
6158 return -EBUSY;
6159
6160 oval = private->talkback_switch;
6161 val = min(ucontrol->value.enumerated.item[0], 2U);
6162
6163 if (oval == val)
6164 return 0;
6165
6166 private->talkback_switch = val;
6167
6168 /* enable/disable talkback */
6169 err = scarlett2_usb_set_config(
6170 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
6171 1, !!val);
6172 if (err < 0)
6173 return err;
6174
6175 /* if talkback is enabled, select main or alt */
6176 err = scarlett2_usb_set_config(
6177 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
6178 1, val == 2);
6179
6180 return err < 0 ? err : 1;
6181 }
6182
6183 static const struct snd_kcontrol_new scarlett2_talkback_enum_ctl = {
6184 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6185 .name = "",
6186 .info = scarlett2_talkback_enum_ctl_info,
6187 .get = scarlett2_talkback_enum_ctl_get,
6188 .put = scarlett2_talkback_enum_ctl_put,
6189 };
6190
scarlett2_talkback_map_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)6191 static int scarlett2_talkback_map_ctl_get(
6192 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
6193 {
6194 struct usb_mixer_elem_info *elem = kctl->private_data;
6195 struct usb_mixer_interface *mixer = elem->head.mixer;
6196 struct scarlett2_data *private = mixer->private_data;
6197 int index = elem->control;
6198
6199 ucontrol->value.integer.value[0] = private->talkback_map[index];
6200
6201 return 0;
6202 }
6203
scarlett2_talkback_map_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)6204 static int scarlett2_talkback_map_ctl_put(
6205 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
6206 {
6207 struct usb_mixer_elem_info *elem = kctl->private_data;
6208 struct usb_mixer_interface *mixer = elem->head.mixer;
6209 struct scarlett2_data *private = mixer->private_data;
6210 int index = elem->control;
6211 int oval, val, err, i;
6212 u16 bitmap = 0;
6213
6214 guard(mutex)(&private->data_mutex);
6215
6216 if (private->hwdep_in_use)
6217 return -EBUSY;
6218
6219 oval = private->talkback_map[index];
6220 val = !!ucontrol->value.integer.value[0];
6221
6222 if (oval == val)
6223 return 0;
6224
6225 private->talkback_map[index] = val;
6226
6227 for (i = 0; i < private->num_mix_out; i++)
6228 bitmap |= private->talkback_map[i] << i;
6229
6230 /* Send updated bitmap to the device */
6231 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_TALKBACK_MAP,
6232 0, bitmap);
6233
6234 return err < 0 ? err : 1;
6235 }
6236
6237 static const struct snd_kcontrol_new scarlett2_talkback_map_ctl = {
6238 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6239 .name = "",
6240 .info = snd_ctl_boolean_mono_info,
6241 .get = scarlett2_talkback_map_ctl_get,
6242 .put = scarlett2_talkback_map_ctl_put,
6243 };
6244
scarlett2_add_talkback_ctls(struct usb_mixer_interface * mixer)6245 static int scarlett2_add_talkback_ctls(struct usb_mixer_interface *mixer)
6246 {
6247 struct scarlett2_data *private = mixer->private_data;
6248 const struct scarlett2_device_info *info = private->info;
6249 int err, i;
6250 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
6251
6252 if (!info->has_talkback)
6253 return 0;
6254
6255 err = scarlett2_add_new_ctl(
6256 mixer, &scarlett2_talkback_enum_ctl,
6257 0, 1, "Talkback Playback Enum",
6258 &private->talkback_ctl);
6259 if (err < 0)
6260 return err;
6261
6262 for (i = 0; i < private->num_mix_out; i++) {
6263 snprintf(s, sizeof(s),
6264 "Talkback Mix %c Playback Switch", i + 'A');
6265 err = scarlett2_add_new_ctl(mixer, &scarlett2_talkback_map_ctl,
6266 i, 1, s, NULL);
6267 if (err < 0)
6268 return err;
6269 }
6270
6271 return 0;
6272 }
6273
6274 /*** Dim/Mute Controls ***/
6275
scarlett2_dim_mute_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)6276 static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
6277 struct snd_ctl_elem_value *ucontrol)
6278 {
6279 struct usb_mixer_elem_info *elem = kctl->private_data;
6280 struct usb_mixer_interface *mixer = elem->head.mixer;
6281 struct scarlett2_data *private = mixer->private_data;
6282 int err;
6283
6284 guard(mutex)(&private->data_mutex);
6285
6286 if (private->hwdep_in_use)
6287 return -EBUSY;
6288
6289 if (private->dim_mute_updated) {
6290 err = scarlett2_update_dim_mute(mixer);
6291 if (err < 0)
6292 return err;
6293 }
6294 ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
6295 return 0;
6296 }
6297
scarlett2_dim_mute_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)6298 static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
6299 struct snd_ctl_elem_value *ucontrol)
6300 {
6301 struct usb_mixer_elem_info *elem = kctl->private_data;
6302 struct usb_mixer_interface *mixer = elem->head.mixer;
6303 struct scarlett2_data *private = mixer->private_data;
6304 int index = elem->control;
6305 int oval, val, err, i;
6306
6307 guard(mutex)(&private->data_mutex);
6308
6309 if (private->hwdep_in_use)
6310 return -EBUSY;
6311
6312 oval = private->dim_mute[index];
6313 val = !!ucontrol->value.integer.value[0];
6314
6315 if (oval == val)
6316 return 0;
6317
6318 private->dim_mute[index] = val;
6319
6320 /* Send switch change to the device */
6321 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
6322 index, val);
6323 if (index == SCARLETT2_BUTTON_MUTE)
6324 for (i = 0; i < private->num_line_out; i++) {
6325 int line_index = line_out_remap(private, i);
6326
6327 if (private->vol_sw_hw_switch[line_index]) {
6328 private->mute_switch[line_index] = val;
6329 snd_ctl_notify(mixer->chip->card,
6330 SNDRV_CTL_EVENT_MASK_VALUE,
6331 &private->mute_ctls[i]->id);
6332 }
6333 }
6334
6335 return err < 0 ? err : 1;
6336 }
6337
6338 static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
6339 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6340 .name = "",
6341 .info = snd_ctl_boolean_mono_info,
6342 .get = scarlett2_dim_mute_ctl_get,
6343 .put = scarlett2_dim_mute_ctl_put
6344 };
6345
6346 /*** Vocaster Speaker/Headphone Mute Controls ***/
6347
scarlett2_sp_hp_mute_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)6348 static int scarlett2_sp_hp_mute_ctl_get(struct snd_kcontrol *kctl,
6349 struct snd_ctl_elem_value *ucontrol)
6350 {
6351 struct usb_mixer_elem_info *elem = kctl->private_data;
6352 struct scarlett2_data *private = elem->head.mixer->private_data;
6353
6354 ucontrol->value.integer.value[0] =
6355 !!(private->sp_hp_mute & (1 << elem->control));
6356
6357 return 0;
6358 }
6359
scarlett2_sp_hp_mute_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)6360 static int scarlett2_sp_hp_mute_ctl_put(struct snd_kcontrol *kctl,
6361 struct snd_ctl_elem_value *ucontrol)
6362 {
6363 struct usb_mixer_elem_info *elem = kctl->private_data;
6364 struct usb_mixer_interface *mixer = elem->head.mixer;
6365 struct scarlett2_data *private = mixer->private_data;
6366 int index = elem->control;
6367 int val, err;
6368
6369 guard(mutex)(&private->data_mutex);
6370
6371 if (private->hwdep_in_use)
6372 return -EBUSY;
6373
6374 val = private->sp_hp_mute;
6375
6376 if (ucontrol->value.integer.value[0])
6377 val |= (1 << index);
6378 else
6379 val &= ~(1 << index);
6380
6381 if (val == private->sp_hp_mute)
6382 return 0;
6383
6384 private->sp_hp_mute = val;
6385
6386 /* Send change to the device */
6387 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SP_HP_MUTE,
6388 0, val);
6389
6390 return err < 0 ? err : 1;
6391 }
6392
6393 static const struct snd_kcontrol_new scarlett2_sp_hp_mute_ctl = {
6394 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6395 .name = "",
6396 .info = snd_ctl_boolean_mono_info,
6397 .get = scarlett2_sp_hp_mute_ctl_get,
6398 .put = scarlett2_sp_hp_mute_ctl_put
6399 };
6400
6401 /*** Create the analogue output controls ***/
6402
scarlett2_add_line_out_ctls(struct usb_mixer_interface * mixer)6403 static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
6404 {
6405 struct scarlett2_data *private = mixer->private_data;
6406 const struct scarlett2_device_info *info = private->info;
6407 int err, i;
6408 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
6409
6410 /* Add R/O HW volume control */
6411 if (scarlett2_has_config_item(private,
6412 SCARLETT2_CONFIG_MASTER_VOLUME)) {
6413 snprintf(s, sizeof(s), "Master HW Playback Volume");
6414 err = scarlett2_add_new_ctl(mixer,
6415 &scarlett2_master_volume_ctl,
6416 0, 1, s, &private->master_vol_ctl);
6417 if (err < 0)
6418 return err;
6419 }
6420
6421 /* Add R/O headphone volume control */
6422 if (scarlett2_has_config_item(private,
6423 SCARLETT2_CONFIG_HEADPHONE_VOLUME)) {
6424 snprintf(s, sizeof(s), "Headphone Playback Volume");
6425 err = scarlett2_add_new_ctl(mixer,
6426 &scarlett2_headphone_volume_ctl,
6427 0, 1, s,
6428 &private->headphone_vol_ctl);
6429 if (err < 0)
6430 return err;
6431 }
6432
6433 /* Add Vocaster speaker/headphone mute controls */
6434 if (private->info->sp_hp_mute_names)
6435 for (i = 0; private->info->sp_hp_mute_names[i]; i++) {
6436 err = scarlett2_add_new_ctl(
6437 mixer, &scarlett2_sp_hp_mute_ctl,
6438 i, 1, private->info->sp_hp_mute_names[i],
6439 NULL);
6440 if (err < 0)
6441 return err;
6442 }
6443
6444 /* Remaining controls are only applicable if the device
6445 * has per-channel line-out volume controls.
6446 */
6447 if (!scarlett2_has_config_item(private,
6448 SCARLETT2_CONFIG_LINE_OUT_VOLUME))
6449 return 0;
6450
6451 /* Add volume controls */
6452 for (i = 0; i < private->num_line_out; i++) {
6453 int index = line_out_remap(private, i);
6454
6455 /* Fader */
6456 if (info->line_out_descrs[i])
6457 snprintf(s, sizeof(s),
6458 "Line %02d (%s) Playback Volume",
6459 i + 1, info->line_out_descrs[i]);
6460 else
6461 snprintf(s, sizeof(s),
6462 "Line %02d Playback Volume",
6463 i + 1);
6464 err = scarlett2_add_new_ctl(mixer,
6465 &scarlett2_line_out_volume_ctl,
6466 i, 1, s, &private->vol_ctls[i]);
6467 if (err < 0)
6468 return err;
6469
6470 /* Mute Switch */
6471 snprintf(s, sizeof(s),
6472 "Line %02d Mute Playback Switch",
6473 i + 1);
6474 err = scarlett2_add_new_ctl(mixer,
6475 &scarlett2_mute_ctl,
6476 i, 1, s,
6477 &private->mute_ctls[i]);
6478 if (err < 0)
6479 return err;
6480
6481 /* SW/HW Switch */
6482 if (scarlett2_has_config_item(private,
6483 SCARLETT2_CONFIG_SW_HW_SWITCH)) {
6484
6485 /* Make the fader and mute controls read-only if the
6486 * SW/HW switch is set to HW
6487 */
6488 if (private->vol_sw_hw_switch[index])
6489 scarlett2_vol_ctl_set_writable(mixer, i, 0);
6490
6491 scnprintf(s, sizeof(s),
6492 "Line Out %02d Volume Control Playback Enum",
6493 i + 1);
6494 err = scarlett2_add_new_ctl(mixer,
6495 &scarlett2_sw_hw_enum_ctl,
6496 i, 1, s,
6497 &private->sw_hw_ctls[i]);
6498 if (err < 0)
6499 return err;
6500
6501 /* Make the switch read-only if the line is
6502 * involved in speaker switching
6503 */
6504 if (private->speaker_switching_switch && i < 4)
6505 scarlett2_sw_hw_ctl_ro(private, i);
6506 }
6507 }
6508
6509 /* Add dim/mute controls */
6510 if (scarlett2_has_config_item(private, SCARLETT2_CONFIG_DIM_MUTE))
6511 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
6512 err = scarlett2_add_new_ctl(
6513 mixer, &scarlett2_dim_mute_ctl,
6514 i, 1, scarlett2_dim_mute_names[i],
6515 &private->dim_mute_ctls[i]);
6516 if (err < 0)
6517 return err;
6518 }
6519
6520 return 0;
6521 }
6522
6523 /*** Create the analogue input controls ***/
6524
scarlett2_add_dsp_ctls(struct usb_mixer_interface * mixer,int i)6525 static int scarlett2_add_dsp_ctls(struct usb_mixer_interface *mixer, int i)
6526 {
6527 struct scarlett2_data *private = mixer->private_data;
6528 const struct scarlett2_device_info *info = private->info;
6529 int j, err;
6530 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
6531 const char *compr_fmt = "Line In %d Compressor %s";
6532 const char *flt_switch_fmt = "Line In %d %s Filter Enable";
6533 const char *flt_fmt = "Line In %d %s Coefficients %d";
6534
6535 /* Add compressor controls */
6536 for (j = 0; j < SCARLETT2_COMPRESSOR_PARAM_COUNT; j++) {
6537 const struct compressor_param *param = &compressor_params[j];
6538 int idx = i * SCARLETT2_COMPRESSOR_PARAM_COUNT + j;
6539
6540 scnprintf(s, sizeof(s), compr_fmt, i + 1, param->name);
6541 err = scarlett2_add_new_ctl(
6542 mixer, &scarlett2_compressor_ctl,
6543 i * SCARLETT2_COMPRESSOR_PARAM_COUNT + j,
6544 1, s, &private->compressor_ctls[idx]);
6545 if (err < 0)
6546 return err;
6547 }
6548
6549 /* Add filter enable controls */
6550 scnprintf(s, sizeof(s), flt_switch_fmt, i + 1, "Pre-Comp");
6551 err = scarlett2_add_new_ctl(
6552 mixer, &scarlett2_precomp_flt_switch_ctl,
6553 i, 1, s, &private->precomp_flt_switch_ctls[i]);
6554 if (err < 0)
6555 return err;
6556
6557 scnprintf(s, sizeof(s), flt_switch_fmt, i + 1, "PEQ");
6558 err = scarlett2_add_new_ctl(
6559 mixer, &scarlett2_peq_flt_switch_ctl,
6560 i, 1, s, &private->peq_flt_switch_ctls[i]);
6561 if (err < 0)
6562 return err;
6563
6564 /* Add filter coefficient controls */
6565 for (j = 0; j < info->precomp_flt_count; j++) {
6566 scnprintf(s, sizeof(s), flt_fmt, i + 1, "Pre-Comp", j + 1);
6567 err = scarlett2_add_new_ctl(
6568 mixer, &scarlett2_precomp_flt_ctl,
6569 i * info->precomp_flt_count + j,
6570 1, s, NULL);
6571 if (err < 0)
6572 return err;
6573 }
6574
6575 for (j = 0; j < info->peq_flt_count; j++) {
6576 scnprintf(s, sizeof(s), flt_fmt, i + 1, "PEQ", j + 1);
6577 err = scarlett2_add_new_ctl(
6578 mixer, &scarlett2_peq_flt_ctl,
6579 i * info->peq_flt_count + j,
6580 1, s, NULL);
6581 if (err < 0)
6582 return err;
6583 }
6584
6585 return 0;
6586 }
6587
scarlett2_add_line_in_ctls(struct usb_mixer_interface * mixer)6588 static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
6589 {
6590 struct scarlett2_data *private = mixer->private_data;
6591 const struct scarlett2_device_info *info = private->info;
6592 int err, i;
6593 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
6594 const char *fmt = "Line In %d %s Capture %s";
6595 const char *fmt2 = "Line In %d-%d %s Capture %s";
6596
6597 /* Add input level (line/inst) controls */
6598 for (i = 0; i < info->level_input_count; i++) {
6599 scnprintf(s, sizeof(s), fmt, i + 1 + info->level_input_first,
6600 "Level", "Enum");
6601 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
6602 i, 1, s, &private->level_ctls[i]);
6603 if (err < 0)
6604 return err;
6605 }
6606
6607 /* Add input pad controls */
6608 for (i = 0; i < info->pad_input_count; i++) {
6609 scnprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
6610 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
6611 i, 1, s, &private->pad_ctls[i]);
6612 if (err < 0)
6613 return err;
6614 }
6615
6616 /* Add input air controls */
6617 for (i = 0; i < info->air_input_count; i++) {
6618 scnprintf(s, sizeof(s), fmt, i + 1 + info->air_input_first,
6619 "Air", info->air_option ? "Enum" : "Switch");
6620 err = scarlett2_add_new_ctl(
6621 mixer, &scarlett2_air_ctl[info->air_option],
6622 i, 1, s, &private->air_ctls[i]);
6623 if (err < 0)
6624 return err;
6625 }
6626
6627 /* Add input DSP controls */
6628 for (i = 0; i < info->dsp_input_count; i++) {
6629 scnprintf(s, sizeof(s), fmt, i + 1, "DSP", "Switch");
6630 err = scarlett2_add_new_ctl(mixer, &scarlett2_dsp_ctl,
6631 i, 1, s, &private->dsp_ctls[i]);
6632 if (err < 0)
6633 return err;
6634
6635 err = scarlett2_add_dsp_ctls(mixer, i);
6636 if (err < 0)
6637 return err;
6638 }
6639
6640 /* Add input mute controls */
6641 for (i = 0; i < info->mute_input_count; i++) {
6642 scnprintf(s, sizeof(s), fmt, i + 1, "Mute", "Switch");
6643 err = scarlett2_add_new_ctl(
6644 mixer, &scarlett2_input_mute_ctl,
6645 i, 1, s, &private->input_mute_ctls[i]);
6646 if (err < 0)
6647 return err;
6648 }
6649
6650 /* Add input phantom controls */
6651 if (info->inputs_per_phantom == 1) {
6652 for (i = 0; i < info->phantom_count; i++) {
6653 scnprintf(s, sizeof(s), fmt,
6654 i + 1 + info->phantom_first,
6655 "Phantom Power", "Switch");
6656 err = scarlett2_add_new_ctl(
6657 mixer, &scarlett2_phantom_ctl,
6658 i, 1, s, &private->phantom_ctls[i]);
6659 if (err < 0)
6660 return err;
6661 }
6662 } else if (info->inputs_per_phantom > 1) {
6663 for (i = 0; i < info->phantom_count; i++) {
6664 int from = i * info->inputs_per_phantom + 1;
6665 int to = (i + 1) * info->inputs_per_phantom;
6666
6667 scnprintf(s, sizeof(s), fmt2, from, to,
6668 "Phantom Power", "Switch");
6669 err = scarlett2_add_new_ctl(
6670 mixer, &scarlett2_phantom_ctl,
6671 i, 1, s, &private->phantom_ctls[i]);
6672 if (err < 0)
6673 return err;
6674 }
6675 }
6676 if (info->phantom_count &&
6677 scarlett2_has_config_item(private,
6678 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE)) {
6679 err = scarlett2_add_new_ctl(
6680 mixer, &scarlett2_phantom_persistence_ctl, 0, 1,
6681 "Phantom Power Persistence Capture Switch", NULL);
6682 if (err < 0)
6683 return err;
6684 }
6685
6686 /* Add input select/link controls */
6687 if (scarlett2_has_config_item(private,
6688 SCARLETT2_CONFIG_INPUT_SELECT_SWITCH)) {
6689 err = scarlett2_add_new_ctl(
6690 mixer, &scarlett2_input_select_ctl, 0, 1,
6691 "Input Select Capture Enum",
6692 &private->input_select_ctl);
6693 if (err < 0)
6694 return err;
6695 }
6696
6697 if (scarlett2_has_config_item(private,
6698 SCARLETT2_CONFIG_INPUT_LINK_SWITCH)) {
6699 for (i = 0; i < info->gain_input_count; i++) {
6700 scnprintf(s, sizeof(s),
6701 "Line In %d Link Capture Switch", i + 1);
6702 err = scarlett2_add_new_ctl(
6703 mixer, &scarlett2_input_link_ctl,
6704 i, 1, s, &private->input_link_ctls[i]);
6705 if (err < 0)
6706 return err;
6707 }
6708 }
6709
6710 /* Add software-controllable input gain controls */
6711 for (i = 0; i < info->gain_input_count; i++) {
6712 scnprintf(s, sizeof(s), fmt, i + 1,
6713 "Gain", "Volume");
6714 err = scarlett2_add_new_ctl(
6715 mixer, &scarlett2_input_gain_ctl,
6716 i, 1, s, &private->input_gain_ctls[i]);
6717 if (err < 0)
6718 return err;
6719 private->input_gain_ctls[i]->tlv.p =
6720 private->config_set->input_gain_tlv;
6721
6722 scnprintf(s, sizeof(s), fmt, i + 1,
6723 "Autogain", "Switch");
6724 err = scarlett2_add_new_ctl(
6725 mixer, &scarlett2_autogain_switch_ctl,
6726 i, 1, s, &private->autogain_ctls[i]);
6727 if (err < 0)
6728 return err;
6729
6730 scnprintf(s, sizeof(s), fmt, i + 1,
6731 "Autogain Status", "Enum");
6732 err = scarlett2_add_new_ctl(
6733 mixer, &scarlett2_autogain_status_ctl,
6734 i, 1, s, &private->autogain_status_ctls[i]);
6735 if (err < 0)
6736 return err;
6737 }
6738
6739 /* Add autogain target controls */
6740 for (i = 0; i < SCARLETT2_AG_TARGET_COUNT; i++)
6741 if (scarlett2_has_config_item(private,
6742 scarlett2_ag_target_configs[i])) {
6743
6744 scnprintf(s, sizeof(s), "Autogain %s Target",
6745 scarlett2_ag_target_names[i]);
6746 err = scarlett2_add_new_ctl(
6747 mixer, &scarlett2_ag_target_ctl,
6748 i, 1, s, &private->ag_target_ctls[i]);
6749 if (err < 0)
6750 return err;
6751 }
6752
6753 /* Add safe-mode input switch controls */
6754 for (i = 0; i < info->safe_input_count; i++) {
6755 scnprintf(s, sizeof(s), fmt, i + 1,
6756 "Safe", "Switch");
6757 err = scarlett2_add_new_ctl(
6758 mixer, &scarlett2_safe_ctl,
6759 i, 1, s, &private->safe_ctls[i]);
6760 if (err < 0)
6761 return err;
6762 }
6763
6764 /* Add PCM Input Switch control */
6765 if (scarlett2_has_config_item(private,
6766 SCARLETT2_CONFIG_PCM_INPUT_SWITCH)) {
6767 err = scarlett2_add_new_ctl(
6768 mixer, &scarlett2_pcm_input_switch_ctl, 0, 1,
6769 "PCM Input Capture Switch",
6770 &private->pcm_input_switch_ctl);
6771 if (err < 0)
6772 return err;
6773 }
6774
6775 return 0;
6776 }
6777
6778 /*** Mixer Volume Controls ***/
6779
scarlett2_update_mix(struct usb_mixer_interface * mixer)6780 static int scarlett2_update_mix(struct usb_mixer_interface *mixer)
6781 {
6782 struct scarlett2_data *private = mixer->private_data;
6783 int i, err;
6784
6785 private->mix_updated = 0;
6786
6787 for (i = 0; i < private->num_mix_out; i++) {
6788 err = scarlett2_usb_get_mix(mixer, i);
6789 if (err < 0)
6790 return err;
6791 }
6792
6793 return 1;
6794 }
6795
scarlett2_mixer_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)6796 static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
6797 struct snd_ctl_elem_info *uinfo)
6798 {
6799 struct usb_mixer_elem_info *elem = kctl->private_data;
6800
6801 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6802 uinfo->count = elem->channels;
6803 uinfo->value.integer.min = 0;
6804 uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
6805 uinfo->value.integer.step = 1;
6806 return 0;
6807 }
6808
scarlett2_mixer_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)6809 static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
6810 struct snd_ctl_elem_value *ucontrol)
6811 {
6812 struct usb_mixer_elem_info *elem = kctl->private_data;
6813 struct usb_mixer_interface *mixer = elem->head.mixer;
6814 struct scarlett2_data *private = mixer->private_data;
6815 int err;
6816
6817 guard(mutex)(&private->data_mutex);
6818
6819 if (private->hwdep_in_use)
6820 return -EBUSY;
6821
6822 if (private->mix_updated) {
6823 err = scarlett2_update_mix(mixer);
6824 if (err < 0)
6825 return err;
6826 }
6827 ucontrol->value.integer.value[0] = private->mix[elem->control];
6828 return 0;
6829 }
6830
scarlett2_mixer_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)6831 static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
6832 struct snd_ctl_elem_value *ucontrol)
6833 {
6834 struct usb_mixer_elem_info *elem = kctl->private_data;
6835 struct usb_mixer_interface *mixer = elem->head.mixer;
6836 struct scarlett2_data *private = mixer->private_data;
6837 int oval, val, mix_num, err;
6838 int index = elem->control;
6839
6840 guard(mutex)(&private->data_mutex);
6841
6842 if (private->hwdep_in_use)
6843 return -EBUSY;
6844
6845 oval = private->mix[index];
6846 val = clamp(ucontrol->value.integer.value[0],
6847 0L, (long)SCARLETT2_MIXER_MAX_VALUE);
6848 mix_num = index / private->num_mix_in;
6849
6850 if (oval == val)
6851 return 0;
6852
6853 private->mix[index] = val;
6854 err = scarlett2_usb_set_mix(mixer, mix_num);
6855
6856 return err < 0 ? err : 1;
6857 }
6858
6859 static const DECLARE_TLV_DB_MINMAX(
6860 db_scale_scarlett2_mixer,
6861 SCARLETT2_MIXER_MIN_DB * 100,
6862 SCARLETT2_MIXER_MAX_DB * 100
6863 );
6864
6865 static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
6866 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6867 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
6868 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
6869 .name = "",
6870 .info = scarlett2_mixer_ctl_info,
6871 .get = scarlett2_mixer_ctl_get,
6872 .put = scarlett2_mixer_ctl_put,
6873 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
6874 .tlv = { .p = db_scale_scarlett2_mixer }
6875 };
6876
scarlett2_add_mixer_ctls(struct usb_mixer_interface * mixer)6877 static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
6878 {
6879 struct scarlett2_data *private = mixer->private_data;
6880 int err, i, j;
6881 int index;
6882 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
6883
6884 for (i = 0, index = 0; i < private->num_mix_out; i++)
6885 for (j = 0; j < private->num_mix_in; j++, index++) {
6886 snprintf(s, sizeof(s),
6887 "Mix %c Input %02d Playback Volume",
6888 'A' + i, j + 1);
6889 err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
6890 index, 1, s,
6891 &private->mix_ctls[index]);
6892 if (err < 0)
6893 return err;
6894 }
6895
6896 return 0;
6897 }
6898
6899 /*** Direct Monitor Control ***/
6900
scarlett2_update_direct_monitor(struct usb_mixer_interface * mixer)6901 static int scarlett2_update_direct_monitor(struct usb_mixer_interface *mixer)
6902 {
6903 struct scarlett2_data *private = mixer->private_data;
6904
6905 private->direct_monitor_updated = 0;
6906
6907 if (!private->info->direct_monitor)
6908 return 0;
6909
6910 return scarlett2_usb_get_config(
6911 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR,
6912 1, &private->direct_monitor_switch);
6913 }
6914
scarlett2_update_monitor_mix(struct usb_mixer_interface * mixer)6915 static int scarlett2_update_monitor_mix(struct usb_mixer_interface *mixer)
6916 {
6917 struct scarlett2_data *private = mixer->private_data;
6918 int err, i;
6919 u16 mix_values[SCARLETT2_MONITOR_MIX_MAX];
6920
6921 if (!private->num_monitor_mix_ctls)
6922 return 0;
6923
6924 err = scarlett2_usb_get_config(
6925 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN,
6926 private->num_monitor_mix_ctls, mix_values);
6927 if (err < 0)
6928 return err;
6929
6930 for (i = 0; i < private->num_monitor_mix_ctls; i++)
6931 private->monitor_mix[i] = scarlett2_mixer_value_to_db(
6932 mix_values[i]);
6933
6934 return 0;
6935 }
6936
scarlett2_direct_monitor_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)6937 static int scarlett2_direct_monitor_ctl_get(
6938 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
6939 {
6940 struct usb_mixer_elem_info *elem = kctl->private_data;
6941 struct usb_mixer_interface *mixer = elem->head.mixer;
6942 struct scarlett2_data *private = mixer->private_data;
6943 int err;
6944
6945 guard(mutex)(&private->data_mutex);
6946
6947 if (private->hwdep_in_use)
6948 return -EBUSY;
6949
6950 if (private->direct_monitor_updated) {
6951 err = scarlett2_update_direct_monitor(mixer);
6952 if (err < 0)
6953 return err;
6954 }
6955 ucontrol->value.enumerated.item[0] = private->direct_monitor_switch;
6956 return 0;
6957 }
6958
scarlett2_direct_monitor_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)6959 static int scarlett2_direct_monitor_ctl_put(
6960 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
6961 {
6962 struct usb_mixer_elem_info *elem = kctl->private_data;
6963 struct usb_mixer_interface *mixer = elem->head.mixer;
6964 struct scarlett2_data *private = mixer->private_data;
6965 int index = elem->control;
6966 int oval, val, err;
6967
6968 guard(mutex)(&private->data_mutex);
6969
6970 if (private->hwdep_in_use)
6971 return -EBUSY;
6972
6973 oval = private->direct_monitor_switch;
6974 val = min(ucontrol->value.enumerated.item[0], 2U);
6975
6976 if (oval == val)
6977 return 0;
6978
6979 private->direct_monitor_switch = val;
6980
6981 /* Send switch change to the device */
6982 err = scarlett2_usb_set_config(
6983 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, index, val);
6984
6985 return err < 0 ? err : 1;
6986 }
6987
scarlett2_direct_monitor_stereo_enum_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)6988 static int scarlett2_direct_monitor_stereo_enum_ctl_info(
6989 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
6990 {
6991 static const char *const values[3] = {
6992 "Off", "Mono", "Stereo"
6993 };
6994
6995 return snd_ctl_enum_info(uinfo, 1, 3, values);
6996 }
6997
6998 /* Direct Monitor for Solo is mono-only and only needs a boolean control
6999 * Direct Monitor for 2i2 is selectable between Off/Mono/Stereo
7000 */
7001 static const struct snd_kcontrol_new scarlett2_direct_monitor_ctl[2] = {
7002 {
7003 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7004 .name = "",
7005 .info = snd_ctl_boolean_mono_info,
7006 .get = scarlett2_direct_monitor_ctl_get,
7007 .put = scarlett2_direct_monitor_ctl_put,
7008 },
7009 {
7010 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7011 .name = "",
7012 .info = scarlett2_direct_monitor_stereo_enum_ctl_info,
7013 .get = scarlett2_direct_monitor_ctl_get,
7014 .put = scarlett2_direct_monitor_ctl_put,
7015 }
7016 };
7017
scarlett2_monitor_mix_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)7018 static int scarlett2_monitor_mix_ctl_get(struct snd_kcontrol *kctl,
7019 struct snd_ctl_elem_value *ucontrol)
7020 {
7021 struct usb_mixer_elem_info *elem = kctl->private_data;
7022 struct scarlett2_data *private = elem->head.mixer->private_data;
7023
7024 ucontrol->value.integer.value[0] = private->monitor_mix[elem->control];
7025
7026 return 0;
7027 }
7028
scarlett2_monitor_mix_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)7029 static int scarlett2_monitor_mix_ctl_put(struct snd_kcontrol *kctl,
7030 struct snd_ctl_elem_value *ucontrol)
7031 {
7032 struct usb_mixer_elem_info *elem = kctl->private_data;
7033 struct usb_mixer_interface *mixer = elem->head.mixer;
7034 struct scarlett2_data *private = mixer->private_data;
7035 int oval, val, err;
7036 int index = elem->control;
7037
7038 guard(mutex)(&private->data_mutex);
7039
7040 if (private->hwdep_in_use)
7041 return -EBUSY;
7042
7043 oval = private->monitor_mix[index];
7044 val = clamp(ucontrol->value.integer.value[0],
7045 0L, (long)SCARLETT2_MIXER_MAX_VALUE);
7046
7047 if (oval == val)
7048 return 0;
7049
7050 private->monitor_mix[index] = val;
7051 err = scarlett2_usb_set_config(
7052 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN,
7053 index, scarlett2_mixer_values[val]);
7054
7055 return err < 0 ? err : 1;
7056 }
7057
7058 static const struct snd_kcontrol_new scarlett2_monitor_mix_ctl = {
7059 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7060 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
7061 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
7062 .name = "",
7063 .info = scarlett2_mixer_ctl_info,
7064 .get = scarlett2_monitor_mix_ctl_get,
7065 .put = scarlett2_monitor_mix_ctl_put,
7066 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
7067 .tlv = { .p = db_scale_scarlett2_mixer }
7068 };
7069
scarlett2_add_direct_monitor_ctls(struct usb_mixer_interface * mixer)7070 static int scarlett2_add_direct_monitor_ctls(struct usb_mixer_interface *mixer)
7071 {
7072 struct scarlett2_data *private = mixer->private_data;
7073 const struct scarlett2_device_info *info = private->info;
7074 const char *s;
7075 int err, i, j, k, index;
7076
7077 if (!info->direct_monitor)
7078 return 0;
7079
7080 s = info->direct_monitor == 1
7081 ? "Direct Monitor Playback Switch"
7082 : "Direct Monitor Playback Enum";
7083
7084 err = scarlett2_add_new_ctl(
7085 mixer, &scarlett2_direct_monitor_ctl[info->direct_monitor - 1],
7086 0, 1, s, &private->direct_monitor_ctl);
7087 if (err < 0)
7088 return err;
7089
7090 if (!private->num_monitor_mix_ctls)
7091 return 0;
7092
7093 /* 1 or 2 direct monitor selections (Mono & Stereo) */
7094 for (i = 0, index = 0; i < info->direct_monitor; i++) {
7095 const char * const format =
7096 "Monitor %sMix %c Input %02d Playback Volume";
7097 const char *mix_type;
7098
7099 if (info->direct_monitor == 1)
7100 mix_type = "";
7101 else if (i == 0)
7102 mix_type = "1 ";
7103 else
7104 mix_type = "2 ";
7105
7106 /* 2 Mix outputs, A/Left & B/Right */
7107 for (j = 0; j < 2; j++)
7108
7109 /* Mix inputs */
7110 for (k = 0; k < private->num_mix_in; k++, index++) {
7111 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
7112
7113 scnprintf(name, sizeof(name), format,
7114 mix_type, 'A' + j, k + 1);
7115
7116 err = scarlett2_add_new_ctl(
7117 mixer, &scarlett2_monitor_mix_ctl,
7118 index, 1, name, NULL);
7119 if (err < 0)
7120 return err;
7121 }
7122 }
7123
7124 return 0;
7125 }
7126
7127 /*** Mux Source Selection Controls ***/
7128
scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)7129 static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
7130 struct snd_ctl_elem_info *uinfo)
7131 {
7132 struct usb_mixer_elem_info *elem = kctl->private_data;
7133 struct scarlett2_data *private = elem->head.mixer->private_data;
7134 const struct scarlett2_device_info *info = private->info;
7135 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
7136 unsigned int item = uinfo->value.enumerated.item;
7137 int items = private->num_mux_srcs;
7138 int port_type;
7139
7140 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
7141 uinfo->count = elem->channels;
7142 uinfo->value.enumerated.items = items;
7143
7144 if (item >= items)
7145 item = uinfo->value.enumerated.item = items - 1;
7146
7147 for (port_type = 0;
7148 port_type < SCARLETT2_PORT_TYPE_COUNT;
7149 port_type++) {
7150 if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
7151 const struct scarlett2_port *port =
7152 &scarlett2_ports[port_type];
7153
7154 if (port_type == SCARLETT2_PORT_TYPE_MIX &&
7155 item >= private->num_mix_out)
7156 scnprintf(uinfo->value.enumerated.name,
7157 sizeof(uinfo->value.enumerated.name),
7158 port->dsp_src_descr,
7159 item - private->num_mix_out + 1);
7160 else
7161 scnprintf(uinfo->value.enumerated.name,
7162 sizeof(uinfo->value.enumerated.name),
7163 port->src_descr,
7164 item + port->src_num_offset);
7165
7166 return 0;
7167 }
7168 item -= port_count[port_type][SCARLETT2_PORT_IN];
7169 }
7170
7171 return -EINVAL;
7172 }
7173
scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)7174 static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
7175 struct snd_ctl_elem_value *ucontrol)
7176 {
7177 struct usb_mixer_elem_info *elem = kctl->private_data;
7178 struct usb_mixer_interface *mixer = elem->head.mixer;
7179 struct scarlett2_data *private = mixer->private_data;
7180 int index = line_out_remap(private, elem->control);
7181 int err;
7182
7183 guard(mutex)(&private->data_mutex);
7184
7185 if (private->hwdep_in_use)
7186 return -EBUSY;
7187
7188 if (private->mux_updated) {
7189 err = scarlett2_usb_get_mux(mixer);
7190 if (err < 0)
7191 return err;
7192 }
7193 ucontrol->value.enumerated.item[0] = private->mux[index];
7194 return 0;
7195 }
7196
scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)7197 static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
7198 struct snd_ctl_elem_value *ucontrol)
7199 {
7200 struct usb_mixer_elem_info *elem = kctl->private_data;
7201 struct usb_mixer_interface *mixer = elem->head.mixer;
7202 struct scarlett2_data *private = mixer->private_data;
7203 int index = line_out_remap(private, elem->control);
7204 int oval, val, err;
7205
7206 guard(mutex)(&private->data_mutex);
7207
7208 if (private->hwdep_in_use)
7209 return -EBUSY;
7210
7211 oval = private->mux[index];
7212 val = min(ucontrol->value.enumerated.item[0],
7213 private->num_mux_srcs - 1U);
7214
7215 if (oval == val)
7216 return 0;
7217
7218 private->mux[index] = val;
7219 err = scarlett2_usb_set_mux(mixer);
7220
7221 return err < 0 ? err : 1;
7222 }
7223
7224 static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
7225 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7226 .name = "",
7227 .info = scarlett2_mux_src_enum_ctl_info,
7228 .get = scarlett2_mux_src_enum_ctl_get,
7229 .put = scarlett2_mux_src_enum_ctl_put,
7230 };
7231
scarlett2_add_mux_enums(struct usb_mixer_interface * mixer)7232 static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
7233 {
7234 struct scarlett2_data *private = mixer->private_data;
7235 const struct scarlett2_device_info *info = private->info;
7236 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
7237 int port_type, channel, i;
7238
7239 for (i = 0, port_type = 0;
7240 port_type < SCARLETT2_PORT_TYPE_COUNT;
7241 port_type++) {
7242 for (channel = 0;
7243 channel < port_count[port_type][SCARLETT2_PORT_OUT];
7244 channel++, i++) {
7245 int err;
7246 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
7247 int channel_num = channel + 1;
7248 const struct scarlett2_port *port =
7249 &scarlett2_ports[port_type];
7250 const char *descr = port->dst_descr;
7251
7252 if (port_type == SCARLETT2_PORT_TYPE_MIX &&
7253 channel >= private->num_mix_in) {
7254 channel_num -= private->num_mix_in;
7255 descr = port->dsp_dst_descr;
7256 }
7257
7258 snprintf(s, sizeof(s) - 5, descr, channel_num);
7259 strcat(s, " Enum");
7260
7261 err = scarlett2_add_new_ctl(mixer,
7262 &scarlett2_mux_src_enum_ctl,
7263 i, 1, s,
7264 &private->mux_ctls[i]);
7265 if (err < 0)
7266 return err;
7267 }
7268 }
7269
7270 return 0;
7271 }
7272
7273 /*** Meter Controls ***/
7274
scarlett2_meter_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)7275 static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
7276 struct snd_ctl_elem_info *uinfo)
7277 {
7278 struct usb_mixer_elem_info *elem = kctl->private_data;
7279
7280 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7281 uinfo->count = elem->channels;
7282 uinfo->value.integer.min = 0;
7283 uinfo->value.integer.max = 4095;
7284 uinfo->value.integer.step = 1;
7285 return 0;
7286 }
7287
scarlett2_meter_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)7288 static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
7289 struct snd_ctl_elem_value *ucontrol)
7290 {
7291 struct usb_mixer_elem_info *elem = kctl->private_data;
7292 struct usb_mixer_interface *mixer = elem->head.mixer;
7293 struct scarlett2_data *private = mixer->private_data;
7294 u8 *meter_level_map = private->meter_level_map;
7295 u16 meter_levels[SCARLETT2_MAX_METERS];
7296 int i, err;
7297
7298 guard(mutex)(&private->data_mutex);
7299
7300 if (private->hwdep_in_use)
7301 return -EBUSY;
7302
7303 err = scarlett2_usb_get_meter_levels(mixer, elem->channels,
7304 meter_levels);
7305 if (err < 0)
7306 return err;
7307
7308 /* copy & translate from meter_levels[] using meter_level_map[] */
7309 for (i = 0; i < elem->channels; i++) {
7310 int idx = meter_level_map[i];
7311 int value;
7312
7313 if (idx == 255)
7314 value = 0;
7315 else
7316 value = meter_levels[idx];
7317
7318 ucontrol->value.integer.value[i] = value;
7319 }
7320
7321 return 0;
7322 }
7323
7324 static const struct snd_kcontrol_new scarlett2_meter_ctl = {
7325 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
7326 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
7327 .name = "",
7328 .info = scarlett2_meter_ctl_info,
7329 .get = scarlett2_meter_ctl_get
7330 };
7331
scarlett2_add_meter_ctl(struct usb_mixer_interface * mixer)7332 static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
7333 {
7334 struct scarlett2_data *private = mixer->private_data;
7335
7336 /* devices without a mixer also don't support reporting levels */
7337 if (!scarlett2_has_mixer(private))
7338 return 0;
7339
7340 return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
7341 0, private->num_mux_dsts,
7342 "Level Meter", NULL);
7343 }
7344
7345 /*** MSD Controls ***/
7346
scarlett2_msd_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)7347 static int scarlett2_msd_ctl_get(struct snd_kcontrol *kctl,
7348 struct snd_ctl_elem_value *ucontrol)
7349 {
7350 struct usb_mixer_elem_info *elem = kctl->private_data;
7351 struct scarlett2_data *private = elem->head.mixer->private_data;
7352
7353 ucontrol->value.integer.value[0] = private->msd_switch;
7354 return 0;
7355 }
7356
scarlett2_msd_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)7357 static int scarlett2_msd_ctl_put(struct snd_kcontrol *kctl,
7358 struct snd_ctl_elem_value *ucontrol)
7359 {
7360 struct usb_mixer_elem_info *elem = kctl->private_data;
7361 struct usb_mixer_interface *mixer = elem->head.mixer;
7362 struct scarlett2_data *private = mixer->private_data;
7363 int oval, val, err;
7364
7365 guard(mutex)(&private->data_mutex);
7366
7367 if (private->hwdep_in_use)
7368 return -EBUSY;
7369
7370 oval = private->msd_switch;
7371 val = !!ucontrol->value.integer.value[0];
7372
7373 if (oval == val)
7374 return 0;
7375
7376 private->msd_switch = val;
7377
7378 /* Send switch change to the device */
7379 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MSD_SWITCH,
7380 0, val);
7381
7382 return err < 0 ? err : 1;
7383 }
7384
7385 static const struct snd_kcontrol_new scarlett2_msd_ctl = {
7386 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7387 .name = "",
7388 .info = snd_ctl_boolean_mono_info,
7389 .get = scarlett2_msd_ctl_get,
7390 .put = scarlett2_msd_ctl_put,
7391 };
7392
scarlett2_add_msd_ctl(struct usb_mixer_interface * mixer)7393 static int scarlett2_add_msd_ctl(struct usb_mixer_interface *mixer)
7394 {
7395 struct scarlett2_data *private = mixer->private_data;
7396
7397 if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_MSD_SWITCH))
7398 return 0;
7399
7400 /* If MSD mode is off, hide the switch by default */
7401 if (!private->msd_switch && !(mixer->chip->setup & SCARLETT2_MSD_ENABLE))
7402 return 0;
7403
7404 /* Add MSD control */
7405 return scarlett2_add_new_ctl(mixer, &scarlett2_msd_ctl,
7406 0, 1, "MSD Mode Switch", NULL);
7407 }
7408
7409 /*** Standalone Control ***/
7410
scarlett2_standalone_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)7411 static int scarlett2_standalone_ctl_get(struct snd_kcontrol *kctl,
7412 struct snd_ctl_elem_value *ucontrol)
7413 {
7414 struct usb_mixer_elem_info *elem = kctl->private_data;
7415 struct scarlett2_data *private = elem->head.mixer->private_data;
7416
7417 ucontrol->value.integer.value[0] = private->standalone_switch;
7418 return 0;
7419 }
7420
scarlett2_standalone_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)7421 static int scarlett2_standalone_ctl_put(struct snd_kcontrol *kctl,
7422 struct snd_ctl_elem_value *ucontrol)
7423 {
7424 struct usb_mixer_elem_info *elem = kctl->private_data;
7425 struct usb_mixer_interface *mixer = elem->head.mixer;
7426 struct scarlett2_data *private = mixer->private_data;
7427 int oval, val, err;
7428
7429 guard(mutex)(&private->data_mutex);
7430
7431 if (private->hwdep_in_use)
7432 return -EBUSY;
7433
7434 oval = private->standalone_switch;
7435 val = !!ucontrol->value.integer.value[0];
7436
7437 if (oval == val)
7438 return 0;
7439
7440 private->standalone_switch = val;
7441
7442 /* Send switch change to the device */
7443 err = scarlett2_usb_set_config(mixer,
7444 SCARLETT2_CONFIG_STANDALONE_SWITCH,
7445 0, val);
7446
7447 return err < 0 ? err : 1;
7448 }
7449
7450 static const struct snd_kcontrol_new scarlett2_standalone_ctl = {
7451 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7452 .name = "",
7453 .info = snd_ctl_boolean_mono_info,
7454 .get = scarlett2_standalone_ctl_get,
7455 .put = scarlett2_standalone_ctl_put,
7456 };
7457
scarlett2_add_standalone_ctl(struct usb_mixer_interface * mixer)7458 static int scarlett2_add_standalone_ctl(struct usb_mixer_interface *mixer)
7459 {
7460 struct scarlett2_data *private = mixer->private_data;
7461
7462 if (!scarlett2_has_config_item(private,
7463 SCARLETT2_CONFIG_STANDALONE_SWITCH))
7464 return 0;
7465
7466 /* Add standalone control */
7467 return scarlett2_add_new_ctl(mixer, &scarlett2_standalone_ctl,
7468 0, 1, "Standalone Switch", NULL);
7469 }
7470
7471 /*** Power Status ***/
7472
scarlett2_update_power_status(struct usb_mixer_interface * mixer)7473 static int scarlett2_update_power_status(struct usb_mixer_interface *mixer)
7474 {
7475 struct scarlett2_data *private = mixer->private_data;
7476 int err;
7477 u8 power_ext, power_low;
7478
7479 private->power_status_updated = 0;
7480
7481 err = scarlett2_usb_get_config(mixer, SCARLETT2_CONFIG_POWER_EXT,
7482 1, &power_ext);
7483 if (err < 0)
7484 return err;
7485
7486 err = scarlett2_usb_get_config(mixer, SCARLETT2_CONFIG_POWER_LOW,
7487 1, &power_low);
7488 if (err < 0)
7489 return err;
7490
7491 if (power_low)
7492 private->power_status = SCARLETT2_POWER_STATUS_FAIL;
7493 else if (power_ext)
7494 private->power_status = SCARLETT2_POWER_STATUS_EXT;
7495 else
7496 private->power_status = SCARLETT2_POWER_STATUS_BUS;
7497
7498 return 0;
7499 }
7500
scarlett2_power_status_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)7501 static int scarlett2_power_status_ctl_get(struct snd_kcontrol *kctl,
7502 struct snd_ctl_elem_value *ucontrol)
7503 {
7504 struct usb_mixer_elem_info *elem = kctl->private_data;
7505 struct usb_mixer_interface *mixer = elem->head.mixer;
7506 struct scarlett2_data *private = mixer->private_data;
7507 int err;
7508
7509 guard(mutex)(&private->data_mutex);
7510
7511 if (private->power_status_updated) {
7512 err = scarlett2_update_power_status(mixer);
7513 if (err < 0)
7514 return err;
7515 }
7516 ucontrol->value.integer.value[0] = private->power_status;
7517 return 0;
7518 }
7519
scarlett2_power_status_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)7520 static int scarlett2_power_status_ctl_info(
7521 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
7522 {
7523 static const char *const values[3] = {
7524 "External", "Bus", "Fail"
7525 };
7526
7527 return snd_ctl_enum_info(uinfo, 1, 3, values);
7528 }
7529
7530 static const struct snd_kcontrol_new scarlett2_power_status_ctl = {
7531 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
7532 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7533 .name = "",
7534 .info = scarlett2_power_status_ctl_info,
7535 .get = scarlett2_power_status_ctl_get,
7536 };
7537
scarlett2_add_power_status_ctl(struct usb_mixer_interface * mixer)7538 static int scarlett2_add_power_status_ctl(struct usb_mixer_interface *mixer)
7539 {
7540 struct scarlett2_data *private = mixer->private_data;
7541
7542 if (!scarlett2_has_config_item(private,
7543 SCARLETT2_CONFIG_POWER_EXT))
7544 return 0;
7545
7546 /* Add power status control */
7547 return scarlett2_add_new_ctl(mixer, &scarlett2_power_status_ctl,
7548 0, 1, "Power Status Card Enum",
7549 &private->power_status_ctl);
7550 }
7551
7552 /*** Bluetooth Volume ***/
7553
scarlett2_update_bluetooth_volume(struct usb_mixer_interface * mixer)7554 static int scarlett2_update_bluetooth_volume(struct usb_mixer_interface *mixer)
7555 {
7556 struct scarlett2_data *private = mixer->private_data;
7557 int err;
7558
7559 private->bluetooth_updated = 0;
7560
7561 if (!private->info->has_bluetooth)
7562 return 0;
7563
7564 err = scarlett2_usb_get_config(mixer,
7565 SCARLETT2_CONFIG_BLUETOOTH_VOLUME,
7566 1, &private->bluetooth_volume);
7567 if (err < 0)
7568 return err;
7569
7570 return 0;
7571 }
7572
scarlett2_bluetooth_volume_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)7573 static int scarlett2_bluetooth_volume_ctl_get(struct snd_kcontrol *kctl,
7574 struct snd_ctl_elem_value *ucontrol)
7575 {
7576 struct usb_mixer_elem_info *elem = kctl->private_data;
7577 struct usb_mixer_interface *mixer = elem->head.mixer;
7578 struct scarlett2_data *private = mixer->private_data;
7579 int err;
7580
7581 guard(mutex)(&private->data_mutex);
7582
7583 if (private->hwdep_in_use)
7584 return -EBUSY;
7585
7586 if (private->bluetooth_updated) {
7587 err = scarlett2_update_bluetooth_volume(mixer);
7588 if (err < 0)
7589 return err;
7590 }
7591 ucontrol->value.integer.value[0] = private->bluetooth_volume;
7592 return 0;
7593 }
7594
scarlett2_bluetooth_volume_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)7595 static int scarlett2_bluetooth_volume_ctl_put(struct snd_kcontrol *kctl,
7596 struct snd_ctl_elem_value *ucontrol)
7597 {
7598 struct usb_mixer_elem_info *elem = kctl->private_data;
7599 struct usb_mixer_interface *mixer = elem->head.mixer;
7600 struct scarlett2_data *private = mixer->private_data;
7601 int oval, val, err;
7602
7603 guard(mutex)(&private->data_mutex);
7604
7605 if (private->hwdep_in_use)
7606 return -EBUSY;
7607
7608 oval = private->bluetooth_volume;
7609 val = clamp(ucontrol->value.integer.value[0],
7610 0L, (long)SCARLETT2_MAX_BLUETOOTH_VOLUME);
7611
7612 if (oval == val)
7613 return 0;
7614
7615 private->bluetooth_volume = val;
7616 err = scarlett2_usb_set_config(mixer,
7617 SCARLETT2_CONFIG_BLUETOOTH_VOLUME,
7618 0, val);
7619
7620 return err < 0 ? err : 1;
7621 }
7622
scarlett2_bluetooth_volume_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)7623 static int scarlett2_bluetooth_volume_ctl_info(
7624 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
7625 {
7626 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7627 uinfo->count = 1;
7628 uinfo->value.integer.min = 0;
7629 uinfo->value.integer.max = SCARLETT2_MAX_BLUETOOTH_VOLUME;
7630 uinfo->value.integer.step = 1;
7631 return 0;
7632 }
7633
7634 static const struct snd_kcontrol_new scarlett2_bluetooth_volume_ctl = {
7635 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7636 .name = "",
7637 .info = scarlett2_bluetooth_volume_ctl_info,
7638 .get = scarlett2_bluetooth_volume_ctl_get,
7639 .put = scarlett2_bluetooth_volume_ctl_put,
7640 };
7641
scarlett2_add_bluetooth_volume_ctl(struct usb_mixer_interface * mixer)7642 static int scarlett2_add_bluetooth_volume_ctl(
7643 struct usb_mixer_interface *mixer)
7644 {
7645 struct scarlett2_data *private = mixer->private_data;
7646
7647 if (!private->info->has_bluetooth)
7648 return 0;
7649
7650 /* Add Bluetooth volume control */
7651 return scarlett2_add_new_ctl(mixer, &scarlett2_bluetooth_volume_ctl,
7652 0, 1, "Bluetooth Capture Volume",
7653 &private->bluetooth_volume_ctl);
7654 }
7655
7656 /*** S/PDIF Mode Controls ***/
7657
scarlett2_update_spdif_mode(struct usb_mixer_interface * mixer)7658 static int scarlett2_update_spdif_mode(struct usb_mixer_interface *mixer)
7659 {
7660 struct scarlett2_data *private = mixer->private_data;
7661 int err, i;
7662 u8 mode;
7663 const u8 *mode_values = private->info->spdif_mode_values;
7664
7665 if (!private->info->spdif_mode_control_name)
7666 return 0;
7667
7668 err = scarlett2_usb_get_config(mixer, SCARLETT2_CONFIG_SPDIF_MODE,
7669 1, &mode);
7670 if (err < 0)
7671 return err;
7672
7673 private->spdif_mode = 0;
7674
7675 for (i = 0; *mode_values != 0xff; i++, mode_values++)
7676 if (*mode_values == mode) {
7677 private->spdif_mode = i;
7678 break;
7679 }
7680
7681 return 0;
7682 }
7683
scarlett2_spdif_mode_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)7684 static int scarlett2_spdif_mode_ctl_info(struct snd_kcontrol *kctl,
7685 struct snd_ctl_elem_info *uinfo)
7686 {
7687 struct usb_mixer_elem_info *elem = kctl->private_data;
7688 struct scarlett2_data *private = elem->head.mixer->private_data;
7689 const char * const *mode_texts = private->info->spdif_mode_texts;
7690 int count = 0;
7691
7692 while (*mode_texts++)
7693 count++;
7694
7695 return snd_ctl_enum_info(uinfo, 1, count,
7696 private->info->spdif_mode_texts);
7697 }
7698
scarlett2_spdif_mode_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)7699 static int scarlett2_spdif_mode_ctl_get(struct snd_kcontrol *kctl,
7700 struct snd_ctl_elem_value *ucontrol)
7701 {
7702 struct usb_mixer_elem_info *elem = kctl->private_data;
7703 struct scarlett2_data *private = elem->head.mixer->private_data;
7704
7705 ucontrol->value.enumerated.item[0] = private->spdif_mode;
7706 return 0;
7707 }
7708
scarlett2_spdif_mode_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)7709 static int scarlett2_spdif_mode_ctl_put(struct snd_kcontrol *kctl,
7710 struct snd_ctl_elem_value *ucontrol)
7711 {
7712 struct usb_mixer_elem_info *elem = kctl->private_data;
7713 struct usb_mixer_interface *mixer = elem->head.mixer;
7714 struct scarlett2_data *private = mixer->private_data;
7715 int oval, val, err;
7716 int i;
7717
7718 guard(mutex)(&private->data_mutex);
7719
7720 oval = private->spdif_mode;
7721 val = ucontrol->value.enumerated.item[0];
7722
7723 if (val < 0)
7724 return -EINVAL;
7725
7726 for (i = 0; i <= val; i++)
7727 if (private->info->spdif_mode_values[i] == 0xff)
7728 return -EINVAL;
7729
7730 if (oval == val)
7731 return 0;
7732
7733 private->spdif_mode = val;
7734
7735 err = scarlett2_usb_set_config(
7736 mixer, SCARLETT2_CONFIG_SPDIF_MODE, 0,
7737 private->info->spdif_mode_values[val]);
7738
7739 return err < 0 ? err : 1;
7740 }
7741
7742 static const struct snd_kcontrol_new scarlett2_spdif_mode_ctl = {
7743 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7744 .name = "",
7745 .info = scarlett2_spdif_mode_ctl_info,
7746 .get = scarlett2_spdif_mode_ctl_get,
7747 .put = scarlett2_spdif_mode_ctl_put,
7748 };
7749
scarlett2_add_spdif_mode_ctl(struct usb_mixer_interface * mixer)7750 static int scarlett2_add_spdif_mode_ctl(struct usb_mixer_interface *mixer)
7751 {
7752 struct scarlett2_data *private = mixer->private_data;
7753
7754 if (!private->info->spdif_mode_control_name)
7755 return 0;
7756
7757 return scarlett2_add_new_ctl(mixer, &scarlett2_spdif_mode_ctl,
7758 0, 1,
7759 private->info->spdif_mode_control_name,
7760 NULL);
7761 }
7762
7763 /*** Notification Handlers ***/
7764
7765 /* Notify on sync change */
scarlett2_notify_sync(struct usb_mixer_interface * mixer)7766 static void scarlett2_notify_sync(struct usb_mixer_interface *mixer)
7767 {
7768 struct scarlett2_data *private = mixer->private_data;
7769
7770 private->sync_updated = 1;
7771
7772 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
7773 &private->sync_ctl->id);
7774 }
7775
7776 /* Notify on monitor change (Gen 2/3) */
scarlett2_notify_monitor(struct usb_mixer_interface * mixer)7777 static void scarlett2_notify_monitor(struct usb_mixer_interface *mixer)
7778 {
7779 struct snd_card *card = mixer->chip->card;
7780 struct scarlett2_data *private = mixer->private_data;
7781 int i;
7782
7783 if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
7784 return;
7785
7786 private->vol_updated = 1;
7787
7788 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
7789 &private->master_vol_ctl->id);
7790
7791 for (i = 0; i < private->num_line_out; i++)
7792 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
7793 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7794 &private->vol_ctls[i]->id);
7795 }
7796
7797 /* Notify on volume change (Gen 4) */
scarlett2_notify_volume(struct usb_mixer_interface * mixer)7798 static void scarlett2_notify_volume(struct usb_mixer_interface *mixer)
7799 {
7800 struct scarlett2_data *private = mixer->private_data;
7801
7802 private->vol_updated = 1;
7803
7804 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
7805 &private->master_vol_ctl->id);
7806 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
7807 &private->headphone_vol_ctl->id);
7808 }
7809
7810 /* Notify on dim/mute change */
scarlett2_notify_dim_mute(struct usb_mixer_interface * mixer)7811 static void scarlett2_notify_dim_mute(struct usb_mixer_interface *mixer)
7812 {
7813 struct snd_card *card = mixer->chip->card;
7814 struct scarlett2_data *private = mixer->private_data;
7815 int i;
7816
7817 if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
7818 return;
7819
7820 private->dim_mute_updated = 1;
7821
7822 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
7823 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7824 &private->dim_mute_ctls[i]->id);
7825
7826 for (i = 0; i < private->num_line_out; i++)
7827 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
7828 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7829 &private->mute_ctls[i]->id);
7830 }
7831
7832 /* Notify on input level switch change */
scarlett2_notify_input_level(struct usb_mixer_interface * mixer)7833 static void scarlett2_notify_input_level(struct usb_mixer_interface *mixer)
7834 {
7835 struct snd_card *card = mixer->chip->card;
7836 struct scarlett2_data *private = mixer->private_data;
7837 const struct scarlett2_device_info *info = private->info;
7838 int i;
7839
7840 private->input_level_updated = 1;
7841
7842 for (i = 0; i < info->level_input_count; i++)
7843 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7844 &private->level_ctls[i]->id);
7845 }
7846
7847 /* Notify on input pad switch change */
scarlett2_notify_input_pad(struct usb_mixer_interface * mixer)7848 static void scarlett2_notify_input_pad(struct usb_mixer_interface *mixer)
7849 {
7850 struct snd_card *card = mixer->chip->card;
7851 struct scarlett2_data *private = mixer->private_data;
7852 const struct scarlett2_device_info *info = private->info;
7853 int i;
7854
7855 private->input_pad_updated = 1;
7856
7857 for (i = 0; i < info->pad_input_count; i++)
7858 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7859 &private->pad_ctls[i]->id);
7860 }
7861
7862 /* Notify on input air switch change */
scarlett2_notify_input_air(struct usb_mixer_interface * mixer)7863 static void scarlett2_notify_input_air(struct usb_mixer_interface *mixer)
7864 {
7865 struct snd_card *card = mixer->chip->card;
7866 struct scarlett2_data *private = mixer->private_data;
7867 const struct scarlett2_device_info *info = private->info;
7868 int i;
7869
7870 private->input_air_updated = 1;
7871
7872 for (i = 0; i < info->air_input_count; i++)
7873 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7874 &private->air_ctls[i]->id);
7875 }
7876
7877 /* Notify on input DSP switch change */
scarlett2_notify_input_dsp(struct usb_mixer_interface * mixer)7878 static void scarlett2_notify_input_dsp(struct usb_mixer_interface *mixer)
7879 {
7880 struct snd_card *card = mixer->chip->card;
7881 struct scarlett2_data *private = mixer->private_data;
7882 const struct scarlett2_device_info *info = private->info;
7883 int i;
7884
7885 private->input_dsp_updated = 1;
7886
7887 for (i = 0; i < info->dsp_input_count; i++)
7888 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7889 &private->dsp_ctls[i]->id);
7890 }
7891
7892 /* Notify on input mute switch change */
scarlett2_notify_input_mute(struct usb_mixer_interface * mixer)7893 static void scarlett2_notify_input_mute(struct usb_mixer_interface *mixer)
7894 {
7895 struct snd_card *card = mixer->chip->card;
7896 struct scarlett2_data *private = mixer->private_data;
7897 const struct scarlett2_device_info *info = private->info;
7898 int i;
7899
7900 private->input_mute_updated = 1;
7901
7902 for (i = 0; i < info->mute_input_count; i++)
7903 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7904 &private->input_mute_ctls[i]->id);
7905 }
7906
7907 /* Notify on input phantom switch change */
scarlett2_notify_input_phantom(struct usb_mixer_interface * mixer)7908 static void scarlett2_notify_input_phantom(struct usb_mixer_interface *mixer)
7909 {
7910 struct snd_card *card = mixer->chip->card;
7911 struct scarlett2_data *private = mixer->private_data;
7912 const struct scarlett2_device_info *info = private->info;
7913 int i;
7914
7915 private->input_phantom_updated = 1;
7916
7917 for (i = 0; i < info->phantom_count; i++)
7918 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7919 &private->phantom_ctls[i]->id);
7920
7921 scarlett2_phantom_notify_access(mixer);
7922 }
7923
7924 /* Notify on "input other" change (level/pad/air/phantom) */
scarlett2_notify_input_other(struct usb_mixer_interface * mixer)7925 static void scarlett2_notify_input_other(struct usb_mixer_interface *mixer)
7926 {
7927 scarlett2_notify_input_level(mixer);
7928 scarlett2_notify_input_pad(mixer);
7929 scarlett2_notify_input_air(mixer);
7930 scarlett2_notify_input_phantom(mixer);
7931 }
7932
7933 /* Notify on input select change */
scarlett2_notify_input_select(struct usb_mixer_interface * mixer)7934 static void scarlett2_notify_input_select(struct usb_mixer_interface *mixer)
7935 {
7936 struct snd_card *card = mixer->chip->card;
7937 struct scarlett2_data *private = mixer->private_data;
7938 const struct scarlett2_device_info *info = private->info;
7939 int i;
7940
7941 if (!scarlett2_has_config_item(private,
7942 SCARLETT2_CONFIG_INPUT_SELECT_SWITCH))
7943 return;
7944
7945 private->input_select_updated = 1;
7946
7947 snd_ctl_notify(card,
7948 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
7949 &private->input_select_ctl->id);
7950
7951 for (i = 0; i < info->gain_input_count; i++)
7952 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7953 &private->input_link_ctls[i]->id);
7954 }
7955
7956 /* Notify on input gain change */
scarlett2_notify_input_gain(struct usb_mixer_interface * mixer)7957 static void scarlett2_notify_input_gain(struct usb_mixer_interface *mixer)
7958 {
7959 struct snd_card *card = mixer->chip->card;
7960 struct scarlett2_data *private = mixer->private_data;
7961 const struct scarlett2_device_info *info = private->info;
7962 int i;
7963
7964 if (!info->gain_input_count)
7965 return;
7966
7967 private->input_gain_updated = 1;
7968
7969 for (i = 0; i < info->gain_input_count; i++)
7970 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7971 &private->input_gain_ctls[i]->id);
7972 }
7973
7974 /* Notify on autogain change */
scarlett2_notify_autogain(struct usb_mixer_interface * mixer)7975 static void scarlett2_notify_autogain(struct usb_mixer_interface *mixer)
7976 {
7977 struct snd_card *card = mixer->chip->card;
7978 struct scarlett2_data *private = mixer->private_data;
7979 const struct scarlett2_device_info *info = private->info;
7980 int i;
7981
7982 if (!info->gain_input_count)
7983 return;
7984
7985 private->autogain_updated = 1;
7986
7987 for (i = 0; i < info->gain_input_count; i++) {
7988 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7989 &private->autogain_ctls[i]->id);
7990 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7991 &private->autogain_status_ctls[i]->id);
7992 }
7993
7994 for (i = 0; i < SCARLETT2_AG_TARGET_COUNT; i++)
7995 if (scarlett2_has_config_item(private,
7996 scarlett2_ag_target_configs[i]))
7997 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
7998 &private->ag_target_ctls[i]->id);
7999
8000 scarlett2_autogain_notify_access(mixer);
8001 }
8002
8003 /* Notify on input safe switch change */
scarlett2_notify_input_safe(struct usb_mixer_interface * mixer)8004 static void scarlett2_notify_input_safe(struct usb_mixer_interface *mixer)
8005 {
8006 struct snd_card *card = mixer->chip->card;
8007 struct scarlett2_data *private = mixer->private_data;
8008 const struct scarlett2_device_info *info = private->info;
8009 int i;
8010
8011 if (!info->safe_input_count)
8012 return;
8013
8014 private->input_safe_updated = 1;
8015
8016 for (i = 0; i < info->safe_input_count; i++)
8017 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
8018 &private->safe_ctls[i]->id);
8019 }
8020
8021 /* Notify on "monitor other" change (speaker switching, talkback) */
scarlett2_notify_monitor_other(struct usb_mixer_interface * mixer)8022 static void scarlett2_notify_monitor_other(struct usb_mixer_interface *mixer)
8023 {
8024 struct snd_card *card = mixer->chip->card;
8025 struct scarlett2_data *private = mixer->private_data;
8026 const struct scarlett2_device_info *info = private->info;
8027
8028 private->monitor_other_updated = 1;
8029
8030 if (info->has_speaker_switching)
8031 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
8032 &private->speaker_switching_ctl->id);
8033
8034 if (info->has_talkback)
8035 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
8036 &private->talkback_ctl->id);
8037
8038 /* if speaker switching was recently enabled or disabled,
8039 * invalidate the dim/mute and mux enum controls
8040 */
8041 if (private->speaker_switching_switched) {
8042 int i;
8043
8044 scarlett2_notify_dim_mute(mixer);
8045
8046 private->speaker_switching_switched = 0;
8047 private->mux_updated = 1;
8048
8049 for (i = 0; i < private->num_mux_dsts; i++)
8050 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
8051 &private->mux_ctls[i]->id);
8052 }
8053 }
8054
8055 /* Notify on direct monitor switch change */
scarlett2_notify_direct_monitor(struct usb_mixer_interface * mixer)8056 static void scarlett2_notify_direct_monitor(struct usb_mixer_interface *mixer)
8057 {
8058 struct snd_card *card = mixer->chip->card;
8059 struct scarlett2_data *private = mixer->private_data;
8060 int count = private->num_mix_in * private->num_mix_out;
8061 int i;
8062
8063 private->direct_monitor_updated = 1;
8064
8065 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
8066 &private->direct_monitor_ctl->id);
8067
8068 if (!scarlett2_has_mixer(private))
8069 return;
8070
8071 private->mix_updated = 1;
8072
8073 /* Notify of change to the mix controls */
8074 for (i = 0; i < count; i++)
8075 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
8076 &private->mix_ctls[i]->id);
8077 }
8078
8079 /* Notify on power change */
scarlett2_notify_power_status(struct usb_mixer_interface * mixer)8080 static void scarlett2_notify_power_status(struct usb_mixer_interface *mixer)
8081 {
8082 struct snd_card *card = mixer->chip->card;
8083 struct scarlett2_data *private = mixer->private_data;
8084
8085 private->power_status_updated = 1;
8086
8087 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
8088 &private->power_status_ctl->id);
8089 }
8090
8091 /* Notify on mux change */
scarlett2_notify_mux(struct usb_mixer_interface * mixer)8092 static void scarlett2_notify_mux(struct usb_mixer_interface *mixer)
8093 {
8094 struct snd_card *card = mixer->chip->card;
8095 struct scarlett2_data *private = mixer->private_data;
8096 int i;
8097
8098 private->mux_updated = 1;
8099
8100 for (i = 0; i < private->num_mux_dsts; i++)
8101 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
8102 &private->mux_ctls[i]->id);
8103 }
8104
8105 /* Notify on PCM input switch change */
scarlett2_notify_pcm_input_switch(struct usb_mixer_interface * mixer)8106 static void scarlett2_notify_pcm_input_switch(struct usb_mixer_interface *mixer)
8107 {
8108 struct snd_card *card = mixer->chip->card;
8109 struct scarlett2_data *private = mixer->private_data;
8110
8111 private->pcm_input_switch_updated = 1;
8112
8113 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
8114 &private->pcm_input_switch_ctl->id);
8115
8116 scarlett2_notify_mux(mixer);
8117 }
8118
8119 /* Notify on Bluetooth change */
scarlett2_notify_bluetooth(struct usb_mixer_interface * mixer)8120 static void scarlett2_notify_bluetooth(struct usb_mixer_interface *mixer)
8121 {
8122 struct snd_card *card = mixer->chip->card;
8123 struct scarlett2_data *private = mixer->private_data;
8124
8125 if (!private->info->has_bluetooth)
8126 return;
8127
8128 private->bluetooth_updated = 1;
8129
8130 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
8131 &private->bluetooth_volume_ctl->id);
8132 }
8133
8134 /* Handle acknowledgement that a command was received; let
8135 * scarlett2_usb() know that it can proceed
8136 */
scarlett2_notify_ack(struct usb_mixer_interface * mixer)8137 static void scarlett2_notify_ack(struct usb_mixer_interface *mixer)
8138 {
8139 struct scarlett2_data *private = mixer->private_data;
8140
8141 /* if running == 0, ignore ACKs */
8142 if (private->running)
8143 complete(&private->cmd_done);
8144 }
8145
8146 /* Interrupt callback */
scarlett2_notify(struct urb * urb)8147 static void scarlett2_notify(struct urb *urb)
8148 {
8149 struct usb_mixer_interface *mixer = urb->context;
8150 int len = urb->actual_length;
8151 int ustatus = urb->status;
8152 u32 data;
8153 struct scarlett2_data *private = mixer->private_data;
8154 const struct scarlett2_notification *notifications =
8155 private->config_set->notifications;
8156
8157 if (ustatus != 0 || len != 8)
8158 goto requeue;
8159
8160 data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
8161
8162 /* Ignore notifications except ACK during initialisation.
8163 * ACK is 0x00000001 on every device.
8164 */
8165 if (private->running < 2)
8166 data &= 1;
8167
8168 while (data && notifications->mask) {
8169 if (data & notifications->mask) {
8170 data &= ~notifications->mask;
8171 if (notifications->func)
8172 notifications->func(mixer);
8173 }
8174 notifications++;
8175 }
8176
8177 if (data)
8178 usb_audio_warn(mixer->chip,
8179 "%s: Unhandled notification: 0x%08x\n",
8180 __func__, data);
8181
8182 requeue:
8183 if (ustatus != -ENOENT &&
8184 ustatus != -ECONNRESET &&
8185 ustatus != -ESHUTDOWN) {
8186 urb->dev = mixer->chip->dev;
8187 usb_submit_urb(urb, GFP_ATOMIC);
8188 } else {
8189 complete(&private->cmd_done);
8190 }
8191 }
8192
8193 /*** Cleanup/Suspend Callbacks ***/
8194
scarlett2_private_free(struct usb_mixer_interface * mixer)8195 static void scarlett2_private_free(struct usb_mixer_interface *mixer)
8196 {
8197 struct scarlett2_data *private = mixer->private_data;
8198
8199 cancel_delayed_work_sync(&private->work);
8200 kfree(private);
8201 mixer->private_data = NULL;
8202 }
8203
scarlett2_private_suspend(struct usb_mixer_interface * mixer)8204 static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
8205 {
8206 struct scarlett2_data *private = mixer->private_data;
8207
8208 if (cancel_delayed_work_sync(&private->work))
8209 scarlett2_config_save(private->mixer);
8210 }
8211
8212 /*** Initialisation ***/
8213
scarlett2_count_io(struct scarlett2_data * private)8214 static void scarlett2_count_io(struct scarlett2_data *private)
8215 {
8216 const struct scarlett2_device_info *info = private->info;
8217 const struct scarlett2_config_set *config_set = info->config_set;
8218 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
8219 int port_type, srcs = 0, dsts = 0, i;
8220
8221 /* Count the number of mux sources and destinations */
8222 for (port_type = 0;
8223 port_type < SCARLETT2_PORT_TYPE_COUNT;
8224 port_type++) {
8225 srcs += port_count[port_type][SCARLETT2_PORT_IN];
8226 dsts += port_count[port_type][SCARLETT2_PORT_OUT];
8227 }
8228
8229 private->num_mux_srcs = srcs;
8230 private->num_mux_dsts = dsts;
8231
8232 /* Mixer inputs are mux outputs and vice versa.
8233 * Scarlett Gen 4 DSP I/O uses SCARLETT2_PORT_TYPE_MIX but
8234 * doesn't have mixer controls.
8235 */
8236 private->num_mix_in =
8237 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT] -
8238 info->dsp_count;
8239
8240 private->num_mix_out =
8241 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN] -
8242 info->dsp_count;
8243
8244 /* Number of analogue line outputs */
8245 private->num_line_out =
8246 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
8247
8248 /* Number of monitor mix controls */
8249 private->num_monitor_mix_ctls =
8250 info->direct_monitor * 2 * private->num_mix_in;
8251
8252 /* Number of autogain status texts */
8253 if (config_set->autogain_status_texts) {
8254 const char * const *texts = config_set->autogain_status_texts;
8255
8256 for (i = 0; texts[i]; i++)
8257 ;
8258 private->num_autogain_status_texts = i;
8259 }
8260 }
8261
8262 /* Look through the interface descriptors for the Focusrite Control
8263 * interface (bInterfaceClass = 255 Vendor Specific Class) and set
8264 * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
8265 * in private
8266 */
scarlett2_find_fc_interface(struct usb_device * dev,struct scarlett2_data * private)8267 static int scarlett2_find_fc_interface(struct usb_device *dev,
8268 struct scarlett2_data *private)
8269 {
8270 struct usb_host_config *config = dev->actconfig;
8271 int i;
8272
8273 for (i = 0; i < config->desc.bNumInterfaces; i++) {
8274 struct usb_interface *intf = config->interface[i];
8275 struct usb_interface_descriptor *desc =
8276 &intf->altsetting[0].desc;
8277 struct usb_endpoint_descriptor *epd;
8278
8279 if (desc->bInterfaceClass != 255)
8280 continue;
8281 if (desc->bNumEndpoints < 1)
8282 continue;
8283
8284 epd = get_endpoint(intf->altsetting, 0);
8285 private->bInterfaceNumber = desc->bInterfaceNumber;
8286 private->bEndpointAddress = usb_endpoint_num(epd);
8287 private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
8288 private->bInterval = epd->bInterval;
8289 return 0;
8290 }
8291
8292 return -EINVAL;
8293 }
8294
8295 /* Initialise private data */
scarlett2_init_private(struct usb_mixer_interface * mixer,const struct scarlett2_device_entry * entry)8296 static int scarlett2_init_private(struct usb_mixer_interface *mixer,
8297 const struct scarlett2_device_entry *entry)
8298 {
8299 struct scarlett2_data *private =
8300 kzalloc_obj(struct scarlett2_data);
8301
8302 if (!private)
8303 return -ENOMEM;
8304
8305 mutex_init(&private->usb_mutex);
8306 mutex_init(&private->data_mutex);
8307 INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
8308
8309 mixer->private_data = private;
8310 mixer->private_free = scarlett2_private_free;
8311 mixer->private_suspend = scarlett2_private_suspend;
8312
8313 private->info = entry->info;
8314 private->config_set = entry->info->config_set;
8315 private->series_name = entry->series_name;
8316 scarlett2_count_io(private);
8317 private->scarlett2_seq = 0;
8318 private->mixer = mixer;
8319
8320 return scarlett2_find_fc_interface(mixer->chip->dev, private);
8321 }
8322
8323 /* Submit a URB to receive notifications from the device */
scarlett2_init_notify(struct usb_mixer_interface * mixer)8324 static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
8325 {
8326 struct usb_device *dev = mixer->chip->dev;
8327 struct scarlett2_data *private = mixer->private_data;
8328 unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
8329 void *transfer_buffer;
8330
8331 if (mixer->urb) {
8332 usb_audio_err(mixer->chip,
8333 "%s: mixer urb already in use!\n", __func__);
8334 return 0;
8335 }
8336
8337 if (usb_pipe_type_check(dev, pipe))
8338 return -EINVAL;
8339
8340 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
8341 if (!mixer->urb)
8342 return -ENOMEM;
8343
8344 transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
8345 if (!transfer_buffer)
8346 return -ENOMEM;
8347
8348 usb_fill_int_urb(mixer->urb, dev, pipe,
8349 transfer_buffer, private->wMaxPacketSize,
8350 scarlett2_notify, mixer, private->bInterval);
8351
8352 init_completion(&private->cmd_done);
8353
8354 return usb_submit_urb(mixer->urb, GFP_KERNEL);
8355 }
8356
8357 /* Cargo cult proprietary initialisation sequence */
scarlett2_usb_init(struct usb_mixer_interface * mixer)8358 static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
8359 {
8360 struct usb_device *dev = mixer->chip->dev;
8361 struct scarlett2_data *private = mixer->private_data;
8362 u8 step0_buf[24];
8363 u8 step2_buf[84];
8364 int err;
8365
8366 if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
8367 return -EINVAL;
8368
8369 /* step 0 */
8370 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
8371 SCARLETT2_USB_CMD_INIT,
8372 step0_buf, sizeof(step0_buf));
8373 if (err < 0)
8374 return err;
8375
8376 /* Set up the interrupt polling for notifications.
8377 * When running is:
8378 * 0: all notifications are ignored
8379 * 1: only ACKs are handled
8380 * 2: all notifications are handled
8381 */
8382 err = scarlett2_init_notify(mixer);
8383 if (err < 0)
8384 return err;
8385
8386 /* sleep for a moment in case of an outstanding ACK */
8387 msleep(20);
8388
8389 /* start handling ACKs, but no other notifications until the
8390 * ALSA controls have been created
8391 */
8392 private->running = 1;
8393
8394 /* step 1 */
8395 private->scarlett2_seq = 1;
8396 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
8397 if (err < 0)
8398 return err;
8399
8400 /* step 2 */
8401 private->scarlett2_seq = 1;
8402 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_2,
8403 NULL, 0,
8404 step2_buf, sizeof(step2_buf));
8405 if (err < 0)
8406 return err;
8407
8408 /* extract 4-byte firmware version from step2_buf[8] */
8409 private->firmware_version = le32_to_cpu(*(__le32 *)(step2_buf + 8));
8410 usb_audio_info(mixer->chip,
8411 "Firmware version %d\n",
8412 private->firmware_version);
8413
8414 return 0;
8415 }
8416
8417 /* Get the flash segment numbers for the App_Settings and App_Upgrade
8418 * segments and put them in the private data
8419 */
scarlett2_get_flash_segment_nums(struct usb_mixer_interface * mixer)8420 static int scarlett2_get_flash_segment_nums(struct usb_mixer_interface *mixer)
8421 {
8422 struct scarlett2_data *private = mixer->private_data;
8423 int err, count, i;
8424
8425 struct {
8426 __le32 size;
8427 __le32 count;
8428 u8 unknown[8];
8429 } __packed flash_info;
8430
8431 struct {
8432 __le32 size;
8433 __le32 flags;
8434 char name[16];
8435 } __packed segment_info;
8436
8437 err = scarlett2_usb(mixer, SCARLETT2_USB_INFO_FLASH,
8438 NULL, 0,
8439 &flash_info, sizeof(flash_info));
8440 if (err < 0)
8441 return err;
8442
8443 count = le32_to_cpu(flash_info.count);
8444
8445 /* sanity check count */
8446 if (count < SCARLETT2_SEGMENT_NUM_MIN ||
8447 count > SCARLETT2_SEGMENT_NUM_MAX + 1) {
8448 usb_audio_err(mixer->chip,
8449 "invalid flash segment count: %d\n", count);
8450 return -EINVAL;
8451 }
8452
8453 for (i = 0; i < count; i++) {
8454 __le32 segment_num_req = cpu_to_le32(i);
8455 int flash_segment_id;
8456
8457 err = scarlett2_usb(mixer, SCARLETT2_USB_INFO_SEGMENT,
8458 &segment_num_req, sizeof(segment_num_req),
8459 &segment_info, sizeof(segment_info));
8460 if (err < 0) {
8461 usb_audio_err(mixer->chip,
8462 "failed to get flash segment info %d: %d\n",
8463 i, err);
8464 return err;
8465 }
8466
8467 if (!strncmp(segment_info.name,
8468 SCARLETT2_SEGMENT_SETTINGS_NAME, 16))
8469 flash_segment_id = SCARLETT2_SEGMENT_ID_SETTINGS;
8470 else if (!strncmp(segment_info.name,
8471 SCARLETT2_SEGMENT_FIRMWARE_NAME, 16))
8472 flash_segment_id = SCARLETT2_SEGMENT_ID_FIRMWARE;
8473 else
8474 continue;
8475
8476 private->flash_segment_nums[flash_segment_id] = i;
8477 private->flash_segment_blocks[flash_segment_id] =
8478 le32_to_cpu(segment_info.size) /
8479 SCARLETT2_FLASH_BLOCK_SIZE;
8480 }
8481
8482 /* segment 0 is App_Gold and we never want to touch that, so
8483 * use 0 as the "not-found" value
8484 */
8485 if (!private->flash_segment_nums[SCARLETT2_SEGMENT_ID_SETTINGS]) {
8486 usb_audio_err(mixer->chip,
8487 "failed to find flash segment %s\n",
8488 SCARLETT2_SEGMENT_SETTINGS_NAME);
8489 return -EINVAL;
8490 }
8491 if (!private->flash_segment_nums[SCARLETT2_SEGMENT_ID_FIRMWARE]) {
8492 usb_audio_err(mixer->chip,
8493 "failed to find flash segment %s\n",
8494 SCARLETT2_SEGMENT_FIRMWARE_NAME);
8495 return -EINVAL;
8496 }
8497
8498 return 0;
8499 }
8500
8501 /* Read configuration from the interface on start */
scarlett2_read_configs(struct usb_mixer_interface * mixer)8502 static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
8503 {
8504 struct scarlett2_data *private = mixer->private_data;
8505 const struct scarlett2_device_info *info = private->info;
8506 int err, i;
8507
8508 if (scarlett2_has_config_item(private, SCARLETT2_CONFIG_MSD_SWITCH)) {
8509 err = scarlett2_usb_get_config(
8510 mixer, SCARLETT2_CONFIG_MSD_SWITCH,
8511 1, &private->msd_switch);
8512 if (err < 0)
8513 return err;
8514 }
8515
8516 if (private->firmware_version < info->min_firmware_version) {
8517 usb_audio_err(mixer->chip,
8518 "Focusrite %s firmware version %d is too old; "
8519 "need %d",
8520 private->series_name,
8521 private->firmware_version,
8522 info->min_firmware_version);
8523 return 0;
8524 }
8525
8526 /* no other controls are created if MSD mode is on */
8527 if (private->msd_switch)
8528 return 0;
8529
8530 err = scarlett2_update_input_level(mixer);
8531 if (err < 0)
8532 return err;
8533
8534 err = scarlett2_update_input_pad(mixer);
8535 if (err < 0)
8536 return err;
8537
8538 err = scarlett2_update_input_air(mixer);
8539 if (err < 0)
8540 return err;
8541
8542 err = scarlett2_update_input_dsp(mixer);
8543 if (err < 0)
8544 return err;
8545
8546 err = scarlett2_update_compressor_values(mixer);
8547 if (err < 0)
8548 return err;
8549
8550 err = scarlett2_update_filter_values(mixer);
8551 if (err < 0)
8552 return err;
8553
8554 err = scarlett2_update_input_mute(mixer);
8555 if (err < 0)
8556 return err;
8557
8558 err = scarlett2_update_input_phantom(mixer);
8559 if (err < 0)
8560 return err;
8561
8562 err = scarlett2_update_direct_monitor(mixer);
8563 if (err < 0)
8564 return err;
8565
8566 /* the rest of the configuration is for devices with a mixer */
8567 if (!scarlett2_has_mixer(private))
8568 return 0;
8569
8570 err = scarlett2_update_monitor_mix(mixer);
8571 if (err < 0)
8572 return err;
8573
8574 err = scarlett2_update_monitor_other(mixer);
8575 if (err < 0)
8576 return err;
8577
8578 if (scarlett2_has_config_item(private,
8579 SCARLETT2_CONFIG_STANDALONE_SWITCH)) {
8580 err = scarlett2_usb_get_config(
8581 mixer, SCARLETT2_CONFIG_STANDALONE_SWITCH,
8582 1, &private->standalone_switch);
8583 if (err < 0)
8584 return err;
8585 }
8586
8587 if (scarlett2_has_config_item(private,
8588 SCARLETT2_CONFIG_POWER_EXT)) {
8589 err = scarlett2_update_power_status(mixer);
8590 if (err < 0)
8591 return err;
8592 }
8593
8594 err = scarlett2_update_sync(mixer);
8595 if (err < 0)
8596 return err;
8597
8598 if (scarlett2_has_config_item(private,
8599 SCARLETT2_CONFIG_LINE_OUT_VOLUME)) {
8600 s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
8601
8602 /* read SW line out volume */
8603 err = scarlett2_usb_get_config(
8604 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
8605 private->num_line_out, &sw_vol);
8606 if (err < 0)
8607 return err;
8608
8609 for (i = 0; i < private->num_line_out; i++)
8610 private->vol[i] = clamp(
8611 sw_vol[i] + SCARLETT2_VOLUME_BIAS,
8612 0, SCARLETT2_VOLUME_BIAS);
8613
8614 /* read SW mute */
8615 err = scarlett2_usb_get_config(
8616 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
8617 private->num_line_out, &private->mute_switch);
8618 if (err < 0)
8619 return err;
8620
8621 for (i = 0; i < private->num_line_out; i++)
8622 private->mute_switch[i] =
8623 !!private->mute_switch[i];
8624
8625 /* read SW/HW switches */
8626 if (scarlett2_has_config_item(private,
8627 SCARLETT2_CONFIG_SW_HW_SWITCH)) {
8628 err = scarlett2_usb_get_config(
8629 mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
8630 private->num_line_out,
8631 &private->vol_sw_hw_switch);
8632 if (err < 0)
8633 return err;
8634
8635 for (i = 0; i < private->num_line_out; i++)
8636 private->vol_sw_hw_switch[i] =
8637 !!private->vol_sw_hw_switch[i];
8638 }
8639 }
8640
8641 err = scarlett2_update_volumes(mixer);
8642 if (err < 0)
8643 return err;
8644
8645 err = scarlett2_update_dim_mute(mixer);
8646 if (err < 0)
8647 return err;
8648
8649 err = scarlett2_update_input_select(mixer);
8650 if (err < 0)
8651 return err;
8652
8653 err = scarlett2_update_input_gain(mixer);
8654 if (err < 0)
8655 return err;
8656
8657 err = scarlett2_update_autogain(mixer);
8658 if (err < 0)
8659 return err;
8660
8661 err = scarlett2_update_input_safe(mixer);
8662 if (err < 0)
8663 return err;
8664
8665 if (scarlett2_has_config_item(private,
8666 SCARLETT2_CONFIG_PCM_INPUT_SWITCH)) {
8667 err = scarlett2_update_pcm_input_switch(mixer);
8668 if (err < 0)
8669 return err;
8670 }
8671
8672 err = scarlett2_update_bluetooth_volume(mixer);
8673 if (err < 0)
8674 return err;
8675
8676 err = scarlett2_update_spdif_mode(mixer);
8677 if (err < 0)
8678 return err;
8679
8680 err = scarlett2_update_mix(mixer);
8681 if (err < 0)
8682 return err;
8683
8684 return scarlett2_usb_get_mux(mixer);
8685 }
8686
get_scarlett2_device_entry(struct usb_mixer_interface * mixer)8687 static const struct scarlett2_device_entry *get_scarlett2_device_entry(
8688 struct usb_mixer_interface *mixer)
8689 {
8690 const struct scarlett2_device_entry *entry = scarlett2_devices;
8691
8692 /* Find entry in scarlett2_devices */
8693 while (entry->usb_id && entry->usb_id != mixer->chip->usb_id)
8694 entry++;
8695 if (!entry->usb_id)
8696 return NULL;
8697
8698 return entry;
8699 }
8700
snd_scarlett2_controls_create(struct usb_mixer_interface * mixer,const struct scarlett2_device_entry * entry)8701 static int snd_scarlett2_controls_create(
8702 struct usb_mixer_interface *mixer,
8703 const struct scarlett2_device_entry *entry)
8704 {
8705 struct scarlett2_data *private;
8706 int err;
8707
8708 /* Initialise private data */
8709 err = scarlett2_init_private(mixer, entry);
8710 if (err < 0)
8711 return err;
8712
8713 private = mixer->private_data;
8714
8715 /* Send proprietary USB initialisation sequence */
8716 err = scarlett2_usb_init(mixer);
8717 if (err < 0)
8718 return err;
8719
8720 /* Get the upgrade & settings flash segment numbers */
8721 err = scarlett2_get_flash_segment_nums(mixer);
8722 if (err < 0)
8723 return err;
8724
8725 /* Add firmware version control */
8726 err = scarlett2_add_firmware_version_ctl(mixer);
8727 if (err < 0)
8728 return err;
8729
8730 /* Add minimum firmware version control */
8731 err = scarlett2_add_min_firmware_version_ctl(mixer);
8732 if (err < 0)
8733 return err;
8734
8735 /* Read volume levels and controls from the interface */
8736 err = scarlett2_read_configs(mixer);
8737 if (err < 0)
8738 return err;
8739
8740 /* Create the MSD control */
8741 err = scarlett2_add_msd_ctl(mixer);
8742 if (err < 0)
8743 return err;
8744
8745 /* If MSD mode is enabled, or if the firmware version is too
8746 * old, don't create any other controls
8747 */
8748 if (private->msd_switch ||
8749 private->firmware_version < private->info->min_firmware_version)
8750 return 0;
8751
8752 /* Create the analogue output controls */
8753 err = scarlett2_add_line_out_ctls(mixer);
8754 if (err < 0)
8755 return err;
8756
8757 /* Create the analogue input controls */
8758 err = scarlett2_add_line_in_ctls(mixer);
8759 if (err < 0)
8760 return err;
8761
8762 /* Create the input, output, and mixer mux input selections */
8763 err = scarlett2_add_mux_enums(mixer);
8764 if (err < 0)
8765 return err;
8766
8767 /* Create the matrix mixer controls */
8768 err = scarlett2_add_mixer_ctls(mixer);
8769 if (err < 0)
8770 return err;
8771
8772 /* Create the level meter controls */
8773 err = scarlett2_add_meter_ctl(mixer);
8774 if (err < 0)
8775 return err;
8776
8777 /* Create the sync control */
8778 err = scarlett2_add_sync_ctl(mixer);
8779 if (err < 0)
8780 return err;
8781
8782 /* Create the direct monitor control(s) */
8783 err = scarlett2_add_direct_monitor_ctls(mixer);
8784 if (err < 0)
8785 return err;
8786
8787 /* Create the speaker switching control */
8788 err = scarlett2_add_speaker_switch_ctl(mixer);
8789 if (err < 0)
8790 return err;
8791
8792 /* Create the talkback controls */
8793 err = scarlett2_add_talkback_ctls(mixer);
8794 if (err < 0)
8795 return err;
8796
8797 /* Create the standalone control */
8798 err = scarlett2_add_standalone_ctl(mixer);
8799 if (err < 0)
8800 return err;
8801
8802 /* Create the power status control */
8803 err = scarlett2_add_power_status_ctl(mixer);
8804 if (err < 0)
8805 return err;
8806
8807 /* Create the Bluetooth volume control */
8808 err = scarlett2_add_bluetooth_volume_ctl(mixer);
8809 if (err < 0)
8810 return err;
8811
8812 /* Create the S/PDIF mode control */
8813 err = scarlett2_add_spdif_mode_ctl(mixer);
8814 if (err < 0)
8815 return err;
8816
8817 /* Set the access mode of controls disabled during
8818 * autogain/phantom power switching.
8819 */
8820 if (private->info->gain_input_count) {
8821 scarlett2_autogain_update_access(mixer);
8822 scarlett2_phantom_update_access(mixer);
8823 }
8824
8825 /* Start handling all notifications */
8826 private->running = 2;
8827
8828 return 0;
8829 }
8830
8831 /*** hwdep interface ***/
8832
8833 /* Set private->hwdep_in_use; prevents access to the ALSA controls
8834 * while doing a config erase/firmware upgrade.
8835 */
scarlett2_lock(struct scarlett2_data * private)8836 static void scarlett2_lock(struct scarlett2_data *private)
8837 {
8838 guard(mutex)(&private->data_mutex);
8839 private->hwdep_in_use = 1;
8840 }
8841
8842 /* Call SCARLETT2_USB_GET_ERASE to get the erase progress */
scarlett2_get_erase_progress(struct usb_mixer_interface * mixer)8843 static int scarlett2_get_erase_progress(struct usb_mixer_interface *mixer)
8844 {
8845 struct scarlett2_data *private = mixer->private_data;
8846 int segment_id, segment_num, err;
8847 u8 erase_resp;
8848
8849 struct {
8850 __le32 segment_num;
8851 __le32 pad;
8852 } __packed erase_req;
8853
8854 segment_id = private->selected_flash_segment_id;
8855 segment_num = private->flash_segment_nums[segment_id];
8856
8857 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
8858 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
8859 return -EFAULT;
8860
8861 /* Send the erase progress request */
8862 erase_req.segment_num = cpu_to_le32(segment_num);
8863 erase_req.pad = 0;
8864
8865 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_ERASE,
8866 &erase_req, sizeof(erase_req),
8867 &erase_resp, sizeof(erase_resp));
8868 if (err < 0)
8869 return err;
8870
8871 return erase_resp;
8872 }
8873
8874 /* Repeatedly call scarlett2_get_erase_progress() until it returns
8875 * 0xff (erase complete) or we've waited 10 seconds (it usually takes
8876 * <3 seconds).
8877 */
scarlett2_wait_for_erase(struct usb_mixer_interface * mixer)8878 static int scarlett2_wait_for_erase(struct usb_mixer_interface *mixer)
8879 {
8880 int i, err;
8881
8882 for (i = 0; i < 100; i++) {
8883 err = scarlett2_get_erase_progress(mixer);
8884 if (err < 0)
8885 return err;
8886
8887 if (err == 0xff)
8888 return 0;
8889
8890 msleep(100);
8891 }
8892
8893 return -ETIMEDOUT;
8894 }
8895
8896 /* Reboot the device; wait for the erase to complete if one is in
8897 * progress.
8898 */
scarlett2_reboot(struct usb_mixer_interface * mixer)8899 static int scarlett2_reboot(struct usb_mixer_interface *mixer)
8900 {
8901 struct scarlett2_data *private = mixer->private_data;
8902
8903 if (private->flash_write_state ==
8904 SCARLETT2_FLASH_WRITE_STATE_ERASING) {
8905 int err = scarlett2_wait_for_erase(mixer);
8906
8907 if (err < 0)
8908 return err;
8909 }
8910
8911 return scarlett2_usb(mixer, SCARLETT2_USB_REBOOT, NULL, 0, NULL, 0);
8912 }
8913
8914 /* Select a flash segment for reading/erasing/writing */
scarlett2_ioctl_select_flash_segment(struct usb_mixer_interface * mixer,unsigned long arg)8915 static int scarlett2_ioctl_select_flash_segment(
8916 struct usb_mixer_interface *mixer,
8917 unsigned long arg)
8918 {
8919 struct scarlett2_data *private = mixer->private_data;
8920 int segment_id, segment_num;
8921
8922 if (get_user(segment_id, (int __user *)arg))
8923 return -EFAULT;
8924
8925 /* Check the segment ID and segment number */
8926 if (segment_id < 0 || segment_id >= SCARLETT2_SEGMENT_ID_COUNT)
8927 return -EINVAL;
8928
8929 segment_num = private->flash_segment_nums[segment_id];
8930 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
8931 segment_num > SCARLETT2_SEGMENT_NUM_MAX) {
8932 usb_audio_err(mixer->chip,
8933 "%s: invalid segment number %d\n",
8934 __func__, segment_id);
8935 return -EFAULT;
8936 }
8937
8938 /* If erasing, wait for it to complete */
8939 if (private->flash_write_state == SCARLETT2_FLASH_WRITE_STATE_ERASING) {
8940 int err = scarlett2_wait_for_erase(mixer);
8941
8942 if (err < 0)
8943 return err;
8944 }
8945
8946 /* Save the selected segment ID and set the state to SELECTED */
8947 private->selected_flash_segment_id = segment_id;
8948 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_SELECTED;
8949
8950 return 0;
8951 }
8952
8953 /* Erase the previously-selected flash segment */
scarlett2_ioctl_erase_flash_segment(struct usb_mixer_interface * mixer)8954 static int scarlett2_ioctl_erase_flash_segment(
8955 struct usb_mixer_interface *mixer)
8956 {
8957 struct scarlett2_data *private = mixer->private_data;
8958 int segment_id, segment_num, err;
8959
8960 struct {
8961 __le32 segment_num;
8962 __le32 pad;
8963 } __packed erase_req;
8964
8965 if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_SELECTED)
8966 return -EINVAL;
8967
8968 segment_id = private->selected_flash_segment_id;
8969 segment_num = private->flash_segment_nums[segment_id];
8970
8971 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
8972 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
8973 return -EFAULT;
8974
8975 /* Prevent access to ALSA controls that access the device from
8976 * here on
8977 */
8978 scarlett2_lock(private);
8979
8980 /* Send the erase request */
8981 erase_req.segment_num = cpu_to_le32(segment_num);
8982 erase_req.pad = 0;
8983
8984 err = scarlett2_usb(mixer, SCARLETT2_USB_ERASE_SEGMENT,
8985 &erase_req, sizeof(erase_req),
8986 NULL, 0);
8987 if (err < 0)
8988 return err;
8989
8990 /* On success, change the state from SELECTED to ERASING */
8991 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_ERASING;
8992
8993 return 0;
8994 }
8995
8996 /* Get the erase progress from the device */
scarlett2_ioctl_get_erase_progress(struct usb_mixer_interface * mixer,unsigned long arg)8997 static int scarlett2_ioctl_get_erase_progress(
8998 struct usb_mixer_interface *mixer,
8999 unsigned long arg)
9000 {
9001 struct scarlett2_data *private = mixer->private_data;
9002 struct scarlett2_flash_segment_erase_progress progress;
9003 int segment_id, segment_num, err;
9004 u8 erase_resp;
9005
9006 struct {
9007 __le32 segment_num;
9008 __le32 pad;
9009 } __packed erase_req;
9010
9011 /* Check that we're erasing */
9012 if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_ERASING)
9013 return -EINVAL;
9014
9015 segment_id = private->selected_flash_segment_id;
9016 segment_num = private->flash_segment_nums[segment_id];
9017
9018 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
9019 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
9020 return -EFAULT;
9021
9022 /* Send the erase progress request */
9023 erase_req.segment_num = cpu_to_le32(segment_num);
9024 erase_req.pad = 0;
9025
9026 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_ERASE,
9027 &erase_req, sizeof(erase_req),
9028 &erase_resp, sizeof(erase_resp));
9029 if (err < 0)
9030 return err;
9031
9032 progress.progress = erase_resp;
9033 progress.num_blocks = private->flash_segment_blocks[segment_id];
9034
9035 if (copy_to_user((void __user *)arg, &progress, sizeof(progress)))
9036 return -EFAULT;
9037
9038 /* If the erase is complete, change the state from ERASING to
9039 * WRITE.
9040 */
9041 if (progress.progress == 0xff)
9042 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_WRITE;
9043
9044 return 0;
9045 }
9046
scarlett2_hwdep_open(struct snd_hwdep * hw,struct file * file)9047 static int scarlett2_hwdep_open(struct snd_hwdep *hw, struct file *file)
9048 {
9049 struct usb_mixer_interface *mixer = hw->private_data;
9050 struct scarlett2_data *private = mixer->private_data;
9051
9052 /* If erasing, wait for it to complete */
9053 if (private->flash_write_state ==
9054 SCARLETT2_FLASH_WRITE_STATE_ERASING) {
9055 int err = scarlett2_wait_for_erase(mixer);
9056
9057 if (err < 0)
9058 return err;
9059 }
9060
9061 /* Set the state to IDLE */
9062 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_IDLE;
9063
9064 return 0;
9065 }
9066
scarlett2_hwdep_ioctl(struct snd_hwdep * hw,struct file * file,unsigned int cmd,unsigned long arg)9067 static int scarlett2_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
9068 unsigned int cmd, unsigned long arg)
9069 {
9070 struct usb_mixer_interface *mixer = hw->private_data;
9071
9072 switch (cmd) {
9073
9074 case SCARLETT2_IOCTL_PVERSION:
9075 return put_user(SCARLETT2_HWDEP_VERSION,
9076 (int __user *)arg) ? -EFAULT : 0;
9077
9078 case SCARLETT2_IOCTL_REBOOT:
9079 return scarlett2_reboot(mixer);
9080
9081 case SCARLETT2_IOCTL_SELECT_FLASH_SEGMENT:
9082 return scarlett2_ioctl_select_flash_segment(mixer, arg);
9083
9084 case SCARLETT2_IOCTL_ERASE_FLASH_SEGMENT:
9085 return scarlett2_ioctl_erase_flash_segment(mixer);
9086
9087 case SCARLETT2_IOCTL_GET_ERASE_PROGRESS:
9088 return scarlett2_ioctl_get_erase_progress(mixer, arg);
9089
9090 default:
9091 return -ENOIOCTLCMD;
9092 }
9093 }
9094
scarlett2_hwdep_read(struct snd_hwdep * hw,char __user * buf,long count,loff_t * offset)9095 static long scarlett2_hwdep_read(struct snd_hwdep *hw,
9096 char __user *buf,
9097 long count, loff_t *offset)
9098 {
9099 struct usb_mixer_interface *mixer = hw->private_data;
9100 struct scarlett2_data *private = mixer->private_data;
9101 int segment_id, segment_num, err;
9102 int flash_size;
9103
9104 /* SCARLETT2_USB_READ_SEGMENT request data */
9105 struct {
9106 __le32 segment_num;
9107 __le32 offset;
9108 __le32 len;
9109 } __packed req;
9110
9111 /* Flash segment must first be selected */
9112 if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_SELECTED)
9113 return -EINVAL;
9114
9115 /* Get the selected flash segment number */
9116 segment_id = private->selected_flash_segment_id;
9117 if (segment_id < 0 || segment_id >= SCARLETT2_SEGMENT_ID_COUNT)
9118 return -EINVAL;
9119
9120 segment_num = private->flash_segment_nums[segment_id];
9121 if (segment_num < 0 ||
9122 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
9123 return -EFAULT;
9124
9125 /* Validate the offset and count */
9126 if (count < 0 || *offset < 0)
9127 return -EINVAL;
9128
9129 /* Reached EOF? */
9130 flash_size = private->flash_segment_blocks[segment_id] *
9131 SCARLETT2_FLASH_BLOCK_SIZE;
9132 if (!count || *offset >= flash_size)
9133 return 0;
9134
9135 /* Limit the numbers of bytes read to SCARLETT2_FLASH_RW_MAX */
9136 if (count > SCARLETT2_FLASH_RW_MAX)
9137 count = SCARLETT2_FLASH_RW_MAX;
9138
9139 /* Limit read to EOF */
9140 if (*offset + count >= flash_size)
9141 count = flash_size - *offset;
9142
9143 /* Create and send the request */
9144 req.segment_num = cpu_to_le32(segment_num);
9145 req.offset = cpu_to_le32(*offset);
9146 req.len = cpu_to_le32(count);
9147
9148 u8 *resp __free(kfree) =
9149 kzalloc(count, GFP_KERNEL);
9150 if (!resp)
9151 return -ENOMEM;
9152
9153 err = scarlett2_usb(mixer, SCARLETT2_USB_READ_SEGMENT,
9154 &req, sizeof(req), resp, count);
9155 if (err < 0)
9156 return err;
9157
9158 /* Copy the response to userspace */
9159 if (copy_to_user(buf, resp, count))
9160 return -EFAULT;
9161
9162 *offset += count;
9163 return count;
9164 }
9165
scarlett2_hwdep_write(struct snd_hwdep * hw,const char __user * buf,long count,loff_t * offset)9166 static long scarlett2_hwdep_write(struct snd_hwdep *hw,
9167 const char __user *buf,
9168 long count, loff_t *offset)
9169 {
9170 struct usb_mixer_interface *mixer = hw->private_data;
9171 struct scarlett2_data *private = mixer->private_data;
9172 int segment_id, segment_num, err, len;
9173 int flash_size;
9174
9175 /* SCARLETT2_USB_WRITE_SEGMENT request data */
9176 struct {
9177 __le32 segment_num;
9178 __le32 offset;
9179 __le32 pad;
9180 u8 data[];
9181 } __packed *req __free(kfree) = NULL;
9182
9183 /* Calculate the maximum permitted in data[] */
9184 const size_t max_data_size = SCARLETT2_FLASH_RW_MAX -
9185 offsetof(typeof(*req), data);
9186
9187 /* If erasing, wait for it to complete */
9188 if (private->flash_write_state ==
9189 SCARLETT2_FLASH_WRITE_STATE_ERASING) {
9190 err = scarlett2_wait_for_erase(mixer);
9191 if (err < 0)
9192 return err;
9193 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_WRITE;
9194
9195 /* Check that an erase has been done & completed */
9196 } else if (private->flash_write_state !=
9197 SCARLETT2_FLASH_WRITE_STATE_WRITE) {
9198 return -EINVAL;
9199 }
9200
9201 /* Check that we're writing to the upgrade firmware */
9202 segment_id = private->selected_flash_segment_id;
9203 if (segment_id != SCARLETT2_SEGMENT_ID_FIRMWARE)
9204 return -EINVAL;
9205
9206 segment_num = private->flash_segment_nums[segment_id];
9207 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
9208 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
9209 return -EFAULT;
9210
9211 /* Validate the offset and count */
9212 flash_size = private->flash_segment_blocks[segment_id] *
9213 SCARLETT2_FLASH_BLOCK_SIZE;
9214
9215 if (count < 0 || *offset < 0)
9216 return -EINVAL;
9217
9218 if (!count)
9219 return 0;
9220
9221 if (*offset >= flash_size || count > flash_size - *offset)
9222 return -ENOSPC;
9223
9224 /* Limit the *req size to SCARLETT2_FLASH_RW_MAX */
9225 if (count > max_data_size)
9226 count = max_data_size;
9227
9228 /* Create and send the request */
9229 len = struct_size(req, data, count);
9230 req = kzalloc(len, GFP_KERNEL);
9231 if (!req)
9232 return -ENOMEM;
9233
9234 req->segment_num = cpu_to_le32(segment_num);
9235 req->offset = cpu_to_le32(*offset);
9236 req->pad = 0;
9237
9238 if (copy_from_user(req->data, buf, count))
9239 return -EFAULT;
9240
9241 err = scarlett2_usb(mixer, SCARLETT2_USB_WRITE_SEGMENT,
9242 req, len, NULL, 0);
9243 if (err < 0)
9244 return err;
9245
9246 *offset += count;
9247 return count;
9248 }
9249
scarlett2_hwdep_release(struct snd_hwdep * hw,struct file * file)9250 static int scarlett2_hwdep_release(struct snd_hwdep *hw, struct file *file)
9251 {
9252 struct usb_mixer_interface *mixer = hw->private_data;
9253 struct scarlett2_data *private = mixer->private_data;
9254
9255 /* Return from the SELECTED or WRITE state to IDLE.
9256 * The ERASING state is left as-is, and checked on next open.
9257 */
9258 if (private &&
9259 private->hwdep_in_use &&
9260 private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_ERASING)
9261 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_IDLE;
9262
9263 return 0;
9264 }
9265
scarlett2_hwdep_init(struct usb_mixer_interface * mixer)9266 static int scarlett2_hwdep_init(struct usb_mixer_interface *mixer)
9267 {
9268 struct snd_hwdep *hw;
9269 int err;
9270
9271 err = snd_hwdep_new(mixer->chip->card, "Focusrite Control", 0, &hw);
9272 if (err < 0)
9273 return err;
9274
9275 hw->private_data = mixer;
9276 hw->exclusive = 1;
9277 hw->ops.open = scarlett2_hwdep_open;
9278 hw->ops.ioctl = scarlett2_hwdep_ioctl;
9279 hw->ops.read = scarlett2_hwdep_read;
9280 hw->ops.write = scarlett2_hwdep_write;
9281 hw->ops.release = scarlett2_hwdep_release;
9282
9283 return 0;
9284 }
9285
9286 /*** device-map file ***/
9287
scarlett2_devmap_read(struct snd_info_entry * entry,void * file_private_data,struct file * file,char __user * buf,size_t count,loff_t pos)9288 static ssize_t scarlett2_devmap_read(
9289 struct snd_info_entry *entry,
9290 void *file_private_data,
9291 struct file *file,
9292 char __user *buf,
9293 size_t count,
9294 loff_t pos)
9295 {
9296 struct usb_mixer_interface *mixer = entry->private_data;
9297 const size_t block_size = SCARLETT2_DEVMAP_BLOCK_SIZE;
9298 size_t copied = 0;
9299
9300 if (pos >= entry->size)
9301 return 0;
9302
9303 if (pos + count > entry->size)
9304 count = entry->size - pos;
9305
9306 u8 *resp_buf __free(kfree) =
9307 kmalloc(block_size, GFP_KERNEL);
9308 if (!resp_buf)
9309 return -ENOMEM;
9310
9311 while (count > 0) {
9312 /* SCARLETT2_USB_GET_DEVMAP reads only on block boundaries,
9313 * so we need to read a whole block and copy the requested
9314 * chunk to userspace.
9315 */
9316
9317 __le32 req;
9318 int err;
9319
9320 /* offset within the block that we're reading */
9321 size_t offset = pos % block_size;
9322
9323 /* read_size is block_size except for the last block */
9324 size_t block_start = pos - offset;
9325 size_t read_size = min_t(size_t,
9326 block_size,
9327 entry->size - block_start);
9328
9329 /* size of the chunk to copy to userspace */
9330 size_t copy_size = min_t(size_t, count, read_size - offset);
9331
9332 /* request the block */
9333 req = cpu_to_le32(pos / block_size);
9334 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_DEVMAP,
9335 &req, sizeof(req), resp_buf, read_size);
9336 if (err < 0)
9337 return copied ? copied : err;
9338
9339 if (copy_to_user(buf, resp_buf + offset, copy_size))
9340 return -EFAULT;
9341
9342 buf += copy_size;
9343 pos += copy_size;
9344 copied += copy_size;
9345 count -= copy_size;
9346 }
9347
9348 return copied;
9349 }
9350
9351 static const struct snd_info_entry_ops scarlett2_devmap_ops = {
9352 .read = scarlett2_devmap_read,
9353 };
9354
scarlett2_devmap_init(struct usb_mixer_interface * mixer)9355 static int scarlett2_devmap_init(struct usb_mixer_interface *mixer)
9356 {
9357 struct snd_card *card = mixer->chip->card;
9358 struct scarlett2_data *private = mixer->private_data;
9359 const struct scarlett2_device_info *info = private->info;
9360 __le16 config_len_buf[2];
9361 int config_len;
9362 struct snd_info_entry *entry;
9363 int err;
9364
9365 /* If the device doesn't support the DEVMAP commands, don't
9366 * create the /proc/asound/cardX/scarlett.json.zlib entry
9367 */
9368 if (!info->has_devmap)
9369 return 0;
9370
9371 err = scarlett2_usb(mixer, SCARLETT2_USB_INFO_DEVMAP,
9372 NULL, 0, &config_len_buf, sizeof(config_len_buf));
9373 if (err < 0)
9374 return err;
9375
9376 config_len = le16_to_cpu(config_len_buf[1]);
9377
9378 err = snd_card_proc_new(card, SCARLETT2_DEVMAP_FILENAME, &entry);
9379 if (err < 0)
9380 return err;
9381
9382 entry->content = SNDRV_INFO_CONTENT_DATA;
9383 entry->private_data = mixer;
9384 entry->c.ops = &scarlett2_devmap_ops;
9385 entry->size = config_len;
9386 entry->mode = S_IFREG | 0444;
9387
9388 return 0;
9389 }
9390
snd_scarlett2_init(struct usb_mixer_interface * mixer)9391 int snd_scarlett2_init(struct usb_mixer_interface *mixer)
9392 {
9393 struct snd_usb_audio *chip = mixer->chip;
9394 const struct scarlett2_device_entry *entry;
9395 int err;
9396
9397 /* only use UAC_VERSION_2 */
9398 if (!mixer->protocol)
9399 return 0;
9400
9401 /* check if the user wants to use the FCP driver instead */
9402 if (chip->setup & SCARLETT2_USE_FCP_DRIVER)
9403 return snd_fcp_init(mixer);
9404
9405 /* find entry in scarlett2_devices */
9406 entry = get_scarlett2_device_entry(mixer);
9407 if (!entry) {
9408 usb_audio_err(mixer->chip,
9409 "%s: missing device entry for %04x:%04x\n",
9410 __func__,
9411 USB_ID_VENDOR(chip->usb_id),
9412 USB_ID_PRODUCT(chip->usb_id));
9413 return 0;
9414 }
9415
9416 if (chip->setup & SCARLETT2_DISABLE) {
9417 usb_audio_info(chip,
9418 "Focusrite %s Mixer Driver disabled "
9419 "by modprobe options (snd_usb_audio "
9420 "vid=0x%04x pid=0x%04x device_setup=%d)\n",
9421 entry->series_name,
9422 USB_ID_VENDOR(chip->usb_id),
9423 USB_ID_PRODUCT(chip->usb_id),
9424 SCARLETT2_DISABLE);
9425 return 0;
9426 }
9427
9428 usb_audio_info(chip,
9429 "Focusrite %s Mixer Driver enabled (pid=0x%04x); "
9430 "report any issues to "
9431 "https://github.com/geoffreybennett/scarlett-gen2/issues",
9432 entry->series_name,
9433 USB_ID_PRODUCT(chip->usb_id));
9434
9435 err = snd_scarlett2_controls_create(mixer, entry);
9436 if (err < 0) {
9437 usb_audio_err(mixer->chip,
9438 "Error initialising %s Mixer Driver: %d",
9439 entry->series_name,
9440 err);
9441 return err;
9442 }
9443
9444 err = scarlett2_hwdep_init(mixer);
9445 if (err < 0) {
9446 usb_audio_err(mixer->chip,
9447 "Error creating %s hwdep device: %d",
9448 entry->series_name,
9449 err);
9450 return err;
9451 }
9452
9453 err = scarlett2_devmap_init(mixer);
9454 if (err < 0)
9455 usb_audio_err(mixer->chip,
9456 "Error creating %s devmap entry: %d",
9457 entry->series_name,
9458 err);
9459
9460 return err;
9461 }
9462