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