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