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