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