File Index
Files and directories
Sign in
FILE

mixer_scarlett2.c

File preview with syntax highlighting.

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