Linux Audio

Check our new training course

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